linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm/swap_slots.c: don't reset the cache slot after use
@ 2020-03-09  9:09 Wei Yang
  2020-03-10  0:48 ` Andrew Morton
  0 siblings, 1 reply; 6+ messages in thread
From: Wei Yang @ 2020-03-09  9:09 UTC (permalink / raw)
  To: akpm; +Cc: linux-mm, linux-kernel, Wei Yang

Currently we would clear the cache slot if it is used. While this is not
necessary, since this entry would not be used until refilled.

Leave it untouched and assigned the value directly to entry which makes
the code little more neat.

Also this patch merges the else and if, since this is the only case we
refill and repeat swap cache.

Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
---
 mm/swap_slots.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/mm/swap_slots.c b/mm/swap_slots.c
index 63a7b4563a57..ff695df3db26 100644
--- a/mm/swap_slots.c
+++ b/mm/swap_slots.c
@@ -309,7 +309,7 @@ int free_swap_slot(swp_entry_t entry)
 
 swp_entry_t get_swap_page(struct page *page)
 {
-	swp_entry_t entry, *pentry;
+	swp_entry_t entry;
 	struct swap_slots_cache *cache;
 
 	entry.val = 0;
@@ -336,13 +336,10 @@ swp_entry_t get_swap_page(struct page *page)
 		if (cache->slots) {
 repeat:
 			if (cache->nr) {
-				pentry = &cache->slots[cache->cur++];
-				entry = *pentry;
-				pentry->val = 0;
+				entry = cache->slots[cache->cur++];
 				cache->nr--;
-			} else {
-				if (refill_swap_slots_cache(cache))
-					goto repeat;
+			} else if (refill_swap_slots_cache(cache)) {
+				goto repeat;
 			}
 		}
 		mutex_unlock(&cache->alloc_lock);
-- 
2.20.1 (Apple Git-117)



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/swap_slots.c: don't reset the cache slot after use
  2020-03-09  9:09 [PATCH] mm/swap_slots.c: don't reset the cache slot after use Wei Yang
@ 2020-03-10  0:48 ` Andrew Morton
  2020-03-10 18:13   ` Tim Chen
  0 siblings, 1 reply; 6+ messages in thread
From: Andrew Morton @ 2020-03-10  0:48 UTC (permalink / raw)
  To: Wei Yang; +Cc: linux-mm, linux-kernel, Tim Chen

On Mon,  9 Mar 2020 17:09:40 +0800 Wei Yang <richard.weiyang@linux.alibaba.com> wrote:

> Currently we would clear the cache slot if it is used. While this is not
> necessary, since this entry would not be used until refilled.
> 
> Leave it untouched and assigned the value directly to entry which makes
> the code little more neat.
> 
> Also this patch merges the else and if, since this is the only case we
> refill and repeat swap cache.

cc Tim, who can hopefully remember how this code works ;)

> --- a/mm/swap_slots.c
> +++ b/mm/swap_slots.c
> @@ -309,7 +309,7 @@ int free_swap_slot(swp_entry_t entry)
>  
>  swp_entry_t get_swap_page(struct page *page)
>  {
> -	swp_entry_t entry, *pentry;
> +	swp_entry_t entry;
>  	struct swap_slots_cache *cache;
>  
>  	entry.val = 0;
> @@ -336,13 +336,10 @@ swp_entry_t get_swap_page(struct page *page)
>  		if (cache->slots) {
>  repeat:
>  			if (cache->nr) {
> -				pentry = &cache->slots[cache->cur++];
> -				entry = *pentry;
> -				pentry->val = 0;
> +				entry = cache->slots[cache->cur++];
>  				cache->nr--;
> -			} else {
> -				if (refill_swap_slots_cache(cache))
> -					goto repeat;
> +			} else if (refill_swap_slots_cache(cache)) {
> +				goto repeat;
>  			}
>  		}
>  		mutex_unlock(&cache->alloc_lock);
> -- 
> 2.20.1 (Apple Git-117)


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/swap_slots.c: don't reset the cache slot after use
  2020-03-10  0:48 ` Andrew Morton
@ 2020-03-10 18:13   ` Tim Chen
  2020-03-10 22:20     ` Wei Yang
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Chen @ 2020-03-10 18:13 UTC (permalink / raw)
  To: Andrew Morton, Wei Yang; +Cc: linux-mm, linux-kernel

