Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: stable@vger.kernel.org
Cc: "Amit Machhiwal" <amachhiw@linux.ibm.com>,
	"Michal Suchánek" <msuchanek@suse.de>,
	"Vincent Donnefort" <vdonnefort@google.com>,
	"Steven Rostedt" <rostedt@goodmis.org>,
	"Sasha Levin" <sashal@kernel.org>
Subject: [PATCH 6.1.y] ring-buffer: Do not check si_mem_available() before deferred init completes
Date: Thu,  3 Dec 2026 23:39:26 -0500	[thread overview]
Message-ID: <20261204043705.2636295-1-sashal@kernel.org> (raw)
In-Reply-To: <2026120218-autism-encrypt-e49e@gregkh>

From: Amit Machhiwal <amachhiw@linux.ibm.com>

[ Upstream commit abf7185109d3986c1f945641d2fb20e44527f7c6 ]

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.

Cc: stable@vger.kernel.org
Fixes: 2a872fa4e9c8 ("ring-buffer: Check if memory is available before allocation")
Link: https://patch.msgid.link/20261118183235.63310-1-amachhiw@linux.ibm.com
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>
Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
[ sashal: Adapted the hunk to the older __rb_allocate_pages() code, where the si_mem_available() check compares against nr_pages with no subbuf order shift and sits at a different indentation. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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 a36f1e411d3b2..6f81707d942cb 100644
--- a/kernel/trace/ring_buffer.c
+++ b/kernel/trace/ring_buffer.c
@@ -1648,8 +1648,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)
 		return -ENOMEM;
 
-- 
2.53.0


      reply	other threads:[~2026-12-04  4:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 13:40 FAILED: patch "[PATCH] ring-buffer: Do not check si_mem_available() before deferred" failed to apply to 6.1-stable tree gregkh
2026-12-04  4:39 ` Sasha Levin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204043705.2636295-1-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=amachhiw@linux.ibm.com \
    --cc=msuchanek@suse.de \
    --cc=rostedt@goodmis.org \
    --cc=stable@vger.kernel.org \
    --cc=vdonnefort@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox