mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v5] ring-buffer: Do not check si_mem_available() before deferred init completes
@ 2026-11-18 18:32 Amit Machhiwal
  0 siblings, 0 replies; only message in thread
From: Amit Machhiwal @ 2026-11-18 18:32 UTC (permalink / raw)
  To: Steven Rostedt, Masami Hiramatsu, linux-trace-kernel, linuxppc-dev
  Cc: Amit Machhiwal, Michal Suchánek, Vincent Donnefort,
	Mathieu Desnoyers, Madhavan Srinivasan, Ritesh Harjani (IBM),
	Harsh Prateek Bora, linux-kernel, kvm, kvm-ppc, stable

With CONFIG_DEFERRED_STRUCT_PAGE_INIT=y, only about one memory section
per node is initialized early in boot (16 MiB on powerpc).  The rest is
initialized on demand by deferred_grow_zone() when an allocation would
otherwise fail, or later by page_alloc_init_late().

Until then NR_FREE_PAGES can be close to zero, because early allocations
(SLUB, vmalloc, ftrace records) use up the first section.
si_mem_available() is derived from NR_FREE_PAGES, so it under-reports.

__rb_allocate_pages() returns -ENOMEM when si_mem_available() is below
the requested size.  This check wrongly fails the buffers allocated for
the boot CPU in early_trace_init() and for secondary CPUs in smp_init(),
both of which run before page_alloc_init_late().  The resulting boot log
shows:

  ERROR: Failed to allocate trace buffer
  ERROR: tracer: failed to allocate ring buffer!

Skip the check until SYSTEM_FREEING_INITMEM on
CONFIG_DEFERRED_STRUCT_PAGE_INIT kernels.  The allocation itself still
fails if memory really is short.

This is the second half of the fix for the early boot crash reported on
ppc64le KVM guests.  Commit b3b974008388 ("tracing: Do not initialize
tracefs work if tracing is disabled") stopped the NULL pointer
dereference in __find_event_file() that followed the failed allocation.
This patch fixes the allocation failure itself.

Fixes: 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")
Cc: stable@vger.kernel.org
Reported-by: Michal Suchánek <msuchanek@suse.de>
Closes: https://lore.kernel.org/all/arYskzbiaNzBR9MD@kunlun.suse.cz/
Reviewed-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Amit Machhiwal <amachhiw@linux.ibm.com>
---
Changes since v4:
 - v4: https://lore.kernel.org/all/20261023145459.47516-1-amachhiw@linux.ibm.com/
 - Rewrote commit message for clarity and conciseness (Vincent, Steven).
 - Structure the deferred init check using if/else with unlikely() (Steven).
 - Added Vincent's R-b.

Changes since v3:
 - v3: https://lore.kernel.org/all/20261022181817.49717-1-amachhiw@linux.ibm.com
 - Replace #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT / #else / #endif with
   IS_ENABLED() + i = LONG_MAX to avoid duplicating the threshold check.
   (Vincent Donnefort)
 - Trim the in-code comment to one sentence. (Vincent Donnefort)

Changes since v2:
 - v2: https://lore.kernel.org/all/20261016134247.70722-1-amachhiw@linux.ibm.com
 - 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:
 - v1: https://lore.kernel.org/all/20261009163557.88467-1-amachhiw@linux.ibm.com
 - 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 | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c
index 75cd8df23c1b..98e8a02b7c43 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -2468,8 +2468,17 @@ 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, si_mem_available()
+		 * will reflect the actual available memory only after
+		 * SYSTEM_FREEING_INITMEM.
 		 */
-		i = si_mem_available();
+		if (IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT) &&
+		    unlikely(system_state < SYSTEM_FREEING_INITMEM))
+			i = LONG_MAX;
+		else
+			i = si_mem_available();
+
 		if (i < (nr_pages << subbuf_order))
 			return -ENOMEM;
 

base-commit: 20eef29b7c8465ac98d7dd6d6f2ac07a8d8056ca
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-18 18:33 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-18 18:32 [PATCH v5] ring-buffer: Do not check si_mem_available() before deferred init completes Amit Machhiwal

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®