Skip to contentSkip to conversation

preempt: Debug for possible missed preemption checks

RFC PATCH In review Steven Rostedt 5 participants 9 messages Jan 17, 2014 – Jan 22, 2014

Subsystems (5)
lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

RFC PATCH 1/1 preempt: Debug for possible missed preemption checks

Peter Zijlstra mentioned that he wanted to catch the following problem:

local_irq_disable();
preempt_disable();
local_irq_enable();

local_irq_disable();
preempt_enable();
local_irq_enable();

Now what's wrong with the above? What happens if an interrupt comes in the middle of that (between the local_irq_enable() and local_irq_disable()) and sets the NEED_RESCHED flag? Because preemption is disabled, it wont schedule there. It expects the schedule will happen when preemption is enabled.

But the problem is that the preempt_enable() happens when a schedule can not take place (interrupts are disabled), so it is ignored. Now when interrupts are enabled, there's no NEED_RESCHED check, and we miss our schedule.

Note, the first preempt_disable() did not need to even be within the irq_disabled section. You can get the same problem with:

preempt_disable();

<interrupt - set NEED_RESCHED>

local_irq_disable();
preempt_enable();
local_irq_enable();

Basically anytime you go from preemption disabled and interrupts enabled, to preemption enabled and interrupts disabled without ever going back to preemption enabled and interrupts disabled state.

This can be solved by keeping track of the preemption and interrupt states for the CPU. Here's the state diagram:

 State 0:  Preempt Enabled, Interrupts Enabled  (PEIE)
 State 1:  Preempt Enabled, Interrupts Disabled (PEID)
 State 2:  Preempt Disabled, Interrupts Enabled (PDIEX) *
 State 3:  Preempt Disabled, Interrupts Enabled (PDIE) **
 State 4:  Preempt Disabled, Interrupts Disabled (PDID)
 State 5:  Preempt Disabled, Interrupts Disabled (PDIDX)
 State 6:  Preempt Enabled, Interrupts Disabled (PEIDX)
 State 7:  Preempt Enabled, Interrupts Enabled (PEIEX) ***

(*) State 2 is the state where problems can occur (an interrupt
setting NEED_RESCHED while preemption is disabled).

Notice that some of the states have the same preemption and interrupts disabled state. The difference between them is that those that went through state 2 (denoted with an "X"), can lead us to state 6 which is the state that can miss a preemption point.

(**) The difference between state 2 and state 3, is that state 3 is
state 2 when it is in an interrupt. Ideally we would just switch state 7 to state 0 if we are in an interrupt, but this code can be called outside the setting of the "in_interrupt()" counter, and we can not detect it. To work around this, state 3 is created to keep from going into states 5, 6 and 7 while in an interrupt.

(***) If we hit state 7, we know that there's a path that exists that
can lead us to miss a required schedule.

The state transactions are:

[preemption state changes] [interrupt state changes]

 State 0: (PEIE)           State 2                       State 1
 State 1: (PEID)           State 4                       State 0
 State 2: (PDIEX)          State 0                       State 5
 State 3: (PDIE)           State 0                       State 4
 State 4: (PDID)           State 1                       State 2
 State 5: (PDIDX)          State 6                       State 2
 State 6: (PEIDX)          State 5                       State 7

State 7: (PEIEX) [End] [End]

When PROVE_LOCKING and PREEMPT is configured, the preempt state tracking is active. Testing this out, I added a module that did the following:

  preempt_disable();
  local_irq_disable();
  preempt_enable();
  local_irq_enable();

I also tested against:

  local_irq_disable();
  preempt_disable();
  local_irq_enable();
  local_irq_disable();
  preempt_enable();
  local_irq_enable();

And here's what the output looks like:

