softirq.c 24 KB

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