Skip to contentSkip to conversation

perf tools: Fix possible compiler warnings in hashmap

PATCH Applied Namhyung Kim 2 participants 6 messages Oct 9, 2024 – Oct 16, 2024

Subsystems (2)
lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf tools: Fix possible compiler warnings in hashmap

The hashmap__for_each_entry[_safe] is accessing 'map' as if it's a pointer. But it does without parentheses so passing a static hash map with an ampersand (like &slab_hash below) caused compiler warnings due to unmatched types.

In file included from util/bpf_lock_contention.c:5:
util/bpf_lock_contention.c: In function ‘exit_slab_cache_iter’:
linux/tools/perf/util/hashmap.h:169:32: error: invalid type argument of ‘->’ (have ‘struct hashmap’)

    169 |         for (bkt = 0; bkt < map->cap; bkt++)                                \
        |                                ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
  /home/namhyung/project/linux/tools/perf/util/hashmap.h:170:31: error: invalid type argument of ‘->’ (have ‘struct hashmap’)
    170 |                 for (cur = map->buckets[bkt]; cur; cur = cur->next)
        |                               ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~

---
I've discovered this while prototyping the slab symbolization for perf lock contention. So this code is not available yet but I'd like to fix the problem first.

Also noticed bpf has the same code and the same problem. I'll send a separate patch for them.

 tools/perf/util/hashmap.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/perf/util/hashmap.h +10 -10
index c12f8320e6682d50..0c4f155e8eb745d9 100644
@@ -166,8 +166,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry(map, cur, bkt)				    \
-	for (bkt = 0; bkt < map->cap; bkt++)				    \
-		for (cur = map->buckets[bkt]; cur; cur = cur->next)
+	for (bkt = 0; bkt < (map)->cap; bkt++)				    \
+		for (cur = (map)->buckets[bkt]; cur; cur = cur->next)
 
 /*
  * hashmap__for_each_entry_safe - iterate over all entries in hashmap, safe
@@ -178,8 +178,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry_safe(map, cur, tmp, bkt)		    \
-	for (bkt = 0; bkt < map->cap; bkt++)				    \
-		for (cur = map->buckets[bkt];				    \
+	for (bkt = 0; bkt < (map)->cap; bkt++)				    \
+		for (cur = (map)->buckets[bkt];				    \
 		     cur && ({tmp = cur->next; true; });		    \
 		     cur = tmp)
 
@@ -190,19 +190,19 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @key: key to iterate entries for
  */
 #define hashmap__for_each_key_entry(map, cur, _key)			    \
