Skip to contentSkip to conversation

hashtable: remove a redundant check in hash_for_each_xxx()

PATCH In review Wei Yang 3 participants 6 messages Oct 6, 2021 – Oct 7, 2021

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 hashtable: remove a redundant check in hash_for_each_xxx()

The three hash_for_each_xxx() helper iterate the hash table with help of hlist_for_each_entry_xxx(), which breaks the loop only when obj is NULL.

This means the check during each iteration is redundant. This patch removes it.

---
 include/linux/hashtable.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
include/linux/hashtable.h +3 -6
index f6c666730b8c..a15719ed303f 100644
@@ -124,8 +124,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each(name, bkt, obj, member)				\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry(obj, &name[bkt], member)
 
 /**
@@ -136,8 +135,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each_rcu(name, bkt, obj, member)			\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry_rcu(obj, &name[bkt], member)
 
 /**
@@ -150,8 +148,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each_safe(name, bkt, tmp, obj, member)			\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
 
 /**
Signature · 1 line
2.23.0
#1 lore raw
Quoted 6 lines · Wei Yang

The three hash_for_each_xxx() helper iterate the hash table with help of hlist_for_each_entry_xxx(), which breaks the loop only when obj is NULL.

This means the check during each iteration is redundant. This patch removes it.

Are you sure that the compiler didn't already remove it? Is the code output the same or different with this change?

How did you test this?

thanks,

greg k-h

#2 lore raw
In reply to Wei Yang
Quoted 17 lines · Wei Yang

The three hash_for_each_xxx() helper iterate the hash table with help of hlist_for_each_entry_xxx(), which breaks the loop only when obj is NULL.

This means the check during each iteration is redundant. This patch removes it.

---
 include/linux/hashtable.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
include/linux/hashtable.h +0 -0
index f6c666730b8c..a15719ed303f 100644

include/linux/hashtable.h static inline void hash_del_rcu(struct hlist_node *node) · line 128

Quoted 7 lines
include/linux/hashtable.h +1 -2
@@ -124,8 +124,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each(name, bkt, obj, member)				\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry(obj, &name[bkt], member)

I think you are missing an important property of this code. What we have here is a new loop command (hash_for_each()) that is constructed from 2 nested loops. This sort of construct is in general difficult to use because in C it is common to use "break" to exit a loop early. 'break' cannot exit two levels of loop though. So if you aren't careful, doing something like

  hash_for_each() {
     do something
     if (some test)
        break;
  }

might not do what you expect. The 'break' will exit the inner loop, but not the outer loop. That could easily lead to buggy code.

But this macro *is* careful. If the loop body *does* use break, then the inner loop will abort but 'obj' will still be non-NULL. The test for NULL in the outer loop causes the outer loop to abort too - as the programmer probably expected.

So by removing the 'obj == NULL' test, you would cause any usage which breaks out of the loop to now be incorrect.

I recommend that instead of this patch, you provide a patch which improves the documentation to make this clear. e.g.

Note: it is safe to 'break' out of this loop even though it is a two nested loops. The 'obj == NULL' test ensures that when the inner loop is broken, the outer loop will break too.

Thanks,
NeilBrown

Quoted 26 lines
include/linux/hashtable.h +2 -4
 /**
@@ -136,8 +135,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each_rcu(name, bkt, obj, member)			\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry_rcu(obj, &name[bkt], member)
 
 /**
@@ -150,8 +148,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each_safe(name, bkt, tmp, obj, member)			\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
 
 /**
Signature · 1 line
2.23.0
author
#3 lore raw
Quoted 39 lines · NeilBrown
Quoted 17 lines · Wei Yang

The three hash_for_each_xxx() helper iterate the hash table with help of hlist_for_each_entry_xxx(), which breaks the loop only when obj is NULL.

This means the check during each iteration is redundant. This patch removes it.

---
 include/linux/hashtable.h | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
include/linux/hashtable.h +0 -0
index f6c666730b8c..a15719ed303f 100644

include/linux/hashtable.h static inline void hash_del_rcu(struct hlist_node *node) · line 128

Quoted 7 lines
include/linux/hashtable.h +1 -2
@@ -124,8 +124,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each(name, bkt, obj, member)				\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry(obj, &name[bkt], member)

I think you are missing an important property of this code. What we have here is a new loop command (hash_for_each()) that is constructed from 2 nested loops. This sort of construct is in general difficult to use because in C it is common to use "break" to exit a loop early. 'break' cannot exit two levels of loop though. So if you aren't careful, doing something like

hash_for_each() {
do something
if (some test)
break;
}

Quoted 8 lines

might not do what you expect. The 'break' will exit the inner loop, but not the outer loop. That could easily lead to buggy code.

But this macro *is* careful. If the loop body *does* use break, then the inner loop will abort but 'obj' will still be non-NULL. The test for NULL in the outer loop causes the outer loop to abort too - as the programmer probably expected.

Thanks for pointing out. I missed this case.

Quoted 10 lines

So by removing the 'obj == NULL' test, you would cause any usage which breaks out of the loop to now be incorrect.

I recommend that instead of this patch, you provide a patch which improves the documentation to make this clear. e.g.

Note: it is safe to 'break' out of this loop even though it is a two nested loops. The 'obj == NULL' test ensures that when the inner loop is broken, the outer loop will break too.

Here is a draft patch based on you comment:

include/linux/hashtable.h +7 -0
index f6c666730b8c..2ff4cb5e6a22 100644
@@ -116,6 +116,13 @@ static inline void hash_del_rcu(struct hlist_node *node)
 	hlist_del_init_rcu(node);
 }
 
+/**
+ * Note: the following three hash_for_each[_xxx] helpers introduce a new loop
+ * command that is constructed from 2 nested loops. It is safe to 'break' out
+ * of this loop even though it is a two nested loops.  The 'obj == NULL' test
+ * ensures that when the inner loop is broken, the outer loop will break too.
+ */
+
 /**
  * hash_for_each - iterate over a hashtable
  * @name: hashtable to iterate

If you feel good, I would like to add

Quoted 30 lines

Thanks,
NeilBrown

Quoted 26 lines
include/linux/hashtable.h +2 -4
 /**
@@ -136,8 +135,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each_rcu(name, bkt, obj, member)			\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry_rcu(obj, &name[bkt], member)
 
 /**
@@ -150,8 +148,7 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @member: the name of the hlist_node within the struct
  */
 #define hash_for_each_safe(name, bkt, tmp, obj, member)			\
