Skip to contentSkip to conversation

mm/list_lru: optimize condition of exiting the loop

PATCH In review Hui Su 4 participants 4 messages Oct 28, 2020 – Nov 2, 2020

Subsystems (2)
lore
Share & tools
lore

Hui Su

author
#0 lore raw

PATCH v2 1/1 mm/list_lru: optimize condition of exiting the loop

In list_lru_walk(), nr_to_walk type is 'unsigned long', so nr_to_walk won't be '< 0'.

In list_lru_walk_node(), nr_to_walk type is 'unsigned long', so *nr_to_walk won't be '< 0' too.

We can use '!nr_to_walk' instead of 'nr_to_walk <= 0', which is more precise.

---
 include/linux/list_lru.h | 2 +-
 mm/list_lru.c            | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/list_lru.h +1 -1
index 9dcaa3e582c9..b7bc4a2636b9 100644
@@ -214,7 +214,7 @@ list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
 	for_each_node_state(nid, N_NORMAL_MEMORY) {
 		isolated += list_lru_walk_node(lru, nid, isolate,
 					       cb_arg, &nr_to_walk);
-		if (nr_to_walk <= 0)
+		if (!nr_to_walk)
 			break;
 	}
 	return isolated;
mm/list_lru.c +2 -2
index 5aa6e44bc2ae..35be4de9fd77 100644
@@ -294,7 +294,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
 
 	isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
 				      nr_to_walk);
-	if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
+	if (*nr_to_walk && list_lru_memcg_aware(lru)) {
 		for_each_memcg_cache_index(memcg_idx) {
 			struct list_lru_node *nlru = &lru->node[nid];
 
@@ -304,7 +304,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
 							nr_to_walk);
 			spin_unlock(&nlru->lock);
 
-			if (*nr_to_walk <= 0)
+			if (!*nr_to_walk)
 				break;
 		}
 	}
Signature · 1 line
2.29.0
Acked-by on v2 1/1
#1 lore raw
Quoted 8 lines · Hui Su

In list_lru_walk(), nr_to_walk type is 'unsigned long', so nr_to_walk won't be '< 0'.

In list_lru_walk_node(), nr_to_walk type is 'unsigned long', so *nr_to_walk won't be '< 0' too.

We can use '!nr_to_walk' instead of 'nr_to_walk <= 0', which is more precise.

Yes, imho comparisons that make no sense are only misleading for the readers. Compilers can probably find out easier, so maybe there's no code generation change, but for making it less misleading:

Quoted 1 line
Quoted 41 lines
---
  include/linux/list_lru.h | 2 +-
  mm/list_lru.c            | 4 ++--
  2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/list_lru.h +1 -1
index 9dcaa3e582c9..b7bc4a2636b9 100644
@@ -214,7 +214,7 @@ list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
  	for_each_node_state(nid, N_NORMAL_MEMORY) {
  		isolated += list_lru_walk_node(lru, nid, isolate,
  					       cb_arg, &nr_to_walk);
-		if (nr_to_walk <= 0)
+		if (!nr_to_walk)
  			break;
  	}
  	return isolated;
mm/list_lru.c +2 -2
index 5aa6e44bc2ae..35be4de9fd77 100644
@@ -294,7 +294,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
 
  	isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
  				      nr_to_walk);
-	if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
+	if (*nr_to_walk && list_lru_memcg_aware(lru)) {
  		for_each_memcg_cache_index(memcg_idx) {
  			struct list_lru_node *nlru = &lru->node[nid];
 
@@ -304,7 +304,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
  							nr_to_walk);
  			spin_unlock(&nlru->lock);
 
-			if (*nr_to_walk <= 0)
+			if (!*nr_to_walk)
  				break;
  		}
  	}
Acked-by on v2 1/1
#2 lore raw
In reply to Hui Su
Quoted 42 lines

In list_lru_walk(), nr_to_walk type is 'unsigned long', so nr_to_walk won't be '< 0'.

In list_lru_walk_node(), nr_to_walk type is 'unsigned long', so *nr_to_walk won't be '< 0' too.

We can use '!nr_to_walk' instead of 'nr_to_walk <= 0', which is more precise.

---
 include/linux/list_lru.h | 2 +-
 mm/list_lru.c            | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/list_lru.h +1 -1
index 9dcaa3e582c9..b7bc4a2636b9 100644
@@ -214,7 +214,7 @@ list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
        for_each_node_state(nid, N_NORMAL_MEMORY) {
                isolated += list_lru_walk_node(lru, nid, isolate,
                                               cb_arg, &nr_to_walk);
-               if (nr_to_walk <= 0)
+               if (!nr_to_walk)
                        break;
        }
        return isolated;
mm/list_lru.c +1 -1
index 5aa6e44bc2ae..35be4de9fd77 100644
@@ -294,7 +294,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
 
        isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
                                      nr_to_walk);
-       if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
+       if (*nr_to_walk && list_lru_memcg_aware(lru)) {
                for_each_memcg_cache_index(memcg_idx) {
                        struct list_lru_node *nlru = &lru->node[nid];
 

mm/list_lru.c unsigned long list_lru_walk_node(struct list_lru *lru, int nid, · line 310

Quoted 8 lines
mm/list_lru.c +1 -1
@@ -304,7 +304,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
                                                        nr_to_walk);
                        spin_unlock(&nlru->lock);
 
-                       if (*nr_to_walk <= 0)
+                       if (!*nr_to_walk)
                                break;
                }
        }
Reviewed-by on v2 1/1
#3 lore raw
In reply to Hui Su
Quoted 11 lines · Hui Su

In list_lru_walk(), nr_to_walk type is 'unsigned long', so nr_to_walk won't be '< 0'.

In list_lru_walk_node(), nr_to_walk type is 'unsigned long', so *nr_to_walk won't be '< 0' too.

We can use '!nr_to_walk' instead of 'nr_to_walk <= 0', which is more precise.

Quoted 41 lines
---
 include/linux/list_lru.h | 2 +-
 mm/list_lru.c            | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
include/linux/list_lru.h +1 -1
index 9dcaa3e582c9..b7bc4a2636b9 100644
@@ -214,7 +214,7 @@ list_lru_walk(struct list_lru *lru, list_lru_walk_cb isolate,
        for_each_node_state(nid, N_NORMAL_MEMORY) {
                isolated += list_lru_walk_node(lru, nid, isolate,
                                               cb_arg, &nr_to_walk);
-               if (nr_to_walk <= 0)
+               if (!nr_to_walk)
                        break;
        }
        return isolated;
mm/list_lru.c +2 -3
index 5aa6e44bc2ae..35be4de9fd77 100644
@@ -294,7 +294,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
 
        isolated += list_lru_walk_one(lru, nid, NULL, isolate, cb_arg,
                                      nr_to_walk);
-       if (*nr_to_walk > 0 && list_lru_memcg_aware(lru)) {
+       if (*nr_to_walk && list_lru_memcg_aware(lru)) {
                for_each_memcg_cache_index(memcg_idx) {
                        struct list_lru_node *nlru = &lru->node[nid];
 
@@ -304,7 +304,7 @@ unsigned long list_lru_walk_node(struct list_lru *lru, int nid,
                                                        nr_to_walk);
                        spin_unlock(&nlru->lock);
 
-                       if (*nr_to_walk <= 0)
+                       if (!*nr_to_walk)
                                break;
                }
        }
--
Quoted 3 lines

2.29.0

--
Yours,
Muchun