Skip to contentSkip to conversation

i2c: atr: annotate i2c_atr_adap_desc->aliases with __counted_by_ptr

PATCH Superseded Thorsten Blum 3 participants 4 messages Apr 27 – Apr 29

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: atr: annotate i2c_atr_adap_desc->aliases with __counted_by_ptr

Add the __counted_by_ptr() compiler attribute to ->aliases to improve bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

---
 include/linux/i2c-atr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/i2c-atr.h +1 -1
index 2bb54dc87c8e..b52a7b9ec536 100644
@@ -71,7 +71,7 @@ struct i2c_atr_adap_desc {
 	struct device *parent;
 	struct fwnode_handle *bus_handle;
 	size_t num_aliases;
-	u16 *aliases;
+	u16 *aliases __counted_by_ptr(num_aliases);
 };
 
 /**
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hello Thorsten,

+Cc Romain, author of the involved code
+Cc Rosen, author of a similar patch elsewhere in i2c-atr [0]

[0] https://lore.kernel.org/linux-i2c/[email protected]/

Quoted 13 lines · Thorsten Blum

Add the __counted_by_ptr() compiler attribute to ->aliases to improve bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

---
 include/linux/i2c-atr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/i2c-atr.h +0 -0
index 2bb54dc87c8e..b52a7b9ec536 100644

include/linux/i2c-atr.h struct i2c_atr_adap_desc { · line 74

Quoted 5 lines
include/linux/i2c-atr.h +1 -1
@@ -71,7 +71,7 @@ struct i2c_atr_adap_desc {
 	struct device *parent;
 	struct fwnode_handle *bus_handle;
 	size_t num_aliases;
-	u16 *aliases;
+	u16 *aliases __counted_by_ptr(num_aliases);

I'm not fully proficient about all counted_by things, but the patch looks OK to me, and definitely @num_aliases is the number of items @aliases must have, so:

--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

#2 lore raw
Quoted 17 lines · Luca Ceresoli

Hello Thorsten,

(quoted diff) +2 -0
+Cc Romain, author of the involved code
+Cc Rosen, author of a similar patch elsewhere in i2c-atr [0]

[0] https://lore.kernel.org/linux-i2c/[email protected]/

Quoted 8 lines · Thorsten Blum

Add the __counted_by_ptr() compiler attribute to ->aliases to improve bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

---
 include/linux/i2c-atr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

include/linux/i2c-atr.h struct i2c_atr_adap_desc { · line 74

Quoted 10 lines
include/linux/i2c-atr.h +1 -1
index 2bb54dc87c8e..b52a7b9ec536 100644
@@ -71,7 +71,7 @@ struct i2c_atr_adap_desc {
      struct device *parent;
      struct fwnode_handle *bus_handle;
      size_t num_aliases;
-     u16 *aliases;
+     u16 *aliases __counted_by_ptr(num_aliases);

I'm not fully proficient about all counted_by things, but the patch looks OK to me, and definitely @num_aliases is the number of items @aliases must have, so:

I prefer my patch.

__counted_by is supported by more versions of GCC/Clang than __counted_by_ptr.

Quoted 7 lines

--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

reviewer
#3 lore raw
Quoted 32 lines · Rosen Penev
Quoted 17 lines · Luca Ceresoli

Hello Thorsten,

(quoted diff) +2 -0
+Cc Romain, author of the involved code
+Cc Rosen, author of a similar patch elsewhere in i2c-atr [0]

[0] https://lore.kernel.org/linux-i2c/[email protected]/

Quoted 8 lines · Thorsten Blum

Add the __counted_by_ptr() compiler attribute to ->aliases to improve bounds checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.

---
 include/linux/i2c-atr.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 14 lines

include/linux/i2c-atr.h struct i2c_atr_adap_desc { · line 74

Quoted 10 lines
include/linux/i2c-atr.h +1 -1
index 2bb54dc87c8e..b52a7b9ec536 100644
@@ -71,7 +71,7 @@ struct i2c_atr_adap_desc {
      struct device *parent;
      struct fwnode_handle *bus_handle;
      size_t num_aliases;
-     u16 *aliases;
+     u16 *aliases __counted_by_ptr(num_aliases);

I'm not fully proficient about all counted_by things, but the patch looks OK to me, and definitely @num_aliases is the number of items @aliases must have, so:

Quoted 3 lines

I prefer my patch.

__counted_by is supported by more versions of GCC/Clang than __counted_by_ptr.

At a quick grep check, they seem to be different things: __counted_by for arrays embedded in structs, __counted_by_ptr for pointers in structs for dynamically allocated arrays. I wish this were documented, though, especially as their implementation is currently identical. :-/

Based on this, I think both Rosen's and Thorsten's changes are OK, using different macros for different members.

Luca

--
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com