Linux Perf Users
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
	 Adrian Hunter <adrian.hunter@intel.com>,
	James Clark <james.clark@linaro.org>,
	 linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 05/26] perf hashmap: Always build perf's hashmap and avoid libbpf symbol conflict
Date: Fri, 13 Nov 2026 13:48:29 -0800	[thread overview]
Message-ID: <20261113214850.3329699-6-irogers@google.com> (raw)
In-Reply-To: <20261113214850.3329699-1-irogers@google.com>

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@google.com>
---
 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


  parent reply	other threads:[~2026-11-13 21:49 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-13 21:48 [PATCH v1 00/26] perf hashmap: Fix undefined behavior, remove ERR_PTR, and add type-safe generic hashmaps Ian Rogers
2026-11-13 21:48 ` [PATCH v1 01/26] perf threads: Fix uninitialized pointer put when thread removal misses Ian Rogers
2026-11-13 21:56   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 02/26] perf annotate: Fix sym_hist_entry memory leak when zeroing histograms Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 03/26] perf stat: Fix per_pkg_mask key leak on hashmap__add failure Ian Rogers
2026-11-13 21:55   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 04/26] perf build: Don't check difference of perf and libbpf hashmap Ian Rogers
2026-11-13 21:54   ` sashiko-bot
2026-11-13 21:48 ` Ian Rogers [this message]
2026-11-13 21:55   ` [PATCH v1 05/26] perf hashmap: Always build perf's hashmap and avoid libbpf symbol conflict sashiko-bot
2026-11-13 21:48 ` [PATCH v1 06/26] perf hashmap: Fix strict aliasing violations in hashmap Ian Rogers
2026-11-13 21:56   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 07/26] perf hashmap: Remove errptr usage from hashmap Ian Rogers
2026-11-13 22:01   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 08/26] perf hashmap: Introduce generic types and macros Ian Rogers
2026-11-13 22:01   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 09/26] perf fncache: Use generic hashmap for fncache Ian Rogers
2026-11-13 21:56   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 10/26] perf ftrace: Use generic hashmap for profile_hash Ian Rogers
2026-11-13 21:57   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 11/26] perf trace: Use generic hashmap for syscall_stats Ian Rogers
2026-11-13 22:04   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 12/26] perf trace: Use generic hashmap for bpf-trace-summary Ian Rogers
2026-11-13 21:58   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 13/26] perf lock: Use generic hashmap for slab_hash Ian Rogers
2026-11-13 21:57   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 14/26] perf stat: Use generic hashmap for per_pkg_mask Ian Rogers
2026-11-13 21:57   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 15/26] perf expr: Use generic hashmap for expr_ids Ian Rogers
2026-11-13 22:02   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 16/26] perf pmu: Use generic hashmap for pmu aliases Ian Rogers
2026-11-13 22:04   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 17/26] perf hwmon_pmu: Use generic hashmap for hwmon events Ian Rogers
2026-11-13 21:58   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 18/26] perf s390: Use generic hashmap for counter name cache Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 19/26] perf threads: Use generic hashmap for thread shards Ian Rogers
2026-11-13 22:01   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 20/26] perf annotate: Use generic hashmap for sym_samples Ian Rogers
2026-11-13 22:08   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 21/26] perf annotate: Use generic hashmap for browser type_hash Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 22/26] perf annotate-data: Use generic hashmap for type_hist samples Ian Rogers
2026-11-13 22:00   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 23/26] perf inject: Use generic hashmap for aslr remap_addresses Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 24/26] perf inject: Use generic hashmap for aslr top_addresses Ian Rogers
2026-11-13 21:59   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 25/26] perf inject: Use generic hashmap for aslr evsel_orig_attrs Ian Rogers
2026-11-13 22:00   ` sashiko-bot
2026-11-13 21:48 ` [PATCH v1 26/26] perf hashmap: Remove untyped struct hashmap API Ian Rogers
2026-11-13 22:01   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261113214850.3329699-6-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox