Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / kernel / irq_work.c
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/bug.h>
9 #include <linux/kernel.h>
10 #include <linux/export.h>
11 #include <linux/irq_work.h>
12 #include <linux/percpu.h>
13 #include <linux/hardirq.h>
14 #include <linux/irqflags.h>
15 #include <linux/sched.h>
16 #include <linux/tick.h>
17 #include <linux/cpu.h>
18 #include <linux/notifier.h>
19 #include <linux/smp.h>
20 #include <linux/interrupt.h>
21 #include <asm/processor.h>
22
23
24 static DEFINE_PER_CPU(struct llist_head, raised_list);
25 static DEFINE_PER_CPU(struct llist_head, lazy_list);
26
27 /*
28  * Claim the entry so that no one else will poke at it.
29  */
30 static bool irq_work_claim(struct irq_work *work)
31 {
32         unsigned long flags, oflags, nflags;
33
34         /*
35          * Start with our best wish as a premise but only trust any
36          * flag value after cmpxchg() result.
37          */
38         flags = work->flags & ~IRQ_WORK_PENDING;
39         for (;;) {
40                 nflags = flags | IRQ_WORK_FLAGS;
41                 oflags = cmpxchg(&work->flags, flags, nflags);
42                 if (oflags == flags)
43                         break;
44                 if (oflags & IRQ_WORK_PENDING)
45                         return false;
46                 flags = oflags;
47                 cpu_relax();
48         }
49
50         return true;
51 }
52
53 void __weak arch_irq_work_raise(void)
54 {
55         /*
56          * Lame architectures will get the timer tick callback
57          */
58 }
59
60 #ifdef CONFIG_SMP
61 /*
62  * Enqueue the irq_work @work on @cpu unless it's already pending
63  * somewhere.
64  *
65  * Can be re-enqueued while the callback is still in progress.
66  */
67 bool irq_work_queue_on(struct irq_work *work, int cpu)
68 {
69         struct llist_head *list;
70
71         /* All work should have been flushed before going offline */
72         WARN_ON_ONCE(cpu_is_offline(cpu));
73
74         /* Arch remote IPI send/receive backend aren't NMI safe */
75         WARN_ON_ONCE(in_nmi());
76
77         /* Only queue if not already pending */
78         if (!irq_work_claim(work))
79                 return false;
80
81         if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL) && !(work->flags & IRQ_WORK_HARD_IRQ))
82                 list = &per_cpu(lazy_list, cpu);
83         else
84                 list = &per_cpu(raised_list, cpu);
85
86         if (llist_add(&work->llnode, list))
87                 arch_send_call_function_single_ipi(cpu);
88
89         return true;
90 }
91 EXPORT_SYMBOL_GPL(irq_work_queue_on);
92 #endif
93
94 /* Enqueue the irq work @work on the current CPU */
95 bool irq_work_queue(struct irq_work *work)
96 {
97         struct llist_head *list;
98         bool lazy_work, realtime = IS_ENABLED(CONFIG_PREEMPT_RT_FULL);
99
100         /* Only queue if not already pending */
101         if (!irq_work_claim(work))
102                 return false;
103
104         /* Queue the entry and raise the IPI if needed. */
105         preempt_disable();
106
107         lazy_work = work->flags & IRQ_WORK_LAZY;
108
109         if (lazy_work || (realtime && !(work->flags & IRQ_WORK_HARD_IRQ)))
110                 list = this_cpu_ptr(&lazy_list);
111         else
112                 list = this_cpu_ptr(&raised_list);
113
114         if (llist_add(&work->llnode, list)) {
115                 if (!lazy_work || tick_nohz_tick_stopped())
116                         arch_irq_work_raise();
117         }
118
119         preempt_enable();
120
121         return true;
122 }
123 EXPORT_SYMBOL_GPL(irq_work_queue);
124
125 bool irq_work_needs_cpu(void)
126 {
127         struct llist_head *raised, *lazy;
128
129         raised = this_cpu_ptr(&raised_list);
130         lazy = this_cpu_ptr(&lazy_list);
131
132         if (llist_empty(raised) && llist_empty(lazy))
133                 return false;
134
135         /* All work should have been flushed before going offline */
136         WARN_ON_ONCE(cpu_is_offline(smp_processor_id()));
137
138         return true;
139 }
140
141 static void irq_work_run_list(struct llist_head *list)
142 {
143         unsigned long flags;
144         struct irq_work *work;
145         struct llist_node *llnode;
146
147         BUG_ON_NONRT(!irqs_disabled());
148
149         if (llist_empty(list))
150                 return;
151
152         llnode = llist_del_all(list);
153         while (llnode != NULL) {
154                 work = llist_entry(llnode, struct irq_work, llnode);
155
156                 llnode = llist_next(llnode);
157
158                 /*
159                  * Clear the PENDING bit, after this point the @work
160                  * can be re-used.
161                  * Make it immediately visible so that other CPUs trying
162                  * to claim that work don't rely on us to handle their data
163                  * while we are in the middle of the func.
164                  */
165                 flags = work->flags & ~IRQ_WORK_PENDING;
166                 xchg(&work->flags, flags);
167
168                 work->func(work);
169                 /*
170                  * Clear the BUSY bit and return to the free state if
171                  * no-one else claimed it meanwhile.
172                  */
173                 (void)cmpxchg(&work->flags, flags, flags & ~IRQ_WORK_BUSY);
174         }
175 }
176
177 /*
178  * hotplug calls this through:
179  *  hotplug_cfd() -> flush_smp_call_function_queue()
180  */
181 void irq_work_run(void)
182 {
183         irq_work_run_list(this_cpu_ptr(&raised_list));
184         if (IS_ENABLED(CONFIG_PREEMPT_RT_FULL)) {
185                 /*
186                  * NOTE: we raise softirq via IPI for safety,
187                  * and execute in irq_work_tick() to move the
188                  * overhead from hard to soft irq context.
189                  */
190                 if (!llist_empty(this_cpu_ptr(&lazy_list)))
191                         raise_softirq(TIMER_SOFTIRQ);
192         } else
193                 irq_work_run_list(this_cpu_ptr(&lazy_list));
194 }
195 EXPORT_SYMBOL_GPL(irq_work_run);
196
197 void irq_work_tick(void)
198 {
199         struct llist_head *raised = this_cpu_ptr(&raised_list);
200
201         if (!llist_empty(raised) && !arch_irq_work_has_interrupt())
202                 irq_work_run_list(raised);
203         irq_work_run_list(this_cpu_ptr(&lazy_list));
204 }
205
206 /*
207  * Synchronize against the irq_work @entry, ensures the entry is not
208  * currently in use.
209  */
210 void irq_work_sync(struct irq_work *work)
211 {
212         WARN_ON_ONCE(irqs_disabled());
213
214         while (work->flags & IRQ_WORK_BUSY)
215                 cpu_relax();
216 }
217 EXPORT_SYMBOL_GPL(irq_work_sync);