mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nicholas Piggin <npiggin@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Nicholas Piggin <npiggin@gmail.com>,
	linux-arch@vger.kernel.org, Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>
Subject: [RFC PATCH 3/3] irqflags: Warn on irq disable when disabled and enable when enabled
Date: Tue,  9 May 2023 21:07:39 +1000	[thread overview]
Message-ID: <20230509110739.241735-4-npiggin@gmail.com> (raw)
In-Reply-To: <20230509110739.241735-1-npiggin@gmail.com>

Add CONFIG_IRQFLAGS_DEBUG checks for local_irq_disable() when irqs
are already disabled, and local_irq_enable() when they are enabled.
This could help catch risky or unbalanced irq manipulation.

Signed-off-by: Nicholas Piggin <npiggin@gmail.com>
---
 include/linux/irqflags.h       | 26 ++++++++++++++++++++++++--
 kernel/locking/irqflag-debug.c | 14 ++++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/include/linux/irqflags.h b/include/linux/irqflags.h
index 5ec0fa71399e..82f54cda2c20 100644
--- a/include/linux/irqflags.h
+++ b/include/linux/irqflags.h
@@ -157,6 +157,18 @@ do {						\
 #endif
 
 #ifdef CONFIG_DEBUG_IRQFLAGS
+extern void warn_bogus_irq_disable(void);
+#define raw_check_bogus_irq_disable()			\
+	do {						\
+		if (unlikely(arch_irqs_disabled()))	\
+			warn_bogus_irq_disable();	\
+	} while (0)
+extern void warn_bogus_irq_enable(void);
+#define raw_check_bogus_irq_enable()			\
+	do {						\
+		if (unlikely(!arch_irqs_disabled()))	\
+			warn_bogus_irq_enable();	\
+	} while (0)
 extern void warn_bogus_irq_restore(void);
 #define raw_check_bogus_irq_restore()			\
 	do {						\
@@ -164,14 +176,24 @@ extern void warn_bogus_irq_restore(void);
 			warn_bogus_irq_restore();	\
 	} while (0)
 #else
+#define raw_check_bogus_irq_disable() do { } while (0)
+#define raw_check_bogus_irq_enable() do { } while (0)
 #define raw_check_bogus_irq_restore() do { } while (0)
 #endif
 
 /*
  * Wrap the arch provided IRQ routines to provide appropriate checks.
  */
-#define raw_local_irq_disable()		arch_local_irq_disable()
-#define raw_local_irq_enable()		arch_local_irq_enable()
+#define raw_local_irq_disable()				\
+	do {						\
+		raw_check_bogus_irq_disable();		\
+		arch_local_irq_disable();		\
+	} while (0)
+#define raw_local_irq_enable()				\
+	do {						\
+		raw_check_bogus_irq_enable();		\
+		arch_local_irq_enable();		\
+	} while (0)
 #define raw_local_irq_save(flags)			\
 	do {						\
 		typecheck(unsigned long, flags);	\
diff --git a/kernel/locking/irqflag-debug.c b/kernel/locking/irqflag-debug.c
index 810b50344d35..20e5e1b9a86f 100644
--- a/kernel/locking/irqflag-debug.c
+++ b/kernel/locking/irqflag-debug.c
@@ -4,6 +4,20 @@
 #include <linux/export.h>
 #include <linux/irqflags.h>
 
+noinstr void warn_bogus_irq_disable(void)
+{
+	instrumentation_begin();
+	WARN_ONCE(1, "raw_local_irq_disable() called with IRQs disabled\n");
+	instrumentation_end();
+}
+EXPORT_SYMBOL(warn_bogus_irq_disable);
+noinstr void warn_bogus_irq_enable(void)
+{
+	instrumentation_begin();
+	WARN_ONCE(1, "raw_local_irq_enable() called with IRQs enabled\n");
+	instrumentation_end();
+}
+EXPORT_SYMBOL(warn_bogus_irq_enable);
 noinstr void warn_bogus_irq_restore(void)
 {
 	instrumentation_begin();
-- 
2.40.1


  parent reply	other threads:[~2023-05-09 11:08 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-09 11:07 [RFC PATCH 0/3] Prohibit irq disable when disabled or " Nicholas Piggin
2023-05-09 11:07 ` [RFC PATCH 1/3] hrtimer: balance irq save/restore Nicholas Piggin
2023-05-09 11:07 ` [RFC PATCH 2/3] init: Require archs call start_kernel with arch irqs disabled Nicholas Piggin
2023-05-09 11:07 ` Nicholas Piggin [this message]
2023-05-22  8:25   ` [RFC PATCH 3/3] irqflags: Warn on irq disable when disabled and enable when enabled kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230509110739.241735-4-npiggin@gmail.com \
    --to=npiggin@gmail.com \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®