Skip to content

Commit b0975c4

Browse files
jognesspmladek
authored andcommitted
printk: adjust string limit macros
The various internal size limit macros have names and/or values that do not fit well to their current usage. Rename the macros so that their purpose is clear and, if needed, provide a more appropriate value. In general, the new macros and values will lead to less memory usage. The new macros are... PRINTK_MESSAGE_MAX: This is the maximum size for a formatted message on a console, devkmsg, or syslog. It does not matter which format the message has (normal or extended). It replaces the use of CONSOLE_EXT_LOG_MAX for console and devkmsg. It replaces the use of CONSOLE_LOG_MAX for syslog. Historically, normal messages have been allowed to print up to 1kB, whereas extended messages have been allowed to print up to 8kB. However, the difference in lengths of these message types is not significant and in multi-line records, normal messages are probably larger. Also, because 1kB is only slightly above the allowed record size, multi-line normal messages could be easily truncated during formatting. This new macro should be significantly larger than the allowed record size to allow sufficient space for extended or multi-line prefix text. A value of 2kB should be plenty of space. For normal messages this represents a doubling of the historically allowed amount. For extended messages it reduces the excessive 8kB size, thus reducing memory usage needed for message formatting. PRINTK_PREFIX_MAX: This is the maximum size allowed for a record prefix (used by console and syslog). It replaces PREFIX_MAX. The value is left unchanged. PRINTKRB_RECORD_MAX: This is the maximum size allowed to be reserved for a record in the ringbuffer. It is used by all readers and writers with the printk ringbuffer. It replaces LOG_LINE_MAX. Previously this was set to "1kB - PREFIX_MAX", which makes some sense if 1kB is the limit for normal message output and prefixes are enabled. However, with the allowance of larger output and the existence of multi-line records, the value is rather bizarre. Round the value up to 1kB. Signed-off-by: John Ogness <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Signed-off-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent ea308da commit b0975c4

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

kernel/printk/internal.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write,
1515
#ifdef CONFIG_PRINTK
1616

1717
#ifdef CONFIG_PRINTK_CALLER
18-
#define PREFIX_MAX 48
18+
#define PRINTK_PREFIX_MAX 48
1919
#else
20-
#define PREFIX_MAX 32
20+
#define PRINTK_PREFIX_MAX 32
2121
#endif
2222

23-
/* the maximum size of a formatted record (i.e. with prefix added per line) */
24-
#define CONSOLE_LOG_MAX 1024
25-
26-
/* the maximum size of a formatted extended record */
27-
#define CONSOLE_EXT_LOG_MAX 8192
23+
/*
24+
* the maximum size of a formatted record (i.e. with prefix added
25+
* per line and dropped messages or in extended message format)
26+
*/
27+
#define PRINTK_MESSAGE_MAX 2048
2828

2929
/* the maximum size allowed to be reserved for a record */
30-
#define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX)
30+
#define PRINTKRB_RECORD_MAX 1024
3131

