From patchwork Mon Apr 23 07:07:36 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Vorontsov X-Patchwork-Id: 8007 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 014B723E49 for ; Mon, 23 Apr 2012 07:09:26 +0000 (UTC) Received: from mail-iy0-f180.google.com (mail-iy0-f180.google.com [209.85.210.180]) by fiordland.canonical.com (Postfix) with ESMTP id A3D67A187DF for ; Mon, 23 Apr 2012 07:09:25 +0000 (UTC) Received: by iage36 with SMTP id e36so22276722iag.11 for ; Mon, 23 Apr 2012 00:09:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=x-forwarded-to:x-forwarded-for:delivered-to:received-spf:date:from :to:cc:subject:message-id:references:mime-version:content-type :content-disposition:in-reply-to:user-agent:x-gm-message-state; bh=07V4mjbk4HlopxJSxyY0MtnMvsZ+WXbo8z/2C3FXvW0=; b=dcNdouOs3foGIXkap/LrayEex/L7ABwWZAgCf4dz7MlGkrv3rBUz61AbgqoEwGa067 xafFk4ZP6wjvBSo7To012Uw+Z6xUveGEm0pSXk7869/QO9VOga8D31PpuE16p9NBdSTL 0O/wAjNbV7rp61TYeKd/FVIonrftAFcguM5ZYUvx2xLxLIdjVCnsmkc42OnR+tHKViME IOok1PrnFYzfy78xIgKv3vwqjBUUrQoMkEGk1Nrk1JOmnKmm9sOsNiUMGrfFZweA5Apl ORcq7q8Tp+W2XOAByAt8uhvoVQ20oSK6iANTcdcKCmKVnZRy6bCO92NeLnHp3jXWzVf1 T69A== Received: by 10.43.49.201 with SMTP id vb9mr876095icb.35.1335164965080; Mon, 23 Apr 2012 00:09:25 -0700 (PDT) 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.137.198 with SMTP id x6csp91196ibt; Mon, 23 Apr 2012 00:09:24 -0700 (PDT) Received: by 10.60.1.7 with SMTP id 7mr21055926oei.71.1335164964571; Mon, 23 Apr 2012 00:09:24 -0700 (PDT) Received: from mail-ob0-f178.google.com (mail-ob0-f178.google.com [209.85.214.178]) by mx.google.com with ESMTPS id k9si7123921obd.140.2012.04.23.00.09.24 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 23 Apr 2012 00:09:24 -0700 (PDT) Received-SPF: neutral (google.com: 209.85.214.178 is neither permitted nor denied by best guess record for domain of anton.vorontsov@linaro.org) client-ip=209.85.214.178; Authentication-Results: mx.google.com; spf=neutral (google.com: 209.85.214.178 is neither permitted nor denied by best guess record for domain of anton.vorontsov@linaro.org) smtp.mail=anton.vorontsov@linaro.org Received: by obbwc18 with SMTP id wc18so9042020obb.37 for ; Mon, 23 Apr 2012 00:09:24 -0700 (PDT) Received: by 10.182.152.72 with SMTP id uw8mr21108589obb.73.1335164964207; Mon, 23 Apr 2012 00:09:24 -0700 (PDT) Received: from localhost ([69.199.155.45]) by mx.google.com with ESMTPS id w4sm12213558oeg.12.2012.04.23.00.09.22 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 23 Apr 2012 00:09:23 -0700 (PDT) Date: Mon, 23 Apr 2012 00:07:36 -0700 From: Anton Vorontsov To: Andrew Morton , Oleg Nesterov Cc: Russell King , Mike Frysinger , Benjamin Herrenschmidt , Richard Weinberger , Paul Mundt , Peter Zijlstra , KOSAKI Motohiro , John Stultz , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, uclinux-dist-devel@blackfin.uclinux.org, linuxppc-dev@lists.ozlabs.org, linux-sh@vger.kernel.org, user-mode-linux-devel@lists.sourceforge.net, linaro-kernel@lists.linaro.org, patches@linaro.org, linux-mm@kvack.org Subject: [PATCH 1/9] cpu: Introduce clear_tasks_mm_cpumask() helper Message-ID: <20120423070736.GA30752@lizard> References: <20120423070641.GA27702@lizard> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20120423070641.GA27702@lizard> User-Agent: Mutt/1.5.21 (2010-09-15) X-Gm-Message-State: ALoCoQnlPE35tKEQ+Mq9LTrEc/PWVNJtqlkZY1JWIPdOucJXe9ZewixFyaTGcLrhiiy9jrgrsb/H Many architectures clear tasks' mm_cpumask like this: read_lock(&tasklist_lock); for_each_process(p) { if (p->mm) cpumask_clear_cpu(cpu, mm_cpumask(p->mm)); } read_unlock(&tasklist_lock); Depending on the context, the code above may have several problems, such as: 1. Working with task->mm w/o getting mm or grabing the task lock is dangerous as ->mm might disappear (exit_mm() assigns NULL under task_lock(), so tasklist lock is not enough). 2. Checking for process->mm is not enough because process' main thread may exit or detach its mm via use_mm(), but other threads may still have a valid mm. This patch implements a small helper function that does things correctly, i.e.: 1. We take the task's lock while whe handle its mm (we can't use get_task_mm()/mmput() pair as mmput() might sleep); 2. To catch exited main thread case, we use find_lock_task_mm(), which walks up all threads and returns an appropriate task (with task lock held). Also, Per Peter Zijlstra's idea, now we don't grab tasklist_lock in the new helper, instead we take the rcu read lock. We can do this because the function is called after the cpu is taken down and marked offline, so no new tasks will get this cpu set in their mm mask. Signed-off-by: Anton Vorontsov --- include/linux/cpu.h | 1 + kernel/cpu.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/linux/cpu.h b/include/linux/cpu.h index ee28844..d2ca49f 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -179,6 +179,7 @@ extern void put_online_cpus(void); #define hotcpu_notifier(fn, pri) cpu_notifier(fn, pri) #define register_hotcpu_notifier(nb) register_cpu_notifier(nb) #define unregister_hotcpu_notifier(nb) unregister_cpu_notifier(nb) +void clear_tasks_mm_cpumask(int cpu); int cpu_down(unsigned int cpu); #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE diff --git a/kernel/cpu.c b/kernel/cpu.c index 2060c6e..ecdf499 100644 --- a/kernel/cpu.c +++ b/kernel/cpu.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include #include #include #include @@ -171,6 +173,30 @@ void __ref unregister_cpu_notifier(struct notifier_block *nb) } EXPORT_SYMBOL(unregister_cpu_notifier); +void clear_tasks_mm_cpumask(int cpu) +{ + struct task_struct *p; + + /* + * This function is called after the cpu is taken down and marked + * offline, so its not like new tasks will ever get this cpu set in + * their mm mask. -- Peter Zijlstra + * Thus, we may use rcu_read_lock() here, instead of grabbing + * full-fledged tasklist_lock. + */ + rcu_read_lock(); + for_each_process(p) { + struct task_struct *t; + + t = find_lock_task_mm(p); + if (!t) + continue; + cpumask_clear_cpu(cpu, mm_cpumask(t->mm)); + task_unlock(t); + } + rcu_read_unlock(); +} + static inline void check_for_tasks(int cpu) { struct task_struct *p;