From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 36F4632D0F3 for ; Mon, 26 Jan 2026 11:16:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769426208; cv=none; b=ILVYE/5EEHT+xCNohrR7f7+bkmA2eMf+zgX6SzoJI6Wtet49djcRWYX5raOzN4YJ/uOY7J+EriVATOesIf4b0HpAtyYRG887w58638CArNZ+Es9BDIXwk+V/Bjz6/Md0LWVg/q+sHG0TgKhqj1+7z9x7Y5N2NDhYWsw/AEg9rNY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769426208; c=relaxed/simple; bh=2ZPNkFD1GYPg9REcGzzprNZfL5xqiLr3nO094G5OXpo=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MpNQU8PEghKQNjsW3bUgtCg7Wnln9hC/QcpABS9gUy/Y9/EPwq8oxa5BuhaAdLr8MFeDXtr/sxUkPAPtBHJOY4LMreLkVe4FenqXM/UTE2A8jPuOpLk5ybtB/jj+pYKcoh1htW0m3MyHQlCJppDvAhju/Nubn2KxeZl3nPyTY0I= 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=M+Oin7m/; arc=none smtp.client-ip=209.85.221.51 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="M+Oin7m/" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-42fb5810d39so2766130f8f.2 for ; Mon, 26 Jan 2026 03:16:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769426205; x=1770031005; 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=HAjYG+JlexRkGuXEPqUsDPk/vmWfkY8Y8l3nBpAyV6M=; b=M+Oin7m/IEty/tduKGA+vi6wptn2nCUyySU1vUqx7cDRczg1iSZwR1CKyJVTyOVaGC Kti9WsjQAzPRLNrcuMhEqLuBd5CTUChDOuniYyc/ac4cj9Hp1zWTYbkVCp8ug7O6MNtp Pa0ORayqPobF3bGwCs9AppPssZPXZx11cr9u0APfFQvhm+M9UTi0Mx5pTklvV6cHVRyz +azkyFVvu650ie0bo5smTHOX6bwf0SMi4cC4tYZwwkjIiwsUbXu8egHjYaXlsn2eBT40 4npfVilYF7oyjPUuN283hYLQCfhsIkc7OgXI5e10sxnUAS7ciiDBoE3uOVS7YPFT2JOz eeag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769426205; x=1770031005; 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=HAjYG+JlexRkGuXEPqUsDPk/vmWfkY8Y8l3nBpAyV6M=; b=mdz95VBVyhHGgXTpbwl6raiYdK9UU+PZIqG680ezzYH4AynlQzVYTjlfydeXhCKzeB Kr9vJH1abWlrYEKOTVWW5sQqUBQPdD+k8WkpAd7PnIEXVoayKBIzCZp37xAu6rBPLe03 U1PjS5ZlMa5UuUdykxXpTPnrEV3SEs8vHLj2TNCJWC/3W/rIB0gztcLMTFlTjsEL9Uav UuDvLF4fSJ4XB++q5gQeMdu1w2NKI6QsRQhKffAQLzbysHTEtFfBFUVd2ES3juT3NZqm MnvHgrWfsKGD5Tkq06bWpr+6HjO1myahPXOp5llkNk9wOQxaxHMPCkMo6ijnifS4K8tG /3Xw== X-Forwarded-Encrypted: i=1; AJvYcCUWTO2phVzQ8DYYkVtmcERmBiDOZ/u83wStYUn+G5YCiptU0RGZzsk6c4auJ9dbtmHkDPlaJGzsvqEgaP4=@vger.kernel.org X-Gm-Message-State: AOJu0Yx0ntby79Rrm10UyHHxlz0OKyDqnDSkvWk5Ezw3woDq041RGYDn PCS35o7OFgQDOaA9VC2+avs29REPYqwQLvN24rNdlMTkuITbF+4crer/ X-Gm-Gg: AZuq6aJiIlXBbTrIjL8jRApsRhA5KO6DU1hPDQ8VB2DS8SvCO2wL8uyTGNCbnFcwbbs StYSuZpkG8ANAH7XjCOD5pnpb3aonK2uaXBRLlFM34k5zOsONkBF+3WWKVT+HTkaK5hs3vABpYM wzCZjD4RjOMIx2A8uRqioRxGVCELdvuD0SXeem9T0DgFw9dUW4h5laqMNj3AH2i7NR/Cpehzix1 5T82C/77qK0M3mMMcWECtDTJRSNLDHgH+jINYlUhldCU3Aw5iuFrP/eY6F5aO4RQPuFYzCbZyJN XOCxLV5DrYl7pbKfzT8JzCbOBQCMgfh7wlufpjf1wC4AsH6r96IWueY+qlPmebDGU963FyY3YkZ 9ziUox62Sof/v4naEV2OPk2CjLl3kX8OvrRt3osaXsAidFtOTlXseqpAI+QNAbd4YTJgF0OMIIu swku3SnlProNUq4W1pbpzoyny4YzQz+TZvUESaYyIRv4XtBw9d8icw X-Received: by 2002:a05:6000:184f:b0:431:16d:63d7 with SMTP id ffacd0b85a97d-435ca198c6bmr7095312f8f.47.1769426205212; Mon, 26 Jan 2026 03:16:45 -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-435b1f744e2sm31432160f8f.31.2026.01.26.03.16.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Jan 2026 03:16:44 -0800 (PST) Date: Mon, 26 Jan 2026 11:16:43 +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 Subject: Re: [PATCH 2/3] arm64: Optimize __READ_ONCE() with CONFIG_LTO=y Message-ID: <20260126111643.534c8274@pumpkin> In-Reply-To: <20260126002936.2676435-3-elver@google.com> References: <20260126002936.2676435-1-elver@google.com> <20260126002936.2676435-3-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 Mon, 26 Jan 2026 01:25:11 +0100 Marco Elver wrote: > 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. Does it even need to be a union? I think (eg): TYPEOF_UNQUAL(*__x) __val; \ ... : "=r" (*(__u32 *)&__val) \ will have the same effect (might need an __force for sparse). Also is the 'default' branch even needed? READ_ONCE() rejects sizes other than 1, 2, 4 and 8. A quick search only found one oversize read - for 'struct vcpu_runstate_info' in arch/x86/kvm/xen.c Requiring that code use a different define might make sense. I also did some x86-64 build timings with compiletime_assert_rwonce_type() commented out. Expanding and compiling that check seems to add just over 1% to the build time. So anything to shrink that define is likely to be noticeable. David > ... > 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/rwonce.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 = &(x); \ > - int atomic = 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 = 0; \ > + __u.__val = *(volatile typeof(*__x) *)__x; \ > } \ > - atomic ? (typeof(*__x))__u.__val : (*(volatile typeof(*__x) *)__x);\ > + __u.__val; \ > }) > > #endif /* !BUILD_VDSO */