From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta0.migadu.com (out-246.mta0.migadu.com [91.218.175.246]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CC3643B4BF for ; Mon, 7 Sep 2026 08:14:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.246 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788768849; cv=none; b=BWuX7fKXrcO8IIsfHL4dnq8ySrvOuLIE/2I2SpZC/zaRQ3Hgjzz3MK8fbIk53VekxJyb2QwYodInlnGbYSDjn/hhkvrGMpH+0Yq609OKuH+gfr8nGaKehcR7yrGD0W17rORC2oD1iSa30cfuNqXzy+yA+FMIfSHcXD5D5Rg/7QA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788768849; c=relaxed/simple; bh=nMTgYk091e0ILTHhRnXGH4A+hErpEAVN/BDSGxT7uPw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Kt3DLSoasioHP43ynVT/ijb+Gg8tB38gpw67/OQLSGLfyKy4geM1HMeWQZ1nFeAyZ4z3L8L6G7dmGJljo8NqjGMyQafL/D4LlPEkJu6X7HxExXn6KWvb9vFhd7QHcr/dBxPd/pi5T1W7mGDseX83jiyRlWPpbTaj0FvrLe+y1po= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=fRc/27Rj; arc=none smtp.client-ip=91.218.175.246 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="fRc/27Rj" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=nMTgYk091e0ILTHhRnXGH4A+hErpEAVN/BDSGxT7uPw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1788768845; v=1; x=1789373645; b=fRc/27Rjb5/PrkOFN1fx1eZb0T5Uj4Qi6vPGV0FxSSZJ4oymS/zA0GU58VMKGHCBqAfkWM4h zFuuexu+23T9Nr22OHgWaPsDLqg49WrDyVwGLpG56T8jJMdPhKvWVlICc9BKOwB+MJGjF9wdp8w bm65RXoDeoZlBp/kUGh5J+pw= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id e2e364529cd50751; Mon, 07 Sep 2026 08:14:04 +0000 X-Mizu-Trace-ID: e2e364529cd50751 X-Migadu-Flow: FLOW_OUT From: Ye Liu To: Oleg Nesterov , "Peter Zijlstra (Intel)" , Marco Elver , Ye Liu , Tejun Heo , Christian Brauner , Bart Van Assche , Yi Tao Cc: Michal Hocko , paulmck@kernel.org, mingo@redhat.com, rostedt@goodmis.org, jpoimboe@kernel.org, mic@digikod.net, tglx@kernel.org, akpm@linux-foundation.org, david@kernel.org, linmiaohe@huawei.com, nao.horiguchi@gmail.com, rafael@kernel.org, gnoack3000@gmail.com, ljs@kernel.org, sj@kernel.org, rientjes@google.com, shakeel.butt@linux.dev, xu.xin16@zte.com.cn, chengming.zhou@linux.dev, will@kernel.org, boqun@kernel.org, frederic@kernel.org, neeraj.upadhyay@kernel.org, joelagnelf@nvidia.com, josh@joshtriplett.org, urezki@gmail.com, juri.lelli@redhat.com, vincent.guittot@linaro.org, tony.luck@intel.com, reinette.chatre@intel.com, x86@kernel.org, paul@paul-moore.com, jmorris@namei.org, serge@hallyn.com, pavel@kernel.org, lance.yang@linux.dev, mhiramat@kernel.org, pmladek@suse.com, longman@redhat.com, mathieu.desnoyers@efficios.com, jiangshanlai@gmail.com, qiang.zhang@linux.dev, dietmar.eggemann@arm.com, bsegall@google.com, mgorman@suse.de, vschneid@redhat.com, kprateek.nayak@amd.com, mark.rutland@arm.com, Dave.Martin@arm.com, james.morse@arm.com, babu.moger@amd.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org, rcu@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-security-module@vger.kernel.org Subject: [PATCH v2 1/8] sched: introduce for_each_process_rculock and for_each_thread_rculock Date: Mon, 7 Sep 2026 16:13:27 +0800 Message-Id: <20260907081334.1152889-2-ye.liu@linux.dev> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260907081334.1152889-1-ye.liu@linux.dev> References: <20260907081334.1152889-1-ye.liu@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Ye Liu Introduce for_each_process_rculock(), for_each_thread_rculock() and for_each_process_thread_rculock() macros that acquire the RCU read lock before the iteration starts and release it when the loop is left, so that the RCU read-side critical section is scoped to the loop body instead of an externally managed rcu_read_lock()/rcu_read_unlock() pair. Signed-off-by: Ye Liu Acked-by: Michal Hocko --- Changes in v2: - Rename macros from *_rcu to *_rculock (Steven Rostedt, Thomas Gleixner) - Improve comment on for_each_process_thread_rculock() double loop - Change prefix from mm: to sched: (Michal Hocko) include/linux/sched/signal.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/include/linux/sched/signal.h b/include/linux/sched/signal.h index 584ae88b435e..110ea9f131f3 100644 --- a/include/linux/sched/signal.h +++ b/include/linux/sched/signal.h @@ -2,6 +2,7 @@ #ifndef _LINUX_SCHED_SIGNAL_H #define _LINUX_SCHED_SIGNAL_H +#include #include #include #include @@ -663,6 +664,30 @@ extern bool current_is_single_threaded(void); #define for_each_process_thread(p, t) \ for_each_process(p) for_each_thread(p, t) +/* + * Variants of for_each_process(), for_each_thread() and + * for_each_process_thread() that automatically acquire and release the + * RCU read lock via scoped_guard(rcu). The lock is held for the + * duration of the loop and released on exit, including via break, + * goto, or return. + */ +#define for_each_process_rculock(p) \ + scoped_guard(rcu) \ + for (p = &init_task ; (p = next_task(p)) != &init_task ; ) + +#define for_each_thread_rculock(p, t) \ + scoped_guard(rcu) \ + __for_each_thread((p)->signal, t) + +/* + * Double loop: 'break' only exits the inner for_each_thread() loop. + * Use 'goto' to exit both loops; the RCU read lock is released + * automatically when the scoped_guard scope is left. + */ +#define for_each_process_thread_rculock(p, t) \ + scoped_guard(rcu) \ + for_each_process(p) for_each_thread(p, t) + typedef int (*proc_visitor)(struct task_struct *p, void *data); void walk_process_tree(struct task_struct *top, proc_visitor, void *); -- 2.25.1