From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244316; b=bdIYCRuHitYYc2bBTyVvVXX7lVPm+N/bzbXhrtjyD4WCLaoro0aeCcpD1QiX9XJoYwZ2 x+Fn4iym3iSH4zkxe+qzUBz+BEZfftbheFUGyKlz+esR05N4nJZHLUL+0RhWr4Iioa4St PO0WuKLNTV5Rua+Rpc/Xwe7APhnqhKsqQnrPz3q+hfZaYNYLKqAmTjbOj6Uby4dC+ztUK Mg2+wujiuuQXT061hM3vmibMtR1uNsDqbaBiwfc62pawvMxbWfi5lBwlZulEw2Za4HU09 BFXPGCtYOwqHlmjGaU3dyIY8gY5dw/VjLQekk+FF7VHo+4OLU7E3BNSrG/YsNianc2Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244316; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=WFQYbBMhFIDNWh8RpPfdlWzTylnEhajA5E05wFXNi1k=; b=D7bUiZi0uIU7PbM6TIPe8ygvsBhWenh8vYp2UyzSaw04AwVQDUeZhpq5VSJYOG1tdzq8 NOqSULeuEvGuPBYvEalzmBiSq/Cm5gFUAWuD1LwunrssvH0RDpsvlr1ofqJjPWl9zcyOy uHQcRNtq6yxIXL4amIPAisouuq83mpjVX3h08rOgmSd8c7EAU1LhEPCMBY7nqC9FBuZeZ idnxGbRJxrCrn7agJhFDGyar+tVSVTTVs3Hq7FBpRlxmoh90QPUz+xI5NHVh8RFI2zCoa 9ZbRBy8sb5plwPM4bap4aljsIPC6ircmpUDtRcuDLL8NU366Uh4XxOjGnwk2G9sN7hg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=JFYp3AiY; arc=none smtp.remote-ip=209.85.210.70 Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 209C7606A334 for ; Sat, 21 Nov 2026 06:58:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 209C7606A334 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=JFYp3AiY Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-862ae90bbdfso1933741a34.1 for ; Fri, 20 Nov 2026 22:58:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244315; x=1795849115; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=WFQYbBMhFIDNWh8RpPfdlWzTylnEhajA5E05wFXNi1k=; b=JFYp3AiYr2KxTluRFn6uMY7+yBx6+T7HVipHkYOk9Cez6+9NOVHCYf/1XHQvoqqhYi kuTEGw27MbAqIiq9lj62ufeFNKUuGJe9LTgTPBqISdFmvpdyCYf1ZAkZ3wSvKYDUGwfz T+faDkqA8yLt73wfcrpmH/5y3MaQEyarDdetrktLzodFY9LHSMFzHw5R/yS9BmRmMg7S uXkMMv9p17DA1Js2E/yz9a/fq7GysX9eBK/9QV9SPjOwWi/umVw02v5pq9Dcv9kkPXbt I9+q2Fo1vzetwxkYzcNdAx9xWCerlftk2PrWgUOXvpHSo9CFZb+KFYCOfwN5aS3vKaCG 9pJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244315; x=1795849115; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=WFQYbBMhFIDNWh8RpPfdlWzTylnEhajA5E05wFXNi1k=; b=0505im8Ld18a+7bG4EMlHzft2dvAQLxAQNwnkMX3VQN4olQA3f6nOiIwTTfy9FiQgL h/An0gnAcOt+WYm6AbKDXi4YJgI0juTGVBeEacJaE93p1AvsblCbtyjnAcnija+ni/jm ErU+qxjpM8ADYX1Syvmmgfp2kZrZ0WU+CEz2VWmU50q40e0zeC3LXEqUInQ+pMffyjiJ 42F2qgsXXLlKv14UA1sQ0cxapDjtRbGinpau66BVPEL6brCX4YJx90SIQ6Rj3YsZN250 wSjo+gTl9wIVGM/IMolDA1PqXxyOk7w2pFTi70g2olMx6w38US0bx905zwHif0i/CEF0 LP5g== X-Forwarded-Encrypted: i=1; AL+hErEyZev0Rb6tM4Qo462QxVHgBSumIAVV/LjTHh6xxgD5LaE2ThM2axsGoCRvn+tafEqQ3EgDEiai4NWKefg=@vger.kernel.org X-Gm-Message-State: APuU4Z8gFB39zDD9yDCVO2QE8Dld3T+6EbFjretGzU2u0az7wUH4UfpV ei1jIRdTmHPsnoBwkFA16eGiS7wPAfcZfEELGDs7wliH+3GIlRlShugB66VFvKYwmZdpHMnBlh3 MhYl/Gj9fBA== X-Received: from oibmo2-n1.prod.google.com ([2002:a05:6809:702:10b0:579:7e19:d2e]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:1314:b0:572:75e6:1cf3 with SMTP id 5614622812f47-57a0b3464fcmr6263252b6e.35.1795244314813; Fri, 20 Nov 2026 22:58:34 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:50 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-2-irogers@google.com> Subject: [PATCH v15 01/23] compiler-context-analysis: Avoid unused parameter warning in _context_unsafe_alias() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" _context_unsafe_alias() does not use its parameter 'p', which triggers -Wunused-parameter when building code that enables -Wextra (such as tools/perf, tools/lib/perf, tools/lib/subcmd, and tools/lib/symbol). Explicitly cast 'p' to void to silence the warning, matching __assume_ctx_lock() and __assume_shared_ctx_lock(). Reviewed-by: Marco Elver Signed-off-by: Ian Rogers --- include/linux/compiler-context-analysis.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/compiler-context-analysis.h b/include/linux/comp= iler-context-analysis.h index 8302ebc2ea8c..024e303188ad 100644 --- a/include/linux/compiler-context-analysis.h +++ b/include/linux/compiler-context-analysis.h @@ -243,7 +243,7 @@ * alias as a function argument). */ #define context_unsafe_alias(p) _context_unsafe_alias((void **)&(p)) -static inline void _context_unsafe_alias(void **p) { } +static inline void _context_unsafe_alias(void **p) { (void)p; } =20 /** * token_context_lock() - declare an abstract global context lock instance --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.200 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244321; b=h7frUBGj+sp014NTx2A0SRHmTfe6LBkq8L+9TuzXfneGaOMLM/rmFHENmxfYrLEWVVH5 X9zKyS+3NgtU/+Y2Y3upWu1hYKEJXMiomlfJWGuNg9hiDkbWTCE7Z6wSwrUs5YZBg1/2/ 9vMtrkYBDY0MEBzuqT8Ov3HPgT9sk79off1sE552wv31ZQwKp6q55/D1bgL6BDu0Ij38N DF61K6jymkBkSJJJk354jKMg3D68CWgcnoqrqp7IYtLfDcYaJX6UiHHqpUw1WyLfR5rDa N122i8NNHpVOMlzdLbKsDhEbuUi+B3wEvCcjAHhrfY3FYZhogeEN6alnBmg5UX83btw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244321; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=7nLG5RcDHPBvTBoMSoLdxm789yCh2MszVwCa8wSqfvw=; b=CZfb8ZwweOLcy+cso/THNFRQuztORjREYCE2uRTNMxOSkWxcT92A9RLif4QdtVz1Atp0 IkJTY3dyCY6Kk4cPrDRSIOm+j+AAgPebZTtMO3eeQpDZM+m+tjkO5vUV9SOsjA2emt9wF kK60KVD8zNZjHtklE6Q3hvLNqf1O6SFm3WqyJagagDkZMAxb9bxw1TSG4T4CXkazU7uWP zNPfq+J1MBChBb1YpSs9+QClKA7E6/ZHmscf/1vJN9Ln+qoQtux4HqqM1L5TUiM9BhOoq hNFvF5AVlpmiOgLGaIV/vLe5TsF6qShputiqdw6hGawCz/k0Z4JGqSb682gbCNEYIjA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=InlBiBXw; arc=none smtp.remote-ip=74.125.82.200 Received: from mail-dy1-f200.google.com (mail-dy1-f200.google.com [74.125.82.200]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4E0C606A32C for ; Sat, 21 Nov 2026 06:58:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A4E0C606A32C 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=InlBiBXw Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-33c35f5ca6cso2172514eec.1 for ; Fri, 20 Nov 2026 22:58:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244320; x=1795849120; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=7nLG5RcDHPBvTBoMSoLdxm789yCh2MszVwCa8wSqfvw=; b=InlBiBXw3kdme9EtMZPgtAXC/jf05VDfEfHhv73MhqFXdsXXu7eIQXv8zFm+BtlCiO Nx3vopqphHLEgw8UN3KKwnl7kmgGyVmg15mJB1NzIt6lbNz0mpPL3nFfQA5DtNEk5Hf3 YOkRzCOG8/cvZVXHkxiOSNMendevIUxHJ5sdbEwBhVW0Zq8K/b+c0VyCAu4lk+1/IO+W ggDPiNxpIZ1nhFlusg5VX3OErWvndayMwWMGLhU70HdqSPe7u+NgrpJldnwGF5GVuu6T PzuezZbTAburTtJZNMS9i5PvMpunRLNzTwAWEZHKZ/FugMJywkyOnZqSsKx8fV860y/I hr0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244320; x=1795849120; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=7nLG5RcDHPBvTBoMSoLdxm789yCh2MszVwCa8wSqfvw=; b=gKWgI79Ao6fZAMcl6rE0TZ+hceOeH3Wym05BuLMp23ibE7JEcpOUB4cmEf/jM5eDDb q0BYR4XDy1W9UsieLB6RDqw2kLDZN/ksbS6i/WeGzyH+5CRcUSX4rkcO61/hngebIM2w jqnb4yCrTvHARsvBigZBH2SI5/d8jckfu/CoYTszNyulK5P6xMF+FdCpIM4ioKUu/nFN 6ReANnhollFiqY8aOnbGKCMaV7dKbfqJ/OUkkmW6Jt2tjTuVLwh9qpwbQ5F7Lt+BE/nh QHIo3K6vrs7tC97KUCsv2hniCzc2C+9D0cWwfMZMYXwJyWH6gzS2blkk5EH0G/Ybu/PN bk4Q== X-Forwarded-Encrypted: i=1; AL+hErHbkTx8uxnUkDfZerPWeJMFUFNsGmuI0AyBFRvJImyT+xmVwW+uHawvqjJ5TWK4nPsjnELo5jo2Bpx+ov8=@vger.kernel.org X-Gm-Message-State: APuU4Z+zs8hOJDGwlCdM7EikF57DCzpIHb4kNSpBMoigQFISJuf2TXd+ jIowaisbCnEkWFX9ymOJvw/uMNCB8jw83jIoq238yxt89Rq92jySOcrdkkab7vp5CpXVUWKDpqK w38EkFSBp2A== X-Received: from dycju14.prod.google.com ([2002:a05:7301:fa0e:b0:385:d2b9:379d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:e9ce:10b0:385:2564:c509 with SMTP id 5a478bee46e88-385700541a6mr6209845eec.24.1795244319090; Fri, 20 Nov 2026 22:58:39 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:51 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-3-irogers@google.com> Subject: [PATCH v15 02/23] tools headers compiler: Synchronize compiler_types.h with the kernel sources From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Synchronize the non-__KERNEL__ sections of tools/include/linux/compiler_types.h and tools/include/linux/compiler-gcc.h with the kernel sources (including ___PASTE/__PASTE, BTF_TYPE_TAG, object-size helpers, asm_goto_output, ASM_INPUT_*, asm_inline, __typeof_unqual__/__unqual_scalar_typeof, __signed_scalar_typeof, __native_word, compiletime_assert, and the __diag pragma helpers required by compiler-context-analysis.h), and remove the duplicated definitions from tools/include/linux/compiler.h. Signed-off-by: Ian Rogers --- tools/include/linux/compiler-gcc.h | 72 +++++++++- tools/include/linux/compiler.h | 50 +------ tools/include/linux/compiler_types.h | 206 ++++++++++++++++++++++++++- 3 files changed, 275 insertions(+), 53 deletions(-) diff --git a/tools/include/linux/compiler-gcc.h b/tools/include/linux/compi= ler-gcc.h index e20f98e14e81..e005e8d5dfda 100644 --- a/tools/include/linux/compiler-gcc.h +++ b/tools/include/linux/compiler-gcc.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_H_ +#ifndef __LINUX_COMPILER_TYPES_H #error "Please do not include directly, include instead." -#endif +#endif /* __LINUX_COMPILER_TYPES_H */ =20 /* * Common definitions for all gcc versions go here. @@ -33,10 +33,76 @@ #define __packed __attribute__((packed)) #endif #ifndef __noreturn -#define __noreturn __attribute__((noreturn)) +#define __noreturn __attribute__((__noreturn__)) #endif #ifndef __aligned #define __aligned(x) __attribute__((aligned(x))) #endif #define __printf(a, b) __attribute__((format(printf, a, b))) #define __scanf(a, b) __attribute__((format(scanf, a, b))) + +/* + * Turn individual warnings and errors on and off locally, depending + * on version. + */ +#define __diag_str1(s) #s +#define __diag_str(s) __diag_str1(s) + +#ifdef __clang__ +#define __diag_clang(version, severity, s) \ + __diag_clang_ ## version(__diag_clang_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_clang_ignore ignored +#define __diag_clang_warn warning +#define __diag_clang_error error + +#define __diag(s) _Pragma(__diag_str(clang diagnostic s)) + +#if __clang_major__ >=3D 23 +#define __diag_clang_23(s) __diag(s) +#else +#define __diag_clang_23(s) +#endif + +#define __diag_clang_all(s) __diag(s) + +#define __diag_ignore_all(option, comment) \ + __diag_clang(all, ignore, option) + +/* + * clang has horrible behavior with "g" or "rm" constraints for asm + * inputs, turning them into something worse than "m". Avoid using + * constraints with multiple possible uses (but "ir" seems to be ok): + * + * https://github.com/llvm/llvm-project/issues/20571 + */ +#define ASM_INPUT_G "ir" +#define ASM_INPUT_RM "r" +#define ASM_OUTPUT_RM "=3Dr" + +#define CC_HAS_TYPEOF_UNQUAL (__clang_major__ > 19 || (__clang_major__ =3D= =3D 19 && __clang_minor__ > 0)) +#else /* !__clang__ */ +#define __diag_GCC(version, severity, s) \ + __diag_GCC_ ## version(__diag_GCC_ ## severity s) + +/* Severity used in pragma directives */ +#define __diag_GCC_ignore ignored +#define __diag_GCC_warn warning +#define __diag_GCC_error error + +#define __diag(s) _Pragma(__diag_str(GCC diagnostic s)) + +#if GCC_VERSION >=3D 80000 +#define __diag_GCC_8(s) __diag(s) +#else +#define __diag_GCC_8(s) +#endif + +#define __diag_GCC_all(s) __diag(s) + +#define __diag_ignore_all(option, comment) \ + __diag(__diag_GCC_ignore option) + +#define CC_HAS_TYPEOF_UNQUAL (__GNUC__ >=3D 14) +#endif /* __clang__ */ diff --git a/tools/include/linux/compiler.h b/tools/include/linux/compiler.h index 03ecf9086643..505fbb0080d0 100644 --- a/tools/include/linux/compiler.h +++ b/tools/include/linux/compiler.h @@ -1,41 +1,11 @@ /* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_H_ -#define _TOOLS_LINUX_COMPILER_H_ +#ifndef __LINUX_COMPILER_H +#define __LINUX_COMPILER_H =20 #ifndef __ASSEMBLY__ =20 #include =20 -#ifndef __compiletime_error -# define __compiletime_error(message) -#endif - -#ifdef __OPTIMIZE__ -# define __compiletime_assert(condition, msg, prefix, suffix) \ - do { \ - extern void prefix ## suffix(void) __compiletime_error(msg); \ - if (!(condition)) \ - prefix ## suffix(); \ - } while (0) -#else -# define __compiletime_assert(condition, msg, prefix, suffix) do { } while= (0) -#endif - -#define _compiletime_assert(condition, msg, prefix, suffix) \ - __compiletime_assert(condition, msg, prefix, suffix) - -/** - * compiletime_assert - break build and emit msg if condition is false - * @condition: a compile-time constant condition to check - * @msg: a message to emit if condition is false - * - * In tradition of POSIX assert, this macro will break the build if the - * supplied condition is *false*, emitting the supplied error message if t= he - * compiler has support to do so. - */ -#define compiletime_assert(condition, msg) \ - _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) - /* Optimization barrier */ /* The "volatile" is due to gcc bugs */ #define barrier() __asm__ __volatile__("": : :"memory") @@ -48,10 +18,6 @@ #define __always_unused __attribute__((__unused__)) #endif =20 -#ifndef __noreturn -#define __noreturn __attribute__((__noreturn__)) -#endif - #ifndef unreachable #define unreachable() __builtin_unreachable() #endif @@ -68,11 +34,6 @@ #define __naked __attribute__((__naked__)) #endif =20 -/* Are two types/vars the same type (ignoring qualifiers)? */ -#ifndef __same_type -# define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(= b)) -#endif - /* Enforce static storage duration. */ #define ASSERT_STATIC_STORAGE(name) \ static typeof(name) * const __always_unused \ @@ -242,11 +203,6 @@ static __always_inline void __write_once_size(volatile= void *p, void *res, int s __u.__val; \ }) =20 - -/* Indirect macros required for expanded argument pasting, eg. __LINE__. */ -#define ___PASTE(a, b) a##b -#define __PASTE(a, b) ___PASTE(a, b) - #ifndef OPTIMIZER_HIDE_VAR /* Make the optimizer believe the variable can be manipulated arbitrarily.= */ #define OPTIMIZER_HIDE_VAR(var) \ @@ -263,4 +219,4 @@ static __always_inline void __write_once_size(volatile = void *p, void *res, int s =20 #endif /* __ASSEMBLY__ */ =20 -#endif /* _TOOLS_LINUX_COMPILER_H */ +#endif /* __LINUX_COMPILER_H */ diff --git a/tools/include/linux/compiler_types.h b/tools/include/linux/com= piler_types.h index b3adbf5682f0..31e9f7d2a466 100644 --- a/tools/include/linux/compiler_types.h +++ b/tools/include/linux/compiler_types.h @@ -2,8 +2,6 @@ #ifndef __LINUX_COMPILER_TYPES_H #define __LINUX_COMPILER_TYPES_H =20 -/* Builtins */ - /* * __has_builtin is supported on gcc >=3D 10, clang >=3D 3 and icc >=3D 21. * In the meantime, to support gcc < 10, we implement __has_builtin @@ -13,6 +11,23 @@ #define __has_builtin(x) (0) #endif =20 +/* Indirect macros required for expanded argument pasting, eg. __LINE__. */ +#define ___PASTE(a, b) a##b +#define __PASTE(a, b) ___PASTE(a, b) + +#ifndef __ASSEMBLY__ + +/* + * Skipped when running bindgen due to a libclang issue; + * see https://github.com/rust-lang/rust-bindgen/issues/2244. + */ +#if defined(CONFIG_DEBUG_INFO_BTF) && defined(CONFIG_PAHOLE_HAS_BTF_TAG) &= & \ + __has_attribute(btf_type_tag) && !defined(__BINDGEN__) +# define BTF_TYPE_TAG(value) __attribute__((btf_type_tag(#value))) +#else +# define BTF_TYPE_TAG(value) /* nothing */ +#endif + #include =20 /* Compiler specific macros. */ @@ -20,14 +35,101 @@ #include #endif =20 +#ifndef __compiletime_error +# define __compiletime_error(message) +#endif + +#ifndef __noreturn +# define __noreturn __attribute__((__noreturn__)) +#endif + +#endif /* __ASSEMBLY__ */ + +/* + * When the size of an allocated object is needed, use the best available + * mechanism to find it. (For cases where sizeof() cannot be used.) + * + * Optional: only supported since gcc >=3D 12 + * + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Object-Size-Checking.html + * clang: https://clang.llvm.org/docs/LanguageExtensions.html#evaluating-o= bject-size + */ +#if __has_builtin(__builtin_dynamic_object_size) +#define __struct_size(p) __builtin_dynamic_object_size(p, 0) +#define __member_size(p) __builtin_dynamic_object_size(p, 1) +#else +#define __struct_size(p) __builtin_object_size(p, 0) +#define __member_size(p) __builtin_object_size(p, 1) +#endif + +/* + * Determine if an attribute has been applied to a variable. + * Using __annotated needs to check for __annotated being available, + * or negative tests may fail when annotation cannot be checked. For + * example, see the definition of __is_cstr(). + */ +#if __has_builtin(__builtin_has_attribute) +#define __annotated(var, attr) __builtin_has_attribute(var, attr) +#endif + +/* + * Optional: only supported since gcc >=3D 15, clang >=3D 19 + * + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_00= 5f_005fbuiltin_005fcounted_005fby_005fref + * clang: https://clang.llvm.org/docs/LanguageExtensions.html#builtin-coun= ted-by-ref + */ +#if __has_builtin(__builtin_counted_by_ref) && \ + !defined(CONFIG_CC_HAS_BROKEN_COUNTED_BY_REF) +#define __flex_counter(FAM) __builtin_counted_by_ref(FAM) +#else +#define __flex_counter(FAM) ((void *)NULL) +#endif + +/* + * Some versions of gcc do not mark 'asm goto' volatile: + * + * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103979 + * + * We do it here by hand, because it doesn't hurt. + */ #ifndef asm_goto_output -#define asm_goto_output(x...) asm goto(x) +#define asm_goto_output(x...) asm volatile goto(x) +#endif + +/* + * Clang has trouble with constraints with multiple + * alternative behaviors ("g" , "rm" and "=3Drm"). + */ +#ifndef ASM_INPUT_G + #define ASM_INPUT_G "g" + #define ASM_INPUT_RM "rm" + #define ASM_OUTPUT_RM "=3Drm" +#endif + +#ifdef CONFIG_CC_HAS_ASM_INLINE +#define asm_inline asm __inline +#else +#define asm_inline asm +#endif + +#ifndef __ASSEMBLY__ +/* + * Use __typeof_unqual__() when available. + */ +#if (defined(CC_HAS_TYPEOF_UNQUAL) && CC_HAS_TYPEOF_UNQUAL) || defined(__C= HECKER__) +# define USE_TYPEOF_UNQUAL 1 +#endif + +/* Are two types/vars the same type (ignoring qualifiers)? */ +#ifndef __same_type +# define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(= b)) #endif =20 /* * __unqual_scalar_typeof(x) - Declare an unqualified scalar type, leaving * non-scalar types unchanged. */ +#ifndef USE_TYPEOF_UNQUAL /* * Prefer C11 _Generic for better compile-times and simpler code. Note: 'c= har' * is not type-compatible with 'signed char', and we define a separate cas= e. @@ -45,5 +147,103 @@ __scalar_type_to_expr_cases(long), \ __scalar_type_to_expr_cases(long long), \ default: (x))) +#else +#define __unqual_scalar_typeof(x) __typeof_unqual__(x) +#endif + +#endif /* !__ASSEMBLY__ */ + +/* + * __signed_scalar_typeof(x) - Declare a signed scalar type, leaving + * non-scalar types unchanged. + */ + +#define __scalar_type_to_signed_cases(type) \ + unsigned type: (signed type)0, \ + signed type: (signed type)0 + +#define __signed_scalar_typeof(x) typeof( \ + _Generic((x), \ + char: (signed char)0, \ + __scalar_type_to_signed_cases(char), \ + __scalar_type_to_signed_cases(short), \ + __scalar_type_to_signed_cases(int), \ + __scalar_type_to_signed_cases(long), \ + __scalar_type_to_signed_cases(long long), \ + default: (x))) + +/* Is this type a native word size -- useful for atomic operations */ +#define __native_word(t) \ + (sizeof(t) =3D=3D sizeof(char) || sizeof(t) =3D=3D sizeof(short) || \ + sizeof(t) =3D=3D sizeof(int) || sizeof(t) =3D=3D sizeof(long)) + +#ifdef __OPTIMIZE__ +/* + * #ifdef __OPTIMIZE__ is only a good approximation; for instance "make + * CFLAGS_foo.o=3D-Og" defines __OPTIMIZE__, does not elide the conditiona= l code + * and can break compilation with wrong error message(s). Combine with + * -U__OPTIMIZE__ when needed. + */ +# define __compiletime_assert(condition, msg, prefix, suffix) \ + do { \ + /* \ + * __noreturn is needed to give the compiler enough \ + * information to avoid certain possibly-uninitialized \ + * warnings (regardless of the build failing). \ + */ \ + __noreturn extern void prefix ## suffix(void) \ + __compiletime_error(msg); \ + if (!(condition)) \ + prefix ## suffix(); \ + } while (0) +#else +# define __compiletime_assert(condition, msg, prefix, suffix) ((void)(cond= ition)) +#endif + +#define _compiletime_assert(condition, msg, prefix, suffix) \ + __compiletime_assert(condition, msg, prefix, suffix) + +/** + * compiletime_assert - break build and emit msg if condition is false + * @condition: a compile-time constant condition to check + * @msg: a message to emit if condition is false + * + * In tradition of POSIX assert, this macro will break the build if the + * supplied condition is *false*, emitting the supplied error message if t= he + * compiler has support to do so. + */ +#define compiletime_assert(condition, msg) \ + _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__) + +#define compiletime_assert_atomic_type(t) \ + compiletime_assert(__native_word(t), \ + "Need native word sized stores/loads for atomicity.") + +/* Helpers for emitting diagnostics in pragmas. */ +#ifndef __diag +#define __diag(string) +#endif + +#ifndef __diag_GCC +#define __diag_GCC(version, severity, string) +#endif + +#ifndef __diag_clang +#define __diag_clang(version, severity, string) +#endif + +#define __diag_push() __diag(push) +#define __diag_pop() __diag(pop) + +#define __diag_ignore(compiler, version, option, comment) \ + __diag_ ## compiler(version, ignore, option) +#define __diag_warn(compiler, version, option, comment) \ + __diag_ ## compiler(version, warn, option) +#define __diag_error(compiler, version, option, comment) \ + __diag_ ## compiler(version, error, option) + +#ifndef __diag_ignore_all +#define __diag_ignore_all(option, comment) +#endif =20 #endif /* __LINUX_COMPILER_TYPES_H */ --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244324; b=jxcdH+4foUWp7LfIK2Eg9irSIdVwKnpIzSIKx+JvMFhwLZEoW0K9KW2cjucYid7924Ly aJ3+tD6KGfmsk2ky2kpodfAFvQ7lxPxm1d/osqGHtEYPHV3iOJwM2AWaHhEW6cp5m8tNi UHxJu+wIyalN6Y/7+8u8GgqesvfWJC4XhydeUnAHxNS5ncF71jnInORGU4pEPjzv5Zmpk I0cFoc9iOaympBi7rtL2fdH0g0eb2hhXDX4MIg71hcphc4K2tuo6SrHg0H27mM3l/7zDL Y6doDpUjZVWTk9rI8TC8RgHCbEu7K6zudEqLG9S8tV78lCi092tbtzhG8/zNwHlrS0A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244324; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=fWlK51DLy3cqxmkOhH3k8ZibfBCwn001syXfCmY1OgA=; b=ME34ckvILQa9MbY9eWLxn6PlAZh3rfuQP+k7pMSi1uM+EikwbIhZkoUKzvMdKgg2Kodl +3C39OPRm0VRvpG0ZUi5IEDX9pNaEvco5eas7xAY0Os+Oo0oP8aVl0kVgyBsOl3/7fVr6 uK6CpEgJ4F5ZdA3cVL3OKQCjx2UDI197FBAj8QpazCbFz09l44R9u+wheVGb9WRESGmly 4jDuT3f/3VMNn0WEOP+41SVKzT/FuSmj/7tu45PbJgxHzQOJpcH4Ozd+BctOZnQW/yiXZ KEL4mjeyMBMLepn+WTrJ4Dh+FA0EYpft4iJjEGuRQsf/xxG9zEs/TRtE86vPj0LI+SA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=IR6RGK6J; arc=none smtp.remote-ip=209.85.216.72 Received: from mail-pj1-f72.google.com (mail-pj1-f72.google.com [209.85.216.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 76297606A337 for ; Sat, 21 Nov 2026 06:58:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 76297606A337 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=IR6RGK6J Received: by mail-pj1-f72.google.com with SMTP id 98e67ed59e1d1-3b41b099809so1645408a91.1 for ; Fri, 20 Nov 2026 22:58:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244323; x=1795849123; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fWlK51DLy3cqxmkOhH3k8ZibfBCwn001syXfCmY1OgA=; b=IR6RGK6Jt4t9/Kb2ACuHUb5Z7PdG1K6ZeD/jWkwuS26Z1YyA+8Q23rcaKOH1DhuaYK +aZQDRRgoZx10RvyLo7ZpXGL3c+sVuUt/YjZiz0r7Cbns3LknXYdD+KWkInBnCFxGPIV +ChsT/+/syV1jZsX0+PNK9gkwonf74q2wrEzU5zjbNZSdiH+KHqp5cn8yWnhgoAkDbPx twvtYg8XJqgO+EXne5ukOk0tGIOGgtccdLguxWTjhYQ7adsHHouDTyrT1Y9oU8+KiERO iqhmByn0UxyH/WiQ/uP7EXYucwwvod0nZ6j3NyRQOXFj7DJxnnbJIrDrxvZ5ipnGh8dG GbGQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244323; x=1795849123; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fWlK51DLy3cqxmkOhH3k8ZibfBCwn001syXfCmY1OgA=; b=h/CvdlHj3vfRXa6PILRzffAMwfLFd9nhdlXIPMBnSuR7uefZA4haQBz/qRIhx8e7UT qGCOBlWjVHZPrGpoQ6DzNkXEokEo41PzCnXqne/biyIiicZgfwiPM9Q15q+c6Tgl/T4S Xevs0ZSvcnR0CIM97PHlnRgPShK5ILM9PSG7XwrEQ7H1IfsupxUF1g916idbtdgX4nsM ah3SkMjuWNkq7Hhg9URWf7SnGDM2rORH3mwvul+AnGu5UOPJQ28Z+HOguuMOPNl/zo+K ZwkB8/jKS/brwKrud6JfvcZeShmk0m55MOkn4W7nEwUt5Dbkfo72mJsFVQhpO7hUnwg7 WHAg== X-Forwarded-Encrypted: i=1; AL+hErGfDQcwwN+Li0FQgIsEDziCl2G4n0lSxy5pelApn+/kHUVTXfoKx3Rdl2dJYpzl5fvpevMXJeyRS4zi0yQ=@vger.kernel.org X-Gm-Message-State: APuU4Z/Vig8KIgZQfZQ0yQ3Iq/Ppz4o0bBLOeU4OsTpBeIxN7tPhWRS1 upuqWk4f1AS1b/2GQ4kisBWS1ZZlz8HeAIx3jDGWkKP82h0n4Fhr31uKKv+Ii9cJvlUSHhFJ0Xa cUuM9Jz/Png== X-Received: from pgbdo9.prod.google.com ([2002:a05:6a02:e89:b0:cdb:5b3d:11bd]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90a:c10e:b0:3b3:b0f4:1bf6 with SMTP id 98e67ed59e1d1-3b41c8ff7b3mr5225608a91.22.1795244322558; Fri, 20 Nov 2026 22:58:42 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:52 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-4-irogers@google.com> Subject: [PATCH v15 03/23] tools headers: Synchronize linux/compiler-context-analysis.h with the kernel sources From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the no-op stub tools/include/linux/compiler-context-analysis.h with the full kernel implementation from include/linux/compiler-context-analysis.h and add it to tools/perf/check-headers.sh so future changes are tracked. Signed-off-by: Ian Rogers --- .../include/linux/compiler-context-analysis.h | 518 ++++++++++++++++-- tools/perf/check-headers.sh | 1 + 2 files changed, 480 insertions(+), 39 deletions(-) diff --git a/tools/include/linux/compiler-context-analysis.h b/tools/includ= e/linux/compiler-context-analysis.h index 13a9115e9e58..024e303188ad 100644 --- a/tools/include/linux/compiler-context-analysis.h +++ b/tools/include/linux/compiler-context-analysis.h @@ -1,42 +1,482 @@ /* SPDX-License-Identifier: GPL-2.0 */ -#ifndef _TOOLS_LINUX_COMPILER_CONTEXT_ANALYSIS_H -#define _TOOLS_LINUX_COMPILER_CONTEXT_ANALYSIS_H - /* * Macros and attributes for compiler-based static context analysis. - * No-op stubs for tools. - */ - -#define __guarded_by(...) -#define __pt_guarded_by(...) - -#define context_lock_struct(name, ...) struct __VA_ARGS__ name - -#define __no_context_analysis -#define __context_unsafe(comment) -#define context_unsafe(...) ({ __VA_ARGS__; }) -#define context_unsafe_alias(p) -#define disable_context_analysis() -#define enable_context_analysis() - -#define __must_hold(...) -#define __must_not_hold(...) -#define __acquires(...) -#define __cond_acquires(ret, x) -#define __releases(...) -#define __acquire(x) (void)0 -#define __release(x) (void)0 - -#define __must_hold_shared(...) -#define __acquires_shared(...) -#define __cond_acquires_shared(ret, x) -#define __releases_shared(...) -#define __acquire_shared(x) (void)0 -#define __release_shared(x) (void)0 - -#define __acquire_ret(call, expr) (call) -#define __acquire_shared_ret(call, expr) (call) -#define __acquires_ret -#define __acquires_shared_ret - -#endif /* _TOOLS_LINUX_COMPILER_CONTEXT_ANALYSIS_H */ + */ + +#ifndef _LINUX_COMPILER_CONTEXT_ANALYSIS_H +#define _LINUX_COMPILER_CONTEXT_ANALYSIS_H + +#if defined(WARN_CONTEXT_ANALYSIS) && !defined(__CHECKER__) && !defined(__= GENKSYMS__) + +/* + * These attributes define new context lock (Clang: capability) types. + * Internal only. + */ +# define __ctx_lock_type(name) __attribute__((capability(#name))) +# define __reentrant_ctx_lock __attribute__((reentrant_capability)) +# define __acquires_ctx_lock(...) __attribute__((acquire_capability(__VA_= ARGS__))) +# define __acquires_shared_ctx_lock(...) __attribute__((acquire_shared_cap= ability(__VA_ARGS__))) +# define __try_acquires_ctx_lock(ret, var) __attribute__((try_acquire_capa= bility(ret, var))) +# define __try_acquires_shared_ctx_lock(ret, var) __attribute__((try_acqui= re_shared_capability(ret, var))) +# define __releases_ctx_lock(...) __attribute__((release_capability(__VA_= ARGS__))) +# define __releases_shared_ctx_lock(...) __attribute__((release_shared_cap= ability(__VA_ARGS__))) +# define __returns_ctx_lock(var) __attribute__((lock_returned(var))) + +/* + * The below are used to annotate code being checked. Internal only. + */ +# define __excludes_ctx_lock(...) __attribute__((locks_excluded(__VA_ARGS= __))) +# define __requires_ctx_lock(...) __attribute__((requires_capability(__VA= _ARGS__))) +# define __requires_shared_ctx_lock(...) __attribute__((requires_shared_ca= pability(__VA_ARGS__))) + +/* + * The "assert_capability" attribute is a bit confusingly named. It does n= ot + * generate a check. Instead, it tells the analysis to *assume* the capabi= lity + * is held. This is used for augmenting runtime assertions, that can then = help + * with patterns beyond the compiler's static reasoning abilities. + */ +# define __assumes_ctx_lock(...) __attribute__((assert_capability(__VA_AR= GS__))) +# define __assumes_shared_ctx_lock(...) __attribute__((assert_shared_capab= ility(__VA_ARGS__))) + +/** + * __guarded_by() - struct member and globals attribute, declares variable + * only accessible within active context + * @...: context lock instance pointer(s) + * + * Declares that the struct member or global variable is only accessible w= ithin + * the context entered by the given context lock(s). Read operations on th= e data + * require shared access to at least one of the context locks, while write + * operations require exclusive access to all listed context locks. + * + * .. code-block:: c + * + * struct some_state { + * spinlock_t lock; + * long counter __guarded_by(&lock); + * }; + * + * struct some_state { + * spinlock_t lock1, lock2; + * long counter __guarded_by(&lock1, &lock2); + * }; + */ +# define __guarded_by(...) __attribute__((guarded_by(__VA_ARGS__))) + +/** + * __pt_guarded_by() - struct member and globals attribute, declares point= ed-to + * data only accessible within active context + * @...: context lock instance pointer(s) + * + * Declares that the data pointed to by the struct member pointer or global + * pointer is only accessible within the context entered by the given cont= ext + * lock(s). Read operations on the data require shared access to at least = one + * of the context locks, while write operations require exclusive access t= o all + * listed context locks. + * + * .. code-block:: c + * + * struct some_state { + * spinlock_t lock; + * long *counter __pt_guarded_by(&lock); + * }; + * + * struct some_state { + * spinlock_t lock1, lock2; + * long *counter __pt_guarded_by(&lock1, &lock2); + * }; + */ +# define __pt_guarded_by(...) __attribute__((pt_guarded_by(__VA_ARGS__))) + +/** + * context_lock_struct() - declare or define a context lock struct + * @name: struct name + * + * Helper to declare or define a struct type that is also a context lock. + * + * .. code-block:: c + * + * context_lock_struct(my_handle) { + * int foo; + * long bar; + * }; + * + * struct some_state { + * ... + * }; + * // ... declared elsewhere ... + * context_lock_struct(some_state); + * + * Note: The implementation defines several helper functions that can acqu= ire + * and release the context lock. + */ +# define context_lock_struct(name, ...) \ + struct __ctx_lock_type(name) __VA_ARGS__ name; \ + static __always_inline void __acquire_ctx_lock(const struct name *var) = \ + __attribute__((overloadable)) __no_context_analysis __acquires_ctx_lock(= var) { } \ + static __always_inline void __acquire_shared_ctx_lock(const struct name *= var) \ + __attribute__((overloadable)) __no_context_analysis __acquires_shared_ct= x_lock(var) { } \ + static __always_inline bool __try_acquire_ctx_lock(const struct name *var= , bool ret) \ + __attribute__((overloadable)) __no_context_analysis __try_acquires_ctx_l= ock(1, var) \ + { return ret; } \ + static __always_inline bool __try_acquire_shared_ctx_lock(const struct na= me *var, bool ret) \ + __attribute__((overloadable)) __no_context_analysis __try_acquires_share= d_ctx_lock(1, var) \ + { return ret; } \ + static __always_inline void __release_ctx_lock(const struct name *var) = \ + __attribute__((overloadable)) __no_context_analysis __releases_ctx_lock(= var) { } \ + static __always_inline void __release_shared_ctx_lock(const struct name *= var) \ + __attribute__((overloadable)) __no_context_analysis __releases_shared_ct= x_lock(var) { } \ + static __always_inline void __assume_ctx_lock(const struct name *var) \ + __attribute__((overloadable)) __assumes_ctx_lock(var) { } \ + static __always_inline void __assume_shared_ctx_lock(const struct name *v= ar) \ + __attribute__((overloadable)) __assumes_shared_ctx_lock(var) { } \ + struct name + +/** + * disable_context_analysis() - disables context analysis + * + * Disables context analysis. Must be paired with a later + * enable_context_analysis(). + */ +# define disable_context_analysis() \ + __diag_push(); \ + __diag_ignore_all("-Wunknown-warning-option", "") \ + __diag_ignore_all("-Wthread-safety", "") \ + __diag_ignore_all("-Wthread-safety-pointer", "") + +/** + * enable_context_analysis() - re-enables context analysis + * + * Re-enables context analysis. Must be paired with a prior + * disable_context_analysis(). + */ +# define enable_context_analysis() __diag_pop() + +/** + * __no_context_analysis - function attribute, disables context analysis + * + * Function attribute denoting that context analysis is disabled for the + * whole function. Prefer use of `context_unsafe()` where possible. + */ +# define __no_context_analysis __attribute__((no_thread_safety_analysis)) + +#else /* !WARN_CONTEXT_ANALYSIS */ + +# define __ctx_lock_type(name) +# define __reentrant_ctx_lock +# define __acquires_ctx_lock(...) +# define __acquires_shared_ctx_lock(...) +# define __try_acquires_ctx_lock(ret, var) +# define __try_acquires_shared_ctx_lock(ret, var) +# define __releases_ctx_lock(...) +# define __releases_shared_ctx_lock(...) +# define __assumes_ctx_lock(...) +# define __assumes_shared_ctx_lock(...) +# define __returns_ctx_lock(var) +# define __guarded_by(...) +# define __pt_guarded_by(...) +# define __excludes_ctx_lock(...) +# define __requires_ctx_lock(...) +# define __requires_shared_ctx_lock(...) +# define __acquire_ctx_lock(var) do { } while (0) +# define __acquire_shared_ctx_lock(var) do { } while (0) +# define __try_acquire_ctx_lock(var, ret) (ret) +# define __try_acquire_shared_ctx_lock(var, ret) (ret) +# define __release_ctx_lock(var) do { } while (0) +# define __release_shared_ctx_lock(var) do { } while (0) +# define __assume_ctx_lock(var) do { (void)(var); } while (0) +# define __assume_shared_ctx_lock(var) do { (void)(var); } while (0) +# define context_lock_struct(name, ...) struct __VA_ARGS__ name +# define disable_context_analysis() +# define enable_context_analysis() +# define __no_context_analysis + +#endif /* WARN_CONTEXT_ANALYSIS */ + +/** + * context_unsafe() - disable context checking for contained code + * + * Disables context checking for contained statements or expression. + * + * .. code-block:: c + * + * struct some_data { + * spinlock_t lock; + * int counter __guarded_by(&lock); + * }; + * + * int foo(struct some_data *d) + * { + * // ... + * // other code that is still checked ... + * // ... + * return context_unsafe(d->counter); + * } + */ +#define context_unsafe(...) \ +({ \ + disable_context_analysis(); \ + __VA_ARGS__; \ + enable_context_analysis() \ +}) + +/** + * __context_unsafe() - function attribute, disable context checking + * @comment: comment explaining why opt-out is safe + * + * Function attribute denoting that context analysis is disabled for the + * whole function. Forces adding an inline comment as argument. + */ +#define __context_unsafe(comment) __no_context_analysis + +/** + * context_unsafe_alias() - helper to insert a context lock "alias barrier" + * @p: pointer aliasing a context lock or object containing context locks + * + * No-op function that acts as a "context lock alias barrier", where the + * analysis rightfully detects that we're switching aliases, but the switc= h is + * considered safe but beyond the analysis reasoning abilities. + * + * This should be inserted before the first use of such an alias. + * + * Implementation Note: The compiler ignores aliases that may be reassigne= d but + * their value cannot be determined (e.g. when passing a non-const pointer= to an + * alias as a function argument). + */ +#define context_unsafe_alias(p) _context_unsafe_alias((void **)&(p)) +static inline void _context_unsafe_alias(void **p) { (void)p; } + +/** + * token_context_lock() - declare an abstract global context lock instance + * @name: token context lock name + * + * Helper that declares an abstract global context lock instance @name, bu= t not + * backed by a real data structure (linker error if accidentally reference= d). + * The type name is `__ctx_lock_@name`. + */ +#define token_context_lock(name, ...) \ + context_lock_struct(__ctx_lock_##name, ##__VA_ARGS__) {}; \ + extern const struct __ctx_lock_##name *name + +/** + * token_context_lock_instance() - declare another instance of a global co= ntext lock + * @ctx: token context lock previously declared with token_context_lock() + * @name: name of additional global context lock instance + * + * Helper that declares an additional instance @name of the same token con= text + * lock class @ctx. This is helpful where multiple related token contexts = are + * declared, to allow using the same underlying type (`__ctx_lock_@ctx`) as + * function arguments. + */ +#define token_context_lock_instance(ctx, name) \ + extern const struct __ctx_lock_##ctx *name + +/* + * Common keywords for static context analysis. + */ + +/** + * __must_hold() - function attribute, caller must hold exclusive context = lock + * + * Function attribute declaring that the caller must hold the given context + * lock instance(s) exclusively. + */ +#define __must_hold(...) __requires_ctx_lock(__VA_ARGS__) + +/** + * __must_not_hold() - function attribute, caller must not hold context lo= ck + * + * Function attribute declaring that the caller must not hold the given co= ntext + * lock instance(s). + */ +#define __must_not_hold(...) __excludes_ctx_lock(__VA_ARGS__) + +/** + * __acquires() - function attribute, function acquires context lock exclu= sively + * + * Function attribute declaring that the function acquires the given conte= xt + * lock instance(s) exclusively, but does not release them. + */ +#define __acquires(...) __acquires_ctx_lock(__VA_ARGS__) + +/* + * Clang's analysis does not care precisely about the value, only that it = is + * either zero or non-zero. So the __cond_acquires() interface might be + * misleading if we say that @ret is the value returned if acquired. Inste= ad, + * provide symbolic variants which we translate. + */ +#define __cond_acquires_impl_true(x, ...) __try_acquires##__VA_ARGS__#= #_ctx_lock(1, x) +#define __cond_acquires_impl_false(x, ...) __try_acquires##__VA_ARGS__#= #_ctx_lock(0, x) +#define __cond_acquires_impl_nonzero(x, ...) __try_acquires##__VA_ARGS__#= #_ctx_lock(1, x) +#define __cond_acquires_impl_0(x, ...) __try_acquires##__VA_ARGS__#= #_ctx_lock(0, x) +#define __cond_acquires_impl_nonnull(x, ...) __try_acquires##__VA_ARGS__#= #_ctx_lock(1, x) +#define __cond_acquires_impl_NULL(x, ...) __try_acquires##__VA_ARGS__#= #_ctx_lock(0, x) + +/** + * __cond_acquires() - function attribute, function conditionally + * acquires a context lock exclusively + * @ret: abstract value returned by function if context lock acquired + * @x: context lock instance pointer + * + * Function attribute declaring that the function conditionally acquires t= he + * given context lock instance @x exclusively, but does not release it. The + * function return value @ret denotes when the context lock is acquired. + * + * @ret may be one of: true, false, nonzero, 0, nonnull, NULL. + */ +#define __cond_acquires(ret, x) __cond_acquires_impl_##ret(x) + +/** + * __releases() - function attribute, function releases a context lock exc= lusively + * + * Function attribute declaring that the function releases the given conte= xt + * lock instance(s) exclusively. The associated context(s) must be active = on + * entry. + */ +#define __releases(...) __releases_ctx_lock(__VA_ARGS__) + +/* + * Clang's analysis does not care precisely about the value, only that it = is + * either zero or non-zero. So the __cond_acquires() interface might be + * misleading if we say that @ret is the value returned if acquired. Inste= ad, + * provide symbolic variants which we translate. + */ +#define __cond_acquires_impl_not_true(x, ...) __try_acquires##__VA_ARG= S__##_ctx_lock(0, x) +#define __cond_acquires_impl_not_false(x, ...) __try_acquires##__VA_ARG= S__##_ctx_lock(1, x) +#define __cond_acquires_impl_not_nonzero(x, ...) __try_acquires##__VA_ARG= S__##_ctx_lock(0, x) +#define __cond_acquires_impl_not_0(x, ...) __try_acquires##__VA_ARG= S__##_ctx_lock(1, x) +#define __cond_acquires_impl_not_nonnull(x, ...) __try_acquires##__VA_ARG= S__##_ctx_lock(0, x) +#define __cond_acquires_impl_not_NULL(x, ...) __try_acquires##__VA_ARG= S__##_ctx_lock(1, x) + +/** + * __cond_releases() - function attribute, function conditionally + * releases a context lock exclusively + * @ret: abstract value returned by function if context lock releases + * @x: context lock instance pointer + * + * Function attribute declaring that the function conditionally releases t= he + * given context lock instance @x exclusively. The associated context(s) m= ust + * be active on entry. The function return value @ret denotes when the con= text + * lock is released. + * + * @ret may be one of: true, false, nonzero, 0, nonnull, NULL. + * + * NOTE: clang does not have a native attribute for this; instead implement + * it as an unconditional release and a conditional acquire for the + * inverted condition -- which is semantically equivalent. + */ +#define __cond_releases(ret, x) __releases(x) __cond_acquires_impl_not_##r= et(x) + +/** + * __acquire() - function to acquire context lock exclusively + * @x: context lock instance pointer + * + * No-op function that acquires the given context lock instance @x exclusi= vely. + */ +#define __acquire(x) __acquire_ctx_lock(x) + +/** + * __release() - function to release context lock exclusively + * @x: context lock instance pointer + * + * No-op function that releases the given context lock instance @x. + */ +#define __release(x) __release_ctx_lock(x) + +/** + * __must_hold_shared() - function attribute, caller must hold shared cont= ext lock + * + * Function attribute declaring that the caller must hold the given context + * lock instance(s) with shared access. + */ +#define __must_hold_shared(...) __requires_shared_ctx_lock(__VA_ARGS__) + +/** + * __acquires_shared() - function attribute, function acquires context loc= k shared + * + * Function attribute declaring that the function acquires the given + * context lock instance(s) with shared access, but does not release them. + */ +#define __acquires_shared(...) __acquires_shared_ctx_lock(__VA_ARGS__) + +/** + * __cond_acquires_shared() - function attribute, function conditionally + * acquires a context lock shared + * @ret: abstract value returned by function if context lock acquired + * @x: context lock instance pointer + * + * Function attribute declaring that the function conditionally acquires t= he + * given context lock instance @x with shared access, but does not release= it. + * The function return value @ret denotes when the context lock is acquire= d. + * + * @ret may be one of: true, false, nonzero, 0, nonnull, NULL. + */ +#define __cond_acquires_shared(ret, x) __cond_acquires_impl_##ret(x, _shar= ed) + +/** + * __releases_shared() - function attribute, function releases a + * context lock shared + * + * Function attribute declaring that the function releases the given conte= xt + * lock instance(s) with shared access. The associated context(s) must be + * active on entry. + */ +#define __releases_shared(...) __releases_shared_ctx_lock(__VA_ARGS__) + +/** + * __acquire_shared() - function to acquire context lock shared + * @x: context lock instance pointer + * + * No-op function that acquires the given context lock instance @x with sh= ared + * access. + */ +#define __acquire_shared(x) __acquire_shared_ctx_lock(x) + +/** + * __release_shared() - function to release context lock shared + * @x: context lock instance pointer + * + * No-op function that releases the given context lock instance @x with sh= ared + * access. + */ +#define __release_shared(x) __release_shared_ctx_lock(x) + +/** + * __acquire_ret() - helper to acquire context lock of return value + * @call: call expression + * @ret_expr: acquire expression that uses __ret + */ +#define __acquire_ret(call, ret_expr) \ + ({ \ + __auto_type __ret =3D call; \ + __acquire(ret_expr); \ + __ret; \ + }) + +/** + * __acquire_shared_ret() - helper to acquire context lock shared of retur= n value + * @call: call expression + * @ret_expr: acquire shared expression that uses __ret + */ +#define __acquire_shared_ret(call, ret_expr) \ + ({ \ + __auto_type __ret =3D call; \ + __acquire_shared(ret_expr); \ + __ret; \ + }) + +/* + * Attributes to mark functions returning acquired context locks. + * + * This is purely cosmetic to help readability, and should be used with the + * above macros as follows: + * + * struct foo { spinlock_t lock; ... }; + * ... + * #define myfunc(...) __acquire_ret(_myfunc(__VA_ARGS__), &__ret->lock) + * struct foo *_myfunc(int bar) __acquires_ret; + * ... + */ +#define __acquires_ret __no_context_analysis +#define __acquires_shared_ret __no_context_analysis + +#endif /* _LINUX_COMPILER_CONTEXT_ANALYSIS_H */ diff --git a/tools/perf/check-headers.sh b/tools/perf/check-headers.sh index 531c0e0e84df..c6b2ecadf9b1 100755 --- a/tools/perf/check-headers.sh +++ b/tools/perf/check-headers.sh @@ -23,6 +23,7 @@ declare -a FILES=3D( "include/linux/bits.h" "include/vdso/bits.h" "include/linux/cfi_types.h" + "include/linux/compiler-context-analysis.h" "include/linux/const.h" "include/vdso/const.h" "include/vdso/unaligned.h" --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244330; b=LnYxt6C2JSbBqITEV355HFISagDPpveMyZJ+wTYX8Db0d9Poq1E01tRXlf/sgOGnTWLt EaMOxcXFQ1lxuAtWar+Evhg3VkWpX5Kyq40jFuUltKe1ryLMfIS9BiNcU3m24gZSD5VfK j8Oyods9eO5nT2pJ7ME8CIqqHGayaK1etapBmZ/ntBFBp6JR8YJISQ0/JLBneGQD6edV2 WwhlzZ6oO5K8DETPIcfDbk4mdTVdOpZMQfkPGUSo3PK8J3NXdXxyBf3sJ4626lRKO01EJ lKBoTwBfQOH49rEG3VFjKazz/m/UPWoW89ASMNgQ+bi/puIkgUUDtLyVEozdLlmNUcw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244330; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=6+Vb+6Y6JW1JNAMpTGhHnBMw9ARyrY+gi54aWY782EE=; b=BHA+EH2uq+w1a76I4yrijGX8KQFiK2rlo7PZyZWe51eag3nu4tD8vMdq6eZyiG+QWo49 EJ+x+iMh3vnjRzvzK/Yc2PiqwA77LoaIbJtoV44PjQu1dx/rdarNWuj1AsHSDoSy0A9mo NY3Xsvfh1cV1fQo/xffXuCyGhWiqj2ZiPIgjtvemwbwdomTy6+rx7Kgrz21RjhD5kkjhv FGBCb2oNZnUEkz2dufbAQpNBNVCiZbyExHlQkiFKVfRxA7fnahBPgldORY7Wi79BBvBKB +Kcp0n4wQgjSkGjGPkojWzyDmgN4wKa9DzfBWyLDwBoSCSHIoNx+ChXQyPELvTvi9EA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="J/2lp90C"; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8BCE7606A331 for ; Sat, 21 Nov 2026 06:58:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8BCE7606A331 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=J/2lp90C Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-38355b03cb8so1604442eec.0 for ; Fri, 20 Nov 2026 22:58:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244329; x=1795849129; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=6+Vb+6Y6JW1JNAMpTGhHnBMw9ARyrY+gi54aWY782EE=; b=J/2lp90CAVyn67FglHZAT19sIf2NPYcs3pmTg3v2Qt5xvPGwWLCQxBwL6dBuMOlyF4 EjLI94oX1tGwEr3xzII/oxwLMJ1pA881OtLhEXdbp3MM3f4DKiT0lEz6ITYKnF7rJkxp X9GRxH21ZIoJipTH2Z/Ypd4bPsJGxFXc++pcPrMXZnlr0NT/6HiZpow1nFn6gIpHGNep BGuc1c616AWH3WdjCTdOGMSzf2Vb1MbGbKjX137N23dOf2wFkIXdHz3I1UsLjtp/3v37 LjIA2YsHqxokrYl6c+FJDAvrhB0XeG7kHh0j7+9MTVvalABP83AFMivGn1J5+0YLNA+K iXvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244329; x=1795849129; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=6+Vb+6Y6JW1JNAMpTGhHnBMw9ARyrY+gi54aWY782EE=; b=izvuj1YUE7apsfeMEo4qe1qTyj0BHqIm8mHWL4jNn6BQXOcEpf9/W4tQIzHSUsHyZ1 fJhUcK4WPKFVUgXUL+5jfQw6LMozI4+laKMWPJP4X61XNPVD+EVlmDamEDQlPE86zpxH HCmZPzrqDwz4HBR2I4dqPTCRGmgXGLwxfSxdnXMQDGft990EL7WqysZSZZLyFqmf1ymC YoleDYPNdrguzsP4BUPoky3NITk9HRl7ZejdCWYB6FQLIl3YSAoN53yWeNNUOeB4l4kz YdD/faZi/z52ckdyXzEAy+3nYJA6iyJJFue3ChWUj6Jp1kdumkuy3Ph1ZTIShFmvQ+mg WXiQ== X-Forwarded-Encrypted: i=1; AL+hErH1X+R2ZJUzO1hIMijlglR31FXnpLyRw4fn55cY7MqH8NgIMfGcxSnbmaDzr/bfLaz0+tr13Az+Q0doNWM=@vger.kernel.org X-Gm-Message-State: APuU4Z9vgIazL61eeZknsPJG7ZP88gesrY8poa7BLzilqBq6uFZoOZjC 0kf9bXuXgXpKs7hMuaYE478+ibNXsWmlazkf0A2R/kehfi3O7ZIQnXD36h94kvcIAsTOTgH86Mi B/wk3HP2uXQ== X-Received: from dyee6.prod.google.com ([2002:a05:693c:6286:b0:386:6850:930a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:488d:b0:366:83dd:46f7 with SMTP id 5a478bee46e88-3856f85220amr5641320eec.17.1795244328232; Fri, 20 Nov 2026 22:58:48 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:53 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-5-irogers@google.com> Subject: [PATCH v15 04/23] perf mutex: Use kernel style compiler-context-analysis.h annotations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace the custom lock safety macros in tools/perf with the kernel's compiler-context-analysis.h annotations (context_lock_struct, __guarded_by, __acquires, __acquires_shared, __releases, __releases_shared, __cond_acquires, __must_hold, __must_hold_shared, __must_not_hold, __returns_ctx_lock, __no_context_analysis, and __context_unsafe), and remove the redundant macro definitions from tools/perf/util/mutex.h. Signed-off-by: Ian Rogers --- tools/perf/builtin-sched.c | 16 +++---- tools/perf/builtin-top.c | 4 +- tools/perf/util/annotate.c | 8 ++-- tools/perf/util/annotate.h | 8 ++-- tools/perf/util/comm.c | 4 +- tools/perf/util/dso.c | 30 ++++++------ tools/perf/util/dso.h | 11 +++-- tools/perf/util/dsos.c | 6 +-- tools/perf/util/intel-tpebs.c | 6 +-- tools/perf/util/libbfd.c | 4 +- tools/perf/util/mutex.c | 4 +- tools/perf/util/mutex.h | 90 +++-------------------------------- tools/perf/util/rwsem.c | 8 ++-- tools/perf/util/rwsem.h | 10 ++-- tools/perf/util/thread.c | 8 ++-- tools/perf/util/thread.h | 2 +- 16 files changed, 71 insertions(+), 148 deletions(-) diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index 39571f03eda2..ee74a6aa5ea3 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -828,8 +828,8 @@ static void *thread_func(void *ctx) } =20 static void create_tasks(struct perf_sched *sched) - EXCLUSIVE_LOCK_FUNCTION(sched->start_work_mutex) - EXCLUSIVE_LOCK_FUNCTION(sched->work_done_wait_mutex) + __acquires(&sched->start_work_mutex) + __acquires(&sched->work_done_wait_mutex) { struct task_desc *task; pthread_attr_t attr; @@ -858,8 +858,8 @@ static void create_tasks(struct perf_sched *sched) } =20 static void destroy_tasks(struct perf_sched *sched) - UNLOCK_FUNCTION(sched->start_work_mutex) - UNLOCK_FUNCTION(sched->work_done_wait_mutex) + __releases(&sched->start_work_mutex) + __releases(&sched->work_done_wait_mutex) { struct task_desc *task; unsigned long i; @@ -878,8 +878,8 @@ static void destroy_tasks(struct perf_sched *sched) } =20 static void wait_for_tasks(struct perf_sched *sched) - EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) - EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) + __must_hold(&sched->work_done_wait_mutex) + __must_hold(&sched->start_work_mutex) { u64 cpu_usage_0, cpu_usage_1; struct task_desc *task; @@ -930,8 +930,8 @@ static void wait_for_tasks(struct perf_sched *sched) } =20 static void run_one_test(struct perf_sched *sched) - EXCLUSIVE_LOCKS_REQUIRED(sched->work_done_wait_mutex) - EXCLUSIVE_LOCKS_REQUIRED(sched->start_work_mutex) + __must_hold(&sched->work_done_wait_mutex) + __must_hold(&sched->start_work_mutex) { u64 T0, T1, delta, avg_delta, fluct; =20 diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c index a272e6649077..a79230b81300 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -207,7 +207,7 @@ static void perf_top__record_precise_ip(struct perf_top= *top, struct hist_entry *he, struct perf_sample *sample, u64 ip) - EXCLUSIVE_LOCKS_REQUIRED(he->hists->lock) + __must_hold(&he->hists->lock) { struct annotation *notes; struct symbol *sym =3D he->ms.sym; @@ -741,7 +741,7 @@ static void *display_thread(void *arg) static int hist_iter__top_callback(struct hist_entry_iter *iter, struct addr_location *al, bool single, void *arg) - EXCLUSIVE_LOCKS_REQUIRED(iter->he->hists->lock) + __must_hold(&iter->he->hists->lock) { struct perf_top *top =3D arg; =20 diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index 66d3453e7ffd..e270d16ce177 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -167,7 +167,7 @@ annotated_source__hist_entry(struct annotated_source *s= rc, const struct evsel *e * unannotated symbols do not pay the memory cost of struct annotation. */ static struct rw_semaphore _annotations_lock; -static struct hashmap *annotations GUARDED_BY(_annotations_lock); +static struct hashmap *annotations __guarded_by(&_annotations_lock); static atomic_size_t nr_annotations; =20 static void annotations_lock_init(void) @@ -175,7 +175,7 @@ static void annotations_lock_init(void) init_rwsem(&_annotations_lock); } =20 -static struct rw_semaphore *annotations_lock(void) LOCK_RETURNED(_annotati= ons_lock) +static struct rw_semaphore *annotations_lock(void) __returns_ctx_lock(&_an= notations_lock) { static pthread_once_t annotations_lock_once =3D PTHREAD_ONCE_INIT; =20 @@ -796,7 +796,7 @@ static struct mutex *annotation__get_mutex(const struct= annotation *notes) } =20 void annotation__lock(struct annotation *notes) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { struct mutex *mutex =3D annotation__get_mutex(notes); =20 @@ -805,7 +805,7 @@ void annotation__lock(struct annotation *notes) } =20 void annotation__unlock(struct annotation *notes) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { struct mutex *mutex =3D annotation__get_mutex(notes); =20 diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h index bc2237609d6e..e0fc48ec5612 100644 --- a/tools/perf/util/annotate.h +++ b/tools/perf/util/annotate.h @@ -369,7 +369,7 @@ struct annotated_branch { u64 *br_cntr; }; =20 -struct LOCKABLE annotation { +context_lock_struct(annotation) { struct annotated_source *src; struct annotated_branch *branch; }; @@ -379,9 +379,9 @@ static inline void annotation__init(struct annotation *= notes __maybe_unused) } void annotation__exit(struct annotation *notes); =20 -void annotation__lock(struct annotation *notes) EXCLUSIVE_LOCK_FUNCTION(*n= otes); -void annotation__unlock(struct annotation *notes) UNLOCK_FUNCTION(*notes); -bool annotation__trylock(struct annotation *notes) EXCLUSIVE_TRYLOCK_FUNCT= ION(true, *notes); +void annotation__lock(struct annotation *notes) __acquires(notes); +void annotation__unlock(struct annotation *notes) __releases(notes); +bool annotation__trylock(struct annotation *notes) __cond_acquires(true, n= otes); =20 static inline int annotation__cycles_width(struct annotation *notes) { diff --git a/tools/perf/util/comm.c b/tools/perf/util/comm.c index 9880247a2c33..6343a3c9debe 100644 --- a/tools/perf/util/comm.c +++ b/tools/perf/util/comm.c @@ -24,7 +24,7 @@ static struct comm_strs { static void comm_strs__remove_if_last(struct comm_str *cs); =20 static void comm_strs__init(void) - NO_THREAD_SAFETY_ANALYSIS /* Inherently single threaded due to pthread_on= ce. */ + __context_unsafe(/* Inherently single threaded due to pthread_once. */) { init_rwsem(&_comm_strs.lock); _comm_strs.capacity =3D 16; @@ -120,7 +120,7 @@ static void comm_strs__remove_if_last(struct comm_str *= cs) } =20 static struct comm_str *__comm_strs__find(struct comm_strs *comm_strs, con= st char *str) - SHARED_LOCKS_REQUIRED(comm_strs->lock) + __must_hold_shared(&comm_strs->lock) { struct comm_str **result; =20 diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index e46cd614eb21..3975b6c935a0 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -525,14 +525,14 @@ void dso__set_module_info(struct dso *dso, struct kmo= d_path *m, */ struct mutex _dso__data_open_lock; static LIST_HEAD(dso__data_open); -static long dso__data_open_cnt GUARDED_BY(_dso__data_open_lock); +static long dso__data_open_cnt __guarded_by(&_dso__data_open_lock); =20 static void dso__data_open_lock_init(void) { mutex_init(&_dso__data_open_lock); } =20 -static struct mutex *dso__data_open_lock(void) LOCK_RETURNED(_dso__data_op= en_lock) +static struct mutex *dso__data_open_lock(void) __returns_ctx_lock(&_dso__d= ata_open_lock) { static pthread_once_t data_open_lock_once =3D PTHREAD_ONCE_INIT; =20 @@ -541,7 +541,7 @@ static struct mutex *dso__data_open_lock(void) LOCK_RET= URNED(_dso__data_open_loc return &_dso__data_open_lock; } =20 -static void dso__list_add(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__= data_open_lock) +static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lo= ck) { list_add_tail(&dso__data(dso)->open_entry, &dso__data_open); #ifdef REFCNT_CHECKING @@ -552,7 +552,7 @@ static void dso__list_add(struct dso *dso) EXCLUSIVE_LO= CKS_REQUIRED(_dso__data_o dso__data_open_cnt++; } =20 -static void dso__list_del(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__= data_open_lock) +static void dso__list_del(struct dso *dso) __must_hold(&_dso__data_open_lo= ck) { list_del_init(&dso__data(dso)->open_entry); #ifdef REFCNT_CHECKING @@ -567,7 +567,7 @@ static void dso__list_del(struct dso *dso) EXCLUSIVE_LO= CKS_REQUIRED(_dso__data_o =20 static void close_first_dso(void); =20 -static int do_open(char *name) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lo= ck) +static int do_open(char *name) __must_hold(&_dso__data_open_lock) { do { int fd =3D open(name, O_RDONLY|O_CLOEXEC); @@ -670,7 +670,7 @@ void dso__put_filename(struct dso *dso __maybe_unused, = char *filename, bool deco } =20 static int __open_dso(struct dso *dso, struct machine *machine) - EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) { int fd =3D -EINVAL; char *name; @@ -703,7 +703,7 @@ static void check_data_close(void); * list/count of open DSO objects. */ static int open_dso(struct dso *dso, struct machine *machine) - EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) { int fd; struct nscookie nsc; @@ -729,7 +729,7 @@ static int open_dso(struct dso *dso, struct machine *ma= chine) return fd; } =20 -static void close_data_fd(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__= data_open_lock) +static void close_data_fd(struct dso *dso) __must_hold(&_dso__data_open_lo= ck) { if (dso__data(dso)->fd >=3D 0) { close(dso__data(dso)->fd); @@ -746,12 +746,12 @@ static void close_data_fd(struct dso *dso) EXCLUSIVE_= LOCKS_REQUIRED(_dso__data_o * Close @dso's data file descriptor and updates * list/count of open DSO objects. */ -static void close_dso(struct dso *dso) EXCLUSIVE_LOCKS_REQUIRED(_dso__data= _open_lock) +static void close_dso(struct dso *dso) __must_hold(&_dso__data_open_lock) { close_data_fd(dso); } =20 -static void close_first_dso(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open= _lock) +static void close_first_dso(void) __must_hold(&_dso__data_open_lock) { struct dso_data *dso_data; struct dso *dso; @@ -796,7 +796,7 @@ void reset_fd_limit(void) fd_limit =3D 0; } =20 -static bool may_cache_fd(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lo= ck) +static bool may_cache_fd(void) __must_hold(&_dso__data_open_lock) { if (!fd_limit) fd_limit =3D get_fd_limit(); @@ -812,7 +812,7 @@ static bool may_cache_fd(void) EXCLUSIVE_LOCKS_REQUIRED= (_dso__data_open_lock) * for opened dso file descriptors. The limit is half * of the RLIMIT_NOFILE files opened. */ -static void check_data_close(void) EXCLUSIVE_LOCKS_REQUIRED(_dso__data_ope= n_lock) +static void check_data_close(void) __must_hold(&_dso__data_open_lock) { bool cache_fd =3D may_cache_fd(); =20 @@ -834,7 +834,7 @@ void dso__data_close(struct dso *dso) } =20 static int dso__find_binary_type_locked(struct dso *dso) - EXCLUSIVE_LOCKS_REQUIRED(dso__lock(dso)) + __must_hold(dso__lock(dso)) { enum dso_binary_type binary_type_data[] =3D { DSO_BINARY_TYPE__BUILD_ID_CACHE, @@ -926,7 +926,7 @@ static int dso__find_binary_type_locked(struct dso *dso) =20 #define FIND_MAX_RETRY 3 static int __dso__find_binary_type(struct dso *dso) - EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) { int ret; int retry =3D 0; @@ -966,7 +966,7 @@ enum dso_binary_type dso__find_binary_type(struct dso *= dso) } =20 static void try_to_open_dso(struct dso *dso, struct machine *machine) - EXCLUSIVE_LOCKS_REQUIRED(_dso__data_open_lock) + __must_hold(&_dso__data_open_lock) { struct dso_data *dso_data =3D dso__data(dso); =20 diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index d5f7382d4e13..f6c60bafcde1 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -629,6 +629,7 @@ static inline void dso__set_loaded(struct dso *dso) } =20 static inline struct mutex *dso__lock(struct dso *dso) + __returns_ctx_lock(&RC_CHK_ACCESS(dso)->lock) { return &RC_CHK_ACCESS(dso)->lock; } @@ -760,7 +761,7 @@ static inline const char *dso__symsrc_filename(const st= ruct dso *dso) return RC_CHK_ACCESS(dso)->symsrc_filename; } =20 -void dso__set_symsrc_filename(struct dso *dso, char *val) EXCLUSIVE_LOCKS_= REQUIRED(dso__lock(dso)); +void dso__set_symsrc_filename(struct dso *dso, char *val) __must_hold(dso_= _lock(dso)); =20 static inline void dso__free_symsrc_filename(struct dso *dso) { @@ -809,7 +810,7 @@ void __dso__improve_id(struct dso *dso, const struct ds= o_id *id); int dso__name_len(const struct dso *dso); =20 struct dso *dso__get(struct dso *dso); -void dso__put(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock); +void dso__put(struct dso *dso) __must_not_hold(&_dso__data_open_lock); =20 static inline void __dso__zput(struct dso **dso) { @@ -919,9 +920,9 @@ void dso__set_module_info(struct dso *dso, struct kmod_= path *m, * TODO */ bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd) - EXCLUSIVE_TRYLOCK_FUNCTION(true, _dso__data_open_lock); -void dso__data_put_fd(struct dso *dso) UNLOCK_FUNCTION(_dso__data_open_loc= k); -void dso__data_close(struct dso *dso) LOCKS_EXCLUDED(_dso__data_open_lock); + __cond_acquires(true, &_dso__data_open_lock); +void dso__data_put_fd(struct dso *dso) __releases(&_dso__data_open_lock); +void dso__data_close(struct dso *dso) __must_not_hold(&_dso__data_open_loc= k); =20 int dso__data_file_size(struct dso *dso, struct machine *machine); off_t dso__data_size(struct dso *dso, struct machine *machine); diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index e927e707abac..a2cd37bd1660 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -157,7 +157,7 @@ static struct dso *__dsos__find_by_longname_id(struct d= sos *dsos, const char *name, const struct dso_id *id, bool write_locked) - SHARED_LOCKS_REQUIRED(dsos->lock) + __must_hold_shared(&dsos->lock) { struct dsos__key key =3D { .long_name =3D name, @@ -266,7 +266,7 @@ static int dsos__find_id_cb(struct dso *dso, void *data) =20 static struct dso *__dsos__find_id(struct dsos *dsos, const char *name, co= nst struct dso_id *id, bool cmp_short, bool write_locked) - SHARED_LOCKS_REQUIRED(dsos->lock) + __must_hold_shared(&dsos->lock) { struct dso *res; =20 @@ -330,7 +330,7 @@ static struct dso *__dsos__addnew_id(struct dsos *dsos,= const char *name, const } =20 static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name,= const struct dso_id *id) - SHARED_LOCKS_REQUIRED(dsos->lock) + __must_hold_shared(&dsos->lock) { struct dso *dso =3D __dsos__find_id(dsos, name, id, false, /*write_locked= =3D*/true); =20 diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c index b41171b5df77..7c0b8b15bd10 100644 --- a/tools/perf/util/intel-tpebs.c +++ b/tools/perf/util/intel-tpebs.c @@ -69,7 +69,7 @@ static struct mutex *tpebs_mtx_get(void) } =20 static struct tpebs_retire_lat *tpebs_retire_lat__find(struct evsel *evsel) - EXCLUSIVE_LOCKS_REQUIRED(tpebs_mtx_get()); + __must_hold(tpebs_mtx_get()); =20 static int evsel__tpebs_start_perf_record(struct evsel *evsel) { @@ -244,7 +244,7 @@ static void *__sample_reader(void *arg __maybe_unused) return NULL; } =20 -static int tpebs_send_record_cmd(const char *msg) EXCLUSIVE_LOCKS_REQUIRED= (tpebs_mtx_get()) +static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_ge= t()) { struct pollfd pollfd =3D { .events =3D POLLIN, }; int ret, len, retries =3D 0; @@ -321,7 +321,7 @@ static int tpebs_send_record_cmd(const char *msg) EXCLU= SIVE_LOCKS_REQUIRED(tpebs /* * tpebs_stop - stop the sample data read thread and the perf record proce= ss. */ -static int tpebs_stop(void) EXCLUSIVE_LOCKS_REQUIRED(tpebs_mtx_get()) +static int tpebs_stop(void) __must_hold(tpebs_mtx_get()) { int ret =3D 0; =20 diff --git a/tools/perf/util/libbfd.c b/tools/perf/util/libbfd.c index 58cadc93e8b5..61bd4f49afba 100644 --- a/tools/perf/util/libbfd.c +++ b/tools/perf/util/libbfd.c @@ -51,13 +51,13 @@ struct a2l_data { asymbol **syms; }; =20 -static bool perf_bfd_lock(void *bfd_mutex) NO_THREAD_SAFETY_ANALYSIS +static bool perf_bfd_lock(void *bfd_mutex) __no_context_analysis { mutex_lock(bfd_mutex); return true; } =20 -static bool perf_bfd_unlock(void *bfd_mutex) NO_THREAD_SAFETY_ANALYSIS +static bool perf_bfd_unlock(void *bfd_mutex) __no_context_analysis { mutex_unlock(bfd_mutex); return true; diff --git a/tools/perf/util/mutex.c b/tools/perf/util/mutex.c index df3b7cce3d5a..f1b99409ea91 100644 --- a/tools/perf/util/mutex.c +++ b/tools/perf/util/mutex.c @@ -125,14 +125,14 @@ void mutex_destroy(struct mutex *mtx) } =20 void mutex_lock(struct mutex *mtx) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { CHECK_ERR(mutex_take_over(mtx, pthread_mutex_lock(&mtx->lock), /*give_up=3D*/false)); } =20 void mutex_unlock(struct mutex *mtx) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { CHECK_ERR(pthread_mutex_unlock(&mtx->lock)); } diff --git a/tools/perf/util/mutex.h b/tools/perf/util/mutex.h index 5de1065ea2b9..fc01a038aefe 100644 --- a/tools/perf/util/mutex.h +++ b/tools/perf/util/mutex.h @@ -4,91 +4,13 @@ =20 #include #include - -/* - * A function-like feature checking macro that is a wrapper around - * `__has_attribute`, which is defined by GCC 5+ and Clang and evaluates t= o a - * nonzero constant integer if the attribute is supported or 0 if not. - */ -#ifdef __has_attribute -#define HAVE_ATTRIBUTE(x) __has_attribute(x) -#else -#define HAVE_ATTRIBUTE(x) 0 -#endif - -#if HAVE_ATTRIBUTE(guarded_by) && HAVE_ATTRIBUTE(pt_guarded_by) && \ - HAVE_ATTRIBUTE(lockable) && HAVE_ATTRIBUTE(exclusive_lock_function) && \ - HAVE_ATTRIBUTE(exclusive_trylock_function) && HAVE_ATTRIBUTE(exclusive_lo= cks_required) && \ - HAVE_ATTRIBUTE(no_thread_safety_analysis) - -/* Documents if a shared field or global variable needs to be protected by= a mutex. */ -#define GUARDED_BY(x) __attribute__((guarded_by(x))) - -/* - * Documents if the memory location pointed to by a pointer should be guar= ded by - * a mutex when dereferencing the pointer. - */ -#define PT_GUARDED_BY(x) __attribute__((pt_guarded_by(x))) - -/* Documents if a type is a lockable type. */ -#define LOCKABLE __attribute__((lockable)) - -/* Documents a function that expects a lock not to be held prior to entry.= */ -#define LOCKS_EXCLUDED(...) __attribute__((locks_excluded(__VA_ARGS__))) - -/* Documents a function that returns a lock. */ -#define LOCK_RETURNED(x) __attribute__((lock_returned(x))) - -/* Documents functions that acquire a lock in the body of a function, and = do not release it. */ -#define EXCLUSIVE_LOCK_FUNCTION(...) __attribute__((exclusive_lock_functi= on(__VA_ARGS__))) - -/* - * Documents functions that acquire a shared (reader) lock in the body of a - * function, and do not release it. - */ -#define SHARED_LOCK_FUNCTION(...) __attribute__((shared_lock_function(__V= A_ARGS__))) - -/* - * Documents functions that expect a lock to be held on entry to the funct= ion, - * and release it in the body of the function. - */ -#define UNLOCK_FUNCTION(...) __attribute__((unlock_function(__VA_ARGS__))) - -/* Documents functions that try to acquire a lock, and return success or f= ailure. */ -#define EXCLUSIVE_TRYLOCK_FUNCTION(...) \ - __attribute__((exclusive_trylock_function(__VA_ARGS__))) - -/* Documents a function that expects a mutex to be held prior to entry. */ -#define EXCLUSIVE_LOCKS_REQUIRED(...) __attribute__((exclusive_locks_requi= red(__VA_ARGS__))) - -/* Documents a function that expects a shared (reader) lock to be held pri= or to entry. */ -#define SHARED_LOCKS_REQUIRED(...) __attribute__((shared_locks_required(__= VA_ARGS__))) - -/* Turns off thread safety checking within the body of a particular functi= on. */ -#define NO_THREAD_SAFETY_ANALYSIS __attribute__((no_thread_safety_analysis= )) - -#else - -#define GUARDED_BY(x) -#define PT_GUARDED_BY(x) -#define LOCKABLE -#define LOCKS_EXCLUDED(...) -#define LOCK_RETURNED(x) -#define EXCLUSIVE_LOCK_FUNCTION(...) -#define SHARED_LOCK_FUNCTION(...) -#define UNLOCK_FUNCTION(...) -#define EXCLUSIVE_TRYLOCK_FUNCTION(...) -#define EXCLUSIVE_LOCKS_REQUIRED(...) -#define SHARED_LOCKS_REQUIRED(...) -#define NO_THREAD_SAFETY_ANALYSIS - -#endif +#include =20 /* * A wrapper around the mutex implementation that allows perf to error che= ck * usage, etc. */ -struct LOCKABLE mutex { +context_lock_struct(mutex) { pthread_mutex_t lock; }; =20 @@ -150,10 +72,10 @@ void mutex_init_recursive(struct mutex *mtx); bool mutex_init_pshared_robust(struct mutex *mtx); void mutex_destroy(struct mutex *mtx); =20 -void mutex_lock(struct mutex *mtx) EXCLUSIVE_LOCK_FUNCTION(*mtx); -void mutex_unlock(struct mutex *mtx) UNLOCK_FUNCTION(*mtx); +void mutex_lock(struct mutex *mtx) __acquires(mtx); +void mutex_unlock(struct mutex *mtx) __releases(mtx); /* Tries to acquire the lock and returns true on success. */ -bool mutex_trylock(struct mutex *mtx) EXCLUSIVE_TRYLOCK_FUNCTION(true, *mt= x); +bool mutex_trylock(struct mutex *mtx) __cond_acquires(true, mtx); =20 /* Default initialize the cond struct. */ void cond_init(struct cond *cnd); @@ -164,7 +86,7 @@ void cond_init(struct cond *cnd); void cond_init_pshared(struct cond *cnd); void cond_destroy(struct cond *cnd); =20 -void cond_wait(struct cond *cnd, struct mutex *mtx) EXCLUSIVE_LOCKS_REQUIR= ED(mtx); +void cond_wait(struct cond *cnd, struct mutex *mtx) __must_hold(mtx); void cond_signal(struct cond *cnd); void cond_broadcast(struct cond *cnd); =20 diff --git a/tools/perf/util/rwsem.c b/tools/perf/util/rwsem.c index 9d26832398db..cc720b482333 100644 --- a/tools/perf/util/rwsem.c +++ b/tools/perf/util/rwsem.c @@ -27,7 +27,7 @@ int exit_rwsem(struct rw_semaphore *sem) } =20 int down_read(struct rw_semaphore *sem) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { #if RWS_ERRORCHECK mutex_lock(&sem->mtx); @@ -38,7 +38,7 @@ int down_read(struct rw_semaphore *sem) } =20 int up_read(struct rw_semaphore *sem) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { #if RWS_ERRORCHECK mutex_unlock(&sem->mtx); @@ -49,7 +49,7 @@ int up_read(struct rw_semaphore *sem) } =20 int down_write(struct rw_semaphore *sem) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { #if RWS_ERRORCHECK mutex_lock(&sem->mtx); @@ -60,7 +60,7 @@ int down_write(struct rw_semaphore *sem) } =20 int up_write(struct rw_semaphore *sem) - NO_THREAD_SAFETY_ANALYSIS + __no_context_analysis { #if RWS_ERRORCHECK mutex_unlock(&sem->mtx); diff --git a/tools/perf/util/rwsem.h b/tools/perf/util/rwsem.h index b102d8143181..235bb63c92d0 100644 --- a/tools/perf/util/rwsem.h +++ b/tools/perf/util/rwsem.h @@ -10,7 +10,7 @@ */ #define RWS_ERRORCHECK 0 =20 -struct LOCKABLE rw_semaphore { +context_lock_struct(rw_semaphore) { #if RWS_ERRORCHECK struct mutex mtx; #else @@ -21,10 +21,10 @@ struct LOCKABLE rw_semaphore { int init_rwsem(struct rw_semaphore *sem); int exit_rwsem(struct rw_semaphore *sem); =20 -int down_read(struct rw_semaphore *sem) SHARED_LOCK_FUNCTION(sem); -int up_read(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem); +int down_read(struct rw_semaphore *sem) __acquires_shared(sem); +int up_read(struct rw_semaphore *sem) __releases_shared(sem); =20 -int down_write(struct rw_semaphore *sem) EXCLUSIVE_LOCK_FUNCTION(sem); -int up_write(struct rw_semaphore *sem) UNLOCK_FUNCTION(sem); +int down_write(struct rw_semaphore *sem) __acquires(sem); +int up_write(struct rw_semaphore *sem) __releases(sem); =20 #endif /* _PERF_RWSEM_H */ diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 91118c301913..616d5bf87baf 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -41,7 +41,7 @@ int thread__init_maps(struct thread *thread, struct machi= ne *machine) } =20 struct thread *thread__new(pid_t pid, pid_t tid) - NO_THREAD_SAFETY_ANALYSIS /* Allocation/creation is inherently single thr= eaded. */ + __context_unsafe(/* Allocation/creation is inherently single threaded. */) { RC_STRUCT(thread) *_thread =3D zalloc(sizeof(*_thread)); struct thread *thread; @@ -203,7 +203,7 @@ int thread__set_namespaces(struct thread *thread, u64 t= imestamp, } =20 static struct comm *__thread__comm(struct thread *thread) - SHARED_LOCKS_REQUIRED(thread__comm_lock(thread)) + __must_hold_shared(thread__comm_lock(thread)) { if (list_empty(thread__comm_list(thread))) return NULL; @@ -250,7 +250,7 @@ struct comm *thread__exec_comm(struct thread *thread) =20 static int ____thread__set_comm(struct thread *thread, const char *str, u64 timestamp, bool exec) - EXCLUSIVE_LOCKS_REQUIRED(thread__comm_lock(thread)) + __must_hold(thread__comm_lock(thread)) { struct comm *new, *curr =3D __thread__comm(thread); =20 @@ -308,7 +308,7 @@ int thread__set_comm_from_proc(struct thread *thread) } =20 static const char *__thread__comm_str(struct thread *thread) - SHARED_LOCKS_REQUIRED(thread__comm_lock(thread)) + __must_hold_shared(thread__comm_lock(thread)) { const struct comm *comm =3D __thread__comm(thread); =20 diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index d82fce8173ae..9d0ce7742c06 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -251,7 +251,7 @@ static inline struct rw_semaphore *thread__comm_lock(st= ruct thread *thread) } =20 static inline struct list_head *thread__comm_list(struct thread *thread) - SHARED_LOCKS_REQUIRED(thread__comm_lock(thread)) + __must_hold_shared(thread__comm_lock(thread)) { return &RC_CHK_ACCESS(thread)->comm_list; } --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244335; b=gdguCfBEwTmkq5JZHiFBd7e33KeJF5xRWyvmFmcERAvPUvZDpCELPe2IuHVfWW6VWkQY UnxR5YbGDHHWJhd5OB9VZcEkY2vhDAywwCKL/LaQtrp14s4hZ8Ymv4o3tNGMRrXxeq0n8 1OF1JMjGR/MfnJJQjAkh+pY80+4uU+4abR/SfBwIMFI9JgbrZFMabhhfp182jKtamZ+/T HGjYDp3W0WQE4kuTuQ+GI1kNDOOr9X388JRdEJ2JDFalvlAr/TnZjbOIr34dX9/nEg99d iEuQ6f15skw5UZo1fxyEfJNen4iNNcFbRnmjpMSYDipde83vui4V2o2GDPBwO+UX0eQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244335; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=fuvtSLQkw2xn+flfUo7lSRyM8NUUkLIzaxviYb0ux+A=; b=ZEYIkRnirGPdvAW8iUx+2POF/cYYWK834+ZnUVoeNVtdbRaR9sYmkatDogIYxjmWs6cG t77wVDXAyhw/bxfRWO6paQsTI7Df2UMx5lMk2NW4IXI3ZW+ZrT7ruxnvk/IN+hvc5b4lb vkGfqsrg2AZeiIAcOMchX9I9HAr34qwk+OemDi8LpWeHDIttkQ5SIJQGNxC5MTIGWBOUn ikEXqRBEsQ20koQlQpQp+ZB8RxNGCFwjcZrFj1bawePtT3cRu3+G/4QzKxZKBTn+Fosqu SugL/71DBOh9pIcdcpu/2yDfkUtttoc6n+NpH0j6TQWirNbDdWqBa+8Ski7bEay+yjg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ApjPnM0W; arc=none smtp.remote-ip=209.85.216.71 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FC76606A320 for ; Sat, 21 Nov 2026 06:58:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3FC76606A320 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=ApjPnM0W Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-3ae19907a31so1732097a91.0 for ; Fri, 20 Nov 2026 22:58:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244334; x=1795849134; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fuvtSLQkw2xn+flfUo7lSRyM8NUUkLIzaxviYb0ux+A=; b=ApjPnM0WSy+DEjdxx4HXB5SaWqeVaqag+bp6fZgUE04lay/Z/09sMx3Fcc4za8tgIY w3WYxB9bQhSOYGyK1GPJGcFDVXKpfzio1NAn1Zd7Jgd5LoQ51+/a0GayJmK4K5BLZLI8 6PIQ72R271ICn5hqYHKT2MZKtZwD3DXaKBjExptYPnUPOBgTXtpSxJI71pWyuNW+4xyC S9BzR3Ao+sm4DcPl1hFehw3wBNj96z4DigKGRNaW/cVKwUfFO1YJeMid9bSPPNAd3QUn OR+yAE86+asFeoi0xc3smPWoW38wOUr439/BE/5rKPA3SPspwD1l688LV/3n5Rd+0giz QqYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244334; x=1795849134; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fuvtSLQkw2xn+flfUo7lSRyM8NUUkLIzaxviYb0ux+A=; b=1N8dWNFqqdeyNONpbOC6iRYwhz7pZ+JUPZWymuwchhn8Fk7zfjSNORT8aWM/kpEmDf YT4pj6XXhKA3akJeOBEsHaFN12XXh6JRjLXCe8U6qR+TX9oUr/5WzaytDzey5J6Y0Mnn VRRUfbAx6aFD7K3d9uecEunWqXpwizBIj03jXViWLLdKYRMSYS8dmnH+v+87yz2zWYHr IPn54UCuah18ul4MGd3rddset0Y3dRI1tBBH9ygB1lRHl4M//i8xzfnm74/7X75C3r7o F2bnQCiuyLNteIo1Rxyj9OajWZG+dG0xZssaKQNfkKrrn/h6lHyGInOuZPT4ehcJ/zxL eUhg== X-Forwarded-Encrypted: i=1; AL+hErFQzi1kfsZZqsIEUHDKPqrfJByryORlQiHvJNtygn0s3QNDxwkCEgtiU6lHF5mBIVods0lHglcV9+eD69c=@vger.kernel.org X-Gm-Message-State: APuU4Z/Q58O0+oXKSAerJZFEGkqivLGObBcZvLHlFGIFAEhziI3taziR 9Dc1xFEAOe63z4nVJz3v4HRV3JCZPBpma0KUGXUU51hkJLsBwny87fMbT7j6MpELDO/2NkYocgE a9ngeeVIqvw== X-Received: from plgw12.prod.google.com ([2002:a17:902:e88c:b0:2ed:4e53:853d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:164d:b0:3b3:a575:846a with SMTP id 98e67ed59e1d1-3b40a5de1bcmr4960690a91.22.1795244334321; Fri, 20 Nov 2026 22:58:54 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:54 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-6-irogers@google.com> Subject: [PATCH v15 05/23] perf build: Enable context analysis and Clang thread-safety flags From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Define -DWARN_CONTEXT_ANALYSIS for Clang builds so the context analysis attributes in linux/compiler-context-analysis.h expand to Clang's thread-safety attributes rather than no-ops, matching the kernel's scripts/Makefile.context-analysis. Also include tools/build/Build.include (updating TMPOUT to place temporary probe directories under $(OUTPUT) when set) and use cc-option to enable -Wthread-safety-pointer, -Wthread-safety-beta, and -fexperimental-late-parse-attributes when supported by Clang. Signed-off-by: Ian Rogers --- tools/build/Build.include | 4 ++-- tools/perf/Makefile.config | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/build/Build.include b/tools/build/Build.include index cd0baa7a168d..cce5e4673ba6 100644 --- a/tools/build/Build.include +++ b/tools/build/Build.include @@ -113,7 +113,7 @@ rust_flags =3D $(filter-out $(RUST_FLAGS_REMOVE_$(obj= )), $(rust_flags_2)) host_c_flags =3D -Wp,-MD,$(depfile) -Wp,-MT,$@ $(HOSTCFLAGS) -D"BUILD_STR(= s)=3D\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj)) =20 # output directory for tests below -TMPOUT =3D .tmp_$$$$ +TMPOUT =3D $(if $(OUTPUT),$(OUTPUT)/,).tmp_$$$$ =20 # try-run # Usage: option =3D $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) @@ -121,8 +121,8 @@ TMPOUT =3D .tmp_$$$$ # automatically cleaned up. try-run =3D $(shell set -e; \ TMP=3D$(TMPOUT)/tmp; \ - mkdir -p $(TMPOUT); \ trap "rm -rf $(TMPOUT)" EXIT; \ + mkdir -p $(TMPOUT); \ if ($(1)) >/dev/null 2>&1; \ then echo "$(2)"; \ else echo "$(3)"; \ diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 1cee80c531ab..aed2e952c8f3 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -12,6 +12,8 @@ ifneq ($(obj-perf),) obj-perf :=3D $(abspath $(obj-perf))/ endif =20 +include $(srctree)/tools/build/Build.include + $(shell printf "" > $(OUTPUT).config-detected) detected =3D $(shell echo "$(1)=3Dy" >> $(OUTPUT).config-detecte= d) detected_var =3D $(shell echo "$(1)=3D$($(1))" >> $(OUTPUT).config-detecte= d) @@ -63,8 +65,11 @@ ifeq ($(CC_NO_CLANG), 0) CXX :=3D $(CXX) $(CLANG_FLAGS) -fintegrated-as endif =20 - # Enabled Wthread-safety analysis for clang builds. - CFLAGS +=3D -Wthread-safety + # Enable context analysis (linux/compiler-context-analysis.h gates its C= lang + # attributes behind WARN_CONTEXT_ANALYSIS) and Clang thread-safety check= s. + CFLAGS +=3D -DWARN_CONTEXT_ANALYSIS -Wthread-safety + CFLAGS +=3D $(call cc-option,-Wthread-safety-pointer -Wthread-safety-bet= a) + CFLAGS +=3D $(call cc-option,-fexperimental-late-parse-attributes) endif =20 include $(srctree)/tools/scripts/Makefile.arch --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244341; b=NUiRT7VfQJrAuBZuaTq/O/z9TlSKlqLN9w6JwPO0xXtVc+4sSy4chhR9ym1SJEljmTF+ qJfqjnapvOi7Pe7a3bv+aXHbSZXzxEhnArMPF5sFZrbno8ziE9FBcMpkMYhtZskFMeLOL CDY5fNgfEfn5YxXYlAVD7A3UZan9xrNPNMoNjCKIVHlvxteBiwj1KsksV0U5XRtVp8YIr vX36zMngZ+BFhP7OcnXG56RrhjNeWLnuVXFhORES0CbuJBIsHdzva9B07lITRF++M1pdv UgfJY9MsBqlY9+65iZIxobMM4+C5HZUNwuMi/BODn3cyT9jAmiQqRjzKVVyiGiu2qnA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244341; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=5uxDxgfBGA5yTPHcCIqyXHP3BnAmG6Jm0Jp5UQ/3dx8=; b=FAa6dOZoCkz/q9h6Hwe7HKdsQhFxyGxS5JRMqKLGs+PC6qyPKv1ihuiPjS02lV1m9yJ5 o6WsAxYqLCtPGisGPrvaq3TAaSOE1ttXZImGVWxl0cx7BS4hD7G0sKcXY+QXgLA05IEG2 sx5sdWCgjWwzNEYMw3tLOLaDlBQFWAwf9TuLiUH0TaMuMIzx6LKy72GWDhhIa1oqXKYY+ Qq2/lwC6D6pIBLvVLBJfHhujkNVo/C0dtoaDX94hZKU8y7JcaHCK2IOTDACrpzCpvrHU+ i8A/FeFbTLwGFEuHTQeVaF9EZ8RH/4lbhLOpyn6QrYu1x2qgDmceE4dCMctOZJ4DISA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=RXfniuAf; arc=none smtp.remote-ip=209.85.210.70 Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A0AB606A333 for ; Sat, 21 Nov 2026 06:59:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6A0AB606A333 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=RXfniuAf Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-862bca06702so3294240a34.0 for ; Fri, 20 Nov 2026 22:59:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244340; x=1795849140; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=5uxDxgfBGA5yTPHcCIqyXHP3BnAmG6Jm0Jp5UQ/3dx8=; b=RXfniuAfp3EtgdBAKLr75KxU1qE5n7pDoI4d1alzGAlfMRAwN3+g3ZJnmIGW4r1buS 0NF7vvAxIMfQLGu2DiyROfiBeiWT50y0M9CRyJrT5qpvGCRcoKV1gO7XCWzbUrIhH6po KyzoqFq/Aj/ZvpMLYhuGzkTV9W7IoPEFMAHmkjc19hMbMZ5yxxr+wk2AL4Ylo8qzKAqr +FwiIPzRmEtTe4ZjvPr0OdLQboYn84U7cpF6/on+F3ewqtk65+gr5q4YhrUINMSpSbe0 KcxKZlQh/2El9pk+oyQ9X/EjtaDhXnNfpUx6x9DbbYd0iA+9I6jOpHZ7YoKXp+80Lpkj 9nQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244340; x=1795849140; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=5uxDxgfBGA5yTPHcCIqyXHP3BnAmG6Jm0Jp5UQ/3dx8=; b=Q78GhHgszy1LLWWey2s8/QlsLYrL6P0yuWhwQO8dWFoRPz5XsdN0k+nOwvn0smEHpm CReM0IsZUIaL9lXnmvrG267kSo0vNIzGqVXAge0r3vm5iUZSFHijl8E7P/ib9LWgUNsN gYH/Q0y+setiRSWI3BwxZbe+VcPZ9VVJLI2/2aujYAGZHu6IUkM03IUJFFwPd0BRZDJc N6c2vEfBeXx0Ay0hhnbaZAG53EvfPqois4/2ovopaJcTWSy+NBt78C04+caXQ4vnd11V n55b2AFvmb4AMx4+Zxim64KH+doaQM9NIArremlu2dSOjuVR3YRqIe2K98tqNpCZLRY8 9ylA== X-Forwarded-Encrypted: i=1; AL+hErFRsETfl8w+WsGYJcwSrc5ChJyNil32M7G4LDg3moOB4WKaj3SRqv4QJ/lLcaWlvmFoQpHC31CnB6Jqjrs=@vger.kernel.org X-Gm-Message-State: APuU4Z9umgQjfRqFMlro7+pgDtZfM+3CYO2PtZEvSKAoSn8vMbkbXxRT Npv8MEOoWdcqs56fCpbOLTE0hvcTVtyz8qc7lyK5IKubaXiryhWEF46b+MUgTRNhcpjFNdG7Qv0 +3k4mbKGocQ== X-Received: from iobdo25.prod.google.com ([2002:a05:6602:4619:b0:9ca:bcca:dd8b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:640a:b0:572:e8fb:2125 with SMTP id 5614622812f47-57a09504aadmr5490293b6e.20.1795244340118; Fri, 20 Nov 2026 22:59:00 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:55 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-7-irogers@google.com> Subject: [PATCH v15 06/23] perf comm: Add __guarded_by annotations to struct comm_strs From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Annotate strs, num_strs, and capacity in struct comm_strs with __guarded_by(&lock) and __pt_guarded_by(&lock) so Clang verifies all accesses hold comm_strs->lock. Signed-off-by: Ian Rogers --- tools/perf/util/comm.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/comm.c b/tools/perf/util/comm.c index 6343a3c9debe..9fc4e91a36e9 100644 --- a/tools/perf/util/comm.c +++ b/tools/perf/util/comm.c @@ -16,9 +16,9 @@ DECLARE_RC_STRUCT(comm_str) { =20 static struct comm_strs { struct rw_semaphore lock; - struct comm_str **strs; - int num_strs; - int capacity; + struct comm_str **strs __pt_guarded_by(&lock) __guarded_by(&lock); + int num_strs __guarded_by(&lock); + int capacity __guarded_by(&lock); } _comm_strs; =20 static void comm_strs__remove_if_last(struct comm_str *cs); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244343; b=f0LwfEGnQhgXwPEv7vebe5r7xKv+T3r6wgHsnNnwrLnJR6mKiYrq+BNShQycFT+XrgU5 RqI5kwEjU8DZ8OKvCsTONA0Hu8DGKAiR/MsjJOqGJxZ8LWhfnMukh7z/8rlWrTodCq/Tn 7In6Vn8+xZRbiwW0wpwhIHqtfqI+0CSas3/elVmiwm/5CGtYn2iJvNf9/sVvJEuBIkL5/ Ob4LiKVqBJW1CbkZcYpW+ILnTixCPiIs8QlCs3zDT65xwZ+mV/TbpzSD6Q1pMcZoHUpK3 lbmi/PYavOqJDMTvjMAcFdLQ9FCgh7HTF4eiBGBvsIsWfkxt8fKjrZeE2Ob0He54P8A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244343; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=ceOCiUal3x2CUb6+gSFDn+xj67LposR+qOo8ZKtSNko=; b=OwAxoPtkHzv1PVv8aDHw3pFasPariNPxVOCPLMseAse6OyfHz7wSa01O8FPwb12nU/wm eiWajNX0qCCV8KcQrEr2jC3QLPXUMcgO8YFnRwWa4jQq7o6uiqpQ5A4JQIFwE22SdAk// LhSyPwFdJj2kdLwINb8GCP8nw2N8NxITTqV4B2vTsfNye8j+TS8ELEz2mO0T9p+dPiTbx eKcBJoMr4pcWaAPw1o3fZXIqoTkzFRSS+qg/R7DIxDDAgmcYYL+syX4mN0lakJvCbSTPG e17BHtBzWp2bkKjPRH3R5RxXA8SZbSoQMEm08AksTSgaTfFh7AIKDKNU8VZ6gIht9GA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="QYPeg/GU"; arc=none smtp.remote-ip=74.125.82.198 Received: from mail-dy1-f198.google.com (mail-dy1-f198.google.com [74.125.82.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F372606A33A for ; Sat, 21 Nov 2026 06:59:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 7F372606A33A 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=QYPeg/GU Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-341eff9e381so2640807eec.1 for ; Fri, 20 Nov 2026 22:59:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244343; x=1795849143; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ceOCiUal3x2CUb6+gSFDn+xj67LposR+qOo8ZKtSNko=; b=QYPeg/GUH0W8t0GKwXHdQVkNgatdDcUR+sG+6SpGYag03+hy1WTBQnWeuuILssHCAy VI0NQYGzGHlET0gPM7krnXW6BZX8TVBhUm8MHNAlWAkYEUgWpOqflYHOMYGcoYX5fpIS cibXR77KZqBpmRdQ5gdHvlkjb878WoaxpbDEmSWVLPYcmuNCa0LtHPriQcoZg2WXuVvf OYbtxe1IbcmYoxi5cyAo6Y3iKafgj4w2UGf/Nbe3dQyf5/XEBWd9bRg6dzfG9wtFr+rR 0pWBIBv/gZUDiIH3vOROTYYqStKon4w/iHD8sVI3j3PUr6bTTiSgTaS1gXjyayoYO9td vQZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244343; x=1795849143; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=ceOCiUal3x2CUb6+gSFDn+xj67LposR+qOo8ZKtSNko=; b=PB7s5u1pVQ55tjivc79yEDF6t6SbcLqTpDm8smIXW3ej55zF43vP0y7V0LfCd7EcDE OeE1nzbJ+bmGEXTBmmgc582A09VGpF2qfIMdHVbgtM67IOcgAIMT5jxosOcX2PPG+v9g KaAR957E2kle1erbtj1FKo+5cJ2rky5Dk2RDk8uCMWgX8PAFm4lwo2iLoHQMX7vC9ybE IDmpTNjz6J8anzteo8/Dx3kfsO27kQv+ntL6ZJJa4TUDil0fpDHgzs0soRqKMglolEx7 P4kT1R2XqqYThF603OUk3D372oIZ9Xby1GA8u9seP8EWPI7y+FqAi2R//lDoNKu9OuEC 8eSA== X-Forwarded-Encrypted: i=1; AL+hErEDWIndtm78syNLUPJwuNgHhxrzcIduTZEl9D6r6MEU2dF/rPnaTzQFsJT30iQ2ORuJSR1QTtSt6bb0s3k=@vger.kernel.org X-Gm-Message-State: APuU4Z+i24zyAuDI5TcjOplA1vKwvHI/ARYFZ8bM2fmT8+HT3qH8qnYl OSZk4Maw1crhsZ/tUAEseB8pPr3GpQ87QUFQHhBU3DMEHQc98doNhjn7lU/QwVNd77TzVMYil/7 nsZbYqWNOgg== X-Received: from dyjz27.prod.google.com ([2002:a05:7300:b1b:b0:381:fb4c:34e1]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:8641:20b0:383:50b7:1d78 with SMTP id 5a478bee46e88-38571223ee7mr5801142eec.40.1795244342218; Fri, 20 Nov 2026 22:59:02 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:56 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-8-irogers@google.com> Subject: [PATCH v15 07/23] perf vdso: Fix unlocked __dsos__add() and race in __machine__addnew_vdso() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" __machine__addnew_vdso() called __dsos__add(&machine->dsos, dso) without holding machine->dsos.lock, and machine__findnew_vdso() and __machine__findnew_compat() initialized machine->vdso_info and created temporary VDSO files without synchronization. Acquire machine->dsos.lock in write mode across machine->vdso_info initialization, duplicate checking via __dsos__find(), temporary file creation, dso__set_long_name(), and __dsos__add(). Fixes: f649ed80f3ca ("perf dsos: Tidy reference counting and locking") Signed-off-by: Ian Rogers --- tools/perf/util/dsos.c | 8 ++++++- tools/perf/util/dsos.h | 2 ++ tools/perf/util/vdso.c | 52 +++++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index a2cd37bd1660..1574c8885d78 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -284,12 +284,18 @@ static struct dso *__dsos__find_id(struct dsos *dsos,= const char *name, const st return res; } =20 +struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_sho= rt, bool write_locked) + __must_hold_shared(&dsos->lock) +{ + return __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, write_locked= ); +} + struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short) { struct dso *res; =20 down_read(&dsos->lock); - res =3D __dsos__find_id(dsos, name, &dso_id_empty, cmp_short, /*write_loc= ked=3D*/false); + res =3D __dsos__find(dsos, name, cmp_short, /*write_locked=3D*/false); up_read(&dsos->lock); return res; } diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h index a26774950866..dbd06f420753 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -30,6 +30,8 @@ void dsos__exit(struct dsos *dsos); =20 int __dsos__add(struct dsos *dsos, struct dso *dso); int dsos__add(struct dsos *dsos, struct dso *dso); +struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_sho= rt, bool write_locked) + __must_hold_shared(&dsos->lock); struct dso *dsos__find(struct dsos *dsos, const char *name, bool cmp_short= ); =20 struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const st= ruct dso_id *id); diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index 7f2dde70f3e4..8bf81e2c9e78 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -131,8 +131,13 @@ static struct dso *__machine__addnew_vdso(struct machi= ne *machine, const char *s =20 dso =3D dso__new(short_name); if (dso !=3D NULL) { - __dsos__add(&machine->dsos, dso); + /* + * Set the long name before adding to machine->dsos so that + * __dsos__add() inserts it in sorted order without needing to + * re-take machine->dsos.lock. + */ dso__set_long_name(dso, long_name, false); + __dsos__add(&machine->dsos, dso); } =20 return dso; @@ -250,15 +255,19 @@ static struct dso *__machine__findnew_compat(struct m= achine *machine, const char *file_name; struct dso *dso; =20 - dso =3D dsos__find(&machine->dsos, vdso_file->dso_name, true); + down_write(&machine->dsos.lock); + dso =3D __dsos__find(&machine->dsos, vdso_file->dso_name, true, /*write_l= ocked=3D*/true); if (dso) - return dso; + goto out_unlock; =20 file_name =3D vdso__get_compat_file(vdso_file); if (!file_name) - return NULL; + goto out_unlock; =20 - return __machine__addnew_vdso(machine, vdso_file->dso_name, file_name); + dso =3D __machine__addnew_vdso(machine, vdso_file->dso_name, file_name); +out_unlock: + up_write(&machine->dsos.lock); + return dso; } =20 static int __machine__findnew_vdso_compat(struct machine *machine, @@ -332,33 +341,38 @@ struct dso *machine__findnew_vdso(struct machine *mac= hine, { struct vdso_info *vdso_info; struct dso *dso =3D NULL; - char *file; + const char *file; =20 + dso =3D machine__find_vdso(machine, thread); + if (dso) + return dso; + + down_write(&machine->dsos.lock); if (!machine->vdso_info) machine->vdso_info =3D vdso_info__new(); =20 vdso_info =3D machine->vdso_info; - if (!vdso_info) + if (!vdso_info) { + up_write(&machine->dsos.lock); return NULL; - - dso =3D machine__find_vdso(machine, thread); - if (dso) - return dso; + } =20 #if BITS_PER_LONG =3D=3D 64 + up_write(&machine->dsos.lock); if (__machine__findnew_vdso_compat(machine, thread, vdso_info, &dso)) return dso; + down_write(&machine->dsos.lock); #endif =20 - dso =3D dsos__find(&machine->dsos, DSO__NAME_VDSO, true); - if (dso) - return dso; - - file =3D get_file(&vdso_info->vdso); - if (!file) - return NULL; + dso =3D __dsos__find(&machine->dsos, DSO__NAME_VDSO, true, /*write_locked= =3D*/true); + if (!dso) { + file =3D get_file(&vdso_info->vdso); + if (file) + dso =3D __machine__addnew_vdso(machine, DSO__NAME_VDSO, file); + } + up_write(&machine->dsos.lock); =20 - return __machine__addnew_vdso(machine, DSO__NAME_VDSO, file); + return dso; } =20 bool dso__is_vdso(const struct dso *dso) --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244346; b=Tv6WpNahjE/dTUo/vYhDzzkvJTAiPfJvERWVKqmRpTYA6Cyhb5N+lj2w1SLKeNk0x1f7 DjHnX7K0b+PQP6qunqOkSQzzZDMpN7uLiuwE0aROQXOo1QgPujsZVvZCZvax7x4S6HyB/ WkefAM5f0swG7hlOaNQLdTrfgQJ1g1SWsXmEcnPl48S72E74PsnEneTwd64u9ZZONfC6H 8+YtOzYGLzEtOtbCrSbam2CNBExODdXzYzCXvuWFLdwkNe/M2/i584NTujFg6fgjadCF4 Qf7itB57CUfRhVX8hThnUa1eB96pC3mbZQfcYWkAYT25ssVZGChsqYQyVGCC5G0S5Hw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244346; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=YaOrepU6AaV9bsaJ1eqR9wb353r378iiA6um3H27iXs=; b=MvjacMrjiZrgWRQSaBRtxCBG0Dw0hZL0TNKDUapBKtqm/RCYuNiX5jZi8kCoQaAv88wL +eKSxrWN9+DX1G0B9V4GwgMI+IVVE64slR31ojVE/nL3ll65a5HOnzJavzfK9dn86tKyG PC6vcFtaBuaEha2zhbV39hZtY/G6C3e+/ptjUKDJUrtni5L2CnHbpPKxPJTZogEoSWY2M kjMP7Jax5CxUwnfDqYi2mnMTZUhKVDWUaIdoQMqx4ikyh8tm/utzDlssCcZ2oRjjpzoZG Q6rFU+P2TT8gl2d6jWheNQ/hBq2u8MGmVq4QvQXVY9bRqdHPagG9axfxvNcPJef3aew== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=NWlG9GPF; arc=none smtp.remote-ip=74.125.82.72 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F095D606A685 for ; Sat, 21 Nov 2026 06:59:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F095D606A685 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=NWlG9GPF Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-1b25d977b91so1915186c88.1 for ; Fri, 20 Nov 2026 22:59:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244345; x=1795849145; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=YaOrepU6AaV9bsaJ1eqR9wb353r378iiA6um3H27iXs=; b=NWlG9GPFXRrhC8HnN8Fpxhynk2bHNo+O3biLoOEDn5YG2Xz+N2CNW7biIP8VqFnp1/ M2i6oBpBXZXV7BlrLOKwPOclFTtaocGAy3Qg5K/4OQnNuZiHSZ0oO3YZqRz7+GBVVbSr 39A+KXkX71ecY0RH15tch1YysYPHUoD4lURF+bAsZVF6VeG3dipI0rpuDOqWwMtlcFTu mS24FJeYYgkjYaZQDqYrL2YzBIdS3eXwXR3cm1JYnLL8o6gJ9W9R0l1q8PYuPt9mjmQC ejRWH7vh0y2UJEcwSAT0eaGNKtU5oDu9cLvpg+Pe6eWXN3PZ18tmohriQWZuEVsuHQjA sU0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244345; x=1795849145; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=YaOrepU6AaV9bsaJ1eqR9wb353r378iiA6um3H27iXs=; b=0g7mOyGP4XU6OBo+HELuCOlz8JDy66CvXt2mYqj5bGm4Lowf0LfrVueevk9ZTxFz9Y RYWqF+KmJTPj/wS7Z8rg2l+vp6pPh0JLqHcddY49rKV5ZPXwl9RYhN67il0k1gC/RK1/ aN/7dNxOIIaXzpHtH5MULYXJfQmy57+hs2NZ2PByXJUP7mRrEELR9SmYQheOVJYwrIAE vcsMQoCRQ94q/4JJmwP7Qq0jAzyVzEa/BuLr8wSgWYiYdTnoS+Qyo3TjoMFi4JmcB57L XCX9Inmq9iHRG4yN5kALgEl9+S/oOOErzgA/NjlRIF3fFCv+hWTJz95aRq/QJiZdboYa UrFw== X-Forwarded-Encrypted: i=1; AL+hErFR8i2vtZ2g4WPtM2ifSxcpNOdcbbp2jVX3qBYr0ItK8sCXjDZR9Bpcw9NGdywo/9TxtQVxO6aiIjut3zE=@vger.kernel.org X-Gm-Message-State: APuU4Z9HrpbO5TYCJFAJbrajRNeV4eBUp44smUA3GvvhVRjBTQsNf0w/ qtrXlIEZEXHKvT4xjFxAiPBijF6cxaC+02ex3NtJf3dBPiWnnwG5sOj51eFF9tbZrlnqp2GEf/L zqBObKccwLg== X-Received: from dled20-n1.prod.google.com ([2002:a05:701b:42d4:10b0:1b3:befc:72bc]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:2508:b0:1b2:3497:5998 with SMTP id a92af1059eb24-1b5568f03abmr4870446c88.36.1795244344566; Fri, 20 Nov 2026 22:59:04 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:57 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-9-irogers@google.com> Subject: [PATCH v15 08/23] perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Factor out dso__is_bpf_prog() from __map__is_bpf_prog() and update dsos__find_kernel_dso_cb() to skip dso__is_bpf_prog(dso), DSO_BINARY_TYPE__BPF_IMAGE, and DSO_BINARY_TYPE__OOL so that dsos__find_kernel_dso() does not match ksymbol DSOs in machine->dsos. In machine__process_ksymbol_register(), also check event->ksymbol.ksym_type =3D=3D PERF_RECORD_KSYMBOL_TYPE_BPF before setting DSO_BINARY_TYPE__BPF_IMAGE for is_bpf_image() symbols prior to inserting the DSO. Signed-off-by: Ian Rogers --- tools/perf/util/dso.h | 16 ++++++++++++++++ tools/perf/util/dsos.c | 3 +++ tools/perf/util/machine.c | 9 ++++----- tools/perf/util/map.c | 14 ++------------ 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index f6c60bafcde1..009bd3001ace 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -1022,6 +1022,22 @@ static inline bool dso__is_kallsyms(const struct dso= *dso) return is_guest_kallsyms_pid_name(name); } =20 +static inline bool dso__is_bpf_prog(const struct dso *dso) +{ + const char *name; + + if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) + return true; + + /* + * If PERF_RECORD_BPF_EVENT is not included, the dso will not have + * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can + * guess the type based on name. + */ + name =3D dso__short_name(dso); + return name && !strncmp(name, "bpf_prog_", sizeof("bpf_prog_") - 1); +} + static inline enum dso_binary_type dso__dbginfo_type(const struct dso *dso) { return RC_CHK_ACCESS(dso)->dbginfo_type; diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index 1574c8885d78..a7f6a98acbaf 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -475,6 +475,9 @@ static int dsos__find_kernel_dso_cb(struct dso *dso, vo= id *data) * is_kernel_module() treats it as a kernel cpumode. */ if (!dso__kernel(dso) || + dso__is_bpf_prog(dso) || + dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_IMAGE || + dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__OOL || is_kernel_module(dso__long_name(dso), PERF_RECORD_MISC_CPUMODE_UNKNOW= N)) return 0; =20 diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 5b4550e97172..4a3461d79593 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -791,6 +791,10 @@ static int machine__process_ksymbol_register(struct ma= chine *machine, dso__set_binary_type(dso, DSO_BINARY_TYPE__OOL); dso__data(dso)->file_size =3D event->ksymbol.len; dso__set_loaded(dso); + } else if (event->ksymbol.ksym_type =3D=3D PERF_RECORD_KSYMBOL_TYPE_BPF = && + is_bpf_image(event->ksymbol.name)) { + dso__set_binary_type(dso, DSO_BINARY_TYPE__BPF_IMAGE); + dso__set_long_name(dso, "", false); } =20 map__set_start(map, event->ksymbol.addr); @@ -802,11 +806,6 @@ static int machine__process_ksymbol_register(struct ma= chine *machine, } =20 dso__set_loaded(dso); - - if (is_bpf_image(event->ksymbol.name)) { - dso__set_binary_type(dso, DSO_BINARY_TYPE__BPF_IMAGE); - dso__set_long_name(dso, "", false); - } } else { dso =3D dso__get(map__dso(map)); } diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index d5319ffa88a7..5155c95df85d 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -248,19 +248,9 @@ bool __map__is_extra_kernel_map(const struct map *map) =20 bool __map__is_bpf_prog(const struct map *map) { - const char *name; - struct dso *dso =3D map__dso(map); - - if (dso__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO) - return true; + const struct dso *dso =3D map__dso(map); =20 - /* - * If PERF_RECORD_BPF_EVENT is not included, the dso will not have - * type of DSO_BINARY_TYPE__BPF_PROG_INFO. In such cases, we can - * guess the type based on name. - */ - name =3D dso__short_name(dso); - return name && (strstr(name, "bpf_prog_") =3D=3D name); + return dso && dso__is_bpf_prog(dso); } =20 bool __map__is_bpf_image(const struct map *map) --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244348; b=EIcwZVeq0mX+5YB2LB4e+zNrA0PpdTuMnFZFa5TCWgVsHaspPiRQLayExVSlaJwYkjL/ dk8PfWwDbUNdVU9ASPZp5zYaPodebEkulZP7tVHMO9N/jbhWYE/maxgbSwuRV8BXkSO5H kgBDi02ccwlu8EhTiNl/AsZgqX92CQJiIqlXmNWk0KbbiEDca104LW2zAGZ9oDn7m5vcH oA6KTI/PHs25bPIPesX36JS2kMRWnS2MGCXk/rgUs1y5rvbqE87LCF++YFoRvd5Nafmg8 QblmPI6ZGjxEVtwoneaIBacvsNQMJ2ak7LyDyIok10c1HE4RRcer/G7EFOLppHQB5mQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244348; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=BRO7iFim5TO3kyhnlqJsKmtRLu5OZtTESX0CvoKHMNc=; b=cAtiBE7xrI/1QPyRNOHCBHgoanEYHIxJ75wdhpD6qKDLTdypbkNclXGnWMQsubUcgQng /1JGhln6TDZiekSjQuFmFzo9jrvLV9a1scENjp5HXdrUNuV6e6HAh6n2iaCp0Sv/t/iTF ah1G6NQLEjQJCAp9i8BOGggssZdPPWESVqKYgSqAlfls2ZIrKcg3Vh4tgdgByGxo8cpPd TeBwcubUNvIXy7zM4dFbN4QZQIzlCUA9XXbB64wqDwNWNvZmHj/xNTVfxkhS3v0UUoaSw HTemyPQxUrMiYsVN6YGshFqa5AGFohUgcKS/s2EdrDFDRAuvnpXFLddBwg/9gMEeVgw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pPDepwGG; arc=none smtp.remote-ip=74.125.82.69 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 95C25606A684 for ; Sat, 21 Nov 2026 06:59:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 95C25606A684 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=pPDepwGG Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1b3a348a6dbso3178467c88.1 for ; Fri, 20 Nov 2026 22:59:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244348; x=1795849148; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=BRO7iFim5TO3kyhnlqJsKmtRLu5OZtTESX0CvoKHMNc=; b=pPDepwGGUb23mWjZdGhrVTgo2+/ZHDDXRihHeFaRr0SRAcPbECsVyarsNxdL6jpUFg 9Z8wAHuc+4SEVXzs76OnXW16FPTF5ttbSE2CMgNgOgLFYmnnrHomAKlK5zb2GCJNF28l XAMGEBHIJz5x2YD75AsSbjpTuubEpgHsw55xasZ9amTkt4Hrsyr2x22sDysBgqQiuwao dXSfGCz2kGwX0L/fHX2nvxpHwDGWnMErUtB4t+nwXhLyrXTFQQhCI+d2WdeqtNhP/iKT g6cZ9Al0+pbVmjEa9jaMgJcJgvEq32ax5X9Jwx0TckokOFCgLkgE2NZQ/c+V3kAjfB2v fw2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244348; x=1795849148; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=BRO7iFim5TO3kyhnlqJsKmtRLu5OZtTESX0CvoKHMNc=; b=JUY4SUYnGcSP0bRFRfx+d31EhFSI6gE8ehjmOhdlMFxMNwNV2XVYXClXmXwRDyb2AB ubm9XUt/nwtOCDeUX9NdxW3g9aKpticXn9dWdZ7iTOayyF6wJPuQNiLbgQs2sgDDm1FR tnhGbl6ZMZVaSB38Hj9kw/bUHr6/V4y1QRKxeXopKA2blDgsqhDQvOSEAolBOAiVn/r7 BEiaarQgOXy9v5tMoVNhek9wvRkDc89Rjt/Ny15wS6mcp4IylBAfEFHHoAapND+H0atD kJVwnRlL/+HNKLP2UKWi3SFDkLuwoMHSs0DJQINIwvtkmAUHxxuTq9OHHXYagJL4qJ/g VEsg== X-Forwarded-Encrypted: i=1; AL+hErHVL4UNvevuLzNSglskBDM0GF/sq+/MNYAsDE7tC4DrGhgtdAcZxgpgEuQFy6B/PbNeLAnAs715nvIFaG8=@vger.kernel.org X-Gm-Message-State: APuU4Z/UnW5obJrSuJkUjYGO4L657w/uPeAVV2ID2uqOXZLmQdFm/IfL B9EyZWdEh3HRv8cZT1W6UmdY9PezwgReKfvv26pUUEYQpNsjffcMf2YiOVCaWLqqog7Uj+4EfAK w/yyeH4q/eA== X-Received: from dlam22-n1.prod.google.com ([2002:a05:701b:2096:10b0:1b5:345d:ed30]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7023:b82:b0:163:a197:722d with SMTP id a92af1059eb24-1b5567f6a63mr6449552c88.4.1795244347246; Fri, 20 Nov 2026 22:59:07 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:58 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-10-irogers@google.com> Subject: [PATCH v15 09/23] perf dsos: Ensure DSOs in auxtrace, machine, and symbol are in dsos From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Place the DSOs created in addr_filter__resolve_syms(), machine__process_ksymbol_register(), and maps__split_kallsyms() into a dsos collection so that all DSOs that may have data fds opened or be referenced by maps belong to a dsos collection. Signed-off-by: Ian Rogers --- tools/perf/util/auxtrace.c | 16 +++++++++++++--- tools/perf/util/machine.c | 4 ++++ tools/perf/util/symbol.c | 4 ++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/tools/perf/util/auxtrace.c b/tools/perf/util/auxtrace.c index 8bb491d389a2..1451de019df0 100644 --- a/tools/perf/util/auxtrace.c +++ b/tools/perf/util/auxtrace.c @@ -29,6 +29,7 @@ #include "config.h" #include "evlist.h" #include "dso.h" +#include "dsos.h" #include "map.h" #include "pmu.h" #include "evsel.h" @@ -2684,7 +2685,7 @@ static int addr_filter__resolve_kernel_syms(struct ad= dr_filter *filt) return 0; } =20 -static struct dso *load_dso(const char *name) +static struct dso *load_dso(const char *name, struct dsos *dsos) { struct map *map; struct dso *dso; @@ -2693,6 +2694,11 @@ static struct dso *load_dso(const char *name) if (!map) return NULL; =20 + if (dsos__add(dsos, map__dso(map))) { + map__put(map); + return NULL; + } + if (map__load(map) < 0) pr_err("File '%s' not found or has no symbols.\n", name); =20 @@ -2794,6 +2800,7 @@ static int addr_filter__resolve_syms(struct addr_filt= er *filt) { u64 start, size; struct dso *dso; + struct dsos dsos; int err =3D 0; =20 if (!filt->sym_from && !filt->sym_to) @@ -2802,9 +2809,11 @@ static int addr_filter__resolve_syms(struct addr_fil= ter *filt) if (!filt->filename) return addr_filter__resolve_kernel_syms(filt); =20 - dso =3D load_dso(filt->filename); + dsos__init(&dsos); + dso =3D load_dso(filt->filename, &dsos); if (!dso) { pr_err("Failed to load symbols from: %s\n", filt->filename); + dsos__exit(&dsos); return -EINVAL; } =20 @@ -2831,13 +2840,14 @@ static int addr_filter__resolve_syms(struct addr_fi= lter *filt) =20 err =3D check_end_after_start(filt, start, size); if (err) - return err; + goto put_dso; =20 filt->size =3D start + size - filt->addr; } =20 put_dso: dso__put(dso); + dsos__exit(&dsos); =20 return err; } diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 4a3461d79593..02711995f0b6 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -797,6 +797,10 @@ static int machine__process_ksymbol_register(struct ma= chine *machine, dso__set_long_name(dso, "", false); } =20 + err =3D dsos__add(&machine->dsos, dso); + if (err) + goto out; + map__set_start(map, event->ksymbol.addr); map__set_end(map, map__start(map) + event->ksymbol.len); err =3D maps__fixup_overlap_and_insert(machine__kernel_maps(machine), ma= p); diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index a1ac1cd1d2e7..8dded88c9763 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1078,6 +1078,10 @@ static int maps__split_kallsyms(struct maps *kmaps, = struct dso *dso, u64 delta, dso__set_symtab_type(ndso, DSO_BINARY_TYPE__KALLSYMS); } dso__set_loaded(ndso); + if (dsos__add(&machine->dsos, ndso)) { + dso__put(ndso); + return -1; + } =20 curr_map =3D map__new2(pos->start, ndso); if (curr_map =3D=3D NULL) { --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244351; b=jYO4JPaNc/zfjhUKJui8QOaw05XHnCrKpNPudWXpUfHerB24cymaPkL0C8akimNSLtcR D1q26+1a3DRdEqSMVPY2EjyqTWQrMZF7cBE9+v0a5PRuHqB7/TFaFbnrY1lqR4QChhpah jt/1pqqZtVLGmueeaWxE3GJnq2SdWAok1z2+fK0GW+PvnadMlO0eedSv82P+ojrGfV6H/ XfXC4fsFI+t1Jf5R7GjN9O57EIpFEyUOOk0eYUDzlHLsNtJicPNPTdJM7qJSzJTBSl8K2 uGEc3pC62IQGIiW/rfIw1XxouYHbznhqq3Ohmb5YsljeNpC4QsHEvhBGiwIfircoRCQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244351; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=P9WzhWBRrWQrIcjhCHBoP+WxUKv4PUHF+mxNCkzIjdk=; b=jXAMjxcT10EFXqpkLa4L83O6F5vQYPYOfzlWRrGRxa+2yp4kHX7/gW5O9fS79ED89xD4 OCzEF8mhdNJawPUkW+DpetlXb/NuHU03euzStVZHYl/ZWFf7pR/ZtYXMHc+V2YrKQDARS 6zn/ybnDcgGlJGytT1HZcb1Vn9ZTK9MnOcncCHEI4k+Rf8tc2MJiiSoTrZIfH13YVSOZX o+9jCJPekymtuZVCrApVGNkvwKJGmGYyipgnDdJUrewWbEDP/jpQIaCv5QN7XFa+lrVCU PJxH00zc5/wmridyLeAL515tanRAUr/Qft5OuiwydC6xKdWi/daFugIu5Ejd6e+OROw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=JMFaAJ9U; arc=none smtp.remote-ip=74.125.82.71 Received: from mail-dl1-f71.google.com (mail-dl1-f71.google.com [74.125.82.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ED2F606A33E for ; Sat, 21 Nov 2026 06:59:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9ED2F606A33E 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=JMFaAJ9U Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-139b62317d0so4490240c88.0 for ; Fri, 20 Nov 2026 22:59:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244350; x=1795849150; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=P9WzhWBRrWQrIcjhCHBoP+WxUKv4PUHF+mxNCkzIjdk=; b=JMFaAJ9UDdrFu7kK3ifn7SvkIur/tvz54q/5r7LPgFg30ME+IRMKlljUS6KRFlkIWm uchbq1xK0IUMiEVjvvColqrHuhnymmR0wNrBzpGkiwg7iWWXGayH+2rdrhDtiLwmw/Yf MwOwjbhbSIzAF6fVQAlZyHNHFe2wihb+lV2lxw4NV6x0dIHGbz/Cw7PTEKip7piqScv4 OkV58ZQHyEepmgQrPqLf7SxjcHxZnl4a4zbRg8qu9vPlRYWJ9AmuYNtL7tr+ugCMV55u OblrAoU+eowRLb5kAxEa38bK82CuahvDBnxU7QZppQ/HqRlzmRdbJ8LpX4aBzvxw1r7m 3W/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244350; x=1795849150; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=P9WzhWBRrWQrIcjhCHBoP+WxUKv4PUHF+mxNCkzIjdk=; b=djraRIkRRH98nPJO8M+nagPvqsgoSqmMugxVWllWyMZ2oizfpl+lwJ/VbBkCAlb1rV dQXTJDJGZAuKrEooqjPK/dpQoHPyFMfl/VK8lIBCpBAypCbF/U8oOrih0vvKUpC6FdUa ePt2cwaKHqBgUJ8tdSUZdroS79TQlLL85VSmQR3z9q18pbxVt9Qwo66e+uA6wdJKv5ow SmziL5ofbJcPVrRRztCDEWSryXvF5fFg+QcRJv6R49vZuK0otfE+R4yzl+Gj3HUi2kmz 93T9KmJXGNN+iXo6qt4ZO7Z3b2cxVOFI6rRYVmyq79v9pBDZUAApFAFb8Z0hTK75c71F Twcw== X-Forwarded-Encrypted: i=1; AL+hErFi0ilN+qmX2LUEqD4gXy3Aeq+Gi3/OdGf29fBfze99hA66CzQW9DEiOCKk3h4gNoiPAOfpRWZ1toHFAks=@vger.kernel.org X-Gm-Message-State: APuU4Z+LBajfxIAEPG4vwkM47rbzMglksq/uJ5sApyG18c8/fdz2nWoh fYOvFm1Dw1c5rYKNnbWaaexkLGf5KyNUnRMlWgtL2GEEhkboONfHy48l8tKf3ZCj1rOii+K94z/ oV6F1tRk+JA== X-Received: from dlx1.prod.google.com ([2002:a05:7022:81:b0:1a7:450c:8b47]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4502:10b0:1b2:539b:496c with SMTP id a92af1059eb24-1b5569050c8mr5961784c88.35.1795244349476; Fri, 20 Nov 2026 22:59:09 -0800 (PST) Date: Fri, 20 Nov 2026 22:57:59 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-11-irogers@google.com> Subject: [PATCH v15 10/23] perf dso: Pass dso_data to close_data_fd() and dso__list_del() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" close_data_fd() and dso__list_del() only operate on struct dso_data fields, while close_first_dso() retrieves a struct dso_data * from dso__data_open. Pass struct dso_data * directly to close_data_fd() and dso__list_del() so close_first_dso() does not need to recover a struct dso * from struct dso_data *. This removes the REFCNT_CHECKING dso pointer from struct dso_data and avoids dropping _dso__data_open_lock in dso__list_del() to call dso__put(), preventing lock-order inversions and races during EMFILE eviction. Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 44 ++++++++++++------------------------------- tools/perf/util/dso.h | 3 --- 2 files changed, 12 insertions(+), 35 deletions(-) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 3975b6c935a0..3d0ed7b0be54 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include "dso.h" =20 +#include #include #include =20 @@ -544,22 +545,14 @@ static struct mutex *dso__data_open_lock(void) __retu= rns_ctx_lock(&_dso__data_op static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lo= ck) { list_add_tail(&dso__data(dso)->open_entry, &dso__data_open); -#ifdef REFCNT_CHECKING - dso__data(dso)->dso =3D dso__get(dso); -#endif - /* Assume the dso is part of dsos, hence the optional reference count abo= ve. */ + /* Assume the dso is part of dsos, holding it live while on dso__data_ope= n. */ assert(dso__dsos(dso)); dso__data_open_cnt++; } =20 -static void dso__list_del(struct dso *dso) __must_hold(&_dso__data_open_lo= ck) +static void dso__list_del(struct dso_data *dso_data) __must_hold(&_dso__da= ta_open_lock) { - list_del_init(&dso__data(dso)->open_entry); -#ifdef REFCNT_CHECKING - mutex_unlock(dso__data_open_lock()); - dso__put(dso__data(dso)->dso); - mutex_lock(dso__data_open_lock()); -#endif + list_del_init(&dso_data->open_entry); WARN_ONCE(dso__data_open_cnt <=3D 0, "DSO data fd counter out of bounds."); dso__data_open_cnt--; @@ -729,13 +722,13 @@ static int open_dso(struct dso *dso, struct machine *= machine) return fd; } =20 -static void close_data_fd(struct dso *dso) __must_hold(&_dso__data_open_lo= ck) +static void close_data_fd(struct dso_data *dso_data) __must_hold(&_dso__da= ta_open_lock) { - if (dso__data(dso)->fd >=3D 0) { - close(dso__data(dso)->fd); - dso__data(dso)->fd =3D -1; - dso__data(dso)->file_size =3D 0; - dso__list_del(dso); + if (dso_data->fd >=3D 0) { + close(dso_data->fd); + dso_data->fd =3D -1; + dso_data->file_size =3D 0; + dso__list_del(dso_data); } } =20 @@ -748,21 +741,15 @@ static void close_data_fd(struct dso *dso) __must_hol= d(&_dso__data_open_lock) */ static void close_dso(struct dso *dso) __must_hold(&_dso__data_open_lock) { - close_data_fd(dso); + close_data_fd(dso__data(dso)); } =20 static void close_first_dso(void) __must_hold(&_dso__data_open_lock) { struct dso_data *dso_data; - struct dso *dso; =20 dso_data =3D list_first_entry(&dso__data_open, struct dso_data, open_entr= y); -#ifdef REFCNT_CHECKING - dso =3D dso_data->dso; -#else - dso =3D container_of(dso_data, struct dso, data); -#endif - close_dso(dso); + close_data_fd(dso_data); } =20 static rlim_t get_fd_limit(void) @@ -1858,9 +1845,6 @@ struct dso *dso__new_id(const char *name, const struc= t dso_id *id) data->fd =3D -1; data->status =3D DSO_DATA_STATUS_UNKNOWN; INIT_LIST_HEAD(&data->open_entry); -#ifdef REFCNT_CHECKING - data->dso =3D NULL; /* Set when on the open_entry list. */ -#endif } return res; } @@ -1918,10 +1902,6 @@ struct dso *dso__get(struct dso *dso) =20 void dso__put(struct dso *dso) { -#ifdef REFCNT_CHECKING - if (dso && dso__data(dso) && refcount_read(&RC_CHK_ACCESS(dso)->refcnt) = =3D=3D 2) - dso__data_close(dso); -#endif if (dso && refcount_dec_and_test(&RC_CHK_ACCESS(dso)->refcnt)) dso__delete(dso); else diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 009bd3001ace..a5e8d195f129 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -261,9 +261,6 @@ struct dso_cache { struct dso_data { struct rb_root cache; struct list_head open_entry; -#ifdef REFCNT_CHECKING - struct dso *dso; -#endif int fd; int status; u32 status_seen; --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244353; b=5Izz0OgBqhUIxe3d9wLvPk9hiaELiydZyaHri5YXjJlWTBm5/HCOMtB6WqXtpkdvWVo4 kAG6A5cp+C7on/VX14Iy4GD2WbHaMpOkpJcEqoSFjsomFbBhmfgVXkBPKtFv87rep4K5e mQ628r10O9DF6IOJ/3YskvqRuppOuO/i/xNyRxTN7E7ZISShuTSA0jInP/q3j5zRadCXU WQgzRw0gkw1B1u1SBqjRiEKA2Kv+aQAfqx6BsmtqaTcCdCQzv2QbZtiRWZR7OoSNfhskt yQDjDX43myr7Xsk6z/pZdhDfrZNNZuOX2Mr7ntHZE7434hAcivSonKE2M2O80yPpgQw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244353; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=voPjIsol10nJNu0QVACVtD+Q0GVPTfnVd5GC9/XqYkM=; b=2DXbAXXJbpkhsThaADs+YekH0Db+/Fv68BPN1fiO+Mef2syM3JG+suQTkXSgeMRIA/JF o8mSieUoTcxV21VLhCNRfc63pjKXBnodY4xL0o/qgYZ4vlhIpuK1D2NSF41pvdKQk4eo6 JJitvrme5P9xkU6ezuD7cM0HyJvCbs1RFAVJPsTTmGPQ6bwCaSPKi8KAHEPuW9nBw1ybD R2Vfnnk3wmDRAav/NO5GAVgWKXHUZDYs2c1bKiZL7A82GQRYXYcicyUXRYXIr6vSWUSfO tt5vfkNaVyijpbLRjzYjv1OuaemYCt98GV2e9silixlBD3xiSJXNVSe8Z4oqeqxdrgg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=lgkMjJCT; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 144CA606A337 for ; Sat, 21 Nov 2026 06:59:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 144CA606A337 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=lgkMjJCT Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-383ec96a7deso1866885eec.1 for ; Fri, 20 Nov 2026 22:59:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244352; x=1795849152; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=voPjIsol10nJNu0QVACVtD+Q0GVPTfnVd5GC9/XqYkM=; b=lgkMjJCT2FlK7zDFa8VIEo4X4/hY3uoiJynuRTSIWg6DAcBHORgRsOIrEqPY8CrKdv fJPugY2RqYks2pNQn/9Spp+uEvWL3Wzjn+GFnsJMr35t6xjAPxAqqC0kbqfMXR0iQpbK qRTQjepxydvS0WjYznbrUZ7QHM9uBTcWINbxF605zfB+XxSfkRtzUdNNg4XUvjTiGv2t bpSvsMTxcvWaibpfLi3Qk2JqqzmioXCHjrSzWjbWUgrL2MmNv4z8U/Irlk5mQTyeqGPD 8XId+vpBL1mh6nzKZaktMTk5wgtY71uMk2H+/hx9scMAWQ7VIZE3jgJyVNTaDHeIpGKK K1yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244352; x=1795849152; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=voPjIsol10nJNu0QVACVtD+Q0GVPTfnVd5GC9/XqYkM=; b=cF+1Aq7yQw90pBv0KugZTp7zdPB1C/eP3sG3jFnahMc4ymZsZyeoaNJgrahmoo0sWH CoNzqP43dnWkEoBlwkwm25qUfNPfKdUTFoaR3b1A166dYxblxHZG0E16q9rm48dgPXxN gSjQrC/ZqyU8N1HICxJWhvhAEhBFvw8Ld5DD8JGLPY+X6RL1RmvQLD5I/Yal3rR4MuNN 5HYO5S8wPQnNFc+u5i2wq3ioIb4mAoo3u5cuP6vJefs2xaaSpmkqrVt1onbLsLGhnXlq pDRSt2DDOxuR5ZDLRSMUkrL6O+Ogau9USSOt7ZegFfK0NYSj0Aye7DORDy+DEnmrl7yQ b+wQ== X-Forwarded-Encrypted: i=1; AL+hErHkAiOj7q8PVnZPTQrMtFwN3L6t1enCQCOZx0fhFoDNB3QFoajJwiZs+Tdta3AATgfBNyNlaCG6xh68jwM=@vger.kernel.org X-Gm-Message-State: APuU4Z+XWrpUC7nzzpH5V8ONLE4E+R/8Uo2pwweixVYTbaEb/lJnvH/7 dlDouKatuVrczCuG7cpSdIj7TdukzTmvMuiJqiS86fFM+7LyHzY2SKE3gj00gfoFXADHJ4G/cGv w+/kehlJxKg== X-Received: from dyz23.prod.google.com ([2002:a05:693c:4097:b0:36a:ead:4dcc]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:e9ce:20b0:36d:5102:9974 with SMTP id 5a478bee46e88-3856c2c2123mr6644587eec.2.1795244351757; Fri, 20 Nov 2026 22:59:11 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:00 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-12-irogers@google.com> Subject: [PATCH v15 11/23] perf dso: Use DEFINE_MUTEX for dso__data_open_lock From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace _dso__data_open_lock and its pthread_once() initialization wrapper dso__data_open_lock() with DEFINE_MUTEX(dso__data_open_lock). Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 68 +++++++++++++++++-------------------------- tools/perf/util/dso.h | 10 +++---- 2 files changed, 32 insertions(+), 46 deletions(-) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 3d0ed7b0be54..5be5d224b246 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -524,25 +524,11 @@ void dso__set_module_info(struct dso *dso, struct kmo= d_path *m, /* * Global list of open DSOs and the counter. */ -struct mutex _dso__data_open_lock; +DEFINE_MUTEX(dso__data_open_lock); static LIST_HEAD(dso__data_open); -static long dso__data_open_cnt __guarded_by(&_dso__data_open_lock); +static long dso__data_open_cnt __guarded_by(&dso__data_open_lock); =20 -static void dso__data_open_lock_init(void) -{ - mutex_init(&_dso__data_open_lock); -} - -static struct mutex *dso__data_open_lock(void) __returns_ctx_lock(&_dso__d= ata_open_lock) -{ - static pthread_once_t data_open_lock_once =3D PTHREAD_ONCE_INIT; - - pthread_once(&data_open_lock_once, dso__data_open_lock_init); - - return &_dso__data_open_lock; -} - -static void dso__list_add(struct dso *dso) __must_hold(&_dso__data_open_lo= ck) +static void dso__list_add(struct dso *dso) __must_hold(&dso__data_open_loc= k) { list_add_tail(&dso__data(dso)->open_entry, &dso__data_open); /* Assume the dso is part of dsos, holding it live while on dso__data_ope= n. */ @@ -550,7 +536,7 @@ static void dso__list_add(struct dso *dso) __must_hold(= &_dso__data_open_lock) dso__data_open_cnt++; } =20 -static void dso__list_del(struct dso_data *dso_data) __must_hold(&_dso__da= ta_open_lock) +static void dso__list_del(struct dso_data *dso_data) __must_hold(&dso__dat= a_open_lock) { list_del_init(&dso_data->open_entry); WARN_ONCE(dso__data_open_cnt <=3D 0, @@ -560,7 +546,7 @@ static void dso__list_del(struct dso_data *dso_data) __= must_hold(&_dso__data_ope =20 static void close_first_dso(void); =20 -static int do_open(char *name) __must_hold(&_dso__data_open_lock) +static int do_open(char *name) __must_hold(&dso__data_open_lock) { do { int fd =3D open(name, O_RDONLY|O_CLOEXEC); @@ -663,7 +649,7 @@ void dso__put_filename(struct dso *dso __maybe_unused, = char *filename, bool deco } =20 static int __open_dso(struct dso *dso, struct machine *machine) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) { int fd =3D -EINVAL; char *name; @@ -696,7 +682,7 @@ static void check_data_close(void); * list/count of open DSO objects. */ static int open_dso(struct dso *dso, struct machine *machine) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) { int fd; struct nscookie nsc; @@ -722,7 +708,7 @@ static int open_dso(struct dso *dso, struct machine *ma= chine) return fd; } =20 -static void close_data_fd(struct dso_data *dso_data) __must_hold(&_dso__da= ta_open_lock) +static void close_data_fd(struct dso_data *dso_data) __must_hold(&dso__dat= a_open_lock) { if (dso_data->fd >=3D 0) { close(dso_data->fd); @@ -739,12 +725,12 @@ static void close_data_fd(struct dso_data *dso_data) = __must_hold(&_dso__data_ope * Close @dso's data file descriptor and updates * list/count of open DSO objects. */ -static void close_dso(struct dso *dso) __must_hold(&_dso__data_open_lock) +static void close_dso(struct dso *dso) __must_hold(&dso__data_open_lock) { close_data_fd(dso__data(dso)); } =20 -static void close_first_dso(void) __must_hold(&_dso__data_open_lock) +static void close_first_dso(void) __must_hold(&dso__data_open_lock) { struct dso_data *dso_data; =20 @@ -783,7 +769,7 @@ void reset_fd_limit(void) fd_limit =3D 0; } =20 -static bool may_cache_fd(void) __must_hold(&_dso__data_open_lock) +static bool may_cache_fd(void) __must_hold(&dso__data_open_lock) { if (!fd_limit) fd_limit =3D get_fd_limit(); @@ -799,7 +785,7 @@ static bool may_cache_fd(void) __must_hold(&_dso__data_= open_lock) * for opened dso file descriptors. The limit is half * of the RLIMIT_NOFILE files opened. */ -static void check_data_close(void) __must_hold(&_dso__data_open_lock) +static void check_data_close(void) __must_hold(&dso__data_open_lock) { bool cache_fd =3D may_cache_fd(); =20 @@ -815,9 +801,9 @@ static void check_data_close(void) __must_hold(&_dso__d= ata_open_lock) */ void dso__data_close(struct dso *dso) { - mutex_lock(dso__data_open_lock()); + mutex_lock(&dso__data_open_lock); close_dso(dso); - mutex_unlock(dso__data_open_lock()); + mutex_unlock(&dso__data_open_lock); } =20 static int dso__find_binary_type_locked(struct dso *dso) @@ -913,7 +899,7 @@ static int dso__find_binary_type_locked(struct dso *dso) =20 #define FIND_MAX_RETRY 3 static int __dso__find_binary_type(struct dso *dso) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) { int ret; int retry =3D 0; @@ -945,15 +931,15 @@ enum dso_binary_type dso__find_binary_type(struct dso= *dso) { int ret; =20 - mutex_lock(dso__data_open_lock()); + mutex_lock(&dso__data_open_lock); ret =3D __dso__find_binary_type(dso); - mutex_unlock(dso__data_open_lock()); + mutex_unlock(&dso__data_open_lock); =20 return ret; } =20 static void try_to_open_dso(struct dso *dso, struct machine *machine) - __must_hold(&_dso__data_open_lock) + __must_hold(&dso__data_open_lock) { struct dso_data *dso_data =3D dso__data(dso); =20 @@ -984,7 +970,7 @@ bool dso__data_get_fd(struct dso *dso, struct machine *= machine, int *fd) if (dso__data(dso)->status =3D=3D DSO_DATA_STATUS_ERROR) return false; =20 - mutex_lock(dso__data_open_lock()); + mutex_lock(&dso__data_open_lock); =20 try_to_open_dso(dso, machine); =20 @@ -992,13 +978,13 @@ bool dso__data_get_fd(struct dso *dso, struct machine= *machine, int *fd) if (*fd >=3D 0) return true; =20 - mutex_unlock(dso__data_open_lock()); + mutex_unlock(&dso__data_open_lock); return false; } =20 void dso__data_put_fd(struct dso *dso __maybe_unused) { - mutex_unlock(dso__data_open_lock()); + mutex_unlock(&dso__data_open_lock); } =20 bool dso__data_status_seen(struct dso *dso, enum dso_data_status_seen by) @@ -1169,7 +1155,7 @@ static ssize_t file_read(struct dso *dso, struct mach= ine *machine, { ssize_t ret; =20 - mutex_lock(dso__data_open_lock()); + mutex_lock(&dso__data_open_lock); =20 /* * dso__data(dso)->fd might be closed if other thread opened another @@ -1185,7 +1171,7 @@ static ssize_t file_read(struct dso *dso, struct mach= ine *machine, =20 ret =3D pread(dso__data(dso)->fd, data, DSO__DATA_CACHE_SIZE, offset); out: - mutex_unlock(dso__data_open_lock()); + mutex_unlock(&dso__data_open_lock); return ret; } =20 @@ -1292,7 +1278,7 @@ static int file_size(struct dso *dso, struct machine = *machine) int ret =3D 0; struct stat st; =20 - mutex_lock(dso__data_open_lock()); + mutex_lock(&dso__data_open_lock); =20 /* * dso__data(dso)->fd might be closed if other thread opened another @@ -1315,7 +1301,7 @@ static int file_size(struct dso *dso, struct machine = *machine) dso__data(dso)->file_size =3D st.st_size; =20 out: - mutex_unlock(dso__data_open_lock()); + mutex_unlock(&dso__data_open_lock); return ret; } =20 @@ -1520,7 +1506,7 @@ uint16_t dso__e_machine_endian(struct dso *dso, struc= t machine *machine, uint32_ return EM_NONE; } =20 - mutex_lock(dso__data_open_lock()); + mutex_lock(&dso__data_open_lock); =20 /* * dso__data(dso)->fd might be closed if other thread opened another @@ -1533,7 +1519,7 @@ uint16_t dso__e_machine_endian(struct dso *dso, struc= t machine *machine, uint32_ else if (e_flags) *e_flags =3D 0; =20 - mutex_unlock(dso__data_open_lock()); + mutex_unlock(&dso__data_open_lock); return e_machine; } =20 diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index a5e8d195f129..e8cd13c101a6 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -354,7 +354,7 @@ DECLARE_RC_STRUCT(dso) { char name[]; }; =20 -extern struct mutex _dso__data_open_lock; +extern struct mutex dso__data_open_lock; extern const struct dso_id dso_id_empty; =20 int dso_id__cmp(const struct dso_id *a, const struct dso_id *b); @@ -807,7 +807,7 @@ void __dso__improve_id(struct dso *dso, const struct ds= o_id *id); int dso__name_len(const struct dso *dso); =20 struct dso *dso__get(struct dso *dso); -void dso__put(struct dso *dso) __must_not_hold(&_dso__data_open_lock); +void dso__put(struct dso *dso) __must_not_hold(&dso__data_open_lock); =20 static inline void __dso__zput(struct dso **dso) { @@ -917,9 +917,9 @@ void dso__set_module_info(struct dso *dso, struct kmod_= path *m, * TODO */ bool dso__data_get_fd(struct dso *dso, struct machine *machine, int *fd) - __cond_acquires(true, &_dso__data_open_lock); -void dso__data_put_fd(struct dso *dso) __releases(&_dso__data_open_lock); -void dso__data_close(struct dso *dso) __must_not_hold(&_dso__data_open_loc= k); + __cond_acquires(true, &dso__data_open_lock); +void dso__data_put_fd(struct dso *dso) __releases(&dso__data_open_lock); +void dso__data_close(struct dso *dso) __must_not_hold(&dso__data_open_lock= ); =20 int dso__data_file_size(struct dso *dso, struct machine *machine); off_t dso__data_size(struct dso *dso, struct machine *machine); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.216.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244355; b=GRdj71UZ8EWpqgtxvv2K5S8krA0bFxgW4ceMyui9tjdbCRpiShcJx5R5aEODHZy/VCQz sl2TAI7Je556zm6ywsjzE4xGdtUoeKINS5vKL2m1RDDAO6NKDiK8uQJN5NULHdkuHNmgg oXqnZ9kzpvU+UVb3lBkRUyLHtOwHA9eR4SvQRwILETPvA/vnbOjWLTcoqDy3lPiiVSSww FFox4rwBWhjhNL5ol3x/rOcaK5ulrlsjYvwJ4v/o5N1FeendRSvNb/1v8PdHdw8crShEe fsEFXDEzAGARZ4mLnVOGWn9kuU7BXb+JxyswfZVt0M8qj/dea4LCqzLGsRMRSA+22tg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244355; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=eypqNFLgLpXUHRcqXnjliravXE92Pjb7dAJMkh1LWac=; b=JCpXEiRR+F/ZkpHWSNBl4VhGURjnoNC5c4t2e9HlEqzDOvzl07kYPkpcSy/khGaDmRG4 +OGyA+pI8C4Ig8fn8p0tr3+/Rvj0RxoSqFbAwJ6gFuX2q5MsZC0b3fE3f36+16flCOXfF GUcRT3fyIW1xTiNBm1rlzAXmnOva6RBIu/zDosyhrk5mp/07clp5TayfTtpXjjRxg8eQ1 ORxJeXTZQLJL85i7QHgX+zMcueEgQazXKoE+MdC3cN4+KsfTnCGiaGBXSrWNcdrIp89VY PZhIQTrAmE0oruWp2M7bry+ItqPc7VcjrdIYkTqeC9eTTUqqMSvpMKUsmcb0ZKi2ohw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=dCYimgCo; arc=none smtp.remote-ip=209.85.216.71 Received: from mail-pj1-f71.google.com (mail-pj1-f71.google.com [209.85.216.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 122B8606A682 for ; Sat, 21 Nov 2026 06:59:14 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 122B8606A682 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=dCYimgCo Received: by mail-pj1-f71.google.com with SMTP id 98e67ed59e1d1-3b427e5a37dso1198620a91.2 for ; Fri, 20 Nov 2026 22:59:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244354; x=1795849154; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=eypqNFLgLpXUHRcqXnjliravXE92Pjb7dAJMkh1LWac=; b=dCYimgCoyuiyDHRGkwM6x9RkgC17CbJLvAC6uxqbGEjDMPHn6Z8IfZg71tjuLPYCfY hKrc+gfR+UuycuntKOrMy6LuHzmN9AiQVDIfbQjRXAR4BvvYfQpcOA5qiGx91sMZbvSZ mU/ecwp4U+ha13CoQYObaxIzuaX36p7BKtYpQQGbPZ3qvLAFjOca+YK5oNuGC2jw3Oyw dKEIgNIjXdX7oep5I0o4u2X0oOIW8dB3QUfYY9ceUS/qS++Hlsz0+no8TkxDTua0cwrF 5Pv1/QD0AC5k9oXsMiSVfcuEK6TDAqa542+jIJu5Bqzbi7QUsaHXGit87JJHICAM2pdB bKzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244354; x=1795849154; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=eypqNFLgLpXUHRcqXnjliravXE92Pjb7dAJMkh1LWac=; b=b5neSEl3lcPMqL+sM3oG6EFpFfI9t55DMraFeVhZ6bQz2onb5xnTMeAqU+01m9t1q5 ypReLS9LbSfdANZvotSZhgTpBbQ35GBQQz2m4OUoDU4PAIsjiB8BclTHLr0orChKtE8u WSws1kyJqrUYPtyToDvdK+UF7ksC9onF0klspvCSNPiQ1+1+FuuoIKPTnp37O1j1EIFv pRfk2IwWk08sAQQrS8PuEi5ddmOLS3Zizo3x8feCgP7h5MlJd/ChYQz5H57Kmmz40eBy wQlCOqPxZAaQ77eJnNg/DuOMjJUaWUHSv8yyKlgBlDK5+DpLNUADtdgCLELQJxuD9UwB si3w== X-Forwarded-Encrypted: i=1; AL+hErGjhw7wj0kadqDJKZVZiZxvQVki4v9nxHt+bNlmBTi2j62Is0AQguIjQFq3PArDF/lQHviTIbae7nfj8jk=@vger.kernel.org X-Gm-Message-State: APuU4Z8RVYtK+PJHug9MQwiAfpm//S6vn6xJBgtAwD5sAPZ5hnAVkrgN XnzeyLwW6uli7wPpHsUJwbo5c4MlEyi6DKpoR/0N7M0tUnY0xLa8W+FwjwP1unt89XQvNVTB9qm c1+IkokQeGg== X-Received: from pjbrm1.prod.google.com ([2002:a17:90b:3ec1:b0:3b3:f9ba:d0b5]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:4483:b0:3ac:bc3d:6fe8 with SMTP id 98e67ed59e1d1-3b41c92f8f9mr4652569a91.34.1795244354221; Fri, 20 Nov 2026 22:59:14 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:01 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-13-irogers@google.com> Subject: [PATCH v15 12/23] perf dsos: Use read lock fast path in dsos__findnew_id() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In dsos__findnew_id(), try finding the DSO under the read lock first and only acquire the write lock when a new DSO must be inserted or an existing DSO needs its ID updated (checked via dso__needs_improve_id()). Prioritize build IDs over inode fields in dso__needs_improve_id() and __dso__improve_id() so an existing DSO with a defined build ID is not updated with inode metadata. Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 45 +++++++++++++++++++++++++++++------------- tools/perf/util/dso.h | 1 + tools/perf/util/dsos.c | 10 ++++++++++ 3 files changed, 42 insertions(+), 14 deletions(-) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 5be5d224b246..1c938d1b11e9 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1685,6 +1685,20 @@ const struct dso_id dso_id_empty =3D { } }; =20 +bool dso__needs_improve_id(const struct dso *dso, const struct dso_id *id) +{ + const struct dso_id *dso_id =3D dso__id_const(dso); + + if (!id || build_id__is_defined(&dso_id->build_id)) + return false; + + if (build_id__is_defined(&id->build_id)) + return true; + + return (id->mmap2_valid && !dso_id->mmap2_valid) || + (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_v= alid); +} + void __dso__improve_id(struct dso *dso, const struct dso_id *id) { struct dsos *dsos =3D dso__dsos(dso); @@ -1692,22 +1706,25 @@ void __dso__improve_id(struct dso *dso, const struc= t dso_id *id) bool changed =3D false; =20 /* dsos write lock held by caller. */ + if (!id || build_id__is_defined(&dso_id->build_id)) + return; =20 - if (id->mmap2_valid && !dso_id->mmap2_valid) { - dso_id->maj =3D id->maj; - dso_id->min =3D id->min; - dso_id->ino =3D id->ino; - dso_id->mmap2_valid =3D true; - changed =3D true; - } - if (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_valid= ) { - dso_id->ino_generation =3D id->ino_generation; - dso_id->mmap2_ino_generation_valid =3D true; - changed =3D true; - } - if (build_id__is_defined(&id->build_id) && !build_id__is_defined(&dso_id-= >build_id)) { + if (build_id__is_defined(&id->build_id)) { dso_id->build_id =3D id->build_id; changed =3D true; + } else { + if (id->mmap2_valid && !dso_id->mmap2_valid) { + dso_id->maj =3D id->maj; + dso_id->min =3D id->min; + dso_id->ino =3D id->ino; + dso_id->mmap2_valid =3D true; + changed =3D true; + } + if (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_vali= d) { + dso_id->ino_generation =3D id->ino_generation; + dso_id->mmap2_ino_generation_valid =3D true; + changed =3D true; + } } if (changed && dsos) dsos->sorted =3D false; @@ -1715,7 +1732,7 @@ void __dso__improve_id(struct dso *dso, const struct = dso_id *id) =20 int dso_id__cmp(const struct dso_id *a, const struct dso_id *b) { - if (a =3D=3D &dso_id_empty || b =3D=3D &dso_id_empty) { + if (!a || !b || a =3D=3D &dso_id_empty || b =3D=3D &dso_id_empty) { /* There is no valid data to compare so the comparison always returns id= entical. */ return 0; } diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index e8cd13c101a6..bb887d10a9ee 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -802,6 +802,7 @@ void dso__delete(struct dso *dso); int dso__cmp_id(struct dso *a, struct dso *b); void dso__set_short_name(struct dso *dso, const char *name, bool name_allo= cated); void dso__set_long_name(struct dso *dso, const char *name, bool name_alloc= ated); +bool dso__needs_improve_id(const struct dso *dso, const struct dso_id *id); void __dso__improve_id(struct dso *dso, const struct dso_id *id); =20 int dso__name_len(const struct dso *dso); diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index a7f6a98acbaf..4ff38cb5e2cb 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -349,6 +349,16 @@ static struct dso *__dsos__findnew_id(struct dsos *dso= s, const char *name, const struct dso *dsos__findnew_id(struct dsos *dsos, const char *name, const st= ruct dso_id *id) { struct dso *dso; + + down_read(&dsos->lock); + dso =3D __dsos__find_id(dsos, name, id, false, /*write_locked=3D*/false); + if (dso && !dso__needs_improve_id(dso, id)) { + up_read(&dsos->lock); + return dso; + } + up_read(&dsos->lock); + dso__put(dso); + down_write(&dsos->lock); dso =3D __dsos__findnew_id(dsos, name, id); up_write(&dsos->lock); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244359; b=pyd56J0IF/NFN8yr6U/2GunuxKN57zNh1DnG80aTjJt9wxAx96GOoc2U32v/Ho1W5Xsy OghQcOkmbTnyNnOoIEjxT1QEX/0sId+WYoKS2USpVk2lVdmumX8gHgGBd+K/lg6S8h5Ku WgENxGdFN1jiYPRFkjuMd/i/JI22FyTvsGWaS+FJU9gpqVHdaSE4e3JgB112Znc1VB6Xh uwoTtu3vE2/NxwxB6GszFU75S7STZEgH4n7MB6mQCq/bSJjhOq6BlOcRPP1Om9rSeurOd rWbns36S9VUa0kPD3ubfjs3h3ma1HaIi3X5gCpru5fkWJ3ui2CL1q7IPcY3aqzWRnkw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244359; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=aVB8Y+p+SSfHh9qUhyaF69ZnIILGyQybiQn1OGrqgeA=; b=lABha3zt7vmFirDTEzyhbUVo35SrmTbwVUqhQpv9K7Dp1tqWlrNUmem+uVmW7ZdFSZzA P3NZ+NuiJZd7rzdQfo4IGPx/i7uH53u9NvQm7RIbd2G2N46ISgfLct4QTBEQH5EogugJI rvQj3e/g76j4FVibIbgw85c851K+3XKKjK5kqBKbihYxgKKib0Uz6WZSQrsp2Z+0b6RSF LvGv4zmX0hhZdLLfdczFW/XPjFsd7D/uxGRiiW00CY5V2uNhQY1nzuz3rMPlz4cV1ZOTd 7gbcnC8ZY62iAgue8dE9Vrcrb4+igTqhQtoOvx5idJmG1ihXIwwXl1pRmRMxf/Z8Oiw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b="bcz/Hiqv"; arc=none smtp.remote-ip=209.85.160.69 Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DEA8606A332 for ; Sat, 21 Nov 2026 06:59:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3DEA8606A332 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=bcz/Hiqv Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-45e6091d8a1so1338333fac.1 for ; Fri, 20 Nov 2026 22:59:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244357; x=1795849157; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=aVB8Y+p+SSfHh9qUhyaF69ZnIILGyQybiQn1OGrqgeA=; b=bcz/Hiqvau/FgiI1iUoF4h2WuewcMNku0Lw8lw0801v3iwqnMJHDykyGuFAsJ6bSGu 4DoLXYnhQTBaihx2J6NOoxHeXlM34Ex1A02LfSeFXHJ9V2A3DcuDt2zaa14XvxdUBeXh RuutfG6xHqzC2MxQ0pifuS6IJQW8UXaLNQk6+zFFWDj2uts4bpovVmqAOvWZtdZrBViS 5Zmwq29iMcTbhjLnZvn1f+3dNEicMPWdaUNQeVvQPb7WRW2NXnq+8Wo5tYulVC4KJhsM jV8450brPVmJa6h8aYZBr6iH8uuR3Z4FWGdxAUEbeG5Egb2yw2s3w4OGkN4hfjz7lmau 4PwQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244357; x=1795849157; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aVB8Y+p+SSfHh9qUhyaF69ZnIILGyQybiQn1OGrqgeA=; b=je+jPBBXf4OFoFbiEACQZBTbrRQ+Q2kjn84yHHlbIiKF3cDjAIGiCL1gQktsP8RUe0 zS/0MHeaLYrJTgH2kSwTFDk3Ptz6/1u0N7oqhwo/5tzc6kTQYEixVHDteuEaR7m21mR5 tebySaxYMpzE2sir17RBw3ceCECtnYy+kjBXo1S+3zlRI9iV2b9Pjl0YldJwm/fiUdOp ONckvMiV8d149rJZF7iRJI9A2m/xsYawBRZv/GTQX0HfFHE7iGYmBtYmf88oNqkkejNW p7AEYJTb6jXlG207FqyJ7B5EBOixWiqLXbPQ6RWxp6AnZOvM1danoNxNrYsXuD1f5J39 9Z1g== X-Forwarded-Encrypted: i=1; AL+hErEcGYocdTjGfSNaJIttwM118GBysNkqtTqIRIEmC2pe0njzodmkOnJlbq7S82OJ5+zA2iP37fJaoOFVk6A=@vger.kernel.org X-Gm-Message-State: APuU4Z++wV+n+YsH8UOr3ZE4NfJwHxfD2ev7zfFB7VwP1elvm3I0GPJo dHxPatd/GSEKX9ubUo+6t/soEt1z/fbJ1JiU5k2l+u+11CmJaVJCPwDtbur1eHhxXKltym9cFoF jB3suOgNUhQ== X-Received: from oacje12.prod.google.com ([2002:a05:687c:2e0c:b0:4a6:98a5:8dea]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:6986:b0:4a7:5eb7:ab8e with SMTP id 586e51a60fabf-4bc84b1d780mr4716649fac.44.1795244356893; Fri, 20 Nov 2026 22:59:16 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:02 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-14-irogers@google.com> Subject: [PATCH v15 13/23] perf dso: Expand context analysis annotations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Annotate struct dsos fields with __guarded_by(&lock), annotate dso__data_open and fd_limit in util/dso.c with __guarded_by(&dso__data_open_lock), and tighten __dsos__findnew_id() and __dsos__addnew_id() from shared to exclusive lock requirements. Signed-off-by: Ian Rogers --- tools/perf/tests/dso-data.c | 4 ++++ tools/perf/util/dso.c | 14 +++++++++----- tools/perf/util/dsos.c | 9 +++++++-- tools/perf/util/dsos.h | 10 +++++----- tools/perf/util/vdso.c | 1 + 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/tools/perf/tests/dso-data.c b/tools/perf/tests/dso-data.c index 18b7052a2dc5..6ef59929e6c0 100644 --- a/tools/perf/tests/dso-data.c +++ b/tools/perf/tests/dso-data.c @@ -116,12 +116,14 @@ static int dso__data_fd(struct dso *dso, struct machi= ne *machine) =20 static void dsos__delete(struct dsos *dsos) { + down_write(&dsos->lock); for (unsigned int i =3D 0; i < dsos->cnt; i++) { struct dso *dso =3D dsos->dsos[i]; =20 dso__data_close(dso); unlink(dso__name(dso)); } + up_write(&dsos->lock); dsos__exit(dsos); } =20 @@ -254,6 +256,7 @@ static int set_fd_limit(int n) } =20 static int test__dso_data_cache(struct test_suite *test __maybe_unused, in= t subtest __maybe_unused) + __context_unsafe(/* single-threaded test */) { struct machine machine; long nr_end, nr =3D open_files_cnt(); @@ -324,6 +327,7 @@ static long new_limit(int count) } =20 static int test__dso_data_reopen(struct test_suite *test __maybe_unused, i= nt subtest __maybe_unused) + __context_unsafe(/* single-threaded test */) { struct machine machine; long nr_end, nr =3D open_files_cnt(), lim =3D new_limit(3); diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 1c938d1b11e9..49813424e4ee 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -525,7 +525,8 @@ void dso__set_module_info(struct dso *dso, struct kmod_= path *m, * Global list of open DSOs and the counter. */ DEFINE_MUTEX(dso__data_open_lock); -static LIST_HEAD(dso__data_open); +static struct list_head dso__data_open __guarded_by(&dso__data_open_lock) = =3D + LIST_HEAD_INIT(dso__data_open); static long dso__data_open_cnt __guarded_by(&dso__data_open_lock); =20 static void dso__list_add(struct dso *dso) __must_hold(&dso__data_open_loc= k) @@ -757,7 +758,7 @@ static rlim_t get_fd_limit(void) return limit; } =20 -static rlim_t fd_limit; +static rlim_t fd_limit __guarded_by(&dso__data_open_lock); =20 /* * Used only by tests/dso-data.c to reset the environment @@ -766,7 +767,8 @@ static rlim_t fd_limit; */ void reset_fd_limit(void) { - fd_limit =3D 0; + /* Only called by single-threaded test setup. */ + context_unsafe(fd_limit =3D 0); } =20 static bool may_cache_fd(void) __must_hold(&dso__data_open_lock) @@ -1726,8 +1728,10 @@ void __dso__improve_id(struct dso *dso, const struct= dso_id *id) changed =3D true; } } - if (changed && dsos) - dsos->sorted =3D false; + if (changed && dsos) { + /* dsos write lock held by caller. */ + context_unsafe(dsos->sorted =3D false); + } } =20 int dso_id__cmp(const struct dso_id *a, const struct dso_id *b) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index 4ff38cb5e2cb..7a56577f4ec8 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -12,6 +12,7 @@ #include =20 void dsos__init(struct dsos *dsos) + __context_unsafe(/* initialization */) { init_rwsem(&dsos->lock); =20 @@ -50,6 +51,7 @@ void dsos__exit(struct dsos *dsos) static int __dsos__for_each_dso(struct dsos *dsos, int (*cb)(struct dso *dso, void *data), void *data) + __must_hold_shared(&dsos->lock) { for (unsigned int i =3D 0; i < dsos->cnt; i++) { struct dso *dso =3D dsos->dsos[i]; @@ -182,7 +184,8 @@ static struct dso *__dsos__find_by_longname_id(struct d= sos *dsos, } qsort(dsos->dsos, dsos->cnt, sizeof(struct dso *), dsos__cmp_long_name_id_short_name); - dsos->sorted =3D true; + /* write_locked is true, so dsos->lock is held exclusively. */ + context_unsafe(dsos->sorted =3D true); } =20 res =3D bsearch(&key, dsos->dsos, dsos->cnt, sizeof(struct dso *), @@ -194,6 +197,7 @@ static struct dso *__dsos__find_by_longname_id(struct d= sos *dsos, } =20 int __dsos__add(struct dsos *dsos, struct dso *dso) + __must_hold(&dsos->lock) { if (!dso) return -EINVAL; @@ -320,6 +324,7 @@ static void dso__set_basename(struct dso *dso) } =20 static struct dso *__dsos__addnew_id(struct dsos *dsos, const char *name, = const struct dso_id *id) + __must_hold(&dsos->lock) { struct dso *dso =3D dso__new_id(name, id); =20 @@ -336,7 +341,7 @@ static struct dso *__dsos__addnew_id(struct dsos *dsos,= const char *name, const } =20 static struct dso *__dsos__findnew_id(struct dsos *dsos, const char *name,= const struct dso_id *id) - __must_hold_shared(&dsos->lock) + __must_hold(&dsos->lock) { struct dso *dso =3D __dsos__find_id(dsos, name, id, false, /*write_locked= =3D*/true); =20 diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h index dbd06f420753..cc7e3a42f9cb 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -19,16 +19,16 @@ struct machine; */ struct dsos { struct rw_semaphore lock; - struct dso **dsos; - unsigned int cnt; - unsigned int allocated; - bool sorted; + struct dso **dsos __pt_guarded_by(&lock) __guarded_by(&lock); + unsigned int cnt __guarded_by(&lock); + unsigned int allocated __guarded_by(&lock); + bool sorted __guarded_by(&lock); }; =20 void dsos__init(struct dsos *dsos); void dsos__exit(struct dsos *dsos); =20 -int __dsos__add(struct dsos *dsos, struct dso *dso); +int __dsos__add(struct dsos *dsos, struct dso *dso) __must_hold(&dsos->loc= k); int dsos__add(struct dsos *dsos, struct dso *dso); struct dso *__dsos__find(struct dsos *dsos, const char *name, bool cmp_sho= rt, bool write_locked) __must_hold_shared(&dsos->lock); diff --git a/tools/perf/util/vdso.c b/tools/perf/util/vdso.c index 8bf81e2c9e78..fe28e346dc2f 100644 --- a/tools/perf/util/vdso.c +++ b/tools/perf/util/vdso.c @@ -126,6 +126,7 @@ void machine__exit_vdso(struct machine *machine) =20 static struct dso *__machine__addnew_vdso(struct machine *machine, const c= har *short_name, const char *long_name) + __must_hold(&machine->dsos.lock) { struct dso *dso; =20 --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.197 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244360; b=5n5EIXDrIKPRMj20eJIolBn6ikqMLQmGYwQQPXUaWMYyqFRZHgTtdVZZ7pBpYRsH2c8R xpHUiR7hZK8ViWdumVK8KZm77qDepVWhdOpq4E/Xy/LaXKeNsi0uikMnAbQPqKOtZXrED DzUYAanOWfJ/gOlNzdrznMtlt/bYbGgRK2mp7V1AcPaV4l63/887vZK16B5jpc/feBfcx TddxVAygIOghIhd4pvsTCvb6KUbZcfiVJrlpTmzqTm26gG27E/7eKrudkiZDMy2xiKkG+ fwmq/tLtwr14HPngXfSZIoBMi56Ph2719uEYKRBm65XihB+ETSY+UXt9O2wsNfPqb+A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244360; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=vVP/kAT6LsjdEcP8tMNnF0/xVXQA95hDWpshiBeuW3s=; b=k/fO7VHmq1X/xogEV/IiWFnD9G8mRFc8Fihm8s1ewhXcXrQ0aI0LsgUK45L1gBjvMeql 244ej/Z/GgpMVCb13ChRPnxE99NC9ttwYv1xrLKnFd9fQQXXB/213CbFBoLkJyfMNrnQP HoQdZCPvx+h7pXh/Pm5gBMTzfGiwnZWL+vJ+hyeE5s6W/oGEWgcyxCS9CevFtHnElBIsn z6QjaDJ6A5N91ldfN75tAe0VVpxW5h2DUDdnor2xdFPDrHOUnJcFVpTIJiylsLodBm5zN LetGV04Q/Rjpj7XjwQiKafTqL4GXUg9KdUnocEBSy7HBV+Q9fX88rPFkYdDhupdT9qA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=kMmAOCyZ; arc=none smtp.remote-ip=74.125.82.197 Received: from mail-dy1-f197.google.com (mail-dy1-f197.google.com [74.125.82.197]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54198606A331 for ; Sat, 21 Nov 2026 06:59:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 54198606A331 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=kMmAOCyZ Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-30bcb065bfdso1831911eec.0 for ; Fri, 20 Nov 2026 22:59:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244360; x=1795849160; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=vVP/kAT6LsjdEcP8tMNnF0/xVXQA95hDWpshiBeuW3s=; b=kMmAOCyZdRyewtk4IS0nUbmYPmlalfpTT+CpnzZVr1tzeM9eJNCXU5zyQ2Qs1pkIcl xCqJJeQSNnT0vIgHy5RCVJlsZC/IQa6wIORwYtJ9Uo4nX/+VGj9zdbkYC7lqsHMutC3d IuAW2vqTAheKdt30N8Jia0YqGmroz6nOBTYKJTcRg40VLgdekq//YF6afFq9imUcIk2r sL0jcS7yXAMyqPh3is/+c4kENkh9STuS/635zpzgLAJE3J+qQ+fP7iPBC5suuQd9SJwz 7HszHnL+o+rSd9ac+bz8MrkmfbJeuXJxdwF+8N2LHPXG1d1xEgDv+OFPUPW4Ag6ajroj DqHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244360; x=1795849160; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vVP/kAT6LsjdEcP8tMNnF0/xVXQA95hDWpshiBeuW3s=; b=NEiMbkSbHFts3vXx67PV+kluafeqaX8rC3TCE/Sxfy6O8ViENVxgavsKnvpBsaYx1m xQEEa+flf/qXV0DamAMQTDwykY0oJtMV4sTLPqDSCeAeKQElGaaXWfPzCA8O2Qh+uyqE CpjeXBj4NxkJcDuLkCgpdTGyZKki8DPisVfC9oyYGMoJIugyx8u/NyGYfngdiBXyPVpg +f2WLXy1lHXNxxtUUhi25Q3ElHzp545sMTcsvN7IsulOONhTJjOlbYa+18ysCXQRaUjf Rlj3oMcOjLZwLYAKgv6nhsfOWZXv0Lbe1UtJhF9JhxoLaTO1dYENRLqt09fyICmyHO5h UvkQ== X-Forwarded-Encrypted: i=1; AL+hErErOD5vuU2IIjCG2Ak6ytiAJEf9B6ka6NOaV/c/EPrBDY1jxgKw1TazbBlS+PkHvmOjETmZvFaG7Gvf42k=@vger.kernel.org X-Gm-Message-State: APuU4Z+5EVP9YfVKbndQM0fkvj2LTkN1Mh9cnhuuZI04EaMCd9NerN88 swQYoFnUKgjpjWrak7VfJHIk3A47OFVzqEhBSGm1nyb0h1r0hHDFYsc76PaqxdlhrEv/3gOu8qg UD/tdFL70ew== X-Received: from dycjx24.prod.google.com ([2002:a05:7300:fd18:b0:351:1933:2028]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:7187:b0:386:bd8:7ab3 with SMTP id 5a478bee46e88-3860bd88965mr4503106eec.29.1795244359194; Fri, 20 Nov 2026 22:59:19 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:03 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-15-irogers@google.com> Subject: [PATCH v15 14/23] perf thread: Expand context analysis annotations to threads and namespaces_lock From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Annotate shard and last_match in struct threads_table_entry with __guarded_by(&lock) and add __must_hold_shared()/__must_hold() to the last_match helpers in util/threads.c. In util/thread.h and util/thread.c, add __returns_ctx_lock() to thread__namespaces_lock() and thread__comm_lock(), annotate namespaces_list and comm_list with __guarded_by(), and add __must_hold_shared()/__must_hold() to the namespaces_list helpers. Signed-off-by: Ian Rogers --- tools/perf/util/thread.c | 2 ++ tools/perf/util/thread.h | 28 +++++++++++++++++----------- tools/perf/util/threads.c | 4 ++++ tools/perf/util/threads.h | 6 +++--- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index 616d5bf87baf..af761b1fc5d5 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -149,6 +149,7 @@ void thread__put(struct thread *thread) } =20 static struct namespaces *__thread__namespaces(struct thread *thread) + __must_hold_shared(thread__namespaces_lock(thread)) { if (list_empty(thread__namespaces_list(thread))) return NULL; @@ -169,6 +170,7 @@ struct namespaces *thread__namespaces(struct thread *th= read) =20 static int __thread__set_namespaces(struct thread *thread, u64 timestamp, struct perf_record_namespaces *event) + __must_hold(thread__namespaces_lock(thread)) { struct namespaces *new, *curr =3D __thread__namespaces(thread); =20 diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index 9d0ce7742c06..a7850eebce4e 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h @@ -48,10 +48,10 @@ DECLARE_RC_STRUCT(thread) { bool exited; bool comm_set; int comm_len; - struct list_head namespaces_list; struct rw_semaphore namespaces_lock; - struct list_head comm_list; + struct list_head namespaces_list __guarded_by(&namespaces_lock); struct rw_semaphore comm_lock; + struct list_head comm_list __guarded_by(&comm_lock); u64 db_id; =20 void *priv; @@ -230,30 +230,36 @@ static inline void thread__set_comm_len(struct thread= *thread, int len) RC_CHK_ACCESS(thread)->comm_len =3D len; } =20 -static inline struct list_head *thread__namespaces_list(struct thread *thr= ead) +static inline struct rw_semaphore *thread__namespaces_lock(const struct th= read *thread) + __returns_ctx_lock(&RC_CHK_ACCESS(thread)->namespaces_lock) { - return &RC_CHK_ACCESS(thread)->namespaces_list; + return (struct rw_semaphore *)&RC_CHK_ACCESS(thread)->namespaces_lock; } =20 -static inline int thread__namespaces_list_empty(const struct thread *threa= d) +static inline struct list_head *thread__namespaces_list(struct thread *thr= ead) + __must_hold_shared(thread__namespaces_lock(thread)) { - return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list); + /* Readers hold the shared lock; writers hold the exclusive lock. */ + return context_unsafe(&RC_CHK_ACCESS(thread)->namespaces_list); } =20 -static inline struct rw_semaphore *thread__namespaces_lock(struct thread *= thread) +static inline int thread__namespaces_list_empty(const struct thread *threa= d) + __must_hold_shared(thread__namespaces_lock(thread)) { - return &RC_CHK_ACCESS(thread)->namespaces_lock; + return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list); } =20 -static inline struct rw_semaphore *thread__comm_lock(struct thread *thread) +static inline struct rw_semaphore *thread__comm_lock(const struct thread *= thread) + __returns_ctx_lock(&RC_CHK_ACCESS(thread)->comm_lock) { - return &RC_CHK_ACCESS(thread)->comm_lock; + return (struct rw_semaphore *)&RC_CHK_ACCESS(thread)->comm_lock; } =20 static inline struct list_head *thread__comm_list(struct thread *thread) __must_hold_shared(thread__comm_lock(thread)) { - return &RC_CHK_ACCESS(thread)->comm_list; + /* Readers hold the shared lock; writers hold the exclusive lock. */ + return context_unsafe(&RC_CHK_ACCESS(thread)->comm_list); } =20 static inline u64 thread__db_id(const struct thread *thread) diff --git a/tools/perf/util/threads.c b/tools/perf/util/threads.c index 5db81b9debb5..7cf951cbc963 100644 --- a/tools/perf/util/threads.c +++ b/tools/perf/util/threads.c @@ -24,6 +24,7 @@ static bool key_equal(long key1, long key2, void *ctx __m= aybe_unused) } =20 void threads__init(struct threads *threads) + __context_unsafe(/* initialization */) { for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { struct threads_table_entry *table =3D &threads->table[i]; @@ -35,6 +36,7 @@ void threads__init(struct threads *threads) } =20 void threads__exit(struct threads *threads) + __context_unsafe(/* teardown */) { threads__remove_all_threads(threads); for (int i =3D 0; i < THREADS__TABLE_SIZE; i++) { @@ -66,6 +68,7 @@ size_t threads__nr(struct threads *threads) */ static struct thread *__threads_table_entry__get_last_match(struct threads= _table_entry *table, pid_t tid) + __must_hold_shared(&table->lock) { struct thread *th, *res =3D NULL; =20 @@ -79,6 +82,7 @@ static struct thread *__threads_table_entry__get_last_mat= ch(struct threads_table =20 static void __threads_table_entry__set_last_match(struct threads_table_ent= ry *table, struct thread *th) + __must_hold(&table->lock) { thread__put(table->last_match); table->last_match =3D thread__get(th); diff --git a/tools/perf/util/threads.h b/tools/perf/util/threads.h index da68d2223f18..54fba16657db 100644 --- a/tools/perf/util/threads.h +++ b/tools/perf/util/threads.h @@ -11,10 +11,10 @@ struct thread; #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS) =20 struct threads_table_entry { - /* Key is tid, value is struct thread. */ - struct hashmap shard; struct rw_semaphore lock; - struct thread *last_match; + /* Key is tid, value is struct thread. */ + struct hashmap shard __guarded_by(&lock); + struct thread *last_match __guarded_by(&lock); }; =20 struct threads { --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244363; b=cHNRr6pxnkW+/+t/sjcLqmr9u1CAjC09Dxbem1SKFObNjqx/qPTQPeSD6nyholHo7+3Q E7qvbIJp2QAzap/gJjQXn55hEeNVR7fiUKCW0h9OjzXSYaFnmPUg8xLSUXFvxikbO6K18 TtYBjjP2QlI/Amu7vgrrdfnPXalps+DQszkOi4N/BeqhzxWKUANZ/GDbhr6tr0owA0p13 0ML+4JYaYrrWr1eUhxXCMIJ+Tv7IVrwr4zcFlEBqt59qjA0WVau+DPIC0+d9o4+NN/swy CBR9ihwL77oLRfls0LsycOtg32vqhV2mqUoZULZCnoQTVBUbzN71ex3aPB8lVWq5Tyg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244363; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=d95iHTNdtf/y8c1gOlDmpA5GClWqrzVm2GjHbOzts1E=; b=dHnP4Bb8e6zUhVr8FqLlyesAsN97GoVI02f3M2/WLvCyHD4Y0ogDxA96KD7kH4bVCXVX 4M+I0qQ3861S5/PkElHKYYoeUPierv8u4ENWVrFEqPIOGG9j7QXEpaY5yY4MkTnkq/O89 W8bYOBnICW3avPU4m1HSbsWz31xs1ZFehrJ5VNikwf0DAMZvP4Li00kDwcYeBfA2MkdO2 cXWHe2mXTw7PqkZHiSB54Xxw68/2WGQvXZBlIXvDLXxdQeFtFfYPCaadzChWD5rH5MkAP KLeyo+Y1Z4LOtfRQYb0W6djQJx2nWhQE1VuAM2gpTiHTIbalRtkjU6pAq+3Nf+CMrxw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=F4P3DI88; arc=none smtp.remote-ip=209.85.167.198 Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E5A2606A33D for ; Sat, 21 Nov 2026 06:59:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2E5A2606A33D 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=F4P3DI88 Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-4cbf700ddccso1455912b6e.0 for ; Fri, 20 Nov 2026 22:59:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244362; x=1795849162; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=d95iHTNdtf/y8c1gOlDmpA5GClWqrzVm2GjHbOzts1E=; b=F4P3DI8899shPOvrCS8jIAml7OLHECSct3jhKXphaDBrr3ZXq2Kn76chC8h2CeMhWG HiF6cqXd7GjAwaK9uGUxEgwDW9FQ/SbGMDt6Cc1SCRnlESb6qxLY3BnZyZrDvAdVz4AU qfXKTwHQ2IFGXvbRg0thXR9XQsSOC8CrlpRYZt0CufmvZlVkacXV5tp5tETOm0HdKKoQ vlNAMngxrj9poXFwRmoM1fPYofpjI8pTsJ4z/ehgEjZv86nkbK2o3dTatRPGQbg4ZktG ltkNDSYH2+87eWOz5V/xDlmvxOazCw6dnsHMN/2ZGJf/rm8YYPc7tnVbP/fKH7hG9ja0 AD+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244362; x=1795849162; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=d95iHTNdtf/y8c1gOlDmpA5GClWqrzVm2GjHbOzts1E=; b=MZLMFFQbTTFkcwq7/tafQfd+5M/tHgQ4ORgo4RYX8IlH4PdMHDBqx+ZSQgPdrsLmxJ /4IJcEnH8DR3MwD/D7JSeIumrK6YDS1WrciULRoCnbw3n7A5rnvpcKeEBxudwQ9aJt7Y W/d+vy+V/Yo/QasqJ13sAwimsR6yBpcm4+JOnRU8XmBTuXHgFZW0GjmMTooSPETy+XYK bqk4oDFR3TYXYov02p8KR3aPQIu44g8uUqTMbOo1VJmk2iWdxoJemyRWmFfP7ckkyF0O C6kaT5XI7HkmQbutz0QmPGnrK5kRJpe8mQHOvp4iSJXUm2yzFq/JltuWqqhUynzUf5Xt xeYA== X-Forwarded-Encrypted: i=1; AL+hErHALQXg27lKz6Ak8ca8MtSRHN9cf0Z6jjtcTqtgBF0TqVEqDuBa37Xxqv1GzjFU00dsNoIHvZTfp/hHsIU=@vger.kernel.org X-Gm-Message-State: APuU4Z/rEwZGiuHA/Mn6exigPITPl7GSDnqCFsByKfB+oSMfNpdb5zAY m8UUEwhGrElGsZRGKbyu9bTUR+7S/GLhsVoVguWc5YQl+rAH4L444voNjSgwPDrEzqyWd23SvkC gD5ewmB686A== X-Received: from ioi20-n1.prod.google.com ([2002:a05:6602:c654:10b0:9c7:66cc:b115]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:f13:b0:567:f784:f939 with SMTP id 5614622812f47-57a0bb54177mr5310815b6e.51.1795244361845; Fri, 20 Nov 2026 22:59:21 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:04 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-16-irogers@google.com> Subject: [PATCH v15 15/23] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" intel-tpebs has two lifecycle bugs around the background 'perf record' process and sample reader thread: 1. tpebs_cmd.out is read without tpebs_mtx and closed by both perf_session__delete() in __sample_reader() and tpebs_stop() (or leaked if perf_session__new() fails). 2. tpebs_stop() reads tpebs_reader_thread after unlocking tpebs_mtx, and tpebs_send_record_cmd() drops tpebs_mtx while writing undelimited control strings and polling ack_fd, allowing concurrent open/close calls to coalesce commands or race to close pipes, join the reader thread, and reap tpebs_cmd twice. Fix (1) by initializing tpebs_cmd.out =3D -1, passing it to __sample_reader(), and resetting tpebs_cmd.out =3D -1 once ownership is transferred (and on start_command() failure). Fix (2) by preparing events under tpebs_mtx, reading tpebs_reader_thread under tpebs_mtx, newline-terminating control commands, serializing open, tpebs_send_record_cmd(), and teardown via a condition variable and generation counter, and ignoring late samples while stopping. Add explicit header includes used by the file. Fixes: 8db5cabcf1b6 ("perf stat: Fork and launch 'perf record' when 'perf s= tat' needs to get retire latency value for a metric.") Fixes: bded0398ac19 ("perf tpebs: Fix concurrent stop races and PID reuse h= azards in tpebs_stop") Signed-off-by: Ian Rogers --- tools/perf/util/intel-tpebs.c | 243 +++++++++++++++++++++++++--------- 1 file changed, 184 insertions(+), 59 deletions(-) diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c index 7c0b8b15bd10..23d1e8bb678c 100644 --- a/tools/perf/util/intel-tpebs.c +++ b/tools/perf/util/intel-tpebs.c @@ -27,6 +27,12 @@ #include #include #include +#include +#include +#include +#include +#include +#include =20 #define PERF_DATA "-" =20 @@ -34,10 +40,17 @@ bool tpebs_recording; enum tpebs_mode tpebs_mode; static LIST_HEAD(tpebs_results); static pthread_t tpebs_reader_thread; -static struct child_process tpebs_cmd; +static struct child_process tpebs_cmd =3D { + .out =3D -1, +}; static int control_fd[2], ack_fd[2]; static struct mutex tpebs_mtx; +static struct cond tpebs_cmd_cond =3D { .cond =3D PTHREAD_COND_INITIALIZER= }; static bool tpebs_stopping; +static bool tpebs_starting; +static bool tpebs_enabled; +static bool tpebs_cmd_in_flight; +static uint64_t tpebs_gen; =20 struct tpebs_retire_lat { struct list_head nd; @@ -120,10 +133,15 @@ static int evsel__tpebs_start_perf_record(struct evse= l *evsel) tpebs_cmd.out =3D -1; ret =3D start_command(&tpebs_cmd); zfree(&tpebs_cmd.argv); + if (ret) { + tpebs_cmd.pid =3D 0; + tpebs_cmd.out =3D -1; + return ret; + } list_for_each_entry(t, &tpebs_results, nd) t->started =3D true; =20 - return ret; + return 0; } =20 static bool is_child_pid(pid_t parent, pid_t child) @@ -195,8 +213,8 @@ static int process_sample_event(const struct perf_tool = *tool __maybe_unused, struct tpebs_retire_lat *t; =20 mutex_lock(tpebs_mtx_get()); - if (tpebs_cmd.pid =3D=3D 0) { - /* Record has terminated. */ + if (tpebs_stopping || tpebs_cmd.pid =3D=3D 0) { + /* Record is stopping or has terminated. */ mutex_unlock(tpebs_mtx_get()); return 0; } @@ -220,13 +238,13 @@ static int process_sample_event(const struct perf_too= l *tool __maybe_unused, return 0; } =20 -static void *__sample_reader(void *arg __maybe_unused) +static void *__sample_reader(void *arg) { struct perf_session *session; struct perf_data data =3D { .mode =3D PERF_DATA_MODE_READ, .path =3D PERF_DATA, - .file.fd =3D tpebs_cmd.out, + .file.fd =3D (int)(intptr_t)arg, }; struct perf_tool tool; =20 @@ -236,8 +254,10 @@ static void *__sample_reader(void *arg __maybe_unused) tool.attr =3D perf_event__process_attr; =20 session =3D perf_session__new(&data, &tool); - if (IS_ERR(session)) + if (IS_ERR(session)) { + close(data.file.fd); return NULL; + } perf_session__process_events(session); perf_session__delete(session); =20 @@ -247,35 +267,49 @@ static void *__sample_reader(void *arg __maybe_unused) static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_ge= t()) { struct pollfd pollfd =3D { .events =3D POLLIN, }; - int ret, len, retries =3D 0; + bool is_stop =3D !strcmp(msg, EVLIST_CTL_CMD_STOP_TAG); + int ret, len, retries =3D 0, ctrl_fd, resp_fd; + char cmd_buf[EVLIST_CTL_CMD_MAX_LEN]; char ack_buf[8]; =20 + while (tpebs_cmd_in_flight) { + if (!is_stop && tpebs_stopping) + return -EBUSY; + cond_wait(&tpebs_cmd_cond, tpebs_mtx_get()); + } + if (!is_stop && tpebs_stopping) + return -EBUSY; + /* Check if the command exited before the send, done with the lock held. = */ if (tpebs_cmd.pid =3D=3D 0) return 0; =20 + ctrl_fd =3D control_fd[1]; + resp_fd =3D ack_fd[0]; + tpebs_cmd_in_flight =3D true; + /* * Let go of the lock while sending/receiving as blocking can starve the * sample reading thread. */ mutex_unlock(tpebs_mtx_get()); =20 - /* Send perf record command.*/ - len =3D strlen(msg); - ret =3D write(control_fd[1], msg, len); + /* Send perf record command. */ + len =3D scnprintf(cmd_buf, sizeof(cmd_buf), "%s\n", msg); + ret =3D write(ctrl_fd, cmd_buf, len); if (ret !=3D len) { pr_err("perf record control write control message '%s' failed\n", msg); ret =3D -EPIPE; goto out; } =20 - if (!strcmp(msg, EVLIST_CTL_CMD_STOP_TAG)) { + if (is_stop) { ret =3D 0; goto out; } =20 /* Wait for an ack. */ - pollfd.fd =3D ack_fd[0]; + pollfd.fd =3D resp_fd; =20 /* * We need this poll to ensure the ack_fd PIPE will not hang @@ -284,7 +318,17 @@ static int tpebs_send_record_cmd(const char *msg) __mu= st_hold(tpebs_mtx_get()) */ again: if (!poll(&pollfd, 1, 500)) { - if (check_if_command_finished(&tpebs_cmd)) { + bool stopping, finished; + + mutex_lock(tpebs_mtx_get()); + stopping =3D tpebs_stopping; + finished =3D !stopping && check_if_command_finished(&tpebs_cmd); + mutex_unlock(tpebs_mtx_get()); + if (stopping) { + ret =3D -EBUSY; + goto out; + } + if (finished) { ret =3D 0; goto out; } @@ -297,7 +341,17 @@ static int tpebs_send_record_cmd(const char *msg) __mu= st_hold(tpebs_mtx_get()) } =20 if (!(pollfd.revents & POLLIN)) { - if (check_if_command_finished(&tpebs_cmd)) { + bool stopping, finished; + + mutex_lock(tpebs_mtx_get()); + stopping =3D tpebs_stopping; + finished =3D !stopping && check_if_command_finished(&tpebs_cmd); + mutex_unlock(tpebs_mtx_get()); + if (stopping) { + ret =3D -EBUSY; + goto out; + } + if (finished) { ret =3D 0; goto out; } @@ -307,7 +361,7 @@ static int tpebs_send_record_cmd(const char *msg) __mus= t_hold(tpebs_mtx_get()) goto out; } =20 - ret =3D read(ack_fd[0], ack_buf, sizeof(ack_buf)); + ret =3D read(resp_fd, ack_buf, sizeof(ack_buf)); if (ret > 0) ret =3D strcmp(ack_buf, EVLIST_CTL_CMD_ACK_TAG); else @@ -315,6 +369,8 @@ static int tpebs_send_record_cmd(const char *msg) __mus= t_hold(tpebs_mtx_get()) out: /* Re-take lock as expected by caller. */ mutex_lock(tpebs_mtx_get()); + tpebs_cmd_in_flight =3D false; + cond_broadcast(&tpebs_cmd_cond); return ret; } =20 @@ -331,12 +387,25 @@ static int tpebs_stop(void) __must_hold(tpebs_mtx_get= ()) /* Like tpebs_start, we should only run tpebs_end once. */ if (tpebs_cmd.pid !=3D 0) { pid_t actual_pid =3D tpebs_cmd.pid; + bool enabled =3D tpebs_enabled; + pthread_t reader_thread; =20 tpebs_stopping =3D true; - tpebs_send_record_cmd(EVLIST_CTL_CMD_STOP_TAG); + tpebs_starting =3D false; + tpebs_enabled =3D false; + tpebs_gen++; + cond_broadcast(&tpebs_cmd_cond); + if ((tpebs_send_record_cmd(EVLIST_CTL_CMD_STOP_TAG) || !enabled) && + !tpebs_cmd.finished) + kill(actual_pid, SIGTERM); + /* + * tpebs_results is empty when stopping; clear pid before + * releasing tpebs_mtx to join the reader thread. + */ tpebs_cmd.pid =3D 0; + reader_thread =3D tpebs_reader_thread; mutex_unlock(tpebs_mtx_get()); - pthread_join(tpebs_reader_thread, NULL); + pthread_join(reader_thread, NULL); mutex_lock(tpebs_mtx_get()); if (control_fd[0] >=3D 0) { close(control_fd[0]); @@ -464,41 +533,53 @@ static struct tpebs_retire_lat *tpebs_retire_lat__fin= d(struct evsel *evsel) return NULL; } =20 -/** - * evsel__tpebs_prepare - create tpebs data structures ready for opening. - * @evsel: retire_latency evsel, all evsels on its list will be prepared. - */ -static int evsel__tpebs_prepare(struct evsel *evsel) +static void tpebs_results__delete(void) __must_hold(tpebs_mtx_get()) +{ + struct tpebs_retire_lat *t, *tmp; + + list_for_each_entry_safe(t, tmp, &tpebs_results, nd) { + list_del_init(&t->nd); + tpebs_retire_lat__delete(t); + } +} + +static int evsel__tpebs_prepare_one(struct evsel *evsel) __must_hold(tpebs= _mtx_get()) { - struct evsel *pos; struct tpebs_retire_lat *tpebs_event; =20 - mutex_lock(tpebs_mtx_get()); - tpebs_event =3D tpebs_retire_lat__find(evsel); - if (tpebs_event) { - /* evsel, or an identically named one, was already prepared. */ - mutex_unlock(tpebs_mtx_get()); + if (tpebs_retire_lat__find(evsel)) return 0; - } + tpebs_event =3D tpebs_retire_lat__new(evsel); - if (!tpebs_event) { - mutex_unlock(tpebs_mtx_get()); + if (!tpebs_event) return -ENOMEM; - } + list_add_tail(&tpebs_event->nd, &tpebs_results); - mutex_unlock(tpebs_mtx_get()); + return 0; +} + +/** + * evsel__tpebs_prepare - create tpebs data structures ready for opening. + * @evsel: retire_latency evsel, all evsels on its list will be prepared. + */ +static int evsel__tpebs_prepare(struct evsel *evsel) __must_hold(tpebs_mtx= _get()) +{ + struct evsel *pos; + int ret; + + ret =3D evsel__tpebs_prepare_one(evsel); + if (ret) + return ret; =20 /* * Eagerly prepare all other evsels on the list to try to ensure that by * open they are all known. */ evlist__for_each_entry(evsel->evlist, pos) { - int ret; - if (pos =3D=3D evsel || !pos->retire_lat) continue; =20 - ret =3D evsel__tpebs_prepare(pos); + ret =3D evsel__tpebs_prepare_one(pos); if (ret) return ret; } @@ -515,12 +596,26 @@ int evsel__tpebs_open(struct evsel *evsel) int ret; bool tpebs_empty; bool started_process =3D false; + bool started_thread =3D false; + uint64_t open_gen =3D 0; =20 /* We should only run tpebs_start when tpebs_recording is enabled. */ if (!tpebs_recording) return 0; =20 mutex_lock(tpebs_mtx_get()); + if (tpebs_starting) { + uint64_t wait_gen =3D tpebs_gen; + + while (tpebs_starting && tpebs_gen =3D=3D wait_gen) + cond_wait(&tpebs_cmd_cond, tpebs_mtx_get()); + + if (tpebs_gen !=3D wait_gen) { + mutex_unlock(tpebs_mtx_get()); + return -EBUSY; + } + } + if (tpebs_stopping) { mutex_unlock(tpebs_mtx_get()); return -EBUSY; @@ -531,25 +626,24 @@ int evsel__tpebs_open(struct evsel *evsel) bool valid; =20 t =3D tpebs_retire_lat__find(evsel); - valid =3D t && t->started; + valid =3D t && t->started && tpebs_enabled; mutex_unlock(tpebs_mtx_get()); /* May fail as the event wasn't started. */ return valid ? 0 : -EBUSY; } - mutex_unlock(tpebs_mtx_get()); =20 ret =3D evsel__tpebs_prepare(evsel); - if (ret) + if (ret) { + tpebs_results__delete(); + mutex_unlock(tpebs_mtx_get()); return ret; - - mutex_lock(tpebs_mtx_get()); - if (tpebs_stopping || tpebs_cmd.pid !=3D 0) { - ret =3D -EBUSY; - goto out; } + tpebs_empty =3D list_empty(&tpebs_results); if (!tpebs_empty) { started_process =3D true; + tpebs_enabled =3D false; + open_gen =3D ++tpebs_gen; /*Create control and ack fd for --control*/ if (pipe(control_fd) < 0) { pr_err("tpebs: Failed to create control fifo"); @@ -567,28 +661,50 @@ int evsel__tpebs_open(struct evsel *evsel) goto out; =20 if (pthread_create(&tpebs_reader_thread, /*attr=3D*/NULL, __sample_reade= r, - /*arg=3D*/NULL)) { - kill(tpebs_cmd.pid, SIGTERM); + (void *)(intptr_t)tpebs_cmd.out)) { pr_err("Could not create thread to process sample data.\n"); ret =3D -1; goto out; } + /* __sample_reader now owns and closes the output fd. */ + tpebs_cmd.out =3D -1; + started_thread =3D true; + tpebs_starting =3D true; ret =3D tpebs_send_record_cmd(EVLIST_CTL_CMD_ENABLE_TAG); + if (!ret) { + if (tpebs_gen !=3D open_gen) { + ret =3D -EBUSY; + } else { + tpebs_enabled =3D true; + if (!tpebs_retire_lat__find(evsel)) + ret =3D -EBUSY; + } + } } out: - if (ret) { - struct tpebs_retire_lat *t =3D tpebs_retire_lat__find(evsel); - - if (t) { - list_del_init(&t->nd); - tpebs_retire_lat__delete(t); - } - - if (started_process) { - if (tpebs_cmd.pid > 0) { - kill(tpebs_cmd.pid, SIGTERM); - finish_command(&tpebs_cmd); + if (started_process && tpebs_gen =3D=3D open_gen) { + tpebs_starting =3D false; + if (ret) { + pid_t actual_pid =3D tpebs_cmd.pid; + + tpebs_stopping =3D true; + tpebs_enabled =3D false; + tpebs_gen++; + tpebs_results__delete(); + cond_broadcast(&tpebs_cmd_cond); + if (actual_pid > 0) { + if (!tpebs_cmd.finished) + kill(actual_pid, SIGTERM); + while (tpebs_cmd_in_flight) + cond_wait(&tpebs_cmd_cond, tpebs_mtx_get()); tpebs_cmd.pid =3D 0; + if (started_thread) { + pthread_t reader_thread =3D tpebs_reader_thread; + + mutex_unlock(tpebs_mtx_get()); + pthread_join(reader_thread, NULL); + mutex_lock(tpebs_mtx_get()); + } } if (tpebs_cmd.out >=3D 0) { close(tpebs_cmd.out); @@ -610,6 +726,14 @@ int evsel__tpebs_open(struct evsel *evsel) close(ack_fd[1]); ack_fd[1] =3D -1; } + if (actual_pid > 0) { + tpebs_cmd.pid =3D actual_pid; + finish_command(&tpebs_cmd); + tpebs_cmd.pid =3D 0; + } + tpebs_stopping =3D false; + } else { + cond_broadcast(&tpebs_cmd_cond); } } mutex_unlock(tpebs_mtx_get()); @@ -645,6 +769,7 @@ int evsel__tpebs_read(struct evsel *evsel, int cpu_map_= idx, int thread) if (ret) return ret; mutex_lock(tpebs_mtx_get()); + t =3D tpebs_retire_lat__find(evsel); } if (t =3D=3D NULL || t->stats.n =3D=3D 0) { /* No sample data, use default. */ --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244365; b=fGujhSe/Iwn40bHly0ZRep/RJEpD/IyHOA6xyErTG3DMzQON1DYjttAkTi8tnQzwndxW DXseXjaCQTRYYcvwP5HygHfPhohinIfmRmIOGNjv3wvfjAcUV/4RxsZWsIB88U3YV9Kp0 cjvyyhx3Dxtr4m5BKTyIe0tDB9RvOaTIJvHOC1ZwqVDFAXmQbslt+o5AOPvYu8gj/tsqP S3QdRtoqO92VKTujrhbPIZfFYdONqx2tff0WQiKB1nWqq7kPvBXMTmVIg/qIiJ6AX05fe eO6dAyBz2FH78XJ0ccaVgrlckV6fxXfh3iY7XpP5W2Zcvz8YAzWJLfQHPyP5x8QfNKQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244365; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=bn9XTrmwtYq+QmrClFz+UoIdH7NFwaTpUsTqvt8FAI8=; b=tbaI0gG8b/sYiuOtzMl0ZdzGj7qLPRgg4WZUpYC6/459YhJkSt1bzAR2D8lpUHzZxypU GwIONDoiPTHi3jvCAH1Q5w3WPp/GzWa+2NLhyR4qsmHMNQeQlrLZFXPjqr6Q+z8lCCoTP D41v9NNm6zeXAaBGqHmh0WWURfkzIp84JuRgS5cTLo61gBWzQUfI9D1vO6NbxMG0/BjMp T4XCfkLVt2PdP18Gb+LyG0r9p2NOZaLb76FtFOKoca/RlGUSqMQKTAEWXGTe7L4zn/MbN 4bXdq16L/2ZJwI+Dy1zvcJYXHrTARaW8lECV9jE9AFbMdzeMzcvRzYMX8j+fy19yr+A== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Sx1IOEEp; arc=none smtp.remote-ip=74.125.82.70 Received: from mail-dl1-f70.google.com (mail-dl1-f70.google.com [74.125.82.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 23DBF606A332 for ; Sat, 21 Nov 2026 06:59:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 23DBF606A332 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Sx1IOEEp Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-1b29df78132so1732624c88.1 for ; Fri, 20 Nov 2026 22:59:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244364; x=1795849164; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=bn9XTrmwtYq+QmrClFz+UoIdH7NFwaTpUsTqvt8FAI8=; b=Sx1IOEEpRMBITWQbRanUse+UtAWgHCU/DV/TO12FMP1Dfvbp+mA1t/TQxqU//j7hv6 IPwjEf6JVT6SLkQmJVx9/qPg9PvJxKFcmKyuExci3kyrMKJYTdPqp1fHcnzP0RQty+bK nWFxhjo2qvU+lgdv7WUsy30dz583+Hrnz97stzi/ozG6bfjKv5YQ9RinPPInoC7hoKx9 b3nI75ZWtcBUkBbPrM+qsCTpJF9CY2qW8YCHNl5o+dAnUs+nT25BPjY9V/Zt6V6LP4Vy D9neW98oQ5PiQmQPHVSfxpqFt1g0xsSFMSPm3RfyS7QqCNY5GNt9mtQokSBSRzmF5Mf5 jAEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244364; x=1795849164; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=bn9XTrmwtYq+QmrClFz+UoIdH7NFwaTpUsTqvt8FAI8=; b=Jx5i+0f57Gd72NxzC9xN9OKaxwUAvORUh7zstkptF9ABere/xiFphy8RZTC2hcyvgZ Q4MbZBN+d8Xj2XjjP2uouUdDO3//bt/k5XL7GmTPjm+iJ6YmKPNfNftL70BMOgNOL+y/ l23LA87JWPAM9fYOztIFwoRWMwFUc+GowU3gYISi/DsQsiSUPxQGbKWJpLtKMK3+VEHi Jh+R4MLJypLZQNh0YgB7ijUsXmIrwy4XxF1lphjpfHoK3Z7gQlZUeGPyVihoz9kb2rXD lBul7RBaLYn9j8fLqH1cCGEmShdzpJOQwkuhLdGjQKriUe1tE9XjgfvxMxa7RY/hgDG1 vGAw== X-Forwarded-Encrypted: i=1; AL+hErEeSXePIuBUG3sX2YG/21Nzan/mh+QvV00CO3KV2SSWV71D5XCMiM7nnfRY3WeXs7AwvygTfLVO4CUMm6s=@vger.kernel.org X-Gm-Message-State: APuU4Z8INOyoUti62GVpsC+ynpR0yM4QKnvKTfL3TxUfK+z4imgQBBGJ RcYNK6DPBtqNNCXnc1ETwsmV9Nhc2e59DQZIQVIlnWPAMSlRmLgT24hSiYjsVUv3oSfuFk2e4Cq N+hrCqArMng== X-Received: from dlaj23.prod.google.com ([2002:a05:701b:2817:b0:1b2:4c55:e320]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:f516:b0:1b5:4aba:b087 with SMTP id a92af1059eb24-1b5567c0c01mr4607989c88.3.1795244363879; Fri, 20 Nov 2026 22:59:23 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:05 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-17-irogers@google.com> Subject: [PATCH v15 16/23] perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx and add lock annotations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Replace tpebs_mtx_get() and its pthread_once() initialization helper tpebs_init() with DEFINE_MUTEX(tpebs_mtx) and static initializers for control_fd and ack_fd, and add __guarded_by(&tpebs_mtx) and __must_hold(&tpebs_mtx) annotations to the TPEBS state. Signed-off-by: Ian Rogers --- tools/perf/util/intel-tpebs.c | 109 +++++++++++++++------------------- 1 file changed, 48 insertions(+), 61 deletions(-) diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c index 23d1e8bb678c..2f3231bab61c 100644 --- a/tools/perf/util/intel-tpebs.c +++ b/tools/perf/util/intel-tpebs.c @@ -38,19 +38,20 @@ =20 bool tpebs_recording; enum tpebs_mode tpebs_mode; -static LIST_HEAD(tpebs_results); -static pthread_t tpebs_reader_thread; -static struct child_process tpebs_cmd =3D { +static DEFINE_MUTEX(tpebs_mtx); +static struct list_head tpebs_results __guarded_by(&tpebs_mtx) =3D LIST_HE= AD_INIT(tpebs_results); +static pthread_t tpebs_reader_thread __guarded_by(&tpebs_mtx); +static struct child_process tpebs_cmd __guarded_by(&tpebs_mtx) =3D { .out =3D -1, }; -static int control_fd[2], ack_fd[2]; -static struct mutex tpebs_mtx; static struct cond tpebs_cmd_cond =3D { .cond =3D PTHREAD_COND_INITIALIZER= }; -static bool tpebs_stopping; -static bool tpebs_starting; -static bool tpebs_enabled; -static bool tpebs_cmd_in_flight; -static uint64_t tpebs_gen; +static int control_fd[2] __guarded_by(&tpebs_mtx) =3D { -1, -1 }; +static int ack_fd[2] __guarded_by(&tpebs_mtx) =3D { -1, -1 }; +static bool tpebs_stopping __guarded_by(&tpebs_mtx); +static bool tpebs_starting __guarded_by(&tpebs_mtx); +static bool tpebs_enabled __guarded_by(&tpebs_mtx); +static bool tpebs_cmd_in_flight __guarded_by(&tpebs_mtx); +static uint64_t tpebs_gen __guarded_by(&tpebs_mtx); =20 struct tpebs_retire_lat { struct list_head nd; @@ -66,25 +67,11 @@ struct tpebs_retire_lat { bool started; }; =20 -static void tpebs_init(void) -{ - mutex_init(&tpebs_mtx); - control_fd[0] =3D control_fd[1] =3D -1; - ack_fd[0] =3D ack_fd[1] =3D -1; -} - -static struct mutex *tpebs_mtx_get(void) -{ - static pthread_once_t tpebs_once =3D PTHREAD_ONCE_INIT; - - pthread_once(&tpebs_once, tpebs_init); - return &tpebs_mtx; -} - static struct tpebs_retire_lat *tpebs_retire_lat__find(struct evsel *evsel) - __must_hold(tpebs_mtx_get()); + __must_hold(&tpebs_mtx); =20 static int evsel__tpebs_start_perf_record(struct evsel *evsel) + __must_hold(&tpebs_mtx) { const char **record_argv; int tpebs_event_size =3D 0, i =3D 0, ret; @@ -212,19 +199,19 @@ static int process_sample_event(const struct perf_too= l *tool __maybe_unused, { struct tpebs_retire_lat *t; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); if (tpebs_stopping || tpebs_cmd.pid =3D=3D 0) { /* Record is stopping or has terminated. */ - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return 0; } t =3D tpebs_retire_lat__find(sample->evsel); if (!t) { - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return -EINVAL; } if (should_ignore_sample(sample, t)) { - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return 0; } /* @@ -234,7 +221,7 @@ static int process_sample_event(const struct perf_tool = *tool __maybe_unused, */ t->last =3D sample->weight3; update_stats(&t->stats, sample->weight3); - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return 0; } =20 @@ -264,7 +251,7 @@ static void *__sample_reader(void *arg) return NULL; } =20 -static int tpebs_send_record_cmd(const char *msg) __must_hold(tpebs_mtx_ge= t()) +static int tpebs_send_record_cmd(const char *msg) __must_hold(&tpebs_mtx) { struct pollfd pollfd =3D { .events =3D POLLIN, }; bool is_stop =3D !strcmp(msg, EVLIST_CTL_CMD_STOP_TAG); @@ -275,7 +262,7 @@ static int tpebs_send_record_cmd(const char *msg) __mus= t_hold(tpebs_mtx_get()) while (tpebs_cmd_in_flight) { if (!is_stop && tpebs_stopping) return -EBUSY; - cond_wait(&tpebs_cmd_cond, tpebs_mtx_get()); + cond_wait(&tpebs_cmd_cond, &tpebs_mtx); } if (!is_stop && tpebs_stopping) return -EBUSY; @@ -292,7 +279,7 @@ static int tpebs_send_record_cmd(const char *msg) __mus= t_hold(tpebs_mtx_get()) * Let go of the lock while sending/receiving as blocking can starve the * sample reading thread. */ - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); =20 /* Send perf record command. */ len =3D scnprintf(cmd_buf, sizeof(cmd_buf), "%s\n", msg); @@ -320,10 +307,10 @@ static int tpebs_send_record_cmd(const char *msg) __m= ust_hold(tpebs_mtx_get()) if (!poll(&pollfd, 1, 500)) { bool stopping, finished; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); stopping =3D tpebs_stopping; finished =3D !stopping && check_if_command_finished(&tpebs_cmd); - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); if (stopping) { ret =3D -EBUSY; goto out; @@ -343,10 +330,10 @@ static int tpebs_send_record_cmd(const char *msg) __m= ust_hold(tpebs_mtx_get()) if (!(pollfd.revents & POLLIN)) { bool stopping, finished; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); stopping =3D tpebs_stopping; finished =3D !stopping && check_if_command_finished(&tpebs_cmd); - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); if (stopping) { ret =3D -EBUSY; goto out; @@ -368,7 +355,7 @@ static int tpebs_send_record_cmd(const char *msg) __mus= t_hold(tpebs_mtx_get()) pr_err("tpebs: perf record control ack failed\n"); out: /* Re-take lock as expected by caller. */ - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); tpebs_cmd_in_flight =3D false; cond_broadcast(&tpebs_cmd_cond); return ret; @@ -377,7 +364,7 @@ static int tpebs_send_record_cmd(const char *msg) __mus= t_hold(tpebs_mtx_get()) /* * tpebs_stop - stop the sample data read thread and the perf record proce= ss. */ -static int tpebs_stop(void) __must_hold(tpebs_mtx_get()) +static int tpebs_stop(void) __must_hold(&tpebs_mtx) { int ret =3D 0; =20 @@ -404,9 +391,9 @@ static int tpebs_stop(void) __must_hold(tpebs_mtx_get()) */ tpebs_cmd.pid =3D 0; reader_thread =3D tpebs_reader_thread; - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); pthread_join(reader_thread, NULL); - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); if (control_fd[0] >=3D 0) { close(control_fd[0]); control_fd[0] =3D -1; @@ -533,7 +520,7 @@ static struct tpebs_retire_lat *tpebs_retire_lat__find(= struct evsel *evsel) return NULL; } =20 -static void tpebs_results__delete(void) __must_hold(tpebs_mtx_get()) +static void tpebs_results__delete(void) __must_hold(&tpebs_mtx) { struct tpebs_retire_lat *t, *tmp; =20 @@ -543,7 +530,7 @@ static void tpebs_results__delete(void) __must_hold(tpe= bs_mtx_get()) } } =20 -static int evsel__tpebs_prepare_one(struct evsel *evsel) __must_hold(tpebs= _mtx_get()) +static int evsel__tpebs_prepare_one(struct evsel *evsel) __must_hold(&tpeb= s_mtx) { struct tpebs_retire_lat *tpebs_event; =20 @@ -562,7 +549,7 @@ static int evsel__tpebs_prepare_one(struct evsel *evsel= ) __must_hold(tpebs_mtx_g * evsel__tpebs_prepare - create tpebs data structures ready for opening. * @evsel: retire_latency evsel, all evsels on its list will be prepared. */ -static int evsel__tpebs_prepare(struct evsel *evsel) __must_hold(tpebs_mtx= _get()) +static int evsel__tpebs_prepare(struct evsel *evsel) __must_hold(&tpebs_mt= x) { struct evsel *pos; int ret; @@ -603,21 +590,21 @@ int evsel__tpebs_open(struct evsel *evsel) if (!tpebs_recording) return 0; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); if (tpebs_starting) { uint64_t wait_gen =3D tpebs_gen; =20 while (tpebs_starting && tpebs_gen =3D=3D wait_gen) - cond_wait(&tpebs_cmd_cond, tpebs_mtx_get()); + cond_wait(&tpebs_cmd_cond, &tpebs_mtx); =20 if (tpebs_gen !=3D wait_gen) { - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return -EBUSY; } } =20 if (tpebs_stopping) { - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return -EBUSY; } /* Only start the events once. */ @@ -627,7 +614,7 @@ int evsel__tpebs_open(struct evsel *evsel) =20 t =3D tpebs_retire_lat__find(evsel); valid =3D t && t->started && tpebs_enabled; - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); /* May fail as the event wasn't started. */ return valid ? 0 : -EBUSY; } @@ -635,7 +622,7 @@ int evsel__tpebs_open(struct evsel *evsel) ret =3D evsel__tpebs_prepare(evsel); if (ret) { tpebs_results__delete(); - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return ret; } =20 @@ -696,14 +683,14 @@ int evsel__tpebs_open(struct evsel *evsel) if (!tpebs_cmd.finished) kill(actual_pid, SIGTERM); while (tpebs_cmd_in_flight) - cond_wait(&tpebs_cmd_cond, tpebs_mtx_get()); + cond_wait(&tpebs_cmd_cond, &tpebs_mtx); tpebs_cmd.pid =3D 0; if (started_thread) { pthread_t reader_thread =3D tpebs_reader_thread; =20 - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); pthread_join(reader_thread, NULL); - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); } } if (tpebs_cmd.out >=3D 0) { @@ -736,7 +723,7 @@ int evsel__tpebs_open(struct evsel *evsel) cond_broadcast(&tpebs_cmd_cond); } } - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return ret; } =20 @@ -756,7 +743,7 @@ int evsel__tpebs_read(struct evsel *evsel, int cpu_map_= idx, int thread) =20 count =3D perf_counts(evsel->counts, cpu_map_idx, thread); =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); t =3D tpebs_retire_lat__find(evsel); /* * If reading the first tpebs result, send a ping to the record @@ -765,10 +752,10 @@ int evsel__tpebs_read(struct evsel *evsel, int cpu_ma= p_idx, int thread) */ if (t && &t->nd =3D=3D tpebs_results.next) { ret =3D tpebs_send_record_cmd(EVLIST_CTL_CMD_PING_TAG); - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); if (ret) return ret; - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); t =3D tpebs_retire_lat__find(evsel); } if (t =3D=3D NULL || t->stats.n =3D=3D 0) { @@ -808,7 +795,7 @@ int evsel__tpebs_read(struct evsel *evsel, int cpu_map_= idx, int thread) break; } } - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); =20 if (old_count) { count->val =3D old_count->val + val; @@ -833,7 +820,7 @@ void evsel__tpebs_close(struct evsel *evsel) { struct tpebs_retire_lat *t; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); t =3D tpebs_retire_lat__find(evsel); if (t) { list_del_init(&t->nd); @@ -842,5 +829,5 @@ void evsel__tpebs_close(struct evsel *evsel) if (list_empty(&tpebs_results)) tpebs_stop(); } - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); } --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244368; b=T9xYirOHz7jPyTibkNif7l0t4s/hsljE2IphXTxE5CP/ce+0fD9kAfEesP3asXRL2Q/o ZTv9mEnS3lLKxCE+ZeF9aJEKZsx7Vf9n6r9IaiSDAyPfExx9TQl912JuXORFRyE/4Vn4C n68T/+hN19R6b3Xm3H2tGJKbqvbOaMQnWt3FrWSvy8V+QLkgcXpOxgDYpBR+ti9EhpTDg vl54jfVLeOFpY47eJN2GoRTUl5XD1iwdTnD2vzQWAFdnTtIxjL8+z0i/DASc39gCFLl08 UjESLwd4EYfHcFqtvoSLbwCNukxd8N9FmHh9wD5L44dMqR6D80x+ecBxK4jFwqWjkdQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244368; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=RDCUlHefqFEbjMztOptVwPQT+i36K/1j1D4kqeoLikQ=; b=uDThBC7dPkhpbUBR8vTkWuQbrEtkKUMQEGZmFnXTVn0ktJD9/xlqDEJMWscc7lvKVt4k dCSxEDdULacSyq/hZOr6LDzjwhv15WOY7Caxno6n5wezq5sB27KZffCvUB3KR9G5fm9O7 mmJeroTx/Q4R/mqL+3iks2e+qpAtc43xOus3uTRxW9ToyJHktJ5xxWUEZ80teuZMjHv8U C2R6EblVJXLckEyB4MPBwEWgb3OwAnyTY/KUl7VhqRh+O6tZ8+k2ayeNE0w7+4wZswC9b +hcUaYCnmJt5EarB0K/ogNEhrv9uVU4Aqhql45HJ2RFLJiQfMYoiszzztk0xcHnHzxg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=AeGFF0ZK; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 573A9606A334 for ; Sat, 21 Nov 2026 06:59:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 573A9606A334 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=AeGFF0ZK Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-341eff9e381so2641288eec.1 for ; Fri, 20 Nov 2026 22:59:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244366; x=1795849166; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=RDCUlHefqFEbjMztOptVwPQT+i36K/1j1D4kqeoLikQ=; b=AeGFF0ZKbz1lnAaz1y7/AWjWr9GiAl4v/7MXQslv906MoFlVh8SK/fpEy1A8KY3Ft/ bH6bE1nZ8It4VFgg6v7IRR7Qxr6PwRS+GWdbDkBG2KoUL2Ex/yxVVtkE35HFuqNPS93l 9o/ySBCQJj982HhDtFJ8ORLS9v92h7lmsgPk4wkQYE7MEEaIxpaJlr0GBtSTm/hXjazS 1LDoJr+r0ew4ZqP480X606btzWDVe7W+gX6O6cTMhxNRsnatpIaGF1IcB8m6ZogmMklB oLDAWJ3wrl666UIfFWzV69oQRzh1h90wdDtEdeT3YGKHo88jKdxM6JgMLJ5l1Wx5jWXz LlfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244366; x=1795849166; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=RDCUlHefqFEbjMztOptVwPQT+i36K/1j1D4kqeoLikQ=; b=hJ7FYOqff4aEPO9NoF2Wtgc/zshtQzhowqExYyT7Xtz9iqVafsRImjAixQiiUiQHn1 yEpWNj/F/ojG0I+9lE9havmHkrnW7aZXwZ0ibqAD4V4yowrIjDhVnbUrMJcMURAbJvPT qGx8XwDL5Xra1fJM15FudsMgOf/LLIhXWIk3bOFK3PH4TLY7/qZqIIq5y4+kbOLczT0U cZhheeu8ymmbM+BD9GOIuanW1lvO/txuQ7EDy99uMCqaIt3+zWKLilVfd6t7jafXSR2j ryfyzJiTDPKDzs3ad3ZZlwBCRxkyOJ6J5kAJQKPaRfcChyboO3mt2U7URH0GSnHIyNYA vNJg== X-Forwarded-Encrypted: i=1; AL+hErFjF6NI9Sn4lOF4XbN8QN2YH9ZGRnIWBhPXsBYyuMUv5hOdazc6kimTbG2DcTKXuOkehhU7V8C4Kfp3pu8=@vger.kernel.org X-Gm-Message-State: APuU4Z8gRcr6krVL8NnteJMVlK2ONddLL5RDMShuuWsMycSj+9hY4XIS 8cTwmwKVyLY1m5tMNkxeZy2smwecoh1+HhL9k14pKeu/pl551y50y9GLl6EByYyJnURpDUbb3zd 3PSAvb+1wXQ== X-Received: from dybtt7.prod.google.com ([2002:a05:7300:f407:b0:369:f864:c5e]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:d0a2:b0:384:df31:6a07 with SMTP id 5a478bee46e88-3857073eeafmr5062590eec.30.1795244366178; Fri, 20 Nov 2026 22:59:26 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:06 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-18-irogers@google.com> Subject: [PATCH v15 17/23] perf config: Add __guarded_by and __must_hold annotations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add __guarded_by() and __must_hold() annotations to static parser and config state in util/config.c. Signed-off-by: Ian Rogers --- tools/perf/util/config.c | 47 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/tools/perf/util/config.c b/tools/perf/util/config.c index 287401f159e6..0e6b77a0a5e7 100644 --- a/tools/perf/util/config.c +++ b/tools/perf/util/config.c @@ -64,15 +64,29 @@ struct perf_stat_config stat_config =3D { =20 char buildid_dir[MAXPATHLEN]; /* root dir for buildid, binary cache */ =20 -static FILE *config_file; -static const char *config_file_name; -static int config_linenr; -static int config_file_eof; -static struct perf_config_set *config_set; +/* Parsing and rewriting share the static parser state. */ +static DEFINE_MUTEX(config_mutex); + +/* Serializes whole perf_config__set_variable() updates. */ +static DEFINE_MUTEX(config_update_mutex); + +/* + * Protects the global config_set. Separate from config_mutex because + * initializing config_set parses the config files, which acquires + * config_mutex. + */ +static DEFINE_MUTEX(config_set_mutex); + +static FILE *config_file __guarded_by(&config_mutex); +static const char *config_file_name __guarded_by(&config_mutex); +static int config_linenr __guarded_by(&config_mutex); +static int config_file_eof __guarded_by(&config_mutex); +static struct perf_config_set *config_set __guarded_by(&config_set_mutex); =20 const char *config_exclusive_filename; =20 static int get_next_char(void) + __must_hold(&config_mutex) { int c; FILE *f; @@ -99,8 +113,9 @@ static int get_next_char(void) } =20 static char *parse_value(void) + __must_hold(&config_mutex) { - static char value[1024]; + static char value[1024] __guarded_by(&config_mutex); int quote =3D 0, comment =3D 0, space =3D 0; size_t len =3D 0; =20 @@ -170,6 +185,7 @@ static inline int iskeychar(int c) } =20 static int get_value(config_fn_t fn, void *data, char *name, unsigned int = len) + __must_hold(&config_mutex) { int c; char *value; @@ -201,6 +217,7 @@ static int get_value(config_fn_t fn, void *data, char *= name, unsigned int len) } =20 static int get_extended_base_var(char *name, int baselen, int c) + __must_hold(&config_mutex) { do { if (c =3D=3D '\n') @@ -237,6 +254,7 @@ static int get_extended_base_var(char *name, int basele= n, int c) } =20 static int get_base_var(char *name) + __must_hold(&config_mutex) { int baselen =3D 0; =20 @@ -257,10 +275,11 @@ static int get_base_var(char *name) } =20 static int perf_parse_file(config_fn_t fn, void *data) + __must_hold(&config_mutex) { int comment =3D 0; int baselen =3D 0; - static char var[MAXNAME]; + static char var[MAXNAME] __guarded_by(&config_mutex); =20 /* U+FEFF Byte Order Mark in UTF8 */ static const unsigned char *utf8_bom =3D (unsigned char *) "\xef\xbb\xbf"; @@ -549,12 +568,6 @@ int perf_default_config(const char *var, const char *v= alue, return 0; } =20 -/* Parsing and rewriting share the static parser state. */ -static DEFINE_MUTEX(config_mutex); - -/* Serializes whole perf_config__set_variable() updates. */ -static DEFINE_MUTEX(config_update_mutex); - static int perf_config_from_file(config_fn_t fn, const char *filename, voi= d *data) { int ret; @@ -737,6 +750,7 @@ static int set_value(struct perf_config_item *item, con= st char *value) =20 static int collect_config(const char *var, const char *value, void *perf_config_set) + __must_hold(&config_mutex) { int ret =3D -1; char *ptr, *key; @@ -853,11 +867,8 @@ struct perf_config_set *perf_config_set__load_file(con= st char *file) return set; } =20 -/* Not config_mutex: building the set parses the config files, which takes= it. */ -static DEFINE_MUTEX(config_set_mutex); - -/* Called with config_set_mutex held. */ static int perf_config__init(void) + __must_hold(&config_set_mutex) { if (config_set =3D=3D NULL) config_set =3D perf_config_set__new(); @@ -975,7 +986,7 @@ int perf_config__set_variable(const char *var, const ch= ar *value) =20 /* Static: the parser publishes it as config_file_name. */ { - static char path[PATH_MAX]; + static char path[PATH_MAX] __guarded_by(&config_update_mutex); char *user_config =3D mkpath(path, sizeof(path), "%s/.perfconfig", geten= v("HOME")); =20 config_filename =3D config_exclusive_filename ?: user_config; --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244374; b=yrEUV4QMBdlgFq7EuKZ/0bcEbbYJzzt+DpVQj6PMsDlIcIsVmctXOfuQ28gkpqZYZfh7 o8HEjnvdhXXgt13YC5gAbCiHY59mcfRxKrWnxTB3BOuUR72ApcQSSdlOqmP/lShsC2o8V dlhO46QmyMwgE+dYUY6iTxKRpnfXTwWZVG+danF02zrsvbPD94zfxMLotH/ZyIBD70mBu stc9P758iJObYvO3hauLTYslDlukOl0xgryrAYMgu5kqONDvNQFLp7bFAz5YpVkE8XcLT tLl8lAqdbs7bTgQ6aQELV64jQBywbHnEdRCLwMhsn4Ugo1hrOXhzBzzebBtI4n2pM1w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244374; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=pze2mI1oPViyxi8zWkxc4AfiCW0ffatK6SkO5RyHMS4=; b=ib594JiWvayTPFPy2Jg+rXAgERt2YOBKI6WCfX8MHgfgxlnGz1sLm8sLnOmO45OSgfhg oMQhJrFOqC1uJjLqefCQDPt8tE82O1oyQ5X6GRXfGLzwfKw1Vk2IsiT0AqR25JJDEFdGM bU2ZPZS+PuWYe39y0AGVHf0/HDKReR1msA2oUUbGEKT/K33p97xh+zJayLcUH8HBRFDKg h6Rb4W7EafbJcpX+S+pZnFrtlypbB05HVxGcaMcaNosRIE20QJLhcJJnbM+/kVf4CB24h iD1Cdrlh/nDAZJ40UM5ZJRAAgrLtEjYlZNE3z/DnCMNx5MwcIz6ryT6lbOaHryRLWmA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Es6Wb+57; arc=none smtp.remote-ip=209.85.214.199 Received: from mail-pl1-f199.google.com (mail-pl1-f199.google.com [209.85.214.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3DC1C606A331 for ; Sat, 21 Nov 2026 06:59:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3DC1C606A331 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Es6Wb+57 Received: by mail-pl1-f199.google.com with SMTP id d9443c01a7336-2ed08904845so23935295ad.3 for ; Fri, 20 Nov 2026 22:59:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244374; x=1795849174; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=pze2mI1oPViyxi8zWkxc4AfiCW0ffatK6SkO5RyHMS4=; b=Es6Wb+57lLQG0eVwUIIjEF2IOr8TMZDW1V7czZcfboMKFdRwUmajtD9sxJ8TscC9+s eKyk7vV2pBjFuEzXED/Zv5LKL4YOy/pWEU24f05LOiRaGp9SKZRjZUquZi8mcQcwbVXU nHFrC7+LA8OZVR45G+B7qR1Gl2QNV5qgqvgbLtAwDDqy5Oq/0/ZGwcj7cvl2KW1xNz0r jAuCkFcHn3vrEwECJK8wn09KFnzpdYQ3Y84WDgptcGeMHkBt8poZm6r1n3J1JaLSKKuE oW1rSbS75jl5N6YmvxjBQBmOQ8mCby0V37mGPz61dbnPSZ137KQOz/221rAHNvJpsImu 9PWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244374; x=1795849174; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=pze2mI1oPViyxi8zWkxc4AfiCW0ffatK6SkO5RyHMS4=; b=F7G0bHxqLZfSZiphj5ZQtpmEUt8L1i7NLEJIFbazVEDWHCCBxS2U6OXRSEW+9on80C p8UUAIeCoUJGL85z7xEaOdIsXgcP5Ctbc5EeYVaUkqCsQunbwB0ptVNuYGA0EyYtvxXa 5+sE1182xeIqc292hWJ8lZIet3c/8sVU6NiE4BN9gR/zh4Yzl2oa5TRNFpNsDv4UjQKd 6AqIDIYRrrQCnfJXZ8y8YgX+PKQqbQdTvkyZIqy3p6fQyGTiVZyiAlkPmkufAOChsnI1 Rvwx+7b/d2lO4BNB5dUU+hUdNo540w5AQwv4winl7gQNPx/BCgbnXSOxJFdChIhLfKc7 dJAg== X-Forwarded-Encrypted: i=1; AL+hErHJm3zvm0a61WhXpM1bSRuhQzMAgfjzoLhzlUfOh5hZnhBc9XzgYleRa4a0BdzzM6PSPyCnf2x0fsEWOaU=@vger.kernel.org X-Gm-Message-State: APuU4Z9MMCgrKZ4+BuIsvplD2+s4A38tL31BJXwXydxuUgVhJzndumKF vTyvfX3/2z3qrGyNv0yPNdPESZGTChgTkZJX/5cqbnb3Ob36/wx7WJAraLszxo9lA+2cDrCTt8k GDb7t8YVJzQ== X-Received: from plbmq7.prod.google.com ([2002:a17:902:fd47:b0:2ef:ceca:4005]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1a0e:b0:2ee:1c51:7eaf with SMTP id d9443c01a7336-2f03f20b203mr57198655ad.31.1795244373110; Fri, 20 Nov 2026 22:59:33 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:07 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-19-irogers@google.com> Subject: [PATCH v15 18/23] perf env: Add __guarded_by and __must_hold annotations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add __guarded_by(), __must_hold(), and __must_hold_shared() annotations to the bpf_progs and cgroups trees and helpers in struct perf_env. Signed-off-by: Ian Rogers --- tools/perf/util/bpf-event.h | 5 +++-- tools/perf/util/env.c | 1 + tools/perf/util/env.h | 19 +++++++++++-------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/tools/perf/util/bpf-event.h b/tools/perf/util/bpf-event.h index da4eeb4a1a73..3f3323f98f49 100644 --- a/tools/perf/util/bpf-event.h +++ b/tools/perf/util/bpf-event.h @@ -6,11 +6,11 @@ #include #include #include +#include "env.h" =20 struct bpf_prog_info; struct machine; union perf_event; -struct perf_env; struct perf_sample; struct perf_session; struct record_opts; @@ -42,7 +42,8 @@ int machine__process_bpf(struct machine *machine, union p= erf_event *event, int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env); void __bpf_event__print_bpf_prog_info(struct perf_bpil *info_linear, struct perf_env *env, - FILE *fp); + FILE *fp) + __must_hold_shared(&env->bpf_progs.lock); void bpf_metadata_free(struct bpf_metadata *metadata); #else static inline int machine__process_bpf(struct machine *machine __maybe_unu= sed, diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c index 50993b0dedc5..9e8e5d1d822a 100644 --- a/tools/perf/util/env.c +++ b/tools/perf/util/env.c @@ -302,6 +302,7 @@ void perf_env__exit(struct perf_env *env) } =20 void perf_env__init(struct perf_env *env) + __context_unsafe(/* initialization */) { memset(env, 0, sizeof(*env)); #ifdef HAVE_LIBBPF_SUPPORT diff --git a/tools/perf/util/env.h b/tools/perf/util/env.h index 23d35425efe3..6b2083e3640c 100644 --- a/tools/perf/util/env.h +++ b/tools/perf/util/env.h @@ -139,16 +139,16 @@ struct perf_env { */ struct { struct rw_semaphore lock; - struct rb_root infos; - u32 infos_cnt; - struct rb_root btfs; - u32 btfs_cnt; + struct rb_root infos __guarded_by(&lock); + u32 infos_cnt __guarded_by(&lock); + struct rb_root btfs __guarded_by(&lock); + u32 btfs_cnt __guarded_by(&lock); } bpf_progs; #endif // HAVE_LIBBPF_SUPPORT /* same reason as above (for perf-top) */ struct { struct rw_semaphore lock; - struct rb_root tree; + struct rb_root tree __guarded_by(&lock); } cgroups; =20 /* For fast cpu to numa node lookup via perf_env__numa_node */ @@ -223,7 +223,8 @@ int perf_env__nr_cpus_avail(struct perf_env *env); void perf_env__init(struct perf_env *env); #ifdef HAVE_LIBBPF_SUPPORT bool __perf_env__insert_bpf_prog_info(struct perf_env *env, - struct bpf_prog_info_node *info_node); + struct bpf_prog_info_node *info_node) + __must_hold(&env->bpf_progs.lock); bool perf_env__insert_bpf_prog_info(struct perf_env *env, struct bpf_prog_info_node *info_node); struct bpf_prog_info_node *perf_env__find_bpf_prog_info(struct perf_env *e= nv, @@ -233,9 +234,11 @@ void perf_env__iterate_bpf_prog_info(struct perf_env *= env, void *data), void *data); bool perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_node); -bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_nod= e); +bool __perf_env__insert_btf(struct perf_env *env, struct btf_node *btf_nod= e) + __must_hold(&env->bpf_progs.lock); struct btf_node *perf_env__find_btf(struct perf_env *env, __u32 btf_id); -struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id); +struct btf_node *__perf_env__find_btf(struct perf_env *env, __u32 btf_id) + __must_hold_shared(&env->bpf_progs.lock); #endif // HAVE_LIBBPF_SUPPORT =20 int perf_env__numa_node(struct perf_env *env, struct perf_cpu cpu); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244380; b=GeEWXfSmlqVVcplwmLJ4Wjf4P+uJPuULEFBr10Eb3s3RE8EzmEp3syJzwZHMxcpVCzIA C3j0IWEvneU2ZnrTz9a8P9RjxC8hlhW0cJWLf7MsdMnxHGD1BM3e+w7j81yMfhm/+dCwC gYpXvcN7nxytxvl8jHQ6utJSb9RxdISxmKcSvp9NSc+Ew68blRSVvYVSZ/4MkHU5iOGiK u++L52A+LZj9usQG5p30SHAokVkjnc2thhkxDTw0EiSlgied4wCN+AYDm1oeI3xoFZLXH TYYcV3qHgfW7DTXjxfNBY6YewC7URJUwnOcM0wjZwLd5IyGEtF7l0J0uIn2LFhgMyxQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244380; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=KOONOUBRea55D4L/MhBbf8K2lc+JFi0aqBhiCgSX6Yw=; b=smpdulE1W81r0AHw2aCAyn6egp5V0NtAHgyHKG6exHso/UJSWGrc+BmKgKnYGKLALEoM hNSWW9DuTBs9wgLcCe32q4J4oxEVGFwX/Q4ijTMrEWkKsuLoPQhx+tGUaNC+66T2ZILB7 hzTMhGZSh1XP+pN+lp7bkK/1IEk8wXl5HvDjolS0hH3u+fjOxOAHCepc+Ua1Hm6Aen8lj gb4jtOQk9dLwJTcCLOZlfDgRhJwk21KOCJnC92Z8IW0MsxsjpGD+xqfJwmrz5KyFH1bS5 zS5iBarpnQvqE4b0XC39CUfVT3KNwI0b4sDxkC7hLLTJRyhe2GxvXVvzLDORd4GUErA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=XTr5xx8D; arc=none smtp.remote-ip=209.85.210.71 Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5672606A332 for ; Sat, 21 Nov 2026 06:59:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B5672606A332 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=XTr5xx8D Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-862ad1e3959so1186407a34.2 for ; Fri, 20 Nov 2026 22:59:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244379; x=1795849179; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=KOONOUBRea55D4L/MhBbf8K2lc+JFi0aqBhiCgSX6Yw=; b=XTr5xx8Dnfe+l2AuL3AwIcShg6lt/rcTnFHr7wqhNtlP45dfH3p2umXPfPwGVvSKLk QKZyt4dQ9J4pEdjvz/t/AlS14D+x0aaAjSFJ77CtBLSzQ9TkPLMlwXHd8kmdju6GQTo4 oIKvi/FuH1/jq3SXIMokDR5wHPvPHi2HH+cupOO1L+plr9zdvsYqwck2QodoiJHLQJvT sdC0ZyskYnIG0zMxw+NrmEBJEN2JYMEfXqLY7V51vLbAe9mD9qGfjTSPsbAHFCOjcWJG qyt0m56DUrdlqy6QRlm7pOWVKQlNYk5S9x1g+UKDEUv3y7JJiB3OJz4wO2mnHQJxqYJP k0PQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244379; x=1795849179; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KOONOUBRea55D4L/MhBbf8K2lc+JFi0aqBhiCgSX6Yw=; b=iT5g5fWQj+eBWmqNQtjf4nX5jbrVGLtVxHIDIpa+XNTy1KpQNYSm25hqlNKmhcnhTd 4qMoRAIX3TyL4YV03pzj9d8fexQROGd0GC+k/4p4D/xdKAOOLCFwNq6ygaLs4Tq2bGpb owRFHL34Mww98D0GKkdkaR57XWweenYvf8dGeWSHNrRdHi+ksUpyONquuIuwWOo6qanM ZurXzISFxQUUxIQ5Su4v2wNPNyQ7LzyoTifhXRZu/jLI10Hz6m9A8VGJ7Mj8r6veLbTh n/TX8bT7E/0/eenERB/0L3jUAsGmYArLZYaaJGlEqgvga3/BN6chs/vARcmkUFsR+XJK ydzA== X-Forwarded-Encrypted: i=1; AL+hErFtolWjbhga/FPngCjto+eZk0t+VVms6fgYzCPzO1OLDDleu/cBeREScY/dMexBK00M85tMtGIRs06QzW8=@vger.kernel.org X-Gm-Message-State: APuU4Z9YMc36qEqB41xL0JJFSoXGucbKxb/7wvfdJDriig+DO9bZT4b0 gzhwViwneK9T9sdDbMeo4ylZ+W1S/7C4qgQWvXm2V1Qc1v4SOWA1CO18klqI11L9FbNiO8rqHku ugU2LoPM+zQ== X-Received: from iobmr2-n2.prod.google.com ([2002:a05:6602:a542:20b0:9e2:221a:fd83]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:1a02:b0:572:2919:eba3 with SMTP id 5614622812f47-57a0ccdebf8mr6144982b6e.67.1795244378377; Fri, 20 Nov 2026 22:59:38 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:08 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-20-irogers@google.com> Subject: [PATCH v15 19/23] perf maps: Remove unused maps__load_first() From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 553d18c98a89 ("perf lock contention: Load kernel map before lookup") removed the last caller of maps__load_first(). Remove the unused function and its declaration. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 10 ---------- tools/perf/util/maps.h | 2 -- 2 files changed, 12 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 675fe0223faa..496a592284c5 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -1457,13 +1457,3 @@ int maps__merge_in(struct maps *kmaps, struct map *n= ew_map) up_write(maps__lock(kmaps)); return 0; } - -void maps__load_first(struct maps *maps) -{ - down_read(maps__lock(maps)); - - if (maps__nr_maps(maps) > 0) - map__load(maps__maps_by_address(maps)[0]); - - up_read(maps__lock(maps)); -} diff --git a/tools/perf/util/maps.h b/tools/perf/util/maps.h index 4ec9b7453a3b..4e6eff244125 100644 --- a/tools/perf/util/maps.h +++ b/tools/perf/util/maps.h @@ -83,6 +83,4 @@ int maps__merge_in(struct maps *kmaps, struct map *new_ma= p); =20 void maps__fixup_end(struct maps *maps); =20 -void maps__load_first(struct maps *maps); - #endif // __PERF_MAPS_H --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.70 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244382; b=RRYLkwSYoaZp2q5PheMyKFEF1mrYxryM5YubZSrQW13LfVYlEA2xCp7lmjeY96dFPbHj 13mOg1p6zCzRZqFen6CDpkMuIPg47wddJs21hzzosgO9YwXWR+SLsG99XDHlHFW6FMKCR wzMGFiXrMu1lNuqG/LH0uIwmmYn7t3OPcUtlA21zDOP6eUazeTkHMCFbVFpCZbAItC//2 25Aa0ecmxx6SJOb05QBVhKp1U6xOe4X8njhNDMydwFsVJemAG7iGLEpsU5VWIsVl+V60N IzVWlN84DCbEuZfCgr8dAdjFuV4+07BfrDcn/f/t1R7EmrTqIB2cJoaCR8S8cH6mCKg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244382; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=3ldaH1DsummI25gX35EZsyDmjsqnnx62kQYSRGas9Bc=; b=Mel6b45SkrGzZsKdj7CEtZGoSYXO0kjeW7GwR15fGgPNAYaIEafmDyqNUlHdIyE9UKza 57cumSQsQanFqJjc5dG8wRuf4E8JUiiKn39C8VPXCuMPPF+lw0MRlSKssZJGG1a2pfxK+ 4vy0UhlqT3H8I3i6n+lo/QcnVuNyg62d3+CekRLPitJdXpcLAiUtwZ6IftOueCQSFwy2A oI4LIwG9y9CiVU8JOSUFxC9IMLHZ/E796vOnkGgz5kTXmejGE6qJOC+LPacI/5oX5Gj2P 3Jgq5JUEIwZJEJ/1Dc2ghrbYN5g4iLIW/Uakh8LYmPE4PvAZo+wzJ3kg6tVeF/Ggy0g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=cIMq7voa; arc=none smtp.remote-ip=209.85.160.70 Received: from mail-oa1-f70.google.com (mail-oa1-f70.google.com [209.85.160.70]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4F78E606A337 for ; Sat, 21 Nov 2026 06:59:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4F78E606A337 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=cIMq7voa Received: by mail-oa1-f70.google.com with SMTP id 586e51a60fabf-45e6091d8a1so1338461fac.1 for ; Fri, 20 Nov 2026 22:59:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244381; x=1795849181; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=3ldaH1DsummI25gX35EZsyDmjsqnnx62kQYSRGas9Bc=; b=cIMq7voaQeCHhfzcptVQ1YdpOt+CBKj4cXe3JwGlFetNcrcaHT7GI4ZqK2qWQ+nAhs revMUeAH7BKvHz1tvZXyRPlyjkXC0K81AtTgyTKcaLFBxkpR2aHssKTDFRNfhkaEYq6k rs7s50WKbKz78DSFDj3oDqJXZCqmt1NAvsThOuEu4csLMKA8jK80Bm8RN7GkNDok7L7R juU629LzPabLVfKtA7byg5qIOA+5IoGvnsPjbRV5QPRWi4udn1qQyse7PidrJ+te+n1r ALhdleQJkNe4wWJpzBJcoVZntR68js0PHRGeLdG1OdJ+Hx6tUXUMGpRViI96ZKogw7o8 14lA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244381; x=1795849181; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=3ldaH1DsummI25gX35EZsyDmjsqnnx62kQYSRGas9Bc=; b=YyAlnsY/FtPAJLR26vGYUu5aDD8xgx+mubMBrpHbKxVhozKcEHVQsVWXlk05BtT/Pf G2mAvPbkb+wimO7y3nAVuWLw0uAi98++RdCVeF4qhORwKb/QXwjsAjnLeVoAUu+hz4vV UPQCy5EWrqsgfjfvMnANXgMlRmVfRV7Fh0HGAsvO7ajNMvaxFhSgRjesTYg3567lW3gy ZhJ3u8ObvV7wgPdatGfoSptbNPlH4Wqsk+LUd4UelKgWqKQ/RPWpz1pMYd5Fpa3Derxw 9/SIyDPKrbLN6G2X65ZotnVP9DkrCJH94g79Wy6AGhpboxougkhfSVP3wvx3ljAe3/h0 Z6dg== X-Forwarded-Encrypted: i=1; AL+hErE39jMu27a0wAyQ0r7QI/xYVm2qoD8ocx4mhCObj0Hi6bSGWQlG5fRNyYi9ebmGNBqUn7Y5zSBPdDN4QjM=@vger.kernel.org X-Gm-Message-State: APuU4Z/Xx+mBUNJ+Wg4Hbkc7Bj22/vha6qonZbemePP0SNwc02qdDSSX MFdIFoE8OpO9a/uhZ7aJonQxTgzM1Gcz5B7QJToGllJ5nOroPNMtIJG6PTCZ+Q356MPuPDhz6Vc yzxijlA7Llw== X-Received: from iofh2.prod.google.com ([2002:a05:6602:6fc2:b0:9c3:dedd:2a97]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:1992:b0:579:f56e:45a2 with SMTP id 5614622812f47-57a0bc8c353mr6204162b6e.53.1795244381098; Fri, 20 Nov 2026 22:59:41 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:09 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-21-irogers@google.com> Subject: [PATCH v15 20/23] perf maps: Fix map index lookup fallback and removal From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" maps__by_address_index() and maps__by_name_index() currently only run their linear pointer scan when maps_by_address_sorted or maps_by_name_sorted is false, returning -1 (UINT_MAX) immediately if bsearch() returns NULL. When a map's start/end address or DSO short_name is updated in place without clearing the sorted flags (for example during module map setup), map__start_cmp() or map__strcmp() tie-breakers can become stale and cause bsearch() to miss an entry that is present in the array. Fix the lookup and removal paths so stale sort keys or missing maps are handled safely: - Fall back to the linear pointer scan in maps__by_address_index() and maps__by_name_index() when bsearch() returns NULL, and avoid calling bsearch() on empty or NULL arrays. - Factor out __maps__remove_at() with bounds checks on address_idx and name_idx (freeing maps_by_name if out of bounds) and remove from maps_by_name before calling map__put() on the maps_by_address entry so REFCNT_CHECKING does not see a freed handle. Have maps__remove_maps() call __maps__remove_at(maps, i) directly so it removes at index i in O(1) without redundant lookup or risking an infinite loop if lookup failed. - Guard against maps__by_address_index() returning -1 wrapping ++i to 0 in maps__find_next_entry(). - Clear maps_by_name_sorted and re-sort maps_by_address when maps__fixup_end() clamps equal-start maps, and use machine__update_kernel_mmap() instead of the unlocked machine__set_kernel_mmap() in machine__create_kernel_maps(). Add a unit test in tests/maps.c covering maps__remove(), maps__remove_maps(), maps__fixup_end(), and maps__find_next_entry(). Fixes: 659ad3492b91 ("perf maps: Switch from rbtree to lazily sorted array = for addresses") Signed-off-by: Ian Rogers --- tools/perf/tests/maps.c | 140 ++++++++++++++++++++++++++++++++++++++ tools/perf/util/machine.c | 21 ++---- tools/perf/util/maps.c | 91 ++++++++++++++++--------- 3 files changed, 207 insertions(+), 45 deletions(-) diff --git a/tools/perf/tests/maps.c b/tools/perf/tests/maps.c index ff6d25f9c043..054e4fafef3f 100644 --- a/tools/perf/tests/maps.c +++ b/tools/perf/tests/maps.c @@ -1,5 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 #include +#include #include #include #include @@ -253,9 +254,148 @@ static int test__maps__fixup_overlap_and_insert(struc= t test_suite *t __maybe_unu return TEST_OK; } =20 +static bool remove_not_keep_cb(struct map *map, void *data) +{ + const char *keep_name =3D data; + + return strcmp(dso__name(map__dso(map)), keep_name) !=3D 0; +} + +static int check_remove_after_end_update(bool swap_order, bool use_by_name, + bool use_fixup_end, bool use_remove_maps) +{ + struct perf_env host_env; + struct machine *machine; + struct maps *maps; + struct map *map_a =3D NULL, *map_b =3D NULL, *map_keep =3D NULL, *missing= =3D NULL; + struct map *map_first, *map_second, *found; + struct map_def expected_after_first[] =3D { + { "map_same", 1000, use_fixup_end ? 1000 : 1100 }, + { "z_keep", 1000, 1300 }, + }; + struct map_def expected_after_remove_maps[] =3D { + { "z_keep", 1000, 1300 }, + }; + int ret =3D TEST_FAIL; + + perf_env__init(&host_env); + machine =3D machine__new_live(&host_env, /*kernel_maps=3D*/false, getpid(= )); + maps =3D maps__new(machine); + if (!machine || !maps) + goto out_env; + + map_a =3D dso__new_map("map_same"); + map_b =3D dso__new_map("map_same"); + map_keep =3D dso__new_map("z_keep"); + missing =3D dso__new_map("missing"); + if (!map_a || !map_b || !map_keep || !missing) + goto out_maps; + + map_first =3D swap_order ? map_b : map_a; + map_second =3D swap_order ? map_a : map_b; + + map__set_start(map_first, 1000); + map__set_end(map_first, 1100); + map__set_start(map_second, 1000); + map__set_end(map_second, 1200); + map__set_start(map_keep, 1000); + map__set_end(map_keep, 1300); + + if (maps__insert(maps, map_first) || + maps__insert(maps, map_second) || + maps__insert(maps, map_keep)) + goto out_maps; + + /* Trigger sorting of maps_by_address, and optionally maps_by_name. */ + found =3D maps__find(maps, 1000); + map__zput(found); + if (use_by_name) { + found =3D maps__find_by_name(maps, "z_keep"); + map__zput(found); + } + + /* Missing map must not wrap around to index 0 in maps__find_next_entry()= . */ + found =3D maps__find_next_entry(maps, missing); + if (found) { + map__zput(found); + goto out_maps; + } + + if (use_fixup_end) { + maps__fixup_end(maps); + } else { + /* + * Simulate in-place end adjustment (as in module map setup), + * which can invert the pointer tie-breaker in map__start_cmp(). + */ + map__set_end(map_second, 1100); + } + + if (use_remove_maps) { + map__zput(map_a); + map__zput(map_b); + map__zput(map_keep); + maps__remove_maps(maps, remove_not_keep_cb, (void *)"z_keep"); + if (use_by_name) { + found =3D maps__find_by_name(maps, "z_keep"); + if (found && maps__nr_maps(maps) =3D=3D 1) + ret =3D TEST_OK; + map__zput(found); + } else { + ret =3D check_maps(expected_after_remove_maps, + ARRAY_SIZE(expected_after_remove_maps), maps); + } + } else { + maps__remove(maps, map_first); + map__zput(map_a); + map__zput(map_b); + map__zput(map_keep); + if (use_by_name) { + found =3D maps__find_by_name(maps, "z_keep"); + if (found && maps__nr_maps(maps) =3D=3D 2) + ret =3D TEST_OK; + map__zput(found); + } else { + ret =3D check_maps(expected_after_first, + ARRAY_SIZE(expected_after_first), maps); + } + } + +out_maps: + map__zput(map_a); + map__zput(map_b); + map__zput(map_keep); + map__zput(missing); +out_env: + maps__zput(maps); + machine__delete(machine); + perf_env__exit(&host_env); + return ret; +} + +static int test__maps__remove(struct test_suite *t __maybe_unused, int sub= test __maybe_unused) +{ + for (int fixup =3D 0; fixup <=3D 1; fixup++) { + for (int by_name =3D 0; by_name <=3D 1; by_name++) { + for (int swap =3D 0; swap <=3D 1; swap++) { + TEST_ASSERT_VAL("maps__remove failed", + check_remove_after_end_update(swap, by_name, + fixup, false) =3D=3D + TEST_OK); + TEST_ASSERT_VAL("maps__remove_maps failed", + check_remove_after_end_update(swap, by_name, + fixup, true) =3D=3D + TEST_OK); + } + } + } + return TEST_OK; +} + static struct test_case tests__maps[] =3D { TEST_CASE("Test merge_in interface", maps__merge_in), TEST_CASE("Test fix up overlap interface", maps__fixup_overlap_and_insert= ), + TEST_CASE("Test remove interface", maps__remove), { .name =3D NULL, } }; =20 diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 02711995f0b6..0cc593d5372a 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -1580,19 +1580,6 @@ static int machine__create_modules(struct machine *m= achine) return 0; } =20 -static void machine__set_kernel_mmap(struct machine *machine, - u64 start, u64 end) -{ - map__set_start(machine->vmlinux_map, start); - map__set_end(machine->vmlinux_map, end); - /* - * Be a bit paranoid here, some perf.data file came with - * a zero sized synthesized MMAP event for the kernel. - */ - if (start =3D=3D 0 && end =3D=3D 0) - map__set_end(machine->vmlinux_map, ~0ULL); -} - struct kernel_mmap_mutation_ctx { u64 start; u64 end; @@ -1604,6 +1591,10 @@ static int kernel_mmap_mutate_cb(struct map *map, vo= id *data) =20 map__set_start(map, ctx->start); map__set_end(map, ctx->end); + /* + * Be a bit paranoid here, some perf.data file came with + * a zero sized synthesized MMAP event for the kernel. + */ if (ctx->start =3D=3D 0 && ctx->end =3D=3D 0) map__set_end(map, ~0ULL); return 0; @@ -1668,8 +1659,10 @@ int machine__create_kernel_maps(struct machine *mach= ine) machine__kernel_map(machine)); =20 if (next) { - machine__set_kernel_mmap(machine, start, map__start(next)); + ret =3D machine__update_kernel_mmap(machine, start, map__start(next)); map__put(next); + if (ret < 0) + goto out_put; } } =20 diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 496a592284c5..2279d8e3edc0 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include #include "debug.h" #include "dso.h" @@ -411,19 +412,19 @@ static int maps__sort_by_name(struct maps *maps) static unsigned int maps__by_address_index(const struct maps *maps, const = struct map *map) { struct map **maps_by_address =3D maps__maps_by_address(maps); + unsigned int nr_maps =3D maps__nr_maps(maps); =20 - if (maps__maps_by_address_sorted(maps)) { + if (nr_maps && maps__maps_by_address_sorted(maps)) { struct map **mapp =3D - bsearch(&map, maps__maps_by_address(maps), maps__nr_maps(maps), + bsearch(&map, maps_by_address, nr_maps, sizeof(*mapp), map__start_cmp); =20 if (mapp) return mapp - maps_by_address; - } else { - for (unsigned int i =3D 0; i < maps__nr_maps(maps); i++) { - if (RC_CHK_ACCESS(maps_by_address[i]) =3D=3D RC_CHK_ACCESS(map)) - return i; - } + } + for (unsigned int i =3D 0; i < nr_maps; i++) { + if (RC_CHK_ACCESS(maps_by_address[i]) =3D=3D RC_CHK_ACCESS(map)) + return i; } pr_err("Map missing from maps"); return -1; @@ -432,19 +433,22 @@ static unsigned int maps__by_address_index(const stru= ct maps *maps, const struct static unsigned int maps__by_name_index(const struct maps *maps, const str= uct map *map) { struct map **maps_by_name =3D maps__maps_by_name(maps); + unsigned int nr_maps =3D maps__nr_maps(maps); + + if (!maps_by_name) + return -1; =20 - if (maps__maps_by_name_sorted(maps)) { + if (nr_maps && maps__maps_by_name_sorted(maps)) { struct map **mapp =3D - bsearch(&map, maps_by_name, maps__nr_maps(maps), + bsearch(&map, maps_by_name, nr_maps, sizeof(*mapp), map__strcmp); =20 if (mapp) return mapp - maps_by_name; - } else { - for (unsigned int i =3D 0; i < maps__nr_maps(maps); i++) { - if (RC_CHK_ACCESS(maps_by_name[i]) =3D=3D RC_CHK_ACCESS(map)) - return i; - } + } + for (unsigned int i =3D 0; i < nr_maps; i++) { + if (RC_CHK_ACCESS(maps_by_name[i]) =3D=3D RC_CHK_ACCESS(map)) + return i; } pr_err("Map missing from maps"); return -1; @@ -540,32 +544,44 @@ int maps__insert(struct maps *maps, struct map *map) return ret; } =20 -static void __maps__remove(struct maps *maps, struct map *map) +static void __maps__remove_at(struct maps *maps, unsigned int address_idx) { struct map **maps_by_address =3D maps__maps_by_address(maps); struct map **maps_by_name =3D maps__maps_by_name(maps); unsigned int nr_maps =3D maps__nr_maps(maps); - unsigned int address_idx; + struct map *map; =20 - /* Slide later mappings over the one to remove */ - address_idx =3D maps__by_address_index(maps, map); - map__put(maps_by_address[address_idx]); - memmove(&maps_by_address[address_idx], - &maps_by_address[address_idx + 1], - (nr_maps - address_idx - 1) * sizeof(*maps_by_address)); + if (address_idx >=3D nr_maps) + return; =20 + /* Slide later mappings over the one to remove */ + map =3D maps_by_address[address_idx]; if (maps_by_name) { unsigned int name_idx =3D maps__by_name_index(maps, map); =20 - map__put(maps_by_name[name_idx]); - memmove(&maps_by_name[name_idx], - &maps_by_name[name_idx + 1], - (nr_maps - name_idx - 1) * sizeof(*maps_by_name)); + if (name_idx < nr_maps) { + map__put(maps_by_name[name_idx]); + memmove(&maps_by_name[name_idx], + &maps_by_name[name_idx + 1], + (nr_maps - name_idx - 1) * sizeof(*maps_by_name)); + } else { + __maps__free_maps_by_name(maps); + } } =20 + map__put(map); + memmove(&maps_by_address[address_idx], + &maps_by_address[address_idx + 1], + (nr_maps - address_idx - 1) * sizeof(*maps_by_address)); + --RC_CHK_ACCESS(maps)->nr_maps; } =20 +static void __maps__remove(struct maps *maps, struct map *map) +{ + __maps__remove_at(maps, maps__by_address_index(maps, map)); +} + void maps__remove(struct maps *maps, struct map *map) { down_write(maps__lock(maps)); @@ -714,7 +730,7 @@ void maps__remove_maps(struct maps *maps, bool (*cb)(st= ruct map *map, void *data maps_by_address =3D maps__maps_by_address(maps); for (unsigned int i =3D 0; i < maps__nr_maps(maps);) { if (cb(maps_by_address[i], data)) { - __maps__remove(maps, maps_by_address[i]); + __maps__remove_at(maps, i); removed =3D true; } else { i++; @@ -1320,7 +1336,7 @@ struct map *maps__find_by_name(struct maps *maps, con= st char *name) =20 struct map *maps__find_next_entry(struct maps *maps, struct map *map) { - unsigned int i; + unsigned int i, nr_maps; struct map *result =3D NULL; =20 down_read(maps__lock(maps)); @@ -1330,7 +1346,8 @@ struct map *maps__find_next_entry(struct maps *maps, = struct map *map) down_read(maps__lock(maps)); } i =3D maps__by_address_index(maps, map); - if (++i < maps__nr_maps(maps)) + nr_maps =3D maps__nr_maps(maps); + if (i < nr_maps && ++i < nr_maps) result =3D map__get(maps__maps_by_address(maps)[i]); =20 up_read(maps__lock(maps)); @@ -1341,6 +1358,7 @@ void maps__fixup_end(struct maps *maps) { struct map **maps_by_address; unsigned int n; + bool revalidate_sort =3D false; =20 down_write(maps__lock(maps)); if (!maps__maps_by_address_sorted(maps)) @@ -1352,16 +1370,27 @@ void maps__fixup_end(struct maps *maps) struct map *prev =3D maps_by_address[i - 1]; struct map *curr =3D maps_by_address[i]; =20 - if (!map__end(prev) || map__end(prev) > map__start(curr)) + if (!map__end(prev) || map__end(prev) > map__start(curr)) { map__set_end(prev, map__start(curr)); + if (map__start(prev) =3D=3D map__start(curr)) + revalidate_sort =3D true; + maps__set_maps_by_name_sorted(maps, false); + } } =20 /* * We still haven't the actual symbols, so guess the * last map final address. */ - if (n > 0 && !map__end(maps_by_address[n - 1])) + if (n > 0 && !map__end(maps_by_address[n - 1])) { map__set_end(maps_by_address[n - 1], ~0ULL); + maps__set_maps_by_name_sorted(maps, false); + } + + if (revalidate_sort) { + maps__set_maps_by_address_sorted(maps, false); + __maps__sort_by_address(maps); + } =20 RC_CHK_ACCESS(maps)->ends_broken =3D false; check_invariants(maps); --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244386; b=4kdnrOgm6xCITe+78A6qzWMFT/56gjqycu0kjSGnXMPW5HLHTWGeW1sEu9tzwo0jaIB4 LVdzligDR+uoz8cleLIUWkmOKF9erfJvMHcQPkXY+XJ23tcAB+hR9HMhbW3AvZ+mTRzc3 WPrD7mqSA3xdbD/b+1e9QDlReC+Pcguk32nXjEg7hujfaI01wi9atBwBSXlHY1kk6ah0y 38U7YTP/Az41kFnAIdS1r4tHRHMW7skq+FlTf96lKivGOVQDVsRR79ym7NP8XxXrinuR5 LF7GxXaWcINNJRI1K8RWgfmU+9UbQwvRC5XYQ3fDOtckG57YJqQPzD3AjY8bZOpRh+g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244386; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=KKCHmH2pVdOE54PhP32y0azMSK1f9AbECq6ur+8bzME=; b=CCVy90qoob9f4mykYtYGigff9Hz4N5o80CfqLAzXEa5+aLdp7T9PnqUZGMHYDHMdWGDI HegVAKZnleY8kLX/T+94XYYwdjOLKpjEqG6Ca9m4TtDIDQj8757qWVgOb/cJt+ya+vSIr gLFkM2f/H0OAbVOgp9dQwbaFET403FxYOajH1Hr6HZEDTzWpWX7C8fAjII+VZeLLxOPZy tZ9TsxsZC3SI3ujiNSQrtBqP40JvUoeiGMqQCR2q53WzC09rAToha/cY4xtLu6j7rYivu j1Xwu9uwZyf9tLbj6dO/cKJ3EssxJ2uThDbKa5gSFUSaN3YPZz9F7oYIABAGSBztDHA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=XcgLXmWJ; arc=none smtp.remote-ip=209.85.167.199 Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40806606A334 for ; Sat, 21 Nov 2026 06:59:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 40806606A334 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=XcgLXmWJ Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-4e778acdbc6so2266447b6e.3 for ; Fri, 20 Nov 2026 22:59:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244384; x=1795849184; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=KKCHmH2pVdOE54PhP32y0azMSK1f9AbECq6ur+8bzME=; b=XcgLXmWJDKvYWVoiloPcxAbvl8qdOFIsYcrxnh+gIszGhvFm4QrBkYGZVzK4AL10wn eHaQAEvf0DbfEAHjvitubeUfL1W1Gfb1rcE2HUcIqbF7mq0XU4LG/yU5hy0UXfxcUCBT G9MyOlxC2J9EOPeegzJg36sAczzoJefTcestThBLgF7dvhyWME7L3qUYRASqpTcW3Az+ vl1F/jbDddWaS2pm1xEoSqEAFB18OhpvxmTsqGXh1Bw5Mnhaq/gg8bs0TVwz0pQT/h+V MzaYX+LuIZHOEOUWwGrhgUd9TrB6tbIDf2THZcJ2Lkqb+f/oYvGUUgWrpqgVLNVmb6SX q49A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244384; x=1795849184; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=KKCHmH2pVdOE54PhP32y0azMSK1f9AbECq6ur+8bzME=; b=d2yfxbQsmRGUA229FJ9iFqPgRnW8nzrtvHofJVGX8s/m6Y/93WJ4AwIQmraSELsbf0 E2/KElxJrl+llN6C6KusIbA5LjgWJehmKSn+4stgnKdMSKLzBwSEVZJ6VJJ7ESRdDrtu rjDQTaY/LNUgm9dFvz7JLmMwJpz3qhPrdEvi2kluFdNVF8ou4oUR5T0V+DRy3e9xDm9T p8FbxuoMh/XtKu9OnuAI0cLR34FG5CxznVeoLULQnQqU84YBMwmBExPDWpSBxPFULZU7 VZChWMQ2t9YGn72KkRrp2JlNg/lKiNLQ61xcsquhQQrgET20YgLG9UPQHkoZJrlXk3W/ MUTA== X-Forwarded-Encrypted: i=1; AL+hErFhzIqJXCSyvsKmZsdwGceIE76yheUj+qMkqifvGd9kw3lLBYkEI1kUweQMa/5GTvPytnI4i3F6QEIbjc0=@vger.kernel.org X-Gm-Message-State: APuU4Z+OAx8DTPgNuiU1QDhCl5ohPeWKXXQ4BLCbND+fqvQq/fw7ttvS as+uGTHkJbRsWLjLd88/IjACsajpLHyH4WlCCSeu9oOThgT753BmDVtq8AvMtssrFBQjgTnascq WEUwDKjO7Ag== X-Received: from oigo23.prod.google.com ([2002:a05:6808:8897:b0:55e:c7d9:a271]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:50ab:b0:4ef:2584:78a3 with SMTP id 5614622812f47-57a0c09be08mr6670317b6e.52.1795244383777; Fri, 20 Nov 2026 22:59:43 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:10 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-22-irogers@google.com> Subject: [PATCH v15 21/23] perf maps: Add context analysis annotations From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add __returns_ctx_lock() to maps__lock(), annotate DECLARE_RC_STRUCT(maps) members protected by lock with __guarded_by(&lock), and annotate internal maps helpers with __must_hold() and __must_hold_shared(). Signed-off-by: Ian Rogers # Conflicts: # tools/perf/util/maps.c --- tools/perf/util/maps.c | 117 ++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 41 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index 2279d8e3edc0..672c0ace5a00 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -31,12 +31,12 @@ DECLARE_RC_STRUCT(maps) { * @maps_by_address: array of maps sorted by their starting address if * maps_by_address_sorted is true. */ - struct map **maps_by_address; + struct map **maps_by_address __guarded_by(&lock); /** * @maps_by_name: optional array of maps sorted by their dso name if * maps_by_name_sorted is true. */ - struct map **maps_by_name; + struct map **maps_by_name __guarded_by(&lock); struct machine *machine; #ifdef HAVE_LIBUNWIND_SUPPORT void *addr_space; @@ -51,26 +51,33 @@ DECLARE_RC_STRUCT(maps) { * @nr_maps: number of maps_by_address, and possibly maps_by_name, * entries that contain maps. */ - unsigned int nr_maps; + unsigned int nr_maps __guarded_by(&lock); /** * @nr_maps_allocated: number of entries in maps_by_address and possibly * maps_by_name. */ - unsigned int nr_maps_allocated; + unsigned int nr_maps_allocated __guarded_by(&lock); /** * @last_search_by_name_idx: cache of last found by name entry's index * as frequent searches for the same dso name are common. */ unsigned int last_search_by_name_idx; /** @maps_by_address_sorted: is maps_by_address sorted. */ - bool maps_by_address_sorted; + bool maps_by_address_sorted __guarded_by(&lock); /** @maps_by_name_sorted: is maps_by_name sorted. */ - bool maps_by_name_sorted; + bool maps_by_name_sorted __guarded_by(&lock); /** @ends_broken: does the map contain a map where end values are unset/u= nsorted? */ - bool ends_broken; + bool ends_broken __guarded_by(&lock); }; =20 +static struct rw_semaphore *maps__lock(const struct maps *maps) + __returns_ctx_lock(&RC_CHK_ACCESS(maps)->lock) +{ + return (struct rw_semaphore *)&RC_CHK_ACCESS(maps)->lock; +} + static void check_invariants(const struct maps *maps __maybe_unused) + __must_hold_shared(maps__lock(maps)) { #ifndef NDEBUG assert(RC_CHK_ACCESS(maps)->nr_maps <=3D RC_CHK_ACCESS(maps)->nr_maps_all= ocated); @@ -120,55 +127,65 @@ static void check_invariants(const struct maps *maps = __maybe_unused) } =20 static struct map **maps__maps_by_address(const struct maps *maps) + __must_hold_shared(maps__lock(maps)) { return RC_CHK_ACCESS(maps)->maps_by_address; } =20 static void maps__set_maps_by_address(struct maps *maps, struct map **new) + __must_hold(maps__lock(maps)) { RC_CHK_ACCESS(maps)->maps_by_address =3D new; =20 } =20 static void maps__set_nr_maps_allocated(struct maps *maps, unsigned int nr= _maps_allocated) + __must_hold(maps__lock(maps)) { RC_CHK_ACCESS(maps)->nr_maps_allocated =3D nr_maps_allocated; } =20 static void maps__set_nr_maps(struct maps *maps, unsigned int nr_maps) + __must_hold(maps__lock(maps)) { RC_CHK_ACCESS(maps)->nr_maps =3D nr_maps; } =20 /* Not in the header, to aid reference counting. */ static struct map **maps__maps_by_name(const struct maps *maps) + __must_hold_shared(maps__lock(maps)) { return RC_CHK_ACCESS(maps)->maps_by_name; =20 } =20 static void maps__set_maps_by_name(struct maps *maps, struct map **new) + __must_hold(maps__lock(maps)) { RC_CHK_ACCESS(maps)->maps_by_name =3D new; =20 } =20 static bool maps__maps_by_address_sorted(const struct maps *maps) + __must_hold_shared(maps__lock(maps)) { return RC_CHK_ACCESS(maps)->maps_by_address_sorted; } =20 static void maps__set_maps_by_address_sorted(struct maps *maps, bool value) + __must_hold(maps__lock(maps)) { RC_CHK_ACCESS(maps)->maps_by_address_sorted =3D value; } =20 static bool maps__maps_by_name_sorted(const struct maps *maps) + __must_hold_shared(maps__lock(maps)) { return RC_CHK_ACCESS(maps)->maps_by_name_sorted; } =20 static void maps__set_maps_by_name_sorted(struct maps *maps, bool value) + __must_hold(maps__lock(maps)) { RC_CHK_ACCESS(maps)->maps_by_name_sorted =3D value; } @@ -178,11 +195,22 @@ struct machine *maps__machine(const struct maps *maps) return RC_CHK_ACCESS(maps)->machine; } =20 -unsigned int maps__nr_maps(const struct maps *maps) +static unsigned int __maps__nr_maps(const struct maps *maps) + __must_hold_shared(maps__lock(maps)) { return RC_CHK_ACCESS(maps)->nr_maps; } =20 +unsigned int maps__nr_maps(const struct maps *maps) +{ + unsigned int nr; + + down_read(maps__lock(maps)); + nr =3D __maps__nr_maps(maps); + up_read(maps__lock(maps)); + return nr; +} + refcount_t *maps__refcnt(struct maps *maps) { return &RC_CHK_ACCESS(maps)->refcnt; @@ -221,12 +249,8 @@ void maps__set_libdw_addr_space_dwfl(struct maps *maps= , void *dwfl) } #endif =20 -static struct rw_semaphore *maps__lock(struct maps *maps) -{ - return &RC_CHK_ACCESS(maps)->lock; -} - static void maps__init(struct maps *maps, struct machine *machine) + __context_unsafe(/* initialization */) { init_rwsem(maps__lock(maps)); RC_CHK_ACCESS(maps)->maps_by_address =3D NULL; @@ -248,11 +272,12 @@ static void maps__init(struct maps *maps, struct mach= ine *machine) } =20 static void maps__exit(struct maps *maps) + __context_unsafe(/* teardown */) { struct map **maps_by_address =3D maps__maps_by_address(maps); struct map **maps_by_name =3D maps__maps_by_name(maps); =20 - for (unsigned int i =3D 0; i < maps__nr_maps(maps); i++) { + for (unsigned int i =3D 0; i < __maps__nr_maps(maps); i++) { map__zput(maps_by_address[i]); if (maps_by_name) map__zput(maps_by_name[i]); @@ -301,6 +326,7 @@ void maps__put(struct maps *maps) } =20 static void __maps__free_maps_by_name(struct maps *maps) + __must_hold(maps__lock(maps)) { if (!maps__maps_by_name(maps)) return; @@ -308,7 +334,7 @@ static void __maps__free_maps_by_name(struct maps *maps) /* * Free everything to try to do it from the rbtree in the next search */ - for (unsigned int i =3D 0; i < maps__nr_maps(maps); i++) + for (unsigned int i =3D 0; i < __maps__nr_maps(maps); i++) map__put(maps__maps_by_name(maps)[i]); =20 zfree(&RC_CHK_ACCESS(maps)->maps_by_name); @@ -341,12 +367,13 @@ static int map__start_cmp(const void *a, const void *= b) } =20 static void __maps__sort_by_address(struct maps *maps) + __must_hold(maps__lock(maps)) { if (maps__maps_by_address_sorted(maps)) return; =20 qsort(maps__maps_by_address(maps), - maps__nr_maps(maps), + __maps__nr_maps(maps), sizeof(struct map *), map__start_cmp); maps__set_maps_by_address_sorted(maps, true); @@ -389,7 +416,7 @@ static int maps__sort_by_name(struct maps *maps) err =3D -ENOMEM; else { struct map **maps_by_address =3D maps__maps_by_address(maps); - unsigned int n =3D maps__nr_maps(maps); + unsigned int n =3D __maps__nr_maps(maps); =20 maps__set_maps_by_name(maps, maps_by_name); for (unsigned int i =3D 0; i < n; i++) @@ -398,7 +425,7 @@ static int maps__sort_by_name(struct maps *maps) } if (!err) { qsort(maps_by_name, - maps__nr_maps(maps), + __maps__nr_maps(maps), sizeof(struct map *), map__strcmp); maps__set_maps_by_name_sorted(maps, true); @@ -410,9 +437,10 @@ static int maps__sort_by_name(struct maps *maps) } =20 static unsigned int maps__by_address_index(const struct maps *maps, const = struct map *map) + __must_hold_shared(maps__lock(maps)) { struct map **maps_by_address =3D maps__maps_by_address(maps); - unsigned int nr_maps =3D maps__nr_maps(maps); + unsigned int nr_maps =3D __maps__nr_maps(maps); =20 if (nr_maps && maps__maps_by_address_sorted(maps)) { struct map **mapp =3D @@ -431,9 +459,10 @@ static unsigned int maps__by_address_index(const struc= t maps *maps, const struct } =20 static unsigned int maps__by_name_index(const struct maps *maps, const str= uct map *map) + __must_hold_shared(maps__lock(maps)) { struct map **maps_by_name =3D maps__maps_by_name(maps); - unsigned int nr_maps =3D maps__nr_maps(maps); + unsigned int nr_maps =3D __maps__nr_maps(maps); =20 if (!maps_by_name) return -1; @@ -474,10 +503,11 @@ static void map__set_kmap_maps(struct map *map, struc= t maps *maps) } =20 static int __maps__insert(struct maps *maps, struct map *new) + __must_hold(maps__lock(maps)) { struct map **maps_by_address =3D maps__maps_by_address(maps); struct map **maps_by_name =3D maps__maps_by_name(maps); - unsigned int nr_maps =3D maps__nr_maps(maps); + unsigned int nr_maps =3D __maps__nr_maps(maps); unsigned int nr_allocate =3D RC_CHK_ACCESS(maps)->nr_maps_allocated; =20 if (nr_maps + 1 > nr_allocate) { @@ -545,10 +575,11 @@ int maps__insert(struct maps *maps, struct map *map) } =20 static void __maps__remove_at(struct maps *maps, unsigned int address_idx) + __must_hold(maps__lock(maps)) { struct map **maps_by_address =3D maps__maps_by_address(maps); struct map **maps_by_name =3D maps__maps_by_name(maps); - unsigned int nr_maps =3D maps__nr_maps(maps); + unsigned int nr_maps =3D __maps__nr_maps(maps); struct map *map; =20 if (address_idx >=3D nr_maps) @@ -578,6 +609,7 @@ static void __maps__remove_at(struct maps *maps, unsign= ed int address_idx) } =20 static void __maps__remove(struct maps *maps, struct map *map) + __must_hold(maps__lock(maps)) { __maps__remove_at(maps, maps__by_address_index(maps, map)); } @@ -640,7 +672,7 @@ bool maps__empty(struct maps *maps) bool res; =20 down_read(maps__lock(maps)); - res =3D maps__nr_maps(maps) =3D=3D 0; + res =3D __maps__nr_maps(maps) =3D=3D 0; up_read(maps__lock(maps)); =20 return res; @@ -668,7 +700,7 @@ int maps__for_each_map(struct maps *maps, int (*cb)(str= uct map *map, void *data) * the array - this may cause maps to be skipped or * repeated. */ - for (unsigned int i =3D 0; i < maps__nr_maps(maps); i++) { + for (unsigned int i =3D 0; i < __maps__nr_maps(maps); i++) { struct map **maps_by_address =3D maps__maps_by_address(maps); struct map *map =3D maps_by_address[i]; =20 @@ -695,7 +727,7 @@ int maps__load_maps(struct maps *maps) return 0; =20 down_read(maps__lock(maps)); - nr_maps =3D maps__nr_maps(maps); + nr_maps =3D __maps__nr_maps(maps); if (nr_maps =3D=3D 0) { up_read(maps__lock(maps)); return 0; @@ -728,7 +760,7 @@ void maps__remove_maps(struct maps *maps, bool (*cb)(st= ruct map *map, void *data down_write(maps__lock(maps)); =20 maps_by_address =3D maps__maps_by_address(maps); - for (unsigned int i =3D 0; i < maps__nr_maps(maps);) { + for (unsigned int i =3D 0; i < __maps__nr_maps(maps);) { if (cb(maps_by_address[i], data)) { __maps__remove_at(maps, i); removed =3D true; @@ -782,7 +814,7 @@ struct symbol *maps__find_symbol_by_name(struct maps *m= aps, const char *name, st * This prevents deadlocking if iteration triggers further map insertions. */ down_read(maps__lock(maps)); - nr_maps =3D maps__nr_maps(maps); + nr_maps =3D __maps__nr_maps(maps); maps_copy =3D calloc(nr_maps, sizeof(*maps_copy)); if (maps_copy) { for (unsigned int i =3D 0; i < nr_maps; i++) { @@ -867,9 +899,10 @@ size_t maps__fprintf(struct maps *maps, FILE *fp) * Same as find_vma() in kernel. */ static unsigned int first_ending_after(struct maps *maps, const struct map= *map) + __must_hold_shared(maps__lock(maps)) { struct map **maps_by_address =3D maps__maps_by_address(maps); - int low =3D 0, high =3D (int)maps__nr_maps(maps) - 1, first =3D high + 1; + int low =3D 0, high =3D (int)__maps__nr_maps(maps) - 1, first =3D high + = 1; =20 assert(maps__maps_by_address_sorted(maps)); if (low <=3D high && map__end(maps_by_address[0]) > map__start(map)) @@ -894,10 +927,11 @@ static unsigned int first_ending_after(struct maps *m= aps, const struct map *map) =20 static int __maps__insert_sorted(struct maps *maps, unsigned int first_aft= er_index, struct map *new1, struct map *new2) + __must_hold(maps__lock(maps)) { struct map **maps_by_address =3D maps__maps_by_address(maps); struct map **maps_by_name =3D maps__maps_by_name(maps); - unsigned int nr_maps =3D maps__nr_maps(maps); + unsigned int nr_maps =3D __maps__nr_maps(maps); unsigned int nr_allocate =3D RC_CHK_ACCESS(maps)->nr_maps_allocated; unsigned int to_add =3D new2 ? 2 : 1; =20 @@ -954,6 +988,7 @@ static int __maps__insert_sorted(struct maps *maps, uns= igned int first_after_ind * adjusted or removed so that new fits without overlapping any entries. */ static int __maps__fixup_overlap_and_insert(struct maps *maps, struct map = *new) + __must_hold(maps__lock(maps)) { int err =3D 0; unsigned int i, ni =3D INT_MAX; // Some gcc complain, but depends on maps= _by_name... @@ -965,7 +1000,7 @@ static int __maps__fixup_overlap_and_insert(struct map= s *maps, struct map *new) * Iterate through entries where the end of the existing entry is * greater-than the new map's start. */ - for (i =3D first_ending_after(maps, new); i < maps__nr_maps(maps); ) { + for (i =3D first_ending_after(maps, new); i < __maps__nr_maps(maps); ) { struct map **maps_by_address =3D maps__maps_by_address(maps); struct map **maps_by_name =3D maps__maps_by_name(maps); struct map *pos =3D maps_by_address[i]; @@ -1076,7 +1111,7 @@ static int __maps__fixup_overlap_and_insert(struct ma= ps *maps, struct map *new) return err; } else { struct map *next =3D NULL; - unsigned int nr_maps =3D maps__nr_maps(maps); + unsigned int nr_maps =3D __maps__nr_maps(maps); =20 if (i + 1 < nr_maps) next =3D maps_by_address[i + 1]; @@ -1160,8 +1195,8 @@ int maps__copy_from(struct maps *dest, struct maps *p= arent) } #endif parent_maps_by_address =3D maps__maps_by_address(parent); - n =3D maps__nr_maps(parent); - if (maps__nr_maps(dest) =3D=3D 0) { + n =3D __maps__nr_maps(parent); + 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 @@ -1245,7 +1280,7 @@ struct map *maps__find(struct maps *maps, u64 ip) if (maps__maps_by_address_sorted(maps)) { struct map **mapp =3D NULL; struct map **maps_by_address =3D maps__maps_by_address(maps); - unsigned int nr_maps =3D maps__nr_maps(maps); + unsigned int nr_maps =3D __maps__nr_maps(maps); =20 if (maps_by_address && nr_maps) mapp =3D bsearch(&ip, maps_by_address, nr_maps, sizeof(*mapp), @@ -1281,7 +1316,7 @@ struct map *maps__find_by_name(struct maps *maps, con= st char *name) =20 /* First check last found entry. */ i =3D RC_CHK_ACCESS(maps)->last_search_by_name_idx; - if (i < maps__nr_maps(maps) && maps__maps_by_name(maps)) { + if (i < __maps__nr_maps(maps) && maps__maps_by_name(maps)) { struct dso *dso =3D map__dso(maps__maps_by_name(maps)[i]); =20 if (dso && strcmp(dso__short_name(dso), name) =3D=3D 0) { @@ -1293,7 +1328,7 @@ struct map *maps__find_by_name(struct maps *maps, con= st char *name) /* Second search sorted array. */ if (!done && maps__maps_by_name_sorted(maps)) { struct map **mapp =3D - bsearch(name, maps__maps_by_name(maps), maps__nr_maps(maps), + bsearch(name, maps__maps_by_name(maps), __maps__nr_maps(maps), sizeof(*mapp), map__strcmp_name); =20 if (mapp) { @@ -1316,7 +1351,7 @@ struct map *maps__find_by_name(struct maps *maps, con= st char *name) =20 down_read(maps__lock(maps)); maps_by_address =3D maps__maps_by_address(maps); - n =3D maps__nr_maps(maps); + n =3D __maps__nr_maps(maps); for (i =3D 0; i < n; i++) { struct map *pos =3D maps_by_address[i]; struct dso *dso =3D map__dso(pos); @@ -1346,7 +1381,7 @@ struct map *maps__find_next_entry(struct maps *maps, = struct map *map) down_read(maps__lock(maps)); } i =3D maps__by_address_index(maps, map); - nr_maps =3D maps__nr_maps(maps); + nr_maps =3D __maps__nr_maps(maps); if (i < nr_maps && ++i < nr_maps) result =3D map__get(maps__maps_by_address(maps)[i]); =20 @@ -1365,7 +1400,7 @@ void maps__fixup_end(struct maps *maps) __maps__sort_by_address(maps); =20 maps_by_address =3D maps__maps_by_address(maps); - n =3D maps__nr_maps(maps); + n =3D __maps__nr_maps(maps); for (unsigned int i =3D 1; i < n; i++) { struct map *prev =3D maps_by_address[i - 1]; struct map *curr =3D maps_by_address[i]; @@ -1423,7 +1458,7 @@ int maps__merge_in(struct maps *kmaps, struct map *ne= w_map) first_after_ =3D first_ending_after(kmaps, new_map); kmaps_maps_by_address =3D maps__maps_by_address(kmaps); =20 - if (first_after_ >=3D maps__nr_maps(kmaps) || + if (first_after_ >=3D __maps__nr_maps(kmaps) || map__start(kmaps_maps_by_address[first_after_]) >=3D map__end(new_map= )) { /* No overlap so regular insert suffices. */ up_read(maps__lock(kmaps)); @@ -1438,7 +1473,7 @@ int maps__merge_in(struct maps *kmaps, struct map *ne= w_map) =20 first_after_ =3D first_ending_after(kmaps, new_map); kmaps_maps_by_address =3D maps__maps_by_address(kmaps); - kmaps__nr_maps =3D maps__nr_maps(kmaps); + kmaps__nr_maps =3D __maps__nr_maps(kmaps); =20 if (first_after_ >=3D kmaps__nr_maps || map__start(kmaps_maps_by_address[first_after_]) >=3D map__end(new_map= )) { --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.72 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244388; b=tehuxS8Lad4BtBl8RbSPtMki92AgLwOpil72y28yXoosHNIj3l0I9vEilM74f+BBUpbS re8DCFMXoGZL2MtrAVRC9sQt/xuqPSHdMO5XQvTQvhdrn4c1fIo/6zSFQuos63RLAVQhH cUovy1BhwQ6+peZ31uquQ7GK5MGf3A8u+FO8qS6n/ayh7OXjiiY8Ya9k9UWJZ2B3DHr61 jGEhftcCCx07bhnW9qx82KO7BZOSnQFf2MVNhLvg9XHo8pZUxc6KvzuLOeAqPoenDPdfE 4UOiTtxpWLRuvp3/KKYzEz7Jv9dyAkbc8R6Vj8KRPJU5ZF5nYfAmVP7pNoSA0yJ/NkQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244388; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=cLjyqIXGGO5RGSsrQlBRevLtl/DbtsbRFRS5XPatqE8=; b=cEFH5JWhvPSxgHTeCUltfuUan1Z/94awJVY0kYXmQKCCvquQGwhF20RzFi3Xf1ugy0N1 AJFx6rqY6N/40I0ZPZK4Gk0ehSzhIX61Q9US1tdKzMYvp6sjtzO2idCMVFLS3P9UicCRZ aynNeQXHN7QZ+WtbDmkv9qCTpriXKwN8NosGYr/1knpV6tckY5cuV53geiyyh3S6yOc7V 2391KX3j2Sxdy5Axvjak4fAkaimfDl5N9PyLyDij0yzgieKjtuu9873a3a5JUJ/GtexiD u8k7PZ4sHLrN9L/BrZzOUcLT64BaszEJUN1lVr2ttGdLK51mnfT3nvp5cz9QvWZP1fQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=NGuA1IvE; arc=none smtp.remote-ip=74.125.82.72 Received: from mail-dl1-f72.google.com (mail-dl1-f72.google.com [74.125.82.72]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94554606A684 for ; Sat, 21 Nov 2026 06:59:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 94554606A684 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=NGuA1IvE Received: by mail-dl1-f72.google.com with SMTP id a92af1059eb24-1b29df78132so1732792c88.1 for ; Fri, 20 Nov 2026 22:59:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244387; x=1795849187; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cLjyqIXGGO5RGSsrQlBRevLtl/DbtsbRFRS5XPatqE8=; b=NGuA1IvEwknYoijiC+Q7Nzn9823fXCjMrT6y4QjXyuej0eUKkB1VXtTIiDXzMuXA9Z XaRQsz1ba8sKXjY1J4Uev+dp+aZry2gBJqsSJXsBEE0ezyjcOgVy4claNKSDLULImoSI D9P2y+b8rjW0O3+6oqn7NbksqRTQNhb4pRDGGxVZNKONS5+SbZ0SKPg45FVccIsNtY/8 udbMk2+myuZ0hhBD1iM9FdRDdxj6TLagmUJXhc4T/NxiBi+9XXR2N0TrrIl2eSLeYBEi lS8wmRpPTM/x3i0+9YpvlPWyqLsw5+MuCig+9YX/KFiQQHnZPPzg7Npyv1g4jdFp8mmE SMkg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244387; x=1795849187; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cLjyqIXGGO5RGSsrQlBRevLtl/DbtsbRFRS5XPatqE8=; b=0nvzdO21I1LfJYtXC/pMFzwbv1y57Kobo+0+H4esTOh5sAkDfgfwrge0yI3eMM7NVf YX5mH7+ht8sXcVdEk3W3d6fFuRkBfH/lMQJ9253JTDnZvCQV8N3EIP0aTh/tcxKNBxbV CJlHQe/+fLQ0LC3frea6Or8VrVFvclBqlTOVS0LgoFvQhCJLhlzR2BCLeANwBlSrZA+J TfWm9RA8Q2iDaTlt1xiTPD8pdgFdu3zZwpYkiChQVX2l8q5Yd1aFv/dPQNni3uAKsJpW WUkoP+rs2LCa/gTk9SlupshGDbmCxtIzs/OfUZy2pMr9zSkrDb+chrcnKJHlmy6nhuh8 Mdrg== X-Forwarded-Encrypted: i=1; AL+hErGmv8S1xXATpMVRBqPw+95DdHa9x6yGtTV7NRePRWVx52GS439xLz7kC9Ibq/igraM4pJ+Lu2QrwEwnr40=@vger.kernel.org X-Gm-Message-State: APuU4Z8L5bPCwh3QL1YnQgerRTA7fWBSjUOtWO6Dva+Na1Vgl00Jq4OU wGK3YSwP+QYsS/SFQ0yjm/GlNWa6pEk4LXeeAQn+3GzBA6N+dUDClKALNm9mEDRvS0Snn5rVw3d 2acdjWwWBcQ== X-Received: from dlgw20-n1.prod.google.com ([2002:a05:701a:cc94:10b0:1a0:100d:fd6]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:3a83:b0:184:992:c9ec with SMTP id a92af1059eb24-1b55693aaabmr5705849c88.38.1795244386190; Fri, 20 Nov 2026 22:59:46 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:11 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-23-irogers@google.com> Subject: [PATCH v15 22/23] perf machine: Hold machines->lock for remaining guests rbtree accesses From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Commit 1324aa051bed ("perf machine: Protect machines->guests rbtree with a rw_semaphore") added machines->lock to protect machines->guests across util/machine.c, but direct traversals and teardown of machines.guests in util/aslr.c, util/build-id.c, and util/session.c were left unlocked. Acquire down_read(&machines->lock) around the guest rbtree traversals in aslr_tool__destroy_machines_priv(), perf_session__write_buildid_table(), machines__for_each_dso(), perf_session__read_build_ids(), perf_session__fprintf(), and perf_session__dsos_hit_all(), and hold down_write(&aslr->machines.lock) when erasing remaining guests in aslr_tool__delete(). Fixes: 1324aa051bed ("perf machine: Protect machines->guests rbtree with a = rw_semaphore") Signed-off-by: Ian Rogers --- tools/perf/util/aslr.c | 4 ++++ tools/perf/util/build-id.c | 8 ++++++++ tools/perf/util/session.c | 8 ++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/aslr.c b/tools/perf/util/aslr.c index e365f807b021..f9ecb6d3f5fb 100644 --- a/tools/perf/util/aslr.c +++ b/tools/perf/util/aslr.c @@ -243,11 +243,13 @@ static void aslr_tool__destroy_machines_priv(struct m= achines *machines) struct rb_node *nd; =20 aslr_tool__free_machine_priv(&machines->host); + down_read(&machines->lock); for (nd =3D rb_first_cached(&machines->guests); nd; nd =3D rb_next(nd)) { struct machine *machine =3D rb_entry(nd, struct machine, rb_node); =20 aslr_tool__free_machine_priv(machine); } + up_read(&machines->lock); } =20 static u64 aslr_tool__findnew_mapping(struct aslr_tool *aslr, @@ -1363,12 +1365,14 @@ void aslr_tool__delete(struct perf_tool *tool) aslr_tool__destroy_machines_priv(&aslr->machines); machines__destroy_kernel_maps(&aslr->machines); =20 + down_write(&aslr->machines.lock); while ((nd =3D rb_first_cached(&aslr->machines.guests)) !=3D NULL) { struct machine *machine =3D rb_entry(nd, struct machine, rb_node); =20 rb_erase_cached(nd, &aslr->machines.guests); machine__delete(machine); } + up_write(&aslr->machines.lock); =20 machines__exit(&aslr->machines); free(aslr); diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index 02233ef0eaff..c792448cc344 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -374,13 +374,16 @@ int perf_session__write_buildid_table(struct perf_ses= sion *session, if (err) return err; =20 + down_read(&session->machines.lock); for (nd =3D rb_first_cached(&session->machines.guests); nd; nd =3D rb_next(nd)) { struct machine *pos =3D rb_entry(nd, struct machine, rb_node); + err =3D machine__write_buildid_table(pos, fd); if (err) break; } + up_read(&session->machines.lock); return err; } =20 @@ -926,12 +929,14 @@ machines__for_each_dso(struct machines *machines, mac= hine__dso_t fn, void *priv) int ret =3D machine__for_each_dso(&machines->host, fn, priv); struct rb_node *nd; =20 + down_read(&machines->lock); for (nd =3D rb_first_cached(&machines->guests); nd; nd =3D rb_next(nd)) { struct machine *pos =3D rb_entry(nd, struct machine, rb_node); =20 ret |=3D machine__for_each_dso(pos, fn, priv); } + up_read(&machines->lock); return ret ? -1 : 0; } =20 @@ -962,11 +967,14 @@ bool perf_session__read_build_ids(struct perf_session= *session, bool with_hits) struct rb_node *nd; bool ret =3D machine__read_build_ids(&session->machines.host, with_hits); =20 + down_read(&session->machines.lock); for (nd =3D rb_first_cached(&session->machines.guests); nd; nd =3D rb_next(nd)) { struct machine *pos =3D rb_entry(nd, struct machine, rb_node); + ret |=3D machine__read_build_ids(pos, with_hits); } + up_read(&session->machines.lock); =20 return ret; } diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index a58b4fe44b62..4e11eabbbe6f 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -3965,11 +3965,13 @@ size_t perf_session__fprintf(struct perf_session *s= ession, FILE *fp) { size_t ret =3D machine__fprintf(&session->machines.host, fp); =20 + down_read(&session->machines.lock); for (struct rb_node *nd =3D rb_first_cached(&session->machines.guests); n= d; nd =3D rb_next(nd)) { struct machine *pos =3D rb_entry(nd, struct machine, rb_node); =20 ret +=3D machine__fprintf(pos, fp); } + up_read(&session->machines.lock); return ret; } =20 @@ -4181,16 +4183,18 @@ int perf_session__dsos_hit_all(struct perf_session = *session) if (err) return err; =20 + down_read(&session->machines.lock); for (nd =3D rb_first_cached(&session->machines.guests); nd; nd =3D rb_next(nd)) { struct machine *pos =3D rb_entry(nd, struct machine, rb_node); =20 err =3D machine__hit_all_dsos(pos); if (err) - return err; + break; } + up_read(&session->machines.lock); =20 - return 0; + return err; } =20 struct perf_env *perf_session__env(struct perf_session *session) --=20 2.56.1.750.gf70dc682cf-goog From nobody Wed Nov 25 13:26:54 2026 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.199 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795244392; b=DQe0p6aLjjexFhukYEvBydCxI1CV1FKq2DHAb6DHfXzc00jPOQOoWXbTx5M4QnK5ec7q 3KzOkLRITEEAkIG3dgA3MReJl7iz41YAk9NDY+0jlxO0tRc0CaayqEgctJ9DfC1jH7EW0 hXT9pdJVwRY7oPp1SfENeoiyhLfFE2dyhHullIhRR+dg9uxMMDsxZTnO1XFwV73ql4pH7 1d1YqiY4IdfT6JDF+Vw3BV0a2fSR6nR9zVHTNH1AvDcUIbH1xVXYovEneSplkbe5qwA48 Y6bnOc7G+/dlfRz8xNgOj+r1vd9SYV5xTIWaMKbKa5FHKQyltpQ8FCFc3UricJuO0Qw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795244392; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=fGAdvKbnnDNvC7VTESnuJjeTLcD1XbFE2xtDIUZCto4=; b=5eHA/q7eu9c2BMYoBpRcNSmSQbZeKOnNyB2fx5pMDAdr+CPCYwW9R7YXc9kewScrjAle 2TVhqJiqJiPRVbDa6O8Xva27qGAJf3cZ8E1Y51pNGCVQfuzuCRhBTLFaKCO1LGwAcpOQ8 rsISptRQAQNh+Fcpsiw0oj3S+gw5if8fDt6VZGPCnEbK1sSuNvV2Hc0aipKJhIDh/w64X PtyBz7CwmCBi40Fqv45wUg3GFnT3cLskfckzSg04UryP437LiR6/wOnlrBvMuSS13AuNg GFSQp2V9MRZxB+uJlTjWSvHxnzY8mvU/3ZaxrEfmu+EEC3xCbYHaGO1ErPYZPeBnj4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=INpYishy; arc=none smtp.remote-ip=74.125.82.199 Received: from mail-dy1-f199.google.com (mail-dy1-f199.google.com [74.125.82.199]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19B78606A330 for ; Sat, 21 Nov 2026 06:59:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 19B78606A330 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=INpYishy Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-386177b0516so4032138eec.1 for ; Fri, 20 Nov 2026 22:59:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1795244391; x=1795849191; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fGAdvKbnnDNvC7VTESnuJjeTLcD1XbFE2xtDIUZCto4=; b=INpYishyBjqzCHtPkLUzALLHXaqZglbxaIt1wiWcwva416LFtcMHBPD2+cGMjFnwo5 tlhv7jPuRv22yW66JdlmNOITf5OD2JCpqy1Yfz+dOkJSUBiEVaknErML0ozRZnojN9BD YAbCoPAOCh4MQ47VZKnT+f6uJxo7wgXDfoceg11V3QX52PYw1uNuyvbcfRYgUjL4a1n6 AxmjgAeAZr6J4/gg3wc8pbud+J1ULoTNT+Cdo69+3xpe1nlsmJrWxZ6UAzZMPcDRFMqV Yiq35+1wY9gL7VKZnda839ti7EbYrTd6CZQP0rBH5NQH3DHUAohPdTSHd15uoQX7vEjG PoXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795244391; x=1795849191; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fGAdvKbnnDNvC7VTESnuJjeTLcD1XbFE2xtDIUZCto4=; b=B+5LT61+vwlC6K6X1CHi/p2YJw6OWdDkraugIsAvRi/eyCSGDNEiFe0gXnlGSOdPX9 U1Q5HxLATrJch2PvGPIgeAHBwoOzel252F5yVtJRdmyxqhH9k4nPIGNzAWzvhIC2nqh2 XfKtzf8DZz20qMsOYCdtEgD3sDbYMz9cLuEOlWrJohPO6EJyxaLVEDdopWVe9HdvDopN yZD44QeQg2o/NEvgeLS02HJ/CMfQlCDZuc3p9AO33QnCmGtkWduZOYzBZ+EMnOfWCWO9 Ae2ZwATl+PNR3geOQAy/ERXhYYr+XjJktG3VUeEtGYHE8xIcxHKQ7OOBo5nxg+r8dH1+ eN+A== X-Forwarded-Encrypted: i=1; AL+hErGdVuZ7cruekFWuETAQ5QA1VEz2xbOgC4UwLnlEeENesB7JoSU8Bd9FRL+Lft3yP+gDjGjGLVs69KnALQM=@vger.kernel.org X-Gm-Message-State: APuU4Z8+/5Z5Ckf2vFxB7ZyZdwX4ytlew/BdyVELxbW3I9WeR4rzjLAy aDokHBZRZLfmH5xwq3CA/mEfgQhxxcUtkeYe9aHxGScqSBEE7spxPwlMWIdsgNnDcLD2JyktLAu lQpfqzW140g== X-Received: from dloo27-n1.prod.google.com ([2002:a05:7023:a5b:10b0:1b2:48fb:f4d0]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:2608:b0:1b5:342a:e4f0 with SMTP id a92af1059eb24-1b5568b678fmr7282977c88.6.1795244390557; Fri, 20 Nov 2026 22:59:50 -0800 (PST) Date: Fri, 20 Nov 2026 22:58:12 -0800 In-Reply-To: <20261121065813.4143649-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261121065813.4143649-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.750.gf70dc682cf-goog Message-ID: <20261121065813.4143649-24-irogers@google.com> Subject: [PATCH v15 23/23] perf machine: Add __guarded_by annotation to machines->guests From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Marco Elver Cc: Bart Van Assche , Jiri Olsa , Adrian Hunter , James Clark , Weilin Wang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Annotate guests in struct machines with __guarded_by(&lock) and mark machines__init() with __context_unsafe(). Signed-off-by: Ian Rogers --- tools/perf/util/machine.c | 1 + tools/perf/util/machine.h | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index 0cc593d5372a..70870baa157e 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -234,6 +234,7 @@ void machine__delete(struct machine *machine) } =20 int machines__init(struct machines *machines, struct perf_session *session) + __context_unsafe(/* initialization */) { int err =3D machine__init(&machines->host, "", HOST_KERNEL_ID); =20 diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 80191545ee8a..67d517742fb6 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -3,6 +3,7 @@ #define __PERF_MACHINE_H =20 #include +#include #include #include "maps.h" #include "dsos.h" @@ -153,7 +154,7 @@ struct machines { struct perf_session *session; struct rw_semaphore lock; struct machine host; - struct rb_root_cached guests; + struct rb_root_cached guests __guarded_by(&lock); }; =20 int machines__init(struct machines *machines, struct perf_session *session= ); --=20 2.56.1.750.gf70dc682cf-goog