softirq.c 26 KB

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