-	for (cur = map->buckets						    \
-		     ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+	for (cur = (map)->buckets					    \
+		     ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
 		     : NULL;						    \
 	     cur;							    \
 	     cur = cur->next)						    \
-		if (map->equal_fn(cur->key, (_key), map->ctx))
+		if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #define hashmap__for_each_key_entry_safe(map, cur, tmp, _key)		    \
-	for (cur = map->buckets						    \
-		     ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+	for (cur = (map)->buckets					    \
+		     ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
 		     : NULL;						    \
 	     cur && ({ tmp = cur->next; true; });			    \
 	     cur = tmp)							    \
-		if (map->equal_fn(cur->key, (_key), map->ctx))
+		if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #endif /* __LIBBPF_HASHMAP_H */
Signature · 1 line
2.47.0.rc0.187.ge670bccf7e-goog
author maintainer
#1 lore raw

PATCH 2/2 perf tools: Remove unnecessary parentheses

The hashmap API used to require parentheses for the hashmap argument if it's not a pointer type. It's now fixed so let's drop the parentheses.

---
 tools/perf/util/threads.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/perf/util/threads.c +2 -2
index ff2b169e0085ed77..6ca0b178fb6c3a9d 100644
@@ -141,7 +141,7 @@ void threads__remove_all_threads(struct threads *threads)
 
 		down_write(&table->lock);
 		__threads_table_entry__set_last_match(table, NULL);
-		hashmap__for_each_entry_safe((&table->shard), cur, tmp, bkt) {
+		hashmap__for_each_entry_safe(&table->shard, cur, tmp, bkt) {
 			struct thread *old_value;
 
 			hashmap__delete(&table->shard, cur->key, /*old_key=*/NULL, &old_value);
@@ -175,7 +175,7 @@ int threads__for_each_thread(struct threads *threads,
 		size_t bkt;
 
 		down_read(&table->lock);
-		hashmap__for_each_entry((&table->shard), cur, bkt) {
+		hashmap__for_each_entry(&table->shard, cur, bkt) {
 			int rc = fn((struct thread *)cur->pvalue, data);
 
 			if (rc != 0) {
Signature · 1 line
2.47.0.rc0.187.ge670bccf7e-goog
#2 lore raw
In reply to Namhyung Kim
Quoted 23 lines · Namhyung Kim

The hashmap__for_each_entry[_safe] is accessing 'map' as if it's a pointer. But it does without parentheses so passing a static hash map with an ampersand (like &slab_hash below) caused compiler warnings due to unmatched types.

In file included from util/bpf_lock_contention.c:5:
util/bpf_lock_contention.c: In function ‘exit_slab_cache_iter’:
linux/tools/perf/util/hashmap.h:169:32: error: invalid type argument of ‘->’ (have ‘struct hashmap’)

    169 |         for (bkt = 0; bkt < map->cap; bkt++)                                \
        |                                ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
  /home/namhyung/project/linux/tools/perf/util/hashmap.h:170:31: error: invalid type argument of ‘->’ (have ‘struct hashmap’)
    170 |                 for (cur = map->buckets[bkt]; cur; cur = cur->next)
        |                               ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
Quoted 8 lines

---
I've discovered this while prototyping the slab symbolization for perf lock contention. So this code is not available yet but I'd like to fix the problem first.

Also noticed bpf has the same code and the same problem. I'll send a separate patch for them.

Yep, please do. Fixes look good, thanks.

Quoted 59 lines
 tools/perf/util/hashmap.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/perf/util/hashmap.h +10 -11
index c12f8320e6682d50..0c4f155e8eb745d9 100644
@@ -166,8 +166,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry(map, cur, bkt)                             \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt]; cur; cur = cur->next)
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt]; cur; cur = cur->next)
 
 /*
  * hashmap__for_each_entry_safe - iterate over all entries in hashmap, safe
@@ -178,8 +178,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry_safe(map, cur, tmp, bkt)                   \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt];                               \
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt];                             \
                     cur && ({tmp = cur->next; true; });                    \
                     cur = tmp)
 
@@ -190,19 +190,19 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @key: key to iterate entries for
  */
 #define hashmap__for_each_key_entry(map, cur, _key)                        \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur;                                                           \
             cur = cur->next)                                               \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #define hashmap__for_each_key_entry_safe(map, cur, tmp, _key)              \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur && ({ tmp = cur->next; true; });                           \
             cur = tmp)                                                     \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #endif /* __LIBBPF_HASHMAP_H */
--

2.47.0.rc0.187.ge670bccf7e-goog

author maintainer
#3 lore raw
Quoted 29 lines · Andrii Nakryiko
Quoted 28 lines · Namhyung Kim

The hashmap__for_each_entry[_safe] is accessing 'map' as if it's a pointer. But it does without parentheses so passing a static hash map with an ampersand (like &slab_hash below) caused compiler warnings due to unmatched types.

In file included from util/bpf_lock_contention.c:5:
util/bpf_lock_contention.c: In function ‘exit_slab_cache_iter’:
linux/tools/perf/util/hashmap.h:169:32: error: invalid type argument of ‘->’ (have ‘struct hashmap’)

    169 |         for (bkt = 0; bkt < map->cap; bkt++)                                \
        |                                ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
  /home/namhyung/project/linux/tools/perf/util/hashmap.h:170:31: error: invalid type argument of ‘->’ (have ‘struct hashmap’)
    170 |                 for (cur = map->buckets[bkt]; cur; cur = cur->next)
        |                               ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~

---
I've discovered this while prototyping the slab symbolization for perf lock contention. So this code is not available yet but I'd like to fix the problem first.

Quoted 5 lines
Quoted 3 lines

Also noticed bpf has the same code and the same problem. I'll send a separate patch for them.

Yep, please do. Fixes look good, thanks.

Sure will do, can I get your Acked-by for this patch?

Thanks,
Namhyung

Quoted 60 lines
Quoted 60 lines
 tools/perf/util/hashmap.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/perf/util/hashmap.h +10 -11
index c12f8320e6682d50..0c4f155e8eb745d9 100644
@@ -166,8 +166,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry(map, cur, bkt)                             \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt]; cur; cur = cur->next)
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt]; cur; cur = cur->next)
 
 /*
  * hashmap__for_each_entry_safe - iterate over all entries in hashmap, safe
@@ -178,8 +178,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry_safe(map, cur, tmp, bkt)                   \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt];                               \
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt];                             \
                     cur && ({tmp = cur->next; true; });                    \
                     cur = tmp)
 
@@ -190,19 +190,19 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @key: key to iterate entries for
  */
 #define hashmap__for_each_key_entry(map, cur, _key)                        \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur;                                                           \
             cur = cur->next)                                               \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #define hashmap__for_each_key_entry_safe(map, cur, tmp, _key)              \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur && ({ tmp = cur->next; true; });                           \
             cur = tmp)                                                     \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #endif /* __LIBBPF_HASHMAP_H */
--

