From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) (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 84BBE37F314 for ; Thu, 13 Aug 2026 11:40:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786621256; cv=none; b=PzuDX+Pv7PjEG4aTQvnwC1IR6gODC8hwBR4yai7l/lX2zPAp2P98HLqeAfGS5ptTf0PBAgGQJwBqrqs2oeKjV3Zle9YECSM5hGlcmfBbxazRAKrYN94ZnB2iR2xnFM5G4Xa9+Rj+VRwwyzvamrAMnE8xi0ZL3ue4qgIeJ2vR9MY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786621256; c=relaxed/simple; bh=tfVwPiaHeMjNqAEUrhsq4jvGvRBg5MInim0v6imje4w=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=I7RRZy7kEd+O9PAHqLJk9ll5NNOdjVKPWp5X3plwjPXzC4UlCt9SvSj7tEJ3jajuoW+4qkhX75Cltu0PEHmQZrb5F0nDHQ08BuGuRipmbjbsi05LtDZtuiMVotv8/rRRnaeqHfY5/7d31cU+p/fJuceL3J3jAR9fC2xKrhHAJfQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=PspXvw1A; arc=none smtp.client-ip=209.85.128.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="PspXvw1A" Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-49553515a8bso28721385e9.1 for ; Thu, 13 Aug 2026 04:40:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1786621253; x=1787226053; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:from:to:cc:subject:date:message-id:reply-to:content-type; bh=DDI4z7xvtwPvzwcl/BrUgP03BBaO6PU0ELtLn2RsLHQ=; b=PspXvw1AAE2l8RHzt35Cc8f+qLYVnBhqn62n3UTcBAmP4d7NB07AjeQ2AmIpgjqPBc eBxYhNpX6wELxFuc9YJDZFGgw2Wg9suhfpHhv4A4OrCt3OVZ/ddlDJAW/kpGJkas+HXq bcYwujuHLxaASXNz4p01kFtcHLxn4XBCIihlYjd4OOZYOybFIRN5GJLlAfO6eaAxocyg DBo3IcFoobRqlpmU9GNx0Sr3w6VJuxWSlTAGtwpGqGgM+yy9mWESCd/7Dc1JRk+aVAGl vm4Cqo4J9bmAAQEI4H4gecEjInEbJQ0oiylLkdxvA6a5welVqsADaKSQa7HT1nznhQNX zcWA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786621253; x=1787226053; h=in-reply-to:content-transfer-encoding:content-disposition :content-type:mime-version:references:message-id:subject:cc:to:from :date:x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to:content-type; bh=DDI4z7xvtwPvzwcl/BrUgP03BBaO6PU0ELtLn2RsLHQ=; b=s4O+vAPTKUYHIRvfdRCIxErNx8SBdBEEims+M8AD1zuIHArA/vqGx2ISNyZlq37ldL GRianCVVf0wdGD52hrqoXrmh85k7JComBdiUKBx7Vc8lV4kDcO6xWMiGUHWhob5IIFrv S9/jqTnrrb8bTu6j6w37xvGxfDX9ygmhPhNt2PNkqRr+vOOqxgUp5Zr1rOdtLsm+oPye UH4eiSOzv/nx692bxums4eVU6FiMAbmdcq8A2NStJYv5N/lZz5ue2DaM1Wyvq38jlNlm 5Or2XdvkI0HjkFRJYKWOLSO7YQXwK7/WNaQrMT6+iuFKkQOTTGYOfMMs0b5nbQJdGhZE 2dXQ== X-Forwarded-Encrypted: i=1; AHgh+RrI+Hr20s+/oR+p+QGJKwMK+j546IDPA7JSCpBkIzAOuV4OHzyCu7O4EmYrS9Mf0zTJkq0MB2/Hykw/QBw=@vger.kernel.org X-Gm-Message-State: AOJu0YwZ8na7E2p8qL8klur44mxkPuNbzrQIMbhTsid9wQo0r+OjwrW8 zCXBk7nyE5hJ8X3Me/DuDVOTlYmZcc4/icY6YfUYAf/WFMA9bM9mPqmX5xvZI52HXdA= X-Gm-Gg: AR+sD13ARwZEhNQo0+FpRLVE+dhi7+JvUobnF+MwH59bDAhYpODs4jOc22uDy8+in0k 2rIe9WS6+g7hfN4gi6UbZrTfjGHdWfYEhtJewl1CO31abDJ/x6pn2mU8Y8z9SAB8BPZQYQUeTZQ v8AUm48moiEw5NxGANfJmDEzGdkWmEnIzVPFtxWCDrbGOKbBkCigoPE276/ebluwiBHbwuYXlGk 8g0t+urNDITafOqHRP5Jg/xA+gAkzFEHzdnTe+Zvm3bM9bNDQMcObw0y9xREI4Dp8/d6Ji0iD/d AGh3ljsH0EGzu5wYoviFnbzpPsIucoEpyLYK3oWX3hOBk6CayjaeiKM+4JF0f+tlAGHsWPxf//+ cwizLm1Leyw4GPCXJHMs2AeYqVHJ9/cPVh1MYHnpXOIq2Af6LbxRHQqZO4dWyZOLQv6qKAbhUGS sCTnIN1cDAB+vC06QiWFcCXBRX4vcHW2Ss6UVinDS2mx3g0BgaMwKmNOcS5hhDS0BqYRvlZKo= X-Received: by 2002:a05:600c:45d5:b0:496:ca1f:a428 with SMTP id 5b1f17b1804b1-499821d26demr50094245e9.19.1786621252545; Thu, 13 Aug 2026 04:40:52 -0700 (PDT) Received: from localhost (109-81-29-60.rct.o2.cz. [109.81.29.60]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49982121649sm55382155e9.1.2026.08.13.04.40.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Aug 2026 04:40:52 -0700 (PDT) Date: Thu, 13 Aug 2026 13:40:51 +0200 From: Michal Hocko To: Ye Liu Cc: Andrew Morton , Ye Liu , David Rientjes , Shakeel Butt , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] mm/oom_kill: simplify remaining RCU sections with scoped_guard(rcu) Message-ID: References: <20260813092933.562028-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-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20260813092933.562028-1-ye.liu@linux.dev> On Thu 13-08-26 17:29:32, Ye Liu wrote: > From: Ye Liu > > Replace the remaining manual rcu_read_lock()/rcu_read_unlock() pairs > in oom_cpuset_eligible(), select_bad_process(), dump_tasks(), > task_will_free_mem(), and __oom_kill_process() with scoped_guard(rcu) > for consistency and simpler control flow. scoped_guard(rcu) limits > the RCU critical section to the loop body rather than the entire > remaining function scope, making the protected region explicit and > safer for future changes. > > Signed-off-by: Ye Liu It almost looks like for_each_thread and for_each_process could gain an rcu varian that would do the rcu thing internally. Have you considered that? I am pretty sure there will be more cases like these. Anyway Acked-by: Michal Hocko Thanks > --- > v2: > - Use scoped_guard instead of guard. > - Link:https://lore.kernel.org/all/20260813032634.344946-1-ye.liu@linux.dev/ > > mm/oom_kill.c | 124 +++++++++++++++++++++++++------------------------- > 1 file changed, 62 insertions(+), 62 deletions(-) > > diff --git a/mm/oom_kill.c b/mm/oom_kill.c > index 5f372f6e26fa..0e8982e5be51 100644 > --- a/mm/oom_kill.c > +++ b/mm/oom_kill.c > @@ -94,27 +94,27 @@ static bool oom_cpuset_eligible(struct task_struct *start, > bool ret = false; > const nodemask_t *mask = oc->nodemask; > > - rcu_read_lock(); > - for_each_thread(start, tsk) { > - if (mask) { > - /* > - * If this is a mempolicy constrained oom, tsk's > - * cpuset is irrelevant. Only return true if its > - * mempolicy intersects current, otherwise it may be > - * needlessly killed. > - */ > - ret = mempolicy_in_oom_domain(tsk, mask); > - } else { > - /* > - * This is not a mempolicy constrained oom, so only > - * check the mems of tsk's cpuset. > - */ > - ret = cpuset_mems_allowed_intersects(current, tsk); > + scoped_guard(rcu) { > + for_each_thread(start, tsk) { > + if (mask) { > + /* > + * If this is a mempolicy constrained oom, tsk's > + * cpuset is irrelevant. Only return true if its > + * mempolicy intersects current, otherwise it may be > + * needlessly killed. > + */ > + ret = mempolicy_in_oom_domain(tsk, mask); > + } else { > + /* > + * This is not a mempolicy constrained oom, so only > + * check the mems of tsk's cpuset. > + */ > + ret = cpuset_mems_allowed_intersects(current, tsk); > + } > + if (ret) > + break; > } > - if (ret) > - break; > } > - rcu_read_unlock(); > > return ret; > } > @@ -368,11 +368,11 @@ static void select_bad_process(struct oom_control *oc) > else { > struct task_struct *p; > > - rcu_read_lock(); > - for_each_process(p) > - if (oom_evaluate_task(p, oc)) > - break; > - rcu_read_unlock(); > + scoped_guard(rcu) { > + for_each_process(p) > + if (oom_evaluate_task(p, oc)) > + break; > + } > } > } > > @@ -430,14 +430,14 @@ static void dump_tasks(struct oom_control *oc) > struct task_struct *p; > int i = 0; > > - rcu_read_lock(); > - for_each_process(p) { > - /* Avoid potential softlockup warning */ > - if ((++i & 1023) == 0) > - touch_softlockup_watchdog(); > - dump_task(p, oc); > + scoped_guard(rcu) { > + for_each_process(p) { > + /* Avoid potential softlockup warning */ > + if ((++i & 1023) == 0) > + touch_softlockup_watchdog(); > + dump_task(p, oc); > + } > } > - rcu_read_unlock(); > } > } > > @@ -894,17 +894,17 @@ static bool task_will_free_mem(struct task_struct *task) > * are dying as well to make sure that a) nobody pins its mm and > * b) the task is also reapable by the oom reaper. > */ > - rcu_read_lock(); > - for_each_process(p) { > - if (!process_shares_mm(p, mm)) > - continue; > - if (same_thread_group(task, p)) > - continue; > - ret = __task_will_free_mem(p); > - if (!ret) > - break; > + scoped_guard(rcu) { > + for_each_process(p) { > + if (!process_shares_mm(p, mm)) > + continue; > + if (same_thread_group(task, p)) > + continue; > + ret = __task_will_free_mem(p); > + if (!ret) > + break; > + } > } > - rcu_read_unlock(); > > return ret; > } > @@ -960,29 +960,29 @@ static void __oom_kill_process(struct task_struct *victim, const char *message) > * That thread will now get access to memory reserves since it has a > * pending fatal signal. > */ > - rcu_read_lock(); > - for_each_process(p) { > - if (!process_shares_mm(p, mm)) > - continue; > - if (same_thread_group(p, victim)) > - continue; > - if (is_global_init(p)) { > - can_oom_reap = false; > - mm_flags_set(MMF_OOM_SKIP, mm); > - pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n", > - task_pid_nr(victim), victim->comm, > - task_pid_nr(p), p->comm); > - continue; > + scoped_guard(rcu) { > + for_each_process(p) { > + if (!process_shares_mm(p, mm)) > + continue; > + if (same_thread_group(p, victim)) > + continue; > + if (is_global_init(p)) { > + can_oom_reap = false; > + mm_flags_set(MMF_OOM_SKIP, mm); > + pr_info("oom killer %d (%s) has mm pinned by %d (%s)\n", > + task_pid_nr(victim), victim->comm, > + task_pid_nr(p), p->comm); > + continue; > + } > + /* > + * No kthread_use_mm() user needs to read from the userspace so > + * we are ok to reap it. > + */ > + if (unlikely(p->flags & PF_KTHREAD)) > + continue; > + do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID); > } > - /* > - * No kthread_use_mm() user needs to read from the userspace so > - * we are ok to reap it. > - */ > - if (unlikely(p->flags & PF_KTHREAD)) > - continue; > - do_send_sig_info(SIGKILL, SEND_SIG_PRIV, p, PIDTYPE_TGID); > } > - rcu_read_unlock(); > > if (can_oom_reap) > queue_oom_reaper(victim); > -- > 2.25.1 -- Michal Hocko SUSE Labs