ftrace: Use flexible array for hash buckets
PATCH 1/1 ftrace: Use flexible array for hash buckets
Store ftrace hash buckets in the ftrace_hash allocation instead of allocating the bucket array separately.
This keeps the bucket storage tied to the hash lifetime and simplifies the allocation and cleanup paths.
--- kernel/trace/ftrace.c | 17 ++--------------- kernel/trace/trace.h | 2 +- 2 files changed, 3 insertions(+), 16 deletions(-)
kernel/trace/ftrace.c +2 -15
@@ -1082,10 +1082,7 @@ struct ftrace_func_probe {* it all the time. These are in a read only section such that if * anyone does try to modify it, it will cause an exception. */ -static const struct hlist_head empty_buckets[1 ]; -static const struct ftrace_hash empty_hash = { - .buckets = (struct hlist_head *)empty_buckets, -}; +static const struct ftrace_hash empty_hash = {};#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)struct ftrace_ops global_ops = {
@@ -1295,7 +1292,6 @@ void free_ftrace_hash(struct ftrace_hash *hash)if (!hash || hash ==EMPTY_HASH )return ;ftrace_hash_clear (hash); -kfree (hash->buckets);kfree (hash); }
@@ -1333,20 +1329,11 @@ EXPORT_SYMBOL_GPL(ftrace_free_filter);struct ftrace_hash *alloc_ftrace_hash (int size_bits) {struct ftrace_hash *hash; -int size; - hash =kzalloc_obj (*hash); + hash =kzalloc_flex (*hash, buckets,BIT (size_bits));if (!hash)return NULL ; - size =1 << size_bits; - hash->buckets =kzalloc_objs (*hash->buckets, size); - -if (!hash->buckets) { -kfree (hash); -return NULL ; - } - hash->size_bits = size_bits;return hash;
kernel/trace/trace.h +1 -1
@@ -1000,10 +1000,10 @@ enum {struct ftrace_hash {unsigned long size_bits; -struct hlist_head *buckets;unsigned long count;unsigned long flags;struct rcu_head rcu; +struct hlist_head buckets[]; }; struct ftrace_func_entry *
Signature · 1 line
2.54.0
Steven Rostedt
maintainerQuoted 7 lines · On Wed, 20 May 2026 15:00:30 -0700 Rosen Penev <[email protected]> wrote:
Store ftrace hash buckets in the
ftrace_hashallocation instead of allocating the bucket array separately.This keeps the bucket storage tied to the hash lifetime and simplifies the allocation and cleanup paths.
Assisted-by Codex:GPT-5.5
I'll let the AI's duke it out!
Quoted 11 lines
Signed-off-by Rosen Penev <[email protected]>--- kernel/trace/ftrace.c | 17 ++--------------- kernel/trace/trace.h | 2 +- 2 files changed, 3 insertions(+), 16 deletions(-)kernel/trace/ftrace.c +0
-0
kernel/trace/ftrace.c struct ftrace_func_probe { · line 1086
Quoted 9 lines
kernel/trace/ftrace.c +1
-4@@ -1082,10 +1082,7 @@ struct ftrace_func_probe {* it all the time. These are in a read only section such that if * anyone does try to modify it, it will cause an exception. */ -static const struct hlist_head empty_buckets[1 ]; -static const struct ftrace_hash empty_hash = { - .buckets = (struct hlist_head *)empty_buckets, -}; +static const struct ftrace_hash empty_hash = {};#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)
According to Sashiko: https://sashiko.dev/#/patchset/20260520220030.16887-1-rosenp%40gmail.com
Could this conversion to a flexible array member cause an out-of-bounds read when iterating over the empty hash? Because empty_hash is now initialized as an empty struct, its flexible array member buckets has a size of 0. However, empty_hash.size_bits is 0, which means loop limits computing '1 << hash->size_bits' will evaluate to 1. If functions like prepare_direct_functions_for_ipmodify() iterate over a default EMPTY_HASH without checking ftrace_hash_empty(), they will attempt to read EMPTY_HASH->buckets[0]. This reads past the end of the struct into adjacent memory in the .rodata section. If that adjacent memory happens to be non-zero, the linked list loop could dereference it and cause a kernel panic. Prior to this patch, empty_buckets provided a safely zeroed array of size 1 to handle this single iteration.
-- Steve
Quoted 38 lines · Steven Rostedt
Quoted 7 lines · On Wed, 20 May 2026 15:00:30 -0700 Rosen Penev <[email protected]> wrote:
Store ftrace hash buckets in the
ftrace_hashallocation instead of allocating the bucket array separately.This keeps the bucket storage tied to the hash lifetime and simplifies the allocation and cleanup paths.
Assisted-by Codex:GPT-5.5I'll let the AI's duke it out!
Quoted 11 lines
Signed-off-by Rosen Penev <[email protected]>--- kernel/trace/ftrace.c | 17 ++--------------- kernel/trace/trace.h | 2 +- 2 files changed, 3 insertions(+), 16 deletions(-)kernel/trace/ftrace.c +0
-0kernel/trace/ftrace.c struct ftrace_func_probe { · line 1086
Quoted 9 lines
kernel/trace/ftrace.c +1
-4@@ -1082,10 +1082,7 @@ struct ftrace_func_probe {* it all the time. These are in a read only section such that if * anyone does try to modify it, it will cause an exception. */ -static const struct hlist_head empty_buckets[1 ]; -static const struct ftrace_hash empty_hash = { - .buckets = (struct hlist_head *)empty_buckets, -}; +static const struct ftrace_hash empty_hash = {};#define EMPTY_HASH ((struct ftrace_hash *)&empty_hash)According to Sashiko: https://sashiko.dev/#/patchset/20260520220030.16887-1-rosenp%40gmail.com
Quoted 14 lines
Could this conversion to a flexible array member cause an out-of-bounds read when iterating over the empty hash? Because
empty_hashis now initialized as an empty struct, its flexible array member buckets has a size of 0. However, empty_hash.size_bits is 0, which means loop limits computing '1 <<hash->size_bits' will evaluate to 1. If functions likeprepare_direct_functions_for_ipmodify()iterate over a defaultEMPTY_HASHwithout checkingftrace_hash_empty(), they will attempt to read EMPTY_HASH->buckets[0]. This reads past the end of the struct into adjacent memory in the .rodata section. If that adjacent memory happens to be non-zero, the linked list loop could dereference it and cause a kernel panic. Prior to this patch,empty_bucketsprovided a safely zeroed array of size 1 to handle this single iteration.
Yeah this looks right. Might as well abandon.
Quoted 2 lines
-- Steve
kernel test robot
build botkernel test robot noticed "BUG:KASAN:global-out-of-bounds_in_ftrace_find_rec_direct" on:
Hello,
kernel test robot noticed "BUG:KASAN:global-out-of-bounds_in_ftrace_find_rec_direct" on:
commit: 42ed22b1b4ae179c78e088477950eb1dd1dc9e90 ("[PATCH] ftrace: Use flexible array for hash buckets")
url: https://github.com/intel-lab-lkp/linux/commits/Rosen-Penev/ftrace-Use-flexible-array-for-hash-buckets/20260521-060312
base: https://git.kernel.org/cgit/linux/kernel/git/trace/linux-trace for-next
patch link: https://lore.kernel.org/all/[email protected]/
patch subject: [PATCH] ftrace: Use flexible array for hash buckets
in testcase: boot
config: x86_64-rhel-9.4-kunit
compiler: gcc-14
test machine: qemu-system-x86_64 -enable-kvm -cpu SandyBridge -smp 2 -m 32G
(please refer to attached dmesg/kmsg for entire log/backtrace)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-lkp/[email protected]
[ 9.849798][ T1] BUG: KASAN: global-out-of-bounds in ftrace_find_rec_direct (trace/ftrace.c:1172 (discriminator 2) trace/ftrace.c:2611 (discriminator 2)) [ 9.849798][ T1] Read of size 8 at addr ffffffff9675bd48 by task swapper/0/1 [ 9.849798][ T1] [ 9.849798][ T1] CPU: 1 UID: 0 PID: 1 Comm: swapper/0 Not tainted 7.1.0-rc3+ #1 PREEMPT(lazy) [ 9.849798][ T1] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 9.849798][ T1] Call Trace: [ 9.849798][ T1] <TASK> [ 9.849798][ T1] dump_stack_lvl (dump_stack.c:94 dump_stack.c:120) [ 9.849798][ T1] print_address_description+0x70/0x300 [ 9.849798][ T1] print_report (kasan/report.c:482) [ 9.849798][ T1] ? __virt_addr_valid (linux/mmzone.h:2198 (discriminator 1) linux/mmzone.h:2280 (discriminator 1) x86/mm/physaddr.c:54 (discriminator 1)) [ 9.849798][ T1] ? ftrace_find_rec_direct (trace/ftrace.c:1172 (discriminator 2) trace/ftrace.c:2611 (discriminator 2)) [ 9.849798][ T1] kasan_report (kasan/report.c:595) [ 9.849798][ T1] ? ftrace_find_rec_direct (trace/ftrace.c:1172 (discriminator 2) trace/ftrace.c:2611 (discriminator 2)) [ 9.849798][ T1] ? __pfx_trace_selftest_dynamic_test_func (??:?) [ 9.849798][ T1] ftrace_find_rec_direct (trace/ftrace.c:1172 (discriminator 2) trace/ftrace.c:2611 (discriminator 2)) [ 9.849798][ T1] register_ftrace_direct (trace/ftrace.c:6057) [ 9.849798][ T1] ? __pfx_ftrace_stub_direct_tramp (x86/kernel/ftrace_64.S:318) [ 9.849798][ T1] trace_selftest_startup_function_graph (trace/trace_selftest.c:1139) [ 9.849798][ T1] ? __pfx_trace_selftest_startup_function_graph (trace/trace_selftest.c:754) [ 9.849798][ T1] run_tracer_selftest (trace/trace.c:1335) [ 9.849798][ T1] register_tracer (trace/trace.c:1375 trace/trace.c:1492) [ 9.849798][ T1] ? __pfx_init_graph_trace (trace/trace_functions_graph.c:1811) [ 9.849798][ T1] do_one_initcall (main.c:1347) [ 9.849798][ T1] ? __pfx_do_one_initcall (trace/events/initcall.h:10) [ 9.849798][ T1] ? asm_sysvec_apic_timer_interrupt (x86/include/asm/idtentry.h:569) [ 9.849798][ T1] ? ret_from_fork_asm (x86/entry/entry_64.S:245) [ 9.849798][ T1] do_initcalls (main.c:1409 (discriminator 1) main.c:1425 (discriminator 1)) [ 9.849798][ T1] kernel_init_freeable (main.c:1445 main.c:1658) [ 9.849798][ T1] ? __pfx_kernel_init_freeable (main.c:1626) [ 9.849798][ T1] ? __pfx_schedule_timeout (??:?) [ 9.849798][ T1] ? __pfx__raw_spin_lock_irq (locking/spinlock.c:183) [ 9.849798][ T1] ? __pfx_kernel_init (main.c:717) [ 9.849798][ T1] kernel_init (main.c:1548) [ 9.849798][ T1] ? __pfx_kernel_init (main.c:717) [ 9.849798][ T1] ret_from_fork (x86/kernel/process.c:158) [ 9.849798][ T1] ? __pfx_ret_from_fork (x86/include/asm/entry-common.h:54) [ 9.849798][ T1] ? switch_fpu (linux/instrumented.h:82 asm-generic/bitops/instrumented-non-atomic.h:141 linux/thread_info.h:133 linux/sched.h:2066 x86/include/asm/fpu/sched.h:34) [ 9.849798][ T1] ? __switch_to (x86/kernel/process_64.c:619) [ 9.849798][ T1] ? __switch_to_asm (x86/entry/entry_64.S:206) [ 9.849798][ T1] ? __pfx_kernel_init (main.c:717) [ 9.849798][ T1] ret_from_fork_asm (x86/entry/entry_64.S:245) [ 9.849798][ T1] </TASK> [ 9.849798][ T1] [ 9.849798][ T1] The buggy address belongs to the variable: [ 9.849798][ T1] empty_hash+0x28/0xa0 [ 9.849798][ T1] [ 9.849798][ T1] The buggy address belongs to the physical page: [ 9.849798][ T1] page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x77d95b [ 9.849798][ T1] flags: 0x17ffffc0002000(reserved|node=0|zone=2|lastcpupid=0x1fffff) [ 9.849798][ T1] raw: 0017ffffc0002000 ffffea001df656c8 ffffea001df656c8 0000000000000000 [ 9.849798][ T1] raw: 0000000000000000 0000000000000000 00000001ffffffff 0000000000000000 [ 9.849798][ T1] page dumped because: kasan: bad access detected [ 9.849798][ T1] [ 9.849798][ T1] Memory state around the buggy address: [ 9.849798][ T1] ffffffff9675bc00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 9.849798][ T1] ffffffff9675bc80: 00 00 00 00 00 00 f9 f9 f9 f9 f9 f9 00 00 00 00 [ 9.849798][ T1] >ffffffff9675bd00: f9 f9 f9 f9 00 00 00 00 00 f9 f9 f9 f9 f9 f9 f9 [ 9.849798][ T1] ^ [ 9.849798][ T1] ffffffff9675bd80: 00 02 f9 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00 [ 9.849798][ T1] ffffffff9675be00: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 [ 9.849798][ T1] ================================================================== [ 9.904835][ T1] Disabling lock debugging due to kernel taint
The kernel config and materials to reproduce are available at:
https://download.01.org/0day-ci/archive/20260522/[email protected]
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki