[PATCH printk v5 11/30] nbcon: Provide functions for drivers to acquire console for non-printing

John Ogness posted 30 patches 1 year, 9 months ago
There is a newer version of this series
[PATCH printk v5 11/30] nbcon: Provide functions for drivers to acquire console for non-printing
Posted by John Ogness 1 year, 9 months ago
Provide functions nbcon_driver_try_acquire() and
nbcon_driver_release() to allow drivers to acquire the nbcon
console and mark it unsafe for handover/takeover.

These functions are to be used by nbcon drivers when performing
non-printing activities that should be synchronized with their
atomic_write() callback.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 include/linux/console.h |  2 ++
 include/linux/printk.h  | 14 +++++++++++
 kernel/printk/nbcon.c   | 55 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/include/linux/console.h b/include/linux/console.h
index aea4f5aa4a45..3c20938f3676 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -304,6 +304,7 @@ struct nbcon_write_context {
  *
  * @nbcon_state:	State for nbcon consoles
  * @nbcon_seq:		Sequence number of the next record for nbcon to print
+ * @nbcon_driver_ctxt:	Context available for driver non-printing operations
  * @pbufs:		Pointer to nbcon private buffer
  */
 struct console {
@@ -399,6 +400,7 @@ struct console {
 
 	atomic_t		__private nbcon_state;
 	atomic_long_t		__private nbcon_seq;
+	struct nbcon_context	__private nbcon_driver_ctxt;
 	struct printk_buffers	*pbufs;
 };
 
diff --git a/include/linux/printk.h b/include/linux/printk.h
index d8b3f51d9e98..d0a1106388d1 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -9,6 +9,8 @@
 #include <linux/ratelimit_types.h>
 #include <linux/once_lite.h>
 
+struct console;
+
 extern const char linux_banner[];
 extern const char linux_proc_banner[];
 
@@ -193,6 +195,8 @@ void show_regs_print_info(const char *log_lvl);
 extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
 extern asmlinkage void dump_stack(void) __cold;
 void printk_trigger_flush(void);
+extern bool nbcon_driver_try_acquire(struct console *con);
+extern void nbcon_driver_release(struct console *con);
 #else
 static inline __printf(1, 0)
 int vprintk(const char *s, va_list args)
@@ -272,6 +276,16 @@ static inline void dump_stack(void)
 static inline void printk_trigger_flush(void)
 {
 }
+
+static inline bool nbcon_driver_try_acquire(struct console *con)
+{
+	return false;
+}
+
+static inline void nbcon_driver_release(struct console *con)
+{
+}
+
 #endif
 
 bool this_cpu_in_panic(void);
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index dd7c3180b335..b8a7e3e136d3 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -5,7 +5,9 @@
 #include <linux/kernel.h>
 #include <linux/console.h>
 #include <linux/delay.h>
+#include <linux/export.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include "internal.h"
 /*
  * Printk console printing implementation for consoles which does not depend
@@ -528,6 +530,7 @@ static struct printk_buffers panic_nbcon_pbufs;
  * nbcon_context_try_acquire - Try to acquire nbcon console
  * @ctxt:	The context of the caller
  *
+ * Context:	Under @ctxt->con->device_lock() or local_irq_save().
  * Return:	True if the console was acquired. False otherwise.
  *
  * If the caller allowed an unsafe hostile takeover, on success the
@@ -535,7 +538,6 @@ static struct printk_buffers panic_nbcon_pbufs;
  * in an unsafe state. Otherwise, on success the caller may assume
  * the console is not in an unsafe state.
  */
-__maybe_unused
 static bool nbcon_context_try_acquire(struct nbcon_context *ctxt)
 {
 	unsigned int cpu = smp_processor_id();
@@ -989,3 +991,54 @@ void nbcon_free(struct console *con)
 
 	con->pbufs = NULL;
 }
+
+/**
+ * nbcon_driver_try_acquire - Try to acquire nbcon console and enter unsafe
+ *				section
+ * @con:	The nbcon console to acquire
+ *
+ * Context:	Under the locking mechanism implemented in
+ *		@con->device_lock() including disabling migration.
+ *
+ * Console drivers will usually use their own internal synchronization
+ * mechasism to synchronize between console printing and non-printing
+ * activities (such as setting baud rates). However, nbcon console drivers
+ * supporting atomic consoles may also want to mark unsafe sections when
+ * performing non-printing activities in order to synchronize against their
+ * atomic_write() callback.
+ *
+ * This function acquires the nbcon console using priority NBCON_PRIO_NORMAL
+ * and marks it unsafe for handover/takeover.
+ */
+bool nbcon_driver_try_acquire(struct console *con)
+{
+	struct nbcon_context *ctxt = &ACCESS_PRIVATE(con, nbcon_driver_ctxt);
+
+	cant_migrate();
+
+	memset(ctxt, 0, sizeof(*ctxt));
+	ctxt->console	= con;
+	ctxt->prio	= NBCON_PRIO_NORMAL;
+
+	if (!nbcon_context_try_acquire(ctxt))
+		return false;
+
+	if (!nbcon_context_enter_unsafe(ctxt))
+		return false;
+
+	return true;
+}
+EXPORT_SYMBOL_GPL(nbcon_driver_try_acquire);
+
+/**
+ * nbcon_driver_release - Exit unsafe section and release the nbcon console
+ * @con:	The nbcon console acquired in nbcon_driver_try_acquire()
+ */
+void nbcon_driver_release(struct console *con)
+{
+	struct nbcon_context *ctxt = &ACCESS_PRIVATE(con, nbcon_driver_ctxt);
+
+	if (nbcon_context_exit_unsafe(ctxt))
+		nbcon_context_release(ctxt);
+}
+EXPORT_SYMBOL_GPL(nbcon_driver_release);
-- 
2.39.2
Re: [PATCH printk v5 11/30] nbcon: Provide functions for drivers to acquire console for non-printing
Posted by Petr Mladek 1 year, 8 months ago
On Thu 2024-05-02 23:44:20, John Ogness wrote:
> Provide functions nbcon_driver_try_acquire() and
> nbcon_driver_release() to allow drivers to acquire the nbcon
> console and mark it unsafe for handover/takeover.

I would prefer to rename 'driver' to 'device' to keep the existing
naming scheme in struct console. The API will be used to
synchronize the 'device' element.

> These functions are to be used by nbcon drivers when performing
> non-printing activities that should be synchronized with their
> atomic_write() callback.

I am still trying to create a mental model around the synchronization
between the device-specific locks and nbcon context. Also it took
me some time to realize that just disambling the migration is
perfectly fine here.

I propose a bit extended commit message:

<proposal>
Subject: nbcon: Add API to acquire context for non-printing operations

Provide functions nbcon_device_try_acquire() and
nbcon_device_release() which will try to acquire the nbcon console
ownership with NBCON_PRIO_NORMAL and mark it unsafe for handover/takeover.

These functions are to be used together with the device-specific
locking when performing non-printing activities on the console device.
They will allow synchronization against atomic_write() callback which
will be serialized, for higher priority contexts, only by acquiring
the console context ownership.

Pitfalls:

The API requires to be called in a context with a disabled migration
because it uses per-CPU variables internally.

The context is set unsafe for a takeover all the time. It guarantees
full serialization against any atomic_write() caller except for
the final flush in panic() which might try an unsafe takeover.
</proposal>

> --- a/include/linux/console.h
> +++ b/include/linux/console.h
> @@ -304,6 +304,7 @@ struct nbcon_write_context {
>   *
>   * @nbcon_state:	State for nbcon consoles
>   * @nbcon_seq:		Sequence number of the next record for nbcon to print
> + * @nbcon_driver_ctxt:	Context available for driver non-printing operations

This provides a context for "@device".

We should call this "@nbcon_device_ctxt" to avoid confusion.


>   * @pbufs:		Pointer to nbcon private buffer
>   */
>  struct console {
> @@ -399,6 +400,7 @@ struct console {
>  
>  	atomic_t		__private nbcon_state;
>  	atomic_long_t		__private nbcon_seq;
> +	struct nbcon_context	__private nbcon_driver_ctxt;
>  	struct printk_buffers	*pbufs;
>  };
>  
> diff --git a/include/linux/printk.h b/include/linux/printk.h
> index d8b3f51d9e98..d0a1106388d1 100644
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -9,6 +9,8 @@
>  #include <linux/ratelimit_types.h>
>  #include <linux/once_lite.h>
>  
> +struct console;
> +
>  extern const char linux_banner[];
>  extern const char linux_proc_banner[];
>  
> @@ -193,6 +195,8 @@ void show_regs_print_info(const char *log_lvl);
>  extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold;
>  extern asmlinkage void dump_stack(void) __cold;
>  void printk_trigger_flush(void);
> +extern bool nbcon_driver_try_acquire(struct console *con);
> +extern void nbcon_driver_release(struct console *con);

Similar here:

s/driver/device/

>  #else
>  static inline __printf(1, 0)
>  int vprintk(const char *s, va_list args)


With all the renames and updated commit message:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr
Re: [PATCH printk v5 11/30] nbcon: Provide functions for drivers to acquire console for non-printing
Posted by John Ogness 1 year, 9 months ago
On 2024-05-02, John Ogness <john.ogness@linutronix.de> wrote:
> diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
> index dd7c3180b335..b8a7e3e136d3 100644
> --- a/kernel/printk/nbcon.c
> +++ b/kernel/printk/nbcon.c
> @@ -989,3 +991,54 @@ void nbcon_free(struct console *con)
>  
>  	con->pbufs = NULL;
>  }
> +
> +/**
> + * nbcon_driver_try_acquire - Try to acquire nbcon console and enter unsafe
> + *				section
> + * @con:	The nbcon console to acquire
> + *
> + * Context:	Under the locking mechanism implemented in
> + *		@con->device_lock() including disabling migration.

 * Return:	True if the console was acquired. False otherwise.

John