Back to home page

LXR

 
 

    


0001 /*
0002  *  linux/kernel/softirq.c
0003  *
0004  *  Copyright (C) 1992 Linus Torvalds
0005  *
0006  *  Distribute under GPLv2.
0007  *
0008  *  Rewritten. Old one was good in 2.2, but in 2.3 it was immoral. --ANK (990903)
0009  */
0010 
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012 
0013 #include <linux/export.h>
0014 #include <linux/kernel_stat.h>
0015 #include <linux/interrupt.h>
0016 #include <linux/init.h>
0017 #include <linux/mm.h>
0018 #include <linux/notifier.h>
0019 #include <linux/percpu.h>
0020 #include <linux/cpu.h>
0021 #include <linux/freezer.h>
0022 #include <linux/kthread.h>
0023 #include <linux/rcupdate.h>
0024 #include <linux/ftrace.h>
0025 #include <linux/smp.h>
0026 #include <linux/smpboot.h>
0027 #include <linux/tick.h>
0028 #include <linux/irq.h>
0029 
0030 #define CREATE_TRACE_POINTS
0031 #include <trace/events/irq.h>
0032 
0033 /*
0034    - No shared variables, all the data are CPU local.
0035    - If a softirq needs serialization, let it serialize itself
0036      by its own spinlocks.
0037    - Even if softirq is serialized, only local cpu is marked for
0038      execution. Hence, we get something sort of weak cpu binding.
0039      Though it is still not clear, will it result in better locality
0040      or will not.
0041 
0042    Examples:
0043    - NET RX softirq. It is multithreaded and does not require
0044      any global serialization.
0045    - NET TX softirq. It kicks software netdevice queues, hence
0046      it is logically serialized per device, but this serialization
0047      is invisible to common code.
0048    - Tasklets: serialized wrt itself.
0049  */
0050 
0051 #ifndef __ARCH_IRQ_STAT
0052 irq_cpustat_t irq_stat[NR_CPUS] ____cacheline_aligned;
0053 EXPORT_SYMBOL(irq_stat);
0054 #endif
0055 
0056 static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
0057 
0058 DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
0059 
0060 const char * const softirq_to_name[NR_SOFTIRQS] = {
0061     "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
0062     "TASKLET", "SCHED", "HRTIMER", "RCU"
0063 };
0064 
0065 /*
0066  * we cannot loop indefinitely here to avoid userspace starvation,
0067  * but we also don't want to introduce a worst case 1/HZ latency
0068  * to the pending events, so lets the scheduler to balance
0069  * the softirq load for us.
0070  */
0071 static void wakeup_softirqd(void)
0072 {
0073     /* Interrupts are disabled: no need to stop preemption */
0074     struct task_struct *tsk = __this_cpu_read(ksoftirqd);
0075 
0076     if (tsk && tsk->state != TASK_RUNNING)
0077         wake_up_process(tsk);
0078 }
0079 
0080 /*
0081  * If ksoftirqd is scheduled, we do not want to process pending softirqs
0082  * right now. Let ksoftirqd handle this at its own rate, to get fairness.
0083  */
0084 static bool ksoftirqd_running(void)
0085 {
0086     struct task_struct *tsk = __this_cpu_read(ksoftirqd);
0087 
0088     return tsk && (tsk->state == TASK_RUNNING);
0089 }
0090 
0091 /*
0092  * preempt_count and SOFTIRQ_OFFSET usage:
0093  * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
0094  *   softirq processing.
0095  * - preempt_count is changed by SOFTIRQ_DISABLE_OFFSET (= 2 * SOFTIRQ_OFFSET)
0096  *   on local_bh_disable or local_bh_enable.
0097  * This lets us distinguish between whether we are currently processing
0098  * softirq and whether we just have bh disabled.
0099  */
0100 
0101 /*
0102  * This one is for softirq.c-internal use,
0103  * where hardirqs are disabled legitimately:
0104  */
0105 #ifdef CONFIG_TRACE_IRQFLAGS
0106 void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
0107 {
0108     unsigned long flags;
0109 
0110     WARN_ON_ONCE(in_irq());
0111 
0112     raw_local_irq_save(flags);
0113     /*
0114      * The preempt tracer hooks into preempt_count_add and will break
0115      * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET
0116      * is set and before current->softirq_enabled is cleared.
0117      * We must manually increment preempt_count here and manually
0118      * call the trace_preempt_off later.
0119      */
0120     __preempt_count_add(cnt);
0121     /*
0122      * Were softirqs turned off above:
0123      */
0124     if (softirq_count() == (cnt & SOFTIRQ_MASK))
0125         trace_softirqs_off(ip);
0126     raw_local_irq_restore(flags);
0127 
0128     if (preempt_count() == cnt) {
0129 #ifdef CONFIG_DEBUG_PREEMPT
0130         current->preempt_disable_ip = get_lock_parent_ip();
0131 #endif
0132         trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
0133     }
0134 }
0135 EXPORT_SYMBOL(__local_bh_disable_ip);
0136 #endif /* CONFIG_TRACE_IRQFLAGS */
0137 
0138 static void __local_bh_enable(unsigned int cnt)
0139 {
0140     WARN_ON_ONCE(!irqs_disabled());
0141 
0142     if (softirq_count() == (cnt & SOFTIRQ_MASK))
0143         trace_softirqs_on(_RET_IP_);
0144     preempt_count_sub(cnt);
0145 }
0146 
0147 /*
0148  * Special-case - softirqs can safely be enabled in
0149  * cond_resched_softirq(), or by __do_softirq(),
0150  * without processing still-pending softirqs:
0151  */
0152 void _local_bh_enable(void)
0153 {
0154     WARN_ON_ONCE(in_irq());
0155     __local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
0156 }
0157 EXPORT_SYMBOL(_local_bh_enable);
0158 
0159 void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
0160 {
0161     WARN_ON_ONCE(in_irq() || irqs_disabled());
0162 #ifdef CONFIG_TRACE_IRQFLAGS
0163     local_irq_disable();
0164 #endif
0165     /*
0166      * Are softirqs going to be turned on now:
0167      */
0168     if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
0169         trace_softirqs_on(ip);
0170     /*
0171      * Keep preemption disabled until we are done with
0172      * softirq processing:
0173      */
0174     preempt_count_sub(cnt - 1);
0175 
0176     if (unlikely(!in_interrupt() && local_softirq_pending())) {
0177         /*
0178          * Run softirq if any pending. And do it in its own stack
0179          * as we may be calling this deep in a task call stack already.
0180          */
0181         do_softirq();
0182     }
0183 
0184     preempt_count_dec();
0185 #ifdef CONFIG_TRACE_IRQFLAGS
0186     local_irq_enable();
0187 #endif
0188     preempt_check_resched();
0189 }
0190 EXPORT_SYMBOL(__local_bh_enable_ip);
0191 
0192 /*
0193  * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
0194  * but break the loop if need_resched() is set or after 2 ms.
0195  * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
0196  * certain cases, such as stop_machine(), jiffies may cease to
0197  * increment and so we need the MAX_SOFTIRQ_RESTART limit as
0198  * well to make sure we eventually return from this method.
0199  *
0200  * These limits have been established via experimentation.
0201  * The two things to balance is latency against fairness -
0202  * we want to handle softirqs as soon as possible, but they
0203  * should not be able to lock up the box.
0204  */
0205 #define MAX_SOFTIRQ_TIME  msecs_to_jiffies(2)
0206 #define MAX_SOFTIRQ_RESTART 10
0207 
0208 #ifdef CONFIG_TRACE_IRQFLAGS
0209 /*
0210  * When we run softirqs from irq_exit() and thus on the hardirq stack we need
0211  * to keep the lockdep irq context tracking as tight as possible in order to
0212  * not miss-qualify lock contexts and miss possible deadlocks.
0213  */
0214 
0215 static inline bool lockdep_softirq_start(void)
0216 {
0217     bool in_hardirq = false;
0218 
0219     if (trace_hardirq_context(current)) {
0220         in_hardirq = true;
0221         trace_hardirq_exit();
0222     }
0223 
0224     lockdep_softirq_enter();
0225 
0226     return in_hardirq;
0227 }
0228 
0229 static inline void lockdep_softirq_end(bool in_hardirq)
0230 {
0231     lockdep_softirq_exit();
0232 
0233     if (in_hardirq)
0234         trace_hardirq_enter();
0235 }
0236 #else
0237 static inline bool lockdep_softirq_start(void) { return false; }
0238 static inline void lockdep_softirq_end(bool in_hardirq) { }
0239 #endif
0240 
0241 asmlinkage __visible void __softirq_entry __do_softirq(void)
0242 {
0243     unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
0244     unsigned long old_flags = current->flags;
0245     int max_restart = MAX_SOFTIRQ_RESTART;
0246     struct softirq_action *h;
0247     bool in_hardirq;
0248     __u32 pending;
0249     int softirq_bit;
0250 
0251     /*
0252      * Mask out PF_MEMALLOC s current task context is borrowed for the
0253      * softirq. A softirq handled such as network RX might set PF_MEMALLOC
0254      * again if the socket is related to swap
0255      */
0256     current->flags &= ~PF_MEMALLOC;
0257 
0258     pending = local_softirq_pending();
0259     account_irq_enter_time(current);
0260 
0261     __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
0262     in_hardirq = lockdep_softirq_start();
0263 
0264 restart:
0265     /* Reset the pending bitmask before enabling irqs */
0266     set_softirq_pending(0);
0267 
0268     local_irq_enable();
0269 
0270     h = softirq_vec;
0271 
0272     while ((softirq_bit = ffs(pending))) {
0273         unsigned int vec_nr;
0274         int prev_count;
0275 
0276         h += softirq_bit - 1;
0277 
0278         vec_nr = h - softirq_vec;
0279         prev_count = preempt_count();
0280 
0281         kstat_incr_softirqs_this_cpu(vec_nr);
0282 
0283         trace_softirq_entry(vec_nr);
0284         h->action(h);
0285         trace_softirq_exit(vec_nr);
0286         if (unlikely(prev_count != preempt_count())) {
0287             pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
0288                    vec_nr, softirq_to_name[vec_nr], h->action,
0289                    prev_count, preempt_count());
0290             preempt_count_set(prev_count);
0291         }
0292         h++;
0293         pending >>= softirq_bit;
0294     }
0295 
0296     rcu_bh_qs();
0297     local_irq_disable();
0298 
0299     pending = local_softirq_pending();
0300     if (pending) {
0301         if (time_before(jiffies, end) && !need_resched() &&
0302             --max_restart)
0303             goto restart;
0304 
0305         wakeup_softirqd();
0306     }
0307 
0308     lockdep_softirq_end(in_hardirq);
0309     account_irq_exit_time(current);
0310     __local_bh_enable(SOFTIRQ_OFFSET);
0311     WARN_ON_ONCE(in_interrupt());
0312     tsk_restore_flags(current, old_flags, PF_MEMALLOC);
0313 }
0314 
0315 asmlinkage __visible void do_softirq(void)
0316 {
0317     __u32 pending;
0318     unsigned long flags;
0319 
0320     if (in_interrupt())
0321         return;
0322 
0323     local_irq_save(flags);
0324 
0325     pending = local_softirq_pending();
0326 
0327     if (pending && !ksoftirqd_running())
0328         do_softirq_own_stack();
0329 
0330     local_irq_restore(flags);
0331 }
0332 
0333 /*
0334  * Enter an interrupt context.
0335  */
0336 void irq_enter(void)
0337 {
0338     rcu_irq_enter();
0339     if (is_idle_task(current) && !in_interrupt()) {
0340         /*
0341          * Prevent raise_softirq from needlessly waking up ksoftirqd
0342          * here, as softirq will be serviced on return from interrupt.
0343          */
0344         local_bh_disable();
0345         tick_irq_enter();
0346         _local_bh_enable();
0347     }
0348 
0349     __irq_enter();
0350 }
0351 
0352 static inline void invoke_softirq(void)
0353 {
0354     if (ksoftirqd_running())
0355         return;
0356 
0357     if (!force_irqthreads) {
0358 #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
0359         /*
0360          * We can safely execute softirq on the current stack if
0361          * it is the irq stack, because it should be near empty
0362          * at this stage.
0363          */
0364         __do_softirq();
0365 #else
0366         /*
0367          * Otherwise, irq_exit() is called on the task stack that can
0368          * be potentially deep already. So call softirq in its own stack
0369          * to prevent from any overrun.
0370          */
0371         do_softirq_own_stack();
0372 #endif
0373     } else {
0374         wakeup_softirqd();
0375     }
0376 }
0377 
0378 static inline void tick_irq_exit(void)
0379 {
0380 #ifdef CONFIG_NO_HZ_COMMON
0381     int cpu = smp_processor_id();
0382 
0383     /* Make sure that timer wheel updates are propagated */
0384     if ((idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
0385         if (!in_interrupt())
0386             tick_nohz_irq_exit();
0387     }
0388 #endif
0389 }
0390 
0391 /*
0392  * Exit an interrupt context. Process softirqs if needed and possible:
0393  */
0394 void irq_exit(void)
0395 {
0396 #ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
0397     local_irq_disable();
0398 #else
0399     WARN_ON_ONCE(!irqs_disabled());
0400 #endif
0401 
0402     account_irq_exit_time(current);
0403     preempt_count_sub(HARDIRQ_OFFSET);
0404     if (!in_interrupt() && local_softirq_pending())
0405         invoke_softirq();
0406 
0407     tick_irq_exit();
0408     rcu_irq_exit();
0409     trace_hardirq_exit(); /* must be last! */
0410 }
0411 
0412 /*
0413  * This function must run with irqs disabled!
0414  */
0415 inline void raise_softirq_irqoff(unsigned int nr)
0416 {
0417     __raise_softirq_irqoff(nr);
0418 
0419     /*
0420      * If we're in an interrupt or softirq, we're done
0421      * (this also catches softirq-disabled code). We will
0422      * actually run the softirq once we return from
0423      * the irq or softirq.
0424      *
0425      * Otherwise we wake up ksoftirqd to make sure we
0426      * schedule the softirq soon.
0427      */
0428     if (!in_interrupt())
0429         wakeup_softirqd();
0430 }
0431 
0432 void raise_softirq(unsigned int nr)
0433 {
0434     unsigned long flags;
0435 
0436     local_irq_save(flags);
0437     raise_softirq_irqoff(nr);
0438     local_irq_restore(flags);
0439 }
0440 
0441 void __raise_softirq_irqoff(unsigned int nr)
0442 {
0443     trace_softirq_raise(nr);
0444     or_softirq_pending(1UL << nr);
0445 }
0446 
0447 void open_softirq(int nr, void (*action)(struct softirq_action *))
0448 {
0449     softirq_vec[nr].action = action;
0450 }
0451 
0452 /*
0453  * Tasklets
0454  */
0455 struct tasklet_head {
0456     struct tasklet_struct *head;
0457     struct tasklet_struct **tail;
0458 };
0459 
0460 static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
0461 static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
0462 
0463 void __tasklet_schedule(struct tasklet_struct *t)
0464 {
0465     unsigned long flags;
0466 
0467     local_irq_save(flags);
0468     t->next = NULL;
0469     *__this_cpu_read(tasklet_vec.tail) = t;
0470     __this_cpu_write(tasklet_vec.tail, &(t->next));
0471     raise_softirq_irqoff(TASKLET_SOFTIRQ);
0472     local_irq_restore(flags);
0473 }
0474 EXPORT_SYMBOL(__tasklet_schedule);
0475 
0476 void __tasklet_hi_schedule(struct tasklet_struct *t)
0477 {
0478     unsigned long flags;
0479 
0480     local_irq_save(flags);
0481     t->next = NULL;
0482     *__this_cpu_read(tasklet_hi_vec.tail) = t;
0483     __this_cpu_write(tasklet_hi_vec.tail,  &(t->next));
0484     raise_softirq_irqoff(HI_SOFTIRQ);
0485     local_irq_restore(flags);
0486 }
0487 EXPORT_SYMBOL(__tasklet_hi_schedule);
0488 
0489 void __tasklet_hi_schedule_first(struct tasklet_struct *t)
0490 {
0491     BUG_ON(!irqs_disabled());
0492 
0493     t->next = __this_cpu_read(tasklet_hi_vec.head);
0494     __this_cpu_write(tasklet_hi_vec.head, t);
0495     __raise_softirq_irqoff(HI_SOFTIRQ);
0496 }
0497 EXPORT_SYMBOL(__tasklet_hi_schedule_first);
0498 
0499 static __latent_entropy void tasklet_action(struct softirq_action *a)
0500 {
0501     struct tasklet_struct *list;
0502 
0503     local_irq_disable();
0504     list = __this_cpu_read(tasklet_vec.head);
0505     __this_cpu_write(tasklet_vec.head, NULL);
0506     __this_cpu_write(tasklet_vec.tail, this_cpu_ptr(&tasklet_vec.head));
0507     local_irq_enable();
0508 
0509     while (list) {
0510         struct tasklet_struct *t = list;
0511 
0512         list = list->next;
0513 
0514         if (tasklet_trylock(t)) {
0515             if (!atomic_read(&t->count)) {
0516                 if (!test_and_clear_bit(TASKLET_STATE_SCHED,
0517                             &t->state))
0518                     BUG();
0519                 t->func(t->data);
0520                 tasklet_unlock(t);
0521                 continue;
0522             }
0523             tasklet_unlock(t);
0524         }
0525 
0526         local_irq_disable();
0527         t->next = NULL;
0528         *__this_cpu_read(tasklet_vec.tail) = t;
0529         __this_cpu_write(tasklet_vec.tail, &(t->next));
0530         __raise_softirq_irqoff(TASKLET_SOFTIRQ);
0531         local_irq_enable();
0532     }
0533 }
0534 
0535 static __latent_entropy void tasklet_hi_action(struct softirq_action *a)
0536 {
0537     struct tasklet_struct *list;
0538 
0539     local_irq_disable();
0540     list = __this_cpu_read(tasklet_hi_vec.head);
0541     __this_cpu_write(tasklet_hi_vec.head, NULL);
0542     __this_cpu_write(tasklet_hi_vec.tail, this_cpu_ptr(&tasklet_hi_vec.head));
0543     local_irq_enable();
0544 
0545     while (list) {
0546         struct tasklet_struct *t = list;
0547 
0548         list = list->next;
0549 
0550         if (tasklet_trylock(t)) {
0551             if (!atomic_read(&t->count)) {
0552                 if (!test_and_clear_bit(TASKLET_STATE_SCHED,
0553                             &t->state))
0554                     BUG();
0555                 t->func(t->data);
0556                 tasklet_unlock(t);
0557                 continue;
0558             }
0559             tasklet_unlock(t);
0560         }
0561 
0562         local_irq_disable();
0563         t->next = NULL;
0564         *__this_cpu_read(tasklet_hi_vec.tail) = t;
0565         __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
0566         __raise_softirq_irqoff(HI_SOFTIRQ);
0567         local_irq_enable();
0568     }
0569 }
0570 
0571 void tasklet_init(struct tasklet_struct *t,
0572           void (*func)(unsigned long), unsigned long data)
0573 {
0574     t->next = NULL;
0575     t->state = 0;
0576     atomic_set(&t->count, 0);
0577     t->func = func;
0578     t->data = data;
0579 }
0580 EXPORT_SYMBOL(tasklet_init);
0581 
0582 void tasklet_kill(struct tasklet_struct *t)
0583 {
0584     if (in_interrupt())
0585         pr_notice("Attempt to kill tasklet from interrupt\n");
0586 
0587     while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
0588         do {
0589             yield();
0590         } while (test_bit(TASKLET_STATE_SCHED, &t->state));
0591     }
0592     tasklet_unlock_wait(t);
0593     clear_bit(TASKLET_STATE_SCHED, &t->state);
0594 }
0595 EXPORT_SYMBOL(tasklet_kill);
0596 
0597 /*
0598  * tasklet_hrtimer
0599  */
0600 
0601 /*
0602  * The trampoline is called when the hrtimer expires. It schedules a tasklet
0603  * to run __tasklet_hrtimer_trampoline() which in turn will call the intended
0604  * hrtimer callback, but from softirq context.
0605  */
0606 static enum hrtimer_restart __hrtimer_tasklet_trampoline(struct hrtimer *timer)
0607 {
0608     struct tasklet_hrtimer *ttimer =
0609         container_of(timer, struct tasklet_hrtimer, timer);
0610 
0611     tasklet_hi_schedule(&ttimer->tasklet);
0612     return HRTIMER_NORESTART;
0613 }
0614 
0615 /*
0616  * Helper function which calls the hrtimer callback from
0617  * tasklet/softirq context
0618  */
0619 static void __tasklet_hrtimer_trampoline(unsigned long data)
0620 {
0621     struct tasklet_hrtimer *ttimer = (void *)data;
0622     enum hrtimer_restart restart;
0623 
0624     restart = ttimer->function(&ttimer->timer);
0625     if (restart != HRTIMER_NORESTART)
0626         hrtimer_restart(&ttimer->timer);
0627 }
0628 
0629 /**
0630  * tasklet_hrtimer_init - Init a tasklet/hrtimer combo for softirq callbacks
0631  * @ttimer:  tasklet_hrtimer which is initialized
0632  * @function:    hrtimer callback function which gets called from softirq context
0633  * @which_clock: clock id (CLOCK_MONOTONIC/CLOCK_REALTIME)
0634  * @mode:    hrtimer mode (HRTIMER_MODE_ABS/HRTIMER_MODE_REL)
0635  */
0636 void tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
0637               enum hrtimer_restart (*function)(struct hrtimer *),
0638               clockid_t which_clock, enum hrtimer_mode mode)
0639 {
0640     hrtimer_init(&ttimer->timer, which_clock, mode);
0641     ttimer->timer.function = __hrtimer_tasklet_trampoline;
0642     tasklet_init(&ttimer->tasklet, __tasklet_hrtimer_trampoline,
0643              (unsigned long)ttimer);
0644     ttimer->function = function;
0645 }
0646 EXPORT_SYMBOL_GPL(tasklet_hrtimer_init);
0647 
0648 void __init softirq_init(void)
0649 {
0650     int cpu;
0651 
0652     for_each_possible_cpu(cpu) {
0653         per_cpu(tasklet_vec, cpu).tail =
0654             &per_cpu(tasklet_vec, cpu).head;
0655         per_cpu(tasklet_hi_vec, cpu).tail =
0656             &per_cpu(tasklet_hi_vec, cpu).head;
0657     }
0658 
0659     open_softirq(TASKLET_SOFTIRQ, tasklet_action);
0660     open_softirq(HI_SOFTIRQ, tasklet_hi_action);
0661 }
0662 
0663 static int ksoftirqd_should_run(unsigned int cpu)
0664 {
0665     return local_softirq_pending();
0666 }
0667 
0668 static void run_ksoftirqd(unsigned int cpu)
0669 {
0670     local_irq_disable();
0671     if (local_softirq_pending()) {
0672         /*
0673          * We can safely run softirq on inline stack, as we are not deep
0674          * in the task stack here.
0675          */
0676         __do_softirq();
0677         local_irq_enable();
0678         cond_resched_rcu_qs();
0679         return;
0680     }
0681     local_irq_enable();
0682 }
0683 
0684 #ifdef CONFIG_HOTPLUG_CPU
0685 /*
0686  * tasklet_kill_immediate is called to remove a tasklet which can already be
0687  * scheduled for execution on @cpu.
0688  *
0689  * Unlike tasklet_kill, this function removes the tasklet
0690  * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
0691  *
0692  * When this function is called, @cpu must be in the CPU_DEAD state.
0693  */
0694 void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
0695 {
0696     struct tasklet_struct **i;
0697 
0698     BUG_ON(cpu_online(cpu));
0699     BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
0700 
0701     if (!test_bit(TASKLET_STATE_SCHED, &t->state))
0702         return;
0703 
0704     /* CPU is dead, so no lock needed. */
0705     for (i = &per_cpu(tasklet_vec, cpu).head; *i; i = &(*i)->next) {
0706         if (*i == t) {
0707             *i = t->next;
0708             /* If this was the tail element, move the tail ptr */
0709             if (*i == NULL)
0710                 per_cpu(tasklet_vec, cpu).tail = i;
0711             return;
0712         }
0713     }
0714     BUG();
0715 }
0716 
0717 static int takeover_tasklets(unsigned int cpu)
0718 {
0719     /* CPU is dead, so no lock needed. */
0720     local_irq_disable();
0721 
0722     /* Find end, append list for that CPU. */
0723     if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
0724         *__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
0725         this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
0726         per_cpu(tasklet_vec, cpu).head = NULL;
0727         per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
0728     }
0729     raise_softirq_irqoff(TASKLET_SOFTIRQ);
0730 
0731     if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
0732         *__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
0733         __this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
0734         per_cpu(tasklet_hi_vec, cpu).head = NULL;
0735         per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
0736     }
0737     raise_softirq_irqoff(HI_SOFTIRQ);
0738 
0739     local_irq_enable();
0740     return 0;
0741 }
0742 #else
0743 #define takeover_tasklets   NULL
0744 #endif /* CONFIG_HOTPLUG_CPU */
0745 
0746 static struct smp_hotplug_thread softirq_threads = {
0747     .store          = &ksoftirqd,
0748     .thread_should_run  = ksoftirqd_should_run,
0749     .thread_fn      = run_ksoftirqd,
0750     .thread_comm        = "ksoftirqd/%u",
0751 };
0752 
0753 static __init int spawn_ksoftirqd(void)
0754 {
0755     cpuhp_setup_state_nocalls(CPUHP_SOFTIRQ_DEAD, "softirq:dead", NULL,
0756                   takeover_tasklets);
0757     BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
0758 
0759     return 0;
0760 }
0761 early_initcall(spawn_ksoftirqd);
0762 
0763 /*
0764  * [ These __weak aliases are kept in a separate compilation unit, so that
0765  *   GCC does not inline them incorrectly. ]
0766  */
0767 
0768 int __init __weak early_irq_init(void)
0769 {
0770     return 0;
0771 }
0772 
0773 int __init __weak arch_probe_nr_irqs(void)
0774 {
0775     return NR_IRQS_LEGACY;
0776 }
0777 
0778 int __init __weak arch_early_irq_init(void)
0779 {
0780     return 0;
0781 }
0782 
0783 unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
0784 {
0785     return from;
0786 }