Skip to content

mm, swap: introduce swap table

Kairui Song linux-mm lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest28 patches 14 repliesIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/28mm, swap: don't scan every fragment clusterinclude/linux/swap.h mm/swapfile.c+9 -240 replies
2/28mm, swap: consolidate the helper for mincoremm/mincore.c mm/swap.h mm/swap_state.c+40 -580 replies
3/28mm/shmem, swap: remove SWAP_MAP_SHMEMinclude/linux/swap.h mm/shmem.c mm/swapfile.c+25 -340 replies
4/28mm, swap: split readahead update out of swap cache lookupmm/memory.c mm/mincore.c mm/shmem.c +4 more+72 -700 replies
5/28mm, swap: sanitize swap cache lookup conventionmm/memory.c mm/shmem.c mm/swap.h +3 more+55 -2313 replies
6/28mm, swap: rearrange swap cluster definition and helpersinclude/linux/swap.h mm/swap.h mm/swapfile.c+92 -1062 replies
7/28mm, swap: tidy up swap device and cluster info helpersinclude/linux/swap.h mm/memory.c mm/page_io.c +3 more+57 -340 replies
8/28mm, swap: use swap table for the swap cache and switch APIinclude/linux/swap.h mm/filemap.c mm/huge_memory.c +9 more+473 -3040 replies
9/28mm/swap: rename __read_swap_cache_async to __swapin_cache_allocmm/swap.h mm/swap_state.c mm/swapfile.c +1 more+14 -140 replies
10/28mm, swap: add a swap helper for bypassing only read aheadmm/swap.h mm/swap_state.c+105 -590 replies
11/28mm, swap: clean up and consolidate helper for mTHP swapin checkmm/memory.c+41 -495 replies
12/28mm, swap: never bypass the swap cache for SWP_SYNCHRONOUS_IOmm/memory.c+116 -1510 replies
13/28mm/shmem, swap: avoid redundant Xarray lookup during swapinmm/shmem.c+37 -300 replies
14/28mm/shmem: never bypass the swap cache for SWP_SYNCHRONOUS_IOmm/shmem.c mm/swap.h mm/swapfile.c+48 -1000 replies
15/28mm, swap: split locked entry freeing into a standalone helpermm/swapfile.c+32 -290 replies
16/28mm, swap: use swap cache as the swap in synchronize layerinclude/linux/swap.h mm/swap.h mm/swap_state.c +3 more+76 -1020 replies
17/28mm, swap: sanitize swap entry management workflowarch/s390/mm/pgtable.c include/linux/swap.h kernel/power/swap.c +6 more+176 -920 replies
18/28mm, swap: rename and introduce folio_free_swap_cachemm/memory-failure.c mm/memory.c mm/shmem.c +4 more+35 -240 replies
19/28mm, swap: clean up and improve swap entries batch freeingmm/swapfile.c+67 -980 replies
20/28mm, swap: check swap table directly for checking cachemm/memory.c mm/swap.h mm/swap_state.c +1 more+48 -353 replies
21/28mm, swap: add folio to swap cache directly on allocationmm/swap.h mm/swap_state.c mm/swapfile.c+93 -850 replies
22/28mm, swap: drop the SWAP_HAS_CACHE flaginclude/linux/swap.h mm/swap.h mm/swap_state.c +1 more+67 -1520 replies
23/28mm, swap: remove no longer needed _swap_info_getmm/swapfile.c+16 -470 replies
24/28mm, swap: implement helpers for reserving data in swap tablemm/swap_table.h+119 -112 replies
25/28mm/workingset: leave highest 8 bits empty for anon shadowmm/swap_table.h mm/workingset.c+27 -130 replies
26/28mm, swap: minor clean up for swaponmm/swapfile.c+26 -360 replies
27/28mm, swap: use swap table to track swap countinclude/linux/swap.h mm/memory.c mm/swap.h +2 more+253 -4590 replies
28/28mm, swap: implement dynamic allocation of swap tablemm/swap.h mm/swap_state.c mm/swap_table.h +1 more+213 -722 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH 00/28] mm, swap: introduce swap table

From: Kairui Song <[email protected]>

This is the series that implements the Swap Table idea propose in the LSF/MM/BPF topic "Integrate swap cache, swap maps with swap allocator" about one month ago [1].

With this series, swap subsystem will have a ~20-30% performance gain from basic sequential swap to heavy workloads, for both 4K and mTHP folios. The idle memory usage is already much lower, the average memory consumption is still the same or will also be even lower (with further works). And this enables many more future optimizations, with better defined swap operations.

