softirq.c 19 KB

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