From nobody Thu Jan 1 08:57:42 2026 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 784DFC07545 for ; Tue, 24 Oct 2023 22:27:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234973AbjJXW1T (ORCPT ); Tue, 24 Oct 2023 18:27:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344531AbjJXW0f (ORCPT ); Tue, 24 Oct 2023 18:26:35 -0400 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 9F75519B0 for ; Tue, 24 Oct 2023 15:25:50 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a92864859bso45302977b3.0 for ; Tue, 24 Oct 2023 15:25:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698186350; x=1698791150; 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=KF5V0xK2Lq20qZdw/Kvq8/AiFWZ8gPx81P4SISP1cMg=; b=PDoKSkaivxThTnIPOnsPmCIN2WrnTiC/G1oFvPQZnObsne4v8Fnkt11X9zR1X3SxKx NcnqThhriSs99HGL1Go4iZJplH4vxD2Yomiv49I2/Ppkl3GNjT1wNLA7Gx/GoRl2dApp 7a+57Ix5QPU8PxvwEj020HjRPXuMw99Sk/XxfdHTs8aeLjkZF11TWkyADAzdyY4T0lit tRyJ+A+OLLZaHmyRR5IR/sthOvNQv4YCoIN+7pXhHGxOf8dMEN2iJouit+8otPCCAt4m QNVK3RJNjiers2FuKcaKXBReaSZHz+igmitIHkp1yyF/bvgfhHnBxiQmvsUUoMlYglmE ry6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698186350; x=1698791150; 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=KF5V0xK2Lq20qZdw/Kvq8/AiFWZ8gPx81P4SISP1cMg=; b=AV09mhNAq21UNzNUu3F62oaZ7cvRGEfL2BVuRJD9U/55JZOYYY7l7tBqgl/KG7ecYS WmBqYp13VJTiiUj7vD8gk2Wsz1yo7e8dROXLlW3o9j9t3mSVC1hCE1b+yfOwzt0HSiIi oiScJglW/XmGmg1mBf1iu7t7WU0JZ84FcR4ZJ5f1PYPpiwefgnYPLE3S92DhW85o2yIH Mh9Tl/GrW39mcE6p2us/WpaKfb8v2mgFKH9lqUGZFVHyd7uhb70CdFbyRAPE2o//dCq0 cjtvNV+agRAdtX5qL39onLiZRFj2QBrsa9NB9pqD9QMraZx/FYxoq70ffJRzhnGntnvx nzWA== X-Gm-Message-State: AOJu0YxzaOBayV4u9n7Pgj1L2zuXrJKXA9oEYwcrkzYAu4QeQfGuN27a lBs7a5FVOBpCbXyEqlwP6DFYVzKf1/mU X-Google-Smtp-Source: AGHT+IEOJYPhCbWcpSIxfmlsBWUiFbNVJk/FsjGiHcROK7e5DC8L6VJsuAqzhWj901vy+FLDNDuKt7TAyPup X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:93d2:18cc:4d63:45ba]) (user=irogers job=sendgmr) by 2002:a81:4e84:0:b0:5a8:2fb9:aeab with SMTP id c126-20020a814e84000000b005a82fb9aeabmr277735ywb.3.1698186349788; Tue, 24 Oct 2023 15:25:49 -0700 (PDT) Date: Tue, 24 Oct 2023 15:23:46 -0700 In-Reply-To: <20231024222353.3024098-1-irogers@google.com> Message-Id: <20231024222353.3024098-44-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 43/50] 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 , 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 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 42d73f00f9c1..f9c77119af22 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -441,7 +441,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 41e9e39b1b4c..725f5d73e93a 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) @@ -851,7 +857,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.42.0.758.gaed0368e0e-goog