Commit 8d5847a6 authored by Ian Rogers's avatar Ian Rogers Committed by Arnaldo Carvalho de Melo

perf maps: Add remove maps function to remove a map based on callback

Removing maps wasn't being done under the write lock. Similar to
maps__for_each_map(), iterate the entries but in this case remove the
entry based on the result of the callback. If an entry was removed
then maps_by_name() also needs updating, so add missed flush.

In dso__load_kcore(), the test of map to save would always be false with
REFCNT_CHECKING because of a missing RC_CHK_ACCESS/RC_CHK_EQUAL.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Acked-by: default avatarNamhyung Kim <namhyung@kernel.org>
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: 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-15-irogers@google.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 9cce3a16
...@@ -13,6 +13,10 @@ ...@@ -13,6 +13,10 @@
#define maps__for_each_entry(maps, map) \ #define maps__for_each_entry(maps, map) \
for (map = maps__first(maps); map; map = map_rb_node__next(map)) for (map = maps__first(maps); map; map = map_rb_node__next(map))
#define maps__for_each_entry_safe(maps, map, next) \
for (map = maps__first(maps), next = map_rb_node__next(map); map; \
map = next, next = map_rb_node__next(map))
static void maps__init(struct maps *maps, struct machine *machine) static void maps__init(struct maps *maps, struct machine *machine)
{ {
refcount_set(maps__refcnt(maps), 1); refcount_set(maps__refcnt(maps), 1);
...@@ -214,6 +218,26 @@ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data) ...@@ -214,6 +218,26 @@ int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data)
return ret; return ret;
} }
void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data)
{
struct map_rb_node *pos, *next;
unsigned int start_nr_maps;
down_write(maps__lock(maps));
start_nr_maps = maps__nr_maps(maps);
maps__for_each_entry_safe(maps, pos, next) {
if (cb(pos->map, data)) {
__maps__remove(maps, pos);
--RC_CHK_ACCESS(maps)->nr_maps;
}
}
if (maps__maps_by_name(maps) && start_nr_maps != maps__nr_maps(maps))
__maps__free_maps_by_name(maps);
up_write(maps__lock(maps));
}
struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp)
{ {
struct map *map = maps__find(maps, addr); struct map *map = maps__find(maps, addr);
......
...@@ -36,10 +36,6 @@ struct map_rb_node *map_rb_node__next(struct map_rb_node *node); ...@@ -36,10 +36,6 @@ struct map_rb_node *map_rb_node__next(struct map_rb_node *node);
struct map_rb_node *maps__find_node(struct maps *maps, struct map *map); struct map_rb_node *maps__find_node(struct maps *maps, struct map *map);
struct map *maps__find(struct maps *maps, u64 addr); struct map *maps__find(struct maps *maps, u64 addr);
#define maps__for_each_entry_safe(maps, map, next) \
for (map = maps__first(maps), next = map_rb_node__next(map); map; \
map = next, next = map_rb_node__next(map))
DECLARE_RC_STRUCT(maps) { DECLARE_RC_STRUCT(maps) {
struct rb_root entries; struct rb_root entries;
struct rw_semaphore lock; struct rw_semaphore lock;
...@@ -80,6 +76,8 @@ static inline void __maps__zput(struct maps **map) ...@@ -80,6 +76,8 @@ static inline void __maps__zput(struct maps **map)
/* Iterate over map calling cb for each entry. */ /* 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); int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data);
/* Iterate over map removing an entry if cb returns true. */
void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data);
static inline struct rb_root *maps__entries(struct maps *maps) static inline struct rb_root *maps__entries(struct maps *maps)
{ {
......
...@@ -1239,13 +1239,23 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data) ...@@ -1239,13 +1239,23 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgoff, void *data)
return 0; return 0;
} }
static bool remove_old_maps(struct map *map, void *data)
{
const struct map *map_to_save = data;
/*
* We need to preserve eBPF maps even if they are covered by kcore,
* because we need to access eBPF dso for source data.
*/
return !RC_CHK_EQUAL(map, map_to_save) && !__map__is_bpf_prog(map);
}
static int dso__load_kcore(struct dso *dso, struct map *map, static int dso__load_kcore(struct dso *dso, struct map *map,
const char *kallsyms_filename) const char *kallsyms_filename)
{ {
struct maps *kmaps = map__kmaps(map); struct maps *kmaps = map__kmaps(map);
struct kcore_mapfn_data md; struct kcore_mapfn_data md;
struct map *replacement_map = NULL; struct map *replacement_map = NULL;
struct map_rb_node *old_node, *next;
struct machine *machine; struct machine *machine;
bool is_64_bit; bool is_64_bit;
int err, fd; int err, fd;
...@@ -1292,17 +1302,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map, ...@@ -1292,17 +1302,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,
} }
/* Remove old maps */ /* Remove old maps */
maps__for_each_entry_safe(kmaps, old_node, next) { maps__remove_maps(kmaps, remove_old_maps, map);
struct map *old_map = old_node->map;
/*
* We need to preserve eBPF maps even if they are
* covered by kcore, because we need to access
* eBPF dso for source data.
*/
if (old_map != map && !__map__is_bpf_prog(old_map))
maps__remove(kmaps, old_map);
}
machine->trampolines_mapped = false; machine->trampolines_mapped = false;
/* Find the kernel map using the '_stext' symbol */ /* Find the kernel map using the '_stext' symbol */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment