Skip to contentSkip to conversation

Compiler Attributes: counted_by: Adjust name and identifier expansion

PATCH Applied Kees Cook 4 participants 7 messages Aug 17, 2023 – Aug 18, 2023 Compiler attributes

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 Compiler Attributes: counted_by: Adjust name and identifier expansion

GCC and Clang's current RFCs name this attribute "counted_by", and have moved away from using a string for the member name. Update the kernel's macros to match. Additionally provide a UAPI no-op macro for UAPI structs that will gain annotations.

Cc Miguel Ojeda <[email protected]>
Cc Nick Desaulniers <[email protected]>
Fixes dd06e72e68bc ("Compiler Attributes: Add __counted_by macro")
---
 include/linux/compiler_attributes.h | 26 +++++++++++++-------------
 include/uapi/linux/stddef.h         |  4 ++++
 2 files changed, 17 insertions(+), 13 deletions(-)
include/linux/compiler_attributes.h +13 -13
index 00efa35c350f..74716a407aac 100644
@@ -94,6 +94,19 @@
 # define __copy(symbol)
 #endif
 
+/*
+ * Optional: only supported since gcc >= 14
+ * Optional: only supported since clang >= 17
+ *
+ *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
+ * clang: https://reviews.llvm.org/D148381
+ */
+#if __has_attribute(__counted_by__)
+# define __counted_by(member)		__attribute__((__counted_by__(member)))
+#else
+# define __counted_by(member)
+#endif
+
 /*
  * Optional: not supported by gcc
  * Optional: only supported since clang >= 14.0
@@ -129,19 +142,6 @@
 # define __designated_init
 #endif
 
-/*
- * Optional: only supported since gcc >= 14
- * Optional: only supported since clang >= 17
- *
- *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
- * clang: https://reviews.llvm.org/D148381
- */
-#if __has_attribute(__element_count__)
-# define __counted_by(member)		__attribute__((__element_count__(#member)))
-#else
-# define __counted_by(member)
-#endif
-
 /*
  * Optional: only supported since clang >= 14.0
  *
include/uapi/linux/stddef.h +4 -0
index 7837ba4fe728..7c3fc3980881 100644
@@ -45,3 +45,7 @@
 		TYPE NAME[]; \
 	}
 #endif
+
+#ifndef __counted_by
+#define __counted_by(m)
+#endif
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Kees Cook

GCC and Clang's current RFCs name this attribute "counted_by", and have moved away from using a string for the member name. Update the kernel's macros to match. Additionally provide a UAPI no-op macro for UAPI structs that will gain annotations.

Cc Miguel Ojeda <[email protected]>
Cc Nick Desaulniers <[email protected]>
Fixes dd06e72e68bc ("Compiler Attributes: Add __counted_by macro")

One comment below.

Quoted 10 lines
---
 include/linux/compiler_attributes.h | 26 +++++++++++++-------------
 include/uapi/linux/stddef.h         |  4 ++++
 2 files changed, 17 insertions(+), 13 deletions(-)
include/linux/compiler_attributes.h +0 -0
index 00efa35c350f..74716a407aac 100644

include/linux/compiler_attributes.h line 99

Quoted 6 lines
include/linux/compiler_attributes.h +3 -0
@@ -94,6 +94,19 @@
 # define __copy(symbol)
 #endif
 
+/*
+ * Optional: only supported since gcc >= 14
+ * Optional: only supported since clang >= 17

Since you are in the area, should the supported clang version be optimistically updated to '18'? 17.x has already branched and I suspect it would be too late in the release cycle to cherry-pick that change from main if it were landed today (which seems pretty unlikely, given there do not appear to be many reviews from high profile clang folks yet).

Quoted 48 lines
include/linux/compiler_attributes.h +10 -13
+ *
+ *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
+ * clang: https://reviews.llvm.org/D148381
+ */
+#if __has_attribute(__counted_by__)
+# define __counted_by(member)		__attribute__((__counted_by__(member)))
+#else
+# define __counted_by(member)
+#endif
+
 /*
  * Optional: not supported by gcc
  * Optional: only supported since clang >= 14.0
@@ -129,19 +142,6 @@
 # define __designated_init
 #endif
 
-/*
- * Optional: only supported since gcc >= 14
- * Optional: only supported since clang >= 17
- *
- *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
- * clang: https://reviews.llvm.org/D148381
- */
-#if __has_attribute(__element_count__)
-# define __counted_by(member)		__attribute__((__element_count__(#member)))
-#else
-# define __counted_by(member)
-#endif
-
 /*
  * Optional: only supported since clang >= 14.0
  *
include/uapi/linux/stddef.h +4 -0
index 7837ba4fe728..7c3fc3980881 100644
@@ -45,3 +45,7 @@
 		TYPE NAME[]; \
 	}
 #endif
+
+#ifndef __counted_by
+#define __counted_by(m)
+#endif
Signature · 1 line
2.34.1
author
#2 lore raw
Quoted 28 lines · Nathan Chancellor
Quoted 9 lines · Kees Cook

GCC and Clang's current RFCs name this attribute "counted_by", and have moved away from using a string for the member name. Update the kernel's macros to match. Additionally provide a UAPI no-op macro for UAPI structs that will gain annotations.

Cc Miguel Ojeda <[email protected]>
Cc Nick Desaulniers <[email protected]>
Fixes dd06e72e68bc ("Compiler Attributes: Add __counted_by macro")

One comment below.

Quoted 13 lines
---
 include/linux/compiler_attributes.h | 26 +++++++++++++-------------
 include/uapi/linux/stddef.h         |  4 ++++
 2 files changed, 17 insertions(+), 13 deletions(-)
include/linux/compiler_attributes.h +0 -0
index 00efa35c350f..74716a407aac 100644
@@ -94,6 +94,19 @@
 # define __copy(symbol)
 #endif
 
Quoted 10 lines

include/linux/compiler_attributes.h line 99

Quoted 3 lines
include/linux/compiler_attributes.h +3 -0
+/*
+ * Optional: only supported since gcc >= 14
+ * Optional: only supported since clang >= 17

Since you are in the area, should the supported clang version be optimistically updated to '18'? 17.x has already branched and I suspect it would be too late in the release cycle to cherry-pick that change from main if it were landed today (which seems pretty unlikely, given there do not appear to be many reviews from high profile clang folks yet).

Oops, yes. Excellent point. I'll bump this to 18.

-Kees

Quoted 49 lines
Quoted 49 lines
include/linux/compiler_attributes.h +10 -13
+ *
+ *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
+ * clang: https://reviews.llvm.org/D148381
+ */
+#if __has_attribute(__counted_by__)
+# define __counted_by(member)		__attribute__((__counted_by__(member)))
+#else
+# define __counted_by(member)
+#endif
+
 /*
  * Optional: not supported by gcc
  * Optional: only supported since clang >= 14.0
@@ -129,19 +142,6 @@
 # define __designated_init
 #endif
 
-/*
- * Optional: only supported since gcc >= 14
- * Optional: only supported since clang >= 17
- *
- *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
- * clang: https://reviews.llvm.org/D148381
- */
-#if __has_attribute(__element_count__)
-# define __counted_by(member)		__attribute__((__element_count__(#member)))
-#else
-# define __counted_by(member)
-#endif
-
 /*
  * Optional: only supported since clang >= 14.0
  *
include/uapi/linux/stddef.h +4 -0
index 7837ba4fe728..7c3fc3980881 100644
@@ -45,3 +45,7 @@
 		TYPE NAME[]; \
 	}
 #endif
+
+#ifndef __counted_by
+#define __counted_by(m)
+#endif
Signature · 1 line
2.34.1
Signature · 1 line
Kees Cook
Acked-by on v1 1/1
#3 lore raw
In reply to Kees Cook
Quoted 10 lines · Kees Cook

GCC and Clang's current RFCs name this attribute "counted_by", and have moved away from using a string for the member name. Update the kernel's macros to match. Additionally provide a UAPI no-op macro for UAPI structs that will gain annotations.

Cc Miguel Ojeda <[email protected]>
Cc Nick Desaulniers <[email protected]>
Fixes dd06e72e68bc ("Compiler Attributes: Add __counted_by macro")

Looks good to me, thanks. Perhaps we should split the UAPI bit into a second patch.

I can take it into my tree; otherwise if you need this elsewhere:

Cheers,
Miguel

author
#4 lore raw
Quoted 7 lines · Miguel Ojeda
Quoted 6 lines · Kees Cook

GCC and Clang's current RFCs name this attribute "counted_by", and have moved away from using a string for the member name. Update the kernel's macros to match. Additionally provide a UAPI no-op macro for UAPI structs that will gain annotations.

Quoted 7 lines
Quoted 4 lines
Cc Miguel Ojeda <[email protected]>
Cc Nick Desaulniers <[email protected]>
Fixes dd06e72e68bc ("Compiler Attributes: Add __counted_by macro")

Looks good to me, thanks. Perhaps we should split the UAPI bit into a second patch.

I'd like to keep the UAPI part -- that was an oversight when I first added it.

Quoted 3 lines

I can take it into my tree; otherwise if you need this elsewhere:

Thanks! It'd be slightly easier for me if it was is my tree, so I'll grab your Ack. :) Let me know if you'd prefer it otherwise.

-Kees

Signature · 1 line
Kees Cook
Reviewed-by on v1 1/1
#5 lore raw
In reply to Kees Cook
Quoted 9 lines · Kees Cook

GCC and Clang's current RFCs name this attribute "counted_by", and have moved away from using a string for the member name. Update the kernel's macros to match. Additionally provide a UAPI no-op macro for UAPI structs that will gain annotations.

Cc Miguel Ojeda <[email protected]>
Cc Nick Desaulniers <[email protected]>
Fixes dd06e72e68bc ("Compiler Attributes: Add __counted_by macro")

Thanks

Signature · 1 line
Gustavo
Quoted 61 lines
---
  include/linux/compiler_attributes.h | 26 +++++++++++++-------------
  include/uapi/linux/stddef.h         |  4 ++++
  2 files changed, 17 insertions(+), 13 deletions(-)
include/linux/compiler_attributes.h +13 -13
index 00efa35c350f..74716a407aac 100644
@@ -94,6 +94,19 @@
  # define __copy(symbol)
  #endif
 
+/*
+ * Optional: only supported since gcc >= 14
+ * Optional: only supported since clang >= 17
+ *
+ *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
+ * clang: https://reviews.llvm.org/D148381
+ */
+#if __has_attribute(__counted_by__)
+# define __counted_by(member)		__attribute__((__counted_by__(member)))
+#else
+# define __counted_by(member)
+#endif
+
  /*
   * Optional: not supported by gcc
   * Optional: only supported since clang >= 14.0
@@ -129,19 +142,6 @@
  # define __designated_init
  #endif
 
-/*
- * Optional: only supported since gcc >= 14
- * Optional: only supported since clang >= 17
- *
- *   gcc: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108896
- * clang: https://reviews.llvm.org/D148381
- */
-#if __has_attribute(__element_count__)
-# define __counted_by(member)		__attribute__((__element_count__(#member)))
-#else
-# define __counted_by(member)
-#endif
-
  /*
   * Optional: only supported since clang >= 14.0
   *
include/uapi/linux/stddef.h +4 -0
index 7837ba4fe728..7c3fc3980881 100644
@@ -45,3 +45,7 @@
  		TYPE NAME[]; \
  	}
  #endif
+
+#ifndef __counted_by
+#define __counted_by(m)
+#endif
author Applied
#6 lore raw
In reply to Kees Cook
Quoted 4 lines · Kees Cook

GCC and Clang's current RFCs name this attribute "counted_by", and have moved away from using a string for the member name. Update the kernel's macros to match. Additionally provide a UAPI no-op macro for UAPI structs that will gain annotations.

Applied to for-next/hardening, thanks!

[1/1] Compiler Attributes: counted_by: Adjust name and identifier expansion

      https://git.kernel.org/kees/c/c8248faf3ca2

Take care,

Signature · 1 line
Kees Cook