2.47.0.rc0.187.ge670bccf7e-goog

#4 lore raw
Quoted 31 lines · Namhyung Kim
Quoted 29 lines · Andrii Nakryiko
Quoted 28 lines · Namhyung Kim

The hashmap__for_each_entry[_safe] is accessing 'map' as if it's a pointer. But it does without parentheses so passing a static hash map with an ampersand (like &slab_hash below) caused compiler warnings due to unmatched types.

In file included from util/bpf_lock_contention.c:5:
util/bpf_lock_contention.c: In function ‘exit_slab_cache_iter’:
linux/tools/perf/util/hashmap.h:169:32: error: invalid type argument of ‘->’ (have ‘struct hashmap’)

    169 |         for (bkt = 0; bkt < map->cap; bkt++)                                \
        |                                ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
  /home/namhyung/project/linux/tools/perf/util/hashmap.h:170:31: error: invalid type argument of ‘->’ (have ‘struct hashmap’)
    170 |                 for (cur = map->buckets[bkt]; cur; cur = cur->next)
        |                               ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~

---
I've discovered this while prototyping the slab symbolization for perf lock contention. So this code is not available yet but I'd like to fix the problem first.

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

Also noticed bpf has the same code and the same problem. I'll send a separate patch for them.

Yep, please do. Fixes look good, thanks.

Sure will do, can I get your Acked-by for this patch?

Sure:

Quoted 63 lines

Thanks,
Namhyung

Quoted 60 lines
Quoted 60 lines
 tools/perf/util/hashmap.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)
tools/perf/util/hashmap.h +10 -11
index c12f8320e6682d50..0c4f155e8eb745d9 100644
@@ -166,8 +166,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry(map, cur, bkt)                             \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt]; cur; cur = cur->next)
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt]; cur; cur = cur->next)
 
 /*
  * hashmap__for_each_entry_safe - iterate over all entries in hashmap, safe
@@ -178,8 +178,8 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @bkt: integer used as a bucket loop cursor
  */
 #define hashmap__for_each_entry_safe(map, cur, tmp, bkt)                   \
-       for (bkt = 0; bkt < map->cap; bkt++)                                \
-               for (cur = map->buckets[bkt];                               \
+       for (bkt = 0; bkt < (map)->cap; bkt++)                              \
+               for (cur = (map)->buckets[bkt];                             \
                     cur && ({tmp = cur->next; true; });                    \
                     cur = tmp)
 
@@ -190,19 +190,19 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
  * @key: key to iterate entries for
  */
 #define hashmap__for_each_key_entry(map, cur, _key)                        \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur;                                                           \
             cur = cur->next)                                               \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #define hashmap__for_each_key_entry_safe(map, cur, tmp, _key)              \
-       for (cur = map->buckets                                             \
-                    ? map->buckets[hash_bits(map->hash_fn((_key), map->ctx), map->cap_bits)] \
+       for (cur = (map)->buckets                                           \
+                    ? (map)->buckets[hash_bits((map)->hash_fn((_key), (map)->ctx), (map)->cap_bits)] \
                     : NULL;                                                \
             cur && ({ tmp = cur->next; true; });                           \
             cur = tmp)                                                     \
-               if (map->equal_fn(cur->key, (_key), map->ctx))
+               if ((map)->equal_fn(cur->key, (_key), (map)->ctx))
 
 #endif /* __LIBBPF_HASHMAP_H */
--

2.47.0.rc0.187.ge670bccf7e-goog

author maintainer Applied
#5 lore raw
In reply to Namhyung Kim
Quoted 20 lines · Namhyung Kim

The hashmap__for_each_entry[_safe] is accessing 'map' as if it's a pointer. But it does without parentheses so passing a static hash map with an ampersand (like &slab_hash below) caused compiler warnings due to unmatched types.

In file included from util/bpf_lock_contention.c:5:
util/bpf_lock_contention.c: In function ‘exit_slab_cache_iter’:
linux/tools/perf/util/hashmap.h:169:32: error: invalid type argument of ‘->’ (have ‘struct hashmap’)

    169 |         for (bkt = 0; bkt < map->cap; bkt++)                                \
        |                                ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
  /home/namhyung/project/linux/tools/perf/util/hashmap.h:170:31: error: invalid type argument of ‘->’ (have ‘struct hashmap’)
    170 |                 for (cur = map->buckets[bkt]; cur; cur = cur->next)
        |                               ^~
  util/bpf_lock_contention.c:105:9: note: in expansion of macro ‘hashmap__for_each_entry’
    105 |         hashmap__for_each_entry(&slab_hash, cur, bkt)
        |         ^~~~~~~~~~~~~~~~~~~~~~~
Quoted 1 line

[...]

Applied to perf-tools-next, thanks!

Best regards,
Namhyung