Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 2 | /* |
Peter Zijlstra | 90eec10 | 2015-11-16 11:08:45 +0100 | [diff] [blame] | 3 | * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 4 | * |
| 5 | * Provides a framework for enqueueing and running callbacks from hardirq |
| 6 | * context. The enqueueing is NMI-safe. |
| 7 | */ |
| 8 | |
Paul Gortmaker | 83e3fa6 | 2012-04-01 16:38:37 -0400 | [diff] [blame] | 9 | #include <linux/bug.h> |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 10 | #include <linux/kernel.h> |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 11 | #include <linux/export.h> |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 12 | #include <linux/irq_work.h> |
Paul Gortmaker | 967d1f9 | 2011-07-18 13:03:04 -0400 | [diff] [blame] | 13 | #include <linux/percpu.h> |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 14 | #include <linux/hardirq.h> |
Chris Metcalf | ef1f098 | 2012-04-11 12:21:39 -0400 | [diff] [blame] | 15 | #include <linux/irqflags.h> |
Frederic Weisbecker | bc6679a | 2012-10-19 16:43:41 -0400 | [diff] [blame] | 16 | #include <linux/sched.h> |
| 17 | #include <linux/tick.h> |
Steven Rostedt | c0e980a | 2012-11-15 11:34:21 -0500 | [diff] [blame] | 18 | #include <linux/cpu.h> |
| 19 | #include <linux/notifier.h> |
Frederic Weisbecker | 4788501 | 2014-05-08 01:37:48 +0200 | [diff] [blame] | 20 | #include <linux/smp.h> |
Paul Gortmaker | 967d1f9 | 2011-07-18 13:03:04 -0400 | [diff] [blame] | 21 | #include <asm/processor.h> |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 22 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 23 | |
Frederic Weisbecker | b93e0b8 | 2014-05-23 18:10:21 +0200 | [diff] [blame] | 24 | static DEFINE_PER_CPU(struct llist_head, raised_list); |
| 25 | static DEFINE_PER_CPU(struct llist_head, lazy_list); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * Claim the entry so that no one else will poke at it. |
| 29 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 30 | static bool irq_work_claim(struct irq_work *work) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 31 | { |
Frederic Weisbecker | 2526987 | 2019-11-08 17:08:56 +0100 | [diff] [blame] | 32 | int oflags; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 33 | |
Peter Zijlstra | 4b44a21 | 2020-05-26 18:11:02 +0200 | [diff] [blame] | 34 | oflags = atomic_fetch_or(IRQ_WORK_CLAIMED | CSD_TYPE_IRQ_WORK, &work->flags); |
Frederic Weisbecker | e0bbe2d | 2012-10-27 15:21:36 +0200 | [diff] [blame] | 35 | /* |
Frederic Weisbecker | 2526987 | 2019-11-08 17:08:56 +0100 | [diff] [blame] | 36 | * If the work is already pending, no need to raise the IPI. |
Frederic Weisbecker | feb4a51 | 2019-11-08 17:08:57 +0100 | [diff] [blame] | 37 | * The pairing atomic_fetch_andnot() in irq_work_run() makes sure |
Frederic Weisbecker | 2526987 | 2019-11-08 17:08:56 +0100 | [diff] [blame] | 38 | * everything we did before is visible. |
Frederic Weisbecker | e0bbe2d | 2012-10-27 15:21:36 +0200 | [diff] [blame] | 39 | */ |
Frederic Weisbecker | 2526987 | 2019-11-08 17:08:56 +0100 | [diff] [blame] | 40 | if (oflags & IRQ_WORK_PENDING) |
| 41 | return false; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 45 | void __weak arch_irq_work_raise(void) |
| 46 | { |
| 47 | /* |
| 48 | * Lame architectures will get the timer tick callback |
| 49 | */ |
| 50 | } |
| 51 | |
Nicholas Piggin | 471ba0e | 2019-04-09 19:34:03 +1000 | [diff] [blame] | 52 | /* Enqueue on current CPU, work must already be claimed and preempt disabled */ |
| 53 | static void __irq_work_queue_local(struct irq_work *work) |
Frederic Weisbecker | 4788501 | 2014-05-08 01:37:48 +0200 | [diff] [blame] | 54 | { |
Nicholas Piggin | 471ba0e | 2019-04-09 19:34:03 +1000 | [diff] [blame] | 55 | /* If the work is "lazy", handle it from next tick if any */ |
Frederic Weisbecker | 153bedb | 2019-11-08 17:08:55 +0100 | [diff] [blame] | 56 | if (atomic_read(&work->flags) & IRQ_WORK_LAZY) { |
Nicholas Piggin | 471ba0e | 2019-04-09 19:34:03 +1000 | [diff] [blame] | 57 | if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) && |
| 58 | tick_nohz_tick_stopped()) |
| 59 | arch_irq_work_raise(); |
| 60 | } else { |
| 61 | if (llist_add(&work->llnode, this_cpu_ptr(&raised_list))) |
| 62 | arch_irq_work_raise(); |
| 63 | } |
Frederic Weisbecker | 4788501 | 2014-05-08 01:37:48 +0200 | [diff] [blame] | 64 | } |
Frederic Weisbecker | 4788501 | 2014-05-08 01:37:48 +0200 | [diff] [blame] | 65 | |
| 66 | /* Enqueue the irq work @work on the current CPU */ |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 67 | bool irq_work_queue(struct irq_work *work) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 68 | { |
anish kumar | c02cf5f | 2013-02-03 22:08:23 +0100 | [diff] [blame] | 69 | /* Only queue if not already pending */ |
| 70 | if (!irq_work_claim(work)) |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 71 | return false; |
anish kumar | c02cf5f | 2013-02-03 22:08:23 +0100 | [diff] [blame] | 72 | |
| 73 | /* Queue the entry and raise the IPI if needed. */ |
Christoph Lameter | 20b8769 | 2010-12-14 10:28:45 -0600 | [diff] [blame] | 74 | preempt_disable(); |
Nicholas Piggin | 471ba0e | 2019-04-09 19:34:03 +1000 | [diff] [blame] | 75 | __irq_work_queue_local(work); |
Christoph Lameter | 20b8769 | 2010-12-14 10:28:45 -0600 | [diff] [blame] | 76 | preempt_enable(); |
Peter Zijlstra | cd578ab | 2014-02-11 16:01:16 +0100 | [diff] [blame] | 77 | |
| 78 | return true; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 79 | } |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 80 | EXPORT_SYMBOL_GPL(irq_work_queue); |
| 81 | |
Nicholas Piggin | 471ba0e | 2019-04-09 19:34:03 +1000 | [diff] [blame] | 82 | /* |
| 83 | * Enqueue the irq_work @work on @cpu unless it's already pending |
| 84 | * somewhere. |
| 85 | * |
| 86 | * Can be re-enqueued while the callback is still in progress. |
| 87 | */ |
| 88 | bool irq_work_queue_on(struct irq_work *work, int cpu) |
| 89 | { |
| 90 | #ifndef CONFIG_SMP |
| 91 | return irq_work_queue(work); |
| 92 | |
| 93 | #else /* CONFIG_SMP: */ |
| 94 | /* All work should have been flushed before going offline */ |
| 95 | WARN_ON_ONCE(cpu_is_offline(cpu)); |
| 96 | |
| 97 | /* Only queue if not already pending */ |
| 98 | if (!irq_work_claim(work)) |
| 99 | return false; |
| 100 | |
| 101 | preempt_disable(); |
| 102 | if (cpu != smp_processor_id()) { |
| 103 | /* Arch remote IPI send/receive backend aren't NMI safe */ |
| 104 | WARN_ON_ONCE(in_nmi()); |
Peter Zijlstra | 4b44a21 | 2020-05-26 18:11:02 +0200 | [diff] [blame] | 105 | __smp_call_single_queue(cpu, &work->llnode); |
Nicholas Piggin | 471ba0e | 2019-04-09 19:34:03 +1000 | [diff] [blame] | 106 | } else { |
| 107 | __irq_work_queue_local(work); |
| 108 | } |
| 109 | preempt_enable(); |
| 110 | |
| 111 | return true; |
| 112 | #endif /* CONFIG_SMP */ |
| 113 | } |
| 114 | |
| 115 | |
Frederic Weisbecker | 00b4295 | 2012-11-07 21:03:07 +0100 | [diff] [blame] | 116 | bool irq_work_needs_cpu(void) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 117 | { |
Frederic Weisbecker | b93e0b8 | 2014-05-23 18:10:21 +0200 | [diff] [blame] | 118 | struct llist_head *raised, *lazy; |
Frederic Weisbecker | 00b4295 | 2012-11-07 21:03:07 +0100 | [diff] [blame] | 119 | |
Christoph Lameter | 22127e9 | 2014-08-17 12:30:25 -0500 | [diff] [blame] | 120 | raised = this_cpu_ptr(&raised_list); |
| 121 | lazy = this_cpu_ptr(&lazy_list); |
Frederic Weisbecker | 76a3306 | 2014-08-16 18:37:19 +0200 | [diff] [blame] | 122 | |
| 123 | if (llist_empty(raised) || arch_irq_work_has_interrupt()) |
| 124 | if (llist_empty(lazy)) |
| 125 | return false; |
Frederic Weisbecker | 00b4295 | 2012-11-07 21:03:07 +0100 | [diff] [blame] | 126 | |
Steven Rostedt | 8aa2acc | 2012-11-15 12:52:44 -0500 | [diff] [blame] | 127 | /* All work should have been flushed before going offline */ |
| 128 | WARN_ON_ONCE(cpu_is_offline(smp_processor_id())); |
| 129 | |
Frederic Weisbecker | 00b4295 | 2012-11-07 21:03:07 +0100 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Peter Zijlstra | 4b44a21 | 2020-05-26 18:11:02 +0200 | [diff] [blame] | 133 | void irq_work_single(void *arg) |
| 134 | { |
| 135 | struct irq_work *work = arg; |
| 136 | int flags; |
| 137 | |
| 138 | /* |
| 139 | * Clear the PENDING bit, after this point the @work |
| 140 | * can be re-used. |
| 141 | * Make it immediately visible so that other CPUs trying |
| 142 | * to claim that work don't rely on us to handle their data |
| 143 | * while we are in the middle of the func. |
| 144 | */ |
| 145 | flags = atomic_fetch_andnot(IRQ_WORK_PENDING, &work->flags); |
| 146 | |
| 147 | lockdep_irq_work_enter(work); |
| 148 | work->func(work); |
| 149 | lockdep_irq_work_exit(work); |
| 150 | /* |
| 151 | * Clear the BUSY bit and return to the free state if |
| 152 | * no-one else claimed it meanwhile. |
| 153 | */ |
| 154 | flags &= ~IRQ_WORK_PENDING; |
| 155 | (void)atomic_cmpxchg(&work->flags, flags, flags & ~IRQ_WORK_BUSY); |
| 156 | } |
| 157 | |
Frederic Weisbecker | b93e0b8 | 2014-05-23 18:10:21 +0200 | [diff] [blame] | 158 | static void irq_work_run_list(struct llist_head *list) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 159 | { |
Thomas Gleixner | d00a08c | 2017-11-12 13:02:51 +0100 | [diff] [blame] | 160 | struct irq_work *work, *tmp; |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 161 | struct llist_node *llnode; |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 162 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 163 | BUG_ON(!irqs_disabled()); |
| 164 | |
Frederic Weisbecker | b93e0b8 | 2014-05-23 18:10:21 +0200 | [diff] [blame] | 165 | if (llist_empty(list)) |
| 166 | return; |
| 167 | |
| 168 | llnode = llist_del_all(list); |
Peter Zijlstra | 4b44a21 | 2020-05-26 18:11:02 +0200 | [diff] [blame] | 169 | llist_for_each_entry_safe(work, tmp, llnode, llnode) |
| 170 | irq_work_single(work); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 171 | } |
Steven Rostedt | c0e980a | 2012-11-15 11:34:21 -0500 | [diff] [blame] | 172 | |
| 173 | /* |
Peter Zijlstra | a77353e | 2014-06-25 07:13:07 +0200 | [diff] [blame] | 174 | * hotplug calls this through: |
| 175 | * hotplug_cfd() -> flush_smp_call_function_queue() |
Steven Rostedt | c0e980a | 2012-11-15 11:34:21 -0500 | [diff] [blame] | 176 | */ |
| 177 | void irq_work_run(void) |
| 178 | { |
Christoph Lameter | 22127e9 | 2014-08-17 12:30:25 -0500 | [diff] [blame] | 179 | irq_work_run_list(this_cpu_ptr(&raised_list)); |
| 180 | irq_work_run_list(this_cpu_ptr(&lazy_list)); |
Steven Rostedt | c0e980a | 2012-11-15 11:34:21 -0500 | [diff] [blame] | 181 | } |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 182 | EXPORT_SYMBOL_GPL(irq_work_run); |
| 183 | |
Frederic Weisbecker | 76a3306 | 2014-08-16 18:37:19 +0200 | [diff] [blame] | 184 | void irq_work_tick(void) |
| 185 | { |
Christoph Lameter | 56e4dea | 2014-10-27 10:49:45 -0500 | [diff] [blame] | 186 | struct llist_head *raised = this_cpu_ptr(&raised_list); |
Frederic Weisbecker | 76a3306 | 2014-08-16 18:37:19 +0200 | [diff] [blame] | 187 | |
| 188 | if (!llist_empty(raised) && !arch_irq_work_has_interrupt()) |
| 189 | irq_work_run_list(raised); |
Christoph Lameter | 56e4dea | 2014-10-27 10:49:45 -0500 | [diff] [blame] | 190 | irq_work_run_list(this_cpu_ptr(&lazy_list)); |
Frederic Weisbecker | 76a3306 | 2014-08-16 18:37:19 +0200 | [diff] [blame] | 191 | } |
| 192 | |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 193 | /* |
| 194 | * Synchronize against the irq_work @entry, ensures the entry is not |
| 195 | * currently in use. |
| 196 | */ |
Huang Ying | 38aaf80 | 2011-09-08 14:00:46 +0800 | [diff] [blame] | 197 | void irq_work_sync(struct irq_work *work) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 198 | { |
Frederic Weisbecker | 3c7169a | 2017-11-06 16:01:26 +0100 | [diff] [blame] | 199 | lockdep_assert_irqs_enabled(); |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 200 | |
Frederic Weisbecker | 153bedb | 2019-11-08 17:08:55 +0100 | [diff] [blame] | 201 | while (atomic_read(&work->flags) & IRQ_WORK_BUSY) |
Peter Zijlstra | e360adb | 2010-10-14 14:01:34 +0800 | [diff] [blame] | 202 | cpu_relax(); |
| 203 | } |
| 204 | EXPORT_SYMBOL_GPL(irq_work_sync); |