===============================
[INFO: preempt check hit problem state]

 irq event stamp: 12
 hardirqs last  enabled at (11): [<ffffffff81666510>] _raw_spin_unlock_irq+0x30/0x60
 hardirqs last disabled at (12): [<ffffffffa050d01e>] dumb_thread+0x1e/0x80 [preempt_bug]
 softirqs last  enabled at (0): [<ffffffff8104f038>] copy_process+0x788/0x1a50
 softirqs last disabled at (0): [<          (null)>]           (null)
 Entered dangerous state at: 
    [<ffffffff8166a83b>] preempt_count_add+0xab/0x110
    [<ffffffffa050d018>] dumb_thread+0x18/0x80 [preempt_bug]
    [<ffffffff81076c03>] kthread+0xf3/0x110
    [<ffffffff8166e52c>] ret_from_fork+0x7c/0xb0
    [<ffffffffffffffff>] 0xffffffffffffffff
 stack backtrace:
 CPU: 4 PID: 3405 Comm: task1 Tainted: G           O 3.13.0-rc8-test+ #60
 Hardware name: Hewlett-Packard HP Compaq Pro 6300 SFF/339A, BIOS K01 v02.05 05/07/2012
  0000000000000006 ffff8800d47e5da8 ffffffff8165d459 0000000000000002
  ffffffffa050d028 ffff8800d47e5de8 ffffffff81099fc8 ffff8800d47e5df8
  ffffffffa050d028 0000000000000000 ffffffffa050d000 0000000000000000
 Call Trace:
  [<ffffffff8165d459>] dump_stack+0x4f/0x7c
  [<ffffffffa050d028>] ? dumb_thread+0x28/0x80 [preempt_bug]
  [<ffffffff81099fc8>] update_pied_state+0x398/0x3b0
  [<ffffffffa050d028>] ? dumb_thread+0x28/0x80 [preempt_bug]
  [<ffffffffa050d000>] ? 0xffffffffa050cfff
  [<ffffffff8109de40>] trace_preempt_on+0x20/0x30
  [<ffffffff8166a749>] preempt_count_sub+0xb9/0x100
  [<ffffffffa050d028>] dumb_thread+0x28/0x80 [preempt_bug]
  [<ffffffff81076c03>] kthread+0xf3/0x110
  [<ffffffff81076b10>] ? flush_kthread_worker+0x150/0x150
  [<ffffffff8166e52c>] ret_from_fork+0x7c/0xb0
  [<ffffffff81076b10>] ? flush_kthread_worker+0x150/0x150
 Last states (starting with most recent):
  1) State 5 (PDIDX)
      pc: 00000001  irqs: D
      .. [<ffffffffa050d01e>] .... dumb_thread+0x1e/0x80 [preempt_bug]
  2) State 2 (PDIEX)
      pc: 00000001  irqs: E
      .. [<ffffffffa050d018>] .... dumb_thread+0x18/0x80 [preempt_bug]
  3) State 0 (PEIE)
      pc: 00000001  irqs: E
      .. [<ffffffff81660d64>] .... __schedule+0x3b4/0x840
  4) State 2 (PDIEX)
      pc: 00000002  irqs: D
      .. [<ffffffff81666510>] .... _raw_spin_unlock_irq+0x30/0x60
  5) State 5 (PDIDX)
      pc: 00000001  irqs: D
      .. [<ffffffff81665dc7>] .... _raw_spin_lock_irq+0x17/0x50
  6) State 2 (PDIEX)
      pc: 00000001  irqs: D
      .. [<ffffffff810b8e19>] .... rcu_note_context_switch+0x99/0x300
  7) State 5 (PDIDX)
      pc: 00000001  irqs: D
      .. [<ffffffff810b8de7>] .... rcu_note_context_switch+0x67/0x300
  8) State 2 (PDIEX)
      pc: 00000001  irqs: E
      .. [<ffffffff816609f9>] .... __schedule+0x49/0x840
  9) State 0 (PEIE)
      pc: 00000001  irqs: E
      .. [<ffffffff81661633>] .... schedule_preempt_disabled+0x13/0x30
  10) State 2 (PDIEX)
      pc: 00000001  irqs: D
      .. [<ffffffff810c49e9>] .... tick_nohz_idle_exit+0x129/0x190
  11) State 5 (PDIDX)
      pc: 00000001  irqs: D
      .. [<ffffffff810c48f1>] .... tick_nohz_idle_exit+0x31/0x190
  12) State 2 (PDIEX)
      pc: 00000001  irqs: D
      .. [<ffffffff810b6019>] .... rcu_idle_exit+0x79/0xe0
  13) State 5 (PDIDX)
      pc: 00000001  irqs: D
      .. [<ffffffff810b5fc2>] .... rcu_idle_exit+0x22/0xe0
  14) State 2 (PDIEX)
      pc: 00000001  irqs: D
      .. [<ffffffff814f122f>] .... cpuidle_enter_state+0x5f/0xe0
  15) State 5 (PDIDX)
      pc: 00000001  irqs: D
      .. [<ffffffff810aca4f>] .... cpu_startup_entry+0xbf/0x320
  16) State 2 (PDIEX)
      pc: 00000001  irqs: D
      .. [<ffffffff810c47aa>] .... tick_nohz_idle_enter+0x4a/0x80
