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