Skip to content

swap: Swapout/swapin THP in one piece

Huang Ying linux-mm lkml 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v1 Latest posting 3/321 patches 9 repliesIn review
  2. v1 posting 2/321 patches 0 repliesSuperseded
  3. v1 posting 1/321 patches 0 repliesSuperseded

v5 from the cover letter

- Rebased on 9/20 HEAD of mmotm/master

- Merged the swap operations implementation for the huge and the normal swap entries when possible

- Added more code comments to improve code readability

- Changed function parameter style to avoid to use Boolean parameter as much as possible

- Fixed a deadlock issue in do_huge_pmd_swap_page(), thanks 0-Day and sparse

Patches in v1 posting 3/3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/21swap: Enable PMD swap operations for CONFIG_THP_SWAParch/x86/include/asm/pgtable.h include/asm-generic/pgtable.h include/linux/swapops.h +1 more+33 -230 replies
2/21swap: Add __swap_duplicate_locked()mm/swapfile.c+35 -280 replies
3/21swap: Support PMD swap mapping in swap_duplicate()include/linux/swap.h mm/memory.c mm/rmap.c +2 more+97 -259 replies
4/21swap: Support PMD swap mapping in put_swap_page()mm/swapfile.c+24 -70 replies
5/21swap: Support PMD swap mapping in free_swap_and_cache()/swap_free()arch/s390/mm/pgtable.c include/linux/swap.h kernel/power/swap.c +4 more+149 -490 replies
6/21swap: Support PMD swap mapping when splitting huge PMDinclude/linux/huge_mm.h include/linux/swap.h mm/huge_memory.c +1 more+85 -50 replies
7/21swap: Support PMD swap mapping in split_swap_cluster()include/linux/swap.h mm/huge_memory.c mm/swapfile.c+57 -250 replies
8/21swap: Support to read a huge swap cluster for swapin a THPinclude/linux/huge_mm.h include/linux/swap.h mm/huge_memory.c +2 more+99 -500 replies
9/21swap: Swapin a THP in one pieceinclude/linux/huge_mm.h mm/huge_memory.c mm/memory.c+193 -60 replies
10/21swap: Support to count THP swapin and its fallbackDocumentation/admin-guide/mm/transhuge.rst include/linux/vm_event_item.h mm/huge_memory.c +2 more+27 -40 replies
11/21swap: Add sysfs interface to configure THP swapinDocumentation/admin-guide/mm/transhuge.rst include/linux/huge_mm.h mm/huge_memory.c+127 -190 replies
12/21swap: Support PMD swap mapping in swapoffinclude/asm-generic/pgtable.h include/linux/huge_mm.h mm/huge_memory.c +1 more+97 -150 replies
13/21swap: Support PMD swap mapping in madvise_free()mm/huge_memory.c mm/madvise.c+40 -160 replies
14/21swap: Support to move swap account for PMD swap mappinginclude/linux/huge_mm.h include/linux/swap.h include/linux/swap_cgroup.h +4 more+174 -400 replies
15/21swap: Support to copy PMD swap mapping when fork()mm/huge_memory.c+57 -150 replies
16/21swap: Free PMD swap mapping when zap_huge_pmd()mm/huge_memory.c+21 -110 replies
17/21swap: Support PMD swap mapping for MADV_WILLNEEDmm/madvise.c+24 -20 replies
18/21swap: Support PMD swap mapping in mincore()mm/mincore.c+31 -60 replies
19/21swap: Support PMD swap mapping in common pathfs/proc/task_mmu.c mm/gup.c mm/huge_memory.c +1 more+34 -230 replies
20/21swap: create PMD swap mapping when unmap the THPinclude/linux/huge_mm.h mm/huge_memory.c mm/rmap.c +1 more+84 -60 replies
21/21swap: Update help of CONFIG_THP_SWAPmm/Kconfig+0 -20 replies

Reviews trailers per patch

1 of 21 patches has a Reviewed-by or Acked-by.

PatchDan Williams
1/21
2/21
3/21
4/21
5/21
6/21
7/21
8/21
9/21
10/21
11/21
12/21
13/21
14/21
15/21
16/21
17/21
18/21
19/21
20/21
21/21R-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v1 thread

Cover letter [PATCH -V5 RESEND 00/21] swap: Swapout/swapin THP in one piece

Hi, Andrew, could you help me to check whether the overall design is reasonable?

Hi, Hugh, Shaohua, Minchan and Rik, could you help me to review the swap part of the patchset? Especially [02/21], [03/21], [04/21], [05/21], [06/21], [07/21], [08/21], [09/21], [10/21], [11/21], [12/21], [20/21], [21/21].

Hi, Andrea and Kirill, could you help me to review the THP part of the patchset? Especially [01/21], [07/21], [09/21], [11/21], [13/21], [15/21], [16/21], [17/21], [18/21], [19/21], [20/21].

Hi, Johannes and Michal, could you help me to review the cgroup part of the patchset? Especially [14/21].

