All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] multipathd: fix memory allocation logic error for polls in uxsock_listen()
@ 2016-08-10  9:32 tang.junhui
  2016-08-10  9:42 ` Hannes Reinecke
  0 siblings, 1 reply; 3+ messages in thread
From: tang.junhui @ 2016-08-10  9:32 UTC (permalink / raw)
  To: christophe varoqui; +Cc: zhang.kai16, dm-devel, tang.junhui

From: "tang.junhui" <tang.junhui@zte.com.cn>

logic error exists in memory allocation for polls in uxsock_listen(), even if the allocated memory size meet the needs, it is still to realloc memory, which is not up to expectations.

Signed-off-by: tang.junhui <tang.junhui@zte.com.cn>
---
 multipathd/uxlsnr.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c
index f114e59..fa29b2a 100644
--- a/multipathd/uxlsnr.c
+++ b/multipathd/uxlsnr.c
@@ -145,7 +145,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 	pthread_cleanup_push(uxsock_cleanup, NULL);
 
 	condlog(3, "uxsock: startup listener");
-	polls = (struct pollfd *)MALLOC(MIN_POLLS + 1);
+	polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct pollfd));
 	if (!polls) {
 		condlog(0, "uxsock: failed to allocate poll fds");
 		return NULL;
@@ -167,9 +167,11 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 		}
 		if (num_clients != old_clients) {
 			struct pollfd *new;
-			if (num_clients < MIN_POLLS) {
+			if (num_clients <= MIN_POLLS && old_clients > MIN_POLLS) {
 				new = REALLOC(polls, (1 + MIN_POLLS) *
 						sizeof(struct pollfd));
+			} else if (num_clients <= MIN_POLLS && old_clients <= MIN_POLLS) {
+				new = polls;
 			} else {
 				new = REALLOC(polls, (1+num_clients) *
 						sizeof(struct pollfd));
@@ -181,7 +183,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
 				pthread_yield();
 				continue;
 			}
-			num_clients = old_clients;
+			old_clients = num_clients;
 			polls = new;
 		}
 		polls[0].fd = ux_sock;
-- 
2.8.1.windows.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] multipathd: fix memory allocation logic error for polls in uxsock_listen()
  2016-08-10  9:32 [PATCH] multipathd: fix memory allocation logic error for polls in uxsock_listen() tang.junhui
@ 2016-08-10  9:42 ` Hannes Reinecke
  2016-08-10  9:49   ` Christophe Varoqui
  0 siblings, 1 reply; 3+ messages in thread
From: Hannes Reinecke @ 2016-08-10  9:42 UTC (permalink / raw)
  To: tang.junhui, christophe varoqui; +Cc: zhang.kai16, dm-devel

On 08/10/2016 11:32 AM, tang.junhui@zte.com.cn wrote:
> From: "tang.junhui" <tang.junhui@zte.com.cn>
> 
> logic error exists in memory allocation for polls in uxsock_listen(), even if the allocated memory size meet the needs, it is still to realloc memory, which is not up to expectations.
> 
> Signed-off-by: tang.junhui <tang.junhui@zte.com.cn>
> ---
>  multipathd/uxlsnr.c | 8 +++++---
>  1 file changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c
> index f114e59..fa29b2a 100644
> --- a/multipathd/uxlsnr.c
> +++ b/multipathd/uxlsnr.c
> @@ -145,7 +145,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
>  	pthread_cleanup_push(uxsock_cleanup, NULL);
>  
>  	condlog(3, "uxsock: startup listener");
> -	polls = (struct pollfd *)MALLOC(MIN_POLLS + 1);
> +	polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct pollfd));
>  	if (!polls) {
>  		condlog(0, "uxsock: failed to allocate poll fds");
>  		return NULL;
> @@ -167,9 +167,11 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
>  		}
>  		if (num_clients != old_clients) {
>  			struct pollfd *new;
> -			if (num_clients < MIN_POLLS) {
> +			if (num_clients <= MIN_POLLS && old_clients > MIN_POLLS) {
>  				new = REALLOC(polls, (1 + MIN_POLLS) *
>  						sizeof(struct pollfd));
> +			} else if (num_clients <= MIN_POLLS && old_clients <= MIN_POLLS) {
> +				new = polls;
>  			} else {
>  				new = REALLOC(polls, (1+num_clients) *
>  						sizeof(struct pollfd));
> @@ -181,7 +183,7 @@ void * uxsock_listen(uxsock_trigger_fn uxsock_trigger, void * trigger_data)
>  				pthread_yield();
>  				continue;
>  			}
> -			num_clients = old_clients;
> +			old_clients = num_clients;
>  			polls = new;
>  		}
>  		polls[0].fd = ux_sock;
> 
Thanks for catching this.

Reviewed-by: Hannes Reinecke <hare@suse.com>

Cheers,

Hannes
-- 
Dr. Hannes Reinecke		   Teamlead Storage & Networking
hare@suse.de			               +49 911 74053 688
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
HRB 21284 (AG Nürnberg)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] multipathd: fix memory allocation logic error for polls in uxsock_listen()
  2016-08-10  9:42 ` Hannes Reinecke
