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