Skip to contentSkip to conversation

mm: kmemleak: Use a memory pool for kmemleak object allocations

PATCH In review Catalin Marinas 5 participants 18 messages Aug 12, 2019 – Dec 5, 2019

Subsystems (5)
lore
Share & tools
lore

Catalin Marinas

author maintainer
#0 lore raw

v3 cover mm: kmemleak: Use a memory pool for kmemleak object allocations

Following the discussions on v2 of this patch(set) [1], this series takes slightly different approach:

- it implements its own simple memory pool that does not rely on the slab allocator

- drops the early log buffer logic entirely since it can now allocate metadata from the memory pool directly before kmemleak is fully initialised

- CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option is renamed to CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE

- moves the kmemleak_init() call earlier (mm_init())

- to avoid a separate memory pool for struct scan_area, it makes the tool robust when such allocations fail as scan areas are rather an optimisation

[1] http://lkml.kernel.org/r/[email protected]

Catalin Marinas (3):
mm: kmemleak: Make the tool tolerant to struct scan_area allocation
failures
mm: kmemleak: Simple memory allocation pool for kmemleak objects
mm: kmemleak: Use the memory pool for early allocations

 init/main.c       |   2 +-
 lib/Kconfig.debug |  11 +-
 mm/kmemleak.c     | 325 ++++++++++++----------------------------------
 3 files changed, 91 insertions(+), 247 deletions(-)
author maintainer
#7 lore raw

PATCH v3 2/3 mm: kmemleak: Simple memory allocation pool for kmemleak objects

Add a memory pool for struct kmemleak_object in case the normal kmem_cache_alloc() fails under the gfp constraints passed by the caller. The mem_pool[] array size is currently fixed at 16000.

---
 mm/kmemleak.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 52 insertions(+), 2 deletions(-)
mm/kmemleak.c +52 -2
index 5ba7fad00fda..2fb86524d70b 100644
@@ -180,11 +180,17 @@ struct kmemleak_object {
 #define HEX_ASCII		1
 /* max number of lines to be printed */
 #define HEX_MAX_LINES		2
+/* memory pool size */
+#define MEM_POOL_SIZE		16000
 
 /* the list of all allocated objects */
 static LIST_HEAD(object_list);
 /* the list of gray-colored objects (see color_gray comment below) */
 static LIST_HEAD(gray_list);
+/* memory pool allocation */
+static struct kmemleak_object mem_pool[MEM_POOL_SIZE];
+static int mem_pool_free_count = ARRAY_SIZE(mem_pool);
+static LIST_HEAD(mem_pool_free_list);
 /* search tree for object boundaries */
 static struct rb_root object_tree_root = RB_ROOT;
 /* rw_lock protecting the access to object_list and object_tree_root */
@@ -451,6 +457,50 @@ static int get_object(struct kmemleak_object *object)
 	return atomic_inc_not_zero(&object->use_count);
 }
 
+/*
+ * Memory pool allocation and freeing. kmemleak_lock must not be held.
+ */
+static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
+{
+	unsigned long flags;
+	struct kmemleak_object *object;
+
+	/* try the slab allocator first */
+	object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+	if (object)
+		return object;
+
+	/* slab allocation failed, try the memory pool */
+	write_lock_irqsave(&kmemleak_lock, flags);
+	object = list_first_entry_or_null(&mem_pool_free_list,
+					  typeof(*object), object_list);
+	if (object)
+		list_del(&object->object_list);
+	else if (mem_pool_free_count)
+		object = &mem_pool[--mem_pool_free_count];
+	write_unlock_irqrestore(&kmemleak_lock, flags);
+
+	return object;
+}
+
+/*
+ * Return the object to either the slab allocator or the memory pool.
+ */
+static void mem_pool_free(struct kmemleak_object *object)
+{
+	unsigned long flags;
+
+	if (object < mem_pool || object >= mem_pool + ARRAY_SIZE(mem_pool)) {
+		kmem_cache_free(object_cache, object);
+		return;
+	}
+
+	/* add the object to the memory pool free list */
+	write_lock_irqsave(&kmemleak_lock, flags);
+	list_add(&object->object_list, &mem_pool_free_list);
+	write_unlock_irqrestore(&kmemleak_lock, flags);
+}
+
 /*
  * RCU callback to free a kmemleak_object.
  */
@@ -469,7 +519,7 @@ static void free_object_rcu(struct rcu_head *rcu)
 		hlist_del(&area->node);
 		kmem_cache_free(scan_area_cache, area);
 	}
