Skip to content

Commit f1a83e6

Browse files
Peter Zijlstraingomolnar
authored andcommitted
lockdep: Correctly annotate hardirq context in irq_exit()
There was a reported deadlock on -rt which lockdep didn't report. It turns out that in irq_exit() we tell lockdep that the hardirq context ends and then do all kinds of locking afterwards. To fix it, move trace_hardirq_exit() to the very end of irq_exit(), this ensures all locking in tick_irq_exit() and rcu_irq_exit() are properly recorded as happening from hardirq context. This however leads to the 'fun' little problem of running softirqs while in hardirq context. To cure this make the softirq code a little more complex (in the CONFIG_TRACE_IRQFLAGS case). Due to stack swizzling arch dependent trickery we cannot pass an argument to __do_softirq() to tell it if it was done from hardirq context or not; so use a side-band argument. When we do __do_softirq() from hardirq context, 'atomically' flip to softirq context and back, so that no locking goes without being in either hard- or soft-irq context. I didn't find any new problems in mainline using this patch, but it did show the -rt problem. Reported-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/n/tip-dgwc5cdksbn0jk09vbmcc9sa@git.kernel.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 9dd1220 commit f1a83e6

1 file changed

Lines changed: 45 additions & 9 deletions

File tree

‎kernel/softirq.c‎

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,52 @@ EXPORT_SYMBOL(local_bh_enable_ip);
213213
#define MAX_SOFTIRQ_TIME msecs_to_jiffies(2)
214214
#define MAX_SOFTIRQ_RESTART 10
215215

216+
#ifdef CONFIG_TRACE_IRQFLAGS
217+
/*
218+
* Convoluted means of passing __do_softirq() a message through the various
219+
* architecture execute_on_stack() bits.
220+
*
221+
* When we run softirqs from irq_exit() and thus on the hardirq stack we need
222+
* to keep the lockdep irq context tracking as tight as possible in order to
223+
* not miss-qualify lock contexts and miss possible deadlocks.
224+
*/
225+
static DEFINE_PER_CPU(int, softirq_from_hardirq);
226+
227+
static inline void lockdep_softirq_from_hardirq(void)
228+
{
229+
this_cpu_write(softirq_from_hardirq, 1);
230+
}
231+
232+
static inline void lockdep_softirq_start(void)
233+
{
234+
if (this_cpu_read(softirq_from_hardirq))
235+
trace_hardirq_exit();
236+
lockdep_softirq_enter();
237+
}
238+
239+
static inline void lockdep_softirq_end(void)
240+
{
241+
lockdep_softirq_exit();
242+
if (this_cpu_read(softirq_from_hardirq)) {
243+
this_cpu_write(softirq_from_hardirq, 0);
244+
trace_hardirq_enter();
245+
}
246+
}
247+
248+
#else
249+
static inline void lockdep_softirq_from_hardirq(void) { }
250+
static inline void lockdep_softirq_start(void) { }
251+
static inline void lockdep_softirq_end(void) { }
252+
#endif
253+
216254
asmlinkage void __do_softirq(void)
217255
{
218-
struct softirq_action *h;
219-
__u32 pending;
220256
unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
221-
int cpu;
222257
unsigned long old_flags = current->flags;
223258
int max_restart = MAX_SOFTIRQ_RESTART;
259+
struct softirq_action *h;
260+
__u32 pending;
261+
int cpu;
224262

225263
/*
226264
* Mask out PF_MEMALLOC s current task context is borrowed for the
@@ -233,7 +271,7 @@ asmlinkage void __do_softirq(void)
233271
account_irq_enter_time(current);
234272

235273
__local_bh_disable(_RET_IP_, SOFTIRQ_OFFSET);
236-
lockdep_softirq_enter();
274+
lockdep_softirq_start();
237275

238276
cpu = smp_processor_id();
239277
restart:
@@ -280,16 +318,13 @@ asmlinkage void __do_softirq(void)
280318
wakeup_softirqd();
281319
}
282320

283-
lockdep_softirq_exit();
284-
321+
lockdep_softirq_end();
285322
account_irq_exit_time(current);
286323
__local_bh_enable(SOFTIRQ_OFFSET);
287324
WARN_ON_ONCE(in_interrupt());
288325
tsk_restore_flags(current, old_flags, PF_MEMALLOC);
289326
}
290327

291-
292-
293328
asmlinkage void do_softirq(void)
294329
{
295330
__u32 pending;
@@ -332,6 +367,7 @@ void irq_enter(void)
332367
static inline void invoke_softirq(void)
333368
{
334369
if (!force_irqthreads) {
370+
lockdep_softirq_from_hardirq();
335371
#ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
336372
/*
337373
* We can safely execute softirq on the current stack if
@@ -377,13 +413,13 @@ void irq_exit(void)
377413
#endif
378414

379415
account_irq_exit_time(current);
380-
trace_hardirq_exit();
381416
preempt_count_sub(HARDIRQ_OFFSET);
382417
if (!in_interrupt() && local_softirq_pending())
383418
invoke_softirq();
384419

385420
tick_irq_exit();
386421
rcu_irq_exit();
422+
trace_hardirq_exit(); /* must be last! */
387423
}
388424

389425
/*

0 commit comments

Comments
 (0)