softirq.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/kernel/softirq.c
  4. *
  5. * Copyright (C) 1992 Linus Torvalds
  6. *
  7. * Rewritten. Old one was good in 2.2, but in 2.3 it was immoral. --ANK (990903)
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/export.h>
  11. #include <linux/kernel_stat.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/init.h>
  14. #include <linux/local_lock.h>
  15. #include <linux/mm.h>
  16. #include <linux/notifier.h>
  17. #include <linux/percpu.h>
  18. #include <linux/cpu.h>
  19. #include <linux/freezer.h>
  20. #include <linux/kthread.h>
  21. #include <linux/rcupdate.h>
  22. #include <linux/ftrace.h>
  23. #include <linux/smp.h>
  24. #include <linux/smpboot.h>
  25. #include <linux/tick.h>
  26. #include <linux/irq.h>
  27. #include <linux/wait_bit.h>
  28. #include <linux/workqueue.h>
  29. #include <asm/softirq_stack.h>
  30. #define CREATE_TRACE_POINTS
  31. #include <trace/events/irq.h>
  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. DEFINE_PER_CPU_ALIGNED(irq_cpustat_t, irq_stat);
  50. EXPORT_PER_CPU_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. const char * const softirq_to_name[NR_SOFTIRQS] = {
  55. "HI", "TIMER", "NET_TX", "NET_RX", "BLOCK", "IRQ_POLL",
  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)
  69. wake_up_process(tsk);
  70. }
  71. #ifdef CONFIG_TRACE_IRQFLAGS
  72. DEFINE_PER_CPU(int, hardirqs_enabled);
  73. DEFINE_PER_CPU(int, hardirq_context);
  74. EXPORT_PER_CPU_SYMBOL_GPL(hardirqs_enabled);
  75. EXPORT_PER_CPU_SYMBOL_GPL(hardirq_context);
  76. #endif
  77. /*
  78. * SOFTIRQ_OFFSET usage:
  79. *
  80. * On !RT kernels 'count' is the preempt counter, on RT kernels this applies
  81. * to a per CPU counter and to task::softirqs_disabled_cnt.
  82. *
  83. * - count is changed by SOFTIRQ_OFFSET on entering or leaving softirq
  84. * processing.
  85. *
  86. * - count is changed by SOFTIRQ_DISABLE_OFFSET (= 2 * SOFTIRQ_OFFSET)
  87. * on local_bh_disable or local_bh_enable.
  88. *
  89. * This lets us distinguish between whether we are currently processing
  90. * softirq and whether we just have bh disabled.
  91. */
  92. #ifdef CONFIG_PREEMPT_RT
  93. /*
  94. * RT accounts for BH disabled sections in task::softirqs_disabled_cnt and
  95. * also in per CPU softirq_ctrl::cnt. This is necessary to allow tasks in a
  96. * softirq disabled section to be preempted.
  97. *
  98. * The per task counter is used for softirq_count(), in_softirq() and
  99. * in_serving_softirqs() because these counts are only valid when the task
  100. * holding softirq_ctrl::lock is running.
  101. *
  102. * The per CPU counter prevents pointless wakeups of ksoftirqd in case that
  103. * the task which is in a softirq disabled section is preempted or blocks.
  104. */
  105. struct softirq_ctrl {
  106. local_lock_t lock;
  107. int cnt;
  108. };
  109. static DEFINE_PER_CPU(struct softirq_ctrl, softirq_ctrl) = {
  110. .lock = INIT_LOCAL_LOCK(softirq_ctrl.lock),
  111. };
  112. /**
  113. * local_bh_blocked() - Check for idle whether BH processing is blocked
  114. *
  115. * Returns false if the per CPU softirq::cnt is 0 otherwise true.
  116. *
  117. * This is invoked from the idle task to guard against false positive
  118. * softirq pending warnings, which would happen when the task which holds
  119. * softirq_ctrl::lock was the only running task on the CPU and blocks on
  120. * some other lock.
  121. */
  122. bool local_bh_blocked(void)
  123. {
  124. return __this_cpu_read(softirq_ctrl.cnt) != 0;
  125. }
  126. void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  127. {
  128. unsigned long flags;
  129. int newcnt;
  130. WARN_ON_ONCE(in_hardirq());
  131. /* First entry of a task into a BH disabled section? */
  132. if (!current->softirq_disable_cnt) {
  133. if (preemptible()) {
  134. local_lock(&softirq_ctrl.lock);
  135. /* Required to meet the RCU bottomhalf requirements. */
  136. rcu_read_lock();
  137. } else {
  138. DEBUG_LOCKS_WARN_ON(this_cpu_read(softirq_ctrl.cnt));
  139. }
  140. }
  141. /*
  142. * Track the per CPU softirq disabled state. On RT this is per CPU
  143. * state to allow preemption of bottom half disabled sections.
  144. */
  145. newcnt = __this_cpu_add_return(softirq_ctrl.cnt, cnt);
  146. /*
  147. * Reflect the result in the task state to prevent recursion on the
  148. * local lock and to make softirq_count() & al work.
  149. */
  150. current->softirq_disable_cnt = newcnt;
  151. if (IS_ENABLED(CONFIG_TRACE_IRQFLAGS) && newcnt == cnt) {
  152. raw_local_irq_save(flags);
  153. lockdep_softirqs_off(ip);
  154. raw_local_irq_restore(flags);
  155. }
  156. }
  157. EXPORT_SYMBOL(__local_bh_disable_ip);
  158. static void __local_bh_enable(unsigned int cnt, bool unlock)
  159. {
  160. unsigned long flags;
  161. int newcnt;
  162. DEBUG_LOCKS_WARN_ON(current->softirq_disable_cnt !=
  163. this_cpu_read(softirq_ctrl.cnt));
  164. if (IS_ENABLED(CONFIG_TRACE_IRQFLAGS) && softirq_count() == cnt) {
  165. raw_local_irq_save(flags);
  166. lockdep_softirqs_on(_RET_IP_);
  167. raw_local_irq_restore(flags);
  168. }
  169. newcnt = __this_cpu_sub_return(softirq_ctrl.cnt, cnt);
  170. current->softirq_disable_cnt = newcnt;
  171. if (!newcnt && unlock) {
  172. rcu_read_unlock();
  173. local_unlock(&softirq_ctrl.lock);
  174. }
  175. }
  176. void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  177. {
  178. bool preempt_on = preemptible();
  179. unsigned long flags;
  180. u32 pending;
  181. int curcnt;
  182. WARN_ON_ONCE(in_hardirq());
  183. lockdep_assert_irqs_enabled();
  184. local_irq_save(flags);
  185. curcnt = __this_cpu_read(softirq_ctrl.cnt);
  186. /*
  187. * If this is not reenabling soft interrupts, no point in trying to
  188. * run pending ones.
  189. */
  190. if (curcnt != cnt)
  191. goto out;
  192. pending = local_softirq_pending();
  193. if (!pending)
  194. goto out;
  195. /*
  196. * If this was called from non preemptible context, wake up the
  197. * softirq daemon.
  198. */
  199. if (!preempt_on) {
  200. wakeup_softirqd();
  201. goto out;
  202. }
  203. /*
  204. * Adjust softirq count to SOFTIRQ_OFFSET which makes
  205. * in_serving_softirq() become true.
  206. */
  207. cnt = SOFTIRQ_OFFSET;
  208. __local_bh_enable(cnt, false);
  209. __do_softirq();
  210. out:
  211. __local_bh_enable(cnt, preempt_on);
  212. local_irq_restore(flags);
  213. }
  214. EXPORT_SYMBOL(__local_bh_enable_ip);
  215. /*
  216. * Invoked from ksoftirqd_run() outside of the interrupt disabled section
  217. * to acquire the per CPU local lock for reentrancy protection.
  218. */
  219. static inline void ksoftirqd_run_begin(void)
  220. {
  221. __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
  222. local_irq_disable();
  223. }
  224. /* Counterpart to ksoftirqd_run_begin() */
  225. static inline void ksoftirqd_run_end(void)
  226. {
  227. __local_bh_enable(SOFTIRQ_OFFSET, true);
  228. WARN_ON_ONCE(in_interrupt());
  229. local_irq_enable();
  230. }
  231. static inline void softirq_handle_begin(void) { }
  232. static inline void softirq_handle_end(void) { }
  233. static inline bool should_wake_ksoftirqd(void)
  234. {
  235. return !this_cpu_read(softirq_ctrl.cnt);
  236. }
  237. static inline void invoke_softirq(void)
  238. {
  239. if (should_wake_ksoftirqd())
  240. wakeup_softirqd();
  241. }
  242. #define SCHED_SOFTIRQ_MASK BIT(SCHED_SOFTIRQ)
  243. /*
  244. * flush_smp_call_function_queue() can raise a soft interrupt in a function
  245. * call. On RT kernels this is undesired and the only known functionalities
  246. * are in the block layer which is disabled on RT, and in the scheduler for
  247. * idle load balancing. If soft interrupts get raised which haven't been
  248. * raised before the flush, warn if it is not a SCHED_SOFTIRQ so it can be
  249. * investigated.
  250. */
  251. void do_softirq_post_smp_call_flush(unsigned int was_pending)
  252. {
  253. unsigned int is_pending = local_softirq_pending();
  254. if (unlikely(was_pending != is_pending)) {
  255. WARN_ON_ONCE(was_pending != (is_pending & ~SCHED_SOFTIRQ_MASK));
  256. invoke_softirq();
  257. }
  258. }
  259. #else /* CONFIG_PREEMPT_RT */
  260. /*
  261. * This one is for softirq.c-internal use, where hardirqs are disabled
  262. * legitimately:
  263. */
  264. #ifdef CONFIG_TRACE_IRQFLAGS
  265. void __local_bh_disable_ip(unsigned long ip, unsigned int cnt)
  266. {
  267. unsigned long flags;
  268. WARN_ON_ONCE(in_hardirq());
  269. raw_local_irq_save(flags);
  270. /*
  271. * The preempt tracer hooks into preempt_count_add and will break
  272. * lockdep because it calls back into lockdep after SOFTIRQ_OFFSET
  273. * is set and before current->softirq_enabled is cleared.
  274. * We must manually increment preempt_count here and manually
  275. * call the trace_preempt_off later.
  276. */
  277. __preempt_count_add(cnt);
  278. /*
  279. * Were softirqs turned off above:
  280. */
  281. if (softirq_count() == (cnt & SOFTIRQ_MASK))
  282. lockdep_softirqs_off(ip);
  283. raw_local_irq_restore(flags);
  284. if (preempt_count() == cnt) {
  285. #ifdef CONFIG_DEBUG_PREEMPT
  286. current->preempt_disable_ip = get_lock_parent_ip();
  287. #endif
  288. trace_preempt_off(CALLER_ADDR0, get_lock_parent_ip());
  289. }
  290. }
  291. EXPORT_SYMBOL(__local_bh_disable_ip);
  292. #endif /* CONFIG_TRACE_IRQFLAGS */
  293. static void __local_bh_enable(unsigned int cnt)
  294. {
  295. lockdep_assert_irqs_disabled();
  296. if (preempt_count() == cnt)
  297. trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
  298. if (softirq_count() == (cnt & SOFTIRQ_MASK))
  299. lockdep_softirqs_on(_RET_IP_);
  300. __preempt_count_sub(cnt);
  301. }
  302. /*
  303. * Special-case - softirqs can safely be enabled by __do_softirq(),
  304. * without processing still-pending softirqs:
  305. */
  306. void _local_bh_enable(void)
  307. {
  308. WARN_ON_ONCE(in_hardirq());
  309. __local_bh_enable(SOFTIRQ_DISABLE_OFFSET);
  310. }
  311. EXPORT_SYMBOL(_local_bh_enable);
  312. void __local_bh_enable_ip(unsigned long ip, unsigned int cnt)
  313. {
  314. WARN_ON_ONCE(in_hardirq());
  315. lockdep_assert_irqs_enabled();
  316. #ifdef CONFIG_TRACE_IRQFLAGS
  317. local_irq_disable();
  318. #endif
  319. /*
  320. * Are softirqs going to be turned on now:
  321. */
  322. if (softirq_count() == SOFTIRQ_DISABLE_OFFSET)
  323. lockdep_softirqs_on(ip);
  324. /*
  325. * Keep preemption disabled until we are done with
  326. * softirq processing:
  327. */
  328. __preempt_count_sub(cnt - 1);
  329. if (unlikely(!in_interrupt() && local_softirq_pending())) {
  330. /*
  331. * Run softirq if any pending. And do it in its own stack
  332. * as we may be calling this deep in a task call stack already.
  333. */
  334. do_softirq();
  335. }
  336. preempt_count_dec();
  337. #ifdef CONFIG_TRACE_IRQFLAGS
  338. local_irq_enable();
  339. #endif
  340. preempt_check_resched();
  341. }
  342. EXPORT_SYMBOL(__local_bh_enable_ip);
  343. static inline void softirq_handle_begin(void)
  344. {
  345. __local_bh_disable_ip(_RET_IP_, SOFTIRQ_OFFSET);
  346. }
  347. static inline void softirq_handle_end(void)
  348. {
  349. __local_bh_enable(SOFTIRQ_OFFSET);
  350. WARN_ON_ONCE(in_interrupt());
  351. }
  352. static inline void ksoftirqd_run_begin(void)
  353. {
  354. local_irq_disable();
  355. }
  356. static inline void ksoftirqd_run_end(void)
  357. {
  358. local_irq_enable();
  359. }
  360. static inline bool should_wake_ksoftirqd(void)
  361. {
  362. return true;
  363. }
  364. static inline void invoke_softirq(void)
  365. {
  366. if (!force_irqthreads() || !__this_cpu_read(ksoftirqd)) {
  367. #ifdef CONFIG_HAVE_IRQ_EXIT_ON_IRQ_STACK
  368. /*
  369. * We can safely execute softirq on the current stack if
  370. * it is the irq stack, because it should be near empty
  371. * at this stage.
  372. */
  373. __do_softirq();
  374. #else
  375. /*
  376. * Otherwise, irq_exit() is called on the task stack that can
  377. * be potentially deep already. So call softirq in its own stack
  378. * to prevent from any overrun.
  379. */
  380. do_softirq_own_stack();
  381. #endif
  382. } else {
  383. wakeup_softirqd();
  384. }
  385. }
  386. asmlinkage __visible void do_softirq(void)
  387. {
  388. __u32 pending;
  389. unsigned long flags;
  390. if (in_interrupt())
  391. return;
  392. local_irq_save(flags);
  393. pending = local_softirq_pending();
  394. if (pending)
  395. do_softirq_own_stack();
  396. local_irq_restore(flags);
  397. }
  398. #endif /* !CONFIG_PREEMPT_RT */
  399. /*
  400. * We restart softirq processing for at most MAX_SOFTIRQ_RESTART times,
  401. * but break the loop if need_resched() is set or after 2 ms.
  402. * The MAX_SOFTIRQ_TIME provides a nice upper bound in most cases, but in
  403. * certain cases, such as stop_machine(), jiffies may cease to
  404. * increment and so we need the MAX_SOFTIRQ_RESTART limit as
  405. * well to make sure we eventually return from this method.
  406. *
  407. * These limits have been established via experimentation.
  408. * The two things to balance is latency against fairness -
  409. * we want to handle softirqs as soon as possible, but they
  410. * should not be able to lock up the box.
  411. */
  412. #define MAX_SOFTIRQ_TIME msecs_to_jiffies(2)
  413. #define MAX_SOFTIRQ_RESTART 10
  414. #ifdef CONFIG_TRACE_IRQFLAGS
  415. /*
  416. * When we run softirqs from irq_exit() and thus on the hardirq stack we need
  417. * to keep the lockdep irq context tracking as tight as possible in order to
  418. * not miss-qualify lock contexts and miss possible deadlocks.
  419. */
  420. static inline bool lockdep_softirq_start(void)
  421. {
  422. bool in_hardirq = false;
  423. if (lockdep_hardirq_context()) {
  424. in_hardirq = true;
  425. lockdep_hardirq_exit();
  426. }
  427. lockdep_softirq_enter();
  428. return in_hardirq;
  429. }
  430. static inline void lockdep_softirq_end(bool in_hardirq)
  431. {
  432. lockdep_softirq_exit();
  433. if (in_hardirq)
  434. lockdep_hardirq_enter();
  435. }
  436. #else
  437. static inline bool lockdep_softirq_start(void) { return false; }
  438. static inline void lockdep_softirq_end(bool in_hardirq) { }
  439. #endif
  440. static void handle_softirqs(bool ksirqd)
  441. {
  442. unsigned long end = jiffies + MAX_SOFTIRQ_TIME;
  443. unsigned long old_flags = current->flags;
  444. int max_restart = MAX_SOFTIRQ_RESTART;
  445. struct softirq_action *h;
  446. bool in_hardirq;
  447. __u32 pending;
  448. int softirq_bit;
  449. /*
  450. * Mask out PF_MEMALLOC as the current task context is borrowed for the
  451. * softirq. A softirq handled, such as network RX, might set PF_MEMALLOC
  452. * again if the socket is related to swapping.
  453. */
  454. current->flags &= ~PF_MEMALLOC;
  455. pending = local_softirq_pending();
  456. softirq_handle_begin();
  457. in_hardirq = lockdep_softirq_start();
  458. account_softirq_enter(current);
  459. restart:
  460. /* Reset the pending bitmask before enabling irqs */
  461. set_softirq_pending(0);
  462. local_irq_enable();
  463. h = softirq_vec;
  464. while ((softirq_bit = ffs(pending))) {
  465. unsigned int vec_nr;
  466. int prev_count;
  467. h += softirq_bit - 1;
  468. vec_nr = h - softirq_vec;
  469. prev_count = preempt_count();
  470. kstat_incr_softirqs_this_cpu(vec_nr);
  471. trace_softirq_entry(vec_nr);
  472. h->action();
  473. trace_softirq_exit(vec_nr);
  474. if (unlikely(prev_count != preempt_count())) {
  475. pr_err("huh, entered softirq %u %s %p with preempt_count %08x, exited with %08x?\n",
  476. vec_nr, softirq_to_name[vec_nr], h->action,
  477. prev_count, preempt_count());
  478. preempt_count_set(prev_count);
  479. }
  480. h++;
  481. pending >>= softirq_bit;
  482. }
  483. if (!IS_ENABLED(CONFIG_PREEMPT_RT) && ksirqd)
  484. rcu_softirq_qs();
  485. local_irq_disable();
  486. pending = local_softirq_pending();
  487. if (pending) {
  488. if (time_before(jiffies, end) && !need_resched() &&
  489. --max_restart)
  490. goto restart;
  491. wakeup_softirqd();
  492. }
  493. account_softirq_exit(current);
  494. lockdep_softirq_end(in_hardirq);
  495. softirq_handle_end();
  496. current_restore_flags(old_flags, PF_MEMALLOC);
  497. }
  498. asmlinkage __visible void __softirq_entry __do_softirq(void)
  499. {
  500. handle_softirqs(false);
  501. }
  502. /**
  503. * irq_enter_rcu - Enter an interrupt context with RCU watching
  504. */
  505. void irq_enter_rcu(void)
  506. {
  507. __irq_enter_raw();
  508. if (tick_nohz_full_cpu(smp_processor_id()) ||
  509. (is_idle_task(current) && (irq_count() == HARDIRQ_OFFSET)))
  510. tick_irq_enter();
  511. account_hardirq_enter(current);
  512. }
  513. /**
  514. * irq_enter - Enter an interrupt context including RCU update
  515. */
  516. void irq_enter(void)
  517. {
  518. ct_irq_enter();
  519. irq_enter_rcu();
  520. }
  521. static inline void tick_irq_exit(void)
  522. {
  523. #ifdef CONFIG_NO_HZ_COMMON
  524. int cpu = smp_processor_id();
  525. /* Make sure that timer wheel updates are propagated */
  526. if ((sched_core_idle_cpu(cpu) && !need_resched()) || tick_nohz_full_cpu(cpu)) {
  527. if (!in_hardirq())
  528. tick_nohz_irq_exit();
  529. }
  530. #endif
  531. }
  532. static inline void __irq_exit_rcu(void)
  533. {
  534. #ifndef __ARCH_IRQ_EXIT_IRQS_DISABLED
  535. local_irq_disable();
  536. #else
  537. lockdep_assert_irqs_disabled();
  538. #endif
  539. account_hardirq_exit(current);
  540. preempt_count_sub(HARDIRQ_OFFSET);
  541. if (!in_interrupt() && local_softirq_pending())
  542. invoke_softirq();
  543. tick_irq_exit();
  544. }
  545. /**
  546. * irq_exit_rcu() - Exit an interrupt context without updating RCU
  547. *
  548. * Also processes softirqs if needed and possible.
  549. */
  550. void irq_exit_rcu(void)
  551. {
  552. __irq_exit_rcu();
  553. /* must be last! */
  554. lockdep_hardirq_exit();
  555. }
  556. /**
  557. * irq_exit - Exit an interrupt context, update RCU and lockdep
  558. *
  559. * Also processes softirqs if needed and possible.
  560. */
  561. void irq_exit(void)
  562. {
  563. __irq_exit_rcu();
  564. ct_irq_exit();
  565. /* must be last! */
  566. lockdep_hardirq_exit();
  567. }
  568. /*
  569. * This function must run with irqs disabled!
  570. */
  571. inline void raise_softirq_irqoff(unsigned int nr)
  572. {
  573. __raise_softirq_irqoff(nr);
  574. /*
  575. * If we're in an interrupt or softirq, we're done
  576. * (this also catches softirq-disabled code). We will
  577. * actually run the softirq once we return from
  578. * the irq or softirq.
  579. *
  580. * Otherwise we wake up ksoftirqd to make sure we
  581. * schedule the softirq soon.
  582. */
  583. if (!in_interrupt() && should_wake_ksoftirqd())
  584. wakeup_softirqd();
  585. }
  586. void raise_softirq(unsigned int nr)
  587. {
  588. unsigned long flags;
  589. local_irq_save(flags);
  590. raise_softirq_irqoff(nr);
  591. local_irq_restore(flags);
  592. }
  593. void __raise_softirq_irqoff(unsigned int nr)
  594. {
  595. lockdep_assert_irqs_disabled();
  596. trace_softirq_raise(nr);
  597. or_softirq_pending(1UL << nr);
  598. }
  599. void open_softirq(int nr, void (*action)(void))
  600. {
  601. softirq_vec[nr].action = action;
  602. }
  603. /*
  604. * Tasklets
  605. */
  606. struct tasklet_head {
  607. struct tasklet_struct *head;
  608. struct tasklet_struct **tail;
  609. };
  610. static DEFINE_PER_CPU(struct tasklet_head, tasklet_vec);
  611. static DEFINE_PER_CPU(struct tasklet_head, tasklet_hi_vec);
  612. static void __tasklet_schedule_common(struct tasklet_struct *t,
  613. struct tasklet_head __percpu *headp,
  614. unsigned int softirq_nr)
  615. {
  616. struct tasklet_head *head;
  617. unsigned long flags;
  618. local_irq_save(flags);
  619. head = this_cpu_ptr(headp);
  620. t->next = NULL;
  621. *head->tail = t;
  622. head->tail = &(t->next);
  623. raise_softirq_irqoff(softirq_nr);
  624. local_irq_restore(flags);
  625. }
  626. void __tasklet_schedule(struct tasklet_struct *t)
  627. {
  628. __tasklet_schedule_common(t, &tasklet_vec,
  629. TASKLET_SOFTIRQ);
  630. }
  631. EXPORT_SYMBOL(__tasklet_schedule);
  632. void __tasklet_hi_schedule(struct tasklet_struct *t)
  633. {
  634. __tasklet_schedule_common(t, &tasklet_hi_vec,
  635. HI_SOFTIRQ);
  636. }
  637. EXPORT_SYMBOL(__tasklet_hi_schedule);
  638. static bool tasklet_clear_sched(struct tasklet_struct *t)
  639. {
  640. if (test_and_clear_bit(TASKLET_STATE_SCHED, &t->state)) {
  641. wake_up_var(&t->state);
  642. return true;
  643. }
  644. WARN_ONCE(1, "tasklet SCHED state not set: %s %pS\n",
  645. t->use_callback ? "callback" : "func",
  646. t->use_callback ? (void *)t->callback : (void *)t->func);
  647. return false;
  648. }
  649. static void tasklet_action_common(struct tasklet_head *tl_head,
  650. unsigned int softirq_nr)
  651. {
  652. struct tasklet_struct *list;
  653. local_irq_disable();
  654. list = tl_head->head;
  655. tl_head->head = NULL;
  656. tl_head->tail = &tl_head->head;
  657. local_irq_enable();
  658. while (list) {
  659. struct tasklet_struct *t = list;
  660. list = list->next;
  661. if (tasklet_trylock(t)) {
  662. if (!atomic_read(&t->count)) {
  663. if (tasklet_clear_sched(t)) {
  664. if (t->use_callback) {
  665. trace_tasklet_entry(t, t->callback);
  666. t->callback(t);
  667. trace_tasklet_exit(t, t->callback);
  668. } else {
  669. trace_tasklet_entry(t, t->func);
  670. t->func(t->data);
  671. trace_tasklet_exit(t, t->func);
  672. }
  673. }
  674. tasklet_unlock(t);
  675. continue;
  676. }
  677. tasklet_unlock(t);
  678. }
  679. local_irq_disable();
  680. t->next = NULL;
  681. *tl_head->tail = t;
  682. tl_head->tail = &t->next;
  683. __raise_softirq_irqoff(softirq_nr);
  684. local_irq_enable();
  685. }
  686. }
  687. static __latent_entropy void tasklet_action(void)
  688. {
  689. workqueue_softirq_action(false);
  690. tasklet_action_common(this_cpu_ptr(&tasklet_vec), TASKLET_SOFTIRQ);
  691. }
  692. static __latent_entropy void tasklet_hi_action(void)
  693. {
  694. workqueue_softirq_action(true);
  695. tasklet_action_common(this_cpu_ptr(&tasklet_hi_vec), HI_SOFTIRQ);
  696. }
  697. void tasklet_setup(struct tasklet_struct *t,
  698. void (*callback)(struct tasklet_struct *))
  699. {
  700. t->next = NULL;
  701. t->state = 0;
  702. atomic_set(&t->count, 0);
  703. t->callback = callback;
  704. t->use_callback = true;
  705. t->data = 0;
  706. }
  707. EXPORT_SYMBOL(tasklet_setup);
  708. void tasklet_init(struct tasklet_struct *t,
  709. void (*func)(unsigned long), unsigned long data)
  710. {
  711. t->next = NULL;
  712. t->state = 0;
  713. atomic_set(&t->count, 0);
  714. t->func = func;
  715. t->use_callback = false;
  716. t->data = data;
  717. }
  718. EXPORT_SYMBOL(tasklet_init);
  719. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
  720. /*
  721. * Do not use in new code. Waiting for tasklets from atomic contexts is
  722. * error prone and should be avoided.
  723. */
  724. void tasklet_unlock_spin_wait(struct tasklet_struct *t)
  725. {
  726. while (test_bit(TASKLET_STATE_RUN, &(t)->state)) {
  727. if (IS_ENABLED(CONFIG_PREEMPT_RT)) {
  728. /*
  729. * Prevent a live lock when current preempted soft
  730. * interrupt processing or prevents ksoftirqd from
  731. * running. If the tasklet runs on a different CPU
  732. * then this has no effect other than doing the BH
  733. * disable/enable dance for nothing.
  734. */
  735. local_bh_disable();
  736. local_bh_enable();
  737. } else {
  738. cpu_relax();
  739. }
  740. }
  741. }
  742. EXPORT_SYMBOL(tasklet_unlock_spin_wait);
  743. #endif
  744. void tasklet_kill(struct tasklet_struct *t)
  745. {
  746. if (in_interrupt())
  747. pr_notice("Attempt to kill tasklet from interrupt\n");
  748. while (test_and_set_bit(TASKLET_STATE_SCHED, &t->state))
  749. wait_var_event(&t->state, !test_bit(TASKLET_STATE_SCHED, &t->state));
  750. tasklet_unlock_wait(t);
  751. tasklet_clear_sched(t);
  752. }
  753. EXPORT_SYMBOL(tasklet_kill);
  754. #if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT)
  755. void tasklet_unlock(struct tasklet_struct *t)
  756. {
  757. smp_mb__before_atomic();
  758. clear_bit(TASKLET_STATE_RUN, &t->state);
  759. smp_mb__after_atomic();
  760. wake_up_var(&t->state);
  761. }
  762. EXPORT_SYMBOL_GPL(tasklet_unlock);
  763. void tasklet_unlock_wait(struct tasklet_struct *t)
  764. {
  765. wait_var_event(&t->state, !test_bit(TASKLET_STATE_RUN, &t->state));
  766. }
  767. EXPORT_SYMBOL_GPL(tasklet_unlock_wait);
  768. #endif
  769. void __init softirq_init(void)
  770. {
  771. int cpu;
  772. for_each_possible_cpu(cpu) {
  773. per_cpu(tasklet_vec, cpu).tail =
  774. &per_cpu(tasklet_vec, cpu).head;
  775. per_cpu(tasklet_hi_vec, cpu).tail =
  776. &per_cpu(tasklet_hi_vec, cpu).head;
  777. }
  778. open_softirq(TASKLET_SOFTIRQ, tasklet_action);
  779. open_softirq(HI_SOFTIRQ, tasklet_hi_action);
  780. }
  781. static int ksoftirqd_should_run(unsigned int cpu)
  782. {
  783. return local_softirq_pending();
  784. }
  785. static void run_ksoftirqd(unsigned int cpu)
  786. {
  787. ksoftirqd_run_begin();
  788. if (local_softirq_pending()) {
  789. /*
  790. * We can safely run softirq on inline stack, as we are not deep
  791. * in the task stack here.
  792. */
  793. handle_softirqs(true);
  794. ksoftirqd_run_end();
  795. cond_resched();
  796. return;
  797. }
  798. ksoftirqd_run_end();
  799. }
  800. #ifdef CONFIG_HOTPLUG_CPU
  801. static int takeover_tasklets(unsigned int cpu)
  802. {
  803. workqueue_softirq_dead(cpu);
  804. /* CPU is dead, so no lock needed. */
  805. local_irq_disable();
  806. /* Find end, append list for that CPU. */
  807. if (&per_cpu(tasklet_vec, cpu).head != per_cpu(tasklet_vec, cpu).tail) {
  808. *__this_cpu_read(tasklet_vec.tail) = per_cpu(tasklet_vec, cpu).head;
  809. __this_cpu_write(tasklet_vec.tail, per_cpu(tasklet_vec, cpu).tail);
  810. per_cpu(tasklet_vec, cpu).head = NULL;
  811. per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
  812. }
  813. raise_softirq_irqoff(TASKLET_SOFTIRQ);
  814. if (&per_cpu(tasklet_hi_vec, cpu).head != per_cpu(tasklet_hi_vec, cpu).tail) {
  815. *__this_cpu_read(tasklet_hi_vec.tail) = per_cpu(tasklet_hi_vec, cpu).head;
  816. __this_cpu_write(tasklet_hi_vec.tail, per_cpu(tasklet_hi_vec, cpu).tail);
  817. per_cpu(tasklet_hi_vec, cpu).head = NULL;
  818. per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
  819. }
  820. raise_softirq_irqoff(HI_SOFTIRQ);
  821. local_irq_enable();
  822. return 0;
  823. }
  824. #else
  825. #define takeover_tasklets NULL
  826. #endif /* CONFIG_HOTPLUG_CPU */
  827. static struct smp_hotplug_thread softirq_threads = {
  828. .store = &ksoftirqd,
  829. .thread_should_run = ksoftirqd_should_run,
  830. .thread_fn = run_ksoftirqd,
  831. .thread_comm = "ksoftirqd/%u",
  832. };
  833. static __init int spawn_ksoftirqd(void)
  834. {
  835. cpuhp_setup_state_nocalls(CPUHP_SOFTIRQ_DEAD, "softirq:dead", NULL,
  836. takeover_tasklets);
  837. BUG_ON(smpboot_register_percpu_thread(&softirq_threads));
  838. return 0;
  839. }
  840. early_initcall(spawn_ksoftirqd);
  841. /*
  842. * [ These __weak aliases are kept in a separate compilation unit, so that
  843. * GCC does not inline them incorrectly. ]
  844. */
  845. int __init __weak early_irq_init(void)
  846. {
  847. return 0;
  848. }
  849. int __init __weak arch_probe_nr_irqs(void)
  850. {
  851. return NR_IRQS_LEGACY;
  852. }
  853. int __init __weak arch_early_irq_init(void)
  854. {
  855. return 0;
  856. }
  857. unsigned int __weak arch_dynirq_lower_bound(unsigned int from)
  858. {
  859. return from;
  860. }