Skip to contentSkip to conversation

nbcon/reboot: Flush nbcon consoles synchronously on reboot

PATCH In review Petr Mladek 5 participants 13 messages Oct 8 – Oct 12

Subsystems (2)
lore
Share & tools
lore

Petr Mladek

author maintainer
#0 lore raw

PATCH 1/1 nbcon/reboot: Flush nbcon consoles synchronously on reboot

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various reboot and halt code paths. They can be detected by the system_state variable.

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

Note that suspend code paths have already been handled, see console_suspend_all() and console_suspend().

---
Hi,

ah, this somehow fallen through cracks.
I am sending it as a proper patch finally.

I am sorry I have been somehow too busy before
the Plumbers conference.

Best Regards,
Petr

---
 kernel/printk/nbcon.c | 4 ++++
 kernel/reboot.c       | 4 ++++
 2 files changed, 8 insertions(+)
kernel/printk/nbcon.c +4 -0
index fb1b37741952..d8f8ec836eea 100644
@@ -1446,6 +1446,10 @@ enum nbcon_prio nbcon_get_default_prio(void)
 	if (panic_on_this_cpu())
 		return NBCON_PRIO_PANIC;
 
+	/* Do not rely on kthreads when the system is going down. */
+	if (system_state > SYSTEM_RUNNING)
+		return NBCON_PRIO_EMERGENCY;
+
 	cpu_emergency_nesting = nbcon_get_cpu_emergency_nesting();
 	if (*cpu_emergency_nesting)
 		return NBCON_PRIO_EMERGENCY;
kernel/reboot.c +4 -0
index d177d89fcc33..776784a82499 100644
@@ -8,6 +8,7 @@
 #define pr_fmt(fmt)	"reboot: " fmt
 
 #include <linux/atomic.h>
+#include <linux/console.h>
 #include <linux/ctype.h>
 #include <linux/export.h>
 #include <linux/kexec.h>
@@ -94,6 +95,7 @@ void emergency_restart(void)
 {
 	kmsg_dump(KMSG_DUMP_EMERG);
 	system_state = SYSTEM_RESTART;
+	printk_trigger_flush();
 	machine_emergency_restart();
 }
 EXPORT_SYMBOL_GPL(emergency_restart);
@@ -102,6 +104,7 @@ void kernel_restart_prepare(char *cmd)
 {
 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
 	system_state = SYSTEM_RESTART;
+	printk_trigger_flush();
 	usermodehelper_disable();
 	device_shutdown();
 }
@@ -305,6 +308,7 @@ static void kernel_shutdown_prepare(enum system_states state)
 	blocking_notifier_call_chain(&reboot_notifier_list,
 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
 	system_state = state;
+	printk_trigger_flush();
 	usermodehelper_disable();
 	device_shutdown();
 }
Signature · 1 line
2.55.0
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 13 lines

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various reboot and halt code paths. They can be detected by the system_state variable.

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

Quoted 7 lines

Note that suspend code paths have already been handled, see console_suspend_all() and console_suspend().

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Petr Mladek
Quoted 14 lines · Petr Mladek

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various
reboot and halt code paths. They can be detected by the system_state variable.

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

Quoted 6 lines

Note that suspend code paths have already been handled, see console_suspend_all() and console_suspend().

Hi, this looks good to me reboot.c ended! :)

Quoted 9 lines

---
Hi,

ah, this somehow fallen through cracks.
I am sending it as a proper patch finally.

I am sorry I have been somehow too busy before
the Plumbers conference.

That's ok :) I'm too unlucky as to not go to any conferences :c

Quoted 53 lines

Best Regards,
Petr
---
kernel/printk/nbcon.c | 4 ++++
kernel/reboot.c | 4 ++++

2 files changed, 8 insertions(+)
kernel/printk/nbcon.c +4 -0
index fb1b37741952..d8f8ec836eea 100644
@@ -1446,6 +1446,10 @@ enum nbcon_prio nbcon_get_default_prio(void)
 	if (panic_on_this_cpu())
 		return NBCON_PRIO_PANIC;
 
