From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753170AbdHODCF (ORCPT ); Mon, 14 Aug 2017 23:02:05 -0400 Received: from mail-pg0-f68.google.com ([74.125.83.68]:33555 "EHLO mail-pg0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753333AbdHODAU (ORCPT ); Mon, 14 Aug 2017 23:00:20 -0400 From: Sergey Senozhatsky To: Petr Mladek , Steven Rostedt Cc: Jan Kara , Andrew Morton , Peter Zijlstra , "Rafael J . Wysocki" , Eric Biederman , Greg Kroah-Hartman , Jiri Slaby , Pavel Machek , Andreas Mohr , Tetsuo Handa , linux-kernel@vger.kernel.org, Sergey Senozhatsky , Sergey Senozhatsky Subject: [RFC][PATCHv5 06/13] printk: register PM notifier Date: Tue, 15 Aug 2017 11:56:18 +0900 Message-Id: <20170815025625.1977-7-sergey.senozhatsky@gmail.com> X-Mailer: git-send-email 2.14.1 In-Reply-To: <20170815025625.1977-1-sergey.senozhatsky@gmail.com> References: <20170815025625.1977-1-sergey.senozhatsky@gmail.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 that 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 Suggested-by: Andreas Mohr --- kernel/printk/printk.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 05165f008bc8..d3f149fad85c 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -49,6 +49,7 @@ #include #include #include +#include #include #include @@ -2913,6 +2914,33 @@ 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_sync(); + break; + + case PM_POST_SUSPEND: + case PM_POST_HIBERNATION: + case PM_POST_RESTORE: + printk_emergency_end_sync(); + break; + + default: + return NOTIFY_DONE; + } + + return NOTIFY_OK; +} + +static struct notifier_block printk_pm_nb = { + .notifier_call = printk_pm_notify, +}; + static int printk_kthread_func(void *data) { while (1) { @@ -2960,9 +2988,16 @@ static int __init init_printk_kthread(void) if (!alloc_cpumask_var(&printk_offload_cpus, GFP_KERNEL)) return -ENOMEM; + if (register_pm_notifier(&printk_pm_nb) != 0) { + pr_err("printk: unable to register PM notifier\n"); + free_cpumask_var(printk_offload_cpus); + return -EINVAL; + } + thread = kthread_run(printk_kthread_func, NULL, "printk"); if (IS_ERR(thread)) { pr_err("printk: unable to create printing thread\n"); + unregister_pm_notifier(&printk_pm_nb); free_cpumask_var(printk_offload_cpus); return PTR_ERR(thread); } -- 2.14.1