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.
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/13 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 15.5% (from about 3.73GB/s to about 4.31GB/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.
Changelog:
v10:
- Remove unlikely() in add_to_swap() per Johannes' comments
- Improve code comments in add_to_swap() and mem_cgroup_try_charge_swap()
v9:
- Rebased on latest -mm tree
- Johannes done extensive cleanups and simplifications
- Johannes resolved the code size increases
- Update the test results
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.
The swap cluster is only available for SSD, so the THP swap optimization in this patchset has no effect for HDD.
With the patch, the swap out throughput improves 11.5% (from about 3.73GB/s to about 4.16GB/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.
--- arch/x86/Kconfig | 1 + include/linux/page-flags.h | 7 +- include/linux/swap.h | 25 ++++- include/linux/swap_cgroup.h | 6 +- mm/Kconfig | 12 +++ mm/huge_memory.c | 11 +- mm/memcontrol.c | 50 ++++----- mm/shmem.c | 2 +- mm/swap_cgroup.c | 40 +++++-- mm/swap_slots.c | 16 ++- mm/swap_state.c | 114 ++++++++++++-------- mm/swapfile.c | 256 ++++++++++++++++++++++++++++++++------------ 12 files changed, 375 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 +5 -2
@@ -326,11 +326,14 @@ 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); } -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 +20 -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,9 +392,9 @@ 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 *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 );
@@ -515,7 +521,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 ;
@@ -548,7 +554,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
@@ -562,7 +568,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) { }
@@ -577,5 +584,13 @@ static inline bool mem_cgroup_swap_full(struct page *page) }#endif +#ifdef CONFIG_THP_SWAP +extern void swapcache_free_cluster (swp_entry_t entry); +#else +static inline void swapcache_free_cluster (swp_entry_t entry) +{ +} +#endif +#endif /* __KERNEL__*/ #endif /* _LINUX_SWAP_H */
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
@@ -2197,7 +2197,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 */
@@ -2208,6 +2208,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) |
@@ -2270,7 +2271,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 );
@@ -2426,7 +2431,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 +26 -24
122 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 +12 -4
@@ -263,7 +263,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; }
@@ -301,11 +302,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
@@ -317,7 +326,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) {
@@ -337,7 +345,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 +186 -70
352 lines not on this page · raw message
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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 42 lines · Ying
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.
Quoted 10 lines
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_64architecture (512). For other architectures which want such THP swap optimization,ARCH_USES_THP_SWAP_CLUSTERneeds to be selected in theKconfigfile for the architecture. In effect, this will enlarge swap cluster size by 2 times onx86_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.
What about other architecures?
I mean THP page size on every architectures would be various. If THP page size is much bigger than 2M, the architecture should have big swap cluster size for supporting THP swap-out feature. It means fast empty-swap cluster consumption so that it can suffer from fragmentation easily which causes THP swap void and swap slot allocations slow due to not being able to use per-cpu.
What I suggested was contiguous multiple swap cluster allocations to meet THP page size. If some of architecure's THP size is 64M and SWAP_CLUSTER_SIZE is 2M, it should allocate 32 contiguos swap clusters. For that, swap layer need to manage clusters sort in order which would be more overhead in CONFIG_THP_SWAP case but I think it's tradeoff. With that, every architectures can support THP swap easily without arch-specific something.
If (PAGE_SIZE * 512) swap cluster size were okay for most of architecture, just increase it. It's orthogonal work regardless of THP swapout. Then, we don't need to manage swap clusters sort in order in x86_64 which SWAP_CLUSTER_SIZE is equal to THP_PAGE_SIZE. It's just a bonus by side-effect.
AFAIR, I suggested it but cannot remember why we cannot go with this way.
Quoted 71 lines
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_infodata 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.
The swap cluster is only available for SSD, so the THP swap optimization in this patchset has no effect for HDD.
With the patch, the swap out throughput improves 11.5% (from about 3.73GB/s to about 4.16GB/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.
[[email protected]: extensive cleanups and simplifications, reduce code size]Signed-off-by "Huang, Ying" <[email protected]>Cc Andrea Arcangeli <[email protected]>Cc Ebru Akagunduz <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Tejun Heo <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>Signed-off-by Johannes Weiner <[email protected]>--- arch/x86/Kconfig | 1 + include/linux/page-flags.h | 7 +- include/linux/swap.h | 25 ++++- include/linux/swap_cgroup.h | 6 +- mm/Kconfig | 12 +++ mm/huge_memory.c | 11 +- mm/memcontrol.c | 50 ++++----- mm/shmem.c | 2 +- mm/swap_cgroup.c | 40 +++++-- mm/swap_slots.c | 16 ++- mm/swap_state.c | 114 ++++++++++++-------- mm/swapfile.c | 256 ++++++++++++++++++++++++++++++++------------ 12 files changed, 375 insertions(+), 165 deletions(-)
< snip >
mm/shmem.c static int shmem_writepage(struct page *page, struct writeback_control *wbc) · line 1296
Quoted 11 lines
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;
If swap is non-ssd, swap.val could be zero. Right? If so, could we retry like anonymous page swapout?
Quoted 15 lines
mm/shmem.c +9
-1-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; + } +
< snip >
Quoted 12 lines
/**
mm/shmem.c +2
-4@@ -178,20 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); - entry =get_swap_page (); +retry: + entry =get_swap_page (page);if (!entry.val) -return 0 ; - -if (mem_cgroup_try_charge_swap (page, entry)) {
mm/shmem.c int add_to_swap(struct page *page, struct list_head *list) · line 198
Quoted 10 lines
mm/shmem.c +1
-9-swapcache_free (entry); -return 0 ; - } - -if (unlikely (PageTransHuge (page))) -if (unlikely (split_huge_page_to_list (page, list))) { -swapcache_free (entry); -return 0 ; - } + goto fail;
So, with non-SSD swap, THP page *always* get the fail to get swp_entry_t and retry after split the page. However, it makes unncessary get_swap_pages call which is not trivial. If there is no SSD swap, thp-swap out should be void without adding any performance overhead.
Hmm, but I have no good idea to do it simple. :(
Re: [PATCH -mm -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 63 lines · Minchan Kim
Quoted 42 lines · Ying
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.Quoted 10 lines
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_64architecture (512). For other architectures which want such THP swap optimization,ARCH_USES_THP_SWAP_CLUSTERneeds to be selected in theKconfigfile for the architecture. In effect, this will enlarge swap cluster size by 2 times onx86_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.What about other architecures?
I mean THP page size on every architectures would be various. If THP page size is much bigger than 2M, the architecture should have big swap cluster size for supporting THP swap-out feature. It means fast empty-swap cluster consumption so that it can suffer from fragmentation easily which causes THP swap void and swap slot allocations slow due to not being able to use per-cpu.
Quoted 7 lines
What I suggested was contiguous multiple swap cluster allocations to meet THP page size. If some of architecure's THP size is 64M and
SWAP_CLUSTER_SIZEis 2M, it should allocate 32 contiguos swap clusters. For that, swap layer need to manage clusters sort in order which would be more overhead inCONFIG_THP_SWAPcase but I think it's tradeoff. With that, every architectures can support THP swap easily without arch-specific something.
That may be a good solution for other architectures. But I am afraid I am not the right person to work on that. Because I don't know the requirement of other architectures, and I have no other architectures machines to work on and measure the performance.
And the swap clusters aren't sorted in order now intentionally to avoid cache line false sharing between the spinlock of struct swap_cluster_info. If we want to sort clusters in order, we need a solution for that.
Quoted 5 lines
If (
PAGE_SIZE* 512) swap cluster size were okay for most of architecture, just increase it. It's orthogonal work regardless of THP swapout. Then, we don't need to manage swap clusters sort in order inx86_64whichSWAP_CLUSTER_SIZEis equal toTHP_PAGE_SIZE. It's just a bonus by side-effect.
Andrew suggested to make swap cluster size = huge page size (or turn on THP swap optimization) only if we enabled CONFIG_THP_SWAP. So that, THP swap optimization will not be turned on unintentionally.
We may adjust default swap cluster size, but I don't think it need to be in this patchset.
Quoted 83 lines
AFAIR, I suggested it but cannot remember why we cannot go with this way.
Quoted 71 lines
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_infodata 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.
The swap cluster is only available for SSD, so the THP swap optimization in this patchset has no effect for HDD.
With the patch, the swap out throughput improves 11.5% (from about 3.73GB/s to about 4.16GB/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.
[[email protected]: extensive cleanups and simplifications, reduce code size]Signed-off-by "Huang, Ying" <[email protected]>Cc Andrea Arcangeli <[email protected]>Cc Ebru Akagunduz <[email protected]>Cc Johannes Weiner <[email protected]>Cc Michal Hocko <[email protected]>Cc Tejun Heo <[email protected]>Cc Hugh Dickins <[email protected]>Cc Shaohua Li <[email protected]>Cc Minchan Kim <[email protected]>Cc Rik van Riel <[email protected]>Signed-off-by Johannes Weiner <[email protected]>--- arch/x86/Kconfig | 1 + include/linux/page-flags.h | 7 +- include/linux/swap.h | 25 ++++- include/linux/swap_cgroup.h | 6 +- mm/Kconfig | 12 +++ mm/huge_memory.c | 11 +- mm/memcontrol.c | 50 ++++----- mm/shmem.c | 2 +- mm/swap_cgroup.c | 40 +++++-- mm/swap_slots.c | 16 ++- mm/swap_state.c | 114 ++++++++++++-------- mm/swapfile.c | 256 ++++++++++++++++++++++++++++++++------------ 12 files changed, 375 insertions(+), 165 deletions(-)< snip >
Quoted 6 lines
mm/shmem.c +0
-0@@ -1290,7 +1290,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)SetPageUptodate (page); }
Quoted 8 lines
mm/shmem.c static int shmem_writepage(struct page *page, struct writeback_control *wbc) · line 1296
Quoted 5 lines
mm/shmem.c +1
-1- swap =get_swap_page (); + swap =get_swap_page (page);if (!swap.val) goto redirty;If swap is non-ssd, swap.val could be zero. Right? If so, could we retry like anonymous page swapout?
This is for shmem, where the THP will be split before goes here. That is, "page" here is always normal page.
Quoted 39 lines
Quoted 15 lines
mm/shmem.c +9
-1-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; + } +< snip >
Quoted 20 lines
/**
mm/shmem.c +2
-12@@ -178,20 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); - entry =get_swap_page (); +retry: + entry =get_swap_page (page);if (!entry.val) -return 0 ; - -if (mem_cgroup_try_charge_swap (page, entry)) { -swapcache_free (entry); -return 0 ; - } - -if (unlikely (PageTransHuge (page))) -if (unlikely (split_huge_page_to_list (page, list))) { -swapcache_free (entry); -return 0 ;
Quoted 8 lines
mm/shmem.c int add_to_swap(struct page *page, struct list_head *list) · line 198
Quoted 2 lines
mm/shmem.c +1
-1- } + goto fail;So, with non-SSD swap, THP page *always* get the fail to get
swp_entry_tand retry after split the page. However, it makes unncessaryget_swap_pagescall which is not trivial. If there is no SSD swap, thp-swap out should be void without adding any performance overhead.
Hmm, but I have no good idea to do it simple. :(
For HDD swap, the device raw throughput is so low (< 100M Bps typically), that the added overhead here will not be a big issue. Do you agree?
Best Regards,
Huang, Ying
Johannes Weiner
maintainerRe: [PATCH -mm -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 30 lines · Ying
Quoted 20 lines · Minchan Kim
Quoted 19 lines · Ying
mm/swap_state.c +2
-12@@ -178,20 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); - entry =get_swap_page (); +retry: + entry =get_swap_page (page);if (!entry.val) -return 0 ; - -if (mem_cgroup_try_charge_swap (page, entry)) { -swapcache_free (entry); -return 0 ; - } - -if (unlikely (PageTransHuge (page))) -if (unlikely (split_huge_page_to_list (page, list))) { -swapcache_free (entry); -return 0 ;Quoted 8 lines
Review int add_to_swap(struct page *page, struct list_head *list) · line 198
Quoted 2 lines
(quoted diff) +1
-1- } + goto fail;So, with non-SSD swap, THP page *always* get the fail to get
swp_entry_tand retry after split the page. However, it makes unncessaryget_swap_pagescall which is not trivial. If there is no SSD swap, thp-swap out should be void without adding any performance overhead.
Hmm, but I have no good idea to do it simple. :(
Quoted 3 lines
For HDD swap, the device raw throughput is so low (< 100M Bps typically), that the added overhead here will not be a big issue. Do you agree?
I fully agree. If you swap to spinning rust, an extra function call here is the least of your concern.
Re: [PATCH -mm -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
In reply to Huang, YingQuoted 73 lines · Ying
Quoted 63 lines · Minchan Kim
Quoted 42 lines · Ying
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.Quoted 10 lines
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_64architecture (512). For other architectures which want such THP swap optimization,ARCH_USES_THP_SWAP_CLUSTERneeds to be selected in theKconfigfile for the architecture. In effect, this will enlarge swap cluster size by 2 times onx86_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.What about other architecures?
I mean THP page size on every architectures would be various. If THP page size is much bigger than 2M, the architecture should have big swap cluster size for supporting THP swap-out feature. It means fast empty-swap cluster consumption so that it can suffer from fragmentation easily which causes THP swap void and swap slot allocations slow due to not being able to use per-cpu.
Quoted 7 lines
What I suggested was contiguous multiple swap cluster allocations to meet THP page size. If some of architecure's THP size is 64M and
SWAP_CLUSTER_SIZEis 2M, it should allocate 32 contiguos swap clusters. For that, swap layer need to manage clusters sort in order which would be more overhead inCONFIG_THP_SWAPcase but I think it's tradeoff. With that, every architectures can support THP swap easily without arch-specific something.
Quoted 4 lines
That may be a good solution for other architectures. But I am afraid I am not the right person to work on that. Because I don't know the requirement of other architectures, and I have no other architectures machines to work on and measure the performance.
IMO, THP swapout is good thing for every architecture so I dobut you need to know other architecture's requirement.
Quoted 5 lines
And the swap clusters aren't sorted in order now intentionally to avoid cache line false sharing between the spinlock of struct
swap_cluster_info. If we want to sort clusters in order, we need a solution for that.
Does it really matter for this work? IOW, if we couldn't solve it, cannot we support THP swapout? I don't think so. That's the least of your worries.
Also, if we have sorted cluster data structure, we need to change current single linked list of swap cluster to other one so we would need to revisit to see whether it's really problem.
Quoted 7 lines
Quoted 6 lines
If (
PAGE_SIZE* 512) swap cluster size were okay for most of architecture, just increase it. It's orthogonal work regardless of THP swapout. Then, we don't need to manage swap clusters sort in order inx86_64whichSWAP_CLUSTER_SIZEis equal toTHP_PAGE_SIZE. It's just a bonus by side-effect.
Quoted 6 lines
Andrew suggested to make swap cluster size = huge page size (or turn on THP swap optimization) only if we enabled
CONFIG_THP_SWAP. So that, THP swap optimization will not be turned on unintentionally.We may adjust default swap cluster size, but I don't think it need to be in this patchset.
That's it. This feature shouldn't be aware of swap cluster size. IOW, it would be better to work with every swap cluster size if the align between THP and swap cluster size is matched at least.
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
mm/shmem.c +0
-0@@ -1290,7 +1290,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)SetPageUptodate (page); }Quoted 8 lines
mm/shmem.c static int shmem_writepage(struct page *page, struct writeback_control *wbc) · line 1296
Quoted 5 lines
mm/shmem.c +1
-1- swap =get_swap_page (); + swap =get_swap_page (page);if (!swap.val) goto redirty;If swap is non-ssd, swap.val could be zero. Right? If so, could we retry like anonymous page swapout?
Quoted 2 lines
This is for shmem, where the THP will be split before goes here. That is, "page" here is always normal page.
Thanks. I missed it.
However, get_swap_page is ugly now. The caller should take care of failure and should retry after split. I hope get_swap_page includes split and retry logic in itself without reling on the caller.
include/linux/swap.h +5 -4
@@ -392,7 +392,7 @@ static inline long get_nr_swap_pages(void) }extern void si_swapinfo (struct sysinfo *); -extern swp_entry_t get_swap_page (struct page *page); +extern swp_entry_t get_swap_page (struct page *page,struct list_head *list);extern swp_entry_t get_swap_page_of_type (int );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 );
@@ -400,7 +400,7 @@ 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 (struct page *page,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 **);
@@ -459,7 +459,7 @@ static inline void swap_free(swp_entry_t swp) { } -static inline void swapcache_free (swp_entry_t swp) +static inline void swapcache_free (struct page *page,swp_entry_t swp) { }
@@ -521,7 +521,8 @@ static inline int try_to_free_swap(struct page *page)return 0 ; } -static inline swp_entry_t get_swap_page (struct page *page) +static inline swp_entry_t get_swap_page (struct page *page, +struct list_head *list) {swp_entry_t entry; entry.val =0 ;
mm/shmem.c +2 -2
@@ -1290,7 +1290,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)SetPageUptodate (page); } - swap =get_swap_page (page); + swap =get_swap_page (page,NULL );if (!swap.val) goto redirty;
@@ -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 (page, swap); redirty:set_page_dirty (page);if (wbc->for_reclaim)
mm/swap_slots.c +10 -2
@@ -302,7 +302,7 @@ int free_swap_slot(swp_entry_t entry)return 0 ; } -swp_entry_t get_swap_page (struct page *page) +swp_entry_t get_swap_page (struct page *page,struct list_head *list) {swp_entry_t entry, *pentry;struct swap_slots_cache *cache;
@@ -312,7 +312,15 @@ swp_entry_t get_swap_page(struct page *page)if (PageTransHuge (page)) {if (hpage_nr_pages (page) ==SWAPFILE_CLUSTER )get_swap_pages (1 , true, &entry); -return entry; +if (entry.val !=0 ) +return entry; +/* +* If swap device is not a SSD or cannot find +* a empty cluster, split the page and fall back +* to swap slot allocation. +*/ +if (split_huge_page_to_list (page, list)) +return entry; }/*
mm/swap_state.c +8 -18
@@ -192,12 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); -retry: - entry =get_swap_page (page); + entry =get_swap_page (page, list);if (!entry.val) - goto fail; +return 0 ; +if (mem_cgroup_try_charge_swap (page, entry)) - goto fail_free; + goto fail;/* * Radix-tree node allocations from PF_MEMALLOC contexts could
@@ -218,7 +218,7 @@ int add_to_swap(struct page *page, struct list_head *list)* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ - goto fail_free; + goto fail;if (PageTransHuge (page)) { err =split_huge_page_to_list (page, list);
@@ -230,14 +230,8 @@ int add_to_swap(struct page *page, struct list_head *list)return 1 ; -fail_free: -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); fail: -if (PageTransHuge (page) && !split_huge_page_to_list (page, list)) - goto retry; +swapcache_free (page, entry);return 0 ; }
@@ -259,11 +253,7 @@ void delete_from_swap_cache(struct page *page)__delete_from_swap_cache (page);spin_unlock_irq (&address_space->tree_lock); -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); - +swapcache_free (page, entry);page_ref_sub (page,hpage_nr_pages (page)); }
@@ -415,7 +405,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ -swapcache_free (entry); +swapcache_free (new_page, entry); }while (err != -ENOMEM );if (new_page)
mm/swapfile.c +10 -2
@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;
@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;
@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ +void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +} +static int swp_entry_cmp (const void *ent1,const void *ent2) {const swp_entry_t *e1 = ent1, *e2 = ent2;
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 (page, swap); }else {void (*freepage)(struct page *);void *shadow =NULL ;
Quoted 49 lines
Quoted 40 lines
Quoted 16 lines
mm/shmem.c +9
-1-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; + } +< snip >
Quoted 20 lines
/**
mm/shmem.c +2
-12@@ -178,20 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); - entry =get_swap_page (); +retry: + entry =get_swap_page (page);if (!entry.val) -return 0 ; - -if (mem_cgroup_try_charge_swap (page, entry)) { -swapcache_free (entry); -return 0 ; - } - -if (unlikely (PageTransHuge (page))) -if (unlikely (split_huge_page_to_list (page, list))) { -swapcache_free (entry); -return 0 ;Quoted 8 lines
mm/shmem.c int add_to_swap(struct page *page, struct list_head *list) · line 198
Quoted 2 lines
mm/shmem.c +1
-1- } + goto fail;So, with non-SSD swap, THP page *always* get the fail to get
swp_entry_tand retry after split the page. However, it makes unncessaryget_swap_pagescall which is not trivial. If there is no SSD swap, thp-swap out should be void without adding any performance overhead.
Hmm, but I have no good idea to do it simple. :(
Quoted 3 lines
For HDD swap, the device raw throughput is so low (< 100M Bps typically), that the added overhead here will not be a big issue. Do you agree?
I agree. Actually, I wanted to remove the pointless overhead if we have a enough *simple* solution. However, as I said, I have no idea so just raised an issue wit hope that someone might have an idea.
Frankly speaking, I think we should support THP swap with hdd as well as ssd but it's limited to just *implementation* direction which is really unfortunate. :(
--
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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 106 lines · Minchan Kim
Quoted 73 lines · Ying
Quoted 63 lines · Minchan Kim
Quoted 42 lines · Ying
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.Quoted 10 lines
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_64architecture (512). For other architectures which want such THP swap optimization,ARCH_USES_THP_SWAP_CLUSTERneeds to be selected in theKconfigfile for the architecture. In effect, this will enlarge swap cluster size by 2 times onx86_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.What about other architecures?
I mean THP page size on every architectures would be various. If THP page size is much bigger than 2M, the architecture should have big swap cluster size for supporting THP swap-out feature. It means fast empty-swap cluster consumption so that it can suffer from fragmentation easily which causes THP swap void and swap slot allocations slow due to not being able to use per-cpu.
Quoted 7 lines
What I suggested was contiguous multiple swap cluster allocations to meet THP page size. If some of architecure's THP size is 64M and
SWAP_CLUSTER_SIZEis 2M, it should allocate 32 contiguos swap clusters. For that, swap layer need to manage clusters sort in order which would be more overhead inCONFIG_THP_SWAPcase but I think it's tradeoff. With that, every architectures can support THP swap easily without arch-specific something.Quoted 4 lines
That may be a good solution for other architectures. But I am afraid I am not the right person to work on that. Because I don't know the requirement of other architectures, and I have no other architectures machines to work on and measure the performance.
IMO, THP swapout is good thing for every architecture so I dobut you need to know other architecture's requirement.
Quoted 5 lines
And the swap clusters aren't sorted in order now intentionally to avoid cache line false sharing between the spinlock of struct
swap_cluster_info. If we want to sort clusters in order, we need a solution for that.Does it really matter for this work? IOW, if we couldn't solve it, cannot we support THP swapout? I don't think so. That's the least of your worries.
Also, if we have sorted cluster data structure, we need to change current single linked list of swap cluster to other one so we would need to revisit to see whether it's really problem.Quoted 11 lines
Quoted 6 lines
If (
PAGE_SIZE* 512) swap cluster size were okay for most of architecture, just increase it. It's orthogonal work regardless of THP swapout. Then, we don't need to manage swap clusters sort in order inx86_64whichSWAP_CLUSTER_SIZEis equal toTHP_PAGE_SIZE. It's just a bonus by side-effect.Andrew suggested to make swap cluster size = huge page size (or turn on THP swap optimization) only if we enabled
CONFIG_THP_SWAP. So that, THP swap optimization will not be turned on unintentionally.
Quoted 6 lines
Quoted 2 lines
We may adjust default swap cluster size, but I don't think it need to be in this patchset.
That's it. This feature shouldn't be aware of swap cluster size. IOW, it would be better to work with every swap cluster size if the align between THP and swap cluster size is matched at least.
Using one swap cluster for each THP is simpler, so why not start from the simple design? Complex design may be necessary in the future, but we can work on that at that time.
Quoted 15 lines
Quoted 15 lines
Quoted 14 lines
mm/shmem.c static int shmem_writepage(struct page *page, struct writeback_control *wbc) · line 1296
Quoted 11 lines
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;If swap is non-ssd, swap.val could be zero. Right? If so, could we retry like anonymous page swapout?
Quoted 8 lines
Quoted 2 lines
This is for shmem, where the THP will be split before goes here. That is, "page" here is always normal page.
Thanks. I missed it.
However,
get_swap_pageis ugly now. The caller should take care of failure and should retry after split. I hopeget_swap_pageincludes split and retry logic in itself without reling on the caller.
The current interface of get_swap_page() and swapcache_free() is proposed by Johannes.
Hi, Johannes, what do you think about Minchan's interface proposal?
Best Regards,
Huang, Ying
Quoted 264 lines
include/linux/swap.h +5
-4@@ -392,7 +392,7 @@ static inline long get_nr_swap_pages(void) }extern void si_swapinfo (struct sysinfo *); -extern swp_entry_t get_swap_page (struct page *page); +extern swp_entry_t get_swap_page (struct page *page,struct list_head *list);extern swp_entry_t get_swap_page_of_type (int );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 );@@ -400,7 +400,7 @@ 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 (struct page *page,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 **);@@ -459,7 +459,7 @@ static inline void swap_free(swp_entry_t swp) { } -static inline void swapcache_free (swp_entry_t swp) +static inline void swapcache_free (struct page *page,swp_entry_t swp) { }@@ -521,7 +521,8 @@ static inline int try_to_free_swap(struct page *page)return 0 ; } -static inline swp_entry_t get_swap_page (struct page *page) +static inline swp_entry_t get_swap_page (struct page *page, +struct list_head *list) {swp_entry_t entry; entry.val =0 ;mm/shmem.c +2
-2@@ -1290,7 +1290,7 @@ static int shmem_writepage(struct page *page, struct writeback_control *wbc)SetPageUptodate (page); } - swap =get_swap_page (page); + swap =get_swap_page (page,NULL );if (!swap.val) goto redirty;@@ -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 (page, swap); redirty:set_page_dirty (page);if (wbc->for_reclaim)mm/swap_slots.c +10
-2@@ -302,7 +302,7 @@ int free_swap_slot(swp_entry_t entry)return 0 ; } -swp_entry_t get_swap_page (struct page *page) +swp_entry_t get_swap_page (struct page *page,struct list_head *list) {swp_entry_t entry, *pentry;struct swap_slots_cache *cache;@@ -312,7 +312,15 @@ swp_entry_t get_swap_page(struct page *page)if (PageTransHuge (page)) {if (hpage_nr_pages (page) ==SWAPFILE_CLUSTER )get_swap_pages (1 , true, &entry); -return entry; +if (entry.val !=0 ) +return entry; +/* +* If swap device is not a SSD or cannot find +* a empty cluster, split the page and fall back +* to swap slot allocation. +*/ +if (split_huge_page_to_list (page, list)) +return entry; }/* mm/swap_state.c +8
-18@@ -192,12 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); -retry: - entry =get_swap_page (page); + entry =get_swap_page (page, list);if (!entry.val) - goto fail; +return 0 ; +if (mem_cgroup_try_charge_swap (page, entry)) - goto fail_free; + goto fail;/* * Radix-tree node allocations from PF_MEMALLOC contexts could @@ -218,7 +218,7 @@ int add_to_swap(struct page *page, struct list_head *list)* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ - goto fail_free; + goto fail;if (PageTransHuge (page)) { err =split_huge_page_to_list (page, list);@@ -230,14 +230,8 @@ int add_to_swap(struct page *page, struct list_head *list)return 1 ; -fail_free: -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); fail: -if (PageTransHuge (page) && !split_huge_page_to_list (page, list)) - goto retry; +swapcache_free (page, entry);return 0 ; }@@ -259,11 +253,7 @@ void delete_from_swap_cache(struct page *page)__delete_from_swap_cache (page);spin_unlock_irq (&address_space->tree_lock); -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); - +swapcache_free (page, entry);page_ref_sub (page,hpage_nr_pages (page)); }@@ -415,7 +405,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ -swapcache_free (entry); +swapcache_free (new_page, entry); }while (err != -ENOMEM );if (new_page)mm/swapfile.c +10
-2@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ +void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +} +static int swp_entry_cmp (const void *ent1,const void *ent2) {const swp_entry_t *e1 = ent1, *e2 = ent2;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 (page, swap); }else {void (*freepage)(struct page *);void *shadow =NULL ;Quoted 49 lines
Quoted 40 lines
Quoted 16 lines
mm/shmem.c +9
-1-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; + } +< snip >
Quoted 20 lines
/**
mm/shmem.c +2
-12@@ -178,20 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); - entry =get_swap_page (); +retry: + entry =get_swap_page (page);if (!entry.val) -return 0 ; - -if (mem_cgroup_try_charge_swap (page, entry)) { -swapcache_free (entry); -return 0 ; - } - -if (unlikely (PageTransHuge (page))) -if (unlikely (split_huge_page_to_list (page, list))) { -swapcache_free (entry); -return 0 ;Quoted 8 lines
mm/shmem.c int add_to_swap(struct page *page, struct list_head *list) · line 198
Quoted 2 lines
mm/shmem.c +1
-1- } + goto fail;So, with non-SSD swap, THP page *always* get the fail to get
swp_entry_tand retry after split the page. However, it makes unncessaryget_swap_pagescall which is not trivial. If there is no SSD swap, thp-swap out should be void without adding any performance overhead.
Hmm, but I have no good idea to do it simple. :(Quoted 3 lines
For HDD swap, the device raw throughput is so low (< 100M Bps typically), that the added overhead here will not be a big issue. Do you agree?
Quoted 8 lines
I agree. Actually, I wanted to remove the pointless overhead if we have a enough *simple* solution. However, as I said, I have no idea so just raised an issue wit hope that someone might have an idea.
Frankly speaking, I think we should support THP swap with hdd as well as ssd but it's limited to just *implementation* direction which is really unfortunate. :(
--
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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Hi Huang,
Quoted 111 lines · Ying
Quoted 109 lines · Minchan Kim
Quoted 73 lines · Ying
Quoted 63 lines · Minchan Kim
Quoted 42 lines · Ying
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.Quoted 10 lines
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_64architecture (512). For other architectures which want such THP swap optimization,ARCH_USES_THP_SWAP_CLUSTERneeds to be selected in theKconfigfile for the architecture. In effect, this will enlarge swap cluster size by 2 times onx86_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.What about other architecures?
I mean THP page size on every architectures would be various. If THP page size is much bigger than 2M, the architecture should have big swap cluster size for supporting THP swap-out feature. It means fast empty-swap cluster consumption so that it can suffer from fragmentation easily which causes THP swap void and swap slot allocations slow due to not being able to use per-cpu.
Quoted 7 lines
What I suggested was contiguous multiple swap cluster allocations to meet THP page size. If some of architecure's THP size is 64M and
SWAP_CLUSTER_SIZEis 2M, it should allocate 32 contiguos swap clusters. For that, swap layer need to manage clusters sort in order which would be more overhead inCONFIG_THP_SWAPcase but I think it's tradeoff. With that, every architectures can support THP swap easily without arch-specific something.Quoted 4 lines
That may be a good solution for other architectures. But I am afraid I am not the right person to work on that. Because I don't know the requirement of other architectures, and I have no other architectures machines to work on and measure the performance.
IMO, THP swapout is good thing for every architecture so I dobut you need to know other architecture's requirement.
Quoted 5 lines
And the swap clusters aren't sorted in order now intentionally to avoid cache line false sharing between the spinlock of struct
swap_cluster_info. If we want to sort clusters in order, we need a solution for that.Does it really matter for this work? IOW, if we couldn't solve it, cannot we support THP swapout? I don't think so. That's the least of your worries.
Also, if we have sorted cluster data structure, we need to change current single linked list of swap cluster to other one so we would need to revisit to see whether it's really problem.Quoted 13 lines
Quoted 6 lines
If (
PAGE_SIZE* 512) swap cluster size were okay for most of architecture, just increase it. It's orthogonal work regardless of THP swapout. Then, we don't need to manage swap clusters sort in order inx86_64whichSWAP_CLUSTER_SIZEis equal toTHP_PAGE_SIZE. It's just a bonus by side-effect.Andrew suggested to make swap cluster size = huge page size (or turn on THP swap optimization) only if we enabled
CONFIG_THP_SWAP. So that, THP swap optimization will not be turned on unintentionally.We may adjust default swap cluster size, but I don't think it need to be in this patchset.
Quoted 7 lines
Quoted 3 lines
That's it. This feature shouldn't be aware of swap cluster size. IOW, it would be better to work with every swap cluster size if the align between THP and swap cluster size is matched at least.
Using one swap cluster for each THP is simpler, so why not start from the simple design? Complex design may be necessary in the future, but we can work on that at that time.
If it were really architecture specific issue, I'm okay with such simple start with major architecture first. However, I don't think it's the case.
THP swap: I don't think it's a architecure issue. It's generally good thing once system supports THP. It is also good thing for HDD swap as well as SSD. (In fact, I don't understand why we should have CONFIG_THP_SWAP. I think
it should work with CONFIG_TRANSPARENT_HUGEPAGE automatically).
Current design is selected for just *simple implementation" and put the heavy drift to make it generalize to others in the future.
I don't think it's good thing. But others might have different opinions so I'm not insisting.
Johannes Weiner
maintainerRe: [PATCH -mm -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
In reply to Minchan KimQuoted 3 lines · Minchan Kim
However,
get_swap_pageis ugly now. The caller should take care of failure and should retry after split. I hopeget_swap_pageincludes split and retry logic in itself without reling on the caller.
I think this makes the interface terrible. It's an allocation function to which you pass a reference object for size - and if the allocation doesn't succeed it'll split your reference object to make it fit?
That's a nasty side effect for this function to have.
Re: [PATCH -mm -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Hi Johannes,
The patch I sent has two clean-up.
First part was as follows:
From 5400dceb3a7739d4e7ff340fc0831e0e1830ec0b Mon Sep 17 00:00:00 2001
From: Minchan Kim <[email protected]>
Date: Fri, 28 Apr 2017 15:04:14 +0900
Subject: [PATCH 1/2] swap: make swapcache_free aware of page size
Now, get_swap_page takes struct page and allocates swap space according to page size(ie, normal or THP) so it would be more clear to take struct page in swapcache_free which is a counter function of get_swap_page without needing if-else statement of caller side.
--- include/linux/swap.h | 4 ++-- mm/shmem.c | 2 +- mm/swap_state.c | 13 +++---------- mm/swapfile.c | 12 ++++++++++-- mm/vmscan.c | 2 +- 5 files changed, 17 insertions(+), 16 deletions(-)
include/linux/swap.h +2 -2
@@ -400,7 +400,7 @@ 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 (struct page *page,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 **);
@@ -459,7 +459,7 @@ static inline void swap_free(swp_entry_t swp) { } -static inline void swapcache_free (swp_entry_t swp) +static inline void swapcache_free (struct page *page,swp_entry_t swp) { }
mm/shmem.c +1 -1
@@ -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 (page, swap); redirty:set_page_dirty (page);if (wbc->for_reclaim)
mm/swap_state.c +3 -10
@@ -231,10 +231,7 @@ int add_to_swap(struct page *page, struct list_head *list)return 1 ; fail_free: -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); +swapcache_free (page, entry); fail:if (PageTransHuge (page) && !split_huge_page_to_list (page, list)) goto retry;
@@ -259,11 +256,7 @@ void delete_from_swap_cache(struct page *page)__delete_from_swap_cache (page);spin_unlock_irq (&address_space->tree_lock); -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); - +swapcache_free (page, entry);page_ref_sub (page,hpage_nr_pages (page)); }
@@ -415,7 +408,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ -swapcache_free (entry); +swapcache_free (new_page, entry); }while (err != -ENOMEM );if (new_page)
mm/swapfile.c +10 -2
@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;
@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;
@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ +void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +} +static int swp_entry_cmp (const void *ent1,const void *ent2) {const swp_entry_t *e1 = ent1, *e2 = ent2;
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 (page, swap); }else {void (*freepage)(struct page *);void *shadow =NULL ;
Signature · 1 line
2.7.4
Quoted 10 lines · Johannes Weiner
Quoted 3 lines · Minchan Kim
However,
get_swap_pageis ugly now. The caller should take care of failure and should retry after split. I hopeget_swap_pageincludes split and retry logic in itself without reling on the caller.I think this makes the interface terrible. It's an allocation function to which you pass a reference object for size - and if the allocation doesn't succeed it'll split your reference object to make it fit?
That's a nasty side effect for this function to have.
It does make sense to me.
With that point of view, retry logic in add_to_swap is awkward to me, too. The add_to_swap is a kind of allocate function(swap slot and swap cache) so I think it would be better to move retry logic to vmscan.c.
What do you think about it?
From e228d67e9677f8eeea1e424cab61b67884d534b4 Mon Sep 17 00:00:00 2001
From: Minchan Kim <[email protected]>
Date: Tue, 2 May 2017 08:30:41 +0900
Subject: [PATCH 2/2] swap: move anonymous THP split logic to vmscan
The add_to_swap aims to allocate swap_space(ie, swap slot and swapcache) so if it fails due to lack of space in case of THP, the caller should split the THP page and retry it with a page.
--- include/linux/swap.h | 4 ++-- mm/swap_state.c | 23 ++++++----------------- mm/vmscan.c | 22 +++++++++++++++++++++- 3 files changed, 29 insertions(+), 20 deletions(-)
include/linux/swap.h +2 -2
@@ -359,7 +359,7 @@ extern struct address_space *swapper_spaces[]; >> SWAP_ADDRESS_SPACE_SHIFT]) extern unsigned long total_swapcache_pages(void);extern void show_swap_cache_info (void ); -extern int add_to_swap (struct page *,struct list_head *list); +extern int add_to_swap (struct page *);extern int add_to_swap_cache (struct page *,swp_entry_t ,gfp_t );extern int __add_to_swap_cache (struct page *page,swp_entry_t entry);extern void __delete_from_swap_cache (struct page *);
@@ -479,7 +479,7 @@ static inline struct page *lookup_swap_cache(swp_entry_t swp)return NULL ; } -static inline int add_to_swap (struct page *page,struct list_head *list) +static inline int add_to_swap (struct page *page) {return 0 ; }
mm/swap_state.c +6 -17
@@ -184,7 +184,7 @@ void __delete_from_swap_cache(struct page *page)* Allocate swap space for the page and add the page to the * swap cache. Caller needs to hold the page lock. */ -int add_to_swap (struct page *page,struct list_head *list) +int add_to_swap (struct page *page) {swp_entry_t entry;int err;
@@ -192,12 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); -retry: entry =get_swap_page (page);if (!entry.val) - goto fail; +return 0 ; +if (mem_cgroup_try_charge_swap (page, entry)) - goto fail_free; + goto fail;/* * Radix-tree node allocations from PF_MEMALLOC contexts could
@@ -218,23 +218,12 @@ int add_to_swap(struct page *page, struct list_head *list)* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ - goto fail_free; - -if (PageTransHuge (page)) { - err =split_huge_page_to_list (page, list); -if (err) { -delete_from_swap_cache (page); -return 0 ; - } - } + goto fail;return 1 ; -fail_free: -swapcache_free (page, entry); fail: -if (PageTransHuge (page) && !split_huge_page_to_list (page, list)) - goto retry; +swapcache_free (page, entry);return 0 ; }
mm/vmscan.c +21 -1
@@ -1125,8 +1125,28 @@ static unsigned long shrink_page_list(struct list_head *page_list, !PageSwapCache(page)) {if (!(sc->gfp_mask & __GFP_IO)) goto keep_locked; -if (!add_to_swap (page, page_list)) +swap_retry: +/* +* Retry after split if we fail to allocate +* swap space of a THP. +*/ +if (!add_to_swap (page)) { +if (!PageTransHuge (page) || +split_huge_page_to_list (page, page_list)) + goto activate_locked; + goto swap_retry; + } + +/* +* Got swap space successfully. But unfortunately, +* we don't support a THP page writeout so split it. +*/ +if (PageTransHuge (page) && +split_huge_page_to_list (page, page_list)) { +delete_from_swap_cache (page); goto activate_locked; + } + may_enter_fs =1 ;/* Adding to swap updated mapping */
Signature · 7 lines
2.7.4 -- 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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Hi Michan,
Quoted 19 lines · Minchan Kim
mm/swapfile.c +2
-2@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;
mm/swapfile.c void swapcache_free_cluster(swp_entry_t entry) · line 1191
Quoted 10 lines
mm/swapfile.c +7
-0@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ +void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +}
I don't think this is cleaner :/
On your second patch:
Quoted 6 lines
mm/vmscan.c +1
-1@@ -1125,8 +1125,28 @@ static unsigned long shrink_page_list(struct list_head *page_list, !PageSwapCache(page)) {if (!(sc->gfp_mask & __GFP_IO)) goto keep_locked; -if (!add_to_swap (page, page_list)) +swap_retry:
mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1138
Quoted 10 lines
mm/vmscan.c +10
-0+/* +* Retry after split if we fail to allocate +* swap space of a THP. +*/ +if (!add_to_swap (page)) { +if (!PageTransHuge (page) || +split_huge_page_to_list (page, page_list)) + goto activate_locked; + goto swap_retry; + }
This is definitely better.
However, I think it'd be cleaner without the label here:
if (!add_to_swap(page)) {
if (!PageTransHuge(page))
goto activate_locked;
/* Split THP and swap individual base pages */
if (split_huge_page_to_list(page, page_list))
goto activate_locked;
if (!add_to_swap(page))
goto activate_locked;
}mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1148
Quoted 9 lines
mm/vmscan.c +8
-0+/* +* Got swap space successfully. But unfortunately, +* we don't support a THP page writeout so split it. +*/ +if (PageTransHuge (page) && +split_huge_page_to_list (page, page_list)) { +delete_from_swap_cache (page); goto activate_locked; + }
Pulling this out of add_to_swap() is an improvement for sure. Add an
XXX: before that "we don't support THP writes" comment for good measure :)
--
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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 47 lines · Johannes Weiner
Hi Michan,
Quoted 19 lines · Minchan Kim
mm/swapfile.c +2
-2@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;mm/swapfile.c void swapcache_free_cluster(swp_entry_t entry) · line 1191
Quoted 10 lines
mm/swapfile.c +7
-0@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ +void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +}I don't think this is cleaner :/
On your second patch:
Quoted 10 lines
mm/vmscan.c +5
-1@@ -1125,8 +1125,28 @@ static unsigned long shrink_page_list(struct list_head *page_list, !PageSwapCache(page)) {if (!(sc->gfp_mask & __GFP_IO)) goto keep_locked; -if (!add_to_swap (page, page_list)) +swap_retry: +/* +* Retry after split if we fail to allocate +* swap space of a THP. +*/
Quoted 8 lines
mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1138
Quoted 6 lines
mm/vmscan.c +6
-0+if (!add_to_swap (page)) { +if (!PageTransHuge (page) || +split_huge_page_to_list (page, page_list)) + goto activate_locked; + goto swap_retry; + }This is definitely better.
Thanks.
Quoted 11 lines
However, I think it'd be cleaner without the label here:
if (!add_to_swap(page)) { if (!PageTransHuge(page)) goto activate_locked; /* Split THP and swap individual base pages */ if (split_huge_page_to_list(page, page_list)) goto activate_locked; if (!add_to_swap(page)) goto activate_locked;
Yes.
Quoted 15 lines
}mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1148
Quoted 9 lines
mm/vmscan.c +8
-0+/* +* Got swap space successfully. But unfortunately, +* we don't support a THP page writeout so split it. +*/ +if (PageTransHuge (page) && +split_huge_page_to_list (page, page_list)) { +delete_from_swap_cache (page); goto activate_locked; + }Pulling this out of
add_to_swap()is an improvement for sure. Add an
XXX: before that "we don't support THP writes" comment for good measure :)
Sure.
It could be a separate patch which makes add_to_swap clean via removing page_list argument but I hope Huang take/fold it when he resend it because it would be more important with THP swap.
Thanks.
Re: [PATCH -mm -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 89 lines · Minchan Kim
Quoted 47 lines · Johannes Weiner
Hi Michan,
Quoted 19 lines · Minchan Kim
mm/swapfile.c +2
-2@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;mm/swapfile.c void swapcache_free_cluster(swp_entry_t entry) · line 1191
Quoted 10 lines
mm/swapfile.c +7
-0@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ +void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +}I don't think this is cleaner :/
On your second patch:
Quoted 10 lines
mm/vmscan.c +5
-1@@ -1125,8 +1125,28 @@ static unsigned long shrink_page_list(struct list_head *page_list, !PageSwapCache(page)) {if (!(sc->gfp_mask & __GFP_IO)) goto keep_locked; -if (!add_to_swap (page, page_list)) +swap_retry: +/* +* Retry after split if we fail to allocate +* swap space of a THP. +*/ Quoted 8 lines
mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1138
Quoted 6 lines
mm/vmscan.c +6
-0+if (!add_to_swap (page)) { +if (!PageTransHuge (page) || +split_huge_page_to_list (page, page_list)) + goto activate_locked; + goto swap_retry; + }This is definitely better.
Thanks.
Quoted 11 lines
However, I think it'd be cleaner without the label here:
if (!add_to_swap(page)) { if (!PageTransHuge(page)) goto activate_locked; /* Split THP and swap individual base pages */ if (split_huge_page_to_list(page, page_list)) goto activate_locked; if (!add_to_swap(page)) goto activate_locked;Yes.
Quoted 15 lines
}mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1148
Quoted 9 lines
mm/vmscan.c +8
-0+/* +* Got swap space successfully. But unfortunately, +* we don't support a THP page writeout so split it. +*/ +if (PageTransHuge (page) && +split_huge_page_to_list (page, page_list)) { +delete_from_swap_cache (page); goto activate_locked; + }Pulling this out of
add_to_swap()is an improvement for sure. Add an
XXX: before that "we don't support THP writes" comment for good measure :)
Quoted 5 lines
Sure.
It could be a separate patch which makes
add_to_swapclean via removingpage_listargument but I hope Huang take/fold it when he resend it because it would be more important with THP swap.
Sure. I will take this patch as one patch of the THP swap series. Because the first patch of the THP swap series is a little big, I don't think it is a good idea to fold this patch into it. Could you update the patch according to Johannes' comments and resend it?
Best Regards,
Huang, Ying
Re: [PATCH -mm -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
On Thu, May 11, 2017 at 08:50:01AM +0800, Huang, Ying wrote:
< snip >
Quoted 53 lines
Quoted 53 lines
Quoted 10 lines
Quoted 10 lines
mm/vmscan.c +5
-1@@ -1125,8 +1125,28 @@ static unsigned long shrink_page_list(struct list_head *page_list, !PageSwapCache(page)) {if (!(sc->gfp_mask & __GFP_IO)) goto keep_locked; -if (!add_to_swap (page, page_list)) +swap_retry: +/* +* Retry after split if we fail to allocate +* swap space of a THP. +*/ Quoted 8 lines
mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1138
Quoted 6 lines
mm/vmscan.c +6
-0+if (!add_to_swap (page)) { +if (!PageTransHuge (page) || +split_huge_page_to_list (page, page_list)) + goto activate_locked; + goto swap_retry; + }This is definitely better.
Thanks.
Quoted 11 lines
However, I think it'd be cleaner without the label here:
if (!add_to_swap(page)) { if (!PageTransHuge(page)) goto activate_locked; /* Split THP and swap individual base pages */ if (split_huge_page_to_list(page, page_list)) goto activate_locked; if (!add_to_swap(page)) goto activate_locked;Yes.
Quoted 15 lines
}mm/vmscan.c static unsigned long shrink_page_list(struct list_head *page_list, · line 1148
Quoted 9 lines
mm/vmscan.c +8
-0+/* +* Got swap space successfully. But unfortunately, +* we don't support a THP page writeout so split it. +*/ +if (PageTransHuge (page) && +split_huge_page_to_list (page, page_list)) { +delete_from_swap_cache (page); goto activate_locked; + }Pulling this out of
add_to_swap()is an improvement for sure. Add an
XXX: before that "we don't support THP writes" comment for good measure :)Sure.
Quoted 8 lines
Quoted 3 lines
It could be a separate patch which makes
add_to_swapclean via removingpage_listargument but I hope Huang take/fold it when he resend it because it would be more important with THP swap.Sure. I will take this patch as one patch of the THP swap series. Because the first patch of the THP swap series is a little big, I don't think it is a good idea to fold this patch into it. Could you update the patch according to Johannes' comments and resend it?
Okay, I will resend this clean-up patch against on yours patch after finishing this discussion.
Thanks.
PATCH 1/2 mm: swap: unify swap slot free functions to put_swap_page
In reply to Huang, YingNow, get_swap_page takes struct page and allocates swap space according to page size(ie, normal or THP) so it would be more cleaner to introduce put_swap_page which is a counter function of get_swap_page. Then, it calls right swap slot free function depending on page's size.
--- include/linux/swap.h | 4 ++-- mm/shmem.c | 2 +- mm/swap_state.c | 13 +++---------- mm/swapfile.c | 8 ++++++++ mm/vmscan.c | 2 +- 5 files changed, 15 insertions(+), 14 deletions(-)
include/linux/swap.h +2 -2
@@ -393,6 +393,7 @@ static inline long get_nr_swap_pages(void)extern void si_swapinfo (struct sysinfo *);extern swp_entry_t get_swap_page (struct page *page); +extern void put_swap_page (struct page *page,swp_entry_t entry);extern swp_entry_t get_swap_page_of_type (int );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 );
@@ -400,7 +401,6 @@ 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_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 **);
@@ -459,7 +459,7 @@ static inline void swap_free(swp_entry_t swp) { } -static inline void swapcache_free (swp_entry_t swp) +static inline void put_swap_page (struct page *page,swp_entry_t swp) { }
mm/shmem.c +1 -1
@@ -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); +put_swap_page (page, swap); redirty:set_page_dirty (page);if (wbc->for_reclaim)
mm/swap_state.c +3 -10
@@ -231,10 +231,7 @@ int add_to_swap(struct page *page, struct list_head *list)return 1 ; fail_free: -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); +put_swap_page (page, entry); fail:if (PageTransHuge (page) && !split_huge_page_to_list (page, list)) goto retry;
@@ -259,11 +256,7 @@ void delete_from_swap_cache(struct page *page)__delete_from_swap_cache (page);spin_unlock_irq (&address_space->tree_lock); -if (PageTransHuge (page)) -swapcache_free_cluster (entry); -else -swapcache_free (entry); - +put_swap_page (page, entry);page_ref_sub (page,hpage_nr_pages (page)); }
@@ -415,7 +408,7 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ -swapcache_free (entry); +put_swap_page (new_page, entry); }while (err != -ENOMEM );if (new_page)
mm/swapfile.c +8 -0
@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ +void put_swap_page (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +swapcache_free (entry); +else +swapcache_free_cluster (entry); +} +static int swp_entry_cmp (const void *ent1,const void *ent2) {const swp_entry_t *e1 = ent1, *e2 = ent2;
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); +put_swap_page (page, swap); }else {void (*freepage)(struct page *);void *shadow =NULL ;
Signature · 7 lines
2.7.4 -- 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 2/2 mm: swap: move anonymous THP split logic to vmscan
The add_to_swap aims to allocate swap_space(ie, swap slot and swapcache) so if it fails due to lack of space in case of THP or something(hdd swap but tries THP swapout) *caller* rather than add_to_swap itself should split the THP page and retry it with base page which is more natural.
--- include/linux/swap.h | 4 ++-- mm/swap_state.c | 23 ++++++----------------- mm/vmscan.c | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 20 deletions(-)
include/linux/swap.h +2 -2
@@ -359,7 +359,7 @@ extern struct address_space *swapper_spaces[]; >> SWAP_ADDRESS_SPACE_SHIFT]) extern unsigned long total_swapcache_pages(void);extern void show_swap_cache_info (void ); -extern int add_to_swap (struct page *,struct list_head *list); +extern int add_to_swap (struct page *);extern int add_to_swap_cache (struct page *,swp_entry_t ,gfp_t );extern int __add_to_swap_cache (struct page *page,swp_entry_t entry);extern void __delete_from_swap_cache (struct page *);
@@ -479,7 +479,7 @@ static inline struct page *lookup_swap_cache(swp_entry_t swp)return NULL ; } -static inline int add_to_swap (struct page *page,struct list_head *list) +static inline int add_to_swap (struct page *page) {return 0 ; }
mm/swap_state.c +6 -17
@@ -184,7 +184,7 @@ void __delete_from_swap_cache(struct page *page)* Allocate swap space for the page and add the page to the * swap cache. Caller needs to hold the page lock. */ -int add_to_swap (struct page *page,struct list_head *list) +int add_to_swap (struct page *page) {swp_entry_t entry;int err;
@@ -192,12 +192,12 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); -retry: entry =get_swap_page (page);if (!entry.val) - goto fail; +return 0 ; +if (mem_cgroup_try_charge_swap (page, entry)) - goto fail_free; + goto fail;/* * Radix-tree node allocations from PF_MEMALLOC contexts could
@@ -218,23 +218,12 @@ int add_to_swap(struct page *page, struct list_head *list)* add_to_swap_cache() doesn't return -EEXIST, so we can safely * clear SWAP_HAS_CACHE flag. */ - goto fail_free; - -if (PageTransHuge (page)) { - err =split_huge_page_to_list (page, list); -if (err) { -delete_from_swap_cache (page); -return 0 ; - } - } + goto fail;return 1 ; -fail_free: -put_swap_page (page, entry); fail: -if (PageTransHuge (page) && !split_huge_page_to_list (page, list)) - goto retry; +put_swap_page (page, entry);return 0 ; }
mm/vmscan.c +16 -1
@@ -1125,8 +1125,23 @@ static unsigned long shrink_page_list(struct list_head *page_list, !PageSwapCache(page)) {if (!(sc->gfp_mask & __GFP_IO)) goto keep_locked; -if (!add_to_swap (page, page_list)) +if (!add_to_swap (page)) { +if (!PageTransHuge (page)) + goto activate_locked; +/* Split THP and swap individual base pages */ +if (split_huge_page_to_list (page, page_list)) + goto activate_locked; +if (!add_to_swap (page)) + goto activate_locked; + } + +/* XXX: We don't support THP writes */ +if (PageTransHuge (page) && +split_huge_page_to_list (page, page_list)) { +delete_from_swap_cache (page); goto activate_locked; + } + may_enter_fs =1 ;/* Adding to swap updated mapping */
Signature · 7 lines
2.7.4 -- 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 2/2] mm: swap: move anonymous THP split logic to vmscan
Quoted 8 lines · Minchan Kim
The
add_to_swapaims to allocateswap_space(ie, swap slot and swapcache)so if it fails due to lack of space in case of THP or something(hdd swap but tries THP swapout) *caller* rather thanadd_to_swapitself should split the THP page and retry it with base page which is more natural.Cc Johannes Weiner <[email protected]>Signed-off-by Minchan Kim <[email protected]>
--
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 1/2] mm: swap: unify swap slot free functions to put_swap_page
In reply to Minchan KimQuoted 8 lines · Minchan Kim
Now,
get_swap_pagetakes struct page and allocates swap space according to pagesize(ie, normal or THP)so it would be more cleaner to introduceput_swap_pagewhich is a counter function ofget_swap_page. Then, it calls right swap slot free function depending on page's size.Cc Johannes Weiner <[email protected]>Signed-off-by Minchan Kim <[email protected]>
--
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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
In reply to Minchan KimQuoted 26 lines · Minchan Kim
Quoted 25 lines · Johannes Weiner
Hi Michan,
Quoted 22 lines · Minchan Kim
mm/swapfile.c +2
-2@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */
Quoted 9 lines
Quoted 9 lines
mm/swapfile.c void swapcache_free_cluster(swp_entry_t entry) · line 1191
Quoted 7 lines
mm/swapfile.c +7
-0+void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +}I don't think this is cleaner :/
Let's see a example add_to_swap. Without it, it looks like that.
int add_to_swap(struct page *page)
{
entry = get_swap_page(page);
..
..
fail:
if (PageTransHuge(page))
swapcache_free_cluster(entry);
else
swapcache_free(entry);
}It doesn't looks good to me because get_swap_page hides where entry allocation is from cluster or slot but when we free the entry allocated, we should be aware of the internal and call right function. :(
Do you think it's better still?
--
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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Quoted 51 lines · Minchan Kim
Quoted 26 lines · Minchan Kim
Quoted 25 lines · Johannes Weiner
Hi Michan,
Quoted 22 lines · Minchan Kim
mm/swapfile.c +2
-2@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ Quoted 9 lines
Quoted 9 lines
mm/swapfile.c void swapcache_free_cluster(swp_entry_t entry) · line 1191
Quoted 7 lines
mm/swapfile.c +7
-0+void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +}I don't think this is cleaner :/
Let's see a example
add_to_swap. Without it, it looks like that.int
add_to_swap(struct page *page){ entry = get_swap_page(page); .. .. fail: if (PageTransHuge(page)) swapcache_free_cluster(entry); else swapcache_free(entry); }
Quoted 4 lines
It doesn't looks good to me because
get_swap_pagehides where entry allocation is from cluster or slot but when we free the entry allocated, we should be aware of the internal and call right function. :(
This could be nicer indeed. I just don't like the underscore versions much, but symmetry with get_swap_page() would be nice.
How about put_swap_page()? :)
That can call the appropriate swapcache_free function then.
--
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 -v10 1/3] mm, THP, swap: Delay splitting THP during swap out
Hi Hannes,
Quoted 57 lines · Johannes Weiner
Quoted 51 lines · Minchan Kim
Quoted 26 lines · Minchan Kim
Quoted 25 lines · Johannes Weiner
Hi Michan,
Quoted 22 lines · Minchan Kim
mm/swapfile.c +2
-2@@ -1144,7 +1144,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -void swapcache_free (swp_entry_t entry) +void __swapcache_free (swp_entry_t entry) {struct swap_info_struct *p;@@ -1156,7 +1156,7 @@ void swapcache_free(swp_entry_t entry) }#ifdef CONFIG_THP_SWAP -void swapcache_free_cluster (swp_entry_t entry) +void __swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;@@ -1182,6 +1182,14 @@ void swapcache_free_cluster(swp_entry_t entry) }#endif /* CONFIG_THP_SWAP */ Quoted 9 lines
Quoted 9 lines
mm/swapfile.c void swapcache_free_cluster(swp_entry_t entry) · line 1191
Quoted 7 lines
mm/swapfile.c +7
-0+void swapcache_free (struct page *page,swp_entry_t entry) +{ +if (!PageTransHuge (page)) +__swapcache_free (entry); +else +__swapcache_free_cluster (entry); +}I don't think this is cleaner :/
Let's see a example
add_to_swap. Without it, it looks like that.int
add_to_swap(struct page *page){ entry = get_swap_page(page); .. .. fail: if (PageTransHuge(page)) swapcache_free_cluster(entry); else swapcache_free(entry); }Quoted 4 lines
It doesn't looks good to me because
get_swap_pagehides where entry allocation is from cluster or slot but when we free the entry allocated, we should be aware of the internal and call right function. :(
Quoted 4 lines
This could be nicer indeed. I just don't like the underscore versions much, but symmetry with
get_swap_page()would be nice.How about
put_swap_page()? :)
Good idea. It's the best one I can do now.
Actually, get_swap_page is awkward to me. Maybe it would be nicer to rename it with get_swap_[slot|entry] but, I will postpone it if someone would be on same page with me in future.
Quoted 2 lines
That can call the appropriate
swapcache_freefunction then.
Yub.
Thanks for the review!
--
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 2/3 mm, THP, swap: Check whether THP can be split firstly
In reply to Huang, YingFrom: Huang Ying <[email protected]>
To swap out THP (Transparent Huage Page), before splitting the THP, 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, so that 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.
With the patch, the swap out throughput improves 3.6% (from about 4.16GB/s to about 4.31GB/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.
--- include/linux/huge_mm.h | 7 +++++++ mm/huge_memory.c | 20 ++++++++++++++++---- mm/swap_state.c | 4 ++++ 3 files changed, 27 insertions(+), 4 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
@@ -2384,6 +2384,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.
@@ -2431,7 +2446,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 {
@@ -2443,8 +2457,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); }
@@ -2453,7 +2465,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 +4 -0
@@ -192,6 +192,10 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); +/* cannot split, skip it */ +if (PageTransHuge (page) && !can_split_huge_page (page,NULL )) +return 0 ; + retry: entry =get_swap_page (page);if (!entry.val)
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 -v10 2/3] mm, THP, swap: Check whether THP can be split firstly
Quoted 18 lines · Ying
From: Huang Ying <[email protected]>
To swap out THP (Transparent Huage Page), before splitting the THP, 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, so that 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.
With the patch, the swap out throughput improves 3.6% (from about 4.16GB/s to about 4.31GB/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.
Quoted 3 lines
Cc Johannes Weiner <[email protected]>Signed-off-by "Huang, Ying" <[email protected]>
--
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 | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
mm/swap_state.c +13 -3
@@ -192,9 +192,19 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); -/* cannot split, skip it */ -if (PageTransHuge (page) && !can_split_huge_page (page,NULL )) -return 0 ; +if (PageTransHuge (page)) { +/* cannot split, skip it */ +if (!can_split_huge_page (page,NULL )) +return 0 ; +/* +* Split pages without a PMD map right away. Chances +* are some or all of the tail pages can be freed +* without IO. +*/ +if (!compound_mapcount (page) && +split_huge_page_to_list (page, list)) +return 0 ; + } retry: entry =get_swap_page (page);
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 -v10 3/3] mm, THP, swap: Enable THP swap optimization only if has compound map
Quoted 10 lines · Ying
From: 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.
Cc Johannes Weiner <[email protected]>Signed-off-by "Huang, Ying" <[email protected]>
CC Kirill to double check the reasoning here
Quoted 33 lines
--- mm/swap_state.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)mm/swap_state.c +13
-3@@ -192,9 +192,19 @@ int add_to_swap(struct page *page, struct list_head *list)VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageUptodate (page), page); -/* cannot split, skip it */ -if (PageTransHuge (page) && !can_split_huge_page (page,NULL )) -return 0 ; +if (PageTransHuge (page)) { +/* cannot split, skip it */ +if (!can_split_huge_page (page,NULL )) +return 0 ; +/* +* Split pages without a PMD map right away. Chances +* are some or all of the tail pages can be freed +* without IO. +*/ +if (!compound_mapcount (page) && +split_huge_page_to_list (page, list)) +return 0 ; + } retry: entry =get_swap_page (page);Signature · 1 line
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>
Kirill A. Shutemov
Acked-byRe: [PATCH -mm -v10 3/3] mm, THP, swap: Enable THP swap optimization only if has compound map
Quoted 9 lines · Johannes Weiner
Quoted 8 lines · Ying
From: 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.
Quoted 6 lines
Quoted 2 lines
Cc Johannes Weiner <[email protected]>Signed-off-by "Huang, Ying" <[email protected]>Acked-by Johannes Weiner <[email protected]>CC Kirill to double check the reasoning here
Looks good to me:
Signature · 7 lines
Kirill A. Shutemov -- 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>