softirq.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * linux/kernel/softirq.c
  3. *
  4. * Copyright (C) 1992 Linus Torvalds
  5. *
  6. * Rewritten. Old one was good in 2.2, but in 2.3 it was immoral. --ANK (990903)
  7. */
  8. #include <linux/module.h>
  9. #include <linux/kernel_stat.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/init.h>
  12. #include <linux/mm.h>
  13. #include <linux/notifier.h>
  14. #include <linux/percpu.h>
  15. #include <linux/cpu.h>
  16. #include <linux/kthread.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/smp.h>
  19. #include <linux/tick.h>
  20. #include <asm/irq.h>
  21. /*
  22. - No shared variables, all the data are CPU local.
  23. - If a softirq needs serialization, let it serialize itself
  24. by its own spinlocks.
  25. - Even if softirq is serialized, only local cpu is marked for
  26. execution. Hence, we get something sort of weak cpu binding.
  27. Though it is still not clear, will it result in better locality
  28. or will not.
  29. Examples:
  30. - NET RX softirq. It is multithreaded and does not require
  31. any global serialization.
  32. - NET TX softirq. It kicks software netdevice queues, hence
  33. it is logically serialized per device, but this serialization
  34. is invisible to common code.
  35. - Tasklets: serialized wrt itself.
  36. */
  37. #ifndef __ARCH_IRQ_STAT
  38. irq_cpustat_t irq_stat[NR_CPUS] ____cacheline_aligned;
  39. EXPORT_SYMBOL(irq_stat);
  40. #endif
  41. static struct softirq_action softirq_vec[32] __cacheline_aligned_in_smp;
  42. static DEFINE_PER_CPU(struct task_struct *, ksoftirqd);
  43. /*
  44. * we cannot loop indefinitely here to avoid userspace starvation,
  45. * but we also don't want to introduce a worst case 1/HZ latency
  46. * to the pending events, so lets the scheduler to balance
  47. * the softirq load for us.
  48. */
  49. static inline void wakeup_softirqd(void)
  50. {
  51. /* Interrupts are disabled: no need to stop preemption */
  52. struct task_struct *tsk = __get_cpu_var(ksoftirqd);
  53. if (tsk && tsk->state != TASK_RUNNING)
  54. wake_up_process(tsk);
  55. }
  56. /*
  57. * This one is for softirq.c-internal use,
  58. * where hardirqs are disabled legitimately:
  59. */
  60. #ifdef CONFIG_TRACE_IRQFLAGS
  61. static void __local_bh_disable(unsigned long ip)
  62. {
  63. unsigned long flags;
  64. WARN_ON_ONCE(in_irq());
  65. raw_local_irq_save(flags);
  66. add_preempt_count(SOFTIRQ_OFFSET);
  67. /*
  68. * Were softirqs turned off above:
  69. */
  70. if (softirq_count() == SOFTIRQ_OFFSET)
  71. trace_softirqs_off(ip);
  72. raw_local_irq_restore(flags);
  73. }
  74. #else /* !CONFIG_TRACE_IRQFLAGS */
  75. static inline void __local_bh_disable(unsigned long ip)
  76. {
  77. add_preempt_count(SOFTIRQ_OFFSET);
  78. barrier();
  79. }
  80. #endif /* CONFIG_TRACE_IRQFLAGS */
  81. void local_bh_disable(void)
  82. {
  83. __local_bh_disable((unsigned long)__builtin_return_address(0));
  84. }
  85. EXPORT_SYMBOL(local_bh_disable);
  86. void __local_bh_enable(void)
  87. {
  88. WARN_ON_ONCE(in_irq());
  89. /*
  90. * softirqs should never be enabled by __local_bh_enable(),
  91. * it always nests inside local_bh_enable() sections:
  92. */
  93. WARN_ON_ONCE(softirq_count() == SOFTIRQ_OFFSET);
  94. sub_preempt_count(SOFTIRQ_OFFSET);
  95. }
  96. EXPORT_SYMBOL_GPL(__local_bh_enable);
  97. /*
  98. * Special-case - softirqs can safely be enabled in
  99. * cond_resched_softirq(), or by __do_softirq(),
  100. * without processing still-pending softirqs:
  101. */
  102. void _local_bh_enable(void)
  103. {
  104. WARN_ON_ONCE(in_irq());
  105. WARN_ON_ONCE(!irqs_disabled());
  106. if (softirq_count() == SOFTIRQ_OFFSET)
  107. trace_softirqs_on((unsigned long)__builtin_return_address(0));
  108. sub_preempt_count(SOFTIRQ_OFFSET);
  109. }
  110. EXPORT_SYMBOL(_local_bh_enable);
  111. void local_bh_enable(void)
  112. {
  113. #ifdef CONFIG_TRACE_IRQFLAGS
  114. unsigned long flags;
  115. WARN_ON_ONCE(in_irq());
  116. #endif
  117. WARN_ON_ONCE(irqs_disabled());
  118. #ifdef CONFIG_TRACE_IRQFLAGS
  119. local_irq_save(flags);
  120. #endif
  121. /*
  122. * Are softirqs going to be turned on now:
  123. */
  124. if (softirq_count() == SOFTIRQ_OFFSET)
  125. trace_softirqs_on((unsigned long)__builtin_return_address(0));
  126. /*
  127. * Keep preemption disabled until we are done with
  128. * softirq processing:
  129. */
  130. sub_preempt_count(SOFTIRQ_OFFSET - 1);
  131. if (unlikely(!in_interrupt() && local_softirq_pending()))
  132. do_softirq();
  133. dec_preempt_count();
  134. #ifdef CONFIG_TRACE_IRQFLAGS
  135. local_irq_restore(flags);
  136. #endif
  137. preempt_check_resched();
  138. }
  139. EXPORT_SYMBOL(local_bh_enable);
  140. void local_bh_enable_ip(unsigned long ip)
  141. {
  142. #ifdef CONFIG_TRACE_IRQFLAGS
  143. unsigned long flags;
  144. WARN_ON_ONCE(in_irq());
  145. local_irq_save(flags);
  146. #endif
  147. /*
  148. * Are softirqs going to be turned on now:
  149. */
  150. if (softirq_count() == SOFTIRQ_OFFSET)
  151. trace_softirqs_on(ip);
  152. /*
  153. * Keep preemption disabled until we are done with
  154. * softirq processing:
  155. */
  156. sub_preempt_count(SOFTIRQ_OFFSET - 1);
  157. if (unlikely(!in_interrupt() && local_softirq_pending()))
  158. do_softirq();
  159. dec_preempt_count();
  160. #ifdef CONFIG_TRACE_IRQFLAGS
  161. local_irq_restore(flags);
  162. #endif
  163. preempt_check_resched();
  164. }
  165. EXPORT_SYMBOL(local_bh_enable_ip);
  166. /*
  167. * We restart softirq processing MAX_SOFTIRQ_RESTART times,
  168. * and we fall back to softirqd after that.
  169. *
  170. * This number has been established via experimentation.
  171. * The two things to balance is latency against fairness -
  172. * we want to handle softirqs as soon as possible, but they
  173. * should not be able to lock up the box.
  174. */
  175. #define MAX_SOFTIRQ_RESTART 10
  176. asmlinkage void __do_softirq(void)
  177. {
  178. struct softirq_action *h;
  179. __u32 pending;
  180. int max_restart = MAX_SOFTIRQ_RESTART;
  181. int cpu;
  182. pending = local_softirq_pending();
  183. account_system_vtime(current);
  184. __local_bh_disable((unsigned long)__builtin_return_address(0));
  185. trace_softirq_enter();
  186. cpu = smp_processor_id();
  187. restart:
  188. /* Reset the pending bitmask before enabling irqs */
  189. set_softirq_pending(0);
  190. local_irq_enable();
  191. h = softirq_vec;
  192. do {
  193. if (pending & 1) {
  194. h->action(h);
  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. trace_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. EXPORT_SYMBOL(do_softirq);
  224. #endif
  225. /*
  226. * Enter an interrupt context.
  227. */
  228. void irq_enter(void)
  229. {
  230. __irq_enter();
  231. #ifdef CONFIG_NO_HZ
  232. if (idle_cpu(smp_processor_id()))
  233. tick_nohz_update_jiffies();
  234. #endif
  235. }
  236. #ifdef __ARCH_IRQ_EXIT_IRQS_DISABLED
  237. # define invoke_softirq() __do_softirq()
  238. #else
  239. # define invoke_softirq() do_softirq()
  240. #endif
  241. /*
  242. * Exit an interrupt context. Process softirqs if needed and possible:
  243. */
  244. void irq_exit(void)
  245. {
  246. account_system_vtime(current);
  247. trace_hardirq_exit();
  248. sub_preempt_count(IRQ_EXIT_OFFSET);
  249. if (!in_interrupt() && local_softirq_pending())
  250. invoke_softirq();
  251. #ifdef CONFIG_NO_HZ
  252. /* Make sure that timer wheel updates are propagated */
  253. if (!in_interrupt() && idle_cpu(smp_processor_id()) && !need_resched())
  254. tick_nohz_stop_sched_tick();
  255. #endif
  256. preempt_enable_no_resched();
  257. }
  258. /*
  259. * This function must run with irqs disabled!
  260. */
  261. inline fastcall void raise_softirq_irqoff(unsigned int nr)
  262. {
  263. __raise_softirq_irqoff(nr);
  264. /*
  265. * If we're in an interrupt or softirq, we're done
  266. * (this also catches softirq-disabled code). We will
  267. * actually run the softirq once we return from
  268. * the irq or softirq.
  269. *
  270. * Otherwise we wake up ksoftirqd to make sure we
  271. * schedule the softirq soon.
  272. */
  273. if (!in_interrupt())
  274. wakeup_softirqd();
  275. }
  276. EXPORT_SYMBOL(raise_softirq_irqoff);
  277. void fastcall raise_softirq(unsigned int nr)
  278. {
  279. unsigned long flags;
  280. local_irq_save(flags);
  281. raise_softirq_irqoff(nr);
  282. local_irq_restore(flags);
  283. }
  284. void open_softirq(int nr, void (*action)(struct softirq_action*), void *data)
  285. {
  286. softirq_vec[nr].data = data;
  287. softirq_vec[nr].action = action;
  288. }
  289. /* Tasklets */
  290. struct tasklet_head
  291. {
  292. struct tasklet_struct *list;
  293. };
  294. /* Some compilers disobey section attribute on statics when not
  295. initialized -- RR */
  296. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec) = { NULL };
  297. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec) = { NULL };
  298. void fastcall __tasklet_schedule(struct tasklet_struct *t)
  299. {
  300. unsigned long flags;
  301. local_irq_save(flags);
  302. t->next = __get_cpu_var(tasklet_vec).list;
  303. __get_cpu_var(tasklet_vec).list = t;
  304. raise_softirq_irqoff(TASKLET_SOFTIRQ);
  305. local_irq_restore(flags);
  306. }
  307. EXPORT_SYMBOL(__tasklet_schedule);
  308. void fastcall __tasklet_hi_schedule(struct tasklet_struct *t)
  309. {
  310. unsigned long flags;
  311. local_irq_save(flags);
  312. t->next = __get_cpu_var(tasklet_hi_vec).list;
  313. __get_cpu_var(tasklet_hi_vec).list = t;
  314. raise_softirq_irqoff(HI_SOFTIRQ);
  315. local_irq_restore(flags);
  316. }
  317. EXPORT_SYMBOL(__tasklet_hi_schedule);
  318. static void tasklet_action(struct softirq_action *a)
  319. {
  320. struct tasklet_struct *list;
  321. local_irq_disable();
  322. list = __get_cpu_var(tasklet_vec).list;
  323. __get_cpu_var(tasklet_vec).list = NULL;
  324. local_irq_enable();
  325. while (list) {
  326. struct tasklet_struct *t = list;
  327. list = list->next;
  328. if (tasklet_trylock(t)) {
  329. if (!atomic_read(&t->count)) {
  330. if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  331. BUG();
  332. t->func(t->data);
  333. tasklet_unlock(t);
  334. continue;
  335. }
  336. tasklet_unlock(t);
  337. }
  338. local_irq_disable();
  339. t->next = __get_cpu_var(tasklet_vec).list;
  340. __get_cpu_var(tasklet_vec).list = t;
  341. __raise_softirq_irqoff(TASKLET_SOFTIRQ);
  342. local_irq_enable();
  343. }
  344. }
  345. static void tasklet_hi_action(struct softirq_action *a)
  346. {
  347. struct tasklet_struct *list;
  348. local_irq_disable();
  349. list = __get_cpu_var(tasklet_hi_vec).list;
  350. __get_cpu_var(tasklet_hi_vec).list = NULL;
  351. local_irq_enable();
  352. while (list) {
  353. struct tasklet_struct *t = list;
  354. list = list->next;
  355. if (tasklet_trylock(t)) {
  356. if (!atomic_read(&t->count)) {
  357. if (!test_and_clear_bit(TASKLET_STATE_SCHED, &t->state))
  358. BUG();
  359. t->func(t->data);
  360. tasklet_unlock(t);
  361. continue;
  362. }
  363. tasklet_unlock(t);
  364. }
  365. local_irq_disable();
  366. t->next = __get_cpu_var(tasklet_hi_vec).list;
  367. __get_cpu_var(tasklet_hi_vec).list = t;
  368. __raise_softirq_irqoff(HI_SOFTIRQ);
  369. local_irq_enable();
  370. }
  371. }
  372. void tasklet_init(struct tasklet_struct *t,
  373. void (*func)(unsigned long), unsigned long data)
  374. {
  375. t->next = NULL;
  376. t->state = 0;
  377. atomic_set(&t->count, 0);
  378. t->func = func;
  379. t->data = data;
  380. }
  381. EXPORT_SYMBOL(tasklet_init);
  382. void tasklet_kill(struct tasklet_struct *t)
  383. {
  384. if (in_interrupt())
  385. printk("Attempt to kill tasklet from interrupt\n");
  386. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state)) {
  387. do
  388. yield();
  389. while (test_bit(TASKLET_STATE_SCHED, &t->state));
  390. }
  391. tasklet_unlock_wait(t);
  392. clear_bit(TASKLET_STATE_SCHED, &t->state);
  393. }
  394. EXPORT_SYMBOL(tasklet_kill);
  395. void __init softirq_init(void)
  396. {
  397. open_softirq(TASKLET_SOFTIRQ, tasklet_action, NULL);
  398. open_softirq(HI_SOFTIRQ, tasklet_hi_action, NULL);
  399. }
  400. static int ksoftirqd(void * __bind_cpu)
  401. {
  402. current->flags |= PF_NOFREEZE;
  403. set_current_state(TASK_INTERRUPTIBLE);
  404. while (!kthread_should_stop()) {
  405. preempt_disable();
  406. if (!local_softirq_pending()) {
  407. preempt_enable_no_resched();
  408. schedule();
  409. preempt_disable();
  410. }
  411. __set_current_state(TASK_RUNNING);
  412. while (local_softirq_pending()) {
  413. /* Preempt disable stops cpu going offline.
  414. If already offline, we'll be on wrong CPU:
  415. don't process */
  416. if (cpu_is_offline((long)__bind_cpu))
  417. goto wait_to_die;
  418. do_softirq();
  419. preempt_enable_no_resched();
  420. cond_resched();
  421. preempt_disable();
  422. }
  423. preempt_enable();
  424. set_current_state(TASK_INTERRUPTIBLE);
  425. }
  426. __set_current_state(TASK_RUNNING);
  427. return 0;
  428. wait_to_die:
  429. preempt_enable();
  430. /* Wait for kthread_stop */
  431. set_current_state(TASK_INTERRUPTIBLE);
  432. while (!kthread_should_stop()) {
  433. schedule();
  434. set_current_state(TASK_INTERRUPTIBLE);
  435. }
  436. __set_current_state(TASK_RUNNING);
  437. return 0;
  438. }
  439. #ifdef CONFIG_HOTPLUG_CPU
  440. /*
  441. * tasklet_kill_immediate is called to remove a tasklet which can already be
  442. * scheduled for execution on @cpu.
  443. *
  444. * Unlike tasklet_kill, this function removes the tasklet
  445. * _immediately_, even if the tasklet is in TASKLET_STATE_SCHED state.
  446. *
  447. * When this function is called, @cpu must be in the CPU_DEAD state.
  448. */
  449. void tasklet_kill_immediate(struct tasklet_struct *t, unsigned int cpu)
  450. {
  451. struct tasklet_struct **i;
  452. BUG_ON(cpu_online(cpu));
  453. BUG_ON(test_bit(TASKLET_STATE_RUN, &t->state));
  454. if (!test_bit(TASKLET_STATE_SCHED, &t->state))
  455. return;
  456. /* CPU is dead, so no lock needed. */
  457. for (i = &per_cpu(tasklet_vec, cpu).list; *i; i = &(*i)->next) {
  458. if (*i == t) {
  459. *i = t->next;
  460. return;
  461. }
  462. }
  463. BUG();
  464. }
  465. static void takeover_tasklets(unsigned int cpu)
  466. {
  467. struct tasklet_struct **i;
  468. /* CPU is dead, so no lock needed. */
  469. local_irq_disable();
  470. /* Find end, append list for that CPU. */
  471. for (i = &__get_cpu_var(tasklet_vec).list; *i; i = &(*i)->next);
  472. *i = per_cpu(tasklet_vec, cpu).list;
  473. per_cpu(tasklet_vec, cpu).list = NULL;
  474. raise_softirq_irqoff(TASKLET_SOFTIRQ);
  475. for (i = &__get_cpu_var(tasklet_hi_vec).list; *i; i = &(*i)->next);
  476. *i = per_cpu(tasklet_hi_vec, cpu).list;
  477. per_cpu(tasklet_hi_vec, cpu).list = NULL;
  478. raise_softirq_irqoff(HI_SOFTIRQ);
  479. local_irq_enable();
  480. }
  481. #endif /* CONFIG_HOTPLUG_CPU */
  482. static int __cpuinit cpu_callback(struct notifier_block *nfb,
  483. unsigned long action,
  484. void *hcpu)
  485. {
  486. int hotcpu = (unsigned long)hcpu;
  487. struct task_struct *p;
  488. switch (action) {
  489. case CPU_UP_PREPARE:
  490. p = kthread_create(ksoftirqd, hcpu, "ksoftirqd/%d", hotcpu);
  491. if (IS_ERR(p)) {
  492. printk("ksoftirqd for %i failed\n", hotcpu);
  493. return NOTIFY_BAD;
  494. }
  495. kthread_bind(p, hotcpu);
  496. per_cpu(ksoftirqd, hotcpu) = p;
  497. break;
  498. case CPU_ONLINE:
  499. wake_up_process(per_cpu(ksoftirqd, hotcpu));
  500. break;
  501. #ifdef CONFIG_HOTPLUG_CPU
  502. case CPU_UP_CANCELED:
  503. if (!per_cpu(ksoftirqd, hotcpu))
  504. break;
  505. /* Unbind so it can run. Fall thru. */
  506. kthread_bind(per_cpu(ksoftirqd, hotcpu),
  507. any_online_cpu(cpu_online_map));
  508. case CPU_DEAD:
  509. p = per_cpu(ksoftirqd, hotcpu);
  510. per_cpu(ksoftirqd, hotcpu) = NULL;
  511. kthread_stop(p);
  512. takeover_tasklets(hotcpu);
  513. break;
  514. #endif /* CONFIG_HOTPLUG_CPU */
  515. }
  516. return NOTIFY_OK;
  517. }
  518. static struct notifier_block __cpuinitdata cpu_nfb = {
  519. .notifier_call = cpu_callback
  520. };
  521. __init int spawn_ksoftirqd(void)
  522. {
  523. void *cpu = (void *)(long)smp_processor_id();
  524. int err = cpu_callback(&cpu_nfb, CPU_UP_PREPARE, cpu);
  525. BUG_ON(err == NOTIFY_BAD);
  526. cpu_callback(&cpu_nfb, CPU_ONLINE, cpu);
  527. register_cpu_notifier(&cpu_nfb);
  528. return 0;
  529. }
  530. #ifdef CONFIG_SMP
  531. /*
  532. * Call a function on all processors
  533. */
  534. int on_each_cpu(void (*func) (void *info), void *info, int retry, int wait)
  535. {
  536. int ret = 0;
  537. preempt_disable();
  538. ret = smp_call_function(func, info, retry, wait);
  539. local_irq_disable();
  540. func(info);
  541. local_irq_enable();
  542. preempt_enable();
  543. return ret;
  544. }
  545. EXPORT_SYMBOL(on_each_cpu);
  546. #endif