mm: PMD-level swap entries for anonymous THPs
Subsystems (22)
Usama Arif
reviewerv3 cover mm: PMD-level swap entries for anonymous THPs
This is the PMD swap entry core series. The preparatory PMD softleaf [1] cleanup series has been merged in akpm/mm-new, so this series is now based directly on akpm/mm-new.
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.
Patch breakdown:
1. mm: add PMD swap entry detection support
Add pmd_is_swap_entry(), teach the softleaf layer that PMD swap entries are valid PMD softleaf entries, and add per-arch pmd_swp_*exclusive helpers.
2. mm: add PMD swap entry splitting support
Teach __split_huge_pmd_locked() to split a PMD swap entry into HPAGE_PMD_NR PTE swap entries. This is the common fallback path whenever PMD-level handling is unsafe or unavailable.
3. mm: handle PMD swap entries in fork path
Copy a PMD swap entry in fork via one swap_dup_entries_direct(HPAGE_PMD_NR) operation, with the same swapoff/mmlist preparation rules used by PTE entries.
4. mm: zswap: add range lookup for large-folio swapin Teach zswap_load() to let all-on-disk large folio reads proceed to the backing swap device, and add zswap_range_has_entry() so PMD swap-entry consumers can split only when a specific range has per-page zswap state.
5. mm: swap in PMD swap entries as whole THPs during swapoff Add swap_pmd_cache_lookup() and use it from swapoff. Empty cache and PMD-sized cache state can be handled at PMD order; split cache state, zswap-backed slots, allocation/read failure, or non-uptodate folios split the PMD and fall back to unuse_pte_range().
6. mm: handle PMD swap entries in non-present PMD walkers Teach zap, mprotect, soft-dirty, uffd-wp, smaps, mincore, mempolicy, khugepaged, HMM, and madvise walkers about PMD swap entries. mincore reports PMD-sized cache state directly and checks per-page slots after the cache has split.
7. mm: handle PMD swap entries in MADV_WILLNEED
Let MADV_WILLNEED prefetch a PMD swap entry at PMD order when safe, treat an already cached PMD-sized folio as complete, and split/retry through PTEs for split cache state, zswap-backed slots, or races with per-slot cache population.
8. mm: handle PMD swap entries in UFFDIO_MOVE
Move PMD swap entries whole when the covered range is empty or backed by one PMD-sized folio. Split/per-slot cache state returns -EAGAIN after splitting so retry can use the PTE move path and update per-page rmap metadata.
9. mm: handle PMD swap entry faults on swap-in
Add do_huge_pmd_swap_page(). It maps a PMD-sized cached folio directly, or allocates/reads at PMD order when the cache is empty and the range has no zswap entries. Split cache state, zswap-backed slots, and PMD-order resource failures fall back to the PTE path.
10. mm: install PMD swap entries on swap-out
Stop forcing TTU_SPLIT_HUGE_PMD for PMD-mappable swapcache folios and install one PMD swap entry instead. Zswap still stores the THP as per-page compressed entries; PMD-order swap-in consumers preserve a PMD-sized cached folio or read an all-on-disk range as a whole THP, and split before reading per-page zswap state.
11. selftests/mm: add PMD swap entry tests
Add pmd_swap selftests covering swap-out/in, fork, fork+COW, repeated cycles, write fault, munmap, mprotect, mremap, pagemap, mincore, MADV_FREE, MADV_WILLNEED, UFFDIO_MOVE, and swapoff.
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.
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_SUPPORTS_PMD_SOFTLEAF 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 the pmd_trans_huge_lock() branch of mincore_pte_range() through mincore_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 (10):
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
arch/arm64/include/asm/pgtable.h | 4 + arch/loongarch/include/asm/pgtable.h | 17 + arch/powerpc/include/asm/book3s/64/pgtable.h | 15 + arch/riscv/include/asm/pgtable.h | 15 + arch/s390/include/asm/pgtable.h | 15 + arch/x86/include/asm/pgtable.h | 15 + fs/proc/task_mmu.c | 43 +- include/linux/huge_mm.h | 11 + include/linux/leafops.h | 24 +- include/linux/pgtable.h | 17 + include/linux/swap.h | 4 +- include/linux/vm_event_item.h | 1 + include/linux/zswap.h | 7 + mm/hmm.c | 3 +- mm/huge_memory.c | 565 ++++++++++++++- mm/internal.h | 36 + mm/khugepaged.c | 6 + mm/madvise.c | 89 ++- mm/memory.c | 42 +- mm/mempolicy.c | 2 + mm/mincore.c | 45 +- mm/rmap.c | 20 + mm/swap.h | 17 + mm/swap_state.c | 44 ++ mm/swapfile.c | 161 ++++- mm/vmscan.c | 9 +- mm/vmstat.c | 1 + mm/zswap.c | 46 +- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/pmd_swap.c | 702 +++++++++++++++++++ 30 files changed, 1878 insertions(+), 99 deletions(-) create mode 100644 tools/testing/selftests/mm/pmd_swap.c
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 1/11 mm: add PMD swap entry detection support
Currently when a PMD-mapped THP is swapped out, the PMD is always split into 512 PTE-level swap entries. To preserve huge page information across swap cycles, later patches will install a single PMD-level swap entry instead. This patch adds 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. Clear the exclusive overlay bit in softleaf_from_pmd() before decoding, matching how soft_dirty and uffd_wp 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 | 4 ++++ arch/loongarch/include/asm/pgtable.h | 17 ++++++++++++++ arch/powerpc/include/asm/book3s/64/pgtable.h | 15 ++++++++++++ arch/riscv/include/asm/pgtable.h | 15 ++++++++++++ arch/s390/include/asm/pgtable.h | 15 ++++++++++++ arch/x86/include/asm/pgtable.h | 15 ++++++++++++ include/linux/leafops.h | 24 ++++++++++++++++---- include/linux/pgtable.h | 17 ++++++++++++++ 8 files changed, 117 insertions(+), 5 deletions(-)
arch/arm64/include/asm/pgtable.h +4 -0
@@ -601,6 +601,10 @@ static inline int pmd_protnone(pmd_t pmd)#define pmd_swp_clear_uffd_wp (pmd) \ pte_pmd(pte_swp_clear_uffd_wp(pmd_pte(pmd)))#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ +#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)))#define pmd_write (pmd) pte_write(pmd_pte(pmd))
arch/loongarch/include/asm/pgtable.h +17 -0
@@ -357,6 +357,23 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return pte; } +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; +} +#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 +15 -0
@@ -699,6 +699,21 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return __pte_raw (pte_raw (pte) &cpu_to_be64 (~_PAGE_SWP_EXCLUSIVE)); } +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)); +} +static inline bool check_pte_access (unsigned long access,unsigned long ptev) {/*
arch/riscv/include/asm/pgtable.h +15 -0
@@ -930,6 +930,21 @@ static inline pmd_t pmd_swp_clear_uffd_wp(pmd_t pmd) }#endif /* CONFIG_HAVE_ARCH_USERFAULTFD_WP */ +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))); +} +#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY static inline bool pmd_soft_dirty (pmd_t pmd) {
arch/s390/include/asm/pgtable.h +15 -0
@@ -870,6 +870,21 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return clear_pte_bit (pte,__pgprot (_PAGE_SWP_EXCLUSIVE)); } +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)); +} +static inline int pte_soft_dirty (pte_t pte) {return pte_val (pte) & _PAGE_SOFT_DIRTY;
arch/x86/include/asm/pgtable.h +15 -0
@@ -1529,6 +1529,21 @@ static inline pte_t pte_swp_clear_exclusive(pte_t pte)return pte_clear_flags (pte, _PAGE_SWP_EXCLUSIVE); } +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); +} +#ifdef CONFIG_HAVE_ARCH_SOFT_DIRTY static inline pte_t pte_swp_mksoft_dirty (pte_t pte) {
include/linux/leafops.h +19 -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_wp (pmd)) pmd =pmd_swp_clear_uffd_wp (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. */
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_SUPPORTS_PMD_SOFTLEAF +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 v3 2/11 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. Unlike migration splits, no folio reference is needed because swap entries point to swap slots, not pages. Each PTE inherits the correct sub-slot offset and preserves soft_dirty, uffd_wp, and exclusive flags.
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 | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-)
mm/huge_memory.c +26 -1
@@ -3143,6 +3143,12 @@ 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); + old_pmd = *pmd; + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + anon_exclusive =pmd_swp_exclusive (old_pmd); }else {/* * Up to this point the pmd is present and huge and userland has
@@ -3279,6 +3285,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_wp (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;
@@ -3302,7 +3327,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 (*pmd) && !pmd_is_swap_entry (*pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 3/11 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.
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 after swap_retry_table_alloc() with GFP_KERNEL, matching the PTE retry in copy_pte_range(). The PMD is stable across the retry because dup_mmap() holds write mmap_lock on both mm_structs.
--- include/linux/swap.h | 4 ++-- mm/huge_memory.c | 53 ++++++++++++++++++++++++++++++++++++++------ mm/memory.c | 2 +- mm/swapfile.c | 7 +++--- 4 files changed, 53 insertions(+), 13 deletions(-)
include/linux/swap.h +2 -2
@@ -451,7 +451,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);/*
@@ -495,7 +495,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 +46 -7
84 lines not on this page · raw message
mm/memory.c +1 -1
@@ -950,7 +950,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);
mm/swapfile.c +4 -3
@@ -3899,8 +3899,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
@@ -3912,7 +3913,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;
@@ -3929,7 +3930,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 v3 4/11 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.
Add zswap_range_has_entry() so PMD swap-entry consumers can make the same range decision before attempting PMD-order swapin.
--- include/linux/zswap.h | 7 +++++++ mm/zswap.c | 46 +++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 10 deletions(-)
include/linux/zswap.h +7 -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_range_has_entry (swp_entry_t entry,unsigned int nr);#else struct zswap_lruvec_state {};
@@ -69,6 +70,12 @@ static inline bool zswap_never_enabled(void)return true; } +static inline bool zswap_range_has_entry (swp_entry_t entry, +unsigned int nr) +{ +return false; +} +#endif #endif /* _LINUX_ZSWAP_H */
mm/zswap.c +36 -10
65 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 5/11 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.
If the PMD-order folio cannot be allocated, 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 the folio is not uptodate, 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.
--- mm/swap.h | 17 ++++++ mm/swap_state.c | 44 ++++++++++++++ mm/swapfile.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 215 insertions(+)
mm/swap.h +17 -0
@@ -303,6 +303,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 +154 -0
172 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 6/11 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.
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() faults in PMD swap entries via hmm_vma_fault() instead of returning -EFAULT. 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.
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, leaking swap slots.
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 the pmd_trans_huge_lock() branch through mincore_swap() for non-present PMDs, 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_swap() returns 1 for migration / device-private entries (preserving the prior behavior for those) and checks swap-cache residency for swap entries.
queue_folios_pmd() in mempolicy silently skips swap entries, matching the PTE walker which only counts migration entries as failures. Without this, mbind(MPOL_MF_STRICT) would spuriously return -EIO on a swapped-out THP.
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.
--- fs/proc/task_mmu.c | 43 +++++++++++++++++++++------------- mm/hmm.c | 3 ++- mm/huge_memory.c | 58 +++++++++++++++++++++++++++++++++++----------- mm/khugepaged.c | 6 +++++ mm/madvise.c | 14 ++++++++++- mm/mempolicy.c | 2 ++ mm/mincore.c | 45 ++++++++++++++++++++++++++++++++++- 7 files changed, 139 insertions(+), 32 deletions(-)
fs/proc/task_mmu.c +27 -16
@@ -1046,6 +1046,23 @@ 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) +{ +int mapcount; + + mss->swap += size; + mapcount =swp_swapcount (entry); +if (mapcount >=2 ) { +u64 pss_delta = (u64 )size <<PSS_SHIFT ; + +do_div (pss_delta, mapcount); + mss->swap_pss += pss_delta; + }else { + mss->swap_pss += (u64 )size <<PSS_SHIFT ; + } +} +static void smaps_pte_entry (pte_t *pte,unsigned long addr,struct mm_walk *walk) {
@@ -1067,18 +1084,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 +1114,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); }
@@ -1752,7 +1762,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); }
@@ -2112,7 +2122,8 @@ 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_swap_entry (pmd) || + (thp_migration_supported () &&pmd_is_migration_entry (pmd))) {const softleaf_t entry =softleaf_from_pmd (pmd);unsigned long offset;
@@ -2550,7 +2561,7 @@ static void make_uffd_wp_pmd(struct vm_area_struct *vma, old =pmdp_invalidate_ad (vma, addr, pmdp); pmd =pmd_mkuffd_wp (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_wp (pmd);set_pmd_at (vma->vm_mm, addr, pmdp, pmd); }
mm/hmm.c +2 -1
@@ -370,7 +370,8 @@ 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_vma_fault (addr, end, required_fault, walk);else return -EFAULT ;
mm/huge_memory.c +45 -13
101 lines not on this page · raw message
mm/khugepaged.c +6 -0
@@ -1101,6 +1101,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
@@ -374,6 +374,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);
@@ -384,6 +393,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;
@@ -665,7 +677,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/mempolicy.c +2 -0
@@ -658,6 +658,8 @@ static void queue_folios_pmd(pmd_t *pmd, struct mm_walk *walk) qp->nr_failed++;return ; } +if (unlikely (pmd_is_swap_entry (*pmd))) +return ; folio =pmd_folio (*pmd);if (is_huge_zero_folio (folio)) { walk->action =ACTION_CONTINUE ;
mm/mincore.c +44 -1
57 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 7/11 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. If PMD-order swapin races with per-slot swap-cache population after dropping the PMD lock, split and retry through the PTE path instead.
--- mm/madvise.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+)
mm/madvise.c +75 -0
@@ -32,6 +32,7 @@#include <linux/leafops.h> #include <linux/shmem_fs.h> #include <linux/mmu_notifier.h> +#include <linux/zswap.h> #include <asm/tlb.h>
@@ -193,6 +194,79 @@ 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)) { +softleaf_t entry =softleaf_from_pmd (pmdval); +struct vm_fault vmf = { + .vma = vma, + .address = start, + .real_address = start, + .pmd = pmd, + }; +struct swap_info_struct *si; +struct folio *folio; +enum swap_pmd_cache cache_state; +bool split = false; + + cache_state =swap_pmd_cache_lookup (entry, &folio); +if (cache_state ==SWAP_PMD_CACHE_HUGE ) { +folio_put (folio); +spin_unlock (ptl); + goto ret; + } +if (cache_state ==SWAP_PMD_CACHE_SPLIT || +zswap_range_has_entry (entry,HPAGE_PMD_NR )) { +spin_unlock (ptl); +__split_huge_pmd (vma, pmd, start, false); + walk->action =ACTION_AGAIN ; + goto ret; + } + +/* +* 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) + goto ret; + + folio =swapin_sync (entry,GFP_HIGHUSER_MOVABLE , +BIT (HPAGE_PMD_ORDER ), &vmf, +NULL ,0 ); +/* +* The empty-cache observation was made under the PMD +* lock, but swap cache can change after dropping it. If +* PMD-order swapin lost a race to per-slot cache state, +* retry through the PTE path. +*/ +if (IS_ERR (folio)) { +if (PTR_ERR (folio) == -EBUSY ) + split = true; + }else if (folio) { +if (folio_nr_pages (folio) !=HPAGE_PMD_NR ) + split = true; +else if (!folio_test_locked (folio) && + !folio_test_uptodate (folio) && +zswap_range_has_entry (entry, +HPAGE_PMD_NR )) + split = true; +folio_put (folio); + } +put_swap_device (si); +if (split) { +__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;
@@ -221,6 +295,7 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start,if (ptep)pte_unmap_unlock (ptep, ptl);swap_read_unplug (splug); +ret:cond_resched ();return 0 ;
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 8/11 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_swap_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, and carry the deposited page table across with the entry.
The dispatcher in move_pages_huge_pmd() now waits for migration on a PMD migration entry (matching the PTE path) and routes PMD swap entries through move_swap_pmd() after pinning the swap device and arming an mmu_notifier range so secondary MMUs see the move.
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.
--- mm/huge_memory.c | 133 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 1 deletion(-)
mm/huge_memory.c +132 -1
@@ -2810,6 +2810,72 @@ int change_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma,#endif #ifdef CONFIG_USERFAULTFD +/* +* 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); +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 ; +} +/* * 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.
@@ -2844,11 +2910,76 @@ 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_pmdval);return -EAGAIN ; } +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; + } +spin_unlock (src_ptl);return -ENOENT ; }
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 9/11 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 swap_cache_alloc_folio() and read from swap.
- 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.
- Restore soft_dirty and uffd_wp from the swap PMD. 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, encrypted) 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 immediately rather than forcing a second fault. Mask VM_FAULT_FALLBACK from its 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. Requires exposing wp_huge_pmd() via mm/internal.h.
- 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 swapin_folio() 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 | 9 ++ mm/huge_memory.c | 216 ++++++++++++++++++++++++++++++++++++++++ mm/internal.h | 36 +++++++ mm/memory.c | 40 +------- 4 files changed, 265 insertions(+), 36 deletions(-)
include/linux/huge_mm.h +9 -0
@@ -531,6 +531,15 @@ vm_fault_t do_huge_pmd_numa_page(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;
mm/huge_memory.c +216 -0
228 lines not on this page · raw message
mm/internal.h +36 -0
@@ -506,6 +506,42 @@ static inline vm_fault_t vmf_anon_prepare(struct vm_fault *vmf) }vm_fault_t do_swap_page (struct vm_fault *vmf); +vm_fault_t wp_huge_pmd (struct vm_fault *vmf); + +/* +* Check if we should call folio_free_swap to free the swap cache. +* folio_free_swap only frees the swap cache to release the slot if swap +* count is zero, so we don't need to check the swap count here. +*/ +static inline bool should_try_to_free_swap (struct swap_info_struct *si, +struct folio *folio, +struct vm_area_struct *vma, +unsigned int extra_refs, +unsigned int fault_flags) +{ +if (!folio_test_swapcache (folio)) +return false; +/* +* Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap +* cache can help save some IO or memory overhead, but these devices +* are fast, and meanwhile, swap cache pinning the slot deferring the +* release of metadata or fragmentation is a more critical issue. +*/ +if (data_race (si->flags &SWP_SYNCHRONOUS_IO )) +return true; +if (mem_cgroup_swap_full (folio) || (vma->vm_flags &VM_LOCKED ) || +folio_test_mlocked (folio)) +return true; +/* +* If we want to map a page that's in the swapcache writable, we +* have to detect via the refcount if we're really the exclusive +* user. Try freeing the swapcache to get rid of the swapcache +* reference only in case it's likely that we'll be the exclusive user. +*/ +return (fault_flags &FAULT_FLAG_WRITE ) && !folio_test_ksm (folio) && +folio_ref_count (folio) == (extra_refs +folio_nr_pages (folio)); +} +void folio_rotate_reclaimable (struct folio *folio);bool __folio_end_writeback (struct folio *folio);void deactivate_file_folio (struct folio *folio);
mm/memory.c +4 -36
@@ -4497,40 +4497,6 @@ static vm_fault_t remove_device_exclusive_entry(struct vm_fault *vmf)return 0 ; } -/* -* Check if we should call folio_free_swap to free the swap cache. -* folio_free_swap only frees the swap cache to release the slot if swap -* count is zero, so we don't need to check the swap count here. -*/ -static inline bool should_try_to_free_swap (struct swap_info_struct *si, -struct folio *folio, -struct vm_area_struct *vma, -unsigned int extra_refs, -unsigned int fault_flags) -{ -if (!folio_test_swapcache (folio)) -return false; -/* -* Always try to free swap cache for SWP_SYNCHRONOUS_IO devices. Swap -* cache can help save some IO or memory overhead, but these devices -* are fast, and meanwhile, swap cache pinning the slot deferring the -* release of metadata or fragmentation is a more critical issue. -*/ -if (data_race (si->flags &SWP_SYNCHRONOUS_IO )) -return true; -if (mem_cgroup_swap_full (folio) || (vma->vm_flags &VM_LOCKED ) || -folio_test_mlocked (folio)) -return true; -/* -* If we want to map a page that's in the swapcache writable, we -* have to detect via the refcount if we're really the exclusive -* user. Try freeing the swapcache to get rid of the swapcache -* reference only in case it's likely that we'll be the exclusive user. -*/ -return (fault_flags &FAULT_FLAG_WRITE ) && !folio_test_ksm (folio) && -folio_ref_count (folio) == (extra_refs +folio_nr_pages (folio)); -} -static vm_fault_t pte_marker_clear (struct vm_fault *vmf) { vmf->pte = pte_offset_map_lock(vmf->vma->vm_mm, vmf->pmd,
@@ -6200,8 +6166,7 @@ static inline vm_fault_t create_huge_pmd(struct vm_fault *vmf)return VM_FAULT_FALLBACK ; } -/* `inline' is required to avoid gcc 4.1.2 build error */ -static inline vm_fault_t wp_huge_pmd (struct vm_fault *vmf) +vm_fault_t wp_huge_pmd (struct vm_fault *vmf) {struct vm_area_struct *vma = vmf->vma;const bool unshare = vmf->flags &FAULT_FLAG_UNSHARE ;
@@ -6486,6 +6451,9 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma,if (pmd_is_migration_entry (vmf.orig_pmd))pmd_migration_entry_wait (mm, vmf.pmd); +else if (IS_ENABLED (CONFIG_THP_SWAP ) && +pmd_is_swap_entry (vmf.orig_pmd)) +return do_huge_pmd_swap_page (&vmf);return 0 ; }if (pmd_trans_huge (vmf.orig_pmd)) {
Signature · 1 line
2.53.0-Meta
Usama Arif
reviewerPATCH v3 10/11 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.
--- include/linux/huge_mm.h | 2 + include/linux/vm_event_item.h | 1 + mm/huge_memory.c | 78 +++++++++++++++++++++++++++++++++++ mm/rmap.c | 20 +++++++++ mm/vmscan.c | 9 +++- mm/vmstat.c | 1 + 6 files changed, 110 insertions(+), 1 deletion(-)
include/linux/huge_mm.h +2 -0
@@ -533,6 +533,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 +78 -0
81 lines not on this page · raw message
mm/rmap.c +20 -0
@@ -2079,6 +2079,26 @@ 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; + +mm_prepare_for_swap_entries (mm); +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
@@ -1321,7 +1321,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
@@ -1421,6 +1421,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 v3 11/11 selftests/mm: add PMD swap entry tests
In reply to Usama ArifExercise the PMD swap entry paths. The tests allocate a PMD-mapped THP, write a known pattern, swap it out via MADV_PAGEOUT, and then exercise different code paths:
- swap-out / swap-in round-trip with data verification
- fork with read-only access from both parent and child
- fork with writes in both processes to verify COW isolation
- repeated swap cycles to catch reference counting issues
- write fault on a swapped PMD to verify dirty handling and PMD mapping restoration
- munmap of a swapped PMD (zap_huge_pmd swap slot cleanup)
- mprotect on a swapped PMD (change_non_present_huge_pmd)
- UFFDIO_MOVE on a swapped PMD (move_pages_huge_pmd swap path)
- mremap of a swapped PMD (move_soft_dirty_pmd)
- pagemap reading (pagemap_pmd_range_thp softleaf_has_pfn guard)
- mincore on a swapped PMD without faulting it in
- MADV_FREE on a swapped PMD: verifies swap slots are freed via pagemap and the memory reads back as zero
- MADV_WILLNEED on a swapped PMD
- swapoff with active PMD swap entries
When zswap is enabled, PMD-order consumers may split a PMD swap entry and retry through the PTE path because zswap stores the range as per-page entries. In that configuration, the tests still verify data correctness and log that the PMD mapping assertion is skipped. With zswap disabled, the tests assert that write faults, UFFDIO_MOVE, MADV_WILLNEED, and swapoff restore a PMD-mapped THP where expected.
--- tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/pmd_swap.c | 702 ++++++++++++++++++++++++++ 2 files changed, 703 insertions(+) create mode 100644 tools/testing/selftests/mm/pmd_swap.c
tools/testing/selftests/mm/Makefile +1 -0
@@ -105,6 +105,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
tools/testing/selftests/mm/pmd_swap.c new file +702 -0
702 lines not on this page · raw message
Signature · 1 line
2.53.0-Meta
kernel test robot
build botRe: [PATCH v3 11/11] selftests/mm: add PMD swap entry tests
kernel test robot noticed the following build errors:
Hi Usama,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Usama-Arif/mm-add-PMD-swap-entry-detection-support/20260704-014151 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20260703173903.3789516-12-usama.arif%40linux.dev patch subject: [PATCH v3 11/11] selftests/mm: add PMD swap entry tests config: riscv-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260704/[email protected]/config) compiler: riscv64-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260704/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from ./include/linux/pgtable.h:6,
from ./include/linux/mm.h:31,
from arch/riscv/kernel/asm-offsets.c:9:
./arch/riscv/include/asm/pgtable.h: In function 'pmd_swp_exclusive':
>> ./arch/riscv/include/asm/pgtable.h:935:16: error: implicit declaration of function 'pte_swp_exclusive'; did you mean 'pmd_swp_exclusive'? [-Wimplicit-function-declaration]
935 | return pte_swp_exclusive(pmd_pte(pmd));
| ^~~~~~~~~~~~~~~~~
| pmd_swp_exclusive
./arch/riscv/include/asm/pgtable.h: In function 'pmd_swp_mkexclusive':
>> ./arch/riscv/include/asm/pgtable.h:940:24: error: implicit declaration of function 'pte_swp_mkexclusive'; did you mean 'pmd_swp_mkexclusive'? [-Wimplicit-function-declaration]
940 | return pte_pmd(pte_swp_mkexclusive(pmd_pte(pmd)));
| ^~~~~~~~~~~~~~~~~~~
| pmd_swp_mkexclusive
>> ./arch/riscv/include/asm/pgtable.h:940:24: error: incompatible type for argument 1 of 'pte_pmd'
940 | return pte_pmd(pte_swp_mkexclusive(pmd_pte(pmd)));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| int
./arch/riscv/include/asm/pgtable.h:758:35: note: expected 'pte_t' but argument is of type 'int'
758 | static inline pmd_t pte_pmd(pte_t pte)
| ~~~~~~^~~
./arch/riscv/include/asm/pgtable.h: In function 'pmd_swp_clear_exclusive':
>> ./arch/riscv/include/asm/pgtable.h:945:24: error: implicit declaration of function 'pte_swp_clear_exclusive'; did you mean 'pmd_swp_clear_exclusive'? [-Wimplicit-function-declaration]
945 | return pte_pmd(pte_swp_clear_exclusive(pmd_pte(pmd)));
| ^~~~~~~~~~~~~~~~~~~~~~~
| pmd_swp_clear_exclusive
./arch/riscv/include/asm/pgtable.h:945:24: error: incompatible type for argument 1 of 'pte_pmd'
945 | return pte_pmd(pte_swp_clear_exclusive(pmd_pte(pmd)));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| int
./arch/riscv/include/asm/pgtable.h:758:35: note: expected 'pte_t' but argument is of type 'int'
758 | static inline pmd_t pte_pmd(pte_t pte)
| ~~~~~~^~~
./arch/riscv/include/asm/pgtable.h: At top level:
>> ./arch/riscv/include/asm/pgtable.h:1218:20: error: conflicting types for 'pte_swp_exclusive'; have 'bool(pte_t)' {aka '_Bool(pte_t)'}
1218 | static inline bool pte_swp_exclusive(pte_t pte)
| ^~~~~~~~~~~~~~~~~
./arch/riscv/include/asm/pgtable.h:935:16: note: previous implicit declaration of 'pte_swp_exclusive' with type 'int()'
935 | return pte_swp_exclusive(pmd_pte(pmd));
| ^~~~~~~~~~~~~~~~~
>> ./arch/riscv/include/asm/pgtable.h:1223:21: error: conflicting types for 'pte_swp_mkexclusive'; have 'pte_t(pte_t)'
1223 | static inline pte_t pte_swp_mkexclusive(pte_t pte)
| ^~~~~~~~~~~~~~~~~~~
./arch/riscv/include/asm/pgtable.h:940:24: note: previous implicit declaration of 'pte_swp_mkexclusive' with type 'int()'
940 | return pte_pmd(pte_swp_mkexclusive(pmd_pte(pmd)));
| ^~~~~~~~~~~~~~~~~~~
>> ./arch/riscv/include/asm/pgtable.h:1228:21: error: conflicting types for 'pte_swp_clear_exclusive'; have 'pte_t(pte_t)'
1228 | static inline pte_t pte_swp_clear_exclusive(pte_t pte)
| ^~~~~~~~~~~~~~~~~~~~~~~
./arch/riscv/include/asm/pgtable.h:945:24: note: previous implicit declaration of 'pte_swp_clear_exclusive' with type 'int()'
945 | return pte_pmd(pte_swp_clear_exclusive(pmd_pte(pmd)));
| ^~~~~~~~~~~~~~~~~~~~~~~vim +935 ./arch/riscv/include/asm/pgtable.h
932
933 static inline bool pmd_swp_exclusive(pmd_t pmd)
934 {
> 935 return pte_swp_exclusive(pmd_pte(pmd));
936 }
937
938 static inline pmd_t pmd_swp_mkexclusive(pmd_t pmd)
939 {
> 940 return pte_pmd(pte_swp_mkexclusive(pmd_pte(pmd)));
941 }
942
943 static inline pmd_t pmd_swp_clear_exclusive(pmd_t pmd)
944 {
> 945 return pte_pmd(pte_swp_clear_exclusive(pmd_pte(pmd))); 946 }
947
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v3 11/11] selftests/mm: add PMD swap entry tests
In reply to Usama Arifkernel test robot noticed the following build errors:
Hi Usama,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Usama-Arif/mm-add-PMD-swap-entry-detection-support/20260704-014151 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20260703173903.3789516-12-usama.arif%40linux.dev patch subject: [PATCH v3 11/11] selftests/mm: add PMD swap entry tests config: i386-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260704/[email protected]/config) compiler: gcc-14 (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260704/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from ./include/linux/mm.h:31,
from ./include/linux/memcontrol.h:21,
from ./include/linux/swap.h:9,
from ./include/linux/suspend.h:5,
from arch/x86/kernel/asm-offsets.c:14:
>> ./include/linux/pgtable.h:1921:21: error: redefinition of 'pmd_swp_mkexclusive'
1921 | static inline pmd_t pmd_swp_mkexclusive(pmd_t pmd)
| ^~~~~~~~~~~~~~~~~~~
In file included from ./arch/x86/include/asm/tlbflush.h:17,
from ./arch/x86/include/asm/uaccess.h:17,
from ./include/linux/uaccess.h:13,
from ./include/linux/sched/task.h:13,
from ./include/linux/sched/signal.h:9,
from ./include/linux/rcuwait.h:6,
from ./include/linux/percpu-rwsem.h:7,
from ./include/linux/fs/super_types.h:13,
from ./include/linux/fs/super.h:5,
from ./include/linux/fs.h:5,
from ./include/linux/cgroup.h:17,
from ./include/linux/memcontrol.h:13:
./arch/x86/include/asm/pgtable.h:1532:21: note: previous definition of 'pmd_swp_mkexclusive' with type 'pmd_t(pmd_t)'
1532 | static inline pmd_t pmd_swp_mkexclusive(pmd_t pmd)
| ^~~~~~~~~~~~~~~~~~~
>> ./include/linux/pgtable.h:1926:20: error: conflicting types for 'pmd_swp_exclusive'; have 'bool(pmd_t)' {aka '_Bool(pmd_t)'}
1926 | static inline bool pmd_swp_exclusive(pmd_t pmd)
| ^~~~~~~~~~~~~~~~~
./arch/x86/include/asm/pgtable.h:1537:19: note: previous definition of 'pmd_swp_exclusive' with type 'int(pmd_t)'
1537 | static inline int pmd_swp_exclusive(pmd_t pmd)
| ^~~~~~~~~~~~~~~~~
>> ./include/linux/pgtable.h:1931:21: error: redefinition of 'pmd_swp_clear_exclusive' 1931 | static inline pmd_t pmd_swp_clear_exclusive(pmd_t pmd)
| ^~~~~~~~~~~~~~~~~~~~~~~
./arch/x86/include/asm/pgtable.h:1542:21: note: previous definition of 'pmd_swp_clear_exclusive' with type 'pmd_t(pmd_t)'
1542 | static inline pmd_t pmd_swp_clear_exclusive(pmd_t pmd)
| ^~~~~~~~~~~~~~~~~~~~~~~
vim +/pmd_swp_mkexclusive +1921 ./include/linux/pgtable.h
1919
1920 #ifndef CONFIG_ARCH_SUPPORTS_PMD_SOFTLEAF
> 1921 static inline pmd_t pmd_swp_mkexclusive(pmd_t pmd)
1922 {
1923 return pmd;
1924 }
1925
> 1926 static inline bool pmd_swp_exclusive(pmd_t pmd)
1927 {
1928 return false;
1929 }
1930
> 1931 static inline pmd_t pmd_swp_clear_exclusive(pmd_t pmd) 1932 {
1933 return pmd;
1934 }
1935 #endif
1936
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Andrew Morton
maintainerQuoted 1 line · On Fri, 3 Jul 2026 10:38:17 -0700 Usama Arif <[email protected]> wrote:
This is the PMD swap entry core series.
Thanks.
AI review said a lot about the new code.
It also might have found a bunch of issues in the existing code. I looked at one (move_pages_huge_pmd() passing a stack-local pgtable_t* to pmd_migration_entry_wait()) and it looks legit.
So can the people who work on this code please take a look?
https://sashiko.dev/#/patchset/[email protected]
Re: [PATCH v3 09/11] mm: handle PMD swap entry faults on swap-in
In reply to Usama ArifQuoted 35 lines · Usama Arif
Add
do_huge_pmd_swap_page()and dispatch to it from__handle_mm_fault()whenvmf->orig_pmdencodes a swap entry. The handler resolves the entire 2 MB mapping in one shot, mirroringdo_swap_page()(PTE path) at PMD granularity:- Look up the folio in the swap cache; on a miss, allocate a PMD-order folio via
swap_cache_alloc_folio()and read from swap.- 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.- Restore
soft_dirtyanduffd_wpfrom the swap PMD. 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 anSWP_STABLE_WRITESbackend (zram, encrypted) so the PMD is mapped read-only; a later write COWs into a fresh folio rather than corrupting the in-flight writeback. Mirrorsdo_swap_page().- When the resulting PMD is read-only but the fault was a write, update
vmf->orig_pmdand callwp_huge_pmd()in the same handler to COW immediately rather than forcing a second fault. MaskVM_FAULT_FALLBACKfrom its return: a PMD-COW that splits to PTE-level is normal, but the bit is part ofVM_FAULT_ERRORand arch fault handlersBUG()on it without SIGBUS/HWPOISON/SIGSEGV. Requires exposingwp_huge_pmd()viamm/internal.h.- Free the swap slot via
should_try_to_free_swap()(hoisted frommm/memory.cintomm/internal.hso PTE- and PMD-level swap-in share the heuristic).
Quoted 8 lines
When PMD-order resources are unavailable (folio allocation fails, the cached folio was split, memcg charge fails, or
swapin_folio()races) split the PMD swap entry into 512 PTE swap entries via__split_huge_pmd()and return 0. The fault retries anddo_swap_page()takes over per-PTE. This avoids returningVM_FAULT_OOMfor transient PMD-order allocation failures.Signed-off-by Usama Arif <[email protected]>
Hello Usama
Amazing work.
Quoted 88 lines
--- include/linux/huge_mm.h | 9 ++ mm/huge_memory.c | 216 ++++++++++++++++++++++++++++++++++++++++ mm/internal.h | 36 +++++++ mm/memory.c | 40 +------- 4 files changed, 265 insertions(+), 36 deletions(-)include/linux/huge_mm.h +9
-0@@ -531,6 +531,15 @@ vm_fault_t do_huge_pmd_numa_page(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;mm/huge_memory.c +46
-0@@ -41,6 +41,7 @@#include <linux/pgalloc.h> #include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> +#include <linux/zswap.h> #include <asm/tlb.h> #include "internal.h" @@ -2312,6 +2313,221 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)return 0 ; } +#ifdef CONFIG_THP_SWAP +/** +* do_huge_pmd_swap_page() - Handle a fault on a PMD-level swap entry. +* @vmf: Fault context. vmf->orig_pmd contains the swap PMD. +* +* A PMD swap entry is a compact encoding for HPAGE_PMD_NR consecutive swap +* slots. If the swap cache still has one PMD-sized folio covering the range, +* map it directly at PMD level. If the range has been split into per-page +* cache state, or zswap may have per-page state for it, split the PMD swap +* entry and retry at PTE granularity. +* +* Return: VM_FAULT_* flags. +*/ +vm_fault_t do_huge_pmd_swap_page (struct vm_fault *vmf) +{ +struct vm_area_struct *vma = vmf->vma; +struct mm_struct *mm = vma->vm_mm; +struct folio *folio; +struct page *page; +struct swap_info_struct *si; +unsigned long haddr = vmf->address &HPAGE_PMD_MASK ; +softleaf_t entry; +swp_entry_t swp_entry; +pmd_t pmd; +vm_fault_t ret =0 ; +bool exclusive; +rmap_t rmap_flags =RMAP_NONE ; +enum swap_pmd_cache cache_state; + + entry =softleaf_from_pmd (vmf->orig_pmd); +if (unlikely (!softleaf_is_swap (entry))) +return 0 ; + + swp_entry = entry; + +/* Prevent swapoff from happening to us. */ + si =get_swap_device (swp_entry); +if (unlikely (!si)) +return 0 ; + + cache_state =swap_pmd_cache_lookup (swp_entry, &folio); +if (cache_state ==SWAP_PMD_CACHE_SPLIT ) + goto split_fallback; +if (!folio) { +/*
mm/huge_memory.c vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) · line 2370
Quoted 10 lines
mm/huge_memory.c +10
-0+* PMD swap entries encode ordinary per-page swap slots. If any +* slot is in zswap, split and let the PTE swap path load the +* range per page. Otherwise the range is all on disk and can be +* read back as one PMD-sized folio. +*/ +if (zswap_range_has_entry (swp_entry,HPAGE_PMD_NR )) + goto split_fallback; + + folio =swapin_sync (swp_entry,GFP_HIGHUSER_MOVABLE , +BIT (HPAGE_PMD_ORDER ), vmf,NULL ,0 );
Hmm, so here we always do a sync swapin which means there is no readahead, kind of make sense since PMD is huge, readahead might be not helpful and a order 0 readahead breaks following PMD. But maybe worth mention it in the commit message at least so it's easiler to follow. (partly because the name swapin_sync is a bit confusing at the moment, we could change that name as we improve swap readahead later).
And just an idea that will it be suffient if we just use the return value of swapin_sync directly? If a smaller folio covers the PMD start entry, the smaller folio is returned and you catched that just fine. If a smaller folio covers a follow-up sub-entry or a sub-entry is freed, -EBUSY is returned here and you just do the split. And it doesn't do the allocation for the first lookup check, so there should be no thrashing issue.
mm/huge_memory.c vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) · line 2380
Quoted 10 lines
mm/huge_memory.c +10
-0+if (IS_ERR_OR_NULL (folio)) + goto split_fallback; + +/* Had to read from swap area: Major fault */ + ret =VM_FAULT_MAJOR ; +count_vm_event (PGMAJFAULT ); +count_memcg_event_mm (mm,PGMAJFAULT ); + } + + ret |=folio_lock_or_retry (folio, vmf);
With PMD sized folio on a slower device, this try lock could fail much more frequently, leading to repeated faults. That's not a new issue and I remember Barry talked about it at the LSFMM this year, just I'm not sure if that will get much worse for PMD?
Usama Arif
reviewerRe: [PATCH v3 09/11] mm: handle PMD swap entry faults on swap-in
Quoted 43 lines · Kairui Song
Quoted 42 lines · Usama Arif
Add
do_huge_pmd_swap_page()and dispatch to it from__handle_mm_fault()whenvmf->orig_pmdencodes a swap entry. The handler resolves the entire 2 MB mapping in one shot, mirroringdo_swap_page()(PTE path) at PMD granularity:- Look up the folio in the swap cache; on a miss, allocate a PMD-order folio via
swap_cache_alloc_folio()and read from swap.- 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.- Restore
soft_dirtyanduffd_wpfrom the swap PMD. 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 anSWP_STABLE_WRITESbackend (zram, encrypted) so the PMD is mapped read-only; a later write COWs into a fresh folio rather than corrupting the in-flight writeback. Mirrorsdo_swap_page().- When the resulting PMD is read-only but the fault was a write, update
vmf->orig_pmdand callwp_huge_pmd()in the same handler to COW immediately rather than forcing a second fault. MaskVM_FAULT_FALLBACKfrom its return: a PMD-COW that splits to PTE-level is normal, but the bit is part ofVM_FAULT_ERRORand arch fault handlersBUG()on it without SIGBUS/HWPOISON/SIGSEGV. Requires exposingwp_huge_pmd()viamm/internal.h.- Free the swap slot via
should_try_to_free_swap()(hoisted frommm/memory.cintomm/internal.hso 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
swapin_folio()races) split the PMD swap entry into 512 PTE swap entries via__split_huge_pmd()and return 0. The fault retries anddo_swap_page()takes over per-PTE. This avoids returningVM_FAULT_OOMfor transient PMD-order allocation failures.
Quoted 5 lines
Quoted 1 line
Signed-off-by Usama Arif <[email protected]>Hello Usama
Amazing work.
Hi Kairui,
Thanks! and Thanks for the review comments!
Quoted 98 lines
Quoted 98 lines
--- include/linux/huge_mm.h | 9 ++ mm/huge_memory.c | 216 ++++++++++++++++++++++++++++++++++++++++ mm/internal.h | 36 +++++++ mm/memory.c | 40 +------- 4 files changed, 265 insertions(+), 36 deletions(-)include/linux/huge_mm.h +9
-0@@ -531,6 +531,15 @@ vm_fault_t do_huge_pmd_numa_page(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;mm/huge_memory.c +55
-0@@ -41,6 +41,7 @@#include <linux/pgalloc.h> #include <linux/pgalloc_tag.h> #include <linux/pagewalk.h> +#include <linux/zswap.h> #include <asm/tlb.h> #include "internal.h" @@ -2312,6 +2313,221 @@ vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)return 0 ; } +#ifdef CONFIG_THP_SWAP +/** +* do_huge_pmd_swap_page() - Handle a fault on a PMD-level swap entry. +* @vmf: Fault context. vmf->orig_pmd contains the swap PMD. +* +* A PMD swap entry is a compact encoding for HPAGE_PMD_NR consecutive swap +* slots. If the swap cache still has one PMD-sized folio covering the range, +* map it directly at PMD level. If the range has been split into per-page +* cache state, or zswap may have per-page state for it, split the PMD swap +* entry and retry at PTE granularity. +* +* Return: VM_FAULT_* flags. +*/ +vm_fault_t do_huge_pmd_swap_page (struct vm_fault *vmf) +{ +struct vm_area_struct *vma = vmf->vma; +struct mm_struct *mm = vma->vm_mm; +struct folio *folio; +struct page *page; +struct swap_info_struct *si; +unsigned long haddr = vmf->address &HPAGE_PMD_MASK ; +softleaf_t entry; +swp_entry_t swp_entry; +pmd_t pmd; +vm_fault_t ret =0 ; +bool exclusive; +rmap_t rmap_flags =RMAP_NONE ; +enum swap_pmd_cache cache_state; + + entry =softleaf_from_pmd (vmf->orig_pmd); +if (unlikely (!softleaf_is_swap (entry))) +return 0 ; + + swp_entry = entry; + +/* Prevent swapoff from happening to us. */ + si =get_swap_device (swp_entry); +if (unlikely (!si)) +return 0 ; + + cache_state =swap_pmd_cache_lookup (swp_entry, &folio); +if (cache_state ==SWAP_PMD_CACHE_SPLIT ) + goto split_fallback; +if (!folio) { +/* +* PMD swap entries encode ordinary per-page swap slots. If any +* slot is in zswap, split and let the PTE swap path load the +* range per page. Otherwise the range is all on disk and can be +* read back as one PMD-sized folio. +*/ +if (zswap_range_has_entry (swp_entry,HPAGE_PMD_NR )) + goto split_fallback; + + folio = swapin_sync(swp_entry, GFP_HIGHUSER_MOVABLE,
Quoted 8 lines
mm/huge_memory.c vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) · line 2370
Quoted 1 line
mm/huge_memory.c +1
-0+ BIT(HPAGE_PMD_ORDER), vmf, NULL, 0);Hmm, so here we always do a sync swapin which means there is no readahead, kind of make sense since PMD is huge, readahead might be not helpful and a order 0 readahead breaks following PMD. But maybe worth mention it in the commit message at least so it's easiler to follow. (partly because the name
swapin_syncis a bit confusing at the moment, we could change that name as we improve swap readahead later).
Yes, I’ll add this to the commit message. Thanks!
Yes readahead won't be useful for PMD as you said.
Quoted 8 lines
And just an idea that will it be suffient if we just use the return value of
swapin_syncdirectly? If a smaller folio covers the PMD start entry, the smaller folio is returned and you catched that just fine. If a smaller folio covers a follow-up sub-entry or a sub-entry is freed,-EBUSYis returned here and you just do the split. And it doesn't do the allocation for the first lookup check, so there should be no thrashing issue.
The main issue over here is zswap.
zswap still stores the range as order-0 objects. If we call PMD-order swapin_sync() when the cache is empty but any slot is in zswap, zswap_load() fails the large-folio load and leaves a non-uptodate PMD-sized folio in swapcache. Splitting the PMD after that is not enough unless we also delete that failed large folio, because the subsequent PTE fault can find the non-uptodate large folio and SIGBUS instead of loading the individual zswap entries.
Quoted 16 lines
mm/huge_memory.c vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) · line 2380
Quoted 11 lines
mm/huge_memory.c +10
-0+if (IS_ERR_OR_NULL (folio)) + goto split_fallback; + +/* Had to read from swap area: Major fault */ + ret =VM_FAULT_MAJOR ; +count_vm_event (PGMAJFAULT ); +count_memcg_event_mm (mm,PGMAJFAULT ); + } + + ret |=folio_lock_or_retry (folio, vmf);With PMD sized folio on a slower device, this try lock could fail much more frequently, leading to repeated faults. That's not a new issue and I remember Barry talked about it at the LSFMM this year, just I'm not sure if that will get much worse for PMD?
Agreed that PMD-sized IO can keep the folio locked longer, especially on slow swap devices. (Which hopefully are dying away :))
The current behavior matches do_swap_page(): on the normal retryable fault path it drops the fault lock and waits for the folio unlock before returning VM_FAULT_RETRY, so it should not be a tight retry loop.
The wait can be longer for 2MB IO, but the retry usually comes back to either an uptodate folio or a PMD that another fault already installed. I think this is the right baseline behavior, and any improvement here belongs with the broader swap readahead / async swapin work.
Yosry Ahmed
maintainerRe: [PATCH v3 04/11] mm: zswap: add range lookup for large-folio swapin
In reply to Usama ArifQuoted 12 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.
Quoted 5 lines
Add
zswap_range_has_entry()so PMD swap-entry consumers can make the same range decision before attempting PMD-order swapin.Signed-off-by Alexandre Ghiti <[email protected]>Signed-off-by Usama Arif <[email protected]>
I found this by accident, please CC me on zswap changes :)
Quoted 35 lines
--- include/linux/zswap.h | 7 +++++++ mm/zswap.c | 46 +++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 10 deletions(-)include/linux/zswap.h +7
-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_range_has_entry (swp_entry_t entry,unsigned int nr);#else struct zswap_lruvec_state {};@@ -69,6 +70,12 @@ static inline bool zswap_never_enabled(void)return true; } +static inline bool zswap_range_has_entry (swp_entry_t entry, +unsigned int nr) +{ +return false; +} +#endif #endif /* _LINUX_ZSWAP_H */ mm/zswap.c +0
-0
mm/zswap.c bool zswap_store(struct folio *folio) · line 1566
Quoted 8 lines
mm/zswap.c +5
-0@@ -1559,6 +1559,27 @@ bool zswap_store(struct folio *folio)return ret; } +/** +* zswap_range_has_entry() - is any slot in [entry, entry + nr) in zswap? +* @entry: base swap entry of the range +* @nr: number of contiguous slots to check +*/
Maybe zswap_is_present() as suggested here:
https://lore.kernel.org/linux-mm/CAO9r8zNni28A+9BiDgDTBRuvGTKnqH91kpbpjjeftfnKSeSOJg@mail.gmail.com/.
IIUC this interface can be used for the same purpose, we just need to pass nr=1.
mm/zswap.c bool zswap_store(struct folio *folio) · line 1578
Quoted 12 lines
mm/zswap.c +12
-0+bool zswap_range_has_entry (swp_entry_t entry,unsigned int nr) +{ +pgoff_t offset =swp_offset (entry); +XA_STATE (xas,swap_zswap_tree (entry), offset); +bool found; + +if (!nr ||zswap_never_enabled ()) +return false; + +rcu_read_lock (); + found = !!xas_find (&xas, offset + nr -1 ); +rcu_read_unlock ();
Why not xa_find()? It seems to handle RCU locking as well as retrying.
Quoted 51 lines
mm/zswap.c +19
-10+ +return found; +} +/** * zswap_load() - load a folio from zswap * @folio: folio to load @@ -1571,10 +1592,9 @@ bool zswap_store(struct folio *folio)* 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, -* but NOT marked up-to-date, so that an IO error is emitted (e.g. -* do_swap_page() will SIGBUS). +* -EIO: if 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). * * -ENOENT: if the swapped out content was not in zswap. The folio remains * locked on return. @@ -1593,13 +1613,19 @@ 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 (zswap_range_has_entry (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 v3 04/11] mm: zswap: add range lookup for large-folio swapin
Quoted 13 lines · Yosry Ahmed
Quoted 12 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.
Quoted 8 lines
Quoted 5 lines
Add
zswap_range_has_entry()so PMD swap-entry consumers can make the same range decision before attempting PMD-order swapin.Signed-off-by Alexandre Ghiti <[email protected]>Signed-off-by Usama Arif <[email protected]>I found this by accident, please CC me on zswap changes :)
Sorry about this! I will CC you in all future revisions of this series.
Quoted 47 lines
Quoted 35 lines
--- include/linux/zswap.h | 7 +++++++ mm/zswap.c | 46 +++++++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 10 deletions(-)include/linux/zswap.h +7
-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_range_has_entry (swp_entry_t entry,unsigned int nr);#else struct zswap_lruvec_state {};@@ -69,6 +70,12 @@ static inline bool zswap_never_enabled(void)return true; } +static inline bool zswap_range_has_entry (swp_entry_t entry, +unsigned int nr) +{ +return false; +} +#endif #endif /* _LINUX_ZSWAP_H */ mm/zswap.c +0
-0mm/zswap.c bool zswap_store(struct folio *folio) · line 1566
Quoted 8 lines
mm/zswap.c +5
-0@@ -1559,6 +1559,27 @@ bool zswap_store(struct folio *folio)return ret; } +/** +* zswap_range_has_entry() - is any slot in [entry, entry + nr) in zswap? +* @entry: base swap entry of the range +* @nr: number of contiguous slots to check +*/ Maybe
zswap_is_present()as suggested here:
https://lore.kernel.org/linux-mm/CAO9r8zNni28A+9BiDgDTBRuvGTKnqH91kpbpjjeftfnKSeSOJg@mail.gmail.com/.
Quoted 2 lines
IIUC this interface can be used for the same purpose, we just need to pass nr=1.
Yes, this sounds better, will change in next revision.
Quoted 16 lines
mm/zswap.c bool zswap_store(struct folio *folio) · line 1578
Quoted 13 lines
mm/zswap.c +12
-0+bool zswap_range_has_entry (swp_entry_t entry,unsigned int nr) +{ +pgoff_t offset =swp_offset (entry); +XA_STATE (xas,swap_zswap_tree (entry), offset); +bool found; + +if (!nr ||zswap_never_enabled ()) +return false; + +rcu_read_lock (); + found = !!xas_find (&xas, offset + nr -1 ); +rcu_read_unlock ();Why not
xa_find()? It seems to handle RCU locking as well as retrying.
Ack, will change in next revision, Thanks!
Quoted 51 lines
Quoted 51 lines
mm/zswap.c +19
-10+ +return found; +} +/** * zswap_load() - load a folio from zswap * @folio: folio to load @@ -1571,10 +1592,9 @@ bool zswap_store(struct folio *folio)* 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, -* but NOT marked up-to-date, so that an IO error is emitted (e.g. -* do_swap_page() will SIGBUS). +* -EIO: if 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). * * -ENOENT: if the swapped out content was not in zswap. The folio remains * locked on return. @@ -1593,13 +1613,19 @@ 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 (zswap_range_has_entry (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 v3 01/11] mm: add PMD swap entry detection support
In reply to Usama ArifQuoted 7 lines · On Fri, 3 Jul 2026 10:38:18 -0700 Usama Arif <[email protected]> wrote:
Currently when a PMD-mapped THP is swapped out, the PMD is always split into 512 PTE-level swap entries. To preserve huge page information across swap cycles, later patches will install a single PMD-level swap entry instead. This patch adds 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. Clear the exclusive overlay bit insoftleaf_from_pmd()before decoding, matching howsoft_dirtyanduffd_wpbits are already stripped.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. Provide generic no-op PMD swap exclusive fallbacks for architectures without PMD softleaf support, matching the generic PMD swap soft-dirty fallbacks.Signed-off-by Usama Arif <[email protected]>
I forgot to format the commit message before sending this patch, sorry about this, will fix in the next revision.