+	/* Do not rely on kthreads when the system is going down. */
+	if (system_state > SYSTEM_RUNNING)
+		return NBCON_PRIO_EMERGENCY;
+
 	cpu_emergency_nesting = nbcon_get_cpu_emergency_nesting();
 	if (*cpu_emergency_nesting)
 		return NBCON_PRIO_EMERGENCY;
kernel/reboot.c +3 -0
index d177d89fcc33..776784a82499 100644
@@ -8,6 +8,7 @@
 #define pr_fmt(fmt)	"reboot: " fmt
 
 #include <linux/atomic.h>
+#include <linux/console.h>
 #include <linux/ctype.h>
 #include <linux/export.h>
 #include <linux/kexec.h>
@@ -94,6 +95,7 @@ void emergency_restart(void)
 {
 	kmsg_dump(KMSG_DUMP_EMERG);
 	system_state = SYSTEM_RESTART;
+	printk_trigger_flush();
 	machine_emergency_restart();
 }
 EXPORT_SYMBOL_GPL(emergency_restart);
@@ -102,6 +104,7 @@ void kernel_restart_prepare(char *cmd)
 {
 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
 	system_state = SYSTEM_RESTART;
+	printk_trigger_flush();
 	usermodehelper_disable();
 	device_shutdown();
 }

kernel/reboot.c static void kernel_shutdown_prepare(enum system_states state) · line 314

Quoted 7 lines
kernel/reboot.c +1 -0
@@ -305,6 +308,7 @@ static void kernel_shutdown_prepare(enum system_states state)
 	blocking_notifier_call_chain(&reboot_notifier_list,
 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
 	system_state = state;
+	printk_trigger_flush();
 	usermodehelper_disable();
 	device_shutdown();
 }

--- Thanks!
"I'm not a very positive person" - Linus torvalds

author maintainer
#3 lore raw
Quoted 18 lines · Bradley Morgan
Quoted 17 lines · Petr Mladek

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various
reboot and halt code paths. They can be detected by the system_state variable.

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

Note that suspend code paths have already been handled, see console_suspend_all() and console_suspend().

Quoted 7 lines
Quoted 3 lines

Hi, this looks good to me reboot.c ended! :)

Honestly, I am more or less ignoring Reviewed-by: Bradley Morgan. I can't recall any problem reported by this person. And the excited, AI-generated, acks do not add much value.

Quoted 11 lines
Quoted 9 lines

---
Hi,

ah, this somehow fallen through cracks.
I am sending it as a proper patch finally.

I am sorry I have been somehow too busy before
the Plumbers conference.

That's ok :) I'm too unlucky as to not go to any conferences :c

I am not sure if there will be any conferences when robots will be ready to attend them.

Best Regards,
Petr

#4 lore raw
Quoted 7 lines · Petr Mladek
Quoted 3 lines

Hi, this looks good to me reboot.c ended! :)

Honestly, I am more or less ignoring Reviewed-by: Bradley Morgan. I can't recall any problem reported by this person. And the excited, AI-generated, acks do not add much value.

to be fair, there is one:

| $ git log --grep "Reported-by: Bradley Morgan" --oneline | wc -l
| 1

and there are even fixes:

| $ git log --author "Bradley Morgan" --grep "Fixes:" --oneline | wc -l
| 12
Quoted 4 lines
Quoted 1 line

That's ok :) I'm too unlucky as to not go to any conferences :c

I am not sure if there will be any conferences when robots will be ready to attend them.

Would you or would not exclude Lieutenant Commander Data from a conference? We don't have positronic brains yet but maybe in a few years :)

Quoted 2 lines

Best Regards,
Petr

Sebastian

#5 lore raw
In reply to Petr Mladek
Quoted 4 lines · Petr Mladek

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Quoted 8 lines

Another situation where printk() could not rely on kthreads are the various reboot and halt code paths. They can be detected by the system_state variable.

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

while this seems okay, didn't we have pr_flush() to flush the output on shutdown/ reboot?

