Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2185941rda; Tue, 24 Oct 2023 15:27:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHwX2EeoGPCKqDAuaoEaIWRTcMV8MAFk/kBBTt2NbpC2i1KL+6KPiuWGNoluyK2dLOrnJ0w X-Received: by 2002:a17:902:d506:b0:1c3:c127:540 with SMTP id b6-20020a170902d50600b001c3c1270540mr12810187plg.29.1698186432792; Tue, 24 Oct 2023 15:27:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698186432; cv=none; d=google.com; s=arc-20160816; b=GHbTjAapGg+ZERdELbOWLGqvAFHhiTQReotyfs08valzpXxxWwmhCaWzS45+NNAE2k /G98kkJkso+Lae5eqGCgm23q+rb0LsudRDdBr1uszrATS4eWaGyxVPxYLzQyZzcjbNuQ JNr3I9B401nzsZpavEq5LlcAUHYqL6tnqrNDujpPvE2Tl8v4k/iqpxnwKO0yFYe+Hjz5 V0m3HU2CjT1CjbtkbY7oo2twXvaMc2BgXfj1UreL93X4ZehrfBvv+9C5qRYCQDokbQh1 gZ0tG4k+GYL9ISJBGcqEnL3qRtj4gmb6hrRw1F6ltq98Z+W5rxtHvsuTySlDWOmPz/hh LyBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=pZmLuOzKOZWnfcppPkzxkV5fHZsLOBY0ABvxWYK/enc=; fh=NAro5oTsDEB2Or99ABwb774QJkwQSkr6EoLVqn/Yde0=; b=AX44tM47h3mpcuU/pJU3YUu+OT/6ZBMskKzx4hiC3aBDdnqb4mOcTJbooFit/QKDy9 V7vR0g0iuooKPszCPCOJwhQD8dwLjBCZSjaH80LQR2SxvLII/Jc4LBm34cZmMO2hbeVB z0zQU6JNhbrfxzpXs6Kc58BHi6Y88O6Vxs0j6jhxHpXaYSTKUDsTWFMTGbxWF6aM9t5v UIVYH9ookeE7GcUwbQLX6dS1av0RP4pD6bbe0PTNDHw0bVhiXBdhY9yi5mB8X1Spz/Fe z4DEAtk+i01RGP8CpQKbRuw8Qem++apxAGC3tkUdudbJXh158AGgUxSNPav3PNhEB19A rXcw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ID9QBfXd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id z17-20020a170903019100b001ca7295810bsi9467324plg.411.2023.10.24.15.27.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 15:27:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ID9QBfXd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 17B12801DD8D; Tue, 24 Oct 2023 15:27:02 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344486AbjJXW0d (ORCPT + 99 others); Tue, 24 Oct 2023 18:26:33 -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-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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 24 Oct 2023 15:27:02 -0700 (PDT) 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 = 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) { 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) 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 map *map = 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); -#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) { struct rb_root entries; struct rw_semaphore lock; @@ -80,6 +76,8 @@ static inline void __maps__zput(struct maps **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); +/* 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) { 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 pgoff, void *data) 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_ACCESS(map) != RC_CHK_ACCESS(map_to_save) && !__map__is_bpf_prog(map); +} + static int dso__load_kcore(struct dso *dso, struct map *map, const char *kallsyms_filename) { struct maps *kmaps = map__kmaps(map); struct kcore_mapfn_data md; struct map *replacement_map = 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 map *map, } /* Remove old maps */ - maps__for_each_entry_safe(kmaps, old_node, next) { - 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); - } + maps__remove_maps(kmaps, remove_old_maps, map); machine->trampolines_mapped = false; /* Find the kernel map using the '_stext' symbol */ -- 2.42.0.758.gaed0368e0e-goog