From: John Ogness <john.ogness@linutronix.de>
To: Petr Mladek <pmladek@suse.com>
Cc: Sergey Senozhatsky <senozhatsky@chromium.org>,
Steven Rostedt <rostedt@goodmis.org>,
Thomas Gleixner <tglx@linutronix.de>,
linux-kernel@vger.kernel.org
Subject: [PATCH printk v5 04/17] printk: nbcon: Add context to usable() and emit()
Date: Fri, 30 Aug 2024 17:35:03 +0206 [thread overview]
Message-ID: <20240830152916.10136-5-john.ogness@linutronix.de> (raw)
In-Reply-To: <20240830152916.10136-1-john.ogness@linutronix.de>
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 <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
---
kernel/printk/internal.h | 8 +++++---
kernel/printk/nbcon.c | 34 +++++++++++++++++++---------------
kernel/printk/printk.c | 6 +++---
3 files changed, 27 insertions(+), 21 deletions(-)
diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 8e36d8695f81..ad631824e7e5 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -97,7 +97,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
* which can also play a role in deciding if @con can be used to print
* records.
*/
-static inline bool console_is_usable(struct console *con, short flags)
+static inline bool console_is_usable(struct console *con, short flags, bool use_atomic)
{
if (!(flags & CON_ENABLED))
return false;
@@ -106,7 +106,8 @@ static inline bool console_is_usable(struct console *con, short flags)
return false;
if (flags & CON_NBCON) {
- if (!con->write_atomic)
+ /* The write_atomic() callback is optional. */
+ if (use_atomic && !con->write_atomic)
return false;
} else {
if (!con->write)
@@ -149,7 +150,8 @@ static inline void nbcon_atomic_flush_pending(void) { }
static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
int cookie) { return false; }
-static inline bool console_is_usable(struct console *con, short flags) { return false; }
+static inline bool console_is_usable(struct console *con, short flags,
+ bool use_atomic) { return false; }
#endif /* CONFIG_PRINTK */
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index d1c640e7e645..de6faf34e114 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -922,6 +922,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf);
/**
* nbcon_emit_next_record - Emit a record in the acquired context
* @wctxt: The write context that will be handed to the write function
+ * @use_atomic: True if the write_atomic() callback is to be used
*
* Return: True if this context still owns the console. False if
* ownership was handed over or taken.
@@ -935,7 +936,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf);
* When true is returned, @wctxt->ctxt.backlog indicates whether there are
* still records pending in the ringbuffer,
*/
-static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)
+static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt, bool use_atomic)
{
struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
struct console *con = ctxt->console;
@@ -946,6 +947,18 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)
unsigned long con_dropped;
unsigned long dropped;
+ /*
+ * This function should never be called for consoles that have not
+ * implemented the necessary callback for writing: i.e. legacy
+ * consoles and, when atomic, nbcon consoles with no write_atomic().
+ * Handle it as if ownership was lost and try to continue.
+ */
+ if (WARN_ON_ONCE((use_atomic && !con->write_atomic) ||
+ !(console_srcu_read_flags(con) & CON_NBCON))) {
+ nbcon_context_release(ctxt);
+ return false;
+ }
+
/*
* The printk buffers are filled within an unsafe section. This
* prevents NBCON_PRIO_NORMAL and NBCON_PRIO_EMERGENCY from
@@ -980,17 +993,8 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)
/* Initialize the write context for driver callbacks. */
nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_len);
- if (con->write_atomic) {
+ if (use_atomic)
con->write_atomic(con, wctxt);
- } else {
- /*
- * This function should never be called for legacy consoles.
- * Handle it as if ownership was lost and try to continue.
- */
- WARN_ON_ONCE(1);
- nbcon_context_release(ctxt);
- return false;
- }
if (!wctxt->outbuf) {
/*
@@ -1119,7 +1123,7 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt)
* The higher priority printing context takes over responsibility
* to print the pending records.
*/
- if (!nbcon_emit_next_record(wctxt))
+ if (!nbcon_emit_next_record(wctxt, true))
return false;
nbcon_context_release(ctxt);
@@ -1220,7 +1224,7 @@ static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_seq,
* handed over or taken over. In both cases the context is no
* longer valid.
*/
- if (!nbcon_emit_next_record(&wctxt))
+ if (!nbcon_emit_next_record(&wctxt, true))
return -EAGAIN;
if (!ctxt->backlog) {
@@ -1306,7 +1310,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq, bool allow_unsafe_takeove
if (!(flags & CON_NBCON))
continue;
- if (!console_is_usable(con, flags))
+ if (!console_is_usable(con, flags, true))
continue;
if (nbcon_seq_read(con) >= stop_seq)
@@ -1491,7 +1495,7 @@ void nbcon_device_release(struct console *con)
* the console is usable throughout flushing.
*/
cookie = console_srcu_read_lock();
- if (console_is_usable(con, console_srcu_read_flags(con)) &&
+ if (console_is_usable(con, console_srcu_read_flags(con), true) &&
prb_read_valid(prb, nbcon_seq_read(con), NULL)) {
/*
* If nbcon_atomic flushing is not available, fallback to
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index c79e962b822a..846306ac2663 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3071,7 +3071,7 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
u64 printk_seq;
bool progress;
- if (!console_is_usable(con, flags))
+ if (!console_is_usable(con, flags, true))
continue;
any_usable = true;
@@ -3773,7 +3773,7 @@ static int unregister_console_locked(struct console *console)
if (!console_is_registered_locked(console))
res = -ENODEV;
- else if (console_is_usable(console, console->flags))
+ else if (console_is_usable(console, console->flags, true))
__pr_flush(console, 1000, true);
/* Disable it unconditionally */
@@ -4043,7 +4043,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre
* that they make forward progress, so only increment
* @diff for usable consoles.
*/
- if (!console_is_usable(c, flags))
+ if (!console_is_usable(c, flags, true))
continue;
if (flags & CON_NBCON) {
--
2.39.2
next prev parent reply other threads:[~2024-08-30 15:29 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-30 15:28 [PATCH printk v5 00/17] add threaded printing + the rest John Ogness
2024-08-30 15:29 ` [PATCH printk v5 01/17] printk: nbcon: Add function for printers to reacquire ownership John Ogness
2024-08-30 15:29 ` [PATCH printk v5 02/17] printk: Fail pr_flush() if before SYSTEM_SCHEDULING John Ogness
2024-08-30 15:29 ` [PATCH printk v5 03/17] printk: Flush console on unregister_console() John Ogness
2024-08-30 15:29 ` John Ogness [this message]
2024-08-30 15:29 ` [PATCH printk v5 05/17] printk: nbcon: Init @nbcon_seq to highest possible John Ogness
2024-08-30 15:29 ` [PATCH printk v5 06/17] printk: nbcon: Introduce printer kthreads John Ogness
2024-09-02 14:19 ` Petr Mladek
2024-08-30 15:29 ` [PATCH printk v5 07/17] printk: nbcon: Relocate nbcon_atomic_emit_one() John Ogness
2024-08-30 15:29 ` [PATCH printk v5 08/17] printk: nbcon: Use thread callback if in task context for legacy John Ogness
2024-08-30 15:29 ` [PATCH printk v5 09/17] printk: nbcon: Rely on kthreads for normal operation John Ogness
2024-09-03 10:10 ` Petr Mladek
2024-09-03 11:50 ` John Ogness
2024-08-30 15:29 ` [PATCH printk v5 10/17] printk: Provide helper for message prepending John Ogness
2024-08-30 15:29 ` [PATCH printk v5 11/17] printk: nbcon: Show replay message on takeover John Ogness
2024-08-30 15:29 ` [PATCH printk v5 12/17] proc: consoles: Add notation to c_start/c_stop John Ogness
2024-08-30 15:29 ` [PATCH printk v5 13/17] proc: Add nbcon support for /proc/consoles John Ogness
2024-08-30 15:29 ` [PATCH printk v5 14/17] tty: sysfs: Add nbcon support for 'active' John Ogness
2024-08-30 15:29 ` [PATCH printk v5 15/17] printk: Implement legacy printer kthread for PREEMPT_RT John Ogness
2024-09-03 13:38 ` Petr Mladek
2024-09-03 14:24 ` John Ogness
2024-08-30 15:29 ` [PATCH printk v5 16/17] printk: nbcon: Assign nice -20 for printing threads John Ogness
2024-08-30 15:29 ` [PATCH printk v5 17/17] printk: Avoid false positive lockdep report for legacy printing John Ogness
2024-09-03 14:53 ` Petr Mladek
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240830152916.10136-5-john.ogness@linutronix.de \
--to=john.ogness@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=tglx@linutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox