]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
printk: nbcon: Use nbcon consoles in console_flush_all()
authorJohn Ogness <john.ogness@linutronix.de>
Tue, 20 Aug 2024 06:29:48 +0000 (08:35 +0206)
committerPetr Mladek <pmladek@suse.com>
Wed, 21 Aug 2024 12:56:24 +0000 (14:56 +0200)
Allow nbcon consoles to print messages in the legacy printk()
caller context (printing via unlock) by integrating them into
console_flush_all(). The write_atomic() callback is used for
printing.

Provide nbcon_legacy_emit_next_record(), which acts as the
nbcon variant of console_emit_next_record(). Call this variant
within console_flush_all() for nbcon consoles. Since nbcon
consoles use their own @nbcon_seq variable to track the next
record to print, this also must be appropriately handled in
console_flush_all().

Note that the legacy printing logic uses @handover to detect
handovers for printing all consoles. For nbcon consoles,
handovers/takeovers occur on a per-console basis and thus do
not cause the console_flush_all() loop to abort.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20240820063001.36405-23-john.ogness@linutronix.de
Signed-off-by: Petr Mladek <pmladek@suse.com>
kernel/printk/internal.h
kernel/printk/nbcon.c
kernel/printk/printk.c

index 0dc9b92b5dd0661889447a5ca8ace67a6c299900..44468f3828f3ef6d3b48d48f2dc2369bc9533bb6 100644 (file)
@@ -78,6 +78,8 @@ void defer_console_output(void);
 
 u16 printk_parse_prefix(const char *text, int *level,
                        enum printk_info_flags *flags);
+void console_lock_spinning_enable(void);
+int console_lock_spinning_disable_and_check(int cookie);
 
 u64 nbcon_seq_read(struct console *con);
 void nbcon_seq_force(struct console *con, u64 seq);
@@ -85,6 +87,8 @@ bool nbcon_alloc(struct console *con);
 void nbcon_free(struct console *con);
 enum nbcon_prio nbcon_get_default_prio(void);
 void nbcon_atomic_flush_pending(void);
+bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
+                                  int cookie);
 
 /*
  * Check if the given console is currently capable and allowed to print
@@ -140,6 +144,8 @@ static inline bool nbcon_alloc(struct console *con) { return false; }
 static inline void nbcon_free(struct console *con) { }
 static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_PRIO_NONE; }
 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; }
 
index 3982d68979d68b7e30191b2794d067e84accf6cf..d09c084c9af4ab0f8a5559f3b5290f9f77c42953 100644 (file)
@@ -992,6 +992,93 @@ enum nbcon_prio nbcon_get_default_prio(void)
 }
 
 /*
+ * nbcon_atomic_emit_one - Print one record for an nbcon console using the
+ *                             write_atomic() callback
+ * @wctxt:     An initialized write context struct to use for this context
+ *
+ * Return:     True, when a record has been printed and there are still
+ *             pending records. The caller might want to continue flushing.
+ *
+ *             False, when there is no pending record, or when the console
+ *             context cannot be acquired, or the ownership has been lost.
+ *             The caller should give up. Either the job is done, cannot be
+ *             done, or will be handled by the owning context.
+ *
+ * This is an internal helper to handle the locking of the console before
+ * calling nbcon_emit_next_record().
+ */
+static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt)
+{
+       struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt);
+
+       if (!nbcon_context_try_acquire(ctxt))
+               return false;
+
+       /*
+        * nbcon_emit_next_record() returns false when the console was
+        * handed over or taken over. In both cases the context is no
+        * longer valid.
+        *
+        * The higher priority printing context takes over responsibility
+        * to print the pending records.
+        */
+       if (!nbcon_emit_next_record(wctxt))
+               return false;
+
+       nbcon_context_release(ctxt);
+
+       return ctxt->backlog;
+}
+
+/**
+ * nbcon_legacy_emit_next_record - Print one record for an nbcon console
+ *                                     in legacy contexts
+ * @con:       The console to print on
+ * @handover:  Will be set to true if a printk waiter has taken over the
+ *             console_lock, in which case the caller is no longer holding
+ *             both the console_lock and the SRCU read lock. Otherwise it
+ *             is set to false.
+ * @cookie:    The cookie from the SRCU read lock.
+ *
+ * Context:    Any context except NMI.
+ * Return:     True, when a record has been printed and there are still
+ *             pending records. The caller might want to continue flushing.
+ *
+ *             False, when there is no pending record, or when the console
+ *             context cannot be acquired, or the ownership has been lost.
+ *             The caller should give up. Either the job is done, cannot be
+ *             done, or will be handled by the owning context.
+ *
+ * This function is meant to be called by console_flush_all() to print records
+ * on nbcon consoles from legacy context (printing via console unlocking).
+ * Essentially it is the nbcon version of console_emit_next_record().
+ */
+bool nbcon_legacy_emit_next_record(struct console *con, bool *handover,
+                                  int cookie)
+{
+       struct nbcon_write_context wctxt = { };
+       struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt);
+       unsigned long flags;
+       bool progress;
+
+       /* Use the same procedure as console_emit_next_record(). */
+       printk_safe_enter_irqsave(flags);
+       console_lock_spinning_enable();
+       stop_critical_timings();
+
+       ctxt->console   = con;
+       ctxt->prio      = nbcon_get_default_prio();
+
+       progress = nbcon_atomic_emit_one(&wctxt);
+
+       start_critical_timings();
+       *handover = console_lock_spinning_disable_and_check(cookie);
+       printk_safe_exit_irqrestore(flags);
+
+       return progress;
+}
+
+/**
  * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using its
  *                                     write_atomic() callback
  * @con:                       The nbcon console to flush
index b8634a153d1df66fdb24776c9207651ba1fb711f..f08bf5e82fc732cde8a87e1abe1a040f9625b4e6 100644 (file)
@@ -1860,7 +1860,7 @@ static bool console_waiter;
  * there may be a waiter spinning (like a spinlock). Also it must be
  * ready to hand over the lock at the end of the section.
  */
-static void console_lock_spinning_enable(void)
+void console_lock_spinning_enable(void)
 {
        /*
         * Do not use spinning in panic(). The panic CPU wants to keep the lock.
@@ -1899,7 +1899,7 @@ lockdep:
  *
  * Return: 1 if the lock rights were passed, 0 otherwise.
  */
-static int console_lock_spinning_disable_and_check(int cookie)
+int console_lock_spinning_disable_and_check(int cookie)
 {
        int waiter;
 
@@ -3021,13 +3021,20 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
                cookie = console_srcu_read_lock();
                for_each_console_srcu(con) {
                        short flags = console_srcu_read_flags(con);
+                       u64 printk_seq;
                        bool progress;
 
                        if (!console_is_usable(con, flags))
                                continue;
                        any_usable = true;
 
-                       progress = console_emit_next_record(con, handover, cookie);
+                       if (flags & CON_NBCON) {
+                               progress = nbcon_legacy_emit_next_record(con, handover, cookie);
+                               printk_seq = nbcon_seq_read(con);
+                       } else {
+                               progress = console_emit_next_record(con, handover, cookie);
+                               printk_seq = con->seq;
+                       }
 
                        /*
                         * If a handover has occurred, the SRCU read lock
@@ -3037,8 +3044,8 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove
                                return false;
 
                        /* Track the next of the highest seq flushed. */
-                       if (con->seq > *next_seq)
-                               *next_seq = con->seq;
+                       if (printk_seq > *next_seq)
+                               *next_seq = printk_seq;
 
                        if (!progress)
                                continue;