All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libmultipath: fix memory API logic error
@ 2016-08-15 10:59 tang.junhui
  2016-08-15 13:46 ` Bart Van Assche
  2016-08-15 13:58 ` Johannes Thumshirn
  0 siblings, 2 replies; 7+ messages in thread
From: tang.junhui @ 2016-08-15 10:59 UTC (permalink / raw)
  To: christophe varoqui; +Cc: zhang.kai16, dm-devel, tang.junhui

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

Memroy API use mem_allocated to record the total size of used memory,
however, it's wrong to use size(p) as the length of freed memory in xfree(),
and memory may also be allocated by STRDUP() or REALLOC(), which is
not calculated into mem_allocated, so the total size of used memory is
not correctly. For these reasons, we removed these incorrectly code to keep
the code clean.

Signed-off-by: tang.junhui <tang.junhui@zte.com.cn>
---
 libmpathpersist/mpath_updatepr.c |  1 -
 libmultipath/memory.c            | 31 -------------------------------
 libmultipath/memory.h            | 13 ++-----------
 3 files changed, 2 insertions(+), 43 deletions(-)

diff --git a/libmpathpersist/mpath_updatepr.c b/libmpathpersist/mpath_updatepr.c
index 9ff4b30..5af2e03 100644
--- a/libmpathpersist/mpath_updatepr.c
+++ b/libmpathpersist/mpath_updatepr.c
@@ -16,7 +16,6 @@
 #include "uxsock.h"
 #include "memory.h"
 
