Skip to content

Commit 13189fa

Browse files
jognesspmladek
authored andcommitted
printk: nbcon: Rely on kthreads for normal operation
Once the kthread is running and available (i.e. @printk_kthreads_running is set), the kthread becomes responsible for flushing any pending messages which are added in NBCON_PRIO_NORMAL context. Namely the legacy console_flush_all() and device_release() no longer flush the console. And nbcon_atomic_flush_pending() used by nbcon_cpu_emergency_exit() no longer flushes messages added after the emergency messages. The console context is safe when used by the kthread only when one of the following conditions are true: 1. Other caller acquires the console context with NBCON_PRIO_NORMAL with preemption disabled. It will release the context before rescheduling. 2. Other caller acquires the console context with NBCON_PRIO_NORMAL under the device_lock. 3. The kthread is the only context which acquires the console with NBCON_PRIO_NORMAL. This is satisfied for all atomic printing call sites: nbcon_legacy_emit_next_record() (#1) nbcon_atomic_flush_pending_con() (#1) nbcon_device_release() (#2) It is even double guaranteed when @printk_kthreads_running is set because then _only_ the kthread will print for NBCON_PRIO_NORMAL. (#3) Signed-off-by: John Ogness <[email protected]> Reviewed-by: Petr Mladek <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Petr Mladek <[email protected]>
1 parent 5c586ba commit 13189fa

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

kernel/printk/internal.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ static inline bool console_is_usable(struct console *con, short flags, bool use_
113113
/* The write_atomic() callback is optional. */
114114
if (use_atomic && !con->write_atomic)
115115
return false;
116+
117+
/*
118+
* For the !use_atomic case, @printk_kthreads_running is not
119+
* checked because the write_thread() callback is also used
120+
* via the legacy loop when the printer threads are not
121+
* available.
122+
*/
116123
} else {
117124
if (!con->write)
118125
return false;
@@ -176,6 +183,7 @@ static inline void nbcon_atomic_flush_pending(void) { }
176183
static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
177184
int cookie, bool use_atomic) { return false; }
178185
static inline void nbcon_kthread_wake(struct console *con) { }
186+
static inline void nbcon_kthreads_wake(void) { }
179187

180188
static inline bool console_is_usable(struct console *con, short flags,
181189
bool use_atomic) { return false; }
@@ -190,13 +198,15 @@ extern bool legacy_allow_panic_sync;
190198
/**
191199
* struct console_flush_type - Define available console flush methods
192200
* @nbcon_atomic: Flush directly using nbcon_atomic() callback
201+
* @nbcon_offload: Offload flush to printer thread
193202
* @legacy_direct: Call the legacy loop in this context
194203
* @legacy_offload: Offload the legacy loop into IRQ
195204
*
196205
* Note that the legacy loop also flushes the nbcon consoles.
197206
*/
198207
struct console_flush_type {
199208
bool nbcon_atomic;
209+
bool nbcon_offload;
200210
bool legacy_direct;
201211
bool legacy_offload;
202212
};
@@ -211,6 +221,22 @@ static inline void printk_get_console_flush_type(struct console_flush_type *ft)
211221

212222
switch (nbcon_get_default_prio()) {
213223
case NBCON_PRIO_NORMAL:
224+
if (have_nbcon_console && !have_boot_console) {
225+
if (printk_kthreads_running)
226+
ft->nbcon_offload = true;
227+
else
228+
ft->nbcon_atomic = true;
229+
}
230+
231+
/* Legacy consoles are flushed directly when possible. */
232+
if (have_legacy_console || have_boot_console) {
233+
if (!is_printk_legacy_deferred())
234+
ft->legacy_direct = true;
235+
else
236+
ft->legacy_offload = true;
237+
}
238+
break;
239+
214240
case NBCON_PRIO_EMERGENCY:
215241
if (have_nbcon_console && !have_boot_console)
216242
ft->nbcon_atomic = true;

kernel/printk/nbcon.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,6 +1494,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
14941494
static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
14951495
bool allow_unsafe_takeover)
14961496
{
1497+
struct console_flush_type ft;
14971498
unsigned long flags;
14981499
int err;
14991500

@@ -1523,10 +1524,12 @@ static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
15231524

15241525
/*
15251526
* If flushing was successful but more records are available, this
1526-
* context must flush those remaining records because there is no
1527-
* other context that will do it.
1527+
* context must flush those remaining records if the printer thread
1528+
* is not available do it.
15281529
*/
1529-
if (prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
1530+
printk_get_console_flush_type(&ft);
1531+
if (!ft.nbcon_offload &&
1532+
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
15301533
stop_seq = prb_next_reserve_seq(prb);
15311534
goto again;
15321535
}
@@ -1754,17 +1757,19 @@ void nbcon_device_release(struct console *con)
17541757

17551758
/*
17561759
* This context must flush any new records added while the console
1757-
* was locked. The console_srcu_read_lock must be taken to ensure
1758-
* the console is usable throughout flushing.
1760+
* was locked if the printer thread is not available to do it. The
1761+
* console_srcu_read_lock must be taken to ensure the console is
1762+
* usable throughout flushing.
17591763
*/
17601764
cookie = console_srcu_read_lock();
1765+
printk_get_console_flush_type(&ft);
17611766
if (console_is_usable(con, console_srcu_read_flags(con), true) &&
1767+
!ft.nbcon_offload &&
17621768
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
17631769
/*
17641770
* If nbcon_atomic flushing is not available, fallback to
17651771
* using the legacy loop.
17661772
*/
1767-
printk_get_console_flush_type(&ft);
17681773
if (ft.nbcon_atomic) {
17691774
__nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false);
17701775
} else if (ft.legacy_direct) {

kernel/printk/printk.c

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,9 @@ asmlinkage int vprintk_emit(int facility, int level,
23842384
if (ft.nbcon_atomic)
23852385
nbcon_atomic_flush_pending();
23862386

2387+
if (ft.nbcon_offload)
2388+
nbcon_kthreads_wake();
2389+
23872390
if (ft.legacy_direct) {
23882391
/*
23892392
* The caller may be holding system-critical or
@@ -2732,6 +2735,7 @@ void suspend_console(void)
27322735

27332736
void resume_console(void)
27342737
{
2738+
struct console_flush_type ft;
27352739
struct console *con;
27362740

27372741
if (!console_suspend_enabled)
@@ -2749,6 +2753,10 @@ void resume_console(void)
27492753
*/
27502754
synchronize_srcu(&console_srcu);
27512755

2756+
printk_get_console_flush_type(&ft);
2757+
if (ft.nbcon_offload)
2758+
nbcon_kthreads_wake();
2759+
27522760
pr_flush(1000, true);
27532761
}
27542762

@@ -3060,6 +3068,7 @@ static inline void printk_kthreads_check_locked(void) { }
30603068
*/
30613069
static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handover)
30623070
{
3071+
struct console_flush_type ft;
30633072
bool any_usable = false;
30643073
struct console *con;
30653074
bool any_progress;
@@ -3071,12 +3080,22 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
30713080
do {
30723081
any_progress = false;
30733082

3083+
printk_get_console_flush_type(&ft);
3084+
30743085
cookie = console_srcu_read_lock();
30753086
for_each_console_srcu(con) {
30763087
short flags = console_srcu_read_flags(con);
30773088
u64 printk_seq;
30783089
bool progress;
30793090

3091+
/*
3092+
* console_flush_all() is only responsible for nbcon
3093+
* consoles when the nbcon consoles cannot print via
3094+
* their atomic or threaded flushing.
3095+
*/
3096+
if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload))
3097+
continue;
3098+
30803099
if (!console_is_usable(con, flags, !do_cond_resched))
30813100
continue;
30823101
any_usable = true;
@@ -3387,9 +3406,25 @@ EXPORT_SYMBOL(console_stop);
33873406

33883407
void console_start(struct console *console)
33893408
{
3409+
struct console_flush_type ft;
3410+
bool is_nbcon;
3411+
33903412
console_list_lock();
33913413
console_srcu_write_flags(console, console->flags | CON_ENABLED);
3414+
is_nbcon = console->flags & CON_NBCON;
33923415
console_list_unlock();
3416+
3417+
/*
3418+
* Ensure that all SRCU list walks have completed. The related
3419+
* printing context must be able to see it is enabled so that
3420+
* it is guaranteed to wake up and resume printing.
3421+
*/
3422+
synchronize_srcu(&console_srcu);
3423+
3424+
printk_get_console_flush_type(&ft);
3425+
if (is_nbcon && ft.nbcon_offload)
3426+
nbcon_kthread_wake(console);
3427+
33933428
__pr_flush(console, 1000, true);
33943429
}
33953430
EXPORT_SYMBOL(console_start);
@@ -4115,6 +4150,8 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
41154150

41164151
/* Flush the consoles so that records up to @seq are printed. */
41174152
printk_get_console_flush_type(&ft);
4153+
if (ft.nbcon_atomic)
4154+
nbcon_atomic_flush_pending();
41184155
if (ft.legacy_direct) {
41194156
console_lock();
41204157
console_unlock();
@@ -4152,8 +4189,10 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
41524189
* that they make forward progress, so only increment
41534190
* @diff for usable consoles.
41544191
*/
4155-
if (!console_is_usable(c, flags, true))
4192+
if (!console_is_usable(c, flags, true) &&
4193+
!console_is_usable(c, flags, false)) {
41564194
continue;
4195+
}
41574196

41584197
if (flags & CON_NBCON) {
41594198
printk_seq = nbcon_seq_read(c);
@@ -4629,8 +4668,15 @@ EXPORT_SYMBOL_GPL(kmsg_dump_rewind);
46294668
*/
46304669
void console_try_replay_all(void)
46314670
{
4671+
struct console_flush_type ft;
4672+
4673+
printk_get_console_flush_type(&ft);
46324674
if (console_trylock()) {
46334675
__console_rewind_all();
4676+
if (ft.nbcon_atomic)
4677+
nbcon_atomic_flush_pending();
4678+
if (ft.nbcon_offload)
4679+
nbcon_kthreads_wake();
46344680
/* Consoles are flushed as part of console_unlock(). */
46354681
console_unlock();
46364682
}

0 commit comments

Comments
 (0)