[PATCH v1 05/26] perf hashmap: Always build perf's hashmap and avoid libbpf symbol conflict

From: Ian Rogers

Date: Fri Nov 13 2026 - 17:18:36 EST


Previously tools/perf/util/hashmap.o was only compiled when libbpf was
disabled or linked dynamically; with static libbpf.a, perf linked
against libbpf's internal hashmap symbols.

To allow perf's hashmap implementation to diverge from libbpf's without
colliding with libbpf.a's global hashmap symbols or churning callers:
- Always build util/hashmap.o in tools/perf/util/Build.
- Rename the backing C functions in util/hashmap.c to __hashmap__*
(__hashmap__init, __hashmap__new, __hashmap__clear, __hashmap__free,
__hashmap__size, __hashmap__capacity, __hashmap__insert,
__hashmap__find, and __hashmap__delete, along with the internal
__hashmap__* helpers).
- Keep the existing struct hashmap and hashmap__* header wrappers in
util/hashmap.h forwarding to those functions, and update the single
direct hashmap_find() call in hwmon_pmu.c to hashmap__find().

Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/util/Build | 9 ----
tools/perf/util/hashmap.c | 70 +++++++++++++++----------------
tools/perf/util/hashmap.h | 83 ++++++++++++++++++++++++++-----------
tools/perf/util/hwmon_pmu.c | 2 +-
4 files changed, 94 insertions(+), 70 deletions(-)

diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 2c1f880c4c47..d9090f217301 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -197,16 +197,7 @@ perf-util-$(CONFIG_LIBELF) += symbol-elf.o
perf-util-$(CONFIG_LIBELF) += probe-file.o
perf-util-$(CONFIG_LIBELF) += probe-event.o

-ifdef CONFIG_LIBBPF_DYNAMIC
- hashmap := 1
-endif
-ifndef CONFIG_LIBBPF
- hashmap := 1
-endif
-
-ifdef hashmap
perf-util-y += hashmap.o
-endif

ifndef CONFIG_LIBELF
perf-util-y += symbol-minimal.o
diff --git a/tools/perf/util/hashmap.c b/tools/perf/util/hashmap.c
index 140ee4055676..b6f80a92f5dd 100644
--- a/tools/perf/util/hashmap.c
+++ b/tools/perf/util/hashmap.c
@@ -21,22 +21,22 @@
/* start with 4 buckets */
#define HASHMAP_MIN_CAP_BITS 2

-static void hashmap_add_entry(struct hashmap_entry **pprev,
- struct hashmap_entry *entry)
+static void __hashmap__add_entry(struct hashmap_entry **pprev,
+ struct hashmap_entry *entry)
{
entry->next = *pprev;
*pprev = entry;
}

-static void hashmap_del_entry(struct hashmap_entry **pprev,
- struct hashmap_entry *entry)
+static void __hashmap__del_entry(struct hashmap_entry **pprev,
+ struct hashmap_entry *entry)
{
*pprev = entry->next;
entry->next = NULL;
}

-void hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
- hashmap_equal_fn equal_fn, void *ctx)
+void __hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
+ hashmap_equal_fn equal_fn, void *ctx)
{
map->hash_fn = hash_fn;
map->equal_fn = equal_fn;
@@ -48,19 +48,19 @@ void hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
map->sz = 0;
}

-struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
- hashmap_equal_fn equal_fn,
- void *ctx)
+struct hashmap *__hashmap__new(hashmap_hash_fn hash_fn,
+ hashmap_equal_fn equal_fn,
+ void *ctx)
{
struct hashmap *map = malloc(sizeof(struct hashmap));

if (!map)
return ERR_PTR(-ENOMEM);
- hashmap__init(map, hash_fn, equal_fn, ctx);
+ __hashmap__init(map, hash_fn, equal_fn, ctx);
return map;
}

-void hashmap__clear(struct hashmap *map)
+void __hashmap__clear(struct hashmap *map)
{
struct hashmap_entry *cur, *tmp;
size_t bkt;
@@ -73,32 +73,32 @@ void hashmap__clear(struct hashmap *map)
map->cap = map->cap_bits = map->sz = 0;
}

-void hashmap__free(struct hashmap *map)
+void __hashmap__free(struct hashmap *map)
{
if (IS_ERR_OR_NULL(map))
return;

- hashmap__clear(map);
+ __hashmap__clear(map);
free(map);
}

-size_t hashmap__size(const struct hashmap *map)
+size_t __hashmap__size(const struct hashmap *map)
{
return map->sz;
}

-size_t hashmap__capacity(const struct hashmap *map)
+size_t __hashmap__capacity(const struct hashmap *map)
{
return map->cap;
}

-static bool hashmap_needs_to_grow(struct hashmap *map)
+static bool __hashmap__needs_to_grow(struct hashmap *map)
{
/* grow if empty or more than 75% filled */
return (map->cap == 0) || ((map->sz + 1) * 4 / 3 > map->cap);
}