-unsigned long mem_allocated;    /* Total memory used in Bytes */
 
 int update_prflag(char * arg1, char * arg2, int noisy)
 {
diff --git a/libmultipath/memory.c b/libmultipath/memory.c
index 1366f45..5441e6a 100644
--- a/libmultipath/memory.c
+++ b/libmultipath/memory.c
@@ -22,37 +22,6 @@
 
 #include "memory.h"
 
-/* Global var */
-unsigned long mem_allocated;	/* Total memory used in Bytes */
-
-void *
-xalloc(unsigned long size)
-{
-	void *mem;
-	if ((mem = malloc(size)))
-		mem_allocated += size;
-	return mem;
-}
-
-void *
-zalloc(unsigned long size)
-{
-	void *mem;
-	if ((mem = malloc(size))) {
-		memset(mem, 0, size);
-		mem_allocated += size;
-	}
-	return mem;
-}
-
-void
-xfree(void *p)
-{
-	mem_allocated -= sizeof (p);
-	free(p);
-	p = NULL;
-}
-
 /*
  * Memory management. in debug mode,
  * help finding eventual memory leak.
diff --git a/libmultipath/memory.h b/libmultipath/memory.h
index 8573f6f..99937b2 100644
--- a/libmultipath/memory.h
+++ b/libmultipath/memory.h
@@ -29,15 +29,6 @@
 #include <string.h>
 #include <assert.h>
 
-/* extern types */
-extern unsigned long mem_allocated;
-extern void *xalloc(unsigned long size);
-extern void *zalloc(unsigned long size);
-extern void xfree(void *p);
-
-/* Global alloc macro */
-#define ALLOC(n) (xalloc(n))
-
 /* Local defines */
 #ifdef _DEBUG_
 
@@ -63,8 +54,8 @@ extern void dbg_free_final(char *);
 
 #else
 
-#define MALLOC(n)    (zalloc(n))
-#define FREE(p)      (xfree(p))
+#define MALLOC(n)    (calloc(1,(n)))
+#define FREE(p)      (free(p))
 #define REALLOC(p,n) (realloc((p),(n)))
 #define STRDUP(n)    (strdup(n))
 
-- 
2.8.1.windows.1

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

* Re: [PATCH] libmultipath: fix memory API logic error
  2016-08-15 10:59 [PATCH] libmultipath: fix memory API logic error tang.junhui
@ 2016-08-15 13:46 ` Bart Van Assche
  2016-08-15 13:58 ` Johannes Thumshirn
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Van Assche @ 2016-08-15 13:46 UTC (permalink / raw)
  To: tang.junhui, christophe varoqui; +Cc: zhang.kai16, dm-devel

On 08/15/16 03:59, tang.junhui@zte.com.cn wrote:
> From: "tang.junhui" <tang.junhui@zte.com.cn>
>
> Memroy API use mem_allocated to record the total size of used memory,
> however, it's wrong to use size(p) as the length of freed memory in xfree(),
> and memory may also be allocated by STRDUP() or REALLOC(), which is
> not calculated into mem_allocated, so the total size of used memory is
> not correctly. For these reasons, we removed these incorrectly code to keep
> the code clean.

Nice work. BTW, I think it's worth mentioning that this patch also fixes 
a data race. Without this patch the global variable mem_allocated is 
manipulated by multiple threads without using atomics and without 
serializing concurrent accesses.

Bart.

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

* Re: [PATCH] libmultipath: fix memory API logic error
  2016-08-15 10:59 [PATCH] libmultipath: fix memory API logic error tang.junhui
  2016-08-15 13:46 ` Bart Van Assche
@ 2016-08-15 13:58 ` Johannes Thumshirn
  2016-08-16  0:51   ` 答复: " tang.junhui
  1 sibling, 1 reply; 7+ messages in thread
From: Johannes Thumshirn @ 2016-08-15 13:58 UTC (permalink / raw)
  To: tang.junhui; +Cc: zhang.kai16, dm-devel, christophe varoqui

On Mon, Aug 15, 2016 at 06:59:09PM +0800, tang.junhui@zte.com.cn wrote:
> From: "tang.junhui" <tang.junhui@zte.com.cn>
> 
> Memroy API use mem_allocated to record the total size of used memory,
> however, it's wrong to use size(p) as the length of freed memory in xfree(),
> and memory may also be allocated by STRDUP() or REALLOC(), which is
> not calculated into mem_allocated, so the total size of used memory is
> not correctly. For these reasons, we removed these incorrectly code to keep
> the code clean.
> 
> Signed-off-by: tang.junhui <tang.junhui@zte.com.cn>
> ---
>  libmpathpersist/mpath_updatepr.c |  1 -
>  libmultipath/memory.c            | 31 -------------------------------
>  libmultipath/memory.h            | 13 ++-----------
>  3 files changed, 2 insertions(+), 43 deletions(-)
> 
> diff --git a/libmpathpersist/mpath_updatepr.c b/libmpathpersist/mpath_updatepr.c
> index 9ff4b30..5af2e03 100644
> --- a/libmpathpersist/mpath_updatepr.c
> +++ b/libmpathpersist/mpath_updatepr.c
> @@ -16,7 +16,6 @@
>  #include "uxsock.h"
>  #include "memory.h"
>  
> -unsigned long mem_allocated;    /* Total memory used in Bytes */
>  
>  int update_prflag(char * arg1, char * arg2, int noisy)
>  {
> diff --git a/libmultipath/memory.c b/libmultipath/memory.c
> index 1366f45..5441e6a 100644
> --- a/libmultipath/memory.c
> +++ b/libmultipath/memory.c
> @@ -22,37 +22,6 @@
>

[...]

> -
> -void
> -xfree(void *p)
> -{
> -	mem_allocated -= sizeof (p);
> -	free(p);
> -	p = NULL;
> -}
> -
>  /*

[...]

> -#define MALLOC(n)    (zalloc(n))
> -#define FREE(p)      (xfree(p))
> +#define MALLOC(n)    (calloc(1,(n)))
> +#define FREE(p)      (free(p))
>  #define REALLOC(p,n) (realloc((p),(n)))
>  #define STRDUP(n)    (strdup(n))

One minor nit, the original xfree() has set the freed pointer to 0, so maybe a

#define FREE(p) do { free(p); p = NULL } while(0)

would be advisable. I personally like setting free'd pointers to NULL,
as a NULL pointer access is a more immediate feedback than some rather
nasty use-after-free.

Just my $0.2

Byte,
	Johannes

-- 
Johannes Thumshirn                                          Storage
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850

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

* 答复: Re:  [PATCH] libmultipath: fix memory API logic error
  2016-08-15 13:58 ` Johannes Thumshirn
@ 2016-08-16  0:51   ` tang.junhui
  0 siblings, 0 replies; 7+ messages in thread
From: tang.junhui @ 2016-08-16  0:51 UTC (permalink / raw)
  To: Johannes Thumshirn; +Cc: zhang.kai16, dm-devel, christophe varoqui


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

Hi, Johannes,
Your advice is nice, thank you
I will send the modified patch later.




发件人:         Johannes Thumshirn <jthumshirn@suse.de>
收件人:         tang.junhui@zte.com.cn, 
抄送:   christophe varoqui <christophe.varoqui@free.fr>, 
zhang.kai16@zte.com.cn, dm-devel@redhat.com
日期:   2016/08/15 21:59
主题:   Re: [dm-devel] [PATCH] libmultipath: fix memory API logic error



On Mon, Aug 15, 2016 at 06:59:09PM +0800, tang.junhui@zte.com.cn wrote:
> From: "tang.junhui" <tang.junhui@zte.com.cn>
> 
> Memroy API use mem_allocated to record the total size of used memory,
> however, it's wrong to use size(p) as the length of freed memory in 
xfree(),
> and memory may also be allocated by STRDUP() or REALLOC(), which is
> not calculated into mem_allocated, so the total size of used memory is
> not correctly. For these reasons, we removed these incorrectly code to 
keep
> the code clean.
> 
> Signed-off-by: tang.junhui <tang.junhui@zte.com.cn>
> ---
>  libmpathpersist/mpath_updatepr.c |  1 -
>  libmultipath/memory.c            | 31 -------------------------------
>  libmultipath/memory.h            | 13 ++-----------
>  3 files changed, 2 insertions(+), 43 deletions(-)
> 
> diff --git a/libmpathpersist/mpath_updatepr.c 
b/libmpathpersist/mpath_updatepr.c
> index 9ff4b30..5af2e03 100644
> --- a/libmpathpersist/mpath_updatepr.c
> +++ b/libmpathpersist/mpath_updatepr.c
> @@ -16,7 +16,6 @@
>  #include "uxsock.h"
>  #include "memory.h"
> 
> -unsigned long mem_allocated;    /* Total memory used in Bytes */
> 
>  int update_prflag(char * arg1, char * arg2, int noisy)
>  {
> diff --git a/libmultipath/memory.c b/libmultipath/memory.c
> index 1366f45..5441e6a 100644
> --- a/libmultipath/memory.c
> +++ b/libmultipath/memory.c
> @@ -22,37 +22,6 @@
>

[...]

> -
> -void
> -xfree(void *p)
> -{
> -              mem_allocated -= sizeof (p);
> -              free(p);
> -              p = NULL;
> -}
> -
>  /*

[...]

> -#define MALLOC(n)    (zalloc(n))
> -#define FREE(p)      (xfree(p))
> +#define MALLOC(n)    (calloc(1,(n)))
> +#define FREE(p)      (free(p))
>  #define REALLOC(p,n) (realloc((p),(n)))
>  #define STRDUP(n)    (strdup(n))

One minor nit, the original xfree() has set the freed pointer to 0, so 
maybe a

#define FREE(p) do { free(p); p = NULL } while(0)

would be advisable. I personally like setting free'd pointers to NULL,
as a NULL pointer access is a more immediate feedback than some rather
nasty use-after-free.

Just my $0.2

Byte,
                 Johannes

-- 
Johannes Thumshirn                                          Storage
jthumshirn@suse.de                                +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850



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

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



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

* [PATCH] libmultipath: fix memory API logic error
@ 2016-08-16  1:55 tang.junhui
  0 siblings, 0 replies; 7+ messages in thread
From: tang.junhui @ 2016-08-16  1:55 UTC (permalink / raw)
  To: christophe varoqui; +Cc: zhang.kai16, dm-devel, tang.junhui

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

Memroy API use mem_allocated to record the total size of used memory,
however, it's wrong to use size(p) as the length of freed memory in xfree(),
and memory may also be allocated by STRDUP() or REALLOC(), which is
not calculated into mem_allocated, so the total size of used memory is
not correctly. For these reasons, we removed these incorrectly code to keep
the code clean.

Signed-off-by: tang.junhui <tang.junhui@zte.com.cn>
---
 libmpathpersist/mpath_updatepr.c |  1 -
 libmultipath/memory.c            | 31 -------------------------------
 libmultipath/memory.h            | 13 ++-----------
 3 files changed, 2 insertions(+), 43 deletions(-)

diff --git a/libmpathpersist/mpath_updatepr.c b/libmpathpersist/mpath_updatepr.c
index 9ff4b30..5af2e03 100644
--- a/libmpathpersist/mpath_updatepr.c
+++ b/libmpathpersist/mpath_updatepr.c
@@ -16,7 +16,6 @@
 #include "uxsock.h"
 #include "memory.h"
 
-unsigned long mem_allocated;    /* Total memory used in Bytes */
 
 int update_prflag(char * arg1, char * arg2, int noisy)
 {
diff --git a/libmultipath/memory.c b/libmultipath/memory.c
index 1366f45..5441e6a 100644
--- a/libmultipath/memory.c
+++ b/libmultipath/memory.c
@@ -22,37 +22,6 @@
 
 #include "memory.h"
 
-/* Global var */
-unsigned long mem_allocated;	/* Total memory used in Bytes */
-
-void *
-xalloc(unsigned long size)
-{
-	void *mem;
-	if ((mem = malloc(size)))
-		mem_allocated += size;
-	return mem;
-}
-
-void *
-zalloc(unsigned long size)
-{
-	void *mem;
-	if ((mem = malloc(size))) {
-		memset(mem, 0, size);
-		mem_allocated += size;
-	}
-	return mem;
-}
-
-void
-xfree(void *p)
-{
-	mem_allocated -= sizeof (p);
-	free(p);
-	p = NULL;
-}
-
 /*
  * Memory management. in debug mode,
  * help finding eventual memory leak.
diff --git a/libmultipath/memory.h b/libmultipath/memory.h
index 8573f6f..29a75ed 100644
--- a/libmultipath/memory.h
+++ b/libmultipath/memory.h
@@ -29,15 +29,6 @@
 #include <string.h>
 #include <assert.h>
 
-/* extern types */
-extern unsigned long mem_allocated;
-extern void *xalloc(unsigned long size);
-extern void *zalloc(unsigned long size);
-extern void xfree(void *p);
-
-/* Global alloc macro */
-#define ALLOC(n) (xalloc(n))
-
 /* Local defines */
 #ifdef _DEBUG_
 
@@ -63,8 +54,8 @@ extern void dbg_free_final(char *);
 
 #else
 
-#define MALLOC(n)    (zalloc(n))
-#define FREE(p)      (xfree(p))
+#define MALLOC(n)    (calloc(1,(n)))
+#define FREE(p)      do { free(p); p = NULL; } while(0)
 #define REALLOC(p,n) (realloc((p),(n)))
 #define STRDUP(n)    (strdup(n))
 
-- 
2.8.1.windows.1

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

* Re: [PATCH] libmultipath: fix memory API logic error
  2016-08-15 12:11 Xose Vazquez Perez
@ 2016-08-15 13:49 ` Bart Van Assche
  0 siblings, 0 replies; 7+ messages in thread
From: Bart Van Assche @ 2016-08-15 13:49 UTC (permalink / raw)
  To: Xose Vazquez Perez, tang.junhui ,
	device-mapper development, Christophe Varoqui

On 08/15/16 05:11, Xose Vazquez Perez wrote:
> libmultipath/memory.[ch] files along with libmultipath/parser.[ch] and
> libmultipath/vector.[ch] come from keepalived/lib/ [1] .
>
> Maybe it's worth to backport their fixes and new features.

Hello Xose,

Are you aware that there are tools available that are much more powerful 
than keepalived's memory.[ch]? See e.g. 
http://valgrind.org/info/tools.html#massif.

Bart.

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

* Re: [PATCH] libmultipath: fix memory API logic error
@ 2016-08-15 12:11 Xose Vazquez Perez
  2016-08-15 13:49 ` Bart Van Assche
  0 siblings, 1 reply; 7+ messages in thread
From: Xose Vazquez Perez @ 2016-08-15 12:11 UTC (permalink / raw)
  To: tang.junhui , device-mapper development, Christophe Varoqui

tang.junhui wrote:

> Memroy API use mem_allocated to record the total size of used memory,
> however, it's wrong to use size(p) as the length of freed memory in xfree(),
> and memory may also be allocated by STRDUP() or REALLOC(), which is
> not calculated into mem_allocated, so the total size of used memory is
> not correctly. For these reasons, we removed these incorrectly code to keep
> the code clean.

>  libmpathpersist/mpath_updatepr.c |  1 -
>  libmultipath/memory.c            | 31 -------------------------------
>  libmultipath/memory.h            | 13 ++-----------

libmultipath/memory.[ch] files along with libmultipath/parser.[ch] and
libmultipath/vector.[ch] come from keepalived/lib/ [1] .

Maybe it's worth to backport their fixes and new features.


[1] https://github.com/acassen/keepalived

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

end of thread, other threads:[~2016-08-16  1:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-08-15 10:59 [PATCH] libmultipath: fix memory API logic error tang.junhui
2016-08-15 13:46 ` Bart Van Assche
2016-08-15 13:58 ` Johannes Thumshirn
2016-08-16  0:51   ` 答复: " tang.junhui
2016-08-15 12:11 Xose Vazquez Perez
2016-08-15 13:49 ` Bart Van Assche
2016-08-16  1:55 tang.junhui

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.