1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/kernel/softirq.c
5 * Copyright (C) 1992 Linus Torvalds
7 * Rewritten. Old one was good in 2.2, but in 2.3 it was immoral. --ANK (990903)
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/export.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/interrupt.h>
15 #include <linux/init.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 #include <linux/wait_bit.h>
30 #include <asm/softirq_stack.h>
32 #define CREATE_TRACE_POINTS
33 #include <trace/events/irq.h>
36 - No shared variables, all the data are CPU local.
37 - If a softirq needs serialization, let it serialize itself
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
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.
53 #ifndef __ARCH_IRQ_STAT
54 DEFINE_PER_CPU_ALIGNED(irq_cpustat_t, irq_stat);
55 EXPORT_PER_CPU_SYMBOL(irq_stat);
58 static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
60 DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
62 const char * const softirq_to_name[NR_SOFTIRQS] = {
63 "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
64 "TASKLET", "SCHED", "HRTIMER", "RCU"
68 * we cannot loop indefinitely here to avoid userspace starvation,
69 * but we also don't want to introduce a worst case 1/HZ latency
70 * to the pending events, so lets the scheduler to balance
71 * the softirq load for us.
73 static void wakeup_softirqd(void)
75 /* Interrupts are disabled: no need to stop preemption */
76 struct task_struct *tsk = __this_cpu_read(ksoftirqd);
78 if (tsk && tsk->state != TASK_RUNNING)
83 * If ksoftirqd is scheduled, we do not want to process pending softirqs
84 * right now. Let ksoftirqd handle this at its own rate, to get fairness,
85 * unless we're doing some of the synchronous softirqs.
87 #define SOFTIRQ_NOW_MASK ((1 << HI_SOFTIRQ) | (1 << TASKLET_SOFTIRQ))
88 static bool ksoftirqd_running(unsigned long pending)
90 struct task_struct *tsk = __this_cpu_read(ksoftirqd);
92 if (pending & SOFTIRQ_NOW_MASK)
94 return tsk && (tsk->state == TASK_RUNNING) &&
95 !__kthread_should_park(tsk);
98 #ifdef CONFIG_TRACE_IRQFLAGS
99 DEFINE_PER_CPU(int, hardirqs_enabled);
100 DEFINE_PER_CPU(int, hardirq_context);
101 EXPORT_PER_CPU_SYMBOL_GPL(hardirqs_enabled);
102 EXPORT_PER_CPU_SYMBOL_GPL(hardirq_context);
106 * preempt_count and SOFTIRQ_OFFSET usage:
107 * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
108 * softirq processing.
109 * - preempt_count is changed by SOFTIRQ_DISABLE_OFFSET (= 2 * SOFTIRQ_OFFSET)
110 * on local_bh_disable or local_bh_enable.
111 * This lets us distinguish between whether we are currently processing
112 * softirq and whether we just have bh disabled.
115 #ifdef CONFIG_TRACE_IRQFLAGS
117 * This is for softirq.c-internal use, where hardirqs are disabled
120 void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
124 WARN_ON_ONCE(in_irq());
126 raw_local_irq_save(flags);
128 * The preempt tracer hooks into preempt_count_add and will break
129 * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET
130 * is set and before current->softirq_enabled is cleared.
131 * We must manually increment preempt_count here and manually
132 * call the trace_preempt_off later.
134 __preempt_count_add(cnt);
136 * Were softirqs turned off above:
138 if (softirq_count() == (cnt & SOFTIRQ_MASK))
139 lockdep_softirqs_off(ip);
140 raw_local_irq_restore(flags);
142 if (preempt_count() == cnt) {
143 #ifdef CONFIG_DEBUG_PREEMPT
144 current->preempt_disable_ip = get_lock_parent_ip();
146 trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
149 EXPORT_SYMBOL(__local_bh_disable_ip);
150 #endif /* CONFIG_TRACE_IRQFLAGS */
152 static void __local_bh_enable(unsigned int cnt)
154 lockdep_assert_irqs_disabled();
156 if (preempt_count() == cnt)
157 trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
159 if (softirq_count() == (cnt & SOFTIRQ_MASK))
160 lockdep_softirqs_on(_RET_IP_);
162 __preempt_count_sub(cnt);
166 * Special-case - softirqs can safely be enabled by __do_softirq(),
167 * without processing still-pending softirqs:
169 void _local_bh_enable(void)
171 WARN_ON_ONCE(in_irq());
172 __local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
174 EXPORT_SYMBOL(_local_bh_enable);
176 void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
178 WARN_ON_ONCE(in_irq());
179 lockdep_assert_irqs_enabled();
180 #ifdef CONFIG_TRACE_IRQFLAGS
184 * Are softirqs going to be turned on now:
186 if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
187 lockdep_softirqs_on(ip);
189 * Keep preemption disabled until we are done with
190 * softirq processing:
192 __preempt_count_sub(cnt - 1);
194 if (unlikely(!in_interrupt() && local_softirq_pending())) {
196 * Run softirq if any pending. And do it in its own stack
197 * as we may be calling this deep in a task call stack already.
203 #ifdef CONFIG_TRACE_IRQFLAGS
206 preempt_check_resched();
208 EXPORT_SYMBOL(__local_bh_enable_ip);
210 static inline void invoke_softirq(void)
212 if (ksoftirqd_running(local_softirq_pending()))
215 if (!force_irqthreads) {
216 #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
218 * We can safely execute softirq on the current stack if
219 * it is the irq stack, because it should be near empty
225 * Otherwise, irq_exit() is called on the task stack that can
226 * be potentially deep already. So call softirq in its own stack
227 * to prevent from any overrun.
229 do_softirq_own_stack();
236 asmlinkage __visible void do_softirq(void)
244 local_irq_save(flags);
246 pending = local_softirq_pending();
248 if (pending && !ksoftirqd_running(pending))
249 do_softirq_own_stack();
251 local_irq_restore(flags);
255 * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
256 * but break the loop if need_resched() is set or after 2 ms.
257 * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
258 * certain cases, such as stop_machine(), jiffies may cease to
259 * increment and so we need the MAX_SOFTIRQ_RESTART limit as
260 * well to make sure we eventually return from this method.
262 * These limits have been established via experimentation.
263 * The two things to balance is latency against fairness -
264 * we want to handle softirqs as soon as possible, but they
265 * should not be able to lock up the box.
267 #define MAX_SOFTIRQ_TIME msecs_to_jiffies(2)
268 #define MAX_SOFTIRQ_RESTART 10
270 #ifdef CONFIG_TRACE_IRQFLAGS
272 * When we run softirqs from irq_exit() and thus on the hardirq stack we need
273 * to keep the lockdep irq context tracking as tight as possible in order to
274 * not miss-qualify lock contexts and miss possible deadlocks.
277 static inline bool lockdep_softirq_start(void)
279 bool in_hardirq = false;
281 if (lockdep_hardirq_context()) {
283 lockdep_hardirq_exit();
286 lockdep_softirq_enter();
291 static inline void lockdep_softirq_end(bool in_hardirq)
293 lockdep_softirq_exit();
296 lockdep_hardirq_enter();
299 static inline bool lockdep_softirq_start(void) { return false; }
300 static inline void lockdep_softirq_end(bool in_hardirq) { }
303 asmlinkage __visible void __softirq_entry __do_softirq(void)
305 unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
306 unsigned long old_flags = current->flags;
307 int max_restart = MAX_SOFTIRQ_RESTART;
308 struct softirq_action *h;
314 * Mask out PF_MEMALLOC as the current task context is borrowed for the
315 * softirq. A softirq handled, such as network RX, might set PF_MEMALLOC
316 * again if the socket is related to swapping.
318 current->flags &= ~PF_MEMALLOC;
320 pending = local_softirq_pending();
322 __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
323 in_hardirq = lockdep_softirq_start();
324 account_softirq_enter(current);
327 /* Reset the pending bitmask before enabling irqs */
328 set_softirq_pending(0);
334 while ((softirq_bit = ffs(pending))) {
338 h += softirq_bit - 1;
340 vec_nr = h - softirq_vec;
341 prev_count = preempt_count();
343 kstat_incr_softirqs_this_cpu(vec_nr);
345 trace_softirq_entry(vec_nr);
347 trace_softirq_exit(vec_nr);
348 if (unlikely(prev_count != preempt_count())) {
349 pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
350 vec_nr, softirq_to_name[vec_nr], h->action,
351 prev_count, preempt_count());
352 preempt_count_set(prev_count);
355 pending >>= softirq_bit;
358 if (__this_cpu_read(ksoftirqd) == current)
362 pending = local_softirq_pending();
364 if (time_before(jiffies, end) && !need_resched() &&
371 account_softirq_exit(current);
372 lockdep_softirq_end(in_hardirq);
373 __local_bh_enable(SOFTIRQ_OFFSET);
374 WARN_ON_ONCE(in_interrupt());
375 current_restore_flags(old_flags, PF_MEMALLOC);
379 * irq_enter_rcu - Enter an interrupt context with RCU watching
381 void irq_enter_rcu(void)
385 if (is_idle_task(current) && (irq_count() == HARDIRQ_OFFSET))
388 account_hardirq_enter(current);
392 * irq_enter - Enter an interrupt context including RCU update
400 static inline void tick_irq_exit(void)
402 #ifdef CONFIG_NO_HZ_COMMON
403 int cpu = smp_processor_id();
405 /* Make sure that timer wheel updates are propagated */
406 if ((idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
408 tick_nohz_irq_exit();
413 static inline void __irq_exit_rcu(void)
415 #ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
418 lockdep_assert_irqs_disabled();
420 account_hardirq_exit(current);
421 preempt_count_sub(HARDIRQ_OFFSET);
422 if (!in_interrupt() && local_softirq_pending())
429 * irq_exit_rcu() - Exit an interrupt context without updating RCU
431 * Also processes softirqs if needed and possible.
433 void irq_exit_rcu(void)
437 lockdep_hardirq_exit();
441 * irq_exit - Exit an interrupt context, update RCU and lockdep
443 * Also processes softirqs if needed and possible.
450 lockdep_hardirq_exit();
454 * This function must run with irqs disabled!
456 inline void raise_softirq_irqoff(unsigned int nr)
458 __raise_softirq_irqoff(nr);
461 * If we're in an interrupt or softirq, we're done
462 * (this also catches softirq-disabled code). We will
463 * actually run the softirq once we return from
464 * the irq or softirq.
466 * Otherwise we wake up ksoftirqd to make sure we
467 * schedule the softirq soon.
473 void raise_softirq(unsigned int nr)
477 local_irq_save(flags);
478 raise_softirq_irqoff(nr);
479 local_irq_restore(flags);
482 void __raise_softirq_irqoff(unsigned int nr)
484 lockdep_assert_irqs_disabled();
485 trace_softirq_raise(nr);
486 or_softirq_pending(1UL << nr);
489 void open_softirq(int nr, void (*action)(struct softirq_action *))
491 softirq_vec[nr].action = action;
497 struct tasklet_head {
498 struct tasklet_struct *head;
499 struct tasklet_struct **tail;
502 static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
503 static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
505 static void __tasklet_schedule_common(struct tasklet_struct *t,
506 struct tasklet_head __percpu *headp,
507 unsigned int softirq_nr)
509 struct tasklet_head *head;
512 local_irq_save(flags);
513 head = this_cpu_ptr(headp);
516 head->tail = &(t->next);
517 raise_softirq_irqoff(softirq_nr);
518 local_irq_restore(flags);
521 void __tasklet_schedule(struct tasklet_struct *t)
523 __tasklet_schedule_common(t, &tasklet_vec,
526 EXPORT_SYMBOL(__tasklet_schedule);
528 void __tasklet_hi_schedule(struct tasklet_struct *t)
530 __tasklet_schedule_common(t, &tasklet_hi_vec,
533 EXPORT_SYMBOL(__tasklet_hi_schedule);
535 static bool tasklet_clear_sched(struct tasklet_struct *t)
537 if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state)) {
538 wake_up_var(&t->state);
542 WARN_ONCE(1, "tasklet SCHED state not set: %s %pS\n",
543 t->use_callback ? "callback" : "func",
544 t->use_callback ? (void *)t->callback : (void *)t->func);
549 static void tasklet_action_common(struct softirq_action *a,
550 struct tasklet_head *tl_head,
551 unsigned int softirq_nr)
553 struct tasklet_struct *list;
556 list = tl_head->head;
557 tl_head->head = NULL;
558 tl_head->tail = &tl_head->head;
562 struct tasklet_struct *t = list;
566 if (tasklet_trylock(t)) {
567 if (!atomic_read(&t->count)) {
568 if (tasklet_clear_sched(t)) {
583 tl_head->tail = &t->next;
584 __raise_softirq_irqoff(softirq_nr);
589 static __latent_entropy void tasklet_action(struct softirq_action *a)
591 tasklet_action_common(a, this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
594 static __latent_entropy void tasklet_hi_action(struct softirq_action *a)
596 tasklet_action_common(a, this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
599 void tasklet_setup(struct tasklet_struct *t,
600 void (*callback)(struct tasklet_struct *))
604 atomic_set(&t->count, 0);
605 t->callback = callback;
606 t->use_callback = true;
609 EXPORT_SYMBOL(tasklet_setup);
611 void tasklet_init(struct tasklet_struct *t,
612 void (*func)(unsigned long), unsigned long data)
616 atomic_set(&t->count, 0);
618 t->use_callback = false;
621 EXPORT_SYMBOL(tasklet_init);
623 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
625 * Do not use in new code. Waiting for tasklets from atomic contexts is
626 * error prone and should be avoided.
628 void tasklet_unlock_spin_wait(struct tasklet_struct *t)
630 while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
631 if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
633 * Prevent a live lock when current preempted soft
634 * interrupt processing or prevents ksoftirqd from
635 * running. If the tasklet runs on a different CPU
636 * then this has no effect other than doing the BH
637 * disable/enable dance for nothing.
646 EXPORT_SYMBOL(tasklet_unlock_spin_wait);
649 void tasklet_kill(struct tasklet_struct *t)
652 pr_notice("Attempt to kill tasklet from interrupt\n");
654 while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
655 wait_var_event(&t->state, !test_bit(TASKLET_STATE_SCHED, &t->state));
657 tasklet_unlock_wait(t);
658 tasklet_clear_sched(t);
660 EXPORT_SYMBOL(tasklet_kill);
662 #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
663 void tasklet_unlock(struct tasklet_struct *t)
665 smp_mb__before_atomic();
666 clear_bit(TASKLET_STATE_RUN, &t->state);
667 smp_mb__after_atomic();
668 wake_up_var(&t->state);
670 EXPORT_SYMBOL_GPL(tasklet_unlock);
672 void tasklet_unlock_wait(struct tasklet_struct *t)
674 wait_var_event(&t->state, !test_bit(TASKLET_STATE_RUN, &t->state));
676 EXPORT_SYMBOL_GPL(tasklet_unlock_wait);
679 void __init softirq_init(void)
683 for_each_possible_cpu(cpu) {
684 per_cpu(tasklet_vec, cpu).tail =
685 &per_cpu(tasklet_vec, cpu).head;
686 per_cpu(tasklet_hi_vec, cpu).tail =
687 &per_cpu(tasklet_hi_vec, cpu).head;
690 open_softirq(TASKLET_SOFTIRQ, tasklet_action);
691 open_softirq(HI_SOFTIRQ, tasklet_hi_action);
694 static int ksoftirqd_should_run(unsigned int cpu)
696 return local_softirq_pending();
699 static void run_ksoftirqd(unsigned int cpu)
702 if (local_softirq_pending()) {
704 * We can safely run softirq on inline stack, as we are not deep
705 * in the task stack here.
715 #ifdef CONFIG_HOTPLUG_CPU
716 static int takeover_tasklets(unsigned int cpu)
718 /* CPU is dead, so no lock needed. */
721 /* Find end, append list for that CPU. */
722 if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
723 *__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
724 __this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
725 per_cpu(tasklet_vec, cpu).head = NULL;
726 per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
728 raise_softirq_irqoff(TASKLET_SOFTIRQ);
730 if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
731 *__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
732 __this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
733 per_cpu(tasklet_hi_vec, cpu).head = NULL;
734 per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
736 raise_softirq_irqoff(HI_SOFTIRQ);
742 #define takeover_tasklets NULL
743 #endif /* CONFIG_HOTPLUG_CPU */
745 static struct smp_hotplug_thread softirq_threads = {
747 .thread_should_run = ksoftirqd_should_run,
748 .thread_fn = run_ksoftirqd,
749 .thread_comm = "ksoftirqd/%u",
752 static __init int spawn_ksoftirqd(void)
754 cpuhp_setup_state_nocalls(CPUHP_SOFTIRQ_DEAD, "softirq:dead", NULL,
756 BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
760 early_initcall(spawn_ksoftirqd);
763 * [ These __weak aliases are kept in a separate compilation unit, so that
764 * GCC does not inline them incorrectly. ]
767 int __init __weak early_irq_init(void)
772 int __init __weak arch_probe_nr_irqs(void)
774 return NR_IRQS_LEGACY;
777 int __init __weak arch_early_irq_init(void)
782 unsigned int __weak arch_dynirq_lower_bound(unsigned int from)