include/linux/ftrace.h +24 -4
Index: linux-trace.git/include/linux/ftrace.h
===================================================================
@@ -634,16 +634,36 @@ static inline void __ftrace_enabled_rest
 #endif
 
 #ifdef CONFIG_PREEMPT_TRACER
-  extern void trace_preempt_on(unsigned long a0, unsigned long a1);
-  extern void trace_preempt_off(unsigned long a0, unsigned long a1);
-#else
+ extern void time_preempt_on(unsigned long a0, unsigned long a1);
+ extern void time_preempt_off(unsigned long a0, unsigned long a1);
+# ifdef CONFIG_PROVE_LOCKING
+   extern void trace_preempt_on(unsigned long a0, unsigned long a1);
+   extern void trace_preempt_off(unsigned long a0, unsigned long a1);
+# else
+static inline void trace_preempt_on(unsigned long a0, unsigned long a1)
+{
+	time_preempt_on(a0, a1);
+}
+static inline void trace_preempt_off(unsigned long a0, unsigned long a1)
+{
+	time_preempt_off(a0, a1);
+}
+# endif /* CONFIG_PROVE_LOCKING */
+#else /* !CONFIG_PREEMPT_TRACER */
+static inline void time_preempt_on(unsigned long a0, unsigned long a1) { }
+ extern void time_preempt_off(unsigned long a0, unsigned long a1);
+# ifdef CONFIG_PROVE_LOCKING
+   extern void trace_preempt_on(unsigned long a0, unsigned long a1);
+   extern void trace_preempt_off(unsigned long a0, unsigned long a1);
+# else
 /*
  * Use defines instead of static inlines because some arches will make code out
  * of the CALLER_ADDR, when we really want these to be a real nop.
  */
 # define trace_preempt_on(a0, a1) do { } while (0)
 # define trace_preempt_off(a0, a1) do { } while (0)
-#endif
+# endif /* CONFIG_PROVE_LOCKING */
+#endif /* CONFIG_PREEMPT_TRACER */
 
 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
 extern void ftrace_init(void);
kernel/locking/lockdep.c +344 -0

365 lines not on this page · raw message

kernel/sched/core.c +4 -2
Index: linux-trace.git/kernel/sched/core.c
===================================================================
@@ -2414,7 +2414,8 @@ void __kprobes preempt_count_add(int val
 	DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
 				PREEMPT_MASK - 10);
 #endif
-	if (preempt_count() == val)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == val)
 		trace_preempt_off(ip, parent_ip);
 }
 EXPORT_SYMBOL(preempt_count_add);
@@ -2435,7 +2436,8 @@ void __kprobes preempt_count_sub(int val
 		return;
 #endif
 
-	if (preempt_count() == val)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == val)
 		trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
 	__preempt_count_sub(val);
 }
kernel/softirq.c +2 -1
Index: linux-trace.git/kernel/softirq.c
===================================================================
@@ -111,7 +111,8 @@ static void __local_bh_disable(unsigned
 		trace_softirqs_off(ip);
 	raw_local_irq_restore(flags);
 
-	if (preempt_count() == cnt)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == cnt)
 		trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
 }
 #else /* !CONFIG_TRACE_IRQFLAGS */
kernel/trace/trace_irqsoff.c +2 -2
Index: linux-trace.git/kernel/trace/trace_irqsoff.c
===================================================================
@@ -516,13 +516,13 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller)
 #endif /*  CONFIG_IRQSOFF_TRACER */
 
 #ifdef CONFIG_PREEMPT_TRACER
-void trace_preempt_on(unsigned long a0, unsigned long a1)
+void time_preempt_on(unsigned long a0, unsigned long a1)
 {
 	if (preempt_trace() && !irq_trace())
 		stop_critical_timing(a0, a1);
 }
 
