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