Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

kernel/panic: allocate taint string buffer dynamically

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.

Link: https://lkml.kernel.org/r/20260222140804.22225-1-rioo.tsukatsukii@gmail.com
Signed-off-by: Rio <rioo.tsukatsukii@gmail.com>
Cc: Joel Granados <joel.granados@kernel.org>
Cc: Petr Mladek <pmladek@suse.com>
Cc: Wang Jinchao <wangjinchao600@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Rio and committed by
Andrew Morton
a9dff0d0 a75d2079

+46 -5
+46 -5
kernel/panic.c
··· 802 802 * small shell script that prints the TAINT_FLAGS_COUNT bits of 803 803 * /proc/sys/kernel/tainted. 804 804 * 805 - * Also, update TAINT_BUF_MAX below. 805 + * Also, update INIT_TAINT_BUF_MAX below. 806 806 */ 807 807 const struct taint_flag taint_flags[TAINT_FLAGS_COUNT] = { 808 808 TAINT_FLAG(PROPRIETARY_MODULE, 'P', 'G'), ··· 856 856 } 857 857 } 858 858 859 - /* 350 can accommodate all taint flags in verbose mode, with some headroom */ 860 - #define TAINT_BUF_MAX 350 859 + /* The initial buffer can accommodate all taint flags in verbose 860 + * mode, with some headroom. Once the allocator is available, the 861 + * exact size is allocated dynamically; the initial buffer remains 862 + * as a fallback if allocation fails. 863 + * 864 + * The verbose taint string currently requires up to 327 characters. 865 + */ 866 + #define INIT_TAINT_BUF_MAX 350 867 + 868 + static char init_taint_buf[INIT_TAINT_BUF_MAX]; 869 + static char *taint_buf = init_taint_buf; 870 + static size_t taint_buf_size = INIT_TAINT_BUF_MAX; 871 + 872 + static __init int alloc_taint_buf(void) 873 + { 874 + int i; 875 + char *buf; 876 + size_t size = 0; 877 + 878 + size += sizeof("Tainted: ") - 1; 879 + for (i = 0; i < TAINT_FLAGS_COUNT; i++) { 880 + size += 2; /* For ", " */ 881 + size += 4; /* For "[%c]=" */ 882 + size += strlen(taint_flags[i].desc); 883 + } 884 + 885 + size += 1; /* For NULL terminator */ 886 + 887 + buf = kmalloc(size, GFP_KERNEL); 888 + 889 + if (!buf) { 890 + /* Allocation may fail; this warning explains possibly 891 + * truncated taint strings 892 + */ 893 + pr_warn_once("taint string buffer allocation failed, using fallback buffer\n"); 894 + return 0; 895 + } 896 + 897 + taint_buf = buf; 898 + taint_buf_size = size; 899 + 900 + return 0; 901 + } 902 + postcore_initcall(alloc_taint_buf); 861 903 862 904 static const char *_print_tainted(bool verbose) 863 905 { 864 - static char buf[TAINT_BUF_MAX]; 865 906 struct seq_buf s; 866 907 867 908 BUILD_BUG_ON(ARRAY_SIZE(taint_flags) != TAINT_FLAGS_COUNT); 868 909 869 - seq_buf_init(&s, buf, sizeof(buf)); 910 + seq_buf_init(&s, taint_buf, taint_buf_size); 870 911 871 912 print_tainted_seq(&s, verbose); 872 913