perf synthetic-events: Use function to add missing maps lock
authorIan Rogers <irogers@google.com>
Thu, 7 Dec 2023 01:16:43 +0000 (17:16 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 19 Dec 2023 00:35:12 +0000 (21:35 -0300)
Switch perf_event__synthesize_modules from loop macro
maps__for_each_entry to maps__for_each_map function that takes
a callback. The function holds the maps lock, which should be
held during iteration.

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-10-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/synthetic-events.c

index a0579c7d7b9e9ecbe0996e8fd54a8f277be1b597..3712186353fb94109e327195d1aee6d2177763ec 100644 (file)
@@ -665,18 +665,74 @@ int perf_event__synthesize_cgroups(struct perf_tool *tool __maybe_unused,
 }
 #endif
 
+struct perf_event__synthesize_modules_maps_cb_args {
+       struct perf_tool *tool;
+       perf_event__handler_t process;
+       struct machine *machine;
+       union perf_event *event;
+};
+
+static int perf_event__synthesize_modules_maps_cb(struct map *map, void *data)
+{
+       struct perf_event__synthesize_modules_maps_cb_args *args = data;
+       union perf_event *event = args->event;
+       struct dso *dso;
+       size_t size;
+
+       if (!__map__is_kmodule(map))
+               return 0;
+
+       dso = map__dso(map);
+       if (symbol_conf.buildid_mmap2) {
+               size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64));
+               event->mmap2.header.type = PERF_RECORD_MMAP2;
+               event->mmap2.header.size = (sizeof(event->mmap2) -
+                                       (sizeof(event->mmap2.filename) - size));
+               memset(event->mmap2.filename + size, 0, args->machine->id_hdr_size);
+               event->mmap2.header.size += args->machine->id_hdr_size;
+               event->mmap2.start = map__start(map);
+               event->mmap2.len   = map__size(map);
+               event->mmap2.pid   = args->machine->pid;
+
+               memcpy(event->mmap2.filename, dso->long_name, dso->long_name_len + 1);
+
+               perf_record_mmap2__read_build_id(&event->mmap2, args->machine, false);
+       } else {
+               size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64));
+               event->mmap.header.type = PERF_RECORD_MMAP;
+               event->mmap.header.size = (sizeof(event->mmap) -
+                                       (sizeof(event->mmap.filename) - size));
+               memset(event->mmap.filename + size, 0, args->machine->id_hdr_size);
+               event->mmap.header.size += args->machine->id_hdr_size;
+               event->mmap.start = map__start(map);
+               event->mmap.len   = map__size(map);
+               event->mmap.pid   = args->machine->pid;
+
+               memcpy(event->mmap.filename, dso->long_name, dso->long_name_len + 1);
+       }
+
+       if (perf_tool__process_synth_event(args->tool, event, args->machine, args->process) != 0)
+               return -1;
+
+       return 0;
+}
+
 int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t process,
                                   struct machine *machine)
 {
-       int rc = 0;
-       struct map_rb_node *pos;
+       int rc;
        struct maps *maps = machine__kernel_maps(machine);
-       union perf_event *event;
-       size_t size = symbol_conf.buildid_mmap2 ?
-                       sizeof(event->mmap2) : sizeof(event->mmap);
+       struct perf_event__synthesize_modules_maps_cb_args args = {
+               .tool = tool,
+               .process = process,
+               .machine = machine,
+       };
+       size_t size = symbol_conf.buildid_mmap2
+               ? sizeof(args.event->mmap2)
+               : sizeof(args.event->mmap);
 
-       event = zalloc(size + machine->id_hdr_size);
-       if (event == NULL) {
+       args.event = zalloc(size + machine->id_hdr_size);
+       if (args.event == NULL) {
                pr_debug("Not enough memory synthesizing mmap event "
                         "for kernel modules\n");
                return -1;
@@ -687,53 +743,13 @@ int perf_event__synthesize_modules(struct perf_tool *tool, perf_event__handler_t
         * __perf_event_mmap
         */
        if (machine__is_host(machine))
-               event->header.misc = PERF_RECORD_MISC_KERNEL;
+               args.event->header.misc = PERF_RECORD_MISC_KERNEL;
        else
-               event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
-
-       maps__for_each_entry(maps, pos) {
-               struct map *map = pos->map;
-               struct dso *dso;
+               args.event->header.misc = PERF_RECORD_MISC_GUEST_KERNEL;
 
-               if (!__map__is_kmodule(map))
-                       continue;
+       rc = maps__for_each_map(maps, perf_event__synthesize_modules_maps_cb, &args);
 
-               dso = map__dso(map);
-               if (symbol_conf.buildid_mmap2) {
-                       size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64));
-                       event->mmap2.header.type = PERF_RECORD_MMAP2;
-                       event->mmap2.header.size = (sizeof(event->mmap2) -
-                                               (sizeof(event->mmap2.filename) - size));
-                       memset(event->mmap2.filename + size, 0, machine->id_hdr_size);
-                       event->mmap2.header.size += machine->id_hdr_size;
-                       event->mmap2.start = map__start(map);
-                       event->mmap2.len   = map__size(map);
-                       event->mmap2.pid   = machine->pid;
-
-                       memcpy(event->mmap2.filename, dso->long_name, dso->long_name_len + 1);
-
-                       perf_record_mmap2__read_build_id(&event->mmap2, machine, false);
-               } else {
-                       size = PERF_ALIGN(dso->long_name_len + 1, sizeof(u64));
-                       event->mmap.header.type = PERF_RECORD_MMAP;
-                       event->mmap.header.size = (sizeof(event->mmap) -
-                                               (sizeof(event->mmap.filename) - size));
-                       memset(event->mmap.filename + size, 0, machine->id_hdr_size);
-                       event->mmap.header.size += machine->id_hdr_size;
-                       event->mmap.start = map__start(map);
-                       event->mmap.len   = map__size(map);
-                       event->mmap.pid   = machine->pid;
-
-                       memcpy(event->mmap.filename, dso->long_name, dso->long_name_len + 1);
-               }
-
-               if (perf_tool__process_synth_event(tool, event, machine, process) != 0) {
-                       rc = -1;
-                       break;
-               }
-       }
-
-       free(event);
+       free(args.event);
        return rc;
 }