From 815fb7cad84c73741fceddb43e64a0107c15511e Mon Sep 17 00:00:00 2001
From: John Ogness <john.ogness@linutronix.de>
Date: Fri, 20 Oct 2023 09:52:59 +0000
Subject: [PATCH 123/184] printk: nbcon: Explicitly release ownership on failed
 emit

Until now it was assumed that ownership has been lost when the
write_atomic() callback fails. nbcon_emit_next_record() only
returns false when ownership has been lost. So explicitly
release ownership if write_atomic() fails to be sure.

Signed-off-by: John Ogness <john.ogness@linutronix.de>
Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/printk/nbcon.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c
index b96077152f49..560d3c478ac1 100644
--- a/kernel/printk/nbcon.c
+++ b/kernel/printk/nbcon.c
@@ -927,14 +927,18 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt)
 	if (con->write_atomic) {
 		done = con->write_atomic(con, wctxt);
 	} else {
-		nbcon_context_release(ctxt);
 		WARN_ON_ONCE(1);
 		done = false;
 	}
 
-	/* If not done, the emit was aborted. */
-	if (!done)
+	if (!done) {
+		/*
+		 * The emit was aborted. This may have been due to a loss
+		 * of ownership. Explicitly release ownership to be sure.
+		 */
+		nbcon_context_release(ctxt);
 		return false;
+	}
 
 	/*
 	 * Since any dropped message was successfully output, reset the
-- 
2.34.1

