Skip to contentSkip to conversation

lockdep: report broken irq restoration

PATCH Superseded Mark Rutland 4 participants 7 messages Dec 9, 2020 – Jan 4, 2021

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lockdep: report broken irq restoration

We generally expect local_irq_save() and local_irq_restore() to be paired and sanely nested, and so local_irq_restore() expects to be called with irqs disabled. Thus, within local_irq_restore() we only trace irq flag changes when unmasking irqs.

This means that a seuence such as:

| local_irq_disable();
| local_irq_save(flags);
| local_irq_enable();
| local_irq_restore(flags);

... is liable to break things, as the local_irq_restore() would mask IRQs without tracing this change.

We don't consider such sequences to be a good idea, so let's define those as forbidden, and add tooling to detect such broken cases.

This patch adds debug code to WARN() when local_irq_restore() is called with irqs enabled. As local_irq_restore() is expected to pair with local_irq_save(), it should never be called with interrupts enabled.

To avoid the possibility of circular header dependencies beteen irqflags.h and bug.h, the warning is handled in a separate C file.

The new code is all conditional on a new CONFIG_DEBUG_IRQFLAGS symbol which is independent of CONFIG_TRACE_IRQFLAGS. As noted above such cases will confuse lockdep, so CONFIG_DEBUG_LOCKDEP now selects CONFIG_DEBUG_IRQFLAGS.

Cc Andy Lutomirski <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 include/linux/irqflags.h       | 18 +++++++++++++++++-
 kernel/locking/Makefile        |  1 +
 kernel/locking/irqflag-debug.c | 12 ++++++++++++
 lib/Kconfig.debug              |  7 +++++++
 4 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 kernel/locking/irqflag-debug.c

Note: as things stand this'll blow up at boot-time on x86 within the io-apic timer_irq_works() boot-time test. I've proposed a fix for that:

https://lore.kernel.org/lkml/[email protected]/

... which was sufficient for booting under QEMU without splats. I'm giving this a soak under Syzkaller on arm64 as that booted cleanly to begin with.

Mark.

