clean up some functions in mm/swap_slots.c
cover clean up some functions in mm/swap_slots.c
When I studied the code of mm/swap_slots.c, I found some places can be improved.
Zhen Lei (3):
mm/swap: simplify alloc_swap_slot_cache()
mm/swap: simplify enable_swap_slots_cache()
mm/swap: remove redundant check for swap_slot_cache_initialized
mm/swap_slots.c | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-)
Signature · 1 line
2.26.0.106.g9fadedd
PATCH 1/3 mm/swap: simplify alloc_swap_slot_cache()
Both "slots" and "slots_ret" are only need to be freed when cache already allocated. Make them closer, seems more clear.
No functional change.
--- mm/swap_slots.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
mm/swap_slots.c +9 -9
@@ -136,9 +136,16 @@ static int alloc_swap_slot_cache(unsigned int cpu)mutex_lock (&swap_slots_cache_mutex); cache = &per_cpu (swp_slots, cpu); -if (cache->slots || cache->slots_ret) +if (cache->slots || cache->slots_ret) {/* cache already allocated */ - goto out; +mutex_unlock (&swap_slots_cache_mutex); + +kvfree (slots); +kvfree (slots_ret); + +return 0 ; + } +if (!cache->lock_initialized) {mutex_init (&cache->alloc_lock);spin_lock_init (&cache->free_lock);
@@ -155,15 +162,8 @@ static int alloc_swap_slot_cache(unsigned int cpu)*/ mb (); cache->slots = slots; - slots =NULL ; cache->slots_ret = slots_ret; - slots_ret =NULL ; -out:mutex_unlock (&swap_slots_cache_mutex); -if (slots) -kvfree (slots); -if (slots_ret) -kvfree (slots_ret);return 0 ; }
Signature · 1 line
2.26.0.106.g9fadedd
Re: [PATCH 1/3] mm/swap: simplify alloc_swap_slot_cache()
Quoted 40 lines · Zhen Lei
Both "slots" and "
slots_ret" are only need to be freed when cache already allocated. Make them closer, seems more clear.No functional change.
Signed-off-by Zhen Lei <[email protected]>--- mm/swap_slots.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)mm/swap_slots.c +9
-3@@ -136,9 +136,16 @@ static int alloc_swap_slot_cache(unsigned int cpu)mutex_lock (&swap_slots_cache_mutex); cache = &per_cpu (swp_slots, cpu); -if (cache->slots || cache->slots_ret) +if (cache->slots || cache->slots_ret) {/* cache already allocated */ - goto out; +mutex_unlock (&swap_slots_cache_mutex); + +kvfree (slots); +kvfree (slots_ret); + +return 0 ; + } +if (!cache->lock_initialized) {mutex_init (&cache->alloc_lock);spin_lock_init (&cache->free_lock);@@ -155,15 +162,8 @@ static int alloc_swap_slot_cache(unsigned int cpu)*/ mb (); cache->slots = slots; - slots =NULL ; cache->slots_ret = slots_ret;
mm/swap_slots.c static int alloc_swap_slot_cache(unsigned int cpu) · line 169
Quoted 10 lines
mm/swap_slots.c +0
-6- slots_ret =NULL ; -out:mutex_unlock (&swap_slots_cache_mutex); -if (slots) -kvfree (slots); -if (slots_ret) -kvfree (slots_ret);return 0 ; }
PATCH 2/3 mm/swap: simplify enable_swap_slots_cache()
In reply to Zhen LeiWhether swap_slot_cache_initialized is true or false, __reenable_swap_slots_cache() is always called. To make this meaning clear, leave only one call to __reenable_swap_slots_cache(). This also make it clearer what extra needs be done when swap_slot_cache_initialized is false.
No functional change.
--- mm/swap_slots.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
mm/swap_slots.c +10 -12
@@ -240,21 +240,19 @@ static int free_slot_cache(unsigned int cpu)int enable_swap_slots_cache (void ) { -int ret =0 ; -mutex_lock (&swap_slots_cache_enable_mutex); -if (swap_slot_cache_initialized) { -__reenable_swap_slots_cache (); - goto out_unlock; - } +if (!swap_slot_cache_initialized) { +int ret; - ret =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"swap_slots_cache" , - alloc_swap_slot_cache, free_slot_cache); -if (WARN_ONCE (ret <0 ,"Cache allocation failed (%s), operating " -"without swap slots cache.\n" , __func__)) - goto out_unlock; + ret =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"swap_slots_cache" , + alloc_swap_slot_cache, free_slot_cache); +if (WARN_ONCE (ret <0 ,"Cache allocation failed (%s), operating " +"without swap slots cache.\n" , __func__)) + goto out_unlock; + + swap_slot_cache_initialized = true; + } - swap_slot_cache_initialized = true;__reenable_swap_slots_cache (); out_unlock:mutex_unlock (&swap_slots_cache_enable_mutex);
Signature · 1 line
2.26.0.106.g9fadedd
Re: [PATCH 2/3] mm/swap: simplify enable_swap_slots_cache()
Quoted 9 lines · Zhen Lei
Whether
swap_slot_cache_initializedis true or false,__reenable_swap_slots_cache()is always called. To make this meaning clear, leave only one call to__reenable_swap_slots_cache(). This also make it clearer what extra needs be done whenswap_slot_cache_initializedis false.No functional change.
Signed-off-by Zhen Lei <[email protected]>
Quoted 41 lines
--- mm/swap_slots.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)mm/swap_slots.c +10
-12@@ -240,21 +240,19 @@ static int free_slot_cache(unsigned int cpu)int enable_swap_slots_cache (void ) { -int ret =0 ; -mutex_lock (&swap_slots_cache_enable_mutex); -if (swap_slot_cache_initialized) { -__reenable_swap_slots_cache (); - goto out_unlock; - } +if (!swap_slot_cache_initialized) { +int ret; - ret =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"swap_slots_cache" , - alloc_swap_slot_cache, free_slot_cache); -if (WARN_ONCE (ret <0 ,"Cache allocation failed (%s), operating " -"without swap slots cache.\n" , __func__)) - goto out_unlock; + ret =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"swap_slots_cache" , + alloc_swap_slot_cache, free_slot_cache); +if (WARN_ONCE (ret <0 ,"Cache allocation failed (%s), operating " +"without swap slots cache.\n" , __func__)) + goto out_unlock; + + swap_slot_cache_initialized = true; + } - swap_slot_cache_initialized = true;__reenable_swap_slots_cache (); out_unlock:mutex_unlock (&swap_slots_cache_enable_mutex);
PATCH 3/3 mm/swap: remove redundant check for swap_slot_cache_initialized
In reply to Zhen LeiBecause enable_swap_slots_cache can only become true in enable_swap_slots_cache(), and depends on swap_slot_cache_initialized is true before. That means, when enable_swap_slots_cache is true, swap_slot_cache_initialized is true also.
So the condition:
"swap_slot_cache_enabled && swap_slot_cache_initialized" can be reduced to "swap_slot_cache_enabled"
And in mathematics:
"!swap_slot_cache_enabled || !swap_slot_cache_initialized" is equal to "!(swap_slot_cache_enabled && swap_slot_cache_initialized)"
So no functional change.
--- mm/swap_slots.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
mm/swap_slots.c +2 -3
@@ -46,8 +46,7 @@ static void __drain_swap_slots_cache(unsigned int type);static void deactivate_swap_slots_cache (void );static void reactivate_swap_slots_cache (void ); -#define use_swap_slot_cache (swap_slot_cache_active && \ - swap_slot_cache_enabled && swap_slot_cache_initialized) +#define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled)#define SLOTS_CACHE 0x1#define SLOTS_CACHE_RET 0x2
@@ -94,7 +93,7 @@ static bool check_cache_active(void) {long pages; -if (!swap_slot_cache_enabled || !swap_slot_cache_initialized) +if (!swap_slot_cache_enabled)return false; pages =get_nr_swap_pages ();
Signature · 1 line
2.26.0.106.g9fadedd
Re: [PATCH 3/3] mm/swap: remove redundant check for swap_slot_cache_initialized
Quoted 25 lines · Zhen Lei
Because
enable_swap_slots_cachecan only become true inenable_swap_slots_cache(), and depends onswap_slot_cache_initializedis true before. That means, whenenable_swap_slots_cacheis true,swap_slot_cache_initializedis true also.So the condition:
"swap_slot_cache_enabled&&swap_slot_cache_initialized" can be reduced to "swap_slot_cache_enabled"And in mathematics:
"!swap_slot_cache_enabled || !swap_slot_cache_initialized" is equal to "!(swap_slot_cache_enabled &&swap_slot_cache_initialized)"So no functional change.
Signed-off-by Zhen Lei <[email protected]>--- mm/swap_slots.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)mm/swap_slots.c +0
-0
mm/swap_slots.c static void __drain_swap_slots_cache(unsigned int type); · line 49
Quoted 6 lines
mm/swap_slots.c +1
-2@@ -46,8 +46,7 @@ static void __drain_swap_slots_cache(unsigned int type);static void deactivate_swap_slots_cache (void );static void reactivate_swap_slots_cache (void ); -#define use_swap_slot_cache (swap_slot_cache_active && \ - swap_slot_cache_enabled && swap_slot_cache_initialized) +#define use_swap_slot_cache (swap_slot_cache_active && swap_slot_cache_enabled)
Yes, swap_slot_cache_enabled does imply swap_slot_cache_initialized in current code. So checking swap_slot_cache_enabled is enough here.
mm/swap_slots.c static bool check_cache_active(void) · line 96
Quoted 9 lines
mm/swap_slots.c +1
-1#define SLOTS_CACHE 0x1#define SLOTS_CACHE_RET 0x2@@ -94,7 +93,7 @@ static bool check_cache_active(void) {long pages; -if (!swap_slot_cache_enabled || !swap_slot_cache_initialized) +if (!swap_slot_cache_enabled)
This simplification is okay. !swap_slot_cache_initialize implies !swap_slot_cache_enabled.
So only !swap_slot_cache_enabled needs to be checked.
mm/swap_slots.c static bool check_cache_active(void) · line 99
Quoted 4 lines
mm/swap_slots.c +0
-0return false; pages =get_nr_swap_pages ();
Hi, all:
Are these patches acceptable?
All these three patches are "Acked-by: Tim Chen <[email protected]>" two months ago.
Quoted 10 lines · Zhen Lei
When I studied the code of
mm/swap_slots.c, I found some places can be improved.Zhen Lei (3):
mm/swap: simplifyalloc_swap_slot_cache()
mm/swap: simplifyenable_swap_slots_cache()
mm/swap: remove redundant check forswap_slot_cache_initializedmm/swap_slots.c | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-)