Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp3431281rdh; Mon, 27 Nov 2023 14:17:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IFv69F2abhEwFFtXTPHIU1sDSS3Pn8m5APo7HpV3TDwd/eg2GH0fJXDYE07YANBVslPruxL X-Received: by 2002:a05:6808:1b2c:b0:3b8:5b3e:ac92 with SMTP id bx44-20020a0568081b2c00b003b85b3eac92mr12747851oib.11.1701123446292; Mon, 27 Nov 2023 14:17:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701123446; cv=none; d=google.com; s=arc-20160816; b=UHBk7rK4I0mVMrkuZrmooNVn8ZlMv/xeQbsVo87KWlfRdFAJ4niMBrR9LJ8+HC4y5s JwNuCOYj/p3hgAtSjt5R2QwzDiKyGBL91GKEL8wk3e8yos2WzjElV5cSXD9hNtuhcNtv ZU7x7Y57XL49kXV8AgeXbb6WjPnGOHgf0yWh1B48VO4cZEECanUSwBa9xvKnH1fVBhkU kE3CGmfGQK2Yaojtt/KQmPWgUvz7/knuJqanzmBzg+cHbQ0NQvhOB4BfiukHq7+/G1Sz ma1c8acmAgFPPsJqxJZJpJ80qRMjpOif4Sx7D5GG6v9x1wlZXd1uF9BZCP776IGxI5Qg uRsg== 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=dubn7azNjRzLb/gPhpvhXF4YdgS4DTAmzBs+Pldy+XA=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=H8Q59+FKEBES/YXES5wbRL8gBy+pM14zAkHzyffuwMtgREzGSVO00rejFQtK0544+3 au7+mQdGlDEr/eFfznIXF4qQY4k3844bfGf0aZVHOLItWZLeurEP4IrTOIQOjmvK5Xp3 rw2JU1cozO+8C/DUVqQeBob08tSFnYJ1BU/zAtSxHaOrYUGkHWysngd6r70tC/hAMKHl k2EKGWRJZx3cynxkXKbhAtp49bd6i7Wf11VbFYmG6mZ3zOtWIubfxr5GijrztJdpyEmJ kvbHRyiM2q6EHbolCIrZh1ZiBgJrFjyHQE/o3iP8gXP42SNO+L9cnkzNIGlsrIWsetFN mOFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1F0Y4RdN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id bh1-20020a056a02020100b005859c81f1f0si11184998pgb.137.2023.11.27.14.17.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 14:17:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=1F0Y4RdN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 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 groat.vger.email (Postfix) with ESMTP id E69188060CA8; Mon, 27 Nov 2023 14:17:18 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233362AbjK0WQm (ORCPT + 99 others); Mon, 27 Nov 2023 17:16:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233685AbjK0WKO (ORCPT ); Mon, 27 Nov 2023 17:10:14 -0500 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A2AF61BEB for ; Mon, 27 Nov 2023 14:10:01 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db3fc4a1254so5501902276.3 for ; Mon, 27 Nov 2023 14:10:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701123001; x=1701727801; 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=dubn7azNjRzLb/gPhpvhXF4YdgS4DTAmzBs+Pldy+XA=; b=1F0Y4RdNqaCYCQTk06YJ6pUPFLkE7uIJ1/Ya10PV8BgKPIRrjQMmXJ82pbYpsttWBT SAURZX/TBEpHGC7ZComRk2qzGAtgSPJgUmG3C1ihtma3AIQ5pMZKnNok3r0ElsaG2rlP 6VZSEtKB5trBSeqnts5YZPydD/hgYXHuG677NWsK7N+aZYMI8hOeEXCMXCn39POryreL biP8FrIrTlH6ASc6ZLghFPeXBjDK3y99+/WJfFvyzEwFKVv+lYBbj51CxkTDQ8sUTJQj a7zpiozgiA4W5I9Az4aS0CJ8jupsD0Cg2n4cgedFwxB95GOEYEbqCt4QEp/8Hw2zYNJO oXNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701123001; x=1701727801; 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=dubn7azNjRzLb/gPhpvhXF4YdgS4DTAmzBs+Pldy+XA=; b=xK2zeK7JDBCsf1kt5+YyMV4Y20U88lZu0jFpH60gyMp/AzAGO5aS38QcJNX70flqrs i7QBxFb88bK1A7z3Q3D8oqj0sHkR8kMRXFy4Z4Q2NeroVexuB2Frt1dEh2QvbtB2H78Y irspCNTyWV0iA78vyPjOxhHejJnYFpUtqr/VuXNIcbOPXUA11NZLY8JoI29UTfx3O68D UuGbB3Mt1GQkRfv6/ys6pYaRPBtE0Mi6tel8btYuTN8nJX6YxIxX6nJcXvY1LDcaL3wn FMo0UUrWZczUNIAbsTQnxXrTS5X7KxX/fAXZX7LVkPlPM5jL7QaNnLlUMI1Wsb1cg9hM 5Xmg== X-Gm-Message-State: AOJu0YwYOx7y7ryRtEELNsdMKf1OeNmS/ydXweUjQnznrLz1XeWF6xJf 6QgLOkGHwDKCkE0Myu39BZVOixBpwnPS X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:829:6e77:9093:f39b]) (user=irogers job=sendgmr) by 2002:a05:6902:e91:b0:db4:5e66:98fe with SMTP id dg17-20020a0569020e9100b00db45e6698femr358446ybb.11.1701123000839; Mon, 27 Nov 2023 14:10:00 -0800 (PST) Date: Mon, 27 Nov 2023 14:08:28 -0800 In-Reply-To: <20231127220902.1315692-1-irogers@google.com> Message-Id: <20231127220902.1315692-17-irogers@google.com> Mime-Version: 1.0 References: <20231127220902.1315692-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Subject: [PATCH v5 16/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 , Kajol Jain , Athira Rajeev , Huacai Chen , Masami Hiramatsu , Vincent Whitchurch , "Steinar H. Gunderson" , Liam Howlett , Miguel Ojeda , Colin Ian King , Dmitrii Dolgov <9erthalion6@gmail.com>, Yang Jihong , Ming Wang , James Clark , K Prateek Nayak , Sean Christopherson , Leo Yan , Ravi Bangoria , German Gomez , Changbin Du , Paolo Bonzini , Li Dong , Sandipan Das , liuwenyu , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, Guilherme Amadio 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,T_SCC_BODY_TEXT_LINE, 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 groat.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 (groat.vger.email [0.0.0.0]); Mon, 27 Nov 2023 14:17:19 -0800 (PST) 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.43.0.rc1.413.gea7ed67945-goog