Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1316897

[PATCH v4 16/22] kmemleak: Convert kmemleak kthread into kthread worker API

From Petr Mladek <pmladek@suse.com>
Newsgroups linux.kernel
Subject [PATCH v4 16/22] kmemleak: Convert kmemleak kthread into kthread worker API
Date 2016-01-25 17:00 +0100
Message-ID <qURIF-68y-67@gated-at.bofh.it> (permalink)
References <qURyW-64A-19@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


Kthreads are currently implemented as an infinite loop. Each
has its own variant of checks for terminating, freezing,
awakening. In many cases it is unclear to say in which state
it is and sometimes it is done a wrong way.

The plan is to convert kthreads into kthread_worker or workqueues
API. It allows to split the functionality into separate operations.
It helps to make a better structure. Also it defines a clean state
where no locks are taken, IRQs blocked, the kthread might sleep
or even be safely migrated.

The kthread worker API is useful when we want to have a dedicated
single thread for the work. It helps to make sure that it is
available when needed. Also it allows a better control, e.g.
define a scheduling priority.

This patch converts the kmemleak kthread into the kthread worker
API because it modifies the scheduling priority.

The result is a simple self-queuing work that just calls kmemleak_scan().

The info messages and set_user_nice() are moved to the functions that
start and stop the worker. These are also renamed to mention worker
instead of thread.

We do not longer need to handle a spurious wakeup and count the remaining
timeout. It is handled by the worker. The delayed work is queued after
the full timeout passes.

Finally, the initial delay is done only when the kthread is started
during the boot. For this we added a parameter to the start function.

Signed-off-by: Petr Mladek <pmladek@suse.com>
CC: Catalin Marinas <catalin.marinas@arm.com>
---
 mm/kmemleak.c | 87 +++++++++++++++++++++++++++++------------------------------
 1 file changed, 43 insertions(+), 44 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 25c0ad36fe38..16f1a7bb1697 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -216,7 +216,8 @@ static int kmemleak_error;
 static unsigned long min_addr = ULONG_MAX;
 static unsigned long max_addr;
 
-static struct task_struct *scan_thread;
+static struct kthread_worker *kmemleak_scan_worker;
+static struct delayed_kthread_work kmemleak_scan_work;
 /* used to avoid reporting of recently allocated objects */
 static unsigned long jiffies_min_age;
 static unsigned long jiffies_last_scan;
@@ -1470,54 +1471,48 @@ static void kmemleak_scan(void)
 }
 
 /*
- * Thread function performing automatic memory scanning. Unreferenced objects
- * at the end of a memory scan are reported but only the first time.
+ * Kthread worker function performing automatic memory scanning.
+ * Unreferenced objects at the end of a memory scan are reported
+ * but only the first time.
  */
-static int kmemleak_scan_thread(void *arg)
+static void kmemleak_scan_func(struct kthread_work *dummy)
 {
-	static int first_run = 1;
-
-	pr_info("Automatic memory scanning thread started\n");
-	set_user_nice(current, 10);
-
-	/*
-	 * Wait before the first scan to allow the system to fully initialize.
-	 */
-	if (first_run) {
-		first_run = 0;
-		ssleep(SECS_FIRST_SCAN);
-	}
-
-	while (!kthread_should_stop()) {
-		signed long timeout = jiffies_scan_wait;
-
-		mutex_lock(&scan_mutex);
-		kmemleak_scan();
-		mutex_unlock(&scan_mutex);
-
-		/* wait before the next scan */
-		while (timeout && !kthread_should_stop())
-			timeout = schedule_timeout_interruptible(timeout);
-	}
-
-	pr_info("Automatic memory scanning thread ended\n");
+	mutex_lock(&scan_mutex);
+	kmemleak_scan();
+	mutex_unlock(&scan_mutex);
 
-	return 0;
+	queue_delayed_kthread_work(kmemleak_scan_worker, &kmemleak_scan_work,
+				   jiffies_scan_wait);
 }
 
 /*
  * Start the automatic memory scanning thread. This function must be called
  * with the scan_mutex held.
  */
