From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) (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 AF0A836604A for ; Thu, 29 Jan 2026 11:41:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.66 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769686907; cv=none; b=X0YnyJkIsxy7QR2s4s0DAVIpQFsUExcJ0TJzIjxKlrNgAkTyhiKkiNncbeJ/D1m9RsTbNCRr9sTzM/J9jw88ww9kJUYFbWFis/IhXGjFK6jWCweHgxmvKj/0jr2knOap1pQDAPAQ+KaY5rWog0y3xDA8xri73lWczIzH1rNmA1g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769686907; c=relaxed/simple; bh=nP8Odpge9e+1m2eIIzLh58vN5z/ciiKpXbKsThkvUC0=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gOCq92PMd73Tp2ZDkfSsTRBVHumuOGWEu90G52OV5Lwz2nl0bQQpFIDT9rMEkUETosUXvLCnrR5SO59xjtiFoe6wUzEV0J/zeyo+p8MDH+XoOGLdyvISAMEruYDLeDEoFaLK77d7A+dB8GlBEXGk/wAPQtEx9ZSpKS/YtQW2oeo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RIyivQJ3; arc=none smtp.client-ip=209.85.221.66 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RIyivQJ3" Received: by mail-wr1-f66.google.com with SMTP id ffacd0b85a97d-435f177a8f7so168075f8f.1 for ; Thu, 29 Jan 2026 03:41:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769686904; x=1770291704; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=fLXst4hKcwqR8aL0m9xNuTfU7cjH0z0TV7vdns2E8pI=; b=RIyivQJ3q14Qgp+sJaz4ZxYMxKwXOaLl8Yjbs4riAwHgwpE6botwhWe0zvjvU3PQil UCC8CfxedRJJXtZ4htAOU3xQwMPHe6c+Z5an2zV+4VhCzUpvSXYZzpj9ugSwo2umu6MK rqzKge0za2qbfeG0F6HFUdIbuQ2krsU+785JgqblCpCfJ4mL/VaoKTrACGQ8OYShxohd IlHkk0VMsV76v8cr9D9djiOoK/FIDtu8I4GQ4F1b24CQFpLA4u9Fj2kZwxBikvf5EGPf IN+VNcA+AR6WbBLuQo2LDhJzMxPGk0ZJysi/sWsIEuGFkoT1KawKdTBhIGYCCwv6EcuG EK5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769686904; x=1770291704; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fLXst4hKcwqR8aL0m9xNuTfU7cjH0z0TV7vdns2E8pI=; b=sjGfWi/bJJ5juxIiOlPfHZrtL/kpvVtcrIz5D/n3AN0pOMPM4NWbQRwetDvMV5eBdz n6+HOG6saZeyNBhof6tEd62FkdRHi4L39SNP2j9tVKWeiC8zW+Qs3WIfZ99fVNSxhyqV B/Mdg0K//WbQ4RrA8p6aIPNaKvGiOi7Ue0OqdxCFkPdoCrAG8CQvJdPNPnYFWr5ooArx A9StdsQHtAoI11LsucVS681ps5ueYg75ei5NRc9l986RCHE0pvvvUYFg4ShYrW+hhroD s9IwWg6mp0PHDrrm+84CEIDEPrUd9HDRItKfXBdh1/HSofeJp88mUdFd3700SVW96JZo hM4Q== X-Forwarded-Encrypted: i=1; AJvYcCVwr5v5Ixgr0Ok2tzO7Isbqoe9oetbrcLNaL/6rIq4fLpWyR/EFWSLf9sH9yY9N9/7ozITIFhY/YDZh08k=@vger.kernel.org X-Gm-Message-State: AOJu0Yw6lE7JYrK4Nk8afWSeujfsI4SQH9jeFM7FIql4uuRyH0axdhIu NRrEvbHq+/wNkAo7+tHaNBNQZngayeFsbZ1YCSlAS/ZIbYT4k+OOaHuA/gj8c3nZ X-Gm-Gg: AZuq6aIPqSKwqDqLQjWiu6QDAxnBrYF8WwFp0XOkLxqVknCUCi1QYCbR79iFpeInhw1 zVgR8OZiUsdRa6fIgKpFfywAYC7CNs10Vxa8hhKeLB3BU7ByHz6s9yFXvpFrnW5jA7MpcgkH/Zc opTVkIZOQ8LwXHCRfhO8rWAA6svWzzAux8oUjxwXI1poHYrASoSddpRbnVaKjP7WCex8J1i8kxv 3SbieZc/I3XkAmPMjWMaTAudiUYiNaD9t7CfYeqcbsyW4OjSaIwrtiIGmEEDPBglxtWUcwBWtMI jvAwcfQNOL5eQak1VOpt947WQUFMZQsVB7L6G9m3yNE9cshmznE24vgQqBwN97Ax+EItVT12FD4 GLi95r8YcVVxSQXYPVdFFw2kTH3Q9QRCML+Xu0hhA86ZNuMPDBlR9D/WqovwKoqKt/zJ5HZZIn6 okxRXIzuE+iAgEjB8zGeoegZLC/zghwtmRCmXN2f1BsEK64+HJDmkf X-Received: by 2002:a05:600c:c16b:b0:47d:264e:b35a with SMTP id 5b1f17b1804b1-48069c1a85bmr92237885e9.13.1769680343784; Thu, 29 Jan 2026 01:52:23 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-48066aaf235sm177623235e9.0.2026.01.29.01.52.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 29 Jan 2026 01:52:23 -0800 (PST) Date: Thu, 29 Jan 2026 09:52:19 +0000 From: David Laight To: Marco Elver Cc: Peter Zijlstra , Will Deacon , 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 Subject: Re: [PATCH v2 3/3] arm64, compiler-context-analysis: Permit alias analysis through __READ_ONCE() with CONFIG_LTO=y Message-ID: <20260129095219.6a3b8dc8@pumpkin> In-Reply-To: <20260129005645.747680-4-elver@google.com> References: <20260129005645.747680-1-elver@google.com> <20260129005645.747680-4-elver@google.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 29 Jan 2026 01:52:34 +0100 Marco Elver wrote: > 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->lock_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 be 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 = 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', Doesn't the previous patch remove that conditional? This description should really refer to the code before this patch. > 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(). Some of that need to be a comment in the code. I also suspect you've just found a bug in the TSA logic. > > 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@intel.com/ > Cc: Peter Zijlstra > Signed-off-by: Marco Elver > --- > v2: > * Rebase. > --- > arch/arm64/include/asm/rwonce.h | 7 +++++-- > 1 file changed, 5 insertions(+), 2 deletions(-) > > diff --git a/arch/arm64/include/asm/rwonce.h b/arch/arm64/include/asm/rwonce.h > index 712de3238f9a..3a50a1d0d17e 100644 > --- a/arch/arm64/include/asm/rwonce.h > +++ b/arch/arm64/include/asm/rwonce.h > @@ -48,8 +48,11 @@ > */ > #define __READ_ONCE(x) \ > ({ \ > - typeof(&(x)) __x = &(x); \ > + auto __x = &(x); \ > + auto __ret = (__rwonce_typeof_unqual(*__x) *)__x; \ > + auto __retp = &__ret; \ > union { __rwonce_typeof_unqual(*__x) __val; char __c[1]; } __u; \ Can you define __val using typeof(__ret)? Saves expanding the macro twice (although it isn't the horrid __unqual_scaler_typeof() any more). David > + *__retp = &__u.__val; \ > switch (sizeof(x)) { \ > case 1: \ > asm volatile(__LOAD_RCPC(b, %w0, %1) \ > @@ -74,7 +77,7 @@ > default: \ > __u.__val = *(volatile typeof(*__x) *)__x; \ > } \ > - __u.__val; \ > + *__ret; \ > }) > > #endif /* !BUILD_VDSO */