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