From nobody Sat Oct 24 14:02:53 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=1792700112; b=GrVAZP59zAnc485CVKegjRnsJgOnZX28iaJpRFrzb02HUwvb1YkVBw6+ECncvmSTuDX6 ONMKPNlXTiSyywtetTftQfFQTQCV+dNNRWVA++QlMf8X7yOTQvUHGa4c+TNMv5llNvZB4 KEbM8asfCroS02VQvDEZm/9xV8Tmsvc8vGB8/Hhy2mo30FBqFmOhRi/JSmfGrzD+jc0Vl IZD1mgHl0EsIPN2pqNCqFzyNYHA0DgBef7QFNvX+pTEzQJrPmKTe/F0EatRzjXLVcWdR/ 5yPyB/Rzc4rB2wyljXba80+oOuV0QnSx9hBhGKryKiz/5CNeFlZT2WuVDpJbzXKmYiQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700112; 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=5OI66hZlzn/cFny6HHofnoLn7Sz3L3IImkQeNPuJ/dA=; b=MTanhGAh6FKUX5ACeq6SEKNfWwn64oGh6FNtDJwDARsdpksrGHigOCJuVH0c0CSetadh diaFppehChV3HUQN7+ZuuOCHTuRU0FU2UPOJVyJnvx07xu+v/9UTXvUXfERnjhNpxmCfs 9hlOD8tW6iCRAqH3ZBGE49XQwoonZ355KEzrxHyph37CqvB/LMzqgcEqGLtF+FjzBFDzG QxLUDZ19+lI4CiuZBb2XftlCMAgNfC8iI2ElvLlyM/CEwVRpn0yxU96yRx5HAxRTY2Vd9 4l0ROShgOtwU2u9xa1mY+7/p4KlZIaz4k2pMD9I3QfzgLph33iNLtboeFc/XTGGPUQg== 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=20251104 header.b=YBWievkW; 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 4B9BF606A32E for ; Thu, 22 Oct 2026 20:15:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4B9BF606A32E 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=YBWievkW Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-1383723dfddso245171c88.0 for ; Thu, 22 Oct 2026 13:15:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700110; x=1793304910; 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=5OI66hZlzn/cFny6HHofnoLn7Sz3L3IImkQeNPuJ/dA=; b=YBWievkWXjuDMsqw5zROegZhlbbSSmHp0Df05KN8Okcz6JGGuyytihMU5g1f2QFjWX 7MBSvMO6bkf57fXn/45IJfRGPh9MxK6ryF1N49nbs56LojzY6dU9tuB26RQJVk1NiyFq qcaeMGifKQXPaLKr4fdgish6w0TVNnmWezRmh9uyKmKFJa6y/PsSl19MWrW1kTt6Rq1T B+te/UArUNIz4DBfCe9EhaFVBDxyAcnzuFfhYpZotTyXK3nUX5s6KNqW5zoZCOiobjio GG8TexqZIQQ+LipNEW+INc6EiNW73K0rGYa10ALttinmI2+b8LfPGIPM6f8t5AA46bSy 8I3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700110; x=1793304910; 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=5OI66hZlzn/cFny6HHofnoLn7Sz3L3IImkQeNPuJ/dA=; b=J7dz3OgkNYS1ETVQybCtCAYmgqhOA2NxA1HOUPQysInzybMJudVpG82pY+IsdDtIeh /COH4hxtG3y6taWfvSmCj0xmYXdDDShHZiBlP3KFwmvNMZhXzzFunc+QFrul+XZE/cAb ijIULnm08xnoZNeHQbzGmXH6ovoRK7Frt/NU6KLpsdFLlv1H25S2nXEMU4soSRezmz7r tF0Ssxom/7Au97JDV83ZmGJKvYhSr6lKx81fdgJknRYwM/KHugi2MabA4vcW2q3Cy3B3 e9lyz2MARHTlXzwbGKTogFaxw1DzJbpSKgub/TohbUz+BwtJloW5x4U1S4lCug3WX4/I VOUA== X-Forwarded-Encrypted: i=1; AKwUvBzGITAZ+OSRipBOQ/UT+ey8ftyBE4VU09YHK2+r9vOO+tJlN+1YhCLEADCLvM1v8l+Zx4MXl2b0yr5AhNY=@vger.kernel.org X-Gm-Message-State: AFq9FYI1lDh/1ElzFHQbcRVo4O3/uXKtLjebuz5qsEaL0ebBgvVCL8kn s8R4Dpa2Rh0C6pvFGX7VTfRGqQINCnGP50Syvn8HbLNaBLApe30CWBKPFPOoOpL7+nUBDsphqUl J40hdxgx60g== X-Received: from dly18-n1.prod.google.com ([2002:a05:701b:2052:10b0:195:13ae:9fe4]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:404:b0:147:8a40:cdf3 with SMTP id a92af1059eb24-195c5ad11d9mr576470c88.24.1792700109901; Thu, 22 Oct 2026 13:15:09 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:34 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-2-irogers@google.com> Subject: [PATCH v8 01/19] compiler-context-analysis: Avoid unused parameter warning in _context_unsafe_alias() From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700114; b=G23lcTFb/scQniegyJi9sRG/zQmeJJxNKZ9GlYz/JD55MrMZvtAAeeK3rc4s4wIDlK+F BkKIJSXKcAmdgmHd6jT5vbFfd7rgl83hodyOuDQ5k8e03sy74+vAHQgMAJU9cXgFyVxib 0xu7nhkos9cnjdAQz9fMnu4GfhJi8q82PCBD1IAuW1zgOLR9y0DMBoVwWPA+HPfNHP/Z8 cH8oTdb707yBc5ioPEZl2gHI5V2zd5ykoQAsNOhH7VnWPCPmH3Zquysnb3WYqVs8ep8Mu GFqtMcSDudl8iyqXRIAqZp0OKMSbkBH2GnXQTVCPC8jMEyaA6Y/fuy70qY5ckicvIUg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700114; 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=xorsMXZVDOpGMdMlqGP06HTMiN994coxJ39fnDckWKA=; b=f08958NrMieWFITGxMacU5xAXpZ3yRK33h8LsFSEi7OmORS3Mo+otS7EA7D78oirWroi Zf6iFpdGRe+d+IpE4jlhKc1xA8ANh4gHbRXpsjn+yqi1ggW9hlqWwvOFwc3GpGLp96MMl Ip92NGmjs0cCLdHD9okeHk0LgSH0+mNiJ2/aA3R25V7o05e6pfa9YdskadLkBcNx/wL5w NdKvWWNLDc20dMrOr1decNf8OhpTwnPpiW1cUVmVykM+j1OEYRyjDCC+ACCEAPTslmcaN sB40mfD8AMlKo4Slmy0n4Humjwj4Pr2PuU4Gj0jDtS3qcw7aszn/+Nvxe5GO+GAvNgA== 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=20251104 header.b=apx5e5bm; 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 BCD0A606A32F for ; Thu, 22 Oct 2026 20:15:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org BCD0A606A32F 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=apx5e5bm Received: by mail-dl1-f70.google.com with SMTP id a92af1059eb24-14373747468so133610c88.1 for ; Thu, 22 Oct 2026 13:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700113; x=1793304913; 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=xorsMXZVDOpGMdMlqGP06HTMiN994coxJ39fnDckWKA=; b=apx5e5bmG2pBa2CUg3mfwlPQSIg5prU0C5NZMUlWEyUBc61l5gAnNXcsA8Nb9mBDId vgfYDG5r8vkVXi/+Ya0SC+STvrMJ5RPs1LX3ro8d2/xptvD4HpTYB2is7eQHMfONR3pP GeSS1nuqlAZcYa0DudzDPpW52GrJISWMLDlKDbqcCMOi1kz2ziHTBwdH2NL5nq01EU4O qqWrFEsbVmiDigno314gQmEu0xwXmKSu3hxYUn3RpiEpF7JoKfo6ofGfUR0h0wk6K1yD SDfgeN7exLgJJMSF/TT5LBHe0lHwkpyLGhI+Ccw9UCYxyiqlIwLmFZLqGT/0zjvEYaTO EA4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700113; x=1793304913; 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=xorsMXZVDOpGMdMlqGP06HTMiN994coxJ39fnDckWKA=; b=qZuCDVkrLMzIl72LfhVcrnCY9W/rG36HOdVTImWFJ5G6hrPahvzLhj2KkH39ZXkO1v EMtMFu58htD5vtBSYDl0ZL8rojEOi5ExCwKzJl/IfChgwMZN+yNdvxnsykCU84L2MvEz oy6lonNFYNoERF5meRNFUhRBBhahAiDzQQ17XwH/Q3LT28hvPp9VW8rcYGaNbPxTumHH DOlVqOdWNF3XihxXgp1himBlzh6MXOHdFvaLqoJ2aHQQPnbJiwKIs3L86SNLDqU7LyXI IVWEDTqr7mj6WbVDcUGiLJzuilOA1Ky3GShS8gQofkhq2LHoZ9m+semDppj0euiAc8/A iPvg== X-Forwarded-Encrypted: i=1; AKwUvBwXAJcm22/aSroTSPG9qCAVz8cX8PLTHEpQHdOoZwt3DWuCzsGUdiu9NWbEqn0zEXlyuapHsDJPLdz70Qc=@vger.kernel.org X-Gm-Message-State: AFq9FYIfOIXIP7+c7h3V3BikTT+M1e7s/mBXMAIC6m/oyNx/rzceCbI0 Pg0srPcZ4tGe2N5qWKP2ouLvsESduuAVAEp/3v9uxFufeS+V9ODfUbCRfywbeYiO8WBGFFWzzI4 +H2ibFx5hdw== X-Received: from dlai24.prod.google.com ([2002:a05:701b:2718:b0:17f:8a38:d873]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:4588:b0:192:fd91:c569 with SMTP id a92af1059eb24-195c4d6136emr464835c88.11.1792700112325; Thu, 22 Oct 2026 13:15:12 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:35 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-3-irogers@google.com> Subject: [PATCH v8 02/19] tools headers compiler: Synchronize compiler_types.h with the kernel sources From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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 f2f54b038168..86d628ba8d60 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 - /* * This returns a constant expression while determining if an argument is * a constant expression, most importantly without evaluating the argument. @@ -237,11 +198,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) \ @@ -258,4 +214,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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700116; b=xaveQQet8pQBrpTJlGRC4t0zscCEmC8Vb3lCRAislHCXWNqWt+Hnq8P9fgezFGYdTZ4U iGTzz8hTAIgcgnxr86aGhZiSd5zeKqcPQ9lVGl6vBO7DhpL3XfYvM8d4gJkoXViVHOg1O 0HVVKMQrPotutLtjojqMNwbnV9HkWrvuibu+Zp0+L/uLQk2HadZtq5bm6NvV3k7jldqvl +jR+A1Ylb75oxJNqeDtnATvlYRtpECI0Xa4aPHl7dhdrgVctcOpqWSUpo/Al4b3ukbClQ suuY+j9akKxz01iCl8xswcw9vpYyzuf1MzxrOw8r37+ca6xAfylrzKwwZC1nhISM2YQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700116; 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=uS2713jWrJCc2soKiLxgtIKJrnHMwmT/jC+m8pFdptU=; b=roWXB+8MB2FMVqiJBOoRmp1VPzPKUBTBAqhvQCsdJrC2PxD9y0zTu4GPFaXjaUuIj40o PFQGr+iK2fEtWfLquXt13jVrEfUWTEF4CNT9Fhn5h4o4w9BW/FRuIVc8MpjWfsu17WnK/ 0by9HvYgi7ZuTZpgcvqSi7S2awFNJ9qOFGfCP4L5WLZsC6xChi/q29O+5lsbNJ+1vBfC2 PaLGHJqO1I09eMnxlkzfzIcjDiEuvhAXlHo/D6MgqCFytAgU5qoAZTQf8zw6bg7W0pBI0 NkFKUfT47sjoXF6+yKH44+WLxCzthV9YjPNLaTreX/R/8+JArX1eLRJuMuWlR8vwKXA== 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=20251104 header.b=IeAAhM9g; 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 E1051606A329 for ; Thu, 22 Oct 2026 20:15:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E1051606A329 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=IeAAhM9g Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-189d17eca17so20273c88.1 for ; Thu, 22 Oct 2026 13:15:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700115; x=1793304915; 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=uS2713jWrJCc2soKiLxgtIKJrnHMwmT/jC+m8pFdptU=; b=IeAAhM9ga96rlMafibFCJ3KaZstLp0DmzVqOz3IBkOEi2AG7o6btufUWZ0hn1QXcb5 Tw69vSK8zcbLnGTcGx+2bhkyMXTxRFT3YjM2s1zGl4BzByRdRBoudu+CwWXBel5Astg9 eyQbSwnj59E1ROrRqmmBoZkjzF4hEoA4B8mjhyFxHYu21pP4eg1tSvQXeeylTeL1YzDf Eox3qfBK7K7/sVzdzjKwwP5hU2B5/OI3VJaXqDjcnhvIlaCwvAI3wf9ELWQgg9DRiD+R JjW/iT0Q9rleOsgJ00x2w97J0g1cuQN19wMCUFv68X/LM8CrxnaBbvJ95facLM6mreHr qqDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700115; x=1793304915; 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=uS2713jWrJCc2soKiLxgtIKJrnHMwmT/jC+m8pFdptU=; b=lFRuhOAhhAXn4peJONOMd8g/lUBpyPrXLTnM/meUv5uBrLQhTfvbuvBLFIRx7+64hS hwS+OqUpq21UDGAfl+DqFRadFGP+bD65j2NM015hsVaE8aF90OnuLvlRluu8pHW3GmnO Qv/I7Q84IeTd+Td72zZ2AJ+eKhZGjSw0tF3Xj+e7kmlsv3C4C9xTuGywaKEzGdHDa7f9 O29aSgWRIgw/wBg5KG7iffLV3sxOXlS7xWpWmOyDevAOYFHjpszw2IYqOlukLg733E96 JHKpUWKnVU0OEaJR9GqgLLGPPBsSj5jve3p+RpYDOQ+jpNHJRn6RNYzzvTcEziHQKxVT 8NoQ== X-Forwarded-Encrypted: i=1; AKwUvBy4PpTULhgNRgvRkv8Q4qm66LWrQGH7pm8VZ34UYCsXJu7jN/EAq2dAyjJ24spc8OCpGzJsnR0eaLccpFk=@vger.kernel.org X-Gm-Message-State: AFq9FYIY4yeQQM6J0OVNbE0zyW+hN0yOxPJUpAc3w4cHJBP8haSuQc4l e1q+l7W9CiaZYTROSmqwTbUid6aJl/OVPbwOgHnxfDt3pnHmrgTRNKVNP7cv0DXYkoTjVueXa/p gGzHaquU+wA== X-Received: from dlam9-n2.prod.google.com ([2002:a05:701b:2089:20b0:195:a8f8:f495]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:4089:b0:158:b51c:4502 with SMTP id a92af1059eb24-195c6a34f77mr296599c88.34.1792700114648; Thu, 22 Oct 2026 13:15:14 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:36 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-4-irogers@google.com> Subject: [PATCH v8 03/19] tools headers: Synchronize linux/compiler-context-analysis.h with the kernel sources From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700118; b=PxQMOjBDnufXhh0F4NijYCyJ1E7QshAhKKUgPRReJZmt1bnOFx7gBSt0AxlugAnWHVKo JMfg38GhgH84ffw10MwsKv0Bl08ohyC15MaIMmV8ih5r6wq5bgrpZsRVbCGPAh9ptwgh1 zuDFINwR11P6fuHsR5Mx6rZdClBIVk23/uQGdC1blek47p6TMiFibBuSb8TVQE+257wRL JqPvA8JR1BATWSRbdWX+zUxkfKMEYZD7XrIqUq1D1EPlxx/4zycm7uOrSrRqbxsGe74jE xIWV7MG6aRoSgyRHL2lTyis/S9pQojqNqYVGG9U3AfbiObCdFznJn7ajzj02OOOOJTQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700118; 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=GHkZFoD6IScmhQzi7ypqz5fTIhCy044bV+qrgkeyJ1U=; b=keYQGQq19wPE5sU7jtOiyFu/mYdYdmIVEzUs/4ZMMhAFjekJAOG6DD6uPjV1pQdQLNz8 Ekc9caZJzt0txrHzQh0wkzwRbM1YZ9khQa1fWyJv8mUeX/CxBJho9+rseULUWp0fzE4vF zQQ24zH2WrsqxWynutVPjtysjshk8F5dXRsl4ZbNOZL1VC6c/aOtp3uuvbnDzHNSbC9Fk qBW5CAimXVNeoEjUxDWt7FThUb2uszAZ4g3M5HNF+EzWdnxte2kbS/T4sGPy1TKrqpUZv 66Kf0209NhsNE+1vd1aaXCM3nnEBd8qXSXxiuXIvj/imwmbTQFteWVWuSW3E2y9w3Rg== 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=20251104 header.b=hYGKBKFB; 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 06A7A606A326 for ; Thu, 22 Oct 2026 20:15:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 06A7A606A326 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=hYGKBKFB Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-30c0d568830so433636eec.1 for ; Thu, 22 Oct 2026 13:15:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700117; x=1793304917; 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=GHkZFoD6IScmhQzi7ypqz5fTIhCy044bV+qrgkeyJ1U=; b=hYGKBKFBG/+PY+nNBrUdYvX1UFxT7zcrfpmOexCkxhrhhpw7prXgrI3xY11fv8K2rs SCX46ht1Bqq3+9nF0J9hIh2qLwGpc2gCOPBaeq1N8/2A6r33Sfwju3e+14GVRpX5vsJm FXj8BoonCxHtZy6RWdmGvSkzg0szuoiyWVPX96IMDrU7yrlCYRoIbRG2hizlRhwfN5Mc 0QLRZo7/1WP1q+YdM4Z9uP0tJYg2A4JNZgPzawmqCWLW5fZLxUxmAK/Z8RdP5xjmP0OH vRUbi+1D1fYNziA+xG0/VhHT3d1p3oqGjPuP1nq6dt7d5eM4icu3v03JPVIvA3dAFNYO ZDXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700117; x=1793304917; 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=GHkZFoD6IScmhQzi7ypqz5fTIhCy044bV+qrgkeyJ1U=; b=y3kL444ssPdufyNsJLCy1lscNVxuBYpTYvpE9IDfA5NvENjFviZck8EmI9pA8EAF8m ohYmiR4gpTYpOeGC9mC7dEEpl6aYHahqYreex8CeWYf2KKVgnBzolswgeD75fCNwHJJB /iFfPjXgHQZ6MtGEfnZRCQV2k1f/DDURtSWv9eKsF/j42c6DLlySgn1f0PDIZga8O95N hpbTEJCF3Xd4IuQrebUBVJnSHuLvcIhJiyVQlmknzzHG0TsC5Zj94sLFuE14dVHucpIG DpNrAESeMcZme30xUHA9t+xkOEWuVdrHnP7aFN6rFdtUKPLU4KkSzIaa4uqD1wWNgqJW LcVA== X-Forwarded-Encrypted: i=1; AKwUvBySZWF/MgibSAOockSV5M9q/n4b01DM7QDvrlmaLlNekIjNoTHtGxGD/QRxgmZgENF4F5X8as/5PtZRHQw=@vger.kernel.org X-Gm-Message-State: AFq9FYKgnv9NLMdJUSu/4L6xAltJ907oZrl0IoHhEFLzKKbNjp3YX3Md OR4lpE/9aLPH36oJr31PoLQBr2wB4qsIpygKCgOOS8a94YV1MbqVRNBgaM/+LCERRzxcaY3Mf7x ZUWEZ5Ei1HA== X-Received: from dlbut1.prod.google.com ([2002:a05:7022:7e01:b0:191:778b:369d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:205b:20b0:190:af51:d56c with SMTP id a92af1059eb24-195c693cbddmr340099c88.31.1792700116763; Thu, 22 Oct 2026 13:15:16 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:37 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-5-irogers@google.com> Subject: [PATCH v8 04/19] perf mutex: Use kernel style compiler-context-analysis.h annotations From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enable -DWARN_CONTEXT_ANALYSIS alongside -Wthread-safety for Clang builds in tools/perf/Makefile.config, 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/Makefile.config | 2 +- tools/perf/builtin-sched.c | 16 +++---- tools/perf/builtin-top.c | 4 +- tools/perf/util/annotate.c | 4 +- tools/perf/util/annotate.h | 8 ++-- tools/perf/util/comm.c | 4 +- tools/perf/util/dso.c | 30 ++++++------ tools/perf/util/dso.h | 9 ++-- 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 +- 17 files changed, 69 insertions(+), 146 deletions(-) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index b4764f69f47f..3eb8d095cda0 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -64,7 +64,7 @@ ifeq ($(CC_NO_CLANG), 0) endif =20 # Enabled Wthread-safety analysis for clang builds. - CFLAGS +=3D -Wthread-safety + CFLAGS +=3D -Wthread-safety -DWARN_CONTEXT_ANALYSIS endif =20 include $(srctree)/tools/scripts/Makefile.arch 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 a3442d2d6edf..9049db154fa4 100644 --- a/tools/perf/builtin-top.c +++ b/tools/perf/builtin-top.c @@ -201,7 +201,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; @@ -730,7 +730,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 86a4beec6528..c466757e8508 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -655,7 +655,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 @@ -664,7 +664,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 1b47c1156b9a..f5465c931721 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 ce98613868e7..24ff066b9878 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 3329f417810c..fa5669709eb3 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; } @@ -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 a4dcb406c79e..38991987f67c 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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700120; b=365Pwro/s1QKjuK5dQUNMe7kxSheQQszbVCuONXQx0kC1zHKP5cTInJtrNx0D5Mt/fzk XeIVSYoqjtsdscxTaiMHqmz5YY8o18azW47Wh+CftOLRAZQ7Gw+WmYQADE8mU5eesWOyI qvDQsQ+gE9EEH3BALM5w2Uwpgnls4zos6NvBp3keur03gOlhseXQdKxY1kEK1y//pGn2P Yb2eQhZjVfXmzdIyrH20WtV0KGbMU776zrrvDRZ1V5Y3Ys1u1xe+31e7JkbKH8BxdlpYE g91/xs2hJYg3kHoNtKTHP4kz7KjH+Fm9V+geiBdjq5EDomJZtIrXplYGfSRftJAUsYQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700120; 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=6JUxMMI/25kOrfypmhpeOOpRGxtyrkB4sVFmB/HCDt8=; b=WJHq+rysQKHeDNPz2vBLIOOLhgMGrLsr6PyFh1Risk+Nv4rQ3FNkZVujqwN/Hoh6GFV6 rh1LxGrI0LW5HK1sn9l/swS5rdl5HUMiOkhr1AcDxhGWPsgICJPnVHWo0qw1SIg56HPnG FpxfL0W/ol2CY7JBrCDh/Ph+hASqsTeTgFyJFBLaEVIWpIB40qxbAomZgi/vIHHwxeXdN C200/G1fUpIIdLJNnENZYIoCE5hjrDGIY9k35eeXNaymGzGzcpo8mwndHosH1thK1QKI9 JyD1SJP+ehCnNzJLqFLLYwbv9G5R8F0UmETnHYwS6f4RETkt3f09lE9TL7WsFFYo9hw== 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=20251104 header.b=hoqMrJuO; 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 5F031606A33D for ; Thu, 22 Oct 2026 20:15:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5F031606A33D 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=hoqMrJuO Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-36998507c13so228516eec.0 for ; Thu, 22 Oct 2026 13:15:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700120; x=1793304920; 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=6JUxMMI/25kOrfypmhpeOOpRGxtyrkB4sVFmB/HCDt8=; b=hoqMrJuOb0Lgwz5zWOvROhCBYCLTVnK1T0SPtxbYqZyXqOFTMxrpWomm8DtuelfNX5 0o91IE449GczzV7rchkpwvsbgP4v3rc/4E6K9loGVD+e6vxcwi0CAdoZNpJHGO7arz4n Uyodg+dFWyHfBnYR5UjxyrXlCZf5DYQ6TOpILMGVUjDM0QKFea5/EHA6RXILtHT+WohQ olHyfeWD159BtPUfA4nuSPSCQk1LZR5NOES14xKx6Vt1ytjES3bNx8PtAz9m88dTwpcZ bIEUjNI5b3DlAIBt1AazqL320+fmzYo2B5gqiHJjb+bwRWaGAfxSiV2tZBSA20o0SiMc m0CA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700120; x=1793304920; 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=6JUxMMI/25kOrfypmhpeOOpRGxtyrkB4sVFmB/HCDt8=; b=PUk7rSHonGUByKHIkw15SnjXizowck2tFoxPI/Xk3gXQHMl7Lsu4zf0GiTVzqRbjK6 yfDplo7+wVSYcAzz6GuuHaqV2rdRvX8Bk0543KnJ++lFgUbNmJ3ZzkuCoV8L0iuDi4Kv sXbCxPKPPeiqJZ3lQpXxcdDAFgrNGKXy5ig/HEG7YmBYkBo9EfrfL2cCgzWkBjfqpfOM ivNm9aIBQ5qjQxz9AZWQWnG4ein4BmVgGpdS/BFxJimyH8OsCT8uebNgnEGj01kmFIoL geOnQunnWuoMb2yAPejR4UiuO8FdcRY/+0OhuHRm/YbxE7M5ztbMK988nfmnJd77wY/v Vp3g== X-Forwarded-Encrypted: i=1; AKwUvBxnvMU0QymBkVi3b1a/n2sPH0b5XYFdYoGeomzO9OZdtAIanlPOt7RBftgnOWK1ZuERJ0iRf+mLeWl7kWU=@vger.kernel.org X-Gm-Message-State: AFq9FYK/8cxyTAJPJ1zWcLIh1BeejqTKSyoUqrl2YbRo7fj/g5iP7vvv nTmLc4oQ4LVxorczJGdEOCPM+gLFelUbImcQSl0x/N5clwGArXFcqw1ve5Z5tD3Xb6uTz4yHf3U cR/A2RCYEGw== X-Received: from dyev8.prod.google.com ([2002:a05:7300:4308:b0:368:d484:7709]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:6505:b0:366:fbdb:2070 with SMTP id 5a478bee46e88-369d989eb57mr380050eec.34.1792700119173; Thu, 22 Oct 2026 13:15:19 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:38 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-6-irogers@google.com> Subject: [PATCH v8 05/19] perf build: Enable additional Clang thread-safety flags From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Align tools/perf's Clang thread-safety flags with the kernel's scripts/Makefile.context-analysis by adding -Wthread-safety-pointer, -Wthread-safety-beta, and (when supported by the compiler) -fexperimental-late-parse-attributes. Signed-off-by: Ian Rogers --- tools/perf/Makefile.config | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tools/perf/Makefile.config b/tools/perf/Makefile.config index 3eb8d095cda0..b50702bac53c 100644 --- a/tools/perf/Makefile.config +++ b/tools/perf/Makefile.config @@ -64,7 +64,13 @@ ifeq ($(CC_NO_CLANG), 0) endif =20 # Enabled Wthread-safety analysis for clang builds. - CFLAGS +=3D -Wthread-safety -DWARN_CONTEXT_ANALYSIS + CFLAGS +=3D -DWARN_CONTEXT_ANALYSIS -Wthread-safety + ifeq ($(shell $(CC) -Werror -Wthread-safety-pointer -Wthread-safety-beta= -c -x c /dev/null -o /dev/null >/dev/null 2>&1 && echo 1), 1) + CFLAGS +=3D -Wthread-safety-pointer -Wthread-safety-beta + endif + ifeq ($(shell $(CC) -Werror -fexperimental-late-parse-attributes -c -x c= /dev/null -o /dev/null >/dev/null 2>&1 && echo 1), 1) + CFLAGS +=3D -fexperimental-late-parse-attributes + endif endif =20 include $(srctree)/tools/scripts/Makefile.arch --=20 2.56.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700124; b=dhmCZD1rXFw8oRCvTtCjlrY1fpd/b5lf/P4xsxpPaHWatQLLIzpIn8lae2+8Msf6JU6N oF7apoyZtxvfnJTGr+oxaNInFo56K+UNq0xFvEqtB15impmVw8/N9zeYPt/LL0NbbkdTw LRMulT4Abu/GY6LNDF4T6sQnUxJtVTejLt3cMumz+3F2iU3LjCIWbKiTqkbFv8iB9C18Z MKKMI653qf/7xWA0EFFe+C0lN42A8Jd9+xgHcbXh0yPT5KWOUHfbfVCPMwNOIvw77lPbg M9mdKEvSF+puHH4FwoS+AslfhPqXPR28faV+xdEefg1ZaoA+xPowPl607FqLgC/eMcw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700124; 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=Bm5F5L/Tc7r70KhD0cqJblZLBdriiRHdrl4SVSE1UvA=; b=149UzyPDk0DjRKudl2wElwJtGu64+gazXaB0UCITJw5rWCe3TE6AV25LntYGzneBDWzM OYmQdfYNcPZ2aUvFezmfv4mfngyP2QTRerFb4DsIB48X/IiZCjWeWh7S8Z9jESpcMbKty erA0dveUAsKJGtaBS7JNyXzAdSM8vyJxQ5ZCPZHWkfUyk3Vipr/K/glH7C/odnV3Fhtep jwnaigADRnRXCnTqzXq+Bbvu0LYExN8hrVOySCnxOHNFF317gG3DB5sg4RFXImlTE+CBR hUyOlLlKr5nWHEmiI2+5lsi4lJ0ckYmH9fTGgsUrbXe4zo/uF1TW0nrnvi0oyU4iW9A== 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=20251104 header.b=Y1ViIcr7; 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 23F88606A321 for ; Thu, 22 Oct 2026 20:15:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 23F88606A321 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=Y1ViIcr7 Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-35c3c370b17so306556eec.0 for ; Thu, 22 Oct 2026 13:15:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700122; x=1793304922; 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=Bm5F5L/Tc7r70KhD0cqJblZLBdriiRHdrl4SVSE1UvA=; b=Y1ViIcr7Z4Ei53rNSNxXvhYmvT5mW7KS5XAe+/ynQIncC3u+IskaWAgLMAHpJlgzQd UloJfTCve6ZGxbXukIUqMsihjkHUyRLLhyPkOIgWnyn3+VCRU9aKn/7SrVLKqwyaMdot fUrmkanM8RB3giIdrTJxhD/PmoFjvWmddyA017AY/NSAHC3woCWtc+Fazims0eoNwT21 KynuyscKt9bD+dIXvxQOpjVolXmwURxh5EAlxvCecaRQ1UyemvaPZ8qLKoCRvmkYtVwy ncCFjGEGpuO2RjiwfUVE8/VK2NN3bsG/uJmpk3G9z2xLq7yppdgDQoz1rvf65W3nrHTU ShDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700122; x=1793304922; 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=Bm5F5L/Tc7r70KhD0cqJblZLBdriiRHdrl4SVSE1UvA=; b=dYOe24AuuIzxLu5CoxkVnNB7xemTD+CyIzdOmgG+HyNNJbeojHA1B4BLMyqEXE+RnF rOuKasskXu2EKVRjM4bIqm9e6+kCBrypArCBt9Fr/LnJ+Ey+O+DgUsU4mN5Nyi7ceHP1 ww4Qf8T2G0kUT2cYFAEu6lWi6ewoEy2UeTs/T4RGWHl2VtELN7JwO5SyjGu9qP4YpD3b WCdm38X3hoNOfcYzmSkrtQ3ghbUBvDnPpcHtGNaudzQ/bwJ3ZvMYe4kCw+4UvBY65deX GsBrl5JvBXGxo1/uuAO4VTavsV22EplF223Hr8EQVAREwI50H/2YxBlMkzroOQRZLnku yQ1A== X-Forwarded-Encrypted: i=1; AKwUvBy/ttZ8Q2fyh8dU652ox6oWAOEZkG5DthIvsiw1wYKCCYL2HMf5eTKSfZlwFSGfpA3rg7YNMUCAsI0/f0A=@vger.kernel.org X-Gm-Message-State: AFq9FYJl069LXzH6Iq9c0ogZThTcjmOINEAcLDWP4vbFfAKS98AVAA0q CDmEDAkiqxhDfRjz0RfciyFFHatj3kUEbppEF9wFu9mCGvQm1blKOh7vs/+7OybHhtxRMbBI8tQ PXw3+CGk+/g== X-Received: from dlbpu2.prod.google.com ([2002:a05:7022:e882:b0:166:1a44:a89]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:2302:b0:168:3243:c91a with SMTP id a92af1059eb24-195c44b7990mr296334c88.13.1792700121995; Thu, 22 Oct 2026 13:15:21 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:39 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-7-irogers@google.com> Subject: [PATCH v8 06/19] perf comm: Add __guarded_by annotations to struct comm_strs From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700125; b=aaR3foBn2wWz37q4ZlAjoDFJldypbNB0ufCujxAA1vWVN8dbNtJylgslyW2jnfidOlWt p9wsLFq1Qyy8iHDxS+s79RkKpnKUKqvg2pHWx2LJ4+T9lLyRQxxnEd1wqWOnd9H5sEe5j Iu6f/D/grLAyCDShpipMKbNcfex/paTgnaCLNFc/QQ0qXIpk9U0uvlUr4/02WAvPdp6V9 3MNrrrOiMyfhMWD0JsEXlaIADtr+OavOR0AYngshGCCIXXe299UjkdYynjlVDFe+B/p/k Y9OU5fZuzdmVxMRUvEpo6f6XCFSOGYURaAOrFs0LaD1XlGVlpD2dCIga+W424lON2QQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700125; 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=Io+g6hIKWvFq2IWPAUSEo1ar0ouYOg7HxlaXmXYkNnQ=; b=2sW3+NeuogDIQC2DTZPni6pX1u1rOMg5xQRkgXm3eyu7h42ZtWPBNmojmRZkvW8Jbwi4 6TdXBcgAGxhW2cIFRGPx/fle6erTmswxz1StmDOVf/QFFQmuM7j0F0IPfRzAJ5cxSH50e zK9URtc9DSZ1alOkwlYnkAr4U8n9QwdolvgNk7vIXRAFouSS0e1a1mdww6ruSssm0ExGC ApJUyfRi6XIStfB0Qru7LpJTyuoGhgl3pUCvHRK0PZOiIVECM4Mm7EYNwki2UvbubUp2B H6MuMzcwM7P2FH+3z+CNj3PUYDbhUPYwJSfnfl9o6MRdLfDUqEB4BV8NcIdBKA0Npkw== 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=20251104 header.b=a9FeYiim; 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 4D760606A324 for ; Thu, 22 Oct 2026 20:15:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4D760606A324 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=a9FeYiim Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-36998507c13so228667eec.0 for ; Thu, 22 Oct 2026 13:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700124; x=1793304924; 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=Io+g6hIKWvFq2IWPAUSEo1ar0ouYOg7HxlaXmXYkNnQ=; b=a9FeYiimmhn4X5R6cNiXmF4X1S6XQhg0zAR3LraW+OF4e2FYe6byxxbPmtXe5f2LMS 6+moRea98/ISg4FsKscEeVNzjdfNfePiO7jHdlHqpJZ7mbpEygFEKK96bDqnfHwau9Bx QCyUwjgdIVfs1RjS8Nrjeq9SD4v/XFLqilSnbFcVE70tOfhNNkTfL81YhCtQtmjsyYcr BHhc6nlrxeTQZpjT8KBQLOVV7vbIUVCK/G0k9PCwqQeARgiDo3R2GlMpaVRYq8uwFeEJ kZWobFOk/eoxK34ixwkQSYHhwIVB/QUkns0rGTyM17+cAVx0VFKQVbDBRIyS6GLYg4/W i79Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700124; x=1793304924; 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=Io+g6hIKWvFq2IWPAUSEo1ar0ouYOg7HxlaXmXYkNnQ=; b=h43HnBNJDgWXszSoLeznKdDHfg8eOpnrALwFQOUTkvCxfiq6gnO5i1i2ZPXkT5yzwT egOi5TLjRj/Ndn4F9aosbLWYdnt8JZJ6RavY9l/yWFUZ0DtXdKnoYsT5iWGIuoCH946A VHJXXYRTYisAdjSfsLKLrjNw9scI1yHfK/E8f/mBC8hBCqfltsmY//Kkn3CKSF63BFXz 0tA93UMCsvBFbS/Hvo4oJJ6qWAUP7vkzArSlghdomFVAXCwRcKar8klrSX4godO8huw2 vUoSkSislpYKTxa39eu+mrJKoLr+yPPxr193vyPICWmQ3RPxnRqTJx1C3t6WHIfajFWL Kq+A== X-Forwarded-Encrypted: i=1; AKwUvBwZJjyaxxSxsCa1syR2YgvTGGYL/nKufZxDgMBPeBuFVoiwHmeJX/3fVyArcFFgSVFMNpIKEkVk5w3TTW4=@vger.kernel.org X-Gm-Message-State: AFq9FYJFMZvRXeSPFO6B7XKx2yxdTL1MfjVv5CZGcJRXtYnsfZ2cya3x LJh1yhZ7oBjZsEIfk32FJW5uE6nqycukVZoT7LcbHjEINL3OHJqMK4qfqkSsOEHnSup9MHqBzxz 9vh4Y8G05OA== X-Received: from dlbsj2-n1.prod.google.com ([2002:a05:7022:f902:10b0:191:45cb:f8d3]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:6b8f:b0:170:64b:b388 with SMTP id a92af1059eb24-195c7e602f0mr528936c88.46.1792700124050; Thu, 22 Oct 2026 13:15:24 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:40 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-8-irogers@google.com> Subject: [PATCH v8 07/19] perf vdso: Fix unlocked __dsos__add() and race in __machine__addnew_vdso() From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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(). Signed-off-by: Ian Rogers --- tools/perf/util/dsos.c | 7 +++++- tools/perf/util/dsos.h | 1 + tools/perf/util/vdso.c | 52 +++++++++++++++++++++++++++--------------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index a2cd37bd1660..40267d3a725d 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -284,12 +284,17 @@ 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) +{ + 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..857a79fd8805 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -30,6 +30,7 @@ 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); 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 c12f5d8c4bf6..93828e660672 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(struct dso *dso) --=20 2.56.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700128; b=xKwMI1+rhxKvE5/wA3zL2lmyfrlQaxrNwPbmf0WVEUAsOKK4zKecgmHZ4onRrHKwRXTv yfFt6xnknJkLQlasxitubISv8V6SY1J0Dudx1cItvUEfDUuLZOaL4p6J3LVTtXIOZuZ08 UxePD8DBdWyC8SDnNIuAZubWpE6BUmx0JZV32HYbavxdxQC/Yz2qA6EVooH0rd/8UVGRT XzFuV1BCYKBa9pMDAfxP+6ADvjrWzjgm6FzLufKW2anQDt5Dq72VDfLgqi/ratPnyC3IY FMYrbl+nRWo4K7Q4C0ejS8S3+oM5QvBmxRKrHEWVNNGNhr99k0xSEu/lnthd4Huml/w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700128; 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=izXqkBzLnJqGM5xH5BnRMsFg7CLuWXtR4LH8CWJ8dTE=; b=1tE0CEGxsYTvtyTn57G1ogegzDaYGTCQQi9o2gikGkp1kbQJ4hKrBji18tIj8XCV5sOY onwmnHcr0hUSKrtqdzrw4Jygev36NLOg+4mBYPORD3M9bqBUf38NAw8Q1CMmrnd4wyavp 7VAqfLdEMyV3Frn0qW6n8SJwGAnD/NtXnLXhhdmQi94vWfUi1QxifkrOoMYSmNDl4K2kX 6ZvdDdjsGnxGbqGy0acg+prm+plTZ0wfcBCjq0qjzkubJ1bZggBgYCbAfpcvABbGECOO9 8oBBnsjwiOXluaLN8O74kWCfa7k52VL1Ahf68z0Oc+iRQPX4teEmb6+/FTFveE/yk/w== 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=20251104 header.b=ezStEsGc; 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 B994660A2601 for ; Thu, 22 Oct 2026 20:15:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B994660A2601 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=ezStEsGc Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-3510d0baf63so452544eec.1 for ; Thu, 22 Oct 2026 13:15:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700127; x=1793304927; 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=izXqkBzLnJqGM5xH5BnRMsFg7CLuWXtR4LH8CWJ8dTE=; b=ezStEsGchfAqfdVSXoS+Ufh869dXjxCb7PcDoezFzqvaSRnBi+9ZUxBwMQvwzoNAvq 4s2fwBb6Tg4FEKCEju8Fud24wR+4+8ydQh1PRhkFkY4Ff3nM2/JMQQn/kbS/L2xMjYMG UxkI1CQQJQ+4V6bWCeOQsHS7TiwCDdnzHvajKSfKHUww8ho1Ip+jQ3/DEBZQgTayt9Ez Ia+9pH/58/ht0Px2dP1kaiLsQSs4lIkJ+iJZirrPQbv0yhVfoFXQC9LWDZzoOpEXrRnc t2/+Ly59KFUCzOcQsw1QnP28jBF8uM1jkpUwDhXmyQpxyBIvsXUcu5MZVwkGizCLe0Yn Ta9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700127; x=1793304927; 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=izXqkBzLnJqGM5xH5BnRMsFg7CLuWXtR4LH8CWJ8dTE=; b=KYVRyWAvresZCKxFh8u4d7qD9nAdSuCVmFUdzxAxYMolXhTM5SmH/UR061tuBo3T7b c2PEEzNPasKrerLdjxDcmfzc5BHWqLLdXWecNS8Gm2P0ZLAxm0zj4hpT4oDfhuyntQFl 7lg8bUbR+wS+rxgSCAFSVoc9SsVAtQXcZNyOEeeLVuOyxN/zfInja83QXuD3wq4wlAxB PRZ48FrNZ63dAS5Lzp1xWCwEQGu8qy5eMLB3meczrXmg+9Ld+bY+pbhAaPmBYGBU6TR9 Bo7MVOLIM4rUoIk/ozAeDlTms62V6pKQGfEyF/zhyUzQFiPYReUx+OP4bdLMwS1+SQbM 5dTw== X-Forwarded-Encrypted: i=1; AKwUvBzmQas6diyXWbc8cs2RI+EbQNkBQUiBJxJ7raqHbFqn1q28KCe7dgSjcrQP2kWRGW4FTItmMTF3/mA0GUk=@vger.kernel.org X-Gm-Message-State: AFq9FYL9g3/2S/Fl2lCZPlXQAv2dpRsLs+P3+fivC0u/9ximRFe7Tcer qq9icbMI9JALB1EkevhgRRMIALUPodZBZmgdeR/cohQLkp0OYM8/LpkhqLFCtha0+ttcIL3ruqK +1DxepWv6tQ== X-Received: from dlbuy12.prod.google.com ([2002:a05:7022:1e0c:b0:18e:11a6:297a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:1305:b0:188:88b3:abda with SMTP id a92af1059eb24-195c65611a3mr316490c88.30.1792700125994; Thu, 22 Oct 2026 13:15:25 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:41 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-9-irogers@google.com> Subject: [PATCH v8 08/19] perf dsos: Skip BPF and OOL DSOs in dsos__find_kernel_dso() From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In machine__process_ksymbol_register(), set the binary type of non-OOL ksymbol DSOs to DSO_BINARY_TYPE__BPF_IMAGE (which machine__process_bpf_event_load() upgrades to DSO_BINARY_TYPE__BPF_PROG_INFO on PERF_BPF_EVENT_PROG_LOAD), and update dsos__find_kernel_dso_cb() to skip DSO_BINARY_TYPE__BPF_PROG_INFO, DSO_BINARY_TYPE__BPF_IMAGE, and DSO_BINARY_TYPE__OOL so that dsos__find_kernel_dso() does not match ksymbol DSOs in machine->dsos. Signed-off-by: Ian Rogers --- tools/perf/util/dsos.c | 3 +++ tools/perf/util/machine.c | 9 ++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index 40267d3a725d..2df0ce58421e 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -474,6 +474,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__binary_type(dso) =3D=3D DSO_BINARY_TYPE__BPF_PROG_INFO || + 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 1d9d3bf57720..cb77e82ed842 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -769,6 +769,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 { + dso__set_binary_type(dso, DSO_BINARY_TYPE__BPF_IMAGE); + if (is_bpf_image(event->ksymbol.name)) + dso__set_long_name(dso, "", false); } =20 map__set_start(map, event->ksymbol.addr); @@ -780,11 +784,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)); } --=20 2.56.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700130; b=YAY3tbkQ53hu/mB3CwOXZoBqdf7jrtmHARMdoxDgMUuXZk8ws3+YBt/+R1bSYjxzWsQ1 aJu8eHhPj/atJH2P0tDRsKEBsAzsNTlJDvI/gSouOZ7PtTd5g2kxCxt3wJFuFEa9Oiwn2 +59PXoWLy5823Fcf4SCmB59ZI463+L5mXxMCvlD5Tqvrje2KA+7MZK1/VAV+CheGbjhq9 9F/wnPcsXqwkeiED/mn3iLey1n3L4ZJ1QqTB5V6Z9yh2HAeWX2Gf0jJDKiXQQUSoaCKam i3HyTdrs6tSxRFgMgKo2Qw55VKCYIh/JCgstq13Yis3JtuTfrD8kIVks03f2H9ddUZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700130; 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=DnxUFuKhILNaqIex02bJDLwWD3shsgeaS+0XyX4ymrc=; b=aWZqmDjD9/ibgPwhrTeM11o7E0GQfHZemmfP79kajl4kzmi9Wvcy1MILj6HHUJzamidg +xPdyyDItKRO7u0N+qXAuRFP9S8Rv0OH5ZKIz9be1w8VQUgl1YZ0phLn5qhoSNt8dUmgq 1SWVxanbXGAB1Pmxja44XS1nz98gLl9ZxvT5sL6tvyCp1VtobbNqmICOdyqNaSjBzhwzN P1hzAbWLgDpGcko9+lXcMrqm47JWFTMW1LETmEQ2WIDWk/YqyM703cvYUCCKIFr4tS1qg 6TNi+xhujpFKlWUwxt9zN2CKc1q2mafVXAen0tHHFZ7xTSfjX84Mwtc3Sp40++F+YYg== 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=20251104 header.b=PEBNHHkL; 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 C0F28606A31F for ; Thu, 22 Oct 2026 20:15:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C0F28606A31F 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=PEBNHHkL Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-359b81ad4e8so245653eec.0 for ; Thu, 22 Oct 2026 13:15:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700129; x=1793304929; 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=DnxUFuKhILNaqIex02bJDLwWD3shsgeaS+0XyX4ymrc=; b=PEBNHHkL2J/W9Yek1ZPeFdZf0+GIxrbUQSz/X3HEFkf24EgwXTnTNKZyqrc832b0nM jdtjm80VVLcmUZHMaPE+NgNjVbvKb+mcyLdOq+ue7Q8Ieyk13HdQhTrSbynfU0Fqn1cL xDOhzjksTxleZQxwbQu1cEFsRI5Fv0qYZeyv89JvumIy/gyfH/y/Paxlh7UciMBuBQaO MzTPDaW87LM+OCSYsxXR4KY4JJNppE8LaJ0CM4gV82DJW1Ln69wQviszBpGvzFs+AT60 YXCJ/ccNIiN8ikADwMpq6wxeShycyu7ScrvAy2dlWdMo9pViqpnLJR5ouoc0umlSQQBQ uY7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700129; x=1793304929; 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=DnxUFuKhILNaqIex02bJDLwWD3shsgeaS+0XyX4ymrc=; b=rmknaGTv7zlAYM0wJbS9VrJa9GMvExUpkCObNgI38egay1sJimT/ruqrlagKGJy86F r2HMRBSK/YD5R1TbHcr22Pe938QMiN4wDDBAHBgCCQWA6a2TNhR5UE9l669KeMFoMw4c Jmbq5Bzf/ApkXLbzmKw5BQobodcr4NjguaLSvS0kEyVLIjTBpJQ+bCi37ZfIi0kSdOZm oPLuGYISmCwt5p1VdBO2oTulOPGaTnnXoQiFRe4OGXyi8J0zTqueZLV65+gfz7iq1iOY XGFAekdXRjdH6kNgN5v38gayz1TuKiwnp6WvaUcZy9wkVZciQxH/Foo8EjEp+8YgxTdc fr9Q== X-Forwarded-Encrypted: i=1; AKwUvBzrk8jo2dJoyE1XRURfm2JVkxyhM+vRPySYZ0roexMHFf7CQDVBD1Z3zAv9DMOlyoL08qv1YtYDkVdOj2E=@vger.kernel.org X-Gm-Message-State: AFq9FYJDmZQkXXq2Nftq3M/sIxiHtdjeiSpwQjFOvCtkLsEGKwx52r67 n1C2CAvR2Aoht6zRPtlRtWbBrc2WWlzy+onY4W2Xo0wJB5CdHtvAoBz3fwZAzgFkpciheDubIKu FpwQGfA1uDQ== X-Received: from dled1-n2.prod.google.com ([2002:a05:701b:42c1:20b0:195:98a8:914e]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:1213:b0:178:d38c:eafa with SMTP id a92af1059eb24-195c59dcb8dmr364254c88.21.1792700128457; Thu, 22 Oct 2026 13:15:28 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:42 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-10-irogers@google.com> Subject: [PATCH v8 09/19] perf dsos: Ensure DSOs in auxtrace, machine, and symbol are in dsos From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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 f30f86156a3a..5fdbd77eb451 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 cb77e82ed842..5135ecfa0467 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -775,6 +775,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 b61a574ce9a3..686fc48841c6 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1090,6 +1090,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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700133; b=rUcCHnlAxv7zfmraWx4F1/lIMjfq1Ffn8ct+5AtJVF6uZ4bAAxQGwrUzA/xeMaor/vBv p1W3iocVz07QDVCRi23mXsTNlC1oCFq/qRFfFzBuMMtdMsBsZiRdKtJSUkcJaES1WdivY C+a55ot/8BUhuqSuezKPJ1/o/jbWnjf5Te6B5qogHl/Mo+tJDTJWSidTimBM0qKnBofER AdZGyE/4NffvqDSakq+mm5QTgKtq4pCvXs7PyLWsPmQX7/8gb1av8O1FC1Iam2EGzl5Dn +O4FAbAN820zCAto/by/Akfnj6uBQO4MBqhqjW6/y6ljgEX6lWOJEX9qdcNXaFA2cbA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700133; 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=mdDZk+7LKcsMH2iquwAxU7dTQOtp6sq4vfpfrMCEV+o=; b=1XC2KbcflZZz8BFAY+nLbBdSKuBUfA/9JKEkydtI9zneFtlM4OuvSDkH1rim5K4ZN5X9 r1SVhOb13juTM4mKj/Zr9xyHY7/Te+AM7yJEZwH06IAp+QjtK+pTV87mOX8lQdFWrNJSt W88TSVBWo5oD2PCJPS2YtzXWcupRCrrTHCQxwHtHav+KgueJgb8YgaliSq2fjp0BmGeQU VsHv05q+CY+S1yBgW8YbJxMv8nxlBtfv8NSY+spAGfc0nCsj0T+J+wZRQGX2bVOpz1QXc ReNb1uhVO2cmCzaW15780yQXT8JEZB8tHD0UrbH/xuYl+3BWyOP//tbrjIRimTsjg9A== 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=20251104 header.b=nP0Px44c; 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 F3653606A31D for ; Thu, 22 Oct 2026 20:15:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F3653606A31D 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=nP0Px44c Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-35c902e0da2so1526322eec.1 for ; Thu, 22 Oct 2026 13:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700132; x=1793304932; 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=mdDZk+7LKcsMH2iquwAxU7dTQOtp6sq4vfpfrMCEV+o=; b=nP0Px44cgoXkR8MdCF4M66/k5Imy6By3aoc7QkzGJTZT4C9lp6BWZmNV80tg/d/7x+ TQuM4dhc3VY4Znpqt06admKj43XiEsQeL+bSByyd/7p/pFD0KJxtqwqYDQbB1zj6Hf+P TGPhV1K849c+sU1WIWIb3BWGzhau+G5Zer3zgbudnvglfs6Wq8ipQeHXmz23CImdeDrr DhGWa6VyFm55HO2YHjVel3yFYTzenffkVuPmnNtaQVZp1+STTmOsqoZdTN2UkZlxEhk4 cOwfd3M84q8aRGoBVw+u9qEnH8owV95TpG/W8xxLoM4IUpZwrV7l8Yz9krLdT0GGUkby U13w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700132; x=1793304932; 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=mdDZk+7LKcsMH2iquwAxU7dTQOtp6sq4vfpfrMCEV+o=; b=pdIHIu6jvyAJe9ihsjelIz4G8Rz0pHYNt4hk/wrisSOC2YOTo4euwmW/C4nc9lCECr ohFDcmYYzb5rTzT/CMItMhMI21Xm/FetYpy+eftkit8Vshgkpcvkd+Q1d3mWP7cICk4V Wg0nrFr3XmpY9yS/rB9DeDEr3veFtU+jXLLVDnc7mN2MfMQszB0c6wE08FitqYGIOvBY nkkonPjEkawuIZyty3tQPYx7iqKJ+iJVn7JbOhZX4ULfAtUlJtiz9WIr2cvnnpVNQSKp E1ez3/ZFlsfoUhRGcyMg2KnltJeLpfinjKp71UT9VUp8eH6zieXbNN9Ma0PvDpHpye5A aGsw== X-Forwarded-Encrypted: i=1; AKwUvBwPn2FvIrMXFPsKy5QycnJ5uCi2ZT1iZkPrfqW+VlMCuzMlCxQFVVaIMD7BEXhhiN49W5b+dCaKsgknSa8=@vger.kernel.org X-Gm-Message-State: AFq9FYJE/ZcEWR7iNzzMdbg6y+KErXgh1bp3TdQaXMQQW/38duXGvlUx sqiNC6Pqm5Ul5g+yEL+yMyomOJ3JRuK2feNL77aB7igw9ZFd/Iwii74L8Bh+JTcK21rSZWXmJyT OKl+Qk/S0qw== X-Received: from dlbur22-n2.prod.google.com ([2002:a05:7022:ea56:20b0:191:1417:6caa]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:370b:b0:14e:d0ba:75af with SMTP id a92af1059eb24-19599bd3a04mr550541c88.6.1792700131706; Thu, 22 Oct 2026 13:15:31 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:43 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-11-irogers@google.com> Subject: [PATCH v8 10/19] perf dso: Pass dso_data to close_data_fd() and dso__list_del() From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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 24ff066b9878..cbc4f30e7f23 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 fa5669709eb3..d6b0a1a4dac9 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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700135; b=rX+p5aVzQifqUl8ZYN6S8JyxpVdJ6lfOIQHVkCtwiJgfM1jg5BbsOofKhswlkKu6VWt2 tYLmyji/wg0TgdA1qvDYSC3NdImFcQnSRgGN3hNWyzF+h2wbHWWc5+tHjdL8HmXPLP9sn QvJeBH1pVaVN+HioZKahIT5VQZNA9D0hd7YVLqalZM3GnYLb8wyQC7LahuZAH1QFm28+d QuCox7Ww+jH1XAdzeQ7xMDXkf53tVvnblWwtcPPJ0S58NqHLaM1Ozerr5tsPhzfnv03HN 0U8dsDwFuafWb6/OSDRKlH9UbpKwH81d/12FpujELl2ydaPmsTzjhmPzeYyekMKQ2cA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700135; 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=/sw3Zy3QfbyRHMtojg/LPx/IqMxZKrch2S1ubTvpkr4=; b=eoU+Tnie+Yc426zdi/x4oK1IaP3+LDA/VgIxhHHu9BfFbciqv7jTvkSVZfFzhsbVOGpm oSSOTiAP99lLK7QEuVfIAMPKRLReUuZvi8f4UUs1qd/pGmIgQ/gbRF20NXgpHAyJ9Uq69 t7LdgUKMQFHn8rgyJNJ+9Xa5EryYyeRZUgs29MB4puyaL5xOA2T+6VTugnG/89Da5rxDv 1/SgyYMowmnVfrdpuL/aWPtmfiZOpWCD2qoHTerIAUiLyur/O+W+UfEBvVUbXmzRDOGPf 393fwUusx1/H/iei2gvdRlhjfy3j+m4tfGLXEXfGL9VFJ7RRg7ISmhd4MTaSN0FhGUg== 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=20251104 header.b=U9DvVgYd; 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 598DF60A2612 for ; Thu, 22 Oct 2026 20:15:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 598DF60A2612 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=U9DvVgYd Received: by mail-dy1-f200.google.com with SMTP id 5a478bee46e88-33713e5e6daso316657eec.0 for ; Thu, 22 Oct 2026 13:15:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700134; x=1793304934; 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=/sw3Zy3QfbyRHMtojg/LPx/IqMxZKrch2S1ubTvpkr4=; b=U9DvVgYd8WewwplEm4jKAonDifVsVuGChcvDw3UWvu6ZRskjfjJ9OuGX0xkz4och+k GtJpXMH+s7qAdlZS7JfuAD+WijCAZAl6550etN5SCoMWwrztsvONS1q2lyGgllI06OZq oObDZiE8ytS9yaGbHNoRKqcCr8l3RuyWIrCVbMmVX8nNMxPvVXJI6UGkYFDyZzRhXyVD KSOrMcfknERHVYUmogKjixpsyFLhyIAKL0UNXpKhLskNg+HWnyzFM20BE8bzngfW9gR5 9ubXGN05AkBwjNHafZyl8YUSRx0RYAga6HO6I6imS/Jcohvl7i+qvOAaGEw0ScYVT4l5 b7ww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700134; x=1793304934; 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=/sw3Zy3QfbyRHMtojg/LPx/IqMxZKrch2S1ubTvpkr4=; b=z/F351No/WwQjm3h07CPyEnWFMNggfWU2pxo4lYSHFDieBzCRwwFaksIZyFclxPZUV EIZq7WmyH1eP3lhVXa0yHh10NgIVXFY3U9SoedH0bHzlVlEKVQGO6Vz/I2b+lDdIT4Om nR5BnOJQtT1xd8bA5CmJL3Up50qgTdSZD06naFdwJcN33hSJvD1MrDMwBQA+BUFvU+Uu 7eispTV//PAwgu3WqN7w9Vq3ZjDLEIaB27whl8fdgWnHumIQsxFKMEjRwGAI/IE6jsw5 exaQYEvyKO6RChhkUpf/lIu438+q/AvblxRnFQBFvXoNBpuWD2G6Z1OzJN1JMNqY1v7N i/Jw== X-Forwarded-Encrypted: i=1; AKwUvBxmWYiHy50CjqmkE6gbKHIYNWIqLMx1QY+bGRi14gNPMg6JmgCdY6fqAd7b9h0nDYyIx5PTnMFXB1a/ukE=@vger.kernel.org X-Gm-Message-State: AFq9FYJPfKqN1g5NOrXfItylO6mEDHqnHTDC6K9O0yEoAVYxSyho5/0r udbUlwYWteU9zdc+WmfRErCRlMFSiJONVIDAvu5DCmPgb67zZAEqFwwhXSsCXEl3++HqzGuu3i1 wQ9FR1nJFuw== X-Received: from dycmz2.prod.google.com ([2002:a05:7300:ea02:b0:369:59e9:b1d0]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7301:152c:b0:34e:f948:d7da with SMTP id 5a478bee46e88-369d972fffcmr252999eec.21.1792700133994; Thu, 22 Oct 2026 13:15:33 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:44 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-12-irogers@google.com> Subject: [PATCH v8 11/19] perf dso: Use DEFINE_MUTEX for dso__data_open_lock From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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 cbc4f30e7f23..f0e02062c09c 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 d6b0a1a4dac9..9f026ccb0eb6 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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700137; b=GL5qCXyWEwEZXRpcVTQwSZo3DH9UtWWhYvqMhlWHn3+DsKMTsiRX2qVXrcKarA4ddImy kHI4udQWcU38i6GPRT8S3a3XZ/bmVGqxVwxRM49XGEw+stVbAC1aCjMBQKewqYot848oo gZUsYf7WlURQ3B+vhVVUoCt5OyZfpo9jxkwSAf6D3skXscEgcASd0y9OwixOv2qtiD3ph HnqrEt0oy6Gw04ar8ui2rh4Fxq0CQyV68vkqtzc4z3Xdf/gkC67crTTMoaXjwjcHrrlND D9mGeAKPr7qDfp78UlUEPAVCjlO9rUNYOdKzngFEhF97QVNb06q36rmGEfGU6+a9bsg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700137; 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=rC4lnjftw6rIxuKKEP8wYqbMgtFehbcRX6Ia8qs7pZI=; b=IiycKleZg68q9xrpCgywZY9mc4C3Ee5T+KIldm5tJE7Bjd8uFmFB94ZHaXnuBhVyqjUG Bea3NEcp7davpw8xiMQjeYWpeTKPFb+v1A9ZIUsX9CoKjpWYw9SPSgi1yPAB+S/rT/Epv PNPsZ9sZJNJBeEvwspfDpUcgzIYwE6rxBSEO80UElnNiDgY2jMXae7XPmwGapGpgAxqqL yOMQsc+RhaOfxI2W4miGvHfl6Il9L0D0DCPrUiPvSYoZT8vQbziNsaVGMoerKcf5yiJ4x NaQfUghYAR126uu7HF+zmxriZsMoqMEMuroBO6L67jfpt+AKbTrqmCjXzfT1AQxlDYg== 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=20251104 header.b=LxC4v93W; 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 61A9960A2601 for ; Thu, 22 Oct 2026 20:15:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 61A9960A2601 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=LxC4v93W Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-1663fb7e005so172766c88.0 for ; Thu, 22 Oct 2026 13:15:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700136; x=1793304936; 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=rC4lnjftw6rIxuKKEP8wYqbMgtFehbcRX6Ia8qs7pZI=; b=LxC4v93WT5J6QA11ow/EiaxkSAio7NS7fI6kJeHGEa2tzf4vDvpaOPbh8L4Isk1trQ d4agkrj9WQrNSxCoHr+S5zTizHo4DFpF7fGre9PubBMaatGRQFfACDG3gjZY0fstqxvg hfeGK5wv/plgEoA9tndwmPWDX4oCW9ViQqz/i5RzcEc0df8mxyrl2psV2nj9Dm9QQHqV lkJobyw79GskvIG0EWzc3S8fdPbAHgtMV4NiCgJhSyMnE9yzTKlb3iK3lJTi4s/52/wq WfQPWY+AK8tDorDDYtq79bxbRdhlRsLLtdHd0QAG+J4++VpWh/EHpFCwE26FVuAUJVHA O5tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700136; x=1793304936; 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=rC4lnjftw6rIxuKKEP8wYqbMgtFehbcRX6Ia8qs7pZI=; b=2ApbB0a3tL7wYKJaV6eiInb9uQCkawSOaf5BbwhLMt/80j1BjoqyCXXnt32DQLlIQu V2ABpm3swZDHWdqx0XpX/zNffDGZOmZBQ0AfBbwkPRaF65zBUC1dEY+vwPH4v7DJZTjc GJ/McELhR7ZNTFD9xRJknM5xSwJwGUS7o2iEu3wMlJMGYmkx/Ri7qEUrWJxzzycBRFjg 5VN/iH6YgrdAxeopfcLZXycfNjXFABYcndLA237AGnl5GSwimKZW5lV6Vb6FNLxiISYJ 7LvO3vbSONTjmSk5PQhJjHvRr8DlE/NDOZ6U6FRXWPcGFW6hq6OaVi/hgoIEkc0cswD5 1YSQ== X-Forwarded-Encrypted: i=1; AKwUvBwJl4fEWEfpC7if1XscdZDRWhadzWuxMFklaFqOssbr9x1YiIhacnO0WETzHnFaCp4svHBXTCu9acQMrEs=@vger.kernel.org X-Gm-Message-State: AFq9FYI3FlFJBtwengYTTbG4/SwqdESDpoKWKfFIFQb64Nru1OpLq1dJ t8CJ33d3EFlk0gNjGHJKwaoTGiHrEu7bhpQxv5AKcVNDgIBlp46DzD9rDQ7nYTxIeCUmKKU8Mbm 8OkI7CyIpUg== X-Received: from dled6-n2.prod.google.com ([2002:a05:701b:42c6:20b0:191:8b40:a632]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4511:20b0:18d:5ef2:1644 with SMTP id a92af1059eb24-19599707360mr525513c88.30.1792700135908; Thu, 22 Oct 2026 13:15:35 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:45 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-13-irogers@google.com> Subject: [PATCH v8 12/19] perf dsos: Use read lock fast path in dsos__findnew_id() From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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()). Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 16 +++++++++++++++- tools/perf/util/dso.h | 1 + tools/perf/util/dsos.c | 10 ++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index f0e02062c09c..4dc518f7cb7d 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1685,6 +1685,18 @@ 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) + return false; + + return (id->mmap2_valid && !dso_id->mmap2_valid) || + (id->mmap2_ino_generation_valid && !dso_id->mmap2_ino_generation_v= alid) || + (build_id__is_defined(&id->build_id) && !build_id__is_defined(&dso= _id->build_id)); +} + void __dso__improve_id(struct dso *dso, const struct dso_id *id) { struct dsos *dsos =3D dso__dsos(dso); @@ -1692,6 +1704,8 @@ void __dso__improve_id(struct dso *dso, const struct = dso_id *id) bool changed =3D false; =20 /* dsos write lock held by caller. */ + if (!id) + return; =20 if (id->mmap2_valid && !dso_id->mmap2_valid) { dso_id->maj =3D id->maj; @@ -1715,7 +1729,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 9f026ccb0eb6..394960cd2004 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 2df0ce58421e..ff58ac2566cf 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -348,6 +348,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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700140; b=XMDiCk6N4jWdqX0PoCyyauki/ZfKrMa47+HIBgQZKKrYD8MjnLkr/KCKhN5qEE24Ew8H qot55fb+7p8kDYmUOaMsGmW48CVwdSUW1NdxwIlUXgSh+2HDXuZ79yAr4ASwJ1slTaYp7 mgHT3C4GjSjBJmrYRK4RMZ4NLjiAEJphYqqGkwq8QtiUbK1wirtBsL1wV1LnKyhPtJNFG jdkzMoU81TnBsl1NRLxc/HlkXg1I1Q5YrLjxitEHSCpGHt6dFGbBOxV/PgoNdDUa3uy08 ZFelQhFR8Vw3AkXu9cgTDtFr2yAxwT9N3RFgIYuktzVzFgj9xrvoSzX/rf8l0YlloBQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700140; 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=e89dsJnrgTqWNie6z3l9SkAKLW3jt2Md0h+tKjeIm1s=; b=2CE38wdv74mhR/IIX3SkqUsGmd8WP0H+iT7OTAjFFwtpiUsN+Mjp5DGywYM4g9lJgUXf PpY2ftv1XJW25ssrjgd28N6JzfxQeUFphF8znLHjg9psGorDeCnNTms+4RTs9ai5WbeWG iShozjeDujYMv1ron5xKQtqfVURiDpQDAUGxBOcB1cmbeECJt0hKIEgkyMoXovJpHXRam SmY7TUilIANbh0EQRcrQKDFWyDZsJeDqVtLCBUTVujnMOBW7bpzgNWmk6YBQprLQngumQ d8S8+RTXNC70yASNdns5IE5D7lAle8Me+xgShe8prOTFr+ShWU7CTLSmolKEDBkgDaw== 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=20251104 header.b=KmqgEqTH; 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 2F9E5606A321 for ; Thu, 22 Oct 2026 20:15:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2F9E5606A321 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=KmqgEqTH Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-169846a19aaso95596c88.0 for ; Thu, 22 Oct 2026 13:15:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700139; x=1793304939; 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=e89dsJnrgTqWNie6z3l9SkAKLW3jt2Md0h+tKjeIm1s=; b=KmqgEqTHABpJsWouhf3WUE6ACtLdMDm2iKGg1KOXEhJopYY/zsSf9Gk3FcILoaqghG bIMTbB1z+QuEIkIWGCgX/3+moCdvKHYidrGTbjOmFNz5aFrlTKnzfeQg2oIthDNmO7Cx lRTzbXATdDG/lBh1eCWyzllZIn4jHalWoQS5/9ScFF8W1AsE56M+fkI6grMD4FNGvPo+ WHyPvJ4kO8S7jgJySYBpu2/korCEodMUyo3nJAcCBm7zYi0qUw/gu5K3Eqr2irQKXz9l oNqGKTr7mdKTh8XCGQB/25CxopkBZ409ujixY8oBgmksK2ycjo9CyMnahZHWux3iuyHH jG7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700139; x=1793304939; 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=e89dsJnrgTqWNie6z3l9SkAKLW3jt2Md0h+tKjeIm1s=; b=Nms+EjQ70srqdl/mFCtsGanR2BNCVQjEuxvS6bRLeUbAXhkr63V8widQvdjh9ccDRD +sg4RNm27R97gIpBTMHGAcLVMqZWFqpiOMFhPsRs2NRFxOIycQ+szo8vQOTk476Pf4pD Qjmg6tGopwl7NYLoFatFrdRWT5q8h7bRtvfj9tLslyf0EQKXbaxylre5bkjG5LZWpm9g b9xiugNJTJ93VgOXqoMUFQGUcD10qSB01dkd9jPuKd637HG4IBtunKRTvW+XHrRr3RGL mTtlAknxggG7G9p8ceCU2HId1CWJRjIMaJ+RYu3IVFOey5fDRHIRAMQT9TiAcDrbcWOO o3AA== X-Forwarded-Encrypted: i=1; AKwUvBxG04jiXwlpEjadB2WTecYGzeCnKPwgZgu/9j6pBf70SanZoN2zSwYehUoQsFpJFYFOuKP+GeddgGzlaOs=@vger.kernel.org X-Gm-Message-State: AFq9FYJCfK9wx3N3OxcXLXPPz8ibie2PKwSjtQghGmicGTSDcLGsRUYP f4kWlX+nQH39ilWouXhvSh/kImDNZsGNrZsH99QTDtFc5Vaz80kPoTdu2mOe/C0nJBwn9BriKO5 JrWgMCApfsg== X-Received: from dlbvv8.prod.google.com ([2002:a05:7022:5f08:b0:191:3c0c:1269]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:220b:b0:187:eb7f:b8f with SMTP id a92af1059eb24-195c4d60aadmr425813c88.13.1792700138931; Thu, 22 Oct 2026 13:15:38 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:46 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-14-irogers@google.com> Subject: [PATCH v8 13/19] perf dso: Expand context analysis annotations From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Annotate struct dsos fields with __guarded_by(&lock), annotate dso__data_op= en 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 | 10 ++++++++-- tools/perf/util/dsos.h | 13 +++++++------ tools/perf/util/vdso.c | 1 + 5 files changed, 29 insertions(+), 13 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 4dc518f7cb7d..5f4b4cd9a089 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) @@ -1723,8 +1725,10 @@ void __dso__improve_id(struct dso *dso, const struct= dso_id *id) dso_id->build_id =3D id->build_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 ff58ac2566cf..7f3a6dabf8a7 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; @@ -285,6 +289,7 @@ static struct dso *__dsos__find_id(struct dsos *dsos, c= onst char *name, const st } =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= ); } @@ -319,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 @@ -335,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 857a79fd8805..cc7e3a42f9cb 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -19,18 +19,19 @@ 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); +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 93828e660672..fc75c0cf372f 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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700142; b=TuqnfFa0/7334ZTW8VO/UmfjDE194+HENMMxkzNPexnqCy7+wRgb8zDM+eNpRrP64yie ClK/fgukUxJYFjWWKlhcV8mKGrrM8sDAD3xXMkGnqArT1SzYw3TGmbNrwwAPhkR/fzI6u /Q2a5V4IZ/j79iHNyUMuqr5ToTOmp1niA+THzUUTDXuvlLyok+ZQb+pxpoTN3132Ig55X 2JS25gu3ptt8i98wyEzg9MELFxbi6vD4lzPg8NO/d/3V+qM/2gHeE3S/1Xp2DJgJvh7sy zyoyE9EoVgVZyhZ5cczqMJ/dPAN8KqRO290MfP7lcgjT3p/NPFPhkPiBorxBmBZjwow== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700142; 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=epHYqq259OlvmbM5bUfE9T1RK5z8edIqXpc6UY2H9Ks=; b=dZ0P5jV6W2Cjf4ibahohfjQDaRFPkEUZOt/YtZLupcbi3fL7zmivbnuhsUiYE5bHWpqd KCBsc8wsuoKKhJXoz3C5LoNM4/zLQ5Gty23IC87uOkJ3DpsotMjbHiEAx+cRxp6smpTpf nDzL40Tia33WKFrlcv/RwEc48DjPktevvX2loJDGtz8gfZ2fhMe6QPEe7QD3x+sYLlRmf bxNy7sPDXWiA4FvcaF11chLSr7uvuDpILTYWpvXqVLAdsi+XCKPxiPiYA9Lp8HElqPBaK 1PL/pDWa3XUyNcYoKh2wBb5HgtvJ7rq4HQvEfeqh3VVUR5L40XHHQQxYdtEIBR+GAOA== 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=20251104 header.b=pig3D3O8; 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 3CA89606A33C for ; Thu, 22 Oct 2026 20:15:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 3CA89606A33C 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=pig3D3O8 Received: by mail-dl1-f71.google.com with SMTP id a92af1059eb24-17dc865c94cso120529c88.1 for ; Thu, 22 Oct 2026 13:15:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700141; x=1793304941; 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=epHYqq259OlvmbM5bUfE9T1RK5z8edIqXpc6UY2H9Ks=; b=pig3D3O8lLh73bJyvZFnj160+oAf4Yr9TS8vbnRHUlfhOC0d6QBxFmZF/RCwzLCWhb Fry/LOKMyQ7mpp5aROw+3JzlliZqALH2D3ZAg3rtItJZADIqgzua8NEmFcAuOH3VWhhb 0oYlFtyfIYw5OgSYU6q6KFhMTKsGayupuhFyrwXBHWYy+1rjcBiB+8xEtFGwwiERuK4I G2oZDHfaCzoPhAu4UaTznJqezGhw45yhjpveOBqRUr/I9Eae6Na2JylA7nJIMWcE1tog S8kWf0zsbiirbmzVqeocTgc7DOlh3M5iRWrWQBGvdEF9Q34PI0wVDF/vOtI8Bb93rUjW IAPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700141; x=1793304941; 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=epHYqq259OlvmbM5bUfE9T1RK5z8edIqXpc6UY2H9Ks=; b=VnbHNOTXxkCtQusag6ghDrrBvga+kxYGoPrlRTEZVE9+oFoOCnaIrhexvTIxwRej0M swqozHd1Usn6xbinTUVNmhGzmm+t54rYmMCYO1wmHFdkpE/5awKojuM5aufkJVnxuoCp JjD7qKWLJWlQNGtwPM+cHy6REj5ajZWcCtDUXvjATqM1h3XRIDvDbLkBwAUBeGf6OzKY z18PAeydP7nXsXD/LmX5FFHt0BII+txuMwAqwGCPJhVR1Kc4btW32rtVcFLfXF5YlYf1 H1wYX2GM4f4aE9QjeI7XbxZ7UMRsClhYtUwE1vNuCZnNlosnGk3HyYXCh6na7lr40I32 5z0A== X-Forwarded-Encrypted: i=1; AKwUvBxuePOl5OB4h/zBZtekQW+/29ZN2NM1AA9/fQLk5b15f1oYcDTd5zou//+X9v6mZeFWJz6I2jAk0SLk48Y=@vger.kernel.org X-Gm-Message-State: AFq9FYI73z+aLP+UJ0RFSHpCmKaT2jRLzuaJnBONuIkEmRha3orjjW74 87IkQs/z61yyJvM7R9UyvIVczrZFi6+EdKohTocvkdaP9QNalvvdoWqBjmohptqGavUVDIeigI9 O0vS/Zrxbsw== X-Received: from dlam18-n1.prod.google.com ([2002:a05:701b:2092:10b0:15d:bfd9:370b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:404:b0:147:8a40:cdf3 with SMTP id a92af1059eb24-195c5ad11d9mr579396c88.24.1792700140868; Thu, 22 Oct 2026 13:15:40 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:47 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-15-irogers@google.com> Subject: [PATCH v8 14/19] perf thread: Expand context analysis annotations to threads and namespaces_lock From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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 6ca0b178fb6c..39b3c420635a 100644 --- a/tools/perf/util/threads.c +++ b/tools/perf/util/threads.c @@ -21,6 +21,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]; @@ -32,6 +33,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++) { @@ -63,6 +65,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 @@ -76,6 +79,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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700149; b=Jaa2K5hYjYgEnfglfUzS43VxezTbZa7f5LQUrZRBnuo0/QD9apgL305N875Ap04QB0Ls BCK8E8g/uz2c/i1jWuT/L1WP1rEkdiKI4i+ykAZcZQ9vDCFqqbTLXMhOa3ycbSwhtTCio zQwRJ2EqWpeyRPWqxPy16Wb20Mi38yhB2hJIqROnDCyr0OTZU6GNUSJPDOmbweNJYYCbf e2n6QLAjzrgSXdr812dl+bbFYInMoO5IsbwUPJLG8shvHNaARfWgCi1X6UWYRy7nFvVdw 7jLhKV/Sog9qWD5qRIOxpW44MJUbiyc8/QephmQ2byUCe0tn80Bgql9ZiBuuUGi/PFg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700149; 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=2xDkyVwKOR14cu0DSimvn78NCfE/+WZV/F/W5kCR4EI=; b=nkLbF+jXjjYso17f/6pdqq2MN2tNj2Eb3+WrTwlQ30uxaxEWaHD7rNQXQpLMhl9/u+Qa e3ap75apA3+DVSQC2qbAgQSC3Wb69WKiyXjxSEPC79XvFlGVOLvVk3wLprqzKIkXRQJSW IREe8NfZOUsjhpRa3zQIcf8w23r4HrH+QSSjx0xMYPMSD/2jTgMc1ueRpXt7GyShvN2it k0v+aMokts5H9IErv237fXCby8cKLPM7eVbF7712910XpJ17M4uzH4Xs61OVhmXID9Byg JrsonAPLDgUDY4dLsFph4zpWfoFCmaw1TIGHzIlNkre7tMpMPl3fSx+3o/4vVwO7y5A== 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=20251104 header.b=VyXX0gr+; 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 8437160A2617 for ; Thu, 22 Oct 2026 20:15:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8437160A2617 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=VyXX0gr+ Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-33c35f5ca6cso276366eec.1 for ; Thu, 22 Oct 2026 13:15:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700149; x=1793304949; 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=2xDkyVwKOR14cu0DSimvn78NCfE/+WZV/F/W5kCR4EI=; b=VyXX0gr+sBaRxXxNGuq3Dw8uX+XUgA3b99aidyguVrUZRT1nlUyIa66CtHvMwdwUHw +7d3v9FW70+nJlCf3w5k/tGSPkLMxh7REBSHxzu5btEKBfB5N8yaiCWDbpjyLZ+4cJSC L3Xhzs4/94+2/DpXqnO7ssjDdcdbpb8+G9qN8ilm6QoUh0k3lST2SNOMLpCqqhuLZCEa 6WHLzFB9m24Vg6S7FLaUhY3m9nOMJfF5RYfC2j0Vw+T8gRb/r/M1bhSC+ar1IayqlQkL jH2kgOPR2ioFvLLwzAOL+GjCEDojGf9raPiZD4aeQL6QWjM6REYRJ3MVMqivc1ZwXgle iqDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700149; x=1793304949; 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=2xDkyVwKOR14cu0DSimvn78NCfE/+WZV/F/W5kCR4EI=; b=rf5TSV9cSD7aBKRRKT97X8NOWJPG3YBOGSr7o9trC8OxKGYvORgamgcuDyjSSlg4Cz a3dBA2LIGzYb+OqiKPcka+IEIaT/SEn09DQ/+O81mGjw+k9+CozmZnaVlEohd51falRR MEfsO3trhCMbk4j/139L6LFbTQ4iPfxMoWadPvdMuUyK3nYByF/ZA7mkVNEsx3P4YzXm CBi5fAUv0sVehH8sed/5LRwpL80SPAwQV1Lb91/kAEqqRR7DLI96Xkf+1ruH/czzFPuJ 95Vog4/FlxXhsjbltvbCWEGzFsj5ZaAK9qCEnsFPAwLeL4wkebDogBICd/qKHpRpcbDS p1ig== X-Forwarded-Encrypted: i=1; AKwUvBwq/Xr+l5OSkkoo8zolHSim+LgJHPpw97nYYBahu5JgqMKtE/zXck8QeJ+75Scfm1zUcov/7TSKx5/YAAQ=@vger.kernel.org X-Gm-Message-State: AFq9FYKuu3P9iXQ6rIfFfLslrII4wKaYINSSHU+Rp9CIoYXIF/HcrNo8 DipjuhLmgexDBR648uiTEi/1+5dfwlKo4dEBSfdq1RY1Vi8I+RMpE490wpuz70PvPnBa/3cBhmb CNRdnySvksA== X-Received: from dleb17-n2.prod.google.com ([2002:a05:701b:4251:20b0:15e:225e:ae03]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4559:10b0:15b:7f7a:730d with SMTP id a92af1059eb24-195c77a8177mr315037c88.28.1792700148205; Thu, 22 Oct 2026 13:15:48 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:48 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-16-irogers@google.com> Subject: [PATCH v8 15/19] perf intel-tpebs: Fix race in tpebs_stop() and tpebs_cmd.out fd handling From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In tpebs_stop(), read tpebs_reader_thread into a local variable before releasing tpebs_mtx to call pthread_join(). Transfer ownership of tpebs_cmd.out to __sample_reader() by passing it as the thread argument and setting tpebs_cmd.out =3D -1 in evsel__tpebs_open() so tpebs_stop() does not double-close the fd closed by perf_session__delete(). Also reset tpebs_cmd.pid =3D 0 and tpebs_cmd.out= =3D -1 and return early if start_command() fails in evsel__tpebs_start_perf_record(), as start_command() closes the pipe internally and may leave cmd->pid =3D -1 on error, and join tpebs_reader_thread on error in evsel__tpebs_open() if it was already started. Include , , , , , and explicitly. Signed-off-by: Ian Rogers --- tools/perf/util/intel-tpebs.c | 51 ++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c index 7c0b8b15bd10..47c939fc2058 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 @@ -120,10 +126,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) @@ -220,13 +231,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 +247,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 @@ -331,12 +344,19 @@ 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; + pthread_t reader_thread; =20 tpebs_stopping =3D true; tpebs_send_record_cmd(EVLIST_CTL_CMD_STOP_TAG); + /* + * tpebs_results is empty when stopping; clear pid so + * process_sample_event() ignores any late samples while + * draining the pipe. + */ 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]); @@ -515,6 +535,7 @@ int evsel__tpebs_open(struct evsel *evsel) int ret; bool tpebs_empty; bool started_process =3D false; + bool started_thread =3D false; =20 /* We should only run tpebs_start when tpebs_recording is enabled. */ if (!tpebs_recording) @@ -567,12 +588,15 @@ 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)) { + (void *)(intptr_t)tpebs_cmd.out)) { kill(tpebs_cmd.pid, SIGTERM); 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; ret =3D tpebs_send_record_cmd(EVLIST_CTL_CMD_ENABLE_TAG); } out: @@ -586,9 +610,22 @@ int evsel__tpebs_open(struct evsel *evsel) =20 if (started_process) { if (tpebs_cmd.pid > 0) { - kill(tpebs_cmd.pid, SIGTERM); + pid_t actual_pid =3D tpebs_cmd.pid; + + tpebs_stopping =3D true; + kill(actual_pid, SIGTERM); + 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()); + } + tpebs_cmd.pid =3D actual_pid; finish_command(&tpebs_cmd); tpebs_cmd.pid =3D 0; + tpebs_stopping =3D false; } if (tpebs_cmd.out >=3D 0) { close(tpebs_cmd.out); --=20 2.56.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700152; b=3DxVdn14WYeNw5LpidNd0nRHCSMQt3GLAaXCE4BI+NFhnCSekA2GHlNUjVWW6kIGeBX7 ryXDFlVg19E1w1TQE7FR38w8G36Ji3Ctbv/tIh6EPoO6ZL86/bXcTNHzwl87Zr8kVegvH 49oR3Bv0NN53ZmhT5JVmMgGusYHMAFmLK6r3xtyMWFfKfHQ4kktLnbuks8KYdW6LhkUvH GxAjui6fLxTx3sK69Mgu4jcKnp4BAS0hRVxHKCm3sINfAW8CsPMofJsW7J5pgvzhdtk5S qv0ny/NI2IAVVBoGfvhlApS+Hn6KAi1Pw1dsrsTRTajV/UlILaDcVgbrTRWYM2/5AbQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700152; 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=+PvHupV3LdkjeIjfKKzL+b+1QpW53jwZ/f1wz3405lQ=; b=4W5JnBwtPVpfIAz7IKrGNpD+y6jeqGe0eVpz8fhE41vbnDc3hqWJJMeTaL2P9AFgnUMu BWxS/XY1IgNr2MRWlBHDZnU0CoM9lY2/+qzSA/wpk7xisCY3HnruTiB+SxCv7BCaUA9/f einniSnrghVJImEzqfCn5KWK7Fg2Izz5Q5LDJ0LAUYTOb2fDXCoubELY6SH2RYgroClmU 9dBkrjxPaXGWXCXueVyijB5FJfIkkzLgJWKPvbIUSy3b0IR9lqu1LQwUIUQkXN2zN1Q4N KjEsnb7BXPsvReJ171os5VRZigeUgQJT3MrRSZireDqQsO2iV7Gf9QZDA0zCVBtVzkw== 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=20251104 header.b=IT1ZFevN; 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 C561860A5BC2 for ; Thu, 22 Oct 2026 20:15:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C561860A5BC2 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=IT1ZFevN Received: by mail-dy1-f199.google.com with SMTP id 5a478bee46e88-3698bd5392cso112734eec.1 for ; Thu, 22 Oct 2026 13:15:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700151; x=1793304951; 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=+PvHupV3LdkjeIjfKKzL+b+1QpW53jwZ/f1wz3405lQ=; b=IT1ZFevN5aCyRr+5OVW2r0TwYDoqpvE9JGPZ4YaB4ejsemzg4VobYGf/qVE918zOPy nvjD0fiU40SM/fCV48lyplJyq92Oxb+X3PDv8qhyJn8FTDaGzYk0+TONl7DP2eBfompL ENqmCBJHYYn7WN7bCkx7LMSgK6E6TMBS8nnkT0F4uNW6jLuwoGIo4H2Ke8Oc9FPUjHSG zj7KK2EQOBxlvK6O6wKvFghv6bL/qWWy56cADszmoW/o52i1tdMQCpzsVHVpbzvSf31t JTMsmir5RrtpziyZlb9ZzQq0+9LsiQJmt5R3tAgsPrcUa5Yhomxq6Hd4xTg+Jm2niGsh ZGVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700151; x=1793304951; 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=+PvHupV3LdkjeIjfKKzL+b+1QpW53jwZ/f1wz3405lQ=; b=2EEtQqtRD8Q8kYELyTouyrDRwpUTZmvwxodRXtefJcPi1aq77mfbMRW3msSeueXPvQ 0rDy+QTPh2vqyEgrYqyKXMSDHsf5EMIEjcfQQwW8+iS23zQ+bsEV3xSurDaboy6Qe7Td KpA3j+cYUHAllFj6U8yU+DZDsUotoceaipQecuorgHCH+h1CMXvfv85JNnG2zuibIuSL dY9gLQViF1PjDzeqfUp711aJ5tmpuZYOTT9s4Nc43VKsMoza+c1Ltv+CkAAfXy57XY0v RHBmhP3jGlvboYxWWPzW1sHPxHNDVz0Hm/swn7/FG3jXRC3pVie4/UZPjQUj+eSltVnE bNxg== X-Forwarded-Encrypted: i=1; AKwUvBw/+GzQs5cZE03cJdvCa3eOmtxmrcOvbqHdcx2/Kd/9MzdVkuXV8ybhyfRnp1UgeaNf5IV9fD5FlJSzGGo=@vger.kernel.org X-Gm-Message-State: AFq9FYIX4borqMtxWOioVRN1eLxP4KwkSjy+0SXdEPjUyKf1U+eh2Llh md13CnOcUGdJXhjBbqxxA6VzQzsa5aRRCSG+EMni4vBzhj5glSicNr/LkOu8Wf5Kda5QHCtrxx+ H/DCmqZSafA== X-Received: from dlboz1.prod.google.com ([2002:a05:7022:1301:b0:164:47f8:a96a]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7022:5f0d:b0:15c:a04c:cd4f with SMTP id a92af1059eb24-195904b1191mr464491c88.0.1792700150451; Thu, 22 Oct 2026 13:15:50 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:49 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-17-irogers@google.com> Subject: [PATCH v8 16/19] perf intel-tpebs: Use DEFINE_MUTEX for tpebs_mtx From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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. Signed-off-by: Ian Rogers --- tools/perf/util/intel-tpebs.c | 79 ++++++++++++++--------------------- 1 file changed, 32 insertions(+), 47 deletions(-) diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c index 47c939fc2058..1812917bfb85 100644 --- a/tools/perf/util/intel-tpebs.c +++ b/tools/perf/util/intel-tpebs.c @@ -38,11 +38,11 @@ =20 bool tpebs_recording; enum tpebs_mode tpebs_mode; +static DEFINE_MUTEX(tpebs_mtx); static LIST_HEAD(tpebs_results); static pthread_t tpebs_reader_thread; static struct child_process tpebs_cmd; -static int control_fd[2], ack_fd[2]; -static struct mutex tpebs_mtx; +static int control_fd[2] =3D { -1, -1 }, ack_fd[2] =3D { -1, -1 }; static bool tpebs_stopping; =20 struct tpebs_retire_lat { @@ -59,23 +59,8 @@ 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) { @@ -205,19 +190,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_cmd.pid =3D=3D 0) { /* Record 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; } /* @@ -227,7 +212,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 @@ -257,7 +242,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, }; int ret, len, retries =3D 0; @@ -271,7 +256,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 strlen(msg); @@ -327,14 +312,14 @@ static int tpebs_send_record_cmd(const char *msg) __m= ust_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); return ret; } =20 /* * 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 @@ -355,9 +340,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; @@ -493,20 +478,20 @@ static int evsel__tpebs_prepare(struct evsel *evsel) struct evsel *pos; struct tpebs_retire_lat *tpebs_event; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); 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()); + mutex_unlock(&tpebs_mtx); return 0; } tpebs_event =3D tpebs_retire_lat__new(evsel); if (!tpebs_event) { - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return -ENOMEM; } list_add_tail(&tpebs_event->nd, &tpebs_results); - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); =20 /* * Eagerly prepare all other evsels on the list to try to ensure that by @@ -541,9 +526,9 @@ int evsel__tpebs_open(struct evsel *evsel) if (!tpebs_recording) return 0; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); if (tpebs_stopping) { - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return -EBUSY; } /* Only start the events once. */ @@ -553,17 +538,17 @@ int evsel__tpebs_open(struct evsel *evsel) =20 t =3D tpebs_retire_lat__find(evsel); valid =3D t && t->started; - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); /* May fail as the event wasn't started. */ return valid ? 0 : -EBUSY; } - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); =20 ret =3D evsel__tpebs_prepare(evsel); if (ret) return ret; =20 - mutex_lock(tpebs_mtx_get()); + mutex_lock(&tpebs_mtx); if (tpebs_stopping || tpebs_cmd.pid !=3D 0) { ret =3D -EBUSY; goto out; @@ -618,9 +603,9 @@ int evsel__tpebs_open(struct evsel *evsel) 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); } tpebs_cmd.pid =3D actual_pid; finish_command(&tpebs_cmd); @@ -649,7 +634,7 @@ int evsel__tpebs_open(struct evsel *evsel) } } } - mutex_unlock(tpebs_mtx_get()); + mutex_unlock(&tpebs_mtx); return ret; } =20 @@ -669,7 +654,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 @@ -678,10 +663,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); } if (t =3D=3D NULL || t->stats.n =3D=3D 0) { /* No sample data, use default. */ @@ -720,7 +705,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; @@ -745,7 +730,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); @@ -754,5 +739,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.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700155; b=ZOymgbLBt1dncBh3sDw7iX8XLQT5A1b744yp1rqZ/V9CEeifJpf2Klo8GdWWbi6BNc2P rHRgV87lMswZqqs4ABguGc1hPJ+Q1x1xw8dnDkPIFp3jwoqw10FNKcXk07PxCTAZHGMV4 hCoWzsmSFsrS5TqYZTVWVaSK0LWEZUDFIXzHRu7n86zr9p3SZAZZ6BuaEz/mGV+HXsTtj i9oAk85f6o7wmXtHqVipOz3XSvvUExOxOlPix2+AQZe1pfbWmi44u5bG2Mka+vFXBQL9g FkNxajcJDGGZUx5rBWMFlOACQ/owsm6sdGFPj+j45oYaPSs/Kyk3KHCO3nBvpIibveQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700155; 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=7iAsvt2YDLoKZFcnRRd7GHgxGV69eRWRoafZBMhdoy0=; b=pMQ2Xr1ER+eSYfo9erDvM+ZZqK2nktVYYoVwQiBU76sQcXr95AcGi2swmTxSiA2P2rvd QnPa6eu82d5PWyg1X3wQv6NbScVX0sPQ9bHCqzXQfeMg0q0sMJCuYszM4/8hO5fOj+Xz8 5OlYYNvbT1mUSa3ZrbdOqIz760cO+Xv4jTAgA5wUOPDb00qYJNX/HA6T52m3P921mAqhL C9Ju10zyaDLa6l+tZUs6QXt+85+p9uAA8Da7KpM7RDayq1UiDmRGxQChKNyOch1vBTVKp vmQElUcSUZBXXJLKtP7KmcHFb09xTIUonKC27vfw56KUlnhNyVOPRNEkF7npdwLQ3LQ== 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=20251104 header.b=eT1O4bxM; 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 DD4B760A5BD4 for ; Thu, 22 Oct 2026 20:15:54 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DD4B760A5BD4 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=eT1O4bxM Received: by mail-dy1-f198.google.com with SMTP id 5a478bee46e88-35c3c370b17so307604eec.0 for ; Thu, 22 Oct 2026 13:15:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700154; x=1793304954; 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=7iAsvt2YDLoKZFcnRRd7GHgxGV69eRWRoafZBMhdoy0=; b=eT1O4bxM8RGd3Z556MQv3fyLrRYr4yy3R/yhtapGF1beIOC6ei2BxFy3qXZUNoDNHi oDkf6DOXr2h7uggDCuc22weIKExPBP90uSuYqSxWRmemWXb5zYKpe1m5EMr6nIQnk5Pu vBvkq7LDrDDwnazPsncebcxs5CofXFPv1K8UPjmkpj4WQB8u7vfuJ0ZLZoH4crDxn4QM cMU7x1r0ZTEIcAI70Soj1dZJce+pzk52YWIx5+CGG3YSlDuZaFtMnYFzhC3xH0MRzW0J 2MjNYKpKwF23H4zDi5ReJM9sOxrmwwjG7BwsSplHjqeVcqN2gxYyz7Id9FL64Fp1WBiY wI2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700154; x=1793304954; 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=7iAsvt2YDLoKZFcnRRd7GHgxGV69eRWRoafZBMhdoy0=; b=D6aBUnmTW4HD2lJiGW/9tL6b5OyDNgzfeU5mu0lAujJz3zsiccn+8sbMpJ+bttzdZo 3OQx4CxJNrTuutuCRAE2rkeoWPI3WrQCpa23ublwCeN/RfvwO/sSh0+sq9RXpdXlzt+P FfdOfOrsorvtcqlnhtG74v2k7YIOsazBZEpZQ0La7KHwzGSmWK2N3hw7bI1WMX5+gUWO 59jZ26x9SLJkDSE9db8gHxqReB3h5Mo2tJmnPRpbJxyC4p0PiolYsQKD8NWcEVylVfQX XCnZupm1AAkckF71M/9O5KtRLMLRn3levIUd9j5jB/yUD35n37OMGHLRIeRsG+l8yOj3 nQvg== X-Forwarded-Encrypted: i=1; AKwUvBxZW1eBenizRqPOJeRpbe5nt3nRB/rrie9wl4JUxsHCdD75/lyCxQJCifhHvFxwQuqHkOi00cTea9877Mw=@vger.kernel.org X-Gm-Message-State: AFq9FYJhyvsHXDmLXQHwUfLrZNZhFgTU8n0mklJBBcxXTrfl1VebeNdk +9QiZ3sYUpPwkeOH9iyv1QTn9iSrHT8u7E6m8QIkzKCjUmCt6boXWYE3E4KuwMmM+U5n7wewW8R wbOJOntAZoA== X-Received: from dlam18-n1.prod.google.com ([2002:a05:701b:2092:10b0:15d:bfd9:370b]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:270f:b0:151:70a0:6bfd with SMTP id a92af1059eb24-195c4f5344dmr337890c88.24.1792700153207; Thu, 22 Oct 2026 13:15:53 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:50 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-18-irogers@google.com> Subject: [PATCH v8 17/19] perf config, intel-tpebs, env: Add __guarded_by and __must_hold annotations From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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, TPEBS state in util/intel-tpebs.c, and bpf_progs and cgroups trees in struct perf_env. Signed-off-by: Ian Rogers --- tools/perf/util/bpf-event.h | 5 ++-- tools/perf/util/config.c | 47 +++++++++++++++++++++-------------- tools/perf/util/env.c | 1 + tools/perf/util/env.h | 19 ++++++++------ tools/perf/util/intel-tpebs.c | 7 +++--- 5 files changed, 48 insertions(+), 31 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/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; 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); diff --git a/tools/perf/util/intel-tpebs.c b/tools/perf/util/intel-tpebs.c index 1812917bfb85..fb3127aa83ce 100644 --- a/tools/perf/util/intel-tpebs.c +++ b/tools/perf/util/intel-tpebs.c @@ -39,11 +39,11 @@ bool tpebs_recording; enum tpebs_mode tpebs_mode; static DEFINE_MUTEX(tpebs_mtx); -static LIST_HEAD(tpebs_results); -static pthread_t tpebs_reader_thread; +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; static int control_fd[2] =3D { -1, -1 }, ack_fd[2] =3D { -1, -1 }; -static bool tpebs_stopping; +static bool tpebs_stopping __guarded_by(&tpebs_mtx); =20 struct tpebs_retire_lat { struct list_head nd; @@ -63,6 +63,7 @@ static struct tpebs_retire_lat *tpebs_retire_lat__find(st= ruct evsel *evsel) __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; --=20 2.56.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700156; b=qguDX+BtEe93UedxgAsWc8pzg8WLCKt7wmK0NBkSOfB7CCGdHW6wfGDU4oRzOAvFNOXu f7SrgKpoNah4uZ1omAefwKov21EEqgBwSEolRyQF/eHBH5M76b0T5OmK31SZZB5Vm5o5s zYoC839HcdCYqY3KR8iAOfN5SpCs5yTZghIXefTLBSMKhklylp3fgsgitE+oV0wrzFngy VWaUGth0zlIJwo4FiNMO1AxnhXK4dfSk3OP4S7kpDWaZ+MpjrUC8QVdKwRBjC004LptAI +uVEPnoGplM7YNzgQxyWvkqGCuJwRUnbQ2BX+Vy2uG77jmBJcWAA1PlBfdSzTF0jVRQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700156; 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=m93d6ML738qvEiSSbh/02KAktLjyqMoz7nlZaRp5/fY=; b=5fE17MSGPfzLfMH04k/ox4FHe1zURO85j5/NVUZZxFFjw1mmtwJB7C4egFUGJulvQ2EL RY5pPI//xJjNKzUSviRzkMe2GH/cv+Q6njCpsmdhITs10O7SI0agtuClNbEfYxMlEG7vS 8cX82iLUqU9HBCFKR1a4xOo7QIdM0OXBKLK+gxxWnvAocZEs8eQRDih3QE+L8xDjhTsai 1dJCeBXSzNjOgN1HulbZitsToveaSESEv2ZJa53nsNkVybvY29hlZLMvF1LFNWZRsydKR KSop1ije/fj3cTRq4Y7qM8/RoY/lUCZAfLU+Kr8knjd5cWOwSf89Oi2OP8fFh9r3XrQ== 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=20251104 header.b=rbmiBBUJ; 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 25FE860A5BE2 for ; Thu, 22 Oct 2026 20:15:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 25FE860A5BE2 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=rbmiBBUJ Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-151ba226e1bso312970c88.0 for ; Thu, 22 Oct 2026 13:15:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700155; x=1793304955; 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=m93d6ML738qvEiSSbh/02KAktLjyqMoz7nlZaRp5/fY=; b=rbmiBBUJR3SL05JWWWJ162WeHaaOeyyTmao+2aSIDjlRjFJwHiyxM0THNMbFLcFm86 V40kiWmGI7QT1D31uZwltdXpFSTbzez84I1HxoHd+v3CBFaxoY0xexynotDkxIvzyan1 be2rnr9Uz53YEqJlWP5JSLoCtUrRbLxbblQY1haGjP8bzIYGHxBWzic1PkiMCRkaIeOP ypeBpMhspuocK+/OmH7AHHyzqtyGSouXsCBK+NX3MNtSlnD5cuc6UDpnKBNiK7o4GcjM Of2cDYJfMeQ73PFG3ztw678WsYNg1TQe+lLTuhTIG8NeSnDQEW+EOFOwI8vA17e7p32z p8/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700155; x=1793304955; 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=m93d6ML738qvEiSSbh/02KAktLjyqMoz7nlZaRp5/fY=; b=JIlZJu+mhhlw5VzCthLj8lRalJ4WP9BDpxRTqQy/8ooFhPh0ZpNufjUB5rV8iClqhd j8E1WhXB/uBHLFKEEP/F5icjgZTXRDPm/R/ZdkS2zQ1q9ZGg+CQNbzequw51x++ob8mz FXHE1WQ+tVOzOlMkeAcBhCWPLsRkO6nwy9167PXIL/HM6WcbasLay2cSp5wtLamQkhmY NphsnEBRHAw4insuhFrnoNk0V7FeZEdnD+OB8NdCzjpSFR92uan+6CR5MOgR8Alpx5A9 tVPJhOdvPzM1Fk3FKtWkDFYwTbZe5V9oE+OGHSJBXeebZEArQCW1Cq3PcTzt8m1jV1oA C+MA== X-Forwarded-Encrypted: i=1; AKwUvBys8zNj/VNyHgDFL/CUmHV1xQdTlEjxJZi0AA9f9yuAOAoWTSHB8cY3cuq97xxROSusBz2THvBk7fGIoek=@vger.kernel.org X-Gm-Message-State: AFq9FYIYtp8d2SqZAikpciJUqqCdjx4CU/g3u3D+YL/Sc15GTUciG+Mw NwrY2Mu/ebRxdj4qlZmzhGqykciTtky6Hr4VenvRnWvp1/XDplDSNuP9RApWPjSXY9BNgn4MACH 0/e5JLNtFvg== X-Received: from dleb10-n2.prod.google.com ([2002:a05:701b:424a:20b0:18a:a95d:606c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4186:10b0:194:23e7:b526 with SMTP id a92af1059eb24-195c77a8247mr373925c88.27.1792700155131; Thu, 22 Oct 2026 13:15:55 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:51 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-19-irogers@google.com> Subject: [PATCH v8 18/19] perf maps: Fix lock inversion in maps__load_first() and bounds check in __maps__remove() From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In maps__load_first(), take a reference to the first map and drop maps__lock(maps) before calling map__load() to avoid a read-to-write self-deadlock when loading kernel maps inserts extra trampoline maps. In __maps__remove(), check that the index returned by maps__by_address_index() and maps__by_name_index() is within nr_maps before indexing and removing the entry, and remove from maps_by_name before calling map__put(maps_by_address[address_idx]) so that callers such as maps__remove_maps() that pass maps_by_address[i] as map do not use a freed handle in maps__by_name_index() under REFCNT_CHECKING. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index f808df2fe77b..e947506c60a5 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" @@ -548,20 +549,27 @@ static void __maps__remove(struct maps *maps, 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 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(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)); + --RC_CHK_ACCESS(maps)->nr_maps; } =20 @@ -1459,10 +1467,15 @@ int maps__merge_in(struct maps *kmaps, struct map *= new_map) =20 void maps__load_first(struct maps *maps) { - down_read(maps__lock(maps)); + struct map *first =3D NULL; =20 + down_read(maps__lock(maps)); if (maps__nr_maps(maps) > 0) - map__load(maps__maps_by_address(maps)[0]); - + first =3D map__get(maps__maps_by_address(maps)[0]); up_read(maps__lock(maps)); + + if (first) { + map__load(first); + map__put(first); + } } --=20 2.56.0.307.g5fe7bd680c-goog From nobody Sat Oct 24 14:02:53 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=1792700159; b=LlcxC0laGJOK3vZoTX8DlUy42l24Prgzr6uq/HXZ9Cywq7gez0eCQLsBN2Zba/T9E4S6 DI5jzg4Di36d7v0vfxmO83U+OswN2AXj41+0pYNeeXHq5EcSXf8dCYxJR+1DWzbuvREqC hOra2kUHXXMsefcbMxk0k2a1ql20YJhN+XVingoDPS2mac/nWeZ1bfwwNTSwP1mPcS/39 r+PXntUlfJKzUvfcqC2s4qig47Ljf/nZyLeWvQECjRvX+1fpwFsr7Qa7L+K9DLn8k0ELT AUCN4LTl/t7XDomSm13oaW66oTU6dr56xX9Eaub901ACwkAhYlv3OS2pO0w+8W8aiJA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700159; 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=NJ3nHojO2Toy+WaFLnXPk1ThxDxousdrv3IeZrRvEL4=; b=p8/maoq38oi+jTTbUzukkzcVe6moRNP4Sz++W43g2eKYVh1ei89txuYvMAVB6xwtb5zR ZSgaQKcRVtAiS//qpRtkxw3+PBGw/paunvIpzMRpocD5EYKW4TDtZ98YlHlPLKmB8gpvV IcxulwWU8+SfDynFV0+aYKCU7pVK+HXmbmwVchOI/axtM2KlqzpHvTvXPGT2Y99Xkf3/s Ji0Ykb0VNLuOWaMRNi3zUR+qgm6yEG1wTeAKHTsPf5qUEFSu0/EZuCqg9stVryka5gH2j WMeVwj7C8nKxckcUXGDiVh2McR+wyI15CywZxE/dD3pjPJzZrGNwLOr4GjPhCJz/5Tg== 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=20251104 header.b=ifu5C3XV; 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 8848C60A6F53 for ; Thu, 22 Oct 2026 20:15:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8848C60A6F53 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=ifu5C3XV Received: by mail-dy1-f197.google.com with SMTP id 5a478bee46e88-36998507c13so230318eec.0 for ; Thu, 22 Oct 2026 13:15:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700157; x=1793304957; 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=NJ3nHojO2Toy+WaFLnXPk1ThxDxousdrv3IeZrRvEL4=; b=ifu5C3XVIyvNFUo5P21U+ehpuNWjXMAFGeqw+rdbmluB/TBFq8HEkQXYQ2q9Ghm48o OkWY0tPCU0VEQ5Pfxfz6UP9NbRKD8Uc03IQN2xVNdMuYPcgNRYv+ndL79S1rDvFNWb3v V5La+QWeez4Do24zSGbXr3yFet0+FCVX38QfHnjFVlCWHlJ/ie2cx4+zGGvwICF/MHkJ QKjxJyhgf7Ma7Fi5KkIh5tUrTsU8VlTp4RxTyKNDcSgibDxsY+bALg/4XWfvT40hTGjz VzbpDVd+TukGB8qNf6tolKalMc714m1LtA+JAvTtpxUQwk/qeEDhH8do/dT1vu5l0HWm P/zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700157; x=1793304957; 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=NJ3nHojO2Toy+WaFLnXPk1ThxDxousdrv3IeZrRvEL4=; b=nNzNUuY+3GlxiMjG0zDtbERU2Ho9T3m6nHWzG/fdHDLj1JG5jcA1h0B7WSuIJacUem Pc8ZMmhrr1nwIh7xLuHVxC9sW1ZAuX2//kBzaozP2QwEUiJxUDtUBMcugIVfNwlkqSBO EnZBHc8v2HPeaycgrs9r10CllmDO6fxWjQrW1ZO9UKUSyUbvwl2a2ctNFFFMM/p/Cqrg Od+qcP3Lbk8zjOCOAitfxkJLcS6eAAGQYGzidkNf2Wus7IauehWcV0f31ojPGFNxxP9T 1IMGQ7vNSZOwMoyD+tjM+q1h4tRiHmT11OoQKLNBBaw1ZhyiZpkFQtxuuZpc42Lr8Rx3 Ecww== X-Forwarded-Encrypted: i=1; AKwUvBy3c/WtZ3Clg6meA3XfO24zhvdu6Xs0cFfyS5zOWTUEelkWbQU69PcQT5ZI3Ur1hk0UuN4cVXTfyRMX43s=@vger.kernel.org X-Gm-Message-State: AFq9FYJ6tltLMUU4Lk25bbon4jgA6ctqHv4TtwUKz8y6DWkbfvOhoV17 S2/l3fhQCmSR8xvwQfMk3VlcwEmHUvtWvqcdqApBm8Fsf0syIBGQjeGLt6iu4dRQ0kom/48bX+7 wFALj2qhTig== X-Received: from dybmt9.prod.google.com ([2002:a05:7300:d589:b0:350:9a68:218d]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:693c:2181:b0:353:7b46:4819 with SMTP id 5a478bee46e88-369d97a9d9dmr242304eec.8.1792700157004; Thu, 22 Oct 2026 13:15:57 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:52 -0700 In-Reply-To: <20261022201453.1717523-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: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-20-irogers@google.com> Subject: [PATCH v8 19/19] perf maps: Add context analysis annotations From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org 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 --- tools/perf/util/maps.c | 122 ++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 44 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index e947506c60a5..1c9e8e22793d 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,18 +437,19 @@ 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); =20 if (maps__maps_by_address_sorted(maps)) { struct map **mapp =3D - bsearch(&map, maps__maps_by_address(maps), maps__nr_maps(maps), + bsearch(&map, maps__maps_by_address(maps), __maps__nr_maps(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++) { + 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; } @@ -431,18 +459,19 @@ static unsigned int maps__by_address_index(const stru= ct 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); =20 if (maps__maps_by_name_sorted(maps)) { struct map **mapp =3D - bsearch(&map, maps_by_name, maps__nr_maps(maps), + bsearch(&map, maps_by_name, __maps__nr_maps(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++) { + 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; } @@ -471,10 +500,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) { @@ -541,10 +571,11 @@ int maps__insert(struct maps *maps, struct map *map) } =20 static void __maps__remove(struct maps *maps, struct map *map) + __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 address_idx; =20 /* Slide later mappings over the one to remove */ @@ -631,7 +662,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; @@ -659,7 +690,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 @@ -686,7 +717,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; @@ -719,7 +750,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(maps, maps_by_address[i]); removed =3D true; @@ -773,7 +804,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++) { @@ -858,9 +889,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)) @@ -885,10 +917,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 @@ -945,6 +978,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... @@ -956,7 +990,7 @@ static int __maps__fixup_overlap_and_insert(struct maps= *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]; @@ -1067,7 +1101,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]; @@ -1151,8 +1185,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 @@ -1236,7 +1270,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), @@ -1272,7 +1306,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) { @@ -1284,7 +1318,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) { @@ -1307,7 +1341,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); @@ -1337,7 +1371,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); - if (++i < maps__nr_maps(maps)) + if (++i < __maps__nr_maps(maps)) result =3D map__get(maps__maps_by_address(maps)[i]); =20 up_read(maps__lock(maps)); @@ -1354,7 +1388,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]; @@ -1401,7 +1435,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)); @@ -1416,7 +1450,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= )) { @@ -1470,7 +1504,7 @@ void maps__load_first(struct maps *maps) struct map *first =3D NULL; =20 down_read(maps__lock(maps)); - if (maps__nr_maps(maps) > 0) + if (__maps__nr_maps(maps) > 0) first =3D map__get(maps__maps_by_address(maps)[0]); up_read(maps__lock(maps)); =20 --=20 2.56.0.307.g5fe7bd680c-goog