mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Michal Hocko <mhocko@kernel.org>
To: David Hildenbrand <david@kernel.org>
Cc: David Rientjes <rientjes@google.com>,
	Ben Gardon <bgardon@google.com>,
	Johannes Weiner <hannes@cmpxchg.org>,
	mhocko@suse.com, Andrew Morton <akpm@linux-foundation.org>,
	<linux-mm@kvack.org>, LKML <linux-kernel@vger.kernel.org>,
	Chris Mason <mason@kernel.org>
Subject: [PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock
Date: Wed,  2 Dec 2026 10:54:03 +0100	[thread overview]
Message-ID: <20261202095450.296675-11-mhocko@kernel.org> (raw)
In-Reply-To: <20261202095450.296675-1-mhocko@kernel.org>

From: Chris Mason <mason@kernel.org>

With the OOM killer disabled in a cgroup v1 memcg, a lost wakeup can
leave a task that contends for the memcg OOM lock asleep indefinitely in
mem_cgroup_oom_synchronize(), with no OOM notification sent for it.

Only the task that wins mem_cgroup_oom_trylock() notifies userspace. A
task that loses the trylock sleeps without a notification of its own, so
the holder has to wake it when the lock is dropped. Commit 18b1d18bc2bd
("memcg, oom: remove explicit wakeup in mem_cgroup_oom_synchronize()")
removed that wakeup, because userspace triggers one. But userspace
answers the holder's notification, and its wakeup only reaches the tasks
that are on memcg_oom_waitq at that time:

    holder A                      contender B
    ========                      ===========
    mem_cgroup_oom_trylock()
    mem_cgroup_oom_notify()
    schedule()
                                  try_charge_memcg() fails
    woken by memcg1_oom_recover()
                                  prepare_to_wait()
                                  mem_cgroup_oom_trylock() fails
                                  schedule()
    mem_cgroup_oom_unlock()

The lock covers a subtree, so the holder can also be at an ancestor of
the contender's memcg: another waiter, or the OOM killer in the charge
path, where memcg1_oom_finish() drops the lock without a wakeup as well.
The contender can then sleep on while its memcg is still at the limit,
again without a notification of its own.

The fix used here is to wake the contenders from the unlock itself, so
that every path that drops the memcg OOM lock wakes them.

Userspace can get one more OOM notification after the holder has left,
if the memcg is still at its limit. An OOM kill in the charge path that
held the lock now also walks memcg_oom_waitq, which holds the waiters of
all memcgs, and is empty on cgroup v2.

Fixes: 18b1d18bc2bd ("memcg, oom: remove explicit wakeup in mem_cgroup_oom_synchronize()")
Signed-off-by: Chris Mason <mason@kernel.org>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: David Hildenbrand (Arm) <david@kernel.org>
---
 mm/memcontrol-v1.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index f2391eea4e0d..bb850d1f0092 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -81,6 +81,7 @@ static struct lockdep_map memcg_oom_lock_dep_map = {
 #endif
 
 DEFINE_SPINLOCK(memcg_oom_lock);
+static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
 
 static u64 mem_cgroup_move_charge_read(struct cgroup_subsys_state *css,
 				struct cftype *cft)
@@ -958,6 +959,12 @@ static void mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
 	for_each_mem_cgroup_tree(iter, memcg)
 		iter->oom_lock = false;
 	spin_unlock(&memcg_oom_lock);
+
+	/*
+	 * A task that lost the trylock sleeps without a notification of its
+	 * own, so nothing but the unlock is sure to wake it.
+	 */
+	__wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
 }
 
 static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
@@ -985,8 +992,6 @@ static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
 	spin_unlock(&memcg_oom_lock);
 }
 
-static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
-
 struct oom_wait_info {
 	struct mem_cgroup *memcg;
 	wait_queue_entry_t	wait;
-- 
2.56.1


  parent reply	other threads:[~2026-12-02  9:55 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:53 [PATCH 0/10] assorted oom fixes Michal Hocko
2026-12-02  9:53 ` [PATCH 01/10] memcg-v1, oom: do not sleep in worker contexts Michal Hocko
2026-12-02  9:53 ` [PATCH 02/10] mm/oom_kill: keep the allocating mm eligible Michal Hocko
2026-12-02  9:53 ` [PATCH 03/10] mm, oom: do not panic on constrained OOMs Michal Hocko
2026-12-02  9:53 ` [PATCH 04/10] mm/oom_kill: ratelimit memcg OOM reports with no victim Michal Hocko
2026-12-02  9:53 ` [PATCH 05/10] mm, oom: thaw tasks sharing the mm with the oom victim Michal Hocko
2026-12-02  9:53 ` [PATCH 06/10] mm, oom: check oom victims prior to numa placement Michal Hocko
2026-12-02  9:54 ` [PATCH 07/10] memcg, oom: wait for rcu_call callbacks before retrying reclaim Michal Hocko
2026-12-02  9:54 ` [PATCH 08/10] mm, " Michal Hocko
2026-12-02  9:54 ` [PATCH 09/10] mm, oom: add rcu_barrier before hiding victim from the oom killer Michal Hocko
2026-12-02  9:54 ` Michal Hocko [this message]
2026-12-02 13:06 ` [PATCH 0/10] assorted oom fixes David Hildenbrand (Arm)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202095450.296675-11-mhocko@kernel.org \
    --to=mhocko@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=bgardon@google.com \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mason@kernel.org \
    --cc=mhocko@suse.com \
    --cc=rientjes@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®