Re: [PATCH 1/2] Compiler Attributes: Add __access macro

From: Marco Elver

Date: Wed Apr 22 2026 - 09:11:52 EST


On Wed, 22 Apr 2026 at 12:20, Marco Elver <elver@xxxxxxxxxx> wrote:
>
> On Wed, 22 Apr 2026 at 12:01, David Laight <david.laight.linux@xxxxxxxxx> wrote:
> >
> > On Tue, 21 Apr 2026 21:03:47 +0200
> > Marco Elver <elver@xxxxxxxxxx> wrote:
> >
> > > Add support for the `__access__` attribute, which is supported since gcc
> > > >= 11 but not currently supported by clang.
> > >
> > > The attribute allows specifying how a function accesses memory passed
> > > via a pointer argument (read_only, write_only, read_write, none) and
> > > optionally the size of the access. Per [1] these annotations only affect
> > > diagnostics, and should not affect code generation:
> > >
> > > "The access attribute enables the detection of invalid or unsafe
> > > accesses by functions or their callers, as well as write-only
> > > accesses to objects that are never read from. Such accesses may
> > > be diagnosed by warnings such as -Wstringop-overflow,
> > > -Wuninitialized, -Wunused, and others."
> > >
> > > [1] https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-access
> > >
> > > Signed-off-by: Marco Elver <elver@xxxxxxxxxx>
> > > ---
> > > include/linux/compiler_attributes.h | 12 ++++++++++++
> > > 1 file changed, 12 insertions(+)
> > >
> > > diff --git a/include/linux/compiler_attributes.h b/include/linux/compiler_attributes.h
> > > index c16d4199bf92..ef4e279e9872 100644
> > > --- a/include/linux/compiler_attributes.h
> > > +++ b/include/linux/compiler_attributes.h
> > > @@ -20,6 +20,18 @@
> > > * Provide links to the documentation of each supported compiler, if it exists.
> > > */
> > >
> > > +/*
> > > + * Optional: only supported since gcc >= 11
> > > + * Optional: not supported by clang
> > > + *
> > > + * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-access
> > > + */
> > > +#if __has_attribute(__access__)
> > > +# define __access(x, ...) __attribute__((__access__(x, ## __VA_ARGS__)))
> > > +#else
> > > +# define __access(x, ...)
> > > +#endif
> >
> > No need to the initial 'x', you can just do:
> > # define __access(...) __attribute__((__access__(__VA_ARGS__)))
> >
> > Putting the actual syntax in the comment would help, eg as:
> > __access__(read_only|read_write|write_only|none, param_number[, size])
>
> Will add.
>
> Arnd reports odd behaviour on gcc-11:
> https://lore.kernel.org/all/4b36f629-9338-40e0-b114-ee37aaad9cf3@xxxxxxxxxxxxxxxx/
>
> The documentation says "When the optional size-index argument is
> omitted for an argument of void* type, the actual pointer argument is
> ignored." - but gcc 11 seems to not ignore it. Is it a known bug with
> gcc 11? I.e. do we need to add a version check?

FWIW, I found that even later GCC seem to complain when using access
'none' - https://godbolt.org/z/jGGG97MW3

I'm not sure if that's intended or not - either that, or the GCC docs
are misleading me here. To avoid this footgun, I'm going to drop this
patch and found a simpler solution using absolute_pointer():
https://lore.kernel.org/all/20260422125256.87513-1-elver@xxxxxxxxxx/