Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1362306rdb; Wed, 6 Dec 2023 17:19:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IG9/1MvSnD20GorSb/iDi2D/1PZrwZ5vWFlla078u5UHPxQGJuxo4s/ow0zZUbF0gn2Cx6i X-Received: by 2002:a17:903:124c:b0:1d0:6ffd:6e71 with SMTP id u12-20020a170903124c00b001d06ffd6e71mr1715425plh.105.1701911956942; Wed, 06 Dec 2023 17:19:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701911956; cv=none; d=google.com; s=arc-20160816; b=t48wpU/TNyH6dlb2m5APMNqBR1X56xSoOgBaV+4u1wEJoiHkhbvdtRkDEvqz8vRUd5 WdxfHNl1nWzy4Kof68JLRUWOGVev4FVpX6oRm3MZhfK1pEYQ89gx4Q/NtBEixPgUncFS XGpEI6iuxzkIgygYr0f0K5rrakp9YwLg74Y4dYHvLpI2j9/flYOndsHCBIcW3YI2ei9r RCqsLnArsPXPksLifqm9SVR1kDzyzt4D7ytdbAHgyKVp5rAX4ZBDVenwTO6ca16PzXJS t70vGUt5viQ46CDiDH7EntA/I3ZQOTBxT261lOVuIrAzryoUts46q/iwxHVbIvkoF5La 38Rw== 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=ozafK+YxnV8oHaLDKY1idAuezimM/K5JtLiBl1FMWy4=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=JwSH5OCXACFyjCRhCrfobAT7NgkIqq/zIAXvkNLBtS4wtEN+/X4ksQk81KCbKEEx/f 05zkH72T5G3Xs1x30d1EL+mV6c8mHdRsuMvxOIY1qOq93nCbIbeX8Mvp3yEeagN+2K+T AGFV/u91Fe/ECK3L/27Vhrq0oXc/nGfX+ggrwmwLKlZX8fzpKgrv9uohEiyVQepovYBZ lMemLaIjIfdd1CajlHA2bB7biT9i7vk/upPktnakWx3bH6nCPcLrXsWog3PncvwBLqDG XaYYUj43Uynv8X98xmOE7Y0Lovk0kii/GUEewEWQuc5QTVW3lHr0S3H9H4Yi0EJt3c7/ 4l+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MyCF4wh1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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. [23.128.96.35]) by mx.google.com with ESMTPS id p12-20020a170902e74c00b001d0ac4424cesi177907plf.138.2023.12.06.17.19.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 17:19:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=MyCF4wh1; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 A0D5A80D6A10; Wed, 6 Dec 2023 17:19:13 -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 S1443025AbjLGBSp (ORCPT + 99 others); Wed, 6 Dec 2023 20:18:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442154AbjLGBS2 (ORCPT ); Wed, 6 Dec 2023 20:18:28 -0500 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 8A5271BCA for ; Wed, 6 Dec 2023 17:18:02 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5d8d3271ff5so1701457b3.2 for ; Wed, 06 Dec 2023 17:18:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911880; x=1702516680; 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=ozafK+YxnV8oHaLDKY1idAuezimM/K5JtLiBl1FMWy4=; b=MyCF4wh1Q3febjw4LwmXl9bao3ZdbKX8Rzsvgng5/kl+IvjKT3ddVR8g4vC7nBGIdq 95Ire528ngTWyB0iz3iWLtYo0TY/2HdD0bmNh7vo94TJ6EKC5SJwzXCFMBj2tuF2bPaG ypVBi7qC+uwiieUkidJ6UZAm8gVB0P2wiFF3XkMj4lIguVHcE2SKJebJWPb1zByDrETT pBakj177G+jSGcWuPrJLjyS2BiUShxDshzuJGYrDtOlXJqwlDxPO3HuQam0zQ+C0X4jO DcePsqT4LUR+ZtuZcyfx1gNR8VNXZz7G2PtWNUSm+vbpDjpubYkiDVY8OJDSfmUpGX6T TqLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911880; x=1702516680; 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=ozafK+YxnV8oHaLDKY1idAuezimM/K5JtLiBl1FMWy4=; b=ZV/ckCotYbd1cRM8myqODlwlkJ4PPW6nXmY9YekSbUZ5PsStGJh9fy1yOfnSL9q+f0 Wnr20Y6AMCo+otMaoJU+LXjXG+OrEUwqEl3KZmRCmu91s9H3PX80/fni/sR5s1WiroZh cZ+tcj79WACSjB35Eme+jlOz11GPPAQOropzIf8aHAhwOPpFwcIokGtlXNcFy105EyUx +7PuTcewlEubwhki6s8OULCDYFwPvFuEoBIBfmRluNgTAqTPijOUGqcIf3AXzPU30Ho8 7P4RUY2Q21XV39p2zO/R/fm54jMjepT4x3D9yqC4XXigBX2e9qO11OQclrITDLNoXCQa S9jw== X-Gm-Message-State: AOJu0YxpTQ18Km1wNZKDP/CjrpeyysPTm0EjPBnX8kf4uuaYfw26S+AZ BaYFPcDO1oGLoA9syKTruBz/Vzal5C3I X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a05:690c:98b:b0:5d4:2ff3:d280 with SMTP id ce11-20020a05690c098b00b005d42ff3d280mr27363ywb.7.1701911879847; Wed, 06 Dec 2023 17:17:59 -0800 (PST) Date: Wed, 6 Dec 2023 17:16:48 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-15-irogers@google.com> Mime-Version: 1.0 References: <20231207011722.1220634-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Subject: [PATCH v6 14/47] 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]); Wed, 06 Dec 2023 17:19:14 -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/RC_CHK_EQUAL. Acked-by: Namhyung Kim 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..be212ba157dc 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_EQUAL(map, 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.rc2.451.g8631bc7472-goog