[RFC][PATCHv3 5/5] printk: register PM notifier

From: Sergey Senozhatsky
Date: Tue May 09 2017 - 04:30:50 EST


It's not always possible/safe to wake_up() printk kernel
thread. For example, late suspend/early resume may printk()
while timekeeping is not initialized yet, so calling into the
scheduler may result in recursive warnings.

Another thing to notice is the fact PM at some point
freezes user space and kernel threads: freeze_processes()
and freeze_kernel_threads(), correspondingly. Thus we need
printk() to operate in emergency mode there and attempt to
immediately flush pending kernel message to the console.

This patch registers PM notifier, so PM can switch printk
to emergency mode from PM_FOO_PREPARE notifiers and return
back to printk threaded mode from PM_POST_FOO notifiers.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@xxxxxxxxx>
Suggested-by: Andreas Mohr <andi@xxxxxxxx>
---
kernel/printk/printk.c | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)

diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index 81ea575728b9..6aae36a29aca 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -50,6 +50,7 @@
#include <linux/sched/task_stack.h>
#include <linux/kthread.h>
#include <uapi/linux/sched/types.h>
+#include <linux/suspend.h>

#include <linux/uaccess.h>
#include <asm/sections.h>
@@ -2928,6 +2929,30 @@ static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) = {
.flags = IRQ_WORK_LAZY,
};

+static int printk_pm_notify(struct notifier_block *notify_block,
+ unsigned long mode, void *unused)
+{
+ switch (mode) {
+ case PM_HIBERNATION_PREPARE:
+ case PM_SUSPEND_PREPARE:
+ case PM_RESTORE_PREPARE:
+ printk_emergency_begin();
+ break;
+
+ case PM_POST_SUSPEND:
+ case PM_POST_HIBERNATION:
+ case PM_POST_RESTORE:
+ printk_emergency_end();
+ break;
+ }
+
+ return 0;
+}
+
+static struct notifier_block printk_pm_nb = {
+ .notifier_call = printk_pm_notify,
+};
+
static int printk_kthread_func(void *data)
{
while (1) {
@@ -2961,6 +2986,8 @@ static int __init init_printk_kthread(void)

sched_setscheduler(thread, SCHED_FIFO, &param);
printk_kthread = thread;
+
+ WARN_ON(register_pm_notifier(&printk_pm_nb) != 0);
return 0;
}
late_initcall(init_printk_kthread);
--
2.12.2