• 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/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 
331 #ifdef CONFIG_RT_SOFTINT_OPTIMIZATION
332 	if (pending | deferred)
333 		wakeup_softirqd();
334 #endif
335 	lockdep_softirq_end(in_hardirq);
336 	account_irq_exit_time(current);
337 	__local_bh_enable(SOFTIRQ_OFFSET);
338 	WARN_ON_ONCE(in_interrupt());
339 	current_restore_flags(old_flags, PF_MEMALLOC);
340 }
341 
do_softirq(void)342 asmlinkage __visible void do_softirq(void)
343 {
344 	__u32 pending;
345 	unsigned long flags;
346 
347 	if (in_interrupt())
348 		return;
349 
350 	local_irq_save(flags);
351 
352 	pending = local_softirq_pending();
353 
354 	if (pending)
355 		do_softirq_own_stack();
356 
357 	local_irq_restore(flags);
358 }
359 
360 /**
361  * irq_enter_rcu - Enter an interrupt context with RCU watching
362  */
irq_enter_rcu(void)363 void irq_enter_rcu(void)
364 {
365 	if (is_idle_task(current) && !in_interrupt()) {
366 		/*
367 		 * Prevent raise_softirq from needlessly waking up ksoftirqd
368 		 * here, as softirq will be serviced on return from interrupt.
369 		 */
370 		local_bh_disable();
371 		tick_irq_enter();
372 		_local_bh_enable();
373 	}
374 	__irq_enter();
375 }
376 
377 /**
378  * irq_enter - Enter an interrupt context including RCU update
379  */
irq_enter(void)380 void irq_enter(void)
381 {
382 	rcu_irq_enter();
383 	irq_enter_rcu();
384 }
385 
invoke_softirq(void)386 static inline void invoke_softirq(void)
387 {
388 	if (!force_irqthreads) {
389 #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
390 		/*
391 		 * We can safely execute softirq on the current stack if
392 		 * it is the irq stack, because it should be near empty
393 		 * at this stage.
394 		 */
395 		__do_softirq();
396 #else
397 		/*
398 		 * Otherwise, irq_exit() is called on the task stack that can
399 		 * be potentially deep already. So call softirq in its own stack
400 		 * to prevent from any overrun.
401 		 */
402 		do_softirq_own_stack();
403 #endif
404 	} else {
405 		wakeup_softirqd();
406 	}
407 }
408 
tick_irq_exit(void)409 static inline void tick_irq_exit(void)
410 {
411 #ifdef CONFIG_NO_HZ_COMMON
412 	int cpu = smp_processor_id();
413 
414 	/* Make sure that timer wheel updates are propagated */
415 	if ((idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
416 		if (!in_irq())
417 			tick_nohz_irq_exit();
418 	}
419 #endif
420 }
421 
__irq_exit_rcu(void)422 static inline void __irq_exit_rcu(void)
423 {
424 #ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
425 	local_irq_disable();
426 #else
427 	lockdep_assert_irqs_disabled();
428 #endif
429 	account_irq_exit_time(current);
430 	preempt_count_sub(HARDIRQ_OFFSET);
431 	if (!in_interrupt() && local_softirq_pending())
432 		invoke_softirq();
433 
434 	tick_irq_exit();
435 }
436 
437 /**
438  * irq_exit_rcu() - Exit an interrupt context without updating RCU
439  *
440  * Also processes softirqs if needed and possible.
441  */
irq_exit_rcu(void)442 void irq_exit_rcu(void)
443 {
444 	__irq_exit_rcu();
445 	 /* must be last! */
446 	lockdep_hardirq_exit();
447 }
448 
449 /**
450  * irq_exit - Exit an interrupt context, update RCU and lockdep
451  *
452  * Also processes softirqs if needed and possible.
453  */
irq_exit(void)454 void irq_exit(void)
455 {
456 	__irq_exit_rcu();
457 	rcu_irq_exit();
458 	 /* must be last! */
459 	lockdep_hardirq_exit();
460 }
461 
462 /*
463  * This function must run with irqs disabled!
464  */
raise_softirq_irqoff(unsigned int nr)465 inline void raise_softirq_irqoff(unsigned int nr)
466 {
467 	__raise_softirq_irqoff(nr);
468 
469 	/*
470 	 * If we're in an interrupt or softirq, we're done
471 	 * (this also catches softirq-disabled code). We will
472 	 * actually run the softirq once we return from
473 	 * the irq or softirq.
474 	 *
475 	 * Otherwise we wake up ksoftirqd to make sure we
476 	 * schedule the softirq soon.
477 	 */
478 	if (!in_interrupt())
479 		wakeup_softirqd();
480 }
481 
raise_softirq(unsigned int nr)482 void raise_softirq(unsigned int nr)
483 {
484 	unsigned long flags;
485 
486 	local_irq_save(flags);
487 	raise_softirq_irqoff(nr);
488 	local_irq_restore(flags);
489 }
490 
__raise_softirq_irqoff(unsigned int nr)491 void __raise_softirq_irqoff(unsigned int nr)
492 {
493 	lockdep_assert_irqs_disabled();
494 	trace_softirq_raise(nr);
495 	or_softirq_pending(1UL << nr);
496 }
497 
open_softirq(int nr,void (* action)(struct softirq_action *))498 void open_softirq(int nr, void (*action)(struct softirq_action *))
499 {
500 	softirq_vec[nr].action = action;
501 }
502 
503 /*
504  * Tasklets
505  */
506 struct tasklet_head {
507 	struct tasklet_struct *head;
508 	struct tasklet_struct **tail;
509 };
510 
511 static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
512 static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
513 
__tasklet_schedule_common(struct tasklet_struct * t,struct tasklet_head __percpu * headp,unsigned int softirq_nr)514 static void __tasklet_schedule_common(struct tasklet_struct *t,
515 				      struct tasklet_head __percpu *headp,
516 				      unsigned int softirq_nr)
517 {
518 	struct tasklet_head *head;
519 	unsigned long flags;
520 
521 	local_irq_save(flags);
522 	head = this_cpu_ptr(headp);
523 	t->next = NULL;
524 	*head->tail = t;
525 	head->tail = &(t->next);
526 	raise_softirq_irqoff(softirq_nr);
527 	local_irq_restore(flags);
528 }
529 
__tasklet_schedule(struct tasklet_struct * t)530 void __tasklet_schedule(struct tasklet_struct *t)
531 {
532 	__tasklet_schedule_common(t, &tasklet_vec,
533 				  TASKLET_SOFTIRQ);
534 }
535 EXPORT_SYMBOL(__tasklet_schedule);
536 
__tasklet_hi_schedule(struct tasklet_struct * t)537 void __tasklet_hi_schedule(struct tasklet_struct *t)
538 {
539 	__tasklet_schedule_common(t, &tasklet_hi_vec,
540 				  HI_SOFTIRQ);
541 }
542 EXPORT_SYMBOL(__tasklet_hi_schedule);
543 
tasklet_action_common(struct softirq_action * a,struct tasklet_head * tl_head,unsigned int softirq_nr)544 static void tasklet_action_common(struct softirq_action *a,
545 				  struct tasklet_head *tl_head,
546 				  unsigned int softirq_nr)
547 {
548 	struct tasklet_struct *list;
549 
550 	local_irq_disable();
551 	list = tl_head->head;
552 	tl_head->head = NULL;
553 	tl_head->tail = &tl_head->head;
554 	local_irq_enable();
555 
556 	while (list) {
557 		struct tasklet_struct *t = list;
558 
559 		list = list->next;
560 
561 		if (tasklet_trylock(t)) {
562 			if (!atomic_read(&t->count)) {
563 				if (!test_and_clear_bit(TASKLET_STATE_SCHED,
564 							&t->state))
565 					BUG();
566 				if (t->use_callback) {
567 					trace_tasklet_entry(t->callback);
568 					t->callback(t);
569 					trace_tasklet_exit(t->callback);
570 				} else {
571 					trace_tasklet_entry(t->func);
572 					t->func(t->data);
573 					trace_tasklet_exit(t->func);
574 				}
575 				tasklet_unlock(t);
576 				continue;
577 			}
578 			tasklet_unlock(t);
579 		}
580 
581 		local_irq_disable();
582 		t->next = NULL;
583 		*tl_head->tail = t;
584 		tl_head->tail = &t->next;
585 		__raise_softirq_irqoff(softirq_nr);
586 		local_irq_enable();
587 	}
588 }
589 
tasklet_action(struct softirq_action * a)590 static __latent_entropy void tasklet_action(struct softirq_action *a)
591 {
592 	tasklet_action_common(a, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
593 }
594 
tasklet_hi_action(struct softirq_action * a)595 static __latent_entropy void tasklet_hi_action(struct softirq_action *a)
596 {
597 	tasklet_action_common(a, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
598 }
599 
tasklet_setup(struct tasklet_struct * t,void (* callback)(struct tasklet_struct *))600 void tasklet_setup(struct tasklet_struct *t,
601 		   void (*callback)(struct tasklet_struct *))
602 {
603 	t->next = NULL;
604 	t->state = 0;
605 	atomic_set(&t->count, 0);
606 	t->callback = callback;
607 	t->use_callback = true;
608 	t->data = 0;
609 }
610 EXPORT_SYMBOL(tasklet_setup);
611 
tasklet_init(struct tasklet_struct * t,void (* func)(unsigned long),unsigned long data)612 void tasklet_init(struct tasklet_struct *t,
613 		  void (*func)(unsigned long), unsigned long data)
614 {
615 	t->next = NULL;
616 	t->state = 0;
617 	atomic_set(&t->count, 0);
618 	t->func = func;
619 	t->use_callback = false;
620 	t->data = data;
621 }
622 EXPORT_SYMBOL(tasklet_init);
623 
tasklet_kill(struct tasklet_struct * t)624 void tasklet_kill(struct tasklet_struct *t)
625 {
626 	if (in_interrupt())
627 		pr_notice("Attempt to kill tasklet from interrupt\n");
628 
629 	while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
630 		do {
631 			yield();
632 		} while (test_bit(TASKLET_STATE_SCHED, &t->state));
633 	}
634 	tasklet_unlock_wait(t);
635 	clear_bit(TASKLET_STATE_SCHED, &t->state);
636 }
637 EXPORT_SYMBOL(tasklet_kill);
638 
softirq_init(void)639 void __init softirq_init(void)
640 {
641 	int cpu;
642 
643 	for_each_possible_cpu(cpu) {
644 		per_cpu(tasklet_vec, cpu).tail =
645 			&per_cpu(tasklet_vec, cpu).head;
646 		per_cpu(tasklet_hi_vec, cpu).tail =
647 			&per_cpu(tasklet_hi_vec, cpu).head;
648 	}
649 
650 	open_softirq(TASKLET_SOFTIRQ, tasklet_action);
651 	open_softirq(HI_SOFTIRQ, tasklet_hi_action);
652 }
653 
ksoftirqd_should_run(unsigned int cpu)654 static int ksoftirqd_should_run(unsigned int cpu)
655 {
656 	return local_softirq_pending();
657 }
658 
run_ksoftirqd(unsigned int cpu)659 static void run_ksoftirqd(unsigned int cpu)
660 {
661 	local_irq_disable();
662 	if (local_softirq_pending()) {
663 		/*
664 		 * We can safely run softirq on inline stack, as we are not deep
665 		 * in the task stack here.
666 		 */
667 		__do_softirq();
668 		local_irq_enable();
669 		cond_resched();
670 		return;
671 	}
672 	local_irq_enable();
673 }
674 
675 #ifdef CONFIG_HOTPLUG_CPU
676 /*
677  * tasklet_kill_immediate is called to remove a tasklet which can already be
678  * scheduled for execution on @cpu.
679  *
680  * Unlike tasklet_kill, this function removes the tasklet
681  * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
682  *
683  * When this function is called, @cpu must be in the CPU_DEAD state.
684  */
tasklet_kill_immediate(struct tasklet_struct * t,unsigned int cpu)685 void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
686 {
687 	struct tasklet_struct **i;
688 
689 	BUG_ON(cpu_online(cpu));
690 	BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
691 
692 	if (!test_bit(TASKLET_STATE_SCHED, &t->state))
693 		return;
694 
695 	/* CPU is dead, so no lock needed. */
696 	for (i = &per_cpu(tasklet_vec, cpu).head; *i; i = &(*i)->next) {
697 		if (*i == t) {
698 			*i = t->next;
699 			/* If this was the tail element, move the tail ptr */
700 			if (*i == NULL)
701 				per_cpu(tasklet_vec, cpu).tail = i;
702 			return;
703 		}
704 	}
705 	BUG();
706 }
707 
takeover_tasklets(unsigned int cpu)708 static int takeover_tasklets(unsigned int cpu)
709 {
710 	/* CPU is dead, so no lock needed. */
711 	local_irq_disable();
712 
713 	/* Find end, append list for that CPU. */
714 	if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
715 		*__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
716 		__this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
717 		per_cpu(tasklet_vec, cpu).head = NULL;
718 		per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
719 	}
720 	raise_softirq_irqoff(TASKLET_SOFTIRQ);
721 
722 	if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
723 		*__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
724 		__this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
725 		per_cpu(tasklet_hi_vec, cpu).head = NULL;
726 		per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
727 	}
728 	raise_softirq_irqoff(HI_SOFTIRQ);
729 
730 	local_irq_enable();
731 	return 0;
732 }
733 #else
734 #define takeover_tasklets	NULL
735 #endif /* CONFIG_HOTPLUG_CPU */
736 
737 static struct smp_hotplug_thread softirq_threads = {
738 	.store			= &ksoftirqd,
739 	.thread_should_run	= ksoftirqd_should_run,
740 	.thread_fn		= run_ksoftirqd,
741 	.thread_comm		= "ksoftirqd/%u",
742 };
743 
spawn_ksoftirqd(void)744 static __init int spawn_ksoftirqd(void)
745 {
746 	cpuhp_setup_state_nocalls(CPUHP_SOFTIRQ_DEAD, "softirq:dead", NULL,
747 				  takeover_tasklets);
748 	BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
749 
750 	return 0;
751 }
752 early_initcall(spawn_ksoftirqd);
753 
754 /*
755  * [ These __weak aliases are kept in a separate compilation unit, so that
756  *   GCC does not inline them incorrectly. ]
757  */
758 
early_irq_init(void)759 int __init __weak early_irq_init(void)
760 {
761 	return 0;
762 }
763 
arch_probe_nr_irqs(void)764 int __init __weak arch_probe_nr_irqs(void)
765 {
766 	return NR_IRQS_LEGACY;
767 }
768 
arch_early_irq_init(void)769 int __init __weak arch_early_irq_init(void)
770 {
771 	return 0;
772 }
773 
arch_dynirq_lower_bound(unsigned int from)774 unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
775 {
776 	return from;
777 }
778