-static int hashmap_grow(struct hashmap *map)
+static int __hashmap__grow(struct hashmap *map)
{
struct hashmap_entry **new_buckets;
struct hashmap_entry *cur, *tmp;
@@ -116,7 +116,7 @@ static int hashmap_grow(struct hashmap *map)

hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
h = hash_bits(map->hash_fn(cur->key, map->ctx), new_cap_bits);
- hashmap_add_entry(&new_buckets[h], cur);
+ __hashmap__add_entry(&new_buckets[h], cur);
}

map->cap = new_cap;
@@ -127,10 +127,10 @@ static int hashmap_grow(struct hashmap *map)
return 0;
}

-static bool hashmap_find_entry(const struct hashmap *map,
- const long key, size_t hash,
- struct hashmap_entry ***pprev,
- struct hashmap_entry **entry)
+static bool __hashmap__find_entry(const struct hashmap *map,
+ const long key, size_t hash,
+ struct hashmap_entry ***pprev,
+ struct hashmap_entry **entry)
{
struct hashmap_entry *cur, **prev_ptr;

@@ -151,9 +151,9 @@ static bool hashmap_find_entry(const struct hashmap *map,
return false;
}

-int hashmap_insert(struct hashmap *map, long key, long value,
- enum hashmap_insert_strategy strategy,
- long *old_key, long *old_value)
+int __hashmap__insert(struct hashmap *map, long key, long value,
+ enum hashmap_insert_strategy strategy,
+ long *old_key, long *old_value)
{
struct hashmap_entry *entry;
size_t h;
@@ -166,7 +166,7 @@ int hashmap_insert(struct hashmap *map, long key, long value,

h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
if (strategy != HASHMAP_APPEND &&
- hashmap_find_entry(map, key, h, NULL, &entry)) {
+ __hashmap__find_entry(map, key, h, NULL, &entry)) {
if (old_key)
*old_key = entry->key;
if (old_value)
@@ -184,8 +184,8 @@ int hashmap_insert(struct hashmap *map, long key, long value,
if (strategy == HASHMAP_UPDATE)
return -ENOENT;

- if (hashmap_needs_to_grow(map)) {
- err = hashmap_grow(map);
+ if (__hashmap__needs_to_grow(map)) {
+ err = __hashmap__grow(map);
if (err)
return err;
h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
@@ -197,19 +197,19 @@ int hashmap_insert(struct hashmap *map, long key, long value,

entry->key = key;
entry->value = value;
- hashmap_add_entry(&map->buckets[h], entry);
+ __hashmap__add_entry(&map->buckets[h], entry);
map->sz++;

return 0;
}

-bool hashmap_find(const struct hashmap *map, long key, long *value)
+bool __hashmap__find(const struct hashmap *map, long key, long *value)
{
struct hashmap_entry *entry;
size_t h;

h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
- if (!hashmap_find_entry(map, key, h, NULL, &entry))
+ if (!__hashmap__find_entry(map, key, h, NULL, &entry))
return false;

if (value)
@@ -217,14 +217,14 @@ bool hashmap_find(const struct hashmap *map, long key, long *value)
return true;
}

-bool hashmap_delete(struct hashmap *map, long key,
- long *old_key, long *old_value)
+bool __hashmap__delete(struct hashmap *map, long key,
+ long *old_key, long *old_value)
{
struct hashmap_entry **pprev, *entry;
size_t h;

h = hash_bits(map->hash_fn(key, map->ctx), map->cap_bits);
- if (!hashmap_find_entry(map, key, h, &pprev, &entry))
+ if (!__hashmap__find_entry(map, key, h, &pprev, &entry))
return false;

if (old_key)
@@ -232,7 +232,7 @@ bool hashmap_delete(struct hashmap *map, long key,
if (old_value)
*old_value = entry->value;

- hashmap_del_entry(pprev, entry);
+ __hashmap__del_entry(pprev, entry);
free(entry);
map->sz--;

diff --git a/tools/perf/util/hashmap.h b/tools/perf/util/hashmap.h
index 0c4f155e8eb7..f8bc70a33026 100644
--- a/tools/perf/util/hashmap.h
+++ b/tools/perf/util/hashmap.h
@@ -5,8 +5,8 @@
*
* Copyright (c) 2019 Facebook
*/
-#ifndef __LIBBPF_HASHMAP_H
-#define __LIBBPF_HASHMAP_H
+#ifndef __PERF_UTIL_HASHMAP_H
+#define __PERF_UTIL_HASHMAP_H

#include <stdbool.h>
#include <stddef.h>
@@ -80,16 +80,49 @@ struct hashmap {
size_t sz;
};

-void hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
- hashmap_equal_fn equal_fn, void *ctx);
-struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
- hashmap_equal_fn equal_fn,
- void *ctx);
-void hashmap__clear(struct hashmap *map);
-void hashmap__free(struct hashmap *map);
+void __hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
+ hashmap_equal_fn equal_fn, void *ctx);
+struct hashmap *__hashmap__new(hashmap_hash_fn hash_fn,
+ hashmap_equal_fn equal_fn,
+ void *ctx);
+void __hashmap__clear(struct hashmap *map);
+void __hashmap__free(struct hashmap *map);

-size_t hashmap__size(const struct hashmap *map);
-size_t hashmap__capacity(const struct hashmap *map);
+size_t __hashmap__size(const struct hashmap *map);
+size_t __hashmap__capacity(const struct hashmap *map);
+
+static inline void hashmap__init(struct hashmap *map, hashmap_hash_fn hash_fn,
+ hashmap_equal_fn equal_fn, void *ctx)
+{
+ __hashmap__init(map, hash_fn, equal_fn, ctx);
+}
+
+static inline struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
+ hashmap_equal_fn equal_fn,
+ void *ctx)
+{
+ return __hashmap__new(hash_fn, equal_fn, ctx);
+}
+
+static inline void hashmap__clear(struct hashmap *map)
+{
+ __hashmap__clear(map);
+}
+
+static inline void hashmap__free(struct hashmap *map)
+{
+ __hashmap__free(map);
+}
+
+static inline size_t hashmap__size(const struct hashmap *map)
+{
+ return __hashmap__size(map);
+}
+
+static inline size_t hashmap__capacity(const struct hashmap *map)
+{
+ return __hashmap__capacity(map);
+}

/*
* Hashmap insertion strategy:
@@ -126,14 +159,14 @@ enum hashmap_insert_strategy {
* through old_key and old_value to allow calling code do proper memory
* management.
*/
-int hashmap_insert(struct hashmap *map, long key, long value,
- enum hashmap_insert_strategy strategy,
- long *old_key, long *old_value);
+int __hashmap__insert(struct hashmap *map, long key, long value,
+ enum hashmap_insert_strategy strategy,
+ long *old_key, long *old_value);

#define hashmap__insert(map, key, value, strategy, old_key, old_value) \
- hashmap_insert((map), (long)(key), (long)(value), (strategy), \
- hashmap_cast_ptr(old_key), \
- hashmap_cast_ptr(old_value))
+ __hashmap__insert((map), (long)(key), (long)(value), (strategy), \
+ hashmap_cast_ptr(old_key), \
+ hashmap_cast_ptr(old_value))

#define hashmap__add(map, key, value) \
hashmap__insert((map), (key), (value), HASHMAP_ADD, NULL, NULL)
@@ -147,17 +180,17 @@ int hashmap_insert(struct hashmap *map, long key, long value,
#define hashmap__append(map, key, value) \
hashmap__insert((map), (key), (value), HASHMAP_APPEND, NULL, NULL)

-bool hashmap_delete(struct hashmap *map, long key, long *old_key, long *old_value);
+bool __hashmap__delete(struct hashmap *map, long key, long *old_key, long *old_value);

-#define hashmap__delete(map, key, old_key, old_value) \
- hashmap_delete((map), (long)(key), \
- hashmap_cast_ptr(old_key), \
- hashmap_cast_ptr(old_value))
+#define hashmap__delete(map, key, old_key, old_value) \
+ __hashmap__delete((map), (long)(key), \
+ hashmap_cast_ptr(old_key), \
+ hashmap_cast_ptr(old_value))

-bool hashmap_find(const struct hashmap *map, long key, long *value);
+bool __hashmap__find(const struct hashmap *map, long key, long *value);

#define hashmap__find(map, key, value) \
- hashmap_find((map), (long)(key), hashmap_cast_ptr(value))
+ __hashmap__find((map), (long)(key), hashmap_cast_ptr(value))

/*
* hashmap__for_each_entry - iterate over all entries in hashmap
@@ -205,4 +238,4 @@ bool hashmap_find(const struct hashmap *map, long key, long *value);
cur = tmp) \
if ((map)->equal_fn(cur->key, (_key), (map)->ctx))

-#endif /* __LIBBPF_HASHMAP_H */
+#endif /* __PERF_UTIL_HASHMAP_H */
diff --git a/tools/perf/util/hwmon_pmu.c b/tools/perf/util/hwmon_pmu.c
index 96aeb379e169..c150fc0d7bd3 100644
--- a/tools/perf/util/hwmon_pmu.c
+++ b/tools/perf/util/hwmon_pmu.c
@@ -571,7 +571,7 @@ bool hwmon_pmu__have_event(struct perf_pmu *pmu, const char *name)

key.type = type;
key.num = number;
- if (hashmap_find(&hwm->events, key.type_and_num, /*value=*/NULL))
+ if (hashmap__find(&hwm->events, key.type_and_num, /*value=*/NULL))
return true;
if (key.num != -1)
return false;
--
2.56.1.703.gf848ed807c-goog