blob: 2674354a5cb8100383f9bbc6efab29c2c4ab6e0d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/softirq.c
3 *
4 * Copyright (C) 1992 Linus Torvalds
5 *
Pavel Machekb10db7f2008-01-30 13:30:00 +01006 * Distribute under GPLv2.
7 *
8 * Rewritten. Old one was good in 2.2, but in 2.3 it was immoral. --ANK (990903)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
Joe Perches40322762014-01-27 17:07:15 -080011#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
Paul Gortmaker9984de12011-05-23 14:51:41 -040013#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel_stat.h>
15#include <linux/interrupt.h>
16#include <linux/init.h>
17#include <linux/mm.h>
18#include <linux/notifier.h>
19#include <linux/percpu.h>
20#include <linux/cpu.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070021#include <linux/freezer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/kthread.h>
23#include <linux/rcupdate.h>
Steven Rostedt7e49fcc2009-01-22 19:01:40 -050024#include <linux/ftrace.h>
Andrew Morton78eef012006-03-22 00:08:16 -080025#include <linux/smp.h>
Thomas Gleixner3e339b52012-07-16 10:42:37 +000026#include <linux/smpboot.h>
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -080027#include <linux/tick.h>
Heiko Carstensa0e39ed2009-04-29 13:51:39 +020028
29#define CREATE_TRACE_POINTS
Steven Rostedtad8d75f2009-04-14 19:39:12 -040030#include <trace/events/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032/*
33 - No shared variables, all the data are CPU local.
34 - If a softirq needs serialization, let it serialize itself
35 by its own spinlocks.
36 - Even if softirq is serialized, only local cpu is marked for
37 execution. Hence, we get something sort of weak cpu binding.
38 Though it is still not clear, will it result in better locality
39 or will not.
40
41 Examples:
42 - NET RX softirq. It is multithreaded and does not require
43 any global serialization.
44 - NET TX softirq. It kicks software netdevice queues, hence
45 it is logically serialized per device, but this serialization
46 is invisible to common code.
47 - Tasklets: serialized wrt itself.
48 */
49
50#ifndef __ARCH_IRQ_STAT
51irq_cpustat_t irq_stat[NR_CPUS] ____cacheline_aligned;
52EXPORT_SYMBOL(irq_stat);
53#endif
54
Alexey Dobriyan978b0112008-09-06 20:04:36 +020055static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Venkatesh Pallipadi4dd53d82010-12-21 17:09:00 -080057DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Jason Baron5d592b42009-03-12 14:33:36 -040059char *softirq_to_name[NR_SOFTIRQS] = {
Li Zefan5dd4de52009-09-17 17:38:32 +080060 "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
Shaohua Li09223372011-06-14 13:26:25 +080061 "TASKLET", "SCHED", "HRTIMER", "RCU"
Jason Baron5d592b42009-03-12 14:33:36 -040062};
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
65 * we cannot loop indefinitely here to avoid userspace starvation,
66 * but we also don't want to introduce a worst case 1/HZ latency
67 * to the pending events, so lets the scheduler to balance
68 * the softirq load for us.
69 */
Thomas Gleixner676cb022009-07-20 23:33:49 +020070static void wakeup_softirqd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 /* Interrupts are disabled: no need to stop preemption */
Christoph Lameter909ea962010-12-08 16:22:55 +010073 struct task_struct *tsk = __this_cpu_read(ksoftirqd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (tsk && tsk->state != TASK_RUNNING)
76 wake_up_process(tsk);
77}
78
79/*
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -070080 * preempt_count and SOFTIRQ_OFFSET usage:
81 * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
82 * softirq processing.
83 * - preempt_count is changed by SOFTIRQ_DISABLE_OFFSET (= 2 * SOFTIRQ_OFFSET)
84 * on local_bh_disable or local_bh_enable.
85 * This lets us distinguish between whether we are currently processing
86 * softirq and whether we just have bh disabled.
87 */
88
89/*
Ingo Molnarde30a2b2006-07-03 00:24:42 -070090 * This one is for softirq.c-internal use,
91 * where hardirqs are disabled legitimately:
92 */
Tim Chen3c829c32006-07-30 03:04:02 -070093#ifdef CONFIG_TRACE_IRQFLAGS
Peter Zijlstra0bd3a172013-11-19 16:13:38 +010094void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
Ingo Molnarde30a2b2006-07-03 00:24:42 -070095{
96 unsigned long flags;
97
98 WARN_ON_ONCE(in_irq());
99
100 raw_local_irq_save(flags);
Steven Rostedt7e49fcc2009-01-22 19:01:40 -0500101 /*
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200102 * The preempt tracer hooks into preempt_count_add and will break
Steven Rostedt7e49fcc2009-01-22 19:01:40 -0500103 * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET
104 * is set and before current->softirq_enabled is cleared.
105 * We must manually increment preempt_count here and manually
106 * call the trace_preempt_off later.
107 */
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200108 __preempt_count_add(cnt);
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700109 /*
110 * Were softirqs turned off above:
111 */
Peter Zijlstra9ea4c382013-11-19 16:13:38 +0100112 if (softirq_count() == (cnt & SOFTIRQ_MASK))
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700113 trace_softirqs_off(ip);
114 raw_local_irq_restore(flags);
Steven Rostedt7e49fcc2009-01-22 19:01:40 -0500115
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700116 if (preempt_count() == cnt)
Steven Rostedt7e49fcc2009-01-22 19:01:40 -0500117 trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700118}
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100119EXPORT_SYMBOL(__local_bh_disable_ip);
Tim Chen3c829c32006-07-30 03:04:02 -0700120#endif /* CONFIG_TRACE_IRQFLAGS */
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700121
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700122static void __local_bh_enable(unsigned int cnt)
123{
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700124 WARN_ON_ONCE(!irqs_disabled());
125
Peter Zijlstra9ea4c382013-11-19 16:13:38 +0100126 if (softirq_count() == (cnt & SOFTIRQ_MASK))
Davidlohr Buesod2e08472013-04-30 11:46:09 -0700127 trace_softirqs_on(_RET_IP_);
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200128 preempt_count_sub(cnt);
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700129}
130
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700131/*
132 * Special-case - softirqs can safely be enabled in
133 * cond_resched_softirq(), or by __do_softirq(),
134 * without processing still-pending softirqs:
135 */
136void _local_bh_enable(void)
137{
Frederic Weisbecker5d60d3e2013-09-24 04:11:35 +0200138 WARN_ON_ONCE(in_irq());
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700139 __local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700140}
141
142EXPORT_SYMBOL(_local_bh_enable);
143
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100144void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700145{
Johannes Berg0f476b6d2008-06-18 09:29:37 +0200146 WARN_ON_ONCE(in_irq() || irqs_disabled());
Tim Chen3c829c32006-07-30 03:04:02 -0700147#ifdef CONFIG_TRACE_IRQFLAGS
Johannes Berg0f476b6d2008-06-18 09:29:37 +0200148 local_irq_disable();
Tim Chen3c829c32006-07-30 03:04:02 -0700149#endif
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700150 /*
151 * Are softirqs going to be turned on now:
152 */
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700153 if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700154 trace_softirqs_on(ip);
155 /*
156 * Keep preemption disabled until we are done with
157 * softirq processing:
158 */
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100159 preempt_count_sub(cnt - 1);
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700160
Frederic Weisbecker0bed6982013-09-05 16:14:00 +0200161 if (unlikely(!in_interrupt() && local_softirq_pending())) {
162 /*
163 * Run softirq if any pending. And do it in its own stack
164 * as we may be calling this deep in a task call stack already.
165 */
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700166 do_softirq();
Frederic Weisbecker0bed6982013-09-05 16:14:00 +0200167 }
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700168
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200169 preempt_count_dec();
Tim Chen3c829c32006-07-30 03:04:02 -0700170#ifdef CONFIG_TRACE_IRQFLAGS
Johannes Berg0f476b6d2008-06-18 09:29:37 +0200171 local_irq_enable();
Tim Chen3c829c32006-07-30 03:04:02 -0700172#endif
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700173 preempt_check_resched();
174}
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100175EXPORT_SYMBOL(__local_bh_enable_ip);
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700176
177/*
Ben Greear34376a52013-06-06 14:29:49 -0700178 * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
179 * but break the loop if need_resched() is set or after 2 ms.
180 * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
181 * certain cases, such as stop_machine(), jiffies may cease to
182 * increment and so we need the MAX_SOFTIRQ_RESTART limit as
183 * well to make sure we eventually return from this method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 *
Eric Dumazetc10d7362013-01-10 15:26:34 -0800185 * These limits have been established via experimentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * The two things to balance is latency against fairness -
187 * we want to handle softirqs as soon as possible, but they
188 * should not be able to lock up the box.
189 */
Eric Dumazetc10d7362013-01-10 15:26:34 -0800190#define MAX_SOFTIRQ_TIME msecs_to_jiffies(2)
Ben Greear34376a52013-06-06 14:29:49 -0700191#define MAX_SOFTIRQ_RESTART 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100193#ifdef CONFIG_TRACE_IRQFLAGS
194/*
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100195 * When we run softirqs from irq_exit() and thus on the hardirq stack we need
196 * to keep the lockdep irq context tracking as tight as possible in order to
197 * not miss-qualify lock contexts and miss possible deadlocks.
198 */
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100199
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100200static inline bool lockdep_softirq_start(void)
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100201{
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100202 bool in_hardirq = false;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100203
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100204 if (trace_hardirq_context(current)) {
205 in_hardirq = true;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100206 trace_hardirq_exit();
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100207 }
208
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100209 lockdep_softirq_enter();
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100210
211 return in_hardirq;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100212}
213
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100214static inline void lockdep_softirq_end(bool in_hardirq)
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100215{
216 lockdep_softirq_exit();
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100217
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100218 if (in_hardirq)
219 trace_hardirq_enter();
220}
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100221#else
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100222static inline bool lockdep_softirq_start(void) { return false; }
223static inline void lockdep_softirq_end(bool in_hardirq) { }
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100224#endif
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226asmlinkage void __do_softirq(void)
227{
Eric Dumazetc10d7362013-01-10 15:26:34 -0800228 unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
Mel Gorman907aed42012-07-31 16:44:07 -0700229 unsigned long old_flags = current->flags;
Ben Greear34376a52013-06-06 14:29:49 -0700230 int max_restart = MAX_SOFTIRQ_RESTART;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100231 struct softirq_action *h;
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100232 bool in_hardirq;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100233 __u32 pending;
Joe Perches2e702b92014-01-27 17:07:14 -0800234 int softirq_bit;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100235 int cpu;
Mel Gorman907aed42012-07-31 16:44:07 -0700236
237 /*
238 * Mask out PF_MEMALLOC s current task context is borrowed for the
239 * softirq. A softirq handled such as network RX might set PF_MEMALLOC
240 * again if the socket is related to swap
241 */
242 current->flags &= ~PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 pending = local_softirq_pending();
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100245 account_irq_enter_time(current);
Paul Mackerras829035fd2006-07-03 00:25:40 -0700246
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100247 __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100248 in_hardirq = lockdep_softirq_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 cpu = smp_processor_id();
251restart:
252 /* Reset the pending bitmask before enabling irqs */
Andi Kleen3f744782005-09-12 18:49:24 +0200253 set_softirq_pending(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Andrew Mortonc70f5d62005-07-30 10:22:49 -0700255 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 h = softirq_vec;
258
Joe Perches2e702b92014-01-27 17:07:14 -0800259 while ((softirq_bit = ffs(pending))) {
260 unsigned int vec_nr;
261 int prev_count;
Thomas Gleixner8e85b4b2008-10-02 10:50:53 +0200262
Joe Perches2e702b92014-01-27 17:07:14 -0800263 h += softirq_bit - 1;
Thomas Gleixnerf4bc6bb2010-10-19 15:00:13 +0200264
Joe Perches2e702b92014-01-27 17:07:14 -0800265 vec_nr = h - softirq_vec;
266 prev_count = preempt_count();
Thomas Gleixner8e85b4b2008-10-02 10:50:53 +0200267
Joe Perches2e702b92014-01-27 17:07:14 -0800268 kstat_incr_softirqs_this_cpu(vec_nr);
269
270 trace_softirq_entry(vec_nr);
271 h->action(h);
272 trace_softirq_exit(vec_nr);
273 if (unlikely(prev_count != preempt_count())) {
Joe Perches40322762014-01-27 17:07:15 -0800274 pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
Joe Perches2e702b92014-01-27 17:07:14 -0800275 vec_nr, softirq_to_name[vec_nr], h->action,
276 prev_count, preempt_count());
277 preempt_count_set(prev_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Joe Perches2e702b92014-01-27 17:07:14 -0800279 rcu_bh_qs(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 h++;
Joe Perches2e702b92014-01-27 17:07:14 -0800281 pending >>= softirq_bit;
282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Andrew Mortonc70f5d62005-07-30 10:22:49 -0700284 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286 pending = local_softirq_pending();
Eric Dumazetc10d7362013-01-10 15:26:34 -0800287 if (pending) {
Ben Greear34376a52013-06-06 14:29:49 -0700288 if (time_before(jiffies, end) && !need_resched() &&
289 --max_restart)
Eric Dumazetc10d7362013-01-10 15:26:34 -0800290 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 wakeup_softirqd();
Eric Dumazetc10d7362013-01-10 15:26:34 -0800293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100295 lockdep_softirq_end(in_hardirq);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100296 account_irq_exit_time(current);
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700297 __local_bh_enable(SOFTIRQ_OFFSET);
Frederic Weisbecker5d60d3e2013-09-24 04:11:35 +0200298 WARN_ON_ONCE(in_interrupt());
Mel Gorman907aed42012-07-31 16:44:07 -0700299 tsk_restore_flags(current, old_flags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302asmlinkage void do_softirq(void)
303{
304 __u32 pending;
305 unsigned long flags;
306
307 if (in_interrupt())
308 return;
309
310 local_irq_save(flags);
311
312 pending = local_softirq_pending();
313
314 if (pending)
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200315 do_softirq_own_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 local_irq_restore(flags);
318}
319
Ingo Molnardde4b2b2007-02-16 01:27:45 -0800320/*
321 * Enter an interrupt context.
322 */
323void irq_enter(void)
324{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100325 rcu_irq_enter();
Frederic Weisbecker0a8a2e72012-01-24 18:59:44 +0100326 if (is_idle_task(current) && !in_interrupt()) {
Venkatesh Pallipadid267f872010-10-04 17:03:23 -0700327 /*
328 * Prevent raise_softirq from needlessly waking up ksoftirqd
329 * here, as softirq will be serviced on return from interrupt.
330 */
331 local_bh_disable();
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200332 tick_check_idle();
Venkatesh Pallipadid267f872010-10-04 17:03:23 -0700333 _local_bh_enable();
334 }
335
336 __irq_enter();
Ingo Molnardde4b2b2007-02-16 01:27:45 -0800337}
338
Heiko Carstensb2a00172012-03-05 15:07:25 -0800339static inline void invoke_softirq(void)
340{
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200341 if (!force_irqthreads) {
Frederic Weisbeckercc1f0272013-09-24 17:17:47 +0200342#ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200343 /*
344 * We can safely execute softirq on the current stack if
345 * it is the irq stack, because it should be near empty
Frederic Weisbeckercc1f0272013-09-24 17:17:47 +0200346 * at this stage.
347 */
348 __do_softirq();
349#else
350 /*
351 * Otherwise, irq_exit() is called on the task stack that can
352 * be potentially deep already. So call softirq in its own stack
353 * to prevent from any overrun.
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200354 */
Frederic Weisbeckerbe6e1012013-09-24 16:39:41 +0200355 do_softirq_own_stack();
Frederic Weisbeckercc1f0272013-09-24 17:17:47 +0200356#endif
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200357 } else {
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000358 wakeup_softirqd();
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200359 }
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Frederic Weisbecker67826ea2013-04-20 17:43:13 +0200362static inline void tick_irq_exit(void)
363{
364#ifdef CONFIG_NO_HZ_COMMON
365 int cpu = smp_processor_id();
366
367 /* Make sure that timer wheel updates are propagated */
368 if ((idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
369 if (!in_interrupt())
370 tick_nohz_irq_exit();
371 }
372#endif
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Exit an interrupt context. Process softirqs if needed and possible:
377 */
378void irq_exit(void)
379{
Thomas Gleixner74eed012013-02-20 22:00:48 +0100380#ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
Frederic Weisbecker4cd5d112013-02-28 20:00:43 +0100381 local_irq_disable();
Thomas Gleixner74eed012013-02-20 22:00:48 +0100382#else
383 WARN_ON_ONCE(!irqs_disabled());
384#endif
385
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100386 account_irq_exit_time(current);
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200387 preempt_count_sub(HARDIRQ_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (!in_interrupt() && local_softirq_pending())
389 invoke_softirq();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800390
Frederic Weisbecker67826ea2013-04-20 17:43:13 +0200391 tick_irq_exit();
Frederic Weisbecker416eb332011-10-07 16:31:02 -0700392 rcu_irq_exit();
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100393 trace_hardirq_exit(); /* must be last! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396/*
397 * This function must run with irqs disabled!
398 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800399inline void raise_softirq_irqoff(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 __raise_softirq_irqoff(nr);
402
403 /*
404 * If we're in an interrupt or softirq, we're done
405 * (this also catches softirq-disabled code). We will
406 * actually run the softirq once we return from
407 * the irq or softirq.
408 *
409 * Otherwise we wake up ksoftirqd to make sure we
410 * schedule the softirq soon.
411 */
412 if (!in_interrupt())
413 wakeup_softirqd();
414}
415
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800416void raise_softirq(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 unsigned long flags;
419
420 local_irq_save(flags);
421 raise_softirq_irqoff(nr);
422 local_irq_restore(flags);
423}
424
Steven Rostedtf0696862012-01-25 20:18:55 -0500425void __raise_softirq_irqoff(unsigned int nr)
426{
427 trace_softirq_raise(nr);
428 or_softirq_pending(1UL << nr);
429}
430
Carlos R. Mafra962cf362008-05-15 11:15:37 -0300431void open_softirq(int nr, void (*action)(struct softirq_action *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 softirq_vec[nr].action = action;
434}
435
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200436/*
437 * Tasklets
438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439struct tasklet_head
440{
Olof Johansson48f20a92008-03-04 15:23:25 -0800441 struct tasklet_struct *head;
442 struct tasklet_struct **tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443};
444
Vegard Nossum4620b492008-06-12 23:21:53 +0200445static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
446static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800448void __tasklet_schedule(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 unsigned long flags;
451
452 local_irq_save(flags);
Olof Johansson48f20a92008-03-04 15:23:25 -0800453 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100454 *__this_cpu_read(tasklet_vec.tail) = t;
455 __this_cpu_write(tasklet_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 raise_softirq_irqoff(TASKLET_SOFTIRQ);
457 local_irq_restore(flags);
458}
459
460EXPORT_SYMBOL(__tasklet_schedule);
461
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800462void __tasklet_hi_schedule(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 unsigned long flags;
465
466 local_irq_save(flags);
Olof Johansson48f20a92008-03-04 15:23:25 -0800467 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100468 *__this_cpu_read(tasklet_hi_vec.tail) = t;
469 __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 raise_softirq_irqoff(HI_SOFTIRQ);
471 local_irq_restore(flags);
472}
473
474EXPORT_SYMBOL(__tasklet_hi_schedule);
475
Vegard Nossum7c692cb2008-05-21 22:53:13 +0200476void __tasklet_hi_schedule_first(struct tasklet_struct *t)
477{
478 BUG_ON(!irqs_disabled());
479
Christoph Lameter909ea962010-12-08 16:22:55 +0100480 t->next = __this_cpu_read(tasklet_hi_vec.head);
481 __this_cpu_write(tasklet_hi_vec.head, t);
Vegard Nossum7c692cb2008-05-21 22:53:13 +0200482 __raise_softirq_irqoff(HI_SOFTIRQ);
483}
484
485EXPORT_SYMBOL(__tasklet_hi_schedule_first);
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static void tasklet_action(struct softirq_action *a)
488{
489 struct tasklet_struct *list;
490
491 local_irq_disable();
Christoph Lameter909ea962010-12-08 16:22:55 +0100492 list = __this_cpu_read(tasklet_vec.head);
493 __this_cpu_write(tasklet_vec.head, NULL);
494 __this_cpu_write(tasklet_vec.tail, &__get_cpu_var(tasklet_vec).head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 local_irq_enable();
496
497 while (list) {
498 struct tasklet_struct *t = list;
499
500 list = list->next;
501
502 if (tasklet_trylock(t)) {
503 if (!atomic_read(&t->count)) {
504 if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
505 BUG();
506 t->func(t->data);
507 tasklet_unlock(t);
508 continue;
509 }
510 tasklet_unlock(t);
511 }
512
513 local_irq_disable();
Olof Johansson48f20a92008-03-04 15:23:25 -0800514 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100515 *__this_cpu_read(tasklet_vec.tail) = t;
516 __this_cpu_write(tasklet_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 __raise_softirq_irqoff(TASKLET_SOFTIRQ);
518 local_irq_enable();
519 }
520}
521
522static void tasklet_hi_action(struct softirq_action *a)
523{
524 struct tasklet_struct *list;
525
526 local_irq_disable();
Christoph Lameter909ea962010-12-08 16:22:55 +0100527 list = __this_cpu_read(tasklet_hi_vec.head);
528 __this_cpu_write(tasklet_hi_vec.head, NULL);
529 __this_cpu_write(tasklet_hi_vec.tail, &__get_cpu_var(tasklet_hi_vec).head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 local_irq_enable();
531
532 while (list) {
533 struct tasklet_struct *t = list;
534
535 list = list->next;
536
537 if (tasklet_trylock(t)) {
538 if (!atomic_read(&t->count)) {
539 if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
540 BUG();
541 t->func(t->data);
542 tasklet_unlock(t);
543 continue;
544 }
545 tasklet_unlock(t);
546 }
547
548 local_irq_disable();
Olof Johansson48f20a92008-03-04 15:23:25 -0800549 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100550 *__this_cpu_read(tasklet_hi_vec.tail) = t;
551 __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 __raise_softirq_irqoff(HI_SOFTIRQ);
553 local_irq_enable();
554 }
555}
556
557
558void tasklet_init(struct tasklet_struct *t,
559 void (*func)(unsigned long), unsigned long data)
560{
561 t->next = NULL;
562 t->state = 0;
563 atomic_set(&t->count, 0);
564 t->func = func;
565 t->data = data;
566}
567
568EXPORT_SYMBOL(tasklet_init);
569
570void tasklet_kill(struct tasklet_struct *t)
571{
572 if (in_interrupt())
Joe Perches40322762014-01-27 17:07:15 -0800573 pr_notice("Attempt to kill tasklet from interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
H Hartley Sweeten79d381c2009-04-16 19:30:18 -0400576 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 yield();
H Hartley Sweeten79d381c2009-04-16 19:30:18 -0400578 } while (test_bit(TASKLET_STATE_SCHED, &t->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580 tasklet_unlock_wait(t);
581 clear_bit(TASKLET_STATE_SCHED, &t->state);
582}
583
584EXPORT_SYMBOL(tasklet_kill);
585
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200586/*
587 * tasklet_hrtimer
588 */
589
590/*
Peter Zijlstrab9c30322010-02-03 18:08:52 +0100591 * The trampoline is called when the hrtimer expires. It schedules a tasklet
592 * to run __tasklet_hrtimer_trampoline() which in turn will call the intended
593 * hrtimer callback, but from softirq context.
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200594 */
595static enum hrtimer_restart __hrtimer_tasklet_trampoline(struct hrtimer *timer)
596{
597 struct tasklet_hrtimer *ttimer =
598 container_of(timer, struct tasklet_hrtimer, timer);
599
Peter Zijlstrab9c30322010-02-03 18:08:52 +0100600 tasklet_hi_schedule(&ttimer->tasklet);
601 return HRTIMER_NORESTART;
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200602}
603
604/*
605 * Helper function which calls the hrtimer callback from
606 * tasklet/softirq context
607 */
608static void __tasklet_hrtimer_trampoline(unsigned long data)
609{
610 struct tasklet_hrtimer *ttimer = (void *)data;
611 enum hrtimer_restart restart;
612
613 restart = ttimer->function(&ttimer->timer);
614 if (restart != HRTIMER_NORESTART)
615 hrtimer_restart(&ttimer->timer);
616}
617
618/**
619 * tasklet_hrtimer_init - Init a tasklet/hrtimer combo for softirq callbacks
620 * @ttimer: tasklet_hrtimer which is initialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300621 * @function: hrtimer callback function which gets called from softirq context
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200622 * @which_clock: clock id (CLOCK_MONOTONIC/CLOCK_REALTIME)
623 * @mode: hrtimer mode (HRTIMER_MODE_ABS/HRTIMER_MODE_REL)
624 */
625void tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
626 enum hrtimer_restart (*function)(struct hrtimer *),
627 clockid_t which_clock, enum hrtimer_mode mode)
628{
629 hrtimer_init(&ttimer->timer, which_clock, mode);
630 ttimer->timer.function = __hrtimer_tasklet_trampoline;
631 tasklet_init(&ttimer->tasklet, __tasklet_hrtimer_trampoline,
632 (unsigned long)ttimer);
633 ttimer->function = function;
634}
635EXPORT_SYMBOL_GPL(tasklet_hrtimer_init);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637void __init softirq_init(void)
638{
Olof Johansson48f20a92008-03-04 15:23:25 -0800639 int cpu;
640
641 for_each_possible_cpu(cpu) {
642 per_cpu(tasklet_vec, cpu).tail =
643 &per_cpu(tasklet_vec, cpu).head;
644 per_cpu(tasklet_hi_vec, cpu).tail =
645 &per_cpu(tasklet_hi_vec, cpu).head;
646 }
647
Carlos R. Mafra962cf362008-05-15 11:15:37 -0300648 open_softirq(TASKLET_SOFTIRQ, tasklet_action);
649 open_softirq(HI_SOFTIRQ, tasklet_hi_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000652static int ksoftirqd_should_run(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000654 return local_softirq_pending();
655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000657static void run_ksoftirqd(unsigned int cpu)
658{
659 local_irq_disable();
660 if (local_softirq_pending()) {
Frederic Weisbecker0bed6982013-09-05 16:14:00 +0200661 /*
662 * We can safely run softirq on inline stack, as we are not deep
663 * in the task stack here.
664 */
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000665 __do_softirq();
666 rcu_note_context_switch(cpu);
667 local_irq_enable();
668 cond_resched();
669 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000671 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674#ifdef CONFIG_HOTPLUG_CPU
675/*
676 * tasklet_kill_immediate is called to remove a tasklet which can already be
677 * scheduled for execution on @cpu.
678 *
679 * Unlike tasklet_kill, this function removes the tasklet
680 * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
681 *
682 * When this function is called, @cpu must be in the CPU_DEAD state.
683 */
684void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
685{
686 struct tasklet_struct **i;
687
688 BUG_ON(cpu_online(cpu));
689 BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
690
691 if (!test_bit(TASKLET_STATE_SCHED, &t->state))
692 return;
693
694 /* CPU is dead, so no lock needed. */
Olof Johansson48f20a92008-03-04 15:23:25 -0800695 for (i = &per_cpu(tasklet_vec, cpu).head; *i; i = &(*i)->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (*i == t) {
697 *i = t->next;
Olof Johansson48f20a92008-03-04 15:23:25 -0800698 /* If this was the tail element, move the tail ptr */
699 if (*i == NULL)
700 per_cpu(tasklet_vec, cpu).tail = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return;
702 }
703 }
704 BUG();
705}
706
707static void takeover_tasklets(unsigned int cpu)
708{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 /* CPU is dead, so no lock needed. */
710 local_irq_disable();
711
712 /* Find end, append list for that CPU. */
Christian Borntraegere5e41722008-05-01 04:34:23 -0700713 if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100714 *__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
715 this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
Christian Borntraegere5e41722008-05-01 04:34:23 -0700716 per_cpu(tasklet_vec, cpu).head = NULL;
717 per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 raise_softirq_irqoff(TASKLET_SOFTIRQ);
720
Christian Borntraegere5e41722008-05-01 04:34:23 -0700721 if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100722 *__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
723 __this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
Christian Borntraegere5e41722008-05-01 04:34:23 -0700724 per_cpu(tasklet_hi_vec, cpu).head = NULL;
725 per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 raise_softirq_irqoff(HI_SOFTIRQ);
728
729 local_irq_enable();
730}
731#endif /* CONFIG_HOTPLUG_CPU */
732
Paul Gortmaker0db06282013-06-19 14:53:51 -0400733static int cpu_callback(struct notifier_block *nfb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned long action,
735 void *hcpu)
736{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 case CPU_DEAD:
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000740 case CPU_DEAD_FROZEN:
741 takeover_tasklets((unsigned long)hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 break;
743#endif /* CONFIG_HOTPLUG_CPU */
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return NOTIFY_OK;
746}
747
Paul Gortmaker0db06282013-06-19 14:53:51 -0400748static struct notifier_block cpu_nfb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .notifier_call = cpu_callback
750};
751
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000752static struct smp_hotplug_thread softirq_threads = {
753 .store = &ksoftirqd,
754 .thread_should_run = ksoftirqd_should_run,
755 .thread_fn = run_ksoftirqd,
756 .thread_comm = "ksoftirqd/%u",
757};
758
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -0700759static __init int spawn_ksoftirqd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 register_cpu_notifier(&cpu_nfb);
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000762
763 BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 return 0;
766}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -0700767early_initcall(spawn_ksoftirqd);
Andrew Morton78eef012006-03-22 00:08:16 -0800768
Yinghai Lu43a25632008-12-28 16:01:13 -0800769/*
770 * [ These __weak aliases are kept in a separate compilation unit, so that
771 * GCC does not inline them incorrectly. ]
772 */
773
774int __init __weak early_irq_init(void)
775{
776 return 0;
777}
778
Yinghai Lu4a046d12009-01-12 17:39:24 -0800779int __init __weak arch_probe_nr_irqs(void)
780{
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200781 return NR_IRQS_LEGACY;
Yinghai Lu4a046d12009-01-12 17:39:24 -0800782}
783
Yinghai Lu43a25632008-12-28 16:01:13 -0800784int __init __weak arch_early_irq_init(void)
785{
786 return 0;
787}