-void trace_preempt_off(unsigned long a0, unsigned long a1)
+void time_preempt_off(unsigned long a0, unsigned long a1)
 {
 	if (preempt_trace() && !irq_trace())
 		start_critical_timing(a0, a1);
#1 lore raw
Quoted 3 lines · On Thu, 16 Jan 2014 23:57:51 -0500 Steven Rostedt <[email protected]> wrote:

When PROVE_LOCKING and PREEMPT is configured, the preempt state tracking is active. Testing this out, I added a module that did the following:

So I assume your kernel at least has no instances of this bug, so we don't need the patch ;) It *is* a fairly daft thing to do.

Maybe stick it in -next for a few months, see if anyone hits it?

maintainer
#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Thu, 16 Jan 2014 23:57:51 -0500 Steven Rostedt <[email protected]> wrote:

When PROVE_LOCKING and PREEMPT is configured, the preempt state tracking is active. Testing this out, I added a module that did the following:

So I assume your kernel at least has no instances of this bug, so we don't need the patch ;) It *is* a fairly daft thing to do.

Yeah, its exceedingly daft, but I do run into it every so often. Say once a year or so.

Also, its usually not really a problem on 'normal' kernels, but it absolutely blows on -rt.

author maintainer
#3 lore raw
In reply to Andrew Morton
Quoted 10 lines · On Thu, 16 Jan 2014 21:12:14 -0800 Andrew Morton <[email protected]>…
Quoted 3 lines · On Thu, 16 Jan 2014 23:57:51 -0500 Steven Rostedt <[email protected]> wrote:

When PROVE_LOCKING and PREEMPT is configured, the preempt state tracking is active. Testing this out, I added a module that did the following:

So I assume your kernel at least has no instances of this bug, so we don't need the patch ;) It *is* a fairly daft thing to do.

Maybe stick it in -next for a few months, see if anyone hits it?

Stephen,

Do you have any objections if I add this change to my for-next branch? I'll do it as a merge as I do not plan on having it go into the next release. But this is an extension to lockdep that when both PROVE_LOCKING and PREEMPT are enabled, it can catch a certain bug. But as Andrew has stated, it did not find any in the kernel that I'm running.

What I propose is to have this go into linux-next, as I assume that people test it with PROVE_LOCKING and PREEMPT enabled, and if someone adds this bug this patch will catch it (if the bug path is taken). Hopefully it would be reported and we know two things. One, someone added a bug, and two, this patch is useful to add to mainline.

Here's the catch 22, it may not be worth adding to mainline if it never catches any bugs. But we wont know that unless we add it to mainline. Maybe adding it to linux-next might be good enough for now.

-- Steve

#4 lore raw

Hi Steve,

Quoted 28 lines · On Sat, 18 Jan 2014 18:44:01 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 10 lines · On Thu, 16 Jan 2014 21:12:14 -0800 Andrew Morton <[email protected]>…
Quoted 3 lines · On Thu, 16 Jan 2014 23:57:51 -0500 Steven Rostedt <[email protected]> wrote:

When PROVE_LOCKING and PREEMPT is configured, the preempt state tracking is active. Testing this out, I added a module that did the following:

So I assume your kernel at least has no instances of this bug, so we don't need the patch ;) It *is* a fairly daft thing to do.

Maybe stick it in -next for a few months, see if anyone hits it?

Do you have any objections if I add this change to my for-next branch? I'll do it as a merge as I do not plan on having it go into the next release. But this is an extension to lockdep that when both PROVE_LOCKING and PREEMPT are enabled, it can catch a certain bug. But as Andrew has stated, it did not find any in the kernel that I'm running.

What I propose is to have this go into linux-next, as I assume that people test it with PROVE_LOCKING and PREEMPT enabled, and if someone adds this bug this patch will catch it (if the bug path is taken). Hopefully it would be reported and we know two things. One, someone added a bug, and two, this patch is useful to add to mainline.

Quoted 3 lines

Here's the catch 22, it may not be worth adding to mainline if it never catches any bugs. But we wont know that unless we add it to mainline. Maybe adding it to linux-next might be good enough for now.

Given that the merge window will probably open today or tomorrow, I would prefer any new code not intended for 3.14 not be added to linux-next until after v3.14-rc1 to avoid unneeded conflicts. If, however, Andrew thinks it is still worth the (maybe minimal) pain, then fine.

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]
author maintainer
#5 lore raw
Quoted 4 lines · On Sun, 19 Jan 2014 11:52:53 +1100 Stephen Rothwell <[email protected]>…