-static void start_scan_thread(void)
+static void start_scan_thread(bool boot)
 {
-	if (scan_thread)
+	unsigned long timeout = 0;
+
+	if (kmemleak_scan_worker)
 		return;
-	scan_thread = kthread_run(kmemleak_scan_thread, NULL, "kmemleak");
-	if (IS_ERR(scan_thread)) {
-		pr_warning("Failed to create the scan thread\n");
-		scan_thread = NULL;
+
+	init_delayed_kthread_work(&kmemleak_scan_work, kmemleak_scan_func);
+	kmemleak_scan_worker = create_kthread_worker(0, "kmemleak");
+	if (IS_ERR(kmemleak_scan_worker)) {
+		pr_warn("Failed to create the memory scan worker\n");
+		kmemleak_scan_worker = NULL;
 	}
+	pr_info("Automatic memory scanning thread started\n");
+	set_user_nice(kmemleak_scan_worker->task, 10);
+
+	/*
+	 * Wait before the first scan to allow the system to fully initialize.
+	 */
+	if (boot)
+		timeout = msecs_to_jiffies(SECS_FIRST_SCAN * MSEC_PER_SEC);
+
+	queue_delayed_kthread_work(kmemleak_scan_worker, &kmemleak_scan_work,
+				   timeout);
 }
 
 /*
@@ -1526,10 +1521,14 @@ static void start_scan_thread(void)
  */
 static void stop_scan_thread(void)
 {
-	if (scan_thread) {
-		kthread_stop(scan_thread);
-		scan_thread = NULL;
-	}
+	if (!kmemleak_scan_worker)
+		return;
+
+	cancel_delayed_kthread_work_sync(&kmemleak_scan_work);
+	destroy_kthread_worker(kmemleak_scan_worker);
+	kmemleak_scan_worker = NULL;
+
+	pr_info("Automatic memory scanning thread ended\n");
 }
 
 /*
@@ -1726,7 +1725,7 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
 	else if (strncmp(buf, "stack=off", 9) == 0)
 		kmemleak_stack_scan = 0;
 	else if (strncmp(buf, "scan=on", 7) == 0)
-		start_scan_thread();
+		start_scan_thread(false);
 	else if (strncmp(buf, "scan=off", 8) == 0)
 		stop_scan_thread();
 	else if (strncmp(buf, "scan=", 5) == 0) {
@@ -1738,7 +1737,7 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
 		stop_scan_thread();
 		if (secs) {
 			jiffies_scan_wait = msecs_to_jiffies(secs * 1000);
-			start_scan_thread();
+			start_scan_thread(false);
 		}
 	} else if (strncmp(buf, "scan", 4) == 0)
 		kmemleak_scan();
@@ -1962,7 +1961,7 @@ static int __init kmemleak_late_init(void)
 	if (!dentry)
 		pr_warning("Failed to create the debugfs kmemleak file\n");
 	mutex_lock(&scan_mutex);
-	start_scan_thread();
+	start_scan_thread(true);
 	mutex_unlock(&scan_mutex);
 
 	pr_info("Kernel memory leak detector initialized\n");
-- 
1.8.5.6

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH v4 00/22] kthread: Use kthread worker API more widely Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
  [PATCH v4 21/22] thermal/intel_powerclamp: Remove duplicated code that starts the kthread Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
    Re: [PATCH v4 21/22] thermal/intel_powerclamp: Remove duplicated  code that starts the kthread Jacob Pan <jacob.jun.pan@linux.intel.com> - 2016-01-25 17:30 +0100
  [PATCH v4 18/22] IB/fmr_pool: Convert the cleanup thread into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
  [PATCH v4 03/22] kthread: Allow to call __kthread_create_on_node() with va_list args Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
  [PATCH v4 19/22] memstick/r592: Better synchronize debug messages in r592_io kthread Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
  [PATCH v4 12/22] kthread: Use try_lock_kthread_work() in flush_kthread_work() Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
  [PATCH v4 17/22] ipmi: Convert kipmi kthread into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
  [PATCH v4 08/22] kthread: Initial support for delayed kthread work Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
    Re: [PATCH v4 08/22] kthread: Initial support for delayed kthread  work Tejun Heo <tj@kernel.org> - 2016-01-25 20:10 +0100
  [PATCH v4 01/22] timer: Allow to check when the timer callback has not finished yet Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
    Re: [PATCH v4 01/22] timer: Allow to check when the timer callback  has not finished yet Tejun Heo <tj@kernel.org> - 2016-01-25 19:50 +0100
  [PATCH v4 06/22] kthread: Add destroy_kthread_worker() Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
  [PATCH v4 22/22] thermal/intel_powerclamp: Convert the kthread to kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 16:50 +0100
    Re: [PATCH v4 22/22] thermal/intel_powerclamp: Convert the kthread  to kthread worker API Jacob Pan <jacob.jun.pan@linux.intel.com> - 2016-01-25 17:40 +0100
  [PATCH v4 20/22] memstick/r592: convert r592_io kthread into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 17:00 +0100
  [PATCH v4 15/22] hung_task: Convert hungtaskd into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 17:00 +0100
  [PATCH v4 16/22] kmemleak: Convert kmemleak kthread into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 17:00 +0100
  [PATCH v4 13/22] mm/huge_page: Convert khugepaged() into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 17:00 +0100
  [PATCH v4 14/22] ring_buffer: Convert benchmark kthreads into kthread worker API Petr Mladek <pmladek@suse.com> - 2016-01-25 17:00 +0100
  [PATCH v4 11/22] kthread: Better support freezable kthread workers Petr Mladek <pmladek@suse.com> - 2016-01-25 17:10 +0100
    Re: [PATCH v4 11/22] kthread: Better support freezable kthread  workers Tejun Heo <tj@kernel.org> - 2016-01-25 20:30 +0100
  [PATCH v4 09/22] kthread: Allow to cancel kthread work Petr Mladek <pmladek@suse.com> - 2016-01-25 17:20 +0100
    Re: [PATCH v4 09/22] kthread: Allow to cancel kthread work Tejun Heo <tj@kernel.org> - 2016-01-25 20:20 +0100
  [PATCH v4 02/22] kthread/smpboot: Do not park in kthread_create_on_cpu() Petr Mladek <pmladek@suse.com> - 2016-01-25 17:20 +0100
  [PATCH v4 07/22] kthread: Detect when a kthread work is used by more workers Petr Mladek <pmladek@suse.com> - 2016-01-25 17:20 +0100
    Re: [PATCH v4 07/22] kthread: Detect when a kthread work is used by  more workers Tejun Heo <tj@kernel.org> - 2016-01-25 20:00 +0100
  [PATCH v4 04/22] kthread: Add create_kthread_worker*() Petr Mladek <pmladek@suse.com> - 2016-01-25 17:20 +0100
    Re: [PATCH v4 04/22] kthread: Add create_kthread_worker*() Tejun Heo <tj@kernel.org> - 2016-01-25 20:00 +0100

csiph-web