Skip to contentSkip to conversation

kmemleak: Add config to select auto scan

PATCH Superseded Prateek Patel 2 participants 2 messages Oct 17, 2018

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kmemleak: Add config to select auto scan

From: Sri Krishna chowdary <[email protected]>

Kmemleak scan is cpu intensive and can stall user tasks at times. To prevent this, add config DEBUG_KMEMLEAK_SCAN_ON to enable/disable auto scan on boot up.
Also protect first_run with CONFIG_DEBUG_KMEMLEAK_SCAN_ON as this is meant for only first automatic scan.

---
 lib/Kconfig.debug | 11 +++++++++++
 mm/kmemleak.c     |  6 ++++++
 2 files changed, 17 insertions(+)
lib/Kconfig.debug +11 -0
index e5e7c03..9542852 100644
@@ -593,6 +593,17 @@ config DEBUG_KMEMLEAK_DEFAULT_OFF
 	  Say Y here to disable kmemleak by default. It can then be enabled
 	  on the command line via kmemleak=on.
 
+config DEBUG_KMEMLEAK_SCAN_ON
+	bool "Enable kmemleak auto scan thread on boot up"
+	default y
+	depends on DEBUG_KMEMLEAK
+	help
+	  Kmemleak scan is cpu intensive and can stall user tasks at times.
+	  This option enables/disables automatic kmemleak scan at boot up.
+
+	  Say N here to disable kmemleak auto scan thread to stop automatic
+	  scanning.
+
 config DEBUG_STACK_USAGE
 	bool "Stack utilization instrumentation"
 	depends on DEBUG_KERNEL && !IA64
mm/kmemleak.c +6 -0
index 877de4f..ac53678 100644
@@ -1647,11 +1647,14 @@ static void kmemleak_scan(void)
  */
 static int kmemleak_scan_thread(void *arg)
 {
+#ifdef CONFIG_DEBUG_KMEMLEAK_SCAN_ON
 	static int first_run = 1;
+#endif
 
 	pr_info("Automatic memory scanning thread started\n");
 	set_user_nice(current, 10);
 
+#ifdef CONFIG_DEBUG_KMEMLEAK_SCAN_ON
 	/*
 	 * Wait before the first scan to allow the system to fully initialize.
 	 */
@@ -1661,6 +1664,7 @@ static int kmemleak_scan_thread(void *arg)
 		while (timeout && !kthread_should_stop())
 			timeout = schedule_timeout_interruptible(timeout);
 	}
+#endif
 
 	while (!kthread_should_stop()) {
 		signed long timeout = jiffies_scan_wait;
@@ -2141,9 +2145,11 @@ static int __init kmemleak_late_init(void)
 		return -ENOMEM;
 	}
 
+#ifdef CONFIG_DEBUG_KMEMLEAK_SCAN_ON
 	mutex_lock(&scan_mutex);
 	start_scan_thread();
 	mutex_unlock(&scan_mutex);
+#endif
 
 	pr_info("Kernel memory leak detector initialized\n");
 
Signature · 1 line
2.1.4
maintainer
#1 lore raw

lib/Kconfig.debug config DEBUG_KMEMLEAK_DEFAULT_OFF · line 596

Quoted 9 lines · Prateek Patel
lib/Kconfig.debug +1 -0
index e5e7c03..9542852 100644
@@ -593,6 +593,17 @@ config DEBUG_KMEMLEAK_DEFAULT_OFF
 	  Say Y here to disable kmemleak by default. It can then be enabled
 	  on the command line via kmemleak=on.
 
+config DEBUG_KMEMLEAK_SCAN_ON

Nitpick: DEBUG_KMEMLEAK_AUTO_SCAN may be a better name since you don't aim to disable scanning altogether.

lib/Kconfig.debug config DEBUG_KMEMLEAK_DEFAULT_OFF · line 601

Quoted 5 lines
lib/Kconfig.debug +5 -0
+	bool "Enable kmemleak auto scan thread on boot up"
+	default y
+	depends on DEBUG_KMEMLEAK
+	help
+	  Kmemleak scan is cpu intensive and can stall user tasks at times.

I guess that depends on the CPU.

lib/Kconfig.debug config DEBUG_KMEMLEAK_DEFAULT_OFF · line 605

Quoted 4 lines
lib/Kconfig.debug +4 -0
+	  This option enables/disables automatic kmemleak scan at boot up.
+
+	  Say N here to disable kmemleak auto scan thread to stop automatic
+	  scanning.

You should also mention that disabling this option also disables automatic reporting of memory leaks. And I'd add a "if unsure, say Y".

Quoted 9 lines
lib/Kconfig.debug +1 -0
+
 config DEBUG_STACK_USAGE
 	bool "Stack utilization instrumentation"
 	depends on DEBUG_KERNEL && !IA64
mm/kmemleak.c +0 -0
index 877de4f..ac53678 100644

mm/kmemleak.c static void kmemleak_scan(void) · line 1652

Quoted 6 lines
mm/kmemleak.c +2 -0
@@ -1647,11 +1647,14 @@ static void kmemleak_scan(void)
  */
 static int kmemleak_scan_thread(void *arg)
 {
+#ifdef CONFIG_DEBUG_KMEMLEAK_SCAN_ON
 	static int first_run = 1;
+#endif
	static int first_run = IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN);

mm/kmemleak.c static int kmemleak_scan_thread(void *arg) · line 1667

Quoted 13 lines
mm/kmemleak.c +2 -0
 	pr_info("Automatic memory scanning thread started\n");
 	set_user_nice(current, 10);
 
+#ifdef CONFIG_DEBUG_KMEMLEAK_SCAN_ON
 	/*
 	 * Wait before the first scan to allow the system to fully initialize.
 	 */
@@ -1661,6 +1664,7 @@ static int kmemleak_scan_thread(void *arg)
 		while (timeout && !kthread_should_stop())
 			timeout = schedule_timeout_interruptible(timeout);
 	}
+#endif

With the first_run change above, this #ifdef is no longer needed.

mm/kmemleak.c static int __init kmemleak_late_init(void) · line 2152

Quoted 12 lines
mm/kmemleak.c +2 -0
 	while (!kthread_should_stop()) {
 		signed long timeout = jiffies_scan_wait;
@@ -2141,9 +2145,11 @@ static int __init kmemleak_late_init(void)
 		return -ENOMEM;
 	}
 
+#ifdef CONFIG_DEBUG_KMEMLEAK_SCAN_ON
 	mutex_lock(&scan_mutex);
 	start_scan_thread();
 	mutex_unlock(&scan_mutex);
+#endif

Please use:

	if (IS_ENABLED(CONFIG_DEBUG_KMEMLEAK_AUTO_SCAN)) {
		...
	}
Signature · 1 line
Catalin