hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/kernel/irq_work.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra
34 *
....@@ -28,24 +29,16 @@
2829 */
2930 static bool irq_work_claim(struct irq_work *work)
3031 {
31
- unsigned long flags, oflags, nflags;
32
+ int oflags;
3233
34
+ oflags = atomic_fetch_or(IRQ_WORK_CLAIMED | CSD_TYPE_IRQ_WORK, &work->flags);
3335 /*
34
- * Start with our best wish as a premise but only trust any
35
- * flag value after cmpxchg() result.
36
+ * If the work is already pending, no need to raise the IPI.
37
+ * The pairing atomic_fetch_andnot() in irq_work_run() makes sure
38
+ * everything we did before is visible.
3639 */
37
- flags = work->flags & ~IRQ_WORK_PENDING;
38
- for (;;) {
39
- nflags = flags | IRQ_WORK_CLAIMED;
40
- oflags = cmpxchg(&work->flags, flags, nflags);
41
- if (oflags == flags)
42
- break;
43
- if (oflags & IRQ_WORK_PENDING)
44
- return false;
45
- flags = oflags;
46
- cpu_relax();
47
- }
48
-
40
+ if (oflags & IRQ_WORK_PENDING)
41
+ return false;
4942 return true;
5043 }
5144
....@@ -60,7 +53,7 @@
6053 static void __irq_work_queue_local(struct irq_work *work)
6154 {
6255 /* If the work is "lazy", handle it from next tick if any */
63
- if (work->flags & IRQ_WORK_LAZY) {
56
+ if (atomic_read(&work->flags) & IRQ_WORK_LAZY) {
6457 if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
6558 tick_nohz_tick_stopped())
6659 arch_irq_work_raise();
....@@ -109,8 +102,7 @@
109102 if (cpu != smp_processor_id()) {
110103 /* Arch remote IPI send/receive backend aren't NMI safe */
111104 WARN_ON_ONCE(in_nmi());
112
- if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
113
- arch_send_call_function_single_ipi(cpu);
105
+ __smp_call_single_queue(cpu, &work->llnode);
114106 } else {
115107 __irq_work_queue_local(work);
116108 }
....@@ -119,7 +111,7 @@
119111 return true;
120112 #endif /* CONFIG_SMP */
121113 }
122
-
114
+EXPORT_SYMBOL_GPL(irq_work_queue_on);
123115
124116 bool irq_work_needs_cpu(void)
125117 {
....@@ -138,11 +130,35 @@
138130 return true;
139131 }
140132
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
+
141158 static void irq_work_run_list(struct llist_head *list)
142159 {
143160 struct irq_work *work, *tmp;
144161 struct llist_node *llnode;
145
- unsigned long flags;
146162
147163 BUG_ON(!irqs_disabled());
148164
....@@ -150,24 +166,8 @@
150166 return;
151167
152168 llnode = llist_del_all(list);
153
- llist_for_each_entry_safe(work, tmp, llnode, llnode) {
154
- /*
155
- * Clear the PENDING bit, after this point the @work
156
- * can be re-used.
157
- * Make it immediately visible so that other CPUs trying
158
- * to claim that work don't rely on us to handle their data
159
- * while we are in the middle of the func.
160
- */
161
- flags = work->flags & ~IRQ_WORK_PENDING;
162
- xchg(&work->flags, flags);
163
-
164
- work->func(work);
165
- /*
166
- * Clear the BUSY bit and return to the free state if
167
- * no-one else claimed it meanwhile.
168
- */
169
- (void)cmpxchg(&work->flags, flags, flags & ~IRQ_WORK_BUSY);
170
- }
169
+ llist_for_each_entry_safe(work, tmp, llnode, llnode)
170
+ irq_work_single(work);
171171 }
172172
173173 /*
....@@ -198,7 +198,7 @@
198198 {
199199 lockdep_assert_irqs_enabled();
200200
201
- while (work->flags & IRQ_WORK_BUSY)
201
+ while (atomic_read(&work->flags) & IRQ_WORK_BUSY)
202202 cpu_relax();
203203 }
204204 EXPORT_SYMBOL_GPL(irq_work_sync);