And for all, Any comment is welcome!

This patchset is based on the 2018-09-20 head of mmotm/master.

This is the final step of THP (Transparent Huge Page) swap optimization. After the first and second step, the splitting huge page is delayed from almost the first step of swapout to after swapout has been finished. In this step, we avoid splitting THP for swapout and swapout/swapin the THP in one piece.

We tested the patchset with vm-scalability benchmark swap-w-seq test case, with 16 processes. The test case forks 16 processes. Each process allocates large anonymous memory range, and writes it from begin to end for 8 rounds. The first round will swapout, while the remaining rounds will swapin and swapout. The test is done on a Xeon E5 v3 system, the swap device used is a RAM simulated PMEM (persistent memory) device. The test result is as follow,

base optimized
---------------- --------------------------
%stddev %change %stddev
\ | \

   1417897 A+-  2%    +992.8%   15494673        vm-scalability.throughput
   1020489 A+-  4%   +1091.2%   12156349        vmstat.swap.si
   1255093 A+-  3%    +940.3%   13056114        vmstat.swap.so
   1259769 A+-  7%   +1818.3%   24166779        meminfo.AnonHugePages
  28021761           -10.7%   25018848 A+-  2%  meminfo.AnonPages
  64080064 A+-  4%     -95.6%    2787565 A+- 33%  interrupts.CAL:Function_call_interrupts
     13.91 A+-  5%     -13.8        0.10 A+- 27%  perf-profile.children.cycles-pp.native_queued_spin_lock_slowpath

Where, the score of benchmark (bytes written per second) improved 992.8%. The swapout/swapin throughput improved 1008% (from about 2.17GB/s to 24.04GB/s). The performance difference is huge. In base kernel, for the first round of writing, the THP is swapout and split, so in the remaining rounds, there is only normal page swapin and swapout. While in optimized kernel, the THP is kept after first swapout, so THP swapin and swapout is used in the remaining rounds. This shows the key benefit to swapout/swapin THP in one piece, the THP will be kept instead of being split. meminfo information verified this, in base kernel only 4.5% of anonymous page are THP during the test, while in optimized kernel, that is 96.6%. The TLB flushing IPI (represented as interrupts.CAL:Function_call_interrupts) reduced 95.6%, while cycles for spinlock reduced from 13.9% to 0.1%. These are performance benefit of THP swapout/swapin too.

Below is the description for all steps of THP swap optimization.

Recently, the performance of the storage devices improved so fast that we cannot saturate the disk bandwidth with single logical CPU when do page swapping even on a high-end server machine. Because the performance of the storage device improved faster than that of single logical CPU. And it seems that the trend will not change in the near future. On the other hand, the THP becomes more and more popular because of increased memory size. So it becomes necessary to optimize THP swap performance.

The advantages to swapout/swapin a THP in one piece include:

- Batch various swap operations for the THP. Many operations need to be done once per THP instead of per normal page, for example, allocating/freeing the swap space, writing/reading the swap space, flushing TLB, page fault, etc. This will improve the performance of the THP swap greatly.

- The THP swap space read/write will be large sequential IO (2M on x86_64). It is particularly helpful for the swapin, which are usually 4k random IO. This will improve the performance of the THP swap too.

- It will help the memory fragmentation, especially when the THP is heavily used by the applications. The THP order pages will be free up after THP swapout.

- It will improve the THP utilization on the system with the swap turned on. Because the speed for khugepaged to collapse the normal pages into the THP is quite slow. After the THP is split during the swapout, it will take quite long time for the normal pages to collapse back into the THP after being swapin. The high THP utilization helps the efficiency of the page based memory management too.

There are some concerns regarding THP swapin, mainly because possible enlarged read/write IO size (for swapout/swapin) may put more overhead on the storage device. To deal with that, the THP swapin is turned on only when necessary. A new sysfs interface:
/sys/kernel/mm/transparent_hugepage/swapin_enabled is added to configure it. It uses "always/never/madvise" logic, to be turned on globally, turned off globally, or turned on only for VMA with MADV_HUGEPAGE, etc.
GE, etc.

Changelog
---------

v5:

- Rebased on 9/20 HEAD of mmotm/master

- Merged the swap operations implementation for the huge and the normal swap entries when possible

- Added more code comments to improve code readability

- Changed function parameter style to avoid to use Boolean parameter as much as possible

- Fixed a deadlock issue in do_huge_pmd_swap_page(), thanks 0-Day and sparse

v4:

- Rebased on 6/14 HEAD of mmotm/master

- Fixed one build bug and several coding style issues, Thanks Daniel Jordon

v3:

- Rebased on 5/18 HEAD of mmotm/master

- Fixed a build bug, Thanks 0-Day!

v2:

- Fixed several build bugs, Thanks 0-Day!

- Improved documentation as suggested by Randy Dunlap.

- Fixed several bugs in reading huge swap cluster