Given that the merge window will probably open today or tomorrow, I would prefer any new code not intended for 3.14 not be added to linux-next until after v3.14-rc1 to avoid unneeded conflicts. If, however, Andrew thinks it is still worth the (maybe minimal) pain, then fine.

I'm not sure this is even intended for 3.15 either ;-)

I'm fine with waiting, to keep from adding any extra pain just before a merge window. I guess the question is, is it OK to keep it in linux-next for 3.15 even though it may not even go into 3.15? Depends on how useful it proves to be. Perhaps it may require staying in linux-next till 3.16.

Perhaps in order to keep merge windows from being an issue, I can add it at each -rc1, and remove it at -rc6, if it didn't catch any bugs. But as soon as it does catch a bug, we can say it's worth going into mainline.

Does that sound fine with you?

-- Steve

#6 lore raw

Hi Steve,

Quoted 9 lines · On Sat, 18 Jan 2014 20:08:22 -0500 Steven Rostedt <[email protected]> wrote:
Quoted 4 lines · On Sun, 19 Jan 2014 11:52:53 +1100 Stephen Rothwell <[email protected]>…

Given that the merge window will probably open today or tomorrow, I would prefer any new code not intended for 3.14 not be added to linux-next until after v3.14-rc1 to avoid unneeded conflicts. If, however, Andrew thinks it is still worth the (maybe minimal) pain, then fine.

Quoted 7 lines

I'm not sure this is even intended for 3.15 either ;-)

I'm fine with waiting, to keep from adding any extra pain just before a merge window. I guess the question is, is it OK to keep it in linux-next for 3.15 even though it may not even go into 3.15? Depends on how useful it proves to be. Perhaps it may require staying in linux-next till 3.16.

If it is smallish and doesn't interact with much other stuff, then that is fine, I guess (I haven't looked at what you are discussing).

It just becomes a pain if it causes non trivial conflicts with real development (or worse runtime problems for testers).

Quoted 3 lines

Perhaps in order to keep merge windows from being an issue, I can add it at each -rc1, and remove it at -rc6, if it didn't catch any bugs. But as soon as it does catch a bug, we can say it's worth going into mainline.

As long as its interactions with other code are minor, it is better for it to stay in once added. Mainly because part of Andrew's patch queue sits on top of linux-next, so removing something from linux-next may cause interesting conflicts in that part.

Signature · 2 lines
Cheers,
Stephen Rothwell                    [email protected]
#7 lore raw
In reply to Steven Rostedt
Quoted 10 lines · Steven Rostedt

Peter Zijlstra mentioned that he wanted to catch the following problem:

local_irq_disable();
preempt_disable();
local_irq_enable();

local_irq_disable();
preempt_enable();
local_irq_enable();

[...]

