From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.migadu.com (out-14.mta1.migadu.com [95.215.58.14]) (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 078D53B5314 for ; Thu, 13 Aug 2026 09:29:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.14 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786613396; cv=none; b=h5dd2w29WpFehx231bzAQJr/b2tQ3N5V8hurI65mADYOozTBZsahmexZ1pdPjdWpHsCoowMCxt3uSWtZAuo7+t4ep7bYegbQZQSCxMfSRW4HL9GsmMFUPuBI/9FlKxbAu7lE/SpFfktQntpyFNLFwvwtpQhKNW6YUpcHuldqfdQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786613396; c=relaxed/simple; bh=zhgOHw63AcUJAplbKIEJvv7m1p0z7+bYI1CwdCRIEhw=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=UpYCmER4gixapOrFvbUdfU0bL+ad0RW636vA/kLjREECxjIBikk5UWE5s+93XWeojUrF2AeSwjI3/uCeHBqBxqbVlb0hKI7RTpq6hrREUPDRMXF1oi2/xu2SsfGZFiAOBx8AHW2MCWkUe71kgiO3ywC3VQdcg+GHM5iXtNYkxBg= 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=ALFQvN5N; arc=none smtp.client-ip=95.215.58.14 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="ALFQvN5N" X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=zhgOHw63AcUJAplbKIEJvv7m1p0z7+bYI1CwdCRIEhw=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1786613391; v=1; x=1787218191; b=ALFQvN5N8F4OP5NWs2iabjQ5BrA44Jw6Pz3aW5mt0W8hIKwIOab6EmELmQXa6XU83nieKMcN CXJQhpkvmgHfdf9cNFJIa3wefPAAqVgWa2o7ATk3pwIHL2g6YaQqbikqxciZcA/GZGSw8p5UdiB eArrUx1JzjAkiRcUFlDGUt+w= X-Envelope-To: linux-kernel@vger.kernel.org Received: from localhost.localdomain (223.70.159.239) by smtp.migadu.com with ESMTPS id 40f498756c1e9c2b; Thu, 13 Aug 2026 09:29:41 +0000 X-Migadu-Flow: FLOW_OUT From: Ye Liu To: Michal Hocko , Andrew Morton Cc: Ye Liu , David Rientjes , Shakeel Butt , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2] mm/oom_kill: simplify remaining RCU sections with scoped_guard(rcu) Date: Thu, 13 Aug 2026 17:29:32 +0800 Message-Id: <20260813092933.562028-1-ye.liu@linux.dev> X-Mailer: git-send-email 2.25.1 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-Transfer-Encoding: 8bit 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 --- 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