Blame view
kernel/irq_work.c
4.75 KB
457c89965 treewide: Add SPD... |
1 |
// SPDX-License-Identifier: GPL-2.0-only |
e360adbe2 irq_work: Add gen... |
2 |
/* |
90eec103b treewide: Remove ... |
3 |
* Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra |
e360adbe2 irq_work: Add gen... |
4 5 6 7 |
* * Provides a framework for enqueueing and running callbacks from hardirq * context. The enqueueing is NMI-safe. */ |
83e3fa6f0 irq_work: fix com... |
8 |
#include <linux/bug.h> |
e360adbe2 irq_work: Add gen... |
9 |
#include <linux/kernel.h> |
9984de1a5 kernel: Map most ... |
10 |
#include <linux/export.h> |
e360adbe2 irq_work: Add gen... |
11 |
#include <linux/irq_work.h> |
967d1f906 kernel: fix two i... |
12 |
#include <linux/percpu.h> |
e360adbe2 irq_work: Add gen... |
13 |
#include <linux/hardirq.h> |
ef1f09825 irq_work: fix com... |
14 |
#include <linux/irqflags.h> |
bc6679aef irq_work: Make se... |
15 16 |
#include <linux/sched.h> #include <linux/tick.h> |
c0e980a4b irq_work: Flush w... |
17 18 |
#include <linux/cpu.h> #include <linux/notifier.h> |
478850160 irq_work: Impleme... |
19 |
#include <linux/smp.h> |
967d1f906 kernel: fix two i... |
20 |
#include <asm/processor.h> |
e360adbe2 irq_work: Add gen... |
21 |
|
e360adbe2 irq_work: Add gen... |
22 |
|
b93e0b8fa irq_work: Split r... |
23 24 |
static DEFINE_PER_CPU(struct llist_head, raised_list); static DEFINE_PER_CPU(struct llist_head, lazy_list); |
e360adbe2 irq_work: Add gen... |
25 26 27 28 |
/* * Claim the entry so that no one else will poke at it. */ |
38aaf8090 irq_work: Use lli... |
29 |
static bool irq_work_claim(struct irq_work *work) |
e360adbe2 irq_work: Add gen... |
30 |
{ |
25269871d irq_work: Fix irq... |
31 |
int oflags; |
e360adbe2 irq_work: Add gen... |
32 |
|
25269871d irq_work: Fix irq... |
33 |
oflags = atomic_fetch_or(IRQ_WORK_CLAIMED, &work->flags); |
e0bbe2d80 irq_work: Fix rac... |
34 |
/* |
25269871d irq_work: Fix irq... |
35 |
* If the work is already pending, no need to raise the IPI. |
feb4a5132 irq_work: Slightl... |
36 |
* The pairing atomic_fetch_andnot() in irq_work_run() makes sure |
25269871d irq_work: Fix irq... |
37 |
* everything we did before is visible. |
e0bbe2d80 irq_work: Fix rac... |
38 |
*/ |
25269871d irq_work: Fix irq... |
39 40 |
if (oflags & IRQ_WORK_PENDING) return false; |
e360adbe2 irq_work: Add gen... |
41 42 |
return true; } |
e360adbe2 irq_work: Add gen... |
43 44 45 46 47 48 |
void __weak arch_irq_work_raise(void) { /* * Lame architectures will get the timer tick callback */ } |
471ba0e68 irq_work: Do not ... |
49 50 |
/* Enqueue on current CPU, work must already be claimed and preempt disabled */ static void __irq_work_queue_local(struct irq_work *work) |
478850160 irq_work: Impleme... |
51 |
{ |
471ba0e68 irq_work: Do not ... |
52 |
/* If the work is "lazy", handle it from next tick if any */ |
153bedbac irq_work: Convert... |
53 |
if (atomic_read(&work->flags) & IRQ_WORK_LAZY) { |
471ba0e68 irq_work: Do not ... |
54 55 56 57 58 59 60 61 |
if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) && tick_nohz_tick_stopped()) arch_irq_work_raise(); } else { if (llist_add(&work->llnode, this_cpu_ptr(&raised_list))) arch_irq_work_raise(); } } |
478850160 irq_work: Impleme... |
62 |
|
471ba0e68 irq_work: Do not ... |
63 64 65 |
/* Enqueue the irq work @work on the current CPU */ bool irq_work_queue(struct irq_work *work) { |
478850160 irq_work: Impleme... |
66 67 68 |
/* Only queue if not already pending */ if (!irq_work_claim(work)) return false; |
471ba0e68 irq_work: Do not ... |
69 70 71 72 |
/* Queue the entry and raise the IPI if needed. */ preempt_disable(); __irq_work_queue_local(work); preempt_enable(); |
6733bab7b irq_work: Map irq... |
73 |
|
478850160 irq_work: Impleme... |
74 75 |
return true; } |
471ba0e68 irq_work: Do not ... |
76 |
EXPORT_SYMBOL_GPL(irq_work_queue); |
478850160 irq_work: Impleme... |
77 |
|
471ba0e68 irq_work: Do not ... |
78 79 80 81 82 83 84 |
/* * Enqueue the irq_work @work on @cpu unless it's already pending * somewhere. * * Can be re-enqueued while the callback is still in progress. */ bool irq_work_queue_on(struct irq_work *work, int cpu) |
e360adbe2 irq_work: Add gen... |
85 |
{ |
471ba0e68 irq_work: Do not ... |
86 87 88 89 90 91 |
#ifndef CONFIG_SMP return irq_work_queue(work); #else /* CONFIG_SMP: */ /* All work should have been flushed before going offline */ WARN_ON_ONCE(cpu_is_offline(cpu)); |
c02cf5f8e irq_work: Remove ... |
92 93 |
/* Only queue if not already pending */ if (!irq_work_claim(work)) |
cd578abb2 perf/x86: Warn to... |
94 |
return false; |
c02cf5f8e irq_work: Remove ... |
95 |
|
20b876918 irq_work: Use per... |
96 |
preempt_disable(); |
471ba0e68 irq_work: Do not ... |
97 98 99 100 101 |
if (cpu != smp_processor_id()) { /* Arch remote IPI send/receive backend aren't NMI safe */ WARN_ON_ONCE(in_nmi()); if (llist_add(&work->llnode, &per_cpu(raised_list, cpu))) arch_send_call_function_single_ipi(cpu); |
b93e0b8fa irq_work: Split r... |
102 |
} else { |
471ba0e68 irq_work: Do not ... |
103 |
__irq_work_queue_local(work); |
bc6679aef irq_work: Make se... |
104 |
} |
20b876918 irq_work: Use per... |
105 |
preempt_enable(); |
cd578abb2 perf/x86: Warn to... |
106 107 |
return true; |
471ba0e68 irq_work: Do not ... |
108 |
#endif /* CONFIG_SMP */ |
e360adbe2 irq_work: Add gen... |
109 |
} |
471ba0e68 irq_work: Do not ... |
110 |
|
e360adbe2 irq_work: Add gen... |
111 |
|
00b429591 irq_work: Don't s... |
112 113 |
bool irq_work_needs_cpu(void) { |
b93e0b8fa irq_work: Split r... |
114 |
struct llist_head *raised, *lazy; |
00b429591 irq_work: Don't s... |
115 |
|
22127e93c time: Replace __g... |
116 117 |
raised = this_cpu_ptr(&raised_list); lazy = this_cpu_ptr(&lazy_list); |
76a33061b irq_work: Force r... |
118 119 120 121 |
if (llist_empty(raised) || arch_irq_work_has_interrupt()) if (llist_empty(lazy)) return false; |
00b429591 irq_work: Don't s... |
122 |
|
8aa2accee irq_work: Warn if... |
123 124 |
/* All work should have been flushed before going offline */ WARN_ON_ONCE(cpu_is_offline(smp_processor_id())); |
00b429591 irq_work: Don't s... |
125 126 |
return true; } |
b93e0b8fa irq_work: Split r... |
127 |
static void irq_work_run_list(struct llist_head *list) |
e360adbe2 irq_work: Add gen... |
128 |
{ |
d00a08cf9 irq/work: Use lli... |
129 |
struct irq_work *work, *tmp; |
38aaf8090 irq_work: Use lli... |
130 |
struct llist_node *llnode; |
e360adbe2 irq_work: Add gen... |
131 |
|
b93e0b8fa irq_work: Split r... |
132 |
BUG_ON(!irqs_disabled()); |
bc6679aef irq_work: Make se... |
133 |
|
b93e0b8fa irq_work: Split r... |
134 |
if (llist_empty(list)) |
e360adbe2 irq_work: Add gen... |
135 |
return; |
b93e0b8fa irq_work: Split r... |
136 |
llnode = llist_del_all(list); |
d00a08cf9 irq/work: Use lli... |
137 |
llist_for_each_entry_safe(work, tmp, llnode, llnode) { |
feb4a5132 irq_work: Slightl... |
138 |
int flags; |
e360adbe2 irq_work: Add gen... |
139 |
/* |
38aaf8090 irq_work: Use lli... |
140 |
* Clear the PENDING bit, after this point the @work |
e360adbe2 irq_work: Add gen... |
141 |
* can be re-used. |
c8446b75b irq_work: Fix rac... |
142 143 144 |
* 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. |
e360adbe2 irq_work: Add gen... |
145 |
*/ |
feb4a5132 irq_work: Slightl... |
146 |
flags = atomic_fetch_andnot(IRQ_WORK_PENDING, &work->flags); |
bc6679aef irq_work: Make se... |
147 |
|
49915ac35 lockdep: Annotate... |
148 |
lockdep_irq_work_enter(work); |
38aaf8090 irq_work: Use lli... |
149 |
work->func(work); |
49915ac35 lockdep: Annotate... |
150 |
lockdep_irq_work_exit(work); |
e360adbe2 irq_work: Add gen... |
151 152 153 154 |
/* * Clear the BUSY bit and return to the free state if * no-one else claimed it meanwhile. */ |
e9838bd51 irq_work: Fix IRQ... |
155 |
flags &= ~IRQ_WORK_PENDING; |
153bedbac irq_work: Convert... |
156 |
(void)atomic_cmpxchg(&work->flags, flags, flags & ~IRQ_WORK_BUSY); |
e360adbe2 irq_work: Add gen... |
157 158 |
} } |
c0e980a4b irq_work: Flush w... |
159 160 |
/* |
a77353e5e irq_work: Remove ... |
161 162 |
* hotplug calls this through: * hotplug_cfd() -> flush_smp_call_function_queue() |
c0e980a4b irq_work: Flush w... |
163 164 165 |
*/ void irq_work_run(void) { |
22127e93c time: Replace __g... |
166 167 |
irq_work_run_list(this_cpu_ptr(&raised_list)); irq_work_run_list(this_cpu_ptr(&lazy_list)); |
c0e980a4b irq_work: Flush w... |
168 |
} |
e360adbe2 irq_work: Add gen... |
169 |
EXPORT_SYMBOL_GPL(irq_work_run); |
76a33061b irq_work: Force r... |
170 171 |
void irq_work_tick(void) { |
56e4dea81 percpu: Convert r... |
172 |
struct llist_head *raised = this_cpu_ptr(&raised_list); |
76a33061b irq_work: Force r... |
173 174 175 |
if (!llist_empty(raised) && !arch_irq_work_has_interrupt()) irq_work_run_list(raised); |
56e4dea81 percpu: Convert r... |
176 |
irq_work_run_list(this_cpu_ptr(&lazy_list)); |
76a33061b irq_work: Force r... |
177 |
} |
e360adbe2 irq_work: Add gen... |
178 179 180 181 |
/* * Synchronize against the irq_work @entry, ensures the entry is not * currently in use. */ |
38aaf8090 irq_work: Use lli... |
182 |
void irq_work_sync(struct irq_work *work) |
e360adbe2 irq_work: Add gen... |
183 |
{ |
3c7169a3b irq_work: Use loc... |
184 |
lockdep_assert_irqs_enabled(); |
e360adbe2 irq_work: Add gen... |
185 |
|
153bedbac irq_work: Convert... |
186 |
while (atomic_read(&work->flags) & IRQ_WORK_BUSY) |
e360adbe2 irq_work: Add gen... |
187 188 189 |
cpu_relax(); } EXPORT_SYMBOL_GPL(irq_work_sync); |