Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1694035 > unrolled thread

[PATCH 2/2] mm/swap: Remove lock_initialized flag from swap_slots_cache

Started byTim Chen <tim.c.chen@linux.intel.com>
First post2017-07-22 00:50 +0200
Last post2017-07-24 19:00 +0200
Articles 3 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH 2/2] mm/swap: Remove lock_initialized flag from swap_slots_cache Tim Chen <tim.c.chen@linux.intel.com> - 2017-07-22 00:50 +0200
    Re: [PATCH 2/2] mm/swap: Remove lock_initialized flag from swap_slots_cache "Huang\, Ying" <ying.huang@intel.com> - 2017-07-24 04:20 +0200
      Re: [PATCH 2/2] mm/swap: Remove lock_initialized flag from  swap_slots_cache Tim Chen <tim.c.chen@linux.intel.com> - 2017-07-24 19:00 +0200

#1694035 — [PATCH 2/2] mm/swap: Remove lock_initialized flag from swap_slots_cache

FromTim Chen <tim.c.chen@linux.intel.com>
Date2017-07-22 00:50 +0200
Subject[PATCH 2/2] mm/swap: Remove lock_initialized flag from swap_slots_cache
Message-ID<u5OUa-2dk-9@gated-at.bofh.it>
We will only reach the lock initialization code
in alloc_swap_slot_cache when the cpu's swap_slots_cache's slots
have not been allocated and swap_slots_cache has not been initialized
previously.  So the lock_initialized check is redundant and unnecessary.
Remove lock_initialized flag from swap_slots_cache to save memory.

