From nobody Sun Jun 14 09:54:34 2026 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 C2B6D34CFAC for ; Thu, 2 Apr 2026 06:56:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775113003; cv=none; b=Pcdr3sLdrkounE6wxuW7POSVIi+kC1BW3d1tmJiVT9KdstwXM5QiXJVy59Nwb1wSu1gv6+8hUos5titPi79u1TWn5nC8q8evvvOkuCD6kqILKE15hU95UN1mgoS5i6LGO/UYyOMcZec8uQ0P1cqH/R8Cb64uoGgki9W0veECwFY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775113003; c=relaxed/simple; bh=duIUmMHX/NLgaTXyePmtipwg8X2dHzvr4MT8FB07vLE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:To:Cc; b=JKjaGgX04P9BRyPyI0l0j41mU/8Yel7gn7v73hC/Z2Tz2RcfTE2YROF3XG9XSG7MX+lEUoWQSKd35wcyjIsn10BpzGHuR3xT2gvjwoKTu2xD/tDgRA4UgTb4JuSIHcc1Juw+nHk1GfJEnC4bT2ww0ocAjVyjZZWHcjcQqFBooCw= 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=IinDgIYH; arc=none smtp.client-ip=209.85.214.177 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="IinDgIYH" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2b24fdac394so4905115ad.3 for ; Wed, 01 Apr 2026 23:56:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775112999; x=1775717799; darn=vger.kernel.org; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:from:to:cc:subject:date:message-id:reply-to; bh=FlpdYf1WLDHAMV/rn1y6xYHlGkLnTRJ4f+O5zsHNUww=; b=IinDgIYH0oyXAPxGGKk4lI6hwBNshlo8FY9/XREnT8nuK7rFpzyb5WdqNr/oSKhPvl VOgjTHqLdijiOybFzWW92uFZPnlbRRb+tu1AmJtr1yTJw47vO+6J5kPKS8C+kM/1BpOx ZLnlhANXeDoCq32y6h+ypHi3Hi6kVz1PkIbpuMmHPcFwM7GkZ6jN13IL6WzhmpIdSmCh 39ectPh0cHz/WE+SmEsrKALYpByT3XE7q8eSO/fPPPZ+Fn+zLhdf46kU8JxR6Va5rd3Q si2gpvewD8uZPkRXeF/S8cE84M/YlprKaOvEsClhalHTjEKcJVg8noPImb2+4XT9TEes Ow/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775112999; x=1775717799; h=cc:to:message-id:content-transfer-encoding:mime-version:subject :date:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=FlpdYf1WLDHAMV/rn1y6xYHlGkLnTRJ4f+O5zsHNUww=; b=I8n1PTNIQTPURkIyNqL5be9XCvtD20gLLMlUpZ9ZQSZ9iv0UT2ZAvSckVg5ND/F5QQ xB2PIhkbgD6Lc0zQeCuIynH2cbm4cKiKTrEEQrh0Q6rN7XNQoQJAAibVF71L3/m+up8w 4sSXrxviGHhQiEaXZywGEXxdvuD3TnhOURbYcgSWli/uYKNS2XHcWL8milb0F2ZW3TAo U/fkHyl+AdgX74GBnq+Ig/ugoaP7Q7vwKmz9bcNUpakB+ICU1ML0Bcmc9JKYtbQTLDA8 ZKOnFO+E3jOIhPqrAGYE6FgSgdUoB8J20iSZGpl0cYeUPrxBqxPj978RX4LvHrwGu1OG 0eng== X-Forwarded-Encrypted: i=1; AJvYcCU1gfsjsdlpYpYvCFYfH3R7rRya3UovwrYJfFONbLi+QaYTf+lIdAqMDySK3vC0sQxa3OZIHkQrMs8QWWw=@vger.kernel.org X-Gm-Message-State: AOJu0Yy1fvHZY4FXrUvrPRsrR1ceJrBG+HGp2dF7BZfN6yv/DxpXbxqP wN/SegS4uY6yjrJ/S3v/EG7qFd40KDUEiWNCvo6DfbPLUO8pAbtP35I+ X-Gm-Gg: AeBDieuWNIqMMPWc9XWFpc+kaxEhGMcImf9sZkSyzYa6taHDsbtAzs2YAKbwGMVZ2rY peVR9cEhcKfkGroLXjF21mz/nusUmAwi6wlwv87cLQzEbm9NEB35OMXd5bCs2LmL8yAmOB9GbjR +XBxofU66GXKeO0FJh2CHLvNotzzq/TCZU/0EVs6Csakv3EJHMlKFYVfuiZQSIG/ORI9RvirVes hKCZWXe1DKFTjonL70a0aD9mCZ00QXOMlazXtw3s125JCVgOL9o8C48aNgsilhGj9ddxZpAUFtn nNJhcblQhncBL2rXCCcaQliqTz7ffFe5AMuw1kbzHYsVHAuAynY9OLotkqGkabztlFSD+4+rpqf 1v1GHxUE1Dh/WutBoid9nXLhfRt8y/qfsoBPuFWQ3VyoGUfW9PWjLeLWldAzmyoiF7uhS4OuK+k Hny+CVvGXWxXigHHYsK9ooneN1WG27VFRWQKHy+Lk0hb2NJ8H8BTKj+HAK+lhKqLCssb8= X-Received: by 2002:a17:903:90b:b0:2b2:470d:6d18 with SMTP id d9443c01a7336-2b269adfea5mr73898055ad.8.1775112999356; Wed, 01 Apr 2026 23:56:39 -0700 (PDT) Received: from 1.0.0.127.in-addr.arpa ([240e:34c:5765:500:7999:76d9:8e52:f837]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b2749a1e9csm19647945ad.55.2026.04.01.23.56.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Apr 2026 23:56:38 -0700 (PDT) From: Hangbin Liu Date: Thu, 02 Apr 2026 14:56:01 +0800 Subject: [PATCH] cmpxchg: allow const-qualified old value in cmpxchg() 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="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260402-cmpxchg-v1-1-4561e2c05d2c@gmail.com> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIzMDEwMj3eTcgorkjHRdo7QUYwPzNBOTRBNDJaDqgqLUtMwKsEnRsbW1AGI cTBRZAAAA X-Change-ID: 20260402-cmpxchg-2fd307f44a41 To: Richard Henderson , Matt Turner , Magnus Lindholm , Vineet Gupta , Brian Cain , "James E.J. Bottomley" , Helge Deller , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , "Christophe Leroy (CS GROUP)" , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , "David S. Miller" , Andreas Larsson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Chris Zankel , Max Filippov Cc: linux-alpha@vger.kernel.org, linux-kernel@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-hexagon@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-riscv@lists.infradead.org, linux-sh@vger.kernel.org, sparclinux@vger.kernel.org, Jakub Kicinski , Hangbin Liu X-Mailer: b4 0.14.3 The old value passed to cmpxchg() is semantically read-only: it is only loaded into a register as a comparand and is never written back. However, the macro currently assigns it implicitly to a local variable of type __typeof__(*(ptr)), which triggers -Werror=3Ddiscarded-qualifiers when old is a const-qualified pointer and ptr points to a non-const type. To avoid this, let's add an explicit cast to __typeof__(*(ptr)) for the old local variable in the cmpxchg macros. This explicit cast suppresses the -Wdiscarded-qualifiers diagnostic. The new value is intentionally left without a cast: new will be stored into *ptr, so silently accepting a const-qualified new would allow callers to store a pointer-to-const into a non-const location without any compiler warning. Suggested-by: Jakub Kicinski Signed-off-by: Hangbin Liu --- This is my first time posting a patch with multi-architecture specific changes, so I'm not sure which target repository I should use. Please correct me if I made any mistakes. --- arch/alpha/include/asm/cmpxchg.h | 4 ++-- arch/arc/include/asm/cmpxchg.h | 4 ++-- arch/hexagon/include/asm/cmpxchg.h | 2 +- arch/parisc/include/asm/cmpxchg.h | 2 +- arch/powerpc/include/asm/cmpxchg.h | 8 ++++---- arch/riscv/include/asm/cmpxchg.h | 4 ++-- arch/sh/include/asm/cmpxchg.h | 2 +- arch/sparc/include/asm/cmpxchg_32.h | 2 +- arch/sparc/include/asm/cmpxchg_64.h | 2 +- arch/x86/include/asm/cmpxchg.h | 2 +- arch/xtensa/include/asm/cmpxchg.h | 2 +- tools/arch/x86/include/asm/cmpxchg.h | 2 +- 12 files changed, 18 insertions(+), 18 deletions(-) diff --git a/arch/alpha/include/asm/cmpxchg.h b/arch/alpha/include/asm/cmpx= chg.h index ae1b96479d0c..b4b8dac759c4 100644 --- a/arch/alpha/include/asm/cmpxchg.h +++ b/arch/alpha/include/asm/cmpxchg.h @@ -234,7 +234,7 @@ ____cmpxchg(volatile void *ptr, unsigned long old, unsi= gned long new, =20 #define arch_cmpxchg_local(ptr, o, n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) ____cmpxchg((ptr), (unsigned long)_o_, \ (unsigned long)_n_, \ @@ -265,7 +265,7 @@ ____cmpxchg(volatile void *ptr, unsigned long old, unsi= gned long new, #define arch_cmpxchg(ptr, o, n) \ ({ \ __typeof__(*(ptr)) __ret; \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ smp_mb(); \ __ret =3D (__typeof__(*(ptr))) ____cmpxchg((ptr), \ diff --git a/arch/arc/include/asm/cmpxchg.h b/arch/arc/include/asm/cmpxchg.h index 76f43db0890f..9637e2cdb5b1 100644 --- a/arch/arc/include/asm/cmpxchg.h +++ b/arch/arc/include/asm/cmpxchg.h @@ -42,7 +42,7 @@ #define arch_cmpxchg_relaxed(ptr, old, new) \ ({ \ __typeof__(ptr) _p_ =3D (ptr); \ - __typeof__(*(ptr)) _o_ =3D (old); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(old); \ __typeof__(*(ptr)) _n_ =3D (new); \ __typeof__(*(ptr)) _prev_; \ \ @@ -64,7 +64,7 @@ #define arch_cmpxchg(ptr, old, new) \ ({ \ volatile __typeof__(ptr) _p_ =3D (ptr); \ - __typeof__(*(ptr)) _o_ =3D (old); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(old); \ __typeof__(*(ptr)) _n_ =3D (new); \ __typeof__(*(ptr)) _prev_; \ unsigned long __flags; \ diff --git a/arch/hexagon/include/asm/cmpxchg.h b/arch/hexagon/include/asm/= cmpxchg.h index 9c58fb81f7fd..7e117964cb6b 100644 --- a/arch/hexagon/include/asm/cmpxchg.h +++ b/arch/hexagon/include/asm/cmpxchg.h @@ -54,7 +54,7 @@ __arch_xchg(unsigned long x, volatile void *ptr, int size) #define arch_cmpxchg(ptr, old, new) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(*(ptr)) __old =3D (old); \ + __typeof__(*(ptr)) __old =3D (__typeof__(*(ptr)))(old); \ __typeof__(*(ptr)) __new =3D (new); \ __typeof__(*(ptr)) __oldval =3D (__typeof__(*(ptr))) 0; \ \ diff --git a/arch/parisc/include/asm/cmpxchg.h b/arch/parisc/include/asm/cm= pxchg.h index bf0a0f1189eb..2a87942227f8 100644 --- a/arch/parisc/include/asm/cmpxchg.h +++ b/arch/parisc/include/asm/cmpxchg.h @@ -78,7 +78,7 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned= long new_, int size) =20 #define arch_cmpxchg(ptr, o, n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ (unsigned long)_n_, sizeof(*(ptr))); \ diff --git a/arch/powerpc/include/asm/cmpxchg.h b/arch/powerpc/include/asm/= cmpxchg.h index dbb50c06f0bf..36d113b13623 100644 --- a/arch/powerpc/include/asm/cmpxchg.h +++ b/arch/powerpc/include/asm/cmpxchg.h @@ -698,7 +698,7 @@ __cmpxchg_acquire(void *ptr, unsigned long old, unsigne= d long new, } #define arch_cmpxchg(ptr, o, n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ (unsigned long)_n_, sizeof(*(ptr))); \ @@ -707,7 +707,7 @@ __cmpxchg_acquire(void *ptr, unsigned long old, unsigne= d long new, =20 #define arch_cmpxchg_local(ptr, o, n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg_local((ptr), (unsigned long)_o_, \ (unsigned long)_n_, sizeof(*(ptr))); \ @@ -715,7 +715,7 @@ __cmpxchg_acquire(void *ptr, unsigned long old, unsigne= d long new, =20 #define arch_cmpxchg_relaxed(ptr, o, n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg_relaxed((ptr), \ (unsigned long)_o_, (unsigned long)_n_, \ @@ -724,7 +724,7 @@ __cmpxchg_acquire(void *ptr, unsigned long old, unsigne= d long new, =20 #define arch_cmpxchg_acquire(ptr, o, n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg_acquire((ptr), \ (unsigned long)_o_, (unsigned long)_n_, \ diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index 8712cf9c69dc..3026a9ff43c4 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -215,7 +215,7 @@ cas_prepend, cas_append) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(*(__ptr)) __old =3D (old); \ + __typeof__(*(__ptr)) __old =3D (__typeof__(*(__ptr)))(old); \ __typeof__(*(__ptr)) __new =3D (new); \ __typeof__(*(__ptr)) __ret; \ \ @@ -331,7 +331,7 @@ union __u128_halves { =20 #define __arch_cmpxchg128(p, o, n, cas_sfx) \ ({ \ - __typeof__(*(p)) __o =3D (o); = \ + __typeof__(*(p)) __o =3D (__typeof__(*(p)))(o); = \ union __u128_halves __hn =3D { .full =3D (n) }; \ union __u128_halves __ho =3D { .full =3D (__o) }; \ register unsigned long t1 asm ("t1") =3D __hn.low; \ diff --git a/arch/sh/include/asm/cmpxchg.h b/arch/sh/include/asm/cmpxchg.h index 1e5dc5ccf7bf..7fb9214d246f 100644 --- a/arch/sh/include/asm/cmpxchg.h +++ b/arch/sh/include/asm/cmpxchg.h @@ -68,7 +68,7 @@ static inline unsigned long __cmpxchg(volatile void * ptr= , unsigned long old, =20 #define arch_cmpxchg(ptr,o,n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ (unsigned long)_n_, sizeof(*(ptr))); \ diff --git a/arch/sparc/include/asm/cmpxchg_32.h b/arch/sparc/include/asm/c= mpxchg_32.h index 8c1a3ca34eeb..f733bd5b5d3b 100644 --- a/arch/sparc/include/asm/cmpxchg_32.h +++ b/arch/sparc/include/asm/cmpxchg_32.h @@ -55,7 +55,7 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned= long new_, int size) =20 #define arch_cmpxchg(ptr, o, n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ (unsigned long)_n_, sizeof(*(ptr))); \ diff --git a/arch/sparc/include/asm/cmpxchg_64.h b/arch/sparc/include/asm/c= mpxchg_64.h index 3de25262c411..4a9ccf80487a 100644 --- a/arch/sparc/include/asm/cmpxchg_64.h +++ b/arch/sparc/include/asm/cmpxchg_64.h @@ -170,7 +170,7 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsign= ed long new, int size) =20 #define arch_cmpxchg(ptr,o,n) \ ({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ + __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ (unsigned long)_n_, sizeof(*(ptr))); \ diff --git a/arch/x86/include/asm/cmpxchg.h b/arch/x86/include/asm/cmpxchg.h index a88b06f1c35e..a7984842c58a 100644 --- a/arch/x86/include/asm/cmpxchg.h +++ b/arch/x86/include/asm/cmpxchg.h @@ -85,7 +85,7 @@ extern void __add_wrong_size(void) #define __raw_cmpxchg(ptr, old, new, size, lock) \ ({ \ __typeof__(*(ptr)) __ret; \ - __typeof__(*(ptr)) __old =3D (old); \ + __typeof__(*(ptr)) __old =3D (__typeof__(*(ptr)))(old); \ __typeof__(*(ptr)) __new =3D (new); \ switch (size) { \ case __X86_CASE_B: \ diff --git a/arch/xtensa/include/asm/cmpxchg.h b/arch/xtensa/include/asm/cm= pxchg.h index b6db4838b175..0f68eddb6d2d 100644 --- a/arch/xtensa/include/asm/cmpxchg.h +++ b/arch/xtensa/include/asm/cmpxchg.h @@ -83,7 +83,7 @@ __cmpxchg(volatile void *ptr, unsigned long old, unsigned= long new, int size) } =20 #define arch_cmpxchg(ptr,o,n) \ - ({ __typeof__(*(ptr)) _o_ =3D (o); \ + ({ __typeof__(*(ptr)) _o_ =3D (__typeof__(*(ptr)))(o); \ __typeof__(*(ptr)) _n_ =3D (n); \ (__typeof__(*(ptr))) __cmpxchg((ptr), (unsigned long)_o_, \ (unsigned long)_n_, sizeof (*(ptr))); \ diff --git a/tools/arch/x86/include/asm/cmpxchg.h b/tools/arch/x86/include/= asm/cmpxchg.h index 0ed9ca2766ad..b71938db9422 100644 --- a/tools/arch/x86/include/asm/cmpxchg.h +++ b/tools/arch/x86/include/asm/cmpxchg.h @@ -35,7 +35,7 @@ extern void __cmpxchg_wrong_size(void) #define __raw_cmpxchg(ptr, old, new, size, lock) \ ({ \ __typeof__(*(ptr)) __ret; \ - __typeof__(*(ptr)) __old =3D (old); \ + __typeof__(*(ptr)) __old =3D (__typeof__(*(ptr)))(old); \ __typeof__(*(ptr)) __new =3D (new); \ switch (size) { \ case __X86_CASE_B: \ --- base-commit: 48b3cd69265f346f64b93064723492da46206e9b change-id: 20260402-cmpxchg-2fd307f44a41 Best regards, --=20 Hangbin Liu