THP swap: Delay splitting THP during swapping out
Subsystems (10)
cover THP swap: Delay splitting THP during swapping out
From: Huang Ying <[email protected]>
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?
Hi, Andrea could you help me to review the THP part of the patchset?
Hi, Johannes, Michal, I am not very confident about the memory cgroup part. 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 with single logical CPU when do page swap out 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 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.
There are some concerns regarding THP swap in, mainly because possible enlarged read/write IO size (for swap in/out) may put more overhead on the storage device. To deal with that, the THP swap in should be turned on only when necessary. For example, it can be selected via "always/never/madvise" logic, to be turned on globally, turned off globally, or turned on only for VMA with MADV_HUGEPAGE, etc.
This patchset is based on 04/04 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 14.9% (from about 3.77GB/s to about 4.34GB/s) in the vm-scalability swap-w-seq test case with 8 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 creates 8 processes, which sequentially allocate and write to the anonymous pages until the RAM and part of the swap device is used up.
The detailed comparison result is as follow,
base base+patchset
---------------- --------------------------
%stddev %change %stddev
\ | \
7043990 A+- 0% +21.2% 8536807 A+- 0% vm-scalability.throughput
109.94 A+- 1% -16.2% 92.09 A+- 0% vm-scalability.time.elapsed_time
3957091 A+- 0% +14.9% 4547173 A+- 0% vmstat.swap.so
31.46 A+- 1% -38.3% 19.42 A+- 0% perf-stat.cache-miss-rate%
1.04 A+- 1% +22.2% 1.27 A+- 0% perf-stat.ipc
9.33 A+- 2% -60.7% 3.67 A+- 1% perf-profile.calltrace.cycles-pp.add_to_swap.shrink_page_list.shrink_inactive_list.shrink_node_memcg.shrink_nodeChangelog:
v8:
- Rebased on latest -mm tree
- Reorganize the patchset per Johannes' comments
- Merge add_to_swap_trans_huge() and add_to_swap() per Johannes' comments
v7:
- Rebased on latest -mm tree
- Revise get_swap_pages() THP support per Tim's comments
v6:
- Rebased on latest -mm tree (cluster lock, etc).
- Fix a potential uninitialized variable bug in __swap_entry_free()
- Revise the swap read-ahead changes to avoid a potential race condition between swap off and swap out in theory.
v5:
- Per Hillf's comments, fix a locking bug in error path of __add_to_swap_cache(). And merge the code to calculate extra_pins into can_split_huge_page().
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
Best Regards,
Huang, Ying
--
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>
PATCH 1/3 mm, THP, swap: Delay splitting THP during swap out
From: Huang Ying <[email protected]>
In this patch, splitting huge page is delayed from almost the first step of swapping out to after allocating the swap space for the THP (Transparent Huge Page) and adding the THP into the swap cache. This will batch the corresponding operation, thus improve THP swap out throughput.
This is the first step for the THP swap optimization. The plan is to delay splitting the THP step by step and avoid splitting the THP finally.
The advantages of the THP swap support include:
- Batch the swap operations for the THP and 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 to improve the THP swap performance.
- The THP swap space read/write will be 2M sequential IO. It is particularly helpful for the swap read, which usually are 4k random IO. This will help to improve the THP swap performance.
- 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 the 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.
There are some concerns regarding THP swap in, mainly because possible enlarged read/write IO size (for swap in/out) may put more overhead on the storage device. To deal with that, the THP swap in should be turned on only when necessary. For example, it can be selected via "always/never/madvise" logic, to be turned on globally, turned off globally, or turned on only for VMA with MADV_HUGEPAGE, etc.
In this patch, one swap cluster is used to hold the contents of each THP swapped out. So, the size of the swap cluster is changed to that of the THP (Transparent Huge Page) on x86_64 architecture (512). For other architectures which want such THP swap optimization, ARCH_USES_THP_SWAP_CLUSTER needs to be selected in the Kconfig file for the architecture. In effect, this will enlarge swap cluster size by 2 times on x86_64. Which may make it harder to find a free cluster when the swap space becomes fragmented. So that, this may reduce the continuous swap space allocation and sequential write in theory. The performance test in 0day shows no regressions caused by this.
In the future of THP swap optimization, some information of the swapped out THP (such as compound map count) will be recorded in the swap_cluster_info data structure.
The mem cgroup swap accounting functions are enhanced to support charge or uncharge a swap cluster backing a THP as a whole.
The swap cluster allocate/free functions are added to allocate/free a swap cluster for a THP. A fair simple algorithm is used for swap cluster allocation, that is, only the first swap device in priority list will be tried to allocate the swap cluster. The function will fail if the trying is not successful, and the caller will fallback to allocate a single swap slot instead. This works good enough for normal cases. If the difference of the number of the free swap clusters among multiple swap devices is significant, it is possible that some THPs are split earlier than necessary. For example, this could be caused by big size difference among multiple swap devices.
The swap cache functions is enhanced to support add/delete THP to/from the swap cache as a set of (HPAGE_PMD_NR) sub-pages. This may be enhanced in the future with multi-order radix tree. But because we will split the THP soon during swapping out, that optimization doesn't make much sense for this first step.
The THP splitting functions are enhanced to support to split THP in swap cache during swapping out. The page lock will be held during allocating the swap cluster, adding the THP into the swap cache and splitting the THP. So in the code path other than swapping out, if the THP need to be split, the PageSwapCache(THP) will be always false.
With the patchset, the swap out throughput improves 14.9% (from about 3.77GB/s to about 4.34GB/s) in the vm-scalability swap-w-seq test case with 8 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 creates 8 processes, which sequentially allocate and write to the anonymous pages until the RAM and part of the swap device is used up.
The detailed comparison result is as follow,
base base+patchset
---------------- --------------------------
%stddev %change %stddev
\ | \
7043990 ± 0% +21.2% 8536807 ± 0% vm-scalability.throughput
109.94 ± 1% -16.2% 92.09 ± 0% vm-scalability.time.elapsed_time
3957091 ± 0% +14.9% 4547173 ± 0% vmstat.swap.so
31.46 ± 1% -38.3% 19.42 ± 0% perf-stat.cache-miss-rate%
1.04 ± 1% +22.2% 1.27 ± 0% perf-stat.ipc
9.33 ± 2% -60.7% 3.67 ± 1% perf-profile.calltrace.cycles-pp.add_to_swap.shrink_page_list.shrink_inactive_list.shrink_node_memcg.shrink_nodeThe swap cluster is only available for SSD, so the THP swap optimization in this patchset has no effect for HDD.
--- arch/x86/Kconfig | 1 + include/linux/page-flags.h | 5 +- include/linux/swap.h | 37 ++++-- include/linux/swap_cgroup.h | 6 +- mm/Kconfig | 13 +++ mm/huge_memory.c | 11 +- mm/memcontrol.c | 58 ++++++---- mm/shmem.c | 6 +- mm/swap_cgroup.c | 40 +++++-- mm/swap_slots.c | 7 +- mm/swap_state.c | 125 +++++++++++++------- mm/swapfile.c | 275 ++++++++++++++++++++++++++++++++------------ mm/vmscan.c | 2 +- 13 files changed, 413 insertions(+), 173 deletions(-)
arch/x86/Kconfig +1 -0
@@ -178,6 +178,7 @@ config X86 select USER_STACKTRACE_SUPPORT select VIRT_TO_BUS select X86_FEATURE_NAMES if PROC_FS + select ARCH_USES_THP_SWAP_CLUSTER if X86_64 config INSTRUCTION_DECODER def_bool y
include/linux/page-flags.h +3 -2
@@ -326,11 +326,12 @@ PAGEFLAG_FALSE(HighMem)#ifdef CONFIG_SWAP static __always_inline int PageSwapCache(struct page *page) { + page =compound_head (page);return PageSwapBacked (page) &&test_bit (PG_swapcache, &page->flags); } -SETPAGEFLAG(SwapCache, swapcache, PF_NO_COMPOUND) -CLEARPAGEFLAG(SwapCache, swapcache, PF_NO_COMPOUND) +SETPAGEFLAG(SwapCache, swapcache, PF_NO_TAIL) +CLEARPAGEFLAG(SwapCache, swapcache, PF_NO_TAIL)#else PAGEFLAG_FALSE(SwapCache)#endif
include/linux/swap.h +28 -9
@@ -386,15 +386,15 @@ static inline long get_nr_swap_pages(void) }extern void si_swapinfo (struct sysinfo *); -extern swp_entry_t get_swap_page (void ); +extern swp_entry_t __get_swap_page (void );extern swp_entry_t get_swap_page_of_type (int ); -extern int get_swap_pages (int n,swp_entry_t swp_entries[]); +extern int get_swap_pages (int n,swp_entry_t swp_entries[],bool huge);extern int add_swap_count_continuation (swp_entry_t ,gfp_t );extern void swap_shmem_alloc (swp_entry_t );extern int swap_duplicate (swp_entry_t );extern int swapcache_prepare (swp_entry_t );extern void swap_free (swp_entry_t ); -extern void swapcache_free (swp_entry_t ); +extern void swapcache_free (swp_entry_t entry,bool huge);extern void swapcache_free_entries (swp_entry_t *entries,int n);extern int free_swap_and_cache (swp_entry_t );extern int swap_type_of (dev_t ,sector_t ,struct block_device **);
@@ -456,7 +456,7 @@ static inline void swap_free(swp_entry_t swp) { } -static inline void swapcache_free (swp_entry_t swp) +static inline void swapcache_free (swp_entry_t swp,bool huge) { }
@@ -518,7 +518,7 @@ static inline int try_to_free_swap(struct page *page)return 0 ; } -static inline swp_entry_t get_swap_page (void ) +static inline swp_entry_t __get_swap_page (void ) {swp_entry_t entry; entry.val =0 ;
@@ -527,6 +527,21 @@ static inline swp_entry_t get_swap_page(void)#endif /* CONFIG_SWAP */ +static inline swp_entry_t get_swap_page (bool huge) +{ +#ifdef CONFIG_THP_SWAP_CLUSTER +if (huge) { +swp_entry_t entry; + +if (get_swap_pages (1 , &entry, true)) +return entry; +else +return (swp_entry_t ) {0 }; + } else +#endif +return __get_swap_page (); +} +#ifdef CONFIG_MEMCG static inline int mem_cgroup_swappiness (struct mem_cgroup *memcg) {
@@ -550,8 +565,10 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)#ifdef CONFIG_MEMCG_SWAP extern void mem_cgroup_swapout (struct page *page,swp_entry_t entry); -extern int mem_cgroup_try_charge_swap (struct page *page,swp_entry_t entry); -extern void mem_cgroup_uncharge_swap (swp_entry_t entry); +extern int mem_cgroup_try_charge_swap (struct page *page,swp_entry_t entry, +bool compound); +extern void mem_cgroup_uncharge_swap (swp_entry_t entry, +unsigned int nr_entries);extern long mem_cgroup_get_nr_swap_pages (struct mem_cgroup *memcg);extern bool mem_cgroup_swap_full (struct page *page);#else
@@ -560,12 +577,14 @@ static inline void mem_cgroup_swapout(struct page *page, swp_entry_t entry) }static inline int mem_cgroup_try_charge_swap (struct page *page, -swp_entry_t entry) +swp_entry_t entry, +bool compound) {return 0 ; } -static inline void mem_cgroup_uncharge_swap (swp_entry_t entry) +static inline void mem_cgroup_uncharge_swap (swp_entry_t entry, +unsigned int nr_entries) { }
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
@@ -499,6 +499,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 +8 -3
@@ -2185,7 +2185,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 */
@@ -2196,6 +2196,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) |
@@ -2258,7 +2259,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 );
@@ -2414,7 +2419,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) ret = -EBUSY ; goto out; } - extra_pins =0 ; + extra_pins =PageSwapCache (page) ?HPAGE_PMD_NR :0 ; mapping =NULL ;anon_vma_lock_write (anon_vma); }else {
mm/memcontrol.c +33 -25
129 lines not on this page · raw message
mm/shmem.c +3 -3
@@ -1290,11 +1290,11 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)SetPageUptodate (page); } - swap =get_swap_page (); + swap =get_swap_page (false);if (!swap.val) goto redirty; -if (mem_cgroup_try_charge_swap (page, swap)) +if (mem_cgroup_try_charge_swap (page, swap, false)) goto free_swap;/*
@@ -1326,7 +1326,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)mutex_unlock (&shmem_swaplist_mutex); free_swap: -swapcache_free (swap); +swapcache_free (swap, false); redirty:set_page_dirty (page);if (wbc->for_reclaim)
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_slots.c +4 -3
@@ -260,7 +260,8 @@ static int refill_swap_slots_cache(struct swap_slots_cache *cache) cache->cur =0 ;if (swap_slot_cache_active) - cache->nr =get_swap_pages (SWAP_SLOTS_CACHE_SIZE , cache->slots); + cache->nr =get_swap_pages (SWAP_SLOTS_CACHE_SIZE , cache->slots, + false);return cache->nr; }
@@ -298,7 +299,7 @@ int free_swap_slot(swp_entry_t entry)return 0 ; } -swp_entry_t get_swap_page (void ) +swp_entry_t __get_swap_page (void ) {swp_entry_t entry, *pentry;struct swap_slots_cache *cache;
@@ -334,7 +335,7 @@ swp_entry_t get_swap_page(void)return entry; } -get_swap_pages (1 , &entry); +get_swap_pages (1 , &entry, false);return entry; }
mm/swap_state.c +81 -44
233 lines not on this page · raw message
mm/swapfile.c +204 -71
377 lines not on this page · raw message
mm/vmscan.c +1 -1
@@ -708,7 +708,7 @@ static int __remove_mapping(struct address_space *mapping, struct page *page,mem_cgroup_swapout (page, swap);__delete_from_swap_cache (page);spin_unlock_irqrestore (&mapping->tree_lock, flags); -swapcache_free (swap); +swapcache_free (swap, false); }else {void (*freepage)(struct page *);void *shadow =NULL ;
Signature · 7 lines
2.11.0 -- 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>
Re: [PATCH -mm -v8 1/3] mm, THP, swap: Delay splitting THP during swap out
Hi Huang,
I reviewed this patch based on the feedback I already provided, but eventually gave up and rewrote it. Please take review feedback more seriously in the future.
Attached below is the reworked patch. Most changes are to the layering (page functions, cluster functions, range functions) so that we don't make the lowest swap range code require a notion of huge pages, or make the memcg page functions take size information that can be gathered from the page itself. I turned the config symbol into a generic THP_SWAP that can later be extended when we add 2MB IO. The rest is function naming, #ifdef removal etc.
Please review whether this is an acceptable version for you.
Thanks
---
From b90e48baa752840ba9c52f03a7a792c9b0c5b656 Mon Sep 17 00:00:00 2001
From: Huang Ying <[email protected]>
Date: Thu, 6 Apr 2017 13:35:13 +0800
Subject: [PATCH] mm, THP, swap: Delay splitting THP during swap out
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
In this patch, splitting huge page is delayed from almost the first step of swapping out to after allocating the swap space for the THP (Transparent Huge Page) and adding the THP into the swap cache. This will batch the corresponding operation, thus improve THP swap out throughput.
This is the first step for the THP swap optimization. The plan is to delay splitting the THP step by step and avoid splitting the THP finally.
The advantages of the THP swap support include:
- Batch the swap operations for the THP and 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 to improve the THP swap performance.
- The THP swap space read/write will be 2M sequential IO. It is particularly helpful for the swap read, which usually are 4k random IO. This will help to improve the THP swap performance.
- 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 the 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.
There are some concerns regarding THP swap in, mainly because possible enlarged read/write IO size (for swap in/out) may put more overhead on the storage device. To deal with that, the THP swap in should be turned on only when necessary. For example, it can be selected via "always/never/madvise" logic, to be turned on globally, turned off globally, or turned on only for VMA with MADV_HUGEPAGE, etc.
In this patch, one swap cluster is used to hold the contents of each THP swapped out. So, the size of the swap cluster is changed to that of the THP (Transparent Huge Page) on x86_64 architecture (512). For other architectures which want such THP swap optimization, ARCH_USES_THP_SWAP_CLUSTER needs to be selected in the Kconfig file for the architecture. In effect, this will enlarge swap cluster size by 2 times on x86_64. Which may make it harder to find a free cluster when the swap space becomes fragmented. So that, this may reduce the continuous swap space allocation and sequential write in theory. The performance test in 0day shows no regressions caused by this.
In the future of THP swap optimization, some information of the swapped out THP (such as compound map count) will be recorded in the swap_cluster_info data structure.
The mem cgroup swap accounting functions are enhanced to support charge or uncharge a swap cluster backing a THP as a whole.
The swap cluster allocate/free functions are added to allocate/free a swap cluster for a THP. A fair simple algorithm is used for swap cluster allocation, that is, only the first swap device in priority list will be tried to allocate the swap cluster. The function will fail if the trying is not successful, and the caller will fallback to allocate a single swap slot instead. This works good enough for normal cases. If the difference of the number of the free swap clusters among multiple swap devices is significant, it is possible that some THPs are split earlier than necessary. For example, this could be caused by big size difference among multiple swap devices.
The swap cache functions is enhanced to support add/delete THP to/from the swap cache as a set of (HPAGE_PMD_NR) sub-pages. This may be enhanced in the future with multi-order radix tree. But because we will split the THP soon during swapping out, that optimization doesn't make much sense for this first step.
The THP splitting functions are enhanced to support to split THP in swap cache during swapping out. The page lock will be held during allocating the swap cluster, adding the THP into the swap cache and splitting the THP. So in the code path other than swapping out, if the THP need to be split, the PageSwapCache(THP) will be always false.
With the patchset, the swap out throughput improves 14.9% (from about 3.77GB/s to about 4.34GB/s) in the vm-scalability swap-w-seq test case with 8 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 creates 8 processes, which sequentially allocate and write to the anonymous pages until the RAM and part of the swap device is used up.
The detailed comparison result is as follow,
base base+patchset
---------------- --------------------------
%stddev %change %stddev
\ | \
7043990 ± 0% +21.2% 8536807 ± 0% vm-scalability.throughput
109.94 ± 1% -16.2% 92.09 ± 0% vm-scalability.time.elapsed_time
3957091 ± 0% +14.9% 4547173 ± 0% vmstat.swap.so
31.46 ± 1% -38.3% 19.42 ± 0% perf-stat.cache-miss-rate%
1.04 ± 1% +22.2% 1.27 ± 0% perf-stat.ipc
9.33 ± 2% -60.7% 3.67 ± 1% perf-profile.calltrace.cycles-pp.add_to_swap.shrink_page_list.shrink_inactive_list.shrink_node_memcg.shrink_nodeThe swap cluster is only available for SSD, so the THP swap optimization in this patchset has no effect for HDD.
--- arch/x86/Kconfig | 1 + include/linux/page-flags.h | 5 +- include/linux/swap.h | 22 +++- include/linux/swap_cgroup.h | 6 +- mm/Kconfig | 12 +++ mm/huge_memory.c | 11 +- mm/memcontrol.c | 49 ++++----- mm/shmem.c | 2 +- mm/swap_cgroup.c | 40 +++++-- mm/swap_slots.c | 17 ++- mm/swap_state.c | 114 ++++++++++++-------- mm/swapfile.c | 246 +++++++++++++++++++++++++++++++------------- 12 files changed, 360 insertions(+), 165 deletions(-)
arch/x86/Kconfig +1 -0
@@ -72,6 +72,7 @@ config X86 select ARCH_WANT_BATCHED_UNMAP_TLB_FLUSH if SMP select ARCH_WANT_FRAME_POINTERS select ARCH_WANTS_DYNAMIC_TASK_STRUCT + select ARCH_WANTS_THP_SWAP if X86_64 select BUILDTIME_EXTABLE_SORT select CLKEVT_I8253 select CLOCKSOURCE_VALIDATE_LAST_CYCLE
include/linux/page-flags.h +3 -2
@@ -326,11 +326,12 @@ PAGEFLAG_FALSE(HighMem)#ifdef CONFIG_SWAP static __always_inline int PageSwapCache(struct page *page) { + page =compound_head (page);return PageSwapBacked (page) &&test_bit (PG_swapcache, &page->flags); } -SETPAGEFLAG(SwapCache, swapcache, PF_NO_COMPOUND) -CLEARPAGEFLAG(SwapCache, swapcache, PF_NO_COMPOUND) +SETPAGEFLAG(SwapCache, swapcache, PF_NO_TAIL) +CLEARPAGEFLAG(SwapCache, swapcache, PF_NO_TAIL)#else PAGEFLAG_FALSE(SwapCache)#endif
include/linux/swap.h +17 -5
@@ -29,6 +29,12 @@ struct bio; SWAP_FLAG_DISCARD_PAGES)#define SWAP_BATCH 64 +#ifdef CONFIG_THP_SWAP +#define SWAPFILE_CLUSTER HPAGE_PMD_NR +#else +#define SWAPFILE_CLUSTER 256 +#endif +static inline int current_is_kswapd (void ) {return current->flags &PF_KSWAPD ;
@@ -386,15 +392,16 @@ static inline long get_nr_swap_pages(void) }extern void si_swapinfo (struct sysinfo *); -extern swp_entry_t get_swap_page (void ); +extern swp_entry_t get_swap_page (struct page *);extern swp_entry_t get_swap_page_of_type (int ); -extern int get_swap_pages (int n,swp_entry_t swp_entries[]); +extern int get_swap_pages (int n,bool cluster,swp_entry_t swp_entries[]);extern int add_swap_count_continuation (swp_entry_t ,gfp_t );extern void swap_shmem_alloc (swp_entry_t );extern int swap_duplicate (swp_entry_t );extern int swapcache_prepare (swp_entry_t );extern void swap_free (swp_entry_t );extern void swapcache_free (swp_entry_t ); +extern void swapcache_free_cluster (swp_entry_t );extern void swapcache_free_entries (swp_entry_t *entries,int n);extern int free_swap_and_cache (swp_entry_t );extern int swap_type_of (dev_t ,sector_t ,struct block_device **);
@@ -460,6 +467,10 @@ static inline void swapcache_free(swp_entry_t swp) { } +static inline void swapcache_free_cluster (swp_entry_t swp) +{ +} +static inline struct page *swapin_readahead (swp_entry_t swp,gfp_t gfp_mask,struct vm_area_struct *vma,unsigned long addr) {
@@ -518,7 +529,7 @@ static inline int try_to_free_swap(struct page *page)return 0 ; } -static inline swp_entry_t get_swap_page (void ) +static inline swp_entry_t get_swap_page (struct page *page) {swp_entry_t entry; entry.val =0 ;
@@ -551,7 +562,7 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup *mem)#ifdef CONFIG_MEMCG_SWAP extern void mem_cgroup_swapout (struct page *page,swp_entry_t entry);extern int mem_cgroup_try_charge_swap (struct page *page,swp_entry_t entry); -extern void mem_cgroup_uncharge_swap (swp_entry_t entry); +extern void mem_cgroup_uncharge_swap (swp_entry_t entry,unsigned int nr_pages);extern long mem_cgroup_get_nr_swap_pages (struct mem_cgroup *memcg);extern bool mem_cgroup_swap_full (struct page *page);#else
@@ -565,7 +576,8 @@ static inline int mem_cgroup_try_charge_swap(struct page *page,return 0 ; } -static inline void mem_cgroup_uncharge_swap (swp_entry_t entry) +static inline void mem_cgroup_uncharge_swap (swp_entry_t entry, +unsigned int nr_pages) { }
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 +12 -0
@@ -447,6 +447,18 @@ choice benefit. endchoice +config ARCH_WANTS_THP_SWAP + def_bool n + +config THP_SWAP + def_bool y + depends on TRANSPARENT_HUGEPAGE && ARCH_WANTS_THP_SWAP + help + Swap transparent huge pages in one piece, without splitting. + XXX: For now this only does clustered swap space allocation. + + For selection by architectures with reasonable THP sizes. + config TRANSPARENT_HUGE_PAGECACHE def_bool y depends on TRANSPARENT_HUGEPAGE
mm/huge_memory.c +8 -3
@@ -2185,7 +2185,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 */
@@ -2196,6 +2196,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) |
@@ -2258,7 +2259,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 );
@@ -2414,7 +2419,7 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) ret = -EBUSY ; goto out; } - extra_pins =0 ; + extra_pins =PageSwapCache (page) ?HPAGE_PMD_NR :0 ; mapping =NULL ;anon_vma_lock_write (anon_vma); }else {
mm/memcontrol.c +25 -24
121 lines not on this page · raw message
mm/shmem.c +1 -1
@@ -1290,7 +1290,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)SetPageUptodate (page); } - swap =get_swap_page (); + swap =get_swap_page (page);if (!swap.val) goto redirty;
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_slots.c +13 -4
@@ -31,6 +31,7 @@#include <linux/cpumask.h> #include <linux/vmalloc.h> #include <linux/mutex.h> +#include <linux/mm.h> #ifdef CONFIG_SWAP
@@ -260,7 +261,8 @@ static int refill_swap_slots_cache(struct swap_slots_cache *cache) cache->cur =0 ;if (swap_slot_cache_active) - cache->nr =get_swap_pages (SWAP_SLOTS_CACHE_SIZE , cache->slots); + cache->nr =get_swap_pages (SWAP_SLOTS_CACHE_SIZE , false, + cache->slots);return cache->nr; }
@@ -298,11 +300,19 @@ int free_swap_slot(swp_entry_t entry)return 0 ; } -swp_entry_t get_swap_page (void ) +swp_entry_t get_swap_page (struct page *page) {swp_entry_t entry, *pentry;struct swap_slots_cache *cache; + entry.val =0 ; + +if (PageTransHuge (page)) { +if (hpage_nr_pages (page) ==SWAPFILE_CLUSTER ) +get_swap_pages (1 , true, &entry); +return entry; + } +/* * Preemption is allowed here, because we may sleep * in refill_swap_slots_cache(). But it is safe, because
@@ -314,7 +324,6 @@ swp_entry_t get_swap_page(void)*/ cache =raw_cpu_ptr (&swp_slots); - entry.val =0 ;if (check_cache_active ()) {mutex_lock (&cache->alloc_lock);if (cache->slots) {
@@ -334,7 +343,7 @@ swp_entry_t get_swap_page(void)return entry; } -get_swap_pages (1 , &entry); +get_swap_pages (1 , false, &entry);return entry; }
mm/swap_state.c +70 -44
207 lines not on this page · raw message
mm/swapfile.c +176 -70
342 lines not on this page · raw message
Signature · 7 lines
2.12.1 -- 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>
Re: [PATCH -mm -v8 1/3] mm, THP, swap: Delay splitting THP during swap out
Hi, Johannes,
Quoted 5 lines · Johannes Weiner
Hi Huang,
I reviewed this patch based on the feedback I already provided, but eventually gave up and rewrote it. Please take review feedback more seriously in the future.
Thanks a lot for your help! I do respect all your review and effort. The -v8 patch doesn't take all your comments, just because I thought we have not reach consensus for some points and I want to use -v8 patch to discuss them.
One concern I have before is whether to split THP firstly when swap space or memcg swap is used up. Now I think your solution is acceptable. And if we receive any regression report for that in the future, it's not very hard to deal with.
Quoted 7 lines
Attached below is the reworked patch. Most changes are to the layering (page functions, cluster functions, range functions) so that we don't make the lowest swap range code require a notion of huge pages, or make the memcg page functions take size information that can be gathered from the page itself. I turned the config symbol into a generic
THP_SWAPthat can later be extended when we add 2MB IO. The rest is function naming, #ifdef removal etc.
For some #ifdef in swapfile.c, it is to avoid unnecessary code size increase for !CONFIG_TRANSPARENT_HUGEPAGE or platform with THP swap optimization disabled. Is it an issue?
Quoted 1 line
Please review whether this is an acceptable version for you.
Yes. It is good for me. I will give it more test on next Monday.
[...]
Quoted 7 lines
mm/Kconfig +0
-0@@ -447,6 +447,18 @@ choice benefit. endchoice
mm/Kconfig choice · line 458
Quoted 10 lines
mm/Kconfig +9
-0+config ARCH_WANTS_THP_SWAP + def_bool n + +config THP_SWAP + def_bool y + depends on TRANSPARENT_HUGEPAGE && ARCH_WANTS_THP_SWAP + help + Swap transparent huge pages in one piece, without splitting. + XXX: For now this only does clustered swap space allocation.
Is 'XXX' here intended.
mm/huge_memory.c
Quoted 8 lines
mm/Kconfig +3
-0+ + For selection by architectures with reasonable THP sizes. + config TRANSPARENT_HUGE_PAGECACHE def_bool y depends on TRANSPARENT_HUGEPAGEmm/huge_memory.c +0
-0
[...]
Best Regards,
Huang, Ying
--
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>
Johannes Weiner
maintainerRe: [PATCH -mm -v8 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 15 lines · Ying
Hi, Johannes,
Quoted 5 lines · Johannes Weiner
Hi Huang,
I reviewed this patch based on the feedback I already provided, but eventually gave up and rewrote it. Please take review feedback more seriously in the future.
Thanks a lot for your help! I do respect all your review and effort. The -v8 patch doesn't take all your comments, just because I thought we have not reach consensus for some points and I want to use -v8 patch to discuss them.
Quoted 4 lines
One concern I have before is whether to split THP firstly when swap space or memcg swap is used up. Now I think your solution is acceptable. And if we receive any regression report for that in the future, it's not very hard to deal with.
If you look at get_scan_count(), we'll stop scanning anonymous pages altogether when swap space runs out. So it might happen to a few THP, but it shouldn't be a big deal.
And yes, in that case I'd really rather wait for any real problems to materialize before we complicate things.
Quoted 11 lines
Quoted 7 lines
Attached below is the reworked patch. Most changes are to the layering (page functions, cluster functions, range functions) so that we don't make the lowest swap range code require a notion of huge pages, or make the memcg page functions take size information that can be gathered from the page itself. I turned the config symbol into a generic
THP_SWAPthat can later be extended when we add 2MB IO. The rest is function naming, #ifdef removal etc.For some #ifdef in
swapfile.c, it is to avoid unnecessary code size increase for !CONFIG_TRANSPARENT_HUGEPAGE or platform with THP swap optimization disabled. Is it an issue?
It saves some code size, but it looks like the biggest cost comes from bloating PageSwapCache(). This is mm/builtin.o with !CONFIG_THP:
add/remove: 1/0 grow/shrink: 34/5 up/down: 920/-311 (609)
function old new delta __free_cluster - 106 +106 get_swap_pages 465 555 +90 migrate_page_move_mapping 1404 1479 +75 shrink_page_list 3573 3632 +59 __delete_from_swap_cache 235 293 +58 __test_set_page_writeback 626 678 +52 __swap_writepage 766 812 +46 try_to_unuse 1763 1795 +32 madvise_free_pte_range 882 912 +30 __set_page_dirty_nobuffers 245 268 +23 migrate_page_copy 565 587 +22 swap_slot_free_notify 133 151 +18 shmem_replace_page 616 633 +17 try_to_free_swap 135 151 +16 test_clear_page_writeback 512 528 +16 swap_set_page_dirty 109 125 +16 swap_readpage 384 400 +16 shmem_unuse 1535 1551 +16 reuse_swap_page 340 356 +16 page_mapping 144 160 +16 migrate_huge_page_move_mapping 483 499 +16 free_swap_and_cache 409 425 +16 free_pages_and_swap_cache 161 177 +16 free_page_and_swap_cache 145 161 +16 do_swap_page 1216 1232 +16 __remove_mapping 408 424 +16 __page_file_mapping 82 98 +16 __page_file_index 70 85 +15 try_to_unmap_one 1324 1337 +13 shmem_getpage_gfp.isra 2358 2371 +13 add_to_swap_cache 47 60 +13 inc_cluster_info_page 204 210 +6 get_swap_page 411 415 +4 shmem_writepage 922 925 +3 sys_swapon 4210 4211 +1 swapcache_free_entries 786 768 -18 __add_to_swap_cache 445 406 -39 delete_from_swap_cache 149 104 -45 scan_swap_map_slots 1953 1889 -64
swap_do_scheduled_discard 713 568 -145
Total: Before=454535, After=455144, chg +0.13%
If I make the compound_head() in there conditional, this patch actually ends up shrinking the code due to the refactoring of the cluster functions:
add/remove: 1/0 grow/shrink: 10/5 up/down: 302/-327 (-25)
function old new delta __free_cluster - 106 +106 get_swap_pages 465 555 +90 __delete_from_swap_cache 235 277 +42 shmem_replace_page 616 633 +17 migrate_page_move_mapping 1404 1418 +14 add_to_swap_cache 47 60 +13 migrate_page_copy 565 571 +6 inc_cluster_info_page 204 210 +6 get_swap_page 411 415 +4 shmem_writepage 922 925 +3 sys_swapon 4210 4211 +1 swapcache_free_entries 786 768 -18 delete_from_swap_cache 149 104 -45 __add_to_swap_cache 445 390 -55 scan_swap_map_slots 1953 1889 -64
swap_do_scheduled_discard 713 568 -145
Total: Before=454535, After=454510, chg -0.01%
But PageSwapCache() is somewhat ugly either way. Even with THP_SWAP compiled in, it seems like most callsites wouldn't test tailpages? Can we get rid of the compound_head() and annotate any callsites working on potential tail pages?
Quoted 3 lines
Quoted 1 line
Please review whether this is an acceptable version for you.
Yes. It is good for me. I will give it more test on next Monday.
Thanks
include/linux/page-flags.h +2 -0
@@ -326,7 +326,9 @@ PAGEFLAG_FALSE(HighMem)#ifdef CONFIG_SWAP static __always_inline int PageSwapCache(struct page *page) { +#ifdef CONFIG_THP_SWAP page =compound_head (page); +#endif return PageSwapBacked (page) &&test_bit (PG_swapcache, &page->flags); }
include/linux/swap.h +8 -5
@@ -401,7 +401,6 @@ extern int swap_duplicate(swp_entry_t);extern int swapcache_prepare (swp_entry_t );extern void swap_free (swp_entry_t );extern void swapcache_free (swp_entry_t ); -extern void swapcache_free_cluster (swp_entry_t );extern void swapcache_free_entries (swp_entry_t *entries,int n);extern int free_swap_and_cache (swp_entry_t );extern int swap_type_of (dev_t ,sector_t ,struct block_device **);
@@ -467,10 +466,6 @@ static inline void swapcache_free(swp_entry_t swp) { } -static inline void swapcache_free_cluster (swp_entry_t swp) -{ -} -static inline struct page *swapin_readahead (swp_entry_t swp,gfp_t gfp_mask,struct vm_area_struct *vma,unsigned long addr) {
@@ -592,5 +587,13 @@ static inline bool mem_cgroup_swap_full(struct page *page) }#endif +#ifdef CONFIG_THP_SWAP +extern void swapcache_free_cluster (swp_entry_t ); +#else +static inline void swapcache_free_cluster (swp_entry_t swp) +{ +} +#endif +#endif /* __KERNEL__*/ #endif /* _LINUX_SWAP_H */
mm/swapfile.c +10 -0
@@ -825,6 +825,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si,return n_ret; } +#ifdef CONFIG_THP_SWAP static int swap_alloc_cluster (struct swap_info_struct *si,swp_entry_t *slot) {unsigned long idx;
@@ -862,6 +863,13 @@ static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)unlock_cluster (ci); swap_range_free(si, offset, SWAPFILE_CLUSTER); } +#else +static int swap_alloc_cluster (struct swap_info_struct *si,swp_entry_t *slot) +{ +VM_WARN_ON_ONCE (1 ); +return 0 ; +} +#endif /* CONFIG_THP_SWAP */ static unsigned long scan_swap_map (struct swap_info_struct *si,unsigned char usage)
@@ -1145,6 +1153,7 @@ void swapcache_free(swp_entry_t entry) } } +#ifdef CONFIG_THP_SWAP void swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);
@@ -1170,6 +1179,7 @@ void swapcache_free_cluster(swp_entry_t entry)swap_free_cluster (si, idx); spin_unlock(&si->lock); } +#endif /* CONFIG_THP_SWAP */ void swapcache_free_entries (swp_entry_t *entries,int n) {
--
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>
Re: [PATCH -mm -v8 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 16 lines · Johannes Weiner
Quoted 15 lines · Ying
Hi, Johannes,
Quoted 5 lines · Johannes Weiner
Hi Huang,
I reviewed this patch based on the feedback I already provided, but eventually gave up and rewrote it. Please take review feedback more seriously in the future.
Thanks a lot for your help! I do respect all your review and effort. The -v8 patch doesn't take all your comments, just because I thought we have not reach consensus for some points and I want to use -v8 patch to discuss them.
Quoted 8 lines
Quoted 4 lines
One concern I have before is whether to split THP firstly when swap space or memcg swap is used up. Now I think your solution is acceptable. And if we receive any regression report for that in the future, it's not very hard to deal with.
If you look at
get_scan_count(), we'll stop scanning anonymous pages altogether when swap space runs out. So it might happen to a few THP, but it shouldn't be a big deal.
Yes. It only influences a few THP.
Quoted 83 lines
And yes, in that case I'd really rather wait for any real problems to materialize before we complicate things.
Quoted 11 lines
Quoted 7 lines
Attached below is the reworked patch. Most changes are to the layering (page functions, cluster functions, range functions) so that we don't make the lowest swap range code require a notion of huge pages, or make the memcg page functions take size information that can be gathered from the page itself. I turned the config symbol into a generic
THP_SWAPthat can later be extended when we add 2MB IO. The rest is function naming, #ifdef removal etc.For some #ifdef in
swapfile.c, it is to avoid unnecessary code size increase for !CONFIG_TRANSPARENT_HUGEPAGE or platform with THP swap optimization disabled. Is it an issue?It saves some code size, but it looks like the biggest cost comes from bloating
PageSwapCache(). This ismm/builtin.owith !CONFIG_THP:add/remove: 1/0 grow/shrink: 34/5 up/down: 920/-311 (609)
function old new delta __free_cluster - 106 +106 get_swap_pages 465 555 +90 migrate_page_move_mapping 1404 1479 +75 shrink_page_list 3573 3632 +59 __delete_from_swap_cache 235 293 +58 __test_set_page_writeback 626 678 +52 __swap_writepage 766 812 +46 try_to_unuse 1763 1795 +32 madvise_free_pte_range 882 912 +30 __set_page_dirty_nobuffers 245 268 +23 migrate_page_copy 565 587 +22 swap_slot_free_notify 133 151 +18 shmem_replace_page 616 633 +17 try_to_free_swap 135 151 +16 test_clear_page_writeback 512 528 +16 swap_set_page_dirty 109 125 +16 swap_readpage 384 400 +16 shmem_unuse 1535 1551 +16 reuse_swap_page 340 356 +16 page_mapping 144 160 +16 migrate_huge_page_move_mapping 483 499 +16 free_swap_and_cache 409 425 +16 free_pages_and_swap_cache 161 177 +16 free_page_and_swap_cache 145 161 +16 do_swap_page 1216 1232 +16 __remove_mapping 408 424 +16 __page_file_mapping 82 98 +16 __page_file_index 70 85 +15 try_to_unmap_one 1324 1337 +13 shmem_getpage_gfp.isra 2358 2371 +13 add_to_swap_cache 47 60 +13 inc_cluster_info_page 204 210 +6 get_swap_page 411 415 +4 shmem_writepage 922 925 +3 sys_swapon 4210 4211 +1 swapcache_free_entries 786 768 -18 __add_to_swap_cache 445 406 -39 delete_from_swap_cache 149 104 -45 scan_swap_map_slots 1953 1889 -64
swap_do_scheduled_discard713 568 -145
Total: Before=454535, After=455144, chg +0.13%If I make the
compound_head()in there conditional, this patch actually ends up shrinking the code due to the refactoring of the cluster functions:add/remove: 1/0 grow/shrink: 10/5 up/down: 302/-327 (-25)
function old new delta __free_cluster - 106 +106 get_swap_pages 465 555 +90 __delete_from_swap_cache 235 277 +42 shmem_replace_page 616 633 +17 migrate_page_move_mapping 1404 1418 +14 add_to_swap_cache 47 60 +13 migrate_page_copy 565 571 +6 inc_cluster_info_page 204 210 +6 get_swap_page 411 415 +4 shmem_writepage 922 925 +3 sys_swapon 4210 4211 +1 swapcache_free_entries 786 768 -18 delete_from_swap_cache 149 104 -45 __add_to_swap_cache 445 390 -55 scan_swap_map_slots 1953 1889 -64
Quoted 2 lines
swap_do_scheduled_discard713 568 -145
Total: Before=454535, After=454510, chg -0.01%
This looks great! Thanks!
Quoted 4 lines
But
PageSwapCache()is somewhat ugly either way. Even withTHP_SWAPcompiled in, it seems like most callsites wouldn't test tailpages? Can we get rid of thecompound_head()and annotate any callsites working on potential tail pages?
I think we can keep the current #ifdef version and make some cleanup in the next step?
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
Please review whether this is an acceptable version for you.
Yes. It is good for me. I will give it more test on next Monday.
Thanks
I think we will fold the below patch into the original one?
Best Regards,
Huang, Ying
Quoted 93 lines
include/linux/page-flags.h +2
-0@@ -326,7 +326,9 @@ PAGEFLAG_FALSE(HighMem)#ifdef CONFIG_SWAP static __always_inline int PageSwapCache(struct page *page) { +#ifdef CONFIG_THP_SWAP page =compound_head (page); +#endif return PageSwapBacked (page) &&test_bit (PG_swapcache, &page->flags); }include/linux/swap.h +8
-5@@ -401,7 +401,6 @@ extern int swap_duplicate(swp_entry_t);extern int swapcache_prepare (swp_entry_t );extern void swap_free (swp_entry_t );extern void swapcache_free (swp_entry_t ); -extern void swapcache_free_cluster (swp_entry_t );extern void swapcache_free_entries (swp_entry_t *entries,int n);extern int free_swap_and_cache (swp_entry_t );extern int swap_type_of (dev_t ,sector_t ,struct block_device **);@@ -467,10 +466,6 @@ static inline void swapcache_free(swp_entry_t swp) { } -static inline void swapcache_free_cluster (swp_entry_t swp) -{ -} -static inline struct page *swapin_readahead (swp_entry_t swp,gfp_t gfp_mask,struct vm_area_struct *vma,unsigned long addr) {@@ -592,5 +587,13 @@ static inline bool mem_cgroup_swap_full(struct page *page) }#endif +#ifdef CONFIG_THP_SWAP +extern void swapcache_free_cluster (swp_entry_t ); +#else +static inline void swapcache_free_cluster (swp_entry_t swp) +{ +} +#endif +#endif /* __KERNEL__*/ #endif /* _LINUX_SWAP_H */ mm/swapfile.c +10
-0@@ -825,6 +825,7 @@ static int scan_swap_map_slots(struct swap_info_struct *si,return n_ret; } +#ifdef CONFIG_THP_SWAP static int swap_alloc_cluster (struct swap_info_struct *si,swp_entry_t *slot) {unsigned long idx;@@ -862,6 +863,13 @@ static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)unlock_cluster (ci); swap_range_free(si, offset, SWAPFILE_CLUSTER); } +#else +static int swap_alloc_cluster (struct swap_info_struct *si,swp_entry_t *slot) +{ +VM_WARN_ON_ONCE (1 ); +return 0 ; +} +#endif /* CONFIG_THP_SWAP */ static unsigned long scan_swap_map (struct swap_info_struct *si,unsigned char usage)@@ -1145,6 +1153,7 @@ void swapcache_free(swp_entry_t entry) } } +#ifdef CONFIG_THP_SWAP void swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);@@ -1170,6 +1179,7 @@ void swapcache_free_cluster(swp_entry_t entry)swap_free_cluster (si, idx); spin_unlock(&si->lock); } +#endif /* CONFIG_THP_SWAP */ void swapcache_free_entries (swp_entry_t *entries,int n) {
PATCH 2/3 mm, THP, swap: Check whether THP can be split firstly
In reply to Huang, YingFrom: Huang Ying <[email protected]>
In the original THP swapping out implementation, before splitting the THP (Transparent Huage Page), the swap cluster will be allocated and the THP will be added into the swap cache. But it is possible that the THP cannot be split, and we must delete the THP from the swap cache and free the swap cluster. To avoid that, in this patch, whether the THP can be split is checked firstly. The check can only be done racy, but it is good enough for most cases.
--- include/linux/huge_mm.h | 7 +++++++ mm/huge_memory.c | 20 ++++++++++++++++---- mm/swap_state.c | 7 ++++++- 3 files changed, 29 insertions(+), 5 deletions(-)
include/linux/huge_mm.h +7 -0
@@ -113,6 +113,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 *pextra_pins);int split_huge_page_to_list (struct page *page,struct list_head *list);static inline int split_huge_page (struct page *page) {
@@ -231,6 +232,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,int *pextra_pins) +{ +BUILD_BUG (); +return false; +}static inline int split_huge_page_to_list (struct page *page,struct list_head *list) {
mm/huge_memory.c +16 -4
@@ -2372,6 +2372,21 @@ 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 *pextra_pins) +{ +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 ; +if (pextra_pins) + *pextra_pins = extra_pins; +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.
@@ -2419,7 +2434,6 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) ret = -EBUSY ; goto out; } - extra_pins =PageSwapCache (page) ?HPAGE_PMD_NR :0 ; mapping =NULL ;anon_vma_lock_write (anon_vma); }else {
@@ -2431,8 +2445,6 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) goto out; } -/* Addidional pins from radix tree */ - extra_pins =HPAGE_PMD_NR ; anon_vma =NULL ;i_mmap_lock_read (mapping); }
@@ -2441,7 +2453,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, &extra_pins)) { ret = -EBUSY ; goto out_unlock; }
mm/swap_state.c +6 -1
@@ -201,7 +201,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageUptodate (page), page);#ifdef CONFIG_THP_SWAP_CLUSTER - huge =PageTransHuge (page); +if (unlikely (PageTransHuge (page))) { +/* cannot split, skip it */ +if (!can_split_huge_page (page,NULL )) +return 0 ; + huge = true; + }#endif retry:
Signature · 7 lines
2.11.0 -- 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>
PATCH 3/3 mm, THP, swap: Enable THP swap optimization only if has compound map
In reply to Huang, YingFrom: Huang Ying <[email protected]>
If there is no compound map for a THP (Transparent Huge Page), it is possible that the map count of some sub-pages of the THP is 0. So it is better to split the THP before swapping out. In this way, the sub-pages not mapped will be freed, and we can avoid the unnecessary swap out operations for these sub-pages.
--- mm/swap_state.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
mm/swap_state.c +3 -1
@@ -205,7 +205,9 @@ int add_to_swap(struct page *page, struct list_head *list)/* cannot split, skip it */ if (!can_split_huge_page (page,NULL ))return 0 ; - huge = true; +/* fallback to split huge page firstly if no PMD map */ +if (compound_mapcount (page)) + huge = true; }#endif
Signature · 7 lines
2.11.0 -- 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>