Re: [RFC PATCH v1 2/4] mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages

From
sashiko-bot@kernel.org
Date
2026-12-01 08:36:18
Also in
linux-mm
Reply to
Hirotaka Yamagata, 2026-11-30
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] mm/rmap: missing vma_is_volatile() check under PTL in
  try_to_unmap_one()
--

commit 72bf3612b141f201ae09c715262c4297b55bbf46
Author: Hirotaka Yamagata [off-list ref]

mm: add PTE_MARKER_PURGED and TTU_PURGE for purged volatile pages

This commit introduces the PTE_MARKER_PURGED marker and TTU_PURGE flag to
record when volatile anonymous pages are discarded under memory pressure.
quoted hunk ↗ jump to hunk
diff --git a/mm/rmap.c b/mm/rmap.c
index d1819fd699380..a3273a9a7d22d 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2359,6 +2359,20 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			pteval = swp_entry_to_pte(make_hwpoison_entry(page));
 			dec_mm_counter(mm, mm_counter(folio));
 			set_pte_at(mm, address, pvmw.pte, pteval);
+		} else if (flags & TTU_PURGE) {
+			/*
+			 * Like swap-out, back off if the page may be pinned.
+			 * See folio_try_share_anon_rmap(): clear PTE first.
+			 */
+			if (PageAnonExclusive(page) &&
+			    folio_try_share_anon_rmap_pte(folio, page)) {
+				set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
+				goto walk_abort;
+			}
+			pteval = make_purged_entry();
+			dec_mm_counter(mm, mm_counter(folio));
+			set_pte_at(mm, address, pvmw.pte, pteval);
+
 		} else if (likely(pte_present(pteval)) && pte_unused(pteval) &&
 			   !userfaultfd_armed(vma)) {
 			/*
[Severity: High]
Can try_to_unmap_one() install a PTE_MARKER_PURGED entry into a VMA after
madvise(MADV_NONVOLATILE) has already cleared VMA_VOLATILE_BIT and scanned
its PTEs?

In the follow-up commit "mm/volatile: purge volatile anonymous folios
during reclaim", __purge_volatile_folio() checks vma_is_volatile(vma)
under the anon_vma read lock, drops that lock, and then calls
try_to_unmap(folio, TTU_PURGE | TTU_SYNC).  Meanwhile, madvise_volatile()
with MADV_NONVOLATILE clears VMA_VOLATILE_BIT without taking folio_lock
(or anon_vma->root->rwsem when no VMA split or merge is needed) and walks
the page tables under PTL:

CPU0 (reclaim)                    CPU1 (MADV_NONVOLATILE)
__purge_volatile_folio()
  folio_lock_anon_vma_read()
  vma_is_volatile(vma) == true
  anon_vma_unlock_read()
                                  madvise_volatile()
                                    vma_clear_flags(..., VMA_VOLATILE_BIT)
                                    mvolatile_check_purged()
                                      lock(ptl)
                                      /* sees present PTE, returns 0 */
                                      unlock(ptl)
  try_to_unmap(..., TTU_PURGE)
    try_to_unmap_one()
      lock(pvmw.ptl)
      set_pte_at(..., make_purged_entry())
                                  /* later access to non-volatile VMA */
                                  handle_pte_marker()  <-- unexpected SIGBUS

Because try_to_unmap_one() does not recheck vma_is_volatile(vma) after
acquiring pvmw.ptl, MADV_NONVOLATILE reports that no pages were purged
while the page is discarded right afterward, causing later accesses to the
non-volatile mapping to raise SIGBUS.  Checking !vma_is_volatile(vma) under
pvmw.ptl in the TTU_PURGE branch (restoring the PTE and aborting the walk)
would close the race.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130083407.2669584-1-hiro@pisnp.net?part=2
Thread (13 messages) 13 messages, 5 authors, last reply 1d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help