[printk,v2,07/26] printk: Check printk_deferred_enter()/_exit() usage

Message ID 20240218185726.1994771-8-john.ogness@linutronix.de
State New
Headers
Series wire up write_atomic() printing |

Commit Message

John Ogness Feb. 18, 2024, 6:57 p.m. UTC
  From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>

Add validation that printk_deferred_enter()/_exit() are called in
non-migration contexts.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
 include/linux/printk.h      |  7 +++++--
 kernel/printk/printk_safe.c | 12 ++++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
  

Comments

Petr Mladek Feb. 21, 2024, 9:55 a.m. UTC | #1
On Sun 2024-02-18 20:03:07, John Ogness wrote:
> From: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> 
> Add validation that printk_deferred_enter()/_exit() are called in
> non-migration contexts.
> 
> --- a/include/linux/printk.h
> +++ b/include/linux/printk.h
> @@ -159,13 +159,16 @@ __printf(1, 2) __cold int _printk_deferred(const char *fmt, ...);
>  
>  extern void __printk_safe_enter(void);
>  extern void __printk_safe_exit(void);

It seems that these two does not longer need to stay in
the global linux/printk.h. The declaration can be
moved to kernel/printk/internal.h (with 'extern' removed).

> +extern void __printk_deferred_enter(void);
> +extern void __printk_deferred_exit(void);
> +
>  /*
>   * The printk_deferred_enter/exit macros are available only as a hack for
>   * some code paths that need to defer all printk console printing. Interrupts
>   * must be disabled for the deferred duration.
>   */
> -#define printk_deferred_enter __printk_safe_enter
> -#define printk_deferred_exit __printk_safe_exit
> +#define printk_deferred_enter() __printk_deferred_enter()
> +#define printk_deferred_exit() __printk_deferred_exit()
>  
>  /*
>   * Please don't use printk_ratelimit(), because it shares ratelimiting state
> diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c
> index 6d10927a07d8..8d9408d653de 100644
> --- a/kernel/printk/printk_safe.c
> +++ b/kernel/printk/printk_safe.c
> @@ -26,6 +26,18 @@ void __printk_safe_exit(void)
>  	this_cpu_dec(printk_context);
>  }
>  
> +void __printk_deferred_enter(void)
> +{
> +	cant_migrate();
> +	this_cpu_inc(printk_context);

I would prefer to call __printk_safe_enter() here to make
it clear that they update printk_context the same way
and have the same effect.

Let's make compiler to do the inlining.

> +}
> +
> +void __printk_deferred_exit(void)
> +{
> +	cant_migrate();
> +	this_cpu_dec(printk_context);

ditto

> +}
> +
>  asmlinkage int vprintk(const char *fmt, va_list args)
>  {
>  #ifdef CONFIG_KGDB_KDB

With the two changes:

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr
  

Patch

diff --git a/include/linux/printk.h b/include/linux/printk.h
index 955e31860095..8d5c5588eec9 100644
--- a/include/linux/printk.h
+++ b/include/linux/printk.h
@@ -159,13 +159,16 @@  __printf(1, 2) __cold int _printk_deferred(const char *fmt, ...);
 
 extern void __printk_safe_enter(void);
 extern void __printk_safe_exit(void);
+extern void __printk_deferred_enter(void);
+extern void __printk_deferred_exit(void);
+
 /*
  * The printk_deferred_enter/exit macros are available only as a hack for
  * some code paths that need to defer all printk console printing. Interrupts
  * must be disabled for the deferred duration.
  */
-#define printk_deferred_enter __printk_safe_enter
-#define printk_deferred_exit __printk_safe_exit
+#define printk_deferred_enter() __printk_deferred_enter()
+#define printk_deferred_exit() __printk_deferred_exit()
 
 /*
  * Please don't use printk_ratelimit(), because it shares ratelimiting state
diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c
index 6d10927a07d8..8d9408d653de 100644
--- a/kernel/printk/printk_safe.c
+++ b/kernel/printk/printk_safe.c
@@ -26,6 +26,18 @@  void __printk_safe_exit(void)
 	this_cpu_dec(printk_context);
 }
 
+void __printk_deferred_enter(void)
+{
+	cant_migrate();
+	this_cpu_inc(printk_context);
+}
+
+void __printk_deferred_exit(void)
+{
+	cant_migrate();
+	this_cpu_dec(printk_context);
+}
+
 asmlinkage int vprintk(const char *fmt, va_list args)
 {
 #ifdef CONFIG_KGDB_KDB