Re: [PATCH] unwind deferred: Annotate struct unwind_cache with __counted_by

From: Steven Rostedt

Date: Fri Nov 14 2025 - 08:53:18 EST


On Fri, 14 Nov 2025 08:43:46 -0500
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> I need to add a comment here that entries is not bound by nr_entries.
>
> https://lore.kernel.org/all/20250730093249.4833be14@xxxxxxxxxxxxxxxxxx/
>
> Maybe this?:

Or better yet, if this compiles (I haven't tried):

diff --git a/include/linux/unwind_deferred_types.h b/include/linux/unwind_deferred_types.h
index 33b62ac25c86..253a69b21e76 100644
--- a/include/linux/unwind_deferred_types.h
+++ b/include/linux/unwind_deferred_types.h
@@ -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];
};

/*
diff --git a/kernel/unwind/deferred.c b/kernel/unwind/deferred.c
index dc6040aae3ee..5dfd0ac264d1 100644
--- a/kernel/unwind/deferred.c
+++ b/kernel/unwind/deferred.c
@@ -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