Skip to contentSkip to conversation

rhashtable: Fix safe iterator cursor substitution

PATCH New Avi Weiss 1 participant 1 message Nov 21

Subsystems (2)
lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 rhashtable: Fix safe iterator cursor substitution

rht_for_each_entry_safe() uses next as both a macro parameter and the rhash_head member name. Passing a cursor with another name substitutes that name into pos->next, producing a nonexistent struct member and a compilation failure.

Rename the cursor parameter to _next to preserve the pos->next member accesses regardless of the caller's variable name.

Fixes 88d6ed15acff ("rhashtable: Convert bucket iterators to take table and index")

---

Alternative approaches considered:
- Use a differently named cursor parameter such as next_node. This is equivalent in behavior; _next keeps the change smaller.
- Remove the unused macro. That avoids the broken expansion but also removes an existing interface potentially used outside the tree.

Validation:
- Original macro: next compiles; saved, tmp and next_entry fail.
- Corrected macro: all four names compile in the isolated source probe.
- The original next expansion is byte-identical after the correction.
- No in-tree expanding consumer was found in the tested base revision.
- Full kernel Kbuild, runtime and external consumers were not tested.

 include/linux/rhashtable.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
include/linux/rhashtable.h +5 -5
index 70dac9214617..00838bf88117 100644
@@ -482,7 +482,7 @@ static inline void rht_assign_unlock(struct bucket_table *tbl,
  * rht_for_each_entry_safe - safely iterate over hash chain of given type
  * @tpos:	the type * to use as a loop cursor.
  * @pos:	the &struct rhash_head to use as a loop cursor.
- * @next:	the &struct rhash_head to use as next in loop cursor.
+ * @_next:	the &struct rhash_head to use as next in loop cursor.
  * @tbl:	the &struct bucket_table
  * @hash:	the hash value / bucket index
  * @member:	name of the &struct rhash_head within the hashable struct.
@@ -490,13 +490,13 @@ static inline void rht_assign_unlock(struct bucket_table *tbl,
  * This hash chain list-traversal primitive allows for the looped code to
  * remove the loop cursor from the list.
  */
-#define rht_for_each_entry_safe(tpos, pos, next, tbl, hash, member)	      \
+#define rht_for_each_entry_safe(tpos, pos, _next, tbl, hash, member)	      \
 	for (pos = rht_ptr(rht_bucket(tbl, hash), tbl, hash),		      \
-	     next = !rht_is_a_nulls(pos) ?				      \
+	     _next = !rht_is_a_nulls(pos) ?				      \
 		       rht_dereference_bucket(pos->next, tbl, hash) : NULL;   \
 	     (!rht_is_a_nulls(pos)) && rht_entry(tpos, pos, member);	      \
-	     pos = next,						      \
-	     next = !rht_is_a_nulls(pos) ?				      \
+	     pos = _next,						      \
+	     _next = !rht_is_a_nulls(pos) ?				      \
 		       rht_dereference_bucket(pos->next, tbl, hash) : NULL)
 
 /**

base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854

Signature · 1 line
2.43.0