On 3/9/20 5:48 PM, Andrew Morton wrote:
> On Mon,  9 Mar 2020 17:09:40 +0800 Wei Yang <richard.weiyang@linux.alibaba.com> wrote:
> 
>> Currently we would clear the cache slot if it is used. While this is not
>> necessary, since this entry would not be used until refilled.
>>
>> Leave it untouched and assigned the value directly to entry which makes
>> the code little more neat.
>>
>> Also this patch merges the else and if, since this is the only case we
>> refill and repeat swap cache.
> 
> cc Tim, who can hopefully remember how this code works ;)
> 
>> --- a/mm/swap_slots.c
>> +++ b/mm/swap_slots.c
>> @@ -309,7 +309,7 @@ int free_swap_slot(swp_entry_t entry)
>>  
>>  swp_entry_t get_swap_page(struct page *page)
>>  {
>> -	swp_entry_t entry, *pentry;
>> +	swp_entry_t entry;
>>  	struct swap_slots_cache *cache;
>>  
>>  	entry.val = 0;
>> @@ -336,13 +336,10 @@ swp_entry_t get_swap_page(struct page *page)
>>  		if (cache->slots) {
>>  repeat:
>>  			if (cache->nr) {
>> -				pentry = &cache->slots[cache->cur++];
>> -				entry = *pentry;
>> -				pentry->val = 0;

The cache entry was cleared after assignment for defensive programming,  So there's
little chance I will be using a slot that has been assigned to someone else.
When I wrote swap_slots.c, this code was new and I want to make sure
that if something went wrong, and I assigned a swap slot that I shouldn't,
I will be able to detect quickly as I will only be stepping on entry 0.

Otherwise such bug will be harder to detect as we will have two users of some random
swap slot stepping on each other.

I'm okay if we want to get rid of this logic, now that the code has been
working correctly long enough.  But I think is good hygiene to clear the
cached entry after it has been assigned. 

>> +				entry = cache->slots[cache->cur++];
>>  				cache->nr--;
>> -			} else {
>> -				if (refill_swap_slots_cache(cache))
>> -					goto repeat;
>> +			} else if (refill_swap_slots_cache(cache)) {

This change looks fine.
>> +				goto repeat;
>>  			}

Tim


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/swap_slots.c: don't reset the cache slot after use
  2020-03-10 18:13   ` Tim Chen
@ 2020-03-10 22:20     ` Wei Yang
  2020-03-10 23:03       ` Tim Chen
  0 siblings, 1 reply; 6+ messages in thread
From: Wei Yang @ 2020-03-10 22:20 UTC (permalink / raw)
  To: Tim Chen; +Cc: Andrew Morton, Wei Yang, linux-mm, linux-kernel

On Tue, Mar 10, 2020 at 11:13:13AM -0700, Tim Chen wrote:
>On 3/9/20 5:48 PM, Andrew Morton wrote:
>> On Mon,  9 Mar 2020 17:09:40 +0800 Wei Yang <richard.weiyang@linux.alibaba.com> wrote:
>> 
>>> Currently we would clear the cache slot if it is used. While this is not
>>> necessary, since this entry would not be used until refilled.
>>>
>>> Leave it untouched and assigned the value directly to entry which makes
>>> the code little more neat.
>>>
>>> Also this patch merges the else and if, since this is the only case we
>>> refill and repeat swap cache.
>> 
>> cc Tim, who can hopefully remember how this code works ;)
>> 
>>> --- a/mm/swap_slots.c
>>> +++ b/mm/swap_slots.c
>>> @@ -309,7 +309,7 @@ int free_swap_slot(swp_entry_t entry)
>>>  
>>>  swp_entry_t get_swap_page(struct page *page)
>>>  {
>>> -	swp_entry_t entry, *pentry;
>>> +	swp_entry_t entry;
>>>  	struct swap_slots_cache *cache;
>>>  
>>>  	entry.val = 0;
>>> @@ -336,13 +336,10 @@ swp_entry_t get_swap_page(struct page *page)
>>>  		if (cache->slots) {
>>>  repeat:
>>>  			if (cache->nr) {
>>> -				pentry = &cache->slots[cache->cur++];
>>> -				entry = *pentry;
>>> -				pentry->val = 0;
>
>The cache entry was cleared after assignment for defensive programming,  So there's
>little chance I will be using a slot that has been assigned to someone else.
>When I wrote swap_slots.c, this code was new and I want to make sure
>that if something went wrong, and I assigned a swap slot that I shouldn't,
>I will be able to detect quickly as I will only be stepping on entry 0.
>
>Otherwise such bug will be harder to detect as we will have two users of some random
>swap slot stepping on each other.
>
>I'm okay if we want to get rid of this logic, now that the code has been
>working correctly long enough.  But I think is good hygiene to clear the
>cached entry after it has been assigned. 
>

This is fine to keep the logic, while I am wondering whether we need to do
this through pointer. cache->slots[] contain the value, we can get and reset
without pointer.

The following code looks more obvious about the logic.

		entry = cache->slots[cache->cur];
		cache->slots[cache->cur++].val = 0;


>>> +				entry = cache->slots[cache->cur++];
>>>  				cache->nr--;
>>> -			} else {
>>> -				if (refill_swap_slots_cache(cache))
>>> -					goto repeat;
>>> +			} else if (refill_swap_slots_cache(cache)) {
>
>This change looks fine.
>>> +				goto repeat;
>>>  			}
>
>Tim

-- 
Wei Yang
Help you, Help me


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/swap_slots.c: don't reset the cache slot after use
  2020-03-10 22:20     ` Wei Yang
@ 2020-03-10 23:03       ` Tim Chen
  2020-03-11  1:17         ` Wei Yang
  0 siblings, 1 reply; 6+ messages in thread
From: Tim Chen @ 2020-03-10 23:03 UTC (permalink / raw)
  To: Wei Yang; +Cc: Andrew Morton, Wei Yang, linux-mm, linux-kernel

On 3/10/20 3:20 PM, Wei Yang wrote:
> On Tue, Mar 10, 2020 at 11:13:13AM -0700, Tim Chen wrote:
>> On 3/9/20 5:48 PM, Andrew Morton wrote:
>>> On Mon,  9 Mar 2020 17:09:40 +0800 Wei Yang <richard.weiyang@linux.alibaba.com> wrote:
>>>
>>>> Currently we would clear the cache slot if it is used. While this is not
>>>> necessary, since this entry would not be used until refilled.
>>>>
>>>> Leave it untouched and assigned the value directly to entry which makes
>>>> the code little more neat.
>>>>
>>>> Also this patch merges the else and if, since this is the only case we
>>>> refill and repeat swap cache.
>>>
>>> cc Tim, who can hopefully remember how this code works ;)
>>>
>>>> --- a/mm/swap_slots.c
>>>> +++ b/mm/swap_slots.c
>>>> @@ -309,7 +309,7 @@ int free_swap_slot(swp_entry_t entry)
>>>>  
>>>>  swp_entry_t get_swap_page(struct page *page)
>>>>  {
>>>> -	swp_entry_t entry, *pentry;
>>>> +	swp_entry_t entry;
>>>>  	struct swap_slots_cache *cache;
>>>>  
>>>>  	entry.val = 0;
>>>> @@ -336,13 +336,10 @@ swp_entry_t get_swap_page(struct page *page)
>>>>  		if (cache->slots) {
>>>>  repeat:
>>>>  			if (cache->nr) {
>>>> -				pentry = &cache->slots[cache->cur++];
>>>> -				entry = *pentry;
>>>> -				pentry->val = 0;
>>
>> The cache entry was cleared after assignment for defensive programming,  So there's
>> little chance I will be using a slot that has been assigned to someone else.
>> When I wrote swap_slots.c, this code was new and I want to make sure
>> that if something went wrong, and I assigned a swap slot that I shouldn't,
>> I will be able to detect quickly as I will only be stepping on entry 0.
>>
>> Otherwise such bug will be harder to detect as we will have two users of some random
>> swap slot stepping on each other.
>>
>> I'm okay if we want to get rid of this logic, now that the code has been
>> working correctly long enough.  But I think is good hygiene to clear the
>> cached entry after it has been assigned. 
>>
> 
> This is fine to keep the logic, while I am wondering whether we need to do
> this through pointer. cache->slots[] contain the value, we can get and reset
> without pointer.
> 
> The following code looks more obvious about the logic.
> 
> 		entry = cache->slots[cache->cur];
> 		cache->slots[cache->cur++].val = 0;

Yes, this looks pretty good.

Thanks.

Tim




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] mm/swap_slots.c: don't reset the cache slot after use
  2020-03-10 23:03       ` Tim Chen
@ 2020-03-11  1:17         ` Wei Yang
  0 siblings, 0 replies; 6+ messages in thread
From: Wei Yang @ 2020-03-11  1:17 UTC (permalink / raw)
  To: Tim Chen; +Cc: Wei Yang, Andrew Morton, Wei Yang, linux-mm, linux-kernel

On Tue, Mar 10, 2020 at 04:03:07PM -0700, Tim Chen wrote:
>On 3/10/20 3:20 PM, Wei Yang wrote:
>> On Tue, Mar 10, 2020 at 11:13:13AM -0700, Tim Chen wrote:
>>> On 3/9/20 5:48 PM, Andrew Morton wrote:
>>>> On Mon,  9 Mar 2020 17:09:40 +0800 Wei Yang <richard.weiyang@linux.alibaba.com> wrote:
>>>>
>>>>> Currently we would clear the cache slot if it is used. While this is not
>>>>> necessary, since this entry would not be used until refilled.
>>>>>
>>>>> Leave it untouched and assigned the value directly to entry which makes
>>>>> the code little more neat.
>>>>>
>>>>> Also this patch merges the else and if, since this is the only case we
>>>>> refill and repeat swap cache.
>>>>
>>>> cc Tim, who can hopefully remember how this code works ;)
>>>>
>>>>> --- a/mm/swap_slots.c
>>>>> +++ b/mm/swap_slots.c
>>>>> @@ -309,7 +309,7 @@ int free_swap_slot(swp_entry_t entry)
>>>>>  
>>>>>  swp_entry_t get_swap_page(struct page *page)
>>>>>  {
>>>>> -	swp_entry_t entry, *pentry;
>>>>> +	swp_entry_t entry;
>>>>>  	struct swap_slots_cache *cache;
>>>>>  
>>>>>  	entry.val = 0;
>>>>> @@ -336,13 +336,10 @@ swp_entry_t get_swap_page(struct page *page)
>>>>>  		if (cache->slots) {
>>>>>  repeat:
>>>>>  			if (cache->nr) {
>>>>> -				pentry = &cache->slots[cache->cur++];
>>>>> -				entry = *pentry;
>>>>> -				pentry->val = 0;
>>>
>>> The cache entry was cleared after assignment for defensive programming,  So there's
>>> little chance I will be using a slot that has been assigned to someone else.
>>> When I wrote swap_slots.c, this code was new and I want to make sure
>>> that if something went wrong, and I assigned a swap slot that I shouldn't,
>>> I will be able to detect quickly as I will only be stepping on entry 0.
>>>
>>> Otherwise such bug will be harder to detect as we will have two users of some random
>>> swap slot stepping on each other.
>>>
>>> I'm okay if we want to get rid of this logic, now that the code has been
>>> working correctly long enough.  But I think is good hygiene to clear the
>>> cached entry after it has been assigned. 
>>>
>> 
>> This is fine to keep the logic, while I am wondering whether we need to do
>> this through pointer. cache->slots[] contain the value, we can get and reset
>> without pointer.
>> 
>> The following code looks more obvious about the logic.
>> 
>> 		entry = cache->slots[cache->cur];
>> 		cache->slots[cache->cur++].val = 0;
>
>Yes, this looks pretty good.

Thanks, I would rephrase v2.

>
>Thanks.
>
>Tim

-- 
Wei Yang
Help you, Help me


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-03-11  1:17 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-09  9:09 [PATCH] mm/swap_slots.c: don't reset the cache slot after use Wei Yang
2020-03-10  0:48 ` Andrew Morton
2020-03-10 18:13   ` Tim Chen
2020-03-10 22:20     ` Wei Yang
2020-03-10 23:03       ` Tim Chen
2020-03-11  1:17         ` Wei Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).