Skip to contentSkip to conversation

kernel/panic: increase buffer size for verbose taint logging

PATCH Applied Rio 2 participants 8 messages Feb 20 – Feb 23

lore
Share & tools
lore

Rio

author
#0 lore raw

PATCH 1/1 kernel/panic: increase buffer size for verbose taint logging

The verbose 'Tainted: ...' string in print_tainted_seq can total to 327 characters while the buffer defined in _print_tainted is 320 bytes. Increase its size to 350 characters to hold all flags, along with some headroom.

---
 kernel/panic.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
kernel/panic.c +4 -2
index c78600212b6c..42b03fdd6ffa 100644
@@ -854,10 +854,12 @@ static void print_tainted_seq(struct seq_buf *s, bool verbose)
 	}
 }
 
+/* 350 can accomadate all taint flags in verbose mode, with some headroom */
+#define TAINT_BUF_MAX 350
+
 static const char *_print_tainted(bool verbose)
 {
-	/* FIXME: what should the size be? */
-	static char buf[sizeof(taint_flags)];
+	static char buf[TAINT_BUF_MAX];
 	struct seq_buf s;
 
 	BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT);
Signature · 1 line
2.52.0
#1 lore raw
Quoted 10 lines · On Fri, 20 Feb 2026 20:45:00 +0530 Rio <[email protected]> wrote:

The verbose 'Tainted: ...' string in print_tainted_seq can total to 327 characters while the buffer defined in _print_tainted is 320 bytes. Increase its size to 350 characters to hold all flags, along with some headroom.

...

kernel/panic.c static void print_tainted_seq(struct seq_buf *s, bool verbose) · line 857

Quoted 4 lines
kernel/panic.c +1 -0
@@ -854,10 +854,12 @@ static void print_tainted_seq(struct seq_buf *s, bool verbose)
 	}
 }
 
+/* 350 can accomadate all taint flags in verbose mode, with some headroom */

"accommodate". I'll fix this.

kernel/panic.c static void print_tainted_seq(struct seq_buf *s, bool verbose) · line 860

Quoted 6 lines
kernel/panic.c +2 -2
+#define TAINT_BUF_MAX 350
+
 static const char *_print_tainted(bool verbose)
 {
-	/* FIXME: what should the size be? */
-	static char buf[sizeof(taint_flags)];

Well that was weird.

kernel/panic.c static void print_tainted_seq(struct seq_buf *s, bool verbose) · line 862

Quoted 1 line
kernel/panic.c +1 -0
+	static char buf[TAINT_BUF_MAX];

Can you think of a way to do this more flexibly? Something which scales as we add more flags?

Probably not, as it's dependent upon the length of the various taint_flags.desc strings.

kernel/panic.c static void print_tainted_seq(struct seq_buf *s, bool verbose) · line 865

Quoted 3 lines
kernel/panic.c +0 -0
 	struct seq_buf s;
 
 	BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT);

How does this look?

kernel/panic.c renamed from kernel/panic.c~kernel-panic-increase-buffer-size-for-verbose-taint-logging-fix +3 -1
@@ -801,6 +801,8 @@ EXPORT_SYMBOL(panic);
  * Documentation/admin-guide/tainted-kernels.rst, including its
  * small shell script that prints the TAINT_FLAGS_COUNT bits of
  * /proc/sys/kernel/tainted.
+ *
+ * Also, update TAINT_BUF_MAX below.
  */
 const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
 	TAINT_FLAG(PROPRIETARY_MODULE,		'P', 'G'),
@@ -854,7 +856,7 @@ static void print_tainted_seq(struct seq
 	}
 }
 
-/* 350 can accomadate all taint flags in verbose mode, with some headroom */
+/* 350 can accommodate all taint flags in verbose mode, with some headroom */
 #define TAINT_BUF_MAX 350
 
 static const char *_print_tainted(bool verbose)

_

author
#2 lore raw
Quoted 1 line

"accommodate". I'll fix this.

Thanks