@ 2016-08-10  9:49   ` Christophe Varoqui
  0 siblings, 0 replies; 3+ messages in thread
From: Christophe Varoqui @ 2016-08-10  9:49 UTC (permalink / raw)
  To: Hannes Reinecke
  Cc: zhang.kai16, device-mapper development, christophe varoqui, tang.junhui


[-- Attachment #1.1: Type: text/plain, Size: 3024 bytes --]

Merged.
Thanks.

On Wed, Aug 10, 2016 at 11:42 AM, Hannes Reinecke <hare@suse.de> wrote:

> On 08/10/2016 11:32 AM, tang.junhui@zte.com.cn wrote:
> > From: "tang.junhui" <tang.junhui@zte.com.cn>
> >
> > logic error exists in memory allocation for polls in uxsock_listen(),
> even if the allocated memory size meet the needs, it is still to realloc
> memory, which is not up to expectations.
> >
> > Signed-off-by: tang.junhui <tang.junhui@zte.com.cn>
> > ---
> >  multipathd/uxlsnr.c | 8 +++++---
> >  1 file changed, 5 insertions(+), 3 deletions(-)
> >
> > diff --git a/multipathd/uxlsnr.c b/multipathd/uxlsnr.c
> > index f114e59..fa29b2a 100644
> > --- a/multipathd/uxlsnr.c
> > +++ b/multipathd/uxlsnr.c
> > @@ -145,7 +145,7 @@ void * uxsock_listen(uxsock_trigger_fn
> uxsock_trigger, void * trigger_data)
> >       pthread_cleanup_push(uxsock_cleanup, NULL);
> >
> >       condlog(3, "uxsock: startup listener");
> > -     polls = (struct pollfd *)MALLOC(MIN_POLLS + 1);
> > +     polls = (struct pollfd *)MALLOC((MIN_POLLS + 1) * sizeof(struct
> pollfd));
> >       if (!polls) {
> >               condlog(0, "uxsock: failed to allocate poll fds");
> >               return NULL;
> > @@ -167,9 +167,11 @@ void * uxsock_listen(uxsock_trigger_fn
> uxsock_trigger, void * trigger_data)
> >               }
> >               if (num_clients != old_clients) {
> >                       struct pollfd *new;
> > -                     if (num_clients < MIN_POLLS) {
> > +                     if (num_clients <= MIN_POLLS && old_clients >
> MIN_POLLS) {
> >                               new = REALLOC(polls, (1 + MIN_POLLS) *
> >                                               sizeof(struct pollfd));
> > +                     } else if (num_clients <= MIN_POLLS && old_clients
> <= MIN_POLLS) {
> > +                             new = polls;
> >                       } else {
> >                               new = REALLOC(polls, (1+num_clients) *
> >                                               sizeof(struct pollfd));
> > @@ -181,7 +183,7 @@ void * uxsock_listen(uxsock_trigger_fn
> uxsock_trigger, void * trigger_data)
> >                               pthread_yield();
> >                               continue;
> >                       }
> > -                     num_clients = old_clients;
> > +                     old_clients = num_clients;
> >                       polls = new;
> >               }
> >               polls[0].fd = ux_sock;
> >
> Thanks for catching this.
>
> Reviewed-by: Hannes Reinecke <hare@suse.com>
>
> Cheers,
>
> Hannes
> --
> Dr. Hannes Reinecke                Teamlead Storage & Networking
> hare@suse.de                                   +49 911 74053 688
> SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
> GF: F. Imendörffer, J. Smithard, J. Guild, D. Upmanyu, G. Norton
> HRB 21284 (AG Nürnberg)
>
> --
> dm-devel mailing list
> dm-devel@redhat.com
> https://www.redhat.com/mailman/listinfo/dm-devel
>

[-- Attachment #1.2: Type: text/html, Size: 4546 bytes --]

[-- Attachment #2: Type: text/plain, Size: 0 bytes --]



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-08-10  9:49 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-10  9:32 [PATCH] multipathd: fix memory allocation logic error for polls in uxsock_listen() tang.junhui
2016-08-10  9:42 ` Hannes Reinecke
2016-08-10  9:49   ` Christophe Varoqui

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.