From patchwork Wed Feb 29 15:21:13 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Antipov X-Patchwork-Id: 7008 Return-Path: X-Original-To: patchwork@peony.canonical.com Delivered-To: patchwork@peony.canonical.com Received: from fiordland.canonical.com (fiordland.canonical.com [91.189.94.145]) by peony.canonical.com (Postfix) with ESMTP id 16F9523DEE for ; Wed, 29 Feb 2012 15:21:23 +0000 (UTC) Received: from mail-gx0-f180.google.com (mail-gx0-f180.google.com [209.85.161.180]) by fiordland.canonical.com (Postfix) with ESMTP id BA541A1815F for ; Wed, 29 Feb 2012 15:21:22 +0000 (UTC) Received: by gglu1 with SMTP id u1so1838189ggl.11 for ; Wed, 29 Feb 2012 07:21:22 -0800 (PST) Received: from mr.google.com ([10.50.187.168]) by 10.50.187.168 with SMTP id ft8mr690408igc.55.1330528882120 (num_hops = 1); Wed, 29 Feb 2012 07:21:22 -0800 (PST) Received: by 10.50.187.168 with SMTP id ft8mr576758igc.55.1330528882031; Wed, 29 Feb 2012 07:21:22 -0800 (PST) X-Forwarded-To: linaro-patchwork@canonical.com X-Forwarded-For: patch@linaro.org linaro-patchwork@canonical.com Delivered-To: patches@linaro.org Received: by 10.231.53.18 with SMTP id k18csp5447ibg; Wed, 29 Feb 2012 07:21:21 -0800 (PST) Received: by 10.204.157.140 with SMTP id b12mr319113bkx.80.1330528880608; Wed, 29 Feb 2012 07:21:20 -0800 (PST) Received: from mail-bk0-f50.google.com (mail-bk0-f50.google.com [209.85.214.50]) by mx.google.com with ESMTPS id ui9si12845049bkb.103.2012.02.29.07.21.20 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 29 Feb 2012 07:21:20 -0800 (PST) Received-SPF: neutral (google.com: 209.85.214.50 is neither permitted nor denied by best guess record for domain of dmitry.antipov@linaro.org) client-ip=209.85.214.50; Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.214.50 is neither permitted nor denied by best guess record for domain of dmitry.antipov@linaro.org) smtp.mail=dmitry.antipov@linaro.org Received: by bkuw11 with SMTP id w11so3913461bku.37 for ; Wed, 29 Feb 2012 07:21:20 -0800 (PST) Received-SPF: pass (google.com: domain of dmitry.antipov@linaro.org designates 10.204.141.15 as permitted sender) client-ip=10.204.141.15; Received: from mr.google.com ([10.204.141.15]) by 10.204.141.15 with SMTP id k15mr352303bku.132.1330528880061 (num_hops = 1); Wed, 29 Feb 2012 07:21:20 -0800 (PST) MIME-Version: 1.0 Received: by 10.204.141.15 with SMTP id k15mr289089bku.132.1330528879846; Wed, 29 Feb 2012 07:21:19 -0800 (PST) Received: from localhost.localdomain ([78.153.153.8]) by mx.google.com with ESMTPS id jd17sm38182578bkb.4.2012.02.29.07.21.17 (version=SSLv3 cipher=OTHER); Wed, 29 Feb 2012 07:21:19 -0800 (PST) From: Dmitry Antipov To: Andrew Morton Cc: Rusty Russell , linux-kernel@vger.kernel.org, linaro-dev@lists.linaro.org, patches@linaro.org, Dmitry Antipov Subject: [PATCH] module: debugging check for runaway kthreads Date: Wed, 29 Feb 2012 19:21:13 +0400 Message-Id: <1330528873-7738-1-git-send-email-dmitry.antipov@linaro.org> X-Mailer: git-send-email 1.7.7.6 X-Gm-Message-State: ALoCoQlsOFs+cPCelGP0YxRHCTBPhvt12xTngZ6gMqEOPYnzDqmeNY482NJIhtsv6bozfvHGG6Cf Debugging option CONFIG_MODULE_KTHREAD_CHECK provides a way to check whether all kernel threads created by the module and have used module code as a thread worker function are really exited when the module is unloaded. The following pseudo-code contains example of an error which is likely to be catched with this debugging check: static struct task_struct *tsk; static DECLARE_COMPLETION(done); static void *func(void *unused) { while (!kthread_should_stop()) real_work(); complete(&done); } static int __init modinit(void) { tsk = kthread_run(func, NULL, "func"); return IS_ERR(tsk) ? PTR_ERR(tsk) : 0; } static void __exit modexit(void) { wait_for_completion(&done); } Reviewed-by: Andrew Morton Signed-off-by: Dmitry Antipov --- include/linux/kthread.h | 3 ++ kernel/kthread.c | 38 +++++++++++++++++++++++++++++++++++ kernel/module.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig.debug | 9 ++++++++ 4 files changed, 101 insertions(+), 0 deletions(-) diff --git a/include/linux/kthread.h b/include/linux/kthread.h index 0714b24..202fe14 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -13,6 +13,9 @@ struct task_struct *kthread_create_on_node(int (*threadfn)(void *data), #define kthread_create(threadfn, data, namefmt, arg...) \ kthread_create_on_node(threadfn, data, -1, namefmt, ##arg) +#ifdef CONFIG_MODULE_KTHREAD_CHECK +unsigned long get_kthread_func(struct task_struct *tsk); +#endif /** * kthread_run - create and wake a thread. diff --git a/kernel/kthread.c b/kernel/kthread.c index 3d3de63..e6f7977 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -38,6 +38,13 @@ struct kthread_create_info struct kthread { int should_stop; +#ifdef CONFIG_MODULE_KTHREAD_CHECK + /* + * Kthread worker function, i.e. first argument + * passed to kthread_create() and kthread_run(). + */ + void *fn; +#endif void *data; struct completion exited; }; @@ -45,6 +52,32 @@ struct kthread { #define to_kthread(tsk) \ container_of((tsk)->vfork_done, struct kthread, exited) +#ifdef CONFIG_MODULE_KTHREAD_CHECK + +/* + * Assuming the task is a kernel thread, try to get it's worker + * function, i.e. the first argument of kthread_create()/kthread_run(). + */ +unsigned long get_kthread_func(struct task_struct *tsk) +{ + struct kthread *kt; + unsigned long addr; + + get_task_struct(tsk); + BUG_ON(!(tsk->flags & PF_KTHREAD)); + kt = to_kthread(tsk); + barrier(); + /* + * Note kt is valid only if vfork_done is initialized. + * See kthread() to check why it's so. + */ + addr = tsk->vfork_done ? (unsigned long)kt->fn : 0UL; + put_task_struct(tsk); + return addr; +} + +#endif /* CONFIG_MODULE_KTHREAD_CHECK */ + /** * kthread_should_stop - should this kthread return now? * @@ -106,8 +139,13 @@ static int kthread(void *_create) int ret; self.should_stop = 0; +#ifdef CONFIG_MODULE_KTHREAD_CHECK + /* Will be used by get_kthread_func(). */ + self.fn = threadfn; +#endif self.data = data; init_completion(&self.exited); + /* Setup self so to_kthread() macro may be used. */ current->vfork_done = &self.exited; /* OK, tell user we're spawned, wait for stop or wakeup */ diff --git a/kernel/module.c b/kernel/module.c index 2c93276..7ad8a03 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -45,6 +45,7 @@ #include #include #include +#include #include #include #include @@ -223,6 +224,8 @@ extern const unsigned long __start___kcrctab_unused[]; extern const unsigned long __start___kcrctab_unused_gpl[]; #endif +static void check_kthreads(struct module *mod); + #ifndef CONFIG_MODVERSIONS #define symversion(base, idx) NULL #else @@ -831,6 +834,7 @@ SYSCALL_DEFINE2(delete_module, const char __user *, name_user, blocking_notifier_call_chain(&module_notify_list, MODULE_STATE_GOING, mod); async_synchronize_full(); + check_kthreads(mod); /* Store the name of the last unloaded module for diagnostic purposes */ strlcpy(last_unloaded_module, mod->name, sizeof(last_unloaded_module)); @@ -3274,8 +3278,55 @@ int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, } return 0; } + +#else /* not CONFIG_KALLSYMS */ + +static inline const char *get_ksymbol(struct module *mod, + unsigned long addr, + unsigned long *size, + unsigned long *offset) +{ + return ""; +} + #endif /* CONFIG_KALLSYMS */ +#ifdef CONFIG_MODULE_KTHREAD_CHECK + +static void check_kthreads(struct module *mod) +{ + unsigned long flags; + struct task_struct *g, *p; + + read_lock_irqsave(&tasklist_lock, flags); + do_each_thread(g, p) { + const char *name; + unsigned long addr, offset, size; + + /* Note kthreadd is special. Other kthreads should + have their struct kthread on the stack until + do_exit() calls schedule() for the last time. */ + if (p->mm || p == kthreadd_task) + continue; + + addr = get_kthread_func(p); + if (__module_text_address(addr) == mod) { + name = get_ksymbol(mod, addr, &size, &offset); + printk(KERN_WARNING "kthread %p[%s:%d] running " + "0x%lx(%s) is still alive, fix module %s, " + "crash possible\n", p, p->comm, p->pid, + addr, name, mod->name); + } + } while_each_thread(g, p); + read_unlock_irqrestore(&tasklist_lock, flags); +} + +#else + +static void check_kthreads(struct module *mod) {} + +#endif /* CONFIG_MODULE_KTHREAD_CHECK */ + static char *module_flags(struct module *mod, char *buf) { int bx = 0; diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 8745ac7..f082a76 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -1121,6 +1121,15 @@ config SYSCTL_SYSCALL_CHECK to properly maintain and use. This enables checks that help you to keep things correct. +config MODULE_KTHREAD_CHECK + bool "Check for runaway kernel threads at module unload" + depends on MODULE_UNLOAD && EXPERIMENTAL && DEBUG_KERNEL + help + This option allows you to check whether all kernel threads created + by the module and have used module code as a thread worker function + are really exited when the module is unloaded. This is mainly for + module developers. If insure, say N. + source mm/Kconfig.debug source kernel/trace/Kconfig