From patchwork Sat Feb 10 03:17:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Rogers X-Patchwork-Id: 199198 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1272279dyd; Fri, 9 Feb 2024 19:20:19 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVvXhPSITlqaPw0gw8i56EixEQ+OYDMUzyoHw+mbQjhf4NRYWJfGISoCTCH7G1xL+Lc2nErgXr8YjB1Ku9hl0vSJNnJJw== X-Google-Smtp-Source: AGHT+IEJlifSaUpSi1Er1uTIYG4Yt0HLpIP9IQyRSF3wS/lLWgAC2LfIIPq6U87Se2q1A9eHIuxl X-Received: by 2002:a17:903:1ca:b0:1d6:fcc3:c98f with SMTP id e10-20020a17090301ca00b001d6fcc3c98fmr1032223plh.29.1707535219714; Fri, 09 Feb 2024 19:20:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707535219; cv=pass; d=google.com; s=arc-20160816; b=mmuYzkaxhHl7AynoTYy5C3EAkHOKr2dLBvT/helLRzGgRMi95/8eSujQk70LfV07ck 2oJkYyA1idsQYFVlE8fDZj9E0f6wVOmx1MMl89xhG5DZ+iLDtLTA0hK8qiF624oQweZ8 RDaLUV2p+yc7s+7GwM10dc5jmNOgk5cyVavZSvy623YcuoWGOeygTgA7RyFyw415Z5e0 94dp22C5xeVDTjiBQ6RD9sBps0hUIP/E2UlZkkVxp7LHDhAjFP374qKWJuGcNgSrq1sV zEul8RlNJdOXm9VwpqmY+mzdFgkd9Cu8lvFIFD5cHKtiCK4Hjd0hlGgkVF0VOwR/sJd6 3UUg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=to:from:subject:references:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:in-reply-to:date :dkim-signature; bh=ie89sB6IlXpvkiQnUaYF5PTvKc7dfqYdcE9ndiylfmM=; fh=2Gx/0lWPjHt1ZhyYB6nZ63XC4gyJrk0vEB8ofyoC9UQ=; b=wf1uLjiQN1xKuVIp+gAlb2a7RFodiTWksnHEJDkMk0f4nS76ZTcWpHhf7F4iMHZhOf baPfecDsAKlelIs3BnKs9VxdKfupj5gisnE3+nnRm10PzYDtlZ4GBwalmmU1FIIWW4TE +1n3QLrvj4MBtiVKKlaEI2wYdJKLIhw15SK+HgdG/Ufm9QdpnTbRBVPwyOIl2yHsNcNm VxV8l2ej1AX0UystlfpMW/sGBLchfpeMBiuS6KKfqVYlOU3CmiDxKHhthT7BCvQt24jY WiEoFnFqb8OQI47PxWw+slU3dnKQELakkITruIyYoa3Q3A1E+6FMTM/8ISrOpfqgK9kc gogg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ueZrGJ0I; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-60269-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60269-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com X-Forwarded-Encrypted: i=2; AJvYcCWS2uDMUW4xrAO+kPGeJm1HqJW3n+xadMmDEtnRwD/lEOEK4T15Fos70nNMgfn6boEZWvJv6mVpZ2yN87IeawZtVqP6YQ== Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id l6-20020a170902d34600b001d952ae03fcsi2627009plk.602.2024.02.09.19.20.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 19:20:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60269-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ueZrGJ0I; arc=pass (i=1 spf=pass spfdomain=flex--irogers.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-60269-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60269-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 7424C2821AB for ; Sat, 10 Feb 2024 03:20:19 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4164563B3; Sat, 10 Feb 2024 03:18:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ueZrGJ0I" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9FA5F171D0 for ; Sat, 10 Feb 2024 03:18:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707535093; cv=none; b=aGVMbUQeAe44xO2Vrdjh5iQttEb+osLq12RFBpJfcL9q26J/jayPrU0rtWVmtMJO/OrbGUpKMLiVdSGS+1ineXu3emo3UigpQr3ZTWxhNh85tO9o/foKZLJX/AqjoX605VOvIbFS0gqzI6W4lQtZ98I0V53aVC5omS736bjQZnQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707535093; c=relaxed/simple; bh=fmY7OGiQyoafs0dUqE06bQrvTXGFG0wOIGJr3qrBCDk=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=b3dzlblQ8CCEmmcIstBhZTLoL7ms8kFVZs8lrD6Sx6sYkL2fh64OP2O4W5BFHvw8sgcFxs2vDM1h61NZ/TshgBcRLEpSrm4FDr9ng1+7XVjyIY2lrdtFnBjNvCS490UMJowsJjTcEl7iUTFVCbdo++qaQGRSBR+NfKXE/KZ0pYY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ueZrGJ0I; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-603c0e020a6so25565527b3.0 for ; Fri, 09 Feb 2024 19:18:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1707535090; x=1708139890; 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=ie89sB6IlXpvkiQnUaYF5PTvKc7dfqYdcE9ndiylfmM=; b=ueZrGJ0IuPfGlwXCKjovy5gGGS30jkNrGtsNC8pnicR1KIk7lhl7dwkZUTTREAOoJn NztsMiVXbJgsmEcNLj4vd4HNyYmGU6iDVlGEsEDePPLWhfY0MrnR1rsXNVEXNnrUx7Xq Ya6tpJEpWKjWuLP/CZ7d5tmfZU0ulM+GHc7NbiuEBH9vpd/pt/jYMMkvWMnni+Zhlab1 x7qQZ8ux3//bPdqmSGd+SC2MqMA0k3M7nDu8yY6UgHEBm/SlbSS5PKGXZnL/lP0dawbD gMm2dlCbtU9N4Rx+SXav2XKoBtqnExXgy4hn2CCnLJNAsuhuna7RTGWqy4QthZpYDbwg Iv2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707535090; x=1708139890; 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=ie89sB6IlXpvkiQnUaYF5PTvKc7dfqYdcE9ndiylfmM=; b=SjyNw+J43iABMvDT8pQMuhRHfnJ+PvKUt/TisUECtemCZfCPZICLYlnJD0H1RGgFF9 i1iacZLmxMvjQ2csHvxvvkiNtAhLDeoKsUMmtjndmTA8VBiLDLP8j7kaCg0vzgdo2NYw ge068lMmeO2pyKu2EuToifnwu5bpK4Vesgxqw610rB4RlJ988OoVozhnOXRam4A3Kf49 A2N78j5clyVGbbuQ9XShvpEY/ckedXxyDbaw3Ci9aSCAtys5tzLoPc7I70h4dNr/cBrS HqYZRUE7dhOQ6+2/7zUTJbxacE0B2tN0NxJYUqnm1/+53T8E/Kvp7qmUmlT1vqYaoEKq M30Q== X-Forwarded-Encrypted: i=1; AJvYcCUwfHG71i0+M/VtGzEx/bMHc2ug+IxCyvK2JEoOXICnCt4ZnNNxnI0fZJTUqLNHvbSZ8h6qcnIQYVPgqvSCqtWKmgFaC+FzBSFIfTJD X-Gm-Message-State: AOJu0Yz6RVAZ5Uj0+puMaDHjlO+6DKMGjtReHREK5JFGKVKOTEOQUGCc /vdocJ8ME/p861d46x1y2jWupIf5a/KcZQKNP+aGcO5JYI1dwxYhYYRozBMuw9v5jzKzrW0M0rW YpWvqcQ== X-Received: from irogers.svl.corp.google.com ([2620:15c:2a3:200:877:241d:8c35:1c5b]) (user=irogers job=sendgmr) by 2002:a0d:d549:0:b0:604:499:fee1 with SMTP id x70-20020a0dd549000000b006040499fee1mr191533ywd.6.1707535090579; Fri, 09 Feb 2024 19:18:10 -0800 (PST) Date: Fri, 9 Feb 2024 19:17:46 -0800 In-Reply-To: <20240210031746.4057262-1-irogers@google.com> Message-Id: <20240210031746.4057262-7-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240210031746.4057262-1-irogers@google.com> X-Mailer: git-send-email 2.43.0.687.g38aa6559b0-goog Subject: [PATCH v3 6/6] perf maps: Locking tidy up of nr_maps From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Song Liu , Colin Ian King , Liam Howlett , K Prateek Nayak , Artem Savkov , Changbin Du , Masami Hiramatsu , Athira Rajeev , Alexey Dobriyan , James Clark , Vincent Whitchurch , Leo Yan , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, bpf@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790480450261826975 X-GMAIL-MSGID: 1790480450261826975 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 Acked-by: Namhyung Kim --- 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 439cefab112a..53aea6d2ef93 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -541,7 +541,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) @@ -871,7 +877,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);