aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/irq_work.c
diff options
context:
space:
mode:
authorFrederic Weisbecker <fweisbec@gmail.com>2012-10-27 09:21:36 -0400
committerFrederic Weisbecker <fweisbec@gmail.com>2012-11-14 11:36:32 -0500
commite0bbe2d80c415bd4063d894ec2ccb336788af814 (patch)
treee8109ef0e91df2d61aa118f7ba30d0522d65668f /kernel/irq_work.c
parentc8446b75be6f85b3d40066922876cb7adc948afb (diff)
irq_work: Fix racy check on work pending flag
Work claiming wants to be SMP-safe. And by the time we try to claim a work, if it is already executing concurrently on another CPU, we want to succeed the claiming and queue the work again because the other CPU may have missed the data we wanted to handle in our work if it's about to complete there. This scenario is summarized below: CPU 1 CPU 2 ----- ----- (flags = 0) cmpxchg(flags, 0, IRQ_WORK_FLAGS) (flags = 3) [...] xchg(flags, IRQ_WORK_BUSY) (flags = 2) func() if (flags & IRQ_WORK_PENDING) (not true) cmpxchg(flags, flags, IRQ_WORK_FLAGS) (flags = 3) [...] cmpxchg(flags, IRQ_WORK_BUSY, 0); (fail, pending on CPU 2) This state machine is synchronized using [cmp]xchg() on the flags. As such, the early IRQ_WORK_PENDING check in CPU 2 above is racy. By the time we check it, we may be dealing with a stale value because we aren't using an atomic accessor. As a result, CPU 2 may "see" that the work is still pending on another CPU while it may be actually completing the work function exection already, leaving our data unprocessed. To fix this, we start by speculating about the value we wish to be in the work->flags but we only make any conclusion after the value returned by the cmpxchg() call that either claims the work or let the current owner handle the pending work for us. Changelog-heavily-inspired-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Acked-by: Steven Rostedt <rostedt@goodmis.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Ingo Molnar <mingo@kernel.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Paul Gortmaker <paul.gortmaker@windriver.com> Cc: Anish Kumar <anish198519851985@gmail.com>
Diffstat (limited to 'kernel/irq_work.c')
-rw-r--r--kernel/irq_work.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/kernel/irq_work.c b/kernel/irq_work.c
index 57be1a6cd8da..64eddd59ed83 100644
--- a/kernel/irq_work.c
+++ b/kernel/irq_work.c
@@ -34,15 +34,21 @@ static DEFINE_PER_CPU(struct llist_head, irq_work_list);
34 */ 34 */
35static bool irq_work_claim(struct irq_work *work) 35static bool irq_work_claim(struct irq_work *work)
36{ 36{
37 unsigned long flags, nflags; 37 unsigned long flags, oflags, nflags;
38 38
39 /*
40 * Start with our best wish as a premise but only trust any
41 * flag value after cmpxchg() result.
42 */
43 flags = work->flags & ~IRQ_WORK_PENDING;
39 for (;;) { 44 for (;;) {
40 flags = work->flags;
41 if (flags & IRQ_WORK_PENDING)
42 return false;
43 nflags = flags | IRQ_WORK_FLAGS; 45 nflags = flags | IRQ_WORK_FLAGS;
44 if (cmpxchg(&work->flags, flags, nflags) == flags) 46 oflags = cmpxchg(&work->flags, flags, nflags);
47 if (oflags == flags)
45 break; 48 break;
49 if (oflags & IRQ_WORK_PENDING)
50 return false;
51 flags = oflags;
46 cpu_relax(); 52 cpu_relax();
47 } 53 }
48 54