Skip to content

Commit 98a0465

Browse files
jognesspmladek
authored andcommitted
printk: fix illegal pbufs access for !CONFIG_PRINTK
When CONFIG_PRINTK is not set, PRINTK_MESSAGE_MAX is 0. This leads to a zero-sized array @outbuf in @printk_shared_pbufs. In console_flush_all() a pointer to the first element of the array is assigned with: char *outbuf = &printk_shared_pbufs.outbuf[0]; For !CONFIG_PRINTK this leads to a compiler warning: warning: array subscript 0 is outside array bounds of 'char[0]' [-Warray-bounds] This is not really dangerous because printk_get_next_message() always returns false for !CONFIG_PRINTK, which leads to @outbuf never being used. However, it makes no sense to even compile these functions for !CONFIG_PRINTK. Extend the existing '#ifdef CONFIG_PRINTK' block to contain the formatting and emitting functions since these have no purpose in !CONFIG_PRINTK. This also allows removing several more !CONFIG_PRINTK dummies as well as moving @suppress_panic_printk into a CONFIG_PRINTK block. Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202309201724.M9BMAQIh-lkp@intel.com/ Signed-off-by: John Ogness <john.ogness@linutronix.de> Reviewed-by: Sergey Senozhatsky <senozhatsky@chromium.org> Signed-off-by: Petr Mladek <pmladek@suse.com> Link: https://lore.kernel.org/r/20230920155238.670439-1-john.ogness@linutronix.de
1 parent 9757acd commit 98a0465

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

kernel/printk/printk.c

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ DEFINE_STATIC_SRCU(console_srcu);
102102
*/
103103
int __read_mostly suppress_printk;
104104

105-
/*
106-
* During panic, heavy printk by other CPUs can delay the
107-
* panic and risk deadlock on console resources.
108-
*/
109-
static int __read_mostly suppress_panic_printk;
110-
111105
#ifdef CONFIG_LOCKDEP
112106
static struct lockdep_map console_lock_dep_map = {
113107
.name = "console_lock"
@@ -445,6 +439,12 @@ static int console_msg_format = MSG_FORMAT_DEFAULT;
445439
static DEFINE_MUTEX(syslog_lock);
446440

447441
#ifdef CONFIG_PRINTK
442+
/*
443+
* During panic, heavy printk by other CPUs can delay the
444+
* panic and risk deadlock on console resources.
445+
*/
446+
static int __read_mostly suppress_panic_printk;
447+
448448
DECLARE_WAIT_QUEUE_HEAD(log_wait);
449449
/* All 3 protected by @syslog_lock. */
450450
/* the next printk record to read by syslog(READ) or /proc/kmsg */
@@ -2346,22 +2346,6 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
23462346

23472347
static u64 syslog_seq;
23482348

2349-
static size_t record_print_text(const struct printk_record *r,
2350-
bool syslog, bool time)
2351-
{
2352-
return 0;
2353-
}
2354-
static ssize_t info_print_ext_header(char *buf, size_t size,
2355-
struct printk_info *info)
2356-
{
2357-
return 0;
2358-
}
2359-
static ssize_t msg_print_ext_body(char *buf, size_t size,
2360-
char *text, size_t text_len,
2361-
struct dev_printk_info *dev_info) { return 0; }
2362-
static void console_lock_spinning_enable(void) { }
2363-
static int console_lock_spinning_disable_and_check(int cookie) { return 0; }
2364-
static bool suppress_message_printing(int level) { return false; }
23652349
static bool pr_flush(int timeout_ms, bool reset_on_progress) { return true; }
23662350
static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; }
23672351

@@ -2715,6 +2699,8 @@ static void __console_unlock(void)
27152699
up_console_sem();
27162700
}
27172701

2702+
#ifdef CONFIG_PRINTK
2703+
27182704
/*
27192705
* Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". This
27202706
* is achieved by shifting the existing message over and inserting the dropped
@@ -2729,7 +2715,6 @@ static void __console_unlock(void)
27292715
*
27302716
* If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated.
27312717
*/
2732-
#ifdef CONFIG_PRINTK
27332718
void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
27342719
{
27352720
struct printk_buffers *pbufs = pmsg->pbufs;
@@ -2761,9 +2746,6 @@ void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped)
27612746
memcpy(outbuf, scratchbuf, len);
27622747
pmsg->outbuf_len += len;
27632748
}
2764-
#else
2765-
#define console_prepend_dropped(pmsg, dropped)
2766-
#endif /* CONFIG_PRINTK */
27672749

27682750
/*
27692751
* Read and format the specified record (or a later record if the specified
@@ -2921,6 +2903,16 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co
29212903
return true;
29222904
}
29232905

2906+
#else
2907+
2908+
static bool console_emit_next_record(struct console *con, bool *handover, int cookie)
2909+
{
2910+
*handover = false;
2911+
return false;
2912+
}
2913+
2914+
#endif /* CONFIG_PRINTK */
2915+
29242916
/*
29252917
* Print out all remaining records to all consoles.
29262918
*

0 commit comments

Comments
 (0)