From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795947158; b=yT05IYskN5DfAEz/L4XsJ2VncPaD7AUB5cthLwqZgBMKOIiTEAEV1WvOzwBQWZZv0JPF PqAb7COhi3pIIJ0Bk9dlUoxdVMblqmR/PqeZLVDPDfia9FrHHB7HbH79bWjLWsv/fK1Ln F/uWSIoHMNeRA6/lXeivaaq1Nmrguk/ubD0sQHg/2DFx1fIr7qpNYd1l13qaylaAE0rkG LP1kPXncwYyWj3Bk4Sx1vsZ2sIv1HEiiNL0DJbTba3sRcCu/4TpN7k74SvB8kNLJYcKSf DaitdFloKBB8XEHkNjzb8JBZO1cCvaG23HJat0/gmqZ94NtWwuU5/snakBdqWdkMAFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795947158; h=Received:DKIM-Signature:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key; bh=9UFukRnJw+IpdIYU+di5GiJhMfCwfue33MmXd2H9Uco=; b=hC+Ugm+5Pn7dvzWNtenHasjmKcLVVAOktKNGeojhXL/ThlvYIZiWMhvpNodn+B6vC30v GenIZN/l4fK5SvOqTZj8hsS01IZRUvnNOF2cX5JO6sLUwy3Y0pCJl14e2i+Pob/mH/V5V IWUgK/m7pjmV9qfS0EZQDmWaCiq6nXbWucQ/E8QU5+4Ne8F7uwPJC32h334lp+i3BC7ef xnSziDP8pj1F7mFL6VcPtY1AfhBXI0K07CaQqFF4mp12xE0kLAdq9ZuSjtrbudaTODYsm SFlYxsIAFDWRqxCH0BmmE73LujK8psX/+0JI9cw0kAb+z/KhrS97UeFaWDiD9h48+uA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KpyZrBX3; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1F96606A31C; Sun, 29 Nov 2026 10:12:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=KpyZrBX3 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80C491F00934; Sun, 29 Nov 2026 10:12:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795947158; bh=9UFukRnJw+IpdIYU+di5GiJhMfCwfue33MmXd2H9Uco=; h=From:Date:Subject:References:In-Reply-To:To:Cc; b=KpyZrBX346DALDuBdtilXWyoor8REO+njYQb0eQI3EWgG0b0o+7AFQVxvkGTX+NpJ 3in+mXQZ+c1MUzpxytoi3Yx8F/0AYSa5MqRvc4EZqWvWeQwapof3snx0Yx//Q2/Eg6 2nAPFdL4XcfXc62yxFVwY6l1WDgeihov3oaArdlQAa4QQWf5RHFTTbeIEltBbCxx8t RgixAXL+PaU2nf310hKvAU+t68jMHEWds3Tq9d5lbzfF9egMSIu9EnhLIAxyCMhkIn 9AeSCl5sUDrlSuMoc9N35dPAwDLg6kO+fCmQcfT6NcerUvFuwS9vFkgVE3lxLzku5s NBDpFz2ssn7Fw== From: "David Hildenbrand (Arm)" Date: Sun, 29 Nov 2026 11:06:49 +0100 Subject: [PATCH 18/32] mm: consistently check MMF_UNSTABLE when populating in CoW mappings X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-fixes_core_3-v1-18-f66094116801@kernel.org> References: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> In-Reply-To: <20261129-fixes_core_3-v1-0-f66094116801@kernel.org> To: Hugh Dickins , Baolin Wang , Andrew Morton , Mike Rapoport , Peter Xu , Michal Hocko , Kiryl Shutsemau , Manfred Spraul , Davidlohr Bueso , Cyrill Gorcunov , Zi Yan , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Lorenzo Stoakes , Rik van Riel , Vlastimil Babka , Harry Yoo , Jann Horn , Yang Shi , Zach O'Keefe , Song Liu , David Stevens , Kefeng Wang , Alex Williamson , Suren Baghdasaryan , Dave Jiang , Matthew Wilcox , Miaohe Lin , David Rientjes , Shakeel Butt , Jan Kara , Mel Gorman , Jason Gunthorpe , John Hubbard , Gerd Hoffmann , Vivek Kasireddy , Dave Airlie , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Hagen Paul Pfeifer , James Bottomley , Felix Kuehling , Alex Sierra , Balbir Singh , Kairui Song , Qi Zheng , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Oscar Salvador , Muchun Song , Dan Williams , Rakie Kim , Hao Li , Christoph Lameter , Roman Gushchin , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt , Alexei Starovoitov , Xu Xin , Chengming Zhou , Shivank Garg Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-cxl@vger.kernel.org, linux-rt-devel@lists.linux.dev, SJ Park , "David Hildenbrand (Arm)" , Chris Mason X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11198; i=david@kernel.org; h=from:subject:message-id; bh=74Yby+foEoky9iB1nWKyN1xlwRYQRvE9d4dm63inxbk=; b=owGbwMvMwCF2/OVpCdfQ3ZWMp9WSGLK5f+rG3zfa+s7GLD2gd970VVIT2R//lcid7M31Ou/TQ rbSFvtJHaUsDGIcDLJiiixBwfcNXr5cV1vtxscMM4eVCWQIAxenAEykRpiR4eKlH1N8ZXU619UW 8ftp26f/nl8XeGqCxLGlT5fvqznm/5GR4fVqjyWbn2QdvyZrOfFkm8r3qqagxQUuCoW3nV/r1fR vZwQA X-Developer-Key: i=david@kernel.org; a=openpgp; fpr=1BD9CAAD735C4C3A460DFCCA4DDE10F700FF835A 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 Signed-off-by: David Hildenbrand (Arm) --- include/linux/oom.h | 48 ++++++++++++++++++++++++++++++++++++++---------- mm/filemap.c | 9 +++++++++ mm/huge_memory.c | 14 ++++++++++++++ mm/memory.c | 30 ++++++++++++++++++++---------- mm/userfaultfd.c | 22 ++++++++++++++++++++++ 5 files changed, 103 insertions(+), 20 deletions(-) diff --git a/include/linux/oom.h b/include/linux/oom.h index 00da05d227e66..a3c16565160a5 100644 --- a/include/linux/oom.h +++ b/include/linux/oom.h @@ -76,18 +76,28 @@ static inline bool tsk_is_oom_victim(struct task_struct * tsk) return tsk->signal->oom_mm; } -/* - * Checks whether a page fault on the given mm is still reliable. - * This is no longer true if the oom reaper started to reap the - * address space which is reflected by MMF_UNSTABLE flag set in - * the mm. At that moment any !shared mapping would lose the content - * and could cause a memory corruption (zero pages instead of the - * original content). +/** + * check_stable_address_space - check whether the address space of an mm is + * still stable + * @mm: the mm to check + * + * Checks whether a page fault on the given mm is still reliable. This is no + * longer true once the OOM reaper or process_mrelease() started reaping the + * address space, which is reflected by MMF_UNSTABLE being set in the mm. At + * that moment, any CoW mapping might have lost content, and populating an + * empty page table entry could cause memory corruption (for example, zero + * pages instead of the original content). + * + * Before populating an empty page table entry, use + * vma_may_populate_pgtable(), which performs this check only for CoW + * mappings. Call this function directly only where the mapping is guaranteed + * to be a CoW mapping, such as a private anonymous mapping, or where the + * check does not relate to a specific mapping. * - * User should call this before establishing a page table entry for - * a !shared mapping and under the proper page table lock. + * When populating a page table entry, call this under the page table lock, + * after checking that the entry is empty and before populating it. * - * Return 0 when the PF is safe VM_FAULT_SIGBUS otherwise. + * Return: 0 if the address space is stable, VM_FAULT_SIGBUS otherwise. */ static inline vm_fault_t check_stable_address_space(struct mm_struct *mm) { @@ -96,6 +106,24 @@ static inline vm_fault_t check_stable_address_space(struct mm_struct *mm) return 0; } +/** + * vma_may_populate_pgtable - check whether empty page table entries may be + * populated + * @vma: the VMA to check + * + * Like check_stable_address_space(), but only performs the check for CoW + * mappings, as only these may have lost CoW'ed anonymous folios. + * + * Return: true if the empty page table entries may be populated, false + * otherwise. + */ +static inline bool vma_may_populate_pgtable(struct vm_area_struct *vma) +{ + if (!vma_is_cow_mapping(vma)) + return true; + return !check_stable_address_space(vma->vm_mm); +} + long oom_badness(struct task_struct *p, unsigned long totalpages); diff --git a/mm/filemap.c b/mm/filemap.c index d730b0ea46678..963eb334dce82 100644 --- a/mm/filemap.c +++ b/mm/filemap.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include "internal.h" @@ -3971,6 +3972,14 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, goto out; } + if (unlikely(!vma_may_populate_pgtable(vma))) { + ret = VM_FAULT_SIGBUS; + pte_unmap_unlock(vmf->pte, vmf->ptl); + folio_unlock(folio); + folio_put(folio); + goto out; + } + folio_type = mm_counter_file(folio); do { unsigned long end; diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 38b16333d3987..83906235c4df2 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1661,6 +1661,11 @@ static vm_fault_t insert_pmd(struct vm_area_struct *vma, unsigned long addr, goto out_unlock; } + if (!vma_may_populate_pgtable(vma)) { + ret = VM_FAULT_SIGBUS; + goto out_unlock; + } + if (fop.is_folio) { entry = folio_mk_pmd(fop.folio, vma->vm_page_prot); @@ -1801,6 +1806,11 @@ static vm_fault_t insert_pud(struct vm_area_struct *vma, unsigned long addr, goto out_unlock; } + if (!vma_may_populate_pgtable(vma)) { + ret = VM_FAULT_SIGBUS; + goto out_unlock; + } + if (fop.is_folio) { entry = folio_mk_pud(fop.folio, vma->vm_page_prot); @@ -3019,6 +3029,10 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm err = -EAGAIN; goto unlock_ptls; } + if (!vma_may_populate_pgtable(dst_vma)) { + err = -EFAULT; + goto unlock_ptls; + } if (src_folio) { if (folio_maybe_dma_pinned(src_folio) || !PageAnonExclusive(&src_folio->page)) { diff --git a/mm/memory.c b/mm/memory.c index 7b0aeb102eff7..2b295a84069fc 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2528,6 +2528,9 @@ static int insert_page_into_pte_locked(struct vm_area_struct *vma, pte_t *pte, return 0; } + if (!vma_may_populate_pgtable(vma)) + return -EFAULT; + /* Ok, finally just insert the thing.. */ pteval = mk_pte(page, prot); if (unlikely(is_zero_folio(folio))) { @@ -2923,6 +2926,7 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, pgprot_t prot) { struct mm_struct *mm = vma->vm_mm; + vm_fault_t ret = VM_FAULT_NOPAGE; pte_t *pte, entry; spinlock_t *ptl; int err; @@ -2937,6 +2941,11 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, if (!pte_none(entry)) goto out_unlock; + if (!vma_may_populate_pgtable(vma)) { + ret = VM_FAULT_SIGBUS; + goto out_unlock; + } + /* Ok, finally just insert the thing.. */ entry = pte_mkspecial(pfn_pte(pfn, prot)); @@ -2945,7 +2954,7 @@ static vm_fault_t insert_pfn(struct vm_area_struct *vma, unsigned long addr, out_unlock: pte_unmap_unlock(pte, ptl); - return VM_FAULT_NOPAGE; + return ret; } /** @@ -5826,6 +5835,11 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa if (unlikely(!pmd_none(*vmf->pmd))) goto out; + if (!vma_may_populate_pgtable(vma)) { + ret = VM_FAULT_SIGBUS; + goto out; + } + flush_icache_pages(vma, page, HPAGE_PMD_NR); entry = folio_mk_pmd(folio, vma->vm_page_prot); @@ -5962,15 +5976,6 @@ vm_fault_t finish_fault(struct vm_fault *vmf) page = vmf->page; folio = page_folio(page); - /* - * check even for read faults because we might have lost our CoWed - * page - */ - if (!(vma->vm_flags & VM_SHARED)) { - ret = check_stable_address_space(vma->vm_mm); - if (ret) - return ret; - } /* A poisoned page must take its own fault */ if (folio_contain_hwpoisoned_page(folio)) @@ -6050,6 +6055,11 @@ vm_fault_t finish_fault(struct vm_fault *vmf) goto fallback; } + if (!vma_may_populate_pgtable(vma)) { + ret = VM_FAULT_SIGBUS; + goto unlock; + } + if (!is_cow && unlikely(folio_ref_count_over_limit(folio))) { ret = VM_FAULT_SIGBUS; goto unlock; diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index 09e383370b48f..4da0c685aad44 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include "internal.h" @@ -372,6 +373,11 @@ static int mfill_atomic_install_pte(pmd_t *dst_pmd, if (!pte_none(dst_ptep) && !pte_is_uffd_marker(dst_ptep)) goto out_unlock; + if (!vma_may_populate_pgtable(dst_vma)) { + ret = -EFAULT; + goto out_unlock; + } + if (page_in_cache) { if (unlikely(folio_ref_count_over_limit(folio))) { ret = -ENOMEM; @@ -653,6 +659,10 @@ static int mfill_atomic_pte_zeropage(struct mfill_state *state) ret = -EEXIST; if (!pte_none(ptep_get(dst_pte))) goto out_unlock; + if (!vma_may_populate_pgtable(dst_vma)) { + ret = -EFAULT; + goto out_unlock; + } set_pte_at(dst_vma->vm_mm, dst_addr, dst_pte, _dst_pte); /* No need to invalidate - it was non-present before */ update_mmu_cache(dst_vma, dst_addr, dst_pte); @@ -1288,6 +1298,10 @@ static long move_present_ptes(struct mm_struct *mm, err = -EAGAIN; goto out; } + if (!vma_may_populate_pgtable(dst_vma)) { + err = -EFAULT; + goto out; + } if (folio_test_large(src_folio) || folio_maybe_dma_pinned(src_folio) || !PageAnonExclusive(&src_folio->page)) { @@ -1377,6 +1391,10 @@ static int move_swap_pte(struct mm_struct *mm, struct vm_area_struct *dst_vma, double_pt_unlock(dst_ptl, src_ptl); return -EAGAIN; } + if (!vma_may_populate_pgtable(dst_vma)) { + double_pt_unlock(dst_ptl, src_ptl); + return -EFAULT; + } /* * The src_folio resides in the swapcache, requiring an update to its @@ -1432,6 +1450,10 @@ static int move_zeropage_pte(struct mm_struct *mm, double_pt_unlock(dst_ptl, src_ptl); return -EAGAIN; } + if (!vma_may_populate_pgtable(dst_vma)) { + double_pt_unlock(dst_ptl, src_ptl); + return -EFAULT; + } zero_pte = pte_mkspecial(pfn_pte(zero_pfn(dst_addr), dst_vma->vm_page_prot)); -- 2.56.1