softirq.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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/export.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. #ifdef CONFIG_SEC_DEBUG
  30. #include <mach/sec_debug.h>
  31. #endif
  32. /*
  33. - No shared variables, all the data are CPU local.
  34. - If a softirq needs serialization, let it serialize itself
  35. by its own spinlocks.
  36. - Even if softirq is serialized, only local cpu is marked for
  37. execution. Hence, we get something sort of weak cpu binding.
  38. Though it is still not clear, will it result in better locality
  39. or will not.
  40. Examples:
  41. - NET RX softirq. It is multithreaded and does not require
  42. any global serialization.
  43. - NET TX softirq. It kicks software netdevice queues, hence
  44. it is logically serialized per device, but this serialization
  45. is invisible to common code.
  46. - Tasklets: serialized wrt itself.
  47. */
  48. #ifndef __ARCH_IRQ_STAT
  49. irq_cpustat_t irq_stat[NR_CPUS] ____cacheline_aligned;
  50. EXPORT_SYMBOL(irq_stat);
  51. #endif
  52. static struct softirq_action softirq_vec[NR_SOFTIRQS] __cacheline_aligned_in_smp;
  53. DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  54. char *softirq_to_name[NR_SOFTIRQS] = {
  55. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "BLOCK_IOPOLL",
  56. "TASKLET", "SCHED", "HRTIMER", "RCU"
  57. };
  58. /*
  59. * we cannot loop indefinitely here to avoid userspace starvation,
  60. * but we also don't want to introduce a worst case 1/HZ latency
  61. * to the pending events, so lets the scheduler to balance
  62. * the softirq load for us.
  63. */
  64. static void wakeup_softirqd(void)
  65. {
  66. /* Interrupts are disabled: no need to stop preemption */
  67. struct task_struct *tsk = __this_cpu_read(ksoftirqd);
  68. if (tsk && tsk->state != TASK_RUNNING)
  69. wake_up_process(tsk);
  70. }
  71. /*
  72. * preempt_count and SOFTIRQ_OFFSET usage:
  73. * - preempt_count is changed by SOFTIRQ_OFFSET on entering or leaving
  74. * softirq processing.
  75. * - preempt_count is changed by SOFTIRQ_DISABLE_OFFSET (= 2 * SOFTIRQ_OFFSET)
  76. * on local_bh_disable or local_bh_enable.
  77. * This lets us distinguish between whether we are currently processing
  78. * softirq and whether we just have bh disabled.
  79. */
  80. /*
  81. * This one is for softirq.c-internal use,
  82. * where hardirqs are disabled legitimately:
  83. */
  84. #ifdef CONFIG_TRACE_IRQFLAGS
  85. static void __local_bh_disable(unsigned long ip, unsigned int cnt)
  86. {
  87. unsigned long flags;
  88. WARN_ON_ONCE(in_irq());
  89. raw_local_irq_save(flags);
  90. /*
  91. * The preempt tracer hooks into add_preempt_count and will break
  92. * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET
  93. * is set and before current->softirq_enabled is cleared.
  94. * We must manually increment preempt_count here and manually
  95. * call the trace_preempt_off later.
  96. */
  97. preempt_count() += cnt;
  98. /*
  99. * Were softirqs turned off above:
  100. */
  101. if (softirq_count() == cnt)
  102. trace_softirqs_off(ip);
  103. raw_local_irq_restore(flags);
  104. if (preempt_count() == cnt)
  105. trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  106. }
  107. #else /* !CONFIG_TRACE_IRQFLAGS */
  108. static inline void __local_bh_disable(unsigned long ip, unsigned int cnt)
  109. {
  110. add_preempt_count(cnt);
  111. barrier();
  112. }
  113. #endif /* CONFIG_TRACE_IRQFLAGS */
  114. void local_bh_disable(void)
  115. {
  116. __local_bh_disable(_RET_IP_, SOFTIRQ_DISABLE_OFFSET);
  117. }
  118. EXPORT_SYMBOL(local_bh_disable);
  119. static void __local_bh_enable(unsigned int cnt)
  120. {
  121. WARN_ON_ONCE(in_irq());
  122. WARN_ON_ONCE(!irqs_disabled());
  123. if (softirq_count() == cnt)
  124. trace_softirqs_on(_RET_IP_);
  125. sub_preempt_count(cnt);
  126. }
  127. /*
  128. * Special-case - softirqs can safely be enabled in
  129. * cond_resched_softirq(), or by __do_softirq(),
  130. * without processing still-pending softirqs:
  131. */
  132. void _local_bh_enable(void)
  133. {
  134. __local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
  135. }
  136. EXPORT_SYMBOL(_local_bh_enable);
  137. static inline void _local_bh_enable_ip(unsigned long ip)
  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. sub_preempt_count(SOFTIRQ_DISABLE_OFFSET - 1);
  153. if (unlikely(!in_interrupt() && local_softirq_pending()))
  154. do_softirq();
  155. dec_preempt_count();
  156. #ifdef CONFIG_TRACE_IRQFLAGS
  157. local_irq_enable();
  158. #endif
  159. preempt_check_resched();
  160. }
  161. void local_bh_enable(void)
  162. {
  163. _local_bh_enable_ip(_RET_IP_);
  164. }
  165. EXPORT_SYMBOL(local_bh_enable);
  166. void local_bh_enable_ip(unsigned long ip)
  167. {
  168. _local_bh_enable_ip(ip);
  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. asmlinkage void __do_softirq(void)
  187. {
  188. struct softirq_action *h;
  189. __u32 pending;
  190. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  191. int cpu;
  192. int max_restart = MAX_SOFTIRQ_RESTART;
  193. pending = local_softirq_pending();
  194. account_system_vtime(current);
  195. __local_bh_disable(_RET_IP_, SOFTIRQ_OFFSET);
  196. lockdep_softirq_enter();
  197. cpu = smp_processor_id();
  198. restart:
  199. /* Reset the pending bitmask before enabling irqs */
  200. set_softirq_pending(0);
  201. local_irq_enable();
  202. h = softirq_vec;
  203. do {
  204. if (pending & 1) {
  205. unsigned int vec_nr = h - softirq_vec;
  206. int prev_count = preempt_count();
  207. kstat_incr_softirqs_this_cpu(vec_nr);
  208. trace_softirq_entry(vec_nr);
  209. #ifdef CONFIG_SEC_DEBUG
  210. secdbg_msg("softirq %pS entry", h->action);
  211. #endif
  212. h->action(h);
  213. #ifdef CONFIG_SEC_DEBUG
  214. secdbg_msg("softirq %pS exit", h->action);
  215. #endif
  216. trace_softirq_exit(vec_nr);
  217. if (unlikely(prev_count != preempt_count())) {
  218. printk(KERN_ERR "huh, entered softirq %u %s %p"
  219. "with preempt_count %08x,"
  220. " exited with %08x?\n", vec_nr,
  221. softirq_to_name[vec_nr], h->action,
  222. prev_count, preempt_count());
  223. preempt_count() = prev_count;
  224. }
  225. rcu_bh_qs(cpu);
  226. }
  227. h++;
  228. pending >>= 1;
  229. } while (pending);
  230. local_irq_disable();
  231. pending = local_softirq_pending();
  232. if (pending) {
  233. if (time_before(jiffies, end) && !need_resched() &&
  234. --max_restart)
  235. goto restart;
  236. wakeup_softirqd();
  237. }
  238. lockdep_softirq_exit();
  239. account_system_vtime(current);
  240. __local_bh_enable(SOFTIRQ_OFFSET);
  241. }
  242. #ifndef __ARCH_HAS_DO_SOFTIRQ
  243. asmlinkage void do_softirq(void)
  244. {
  245. __u32 pending;
  246. unsigned long flags;
  247. if (in_interrupt())
  248. return;
  249. local_irq_save(flags);
  250. pending = local_softirq_pending();
  251. if (pending)
  252. __do_softirq();
  253. local_irq_restore(flags);
  254. }
  255. #endif
  256. /*
  257. * Enter an interrupt context.
  258. */
  259. void irq_enter(void)
  260. {
  261. int cpu = smp_processor_id();
  262. rcu_irq_enter();
  263. if (is_idle_task(current) && !in_interrupt()) {
  264. /*
  265. * Prevent raise_softirq from needlessly waking up ksoftirqd
  266. * here, as softirq will be serviced on return from interrupt.
  267. */
  268. local_bh_disable();
  269. tick_check_idle(cpu);
  270. _local_bh_enable();
  271. }
  272. __irq_enter();
  273. }
  274. static inline void invoke_softirq(void)
  275. {
  276. if (!force_irqthreads) {
  277. #ifdef __ARCH_IRQ_EXIT_IRQS_DISABLED
  278. __do_softirq();
  279. #else
  280. do_softirq();
  281. #endif
  282. } else {
  283. __local_bh_disable(_RET_IP_, SOFTIRQ_OFFSET);
  284. wakeup_softirqd();
  285. __local_bh_enable(SOFTIRQ_OFFSET);
  286. }
  287. }
  288. /*
  289. * Exit an interrupt context. Process softirqs if needed and possible:
  290. */
  291. void irq_exit(void)
  292. {
  293. account_system_vtime(current);
  294. trace_hardirq_exit();
  295. #ifdef CONFIG_SEC_DEBUG
  296. secdbg_msg("hardirq exit");
  297. #endif
  298. sub_preempt_count(IRQ_EXIT_OFFSET);
  299. if (!in_interrupt() && local_softirq_pending())
  300. invoke_softirq();
  301. #ifdef CONFIG_NO_HZ
  302. /* Make sure that timer wheel updates are propagated */
  303. if (idle_cpu(smp_processor_id()) && !in_interrupt() && !need_resched())
  304. tick_nohz_irq_exit();
  305. #endif
  306. rcu_irq_exit();
  307. sched_preempt_enable_no_resched();
  308. }
  309. /*
  310. * This function must run with irqs disabled!
  311. */
  312. inline void raise_softirq_irqoff(unsigned int nr)
  313. {
  314. __raise_softirq_irqoff(nr);
  315. /*
  316. * If we're in an interrupt or softirq, we're done
  317. * (this also catches softirq-disabled code). We will
  318. * actually run the softirq once we return from
  319. * the irq or softirq.
  320. *
  321. * Otherwise we wake up ksoftirqd to make sure we
  322. * schedule the softirq soon.
  323. */
  324. if (!in_interrupt())
  325. wakeup_softirqd();
  326. }
  327. void raise_softirq(unsigned int nr)
  328. {
  329. unsigned long flags;
  330. local_irq_save(flags);
  331. raise_softirq_irqoff(nr);
  332. local_irq_restore(flags);
  333. }
  334. void __raise_softirq_irqoff(unsigned int nr)
  335. {
  336. trace_softirq_raise(nr);
  337. or_softirq_pending(1UL << nr);
  338. }
  339. void open_softirq(int nr, void (*action)(struct softirq_action *))
  340. {
  341. softirq_vec[nr].action = action;
  342. }
  343. /*
  344. * Tasklets
  345. */
  346. struct tasklet_head
  347. {
  348. struct tasklet_struct *head;
  349. struct tasklet_struct **tail;
  350. };
  351. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  352. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  353. void __tasklet_schedule(struct tasklet_struct *t)
  354. {
  355. unsigned long flags;
  356. local_irq_save(flags);
  357. t->next = NULL;
  358. *__this_cpu_read(tasklet_vec.tail) = t;
  359. __this_cpu_write(tasklet_vec.tail, &(t->next));
  360. raise_softirq_irqoff(TASKLET_SOFTIRQ);
  361. local_irq_restore(flags);
  362. }
  363. EXPORT_SYMBOL(__tasklet_schedule);
  364. void __tasklet_hi_schedule(struct tasklet_struct *t)
  365. {
  366. unsigned long flags;
  367. local_irq_save(flags);
  368. t->next = NULL;
  369. *__this_cpu_read(tasklet_hi_vec.tail) = t;
  370. __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  371. raise_softirq_irqoff(HI_SOFTIRQ);
  372. local_irq_restore(flags);
  373. }
  374. EXPORT_SYMBOL(__tasklet_hi_schedule);
  375. void __tasklet_hi_schedule_first(struct tasklet_struct *t)
  376. {
  377. BUG_ON(!irqs_disabled());
  378. t->next = __this_cpu_read(tasklet_hi_vec.head);
  379. __this_cpu_write(tasklet_hi_vec.head, t);
  380. __raise_softirq_irqoff(HI_SOFTIRQ);
  381. }
  382. EXPORT_SYMBOL(__tasklet_hi_schedule_first);
  383. static void tasklet_action(struct softirq_action *a)
  384. {
  385. struct tasklet_struct *list;
  386. local_irq_disable();
  387. list = __this_cpu_read(tasklet_vec.head);
  388. __this_cpu_write(tasklet_vec.head, NULL);
  389. __this_cpu_write(tasklet_vec.tail, &__get_cpu_var(tasklet_vec).head);
  390. local_irq_enable();
  391. while (list) {
  392. struct tasklet_struct *t = list;
  393. list = list->next;
  394. if (tasklet_trylock(t)) {
  395. if (!atomic_read(&t->count)) {
  396. if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  397. BUG();
  398. #ifdef CONFIG_SEC_DEBUG
  399. sec_debug_irq_sched_log(-1, t->func, 3);
  400. t->func(t->data);
  401. sec_debug_irq_sched_log(-1, t->func, 4);
  402. #else
  403. t->func(t->data);
  404. #endif
  405. tasklet_unlock(t);
  406. continue;
  407. }
  408. tasklet_unlock(t);
  409. }
  410. local_irq_disable();
  411. t->next = NULL;
  412. *__this_cpu_read(tasklet_vec.tail) = t;
  413. __this_cpu_write(tasklet_vec.tail, &(t->next));
  414. __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  415. local_irq_enable();
  416. }
  417. }
  418. static void tasklet_hi_action(struct softirq_action *a)
  419. {
  420. struct tasklet_struct *list;
  421. local_irq_disable();
  422. list = __this_cpu_read(tasklet_hi_vec.head);
  423. __this_cpu_write(tasklet_hi_vec.head, NULL);
  424. __this_cpu_write(tasklet_hi_vec.tail, &__get_cpu_var(tasklet_hi_vec).head);
  425. local_irq_enable();
  426. while (list) {
  427. struct tasklet_struct *t = list;
  428. list = list->next;
  429. if (tasklet_trylock(t)) {
  430. if (!atomic_read(&t->count)) {
  431. if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  432. BUG();
  433. t->func(t->data);
  434. tasklet_unlock(t);
  435. continue;
  436. }
  437. tasklet_unlock(t);
  438. }
  439. local_irq_disable();
  440. t->next = NULL;
  441. *__this_cpu_read(tasklet_hi_vec.tail) = t;
  442. __this_cpu_write(tasklet_hi_vec.tail, &(t->next));
  443. __raise_softirq_irqoff(HI_SOFTIRQ);
  444. local_irq_enable();
  445. }
  446. }
  447. void tasklet_init(struct tasklet_struct *t,
  448. void (*func)(unsigned long), unsigned long data)
  449. {
  450. t->next = NULL;
  451. t->state = 0;
  452. atomic_set(&t->count, 0);
  453. t->func = func;
  454. t->data = data;
  455. }
  456. EXPORT_SYMBOL(tasklet_init);
  457. void tasklet_kill(struct tasklet_struct *t)
  458. {
  459. if (in_interrupt())
  460. printk("Attempt to kill tasklet from interrupt\n");
  461. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  462. do {
  463. yield();
  464. } while (test_bit(TASKLET_STATE_SCHED, &t->state));
  465. }
  466. tasklet_unlock_wait(t);
  467. clear_bit(TASKLET_STATE_SCHED, &t->state);
  468. }
  469. EXPORT_SYMBOL(tasklet_kill);
  470. /*
  471. * tasklet_hrtimer
  472. */
  473. /*
  474. * The trampoline is called when the hrtimer expires. It schedules a tasklet
  475. * to run __tasklet_hrtimer_trampoline() which in turn will call the intended
  476. * hrtimer callback, but from softirq context.
  477. */
  478. static enum hrtimer_restart __hrtimer_tasklet_trampoline(struct hrtimer *timer)
  479. {
  480. struct tasklet_hrtimer *ttimer =
  481. container_of(timer, struct tasklet_hrtimer, timer);
  482. tasklet_hi_schedule(&ttimer->tasklet);
  483. return HRTIMER_NORESTART;
  484. }
  485. /*
  486. * Helper function which calls the hrtimer callback from
  487. * tasklet/softirq context
  488. */
  489. static void __tasklet_hrtimer_trampoline(unsigned long data)
  490. {
  491. struct tasklet_hrtimer *ttimer = (void *)data;
  492. enum hrtimer_restart restart;
  493. restart = ttimer->function(&ttimer->timer);
  494. if (restart != HRTIMER_NORESTART)
  495. hrtimer_restart(&ttimer->timer);
  496. }
  497. /**
  498. * tasklet_hrtimer_init - Init a tasklet/hrtimer combo for softirq callbacks
  499. * @ttimer: tasklet_hrtimer which is initialized
  500. * @function: hrtimer callback function which gets called from softirq context
  501. * @which_clock: clock id (CLOCK_MONOTONIC/CLOCK_REALTIME)
  502. * @mode: hrtimer mode (HRTIMER_MODE_ABS/HRTIMER_MODE_REL)
  503. */
  504. void tasklet_hrtimer_init(struct tasklet_hrtimer *ttimer,
  505. enum hrtimer_restart (*function)(struct hrtimer *),
  506. clockid_t which_clock, enum hrtimer_mode mode)
  507. {
  508. hrtimer_init(&ttimer->timer, which_clock, mode);
  509. ttimer->timer.function = __hrtimer_tasklet_trampoline;
  510. tasklet_init(&ttimer->tasklet, __tasklet_hrtimer_trampoline,
  511. (unsigned long)ttimer);
  512. ttimer->function = function;
  513. }
  514. EXPORT_SYMBOL_GPL(tasklet_hrtimer_init);
  515. /*
  516. * Remote softirq bits
  517. */
  518. DEFINE_PER_CPU(struct list_head [NR_SOFTIRQS], softirq_work_list);
  519. EXPORT_PER_CPU_SYMBOL(softirq_work_list);
  520. static void __local_trigger(struct call_single_data *cp, int softirq)
  521. {
  522. struct list_head *head = &__get_cpu_var(softirq_work_list[softirq]);
  523. list_add_tail(&cp->list, head);
  524. /* Trigger the softirq only if the list was previously empty. */
  525. if (head->next == &cp->list)
  526. raise_softirq_irqoff(softirq);
  527. }
  528. #ifdef CONFIG_USE_GENERIC_SMP_HELPERS
  529. static void remote_softirq_receive(void *data)
  530. {
  531. struct call_single_data *cp = data;
  532. unsigned long flags;
  533. int softirq;
  534. softirq = cp->priv;
  535. local_irq_save(flags);
  536. __local_trigger(cp, softirq);
  537. local_irq_restore(flags);
  538. }
  539. static int __try_remote_softirq(struct call_single_data *cp, int cpu, int softirq)
  540. {
  541. if (cpu_online(cpu)) {
  542. cp->func = remote_softirq_receive;
  543. cp->info = cp;
  544. cp->flags = 0;
  545. cp->priv = softirq;
  546. __smp_call_function_single(cpu, cp, 0);
  547. return 0;
  548. }
  549. return 1;
  550. }
  551. #else /* CONFIG_USE_GENERIC_SMP_HELPERS */
  552. static int __try_remote_softirq(struct call_single_data *cp, int cpu, int softirq)
  553. {
  554. return 1;
  555. }
  556. #endif
  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. * @this_cpu: the currently executing cpu
  562. * @softirq: the softirq for the work
  563. *
  564. * Attempt to schedule softirq work on a remote cpu. If this cannot be
  565. * done, the work is instead queued up on the local cpu.
  566. *
  567. * Interrupts must be disabled.
  568. */
  569. void __send_remote_softirq(struct call_single_data *cp, int cpu, int this_cpu, int softirq)
  570. {
  571. if (cpu == this_cpu || __try_remote_softirq(cp, cpu, softirq))
  572. __local_trigger(cp, softirq);
  573. }
  574. EXPORT_SYMBOL(__send_remote_softirq);
  575. /**
  576. * send_remote_softirq - try to schedule softirq work on a remote cpu
  577. * @cp: private SMP call function data area
  578. * @cpu: the remote cpu
  579. * @softirq: the softirq for the work
  580. *
  581. * Like __send_remote_softirq except that disabling interrupts and
  582. * computing the current cpu is done for the caller.
  583. */
  584. void send_remote_softirq(struct call_single_data *cp, int cpu, int softirq)
  585. {
  586. unsigned long flags;
  587. int this_cpu;
  588. local_irq_save(flags);
  589. this_cpu = smp_processor_id();
  590. __send_remote_softirq(cp, cpu, this_cpu, softirq);
  591. local_irq_restore(flags);
  592. }
  593. EXPORT_SYMBOL(send_remote_softirq);
  594. static int __cpuinit remote_softirq_cpu_notify(struct notifier_block *self,
  595. unsigned long action, void *hcpu)
  596. {
  597. /*
  598. * If a CPU goes away, splice its entries to the current CPU
  599. * and trigger a run of the softirq
  600. */
  601. if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
  602. int cpu = (unsigned long) hcpu;
  603. int i;
  604. local_irq_disable();
  605. for (i = 0; i < NR_SOFTIRQS; i++) {
  606. struct list_head *head = &per_cpu(softirq_work_list[i], cpu);
  607. struct list_head *local_head;
  608. if (list_empty(head))
  609. continue;
  610. local_head = &__get_cpu_var(softirq_work_list[i]);
  611. list_splice_init(head, local_head);
  612. raise_softirq_irqoff(i);
  613. }
  614. local_irq_enable();
  615. }
  616. return NOTIFY_OK;
  617. }
  618. static struct notifier_block __cpuinitdata remote_softirq_cpu_notifier = {
  619. .notifier_call = remote_softirq_cpu_notify,
  620. };
  621. void __init softirq_init(void)
  622. {
  623. int cpu;
  624. for_each_possible_cpu(cpu) {
  625. int i;
  626. per_cpu(tasklet_vec, cpu).tail =
  627. &per_cpu(tasklet_vec, cpu).head;
  628. per_cpu(tasklet_hi_vec, cpu).tail =
  629. &per_cpu(tasklet_hi_vec, cpu).head;
  630. for (i = 0; i < NR_SOFTIRQS; i++)
  631. INIT_LIST_HEAD(&per_cpu(softirq_work_list[i], cpu));
  632. }
  633. register_hotcpu_notifier(&remote_softirq_cpu_notifier);
  634. open_softirq(TASKLET_SOFTIRQ, tasklet_action);
  635. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  636. }
  637. static int run_ksoftirqd(void * __bind_cpu)
  638. {
  639. set_current_state(TASK_INTERRUPTIBLE);
  640. while (!kthread_should_stop()) {
  641. preempt_disable();
  642. if (!local_softirq_pending()) {
  643. schedule_preempt_disabled();
  644. }
  645. __set_current_state(TASK_RUNNING);
  646. while (local_softirq_pending()) {
  647. /* Preempt disable stops cpu going offline.
  648. If already offline, we'll be on wrong CPU:
  649. don't process */
  650. if (cpu_is_offline((long)__bind_cpu))
  651. goto wait_to_die;
  652. local_irq_disable();
  653. if (local_softirq_pending())
  654. __do_softirq();
  655. local_irq_enable();
  656. sched_preempt_enable_no_resched();
  657. cond_resched();
  658. preempt_disable();
  659. rcu_note_context_switch((long)__bind_cpu);
  660. }
  661. preempt_enable();
  662. set_current_state(TASK_INTERRUPTIBLE);
  663. }
  664. __set_current_state(TASK_RUNNING);
  665. return 0;
  666. wait_to_die:
  667. preempt_enable();
  668. /* Wait for kthread_stop */
  669. set_current_state(TASK_INTERRUPTIBLE);
  670. while (!kthread_should_stop()) {
  671. schedule();
  672. set_current_state(TASK_INTERRUPTIBLE);
  673. }
  674. __set_current_state(TASK_RUNNING);
  675. return 0;
  676. }
  677. #ifdef CONFIG_HOTPLUG_CPU
  678. /*
  679. * tasklet_kill_immediate is called to remove a tasklet which can already be
  680. * scheduled for execution on @cpu.
  681. *
  682. * Unlike tasklet_kill, this function removes the tasklet
  683. * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
  684. *
  685. * When this function is called, @cpu must be in the CPU_DEAD state.
  686. */
  687. void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
  688. {
  689. struct tasklet_struct **i;
  690. BUG_ON(cpu_online(cpu));
  691. BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
  692. if (!test_bit(TASKLET_STATE_SCHED, &t->state))
  693. return;
  694. /* CPU is dead, so no lock needed. */
  695. for (i = &per_cpu(tasklet_vec, cpu).head; *i; i = &(*i)->next) {
  696. if (*i == t) {
  697. *i = t->next;
  698. /* If this was the tail element, move the tail ptr */
  699. if (*i == NULL)
  700. per_cpu(tasklet_vec, cpu).tail = i;
  701. return;
  702. }
  703. }
  704. BUG();
  705. }
  706. static void takeover_tasklets(unsigned int cpu)
  707. {
  708. /* CPU is dead, so no lock needed. */
  709. local_irq_disable();
  710. /* Find end, append list for that CPU. */
  711. if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
  712. *__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
  713. this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
  714. per_cpu(tasklet_vec, cpu).head = NULL;
  715. per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
  716. }
  717. raise_softirq_irqoff(TASKLET_SOFTIRQ);
  718. if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
  719. *__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
  720. __this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
  721. per_cpu(tasklet_hi_vec, cpu).head = NULL;
  722. per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
  723. }
  724. raise_softirq_irqoff(HI_SOFTIRQ);
  725. local_irq_enable();
  726. }
  727. #endif /* CONFIG_HOTPLUG_CPU */
  728. static int __cpuinit cpu_callback(struct notifier_block *nfb,
  729. unsigned long action,
  730. void *hcpu)
  731. {
  732. int hotcpu = (unsigned long)hcpu;
  733. struct task_struct *p;
  734. switch (action) {
  735. case CPU_UP_PREPARE:
  736. case CPU_UP_PREPARE_FROZEN:
  737. p = kthread_create_on_node(run_ksoftirqd,
  738. hcpu,
  739. cpu_to_node(hotcpu),
  740. "ksoftirqd/%d", hotcpu);
  741. if (IS_ERR(p)) {
  742. printk("ksoftirqd for %i failed\n", hotcpu);
  743. return notifier_from_errno(PTR_ERR(p));
  744. }
  745. kthread_bind(p, hotcpu);
  746. per_cpu(ksoftirqd, hotcpu) = p;
  747. break;
  748. case CPU_ONLINE:
  749. case CPU_ONLINE_FROZEN:
  750. wake_up_process(per_cpu(ksoftirqd, hotcpu));
  751. break;
  752. #ifdef CONFIG_HOTPLUG_CPU
  753. case CPU_UP_CANCELED:
  754. case CPU_UP_CANCELED_FROZEN:
  755. if (!per_cpu(ksoftirqd, hotcpu))
  756. break;
  757. /* Unbind so it can run. Fall thru. */
  758. kthread_bind(per_cpu(ksoftirqd, hotcpu),
  759. cpumask_any(cpu_online_mask));
  760. case CPU_DEAD:
  761. case CPU_DEAD_FROZEN: {
  762. static const struct sched_param param = {
  763. .sched_priority = MAX_RT_PRIO-1
  764. };
  765. p = per_cpu(ksoftirqd, hotcpu);
  766. per_cpu(ksoftirqd, hotcpu) = NULL;
  767. sched_setscheduler_nocheck(p, SCHED_FIFO, &param);
  768. kthread_stop(p);
  769. takeover_tasklets(hotcpu);
  770. break;
  771. }
  772. #endif /* CONFIG_HOTPLUG_CPU */
  773. }
  774. return NOTIFY_OK;
  775. }
  776. static struct notifier_block __cpuinitdata cpu_nfb = {
  777. .notifier_call = cpu_callback
  778. };
  779. static __init int spawn_ksoftirqd(void)
  780. {
  781. void *cpu = (void *)(long)smp_processor_id();
  782. int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  783. BUG_ON(err != NOTIFY_OK);
  784. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  785. register_cpu_notifier(&cpu_nfb);
  786. return 0;
  787. }
  788. early_initcall(spawn_ksoftirqd);
  789. /*
  790. * [ These __weak aliases are kept in a separate compilation unit, so that
  791. * GCC does not inline them incorrectly. ]
  792. */
  793. int __init __weak early_irq_init(void)
  794. {
  795. return 0;
  796. }
  797. #ifdef CONFIG_GENERIC_HARDIRQS
  798. int __init __weak arch_probe_nr_irqs(void)
  799. {
  800. return NR_IRQS_LEGACY;
  801. }
  802. int __init __weak arch_early_irq_init(void)
  803. {
  804. return 0;
  805. }
  806. #endif