[RFC][PATCHv5 03/13] printk: add sync printk_emergency API

Sergey Senozhatsky
2017-08-15T03:00:10Z

We already have `async' printk_emergency_begin(), which returns
immediately and does not guarantee that `printk_kthread' will
stop by the time it returns. Add `sync' version, which waits for
`printk_kthread' to stop.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
include/linux/console.h | 2 ++
kernel/printk/printk.c | 38 ++++++++++++++++++++++++++++++++++++--
2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/include/linux/console.h b/include/linux/console.h
index 07005db4c788..8ce29b2381d2 100644
--- a/include/linux/console.h
+++ b/include/linux/console.h
@@ -189,6 +189,8 @@ extern void resume_console(void);

extern void printk_emergency_begin(void);
extern void printk_emergency_end(void);
+extern int printk_emergency_begin_sync(void);
+extern int printk_emergency_end_sync(void);

int mda_console_init(void);
void prom_con_init(void);
diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c
index a86767d4d619..5d4b4beeebf5 100644
--- a/kernel/printk/printk.c
+++ b/kernel/printk/printk.c
@@ -485,8 +485,8 @@ static inline bool printk_kthread_should_stop(void)
{
if (current != printk_kthread)
return false;
- /* An emergency mode */
- return (atomic_read(&printk_emergency) != 0);
+ /* An emergency mode or a pending printk_emergency_begin_sync() */
+ return (atomic_read(&printk_emergency) != 0 || kthread_should_park());
}

/*
@@ -509,6 +509,34 @@ void printk_emergency_end(void)
}
EXPORT_SYMBOL_GPL(printk_emergency_end);

+/*
+ * This disables printing offloading and instead attempts
+ * to do the usual console_trylock()->console_unlock().
+ *
+ * Note, this does wait for printk_kthread to stop.
+ */
+int printk_emergency_begin_sync(void)
+{
+ atomic_inc(&printk_emergency);
+ if (!printk_kthread)
+ return -EINVAL;
+
+ return kthread_park(printk_kthread);
+}
+EXPORT_SYMBOL_GPL(printk_emergency_begin_sync);
+
+/* This re-enables printk_kthread offloading. */
+int printk_emergency_end_sync(void)
+{
+ atomic_dec(&printk_emergency);
+ if (!printk_kthread)
+ return -EINVAL;
+
+ kthread_unpark(printk_kthread);
+ return 0;
+}
+EXPORT_SYMBOL_GPL(printk_emergency_end_sync);
+
/*
* Under heavy printing load or with a slow serial console (or both)
* console_unlock() can stall CPUs, which can result in soft/hard-lockups,
@@ -2018,6 +2046,12 @@ EXPORT_SYMBOL_GPL(printk_emergency_begin);
void printk_emergency_end(void) {}
EXPORT_SYMBOL_GPL(printk_emergency_end);

+int printk_emergency_begin_sync(void) {}
+EXPORT_SYMBOL_GPL(printk_emergency_begin_sync);
+
+int printk_emergency_end_sync(void) {}
+EXPORT_SYMBOL_GPL(printk_emergency_end_sync);
+
static inline bool console_offload_printing(void) { return false; }

#endif /* CONFIG_PRINTK */
--
2.14.1