mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Rio <rioo.tsukatsukii@gmail.com>
Cc: feng.tang@linux.alibaba.com, joel.granados@kernel.org,
	linux-kernel@vger.kernel.org, pmladek@suse.com,
	pnina.feder@mobileye.com, wangjinchao600@gmail.com
Subject: Re: [PATCHv2] kernel/panic: allocate taint string buffer dynamically
Date: Sun, 22 Feb 2026 11:25:52 -0800	[thread overview]
Message-ID: <20260222112552.c28915733dff5ad01ac3c2fc@linux-foundation.org> (raw)
In-Reply-To: <20260222140804.22225-1-rioo.tsukatsukii@gmail.com>

On Sun, 22 Feb 2026 19:38:04 +0530 Rio <rioo.tsukatsukii@gmail.com> 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.

> 
> --- a/kernel/panic.c
> +++ b/kernel/panic.c
> @@ -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];

OK, this cannot be __initdata because

> +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;

We may end up using it after boot time.

> +	}
> +
> +	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 ;)



  parent reply	other threads:[~2026-02-22 19:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-20 15:15 [PATCH] kernel/panic: increase buffer size for verbose taint logging Rio
2026-02-20 21:06 ` Andrew Morton
2026-02-21  6:56   ` Rio
2026-02-21 20:03     ` Andrew Morton
2026-02-22 14:08 ` [PATCHv2] kernel/panic: allocate taint string buffer dynamically Rio
2026-02-22 15:23   ` Rio
2026-02-22 19:25   ` Andrew Morton [this message]
2026-02-23  3:59   ` [PATCHv3] kernel/panic: mark init_taint_buf as __initdata and panic instead of warning in alloc_taint_buf() Rio

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=20260222112552.c28915733dff5ad01ac3c2fc@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=feng.tang@linux.alibaba.com \
    --cc=joel.granados@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pmladek@suse.com \
    --cc=pnina.feder@mobileye.com \
    --cc=rioo.tsukatsukii@gmail.com \
    --cc=wangjinchao600@gmail.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

all inboxes | Powered by JetHome®