compiler_types: Allow opting out of __counted_by and __counted_by_ptr
PATCH 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the counted_by annotations: the bounds checks they feed (FORTIFY_SOURCE, UBSAN_BOUNDS) are already disabled there.
The annotations can also break the build. A __counted_by_ptr() that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-level Makefile adds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.
Overriding the macros from a Makefile doesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' or '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op. The two are kept separate but parallel so they can be folded together once all supported compilers handle '__counted_by' on pointers.
--- include/linux/compiler_types.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
include/linux/compiler_types.h +13 -2
@@ -371,8 +371,14 @@ struct ftrace_likely_data {* * __bdos on clang < 19.1.3 can be off by 4: * https://github.com/llvm/llvm-project/pull/112636 +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY to drop the annotation, since it gains nothing from the +* bounds checks. Kept in step with __NO_COUNTED_BY_PTR below so the two can +* be folded together once __counted_by covers pointers on all supported +* compilers. */ -#ifdef CONFIG_CC_HAS_COUNTED_BY +#if defined(CONFIG_CC_HAS_COUNTED_BY ) && !defined(__NO_COUNTED_BY)# define __counted_by (member) __attribute__((__counted_by__(member)))#else # define __counted_by (member)
@@ -387,8 +393,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY_PTR to drop the annotation, as with __NO_COUNTED_BY. Such +* code may also lack the flag Clang needs to parse a reference to a +* later-declared member (-fexperimental-late-parse-attributes). */ -#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR ) && !defined(__NO_COUNTED_BY_PTR)#define __counted_by_ptr (member) __attribute__((__counted_by__(member)))#else #define __counted_by_ptr (member)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Hi,
Quoted 64 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Overriding the macros from a
Makefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' or '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op. The two are kept separate but parallel so they can be folded together once all supported compilers handle '__counted_by' on pointers.Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>--- include/linux/compiler_types.h | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)include/linux/compiler_types.h +13
-2@@ -371,8 +371,14 @@ struct ftrace_likely_data {* * __bdos on clang < 19.1.3 can be off by 4: * https://github.com/llvm/llvm-project/pull/112636 +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY to drop the annotation, since it gains nothing from the +* bounds checks. Kept in step with __NO_COUNTED_BY_PTR below so the two can +* be folded together once __counted_by covers pointers on all supported +* compilers. */ -#ifdef CONFIG_CC_HAS_COUNTED_BY +#if defined(CONFIG_CC_HAS_COUNTED_BY ) && !defined(__NO_COUNTED_BY)# define __counted_by (member) __attribute__((__counted_by__(member)))#else # define __counted_by (member)@@ -387,8 +393,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY_PTR to drop the annotation, as with __NO_COUNTED_BY. Such +* code may also lack the flag Clang needs to parse a reference to a +* later-declared member (-fexperimental-late-parse-attributes). */ -#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR ) && !defined(__NO_COUNTED_BY_PTR)#define __counted_by_ptr (member) __attribute__((__counted_by__(member)))#else #define __counted_by_ptr (member)Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Thanks
Justin
Hi Bill,
Quoted 11 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.
Quoted 9 lines
Overriding the macros from a
Makefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' or '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op. The two are kept separate but parallel so they can be folded together once all supported compilers handle '__counted_by' on pointers.
I'd prefer a single macro here - if there is ever a case where we need to turn off one but not the other, we can revisit.
Otherwise, this looks good to me - thanks.
Quoted 25 lines · Ard Biesheuvel
Hi Bill,
Quoted 11 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Quoted 9 lines
Overriding the macros from a
Makefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' or '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op. The two are kept separate but parallel so they can be folded together once all supported compilers handle '__counted_by' on pointers.
Quoted 4 lines
I'd prefer a single macro here - if there is ever a case where we need to turn off one but not the other, we can revisit.
Otherwise, this looks good to me - thanks.
Hi Ard,
I wanted to do it this way because the two attributes rely upon compiler versions, and this gives us more flexibility. Eventually, there will be Only One(tm), once the minimal compiler version supports both __counted_by and __counted_by_ptr.
But it's a small issue. I can resend with just the __NO_COUNTED_BY_PTR part.
-bw
PATCH 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr
In reply to Bill WendlingCode that runs outside the kernel proper, such as the EFI stub, gets nothing out of the counted_by annotations: the bounds checks they feed (FORTIFY_SOURCE, UBSAN_BOUNDS) are already disabled there.
The annotations can also break the build. A __counted_by_ptr() that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-level Makefile adds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.
Overriding the __counted_by_ptr macro from a Makefile doesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op.
---
v2: Only specify __NO_COUNTED_BY_PTR. Leave __NO_COUNTED_BY until we need it.
--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +6 -1
@@ -387,8 +387,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY_PTR to drop the annotation. Such code may also lack the flag +* Clang needs to parse a reference to a later-declared member +* (-fexperimental-late-parse-attributes). */ -#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR ) && !defined(__NO_COUNTED_BY_PTR)#define __counted_by_ptr (member) __attribute__((__counted_by__(member)))#else #define __counted_by_ptr (member)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
PATCH v3 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr
In reply to Bill WendlingCode that runs outside the kernel proper, such as the EFI stub, gets nothing out of the counted_by annotations: the bounds checks they feed (FORTIFY_SOURCE, UBSAN_BOUNDS) are already disabled there.
The annotations can also break the build. A __counted_by_ptr() that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-level Makefile adds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.
Overriding the __counted_by_ptr macro from a Makefile doesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op.
---
v2: Only specify __NO_COUNTED_BY_PTR. Leave __NO_COUNTED_BY until we need it.
v3: Add the version to the Subject line.
--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +6 -1
@@ -387,8 +387,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY_PTR to drop the annotation. Such code may also lack the flag +* Clang needs to parse a reference to a later-declared member +* (-fexperimental-late-parse-attributes). */ -#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR ) && !defined(__NO_COUNTED_BY_PTR)#define __counted_by_ptr (member) __attribute__((__counted_by__(member)))#else #define __counted_by_ptr (member)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Quoted 47 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Overriding the
__counted_by_ptrmacro from aMakefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op.Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>---
v2: Only specify__NO_COUNTED_BY_PTR. Leave__NO_COUNTED_BYuntil we need it.
v3: Add the version to the Subject line.--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)include/linux/compiler_types.h +0
-0@@ -387,8 +387,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html
* clang:
https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
+ *
+ * Code that runs outside the kernel proper (e.g. the EFI stub) can define
+ *__NO_COUNTED_BY_PTRto drop the annotation. Such code may also lack the flag
+ * Clang needs to parse a reference to a later-declared member
+ * (-fexperimental-late-parse-attributes).
*/include/linux/compiler_types.h +1
-1-#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR) &&
Quoted 5 lines
!defined(__NO_COUNTED_BY_PTR)
#define__counted_by_ptr(member)__attribute__((__counted_by__(member)))
#else
#define__counted_by_ptr(member)
Apologies, I may have been unclear.
What I would like to see here is something like
#ifndef __NO_COUNTED_BY
#define __counted_by(member) __attribute__((....)) #define __counted_by_ptr(member) __attribute__((....)) #else
#define __counted_by(member)
#define __counted_by_ptr(member)
#endif
Hi,
Quoted 61 lines · Ard Biesheuvel
Quoted 47 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Overriding the
__counted_by_ptrmacro from aMakefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op.Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>---
v2: Only specify__NO_COUNTED_BY_PTR. Leave__NO_COUNTED_BYuntil we need it.
v3: Add the version to the Subject line.--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)include/linux/compiler_types.h +0
-0@@ -387,8 +387,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html
* clang:
https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
+ *
+ * Code that runs outside the kernel proper (e.g. the EFI stub) can define
+ *__NO_COUNTED_BY_PTRto drop the annotation. Such code may also lack the flag
+ * Clang needs to parse a reference to a later-declared member
+ * (-fexperimental-late-parse-attributes).
*/include/linux/compiler_types.h +1
-1-#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR) &&Quoted 5 lines
!defined(__NO_COUNTED_BY_PTR)
#define__counted_by_ptr(member)__attribute__((__counted_by__(member)))
#else
#define__counted_by_ptr(member)Apologies, I may have been unclear.
What I would like to see here is something like
Quoted 7 lines
#ifndef
__NO_COUNTED_BY
#define__counted_by(member)__attribute__((....)) #define__counted_by_ptr(member)__attribute__((....)) #else
#define__counted_by(member)
#define__counted_by_ptr(member)
#endif
FWIW, Bill's original patch had this explanation
"The two are kept separate but parallel so they can be folded together once all supported compilers handle '__counted_by' on pointers."
Justin
Quoted 72 lines · Justin Stitt
Hi,
Quoted 61 lines · Ard Biesheuvel
Quoted 47 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Overriding the
__counted_by_ptrmacro from aMakefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op.Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>---
v2: Only specify__NO_COUNTED_BY_PTR. Leave__NO_COUNTED_BYuntil we need it.
v3: Add the version to the Subject line.--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)include/linux/compiler_types.h +0
-0@@ -387,8 +387,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html
* clang:
https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
+ *
+ * Code that runs outside the kernel proper (e.g. the EFI stub) can define
+ *__NO_COUNTED_BY_PTRto drop the annotation. Such code may also lack the flag
+ * Clang needs to parse a reference to a later-declared member
+ * (-fexperimental-late-parse-attributes).
*/include/linux/compiler_types.h +1
-1-#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR) &&Quoted 5 lines
!defined(__NO_COUNTED_BY_PTR)
#define__counted_by_ptr(member)__attribute__((__counted_by__(member)))
#else
#define__counted_by_ptr(member)Apologies, I may have been unclear.
What I would like to see here is something like
Quoted 7 lines
#ifndef
__NO_COUNTED_BY
#define__counted_by(member)__attribute__((....)) #define__counted_by_ptr(member)__attribute__((....)) #else
#define__counted_by(member)
#define__counted_by_ptr(member)
#endif
Quoted 6 lines
FWIW, Bill's original patch had this explanation
"The two are kept separate but parallel so they can be folded together once all supported compilers handle '
__counted_by' on pointers."
When opting out of this, whether or not the compiler supports all variants of counted_by() is irrelevant. The same #define should just opt out of all of them.
If the need arises to be more granular here, we can always add that later but I don't want to have to add
-D__NO_COUNTED_BY -D__NO_COUNTED_BY_PTR
everywhere today, and go back and remove the second part once all compilers have caught up.
Quoted 84 lines · Ard Biesheuvel
Quoted 72 lines · Justin Stitt
Hi,
Quoted 61 lines · Ard Biesheuvel
Quoted 47 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Overriding the
__counted_by_ptrmacro from aMakefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY_PTR' to turn the corresponding annotation into a no-op.Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>---
v2: Only specify__NO_COUNTED_BY_PTR. Leave__NO_COUNTED_BYuntil we need it.
v3: Add the version to the Subject line.--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)include/linux/compiler_types.h +0
-0@@ -387,8 +387,13 @@ struct ftrace_likely_data {* * gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html
* clang:
https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null
+ *
+ * Code that runs outside the kernel proper (e.g. the EFI stub) can define
+ *__NO_COUNTED_BY_PTRto drop the annotation. Such code may also lack the flag
+ * Clang needs to parse a reference to a later-declared member
+ * (-fexperimental-late-parse-attributes).
*/include/linux/compiler_types.h +1
-1-#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR) &&Quoted 5 lines
!defined(__NO_COUNTED_BY_PTR)
#define__counted_by_ptr(member)__attribute__((__counted_by__(member)))
#else
#define__counted_by_ptr(member)Apologies, I may have been unclear.
What I would like to see here is something like
Quoted 7 lines
#ifndef
__NO_COUNTED_BY
#define__counted_by(member)__attribute__((....)) #define__counted_by_ptr(member)__attribute__((....)) #else
#define__counted_by(member)
#define__counted_by_ptr(member)
#endifQuoted 6 lines
FWIW, Bill's original patch had this explanation
"The two are kept separate but parallel so they can be folded together once all supported compilers handle '
__counted_by' on pointers."When opting out of this, whether or not the compiler supports all variants of
counted_by()is irrelevant. The same #define should just opt out of all of them.
Quoted 8 lines
If the need arises to be more granular here, we can always add that later but I don't want to have to add
-D__NO_COUNTED_BY -D__NO_COUNTED_BY_PTR
everywhere today, and go back and remove the second part once all compilers have caught up.
It would reduce churn in the code base once we no longer need a separate '__counted_by_ptr' macro. However, we should specify that __NO_COUNTED_BY should be used sparingly, as there are other, better ways to "opt out" of the __counted_by family of attributes. Basically, it should only be used if the Makefile doesn't inherit its KCFLAGS from the root Makefile.
-bw
PATCH v4 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr
In reply to Bill WendlingCode that runs outside the kernel proper, such as the EFI stub, gets nothing out of the counted_by annotations: the bounds checks they feed (FORTIFY_SOURCE, UBSAN_BOUNDS) are already disabled there.
The annotations can also break the build. A __counted_by_ptr() that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-level Makefile adds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.
Overriding the macros from a Makefile doesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' to turn the corresponding annotation into a no-op.
---
v2: Only specify __NO_COUNTED_BY_PTR. Leave __NO_COUNTED_BY until we need it.
v3: Add the version to the Subject line.
v4: Use __NO_COUNTED_BY to reduce churn later on.
--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
include/linux/compiler_types.h +6 -1
@@ -371,8 +371,13 @@ struct ftrace_likely_data {* * __bdos on clang < 19.1.3 can be off by 4: * https://github.com/llvm/llvm-project/pull/112636 +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY to drop the annotation, since it gains nothing from the +* bounds checks. __NO_COUNTED_BY should be used sparingly, because there are +* better options for opting out of bounds checking. */ -#ifdef CONFIG_CC_HAS_COUNTED_BY +#if defined(CONFIG_CC_HAS_COUNTED_BY ) && !defined(__NO_COUNTED_BY)# define __counted_by (member) __attribute__((__counted_by__(member)))#else # define __counted_by (member)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Quoted 36 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Overriding the macros from a
Makefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' to turn the corresponding annotation into a no-op.Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>---
v2: Only specify__NO_COUNTED_BY_PTR. Leave__NO_COUNTED_BYuntil we need it.
v3: Add the version to the Subject line.
v4: Use__NO_COUNTED_BYto reduce churn later on.--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)include/linux/compiler_types.h +1
-0@@ -371,8 +371,13 @@ struct ftrace_likely_data {* * __bdos on clang < 19.1.3 can be off by 4: * https://github.com/llvm/llvm-project/pull/112636 +*
include/linux/compiler_types.h struct ftrace_likely_data { · line 383
Quoted 10 lines
include/linux/compiler_types.h +5
-1+* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY to drop the annotation, since it gains nothing from the +* bounds checks. __NO_COUNTED_BY should be used sparingly, because there are +* better options for opting out of bounds checking. */ -#ifdef CONFIG_CC_HAS_COUNTED_BY +#if defined(CONFIG_CC_HAS_COUNTED_BY ) && !defined(__NO_COUNTED_BY)# define __counted_by (member) __attribute__((__counted_by__(member)))#else # define __counted_by (member)
What happened to counted_by_ptr() now? __NO_COUNTED_BY should disable that too.
Quoted 45 lines · Ard Biesheuvel
Quoted 41 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.Overriding the macros from a
Makefiledoesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' to turn the corresponding annotation into a no-op.Assisted-by LLMSigned-off-by Bill Wendling <[email protected]>---
v2: Only specify__NO_COUNTED_BY_PTR. Leave__NO_COUNTED_BYuntil we need it.
v3: Add the version to the Subject line.
v4: Use__NO_COUNTED_BYto reduce churn later on.--- include/linux/compiler_types.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)include/linux/compiler_types.h +5
-0@@ -371,8 +371,13 @@ struct ftrace_likely_data {* * __bdos on clang < 19.1.3 can be off by 4: * https://github.com/llvm/llvm-project/pull/112636 +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY to drop the annotation, since it gains nothing from the +* bounds checks. __NO_COUNTED_BY should be used sparingly, because there are +* better options for opting out of bounds checking. */
Quoted 9 lines
include/linux/compiler_types.h struct ftrace_likely_data { · line 383
Quoted 5 lines
include/linux/compiler_types.h +1
-1-#ifdef CONFIG_CC_HAS_COUNTED_BY +#if defined(CONFIG_CC_HAS_COUNTED_BY ) && !defined(__NO_COUNTED_BY)# define __counted_by (member) __attribute__((__counted_by__(member)))#else # define __counted_by (member)What happened to
counted_by_ptr()now?__NO_COUNTED_BYshould disable that too.
I realized that just now. *sigh* I'd take up basketweaving, but I'd probably get splinters that would fester and I'd lose fingers.
Fixed in v5.
-bw
Quoted 10 lines · Bill Wendling
Code that runs outside the kernel proper, such as the EFI stub, gets nothing out of the
counted_byannotations: the bounds checks they feed (FORTIFY_SOURCE,UBSAN_BOUNDS) are already disabled there.The annotations can also break the build. A
__counted_by_ptr()that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-levelMakefileadds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.
We have a distinction between parsing and output (instrumentation, linkage, etc). FORTIFY_SOURCE and UBSAN_BOUNDS (and lots of other things) control output (e.g. fortify alternative linkages, ubsan instrumentation). We don't normally have parsing issues, as that kind of thing is usually controlled by compiler flag options (like here). For example, if a transparent struct ever leaked into a header that libstub includes, we would explode as well, due to x86's resulting lack of -fms-extensions.
So, I this should _not_ be managed with a NO_* flag, as that has been traditionally about suppressing output. I don't think we want to mix that idiom with parsing issues.
And other architectures solve this problem by not wiping KBUILD_CFLAGS in the first place. :P So if we want to continue to accept the x86 exception (which I would argue is the actual problem), we likely need to, instead, construct an explicit export that is used to collect parsing control options so that it can be re-included here. Today, I can think of -fms-extensions besides -fexperimental-late-parse-attributes.
KBUILD_PARSE_CFLAGS += -fms-extensions
...KBUILD_PARSE_CFLAGS += -fexperimental-late-parse-attributes
...
export KBUILD_PARSE_CFLAGSKBUILD_CFLAGS += $(KBUILD_PARSE_CFLAGS)
...
We already do something like this for CLANG_FLAGS, which, given -fexperimental-late-parse-attributes being Clang-specific, perhaps we ignore my -fms-extensions future-proofing, and just add it there, but it doesn't look like that is how scripts/Makefile.clang was intended to be used.
But, again, I think the problem is x86's wipe of the flags. In fact, I see an explicit problem with that today, which is the loss of -fauto-trivial-var-init, which all the other arch's stub gain (it's an output flag, but it happens to neither change linkage nor create hooked instrumentation). So x86 efi stub lacks stack var zeroing but all the other archs have it.
Can we just fix x86 correctly to use filter-out, etc, there?
-Kees
Signature · 1 line
Kees Cook
Quoted 15 lines · Kees Cook
And other architectures solve this problem by not wiping
KBUILD_CFLAGSin the first place. :P So if we want to continue to accept the x86 exception (which I would argue is the actual problem), we likely need to, instead, construct an explicit export that is used to collect parsing control options so that it can be re-included here. Today, I can think of -fms-extensions besides -fexperimental-late-parse-attributes.
KBUILD_PARSE_CFLAGS+= -fms-extensions
...KBUILD_PARSE_CFLAGS+= -fexperimental-late-parse-attributes
...
exportKBUILD_PARSE_CFLAGSKBUILD_CFLAGS+= $(KBUILD_PARSE_CFLAGS)
...
Quoted 5 lines
We already do something like this for
CLANG_FLAGS, which, given -fexperimental-late-parse-attributes being Clang-specific, perhaps we ignore my -fms-extensions future-proofing, and just add it there, but it doesn't look like that is howscripts/Makefile.clangwas intended to be used.
I agree that fixing the x86 efistub to mirror other architectures is the right fix for this particular instance of the problem (as you note this is not the first time this has bitten us). However, we may want to consider adding '-fexperimental-late-parse-attributes' to CC_FLAGS_DIALECT as well so that the many other places in the kernel that blow away KBUILD_CFLAGS won't have similar problems if __counted_by_ptr happens to be used there, as this type of solution might not always be possible. We could rename it to CC_FLAGS_PARSE or something like that but I am not sure it is strictly necessary.
Signature · 2 lines
Cheers, Nathan
Quoted 17 lines · Nathan Chancellor
Quoted 15 lines · Kees Cook
And other architectures solve this problem by not wiping
KBUILD_CFLAGSin the first place. :P So if we want to continue to accept the x86 exception (which I would argue is the actual problem), we likely need to, instead, construct an explicit export that is used to collect parsing control options so that it can be re-included here. Today, I can think of -fms-extensions besides -fexperimental-late-parse-attributes.
KBUILD_PARSE_CFLAGS+= -fms-extensions
...KBUILD_PARSE_CFLAGS+= -fexperimental-late-parse-attributes
...
exportKBUILD_PARSE_CFLAGSKBUILD_CFLAGS+= $(KBUILD_PARSE_CFLAGS)
...
Quoted 16 lines
Quoted 5 lines
We already do something like this for
CLANG_FLAGS, which, given -fexperimental-late-parse-attributes being Clang-specific, perhaps we ignore my -fms-extensions future-proofing, and just add it there, but it doesn't look like that is howscripts/Makefile.clangwas intended to be used.I agree that fixing the x86 efistub to mirror other architectures is the right fix for this particular instance of the problem (as you note this is not the first time this has bitten us). However, we may want to consider adding '-fexperimental-late-parse-attributes' to
CC_FLAGS_DIALECTas well so that the many other places in the kernel that blow awayKBUILD_CFLAGSwon't have similar problems if__counted_by_ptrhappens to be used there, as this type of solution might not always be possible. We could rename it toCC_FLAGS_PARSEor something like that but I am not sure it is strictly necessary.
Ard,
What's the reason why x86 doesn't need the KBUILD_CFLAGS flags? Is there something that breaks things?
-bw
Quoted 4 lines · Bill Wendling
Ard,
What's the reason why x86 doesn't need the
KBUILD_CFLAGSflags? Is there something that breaks things?
I think it is mostly historical. See Kees's series to address it:
https://lore.kernel.org/[email protected]/
Signature · 2 lines
Cheers, Nathan
Quoted 10 lines · Nathan Chancellor
Quoted 4 lines · Bill Wendling
Ard,
What's the reason why x86 doesn't need the
KBUILD_CFLAGSflags? Is there something that breaks things?I think it is mostly historical. See Kees's series to address it:
I've queued that up now in the EFI tree.
Do we still need this patch then?
Quoted 7 lines · Ard Biesheuvel
Quoted 6 lines · Nathan Chancellor
Quoted 4 lines · Bill Wendling
Ard,
What's the reason why x86 doesn't need the
KBUILD_CFLAGSflags? Is there something that breaks things?
Quoted 8 lines
Quoted 4 lines
I think it is mostly historical. See Kees's series to address it:
I've queued that up now in the EFI tree.
Do we still need this patch then?
I don't think so, since the efistub should now get '-fexperimental-late-parse-attributes' from the main KBUILD_CFLAGS, which is what prompted this patch in the first place:
https://lore.kernel.org/CAGG=3QWCmQ+LVvn2dtjEauj5WFwvx=pvg4Q6man_A2imdjRe7A@mail.gmail.com/
As I mentioned up thread and to Kees in person, we should still consider adding '-fexperimental-late-parse-attributes' into CC_FLAGS_DIALECT to cover other areas that may see __counted_by_ptr use but it is probably not as high priority.
Signature · 2 lines
Cheers, Nathan
Quoted 22 lines · Nathan Chancellor
Quoted 7 lines · Ard Biesheuvel
Quoted 6 lines · Nathan Chancellor
Quoted 4 lines · Bill Wendling
Ard,
What's the reason why x86 doesn't need the
KBUILD_CFLAGSflags? Is there something that breaks things?Quoted 8 lines
Quoted 4 lines
I think it is mostly historical. See Kees's series to address it:
I've queued that up now in the EFI tree.
Do we still need this patch then?
I don't think so, since the efistub should now get '-fexperimental-late-parse-attributes' from the main
KBUILD_CFLAGS, which is what prompted this patch in the first place:
Quoted 7 lines
https://lore.kernel.org/CAGG=3QWCmQ+LVvn2dtjEauj5WFwvx=pvg4Q6man_A2imdjRe7A@mail.gmail.com/
As I mentioned up thread and to Kees in person, we should still consider adding '-fexperimental-late-parse-attributes' into
CC_FLAGS_DIALECTto cover other areas that may see__counted_by_ptruse but it is probably not as high priority.
Excellent.
Quoted 25 lines · Ard Biesheuvel
Quoted 24 lines · Nathan Chancellor
Quoted 7 lines · Ard Biesheuvel
Quoted 6 lines · Nathan Chancellor
Quoted 4 lines · Bill Wendling
Ard,
What's the reason why x86 doesn't need the
KBUILD_CFLAGSflags? Is there something that breaks things?Quoted 8 lines
Quoted 4 lines
I think it is mostly historical. See Kees's series to address it:
I've queued that up now in the EFI tree.
Do we still need this patch then?
I don't think so, since the efistub should now get '-fexperimental-late-parse-attributes' from the main
KBUILD_CFLAGS, which is what prompted this patch in the first place:https://lore.kernel.org/CAGG=3QWCmQ+LVvn2dtjEauj5WFwvx=pvg4Q6man_A2imdjRe7A@mail.gmail.com/
Quoted 7 lines
Quoted 5 lines
As I mentioned up thread and to Kees in person, we should still consider adding '-fexperimental-late-parse-attributes' into
CC_FLAGS_DIALECTto cover other areas that may see__counted_by_ptruse but it is probably not as high priority.Excellent.
What Nathan said about needing this patch. Once Kees's stuff is integrated, all should be well and never break again. (very J/K) :-D
-bw
PATCH v5 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr
In reply to Bill WendlingCode that runs outside the kernel proper, such as the EFI stub, gets nothing out of the counted_by annotations: the bounds checks they feed (FORTIFY_SOURCE, UBSAN_BOUNDS) are already disabled there.
The annotations can also break the build. A __counted_by_ptr() that names a member declared after the pointer needs Clang's '-fexperimental-late-parse-attributes', which the top-level Makefile adds to 'KBUILD_CFLAGS'. The x86 EFI stub builds its own cflags and does not get that flag, so it fails as soon as such a struct is pulled in through a common header.
Overriding the macros from a Makefile doesn't work:
'compiler_types.h' is pulled in with '-include', which is processed after all -D/-U options, so it re-establishes the definitions. Follow the '__NO_FORTIFY' precedent instead: let a build define '__NO_COUNTED_BY' to turn the corresponding annotation into a no-op.
---
v2: Only specify __NO_COUNTED_BY_PTR. Leave __NO_COUNTED_BY until we need it.
v3: Add the version to the Subject line.
v4: Use __NO_COUNTED_BY to reduce churn later on.
v5: Probably good to add the logic in the correct place.
--- include/linux/compiler_types.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
include/linux/compiler_types.h +7 -2
@@ -371,8 +371,13 @@ struct ftrace_likely_data {* * __bdos on clang < 19.1.3 can be off by 4: * https://github.com/llvm/llvm-project/pull/112636 +* +* Code that runs outside the kernel proper (e.g. the EFI stub) can define +* __NO_COUNTED_BY to drop the annotation, since it gains nothing from the +* bounds checks. __NO_COUNTED_BY should be used sparingly, because there are +* better options for opting out of bounds checking. */ -#ifdef CONFIG_CC_HAS_COUNTED_BY +#if defined(CONFIG_CC_HAS_COUNTED_BY ) && !defined(__NO_COUNTED_BY)# define __counted_by (member) __attribute__((__counted_by__(member)))#else # define __counted_by (member)
@@ -388,7 +393,7 @@ struct ftrace_likely_data {* gcc: https://gcc.gnu.org/pipermail/gcc-patches/2025-April/681727.html * clang: https://clang.llvm.org/docs/AttributeReference.html#counted-by-counted-by-or-null-sized-by-sized-by-or-null */ -#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR +#if defined(CONFIG_CC_HAS_COUNTED_BY_PTR ) && !defined(__NO_COUNTED_BY)#define __counted_by_ptr (member) __attribute__((__counted_by__(member)))#else #define __counted_by_ptr (member)
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog