unwind deferred: Annotate struct unwind_cache with __counted_by
PATCH 1/1 unwind deferred: Annotate struct unwind_cache with __counted_by
Add the __counted_by() compiler attribute to the flexible array member 'entries' to improve access bounds-checking via CONFIG_UBSAN_BOUNDS and CONFIG_FORTIFY_SOURCE.
--- include/linux/unwind_deferred_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/unwind_deferred_types.h +1 -1
@@ -5,7 +5,7 @@struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; + unsigned long entries[] __counted_by(nr_entries); };/*
Signature · 1 line
2.51.1
Steven Rostedt
maintainerQuoted 14 lines · On Fri, 14 Nov 2025 13:27:47 +0100 Thorsten Blum <[email protected]>…
Add the
__counted_by()compiler attribute to the flexible array member 'entries' to improve access bounds-checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Signed-off-by Thorsten Blum <[email protected]>--- include/linux/unwind_deferred_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/unwind_deferred_types.h +0
-0
include/linux/unwind_deferred_types.h line 11
Quoted 8 lines
include/linux/unwind_deferred_types.h +1
-1@@ -5,7 +5,7 @@struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; + unsigned long entries[] __counted_by(nr_entries); };/*
Wrong!
I need to add a comment here that entries is not bound by nr_entries.
https://lore.kernel.org/all/[email protected]/
Maybe this?:
include/linux/unwind_deferred_types.h +1 -1
@@ -5,7 +5,7 @@struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[];/* Fixed size, not bound by nr_entries */ };/*
-- Steve
Steven Rostedt
maintainerQuoted 5 lines · On Fri, 14 Nov 2025 08:43:46 -0500 Steven Rostedt <[email protected]> wrote:
I need to add a comment here that entries is not bound by
nr_entries.https://lore.kernel.org/all/[email protected]/Maybe this?:
Or better yet, if this compiles (I haven't tried):
include/linux/unwind_deferred_types.h +5 -1
@@ -2,10 +2,14 @@#ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H#define _LINUX_UNWIND_USER_DEFERRED_TYPES_H +/* Make the cache fit in a 4K page */ +#define UNWIND_MAX_ENTRIES \ + ((SZ_4K - offset_of(struct unwind_cache, entries)) / sizeof(long)) +struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[UNWIND_MAX_ENTRIES ]; };/*
kernel/unwind/deferred.c +1 -6
@@ -37,10 +37,6 @@ static inline bool try_assign_cnt(struct unwind_task_info *info, u32 cnt) }#endif -/* Make the cache fit in a 4K page */ -#define UNWIND_MAX_ENTRIES \ - ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) -/* Guards adding to or removing from the list of callbacks */ static DEFINE_MUTEX (callback_mutex);static LIST_HEAD (callbacks);
@@ -118,8 +114,7 @@ int unwind_user_faultable(struct unwind_stacktrace *trace)return -EINVAL ;if (!info->cache) { - info->cache =kzalloc (struct_size (cache, entries,UNWIND_MAX_ENTRIES ), -GFP_KERNEL ); + info->cache =kzalloc (sizeof (*cache),GFP_KERNEL );if (!info->cache)return -ENOMEM ; }
-- Steve
Quoted 39 lines · On Fri, 14 Nov 2025 08:43:46 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 14 lines · On Fri, 14 Nov 2025 13:27:47 +0100 Thorsten Blum <[email protected]>…
Add the
__counted_by()compiler attribute to the flexible array member 'entries' to improve access bounds-checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Signed-off-by Thorsten Blum <[email protected]>--- include/linux/unwind_deferred_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/unwind_deferred_types.h +0
-0include/linux/unwind_deferred_types.h line 11
Quoted 8 lines
include/linux/unwind_deferred_types.h +1
-1@@ -5,7 +5,7 @@struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; + unsigned long entries[] __counted_by(nr_entries); };/* Wrong!
I need to add a comment here that entries is not bound by
nr_entries.https://lore.kernel.org/all/[email protected]/Maybe this?:
include/linux/unwind_deferred_types.h +0
-0
include/linux/unwind_deferred_types.h line 6
Quoted 2 lines
include/linux/unwind_deferred_types.h +0
-0@@ -5,7 +5,7 @@struct unwind_cache {unsigned long unwind_completed;
Does that need to be 'long' - 'int' would fit in the padding on 64bit.
include/linux/unwind_deferred_types.h line 9
Quoted 4 lines
include/linux/unwind_deferred_types.h +1
-1unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[];/* Fixed size, not bound by nr_entries */ };
Perhaps it should be:
unsigned long entries[ /* MAX_UNWIND_ENTRIES */ ];
DavidQuoted 7 lines
include/linux/unwind_deferred_types.h +0
-1/* -- Steve
Steven Rostedt
maintainerQuoted 37 lines · On Fri, 14 Nov 2025 14:31:04 +0000 David Laight <[email protected]>…
Quoted 34 lines · On Fri, 14 Nov 2025 08:43:46 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 14 lines · On Fri, 14 Nov 2025 13:27:47 +0100 Thorsten Blum <[email protected]>…
Add the
__counted_by()compiler attribute to the flexible array member 'entries' to improve access bounds-checking viaCONFIG_UBSAN_BOUNDSandCONFIG_FORTIFY_SOURCE.Signed-off-by Thorsten Blum <[email protected]>--- include/linux/unwind_deferred_types.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)include/linux/unwind_deferred_types.h +0
-0include/linux/unwind_deferred_types.h line 11
Quoted 8 lines
include/linux/unwind_deferred_types.h +1
-1@@ -5,7 +5,7 @@struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; + unsigned long entries[] __counted_by(nr_entries); };/* Wrong!
I need to add a comment here that entries is not bound by
nr_entries.https://lore.kernel.org/all/[email protected]/Maybe this?:
Quoted 9 lines
include/linux/unwind_deferred_types.h line 6
Quoted 7 lines
include/linux/unwind_deferred_types.h +0
-0@@ -5,7 +5,7 @@struct unwind_cache {unsigned long unwind_completed;Does that need to be 'long' - 'int' would fit in the padding on 64bit.
We could make it 32 bit, then the number of tracers attached would be the same as it would be on 32 bit archs (which would be 30). Maybe that's enough.
Quoted 8 lines
include/linux/unwind_deferred_types.h line 9
Quoted 5 lines
include/linux/unwind_deferred_types.h +1
-1unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[];/* Fixed size, not bound by nr_entries */ };Perhaps it should be:
unsigned long entries[ /* MAX_UNWIND_ENTRIES */ ];
Whatever would keep the coccinelle folks from sending more patches.
-- Steve
Steven Rostedt
maintainerQuoted 7 lines · On Fri, 14 Nov 2025 08:53:32 -0500 Steven Rostedt <[email protected]> wrote:
include/linux/unwind_deferred_types.h +1
-0@@ -2,10 +2,14 @@#ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H#define _LINUX_UNWIND_USER_DEFERRED_TYPES_H +/* Make the cache fit in a 4K page */
include/linux/unwind_deferred_types.h line 14
Quoted 10 lines
include/linux/unwind_deferred_types.h +4
-1+#define UNWIND_MAX_ENTRIES \ + ((SZ_4K - offset_of(struct unwind_cache, entries)) / sizeof(long)) +struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[UNWIND_MAX_ENTRIES ]; };
Nope, this fails with:
/work/git/test-linux.git/include/linux/stddef.h:16:33: error: invalid use of undefined type ‘struct unwind_cache’
16 | #define offsetof(TYPE, MEMBER) __builtin_offsetof(TYPE, MEMBER)
| ^~~~~~~~~~~~~~~~~~/work/git/test-linux.git/include/linux/unwind_deferred_types.h:9:19: note: in expansion of macro ‘offsetof’
9 | ((SZ_4K - offsetof(struct unwind_cache, entries)) / sizeof(long))
| ^~~~~~~~
/work/git/test-linux.git/include/linux/unwind_deferred_types.h:14:41: note: in expansion of macro ‘UNWIND_MAX_ENTRIES’
14 | unsigned long entries[UNWIND_MAX_ENTRIES];
| ^~~~~~~~~~~~~~~~~~-- Steve
Quoted 18 lines · On Fri, 14 Nov 2025 08:53:32 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 5 lines · On Fri, 14 Nov 2025 08:43:46 -0500 Steven Rostedt <[email protected]> wrote:
I need to add a comment here that entries is not bound by
nr_entries.https://lore.kernel.org/all/[email protected]/Maybe this?:
Or better yet, if this compiles (I haven't tried):
include/linux/unwind_deferred_types.h +0
-0@@ -2,10 +2,14 @@#ifndef _LINUX_UNWIND_USER_DEFERRED_TYPES_H#define _LINUX_UNWIND_USER_DEFERRED_TYPES_H
include/linux/unwind_deferred_types.h line 12
Quoted 10 lines
include/linux/unwind_deferred_types.h +5
-1+/* Make the cache fit in a 4K page */ +#define UNWIND_MAX_ENTRIES \ + ((SZ_4K - offset_of(struct unwind_cache, entries)) / sizeof(long)) +struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[UNWIND_MAX_ENTRIES ];
That won't compile - I tried it.
You could add __aligned(4096) to force the structure to be padded to 4k, and then define UNWIND_MAX_ENTRIES in terms of the structure size.
David
Quoted 31 lines
include/linux/unwind_deferred_types.h +0
-0};/* kernel/unwind/deferred.c +1
-7@@ -37,10 +37,6 @@ static inline bool try_assign_cnt(struct unwind_task_info *info, u32 cnt) }#endif -/* Make the cache fit in a 4K page */ -#define UNWIND_MAX_ENTRIES \ - ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) -/* Guards adding to or removing from the list of callbacks */ static DEFINE_MUTEX (callback_mutex);static LIST_HEAD (callbacks);@@ -118,8 +114,7 @@ int unwind_user_faultable(struct unwind_stacktrace *trace)return -EINVAL ;if (!info->cache) { - info->cache =kzalloc (struct_size (cache, entries,UNWIND_MAX_ENTRIES ), -GFP_KERNEL ); + info->cache =kzalloc (sizeof (*cache),GFP_KERNEL );if (!info->cache)return -ENOMEM ; } -- Steve
Steven Rostedt
maintainerQuoted 12 lines · On Fri, 14 Nov 2025 15:02:09 +0000 David Laight <[email protected]>…
include/linux/unwind_deferred_types.h line 12
Quoted 9 lines
include/linux/unwind_deferred_types.h +5
-1+/* Make the cache fit in a 4K page */ +#define UNWIND_MAX_ENTRIES \ + ((SZ_4K - offset_of(struct unwind_cache, entries)) / sizeof(long)) +struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[UNWIND_MAX_ENTRIES ];That won't compile - I tried it.
Quoted 2 lines
You could add
__aligned(4096)to force the structure to be padded to 4k, and then defineUNWIND_MAX_ENTRIESin terms of the structure size.
I rather not do more tricks than what a comment would solve.
-- Steve
Steven Rostedt
maintainerQuoted 10 lines · On Fri, 14 Nov 2025 09:56:44 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 8 lines
include/linux/unwind_deferred_types.h line 9
Quoted 5 lines
include/linux/unwind_deferred_types.h +1
-1unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[];/* Fixed size, not bound by nr_entries */ };Perhaps it should be:
unsigned long entries[ /* MAX_UNWIND_ENTRIES */ ];Whatever would keep the coccinelle folks from sending more patches.
Thorsten,
Which comment would you feel is more obvious that entries is not bound by nr_entries and prevent this patch from being sent again?
-- Steve
Steven Rostedt
maintainerQuoted 12 lines · On Fri, 14 Nov 2025 10:49:52 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 9 lines · On Fri, 14 Nov 2025 09:56:44 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 8 lines
Review
Quoted 5 lines
(quoted diff) +1
-1unsigned int nr_entries; - unsigned long entries[]; + unsigned long entries[]; /* Fixed size, not bound by nr_entries */ };Perhaps it should be:
unsigned long entries[ /* MAX_UNWIND_ENTRIES */ ];
Quoted 7 lines
Quoted 1 line
Whatever would keep the coccinelle folks from sending more patches.
Thorsten,
Which comment would you feel is more obvious that entries is not bound by
nr_entriesand prevent this patch from being sent again?
Or we can simply move the macro to the header:
include/linux/unwind_deferred_types.h +5 -1
@@ -5,10 +5,14 @@#include <linux/types.h> #include <linux/atomic.h> +/* Make the cache fit in a 4K page */ +#define UNWIND_MAX_ENTRIES \ + ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) +struct unwind_cache {unsigned long unwind_completed;unsigned int nr_entries; -unsigned long entries[]; +unsigned long entries[/* UNWIND_MAX_ENTRIES */ ]; };/*
kernel/unwind/deferred.c +0 -4
@@ -37,10 +37,6 @@ static inline bool try_assign_cnt(struct unwind_task_info *info, u32 cnt) }#endif -/* Make the cache fit in a 4K page */ -#define UNWIND_MAX_ENTRIES \ - ((SZ_4K - sizeof(struct unwind_cache)) / sizeof(long)) -/* Guards adding to or removing from the list of callbacks */ static DEFINE_MUTEX (callback_mutex);static LIST_HEAD (callbacks);
-- Steve
Quoted 2 lines · Steven Rostedt
Which comment would you feel is more obvious that entries is not bound by
nr_entriesand prevent this patch from being sent again?
Both seem fine to me.