On v4.4-rt, drain_all_stock() calls drain_local_stock() directly for the local CPU, in the context of the task that hit the memcg limit in try_charge(). Since commit 6026be2f750c ("mm/memcontrol: Don't call schedule_work_on in preemption disabled context") that path holds only get_cpu_light(), which disables migration but not preemption; without CONFIG_SMP it is just barrier(). For the other CPUs it runs from a per-CPU worker, which is pinned but also preemptible. consume_stock() and refill_stock() use get_cpu_var() and assume they have the per-CPU stock to themselves, but drain_stock() can be preempted. It reads stock->nr_pages separately for the memory uncharge, the memsw uncharge and css_put_many(), then clears nr_pages, drops the css reference and clears stock->cached. If another task on the same CPU uses the stock in between: - pages taken from the stock after they were uncharged leave usage below cache+rss; - a batch remainder added before nr_pages is cleared stays charged with nothing behind it, and memsw no longer matches memory when it lands between the two uncharges; - a refill between clearing nr_pages and clearing cached leaves the stock as {NULL, N}. Older 4.4-rt kernels then oops in page_counter_uncharge() on the next refill_stock(); with the "if (!old) return;" check the N pages go to whichever memcg charges next. On a single-CPU ARM board running PREEMPT_RT_FULL at its memcg limit, a stress test makes memsw and memory drift apart within an hour. In the field, memory and memsw were over-counted until the memcg was OOM-killed with far less memory in use, and a 4.4.62-rt73 based kernel hit the NULL pointer oops. Disable preemption in drain_local_stock() with get_cpu_var(), as consume_stock() and refill_stock() do. This is fine on RT: drain_stock() only calls the lockless page_counter_uncharge() and css_put*(), which releases through swork, and refill_stock() already calls it with preemption disabled since commit 8f3f54d23507 ("Revert "memcontrol: Prevent scheduling while atomic in cgroup code""). With this patch the stress test shows no drift and, with CONFIG_DEBUG_ATOMIC_SLEEP=y and CONFIG_DEBUG_PREEMPT=y, no splats. Mainline and v4.19-rt are not affected: drain_local_stock() runs with interrupts disabled there since commit db2ba40c277d ("mm: memcontrol: make per-cpu charge cache IRQ-safe for socket accounting"). Fixes: 6026be2f750c ("mm/memcontrol: Don't call schedule_work_on in preemption disabled context") Signed-off-by: Takuo Koguchi --- v2: - Tag the subject for linux-4.4.y-cip-rt (against v4.4.302-cip115-rt63). - Rewrite the changelog to explain the race (v1 read like a warning report) and add Fixes:. - Comment: get_cpu_light() disables migration but not preemption. - No functional change. v1: https://lore.kernel.org/r/20261125043900.3618611-1-takuo.koguchi.sw@hitachi.com mm/memcontrol.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/mm/memcontrol.c b/mm/memcontrol.c index f9d80952723c9..adaedb369c26a 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -1933,14 +1933,22 @@ static void drain_stock(struct memcg_stock_pcp *stock) } /* - * This must be called under preempt disabled or must be called by - * a thread which is pinned to local cpu. + * Drain this cpu's stock. Called directly by drain_all_stock() for the + * local cpu and as a work item for the other cpus. Preemption is + * disabled here because neither caller guarantees it on PREEMPT_RT: + * drain_all_stock() only holds get_cpu_light(), which disables + * migration but not preemption, and the work item runs in a pinned but + * preemptible kworker. consume_stock() and refill_stock() run with + * preemption disabled, so this keeps every user of the per-cpu stock + * serialized. */ static void drain_local_stock(struct work_struct *dummy) { - struct memcg_stock_pcp *stock = this_cpu_ptr(&memcg_stock); + struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock); + drain_stock(stock); clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags); + put_cpu_var(memcg_stock); } /* -- 2.34.1