From nobody Sat Feb 7 17:19:49 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 13A3718DB2A for ; Mon, 26 Jan 2026 00:33:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769387590; cv=none; b=tve+Vs9VP2gZ0v+RnjSGtjGMapfzGX/bXIJqrQlEmEGOcPRsgd5Pb9qCWgNXH/c83dRApz0bkfFmQFs5HESOGERzo4ktyLpLQ/AumZgPiV16NR9Hn3D38Au7HWm23eOWUKmxfOikwqy+YO9q5TJ66Aa3JDj1tgRCXptVKuoS70A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769387590; c=relaxed/simple; bh=LHP7vc+QAjKrcQ/nSd8XnShnI8hVZmkUtqzqx890SiM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bi+RRpDZ1hlnS0kJ0KXuqphjYJTZ7ijZHfK/CVpBBkF89P8dX6QdtjSzOVWH+hNwbXMCOaYnnXkflQjXjHDwS4enWsNVeYewYHQFhDSyBIO/4g4iObUzKREteZ/hhuXtW+84MeN4Vr1h2KCj9yj0c1WbTfXAmnaH/1s9ftR+Zyw= 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=AcETk8PD; arc=none smtp.client-ip=209.85.128.74 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="AcETk8PD" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4804157a3c9so43412925e9.1 for ; Sun, 25 Jan 2026 16:33:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769387587; x=1769992387; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=y+0aVR3neN19eb/78h0OkLoTFjYmtJx01fQbpzy1/E0=; b=AcETk8PDSMv8aVjSO+RdYOzTLb8vqVLKzAkIFVQ9+RAZJKz/9iYUVFDqUf6B3o3mM+ vw6y82JsMTl5hk8I83HFvpXzeqMsMOpLk4/GR2gVQ1xbSjfhENJu+hozVCk6h8DK1m8Z 3LZGmMatTig0blpyjcExkLhq2y65OCRtMc2Y/R0zmC3e+h48yJRitfTMjycJpZGk1HrR OMCO70jYyqTWXbPZqI4h05rQz2yn6Tmz7Tkbrf4sSfapav2CEJt1QBsfq+mkpY990IWL asvrt68VulZ0UIpqjvy6ERf8yBcD8MWrbYt2g99MvKCItp6VBZtt/Jk8EoqwWVP1ZzaQ ogug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769387587; x=1769992387; h=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; bh=y+0aVR3neN19eb/78h0OkLoTFjYmtJx01fQbpzy1/E0=; b=LJZGNmPih3O/76zEB2p0xh5+ZgcFf8VEdcRbXO4u5YN1UVhd+O4/dCY/dva9w8bgUt HUViLhhJMM4666IMSrIb1XcKU8d/d+PfmkBcUNCu1CpRpPF7K8Vqw1trJhwu4MTBISA+ BQnQ2rA14uqWquWAUFSse4j2+8Fawv4n0XEoGfI12Lg653SLvWeglwuehCvb7RIy4k0R gFiPhddgS22wruxkxrnboj8faKtA0VUXHcQlgbP/3gMfk3zOjqZsU/qGxElRl5BMC85F 9b1DTpwWgJaV7THOp+W/K+NF7wWfVEiLKOWPz6r4nPDS0WNyWnKvk7rXMmXmjQ9XXFIf 54HQ== X-Forwarded-Encrypted: i=1; AJvYcCW1iHOSQwo1VbqLzi9fYhRDv5xv15mb535N3LENZ8tPnsIKCV85HeBVgxeGrNIjeYkmRxahyBYAXZxzhJU=@vger.kernel.org X-Gm-Message-State: AOJu0YwFSKzX0dOjTb1DK8596eabcV3kNQMJosL2ufDPG6WexSP1f7FM TS3deKPTDvaXJnY07TQDz/K+OyAptFX7oKLSBCKirsqU2p2RzjKePLER/TxzHvb0kOyi4MPOc7W 8pQ== X-Received: from wmne21.prod.google.com ([2002:a05:600c:4395:b0:477:a181:1922]) (user=elver job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600d:6405:10b0:475:dde5:d91b with SMTP id 5b1f17b1804b1-4805f624d56mr26596795e9.17.1769387587598; Sun, 25 Jan 2026 16:33:07 -0800 (PST) Date: Mon, 26 Jan 2026 01:25:10 +0100 In-Reply-To: <20260126002936.2676435-1-elver@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260126002936.2676435-1-elver@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260126002936.2676435-2-elver@google.com> Subject: [PATCH 1/3] arm64: Fix non-atomic __READ_ONCE() with CONFIG_LTO=y From: Marco Elver To: elver@google.com, Peter Zijlstra , Will Deacon Cc: Ingo Molnar , Thomas Gleixner , Boqun Feng , Waiman Long , Bart Van Assche , llvm@lists.linux.dev, Catalin Marinas , Arnd Bergmann , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The implementation of __READ_ONCE() under CONFIG_LTO=3Dy incorrectly qualified the fallback "once" access for types larger than 8 bytes, which are not atomic but should still happen "once" and suppress common compiler optimizations. The cast `volatile typeof(__x)` applied the volatile qualifier to the pointer type itself rather than the pointee. This created a volatile pointer to a non-volatile type, which violated __READ_ONCE() semantics. Fix this by casting to `volatile typeof(*__x) *`. With a defconfig + LTO + debug options build, we see the following functions to be affected: xen_manage_runstate_time (884 -> 944 bytes) xen_steal_clock (248 -> 340 bytes) ^-- use __READ_ONCE() to load vcpu_runstate_info structs Fixes: e35123d83ee3 ("arm64: lto: Strengthen READ_ONCE() to acquire when CO= NFIG_LTO=3Dy") Cc: Signed-off-by: Marco Elver --- arch/arm64/include/asm/rwonce.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/include/asm/rwonce.h b/arch/arm64/include/asm/rwonc= e.h index 78beceec10cd..fc0fb42b0b64 100644 --- a/arch/arm64/include/asm/rwonce.h +++ b/arch/arm64/include/asm/rwonce.h @@ -58,7 +58,7 @@ default: \ atomic =3D 0; \ } \ - atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(__x))__x);\ + atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(*__x) *)__x);\ }) =20 #endif /* !BUILD_VDSO */ --=20 2.52.0.457.g6b5491de43-goog From nobody Sat Feb 7 17:19:49 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 E200516DC28 for ; Mon, 26 Jan 2026 00:33:11 +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=1769387593; cv=none; b=iXTXuF0a/BWJVCtGJhLndrxDbfJgfAOeRWe49qTsSIHW+bmEzsmRwHKvdW904Y7s6ac5mLBFGjcGgdZSC1igp8aoIITdJh5YNNgzG2ouwd7FNNzIwK2A1ZYCK+n9DeRUeT4wjeqFS2fd4gU4CPevucwyZwftdRUcOAKB1Fwys6g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769387593; c=relaxed/simple; bh=CL1Mshu87ynwu8kNu8sWz+pKSZGzdgDZHPjtAF+6ga4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WwSUkb5TAJGSDLyjV014dLIJ2QioaCdGr4Viv4D1GqJxOzldNlGygiBMnHSWsq6XTOiYsAAwce4sHbzp7XrWh9Iy6Ogi7ekNK/dy2j5mM8MDnbCwbHGfoHgV2hftKnFGsqSbBy1+hEebagLFDEQ683dLsOIjiRcU05JRnWKb37k= 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=GqRIk4jT; 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="GqRIk4jT" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4801e3aab32so18875185e9.3 for ; Sun, 25 Jan 2026 16:33:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769387590; x=1769992390; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=26BDCJEdJLp7RpCdQh1+Gie1UpBf3nEsfBaL4sC4xLw=; b=GqRIk4jT2MLcyJGzOVE/7z5R1lqLoYyiF6EU/rIF0x64icb606KxUft0TnGFg7G6VL 6wzvasvc/Xm10vPfy/ui5K34HqORdfTQbjETb5489qY7wGrZB75Kp+GDpX9W+Cexppry n5wrj0Gc4pT6VXZ/5sQ35vLpKnavh9Lf0FxlOyMVZHjtY7HOf89p0iG4WM18UCL9pSkw vGftsU+X7e7gUoik5agmJ8YVUe1q+ZkWMhu27luaqTNv8dA9r53CpcdqVD8RYRLljmZu HoE24khaMYwsImPdm7pR0/T2ppRRK8FkB9NbZcStKR8Yj9+Y6pzunM4D1glTUb0q/uGb PuRw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769387590; x=1769992390; h=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; bh=26BDCJEdJLp7RpCdQh1+Gie1UpBf3nEsfBaL4sC4xLw=; b=c0IKKIpp4O1epMOlOReI0OV0BhJ+HFmbkv0QRqHYq1CN1OwBKl5EuJF6Fit71Rg09l pweo+Qh6mgQipz4qllTlOz1GIDEJJSQrHUXU1tRJhCtWEA8htwlwiraVeXNGrDhvSqOM hW5TvLo7QNkIYtJNnN9ntWPscI5DBpqjZjbDNUEtlNDoBeZNMoLg7G5zhZhByhGp8t7L HeQT4nA48AHo7OmOm+ur6nAE39EMa0KDokp8bwemT88Wf9DMhAlw4IX7K1e0C3d2j/Kf 726V5D6ahs05QeYfpFVNKInlrXB0JnEo21x5Jw3D8wVOY1qpaw6+ZpFH862YloFWfOg4 3NEQ== X-Forwarded-Encrypted: i=1; AJvYcCWjaytmKwKquG7ItqmNvSBz00hNRp5CCCL1YtV9o2tG752AiU6MDb9ZNu9GjXKIcEaU9M97meVo/hOK87k=@vger.kernel.org X-Gm-Message-State: AOJu0YwCx9aTxH1tGJ7l0YJSVxgTkbiYuxJL5Pg4wrYq6D9eO3rd15H+ tITGab6HT+FDa7D0upeKAu4/4EgA3bLUJxUsHUmoiqcJS3Nz+JfWYQdBh6zXbV9FSwsFGUDoTby cAQ== X-Received: from wmbjw5.prod.google.com ([2002:a05:600c:5745:b0:47e:e4f5:36ca]) (user=elver job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:190c:b0:477:9dc1:b706 with SMTP id 5b1f17b1804b1-4805ce67eb4mr48399755e9.19.1769387590378; Sun, 25 Jan 2026 16:33:10 -0800 (PST) Date: Mon, 26 Jan 2026 01:25:11 +0100 In-Reply-To: <20260126002936.2676435-1-elver@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260126002936.2676435-1-elver@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260126002936.2676435-3-elver@google.com> Subject: [PATCH 2/3] arm64: Optimize __READ_ONCE() with CONFIG_LTO=y From: Marco Elver To: elver@google.com, Peter Zijlstra , Will Deacon Cc: Ingo Molnar , Thomas Gleixner , Boqun Feng , Waiman Long , Bart Van Assche , llvm@lists.linux.dev, Catalin Marinas , Arnd Bergmann , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rework arm64 LTO __READ_ONCE() to improve code generation as follows: 1. Replace the _Generic-based __unqual_scalar_typeof() with the builtin typeof_unqual(). This strips qualifiers from all types, not just integer types, which is required to be able to assign (must be non-const) to __u.__val in the non-atomic case (required for #2). One subtle point here is that non-integer types of __val could be const or volatile within the union with the old __unqual_scalar_typeof(), if the passed variable is const or volatile. This would then result in a forced load from the stack if __u.__val is volatile; in the case of const, it does look odd if the underlying storage changes, but the compiler is told said member is "const" -- it smells like UB. 2. Eliminate the atomic flag and ternary conditional expression. Move the fallback volatile load into the default case of the switch, ensuring __u is unconditionally initialized across all paths. The statement expression now unconditionally returns __u.__val. This refactoring appears to help the compiler improve (or fix) code generation. With a defconfig + LTO + debug options builds, we observe different codegen for the following functions: btrfs_reclaim_sweep (708 -> 1032 bytes) btrfs_sinfo_bg_reclaim_threshold_store (200 -> 204 bytes) check_mem_access (3652 -> 3692 bytes) [inlined bpf_map_is_rdonly] console_flush_all (1268 -> 1264 bytes) console_lock_spinning_disable_and_check (180 -> 176 bytes) igb_add_filter (640 -> 636 bytes) igb_config_tx_modes (2404 -> 2400 bytes) kvm_vcpu_on_spin (480 -> 476 bytes) map_freeze (376 -> 380 bytes) netlink_bind (1664 -> 1656 bytes) nmi_cpu_backtrace (404 -> 400 bytes) set_rps_cpu (516 -> 520 bytes) swap_cluster_readahead (944 -> 932 bytes) tcp_accecn_third_ack (328 -> 336 bytes) tcp_create_openreq_child (1764 -> 1772 bytes) tcp_data_queue (5784 -> 5892 bytes) tcp_ecn_rcv_synack (620 -> 628 bytes) xen_manage_runstate_time (944 -> 896 bytes) xen_steal_clock (340 -> 296 bytes) Increase of some functions are due to more aggressive inlining due to better codegen (in this build, e.g. bpf_map_is_rdonly is no longer present due to being inlined completely). Signed-off-by: Marco Elver --- arch/arm64/include/asm/rwonce.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/arm64/include/asm/rwonce.h b/arch/arm64/include/asm/rwonc= e.h index fc0fb42b0b64..9963948f4b44 100644 --- a/arch/arm64/include/asm/rwonce.h +++ b/arch/arm64/include/asm/rwonce.h @@ -32,8 +32,7 @@ #define __READ_ONCE(x) \ ({ \ typeof(&(x)) __x =3D &(x); \ - int atomic =3D 1; \ - union { __unqual_scalar_typeof(*__x) __val; char __c[1]; } __u; \ + union { TYPEOF_UNQUAL(*__x) __val; char __c[1]; } __u; \ switch (sizeof(x)) { \ case 1: \ asm volatile(__LOAD_RCPC(b, %w0, %1) \ @@ -56,9 +55,9 @@ : "Q" (*__x) : "memory"); \ break; \ default: \ - atomic =3D 0; \ + __u.__val =3D *(volatile typeof(*__x) *)__x; \ } \ - atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(*__x) *)__x);\ + __u.__val; \ }) =20 #endif /* !BUILD_VDSO */ --=20 2.52.0.457.g6b5491de43-goog From nobody Sat Feb 7 17:19:49 2026 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (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 796A013A258 for ; Mon, 26 Jan 2026 00:33:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769387597; cv=none; b=tiRC7iAmcyPsSHgAzSFFybguhMzlbDbAIzRQaDuqfn5tYQKFD8okWhQiuBcWpaSag8Fs7WUcIbhb1C/bOnsfNH9TvLQ0FfLiiUUYTUfezUijrLkZKdvV7S7OaTrm1iOXBWe4blsbK/Wdh2aLUKe/GyXKwTDZ2sapyyQiKecDMms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769387597; c=relaxed/simple; bh=fyabtr/WWe4ln44fb6JDEbFXLec2d5CvMKWPch2hh8I=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=rQYWJyXKLZ66Q3qK/Ph/wpVc2L1ZuQo6Abkimj+RmWxErlEsOvNNYuyWBY8MHCicO6ddav+jQKEP7EiM6QIVwFBn7m3QUGUJQSPbP6wt03emPvg9yn8UY9f3CDhWDEvVBgXtHVfyjxk8C3cpuQgzs0tfmBT+5x2JiDwK4j9Kuf4= 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=u/gGmr8P; arc=none smtp.client-ip=209.85.128.74 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="u/gGmr8P" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4801e2e3532so26681925e9.2 for ; Sun, 25 Jan 2026 16:33:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769387593; x=1769992393; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=YzvRauRfmowbTuXqteNercyqQMxwj4TFlVBC4P/9vK8=; b=u/gGmr8PNq6uIrGXBXui95cmtMYTnYk/sqzPJ/WI9uQPT8OjDxbSJlF1Wl2+DIo29C GOzJomOIMTY/jtLRxj5t1u1fQKRU+46Dux91RGW/mhr9EyLB3kKHcF9S4cXPUHBNe2TJ KMqY8nYHHo+UG7JA7QCYh83x1sld9SFG8Ypzpge8YPM0FayAAPClKkXEzSLGctfNrD0i I+ngfoNIfCCxMfeHakUEz/2SOuRPiTx6FKzAMnTFo/fiHtubDMNCkoQQiILpSLLLItLJ eEOKnZXQGjPafQ8SZz7/n//dHkFeVQt6Y6DwSpdfbeDJwUsPJwZ1J4umz66hTMI1Sld1 mRPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769387593; x=1769992393; h=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; bh=YzvRauRfmowbTuXqteNercyqQMxwj4TFlVBC4P/9vK8=; b=dYUxfV/XYiWbeMi8rWli5obeQyOLtEcxXvxuU2+MJ7SApfu6bL+u1nBsQ8CW0QqSIM +/v772grNxt7iAxahtOsVjy6tivjN2GpkhqxHapxsWTG7YasW6V6hlF4+jEIV8QDXPOR m21mNd73k/mzYxVGg1TpZj61DvmL+sbBj35bIO2wY4kpepLyInfRhjyaSQKdIBhaRxEM 8d5b767RWBGwoyF+NJGTBRpaU60WLl7xGf3ytGFafRd7wq39fiZ4DZBV7fz+8UHekg2K t0CygHBp9XD92cSIOnlFIcGsndySOCaW6/mpkA74/ALukmJArQEIrb/hp7N/B1Gi3uS2 khMA== X-Forwarded-Encrypted: i=1; AJvYcCUGyqk79vK5vZ0Xvg3LqoXxAQU1Xo0yyYL29KM2nPHM1rGDgqxzo34eP9JBk1xiwiIMKXAtWCJ7mIcrFVk=@vger.kernel.org X-Gm-Message-State: AOJu0Yz0sYw6ltcQ+DYyOROSSn2d4Ds6CYSCcRM+XaelB3oWNId3TXq7 EuVKiBA0zeRj28OInoPZ/WdnU07ImdPae2/N88VuYvrn+e5Vt8cMagBYEMQhkMab7VB9nydHOKJ Tpg== X-Received: from wmps41.prod.google.com ([2002:a05:600c:31a9:b0:480:4a03:7b7d]) (user=elver job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4684:b0:47e:e20e:bba3 with SMTP id 5b1f17b1804b1-4805cd44c41mr51919685e9.7.1769387593066; Sun, 25 Jan 2026 16:33:13 -0800 (PST) Date: Mon, 26 Jan 2026 01:25:12 +0100 In-Reply-To: <20260126002936.2676435-1-elver@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260126002936.2676435-1-elver@google.com> X-Mailer: git-send-email 2.52.0.457.g6b5491de43-goog Message-ID: <20260126002936.2676435-4-elver@google.com> Subject: [PATCH 3/3] arm64, compiler-context-analysis: Permit alias analysis through __READ_ONCE() with CONFIG_LTO=y From: Marco Elver To: elver@google.com, Peter Zijlstra , Will Deacon Cc: Ingo Molnar , Thomas Gleixner , Boqun Feng , Waiman Long , Bart Van Assche , llvm@lists.linux.dev, Catalin Marinas , Arnd Bergmann , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, kernel test robot Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When enabling Clang's Context Analysis (aka. Thread Safety Analysis) on kernel/futex/core.o (see Peter's changes at [1]), in arm64 LTO builds we could see: | kernel/futex/core.c:982:1: warning: spinlock 'atomic ? __u.__val : q->loc= k_ptr' is still held at the end of function [-Wthread-safety-analysis] | 982 | } | | ^ | kernel/futex/core.c:976:2: note: spinlock acquired here | 976 | spin_lock(lock_ptr); | | ^ | kernel/futex/core.c:982:1: warning: expecting spinlock 'q->lock_ptr' to b= e held at the end of function [-Wthread-safety-analysis] | 982 | } | | ^ | kernel/futex/core.c:966:6: note: spinlock acquired here | 966 | void futex_q_lockptr_lock(struct futex_q *q) | | ^ | 2 warnings generated. Where we have: extern void futex_q_lockptr_lock(struct futex_q *q) __acquires(q->lock_ptr= ); .. void futex_q_lockptr_lock(struct futex_q *q) { spinlock_t *lock_ptr; /* * See futex_unqueue() why lock_ptr can change. */ guard(rcu)(); retry: >> lock_ptr =3D READ_ONCE(q->lock_ptr); spin_lock(lock_ptr); ... } The READ_ONCE() above is expanded to arm64's LTO __READ_ONCE(). Here, Clang Thread Safety Analysis's alias analysis resolves 'lock_ptr' to 'atomic ? __u.__val : q->lock_ptr', and considers this the identity of the context lock given it can't see through the inline assembly; however, we simply want 'q->lock_ptr' as the canonical context lock. While for code generation the compiler simplified to __u.__val for pointers (8 byte case -> atomic), TSA's analysis (a) happens much earlier on the AST, and (b) would be the wrong deduction. Now that we've gotten rid of the 'atomic' ternary comparison, we can return '__u.__val' through a pointer that we initialize with '&x', but then change with a pointer-to-pointer. When READ_ONCE()'ing a context lock pointer, TSA's alias analysis does not invalidate the initial alias when updated through the pointer-to-pointer, and we make it effectively "see through" the __READ_ONCE(). Code generation is unchanged. Link: https://lkml.kernel.org/r/20260121110704.221498346@infradead.org [1] Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202601221040.TeM0ihff-lkp@int= el.com/ Cc: Peter Zijlstra Signed-off-by: Marco Elver --- arch/arm64/include/asm/rwonce.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/arch/arm64/include/asm/rwonce.h b/arch/arm64/include/asm/rwonc= e.h index 9963948f4b44..85b1dd7b0274 100644 --- a/arch/arm64/include/asm/rwonce.h +++ b/arch/arm64/include/asm/rwonce.h @@ -31,8 +31,10 @@ */ #define __READ_ONCE(x) \ ({ \ - typeof(&(x)) __x =3D &(x); \ + auto __x =3D &(x); \ + auto __ret =3D (TYPEOF_UNQUAL(*__x) *)__x, *__retp =3D &__ret; \ union { TYPEOF_UNQUAL(*__x) __val; char __c[1]; } __u; \ + *__retp =3D &__u.__val; \ switch (sizeof(x)) { \ case 1: \ asm volatile(__LOAD_RCPC(b, %w0, %1) \ @@ -57,7 +59,7 @@ default: \ __u.__val =3D *(volatile typeof(*__x) *)__x; \ } \ - __u.__val; \ + *__ret; \ }) =20 #endif /* !BUILD_VDSO */ --=20 2.52.0.457.g6b5491de43-goog