-	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
-			(bkt)++)\
+	for ((bkt) = 0, obj = NULL; (bkt) < HASH_SIZE(name); (bkt)++)	\
 		hlist_for_each_entry_safe(obj, tmp, &name[bkt], member)
 
 /**
Signature · 1 line
2.23.0
Signature · 2 lines
Wei Yang
Help you, Help me
Reviewed-by
#4 lore raw
Quoted 23 lines · Wei Yang

Here is a draft patch based on you comment:

include/linux/hashtable.h +7 -0
index f6c666730b8c..2ff4cb5e6a22 100644
@@ -116,6 +116,13 @@ static inline void hash_del_rcu(struct hlist_node *node)
 	hlist_del_init_rcu(node);
 }
 
+/**
+ * Note: the following three hash_for_each[_xxx] helpers introduce a new loop
+ * command that is constructed from 2 nested loops. It is safe to 'break' out
+ * of this loop even though it is a two nested loops.  The 'obj == NULL' test
+ * ensures that when the inner loop is broken, the outer loop will break too.
+ */
+
 /**
  * hash_for_each - iterate over a hashtable
  * @name: hashtable to iterate
Quoted 3 lines

If you feel good, I would like to add

That's definitely an improvement.

I'd probably put it in the kernel-doc comment for hash_for_each, then in the other two just put the "it is safe" bit. Something like the following. But I don't feel strongly about it.
I'm happy to say

for your patch.

Thanks,
NeilBrown

include/linux/hashtable.h +8 -0
index f6c666730b8c..61db940c9501 100644
@@ -122,6 +122,10 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @bkt: integer to use as bucket loop cursor
  * @obj: the type * to use as a loop cursor for each entry
  * @member: the name of the hlist_node within the struct
+ *
+ * Note: It is safe to 'break' out of this loop even though it is a two
+ * nested loops.  The 'obj == NULL' test ensures that when the inner loop
+ * is broken, the outer loop will break too.
  */
 #define hash_for_each(name, bkt, obj, member)				\
 	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
