From nobody Fri Apr 3 10:19:54 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D54A2288CB; Tue, 24 Mar 2026 14:09:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774361390; cv=none; b=mnUFohbs+yuUfnOqOX3j9cRgpMLy8abiapIaBBt6EGqaLIIYVV+Mz5mEatvA7DdqJvncoNluLclvILLhZ7XT0XC/qtboaG7l7T1izEtfEfClTY064hP/+TMkUR4MfZ0/n5Cb1RBnSnELT6bIiVIHjO3MrZa7fr0RsNkHlEKMkzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774361390; c=relaxed/simple; bh=Vitney8RYzWtLc+eKFhSoTESwsQV7txtFA4svm9bwHU=; h=Date:From:To:Subject:Cc:In-Reply-To:References:MIME-Version: Message-ID:Content-Type; b=NhrBOkwkNdQE0Lls6z6ozNxzhZw5o545rxMnXROrdS6x+WtEUFXjWhOg33omBtgIjfO0JT/2yu8+iN1iGFRM3XbJAqq2gpmkREMrDmGWnZOQDhmROCwlcuVan77BxTt/8q+IRbYSrRHYjf5YswASPqHv3PjoHhfPHEEfZDyp1XQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HxPbSwsO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=MK3eIoXa; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HxPbSwsO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="MK3eIoXa" Date: Tue, 24 Mar 2026 14:09:45 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1774361387; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zZrkN7Jkd7+Rgljx4ZSgNq1waJIS/3sNBYzYDhtJ07o=; b=HxPbSwsO5rIq/wUJUeYqA1dfKuNVzd1SggkIRwZDag42r27cr3W3H9gfP3bUl6RUqBmWR7 4/a6C+ne/MXzdyuF+ggui9j4rkFIdSwSZTCvOxZ1rLPQtMLzWCEeasNt0lH0gwndpH/Qa/ NGvdlR8WV5qbhotNgI/BWGH8S7U/WH7KfxUU9J9mHdDgnfH7SqBXZ7tOP5twFh4J6qKyqJ rEBJbU1ZGPcsX1ioCvIVfBhlqULkcrvTtVdP2RliRjcJ4e7yBtY4lTa6G96u8QEpumfFEL wrcomBw72bQMaxSgmbCCM0zqhJjCUzCgh+aw+kSHq1rlaZu1+gV5LlCPRmtwcA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1774361387; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zZrkN7Jkd7+Rgljx4ZSgNq1waJIS/3sNBYzYDhtJ07o=; b=MK3eIoXaMEqjfG9d/lQ9rQgZoeZlRoMtQ+EzdpgfuG9rKBtle9+LXIYy1V56knGeTJlegA aTDKMWHBUe3GioBw== From: "tip-bot2 for Marco Elver" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: locking/core] compiler: Simplify generic RELOC_HIDE() Cc: Bart Van Assche , Sean Christopherson , Marco Elver , "Peter Zijlstra (Intel)" , Nathan Chancellor , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20260319135245.1420780-1-elver@google.com> References: <20260319135245.1420780-1-elver@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Message-ID: <177436138567.1647592.1993694466924465359.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Precedence: bulk Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The following commit has been merged into the locking/core branch of tip: Commit-ID: a21c1e961de28b95099a9ca2c3774b2eee1a33bb Gitweb: https://git.kernel.org/tip/a21c1e961de28b95099a9ca2c3774b2ee= e1a33bb Author: Marco Elver AuthorDate: Thu, 19 Mar 2026 14:52:38 +01:00 Committer: Peter Zijlstra CommitterDate: Tue, 24 Mar 2026 15:08:05 +01:00 compiler: Simplify generic RELOC_HIDE() 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. Reported-by: Bart Van Assche Reported-by: Sean Christopherson Signed-off-by: Marco Elver Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Nathan Chancellor Link: https://lore.kernel.org/all/e3946223-4543-4a76-a328-9c6865e95192@acm.= org/ Link: https://patch.msgid.link/20260319135245.1420780-1-elver@google.com --- 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 af16624..cb2f605 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 140efa8..06b4a6a 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)); +}