Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf maps: Locking tidy up of nr_maps

After this change maps__nr_maps is only used by tests, existing users
are migrated to maps__empty. Compute maps__empty under the read lock.

Signed-off-by: Ian Rogers <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: K Prateek Nayak <kprateek.nayak@amd.com>
Cc: James Clark <james.clark@arm.com>
Cc: Vincent Whitchurch <vincent.whitchurch@axis.com>
Cc: Alexey Dobriyan <adobriyan@gmail.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Changbin Du <changbin.du@huawei.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Song Liu <song@kernel.org>
Cc: Leo Yan <leo.yan@linux.dev>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Artem Savkov <asavkov@redhat.com>
Cc: bpf@vger.kernel.org
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240210031746.4057262-7-irogers@google.com

authored by

Ian Rogers and committed by
Namhyung Kim
923e4616 ff0bd799

+11 -5
+1 -1
tools/perf/util/machine.c
··· 440 440 return NULL; 441 441 442 442 /* Assume maps are set up if there are any */ 443 - if (maps__nr_maps(thread__maps(thread))) 443 + if (!maps__empty(thread__maps(thread))) 444 444 return thread; 445 445 446 446 host_thread = machine__find_thread(host_machine, -1, pid);
+8 -2
tools/perf/util/maps.c
··· 541 541 542 542 bool maps__empty(struct maps *maps) 543 543 { 544 - return maps__nr_maps(maps) == 0; 544 + bool res; 545 + 546 + down_read(maps__lock(maps)); 547 + res = maps__nr_maps(maps) == 0; 548 + up_read(maps__lock(maps)); 549 + 550 + return res; 545 551 } 546 552 547 553 bool maps__equal(struct maps *a, struct maps *b) ··· 877 871 878 872 parent_maps_by_address = maps__maps_by_address(parent); 879 873 n = maps__nr_maps(parent); 880 - if (maps__empty(dest)) { 874 + if (maps__nr_maps(dest) == 0) { 881 875 /* No existing mappings so just copy from parent to avoid reallocs in insert. */ 882 876 unsigned int nr_maps_allocated = RC_CHK_ACCESS(parent)->nr_maps_allocated; 883 877 struct map **dest_maps_by_address =
+2 -2
tools/perf/util/maps.h
··· 43 43 void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data); 44 44 45 45 struct machine *maps__machine(const struct maps *maps); 46 - unsigned int maps__nr_maps(const struct maps *maps); 47 - refcount_t *maps__refcnt(struct maps *maps); 46 + unsigned int maps__nr_maps(const struct maps *maps); /* Test only. */ 47 + refcount_t *maps__refcnt(struct maps *maps); /* Test only. */ 48 48 49 49 #ifdef HAVE_LIBUNWIND_SUPPORT 50 50 void *maps__addr_space(const struct maps *maps);