From: Petr Mladek <pmladek@suse.com> To: Andrew Morton <akpm@linux-foundation.org>, Oleg Nesterov <oleg@redhat.com>, Tejun Heo <tj@kernel.org>, Ingo Molnar <mingo@redhat.com>, Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org>, "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>, Josh Triplett <josh@joshtriplett.org>, Thomas Gleixner <tglx@linutronix.de>, Linus Torvalds <torvalds@linux-foundation.org>, Jiri Kosina <jkosina@suse.cz>, Borislav Petkov <bp@suse.de>, Michal Hocko <mhocko@suse.cz>, linux-mm@kvack.org, Vlastimil Babka <vbabka@suse.cz>, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.com>, Catalin Marinas <catalin.marinas@arm.com> Subject: [PATCH v4 16/22] kmemleak: Convert kmemleak kthread into kthread worker API Date: Mon, 25 Jan 2016 16:45:05 +0100 [thread overview] Message-ID: <1453736711-6703-17-git-send-email-pmladek@suse.com> (raw) In-Reply-To: <1453736711-6703-1-git-send-email-pmladek@suse.com> 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
WARNING: multiple messages have this Message-ID (diff)
From: Petr Mladek <pmladek@suse.com> To: Andrew Morton <akpm@linux-foundation.org>, Oleg Nesterov <oleg@redhat.com>, Tejun Heo <tj@kernel.org>, Ingo Molnar <mingo@redhat.com>, Peter Zijlstra <peterz@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org>, "Paul E. McKenney" <paulmck@linux.vnet.ibm.com>, Josh Triplett <josh@joshtriplett.org>, Thomas Gleixner <tglx@linutronix.de>, Linus Torvalds <torvalds@linux-foundation.org>, Jiri Kosina <jkosina@suse.cz>, Borislav Petkov <bp@suse.de>, Michal Hocko <mhocko@suse.cz>, linux-mm@kvack.org, Vlastimil Babka <vbabka@suse.cz>, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek <pmladek@suse.com>, Catalin Marinas <catalin.marinas@arm.com> Subject: [PATCH v4 16/22] kmemleak: Convert kmemleak kthread into kthread worker API Date: Mon, 25 Jan 2016 16:45:05 +0100 [thread overview] Message-ID: <1453736711-6703-17-git-send-email-pmladek@suse.com> (raw) In-Reply-To: <1453736711-6703-1-git-send-email-pmladek@suse.com> 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 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo@kvack.org. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2016-01-25 15:52 UTC|newest] Thread overview: 82+ messages / expand[flat|nested] mbox.gz Atom feed top 2016-01-25 15:44 [PATCH v4 00/22] kthread: Use kthread worker API more widely Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 01/22] timer: Allow to check when the timer callback has not finished yet Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 18:44 ` Tejun Heo 2016-01-25 18:44 ` Tejun Heo 2016-01-25 15:44 ` [PATCH v4 02/22] kthread/smpboot: Do not park in kthread_create_on_cpu() Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 03/22] kthread: Allow to call __kthread_create_on_node() with va_list args Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 04/22] kthread: Add create_kthread_worker*() Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 18:53 ` Tejun Heo 2016-01-25 18:53 ` Tejun Heo 2016-02-16 15:44 ` Petr Mladek 2016-02-16 15:44 ` Petr Mladek 2016-02-16 16:08 ` Tejun Heo 2016-02-16 16:08 ` Tejun Heo 2016-02-16 16:08 ` Tejun Heo 2016-02-16 16:10 ` Petr Mladek 2016-02-16 16:10 ` Petr Mladek 2016-02-16 16:10 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 05/22] kthread: Add drain_kthread_worker() Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 06/22] kthread: Add destroy_kthread_worker() Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 07/22] kthread: Detect when a kthread work is used by more workers Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 18:57 ` Tejun Heo 2016-01-25 18:57 ` Tejun Heo 2016-01-25 18:57 ` Tejun Heo 2016-02-16 16:38 ` Petr Mladek 2016-02-16 16:38 ` Petr Mladek 2016-02-16 16:38 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 08/22] kthread: Initial support for delayed kthread work Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 19:04 ` Tejun Heo 2016-01-25 19:04 ` Tejun Heo 2016-01-25 19:04 ` Tejun Heo 2016-01-25 15:44 ` [PATCH v4 09/22] kthread: Allow to cancel " Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 19:17 ` Tejun Heo 2016-01-25 19:17 ` Tejun Heo 2016-02-19 16:22 ` Petr Mladek 2016-02-19 16:22 ` Petr Mladek 2016-01-25 15:44 ` [PATCH v4 10/22] kthread: Allow to modify delayed " Petr Mladek 2016-01-25 15:44 ` Petr Mladek 2016-01-25 19:19 ` Tejun Heo 2016-01-25 19:19 ` Tejun Heo 2016-01-25 15:45 ` [PATCH v4 11/22] kthread: Better support freezable kthread workers Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 19:21 ` Tejun Heo 2016-01-25 19:21 ` Tejun Heo 2016-01-25 15:45 ` [PATCH v4 12/22] kthread: Use try_lock_kthread_work() in flush_kthread_work() Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` [PATCH v4 13/22] mm/huge_page: Convert khugepaged() into kthread worker API Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` [PATCH v4 14/22] ring_buffer: Convert benchmark kthreads " Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` [PATCH v4 15/22] hung_task: Convert hungtaskd " Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` Petr Mladek [this message] 2016-01-25 15:45 ` [PATCH v4 16/22] kmemleak: Convert kmemleak kthread " Petr Mladek 2016-01-25 15:45 ` [PATCH v4 17/22] ipmi: Convert kipmi " Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` [PATCH v4 18/22] IB/fmr_pool: Convert the cleanup thread " Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` [PATCH v4 19/22] memstick/r592: Better synchronize debug messages in r592_io kthread Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` [PATCH v4 20/22] memstick/r592: convert r592_io kthread into kthread worker API Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 15:45 ` [PATCH v4 21/22] thermal/intel_powerclamp: Remove duplicated code that starts the kthread Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 16:23 ` Jacob Pan 2016-01-25 16:23 ` Jacob Pan 2016-01-25 15:45 ` [PATCH v4 22/22] thermal/intel_powerclamp: Convert the kthread to kthread worker API Petr Mladek 2016-01-25 15:45 ` Petr Mladek 2016-01-25 16:28 ` Jacob Pan 2016-01-25 16:28 ` Jacob Pan
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1453736711-6703-17-git-send-email-pmladek@suse.com \ --to=pmladek@suse.com \ --cc=akpm@linux-foundation.org \ --cc=bp@suse.de \ --cc=catalin.marinas@arm.com \ --cc=jkosina@suse.cz \ --cc=josh@joshtriplett.org \ --cc=linux-api@vger.kernel.org \ --cc=linux-kernel@vger.kernel.org \ --cc=linux-mm@kvack.org \ --cc=mhocko@suse.cz \ --cc=mingo@redhat.com \ --cc=oleg@redhat.com \ --cc=paulmck@linux.vnet.ibm.com \ --cc=peterz@infradead.org \ --cc=rostedt@goodmis.org \ --cc=tglx@linutronix.de \ --cc=tj@kernel.org \ --cc=torvalds@linux-foundation.org \ --cc=vbabka@suse.cz \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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.