mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared.patch removed from -mm tree
The quilt patch titled
Subject: mm: convert folio_likely_mapped_shared() to folio_maybe_mapped_shared()
has been removed from the -mm tree. Its filename was
mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared.patch
This patch was dropped because it was merged into the mm-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
------------------------------------------------------
From: David Hildenbrand <[email protected]>
Subject: mm: convert folio_likely_mapped_shared() to folio_maybe_mapped_shared()
Date: Mon, 3 Mar 2025 17:30:07 +0100
Let's reuse our new MM ownership tracking infrastructure for large folios to make folio_likely_mapped_shared() never return false negatives -- never indicating "not mapped shared" although the folio *is* mapped shared. With that, we can rename it to folio_maybe_mapped_shared() and get rid of the dependency on the mapcount of the first folio page.
The semantics are now arguably clearer: no mixture of "false negatives" and "false positives", only the remaining possibility for "false positives".
Thoroughly document the new semantics. We might now detect that a large folio is "maybe mapped shared" although it *no longer* is -- but once was. Now, if more than two MMs mapped a folio at the same time, and the MM mapping the folio exclusively at the end is not one tracked in the two folio MM slots, we will detect the folio as "maybe mapped shared".
For anonymous folios, usually (except weird corner cases) all PTEs that target a "maybe mapped shared" folio are R/O. As soon as a child process would write to them (iow, actively use them), we would CoW and effectively replace these PTEs. Most cases (below) are not expected to really matter with large anonymous folios for this reason.
Most importantly, there will be no change at all for:
* small folios
* hugetlb folios
* PMD-mapped PMD-sized THPs (single mapping)
This change has the potential to affect existing callers of folio_likely_mapped_shared() -> folio_maybe_mapped_shared():
(1) fs/proc/task_mmu.c: no change (hugetlb)
(2) khugepaged counts PTEs that target shared folios towards max_ptes_shared (default: HPAGE_PMD_NR / 2), meaning we could skip a collapse where we would have previously collapsed. This only applies to anonymous folios and is not expected to matter in practice.
Worth noting that this change sorts out case (A) documented in commit 1bafe96e89f0 ("mm/khugepaged: replace page_mapcount() check by folio_likely_mapped_shared()") by removing the possibility for "false negatives".
(3) MADV_COLD / MADV_PAGEOUT / MADV_FREE will not try splitting PTE-mapped THPs that are considered shared but not fully covered by the requested range, consequently not processing them.
PMD-mapped PMD-sized THP are not affected, or when all PTEs are covered. These functions are usually only called on anon/file folios that are exclusively mapped most of the time (no other file mappings or no fork()), so the "false negatives" are not expected to matter in practice.
(4) mbind() / migrate_pages() / move_pages() will refuse to migrate shared folios unless MPOL_MF_MOVE_ALL is effective (requires CAP_SYS_NICE). We will now reject some folios that could be migrated.
Similar to (3), especially with MPOL_MF_MOVE_ALL, so this is not expected to matter in practice.
Note that cpuset_migrate_mm_workfn() calls do_migrate_pages() with MPOL_MF_MOVE_ALL.
(5) NUMA hinting
mm/migrate.c:migrate_misplaced_folio_prepare() will skip file folios that are probably shared libraries (-> "mapped shared" and executable). This check would have detected it as a shared library at some point (at least 3 MMs mapping it), so detecting it afterwards does not sound wrong (still a shared library). Not expected to matter.
mm/memory.c:numa_migrate_check() will indicate TNF_SHARED in MAP_SHARED file mappings when encountering a shared folio. Similar reasoning, not expected to matter.
mm/mprotect.c:change_pte_range() will skip folios detected as shared in CoW mappings. Similarly, this is not expected to matter in practice, but if it would ever be a problem we could relax that check a bit (e.g., basing it on the average page-mapcount in a folio), because it was only an optimization when many (e.g., 288) processes were mapping the same folios -- see commit 859d4adc3415 ("mm: numa: do not trap faults on shared data section pages.")
(6) mm/rmap.c:folio_referenced_one() will skip exclusive swapbacked folios in dying processes. Applies to anonymous folios only. Without "false negatives", we'll now skip all actually shared ones. Skipping ones that are actually exclusive won't really matter, it's a pure optimization, and is not expected to matter in practice.
In theory, one can detect the problematic scenario: folio_mapcount() > 0 and no folio MM slot is occupied ("state unknown"). One could reset the MM slots while doing an rmap walk, which migration / folio split already do when setting everything up. Further, when batching PTEs we might naturally learn about a owner (e.g., folio_mapcount() == nr_ptes) and could update the owner. However, we'll defer that until the scenarios where it would really matter are clear.
---
fs/proc/task_mmu.c | 4 ++-- include/linux/mm.h | 43 ++++++++++++++++++++++--------------------- mm/huge_memory.c | 2 +- mm/khugepaged.c | 8 +++----- mm/madvise.c | 6 +++--- mm/memory.c | 2 +- mm/mempolicy.c | 8 ++++---- mm/migrate.c | 7 +++---- mm/mprotect.c | 2 +- mm/rmap.c | 2 +- 10 files changed, 41 insertions(+), 43 deletions(-)
fs/proc/task_mmu.c renamed from fs/proc/task_mmu.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +2 -2
@@ -1023,7 +1023,7 @@ static int smaps_hugetlb_range(pte_t *ptif (folio) {/* We treat non-present entries as "maybe shared". */ -if (!present ||folio_likely_mapped_shared (folio) || +if (!present ||folio_maybe_mapped_shared (folio) ||hugetlb_pmd_shared (pte)) mss->shared_hugetlb +=huge_page_size (hstate_vma (vma));else
@@ -1882,7 +1882,7 @@ static int pagemap_hugetlb_range(pte_t *if (!folio_test_anon (folio)) flags |=PM_FILE ; -if (!folio_likely_mapped_shared (folio) && +if (!folio_maybe_mapped_shared (folio) && !hugetlb_pmd_shared (ptep)) flags |=PM_MMAP_EXCLUSIVE ;
include/linux/mm.h renamed from include/linux/mm.h~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +22 -21
76 lines not on this page · raw message
mm/huge_memory.c renamed from mm/huge_memory.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +1 -1
@@ -2155,7 +2155,7 @@ bool madvise_free_huge_pmd(struct mmu_ga* If other processes are mapping this folio, we couldn't discard * the folio unless they all do MADV_FREE so let's skip the folio. */ -if (folio_likely_mapped_shared (folio)) +if (folio_maybe_mapped_shared (folio)) goto out;if (!folio_trylock (folio))
mm/khugepaged.c renamed from mm/khugepaged.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +3 -5
@@ -607,7 +607,7 @@ static int __collapse_huge_page_isolate(VM_BUG_ON_FOLIO (!folio_test_anon (folio), folio);/* See hpage_collapse_scan_pmd(). */ -if (folio_likely_mapped_shared (folio)) { +if (folio_maybe_mapped_shared (folio)) { ++shared;if (cc->is_khugepaged && shared > khugepaged_max_ptes_shared) {
@@ -1359,11 +1359,9 @@ static int hpage_collapse_scan_pmd(struc/* * We treat a single page as shared if any part of the THP -* is shared. "False negatives" from -* folio_likely_mapped_shared() are not expected to matter -* much in practice. +* is shared. */ -if (folio_likely_mapped_shared (folio)) { +if (folio_maybe_mapped_shared (folio)) { ++shared;if (cc->is_khugepaged && shared > khugepaged_max_ptes_shared) {
mm/madvise.c renamed from mm/madvise.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +3 -3
@@ -387,7 +387,7 @@ static int madvise_cold_or_pageout_pte_r folio =pmd_folio (orig_pmd);/* Do not interfere with other mappings of this folio */ -if (folio_likely_mapped_shared (folio)) +if (folio_maybe_mapped_shared (folio)) goto huge_unlock;if (pageout_anon_only_filter && !folio_test_anon (folio))
@@ -486,7 +486,7 @@ restart:if (nr <folio_nr_pages (folio)) {int err; -if (folio_likely_mapped_shared (folio)) +if (folio_maybe_mapped_shared (folio))continue ;if (pageout_anon_only_filter && !folio_test_anon (folio))continue ;
@@ -721,7 +721,7 @@ static int madvise_free_pte_range(pmd_tif (nr <folio_nr_pages (folio)) {int err; -if (folio_likely_mapped_shared (folio)) +if (folio_maybe_mapped_shared (folio))continue ;if (!folio_trylock (folio))continue ;
mm/memory.c renamed from mm/memory.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +1 -1
@@ -5698,7 +5698,7 @@ int numa_migrate_check(struct folio *fol* Flag if the folio is shared between multiple address spaces. This * is later used when determining whether to group tasks together */ -if (folio_likely_mapped_shared (folio) && (vma->vm_flags &VM_SHARED )) +if (folio_maybe_mapped_shared (folio) && (vma->vm_flags &VM_SHARED )) *flags |=TNF_SHARED ;/* * For memory tiering mode, cpupid of slow memory page is used
mm/mempolicy.c renamed from mm/mempolicy.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +4 -4
@@ -642,11 +642,11 @@ static int queue_folios_hugetlb(pte_t *p* Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. * Choosing not to migrate a shared folio is not counted as a failure. * -* See folio_likely_mapped_shared() on possible imprecision when we +* See folio_maybe_mapped_shared() on possible imprecision when we * cannot easily detect if a folio is shared. */ if ((flags &MPOL_MF_MOVE_ALL ) || - (!folio_likely_mapped_shared (folio) && !hugetlb_pmd_shared (pte))) + (!folio_maybe_mapped_shared (folio) && !hugetlb_pmd_shared (pte)))if (!folio_isolate_hugetlb (folio, qp->pagelist)) qp->nr_failed++; unlock:
@@ -1033,10 +1033,10 @@ static bool migrate_folio_add(struct fol* Unless MPOL_MF_MOVE_ALL, we try to avoid migrating a shared folio. * Choosing not to migrate a shared folio is not counted as a failure. * -* See folio_likely_mapped_shared() on possible imprecision when we +* See folio_maybe_mapped_shared() on possible imprecision when we * cannot easily detect if a folio is shared. */ -if ((flags &MPOL_MF_MOVE_ALL ) || !folio_likely_mapped_shared (folio)) { +if ((flags &MPOL_MF_MOVE_ALL ) || !folio_maybe_mapped_shared (folio)) {if (folio_isolate_lru (folio)) {list_add_tail (&folio->lru, foliolist); node_stat_mod_folio(folio,
mm/migrate.c renamed from mm/migrate.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +3 -4
@@ -2226,7 +2226,7 @@ static int __add_folio_for_migration(strif (folio_nid (folio) == node)return 0 ; -if (folio_likely_mapped_shared (folio) && !migrate_all) +if (folio_maybe_mapped_shared (folio) && !migrate_all)return -EACCES ;if (folio_test_hugetlb (folio)) {
@@ -2651,11 +2651,10 @@ int migrate_misplaced_folio_prepare(stru* processes with execute permissions as they are probably * shared libraries. * -* See folio_likely_mapped_shared() on possible imprecision +* See folio_maybe_mapped_shared() on possible imprecision * when we cannot easily detect if a folio is shared. */ -if ((vma->vm_flags &VM_EXEC ) && -folio_likely_mapped_shared (folio)) +if ((vma->vm_flags &VM_EXEC ) &&folio_maybe_mapped_shared (folio))return -EACCES ;/*
mm/mprotect.c renamed from mm/mprotect.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +1 -1
@@ -133,7 +133,7 @@ static long change_pte_range(struct mmu_/* Also skip shared copy-on-write pages */ if (is_cow_mapping (vma->vm_flags) && (folio_maybe_dma_pinned (folio) || -folio_likely_mapped_shared (folio))) +folio_maybe_mapped_shared (folio)))continue ;/*
mm/rmap.c renamed from mm/rmap.c~mm-convert-folio_likely_mapped_shared-to-folio_maybe_mapped_shared +1 -1
@@ -889,7 +889,7 @@ static bool folio_referenced_one(structif ((!atomic_read (&vma->vm_mm->mm_users) ||check_stable_address_space (vma->vm_mm)) &&folio_test_anon (folio) &&folio_test_swapbacked (folio) && - !folio_likely_mapped_shared (folio)) { + !folio_maybe_mapped_shared (folio)) { pra->referenced =-1 ;page_vma_mapped_walk_done (&pvmw);return false;
_
Patches currently in -mm which might be from [email protected] are