@@ -134,6 +138,8 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @bkt: integer to use as bucket loop cursor
  * @obj: the type * to use as a loop cursor for each entry
  * @member: the name of the hlist_node within the struct
+ *
+ * It is safe to 'break' out of this loop.
  */
 #define hash_for_each_rcu(name, bkt, obj, member)			\
 	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
@@ -148,6 +154,8 @@ static inline void hash_del_rcu(struct hlist_node *node)
  * @tmp: a &struct hlist_node used for temporary storage
  * @obj: the type * to use as a loop cursor for each entry
  * @member: the name of the hlist_node within the struct
+ *
+ * It is safe to 'break' out of this loop.
  */
 #define hash_for_each_safe(name, bkt, tmp, obj, member)			\
 	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
author
#5 lore raw
Quoted 30 lines · NeilBrown
Quoted 23 lines · Wei Yang

Here is a draft patch based on you comment:

include/linux/hashtable.h +7 -0
index f6c666730b8c..2ff4cb5e6a22 100644
@@ -116,6 +116,13 @@ static inline void hash_del_rcu(struct hlist_node *node)
 	hlist_del_init_rcu(node);
 }
 
+/**
+ * Note: the following three hash_for_each[_xxx] helpers introduce a new loop
+ * command that is constructed from 2 nested loops. It is safe to 'break' out
+ * of this loop even though it is a two nested loops.  The 'obj == NULL' test
+ * ensures that when the inner loop is broken, the outer loop will break too.
+ */
+
 /**
  * hash_for_each - iterate over a hashtable
  * @name: hashtable to iterate
Quoted 3 lines

If you feel good, I would like to add

That's definitely an improvement.

Quoted 6 lines

I'd probably put it in the kernel-doc comment for hash_for_each, then in the other two just put the "it is safe" bit. Something like the following. But I don't feel strongly about it.
I'm happy to say

Thanks for your detailed instruction :-)

Quoted 39 lines

for your patch.

Thanks,
NeilBrown

include/linux/hashtable.h +8 -0
index f6c666730b8c..61db940c9501 100644
@@ -122,6 +122,10 @@ static inline void hash_del_rcu(struct hlist_node *node)
 * @bkt: integer to use as bucket loop cursor
 * @obj: the type * to use as a loop cursor for each entry
 * @member: the name of the hlist_node within the struct
+ *
+ * Note: It is safe to 'break' out of this loop even though it is a two
+ * nested loops.  The 'obj == NULL' test ensures that when the inner loop
+ * is broken, the outer loop will break too.
 */
 #define hash_for_each(name, bkt, obj, member)				\
 	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
@@ -134,6 +138,8 @@ static inline void hash_del_rcu(struct hlist_node *node)
 * @bkt: integer to use as bucket loop cursor
 * @obj: the type * to use as a loop cursor for each entry
 * @member: the name of the hlist_node within the struct
+ *
+ * It is safe to 'break' out of this loop.
 */
 #define hash_for_each_rcu(name, bkt, obj, member)			\
 	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
@@ -148,6 +154,8 @@ static inline void hash_del_rcu(struct hlist_node *node)
 * @tmp: a &struct hlist_node used for temporary storage
 * @obj: the type * to use as a loop cursor for each entry
 * @member: the name of the hlist_node within the struct
+ *
+ * It is safe to 'break' out of this loop.
 */
 #define hash_for_each_safe(name, bkt, tmp, obj, member)			\
 	for ((bkt) = 0, obj = NULL; obj == NULL && (bkt) < HASH_SIZE(name);\
Signature · 2 lines
Wei Yang
Help you, Help me