Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com> |
| 3 | * |
| 4 | * Provides a framework for enqueueing and running callbacks from hardirq |
| 5 | * context. The enqueueing is NMI-safe. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame^] | 9 | #include <linux/export.h> |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 10 | #include <linux/irq_work.h> |
| 11 | #include <linux/hardirq.h> |
| 12 | |
| 13 | /* |
| 14 | * An entry can be in one of four states: |
| 15 | * |
| 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 |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #define IRQ_WORK_PENDING 1UL |
| 23 | #define IRQ_WORK_BUSY 2UL |
| 24 | #define IRQ_WORK_FLAGS 3UL |
| 25 | |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 26 | static DEFINE_PER_CPU(struct llist_head, irq_work_list); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 27 | |
| 28 | /* |
| 29 | * Claim the entry so that no one else will poke at it. |
| 30 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 31 | static bool irq_work_claim(struct irq_work *work) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 32 | { |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 33 | unsigned long flags, nflags; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 34 | |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 35 | for (;;) { |
| 36 | flags = work->flags; |
| 37 | if (flags & IRQ_WORK_PENDING) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 38 | return false; |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 39 | nflags = flags | IRQ_WORK_FLAGS; |
| 40 | if (cmpxchg(&work->flags, flags, nflags) == flags) |
| 41 | break; |
| 42 | cpu_relax(); |
| 43 | } |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 44 | |
| 45 | return true; |
| 46 | } |
| 47 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 48 | void __weak arch_irq_work_raise(void) |
| 49 | { |
| 50 | /* |
| 51 | * Lame architectures will get the timer tick callback |
| 52 | */ |
| 53 | } |
| 54 | |
| 55 | /* |
| 56 | * Queue the entry and raise the IPI if needed. |
| 57 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 58 | static void __irq_work_queue(struct irq_work *work) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 59 | { |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 60 | bool empty; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 61 | |
Christoph Lameter | 20b8769 | 2010-12-14 10:28:45 -0600 | [diff] [blame] | 62 | preempt_disable(); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 63 | |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 64 | empty = llist_add(&work->llnode, &__get_cpu_var(irq_work_list)); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 65 | /* The list was empty, raise self-interrupt to start processing. */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 66 | if (empty) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 67 | arch_irq_work_raise(); |
| 68 | |
Christoph Lameter | 20b8769 | 2010-12-14 10:28:45 -0600 | [diff] [blame] | 69 | preempt_enable(); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Enqueue the irq_work @entry, returns true on success, failure when the |
| 74 | * @entry was already enqueued by someone else. |
| 75 | * |
| 76 | * Can be re-enqueued while the callback is still in progress. |
| 77 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 78 | bool irq_work_queue(struct irq_work *work) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 79 | { |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 80 | if (!irq_work_claim(work)) { |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 81 | /* |
| 82 | * Already enqueued, can't do! |
| 83 | */ |
| 84 | return false; |
| 85 | } |
| 86 | |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 87 | __irq_work_queue(work); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | EXPORT_SYMBOL_GPL(irq_work_queue); |
| 91 | |
| 92 | /* |
| 93 | * Run the irq_work entries on this cpu. Requires to be ran from hardirq |
| 94 | * context with local IRQs disabled. |
| 95 | */ |
| 96 | void irq_work_run(void) |
| 97 | { |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 98 | struct irq_work *work; |
| 99 | struct llist_head *this_list; |
| 100 | struct llist_node *llnode; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 101 | |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 102 | this_list = &__get_cpu_var(irq_work_list); |
| 103 | if (llist_empty(this_list)) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 104 | return; |
| 105 | |
| 106 | BUG_ON(!in_irq()); |
| 107 | BUG_ON(!irqs_disabled()); |
| 108 | |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 109 | llnode = llist_del_all(this_list); |
| 110 | while (llnode != NULL) { |
| 111 | work = llist_entry(llnode, struct irq_work, llnode); |
Christoph Lameter | 20b8769 | 2010-12-14 10:28:45 -0600 | [diff] [blame] | 112 | |
Peter Zijlstra | 924f8f5 | 2011-09-12 13:12:28 +0200 | [diff] [blame] | 113 | llnode = llist_next(llnode); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 114 | |
| 115 | /* |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 116 | * Clear the PENDING bit, after this point the @work |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 117 | * can be re-used. |
| 118 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 119 | work->flags = IRQ_WORK_BUSY; |
| 120 | work->func(work); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 121 | /* |
| 122 | * Clear the BUSY bit and return to the free state if |
| 123 | * no-one else claimed it meanwhile. |
| 124 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 125 | (void)cmpxchg(&work->flags, IRQ_WORK_BUSY, 0); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | EXPORT_SYMBOL_GPL(irq_work_run); |
| 129 | |
| 130 | /* |
| 131 | * Synchronize against the irq_work @entry, ensures the entry is not |
| 132 | * currently in use. |
| 133 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 134 | void irq_work_sync(struct irq_work *work) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 135 | { |
| 136 | WARN_ON_ONCE(irqs_disabled()); |
| 137 | |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 138 | while (work->flags & IRQ_WORK_BUSY) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 139 | cpu_relax(); |
| 140 | } |
| 141 | EXPORT_SYMBOL_GPL(irq_work_sync); |