-	kmem_cache_free(object_cache, object);
+	mem_pool_free(object);
 }
 
 /*
@@ -552,7 +602,7 @@ static struct kmemleak_object *create_object(unsigned long ptr, size_t size,
 	struct rb_node **link, *rb_parent;
 	unsigned long untagged_ptr;
 
-	object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+	object = mem_pool_alloc(gfp);
 	if (!object) {
 		pr_warn("Cannot allocate a kmemleak_object structure\n");
 		kmemleak_disable();
author maintainer
#9 lore raw

PATCH v3 3/3 mm: kmemleak: Use the memory pool for early allocations

In reply to Catalin Marinas

Currently kmemleak uses a static early_log buffer to trace all memory allocation/freeing before the slab allocator is initialised. Such early log is replayed during kmemleak_init() to properly initialise the kmemleak metadata for objects allocated up that point. With a memory pool that does not rely on the slab allocator, it is possible to skip this early log entirely.

In order to remove the early logging, consider kmemleak_enabled == 1 by default while the kmem_cache availability is checked directly on the object_cache and scan_area_cache variables. The RCU callback is only invoked after object_cache has been initialised as we wouldn't have any concurrent list traversal before this.

In order to reduce the number of callbacks before kmemleak is fully initialised, move the kmemleak_init() call to mm_init().

---
 init/main.c       |   2 +-
 lib/Kconfig.debug |  11 +-
 mm/kmemleak.c     | 267 +++++-----------------------------------------
 3 files changed, 35 insertions(+), 245 deletions(-)
init/main.c +1 -1
index 96f8d5af52d6..ca05e3cd7ef7 100644
@@ -556,6 +556,7 @@ static void __init mm_init(void)
 	report_meminit();
 	mem_init();
 	kmem_cache_init();
+	kmemleak_init();
 	pgtable_init();
 	debug_objects_mem_init();
 	vmalloc_init();
@@ -740,7 +741,6 @@ asmlinkage __visible void __init start_kernel(void)
 		initrd_start = 0;
 	}
 #endif
-	kmemleak_init();
 	setup_per_cpu_pageset();
 	numa_policy_init();
 	acpi_early_init();
lib/Kconfig.debug +6 -5
index 4d39540011e2..39df06ffd9f4 100644
@@ -592,17 +592,18 @@ config DEBUG_KMEMLEAK
 	  In order to access the kmemleak file, debugfs needs to be
 	  mounted (usually at /sys/kernel/debug).
 
-config DEBUG_KMEMLEAK_EARLY_LOG_SIZE
-	int "Maximum kmemleak early log entries"
+config DEBUG_KMEMLEAK_MEM_POOL_SIZE
+	int "Kmemleak memory pool size"
 	depends on DEBUG_KMEMLEAK
 	range 200 40000
 	default 16000
 	help
 	  Kmemleak must track all the memory allocations to avoid
 	  reporting false positives. Since memory may be allocated or
-	  freed before kmemleak is initialised, an early log buffer is
-	  used to store these actions. If kmemleak reports "early log
-	  buffer exceeded", please increase this value.
+	  freed before kmemleak is fully initialised, use a static pool
+	  of metadata objects to track such callbacks. After kmemleak is
+	  fully initialised, this memory pool acts as an emergency one
+	  if slab allocations fail.
 
 config DEBUG_KMEMLEAK_TEST
 	tristate "Simple test for the kernel memory leak detector"
mm/kmemleak.c +28 -239

431 lines not on this page · raw message

author maintainer
#1 lore raw

PATCH v3 1/3 mm: kmemleak: Make the tool tolerant to struct scan_area allocation failures

In reply to Catalin Marinas

Object scan areas are an optimisation aimed to decrease the false positives and slightly improve the scanning time of large objects known to only have a few specific pointers. If a struct scan_area fails to allocate, kmemleak can still function normally by scanning the full object.

Introduce an OBJECT_FULL_SCAN flag and mark objects as such when scan_area allocation fails.

---
 mm/kmemleak.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
mm/kmemleak.c +10 -6
index f6e602918dac..5ba7fad00fda 100644
@@ -168,6 +168,8 @@ struct kmemleak_object {
 #define OBJECT_REPORTED		(1 << 1)
 /* flag set to not scan the object */
 #define OBJECT_NO_SCAN		(1 << 2)
+/* flag set to fully scan the object when scan_area allocation failed */
+#define OBJECT_FULL_SCAN	(1 << 3)
 
 #define HEX_PREFIX		"    "
 /* number of bytes to print per line; must be 16 or 32 */
@@ -773,12 +775,14 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
 	}
 
 	area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
-	if (!area) {
-		pr_warn("Cannot allocate a scan area\n");
-		goto out;
-	}
 
 	spin_lock_irqsave(&object->lock, flags);
+	if (!area) {
+		pr_warn_once("Cannot allocate a scan area, scanning the full object\n");
+		/* mark the object for full scan to avoid false positives */
+		object->flags |= OBJECT_FULL_SCAN;
+		goto out_unlock;
+	}
 	if (size == SIZE_MAX) {
 		size = object->pointer + object->size - ptr;
 	} else if (ptr + size > object->pointer + object->size) {
@@ -795,7 +799,6 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
 	hlist_add_head(&area->node, &object->area_list);
 out_unlock:
 	spin_unlock_irqrestore(&object->lock, flags);
-out:
 	put_object(object);
 }
 
