From nobody Sun Dec 28 17:29:11 2025 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 CA8E8C4167B for ; Thu, 7 Dec 2023 01:20:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443096AbjLGBTx (ORCPT ); Wed, 6 Dec 2023 20:19:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47678 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443001AbjLGBTK (ORCPT ); Wed, 6 Dec 2023 20:19:10 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F3A382689 for ; Wed, 6 Dec 2023 17:18:33 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5d67fc68a82so1634357b3.2 for ; Wed, 06 Dec 2023 17:18:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701911912; x=1702516712; 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=4LNOfIhT6VQbu6YMuQ9Daw6AHLC7GC1HLAQGXsSFOs8=; b=spC5VPaNjMYyYixRS9qcQXXZoWN9vbtNwnlhpT5POFovwtcF9BkGOs+X6uhrTS42dD nx2g1Si0i9s8Vo+aGiuxPvF/K06r9/Cm449seCJRGFAo5W9JUEsp4tw6nMouyLTCNpFN Ma58HAgGEgQiGSCCn9j3fTmfvuBWENZdeOpA0lUOqSAkDeLHDI3EzAn70K4H8rKLIkjL 7h7PBM928LIW90a16orEDSgD21cFSRMxb1OyWWW2DPQnH4ylQ4N4cxWyLmBivOrWFLpj A0lkUmfDB0zFIng5KOQK6xZGXu2RUZo/QjaFaLOUHcNoSh1878zX8aNAqEUL+IFztUrZ linw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701911912; x=1702516712; 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=4LNOfIhT6VQbu6YMuQ9Daw6AHLC7GC1HLAQGXsSFOs8=; b=oOb5jkfqD6NXlN3STR4D+/XtBY11SoBh2hmyD5ry6hLo7/ABK5E1Aijc1CqMGestqI l6QwgQc3n49KIW6HpUHp5GEwtephNr9JwcSXDyE3XgisA6K/DAuavCPbPGPuHmv+UQ06 bj5fstjmooOBAa0mMwqxXbddc3MlVNoLfZe4h6/Z3hgOevNLgGIfWVK5b4nIahoBBaLT fgqhcspvUQR4GsoxBr3ZzCEc9in6yGUi4zOBv5ijCqQpmppbfnXwvfkr93I7V7woseSX 9982dns1Bl23bIZb4skTyZr1THRAipGP+K55CSlOK2929b2xIoyDwy3nmio5byeL2dOi 2FFA== X-Gm-Message-State: AOJu0YzpAF0Pbw2KzH0T0n8DpI45W9O/N/4E9mFqJARkvTzaaiLZfDV5 zQhJmKg3tAaOF7awF3gh6jNmR2f67gsS X-Google-Smtp-Source: AGHT+IFI97iFSQmYgFB4tRS3qVXoRcjvu2Hrx0v/As4OljxClj3ZZta/nbR2nm3SBLgPyQFHJQRGptp33ALc X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:35bf:293e:7696:34e9]) (user=irogers job=sendgmr) by 2002:a81:be16:0:b0:5ca:20f3:ca21 with SMTP id i22-20020a81be16000000b005ca20f3ca21mr31037ywn.1.1701911912594; Wed, 06 Dec 2023 17:18:32 -0800 (PST) Date: Wed, 6 Dec 2023 17:17:02 -0800 In-Reply-To: <20231207011722.1220634-1-irogers@google.com> Message-Id: <20231207011722.1220634-29-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 28/47] perf maps: Locking tidy up of nr_maps 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After this change maps__nr_maps is only used by tests, existing users are migrated to maps__empty. Compute maps__empty under the read lock. Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 2 +- tools/perf/util/maps.c | 10 ++++++++-- tools/perf/util/maps.h | 4 ++-- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 4911734411b5..3da92f18814a 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -440,7 +440,7 @@ static struct thread *findnew_guest_code(struct machine= *machine, return NULL; =20 /* Assume maps are set up if there are any */ - if (maps__nr_maps(thread__maps(thread))) + if (!maps__empty(thread__maps(thread))) return thread; =20 host_thread =3D machine__find_thread(host_machine, -1, pid); diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index c08e412a4313..cb52de9d6c2a 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -528,7 +528,13 @@ void maps__remove(struct maps *maps, struct map *map) =20 bool maps__empty(struct maps *maps) { - return maps__nr_maps(maps) =3D=3D 0; + bool res; + + down_read(maps__lock(maps)); + res =3D maps__nr_maps(maps) =3D=3D 0; + up_read(maps__lock(maps)); + + return res; } =20 bool maps__equal(struct maps *a, struct maps *b) @@ -852,7 +858,7 @@ int maps__copy_from(struct maps *dest, struct maps *par= ent) =20 parent_maps_by_address =3D maps__maps_by_address(parent); n =3D maps__nr_maps(parent); - if (maps__empty(dest)) { + if (maps__nr_maps(dest) =3D=3D 0) { /* No existing mappings so just copy from parent to avoid reallocs in in= sert. */ unsigned int nr_maps_allocated =3D RC_CHK_ACCESS(parent)->nr_maps_alloca= ted; struct map **dest_maps_by_address =3D diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 4bcba136ffe5..d9aa62ed968a 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -43,8 +43,8 @@ int maps__for_each_map(struct maps *maps, int (*cb)(struc= t map *map, void *data) void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void= *data), void *data); =20 struct machine *maps__machine(const struct maps *maps); -unsigned int maps__nr_maps(const struct maps *maps); -refcount_t *maps__refcnt(struct maps *maps); +unsigned int maps__nr_maps(const struct maps *maps); /* Test only. */ +refcount_t *maps__refcnt(struct maps *maps); /* Test only. */ =20 #ifdef HAVE_LIBUNWIND_SUPPORT void *maps__addr_space(const struct maps *maps); --=20 2.43.0.rc2.451.g8631bc7472-goog