softirq.c 19 KB

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