@@ -1408,7 +1411,8 @@ static void scan_object(struct kmemleak_object *object)
 	if (!(object->flags & OBJECT_ALLOCATED))
 		/* already freed object */
 		goto out;
-	if (hlist_empty(&object->area_list)) {
+	if (hlist_empty(&object->area_list) ||
+	    object->flags & OBJECT_FULL_SCAN) {
 		void *start = (void *)object->pointer;
 		void *end = (void *)(object->pointer + object->size);
 		void *next;
maintainer
#13 lore raw
In reply to Catalin Marinas
Quoted 13 lines · On Mon, 12 Aug 2019 17:06:39 +0100 Catalin Marinas <[email protected]>…

Following the discussions on v2 of this patch(set) [1], this series takes slightly different approach:

- it implements its own simple memory pool that does not rely on the slab allocator

- drops the early log buffer logic entirely since it can now allocate metadata from the memory pool directly before kmemleak is fully initialised

- CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option is renamed to CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE

Quoted 7 lines

- moves the kmemleak_init() call earlier (mm_init())

- to avoid a separate memory pool for struct scan_area, it makes the tool robust when such allocations fail as scan areas are rather an optimisation

[1] http://lkml.kernel.org/r/[email protected]

Using the term "memory pool" is a little unfortunate, but better than using "mempool"!

The changelog doesn't answer the very first question: why not use mempools. Please send along a paragraph which explains this decision.

author maintainer
#10 lore raw

Re: [PATCH v3 3/3] mm: kmemleak: Use the memory pool for early allocations

In reply to Catalin Marinas

mm/kmemleak.c static struct kmemleak_object *mem_pool_alloc(gfp_t gfp) · line 427

Quoted 13 lines · Catalin Marinas
mm/kmemleak.c +6 -3
@@ -466,9 +419,13 @@ static struct kmemleak_object *mem_pool_alloc(gfp_t gfp)
 	struct kmemleak_object *object;
 
 	/* try the slab allocator first */
-	object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
-	if (object)
-		return object;
+	if (object_cache) {
+		object = kmem_cache_alloc(object_cache, gfp_kmemleak_mask(gfp));
+		if (object)
+			return object;
+		else
+			WARN_ON_ONCE(1);

Oops, this was actually my debug warning just to make sure it triggered (tested with failslab). The WARN_ON_ONCE(1) should be removed (I changed it locally in case I post an update).

I noticed it in Andrew's subsequent checkpatch fix.

Signature · 1 line
Catalin
author maintainer
#8 lore raw

Re: [PATCH v3 2/3] mm: kmemleak: Simple memory allocation pool for kmemleak objects

In reply to Catalin Marinas
Quoted 3 lines · Catalin Marinas

Add a memory pool for struct kmemleak_object in case the normal kmem_cache_alloc() fails under the gfp constraints passed by the caller. The mem_pool[] array size is currently fixed at 16000.

Following Andrew's comment, I'd add this paragraph here:

-----------8<------------------------
We are not using the existing mempool kernel API since this requires the slab allocator to be available (for pool->elements allocation). A subsequent kmemleak patch will replace the static early log buffer with the pool allocation introduced here and this functionality is required to be available before the slab was initialised. -----------8<------------------------

(patch updated locally)

Signature · 1 line
Catalin
author maintainer
#14 lore raw
In reply to Andrew Morton
Quoted 17 lines · Andrew Morton
Quoted 15 lines · On Mon, 12 Aug 2019 17:06:39 +0100 Catalin Marinas <[email protected]>…

Following the discussions on v2 of this patch(set) [1], this series takes slightly different approach:

- it implements its own simple memory pool that does not rely on the slab allocator

- drops the early log buffer logic entirely since it can now allocate metadata from the memory pool directly before kmemleak is fully initialised

- CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option is renamed to CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE

- moves the kmemleak_init() call earlier (mm_init())

Quoted 8 lines
Quoted 5 lines

- to avoid a separate memory pool for struct scan_area, it makes the tool robust when such allocations fail as scan areas are rather an optimisation

[1] http://lkml.kernel.org/r/[email protected]

Using the term "memory pool" is a little unfortunate, but better than using "mempool"!

I agree, it could have been more inspired. What about "metadata pool" (together with function name updates etc.)? Happy to send a v4.

Quoted 2 lines

The changelog doesn't answer the very first question: why not use mempools. Please send along a paragraph which explains this decision.

I posted one in reply to the patch where the changelog should be updated.

Thanks.

Signature · 1 line
Catalin
#11 lore raw

Re: [PATCH v3 3/3] mm: kmemleak: Use the memory pool for early allocations

In reply to Catalin Marinas
Quoted 55 lines · Catalin Marinas

Currently kmemleak uses a static early_log buffer to trace all memory allocation/freeing before the slab allocator is initialised. Such early log is replayed during kmemleak_init() to properly initialise the kmemleak metadata for objects allocated up that point. With a memory pool that does not rely on the slab allocator, it is possible to skip this early log entirely.

In order to remove the early logging, consider kmemleak_enabled == 1 by default while the kmem_cache availability is checked directly on the object_cache and scan_area_cache variables. The RCU callback is only invoked after object_cache has been initialised as we wouldn't have any concurrent list traversal before this.

In order to reduce the number of callbacks before kmemleak is fully initialised, move the kmemleak_init() call to mm_init().

---
 init/main.c       |   2 +-
 lib/Kconfig.debug |  11 +-
 mm/kmemleak.c     | 267 +++++-----------------------------------------

 3 files changed, 35 insertions(+), 245 deletions(-)
init/main.c +1 -1
index 96f8d5af52d6..ca05e3cd7ef7 100644
@@ -556,6 +556,7 @@ static void __init mm_init(void)
  	report_meminit();
  	mem_init();
  	kmem_cache_init();
+	kmemleak_init();
  	pgtable_init();
  	debug_objects_mem_init();
  	vmalloc_init();
@@ -740,7 +741,6 @@ asmlinkage __visible void __init start_kernel(void)
  		initrd_start = 0;
  	}
  #endif
-	kmemleak_init();
  	setup_per_cpu_pageset();
  	numa_policy_init();
  	acpi_early_init();
lib/Kconfig.debug +0 -0
index 4d39540011e2..39df06ffd9f4 100644
@@ -592,17 +592,18 @@ config DEBUG_KMEMLEAK

    In order to access the kmemleak file, debugfs needs to be mounted (usually at /sys/kernel/debug).

lib/Kconfig.debug +2 -2
-config DEBUG_KMEMLEAK_EARLY_LOG_SIZE
-	int "Maximum kmemleak early log entries"
+config DEBUG_KMEMLEAK_MEM_POOL_SIZE
+	int "Kmemleak memory pool size"
Quoted 3 lines

  depends on DEBUG_KMEMLEAK
  range 200 40000
  default 16000

Hmm, this seems way too small. My previous round of testing with kmemleak.mempool=524288 works quite well on all architectures.

author maintainer
#12 lore raw

Re: [PATCH v3 3/3] mm: kmemleak: Use the memory pool for early allocations

Quoted 11 lines · Qian Cai
Quoted 10 lines · Catalin Marinas
lib/Kconfig.debug +0 -0
index 4d39540011e2..39df06ffd9f4 100644
@@ -592,17 +592,18 @@ config DEBUG_KMEMLEAK

    In order to access the kmemleak file, debugfs needs to be mounted (usually at /sys/kernel/debug).

lib/Kconfig.debug +0 -2
-config DEBUG_KMEMLEAK_EARLY_LOG_SIZE
-	int "Maximum kmemleak early log entries"
Quoted 8 lines
Quoted 5 lines
lib/Kconfig.debug +2 -0
+config DEBUG_KMEMLEAK_MEM_POOL_SIZE
+	int "Kmemleak memory pool size"

  depends on DEBUG_KMEMLEAK
  range 200 40000
  default 16000

Hmm, this seems way too small. My previous round of testing with kmemleak.mempool=524288 works quite well on all architectures.

We can change the upper bound here to 1M but I'd keep the default sane. Not everyone is running tests under OOM.

Signature · 1 line
Catalin
#2 lore raw

Re: [PATCH v3 1/3] mm: kmemleak: Make the tool tolerant to struct scan_area allocation failures

In reply to Catalin Marinas
Quoted 8 lines · Catalin Marinas

Object scan areas are an optimisation aimed to decrease the false positives and slightly improve the scanning time of large objects known to only have a few specific pointers. If a struct scan_area fails to allocate, kmemleak can still function normally by scanning the full object.

Introduce an OBJECT_FULL_SCAN flag and mark objects as such when scan_area allocation fails.

I came across this one while bisecting sudden drop in throughput of a 100Gbit Mellanox CX4 ethernet card in a PPC POWER9 system, the speed dropped from 100Gbit to about 40Gbit. Bisect pointed at dba82d943177, this are the relevant config options:

[fstn1-p1 kernel]$ grep KMEMLEAK ~/pbuild/kernel-le-4g/.config CONFIG_HAVE_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK=y CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=16000
# CONFIG_DEBUG_KMEMLEAK_TEST is not set
# CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF is not set CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN=y

Setting CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE=400 or even 4000 (this is what KMEMLEAK_EARLY_LOG_SIZE is now in the master) produces soft lockups on the recent upstream (sha1 a3c0e7b1fe1f):

[c000001fde64fb60] [c000000000c24ed4] _raw_write_unlock_irqrestore+0x54/0x70 [c000001fde64fb90] [c0000000004117e4] find_and_remove_object+0xa4/0xd0 [c000001fde64fbe0] [c000000000411c74] delete_object_full+0x24/0x50 [c000001fde64fc00] [c000000000411d28] __kmemleak_do_cleanup+0x88/0xd0 [c000001fde64fc40] [c00000000012a1a4] process_one_work+0x374/0x6a0 [c000001fde64fd20] [c00000000012a548] worker_thread+0x78/0x5a0 [c000001fde64fdb0] [c000000000135508] kthread+0x198/0x1a0 [c000001fde64fe20] [c00000000000b980] ret_from_kernel_thread+0x5c/0x7c

KMEMLEAK_EARLY_LOG_SIZE=8000 works but slow.

Interestingly KMEMLEAK_EARLY_LOG_SIZE=400 on dba82d943177 still worked and I saw my 100Gbit. Disabling KMEMLEAK also fixes the speed (apparently).

Is that something expected? Thanks,

Quoted 57 lines
---
 mm/kmemleak.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
mm/kmemleak.c +10 -6
index f6e602918dac..5ba7fad00fda 100644
@@ -168,6 +168,8 @@ struct kmemleak_object {
 #define OBJECT_REPORTED		(1 << 1)
 /* flag set to not scan the object */
 #define OBJECT_NO_SCAN		(1 << 2)
+/* flag set to fully scan the object when scan_area allocation failed */
+#define OBJECT_FULL_SCAN	(1 << 3)
 
 #define HEX_PREFIX		"    "
 /* number of bytes to print per line; must be 16 or 32 */
@@ -773,12 +775,14 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
 	}
 
 	area = kmem_cache_alloc(scan_area_cache, gfp_kmemleak_mask(gfp));
-	if (!area) {
-		pr_warn("Cannot allocate a scan area\n");
-		goto out;
-	}
 
 	spin_lock_irqsave(&object->lock, flags);
+	if (!area) {
+		pr_warn_once("Cannot allocate a scan area, scanning the full object\n");
+		/* mark the object for full scan to avoid false positives */
+		object->flags |= OBJECT_FULL_SCAN;
+		goto out_unlock;
+	}
 	if (size == SIZE_MAX) {
 		size = object->pointer + object->size - ptr;
 	} else if (ptr + size > object->pointer + object->size) {
@@ -795,7 +799,6 @@ static void add_scan_area(unsigned long ptr, size_t size, gfp_t gfp)
 	hlist_add_head(&area->node, &object->area_list);
 out_unlock:
 	spin_unlock_irqrestore(&object->lock, flags);
-out:
 	put_object(object);
 }
 
@@ -1408,7 +1411,8 @@ static void scan_object(struct kmemleak_object *object)
 	if (!(object->flags & OBJECT_ALLOCATED))
 		/* already freed object */
 		goto out;
-	if (hlist_empty(&object->area_list)) {
+	if (hlist_empty(&object->area_list) ||
+	    object->flags & OBJECT_FULL_SCAN) {
 		void *start = (void *)object->pointer;
 		void *end = (void *)(object->pointer + object->size);
 		void *next;
Signature · 1 line
Alexey
#3 lore raw

Re: [PATCH v3 1/3] mm: kmemleak: Make the tool tolerant to struct scan_area allocation failures

Quoted 28 lines · Alexey Kardashevskiy

I came across this one while bisecting sudden drop in throughput of a 100Gbit Mellanox CX4 ethernet card in a PPC POWER9 system, the speed dropped from 100Gbit to about 40Gbit. Bisect pointed at dba82d943177, this are the relevant config options:

[fstn1-p1 kernel]$ grep KMEMLEAK ~/pbuild/kernel-le-4g/.config CONFIG_HAVE_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK=y CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=16000
# CONFIG_DEBUG_KMEMLEAK_TEST is not set
# CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF is not set CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN=y

Setting CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE=400 or even 4000 (this is what KMEMLEAK_EARLY_LOG_SIZE is now in the master) produces soft lockups on the recent upstream (sha1 a3c0e7b1fe1f):

[c000001fde64fb60] [c000000000c24ed4] _raw_write_unlock_irqrestore+0x54/0x70 [c000001fde64fb90] [c0000000004117e4] find_and_remove_object+0xa4/0xd0 [c000001fde64fbe0] [c000000000411c74] delete_object_full+0x24/0x50 [c000001fde64fc00] [c000000000411d28] __kmemleak_do_cleanup+0x88/0xd0 [c000001fde64fc40] [c00000000012a1a4] process_one_work+0x374/0x6a0 [c000001fde64fd20] [c00000000012a548] worker_thread+0x78/0x5a0 [c000001fde64fdb0] [c000000000135508] kthread+0x198/0x1a0 [c000001fde64fe20] [c00000000000b980] ret_from_kernel_thread+0x5c/0x7c

Quoted 6 lines

KMEMLEAK_EARLY_LOG_SIZE=8000 works but slow.

Interestingly KMEMLEAK_EARLY_LOG_SIZE=400 on dba82d943177 still worked and I saw my 100Gbit. Disabling KMEMLEAK also fixes the speed (apparently).

Is that something expected? Thanks,

It is expected that a debug option like this will make the system slower.

author maintainer
#4 lore raw

Re: [PATCH v3 1/3] mm: kmemleak: Make the tool tolerant to struct scan_area allocation failures

In reply to Alexey Kardashevskiy
Quoted 15 lines · Alexey Kardashevskiy
Quoted 8 lines · Catalin Marinas

Object scan areas are an optimisation aimed to decrease the false positives and slightly improve the scanning time of large objects known to only have a few specific pointers. If a struct scan_area fails to allocate, kmemleak can still function normally by scanning the full object.

Introduce an OBJECT_FULL_SCAN flag and mark objects as such when scan_area allocation fails.

I came across this one while bisecting sudden drop in throughput of a 100Gbit Mellanox CX4 ethernet card in a PPC POWER9 system, the speed dropped from 100Gbit to about 40Gbit. Bisect pointed at dba82d943177, this are the relevant config options:

Quoted 7 lines

[fstn1-p1 kernel]$ grep KMEMLEAK ~/pbuild/kernel-le-4g/.config CONFIG_HAVE_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK=y CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=16000
# CONFIG_DEBUG_KMEMLEAK_TEST is not set
# CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF is not set CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN=y

The throughput drop is probably caused caused by kmemleak slowing down all memory allocations (including skb). So that's expected. You may get similar drop with other debug options like lock proving, kasan.

Quoted 4 lines

Setting CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE=400 or even 4000 (this is what KMEMLEAK_EARLY_LOG_SIZE is now in the master) produces soft lockups on the recent upstream (sha1 a3c0e7b1fe1f):

Quoted 8 lines

[c000001fde64fb60] [c000000000c24ed4] _raw_write_unlock_irqrestore+0x54/0x70 [c000001fde64fb90] [c0000000004117e4] find_and_remove_object+0xa4/0xd0 [c000001fde64fbe0] [c000000000411c74] delete_object_full+0x24/0x50 [c000001fde64fc00] [c000000000411d28] __kmemleak_do_cleanup+0x88/0xd0 [c000001fde64fc40] [c00000000012a1a4] process_one_work+0x374/0x6a0 [c000001fde64fd20] [c00000000012a548] worker_thread+0x78/0x5a0 [c000001fde64fdb0] [c000000000135508] kthread+0x198/0x1a0 [c000001fde64fe20] [c00000000000b980] ret_from_kernel_thread+0x5c/0x7c

That's the kmemleak disabling path. I don't have the full log but I suspect by setting a small pool size, kmemleak failed to allocate memory and went into disabling itself. The clean-up above tries to remove the allocated metadata. It seems that it takes significant time on your platform. Not sure how to avoid the soft lock-up but I wouldn't bother too much about it, it's only triggered by a previous error condition disabling kmemleak.

Quoted 5 lines

KMEMLEAK_EARLY_LOG_SIZE=8000 works but slow.

Interestingly KMEMLEAK_EARLY_LOG_SIZE=400 on dba82d943177 still worked and I saw my 100Gbit. Disabling KMEMLEAK also fixes the speed (apparently).

A small memory pool for kmemleak just disables it shortly after boot, so it's no longer in the way and you get your throughput back.

Quoted 1 line

Is that something expected? Thanks,

Yes for the throughput. Not sure about the soft lock-up. Do you have the full log around the lock-up?

Signature · 1 line
Catalin
#5 lore raw

Re: [PATCH v3 1/3] mm: kmemleak: Make the tool tolerant to struct scan_area allocation failures

Quoted 24 lines · Catalin Marinas
Quoted 15 lines · Alexey Kardashevskiy
Quoted 8 lines · Catalin Marinas

Object scan areas are an optimisation aimed to decrease the false positives and slightly improve the scanning time of large objects known to only have a few specific pointers. If a struct scan_area fails to allocate, kmemleak can still function normally by scanning the full object.

Introduce an OBJECT_FULL_SCAN flag and mark objects as such when scan_area allocation fails.

I came across this one while bisecting sudden drop in throughput of a 100Gbit Mellanox CX4 ethernet card in a PPC POWER9 system, the speed dropped from 100Gbit to about 40Gbit. Bisect pointed at dba82d943177, this are the relevant config options:

Quoted 7 lines

[fstn1-p1 kernel]$ grep KMEMLEAK ~/pbuild/kernel-le-4g/.config CONFIG_HAVE_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK=y CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=16000
# CONFIG_DEBUG_KMEMLEAK_TEST is not set
# CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF is not set CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN=y

Quoted 3 lines

The throughput drop is probably caused caused by kmemleak slowing down all memory allocations (including skb). So that's expected. You may get similar drop with other debug options like lock proving, kasan.

I was not precise. I meant that before dba82d943177 kmemleak would work but would not slow network down (at least 100Gbit) and now it does which is downgrade so I was wondering if kmemleak just got so much better to justify this change or there is a bug somewhere, so which one is it? Or "LOG_SIZE=400" never really worked? See my findings below though.

If it was always slow, it is expected indeed.

Quoted 28 lines
Quoted 5 lines

Setting CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE=400 or even 4000 (this is what KMEMLEAK_EARLY_LOG_SIZE is now in the master) produces soft lockups on the recent upstream (sha1 a3c0e7b1fe1f):

Quoted 8 lines

[c000001fde64fb60] [c000000000c24ed4] _raw_write_unlock_irqrestore+0x54/0x70 [c000001fde64fb90] [c0000000004117e4] find_and_remove_object+0xa4/0xd0 [c000001fde64fbe0] [c000000000411c74] delete_object_full+0x24/0x50 [c000001fde64fc00] [c000000000411d28] __kmemleak_do_cleanup+0x88/0xd0 [c000001fde64fc40] [c00000000012a1a4] process_one_work+0x374/0x6a0 [c000001fde64fd20] [c00000000012a548] worker_thread+0x78/0x5a0 [c000001fde64fdb0] [c000000000135508] kthread+0x198/0x1a0 [c000001fde64fe20] [c00000000000b980] ret_from_kernel_thread+0x5c/0x7c

That's the kmemleak disabling path. I don't have the full log but I suspect by setting a small pool size, kmemleak failed to allocate memory and went into disabling itself. The clean-up above tries to remove the allocated metadata. It seems that it takes significant time on your platform. Not sure how to avoid the soft lock-up but I wouldn't bother too much about it, it's only triggered by a previous error condition disabling kmemleak.

Quoted 5 lines

KMEMLEAK_EARLY_LOG_SIZE=8000 works but slow.

Interestingly KMEMLEAK_EARLY_LOG_SIZE=400 on dba82d943177 still worked and I saw my 100Gbit. Disabling KMEMLEAK also fixes the speed (apparently).

Quoted 7 lines

A small memory pool for kmemleak just disables it shortly after boot, so it's no longer in the way and you get your throughput back.

Quoted 1 line

Is that something expected? Thanks,

Yes for the throughput. Not sure about the soft lock-up. Do you have the full log around the lock-up?

I was going to post one but then I received "kmemleak: Do not corrupt the object_list during clean-up" which fixed lockups and took throughput back to normal, I'll reply there too. Thanks,

Signature · 1 line
Alexey
author maintainer
#6 lore raw

Re: [PATCH v3 1/3] mm: kmemleak: Make the tool tolerant to struct scan_area allocation failures

Quoted 29 lines · Alexey Kardashevskiy
Quoted 24 lines · Catalin Marinas
Quoted 15 lines · Alexey Kardashevskiy
Quoted 8 lines · Catalin Marinas

Object scan areas are an optimisation aimed to decrease the false positives and slightly improve the scanning time of large objects known to only have a few specific pointers. If a struct scan_area fails to allocate, kmemleak can still function normally by scanning the full object.

Introduce an OBJECT_FULL_SCAN flag and mark objects as such when scan_area allocation fails.

I came across this one while bisecting sudden drop in throughput of a 100Gbit Mellanox CX4 ethernet card in a PPC POWER9 system, the speed dropped from 100Gbit to about 40Gbit. Bisect pointed at dba82d943177, this are the relevant config options:

Quoted 7 lines

[fstn1-p1 kernel]$ grep KMEMLEAK ~/pbuild/kernel-le-4g/.config CONFIG_HAVE_DEBUG_KMEMLEAK=y
CONFIG_DEBUG_KMEMLEAK=y CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE=16000
# CONFIG_DEBUG_KMEMLEAK_TEST is not set
# CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF is not set CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN=y

Quoted 3 lines

The throughput drop is probably caused caused by kmemleak slowing down all memory allocations (including skb). So that's expected. You may get similar drop with other debug options like lock proving, kasan.

Quoted 5 lines

I was not precise. I meant that before dba82d943177 kmemleak would work but would not slow network down (at least 100Gbit) and now it does which is downgrade so I was wondering if kmemleak just got so much better to justify this change or there is a bug somewhere, so which one is it? Or "LOG_SIZE=400" never really worked?

I suspect LOG_SIZE=400 never worked on your system (you can check the log for any kmemleak disabled messages).

Thanks for testing the patch.

Signature · 1 line
Catalin
#15 lore raw
In reply to Catalin Marinas

On 8/12/2019 7:06 PM, Catalin Marinas wrote:

Following the discussions on v2 of this patch(set) [1], this series takes slightly different approach:

- it implements its own simple memory pool that does not rely on the slab allocator

- drops the early log buffer logic entirely since it can now allocate metadata from the memory pool directly before kmemleak is fully initialised

- CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option is renamed to CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE

- moves the kmemleak_init() call earlier (mm_init())

- to avoid a separate memory pool for struct scan_area, it makes the tool robust when such allocations fail as scan areas are rather an optimisation

[1] http://lkml.kernel.org/r/[email protected]

Catalin Marinas (3):
mm: kmemleak: Make the tool tolerant to struct scan_area allocation
failures
mm: kmemleak: Simple memory allocation pool for kmemleak objects
mm: kmemleak: Use the memory pool for early allocations

 init/main.c       |   2 +-
 lib/Kconfig.debug |  11 +-
 mm/kmemleak.c     | 325 ++++++++++++----------------------------------
 3 files changed, 91 insertions(+), 247 deletions(-)

Hi Catalin,

We observe severe degradation in our network performance affecting all of our NICs. The degradation is directly linked to this patch.

What we run:
Simple Iperf TCP loopback with 8 streams on ConnectX5-100GbE. Since it's a loopback test, traffic goes from the socket through the IP stack and back to the socket, without going through the NIC driver.

What we observe:
Throughput performance:
- Kernel 5.3GA - Throughput was 230Gbps
- Kernel 5.4-rc1 and later - Throughput is 50Gbps

CPU utilization-wise:
Using perf we see much higher CPU utilization with kmem related functions:

Function | Kernel 5.3GA | Kernel 5.4-rc1 and later

--------------------------|--------------|-------------------------

__kfree_skb | 3.4% | 11.0%

kmem_cache_free | 0.3% | 10.2%

__alloc_skb | 2.2% | 26.0%

queued_spin_lock_slowpath | 1.3% | 26.3%

delete_object_full | Not used | 18.0%

'delete_object_full()' function seems like the one which starts the slower flow. One of the conditions causing this function to kick into action is 'kmemleak_free_enabled' flag, which was changed to enabled by default by your series.
Reverting discussed series restore the performance almost completely.

Can you help shed light on the subject?

author maintainer
#16 lore raw
Quoted 33 lines · Noam Stolero
Quoted 27 lines · Catalin Marinas

Following the discussions on v2 of this patch(set) [1], this series takes slightly different approach:

- it implements its own simple memory pool that does not rely on the slab allocator

- drops the early log buffer logic entirely since it can now allocate metadata from the memory pool directly before kmemleak is fully initialised

- CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option is renamed to CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE

- moves the kmemleak_init() call earlier (mm_init())

- to avoid a separate memory pool for struct scan_area, it makes the tool robust when such allocations fail as scan areas are rather an optimisation

[1] http://lkml.kernel.org/r/[email protected]

Catalin Marinas (3):
mm: kmemleak: Make the tool tolerant to struct scan_area allocation
failures
mm: kmemleak: Simple memory allocation pool for kmemleak objects
mm: kmemleak: Use the memory pool for early allocations

Quoted 4 lines

init/main.c | 2 +-
lib/Kconfig.debug | 11 +-
mm/kmemleak.c | 325 ++++++++++++----------------------------------
3 files changed, 91 insertions(+), 247 deletions(-)

Quoted 7 lines

We observe severe degradation in our network performance affecting all of our NICs. The degradation is directly linked to this patch.

What we run:
Simple Iperf TCP loopback with 8 streams on ConnectX5-100GbE. Since it's a loopback test, traffic goes from the socket through the IP stack and back to the socket, without going through the NIC driver.

Something similar was reported before. Can you try commit 2abd839aa7e6 ("kmemleak: Do not corrupt the object_list during clean-up") and see if it fixes the problem for you? It was merged in 5.4-rc4.

Signature · 1 line
Catalin
#17 lore raw
Quoted 36 lines · Catalin Marinas
Quoted 35 lines · Noam Stolero
Quoted 27 lines · Catalin Marinas

Following the discussions on v2 of this patch(set) [1], this series takes slightly different approach:

- it implements its own simple memory pool that does not rely on the slab allocator

- drops the early log buffer logic entirely since it can now allocate metadata from the memory pool directly before kmemleak is fully initialised

- CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE option is renamed to CONFIG_DEBUG_KMEMLEAK_MEM_POOL_SIZE

- moves the kmemleak_init() call earlier (mm_init())

- to avoid a separate memory pool for struct scan_area, it makes the tool robust when such allocations fail as scan areas are rather an optimisation

[1] http://lkml.kernel.org/r/[email protected]

Catalin Marinas (3):
mm: kmemleak: Make the tool tolerant to struct scan_area allocation
failures
mm: kmemleak: Simple memory allocation pool for kmemleak objects
mm: kmemleak: Use the memory pool for early allocations

Quoted 4 lines

init/main.c | 2 +-
lib/Kconfig.debug | 11 +-
mm/kmemleak.c | 325 ++++++++++++----------------------------------
3 files changed, 91 insertions(+), 247 deletions(-)

We observe severe degradation in our network performance affecting all of our NICs. The degradation is directly linked to this patch.

Quoted 7 lines
Quoted 4 lines

What we run:
Simple Iperf TCP loopback with 8 streams on ConnectX5-100GbE. Since it's a loopback test, traffic goes from the socket through the IP stack and back to the socket, without going through the NIC driver.

Something similar was reported before. Can you try commit 2abd839aa7e6 ("kmemleak: Do not corrupt the object_list during clean-up") and see if it fixes the problem for you? It was merged in 5.4-rc4.

I've tested this commit, as well as 5.4.0-rc6 and 5.4.0 GA versions. We don't see the issue I've mentioned.

Thank you for the quick response and the assistance.