Quoted 7 lines

Note that suspend code paths have already been handled, see console_suspend_all() and console_suspend().

Sebastian

author maintainer
#6 lore raw
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Petr Mladek

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various reboot and halt code paths. They can be detected by the system_state variable.

Quoted 7 lines
Quoted 4 lines

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

while this seems okay, didn't we have pr_flush() to flush the output on shutdown/ reboot?

Good point! We should clean this up.

Anyway, IMHO, we should switch to NBCON_PRIO_EMERGENCY for the post-running states and flush the pending messages immediately. It is more reliable. It is easy to find the locations when system_state() is set. And all follow-up printk() calls will try the direct flush so that they won't rely on an explicit flush.

An ideal solution would be to add an wrapper, e.g.

	void set_syste_state(enum system_states state)
	{
		system_state = state;

		if (system_state > SYSTEM_RUNNING)
			pr_flush(0, true);
	}

Another thing is that printk_trigger_flush() is an overkill. It tries to wake kthreads even via irq_work but we are interested only in the direct flush.

I am not sure why neither me nor John used pr_flush(). It might be because it originally did not flush atomic consoles directly. At least I had an outdated mental map.

Also the timeout should not be needed because all consoles should be flushed directly. But it can be solved by using zero timeout.

In fact, we should block the kthreads to prevent seeing more incomplete/interrupted messages, see the commit c41c0ebfa1e0eb ("printk/nbcon: Block printk kthreads when any CPU is in an emergency context"). Something like:

kernel/printk/nbcon.c +10 -7
index d8f8ec836eea..1c21c1e65b00 100644
@@ -1187,13 +1187,14 @@ static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_contex
 		return true;
 
 	/*
-	 * Block the kthread when the system is in an emergency or panic mode.
-	 * It increases the chance that these contexts would be able to show
-	 * the messages directly. And it reduces the risk of interrupted writes
-	 * where the context with a higher priority takes over the nbcon console
-	 * ownership in the middle of a message.
+	 * Block the kthread when the system is in an emergency, going down,
+	 * or panic mode. It increases the chance that these contexts would
+	 * be able to show the messages directly. And it reduces the risk of
+	 * interrupted writes where the context with a higher priority takes
+	 * over the nbcon console ownership in the middle of a message.
 	 */
 	if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)) ||
+	    unlikely(system_state > SYSTEM_RUNNING) ||
 	    unlikely(panic_in_progress()))
 		return false;
 
@@ -1249,10 +1250,12 @@ static int nbcon_kthread_func(void *__console)
 			return 0;
 
 		/*
-		 * Block the kthread when the system is in an emergency or panic
-		 * mode. See nbcon_kthread_should_wakeup() for more details.
+		 * Block the kthread when the system is in an emergency, going
+		 * down, or panic mode. See nbcon_kthread_should_wakeup() for
+		 * more details.
 		 */
 		if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)) ||
+		    unlikely(system_state > SYSTEM_RUNNING) ||
 		    unlikely(panic_in_progress()))
 			goto wait_for_event;
 

But wait, this might cause regression on netconsole which sets CON_NBCON_ATOMIC_UNSAFE and is not able to flush the messages directly a safe way.

A possibility would be to use con->write_thread() when pr_flush() is called in task context. It should be safe in most shutdown code paths except for the emergency_restart().

And we need an explicit pr_flush() even later in the halt and maybe even some unsafe flush later in emergency_restart() code path.

Sigh, this is getting complicated.

Best Regards,
Petr

#7 lore raw
Quoted 37 lines · Petr Mladek
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Petr Mladek

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various reboot and halt code paths. They can be detected by the system_state variable.

Quoted 7 lines
Quoted 4 lines

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

while this seems okay, didn't we have pr_flush() to flush the output on shutdown/ reboot?

Good point! We should clean this up.

Anyway, IMHO, we should switch to NBCON_PRIO_EMERGENCY for the post-running states and flush the pending messages immediately. It is more reliable. It is easy to find the locations when system_state() is set. And all follow-up printk() calls will try the direct flush so that they won't rely on an explicit flush.

