[PATCH 10/10] memcg: wake v1 OOM lock contenders on unlock
From: Michal Hocko
Date: Wed Dec 02 2026 - 04:55:16 EST
From: Chris Mason <mason@xxxxxxxxxx>
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@xxxxxxxxxx>
Acked-by: Michal Hocko <mhocko@xxxxxxxx>
Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
---
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