Re: [PATCH 01/10] memcg-v1, oom: do not sleep in worker contexts
From: Michal Hocko
Date: Wed Dec 02 2026 - 16:28:44 EST
On Wed 02-12-26 12:21:55, Joshua Hahn wrote:
> On Wed, Dec 02, 2026 at 10:53:54AM +0100, Michal Hocko wrote:
> > 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.
>
> Hi Michal, I hope you are doing well. Thank you for the fix!
>
> This was a fun fix to review because I wasn't entirely familiar with how
> v1 handles OOMs when the killer is disabled, so I got to learn about
> this mechanism.
Thanks for the review. Yes, this feature is quite tricky because it has
loose ends quite far away and a unusual worker context change while
sleeping inside a worker makes this hard to spot.
> The fix looks good to me, thanks!
> Reviewed-by: Joshua Hahn <joshua.hahnjy@xxxxxxxxx>
>
> And I also reproduced the issue and saw that I would get a KASAN
> slab-use-after-free before, but no such error with this patch applied.
> I don't have easy access to real IOMMU or SVA devices, so I wrote a
> module to exercise the path that you note in the commit. It just
> mimics iommu_sva_handle_mm().
> Tested-by: Joshua Hahn <joshua.hahnjy@xxxxxxxxx>
Yes, this is the most trivial way to trigger that. Thanks for trying.
> > 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
> >
> >
--
Michal Hocko
SUSE Labs