From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ed1-f67.google.com (mail-ed1-f67.google.com [209.85.208.67]) (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 93E8A2ECEBB for ; Fri, 30 Jan 2026 16:48:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.67 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769791701; cv=none; b=rJ2slQaUarLH9bcHNDacYChthLiDays8u7VUBJCGIEb3OmwyUdzSY50k/YYwsRL7GY71z2vC6E0XlogbSqvvfM2lHO/g59/6SyWR/fHmGhAYdWvCxrk2YEX8ogoiK/LkUlrYfIRXY398aBtybxC/oW6kBIs2oulxAt0sbi1OzDo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769791701; c=relaxed/simple; bh=f5GCbVXBlczNwQ5Vaf6tzAND2FAuuEYvNWmS17pcDkI=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=ZNswVFu9IzCHtWwF4nq/AeCqbtHcKtCx8PzdxCSKNOYmWQ5rgNv7JWZTOiYWOJA9qOo0AiaDwQ+R9hAQSk5F+RF8sMhHqSYZpy+krtav9z6BRPYnpF/S6vJVFy1h3aKwBYQeDt7so5wmPBC/c+dAP+yZbp9T2E9o5tFBQ7qxhgY= 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=lC40asZo; arc=none smtp.client-ip=209.85.208.67 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="lC40asZo" Received: by mail-ed1-f67.google.com with SMTP id 4fb4d7f45d1cf-658b511573cso4070844a12.0 for ; Fri, 30 Jan 2026 08:48:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769791698; x=1770396498; 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=D5YrrQBmasl55YNR+UE7+Tj54HTQyt4aQnFBBbSZIJY=; b=lC40asZoIp/BmWKiN+Mlx5jWSkLFf+8qskY0Ls9LTjhzsimXYWsnJ0rWDhIfNQI8+b 3RKygWR1doje8eahqAXd9LJQD2gIxE9iBBuS2lN8GYv9vnJsXUz7j1DjO1gRa5KtdIYU Xg4HHHpcQXj6uSuhTNtageuID63BcXTmuvrOMP+YIi5fSyQwIHuf0EV3928jZId12BBw /QUhWZEXDpbF70owih+xCqqq3TfLf0UKy40nhkyXe3qTg++D/mRB8ucIONCzkK3i2WGa rsTVnwfg7ujstOGFSfNLmpPyyKT/lQg1KuoYE54nswTblMvNuyGtWc4RKz7sm+5XVQVx BoxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769791698; x=1770396498; 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=D5YrrQBmasl55YNR+UE7+Tj54HTQyt4aQnFBBbSZIJY=; b=L3AEivb13AM1BpTwOaIVBMbNhP2pc99Vez9Qe5OcGTveRrdfcluZEvU8HmCQstF0Xs TWVGomyckqRFAgj0Ko/0zo5FLzJ3jwEf+xL1YSwt715/4hP4bq4CZlMMO9LqpsDMSYu9 W41n8YYwKAZAUUwMEZ/403h6dF+1+qkll1RDdnLWUxx3li6xEpx4hd32sWc4V9Nl2YUA u6LFpN9bWyN+wCiuzQQxJLUg8B+yO9v6sXzeQ9TgwyHTCmg3y7YQAskdm+sqNcdm4wlo TiLpJMlAvTSe9Km8mKdfmZbhMSAtzptiV6Ae4x7/FcbxQ3UWjDiUh4Fphi+gJ1qc+PHL gNwg== X-Forwarded-Encrypted: i=1; AJvYcCWKdDKCu7TsM0bM7rNcTAnLgWEm6oDXBMa1goS1X/mBXi3cIus2rGrApO00dxHM72Udocg0ZM93fYw1wlM=@vger.kernel.org X-Gm-Message-State: AOJu0Yzj71vUBS7sLa3mX558nQnrk1DCm3HT+NJ6IEmAY/CrcxyLCG/j wmwsehp8yyRkLBTO87yyXKMMUwul1saFsiEn0QeiGPH5FLxF/0FAN3tW3Wr0ob14 X-Gm-Gg: AZuq6aI0Cia2i3aW1oeOkOpbf3wR9wk+jS0Q4F3nDbFd8QyPG+k40z2GcL2tk3HQoNN zeMJpgONacKv33h1LS1LHUOmJK1SUp4Yvf9bKyMmW5+b4rgjNsk88Xh7mz/ru8exrrDn7jULIIO gefaU2t7aKNlshr8uXtAdleQefVW1G8zPmlS29FelJm2PGaRQJ2qTdZbGXW4sCct6hYJuinVdun 7zRBsJEkuexmZxSS8iFjLp1hnmZncQZn55M8VqBBgWZfLSY753Y+p3metL0IplWJObbu+bp5GbA q3qm8aMWt02jlvBBAiQldk8kD8/3FvllcojJ5hstFpuJInTqjT7JVlQWO2ta1rGJlQSYh9rRy4R QSkvLeGIRHPtmnTS9b3tgxqNo3cANSsnKv7r59+rky6XmMCwXGJAqwZzWq9t3wBSdZulVUNTZu6 ZqqaxU5nID8tfJRZYxuwTHaqTn+u/+kjh+x4uQo0XudYdsrgFEZ4HR X-Received: by 2002:a05:6000:3105:b0:430:f5ed:83f9 with SMTP id ffacd0b85a97d-435f3a6ccacmr4297915f8f.2.1769786016653; Fri, 30 Jan 2026 07:13:36 -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 ffacd0b85a97d-435e1322f40sm22288828f8f.34.2026.01.30.07.13.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jan 2026 07:13:36 -0800 (PST) Date: Fri, 30 Jan 2026 15:13:34 +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 , Boqun Feng Subject: Re: [PATCH v3 3/3] arm64, compiler-context-analysis: Permit alias analysis through __READ_ONCE() with CONFIG_LTO=y Message-ID: <20260130151334.2191973d@pumpkin> In-Reply-To: <20260130132951.2714396-4-elver@google.com> References: <20260130132951.2714396-1-elver@google.com> <20260130132951.2714396-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 Fri, 30 Jan 2026 14:28:26 +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); > ... > } > > At the time of the above report (prior to removal of the 'atomic' flag), > Clang Thread Safety Analysis's alias analysis resolved 'lock_ptr' to > 'atomic ? __u.__val : q->lock_ptr' (now just '__u.__val'), and used > this as the identity of the context lock given it cannot "see through" > the inline assembly; however, we 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' was set), 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 update via 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@intel.com/ > Cc: Peter Zijlstra > Tested-by: Boqun Feng > Signed-off-by: Marco Elver LGTM (for an obscure definition of G). Reviewed-by: David Laight > --- > v3: > * Use 'typeof(*__ret)'. > * Commit message. > > v2: > * Rebase. > --- > arch/arm64/include/asm/rwonce.h | 10 +++++++--- > 1 file changed, 7 insertions(+), 3 deletions(-) > > diff --git a/arch/arm64/include/asm/rwonce.h b/arch/arm64/include/asm/rwonce.h > index 42c9e8429274..b7de74d4bf07 100644 > --- a/arch/arm64/include/asm/rwonce.h > +++ b/arch/arm64/include/asm/rwonce.h > @@ -45,8 +45,12 @@ > */ > #define __READ_ONCE(x) \ > ({ \ > - typeof(&(x)) __x = &(x); \ > - union { __rwonce_typeof_unqual(*__x) __val; char __c[1]; } __u; \ > + auto __x = &(x); \ > + auto __ret = (__rwonce_typeof_unqual(*__x) *)__x; \ > + /* Hides alias reassignment from Clang's -Wthread-safety. */ \ > + auto __retp = &__ret; \ > + union { typeof(*__ret) __val; char __c[1]; } __u; \ > + *__retp = &__u.__val; \ > switch (sizeof(x)) { \ > case 1: \ > asm volatile(__LOAD_RCPC(b, %w0, %1) \ > @@ -71,7 +75,7 @@ > default: \ > __u.__val = *(volatile typeof(*__x) *)__x; \ > } \ > - __u.__val; \ > + *__ret; \ > }) > > #endif /* !BUILD_VDSO */