mm: PMD-level swap entries for anonymous THPs
Subsystems (23)
Usama Arif
reviewerv6 cover mm: PMD-level swap entries for anonymous THPs
When reclaim swaps out a PMD-mapped anonymous THP today, the PMD is split into HPAGE_PMD_NR PTE-level swap entries via TTU_SPLIT_HUGE_PMD before unmap. This series introduces a PMD-level swap entry so the huge mapping can survive the swap round-trip and do_huge_pmd_swap_page() can restore the PMD mapping directly on swap-in, without waiting for khugepaged to collapse the range later.
The PMD swap entry is a compact page-table encoding for HPAGE_PMD_NR consecutive swap slots. swap_map accounting remains per-slot and is unchanged. Importantly, a PMD swap entry does not promise that the swap cache always contains one PMD-sized folio. While the cache is empty or contains one PMD-sized folio, PMD-level handling can proceed. Once the cache has split/per-slot state, users either inspect the individual slots directly (mincore) or split the PMD swap entry and retry through the PTE path (fault, swapoff, MADV_WILLNEED, UFFDIO_MOVE). Likewise, if any slot is still backed by zswap's per-page store, PMD-order swap-in consumers split and let the PTE path load the range page by page; an all-on-disk range can still be read back as one PMD-sized folio.
The series is ordered so every consumer can handle PMD swap entries before the swap-out producer starts installing them. The swap-out patch is the last functional change.
Notes on zswap:
Native PMD-order zswap load/store is intentionally left for a follow-up. Alexandre Ghiti is currently working this.
This series can still preserve PMD swap entries while zswap is enabled:
zswap stores the THP as order-0 entries, and PMD-order swap-in consumers split any range that has zswap entries before reading it. If zswap has written the whole range back to disk, or the swap cache still contains one PMD-sized folio, PMD-level handling can proceed.
Testing:
The 16 pmd_swap selftests pass on x86_64 with zswap both disabled and enabled. PMD_SWAP_DEVICE was set, so the swapoff test ran in both configurations.
v5 -> v6: https://lore.kernel.org/all/[email protected]/
- Add patch 1 to rename pmd_to_softleaf_folio() to pmd_softleaf_to_folio(). No functional change. (Dev Jain)
- Patch 2: warn when pmd_softleaf_to_folio() is given a non-PFN softleaf rather than silently returning NULL. (Dev Jain)
- Patch 4: bound the fork extend-table fallback to one retry, re-read the PMD under its lock, normalize unrecoverable copy_huge_pmd() errors to -ENOMEM so copy_pmd_range() cannot clear and leak the source swap PMD, and drop a redundant thp_migration_supported() gate.
- Patch 5: check multi-page swap-cache insertions for zswap-backed slots in __swap_cache_add_check() under the cluster lock, both before allocation and before insertion, and reject mixed zswap/disk state with -EBUSY. (Yosry Ahmed, Nhat Pham)
- Patch 6: on a failed non-uptodate PMD-order read, remove the large folio from swap cache before splitting so order-0
fallback retries individual slots rather than poisoning the whole
2 MiB range; retain hardware-poisoned folios for per-subpage handling.
- Patch 7: make HMM snapshot mode report a PMD swap entry as non-resident, matching PTE swap entries, rather than HMM_PFN_ERROR. Drop redundant thp_migration_supported() gates and simplify non-present PMD handling.
- Patch 8: factor PMD MADV_WILLNEED prefetch into swapin_pmd_swap_entry(), split and retry through PTEs after any PMD-order swapin failure, and replace the racy folio_test_locked() plus folio_lock() sequence with folio_trylock().
- Patch 9: guard PMD-swap UFFDIO_MOVE code with CONFIG_THP_SWAP, clarify RWP marker propagation, and reject a PMD swap entry at the destination with -EEXIST so UFFDIO_MOVE cannot loop forever on -EAGAIN.
- Patch 10: honor current THP/VMA policy before PMD-order swap-in, recheck that the PMD is still the original swap entry before splitting for PTE fallback, and provide the CONFIG_TRANSPARENT_HUGEPAGE wp_huge_pmd() declaration/stub needed by THP=n builds.
- Patch 11: make an invalid set_pmd_swap_entry() walk context warn and return -EINVAL instead of falsely reporting success and corrupting the MM_ANONPAGES/MM_SWAPENTS accounting, and add an exact PMD-size folio precondition check. (Luiz Capitulino)
- Patch 12: use /proc/swaps for prerequisite detection, check MADV_HUGEPAGE, and distinguish an environment that cannot allocate a PMD THP (SKIP) from a swap-out validation failure (FAIL). Add partial-mprotect and partial-munmap split coverage. Strengthen munmap/MADV_FREE VmSwap accounting, pagemap slot-offset checks, and mprotect/mremap swapped-state checks; force mremap to move, check munmap()'s return, and mark the UFFDIO_MOVE destination MADV_HUGEPAGE before asserting PMD restoration. Move common setup and cleanup into one fixture, merge the swapoff fixture, remove the redundant cycles test, and make the data pattern differ between base pages so the split tests can detect incorrect slot ordering. Order fork-COW so the parent writes while the child still holds the untouched shared swap entry. (Luiz Capitulino)
- Clarify commit messages throughout. Retain TTU_SPLIT_HUGE_PMD after prototyping its removal: removing it here requires an extra rmap walk and broadens the series beyond PMD swap entries. (Matthew Wilcox)
- Rebase onto akpm/mm-new from 15 August (4b65683fd25f).
v4 -> v5: https://lore.kernel.org/all/[email protected]/
- Commit message improvements for almost all patches (Yosry for zswap patch)
- Patch 1: make pmd_to_softleaf_folio() reject softleaf entries that do not encode a PFN, so a PMD swap offset is never interpreted as one. PMD swap entries remain valid softleaf entries for classification. (sashiko)
- Patch 2: use the existing pmd_swp_uffd() helper and force freeze=false for PMD swap entries, which have no struct page for the migration-entry freeze path. (sashiko)
- Patch 3: document that the caller's page-table or swap-cache reference pins every source slot while a partial PMD-sized duplication is rolled back. Keep the pre-existing PTE fork retry behavior outside this series. (sashiko)
- Patch 5: split to the PTE path rather than mapping a PMD-sized folio containing a hardware-poisoned subpage, and restore PAGE_NONE when swapoff restores a UFFD marker in an RWP VMA. (sashiko)
- Patch 6: account SwapPss for a PMD swap entry one slot at a time because the slots can have different swap reference counts.
- Patch 7: if PMD-order MADV_WILLNEED encounters newly populated per-page zswap state, revalidate and remove the failed clean PMD-sized cache folio before retrying through PTEs.
- Patch 8: mark a moved PMD swap entry for UFFD when the UFFDIO_MOVE destination VMA is RWP-registered. (sashiko)
- Patch 9: restore PAGE_NONE for UFFD RWP swap-in, preserve the original write-fault state through swap-slot release and COW handling, remove the unnecessary LRU drain, and prevent PTE batching from mapping a poisoned subpage. (sashiko)
- Patch 10: add and document thp_swpout_pmd, which counts PMD mappings replaced by PMD-level swap entries rather than swapped folios.
- Patch 11: register pmd_swap with the default mm selftest runner, preserve errno across UFFDIO_MOVE cleanup, check swapoff residency before the first memory access, add a parent-side write and verification to the fork+COW test, and add RWP regression coverage for swap-in, UFFDIO_MOVE, and swapoff. (sashiko)
- Keep do_huge_pmd_swap_page() in patch 9. Patches 6 and 7 only add consumers; patch 10 remains the first producer, so no PMD swap entry can reach those paths before the fault handler is present. (sashiko)
- Rebase onto latest akpm/mm-new from 22 July (5e0603ba185a)
v3 -> v4: https://lore.kernel.org/all/[email protected]/
- Patch 1: guard the new arch-specific pmd_swp_mkexclusive / pmd_swp_exclusive / pmd_swp_clear_exclusive helpers on arm64, loongarch, powerpc, riscv, s390, and x86 with CONFIG_ARCH_HAS_PMD_SOFTLEAVES, matching the pattern already
used for pmd_swp_soft_dirty. Also fixes the redefinition-vs- generic-fallback build errors kernel test robot reported on i386-allnoconfig-bpf and riscv-allnoconfig-bpf, and rewraps
the patch 1 commit message paragraphs to ~75 columns.
(sashiko, kernel test robot, Usama Arif)
- Patch 2: switch the trailing folio_remove_rmap_pmd() gate in __split_huge_pmd_locked() from *pmd to old_pmd, old_pmd retains
the original present-or-non-present classification for every
branch above. (sashiko)
- Patch 3: teach swap_retry_table_alloc() (and the underlying swap_extend_table_alloc()) to accept an nr parameter and scan
every slot in [ci_off, ci_off + nr) before committing an extend-table allocation. (sashiko)
- Patch 4: rename zswap_range_has_entry() to zswap_is_present() so
the same helper serves both single-slot (nr=1) and range queries, and switch the implementation from XA_STATE + xas_find() to xa_find(), which handles RCU locking and internal-retry markers itself. Rename the callers in patches 5, 7, 9. (Yosry)
- Patch 9: refuse to map a swap-cache folio in do_huge_pmd_swap_page() when folio_contain_hwpoisoned_page() reports a poisoned subpage; split the PMD swap entry so do_swap_page() can return VM_FAULT_HWPOISON per subpage instead of the PMD handler mapping
the corrupted memory as one THP. Mirrors the PageHWPoison check
the PTE swap-in path already performs. (sashiko)
- Patch 9: note explicitly in the commit message that PMD-order swap-in deliberately skips the order-0 readahead paths, order-0 readahead would populate per-page swap-cache state and force the
PMD swap entry to split before the fault could finish. (Kairui)
- Patch 10: move mm_prepare_for_swap_entries() into set_pmd_swap_entry() between folio_dup_swap() and set_pmd_at()
so this mm is on init_mm.mmlist before any swap PMD referencing slots with a non-zero swap_map becomes visible. Matches the PTE swap-out ordering. (sashiko)
- rebase onto latest akpm/mm-new (61cccb8363fcc282d4ae0555b8739dd227f5ad0b)
v2 -> v3: https://lore.kernel.org/all/[email protected]/
- Clarified the PMD swap entry rule: it is a compact encoding for HPAGE_PMD_NR swap slots, not a guarantee that swap cache always has one PMD-sized folio. (Lance Yang)
- Swapoff, fault, MADV_WILLNEED, and UFFDIO_MOVE now classify the whole PMD swap-cache range and split/retry through the PTE path for split/per-slot cache state. (Lance Yang)
- mincore handles PMD swap entries without assuming one lookup covers a split swap-cache range. (Lance Yang)
- UFFDIO_MOVE rechecks all HPAGE_PMD_NR slots before moving an empty PMD swap-cache range, avoiding stale rmap metadata for per-slot cached folios.
- Added a standalone zswap prerequisite patch from Alexandre that distinguishes all-on-disk large-folio ranges from ranges with per-page zswap entries.
- Replaced the global zswap-ever-enabled policy with per-range zswap
checks: PMD swap entries can still be installed while zswap is enabled, and PMD-order swap-in consumers split when the range has per-page zswap state.
- Added a mincore selftest and updated MADV_WILLNEED coverage so the test checks that the PMD swap entry remains in place until first touch. Total pmd_swap coverage is now 14 tests.
v1 -> v2: https://lore.kernel.org/all/[email protected]/
- Patch 1: convert two additional softleaf_to_pmd() callers that landed in mm-unstable since v1 (mm/debug_vm_pgtable.c, mm/migrate_device.c) (Dev)
- Patch 2: rename helper ensure_on_mmlist() to mm_prepare_for_swap_entries() to better describe its purpose
(David)
- Patch 3: drop VM_WARN_ON_ONCE(!pmd_is_migration_entry) as
Dev posted it as a separate patch.
- Patch 5 (new): move softleaf_to_folio() inside the device-private branch in migrate_vma_collect_pmd(); same class of fix as patch 4 but for the migrate-device PMD walker.
- Patch 6 (new): rename CONFIG_ARCH_ENABLE_THP_MIGRATION to CONFIG_ARCH_HAS_PMD_SOFTLEAVES so the gate that now drives swap-entry support too is named for what it actually controls
(PMD softleaf entries), not just migration. (Dev)
- Patch 7: add the missing pmd_swp_exclusive / mkexclusive / clear_exclusive helpers for powerpc.
- Patches 10 and 14: use upstream swapin_sync() (bundles swap_cache_alloc_folio + swap_read_folio + the -EEXIST race
retry) instead of the bespoke swapin_alloc_pmd_folio() helper
from v1; do_swap_page and shmem_swapin_folio use the same
helper (Kairui)
- Patch 10: construct a stack vm_fault for the swapoff swap-in so
the allocator can resolve a mempolicy, mirroring how the PTE
swapoff path (unuse_pte_range) already does it.
- Patch 11: extend coverage to check_pmd_state() in khugepaged so a swapped-out PMD-mapped THP is treated as SCAN_PMD_MAPPED (matches the existing migration-entry handling). Route PMD swap entries in the pmd_trans_huge_lock() branch of mincore_pte_range() through mincore_pmd_swap() so a swapped-out PMD-mapped THP isn't reported as resident.
- Patch 12 (new): handle PMD swap entries in MADV_WILLNEED via swapin_sync(BIT(HPAGE_PMD_ORDER)); a naive order-0 read-ahead
would force the subsequent fault to split.
- Patch 13: refuse UFFDIO_MOVE with -EBUSY if the swap-cache folio
was split between swap-out and the move, matching
move_pages_pte()'s rejection of large folios; otherwise only one
of the 512 anon-rmaps would be re-anchored to dst_vma.
- Patch 16: alloc_fill_swap_thp() now uses the existing mmap_pmd_aligned() helper so tests don't flake/skip based on VA placement; new MADV_WILLNEED test that watches the PMD-order
mTHP swpin counter; swapoff test restructured to use the kselftest_harness ASSERT cleanup blocks (no double swapoff, no verify-after-munmap).
- Collected Acks and Reviews.
[1] https://lore.kernel.org/all/[email protected]/
Alexandre Ghiti (1):
mm: zswap: add range lookup for large-folio swapin
Usama Arif (11):
mm: rename pmd_to_softleaf_folio() to pmd_softleaf_to_folio()
mm: add PMD swap entry detection support
mm: add PMD swap entry splitting support
mm: handle PMD swap entries in fork path
mm: swap in PMD swap entries as whole THPs during swapoff
mm: handle PMD swap entries in non-present PMD walkers
mm: handle PMD swap entries in MADV_WILLNEED
mm: handle PMD swap entries in UFFDIO_MOVE
mm: handle PMD swap entry faults on swap-in
mm: install PMD swap entries on swap-out selftests/mm: add PMD swap entry tests
Documentation/admin-guide/mm/transhuge.rst | 5 + arch/arm64/include/asm/pgtable.h | 6 + arch/loongarch/include/asm/pgtable.h | 19 + arch/powerpc/include/asm/book3s/64/pgtable.h | 17 + arch/riscv/include/asm/pgtable.h | 15 + arch/s390/include/asm/pgtable.h | 17 + arch/x86/include/asm/pgtable.h | 17 + fs/proc/task_mmu.c | 46 +- include/linux/huge_mm.h | 16 + include/linux/leafops.h | 30 +- include/linux/pgtable.h | 17 + include/linux/swap.h | 4 +- include/linux/vm_event_item.h | 1 + include/linux/zswap.h | 6 + mm/hmm.c | 11 +- mm/huge_memory.c | 607 ++++++++++++++- mm/internal.h | 42 ++ mm/khugepaged.c | 6 + mm/madvise.c | 120 ++- mm/memory.c | 47 +- mm/mincore.c | 45 +- mm/rmap.c | 19 + mm/swap.h | 22 +- mm/swap_state.c | 54 ++ mm/swapfile.c | 209 +++++- mm/userfaultfd.c | 14 + mm/vmscan.c | 9 +- mm/vmstat.c | 1 + mm/zswap.c | 46 +- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/ksft_pmd_swap.sh | 4 + tools/testing/selftests/mm/pmd_swap.c | 742 +++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 33 files changed, 2104 insertions(+), 116 deletions(-) create mode 100755 tools/testing/selftests/mm/ksft_pmd_swap.sh create mode 100644 tools/testing/selftests/mm/pmd_swap.c
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 1/12 mm: rename pmd_to_softleaf_folio() to pmd_softleaf_to_folio()
pmd_to_softleaf_folio() converts the softleaf entry encoded by a PMD to a folio. Rename it to pmd_softleaf_to_folio() to make the conversion direction explicit and align it with softleaf_to_folio().
No functional change.
--- include/linux/leafops.h | 4 ++-- mm/huge_memory.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/leafops.h +2 -2
@@ -657,7 +657,7 @@ static inline bool pmd_is_valid_softleaf(pmd_t pmd) }/** -* pmd_to_softleaf_folio() - Convert the PMD entry to a folio. +* pmd_softleaf_to_folio() - Convert the PMD softleaf entry to a folio. * @pmd: PMD entry. * * The PMD entry is expected to be a valid PMD softleaf entry.
@@ -665,7 +665,7 @@ static inline bool pmd_is_valid_softleaf(pmd_t pmd)* Returns: the folio the softleaf entry references if this is a valid softleaf * entry, otherwise NULL. */ -static inline struct folio *pmd_to_softleaf_folio (pmd_t pmd) +static inline struct folio *pmd_softleaf_to_folio (pmd_t pmd) {const softleaf_t entry =softleaf_from_pmd (pmd);
mm/huge_memory.c +1 -1
@@ -2467,7 +2467,7 @@ static struct folio *normal_or_softleaf_folio_pmd(struct vm_area_struct *vma,if (!thp_migration_supported ())WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); -return pmd_to_softleaf_folio (pmdval); +return pmd_softleaf_to_folio (pmdval); } static bool has_deposited_pgtable(struct vm_area_struct *vma, pmd_t pmdval,
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 2/12 mm: add PMD swap entry detection support
In reply to Usama ArifCurrently when a PMD-mapped THP is swapped out, the PMD is always split into HPAGE_PMD_NR PTE-level swap entries. To preserve huge page information across swap cycles, later patches will install a single PMD-level swap entry instead. Add the infrastructure to detect those entries.
Teach the softleaf layer to recognise PMD swap entries:pmd_is_swap_entry() detects them and softleaf_is_valid_pmd_entry() accepts them as a valid non-present type. Because swap entries do not encode a PFN, make pmd_softleaf_to_folio() warn and return NULL for them instead of passing the swap offset to softleaf_to_folio(). Clear the exclusive overlay bit in softleaf_from_pmd() before decoding, matching how soft_dirty and uffd bits are already stripped.
Add pmd_swp_mkexclusive(), pmd_swp_exclusive(), and pmd_swp_clear_exclusive() helpers to each architecture that supports PMD softleaf entries (x86, arm64, s390, riscv, loongarch, powerpc), mirroring the existing PTE swap exclusive helpers in each arch's pgtable.h. Provide generic no-op PMD swap exclusive fallbacks for architectures without PMD softleaf support, matching the generic PMD swap soft-dirty fallbacks.
--- arch/arm64/include/asm/pgtable.h | 6 +++++ arch/loongarch/include/asm/pgtable.h | 19 ++++++++++++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 17 +++++++++++++ arch/riscv/include/asm/pgtable.h | 15 +++++++++++ arch/s390/include/asm/pgtable.h | 17 +++++++++++++ arch/x86/include/asm/pgtable.h | 17 +++++++++++++ include/linux/leafops.h | 26 ++++++++++++++++---- include/linux/pgtable.h | 17 +++++++++++++ 8 files changed, 129 insertions(+), 5 deletions(-)
arch/arm64/include/asm/pgtable.h +6 -0
@@ -598,6 +598,12 @@ static inline int pmd_protnone(pmd_t pmd)#define pmd_swp_clear_uffd (pmd) \ pte_pmd(pte_swp_clear_uffd(pmd_pte(pmd)))#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ +#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES +#define pmd_swp_exclusive (pmd) pte_swp_exclusive(pmd_pte(pmd)) +#define pmd_swp_mkexclusive (pmd) pte_pmd(pte_swp_mkexclusive(pmd_pte(pmd))) +#define pmd_swp_clear_exclusive (pmd) \ + pte_pmd(pte_swp_clear_exclusive(pmd_pte(pmd))) +#endif #define pmd_write (pmd) pte_write(pmd_pte(pmd))
arch/loongarch/include/asm/pgtable.h +19 -0
@@ -357,6 +357,25 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return pte; } +#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES +static inline pmd_t pmd_swp_mkexclusive (pmd_t pmd) +{ +pmd_val (pmd) |= _PAGE_SWP_EXCLUSIVE; +return pmd; +} + +static inline bool pmd_swp_exclusive (pmd_t pmd) +{ +return pmd_val (pmd) & _PAGE_SWP_EXCLUSIVE; +} + +static inline pmd_t pmd_swp_clear_exclusive (pmd_t pmd) +{ +pmd_val (pmd) &= ~_PAGE_SWP_EXCLUSIVE; +return pmd; +} +#endif +#define pte_none (pte) (!(pte_val(pte) & ~_PAGE_GLOBAL))#define pte_present (pte) (pte_val(pte) & (_PAGE_PRESENT | _PAGE_PROTNONE))#define pte_no_exec (pte) (pte_val(pte) & _PAGE_NO_EXEC)
arch/powerpc/include/asm/book3s/64/pgtable.h +17 -0
@@ -699,6 +699,23 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return __pte_raw (pte_raw (pte) &cpu_to_be64 (~_PAGE_SWP_EXCLUSIVE)); } +#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES +static inline pmd_t pmd_swp_mkexclusive (pmd_t pmd) +{ +return __pmd_raw (pmd_raw (pmd) |cpu_to_be64 (_PAGE_SWP_EXCLUSIVE)); +} + +static inline bool pmd_swp_exclusive (pmd_t pmd) +{ +return !!(pmd_raw (pmd) &cpu_to_be64 (_PAGE_SWP_EXCLUSIVE)); +} + +static inline pmd_t pmd_swp_clear_exclusive (pmd_t pmd) +{ +return __pmd_raw (pmd_raw (pmd) &cpu_to_be64 (~_PAGE_SWP_EXCLUSIVE)); +} +#endif +static inline bool check_pte_access (unsigned long access,unsigned long ptev) {/*
arch/riscv/include/asm/pgtable.h +15 -0
@@ -1213,6 +1213,21 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte) }#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES +static inline bool pmd_swp_exclusive (pmd_t pmd) +{ +return pte_swp_exclusive (pmd_pte (pmd)); +} + +static inline pmd_t pmd_swp_mkexclusive (pmd_t pmd) +{ +return pte_pmd (pte_swp_mkexclusive (pmd_pte (pmd))); +} + +static inline pmd_t pmd_swp_clear_exclusive (pmd_t pmd) +{ +return pte_pmd (pte_swp_clear_exclusive (pmd_pte (pmd))); +} +#define __pmd_to_swp_entry (pmd) ((swp_entry_t) { pmd_val(pmd) })#define __swp_entry_to_pmd (swp) __pmd((swp).val)#endif /* CONFIG_ARCH_HAS_PMD_SOFTLEAVES */
arch/s390/include/asm/pgtable.h +17 -0
@@ -870,6 +870,23 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return clear_pte_bit (pte,__pgprot (_PAGE_SWP_EXCLUSIVE)); } +#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES +static inline pmd_t pmd_swp_mkexclusive (pmd_t pmd) +{ +return set_pmd_bit (pmd,__pgprot (_PAGE_SWP_EXCLUSIVE)); +} + +static inline bool pmd_swp_exclusive (pmd_t pmd) +{ +return pmd_val (pmd) & _PAGE_SWP_EXCLUSIVE; +} + +static inline pmd_t pmd_swp_clear_exclusive (pmd_t pmd) +{ +return clear_pmd_bit (pmd,__pgprot (_PAGE_SWP_EXCLUSIVE)); +} +#endif +static inline int pte_soft_dirty (pte_t pte) {return pte_val (pte) & _PAGE_SOFT_DIRTY;
arch/x86/include/asm/pgtable.h +17 -0
@@ -1525,6 +1525,23 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return pte_clear_flags (pte, _PAGE_SWP_EXCLUSIVE); } +#ifdef CONFIG_ARCH_HAS_PMD_SOFTLEAVES +static inline pmd_t pmd_swp_mkexclusive (pmd_t pmd) +{ +return pmd_set_flags (pmd, _PAGE_SWP_EXCLUSIVE); +} + +static inline int pmd_swp_exclusive (pmd_t pmd) +{ +return pmd_flags (pmd) & _PAGE_SWP_EXCLUSIVE; +} + +static inline pmd_t pmd_swp_clear_exclusive (pmd_t pmd) +{ +return pmd_clear_flags (pmd, _PAGE_SWP_EXCLUSIVE); +} +#endif +#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY static inline pte_t pte_swp_mksoft_dirty (pte_t pte) {
include/linux/leafops.h +21 -5
@@ -102,6 +102,8 @@ static inline softleaf_t softleaf_from_pmd(pmd_t pmd) pmd =pmd_swp_clear_soft_dirty (pmd);if (pmd_swp_uffd (pmd)) pmd =pmd_swp_clear_uffd (pmd); +if (pmd_swp_exclusive (pmd)) + pmd =pmd_swp_clear_exclusive (pmd); arch_entry =__pmd_to_swp_entry (pmd);/* Temporary until swp_entry_t eliminated. */
@@ -634,18 +636,30 @@ static inline bool pmd_is_migration_entry(pmd_t pmd)*/ static inline bool softleaf_is_valid_pmd_entry (softleaf_t entry) { -/* Only device private, migration entries valid for PMD. */ +/* Device private, migration, and swap entries valid for PMD. */ return softleaf_is_device_private (entry) || -softleaf_is_migration (entry); +softleaf_is_migration (entry) || +softleaf_is_swap (entry); +} + +/** +* pmd_is_swap_entry() - Does this PMD entry encode an actual swap entry? +* @pmd: PMD entry. +* +* Returns: true if the PMD encodes a swap entry, otherwise false. +*/ +static inline bool pmd_is_swap_entry (pmd_t pmd) +{ +return softleaf_is_swap (softleaf_from_pmd (pmd)); }/** * pmd_is_valid_softleaf() - Is this PMD entry a valid softleaf entry? * @pmd: PMD entry. * -* PMD leaf entries are valid only if they are device private or migration -* entries. This function asserts that a PMD leaf entry is valid in this -* respect. +* PMD leaf entries are valid only if they are device private, migration, +* or swap entries. This function asserts that a PMD leaf entry is valid +* in this respect. * * Returns: true if the PMD entry is a valid leaf entry, otherwise false. */
@@ -673,6 +687,8 @@ static inline struct folio *pmd_softleaf_to_folio(pmd_t pmd)VM_WARN_ON_ONCE (true);return NULL ; } +if (WARN_ON_ONCE (!softleaf_has_pfn (entry))) +return NULL ;return softleaf_to_folio (entry); }
include/linux/pgtable.h +17 -0
@@ -1917,6 +1917,23 @@ static inline pmd_t pmd_swp_clear_soft_dirty(pmd_t pmd) }#endif +#ifndef CONFIG_ARCH_HAS_PMD_SOFTLEAVES +static inline pmd_t pmd_swp_mkexclusive (pmd_t pmd) +{ +return pmd; +} + +static inline bool pmd_swp_exclusive (pmd_t pmd) +{ +return false; +} + +static inline pmd_t pmd_swp_clear_exclusive (pmd_t pmd) +{ +return pmd; +} +#endif +#ifndef __HAVE_PFNMAP_TRACKING/* * Interfaces that can be used by architecture code to keep track of
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 3/12 mm: add PMD swap entry splitting support
In reply to Usama ArifAdd a swap branch in __split_huge_pmd_locked() that splits a PMD swap entry into 512 PTE swap entries. No folio reference is needed because swap entries point to swap slots rather than pages. Each PTE inherits the correct sub-slot offset and preserves soft_dirty, uffd_wp, and exclusive flags.
The folio_remove_rmap_pmd() gate at the end must inspect old_pmd rather than *pmd: for a present THP split, *pmd has already been cleared by pmdp_invalidate(), and that invalidated bit pattern can decode as a plausible swap entry.
This branch is reached from the explicit __split_huge_pmd() callers that hit a non-present PMD: partial-range mprotect / munmap, the wp_huge_pmd() PMD-COW fallback, and the swap-in / swapoff fallbacks added in later patches when the cached folio is no longer PMD-sized. page_vma_mapped_walk() does not iterate PMD swap entries, so try_to_unmap_one() and try_to_migrate_one() do not reach this branch and freeze=true cannot occur in this branch today. page and folio are therefore left uninitialized in the swap branch; a VM_WARN_ON_ONCE(freeze) catches any future caller that breaks this invariant before the freeze path dereferences page_to_pfn(page + i) or put_page(page).
--- mm/huge_memory.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
mm/huge_memory.c +28 -1
@@ -3252,6 +3252,14 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , vma, haddr, rmap_flags); } + }else if (pmd_is_swap_entry (*pmd)) { +VM_WARN_ON_ONCE (freeze); +/* Swap entries have no page for the migration freeze path. */ + freeze = false; + old_pmd = *pmd; + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd (old_pmd); + anon_exclusive =pmd_swp_exclusive (old_pmd); }else {/* * Up to this point the pmd is present and huge and userland has
@@ -3388,6 +3396,25 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,VM_WARN_ON (!pte_none (ptep_get (pte + i)));set_pte_at (mm, addr, pte + i, entry); } + }else if (pmd_is_swap_entry (old_pmd)) { +softleaf_t sl_entry =softleaf_from_pmd (old_pmd); +pte_t swp_pte; +swp_entry_t sub_entry; + +for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; + i++, addr +=PAGE_SIZE ) { + sub_entry =swp_entry (swp_type (sl_entry), +swp_offset (sl_entry) + i); + swp_pte =swp_entry_to_pte (sub_entry); +if (soft_dirty) + swp_pte =pte_swp_mksoft_dirty (swp_pte); +if (uffd_wp) + swp_pte =pte_swp_mkuffd (swp_pte); +if (anon_exclusive) + swp_pte =pte_swp_mkexclusive (swp_pte); +VM_WARN_ON (!pte_none (ptep_get (pte + i))); +set_pte_at (mm, addr, pte + i, swp_pte); + } }else {pte_t entry;
@@ -3415,7 +3442,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, }pte_unmap (pte); -if (!pmd_is_migration_entry (*pmd)) +if (!pmd_is_migration_entry (old_pmd) && !pmd_is_swap_entry (old_pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 4/12 mm: handle PMD swap entries in fork path
In reply to Usama ArifTeach copy_huge_pmd()/copy_huge_non_present_pmd() about swap entries, mirroring copy_nonpresent_pte().
swap_dup_entry_direct() gains a nr parameter (and is renamed to swap_dup_entries_direct()) so it can duplicate a contiguous range of swap slots in one call, matching the existing swap_put_entries_direct(entry, nr) API. Existing callers pass 1.
swap_retry_table_alloc() likewise gains a nr parameter so the outer retry knows how many slots the caller was trying to duplicate. The underlying swap_extend_table_alloc() now scans every slot in [ci_off, ci_off + nr) to confirm that at least one still needs the per-cluster extend table before committing an allocation.
copy_huge_non_present_pmd() "copies" PMD swap entries during fork instead of splitting, preserving the THP. This mirrors copy_nonpresent_pte() which duplicates the swap slot refcount, clears the exclusive bit on the source, and adds the destination mm to mmlist. If swap_dup_entries_direct() fails (GFP_ATOMIC table alloc), copy_huge_pmd() retries once after swap_retry_table_alloc(entry, HPAGE_PMD_NR, GFP_KERNEL).
--- include/linux/swap.h | 4 +-- mm/huge_memory.c | 60 ++++++++++++++++++++++++++++++++++++++------ mm/memory.c | 4 +-- mm/swap.h | 5 ++-- mm/swapfile.c | 40 ++++++++++++++++++----------- 5 files changed, 86 insertions(+), 27 deletions(-)
include/linux/swap.h +2 -2
@@ -395,7 +395,7 @@ sector_t swap_folio_sector(struct folio *folio);* All entries must be allocated by folio_alloc_swap(). And they must have * a swap count > 1. See comments of folio_*_swap helpers for more info. */ -int swap_dup_entry_direct (swp_entry_t entry); +int swap_dup_entries_direct (swp_entry_t entry,int nr);void swap_put_entries_direct (swp_entry_t entry,int nr);/*
@@ -439,7 +439,7 @@ static inline void free_swap_cache(struct folio *folio) { } -static inline int swap_dup_entry_direct (swp_entry_t ent) +static inline int swap_dup_entries_direct (swp_entry_t ent,int nr) {return 0 ; }
mm/huge_memory.c +53 -7
97 lines not on this page · raw message
mm/memory.c +2 -2
@@ -1016,7 +1016,7 @@ copy_nonpresent_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,struct page *page;if (likely (softleaf_is_swap (entry))) { -if (swap_dup_entry_direct (entry) <0 ) +if (swap_dup_entries_direct (entry,1 ) <0 )return -EIO ;mm_prepare_for_swap_entries (dst_mm);
@@ -1431,7 +1431,7 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,if (ret == -EIO ) {VM_WARN_ON_ONCE (!entry.val); -if (swap_retry_table_alloc (entry,GFP_KERNEL ) <0 ) { +if (swap_retry_table_alloc (entry,1 ,GFP_KERNEL ) <0 ) { ret = -ENOMEM ; goto out; }
mm/swap.h +3 -2
@@ -222,7 +222,7 @@ static inline void swap_cluster_unlock_irq(struct swap_cluster_info *ci)spin_unlock_irq (&ci->lock); } -extern int swap_retry_table_alloc (swp_entry_t entry,gfp_t gfp); +int swap_retry_table_alloc (swp_entry_t entry,unsigned int nr,gfp_t gfp);/* * Below are the core routines for doing swap for a folio.
@@ -428,7 +428,8 @@ static inline int swap_writeout(struct swap_io_ctx *ctx, struct folio *folio)return 0 ; } -static inline int swap_retry_table_alloc (swp_entry_t entry,gfp_t gfp) +static inline int swap_retry_table_alloc (swp_entry_t entry,unsigned int nr, +gfp_t gfp) {return -EINVAL ; }
mm/swapfile.c +26 -14
@@ -1462,9 +1462,11 @@ static bool swap_sync_discard(void)static int swap_extend_table_alloc (struct swap_info_struct *si,struct swap_cluster_info *ci, -unsigned int ci_off,gfp_t gfp) +unsigned int ci_off,unsigned int nr, +gfp_t gfp) {int count; +unsigned int i;void *table; table =kzalloc (sizeof (ci->extend_table[0 ]) *SWAPFILE_CLUSTER , gfp);
@@ -1480,15 +1482,21 @@ static int swap_extend_table_alloc(struct swap_info_struct *si,*/ if (!cluster_table_is_alloced (ci)) goto out_free; - count =swp_tb_get_count (__swap_table_get (ci, ci_off)); -if (count < (SWP_TB_COUNT_MAX -1 )) - goto out_free;if (ci->extend_table) goto out_free; - - ci->extend_table = table; -spin_unlock (&ci->lock); -return 0 ; +/* +* The caller may not know which slot in [ci_off, ci_off + nr) hit +* SWP_TB_COUNT_MAX - 1. Confirm at least one slot in the range still +* needs the extend table before committing the allocation. +*/ +for (i =0 ; i < nr; i++) { + count =swp_tb_get_count (__swap_table_get (ci, ci_off + i)); +if (count >= (SWP_TB_COUNT_MAX -1 )) { + ci->extend_table = table; +spin_unlock (&ci->lock); +return 0 ; + } + } out_free:spin_unlock (&ci->lock);
@@ -1496,7 +1504,7 @@ static int swap_extend_table_alloc(struct swap_info_struct *si,return 0 ; } -int swap_retry_table_alloc (swp_entry_t entry,gfp_t gfp) +int swap_retry_table_alloc (swp_entry_t entry,unsigned int nr,gfp_t gfp) {int ret;struct swap_info_struct *si;
@@ -1508,7 +1516,8 @@ int swap_retry_table_alloc(swp_entry_t entry, gfp_t gfp)return 0 ; ci =__swap_offset_to_cluster (si, offset); - ret =swap_extend_table_alloc (si, ci,swp_cluster_offset (entry), gfp); + ret =swap_extend_table_alloc (si, ci,swp_cluster_offset (entry), nr, + gfp);put_swap_device (si);return ret;
@@ -1709,7 +1718,8 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,if (unlikely (err)) {if (err == -ENOMEM ) {spin_unlock (&ci->lock); - err =swap_extend_table_alloc (si, ci, ci_off,GFP_ATOMIC ); + err =swap_extend_table_alloc (si, ci, ci_off,1 , +GFP_ATOMIC );spin_lock (&ci->lock);if (!err) goto restart;
@@ -1720,6 +1730,7 @@ static int swap_dup_entries_cluster(struct swap_info_struct *si,swap_cluster_unlock (ci);return 0 ; failed: +/* The caller's page-table or swap-cache reference pins every slot. */ while (ci_off-- > ci_start)__swap_cluster_put_entry (ci, ci_off);swap_extend_table_try_free (ci);
@@ -3925,8 +3936,9 @@ void si_swapinfo(struct sysinfo *val) }/* -* swap_dup_entry_direct() - Increase reference count of a swap entry by one. +* swap_dup_entries_direct() - Increase reference count of swap entries by one. * @entry: first swap entry from which we want to increase the refcount. +* @nr: number of contiguous swap entries to duplicate. * * Returns 0 for success, or -ENOMEM if the extend table is required * but could not be atomically allocated. Returns -EINVAL if the swap
@@ -3938,7 +3950,7 @@ void si_swapinfo(struct sysinfo *val)* Also the swap entry must have a count >= 1. Otherwise folio_dup_swap should * be used. */ -int swap_dup_entry_direct (swp_entry_t entry) +int swap_dup_entries_direct (swp_entry_t entry,int nr) {struct swap_info_struct *si;
@@ -3955,7 +3967,7 @@ int swap_dup_entry_direct(swp_entry_t entry)*/ VM_WARN_ON_ONCE (!swap_entry_swapped (si, entry)); -return swap_dup_entries_cluster (si,swp_offset (entry),1 ); +return swap_dup_entries_cluster (si,swp_offset (entry), nr); }#if defined(CONFIG_MEMCG ) && defined(CONFIG_BLK_CGROUP )
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 5/12 mm: zswap: add range lookup for large-folio swapin
In reply to Usama ArifFrom: Alexandre Ghiti <[email protected]>
A large folio reaches zswap_load() only when the caller expects the whole range to be on disk. Zswap still stores large folios as independent order-0 entries, so reconstructing a large folio from zswap entries would risk returning partially initialized data.
Teach zswap_load() to scan the covered range. If no slot is in zswap, return -ENOENT so swap_read_folio() reads the backing device. If any slot is still in zswap, fail the large-folio read so the caller can fall back to per-page swapin.
Return -EIO rather than -EINVAL for that conflict. Large-folio loads are now valid requests; the error means zswap cannot safely satisfy the request from partial per-page compressed state, not that the request is unsupported. Existing callers only distinguish -ENOENT, so this is a semantic clarification rather than a behavioral change.
Add zswap_is_present() so PMD swap-entry consumers can make the same range decision before attempting PMD-order swapin. Also use it from __swap_cache_add_check() for multi-page insertions while holding the swap cluster lock. That check runs before folio allocation and again immediately before swap-cache insertion, closing the race with zswap writeback and rejecting mixed zswap/disk backing with -EBUSY.
--- include/linux/zswap.h | 6 ++++++ mm/swap_state.c | 10 ++++++++++ mm/zswap.c | 46 +++++++++++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 13 deletions(-)
include/linux/zswap.h +6 -0
@@ -35,6 +35,7 @@ void zswap_lruvec_state_init(struct lruvec *lruvec);void zswap_folio_swapin (struct folio *folio);bool zswap_is_enabled (void );bool zswap_never_enabled (void ); +bool zswap_is_present (swp_entry_t entry,unsigned int nr);#else struct zswap_lruvec_state {};
@@ -69,6 +70,11 @@ static inline bool zswap_never_enabled(void)return true; } +static inline bool zswap_is_present (swp_entry_t entry,unsigned int nr) +{ +return false; +} +#endif #endif /* _LINUX_ZSWAP_H */
mm/swap_state.c +10 -0
@@ -12,6 +12,7 @@#include <linux/kernel_stat.h> #include <linux/mempolicy.h> #include <linux/swap.h> +#include <linux/zswap.h> #include <linux/leafops.h> #include <linux/init.h> #include <linux/pagemap.h>
@@ -191,6 +192,15 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,if (nr ==1 )return 0 ; +/* +* The cluster lock serializes swap-cache insertion with zswap +* writeback. Reject mixed zswap/disk backing before allocating a +* large folio and recheck it before adding the folio to swap cache. +*/ +if (zswap_is_present (swp_entry (swp_type (targ_entry), +round_down (swp_offset (targ_entry), nr)), nr)) +return -EBUSY ; + is_zero =__swap_table_test_zero (ci, ci_off); ci_off =round_down (ci_off, nr); ci_end = ci_off + nr;
mm/zswap.c +33 -13
65 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 6/12 mm: swap in PMD swap entries as whole THPs during swapoff
In reply to Usama ArifAdd swap_pmd_cache_lookup() to classify the swap cache behind a PMD swap entry as empty, backed by one PMD-sized folio, or requiring per-page handling because at least one covered slot has a smaller folio in the swap cache. PMD swap entries are handled at PMD granularity only while the covered cache range is empty or backed by a PMD-sized folio; a split cache forces the entry to be split and retried through the PTE path.
Add unuse_pmd() and call it from unuse_pmd_range() to swap in PMD-level swap entries as whole THPs during swapoff. This mirrors the existing unuse_pte_range() but operates at PMD granularity. Preserve soft-dirty, exclusive, and UFFD state when installing the present PMD. If an RWP VMA has a UFFD-marked swap entry, also restore PAGE_NONE so the first subsequent access still generates a userfault.
If the PMD-order folio cannot be allocated or read, the swap cache already contains per-page folios in the covered range (e.g. split in the swap cache by deferred_split_scan() or memory_failure() while the PMD swap entry was installed), or any subpage is hardware-poisoned, the PMD swap entry is split into PTE-level entries via __split_huge_pmd() and a non-zero error is returned so unuse_pmd_range() falls through to unuse_pte_range(), which handles the individual entries at order-0.
Remove a failed !uptodate PMD-sized folio from swap cache before splitting so PTE fallback rereads each slot independently. Keep hwpoisoned folios cached so PTE fallback can isolate bad subpages.
--- mm/swap.h | 17 +++++ mm/swap_state.c | 44 +++++++++++++ mm/swapfile.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 230 insertions(+)
mm/swap.h +17 -0
@@ -311,6 +311,23 @@ static inline bool folio_matches_swap_entry(const struct folio *folio,bool swap_cache_has_folio (swp_entry_t entry);struct folio *swap_cache_get_folio (swp_entry_t entry);void *swap_cache_get_shadow (swp_entry_t entry); +enum swap_pmd_cache { +SWAP_PMD_CACHE_EMPTY , +SWAP_PMD_CACHE_HUGE , +SWAP_PMD_CACHE_SPLIT , +}; + +#ifdef CONFIG_THP_SWAP +enum swap_pmd_cache swap_pmd_cache_lookup (swp_entry_t entry, +struct folio **foliop); +#else +static inline enum swap_pmd_cache swap_pmd_cache_lookup (swp_entry_t entry, +struct folio **foliop) +{ + *foliop =NULL ; +return SWAP_PMD_CACHE_EMPTY ; +} +#endif void swap_cache_del_folio (struct folio *folio); struct folio *swap_cache_alloc_folio(swp_entry_t target_entry, gfp_t gfp_mask, unsigned long orders, struct vm_fault *vmf,
mm/swap_state.c +44 -0
50 lines not on this page · raw message
mm/swapfile.c +169 -0
187 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 7/12 mm: handle PMD swap entries in non-present PMD walkers
In reply to Usama ArifTeach the remaining non-present PMD walkers about swap entries, mirroring the PTE-level equivalents.
smaps_pmd_entry() accounts swap and swap_pss via a new shared smaps_account_swap() helper used by both PTE and PMD paths. For a PMD range, it accounts each covered slot separately because their swap reference counts can differ.
move_soft_dirty_pmd(), clear_soft_dirty_pmd(), and make_uffd_wp_pmd(), pagemap_pmd_range_thp() and change_huge_pmd() handle swap entries alongside migration entries.
hmm_vma_handle_absent_pmd() records a fault for PMD swap entries via hmm_record_fault() instead of returning -EFAULT, allowing hmm_range_fault() to fault them in. The first per-page handle_mm_fault() call triggers do_huge_pmd_swap_page(), which maps the entire folio; subsequent calls become harmless huge_pmd_set_accessed() and the walker retries with a present PMD. When no fault is requested, it reports the range as non-resident rather than HMM_PFN_ERROR, matching the PTE swap entry path.
madvise_free_huge_pmd() handles PMD swap entries directly: for a full-range MADV_FREE it clears the PMD, frees the deposited page table, and releases the swap slots; for a partial range it splits to PTE swap entries. Without this, MADV_FREE silently becomes a no-op on swapped-out THPs and leaves the swap slots allocated, unlike the PTE path which frees them.
zap_huge_pmd() frees swap slots via swap_put_entries_direct(), matching zap_nonpresent_ptes().
change_non_present_huge_pmd() skips write-permission changes for swap entries and only updates uffd_wp, matching change_softleaf_pte().
madvise_cold_or_pageout_pte_range() skips PMD swap entries early. MADV_COLD and MADV_PAGEOUT operate on resident folios, so a swapped-out THP has nothing to deactivate or reclaim; skipping also prevents the walker from descending into or splitting the PMD swap entry. The locked THP path also treats a racing PMD swap entry as handled before checking for other non-present PMD types.
mincore_pte_range() routes PMD swap entries through a new mincore_pmd_swap() helper, matching how the PTE path already calls mincore_swap() for non-present PTEs. Without this a swapped-out PMD-mapped THP would be reported as resident, because pmd_is_huge() (and therefore pmd_trans_huge_lock()) accepts any non-present non-none PMD and the old branch unconditionally did memset(vec, 1, nr). mincore_pmd_swap() checks the PMD-sized swap-cache folio, or the individual slots if it was split. Migration and device-private PMDs keep their previous behavior of reporting 1.
check_pmd_state() in khugepaged returns SCAN_PMD_MAPPED for PMD swap entries, treating a swapped-out THP as still being a THP from khugepaged's perspective and matching the existing migration-entry handling.
change_huge_pmd() and pagemap_pmd_range_thp() drop redundant thp_migration_supported() gates: when PMD softleaves are unsupported, softleaf_from_pmd() returns a none entry and pmd_is_valid_softleaf() is false.
--- fs/proc/task_mmu.c | 46 ++++++++++++++++++++++++++-------------- mm/hmm.c | 11 +++++++++- mm/huge_memory.c | 53 ++++++++++++++++++++++++++++++++++++---------- mm/khugepaged.c | 6 ++++++ mm/madvise.c | 14 +++++++++++- mm/mincore.c | 45 ++++++++++++++++++++++++++++++++++++++- 6 files changed, 145 insertions(+), 30 deletions(-)
fs/proc/task_mmu.c +30 -16
@@ -1046,6 +1046,27 @@ static void smaps_pte_hole_lookup(unsigned long addr, struct mm_walk *walk)#endif } +static void smaps_account_swap (struct mem_size_stats *mss, +softleaf_t entry,unsigned long size) +{ +unsigned long nr_pages = size >>PAGE_SHIFT ; + + mss->swap += size; +do { +int mapcount =swp_swapcount (entry); + +if (mapcount >=2 ) { +u64 pss_delta = (u64 )PAGE_SIZE <<PSS_SHIFT ; + +do_div (pss_delta, mapcount); + mss->swap_pss += pss_delta; + }else { + mss->swap_pss += (u64 )PAGE_SIZE <<PSS_SHIFT ; + } + entry.val++; + }while (--nr_pages); +} +static void smaps_pte_entry (pte_t *pte,unsigned long addr,struct mm_walk *walk) {
@@ -1067,18 +1088,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,const softleaf_t entry =softleaf_from_pte (ptent);if (softleaf_is_swap (entry)) { -int mapcount; - - mss->swap +=PAGE_SIZE ; - mapcount =swp_swapcount (entry); -if (mapcount >=2 ) { -u64 pss_delta = (u64 )PAGE_SIZE <<PSS_SHIFT ; - -do_div (pss_delta, mapcount); - mss->swap_pss += pss_delta; - }else { - mss->swap_pss += (u64 )PAGE_SIZE <<PSS_SHIFT ; - } +smaps_account_swap (mss, entry,PAGE_SIZE ); }else if (softleaf_has_pfn (entry)) {if (softleaf_is_device_private (entry)) present = true;
@@ -1108,9 +1118,13 @@ static void smaps_pmd_entry(pmd_t *pmd, unsigned long addr,if (pmd_present (*pmd)) { page =vm_normal_page_pmd (vma, addr, *pmd); present = true; - }else if (unlikely (thp_migration_supported ())) { + }else {const softleaf_t entry =softleaf_from_pmd (*pmd); +if (softleaf_is_swap (entry)) { +smaps_account_swap (mss, entry,HPAGE_PMD_SIZE ); +return ; + }if (softleaf_has_pfn (entry)) page =softleaf_to_page (entry); }
@@ -1755,7 +1769,7 @@ static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma, pmd =pmd_clear_soft_dirty (pmd);set_pmd_at (vma->vm_mm, addr, pmdp, pmd); - }else if (pmd_is_migration_entry (pmd)) { + }else if (pmd_is_migration_entry (pmd) ||pmd_is_swap_entry (pmd)) { pmd =pmd_swp_clear_soft_dirty (pmd);set_pmd_at (vma->vm_mm, addr, pmdp, pmd); }
@@ -2115,7 +2129,7 @@ static int pagemap_pmd_range_thp(pmd_t *pmdp, unsigned long addr, flags |=PM_UFFD_WP ;if (pm->show_pfn) frame =pmd_pfn (pmd) + idx; - }else if (thp_migration_supported ()) { + }else if (pmd_is_valid_softleaf (pmd)) {const softleaf_t entry =softleaf_from_pmd (pmd);unsigned long offset;
@@ -2581,7 +2595,7 @@ static void make_uffd_wp_pmd(struct vm_area_struct *vma, old =pmdp_invalidate_ad (vma, addr, pmdp); pmd =pmd_mkuffd (old);set_pmd_at (vma->vm_mm, addr, pmdp, pmd); - }else if (pmd_is_migration_entry (pmd)) { + }else if (pmd_is_migration_entry (pmd) ||pmd_is_swap_entry (pmd)) { pmd =pmd_swp_mkuffd (pmd);set_pmd_at (vma->vm_mm, addr, pmdp, pmd); }
mm/hmm.c +10 -1
@@ -377,12 +377,21 @@ static int hmm_vma_handle_absent_pmd(struct mm_walk *walk, unsigned long start, required_fault =hmm_range_need_fault (hmm_vma_walk, hmm_pfns, npages,0 );if (required_fault) { -if (softleaf_is_device_private (entry)) +if (softleaf_is_device_private (entry) || +softleaf_is_swap (entry))return hmm_record_fault (addr, end, required_fault, walk);else return -EFAULT ; } +/* +* A swapped-out THP is not resident. Report it as not-valid, +* matching what hmm_vma_handle_pte() does for a PTE swap entry when +* no fault was requested. +*/ +if (softleaf_is_swap (entry)) +return hmm_pfns_fill (start, end, range,0 ); + return hmm_pfns_fill(start, end, range, HMM_PFN_ERROR); }#else
mm/huge_memory.c +42 -11
95 lines not on this page · raw message
mm/khugepaged.c +6 -0
@@ -1125,6 +1125,12 @@ static inline enum scan_result check_pmd_state(pmd_t *pmd)*/ if (pmd_is_migration_entry (pmde))return SCAN_PMD_MAPPED ; +/* +* A PMD-mapped THP that has been swapped out is still a THP from +* khugepaged's perspective; treat it like a present huge PMD. +*/ +if (pmd_is_swap_entry (pmde)) +return SCAN_PMD_MAPPED ;if (!pmd_present (pmde))return SCAN_NO_PTE_TABLE ;if (pmd_trans_huge (pmde))
mm/madvise.c +13 -1
@@ -375,6 +375,15 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, !can_do_file_pageout (vma);#ifdef CONFIG_TRANSPARENT_HUGEPAGE +/* +* Swapped-out THPs have no resident folio to deactivate or reclaim. +* Avoid descending into or splitting a PMD swap entry. +*/ +if (pmd_is_swap_entry (*pmd)) { + walk->action =ACTION_CONTINUE ; +return 0 ; + } +if (pmd_trans_huge (*pmd)) {pmd_t orig_pmd;unsigned long next =pmd_addr_end (addr, end);
@@ -385,6 +394,9 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd,return 0 ; orig_pmd = *pmd; +if (pmd_is_swap_entry (orig_pmd)) + goto huge_unlock; +if (is_huge_zero_pmd (orig_pmd)) goto huge_unlock;
@@ -666,7 +678,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,int nr, max_nr; next =pmd_addr_end (addr, end); -if (pmd_trans_huge (*pmd)) +if (pmd_trans_huge (*pmd) ||pmd_is_swap_entry (*pmd))if (madvise_free_huge_pmd (tlb, vma, pmd, addr, next))return 0 ;
mm/mincore.c +44 -1
57 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 8/12 mm: handle PMD swap entries in MADV_WILLNEED
In reply to Usama Arifswapin_walk_pmd_entry() walks PTEs and skips non-present PMDs, so MADV_WILLNEED is a no-op on a PMD swap entry.
Handle PMD swap entries under pmd_trans_huge_lock(). If the covered swap-cache range already has a PMD-sized folio, there is nothing left to prefetch. If the range has split cache state, or any covered slot currently has a zswap entry, split the PMD swap entry and ask the walker to retry so the PTE path can handle the individual slots.
Otherwise pin the swap device and read the folio in at PMD order via swapin_sync(BIT(HPAGE_PMD_ORDER)). This keeps the subsequent fault on the do_huge_pmd_swap_page() path and avoids order-0 readahead needlessly splitting the PMD swap entry.
Any failure of the PMD-order swapin splits the entry and retries through the PTE path. That covers losing a race with per-slot swap-cache population (-EBUSY) after dropping the PMD lock, but also the -ENOMEM that a PMD-order allocation can easily hit: leaving the entry alone would make MADV_WILLNEED prefetch nothing at all for the range, while the PTE path can still read the 512 slots at order 0.
If per-page zswap state reappears during the read, remove the failed clean PMD-sized folio from swap cache before splitting so the PTE path can load each slot. This uses folio_trylock(): the lock is only free once the read has completed, so MADV_WILLNEED never blocks on in-flight I/O, and unlike testing folio_test_locked() directly it cannot race with an unrelated lock holder.
--- mm/madvise.c | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+)
mm/madvise.c +106 -0
@@ -33,6 +33,7 @@#include <linux/shmem_fs.h> #include <linux/mmu_notifier.h> #include <linux/swap_ops.h> +#include <linux/zswap.h> #include <asm/tlb.h>
@@ -185,6 +186,93 @@ static int madvise_update_vma(vm_flags_t new_flags, }#ifdef CONFIG_SWAP +/* +* Prefetch a whole PMD swap entry as one PMD-order folio. +* +* Called with the PMD lock held; always drops it. Returns true when the +* caller should ask the walker to retry so the PTE path can handle the +* covered slots individually. +*/ +static bool swapin_pmd_swap_entry (struct vm_area_struct *vma,pmd_t *pmd, +unsigned long addr,softleaf_t entry, +spinlock_t *ptl) +{ +struct vm_fault vmf = { + .vma = vma, + .address = addr, + .real_address = addr, + .pmd = pmd, + }; +enum swap_pmd_cache cache_state; +struct swap_info_struct *si; +struct folio *folio; +bool split = false; + + cache_state =swap_pmd_cache_lookup (entry, &folio); +if (cache_state ==SWAP_PMD_CACHE_HUGE ) { +/* Already cached as one PMD-sized folio, nothing to do. */ +folio_put (folio); +spin_unlock (ptl); +return false; + } +if (cache_state ==SWAP_PMD_CACHE_SPLIT || +zswap_is_present (entry,HPAGE_PMD_NR )) { +spin_unlock (ptl); +return true; + } + +/* +* Pin the swap device under the PMD lock so the PMD-swap-entry +* observation keeps the entry valid for swapin_sync(). +*/ + si =get_swap_device (entry); +spin_unlock (ptl); +if (!si) +return false; + + folio =swapin_sync (entry,GFP_HIGHUSER_MOVABLE ,BIT (HPAGE_PMD_ORDER ), + &vmf,NULL ,0 ); + +/* +* Fall back to PTE-order swapin: a PMD-order failure does not mean +* that individual slots cannot be read. +*/ +if (IS_ERR_OR_NULL (folio)) { + split = true; + goto out; + } + +if (folio_nr_pages (folio) !=HPAGE_PMD_NR ) { + split = true; + goto out_put; + } + +/* +* A trylock only succeeds once the read has completed, so this never +* blocks MADV_WILLNEED on in-flight I/O. A failed PMD-order zswap load +* leaves the folio clean and not uptodate; drop it from the swap cache +* so the PTE retry can load the per-page state. Another thread may +* have removed it already, so revalidate the association first. +*/ +if (!folio_trylock (folio)) + goto out_put; + +if (!folio_test_uptodate (folio) && +zswap_is_present (entry,HPAGE_PMD_NR )) { +if (folio_matches_swap_entry (folio, entry)) +swap_cache_del_folio (folio); + split = true; + } +folio_unlock (folio); + +out_put: +folio_put (folio); +out: +/* Keep the device pinned until the last use of @entry. */ +put_swap_device (si); +return split; +} +static int swapin_walk_pmd_entry (pmd_t *pmd,unsigned long start,unsigned long end,struct mm_walk *walk) {
@@ -194,6 +282,23 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,spinlock_t *ptl;unsigned long addr; + ptl =pmd_trans_huge_lock (pmd, vma); +if (ptl) { +pmd_t pmdval = *pmd; + +if (pmd_is_swap_entry (pmdval)) { +/* swapin_pmd_swap_entry() always drops the PMD lock. */ +if (swapin_pmd_swap_entry (vma, pmd, start, +softleaf_from_pmd (pmdval), + ptl)) { +__split_huge_pmd (vma, pmd, start, false); + walk->action =ACTION_AGAIN ; + } + goto ret; + } +spin_unlock (ptl); + } +for (addr = start; addr < end; addr +=PAGE_SIZE ) {pte_t pte;softleaf_t entry;
@@ -222,6 +327,7 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,if (ptep)pte_unmap_unlock (ptep, ptl);swap_read_submit (&ctx); +ret:cond_resched ();return 0 ;
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 9/12 mm: handle PMD swap entries in UFFDIO_MOVE
In reply to Usama Arifmove_pages_huge_pmd() returned -ENOENT for any non-trans_huge, non-migration PMD, which fails aligned UFFDIO_MOVE on a swapped-out THP -- the PMD swap entry is a perfectly valid mapping that should move whole. Splitting via the move_pages_ptes() fallback isn't a substitute either: __split_huge_pmd_locked() splits a PMD swap entry into HPAGE_PMD_NR PTE swap entries pointing at the same swap-cache folio, but move_pages_pte() refuses any swap-cache folio that is still large and returns -EBUSY.
Add move_swap_pmd(), modeled on move_swap_pte(), that moves the swap entry whole-PMD and re-anchors a PMD-sized swap-cache folio's anon rmap to the destination VMA. Reject !pmd_swp_exclusive() entries with -EBUSY to preserve UFFDIO_MOVE's single-owner semantics, propagate soft-dirty, arm the UFFD marker for an RWP-registered destination, and carry the deposited page table across with the entry. The marker handling matches move_swap_pte(): the source marker rides along with the entry and is additionally armed for an RWP destination.
The dispatcher in move_pages_huge_pmd() routes PMD swap entries through move_swap_pmd() after pinning the swap device and arming an mmu_notifier range. Both are guarded by CONFIG_THP_SWAP, since swap_pmd_cache_lookup() and friends only exist under CONFIG_SWAP and PMD swap entries cannot exist without CONFIG_THP_SWAP.
Before moving, classify the whole PMD swap-cache range with swap_pmd_cache_lookup(). A PMD swap entry can be moved whole only if the covered range is empty or backed by one PMD-sized folio. If the range already has per-slot cache state, split the PMD swap entry and return -EAGAIN so the caller retries through the PTE path.
If a PMD-sized folio is cached, lock and revalidate that it still matches the PMD swap entry. If no folio is cached, recheck all HPAGE_PMD_NR slots under both PMD locks before moving the entry; any per-slot folio that appears needs the PTE move path to update its rmap metadata. This avoids moving the PMD while cached folios still point at the old anon_vma/index.
Finally, reject a PMD swap entry at the *destination* with -EEXIST in move_pages(). Such a destination is not a hole, and unlike a PMD migration entry it does not resolve on its own: pte_alloc() skips a !pmd_none PMD, pte_offset_map_rw_nolock() then fails on the non-present PMD, and move_pages() would retry the resulting -EAGAIN forever, only escapable with a fatal signal.
--- mm/huge_memory.c | 140 ++++++++++++++++++++++++++++++++++++++++++++++- mm/userfaultfd.c | 14 +++++ 2 files changed, 153 insertions(+), 1 deletion(-)
mm/huge_memory.c +139 -1
@@ -2913,6 +2913,77 @@ int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,#endif #ifdef CONFIG_USERFAULTFD +#ifdef CONFIG_THP_SWAP +/* +* Move a PMD-level swap entry from src_pmd to dst_pmd. Both PMD locks are +* acquired here; src_folio (if present) must already be locked. The deposited +* page table backing the source THP is moved across with the entry. +*/ +static int move_swap_pmd (struct mm_struct *mm,struct vm_area_struct *dst_vma, +unsigned long dst_addr,unsigned long src_addr, +pmd_t *dst_pmd,pmd_t *src_pmd, +pmd_t orig_dst_pmd,pmd_t orig_src_pmd, +spinlock_t *dst_ptl,spinlock_t *src_ptl, +struct folio *src_folio,swp_entry_t entry) +{ +pgtable_t src_pgtable; +pmd_t moved_pmd; + +/* +* The folio may have been freed and reused for a different swap entry +* while it was unlocked. Re-verify the association. +*/ +if (src_folio &&unlikely (!folio_matches_swap_entry (src_folio, entry) || +folio_nr_pages (src_folio) !=HPAGE_PMD_NR )) +return -EAGAIN ; + +double_pt_lock (dst_ptl, src_ptl); + +if (!pmd_same (*src_pmd, orig_src_pmd) || + !pmd_same (*dst_pmd, orig_dst_pmd)) { +double_pt_unlock (dst_ptl, src_ptl); +return -EAGAIN ; + } + +/* +* If the folio is in the swap cache, re-anchor its anon rmap to the +* destination VMA so a future swap-in fault at dst_addr finds it. +* Otherwise, re-check the whole PMD swap range: a PMD swap entry is +* only a compact encoding for 512 swap slots, and any per-slot cached +* folio would need the PTE move path to update its rmap metadata. +*/ +if (src_folio) { +folio_move_anon_rmap (src_folio, dst_vma); + src_folio->index =linear_page_index (dst_vma, dst_addr); + }else { +unsigned int type =swp_type (entry); +pgoff_t offset =swp_offset (entry); +int i; + +for (i =0 ; i <HPAGE_PMD_NR ; i++) { +if (swap_cache_has_folio (swp_entry (type, offset + i))) { +double_pt_unlock (dst_ptl, src_ptl); +return -EAGAIN ; + } + } + } + + moved_pmd =pmdp_huge_get_and_clear (mm, src_addr, src_pmd); +if (pgtable_supports_soft_dirty ()) + moved_pmd =pmd_swp_mksoft_dirty (moved_pmd); +/* Re-arm RWP on the moved swap entry if dst_vma is RWP-registered. */ +if (userfaultfd_rwp (dst_vma)) + moved_pmd =pmd_swp_mkuffd (moved_pmd); +set_pmd_at (mm, dst_addr, dst_pmd, moved_pmd); + + src_pgtable =pgtable_trans_huge_withdraw (mm, src_pmd); +pgtable_trans_huge_deposit (mm, dst_pmd, src_pgtable); + +double_pt_unlock (dst_ptl, src_ptl); +return 0 ; +} +#endif /* CONFIG_THP_SWAP */ +/* * The PT lock for src_pmd and dst_vma/src_vma (for reading) are locked by * the caller, but it must return after releasing the page_table_lock.
@@ -2947,11 +3018,78 @@ int move_pages_huge_pmd(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd, pm }if (!pmd_trans_huge (src_pmdval)) { -spin_unlock (src_ptl);if (pmd_is_migration_entry (src_pmdval)) { +spin_unlock (src_ptl);pmd_migration_entry_wait (mm, src_pmd);return -EAGAIN ; } +#ifdef CONFIG_THP_SWAP +if (pmd_is_swap_entry (src_pmdval)) { +swp_entry_t entry; +struct swap_info_struct *si; +enum swap_pmd_cache cache_state; + +/* +* UFFDIO_MOVE on anon mappings requires single-owner +* semantics; refuse to move a shared swap entry. +*/ +if (!pmd_swp_exclusive (src_pmdval)) { +spin_unlock (src_ptl); +return -EBUSY ; + } + + entry =softleaf_from_pmd (src_pmdval); +spin_unlock (src_ptl); + +/* Pin the swap device against a racing swapoff. */ + si =get_swap_device (entry); +if (unlikely (!si)) +return -EAGAIN ; + + src_folio =NULL ; + cache_state =swap_pmd_cache_lookup (entry, &src_folio); +if (cache_state ==SWAP_PMD_CACHE_SPLIT ) { +put_swap_device (si); +__split_huge_pmd (src_vma, src_pmd, src_addr, false); +return -EAGAIN ; + } + +mmu_notifier_range_init (&range,MMU_NOTIFY_CLEAR ,0 , + mm, src_addr, + src_addr +HPAGE_PMD_SIZE ); +mmu_notifier_invalidate_range_start (&range); + +if (src_folio) { +folio_lock (src_folio); +if (!folio_matches_swap_entry (src_folio, entry) || +folio_nr_pages (src_folio) !=HPAGE_PMD_NR ) { + err = -EAGAIN ; +folio_unlock (src_folio); +folio_put (src_folio); +mmu_notifier_invalidate_range_end (&range); +put_swap_device (si); +__split_huge_pmd (src_vma, src_pmd, + src_addr, false); +return err; + } + } + + dst_ptl =pmd_lockptr (mm, dst_pmd); + err =move_swap_pmd (mm, dst_vma, dst_addr, src_addr, + dst_pmd, src_pmd, dst_pmdval, + src_pmdval, dst_ptl, src_ptl, + src_folio, entry); + +mmu_notifier_invalidate_range_end (&range); +if (src_folio) { +folio_unlock (src_folio); +folio_put (src_folio); + } +put_swap_device (si); +return err; + } +#endif /* CONFIG_THP_SWAP */ +spin_unlock (src_ptl);return -ENOENT ; }
mm/userfaultfd.c +14 -0
@@ -2106,6 +2106,20 @@ static ssize_t move_pages(struct userfaultfd_ctx *ctx, unsigned long dst_start,break ; } +/* +* A PMD swap entry at dst is a swapped-out THP, not a hole, +* and unlike a PMD migration entry it will not resolve on its +* own. Nothing below faults it back in: pte_alloc() skips a +* !pmd_none PMD, pte_offset_map_rw_nolock() then fails on the +* non-present PMD, and the -EAGAIN that produces would be +* retried forever by the loop below. Be strict, exactly as for +* a present THP. +*/ +if (unlikely (pmd_is_swap_entry (dst_pmdval))) { + err = -EEXIST ; +break ; + } + ptl =pmd_trans_huge_lock (src_pmd, src_vma);if (ptl) {/* Check if we can move the pmd without splitting it. */
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 10/12 mm: handle PMD swap entry faults on swap-in
In reply to Usama ArifAdd do_huge_pmd_swap_page() and dispatch to it from __handle_mm_fault() when vmf->orig_pmd encodes a swap entry. The handler resolves the entire 2 MB mapping in one shot, mirroring do_swap_page() (PTE path) at PMD granularity:
- Look up the folio in the swap cache; on a miss, allocate a PMD-order folio via swapin_sync(BIT(HPAGE_PMD_ORDER)) and read from swap. This deliberately skips the existing order-0 swap readahead paths: the fault already asks for the whole PMD range, while order-0 readahead would populate per-page swap cache state and force the PMD swap entry to split. If the range already has per-page swap-cache or zswap state, split and retry through PTEs.
- After locking, re-validate that the folio still corresponds to our entry and is still PMD-sized. Between the unlocked cache lookup and the lock, a racing swap-in on the same entry may have removed it from the cache via folio_free_swap(), or reclaim / memory_failure / deferred-split may have split the folio into smaller folios.
- Refuse to map a folio that contains a hardware-poisoned subpage. On a hit, split the PMD swap entry so do_swap_page() can return VM_FAULT_HWPOISON per subpage, matching the PTE swap-in PageHWPoison check. Disable large-folio PTE batching for such a folio so the fallback cannot map the poisoned subpage as part of a batch.
- Restore soft_dirty and uffd_wp from the swap PMD. For an RWP VMA, also restore PAGE_NONE so the next access reaches userfaultfd. Map writable only when the entry was exclusive, the VMA permits writes, and uffd-wp is not armed. Drop the exclusive marker when the cached folio is under writeback to an SWP_STABLE_WRITES backend (zram) so the PMD is mapped read-only; a later write COWs into a fresh folio rather than corrupting the in-flight writeback. Mirrors do_swap_page().
- When the resulting PMD is read-only but the fault was a write, update vmf->orig_pmd and call wp_huge_pmd() in the same handler to COW without waiting for a second fault, unless wp_huge_pmd() itself has to fall back to PTE level. Leave an RWP-restored PMD for userfaultfd. Mask VM_FAULT_FALLBACK from the return: a PMD-COW that splits to PTE-level is normal, but the bit is part of VM_FAULT_ERROR and arch fault handlers BUG() on it without SIGBUS/HWPOISON/SIGSEGV.
- Free the swap slot via should_try_to_free_swap() (hoisted from mm/memory.c into mm/internal.h so PTE- and PMD-level swap-in share the heuristic).
When PMD-order resources are unavailable (folio allocation fails, the cached folio was split, memcg charge fails, or swap-cache insertion races) split the PMD swap entry into 512 PTE swap entries via __split_huge_pmd() and return 0. The fault retries and do_swap_page() takes over per-PTE. This avoids returning VM_FAULT_OOM for transient PMD-order allocation failures.
--- include/linux/huge_mm.h | 14 +++ mm/huge_memory.c | 242 ++++++++++++++++++++++++++++++++++++++++ mm/internal.h | 42 +++++++ mm/memory.c | 43 ++----- 4 files changed, 306 insertions(+), 35 deletions(-)
include/linux/huge_mm.h +14 -0
@@ -552,6 +552,15 @@ vm_fault_t do_huge_pmd_uffd_rwp(struct vm_fault *vmf);vm_fault_t do_huge_pmd_device_private (struct vm_fault *vmf); +#ifdef CONFIG_THP_SWAP +vm_fault_t do_huge_pmd_swap_page (struct vm_fault *vmf); +#else +static inline vm_fault_t do_huge_pmd_swap_page (struct vm_fault *vmf) +{ +return 0 ; +} +#endif +extern struct folio *huge_zero_folio;extern unsigned long huge_zero_pfn;
@@ -754,6 +763,11 @@ static inline vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf)return 0 ; } +static inline vm_fault_t do_huge_pmd_swap_page (struct vm_fault *vmf) +{ +return 0 ; +} +static inline bool is_huge_zero_folio (const struct folio *folio) {return false;
mm/huge_memory.c +242 -0
254 lines not on this page · raw message
mm/internal.h +42 -0
48 lines not on this page · raw message
mm/memory.c +8 -35
73 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 11/12 mm: install PMD swap entries on swap-out
In reply to Usama ArifReclaim today splits a PMD-mapped anonymous THP into 512 PTE swap entries before unmap, losing the huge mapping across the swap round-trip and forcing khugepaged to rebuild it later. The contiguous swap range was already secured when the folio was added to the swap cache (a non-contiguous allocation would have split the folio earlier), so the PMD can be replaced by a single PMD-level swap entry instead.
This patch mirrors the existing PTE swap-out path at PMD granularity:
- shrink_folio_list() drops TTU_SPLIT_HUGE_PMD for PMD-mappable swapcache folios. zswap is handled by the PMD swap-in users: if any covered slot currently has a zswap entry, they split the PMD swap entry and fall back to the per-PTE path.
- try_to_unmap_one() now has a PMD branch that calls set_pmd_swap_entry() and adjusts MM_ANONPAGES / MM_SWAPENTS by HPAGE_PMD_NR before walk_done. TTU_SPLIT_HUGE_PMD remains the fallback.
- set_pmd_swap_entry() is the installer. Mirroring the PTE swap-out sequence at PMD granularity, it clears the present mapping (keeping the original for rollback), bumps the swap_map refcount for the folio's 512 slots, transfers the exclusive state in the swap entry, propagates the dirty bit to the folio so writeback is not lost, and installs a swap PMD that preserves the original soft-dirty / uffd-wp / exclusive bits. Any failing step rolls back the present mapping.
The swap entry value matches what 512 PTE swap entries would encode, so swap_map refcounting is unchanged: each of the 512 slots carries a count of 1, released individually on later split or together on swap-in.
Add thp_swpout_pmd to count each PMD mapping replaced by a PMD-level swap entry. Unlike the folio-level thp_swpout counter, a fork-shared THP can increment this counter once for each mapping; document that distinction.
--- Documentation/admin-guide/mm/transhuge.rst | 5 ++ include/linux/huge_mm.h | 2 + include/linux/vm_event_item.h | 1 + mm/huge_memory.c | 81 ++++++++++++++++++++++ mm/rmap.c | 19 +++++ mm/vmscan.c | 9 ++- mm/vmstat.c | 1 + 7 files changed, 117 insertions(+), 1 deletion(-)
Documentation/admin-guide/mm/transhuge.rst +5 -0
@@ -632,6 +632,11 @@ thp_swpout is incremented every time a huge page is swapout in one piece without splitting. +thp_swpout_pmd + is incremented every time a PMD mapping is replaced by a PMD-level + swap entry. A fork-shared THP can increment this counter once for each + PMD mapping that is swapped out. + thp_swpout_fallback is incremented if a huge page has to be split before swapout. Usually because failed to allocate some continuous swap space
include/linux/huge_mm.h +2 -0
@@ -554,6 +554,8 @@ vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf);#ifdef CONFIG_THP_SWAP vm_fault_t do_huge_pmd_swap_page (struct vm_fault *vmf); +int set_pmd_swap_entry (struct page_vma_mapped_walk *pvmw, +struct folio *folio);#else static inline vm_fault_t do_huge_pmd_swap_page (struct vm_fault *vmf) {
include/linux/vm_event_item.h +1 -0
@@ -108,6 +108,7 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT,THP_ZERO_PAGE_ALLOC_FAILED ,THP_SWPOUT ,THP_SWPOUT_FALLBACK , +THP_SWPOUT_PMD , #endif #ifdef CONFIG_BALLOON BALLOON_INFLATE,
mm/huge_memory.c +81 -0
84 lines not on this page · raw message
mm/rmap.c +19 -0
@@ -2282,6 +2282,25 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, goto walk_abort; } +#ifdef CONFIG_THP_SWAP +/* +* If the folio is in the swap cache and we're not +* asked to split, install a PMD-level swap entry. +*/ +if (!(flags &TTU_SPLIT_HUGE_PMD ) && +folio_test_anon (folio) && +folio_test_swapcache (folio)) { +if (set_pmd_swap_entry (&pvmw, folio)) + goto walk_abort; + +add_mm_counter (mm,MM_ANONPAGES , + -HPAGE_PMD_NR ); +add_mm_counter (mm,MM_SWAPENTS , +HPAGE_PMD_NR ); + goto walk_done; + } +#endif +if (flags &TTU_SPLIT_HUGE_PMD ) {/* * We temporarily have to drop the PTL and
mm/vmscan.c +8 -1
@@ -1329,7 +1329,14 @@ static unsigned int shrink_folio_list(struct list_head *folio_list,enum ttu_flags flags =TTU_BATCH_FLUSH ;bool was_swapbacked =folio_test_swapbacked (folio); -if (folio_test_pmd_mappable (folio)) +/* +* With THP_SWAP, PMD-mappable folios already in the +* swap cache can be unmapped with a PMD-level swap +* entry, avoiding the cost of splitting the PMD. +*/ +if (folio_test_pmd_mappable (folio) && + !(IS_ENABLED (CONFIG_THP_SWAP ) && +folio_test_swapcache (folio))) flags |=TTU_SPLIT_HUGE_PMD ;/* * Without TTU_SYNC, try_to_unmap will only begin to
mm/vmstat.c +1 -0
@@ -1435,6 +1435,7 @@ const char * const vmstat_text[] = { [I(THP_ZERO_PAGE_ALLOC_FAILED)] = "thp_zero_page_alloc_failed", [I(THP_SWPOUT)] = "thp_swpout", [I(THP_SWPOUT_FALLBACK)] = "thp_swpout_fallback", + [I (THP_SWPOUT_PMD )] ="thp_swpout_pmd" , #endif #ifdef CONFIG_BALLOON [I(BALLOON_INFLATE)] = "balloon_inflate",
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v6 12/12 selftests/mm: add PMD swap entry tests
In reply to Usama ArifExercise the PMD swap entry paths. Each test gets a fresh PMD-mapped THP from fixture setup, fills it with a page-distinct pattern, swaps it out with MADV_PAGEOUT, and verifies that thp_swpout_pmd increased.
The tests are:
- basic: fault in a swapped PMD and verify its contents.
- fork: verify parent and child can fault in the shared swap entry.
- fork_cow: verify parent and child writes remain isolated.
- write: fault in by writing one byte and preserve the rest of the THP.
- rwp_swapin: verify userfaultfd RWP survives PMD-order swap-in.
- munmap: unmap the full entry and check that VmSwap drops.
- mprotect: change full-range protections without faulting the entry in.
- split_mprotect: change half-range protections and verify the data.
- split_munmap: unmap half, drop its accounting, and preserve the rest.
- uffdio_move: move the entry and RWP state, then fault it in at dst.
- mremap: force the entry to a new aligned address and verify the data.
- pagemap: verify swapped bits and consecutive swap-slot offsets.
- mincore: walk the entry without faulting it in.
- madvise_free: release the slots, clear the entry, and verify zeroes.
- madvise_willneed: prefetch the entry and verify subsequent swap-in.
- swapoff: unuse the entry and preserve data and PMD/RWP state.
Fixture teardown owns the mappings and file descriptors and restores swap after assertion failures. PMD_SWAP_DEVICE remains optional for swapoff.
Distinguish an environment that cannot allocate a PMD THP from a failure to install a PMD swap entry, so the former skips while the latter fails. Also check VmSwap accounting, pagemap slot offsets, swapped state after non-faulting operations, and PMD restoration when zswap does not require PTE fallback.
Register the test with run_vmtests.sh and the default kselftest runner.
--- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/ksft_pmd_swap.sh | 4 + tools/testing/selftests/mm/pmd_swap.c | 742 ++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 752 insertions(+) create mode 100755 tools/testing/selftests/mm/ksft_pmd_swap.sh create mode 100644 tools/testing/selftests/mm/pmd_swap.c
tools/testing/selftests/mm/Makefile +2 -0
@@ -104,6 +104,7 @@ TEST_GEN_FILES += guard-regions TEST_GEN_FILES += merge TEST_GEN_FILES += rmap TEST_GEN_FILES += folio_split_race_test +TEST_GEN_FILES += pmd_swap ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty
@@ -165,6 +166,7 @@ TEST_PROGS += ksft_mremap.sh TEST_PROGS += ksft_pagemap.sh TEST_PROGS += ksft_pfnmap.sh TEST_PROGS += ksft_pkey.sh +TEST_PROGS += ksft_pmd_swap.sh TEST_PROGS += ksft_process_madv.sh TEST_PROGS += ksft_process_mrelease.sh TEST_PROGS += ksft_rmap.sh
tools/testing/selftests/mm/ksft_pmd_swap.sh new file +4 -0
@@ -0,0 +1,4 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0 + +./run_vmtests.sh -t pmd_swap
tools/testing/selftests/mm/pmd_swap.c new file +742 -0
742 lines not on this page · raw message
tools/testing/selftests/mm/run_vmtests.sh +4 -0
@@ -69,6 +69,8 @@ separated by spaces: test pagemap_scan IOCTL - pfnmap tests for VM_PFNMAP handling +- pmd_swap + tests for PMD-level swap entries - process_madv test for process_madv - cow
@@ -399,6 +401,8 @@ CATEGORY="pagemap" run_test ./pagemap_ioctl CATEGORY="pfnmap" run_test ./pfnmap +CATEGORY="pmd_swap" run_test ./pmd_swap + # COW tests CATEGORY="cow" run_test ./cow
Signature · 1 line
2.53.0-Meta
Re: [PATCH v6 01/12] mm: rename pmd_to_softleaf_folio() to pmd_softleaf_to_folio()
In reply to Usama ArifQuoted 9 lines · Usama Arif
pmd_to_softleaf_folio()converts the softleaf entry encoded by a PMD to a folio. Rename it topmd_softleaf_to_folio()to make the conversion direction explicit and align it withsoftleaf_to_folio().No functional change.
Suggested-by Dev Jain <[email protected]>Signed-off-by Usama Arif <[email protected]>---
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 02/12] mm: add PMD swap entry detection support
In reply to Usama ArifQuoted 14 lines · Usama Arif
Currently when a PMD-mapped THP is swapped out, the PMD is always split into
HPAGE_PMD_NRPTE-level swap entries. To preserve huge page information across swap cycles, later patches will install a single PMD-level swap entry instead. Add the infrastructure to detect those entries.Teach the softleaf layer to recognise PMD swap entries:
pmd_is_swap_entry()detects them andsoftleaf_is_valid_pmd_entry()accepts them as a valid non-present type. Because swap entries do not encode a PFN, makepmd_softleaf_to_folio()warn and return NULL for them instead of passing the swap offset tosoftleaf_to_folio(). Clear the exclusive overlay bit insoftleaf_from_pmd()before decoding, matching howsoft_dirtyand uffd bits are already stripped.
Quoted 5 lines
Add
pmd_swp_mkexclusive(),pmd_swp_exclusive(), andpmd_swp_clear_exclusive()helpers to each architecture that supports PMD softleaf entries (x86, arm64, s390, riscv, loongarch, powerpc), mirroring the existing PTE swap exclusive helpers in each arch'spgtable.h.
Ah, for migration entries we still use a dedicated migratetype. I actually have on my todo list to move to PTE bits as well.
(likely the _swp_ part should then be renamed to indicate that this is for softdirty entries, not just swap entries)
Quoted 3 lines
Provide generic no-op PMD swap exclusive fallbacks for architectures without PMD softleaf support, matching the generic PMD swap soft-dirty fallbacks.
No softleaf implies to migration and no swap, so this would work.
You should extend mm/debug_vm_pgtable.c in a separate commit to test what pte_swap_exclusive_tests() tests for PMDs.
Quoted 9 lines
Signed-off-by Usama Arif <[email protected]>--- arch/arm64/include/asm/pgtable.h | 6 +++++ arch/loongarch/include/asm/pgtable.h | 19 ++++++++++++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 17 +++++++++++++ arch/riscv/include/asm/pgtable.h | 15 +++++++++++ arch/s390/include/asm/pgtable.h | 17 +++++++++++++ arch/x86/include/asm/pgtable.h | 17 +++++++++++++
I'm sorry for asking you to compete with Kiryll's series by creating one patch for each architecture that directly jumps at arch maintainers :)
[...]
include/linux/leafops.h static inline softleaf_t softleaf_from_pmd(pmd_t pmd) · line 106
Quoted 13 lines
#ifdef
CONFIG_HAVE_ARCH_SOFT_DIRTY
static inlinepte_tpte_swp_mksoft_dirty(pte_t pte){include/linux/leafops.h +2
-0@@ -102,6 +102,8 @@ static inline softleaf_t softleaf_from_pmd(pmd_t pmd) pmd =pmd_swp_clear_soft_dirty (pmd);if (pmd_swp_uffd (pmd)) pmd =pmd_swp_clear_uffd (pmd); +if (pmd_swp_exclusive (pmd)) + pmd =pmd_swp_clear_exclusive (pmd);
Can't we just unconditionally clear these flags?
pmd = pmd_swp_clear_uffd(pmd);
pmd = pmd_swp_clear_exclusive(pmd);
Avoids these rather unnecessary conditionals unless I am missing something.
include/linux/leafops.h static inline bool pmd_is_migration_entry(pmd_t pmd) · line 639
Quoted 9 lines
include/linux/leafops.h +1
-1arch_entry =__pmd_to_swp_entry (pmd);/* Temporary until swp_entry_t eliminated. */ @@ -634,18 +636,30 @@ static inline bool pmd_is_migration_entry(pmd_t pmd)*/ static inline bool softleaf_is_valid_pmd_entry (softleaf_t entry) { -/* Only device private, migration entries valid for PMD. */ +/* Device private, migration, and swap entries valid for PMD. */
Can we just drop that comment? I mean, it's as clear as it gets in the code immediately below :)
Quoted 6 lines
include/linux/leafops.h +4
-1return softleaf_is_device_private (entry) || -softleaf_is_migration (entry); +softleaf_is_migration (entry) || +softleaf_is_swap (entry); +} +
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 03/12] mm: add PMD swap entry splitting support
In reply to Usama ArifQuoted 33 lines · Usama Arif
Add a swap branch in
__split_huge_pmd_locked()that splits a PMD swap entry into 512 PTE swap entries. No folio reference is needed because swap entries point to swap slots rather than pages. Each PTE inherits the correct sub-slot offset and preservessoft_dirty,uffd_wp, and exclusive flags.The
folio_remove_rmap_pmd()gate at the end must inspectold_pmdrather than *pmd: for a present THP split, *pmd has already been cleared bypmdp_invalidate(), and that invalidated bit pattern can decode as a plausible swap entry.This branch is reached from the explicit
__split_huge_pmd()callers that hit a non-present PMD: partial-range mprotect / munmap, thewp_huge_pmd()PMD-COW fallback, and the swap-in / swapoff fallbacks added in later patches when the cached folio is no longer PMD-sized.page_vma_mapped_walk()does not iterate PMD swap entries, sotry_to_unmap_one()andtry_to_migrate_one()do not reach this branch and freeze=true cannot occur in this branch today. page and folio are therefore left uninitialized in the swap branch; aVM_WARN_ON_ONCE(freeze)catches any future caller that breaks this invariant before the freeze path dereferencespage_to_pfn(page + i)orput_page(page).Signed-off-by Usama Arif <[email protected]>--- mm/huge_memory.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)mm/huge_memory.c +0
-0
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3258
Quoted 7 lines
mm/huge_memory.c +4
-0@@ -3252,6 +3252,14 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , vma, haddr, rmap_flags); } + }else if (pmd_is_swap_entry (*pmd)) { +VM_WARN_ON_ONCE (freeze); +/* Swap entries have no page for the migration freeze path. */ + freeze = false;
It's odd to VM_WARN_ON_ONCE() and then set freeze=false;
I'd just add the comment above the VM_WARN_ON_ONCE() and drop the =false.
freeze=true really only applies during page migration, where swap entries don't apply.
I think it's time to clean that up ... that is
a) Expose a helper called split_pmd_to_migration_entries() that is only used by code that installs migration entries.
b) Hide that "freeze" flag from all other file-external functions
c) Rename the boolean to "use_migration_entries"
Then it's rather clear in this code that this should never happen.
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3400
Quoted 13 lines
mm/huge_memory.c +6
-0+ old_pmd = *pmd; + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd (old_pmd); + anon_exclusive =pmd_swp_exclusive (old_pmd); }else {/* * Up to this point the pmd is present and huge and userland has @@ -3388,6 +3396,25 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,VM_WARN_ON (!pte_none (ptep_get (pte + i)));set_pte_at (mm, addr, pte + i, entry); } + }else if (pmd_is_swap_entry (old_pmd)) { +softleaf_t sl_entry =softleaf_from_pmd (old_pmd);
No existing code uses "sl_entry". Maybe just call it "pmd_swp_entry"/"swp_entry" and below "pte_swp_entry".
Apart from that nothing jumped at me :)
Signature · 3 lines
Cheers, David
Yosry Ahmed
maintainerRe: [PATCH v6 05/12] mm: zswap: add range lookup for large-folio swapin
In reply to Usama ArifQuoted 70 lines · Usama Arif
From: Alexandre Ghiti <[email protected]>
A large folio reaches
zswap_load()only when the caller expects the whole range to be on disk. Zswap still stores large folios as independent order-0 entries, so reconstructing a large folio from zswap entries would risk returning partially initialized data.Teach
zswap_load()to scan the covered range. If no slot is in zswap, return-ENOENTsoswap_read_folio()reads the backing device. If any slot is still in zswap, fail the large-folio read so the caller can fall back to per-page swapin.Return
-EIOrather than-EINVALfor that conflict. Large-folio loads are now valid requests; the error means zswap cannot safely satisfy the request from partial per-page compressed state, not that the request is unsupported. Existing callers only distinguish-ENOENT, so this is a semantic clarification rather than a behavioral change.Add
zswap_is_present()so PMD swap-entry consumers can make the same range decision before attempting PMD-order swapin. Also use it from__swap_cache_add_check()for multi-page insertions while holding the swap cluster lock. That check runs before folio allocation and again immediately before swap-cache insertion, closing the race with zswap writeback and rejecting mixed zswap/disk backing with-EBUSY.Signed-off-by Alexandre Ghiti <[email protected]>Signed-off-by Usama Arif <[email protected]>--- include/linux/zswap.h | 6 ++++++ mm/swap_state.c | 10 ++++++++++ mm/zswap.c | 46 +++++++++++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 13 deletions(-)include/linux/zswap.h +6
-0@@ -35,6 +35,7 @@ void zswap_lruvec_state_init(struct lruvec *lruvec);void zswap_folio_swapin (struct folio *folio);bool zswap_is_enabled (void );bool zswap_never_enabled (void ); +bool zswap_is_present (swp_entry_t entry,unsigned int nr);#else struct zswap_lruvec_state {};@@ -69,6 +70,11 @@ static inline bool zswap_never_enabled(void)return true; } +static inline bool zswap_is_present (swp_entry_t entry,unsigned int nr) +{ +return false; +} +#endif #endif /* _LINUX_ZSWAP_H */ mm/swap_state.c +1
-0@@ -12,6 +12,7 @@#include <linux/kernel_stat.h> #include <linux/mempolicy.h> #include <linux/swap.h> +#include <linux/zswap.h> #include <linux/leafops.h> #include <linux/init.h> #include <linux/pagemap.h>
mm/swap_state.c static int __swap_cache_add_check(struct swap_cluster_info *ci, · line 201
Quoted 10 lines
mm/swap_state.c +7
-0@@ -191,6 +192,15 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,if (nr ==1 )return 0 ; +/* +* The cluster lock serializes swap-cache insertion with zswap +* writeback. Reject mixed zswap/disk backing before allocating a +* large folio and recheck it before adding the folio to swap cache. +*/ +if (zswap_is_present (swp_entry (swp_type (targ_entry), +round_down (swp_offset (targ_entry), nr)), nr))
Sorry I didn't catch it in the discussion in v5, but I think it's actually clearer to do have this check in __swap_cache_alloc() as you initially suggested, but not necessarily under the lock.
I don't think the cluster lock is relevant per se, but rather the actual swap cache allocation. Once you allocate the folio in the swapcache, you cannot race with zswap store or writeback. I think the comment here is a bit misleading in that regard. Especially that it mentions writeback, but I think the real risk is racing with zswap store?
The check here is performed twice, once before allocating the folio and once after. I think the one before allocating the folio is not really useful, as we don't actually allocate the swapcache entry so nothing actually prevents a zswap store/writeback from happening right after releasing the lock.
We already have a failure path in __swap_cache_alloc() after allocating the folio and dropping the lock. Can we use the same path to check if zswap is present?
Maybe something along these lines (completely untested):
mm/swap_state.c +11 -1
@@ -415,6 +415,7 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,swp_entry_t entry;struct folio *folio;void *shadow =NULL ; +bool large_in_zswap;unsigned short memcg_id;unsigned long address, nr_pages =1UL << order;struct vm_area_struct *vma = vmf ? vmf->vma :NULL ;
@@ -459,7 +460,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,__swap_cache_do_add_folio (ci, folio, entry);spin_unlock (&ci->lock); -if (mem_cgroup_swapin_charge_folio (folio, memcg_id, +/* +* Check if any part of the folio is in zswap after stabilizing the swap +* cache entry to avoid races with zswap store/writeback. Reject +* high-order entries that are partially or fully in zswap, as it is not +* supported. +*/ + large_in_zswap = order &&zswap_is_present (entry, nr_pages); + +if (large_in_zswap || +mem_cgroup_swapin_charge_folio (folio, memcg_id, vmf ? vmf->vma->vm_mm :NULL , gfp)) {spin_lock (&ci->lock);__swap_cache_do_del_folio (ci, folio, entry, shadow);
Quoted 80 lines
mm/swap_state.c +2
-0+return -EBUSY ; + is_zero =__swap_table_test_zero (ci, ci_off); ci_off =round_down (ci_off, nr); ci_end = ci_off + nr;mm/zswap.c +33
-13@@ -1571,6 +1571,23 @@ bool zswap_store(struct folio *folio)return ret; } +/** +* zswap_is_present() - is any slot in [entry, entry + nr) in zswap? +* @entry: base swap entry of the range +* @nr: number of contiguous slots to check (pass 1 for a single-slot query) +*/ +bool zswap_is_present (swp_entry_t entry,unsigned int nr) +{ +pgoff_t offset =swp_offset (entry); +struct xarray *tree =swap_zswap_tree (entry); +unsigned long index = offset; + +if (!nr ||zswap_never_enabled ()) +return false; + +return xa_find (tree, &index, offset + nr -1 ,XA_PRESENT ); +} +/** * zswap_load() - load a folio from zswap * @folio: folio to load @@ -1578,13 +1595,9 @@ bool zswap_store(struct folio *folio)* Return: 0 on success, with the folio unlocked and marked up-to-date, or one * of the following error codes: * -* -EIO: if the swapped out content was in zswap, but could not be loaded -* into the page due to a decompression failure. The folio is unlocked, but -* NOT marked up-to-date, so that an IO error is emitted (e.g. do_swap_page() -* will SIGBUS). -* -* -EINVAL: if the swapped out content was in zswap, but the page belongs -* to a large folio, which is not supported by zswap. The folio is unlocked, +* -EIO: if the swapped out content was in zswap but could not be handed +* back, either because decompression failed or because a slot in a +* large-folio range is unexpectedly still in zswap. The folio is unlocked, * but NOT marked up-to-date, so that an IO error is emitted (e.g. * do_swap_page() will SIGBUS). * @@ -1605,13 +1618,20 @@ int zswap_load(struct folio *folio)return -ENOENT ;/* -* Large folios should not be swapped in while zswap is being used, as -* they are not properly handled. Zswap does not properly load large -* folios, and a large folio may only be partially in zswap. +* A large folio reaches zswap_load() only when its whole range is +* expected to be on disk: PMD swap-entry consumers split before +* calling into PMD-order swapin whenever any slot is still in zswap. +* Confirm the range is entirely absent from zswap and return -ENOENT +* so the caller reads it from disk; if a slot is unexpectedly still in +* zswap, fail the read rather than return partially-initialized data. */ -if (WARN_ON_ONCE (folio_test_large (folio))) { -folio_unlock (folio); -return -EINVAL ; +if (folio_test_large (folio)) { +if (WARN_ON_ONCE (zswap_is_present (swp, +folio_nr_pages (folio)))) { +folio_unlock (folio); +return -EIO ; + } +return -ENOENT ; } entry =xa_load (tree, offset);Signature · 1 line
2.53.0-Meta
Re: [PATCH v6 01/12] mm: rename pmd_to_softleaf_folio() to pmd_softleaf_to_folio()
In reply to Usama ArifQuoted 8 lines · Usama Arif
pmd_to_softleaf_folio()converts the softleaf entry encoded by a PMD to a folio. Rename it topmd_softleaf_to_folio()to make the conversion direction explicit and align it withsoftleaf_to_folio().No functional change.
Suggested-by Dev Jain <[email protected]>Signed-off-by Usama Arif <[email protected]>
Haha OK fair enough that's a valid correction!
Quoted 41 lines
--- include/linux/leafops.h | 4 ++-- mm/huge_memory.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)include/linux/leafops.h +2
-2@@ -657,7 +657,7 @@ static inline bool pmd_is_valid_softleaf(pmd_t pmd) }/** -* pmd_to_softleaf_folio() - Convert the PMD entry to a folio. +* pmd_softleaf_to_folio() - Convert the PMD softleaf entry to a folio. * @pmd: PMD entry. * * The PMD entry is expected to be a valid PMD softleaf entry. @@ -665,7 +665,7 @@ static inline bool pmd_is_valid_softleaf(pmd_t pmd)* Returns: the folio the softleaf entry references if this is a valid softleaf * entry, otherwise NULL. */ -static inline struct folio *pmd_to_softleaf_folio (pmd_t pmd) +static inline struct folio *pmd_softleaf_to_folio (pmd_t pmd) {const softleaf_t entry =softleaf_from_pmd (pmd);mm/huge_memory.c +1
-2@@ -2467,7 +2467,7 @@ static struct folio *normal_or_softleaf_folio_pmd(struct vm_area_struct *vma,if (!thp_migration_supported ())WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); -return pmd_to_softleaf_folio (pmdval); +return pmd_softleaf_to_folio (pmdval); } static bool has_deposited_pgtable(struct vm_area_struct *vma, pmd_t pmdval, --
Quoted 2 lines
2.53.0-Meta
--
Cheers, Lorenzo
Re: [PATCH v6 01/12] mm: rename pmd_to_softleaf_folio() to pmd_softleaf_to_folio()
In reply to Usama ArifQuoted 4 lines · Usama Arif
pmd_to_softleaf_folio()converts the softleaf entry encoded by a PMD to a folio. Rename it topmd_softleaf_to_folio()to make the conversion direction explicit and align it withsoftleaf_to_folio().
Quoted 9 lines
No functional change.
Suggested-by Dev Jain <[email protected]>Signed-off-by Usama Arif <[email protected]>--- include/linux/leafops.h | 4 ++-- mm/huge_memory.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
LGTM.
Best Regards,
Yan, Zi
Lorenzo Stoakes (ARM)
maintainerRe: [PATCH v6 02/12] mm: add PMD swap entry detection support
In reply to David Hildenbrand (Arm)Quoted 15 lines · David Hildenbrand (Arm)
Quoted 14 lines · Usama Arif
Currently when a PMD-mapped THP is swapped out, the PMD is always split into
HPAGE_PMD_NRPTE-level swap entries. To preserve huge page information across swap cycles, later patches will install a single PMD-level swap entry instead. Add the infrastructure to detect those entries.Teach the softleaf layer to recognise PMD swap entries:
pmd_is_swap_entry()detects them andsoftleaf_is_valid_pmd_entry()accepts them as a valid non-present type. Because swap entries do not encode a PFN, makepmd_softleaf_to_folio()warn and return NULL for them instead of passing the swap offset tosoftleaf_to_folio(). Clear the exclusive overlay bit insoftleaf_from_pmd()before decoding, matching howsoft_dirtyand uffd bits are already stripped.
Quoted 8 lines
Quoted 5 lines
Add
pmd_swp_mkexclusive(),pmd_swp_exclusive(), andpmd_swp_clear_exclusive()helpers to each architecture that supports PMD softleaf entries (x86, arm64, s390, riscv, loongarch, powerpc), mirroring the existing PTE swap exclusive helpers in each arch'spgtable.h.Ah, for migration entries we still use a dedicated migratetype. I actually have on my todo list to move to PTE bits as well.
We do seem to have rather hacked in PMD level stuff across the board previously :)
Quoted 3 lines
(likely the _swp_ part should then be renamed to indicate that this is for softdirty entries, not just swap entries)
You mean softleaf?
Quoted 9 lines
Quoted 4 lines
Provide generic no-op PMD swap exclusive fallbacks for architectures without PMD softleaf support, matching the generic PMD swap soft-dirty fallbacks.
No softleaf implies to migration and no swap, so this would work.
You should extend
mm/debug_vm_pgtable.cin a separate commit to test whatpte_swap_exclusive_tests()tests for PMDs.
BTW one thing I should maybe mention here is that I never quite _finished_ the softleaf thing, so there's some awkward overlap between swap stuff and softleaf stuff (swap entries _are_ softleaf entries, just a specific kind).
Infinite TODO list etc. (maybe this is a hint/nudge to somebody else to do it ;)
Quoted 61 lines
Quoted 10 lines
Signed-off-by Usama Arif <[email protected]>--- arch/arm64/include/asm/pgtable.h | 6 +++++ arch/loongarch/include/asm/pgtable.h | 19 ++++++++++++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 17 +++++++++++++ arch/riscv/include/asm/pgtable.h | 15 +++++++++++ arch/s390/include/asm/pgtable.h | 17 +++++++++++++ arch/x86/include/asm/pgtable.h | 17 +++++++++++++I'm sorry for asking you to compete with Kiryll's series by creating one patch for each architecture that directly jumps at arch maintainers :)
[...]
include/linux/leafops.h static inline softleaf_t softleaf_from_pmd(pmd_t pmd) · line 106
Quoted 13 lines
#ifdef
CONFIG_HAVE_ARCH_SOFT_DIRTY
static inlinepte_tpte_swp_mksoft_dirty(pte_t pte){include/linux/leafops.h +2
-0@@ -102,6 +102,8 @@ static inline softleaf_t softleaf_from_pmd(pmd_t pmd) pmd =pmd_swp_clear_soft_dirty (pmd);if (pmd_swp_uffd (pmd)) pmd =pmd_swp_clear_uffd (pmd); +if (pmd_swp_exclusive (pmd)) + pmd =pmd_swp_clear_exclusive (pmd);Can't we just unconditionally clear these flags?
pmd =
pmd_swp_clear_uffd(pmd);
pmd =pmd_swp_clear_exclusive(pmd);Avoids these rather unnecessary conditionals unless I am missing something.
include/linux/leafops.h static inline bool pmd_is_migration_entry(pmd_t pmd) · line 639
Quoted 9 lines
include/linux/leafops.h +1
-1arch_entry =__pmd_to_swp_entry (pmd);/* Temporary until swp_entry_t eliminated. */ @@ -634,18 +636,30 @@ static inline bool pmd_is_migration_entry(pmd_t pmd)*/ static inline bool softleaf_is_valid_pmd_entry (softleaf_t entry) { -/* Only device private, migration entries valid for PMD. */ +/* Device private, migration, and swap entries valid for PMD. */ Can we just drop that comment? I mean, it's as clear as it gets in the code immediately below :)
include/linux/leafops.h static inline bool pmd_is_migration_entry(pmd_t pmd) · line 644
Quoted 6 lines
include/linux/leafops.h +4
-1return softleaf_is_device_private (entry) || -softleaf_is_migration (entry); +softleaf_is_migration (entry) || +softleaf_is_swap (entry); +} +--
Cheers,
Quoted 1 line
David
--
Cheers, Lorenzo
Lance Yang
reviewerRe: [PATCH v6 06/12] mm: swap in PMD swap entries as whole THPs during swapoff
In reply to Usama Arif+Cc Miaohe
On Tue, Aug 18, 2026 at 06:09:47AM -0700, Usama Arif wrote:
[...]
Quoted 27 lines
mm/swapfile.c +27
-0+#ifdef CONFIG_THP_SWAP +/* +* unuse_pmd - Map a locked folio at PMD granularity during swapoff. +* +* The caller provides a locked, swapped-in folio. Returns 0 on success +* (PMD was mapped). Returns -EAGAIN if the swap cache folio no longer +* matches the entry or the PMD changed under the lock (try_to_unuse will +* rescan). Returns -EIO if the folio is not uptodate or contains a poisoned +* subpage; in that case the PMD is split so unuse_pte_range() can handle +* individual pages. +*/ +static int unuse_pmd (struct vm_area_struct *vma,pmd_t *pmd, +unsigned long addr,softleaf_t entry, +struct folio *folio) +{ +struct mm_struct *mm = vma->vm_mm; +struct page *page; +pmd_t new_pmd, old_pmd; +spinlock_t *ptl; +rmap_t rmap_flags =RMAP_NONE ; +bool exclusive; + +if (unlikely (!folio_matches_swap_entry (folio, entry))) +return -EAGAIN ; + +if (unlikely (!folio_test_uptodate (folio))) { +/* Let PTE fallback reread each slot independently. */
mm/swapfile.c static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 2710
Quoted 10 lines
mm/swapfile.c +10
-0+swap_cache_del_folio (folio); +__split_huge_pmd (vma, pmd, addr, false); +return -EIO ; + } + +if (unlikely (folio_contain_hwpoisoned_page (folio))) { +/* Let PTE fallback isolate the poisoned subpages. */ +__split_huge_pmd (vma, pmd, addr, false); +return -EIO ; + }
Hmm ... can this miss a poisoned tail?
memory_failure() sets PageHWPoison(p) before taking folio_lock(), but PG_has_hwpoisoned is only set later. Since unuse_pmd_entry() holds folio lock across this check, memory_failure() can be blocked on folio_lock() with a tail already poisoned, IIUC ...
folio_contain_hwpoisoned_page() then sees neither a poisoned head nor PG_has_hwpoisoned, and set_pmd_at() maps that tail through a normal PMD.
Note that unuse_pte() checks PageHWPoison(page) directly. Should we check each subpage here as well before installing the PMD?
Quoted 40 lines
mm/swapfile.c +40
-0+ + page =folio_page (folio,0 ); + + ptl =pmd_lock (mm, pmd); + old_pmd =pmdp_get (pmd); + +if (!pmd_is_swap_entry (old_pmd) || +softleaf_from_pmd (old_pmd).val != entry.val) { +spin_unlock (ptl); +return -EAGAIN ; + } + + exclusive =pmd_swp_exclusive (old_pmd); + +/* +* Some architectures may have to restore extra metadata to the folio +* when reading from swap. This metadata may be indexed by swap entry +* so this must be called before folio_put_swap(). +*/ +arch_swap_restore (folio_swap (entry, folio), folio); + +add_mm_counter (mm,MM_ANONPAGES ,HPAGE_PMD_NR ); +add_mm_counter (mm,MM_SWAPENTS , -HPAGE_PMD_NR ); + + new_pmd =folio_mk_pmd (folio, vma->vm_page_prot); + new_pmd =pmd_mkold (new_pmd); +if (pmd_swp_soft_dirty (old_pmd)) + new_pmd =pmd_mksoft_dirty (new_pmd); +if (pmd_swp_uffd (old_pmd)) + new_pmd =pmd_mkuffd (new_pmd); +if (pmd_swp_uffd (old_pmd) &&userfaultfd_rwp (vma)) + new_pmd =pmd_modify (new_pmd,PAGE_NONE ); + +if (exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_get (folio); +if (!folio_test_anon (folio)) +folio_add_new_anon_rmap (folio, vma, addr, rmap_flags); +else
mm/swapfile.c static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 2760
Quoted 10 lines
mm/swapfile.c +10
-0+folio_add_anon_rmap_pmd (folio, page, vma, addr, rmap_flags); + +set_pmd_at (mm, addr, pmd, new_pmd); +folio_put_swap (folio,NULL ); + +spin_unlock (ptl); + +folio_free_swap (folio); +return 0 ; +}
[...]
Cheers, Lance
Lance Yang
reviewerRe: [PATCH v6 12/12] selftests/mm: add PMD swap entry tests
In reply to Usama ArifQuoted 23 lines · Usama Arif
Exercise the PMD swap entry paths. Each test gets a fresh PMD-mapped THP from fixture setup, fills it with a page-distinct pattern, swaps it out with
MADV_PAGEOUT, and verifies thatthp_swpout_pmdincreased.The tests are:
- basic: fault in a swapped PMD and verify its contents.
- fork: verify parent and child can fault in the shared swap entry.
-fork_cow: verify parent and child writes remain isolated.
- write: fault in by writing one byte and preserve the rest of the THP.
-rwp_swapin: verify userfaultfd RWP survives PMD-order swap-in.
- munmap: unmap the full entry and check that VmSwap drops.
- mprotect: change full-range protections without faulting the entry in.
-split_mprotect: change half-range protections and verify the data.
-split_munmap: unmap half, drop its accounting, and preserve the rest.
-uffdio_move: move the entry and RWP state, then fault it in at dst.
- mremap: force the entry to a new aligned address and verify the data.
- pagemap: verify swapped bits and consecutive swap-slot offsets.
- mincore: walk the entry without faulting it in.
-madvise_free: release the slots, clear the entry, and verify zeroes.
-madvise_willneed: prefetch the entry and verify subsequent swap-in.
- swapoff: unuse the entry and preserve data and PMD/RWP state.
Quoted 3 lines
Fixture teardown owns the mappings and file descriptors and restores swap after assertion failures.
PMD_SWAP_DEVICEremains optional for swapoff.
How do we know the PMD swap entry is on PMD_SWAP_DEVICE?
Assume another higher-priority swap device is active. MADV_PAGEOUT may place the entry there, so swapoff() operates on the wrong device. This can make the test pass with zswap enabled and fail with it disabled.
Should PMD_SWAP_DEVICE be required to be the only active swap device?
Cheers, Lance
Quoted 823 lines
Distinguish an environment that cannot allocate a PMD THP from a failure to install a PMD swap entry, so the former skips while the latter fails. Also check VmSwap accounting, pagemap slot offsets, swapped state after non-faulting operations, and PMD restoration when zswap does not require PTE fallback.
Register the test with
run_vmtests.shand the default kselftest runner.Signed-off-by Usama Arif <[email protected]>---
tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/ksft_pmd_swap.sh | 4 + tools/testing/selftests/mm/pmd_swap.c | 742 ++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 752 insertions(+)create mode 100755
tools/testing/selftests/mm/ksft_pmd_swap.shcreate mode 100644tools/testing/selftests/mm/pmd_swap.ctools/testing/selftests/mm/Makefile +0
-0@@ -104,6 +104,7 @@ TEST_GEN_FILES += guard-regions
TEST_GEN_FILES+= mergeTEST_GEN_FILES+= rmapTEST_GEN_FILES+=folio_split_race_test
+TEST_GEN_FILES +=pmd_swapifneq ($(ARCH),arm64)
TEST_GEN_FILES+= soft-dirtytools/testing/selftests/mm/Makefile +0
-0@@ -165,6 +166,7 @@ TEST_PROGS += ksft_mremap.sh
TEST_PROGS+=ksft_pagemap.shTEST_PROGS+=ksft_pfnmap.shTEST_PROGS+=ksft_pkey.sh
+TEST_PROGS +=ksft_pmd_swap.shTEST_PROGS+=ksft_process_madv.shTEST_PROGS+=ksft_process_mrelease.shTEST_PROGS+=ksft_rmap.shtools/testing/selftests/mm/ksft_pmd_swap.sh new file +4
-0@@ -0,0 +1,4 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0 + +./run_vmtests.sh -t pmd_swaptools/testing/selftests/mm/pmd_swap.c new file +742
-0@@ -0,0 +1,742 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Test PMD-level swap entries and their users. */ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <errno.h> +#include <stdint.h> +#include <sys/random.h> +#include <sys/swap.h> +#include <sys/syscall.h> +#include <sys/ioctl.h> +#include <poll.h> +#include <pthread.h> +#include <linux/userfaultfd.h> +#include <time.h> + +#include "kselftest_harness.h" +#include "vm_util.h" + +#define ZSWAP_ENABLED_PATH "/sys/module/zswap/parameters/enabled" + +/* pagemap: bits 0-54 hold the PFN, or type|offset for a swap entry. */ +#define PM_PFRAME_MASK ((1ULL << 55) - 1) +/* Must match MAX_SWAPFILES_SHIFT in include/linux/swap.h. */ +#define MAX_SWAPFILES_SHIFT 5 + +static bool check_swapped (int pagemap_fd,char *addr,unsigned long size) +{ +unsigned long off; + +for (off =0 ; off < size; off +=getpagesize ()) +if (!pagemap_is_swapped (pagemap_fd, addr + off)) +return false; +return true; +} + +static bool zswap_enabled (void ) +{ +char enabled =0 ; +FILE *f; + + f =fopen (ZSWAP_ENABLED_PATH ,"r" ); +if (!f) +return false; + +if (fscanf (f," %c" , &enabled) !=1 ) + enabled =0 ; +fclose (f); + +return enabled =='Y' || enabled =='y' || enabled =='1' ; +} + +static bool swap_available (unsigned long required_bytes) +{ +unsigned long required_kb = (required_bytes +1023 ) /1024 ; +unsigned long size_kb, used_kb; +char line[256 ]; +bool ret = false; +FILE *f; + + f =fopen ("/proc/swaps" ,"r" ); +if (!f) +return false; + +/* Skip the header. */ +if (!fgets (line,sizeof (line), f)) + goto out; + +while (fgets (line,sizeof (line), f)) { +if (sscanf (line,"%*s %*s %lu %lu" , &size_kb, &used_kb) ==2 && + size_kb >= used_kb && size_kb - used_kb >= required_kb) { + ret = true; +break ; + } + } + +out: +fclose (f); +return ret; +} + +static unsigned long read_vm_event (const char *name) +{ +char line[256 ]; +size_t name_len =strlen (name); +unsigned long val =0 ; +FILE *f; + + f =fopen ("/proc/vmstat" ,"r" ); +if (!f) +return 0 ; +while (fgets (line,sizeof (line), f)) { +if (!strncmp (line, name, name_len) && line[name_len] ==' ' ) { + val =strtoul (line + name_len +1 ,NULL ,10 ); +break ; + } + } +fclose (f); +return val; +} + +static unsigned int random_seed (void ) +{ +unsigned int seed; + +if (getrandom (&seed,sizeof (seed),0 ) !=sizeof (seed)) + seed = (unsigned int )time (NULL ); +return seed; +} + +static unsigned char pattern_byte (unsigned int seed,unsigned long off) +{ +return (unsigned char )(seed + off + (off >>8 ) + (off >>16 )); +} + +static void fill_pattern (char *buf,unsigned long size,unsigned int seed) +{ +unsigned long i; + +for (i =0 ; i < size; i++) + buf[i] = (char )pattern_byte (seed, i); +} + +static bool verify_pattern_range (char *buf,unsigned long size, +unsigned int seed,unsigned long offset) +{ +unsigned long i; + +for (i =0 ; i < size; i++) +if ((unsigned char )buf[i] !=pattern_byte (seed, offset + i)) +return false; +return true; +} + +static bool verify_pattern (char *buf,unsigned long size,unsigned int seed) +{ +return verify_pattern_range (buf, size, seed,0 ); +} + +static bool verify_zero (char *buf,unsigned long size) +{ +unsigned long i; + +for (i =0 ; i < size; i++) +if (buf[i]) +return false; +return true; +} + +/* +* mmap an anonymous PMD-aligned region of pmd_size bytes. Over-allocates +* by one PMD and trims the unaligned head/tail so the returned address is +* PMD-aligned (required for whole-PMD UFFDIO_MOVE). +*/ +static char *mmap_pmd_aligned (unsigned long pmd_size) +{ +unsigned long pad = pmd_size; +char *raw, *aligned; + + raw =mmap (NULL , pmd_size + pad,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (raw ==MAP_FAILED ) +return MAP_FAILED ; + + aligned = (char *)(((uintptr_t )raw + pmd_size -1 ) & ~(pmd_size -1 )); +if (aligned != raw) +munmap (raw, aligned - raw); +if (aligned + pmd_size != raw + pmd_size + pad) +munmap (aligned + pmd_size, + (raw + pmd_size + pad) - (aligned + pmd_size)); +return aligned; +} + +enum swap_thp_result { +SWAP_THP_OK , +SWAP_THP_UNAVAILABLE , +SWAP_THP_FAILED , +}; + +/* Per-process swapped size in bytes, from /proc/self/status VmSwap. */ +static unsigned long read_vmswap (void ) +{ +char line[256 ]; +unsigned long kb =0 ; +FILE *f; + + f =fopen ("/proc/self/status" ,"r" ); +if (!f) +return 0 ; +while (fgets (line,sizeof (line), f)) { +if (!strncmp (line,"VmSwap:" ,7 )) { + kb =strtoul (line +7 ,NULL ,10 ); +break ; + } + } +fclose (f); +return kb *1024 ; +} + +static bool swap_out_pmd (char *mem,unsigned long pmd_size,int pagemap_fd) +{ +unsigned long before =read_vm_event ("thp_swpout_pmd" ); +unsigned long after; + +if (madvise (mem, pmd_size,MADV_PAGEOUT )) { +ksft_print_msg ("MADV_PAGEOUT failed: %s\n" ,strerror (errno)); +return false; + } +if (!check_swapped (pagemap_fd, mem, pmd_size)) { +ksft_print_msg ("MADV_PAGEOUT did not swap the whole PMD range\n" ); +return false; + } + + after =read_vm_event ("thp_swpout_pmd" ); +ksft_print_msg ("thp_swpout_pmd: %lu -> %lu\n" , before, after); +return after > before; +} + +static char *alloc_fill_swap_thp (unsigned long pmd_size,int pagemap_fd, +unsigned int seed,enum swap_thp_result *res) +{ +char *mem; + + *res =SWAP_THP_UNAVAILABLE ; + + mem =mmap_pmd_aligned (pmd_size); +if (mem ==MAP_FAILED ) +return MAP_FAILED ; + +if (madvise (mem, pmd_size,MADV_HUGEPAGE )) { +ksft_print_msg ("MADV_HUGEPAGE failed: %s\n" ,strerror (errno)); +munmap (mem, pmd_size); +return MAP_FAILED ; + } +fill_pattern (mem, pmd_size, seed); + +if (!check_huge_anon (mem, pmd_size,1 , pmd_size)) { +munmap (mem, pmd_size); +return MAP_FAILED ; + } + *res =SWAP_THP_FAILED ; + +if (!swap_out_pmd (mem, pmd_size, pagemap_fd)) { +munmap (mem, pmd_size); +return MAP_FAILED ; + } + + *res =SWAP_THP_OK ; +return mem; +} + +struct rwp_access_args { +unsigned char *addr; +unsigned char expected; +bool write; +bool ok; +}; + +static void *rwp_access_thread (void *data) +{ +struct rwp_access_args *args = data; + +if (args->write) + *args->addr = args->expected; + args->ok = *args->addr == args->expected; +return NULL ; +} + +static int register_rwp (char *addr,unsigned long size,bool protect) +{ +struct uffdio_register reg = {}; +struct uffdio_rwprotect rwp = {}; +struct uffdio_api api = {}; +int uffd; + + uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (uffd <0 ) +return -1 ; + + api.api =UFFD_API ; + api.features =UFFD_FEATURE_RWP ; +if (ioctl (uffd,UFFDIO_API , &api) || + !(api.features &UFFD_FEATURE_RWP )) + goto error; + + reg.range.start = (unsigned long )addr; + reg.range.len = size; + reg.mode =UFFDIO_REGISTER_MODE_RWP ; +if (ioctl (uffd,UFFDIO_REGISTER , ®)) + goto error; + +if (!protect) +return uffd; + + rwp.range.start = (unsigned long )addr; + rwp.range.len = size; + rwp.mode =UFFDIO_RWPROTECT_MODE_RWP ; +if (!ioctl (uffd,UFFDIO_RWPROTECT , &rwp)) +return uffd; + +error: +close (uffd); +return -1 ; +} + +static bool expect_rwp_fault (int uffd,char *addr,unsigned long size, +unsigned char expected,bool write) +{ +struct rwp_access_args args = { + .addr = (unsigned char *)addr, + .expected = expected, + .write = write, + }; +struct uffdio_rwprotect rwp = { + .range = { + .start = (unsigned long )addr, + .len = size, + }, + }; +struct pollfd pollfd = { + .fd = uffd, + .events =POLLIN , + }; +struct uffd_msg msg = {}; +pthread_t thread; +bool saw_rwp = false; +int ret; + +if (pthread_create (&thread,NULL , rwp_access_thread, &args)) +return false; + + ret =poll (&pollfd,1 ,5000 ); +if (ret ==1 && (pollfd.revents &POLLIN ) && +read (uffd, &msg,sizeof (msg)) == (ssize_t )sizeof (msg)) { + saw_rwp = msg.event ==UFFD_EVENT_PAGEFAULT && + (msg.arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_RWP ); + } + +/* Resolve the access even on failure so the worker cannot remain blocked. */ +ioctl (uffd,UFFDIO_RWPROTECT , &rwp); +if (pthread_join (thread,NULL )) +return false; +return saw_rwp && args.ok; +} + +FIXTURE (pmd_swap) +{ +unsigned long pmd_size; +unsigned long mem_len; +int pagemap_fd; +int uffd; +unsigned int seed; +bool zswap_enabled; +bool swap_disabled; +const char *swap_dev; +char *mem; +char *aux; +}; + +FIXTURE_SETUP (pmd_swap) +{ +enum swap_thp_result res; + + self->pagemap_fd =-1 ; + self->uffd =-1 ; + self->mem =MAP_FAILED ; + self->aux =MAP_FAILED ; + self->mem_len =0 ; + self->swap_disabled = false; + self->swap_dev =getenv ("PMD_SWAP_DEVICE" ); +if (!strcmp (_metadata->name,"swapoff" ) && !self->swap_dev) +SKIP (return,"PMD_SWAP_DEVICE env var not set\n" ); + + self->pmd_size =read_pmd_pagesize (); +if (!self->pmd_size) +SKIP (return,"Cannot determine PMD size\n" ); + + self->pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY ); +if (self->pagemap_fd <0 ) +SKIP (return,"Cannot open /proc/self/pagemap\n" ); + +if (!swap_available (self->pmd_size)) +SKIP (return,"No active swap device has enough free space\n" ); + + self->seed =random_seed (); + self->zswap_enabled =zswap_enabled (); + self->mem =alloc_fill_swap_thp (self->pmd_size, self->pagemap_fd, + self->seed, &res); +if (self->mem ==MAP_FAILED ) { +ASSERT_NE (res,SWAP_THP_FAILED ); +SKIP (return,"Could not create swapped THP\n" ); + } + self->mem_len = self->pmd_size; +} + +FIXTURE_TEARDOWN (pmd_swap) +{ +int swap_err =0 ; +int swap_ret =0 ; + +if (self->swap_disabled) { + swap_ret =swapon (self->swap_dev,0 ); + swap_err = errno; + } +if (self->uffd >=0 ) +close (self->uffd); +if (self->aux !=MAP_FAILED ) +munmap (self->aux, self->pmd_size); +if (self->mem !=MAP_FAILED ) +munmap (self->mem, self->mem_len); +if (self->pagemap_fd >=0 ) +close (self->pagemap_fd); + + EXPECT_EQ(swap_ret, 0) { +TH_LOG ("swapon(%s) failed: %s" , self->swap_dev, +strerror (swap_err)); + } +} + +TEST_F(pmd_swap, basic) +{ +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, fork) +{ +pid_t pid; +int status; + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) +_exit (verify_pattern (self->mem, self->pmd_size, + self->seed) ?0 :1 ); + +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); + +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); +} + +TEST_F(pmd_swap, fork_cow) +{ +unsigned int parent_seed = self->seed; +unsigned int child_seed = ~self->seed; +unsigned int new_seed = self->seed ^0xa5a5a5a5 ; +int release_child[2 ]; +bool parent_ok; +char c =0 ; +pid_t pid; +int status, ret; + +ASSERT_EQ (pipe (release_child),0 ); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +close (release_child[1 ]); +if (read (release_child[0 ], &c,1 ) !=1 ) +_exit (1 ); +if (!verify_pattern (self->mem, self->pmd_size, parent_seed)) +_exit (2 ); +fill_pattern (self->mem, self->pmd_size, child_seed); +if (!verify_pattern (self->mem, self->pmd_size, child_seed)) +_exit (3 ); +_exit (0 ); + } + +close (release_child[0 ]); +fill_pattern (self->mem, self->pmd_size, new_seed); + parent_ok =verify_pattern (self->mem, self->pmd_size, new_seed); + ret =write (release_child[1 ], &c,1 ); +close (release_child[1 ]); +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_EQ (ret,1 ); +ASSERT_TRUE (parent_ok); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, new_seed)); +} + +TEST_F(pmd_swap, write) +{ + self->mem[0 ] =0xbb ; +ASSERT_EQ (self->mem[0 ], (char )0xbb ); +ASSERT_TRUE (verify_pattern_range (self->mem +1 , self->pmd_size -1 , + self->seed,1 )); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, rwp_swapin) +{ + self->uffd =register_rwp (self->mem, self->pmd_size, true); +if (self->uffd <0 ) +SKIP (return,"Userfaultfd RWP unsupported\n" ); + +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->mem, self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, munmap) +{ +unsigned long swap_before, swap_after; +int ret; + + swap_before =read_vmswap (); +ASSERT_GE (swap_before, self->pmd_size); + + ret =munmap (self->mem, self->pmd_size); +if (!ret) { + self->mem =MAP_FAILED ; + self->mem_len =0 ; + } +ASSERT_EQ (ret,0 ); + + swap_after =read_vmswap (); +ASSERT_LE (swap_after, swap_before - self->pmd_size); +} + +TEST_F(pmd_swap, mprotect) +{ +ASSERT_EQ (mprotect (self->mem, self->pmd_size,PROT_READ ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_EQ (mprotect (self->mem, self->pmd_size, +PROT_READ |PROT_WRITE ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, split_mprotect) +{ +unsigned long half = self->pmd_size /2 ; + +ASSERT_EQ (mprotect (self->mem, half,PROT_READ ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_EQ (mprotect (self->mem, half,PROT_READ |PROT_WRITE ),0 ); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, split_munmap) +{ +unsigned long half = self->pmd_size /2 ; +unsigned long swap_before =read_vmswap (); +unsigned long i; +char *base = self->mem; +int ret; + +ASSERT_GE (swap_before, half); + ret =munmap (base, half); +if (!ret) { + self->mem = base + half; + self->mem_len = half; + } +ASSERT_EQ (ret,0 ); +ASSERT_LE (read_vmswap (), swap_before - half); + +for (i =0 ; i < half; i +=getpagesize ()) +ASSERT_TRUE (pagemap_is_swapped (self->pagemap_fd, + self->mem + i)); +ASSERT_TRUE (verify_pattern_range (self->mem, half, self->seed, half)); +} + +TEST_F(pmd_swap, uffdio_move) +{ +struct uffdio_register reg = {}; +struct uffdio_move move = {}; +struct uffdio_api api = {}; +bool rwp; + + self->aux =mmap_pmd_aligned (self->pmd_size); +if (self->aux ==MAP_FAILED ) +SKIP (return,"Could not mmap aligned dst\n" ); +ASSERT_EQ (madvise (self->aux, self->pmd_size,MADV_HUGEPAGE ),0 ); + + self->uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (self->uffd <0 ) +SKIP (return,"userfaultfd unavailable\n" ); + + api.api =UFFD_API ; + api.features =UFFD_FEATURE_MOVE |UFFD_FEATURE_RWP ; +if (ioctl (self->uffd,UFFDIO_API , &api) || + !(api.features &UFFD_FEATURE_MOVE )) +SKIP (return,"UFFD_FEATURE_MOVE unsupported\n" ); + rwp = api.features &UFFD_FEATURE_RWP ; + + reg.range.start = (unsigned long )self->aux; + reg.range.len = self->pmd_size; + reg.mode =UFFDIO_REGISTER_MODE_MISSING | + (rwp ?UFFDIO_REGISTER_MODE_RWP :0 ); +ASSERT_EQ (ioctl (self->uffd,UFFDIO_REGISTER , ®),0 ); + + move.dst = (unsigned long )self->aux; + move.src = (unsigned long )self->mem; + move.len = self->pmd_size; +ASSERT_EQ (ioctl (self->uffd,UFFDIO_MOVE , &move),0 ); +ASSERT_EQ (move.move, self->pmd_size); + +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->aux, + self->pmd_size)); +if (rwp) +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->aux, + self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->aux, self->pmd_size, self->seed)); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->aux, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, mremap) +{ +char *new_mem, *dst; + + self->aux =mmap_pmd_aligned (self->pmd_size); +if (self->aux ==MAP_FAILED ) +SKIP (return,"Could not mmap aligned dst\n" ); + dst = self->aux; + + new_mem =mremap (self->mem, self->pmd_size, self->pmd_size, +MREMAP_MAYMOVE |MREMAP_FIXED , dst); +if (new_mem !=MAP_FAILED ) { + self->mem = new_mem; + self->aux =MAP_FAILED ; + } +ASSERT_NE (new_mem,MAP_FAILED ); +ASSERT_EQ (new_mem, dst); + +ASSERT_TRUE (check_swapped (self->pagemap_fd, new_mem, self->pmd_size)); +ASSERT_TRUE (verify_pattern (new_mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, pagemap) +{ +uint64_t entry, first =0 ; +unsigned long off; + +for (off =0 ; off < self->pmd_size; off +=getpagesize ()) { + entry =pagemap_get_entry (self->pagemap_fd, self->mem + off); +ASSERT_TRUE (entry & (1ULL <<62 )); +ASSERT_FALSE (entry & (1ULL <<63 )); + +if (entry &PM_PFRAME_MASK ) { +uint64_t idx = off /getpagesize (); + +if (!off) + first = entry &PM_PFRAME_MASK ; +ASSERT_EQ (entry &PM_PFRAME_MASK , + first + (idx <<MAX_SWAPFILES_SHIFT )); + } + } +} + +TEST_F(pmd_swap, mincore) +{ +unsigned long pages = self->pmd_size /getpagesize (); +unsigned char vec[pages]; + +ASSERT_EQ (mincore (self->mem, self->pmd_size, vec),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +} + +TEST_F(pmd_swap, madvise_free) +{ +unsigned long swap_before =read_vmswap (); +unsigned long i; + +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_GE (swap_before, self->pmd_size); +ASSERT_EQ (madvise (self->mem, self->pmd_size,MADV_FREE ),0 ); +for (i =0 ; i < self->pmd_size; i +=getpagesize ()) +ASSERT_FALSE (pagemap_is_swapped (self->pagemap_fd, + self->mem + i)); +ASSERT_LE (read_vmswap (), swap_before - self->pmd_size); +ASSERT_TRUE (verify_zero (self->mem, self->pmd_size)); +} + +TEST_F(pmd_swap, madvise_willneed) +{ +ASSERT_EQ (madvise (self->mem, self->pmd_size,MADV_WILLNEED ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, swapoff) +{ +int ret, err; + + self->uffd =register_rwp (self->mem, self->pmd_size, true); + + ret =swapoff (self->swap_dev); + err = errno; +if (!ret) + self->swap_disabled = true; + ASSERT_EQ(ret, 0) { +TH_LOG ("swapoff(%s) failed: %s" , self->swap_dev,strerror (err)); + } + +/* +* Check residency before touching the memory. If we read +* first, a bug that left a PMD swap entry in place after swapoff +* would silently trigger do_huge_pmd_swap_page() and reinstall a +* PMD mapping, masking the regression. +*/ +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +if (self->uffd >=0 ) +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->mem, + self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); + + ret =swapon (self->swap_dev,0 ); + err = errno; +if (!ret) + self->swap_disabled = false; + ASSERT_EQ(ret, 0) { +TH_LOG ("swapon(%s) failed: %s" , self->swap_dev,strerror (err)); + } +} + +TEST_HARNESS_MAIN tools/testing/selftests/mm/run_vmtests.sh +2
-3@@ -69,6 +69,8 @@ separated by spaces: test pagemap_scan IOCTL - pfnmap tests for VM_PFNMAP handling +- pmd_swap + tests for PMD-level swap entries - process_madv test for process_madv - cow@@ -399,6 +401,8 @@ CATEGORY="pagemap" run_test ./pagemap_ioctlCATEGORY="pfnmap"
run_test./pfnmaptools/testing/selftests/mm/run_vmtests.sh +2
-0+CATEGORY="pmd_swap" run_test ./pmd_swap +# COW tests
CATEGORY="cow"run_test./cowSignature · 1 line
2.53.0-Meta
Usama Arif
reviewerRe: [PATCH v6 02/12] mm: add PMD swap entry detection support
In reply to David Hildenbrand (Arm)Quoted 21 lines · David Hildenbrand (Arm)
Quoted 14 lines · Usama Arif
Currently when a PMD-mapped THP is swapped out, the PMD is always split into
HPAGE_PMD_NRPTE-level swap entries. To preserve huge page information across swap cycles, later patches will install a single PMD-level swap entry instead. Add the infrastructure to detect those entries.Teach the softleaf layer to recognise PMD swap entries:
pmd_is_swap_entry()detects them andsoftleaf_is_valid_pmd_entry()accepts them as a valid non-present type. Because swap entries do not encode a PFN, makepmd_softleaf_to_folio()warn and return NULL for them instead of passing the swap offset tosoftleaf_to_folio(). Clear the exclusive overlay bit insoftleaf_from_pmd()before decoding, matching howsoft_dirtyand uffd bits are already stripped.Quoted 5 lines
Add
pmd_swp_mkexclusive(),pmd_swp_exclusive(), andpmd_swp_clear_exclusive()helpers to each architecture that supports PMD softleaf entries (x86, arm64, s390, riscv, loongarch, powerpc), mirroring the existing PTE swap exclusive helpers in each arch'spgtable.h.
Quoted 6 lines
Ah, for migration entries we still use a dedicated migratetype. I actually have on my todo list to move to PTE bits as well.
(likely the _swp_ part should then be renamed to indicate that this is for softdirty entries, not just swap entries)
Agreed. I kept the pmd_swp_* spelling to mirror the existing pte_swp_* API. Renaming the broader softleaf API is independent of this series. Maybe I can add it as a followup?
Quoted 9 lines
Quoted 3 lines
Provide generic no-op PMD swap exclusive fallbacks for architectures without PMD softleaf support, matching the generic PMD swap soft-dirty fallbacks.
No softleaf implies to migration and no swap, so this would work.
You should extend
mm/debug_vm_pgtable.cin a separate commit to test whatpte_swap_exclusive_tests()tests for PMDs.
I have added this in my series, and will include it in the next revision, Thanks!
Quoted 10 lines
Quoted 9 lines
Signed-off-by Usama Arif <[email protected]>--- arch/arm64/include/asm/pgtable.h | 6 +++++ arch/loongarch/include/asm/pgtable.h | 19 ++++++++++++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 17 +++++++++++++ arch/riscv/include/asm/pgtable.h | 15 +++++++++++ arch/s390/include/asm/pgtable.h | 17 +++++++++++++ arch/x86/include/asm/pgtable.h | 17 +++++++++++++
Quoted 3 lines
I'm sorry for asking you to compete with Kiryll's series by creating one patch for each architecture that directly jumps at arch maintainers :)
I have now split the architecture helpers into separate patches, followed by generic support for the next revision. I think I will need split patch 7 and patch 12 into several independent patches as well? This will significantly grow the patch count which is what I was trying to avoid as I feel that makes it more daunting to review (atleast for me), but I will try and keep it under 57 patches to not beat Kiryl :)
Quoted 18 lines
[...]
include/linux/leafops.h static inline softleaf_t softleaf_from_pmd(pmd_t pmd) · line 106
Quoted 13 lines
#ifdef
CONFIG_HAVE_ARCH_SOFT_DIRTY
static inlinepte_tpte_swp_mksoft_dirty(pte_t pte){include/linux/leafops.h +2
-0@@ -102,6 +102,8 @@ static inline softleaf_t softleaf_from_pmd(pmd_t pmd) pmd =pmd_swp_clear_soft_dirty (pmd);if (pmd_swp_uffd (pmd)) pmd =pmd_swp_clear_uffd (pmd); +if (pmd_swp_exclusive (pmd)) + pmd =pmd_swp_clear_exclusive (pmd);Can't we just unconditionally clear these flags?
Quoted 5 lines
pmd =
pmd_swp_clear_uffd(pmd);
pmd =pmd_swp_clear_exclusive(pmd);Avoids these rather unnecessary conditionals unless I am missing something.
Yes, we can, will do in next revision.
Quoted 14 lines
include/linux/leafops.h static inline bool pmd_is_migration_entry(pmd_t pmd) · line 639
Quoted 10 lines
include/linux/leafops.h +1
-1arch_entry =__pmd_to_swp_entry (pmd);/* Temporary until swp_entry_t eliminated. */ @@ -634,18 +636,30 @@ static inline bool pmd_is_migration_entry(pmd_t pmd)*/ static inline bool softleaf_is_valid_pmd_entry (softleaf_t entry) { -/* Only device private, migration entries valid for PMD. */ +/* Device private, migration, and swap entries valid for PMD. */ Can we just drop that comment? I mean, it's as clear as it gets in the code immediately below :)
Will do in next revision.
Thanks for the reviews!
Quoted 7 lines
Quoted 6 lines
include/linux/leafops.h +4
-1return softleaf_is_device_private (entry) || -softleaf_is_migration (entry); +softleaf_is_migration (entry) || +softleaf_is_swap (entry); +} +
Usama Arif
reviewerRe: [PATCH v6 02/12] mm: add PMD swap entry detection support
In reply to Lorenzo Stoakes (ARM)Quoted 25 lines · Lorenzo Stoakes (ARM)
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 14 lines · Usama Arif
Currently when a PMD-mapped THP is swapped out, the PMD is always split into
HPAGE_PMD_NRPTE-level swap entries. To preserve huge page information across swap cycles, later patches will install a single PMD-level swap entry instead. Add the infrastructure to detect those entries.Teach the softleaf layer to recognise PMD swap entries:
pmd_is_swap_entry()detects them andsoftleaf_is_valid_pmd_entry()accepts them as a valid non-present type. Because swap entries do not encode a PFN, makepmd_softleaf_to_folio()warn and return NULL for them instead of passing the swap offset tosoftleaf_to_folio(). Clear the exclusive overlay bit insoftleaf_from_pmd()before decoding, matching howsoft_dirtyand uffd bits are already stripped.Quoted 8 lines
Quoted 5 lines
Add
pmd_swp_mkexclusive(),pmd_swp_exclusive(), andpmd_swp_clear_exclusive()helpers to each architecture that supports PMD softleaf entries (x86, arm64, s390, riscv, loongarch, powerpc), mirroring the existing PTE swap exclusive helpers in each arch'spgtable.h.Ah, for migration entries we still use a dedicated migratetype. I actually have on my todo list to move to PTE bits as well.
Quoted 9 lines
We do seem to have rather hacked in PMD level stuff across the board previously :)
Quoted 3 lines
(likely the _swp_ part should then be renamed to indicate that this is for softdirty entries, not just swap entries)
You mean softleaf?
Yes, I read that as softleaf as well.>>
Quoted 9 lines
Quoted 8 lines
Quoted 3 lines
Provide generic no-op PMD swap exclusive fallbacks for architectures without PMD softleaf support, matching the generic PMD swap soft-dirty fallbacks.
No softleaf implies to migration and no swap, so this would work.
You should extend
mm/debug_vm_pgtable.cin a separate commit to test whatpte_swap_exclusive_tests()tests for PMDs.
Quoted 7 lines
BTW one thing I should maybe mention here is that I never quite _finished_ the softleaf thing, so there's some awkward overlap between swap stuff and softleaf stuff (swap entries _are_ softleaf entries, just a specific kind).
Infinite TODO list etc. (maybe this is a hint/nudge to somebody else to do it ;)
I kind of tried to tackle some this along the way, with the prep series in
[1]
[1] https://lore.kernel.org/all/[email protected]/#t
Quoted 65 lines
Quoted 61 lines
Quoted 10 lines
Signed-off-by Usama Arif <[email protected]>--- arch/arm64/include/asm/pgtable.h | 6 +++++ arch/loongarch/include/asm/pgtable.h | 19 ++++++++++++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 17 +++++++++++++ arch/riscv/include/asm/pgtable.h | 15 +++++++++++ arch/s390/include/asm/pgtable.h | 17 +++++++++++++ arch/x86/include/asm/pgtable.h | 17 +++++++++++++I'm sorry for asking you to compete with Kiryll's series by creating one patch for each architecture that directly jumps at arch maintainers :)
[...]
include/linux/leafops.h static inline softleaf_t softleaf_from_pmd(pmd_t pmd) · line 106
Quoted 13 lines
#ifdef
CONFIG_HAVE_ARCH_SOFT_DIRTY
static inlinepte_tpte_swp_mksoft_dirty(pte_t pte){include/linux/leafops.h +2
-0@@ -102,6 +102,8 @@ static inline softleaf_t softleaf_from_pmd(pmd_t pmd) pmd =pmd_swp_clear_soft_dirty (pmd);if (pmd_swp_uffd (pmd)) pmd =pmd_swp_clear_uffd (pmd); +if (pmd_swp_exclusive (pmd)) + pmd =pmd_swp_clear_exclusive (pmd);Can't we just unconditionally clear these flags?
pmd =
pmd_swp_clear_uffd(pmd);
pmd =pmd_swp_clear_exclusive(pmd);Avoids these rather unnecessary conditionals unless I am missing something.
include/linux/leafops.h static inline bool pmd_is_migration_entry(pmd_t pmd) · line 639
Quoted 9 lines
include/linux/leafops.h +1
-1arch_entry =__pmd_to_swp_entry (pmd);/* Temporary until swp_entry_t eliminated. */ @@ -634,18 +636,30 @@ static inline bool pmd_is_migration_entry(pmd_t pmd)*/ static inline bool softleaf_is_valid_pmd_entry (softleaf_t entry) { -/* Only device private, migration entries valid for PMD. */ +/* Device private, migration, and swap entries valid for PMD. */ Can we just drop that comment? I mean, it's as clear as it gets in the code immediately below :)
include/linux/leafops.h static inline bool pmd_is_migration_entry(pmd_t pmd) · line 644
Quoted 6 lines
include/linux/leafops.h +4
-1return softleaf_is_device_private (entry) || -softleaf_is_migration (entry); +softleaf_is_migration (entry) || +softleaf_is_swap (entry); +} +--
Cheers,Quoted 1 line
David
--
Cheers, Lorenzo
Usama Arif
reviewerRe: [PATCH v6 03/12] mm: add PMD swap entry splitting support
In reply to David Hildenbrand (Arm)Quoted 44 lines · David Hildenbrand (Arm)
Quoted 33 lines · Usama Arif
Add a swap branch in
__split_huge_pmd_locked()that splits a PMD swap entry into 512 PTE swap entries. No folio reference is needed because swap entries point to swap slots rather than pages. Each PTE inherits the correct sub-slot offset and preservessoft_dirty,uffd_wp, and exclusive flags.The
folio_remove_rmap_pmd()gate at the end must inspectold_pmdrather than *pmd: for a present THP split, *pmd has already been cleared bypmdp_invalidate(), and that invalidated bit pattern can decode as a plausible swap entry.This branch is reached from the explicit
__split_huge_pmd()callers that hit a non-present PMD: partial-range mprotect / munmap, thewp_huge_pmd()PMD-COW fallback, and the swap-in / swapoff fallbacks added in later patches when the cached folio is no longer PMD-sized.page_vma_mapped_walk()does not iterate PMD swap entries, sotry_to_unmap_one()andtry_to_migrate_one()do not reach this branch and freeze=true cannot occur in this branch today. page and folio are therefore left uninitialized in the swap branch; aVM_WARN_ON_ONCE(freeze)catches any future caller that breaks this invariant before the freeze path dereferencespage_to_pfn(page + i)orput_page(page).Signed-off-by Usama Arif <[email protected]>--- mm/huge_memory.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)mm/huge_memory.c +0
-0mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3258
Quoted 7 lines
mm/huge_memory.c +4
-0@@ -3252,6 +3252,14 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , vma, haddr, rmap_flags); } + }else if (pmd_is_swap_entry (*pmd)) { +VM_WARN_ON_ONCE (freeze); +/* Swap entries have no page for the migration freeze path. */ + freeze = false;It's odd to
VM_WARN_ON_ONCE()and then set freeze=false;
Quoted 5 lines
I'd just add the comment above the
VM_WARN_ON_ONCE()and drop the =false.freeze=true really only applies during page migration, where swap entries don't apply.
Ack
Quoted 11 lines
I think it's time to clean that up ... that is
a) Expose a helper called
split_pmd_to_migration_entries()that is only used by code that installs migration entries.b) Hide that "freeze" flag from all other file-external functions
c) Rename the boolean to "
use_migration_entries"Then it's rather clear in this code that this should never happen.
Thanks! I have done this now for next revision. The migration choice is now private as use_migration_entries, the normal split APIs no longer expose it, and the rmap migration path uses split_pmd_to_migration_entries().
m
Quoted 19 lines
Quoted 10 lines
mm/huge_memory.c +4
-0+ old_pmd = *pmd; + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd (old_pmd); + anon_exclusive =pmd_swp_exclusive (old_pmd); }else {/* * Up to this point the pmd is present and huge and userland has mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3400
Quoted 5 lines
mm/huge_memory.c +2
-0@@ -3388,6 +3396,25 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,VM_WARN_ON (!pte_none (ptep_get (pte + i)));set_pte_at (mm, addr, pte + i, entry); } + }else if (pmd_is_swap_entry (old_pmd)) { +softleaf_t sl_entry =softleaf_from_pmd (old_pmd);No existing code uses "
sl_entry". Maybe just call it "pmd_swp_entry"/"swp_entry" and below "pte_swp_entry".
Thanks! Will do in next revision
Quoted 4 lines
Apart from that nothing jumped at me :)
Thanks for the review!
Usama Arif
reviewerRe: [PATCH v6 05/12] mm: zswap: add range lookup for large-folio swapin
In reply to Yosry AhmedQuoted 99 lines · Yosry Ahmed
Quoted 70 lines · Usama Arif
From: Alexandre Ghiti <[email protected]>
A large folio reaches
zswap_load()only when the caller expects the whole range to be on disk. Zswap still stores large folios as independent order-0 entries, so reconstructing a large folio from zswap entries would risk returning partially initialized data.Teach
zswap_load()to scan the covered range. If no slot is in zswap, return-ENOENTsoswap_read_folio()reads the backing device. If any slot is still in zswap, fail the large-folio read so the caller can fall back to per-page swapin.Return
-EIOrather than-EINVALfor that conflict. Large-folio loads are now valid requests; the error means zswap cannot safely satisfy the request from partial per-page compressed state, not that the request is unsupported. Existing callers only distinguish-ENOENT, so this is a semantic clarification rather than a behavioral change.Add
zswap_is_present()so PMD swap-entry consumers can make the same range decision before attempting PMD-order swapin. Also use it from__swap_cache_add_check()for multi-page insertions while holding the swap cluster lock. That check runs before folio allocation and again immediately before swap-cache insertion, closing the race with zswap writeback and rejecting mixed zswap/disk backing with-EBUSY.Signed-off-by Alexandre Ghiti <[email protected]>Signed-off-by Usama Arif <[email protected]>--- include/linux/zswap.h | 6 ++++++ mm/swap_state.c | 10 ++++++++++ mm/zswap.c | 46 +++++++++++++++++++++++++++++++------------ 3 files changed, 49 insertions(+), 13 deletions(-)include/linux/zswap.h +6
-0@@ -35,6 +35,7 @@ void zswap_lruvec_state_init(struct lruvec *lruvec);void zswap_folio_swapin (struct folio *folio);bool zswap_is_enabled (void );bool zswap_never_enabled (void ); +bool zswap_is_present (swp_entry_t entry,unsigned int nr);#else struct zswap_lruvec_state {};@@ -69,6 +70,11 @@ static inline bool zswap_never_enabled(void)return true; } +static inline bool zswap_is_present (swp_entry_t entry,unsigned int nr) +{ +return false; +} +#endif #endif /* _LINUX_ZSWAP_H */ mm/swap_state.c +1
-0@@ -12,6 +12,7 @@#include <linux/kernel_stat.h> #include <linux/mempolicy.h> #include <linux/swap.h> +#include <linux/zswap.h> #include <linux/leafops.h> #include <linux/init.h> #include <linux/pagemap.h> mm/swap_state.c static int __swap_cache_add_check(struct swap_cluster_info *ci, · line 201
Quoted 10 lines
mm/swap_state.c +7
-0@@ -191,6 +192,15 @@ static int __swap_cache_add_check(struct swap_cluster_info *ci,if (nr ==1 )return 0 ; +/* +* The cluster lock serializes swap-cache insertion with zswap +* writeback. Reject mixed zswap/disk backing before allocating a +* large folio and recheck it before adding the folio to swap cache. +*/ +if (zswap_is_present (swp_entry (swp_type (targ_entry), +round_down (swp_offset (targ_entry), nr)), nr))Sorry I didn't catch it in the discussion in v5, but I think it's actually clearer to do have this check in
__swap_cache_alloc()as you initially suggested, but not necessarily under the lock.I don't think the cluster lock is relevant per se, but rather the actual swap cache allocation. Once you allocate the folio in the swapcache, you cannot race with zswap store or writeback. I think the comment here is a bit misleading in that regard. Especially that it mentions writeback, but I think the real risk is racing with zswap store?
The check here is performed twice, once before allocating the folio and once after. I think the one before allocating the folio is not really useful, as we don't actually allocate the swapcache entry so nothing actually prevents a zswap store/writeback from happening right after releasing the lock.
Quoted 3 lines
We already have a failure path in
__swap_cache_alloc()after allocating the folio and dropping the lock. Can we use the same path to check if zswap is present?
Yes, I think the above makes sense. I am looking at the code and I think we can check the range after __swap_cache_do_add_folio() and use the existing allocation rollback path if the folio exists in zswap.
The code will look slightly different from the diff below, but will achieve the same thing.
Thanks for the review!
Quoted 115 lines
Maybe something along these lines (completely untested):
mm/swap_state.c +11
-1@@ -415,6 +415,7 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,swp_entry_t entry;struct folio *folio;void *shadow =NULL ; +bool large_in_zswap;unsigned short memcg_id;unsigned long address, nr_pages =1UL << order;struct vm_area_struct *vma = vmf ? vmf->vma :NULL ;@@ -459,7 +460,16 @@ static struct folio *__swap_cache_alloc(struct swap_cluster_info *ci,__swap_cache_do_add_folio (ci, folio, entry);spin_unlock (&ci->lock); -if (mem_cgroup_swapin_charge_folio (folio, memcg_id, +/* +* Check if any part of the folio is in zswap after stabilizing the swap +* cache entry to avoid races with zswap store/writeback. Reject +* high-order entries that are partially or fully in zswap, as it is not +* supported. +*/ + large_in_zswap = order &&zswap_is_present (entry, nr_pages); + +if (large_in_zswap || +mem_cgroup_swapin_charge_folio (folio, memcg_id, vmf ? vmf->vma->vm_mm :NULL , gfp)) {spin_lock (&ci->lock);__swap_cache_do_del_folio (ci, folio, entry, shadow);Quoted 82 lines
mm/swap_state.c +2
-0+return -EBUSY ; + is_zero =__swap_table_test_zero (ci, ci_off); ci_off =round_down (ci_off, nr); ci_end = ci_off + nr;mm/zswap.c +33
-13@@ -1571,6 +1571,23 @@ bool zswap_store(struct folio *folio)return ret; } +/** +* zswap_is_present() - is any slot in [entry, entry + nr) in zswap? +* @entry: base swap entry of the range +* @nr: number of contiguous slots to check (pass 1 for a single-slot query) +*/ +bool zswap_is_present (swp_entry_t entry,unsigned int nr) +{ +pgoff_t offset =swp_offset (entry); +struct xarray *tree =swap_zswap_tree (entry); +unsigned long index = offset; + +if (!nr ||zswap_never_enabled ()) +return false; + +return xa_find (tree, &index, offset + nr -1 ,XA_PRESENT ); +} +/** * zswap_load() - load a folio from zswap * @folio: folio to load @@ -1578,13 +1595,9 @@ bool zswap_store(struct folio *folio)* Return: 0 on success, with the folio unlocked and marked up-to-date, or one * of the following error codes: * -* -EIO: if the swapped out content was in zswap, but could not be loaded -* into the page due to a decompression failure. The folio is unlocked, but -* NOT marked up-to-date, so that an IO error is emitted (e.g. do_swap_page() -* will SIGBUS). -* -* -EINVAL: if the swapped out content was in zswap, but the page belongs -* to a large folio, which is not supported by zswap. The folio is unlocked, +* -EIO: if the swapped out content was in zswap but could not be handed +* back, either because decompression failed or because a slot in a +* large-folio range is unexpectedly still in zswap. The folio is unlocked, * but NOT marked up-to-date, so that an IO error is emitted (e.g. * do_swap_page() will SIGBUS). * @@ -1605,13 +1618,20 @@ int zswap_load(struct folio *folio)return -ENOENT ;/* -* Large folios should not be swapped in while zswap is being used, as -* they are not properly handled. Zswap does not properly load large -* folios, and a large folio may only be partially in zswap. +* A large folio reaches zswap_load() only when its whole range is +* expected to be on disk: PMD swap-entry consumers split before +* calling into PMD-order swapin whenever any slot is still in zswap. +* Confirm the range is entirely absent from zswap and return -ENOENT +* so the caller reads it from disk; if a slot is unexpectedly still in +* zswap, fail the read rather than return partially-initialized data. */ -if (WARN_ON_ONCE (folio_test_large (folio))) { -folio_unlock (folio); -return -EINVAL ; +if (folio_test_large (folio)) { +if (WARN_ON_ONCE (zswap_is_present (swp, +folio_nr_pages (folio)))) { +folio_unlock (folio); +return -EIO ; + } +return -ENOENT ; } entry =xa_load (tree, offset);Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerRe: [PATCH v6 06/12] mm: swap in PMD swap entries as whole THPs during swapoff
In reply to Lance YangQuoted 49 lines · Lance Yang
+Cc Miaohe
On Tue, Aug 18, 2026 at 06:09:47AM -0700, Usama Arif wrote:
[...]Quoted 27 lines
mm/swapfile.c +27
-0+#ifdef CONFIG_THP_SWAP +/* +* unuse_pmd - Map a locked folio at PMD granularity during swapoff. +* +* The caller provides a locked, swapped-in folio. Returns 0 on success +* (PMD was mapped). Returns -EAGAIN if the swap cache folio no longer +* matches the entry or the PMD changed under the lock (try_to_unuse will +* rescan). Returns -EIO if the folio is not uptodate or contains a poisoned +* subpage; in that case the PMD is split so unuse_pte_range() can handle +* individual pages. +*/ +static int unuse_pmd (struct vm_area_struct *vma,pmd_t *pmd, +unsigned long addr,softleaf_t entry, +struct folio *folio) +{ +struct mm_struct *mm = vma->vm_mm; +struct page *page; +pmd_t new_pmd, old_pmd; +spinlock_t *ptl; +rmap_t rmap_flags =RMAP_NONE ; +bool exclusive; + +if (unlikely (!folio_matches_swap_entry (folio, entry))) +return -EAGAIN ; + +if (unlikely (!folio_test_uptodate (folio))) { +/* Let PTE fallback reread each slot independently. */ mm/swapfile.c static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 2710
Quoted 10 lines
mm/swapfile.c +10
-0+swap_cache_del_folio (folio); +__split_huge_pmd (vma, pmd, addr, false); +return -EIO ; + } + +if (unlikely (folio_contain_hwpoisoned_page (folio))) { +/* Let PTE fallback isolate the poisoned subpages. */ +__split_huge_pmd (vma, pmd, addr, false); +return -EIO ; + }Hmm ... can this miss a poisoned tail?
memory_failure()sets PageHWPoison(p) before takingfolio_lock(), butPG_has_hwpoisonedis only set later. Sinceunuse_pmd_entry()holds folio lock across this check,memory_failure()can be blocked onfolio_lock()with a tail already poisoned, IIUC ...
Quoted 5 lines
folio_contain_hwpoisoned_page()then sees neither a poisoned head norPG_has_hwpoisoned, andset_pmd_at()maps that tail through a normal PMD.Note that
unuse_pte()checks PageHWPoison(page) directly. Should we check each subpage here as well before installing the PMD?
Good catch!
I am going to introduce a helper like below and use it in the next revision. Thanks!
static inline bool folio_has_hwpoisoned_subpage(struct folio *folio)
{
unsigned long i;
for (i = 0; i < folio_nr_pages(folio); i++)
if (PageHWPoison(folio_page(folio, i)))
return true;
return false;
}Quoted 55 lines
Quoted 41 lines
mm/swapfile.c +40
-0+ + page =folio_page (folio,0 ); + + ptl =pmd_lock (mm, pmd); + old_pmd =pmdp_get (pmd); + +if (!pmd_is_swap_entry (old_pmd) || +softleaf_from_pmd (old_pmd).val != entry.val) { +spin_unlock (ptl); +return -EAGAIN ; + } + + exclusive =pmd_swp_exclusive (old_pmd); + +/* +* Some architectures may have to restore extra metadata to the folio +* when reading from swap. This metadata may be indexed by swap entry +* so this must be called before folio_put_swap(). +*/ +arch_swap_restore (folio_swap (entry, folio), folio); + +add_mm_counter (mm,MM_ANONPAGES ,HPAGE_PMD_NR ); +add_mm_counter (mm,MM_SWAPENTS , -HPAGE_PMD_NR ); + + new_pmd =folio_mk_pmd (folio, vma->vm_page_prot); + new_pmd =pmd_mkold (new_pmd); +if (pmd_swp_soft_dirty (old_pmd)) + new_pmd =pmd_mksoft_dirty (new_pmd); +if (pmd_swp_uffd (old_pmd)) + new_pmd =pmd_mkuffd (new_pmd); +if (pmd_swp_uffd (old_pmd) &&userfaultfd_rwp (vma)) + new_pmd =pmd_modify (new_pmd,PAGE_NONE ); + +if (exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_get (folio); +if (!folio_test_anon (folio)) +folio_add_new_anon_rmap (folio, vma, addr, rmap_flags); +else mm/swapfile.c static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 2760
Quoted 10 lines
mm/swapfile.c +10
-0+folio_add_anon_rmap_pmd (folio, page, vma, addr, rmap_flags); + +set_pmd_at (mm, addr, pmd, new_pmd); +folio_put_swap (folio,NULL ); + +spin_unlock (ptl); + +folio_free_swap (folio); +return 0 ; +}[...]
Cheers, Lance
Usama Arif
reviewerRe: [PATCH v6 12/12] selftests/mm: add PMD swap entry tests
In reply to Lance YangQuoted 29 lines · Lance Yang
Quoted 23 lines · Usama Arif
Exercise the PMD swap entry paths. Each test gets a fresh PMD-mapped THP from fixture setup, fills it with a page-distinct pattern, swaps it out with
MADV_PAGEOUT, and verifies thatthp_swpout_pmdincreased.The tests are:
- basic: fault in a swapped PMD and verify its contents.
- fork: verify parent and child can fault in the shared swap entry.
-fork_cow: verify parent and child writes remain isolated.
- write: fault in by writing one byte and preserve the rest of the THP.
-rwp_swapin: verify userfaultfd RWP survives PMD-order swap-in.
- munmap: unmap the full entry and check that VmSwap drops.
- mprotect: change full-range protections without faulting the entry in.
-split_mprotect: change half-range protections and verify the data.
-split_munmap: unmap half, drop its accounting, and preserve the rest.
-uffdio_move: move the entry and RWP state, then fault it in at dst.
- mremap: force the entry to a new aligned address and verify the data.
- pagemap: verify swapped bits and consecutive swap-slot offsets.
- mincore: walk the entry without faulting it in.
-madvise_free: release the slots, clear the entry, and verify zeroes.
-madvise_willneed: prefetch the entry and verify subsequent swap-in.
- swapoff: unuse the entry and preserve data and PMD/RWP state.Quoted 3 lines
Fixture teardown owns the mappings and file descriptors and restores swap after assertion failures.
PMD_SWAP_DEVICEremains optional for swapoff.
Quoted 6 lines
How do we know the PMD swap entry is on
PMD_SWAP_DEVICE?Quoted 1 line
Assume another higher-priority swap device is active.
MADV_PAGEOUTmayplace the entry there, so
swapoff()operates on the wrong device. This can make the test pass with zswap enabled and fail with it disabled.Should
PMD_SWAP_DEVICEbe required to be the only active swap device?
Ah yeah, you are right. I dont like it as it will likely mean that the swapoff test wont be exercised always, but I will change the swapoff test so that it now runs only when PMD_SWAP_DEVICE identifies the sole active swap device, ensuring pageout and swapoff target the same backend.
Quoted 826 lines
Cheers, Lance
Quoted 823 lines
Distinguish an environment that cannot allocate a PMD THP from a failure to install a PMD swap entry, so the former skips while the latter fails. Also check VmSwap accounting, pagemap slot offsets, swapped state after non-faulting operations, and PMD restoration when zswap does not require PTE fallback.
Register the test with
run_vmtests.shand the default kselftest runner.Signed-off-by Usama Arif <[email protected]>---
tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/ksft_pmd_swap.sh | 4 + tools/testing/selftests/mm/pmd_swap.c | 742 ++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 752 insertions(+)create mode 100755
tools/testing/selftests/mm/ksft_pmd_swap.shcreate mode 100644tools/testing/selftests/mm/pmd_swap.ctools/testing/selftests/mm/Makefile +0
-0@@ -104,6 +104,7 @@ TEST_GEN_FILES += guard-regions
TEST_GEN_FILES+= mergeTEST_GEN_FILES+= rmapTEST_GEN_FILES+=folio_split_race_test
+TEST_GEN_FILES +=pmd_swapifneq ($(ARCH),arm64)
TEST_GEN_FILES+= soft-dirtytools/testing/selftests/mm/Makefile +0
-0@@ -165,6 +166,7 @@ TEST_PROGS += ksft_mremap.sh
TEST_PROGS+=ksft_pagemap.shTEST_PROGS+=ksft_pfnmap.shTEST_PROGS+=ksft_pkey.sh
+TEST_PROGS +=ksft_pmd_swap.shTEST_PROGS+=ksft_process_madv.shTEST_PROGS+=ksft_process_mrelease.shTEST_PROGS+=ksft_rmap.shtools/testing/selftests/mm/ksft_pmd_swap.sh new file +4
-0@@ -0,0 +1,4 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0 + +./run_vmtests.sh -t pmd_swaptools/testing/selftests/mm/pmd_swap.c new file +742
-0@@ -0,0 +1,742 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Test PMD-level swap entries and their users. */ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <errno.h> +#include <stdint.h> +#include <sys/random.h> +#include <sys/swap.h> +#include <sys/syscall.h> +#include <sys/ioctl.h> +#include <poll.h> +#include <pthread.h> +#include <linux/userfaultfd.h> +#include <time.h> + +#include "kselftest_harness.h" +#include "vm_util.h" + +#define ZSWAP_ENABLED_PATH "/sys/module/zswap/parameters/enabled" + +/* pagemap: bits 0-54 hold the PFN, or type|offset for a swap entry. */ +#define PM_PFRAME_MASK ((1ULL << 55) - 1) +/* Must match MAX_SWAPFILES_SHIFT in include/linux/swap.h. */ +#define MAX_SWAPFILES_SHIFT 5 + +static bool check_swapped (int pagemap_fd,char *addr,unsigned long size) +{ +unsigned long off; + +for (off =0 ; off < size; off +=getpagesize ()) +if (!pagemap_is_swapped (pagemap_fd, addr + off)) +return false; +return true; +} + +static bool zswap_enabled (void ) +{ +char enabled =0 ; +FILE *f; + + f =fopen (ZSWAP_ENABLED_PATH ,"r" ); +if (!f) +return false; + +if (fscanf (f," %c" , &enabled) !=1 ) + enabled =0 ; +fclose (f); + +return enabled =='Y' || enabled =='y' || enabled =='1' ; +} + +static bool swap_available (unsigned long required_bytes) +{ +unsigned long required_kb = (required_bytes +1023 ) /1024 ; +unsigned long size_kb, used_kb; +char line[256 ]; +bool ret = false; +FILE *f; + + f =fopen ("/proc/swaps" ,"r" ); +if (!f) +return false; + +/* Skip the header. */ +if (!fgets (line,sizeof (line), f)) + goto out; + +while (fgets (line,sizeof (line), f)) { +if (sscanf (line,"%*s %*s %lu %lu" , &size_kb, &used_kb) ==2 && + size_kb >= used_kb && size_kb - used_kb >= required_kb) { + ret = true; +break ; + } + } + +out: +fclose (f); +return ret; +} + +static unsigned long read_vm_event (const char *name) +{ +char line[256 ]; +size_t name_len =strlen (name); +unsigned long val =0 ; +FILE *f; + + f =fopen ("/proc/vmstat" ,"r" ); +if (!f) +return 0 ; +while (fgets (line,sizeof (line), f)) { +if (!strncmp (line, name, name_len) && line[name_len] ==' ' ) { + val =strtoul (line + name_len +1 ,NULL ,10 ); +break ; + } + } +fclose (f); +return val; +} + +static unsigned int random_seed (void ) +{ +unsigned int seed; + +if (getrandom (&seed,sizeof (seed),0 ) !=sizeof (seed)) + seed = (unsigned int )time (NULL ); +return seed; +} + +static unsigned char pattern_byte (unsigned int seed,unsigned long off) +{ +return (unsigned char )(seed + off + (off >>8 ) + (off >>16 )); +} + +static void fill_pattern (char *buf,unsigned long size,unsigned int seed) +{ +unsigned long i; + +for (i =0 ; i < size; i++) + buf[i] = (char )pattern_byte (seed, i); +} + +static bool verify_pattern_range (char *buf,unsigned long size, +unsigned int seed,unsigned long offset) +{ +unsigned long i; + +for (i =0 ; i < size; i++) +if ((unsigned char )buf[i] !=pattern_byte (seed, offset + i)) +return false; +return true; +} + +static bool verify_pattern (char *buf,unsigned long size,unsigned int seed) +{ +return verify_pattern_range (buf, size, seed,0 ); +} + +static bool verify_zero (char *buf,unsigned long size) +{ +unsigned long i; + +for (i =0 ; i < size; i++) +if (buf[i]) +return false; +return true; +} + +/* +* mmap an anonymous PMD-aligned region of pmd_size bytes. Over-allocates +* by one PMD and trims the unaligned head/tail so the returned address is +* PMD-aligned (required for whole-PMD UFFDIO_MOVE). +*/ +static char *mmap_pmd_aligned (unsigned long pmd_size) +{ +unsigned long pad = pmd_size; +char *raw, *aligned; + + raw =mmap (NULL , pmd_size + pad,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (raw ==MAP_FAILED ) +return MAP_FAILED ; + + aligned = (char *)(((uintptr_t )raw + pmd_size -1 ) & ~(pmd_size -1 )); +if (aligned != raw) +munmap (raw, aligned - raw); +if (aligned + pmd_size != raw + pmd_size + pad) +munmap (aligned + pmd_size, + (raw + pmd_size + pad) - (aligned + pmd_size)); +return aligned; +} + +enum swap_thp_result { +SWAP_THP_OK , +SWAP_THP_UNAVAILABLE , +SWAP_THP_FAILED , +}; + +/* Per-process swapped size in bytes, from /proc/self/status VmSwap. */ +static unsigned long read_vmswap (void ) +{ +char line[256 ]; +unsigned long kb =0 ; +FILE *f; + + f =fopen ("/proc/self/status" ,"r" ); +if (!f) +return 0 ; +while (fgets (line,sizeof (line), f)) { +if (!strncmp (line,"VmSwap:" ,7 )) { + kb =strtoul (line +7 ,NULL ,10 ); +break ; + } + } +fclose (f); +return kb *1024 ; +} + +static bool swap_out_pmd (char *mem,unsigned long pmd_size,int pagemap_fd) +{ +unsigned long before =read_vm_event ("thp_swpout_pmd" ); +unsigned long after; + +if (madvise (mem, pmd_size,MADV_PAGEOUT )) { +ksft_print_msg ("MADV_PAGEOUT failed: %s\n" ,strerror (errno)); +return false; + } +if (!check_swapped (pagemap_fd, mem, pmd_size)) { +ksft_print_msg ("MADV_PAGEOUT did not swap the whole PMD range\n" ); +return false; + } + + after =read_vm_event ("thp_swpout_pmd" ); +ksft_print_msg ("thp_swpout_pmd: %lu -> %lu\n" , before, after); +return after > before; +} + +static char *alloc_fill_swap_thp (unsigned long pmd_size,int pagemap_fd, +unsigned int seed,enum swap_thp_result *res) +{ +char *mem; + + *res =SWAP_THP_UNAVAILABLE ; + + mem =mmap_pmd_aligned (pmd_size); +if (mem ==MAP_FAILED ) +return MAP_FAILED ; + +if (madvise (mem, pmd_size,MADV_HUGEPAGE )) { +ksft_print_msg ("MADV_HUGEPAGE failed: %s\n" ,strerror (errno)); +munmap (mem, pmd_size); +return MAP_FAILED ; + } +fill_pattern (mem, pmd_size, seed); + +if (!check_huge_anon (mem, pmd_size,1 , pmd_size)) { +munmap (mem, pmd_size); +return MAP_FAILED ; + } + *res =SWAP_THP_FAILED ; + +if (!swap_out_pmd (mem, pmd_size, pagemap_fd)) { +munmap (mem, pmd_size); +return MAP_FAILED ; + } + + *res =SWAP_THP_OK ; +return mem; +} + +struct rwp_access_args { +unsigned char *addr; +unsigned char expected; +bool write; +bool ok; +}; + +static void *rwp_access_thread (void *data) +{ +struct rwp_access_args *args = data; + +if (args->write) + *args->addr = args->expected; + args->ok = *args->addr == args->expected; +return NULL ; +} + +static int register_rwp (char *addr,unsigned long size,bool protect) +{ +struct uffdio_register reg = {}; +struct uffdio_rwprotect rwp = {}; +struct uffdio_api api = {}; +int uffd; + + uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (uffd <0 ) +return -1 ; + + api.api =UFFD_API ; + api.features =UFFD_FEATURE_RWP ; +if (ioctl (uffd,UFFDIO_API , &api) || + !(api.features &UFFD_FEATURE_RWP )) + goto error; + + reg.range.start = (unsigned long )addr; + reg.range.len = size; + reg.mode =UFFDIO_REGISTER_MODE_RWP ; +if (ioctl (uffd,UFFDIO_REGISTER , ®)) + goto error; + +if (!protect) +return uffd; + + rwp.range.start = (unsigned long )addr; + rwp.range.len = size; + rwp.mode =UFFDIO_RWPROTECT_MODE_RWP ; +if (!ioctl (uffd,UFFDIO_RWPROTECT , &rwp)) +return uffd; + +error: +close (uffd); +return -1 ; +} + +static bool expect_rwp_fault (int uffd,char *addr,unsigned long size, +unsigned char expected,bool write) +{ +struct rwp_access_args args = { + .addr = (unsigned char *)addr, + .expected = expected, + .write = write, + }; +struct uffdio_rwprotect rwp = { + .range = { + .start = (unsigned long )addr, + .len = size, + }, + }; +struct pollfd pollfd = { + .fd = uffd, + .events =POLLIN , + }; +struct uffd_msg msg = {}; +pthread_t thread; +bool saw_rwp = false; +int ret; + +if (pthread_create (&thread,NULL , rwp_access_thread, &args)) +return false; + + ret =poll (&pollfd,1 ,5000 ); +if (ret ==1 && (pollfd.revents &POLLIN ) && +read (uffd, &msg,sizeof (msg)) == (ssize_t )sizeof (msg)) { + saw_rwp = msg.event ==UFFD_EVENT_PAGEFAULT && + (msg.arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_RWP ); + } + +/* Resolve the access even on failure so the worker cannot remain blocked. */ +ioctl (uffd,UFFDIO_RWPROTECT , &rwp); +if (pthread_join (thread,NULL )) +return false; +return saw_rwp && args.ok; +} + +FIXTURE (pmd_swap) +{ +unsigned long pmd_size; +unsigned long mem_len; +int pagemap_fd; +int uffd; +unsigned int seed; +bool zswap_enabled; +bool swap_disabled; +const char *swap_dev; +char *mem; +char *aux; +}; + +FIXTURE_SETUP (pmd_swap) +{ +enum swap_thp_result res; + + self->pagemap_fd =-1 ; + self->uffd =-1 ; + self->mem =MAP_FAILED ; + self->aux =MAP_FAILED ; + self->mem_len =0 ; + self->swap_disabled = false; + self->swap_dev =getenv ("PMD_SWAP_DEVICE" ); +if (!strcmp (_metadata->name,"swapoff" ) && !self->swap_dev) +SKIP (return,"PMD_SWAP_DEVICE env var not set\n" ); + + self->pmd_size =read_pmd_pagesize (); +if (!self->pmd_size) +SKIP (return,"Cannot determine PMD size\n" ); + + self->pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY ); +if (self->pagemap_fd <0 ) +SKIP (return,"Cannot open /proc/self/pagemap\n" ); + +if (!swap_available (self->pmd_size)) +SKIP (return,"No active swap device has enough free space\n" ); + + self->seed =random_seed (); + self->zswap_enabled =zswap_enabled (); + self->mem =alloc_fill_swap_thp (self->pmd_size, self->pagemap_fd, + self->seed, &res); +if (self->mem ==MAP_FAILED ) { +ASSERT_NE (res,SWAP_THP_FAILED ); +SKIP (return,"Could not create swapped THP\n" ); + } + self->mem_len = self->pmd_size; +} + +FIXTURE_TEARDOWN (pmd_swap) +{ +int swap_err =0 ; +int swap_ret =0 ; + +if (self->swap_disabled) { + swap_ret =swapon (self->swap_dev,0 ); + swap_err = errno; + } +if (self->uffd >=0 ) +close (self->uffd); +if (self->aux !=MAP_FAILED ) +munmap (self->aux, self->pmd_size); +if (self->mem !=MAP_FAILED ) +munmap (self->mem, self->mem_len); +if (self->pagemap_fd >=0 ) +close (self->pagemap_fd); + + EXPECT_EQ(swap_ret, 0) { +TH_LOG ("swapon(%s) failed: %s" , self->swap_dev, +strerror (swap_err)); + } +} + +TEST_F(pmd_swap, basic) +{ +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, fork) +{ +pid_t pid; +int status; + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) +_exit (verify_pattern (self->mem, self->pmd_size, + self->seed) ?0 :1 ); + +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); + +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); +} + +TEST_F(pmd_swap, fork_cow) +{ +unsigned int parent_seed = self->seed; +unsigned int child_seed = ~self->seed; +unsigned int new_seed = self->seed ^0xa5a5a5a5 ; +int release_child[2 ]; +bool parent_ok; +char c =0 ; +pid_t pid; +int status, ret; + +ASSERT_EQ (pipe (release_child),0 ); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +close (release_child[1 ]); +if (read (release_child[0 ], &c,1 ) !=1 ) +_exit (1 ); +if (!verify_pattern (self->mem, self->pmd_size, parent_seed)) +_exit (2 ); +fill_pattern (self->mem, self->pmd_size, child_seed); +if (!verify_pattern (self->mem, self->pmd_size, child_seed)) +_exit (3 ); +_exit (0 ); + } + +close (release_child[0 ]); +fill_pattern (self->mem, self->pmd_size, new_seed); + parent_ok =verify_pattern (self->mem, self->pmd_size, new_seed); + ret =write (release_child[1 ], &c,1 ); +close (release_child[1 ]); +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_EQ (ret,1 ); +ASSERT_TRUE (parent_ok); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, new_seed)); +} + +TEST_F(pmd_swap, write) +{ + self->mem[0 ] =0xbb ; +ASSERT_EQ (self->mem[0 ], (char )0xbb ); +ASSERT_TRUE (verify_pattern_range (self->mem +1 , self->pmd_size -1 , + self->seed,1 )); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, rwp_swapin) +{ + self->uffd =register_rwp (self->mem, self->pmd_size, true); +if (self->uffd <0 ) +SKIP (return,"Userfaultfd RWP unsupported\n" ); + +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->mem, self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, munmap) +{ +unsigned long swap_before, swap_after; +int ret; + + swap_before =read_vmswap (); +ASSERT_GE (swap_before, self->pmd_size); + + ret =munmap (self->mem, self->pmd_size); +if (!ret) { + self->mem =MAP_FAILED ; + self->mem_len =0 ; + } +ASSERT_EQ (ret,0 ); + + swap_after =read_vmswap (); +ASSERT_LE (swap_after, swap_before - self->pmd_size); +} + +TEST_F(pmd_swap, mprotect) +{ +ASSERT_EQ (mprotect (self->mem, self->pmd_size,PROT_READ ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_EQ (mprotect (self->mem, self->pmd_size, +PROT_READ |PROT_WRITE ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, split_mprotect) +{ +unsigned long half = self->pmd_size /2 ; + +ASSERT_EQ (mprotect (self->mem, half,PROT_READ ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_EQ (mprotect (self->mem, half,PROT_READ |PROT_WRITE ),0 ); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, split_munmap) +{ +unsigned long half = self->pmd_size /2 ; +unsigned long swap_before =read_vmswap (); +unsigned long i; +char *base = self->mem; +int ret; + +ASSERT_GE (swap_before, half); + ret =munmap (base, half); +if (!ret) { + self->mem = base + half; + self->mem_len = half; + } +ASSERT_EQ (ret,0 ); +ASSERT_LE (read_vmswap (), swap_before - half); + +for (i =0 ; i < half; i +=getpagesize ()) +ASSERT_TRUE (pagemap_is_swapped (self->pagemap_fd, + self->mem + i)); +ASSERT_TRUE (verify_pattern_range (self->mem, half, self->seed, half)); +} + +TEST_F(pmd_swap, uffdio_move) +{ +struct uffdio_register reg = {}; +struct uffdio_move move = {}; +struct uffdio_api api = {}; +bool rwp; + + self->aux =mmap_pmd_aligned (self->pmd_size); +if (self->aux ==MAP_FAILED ) +SKIP (return,"Could not mmap aligned dst\n" ); +ASSERT_EQ (madvise (self->aux, self->pmd_size,MADV_HUGEPAGE ),0 ); + + self->uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (self->uffd <0 ) +SKIP (return,"userfaultfd unavailable\n" ); + + api.api =UFFD_API ; + api.features =UFFD_FEATURE_MOVE |UFFD_FEATURE_RWP ; +if (ioctl (self->uffd,UFFDIO_API , &api) || + !(api.features &UFFD_FEATURE_MOVE )) +SKIP (return,"UFFD_FEATURE_MOVE unsupported\n" ); + rwp = api.features &UFFD_FEATURE_RWP ; + + reg.range.start = (unsigned long )self->aux; + reg.range.len = self->pmd_size; + reg.mode =UFFDIO_REGISTER_MODE_MISSING | + (rwp ?UFFDIO_REGISTER_MODE_RWP :0 ); +ASSERT_EQ (ioctl (self->uffd,UFFDIO_REGISTER , ®),0 ); + + move.dst = (unsigned long )self->aux; + move.src = (unsigned long )self->mem; + move.len = self->pmd_size; +ASSERT_EQ (ioctl (self->uffd,UFFDIO_MOVE , &move),0 ); +ASSERT_EQ (move.move, self->pmd_size); + +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->aux, + self->pmd_size)); +if (rwp) +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->aux, + self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->aux, self->pmd_size, self->seed)); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->aux, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, mremap) +{ +char *new_mem, *dst; + + self->aux =mmap_pmd_aligned (self->pmd_size); +if (self->aux ==MAP_FAILED ) +SKIP (return,"Could not mmap aligned dst\n" ); + dst = self->aux; + + new_mem =mremap (self->mem, self->pmd_size, self->pmd_size, +MREMAP_MAYMOVE |MREMAP_FIXED , dst); +if (new_mem !=MAP_FAILED ) { + self->mem = new_mem; + self->aux =MAP_FAILED ; + } +ASSERT_NE (new_mem,MAP_FAILED ); +ASSERT_EQ (new_mem, dst); + +ASSERT_TRUE (check_swapped (self->pagemap_fd, new_mem, self->pmd_size)); +ASSERT_TRUE (verify_pattern (new_mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, pagemap) +{ +uint64_t entry, first =0 ; +unsigned long off; + +for (off =0 ; off < self->pmd_size; off +=getpagesize ()) { + entry =pagemap_get_entry (self->pagemap_fd, self->mem + off); +ASSERT_TRUE (entry & (1ULL <<62 )); +ASSERT_FALSE (entry & (1ULL <<63 )); + +if (entry &PM_PFRAME_MASK ) { +uint64_t idx = off /getpagesize (); + +if (!off) + first = entry &PM_PFRAME_MASK ; +ASSERT_EQ (entry &PM_PFRAME_MASK , + first + (idx <<MAX_SWAPFILES_SHIFT )); + } + } +} + +TEST_F(pmd_swap, mincore) +{ +unsigned long pages = self->pmd_size /getpagesize (); +unsigned char vec[pages]; + +ASSERT_EQ (mincore (self->mem, self->pmd_size, vec),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +} + +TEST_F(pmd_swap, madvise_free) +{ +unsigned long swap_before =read_vmswap (); +unsigned long i; + +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_GE (swap_before, self->pmd_size); +ASSERT_EQ (madvise (self->mem, self->pmd_size,MADV_FREE ),0 ); +for (i =0 ; i < self->pmd_size; i +=getpagesize ()) +ASSERT_FALSE (pagemap_is_swapped (self->pagemap_fd, + self->mem + i)); +ASSERT_LE (read_vmswap (), swap_before - self->pmd_size); +ASSERT_TRUE (verify_zero (self->mem, self->pmd_size)); +} + +TEST_F(pmd_swap, madvise_willneed) +{ +ASSERT_EQ (madvise (self->mem, self->pmd_size,MADV_WILLNEED ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, swapoff) +{ +int ret, err; + + self->uffd =register_rwp (self->mem, self->pmd_size, true); + + ret =swapoff (self->swap_dev); + err = errno; +if (!ret) + self->swap_disabled = true; + ASSERT_EQ(ret, 0) { +TH_LOG ("swapoff(%s) failed: %s" , self->swap_dev,strerror (err)); + } + +/* +* Check residency before touching the memory. If we read +* first, a bug that left a PMD swap entry in place after swapoff +* would silently trigger do_huge_pmd_swap_page() and reinstall a +* PMD mapping, masking the regression. +*/ +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +if (self->uffd >=0 ) +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->mem, + self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); + + ret =swapon (self->swap_dev,0 ); + err = errno; +if (!ret) + self->swap_disabled = false; + ASSERT_EQ(ret, 0) { +TH_LOG ("swapon(%s) failed: %s" , self->swap_dev,strerror (err)); + } +} + +TEST_HARNESS_MAIN tools/testing/selftests/mm/run_vmtests.sh +2
-3@@ -69,6 +69,8 @@ separated by spaces: test pagemap_scan IOCTL - pfnmap tests for VM_PFNMAP handling +- pmd_swap + tests for PMD-level swap entries - process_madv test for process_madv - cow@@ -399,6 +401,8 @@ CATEGORY="pagemap" run_test ./pagemap_ioctlCATEGORY="pfnmap"
run_test./pfnmaptools/testing/selftests/mm/run_vmtests.sh +2
-0+CATEGORY="pmd_swap" run_test ./pmd_swap +# COW tests
CATEGORY="cow"run_test./cowSignature · 1 line
2.53.0-Meta
Usama Arif
reviewerQuoted 7 lines · Usama Arif
When reclaim swaps out a PMD-mapped anonymous THP today, the PMD is split into
HPAGE_PMD_NRPTE-level swap entries viaTTU_SPLIT_HUGE_PMDbefore unmap. This series introduces a PMD-level swap entry so the huge mapping can survive the swap round-trip anddo_huge_pmd_swap_page()can restore the PMD mapping directly on swap-in, without waiting for khugepaged to collapse the range later.
Addressing sashiko feedback from [1] here as its a common reply for most of it.
The PMD swap entry is installed in patch 11, until that time, the path in patches 1-10 is not taken, so the feedback from sashiko is mostly wrong as the feedback in patches 1-10 is that PMD swap entry is not handled in another path, but it is in a later patch from where the feedback is being given.
If patches 1-10 were squashed into a single patch, I imagine sashiko will be happy :)
[1] https://sashiko.dev/#/patchset/20260818131202.494754-1-usama.arif%40linux.dev
Re: [PATCH v6 01/12] mm: rename pmd_to_softleaf_folio() to pmd_softleaf_to_folio()
In reply to Usama ArifQuoted 8 lines · Usama Arif
pmd_to_softleaf_folio()converts the softleaf entry encoded by a PMD to a folio. Rename it topmd_softleaf_to_folio()to make the conversion direction explicit and align it withsoftleaf_to_folio().No functional change.
Suggested-by Dev Jain <[email protected]>Signed-off-by Usama Arif <[email protected]>
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Kiryl Shutsemau
reviewerRe: [PATCH v6 02/12] mm: add PMD swap entry detection support
In reply to Usama ArifQuoted 2 lines · Usama Arif
I have now split the architecture helpers into separate patches, followed by generic support for the next revision.
Shouldn't generic come first?
Quoted 4 lines
I think I will need split patch 7 and patch 12 into
several independent patches as well? This will significantly grow the patch count which is what I was trying to avoid as I feel that makes it more daunting to review (atleast for me), but I will try and keep it under 57 patches to not beat Kiryl :)
Resistance is futile. Relax in creep into triple digit patch number. :P
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Kiryl Shutsemau
reviewerRe: [PATCH v6 03/12] mm: add PMD swap entry splitting support
In reply to Usama ArifQuoted 10 lines · Usama Arif
Add a swap branch in
__split_huge_pmd_locked()that splits a PMD swap entry into 512 PTE swap entries. No folio reference is needed because swap entries point to swap slots rather than pages. Each PTE inherits the correct sub-slot offset and preservessoft_dirty,uffd_wp, and exclusive flags.The
folio_remove_rmap_pmd()gate at the end must inspectold_pmdrather than *pmd: for a present THP split, *pmd has already been cleared bypmdp_invalidate(), and that invalidated bit pattern can decode as a plausible swap entry.
Hm. On x86, pmd_present() recognises !_PAGE_PRESENT | _PAGE_PSE PMDs as present. Other archs do similar thing. pmdp_invalidate() clears present bit, but keeps PSE around, so it is present from SW PoV.
pmd_is_migration_entry() should work correctly on pmdp_invalidate()ed entries.
Quoted 22 lines
This branch is reached from the explicit
__split_huge_pmd()callers that hit a non-present PMD: partial-range mprotect / munmap, thewp_huge_pmd()PMD-COW fallback, and the swap-in / swapoff fallbacks added in later patches when the cached folio is no longer PMD-sized.page_vma_mapped_walk()does not iterate PMD swap entries, sotry_to_unmap_one()andtry_to_migrate_one()do not reach this branch and freeze=true cannot occur in this branch today. page and folio are therefore left uninitialized in the swap branch; aVM_WARN_ON_ONCE(freeze)catches any future caller that breaks this invariant before the freeze path dereferencespage_to_pfn(page + i)orput_page(page).Signed-off-by Usama Arif <[email protected]>--- mm/huge_memory.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)mm/huge_memory.c +0
-0
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3259
Quoted 8 lines
mm/huge_memory.c +5
-0@@ -3252,6 +3252,14 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , vma, haddr, rmap_flags); } + }else if (pmd_is_swap_entry (*pmd)) { +VM_WARN_ON_ONCE (freeze); +/* Swap entries have no page for the migration freeze path. */ + freeze = false; + old_pmd = *pmd;
PTL protects us from value drifting under us if compiler decides to load it multiple times, right? Same pattern as the migration/device-private branches above, so not a new concern, just checking.
BTW, do we want VM_WARN_ON_ONCE(pmd_is_swap_entry(old_pmd)) in !vma_is_anonymous() branch to avoid surprises?
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Kiryl Shutsemau
reviewerRe: [PATCH v6 03/12] mm: add PMD swap entry splitting support
In reply to David Hildenbrand (Arm)Quoted 44 lines · David Hildenbrand (Arm)
Quoted 33 lines · Usama Arif
Add a swap branch in
__split_huge_pmd_locked()that splits a PMD swap entry into 512 PTE swap entries. No folio reference is needed because swap entries point to swap slots rather than pages. Each PTE inherits the correct sub-slot offset and preservessoft_dirty,uffd_wp, and exclusive flags.The
folio_remove_rmap_pmd()gate at the end must inspectold_pmdrather than *pmd: for a present THP split, *pmd has already been cleared bypmdp_invalidate(), and that invalidated bit pattern can decode as a plausible swap entry.This branch is reached from the explicit
__split_huge_pmd()callers that hit a non-present PMD: partial-range mprotect / munmap, thewp_huge_pmd()PMD-COW fallback, and the swap-in / swapoff fallbacks added in later patches when the cached folio is no longer PMD-sized.page_vma_mapped_walk()does not iterate PMD swap entries, sotry_to_unmap_one()andtry_to_migrate_one()do not reach this branch and freeze=true cannot occur in this branch today. page and folio are therefore left uninitialized in the swap branch; aVM_WARN_ON_ONCE(freeze)catches any future caller that breaks this invariant before the freeze path dereferencespage_to_pfn(page + i)orput_page(page).Signed-off-by Usama Arif <[email protected]>--- mm/huge_memory.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)mm/huge_memory.c +0
-0mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3258
Quoted 7 lines
mm/huge_memory.c +4
-0@@ -3252,6 +3252,14 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , vma, haddr, rmap_flags); } + }else if (pmd_is_swap_entry (*pmd)) { +VM_WARN_ON_ONCE (freeze); +/* Swap entries have no page for the migration freeze path. */ + freeze = false;It's odd to
VM_WARN_ON_ONCE()and then set freeze=false;
Quoted 1 line
I'd just add the comment above the
VM_WARN_ON_ONCE()and drop the =false.
VM_WARN_ON_ONCE() is compiled out without DEBUG_VM. The freeze = false is what actually keeps the swap entry out of
if (freeze || pmd_is_migration_entry(old_pmd)) {
...
make_writable_migration_entry(page_to_pfn(page + i));and out of the put_page(page) below it, where page is uninitialized in the swap branch. Dropping it leaves nothing on production builds.
If freeze = false goes, folio and page must be set to NULL there (or something along the lines).
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Kiryl Shutsemau
reviewerRe: [PATCH v6 04/12] mm: handle PMD swap entries in fork path
In reply to Usama ArifQuoted 7 lines · Usama Arif
Teach copy_huge_pmd()/copy_huge_non_present_pmd() about swap entries, mirroring
copy_nonpresent_pte().
swap_dup_entry_direct()gains a nr parameter (and is renamed toswap_dup_entries_direct()) so it can duplicate a contiguous range of swap slots in one call, matching the existingswap_put_entries_direct(entry, nr)API. Existing callers pass 1.
...
mm/swapfile.c int swap_dup_entry_direct(swp_entry_t entry) · line 3971
Quoted 7 lines
mm/swapfile.c +1
-1@@ -3955,7 +3967,7 @@ int swap_dup_entry_direct(swp_entry_t entry)*/ VM_WARN_ON_ONCE (!swap_entry_swapped (si, entry)); -return swap_dup_entries_cluster (si,swp_offset (entry),1 ); +return swap_dup_entries_cluster (si,swp_offset (entry), nr); }
swap_put_entries_direct() bounds end_offset against si->max and walks the range cluster by cluster. Here nr goes straight into the cluster helper, which does ci_end = offset % SWAPFILE_CLUSTER + nr and indexes up to ci_end - 1. __swap_table_get() VM_WARN_ON_ONCE()s on off >= SWAPFILE_CLUSTER, but still reads table[off].
So is the range required to sit inside one cluster? I assume it works out because a PMD swap entry is cluster-aligned and SWAPFILE_CLUSTER == HPAGE_PMD_NR, but I don't see it stated anywhere.
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 02/12] mm: add PMD swap entry detection support
In reply to Usama ArifQuoted 13 lines
Quoted 9 lines
Quoted 8 lines
Signed-off-by Usama Arif <[email protected]>--- arch/arm64/include/asm/pgtable.h | 6 +++++ arch/loongarch/include/asm/pgtable.h | 19 ++++++++++++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 17 +++++++++++++ arch/riscv/include/asm/pgtable.h | 15 +++++++++++ arch/s390/include/asm/pgtable.h | 17 +++++++++++++ arch/x86/include/asm/pgtable.h | 17 +++++++++++++Quoted 3 lines
I'm sorry for asking you to compete with Kiryll's series by creating one patch for each architecture that directly jumps at arch maintainers :)
Quoted 5 lines
I have now split the architecture helpers into separate patches, followed by generic support for the next revision. I think I will need split patch 7 and patch 12 into several independent patches as well? This will significantly grow the patch count which is what I was trying to avoid as I feel that makes it more daunting to review (atleast for me), but I will try and keep it under 57 patches to not beat Kiryl :)
Note that such mechanical arch patches are extremely easy to review. Doing it per arch is the clearest ask from arch maintainers to ack a little patch :)
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 03/12] mm: add PMD swap entry splitting support
In reply to Kiryl ShutsemauQuoted 50 lines · Kiryl Shutsemau
Quoted 44 lines · David Hildenbrand (Arm)
Quoted 33 lines · Usama Arif
Add a swap branch in
__split_huge_pmd_locked()that splits a PMD swap entry into 512 PTE swap entries. No folio reference is needed because swap entries point to swap slots rather than pages. Each PTE inherits the correct sub-slot offset and preservessoft_dirty,uffd_wp, and exclusive flags.The
folio_remove_rmap_pmd()gate at the end must inspectold_pmdrather than *pmd: for a present THP split, *pmd has already been cleared bypmdp_invalidate(), and that invalidated bit pattern can decode as a plausible swap entry.This branch is reached from the explicit
__split_huge_pmd()callers that hit a non-present PMD: partial-range mprotect / munmap, thewp_huge_pmd()PMD-COW fallback, and the swap-in / swapoff fallbacks added in later patches when the cached folio is no longer PMD-sized.page_vma_mapped_walk()does not iterate PMD swap entries, sotry_to_unmap_one()andtry_to_migrate_one()do not reach this branch and freeze=true cannot occur in this branch today. page and folio are therefore left uninitialized in the swap branch; aVM_WARN_ON_ONCE(freeze)catches any future caller that breaks this invariant before the freeze path dereferencespage_to_pfn(page + i)orput_page(page).Signed-off-by Usama Arif <[email protected]>--- mm/huge_memory.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)mm/huge_memory.c +0
-0mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3258
Quoted 7 lines
mm/huge_memory.c +4
-0@@ -3252,6 +3252,14 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , vma, haddr, rmap_flags); } + }else if (pmd_is_swap_entry (*pmd)) { +VM_WARN_ON_ONCE (freeze); +/* Swap entries have no page for the migration freeze path. */ + freeze = false;It's odd to
VM_WARN_ON_ONCE()and then set freeze=false;Quoted 1 line
I'd just add the comment above the
VM_WARN_ON_ONCE()and drop the =false.
VM_WARN_ON_ONCE()is compiled out withoutDEBUG_VM. The freeze = false is what actually keeps the swap entry out of
Quoted 6 lines
if (freeze || pmd_is_migration_entry(old_pmd)) { ... make_writable_migration_entry(page_to_pfn(page + i));and out of the
put_page(page)below it, where page is uninitialized in the swap branch. Dropping it leaves nothing on production builds.
Right, because it's a condition you shouldn't hit on production builds.
And I raised how we can restructure the code to make it clear that we are being overly cautions here.
Let's not add unnecessary code just because the existing code is hard to follow.
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 03/12] mm: add PMD swap entry splitting support
In reply to Usama ArifQuoted 21 lines
m
Quoted 19 lines
Quoted 10 lines
mm/huge_memory.c +4
-0+ old_pmd = *pmd; + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd (old_pmd); + anon_exclusive =pmd_swp_exclusive (old_pmd); }else {/* * Up to this point the pmd is present and huge and userland has mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3400
Quoted 5 lines
mm/huge_memory.c +2
-0@@ -3388,6 +3396,25 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,VM_WARN_ON (!pte_none (ptep_get (pte + i)));set_pte_at (mm, addr, pte + i, entry); } + }else if (pmd_is_swap_entry (old_pmd)) { +softleaf_t sl_entry =softleaf_from_pmd (old_pmd);No existing code uses "
sl_entry". Maybe just call it "pmd_swp_entry"/"swp_entry" and below "pte_swp_entry".
Quoted 1 line
Thanks! Will do in next revision
Thanks, that will improve the code :)
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH v6 02/12] mm: add PMD swap entry detection support
In reply to Usama ArifQuoted 12 lines · Usama Arif
Quoted 9 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines · David Hildenbrand (Arm)
Ah, for migration entries we still use a dedicated migratetype. I actually have on my todo list to move to PTE bits as well.
We do seem to have rather hacked in PMD level stuff across the board previously :)
Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
(likely the _swp_ part should then be renamed to indicate that this is for softdirty entries, not just swap entries)
You mean softleaf?
Yes, I read that as softleaf as well.>>
Haha, yes :)
Signature · 3 lines
Cheers, David
Re: [PATCH v6 12/12] selftests/mm: add PMD swap entry tests
In reply to Usama ArifQuoted 27 lines · Usama Arif
Exercise the PMD swap entry paths. Each test gets a fresh PMD-mapped THP from fixture setup, fills it with a page-distinct pattern, swaps it out with
MADV_PAGEOUT, and verifies thatthp_swpout_pmdincreased.The tests are:
- basic: fault in a swapped PMD and verify its contents.
- fork: verify parent and child can fault in the shared swap entry.
-fork_cow: verify parent and child writes remain isolated.
- write: fault in by writing one byte and preserve the rest of the THP.
-rwp_swapin: verify userfaultfd RWP survives PMD-order swap-in.
- munmap: unmap the full entry and check that VmSwap drops.
- mprotect: change full-range protections without faulting the entry in.
-split_mprotect: change half-range protections and verify the data.
-split_munmap: unmap half, drop its accounting, and preserve the rest.
-uffdio_move: move the entry and RWP state, then fault it in at dst.
- mremap: force the entry to a new aligned address and verify the data.
- pagemap: verify swapped bits and consecutive swap-slot offsets.
- mincore: walk the entry without faulting it in.
-madvise_free: release the slots, clear the entry, and verify zeroes.
-madvise_willneed: prefetch the entry and verify subsequent swap-in.
- swapoff: unuse the entry and preserve data and PMD/RWP state.Fixture teardown owns the mappings and file descriptors and restores swap after assertion failures.
PMD_SWAP_DEVICEremains optional for swapoff.
Quoted 7 lines
Distinguish an environment that cannot allocate a PMD THP from a failure to install a PMD swap entry, so the former skips while the latter fails. Also check VmSwap accounting, pagemap slot offsets, swapped state after non-faulting operations, and PMD restoration when zswap does not require PTE fallback.
Register the test with
run_vmtests.shand the default kselftest runner.
Thanks for addressing the comments against v5, here's two general comments:
1. On looking a bit closer at this, I realized that during swap-in do_huge_pmd_swap_page() is retrieving the folio from the swap cache and not exercising the swapin_sync() code path (at least this is the case on a KVM a guest). I was able to workaround this on a test app by running it from a cgroup and triggering reclaim manually after swap-out but before swap-in. I'm not sure how complex it would be for you to implement this though
2. Did you use AI to generate this code? If yes, please add the Assisted-by tag. I'm saying this because the code is weirdly uniform and there's a bit of uneeded complexity
Small nits below, but the parts that I reviewed look good to me.
Quoted 142 lines
Signed-off-by Usama Arif <[email protected]>--- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/ksft_pmd_swap.sh | 4 + tools/testing/selftests/mm/pmd_swap.c | 742 ++++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 4 files changed, 752 insertions(+)create mode 100755
tools/testing/selftests/mm/ksft_pmd_swap.shcreate mode 100644tools/testing/selftests/mm/pmd_swap.ctools/testing/selftests/mm/Makefile +2
-0@@ -104,6 +104,7 @@ TEST_GEN_FILES += guard-regions TEST_GEN_FILES += merge TEST_GEN_FILES += rmap TEST_GEN_FILES += folio_split_race_test +TEST_GEN_FILES += pmd_swap ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty@@ -165,6 +166,7 @@ TEST_PROGS += ksft_mremap.sh TEST_PROGS += ksft_pagemap.sh TEST_PROGS += ksft_pfnmap.sh TEST_PROGS += ksft_pkey.sh +TEST_PROGS += ksft_pmd_swap.sh TEST_PROGS += ksft_process_madv.sh TEST_PROGS += ksft_process_mrelease.sh TEST_PROGS += ksft_rmap.shtools/testing/selftests/mm/ksft_pmd_swap.sh new file +4
-0@@ -0,0 +1,4 @@ +#!/bin/sh -e +# SPDX-License-Identifier: GPL-2.0 + +./run_vmtests.sh -t pmd_swaptools/testing/selftests/mm/pmd_swap.c new file +95
-0@@ -0,0 +1,742 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Test PMD-level swap entries and their users. */ +#define _GNU_SOURCE +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/mman.h> +#include <sys/wait.h> +#include <fcntl.h> +#include <errno.h> +#include <stdint.h> +#include <sys/random.h> +#include <sys/swap.h> +#include <sys/syscall.h> +#include <sys/ioctl.h> +#include <poll.h> +#include <pthread.h> +#include <linux/userfaultfd.h> +#include <time.h> + +#include "kselftest_harness.h" +#include "vm_util.h" + +#define ZSWAP_ENABLED_PATH "/sys/module/zswap/parameters/enabled" + +/* pagemap: bits 0-54 hold the PFN, or type|offset for a swap entry. */ +#define PM_PFRAME_MASK ((1ULL << 55) - 1) +/* Must match MAX_SWAPFILES_SHIFT in include/linux/swap.h. */ +#define MAX_SWAPFILES_SHIFT 5 + +static bool check_swapped (int pagemap_fd,char *addr,unsigned long size) +{ +unsigned long off; + +for (off =0 ; off < size; off +=getpagesize ()) +if (!pagemap_is_swapped (pagemap_fd, addr + off)) +return false; +return true; +} + +static bool zswap_enabled (void ) +{ +char enabled =0 ; +FILE *f; + + f =fopen (ZSWAP_ENABLED_PATH ,"r" ); +if (!f) +return false; + +if (fscanf (f," %c" , &enabled) !=1 ) + enabled =0 ; +fclose (f); + +return enabled =='Y' || enabled =='y' || enabled =='1' ; +} + +static bool swap_available (unsigned long required_bytes) +{ +unsigned long required_kb = (required_bytes +1023 ) /1024 ; +unsigned long size_kb, used_kb; +char line[256 ]; +bool ret = false; +FILE *f; + + f =fopen ("/proc/swaps" ,"r" ); +if (!f) +return false; + +/* Skip the header. */ +if (!fgets (line,sizeof (line), f)) + goto out; + +while (fgets (line,sizeof (line), f)) { +if (sscanf (line,"%*s %*s %lu %lu" , &size_kb, &used_kb) ==2 && + size_kb >= used_kb && size_kb - used_kb >= required_kb) { + ret = true; +break ; + } + } + +out: +fclose (f); +return ret; +} + +static unsigned long read_vm_event (const char *name) +{ +char line[256 ]; +size_t name_len =strlen (name); +unsigned long val =0 ; +FILE *f; + + f =fopen ("/proc/vmstat" ,"r" ); +if (!f)
tools/testing/selftests/mm/pmd_swap.c line 105
Quoted 10 lines
tools/testing/selftests/mm/pmd_swap.c new file +10
-0+return 0 ; +while (fgets (line,sizeof (line), f)) { +if (!strncmp (line, name, name_len) && line[name_len] ==' ' ) { + val =strtoul (line + name_len +1 ,NULL ,10 ); +break ; + } + } +fclose (f); +return val; +}
Maybe put this in vm_util.c as you made it generic?
Quoted 4 lines
tools/testing/selftests/mm/pmd_swap.c new file +4
-0+ +static unsigned int random_seed (void ) +{ +unsigned int seed;
tools/testing/selftests/mm/pmd_swap.c line 119
Quoted 10 lines
tools/testing/selftests/mm/pmd_swap.c new file +10
-0+ +if (getrandom (&seed,sizeof (seed),0 ) !=sizeof (seed)) + seed = (unsigned int )time (NULL ); +return seed; +} + +static unsigned char pattern_byte (unsigned int seed,unsigned long off) +{ +return (unsigned char )(seed + off + (off >>8 ) + (off >>16 )); +}
The shifting is not wrong, but I'm not sure it's necessary.
Quoted 126 lines
tools/testing/selftests/mm/pmd_swap.c new file +126
-0+ +static void fill_pattern (char *buf,unsigned long size,unsigned int seed) +{ +unsigned long i; + +for (i =0 ; i < size; i++) + buf[i] = (char )pattern_byte (seed, i); +} + +static bool verify_pattern_range (char *buf,unsigned long size, +unsigned int seed,unsigned long offset) +{ +unsigned long i; + +for (i =0 ; i < size; i++) +if ((unsigned char )buf[i] !=pattern_byte (seed, offset + i)) +return false; +return true; +} + +static bool verify_pattern (char *buf,unsigned long size,unsigned int seed) +{ +return verify_pattern_range (buf, size, seed,0 ); +} + +static bool verify_zero (char *buf,unsigned long size) +{ +unsigned long i; + +for (i =0 ; i < size; i++) +if (buf[i]) +return false; +return true; +} + +/* +* mmap an anonymous PMD-aligned region of pmd_size bytes. Over-allocates +* by one PMD and trims the unaligned head/tail so the returned address is +* PMD-aligned (required for whole-PMD UFFDIO_MOVE). +*/ +static char *mmap_pmd_aligned (unsigned long pmd_size) +{ +unsigned long pad = pmd_size; +char *raw, *aligned; + + raw =mmap (NULL , pmd_size + pad,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); +if (raw ==MAP_FAILED ) +return MAP_FAILED ; + + aligned = (char *)(((uintptr_t )raw + pmd_size -1 ) & ~(pmd_size -1 )); +if (aligned != raw) +munmap (raw, aligned - raw); +if (aligned + pmd_size != raw + pmd_size + pad) +munmap (aligned + pmd_size, + (raw + pmd_size + pad) - (aligned + pmd_size)); +return aligned; +} + +enum swap_thp_result { +SWAP_THP_OK , +SWAP_THP_UNAVAILABLE , +SWAP_THP_FAILED , +}; + +/* Per-process swapped size in bytes, from /proc/self/status VmSwap. */ +static unsigned long read_vmswap (void ) +{ +char line[256 ]; +unsigned long kb =0 ; +FILE *f; + + f =fopen ("/proc/self/status" ,"r" ); +if (!f) +return 0 ; +while (fgets (line,sizeof (line), f)) { +if (!strncmp (line,"VmSwap:" ,7 )) { + kb =strtoul (line +7 ,NULL ,10 ); +break ; + } + } +fclose (f); +return kb *1024 ; +} + +static bool swap_out_pmd (char *mem,unsigned long pmd_size,int pagemap_fd) +{ +unsigned long before =read_vm_event ("thp_swpout_pmd" ); +unsigned long after; + +if (madvise (mem, pmd_size,MADV_PAGEOUT )) { +ksft_print_msg ("MADV_PAGEOUT failed: %s\n" ,strerror (errno)); +return false; + } +if (!check_swapped (pagemap_fd, mem, pmd_size)) { +ksft_print_msg ("MADV_PAGEOUT did not swap the whole PMD range\n" ); +return false; + } + + after =read_vm_event ("thp_swpout_pmd" ); +ksft_print_msg ("thp_swpout_pmd: %lu -> %lu\n" , before, after); +return after > before; +} + +static char *alloc_fill_swap_thp (unsigned long pmd_size,int pagemap_fd, +unsigned int seed,enum swap_thp_result *res) +{ +char *mem; + + *res =SWAP_THP_UNAVAILABLE ; + + mem =mmap_pmd_aligned (pmd_size); +if (mem ==MAP_FAILED ) +return MAP_FAILED ; + +if (madvise (mem, pmd_size,MADV_HUGEPAGE )) { +ksft_print_msg ("MADV_HUGEPAGE failed: %s\n" ,strerror (errno)); +munmap (mem, pmd_size); +return MAP_FAILED ; + } +fill_pattern (mem, pmd_size, seed); + +if (!check_huge_anon (mem, pmd_size,1 , pmd_size)) { +munmap (mem, pmd_size); +return MAP_FAILED ; + }
tools/testing/selftests/mm/pmd_swap.c line 255
Quoted 10 lines
tools/testing/selftests/mm/pmd_swap.c new file +10
-0+ *res =SWAP_THP_FAILED ; + +if (!swap_out_pmd (mem, pmd_size, pagemap_fd)) { +munmap (mem, pmd_size); +return MAP_FAILED ; + } + + *res =SWAP_THP_OK ; +return mem; +}
Is the enum really needed? Maybe just have a bool *swap_failed instead?
Quoted 112 lines
tools/testing/selftests/mm/pmd_swap.c new file +112
-0+ +struct rwp_access_args { +unsigned char *addr; +unsigned char expected; +bool write; +bool ok; +}; + +static void *rwp_access_thread (void *data) +{ +struct rwp_access_args *args = data; + +if (args->write) + *args->addr = args->expected; + args->ok = *args->addr == args->expected; +return NULL ; +} + +static int register_rwp (char *addr,unsigned long size,bool protect) +{ +struct uffdio_register reg = {}; +struct uffdio_rwprotect rwp = {}; +struct uffdio_api api = {}; +int uffd; + + uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (uffd <0 ) +return -1 ; + + api.api =UFFD_API ; + api.features =UFFD_FEATURE_RWP ; +if (ioctl (uffd,UFFDIO_API , &api) || + !(api.features &UFFD_FEATURE_RWP )) + goto error; + + reg.range.start = (unsigned long )addr; + reg.range.len = size; + reg.mode =UFFDIO_REGISTER_MODE_RWP ; +if (ioctl (uffd,UFFDIO_REGISTER , ®)) + goto error; + +if (!protect) +return uffd; + + rwp.range.start = (unsigned long )addr; + rwp.range.len = size; + rwp.mode =UFFDIO_RWPROTECT_MODE_RWP ; +if (!ioctl (uffd,UFFDIO_RWPROTECT , &rwp)) +return uffd; + +error: +close (uffd); +return -1 ; +} + +static bool expect_rwp_fault (int uffd,char *addr,unsigned long size, +unsigned char expected,bool write) +{ +struct rwp_access_args args = { + .addr = (unsigned char *)addr, + .expected = expected, + .write = write, + }; +struct uffdio_rwprotect rwp = { + .range = { + .start = (unsigned long )addr, + .len = size, + }, + }; +struct pollfd pollfd = { + .fd = uffd, + .events =POLLIN , + }; +struct uffd_msg msg = {}; +pthread_t thread; +bool saw_rwp = false; +int ret; + +if (pthread_create (&thread,NULL , rwp_access_thread, &args)) +return false; + + ret =poll (&pollfd,1 ,5000 ); +if (ret ==1 && (pollfd.revents &POLLIN ) && +read (uffd, &msg,sizeof (msg)) == (ssize_t )sizeof (msg)) { + saw_rwp = msg.event ==UFFD_EVENT_PAGEFAULT && + (msg.arg.pagefault.flags &UFFD_PAGEFAULT_FLAG_RWP ); + } + +/* Resolve the access even on failure so the worker cannot remain blocked. */ +ioctl (uffd,UFFDIO_RWPROTECT , &rwp); +if (pthread_join (thread,NULL )) +return false; +return saw_rwp && args.ok; +} + +FIXTURE (pmd_swap) +{ +unsigned long pmd_size; +unsigned long mem_len; +int pagemap_fd; +int uffd; +unsigned int seed; +bool zswap_enabled; +bool swap_disabled; +const char *swap_dev; +char *mem; +char *aux; +}; + +FIXTURE_SETUP (pmd_swap) +{ +enum swap_thp_result res;
tools/testing/selftests/mm/pmd_swap.c line 377
Quoted 10 lines
tools/testing/selftests/mm/pmd_swap.c new file +10
-0+ + self->pagemap_fd =-1 ; + self->uffd =-1 ; + self->mem =MAP_FAILED ; + self->aux =MAP_FAILED ; + self->mem_len =0 ; + self->swap_disabled = false; + self->swap_dev =getenv ("PMD_SWAP_DEVICE" ); +if (!strcmp (_metadata->name,"swapoff" ) && !self->swap_dev) +SKIP (return,"PMD_SWAP_DEVICE env var not set\n" );
This seeems to be specific to the swapoff test? If yes, it would be better to have it contained in the test itself not the fixture.
Quoted 387 lines
tools/testing/selftests/mm/pmd_swap.c new file +365
-0+ + self->pmd_size =read_pmd_pagesize (); +if (!self->pmd_size) +SKIP (return,"Cannot determine PMD size\n" ); + + self->pagemap_fd =open ("/proc/self/pagemap" ,O_RDONLY ); +if (self->pagemap_fd <0 ) +SKIP (return,"Cannot open /proc/self/pagemap\n" ); + +if (!swap_available (self->pmd_size)) +SKIP (return,"No active swap device has enough free space\n" ); + + self->seed =random_seed (); + self->zswap_enabled =zswap_enabled (); + self->mem =alloc_fill_swap_thp (self->pmd_size, self->pagemap_fd, + self->seed, &res); +if (self->mem ==MAP_FAILED ) { +ASSERT_NE (res,SWAP_THP_FAILED ); +SKIP (return,"Could not create swapped THP\n" ); + } + self->mem_len = self->pmd_size; +} + +FIXTURE_TEARDOWN (pmd_swap) +{ +int swap_err =0 ; +int swap_ret =0 ; + +if (self->swap_disabled) { + swap_ret =swapon (self->swap_dev,0 ); + swap_err = errno; + } +if (self->uffd >=0 ) +close (self->uffd); +if (self->aux !=MAP_FAILED ) +munmap (self->aux, self->pmd_size); +if (self->mem !=MAP_FAILED ) +munmap (self->mem, self->mem_len); +if (self->pagemap_fd >=0 ) +close (self->pagemap_fd); + + EXPECT_EQ(swap_ret, 0) { +TH_LOG ("swapon(%s) failed: %s" , self->swap_dev, +strerror (swap_err)); + } +} + +TEST_F(pmd_swap, basic) +{ +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, fork) +{ +pid_t pid; +int status; + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) +_exit (verify_pattern (self->mem, self->pmd_size, + self->seed) ?0 :1 ); + +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); + +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); +} + +TEST_F(pmd_swap, fork_cow) +{ +unsigned int parent_seed = self->seed; +unsigned int child_seed = ~self->seed; +unsigned int new_seed = self->seed ^0xa5a5a5a5 ; +int release_child[2 ]; +bool parent_ok; +char c =0 ; +pid_t pid; +int status, ret; + +ASSERT_EQ (pipe (release_child),0 ); + + pid =fork (); +ASSERT_GE (pid,0 ); + +if (pid ==0 ) { +close (release_child[1 ]); +if (read (release_child[0 ], &c,1 ) !=1 ) +_exit (1 ); +if (!verify_pattern (self->mem, self->pmd_size, parent_seed)) +_exit (2 ); +fill_pattern (self->mem, self->pmd_size, child_seed); +if (!verify_pattern (self->mem, self->pmd_size, child_seed)) +_exit (3 ); +_exit (0 ); + } + +close (release_child[0 ]); +fill_pattern (self->mem, self->pmd_size, new_seed); + parent_ok =verify_pattern (self->mem, self->pmd_size, new_seed); + ret =write (release_child[1 ], &c,1 ); +close (release_child[1 ]); +ASSERT_EQ (waitpid (pid, &status,0 ), pid); +ASSERT_EQ (ret,1 ); +ASSERT_TRUE (parent_ok); +ASSERT_TRUE (WIFEXITED (status)); +ASSERT_EQ (WEXITSTATUS (status),0 ); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, new_seed)); +} + +TEST_F(pmd_swap, write) +{ + self->mem[0 ] =0xbb ; +ASSERT_EQ (self->mem[0 ], (char )0xbb ); +ASSERT_TRUE (verify_pattern_range (self->mem +1 , self->pmd_size -1 , + self->seed,1 )); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, rwp_swapin) +{ + self->uffd =register_rwp (self->mem, self->pmd_size, true); +if (self->uffd <0 ) +SKIP (return,"Userfaultfd RWP unsupported\n" ); + +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->mem, self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, munmap) +{ +unsigned long swap_before, swap_after; +int ret; + + swap_before =read_vmswap (); +ASSERT_GE (swap_before, self->pmd_size); + + ret =munmap (self->mem, self->pmd_size); +if (!ret) { + self->mem =MAP_FAILED ; + self->mem_len =0 ; + } +ASSERT_EQ (ret,0 ); + + swap_after =read_vmswap (); +ASSERT_LE (swap_after, swap_before - self->pmd_size); +} + +TEST_F(pmd_swap, mprotect) +{ +ASSERT_EQ (mprotect (self->mem, self->pmd_size,PROT_READ ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_EQ (mprotect (self->mem, self->pmd_size, +PROT_READ |PROT_WRITE ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, split_mprotect) +{ +unsigned long half = self->pmd_size /2 ; + +ASSERT_EQ (mprotect (self->mem, half,PROT_READ ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_EQ (mprotect (self->mem, half,PROT_READ |PROT_WRITE ),0 ); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, split_munmap) +{ +unsigned long half = self->pmd_size /2 ; +unsigned long swap_before =read_vmswap (); +unsigned long i; +char *base = self->mem; +int ret; + +ASSERT_GE (swap_before, half); + ret =munmap (base, half); +if (!ret) { + self->mem = base + half; + self->mem_len = half; + } +ASSERT_EQ (ret,0 ); +ASSERT_LE (read_vmswap (), swap_before - half); + +for (i =0 ; i < half; i +=getpagesize ()) +ASSERT_TRUE (pagemap_is_swapped (self->pagemap_fd, + self->mem + i)); +ASSERT_TRUE (verify_pattern_range (self->mem, half, self->seed, half)); +} + +TEST_F(pmd_swap, uffdio_move) +{ +struct uffdio_register reg = {}; +struct uffdio_move move = {}; +struct uffdio_api api = {}; +bool rwp; + + self->aux =mmap_pmd_aligned (self->pmd_size); +if (self->aux ==MAP_FAILED ) +SKIP (return,"Could not mmap aligned dst\n" ); +ASSERT_EQ (madvise (self->aux, self->pmd_size,MADV_HUGEPAGE ),0 ); + + self->uffd =syscall (__NR_userfaultfd,O_CLOEXEC |O_NONBLOCK ); +if (self->uffd <0 ) +SKIP (return,"userfaultfd unavailable\n" ); + + api.api =UFFD_API ; + api.features =UFFD_FEATURE_MOVE |UFFD_FEATURE_RWP ; +if (ioctl (self->uffd,UFFDIO_API , &api) || + !(api.features &UFFD_FEATURE_MOVE )) +SKIP (return,"UFFD_FEATURE_MOVE unsupported\n" ); + rwp = api.features &UFFD_FEATURE_RWP ; + + reg.range.start = (unsigned long )self->aux; + reg.range.len = self->pmd_size; + reg.mode =UFFDIO_REGISTER_MODE_MISSING | + (rwp ?UFFDIO_REGISTER_MODE_RWP :0 ); +ASSERT_EQ (ioctl (self->uffd,UFFDIO_REGISTER , ®),0 ); + + move.dst = (unsigned long )self->aux; + move.src = (unsigned long )self->mem; + move.len = self->pmd_size; +ASSERT_EQ (ioctl (self->uffd,UFFDIO_MOVE , &move),0 ); +ASSERT_EQ (move.move, self->pmd_size); + +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->aux, + self->pmd_size)); +if (rwp) +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->aux, + self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->aux, self->pmd_size, self->seed)); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->aux, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, mremap) +{ +char *new_mem, *dst; + + self->aux =mmap_pmd_aligned (self->pmd_size); +if (self->aux ==MAP_FAILED ) +SKIP (return,"Could not mmap aligned dst\n" ); + dst = self->aux; + + new_mem =mremap (self->mem, self->pmd_size, self->pmd_size, +MREMAP_MAYMOVE |MREMAP_FIXED , dst); +if (new_mem !=MAP_FAILED ) { + self->mem = new_mem; + self->aux =MAP_FAILED ; + } +ASSERT_NE (new_mem,MAP_FAILED ); +ASSERT_EQ (new_mem, dst); + +ASSERT_TRUE (check_swapped (self->pagemap_fd, new_mem, self->pmd_size)); +ASSERT_TRUE (verify_pattern (new_mem, self->pmd_size, self->seed)); +} + +TEST_F(pmd_swap, pagemap) +{ +uint64_t entry, first =0 ; +unsigned long off; + +for (off =0 ; off < self->pmd_size; off +=getpagesize ()) { + entry =pagemap_get_entry (self->pagemap_fd, self->mem + off); +ASSERT_TRUE (entry & (1ULL <<62 )); +ASSERT_FALSE (entry & (1ULL <<63 )); + +if (entry &PM_PFRAME_MASK ) { +uint64_t idx = off /getpagesize (); + +if (!off) + first = entry &PM_PFRAME_MASK ; +ASSERT_EQ (entry &PM_PFRAME_MASK , + first + (idx <<MAX_SWAPFILES_SHIFT )); + } + } +} + +TEST_F(pmd_swap, mincore) +{ +unsigned long pages = self->pmd_size /getpagesize (); +unsigned char vec[pages]; + +ASSERT_EQ (mincore (self->mem, self->pmd_size, vec),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +} + +TEST_F(pmd_swap, madvise_free) +{ +unsigned long swap_before =read_vmswap (); +unsigned long i; + +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_GE (swap_before, self->pmd_size); +ASSERT_EQ (madvise (self->mem, self->pmd_size,MADV_FREE ),0 ); +for (i =0 ; i < self->pmd_size; i +=getpagesize ()) +ASSERT_FALSE (pagemap_is_swapped (self->pagemap_fd, + self->mem + i)); +ASSERT_LE (read_vmswap (), swap_before - self->pmd_size); +ASSERT_TRUE (verify_zero (self->mem, self->pmd_size)); +} + +TEST_F(pmd_swap, madvise_willneed) +{ +ASSERT_EQ (madvise (self->mem, self->pmd_size,MADV_WILLNEED ),0 ); +ASSERT_TRUE (check_swapped (self->pagemap_fd, self->mem, + self->pmd_size)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +} + +TEST_F(pmd_swap, swapoff) +{ +int ret, err; + + self->uffd =register_rwp (self->mem, self->pmd_size, true); + + ret =swapoff (self->swap_dev); + err = errno; +if (!ret) + self->swap_disabled = true; + ASSERT_EQ(ret, 0) { +TH_LOG ("swapoff(%s) failed: %s" , self->swap_dev,strerror (err)); + } + +/* +* Check residency before touching the memory. If we read +* first, a bug that left a PMD swap entry in place after swapoff +* would silently trigger do_huge_pmd_swap_page() and reinstall a +* PMD mapping, masking the regression. +*/ +if (!self->zswap_enabled) +ASSERT_TRUE (check_huge_anon (self->mem, self->pmd_size,1 , + self->pmd_size)); +if (self->uffd >=0 ) +ASSERT_TRUE (expect_rwp_fault (self->uffd, self->mem, + self->pmd_size, +pattern_byte (self->seed,0 ), false)); +ASSERT_TRUE (verify_pattern (self->mem, self->pmd_size, self->seed)); + + ret =swapon (self->swap_dev,0 ); + err = errno; +if (!ret) + self->swap_disabled = false; + ASSERT_EQ(ret, 0) { +TH_LOG ("swapon(%s) failed: %s" , self->swap_dev,strerror (err)); + } +} + +TEST_HARNESS_MAIN tools/testing/selftests/mm/run_vmtests.sh +4
-0@@ -69,6 +69,8 @@ separated by spaces: test pagemap_scan IOCTL - pfnmap tests for VM_PFNMAP handling +- pmd_swap + tests for PMD-level swap entries - process_madv test for process_madv - cow@@ -399,6 +401,8 @@ CATEGORY="pagemap" run_test ./pagemap_ioctl CATEGORY="pfnmap" run_test ./pfnmap +CATEGORY="pmd_swap" run_test ./pmd_swap + # COW tests CATEGORY="cow" run_test ./cow
Quoted 33 lines · Usama Arif
When reclaim swaps out a PMD-mapped anonymous THP today, the PMD is split into
HPAGE_PMD_NRPTE-level swap entries viaTTU_SPLIT_HUGE_PMDbefore unmap. This series introduces a PMD-level swap entry so the huge mapping can survive the swap round-trip anddo_huge_pmd_swap_page()can restore the PMD mapping directly on swap-in, without waiting for khugepaged to collapse the range later.The PMD swap entry is a compact page-table encoding for
HPAGE_PMD_NRconsecutive swap slots.swap_mapaccounting remains per-slot and is unchanged. Importantly, a PMD swap entry does not promise that the swap cache always contains one PMD-sized folio. While the cache is empty or contains one PMD-sized folio, PMD-level handling can proceed. Once the cache has split/per-slot state, users either inspect the individual slots directly (mincore) or split the PMD swap entry and retry through the PTE path (fault, swapoff,MADV_WILLNEED,UFFDIO_MOVE). Likewise, if any slot is still backed by zswap's per-page store, PMD-order swap-in consumers split and let the PTE path load the range page by page; an all-on-disk range can still be read back as one PMD-sized folio.The series is ordered so every consumer can handle PMD swap entries before the swap-out producer starts installing them. The swap-out patch is the last functional change.
Notes on zswap:
Native PMD-order zswap load/store is intentionally left for a follow-up. Alexandre Ghiti is currently working this.
This series can still preserve PMD swap entries while zswap is enabled:
zswap stores the THP as order-0 entries, and PMD-order swap-in consumers split any range that has zswap entries before reading it. If zswap has written the whole range back to disk, or the swap cache still contains one PMD-sized folio, PMD-level handling can proceed.
Quoted 5 lines
Testing:
The 16
pmd_swapselftests pass onx86_64with zswap both disabled and enabled.PMD_SWAP_DEVICEwas set, so the swapoff test ran in both configurations.
I ran the pmd_swap selftests with zswap disabled on x86_64 and arm64, it worked (noting the fact that on swap-in we may be retrieving the folio from the swap cache as I commented on the kselftest patch).
Quoted 260 lines
v5 -> v6: https://lore.kernel.org/all/[email protected]/
- Add patch 1 to renamepmd_to_softleaf_folio()topmd_softleaf_to_folio(). No functional change. (Dev Jain)
- Patch 2: warn whenpmd_softleaf_to_folio()is given a non-PFN softleaf rather than silently returning NULL. (Dev Jain)
- Patch 4: bound the fork extend-table fallback to one retry, re-read the PMD under its lock, normalize unrecoverablecopy_huge_pmd()errors to-ENOMEMsocopy_pmd_range()cannot clear and leak the source swap PMD, and drop a redundantthp_migration_supported()gate.
- Patch 5: check multi-page swap-cache insertions for zswap-backed slots in__swap_cache_add_check()under the cluster lock, both before allocation and before insertion, and reject mixed zswap/disk state with-EBUSY. (Yosry Ahmed, Nhat Pham)
- Patch 6: on a failed non-uptodate PMD-order read, remove the large folio from swap cache before splitting so order-0
fallback retries individual slots rather than poisoning the whole
2 MiB range; retain hardware-poisoned folios for per-subpage handling.
- Patch 7: make HMM snapshot mode report a PMD swap entry as non-resident, matching PTE swap entries, rather thanHMM_PFN_ERROR. Drop redundantthp_migration_supported()gates and simplify non-present PMD handling.
- Patch 8: factor PMDMADV_WILLNEEDprefetch intoswapin_pmd_swap_entry(), split and retry through PTEs after any PMD-order swapin failure, and replace the racyfolio_test_locked()plusfolio_lock()sequence withfolio_trylock().
- Patch 9: guard PMD-swapUFFDIO_MOVEcode withCONFIG_THP_SWAP, clarify RWP marker propagation, and reject a PMD swap entry at the destination with-EEXISTsoUFFDIO_MOVEcannot loop forever on-EAGAIN.
- Patch 10: honor current THP/VMA policy before PMD-order swap-in, recheck that the PMD is still the original swap entry before splitting for PTE fallback, and provide theCONFIG_TRANSPARENT_HUGEPAGEwp_huge_pmd()declaration/stub needed by THP=n builds.
- Patch 11: make an invalidset_pmd_swap_entry()walk context warn and return-EINVALinstead of falsely reporting success and corrupting the MM_ANONPAGES/MM_SWAPENTS accounting, and add an exact PMD-size folio precondition check. (Luiz Capitulino)
- Patch 12: use/proc/swapsfor prerequisite detection, checkMADV_HUGEPAGE, and distinguish an environment that cannot allocate a PMD THP (SKIP) from a swap-out validation failure (FAIL). Add partial-mprotect and partial-munmap split coverage. Strengthen munmap/MADV_FREE VmSwap accounting, pagemap slot-offset checks, and mprotect/mremap swapped-state checks; force mremap to move, check munmap()'s return, and mark theUFFDIO_MOVEdestinationMADV_HUGEPAGEbefore asserting PMD restoration. Move common setup and cleanup into one fixture, merge the swapoff fixture, remove the redundant cycles test, and make the data pattern differ between base pages so the split tests can detect incorrect slot ordering. Order fork-COW so the parent writes while the child still holds the untouched shared swap entry. (Luiz Capitulino)
- Clarify commit messages throughout. RetainTTU_SPLIT_HUGE_PMDafter prototyping its removal: removing it here requires an extra rmap walk and broadens the series beyond PMD swap entries. (Matthew Wilcox)
- Rebase onto akpm/mm-new from 15 August (4b65683fd25f).v4 -> v5: https://lore.kernel.org/all/[email protected]/
- Commit message improvements for almost all patches (Yosry for zswap patch)
- Patch 1: makepmd_to_softleaf_folio()reject softleaf entries that do not encode a PFN, so a PMD swap offset is never interpreted as one. PMD swap entries remain valid softleaf entries for classification. (sashiko)
- Patch 2: use the existingpmd_swp_uffd()helper and force freeze=false for PMD swap entries, which have no struct page for the migration-entry freeze path. (sashiko)
- Patch 3: document that the caller's page-table or swap-cache reference pins every source slot while a partial PMD-sized duplication is rolled back. Keep the pre-existing PTE fork retry behavior outside this series. (sashiko)
- Patch 5: split to the PTE path rather than mapping a PMD-sized folio containing a hardware-poisoned subpage, and restorePAGE_NONEwhen swapoff restores a UFFD marker in an RWP VMA. (sashiko)
- Patch 6: account SwapPss for a PMD swap entry one slot at a time because the slots can have different swap reference counts.
- Patch 7: if PMD-orderMADV_WILLNEEDencounters newly populated per-page zswap state, revalidate and remove the failed clean PMD-sized cache folio before retrying through PTEs.
- Patch 8: mark a moved PMD swap entry for UFFD when theUFFDIO_MOVEdestination VMA is RWP-registered. (sashiko)
- Patch 9: restorePAGE_NONEfor UFFD RWP swap-in, preserve the original write-fault state through swap-slot release and COW handling, remove the unnecessary LRU drain, and prevent PTE batching from mapping a poisoned subpage. (sashiko)
- Patch 10: add and documentthp_swpout_pmd, which counts PMD mappings replaced by PMD-level swap entries rather than swapped folios.
- Patch 11: registerpmd_swapwith the default mm selftest runner, preserve errno acrossUFFDIO_MOVEcleanup, check swapoff residency before the first memory access, add a parent-side write and verification to the fork+COW test, and add RWP regression coverage for swap-in,UFFDIO_MOVE, and swapoff. (sashiko)
- Keepdo_huge_pmd_swap_page()in patch 9. Patches 6 and 7 only add consumers; patch 10 remains the first producer, so no PMD swap entry can reach those paths before the fault handler is present. (sashiko)
- Rebase onto latest akpm/mm-new from 22 July (5e0603ba185a)v3 -> v4: https://lore.kernel.org/all/[email protected]/
- Patch 1: guard the new arch-specificpmd_swp_mkexclusive/pmd_swp_exclusive/pmd_swp_clear_exclusivehelpers on arm64, loongarch, powerpc, riscv, s390, and x86 withCONFIG_ARCH_HAS_PMD_SOFTLEAVES, matching the pattern already
used forpmd_swp_soft_dirty. Also fixes the redefinition-vs- generic-fallback build errors kernel test robot reported on i386-allnoconfig-bpf and riscv-allnoconfig-bpf, and rewraps
the patch 1 commit message paragraphs to ~75 columns.
(sashiko, kernel test robot, Usama Arif)
- Patch 2: switch the trailingfolio_remove_rmap_pmd()gate in__split_huge_pmd_locked()from *pmd toold_pmd,old_pmdretains
the original present-or-non-present classification for every
branch above. (sashiko)
- Patch 3: teachswap_retry_table_alloc()(and the underlyingswap_extend_table_alloc()) to accept an nr parameter and scan
every slot in [ci_off,ci_off+ nr) before committing an extend-table allocation. (sashiko)
- Patch 4: renamezswap_range_has_entry()tozswap_is_present()so
the same helper serves both single-slot (nr=1) and range queries, and switch the implementation fromXA_STATE+xas_find()toxa_find(), which handles RCU locking and internal-retry markers itself. Rename the callers in patches 5, 7, 9. (Yosry)
- Patch 9: refuse to map a swap-cache folio indo_huge_pmd_swap_page()whenfolio_contain_hwpoisoned_page()reports a poisoned subpage; split the PMD swap entry sodo_swap_page()can returnVM_FAULT_HWPOISONper subpage instead of the PMD handler mapping the corrupted memory as one THP. Mirrors the PageHWPoison check the PTE swap-in path already performs. (sashiko)
- Patch 9: note explicitly in the commit message that PMD-order swap-in deliberately skips the order-0 readahead paths, order-0 readahead would populate per-page swap-cache state and force the PMD swap entry to split before the fault could finish. (Kairui)
- Patch 10: movemm_prepare_for_swap_entries()intoset_pmd_swap_entry()betweenfolio_dup_swap()andset_pmd_at()
so this mm is on init_mm.mmlist before any swap PMD referencing slots with a non-zeroswap_mapbecomes visible. Matches the PTE swap-out ordering. (sashiko)
- rebase onto latest akpm/mm-new (61cccb8363fcc282d4ae0555b8739dd227f5ad0b)v2 -> v3: https://lore.kernel.org/all/[email protected]/
- Clarified the PMD swap entry rule: it is a compact encoding forHPAGE_PMD_NRswap slots, not a guarantee that swap cache always has one PMD-sized folio. (Lance Yang)
- Swapoff, fault,MADV_WILLNEED, andUFFDIO_MOVEnow classify the whole PMD swap-cache range and split/retry through the PTE path for split/per-slot cache state. (Lance Yang)
- mincore handles PMD swap entries without assuming one lookup covers a split swap-cache range. (Lance Yang)
-UFFDIO_MOVErechecks allHPAGE_PMD_NRslots before moving an empty PMD swap-cache range, avoiding stale rmap metadata for per-slot cached folios.
- Added a standalone zswap prerequisite patch from Alexandre that distinguishes all-on-disk large-folio ranges from ranges with per-page zswap entries.
- Replaced the global zswap-ever-enabled policy with per-range zswap
checks: PMD swap entries can still be installed while zswap is enabled, and PMD-order swap-in consumers split when the range has per-page zswap state.
- Added a mincore selftest and updatedMADV_WILLNEEDcoverage so the test checks that the PMD swap entry remains in place until first touch. Totalpmd_swapcoverage is now 14 tests.v1 -> v2: https://lore.kernel.org/all/[email protected]/
- Patch 1: convert two additionalsoftleaf_to_pmd()callers that landed in mm-unstable since v1 (mm/debug_vm_pgtable.c,mm/migrate_device.c) (Dev)
- Patch 2: rename helperensure_on_mmlist()tomm_prepare_for_swap_entries()to better describe its purpose (David)
- Patch 3: dropVM_WARN_ON_ONCE(!pmd_is_migration_entry)as
Dev posted it as a separate patch.
- Patch 5 (new): movesoftleaf_to_folio()inside the device-private branch inmigrate_vma_collect_pmd(); same class of fix as patch 4 but for the migrate-device PMD walker.
- Patch 6 (new): renameCONFIG_ARCH_ENABLE_THP_MIGRATIONtoCONFIG_ARCH_HAS_PMD_SOFTLEAVESso the gate that now drives swap-entry support too is named for what it actually controls
(PMD softleaf entries), not just migration. (Dev)
- Patch 7: add the missingpmd_swp_exclusive/ mkexclusive /clear_exclusivehelpers for powerpc.
- Patches 10 and 14: use upstreamswapin_sync()(bundlesswap_cache_alloc_folio+swap_read_folio+ the-EEXISTrace
retry) instead of the bespokeswapin_alloc_pmd_folio()helper
from v1;do_swap_pageandshmem_swapin_foliouse the same
helper (Kairui)
- Patch 10: construct a stackvm_faultfor the swapoff swap-in so
the allocator can resolve a mempolicy, mirroring how the PTE swapoff path (unuse_pte_range) already does it.
- Patch 11: extend coverage tocheck_pmd_state()in khugepaged so a swapped-out PMD-mapped THP is treated asSCAN_PMD_MAPPED(matches the existing migration-entry handling). Route PMD swap entries in thepmd_trans_huge_lock()branch ofmincore_pte_range()throughmincore_pmd_swap()so a swapped-out PMD-mapped THP isn't reported as resident.
- Patch 12 (new): handle PMD swap entries inMADV_WILLNEEDvia swapin_sync(BIT(HPAGE_PMD_ORDER)); a naive order-0 read-ahead
would force the subsequent fault to split.
- Patch 13: refuseUFFDIO_MOVEwith-EBUSYif the swap-cache folio
was split between swap-out and the move, matching move_pages_pte()'s rejection of large folios; otherwise only one
of the 512 anon-rmaps would be re-anchored todst_vma.
- Patch 16:alloc_fill_swap_thp()now uses the existingmmap_pmd_aligned()helper so tests don't flake/skip based on VA placement; newMADV_WILLNEEDtest that watches the PMD-order
mTHP swpin counter; swapoff test restructured to use thekselftest_harnessASSERT cleanup blocks (no double swapoff, no verify-after-munmap).
- Collected Acks and Reviews.[1] https://lore.kernel.org/all/[email protected]/
Alexandre Ghiti (1):
mm: zswap: add range lookup for large-folio swapinUsama Arif (11):
mm: renamepmd_to_softleaf_folio()topmd_softleaf_to_folio()
mm: add PMD swap entry detection support
mm: add PMD swap entry splitting support
mm: handle PMD swap entries in fork path
mm: swap in PMD swap entries as whole THPs during swapoff
mm: handle PMD swap entries in non-present PMD walkers
mm: handle PMD swap entries inMADV_WILLNEED
mm: handle PMD swap entries inUFFDIO_MOVE
mm: handle PMD swap entry faults on swap-in
mm: install PMD swap entries on swap-out selftests/mm: add PMD swap entry testsDocumentation/admin-guide/mm/transhuge.rst | 5 + arch/arm64/include/asm/pgtable.h | 6 + arch/loongarch/include/asm/pgtable.h | 19 + arch/powerpc/include/asm/book3s/64/pgtable.h | 17 + arch/riscv/include/asm/pgtable.h | 15 + arch/s390/include/asm/pgtable.h | 17 + arch/x86/include/asm/pgtable.h | 17 + fs/proc/task_mmu.c | 46 +- include/linux/huge_mm.h | 16 + include/linux/leafops.h | 30 +- include/linux/pgtable.h | 17 + include/linux/swap.h | 4 +- include/linux/vm_event_item.h | 1 + include/linux/zswap.h | 6 + mm/hmm.c | 11 +- mm/huge_memory.c | 607 ++++++++++++++- mm/internal.h | 42 ++ mm/khugepaged.c | 6 + mm/madvise.c | 120 ++- mm/memory.c | 47 +- mm/mincore.c | 45 +- mm/rmap.c | 19 + mm/swap.h | 22 +- mm/swap_state.c | 54 ++ mm/swapfile.c | 209 +++++- mm/userfaultfd.c | 14 + mm/vmscan.c | 9 +- mm/vmstat.c | 1 + mm/zswap.c | 46 +- tools/testing/selftests/mm/Makefile | 2 + tools/testing/selftests/mm/ksft_pmd_swap.sh | 4 + tools/testing/selftests/mm/pmd_swap.c | 742 +++++++++++++++++++ tools/testing/selftests/mm/run_vmtests.sh | 4 + 33 files changed, 2104 insertions(+), 116 deletions(-)create mode 100755
tools/testing/selftests/mm/ksft_pmd_swap.shcreate mode 100644tools/testing/selftests/mm/pmd_swap.c