Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp1362303rdb; Wed, 6 Dec 2023 17:19:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IEYbBIGkXnBd24D2oA+goztnqktNxXbBkCanneSIGIQ3OVG6ygc5meQnXhOemXhv+qQH/yN X-Received: by 2002:a05:6a20:5488:b0:18f:e1af:d51b with SMTP id i8-20020a056a20548800b0018fe1afd51bmr2109630pzk.110.1701911956118; 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=V9eS1s5NMuiu5EdM0mO4pK/4LgjdW2Y7CPzQNHDYUkd3oJl0uNCwPM14W5PIpNDTWu JzAcKw4GF2IQa+PySCU1bTAAEFf6qkvlDAJJp2ev+FOZuhSiTQxx+7CsVj4gnPw8In+t TaSrOghkDCKZ2rlYaZ8AFM94bs82Zjn7iS4ohu8CGJUS+jeXKkw2Cb17jCZzED39WM6P TC6Sh6PVC/Cd9oj9H2srdTmk+U+Vvc1FprdUiqdEzNloQ9+USQrUpQkOTszG3Tksy2GP 5Ds5QSbczbYiC59mNiaQBj0UYd29NoMWyohcziC+pJy6XHEGwv2+KsNdurxBXRotY9+P jQLQ== 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=8uJ4Fnb6KBcCgmxkcQQeI5pptPcrA/cGJHUyHI7nyas=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=HdkVTDpuxtMQWEjiIJfm4Ij1RQUtqHz3/JBUqP413bnPzvRyveXfxIJXCTMJOyH8eV +NWuLrP8TLtVf/cBWdgNX/BAat/eO8QBfso+StGG7122oJ6YPYBpQazZEURT2HepPJvz yn7UZab6PpfH9TDwN46ZyByJdf7AiW3j9PRTode24ms/gCx2q1iTkFpo8TtvRt/sRczm yerumwigspNXfKTBLYKnyktVh4xBW1PxBK9HChGbaIoY7qOMXiW8Et9ciKOfq8rmSybb 1J83NJESpUrj1oakOCoFOoPTDNQwHYopHVbB3Af82kBbiPYaV0Lf4Gcv5KrbhIVGFCve p7Pw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OoCZZncP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id x6-20020a056a000bc600b006ce080f6ebbsi252888pfu.225.2023.12.06.17.19.15 (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 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=OoCZZncP; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 howler.vger.email (Postfix) with ESMTP id BBCAB83EE9D4; Wed, 6 Dec 2023 17:19:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1441997AbjLGBRa (ORCPT + 99 others); Wed, 6 Dec 2023 20:17:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52146 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1441989AbjLGBR1 (ORCPT ); Wed, 6 Dec 2023 20:17:27 -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 EEE5610D for ; Wed, 6 Dec 2023 17:17:32 -0800 (PST) Received: by mail-yb1-xb4a.google.com with SMTP id 3f1490d57ef6-db9612b8997so540222276.2 for ; Wed, 06 Dec 2023 17:17:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911852; x=1702516652; 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=8uJ4Fnb6KBcCgmxkcQQeI5pptPcrA/cGJHUyHI7nyas=; b=OoCZZncPPQd5kgK1UOCDwoF+/NmySNc8s713ECp1pSEVfB//uQaj7OTBcg+0fto6Bt uSDlbi7aC/sA5QxPFJylv7RP/PNWaPDhdfH6HISvd1scfuDBvM7CzZ63JAU0tF1b1Oik xOPi6ojQdhezJorS+/uNhcbAsh8MPB1UILsyl6lzywEX0XymaoD9tKhJXC6A7f6jVyUP AShSexM6qybIt2VUOWn+MH0CLu0Yhs9RHH7pJlp3W7jvnnCxv8/Bu2iT1pyTtR28j+U9 fIThd6nJiNQWfxRlTlTeqohN19dOvZe2fq/MeEO3zJwXSr3BBPqruoapZ7w1QSdrUXvG +//w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911852; x=1702516652; 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=8uJ4Fnb6KBcCgmxkcQQeI5pptPcrA/cGJHUyHI7nyas=; b=U9WF7ZUIhlLOhrVsyYPqre77xsVZZKLckE98eU3bRvMmhutfOwdP6gcVmvssAFbXeQ I0YsQcHUEbMFB35KYiFC+l3PLSC8+2Am7XfitrJpkRnwaD50nZiwXYgz5M+N8s5B3z2r X6pAruJs80cvGX5mM/wF3t6AualDVqUcUGwGvsFW7FLXzslBG/4b5AhgxCF7DZNU8K74 2q6hlVChstF6XPK1xnWqX5ycWNG9fVCzpgohEooTxtKXNDtADBjVF7Vmc3wCxz63EmKr 8AslCCXJqNRR/MksFhvQ/D3pgk/GQdGDYQMZWlqceHb7v/FXHiUyORqNFyTkMkq/zvVY 9jbw== X-Gm-Message-State: AOJu0Yzkk9N3FAWcGZWhHiiNC+djxzSd0hOEOxZDT9UHQR11TroYJC5N E4GB6hX8fmiq38oFtyt5YmJe4rqGbR2U X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a25:cb0a:0:b0:db5:3aa1:827c with SMTP id b10-20020a25cb0a000000b00db53aa1827cmr32204ybg.13.1701911851794; Wed, 06 Dec 2023 17:17:31 -0800 (PST) Date: Wed, 6 Dec 2023 17:16:36 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-3-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 02/47] perf maps: Add maps__for_each_map to iterate maps holding the lock 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 howler.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 (howler.vger.email [0.0.0.0]); Wed, 06 Dec 2023 17:19:05 -0800 (PST) The macro maps__for_each_entry is error prone as it doesn't require holding the maps lock. Add a new function that iterates the maps holding the read lock. Convert maps__find_symbol_by_name and maps__fprintf to use callbacks, the latter being an example of where the read lock wasn't being held. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 99 ++++++++++++++++++++++++++++-------------- tools/perf/util/maps.h | 3 ++ 2 files changed, 69 insertions(+), 33 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 9a011aed4b75..160a6dce54bb 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -196,6 +196,21 @@ void maps__put(struct maps *maps) RC_CHK_PUT(maps); } +int maps__for_each_map(struct maps *maps, int (*cb)(struct map *map, void *data), void *data) +{ + struct map_rb_node *pos; + int ret = 0; + + down_read(maps__lock(maps)); + maps__for_each_entry(maps, pos) { + ret = cb(pos->map, data); + if (ret) + break; + } + up_read(maps__lock(maps)); + return ret; +} + struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) { struct map *map = maps__find(maps, addr); @@ -210,31 +225,40 @@ struct symbol *maps__find_symbol(struct maps *maps, u64 addr, struct map **mapp) return NULL; } -struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp) -{ +struct maps__find_symbol_by_name_args { + struct map **mapp; + const char *name; struct symbol *sym; - struct map_rb_node *pos; +}; - down_read(maps__lock(maps)); +static int maps__find_symbol_by_name_cb(struct map *map, void *data) +{ + struct maps__find_symbol_by_name_args *args = data; - maps__for_each_entry(maps, pos) { - sym = map__find_symbol_by_name(pos->map, name); + args->sym = map__find_symbol_by_name(map, args->name); + if (!args->sym) + return 0; - if (sym == NULL) - continue; - if (!map__contains_symbol(pos->map, sym)) { - sym = NULL; - continue; - } - if (mapp != NULL) - *mapp = pos->map; - goto out; + if (!map__contains_symbol(map, args->sym)) { + args->sym = NULL; + return 0; } - sym = NULL; -out: - up_read(maps__lock(maps)); - return sym; + if (args->mapp != NULL) + *args->mapp = map__get(map); + return 1; +} + +struct symbol *maps__find_symbol_by_name(struct maps *maps, const char *name, struct map **mapp) +{ + struct maps__find_symbol_by_name_args args = { + .mapp = mapp, + .name = name, + .sym = NULL, + }; + + maps__for_each_map(maps, maps__find_symbol_by_name_cb, &args); + return args.sym; } int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) @@ -253,25 +277,34 @@ int maps__find_ams(struct maps *maps, struct addr_map_symbol *ams) return ams->ms.sym ? 0 : -1; } -size_t maps__fprintf(struct maps *maps, FILE *fp) -{ - size_t printed = 0; - struct map_rb_node *pos; +struct maps__fprintf_args { + FILE *fp; + size_t printed; +}; - down_read(maps__lock(maps)); +static int maps__fprintf_cb(struct map *map, void *data) +{ + struct maps__fprintf_args *args = data; - maps__for_each_entry(maps, pos) { - printed += fprintf(fp, "Map:"); - printed += map__fprintf(pos->map, fp); - if (verbose > 2) { - printed += dso__fprintf(map__dso(pos->map), fp); - printed += fprintf(fp, "--\n"); - } + args->printed += fprintf(args->fp, "Map:"); + args->printed += map__fprintf(map, args->fp); + if (verbose > 2) { + args->printed += dso__fprintf(map__dso(map), args->fp); + args->printed += fprintf(args->fp, "--\n"); } + return 0; +} - up_read(maps__lock(maps)); +size_t maps__fprintf(struct maps *maps, FILE *fp) +{ + struct maps__fprintf_args args = { + .fp = fp, + .printed = 0, + }; + + maps__for_each_map(maps, maps__fprintf_cb, &args); - return printed; + return args.printed; } int maps__fixup_overlappings(struct maps *maps, struct map *map, FILE *fp) diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index a689149be8c4..14ad95979257 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -81,6 +81,9 @@ static inline void __maps__zput(struct maps **map) #define maps__zput(map) __maps__zput(&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); + static inline struct rb_root *maps__entries(struct maps *maps) { return &RC_CHK_ACCESS(maps)->entries; -- 2.43.0.rc2.451.g8631bc7472-goog