xref: /openbmc/linux/kernel/softirq.c (revision 4d75f5c664195b970e1cd2fd25b65b5eff257a0a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *	linux/kernel/softirq.c
4  *
5  *	Copyright (C) 1992 Linus Torvalds
6  *
7  *	Rewritten. Old one was good in 2.2, but in 2.3 it was immoral. --ANK (990903)
8  */
9 
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/export.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.h>
16 #include <linux/local_lock.h>
17 #include <linux/mm.h>
18 #include <linux/notifier.h>
19 #include <linux/percpu.h>
20 #include <linux/cpu.h>
21 #include <linux/freezer.h>
22 #include <linux/kthread.h>
23 #include <linux/rcupdate.h>
24 #include <linux/ftrace.h>
25 #include <linux/smp.h>
26 #include <linux/smpboot.h>
27 #include <linux/tick.h>
28 #include <linux/irq.h>
29 #include <linux/wait_bit.h>
30 
31 #include <asm/softirq_stack.h>
32 
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/irq.h>
35 
36 /*
37    - No shared variables, all the data are CPU local.
38    - If a softirq needs serialization, let it serialize itself
39      by its own spinlocks.
40    - Even if softirq is serialized, only local cpu is marked for
41      execution. Hence, we get something sort of weak cpu binding.
42      Though it is still not clear, will it result in better locality
43      or will not.
44 
45    Examples:
46    - NET RX softirq. It is multithreaded and does not require
47      any global serialization.
48    - NET TX softirq. It kicks software netdevice queues, hence
49      it is logically serialized per device, but this serialization
50      is invisible to common code.
51    - Tasklets: serialized wrt itself.
52  */
53 
54 #ifndef __ARCH_IRQ_STAT
55 DEFINE_PER_CPU_ALIGNED(irq_cpustat_t, irq_stat);
56 EXPORT_PER_CPU_SYMBOL(irq_stat);
57 #endif
58 
59 static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
60 
61 DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
62 
63 const char * const softirq_to_name[NR_SOFTIRQS] = {
64 	"HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
65 	"TASKLET", "SCHED", "HRTIMER", "RCU"
66 };
67 
68 /*
69  * we cannot loop indefinitely here to avoid userspace starvation,
70  * but we also don't want to introduce a worst case 1/HZ latency
71  * to the pending events, so lets the scheduler to balance
72  * the softirq load for us.
73  */
wakeup_softirqd(void)74 static void wakeup_softirqd(void)
75 {
76 	/* Interrupts are disabled: no need to stop preemption */
77 	struct task_struct *tsk = __this_cpu_read(ksoftirqd);
78 
79 	if (tsk)
80 		wake_up_process(tsk);
81 }
82 
83 #ifdef CONFIG_TRACE_IRQFLAGS
84 DEFINE_PER_CPU(int, hardirqs_enabled);
85 DEFINE_PER_CPU(int, hardirq_context);
86 EXPORT_PER_CPU_SYMBOL_GPL(hardirqs_enabled);
87 EXPORT_PER_CPU_SYMBOL_GPL(hardirq_context);
88 #endif
89 
90 /*
91  * SOFTIRQ_OFFSET usage:
92  *
93  * On !RT kernels 'count' is the preempt counter, on RT kernels this applies
94  * to a per CPU counter and to task::softirqs_disabled_cnt.
95  *
96  * - count is changed by SOFTIRQ_OFFSET on entering or leaving softirq
97  *   processing.
98  *
99  * - count is changed by SOFTIRQ_DISABLE_OFFSET (= 2 * SOFTIRQ_OFFSET)
100  *   on local_bh_disable or local_bh_enable.
101  *
102  * This lets us distinguish between whether we are currently processing
103  * softirq and whether we just have bh disabled.
104  */
105 #ifdef CONFIG_PREEMPT_RT
106 
107 /*
108  * RT accounts for BH disabled sections in task::softirqs_disabled_cnt and
109  * also in per CPU softirq_ctrl::cnt. This is necessary to allow tasks in a
110  * softirq disabled section to be preempted.
111  *
112  * The per task counter is used for softirq_count(), in_softirq() and
113  * in_serving_softirqs() because these counts are only valid when the task
114  * holding softirq_ctrl::lock is running.
115  *
116  * The per CPU counter prevents pointless wakeups of ksoftirqd in case that
117  * the task which is in a softirq disabled section is preempted or blocks.
118  */
119 struct softirq_ctrl {
120 	local_lock_t	lock;
121 	int		cnt;
122 };
123 
124 static DEFINE_PER_CPU(struct softirq_ctrl, softirq_ctrl) = {
125 	.lock	= INIT_LOCAL_LOCK(softirq_ctrl.lock),
126 };
127 
128 #ifdef CONFIG_DEBUG_LOCK_ALLOC
129 static struct lock_class_key bh_lock_key;
130 struct lockdep_map bh_lock_map = {
131 	.name			= "local_bh",
132 	.key			= &bh_lock_key,
133 	.wait_type_outer	= LD_WAIT_FREE,
134 	.wait_type_inner	= LD_WAIT_CONFIG, /* PREEMPT_RT makes BH preemptible. */
135 	.lock_type		= LD_LOCK_PERCPU,
136 };
137 EXPORT_SYMBOL_GPL(bh_lock_map);
138 #endif
139 
140 /**
141  * local_bh_blocked() - Check for idle whether BH processing is blocked
142  *
143  * Returns false if the per CPU softirq::cnt is 0 otherwise true.
144  *
145  * This is invoked from the idle task to guard against false positive
146  * softirq pending warnings, which would happen when the task which holds
147  * softirq_ctrl::lock was the only running task on the CPU and blocks on
148  * some other lock.
149  */
local_bh_blocked(void)150 bool local_bh_blocked(void)
151 {
152 	return __this_cpu_read(softirq_ctrl.cnt) != 0;
153 }
154 
__local_bh_disable_ip(unsigned long ip,unsigned int cnt)155 void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
156 {
157 	unsigned long flags;
158 	int newcnt;
159 
160 	WARN_ON_ONCE(in_hardirq());
161 
162 	lock_map_acquire_read(&bh_lock_map);
163 
164 	/* First entry of a task into a BH disabled section? */
165 	if (!current->softirq_disable_cnt) {
166 		if (preemptible()) {
167 			local_lock(&softirq_ctrl.lock);
168 			/* Required to meet the RCU bottomhalf requirements. */
169 			rcu_read_lock();
170 		} else {
171 			DEBUG_LOCKS_WARN_ON(this_cpu_read(softirq_ctrl.cnt));
172 		}
173 	}
174 
175 	/*
176 	 * Track the per CPU softirq disabled state. On RT this is per CPU
177 	 * state to allow preemption of bottom half disabled sections.
178 	 */
179 	newcnt = __this_cpu_add_return(softirq_ctrl.cnt, cnt);
180 	/*
181 	 * Reflect the result in the task state to prevent recursion on the
182 	 * local lock and to make softirq_count() & al work.
183 	 */
184 	current->softirq_disable_cnt = newcnt;
185 
186 	if (IS_ENABLED(CONFIG_TRACE_IRQFLAGS) && newcnt == cnt) {
187 		raw_local_irq_save(flags);
188 		lockdep_softirqs_off(ip);
189 		raw_local_irq_restore(flags);
190 	}
191 }
192 EXPORT_SYMBOL(__local_bh_disable_ip);
193 
__local_bh_enable(unsigned int cnt,bool unlock)194 static void __local_bh_enable(unsigned int cnt, bool unlock)
195 {
196 	unsigned long flags;
197 	int newcnt;
198 
199 	DEBUG_LOCKS_WARN_ON(current->softirq_disable_cnt !=
200 			    this_cpu_read(softirq_ctrl.cnt));
201 
202 	if (IS_ENABLED(CONFIG_TRACE_IRQFLAGS) && softirq_count() == cnt) {
203 		raw_local_irq_save(flags);
204 		lockdep_softirqs_on(_RET_IP_);
205 		raw_local_irq_restore(flags);
206 	}
207 
208 	newcnt = __this_cpu_sub_return(softirq_ctrl.cnt, cnt);
209 	current->softirq_disable_cnt = newcnt;
210 
211 	if (!newcnt && unlock) {
212 		rcu_read_unlock();
213 		local_unlock(&softirq_ctrl.lock);
214 	}
215 }
216 
__local_bh_enable_ip(unsigned long ip,unsigned int cnt)217 void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
218 {
219 	bool preempt_on = preemptible();
220 	unsigned long flags;
221 	u32 pending;
222 	int curcnt;
223 
224 	WARN_ON_ONCE(in_hardirq());
225 	lockdep_assert_irqs_enabled();
226 
227 	lock_map_release(&bh_lock_map);
228 
229 	local_irq_save(flags);
230 	curcnt = __this_cpu_read(softirq_ctrl.cnt);
231 
232 	/*
233 	 * If this is not reenabling soft interrupts, no point in trying to
234 	 * run pending ones.
235 	 */
236 	if (curcnt != cnt)
237 		goto out;
238 
239 	pending = local_softirq_pending();
240 	if (!pending)
241 		goto out;
242 
243 	/*
244 	 * If this was called from non preemptible context, wake up the
245 	 * softirq daemon.
246 	 */
247 	if (!preempt_on) {
248 		wakeup_softirqd();
249 		goto out;
250 	}
251 
252 	/*
253 	 * Adjust softirq count to SOFTIRQ_OFFSET which makes
254 	 * in_serving_softirq() become true.
255 	 */
256 	cnt = SOFTIRQ_OFFSET;
257 	__local_bh_enable(cnt, false);
258 	__do_softirq();
259 
260 out:
261 	__local_bh_enable(cnt, preempt_on);
262 	local_irq_restore(flags);
263 }
264 EXPORT_SYMBOL(__local_bh_enable_ip);
265 
266 /*
267  * Invoked from ksoftirqd_run() outside of the interrupt disabled section
268  * to acquire the per CPU local lock for reentrancy protection.
269  */
ksoftirqd_run_begin(void)270 static inline void ksoftirqd_run_begin(void)
271 {
272 	__local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
273 	local_irq_disable();
274 }
275 
276 /* Counterpart to ksoftirqd_run_begin() */
ksoftirqd_run_end(void)277 static inline void ksoftirqd_run_end(void)
278 {
279 	/* pairs with the lock_map_acquire_read() in ksoftirqd_run_begin() */
280 	lock_map_release(&bh_lock_map);
281 	__local_bh_enable(SOFTIRQ_OFFSET, true);
282 	WARN_ON_ONCE(in_interrupt());
283 	local_irq_enable();
284 }
285 
softirq_handle_begin(void)286 static inline void softirq_handle_begin(void) { }
softirq_handle_end(void)287 static inline void softirq_handle_end(void) { }
288 
should_wake_ksoftirqd(void)289 static inline bool should_wake_ksoftirqd(void)
290 {
291 	return !this_cpu_read(softirq_ctrl.cnt);
292 }
293 
invoke_softirq(void)294 static inline void invoke_softirq(void)
295 {
296 	if (should_wake_ksoftirqd())
297 		wakeup_softirqd();
298 }
299 
300 #define SCHED_SOFTIRQ_MASK	BIT(SCHED_SOFTIRQ)
301 
302 /*
303  * flush_smp_call_function_queue() can raise a soft interrupt in a function
304  * call. On RT kernels this is undesired and the only known functionalities
305  * are in the block layer which is disabled on RT, and in the scheduler for
306  * idle load balancing. If soft interrupts get raised which haven't been
307  * raised before the flush, warn if it is not a SCHED_SOFTIRQ so it can be
308  * investigated.
309  */
do_softirq_post_smp_call_flush(unsigned int was_pending)310 void do_softirq_post_smp_call_flush(unsigned int was_pending)
311 {
312 	unsigned int is_pending = local_softirq_pending();
313 
314 	if (unlikely(was_pending != is_pending)) {
315 		WARN_ON_ONCE(was_pending != (is_pending & ~SCHED_SOFTIRQ_MASK));
316 		invoke_softirq();
317 	}
318 }
319 
320 #else /* CONFIG_PREEMPT_RT */
321 
322 /*
323  * This one is for softirq.c-internal use, where hardirqs are disabled
324  * legitimately:
325  */
326 #ifdef CONFIG_TRACE_IRQFLAGS
__local_bh_disable_ip(unsigned long ip,unsigned int cnt)327 void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
328 {
329 	unsigned long flags;
330 
331 	WARN_ON_ONCE(in_hardirq());
332 
333 	raw_local_irq_save(flags);
334 	/*
335 	 * The preempt tracer hooks into preempt_count_add and will break
336 	 * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET
337 	 * is set and before current->softirq_enabled is cleared.
338 	 * We must manually increment preempt_count here and manually
339 	 * call the trace_preempt_off later.
340 	 */
341 	__preempt_count_add(cnt);
342 	/*
343 	 * Were softirqs turned off above:
344 	 */
345 	if (softirq_count() == (cnt & SOFTIRQ_MASK))
346 		lockdep_softirqs_off(ip);
347 	raw_local_irq_restore(flags);
348 
349 	if (preempt_count() == cnt) {
350 #ifdef CONFIG_DEBUG_PREEMPT
351 		current->preempt_disable_ip = get_lock_parent_ip();
352 #endif
353 		trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
354 	}
355 }
356 EXPORT_SYMBOL(__local_bh_disable_ip);
357 #endif /* CONFIG_TRACE_IRQFLAGS */
358 
__local_bh_enable(unsigned int cnt)359 static void __local_bh_enable(unsigned int cnt)
360 {
361 	lockdep_assert_irqs_disabled();
362 
363 	if (preempt_count() == cnt)
364 		trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
365 
366 	if (softirq_count() == (cnt & SOFTIRQ_MASK))
367 		lockdep_softirqs_on(_RET_IP_);
368 
369 	__preempt_count_sub(cnt);
370 }
371 
372 /*
373  * Special-case - softirqs can safely be enabled by __do_softirq(),
374  * without processing still-pending softirqs:
375  */
_local_bh_enable(void)376 void _local_bh_enable(void)
377 {
378 	WARN_ON_ONCE(in_hardirq());
379 	__local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
380 }
381 EXPORT_SYMBOL(_local_bh_enable);
382 
__local_bh_enable_ip(unsigned long ip,unsigned int cnt)383 void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
384 {
385 	WARN_ON_ONCE(in_hardirq());
386 	lockdep_assert_irqs_enabled();
387 #ifdef CONFIG_TRACE_IRQFLAGS
388 	local_irq_disable();
389 #endif
390 	/*
391 	 * Are softirqs going to be turned on now:
392 	 */
393 	if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
394 		lockdep_softirqs_on(ip);
395 	/*
396 	 * Keep preemption disabled until we are done with
397 	 * softirq processing:
398 	 */
399 	__preempt_count_sub(cnt - 1);
400 
401 	if (unlikely(!in_interrupt() && local_softirq_pending())) {
402 		/*
403 		 * Run softirq if any pending. And do it in its own stack
404 		 * as we may be calling this deep in a task call stack already.
405 		 */
406 		do_softirq();
407 	}
408 
409 	preempt_count_dec();
410 #ifdef CONFIG_TRACE_IRQFLAGS
411 	local_irq_enable();
412 #endif
413 	preempt_check_resched();
414 }
415 EXPORT_SYMBOL(__local_bh_enable_ip);
416 
softirq_handle_begin(void)417 static inline void softirq_handle_begin(void)
418 {
419 	__local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
420 }
421 
softirq_handle_end(void)422 static inline void softirq_handle_end(void)
423 {
424 	__local_bh_enable(SOFTIRQ_OFFSET);
425 	WARN_ON_ONCE(in_interrupt());
426 }
427 
ksoftirqd_run_begin(void)428 static inline void ksoftirqd_run_begin(void)
429 {
430 	local_irq_disable();
431 }
432 
ksoftirqd_run_end(void)433 static inline void ksoftirqd_run_end(void)
434 {
435 	local_irq_enable();
436 }
437 
should_wake_ksoftirqd(void)438 static inline bool should_wake_ksoftirqd(void)
439 {
440 	return true;
441 }
442 
invoke_softirq(void)443 static inline void invoke_softirq(void)
444 {
445 	if (!force_irqthreads() || !__this_cpu_read(ksoftirqd)) {
446 #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
447 		/*
448 		 * We can safely execute softirq on the current stack if
449 		 * it is the irq stack, because it should be near empty
450 		 * at this stage.
451 		 */
452 		__do_softirq();
453 #else
454 		/*
455 		 * Otherwise, irq_exit() is called on the task stack that can
456 		 * be potentially deep already. So call softirq in its own stack
457 		 * to prevent from any overrun.
458 		 */
459 		do_softirq_own_stack();
460 #endif
461 	} else {
462 		wakeup_softirqd();
463 	}
464 }
465 
do_softirq(void)466 asmlinkage __visible void do_softirq(void)
467 {
468 	__u32 pending;
469 	unsigned long flags;
470 
471 	if (in_interrupt())
472 		return;
473 
474 	local_irq_save(flags);
475 
476 	pending = local_softirq_pending();
477 
478 	if (pending)
479 		do_softirq_own_stack();
480 
481 	local_irq_restore(flags);
482 }
483 
484 #endif /* !CONFIG_PREEMPT_RT */
485 
486 /*
487  * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
488  * but break the loop if need_resched() is set or after 2 ms.
489  * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
490  * certain cases, such as stop_machine(), jiffies may cease to
491  * increment and so we need the MAX_SOFTIRQ_RESTART limit as
492  * well to make sure we eventually return from this method.
493  *
494  * These limits have been established via experimentation.
495  * The two things to balance is latency against fairness -
496  * we want to handle softirqs as soon as possible, but they
497  * should not be able to lock up the box.
498  */
499 #define MAX_SOFTIRQ_TIME  msecs_to_jiffies(2)
500 #define MAX_SOFTIRQ_RESTART 10
501 
502 #ifdef CONFIG_TRACE_IRQFLAGS
503 /*
504  * When we run softirqs from irq_exit() and thus on the hardirq stack we need
505  * to keep the lockdep irq context tracking as tight as possible in order to
506  * not miss-qualify lock contexts and miss possible deadlocks.
507  */
508 
lockdep_softirq_start(void)509 static inline bool lockdep_softirq_start(void)
510 {
511 	bool in_hardirq = false;
512 
513 	if (lockdep_hardirq_context()) {
514 		in_hardirq = true;
515 		lockdep_hardirq_exit();
516 	}
517 
518 	lockdep_softirq_enter();
519 
520 	return in_hardirq;
521 }
522 
lockdep_softirq_end(bool in_hardirq)523 static inline void lockdep_softirq_end(bool in_hardirq)
524 {
525 	lockdep_softirq_exit();
526 
527 	if (in_hardirq)
528 		lockdep_hardirq_enter();
529 }
530 #else
lockdep_softirq_start(void)531 static inline bool lockdep_softirq_start(void) { return false; }
lockdep_softirq_end(bool in_hardirq)532 static inline void lockdep_softirq_end(bool in_hardirq) { }
533 #endif
534 
handle_softirqs(bool ksirqd)535 static void handle_softirqs(bool ksirqd)
536 {
537 	unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
538 	unsigned long old_flags = current->flags;
539 	int max_restart = MAX_SOFTIRQ_RESTART;
540 	struct softirq_action *h;
541 	bool in_hardirq;
542 	__u32 pending;
543 	int softirq_bit;
544 
545 	/*
546 	 * Mask out PF_MEMALLOC as the current task context is borrowed for the
547 	 * softirq. A softirq handled, such as network RX, might set PF_MEMALLOC
548 	 * again if the socket is related to swapping.
549 	 */
550 	current->flags &= ~PF_MEMALLOC;
551 
552 	pending = local_softirq_pending();
553 
554 	softirq_handle_begin();
555 	in_hardirq = lockdep_softirq_start();
556 	account_softirq_enter(current);
557 
558 restart:
559 	/* Reset the pending bitmask before enabling irqs */
560 	set_softirq_pending(0);
561 
562 	local_irq_enable();
563 
564 	h = softirq_vec;
565 
566 	while ((softirq_bit = ffs(pending))) {
567 		unsigned int vec_nr;
568 		int prev_count;
569 
570 		h += softirq_bit - 1;
571 
572 		vec_nr = h - softirq_vec;
573 		prev_count = preempt_count();
574 
575 		kstat_incr_softirqs_this_cpu(vec_nr);
576 
577 		trace_softirq_entry(vec_nr);
578 		h->action(h);
579 		trace_softirq_exit(vec_nr);
580 		if (unlikely(prev_count != preempt_count())) {
581 			pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
582 			       vec_nr, softirq_to_name[vec_nr], h->action,
583 			       prev_count, preempt_count());
584 			preempt_count_set(prev_count);
585 		}
586 		h++;
587 		pending >>= softirq_bit;
588 	}
589 
590 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) && ksirqd)
591 		rcu_softirq_qs();
592 
593 	local_irq_disable();
594 
595 	pending = local_softirq_pending();
596 	if (pending) {
597 		if (time_before(jiffies, end) && !need_resched() &&
598 		    --max_restart)
599 			goto restart;
600 
601 		wakeup_softirqd();
602 	}
603 
604 	account_softirq_exit(current);
605 	lockdep_softirq_end(in_hardirq);
606 	softirq_handle_end();
607 	current_restore_flags(old_flags, PF_MEMALLOC);
608 }
609 
__do_softirq(void)610 asmlinkage __visible void __softirq_entry __do_softirq(void)
611 {
612 	handle_softirqs(false);
613 }
614 
615 /**
616  * irq_enter_rcu - Enter an interrupt context with RCU watching
617  */
irq_enter_rcu(void)618 void irq_enter_rcu(void)
619 {
620 	__irq_enter_raw();
621 
622 	if (tick_nohz_full_cpu(smp_processor_id()) ||
623 	    (is_idle_task(current) && (irq_count() == HARDIRQ_OFFSET)))
624 		tick_irq_enter();
625 
626 	account_hardirq_enter(current);
627 }
628 
629 /**
630  * irq_enter - Enter an interrupt context including RCU update
631  */
irq_enter(void)632 void irq_enter(void)
633 {
634 	ct_irq_enter();
635 	irq_enter_rcu();
636 }
637 
tick_irq_exit(void)638 static inline void tick_irq_exit(void)
639 {
640 #ifdef CONFIG_NO_HZ_COMMON
641 	int cpu = smp_processor_id();
642 
643 	/* Make sure that timer wheel updates are propagated */
644 	if ((sched_core_idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
645 		if (!in_hardirq())
646 			tick_nohz_irq_exit();
647 	}
648 #endif
649 }
650 
__irq_exit_rcu(void)651 static inline void __irq_exit_rcu(void)
652 {
653 #ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
654 	local_irq_disable();
655 #else
656 	lockdep_assert_irqs_disabled();
657 #endif
658 	account_hardirq_exit(current);
659 	preempt_count_sub(HARDIRQ_OFFSET);
660 	if (!in_interrupt() && local_softirq_pending())
661 		invoke_softirq();
662 
663 	tick_irq_exit();
664 }
665 
666 /**
667  * irq_exit_rcu() - Exit an interrupt context without updating RCU
668  *
669  * Also processes softirqs if needed and possible.
670  */
irq_exit_rcu(void)671 void irq_exit_rcu(void)
672 {
673 	__irq_exit_rcu();
674 	 /* must be last! */
675 	lockdep_hardirq_exit();
676 }
677 
678 /**
679  * irq_exit - Exit an interrupt context, update RCU and lockdep
680  *
681  * Also processes softirqs if needed and possible.
682  */
irq_exit(void)683 void irq_exit(void)
684 {
685 	__irq_exit_rcu();
686 	ct_irq_exit();
687 	 /* must be last! */
688 	lockdep_hardirq_exit();
689 }
690 
691 /*
692  * This function must run with irqs disabled!
693  */
raise_softirq_irqoff(unsigned int nr)694 inline void raise_softirq_irqoff(unsigned int nr)
695 {
696 	__raise_softirq_irqoff(nr);
697 
698 	/*
699 	 * If we're in an interrupt or softirq, we're done
700 	 * (this also catches softirq-disabled code). We will
701 	 * actually run the softirq once we return from
702 	 * the irq or softirq.
703 	 *
704 	 * Otherwise we wake up ksoftirqd to make sure we
705 	 * schedule the softirq soon.
706 	 */
707 	if (!in_interrupt() && should_wake_ksoftirqd())
708 		wakeup_softirqd();
709 }
710 
raise_softirq(unsigned int nr)711 void raise_softirq(unsigned int nr)
712 {
713 	unsigned long flags;
714 
715 	local_irq_save(flags);
716 	raise_softirq_irqoff(nr);
717 	local_irq_restore(flags);
718 }
719 
__raise_softirq_irqoff(unsigned int nr)720 void __raise_softirq_irqoff(unsigned int nr)
721 {
722 	lockdep_assert_irqs_disabled();
723 	trace_softirq_raise(nr);
724 	or_softirq_pending(1UL << nr);
725 }
726 
open_softirq(int nr,void (* action)(struct softirq_action *))727 void open_softirq(int nr, void (*action)(struct softirq_action *))
728 {
729 	softirq_vec[nr].action = action;
730 }
731 
732 /*
733  * Tasklets
734  */
735 struct tasklet_head {
736 	struct tasklet_struct *head;
737 	struct tasklet_struct **tail;
738 };
739 
740 static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
741 static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
742 
__tasklet_schedule_common(struct tasklet_struct * t,struct tasklet_head __percpu * headp,unsigned int softirq_nr)743 static void __tasklet_schedule_common(struct tasklet_struct *t,
744 				      struct tasklet_head __percpu *headp,
745 				      unsigned int softirq_nr)
746 {
747 	struct tasklet_head *head;
748 	unsigned long flags;
749 
750 	local_irq_save(flags);
751 	head = this_cpu_ptr(headp);
752 	t->next = NULL;
753 	*head->tail = t;
754 	head->tail = &(t->next);
755 	raise_softirq_irqoff(softirq_nr);
756 	local_irq_restore(flags);
757 }
758 
__tasklet_schedule(struct tasklet_struct * t)759 void __tasklet_schedule(struct tasklet_struct *t)
760 {
761 	__tasklet_schedule_common(t, &tasklet_vec,
762 				  TASKLET_SOFTIRQ);
763 }
764 EXPORT_SYMBOL(__tasklet_schedule);
765 
__tasklet_hi_schedule(struct tasklet_struct * t)766 void __tasklet_hi_schedule(struct tasklet_struct *t)
767 {
768 	__tasklet_schedule_common(t, &tasklet_hi_vec,
769 				  HI_SOFTIRQ);
770 }
771 EXPORT_SYMBOL(__tasklet_hi_schedule);
772 
tasklet_clear_sched(struct tasklet_struct * t)773 static bool tasklet_clear_sched(struct tasklet_struct *t)
774 {
775 	if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state)) {
776 		wake_up_var(&t->state);
777 		return true;
778 	}
779 
780 	WARN_ONCE(1, "tasklet SCHED state not set: %s %pS\n",
781 		  t->use_callback ? "callback" : "func",
782 		  t->use_callback ? (void *)t->callback : (void *)t->func);
783 
784 	return false;
785 }
786 
tasklet_action_common(struct softirq_action * a,struct tasklet_head * tl_head,unsigned int softirq_nr)787 static void tasklet_action_common(struct softirq_action *a,
788 				  struct tasklet_head *tl_head,
789 				  unsigned int softirq_nr)
790 {
791 	struct tasklet_struct *list;
792 
793 	local_irq_disable();
794 	list = tl_head->head;
795 	tl_head->head = NULL;
796 	tl_head->tail = &tl_head->head;
797 	local_irq_enable();
798 
799 	while (list) {
800 		struct tasklet_struct *t = list;
801 
802 		list = list->next;
803 
804 		if (tasklet_trylock(t)) {
805 			if (!atomic_read(&t->count)) {
806 				if (tasklet_clear_sched(t)) {
807 					if (t->use_callback) {
808 						trace_tasklet_entry(t, t->callback);
809 						t->callback(t);
810 						trace_tasklet_exit(t, t->callback);
811 					} else {
812 						trace_tasklet_entry(t, t->func);
813 						t->func(t->data);
814 						trace_tasklet_exit(t, t->func);
815 					}
816 				}
817 				tasklet_unlock(t);
818 				continue;
819 			}
820 			tasklet_unlock(t);
821 		}
822 
823 		local_irq_disable();
824 		t->next = NULL;
825 		*tl_head->tail = t;
826 		tl_head->tail = &t->next;
827 		__raise_softirq_irqoff(softirq_nr);
828 		local_irq_enable();
829 	}
830 }
831 
tasklet_action(struct softirq_action * a)832 static __latent_entropy void tasklet_action(struct softirq_action *a)
833 {
834 	tasklet_action_common(a, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
835 }
836 
tasklet_hi_action(struct softirq_action * a)837 static __latent_entropy void tasklet_hi_action(struct softirq_action *a)
838 {
839 	tasklet_action_common(a, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
840 }
841 
tasklet_setup(struct tasklet_struct * t,void (* callback)(struct tasklet_struct *))842 void tasklet_setup(struct tasklet_struct *t,
843 		   void (*callback)(struct tasklet_struct *))
844 {
845 	t->next = NULL;
846 	t->state = 0;
847 	atomic_set(&t->count, 0);
848 	t->callback = callback;
849 	t->use_callback = true;
850 	t->data = 0;
851 }
852 EXPORT_SYMBOL(tasklet_setup);
853 
tasklet_init(struct tasklet_struct * t,void (* func)(unsigned long),unsigned long data)854 void tasklet_init(struct tasklet_struct *t,
855 		  void (*func)(unsigned long), unsigned long data)
856 {
857 	t->next = NULL;
858 	t->state = 0;
859 	atomic_set(&t->count, 0);
860 	t->func = func;
861 	t->use_callback = false;
862 	t->data = data;
863 }
864 EXPORT_SYMBOL(tasklet_init);
865 
866 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
867 /*
868  * Do not use in new code. Waiting for tasklets from atomic contexts is
869  * error prone and should be avoided.
870  */
tasklet_unlock_spin_wait(struct tasklet_struct * t)871 void tasklet_unlock_spin_wait(struct tasklet_struct *t)
872 {
873 	while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
874 		if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
875 			/*
876 			 * Prevent a live lock when current preempted soft
877 			 * interrupt processing or prevents ksoftirqd from
878 			 * running. If the tasklet runs on a different CPU
879 			 * then this has no effect other than doing the BH
880 			 * disable/enable dance for nothing.
881 			 */
882 			local_bh_disable();
883 			local_bh_enable();
884 		} else {
885 			cpu_relax();
886 		}
887 	}
888 }
889 EXPORT_SYMBOL(tasklet_unlock_spin_wait);
890 #endif
891 
tasklet_kill(struct tasklet_struct * t)892 void tasklet_kill(struct tasklet_struct *t)
893 {
894 	if (in_interrupt())
895 		pr_notice("Attempt to kill tasklet from interrupt\n");
896 
897 	while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
898 		wait_var_event(&t->state, !test_bit(TASKLET_STATE_SCHED, &t->state));
899 
900 	tasklet_unlock_wait(t);
901 	tasklet_clear_sched(t);
902 }
903 EXPORT_SYMBOL(tasklet_kill);
904 
905 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
tasklet_unlock(struct tasklet_struct * t)906 void tasklet_unlock(struct tasklet_struct *t)
907 {
908 	smp_mb__before_atomic();
909 	clear_bit(TASKLET_STATE_RUN, &t->state);
910 	smp_mb__after_atomic();
911 	wake_up_var(&t->state);
912 }
913 EXPORT_SYMBOL_GPL(tasklet_unlock);
914 
tasklet_unlock_wait(struct tasklet_struct * t)915 void tasklet_unlock_wait(struct tasklet_struct *t)
916 {
917 	wait_var_event(&t->state, !test_bit(TASKLET_STATE_RUN, &t->state));
918 }
919 EXPORT_SYMBOL_GPL(tasklet_unlock_wait);
920 #endif
921 
softirq_init(void)922 void __init softirq_init(void)
923 {
924 	int cpu;
925 
926 	for_each_possible_cpu(cpu) {
927 		per_cpu(tasklet_vec, cpu).tail =
928 			&per_cpu(tasklet_vec, cpu).head;
929 		per_cpu(tasklet_hi_vec, cpu).tail =
930 			&per_cpu(tasklet_hi_vec, cpu).head;
931 	}
932 
933 	open_softirq(TASKLET_SOFTIRQ, tasklet_action);
934 	open_softirq(HI_SOFTIRQ, tasklet_hi_action);
935 }
936 
ksoftirqd_should_run(unsigned int cpu)937 static int ksoftirqd_should_run(unsigned int cpu)
938 {
939 	return local_softirq_pending();
940 }
941 
run_ksoftirqd(unsigned int cpu)942 static void run_ksoftirqd(unsigned int cpu)
943 {
944 	ksoftirqd_run_begin();
945 	if (local_softirq_pending()) {
946 		/*
947 		 * We can safely run softirq on inline stack, as we are not deep
948 		 * in the task stack here.
949 		 */
950 		handle_softirqs(true);
951 		ksoftirqd_run_end();
952 		cond_resched();
953 		return;
954 	}
955 	ksoftirqd_run_end();
956 }
957 
958 #ifdef CONFIG_HOTPLUG_CPU
takeover_tasklets(unsigned int cpu)959 static int takeover_tasklets(unsigned int cpu)
960 {
961 	/* CPU is dead, so no lock needed. */
962 	local_irq_disable();
963 
964 	/* Find end, append list for that CPU. */
965 	if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
966 		*__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
967 		__this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
968 		per_cpu(tasklet_vec, cpu).head = NULL;
969 		per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
970 	}
971 	raise_softirq_irqoff(TASKLET_SOFTIRQ);
972 
973 	if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
974 		*__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
975 		__this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
976 		per_cpu(tasklet_hi_vec, cpu).head = NULL;
977 		per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
978 	}
979 	raise_softirq_irqoff(HI_SOFTIRQ);
980 
981 	local_irq_enable();
982 	return 0;
983 }
984 #else
985 #define takeover_tasklets	NULL
986 #endif /* CONFIG_HOTPLUG_CPU */
987 
988 static struct smp_hotplug_thread softirq_threads = {
989 	.store			= &ksoftirqd,
990 	.thread_should_run	= ksoftirqd_should_run,
991 	.thread_fn		= run_ksoftirqd,
992 	.thread_comm		= "ksoftirqd/%u",
993 };
994 
spawn_ksoftirqd(void)995 static __init int spawn_ksoftirqd(void)
996 {
997 	cpuhp_setup_state_nocalls(CPUHP_SOFTIRQ_DEAD, "softirq:dead", NULL,
998 				  takeover_tasklets);
999 	BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
1000 
1001 	return 0;
1002 }
1003 early_initcall(spawn_ksoftirqd);
1004 
1005 /*
1006  * [ These __weak aliases are kept in a separate compilation unit, so that
1007  *   GCC does not inline them incorrectly. ]
1008  */
1009 
early_irq_init(void)1010 int __init __weak early_irq_init(void)
1011 {
1012 	return 0;
1013 }
1014 
arch_probe_nr_irqs(void)1015 int __init __weak arch_probe_nr_irqs(void)
1016 {
1017 	return NR_IRQS_LEGACY;
1018 }
1019 
arch_early_irq_init(void)1020 int __init __weak arch_early_irq_init(void)
1021 {
1022 	return 0;
1023 }
1024 
arch_dynirq_lower_bound(unsigned int from)1025 unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
1026 {
1027 	return from;
1028 }
1029