Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net [23.128.96.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BF893CCF2 for ; Tue, 24 Oct 2023 22:26:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PDoKSkai" 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 A2FAE1BEC for ; Tue, 24 Oct 2023 15:25:50 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-5a88f9a1cf7so64621477b3.3 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=nuoGyWYDtPJT1A06BWbxa73lR4NQ1+pHSeBoZshnaEVsK4OZ+0XcF09Yp47xNrKBub /P8nLuinxFJ8uSGvHi4evfNIVfWCDMif24/ZdXuHavX1OiC0S8pt69FIPRZpya+gd4V+ lzyQdUXZkTUWAzd20riyX+KjXQrb9jG/pWmTqRKtkMh4x/yUyOL791FnWWhtRPt5lcFm 1lvPpTU7LCAEFL0e2rC8rdn57oFFdbCCNqjcMAIE+lqKzoGlQ3Uux3x700ssZnAjFzjA 4FRI/nqD8zvjpqhW9ioPRNPg9mR2e5V/SzUhH6wLY2Ibo+xbBqqkKvWTXUNzQ4sVBeky LWPg== X-Gm-Message-State: AOJu0YwJnsAc8gHSwRVOcf5lj4a0Qi8uBloTYqXTY2aB8fPeIfb+0+0W Kx/5sbKImZXaYK0olHDVts/9OR42jJfH 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> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: 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 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; /* Assume maps are set up if there are any */ - if (maps__nr_maps(thread__maps(thread))) + if (!maps__empty(thread__maps(thread))) return thread; host_thread = 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) bool maps__empty(struct maps *maps) { - return maps__nr_maps(maps) == 0; + bool res; + + down_read(maps__lock(maps)); + res = maps__nr_maps(maps) == 0; + up_read(maps__lock(maps)); + + return res; } bool maps__equal(struct maps *a, struct maps *b) @@ -851,7 +857,7 @@ int maps__copy_from(struct maps *dest, struct maps *parent) parent_maps_by_address = maps__maps_by_address(parent); n = maps__nr_maps(parent); - if (maps__empty(dest)) { + if (maps__nr_maps(dest) == 0) { /* No existing mappings so just copy from parent to avoid reallocs in insert. */ unsigned int nr_maps_allocated = RC_CHK_ACCESS(parent)->nr_maps_allocated; struct map **dest_maps_by_address = 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)(struct map *map, void *data) void maps__remove_maps(struct maps *maps, bool (*cb)(struct map *map, void *data), void *data); 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. */ #ifdef HAVE_LIBUNWIND_SUPPORT void *maps__addr_space(const struct maps *maps); -- 2.42.0.758.gaed0368e0e-goog