perf maps: Add maps__for_each_map to iterate maps holding the lock
authorIan Rogers <irogers@google.com>
Thu, 7 Dec 2023 01:16:36 +0000 (17:16 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 19 Dec 2023 00:34:54 +0000 (21:34 -0300)
The macro maps__for_each_entry is error prone as it doesn't require
holding the maps lock.

Add a new function that iterates the maps holding the read lock.

Convert maps__find_symbol_by_name() and maps__fprintf() to use callbacks,
the latter being an example of where the read lock wasn't being held.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Dmitrii Dolgov <9erthalion6@gmail.com>
Cc: German Gomez <german.gomez@arm.com>
Cc: Guilherme Amadio <amadio@gentoo.org>
Cc: Huacai Chen <chenhuacai@kernel.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@arm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Li Dong <lidong@vivo.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Miguel Ojeda <ojeda@kernel.org>
Cc: Ming Wang <wangming01@loongson.cn>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Nick Terrell <terrelln@fb.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Sean Christopherson <seanjc@google.com>
Cc: Steinar H. Gunderson <sesse@google.com>
Cc: Vincent Whitchurch <vincent.whitchurch@axis.com>
Cc: Wenyu Liu <liuwenyu7@huawei.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Link: https://lore.kernel.org/r/20231207011722.1220634-3-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/maps.c
tools/perf/util/maps.h

index 9a011ae..160a6dc 100644 (file)
@@ -196,6 +196,21 @@ void maps__put(struct maps *maps)
                RC_CHK_PUT(maps);
 }
 
+int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data)
+{
+       struct map_rb_node *pos;
+       int ret = 0;
+
+       down_read(maps__lock(maps));
+       maps__for_each_entry(maps, pos) {
+               ret = cb(pos->map, data);
+               if (ret)
+                       break;
+       }
+       up_read(maps__lock(maps));
+       return ret;
+}
+
 struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)
 {
        struct map *map = maps__find(maps, addr);
@@ -210,31 +225,40 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)
        return NULL;
 }
 
-struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp)
-{
+struct maps__find_symbol_by_name_args {
+       struct map **mapp;
+       const char *name;
        struct symbol *sym;
-       struct map_rb_node *pos;
+};
 
-       down_read(maps__lock(maps));
+static int maps__find_symbol_by_name_cb(struct map *map, void *data)
+{
+       struct maps__find_symbol_by_name_args *args = data;
 
-       maps__for_each_entry(maps, pos) {
-               sym = map__find_symbol_by_name(pos->map, name);
+       args->sym = map__find_symbol_by_name(map, args->name);
+       if (!args->sym)
+               return 0;
 
-               if (sym == NULL)
-                       continue;
-               if (!map__contains_symbol(pos->map, sym)) {
-                       sym = NULL;
-                       continue;
-               }
-               if (mapp != NULL)
-                       *mapp = pos->map;
-               goto out;
+       if (!map__contains_symbol(map, args->sym)) {
+               args->sym = NULL;
+               return 0;
        }
 
-       sym = NULL;
-out:
-       up_read(maps__lock(maps));
-       return sym;
+       if (args->mapp != NULL)
+               *args->mapp = map__get(map);
+       return 1;
+}
+
+struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp)
+{
+       struct maps__find_symbol_by_name_args args = {
+               .mapp = mapp,
+               .name = name,
+               .sym = NULL,
+       };
+
+       maps__for_each_map(maps, maps__find_symbol_by_name_cb, &args);
+       return args.sym;
 }
 
 int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams)
@@ -253,25 +277,34 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams)
        return ams->ms.sym ? 0 : -1;
 }
 
-size_t maps__fprintf(struct maps *maps, FILE *fp)
-{
-       size_t printed = 0;
-       struct map_rb_node *pos;
+struct maps__fprintf_args {
+       FILE *fp;
+       size_t printed;
+};
 
-       down_read(maps__lock(maps));
+static int maps__fprintf_cb(struct map *map, void *data)
+{
+       struct maps__fprintf_args *args = data;
 
-       maps__for_each_entry(maps, pos) {
-               printed += fprintf(fp, "Map:");
-               printed += map__fprintf(pos->map, fp);
-               if (verbose > 2) {
-                       printed += dso__fprintf(map__dso(pos->map), fp);
-                       printed += fprintf(fp, "--\n");
-               }
+       args->printed += fprintf(args->fp, "Map:");
+       args->printed += map__fprintf(map, args->fp);
+       if (verbose > 2) {
+               args->printed += dso__fprintf(map__dso(map), args->fp);
+               args->printed += fprintf(args->fp, "--\n");
        }
+       return 0;
+}
 
-       up_read(maps__lock(maps));
+size_t maps__fprintf(struct maps *maps, FILE *fp)
+{
+       struct maps__fprintf_args args = {
+               .fp = fp,
+               .printed = 0,
+       };
+
+       maps__for_each_map(maps, maps__fprintf_cb, &args);
 
-       return printed;
+       return args.printed;
 }
 
 int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp)
index a689149..14ad959 100644 (file)
@@ -81,6 +81,9 @@ static inline void __maps__zput(struct maps **map)
 
 #define maps__zput(map) __maps__zput(&map)
 
+/* Iterate over map calling cb for each entry. */
+int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data);
+
 static inline struct rb_root *maps__entries(struct maps *maps)
 {
        return &RC_CHK_ACCESS(maps)->entries;