swap: THP optimizing refactoring
Subsystems (2)
v3 cover swap: THP optimizing refactoring
This patchset is based on 2018-07-13 head of mmotm tree.
Now the THP (Transparent Huge Page) swap optimizing is implemented in the way like below,
#ifdef CONFIG_THP_SWAPhuge_function(...)
{
}
#elsenormal_function(...)
{
}
#endif
general_function(...)
{
if (huge)
return thp_function(...);
else
return normal_function(...);
}As pointed out by Dave Hansen, this will,
1. Created a new, wholly untested code path for huge page
2. Created two places to patch bugs
3. Are not reusing code when possible
This patchset is to address these problems via merging huge/normal code path/functions if possible.
One concern is that this may cause code size to dilate when !CONFIG_TRANSPARENT_HUGEPAGE. The data shows that most refactoring will only cause quite slight code size increase.
Best Regards,
Huang, Ying
PATCH v3 1/8 swap: Add comments to lock_cluster_or_swap_info()
To improve the code readability.
--- mm/swapfile.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/swapfile.c +6 -0
@@ -297,13 +297,19 @@ static inline void unlock_cluster(struct swap_cluster_info *ci)spin_unlock (&ci->lock); } +/* +* Determine the locking method in use for this device. Return +* swap_cluster_info if SSD-style cluster-based locking is in place. +*/ static inline struct swap_cluster_info *lock_cluster_or_swap_info (struct swap_info_struct *si,unsigned long offset) {struct swap_cluster_info *ci; +/* Try to use fine-grained SSD-style locking if available: */ ci =lock_cluster (si, offset); +/* Otherwise, fall back to traditional, coarse locking: */ if (!ci)spin_lock (&si->lock);
Signature · 1 line
2.16.4
Re: [PATCH v3 1/8] swap: Add comments to lock_cluster_or_swap_info()
mm/swapfile.c static inline void unlock_cluster(struct swap_cluster_info *ci) · line 310
Quoted 11 lines · Huang Ying
mm/swapfile.c +5
-0+/* +* Determine the locking method in use for this device. Return +* swap_cluster_info if SSD-style cluster-based locking is in place. +*/ static inline struct swap_cluster_info *lock_cluster_or_swap_info (struct swap_info_struct *si,unsigned long offset) {struct swap_cluster_info *ci; +/* Try to use fine-grained SSD-style locking if available: */
Once you touch this are can you also please use standard two-tab alignment for the spill-over function arguments:
static inline struct swap_cluster_info *lock_cluster_or_swap_info(
struct swap_info_struct *si, unsigned long offset)
Re: [PATCH v3 1/8] swap: Add comments to lock_cluster_or_swap_info()
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Huang Ying
mm/swapfile.c +4
-0+/* +* Determine the locking method in use for this device. Return +* swap_cluster_info if SSD-style cluster-based locking is in place. +*/ static inline struct swap_cluster_info *lock_cluster_or_swap_info (struct swap_info_struct *si,unsigned long offset) {struct swap_cluster_info *ci;
Quoted 7 lines
mm/swapfile.c static inline void unlock_cluster(struct swap_cluster_info *ci) · line 310
Quoted 1 line
mm/swapfile.c +1
-0+/* Try to use fine-grained SSD-style locking if available: */ Once you touch this are can you also please use standard two-tab alignment for the spill-over function arguments:
static inline struct
swap_cluster_info*lock_cluster_or_swap_info(
structswap_info_struct*si, unsigned long offset)
Sure. Will change this in next version.
Best Regards,
Huang, Ying
PATCH v3 2/8 mm/swapfile.c: Replace some #ifdef with IS_ENABLED()
In reply to Huang YingIn mm/swapfile.c, THP (Transparent Huge Page) swap specific code is enclosed by #ifdef CONFIG_THP_SWAP/#endif to avoid code dilating when THP isn't enabled. But #ifdef/#endif in .c file hurt the code readability, so Dave suggested to use IS_ENABLED(CONFIG_THP_SWAP) instead and let compiler to do the dirty job for us. This has potential to remove some duplicated code too. From output of size,
text data bss dec hex filename
THP=y: 26269 2076 340 28685 700d mm/swapfile.o ifdef/endif: 24115 2028 340 26483 6773 mm/swapfile.o IS_ENABLED: 24179 2028 340 26547 67b3 mm/swapfile.o
IS_ENABLED() based solution works quite well, almost as good as that of #ifdef/#endif. And from the diffstat, the removed lines are more than added lines.
One #ifdef for split_swap_cluster() is kept. Because it is a public function with a stub implementation for CONFIG_THP_SWAP=n in swap.h.
--- mm/swapfile.c | 60 ++++++++++++++++++++--------------------------------------- 1 file changed, 20 insertions(+), 40 deletions(-)
mm/swapfile.c +20 -40
@@ -870,7 +870,6 @@ static int scan_swap_map_slots(struct swap_info_struct *si,return n_ret; } -#ifdef CONFIG_THP_SWAP static int swap_alloc_cluster (struct swap_info_struct *si,swp_entry_t *slot) {unsigned long idx;
@@ -878,6 +877,15 @@ static int swap_alloc_cluster(struct swap_info_struct *si, swp_entry_t *slot)unsigned long offset, i;unsigned char *map; +/* +* Should not even be attempting cluster allocations when huge +* page swap is disabled. Warn and fail the allocation. +*/ +if (!IS_ENABLED (CONFIG_THP_SWAP )) { +VM_WARN_ON_ONCE (1 ); +return 0 ; + } +if (cluster_list_empty (&si->free_clusters))return 0 ;
@@ -908,13 +916,6 @@ static void swap_free_cluster(struct swap_info_struct *si, unsigned long idx)unlock_cluster (ci);swap_range_free (si, offset,SWAPFILE_CLUSTER ); } -#else -static int swap_alloc_cluster (struct swap_info_struct *si,swp_entry_t *slot) -{ -VM_WARN_ON_ONCE (1 ); -return 0 ; -} -#endif /* CONFIG_THP_SWAP */ static unsigned long scan_swap_map (struct swap_info_struct *si,unsigned char usage)
@@ -1260,7 +1261,6 @@ static void swapcache_free(swp_entry_t entry) } } -#ifdef CONFIG_THP_SWAP static void swapcache_free_cluster (swp_entry_t entry) {unsigned long offset =swp_offset (entry);
@@ -1271,6 +1271,9 @@ static void swapcache_free_cluster(swp_entry_t entry)unsigned int i, free_entries =0 ;unsigned char val; +if (!IS_ENABLED (CONFIG_THP_SWAP )) +return ; + si =_swap_info_get (entry);if (!si)return ;
@@ -1306,6 +1309,7 @@ static void swapcache_free_cluster(swp_entry_t entry) } } +#ifdef CONFIG_THP_SWAP int split_swap_cluster (swp_entry_t entry) {struct swap_info_struct *si;
@@ -1320,11 +1324,7 @@ int split_swap_cluster(swp_entry_t entry)unlock_cluster (ci); return 0; } -#else -static inline void swapcache_free_cluster (swp_entry_t entry) -{ -} -#endif /* CONFIG_THP_SWAP */ +#endif void put_swap_page (struct page *page,swp_entry_t entry) {
@@ -1483,7 +1483,6 @@ int swp_swapcount(swp_entry_t entry)return count; } -#ifdef CONFIG_THP_SWAP static bool swap_page_trans_huge_swapped (struct swap_info_struct *si,swp_entry_t entry) {
@@ -1494,6 +1493,9 @@ static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,int i;bool ret = false; +if (!IS_ENABLED (CONFIG_THP_SWAP )) +return swap_swapcount (si, entry) !=0 ; + ci =lock_cluster_or_swap_info (si, offset);if (!ci || !cluster_is_huge (ci)) {if (map[roffset] !=SWAP_HAS_CACHE )
@@ -1516,7 +1518,7 @@ static bool page_swapped(struct page *page)swp_entry_t entry;struct swap_info_struct *si; -if (likely (!PageTransCompound (page))) +if (!IS_ENABLED (CONFIG_THP_SWAP ) ||likely (!PageTransCompound (page)))return page_swapcount (page) !=0 ; page =compound_head (page);
@@ -1540,10 +1542,8 @@ static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,/* hugetlbfs shouldn't call it */ VM_BUG_ON_PAGE (PageHuge (page), page); -if (likely (!PageTransCompound (page))) { - mapcount =atomic_read (&page->_mapcount) +1 ; -if (total_mapcount) - *total_mapcount = mapcount; +if (!IS_ENABLED (CONFIG_THP_SWAP ) ||likely (!PageTransCompound (page))) { + mapcount =page_trans_huge_mapcount (page, total_mapcount);if (PageSwapCache (page)) swapcount =page_swapcount (page);if (total_swapcount)
@@ -1590,26 +1590,6 @@ static int page_trans_huge_map_swapcount(struct page *page, int *total_mapcount,return map_swapcount; } -#else -#define swap_page_trans_huge_swapped (si, entry) swap_swapcount(si, entry) -#define page_swapped (page) (page_swapcount(page) != 0) - -static int page_trans_huge_map_swapcount (struct page *page,int *total_mapcount, -int *total_swapcount) -{ -int mapcount, swapcount =0 ; - -/* hugetlbfs shouldn't call it */ -VM_BUG_ON_PAGE (PageHuge (page), page); - - mapcount =page_trans_huge_mapcount (page, total_mapcount); -if (PageSwapCache (page)) - swapcount =page_swapcount (page); -if (total_swapcount) - *total_swapcount = swapcount; -return mapcount + swapcount; -} -#endif /* * We can write to an anon page without COW if there are no other references
Signature · 1 line
2.16.4
PATCH v3 3/8 swap: Use swap_count() in swap_page_trans_huge_swapped()
In reply to Huang YingIn swap_page_trans_huge_swapped(), to identify whether there's any page table mapping for a 4k sized swap entry, "si->swap_map[i] != SWAP_HAS_CACHE" is used. This works correctly now, because all users of the function will only call it after checking SWAP_HAS_CACHE. But as pointed out by Daniel, it is better to use "swap_count(map[i])" here, because it works for "map[i] == 0" case too.
And this makes the implementation more consistent between normal and huge swap entry.
--- mm/swapfile.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/swapfile.c +2 -2
@@ -1498,12 +1498,12 @@ static bool swap_page_trans_huge_swapped(struct swap_info_struct *si, ci =lock_cluster_or_swap_info (si, offset);if (!ci || !cluster_is_huge (ci)) { -if (map[roffset] !=SWAP_HAS_CACHE ) +if (swap_count (map[roffset])) ret = true; goto unlock_out; }for (i =0 ; i <SWAPFILE_CLUSTER ; i++) { -if (map[offset + i] !=SWAP_HAS_CACHE ) { +if (swap_count (map[offset + i])) { ret = true;break ; }
Signature · 1 line
2.16.4
PATCH v3 4/8 swap: Unify normal/huge code path in swap_page_trans_huge_swapped()
In reply to Huang YingAs suggested by Dave, we should unify the code path for normal and huge swap support if possible to avoid duplicated code, bugs, etc. and make it easier to review code.
In this patch, the normal/huge code path in swap_page_trans_huge_swapped() is unified, the added and removed lines are same. And the binary size is kept almost same when CONFIG_TRANSPARENT_HUGEPAGE=n.
text data bss dec hex filename
base: 24179 2028 340 26547 67b3 mm/swapfile.o
unified: 24215 2028 340 26583 67d7 mm/swapfile.o
--- mm/swapfile.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/swapfile.c +4 -4
@@ -270,7 +270,10 @@ static inline void cluster_set_null(struct swap_cluster_info *info)static inline bool cluster_is_huge (struct swap_cluster_info *info) { -return info->flags &CLUSTER_FLAG_HUGE ; +if (IS_ENABLED (CONFIG_THP_SWAP )) +return info->flags &CLUSTER_FLAG_HUGE ; +else +return false; }static inline void cluster_clear_huge (struct swap_cluster_info *info)
@@ -1493,9 +1496,6 @@ static bool swap_page_trans_huge_swapped(struct swap_info_struct *si,int i;bool ret = false; -if (!IS_ENABLED (CONFIG_THP_SWAP )) -return swap_swapcount (si, entry) !=0 ; - ci =lock_cluster_or_swap_info (si, offset);if (!ci || !cluster_is_huge (ci)) {if (swap_count (map[roffset]))
Signature · 1 line
2.16.4
Re: [PATCH v3 4/8] swap: Unify normal/huge code path in swap_page_trans_huge_swapped()
mm/swapfile.c static inline void cluster_set_null(struct swap_cluster_info *info) · line 276
Quoted 7 lines
static inline bool
cluster_is_huge(struct swap_cluster_info *info){mm/swapfile.c +4
-1-return info->flags &CLUSTER_FLAG_HUGE ; +if (IS_ENABLED (CONFIG_THP_SWAP )) +return info->flags &CLUSTER_FLAG_HUGE ; +else +return false;
Nitpick: no need for an else after a return:
if (IS_ENABLED(CONFIG_THP_SWAP)) return info->flags & CLUSTER_FLAG_HUGE; return false;
Re: [PATCH v3 4/8] swap: Unify normal/huge code path in swap_page_trans_huge_swapped()
Quoted 10 lines · Christoph Hellwig
mm/swapfile.c static inline void cluster_set_null(struct swap_cluster_info *info) · line 276
Quoted 7 lines
static inline bool
cluster_is_huge(struct swap_cluster_info *info){mm/swapfile.c +4
-1-return info->flags &CLUSTER_FLAG_HUGE ; +if (IS_ENABLED (CONFIG_THP_SWAP )) +return info->flags &CLUSTER_FLAG_HUGE ; +else +return false;Nitpick: no need for an else after a return:
Quoted 3 lines
if (IS_ENABLED(CONFIG_THP_SWAP)) return info->flags & CLUSTER_FLAG_HUGE; return false;
Sure. Will change this in next version.
Best Regards,
Huang, Ying
PATCH v3 5/8 swap: Unify normal/huge code path in put_swap_page()
In reply to Huang YingIn this patch, the normal/huge code path in put_swap_page() and several helper functions are unified to avoid duplicated code, bugs, etc. and make it easier to review the code.
The removed lines are more than added lines. And the binary size is kept exactly same when CONFIG_TRANSPARENT_HUGEPAGE=n.
--- mm/swapfile.c | 83 ++++++++++++++++++++++++++--------------------------------- 1 file changed, 37 insertions(+), 46 deletions(-)
mm/swapfile.c +37 -46
@@ -205,8 +205,16 @@ static void discard_swap_cluster(struct swap_info_struct *si,#ifdef CONFIG_THP_SWAP #define SWAPFILE_CLUSTER HPAGE_PMD_NR + +#define swap_entry_size (size) (size)#else #define SWAPFILE_CLUSTER 256 + +/* +* Define swap_entry_size() as constant to let compiler to optimize +* out some code if !CONFIG_THP_SWAP +*/ +#define swap_entry_size (size) 1#endif #define LATENCY_LIMIT 256
@@ -1253,18 +1261,7 @@ void swap_free(swp_entry_t entry)/* * Called after dropping swapcache to decrease refcnt to swap entries. */ -static void swapcache_free (swp_entry_t entry) -{ -struct swap_info_struct *p; - - p =_swap_info_get (entry); -if (p) { -if (!__swap_entry_free (p, entry,SWAP_HAS_CACHE )) -free_swap_slot (entry); - } -} - -static void swapcache_free_cluster (swp_entry_t entry) +void put_swap_page (struct page *page,swp_entry_t entry) {unsigned long offset =swp_offset (entry);unsigned long idx = offset /SWAPFILE_CLUSTER ;
@@ -1273,39 +1270,41 @@ static void swapcache_free_cluster(swp_entry_t entry)unsigned char *map;unsigned int i, free_entries =0 ;unsigned char val; - -if (!IS_ENABLED (CONFIG_THP_SWAP )) -return ; +int size =swap_entry_size (hpage_nr_pages (page)); si =_swap_info_get (entry);if (!si)return ; - ci =lock_cluster (si, offset); -VM_BUG_ON (!cluster_is_huge (ci)); - map = si->swap_map + offset; -for (i =0 ; i <SWAPFILE_CLUSTER ; i++) { - val = map[i]; -VM_BUG_ON (!(val &SWAP_HAS_CACHE )); -if (val ==SWAP_HAS_CACHE ) - free_entries++; - } -if (!free_entries) { -for (i =0 ; i <SWAPFILE_CLUSTER ; i++) - map[i] &= ~SWAP_HAS_CACHE ; - } -cluster_clear_huge (ci); -unlock_cluster (ci); -if (free_entries ==SWAPFILE_CLUSTER ) { -spin_lock (&si->lock); +if (size ==SWAPFILE_CLUSTER ) { ci =lock_cluster (si, offset); -memset (map,0 ,SWAPFILE_CLUSTER ); +VM_BUG_ON (!cluster_is_huge (ci)); + map = si->swap_map + offset; +for (i =0 ; i <SWAPFILE_CLUSTER ; i++) { + val = map[i]; +VM_BUG_ON (!(val &SWAP_HAS_CACHE )); +if (val ==SWAP_HAS_CACHE ) + free_entries++; + } +if (!free_entries) { +for (i =0 ; i <SWAPFILE_CLUSTER ; i++) + map[i] &= ~SWAP_HAS_CACHE ; + } +cluster_clear_huge (ci);unlock_cluster (ci); -mem_cgroup_uncharge_swap (entry,SWAPFILE_CLUSTER ); -swap_free_cluster (si, idx); -spin_unlock (&si->lock); - }else if (free_entries) { -for (i =0 ; i <SWAPFILE_CLUSTER ; i++, entry.val++) { +if (free_entries ==SWAPFILE_CLUSTER ) { +spin_lock (&si->lock); + ci =lock_cluster (si, offset); +memset (map,0 ,SWAPFILE_CLUSTER ); +unlock_cluster (ci); +mem_cgroup_uncharge_swap (entry,SWAPFILE_CLUSTER ); +swap_free_cluster (si, idx); +spin_unlock (&si->lock); +return ; + } + } +if (size ==1 || free_entries) { +for (i =0 ; i < size; i++, entry.val++) {if (!__swap_entry_free (si, entry,SWAP_HAS_CACHE ))free_swap_slot (entry); }
@@ -1329,14 +1328,6 @@ int split_swap_cluster(swp_entry_t entry) }#endif -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;
Signature · 1 line
2.16.4
PATCH v3 6/8 swap, get_swap_pages: Use entry_size instead of cluster in parameter
In reply to Huang YingAs suggested by Matthew Wilcox, it is better to use "int entry_size" instead of "bool cluster" as parameter to specify whether to operate for huge or normal swap entries. Because this improve the flexibility to support other swap entry size. And Dave Hansen thinks that this improves code readability too.
So in this patch, the "bool cluster" parameter of get_swap_pages() is replaced by "int entry_size".
And nr_swap_entries() trick is used to reduce the binary size when !CONFIG_TRANSPARENT_HUGE_PAGE.
text data bss dec hex filename base 24215 2028 340 26583 67d7 mm/swapfile.o head 24123 2004 340 26467 6763 mm/swapfile.o
--- include/linux/swap.h | 2 +- mm/swap_slots.c | 8 ++++---- mm/swapfile.c | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-)
include/linux/swap.h +1 -1
@@ -443,7 +443,7 @@ 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 get_swap_pages (int n,swp_entry_t swp_entries[],int entry_size);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 );
mm/swap_slots.c +4 -4
@@ -269,8 +269,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 , false, - cache->slots); + cache->nr =get_swap_pages (SWAP_SLOTS_CACHE_SIZE , + cache->slots,1 );return cache->nr; }
@@ -316,7 +316,7 @@ swp_entry_t get_swap_page(struct page *page)if (PageTransHuge (page)) {if (IS_ENABLED (CONFIG_THP_SWAP )) -get_swap_pages (1 , true, &entry); +get_swap_pages (1 , &entry,HPAGE_PMD_NR ); goto out; }
@@ -350,7 +350,7 @@ swp_entry_t get_swap_page(struct page *page) goto out; } -get_swap_pages (1 , false, &entry); +get_swap_pages (1 , &entry,1 ); out:if (mem_cgroup_try_charge_swap (page, entry)) {put_swap_page (page, entry);
mm/swapfile.c +8 -8
@@ -943,18 +943,18 @@ static unsigned long scan_swap_map(struct swap_info_struct *si, } -int get_swap_pages (int n_goal,bool cluster,swp_entry_t swp_entries[]) +int get_swap_pages (int n_goal,swp_entry_t swp_entries[],int entry_size) { -unsigned long nr_pages = cluster ?SWAPFILE_CLUSTER :1 ; +unsigned long size =swap_entry_size (entry_size);struct swap_info_struct *si, *next;long avail_pgs;int n_ret =0 ;int node;/* Only single cluster request supported */ -WARN_ON_ONCE (n_goal >1 && cluster); +WARN_ON_ONCE (n_goal >1 && size ==SWAPFILE_CLUSTER ); - avail_pgs =atomic_long_read (&nr_swap_pages) / nr_pages; + avail_pgs =atomic_long_read (&nr_swap_pages) / size;if (avail_pgs <=0 ) goto noswap;
@@ -964,7 +964,7 @@ int get_swap_pages(int n_goal, bool cluster, swp_entry_t swp_entries[])if (n_goal > avail_pgs) n_goal = avail_pgs; -atomic_long_sub (n_goal * nr_pages, &nr_swap_pages); +atomic_long_sub (n_goal * size, &nr_swap_pages);spin_lock (&swap_avail_lock);
@@ -991,14 +991,14 @@ int get_swap_pages(int n_goal, bool cluster, swp_entry_t swp_entries[])spin_unlock (&si->lock); goto nextsi; } -if (cluster) { +if (size ==SWAPFILE_CLUSTER ) {if (!(si->flags &SWP_FILE )) n_ret =swap_alloc_cluster (si, swp_entries); }else n_ret =scan_swap_map_slots (si,SWAP_HAS_CACHE , n_goal, swp_entries);spin_unlock (&si->lock); -if (n_ret || cluster) +if (n_ret || size ==SWAPFILE_CLUSTER ) goto check_out;pr_debug ("scan_swap_map of si %d failed to find offset\n" , si->type);
@@ -1024,7 +1024,7 @@ int get_swap_pages(int n_goal, bool cluster, swp_entry_t swp_entries[]) check_out:if (n_ret < n_goal) -atomic_long_add ((long )(n_goal - n_ret) * nr_pages, +atomic_long_add ((long )(n_goal - n_ret) * size, &nr_swap_pages); noswap:return n_ret;
Signature · 1 line
2.16.4
PATCH v3 7/8 swap: Add __swap_entry_free_locked()
In reply to Huang YingThe part of __swap_entry_free() with lock held is separated into a new function __swap_entry_free_locked(). Because we want to reuse that piece of code in some other places.
Just mechanical code refactoring, there is no any functional change in this function.
--- mm/swapfile.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
mm/swapfile.c +14 -6
@@ -1184,16 +1184,13 @@ struct swap_info_struct *get_swap_device(swp_entry_t entry)return NULL ; } -static unsigned char __swap_entry_free (struct swap_info_struct *p, -swp_entry_t entry,unsigned char usage) +static unsigned char __swap_entry_free_locked (struct swap_info_struct *p, +unsigned long offset, +unsigned char usage) { -struct swap_cluster_info *ci; -unsigned long offset =swp_offset (entry);unsigned char count;unsigned char has_cache; - ci =lock_cluster_or_swap_info (p, offset); - count = p->swap_map[offset]; has_cache = count &SWAP_HAS_CACHE ;
@@ -1221,6 +1218,17 @@ static unsigned char __swap_entry_free(struct swap_info_struct *p, usage = count | has_cache; p->swap_map[offset] = usage ? :SWAP_HAS_CACHE ; +return usage; +} + +static unsigned char __swap_entry_free (struct swap_info_struct *p, +swp_entry_t entry,unsigned char usage) +{ +struct swap_cluster_info *ci; +unsigned long offset =swp_offset (entry); + + ci =lock_cluster_or_swap_info (p, offset); + usage =__swap_entry_free_locked (p, offset, usage);unlock_cluster_or_swap_info (p, ci);return usage;
Signature · 1 line
2.16.4
PATCH v3 8/8 swap, put_swap_page: Share more between huge/normal code path
In reply to Huang YingIn this patch, locking related code is shared between huge/normal code path in put_swap_page() to reduce code duplication. And free_entries == 0 case is merged into more general free_entries != SWAPFILE_CLUSTER case, because the new locking method makes it easy.
The added lines is same as the removed lines. But the code size is increased when CONFIG_TRANSPARENT_HUGEPAGE=n.
text data bss dec hex filename
base: 24123 2004 340 26467 6763 mm/swapfile.o
unified: 24485 2004 340 26829 68cd mm/swapfile.o
Dig on step deeper with size -A mm/swapfile.o for base and unified kernel and compare the result, yields,
-.text 17723 0 +.text 17835 0 -.orc_unwind_ip 1380 0 +.orc_unwind_ip 1480 0 -.orc_unwind 2070 0 +.orc_unwind 2220 0
-Total 26686
+Total 27048
The total difference is the same. The text segment difference is much
smaller: 112. More difference comes from the ORC unwinder
segments: (1480 + 2220) - (1380 + 2070) = 250. If the frame pointer unwinder is used, this costs nothing.
--- mm/swapfile.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
mm/swapfile.c +10 -10
@@ -1284,8 +1284,8 @@ void put_swap_page(struct page *page, swp_entry_t entry)if (!si)return ; + ci =lock_cluster_or_swap_info (si, offset);if (size ==SWAPFILE_CLUSTER ) { - ci =lock_cluster (si, offset);VM_BUG_ON (!cluster_is_huge (ci)); map = si->swap_map + offset;for (i =0 ; i <SWAPFILE_CLUSTER ; i++) {
@@ -1294,13 +1294,9 @@ void put_swap_page(struct page *page, swp_entry_t entry)if (val ==SWAP_HAS_CACHE ) free_entries++; } -if (!free_entries) { -for (i =0 ; i <SWAPFILE_CLUSTER ; i++) - map[i] &= ~SWAP_HAS_CACHE ; - }cluster_clear_huge (ci); -unlock_cluster (ci);if (free_entries ==SWAPFILE_CLUSTER ) { +unlock_cluster_or_swap_info (si, ci);spin_lock (&si->lock); ci =lock_cluster (si, offset);memset (map,0 ,SWAPFILE_CLUSTER );
@@ -1311,12 +1307,16 @@ void put_swap_page(struct page *page, swp_entry_t entry)return ; } } -if (size ==1 || free_entries) { -for (i =0 ; i < size; i++, entry.val++) { -if (!__swap_entry_free (si, entry,SWAP_HAS_CACHE )) -free_swap_slot (entry); +for (i =0 ; i < size; i++, entry.val++) { +if (!__swap_entry_free_locked (si, offset + i,SWAP_HAS_CACHE )) { +unlock_cluster_or_swap_info (si, ci); +free_swap_slot (entry); +if (i == size -1 ) +return ; +lock_cluster_or_swap_info (si, offset); } } +unlock_cluster_or_swap_info (si, ci); }#ifdef CONFIG_THP_SWAP
Signature · 1 line
2.16.4
These all look sane to me and the size increases are modest and well worth the cleanup.
Feel free to add my ack on these.