kernel/panic.c static void print_tainted_seq(struct seq · line 860

Quoted 9 lines

How does this look?

+ *
+ * Also, update TAINT_BUF_MAX below.
*/
...

kernel/panic.c renamed from kernel/panic.c~kernel-panic-increase-buffer-size-for-verbose-taint-logging-fix +1 -1
-/* 350 can accomadate all taint flags in verbose mode, with some headroom */
+/* 350 can accommodate all taint flags in verbose mode, with some headroom */
 #define TAINT_BUF_MAX 350

Looks good, though it might help future maintainers if we document the current maximum length of the verbose string (excluding the trailing NULL). Happy to send a small follow-up patch if needed.

#3 lore raw
Quoted 6 lines · On Sat, 21 Feb 2026 12:26:03 +0530 Rio <[email protected]> wrote:
Quoted 1 line

"accommodate". I'll fix this.

Thanks

Quoted 1 line

How does this look?

Quoted 11 lines

kernel/panic.c static void print_tainted_seq(struct seq · line 860

Quoted 7 lines

+ *
+ * Also, update TAINT_BUF_MAX below.
*/
...

kernel/panic.c renamed from kernel/panic.c~kernel-panic-increase-buffer-size-for-verbose-taint-logging-fix +1 -1
-/* 350 can accomadate all taint flags in verbose mode, with some headroom */
+/* 350 can accommodate all taint flags in verbose mode, with some headroom */
 #define TAINT_BUF_MAX 350

Looks good, though it might help future maintainers if we document the current maximum length of the verbose string (excluding the trailing NULL). Happy to send a small follow-up patch if needed.

OK, how about this idea.

- Allocating static storage is annoying: dynamic allocation is better

- Now knowing how large to make the storage is also annoying.

So:

- define a static buffer, mark it __initdata so it gets released later in boot. Make print_tainted() use that, initially, so print_tainted() is available at initial bootup time.

- write a new __init function which adds up all string lengths in taint_flags[] and calculates the precise maximum needed buffer size.

Allocate that much memory (kmalloc) and switch print_tainted() over to using that buffer.

- When __init data gets released, the initial static buffer and the new __init function get thrown away.

author
#4 lore raw

PATCH v2 1/1 kernel/panic: allocate taint string buffer dynamically

In reply to Rio

The buffer used to hold the taint string is statically allocated, which requires updating whenever a new taint flag is added.

Instead, allocate the exact required length at boot once the allocator is available in an init function. The allocation sums the string lengths in taint_flags[], along with space for separators and formatting. print_tainted() is switched to use this dynamically allocated buffer.

If allocation fails, print_tainted() warns about the failure and continues to use the original static buffer as a fallback.

---
V1 -> V2: Allocate taint string buffer dynamically during init

v1: https://lore.kernel.org/all/[email protected]/

 kernel/panic.c | 51 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 46 insertions(+), 5 deletions(-)
kernel/panic.c +46 -5
index 75368738d32d..5d498ff8a18b 100644
@@ -802,7 +802,7 @@ EXPORT_SYMBOL(panic);
  * small shell script that prints the TAINT_FLAGS_COUNT bits of
  * /proc/sys/kernel/tainted.
  *
- * Also, update TAINT_BUF_MAX below.
+ * Also, update INIT_TAINT_BUF_MAX below.
  */
 const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
 	TAINT_FLAG(PROPRIETARY_MODULE,		'P', 'G'),
@@ -856,17 +856,58 @@ static void print_tainted_seq(struct seq_buf *s, bool verbose)
 	}
 }
 
