From patchwork Thu Dec 7 01:17:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 174847 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp4488303vqy; Wed, 6 Dec 2023 17:20:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IHBTdxlxWyeollMG3vQ+dvreI1T8WbJvlgbumYd0aFRn9U6E4QvEkPw7uuavgzBKMi47XAb X-Received: by 2002:a17:90b:3eca:b0:286:8f8f:5e3d with SMTP id rm10-20020a17090b3eca00b002868f8f5e3dmr2142223pjb.30.1701912016191; Wed, 06 Dec 2023 17:20:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701912016; cv=none; d=google.com; s=arc-20160816; b=tMXjth9sDPW2A66VUGlm4IGhozKv8WoQ6JwoWOApFifP7Ul3FujKgbSu+FjTHwnlTR qc4vNYVXORHmmaPvpSfpPNEYRJvwlxoyPIhFGkrCMLE4rCPqP7/WMG331LNsK/oUhGD9 vWDijAMjRk+kIpI6LkLBoPYJex/lW3TE4RiDKf1MFw5IkgjdLcydp/Lg4fKd8H3EKOET c8mI/4qpT355nSnlQvqxnfOtnHVdsxal0136Ur8rhINdZiMsOMLmjlIMg8gp1JCzWyTT Wi5gzbUSzx6yVVlVteV/UDfaO7ald3hvc9TBoJn1X3H6bCDfFgKDhnNwZIBiClDXfoGn Uvdw== 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=4LNOfIhT6VQbu6YMuQ9Daw6AHLC7GC1HLAQGXsSFOs8=; fh=Io7x3OacpefNRRNKoYAzVE0aWTDwD7FRzyCdD1e3AN4=; b=Crh6/18yC6W3//spNL/dvr1oGu0EXCT0fEfaWNGq19ZXqV2Z8VguNsqYtZ6qOT77j6 AjU1ZkQbP119dKpBSKJbNQUydMSahhUiJZcTh1F2/P+ohhyhVZkgqVTlzxGRuP4V0ox2 iEzBtdpdBtJkK0J/sZbcUgF5w4HPd+Pfi8z2PoRQaKKXfH59XX4XbSLIyBLX5BQBUZey 7zBQrCMfoiqIQkON/DShy0WZtT7OnvW4ziUuicMdHqjDzDoyTUOetPi7jyUY28bKSeQe oZLY0fdeOCRJYRuKhZP+JGddai0tLJfG/5blBkBGxxlY4itrg1qn5FO5QO5fFcws/hMV 8Dyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=spC5VPaN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id 19-20020a170902c11300b001d08c8e5115si160021pli.237.2023.12.06.17.20.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 17:20:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=spC5VPaN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 snail.vger.email (Postfix) with ESMTP id 397BB80DEA74; Wed, 6 Dec 2023 17:20:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443047AbjLGBTw (ORCPT + 99 others); Wed, 6 Dec 2023 20:19:52 -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-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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Wed, 06 Dec 2023 17:20:15 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784584094458427188 X-GMAIL-MSGID: 1784584094458427188 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; /* 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 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) 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) @@ -852,7 +858,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);