An ideal solution would be to add an wrapper, e.g.

	void set_syste_state(enum system_states state)
	{
		system_state = state;

		if (system_state > SYSTEM_RUNNING)
			pr_flush(0, true);
	}
Quoted 7 lines

Another thing is that printk_trigger_flush() is an overkill. It tries to wake kthreads even via irq_work but we are interested only in the direct flush.

I am not sure why neither me nor John used pr_flush(). It might be because it originally did not flush atomic consoles directly. At least I had an outdated mental map.

The nbcon should be awake because there is outstanding printing to be done as far as I understand. It is just not running or behind. So you would need just wait until the thread is done before you continue.

This pops up now because the 8250 does it via the thread now. If you don't have an atomic console implementation but use the thread anyway (say the PREEMPT_RT case) then you would end up with the same problem, right?

Recently I did play with CPU shutdown and noticed that PREEMPT_RT misses an irq_work sync which explained missing printk output. You probably recall the printk part of this :)

Now that I look more into it, the above with pr_flush() does not work because by the time SYSTEM_SUSPEND is assigned it expects interrupts to be disabled (or disables them a few lines earlier).

emergency_restart() looks like a bad candidate for a flush. kernel_restart_prepare() on the other hand would be good. And there is a flush in kernel_power_off().

In general I would expect sysrq-b to reboot immediately without the flush but an ordinary reboot should flush.

Quoted 7 lines

Also the timeout should not be needed because all consoles should be flushed directly. But it can be solved by using zero timeout.

In fact, we should block the kthreads to prevent seeing more incomplete/interrupted messages, see the commit c41c0ebfa1e0eb ("printk/nbcon: Block printk kthreads when any CPU is in an emergency context"). Something like:

But if we have more than one console and more than one CPU we could let them flush in parallel and just wait for them do be done?

Quoted 40 lines
kernel/printk/nbcon.c +10 -7
index d8f8ec836eea..1c21c1e65b00 100644
@@ -1187,13 +1187,14 @@ static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_contex
 		return true;
 
 	/*
-	 * Block the kthread when the system is in an emergency or panic mode.
-	 * It increases the chance that these contexts would be able to show
-	 * the messages directly. And it reduces the risk of interrupted writes
-	 * where the context with a higher priority takes over the nbcon console
-	 * ownership in the middle of a message.
+	 * Block the kthread when the system is in an emergency, going down,
+	 * or panic mode. It increases the chance that these contexts would
+	 * be able to show the messages directly. And it reduces the risk of
+	 * interrupted writes where the context with a higher priority takes
+	 * over the nbcon console ownership in the middle of a message.
 	 */
 	if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)) ||
+	    unlikely(system_state > SYSTEM_RUNNING) ||
 	    unlikely(panic_in_progress()))
 		return false;
 
@@ -1249,10 +1250,12 @@ static int nbcon_kthread_func(void *__console)
 			return 0;
 
 		/*
-		 * Block the kthread when the system is in an emergency or panic
-		 * mode. See nbcon_kthread_should_wakeup() for more details.
+		 * Block the kthread when the system is in an emergency, going
+		 * down, or panic mode. See nbcon_kthread_should_wakeup() for
+		 * more details.
 		 */
 		if (unlikely(atomic_read(&nbcon_cpu_emergency_cnt)) ||
+		    unlikely(system_state > SYSTEM_RUNNING) ||
 		    unlikely(panic_in_progress()))
 			goto wait_for_event;
 
Quoted 3 lines

But wait, this might cause regression on netconsole which sets CON_NBCON_ATOMIC_UNSAFE and is not able to flush the messages directly a safe way.

same as PREEMPT_RT.

Quoted 9 lines

A possibility would be to use con->write_thread() when pr_flush() is called in task context. It should be safe in most shutdown code paths except for the emergency_restart().

And we need an explicit pr_flush() even later in the halt and maybe even some unsafe flush later in emergency_restart() code path.

Sigh, this is getting complicated.

