Skip to contentSkip to conversation

compiler_types: Allow opting out of __counted_by and __counted_by_ptr

PATCH In review Bill Wendling 5 participants 22 messages Oct 5 – Oct 8

lore
Share & tools
lore
#0 lore raw

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.

Assisted-by LLM
---
 include/linux/compiler_types.h | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
include/linux/compiler_types.h +13 -2
index c5921f139007..916a946f623c 100644
@@ -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
Reviewed-by on v1 1/1
#1 lore raw

Hi,

Quoted 64 lines · Bill Wendling

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.

Assisted-by LLM
---
 include/linux/compiler_types.h | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
include/linux/compiler_types.h +13 -2
index c5921f139007..916a946f623c 100644
@@ -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

#2 lore raw
In reply to Bill Wendling

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_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.

Quoted 9 lines

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.

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.

#3 lore raw
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_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.

Quoted 9 lines

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.

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

#4 lore raw

PATCH 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr

In reply to Bill Wendling

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 __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.

Assisted-by LLM

---
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
index c5921f139007..8bde6798b4ec 100644
@@ -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
#5 lore raw

PATCH v3 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr

In reply to Bill Wendling

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 __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.

Assisted-by LLM

---
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
index c5921f139007..8bde6798b4ec 100644
@@ -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
#6 lore raw
Quoted 47 lines · Bill Wendling

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 __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.

Assisted-by LLM

---
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 +0 -0
index c5921f139007..8bde6798b4ec 100644
@@ -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).
*/

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

#7 lore raw

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_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.

Assisted-by LLM

---
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 +0 -0
index c5921f139007..8bde6798b4ec 100644
@@ -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).
*/

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

#8 lore raw
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_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.

Assisted-by LLM

---
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 +0 -0
index c5921f139007..8bde6798b4ec 100644
@@ -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).
*/

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.

#9 lore raw
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_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.

Assisted-by LLM

---
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 +0 -0
index c5921f139007..8bde6798b4ec 100644
@@ -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).
*/

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.

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

#10 lore raw

PATCH v4 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr

In reply to Bill Wendling

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' to turn the corresponding annotation into a no-op.

Assisted-by LLM

---
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
index c5921f139007..05e2b637f1cd 100644
@@ -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
#11 lore raw
Quoted 36 lines · Bill Wendling

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' to turn the corresponding annotation into a no-op.

Assisted-by LLM

---
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 +1 -0
index c5921f139007..05e2b637f1cd 100644
@@ -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.

#12 lore raw
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_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.

Assisted-by LLM

---
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 +5 -0
index c5921f139007..05e2b637f1cd 100644
@@ -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_BY should 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

#13 lore raw
In reply to Bill Wendling
Quoted 10 lines · Bill Wendling

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.

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_CFLAGS
KBUILD_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
#14 lore raw
Quoted 15 lines · Kees Cook

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_CFLAGS
KBUILD_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 how scripts/Makefile.clang was 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
#15 lore raw
Quoted 17 lines · Nathan Chancellor
Quoted 15 lines · Kees Cook

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_CFLAGS
KBUILD_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 how scripts/Makefile.clang was 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.

Ard,

What's the reason why x86 doesn't need the KBUILD_CFLAGS flags? Is there something that breaks things?

-bw

#17 lore raw
Quoted 10 lines · Nathan Chancellor
Quoted 4 lines · Bill Wendling

Ard,

What's the reason why x86 doesn't need the KBUILD_CFLAGS flags? 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]/

I've queued that up now in the EFI tree.

Do we still need this patch then?

#18 lore raw
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_CFLAGS flags? 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:

https://lore.kernel.org/[email protected]/

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
#19 lore raw
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_CFLAGS flags? 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:

https://lore.kernel.org/[email protected]/

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_DIALECT to cover other areas that may see __counted_by_ptr use but it is probably not as high priority.

Excellent.

#20 lore raw
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_CFLAGS flags? 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:

https://lore.kernel.org/[email protected]/

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_DIALECT to cover other areas that may see __counted_by_ptr use 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

#21 lore raw

PATCH v5 1/1 compiler_types: Allow opting out of __counted_by and __counted_by_ptr

In reply to Bill Wendling

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' to turn the corresponding annotation into a no-op.

Assisted-by LLM

---
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
index c5921f139007..932e81e7630d 100644
@@ -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