[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <1447853127-3461-17-git-send-email-pmladek@suse.com>
Date: Wed, 18 Nov 2015 14:25:21 +0100
From: Petr Mladek <pmladek@...e.com>
To: Andrew Morton <akpm@...ux-foundation.org>,
Oleg Nesterov <oleg@...hat.com>, Tejun Heo <tj@...nel.org>,
Ingo Molnar <mingo@...hat.com>,
Peter Zijlstra <peterz@...radead.org>
Cc: Steven Rostedt <rostedt@...dmis.org>,
"Paul E. McKenney" <paulmck@...ux.vnet.ibm.com>,
Josh Triplett <josh@...htriplett.org>,
Thomas Gleixner <tglx@...utronix.de>,
Linus Torvalds <torvalds@...ux-foundation.org>,
Jiri Kosina <jkosina@...e.cz>, Borislav Petkov <bp@...e.de>,
Michal Hocko <mhocko@...e.cz>, linux-mm@...ck.org,
Vlastimil Babka <vbabka@...e.cz>, linux-api@...r.kernel.org,
linux-kernel@...r.kernel.org, Petr Mladek <pmladek@...e.com>,
Catalin Marinas <catalin.marinas@....com>
Subject: [PATCH v3 16/22] kmemleak: Convert kmemleak kthread into kthread worker API
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@...e.com>
CC: Catalin Marinas <catalin.marinas@....com>
---
mm/kmemleak.c | 86 +++++++++++++++++++++++++++++------------------------------
1 file changed, 42 insertions(+), 44 deletions(-)
diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 19423a45d7d7..cb447d86a0e1 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -217,7 +217,9 @@ 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 void kmemleak_scan_func(struct kthread_work *dummy);
+static DEFINE_DELAYED_KTHREAD_WORK(kmemleak_scan_work, kmemleak_scan_func);
/* used to avoid reporting of recently allocated objects */
static unsigned long jiffies_min_age;
static unsigned long jiffies_last_scan;
@@ -1471,54 +1473,46 @@ 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;
+ 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);
}
/*
@@ -1527,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");
}
/*
@@ -1727,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) {
@@ -1739,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();
@@ -1963,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
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@...r.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Powered by blists - more mailing lists