So maybe just identify the few spots in the reboot case.

Quoted 2 lines

Best Regards,
Petr

Sebastian

maintainer
#8 lore raw
In reply to Petr Mladek
Quoted 5 lines · Petr Mladek

Anyway, IMHO, we should switch to NBCON_PRIO_EMERGENCY for the post-running states and flush the pending messages immediately. It is more reliable. It is easy to find the locations when system_state() is set. And all follow-up printk() calls will try the direct flush so that they won't rely on an explicit flush.

We need to be careful about abusing CPU state in order to get certain functionality. We already have things in place to gracefully switch to atomic after shutting down the printing kthreads.

I will not have a chance to seriously look at this until I get back next week. But at a quick glance, it looks like a roll of duct tape.

John

author maintainer
#9 lore raw
Quoted 10 lines · John Ogness
Quoted 5 lines · Petr Mladek

Anyway, IMHO, we should switch to NBCON_PRIO_EMERGENCY for the post-running states and flush the pending messages immediately. It is more reliable. It is easy to find the locations when system_state() is set. And all follow-up printk() calls will try the direct flush so that they won't rely on an explicit flush.

We need to be careful about abusing CPU state in order to get certain functionality. We already have things in place to gracefully switch to atomic after shutting down the printing kthreads.

This explains why I see the messages even without this patch. I wonder what is actually going on in the test.

The log contains:

</paste>
LKP: ttyS0: 256: LKP: tbox cant kexec and rebooting forcely </paste>

I see this message in lkp-tests/bin/lkp-setup-rootfs:

<cat lkp-tests/bin/lkp-setup-rootfs>
lkp_reboot()
{
local tbox_state="rebooting"
[ -n "$1" ] && tbox_state="rebooting_$1"

set_tbox_wtmp $tbox_state

# Avoid get stuck in yocto minimal rootfs. # LKP: rebooting
# [ 8.124081] LKP: rebooting
# # (shell prompt)
is_virt && [ -e '/proc/sysrq-trigger' ] && {
sync
echo s >/proc/sysrq-trigger echo b >/proc/sysrq-trigger
}

start_reboot_watchdog
reboot_tbox 2>/dev/null

exit
}
[...]
if tbox_cant_kexec; then
echo_to_tty "LKP: tbox cant kexec and rebooting forcely" lkp_reboot "cant_kexec"
fi
</cat lkp-tests/bin/lkp-setup-rootfs>

so, it ends with reboot_tbox which is:

<cat lkp-tests/lib/reboot.sh>
reboot_tbox()
{
reboot
}
</cat lkp-tests/lib/reboot.sh

which is classic reboot. I am not sure where it expects the warning. I actually can't find the code which prints:

<paste>

BUG: kernel reboot-without-warning in test stage

</paste>

I might need to try reproducing it with the lkp-test framework. But it has to wait for the next week.

Best Regards,
Petr

maintainer
#10 lore raw
In reply to Sebastian Andrzej Siewior
Quoted 9 lines · Sebastian Andrzej Siewior
Quoted 8 lines · Petr Mladek

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various reboot and halt code paths. They can be detected by the system_state variable.

Quoted 7 lines
Quoted 4 lines

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously. Also do not rely on printk() and explicitly flush the consoles after these states are set.

while this seems okay, didn't we have pr_flush() to flush the output on shutdown/ reboot?

We have the syscore.ops.shutdown registered notifier that gracefully shuts down the kthread printers and transitions to atomic printing.

There is a pr_flush() in kernel_power_off(), but actually that is no longer needed and should be removed.

John

maintainer
#11 lore raw
Quoted 8 lines · John Ogness
Quoted 2 lines

while this seems okay, didn't we have pr_flush() to flush the output on shutdown/ reboot?

We have the syscore.ops.shutdown registered notifier that gracefully shuts down the kthread printers and transitions to atomic printing.

There is a pr_flush() in kernel_power_off(), but actually that is no longer needed and should be removed.

After reviewing [0] I am reminded why pr_flush() exists in kernel_power_off(). It is purely for legacy consoles on PREEMPT_RT.