kernel/locking/lockdep.c void lockdep_rcu_suspicious(const char * · line 4473

Quoted 13 lines
kernel/locking/lockdep.c +13 -0
+
+static void update_pied_state(enum pied_stat_type type, bool enable,
+			      unsigned long ip)
+{
+	struct stack_trace *trace;
+	int state, new_state;
+	unsigned long flags;
+
+	if (pied_failed)
+		return;
+
+	if (this_cpu_read(pied_recursive))
+		return;

Maybe I'm just missing something obvious, but it wasn't clear to me how or what the recursive check above would reliably protect against, when the increment is ~20 lines below and not a cmpxchg here.

Thanks,
Paul.
--

Quoted 193 lines
kernel/locking/lockdep.c +128 -0
+
+	/*
+	 * Boot up may start with interrupts and/or preemption
+	 * disabled. We can't start the state updates till
+	 * we have synced with the initial state.
+	 */
+	if (!this_cpu_read(pied_initialized)) {
+		/*
+		 * The first time we enable preemption with interrupts
+		 * enabled on a CPU, start the state transactions.
+		 */
+		if (!in_interrupt() && type == PIED_STATE_PREEMPT &&
+		    enable && !irqs_disabled())
+			this_cpu_write(pied_initialized, 1);
+		return;
+	}
+
+	if (type == PIED_STATE_INTERRUPT) {
+		if (enable == false) {
+			/* Ignore nested disabling of interrupts */
+			if (this_cpu_read(pied_irqsoff))
+				return;
+			this_cpu_write(pied_irqsoff, 1);
+		} else
+			this_cpu_write(pied_irqsoff, 0);
+	}
+
+	this_cpu_inc(pied_recursive);
+	raw_local_irq_save(flags);
+
+	state = this_cpu_read(current_pied_state);
+
+	switch (type) {
+	case PIED_STATE_PREEMPT:
+		new_state = pied_state_trans[state].preempt_change;
+		switch (new_state) {
+		case 0: case 1: case 6: case 7:
+			if (!enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		default:
+			if (enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		}
+		break;
+	case PIED_STATE_INTERRUPT:
+		new_state = pied_state_trans[state].interrupt_change;
+		switch (new_state) {
+		case 0: case 2: case 3: case 7:
+			if (!enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		default:
+			if (enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		}
+		break;
+	}
+
+	switch (new_state) {
+	case PIED_DANGEROUS_STATE:
+		/*
+		 * If we are in an interrupt, then we need to switch
+		 * to state 3 to prevent from going into state 5, 6 and 7.
+		 *
+		 * PDIEX ==> PDIE
+		 */
+		if (in_interrupt()) {
+			new_state = 3;
+			break;
+		}
+		trace = this_cpu_ptr(&pied_stack_trace);
+		trace->nr_entries = 0;
+		trace->max_entries = PIED_STACK_MAX;
+		trace->entries = this_cpu_ptr(pied_stack);
+
+		trace->skip = 3;
+
+		save_stack_trace(trace);
+
+		break;
+	case PIED_BAD_STATE:
+
+		/*
+		 * Interrupts themselves do not cause problems as they
+		 * always check NEED_RESCHED when going back to normal context.
+		 *
+		 * PEIEX ==> PEIE
+		 */
+		if (in_interrupt()) {
+			new_state = 0;
+			break;
+		}
+
+		lockdep_off();
+		pied_failed = true;
+		printk("\n");
+		printk("===============================\n");
+		printk("[INFO: preempt check hit problem state]\n");
+		print_irqtrace_events(current);
+		printk("Entered dangerous state at: \n");
+		print_stack_trace(this_cpu_ptr(&pied_stack_trace), 2); 
+		printk("\nstack backtrace:\n");
+		dump_stack();
+		print_pied_trail();
+		lockdep_on();
+		break;
+	}
+	this_cpu_write(current_pied_state, new_state);
+	update_pied_trail(new_state, ip, irqs_disabled_flags(flags));
+	raw_local_irq_restore(flags);
+	this_cpu_dec(pied_recursive);
+}
+
+void trace_preempt_on(unsigned long a0, unsigned long a1)
+{
+	time_preempt_on(a0, a1);
+	update_pied_state(PIED_STATE_PREEMPT, true, a0);
+}
+
+void trace_preempt_off(unsigned long a0, unsigned long a1)
+{
+	time_preempt_off(a0, a1);
+	update_pied_state(PIED_STATE_PREEMPT, false, a0);
+}
+#endif /* CONFIG_PREEMPT */

Index: linux-trace.git/kernel/sched/core.c
===================================================================

kernel/sched/core.c +4 -2
@@ -2414,7 +2414,8 @@ void __kprobes preempt_count_add(int val
 	DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
 				PREEMPT_MASK - 10);
 #endif
-	if (preempt_count() == val)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == val)
 		trace_preempt_off(ip, parent_ip);
 }
 EXPORT_SYMBOL(preempt_count_add);
@@ -2435,7 +2436,8 @@ void __kprobes preempt_count_sub(int val
 		return;
 #endif
 
-	if (preempt_count() == val)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == val)
 		trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
 	__preempt_count_sub(val);
 }

Index: linux-trace.git/kernel/softirq.c
===================================================================

kernel/softirq.c +2 -1
@@ -111,7 +111,8 @@ static void __local_bh_disable(unsigned
 		trace_softirqs_off(ip);
 	raw_local_irq_restore(flags);
 
-	if (preempt_count() == cnt)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == cnt)
 		trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
 }
 #else /* !CONFIG_TRACE_IRQFLAGS */

Index: linux-trace.git/kernel/trace/trace_irqsoff.c
===================================================================

kernel/trace/trace_irqsoff.c +2 -3
@@ -516,13 +516,13 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller)
 #endif /*  CONFIG_IRQSOFF_TRACER */
 
 #ifdef CONFIG_PREEMPT_TRACER
