Re: [PATCH 18/32] mm: consistently check MMF_UNSTABLE when populating in CoW mappings

From: David Hildenbrand (Arm)

Date: Wed Dec 02 2026 - 17:39:27 EST


On 12/2/26 22:46, Gregory Price (Meta) wrote:
> On 2026-11-29 11:06 +0100, David Hildenbrand (Arm) wrote:
>> The OOM reaper and process_mrelease() zap private mappings of a target
>> MM, including CoW'ed anonymous folios. Re-populating these page table
>> entries is bad: it will look like a memory corruption to anybody still
>> accessing the MM. For example, a read fault could map a pagecache folio
>> where the process previously mapped a CoW'ed anon folio.
>>
>> We must consistently disallow populating page table entries in CoW
>> mappings if MMF_UNSTABLE is set, and we must check under the page table
>> lock.
>>
>> We are still missing checks when populating empty page table entries
>> in CoW mappings in:
>> - filemap_map_pages() (fault-around)
>> - do_set_pmd() (fault-around, MADV_COLLAPSE)
>> - the vmf_insert_*() helpers (fs DAX, drivers)
>> - UFFDIO_COPY, UFFDIO_ZEROPAGE, UFFDIO_CONTINUE and UFFDIO_MOVE
>>
>> Let's fix it by adding these checks. To avoid duplicating code, add
>> vma_may_populate_pgtable() and document it properly. Also extend the
>> documentation of check_stable_address_space().
>>
>> Also fix the check in finish_fault(), which must really happen under the
>> page table lock to handle races with the OOM reaping correctly.
>>
>> After reaping, populating page table entries in CoW mappings now
>> consistently fails (e.g., with SIGBUS or -EFAULT).
>>
>> At some point we should probably consistently just use
>> vma_may_populate_pgtable() when populating; also, we can think about
>> checking for the existence of an anon_vma instead of checking for CoW
>> mappings. We should also make __oom_reap_task_mm() check for
>> vma_is_cow_mapping() instead.
>>
>> This has low urgency, as the target MM is about to go away anyway. No
>> need to CC stable.
>>
>> Fixes: aac453635549 ("mm, oom: introduce oom reaper")
>> Reported-by: Chris Mason <mason@xxxxxxxxxx>
>> Signed-off-by: David Hildenbrand (Arm) <david@xxxxxxxxxx>
>
> used uffd + a child locked in inode-lock wait and reproduced
> this pretty trivially.
>
> the new hooks everywhere seem regretable
Yeah, but we effectively had this already in place everywhere we were sure that
we are dealing with CoW mappings:

[...]
kernel/events/uprobes.c: if (check_stable_address_space(mm))
mm/hmm.c: if (check_stable_address_space(mm)) {
mm/huge_memory.c: ret = check_stable_address_space(vma->vm_mm);
mm/huge_memory.c: ret =
check_stable_address_space(vma->vm_mm);
mm/huge_memory.c: ret = check_stable_address_space(vma->vm_mm);
mm/memory.c: ret = check_stable_address_space(vma->vm_mm);
mm/memory.c: ret = check_stable_address_space(vma->vm_mm);
mm/memory.c: ret = check_stable_address_space(vma->vm_mm);
mm/migrate_device.c: csa_ret = check_stable_address_space(vma->vm_mm);
mm/migrate_device.c: if (check_stable_address_space(mm))
mm/rmap.c: check_stable_address_space(vma->vm_mm)) &&
mm/swapfile.c: if (check_stable_address_space(mm))

It's nasty that we have to deal with that.

I don't understand why we even keep the target process for an OOM running and
faulting in stuff in shared mappings etc.

Maybe at some point we can just block anything more consistently.

But I'm sure there is a history to all of that ...

--
Cheers,

David