mm, swap: introduce swap table as swap cache (phase I)
Subsystems (16)
v3 cover mm, swap: introduce swap table as swap cache (phase I)
From: Kairui Song <[email protected]>
This is the first phase of the bigger series implementing basic infrastructures for the Swap Table idea proposed at the LSF/MM/BPF topic "Integrate swap cache, swap maps with swap allocator" [1]. To give credit where it is due, this is based on Chris Li's idea and a prototype of using cluster size atomic arrays to implement swap cache.
This phase I contains 15 patches, introduces the swap table infrastructure and uses it as the swap cache backend. By doing so, we have up to ~5-20% performance gain in throughput, RPS or build time for benchmark and workload tests. The speed up is due to less contention on the swap cache access and shallower swap cache lookup path. The cluster size is much finer-grained than the 64M address space split, which is removed in this phase I. It also unifies and cleans up the swap code base.
Each swap cluster will dynamically allocate the swap table, which is an atomic array to cover every swap slot in the cluster. It replaces the swap cache backed by XArray. In phase I, the static allocated swap_map still co-exists with the swap table. The memory usage is about the same as the original on average. A few exception test cases show about 1% higher in memory usage. In the following phases of the series, swap_map will merge into the swap table without additional memory allocation. It will result in net memory reduction compared to the original swap cache.
Testing has shown that phase I has a significant performance improvement from 8c/1G ARM machine to 48c96t/128G x86_64 servers in many practical workloads.
The full picture with a summary can be found at [2]. An older bigger series of 28 patches is posted at [3].
vm-scability test:
==================
Test with:
usemem --init-time -O -y -x -n 31 1G (4G memcg, PMEM as swap)
Before: After: System time: 219.12s 158.16s (-27.82%) Sum Throughput: 4767.13 MB/s 6128.59 MB/s (+28.55%) Single process Throughput: 150.21 MB/s 196.52 MB/s (+30.83%) Free latency: 175047.58 us 131411.87 us (-24.92%)
usemem --init-time -O -y -x -n 32 1536M (16G memory, global pressure, PMEM as swap)
Before: After: System time: 356.16s 284.68s (-20.06%) Sum Throughput: 4648.35 MB/s 5453.52 MB/s (+17.32%) Single process Throughput: 141.63 MB/s 168.35 MB/s (+18.86%) Free latency: 499907.71 us 484977.03 us (-2.99%)
This shows an improvement of more than 20% improvement in most readings.
Build kernel test:
==================
The following result matrix is from building kernel with defconfig on tmpfs with ZSWAP / ZRAM, using different memory pressure and setups. Measuring sys and real time in seconds, less is better (user time is almost identical as expected):
-j<NR> / Mem | Sys before / after | Real before / after
Using 16G ZRAM with memcg limit:
6 / 192M | 9686 / 9472 -2.21% | 2130 / 2096 -1.59%
12 / 256M | 6610 / 6451 -2.41% | 827 / 812 -1.81%
24 / 384M | 5938 / 5701 -3.37% | 414 / 405 -2.17%
48 / 768M | 4696 / 4409 -6.11% | 188 / 182 -3.19%
With 64k folio:
24 / 512M | 4222 / 4162 -1.42% | 326 / 321 -1.53%
48 / 1G | 3688 / 3622 -1.79% | 151 / 149 -1.32%
With ZSWAP with 3G memcg (using higher limit due to kmem account):
48 / 3G | 603 / 581 -3.65% | 81 / 80 -1.23%Testing extremely high global memory and schedule pressure: Using ZSWAP with 32G NVMEs in a 48c VM that has 4G memory, no memcg limit, system components take up about 1.5G already, using make -j48 to build defconfig:
Before: sys time: 2069.53s real time: 135.76s After: sys time: 2021.13s (-2.34%) real time: 134.23s (-1.12%)
On another 48c 4G memory VM, using 16G ZRAM as swap, testing make -j48 with same config:
Before: sys time: 1756.96s real time: 111.01s After: sys time: 1715.90s (-2.34%) real time: 109.51s (-1.35%)
All cases are more or less faster, and no regression even under extremely heavy global memory pressure.
Redis / Valkey bench:
=====================
The test machine is a ARM64 VM with 1536M memory 12 cores, Redis is set to use 2500M memory, and ZRAM swap size is set to 5G:
Testing with:
redis-benchmark -r 2000000 -n 2000000 -d 1024 -c 12 -P 32 -t get
no BGSAVE with BGSAVE Before: 487576.06 RPS 280016.02 RPS After: 487541.76 RPS (-0.01%) 300155.32 RPS (+7.19%)
Testing with:
redis-benchmark -r 2500000 -n 2500000 -d 1024 -c 12 -P 32 -t get
no BGSAVE with BGSAVE Before: 466789.59 RPS 281213.92 RPS After: 466402.89 RPS (-0.08%) 298411.84 RPS (+6.12%)
With BGSAVE enabled, most Redis memory will have a swap count > 1 so swap cache is heavily in use. We can see a about 6% performance gain. No BGSAVE is very slightly slower (<0.1%) due to the higher memory pressure of the co-existence of swap_map and swap table. This will be optimzed into a net gain and up to 20% gain in BGSAVE case in the following phases.
HDD swap is also ~40% faster with usemem because we removed an old contention workaround.
---
V3 changes:
There is basically no code change, mostly clean up and comment changes.
- Renames a few variable and functions [ David Hildenbrand, Chris Li ]
- Move the folio_matches_swap_entry check under unuse_pte in patch 5 [ David Hildenbrand ].
- Remove two redundant function params for the folio replace helper in patch 10, and add comment about folio setup [ David Hildenbrand ].
- Move the shmem clean up patch after the API rename patch to fix build error, no code change. [ Baolin Wang ]
- Fix build error with !SWAP and !SHMEM config. [ Klara Modin, SeongJae Park ]
- Fix a few typo and blank lines [ David Hildenbrand ]
- Minor code style change [ Chris Li ].
- Fix documentation warning from bot. [ Chris Li ]
Link to V2:
- https://lore.kernel.org/linux-mm/[email protected]/
V2 main updates:
- Added some more non-debug atomic sanity checks, which very slightly slow down the performance by a bit compared to V1 for some tests. But let's be more cautious in phase I, in case there is any hidden bug in swap, since it has a rather complex historical baggage. And phase I is a critical step for follow up swap table series [ Chris Li ]. These sanity checks can be removed very easily later or wrapped with a DEBUG_VM_SWAP as more swap table features land in.
- The code is basically same as V1, except one missing shmem statistic fixed thanks to [ Baolin Wang ]
- Added a lot of kernel docs, and more constify of sturct folio arguments, and better function names, thanks to [ David Hildenbrand ] and [ Chris Li ].
- Removed the arguable readahead change thanks to [ Chris Li ].
- Add an IRQ sanity check, have been testing with this check on for a long time but forgot to include it in V1.
Other updates:
- One VM_WARN_ON_ONCE to VM_WARN_ON_ONCE_FOLIO and minor adjust [ Barry Song ]
- Split out 4 patches from the old patches. The end result is still the same but should be easier to review [ Chris Li, David Hildenbrand ]
- Avoid some trivial blank line or variable name changes and comment fix [ Baoquan He, Chris Li, David Hildenbrand ]
- Added HDD test benchmark result [ Chris Li ]
- Rename and simplified folio_matches_swap_entry a little bit. Link to V1:
- https://lore.kernel.org/linux-mm/[email protected]/
Chris Li (1):
docs/mm: add document for swap table
Kairui Song (14):
mm, swap: use unified helper for swap cache look up
mm, swap: fix swap cache index error when retrying reclaim mm, swap: check page poison flag after locking it
mm, swap: always lock and check the swap cache folio before use mm, swap: rename and move some swap cluster definition and helpers mm, swap: tidy up swap device and cluster info helpers mm, swap: cleanup swap cache API and add kerneldoc mm/shmem, swap: remove redundant error handling for replacing folio mm, swap: wrap swap cache replacement with a helper
mm, swap: use the swap table for the swap cache and switch API mm, swap: mark swap address space ro and add context debug check mm, swap: remove contention workaround for swap cache mm, swap: implement dynamic allocation of swap table
mm, swap: use a single page for swap table when the size fits
Documentation/mm/index.rst | 1 + Documentation/mm/swap-table.rst | 72 +++++ MAINTAINERS | 2 + include/linux/swap.h | 42 --- mm/filemap.c | 2 +- mm/huge_memory.c | 15 +- mm/memory-failure.c | 2 +- mm/memory.c | 27 +- mm/migrate.c | 28 +- mm/mincore.c | 3 +- mm/page_io.c | 12 +- mm/shmem.c | 58 ++-- mm/swap.h | 311 +++++++++++++++++++--- mm/swap_state.c | 450 +++++++++++++++++-------------- mm/swap_table.h | 130 +++++++++ mm/swapfile.c | 458 +++++++++++++++++++++----------- mm/userfaultfd.c | 5 +- mm/vmscan.c | 20 +- mm/zswap.c | 9 +- 19 files changed, 1112 insertions(+), 535 deletions(-) create mode 100644 Documentation/mm/swap-table.rst create mode 100644 mm/swap_table.h
Signature · 1 line
2.51.0
PATCH v3 1/15 docs/mm: add document for swap table
From: Kairui Song <[email protected]>
From: Chris Li <[email protected]>
Swap table is the new swap cache.
--- Documentation/mm/index.rst | 1 + Documentation/mm/swap-table.rst | 72 +++++++++++++++++++++++++++++++++ MAINTAINERS | 1 + 3 files changed, 74 insertions(+) create mode 100644 Documentation/mm/swap-table.rst
Documentation/mm/index.rst +1 -0
@@ -57,6 +57,7 @@ documentation, or deleted if it has served its purpose. page_table_check remap_file_pages split_page_table_lock + swap-table transhuge unevictable-lru vmalloced-kernel-stacks
Documentation/mm/swap-table.rst new file +72 -0
72 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -16225,6 +16225,7 @@ R: Barry Song <[email protected]> R: Chris Li <[email protected]> L: [email protected] S: Maintained +F: Documentation/mm/swap-table.rst F: include/linux/swap.h F: include/linux/swapfile.h F: include/linux/swapops.h
Signature · 1 line
2.51.0
PATCH v3 2/15 mm, swap: use unified helper for swap cache look up
In reply to Kairui SongFrom: Kairui Song <[email protected]>
The swap cache lookup helper swap_cache_get_folio currently does readahead updates as well, so callers that are not doing swapin from any VMA or mapping are forced to reuse filemap helpers instead, and have to access the swap cache space directly.
So decouple readahead update with swap cache lookup. Move the readahead update part into a standalone helper. Let the caller call the readahead update helper if they do readahead. And convert all swap cache lookups to use swap_cache_get_folio.
After this commit, there are only three special cases for accessing swap cache space now: huge memory splitting, migration, and shmem replacing, because they need to lock the XArray. The following commits will wrap their accesses to the swap cache too, with special helpers.
And worth noting, currently dropbehind is not supported for anon folio, and we will never see a dropbehind folio in swap cache. The unified helper can be updated later to handle that.
While at it, add proper kernedoc for touched helpers.
No functional change.
--- mm/memory.c | 6 ++- mm/mincore.c | 3 +- mm/shmem.c | 4 +- mm/swap.h | 13 ++++-- mm/swap_state.c | 109 +++++++++++++++++++++++++---------------------- mm/swapfile.c | 11 +++-- mm/userfaultfd.c | 5 +-- 7 files changed, 81 insertions(+), 70 deletions(-)
mm/memory.c +4 -2
@@ -4660,9 +4660,11 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)if (unlikely (!si)) goto out; - folio =swap_cache_get_folio (entry, vma, vmf->address); -if (folio) + folio =swap_cache_get_folio (entry); +if (folio) { +swap_update_readahead (folio, vma, vmf->address); page =folio_file_page (folio,swp_offset (entry)); + } swapcache = folio;if (!folio) {
mm/mincore.c +1 -2
@@ -76,8 +76,7 @@ static unsigned char mincore_swap(swp_entry_t entry, bool shmem)if (!si)return 0 ; } - folio =filemap_get_entry (swap_address_space (entry), -swap_cache_index (entry)); + folio =swap_cache_get_folio (entry);if (shmem)put_swap_device (si);/* The swap cache space contains either folio, shadow or NULL */
mm/shmem.c +3 -1
@@ -2317,7 +2317,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, }/* Look it up and read it in.. */ - folio =swap_cache_get_folio (swap,NULL ,0 ); + folio =swap_cache_get_folio (swap);if (!folio) {if (data_race (si->flags &SWP_SYNCHRONOUS_IO )) {/* Direct swapin skipping swap cache & readahead */
@@ -2342,6 +2342,8 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index,count_vm_event (PGMAJFAULT );count_memcg_event_mm (fault_mm,PGMAJFAULT ); } + }else { +swap_update_readahead (folio,NULL ,0 ); }if (order >folio_order (folio)) {
mm/swap.h +9 -4
@@ -62,8 +62,7 @@ void delete_from_swap_cache(struct folio *folio);void clear_shadow_from_swap_cache (int type,unsigned long begin,unsigned long end);void swapcache_clear (struct swap_info_struct *si,swp_entry_t entry,int nr); -struct folio *swap_cache_get_folio (swp_entry_t entry, -struct vm_area_struct *vma,unsigned long addr); +struct folio *swap_cache_get_folio (swp_entry_t entry);struct folio *read_swap_cache_async (swp_entry_t entry,gfp_t gfp_mask,struct vm_area_struct *vma,unsigned long addr,struct swap_iocb **plug);
@@ -74,6 +73,8 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t flag, struct mempolicy *mpol, pgoff_t ilx);struct folio *swapin_readahead (swp_entry_t entry,gfp_t flag,struct vm_fault *vmf); +void swap_update_readahead (struct folio *folio,struct vm_area_struct *vma, +unsigned long addr);static inline unsigned int folio_swap_flags (struct folio *folio) {
@@ -159,6 +160,11 @@ static inline struct folio *swapin_readahead(swp_entry_t swp, gfp_t gfp_mask,return NULL ; } +static inline void swap_update_readahead (struct folio *folio, +struct vm_area_struct *vma,unsigned long addr) +{ +} +static inline int swap_writeout (struct folio *folio,struct swap_iocb **swap_plug) {
@@ -169,8 +175,7 @@ static inline void swapcache_clear(struct swap_info_struct *si, swp_entry_t entr { } -static inline struct folio *swap_cache_get_folio (swp_entry_t entry, -struct vm_area_struct *vma,unsigned long addr) +static inline struct folio *swap_cache_get_folio (swp_entry_t entry) {return NULL ; }
mm/swap_state.c +57 -52
132 lines not on this page · raw message
mm/swapfile.c +5 -6
@@ -213,15 +213,14 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si, unsigned long offset, unsigned long flags) {swp_entry_t entry =swp_entry (si->type, offset); -struct address_space *address_space =swap_address_space (entry);struct swap_cluster_info *ci;struct folio *folio;int ret, nr_pages;bool need_reclaim; again: - folio =filemap_get_folio (address_space,swap_cache_index (entry)); -if (IS_ERR (folio)) + folio =swap_cache_get_folio (entry); +if (!folio)return 0 ; nr_pages =folio_nr_pages (folio);
@@ -2131,7 +2130,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,pte_unmap (pte); pte =NULL ; - folio =swap_cache_get_folio (entry, vma, addr); + folio =swap_cache_get_folio (entry);if (!folio) {struct vm_fault vmf = { .vma = vma,
@@ -2357,8 +2356,8 @@ static int try_to_unuse(unsigned int type) (i = find_next_to_unuse(si, i)) != 0) { entry =swp_entry (type, i); - folio =filemap_get_folio (swap_address_space (entry),swap_cache_index (entry)); -if (IS_ERR (folio)) + folio =swap_cache_get_folio (entry); +if (!folio)continue ;/*
mm/userfaultfd.c +2 -3
@@ -1489,9 +1489,8 @@ static long move_pages_ptes(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd* separately to allow proper handling. */ if (!src_folio) - folio =filemap_get_folio (swap_address_space (entry), -swap_cache_index (entry)); -if (!IS_ERR_OR_NULL (folio)) { + folio =swap_cache_get_folio (entry); +if (folio) {if (folio_test_large (folio)) { ret = -EBUSY ;folio_put (folio);
Signature · 1 line
2.51.0
PATCH v3 3/15 mm, swap: fix swap cache index error when retrying reclaim
In reply to Kairui SongFrom: Kairui Song <[email protected]>
The allocator will reclaim cached slots while scanning. Currently, it will try again if reclaim found a folio that is already removed from the swap cache due to a race. But the following lookup will be using the wrong index. It won't cause any OOB issue since the swap cache index is truncated upon lookup, but it may lead to reclaiming of an irrelevant folio.
This should not cause a measurable issue, but we should fix it.
--- mm/swapfile.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/swapfile.c +4 -4
@@ -212,7 +212,7 @@ static bool swap_is_last_map(struct swap_info_struct *si,static int __try_to_reclaim_swap (struct swap_info_struct *si,unsigned long offset,unsigned long flags) { -swp_entry_t entry =swp_entry (si->type, offset); +const swp_entry_t entry =swp_entry (si->type, offset);struct swap_cluster_info *ci;struct folio *folio;int ret, nr_pages;
@@ -240,13 +240,13 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,* Offset could point to the middle of a large folio, or folio * may no longer point to the expected offset before it's locked. */ - entry = folio->swap; -if (offset <swp_offset (entry) || offset >=swp_offset (entry) + nr_pages) { +if (offset <swp_offset (folio->swap) || + offset >=swp_offset (folio->swap) + nr_pages) {folio_unlock (folio);folio_put (folio); goto again; } - offset =swp_offset (entry); + offset =swp_offset (folio->swap); need_reclaim = ((flags & TTRS_ANYWAY) || ((flags & TTRS_UNMAPPED) && !folio_mapped(folio)) ||
Signature · 1 line
2.51.0
PATCH v3 4/15 mm, swap: check page poison flag after locking it
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Instead of checking the poison flag only in the fast swap cache lookup path, always check the poison flags after locking a swap cache folio.
There are two reasons to do so.
The folio is unstable and could be removed from the swap cache anytime, so it's totally possible that the folio is no longer the backing folio of a swap entry, and could be an irrelevant poisoned folio. We might mistakenly kill a faulting process.
And it's totally possible or even common for the slow swap in path (swapin_readahead) to bring in a cached folio. The cache folio could be poisoned, too. Only checking the poison flag in the fast path will miss such folios.
The race window is tiny, so it's very unlikely to happen, though. While at it, also add a unlikely prefix.
--- mm/memory.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
mm/memory.c +11 -11
@@ -4661,10 +4661,8 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) goto out; folio =swap_cache_get_folio (entry); -if (folio) { +if (folio)swap_update_readahead (folio, vma, vmf->address); - page =folio_file_page (folio,swp_offset (entry)); - } swapcache = folio;if (!folio) {
@@ -4735,20 +4733,13 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) ret =VM_FAULT_MAJOR ;count_vm_event (PGMAJFAULT );count_memcg_event_mm (vma->vm_mm,PGMAJFAULT ); - page =folio_file_page (folio,swp_offset (entry)); - }else if (PageHWPoison (page)) { -/* -* hwpoisoned dirty swapcache pages are kept for killing -* owner processes (which may be unknown at hwpoison time) -*/ - ret =VM_FAULT_HWPOISON ; - goto out_release; } ret |=folio_lock_or_retry (folio, vmf);if (ret &VM_FAULT_RETRY ) goto out_release; + page =folio_file_page (folio,swp_offset (entry));if (swapcache) {/* * Make sure folio_free_swap() or swapoff did not release the
@@ -4761,6 +4752,15 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) page_swap_entry(page).val != entry.val)) goto out_page; +if (unlikely (PageHWPoison (page))) { +/* +* hwpoisoned dirty swapcache pages are kept for killing +* owner processes (which may be unknown at hwpoison time) +*/ + ret =VM_FAULT_HWPOISON ; + goto out_page; + } +/* * KSM sometimes has to copy on read faults, for example, if * folio->index of non-ksm folios would be nonlinear inside the
Signature · 1 line
2.51.0
PATCH v3 5/15 mm, swap: always lock and check the swap cache folio before use
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Swap cache lookup only increases the reference count of the returned folio. That's not enough to ensure a folio is stable in the swap cache, so the folio could be removed from the swap cache at any time. The caller should always lock and check the folio before using it.
We have just documented this in kerneldoc, now introduce a helper for swap cache folio verification with proper sanity checks.
Also, sanitize a few current users to use this convention and the new helper for easier debugging. They were not having observable problems yet, only trivial issues like wasted CPU cycles on swapoff or reclaiming. They would fail in some other way, but it is still better to always follow this convention to make things robust and make later commits easier to do.
--- mm/memory.c | 3 +-- mm/swap.h | 27 +++++++++++++++++++++++++++ mm/swap_state.c | 7 +++++-- mm/swapfile.c | 11 ++++++++--- 4 files changed, 41 insertions(+), 7 deletions(-)
mm/memory.c +1 -2
@@ -4748,8 +4748,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)* swapcache, we need to check that the page's swap has not * changed. */ -if (unlikely (!folio_test_swapcache (folio) || -page_swap_entry (page).val != entry.val)) +if (unlikely (!folio_matches_swap_entry (folio, entry))) goto out_page;if (unlikely (PageHWPoison (page))) {
mm/swap.h +27 -0
@@ -52,6 +52,28 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)return swp_offset (entry) &SWAP_ADDRESS_SPACE_MASK ; } +/** +* folio_matches_swap_entry - Check if a folio matches a given swap entry. +* @folio: The folio. +* @entry: The swap entry to check against. +* +* Context: The caller should have the folio locked to ensure it's stable +* and nothing will move it in or out of the swap cache. +* Return: true or false. +*/ +static inline bool folio_matches_swap_entry (const struct folio *folio, +swp_entry_t entry) +{ +swp_entry_t folio_entry = folio->swap; +long nr_pages =folio_nr_pages (folio); + +VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio); +if (!folio_test_swapcache (folio)) +return false; +VM_WARN_ON_ONCE_FOLIO (!IS_ALIGNED (folio_entry.val, nr_pages), folio); +return folio_entry.val ==round_down (entry.val, nr_pages); +} +void show_swap_cache_info (void );void *get_shadow_from_swap_cache (swp_entry_t entry); int add_to_swap_cache(struct folio *folio, swp_entry_t entry,
@@ -144,6 +166,11 @@ static inline pgoff_t swap_cache_index(swp_entry_t entry)return 0 ; } +static inline bool folio_matches_swap_entry (const struct folio *folio,swp_entry_t entry) +{ +return false; +} +static inline void show_swap_cache_info (void ) { }
mm/swap_state.c +5 -2
@@ -79,7 +79,7 @@ void show_swap_cache_info(void)* with reference count or locks. * Return: Returns the found folio on success, NULL otherwise. The caller * must lock and check if the folio still matches the swap entry before -* use. +* use (e.g. with folio_matches_swap_entry). */ struct folio *swap_cache_get_folio (swp_entry_t entry) {
@@ -346,7 +346,10 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,for (;;) {int err; -/* Check the swap cache in case the folio is already there */ +/* +* Check the swap cache first, if a cached folio is found, +* return it unlocked. The caller will lock and check it. +*/ folio =swap_cache_get_folio (entry);if (folio) goto got_folio;
mm/swapfile.c +8 -3
@@ -240,14 +240,12 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,* Offset could point to the middle of a large folio, or folio * may no longer point to the expected offset before it's locked. */ -if (offset <swp_offset (folio->swap) || - offset >=swp_offset (folio->swap) + nr_pages) { +if (!folio_matches_swap_entry (folio, entry)) {folio_unlock (folio);folio_put (folio); goto again; } offset =swp_offset (folio->swap); - need_reclaim = ((flags &TTRS_ANYWAY ) || ((flags &TTRS_UNMAPPED ) && !folio_mapped (folio)) || ((flags &TTRS_FULL ) &&mem_cgroup_swap_full (folio)));
@@ -2004,6 +2002,13 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,bool hwpoisoned = false;int ret =1 ; +/* +* If the folio is removed from swap cache by others, continue to +* unuse other PTEs. try_to_unuse may try again if we missed this one. +*/ +if (!folio_matches_swap_entry (folio, entry)) +return 0 ; + swapcache = folio; folio =ksm_might_need_to_copy (folio, vma, addr);if (unlikely (!folio))
Signature · 1 line
2.51.0
PATCH v3 6/15 mm, swap: rename and move some swap cluster definition and helpers
In reply to Kairui SongFrom: Kairui Song <[email protected]>
No feature change, move cluster related definitions and helpers to mm/swap.h, also tidy up and add a "swap_" prefix for cluster lock/unlock helpers, so they can be used outside of swap files. And while at it, add kerneldoc.
--- include/linux/swap.h | 34 ---------------- mm/swap.h | 70 ++++++++++++++++++++++++++++++++ mm/swapfile.c | 97 +++++++++++++------------------------------- 3 files changed, 99 insertions(+), 102 deletions(-)
include/linux/swap.h +0 -34
@@ -235,40 +235,6 @@ enum {/* Special value in each swap_map continuation */ #define SWAP_CONT_MAX 0x7f/* Max count */ -/* -* We use this to track usage of a cluster. A cluster is a block of swap disk -* space with SWAPFILE_CLUSTER pages long and naturally aligns in disk. All -* free clusters are organized into a list. We fetch an entry from the list to -* get a free cluster. -* -* The flags field determines if a cluster is free. This is -* protected by cluster lock. -*/ -struct swap_cluster_info { -spinlock_t lock;/* -* Protect swap_cluster_info fields -* other than list, and swap_info_struct->swap_map -* elements corresponding to the swap cluster. -*/ -u16 count; -u8 flags; -u8 order; -struct list_head list; -}; - -/* All on-list cluster must have a non-zero flag. */ -enum swap_cluster_flags { -CLUSTER_FLAG_NONE =0 ,/* For temporary off-list cluster */ -CLUSTER_FLAG_FREE , -CLUSTER_FLAG_NONFULL , -CLUSTER_FLAG_FRAG , -/* Clusters with flags above are allocatable */ -CLUSTER_FLAG_USABLE =CLUSTER_FLAG_FRAG , -CLUSTER_FLAG_FULL , -CLUSTER_FLAG_DISCARD , -CLUSTER_FLAG_MAX , -}; -/* * The first page in the swap file is the swap header, which is always marked * bad to prevent it from being allocated as an entry. This also prevents the
mm/swap.h +70 -0
80 lines not on this page · raw message
mm/swapfile.c +29 -68
264 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v3 7/15 mm, swap: tidy up swap device and cluster info helpers
In reply to Kairui SongFrom: Kairui Song <[email protected]>
swp_swap_info is the most commonly used helper for retrieving swap info. It has an internal check that may lead to a NULL return value, but almost none of its caller checks the return value, making the internal check pointless. In fact, most of these callers already ensured the entry is valid and never expect a NULL value.
Tidy this up and improve the function names. If the caller can make sure the swap entry/type is valid and the device is pinned, use the new introduced __swap_entry_to_info/__swap_type_to_info instead. They have more debug sanity checks and lower overhead as they are inlined.
Callers that may expect a NULL value should use
swap_entry_to_info/swap_type_to_info instead.
No feature change. The rearranged codes should have had no effect, or they should have been hitting NULL de-ref bugs already. Only some new sanity checks are added so potential issues may show up in debug build.
The new helpers will be frequently used with swap table later when working with swap cache folios. A locked swap cache folio ensures the entries are valid and stable so these helpers are very helpful.
--- include/linux/swap.h | 6 ------ mm/page_io.c | 12 ++++++------ mm/swap.h | 38 +++++++++++++++++++++++++++++++++----- mm/swap_state.c | 4 ++-- mm/swapfile.c | 37 +++++++++++++++++++------------------ 5 files changed, 60 insertions(+), 37 deletions(-)
include/linux/swap.h +0 -6
@@ -479,7 +479,6 @@ extern sector_t swapdev_block(int, pgoff_t);extern int __swap_count (swp_entry_t entry);extern bool swap_entry_swapped (struct swap_info_struct *si,swp_entry_t entry);extern int swp_swapcount (swp_entry_t entry); -struct swap_info_struct *swp_swap_info (swp_entry_t entry);struct backing_dev_info ;extern int init_swap_address_space (unsigned int type,unsigned long nr_pages);extern void exit_swap_address_space (unsigned int type);
@@ -492,11 +491,6 @@ static inline void put_swap_device(struct swap_info_struct *si) }#else /* CONFIG_SWAP */ -static inline struct swap_info_struct *swp_swap_info (swp_entry_t entry) -{ -return NULL ; -} -static inline struct swap_info_struct *get_swap_device (swp_entry_t entry) {return NULL ;
mm/page_io.c +6 -6
@@ -204,7 +204,7 @@ static bool is_folio_zero_filled(struct folio *folio)static void swap_zeromap_folio_set (struct folio *folio) {struct obj_cgroup *objcg =get_obj_cgroup_from_folio (folio); -struct swap_info_struct *sis =swp_swap_info (folio->swap); +struct swap_info_struct *sis =__swap_entry_to_info (folio->swap);int nr_pages =folio_nr_pages (folio);swp_entry_t entry;unsigned int i;
@@ -223,7 +223,7 @@ static void swap_zeromap_folio_set(struct folio *folio)static void swap_zeromap_folio_clear (struct folio *folio) { -struct swap_info_struct *sis =swp_swap_info (folio->swap); +struct swap_info_struct *sis =__swap_entry_to_info (folio->swap);swp_entry_t entry;unsigned int i;
@@ -374,7 +374,7 @@ static void sio_write_complete(struct kiocb *iocb, long ret)static void swap_writepage_fs (struct folio *folio,struct swap_iocb **swap_plug) {struct swap_iocb *sio = swap_plug ? *swap_plug :NULL ; -struct swap_info_struct *sis =swp_swap_info (folio->swap); +struct swap_info_struct *sis =__swap_entry_to_info (folio->swap);struct file *swap_file = sis->swap_file;loff_t pos =swap_dev_pos (folio->swap);
@@ -446,7 +446,7 @@ static void swap_writepage_bdev_async(struct folio *folio,void __swap_writepage (struct folio *folio,struct swap_iocb **swap_plug) { -struct swap_info_struct *sis =swp_swap_info (folio->swap); +struct swap_info_struct *sis =__swap_entry_to_info (folio->swap);VM_BUG_ON_FOLIO (!folio_test_swapcache (folio), folio);/*
@@ -537,7 +537,7 @@ static bool swap_read_folio_zeromap(struct folio *folio)static void swap_read_folio_fs (struct folio *folio,struct swap_iocb **plug) { -struct swap_info_struct *sis =swp_swap_info (folio->swap); +struct swap_info_struct *sis =__swap_entry_to_info (folio->swap);struct swap_iocb *sio =NULL ;loff_t pos =swap_dev_pos (folio->swap);
@@ -608,7 +608,7 @@ static void swap_read_folio_bdev_async(struct folio *folio,void swap_read_folio (struct folio *folio,struct swap_iocb **plug) { -struct swap_info_struct *sis =swp_swap_info (folio->swap); +struct swap_info_struct *sis =__swap_entry_to_info (folio->swap);bool synchronous = sis->flags &SWP_SYNCHRONOUS_IO ;bool workingset =folio_test_workingset (folio);unsigned long pflags;
mm/swap.h +33 -5
@@ -15,6 +15,8 @@ extern int page_cluster;#define swap_entry_order (order) 0#endif +extern struct swap_info_struct *swap_info[]; +/* * We use this to track usage of a cluster. A cluster is a block of swap disk * space with SWAPFILE_CLUSTER pages long and naturally aligns in disk. All
@@ -53,9 +55,29 @@ enum swap_cluster_flags {#include <linux/swapops.h> /* for swp_offset */ #include <linux/blk_types.h> /* for bio_end_io_t */ -static inline struct swap_cluster_info *swp_offset_cluster ( +/* +* Callers of all helpers below must ensure the entry, type, or offset is +* valid, and protect the swap device with reference count or locks. +*/ +static inline struct swap_info_struct *__swap_type_to_info (int type) +{ +struct swap_info_struct *si; + + si =READ_ONCE (swap_info[type]);/* rcu_dereference() */ +VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ +return si; +} + +static inline struct swap_info_struct *__swap_entry_to_info (swp_entry_t entry) +{ +return __swap_type_to_info (swp_type (entry)); +} + +static inline struct swap_cluster_info *__swap_offset_to_cluster (struct swap_info_struct *si,pgoff_t offset) { +VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ +VM_WARN_ON_ONCE (offset >= si->max);return &si->cluster_info[offset /SWAPFILE_CLUSTER ]; }
@@ -70,8 +92,9 @@ static inline struct swap_cluster_info *swp_offset_cluster(static inline struct swap_cluster_info *swap_cluster_lock (struct swap_info_struct *si,unsigned long offset) { -struct swap_cluster_info *ci =swp_offset_cluster (si, offset); +struct swap_cluster_info *ci =__swap_offset_to_cluster (si, offset); +VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ spin_lock (&ci->lock);return ci; }
@@ -170,7 +193,7 @@ void swap_update_readahead(struct folio *folio, struct vm_area_struct *vma,static inline unsigned int folio_swap_flags (struct folio *folio) { -return swp_swap_info (folio->swap)->flags; +return __swap_entry_to_info (folio->swap)->flags; }/*
@@ -181,7 +204,7 @@ static inline unsigned int folio_swap_flags(struct folio *folio)static inline int swap_zeromap_batch (swp_entry_t entry,int max_nr,bool *is_zeromap) { -struct swap_info_struct *sis =swp_swap_info (entry); +struct swap_info_struct *sis =__swap_entry_to_info (entry);unsigned long start =swp_offset (entry);unsigned long end = start + max_nr;bool first_bit;
@@ -200,7 +223,7 @@ static inline int swap_zeromap_batch(swp_entry_t entry, int max_nr,static inline int non_swapcache_batch (swp_entry_t entry,int max_nr) { -struct swap_info_struct *si =swp_swap_info (entry); +struct swap_info_struct *si =__swap_entry_to_info (entry);pgoff_t offset =swp_offset (entry);int i;
@@ -219,6 +242,11 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr)#else /* CONFIG_SWAP */ struct swap_iocb ; +static inline struct swap_info_struct *__swap_entry_to_info (swp_entry_t entry) +{ +return NULL ; +} +static inline void swap_read_folio (struct folio *folio,struct swap_iocb **plug) { }
mm/swap_state.c +2 -2
@@ -336,7 +336,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask, struct mempolicy *mpol, pgoff_t ilx, bool *new_page_allocated, bool skip_if_exists) { - struct swap_info_struct *si = swp_swap_info(entry); + struct swap_info_struct *si = __swap_entry_to_info(entry);struct folio *folio;struct folio *new_folio =NULL ;struct folio *result =NULL ;
@@ -560,7 +560,7 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,unsigned long offset = entry_offset;unsigned long start_offset, end_offset;unsigned long mask; -struct swap_info_struct *si =swp_swap_info (entry); +struct swap_info_struct *si =__swap_entry_to_info (entry);struct blk_plug plug;struct swap_iocb *splug =NULL ;bool page_allocated;
mm/swapfile.c +19 -18
@@ -102,7 +102,7 @@ static PLIST_HEAD(swap_active_head);static struct plist_head *swap_avail_heads;static DEFINE_SPINLOCK (swap_avail_lock); -static struct swap_info_struct *swap_info[MAX_SWAPFILES ]; +struct swap_info_struct *swap_info[MAX_SWAPFILES ];static DEFINE_MUTEX (swapon_mutex);
@@ -124,14 +124,20 @@ static DEFINE_PER_CPU(struct percpu_swap_cluster, percpu_swap_cluster) = { .lock = INIT_LOCAL_LOCK(), }; -static struct swap_info_struct *swap_type_to_swap_info (int type) +/* May return NULL on invalid type, caller must check for NULL return */ +static struct swap_info_struct *swap_type_to_info (int type) {if (type >=MAX_SWAPFILES )return NULL ; -return READ_ONCE (swap_info[type]);/* rcu_dereference() */ } +/* May return NULL on invalid entry, caller must check for NULL return */ +static struct swap_info_struct *swap_entry_to_info (swp_entry_t entry) +{ +return swap_type_to_info (swp_type (entry)); +} +static inline unsigned char swap_count (unsigned char ent) {return ent & ~SWAP_HAS_CACHE ;/* may include COUNT_CONTINUED flag */
@@ -341,7 +347,7 @@ offset_to_swap_extent(struct swap_info_struct *sis, unsigned long offset)sector_t swap_folio_sector (struct folio *folio) { -struct swap_info_struct *sis =swp_swap_info (folio->swap); +struct swap_info_struct *sis =__swap_entry_to_info (folio->swap);struct swap_extent *se;sector_t sector;pgoff_t offset;
@@ -1299,7 +1305,7 @@ static struct swap_info_struct *_swap_info_get(swp_entry_t entry)if (!entry.val) goto out; - si =swp_swap_info (entry); + si =swap_entry_to_info (entry);if (!si) goto bad_nofile;if (data_race (!(si->flags &SWP_USED )))
@@ -1414,7 +1420,7 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)if (!entry.val) goto out; - si =swp_swap_info (entry); + si =swap_entry_to_info (entry);if (!si) goto bad_nofile;if (!get_swap_device_info (si))
@@ -1537,7 +1543,7 @@ static void swap_entries_free(struct swap_info_struct *si,unsigned char *map_end = map + nr_pages;/* It should never free entries across different clusters */ -VM_BUG_ON (ci !=swp_offset_cluster (si, offset + nr_pages -1 )); +VM_BUG_ON (ci !=__swap_offset_to_cluster (si, offset + nr_pages -1 ));VM_BUG_ON (cluster_is_empty (ci));VM_BUG_ON (ci->count < nr_pages);
@@ -1595,7 +1601,7 @@ void put_swap_folio(struct folio *folio, swp_entry_t entry)int __swap_count (swp_entry_t entry) { -struct swap_info_struct *si =swp_swap_info (entry); +struct swap_info_struct *si =__swap_entry_to_info (entry);pgoff_t offset =swp_offset (entry);return swap_count (si->swap_map[offset]);
@@ -1826,7 +1832,7 @@ void free_swap_and_cache_nr(swp_entry_t entry, int nr)swp_entry_t get_swap_page_of_type (int type) { -struct swap_info_struct *si =swap_type_to_swap_info (type); +struct swap_info_struct *si =swap_type_to_info (type);unsigned long offset;swp_entry_t entry = {0 };
@@ -1907,7 +1913,7 @@ int find_first_swap(dev_t *device)*/ sector_t swapdev_block (int type,pgoff_t offset) { -struct swap_info_struct *si =swap_type_to_swap_info (type); +struct swap_info_struct *si =swap_type_to_info (type);struct swap_extent *se;if (!si || !(si->flags &SWP_WRITEOK ))
@@ -2836,7 +2842,7 @@ static void *swap_start(struct seq_file *swap, loff_t *pos)if (!l)return SEQ_START_TOKEN ; -for (type =0 ; (si =swap_type_to_swap_info (type)); type++) { +for (type =0 ; (si =swap_type_to_info (type)); type++) {if (!(si->flags &SWP_USED ) || !si->swap_map)continue ;if (!--l)
@@ -2857,7 +2863,7 @@ static void *swap_next(struct seq_file *swap, void *v, loff_t *pos) type = si->type +1 ; ++(*pos); -for (; (si =swap_type_to_swap_info (type)); type++) { +for (; (si =swap_type_to_info (type)); type++) {if (!(si->flags &SWP_USED ) || !si->swap_map)continue ;return si;
@@ -3530,7 +3536,7 @@ static int __swap_duplicate(swp_entry_t entry, unsigned char usage, int nr)unsigned char has_cache;int err, i; - si =swp_swap_info (entry); + si =swap_entry_to_info (entry);if (WARN_ON_ONCE (!si)) {pr_err ("%s%08lx\n" , Bad_file, entry.val);return -EINVAL ;
@@ -3645,11 +3651,6 @@ void swapcache_clear(struct swap_info_struct *si, swp_entry_t entry, int nr)swap_entries_put_cache (si, entry, nr); } -struct swap_info_struct *swp_swap_info (swp_entry_t entry) -{ -return swap_type_to_swap_info (swp_type (entry)); -} -/* * add_swap_count_continuation - called when a swap count is duplicated * beyond SWAP_MAP_MAX, it allocates a new page and links that to the entry's
Signature · 1 line
2.51.0
PATCH v3 8/15 mm, swap: cleanup swap cache API and add kerneldoc
In reply to Kairui SongFrom: Kairui Song <[email protected]>
In preparation for replacing the swap cache backend with the swap table, clean up and add proper kernel doc for all swap cache APIs. Now all swap cache APIs are well-defined with consistent names.
No feature change, only renaming and documenting.
--- mm/filemap.c | 2 +- mm/memory-failure.c | 2 +- mm/memory.c | 2 +- mm/swap.h | 48 ++++++++++++++----------- mm/swap_state.c | 86 ++++++++++++++++++++++++++++++++------------- mm/swapfile.c | 8 ++--- mm/vmscan.c | 2 +- mm/zswap.c | 2 +- 8 files changed, 98 insertions(+), 54 deletions(-)
mm/filemap.c +1 -1
@@ -4525,7 +4525,7 @@ static void filemap_cachestat(struct address_space *mapping,* invalidation, so there might not be * a shadow in the swapcache (yet). */ - shadow =get_shadow_from_swap_cache (swp); + shadow =swap_cache_get_shadow (swp);if (!shadow) goto resched; }
mm/memory-failure.c +1 -1
@@ -1127,7 +1127,7 @@ static int me_swapcache_clean(struct page_state *ps, struct page *p)struct folio *folio =page_folio (p);int ret; -delete_from_swap_cache (folio); +swap_cache_del_folio (folio); ret =delete_from_lru_cache (folio) ?MF_FAILED :MF_RECOVERED ;folio_unlock (folio);
mm/memory.c +1 -1
@@ -4699,7 +4699,7 @@ vm_fault_t do_swap_page(struct vm_fault *vmf)memcg1_swapin (entry, nr_pages); - shadow =get_shadow_from_swap_cache (entry); + shadow =swap_cache_get_shadow (entry);if (shadow)workingset_refault (folio, shadow);
mm/swap.h +27 -21
72 lines not on this page · raw message
mm/swap_state.c +62 -24
142 lines not on this page · raw message
mm/swapfile.c +4 -4
@@ -266,7 +266,7 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,if (!need_reclaim) goto out_unlock; -delete_from_swap_cache (folio); +swap_cache_del_folio (folio);folio_set_dirty (folio); ret = nr_pages; out_unlock:
@@ -1123,7 +1123,7 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,swap_slot_free_notify (si->bdev, offset); offset++; } -clear_shadow_from_swap_cache (si->type, begin, end); +swap_cache_clear_shadow (si->type, begin, end);/* * Make sure that try_to_unuse() observes si->inuse_pages reaching 0
@@ -1288,7 +1288,7 @@ int folio_alloc_swap(struct folio *folio, gfp_t gfp)* TODO: this could cause a theoretical memory reclaim * deadlock in the swap out path. */ -if (add_to_swap_cache (folio, entry, gfp | __GFP_NOMEMALLOC,NULL )) +if (swap_cache_add_folio (folio, entry, gfp | __GFP_NOMEMALLOC,NULL )) goto out_free;return 0 ;
@@ -1758,7 +1758,7 @@ bool folio_free_swap(struct folio *folio)if (folio_swapped (folio))return false; -delete_from_swap_cache (folio); +swap_cache_del_folio (folio);folio_set_dirty (folio);return true; }
mm/vmscan.c +1 -1
@@ -776,7 +776,7 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,if (reclaimed && !mapping_exiting (mapping)) shadow =workingset_eviction (folio, target_memcg); -__delete_from_swap_cache (folio, swap, shadow); +__swap_cache_del_folio (folio, swap, shadow);memcg1_swapout (folio, swap);xa_unlock_irq (&mapping->i_pages);put_swap_folio (folio, swap);
mm/zswap.c +1 -1
@@ -1069,7 +1069,7 @@ static int zswap_writeback_entry(struct zswap_entry *entry, out:if (ret && ret != -EEXIST ) { -delete_from_swap_cache (folio); +swap_cache_del_folio (folio);folio_unlock (folio); }folio_put (folio);
Signature · 1 line
2.51.0
PATCH v3 9/15 mm/shmem, swap: remove redundant error handling for replacing folio
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Shmem may replace a folio in the swap cache if the cached one doesn't fit the swapin's GFP zone. When doing so, shmem has already double checked that the swap cache folio is locked, still has the swap cache flag set, and contains the wanted swap entry. So it is impossible to fail due to an XArray mismatch. There is even a comment for that.
Delete the defensive error handling path, and add a WARN_ON instead:
if that happened, something has broken the basic principle of how the swap cache works, we should catch and fix that.
--- mm/shmem.c | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-)
mm/shmem.c +12 -30
@@ -1661,13 +1661,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug, }/* -* The delete_from_swap_cache() below could be left for +* The swap_cache_del_folio() below could be left for * shrink_folio_list()'s folio_free_swap() to dispose of; * but I'm a little nervous about letting this folio out of * shmem_writeout() in a hybrid half-tmpfs-half-swap state * e.g. folio_mapping(folio) might give an unexpected answer. */ -delete_from_swap_cache (folio); +swap_cache_del_folio (folio); goto redirty; }if (nr_pages >1 )
@@ -2045,7 +2045,7 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode, new->swap = entry;memcg1_swapin (entry, nr_pages); - shadow =get_shadow_from_swap_cache (entry); + shadow =swap_cache_get_shadow (entry);if (shadow)workingset_refault (new, shadow);folio_add_lru (new);
@@ -2121,35 +2121,17 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,/* Swap cache still stores N entries instead of a high-order entry */ xa_lock_irq (&swap_mapping->i_pages);for (i =0 ; i < nr_pages; i++) { -void *item =xas_load (&xas); - -if (item != old) { - error = -ENOENT ; -break ; - } - -xas_store (&xas, new); +WARN_ON_ONCE (xas_store (&xas, new));xas_next (&xas); } -if (!error) { -mem_cgroup_replace_folio (old, new); -shmem_update_stats (new, nr_pages); -shmem_update_stats (old, -nr_pages); - }xa_unlock_irq (&swap_mapping->i_pages); -if (unlikely (error)) { -/* -* Is this possible? I think not, now that our callers -* check both the swapcache flag and folio->private -* after getting the folio lock; but be defensive. -* Reverse old to newpage for clear and free. -*/ - old = new; - }else { -folio_add_lru (new); - *foliop = new; - } +mem_cgroup_replace_folio (old, new); +shmem_update_stats (new, nr_pages); +shmem_update_stats (old, -nr_pages); + +folio_add_lru (new); + *foliop = new;folio_clear_swapcache (old); old->private =NULL ;
@@ -2183,7 +2165,7 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, nr_pages =folio_nr_pages (folio);folio_wait_writeback (folio);if (!skip_swapcache) -delete_from_swap_cache (folio); +swap_cache_del_folio (folio);/* * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks)
@@ -2422,7 +2404,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, folio->swap.val =0 ;swapcache_clear (si, swap, nr_pages); }else { -delete_from_swap_cache (folio); +swap_cache_del_folio (folio); }folio_mark_dirty (folio);swap_free_nr (swap, nr_pages);
Signature · 1 line
2.51.0
PATCH v3 10/15 mm, swap: wrap swap cache replacement with a helper
In reply to Kairui SongFrom: Kairui Song <[email protected]>
There are currently three swap cache users that are trying to replace an existing folio with a new one: huge memory splitting, migration, and shmem replacement. What they are doing is quite similar.
Introduce a common helper for this. In later commits, this can be easily switched to use the swap table by updating this helper.
The newly added helper also makes the swap cache API better defined, and make debugging easier by adding a few more debug checks.
Migration and shmem replace are meant to clone the folio, including content, swap entry value, and flags. And splitting will adjust each sub folio's swap entry according to order, which could be non-uniform in the future. So document it clearly that it's the caller's responsibility to set up the new folio's swap entries and flags before calling the helper. The helper will just follow the new folio's entry value.
This also prepares for replacing high-order folios in the swap cache. Currently, only splitting to order 0 is allowed for swap cache folios. Using the new helper, we can handle high-order folio splitting better.
--- mm/huge_memory.c | 4 +--- mm/migrate.c | 11 +++-------- mm/shmem.c | 10 ++-------- mm/swap.h | 5 +++++ mm/swap_state.c | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 19 deletions(-)
mm/huge_memory.c +1 -3
@@ -3798,9 +3798,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* NOTE: shmem in swap cache is not supported yet. */ if (swap_cache) { -__xa_store (&swap_cache->i_pages, -swap_cache_index (new_folio->swap), - new_folio,0 ); +__swap_cache_replace_folio (folio, new_folio);continue ; }
mm/migrate.c +3 -8
@@ -566,7 +566,6 @@ static int __folio_migrate_mapping(struct address_space *mapping,struct zone *oldzone, *newzone;int dirty;long nr =folio_nr_pages (folio); -long entries, i;if (!mapping) {/* Take off deferred split queue while frozen and memcg set */
@@ -615,9 +614,6 @@ static int __folio_migrate_mapping(struct address_space *mapping,if (folio_test_swapcache (folio)) {folio_set_swapcache (newfolio); newfolio->private =folio_get_private (folio); - entries = nr; - }else { - entries =1 ; }/* Move dirty while folio refs frozen and newfolio not yet exposed */
@@ -627,11 +623,10 @@ static int __folio_migrate_mapping(struct address_space *mapping,folio_set_dirty (newfolio); } -/* Swap cache still stores N entries instead of a high-order entry */ -for (i =0 ; i < entries; i++) { +if (folio_test_swapcache (folio)) +__swap_cache_replace_folio (folio, newfolio); +else xas_store (&xas, newfolio); -xas_next (&xas); - }/* * Drop cache reference from old folio by unfreezing
mm/shmem.c +2 -8
@@ -2086,10 +2086,8 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,struct folio *new, *old = *foliop;swp_entry_t entry = old->swap;struct address_space *swap_mapping =swap_address_space (entry); -pgoff_t swap_index =swap_cache_index (entry); -XA_STATE (xas, &swap_mapping->i_pages, swap_index);int nr_pages =folio_nr_pages (old); -int error =0 , i; +int error =0 ;/* * We have arrived here because our zones are constrained, so don't
@@ -2118,12 +2116,8 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, new->swap = entry;folio_set_swapcache (new); -/* Swap cache still stores N entries instead of a high-order entry */ xa_lock_irq (&swap_mapping->i_pages); -for (i =0 ; i < nr_pages; i++) { -WARN_ON_ONCE (xas_store (&xas, new)); -xas_next (&xas); - } +__swap_cache_replace_folio (old, new);xa_unlock_irq (&swap_mapping->i_pages);mem_cgroup_replace_folio (old, new);
mm/swap.h +5 -0
@@ -185,6 +185,7 @@ int swap_cache_add_folio(struct folio *folio, swp_entry_t entry,void swap_cache_del_folio (struct folio *folio);void __swap_cache_del_folio (struct folio *folio,swp_entry_t entry,void *shadow); +void __swap_cache_replace_folio (struct folio *old,struct folio *new);void swap_cache_clear_shadow (int type,unsigned long begin,unsigned long end);
@@ -336,6 +337,10 @@ static inline void __swap_cache_del_folio(struct folio *folio, swp_entry_t entry { } +static inline void __swap_cache_replace_folio (struct folio *old,struct folio *new) +{ +} +static inline unsigned int folio_swap_flags (struct folio *folio) {return 0 ;
mm/swap_state.c +33 -0
@@ -234,6 +234,39 @@ void swap_cache_del_folio(struct folio *folio)folio_ref_sub (folio,folio_nr_pages (folio)); } +/** +* __swap_cache_replace_folio - Replace a folio in the swap cache. +* @old: The old folio to be replaced. +* @new: The new folio. +* +* Replace an existing folio in the swap cache with a new folio. The +* caller is responsible for setting up the new folio's flag and swap +* entries. Replacement will take the new folio's swap entry value as +* the starting offset to override all slots covered by the new folio. +* +* Context: Caller must ensure both folios are locked, also lock the +* swap address_space that holds the old folio to avoid races. +*/ +void __swap_cache_replace_folio (struct folio *old,struct folio *new) +{ +swp_entry_t entry = new->swap; +unsigned long nr_pages =folio_nr_pages (new); +unsigned long offset =swap_cache_index (entry); +unsigned long end = offset + nr_pages; + +XA_STATE (xas, &swap_address_space (entry)->i_pages, offset); + +VM_WARN_ON_ONCE (!folio_test_swapcache (old) || !folio_test_swapcache (new)); +VM_WARN_ON_ONCE (!folio_test_locked (old) || !folio_test_locked (new)); +VM_WARN_ON_ONCE (!entry.val); + +/* Swap cache still stores N entries instead of a high-order entry */ +do { +WARN_ON_ONCE (xas_store (&xas, new) != old); +xas_next (&xas); + }while (++offset < end); +} +/** * swap_cache_clear_shadow - Clears a set of shadows in the swap cache. * @type: Indicates the swap device.
Signature · 1 line
2.51.0
PATCH v3 11/15 mm, swap: use the swap table for the swap cache and switch API
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Introduce basic swap table infrastructures, which are now just a fixed-sized flat array inside each swap cluster, with access wrappers.
Each cluster contains a swap table of 512 entries. Each table entry is an opaque atomic long. It could be in 3 types: a shadow type (XA_VALUE), a folio type (pointer), or NULL.
In this first step, it only supports storing a folio or shadow, and it is a drop-in replacement for the current swap cache. Convert all swap cache users to use the new sets of APIs. Chris Li has been suggesting using a new infrastructure for swap cache for better performance, and that idea combined well with the swap table as the new backing structure. Now the lock contention range is reduced to 2M clusters, which is much smaller than the 64M address_space. And we can also drop the multiple address_space design.
All the internal works are done with swap_cache_get_* helpers. Swap cache lookup is still lock-less like before, and the helper's contexts are same with original swap cache helpers. They still require a pin on the swap device to prevent the backing data from being freed.
Swap cache updates are now protected by the swap cluster lock instead of the Xarray lock. This is mostly handled internally, but new __swap_cache_* helpers require the caller to lock the cluster. So, a few new cluster access and locking helpers are also introduced.
A fully cluster-based unified swap table can be implemented on top of this to take care of all count tracking and synchronization work, with dynamic allocation. It should reduce the memory usage while making the performance even better.
--- MAINTAINERS | 1 + include/linux/swap.h | 2 - mm/huge_memory.c | 13 +- mm/migrate.c | 19 ++- mm/shmem.c | 8 +- mm/swap.h | 154 +++++++++++++++++------ mm/swap_state.c | 293 +++++++++++++++++++------------------------ mm/swap_table.h | 97 ++++++++++++++ mm/swapfile.c | 100 +++++++++++---- mm/vmscan.c | 20 ++- 10 files changed, 459 insertions(+), 248 deletions(-) create mode 100644 mm/swap_table.h
MAINTAINERS +1 -0
@@ -16232,6 +16232,7 @@ F: include/linux/swapops.h F: mm/page_io.c F: mm/swap.c F: mm/swap.h +F: mm/swap_table.h F: mm/swap_state.c F: mm/swapfile.c
include/linux/swap.h +0 -2
@@ -480,8 +480,6 @@ extern int __swap_count(swp_entry_t entry);extern bool swap_entry_swapped (struct swap_info_struct *si,swp_entry_t entry);extern int swp_swapcount (swp_entry_t entry);struct backing_dev_info ; -extern int init_swap_address_space (unsigned int type,unsigned long nr_pages); -extern void exit_swap_address_space (unsigned int type);extern struct swap_info_struct *get_swap_device (swp_entry_t entry);sector_t swap_folio_sector (struct folio *folio);
mm/huge_memory.c +6 -7
@@ -3720,7 +3720,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Prevent deferred_split_scan() touching ->_refcount */ spin_lock (&ds_queue->split_queue_lock);if (folio_ref_freeze (folio,1 + extra_pins)) { -struct address_space *swap_cache =NULL ; +struct swap_cluster_info *ci =NULL ;struct lruvec *lruvec;int expected_refs;
@@ -3764,8 +3764,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order, goto fail; } - swap_cache =swap_address_space (folio->swap); -xa_lock (&swap_cache->i_pages); + ci =swap_cluster_get_and_lock (folio); }/* lock lru list/PageCompound, ref frozen by page_ref_freeze */
@@ -3797,8 +3796,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* Anonymous folio with swap cache. * NOTE: shmem in swap cache is not supported yet. */ -if (swap_cache) { -__swap_cache_replace_folio (folio, new_folio); +if (ci) { +__swap_cache_replace_folio (ci, folio, new_folio);continue ; }
@@ -3833,8 +3832,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,unlock_page_lruvec (lruvec); -if (swap_cache) -xa_unlock (&swap_cache->i_pages); +if (ci) +swap_cluster_unlock (ci); }else {spin_unlock (&ds_queue->split_queue_lock); ret = -EAGAIN ;
mm/migrate.c +15 -4
@@ -563,6 +563,7 @@ static int __folio_migrate_mapping(struct address_space *mapping, struct folio *newfolio, struct folio *folio, int expected_count) { XA_STATE(xas, &mapping->i_pages, folio_index(folio)); +struct swap_cluster_info *ci =NULL ;struct zone *oldzone, *newzone;int dirty;long nr =folio_nr_pages (folio);
@@ -591,9 +592,16 @@ static int __folio_migrate_mapping(struct address_space *mapping, oldzone =folio_zone (folio); newzone =folio_zone (newfolio); -xas_lock_irq (&xas); +if (folio_test_swapcache (folio)) + ci =swap_cluster_get_and_lock_irq (folio); +else +xas_lock_irq (&xas); +if (!folio_ref_freeze (folio, expected_count)) { -xas_unlock_irq (&xas); +if (ci) +swap_cluster_unlock (ci); +else +xas_unlock_irq (&xas);return -EAGAIN ; }
@@ -624,7 +632,7 @@ static int __folio_migrate_mapping(struct address_space *mapping, }if (folio_test_swapcache (folio)) -__swap_cache_replace_folio (folio, newfolio); +__swap_cache_replace_folio (ci, folio, newfolio);else xas_store (&xas, newfolio);
@@ -635,8 +643,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,*/ folio_ref_unfreeze (folio, expected_count - nr); -xas_unlock (&xas);/* Leave irq disabled to prevent preemption while updating stats */ +if (ci) +swap_cluster_unlock (ci); +else +xas_unlock (&xas);/* * If moved to a different zone then also account
mm/shmem.c +4 -4
@@ -2083,9 +2083,9 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, struct shmem_inode_info *info, pgoff_t index, struct vm_area_struct *vma) { + struct swap_cluster_info *ci;struct folio *new, *old = *foliop;swp_entry_t entry = old->swap; -struct address_space *swap_mapping =swap_address_space (entry);int nr_pages =folio_nr_pages (old);int error =0 ;
@@ -2116,9 +2116,9 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, new->swap = entry;folio_set_swapcache (new); -xa_lock_irq (&swap_mapping->i_pages); -__swap_cache_replace_folio (old, new); -xa_unlock_irq (&swap_mapping->i_pages); + ci =swap_cluster_get_and_lock_irq (old); +__swap_cache_replace_folio (ci, old, new); +swap_cluster_unlock (ci);mem_cgroup_replace_folio (old, new);shmem_update_stats (new, nr_pages);
mm/swap.h +119 -35
245 lines not on this page · raw message
mm/swap_state.c +128 -165
413 lines not on this page · raw message
mm/swap_table.h new file +97 -0
97 lines not on this page · raw message
mm/swapfile.c +75 -25
201 lines not on this page · raw message
mm/vmscan.c +14 -6
@@ -730,13 +730,18 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio, {int refcount;void *shadow =NULL ; +struct swap_cluster_info *ci;BUG_ON (!folio_test_locked (folio));BUG_ON (mapping !=folio_mapping (folio)); -if (!folio_test_swapcache (folio)) +if (folio_test_swapcache (folio)) { + ci =swap_cluster_get_and_lock_irq (folio); + }else {spin_lock (&mapping->host->i_lock); -xa_lock_irq (&mapping->i_pages); +xa_lock_irq (&mapping->i_pages); + } +/* * The non racy check for a busy folio. *
@@ -776,9 +781,9 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,if (reclaimed && !mapping_exiting (mapping)) shadow =workingset_eviction (folio, target_memcg); -__swap_cache_del_folio (folio, swap, shadow); +__swap_cache_del_folio (ci, folio, swap, shadow);memcg1_swapout (folio, swap); -xa_unlock_irq (&mapping->i_pages); +swap_cluster_unlock_irq (ci);put_swap_folio (folio, swap); }else {void (*free_folio)(struct folio *);
@@ -816,9 +821,12 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,return 1 ; cannot_free: -xa_unlock_irq (&mapping->i_pages); -if (!folio_test_swapcache (folio)) +if (folio_test_swapcache (folio)) { +swap_cluster_unlock_irq (ci); + }else { +xa_unlock_irq (&mapping->i_pages);spin_unlock (&mapping->host->i_lock); + }return 0 ; }
Signature · 1 line
2.51.0
PATCH v3 12/15 mm, swap: mark swap address space ro and add context debug check
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Swap cache is now backed by swap table, and the address space is not holding any mutable data anymore. And swap cache is now protected by the swap cluster lock, instead of the XArray lock. All access to swap cache are wrapped by swap cache helpers. Locking is mostly handled internally by swap cache helpers, only a few __swap_cache_* helpers require the caller to lock the cluster by themselves.
Worth noting that, unlike XArray, the cluster lock is not IRQ safe. The swap cache was very different compared to filemap, and now it's completely separated from filemap. Nothing wants to mark or change anything or do a writeback callback in IRQ.
So explicitly document this and add a debug check to avoid further potential misuse. And mark the swap cache space as read-only to avoid any user wrongly mixing unexpected filemap helpers with swap cache.
--- mm/swap.h | 12 +++++++++++- mm/swap_state.c | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-)
mm/swap.h +11 -1
@@ -99,6 +99,16 @@ static __always_inline struct swap_cluster_info *__swap_cluster_lock( {struct swap_cluster_info *ci =__swap_offset_to_cluster (si, offset); +/* +* Nothing modifies swap cache in an IRQ context. All access to +* swap cache is wrapped by swap_cache_* helpers, and swap cache +* writeback is handled outside of IRQs. Swapin or swapout never +* occurs in IRQ, and neither does in-place split or replace. +* +* Besides, modifying swap cache requires synchronization with +* swap_map, which was never IRQ safe. +*/ +VM_WARN_ON_ONCE (!in_task ());VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ if (irq)spin_lock_irq (&ci->lock);
@@ -192,7 +202,7 @@ void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);#define SWAP_ADDRESS_SPACE_SHIFT 14#define SWAP_ADDRESS_SPACE_PAGES (1 << SWAP_ADDRESS_SPACE_SHIFT)#define SWAP_ADDRESS_SPACE_MASK (SWAP_ADDRESS_SPACE_PAGES - 1) -extern struct address_space swap_space; +extern struct address_space swap_space__ro_after_init ;static inline struct address_space *swap_address_space (swp_entry_t entry) {return &swap_space;
mm/swap_state.c +2 -1
@@ -37,7 +37,8 @@ static const struct address_space_operations swap_aops = {#endif }; -struct address_space swap_space__read_mostly = { +/* Set swap_space as read only as swap cache is handled by swap table */ +struct address_space swap_space__ro_after_init = { .a_ops = &swap_aops, };
Signature · 1 line
2.51.0
PATCH v3 13/15 mm, swap: remove contention workaround for swap cache
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Swap cluster setup will try to shuffle the clusters on initialization. It was helpful to avoid contention for the swap cache space. The cluster size (2M) was much smaller than each swap cache space (64M), so shuffling the cluster means the allocator will try to allocate swap slots that are in different swap cache spaces for each CPU, reducing the chance of two CPUs using the same swap cache space, and hence reducing the contention.
Now, swap cache is managed by swap clusters, this shuffle is pointless. Just remove it, and clean up related macros.
This also improves the HDD swap performance as shuffling IO is a bad idea for HDD, and now the shuffling is gone. Test have shown a ~40% performance gain for HDD [1]:
Doing sequential swap in of 8G data using 8 processes with usemem, average of 3 test runs:
Before: 1270.91 KB/s per process After: 1849.54 KB/s per process
--- mm/swap.h | 4 ---- mm/swapfile.c | 32 ++++++++------------------------ mm/zswap.c | 7 +++++-- 3 files changed, 13 insertions(+), 30 deletions(-)
mm/swap.h +0 -4
@@ -198,10 +198,6 @@ int swap_writeout(struct folio *folio, struct swap_iocb **swap_plug);void __swap_writepage (struct folio *folio,struct swap_iocb **swap_plug);/* linux/mm/swap_state.c */ -/* One swap address space for each 64M swap space */ -#define SWAP_ADDRESS_SPACE_SHIFT 14 -#define SWAP_ADDRESS_SPACE_PAGES (1 << SWAP_ADDRESS_SPACE_SHIFT) -#define SWAP_ADDRESS_SPACE_MASK (SWAP_ADDRESS_SPACE_PAGES - 1)extern struct address_space swap_space__ro_after_init ;static inline struct address_space *swap_address_space (swp_entry_t entry) {
mm/swapfile.c +8 -24
@@ -3203,21 +3203,14 @@ static int setup_swap_map(struct swap_info_struct *si,return 0 ; } -#define SWAP_CLUSTER_INFO_COLS \ - DIV_ROUND_UP(L1_CACHE_BYTES, sizeof(struct swap_cluster_info)) -#define SWAP_CLUSTER_SPACE_COLS \ - DIV_ROUND_UP(SWAP_ADDRESS_SPACE_PAGES, SWAPFILE_CLUSTER) -#define SWAP_CLUSTER_COLS \ - max_t(unsigned int, SWAP_CLUSTER_INFO_COLS, SWAP_CLUSTER_SPACE_COLS) -static struct swap_cluster_info *setup_clusters (struct swap_info_struct *si,union swap_header *swap_header,unsigned long maxpages) {unsigned long nr_clusters =DIV_ROUND_UP (maxpages,SWAPFILE_CLUSTER );struct swap_cluster_info *cluster_info; -unsigned long i, j, idx;int err = -ENOMEM ; +unsigned long i; cluster_info =kvcalloc (nr_clusters,sizeof (*cluster_info),GFP_KERNEL );if (!cluster_info)
@@ -3266,22 +3259,13 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,INIT_LIST_HEAD (&si->frag_clusters[i]); } -/* -* Reduce false cache line sharing between cluster_info and -* sharing same address space. -*/ -for (j =0 ; j <SWAP_CLUSTER_COLS ; j++) { -for (i =0 ; i <DIV_ROUND_UP (nr_clusters,SWAP_CLUSTER_COLS ); i++) { -struct swap_cluster_info *ci; - idx = i *SWAP_CLUSTER_COLS + j; - ci = cluster_info + idx; -if (idx >= nr_clusters) -continue ; -if (ci->count) { - ci->flags =CLUSTER_FLAG_NONFULL ; -list_add_tail (&ci->list, &si->nonfull_clusters[0 ]); -continue ; - } +for (i =0 ; i < nr_clusters; i++) { +struct swap_cluster_info *ci = &cluster_info[i]; + +if (ci->count) { + ci->flags =CLUSTER_FLAG_NONFULL ; +list_add_tail (&ci->list, &si->nonfull_clusters[0 ]); + }else { ci->flags =CLUSTER_FLAG_FREE ;list_add_tail (&ci->list, &si->free_clusters); }
mm/zswap.c +5 -2
@@ -225,10 +225,13 @@ static bool zswap_has_pool;* helpers and fwd declarations **********************************/ +/* One swap address space for each 64M swap space */ +#define ZSWAP_ADDRESS_SPACE_SHIFT 14 +#define ZSWAP_ADDRESS_SPACE_PAGES (1 << ZSWAP_ADDRESS_SPACE_SHIFT)static inline struct xarray *swap_zswap_tree (swp_entry_t swp) {return &zswap_trees[swp_type (swp)][swp_offset (swp) - >>SWAP_ADDRESS_SPACE_SHIFT ]; + >>ZSWAP_ADDRESS_SPACE_SHIFT ]; }#define zswap_pool_debug (msg, p) \
@@ -1674,7 +1677,7 @@ int zswap_swapon(int type, unsigned long nr_pages)struct xarray *trees, *tree;unsigned int nr, i; - nr =DIV_ROUND_UP (nr_pages,SWAP_ADDRESS_SPACE_PAGES ); + nr =DIV_ROUND_UP (nr_pages,ZSWAP_ADDRESS_SPACE_PAGES ); trees =kvcalloc (nr,sizeof (*tree),GFP_KERNEL );if (!trees) {pr_err ("alloc failed, zswap disabled for swap type %d\n" , type);
Signature · 1 line
2.51.0
PATCH v3 14/15 mm, swap: implement dynamic allocation of swap table
In reply to Kairui SongFrom: Kairui Song <[email protected]>
Now swap table is cluster based, which means free clusters can free its table since no one should modify it.
There could be speculative readers, like swap cache look up, protect them by making them RCU protected. All swap table should be filled with null entries before free, so such readers will either see a NULL pointer or a null filled table being lazy freed.
On allocation, allocate the table when a cluster is used by any order.
This way, we can reduce the memory usage of large swap device significantly.
This idea to dynamically release unused swap cluster data was initially suggested by Chris Li while proposing the cluster swap allocator and it suits the swap table idea very well.
--- mm/swap.h | 2 +- mm/swap_state.c | 9 +-- mm/swap_table.h | 37 ++++++++- mm/swapfile.c | 202 ++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 199 insertions(+), 51 deletions(-)
mm/swap.h +1 -1
@@ -36,7 +36,7 @@ struct swap_cluster_info {u16 count;u8 flags;u8 order; -atomic_long_t *table;/* Swap table entries, see mm/swap_table.h */ +atomic_long_t __rcu *table;/* Swap table entries, see mm/swap_table.h */ struct list_head list; };
mm/swap_state.c +4 -5
@@ -91,8 +91,8 @@ struct folio *swap_cache_get_folio(swp_entry_t entry)struct folio *folio;for (;;) { - swp_tb =__swap_table_get (__swap_entry_to_cluster (entry), -swp_cluster_offset (entry)); + swp_tb =swap_table_get (__swap_entry_to_cluster (entry), +swp_cluster_offset (entry));if (!swp_tb_is_folio (swp_tb))return NULL ; folio =swp_tb_to_folio (swp_tb);
@@ -115,11 +115,10 @@ void *swap_cache_get_shadow(swp_entry_t entry) {unsigned long swp_tb; - swp_tb =__swap_table_get (__swap_entry_to_cluster (entry), -swp_cluster_offset (entry)); + swp_tb =swap_table_get (__swap_entry_to_cluster (entry), +swp_cluster_offset (entry));if (swp_tb_is_shadow (swp_tb))return swp_tb_to_shadow (swp_tb); -return NULL ; }
mm/swap_table.h +34 -3
@@ -2,8 +2,15 @@#ifndef _MM_SWAP_TABLE_H#define _MM_SWAP_TABLE_H +#include <linux/rcupdate.h> +#include <linux/atomic.h> #include "swap.h" +/* A typical flat array in each cluster as swap table */ +struct swap_table { +atomic_long_t entries[SWAPFILE_CLUSTER ]; +}; +/* * A swap table entry represents the status of a swap slot on a swap * (physical or virtual) device. The swap table in each cluster is a
@@ -76,22 +83,46 @@ static inline void *swp_tb_to_shadow(unsigned long swp_tb)static inline void __swap_table_set (struct swap_cluster_info *ci,unsigned int off,unsigned long swp_tb) { +atomic_long_t *table =rcu_dereference_protected (ci->table, true); + +lockdep_assert_held (&ci->lock);VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); -atomic_long_set (&ci->table[off], swp_tb); +atomic_long_set (&table[off], swp_tb); }static inline unsigned long __swap_table_xchg (struct swap_cluster_info *ci,unsigned int off,unsigned long swp_tb) { +atomic_long_t *table =rcu_dereference_protected (ci->table, true); + +lockdep_assert_held (&ci->lock);VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER );/* Ordering is guaranteed by cluster lock, relax */ -return atomic_long_xchg_relaxed (&ci->table[off], swp_tb); +return atomic_long_xchg_relaxed (&table[off], swp_tb); }static inline unsigned long __swap_table_get (struct swap_cluster_info *ci,unsigned int off) { +atomic_long_t *table; +VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); -return atomic_long_read (&ci->table[off]); + table =rcu_dereference_check (ci->table,lockdep_is_held (&ci->lock)); + +return atomic_long_read (&table[off]); +} + +static inline unsigned long swap_table_get (struct swap_cluster_info *ci, +unsigned int off) +{ +atomic_long_t *table; +unsigned long swp_tb; + +rcu_read_lock (); + table =rcu_dereference (ci->table); + swp_tb = table ?atomic_long_read (&table[off]) :null_to_swp_tb (); +rcu_read_unlock (); + +return swp_tb; }#endif
mm/swapfile.c +160 -42
352 lines not on this page · raw message
Signature · 1 line
2.51.0
PATCH v3 15/15 mm, swap: use a single page for swap table when the size fits
In reply to Kairui SongFrom: Kairui Song <[email protected]>
We have a cluster size of 512 slots. Each slot consumes 8 bytes in swap table so the swap table size of each cluster is exactly one page (4K).
If that condition is true, allocate one page direct and disable the slab cache to reduce the memory usage of swap table and avoid fragmentation.
--- mm/swap_table.h | 2 ++ mm/swapfile.c | 50 ++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 43 insertions(+), 9 deletions(-)
mm/swap_table.h +2 -0
@@ -11,6 +11,8 @@ struct swap_table {atomic_long_t entries[SWAPFILE_CLUSTER ]; }; +#define SWP_TABLE_USE_PAGE (sizeof(struct swap_table) == PAGE_SIZE) +/* * A swap table entry represents the status of a swap slot on a swap * (physical or virtual) device. The swap table in each cluster is a
mm/swapfile.c +41 -9
92 lines not on this page · raw message
Signature · 1 line
2.51.0
Re: [PATCH v3 01/15] docs/mm: add document for swap table
In reply to Kairui SongQuoted 4 lines · Kairui Song
From: Kairui Song <[email protected]>
From: Chris Li <[email protected]>
So sorry about this. I did fix my git config and verified that, but sent the email on another machine that still having a broken config :/
Hi Andrew, can you help fix the Author to be Chris here?
BTW I saw the current version in mm-new, the author is already Chris, which is correct.
Chris Li
maintainerRe: [PATCH v3 01/15] docs/mm: add document for swap table
Quoted 7 lines · Kairui Song
Quoted 4 lines · Kairui Song
From: Kairui Song <[email protected]>
From: Chris Li <[email protected]>
Quoted 7 lines
So sorry about this. I did fix my git config and verified that, but sent the email on another machine that still having a broken config :/
Hi Andrew, can you help fix the Author to be Chris here?
BTW I saw the current version in mm-new, the author is already Chris, which is correct.
If the mm-new got it right, you have nothing to worry about. I assume Andrew's tooling already takes care of this common issue.
Chris
Andrew Morton
maintainerQuoted 5 lines · On Thu, 11 Sep 2025 00:08:18 +0800 Kairui Song <[email protected]> wrote:
V3 changes:
There is basically no code change, mostly clean up and comment changes.
- Renames a few variable and functions [ David Hildenbrand, Chris Li ]
- Move thefolio_matches_swap_entrycheck underunuse_ptein patch 5 [ David Hildenbrand ].
Quoted 8 lines
- Remove two redundant function params for the folio replace helper in patch 10, and add comment about folio setup [ David Hildenbrand ].
- Move the shmem clean up patch after the API rename patch to fix build error, no code change. [ Baolin Wang ]
- Fix build error with !SWAP and !SHMEM config. [ Klara Modin, SeongJae Park ]
- Fix a few typo and blank lines [ David Hildenbrand ]
- Minor code style change [ Chris Li ].
- Fix documentation warning from bot. [ Chris Li ]
Thanks, I updated mm-unstable to this v3 series.
Lance Yang
reviewerRe: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
In reply to Kairui SongHi Kairui,
I'm hitting a build error with allnoconfig:
In file included from mm/shmem.c:44:mm/swap.h: In function ‘folio_index’:mm/swap.h:462:24: error: implicit declaration of function ‘swp_offset’; did you mean ‘pmd_offset’? [-Wimplicit-function-declaration]
462 | return swp_offset(folio->swap);
It looks like a header might be missing in mm/swap.h. Please let me know if you need any more information.
Cheers,
Lance
Quoted 1,274 lines · Kairui Song
From: Kairui Song <[email protected]>
Introduce basic swap table infrastructures, which are now just a fixed-sized flat array inside each swap cluster, with access wrappers.
Each cluster contains a swap table of 512 entries. Each table entry is an opaque atomic long. It could be in 3 types: a shadow type (
XA_VALUE), a folio type (pointer), or NULL.In this first step, it only supports storing a folio or shadow, and it is a drop-in replacement for the current swap cache. Convert all swap cache users to use the new sets of APIs. Chris Li has been suggesting using a new infrastructure for swap cache for better performance, and that idea combined well with the swap table as the new backing structure. Now the lock contention range is reduced to 2M clusters, which is much smaller than the 64M
address_space. And we can also drop the multipleaddress_spacedesign.All the internal works are done with swap_cache_get_* helpers. Swap cache lookup is still lock-less like before, and the helper's contexts are same with original swap cache helpers. They still require a pin on the swap device to prevent the backing data from being freed.
Swap cache updates are now protected by the swap cluster lock instead of the Xarray lock. This is mostly handled internally, but new __swap_cache_* helpers require the caller to lock the cluster. So, a few new cluster access and locking helpers are also introduced.
A fully cluster-based unified swap table can be implemented on top of this to take care of all count tracking and synchronization work, with dynamic allocation. It should reduce the memory usage while making the performance even better.
Co-developed-by Chris Li <[email protected]>Signed-off-by Chris Li <[email protected]>Signed-off-by Kairui Song <[email protected]>Acked-by Chris Li <[email protected]>--- MAINTAINERS | 1 + include/linux/swap.h | 2 - mm/huge_memory.c | 13 +- mm/migrate.c | 19 ++- mm/shmem.c | 8 +- mm/swap.h | 154 +++++++++++++++++------ mm/swap_state.c | 293 +++++++++++++++++++------------------------ mm/swap_table.h | 97 ++++++++++++++ mm/swapfile.c | 100 +++++++++++---- mm/vmscan.c | 20 ++- 10 files changed, 459 insertions(+), 248 deletions(-) create mode 100644 mm/swap_table.hMAINTAINERS +1
-0@@ -16232,6 +16232,7 @@ F: include/linux/swapops.h F: mm/page_io.c F: mm/swap.c F: mm/swap.h +F: mm/swap_table.h F: mm/swap_state.c F: mm/swapfile.cinclude/linux/swap.h +0
-2@@ -480,8 +480,6 @@ extern int __swap_count(swp_entry_t entry);extern bool swap_entry_swapped (struct swap_info_struct *si,swp_entry_t entry);extern int swp_swapcount (swp_entry_t entry);struct backing_dev_info ; -extern int init_swap_address_space (unsigned int type,unsigned long nr_pages); -extern void exit_swap_address_space (unsigned int type);extern struct swap_info_struct *get_swap_device (swp_entry_t entry);sector_t swap_folio_sector (struct folio *folio);mm/huge_memory.c +6
-7@@ -3720,7 +3720,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Prevent deferred_split_scan() touching ->_refcount */ spin_lock (&ds_queue->split_queue_lock);if (folio_ref_freeze (folio,1 + extra_pins)) { -struct address_space *swap_cache =NULL ; +struct swap_cluster_info *ci =NULL ;struct lruvec *lruvec;int expected_refs;@@ -3764,8 +3764,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order, goto fail; } - swap_cache =swap_address_space (folio->swap); -xa_lock (&swap_cache->i_pages); + ci =swap_cluster_get_and_lock (folio); }/* lock lru list/PageCompound, ref frozen by page_ref_freeze */ @@ -3797,8 +3796,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* Anonymous folio with swap cache. * NOTE: shmem in swap cache is not supported yet. */ -if (swap_cache) { -__swap_cache_replace_folio (folio, new_folio); +if (ci) { +__swap_cache_replace_folio (ci, folio, new_folio);continue ; }@@ -3833,8 +3832,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,unlock_page_lruvec (lruvec); -if (swap_cache) -xa_unlock (&swap_cache->i_pages); +if (ci) +swap_cluster_unlock (ci); }else {spin_unlock (&ds_queue->split_queue_lock); ret = -EAGAIN ;mm/migrate.c +15
-4@@ -563,6 +563,7 @@ static int __folio_migrate_mapping(struct address_space *mapping, struct folio *newfolio, struct folio *folio, int expected_count) { XA_STATE(xas, &mapping->i_pages, folio_index(folio)); +struct swap_cluster_info *ci =NULL ;struct zone *oldzone, *newzone;int dirty;long nr =folio_nr_pages (folio);@@ -591,9 +592,16 @@ static int __folio_migrate_mapping(struct address_space *mapping, oldzone =folio_zone (folio); newzone =folio_zone (newfolio); -xas_lock_irq (&xas); +if (folio_test_swapcache (folio)) + ci =swap_cluster_get_and_lock_irq (folio); +else +xas_lock_irq (&xas); +if (!folio_ref_freeze (folio, expected_count)) { -xas_unlock_irq (&xas); +if (ci) +swap_cluster_unlock (ci); +else +xas_unlock_irq (&xas);return -EAGAIN ; }@@ -624,7 +632,7 @@ static int __folio_migrate_mapping(struct address_space *mapping, }if (folio_test_swapcache (folio)) -__swap_cache_replace_folio (folio, newfolio); +__swap_cache_replace_folio (ci, folio, newfolio);else xas_store (&xas, newfolio);@@ -635,8 +643,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,*/ folio_ref_unfreeze (folio, expected_count - nr); -xas_unlock (&xas);/* Leave irq disabled to prevent preemption while updating stats */ +if (ci) +swap_cluster_unlock (ci); +else +xas_unlock (&xas);/* * If moved to a different zone then also account mm/shmem.c +4
-4@@ -2083,9 +2083,9 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, struct shmem_inode_info *info, pgoff_t index, struct vm_area_struct *vma) { + struct swap_cluster_info *ci;struct folio *new, *old = *foliop;swp_entry_t entry = old->swap; -struct address_space *swap_mapping =swap_address_space (entry);int nr_pages =folio_nr_pages (old);int error =0 ;@@ -2116,9 +2116,9 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, new->swap = entry;folio_set_swapcache (new); -xa_lock_irq (&swap_mapping->i_pages); -__swap_cache_replace_folio (old, new); -xa_unlock_irq (&swap_mapping->i_pages); + ci =swap_cluster_get_and_lock_irq (old); +__swap_cache_replace_folio (ci, old, new); +swap_cluster_unlock (ci);mem_cgroup_replace_folio (old, new);shmem_update_stats (new, nr_pages);mm/swap.h +119
-35@@ -2,6 +2,7 @@#ifndef _MM_SWAP_H#define _MM_SWAP_H +#include <linux/atomic.h> /* for atomic_long_t */ struct mempolicy ;struct swap_iocb ;@@ -35,6 +36,7 @@ struct swap_cluster_info {u16 count;u8 flags;u8 order; +atomic_long_t *table;/* Swap table entries, see mm/swap_table.h */ struct list_head list; };@@ -55,6 +57,11 @@ enum swap_cluster_flags {#include <linux/swapops.h> /* for swp_offset */ #include <linux/blk_types.h> /* for bio_end_io_t */ +static inline unsigned int swp_cluster_offset (swp_entry_t entry) +{ +return swp_offset (entry) %SWAPFILE_CLUSTER ; +} +/* * Callers of all helpers below must ensure the entry, type, or offset is * valid, and protect the swap device with reference count or locks. @@ -81,6 +88,25 @@ static inline struct swap_cluster_info *__swap_offset_to_cluster(return &si->cluster_info[offset /SWAPFILE_CLUSTER ]; } +static inline struct swap_cluster_info *__swap_entry_to_cluster (swp_entry_t entry) +{ +return __swap_offset_to_cluster (__swap_entry_to_info (entry), +swp_offset (entry)); +} + +static __always_inline struct swap_cluster_info *__swap_cluster_lock( +struct swap_info_struct *si,unsigned long offset,bool irq) +{ +struct swap_cluster_info *ci =__swap_offset_to_cluster (si, offset); + +VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ +if (irq) +spin_lock_irq (&ci->lock); +else +spin_lock (&ci->lock); +return ci; +} +/** * swap_cluster_lock - Lock and return the swap cluster of given offset. * @si: swap device the cluster belongs to. @@ -92,11 +118,49 @@ static inline struct swap_cluster_info *__swap_offset_to_cluster(static inline struct swap_cluster_info *swap_cluster_lock (struct swap_info_struct *si,unsigned long offset) { -struct swap_cluster_info *ci =__swap_offset_to_cluster (si, offset); +return __swap_cluster_lock (si, offset, false); +} -VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ -spin_lock (&ci->lock); -return ci; +static inline struct swap_cluster_info *__swap_cluster_get_and_lock ( +const struct folio *folio,bool irq) +{ +VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio); +VM_WARN_ON_ONCE_FOLIO (!folio_test_swapcache (folio), folio); +return __swap_cluster_lock (__swap_entry_to_info (folio->swap), +swp_offset (folio->swap), irq); +} + +/* +* swap_cluster_get_and_lock - Locks the cluster that holds a folio's entries. +* @folio: The folio. +* +* This locks and returns the swap cluster that contains a folio's swap +* entries. The swap entries of a folio are always in one single cluster. +* The folio has to be locked so its swap entries won't change and the +* cluster won't be freed. +* +* Context: Caller must ensure the folio is locked and in the swap cache. +* Return: Pointer to the swap cluster. +*/ +static inline struct swap_cluster_info *swap_cluster_get_and_lock ( +const struct folio *folio) +{ +return __swap_cluster_get_and_lock (folio, false); +} + +/* +* swap_cluster_get_and_lock_irq - Locks the cluster that holds a folio's entries. +* @folio: The folio. +* +* Same as swap_cluster_get_and_lock but also disable IRQ. +* +* Context: Caller must ensure the folio is locked and in the swap cache. +* Return: Pointer to the swap cluster. +*/ +static inline struct swap_cluster_info *swap_cluster_get_and_lock_irq ( +const struct folio *folio) +{ +return __swap_cluster_get_and_lock (folio, true); }static inline void swap_cluster_unlock (struct swap_cluster_info *ci)@@ -104,6 +168,11 @@ static inline void swap_cluster_unlock(struct swap_cluster_info *ci)spin_unlock (&ci->lock); } +static inline void swap_cluster_unlock_irq (struct swap_cluster_info *ci) +{ +spin_unlock_irq (&ci->lock); +} +/* linux/mm/page_io.c */ int sio_pool_init (void );struct swap_iocb ;@@ -123,10 +192,11 @@ void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);#define SWAP_ADDRESS_SPACE_SHIFT 14#define SWAP_ADDRESS_SPACE_PAGES (1 << SWAP_ADDRESS_SPACE_SHIFT)#define SWAP_ADDRESS_SPACE_MASK (SWAP_ADDRESS_SPACE_PAGES - 1) -extern struct address_space *swapper_spaces[]; -#define swap_address_space (entry) \ - (&swapper_spaces[swp_type(entry)][swp_offset(entry) \ - >> SWAP_ADDRESS_SPACE_SHIFT]) +extern struct address_space swap_space; +static inline struct address_space *swap_address_space (swp_entry_t entry) +{ +return &swap_space; +}/* * Return the swap device position of the swap entry. @@ -136,15 +206,6 @@ static inline loff_t swap_dev_pos(swp_entry_t entry)return ((loff_t )swp_offset (entry)) <<PAGE_SHIFT ; } -/* -* Return the swap cache index of the swap entry. -*/ -static inline pgoff_t swap_cache_index (swp_entry_t entry) -{ -BUILD_BUG_ON ((SWP_OFFSET_MASK |SWAP_ADDRESS_SPACE_MASK ) !=SWP_OFFSET_MASK ); -return swp_offset (entry) &SWAP_ADDRESS_SPACE_MASK ; -} -/** * folio_matches_swap_entry - Check if a folio matches a given swap entry. * @folio: The folio. @@ -180,14 +241,14 @@ static inline bool folio_matches_swap_entry(const struct folio *folio,*/ struct folio *swap_cache_get_folio (swp_entry_t entry);void *swap_cache_get_shadow (swp_entry_t entry); -int swap_cache_add_folio (struct folio *folio,swp_entry_t entry, -gfp_t gfp,void **shadow); +void swap_cache_add_folio (struct folio *folio,swp_entry_t entry,void **shadow);void swap_cache_del_folio (struct folio *folio); -void __swap_cache_del_folio (struct folio *folio, -swp_entry_t entry,void *shadow); -void __swap_cache_replace_folio (struct folio *old,struct folio *new); -void swap_cache_clear_shadow (int type,unsigned long begin, -unsigned long end); +/* Below helpers require the caller to lock and pass in the swap cluster. */ +void __swap_cache_del_folio (struct swap_cluster_info *ci, +struct folio *folio,swp_entry_t entry,void *shadow); +void __swap_cache_replace_folio (struct swap_cluster_info *ci, +struct folio *old,struct folio *new); +void __swap_cache_clear_shadow (swp_entry_t entry,int nr_ents);void show_swap_cache_info (void );void swapcache_clear (struct swap_info_struct *si,swp_entry_t entry,int nr);@@ -255,6 +316,32 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr)#else /* CONFIG_SWAP */ struct swap_iocb ; +static inline struct swap_cluster_info *swap_cluster_lock ( +struct swap_info_struct *si,pgoff_t offset,bool irq) +{ +return NULL ; +} + +static inline struct swap_cluster_info *swap_cluster_get_and_lock ( +struct folio *folio) +{ +return NULL ; +} + +static inline struct swap_cluster_info *swap_cluster_get_and_lock_irq ( +struct folio *folio) +{ +return NULL ; +} + +static inline void swap_cluster_unlock (struct swap_cluster_info *ci) +{ +} + +static inline void swap_cluster_unlock_irq (struct swap_cluster_info *ci) +{ +} +static inline struct swap_info_struct *__swap_entry_to_info (swp_entry_t entry) {return NULL ;@@ -272,11 +359,6 @@ static inline struct address_space *swap_address_space(swp_entry_t entry)return NULL ; } -static inline pgoff_t swap_cache_index (swp_entry_t entry) -{ -return 0 ; -} -static inline bool folio_matches_swap_entry (const struct folio *folio,swp_entry_t entry) {return false;@@ -323,21 +405,21 @@ static inline void *swap_cache_get_shadow(swp_entry_t entry)return NULL ; } -static inline int swap_cache_add_folio (swp_entry_t entry,struct folio *folio, -gfp_t gfp,void **shadow) +static inline void swap_cache_add_folio (struct folio *folio,swp_entry_t entry,void **shadow) { -return -EINVAL ; }static inline void swap_cache_del_folio (struct folio *folio) { } -static inline void __swap_cache_del_folio (struct folio *folio,swp_entry_t entry,void *shadow) +static inline void __swap_cache_del_folio (struct swap_cluster_info *ci, +struct folio *folio,swp_entry_t entry,void *shadow) { } -static inline void __swap_cache_replace_folio (struct folio *old,struct folio *new) +static inline void __swap_cache_replace_folio (struct swap_cluster_info *ci, +struct folio *old,struct folio *new) { }@@ -371,8 +453,10 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr)*/ static inline pgoff_t folio_index (struct folio *folio) { +#ifdef CONFIG_SWAP if (unlikely (folio_test_swapcache (folio))) -return swap_cache_index (folio->swap); +return swp_offset (folio->swap); +#endif return folio->index; }mm/swap_state.c +128
-165@@ -23,6 +23,7 @@#include <linux/huge_mm.h> #include <linux/shmem_fs.h> #include "internal.h" +#include "swap_table.h" #include "swap.h" /* @@ -36,8 +37,10 @@ static const struct address_space_operations swap_aops = {#endif }; -struct address_space *swapper_spaces[MAX_SWAPFILES ]__read_mostly ; -static unsigned int nr_swapper_spaces[MAX_SWAPFILES ]__read_mostly ; +struct address_space swap_space__read_mostly = { + .a_ops = &swap_aops, +}; +static bool enable_vma_readahead__read_mostly = true;#define SWAP_RA_ORDER_CEILING 5@@ -83,11 +86,20 @@ void show_swap_cache_info(void)*/ struct folio *swap_cache_get_folio (swp_entry_t entry) { -struct folio *folio =filemap_get_folio (swap_address_space (entry), -swap_cache_index (entry)); -if (IS_ERR (folio)) -return NULL ; -return folio; +unsigned long swp_tb; +struct folio *folio; + +for (;;) { + swp_tb =__swap_table_get (__swap_entry_to_cluster (entry), +swp_cluster_offset (entry)); +if (!swp_tb_is_folio (swp_tb)) +return NULL ; + folio =swp_tb_to_folio (swp_tb); +if (likely (folio_try_get (folio))) +return folio; + } + +return NULL ; }/** @@ -100,13 +112,13 @@ struct folio *swap_cache_get_folio(swp_entry_t entry)*/ void *swap_cache_get_shadow (swp_entry_t entry) { -struct address_space *address_space =swap_address_space (entry); -pgoff_t idx =swap_cache_index (entry); -void *shadow; +unsigned long swp_tb; + + swp_tb =__swap_table_get (__swap_entry_to_cluster (entry), +swp_cluster_offset (entry)); +if (swp_tb_is_shadow (swp_tb)) +return swp_tb_to_shadow (swp_tb); - shadow =xa_load (&address_space->i_pages, idx); -if (xa_is_value (shadow)) -return shadow;return NULL ; }@@ -119,61 +131,48 @@ void *swap_cache_get_shadow(swp_entry_t entry)* * Context: Caller must ensure @entry is valid and protect the swap device * with reference count or locks. -* The caller also needs to mark the corresponding swap_map slots with -* SWAP_HAS_CACHE to avoid race or conflict. -* Return: Returns 0 on success, error code otherwise. +* The caller also needs to update the corresponding swap_map slots with +* SWAP_HAS_CACHE bit to avoid race or conflict. */ -int swap_cache_add_folio (struct folio *folio,swp_entry_t entry, -gfp_t gfp,void **shadowp) +void swap_cache_add_folio (struct folio *folio,swp_entry_t entry,void **shadowp) { -struct address_space *address_space =swap_address_space (entry); -pgoff_t idx =swap_cache_index (entry); -XA_STATE_ORDER (xas, &address_space->i_pages, idx,folio_order (folio)); -unsigned long i, nr =folio_nr_pages (folio); -void *old; - -xas_set_update (&xas, workingset_update_node); - -VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio); -VM_BUG_ON_FOLIO (folio_test_swapcache (folio), folio); -VM_BUG_ON_FOLIO (!folio_test_swapbacked (folio), folio); +void *shadow =NULL ; +unsigned long old_tb, new_tb; +struct swap_cluster_info *ci; +unsigned int ci_start, ci_off, ci_end; +unsigned long nr_pages =folio_nr_pages (folio); + +VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_swapcache (folio), folio); +VM_WARN_ON_ONCE_FOLIO (!folio_test_swapbacked (folio), folio); + + new_tb =folio_to_swp_tb (folio); + ci_start =swp_cluster_offset (entry); + ci_end = ci_start + nr_pages; + ci_off = ci_start; + ci =swap_cluster_lock (__swap_entry_to_info (entry),swp_offset (entry)); +do { + old_tb =__swap_table_xchg (ci, ci_off, new_tb); +WARN_ON_ONCE (swp_tb_is_folio (old_tb)); +if (swp_tb_is_shadow (old_tb)) + shadow =swp_tb_to_shadow (old_tb); + }while (++ci_off < ci_end); -folio_ref_add (folio, nr); +folio_ref_add (folio, nr_pages);folio_set_swapcache (folio); folio->swap = entry; +swap_cluster_unlock (ci); -do { -xas_lock_irq (&xas); -xas_create_range (&xas); -if (xas_error (&xas)) - goto unlock; -for (i =0 ; i < nr; i++) { -VM_BUG_ON_FOLIO (xas.xa_index != idx + i, folio); -if (shadowp) { - old =xas_load (&xas); -if (xa_is_value (old)) - *shadowp = old; - } -xas_store (&xas, folio); -xas_next (&xas); - } - address_space->nrpages += nr; -__node_stat_mod_folio (folio,NR_FILE_PAGES , nr); -__lruvec_stat_mod_folio (folio,NR_SWAPCACHE , nr); -unlock: -xas_unlock_irq (&xas); - }while (xas_nomem (&xas, gfp)); - -if (!xas_error (&xas)) -return 0 ; +node_stat_mod_folio (folio,NR_FILE_PAGES , nr_pages); +lruvec_stat_mod_folio (folio,NR_SWAPCACHE , nr_pages); -folio_clear_swapcache (folio); -folio_ref_sub (folio, nr); -return xas_error (&xas); +if (shadowp) + *shadowp = shadow; }/** * __swap_cache_del_folio - Removes a folio from the swap cache. +* @ci: The locked swap cluster. * @folio: The folio. * @entry: The first swap entry that the folio corresponds to. * @shadow: shadow value to be filled in the swap cache. @@ -181,34 +180,36 @@ int swap_cache_add_folio(struct folio *folio, swp_entry_t entry,* Removes a folio from the swap cache and fills a shadow in place. * This won't put the folio's refcount. The caller has to do that. * -* Context: Caller must hold the xa_lock, ensure the folio is -* locked and in the swap cache, using the index of @entry. +* Context: Caller must ensure the folio is locked and in the swap cache +* using the index of @entry, and lock the cluster that holds the entries. */ -void __swap_cache_del_folio (struct folio *folio, +void __swap_cache_del_folio (struct swap_cluster_info *ci,struct folio *folio,swp_entry_t entry,void *shadow) { -struct address_space *address_space =swap_address_space (entry); -int i; -long nr =folio_nr_pages (folio); -pgoff_t idx =swap_cache_index (entry); -XA_STATE (xas, &address_space->i_pages, idx); - -xas_set_update (&xas, workingset_update_node); - -VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio); -VM_BUG_ON_FOLIO (!folio_test_swapcache (folio), folio); -VM_BUG_ON_FOLIO (folio_test_writeback (folio), folio); - -for (i =0 ; i < nr; i++) { -void *entry =xas_store (&xas, shadow); -VM_BUG_ON_PAGE (entry != folio, entry); -xas_next (&xas); - } +unsigned long old_tb, new_tb; +unsigned int ci_start, ci_off, ci_end; +unsigned long nr_pages =folio_nr_pages (folio); + +VM_WARN_ON_ONCE (__swap_entry_to_cluster (entry) != ci); +VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio); +VM_WARN_ON_ONCE_FOLIO (!folio_test_swapcache (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_writeback (folio), folio); + + new_tb =shadow_swp_to_tb (shadow); + ci_start =swp_cluster_offset (entry); + ci_end = ci_start + nr_pages; + ci_off = ci_start; +do { +/* If shadow is NULL, we sets an empty shadow */ + old_tb =__swap_table_xchg (ci, ci_off, new_tb); +WARN_ON_ONCE (!swp_tb_is_folio (old_tb) || +swp_tb_to_folio (old_tb) != folio); + }while (++ci_off < ci_end); + folio->swap.val =0 ;folio_clear_swapcache (folio); - address_space->nrpages -= nr; -__node_stat_mod_folio (folio,NR_FILE_PAGES , -nr); -__lruvec_stat_mod_folio (folio,NR_SWAPCACHE , -nr); +node_stat_mod_folio (folio,NR_FILE_PAGES , -nr_pages); +lruvec_stat_mod_folio (folio,NR_SWAPCACHE , -nr_pages); }/** @@ -223,12 +224,12 @@ void __swap_cache_del_folio(struct folio *folio,*/ void swap_cache_del_folio (struct folio *folio) { +struct swap_cluster_info *ci;swp_entry_t entry = folio->swap; -struct address_space *address_space =swap_address_space (entry); -xa_lock_irq (&address_space->i_pages); -__swap_cache_del_folio (folio, entry,NULL ); -xa_unlock_irq (&address_space->i_pages); + ci =swap_cluster_lock (__swap_entry_to_info (entry),swp_offset (entry)); +__swap_cache_del_folio (ci, folio, entry,NULL ); +swap_cluster_unlock (ci);put_swap_folio (folio, entry);folio_ref_sub (folio,folio_nr_pages (folio));@@ -236,6 +237,7 @@ void swap_cache_del_folio(struct folio *folio)/** * __swap_cache_replace_folio - Replace a folio in the swap cache. +* @ci: The locked swap cluster. * @old: The old folio to be replaced. * @new: The new folio. * @@ -244,65 +246,62 @@ void swap_cache_del_folio(struct folio *folio)* entries. Replacement will take the new folio's swap entry value as * the starting offset to override all slots covered by the new folio. * -* Context: Caller must ensure both folios are locked, also lock the -* swap address_space that holds the old folio to avoid races. +* Context: Caller must ensure both folios are locked, and lock the +* cluster that holds the old folio to be replaced. */ -void __swap_cache_replace_folio (struct folio *old,struct folio *new) +void __swap_cache_replace_folio (struct swap_cluster_info *ci, +struct folio *old,struct folio *new) {swp_entry_t entry = new->swap;unsigned long nr_pages =folio_nr_pages (new); -unsigned long offset =swap_cache_index (entry); -unsigned long end = offset + nr_pages; - -XA_STATE (xas, &swap_address_space (entry)->i_pages, offset); +unsigned int ci_off =swp_cluster_offset (entry); +unsigned int ci_end = ci_off + nr_pages; +unsigned long old_tb, new_tb;VM_WARN_ON_ONCE (!folio_test_swapcache (old) || !folio_test_swapcache (new));VM_WARN_ON_ONCE (!folio_test_locked (old) || !folio_test_locked (new));VM_WARN_ON_ONCE (!entry.val);/* Swap cache still stores N entries instead of a high-order entry */ + new_tb =folio_to_swp_tb (new);do { -WARN_ON_ONCE (xas_store (&xas, new) != old); -xas_next (&xas); - }while (++offset < end); + old_tb =__swap_table_xchg (ci, ci_off, new_tb); +WARN_ON_ONCE (!swp_tb_is_folio (old_tb) ||swp_tb_to_folio (old_tb) != old); + }while (++ci_off < ci_end); + +/* +* If the old folio is partially replaced (e.g., splitting a large +* folio, the old folio is shrunk, and new split sub folios replace +* the shrunk part), ensure the new folio doesn't overlap it. +*/ +if (IS_ENABLED (CONFIG_DEBUG_VM ) && +folio_order (old) !=folio_order (new)) { + ci_off =swp_cluster_offset (old->swap); + ci_end = ci_off +folio_nr_pages (old); +while (ci_off++ < ci_end) +WARN_ON_ONCE (swp_tb_to_folio (__swap_table_get (ci, ci_off)) != old); + } }/** * swap_cache_clear_shadow - Clears a set of shadows in the swap cache. -* @type: Indicates the swap device. -* @begin: Beginning offset of the range. -* @end: Ending offset of the range. +* @entry: The starting index entry. +* @nr_ents: How many slots need to be cleared. * -* Context: Caller must ensure the range is valid and hold a reference to -* the swap device. +* Context: Caller must ensure the range is valid, not occupied by, +* any folio and protect the swap device with reference count or locks. */ -void swap_cache_clear_shadow (int type,unsigned long begin, -unsigned long end) +void __swap_cache_clear_shadow (swp_entry_t entry,int nr_ents) { -unsigned long curr = begin; -void *old; - -for (;;) { -swp_entry_t entry =swp_entry (type, curr); -unsigned long index = curr &SWAP_ADDRESS_SPACE_MASK ; -struct address_space *address_space =swap_address_space (entry); -XA_STATE (xas, &address_space->i_pages, index); - -xas_set_update (&xas, workingset_update_node); - -xa_lock_irq (&address_space->i_pages); -xas_for_each (&xas, old,min (index + (end - curr),SWAP_ADDRESS_SPACE_PAGES )) { -if (!xa_is_value (old)) -continue ; -xas_store (&xas,NULL ); - } -xa_unlock_irq (&address_space->i_pages); +struct swap_cluster_info *ci =__swap_entry_to_cluster (entry); +unsigned int ci_off =swp_cluster_offset (entry), ci_end; +unsigned long old; -/* search the next swapcache until we meet end */ - curr =ALIGN ((curr +1 ),SWAP_ADDRESS_SPACE_PAGES ); -if (curr > end) -break ; - } + ci_end = ci_off + nr_ents; +do { + old =__swap_table_xchg (ci, ci_off,null_to_swp_tb ()); +WARN_ON_ONCE (swp_tb_is_folio (old)); + }while (++ci_off < ci_end); }/* @@ -482,10 +481,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,if (mem_cgroup_swapin_charge_folio (new_folio,NULL , gfp_mask, entry)) goto fail_unlock; -/* May fail (-ENOMEM) if XArray node allocation failed. */ -if (swap_cache_add_folio (new_folio, entry, gfp_mask &GFP_RECLAIM_MASK , &shadow)) - goto fail_unlock; - +swap_cache_add_folio (new_folio, entry, &shadow);memcg1_swapin (entry,1 );if (shadow)@@ -677,41 +673,6 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,return folio; } -int init_swap_address_space (unsigned int type,unsigned long nr_pages) -{ -struct address_space *spaces, *space; -unsigned int i, nr; - - nr =DIV_ROUND_UP (nr_pages,SWAP_ADDRESS_SPACE_PAGES ); - spaces =kvcalloc (nr,sizeof (struct address_space ),GFP_KERNEL ); -if (!spaces) -return -ENOMEM ; -for (i =0 ; i < nr; i++) { - space = spaces + i; -xa_init_flags (&space->i_pages,XA_FLAGS_LOCK_IRQ ); -atomic_set (&space->i_mmap_writable,0 ); - space->a_ops = &swap_aops; -/* swap cache doesn't use writeback related tags */ -mapping_set_no_writeback_tags (space); - } - nr_swapper_spaces[type] = nr; - swapper_spaces[type] = spaces; - -return 0 ; -} - -void exit_swap_address_space (unsigned int type) -{ -int i; -struct address_space *spaces = swapper_spaces[type]; - -for (i =0 ; i < nr_swapper_spaces[type]; i++) -VM_WARN_ON_ONCE (!mapping_empty (&spaces[i])); -kvfree (spaces); - nr_swapper_spaces[type] =0 ; - swapper_spaces[type] =NULL ; -} -static int swap_vma_ra_win (struct vm_fault *vmf,unsigned long *start,unsigned long *end) {@@ -884,7 +845,7 @@ static const struct attribute_group swap_attr_group = { .attrs = swap_attrs, }; -static int __init swap_init_sysfs (void ) +static int __init swap_init (void ) {int err;struct kobject *swap_kobj;@@ -899,11 +860,13 @@ static int __init swap_init_sysfs(void)pr_err ("failed to register swap group\n" ); goto delete_obj; } +/* Swap cache writeback is LRU based, no tags for it */ +mapping_set_no_writeback_tags (&swap_space);return 0 ; delete_obj:kobject_put (swap_kobj);return err; } -subsys_initcall (swap_init_sysfs); +subsys_initcall (swap_init);#endif mm/swap_table.h new file +97
-0@@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _MM_SWAP_TABLE_H +#define _MM_SWAP_TABLE_H + +#include "swap.h" + +/* +* A swap table entry represents the status of a swap slot on a swap +* (physical or virtual) device. The swap table in each cluster is a +* 1:1 map of the swap slots in this cluster. +* +* Each swap table entry could be a pointer (folio), a XA_VALUE +* (shadow), or NULL. +*/ + +/* +* Helpers for casting one type of info into a swap table entry. +*/ +static inline unsigned long null_to_swp_tb (void ) +{ +BUILD_BUG_ON (sizeof (unsigned long ) !=sizeof (atomic_long_t)); +return 0 ; +} + +static inline unsigned long folio_to_swp_tb (struct folio *folio) +{ +BUILD_BUG_ON (sizeof (unsigned long ) !=sizeof (void *)); +return (unsigned long )folio; +} + +static inline unsigned long shadow_swp_to_tb (void *shadow) +{ +BUILD_BUG_ON ((BITS_PER_XA_VALUE +1 ) != +BITS_PER_BYTE *sizeof (unsigned long )); +VM_WARN_ON_ONCE (shadow && !xa_is_value (shadow)); +return (unsigned long )shadow; +} + +/* +* Helpers for swap table entry type checking. +*/ +static inline bool swp_tb_is_null (unsigned long swp_tb) +{ +return !swp_tb; +} + +static inline bool swp_tb_is_folio (unsigned long swp_tb) +{ +return !xa_is_value ((void *)swp_tb) && !swp_tb_is_null (swp_tb); +} + +static inline bool swp_tb_is_shadow (unsigned long swp_tb) +{ +return xa_is_value ((void *)swp_tb); +} + +/* +* Helpers for retrieving info from swap table. +*/ +static inline struct folio *swp_tb_to_folio (unsigned long swp_tb) +{ +VM_WARN_ON (!swp_tb_is_folio (swp_tb)); +return (void *)swp_tb; +} + +static inline void *swp_tb_to_shadow (unsigned long swp_tb) +{ +VM_WARN_ON (!swp_tb_is_shadow (swp_tb)); +return (void *)swp_tb; +} + +/* +* Helpers for accessing or modifying the swap table of a cluster, +* the swap cluster must be locked. +*/ +static inline void __swap_table_set (struct swap_cluster_info *ci, +unsigned int off,unsigned long swp_tb) +{ +VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); +atomic_long_set (&ci->table[off], swp_tb); +} + +static inline unsigned long __swap_table_xchg (struct swap_cluster_info *ci, +unsigned int off,unsigned long swp_tb) +{ +VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); +/* Ordering is guaranteed by cluster lock, relax */ +return atomic_long_xchg_relaxed (&ci->table[off], swp_tb); +} + +static inline unsigned long __swap_table_get (struct swap_cluster_info *ci, +unsigned int off) +{ +VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); +return atomic_long_read (&ci->table[off]); +} +#endif mm/swapfile.c +75
-25@@ -46,6 +46,7 @@#include <asm/tlbflush.h> #include <linux/swapops.h> #include <linux/swap_cgroup.h> +#include "swap_table.h" #include "internal.h" #include "swap.h" @@ -420,6 +421,34 @@ static inline unsigned int cluster_offset(struct swap_info_struct *si,return cluster_index (si, ci) *SWAPFILE_CLUSTER ; } +static int swap_cluster_alloc_table (struct swap_cluster_info *ci) +{ +WARN_ON (ci->table); + ci->table =kzalloc (sizeof (unsigned long ) *SWAPFILE_CLUSTER ,GFP_KERNEL ); +if (!ci->table) +return -ENOMEM ; +return 0 ; +} + +static void swap_cluster_free_table (struct swap_cluster_info *ci) +{ +unsigned int ci_off; +unsigned long swp_tb; + +if (!ci->table) +return ; + +for (ci_off =0 ; ci_off <SWAPFILE_CLUSTER ; ci_off++) { + swp_tb =__swap_table_get (ci, ci_off); +if (!swp_tb_is_null (swp_tb)) +pr_err_once ("swap: unclean swap space on swapoff: 0x%lx" , + swp_tb); + } + +kfree (ci->table); + ci->table =NULL ; +} +static void move_cluster (struct swap_info_struct *si,struct swap_cluster_info *ci,struct list_head *list,enum swap_cluster_flags new_flags)@@ -702,6 +731,26 @@ static bool cluster_scan_range(struct swap_info_struct *si,return true; } +/* +* Currently, the swap table is not used for count tracking, just +* do a sanity check here to ensure nothing leaked, so the swap +* table should be empty upon freeing. +*/ +static void swap_cluster_assert_table_empty (struct swap_cluster_info *ci, +unsigned int start,unsigned int nr) +{ +unsigned int ci_off = start %SWAPFILE_CLUSTER ; +unsigned int ci_end = ci_off + nr; +unsigned long swp_tb; + +if (IS_ENABLED (CONFIG_DEBUG_VM )) { +do { + swp_tb =__swap_table_get (ci, ci_off); +VM_WARN_ON_ONCE (!swp_tb_is_null (swp_tb)); + }while (++ci_off < ci_end); + } +} +static bool cluster_alloc_range (struct swap_info_struct *si,struct swap_cluster_info *ci,unsigned int start,unsigned char usage,unsigned int order)@@ -721,6 +770,7 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster ci->order = order;memset (si->swap_map + start, usage, nr_pages); +swap_cluster_assert_table_empty (ci, start, nr_pages);swap_range_alloc (si, nr_pages); ci->count += nr_pages;@@ -1123,7 +1173,7 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,swap_slot_free_notify (si->bdev, offset); offset++; } -swap_cache_clear_shadow (si->type, begin, end); +__swap_cache_clear_shadow (swp_entry (si->type, begin), nr_entries);/* * Make sure that try_to_unuse() observes si->inuse_pages reaching 0 @@ -1280,16 +1330,7 @@ int folio_alloc_swap(struct folio *folio, gfp_t gfp)if (!entry.val)return -ENOMEM ; -/* -* XArray node allocations from PF_MEMALLOC contexts could -* completely exhaust the page allocator. __GFP_NOMEMALLOC -* stops emergency reserves from being allocated. -* -* TODO: this could cause a theoretical memory reclaim -* deadlock in the swap out path. -*/ -if (swap_cache_add_folio (folio, entry, gfp | __GFP_NOMEMALLOC,NULL )) - goto out_free; +swap_cache_add_folio (folio, entry,NULL );return 0 ;@@ -1555,6 +1596,7 @@ static void swap_entries_free(struct swap_info_struct *si,mem_cgroup_uncharge_swap (entry, nr_pages);swap_range_free (si, offset, nr_pages); +swap_cluster_assert_table_empty (ci, offset, nr_pages);if (!ci->count)free_cluster (si, ci);@@ -2633,6 +2675,18 @@ static void wait_for_allocation(struct swap_info_struct *si) } } +static void free_cluster_info (struct swap_cluster_info *cluster_info, +unsigned long maxpages) +{ +int i, nr_clusters =DIV_ROUND_UP (maxpages,SWAPFILE_CLUSTER ); + +if (!cluster_info) +return ; +for (i =0 ; i < nr_clusters; i++) +swap_cluster_free_table (&cluster_info[i]); +kvfree (cluster_info); +} +/* * Called after swap device's reference count is dead, so * neither scan nor allocation will use it. @@ -2767,12 +2821,13 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) swap_file = p->swap_file; p->swap_file =NULL ; - p->max =0 ; swap_map = p->swap_map; p->swap_map =NULL ; zeromap = p->zeromap; p->zeromap =NULL ; cluster_info = p->cluster_info; +free_cluster_info (cluster_info, p->max); + p->max =0 ; p->cluster_info =NULL ;spin_unlock (&p->lock);spin_unlock (&swap_lock);@@ -2783,10 +2838,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) p->global_cluster =NULL ;vfree (swap_map);kvfree (zeromap); -kvfree (cluster_info);/* Destroy swap account information */ swap_cgroup_swapoff (p->type); -exit_swap_address_space (p->type); inode = mapping->host;@@ -3170,8 +3223,11 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,if (!cluster_info) goto err; -for (i =0 ; i < nr_clusters; i++) +for (i =0 ; i < nr_clusters; i++) {spin_lock_init (&cluster_info[i].lock); +if (swap_cluster_alloc_table (&cluster_info[i])) + goto err_free; + }if (!(si->flags &SWP_SOLIDSTATE )) { si->global_cluster = kmalloc(sizeof(*si->global_cluster),@@ -3232,9 +3288,8 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, }return cluster_info; - err_free: -kvfree (cluster_info); +free_cluster_info (cluster_info, maxpages); err:return ERR_PTR (err); }@@ -3428,13 +3483,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) } } - error =init_swap_address_space (si->type, maxpages); -if (error) - goto bad_swap_unlock_inode; - error =zswap_swapon (si->type, maxpages);if (error) - goto free_swap_address_space; + goto bad_swap_unlock_inode;/* * Flush any pending IO and dirty mappings before we start using this @@ -3469,8 +3520,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto out; free_swap_zswap:zswap_swapoff (si->type); -free_swap_address_space: -exit_swap_address_space (si->type); bad_swap_unlock_inode:inode_unlock (inode); bad_swap:@@ -3485,7 +3534,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)spin_unlock (&swap_lock);vfree (swap_map);kvfree (zeromap); -kvfree (cluster_info); +if (cluster_info) +free_cluster_info (cluster_info, maxpages);if (inced_nr_rotate_swap)atomic_dec (&nr_rotate_swap);if (swap_file)mm/vmscan.c +14
-7@@ -730,13 +730,18 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio, {int refcount;void *shadow =NULL ; +struct swap_cluster_info *ci;BUG_ON (!folio_test_locked (folio));BUG_ON (mapping !=folio_mapping (folio)); -if (!folio_test_swapcache (folio)) +if (folio_test_swapcache (folio)) { + ci =swap_cluster_get_and_lock_irq (folio); + }else {spin_lock (&mapping->host->i_lock); -xa_lock_irq (&mapping->i_pages); +xa_lock_irq (&mapping->i_pages); + } +/* * The non racy check for a busy folio. * @@ -776,9 +781,9 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,if (reclaimed && !mapping_exiting (mapping)) shadow =workingset_eviction (folio, target_memcg); -__swap_cache_del_folio (folio, swap, shadow); +__swap_cache_del_folio (ci, folio, swap, shadow);memcg1_swapout (folio, swap); -xa_unlock_irq (&mapping->i_pages); +swap_cluster_unlock_irq (ci);put_swap_folio (folio, swap); }else {void (*free_folio)(struct folio *);@@ -816,9 +821,12 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,return 1 ; cannot_free: -xa_unlock_irq (&mapping->i_pages); -if (!folio_test_swapcache (folio)) +if (folio_test_swapcache (folio)) { +swap_cluster_unlock_irq (ci); + }else { +xa_unlock_irq (&mapping->i_pages);spin_unlock (&mapping->host->i_lock); + }return 0 ; } --2.51.0
Lance Yang
reviewerRe: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
Quoted 12 lines · Lance Yang
Hi Kairui,
I'm hitting a build error with allnoconfig:
In file included from
mm/shmem.c:44:mm/swap.h: In function ‘folio_index’:mm/swap.h:462:24: error: implicit declaration of function ‘swp_offset’; did you mean ‘pmd_offset’? [-Wimplicit-function-declaration]
462 | returnswp_offset(folio->swap);
Quoted 2 lines
It looks like a header might be missing in
mm/swap.h. Please let me know if you need any more information.
Confirmed that just adding #include <linux/swapops.h> into mm/swap.h fixes it.
mm/swap.h +1 -0
@@ -3,6 +3,7 @@#define _MM_SWAP_H#include <linux/atomic.h> /* for atomic_long_t */ +#include <linux/swapops.h> struct mempolicy ;struct swap_iocb ;
Cheers,
Lance
Quoted 1,280 lines
Cheers,
LanceQuoted 1,274 lines · Kairui Song
From: Kairui Song <[email protected]>
Introduce basic swap table infrastructures, which are now just a fixed-sized flat array inside each swap cluster, with access wrappers.
Each cluster contains a swap table of 512 entries. Each table entry is an opaque atomic long. It could be in 3 types: a shadow type (
XA_VALUE), a folio type (pointer), or NULL.In this first step, it only supports storing a folio or shadow, and it is a drop-in replacement for the current swap cache. Convert all swap cache users to use the new sets of APIs. Chris Li has been suggesting using a new infrastructure for swap cache for better performance, and that idea combined well with the swap table as the new backing structure. Now the lock contention range is reduced to 2M clusters, which is much smaller than the 64M
address_space. And we can also drop the multipleaddress_spacedesign.All the internal works are done with swap_cache_get_* helpers. Swap cache lookup is still lock-less like before, and the helper's contexts are same with original swap cache helpers. They still require a pin on the swap device to prevent the backing data from being freed.
Swap cache updates are now protected by the swap cluster lock instead of the Xarray lock. This is mostly handled internally, but new __swap_cache_* helpers require the caller to lock the cluster. So, a few new cluster access and locking helpers are also introduced.
A fully cluster-based unified swap table can be implemented on top of this to take care of all count tracking and synchronization work, with dynamic allocation. It should reduce the memory usage while making the performance even better.
Co-developed-by Chris Li <[email protected]>Signed-off-by Chris Li <[email protected]>Signed-off-by Kairui Song <[email protected]>Acked-by Chris Li <[email protected]>--- MAINTAINERS | 1 + include/linux/swap.h | 2 - mm/huge_memory.c | 13 +- mm/migrate.c | 19 ++- mm/shmem.c | 8 +- mm/swap.h | 154 +++++++++++++++++------ mm/swap_state.c | 293 +++++++++++++++++++------------------------ mm/swap_table.h | 97 ++++++++++++++ mm/swapfile.c | 100 +++++++++++---- mm/vmscan.c | 20 ++- 10 files changed, 459 insertions(+), 248 deletions(-) create mode 100644 mm/swap_table.hMAINTAINERS +1
-0@@ -16232,6 +16232,7 @@ F: include/linux/swapops.h F: mm/page_io.c F: mm/swap.c F: mm/swap.h +F: mm/swap_table.h F: mm/swap_state.c F: mm/swapfile.cinclude/linux/swap.h +0
-2@@ -480,8 +480,6 @@ extern int __swap_count(swp_entry_t entry);extern bool swap_entry_swapped (struct swap_info_struct *si,swp_entry_t entry);extern int swp_swapcount (swp_entry_t entry);struct backing_dev_info ; -extern int init_swap_address_space (unsigned int type,unsigned long nr_pages); -extern void exit_swap_address_space (unsigned int type);extern struct swap_info_struct *get_swap_device (swp_entry_t entry);sector_t swap_folio_sector (struct folio *folio);mm/huge_memory.c +6
-7@@ -3720,7 +3720,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,/* Prevent deferred_split_scan() touching ->_refcount */ spin_lock (&ds_queue->split_queue_lock);if (folio_ref_freeze (folio,1 + extra_pins)) { -struct address_space *swap_cache =NULL ; +struct swap_cluster_info *ci =NULL ;struct lruvec *lruvec;int expected_refs;@@ -3764,8 +3764,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order, goto fail; } - swap_cache =swap_address_space (folio->swap); -xa_lock (&swap_cache->i_pages); + ci =swap_cluster_get_and_lock (folio); }/* lock lru list/PageCompound, ref frozen by page_ref_freeze */ @@ -3797,8 +3796,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* Anonymous folio with swap cache. * NOTE: shmem in swap cache is not supported yet. */ -if (swap_cache) { -__swap_cache_replace_folio (folio, new_folio); +if (ci) { +__swap_cache_replace_folio (ci, folio, new_folio);continue ; }@@ -3833,8 +3832,8 @@ static int __folio_split(struct folio *folio, unsigned int new_order,unlock_page_lruvec (lruvec); -if (swap_cache) -xa_unlock (&swap_cache->i_pages); +if (ci) +swap_cluster_unlock (ci); }else {spin_unlock (&ds_queue->split_queue_lock); ret = -EAGAIN ;mm/migrate.c +15
-4@@ -563,6 +563,7 @@ static int __folio_migrate_mapping(struct address_space *mapping, struct folio *newfolio, struct folio *folio, int expected_count) { XA_STATE(xas, &mapping->i_pages, folio_index(folio)); +struct swap_cluster_info *ci =NULL ;struct zone *oldzone, *newzone;int dirty;long nr =folio_nr_pages (folio);@@ -591,9 +592,16 @@ static int __folio_migrate_mapping(struct address_space *mapping, oldzone =folio_zone (folio); newzone =folio_zone (newfolio); -xas_lock_irq (&xas); +if (folio_test_swapcache (folio)) + ci =swap_cluster_get_and_lock_irq (folio); +else +xas_lock_irq (&xas); +if (!folio_ref_freeze (folio, expected_count)) { -xas_unlock_irq (&xas); +if (ci) +swap_cluster_unlock (ci); +else +xas_unlock_irq (&xas);return -EAGAIN ; }@@ -624,7 +632,7 @@ static int __folio_migrate_mapping(struct address_space *mapping, }if (folio_test_swapcache (folio)) -__swap_cache_replace_folio (folio, newfolio); +__swap_cache_replace_folio (ci, folio, newfolio);else xas_store (&xas, newfolio);@@ -635,8 +643,11 @@ static int __folio_migrate_mapping(struct address_space *mapping,*/ folio_ref_unfreeze (folio, expected_count - nr); -xas_unlock (&xas);/* Leave irq disabled to prevent preemption while updating stats */ +if (ci) +swap_cluster_unlock (ci); +else +xas_unlock (&xas);/* * If moved to a different zone then also account mm/shmem.c +4
-4@@ -2083,9 +2083,9 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, struct shmem_inode_info *info, pgoff_t index, struct vm_area_struct *vma) { + struct swap_cluster_info *ci;struct folio *new, *old = *foliop;swp_entry_t entry = old->swap; -struct address_space *swap_mapping =swap_address_space (entry);int nr_pages =folio_nr_pages (old);int error =0 ;@@ -2116,9 +2116,9 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, new->swap = entry;folio_set_swapcache (new); -xa_lock_irq (&swap_mapping->i_pages); -__swap_cache_replace_folio (old, new); -xa_unlock_irq (&swap_mapping->i_pages); + ci =swap_cluster_get_and_lock_irq (old); +__swap_cache_replace_folio (ci, old, new); +swap_cluster_unlock (ci);mem_cgroup_replace_folio (old, new);shmem_update_stats (new, nr_pages);mm/swap.h +119
-35@@ -2,6 +2,7 @@#ifndef _MM_SWAP_H#define _MM_SWAP_H +#include <linux/atomic.h> /* for atomic_long_t */ struct mempolicy ;struct swap_iocb ;@@ -35,6 +36,7 @@ struct swap_cluster_info {u16 count;u8 flags;u8 order; +atomic_long_t *table;/* Swap table entries, see mm/swap_table.h */ struct list_head list; };@@ -55,6 +57,11 @@ enum swap_cluster_flags {#include <linux/swapops.h> /* for swp_offset */ #include <linux/blk_types.h> /* for bio_end_io_t */ +static inline unsigned int swp_cluster_offset (swp_entry_t entry) +{ +return swp_offset (entry) %SWAPFILE_CLUSTER ; +} +/* * Callers of all helpers below must ensure the entry, type, or offset is * valid, and protect the swap device with reference count or locks. @@ -81,6 +88,25 @@ static inline struct swap_cluster_info *__swap_offset_to_cluster(return &si->cluster_info[offset /SWAPFILE_CLUSTER ]; } +static inline struct swap_cluster_info *__swap_entry_to_cluster (swp_entry_t entry) +{ +return __swap_offset_to_cluster (__swap_entry_to_info (entry), +swp_offset (entry)); +} + +static __always_inline struct swap_cluster_info *__swap_cluster_lock( +struct swap_info_struct *si,unsigned long offset,bool irq) +{ +struct swap_cluster_info *ci =__swap_offset_to_cluster (si, offset); + +VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ +if (irq) +spin_lock_irq (&ci->lock); +else +spin_lock (&ci->lock); +return ci; +} +/** * swap_cluster_lock - Lock and return the swap cluster of given offset. * @si: swap device the cluster belongs to. @@ -92,11 +118,49 @@ static inline struct swap_cluster_info *__swap_offset_to_cluster(static inline struct swap_cluster_info *swap_cluster_lock (struct swap_info_struct *si,unsigned long offset) { -struct swap_cluster_info *ci =__swap_offset_to_cluster (si, offset); +return __swap_cluster_lock (si, offset, false); +} -VM_WARN_ON_ONCE (percpu_ref_is_zero (&si->users));/* race with swapoff */ -spin_lock (&ci->lock); -return ci; +static inline struct swap_cluster_info *__swap_cluster_get_and_lock ( +const struct folio *folio,bool irq) +{ +VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio); +VM_WARN_ON_ONCE_FOLIO (!folio_test_swapcache (folio), folio); +return __swap_cluster_lock (__swap_entry_to_info (folio->swap), +swp_offset (folio->swap), irq); +} + +/* +* swap_cluster_get_and_lock - Locks the cluster that holds a folio's entries. +* @folio: The folio. +* +* This locks and returns the swap cluster that contains a folio's swap +* entries. The swap entries of a folio are always in one single cluster. +* The folio has to be locked so its swap entries won't change and the +* cluster won't be freed. +* +* Context: Caller must ensure the folio is locked and in the swap cache. +* Return: Pointer to the swap cluster. +*/ +static inline struct swap_cluster_info *swap_cluster_get_and_lock ( +const struct folio *folio) +{ +return __swap_cluster_get_and_lock (folio, false); +} + +/* +* swap_cluster_get_and_lock_irq - Locks the cluster that holds a folio's entries. +* @folio: The folio. +* +* Same as swap_cluster_get_and_lock but also disable IRQ. +* +* Context: Caller must ensure the folio is locked and in the swap cache. +* Return: Pointer to the swap cluster. +*/ +static inline struct swap_cluster_info *swap_cluster_get_and_lock_irq ( +const struct folio *folio) +{ +return __swap_cluster_get_and_lock (folio, true); }static inline void swap_cluster_unlock (struct swap_cluster_info *ci)@@ -104,6 +168,11 @@ static inline void swap_cluster_unlock(struct swap_cluster_info *ci)spin_unlock (&ci->lock); } +static inline void swap_cluster_unlock_irq (struct swap_cluster_info *ci) +{ +spin_unlock_irq (&ci->lock); +} +/* linux/mm/page_io.c */ int sio_pool_init (void );struct swap_iocb ;@@ -123,10 +192,11 @@ void __swap_writepage(struct folio *folio, struct swap_iocb **swap_plug);#define SWAP_ADDRESS_SPACE_SHIFT 14#define SWAP_ADDRESS_SPACE_PAGES (1 << SWAP_ADDRESS_SPACE_SHIFT)#define SWAP_ADDRESS_SPACE_MASK (SWAP_ADDRESS_SPACE_PAGES - 1) -extern struct address_space *swapper_spaces[]; -#define swap_address_space (entry) \ - (&swapper_spaces[swp_type(entry)][swp_offset(entry) \ - >> SWAP_ADDRESS_SPACE_SHIFT]) +extern struct address_space swap_space; +static inline struct address_space *swap_address_space (swp_entry_t entry) +{ +return &swap_space; +}/* * Return the swap device position of the swap entry. @@ -136,15 +206,6 @@ static inline loff_t swap_dev_pos(swp_entry_t entry)return ((loff_t )swp_offset (entry)) <<PAGE_SHIFT ; } -/* -* Return the swap cache index of the swap entry. -*/ -static inline pgoff_t swap_cache_index (swp_entry_t entry) -{ -BUILD_BUG_ON ((SWP_OFFSET_MASK |SWAP_ADDRESS_SPACE_MASK ) !=SWP_OFFSET_MASK ); -return swp_offset (entry) &SWAP_ADDRESS_SPACE_MASK ; -} -/** * folio_matches_swap_entry - Check if a folio matches a given swap entry. * @folio: The folio. @@ -180,14 +241,14 @@ static inline bool folio_matches_swap_entry(const struct folio *folio,*/ struct folio *swap_cache_get_folio (swp_entry_t entry);void *swap_cache_get_shadow (swp_entry_t entry); -int swap_cache_add_folio (struct folio *folio,swp_entry_t entry, -gfp_t gfp,void **shadow); +void swap_cache_add_folio (struct folio *folio,swp_entry_t entry,void **shadow);void swap_cache_del_folio (struct folio *folio); -void __swap_cache_del_folio (struct folio *folio, -swp_entry_t entry,void *shadow); -void __swap_cache_replace_folio (struct folio *old,struct folio *new); -void swap_cache_clear_shadow (int type,unsigned long begin, -unsigned long end); +/* Below helpers require the caller to lock and pass in the swap cluster. */ +void __swap_cache_del_folio (struct swap_cluster_info *ci, +struct folio *folio,swp_entry_t entry,void *shadow); +void __swap_cache_replace_folio (struct swap_cluster_info *ci, +struct folio *old,struct folio *new); +void __swap_cache_clear_shadow (swp_entry_t entry,int nr_ents);void show_swap_cache_info (void );void swapcache_clear (struct swap_info_struct *si,swp_entry_t entry,int nr);@@ -255,6 +316,32 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr)#else /* CONFIG_SWAP */ struct swap_iocb ; +static inline struct swap_cluster_info *swap_cluster_lock ( +struct swap_info_struct *si,pgoff_t offset,bool irq) +{ +return NULL ; +} + +static inline struct swap_cluster_info *swap_cluster_get_and_lock ( +struct folio *folio) +{ +return NULL ; +} + +static inline struct swap_cluster_info *swap_cluster_get_and_lock_irq ( +struct folio *folio) +{ +return NULL ; +} + +static inline void swap_cluster_unlock (struct swap_cluster_info *ci) +{ +} + +static inline void swap_cluster_unlock_irq (struct swap_cluster_info *ci) +{ +} +static inline struct swap_info_struct *__swap_entry_to_info (swp_entry_t entry) {return NULL ;@@ -272,11 +359,6 @@ static inline struct address_space *swap_address_space(swp_entry_t entry)return NULL ; } -static inline pgoff_t swap_cache_index (swp_entry_t entry) -{ -return 0 ; -} -static inline bool folio_matches_swap_entry (const struct folio *folio,swp_entry_t entry) {return false;@@ -323,21 +405,21 @@ static inline void *swap_cache_get_shadow(swp_entry_t entry)return NULL ; } -static inline int swap_cache_add_folio (swp_entry_t entry,struct folio *folio, -gfp_t gfp,void **shadow) +static inline void swap_cache_add_folio (struct folio *folio,swp_entry_t entry,void **shadow) { -return -EINVAL ; }static inline void swap_cache_del_folio (struct folio *folio) { } -static inline void __swap_cache_del_folio (struct folio *folio,swp_entry_t entry,void *shadow) +static inline void __swap_cache_del_folio (struct swap_cluster_info *ci, +struct folio *folio,swp_entry_t entry,void *shadow) { } -static inline void __swap_cache_replace_folio (struct folio *old,struct folio *new) +static inline void __swap_cache_replace_folio (struct swap_cluster_info *ci, +struct folio *old,struct folio *new) { }@@ -371,8 +453,10 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr)*/ static inline pgoff_t folio_index (struct folio *folio) { +#ifdef CONFIG_SWAP if (unlikely (folio_test_swapcache (folio))) -return swap_cache_index (folio->swap); +return swp_offset (folio->swap); +#endif return folio->index; }mm/swap_state.c +128
-165@@ -23,6 +23,7 @@#include <linux/huge_mm.h> #include <linux/shmem_fs.h> #include "internal.h" +#include "swap_table.h" #include "swap.h" /* @@ -36,8 +37,10 @@ static const struct address_space_operations swap_aops = {#endif }; -struct address_space *swapper_spaces[MAX_SWAPFILES ]__read_mostly ; -static unsigned int nr_swapper_spaces[MAX_SWAPFILES ]__read_mostly ; +struct address_space swap_space__read_mostly = { + .a_ops = &swap_aops, +}; +static bool enable_vma_readahead__read_mostly = true;#define SWAP_RA_ORDER_CEILING 5@@ -83,11 +86,20 @@ void show_swap_cache_info(void)*/ struct folio *swap_cache_get_folio (swp_entry_t entry) { -struct folio *folio =filemap_get_folio (swap_address_space (entry), -swap_cache_index (entry)); -if (IS_ERR (folio)) -return NULL ; -return folio; +unsigned long swp_tb; +struct folio *folio; + +for (;;) { + swp_tb =__swap_table_get (__swap_entry_to_cluster (entry), +swp_cluster_offset (entry)); +if (!swp_tb_is_folio (swp_tb)) +return NULL ; + folio =swp_tb_to_folio (swp_tb); +if (likely (folio_try_get (folio))) +return folio; + } + +return NULL ; }/** @@ -100,13 +112,13 @@ struct folio *swap_cache_get_folio(swp_entry_t entry)*/ void *swap_cache_get_shadow (swp_entry_t entry) { -struct address_space *address_space =swap_address_space (entry); -pgoff_t idx =swap_cache_index (entry); -void *shadow; +unsigned long swp_tb; + + swp_tb =__swap_table_get (__swap_entry_to_cluster (entry), +swp_cluster_offset (entry)); +if (swp_tb_is_shadow (swp_tb)) +return swp_tb_to_shadow (swp_tb); - shadow =xa_load (&address_space->i_pages, idx); -if (xa_is_value (shadow)) -return shadow;return NULL ; }@@ -119,61 +131,48 @@ void *swap_cache_get_shadow(swp_entry_t entry)* * Context: Caller must ensure @entry is valid and protect the swap device * with reference count or locks. -* The caller also needs to mark the corresponding swap_map slots with -* SWAP_HAS_CACHE to avoid race or conflict. -* Return: Returns 0 on success, error code otherwise. +* The caller also needs to update the corresponding swap_map slots with +* SWAP_HAS_CACHE bit to avoid race or conflict. */ -int swap_cache_add_folio (struct folio *folio,swp_entry_t entry, -gfp_t gfp,void **shadowp) +void swap_cache_add_folio (struct folio *folio,swp_entry_t entry,void **shadowp) { -struct address_space *address_space =swap_address_space (entry); -pgoff_t idx =swap_cache_index (entry); -XA_STATE_ORDER (xas, &address_space->i_pages, idx,folio_order (folio)); -unsigned long i, nr =folio_nr_pages (folio); -void *old; - -xas_set_update (&xas, workingset_update_node); - -VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio); -VM_BUG_ON_FOLIO (folio_test_swapcache (folio), folio); -VM_BUG_ON_FOLIO (!folio_test_swapbacked (folio), folio); +void *shadow =NULL ; +unsigned long old_tb, new_tb; +struct swap_cluster_info *ci; +unsigned int ci_start, ci_off, ci_end; +unsigned long nr_pages =folio_nr_pages (folio); + +VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_swapcache (folio), folio); +VM_WARN_ON_ONCE_FOLIO (!folio_test_swapbacked (folio), folio); + + new_tb =folio_to_swp_tb (folio); + ci_start =swp_cluster_offset (entry); + ci_end = ci_start + nr_pages; + ci_off = ci_start; + ci =swap_cluster_lock (__swap_entry_to_info (entry),swp_offset (entry)); +do { + old_tb =__swap_table_xchg (ci, ci_off, new_tb); +WARN_ON_ONCE (swp_tb_is_folio (old_tb)); +if (swp_tb_is_shadow (old_tb)) + shadow =swp_tb_to_shadow (old_tb); + }while (++ci_off < ci_end); -folio_ref_add (folio, nr); +folio_ref_add (folio, nr_pages);folio_set_swapcache (folio); folio->swap = entry; +swap_cluster_unlock (ci); -do { -xas_lock_irq (&xas); -xas_create_range (&xas); -if (xas_error (&xas)) - goto unlock; -for (i =0 ; i < nr; i++) { -VM_BUG_ON_FOLIO (xas.xa_index != idx + i, folio); -if (shadowp) { - old =xas_load (&xas); -if (xa_is_value (old)) - *shadowp = old; - } -xas_store (&xas, folio); -xas_next (&xas); - } - address_space->nrpages += nr; -__node_stat_mod_folio (folio,NR_FILE_PAGES , nr); -__lruvec_stat_mod_folio (folio,NR_SWAPCACHE , nr); -unlock: -xas_unlock_irq (&xas); - }while (xas_nomem (&xas, gfp)); - -if (!xas_error (&xas)) -return 0 ; +node_stat_mod_folio (folio,NR_FILE_PAGES , nr_pages); +lruvec_stat_mod_folio (folio,NR_SWAPCACHE , nr_pages); -folio_clear_swapcache (folio); -folio_ref_sub (folio, nr); -return xas_error (&xas); +if (shadowp) + *shadowp = shadow; }/** * __swap_cache_del_folio - Removes a folio from the swap cache. +* @ci: The locked swap cluster. * @folio: The folio. * @entry: The first swap entry that the folio corresponds to. * @shadow: shadow value to be filled in the swap cache. @@ -181,34 +180,36 @@ int swap_cache_add_folio(struct folio *folio, swp_entry_t entry,* Removes a folio from the swap cache and fills a shadow in place. * This won't put the folio's refcount. The caller has to do that. * -* Context: Caller must hold the xa_lock, ensure the folio is -* locked and in the swap cache, using the index of @entry. +* Context: Caller must ensure the folio is locked and in the swap cache +* using the index of @entry, and lock the cluster that holds the entries. */ -void __swap_cache_del_folio (struct folio *folio, +void __swap_cache_del_folio (struct swap_cluster_info *ci,struct folio *folio,swp_entry_t entry,void *shadow) { -struct address_space *address_space =swap_address_space (entry); -int i; -long nr =folio_nr_pages (folio); -pgoff_t idx =swap_cache_index (entry); -XA_STATE (xas, &address_space->i_pages, idx); - -xas_set_update (&xas, workingset_update_node); - -VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio); -VM_BUG_ON_FOLIO (!folio_test_swapcache (folio), folio); -VM_BUG_ON_FOLIO (folio_test_writeback (folio), folio); - -for (i =0 ; i < nr; i++) { -void *entry =xas_store (&xas, shadow); -VM_BUG_ON_PAGE (entry != folio, entry); -xas_next (&xas); - } +unsigned long old_tb, new_tb; +unsigned int ci_start, ci_off, ci_end; +unsigned long nr_pages =folio_nr_pages (folio); + +VM_WARN_ON_ONCE (__swap_entry_to_cluster (entry) != ci); +VM_WARN_ON_ONCE_FOLIO (!folio_test_locked (folio), folio); +VM_WARN_ON_ONCE_FOLIO (!folio_test_swapcache (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_writeback (folio), folio); + + new_tb =shadow_swp_to_tb (shadow); + ci_start =swp_cluster_offset (entry); + ci_end = ci_start + nr_pages; + ci_off = ci_start; +do { +/* If shadow is NULL, we sets an empty shadow */ + old_tb =__swap_table_xchg (ci, ci_off, new_tb); +WARN_ON_ONCE (!swp_tb_is_folio (old_tb) || +swp_tb_to_folio (old_tb) != folio); + }while (++ci_off < ci_end); + folio->swap.val =0 ;folio_clear_swapcache (folio); - address_space->nrpages -= nr; -__node_stat_mod_folio (folio,NR_FILE_PAGES , -nr); -__lruvec_stat_mod_folio (folio,NR_SWAPCACHE , -nr); +node_stat_mod_folio (folio,NR_FILE_PAGES , -nr_pages); +lruvec_stat_mod_folio (folio,NR_SWAPCACHE , -nr_pages); }/** @@ -223,12 +224,12 @@ void __swap_cache_del_folio(struct folio *folio,*/ void swap_cache_del_folio (struct folio *folio) { +struct swap_cluster_info *ci;swp_entry_t entry = folio->swap; -struct address_space *address_space =swap_address_space (entry); -xa_lock_irq (&address_space->i_pages); -__swap_cache_del_folio (folio, entry,NULL ); -xa_unlock_irq (&address_space->i_pages); + ci =swap_cluster_lock (__swap_entry_to_info (entry),swp_offset (entry)); +__swap_cache_del_folio (ci, folio, entry,NULL ); +swap_cluster_unlock (ci);put_swap_folio (folio, entry);folio_ref_sub (folio,folio_nr_pages (folio));@@ -236,6 +237,7 @@ void swap_cache_del_folio(struct folio *folio)/** * __swap_cache_replace_folio - Replace a folio in the swap cache. +* @ci: The locked swap cluster. * @old: The old folio to be replaced. * @new: The new folio. * @@ -244,65 +246,62 @@ void swap_cache_del_folio(struct folio *folio)* entries. Replacement will take the new folio's swap entry value as * the starting offset to override all slots covered by the new folio. * -* Context: Caller must ensure both folios are locked, also lock the -* swap address_space that holds the old folio to avoid races. +* Context: Caller must ensure both folios are locked, and lock the +* cluster that holds the old folio to be replaced. */ -void __swap_cache_replace_folio (struct folio *old,struct folio *new) +void __swap_cache_replace_folio (struct swap_cluster_info *ci, +struct folio *old,struct folio *new) {swp_entry_t entry = new->swap;unsigned long nr_pages =folio_nr_pages (new); -unsigned long offset =swap_cache_index (entry); -unsigned long end = offset + nr_pages; - -XA_STATE (xas, &swap_address_space (entry)->i_pages, offset); +unsigned int ci_off =swp_cluster_offset (entry); +unsigned int ci_end = ci_off + nr_pages; +unsigned long old_tb, new_tb;VM_WARN_ON_ONCE (!folio_test_swapcache (old) || !folio_test_swapcache (new));VM_WARN_ON_ONCE (!folio_test_locked (old) || !folio_test_locked (new));VM_WARN_ON_ONCE (!entry.val);/* Swap cache still stores N entries instead of a high-order entry */ + new_tb =folio_to_swp_tb (new);do { -WARN_ON_ONCE (xas_store (&xas, new) != old); -xas_next (&xas); - }while (++offset < end); + old_tb =__swap_table_xchg (ci, ci_off, new_tb); +WARN_ON_ONCE (!swp_tb_is_folio (old_tb) ||swp_tb_to_folio (old_tb) != old); + }while (++ci_off < ci_end); + +/* +* If the old folio is partially replaced (e.g., splitting a large +* folio, the old folio is shrunk, and new split sub folios replace +* the shrunk part), ensure the new folio doesn't overlap it. +*/ +if (IS_ENABLED (CONFIG_DEBUG_VM ) && +folio_order (old) !=folio_order (new)) { + ci_off =swp_cluster_offset (old->swap); + ci_end = ci_off +folio_nr_pages (old); +while (ci_off++ < ci_end) +WARN_ON_ONCE (swp_tb_to_folio (__swap_table_get (ci, ci_off)) != old); + } }/** * swap_cache_clear_shadow - Clears a set of shadows in the swap cache. -* @type: Indicates the swap device. -* @begin: Beginning offset of the range. -* @end: Ending offset of the range. +* @entry: The starting index entry. +* @nr_ents: How many slots need to be cleared. * -* Context: Caller must ensure the range is valid and hold a reference to -* the swap device. +* Context: Caller must ensure the range is valid, not occupied by, +* any folio and protect the swap device with reference count or locks. */ -void swap_cache_clear_shadow (int type,unsigned long begin, -unsigned long end) +void __swap_cache_clear_shadow (swp_entry_t entry,int nr_ents) { -unsigned long curr = begin; -void *old; - -for (;;) { -swp_entry_t entry =swp_entry (type, curr); -unsigned long index = curr &SWAP_ADDRESS_SPACE_MASK ; -struct address_space *address_space =swap_address_space (entry); -XA_STATE (xas, &address_space->i_pages, index); - -xas_set_update (&xas, workingset_update_node); - -xa_lock_irq (&address_space->i_pages); -xas_for_each (&xas, old,min (index + (end - curr),SWAP_ADDRESS_SPACE_PAGES )) { -if (!xa_is_value (old)) -continue ; -xas_store (&xas,NULL ); - } -xa_unlock_irq (&address_space->i_pages); +struct swap_cluster_info *ci =__swap_entry_to_cluster (entry); +unsigned int ci_off =swp_cluster_offset (entry), ci_end; +unsigned long old; -/* search the next swapcache until we meet end */ - curr =ALIGN ((curr +1 ),SWAP_ADDRESS_SPACE_PAGES ); -if (curr > end) -break ; - } + ci_end = ci_off + nr_ents; +do { + old =__swap_table_xchg (ci, ci_off,null_to_swp_tb ()); +WARN_ON_ONCE (swp_tb_is_folio (old)); + }while (++ci_off < ci_end); }/* @@ -482,10 +481,7 @@ struct folio *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,if (mem_cgroup_swapin_charge_folio (new_folio,NULL , gfp_mask, entry)) goto fail_unlock; -/* May fail (-ENOMEM) if XArray node allocation failed. */ -if (swap_cache_add_folio (new_folio, entry, gfp_mask &GFP_RECLAIM_MASK , &shadow)) - goto fail_unlock; - +swap_cache_add_folio (new_folio, entry, &shadow);memcg1_swapin (entry,1 );if (shadow)@@ -677,41 +673,6 @@ struct folio *swap_cluster_readahead(swp_entry_t entry, gfp_t gfp_mask,return folio; } -int init_swap_address_space (unsigned int type,unsigned long nr_pages) -{ -struct address_space *spaces, *space; -unsigned int i, nr; - - nr =DIV_ROUND_UP (nr_pages,SWAP_ADDRESS_SPACE_PAGES ); - spaces =kvcalloc (nr,sizeof (struct address_space ),GFP_KERNEL ); -if (!spaces) -return -ENOMEM ; -for (i =0 ; i < nr; i++) { - space = spaces + i; -xa_init_flags (&space->i_pages,XA_FLAGS_LOCK_IRQ ); -atomic_set (&space->i_mmap_writable,0 ); - space->a_ops = &swap_aops; -/* swap cache doesn't use writeback related tags */ -mapping_set_no_writeback_tags (space); - } - nr_swapper_spaces[type] = nr; - swapper_spaces[type] = spaces; - -return 0 ; -} - -void exit_swap_address_space (unsigned int type) -{ -int i; -struct address_space *spaces = swapper_spaces[type]; - -for (i =0 ; i < nr_swapper_spaces[type]; i++) -VM_WARN_ON_ONCE (!mapping_empty (&spaces[i])); -kvfree (spaces); - nr_swapper_spaces[type] =0 ; - swapper_spaces[type] =NULL ; -} -static int swap_vma_ra_win (struct vm_fault *vmf,unsigned long *start,unsigned long *end) {@@ -884,7 +845,7 @@ static const struct attribute_group swap_attr_group = { .attrs = swap_attrs, }; -static int __init swap_init_sysfs (void ) +static int __init swap_init (void ) {int err;struct kobject *swap_kobj;@@ -899,11 +860,13 @@ static int __init swap_init_sysfs(void)pr_err ("failed to register swap group\n" ); goto delete_obj; } +/* Swap cache writeback is LRU based, no tags for it */ +mapping_set_no_writeback_tags (&swap_space);return 0 ; delete_obj:kobject_put (swap_kobj);return err; } -subsys_initcall (swap_init_sysfs); +subsys_initcall (swap_init);#endif mm/swap_table.h new file +97
-0@@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _MM_SWAP_TABLE_H +#define _MM_SWAP_TABLE_H + +#include "swap.h" + +/* +* A swap table entry represents the status of a swap slot on a swap +* (physical or virtual) device. The swap table in each cluster is a +* 1:1 map of the swap slots in this cluster. +* +* Each swap table entry could be a pointer (folio), a XA_VALUE +* (shadow), or NULL. +*/ + +/* +* Helpers for casting one type of info into a swap table entry. +*/ +static inline unsigned long null_to_swp_tb (void ) +{ +BUILD_BUG_ON (sizeof (unsigned long ) !=sizeof (atomic_long_t)); +return 0 ; +} + +static inline unsigned long folio_to_swp_tb (struct folio *folio) +{ +BUILD_BUG_ON (sizeof (unsigned long ) !=sizeof (void *)); +return (unsigned long )folio; +} + +static inline unsigned long shadow_swp_to_tb (void *shadow) +{ +BUILD_BUG_ON ((BITS_PER_XA_VALUE +1 ) != +BITS_PER_BYTE *sizeof (unsigned long )); +VM_WARN_ON_ONCE (shadow && !xa_is_value (shadow)); +return (unsigned long )shadow; +} + +/* +* Helpers for swap table entry type checking. +*/ +static inline bool swp_tb_is_null (unsigned long swp_tb) +{ +return !swp_tb; +} + +static inline bool swp_tb_is_folio (unsigned long swp_tb) +{ +return !xa_is_value ((void *)swp_tb) && !swp_tb_is_null (swp_tb); +} + +static inline bool swp_tb_is_shadow (unsigned long swp_tb) +{ +return xa_is_value ((void *)swp_tb); +} + +/* +* Helpers for retrieving info from swap table. +*/ +static inline struct folio *swp_tb_to_folio (unsigned long swp_tb) +{ +VM_WARN_ON (!swp_tb_is_folio (swp_tb)); +return (void *)swp_tb; +} + +static inline void *swp_tb_to_shadow (unsigned long swp_tb) +{ +VM_WARN_ON (!swp_tb_is_shadow (swp_tb)); +return (void *)swp_tb; +} + +/* +* Helpers for accessing or modifying the swap table of a cluster, +* the swap cluster must be locked. +*/ +static inline void __swap_table_set (struct swap_cluster_info *ci, +unsigned int off,unsigned long swp_tb) +{ +VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); +atomic_long_set (&ci->table[off], swp_tb); +} + +static inline unsigned long __swap_table_xchg (struct swap_cluster_info *ci, +unsigned int off,unsigned long swp_tb) +{ +VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); +/* Ordering is guaranteed by cluster lock, relax */ +return atomic_long_xchg_relaxed (&ci->table[off], swp_tb); +} + +static inline unsigned long __swap_table_get (struct swap_cluster_info *ci, +unsigned int off) +{ +VM_WARN_ON_ONCE (off >=SWAPFILE_CLUSTER ); +return atomic_long_read (&ci->table[off]); +} +#endif mm/swapfile.c +75
-25@@ -46,6 +46,7 @@#include <asm/tlbflush.h> #include <linux/swapops.h> #include <linux/swap_cgroup.h> +#include "swap_table.h" #include "internal.h" #include "swap.h" @@ -420,6 +421,34 @@ static inline unsigned int cluster_offset(struct swap_info_struct *si,return cluster_index (si, ci) *SWAPFILE_CLUSTER ; } +static int swap_cluster_alloc_table (struct swap_cluster_info *ci) +{ +WARN_ON (ci->table); + ci->table =kzalloc (sizeof (unsigned long ) *SWAPFILE_CLUSTER ,GFP_KERNEL ); +if (!ci->table) +return -ENOMEM ; +return 0 ; +} + +static void swap_cluster_free_table (struct swap_cluster_info *ci) +{ +unsigned int ci_off; +unsigned long swp_tb; + +if (!ci->table) +return ; + +for (ci_off =0 ; ci_off <SWAPFILE_CLUSTER ; ci_off++) { + swp_tb =__swap_table_get (ci, ci_off); +if (!swp_tb_is_null (swp_tb)) +pr_err_once ("swap: unclean swap space on swapoff: 0x%lx" , + swp_tb); + } + +kfree (ci->table); + ci->table =NULL ; +} +static void move_cluster (struct swap_info_struct *si,struct swap_cluster_info *ci,struct list_head *list,enum swap_cluster_flags new_flags)@@ -702,6 +731,26 @@ static bool cluster_scan_range(struct swap_info_struct *si,return true; } +/* +* Currently, the swap table is not used for count tracking, just +* do a sanity check here to ensure nothing leaked, so the swap +* table should be empty upon freeing. +*/ +static void swap_cluster_assert_table_empty (struct swap_cluster_info *ci, +unsigned int start,unsigned int nr) +{ +unsigned int ci_off = start %SWAPFILE_CLUSTER ; +unsigned int ci_end = ci_off + nr; +unsigned long swp_tb; + +if (IS_ENABLED (CONFIG_DEBUG_VM )) { +do { + swp_tb =__swap_table_get (ci, ci_off); +VM_WARN_ON_ONCE (!swp_tb_is_null (swp_tb)); + }while (++ci_off < ci_end); + } +} +static bool cluster_alloc_range (struct swap_info_struct *si,struct swap_cluster_info *ci,unsigned int start,unsigned char usage,unsigned int order)@@ -721,6 +770,7 @@ static bool cluster_alloc_range(struct swap_info_struct *si, struct swap_cluster ci->order = order;memset (si->swap_map + start, usage, nr_pages); +swap_cluster_assert_table_empty (ci, start, nr_pages);swap_range_alloc (si, nr_pages); ci->count += nr_pages;@@ -1123,7 +1173,7 @@ static void swap_range_free(struct swap_info_struct *si, unsigned long offset,swap_slot_free_notify (si->bdev, offset); offset++; } -swap_cache_clear_shadow (si->type, begin, end); +__swap_cache_clear_shadow (swp_entry (si->type, begin), nr_entries);/* * Make sure that try_to_unuse() observes si->inuse_pages reaching 0 @@ -1280,16 +1330,7 @@ int folio_alloc_swap(struct folio *folio, gfp_t gfp)if (!entry.val)return -ENOMEM ; -/* -* XArray node allocations from PF_MEMALLOC contexts could -* completely exhaust the page allocator. __GFP_NOMEMALLOC -* stops emergency reserves from being allocated. -* -* TODO: this could cause a theoretical memory reclaim -* deadlock in the swap out path. -*/ -if (swap_cache_add_folio (folio, entry, gfp | __GFP_NOMEMALLOC,NULL )) - goto out_free; +swap_cache_add_folio (folio, entry,NULL );return 0 ;@@ -1555,6 +1596,7 @@ static void swap_entries_free(struct swap_info_struct *si,mem_cgroup_uncharge_swap (entry, nr_pages);swap_range_free (si, offset, nr_pages); +swap_cluster_assert_table_empty (ci, offset, nr_pages);if (!ci->count)free_cluster (si, ci);@@ -2633,6 +2675,18 @@ static void wait_for_allocation(struct swap_info_struct *si) } } +static void free_cluster_info (struct swap_cluster_info *cluster_info, +unsigned long maxpages) +{ +int i, nr_clusters =DIV_ROUND_UP (maxpages,SWAPFILE_CLUSTER ); + +if (!cluster_info) +return ; +for (i =0 ; i < nr_clusters; i++) +swap_cluster_free_table (&cluster_info[i]); +kvfree (cluster_info); +} +/* * Called after swap device's reference count is dead, so * neither scan nor allocation will use it. @@ -2767,12 +2821,13 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) swap_file = p->swap_file; p->swap_file =NULL ; - p->max =0 ; swap_map = p->swap_map; p->swap_map =NULL ; zeromap = p->zeromap; p->zeromap =NULL ; cluster_info = p->cluster_info; +free_cluster_info (cluster_info, p->max); + p->max =0 ; p->cluster_info =NULL ;spin_unlock (&p->lock);spin_unlock (&swap_lock);@@ -2783,10 +2838,8 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile) p->global_cluster =NULL ;vfree (swap_map);kvfree (zeromap); -kvfree (cluster_info);/* Destroy swap account information */ swap_cgroup_swapoff (p->type); -exit_swap_address_space (p->type); inode = mapping->host;@@ -3170,8 +3223,11 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si,if (!cluster_info) goto err; -for (i =0 ; i < nr_clusters; i++) +for (i =0 ; i < nr_clusters; i++) {spin_lock_init (&cluster_info[i].lock); +if (swap_cluster_alloc_table (&cluster_info[i])) + goto err_free; + }if (!(si->flags &SWP_SOLIDSTATE )) { si->global_cluster = kmalloc(sizeof(*si->global_cluster),@@ -3232,9 +3288,8 @@ static struct swap_cluster_info *setup_clusters(struct swap_info_struct *si, }return cluster_info; - err_free: -kvfree (cluster_info); +free_cluster_info (cluster_info, maxpages); err:return ERR_PTR (err); }@@ -3428,13 +3483,9 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) } } - error =init_swap_address_space (si->type, maxpages); -if (error) - goto bad_swap_unlock_inode; - error =zswap_swapon (si->type, maxpages);if (error) - goto free_swap_address_space; + goto bad_swap_unlock_inode;/* * Flush any pending IO and dirty mappings before we start using this @@ -3469,8 +3520,6 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags) goto out; free_swap_zswap:zswap_swapoff (si->type); -free_swap_address_space: -exit_swap_address_space (si->type); bad_swap_unlock_inode:inode_unlock (inode); bad_swap:@@ -3485,7 +3534,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)spin_unlock (&swap_lock);vfree (swap_map);kvfree (zeromap); -kvfree (cluster_info); +if (cluster_info) +free_cluster_info (cluster_info, maxpages);if (inced_nr_rotate_swap)atomic_dec (&nr_rotate_swap);if (swap_file)mm/vmscan.c +14
-7@@ -730,13 +730,18 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio, {int refcount;void *shadow =NULL ; +struct swap_cluster_info *ci;BUG_ON (!folio_test_locked (folio));BUG_ON (mapping !=folio_mapping (folio)); -if (!folio_test_swapcache (folio)) +if (folio_test_swapcache (folio)) { + ci =swap_cluster_get_and_lock_irq (folio); + }else {spin_lock (&mapping->host->i_lock); -xa_lock_irq (&mapping->i_pages); +xa_lock_irq (&mapping->i_pages); + } +/* * The non racy check for a busy folio. * @@ -776,9 +781,9 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,if (reclaimed && !mapping_exiting (mapping)) shadow =workingset_eviction (folio, target_memcg); -__swap_cache_del_folio (folio, swap, shadow); +__swap_cache_del_folio (ci, folio, swap, shadow);memcg1_swapout (folio, swap); -xa_unlock_irq (&mapping->i_pages); +swap_cluster_unlock_irq (ci);put_swap_folio (folio, swap); }else {void (*free_folio)(struct folio *);@@ -816,9 +821,12 @@ static int __remove_mapping(struct address_space *mapping, struct folio *folio,return 1 ; cannot_free: -xa_unlock_irq (&mapping->i_pages); -if (!folio_test_swapcache (folio)) +if (folio_test_swapcache (folio)) { +swap_cluster_unlock_irq (ci); + }else { +xa_unlock_irq (&mapping->i_pages);spin_unlock (&mapping->host->i_lock); + }return 0 ; } --2.51.0
Re: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
Quoted 31 lines · Lance Yang
Quoted 12 lines · Lance Yang
Hi Kairui,
I'm hitting a build error with allnoconfig:
In file included from
mm/shmem.c:44:mm/swap.h: In function ‘folio_index’:mm/swap.h:462:24: error: implicit declaration of function ‘swp_offset’; did you mean ‘pmd_offset’? [-Wimplicit-function-declaration]
462 | returnswp_offset(folio->swap);Quoted 2 lines
It looks like a header might be missing in
mm/swap.h. Please let me know if you need any more information.Confirmed that just adding #include <
linux/swapops.h> intomm/swap.hfixes it.mm/swap.h +1
-0@@ -3,6 +3,7 @@#define _MM_SWAP_H#include <linux/atomic.h> /* for atomic_long_t */ +#include <linux/swapops.h> struct mempolicy ;struct swap_iocb ;
Quoted 3 lines
Cheers,
Lance
Hi Lance,
You are testing V2 not V3 right? The build error is in V2 and I can confirm that. But In V3 I've added "ifdef CONFIG_SWAP" for the swp_offset usage in swap.h. I've just tested allnoconfig and it works fine on aarch64 and x86_64.
Lance Yang
reviewerRe: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
Quoted 38 lines · Kairui Song
Quoted 31 lines · Lance Yang
Quoted 12 lines · Lance Yang
Hi Kairui,
I'm hitting a build error with allnoconfig:
In file included from
mm/shmem.c:44:mm/swap.h: In function ‘folio_index’:mm/swap.h:462:24: error: implicit declaration of function ‘swp_offset’; did you mean ‘pmd_offset’? [-Wimplicit-function-declaration]
462 | returnswp_offset(folio->swap);Quoted 2 lines
It looks like a header might be missing in
mm/swap.h. Please let me know if you need any more information.Confirmed that just adding #include <
linux/swapops.h> intomm/swap.hfixes it.mm/swap.h +1
-0@@ -3,6 +3,7 @@#define _MM_SWAP_H#include <linux/atomic.h> /* for atomic_long_t */ +#include <linux/swapops.h> struct mempolicy ;struct swap_iocb ;Quoted 3 lines
Cheers,
LanceHi Lance,
Quoted 6 lines
You are testing V2 not V3 right? The build error is in V2 and I can confirm that. But In V3 I've added "ifdef
CONFIG_SWAP" for theswp_offsetusage inswap.h. I've just tested allnoconfig and it works fine on aarch64 andx86_64.
Ah, I was testing V2. My apologies for the noise!
Re: [PATCH v3 09/15] mm/shmem, swap: remove redundant error handling for replacing folio
In reply to Kairui SongQuoted 39 lines · Kairui Song
From: Kairui Song <[email protected]>
Shmem may replace a folio in the swap cache if the cached one doesn't fit the swapin's GFP zone. When doing so, shmem has already double checked that the swap cache folio is locked, still has the swap cache flag set, and contains the wanted swap entry. So it is impossible to fail due to an XArray mismatch. There is even a comment for that.
Delete the defensive error handling path, and add a
WARN_ONinstead:
if that happened, something has broken the basic principle of how the swap cache works, we should catch and fix that.Signed-off-by Kairui Song <[email protected]>Reviewed-by David Hildenbrand <[email protected]>--- mm/shmem.c | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-)mm/shmem.c +2
-2@@ -1661,13 +1661,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug, }/* -* The delete_from_swap_cache() below could be left for +* The swap_cache_del_folio() below could be left for * shrink_folio_list()'s folio_free_swap() to dispose of; * but I'm a little nervous about letting this folio out of * shmem_writeout() in a hybrid half-tmpfs-half-swap state * e.g. folio_mapping(folio) might give an unexpected answer. */ -delete_from_swap_cache (folio); +swap_cache_del_folio (folio); goto redirty; }if (nr_pages >1 )
mm/shmem.c static struct folio *shmem_swap_alloc_folio(struct inode *inode, · line 2048
Quoted 5 lines
mm/shmem.c +1
-1@@ -2045,7 +2045,7 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode, new->swap = entry;memcg1_swapin (entry, nr_pages); - shadow =get_shadow_from_swap_cache (entry); + shadow =swap_cache_get_shadow (entry);
Again, there are still some issues with the patch split. The swapcache related APIs replacement should be placed in Patch 8, otherwise there will be buidling errors after applying Patch 8.
With this issue fixed:
Quoted 64 lines
mm/shmem.c +9
-27if (shadow)workingset_refault (new, shadow);folio_add_lru (new);@@ -2121,35 +2121,17 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,/* Swap cache still stores N entries instead of a high-order entry */ xa_lock_irq (&swap_mapping->i_pages);for (i =0 ; i < nr_pages; i++) { -void *item =xas_load (&xas); - -if (item != old) { - error = -ENOENT ; -break ; - } - -xas_store (&xas, new); +WARN_ON_ONCE (xas_store (&xas, new));xas_next (&xas); } -if (!error) { -mem_cgroup_replace_folio (old, new); -shmem_update_stats (new, nr_pages); -shmem_update_stats (old, -nr_pages); - }xa_unlock_irq (&swap_mapping->i_pages); -if (unlikely (error)) { -/* -* Is this possible? I think not, now that our callers -* check both the swapcache flag and folio->private -* after getting the folio lock; but be defensive. -* Reverse old to newpage for clear and free. -*/ - old = new; - }else { -folio_add_lru (new); - *foliop = new; - } +mem_cgroup_replace_folio (old, new); +shmem_update_stats (new, nr_pages); +shmem_update_stats (old, -nr_pages); + +folio_add_lru (new); + *foliop = new;folio_clear_swapcache (old); old->private =NULL ;@@ -2183,7 +2165,7 @@ static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, nr_pages =folio_nr_pages (folio);folio_wait_writeback (folio);if (!skip_swapcache) -delete_from_swap_cache (folio); +swap_cache_del_folio (folio);/* * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks) @@ -2422,7 +2404,7 @@ static int shmem_swapin_folio(struct inode *inode, pgoff_t index, folio->swap.val =0 ;swapcache_clear (si, swap, nr_pages); }else { -delete_from_swap_cache (folio); +swap_cache_del_folio (folio); }folio_mark_dirty (folio);swap_free_nr (swap, nr_pages);
Re: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
In reply to Lance YangQuoted 11 lines · Lance Yang
Hi Kairui,
I'm hitting a build error with allnoconfig:
In file included from
mm/shmem.c:44:mm/swap.h: In function ‘folio_index’:mm/swap.h:462:24: error: implicit declaration of function ‘swp_offset’; did you mean ‘pmd_offset’? [-Wimplicit-function-declaration]
462 | returnswp_offset(folio->swap);
Quoted 2 lines
It looks like a header might be missing in
mm/swap.h. Please let me know if you need any more information.
Thanks, I just hit this too with a nommu build locally :)
Quoted 3 lines
Cheers,
Lance
Cheers, Lorenzo
Re: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
In reply to Lance YangQuoted 46 lines · Lance Yang
Quoted 43 lines · Kairui Song
Quoted 31 lines · Lance Yang
Quoted 12 lines · Lance Yang
Hi Kairui,
I'm hitting a build error with allnoconfig:
In file included from
mm/shmem.c:44:mm/swap.h: In function ‘folio_index’:mm/swap.h:462:24: error: implicit declaration of function ‘swp_offset’; did you mean ‘pmd_offset’? [-Wimplicit-function-declaration]
462 | returnswp_offset(folio->swap);Quoted 2 lines
It looks like a header might be missing in
mm/swap.h. Please let me know if you need any more information.Confirmed that just adding #include <
linux/swapops.h> intomm/swap.hfixes it.mm/swap.h +1
-0@@ -3,6 +3,7 @@#define _MM_SWAP_H#include <linux/atomic.h> /* for atomic_long_t */ +#include <linux/swapops.h> struct mempolicy ;struct swap_iocb ;Quoted 3 lines
Cheers,
LanceHi Lance,
You are testing V2 not V3 right? The build error is in V2 and I can confirm that. But In V3 I've added "ifdef
CONFIG_SWAP" for theswp_offsetusage inswap.h. I've just tested allnoconfig and it works fine on aarch64 andx86_64.
Quoted 5 lines
Quoted 1 line
Ah, I was testing V2. My apologies for the noise!
OK so my bug is different then :)
I think the issue is that you're not properly checking for:
#ifdef CONFIG_MMU
...
#endif
This is v3 (I see the #ifdef CONFIG_SWAP) btw.
So swapops.h is wrapped by #ifdef CONFIG_MMU which is why the declaration is missing.
The below fixpatch fixes things for me.
Cheers, Lorenzo
----8<----
From 5a3969a438af9c33422a45fe813a44068d784b2f Mon Sep 17 00:00:00 2001
From: Lorenzo Stoakes <[email protected]>
Date: Fri, 12 Sep 2025 10:28:40 +0100
Subject: [PATCH] fix
--- arch/sparc/include/asm/pgtable_64.h | 3 +++ mm/swap.h | 37 +++++++++++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-)
arch/sparc/include/asm/pgtable_64.h +3 -0
@@ -1050,6 +1050,9 @@ int page_in_phys_avail(unsigned long paddr);int remap_pfn_range (struct vm_area_struct *,unsigned long ,unsigned long ,unsigned long ,pgprot_t ); +void remap_pfn_range_prepare (struct vm_area_desc *desc,unsigned long pfn); +int remap_pfn_range_complete (struct vm_area_struct *vma,unsigned long addr, +unsigned long pfn,unsigned long size,pgprot_t pgprot);void adi_restore_tags (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long addr,pte_t pte);
mm/swap.h +22 -15
@@ -53,7 +53,7 @@ enum swap_cluster_flags { CLUSTER_FLAG_MAX, }; -#ifdef CONFIG_SWAP +#if defined(CONFIG_SWAP ) && defined(CONFIG_MMU )#include <linux/swapops.h> /* for swp_offset */ #include <linux/blk_types.h> /* for bio_end_io_t */
@@ -317,7 +317,26 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) return i; } +/** +* folio_index - File index of a folio. +* @folio: The folio. +* +* For a folio which is either in the page cache or the swap cache, +* return its index within the address_space it belongs to. If you know +* the folio is definitely in the page cache, you can look at the folio's +* index directly. +* +* Return: The index (offset in units of pages) of a folio in its file. +*/ +static inline pgoff_t folio_index (struct folio *folio) +{ +if (unlikely (folio_test_swapcache (folio))) +return swp_offset (folio->swap); +return folio->index; +} +#else /* CONFIG_SWAP */ +struct swap_iocb ;static inline struct swap_cluster_info *swap_cluster_lock (struct swap_info_struct *si,pgoff_t offset,bool irq)
@@ -442,24 +461,12 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) {return 0 ; } -#endif /* CONFIG_SWAP */ -/** -* folio_index - File index of a folio. -* @folio: The folio. -* -* For a folio which is either in the page cache or the swap cache, -* return its index within the address_space it belongs to. If you know -* the folio is definitely in the page cache, you can look at the folio's -* index directly. -* -* Return: The index (offset in units of pages) of a folio in its file. -*/ static inline pgoff_t folio_index (struct folio *folio) { -if (unlikely (folio_test_swapcache (folio))) -return swp_offset (folio->swap);return folio->index; } +#endif +#endif /* _MM_SWAP_H */
--
2.51.0
Re: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
Quoted 165 lines · Lorenzo Stoakes
Quoted 46 lines · Lance Yang
Quoted 43 lines · Kairui Song
Quoted 31 lines · Lance Yang
Quoted 12 lines · Lance Yang
Hi Kairui,
I'm hitting a build error with allnoconfig:
In file included from
mm/shmem.c:44:mm/swap.h: In function ‘folio_index’:mm/swap.h:462:24: error: implicit declaration of function ‘swp_offset’; did you mean ‘pmd_offset’? [-Wimplicit-function-declaration]
462 | returnswp_offset(folio->swap);Quoted 2 lines
It looks like a header might be missing in
mm/swap.h. Please let me know if you need any more information.Confirmed that just adding #include <
linux/swapops.h> intomm/swap.hfixes it.mm/swap.h +1
-0@@ -3,6 +3,7 @@#define _MM_SWAP_H#include <linux/atomic.h> /* for atomic_long_t */ +#include <linux/swapops.h> struct mempolicy ;struct swap_iocb ;Quoted 3 lines
Cheers,
LanceHi Lance,
You are testing V2 not V3 right? The build error is in V2 and I can confirm that. But In V3 I've added "ifdef
CONFIG_SWAP" for theswp_offsetusage inswap.h. I've just tested allnoconfig and it works fine on aarch64 andx86_64.Quoted 5 lines
Quoted 1 line
Ah, I was testing V2. My apologies for the noise!
OK so my bug is different then :)
I think the issue is that you're not properly checking for:
#ifdef
CONFIG_MMU
...
#endifThis is v3 (I see the #ifdef
CONFIG_SWAP) btw.So
swapops.his wrapped by #ifdefCONFIG_MMUwhich is why the declaration is missing.The below fixpatch fixes things for me.
Cheers, Lorenzo
----8<----
From5a3969a438af9c33422a45fe813a44068d784b2fMon Sep 17 00:00:00 2001
From: Lorenzo Stoakes <[email protected]>
Date: Fri, 12 Sep 2025 10:28:40 +0100
Subject: [PATCH] fixSigned-off-by Lorenzo Stoakes <[email protected]>--- arch/sparc/include/asm/pgtable_64.h | 3 +++ mm/swap.h | 37 +++++++++++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-)arch/sparc/include/asm/pgtable_64.h +3
-0@@ -1050,6 +1050,9 @@ int page_in_phys_avail(unsigned long paddr);int remap_pfn_range (struct vm_area_struct *,unsigned long ,unsigned long ,unsigned long ,pgprot_t ); +void remap_pfn_range_prepare (struct vm_area_desc *desc,unsigned long pfn); +int remap_pfn_range_complete (struct vm_area_struct *vma,unsigned long addr, +unsigned long pfn,unsigned long size,pgprot_t pgprot);void adi_restore_tags (struct mm_struct *mm,struct vm_area_struct *vma,unsigned long addr,pte_t pte);mm/swap.h +22
-16@@ -53,7 +53,7 @@ enum swap_cluster_flags { CLUSTER_FLAG_MAX, }; -#ifdef CONFIG_SWAP +#if defined(CONFIG_SWAP ) && defined(CONFIG_MMU )#include <linux/swapops.h> /* for swp_offset */ #include <linux/blk_types.h> /* for bio_end_io_t */ @@ -317,7 +317,26 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) return i; } +/** +* folio_index - File index of a folio. +* @folio: The folio. +* +* For a folio which is either in the page cache or the swap cache, +* return its index within the address_space it belongs to. If you know +* the folio is definitely in the page cache, you can look at the folio's +* index directly. +* +* Return: The index (offset in units of pages) of a folio in its file. +*/ +static inline pgoff_t folio_index (struct folio *folio) +{ +if (unlikely (folio_test_swapcache (folio))) +return swp_offset (folio->swap); +return folio->index; +} +#else /* CONFIG_SWAP */ +struct swap_iocb ;static inline struct swap_cluster_info *swap_cluster_lock (struct swap_info_struct *si,pgoff_t offset,bool irq)@@ -442,24 +461,12 @@ static inline int non_swapcache_batch(swp_entry_t entry, int max_nr) {return 0 ; } -#endif /* CONFIG_SWAP */ -/** -* folio_index - File index of a folio. -* @folio: The folio. -* -* For a folio which is either in the page cache or the swap cache, -* return its index within the address_space it belongs to. If you know -* the folio is definitely in the page cache, you can look at the folio's -* index directly. -* -* Return: The index (offset in units of pages) of a folio in its file. -*/ static inline pgoff_t folio_index (struct folio *folio) { -if (unlikely (folio_test_swapcache (folio))) -return swp_offset (folio->swap);return folio->index; } +#endif +#endif /* _MM_SWAP_H */ --
Quoted 1 line
2.51.0
Thanks for the info. But I think V3 should be good?
CONFIG_SWAP depends on CONFIG_MMU so if you have !CONFIG_MMU, you must have !CONFIG_SWAP.
I didn't adjust the header in V3 and fixed it differently from what Lance is suggesting, V3 wraps swp_offset with CONFIG_SWAP. It should covers !CONFIG_MMU case too.
The folio_index in V3 (this patch) looks like this:
static inline pgoff_t folio_index(struct folio *folio) {
#ifdef CONFIG_SWAP
if (unlikely(folio_test_swapcache(folio)))
return swp_offset(folio->swap);
#endif
return folio->index;
}I just tested !MMU and it seems fine.
Re: [PATCH v3 11/15] mm, swap: use the swap table for the swap cache and switch API
Quoted 13 lines · Kairui Song
Thanks for the info. But I think V3 should be good?
CONFIG_SWAPdepends onCONFIG_MMUso if you have !CONFIG_MMU, you must have !CONFIG_SWAP.I didn't adjust the header in V3 and fixed it differently from what Lance is suggesting, V3 wraps
swp_offsetwithCONFIG_SWAP. It should covers !CONFIG_MMU case too.The
folio_indexin V3 (this patch) looks like this:static inline
pgoff_tfolio_index(struct folio *folio){
Quoted 8 lines
#ifdef CONFIG_SWAP if (unlikely(folio_test_swapcache(folio))) return swp_offset(folio->swap); #endif return folio->index; }I just tested !MMU and it seems fine.
Yup my bad, it's because mm-new is not actually up-to-date properly. So mm-new build is broken until v3 is taken.
Re: [PATCH v3 09/15] mm/shmem, swap: remove redundant error handling for replacing folio
In reply to Baolin WangQuoted 50 lines · Baolin Wang
Quoted 39 lines · Kairui Song
From: Kairui Song <[email protected]>
Shmem may replace a folio in the swap cache if the cached one doesn't fit the swapin's GFP zone. When doing so, shmem has already double checked that the swap cache folio is locked, still has the swap cache flag set, and contains the wanted swap entry. So it is impossible to fail due to an XArray mismatch. There is even a comment for that.
Delete the defensive error handling path, and add a
WARN_ONinstead:
if that happened, something has broken the basic principle of how the swap cache works, we should catch and fix that.Signed-off-by Kairui Song <[email protected]>Reviewed-by David Hildenbrand <[email protected]>--- mm/shmem.c | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-)mm/shmem.c +2
-2@@ -1661,13 +1661,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug, }/* -* The delete_from_swap_cache() below could be left for +* The swap_cache_del_folio() below could be left for * shrink_folio_list()'s folio_free_swap() to dispose of; * but I'm a little nervous about letting this folio out of * shmem_writeout() in a hybrid half-tmpfs-half-swap state * e.g. folio_mapping(folio) might give an unexpected answer. */ -delete_from_swap_cache (folio); +swap_cache_del_folio (folio); goto redirty; }if (nr_pages >1 )mm/shmem.c static struct folio *shmem_swap_alloc_folio(struct inode *inode, · line 2048
Quoted 5 lines
mm/shmem.c +1
-1@@ -2045,7 +2045,7 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode, new->swap = entry;memcg1_swapin (entry, nr_pages); - shadow =get_shadow_from_swap_cache (entry); + shadow =swap_cache_get_shadow (entry);Again, there are still some issues with the patch split. The swapcache related APIs replacement should be placed in Patch 8, otherwise there will be buidling errors after applying Patch 8.
Quoted 3 lines
With this issue fixed:
Reviewed-by Baolin Wang <[email protected]>
Hi Baolin
Yeah you are right, I need to move these few changes to patch 8.
BTW I also found that the WARN_ON and irq unlock needs following fix, the stats update need to be done with irq disabled:
mm/shmem.c +0 -0
@@ -2121,14 +2121,14 @@ static int shmem_replace_folio(struct folio
**foliop, gfp_t gfp,
/* Swap cache still stores N entries instead of a high-order entry */
xa_lock_irq(&swap_mapping->i_pages);
for (i = 0; i < nr_pages; i++) {
- WARN_ON_ONCE(xas_store(&xas, new));
+ WARN_ON_ONCE(xas_store(&xas, new) != old);
xas_next(&xas);
}- xa_unlock_irq(&swap_mapping->i_pages);
mem_cgroup_replace_folio(old, new);
shmem_update_stats(new, nr_pages);
shmem_update_stats(old, -nr_pages);+ xa_unlock_irq(&swap_mapping->i_pages);
folio_add_lru(new);
*foliop = new;Re: [PATCH v3 04/15] mm, swap: check page poison flag after locking it
In reply to Kairui SongQuoted 17 lines · Kairui Song
From: Kairui Song <[email protected]>
Instead of checking the poison flag only in the fast swap cache lookup path, always check the poison flags after locking a swap cache folio.
There are two reasons to do so.
The folio is unstable and could be removed from the swap cache anytime, so it's totally possible that the folio is no longer the backing folio of a swap entry, and could be an irrelevant poisoned folio. We might mistakenly kill a faulting process.
And it's totally possible or even common for the slow swap in path (
swapin_readahead) to bring in a cached folio. The cache folio could be poisoned, too. Only checking the poison flag in the fast path will miss such folios.
Quoted 6 lines
The race window is tiny, so it's very unlikely to happen, though. While at it, also add a unlikely prefix.
Signed-off-by Kairui Song <[email protected]>Acked-by Chris Li <[email protected]>---
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v3 05/15] mm, swap: always lock and check the swap cache folio before use
In reply to Kairui SongQuoted 19 lines · Kairui Song
From: Kairui Song <[email protected]>
Swap cache lookup only increases the reference count of the returned folio. That's not enough to ensure a folio is stable in the swap cache, so the folio could be removed from the swap cache at any time. The caller should always lock and check the folio before using it.
We have just documented this in kerneldoc, now introduce a helper for swap cache folio verification with proper sanity checks.
Also, sanitize a few current users to use this convention and the new helper for easier debugging. They were not having observable problems yet, only trivial issues like wasted CPU cycles on swapoff or reclaiming. They would fail in some other way, but it is still better to always follow this convention to make things robust and make later commits easier to do.
Signed-off-by Kairui Song <[email protected]>---
[...]
Quoted 6 lines
index
4baebd8b48f4..f1a4d381d719100644mm/swapfile.c +0
-0@@ -240,14 +240,12 @@ static int __try_to_reclaim_swap(struct swap_info_struct *si,* Offset could point to the middle of a large folio, or folio * may no longer point to the expected offset before it's locked.
mm/swapfile.c static int __try_to_reclaim_swap(struct swap_info_struct *si, · line 250
Quoted 10 lines
mm/swapfile.c +1
-3*/ -if (offset <swp_offset (folio->swap) || - offset >=swp_offset (folio->swap) + nr_pages) { +if (!folio_matches_swap_entry (folio, entry)) {folio_unlock (folio);folio_put (folio); goto again; } offset =swp_offset (folio->swap); -
Nit: unrelated change.
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v3 10/15] mm, swap: wrap swap cache replacement with a helper
In reply to Kairui SongQuoted 19 lines · Kairui Song
From: Kairui Song <[email protected]>
There are currently three swap cache users that are trying to replace an existing folio with a new one: huge memory splitting, migration, and shmem replacement. What they are doing is quite similar.
Introduce a common helper for this. In later commits, this can be easily switched to use the swap table by updating this helper.
The newly added helper also makes the swap cache API better defined, and make debugging easier by adding a few more debug checks.
Migration and shmem replace are meant to clone the folio, including content, swap entry value, and flags. And splitting will adjust each sub folio's swap entry according to order, which could be non-uniform in the future. So document it clearly that it's the caller's responsibility to set up the new folio's swap entries and flags before calling the helper. The helper will just follow the new folio's entry value.
Quoted 7 lines
This also prepares for replacing high-order folios in the swap cache. Currently, only splitting to order 0 is allowed for swap cache folios. Using the new helper, we can handle high-order folio splitting better.
Signed-off-by Kairui Song <[email protected]>Reviewed-by Baolin Wang <[email protected]>---
Signature · 3 lines
Cheers David / dhildenb
Re: [PATCH v3 04/15] mm, swap: check page poison flag after locking it
In reply to Kairui SongQuoted 18 lines · Kairui Song
From: Kairui Song <[email protected]>
Instead of checking the poison flag only in the fast swap cache lookup path, always check the poison flags after locking a swap cache folio.
There are two reasons to do so.
The folio is unstable and could be removed from the swap cache anytime, so it's totally possible that the folio is no longer the backing folio of a swap entry, and could be an irrelevant poisoned folio. We might mistakenly kill a faulting process.
And it's totally possible or even common for the slow swap in path (
swapin_readahead) to bring in a cached folio. The cache folio could be poisoned, too. Only checking the poison flag in the fast path will miss such folios.
Quoted 5 lines
The race window is tiny, so it's very unlikely to happen, though. While at it, also add a unlikely prefix.
Signed-off-by Kairui Song <[email protected]>Acked-by Chris Li <[email protected]>
Baolin Wang
reviewerRe: [PATCH v3 09/15] mm/shmem, swap: remove redundant error handling for replacing folio
In reply to Kairui SongQuoted 56 lines · Kairui Song
Quoted 50 lines · Baolin Wang
Quoted 39 lines · Kairui Song
From: Kairui Song <[email protected]>
Shmem may replace a folio in the swap cache if the cached one doesn't fit the swapin's GFP zone. When doing so, shmem has already double checked that the swap cache folio is locked, still has the swap cache flag set, and contains the wanted swap entry. So it is impossible to fail due to an XArray mismatch. There is even a comment for that.
Delete the defensive error handling path, and add a
WARN_ONinstead:
if that happened, something has broken the basic principle of how the swap cache works, we should catch and fix that.Signed-off-by Kairui Song <[email protected]>Reviewed-by David Hildenbrand <[email protected]>--- mm/shmem.c | 42 ++++++++++++------------------------------ 1 file changed, 12 insertions(+), 30 deletions(-)mm/shmem.c +2
-2@@ -1661,13 +1661,13 @@ int shmem_writeout(struct folio *folio, struct swap_iocb **plug, }/* -* The delete_from_swap_cache() below could be left for +* The swap_cache_del_folio() below could be left for * shrink_folio_list()'s folio_free_swap() to dispose of; * but I'm a little nervous about letting this folio out of * shmem_writeout() in a hybrid half-tmpfs-half-swap state * e.g. folio_mapping(folio) might give an unexpected answer. */ -delete_from_swap_cache (folio); +swap_cache_del_folio (folio); goto redirty; }if (nr_pages >1 )mm/shmem.c static struct folio *shmem_swap_alloc_folio(struct inode *inode, · line 2048
Quoted 5 lines
mm/shmem.c +1
-1@@ -2045,7 +2045,7 @@ static struct folio *shmem_swap_alloc_folio(struct inode *inode, new->swap = entry;memcg1_swapin (entry, nr_pages); - shadow =get_shadow_from_swap_cache (entry); + shadow =swap_cache_get_shadow (entry);Again, there are still some issues with the patch split. The swapcache related APIs replacement should be placed in Patch 8, otherwise there will be buidling errors after applying Patch 8.
Quoted 3 lines
With this issue fixed:
Reviewed-by Baolin Wang <[email protected]>
Quoted 6 lines
Hi Baolin
Yeah you are right, I need to move these few changes to patch 8.
BTW I also found that the
WARN_ONand irq unlock needs following fix, the stats update need to be done with irq disabled:
Yes, indeed, I overlooked this.
Quoted 22 lines
mm/shmem.c +2
-2@@ -2121,14 +2121,14 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,/* Swap cache still stores N entries instead of a high-order entry */ xa_lock_irq (&swap_mapping->i_pages);for (i =0 ; i < nr_pages; i++) { -WARN_ON_ONCE (xas_store (&xas, new)); +WARN_ON_ONCE (xas_store (&xas, new) != old);xas_next (&xas); } -xa_unlock_irq (&swap_mapping->i_pages);mem_cgroup_replace_folio (old, new);shmem_update_stats (new, nr_pages);shmem_update_stats (old, -nr_pages); +xa_unlock_irq (&swap_mapping->i_pages);folio_add_lru (new);*foliop = new;
Re: [PATCH v3 05/15] mm, swap: always lock and check the swap cache folio before use
In reply to Kairui Songmm/swapfile.c static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · line 2008
Quoted 8 lines · Kairui Song
mm/swapfile.c +4
-0@@ -2004,6 +2002,13 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,bool hwpoisoned = false;int ret =1 ; +/* +* If the folio is removed from swap cache by others, continue to +* unuse other PTEs. try_to_unuse may try again if we missed this one. +*/
It took me a while to figure out why we add a folio_matches_swap_entry() check here but we don't have an existing check for folio swap entry matching in this function. Can you confirm that if a race has happened causing the folio swap entry mismatch, then try_to_usue() MUST try again rather than "may" try again. It seems to me that it is a MUST rather than "may". I am curious in what condition the mismatch happens and the try_to_unuse() does not need to try again.
BTW, this function has three types of return value, 1, 0, and negative for error. The 0 and 1 are ignored by the caller, the caller only cares about the negative value. Overall this unuse_pte() and try_to_unuse() walk feels complicated and maybe a candidate for later clean up. That is not your patch's fault. I am not requesting a cleanup in this series.
With that Nitpick,
Chris
Quoted 9 lines
mm/swapfile.c +3
-1+if (!folio_matches_swap_entry (folio, entry)) +return 0 ; + swapcache = folio; folio =ksm_might_need_to_copy (folio, vma, addr);if (unlikely (!folio)) --2.51.0
Re: [PATCH v3 10/15] mm, swap: wrap swap cache replacement with a helper
In reply to Kairui SongChris
Quoted 186 lines · Kairui Song
From: Kairui Song <[email protected]>
There are currently three swap cache users that are trying to replace an existing folio with a new one: huge memory splitting, migration, and shmem replacement. What they are doing is quite similar.
Introduce a common helper for this. In later commits, this can be easily switched to use the swap table by updating this helper.
The newly added helper also makes the swap cache API better defined, and make debugging easier by adding a few more debug checks.
Migration and shmem replace are meant to clone the folio, including content, swap entry value, and flags. And splitting will adjust each sub folio's swap entry according to order, which could be non-uniform in the future. So document it clearly that it's the caller's responsibility to set up the new folio's swap entries and flags before calling the helper. The helper will just follow the new folio's entry value.
This also prepares for replacing high-order folios in the swap cache. Currently, only splitting to order 0 is allowed for swap cache folios. Using the new helper, we can handle high-order folio splitting better.
Signed-off-by Kairui Song <[email protected]>Reviewed-by Baolin Wang <[email protected]>--- mm/huge_memory.c | 4 +--- mm/migrate.c | 11 +++-------- mm/shmem.c | 10 ++-------- mm/swap.h | 5 +++++ mm/swap_state.c | 33 +++++++++++++++++++++++++++++++++ 5 files changed, 44 insertions(+), 19 deletions(-)mm/huge_memory.c +1
-3@@ -3798,9 +3798,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,* NOTE: shmem in swap cache is not supported yet. */ if (swap_cache) { -__xa_store (&swap_cache->i_pages, -swap_cache_index (new_folio->swap), - new_folio,0 ); +__swap_cache_replace_folio (folio, new_folio);continue ; }mm/migrate.c +3
-8@@ -566,7 +566,6 @@ static int __folio_migrate_mapping(struct address_space *mapping,struct zone *oldzone, *newzone;int dirty;long nr =folio_nr_pages (folio); -long entries, i;if (!mapping) {/* Take off deferred split queue while frozen and memcg set */ @@ -615,9 +614,6 @@ static int __folio_migrate_mapping(struct address_space *mapping,if (folio_test_swapcache (folio)) {folio_set_swapcache (newfolio); newfolio->private =folio_get_private (folio); - entries = nr; - }else { - entries =1 ; }/* Move dirty while folio refs frozen and newfolio not yet exposed */ @@ -627,11 +623,10 @@ static int __folio_migrate_mapping(struct address_space *mapping,folio_set_dirty (newfolio); } -/* Swap cache still stores N entries instead of a high-order entry */ -for (i =0 ; i < entries; i++) { +if (folio_test_swapcache (folio)) +__swap_cache_replace_folio (folio, newfolio); +else xas_store (&xas, newfolio); -xas_next (&xas); - }/* * Drop cache reference from old folio by unfreezing mm/shmem.c +2
-8@@ -2086,10 +2086,8 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp,struct folio *new, *old = *foliop;swp_entry_t entry = old->swap;struct address_space *swap_mapping =swap_address_space (entry); -pgoff_t swap_index =swap_cache_index (entry); -XA_STATE (xas, &swap_mapping->i_pages, swap_index);int nr_pages =folio_nr_pages (old); -int error =0 , i; +int error =0 ;/* * We have arrived here because our zones are constrained, so don't @@ -2118,12 +2116,8 @@ static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, new->swap = entry;folio_set_swapcache (new); -/* Swap cache still stores N entries instead of a high-order entry */ xa_lock_irq (&swap_mapping->i_pages); -for (i =0 ; i < nr_pages; i++) { -WARN_ON_ONCE (xas_store (&xas, new)); -xas_next (&xas); - } +__swap_cache_replace_folio (old, new);xa_unlock_irq (&swap_mapping->i_pages);mem_cgroup_replace_folio (old, new);mm/swap.h +5
-0@@ -185,6 +185,7 @@ int swap_cache_add_folio(struct folio *folio, swp_entry_t entry,void swap_cache_del_folio (struct folio *folio);void __swap_cache_del_folio (struct folio *folio,swp_entry_t entry,void *shadow); +void __swap_cache_replace_folio (struct folio *old,struct folio *new);void swap_cache_clear_shadow (int type,unsigned long begin,unsigned long end);@@ -336,6 +337,10 @@ static inline void __swap_cache_del_folio(struct folio *folio, swp_entry_t entry { } +static inline void __swap_cache_replace_folio (struct folio *old,struct folio *new) +{ +} +static inline unsigned int folio_swap_flags (struct folio *folio) {return 0 ;mm/swap_state.c +33
-1@@ -234,6 +234,39 @@ void swap_cache_del_folio(struct folio *folio)folio_ref_sub (folio,folio_nr_pages (folio)); } +/** +* __swap_cache_replace_folio - Replace a folio in the swap cache. +* @old: The old folio to be replaced. +* @new: The new folio. +* +* Replace an existing folio in the swap cache with a new folio. The +* caller is responsible for setting up the new folio's flag and swap +* entries. Replacement will take the new folio's swap entry value as +* the starting offset to override all slots covered by the new folio. +* +* Context: Caller must ensure both folios are locked, also lock the +* swap address_space that holds the old folio to avoid races. +*/ +void __swap_cache_replace_folio (struct folio *old,struct folio *new) +{ +swp_entry_t entry = new->swap; +unsigned long nr_pages =folio_nr_pages (new); +unsigned long offset =swap_cache_index (entry); +unsigned long end = offset + nr_pages; + +XA_STATE (xas, &swap_address_space (entry)->i_pages, offset); + +VM_WARN_ON_ONCE (!folio_test_swapcache (old) || !folio_test_swapcache (new)); +VM_WARN_ON_ONCE (!folio_test_locked (old) || !folio_test_locked (new)); +VM_WARN_ON_ONCE (!entry.val); + +/* Swap cache still stores N entries instead of a high-order entry */ +do { +WARN_ON_ONCE (xas_store (&xas, new) != old); +xas_next (&xas); + }while (++offset < end); +} +/** * swap_cache_clear_shadow - Clears a set of shadows in the swap cache. * @type: Indicates the swap device. -- 2.51.0
Chris Li
maintainerRe: [PATCH v3 09/15] mm/shmem, swap: remove redundant error handling for replacing folio
In reply to Kairui SongQuoted 9 lines · Kairui Song
Quoted 8 lines
Again, there are still some issues with the patch split. The swapcache related APIs replacement should be placed in Patch 8, otherwise there will be buidling errors after applying Patch 8.
With this issue fixed:
Reviewed-by Baolin Wang <[email protected]>
Quoted 6 lines
Hi Baolin
Yeah you are right, I need to move these few changes to patch 8.
BTW I also found that the
WARN_ONand irq unlock needs following fix, the stats update need to be done with irq disabled:
I will skip this patch and wait for your new fixed patch then.
Chris
Re: [PATCH v3 14/15] mm, swap: implement dynamic allocation of swap table
In reply to Kairui SongQuoted 30 lines · On Thu, 11 Sep 2025 00:08:32 +0800 Kairui Song <[email protected]> wrote:
From: Kairui Song <[email protected]>
Now swap table is cluster based, which means free clusters can free its table since no one should modify it.
There could be speculative readers, like swap cache look up, protect them by making them RCU protected. All swap table should be filled with null entries before free, so such readers will either see a NULL pointer or a null filled table being lazy freed.
On allocation, allocate the table when a cluster is used by any order.
This way, we can reduce the memory usage of large swap device significantly.
This idea to dynamically release unused swap cluster data was initially suggested by Chris Li while proposing the cluster swap allocator and it suits the swap table idea very well.
Co-developed-by Chris Li <[email protected]>Signed-off-by Chris Li <[email protected]>Signed-off-by Kairui Song <[email protected]>Acked-by Chris Li <[email protected]>--- mm/swap.h | 2 +- mm/swap_state.c | 9 +-- mm/swap_table.h | 37 ++++++++- mm/swapfile.c | 202 ++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 199 insertions(+), 51 deletions(-)
[ ... ]
Quoted 5 lines
mm/swapfile.c +0
-0
[ ... ]
Quoted 55 lines
mm/swapfile.c +43
-9+/* +* Allocate a swap table may need to sleep, which leads to migration, +* so attempt an atomic allocation first then fallback and handle +* potential race. +*/ +static struct swap_cluster_info * +swap_cluster_alloc_table (struct swap_info_struct *si, +struct swap_cluster_info *ci, +int order) { -unsigned int ci_off; -unsigned long swp_tb; +struct swap_cluster_info *pcp_ci; +struct swap_table *table; +unsigned long offset; -if (!ci->table) -return ; +/* +* Only cluster isolation from the allocator does table allocation. +* Swap allocator uses a percpu cluster and holds the local lock. +*/ +lockdep_assert_held (&ci->lock); +lockdep_assert_held (&this_cpu_ptr (&percpu_swap_cluster)->lock); + + table =kmem_cache_zalloc (swap_table_cachep, + __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); +if (table) { +rcu_assign_pointer (ci->table, table); +return ci; + } + +/* +* Try a sleep allocation. Each isolated free cluster may cause +* a sleep allocation, but there is a limited number of them, so +* the potential recursive allocation should be limited. +*/ +spin_unlock (&ci->lock); +if (!(si->flags &SWP_SOLIDSTATE )) +spin_unlock (&si->global_cluster_lock); +local_unlock (&percpu_swap_cluster.lock); + table =kmem_cache_zalloc (swap_table_cachep, __GFP_HIGH |GFP_KERNEL ); -for (ci_off =0 ; ci_off <SWAPFILE_CLUSTER ; ci_off++) { - swp_tb =__swap_table_get (ci, ci_off); -if (!swp_tb_is_null (swp_tb)) -pr_err_once ("swap: unclean swap space on swapoff: 0x%lx" , - swp_tb); +local_lock (&percpu_swap_cluster.lock); +if (!(si->flags &SWP_SOLIDSTATE )) +spin_lock (&si->global_cluster_lock); +/* +* Back to atomic context. First, check if we migrated to a new +* CPU with a usable percpu cluster. If so, try using that instead. +* No need to check it for the spinning device, as swap is
mm/swapfile.c static inline unsigned int cluster_offset(struct swap_info_struct *si, · line 504
Quoted 10 lines
mm/swapfile.c +10
-0+* serialized by the global lock on them. +* +* The is_usable check is a bit rough, but ensures order 0 success. +*/ + offset =this_cpu_read (percpu_swap_cluster.offset[order]); +if ((si->flags &SWP_SOLIDSTATE ) && offset) { + pcp_ci =swap_cluster_lock (si, offset); +if (cluster_is_usable (pcp_ci, order) && + pcp_ci->count <SWAPFILE_CLUSTER ) { + ci = pcp_ci;
^^^^^^^^^^^^^
ci came from the caller, and in the case of isolate_lock_cluster() they had just removed it from a list. We overwrite ci and return something different.
Quoted 25 lines
mm/swapfile.c +18
-2+ goto free_table; + } +swap_cluster_unlock (pcp_ci); } -kfree (ci->table); - ci->table =NULL ; +if (!table) +return NULL ; + +spin_lock (&ci->lock); +/* Nothing should have touched the dangling empty cluster. */ +if (WARN_ON_ONCE (cluster_table_is_alloced (ci))) + goto free_table; + +rcu_assign_pointer (ci->table, table); +return ci; + +free_table: +if (table) +kmem_cache_free (swap_table_cachep, table); +return ci; } static void move_cluster(struct swap_info_struct *si,
[ ... ]
mm/swapfile.c static struct swap_cluster_info *isolate_lock_cluster( · line 587
Quoted 6 lines
mm/swapfile.c +1
-1@@ -513,13 +584,19 @@ static struct swap_cluster_info *isolate_lock_cluster(list_del (&ci->list); ci->flags =CLUSTER_FLAG_NONE ; - ret = ci; + found = ci;
We've pulled ci off the list here.
mm/swapfile.c static struct swap_cluster_info *isolate_lock_cluster( · line 596
Quoted 11 lines
mm/swapfile.c +5
-2break ; } -out:spin_unlock (&si->lock); -return ret; +if (found && !cluster_table_is_alloced (found)) { +/* Only an empty free cluster's swap table can be freed. */ +VM_WARN_ON_ONCE (list != &si->free_clusters); +VM_WARN_ON_ONCE (!cluster_is_empty (found)); +return swap_cluster_alloc_table (si, found, order);
swap_cluster_alloc_table() may have switched for a different ci? What happens to the one we pulled off the list?
-chris
Re: [PATCH v3 14/15] mm, swap: implement dynamic allocation of swap table
Quoted 110 lines · Chris Mason
Quoted 30 lines · On Thu, 11 Sep 2025 00:08:32 +0800 Kairui Song <[email protected]> wrote:
From: Kairui Song <[email protected]>
Now swap table is cluster based, which means free clusters can free its table since no one should modify it.
There could be speculative readers, like swap cache look up, protect them by making them RCU protected. All swap table should be filled with null entries before free, so such readers will either see a NULL pointer or a null filled table being lazy freed.
On allocation, allocate the table when a cluster is used by any order.
This way, we can reduce the memory usage of large swap device significantly.
This idea to dynamically release unused swap cluster data was initially suggested by Chris Li while proposing the cluster swap allocator and it suits the swap table idea very well.
Co-developed-by Chris Li <[email protected]>Signed-off-by Chris Li <[email protected]>Signed-off-by Kairui Song <[email protected]>Acked-by Chris Li <[email protected]>--- mm/swap.h | 2 +- mm/swap_state.c | 9 +-- mm/swap_table.h | 37 ++++++++- mm/swapfile.c | 202 ++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 199 insertions(+), 51 deletions(-)[ ... ]
Quoted 5 lines
mm/swapfile.c +0
-0[ ... ]
Quoted 55 lines
mm/swapfile.c +43
-9+/* +* Allocate a swap table may need to sleep, which leads to migration, +* so attempt an atomic allocation first then fallback and handle +* potential race. +*/ +static struct swap_cluster_info * +swap_cluster_alloc_table (struct swap_info_struct *si, +struct swap_cluster_info *ci, +int order) { -unsigned int ci_off; -unsigned long swp_tb; +struct swap_cluster_info *pcp_ci; +struct swap_table *table; +unsigned long offset; -if (!ci->table) -return ; +/* +* Only cluster isolation from the allocator does table allocation. +* Swap allocator uses a percpu cluster and holds the local lock. +*/ +lockdep_assert_held (&ci->lock); +lockdep_assert_held (&this_cpu_ptr (&percpu_swap_cluster)->lock); + + table =kmem_cache_zalloc (swap_table_cachep, + __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN); +if (table) { +rcu_assign_pointer (ci->table, table); +return ci; + } + +/* +* Try a sleep allocation. Each isolated free cluster may cause +* a sleep allocation, but there is a limited number of them, so +* the potential recursive allocation should be limited. +*/ +spin_unlock (&ci->lock); +if (!(si->flags &SWP_SOLIDSTATE )) +spin_unlock (&si->global_cluster_lock); +local_unlock (&percpu_swap_cluster.lock); + table =kmem_cache_zalloc (swap_table_cachep, __GFP_HIGH |GFP_KERNEL ); -for (ci_off =0 ; ci_off <SWAPFILE_CLUSTER ; ci_off++) { - swp_tb =__swap_table_get (ci, ci_off); -if (!swp_tb_is_null (swp_tb)) -pr_err_once ("swap: unclean swap space on swapoff: 0x%lx" , - swp_tb); +local_lock (&percpu_swap_cluster.lock); +if (!(si->flags &SWP_SOLIDSTATE )) +spin_lock (&si->global_cluster_lock); +/* +* Back to atomic context. First, check if we migrated to a new +* CPU with a usable percpu cluster. If so, try using that instead. +* No need to check it for the spinning device, as swap is mm/swapfile.c static inline unsigned int cluster_offset(struct swap_info_struct *si, · line 504
Quoted 10 lines
mm/swapfile.c +10
-0+* serialized by the global lock on them. +* +* The is_usable check is a bit rough, but ensures order 0 success. +*/ + offset =this_cpu_read (percpu_swap_cluster.offset[order]); +if ((si->flags &SWP_SOLIDSTATE ) && offset) { + pcp_ci =swap_cluster_lock (si, offset); +if (cluster_is_usable (pcp_ci, order) && + pcp_ci->count <SWAPFILE_CLUSTER ) { + ci = pcp_ci;^^^^^^^^^^^^^
Quoted 3 lines
ci came from the caller, and in the case of
isolate_lock_cluster()they had just removed it from a list. We overwrite ci and return something different.
Yes, that's expected. See the comment above. We have just dropped local lock so it's possible that we migrated to another CPU which has its own percpu cache ci (percpu_swap_cluster.offset).
To avoid fragmentation, drop the isolated ci and use the percpu ci instead. But you are right that I need to add the ci back to the list, or it will be leaked. Thanks!
Re: [PATCH v3 05/15] mm, swap: always lock and check the swap cache folio before use
In reply to Chris LiQuoted 6 lines · Chris Li
Quoted 4 lines · Kairui Song
mm/swapfile.c +0
-0@@ -2004,6 +2002,13 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,bool hwpoisoned = false;int ret =1 ;
Quoted 13 lines
mm/swapfile.c static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd, · line 2008
Quoted 4 lines
mm/swapfile.c +4
-0+/* +* If the folio is removed from swap cache by others, continue to +* unuse other PTEs. try_to_unuse may try again if we missed this one. +*/ It took me a while to figure out why we add a
folio_matches_swap_entry()check here but we don't have an existing check for folio swap entry matching in this function. Can you confirm that if a race has happened causing the folio swap entry mismatch, thentry_to_usue()MUST try again rather than "may" try again. It seems to me that it is a MUST rather than "may". I am curious in what condition the mismatch happens and thetry_to_unuse()does not need to try again.
It depends, I think it may, because: for example here we are unusing folio A with entry S1, and a raced process just swapin folio A then remove it from swap cache. If the raced process didn't swapout the PTE again, then there is no need to retry as we are dong with this PTE.
There are many races, someone else swapped out folio A again using S2. Then here we will see a !folio_matches_swap_entry. And that's OK.
But there have been many other subtle race conditions in other places, e.g. another folio occupied the same PTE and got swapped out using S1, causing PTE == S1 and here got a wrong folio A installed. This isn't happening here, because we have removed the !SWP_WRITEOK flag from the si during swapoff...
It's really complex and fragile, so just make it easier, check folio_matches_swap_entry and abort early, is safer and follows the convention.
Quoted 6 lines
BTW, this function has three types of return value, 1, 0, and negative for error. The 0 and 1 are ignored by the caller, the caller only cares about the negative value. Overall this
unuse_pte()andtry_to_unuse()walk feels complicated and maybe a candidate for later clean up. That is not your patch's fault. I am not requesting a cleanup in this series.
Right, totally agree, we can cleanup the swapoff part later.
Quoted 3 lines
With that Nitpick,
Acked-by Chris Li <[email protected]>
Thanks!
Re: [PATCH v3 14/15] mm, swap: implement dynamic allocation of swap table
In reply to Kairui SongQuoted 3 lines · Kairui Song
Quoted 2 lines · Chris Mason
On Thu, 11 Sep 2025 00:08:32 +0800 Kairui Song <[email protected]> wrote:
[ ... ]
spin_lock(&si->global_cluster_lock);
Quoted 19 lines
Quoted 14 lines
Quoted 4 lines
mm/swapfile.c +4
-0+/* +* Back to atomic context. First, check if we migrated to a new +* CPU with a usable percpu cluster. If so, try using that instead. +* No need to check it for the spinning device, as swap is mm/swapfile.c static inline unsigned int cluster_offset(struct swap_info_struct *si, · line 504
Quoted 10 lines
mm/swapfile.c +10
-0+* serialized by the global lock on them. +* +* The is_usable check is a bit rough, but ensures order 0 success. +*/ + offset =this_cpu_read (percpu_swap_cluster.offset[order]); +if ((si->flags &SWP_SOLIDSTATE ) && offset) { + pcp_ci =swap_cluster_lock (si, offset); +if (cluster_is_usable (pcp_ci, order) && + pcp_ci->count <SWAPFILE_CLUSTER ) { + ci = pcp_ci;Quoted 4 lines
^^^^^^^^^^^^^ci came from the caller, and in the case of
isolate_lock_cluster()they had just removed it from a list. We overwrite ci and return something different.
Quoted 7 lines
Yes, that's expected. See the comment above. We have just dropped local lock so it's possible that we migrated to another CPU which has its own percpu cache ci (percpu_swap_cluster.offset).
To avoid fragmentation, drop the isolated ci and use the percpu ci instead. But you are right that I need to add the ci back to the list, or it will be leaked. Thanks!
Yeah, the comment helped a lot (thank you). It was just the leak I was worried about ;)
-chris
Chris Li
maintainerRe: [PATCH v3 14/15] mm, swap: implement dynamic allocation of swap table
Quoted 33 lines · Chris Mason
Quoted 3 lines · Kairui Song
Quoted 2 lines · Chris Mason
On Thu, 11 Sep 2025 00:08:32 +0800 Kairui Song <[email protected]> wrote:
[ ... ]
spin_lock(&si->global_cluster_lock);Quoted 23 lines
Quoted 14 lines
Quoted 4 lines
mm/swapfile.c +4
-0+/* +* Back to atomic context. First, check if we migrated to a new +* CPU with a usable percpu cluster. If so, try using that instead. +* No need to check it for the spinning device, as swap is mm/swapfile.c static inline unsigned int cluster_offset(struct swap_info_struct *si, · line 504
Quoted 10 lines
mm/swapfile.c +10
-0+* serialized by the global lock on them. +* +* The is_usable check is a bit rough, but ensures order 0 success. +*/ + offset =this_cpu_read (percpu_swap_cluster.offset[order]); +if ((si->flags &SWP_SOLIDSTATE ) && offset) { + pcp_ci =swap_cluster_lock (si, offset); +if (cluster_is_usable (pcp_ci, order) && + pcp_ci->count <SWAPFILE_CLUSTER ) { + ci = pcp_ci;Quoted 4 lines
^^^^^^^^^^^^^ci came from the caller, and in the case of
isolate_lock_cluster()they had just removed it from a list. We overwrite ci and return something different.Yes, that's expected. See the comment above. We have just dropped local lock so it's possible that we migrated to another CPU which has its own percpu cache ci (percpu_swap_cluster.offset).
Quoted 6 lines
Quoted 3 lines
To avoid fragmentation, drop the isolated ci and use the percpu ci instead. But you are right that I need to add the ci back to the list, or it will be leaked. Thanks!
Yeah, the comment helped a lot (thank you). It was just the leak I was worried about ;)
As Kairui said, that is not a leak, it is the intended behavior. It rotates the listhead when fetching the ci from the list to avoid repeatedly trying some fragment cluster which has a very low success rate. Otherwise it can stall on the same fragmented list. It does look odd at the first glance. That is the best we can do so far without introducing a lot of repeating rotation logic to the caller. If you find other ways to improve the reading without performance penalty and make code simpler, feel free to make suggestions or even patches.
Chris
Chris Li
maintainerRe: [PATCH v3 14/15] mm, swap: implement dynamic allocation of swap table
Quoted 42 lines · Chris Li
Quoted 33 lines · Chris Mason
Quoted 3 lines · Kairui Song
Quoted 2 lines · Chris Mason
On Thu, 11 Sep 2025 00:08:32 +0800 Kairui Song <[email protected]> wrote:
[ ... ]
spin_lock(&si->global_cluster_lock);Quoted 23 lines
Quoted 14 lines
Quoted 4 lines
mm/swapfile.c +4
-0+/* +* Back to atomic context. First, check if we migrated to a new +* CPU with a usable percpu cluster. If so, try using that instead. +* No need to check it for the spinning device, as swap is mm/swapfile.c static inline unsigned int cluster_offset(struct swap_info_struct *si, · line 504
Quoted 10 lines
mm/swapfile.c +10
-0+* serialized by the global lock on them. +* +* The is_usable check is a bit rough, but ensures order 0 success. +*/ + offset =this_cpu_read (percpu_swap_cluster.offset[order]); +if ((si->flags &SWP_SOLIDSTATE ) && offset) { + pcp_ci =swap_cluster_lock (si, offset); +if (cluster_is_usable (pcp_ci, order) && + pcp_ci->count <SWAPFILE_CLUSTER ) { + ci = pcp_ci;Quoted 4 lines
^^^^^^^^^^^^^ci came from the caller, and in the case of
isolate_lock_cluster()they had just removed it from a list. We overwrite ci and return something different.Yes, that's expected. See the comment above. We have just dropped local lock so it's possible that we migrated to another CPU which has its own percpu cache ci (percpu_swap_cluster.offset).
Quoted 6 lines
Quoted 3 lines
To avoid fragmentation, drop the isolated ci and use the percpu ci instead. But you are right that I need to add the ci back to the list, or it will be leaked. Thanks!
Yeah, the comment helped a lot (thank you). It was just the leak I was worried about ;)
Quoted 8 lines
As Kairui said, that is not a leak, it is the intended behavior. It rotates the listhead when fetching the ci from the list to avoid repeatedly trying some fragment cluster which has a very low success rate. Otherwise it can stall on the same fragmented list. It does look odd at the first glance. That is the best we can do so far without introducing a lot of repeating rotation logic to the caller. If you find other ways to improve the reading without performance penalty and make code simpler, feel free to make suggestions or even patches.
Sorry I take back what I just said. There might be a real leak as you point out. My bad.
Chris
Re: [PATCH v3 05/15] mm, swap: always lock and check the swap cache folio before use
In reply to Kairui SongQuoted 11 lines · Kairui Song
From: Kairui Song <[email protected]>
Swap cache lookup only increases the reference count of the returned folio. That's not enough to ensure a folio is stable in the swap cache, so the folio could be removed from the swap cache at any time. The caller should always lock and check the folio before using it.
We have just documented this in kerneldoc, now introduce a helper for swap cache folio verification with proper sanity checks.
Quoted 8 lines
Also, sanitize a few current users to use this convention and the new helper for easier debugging. They were not having observable problems yet, only trivial issues like wasted CPU cycles on swapoff or reclaiming. They would fail in some other way, but it is still better to always follow this convention to make things robust and make later commits easier to do.
Signed-off-by Kairui Song <[email protected]>
Apologies for the slow response. Swap code is dense, so it takes me awhile to read each patch :(
Anyway, this LGTM!