From nobody Mon Apr 6 10:31:53 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 121F03264D5 for ; Thu, 19 Mar 2026 13:52:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773928380; cv=none; b=d0a4brwKVTqvBPlrVahU3BwYMOwaEM0GeD9Xz4ff5mBZ7MjD11rHMu//DH/CCcz75pk0RIINgke+J8feFdqbqMB2qYia8lB1O5FSDs8P4uKbwGSdfxfotf3w0fE7b8m09JzN9F7qag2eDyNabHAIDwgBrOhLiffuM1XrXUtH9ns= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773928380; c=relaxed/simple; bh=N3JUv4FAKrJiGsjb/JjZ5eEIy7p5lTbcpeg4XLkvWbI=; h=Date:Mime-Version:Message-ID:Subject:From:To:Cc:Content-Type; b=IgBLOIksRUWje//lwnYWQncFCy8SAFauogqAbQ/+Ry00uW4c5HsRKlOlvFHAdhfr9bFMZrfOuETKI0ba7IX4hTPdwKwl2IjNtzc3kE1LXV+IBiKh51Jm7/x4s4Sq/+SuzuZVZdl1n+2DUEzMBAz6gWzKWoSIVdz+81v3bn/Rn9s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--elver.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ni1dhiab; arc=none smtp.client-ip=209.85.128.73 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--elver.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ni1dhiab" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4852cf0318dso8990775e9.3 for ; Thu, 19 Mar 2026 06:52:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1773928377; x=1774533177; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=eZ48RxtqhFC+Ft2ryzXO0wxvBOUpn0Fzs00xvrlNRUI=; b=ni1dhiabqfJ+dWBC7ua1nCCRSQrTHTGfmzSJvMXgzs67f8xgLN2msrPkEN3EkS/D3p 6x7CaFwLDyCcg9NVhMvyIxqiJfLXQpVHFP2dOK4Sa+pBu86FECCKSke/ZYTj1Kj5F9L5 uaBUO7ZO60874mKNteu5m3gwz3mmbvhXEPK1sWD8JoWSR1VdAJbsRviXCPskmWNU7LXy qkKY046sMCK+ynvGbZDhsa9fTkGGqZG9ZonY7h6YvZ6M1fM0FkGSLPV2q/7WJvJe1whp nYHcOccgcX4Xta7Rdw/NcHEvwShTWHYjDNpkg/CFJ6XkfBtaSJs+NfdlKKYIRS+tB3aP 1znQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1773928377; x=1774533177; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=eZ48RxtqhFC+Ft2ryzXO0wxvBOUpn0Fzs00xvrlNRUI=; b=kS4sojesxP/vsWEFCQtPcszIE6GwC+pj4z6q/0h+VPQgdONtib8rrjtUlb7QhRk8n+ anm+g9Z1Obwb7Udw1M7H4xfbWq9Ln+7kdX4T7r2dFv8VqL5NRNBpn1Iifh90BOrVXLNk hsdVqqnGJzxPlUF8qRqdp2eMWQcFbXlB7rGjPUXg9po+DQn5buqs+WMk0zCVqEC/hCJY Rp5JiS1kbVC7K3jFYlaHwHH8ZHF9DTdTtpxR1sYyeGDa8xCbrcKHw/+CbVHrEZI1Bmnd P6qvI9Lpvf8TF4yjTyEv20QPSHI1k6GzozpfXP8t/hD5cY0OcOB/6OFsYkUtTPZOqlBa 3byQ== X-Forwarded-Encrypted: i=1; AJvYcCUCN3KzrCsyotPLMUmPIsd4wAmWZfcF/2x6jl0JbFcQCwAMcFoqsRCCxQF1vid6aX/bs+alauuhR9a/1Po=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2SSZIY6EZE01D6Xv4XSO6WlHDGsxfzDRJwP/ahJ5Qjgr4OYPN +xjfOpTH9r7XIMawI/rb7ZG3QdXBeOW3PgR/3QqOd9ZSSpVzaYn4z1ev8zxnkPdlJ3XIONa2Pvn w1g== X-Received: from wmol21.prod.google.com ([2002:a05:600c:47d5:b0:485:33e8:9296]) (user=elver job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:b8a:b0:486:f308:94ec with SMTP id 5b1f17b1804b1-486f456fdeemr126886675e9.24.1773928377192; Thu, 19 Mar 2026 06:52:57 -0700 (PDT) Date: Thu, 19 Mar 2026 14:52:38 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 X-Mailer: git-send-email 2.53.0.851.ga537e3e6e9-goog Message-ID: <20260319135245.1420780-1-elver@google.com> Subject: [PATCH] compiler: Simplify generic RELOC_HIDE() From: Marco Elver To: elver@google.com, Peter Zijlstra Cc: Ingo Molnar , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , llvm@lists.linux.dev, Bart Van Assche , David Laight , linux-kernel@vger.kernel.org, Sean Christopherson Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When enabling Context Analysis (CONTEXT_ANALYSIS :=3D y) in arch/x86/kvm code, Clang's Thread Safety Analysis failed to recognize that identical per_cpu() accesses refer to the same lock: | CC [M] arch/x86/kvm/vmx/posted_intr.o | arch/x86/kvm/vmx/posted_intr.c:186:2: error: releasing raw_spinlock '__pt= r + __per_cpu_offset[vcpu->cpu]' that was not held [-Werror,-Wthread-safety= -analysis] | 186 | raw_spin_unlock(&per_cpu(wakeup_vcpus_on_cpu_lock, vcpu->= cpu)); | | ^ | ./include/linux/spinlock.h:276:32: note: expanded from macro 'raw_spin_un= lock' | 276 | #define raw_spin_unlock(lock) _raw_spin_unlock(lock) | | ^ | arch/x86/kvm/vmx/posted_intr.c:207:1: error: raw_spinlock '__ptr + __per_= cpu_offset[vcpu->cpu]' is still held at the end of function [-Werror,-Wthre= ad-safety-analysis] | 207 | } | | ^ | arch/x86/kvm/vmx/posted_intr.c:182:2: note: raw_spinlock acquired here | 182 | raw_spin_lock_nested(&per_cpu(wakeup_vcpus_on_cpu_lock, v= cpu->cpu), | | ^ | ./include/linux/spinlock.h:235:2: note: expanded from macro 'raw_spin_loc= k_nested' | 235 | _raw_spin_lock(((void)(subclass), (lock))) | | ^ | 2 errors generated. This occurred because the default RELOC_HIDE() implementation (used by the per-CPU macros) is a statement expression containing an intermediate 'unsigned long' variable (this version appears to predate Git history). While the analysis strips away inner casts when resolving pointer aliases, it stops when encountering intermediate non-pointer variables (this is Thread Safety Analysis specific and irrelevant for codegen). This prevents the analysis from concluding that the pointers passed to e.g. raw_spin_lock() and raw_spin_unlock() were identical when per-CPU accessors are used. Simplify RELOC_HIDE() to a single expression. This preserves the intent of obfuscating UB-introducing out-of-bounds pointer calculations from the compiler via the 'unsigned long' cast, but allows the alias analysis to successfully resolve the pointers. Using a recent Clang version, I observe that generated code remains the same for vmlinux; the intermediate variable was already being optimized away (for any respectable modern compiler, not doing so would be an optimizer bug). Note that GCC provides its own version of RELOC_HIDE(), so this change only affects Clang builds. Add a test case to lib/test_context-analysis.c to catch any regressions. Link: https://lore.kernel.org/all/e3946223-4543-4a76-a328-9c6865e95192@acm.= org/ Reported-by: Bart Van Assche Reported-by: Sean Christopherson Signed-off-by: Marco Elver Reviewed-by: Nathan Chancellor --- include/linux/compiler.h | 5 +---- lib/test_context-analysis.c | 11 +++++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/linux/compiler.h b/include/linux/compiler.h index af16624b29fd..cb2f6050bdf7 100644 --- a/include/linux/compiler.h +++ b/include/linux/compiler.h @@ -149,10 +149,7 @@ void ftrace_likely_update(struct ftrace_likely_data *f= , int val, #endif =20 #ifndef RELOC_HIDE -# define RELOC_HIDE(ptr, off) \ - ({ unsigned long __ptr; \ - __ptr =3D (unsigned long) (ptr); \ - (typeof(ptr)) (__ptr + (off)); }) +# define RELOC_HIDE(ptr, off) ((typeof(ptr))((unsigned long)(ptr) + (off))) #endif =20 #define absolute_pointer(val) RELOC_HIDE((void *)(val), 0) diff --git a/lib/test_context-analysis.c b/lib/test_context-analysis.c index 140efa8a9763..06b4a6a028e0 100644 --- a/lib/test_context-analysis.c +++ b/lib/test_context-analysis.c @@ -596,3 +596,14 @@ static void __used test_ww_mutex_lock_ctx(struct test_= ww_mutex_data *d) =20 ww_mutex_destroy(&d->mtx); } + +static DEFINE_PER_CPU(raw_spinlock_t, test_per_cpu_lock); + +static void __used test_per_cpu(int cpu) +{ + raw_spin_lock(&per_cpu(test_per_cpu_lock, cpu)); + raw_spin_unlock(&per_cpu(test_per_cpu_lock, cpu)); + + raw_spin_lock(per_cpu_ptr(&test_per_cpu_lock, cpu)); + raw_spin_unlock(per_cpu_ptr(&test_per_cpu_lock, cpu)); +} --=20 2.53.0.851.ga537e3e6e9-goog