This series is stable and mergeable on both mm-unstable and mm-stable. It's a long series so it might be challenging to review though. It has been working well with many stress tests.

You can also find the latest branch here:
https://github.com/ryncsn/linux/tree/kasong/devel/swap-table

With swap table, a table entry will be the fundamental and the only needed data structure for: swap cache, swap map, swap cgroup map. Reducing the memory usage and making the performance better, also provide more flexibility and better abstraction.

/*
* Swap table entry type and bit layouts:
*

 * NULL:     | ------------    0   -------------|
 * Shadow:   | SWAP_COUNT |---- SHADOW_VAL ---|1|
 * Folio:    | SWAP_COUNT |------ PFN -------|10|
 * Pointer:  |----------- Pointer ----------|100|

*/

This series contains many clean up and refractors due to many historical issues with the SWAP subsystem, e.g. the fuzzy workflow and definition of how swap entries are handled, and a lot of corner issues as mentioned in the LSF/MM/BPF talk.

There could be temporary increase of complicity or memory consumption in the middle of this series, but in the end it's a much simplified and sanitized in the end. And these patches have dependency on each other due to the current complex swap design. So this takes a long series.

This series cleaned up most of the issues and improved the situation with following order:
- Simplification and optimizations (Patch 1 - 3)
- Tidy up swap info and cache lookup (Patch 4 - 6)
- Introduce basic swap table infrastructure (Patch 7 - 8)
- Removed swap cache bypassing with SWP_SYNCHRONOUS_IO, enabling mTHP for more workloads (Patch 9 - 14).
- Simplify swap in synchronization with swap cache, eliminating long tailing issues and improve performance, swap can be synced with folio lock now (Patch 15 - 16).
- Make most swap operations into folio based. We now can use folio based helpers that ensures the swap entries are stable with folio lock, which also make more optimization and sanity checks doable. (Patch 17 - 18)
- Removed SWAP_HAS_CACHE. (Patch 19 - 22)
- Completely rework the swap counting using swap table, and remove COUNT_CONTINUED (Patch 23 - 27).
- Dynamic reclaim and allocation for swap table (Patch 28)

And the performance is looking great too:

vm-scalability usemem shows a great improvement:
Test using: usemem --init-time -O -y -x -n 31 1G (1G memcg, pmem as swap)

                Before:         After:
System time:    217.39s         161.59s      (-25.67%)
Throughput:     3933.58 MB/s    4975.55 MB/s (+26.48%)

(Similar results with random usemem -R)

Build kernel with defconfig on tmpfs with ZRAM:
Below results shows a test matrix using different memory cgroup limit and job numbers.

  make -j<NR>|  Total Sys Time (seconds) |   Total Time (seconds)
  (NR / Mem )|  (Before / After / Delta) | (Before / After / Delta)
  With 4k pages only:
   6 / 192M  |    5327 /  3915 / -26.5%  |    1427 /  1141 / -20.0%
  12 / 256M  |    5373 /  4009 / -25.3%  |     743 /   606 / -18.4%
  24 / 384M  |    6149 /  4523 / -26.4%  |     438 /   353 / -19.4%
  48 / 768M  |    7285 /  4521 / -37.9%  |     251 /   190 / -24.3%
  With 64k mTHP:
  24 / 512M  |    4399 /  3328 / -24.3%  |     345 /   289 / -16.2%
  48 /   1G  |    5072 /  3406 / -32.8%  |     187 /   150 / -19.7%

Memory usage is also reduced. Although this series haven't remove the swap cgroup array yet, the peak usage of one swap entry is already reduced from 12 bytes to 10 bytes. And the swap table is dynamically allocated which means the idle memory usage will be reduced by a lot.

Some other high lights and notes:

1. This series introduce a set of helpers "folio_alloc_swap", "folio_dup_swap", "folio_put_swap", "folio_free_swap*" to make most swap operations folio based, this should brought a clean border between swap and rest of mm. Also split the hibernation swap entry allocation out of ordinary swap operations.

3. This series enabled mTHP swap-in, and read ahead skipping for more workloads, as it removed the swap cache bypassing path:

We currently only do mTHP swap in and read ahead bypass for SWP_SYNCHRONOUS_IO device only when swap count of all related entries are equal to one. This makes no sense, clearly read ahead and mTHP behaviour should have nothing to do with swap count, it's only a defect due to current design that they are coupled with swap cache bypassing. This series removed that limitation while showing a major performance improvement.

