softirq.c 20 KB

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