blob: 850967068aaf3ff0711587d1624ac353216c4e2e [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
Joe Perchesce85b4f2014-01-27 17:07:16 -080059const char * const 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}
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700141EXPORT_SYMBOL(_local_bh_enable);
142
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100143void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700144{
Johannes Berg0f476b6d2008-06-18 09:29:37 +0200145 WARN_ON_ONCE(in_irq() || irqs_disabled());
Tim Chen3c829c32006-07-30 03:04:02 -0700146#ifdef CONFIG_TRACE_IRQFLAGS
Johannes Berg0f476b6d2008-06-18 09:29:37 +0200147 local_irq_disable();
Tim Chen3c829c32006-07-30 03:04:02 -0700148#endif
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700149 /*
150 * Are softirqs going to be turned on now:
151 */
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700152 if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700153 trace_softirqs_on(ip);
154 /*
155 * Keep preemption disabled until we are done with
156 * softirq processing:
Joe Perchesce85b4f2014-01-27 17:07:16 -0800157 */
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100158 preempt_count_sub(cnt - 1);
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700159
Frederic Weisbecker0bed6982013-09-05 16:14:00 +0200160 if (unlikely(!in_interrupt() && local_softirq_pending())) {
161 /*
162 * Run softirq if any pending. And do it in its own stack
163 * as we may be calling this deep in a task call stack already.
164 */
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700165 do_softirq();
Frederic Weisbecker0bed6982013-09-05 16:14:00 +0200166 }
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700167
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200168 preempt_count_dec();
Tim Chen3c829c32006-07-30 03:04:02 -0700169#ifdef CONFIG_TRACE_IRQFLAGS
Johannes Berg0f476b6d2008-06-18 09:29:37 +0200170 local_irq_enable();
Tim Chen3c829c32006-07-30 03:04:02 -0700171#endif
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700172 preempt_check_resched();
173}
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100174EXPORT_SYMBOL(__local_bh_enable_ip);
Ingo Molnarde30a2b2006-07-03 00:24:42 -0700175
176/*
Ben Greear34376a52013-06-06 14:29:49 -0700177 * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
178 * but break the loop if need_resched() is set or after 2 ms.
179 * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
180 * certain cases, such as stop_machine(), jiffies may cease to
181 * increment and so we need the MAX_SOFTIRQ_RESTART limit as
182 * well to make sure we eventually return from this method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
Eric Dumazetc10d7362013-01-10 15:26:34 -0800184 * These limits have been established via experimentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 * The two things to balance is latency against fairness -
186 * we want to handle softirqs as soon as possible, but they
187 * should not be able to lock up the box.
188 */
Eric Dumazetc10d7362013-01-10 15:26:34 -0800189#define MAX_SOFTIRQ_TIME msecs_to_jiffies(2)
Ben Greear34376a52013-06-06 14:29:49 -0700190#define MAX_SOFTIRQ_RESTART 10
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100192#ifdef CONFIG_TRACE_IRQFLAGS
193/*
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100194 * When we run softirqs from irq_exit() and thus on the hardirq stack we need
195 * to keep the lockdep irq context tracking as tight as possible in order to
196 * not miss-qualify lock contexts and miss possible deadlocks.
197 */
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100198
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100199static inline bool lockdep_softirq_start(void)
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100200{
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100201 bool in_hardirq = false;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100202
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100203 if (trace_hardirq_context(current)) {
204 in_hardirq = true;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100205 trace_hardirq_exit();
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100206 }
207
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100208 lockdep_softirq_enter();
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100209
210 return in_hardirq;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100211}
212
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100213static inline void lockdep_softirq_end(bool in_hardirq)
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100214{
215 lockdep_softirq_exit();
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100216
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100217 if (in_hardirq)
218 trace_hardirq_enter();
219}
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100220#else
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100221static inline bool lockdep_softirq_start(void) { return false; }
222static inline void lockdep_softirq_end(bool in_hardirq) { }
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100223#endif
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225asmlinkage void __do_softirq(void)
226{
Eric Dumazetc10d7362013-01-10 15:26:34 -0800227 unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
Mel Gorman907aed42012-07-31 16:44:07 -0700228 unsigned long old_flags = current->flags;
Ben Greear34376a52013-06-06 14:29:49 -0700229 int max_restart = MAX_SOFTIRQ_RESTART;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100230 struct softirq_action *h;
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100231 bool in_hardirq;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100232 __u32 pending;
Joe Perches2e702b92014-01-27 17:07:14 -0800233 int softirq_bit;
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100234 int cpu;
Mel Gorman907aed42012-07-31 16:44:07 -0700235
236 /*
237 * Mask out PF_MEMALLOC s current task context is borrowed for the
238 * softirq. A softirq handled such as network RX might set PF_MEMALLOC
239 * again if the socket is related to swap
240 */
241 current->flags &= ~PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 pending = local_softirq_pending();
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100244 account_irq_enter_time(current);
Paul Mackerras829035fd2006-07-03 00:25:40 -0700245
Peter Zijlstra0bd3a172013-11-19 16:13:38 +0100246 __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100247 in_hardirq = lockdep_softirq_start();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 cpu = smp_processor_id();
250restart:
251 /* Reset the pending bitmask before enabling irqs */
Andi Kleen3f744782005-09-12 18:49:24 +0200252 set_softirq_pending(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Andrew Mortonc70f5d62005-07-30 10:22:49 -0700254 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 h = softirq_vec;
257
Joe Perches2e702b92014-01-27 17:07:14 -0800258 while ((softirq_bit = ffs(pending))) {
259 unsigned int vec_nr;
260 int prev_count;
Thomas Gleixner8e85b4b2008-10-02 10:50:53 +0200261
Joe Perches2e702b92014-01-27 17:07:14 -0800262 h += softirq_bit - 1;
Thomas Gleixnerf4bc6bb2010-10-19 15:00:13 +0200263
Joe Perches2e702b92014-01-27 17:07:14 -0800264 vec_nr = h - softirq_vec;
265 prev_count = preempt_count();
Thomas Gleixner8e85b4b2008-10-02 10:50:53 +0200266
Joe Perches2e702b92014-01-27 17:07:14 -0800267 kstat_incr_softirqs_this_cpu(vec_nr);
268
269 trace_softirq_entry(vec_nr);
270 h->action(h);
271 trace_softirq_exit(vec_nr);
272 if (unlikely(prev_count != preempt_count())) {
Joe Perches40322762014-01-27 17:07:15 -0800273 pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
Joe Perches2e702b92014-01-27 17:07:14 -0800274 vec_nr, softirq_to_name[vec_nr], h->action,
275 prev_count, preempt_count());
276 preempt_count_set(prev_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Joe Perches2e702b92014-01-27 17:07:14 -0800278 rcu_bh_qs(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 h++;
Joe Perches2e702b92014-01-27 17:07:14 -0800280 pending >>= softirq_bit;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Andrew Mortonc70f5d62005-07-30 10:22:49 -0700283 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 pending = local_softirq_pending();
Eric Dumazetc10d7362013-01-10 15:26:34 -0800286 if (pending) {
Ben Greear34376a52013-06-06 14:29:49 -0700287 if (time_before(jiffies, end) && !need_resched() &&
288 --max_restart)
Eric Dumazetc10d7362013-01-10 15:26:34 -0800289 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 wakeup_softirqd();
Eric Dumazetc10d7362013-01-10 15:26:34 -0800292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Frederic Weisbecker5c4853b2013-11-20 01:07:34 +0100294 lockdep_softirq_end(in_hardirq);
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100295 account_irq_exit_time(current);
Venkatesh Pallipadi75e10562010-10-04 17:03:16 -0700296 __local_bh_enable(SOFTIRQ_OFFSET);
Frederic Weisbecker5d60d3e2013-09-24 04:11:35 +0200297 WARN_ON_ONCE(in_interrupt());
Mel Gorman907aed42012-07-31 16:44:07 -0700298 tsk_restore_flags(current, old_flags, PF_MEMALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301asmlinkage void do_softirq(void)
302{
303 __u32 pending;
304 unsigned long flags;
305
306 if (in_interrupt())
307 return;
308
309 local_irq_save(flags);
310
311 pending = local_softirq_pending();
312
313 if (pending)
Frederic Weisbecker7d65f4a2013-09-05 15:49:45 +0200314 do_softirq_own_stack();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 local_irq_restore(flags);
317}
318
Ingo Molnardde4b2b2007-02-16 01:27:45 -0800319/*
320 * Enter an interrupt context.
321 */
322void irq_enter(void)
323{
Paul E. McKenney64db4cf2008-12-18 21:55:32 +0100324 rcu_irq_enter();
Frederic Weisbecker0a8a2e72012-01-24 18:59:44 +0100325 if (is_idle_task(current) && !in_interrupt()) {
Venkatesh Pallipadid267f872010-10-04 17:03:23 -0700326 /*
327 * Prevent raise_softirq from needlessly waking up ksoftirqd
328 * here, as softirq will be serviced on return from interrupt.
329 */
330 local_bh_disable();
Frederic Weisbeckere8fcaa52013-08-07 22:28:01 +0200331 tick_check_idle();
Venkatesh Pallipadid267f872010-10-04 17:03:23 -0700332 _local_bh_enable();
333 }
334
335 __irq_enter();
Ingo Molnardde4b2b2007-02-16 01:27:45 -0800336}
337
Heiko Carstensb2a00172012-03-05 15:07:25 -0800338static inline void invoke_softirq(void)
339{
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200340 if (!force_irqthreads) {
Frederic Weisbeckercc1f0272013-09-24 17:17:47 +0200341#ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200342 /*
343 * We can safely execute softirq on the current stack if
344 * it is the irq stack, because it should be near empty
Frederic Weisbeckercc1f0272013-09-24 17:17:47 +0200345 * at this stage.
346 */
347 __do_softirq();
348#else
349 /*
350 * Otherwise, irq_exit() is called on the task stack that can
351 * be potentially deep already. So call softirq in its own stack
352 * to prevent from any overrun.
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200353 */
Frederic Weisbeckerbe6e1012013-09-24 16:39:41 +0200354 do_softirq_own_stack();
Frederic Weisbeckercc1f0272013-09-24 17:17:47 +0200355#endif
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200356 } else {
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000357 wakeup_softirqd();
Frederic Weisbeckerded79752013-09-24 00:50:25 +0200358 }
Thomas Gleixner8d32a302011-02-23 23:52:23 +0000359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Frederic Weisbecker67826ea2013-04-20 17:43:13 +0200361static inline void tick_irq_exit(void)
362{
363#ifdef CONFIG_NO_HZ_COMMON
364 int cpu = smp_processor_id();
365
366 /* Make sure that timer wheel updates are propagated */
367 if ((idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
368 if (!in_interrupt())
369 tick_nohz_irq_exit();
370 }
371#endif
372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374/*
375 * Exit an interrupt context. Process softirqs if needed and possible:
376 */
377void irq_exit(void)
378{
Thomas Gleixner74eed012013-02-20 22:00:48 +0100379#ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
Frederic Weisbecker4cd5d112013-02-28 20:00:43 +0100380 local_irq_disable();
Thomas Gleixner74eed012013-02-20 22:00:48 +0100381#else
382 WARN_ON_ONCE(!irqs_disabled());
383#endif
384
Frederic Weisbecker6a616712012-12-16 20:00:34 +0100385 account_irq_exit_time(current);
Peter Zijlstrabdb43802013-09-10 12:15:23 +0200386 preempt_count_sub(HARDIRQ_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (!in_interrupt() && local_softirq_pending())
388 invoke_softirq();
Thomas Gleixner79bf2bb2007-02-16 01:28:03 -0800389
Frederic Weisbecker67826ea2013-04-20 17:43:13 +0200390 tick_irq_exit();
Frederic Weisbecker416eb332011-10-07 16:31:02 -0700391 rcu_irq_exit();
Peter Zijlstraf1a83e62013-11-19 16:42:47 +0100392 trace_hardirq_exit(); /* must be last! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395/*
396 * This function must run with irqs disabled!
397 */
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800398inline void raise_softirq_irqoff(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 __raise_softirq_irqoff(nr);
401
402 /*
403 * If we're in an interrupt or softirq, we're done
404 * (this also catches softirq-disabled code). We will
405 * actually run the softirq once we return from
406 * the irq or softirq.
407 *
408 * Otherwise we wake up ksoftirqd to make sure we
409 * schedule the softirq soon.
410 */
411 if (!in_interrupt())
412 wakeup_softirqd();
413}
414
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800415void raise_softirq(unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 unsigned long flags;
418
419 local_irq_save(flags);
420 raise_softirq_irqoff(nr);
421 local_irq_restore(flags);
422}
423
Steven Rostedtf0696862012-01-25 20:18:55 -0500424void __raise_softirq_irqoff(unsigned int nr)
425{
426 trace_softirq_raise(nr);
427 or_softirq_pending(1UL << nr);
428}
429
Carlos R. Mafra962cf362008-05-15 11:15:37 -0300430void open_softirq(int nr, void (*action)(struct softirq_action *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 softirq_vec[nr].action = action;
433}
434
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200435/*
436 * Tasklets
437 */
Joe Perchesce85b4f2014-01-27 17:07:16 -0800438struct tasklet_head {
Olof Johansson48f20a92008-03-04 15:23:25 -0800439 struct tasklet_struct *head;
440 struct tasklet_struct **tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441};
442
Vegard Nossum4620b492008-06-12 23:21:53 +0200443static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
444static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800446void __tasklet_schedule(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 unsigned long flags;
449
450 local_irq_save(flags);
Olof Johansson48f20a92008-03-04 15:23:25 -0800451 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100452 *__this_cpu_read(tasklet_vec.tail) = t;
453 __this_cpu_write(tasklet_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 raise_softirq_irqoff(TASKLET_SOFTIRQ);
455 local_irq_restore(flags);
456}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457EXPORT_SYMBOL(__tasklet_schedule);
458
Harvey Harrison7ad5b3a2008-02-08 04:19:53 -0800459void __tasklet_hi_schedule(struct tasklet_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 unsigned long flags;
462
463 local_irq_save(flags);
Olof Johansson48f20a92008-03-04 15:23:25 -0800464 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100465 *__this_cpu_read(tasklet_hi_vec.tail) = t;
466 __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 raise_softirq_irqoff(HI_SOFTIRQ);
468 local_irq_restore(flags);
469}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470EXPORT_SYMBOL(__tasklet_hi_schedule);
471
Vegard Nossum7c692cb2008-05-21 22:53:13 +0200472void __tasklet_hi_schedule_first(struct tasklet_struct *t)
473{
474 BUG_ON(!irqs_disabled());
475
Christoph Lameter909ea962010-12-08 16:22:55 +0100476 t->next = __this_cpu_read(tasklet_hi_vec.head);
477 __this_cpu_write(tasklet_hi_vec.head, t);
Vegard Nossum7c692cb2008-05-21 22:53:13 +0200478 __raise_softirq_irqoff(HI_SOFTIRQ);
479}
Vegard Nossum7c692cb2008-05-21 22:53:13 +0200480EXPORT_SYMBOL(__tasklet_hi_schedule_first);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static void tasklet_action(struct softirq_action *a)
483{
484 struct tasklet_struct *list;
485
486 local_irq_disable();
Christoph Lameter909ea962010-12-08 16:22:55 +0100487 list = __this_cpu_read(tasklet_vec.head);
488 __this_cpu_write(tasklet_vec.head, NULL);
489 __this_cpu_write(tasklet_vec.tail, &__get_cpu_var(tasklet_vec).head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 local_irq_enable();
491
492 while (list) {
493 struct tasklet_struct *t = list;
494
495 list = list->next;
496
497 if (tasklet_trylock(t)) {
498 if (!atomic_read(&t->count)) {
Joe Perchesce85b4f2014-01-27 17:07:16 -0800499 if (!test_and_clear_bit(TASKLET_STATE_SCHED,
500 &t->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 BUG();
502 t->func(t->data);
503 tasklet_unlock(t);
504 continue;
505 }
506 tasklet_unlock(t);
507 }
508
509 local_irq_disable();
Olof Johansson48f20a92008-03-04 15:23:25 -0800510 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100511 *__this_cpu_read(tasklet_vec.tail) = t;
512 __this_cpu_write(tasklet_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 __raise_softirq_irqoff(TASKLET_SOFTIRQ);
514 local_irq_enable();
515 }
516}
517
518static void tasklet_hi_action(struct softirq_action *a)
519{
520 struct tasklet_struct *list;
521
522 local_irq_disable();
Christoph Lameter909ea962010-12-08 16:22:55 +0100523 list = __this_cpu_read(tasklet_hi_vec.head);
524 __this_cpu_write(tasklet_hi_vec.head, NULL);
525 __this_cpu_write(tasklet_hi_vec.tail, &__get_cpu_var(tasklet_hi_vec).head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 local_irq_enable();
527
528 while (list) {
529 struct tasklet_struct *t = list;
530
531 list = list->next;
532
533 if (tasklet_trylock(t)) {
534 if (!atomic_read(&t->count)) {
Joe Perchesce85b4f2014-01-27 17:07:16 -0800535 if (!test_and_clear_bit(TASKLET_STATE_SCHED,
536 &t->state))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 BUG();
538 t->func(t->data);
539 tasklet_unlock(t);
540 continue;
541 }
542 tasklet_unlock(t);
543 }
544
545 local_irq_disable();
Olof Johansson48f20a92008-03-04 15:23:25 -0800546 t->next = NULL;
Christoph Lameter909ea962010-12-08 16:22:55 +0100547 *__this_cpu_read(tasklet_hi_vec.tail) = t;
548 __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 __raise_softirq_irqoff(HI_SOFTIRQ);
550 local_irq_enable();
551 }
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554void tasklet_init(struct tasklet_struct *t,
555 void (*func)(unsigned long), unsigned long data)
556{
557 t->next = NULL;
558 t->state = 0;
559 atomic_set(&t->count, 0);
560 t->func = func;
561 t->data = data;
562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563EXPORT_SYMBOL(tasklet_init);
564
565void tasklet_kill(struct tasklet_struct *t)
566{
567 if (in_interrupt())
Joe Perches40322762014-01-27 17:07:15 -0800568 pr_notice("Attempt to kill tasklet from interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
H Hartley Sweeten79d381c2009-04-16 19:30:18 -0400571 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 yield();
H Hartley Sweeten79d381c2009-04-16 19:30:18 -0400573 } while (test_bit(TASKLET_STATE_SCHED, &t->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 tasklet_unlock_wait(t);
576 clear_bit(TASKLET_STATE_SCHED, &t->state);
577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578EXPORT_SYMBOL(tasklet_kill);
579
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200580/*
581 * tasklet_hrtimer
582 */
583
584/*
Peter Zijlstrab9c30322010-02-03 18:08:52 +0100585 * The trampoline is called when the hrtimer expires. It schedules a tasklet
586 * to run __tasklet_hrtimer_trampoline() which in turn will call the intended
587 * hrtimer callback, but from softirq context.
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200588 */
589static enum hrtimer_restart __hrtimer_tasklet_trampoline(struct hrtimer *timer)
590{
591 struct tasklet_hrtimer *ttimer =
592 container_of(timer, struct tasklet_hrtimer, timer);
593
Peter Zijlstrab9c30322010-02-03 18:08:52 +0100594 tasklet_hi_schedule(&ttimer->tasklet);
595 return HRTIMER_NORESTART;
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200596}
597
598/*
599 * Helper function which calls the hrtimer callback from
600 * tasklet/softirq context
601 */
602static void __tasklet_hrtimer_trampoline(unsigned long data)
603{
604 struct tasklet_hrtimer *ttimer = (void *)data;
605 enum hrtimer_restart restart;
606
607 restart = ttimer->function(&ttimer->timer);
608 if (restart != HRTIMER_NORESTART)
609 hrtimer_restart(&ttimer->timer);
610}
611
612/**
613 * tasklet_hrtimer_init - Init a tasklet/hrtimer combo for softirq callbacks
614 * @ttimer: tasklet_hrtimer which is initialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300615 * @function: hrtimer callback function which gets called from softirq context
Peter Zijlstra9ba5f002009-07-22 14:18:35 +0200616 * @which_clock: clock id (CLOCK_MONOTONIC/CLOCK_REALTIME)
617 * @mode: hrtimer mode (HRTIMER_MODE_ABS/HRTIMER_MODE_REL)
618 */
619void tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
620 enum hrtimer_restart (*function)(struct hrtimer *),
621 clockid_t which_clock, enum hrtimer_mode mode)
622{
623 hrtimer_init(&ttimer->timer, which_clock, mode);
624 ttimer->timer.function = __hrtimer_tasklet_trampoline;
625 tasklet_init(&ttimer->tasklet, __tasklet_hrtimer_trampoline,
626 (unsigned long)ttimer);
627 ttimer->function = function;
628}
629EXPORT_SYMBOL_GPL(tasklet_hrtimer_init);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631void __init softirq_init(void)
632{
Olof Johansson48f20a92008-03-04 15:23:25 -0800633 int cpu;
634
635 for_each_possible_cpu(cpu) {
636 per_cpu(tasklet_vec, cpu).tail =
637 &per_cpu(tasklet_vec, cpu).head;
638 per_cpu(tasklet_hi_vec, cpu).tail =
639 &per_cpu(tasklet_hi_vec, cpu).head;
640 }
641
Carlos R. Mafra962cf362008-05-15 11:15:37 -0300642 open_softirq(TASKLET_SOFTIRQ, tasklet_action);
643 open_softirq(HI_SOFTIRQ, tasklet_hi_action);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000646static int ksoftirqd_should_run(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000648 return local_softirq_pending();
649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000651static void run_ksoftirqd(unsigned int cpu)
652{
653 local_irq_disable();
654 if (local_softirq_pending()) {
Frederic Weisbecker0bed6982013-09-05 16:14:00 +0200655 /*
656 * We can safely run softirq on inline stack, as we are not deep
657 * in the task stack here.
658 */
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000659 __do_softirq();
660 rcu_note_context_switch(cpu);
661 local_irq_enable();
662 cond_resched();
663 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000665 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668#ifdef CONFIG_HOTPLUG_CPU
669/*
670 * tasklet_kill_immediate is called to remove a tasklet which can already be
671 * scheduled for execution on @cpu.
672 *
673 * Unlike tasklet_kill, this function removes the tasklet
674 * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
675 *
676 * When this function is called, @cpu must be in the CPU_DEAD state.
677 */
678void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
679{
680 struct tasklet_struct **i;
681
682 BUG_ON(cpu_online(cpu));
683 BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
684
685 if (!test_bit(TASKLET_STATE_SCHED, &t->state))
686 return;
687
688 /* CPU is dead, so no lock needed. */
Olof Johansson48f20a92008-03-04 15:23:25 -0800689 for (i = &per_cpu(tasklet_vec, cpu).head; *i; i = &(*i)->next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (*i == t) {
691 *i = t->next;
Olof Johansson48f20a92008-03-04 15:23:25 -0800692 /* If this was the tail element, move the tail ptr */
693 if (*i == NULL)
694 per_cpu(tasklet_vec, cpu).tail = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return;
696 }
697 }
698 BUG();
699}
700
701static void takeover_tasklets(unsigned int cpu)
702{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 /* CPU is dead, so no lock needed. */
704 local_irq_disable();
705
706 /* Find end, append list for that CPU. */
Christian Borntraegere5e41722008-05-01 04:34:23 -0700707 if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100708 *__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
709 this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
Christian Borntraegere5e41722008-05-01 04:34:23 -0700710 per_cpu(tasklet_vec, cpu).head = NULL;
711 per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 raise_softirq_irqoff(TASKLET_SOFTIRQ);
714
Christian Borntraegere5e41722008-05-01 04:34:23 -0700715 if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
Christoph Lameter909ea962010-12-08 16:22:55 +0100716 *__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
717 __this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
Christian Borntraegere5e41722008-05-01 04:34:23 -0700718 per_cpu(tasklet_hi_vec, cpu).head = NULL;
719 per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
720 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 raise_softirq_irqoff(HI_SOFTIRQ);
722
723 local_irq_enable();
724}
725#endif /* CONFIG_HOTPLUG_CPU */
726
Joe Perchesce85b4f2014-01-27 17:07:16 -0800727static int cpu_callback(struct notifier_block *nfb, unsigned long action,
728 void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 switch (action) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731#ifdef CONFIG_HOTPLUG_CPU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 case CPU_DEAD:
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000733 case CPU_DEAD_FROZEN:
734 takeover_tasklets((unsigned long)hcpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 break;
736#endif /* CONFIG_HOTPLUG_CPU */
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return NOTIFY_OK;
739}
740
Paul Gortmaker0db06282013-06-19 14:53:51 -0400741static struct notifier_block cpu_nfb = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 .notifier_call = cpu_callback
743};
744
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000745static struct smp_hotplug_thread softirq_threads = {
746 .store = &ksoftirqd,
747 .thread_should_run = ksoftirqd_should_run,
748 .thread_fn = run_ksoftirqd,
749 .thread_comm = "ksoftirqd/%u",
750};
751
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -0700752static __init int spawn_ksoftirqd(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 register_cpu_notifier(&cpu_nfb);
Thomas Gleixner3e339b52012-07-16 10:42:37 +0000755
756 BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return 0;
759}
Eduard - Gabriel Munteanu7babe8d2008-07-25 19:45:11 -0700760early_initcall(spawn_ksoftirqd);
Andrew Morton78eef012006-03-22 00:08:16 -0800761
Yinghai Lu43a25632008-12-28 16:01:13 -0800762/*
763 * [ These __weak aliases are kept in a separate compilation unit, so that
764 * GCC does not inline them incorrectly. ]
765 */
766
767int __init __weak early_irq_init(void)
768{
769 return 0;
770}
771
Yinghai Lu4a046d12009-01-12 17:39:24 -0800772int __init __weak arch_probe_nr_irqs(void)
773{
Thomas Gleixnerb683de22010-09-27 20:55:03 +0200774 return NR_IRQS_LEGACY;
Yinghai Lu4a046d12009-01-12 17:39:24 -0800775}
776
Yinghai Lu43a25632008-12-28 16:01:13 -0800777int __init __weak arch_early_irq_init(void)
778{
779 return 0;
780}