Compiler Attributes: Add __access macro
Subsystems (2)
Marco Elver
maintainerPATCH 1/2 Compiler Attributes: Add __access macro
Add support for the __access__ attribute, which is supported since gcc
Quoted 1 line
= 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
--- include/linux/compiler_attributes.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
include/linux/compiler_attributes.h +12 -0
@@ -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 +/* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute */
Signature · 1 line
2.54.0.rc2.533.g4f5dca5207-goog
Marco Elver
maintainerPATCH 2/2 kcsan: Silence -Wmaybe-uninitialized when calling __kcsan_check_access()
Some subsystems enable -Wmaybe-uninitialized [1], which can trigger false positives when KCSAN is enabled. Specifically, passing an uninitialized variable to functions that instrument accesses (e.g., copy_from_user()) results in calls to __kcsan_check_access().
Because __kcsan_check_access() takes a const volatile void *ptr, GCC infers that the function may read the memory location, and thus warns if the passed variable is uninitialized.
However, KCSAN is a dynamic analysis tool for data race detection. While it does read the memory location to detect concurrent modifications, the "initialized'ness" of the memory location is irrelevant for its analysis.
Silence the warning by annotating __kcsan_check_access() with __access(none, 1). This tells the compiler that the pointer is not used to access the referenced object, avoiding the false positive.
This fixes warnings like:
| CC fs/ntfs3/file.o | In file included from include/asm-generic/rwonce.h:27, | from arch/arm64/include/asm/rwonce.h:81, | from include/linux/compiler.h:369, | from include/linux/array_size.h:5, | from include/linux/kernel.h:16, | from include/linux/backing-dev.h:12, | from fs/ntfs3/file.c:10: | In function 'instrument_copy_from_user_before', | inlined from '_inline_copy_from_user' at include/linux/uaccess.h:184:2, | inlined from 'copy_from_user' at include/linux/uaccess.h:221:9, | inlined from 'ntfs_ioctl_fitrim' at fs/ntfs3/file.c:77:6, | inlined from 'ntfs_ioctl' at fs/ntfs3/file.c:164:10: | include/linux/kcsan-checks.h:220:28: error: 'range' may be used uninitialized [-Werror=maybe-uninitialized] | 220 | #define kcsan_check_access __kcsan_check_access | | ^ | include/linux/kcsan-checks.h:311:9: note: in expansion of macro 'kcsan_check_access' | 311 | kcsan_check_access(ptr, size, KCSAN_ACCESS_WRITE) | | ^~~~~~~~~~~~~~~~~~ | include/linux/instrumented.h:147:9: note: in expansion of macro 'kcsan_check_write' | 147 | kcsan_check_write(to, n); | | ^~~~~~~~~~~~~~~~~ | include/linux/kcsan-checks.h: In function 'ntfs_ioctl': | include/linux/kcsan-checks.h:37:6: note: by argument 1 of type 'const volatile void *' to '__kcsan_check_access' declared here | 37 | void __kcsan_check_access(const volatile void *ptr, size_t size, int type); | | ^~~~~~~~~~~~~~~~~~~~ | fs/ntfs3/file.c:65:29: note: 'range' declared here | 65 | struct fstrim_range range; | | ^~~~~
--- include/linux/kcsan-checks.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kcsan-checks.h +1 -1
@@ -34,7 +34,7 @@* @size: size of access * @type: access type modifier */ -void __kcsan_check_access (const volatile void *ptr,size_t size,int type); +void __kcsan_check_access(const volatile void *ptr, size_t size, int type) __access(none, 1);/* * See definition of __tsan_atomic_signal_fence() in kernel/kcsan/core.c.
Signature · 1 line
2.54.0.rc2.533.g4f5dca5207-goog
Quoted 5 lines · Marco Elver
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 11 but not currently supported by clang.
+ * Optional: only supported since gcc >= 11
From a quick try in Compiler Explorer, it seems GCC recognizes it starting with GCC 10 rather than 11. It also seems to be in the docs. Is there a reason for the 11?
Apart from that, the addition looks OK:
Thanks!
Cheers,
Miguel
Marco Elver
maintainerQuoted 11 lines · Miguel Ojeda
Quoted 5 lines · Marco Elver
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 11 but not currently supported by clang.
+ * Optional: only supported since gcc >= 11
From a quick try in Compiler Explorer, it seems GCC recognizes it starting with GCC 10 rather than 11. It also seems to be in the docs. Is there a reason for the 11?
Oh, will fix. In the original report by Arnd we somehow concluded it was 11, but I guess I should double check such things. :-)
Quoted 3 lines
Apart from that, the addition looks OK:
Acked-by Miguel Ojeda <[email protected]>
Thanks!
Quoted 8 lines · Marco Elver
Quoted 6 lines · Miguel Ojeda
Quoted 3 lines · Marco Elver
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 11 but not currently supported by clang.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
+ * Optional: only supported since gcc >= 11
From a quick try in Compiler Explorer, it seems GCC recognizes it starting with GCC 10 rather than 11. It also seems to be in the docs. Is there a reason for the 11?
Oh, will fix. In the original report by Arnd we somehow concluded it was 11, but I guess I should double check such things. :-)
gcc-10 knows the 'access' attribute, but not the 'none' variant:
echo 'int f(void *p) __attribute__((access(none, 1)));' | /home/arnd/cross/arm64/gcc-10.5.0-nolibc/aarch64-linux/bin/aarch64-linux-gcc -xc - -o /dev/null -c <stdin>:1:1: error: attribute 'access' invalid mode 'none'; expected one of 'read_only', 'read_write', or 'write_only'
Arnd
Marco Elver
maintainerQuoted 16 lines · Arnd Bergmann
Quoted 14 lines · Marco Elver
Quoted 11 lines · Miguel Ojeda
Quoted 5 lines · Marco Elver
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 11 but not currently supported by clang.
+ * Optional: only supported since gcc >= 11
From a quick try in Compiler Explorer, it seems GCC recognizes it starting with GCC 10 rather than 11. It also seems to be in the docs. Is there a reason for the 11?
Quoted 7 lines
Quoted 2 lines
Oh, will fix. In the original report by Arnd we somehow concluded it was 11, but I guess I should double check such things. :-)
gcc-10 knows the 'access' attribute, but not the 'none' variant:
echo 'int
f(void *p)__attribute__((access(none, 1)));' |/home/arnd/cross/arm64/gcc-10.5.0-nolibc/aarch64-linux/bin/aarch64-linux-gcc-xc - -o/dev/null-c <stdin>:1:1: error: attribute 'access' invalid mode 'none'; expected one of 'read_only', 'read_write', or 'write_only'
Good to know. KCSAN requires GCC 11+, so this is fine.
Quoted 5 lines · Arnd Bergmann
gcc-10 knows the 'access' attribute, but not the 'none' variant:
echo 'int
f(void *p)__attribute__((access(none, 1)));' |/home/arnd/cross/arm64/gcc-10.5.0-nolibc/aarch64-linux/bin/aarch64-linux-gcc-xc - -o/dev/null-c <stdin>:1:1: error: attribute 'access' invalid mode 'none'; expected one of 'read_only', 'read_write', or 'write_only'
Which is exactly the one we use in #2, indeed.
That makes sense, thanks!
We should still change the number here, because __has_attribute will pass even with GCC 10, i.e. since someone using none without a gate in Kconfig or elsewhere may not realize the mistake.
So we should probably add a comment that the access mode none is only available in GCC 11 -- especially so if we add the docs David suggests, because otherwise it seems like it is supported.
Cheers,
Miguel
Quoted 5 lines · Miguel Ojeda
Which is exactly the one we use in #2, indeed.
That makes sense, thanks!
Quoted 7 lines
We should still change the number here, because
__has_attributewill pass even with GCC 10, i.e. since someone usingnonewithout a gate inKconfigor elsewhere may not realize the mistake.So we should probably add a comment that the access mode
noneis only available in GCC 11 -- especially so if we add the docs David suggests, because otherwise it seems like it is supported.
Either that or we simply move it to compiler-gcc.h or similar with a GCC_VERSION check instead.
...which given what Marco is saying in a sibling comment, may be needed anyway.
Cheers,
Miguel
Quoted 11 lines · On Wed, 22 Apr 2026 12:25:26 +0200 Miguel Ojeda…
Quoted 5 lines · Arnd Bergmann
gcc-10 knows the 'access' attribute, but not the 'none' variant:
echo 'int
f(void *p)__attribute__((access(none, 1)));' |/home/arnd/cross/arm64/gcc-10.5.0-nolibc/aarch64-linux/bin/aarch64-linux-gcc-xc - -o/dev/null-c <stdin>:1:1: error: attribute 'access' invalid mode 'none'; expected one of 'read_only', 'read_write', or 'write_only'Which is exactly the one we use in #2, indeed.
That makes sense, thanks!
Quoted 7 lines
We should still change the number here, because
__has_attributewill pass even with GCC 10, i.e. since someone usingnonewithout a gate inKconfigor elsewhere may not realize the mistake.So we should probably add a comment that the access mode
noneis only available in GCC 11 -- especially so if we add the docs David suggests, because otherwise it seems like it is supported.
Especially since the gcc docs make it pretty impossible to find out when anything was added. Trial and error on godbolt shouldn't really be needed.
(and I missed that the size/count is the argument number as well).
David
Quoted 4 lines
Cheers,
Miguel
Quoted 14 lines · Marco Elver
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 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."
Quoted 3 lines
[1] https://gcc.gnu.org/onlinedocs/gcc/Common-Attributes.html#index-access
Signed-off-by Marco Elver <[email protected]>
Quoted 30 lines
--- include/linux/compiler_attributes.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)include/linux/compiler_attributes.h +12
-0@@ -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 +/* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute */ Signature · 1 line
2.54.0.rc2.533.g4f5dca5207-goog
Quoted 30 lines · On Tue, 21 Apr 2026 21:03:47 +0200 Marco Elver <[email protected]> wrote:
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 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 <[email protected]>--- include/linux/compiler_attributes.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)include/linux/compiler_attributes.h +1
-0@@ -20,6 +20,18 @@* Provide links to the documentation of each supported compiler, if it exists. */ +/*
include/linux/compiler_attributes.h line 33
Quoted 10 lines
include/linux/compiler_attributes.h +10
-0+* 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])
David
Quoted 4 lines
include/linux/compiler_attributes.h +1
-0+/* * gcc: https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#index-alias-function-attribute */
Marco Elver
maintainerQuoted 46 lines · David Laight
Quoted 30 lines · On Tue, 21 Apr 2026 21:03:47 +0200 Marco Elver <[email protected]> wrote:
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 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 <[email protected]>--- include/linux/compiler_attributes.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)include/linux/compiler_attributes.h +1
-0@@ -20,6 +20,18 @@* Provide links to the documentation of each supported compiler, if it exists. */ +/* include/linux/compiler_attributes.h line 33
Quoted 10 lines
include/linux/compiler_attributes.h +10
-0+* 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:
Quoted 4 lines
# 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/[email protected]/
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?
Marco Elver
maintainerQuoted 55 lines · Marco Elver
Quoted 46 lines · David Laight
Quoted 30 lines · On Tue, 21 Apr 2026 21:03:47 +0200 Marco Elver <[email protected]> wrote:
Add support for the
__access__attribute, which is supported since gccQuoted 1 line
= 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 <[email protected]>--- include/linux/compiler_attributes.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)include/linux/compiler_attributes.h +1
-0@@ -20,6 +20,18 @@* Provide links to the documentation of each supported compiler, if it exists. */ +/* include/linux/compiler_attributes.h line 33
Quoted 10 lines
include/linux/compiler_attributes.h +10
-0+* 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:
Quoted 4 lines
# 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.
Quoted 7 lines
Arnd reports odd behaviour on gcc-11:
https://lore.kernel.org/all/[email protected]/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/[email protected]/
Quoted 3 lines · David Laight
Putting the actual syntax in the comment would help, eg as:
__access__(read_only|read_write|write_only|none, param_number[, size])
I guess you mean as a quick reminder for those that already know about the attribute?
That sounds OK for an attribute like this, though generally speaking I would avoid duplicating the compiler docs. (Hopefully people will still read the actual docs... :)
By the way, that could be __access, i.e. documenting our side, i.e. the macro.
Cheers,
Miguel
Quoted 6 lines · On Wed, 22 Apr 2026 12:25:32 +0200 Miguel Ojeda…
Quoted 3 lines · David Laight
Putting the actual syntax in the comment would help, eg as:
__access__(read_only|read_write|write_only|none, param_number[, size])
Quoted 6 lines
I guess you mean as a quick reminder for those that already know about the attribute?
That sounds OK for an attribute like this, though generally speaking I would avoid duplicating the compiler docs. (Hopefully people will still read the actual docs... :)
It might save someone who is just 'wondering what it is about' from having to read the gcc docs - particularly if reading the file where you don't have a browser handy, or in an editor that doesn't support following links.
I also had to read a lot of the 'actual doc' to get a hint of what it all meant.
Quoted 1 line
By the way, that could be
__access, i.e. documenting our side, i.e. the macro.
True, and ', size' should be ', count'.
David
Quoted 3 lines
Cheers,
Miguel