[PATCH printk v3 02/19] printk: nbcon: Consolidate alloc() and init()

From: John Ogness
Date: Mon Jul 22 2024 - 13:20:29 EST


Rather than splitting the nbcon allocation and initialization into
two pieces, perform all initialization in nbcon_alloc(). Later,
the init_seq is calculated and can be explicitly set using
nbcon_seq_force(). This removes the need for the strong rules of
nbcon_init() that even included a BUG_ON(). It also more closely
matches the setup logic of the legacy consoles.

Signed-off-by: John Ogness <john.ogness@xxxxxxxxxxxxx>
---
kernel/printk/internal.h | 2 --
kernel/printk/nbcon.c | 35 ++++++++++-------------------------
kernel/printk/printk.c | 2 +-
3 files changed, 11 insertions(+), 28 deletions(-)

diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h
index 0439cf2fdc22..d58f5cefbac3 100644
--- a/kernel/printk/internal.h
+++ b/kernel/printk/internal.h
@@ -86,7 +86,6 @@ 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);
bool nbcon_alloc(struct console *con);
-void nbcon_init(struct console *con, u64 init_seq);
void nbcon_free(struct console *con);
enum nbcon_prio nbcon_get_default_prio(void);
void nbcon_atomic_flush_pending(void);
@@ -144,7 +143,6 @@ static inline bool printk_percpu_data_ready(void) { return false; }
static inline u64 nbcon_seq_read(struct console *con) { return 0; }
static inline void nbcon_seq_force(struct console *con, u64 seq) { }
static inline bool nbcon_alloc(struct console *con) { return false; }
-static inline void nbcon_init(struct console *con, u64 init_seq) { }
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) { }
diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index d8388faa6500..1388e23a439f 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -1343,17 +1343,21 @@ void nbcon_cpu_emergency_flush(void)
}

/**
- * nbcon_alloc - Allocate buffers needed by the nbcon console
- * @con: Console to allocate buffers for
+ * nbcon_alloc - Allocate and init the nbcon console specific data
+ * @con: Console to initialize
*
- * Return: True on success. False otherwise and the console cannot
- * be used.
+ * Return: True if the console was fully allocated and initialized.
+ * Otherwise @con must not be registered.
*
- * This is not part of nbcon_init() because buffer allocation must
- * be performed earlier in the console registration process.
+ * When allocation and init was successful, the console must be properly
+ * freed using nbcon_free() once it is no longer needed.
*/
bool nbcon_alloc(struct console *con)
{
+ struct nbcon_state state = { };
+
+ nbcon_state_set(con, &state);
+
if (con->flags & CON_BOOT) {
/*
* Boot console printing is synchronized with legacy console
@@ -1372,25 +1376,6 @@ bool nbcon_alloc(struct console *con)
return true;
}

-/**
- * nbcon_init - Initialize the nbcon console specific data
- * @con: Console to initialize
- * @init_seq: Sequence number of the first record to be emitted
- *
- * nbcon_alloc() *must* be called and succeed before this function
- * is called.
- */
-void nbcon_init(struct console *con, u64 init_seq)
-{
- struct nbcon_state state = { };
-
- /* nbcon_alloc() must have been called and successful! */
- BUG_ON(!con->pbufs);
-
- nbcon_seq_force(con, init_seq);
- nbcon_state_set(con, &state);
-}
-
/**
* nbcon_free - Free and cleanup the nbcon console specific data
* @con: Console to free/cleanup nbcon data
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 5090c0591f88..641c2a8b0a09 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -3631,7 +3631,7 @@ void register_console(struct console *newcon)

if (newcon->flags & CON_NBCON) {
have_nbcon_console = true;
- nbcon_init(newcon, init_seq);
+ nbcon_seq_force(newcon, init_seq);
} else {
have_legacy_console = true;
newcon->seq = init_seq;
--
2.39.2