Subject: [PATCH -v4] THP swap: Delay splitting THP during swapping out
Subsystems (7)
PATCH 1/1 Subject: [PATCH -v4] THP swap: Delay splitting THP during swapping out
From: Huang Ying <[email protected]>
Johannes suggested me to use two big patches instead 9 patches. And he feels that is easier for him to review. I am not sure whether this is desirable for other reviewers too. So I sent out both versions for review. If this version is preferable for more reviewers, I will take this way. Otherwise, I will still use the traditional 9 patches version as main version and use this version for review by some reviewers.
Because of that, I didn't separate can_split_huge_page() etc. code into a separate patch as Johannes suggested for now. And the patch description should be improved if big patches are chosen finally.
This patchset is to optimize the performance of Transparent Huge Page (THP) swap.
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 [01/10], [04/10], [05/10], [06/10], [07/10], [10/10].
Hi, Andrea and Kirill, could you help me to review the THP part of the patchset? Especially [02/10], [03/10], [09/10] and [10/10].
Hi, Johannes, Michal and Vladimir, I am not very confident about the memory cgroup part, especially [02/10] and [03/10]. Could you help me to review it?
And for all, Any comment is welcome!
Recently, the performance of the storage devices improved so fast that we cannot saturate the disk bandwidth when do page swap out even on a high-end server machine. Because the performance of the storage device improved faster than that of 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 of the THP swap support include:
- Batch the swap operations for the THP to reduce lock acquiring/releasing, including allocating/freeing the swap space, adding/deleting to/from the swap cache, and writing/reading the swap space, etc. This will help improve the performance of the THP swap.
- The THP swap space read/write will be 2M sequential IO. It is particularly helpful for the swap read, 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 2M continuous pages will be free up after THP swapping out.
- 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 swapping out, it will take quite long time for the normal pages to collapse back into the THP after being swapped in. The high THP utilization helps the efficiency of the page based memory management too.
This patchset is based on 9/20 head of mmotm/master.
This patchset is the first step for the THP swap support. The plan is to delay splitting THP step by step, finally avoid splitting THP during the THP swapping out and swap out/in the THP as a whole.
As the first step, in this patchset, the splitting huge page is delayed from almost the first step of swapping out to after allocating the swap space for the THP and adding the THP into the swap cache. This will reduce lock acquiring/releasing for the locks used for the swap cache management.
With the patchset, the swap out throughput improves 12.1% (from about 1.12GB/s to about 1.25GB/s) in the vm-scalability swap-w-seq test case with 16 processes. The test is done on a Xeon E5 v3 system. The swap device used is a RAM simulated PMEM (persistent memory) device. To test the sequential swapping out, the test case uses 16 processes, which sequentially allocate and write to the anonymous pages until the RAM and part of the swap device is used up.
The detailed compare result is as follow,
base base+patchset
---------------- --------------------------
%stddev %change %stddev
\ | \
1118821 A+- 0% +12.1% 1254241 A+- 1% vmstat.swap.so
2460636 A+- 1% +10.6% 2720983 A+- 1% vm-scalability.throughput
308.79 A+- 1% -7.9% 284.53 A+- 1% vm-scalability.time.elapsed_time
1639 A+- 4% +232.3% 5446 A+- 1% meminfo.SwapCached
0.70 A+- 3% +8.7% 0.77 A+- 5% perf-stat.ipc
9.82 A+- 8% -31.6% 6.72 A+- 2% perf-profile.cycles-pp._raw_spin_lock_irq.__add_to_swap_cache.add_to_swap_cache.add_to_swap.shrink_page_listQuoted 1 line
From the swap out throughput number, we can find, even tested on a RAM
simulated PMEM (Persistent Memory) device, the swap out throughput can reach only about 1.1GB/s. While, in the file IO test, the sequential write throughput of an Intel P3700 SSD can reach about 1.8GB/s steadily. And according the following URL,
https://www-ssl.intel.com/content/www/us/en/solid-state-drives/intel-ssd-dc-family-for-pcie.html
The sequential write throughput of Intel P3608 SSD can reach about 3.0GB/s, while the random read IOPS can reach about 850k. It is clear that the bottleneck has moved from the disk to the kernel swap component itself.
The improved storage device performance should have made the swap becomes a better feature than before with better performance. But because of the issues of kernel swap component itself, the swap performance is still kept at the low level. That prevents the swap feature to be used by more users. And this in turn causes few kernel developers think it is necessary to optimize kernel swap component. To break the loop, we need to optimize the performance of kernel swap component. Optimize the THP swap performance is part of it.
Changelog:
v4:
- Per Johannes' comments, simplified swap cgroup array accessing code.
- Per Kirill and Dave Hansen's comments, used HPAGE_PMD_NR instead of HPAGE_SIZE/PAGE_SIZE.
- Per Anshuman's comments, used HPAGE_PMD_NR instead of 512 in patch description.
v3:
- Per Andrew's suggestion, used a more systematical way to determine whether to enable THP swap optimization
- Per Andrew's comments, moved as much as possible code into #ifdef CONFIG_TRANSPARENT_HUGE_PAGE/#endif or "if (PageTransHuge())"
- Fixed some coding style warning.
v2:
- Original [1/11] sent separately and merged
- Use switch in 10/10 per Hiff's suggestion
--- arch/x86/Kconfig | 1 + include/linux/huge_mm.h | 7 ++ include/linux/page-flags.h | 2 +- include/linux/swap.h | 45 ++++++-- include/linux/swap_cgroup.h | 6 +- mm/Kconfig | 13 +++ mm/huge_memory.c | 26 ++++- mm/memcontrol.c | 55 ++++++---- mm/shmem.c | 2 +- mm/swap_cgroup.c | 40 +++++-- mm/swap_state.c | 125 +++++++++++++++++---- mm/swapfile.c | 260 +++++++++++++++++++++++++++++++++----------- 12 files changed, 445 insertions(+), 137 deletions(-)
arch/x86/Kconfig +1 -0
@@ -164,6 +164,7 @@ config X86 select HAVE_STACK_VALIDATION if X86_64 select ARCH_USES_HIGH_VMA_FLAGS if X86_INTEL_MEMORY_PROTECTION_KEYS select ARCH_HAS_PKEYS if X86_INTEL_MEMORY_PROTECTION_KEYS + select ARCH_USES_THP_SWAP_CLUSTER if X86_64 config INSTRUCTION_DECODER def_bool y
include/linux/huge_mm.h +7 -0
@@ -94,6 +94,7 @@ extern unsigned long thp_get_unmapped_area(struct file *filp,extern void prep_transhuge_page (struct page *page);extern void free_transhuge_page (struct page *page); +bool can_split_huge_page (struct page *page);int split_huge_page_to_list (struct page *page,struct list_head *list);static inline int split_huge_page (struct page *page) {
@@ -176,6 +177,12 @@ static inline void prep_transhuge_page(struct page *page) {}#define thp_get_unmapped_area NULL +static inline bool +can_split_huge_page (struct page *page) +{ +BUILD_BUG (); +return false; +}static inline int split_huge_page_to_list (struct page *page,struct list_head *list) {
include/linux/page-flags.h +1 -1
@@ -314,7 +314,7 @@ PAGEFLAG_FALSE(HighMem)#endif #ifdef CONFIG_SWAP -PAGEFLAG(SwapCache, swapcache, PF_NO_COMPOUND) +PAGEFLAG(SwapCache, swapcache, PF_NO_TAIL)#else PAGEFLAG_FALSE(SwapCache)#endif
include/linux/swap.h +38 -7
93 lines not on this page · raw message
include/linux/swap_cgroup.h +4 -2
@@ -7,7 +7,8 @@extern unsigned short swap_cgroup_cmpxchg (swp_entry_t ent,unsigned short old,unsigned short new); -extern unsigned short swap_cgroup_record (swp_entry_t ent,unsigned short id); +extern unsigned short swap_cgroup_record (swp_entry_t ent,unsigned short id, +unsigned int nr_ents);extern unsigned short lookup_swap_cgroup_id (swp_entry_t ent);extern int swap_cgroup_swapon (int type,unsigned long max_pages);extern void swap_cgroup_swapoff (int type);
@@ -15,7 +16,8 @@ extern void swap_cgroup_swapoff(int type);#else static inline -unsigned short swap_cgroup_record (swp_entry_t ent,unsigned short id) +unsigned short swap_cgroup_record (swp_entry_t ent,unsigned short id, +unsigned int nr_ents) {return 0 ; }
mm/Kconfig +13 -0
@@ -503,6 +503,19 @@ config FRONTSWAP If unsure, say Y to enable frontswap. +config ARCH_USES_THP_SWAP_CLUSTER + bool + default n + +config THP_SWAP_CLUSTER + bool + depends on SWAP && TRANSPARENT_HUGEPAGE && ARCH_USES_THP_SWAP_CLUSTER + default y + help + Use one swap cluster to hold the contents of the THP + (Transparent Huge Page) swapped out. The size of the swap + cluster will be same as that of THP. + config CMA bool "Contiguous Memory Allocator" depends on HAVE_MEMBLOCK && MMU
mm/huge_memory.c +22 -4
@@ -1834,7 +1834,7 @@ static void __split_huge_page_tail(struct page *head, int tail,* atomic_set() here would be safe on all archs (and not only on x86), * it's safer to use atomic_inc()/atomic_add(). */ -if (PageAnon (head)) { +if (PageAnon (head) && !PageSwapCache (head)) {page_ref_inc (page_tail); }else {/* Additional pin to radix tree */
@@ -1845,6 +1845,7 @@ static void __split_huge_page_tail(struct page *head, int tail, page_tail->flags |= (head->flags & ((1L << PG_referenced) | (1L << PG_swapbacked) | + (1L << PG_swapcache) | (1L << PG_mlocked) | (1L << PG_uptodate) | (1L << PG_active) |
@@ -1907,7 +1908,11 @@ static void __split_huge_page(struct page *page, struct list_head *list,ClearPageCompound (head);/* See comment in __split_huge_page_tail() */ if (PageAnon (head)) { -page_ref_inc (head); +/* Additional pin to radix tree of swap cache */ +if (PageSwapCache (head)) +page_ref_add (head,2 ); +else +page_ref_inc (head); }else {/* Additional pin to radix tree */ page_ref_add (head,2 );
@@ -2016,6 +2021,19 @@ int page_trans_huge_mapcount(struct page *page, int *total_mapcount)return ret; } +/* Racy check whether the huge page can be split */ +bool can_split_huge_page (struct page *page) +{ +int extra_pins; + +/* Additional pins from radix tree */ +if (PageAnon (page)) + extra_pins =PageSwapCache (page) ?HPAGE_PMD_NR :0 ; +else + extra_pins =HPAGE_PMD_NR ; +return total_mapcount (page) ==page_count (page) - extra_pins -1 ; +} +/* * This function splits huge page into normal pages. @page can point to any * subpage of huge page to split. Split doesn't change the position of @page.
@@ -2064,7 +2082,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) ret = -EBUSY ; goto out; } - extra_pins =0 ; + extra_pins =PageSwapCache (head) ?HPAGE_PMD_NR :0 ; mapping =NULL ;anon_vma_lock_write (anon_vma); }else {
@@ -2086,7 +2104,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)* Racy check if we can split the page, before freeze_page() will * split PMDs */ -if (total_mapcount (head) !=page_count (head) - extra_pins -1 ) { +if (!can_split_huge_page (head)) { ret = -EBUSY ; goto out_unlock; }
mm/memcontrol.c +31 -24
123 lines not on this page · raw message
mm/shmem.c +1 -1
@@ -1248,7 +1248,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)if (!swap.val) goto redirty; -if (mem_cgroup_try_charge_swap (page, swap)) +if (mem_cgroup_try_charge_swap (page, swap,1 )) goto free_swap;/*
mm/swap_cgroup.c +30 -10
@@ -58,21 +58,27 @@ static int swap_cgroup_prepare(int type)return -ENOMEM ; } +static struct swap_cgroup *__lookup_swap_cgroup (struct swap_cgroup_ctrl *ctrl, +pgoff_t offset) +{ +struct page *mappage; +struct swap_cgroup *sc; + + mappage = ctrl->map[offset /SC_PER_PAGE ]; + sc =page_address (mappage); +return sc + offset %SC_PER_PAGE ; +} +static struct swap_cgroup *lookup_swap_cgroup (swp_entry_t ent,struct swap_cgroup_ctrl **ctrlp) {pgoff_t offset =swp_offset (ent);struct swap_cgroup_ctrl *ctrl; -struct page *mappage; -struct swap_cgroup *sc; ctrl = &swap_cgroup_ctrl[swp_type (ent)];if (ctrlp) *ctrlp = ctrl; - - mappage = ctrl->map[offset /SC_PER_PAGE ]; - sc =page_address (mappage); -return sc + offset %SC_PER_PAGE ; +return __lookup_swap_cgroup (ctrl, offset); }/**
@@ -105,25 +111,39 @@ unsigned short swap_cgroup_cmpxchg(swp_entry_t ent, }/** -* swap_cgroup_record - record mem_cgroup for this swp_entry. -* @ent: swap entry to be recorded into +* swap_cgroup_record - record mem_cgroup for a set of swap entries +* @ent: the first swap entry to be recorded into * @id: mem_cgroup to be recorded +* @nr_ents: number of swap entries to be recorded * * Returns old value at success, 0 at failure. * (Of course, old value can be 0.) */ -unsigned short swap_cgroup_record (swp_entry_t ent,unsigned short id) +unsigned short swap_cgroup_record (swp_entry_t ent,unsigned short id, +unsigned int nr_ents) {struct swap_cgroup_ctrl *ctrl;struct swap_cgroup *sc;unsigned short old;unsigned long flags; +pgoff_t offset =swp_offset (ent); +pgoff_t end = offset + nr_ents; sc =lookup_swap_cgroup (ent, &ctrl);spin_lock_irqsave (&ctrl->lock, flags); old = sc->id; - sc->id = id; +for (;;) { +VM_BUG_ON (sc->id != old); + sc->id = id; + offset++; +if (offset == end) +break ; +if (offset %SC_PER_PAGE ) + sc++; +else + sc =__lookup_swap_cgroup (ctrl, offset); + }spin_unlock_irqrestore (&ctrl->lock, flags);return old;
mm/swap_state.c +103 -22
216 lines not on this page · raw message
mm/swapfile.c +194 -66
353 lines not on this page · raw message
Signature · 7 lines
2.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>