tracing: Fix early boot trace buffer allocation and tracefs init crash
Subsystems (4)
cover tracing: Fix early boot trace buffer allocation and tracefs init crash
This series addresses an early boot crash and trace buffer allocation failure reported on ppc64le KVM guests [1] (and reproducible under CONFIG_DEFERRED_STRUCT_PAGE_INIT=y with large static kernel footprints).
Patch 1 fixes the crash (NULL pointer dereference in __find_event_file()):
When trace buffer allocation fails during early boot, tracing is left disabled. However, tracer_init_tracefs_work_func() runs asynchronously via fs_initcall and traverses uninitialized event lists without checking tracing_disabled. Checking tracing_disabled early averts the crash.
Patch 2 fixes the root cause of the early allocation failure:
During early_trace_init(), __rb_allocate_pages() performs a heuristic check using si_mem_available(). Under CONFIG_DEFERRED_STRUCT_PAGE_INIT=y, NR_FREE_PAGES only reflects the initial non-deferred pool (e.g. 1 section per node) because the remaining memory has not yet been initialized. On kernels with large static binary footprints (debug configs, early SLUB/vmalloc/ftrace records), this pool is quickly consumed, causing si_mem_available() to return 0 and trigger a false -ENOMEM before the buddy allocator has the opportunity to grow the zone on demand via deferred_grow_zone(). Skipping the check during SYSTEM_BOOTING allows the allocation to succeed.
Both fixes have been verified by Michal Suchánek.
[1] https://lore.kernel.org/all/[email protected]/
Amit Machhiwal (2):
tracing: Do not initialize tracefs work if tracing is disabled
ring-buffer: Do not check si_mem_available() during SYSTEM_BOOTING
kernel/trace/ring_buffer.c | 8 +++++++- kernel/trace/trace.c | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-)
base-commit: af32da41b0327b9c6a37856ba82b6760d6c8d10e
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH 1/2 tracing: Do not initialize tracefs work if tracing is disabled
If trace buffer allocation fails during early boot (e.g. in early_trace_init() -> allocate_trace_buffers()), tracing_disabled is left set to 1 and the global trace array events list remains uninitialized.
Later during boot, tracer_init_tracefs() (invoked via fs_initcall) queues tracerfs_init_work asynchronously without checking if tracing was disabled. When tracer_init_tracefs_work_func() runs, it calls event_trace_init() and init_tracer_tracefs(&global_trace, NULL) unconditionally. This leads to __find_event_file() dereferencing an uninitialized tr->events list, causing an early boot NULL pointer dereference Oops:
BUG: Kernel NULL pointer dereference on read at 0x00000010 Faulting instruction address: 0xc000000000485be0 Oops: Kernel access of bad area, sig: 7 [#1] ... NIP [c000000000485be0] __find_event_file+0x70/0x3c0 LR [c000000000448874] init_tracer_tracefs+0x274/0xc80 Call Trace: init_tracer_tracefs+0x274/0xc80 tracer_init_tracefs_work_func+0x50/0x320 process_one_work+0x1e8/0x5c0 worker_thread+0x1dc/0x3d0 kthread+0x194/0x1b0 start_kernel_thread+0x14/0x18
Check tracing_disabled at the beginning of tracer_init_tracefs_work_func() and return immediately if tracing is disabled or failed to initialize, preventing the crash.
--- kernel/trace/trace.c | 6 ++++++ 1 file changed, 6 insertions(+)
kernel/trace/trace.c +6 -0
@@ -9287,6 +9287,12 @@ static struct notifier_block trace_module_nb = {static __init void tracer_init_tracefs_work_func (struct work_struct *work) { +/* +* Do not attempt to initialize tracefs if tracing was disabled or +* failed to allocate +*/ +if (tracing_disabled) +return ;event_trace_init ();
Signature · 1 line
2.54.0 (Apple Git-157)
Re: [PATCH 1/2] tracing: Do not initialize tracefs work if tracing is disabled
Quoted 27 lines · Amit Machhiwal
If trace buffer allocation fails during early boot (e.g. in
early_trace_init()->allocate_trace_buffers()),tracing_disabledis left set to 1 and the global trace array events list remains uninitialized.Later during boot,
tracer_init_tracefs()(invoked viafs_initcall) queuestracerfs_init_workasynchronously without checking if tracing was disabled. Whentracer_init_tracefs_work_func()runs, it callsevent_trace_init()andinit_tracer_tracefs(&global_trace, NULL)unconditionally. This leads to__find_event_file()dereferencing an uninitializedtr->eventslist, causing an early boot NULL pointer dereference Oops:BUG: Kernel NULL pointer dereference on read at 0x00000010 Faulting instruction address: 0xc000000000485be0 Oops: Kernel access of bad area, sig: 7 [#1] ... NIP [c000000000485be0] __find_event_file+0x70/0x3c0 LR [c000000000448874] init_tracer_tracefs+0x274/0xc80 Call Trace: init_tracer_tracefs+0x274/0xc80 tracer_init_tracefs_work_func+0x50/0x320 process_one_work+0x1e8/0x5c0 worker_thread+0x1dc/0x3d0 kthread+0x194/0x1b0 start_kernel_thread+0x14/0x18
Quoted 3 lines
Check
tracing_disabledat the beginning oftracer_init_tracefs_work_func()and return immediately if tracing is disabled or failed to initialize, preventing the crash.
Thanks for the patch
Quoted 30 lines
Fixes 6621a7004684 ("tracing: make tracer_init_tracefs initcall asynchronous")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- kernel/trace/trace.c | 6 ++++++ 1 file changed, 6 insertions(+)kernel/trace/trace.c +6
-0@@ -9287,6 +9287,12 @@ static struct notifier_block trace_module_nb = {static __init void tracer_init_tracefs_work_func (struct work_struct *work) { +/* +* Do not attempt to initialize tracefs if tracing was disabled or +* failed to allocate +*/ +if (tracing_disabled) +return ;event_trace_init ();Signature · 1 line
2.54.0 (Apple Git-157)
Re: [PATCH 1/2] tracing: Do not initialize tracefs work if tracing is disabled
In reply to Amit MachhiwalQuoted 27 lines · Amit Machhiwal
If trace buffer allocation fails during early boot (e.g. in
early_trace_init()->allocate_trace_buffers()),tracing_disabledis left set to 1 and the global trace array events list remains uninitialized.Later during boot,
tracer_init_tracefs()(invoked viafs_initcall) queuestracerfs_init_workasynchronously without checking if tracing was disabled. Whentracer_init_tracefs_work_func()runs, it callsevent_trace_init()andinit_tracer_tracefs(&global_trace, NULL)unconditionally. This leads to__find_event_file()dereferencing an uninitializedtr->eventslist, causing an early boot NULL pointer dereference Oops:BUG: Kernel NULL pointer dereference on read at 0x00000010 Faulting instruction address: 0xc000000000485be0 Oops: Kernel access of bad area, sig: 7 [#1] ... NIP [c000000000485be0] __find_event_file+0x70/0x3c0 LR [c000000000448874] init_tracer_tracefs+0x274/0xc80 Call Trace: init_tracer_tracefs+0x274/0xc80 tracer_init_tracefs_work_func+0x50/0x320 process_one_work+0x1e8/0x5c0 worker_thread+0x1dc/0x3d0 kthread+0x194/0x1b0 start_kernel_thread+0x14/0x18
Quoted 4 lines
Check
tracing_disabledat the beginning oftracer_init_tracefs_work_func()and return immediately if tracing is disabled or failed to initialize, preventing the crash.
Looks good to me.
Quoted 29 lines
Fixes 6621a7004684 ("tracing: make tracer_init_tracefs initcall asynchronous")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- kernel/trace/trace.c | 6 ++++++ 1 file changed, 6 insertions(+)kernel/trace/trace.c +6
-0@@ -9287,6 +9287,12 @@ static struct notifier_block trace_module_nb = {static __init void tracer_init_tracefs_work_func (struct work_struct *work) { +/* +* Do not attempt to initialize tracefs if tracing was disabled or +* failed to allocate +*/ +if (tracing_disabled) +return ;event_trace_init ();Signature · 1 line
2.54.0 (Apple Git-157)
Thanks!
Signature · 1 line
Masami Hiramatsu (Google) <[email protected]>
PATCH 2/2 ring-buffer: Do not check si_mem_available() during SYSTEM_BOOTING
In reply to Amit MachhiwalDuring early boot (early_trace_init() called from start_kernel()), tracing allocates initial ring buffers (temp_buffer, array_buffer, and snapshot_buffer) for CPU 0.
Before attempting page allocation, __rb_allocate_pages() performs a heuristic check using si_mem_available() to return early with -ENOMEM if memory appears insufficient.
However, on kernels built with CONFIG_DEFERRED_STRUCT_PAGE_INIT=y, defer_init() leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front. On kernels with large static binary footprints (such as debug configurations enabling PAGE_OWNER, DEBUG_PAGEALLOC, KFENCE, or SLUB_DEBUG), the static kernel image and early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this initial pool.
At T=0.000000, watermarks have not yet been established (totalreserve_pages = 0), so si_mem_available() returns the raw free page count (often 0-1 pages). When the snapshot buffer or global trace buffer attempts to allocate 2 sub-pages, si_mem_available() returns < nr_pages and prematurely aborts with -ENOMEM.
This failure is false: if the page allocation were actually attempted via alloc_pages_node(), the page allocator would trigger deferred_grow_zone() on demand to initialise additional deferred memory sections. Checking si_mem_available() before attempting the allocation short-circuits this on-demand growth.
Skip the si_mem_available() check when system_state == SYSTEM_BOOTING. Once the system transitions past early boot and page_alloc_init_late() initialises all deferred memory, si_mem_available() accurately reflects system-wide free memory and the check operates as intended for runtime allocations.
--- kernel/trace/ring_buffer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
kernel/trace/ring_buffer.c +7 -1
@@ -2452,9 +2452,15 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* Skip this check during early boot: with CONFIG_DEFERRED_STRUCT_PAGE_INIT, +* NR_FREE_PAGES only reflects the initial non-deferred pool at this +* stage. si_mem_available() returns a false negative while actual +* allocations succeed by growing the zone on demand via +* deferred_grow_zone(). */ i =si_mem_available (); -if (i < nr_pages) +if (system_state !=SYSTEM_BOOTING && i < nr_pages)return -ENOMEM ;/*
Signature · 1 line
2.54.0 (Apple Git-157)
[email protected]
AI reviewRe: [PATCH 2/2] ring-buffer: Do not check si_mem_available() during SYSTEM_BOOTING
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] trace/ring_buffer: SMP boot failure due to premature si_mem_available() check --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] trace/ring_buffer: SMP boot failure due to premature si_mem_available() check --
commit b21cf9115adfbd1fd082e26d147a363a37bad846
Author: Amit Machhiwal <[email protected]>
ring-buffer: Do not check si_mem_available() during SYSTEM_BOOTING
This commit modifies early boot tracing allocation to bypass the si_mem_available() check when system_state is SYSTEM_BOOTING. It aims to prevent premature -ENOMEM failures on systems with deferred page initialization.
Quoted 8 lines
kernel/trace/ring_buffer.c +0
-0@@ -2452,9 +2452,15 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed.
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2463
Quoted 10 lines
kernel/trace/ring_buffer.c +7
-1+* +* Skip this check during early boot: with CONFIG_DEFERRED_STRUCT_PAGE_INIT, +* NR_FREE_PAGES only reflects the initial non-deferred pool at this +* stage. si_mem_available() returns a false negative while actual +* allocations succeed by growing the zone on demand via +* deferred_grow_zone(). */ i =si_mem_available (); -if (i < nr_pages) +if (system_state !=SYSTEM_BOOTING && i < nr_pages)
[Severity: High]
Could this condition cause an SMP boot failure by re-enabling the check too early?
The system transitions to SYSTEM_SCHEDULING in rest_init() before the init thread starts. When the init thread runs kernel_init_freeable(), it sets up secondary CPUs via smp_init() before deferred memory is initialized:
init/main.c:kernel_init_freeable() {
...
smp_init();
...
page_alloc_init_late();
...
}When smp_init() brings up a CPU, it triggers the CPU hotplug callback trace_rb_cpu_prepare(), which eventually calls __rb_allocate_pages().
Since system_state is now SYSTEM_SCHEDULING (not SYSTEM_BOOTING) when __rb_allocate_pages() is called, the new check evaluates to true and si_mem_available() executes. Because page_alloc_init_late() hasn't run yet, deferred memory is still uninitialized, which means si_mem_available() will return a false negative and abort the allocation with -ENOMEM.
Would this failure propagate through the CPU hotplug state machine and abort the bring-up of secondary CPUs when CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled?
Quoted 3 lines
kernel/trace/ring_buffer.c +0
-0return -ENOMEM ;/*
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH 2/2] ring-buffer: Do not check si_mem_available() during SYSTEM_BOOTING
In reply to Amit MachhiwalQuoted 28 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0.Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.However, on kernels built with
CONFIG_DEFERRED_STRUCT_PAGE_INIT=y,defer_init()leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front. On kernels with large static binary footprints (such as debug configurations enablingPAGE_OWNER,DEBUG_PAGEALLOC, KFENCE, orSLUB_DEBUG), the static kernel image and early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this initial pool.At T=0.000000, watermarks have not yet been established (
totalreserve_pages= 0), sosi_mem_available()returns the raw free page count (often 0-1 pages). When the snapshot buffer or global trace buffer attempts to allocate 2 sub-pages,si_mem_available()returns <nr_pagesand prematurely aborts with-ENOMEM.This failure is false: if the page allocation were actually attempted via
alloc_pages_node(), the page allocator would triggerdeferred_grow_zone()on demand to initialise additional deferred memory sections. Checkingsi_mem_available()before attempting the allocation short-circuits this on-demand growth.
Quoted 5 lines
Skip the
si_mem_available()check whensystem_state==SYSTEM_BOOTING. Once the system transitions past early boot andpage_alloc_init_late()initialises all deferred memory,si_mem_available()accurately reflects system-wide free memory and the check operates as intended for runtime allocations.
Quoted 35 lines
Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- kernel/trace/ring_buffer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)kernel/trace/ring_buffer.c +7
-1@@ -2452,9 +2452,15 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* Skip this check during early boot: with CONFIG_DEFERRED_STRUCT_PAGE_INIT, +* NR_FREE_PAGES only reflects the initial non-deferred pool at this +* stage. si_mem_available() returns a false negative while actual +* allocations succeed by growing the zone on demand via +* deferred_grow_zone(). */ i =si_mem_available (); -if (i < nr_pages) +if (system_state !=SYSTEM_BOOTING && i < nr_pages)return -ENOMEM ;/* Signature · 1 line
2.54.0 (Apple Git-157)
Vincent Donnefort
maintainerRe: [PATCH 2/2] ring-buffer: Do not check si_mem_available() during SYSTEM_BOOTING
In reply to Amit MachhiwalQuoted 48 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0.Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.However, on kernels built with
CONFIG_DEFERRED_STRUCT_PAGE_INIT=y,defer_init()leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front. On kernels with large static binary footprints (such as debug configurations enablingPAGE_OWNER,DEBUG_PAGEALLOC, KFENCE, orSLUB_DEBUG), the static kernel image and early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this initial pool.At T=0.000000, watermarks have not yet been established (
totalreserve_pages= 0), sosi_mem_available()returns the raw free page count (often 0-1 pages). When the snapshot buffer or global trace buffer attempts to allocate 2 sub-pages,si_mem_available()returns <nr_pagesand prematurely aborts with-ENOMEM.This failure is false: if the page allocation were actually attempted via
alloc_pages_node(), the page allocator would triggerdeferred_grow_zone()on demand to initialise additional deferred memory sections. Checkingsi_mem_available()before attempting the allocation short-circuits this on-demand growth.Skip the
si_mem_available()check whensystem_state==SYSTEM_BOOTING. Once the system transitions past early boot andpage_alloc_init_late()initialises all deferred memory,si_mem_available()accurately reflects system-wide free memory and the check operates as intended for runtime allocations.Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- kernel/trace/ring_buffer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)kernel/trace/ring_buffer.c +0
-0
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2460
Quoted 9 lines
kernel/trace/ring_buffer.c +6
-0@@ -2452,9 +2452,15 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* Skip this check during early boot: with CONFIG_DEFERRED_STRUCT_PAGE_INIT, +* NR_FREE_PAGES only reflects the initial non-deferred pool at this +* stage. si_mem_available() returns a false negative while actual +* allocations succeed by growing the zone on demand via +* deferred_grow_zone().
As Sashiko pointed-out, if si_mem_available() is only ready after page_alloc_init_late() when CONFIG_DEFERRED_STRUCT_PAGE_INIT, then checking for system_state >= SYSTEM_FREEING_INITMEM might be more correct.
Also I wonder if we don't want to guard with CONFIG_DEFERRED_STRUCT_PAGE_INIT, to keep the verification when CONFIG_DEFERRED_STRUCT_PAGE_INIT is unset.
Quoted 11 lines
kernel/trace/ring_buffer.c +1
-1*/ i =si_mem_available (); -if (i < nr_pages) +if (system_state !=SYSTEM_BOOTING && i < nr_pages)return -ENOMEM ;/* Signature · 1 line
2.54.0 (Apple Git-157)
Signature · 1 line
Vincent
Re: [PATCH 2/2] ring-buffer: Do not check si_mem_available() during SYSTEM_BOOTING
Hi Vincent,
Thanks for reviewing this patch.
Quoted 63 lines · Vincent Donnefort
Quoted 48 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0.Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.However, on kernels built with
CONFIG_DEFERRED_STRUCT_PAGE_INIT=y,defer_init()leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front. On kernels with large static binary footprints (such as debug configurations enablingPAGE_OWNER,DEBUG_PAGEALLOC, KFENCE, orSLUB_DEBUG), the static kernel image and early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this initial pool.At T=0.000000, watermarks have not yet been established (
totalreserve_pages= 0), sosi_mem_available()returns the raw free page count (often 0-1 pages). When the snapshot buffer or global trace buffer attempts to allocate 2 sub-pages,si_mem_available()returns <nr_pagesand prematurely aborts with-ENOMEM.This failure is false: if the page allocation were actually attempted via
alloc_pages_node(), the page allocator would triggerdeferred_grow_zone()on demand to initialise additional deferred memory sections. Checkingsi_mem_available()before attempting the allocation short-circuits this on-demand growth.Skip the
si_mem_available()check whensystem_state==SYSTEM_BOOTING. Once the system transitions past early boot andpage_alloc_init_late()initialises all deferred memory,si_mem_available()accurately reflects system-wide free memory and the check operates as intended for runtime allocations.Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>--- kernel/trace/ring_buffer.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)kernel/trace/ring_buffer.c +0
-0kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2460
Quoted 9 lines
kernel/trace/ring_buffer.c +6
-0@@ -2452,9 +2452,15 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* Skip this check during early boot: with CONFIG_DEFERRED_STRUCT_PAGE_INIT, +* NR_FREE_PAGES only reflects the initial non-deferred pool at this +* stage. si_mem_available() returns a false negative while actual +* allocations succeed by growing the zone on demand via +* deferred_grow_zone(). As Sashiko pointed-out, if
si_mem_available()is only ready afterpage_alloc_init_late()whenCONFIG_DEFERRED_STRUCT_PAGE_INIT, then checking forsystem_state>=SYSTEM_FREEING_INITMEMmight be more correct.
Quoted 2 lines
Also I wonder if we don't want to guard with
CONFIG_DEFERRED_STRUCT_PAGE_INIT, to keep the verification whenCONFIG_DEFERRED_STRUCT_PAGE_INITis unset.
Both suggestions adopted in v2 — using system_state >= SYSTEM_FREEING_INITMEM and wrapping in #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT.
Thanks,
Amit
PATCH v2 1/1 ring-buffer: Do not check si_mem_available() before deferred init completes
In reply to Amit MachhiwalDuring early boot (early_trace_init() called from start_kernel()), tracing allocates initial ring buffers (temp_buffer, array_buffer, and snapshot_buffer) for CPU 0. Later, as secondary CPUs come online during smp_init(), each CPU's ring buffer is allocated via trace_rb_cpu_prepare().
Before attempting page allocation, __rb_allocate_pages() performs a heuristic check using si_mem_available() to return early with -ENOMEM if memory appears insufficient.
However, on kernels built with CONFIG_DEFERRED_STRUCT_PAGE_INIT=y, NR_FREE_PAGES only reflects the initial non-deferred pool across the entire period from early_trace_init() through smp_init(). defer_init() leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front, and on kernels with large static binary footprints (debug configurations enabling PAGE_OWNER, DEBUG_PAGEALLOC, KFENCE, or SLUB_DEBUG), early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this pool.
si_mem_available() therefore returns a false negative (often 0-1 pages) across both SYSTEM_BOOTING (for the initial CPU 0 buffers) and SYSTEM_SCHEDULING (for secondary CPU buffers via trace_rb_cpu_prepare()), causing __rb_allocate_pages() to prematurely abort with -ENOMEM.
This failure is false: the buddy allocator is designed to replenish ZONE_NORMAL on demand via deferred_grow_zone() when an actual allocation is attempted. Checking si_mem_available() before calling alloc_pages_node() short-circuits this on-demand growth entirely.
deferred_grow_zone() is only permanently disabled after page_alloc_init_late() completes (it spawns per-node kthreads and blocks until done), which happens inside kernel_init_freeable() — before system_state is set to SYSTEM_FREEING_INITMEM. Skip the si_mem_available() check until SYSTEM_FREEING_INITMEM, at which point all deferred pages are initialized and si_mem_available() is accurate.
Wrap the guard in #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT so that on kernels without deferred init the original check is preserved unchanged.
---
Changes since v1:
- Extend the guard to cover SYSTEM_SCHEDULING as well: smp_init() calls trace_rb_cpu_prepare() -> __rb_allocate_pages() before page_alloc_init_late() completes, so the false negative window spans both SYSTEM_BOOTING and SYSTEM_SCHEDULING. Use system_state >= SYSTEM_FREEING_INITMEM as the correct boundary. (Sashiko, Vincent Donnefort)
- Wrap in #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT to preserve the original check unchanged on non-deferred kernels. (Vincent Donnefort)
- Drop Tested-by: guard condition changed, requesting fresh test.
kernel/trace/ring_buffer.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
kernel/trace/ring_buffer.c +14 -0
@@ -2452,10 +2452,24 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only reflects +* the initial non-deferred pool during early boot and throughout SMP +* bring-up (smp_init() runs before page_alloc_init_late() completes +* deferred initialization). si_mem_available() returns a false negative +* across this entire window while actual allocations succeed by growing +* the zone on demand via deferred_grow_zone(). Only re-enable the check +* from SYSTEM_FREEING_INITMEM onward, after page_alloc_init_late() has +* finished and si_mem_available() is accurate. */ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && i < nr_pages) +return -ENOMEM ; +#else if (i < nr_pages)return -ENOMEM ; +#endif /* * If a user thread allocates too much, and si_mem_available()
base-commit: 2f2f7eeef8b0a1a9b9ca998231419873ccae8d4e
Signature · 1 line
2.54.0 (Apple Git-157)
[email protected]
AI reviewRe: [PATCH v2] ring-buffer: Do not check si_mem_available() before deferred init completes
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] mm/trace: OOM panic via GFP_KERNEL metadata allocations --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] mm/trace: OOM panic via GFP_KERNEL metadata allocations
--
commit 5164ff519a3f7b78b5e2f07fc662c8cbbef0267e
Author: Amit Machhiwal <[email protected]>
ring-buffer: Do not check si_mem_available() before deferred init completes
During early boot, si_mem_available() can return a false negative on kernels built with CONFIG_DEFERRED_STRUCT_PAGE_INIT. Skip the si_mem_available() check until SYSTEM_FREEING_INITMEM, when deferred pages are fully initialized.
Quoted 12 lines
kernel/trace/ring_buffer.c +4
-0@@ -2452,10 +2452,24 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only reflects +* the initial non-deferred pool during early boot and throughout SMP +* bring-up (smp_init() runs before page_alloc_init_late() completes
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2468
Quoted 10 lines
kernel/trace/ring_buffer.c +8
-0+* deferred initialization). si_mem_available() returns a false negative +* across this entire window while actual allocations succeed by growing +* the zone on demand via deferred_grow_zone(). Only re-enable the check +* from SYSTEM_FREEING_INITMEM onward, after page_alloc_init_late() has +* finished and si_mem_available() is accurate. */ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && i < nr_pages) +return -ENOMEM ;
[Severity: High]
Does bypassing the si_mem_available() check during early boot expose the kernel to an out of memory panic?
By skipping this check, __rb_allocate_pages() will proceed to the allocation loop even if the requested nr_pages exceeds available memory. The loop contains metadata allocations via alloc_cpu_page():
kernel/trace/ring_buffer.c:__rb_allocate_pages() {
...
for (i = 0; i < nr_pages; i++) {
bpage = alloc_cpu_page(cpu_buffer->cpu);
if (!bpage)
goto free_pages;
...
}alloc_cpu_page() uses GFP_KERNEL without __GFP_RETRY_MAYFAIL:
#define alloc_cpu_page(cpu) (struct buffer_page *) \
kzalloc_node(ALIGN(sizeof(struct buffer_page), \
cache_line_size()), GFP_KERNEL, cpu_to_node(cpu))If the loop exhausts the buddy allocator, won't the slab allocator's subsequent attempt to allocate a new page for the metadata struct using GFP_KERNEL trigger a kernel panic during early boot?
Quoted 7 lines
kernel/trace/ring_buffer.c +2
-0+#else if (i < nr_pages)return -ENOMEM ; +#endif /* * If a user thread allocates too much, and si_mem_available()
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2] ring-buffer: Do not check si_mem_available() before deferred init completes
In reply to Amit MachhiwalQuoted 88 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0. Later, as secondary CPUs come online duringsmp_init(), each CPU's ring buffer is allocated viatrace_rb_cpu_prepare().Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.However, on kernels built with
CONFIG_DEFERRED_STRUCT_PAGE_INIT=y,NR_FREE_PAGESonly reflects the initial non-deferred pool across the entire period fromearly_trace_init()throughsmp_init().defer_init()leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front, and on kernels with large static binary footprints (debug configurations enablingPAGE_OWNER,DEBUG_PAGEALLOC, KFENCE, orSLUB_DEBUG), early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this pool.
si_mem_available()therefore returns a false negative (often 0-1 pages) across bothSYSTEM_BOOTING(for the initial CPU 0 buffers) andSYSTEM_SCHEDULING(for secondary CPU buffers viatrace_rb_cpu_prepare()), causing__rb_allocate_pages()to prematurely abort with-ENOMEM.This failure is false: the buddy allocator is designed to replenish
ZONE_NORMALon demand viadeferred_grow_zone()when an actual allocation is attempted. Checkingsi_mem_available()before callingalloc_pages_node()short-circuits this on-demand growth entirely.
deferred_grow_zone()is only permanently disabled afterpage_alloc_init_late()completes (it spawns per-node kthreads and blocks until done), which happens insidekernel_init_freeable()— beforesystem_stateis set toSYSTEM_FREEING_INITMEM. Skip thesi_mem_available()check untilSYSTEM_FREEING_INITMEM, at which point all deferred pages are initialized andsi_mem_available()is accurate.Wrap the guard in #ifdef
CONFIG_DEFERRED_STRUCT_PAGE_INITso that on kernels without deferred init the original check is preserved unchanged.Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>---
Changes since v1:
- Extend the guard to coverSYSTEM_SCHEDULINGas well:smp_init()callstrace_rb_cpu_prepare()->__rb_allocate_pages()beforepage_alloc_init_late()completes, so the false negative window spans bothSYSTEM_BOOTINGandSYSTEM_SCHEDULING. Usesystem_state>=SYSTEM_FREEING_INITMEMas the correct boundary. (Sashiko, Vincent Donnefort)
- Wrap in #ifdefCONFIG_DEFERRED_STRUCT_PAGE_INITto preserve the original check unchanged on non-deferred kernels. (Vincent Donnefort)
- Drop Tested-by: guard condition changed, requesting fresh test.kernel/trace/ring_buffer.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/trace/ring_buffer.c +14
-0@@ -2452,10 +2452,24 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only reflects +* the initial non-deferred pool during early boot and throughout SMP +* bring-up (smp_init() runs before page_alloc_init_late() completes +* deferred initialization). si_mem_available() returns a false negative +* across this entire window while actual allocations succeed by growing +* the zone on demand via deferred_grow_zone(). Only re-enable the check +* from SYSTEM_FREEING_INITMEM onward, after page_alloc_init_late() has +* finished and si_mem_available() is accurate. */ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && i < nr_pages) +return -ENOMEM ; +#else if (i < nr_pages)return -ENOMEM ; +#endif /* * If a user thread allocates too much, and si_mem_available()
Quoted 1 line
base-commit:
2f2f7eeef8b0a1a9b9ca998231419873ccae8d4e
Hello,
due to other conflicting changes this no longer applies.
Will you send a rebase?
Thanks
Michal
Re: [PATCH v2] ring-buffer: Do not check si_mem_available() before deferred init completes
Quoted 10 lines · Michal Suchánek
Quoted 9 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0. Later, as secondary CPUs come online duringsmp_init(), each CPU's ring buffer is allocated viatrace_rb_cpu_prepare().Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.
<snip>
Quoted 33 lines
Quoted 33 lines
kernel/trace/ring_buffer.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+)kernel/trace/ring_buffer.c +14
-0@@ -2452,10 +2452,24 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only reflects +* the initial non-deferred pool during early boot and throughout SMP +* bring-up (smp_init() runs before page_alloc_init_late() completes +* deferred initialization). si_mem_available() returns a false negative +* across this entire window while actual allocations succeed by growing +* the zone on demand via deferred_grow_zone(). Only re-enable the check +* from SYSTEM_FREEING_INITMEM onward, after page_alloc_init_late() has +* finished and si_mem_available() is accurate. */ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && i < nr_pages) +return -ENOMEM ; +#else if (i < nr_pages)return -ENOMEM ; +#endif /* * If a user thread allocates too much, and si_mem_available()
Quoted 7 lines
Quoted 1 line
base-commit:
2f2f7eeef8b0a1a9b9ca998231419873ccae8d4eHello,
due to other conflicting changes this no longer applies.
Will you send a rebase?
Hi Michal,
Thanks for checking and notifying. I'll look into it and send a v3 as needed.
Thanks,
Amit
PATCH v3 1/1 ring-buffer: Do not check si_mem_available() before deferred init completes
In reply to Amit MachhiwalDuring early boot (early_trace_init() called from start_kernel()), tracing allocates initial ring buffers (temp_buffer, array_buffer, and snapshot_buffer) for CPU 0. Later, as secondary CPUs come online during smp_init(), each CPU's ring buffer is allocated via trace_rb_cpu_prepare().
Before attempting page allocation, __rb_allocate_pages() performs a heuristic check using si_mem_available() to return early with -ENOMEM if memory appears insufficient.
However, on kernels built with CONFIG_DEFERRED_STRUCT_PAGE_INIT=y, NR_FREE_PAGES only reflects the initial non-deferred pool across the entire period from early_trace_init() through smp_init(). defer_init() leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front, and on kernels with large static binary footprints (debug configurations enabling PAGE_OWNER, DEBUG_PAGEALLOC, KFENCE, or SLUB_DEBUG), early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this pool.
si_mem_available() therefore returns a false negative (often 0-1 pages) across both SYSTEM_BOOTING (for the initial CPU 0 buffers) and SYSTEM_SCHEDULING (for secondary CPU buffers via trace_rb_cpu_prepare()), causing __rb_allocate_pages() to prematurely abort with -ENOMEM.
This failure is false: the buddy allocator is designed to replenish ZONE_NORMAL on demand via deferred_grow_zone() when an actual allocation is attempted. Checking si_mem_available() before calling alloc_pages_node() short-circuits this on-demand growth entirely.
deferred_grow_zone() is only permanently disabled after page_alloc_init_late() completes (it spawns per-node kthreads and blocks until done), which happens inside kernel_init_freeable() — before system_state is set to SYSTEM_FREEING_INITMEM. Skip the si_mem_available() check until SYSTEM_FREEING_INITMEM, at which point all deferred pages are initialized and si_mem_available() is accurate.
Wrap the guard in #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT so that on kernels without deferred init the original check is preserved unchanged.
---
Changes since v2:
- Rebase on top of upstream ring_buffer.c changes from Vincent Donnefort; update threshold to i < (nr_pages << subbuf_order). (Michal Suchánek)
Changes since v1:
- Extend the guard to cover SYSTEM_SCHEDULING as well: smp_init() calls trace_rb_cpu_prepare() -> __rb_allocate_pages() before page_alloc_init_late() completes, so the false negative window spans both SYSTEM_BOOTING and SYSTEM_SCHEDULING. Use system_state >= SYSTEM_FREEING_INITMEM as the correct boundary. (Sashiko, Vincent Donnefort)
- Wrap in #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT to preserve the original check unchanged on non-deferred kernels. (Vincent Donnefort)
- Drop Tested-by: guard condition changed, requesting fresh test.
kernel/trace/ring_buffer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
kernel/trace/ring_buffer.c +17 -0
@@ -2463,10 +2463,27 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only +* reflects the initial non-deferred pool during early boot and +* throughout SMP bring-up (smp_init() runs before +* page_alloc_init_late() completes deferred initialization). +* si_mem_available() returns a false negative across this +* entire window while actual allocations succeed by growing the +* zone on demand via deferred_grow_zone(). Only re-enable the +* check from SYSTEM_FREEING_INITMEM onward, after +* page_alloc_init_late() has finished and si_mem_available() is +* accurate. */ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && + i < (nr_pages << subbuf_order)) +return -ENOMEM ; +#else if (i < (nr_pages << subbuf_order))return -ENOMEM ; +#endif user_thread = current->mm !=NULL ; }
base-commit: 7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614
Signature · 1 line
2.54.0 (Apple Git-157)
Vincent Donnefort
maintainerRe: [PATCH v3] ring-buffer: Do not check si_mem_available() before deferred init completes
Quoted 38 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0. Later, as secondary CPUs come online duringsmp_init(), each CPU's ring buffer is allocated viatrace_rb_cpu_prepare().Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.However, on kernels built with
CONFIG_DEFERRED_STRUCT_PAGE_INIT=y,NR_FREE_PAGESonly reflects the initial non-deferred pool across the entire period fromearly_trace_init()throughsmp_init().defer_init()leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front, and on kernels with large static binary footprints (debug configurations enablingPAGE_OWNER,DEBUG_PAGEALLOC, KFENCE, orSLUB_DEBUG), early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this pool.
si_mem_available()therefore returns a false negative (often 0-1 pages) across bothSYSTEM_BOOTING(for the initial CPU 0 buffers) andSYSTEM_SCHEDULING(for secondary CPU buffers viatrace_rb_cpu_prepare()), causing__rb_allocate_pages()to prematurely abort with-ENOMEM.This failure is false: the buddy allocator is designed to replenish
ZONE_NORMALon demand viadeferred_grow_zone()when an actual allocation is attempted. Checkingsi_mem_available()before callingalloc_pages_node()short-circuits this on-demand growth entirely.
deferred_grow_zone()is only permanently disabled afterpage_alloc_init_late()completes (it spawns per-node kthreads and blocks until done), which happens insidekernel_init_freeable()— beforesystem_stateis set toSYSTEM_FREEING_INITMEM. Skip thesi_mem_available()check untilSYSTEM_FREEING_INITMEM, at which point all deferred pages are initialized andsi_mem_available()is accurate.Wrap the guard in #ifdef
CONFIG_DEFERRED_STRUCT_PAGE_INITso that on kernels without deferred init the original check is preserved unchanged.
Quoted 6 lines
Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>
It is usually good practice to not use in-reply-to to link the series between each other.
Quoted 27 lines
---
Changes since v2:
- Rebase on top of upstreamring_buffer.cchanges from Vincent Donnefort; update threshold to i < (nr_pages<<subbuf_order). (Michal Suchánek)Changes since v1:
- Extend the guard to coverSYSTEM_SCHEDULINGas well:smp_init()callstrace_rb_cpu_prepare()->__rb_allocate_pages()beforepage_alloc_init_late()completes, so the false negative window spans bothSYSTEM_BOOTINGandSYSTEM_SCHEDULING. Usesystem_state>=SYSTEM_FREEING_INITMEMas the correct boundary. (Sashiko, Vincent Donnefort)
- Wrap in #ifdefCONFIG_DEFERRED_STRUCT_PAGE_INITto preserve the original check unchanged on non-deferred kernels. (Vincent Donnefort)
- Drop Tested-by: guard condition changed, requesting fresh test.kernel/trace/ring_buffer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)kernel/trace/ring_buffer.c +1
-0@@ -2463,10 +2463,27 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +*
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2476
Quoted 10 lines
kernel/trace/ring_buffer.c +10
-0+* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only +* reflects the initial non-deferred pool during early boot and +* throughout SMP bring-up (smp_init() runs before +* page_alloc_init_late() completes deferred initialization). +* si_mem_available() returns a false negative across this +* entire window while actual allocations succeed by growing the +* zone on demand via deferred_grow_zone(). Only re-enable the +* check from SYSTEM_FREEING_INITMEM onward, after +* page_alloc_init_late() has finished and si_mem_available() is +* accurate.
This is quite a massive comment for something simple: When CONFIG_DEFERRED_STRUCT_PAGE_INIT, si_mem_available() will reflect the actual available memory only after SYSTEM_FREEING_INITMEM.
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2486
Quoted 10 lines
kernel/trace/ring_buffer.c +6
-0*/ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && + i < (nr_pages << subbuf_order)) +return -ENOMEM ; +#else if (i < (nr_pages << subbuf_order))return -ENOMEM ; +#endif
Could we avoid the double i < (nr_pages << subbuf_order) with something like
if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT) &&
system_state < SYSTEM_FREEING_INITMEM)
i = LONG_MAX;
Quoted 8 lines
kernel/trace/ring_buffer.c +0
-0user_thread = current->mm !=NULL ; }base-commit:
7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614Signature · 1 line
2.54.0 (Apple Git-157)
Signature · 1 line
Vincent
Vincent Donnefort
maintainerRe: [PATCH v3] ring-buffer: Do not check si_mem_available() before deferred init completes
Quoted 104 lines · Vincent Donnefort
Quoted 38 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0. Later, as secondary CPUs come online duringsmp_init(), each CPU's ring buffer is allocated viatrace_rb_cpu_prepare().Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.However, on kernels built with
CONFIG_DEFERRED_STRUCT_PAGE_INIT=y,NR_FREE_PAGESonly reflects the initial non-deferred pool across the entire period fromearly_trace_init()throughsmp_init().defer_init()leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front, and on kernels with large static binary footprints (debug configurations enablingPAGE_OWNER,DEBUG_PAGEALLOC, KFENCE, orSLUB_DEBUG), early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this pool.
si_mem_available()therefore returns a false negative (often 0-1 pages) across bothSYSTEM_BOOTING(for the initial CPU 0 buffers) andSYSTEM_SCHEDULING(for secondary CPU buffers viatrace_rb_cpu_prepare()), causing__rb_allocate_pages()to prematurely abort with-ENOMEM.This failure is false: the buddy allocator is designed to replenish
ZONE_NORMALon demand viadeferred_grow_zone()when an actual allocation is attempted. Checkingsi_mem_available()before callingalloc_pages_node()short-circuits this on-demand growth entirely.
deferred_grow_zone()is only permanently disabled afterpage_alloc_init_late()completes (it spawns per-node kthreads and blocks until done), which happens insidekernel_init_freeable()— beforesystem_stateis set toSYSTEM_FREEING_INITMEM. Skip thesi_mem_available()check untilSYSTEM_FREEING_INITMEM, at which point all deferred pages are initialized andsi_mem_available()is accurate.Wrap the guard in #ifdef
CONFIG_DEFERRED_STRUCT_PAGE_INITso that on kernels without deferred init the original check is preserved unchanged.Quoted 6 lines
Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>It is usually good practice to not use in-reply-to to link the series between each other.
Quoted 27 lines
---
Changes since v2:
- Rebase on top of upstreamring_buffer.cchanges from Vincent Donnefort; update threshold to i < (nr_pages<<subbuf_order). (Michal Suchánek)Changes since v1:
- Extend the guard to coverSYSTEM_SCHEDULINGas well:smp_init()callstrace_rb_cpu_prepare()->__rb_allocate_pages()beforepage_alloc_init_late()completes, so the false negative window spans bothSYSTEM_BOOTINGandSYSTEM_SCHEDULING. Usesystem_state>=SYSTEM_FREEING_INITMEMas the correct boundary. (Sashiko, Vincent Donnefort)
- Wrap in #ifdefCONFIG_DEFERRED_STRUCT_PAGE_INITto preserve the original check unchanged on non-deferred kernels. (Vincent Donnefort)
- Drop Tested-by: guard condition changed, requesting fresh test.kernel/trace/ring_buffer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)kernel/trace/ring_buffer.c +1
-0@@ -2463,10 +2463,27 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2476
Quoted 10 lines
kernel/trace/ring_buffer.c +10
-0+* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only +* reflects the initial non-deferred pool during early boot and +* throughout SMP bring-up (smp_init() runs before +* page_alloc_init_late() completes deferred initialization). +* si_mem_available() returns a false negative across this +* entire window while actual allocations succeed by growing the +* zone on demand via deferred_grow_zone(). Only re-enable the +* check from SYSTEM_FREEING_INITMEM onward, after +* page_alloc_init_late() has finished and si_mem_available() is +* accurate. This is quite a massive comment for something simple: When
CONFIG_DEFERRED_STRUCT_PAGE_INIT,si_mem_available()will reflect the actual available memory only afterSYSTEM_FREEING_INITMEM.kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2486
Quoted 10 lines
kernel/trace/ring_buffer.c +6
-0*/ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && + i < (nr_pages << subbuf_order)) +return -ENOMEM ; +#else if (i < (nr_pages << subbuf_order))return -ENOMEM ; +#endif Could we avoid the double i < (
nr_pages<<subbuf_order) with something like
Quoted 4 lines
if (
IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)&&
system_state<SYSTEM_FREEING_INITMEM)
i =LONG_MAX;
or even i = totalram_pages()?
Quoted 11 lines
Quoted 8 lines
kernel/trace/ring_buffer.c +0
-0user_thread = current->mm !=NULL ; }base-commit:
7be4bc52fd9e0e55dc9a2e0232b89427d2c4b614Signature · 1 line
2.54.0 (Apple Git-157)Signature · 1 line
Vincent
Signature · 1 line
Vincent
Re: [PATCH v3] ring-buffer: Do not check si_mem_available() before deferred init completes
Hi Vincent,
Thanks for reviewing this version. Please find my response inline below.
Quoted 47 lines · Vincent Donnefort
Quoted 46 lines · Vincent Donnefort
Quoted 44 lines · Amit Machhiwal
During early boot (
early_trace_init()called fromstart_kernel()), tracing allocates initial ring buffers (temp_buffer,array_buffer, andsnapshot_buffer) for CPU 0. Later, as secondary CPUs come online duringsmp_init(), each CPU's ring buffer is allocated viatrace_rb_cpu_prepare().Before attempting page allocation,
__rb_allocate_pages()performs a heuristic check usingsi_mem_available()to return early with-ENOMEMif memory appears insufficient.However, on kernels built with
CONFIG_DEFERRED_STRUCT_PAGE_INIT=y,NR_FREE_PAGESonly reflects the initial non-deferred pool across the entire period fromearly_trace_init()throughsmp_init().defer_init()leaves only a single section per node (e.g. 16 MiB with 64 KB pages) initialized up-front, and on kernels with large static binary footprints (debug configurations enablingPAGE_OWNER,DEBUG_PAGEALLOC, KFENCE, orSLUB_DEBUG), early core initialisations (SLUB caches, vmalloc, static ftrace records) consume virtually all managed pages in this pool.
si_mem_available()therefore returns a false negative (often 0-1 pages) across bothSYSTEM_BOOTING(for the initial CPU 0 buffers) andSYSTEM_SCHEDULING(for secondary CPU buffers viatrace_rb_cpu_prepare()), causing__rb_allocate_pages()to prematurely abort with-ENOMEM.This failure is false: the buddy allocator is designed to replenish
ZONE_NORMALon demand viadeferred_grow_zone()when an actual allocation is attempted. Checkingsi_mem_available()before callingalloc_pages_node()short-circuits this on-demand growth entirely.
deferred_grow_zone()is only permanently disabled afterpage_alloc_init_late()completes (it spawns per-node kthreads and blocks until done), which happens insidekernel_init_freeable()— beforesystem_stateis set toSYSTEM_FREEING_INITMEM. Skip thesi_mem_available()check untilSYSTEM_FREEING_INITMEM, at which point all deferred pages are initialized andsi_mem_available()is accurate.Wrap the guard in #ifdef
CONFIG_DEFERRED_STRUCT_PAGE_INITso that on kernels without deferred init the original check is preserved unchanged.Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>
Quoted 2 lines
Quoted 2 lines
It is usually good practice to not use in-reply-to to link the series between each other.
Noted, thanks. Since the patch 1 from this series was already picked, I thought keeping the new versions of patch 2 may make sense. But will send v4 as a standalone thread.
Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
---
Changes since v2:
- Rebase on top of upstreamring_buffer.cchanges from Vincent Donnefort; update threshold to i < (nr_pages<<subbuf_order). (Michal Suchánek)Changes since v1:
- Extend the guard to coverSYSTEM_SCHEDULINGas well:smp_init()callstrace_rb_cpu_prepare()->__rb_allocate_pages()beforepage_alloc_init_late()completes, so the false negative window spans bothSYSTEM_BOOTINGandSYSTEM_SCHEDULING. Usesystem_state>=SYSTEM_FREEING_INITMEMas the correct boundary. (Sashiko, Vincent Donnefort)
- Wrap in #ifdefCONFIG_DEFERRED_STRUCT_PAGE_INITto preserve the original check unchanged on non-deferred kernels. (Vincent Donnefort)
- Drop Tested-by: guard condition changed, requesting fresh test.kernel/trace/ring_buffer.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)kernel/trace/ring_buffer.c +7
-0@@ -2463,10 +2463,27 @@ static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer,* memory. It may not be accurate. But we don't care, we just want * to prevent doing any allocation when it is obvious that it is * not going to succeed. +* +* With CONFIG_DEFERRED_STRUCT_PAGE_INIT, NR_FREE_PAGES only +* reflects the initial non-deferred pool during early boot and +* throughout SMP bring-up (smp_init() runs before +* page_alloc_init_late() completes deferred initialization). +* si_mem_available() returns a false negative across this +* entire window while actual allocations succeed by growing the
Quoted 8 lines
Quoted 8 lines
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2476
Quoted 4 lines
kernel/trace/ring_buffer.c +4
-0+* zone on demand via deferred_grow_zone(). Only re-enable the +* check from SYSTEM_FREEING_INITMEM onward, after +* page_alloc_init_late() has finished and si_mem_available() is +* accurate. This is quite a massive comment for something simple: When
CONFIG_DEFERRED_STRUCT_PAGE_INIT,si_mem_available()will reflect the actual available memory only afterSYSTEM_FREEING_INITMEM.
Agreed, will trim it down to that one sentence.
Quoted 14 lines
Quoted 14 lines
kernel/trace/ring_buffer.c static int __rb_allocate_pages(struct ring_buffer_per_cpu *cpu_buffer, · line 2486
Quoted 11 lines
kernel/trace/ring_buffer.c +6
-0*/ i =si_mem_available (); +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT +if (system_state >=SYSTEM_FREEING_INITMEM && + i < (nr_pages << subbuf_order)) +return -ENOMEM ; +#else if (i < (nr_pages << subbuf_order))return -ENOMEM ; +#endif Could we avoid the double i < (
nr_pages<<subbuf_order) with something like
Quoted 6 lines
Quoted 4 lines
if (
IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)&&
system_state<SYSTEM_FREEING_INITMEM)
i =LONG_MAX;or even i =
totalram_pages()?
The IS_ENABLED() pattern is cleaner, will adopt it. However, totalram_pages() is not safe as the sentinel here.
_totalram_pages is set by a single totalram_pages_add() call inside memblock_free_all(), which accumulates the total non-deferred page count from free_low_memory_core_early() and adds it in one shot. The deferred pages freed later by deferred_free_pages() -> __free_pages_core(MEMINIT_EARLY) take the else branch in __free_pages_core() which only updates zone->managed_pages directly — totalram_pages_add() is never called for them (the kernel comment there even says "memblock adjusts totalram_pages() manually"). As a result, totalram_pages() returns the same small initial-section value (~16 MiB on ppc64le) throughout the entire window, and setting i = totalram_pages() would still trigger the i < (nr_pages << subbuf_order) check falsely.
LONG_MAX is unambiguous — it unconditionally passes the threshold check regardless of nr_pages or subbuf_order, which is exactly the intent. Will use that in v4.
Thanks,
Amit
Steven Rostedt
maintainerRe: [PATCH v3] ring-buffer: Do not check si_mem_available() before deferred init completes
In reply to Vincent DonnefortQuoted 10 lines · On Fri, 23 Oct 2026 09:18:10 +0100 Vincent Donnefort <[email protected]>…
Quoted 7 lines
Fixes 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")Reported-by Michal Suchánek <[email protected]>Signed-off-by Amit Machhiwal <[email protected]>It is usually good practice to not use in-reply-to to link the series between each other.
Correct, the best thing to do is add a link to v2 below:
Quoted 3 lines
Quoted 3 lines
---
Changes since v2:
As in:
Changes since v2: https://lore.kernel.org/all/[email protected]/
As versions in threads of previous versions gets messing and confusing. And in most cases, just dropped entirely.
Quoted 3 lines
Quoted 3 lines
- Rebase on top of upstream
ring_buffer.cchanges from Vincent Donnefort; update threshold to i < (nr_pages<<subbuf_order). (Michal Suchánek)
-- Steve