-void trace_preempt_on(unsigned long a0, unsigned long a1)
+void time_preempt_on(unsigned long a0, unsigned long a1)
 {
 	if (preempt_trace() && !irq_trace())
 		stop_critical_timing(a0, a1);
 }
 
-void trace_preempt_off(unsigned long a0, unsigned long a1)
+void time_preempt_off(unsigned long a0, unsigned long a1)
 {
 	if (preempt_trace() && !irq_trace())
 		start_critical_timing(a0, a1);
 
--

To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author maintainer
#8 lore raw
Quoted 9 lines · On Wed, 22 Jan 2014 14:47:42 -0500 Paul Gortmaker…
Quoted 9 lines
kernel/locking/lockdep.c +9 -0
+
+static void update_pied_state(enum pied_stat_type type, bool enable,
+			      unsigned long ip)
+{
+	struct stack_trace *trace;
+	int state, new_state;
+	unsigned long flags;
+
+	if (pied_failed)
Quoted 8 lines

kernel/locking/lockdep.c void lockdep_rcu_suspicious(const char * · line 4473

Quoted 4 lines
kernel/locking/lockdep.c +4 -0
+		return;
+
+	if (this_cpu_read(pied_recursive))
+		return;

Maybe I'm just missing something obvious, but it wasn't clear to me how or what the recursive check above would reliably protect against, when the increment is ~20 lines below and not a cmpxchg here.

It protects against interrupts.

You don't need cmpxchg to protect against interrupts. Think about it. Even a simple "read/modify/write" works too. The key is, interrupts are nested:

read x = 1
x = x + 1 (modified on stack)

interrupt comes in.

read x = 1 (everything is ok to continue our state)

x = x + 1

write x (x now equals 2)

update the state

read x = 2

x = x - 1

write x (x is back to 1)

interrupt ends

write x (x is now 2, just like it would be without the
interrupt)

read state (the state is what we expect it to be)

The only thing you need is a barrier to keep gcc from messing things up, but the local_irq_save() does that for us.

Note, the bad state can't happen in an interrupt, but we keep track of states in the interrupt because it makes it easier to compute. Too many corner cases to ignore interrupts (I tried). But the interrupts that happens inside this code are not those corner cases and we can ignore (with the recursive flag).

-- Steve

Quoted 198 lines

Thanks,
Paul.
--

Quoted 193 lines
kernel/locking/lockdep.c +128 -0
+
+	/*
+	 * Boot up may start with interrupts and/or preemption
+	 * disabled. We can't start the state updates till
+	 * we have synced with the initial state.
+	 */
+	if (!this_cpu_read(pied_initialized)) {
+		/*
+		 * The first time we enable preemption with interrupts
+		 * enabled on a CPU, start the state transactions.
+		 */
+		if (!in_interrupt() && type == PIED_STATE_PREEMPT &&
+		    enable && !irqs_disabled())
+			this_cpu_write(pied_initialized, 1);
+		return;
+	}
+
+	if (type == PIED_STATE_INTERRUPT) {
+		if (enable == false) {
+			/* Ignore nested disabling of interrupts */
+			if (this_cpu_read(pied_irqsoff))
+				return;
+			this_cpu_write(pied_irqsoff, 1);
+		} else
+			this_cpu_write(pied_irqsoff, 0);
+	}
+
+	this_cpu_inc(pied_recursive);
+	raw_local_irq_save(flags);
+
+	state = this_cpu_read(current_pied_state);
+
+	switch (type) {
+	case PIED_STATE_PREEMPT:
+		new_state = pied_state_trans[state].preempt_change;
+		switch (new_state) {
+		case 0: case 1: case 6: case 7:
+			if (!enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		default:
+			if (enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		}
+		break;
+	case PIED_STATE_INTERRUPT:
+		new_state = pied_state_trans[state].interrupt_change;
+		switch (new_state) {
+		case 0: case 2: case 3: case 7:
+			if (!enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		default:
+			if (enable)
+				pied_state_bug(type, enable, state, new_state);
+			break;
+		}
+		break;
+	}
+
+	switch (new_state) {
+	case PIED_DANGEROUS_STATE:
+		/*
+		 * If we are in an interrupt, then we need to switch
+		 * to state 3 to prevent from going into state 5, 6 and 7.
+		 *
+		 * PDIEX ==> PDIE
+		 */
+		if (in_interrupt()) {
+			new_state = 3;
+			break;
+		}
+		trace = this_cpu_ptr(&pied_stack_trace);
+		trace->nr_entries = 0;
+		trace->max_entries = PIED_STACK_MAX;
+		trace->entries = this_cpu_ptr(pied_stack);
+
+		trace->skip = 3;
+
+		save_stack_trace(trace);
+
+		break;
+	case PIED_BAD_STATE:
+
+		/*
+		 * Interrupts themselves do not cause problems as they
+		 * always check NEED_RESCHED when going back to normal context.
+		 *
+		 * PEIEX ==> PEIE
+		 */
+		if (in_interrupt()) {
+			new_state = 0;
+			break;
+		}
+
+		lockdep_off();
+		pied_failed = true;
+		printk("\n");
+		printk("===============================\n");
+		printk("[INFO: preempt check hit problem state]\n");
+		print_irqtrace_events(current);
+		printk("Entered dangerous state at: \n");
+		print_stack_trace(this_cpu_ptr(&pied_stack_trace), 2); 
+		printk("\nstack backtrace:\n");
+		dump_stack();
+		print_pied_trail();
+		lockdep_on();
+		break;
+	}
+	this_cpu_write(current_pied_state, new_state);
+	update_pied_trail(new_state, ip, irqs_disabled_flags(flags));
+	raw_local_irq_restore(flags);
+	this_cpu_dec(pied_recursive);
+}
+
+void trace_preempt_on(unsigned long a0, unsigned long a1)
+{
+	time_preempt_on(a0, a1);
+	update_pied_state(PIED_STATE_PREEMPT, true, a0);
+}
+
+void trace_preempt_off(unsigned long a0, unsigned long a1)
+{
+	time_preempt_off(a0, a1);
+	update_pied_state(PIED_STATE_PREEMPT, false, a0);
+}
+#endif /* CONFIG_PREEMPT */

Index: linux-trace.git/kernel/sched/core.c
===================================================================

kernel/sched/core.c +4 -2
@@ -2414,7 +2414,8 @@ void __kprobes preempt_count_add(int val
 	DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
 				PREEMPT_MASK - 10);
 #endif
-	if (preempt_count() == val)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == val)
 		trace_preempt_off(ip, parent_ip);
 }
 EXPORT_SYMBOL(preempt_count_add);
@@ -2435,7 +2436,8 @@ void __kprobes preempt_count_sub(int val
 		return;
 #endif
 
-	if (preempt_count() == val)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == val)
 		trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
 	__preempt_count_sub(val);
 }

Index: linux-trace.git/kernel/softirq.c
===================================================================

kernel/softirq.c +2 -1
@@ -111,7 +111,8 @@ static void __local_bh_disable(unsigned
 		trace_softirqs_off(ip);
 	raw_local_irq_restore(flags);
 
-	if (preempt_count() == cnt)
+	/* PREEMPT_ACTIVE gets set directly, it must be ignored */
+	if ((preempt_count() & ~PREEMPT_ACTIVE) == cnt)
 		trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
 }
 #else /* !CONFIG_TRACE_IRQFLAGS */

Index: linux-trace.git/kernel/trace/trace_irqsoff.c
===================================================================

kernel/trace/trace_irqsoff.c +2 -3
@@ -516,13 +516,13 @@ EXPORT_SYMBOL(trace_hardirqs_off_caller)
 #endif /*  CONFIG_IRQSOFF_TRACER */
 
 #ifdef CONFIG_PREEMPT_TRACER
-void trace_preempt_on(unsigned long a0, unsigned long a1)
+void time_preempt_on(unsigned long a0, unsigned long a1)
 {
 	if (preempt_trace() && !irq_trace())
 		stop_critical_timing(a0, a1);
 }
 
-void trace_preempt_off(unsigned long a0, unsigned long a1)
+void time_preempt_off(unsigned long a0, unsigned long a1)
 {
 	if (preempt_trace() && !irq_trace())
 		start_critical_timing(a0, a1);
 
--

To unsubscribe from this list: send the line "unsubscribe linux-rt-users" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html