From nobody Thu Jan 1 08:57:44 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33944C25B47 for ; Tue, 24 Oct 2023 22:26:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344638AbjJXW0e (ORCPT ); Tue, 24 Oct 2023 18:26:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54676 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344478AbjJXWZv (ORCPT ); Tue, 24 Oct 2023 18:25:51 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1523126BC for ; Tue, 24 Oct 2023 15:25:20 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5a7d261a84bso67525687b3.3 for ; Tue, 24 Oct 2023 15:25:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186318; x=1698791118; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=pZmLuOzKOZWnfcppPkzxkV5fHZsLOBY0ABvxWYK/enc=; b=ID9QBfXdKzQpRhPP1pLBWsBqXDwvD4tx5lGwebDU+tMCIdIec9UizeDmQVVi7Lj6T7 kTZCgylWzQWvQ+/0jyAFYrou+h05rcoG4IYZEEZzkP1S23FixAPK7SffQbeICeX91ifl tabqnjxixZu4V0JrSGuh/u0QIaI1funTcwQv9h6CYEDCYHohDWCnK+ZwOU8EdfdumYsC hGGbuaWdWer42j30Vg9Bfr5yHaQazN5nK5XIW9NVN7DdEY0MrA633MU3gXYvOZLW5WoP ULEjC/szs8H7+A+IbXehdDY+Pz3aiT418JdiCFZVbGwDTwjzmBm6KqhUlk0fPvYM1jrX CJWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186318; x=1698791118; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=pZmLuOzKOZWnfcppPkzxkV5fHZsLOBY0ABvxWYK/enc=; b=NTQA9Q2mMt1Pe5N0SSD8XIcOKVXSeNOpDlOEnpCGOByakd4FVDEEI350SnugqVP9AS tXyqEG/z3bElf7J4X/w4jid/jPd2TgbBG4Nzy3OjBx8FxZhGzWLpqAe48+qRHtIushPG YTE7biAY2BrP6laLbWGoV67eUzfDQ5StZ8yQDqpU6Nx2hl+rB4S7AkPDDz1kAN99JvGg n0HbLXdU1zBHGDNJLeHFn/58ccQlogEbFClHDB/NzVDanb0KbiU+TPkEDBgE8V8Nb6nD QYrucA5eG6rPGNXKmaZgrRTNf1y4gtCdYPW3AGaUUp/N7PqJ0jgQwW9G5ixtilEIxfYU ULNg== X-Gm-Message-State: AOJu0YyM5QnXjQCwr5b3G4thd+rmZOi2qVTzCzhHpXFM49VCE1H4lznP toD3T/r6sySyNV/5xUivwsigIe0prl9i X-Google-Smtp-Source: AGHT+IHyzctCClnCKKRiBfWMxj+KXkNvs+dEJRni839bJcbmNMvWz4cQmRuKwnepiXHcCccKeecGHb4nDbRA X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a0d:ca8b:0:b0:589:a855:7af with SMTP id m133-20020a0dca8b000000b00589a85507afmr268607ywd.7.1698186318505; Tue, 24 Oct 2023 15:25:18 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:32 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-30-irogers@google.com> Mime-Version: 1.0 References: <20231024222353.3024098-1-irogers@google.com> X-Mailer: git-send-email 2.42.0.758.gaed0368e0e-goog Subject: [PATCH v3 29/50] perf maps: Add remove maps function to remove a map based on callback From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Adrian Hunter , Nick Terrell , Kan Liang , Andi Kleen , Leo Yan , Song Liu , Sandipan Das , James Clark , Anshuman Khandual , Miguel Ojeda , Liam Howlett , Yang Jihong , Athira Rajeev , Kajol Jain , K Prateek Nayak , Sean Christopherson , Yanteng Si , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Masami Hiramatsu , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 24 ++++++++++++++++++++++++ tools/perf/util/maps.h | 6 ++---- tools/perf/util/symbol.c | 24 ++++++++++++------------ 3 files changed, 38 insertions(+), 16 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 00e6589bba10..f13fd3a9686b 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -13,6 +13,10 @@ #define maps__for_each_entry(maps, map) \ for (map =3D maps__first(maps); map; map =3D map_rb_node__next(map)) =20 +#define maps__for_each_entry_safe(maps, map, next) \ + for (map =3D maps__first(maps), next =3D map_rb_node__next(map); map; \ + map =3D next, next =3D map_rb_node__next(map)) + static void maps__init(struct maps *maps, struct machine *machine) { refcount_set(maps__refcnt(maps), 1); @@ -214,6 +218,26 @@ int maps__for_each_map(struct maps *maps, int (*cb)(st= ruct map *map, void *data) return ret; } =20 +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 =3D 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 !=3D 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 map *map =3D maps__find(maps, addr); diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 8ac30cdaf5bd..b94ad5c8fea7 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -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 *maps__find(struct maps *maps, u64 addr); =20 -#define maps__for_each_entry_safe(maps, map, next) \ - for (map =3D maps__first(maps), next =3D map_rb_node__next(map); map; \ - map =3D next, next =3D map_rb_node__next(map)) - DECLARE_RC_STRUCT(maps) { struct rb_root entries; struct rw_semaphore lock; @@ -80,6 +76,8 @@ static inline void __maps__zput(struct maps **map) =20 /* 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); +/* 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); =20 static inline struct rb_root *maps__entries(struct maps *maps) { diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index 72f03b875478..30da8a405d11 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1239,13 +1239,23 @@ static int kcore_mapfn(u64 start, u64 len, u64 pgof= f, void *data) return 0; } =20 +static bool remove_old_maps(struct map *map, void *data) +{ + const struct map *map_to_save =3D 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_ACCESS(map) !=3D RC_CHK_ACCESS(map_to_save) && !__map__is_b= pf_prog(map); +} + static int dso__load_kcore(struct dso *dso, struct map *map, const char *kallsyms_filename) { struct maps *kmaps =3D map__kmaps(map); struct kcore_mapfn_data md; struct map *replacement_map =3D NULL; - struct map_rb_node *old_node, *next; struct machine *machine; bool is_64_bit; int err, fd; @@ -1292,17 +1302,7 @@ static int dso__load_kcore(struct dso *dso, struct m= ap *map, } =20 /* Remove old maps */ - maps__for_each_entry_safe(kmaps, old_node, next) { - struct map *old_map =3D 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 !=3D map && !__map__is_bpf_prog(old_map)) - maps__remove(kmaps, old_map); - } + maps__remove_maps(kmaps, remove_old_maps, map); machine->trampolines_mapped =3D false; =20 /* Find the kernel map using the '_stext' symbol */ --=20 2.42.0.758.gaed0368e0e-goog