3232
/* Flags for a single printk record. */
3333
enum printk_info_flags {
@@ -63,10 +63,9 @@ u16 printk_parse_prefix(const char *text, int *level,
6363
enum printk_info_flags *flags);
6464
#else
6565

66-
#define PREFIX_MAX 0
67-
#define CONSOLE_LOG_MAX 0
68-
#define CONSOLE_EXT_LOG_MAX 0
69-
#define LOG_LINE_MAX 0
66+
#define PRINTK_PREFIX_MAX 0
67+
#define PRINTK_MESSAGE_MAX 0
68+
#define PRINTKRB_RECORD_MAX 0
7069

7170
/*
7271
* In !PRINTK builds we still export console_sem
@@ -85,8 +84,8 @@ static inline bool printk_percpu_data_ready(void) { return false; }
8584
* @scratchbuf: Used as temporary ringbuffer reading and string-print space.
8685
*/
8786
struct printk_buffers {
88-
char outbuf[CONSOLE_EXT_LOG_MAX];
89-
char scratchbuf[LOG_LINE_MAX];
87+
char outbuf[PRINTK_MESSAGE_MAX];
88+
char scratchbuf[PRINTKRB_RECORD_MAX];
9089
};
9190

9291
/**

kernel/printk/printk.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ static ssize_t devkmsg_write(struct kiocb *iocb, struct iov_iter *from)
729729
size_t len = iov_iter_count(from);
730730
ssize_t ret = len;
731731

732-
if (!user || len > LOG_LINE_MAX)
732+
if (!user || len > PRINTKRB_RECORD_MAX)
733733
return -EINVAL;
734734

735735
/* Ignore when user logging is disabled. */
@@ -1127,7 +1127,7 @@ static unsigned int __init add_to_rb(struct printk_ringbuffer *rb,
11271127
return prb_record_text_space(&e);
11281128
}
11291129

1130-
static char setup_text_buf[LOG_LINE_MAX] __initdata;
1130+
static char setup_text_buf[PRINTKRB_RECORD_MAX] __initdata;
11311131

11321132
void __init setup_log_buf(int early)
11331133
{
@@ -1393,7 +1393,7 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
13931393
size_t text_len = r->info->text_len;
13941394
size_t buf_size = r->text_buf_size;
13951395
char *text = r->text_buf;
1396-
char prefix[PREFIX_MAX];
1396+
char prefix[PRINTK_PREFIX_MAX];
13971397
bool truncated = false;
13981398
size_t prefix_len;
13991399
size_t line_len;
@@ -1492,7 +1492,7 @@ static size_t get_record_print_text_size(struct printk_info *info,
14921492
unsigned int line_count,
14931493
bool syslog, bool time)
14941494
{
1495-
char prefix[PREFIX_MAX];
1495+
char prefix[PRINTK_PREFIX_MAX];
14961496
size_t prefix_len;
14971497

14981498
prefix_len = info_print_prefix(info, syslog, time, prefix);
@@ -1558,11 +1558,11 @@ static int syslog_print(char __user *buf, int size)
15581558
int len = 0;
15591559
u64 seq;
15601560

1561-
text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL);
1561+
text = kmalloc(PRINTK_MESSAGE_MAX, GFP_KERNEL);
15621562
if (!text)
15631563
return -ENOMEM;
15641564

1565-
prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
1565+
prb_rec_init_rd(&r, &info, text, PRINTK_MESSAGE_MAX);
15661566

15671567
mutex_lock(&syslog_lock);
15681568

@@ -1663,7 +1663,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
16631663
u64 seq;
16641664
bool time;
16651665

1666-
text = kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL);
1666+
text = kmalloc(PRINTK_MESSAGE_MAX, GFP_KERNEL);
16671667
if (!text)
16681668
return -ENOMEM;
16691669

@@ -1675,7 +1675,7 @@ static int syslog_print_all(char __user *buf, int size, bool clear)
16751675
seq = find_first_fitting_seq(latched_seq_read_nolock(&clear_seq), -1,
16761676
size, true, time);
16771677

1678-
prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX);
1678+
prb_rec_init_rd(&r, &info, text, PRINTK_MESSAGE_MAX);
16791679

16801680
len = 0;
16811681
prb_for_each_record(seq, prb, seq, &r) {
@@ -2198,8 +2198,8 @@ int vprintk_store(int facility, int level,
21982198
reserve_size = vsnprintf(&prefix_buf[0], sizeof(prefix_buf), fmt, args2) + 1;
21992199
va_end(args2);
22002200

2201-
if (reserve_size > LOG_LINE_MAX)
2202-
reserve_size = LOG_LINE_MAX;
2201+
if (reserve_size > PRINTKRB_RECORD_MAX)
2202+
reserve_size = PRINTKRB_RECORD_MAX;
22032203

22042204
/* Extract log level or control flags. */
22052205
if (facility == 0)
@@ -2213,7 +2213,7 @@ int vprintk_store(int facility, int level,
22132213

22142214
if (flags & LOG_CONT) {
22152215
prb_rec_init_wr(&r, reserve_size);
2216-
if (prb_reserve_in_last(&e, prb, &r, caller_id, LOG_LINE_MAX)) {
2216+
if (prb_reserve_in_last(&e, prb, &r, caller_id, PRINTKRB_RECORD_MAX)) {
22172217
text_len = printk_sprint(&r.text_buf[r.info->text_len], reserve_size,
22182218
facility, &flags, fmt, args);
22192219
r.info->text_len += text_len;
@@ -2725,7 +2725,7 @@ static void console_prepend_dropped(struct printk_message *pmsg, unsigned long d
27252725
* It is a rather theoretical problem when someone tries to
27262726
* use a minimalist buffer.
27272727
*/
2728-
if (WARN_ON_ONCE(len + PREFIX_MAX >= outbuf_sz))
2728+
if (WARN_ON_ONCE(len + PRINTK_PREFIX_MAX >= outbuf_sz))
27292729
return;
27302730

27312731
if (pmsg->outbuf_len + len >= outbuf_sz) {

0 commit comments

Comments
 (0)