[PATCH 01/10] memcg-v1, oom: do not sleep in worker contexts

From: Michal Hocko

Date: Wed Dec 02 2026 - 04:55:04 EST


From: Michal Hocko <mhocko@xxxxxxxx>

SVA device faults at the limit of a v1 memcg with the OOM killer
disabled can leak a css reference, or cause a use-after-free of the
memcg.

handle_mm_fault() enables the memcg OOM handling for FAULT_FLAG_USER, so
a failed charge can leave a css reference in current->memcg_in_oom. On
VM_FAULT_OOM the caller has to drop it in pagefault_out_of_memory(). But
iommu_sva_handle_mm() sets the flag for faults that it handles in a
kworker, and never makes that call. The pointer stays on the kworker,
and fork copies it to usermode helpers without a reference:

kworker usermode helper
======= ===============
iommu_sva_handle_mm()
memcg1_oom_prepare()
css_get()
current->memcg_in_oom = memcg
call_usermodehelper_exec_work()
dup_task_struct()
handle_mm_fault()
css_put()
iommu_sva_handle_mm()
handle_mm_fault()
css_put()

Fix this by explicitly avoid oom_disabled path for workers. This would
simply fail the charge and bail out without triggering the oom killer
same as if this happened outside of #PF handler.

Fixes: fc36479db74e ("iommu: Add a page fault handler")
Cc: stable
Reported-by: Chris Mason <mason@xxxxxxxxxx>
Signed-off-by: Michal Hocko <mhocko@xxxxxxxx>
---
mm/memcontrol-v1.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mm/memcontrol-v1.c b/mm/memcontrol-v1.c
index b7f286888507..f2391eea4e0d 100644
--- a/mm/memcontrol-v1.c
+++ b/mm/memcontrol-v1.c
@@ -1100,7 +1100,8 @@ bool memcg1_oom_prepare(struct mem_cgroup *memcg, bool *locked)
* victim and then we have to bail out from the charge path.
*/
if (READ_ONCE(memcg->oom_kill_disable)) {
- if (current->in_user_fault) {
+ if (current->in_user_fault &&
+ !(current->flags & (PF_WQ_WORKER|PF_IO_WORKER))) {
css_get(&memcg->css);
current->memcg_in_oom = memcg;
}
--
2.56.1