-/* 350 can accommodate all taint flags in verbose mode, with some headroom */
-#define TAINT_BUF_MAX 350
+/* The initial buffer can accommodate all taint flags in verbose
+ * mode, with some headroom. Once the allocator is available, the
+ * exact size is allocated dynamically; the initial buffer remains
+ * as a fallback if allocation fails.
+ *
+ * The verbose taint string currently requires up to 327 characters.
+ */
+#define INIT_TAINT_BUF_MAX 350
+
+static char init_taint_buf[INIT_TAINT_BUF_MAX];
+static char *taint_buf = init_taint_buf;
+static size_t taint_buf_size = INIT_TAINT_BUF_MAX;
+
+static __init int alloc_taint_buf(void)
+{
+	int i;
+	char *buf;
+	size_t size = 0;
+
+	size += sizeof("Tainted: ") - 1;
+	for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
+		size += 2; /* For ", " */
+		size += 4; /* For "[%c]=" */
+		size += strlen(taint_flags[i].desc);
+	}
+
+	size += 1; /* For NULL terminator */
+
+	buf = kmalloc(size, GFP_KERNEL);
+
+	if (!buf) {
+		/* Allocation may fail; this warning explains possibly
+		 * truncated taint strings
+		 */
+		pr_warn_once("taint string buffer allocation failed, using fallback buffer\n");
+		return 0;
+	}
+
+	taint_buf = buf;
+	taint_buf_size = size;
+
+	return 0;
+}
+postcore_initcall(alloc_taint_buf);
 
 static const char *_print_tainted(bool verbose)
 {
-	static char buf[TAINT_BUF_MAX];
 	struct seq_buf s;
 
 	BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT);
 
-	seq_buf_init(&s, buf, sizeof(buf));
+	seq_buf_init(&s, taint_buf, taint_buf_size);
 
 	print_tainted_seq(&s, verbose);
 
Signature · 1 line
2.52.0
author
#5 lore raw

Re: [PATCHv2] kernel/panic: allocate taint string buffer dynamically

This follow-up provides some tradeoffs between v1 and v2

- v1 used a fixed-size static buffer, requiring manual updates whenever new strings are added to taint_flags[].
- v2 uses a fixed-size static buffer only for initial boot. Once kmalloc is available, it allocates a dynamically sized buffer based on the total length of strings in taint_flags[].

- v1 needs an update every time taint_flags[] is updated.
- v2 relies on the initial buffer as a fallback mechanism in case kmalloc fails. The initial buffer is currently large enough for all taint strings, but future updates to taint_flags[] may require adjustments.

- v1 is more predictable, but requires more manual maintenance.
- v2 is more convenient, but introduces subtle edge cases and potential allocation issues.

#6 lore raw

Re: [PATCHv2] kernel/panic: allocate taint string buffer dynamically

In reply to Rio
Quoted 11 lines · On Sun, 22 Feb 2026 19:38:04 +0530 Rio <[email protected]> wrote:

The buffer used to hold the taint string is statically allocated, which requires updating whenever a new taint flag is added.

Instead, allocate the exact required length at boot once the allocator is available in an init function. The allocation sums the string lengths in taint_flags[], along with space for separators and formatting. print_tainted() is switched to use this dynamically allocated buffer.

If allocation fails, print_tainted() warns about the failure and continues to use the original static buffer as a fallback.

Lovely, thanks.

Quoted 18 lines
kernel/panic.c +1 -3
@@ -802,7 +802,7 @@ EXPORT_SYMBOL(panic);
  * small shell script that prints the TAINT_FLAGS_COUNT bits of
  * /proc/sys/kernel/tainted.
  *
- * Also, update TAINT_BUF_MAX below.
+ * Also, update INIT_TAINT_BUF_MAX below.
  */
 const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = {
 	TAINT_FLAG(PROPRIETARY_MODULE,		'P', 'G'),
@@ -856,17 +856,58 @@ static void print_tainted_seq(struct seq_buf *s, bool verbose)
 	}
 }
 
-/* 350 can accommodate all taint flags in verbose mode, with some headroom */
-#define TAINT_BUF_MAX 350

kernel/panic.c static void print_tainted_seq(struct seq_buf *s, bool verbose) · line 868

Quoted 10 lines
kernel/panic.c +10 -0
+/* The initial buffer can accommodate all taint flags in verbose
+ * mode, with some headroom. Once the allocator is available, the
+ * exact size is allocated dynamically; the initial buffer remains
+ * as a fallback if allocation fails.
+ *
+ * The verbose taint string currently requires up to 327 characters.
+ */
+#define INIT_TAINT_BUF_MAX 350
+
+static char init_taint_buf[INIT_TAINT_BUF_MAX];

