2 * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
4 * Provides a framework for enqueueing and running callbacks from hardirq
5 * context. The enqueueing is NMI-safe.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/irq_work.h>
11 #include <linux/hardirq.h>
14 * An entry can be in one of four states:
16 * free NULL, 0 -> {claimed} : free to be used
17 * claimed NULL, 3 -> {pending} : claimed to be enqueued
18 * pending next, 3 -> {busy} : queued, pending callback
19 * busy NULL, 2 -> {free, claimed} : callback in progress, can be claimed
22 #define IRQ_WORK_PENDING 1UL
23 #define IRQ_WORK_BUSY 2UL
24 #define IRQ_WORK_FLAGS 3UL
26 static DEFINE_PER_CPU(struct llist_head, irq_work_list);
29 * Claim the entry so that no one else will poke at it.
31 static bool irq_work_claim(struct irq_work *work)
33 unsigned long flags, nflags;
37 if (flags & IRQ_WORK_PENDING)
39 nflags = flags | IRQ_WORK_FLAGS;
40 if (cmpxchg(&work->flags, flags, nflags) == flags)
48 void __weak arch_irq_work_raise(void)
51 * Lame architectures will get the timer tick callback
56 * Queue the entry and raise the IPI if needed.
58 static void __irq_work_queue(struct irq_work *work)
64 empty = llist_add(&work->llnode, &__get_cpu_var(irq_work_list));
65 /* The list was empty, raise self-interrupt to start processing. */
67 arch_irq_work_raise();
73 * Enqueue the irq_work @entry, returns true on success, failure when the
74 * @entry was already enqueued by someone else.
76 * Can be re-enqueued while the callback is still in progress.
78 bool irq_work_queue(struct irq_work *work)
80 if (!irq_work_claim(work)) {
82 * Already enqueued, can't do!
87 __irq_work_queue(work);
90 EXPORT_SYMBOL_GPL(irq_work_queue);
93 * Run the irq_work entries on this cpu. Requires to be ran from hardirq
94 * context with local IRQs disabled.
96 void irq_work_run(void)
98 struct irq_work *work;
99 struct llist_head *this_list;
100 struct llist_node *llnode;
102 this_list = &__get_cpu_var(irq_work_list);
103 if (llist_empty(this_list))
107 BUG_ON(!irqs_disabled());
109 llnode = llist_del_all(this_list);
110 while (llnode != NULL) {
111 work = llist_entry(llnode, struct irq_work, llnode);
113 llnode = llist_next(llnode);
116 * Clear the PENDING bit, after this point the @work
119 work->flags = IRQ_WORK_BUSY;
122 * Clear the BUSY bit and return to the free state if
123 * no-one else claimed it meanwhile.
125 (void)cmpxchg(&work->flags, IRQ_WORK_BUSY, 0);
128 EXPORT_SYMBOL_GPL(irq_work_run);
131 * Synchronize against the irq_work @entry, ensures the entry is not
134 void irq_work_sync(struct irq_work *work)
136 WARN_ON_ONCE(irqs_disabled());
138 while (work->flags & IRQ_WORK_BUSY)
141 EXPORT_SYMBOL_GPL(irq_work_sync);