This not only showed a performance gain, also should reduce mTHP fragmentation.

4. By removing the old swap cache design, now all swap cache are protected by fine grained cluster lock, this also removed the cluster shuffle algorithm should improve the performance for SWAP on HDD too (Fixing [4]). And also got rid of the many swap address_space instance design.

5. I dropped some future doable optimizations for now, e.g. the folio based helper will be an essential part for dropping the swap cgroup control map, which will improve the performance and reduce memory usage even more. It could be done later. And more folio batched operations could be done based on this. So this series is not in the best shape but already looking good enough.

Future work items:

1. More tests, and maybe some of the patches need to be split into smaller ones or need a few preparation series.

2. Integrate with Nhat Pham's Virtual swap space [2], while this series improves the performance and added a sanitized workload for SWAP, nothing changed feature wise. The swap table idea is suppose to be able to handle things like a virtual device in a cleaner way with both lower overhead and better flexibility, more work is needed to figure out a way to implement it.

3. Some helpers from this series could be very helpful for future works. E.g. the folio based swap helpers, now locking a folio will stabilize its swap entries, which could also be used to stabilize the under layer swap device's entries if a virtual device design is implemented, hence simplify the locking design. Also more entry types could be added for things like zero map or shmem.

4. The unified swap in path now already enabled mTHP swap in for swap count > 1 entries. This also making unify the read ahead of shmem / anon (as demostrated a year ago [3], that one conflicted with the standalone mTHP swapin path but now it's unified) doable now.

We can also implement a read ahead based mTHP swapin based on this too. This needs more discussion.

Kairui Song (27):
mm, swap: don't scan every fragment cluster
mm, swap: consolidate the helper for mincore
mm, swap: split readahead update out of swap cache lookup mm, swap: sanitize swap cache lookup convention
mm, swap: rearrange swap cluster definition and helpers mm, swap: tidy up swap device and cluster info helpers mm, swap: use swap table for the swap cache and switch API mm/swap: rename __read_swap_cache_async to __swapin_cache_alloc mm, swap: add a swap helper for bypassing only read ahead mm, swap: clean up and consolidate helper for mTHP swapin check mm, swap: never bypass the swap cache for SWP_SYNCHRONOUS_IO mm/shmem, swap: avoid redundant Xarray lookup during swapin mm/shmem: never bypass the swap cache for SWP_SYNCHRONOUS_IO mm, swap: split locked entry freeing into a standalone helper mm, swap: use swap cache as the swap in synchronize layer mm, swap: sanitize swap entry management workflow
mm, swap: rename and introduce folio_free_swap_cache mm, swap: clean up and improve swap entries batch freeing mm, swap: check swap table directly for checking cache mm, swap: add folio to swap cache directly on allocation mm, swap: drop the SWAP_HAS_CACHE flag
mm, swap: remove no longer needed _swap_info_get
mm, swap: implement helpers for reserving data in swap table mm/workingset: leave highest 8 bits empty for anon shadow mm, swap: minor clean up for swapon
mm, swap: use swap table to track swap count
mm, swap: implement dynamic allocation of swap table

Nhat Pham (1):
mm/shmem, swap: remove SWAP_MAP_SHMEM

 arch/s390/mm/pgtable.c |    2 +-
 include/linux/swap.h   |  119 +--
 kernel/power/swap.c    |    8 +-
 mm/filemap.c           |   20 +-
 mm/huge_memory.c       |   20 +-
 mm/madvise.c           |    2 +-
 mm/memory-failure.c    |    2 +-
 mm/memory.c            |  384 ++++-----
 mm/migrate.c           |   28 +-
 mm/mincore.c           |   49 +-
 mm/page_io.c           |   12 +-
 mm/rmap.c              |    7 +-
 mm/shmem.c             |  204 ++---
 mm/swap.h              |  316 ++++++--
 mm/swap_state.c        |  646 ++++++++-------
 mm/swap_table.h        |  231 ++++++
 mm/swapfile.c          | 1708 +++++++++++++++++-----------------------
 mm/userfaultfd.c       |    9 +-
 mm/vmscan.c            |   22 +-
 mm/workingset.c        |   39 +-
 mm/zswap.c             |   13 +-
 21 files changed, 1981 insertions(+), 1860 deletions(-)
 create mode 100644 mm/swap_table.h
Signature · 1 line
2.49.0