I am wondering if instead printk_kthreads_shutdown() should make an effort in this case (legacy + PREEMPT_RT):

kernel/printk/printk.c +9 -0
index 0bbf6409d467a..10b655f05fa56 100644
@@ -3776,6 +3776,15 @@ static void printk_kthreads_shutdown(void *data)
 		 */
 		nbcon_atomic_flush_pending();
 	}
+
+	/*
+	 * If legacy kthread printing is in use, give it a chance to
+	 * flush any backlog. The kthread is not stopped because when
+	 * forced, it is the only way legacy consoles can print.
+	 */
+	if (force_legacy_kthread() && printk_legacy_kthread)
+		pr_flush(1000, true);
+
 	console_list_unlock();
 }
 

That would still not cover the final "Power down", so probably the pr_flush() should also continue to exist in kernel_power_off(). But it is PREEMPT_RT + legacy specific, so I am not too happy about it.

John

[0] https://lore.kernel.org/lkml/a68e2e94-b4c6-4791-b581-ecbf3fee28e9@paulmck-laptop

maintainer
#12 lore raw
In reply to Petr Mladek
Quoted 7 lines · Petr Mladek

NBCON consoles emit messages in a dedicated kthreads when the system is working properly. printk() tries to flush them synchronously in explicitly marked emergency context and in panic().

Another situation where printk() could not rely on kthreads are the various reboot and halt code paths. They can be detected by the system_state variable.

We are already using the syscore.ops.shutdown notifier for this.

Quoted 2 lines

Let's default to NBCON_PRIO_EMERGENCY for the post-running states. printk() will automatically try flushing the consoles synchronously.

Switching to emergency state can cause ugly output and is best effort. For emergencies it is acceptable, but I am against this in the general case. A normal shutdown/reboot is not an emergency. I prefer we mark the appropriate emergency situations.

kernel/printk/nbcon.c enum nbcon_prio nbcon_get_default_prio(void) · line 1452

Quoted 12 lines
kernel/printk/nbcon.c +4 -0
index fb1b37741952..d8f8ec836eea 100644
@@ -1446,6 +1446,10 @@ enum nbcon_prio nbcon_get_default_prio(void)
 	if (panic_on_this_cpu())
 		return NBCON_PRIO_PANIC;
 
+	/* Do not rely on kthreads when the system is going down. */
+	if (system_state > SYSTEM_RUNNING)
+		return NBCON_PRIO_EMERGENCY;
+

Too general. The vast majority of cases do not need this.

kernel/reboot.c void emergency_restart(void) · line 98

Quoted 9 lines
kernel/reboot.c +1 -0
index d177d89fcc33..776784a82499 100644
@@ -94,6 +95,7 @@ void emergency_restart(void)
 {
 	kmsg_dump(KMSG_DUMP_EMERG);
 	system_state = SYSTEM_RESTART;
+	printk_trigger_flush();

This is clearly an emergency. Similar to what we did for MIPS, I would add:

	nbcon_cpu_emergency_enter();

before triggering the flush.

kernel/reboot.c void kernel_restart_prepare(char *cmd) · line 107

Quoted 5 lines
kernel/reboot.c +1 -0
@@ -102,6 +104,7 @@ void kernel_restart_prepare(char *cmd)
 {
 	blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
 	system_state = SYSTEM_RESTART;
+	printk_trigger_flush();

This is not necessary. This is handled by the syscore notifier.

kernel/reboot.c static void kernel_shutdown_prepare(enum system_states state) · line 311

Quoted 8 lines
kernel/reboot.c +1 -0
 	usermodehelper_disable();
 	device_shutdown();
 }
@@ -305,6 +308,7 @@ static void kernel_shutdown_prepare(enum system_states state)
 	blocking_notifier_call_chain(&reboot_notifier_list,
 		(state == SYSTEM_HALT) ? SYS_HALT : SYS_POWER_OFF, NULL);
 	system_state = state;
+	printk_trigger_flush();

Ditto.

John