Skip to content

Commit ab6f762

Browse files
sergey-senozhatskytorvalds
authored andcommitted
printk: queue wake_up_klogd irq_work only if per-CPU areas are ready
printk_deferred(), similarly to printk_safe/printk_nmi, does not immediately attempt to print a new message on the consoles, avoiding calls into non-reentrant kernel paths, e.g. scheduler or timekeeping, which potentially can deadlock the system. Those printk() flavors, instead, rely on per-CPU flush irq_work to print messages from safer contexts. For same reasons (recursive scheduler or timekeeping calls) printk() uses per-CPU irq_work in order to wake up user space syslog/kmsg readers. However, only printk_safe/printk_nmi do make sure that per-CPU areas have been initialised and that it's safe to modify per-CPU irq_work. This means that, for instance, should printk_deferred() be invoked "too early", that is before per-CPU areas are initialised, printk_deferred() will perform illegal per-CPU access. Lech Perczak [0] reports that after commit 1b710b1 ("char/random: silence a lockdep splat with printk()") user-space syslog/kmsg readers are not able to read new kernel messages. The reason is printk_deferred() being called too early (as was pointed out by Petr and John). Fix printk_deferred() and do not queue per-CPU irq_work before per-CPU areas are initialized. Link: https://lore.kernel.org/lkml/[email protected]/ Reported-by: Lech Perczak <[email protected]> Signed-off-by: Sergey Senozhatsky <[email protected]> Tested-by: Jann Horn <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Theodore Ts'o <[email protected]> Cc: John Ogness <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 87ad46e commit ab6f762

File tree

5 files changed

+40
-16
lines changed

5 files changed

+40
-16
lines changed

include/linux/printk.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,6 @@ __printf(1, 2) void dump_stack_set_arch_desc(const char *fmt, ...);
202202
void dump_stack_print_info(const char *log_lvl);
203203
void show_regs_print_info(const char *log_lvl);
204204
extern asmlinkage void dump_stack(void) __cold;
205-
extern void printk_safe_init(void);
206205
extern void printk_safe_flush(void);
207206
extern void printk_safe_flush_on_panic(void);
208207
#else
@@ -269,10 +268,6 @@ static inline void dump_stack(void)
269268
{
270269
}
271270

272-
static inline void printk_safe_init(void)
273-
{
274-
}
275-
276271
static inline void printk_safe_flush(void)
277272
{
278273
}

init/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,6 @@ asmlinkage __visible void __init start_kernel(void)
913913
boot_init_stack_canary();
914914

915915
time_init();
916-
printk_safe_init();
917916
perf_event_init();
918917
profile_init();
919918
call_function_init();

kernel/printk/internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ __printf(1, 0) int vprintk_func(const char *fmt, va_list args);
2323
void __printk_safe_enter(void);
2424
void __printk_safe_exit(void);
2525

26+
void printk_safe_init(void);
27+
bool printk_percpu_data_ready(void);
28+
2629
#define printk_safe_enter_irqsave(flags) \
2730
do { \
2831
local_irq_save(flags); \
@@ -64,4 +67,6 @@ __printf(1, 0) int vprintk_func(const char *fmt, va_list args) { return 0; }
6467
#define printk_safe_enter_irq() local_irq_disable()
6568
#define printk_safe_exit_irq() local_irq_enable()
6669

70+
static inline void printk_safe_init(void) { }
71+
static inline bool printk_percpu_data_ready(void) { return false; }
6772
#endif /* CONFIG_PRINTK */

kernel/printk/printk.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,18 @@ static char __log_buf[__LOG_BUF_LEN] __aligned(LOG_ALIGN);
460460
static char *log_buf = __log_buf;
461461
static u32 log_buf_len = __LOG_BUF_LEN;
462462

463+
/*
464+
* We cannot access per-CPU data (e.g. per-CPU flush irq_work) before
465+
* per_cpu_areas are initialised. This variable is set to true when
466+
* it's safe to access per-CPU data.
467+
*/
468+
static bool __printk_percpu_data_ready __read_mostly;
469+
470+
bool printk_percpu_data_ready(void)
471+
{
472+
return __printk_percpu_data_ready;
473+
}
474+
463475
/* Return log buffer address */
464476
char *log_buf_addr_get(void)
465477
{
@@ -1146,12 +1158,28 @@ static void __init log_buf_add_cpu(void)
11461158
static inline void log_buf_add_cpu(void) {}
11471159
#endif /* CONFIG_SMP */
11481160

1161+
static void __init set_percpu_data_ready(void)
1162+
{
1163+
printk_safe_init();
1164+
/* Make sure we set this flag only after printk_safe() init is done */
1165+
barrier();
1166+
__printk_percpu_data_ready = true;
1167+
}
1168+
11491169
void __init setup_log_buf(int early)
11501170
{
11511171
unsigned long flags;
11521172
char *new_log_buf;
11531173
unsigned int free;
11541174

1175+
/*
1176+
* Some archs call setup_log_buf() multiple times - first is very
1177+
* early, e.g. from setup_arch(), and second - when percpu_areas
1178+
* are initialised.
1179+
*/
1180+
if (!early)
1181+
set_percpu_data_ready();
1182+
11551183
if (log_buf != __log_buf)
11561184
return;
11571185

@@ -2975,6 +3003,9 @@ static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
29753003

29763004
void wake_up_klogd(void)
29773005
{
3006+
if (!printk_percpu_data_ready())
3007+
return;
3008+
29783009
preempt_disable();
29793010
if (waitqueue_active(&log_wait)) {
29803011
this_cpu_or(printk_pending, PRINTK_PENDING_WAKEUP);
@@ -2985,6 +3016,9 @@ void wake_up_klogd(void)
29853016

29863017
void defer_console_output(void)
29873018
{
3019+
if (!printk_percpu_data_ready())
3020+
return;
3021+
29883022
preempt_disable();
29893023
__this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT);
29903024
irq_work_queue(this_cpu_ptr(&wake_up_klogd_work));

kernel/printk/printk_safe.c

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
* There are situations when we want to make sure that all buffers
2828
* were handled or when IRQs are blocked.
2929
*/
30-
static int printk_safe_irq_ready __read_mostly;
3130

3231
#define SAFE_LOG_BUF_LEN ((1 << CONFIG_PRINTK_SAFE_LOG_BUF_SHIFT) - \
3332
sizeof(atomic_t) - \
@@ -51,7 +50,7 @@ static DEFINE_PER_CPU(struct printk_safe_seq_buf, nmi_print_seq);
5150
/* Get flushed in a more safe context. */
5251
static void queue_flush_work(struct printk_safe_seq_buf *s)
5352
{
54-
if (printk_safe_irq_ready)
53+
if (printk_percpu_data_ready())
5554
irq_work_queue(&s->work);
5655
}
5756

@@ -402,14 +401,6 @@ void __init printk_safe_init(void)
402401
#endif
403402
}
404403

405-
/*
406-
* In the highly unlikely event that a NMI were to trigger at
407-
* this moment. Make sure IRQ work is set up before this
408-
* variable is set.
409-
*/
410-
barrier();
411-
printk_safe_irq_ready = 1;
412-
413404
/* Flush pending messages that did not have scheduled IRQ works. */
414405
printk_safe_flush();
415406
}

0 commit comments

Comments
 (0)