Reported-by: Wenwei Tao <wenwei.tww@alibaba-inc.com>
Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
---
 include/linux/swap_slots.h | 1 -
 mm/swap_slots.c            | 9 ++++-----
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/include/linux/swap_slots.h b/include/linux/swap_slots.h
index 6ef92d1..a75c30b 100644
--- a/include/linux/swap_slots.h
+++ b/include/linux/swap_slots.h
@@ -10,7 +10,6 @@
 #define THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE	(2*SWAP_SLOTS_CACHE_SIZE)
 
 struct swap_slots_cache {
-	bool		lock_initialized;
 	struct mutex	alloc_lock; /* protects slots, nr, cur */
 	swp_entry_t	*slots;
 	int		nr;
diff --git a/mm/swap_slots.c b/mm/swap_slots.c
index 4c5457c..c039e6c 100644
--- a/mm/swap_slots.c
+++ b/mm/swap_slots.c
@@ -140,11 +140,10 @@ static int alloc_swap_slot_cache(unsigned int cpu)
 	if (cache->slots || cache->slots_ret)
 		/* cache already allocated */
 		goto out;
-	if (!cache->lock_initialized) {
-		mutex_init(&cache->alloc_lock);
-		spin_lock_init(&cache->free_lock);
-		cache->lock_initialized = true;
-	}
+
+	mutex_init(&cache->alloc_lock);
+	spin_lock_init(&cache->free_lock);
+
 	cache->nr = 0;
 	cache->cur = 0;
 	cache->n_ret = 0;
-- 
2.9.4

[toc] | [next] | [standalone]


#1694404

From"Huang\, Ying" <ying.huang@intel.com>
Date2017-07-24 04:20 +0200
Message-ID<u6B8u-79T-9@gated-at.bofh.it>
In reply to#1694035
Hi, Tim,

Tim Chen <tim.c.chen@linux.intel.com> writes:

> We will only reach the lock initialization code
> in alloc_swap_slot_cache when the cpu's swap_slots_cache's slots
> have not been allocated and swap_slots_cache has not been initialized
> previously.  So the lock_initialized check is redundant and unnecessary.
> Remove lock_initialized flag from swap_slots_cache to save memory.

Is there a race condition with CPU offline/online when preempt is enabled?

CPU A                                   CPU B
-----                                   -----
                                        get_swap_page()
                                          get cache[B], cache[B]->slots != NULL
                                          preempted and moved to CPU A
                                        be offlined
                                        be onlined
                                          alloc_swap_slot_cache()
mutex_lock(cache[B]->alloc_lock)
                                            mutex_init(cache[B]->alloc_lock) !!!

The cache[B]->alloc_lock will be reinitialized when it is still held.

Best Regards,
Huang, Ying

> Reported-by: Wenwei Tao <wenwei.tww@alibaba-inc.com>
> Signed-off-by: Tim Chen <tim.c.chen@linux.intel.com>
> ---
>  include/linux/swap_slots.h | 1 -
>  mm/swap_slots.c            | 9 ++++-----
>  2 files changed, 4 insertions(+), 6 deletions(-)
>
> diff --git a/include/linux/swap_slots.h b/include/linux/swap_slots.h
> index 6ef92d1..a75c30b 100644
> --- a/include/linux/swap_slots.h
> +++ b/include/linux/swap_slots.h
> @@ -10,7 +10,6 @@
>  #define THRESHOLD_DEACTIVATE_SWAP_SLOTS_CACHE	(2*SWAP_SLOTS_CACHE_SIZE)
>  
>  struct swap_slots_cache {
> -	bool		lock_initialized;
>  	struct mutex	alloc_lock; /* protects slots, nr, cur */
>  	swp_entry_t	*slots;
>  	int		nr;
> diff --git a/mm/swap_slots.c b/mm/swap_slots.c
> index 4c5457c..c039e6c 100644
> --- a/mm/swap_slots.c
> +++ b/mm/swap_slots.c
> @@ -140,11 +140,10 @@ static int alloc_swap_slot_cache(unsigned int cpu)
>  	if (cache->slots || cache->slots_ret)
>  		/* cache already allocated */
>  		goto out;
> -	if (!cache->lock_initialized) {
> -		mutex_init(&cache->alloc_lock);
> -		spin_lock_init(&cache->free_lock);
> -		cache->lock_initialized = true;
> -	}
> +
> +	mutex_init(&cache->alloc_lock);
> +	spin_lock_init(&cache->free_lock);
> +
>  	cache->nr = 0;
>  	cache->cur = 0;
>  	cache->n_ret = 0;

[toc] | [prev] | [next] | [standalone]


#1694891 — Re: [PATCH 2/2] mm/swap: Remove lock_initialized flag from swap_slots_cache

FromTim Chen <tim.c.chen@linux.intel.com>
Date2017-07-24 19:00 +0200
SubjectRe: [PATCH 2/2] mm/swap: Remove lock_initialized flag from swap_slots_cache
Message-ID<u6OS5-7yA-7@gated-at.bofh.it>
In reply to#1694404
On 07/23/2017 07:15 PM, Huang, Ying wrote:
> Hi, Tim,
> 
> Tim Chen <tim.c.chen@linux.intel.com> writes:
> 
>> We will only reach the lock initialization code
>> in alloc_swap_slot_cache when the cpu's swap_slots_cache's slots
>> have not been allocated and swap_slots_cache has not been initialized
>> previously.  So the lock_initialized check is redundant and unnecessary.
>> Remove lock_initialized flag from swap_slots_cache to save memory.
> 
> Is there a race condition with CPU offline/online when preempt is enabled?
> 
> CPU A                                   CPU B
> -----                                   -----
>                                         get_swap_page()
>                                           get cache[B], cache[B]->slots != NULL
>                                           preempted and moved to CPU A
>                                         be offlined
>                                         be onlined
>                                           alloc_swap_slot_cache()
> mutex_lock(cache[B]->alloc_lock)
>                                             mutex_init(cache[B]->alloc_lock) !!!
> 
> The cache[B]->alloc_lock will be reinitialized when it is still held.

Looks like for this case the lock_initialized flag is still needed
to prevent such races and prevent re-initialization of taken locks.

Okay, let's scrap patch 2.

Thanks.

Tim

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web