Skip to content

Commit 6cb58cf

Browse files
jognesspmladek
authored andcommitted
printk: nbcon: Add context to usable() and emit()
The nbcon consoles will have two callbacks to be used for different contexts. In order to determine if an nbcon console is usable, console_is_usable() must know if it is a context that will need to use the optional write_atomic() callback. Also, nbcon_emit_next_record() must know which callback it needs to call. Add an extra parameter @use_atomic to console_is_usable() and nbcon_emit_next_record() to specify this. Since so far only the write_atomic() callback exists, @use_atomic is set to true for all call sites. For legacy consoles, @use_atomic is not used. 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 0e53e2d commit 6cb58cf

File tree

3 files changed

+27
-21
lines changed

3 files changed

+27
-21
lines changed

kernel/printk/internal.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
9797
* which can also play a role in deciding if @con can be used to print
9898
* records.
9999
*/
100-
static inline bool console_is_usable(struct console *con, short flags)
100+
static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
101101
{
102102
if (!(flags & CON_ENABLED))
103103
return false;
@@ -106,7 +106,8 @@ static inline bool console_is_usable(struct console *con, short flags)
106106
return false;
107107

108108
if (flags & CON_NBCON) {
109-
if (!con->write_atomic)
109+
/* The write_atomic() callback is optional. */
110+
if (use_atomic && !con->write_atomic)
110111
return false;
111112
} else {
112113
if (!con->write)
@@ -149,7 +150,8 @@ static inline void nbcon_atomic_flush_pending(void) { }
149150
static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
150151
int cookie) { return false; }
151152

152-
static inline bool console_is_usable(struct console *con, short flags) { return false; }
153+
static inline bool console_is_usable(struct console *con, short flags,
154+
bool use_atomic) { return false; }
153155

154156
#endif /* CONFIG_PRINTK */
155157

kernel/printk/nbcon.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf);
922922
/**
923923
* nbcon_emit_next_record - Emit a record in the acquired context
924924
* @wctxt: The write context that will be handed to the write function
925+
* @use_atomic: True if the write_atomic() callback is to be used
925926
*
926927
* Return: True if this context still owns the console. False if
927928
* ownership was handed over or taken.
@@ -935,7 +936,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf);
935936
* When true is returned, @wctxt->ctxt.backlog indicates whether there are
936937
* still records pending in the ringbuffer,
937938
*/
938-
static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)
939+
static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt, bool use_atomic)
939940
{
940941
struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
941942
struct console *con = ctxt->console;
@@ -946,6 +947,18 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)
946947
unsigned long con_dropped;
947948
unsigned long dropped;
948949

950+
/*
951+
* This function should never be called for consoles that have not
952+
* implemented the necessary callback for writing: i.e. legacy
953+
* consoles and, when atomic, nbcon consoles with no write_atomic().
954+
* Handle it as if ownership was lost and try to continue.
955+
*/
956+
if (WARN_ON_ONCE((use_atomic && !con->write_atomic) ||
957+
!(console_srcu_read_flags(con) & CON_NBCON))) {
958+
nbcon_context_release(ctxt);
959+
return false;
960+
}
961+
949962
/*
950963
* The printk buffers are filled within an unsafe section. This
951964
* prevents NBCON_PRIO_NORMAL and NBCON_PRIO_EMERGENCY from
@@ -980,17 +993,8 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)
980993
/* Initialize the write context for driver callbacks. */
981994
nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_len);
982995

983-
if (con->write_atomic) {
996+
if (use_atomic)
984997
con->write_atomic(con, wctxt);
985-
} else {
986-
/*
987-
* This function should never be called for legacy consoles.
988-
* Handle it as if ownership was lost and try to continue.
989-
*/
990-
WARN_ON_ONCE(1);
991-
nbcon_context_release(ctxt);
992-
return false;
993-
}
994998

995999
if (!wctxt->outbuf) {
9961000
/*
@@ -1118,7 +1122,7 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt)
11181122
* The higher priority printing context takes over responsibility
11191123
* to print the pending records.
11201124
*/
1121-
if (!nbcon_emit_next_record(wctxt))
1125+
if (!nbcon_emit_next_record(wctxt, true))
11221126
return false;
11231127

11241128
nbcon_context_release(ctxt);
@@ -1219,7 +1223,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
12191223
* handed over or taken over. In both cases the context is no
12201224
* longer valid.
12211225
*/
1222-
if (!nbcon_emit_next_record(&wctxt))
1226+
if (!nbcon_emit_next_record(&wctxt, true))
12231227
return -EAGAIN;
12241228

12251229
if (!ctxt->backlog) {
@@ -1305,7 +1309,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq, bool allow_unsafe_takeove
13051309
if (!(flags & CON_NBCON))
13061310
continue;
13071311

1308-
if (!console_is_usable(con, flags))
1312+
if (!console_is_usable(con, flags, true))
13091313
continue;
13101314

13111315
if (nbcon_seq_read(con) >= stop_seq)
@@ -1490,7 +1494,7 @@ void nbcon_device_release(struct console *con)
14901494
* the console is usable throughout flushing.
14911495
*/
14921496
cookie = console_srcu_read_lock();
1493-
if (console_is_usable(con, console_srcu_read_flags(con)) &&
1497+
if (console_is_usable(con, console_srcu_read_flags(con), true) &&
14941498
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
14951499
/*
14961500
* If nbcon_atomic flushing is not available, fallback to

kernel/printk/printk.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3071,7 +3071,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
30713071
u64 printk_seq;
30723072
bool progress;
30733073

3074-
if (!console_is_usable(con, flags))
3074+
if (!console_is_usable(con, flags, true))
30753075
continue;
30763076
any_usable = true;
30773077

@@ -3773,7 +3773,7 @@ static int unregister_console_locked(struct console *console)
37733773

37743774
if (!console_is_registered_locked(console))
37753775
res = -ENODEV;
3776-
else if (console_is_usable(console, console->flags))
3776+
else if (console_is_usable(console, console->flags, true))
37773777
__pr_flush(console, 1000, true);
37783778

37793779
/* Disable it unconditionally */
@@ -4043,7 +4043,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
40434043
* that they make forward progress, so only increment
40444044
* @diff for usable consoles.
40454045
*/
4046-
if (!console_is_usable(c, flags))
4046+
if (!console_is_usable(c, flags, true))
40474047
continue;
40484048

40494049
if (flags & CON_NBCON) {

0 commit comments

Comments
 (0)