include/linux/irqflags.h +17 -1
index 3ed4e8771b64..bca3c6fa8270 100644
@@ -220,10 +220,26 @@ do {						\
 
 #else /* !CONFIG_TRACE_IRQFLAGS */
 
+#ifdef CONFIG_DEBUG_IRQFLAGS
+extern void warn_bogus_irq_restore(bool *warned);
+#define check_bogus_irq_restore()				\
+	do {							\
+		static bool __section(".data.once") __warned;	\
+		if (unlikely(!raw_irqs_disabled()))		\
+			warn_bogus_irq_restore(&__warned);	\
+	} while (0)
+#else
+#define check_bogus_irq_restore() do { } while (0)
+#endif
+
 #define local_irq_enable()	do { raw_local_irq_enable(); } while (0)
 #define local_irq_disable()	do { raw_local_irq_disable(); } while (0)
 #define local_irq_save(flags)	do { raw_local_irq_save(flags); } while (0)
-#define local_irq_restore(flags) do { raw_local_irq_restore(flags); } while (0)
+#define local_irq_restore(flags)		\
+	do {					\
+		check_bogus_irq_restore();	\
+		raw_local_irq_restore(flags);	\
+	} while (0)
 #define safe_halt()		do { raw_safe_halt(); } while (0)
 
 #endif /* CONFIG_TRACE_IRQFLAGS */
kernel/locking/Makefile +1 -0
index 6d11cfb9b41f..8838f1d7c4a2 100644
@@ -15,6 +15,7 @@ CFLAGS_REMOVE_mutex-debug.o = $(CC_FLAGS_FTRACE)
 CFLAGS_REMOVE_rtmutex-debug.o = $(CC_FLAGS_FTRACE)
 endif
 
+obj-$(CONFIG_DEBUG_IRQFLAGS) += irqflag-debug.o
 obj-$(CONFIG_DEBUG_MUTEXES) += mutex-debug.o
 obj-$(CONFIG_LOCKDEP) += lockdep.o
 ifeq ($(CONFIG_PROC_FS),y)
kernel/locking/irqflag-debug.c new file +12 -0
new file mode 100644
index 000000000000..3024c6837ac2
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/bug.h>
+
+void warn_bogus_irq_restore(bool *warned)
+{
+	if (*warned)
+		return;
+
+	*warned = true;
+	WARN(1, "local_irq_restore() called with IRQs enabled\n");
+}
lib/Kconfig.debug +7 -0
index c789b39ed527..f7895d57bb08 100644
@@ -1312,6 +1312,7 @@ config LOCKDEP_SMALL
 config DEBUG_LOCKDEP
 	bool "Lock dependency engine debugging"
 	depends on DEBUG_KERNEL && LOCKDEP
+	select DEBUG_IRQFLAGS
 	help
 	  If you say Y here, the lock dependency engine will do
 	  additional runtime checks to debug itself, at the price
@@ -1400,6 +1401,12 @@ config TRACE_IRQFLAGS_NMI
 	depends on TRACE_IRQFLAGS
 	depends on TRACE_IRQFLAGS_NMI_SUPPORT
 
+config DEBUG_IRQFLAGS
+	bool "Interrupt mask debugging"
+	help
+	  Enables checks for potentially unsafe enabling or disabling of
+	  interrupts.
+
 config STACKTRACE
 	bool "Stack backtrace support"
 	depends on STACKTRACE_SUPPORT
Signature · 1 line
2.11.0
#1 lore raw
Quoted 61 lines · Mark Rutland

We generally expect local_irq_save() and local_irq_restore() to be paired and sanely nested, and so local_irq_restore() expects to be called with irqs disabled. Thus, within local_irq_restore() we only trace irq flag changes when unmasking irqs.

This means that a seuence such as:

| local_irq_disable();
| local_irq_save(flags);
| local_irq_enable();
| local_irq_restore(flags);

... is liable to break things, as the local_irq_restore() would mask IRQs without tracing this change.

We don't consider such sequences to be a good idea, so let's define those as forbidden, and add tooling to detect such broken cases.

This patch adds debug code to WARN() when local_irq_restore() is called with irqs enabled. As local_irq_restore() is expected to pair with local_irq_save(), it should never be called with interrupts enabled.

To avoid the possibility of circular header dependencies beteen irqflags.h and bug.h, the warning is handled in a separate C file.

The new code is all conditional on a new CONFIG_DEBUG_IRQFLAGS symbol which is independent of CONFIG_TRACE_IRQFLAGS. As noted above such cases will confuse lockdep, so CONFIG_DEBUG_LOCKDEP now selects CONFIG_DEBUG_IRQFLAGS.

Cc Andy Lutomirski <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Juergen Gross <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Thomas Gleixner <[email protected]>
---
 include/linux/irqflags.h       | 18 +++++++++++++++++-
 kernel/locking/Makefile        |  1 +
 kernel/locking/irqflag-debug.c | 12 ++++++++++++
 lib/Kconfig.debug              |  7 +++++++
 4 files changed, 37 insertions(+), 1 deletion(-)
 create mode 100644 kernel/locking/irqflag-debug.c

Note: as things stand this'll blow up at boot-time on x86 within the io-apic timer_irq_works() boot-time test. I've proposed a fix for that:

https://lore.kernel.org/lkml/[email protected]/

... which was sufficient for booting under QEMU without splats. I'm giving this a soak under Syzkaller on arm64 as that booted cleanly to begin with.

Mark.

include/linux/irqflags.h +0 -0
index 3ed4e8771b64..bca3c6fa8270 100644
@@ -220,10 +220,26 @@ do {                                              \
 

include/linux/irqflags.h do { \ · line 230

Quoted 10 lines
include/linux/irqflags.h +8 -0
 #else /* !CONFIG_TRACE_IRQFLAGS */
 
+#ifdef CONFIG_DEBUG_IRQFLAGS
+extern void warn_bogus_irq_restore(bool *warned);
+#define check_bogus_irq_restore()                              \
+       do {                                                    \
+               static bool __section(".data.once") __warned;   \
+               if (unlikely(!raw_irqs_disabled()))             \
+                       warn_bogus_irq_restore(&__warned);      \
+       } while (0)

What's the benefit of having a per-caller __warned instead of just having a single global one in warn_bogus_irq_restore()?

#2 lore raw
Quoted 9 lines · Andy Lutomirski
Quoted 8 lines · Mark Rutland
include/linux/irqflags.h +0 -0
index 3ed4e8771b64..bca3c6fa8270 100644
@@ -220,10 +220,26 @@ do {                                              \
 
 #else /* !CONFIG_TRACE_IRQFLAGS */
 
Quoted 11 lines

include/linux/irqflags.h do { \ · line 230

Quoted 8 lines
include/linux/irqflags.h +8 -0
+#ifdef CONFIG_DEBUG_IRQFLAGS
+extern void warn_bogus_irq_restore(bool *warned);
+#define check_bogus_irq_restore()                              \
+       do {                                                    \
+               static bool __section(".data.once") __warned;   \
+               if (unlikely(!raw_irqs_disabled()))             \
+                       warn_bogus_irq_restore(&__warned);      \
+       } while (0)

What's the benefit of having a per-caller __warned instead of just having a single global one in warn_bogus_irq_restore()?

I'd copied that from the previous proposal, and had assumed that if we had several distinct violations we'd want to report all of them in one boot session. I'm perfectly happy to get rid of that and make warn_bogus_irq_restore() use WARN_ONCE() directly.

I'm using this with a local Syzkaller instance, and as that will kill the kernel on the first WARN() anyway, it makes no difference to me. I guess in the field having a single warning is likely to be good enough too.

Thanks,
Mark.

#3 lore raw
In reply to Mark Rutland
I love your patch! Perhaps something to improve:

Hi Mark,

I love your patch! Perhaps something to improve:

[auto build test WARNING on tip/locking/core]
[also build test WARNING on linux/master linus/master v5.10-rc7 next-20201209] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Mark-Rutland/lockdep-report-broken-irq-restoration/20201210-023758
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git cb262935a166bdef0ccfe6e2adffa00c0f2d038a
config: mips-randconfig-r026-20201209 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 1968804ac726e7674d5de22bc2204b45857da344)

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# install mips cross compiling tool for clang build
# apt-get install binutils-mips-linux-gnu
# https://github.com/0day-ci/linux/commit/01544766d5d8d1aa2604444debae272f53f058f9
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mark-Rutland/lockdep-report-broken-irq-restoration/20201210-023758 git checkout 01544766d5d8d1aa2604444debae272f53f058f9
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=mips

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> kernel/locking/irqflag-debug.c:5:6: warning: no previous prototype for function 'warn_bogus_irq_restore' [-Wmissing-prototypes]
   void warn_bogus_irq_restore(bool *warned)
        ^

kernel/locking/irqflag-debug.c:5:1: note: declare 'static' if the function is not intended to be used outside of this translation unit void warn_bogus_irq_restore(bool *warned)
^
static
1 warning generated.

vim +/warn_bogus_irq_restore +5 kernel/locking/irqflag-debug.c

     4	
   > 5	void warn_bogus_irq_restore(bool *warned)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#4 lore raw
In reply to Mark Rutland
I love your patch! Perhaps something to improve:

Hi Mark,

I love your patch! Perhaps something to improve:

[auto build test WARNING on tip/locking/core]
[also build test WARNING on linux/master linus/master v5.10-rc7 next-20201209] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Mark-Rutland/lockdep-report-broken-irq-restoration/20201210-023758
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git cb262935a166bdef0ccfe6e2adffa00c0f2d038a
config: arc-randconfig-r032-20201209 (attached as .config)
compiler: arceb-elf-gcc (GCC) 9.3.0

reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# https://github.com/0day-ci/linux/commit/01544766d5d8d1aa2604444debae272f53f058f9
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Mark-Rutland/lockdep-report-broken-irq-restoration/20201210-023758 git checkout 01544766d5d8d1aa2604444debae272f53f058f9
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-9.3.0 make.cross ARCH=arc

If you fix the issue, kindly add following tag as appropriate

All warnings (new ones prefixed by >>):

>> kernel/locking/irqflag-debug.c:5:6: warning: no previous prototype for 'warn_bogus_irq_restore' [-Wmissing-prototypes]

5 | void warn_bogus_irq_restore(bool *warned)
| ^~~~~~~~~~~~~~~~~~~~~~

vim +/warn_bogus_irq_restore +5 kernel/locking/irqflag-debug.c

     4	
   > 5	void warn_bogus_irq_restore(bool *warned)

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

maintainer
#5 lore raw
In reply to Mark Rutland
Quoted 1 line · Mark Rutland

This means that a seuence such as:

+q

Quoted 19 lines
include/linux/irqflags.h +11 -0
index 3ed4e8771b64..bca3c6fa8270 100644
@@ -220,10 +220,26 @@ do {						\
 
 #else /* !CONFIG_TRACE_IRQFLAGS */
 
+#ifdef CONFIG_DEBUG_IRQFLAGS
+extern void warn_bogus_irq_restore(bool *warned);
+#define check_bogus_irq_restore()				\
+	do {							\
+		static bool __section(".data.once") __warned;	\
+		if (unlikely(!raw_irqs_disabled()))		\
+			warn_bogus_irq_restore(&__warned);	\
+	} while (0)
+#else
+#define check_bogus_irq_restore() do { } while (0)
+#endif

include/linux/irqflags.h do { \ · line 242

Quoted 10 lines
include/linux/irqflags.h +6 -1
+
 #define local_irq_enable()	do { raw_local_irq_enable(); } while (0)
 #define local_irq_disable()	do { raw_local_irq_disable(); } while (0)
 #define local_irq_save(flags)	do { raw_local_irq_save(flags); } while (0)
-#define local_irq_restore(flags) do { raw_local_irq_restore(flags); } while (0)
+#define local_irq_restore(flags)		\
+	do {					\
+		check_bogus_irq_restore();	\
+		raw_local_irq_restore(flags);	\
+	} while (0)

Shouldn't that be in raw_local_irq_restore() ?

include/linux/irqflags.h do { \ · line 245

Quoted 3 lines
include/linux/irqflags.h +0 -0
 #define safe_halt()		do { raw_safe_halt(); } while (0)
 
 #endif /* CONFIG_TRACE_IRQFLAGS */
include/linux/irqflags.h +2 -5
@@ -162,6 +162,7 @@ do {						\
 #define raw_local_irq_restore(flags)			\
 	do {						\
 		typecheck(unsigned long, flags);	\
+		check_bogus_irq_restore();		\
 		arch_local_irq_restore(flags);		\
 	} while (0)
 #define raw_local_save_flags(flags)			\
@@ -235,11 +236,7 @@ extern void warn_bogus_irq_restore(bool
 #define local_irq_enable()	do { raw_local_irq_enable(); } while (0)
 #define local_irq_disable()	do { raw_local_irq_disable(); } while (0)
 #define local_irq_save(flags)	do { raw_local_irq_save(flags); } while (0)
-#define local_irq_restore(flags)		\
-	do {					\
-		check_bogus_irq_restore();	\
-		raw_local_irq_restore(flags);	\
-	} while (0)
+#define local_irq_restore(flags) do { raw_local_irq_restore(flags); } while (0)
 #define safe_halt()		do { raw_safe_halt(); } while (0)
 
 #endif /* CONFIG_TRACE_IRQFLAGS */
#6 lore raw
Quoted 28 lines · Peter Zijlstra
Quoted 1 line · Mark Rutland

This means that a seuence such as:

+q

Quoted 23 lines
include/linux/irqflags.h +12 -0
index 3ed4e8771b64..bca3c6fa8270 100644
@@ -220,10 +220,26 @@ do {						\
 
 #else /* !CONFIG_TRACE_IRQFLAGS */
 
+#ifdef CONFIG_DEBUG_IRQFLAGS
+extern void warn_bogus_irq_restore(bool *warned);
+#define check_bogus_irq_restore()				\
+	do {							\
+		static bool __section(".data.once") __warned;	\
+		if (unlikely(!raw_irqs_disabled()))		\
+			warn_bogus_irq_restore(&__warned);	\
+	} while (0)
+#else
+#define check_bogus_irq_restore() do { } while (0)
+#endif
+
 #define local_irq_enable()	do { raw_local_irq_enable(); } while (0)
 #define local_irq_disable()	do { raw_local_irq_disable(); } while (0)
 #define local_irq_save(flags)	do { raw_local_irq_save(flags); } while (0)
Quoted 8 lines

include/linux/irqflags.h do { \ · line 242

Quoted 6 lines
include/linux/irqflags.h +5 -1
-#define local_irq_restore(flags) do { raw_local_irq_restore(flags); } while (0)
+#define local_irq_restore(flags)		\
+	do {					\
+		check_bogus_irq_restore();	\
+		raw_local_irq_restore(flags);	\
+	} while (0)

Shouldn't that be in raw_local_irq_restore() ?

Yup, that'd be preferable. Note that will require a refactoring here if we want the warning to be available regardless of CONFIG_TRACE_IRQFLAGS.

I'd intended to rejig that for v2, but I didn't get the chance before finishing work for the year, and had only done some basic rework in my WIP (e.g. removing the per-instance bool as per Andy's comments):

https://git.kernel.org/pub/scm/linux/kernel/git/mark/linux.git/commit/?h=fuzzing/5.10-rc7&id=61336e25d1415a4ac3aaf8cf75105c2ec2eb95e7

I'll try to respin that in the next few days if I get the chance.

Thanks,
Mark.

Quoted 28 lines
Quoted 4 lines
include/linux/irqflags.h +0 -0
 #define safe_halt()		do { raw_safe_halt(); } while (0)
 
 #endif /* CONFIG_TRACE_IRQFLAGS */
include/linux/irqflags.h +2 -5
@@ -162,6 +162,7 @@ do {						\
 #define raw_local_irq_restore(flags)			\
 	do {						\
 		typecheck(unsigned long, flags);	\
+		check_bogus_irq_restore();		\
 		arch_local_irq_restore(flags);		\
 	} while (0)
 #define raw_local_save_flags(flags)			\
@@ -235,11 +236,7 @@ extern void warn_bogus_irq_restore(bool
 #define local_irq_enable()	do { raw_local_irq_enable(); } while (0)
 #define local_irq_disable()	do { raw_local_irq_disable(); } while (0)
 #define local_irq_save(flags)	do { raw_local_irq_save(flags); } while (0)
-#define local_irq_restore(flags)		\
-	do {					\
-		check_bogus_irq_restore();	\
-		raw_local_irq_restore(flags);	\
-	} while (0)
+#define local_irq_restore(flags) do { raw_local_irq_restore(flags); } while (0)
 #define safe_halt()		do { raw_safe_halt(); } while (0)
 
 #endif /* CONFIG_TRACE_IRQFLAGS */