blob: 73288914ed5e78cc44b596ab5428ac98aceb73b3 [file] [log] [blame]
Peter Zijlstrae360adb2010-10-14 14:01:34 +08001/*
Peter Zijlstra90eec102015-11-16 11:08:45 +01002 * Copyright (C) 2010 Red Hat, Inc., Peter Zijlstra
Peter Zijlstrae360adb2010-10-14 14:01:34 +08003 *
4 * Provides a framework for enqueueing and running callbacks from hardirq
5 * context. The enqueueing is NMI-safe.
6 */
7
Paul Gortmaker83e3fa62012-04-01 16:38:37 -04008#include <linux/bug.h>
Peter Zijlstrae360adb2010-10-14 14:01:34 +08009#include <linux/kernel.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040010#include <linux/export.h>
Peter Zijlstrae360adb2010-10-14 14:01:34 +080011#include <linux/irq_work.h>
Paul Gortmaker967d1f92011-07-18 13:03:04 -040012#include <linux/percpu.h>
Peter Zijlstrae360adb2010-10-14 14:01:34 +080013#include <linux/hardirq.h>
Chris Metcalfef1f0982012-04-11 12:21:39 -040014#include <linux/irqflags.h>
Frederic Weisbeckerbc6679a2012-10-19 16:43:41 -040015#include <linux/sched.h>
16#include <linux/tick.h>
Steven Rostedtc0e980a2012-11-15 11:34:21 -050017#include <linux/cpu.h>
18#include <linux/notifier.h>
Frederic Weisbecker47885012014-05-08 01:37:48 +020019#include <linux/smp.h>
Paul Gortmaker967d1f92011-07-18 13:03:04 -040020#include <asm/processor.h>
Peter Zijlstrae360adb2010-10-14 14:01:34 +080021
Peter Zijlstrae360adb2010-10-14 14:01:34 +080022
Frederic Weisbeckerb93e0b82014-05-23 18:10:21 +020023static DEFINE_PER_CPU(struct llist_head, raised_list);
24static DEFINE_PER_CPU(struct llist_head, lazy_list);
Peter Zijlstrae360adb2010-10-14 14:01:34 +080025
26/*
27 * Claim the entry so that no one else will poke at it.
28 */
Huang Ying38aaf802011-09-08 14:00:46 +080029static bool irq_work_claim(struct irq_work *work)
Peter Zijlstrae360adb2010-10-14 14:01:34 +080030{
Frederic Weisbeckere0bbe2d2012-10-27 15:21:36 +020031 unsigned long flags, oflags, nflags;
Peter Zijlstrae360adb2010-10-14 14:01:34 +080032
Frederic Weisbeckere0bbe2d2012-10-27 15:21:36 +020033 /*
34 * Start with our best wish as a premise but only trust any
35 * flag value after cmpxchg() result.
36 */
37 flags = work->flags & ~IRQ_WORK_PENDING;
Huang Ying38aaf802011-09-08 14:00:46 +080038 for (;;) {
Bartosz Golaszewski6baf9e62018-01-05 05:19:56 +010039 nflags = flags | IRQ_WORK_CLAIMED;
Frederic Weisbeckere0bbe2d2012-10-27 15:21:36 +020040 oflags = cmpxchg(&work->flags, flags, nflags);
41 if (oflags == flags)
Huang Ying38aaf802011-09-08 14:00:46 +080042 break;
Frederic Weisbeckere0bbe2d2012-10-27 15:21:36 +020043 if (oflags & IRQ_WORK_PENDING)
44 return false;
45 flags = oflags;
Huang Ying38aaf802011-09-08 14:00:46 +080046 cpu_relax();
47 }
Peter Zijlstrae360adb2010-10-14 14:01:34 +080048
49 return true;
50}
51
Peter Zijlstrae360adb2010-10-14 14:01:34 +080052void __weak arch_irq_work_raise(void)
53{
54 /*
55 * Lame architectures will get the timer tick callback
56 */
57}
58
Nicholas Piggin471ba0e2019-04-09 19:34:03 +100059/* Enqueue on current CPU, work must already be claimed and preempt disabled */
60static void __irq_work_queue_local(struct irq_work *work)
Frederic Weisbecker47885012014-05-08 01:37:48 +020061{
Nicholas Piggin471ba0e2019-04-09 19:34:03 +100062 /* If the work is "lazy", handle it from next tick if any */
63 if (work->flags & IRQ_WORK_LAZY) {
64 if (llist_add(&work->llnode, this_cpu_ptr(&lazy_list)) &&
65 tick_nohz_tick_stopped())
66 arch_irq_work_raise();
67 } else {
68 if (llist_add(&work->llnode, this_cpu_ptr(&raised_list)))
69 arch_irq_work_raise();
70 }
Frederic Weisbecker47885012014-05-08 01:37:48 +020071}
Frederic Weisbecker47885012014-05-08 01:37:48 +020072
73/* Enqueue the irq work @work on the current CPU */
Peter Zijlstracd578ab2014-02-11 16:01:16 +010074bool irq_work_queue(struct irq_work *work)
Peter Zijlstrae360adb2010-10-14 14:01:34 +080075{
anish kumarc02cf5f2013-02-03 22:08:23 +010076 /* Only queue if not already pending */
77 if (!irq_work_claim(work))
Peter Zijlstracd578ab2014-02-11 16:01:16 +010078 return false;
anish kumarc02cf5f2013-02-03 22:08:23 +010079
80 /* Queue the entry and raise the IPI if needed. */
Christoph Lameter20b87692010-12-14 10:28:45 -060081 preempt_disable();
Nicholas Piggin471ba0e2019-04-09 19:34:03 +100082 __irq_work_queue_local(work);
Christoph Lameter20b87692010-12-14 10:28:45 -060083 preempt_enable();
Peter Zijlstracd578ab2014-02-11 16:01:16 +010084
85 return true;
Peter Zijlstrae360adb2010-10-14 14:01:34 +080086}
Peter Zijlstrae360adb2010-10-14 14:01:34 +080087EXPORT_SYMBOL_GPL(irq_work_queue);
88
Nicholas Piggin471ba0e2019-04-09 19:34:03 +100089/*
90 * Enqueue the irq_work @work on @cpu unless it's already pending
91 * somewhere.
92 *
93 * Can be re-enqueued while the callback is still in progress.
94 */
95bool irq_work_queue_on(struct irq_work *work, int cpu)
96{
97#ifndef CONFIG_SMP
98 return irq_work_queue(work);
99
100#else /* CONFIG_SMP: */
101 /* All work should have been flushed before going offline */
102 WARN_ON_ONCE(cpu_is_offline(cpu));
103
104 /* Only queue if not already pending */
105 if (!irq_work_claim(work))
106 return false;
107
108 preempt_disable();
109 if (cpu != smp_processor_id()) {
110 /* Arch remote IPI send/receive backend aren't NMI safe */
111 WARN_ON_ONCE(in_nmi());
112 if (llist_add(&work->llnode, &per_cpu(raised_list, cpu)))
113 arch_send_call_function_single_ipi(cpu);
114 } else {
115 __irq_work_queue_local(work);
116 }
117 preempt_enable();
118
119 return true;
120#endif /* CONFIG_SMP */
121}
122
123
Frederic Weisbecker00b42952012-11-07 21:03:07 +0100124bool irq_work_needs_cpu(void)
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800125{
Frederic Weisbeckerb93e0b82014-05-23 18:10:21 +0200126 struct llist_head *raised, *lazy;
Frederic Weisbecker00b42952012-11-07 21:03:07 +0100127
Christoph Lameter22127e92014-08-17 12:30:25 -0500128 raised = this_cpu_ptr(&raised_list);
129 lazy = this_cpu_ptr(&lazy_list);
Frederic Weisbecker76a33062014-08-16 18:37:19 +0200130
131 if (llist_empty(raised) || arch_irq_work_has_interrupt())
132 if (llist_empty(lazy))
133 return false;
Frederic Weisbecker00b42952012-11-07 21:03:07 +0100134
Steven Rostedt8aa2acc2012-11-15 12:52:44 -0500135 /* All work should have been flushed before going offline */
136 WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
137
Frederic Weisbecker00b42952012-11-07 21:03:07 +0100138 return true;
139}
140
Frederic Weisbeckerb93e0b82014-05-23 18:10:21 +0200141static void irq_work_run_list(struct llist_head *list)
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800142{
Thomas Gleixnerd00a08c2017-11-12 13:02:51 +0100143 struct irq_work *work, *tmp;
Huang Ying38aaf802011-09-08 14:00:46 +0800144 struct llist_node *llnode;
Thomas Gleixnerd00a08c2017-11-12 13:02:51 +0100145 unsigned long flags;
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800146
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800147 BUG_ON(!irqs_disabled());
148
Frederic Weisbeckerb93e0b82014-05-23 18:10:21 +0200149 if (llist_empty(list))
150 return;
151
152 llnode = llist_del_all(list);
Thomas Gleixnerd00a08c2017-11-12 13:02:51 +0100153 llist_for_each_entry_safe(work, tmp, llnode, llnode) {
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800154 /*
Huang Ying38aaf802011-09-08 14:00:46 +0800155 * Clear the PENDING bit, after this point the @work
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800156 * can be re-used.
Frederic Weisbeckerc8446b72012-10-30 13:33:54 +0100157 * 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.
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800160 */
Frederic Weisbeckerbc6679a2012-10-19 16:43:41 -0400161 flags = work->flags & ~IRQ_WORK_PENDING;
162 xchg(&work->flags, flags);
163
Huang Ying38aaf802011-09-08 14:00:46 +0800164 work->func(work);
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800165 /*
166 * Clear the BUSY bit and return to the free state if
167 * no-one else claimed it meanwhile.
168 */
Frederic Weisbeckerbc6679a2012-10-19 16:43:41 -0400169 (void)cmpxchg(&work->flags, flags, flags & ~IRQ_WORK_BUSY);
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800170 }
171}
Steven Rostedtc0e980a2012-11-15 11:34:21 -0500172
173/*
Peter Zijlstraa77353e2014-06-25 07:13:07 +0200174 * hotplug calls this through:
175 * hotplug_cfd() -> flush_smp_call_function_queue()
Steven Rostedtc0e980a2012-11-15 11:34:21 -0500176 */
177void irq_work_run(void)
178{
Christoph Lameter22127e92014-08-17 12:30:25 -0500179 irq_work_run_list(this_cpu_ptr(&raised_list));
180 irq_work_run_list(this_cpu_ptr(&lazy_list));
Steven Rostedtc0e980a2012-11-15 11:34:21 -0500181}
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800182EXPORT_SYMBOL_GPL(irq_work_run);
183
Frederic Weisbecker76a33062014-08-16 18:37:19 +0200184void irq_work_tick(void)
185{
Christoph Lameter56e4dea2014-10-27 10:49:45 -0500186 struct llist_head *raised = this_cpu_ptr(&raised_list);
Frederic Weisbecker76a33062014-08-16 18:37:19 +0200187
188 if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
189 irq_work_run_list(raised);
Christoph Lameter56e4dea2014-10-27 10:49:45 -0500190 irq_work_run_list(this_cpu_ptr(&lazy_list));
Frederic Weisbecker76a33062014-08-16 18:37:19 +0200191}
192
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800193/*
194 * Synchronize against the irq_work @entry, ensures the entry is not
195 * currently in use.
196 */
Huang Ying38aaf802011-09-08 14:00:46 +0800197void irq_work_sync(struct irq_work *work)
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800198{
Frederic Weisbecker3c7169a2017-11-06 16:01:26 +0100199 lockdep_assert_irqs_enabled();
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800200
Huang Ying38aaf802011-09-08 14:00:46 +0800201 while (work->flags & IRQ_WORK_BUSY)
Peter Zijlstrae360adb2010-10-14 14:01:34 +0800202 cpu_relax();
203}
204EXPORT_SYMBOL_GPL(irq_work_sync);