Skip to contentSkip to conversation

mm/swap_slots.c: don't reset the cache slot after use

PATCH In review Wei Yang 4 participants 6 messages Mar 9, 2020 – Mar 11, 2020 Memory management

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 mm/swap_slots.c: don't reset the cache slot after use

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.

---
 mm/swap_slots.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
mm/swap_slots.c +4 -7
index 63a7b4563a57..ff695df3db26 100644
@@ -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);
Signature · 1 line
2.20.1 (Apple Git-117)
maintainer
#1 lore raw
Quoted 8 lines · On Mon, 9 Mar 2020 17:09:40 +0800 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.

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

Quoted 30 lines
mm/swap_slots.c +4 -7
@@ -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);
Signature · 1 line
2.20.1 (Apple Git-117)
#2 lore raw
Quoted 23 lines · Andrew Morton
Quoted 8 lines · On Mon, 9 Mar 2020 17:09:40 +0800 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.

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

Quoted 10 lines
mm/swap_slots.c +1 -1
@@ -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;
 
Quoted 8 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 341

Quoted 8 lines
mm/swap_slots.c +0 -3
 	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.

Quoted 6 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 341

Quoted 6 lines
mm/swap_slots.c +2 -3
+				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.

Quoted 2 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 343

Quoted 2 lines
mm/swap_slots.c +1 -0
+				goto repeat;
 			}

Tim

#3 lore raw
Quoted 39 lines · Tim Chen
Quoted 23 lines · Andrew Morton
Quoted 8 lines · On Mon, 9 Mar 2020 17:09:40 +0800 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.

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

Quoted 10 lines
mm/swap_slots.c +1 -1
@@ -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;
 
Quoted 8 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 341

Quoted 8 lines
mm/swap_slots.c +0 -3
 	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.

Quoted 7 lines

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;
Quoted 12 lines
Quoted 6 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 341

Quoted 6 lines
mm/swap_slots.c +2 -3
+				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.

Quoted 2 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 343

Quoted 2 lines
mm/swap_slots.c +1 -0
+				goto repeat;
 			}

Tim

Signature · 2 lines
Wei Yang
Help you, Help me
#4 lore raw
Quoted 48 lines · Wei Yang
Quoted 39 lines · Tim Chen
Quoted 23 lines · Andrew Morton
Quoted 8 lines · On Mon, 9 Mar 2020 17:09:40 +0800 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.

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

Quoted 10 lines
mm/swap_slots.c +1 -1
@@ -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;
 
Quoted 8 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 341

Quoted 8 lines
mm/swap_slots.c +0 -3
 	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.

Quoted 7 lines

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.

Quoted 8 lines

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

author
#5 lore raw
Quoted 53 lines · Tim Chen
Quoted 52 lines · Wei Yang
Quoted 39 lines · Tim Chen
Quoted 23 lines · Andrew Morton
Quoted 8 lines · On Mon, 9 Mar 2020 17:09:40 +0800 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.

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

Quoted 10 lines
mm/swap_slots.c +1 -1
@@ -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;
 
Quoted 8 lines

mm/swap_slots.c swp_entry_t get_swap_page(struct page *page) · line 341

Quoted 8 lines
mm/swap_slots.c +0 -3
 	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.

Quoted 7 lines

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.

Quoted 6 lines
Quoted 4 lines

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.

Quoted 4 lines

Thanks.

Tim

Signature · 2 lines
Wei Yang
Help you, Help me