2010-10-14 06:01:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
|
|
|
|
*
|
|
|
|
* Provides a framework for enqueueing and running callbacks from hardirq
|
|
|
|
* context. The enqueueing is NMI-safe.
|
|
|
|
*/
|
|
|
|
|
2012-04-01 20:38:37 +00:00
|
|
|
#include <linux/bug.h>
|
2010-10-14 06:01:34 +00:00
|
|
|
#include <linux/kernel.h>
|
2011-05-23 18:51:41 +00:00
|
|
|
#include <linux/export.h>
|
2010-10-14 06:01:34 +00:00
|
|
|
#include <linux/irq_work.h>
|
2011-07-18 17:03:04 +00:00
|
|
|
#include <linux/percpu.h>
|
2010-10-14 06:01:34 +00:00
|
|
|
#include <linux/hardirq.h>
|
2012-04-11 16:21:39 +00:00
|
|
|
#include <linux/irqflags.h>
|
2012-11-15 16:34:21 +00:00
|
|
|
#include <linux/cpu.h>
|
|
|
|
#include <linux/notifier.h>
|
2011-07-18 17:03:04 +00:00
|
|
|
#include <asm/processor.h>
|
2010-10-14 06:01:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* An entry can be in one of four states:
|
|
|
|
*
|
|
|
|
* free NULL, 0 -> {claimed} : free to be used
|
|
|
|
* claimed NULL, 3 -> {pending} : claimed to be enqueued
|
|
|
|
* pending next, 3 -> {busy} : queued, pending callback
|
|
|
|
* busy NULL, 2 -> {free, claimed} : callback in progress, can be claimed
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define IRQ_WORK_PENDING 1UL
|
|
|
|
#define IRQ_WORK_BUSY 2UL
|
|
|
|
#define IRQ_WORK_FLAGS 3UL
|
|
|
|
|
2011-09-08 06:00:46 +00:00
|
|
|
static DEFINE_PER_CPU(struct llist_head, irq_work_list);
|
2010-10-14 06:01:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Claim the entry so that no one else will poke at it.
|
|
|
|
*/
|
2011-09-08 06:00:46 +00:00
|
|
|
static bool irq_work_claim(struct irq_work *work)
|
2010-10-14 06:01:34 +00:00
|
|
|
{
|
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>
2012-10-27 13:21:36 +00:00
|
|
|
unsigned long flags, oflags, nflags;
|
2010-10-14 06:01:34 +00:00
|
|
|
|
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>
2012-10-27 13:21:36 +00:00
|
|
|
/*
|
|
|
|
* Start with our best wish as a premise but only trust any
|
|
|
|
* flag value after cmpxchg() result.
|
|
|
|
*/
|
|
|
|
flags = work->flags & ~IRQ_WORK_PENDING;
|
2011-09-08 06:00:46 +00:00
|
|
|
for (;;) {
|
|
|
|
nflags = flags | IRQ_WORK_FLAGS;
|
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>
2012-10-27 13:21:36 +00:00
|
|
|
oflags = cmpxchg(&work->flags, flags, nflags);
|
|
|
|
if (oflags == flags)
|
2011-09-08 06:00:46 +00:00
|
|
|
break;
|
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>
2012-10-27 13:21:36 +00:00
|
|
|
if (oflags & IRQ_WORK_PENDING)
|
|
|
|
return false;
|
|
|
|
flags = oflags;
|
2011-09-08 06:00:46 +00:00
|
|
|
cpu_relax();
|
|
|
|
}
|
2010-10-14 06:01:34 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __weak arch_irq_work_raise(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Lame architectures will get the timer tick callback
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Queue the entry and raise the IPI if needed.
|
|
|
|
*/
|
2011-09-08 06:00:46 +00:00
|
|
|
static void __irq_work_queue(struct irq_work *work)
|
2010-10-14 06:01:34 +00:00
|
|
|
{
|
2011-09-08 06:00:46 +00:00
|
|
|
bool empty;
|
2010-10-14 06:01:34 +00:00
|
|
|
|
2010-12-14 16:28:45 +00:00
|
|
|
preempt_disable();
|
2010-10-14 06:01:34 +00:00
|
|
|
|
2011-09-08 06:00:46 +00:00
|
|
|
empty = llist_add(&work->llnode, &__get_cpu_var(irq_work_list));
|
2010-10-14 06:01:34 +00:00
|
|
|
/* The list was empty, raise self-interrupt to start processing. */
|
2011-09-08 06:00:46 +00:00
|
|
|
if (empty)
|
2010-10-14 06:01:34 +00:00
|
|
|
arch_irq_work_raise();
|
|
|
|
|
2010-12-14 16:28:45 +00:00
|
|
|
preempt_enable();
|
2010-10-14 06:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enqueue the irq_work @entry, returns true on success, failure when the
|
|
|
|
* @entry was already enqueued by someone else.
|
|
|
|
*
|
|
|
|
* Can be re-enqueued while the callback is still in progress.
|
|
|
|
*/
|
2011-09-08 06:00:46 +00:00
|
|
|
bool irq_work_queue(struct irq_work *work)
|
2010-10-14 06:01:34 +00:00
|
|
|
{
|
2011-09-08 06:00:46 +00:00
|
|
|
if (!irq_work_claim(work)) {
|
2010-10-14 06:01:34 +00:00
|
|
|
/*
|
|
|
|
* Already enqueued, can't do!
|
|
|
|
*/
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-08 06:00:46 +00:00
|
|
|
__irq_work_queue(work);
|
2010-10-14 06:01:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(irq_work_queue);
|
|
|
|
|
2012-11-07 20:03:07 +00:00
|
|
|
bool irq_work_needs_cpu(void)
|
|
|
|
{
|
|
|
|
struct llist_head *this_list;
|
|
|
|
|
|
|
|
this_list = &__get_cpu_var(irq_work_list);
|
|
|
|
if (llist_empty(this_list))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-15 16:34:21 +00:00
|
|
|
static void __irq_work_run(void)
|
2010-10-14 06:01:34 +00:00
|
|
|
{
|
2011-09-08 06:00:46 +00:00
|
|
|
struct irq_work *work;
|
|
|
|
struct llist_head *this_list;
|
|
|
|
struct llist_node *llnode;
|
2010-10-14 06:01:34 +00:00
|
|
|
|
2011-09-08 06:00:46 +00:00
|
|
|
this_list = &__get_cpu_var(irq_work_list);
|
|
|
|
if (llist_empty(this_list))
|
2010-10-14 06:01:34 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
BUG_ON(!irqs_disabled());
|
|
|
|
|
2011-09-08 06:00:46 +00:00
|
|
|
llnode = llist_del_all(this_list);
|
|
|
|
while (llnode != NULL) {
|
|
|
|
work = llist_entry(llnode, struct irq_work, llnode);
|
2010-10-14 06:01:34 +00:00
|
|
|
|
2011-09-12 11:12:28 +00:00
|
|
|
llnode = llist_next(llnode);
|
2010-10-14 06:01:34 +00:00
|
|
|
|
|
|
|
/*
|
2011-09-08 06:00:46 +00:00
|
|
|
* Clear the PENDING bit, after this point the @work
|
2010-10-14 06:01:34 +00:00
|
|
|
* can be re-used.
|
irq_work: Fix racy IRQ_WORK_BUSY flag setting
The IRQ_WORK_BUSY flag is set right before we execute the
work. Once this flag value is set, the work enters a
claimable state again.
So if we have specific data to compute in our work, we ensure it's
either handled by another CPU or locally by enqueuing the work again.
This state machine is guanranteed by atomic operations on the flags.
So when we set IRQ_WORK_BUSY without using an xchg-like operation,
we break this guarantee as in the following summarized scenario:
CPU 1 CPU 2
----- -----
(flags = 0)
old_flags = flags;
(flags = 0)
cmpxchg(flags, old_flags,
old_flags | IRQ_WORK_FLAGS)
(flags = 3)
[...]
flags = IRQ_WORK_BUSY
(flags = 2)
func()
(sees flags = 3)
cmpxchg(flags, old_flags,
old_flags | IRQ_WORK_FLAGS)
(give up)
cmpxchg(flags, 2, 0);
(flags = 0)
CPU 1 claims a work and executes it, so it sets IRQ_WORK_BUSY and
the work is again in a claimable state. Now CPU 2 has new data to process
and try to claim that work but it may see a stale value of the flags
and think the work is still pending somewhere that will handle our data.
This is because CPU 1 doesn't set IRQ_WORK_BUSY atomically.
As a result, the data expected to be handle by CPU 2 won't get handled.
To fix this, use xchg() to set IRQ_WORK_BUSY, this way we ensure the CPU 2
will see the correct value with cmpxchg() using the expected ordering.
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>
2012-10-30 12:33:54 +00:00
|
|
|
* Make it immediately visible so that other CPUs trying
|
|
|
|
* to claim that work don't rely on us to handle their data
|
|
|
|
* while we are in the middle of the func.
|
2010-10-14 06:01:34 +00:00
|
|
|
*/
|
irq_work: Fix racy IRQ_WORK_BUSY flag setting
The IRQ_WORK_BUSY flag is set right before we execute the
work. Once this flag value is set, the work enters a
claimable state again.
So if we have specific data to compute in our work, we ensure it's
either handled by another CPU or locally by enqueuing the work again.
This state machine is guanranteed by atomic operations on the flags.
So when we set IRQ_WORK_BUSY without using an xchg-like operation,
we break this guarantee as in the following summarized scenario:
CPU 1 CPU 2
----- -----
(flags = 0)
old_flags = flags;
(flags = 0)
cmpxchg(flags, old_flags,
old_flags | IRQ_WORK_FLAGS)
(flags = 3)
[...]
flags = IRQ_WORK_BUSY
(flags = 2)
func()
(sees flags = 3)
cmpxchg(flags, old_flags,
old_flags | IRQ_WORK_FLAGS)
(give up)
cmpxchg(flags, 2, 0);
(flags = 0)
CPU 1 claims a work and executes it, so it sets IRQ_WORK_BUSY and
the work is again in a claimable state. Now CPU 2 has new data to process
and try to claim that work but it may see a stale value of the flags
and think the work is still pending somewhere that will handle our data.
This is because CPU 1 doesn't set IRQ_WORK_BUSY atomically.
As a result, the data expected to be handle by CPU 2 won't get handled.
To fix this, use xchg() to set IRQ_WORK_BUSY, this way we ensure the CPU 2
will see the correct value with cmpxchg() using the expected ordering.
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>
2012-10-30 12:33:54 +00:00
|
|
|
xchg(&work->flags, IRQ_WORK_BUSY);
|
2011-09-08 06:00:46 +00:00
|
|
|
work->func(work);
|
2010-10-14 06:01:34 +00:00
|
|
|
/*
|
|
|
|
* Clear the BUSY bit and return to the free state if
|
|
|
|
* no-one else claimed it meanwhile.
|
|
|
|
*/
|
2011-09-08 06:00:46 +00:00
|
|
|
(void)cmpxchg(&work->flags, IRQ_WORK_BUSY, 0);
|
2010-10-14 06:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-15 16:34:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Run the irq_work entries on this cpu. Requires to be ran from hardirq
|
|
|
|
* context with local IRQs disabled.
|
|
|
|
*/
|
|
|
|
void irq_work_run(void)
|
|
|
|
{
|
|
|
|
BUG_ON(!in_irq());
|
|
|
|
__irq_work_run();
|
|
|
|
}
|
2010-10-14 06:01:34 +00:00
|
|
|
EXPORT_SYMBOL_GPL(irq_work_run);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Synchronize against the irq_work @entry, ensures the entry is not
|
|
|
|
* currently in use.
|
|
|
|
*/
|
2011-09-08 06:00:46 +00:00
|
|
|
void irq_work_sync(struct irq_work *work)
|
2010-10-14 06:01:34 +00:00
|
|
|
{
|
|
|
|
WARN_ON_ONCE(irqs_disabled());
|
|
|
|
|
2011-09-08 06:00:46 +00:00
|
|
|
while (work->flags & IRQ_WORK_BUSY)
|
2010-10-14 06:01:34 +00:00
|
|
|
cpu_relax();
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(irq_work_sync);
|
2012-11-15 16:34:21 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_HOTPLUG_CPU
|
|
|
|
static int irq_work_cpu_notify(struct notifier_block *self,
|
|
|
|
unsigned long action, void *hcpu)
|
|
|
|
{
|
|
|
|
long cpu = (long)hcpu;
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case CPU_DYING:
|
|
|
|
/* Called from stop_machine */
|
|
|
|
if (WARN_ON_ONCE(cpu != smp_processor_id()))
|
|
|
|
break;
|
|
|
|
__irq_work_run();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return NOTIFY_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block cpu_notify;
|
|
|
|
|
|
|
|
static __init int irq_work_init_cpu_notifier(void)
|
|
|
|
{
|
|
|
|
cpu_notify.notifier_call = irq_work_cpu_notify;
|
|
|
|
cpu_notify.priority = 0;
|
|
|
|
register_cpu_notifier(&cpu_notify);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
device_initcall(irq_work_init_cpu_notifier);
|
|
|
|
|
|
|
|
#endif /* CONFIG_HOTPLUG_CPU */
|