xref: /OK3568_Linux_fs/kernel/kernel/softirq.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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/mm.h>
17 #include <linux/notifier.h>
18 #include <linux/percpu.h>
19 #include <linux/cpu.h>
20 #include <linux/freezer.h>
21 #include <linux/kthread.h>
22 #include <linux/rcupdate.h>
23 #include <linux/ftrace.h>
24 #include <linux/smp.h>
25 #include <linux/smpboot.h>
26 #include <linux/tick.h>
27 #include <linux/irq.h>
28 
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/irq.h>
31 
32 EXPORT_TRACEPOINT_SYMBOL_GPL(irq_handler_entry);
33 EXPORT_TRACEPOINT_SYMBOL_GPL(irq_handler_exit);
34 
35 /*
36    - No shared variables, all the data are CPU local.
37    - If a softirq needs serialization, let it serialize itself
38      by its own spinlocks.
39    - Even if softirq is serialized, only local cpu is marked for
40      execution. Hence, we get something sort of weak cpu binding.
41      Though it is still not clear, will it result in better locality
42      or will not.
43 
44    Examples:
45    - NET RX softirq. It is multithreaded and does not require
46      any global serialization.
47    - NET TX softirq. It kicks software netdevice queues, hence
48      it is logically serialized per device, but this serialization
49      is invisible to common code.
50    - Tasklets: serialized wrt itself.
51  */
52 
53 #ifndef __ARCH_IRQ_STAT
54 DEFINE_PER_CPU_ALIGNED(irq_cpustat_t, irq_stat);
55 EXPORT_PER_CPU_SYMBOL(irq_stat);
56 #endif
57 
58 static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
59 
60 DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
61 EXPORT_PER_CPU_SYMBOL_GPL(ksoftirqd);
62 
63 /*
64  * active_softirqs -- per cpu, a mask of softirqs that are being handled,
65  * with the expectation that approximate answers are acceptable and therefore
66  * no synchronization.
67  */
68 DEFINE_PER_CPU(__u32, active_softirqs);
69 
70 const char * const softirq_to_name[NR_SOFTIRQS] = {
71 	"HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
72 	"TASKLET", "SCHED", "HRTIMER", "RCU"
73 };
74 
75 /*
76  * we cannot loop indefinitely here to avoid userspace starvation,
77  * but we also don't want to introduce a worst case 1/HZ latency
78  * to the pending events, so lets the scheduler to balance
79  * the softirq load for us.
80  */
wakeup_softirqd(void)81 static void wakeup_softirqd(void)
82 {
83 	/* Interrupts are disabled: no need to stop preemption */
84 	struct task_struct *tsk = __this_cpu_read(ksoftirqd);
85 
86 	if (tsk && tsk->state != TASK_RUNNING)
87 		wake_up_process(tsk);
88 }
89 
90 /*
91  * preempt_count and SOFTIRQ_OFFSET usage:
92  * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
93  *   softirq processing.
94  * - preempt_count is changed by SOFTIRQ_DISABLE_OFFSET (= 2 * SOFTIRQ_OFFSET)
95  *   on local_bh_disable or local_bh_enable.
96  * This lets us distinguish between whether we are currently processing
97  * softirq and whether we just have bh disabled.
98  */
99 
100 /*
101  * This one is for softirq.c-internal use,
102  * where hardirqs are disabled legitimately:
103  */
104 #ifdef CONFIG_TRACE_IRQFLAGS
105 
106 DEFINE_PER_CPU(int, hardirqs_enabled);
107 DEFINE_PER_CPU(int, hardirq_context);
108 EXPORT_PER_CPU_SYMBOL_GPL(hardirqs_enabled);
109 EXPORT_PER_CPU_SYMBOL_GPL(hardirq_context);
110 
__local_bh_disable_ip(unsigned long ip,unsigned int cnt)111 void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
112 {
113 	unsigned long flags;
114 
115 	WARN_ON_ONCE(in_irq());
116 
117 	raw_local_irq_save(flags);
118 	/*
119 	 * The preempt tracer hooks into preempt_count_add and will break
120 	 * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET
121 	 * is set and before current->softirq_enabled is cleared.
122 	 * We must manually increment preempt_count here and manually
123 	 * call the trace_preempt_off later.
124 	 */
125 	__preempt_count_add(cnt);
126 	/*
127 	 * Were softirqs turned off above:
128 	 */
129 	if (softirq_count() == (cnt & SOFTIRQ_MASK))
130 		lockdep_softirqs_off(ip);
131 	raw_local_irq_restore(flags);
132 
133 	if (preempt_count() == cnt) {
134 #ifdef CONFIG_DEBUG_PREEMPT
135 		current->preempt_disable_ip = get_lock_parent_ip();
136 #endif
137 		trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
138 	}
139 }
140 EXPORT_SYMBOL(__local_bh_disable_ip);
141 #endif /* CONFIG_TRACE_IRQFLAGS */
142 
__local_bh_enable(unsigned int cnt)143 static void __local_bh_enable(unsigned int cnt)
144 {
145 	lockdep_assert_irqs_disabled();
146 
147 	if (preempt_count() == cnt)
148 		trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
149 
150 	if (softirq_count() == (cnt & SOFTIRQ_MASK))
151 		lockdep_softirqs_on(_RET_IP_);
152 
153 	__preempt_count_sub(cnt);
154 }
155 
156 /*
157  * Special-case - softirqs can safely be enabled by __do_softirq(),
158  * without processing still-pending softirqs:
159  */
_local_bh_enable(void)160 void _local_bh_enable(void)
161 {
162 	WARN_ON_ONCE(in_irq());
163 	__local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
164 }
165 EXPORT_SYMBOL(_local_bh_enable);
166 
__local_bh_enable_ip(unsigned long ip,unsigned int cnt)167 void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
168 {
169 	WARN_ON_ONCE(in_irq());
170 	lockdep_assert_irqs_enabled();
171 #ifdef CONFIG_TRACE_IRQFLAGS
172 	local_irq_disable();
173 #endif
174 	/*
175 	 * Are softirqs going to be turned on now:
176 	 */
177 	if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
178 		lockdep_softirqs_on(ip);
179 	/*
180 	 * Keep preemption disabled until we are done with
181 	 * softirq processing:
182 	 */
183 	preempt_count_sub(cnt - 1);
184 
185 	if (unlikely(!in_interrupt() && local_softirq_pending())) {
186 		/*
187 		 * Run softirq if any pending. And do it in its own stack
188 		 * as we may be calling this deep in a task call stack already.
189 		 */
190 		do_softirq();
191 	}
192 
193 	preempt_count_dec();
194 #ifdef CONFIG_TRACE_IRQFLAGS
195 	local_irq_enable();
196 #endif
197 	preempt_check_resched();
198 }
199 EXPORT_SYMBOL(__local_bh_enable_ip);
200 
201 /*
202  * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
203  * but break the loop if need_resched() is set or after 2 ms.
204  * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
205  * certain cases, such as stop_machine(), jiffies may cease to
206  * increment and so we need the MAX_SOFTIRQ_RESTART limit as
207  * well to make sure we eventually return from this method.
208  *
209  * These limits have been established via experimentation.
210  * The two things to balance is latency against fairness -
211  * we want to handle softirqs as soon as possible, but they
212  * should not be able to lock up the box.
213  */
214 #define MAX_SOFTIRQ_TIME  msecs_to_jiffies(2)
215 #define MAX_SOFTIRQ_RESTART 10
216 
217 #ifdef CONFIG_TRACE_IRQFLAGS
218 /*
219  * When we run softirqs from irq_exit() and thus on the hardirq stack we need
220  * to keep the lockdep irq context tracking as tight as possible in order to
221  * not miss-qualify lock contexts and miss possible deadlocks.
222  */
223 
lockdep_softirq_start(void)224 static inline bool lockdep_softirq_start(void)
225 {
226 	bool in_hardirq = false;
227 
228 	if (lockdep_hardirq_context()) {
229 		in_hardirq = true;
230 		lockdep_hardirq_exit();
231 	}
232 
233 	lockdep_softirq_enter();
234 
235 	return in_hardirq;
236 }
237 
lockdep_softirq_end(bool in_hardirq)238 static inline void lockdep_softirq_end(bool in_hardirq)
239 {
240 	lockdep_softirq_exit();
241 
242 	if (in_hardirq)
243 		lockdep_hardirq_enter();
244 }
245 #else
lockdep_softirq_start(void)246 static inline bool lockdep_softirq_start(void) { return false; }
lockdep_softirq_end(bool in_hardirq)247 static inline void lockdep_softirq_end(bool in_hardirq) { }
248 #endif
249 
250 #define softirq_deferred_for_rt(pending)		\
251 ({							\
252 	__u32 deferred = 0;				\
253 	if (cpupri_check_rt()) {			\
254 		deferred = pending & LONG_SOFTIRQ_MASK; \
255 		pending &= ~LONG_SOFTIRQ_MASK;		\
256 	}						\
257 	deferred;					\
258 })
259 
__do_softirq(void)260 asmlinkage __visible void __softirq_entry __do_softirq(void)
261 {
262 	unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
263 	unsigned long old_flags = current->flags;
264 	int max_restart = MAX_SOFTIRQ_RESTART;
265 	struct softirq_action *h;
266 	bool in_hardirq;
267 	__u32 deferred;
268 	__u32 pending;
269 	int softirq_bit;
270 
271 	/*
272 	 * Mask out PF_MEMALLOC as the current task context is borrowed for the
273 	 * softirq. A softirq handled, such as network RX, might set PF_MEMALLOC
274 	 * again if the socket is related to swapping.
275 	 */
276 	current->flags &= ~PF_MEMALLOC;
277 
278 	pending = local_softirq_pending();
279 	deferred = softirq_deferred_for_rt(pending);
280 	account_irq_enter_time(current);
281 	__local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
282 	in_hardirq = lockdep_softirq_start();
283 
284 restart:
285 	/* Reset the pending bitmask before enabling irqs */
286 	set_softirq_pending(deferred);
287 	__this_cpu_write(active_softirqs, pending);
288 
289 	local_irq_enable();
290 
291 	h = softirq_vec;
292 
293 	while ((softirq_bit = ffs(pending))) {
294 		unsigned int vec_nr;
295 		int prev_count;
296 
297 		h += softirq_bit - 1;
298 
299 		vec_nr = h - softirq_vec;
300 		prev_count = preempt_count();
301 
302 		kstat_incr_softirqs_this_cpu(vec_nr);
303 
304 		trace_softirq_entry(vec_nr);
305 		h->action(h);
306 		trace_softirq_exit(vec_nr);
307 		if (unlikely(prev_count != preempt_count())) {
308 			pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
309 			       vec_nr, softirq_to_name[vec_nr], h->action,
310 			       prev_count, preempt_count());
311 			preempt_count_set(prev_count);
312 		}
313 		h++;
314 		pending >>= softirq_bit;
315 	}
316 
317 	__this_cpu_write(active_softirqs, 0);
318 	if (__this_cpu_read(ksoftirqd) == current)
319 		rcu_softirq_qs();
320 	local_irq_disable();
321 
322 	pending = local_softirq_pending();
323 	deferred = softirq_deferred_for_rt(pending);
324 
325 	if (pending) {
326 		if (time_before(jiffies, end) && !need_resched() &&
327 		    --max_restart)
328 			goto restart;
329 
330 #ifndef CONFIG_RT_SOFTINT_OPTIMIZATION
331 		wakeup_softirqd();
332 #endif
333 	}
334 
335 #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
336 	if (pending | deferred)
337 		wakeup_softirqd();
338 #endif
339 	lockdep_softirq_end(in_hardirq);
340 	account_irq_exit_time(current);
341 	__local_bh_enable(SOFTIRQ_OFFSET);
342 	WARN_ON_ONCE(in_interrupt());
343 	current_restore_flags(old_flags, PF_MEMALLOC);
344 }
345 
do_softirq(void)346 asmlinkage __visible void do_softirq(void)
347 {
348 	__u32 pending;
349 	unsigned long flags;
350 
351 	if (in_interrupt())
352 		return;
353 
354 	local_irq_save(flags);
355 
356 	pending = local_softirq_pending();
357 
358 	if (pending)
359 		do_softirq_own_stack();
360 
361 	local_irq_restore(flags);
362 }
363 
364 /**
365  * irq_enter_rcu - Enter an interrupt context with RCU watching
366  */
irq_enter_rcu(void)367 void irq_enter_rcu(void)
368 {
369 	if (is_idle_task(current) && !in_interrupt()) {
370 		/*
371 		 * Prevent raise_softirq from needlessly waking up ksoftirqd
372 		 * here, as softirq will be serviced on return from interrupt.
373 		 */
374 		local_bh_disable();
375 		tick_irq_enter();
376 		_local_bh_enable();
377 	}
378 	__irq_enter();
379 }
380 
381 /**
382  * irq_enter - Enter an interrupt context including RCU update
383  */
irq_enter(void)384 void irq_enter(void)
385 {
386 	rcu_irq_enter();
387 	irq_enter_rcu();
388 }
389 
invoke_softirq(void)390 static inline void invoke_softirq(void)
391 {
392 	if (!force_irqthreads) {
393 #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
394 		/*
395 		 * We can safely execute softirq on the current stack if
396 		 * it is the irq stack, because it should be near empty
397 		 * at this stage.
398 		 */
399 		__do_softirq();
400 #else
401 		/*
402 		 * Otherwise, irq_exit() is called on the task stack that can
403 		 * be potentially deep already. So call softirq in its own stack
404 		 * to prevent from any overrun.
405 		 */
406 		do_softirq_own_stack();
407 #endif
408 	} else {
409 		wakeup_softirqd();
410 	}
411 }
412 
tick_irq_exit(void)413 static inline void tick_irq_exit(void)
414 {
415 #ifdef CONFIG_NO_HZ_COMMON
416 	int cpu = smp_processor_id();
417 
418 	/* Make sure that timer wheel updates are propagated */
419 	if ((idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
420 		if (!in_irq())
421 			tick_nohz_irq_exit();
422 	}
423 #endif
424 }
425 
__irq_exit_rcu(void)426 static inline void __irq_exit_rcu(void)
427 {
428 #ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
429 	local_irq_disable();
430 #else
431 	lockdep_assert_irqs_disabled();
432 #endif
433 	account_irq_exit_time(current);
434 	preempt_count_sub(HARDIRQ_OFFSET);
435 	if (!in_interrupt() && local_softirq_pending())
436 		invoke_softirq();
437 
438 	tick_irq_exit();
439 }
440 
441 /**
442  * irq_exit_rcu() - Exit an interrupt context without updating RCU
443  *
444  * Also processes softirqs if needed and possible.
445  */
irq_exit_rcu(void)446 void irq_exit_rcu(void)
447 {
448 	__irq_exit_rcu();
449 	 /* must be last! */
450 	lockdep_hardirq_exit();
451 }
452 
453 /**
454  * irq_exit - Exit an interrupt context, update RCU and lockdep
455  *
456  * Also processes softirqs if needed and possible.
457  */
irq_exit(void)458 void irq_exit(void)
459 {
460 	__irq_exit_rcu();
461 	rcu_irq_exit();
462 	 /* must be last! */
463 	lockdep_hardirq_exit();
464 }
465 
466 /*
467  * This function must run with irqs disabled!
468  */
raise_softirq_irqoff(unsigned int nr)469 inline void raise_softirq_irqoff(unsigned int nr)
470 {
471 	__raise_softirq_irqoff(nr);
472 
473 	/*
474 	 * If we're in an interrupt or softirq, we're done
475 	 * (this also catches softirq-disabled code). We will
476 	 * actually run the softirq once we return from
477 	 * the irq or softirq.
478 	 *
479 	 * Otherwise we wake up ksoftirqd to make sure we
480 	 * schedule the softirq soon.
481 	 */
482 	if (!in_interrupt())
483 		wakeup_softirqd();
484 }
485 
raise_softirq(unsigned int nr)486 void raise_softirq(unsigned int nr)
487 {
488 	unsigned long flags;
489 
490 	local_irq_save(flags);
491 	raise_softirq_irqoff(nr);
492 	local_irq_restore(flags);
493 }
494 
__raise_softirq_irqoff(unsigned int nr)495 void __raise_softirq_irqoff(unsigned int nr)
496 {
497 	lockdep_assert_irqs_disabled();
498 	trace_softirq_raise(nr);
499 	or_softirq_pending(1UL << nr);
500 }
501 
open_softirq(int nr,void (* action)(struct softirq_action *))502 void open_softirq(int nr, void (*action)(struct softirq_action *))
503 {
504 	softirq_vec[nr].action = action;
505 }
506 
507 /*
508  * Tasklets
509  */
510 struct tasklet_head {
511 	struct tasklet_struct *head;
512 	struct tasklet_struct **tail;
513 };
514 
515 static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
516 static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
517 
__tasklet_schedule_common(struct tasklet_struct * t,struct tasklet_head __percpu * headp,unsigned int softirq_nr)518 static void __tasklet_schedule_common(struct tasklet_struct *t,
519 				      struct tasklet_head __percpu *headp,
520 				      unsigned int softirq_nr)
521 {
522 	struct tasklet_head *head;
523 	unsigned long flags;
524 
525 	local_irq_save(flags);
526 	head = this_cpu_ptr(headp);
527 	t->next = NULL;
528 	*head->tail = t;
529 	head->tail = &(t->next);
530 	raise_softirq_irqoff(softirq_nr);
531 	local_irq_restore(flags);
532 }
533 
__tasklet_schedule(struct tasklet_struct * t)534 void __tasklet_schedule(struct tasklet_struct *t)
535 {
536 	__tasklet_schedule_common(t, &tasklet_vec,
537 				  TASKLET_SOFTIRQ);
538 }
539 EXPORT_SYMBOL(__tasklet_schedule);
540 
__tasklet_hi_schedule(struct tasklet_struct * t)541 void __tasklet_hi_schedule(struct tasklet_struct *t)
542 {
543 	__tasklet_schedule_common(t, &tasklet_hi_vec,
544 				  HI_SOFTIRQ);
545 }
546 EXPORT_SYMBOL(__tasklet_hi_schedule);
547 
tasklet_action_common(struct softirq_action * a,struct tasklet_head * tl_head,unsigned int softirq_nr)548 static void tasklet_action_common(struct softirq_action *a,
549 				  struct tasklet_head *tl_head,
550 				  unsigned int softirq_nr)
551 {
552 	struct tasklet_struct *list;
553 
554 	local_irq_disable();
555 	list = tl_head->head;
556 	tl_head->head = NULL;
557 	tl_head->tail = &tl_head->head;
558 	local_irq_enable();
559 
560 	while (list) {
561 		struct tasklet_struct *t = list;
562 
563 		list = list->next;
564 
565 		if (tasklet_trylock(t)) {
566 			if (!atomic_read(&t->count)) {
567 				if (!test_and_clear_bit(TASKLET_STATE_SCHED,
568 							&t->state))
569 					BUG();
570 				if (t->use_callback) {
571 					trace_tasklet_entry(t->callback);
572 					t->callback(t);
573 					trace_tasklet_exit(t->callback);
574 				} else {
575 					trace_tasklet_entry(t->func);
576 					t->func(t->data);
577 					trace_tasklet_exit(t->func);
578 				}
579 				tasklet_unlock(t);
580 				continue;
581 			}
582 			tasklet_unlock(t);
583 		}
584 
585 		local_irq_disable();
586 		t->next = NULL;
587 		*tl_head->tail = t;
588 		tl_head->tail = &t->next;
589 		__raise_softirq_irqoff(softirq_nr);
590 		local_irq_enable();
591 	}
592 }
593 
tasklet_action(struct softirq_action * a)594 static __latent_entropy void tasklet_action(struct softirq_action *a)
595 {
596 	tasklet_action_common(a, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
597 }
598 
tasklet_hi_action(struct softirq_action * a)599 static __latent_entropy void tasklet_hi_action(struct softirq_action *a)
600 {
601 	tasklet_action_common(a, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
602 }
603 
tasklet_setup(struct tasklet_struct * t,void (* callback)(struct tasklet_struct *))604 void tasklet_setup(struct tasklet_struct *t,
605 		   void (*callback)(struct tasklet_struct *))
606 {
607 	t->next = NULL;
608 	t->state = 0;
609 	atomic_set(&t->count, 0);
610 	t->callback = callback;
611 	t->use_callback = true;
612 	t->data = 0;
613 }
614 EXPORT_SYMBOL(tasklet_setup);
615 
tasklet_init(struct tasklet_struct * t,void (* func)(unsigned long),unsigned long data)616 void tasklet_init(struct tasklet_struct *t,
617 		  void (*func)(unsigned long), unsigned long data)
618 {
619 	t->next = NULL;
620 	t->state = 0;
621 	atomic_set(&t->count, 0);
622 	t->func = func;
623 	t->use_callback = false;
624 	t->data = data;
625 }
626 EXPORT_SYMBOL(tasklet_init);
627 
tasklet_kill(struct tasklet_struct * t)628 void tasklet_kill(struct tasklet_struct *t)
629 {
630 	if (in_interrupt())
631 		pr_notice("Attempt to kill tasklet from interrupt\n");
632 
633 	while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
634 		do {
635 			yield();
636 		} while (test_bit(TASKLET_STATE_SCHED, &t->state));
637 	}
638 	tasklet_unlock_wait(t);
639 	clear_bit(TASKLET_STATE_SCHED, &t->state);
640 }
641 EXPORT_SYMBOL(tasklet_kill);
642 
softirq_init(void)643 void __init softirq_init(void)
644 {
645 	int cpu;
646 
647 	for_each_possible_cpu(cpu) {
648 		per_cpu(tasklet_vec, cpu).tail =
649 			&per_cpu(tasklet_vec, cpu).head;
650 		per_cpu(tasklet_hi_vec, cpu).tail =
651 			&per_cpu(tasklet_hi_vec, cpu).head;
652 	}
653 
654 	open_softirq(TASKLET_SOFTIRQ, tasklet_action);
655 	open_softirq(HI_SOFTIRQ, tasklet_hi_action);
656 }
657 
ksoftirqd_should_run(unsigned int cpu)658 static int ksoftirqd_should_run(unsigned int cpu)
659 {
660 	return local_softirq_pending();
661 }
662 
run_ksoftirqd(unsigned int cpu)663 static void run_ksoftirqd(unsigned int cpu)
664 {
665 	local_irq_disable();
666 	if (local_softirq_pending()) {
667 		/*
668 		 * We can safely run softirq on inline stack, as we are not deep
669 		 * in the task stack here.
670 		 */
671 		__do_softirq();
672 		local_irq_enable();
673 		cond_resched();
674 		return;
675 	}
676 	local_irq_enable();
677 }
678 
679 #ifdef CONFIG_HOTPLUG_CPU
680 /*
681  * tasklet_kill_immediate is called to remove a tasklet which can already be
682  * scheduled for execution on @cpu.
683  *
684  * Unlike tasklet_kill, this function removes the tasklet
685  * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
686  *
687  * When this function is called, @cpu must be in the CPU_DEAD state.
688  */
tasklet_kill_immediate(struct tasklet_struct * t,unsigned int cpu)689 void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
690 {
691 	struct tasklet_struct **i;
692 
693 	BUG_ON(cpu_online(cpu));
694 	BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
695 
696 	if (!test_bit(TASKLET_STATE_SCHED, &t->state))
697 		return;
698 
699 	/* CPU is dead, so no lock needed. */
700 	for (i = &per_cpu(tasklet_vec, cpu).head; *i; i = &(*i)->next) {
701 		if (*i == t) {
702 			*i = t->next;
703 			/* If this was the tail element, move the tail ptr */
704 			if (*i == NULL)
705 				per_cpu(tasklet_vec, cpu).tail = i;
706 			return;
707 		}
708 	}
709 	BUG();
710 }
711 
takeover_tasklets(unsigned int cpu)712 static int takeover_tasklets(unsigned int cpu)
713 {
714 	/* CPU is dead, so no lock needed. */
715 	local_irq_disable();
716 
717 	/* Find end, append list for that CPU. */
718 	if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
719 		*__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
720 		__this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
721 		per_cpu(tasklet_vec, cpu).head = NULL;
722 		per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
723 	}
724 	raise_softirq_irqoff(TASKLET_SOFTIRQ);
725 
726 	if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
727 		*__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
728 		__this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
729 		per_cpu(tasklet_hi_vec, cpu).head = NULL;
730 		per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
731 	}
732 	raise_softirq_irqoff(HI_SOFTIRQ);
733 
734 	local_irq_enable();
735 	return 0;
736 }
737 #else
738 #define takeover_tasklets	NULL
739 #endif /* CONFIG_HOTPLUG_CPU */
740 
741 static struct smp_hotplug_thread softirq_threads = {
742 	.store			= &ksoftirqd,
743 	.thread_should_run	= ksoftirqd_should_run,
744 	.thread_fn		= run_ksoftirqd,
745 	.thread_comm		= "ksoftirqd/%u",
746 };
747 
spawn_ksoftirqd(void)748 static __init int spawn_ksoftirqd(void)
749 {
750 	cpuhp_setup_state_nocalls(CPUHP_SOFTIRQ_DEAD, "softirq:dead", NULL,
751 				  takeover_tasklets);
752 	BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
753 
754 	return 0;
755 }
756 early_initcall(spawn_ksoftirqd);
757 
758 /*
759  * [ These __weak aliases are kept in a separate compilation unit, so that
760  *   GCC does not inline them incorrectly. ]
761  */
762 
early_irq_init(void)763 int __init __weak early_irq_init(void)
764 {
765 	return 0;
766 }
767 
arch_probe_nr_irqs(void)768 int __init __weak arch_probe_nr_irqs(void)
769 {
770 	return NR_IRQS_LEGACY;
771 }
772 
arch_early_irq_init(void)773 int __init __weak arch_early_irq_init(void)
774 {
775 	return 0;
776 }
777 
arch_dynirq_lower_bound(unsigned int from)778 unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
779 {
780 	return from;
781 }
782