softirq.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. * Remote softirq infrastructure is by Jens Axboe.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/mm.h>
  17. #include <linux/notifier.h>
  18. #include <linux/percpu.h>
  19. #include <linux/cpu.h>
  20. #include <linux/freezer.h>
  21. #include <linux/kthread.h>
  22. #include <linux/rcupdate.h>
  23. #include <linux/ftrace.h>
  24. #include <linux/smp.h>
  25. #include <linux/tick.h>
  26. #define CREATE_TRACE_POINTS
  27. #include <trace/events/irq.h>
  28. #include <asm/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. char *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. static void __local_bh_disable(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 add_preempt_count 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() += cnt;
  95. /*
  96. * Were softirqs turned off above:
  97. */
  98. if (softirq_count() == cnt)
  99. trace_softirqs_off(ip);
  100. raw_local_irq_restore(flags);
  101. if (preempt_count() == cnt)
  102. trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  103. }
  104. #else /* !CONFIG_TRACE_IRQFLAGS */
  105. static inline void __local_bh_disable(unsigned long ip, unsigned int cnt)
  106. {
  107. add_preempt_count(cnt);
  108. barrier();
  109. }
  110. #endif /* CONFIG_TRACE_IRQFLAGS */
  111. void local_bh_disable(void)
  112. {
  113. __local_bh_disable((unsigned long)__builtin_return_address(0),
  114. SOFTIRQ_DISABLE_OFFSET);
  115. }
  116. EXPORT_SYMBOL(local_bh_disable);
  117. static void __local_bh_enable(unsigned int cnt)
  118. {
  119. WARN_ON_ONCE(in_irq());
  120. WARN_ON_ONCE(!irqs_disabled());
  121. if (softirq_count() == cnt)
  122. trace_softirqs_on((unsigned long)__builtin_return_address(0));
  123. sub_preempt_count(cnt);
  124. }
  125. /*
  126. * Special-case - softirqs can safely be enabled in
  127. * cond_resched_softirq(), or by __do_softirq(),
  128. * without processing still-pending softirqs:
  129. */
  130. void _local_bh_enable(void)
  131. {
  132. __local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
  133. }
  134. EXPORT_SYMBOL(_local_bh_enable);
  135. static inline void _local_bh_enable_ip(unsigned long ip)
  136. {
  137. WARN_ON_ONCE(in_irq() || irqs_disabled());
  138. #ifdef CONFIG_TRACE_IRQFLAGS
  139. local_irq_disable();
  140. #endif
  141. /*
  142. * Are softirqs going to be turned on now:
  143. */
  144. if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
  145. trace_softirqs_on(ip);
  146. /*
  147. * Keep preemption disabled until we are done with
  148. * softirq processing:
  149. */
  150. sub_preempt_count(SOFTIRQ_DISABLE_OFFSET - 1);
  151. if (unlikely(!in_interrupt() && local_softirq_pending()))
  152. do_softirq();
  153. dec_preempt_count();
  154. #ifdef CONFIG_TRACE_IRQFLAGS
  155. local_irq_enable();
  156. #endif
  157. preempt_check_resched();
  158. }
  159. void local_bh_enable(void)
  160. {
  161. _local_bh_enable_ip((unsigned long)__builtin_return_address(0));
  162. }
  163. EXPORT_SYMBOL(local_bh_enable);
  164. void local_bh_enable_ip(unsigned long ip)
  165. {
  166. _local_bh_enable_ip(ip);
  167. }
  168. EXPORT_SYMBOL(local_bh_enable_ip);
  169. /*
  170. * We restart softirq processing MAX_SOFTIRQ_RESTART times,
  171. * and we fall back to softirqd after that.
  172. *
  173. * This number has been established via experimentation.
  174. * The two things to balance is latency against fairness -
  175. * we want to handle softirqs as soon as possible, but they
  176. * should not be able to lock up the box.
  177. */
  178. #define MAX_SOFTIRQ_RESTART 10
  179. asmlinkage void __do_softirq(void)
  180. {
  181. struct softirq_action *h;
  182. __u32 pending;
  183. int max_restart = MAX_SOFTIRQ_RESTART;
  184. int cpu;
  185. pending = local_softirq_pending();
  186. account_system_vtime(current);
  187. __local_bh_disable((unsigned long)__builtin_return_address(0),
  188. SOFTIRQ_OFFSET);
  189. lockdep_softirq_enter();
  190. cpu = smp_processor_id();
  191. restart:
  192. /* Reset the pending bitmask before enabling irqs */
  193. set_softirq_pending(0);
  194. local_irq_enable();
  195. h = softirq_vec;
  196. do {
  197. if (pending & 1) {
  198. unsigned int vec_nr = h - softirq_vec;
  199. int prev_count = preempt_count();
  200. kstat_incr_softirqs_this_cpu(vec_nr);
  201. trace_softirq_entry(vec_nr);
  202. h->action(h);
  203. trace_softirq_exit(vec_nr);
  204. if (unlikely(prev_count != preempt_count())) {
  205. printk(KERN_ERR "huh, entered softirq %u %s %p"
  206. "with preempt_count %08x,"
  207. " exited with %08x?\n", vec_nr,
  208. softirq_to_name[vec_nr], h->action,
  209. prev_count, preempt_count());
  210. preempt_count() = prev_count;
  211. }
  212. rcu_bh_qs(cpu);
  213. }
  214. h++;
  215. pending >>= 1;
  216. } while (pending);
  217. local_irq_disable();
  218. pending = local_softirq_pending();
  219. if (pending && --max_restart)
  220. goto restart;
  221. if (pending)
  222. wakeup_softirqd();
  223. lockdep_softirq_exit();
  224. account_system_vtime(current);
  225. __local_bh_enable(SOFTIRQ_OFFSET);
  226. }
  227. #ifndef __ARCH_HAS_DO_SOFTIRQ
  228. asmlinkage void do_softirq(void)
  229. {
  230. __u32 pending;
  231. unsigned long flags;
  232. if (in_interrupt())
  233. return;
  234. local_irq_save(flags);
  235. pending = local_softirq_pending();
  236. if (pending)
  237. __do_softirq();
  238. local_irq_restore(flags);
  239. }
  240. #endif
  241. /*
  242. * Enter an interrupt context.
  243. */
  244. void irq_enter(void)
  245. {
  246. int cpu = smp_processor_id();
  247. rcu_irq_enter();
  248. if (idle_cpu(cpu) && !in_interrupt()) {
  249. /*
  250. * Prevent raise_softirq from needlessly waking up ksoftirqd
  251. * here, as softirq will be serviced on return from interrupt.
  252. */
  253. local_bh_disable();
  254. tick_check_idle(cpu);
  255. _local_bh_enable();
  256. }
  257. __irq_enter();
  258. }
  259. #ifdef __ARCH_IRQ_EXIT_IRQS_DISABLED
  260. static inline void invoke_softirq(void)
  261. {
  262. if (!force_irqthreads)
  263. __do_softirq();
  264. else {
  265. __local_bh_disable((unsigned long)__builtin_return_address(0),
  266. SOFTIRQ_OFFSET);
  267. wakeup_softirqd();
  268. __local_bh_enable(SOFTIRQ_OFFSET);
  269. }
  270. }
  271. #else
  272. static inline void invoke_softirq(void)
  273. {
  274. if (!force_irqthreads)
  275. do_softirq();
  276. else {
  277. __local_bh_disable((unsigned long)__builtin_return_address(0),
  278. SOFTIRQ_OFFSET);
  279. wakeup_softirqd();
  280. __local_bh_enable(SOFTIRQ_OFFSET);
  281. }
  282. }
  283. #endif
  284. /*
  285. * Exit an interrupt context. Process softirqs if needed and possible:
  286. */
  287. void irq_exit(void)
  288. {
  289. account_system_vtime(current);
  290. trace_hardirq_exit();
  291. sub_preempt_count(IRQ_EXIT_OFFSET);
  292. if (!in_interrupt() && local_softirq_pending())
  293. invoke_softirq();
  294. rcu_irq_exit();
  295. #ifdef CONFIG_NO_HZ
  296. /* Make sure that timer wheel updates are propagated */
  297. if (idle_cpu(smp_processor_id()) && !in_interrupt() && !need_resched())
  298. tick_nohz_stop_sched_tick(0);
  299. #endif
  300. preempt_enable_no_resched();
  301. }
  302. /*
  303. * This function must run with irqs disabled!
  304. */
  305. inline void raise_softirq_irqoff(unsigned int nr)
  306. {
  307. __raise_softirq_irqoff(nr);
  308. /*
  309. * If we're in an interrupt or softirq, we're done
  310. * (this also catches softirq-disabled code). We will
  311. * actually run the softirq once we return from
  312. * the irq or softirq.
  313. *
  314. * Otherwise we wake up ksoftirqd to make sure we
  315. * schedule the softirq soon.
  316. */
  317. if (!in_interrupt())
  318. wakeup_softirqd();
  319. }
  320. void raise_softirq(unsigned int nr)
  321. {
  322. unsigned long flags;
  323. local_irq_save(flags);
  324. raise_softirq_irqoff(nr);
  325. local_irq_restore(flags);
  326. }
  327. void open_softirq(int nr, void (*action)(struct softirq_action *))
  328. {
  329. softirq_vec[nr].action = action;
  330. }
  331. /*
  332. * Tasklets
  333. */
  334. struct tasklet_head
  335. {
  336. struct tasklet_struct *head;
  337. struct tasklet_struct **tail;
  338. };
  339. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  340. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  341. void __tasklet_schedule(struct tasklet_struct *t)
  342. {
  343. unsigned long flags;
  344. local_irq_save(flags);
  345. t->next = NULL;
  346. *__this_cpu_read(tasklet_vec.tail) = t;
  347. __this_cpu_write(tasklet_vec.tail, &(t->next));
  348. raise_softirq_irqoff(TASKLET_SOFTIRQ);
  349. local_irq_restore(flags);
  350. }
  351. EXPORT_SYMBOL(__tasklet_schedule);
  352. void __tasklet_hi_schedule(struct tasklet_struct *t)
  353. {
  354. unsigned long flags;
  355. local_irq_save(flags);
  356. t->next = NULL;
  357. *__this_cpu_read(tasklet_hi_vec.tail) = t;
  358. __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  359. raise_softirq_irqoff(HI_SOFTIRQ);
  360. local_irq_restore(flags);
  361. }
  362. EXPORT_SYMBOL(__tasklet_hi_schedule);
  363. void __tasklet_hi_schedule_first(struct tasklet_struct *t)
  364. {
  365. BUG_ON(!irqs_disabled());
  366. t->next = __this_cpu_read(tasklet_hi_vec.head);
  367. __this_cpu_write(tasklet_hi_vec.head, t);
  368. __raise_softirq_irqoff(HI_SOFTIRQ);
  369. }
  370. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  371. static void tasklet_action(struct softirq_action *a)
  372. {
  373. struct tasklet_struct *list;
  374. local_irq_disable();
  375. list = __this_cpu_read(tasklet_vec.head);
  376. __this_cpu_write(tasklet_vec.head, NULL);
  377. __this_cpu_write(tasklet_vec.tail, &__get_cpu_var(tasklet_vec).head);
  378. local_irq_enable();
  379. while (list) {
  380. struct tasklet_struct *t = list;
  381. list = list->next;
  382. if (tasklet_trylock(t)) {
  383. if (!atomic_read(&t->count)) {
  384. if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  385. BUG();
  386. t->func(t->data);
  387. tasklet_unlock(t);
  388. continue;
  389. }
  390. tasklet_unlock(t);
  391. }
  392. local_irq_disable();
  393. t->next = NULL;
  394. *__this_cpu_read(tasklet_vec.tail) = t;
  395. __this_cpu_write(tasklet_vec.tail, &(t->next));
  396. __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  397. local_irq_enable();
  398. }
  399. }
  400. static void tasklet_hi_action(struct softirq_action *a)
  401. {
  402. struct tasklet_struct *list;
  403. local_irq_disable();
  404. list = __this_cpu_read(tasklet_hi_vec.head);
  405. __this_cpu_write(tasklet_hi_vec.head, NULL);
  406. __this_cpu_write(tasklet_hi_vec.tail, &__get_cpu_var(tasklet_hi_vec).head);
  407. local_irq_enable();
  408. while (list) {
  409. struct tasklet_struct *t = list;
  410. list = list->next;
  411. if (tasklet_trylock(t)) {
  412. if (!atomic_read(&t->count)) {
  413. if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  414. BUG();
  415. t->func(t->data);
  416. tasklet_unlock(t);
  417. continue;
  418. }
  419. tasklet_unlock(t);
  420. }
  421. local_irq_disable();
  422. t->next = NULL;
  423. *__this_cpu_read(tasklet_hi_vec.tail) = t;
  424. __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  425. __raise_softirq_irqoff(HI_SOFTIRQ);
  426. local_irq_enable();
  427. }
  428. }
  429. void tasklet_init(struct tasklet_struct *t,
  430. void (*func)(unsigned long), unsigned long data)
  431. {
  432. t->next = NULL;
  433. t->state = 0;
  434. atomic_set(&t->count, 0);
  435. t->func = func;
  436. t->data = data;
  437. }
  438. EXPORT_SYMBOL(tasklet_init);
  439. void tasklet_kill(struct tasklet_struct *t)
  440. {
  441. if (in_interrupt())
  442. printk("Attempt to kill tasklet from interrupt\n");
  443. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  444. do {
  445. yield();
  446. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  447. }
  448. tasklet_unlock_wait(t);
  449. clear_bit(TASKLET_STATE_SCHED, &t->state);
  450. }
  451. EXPORT_SYMBOL(tasklet_kill);
  452. /*
  453. * tasklet_hrtimer
  454. */
  455. /*
  456. * The trampoline is called when the hrtimer expires. It schedules a tasklet
  457. * to run __tasklet_hrtimer_trampoline() which in turn will call the intended
  458. * hrtimer callback, but from softirq context.
  459. */
  460. static enum hrtimer_restart __hrtimer_tasklet_trampoline(struct hrtimer *timer)
  461. {
  462. struct tasklet_hrtimer *ttimer =
  463. container_of(timer, struct tasklet_hrtimer, timer);
  464. tasklet_hi_schedule(&ttimer->tasklet);
  465. return HRTIMER_NORESTART;
  466. }
  467. /*
  468. * Helper function which calls the hrtimer callback from
  469. * tasklet/softirq context
  470. */
  471. static void __tasklet_hrtimer_trampoline(unsigned long data)
  472. {
  473. struct tasklet_hrtimer *ttimer = (void *)data;
  474. enum hrtimer_restart restart;
  475. restart = ttimer->function(&ttimer->timer);
  476. if (restart != HRTIMER_NORESTART)
  477. hrtimer_restart(&ttimer->timer);
  478. }
  479. /**
  480. * tasklet_hrtimer_init - Init a tasklet/hrtimer combo for softirq callbacks
  481. * @ttimer: tasklet_hrtimer which is initialized
  482. * @function: hrtimer callback function which gets called from softirq context
  483. * @which_clock: clock id (CLOCK_MONOTONIC/CLOCK_REALTIME)
  484. * @mode: hrtimer mode (HRTIMER_MODE_ABS/HRTIMER_MODE_REL)
  485. */
  486. void tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
  487. enum hrtimer_restart (*function)(struct hrtimer *),
  488. clockid_t which_clock, enum hrtimer_mode mode)
  489. {
  490. hrtimer_init(&ttimer->timer, which_clock, mode);
  491. ttimer->timer.function = __hrtimer_tasklet_trampoline;
  492. tasklet_init(&ttimer->tasklet, __tasklet_hrtimer_trampoline,
  493. (unsigned long)ttimer);
  494. ttimer->function = function;
  495. }
  496. EXPORT_SYMBOL_GPL(tasklet_hrtimer_init);
  497. /*
  498. * Remote softirq bits
  499. */
  500. DEFINE_PER_CPU(struct list_head [NR_SOFTIRQS], softirq_work_list);
  501. EXPORT_PER_CPU_SYMBOL(softirq_work_list);
  502. static void __local_trigger(struct call_single_data *cp, int softirq)
  503. {
  504. struct list_head *head = &__get_cpu_var(softirq_work_list[softirq]);
  505. list_add_tail(&cp->list, head);
  506. /* Trigger the softirq only if the list was previously empty. */
  507. if (head->next == &cp->list)
  508. raise_softirq_irqoff(softirq);
  509. }
  510. #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
  511. static void remote_softirq_receive(void *data)
  512. {
  513. struct call_single_data *cp = data;
  514. unsigned long flags;
  515. int softirq;
  516. softirq = cp->priv;
  517. local_irq_save(flags);
  518. __local_trigger(cp, softirq);
  519. local_irq_restore(flags);
  520. }
  521. static int __try_remote_softirq(struct call_single_data *cp, int cpu, int softirq)
  522. {
  523. if (cpu_online(cpu)) {
  524. cp->func = remote_softirq_receive;
  525. cp->info = cp;
  526. cp->flags = 0;
  527. cp->priv = softirq;
  528. __smp_call_function_single(cpu, cp, 0);
  529. return 0;
  530. }
  531. return 1;
  532. }
  533. #else /* CONFIG_USE_GENERIC_SMP_HELPERS */
  534. static int __try_remote_softirq(struct call_single_data *cp, int cpu, int softirq)
  535. {
  536. return 1;
  537. }
  538. #endif
  539. /**
  540. * __send_remote_softirq - try to schedule softirq work on a remote cpu
  541. * @cp: private SMP call function data area
  542. * @cpu: the remote cpu
  543. * @this_cpu: the currently executing cpu
  544. * @softirq: the softirq for the work
  545. *
  546. * Attempt to schedule softirq work on a remote cpu. If this cannot be
  547. * done, the work is instead queued up on the local cpu.
  548. *
  549. * Interrupts must be disabled.
  550. */
  551. void __send_remote_softirq(struct call_single_data *cp, int cpu, int this_cpu, int softirq)
  552. {
  553. if (cpu == this_cpu || __try_remote_softirq(cp, cpu, softirq))
  554. __local_trigger(cp, softirq);
  555. }
  556. EXPORT_SYMBOL(__send_remote_softirq);
  557. /**
  558. * send_remote_softirq - try to schedule softirq work on a remote cpu
  559. * @cp: private SMP call function data area
  560. * @cpu: the remote cpu
  561. * @softirq: the softirq for the work
  562. *
  563. * Like __send_remote_softirq except that disabling interrupts and
  564. * computing the current cpu is done for the caller.
  565. */
  566. void send_remote_softirq(struct call_single_data *cp, int cpu, int softirq)
  567. {
  568. unsigned long flags;
  569. int this_cpu;
  570. local_irq_save(flags);
  571. this_cpu = smp_processor_id();
  572. __send_remote_softirq(cp, cpu, this_cpu, softirq);
  573. local_irq_restore(flags);
  574. }
  575. EXPORT_SYMBOL(send_remote_softirq);
  576. static int __cpuinit remote_softirq_cpu_notify(struct notifier_block *self,
  577. unsigned long action, void *hcpu)
  578. {
  579. /*
  580. * If a CPU goes away, splice its entries to the current CPU
  581. * and trigger a run of the softirq
  582. */
  583. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  584. int cpu = (unsigned long) hcpu;
  585. int i;
  586. local_irq_disable();
  587. for (i = 0; i < NR_SOFTIRQS; i++) {
  588. struct list_head *head = &per_cpu(softirq_work_list[i], cpu);
  589. struct list_head *local_head;
  590. if (list_empty(head))
  591. continue;
  592. local_head = &__get_cpu_var(softirq_work_list[i]);
  593. list_splice_init(head, local_head);
  594. raise_softirq_irqoff(i);
  595. }
  596. local_irq_enable();
  597. }
  598. return NOTIFY_OK;
  599. }
  600. static struct notifier_block __cpuinitdata remote_softirq_cpu_notifier = {
  601. .notifier_call = remote_softirq_cpu_notify,
  602. };
  603. void __init softirq_init(void)
  604. {
  605. int cpu;
  606. for_each_possible_cpu(cpu) {
  607. int i;
  608. per_cpu(tasklet_vec, cpu).tail =
  609. &per_cpu(tasklet_vec, cpu).head;
  610. per_cpu(tasklet_hi_vec, cpu).tail =
  611. &per_cpu(tasklet_hi_vec, cpu).head;
  612. for (i = 0; i < NR_SOFTIRQS; i++)
  613. INIT_LIST_HEAD(&per_cpu(softirq_work_list[i], cpu));
  614. }
  615. register_hotcpu_notifier(&remote_softirq_cpu_notifier);
  616. open_softirq(TASKLET_SOFTIRQ, tasklet_action);
  617. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  618. }
  619. static int run_ksoftirqd(void * __bind_cpu)
  620. {
  621. set_current_state(TASK_INTERRUPTIBLE);
  622. while (!kthread_should_stop()) {
  623. preempt_disable();
  624. if (!local_softirq_pending()) {
  625. preempt_enable_no_resched();
  626. schedule();
  627. preempt_disable();
  628. }
  629. __set_current_state(TASK_RUNNING);
  630. while (local_softirq_pending()) {
  631. /* Preempt disable stops cpu going offline.
  632. If already offline, we'll be on wrong CPU:
  633. don't process */
  634. if (cpu_is_offline((long)__bind_cpu))
  635. goto wait_to_die;
  636. local_irq_disable();
  637. if (local_softirq_pending())
  638. __do_softirq();
  639. local_irq_enable();
  640. preempt_enable_no_resched();
  641. cond_resched();
  642. preempt_disable();
  643. rcu_note_context_switch((long)__bind_cpu);
  644. }
  645. preempt_enable();
  646. set_current_state(TASK_INTERRUPTIBLE);
  647. }
  648. __set_current_state(TASK_RUNNING);
  649. return 0;
  650. wait_to_die:
  651. preempt_enable();
  652. /* Wait for kthread_stop */
  653. set_current_state(TASK_INTERRUPTIBLE);
  654. while (!kthread_should_stop()) {
  655. schedule();
  656. set_current_state(TASK_INTERRUPTIBLE);
  657. }
  658. __set_current_state(TASK_RUNNING);
  659. return 0;
  660. }
  661. #ifdef CONFIG_HOTPLUG_CPU
  662. /*
  663. * tasklet_kill_immediate is called to remove a tasklet which can already be
  664. * scheduled for execution on @cpu.
  665. *
  666. * Unlike tasklet_kill, this function removes the tasklet
  667. * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
  668. *
  669. * When this function is called, @cpu must be in the CPU_DEAD state.
  670. */
  671. void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
  672. {
  673. struct tasklet_struct **i;
  674. BUG_ON(cpu_online(cpu));
  675. BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
  676. if (!test_bit(TASKLET_STATE_SCHED, &t->state))
  677. return;
  678. /* CPU is dead, so no lock needed. */
  679. for (i = &per_cpu(tasklet_vec, cpu).head; *i; i = &(*i)->next) {
  680. if (*i == t) {
  681. *i = t->next;
  682. /* If this was the tail element, move the tail ptr */
  683. if (*i == NULL)
  684. per_cpu(tasklet_vec, cpu).tail = i;
  685. return;
  686. }
  687. }
  688. BUG();
  689. }
  690. static void takeover_tasklets(unsigned int cpu)
  691. {
  692. /* CPU is dead, so no lock needed. */
  693. local_irq_disable();
  694. /* Find end, append list for that CPU. */
  695. if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
  696. *__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
  697. this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
  698. per_cpu(tasklet_vec, cpu).head = NULL;
  699. per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
  700. }
  701. raise_softirq_irqoff(TASKLET_SOFTIRQ);
  702. if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
  703. *__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
  704. __this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
  705. per_cpu(tasklet_hi_vec, cpu).head = NULL;
  706. per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
  707. }
  708. raise_softirq_irqoff(HI_SOFTIRQ);
  709. local_irq_enable();
  710. }
  711. #endif /* CONFIG_HOTPLUG_CPU */
  712. static int __cpuinit cpu_callback(struct notifier_block *nfb,
  713. unsigned long action,
  714. void *hcpu)
  715. {
  716. int hotcpu = (unsigned long)hcpu;
  717. struct task_struct *p;
  718. switch (action) {
  719. case CPU_UP_PREPARE:
  720. case CPU_UP_PREPARE_FROZEN:
  721. p = kthread_create_on_node(run_ksoftirqd,
  722. hcpu,
  723. cpu_to_node(hotcpu),
  724. "ksoftirqd/%d", hotcpu);
  725. if (IS_ERR(p)) {
  726. printk("ksoftirqd for %i failed\n", hotcpu);
  727. return notifier_from_errno(PTR_ERR(p));
  728. }
  729. kthread_bind(p, hotcpu);
  730. per_cpu(ksoftirqd, hotcpu) = p;
  731. break;
  732. case CPU_ONLINE:
  733. case CPU_ONLINE_FROZEN:
  734. wake_up_process(per_cpu(ksoftirqd, hotcpu));
  735. break;
  736. #ifdef CONFIG_HOTPLUG_CPU
  737. case CPU_UP_CANCELED:
  738. case CPU_UP_CANCELED_FROZEN:
  739. if (!per_cpu(ksoftirqd, hotcpu))
  740. break;
  741. /* Unbind so it can run. Fall thru. */
  742. kthread_bind(per_cpu(ksoftirqd, hotcpu),
  743. cpumask_any(cpu_online_mask));
  744. case CPU_DEAD:
  745. case CPU_DEAD_FROZEN: {
  746. static const struct sched_param param = {
  747. .sched_priority = MAX_RT_PRIO-1
  748. };
  749. p = per_cpu(ksoftirqd, hotcpu);
  750. per_cpu(ksoftirqd, hotcpu) = NULL;
  751. sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
  752. kthread_stop(p);
  753. takeover_tasklets(hotcpu);
  754. break;
  755. }
  756. #endif /* CONFIG_HOTPLUG_CPU */
  757. }
  758. return NOTIFY_OK;
  759. }
  760. static struct notifier_block __cpuinitdata cpu_nfb = {
  761. .notifier_call = cpu_callback
  762. };
  763. static __init int spawn_ksoftirqd(void)
  764. {
  765. void *cpu = (void *)(long)smp_processor_id();
  766. int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  767. BUG_ON(err != NOTIFY_OK);
  768. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  769. register_cpu_notifier(&cpu_nfb);
  770. return 0;
  771. }
  772. early_initcall(spawn_ksoftirqd);
  773. /*
  774. * [ These __weak aliases are kept in a separate compilation unit, so that
  775. * GCC does not inline them incorrectly. ]
  776. */
  777. int __init __weak early_irq_init(void)
  778. {
  779. return 0;
  780. }
  781. #ifdef CONFIG_GENERIC_HARDIRQS
  782. int __init __weak arch_probe_nr_irqs(void)
  783. {
  784. return NR_IRQS_LEGACY;
  785. }
  786. int __init __weak arch_early_irq_init(void)
  787. {
  788. return 0;
  789. }
  790. #endif