Skip to contentSkip to conversation

mm/swap_slots.c: Remove unnecessary NULL pointer check

PATCH In review Miaohe Lin 2 participants 3 messages Jan 9, 2021 – Jan 14, 2021 Memory management

lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm/swap_slots.c: Remove unnecessary NULL pointer check

The cache->slots and cache->slots_ret is already checked before we try to drain it. And kvfree can handle the NULL pointer itself. So remove the NULL pointer check here.

---
 mm/swap_slots.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
mm/swap_slots.c +3 -4
index 0357fbe70645..4cf99ce033d0 100644
@@ -178,7 +178,7 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
 		swapcache_free_entries(cache->slots + cache->cur, cache->nr);
 		cache->cur = 0;
 		cache->nr = 0;
-		if (free_slots && cache->slots) {
+		if (free_slots) {
 			kvfree(cache->slots);
 			cache->slots = NULL;
 		}
@@ -188,13 +188,12 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
 		spin_lock_irq(&cache->free_lock);
 		swapcache_free_entries(cache->slots_ret, cache->n_ret);
 		cache->n_ret = 0;
-		if (free_slots && cache->slots_ret) {
+		if (free_slots) {
 			slots = cache->slots_ret;
 			cache->slots_ret = NULL;
 		}
 		spin_unlock_irq(&cache->free_lock);
-		if (slots)
-			kvfree(slots);
+		kvfree(slots);
 	}
 }
 
Signature · 1 line
2.19.1
#1 lore raw

mm/swap_slots.c static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, · line 181

Quoted 10 lines · Miaohe Lin

The cache->slots and cache->slots_ret is already checked before we try to drain it. And kvfree can handle the NULL pointer itself. So remove the NULL pointer check here.

mm/swap_slots.c +1 -1
@@ -178,7 +178,7 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
 		swapcache_free_entries(cache->slots + cache->cur, cache->nr);
 		cache->cur = 0;
 		cache->nr = 0;
-		if (free_slots && cache->slots) {
+		if (free_slots) {

Prove that swapcache_free_entries() doesn't change cache->slots.

mm/swap_slots.c static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, · line 191

Quoted 6 lines
mm/swap_slots.c +1 -1
@@ -188,13 +188,12 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
 		spin_lock_irq(&cache->free_lock);
 		swapcache_free_entries(cache->slots_ret, cache->n_ret);
 		cache->n_ret = 0;
-		if (free_slots && cache->slots_ret) {
+		if (free_slots) {

... or ->slots_ret

mm/swap_slots.c static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, · line 196

Quoted 3 lines
mm/swap_slots.c +1 -2
-		if (slots)
-			kvfree(slots);
+		kvfree(slots);

This is fine.

author
#2 lore raw

Hi:

Quoted 5 lines · Matthew Wilcox
Quoted 3 lines · Miaohe Lin

The cache->slots and cache->slots_ret is already checked before we try to drain it. And kvfree can handle the NULL pointer itself. So remove the NULL pointer check here.

Quoted 9 lines

mm/swap_slots.c static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, · line 181

Quoted 6 lines
mm/swap_slots.c +1 -1
@@ -178,7 +178,7 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
 		swapcache_free_entries(cache->slots + cache->cur, cache->nr);
 		cache->cur = 0;
 		cache->nr = 0;
-		if (free_slots && cache->slots) {
+		if (free_slots) {

Prove that swapcache_free_entries() doesn't change cache->slots.

Yeh... I see. I thought swap_slots_cache_mutex could totally guard against this.

Quoted 9 lines

mm/swap_slots.c static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, · line 191

Quoted 6 lines
mm/swap_slots.c +1 -1
@@ -188,13 +188,12 @@ static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type,
 		spin_lock_irq(&cache->free_lock);
 		swapcache_free_entries(cache->slots_ret, cache->n_ret);
 		cache->n_ret = 0;
-		if (free_slots && cache->slots_ret) {
+		if (free_slots) {

... or ->slots_ret

Quoted 7 lines

mm/swap_slots.c static void drain_slots_cache_cpu(unsigned int cpu, unsigned int type, · line 196

Quoted 3 lines
mm/swap_slots.c +1 -2
-		if (slots)
-			kvfree(slots);
+		kvfree(slots);

This is fine.
.

Many thanks for your review and reply!