From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 089DC3A3E74 for ; Thu, 2 Apr 2026 08:57:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.43 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775120274; cv=none; b=bZEKcEgNRD6kXX6Bi1eAKoszLorBk8BTq1n5hDdkEztiQ+KqPoKqNlhe80n1jkyeW3E8cfXPssVvo5DHYWn08LdmgBK5lGPYknWkgKzJZSDsy/G/u+v9UCLA3F7B2nG3DuGUzQ92zIrW7H2QsWZEf1/1fiYtRSI+/c67bMVVJt8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775120274; c=relaxed/simple; bh=7tyJ0+wz5LzsOmdaY0Yimnd2RzEPrZZv3nZHEii7oEs=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=m6FrOcUzGDS8+w0J59q7OEME5qknuRJDkAjHjx4UYYaLsGkMBoU2XXdgXnBmRZ7vyiNVGyIn78ZZk9pT8+JdhZVzcRhdGb+BDgl8i0vY2W8OdiQ2Q4TP99bRzm8B61ZIBsYfDfVrKGTb2lCZsP6GWX5DixbaQDlLc4ScLiX/56s= 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=N43yz0Pb; arc=none smtp.client-ip=209.85.128.43 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="N43yz0Pb" Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-487012ce896so3116195e9.0 for ; Thu, 02 Apr 2026 01:57:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1775120270; x=1775725070; 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=LnQTQFMkaspyfTBlP7NJBNzFuL146lWNPT7LvqeL2XU=; b=N43yz0PbY37tKQwLI7fxz/TezjohnBPHceaTDSIHXJZ3lR+l7erEdzyuYI3/5fRC/P aKbwZiXH/cRosXudrYOUKNUT8TPbaLVOs6PeIW/HuQH5TsnCf4Fm5Sgo24YdKsa6ii/U /fju0wbiv5lfrFjides8gma93qJvGrmo2JqMyd4NJj0a7E9wY3w+nIAyjyJTMEfvk7PG g3zBjCmG4+hbFTu/luNZzrq8m7P9/XsoiMeRQT/rmz73FwKb6JnSjpaUqV0ZmOFNtuUi vKFA8PaRd5wgturtCUT32KaTz75xIUFFg4zOpfG2jjDwyipSacuYVDVzYgsXBFUpnDzh LkIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775120270; x=1775725070; 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=LnQTQFMkaspyfTBlP7NJBNzFuL146lWNPT7LvqeL2XU=; b=pfuy11n2Cv7Mi1Y0Q3LHlmoyUuwbgM9sf8LcnQTsqkAG3mPKnkgRYI3rDstbkVg2F8 II/yvz7ZcStn+MOUqjR6K5LhgAKP2rjgiEe6vkGrsAdoS2oi7QQAnjIUwZStNCZpSilv j+Bvt+zRF5Ikewz2kROzujHZs3q7iSatbAuG0Oor13iOe+oiapP/9L99+uW2U/d9BSfL Znbgw1zwoz7ZdYPGzwSoQKFBQKf1N7vcUDH9kHVqlvdh21Smz9CNNfwbck5KoJVGzeP0 AjthsMlXMqQIAaTZwITwHZgZhpPvv5XdAkt2/GnrUBoM5f6TSY3j3nYwZ1hbMtX1ptZ8 dudA== X-Forwarded-Encrypted: i=1; AJvYcCX9RjkqT0+DIMBLEXrMYJAvTKX4WE2Bf0L2xnC8FYBhwebqZ65lg00t8rofT8xheleOPJTl0k53GoDdVTo=@vger.kernel.org X-Gm-Message-State: AOJu0YxLFS+9uHekgvkuRSkM0+85X8fWEO+ybRBokM7XBydRiJOFJiJ1 2Ew2g4M08VOSmcDhlbUvBfZR7f7UwrkV6307G11s1s3oyM1yxJsPr3ao X-Gm-Gg: ATEYQzwwBjiMAN8+Gf19aKtiGP4Z5zGDVXxk+Gz//olGBzBQFdXrLhM6NctQHb6Rpb9 mM8eukYUoraFBlYV5Lk8xvW+C/YLE+gBydI/Voj/Nc2L09SLrgWQJpzjocldPL41LyVJSzepXZu otNdVfsebJtgmiawP0JGST9P3hcKI48k3yZRJpe+/usPtsYuqF8gn/FD77kMdTvlAg9lpR3dzQ+ xMHn5uR5E9iVvdmI/bBtnIczn/oqo7h3dFn98rkh8cohM8PCjqiC5hITfKZSmnnzZTIzPNNqfsB eCuOb+6b4VpMhdWzldhtVeRbZfWdnnbGnbK60vCXaNiYgc8ipRiw1qSfoi4ENmhHuzLR0DPL3d7 6F14nsO2rXt7KZaLKl9d6phy1yXjifhK/JoSkF42vRiELl7D+PU95HNPHSh1NeVvs+Eg/2MpPao IjCDviNc0pWMH66ju4B9Js4NE1QlKUs3Ax0wbNDYky6XcqlqfSZbTxGRoxvIKH X-Received: by 2002:a05:600c:630f:b0:487:288:1198 with SMTP id 5b1f17b1804b1-4888359da58mr102555095e9.22.1775120270131; Thu, 02 Apr 2026 01:57:50 -0700 (PDT) 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-4887e735532sm217845265e9.0.2026.04.02.01.57.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Apr 2026 01:57:49 -0700 (PDT) Date: Thu, 2 Apr 2026 09:57:47 +0100 From: David Laight To: Hangbin Liu Cc: 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 , 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 Subject: Re: [PATCH] cmpxchg: allow const-qualified old value in cmpxchg() Message-ID: <20260402095747.6efbd456@pumpkin> In-Reply-To: <20260402-cmpxchg-v1-1-4561e2c05d2c@gmail.com> References: <20260402-cmpxchg-v1-1-4561e2c05d2c@gmail.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, 02 Apr 2026 14:56:01 +0800 Hangbin Liu wrote: > 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=discarded-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 ... > > diff --git a/arch/alpha/include/asm/cmpxchg.h b/arch/alpha/include/asm/cmpxchg.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, unsigned long new, > > #define arch_cmpxchg_local(ptr, o, n) \ > ({ \ > - __typeof__(*(ptr)) _o_ = (o); \ > + __typeof__(*(ptr)) _o_ = (__typeof__(*(ptr)))(o); \ > __typeof__(*(ptr)) _n_ = (n); \ > (__typeof__(*(ptr))) ____cmpxchg((ptr), (unsigned long)_o_, \ > (unsigned long)_n_, \ That looks like it loses the check that 'o' has the same type as '*ptr'. Maybe this works? auto _o_ = 1 ? (o) : *(ptr); David