OK, this cannot be __initdata because

Quoted 16 lines
kernel/panic.c +16 -0
+static char *taint_buf = init_taint_buf;
+static size_t taint_buf_size = INIT_TAINT_BUF_MAX;
+
+static __init int alloc_taint_buf(void)
+{
+	int i;
+	char *buf;
+	size_t size = 0;
+
+	size += sizeof("Tainted: ") - 1;
+	for (i = 0; i < TAINT_FLAGS_COUNT; i++) {
+		size += 2; /* For ", " */
+		size += 4; /* For "[%c]=" */
+		size += strlen(taint_flags[i].desc);
+	}
+

kernel/panic.c static void print_tainted_seq(struct seq_buf *s, bool verbose) · line 894

Quoted 10 lines
kernel/panic.c +10 -0
+	size += 1; /* For NULL terminator */
+
+	buf = kmalloc(size, GFP_KERNEL);
+
+	if (!buf) {
+		/* Allocation may fail; this warning explains possibly
+		 * truncated taint strings
+		 */
+		pr_warn_once("taint string buffer allocation failed, using fallback buffer\n");
+		return 0;

We may end up using it after boot time.

kernel/panic.c static void print_tainted_seq(struct seq_buf *s, bool verbose) · line 902

Quoted 8 lines
kernel/panic.c +8 -0
+	}
+
+	taint_buf = buf;
+	taint_buf_size = size;
+
+	return 0;
+}
+postcore_initcall(alloc_taint_buf);

However there's a convention of assuming that __init-time allocations cannot fail. Because if a kmalloc() were to fail at this time, the kernel is hopelessly messed up anyway.

So we could simply panic() if that kmalloc failed, then make that 350-byte buffer __initdata.

That saves about $100 per machine with current DRAM prices ;)

author
#7 lore raw

PATCH v3 1/1 kernel/panic: mark init_taint_buf as __initdata and panic instead of warning in alloc_taint_buf()

In reply to Rio
Quoted 5 lines · Andrew Morton

However there's a convention of assuming that __init-time allocations cannot fail. Because if a kmalloc() were to fail at this time, the kernel is hopelessly messed up anyway.
So we could simply panic() if that kmalloc failed, then make that 350-byte buffer __initdata.

Ah, got it. I made it __initdata, and replaced the warn with panic.

Quoted 1 line

That saves about $100 per machine with current DRAM prices ;)

Haha, saving every bit counts huh

---
V2 -> V3: Mark initial buffer as __initdata and panic if allocation fails
V1 -> V2: Allocate taint string buffer dynamically during init

v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/

 kernel/panic.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
kernel/panic.c +3 -7
index 5d498ff8a18b..20feada5319d 100644
@@ -865,8 +865,8 @@ static void print_tainted_seq(struct seq_buf *s, bool verbose)
  */
 #define INIT_TAINT_BUF_MAX 350
 
-static char init_taint_buf[INIT_TAINT_BUF_MAX];
-static char *taint_buf = init_taint_buf;
+static char init_taint_buf[INIT_TAINT_BUF_MAX] __initdata;
+static char *taint_buf __refdata = init_taint_buf;
 static size_t taint_buf_size = INIT_TAINT_BUF_MAX;
 
 static __init int alloc_taint_buf(void)
@@ -887,11 +887,7 @@ static __init int alloc_taint_buf(void)
 	buf = kmalloc(size, GFP_KERNEL);
 
 	if (!buf) {
-		/* Allocation may fail; this warning explains possibly
-		 * truncated taint strings
-		 */
-		pr_warn_once("taint string buffer allocation failed, using fallback buffer\n");
-		return 0;
+		panic("Failed to allocate taint string buffer");
 	}
 
 	taint_buf = buf;
Signature · 1 line
2.52.0