Re: [PATCH] cmpxchg: allow const-qualified old value in cmpxchg()
Peter Zijlstra <[email protected]> Thu, 2 Apr 2026 12:47:33 +0200
| Newsgroups | org.kernel.vger.linux-hexagon,org.infradead.lists.linux-riscv,org.infradead.lists.linux-snps-arc,org.kernel.vger.linux-alpha,org.kernel.vger.linux-kernel,org.kernel.vger.linux-parisc,org.kernel.vger.linux-sh,org.kernel.vger.sparclinux,org.ozlabs.lists.linuxppc-dev |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Apr 02, 2026 at 02:56:01PM +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 <[email protected]> > Signed-off-by: Hangbin Liu <[email protected]> > --- Where is this a problem? I've never seen this. Also, new code should probably use try_cmpxchg() anyway.