rcutiny_plugin.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion, the Bloatwatch edition
  3. * Internal non-public definitions that provide either classic
  4. * or preemptible semantics.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. * Copyright (c) 2010 Linaro
  21. *
  22. * Author: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  23. */
  24. #include <linux/kthread.h>
  25. #include <linux/module.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/seq_file.h>
  28. /* Global control variables for rcupdate callback mechanism. */
  29. struct rcu_ctrlblk {
  30. struct rcu_head *rcucblist; /* List of pending callbacks (CBs). */
  31. struct rcu_head **donetail; /* ->next pointer of last "done" CB. */
  32. struct rcu_head **curtail; /* ->next pointer of last CB. */
  33. RCU_TRACE(long qlen); /* Number of pending CBs. */
  34. RCU_TRACE(char *name); /* Name of RCU type. */
  35. };
  36. /* Definition for rcupdate control block. */
  37. static struct rcu_ctrlblk rcu_sched_ctrlblk = {
  38. .donetail = &rcu_sched_ctrlblk.rcucblist,
  39. .curtail = &rcu_sched_ctrlblk.rcucblist,
  40. RCU_TRACE(.name = "rcu_sched")
  41. };
  42. static struct rcu_ctrlblk rcu_bh_ctrlblk = {
  43. .donetail = &rcu_bh_ctrlblk.rcucblist,
  44. .curtail = &rcu_bh_ctrlblk.rcucblist,
  45. RCU_TRACE(.name = "rcu_bh")
  46. };
  47. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  48. int rcu_scheduler_active __read_mostly;
  49. EXPORT_SYMBOL_GPL(rcu_scheduler_active);
  50. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  51. #ifdef CONFIG_TINY_PREEMPT_RCU
  52. #include <linux/delay.h>
  53. /* Global control variables for preemptible RCU. */
  54. struct rcu_preempt_ctrlblk {
  55. struct rcu_ctrlblk rcb; /* curtail: ->next ptr of last CB for GP. */
  56. struct rcu_head **nexttail;
  57. /* Tasks blocked in a preemptible RCU */
  58. /* read-side critical section while an */
  59. /* preemptible-RCU grace period is in */
  60. /* progress must wait for a later grace */
  61. /* period. This pointer points to the */
  62. /* ->next pointer of the last task that */
  63. /* must wait for a later grace period, or */
  64. /* to &->rcb.rcucblist if there is no */
  65. /* such task. */
  66. struct list_head blkd_tasks;
  67. /* Tasks blocked in RCU read-side critical */
  68. /* section. Tasks are placed at the head */
  69. /* of this list and age towards the tail. */
  70. struct list_head *gp_tasks;
  71. /* Pointer to the first task blocking the */
  72. /* current grace period, or NULL if there */
  73. /* is no such task. */
  74. struct list_head *exp_tasks;
  75. /* Pointer to first task blocking the */
  76. /* current expedited grace period, or NULL */
  77. /* if there is no such task. If there */
  78. /* is no current expedited grace period, */
  79. /* then there cannot be any such task. */
  80. #ifdef CONFIG_RCU_BOOST
  81. struct list_head *boost_tasks;
  82. /* Pointer to first task that needs to be */
  83. /* priority-boosted, or NULL if no priority */
  84. /* boosting is needed. If there is no */
  85. /* current or expedited grace period, there */
  86. /* can be no such task. */
  87. #endif /* #ifdef CONFIG_RCU_BOOST */
  88. u8 gpnum; /* Current grace period. */
  89. u8 gpcpu; /* Last grace period blocked by the CPU. */
  90. u8 completed; /* Last grace period completed. */
  91. /* If all three are equal, RCU is idle. */
  92. #ifdef CONFIG_RCU_BOOST
  93. unsigned long boost_time; /* When to start boosting (jiffies) */
  94. #endif /* #ifdef CONFIG_RCU_BOOST */
  95. #ifdef CONFIG_RCU_TRACE
  96. unsigned long n_grace_periods;
  97. #ifdef CONFIG_RCU_BOOST
  98. unsigned long n_tasks_boosted;
  99. /* Total number of tasks boosted. */
  100. unsigned long n_exp_boosts;
  101. /* Number of tasks boosted for expedited GP. */
  102. unsigned long n_normal_boosts;
  103. /* Number of tasks boosted for normal GP. */
  104. unsigned long n_balk_blkd_tasks;
  105. /* Refused to boost: no blocked tasks. */
  106. unsigned long n_balk_exp_gp_tasks;
  107. /* Refused to boost: nothing blocking GP. */
  108. unsigned long n_balk_boost_tasks;
  109. /* Refused to boost: already boosting. */
  110. unsigned long n_balk_notyet;
  111. /* Refused to boost: not yet time. */
  112. unsigned long n_balk_nos;
  113. /* Refused to boost: not sure why, though. */
  114. /* This can happen due to race conditions. */
  115. #endif /* #ifdef CONFIG_RCU_BOOST */
  116. #endif /* #ifdef CONFIG_RCU_TRACE */
  117. };
  118. static struct rcu_preempt_ctrlblk rcu_preempt_ctrlblk = {
  119. .rcb.donetail = &rcu_preempt_ctrlblk.rcb.rcucblist,
  120. .rcb.curtail = &rcu_preempt_ctrlblk.rcb.rcucblist,
  121. .nexttail = &rcu_preempt_ctrlblk.rcb.rcucblist,
  122. .blkd_tasks = LIST_HEAD_INIT(rcu_preempt_ctrlblk.blkd_tasks),
  123. RCU_TRACE(.rcb.name = "rcu_preempt")
  124. };
  125. static void rcu_read_unlock_special(struct task_struct *t);
  126. static int rcu_preempted_readers_exp(void);
  127. static void rcu_report_exp_done(void);
  128. /*
  129. * Return true if the CPU has not yet responded to the current grace period.
  130. */
  131. static int rcu_cpu_blocking_cur_gp(void)
  132. {
  133. return rcu_preempt_ctrlblk.gpcpu != rcu_preempt_ctrlblk.gpnum;
  134. }
  135. /*
  136. * Check for a running RCU reader. Because there is only one CPU,
  137. * there can be but one running RCU reader at a time. ;-)
  138. *
  139. * Returns zero if there are no running readers. Returns a positive
  140. * number if there is at least one reader within its RCU read-side
  141. * critical section. Returns a negative number if an outermost reader
  142. * is in the midst of exiting from its RCU read-side critical section
  143. *
  144. * Returns zero if there are no running readers. Returns a positive
  145. * number if there is at least one reader within its RCU read-side
  146. * critical section. Returns a negative number if an outermost reader
  147. * is in the midst of exiting from its RCU read-side critical section.
  148. */
  149. static int rcu_preempt_running_reader(void)
  150. {
  151. return current->rcu_read_lock_nesting;
  152. }
  153. /*
  154. * Check for preempted RCU readers blocking any grace period.
  155. * If the caller needs a reliable answer, it must disable hard irqs.
  156. */
  157. static int rcu_preempt_blocked_readers_any(void)
  158. {
  159. return !list_empty(&rcu_preempt_ctrlblk.blkd_tasks);
  160. }
  161. /*
  162. * Check for preempted RCU readers blocking the current grace period.
  163. * If the caller needs a reliable answer, it must disable hard irqs.
  164. */
  165. static int rcu_preempt_blocked_readers_cgp(void)
  166. {
  167. return rcu_preempt_ctrlblk.gp_tasks != NULL;
  168. }
  169. /*
  170. * Return true if another preemptible-RCU grace period is needed.
  171. */
  172. static int rcu_preempt_needs_another_gp(void)
  173. {
  174. return *rcu_preempt_ctrlblk.rcb.curtail != NULL;
  175. }
  176. /*
  177. * Return true if a preemptible-RCU grace period is in progress.
  178. * The caller must disable hardirqs.
  179. */
  180. static int rcu_preempt_gp_in_progress(void)
  181. {
  182. return rcu_preempt_ctrlblk.completed != rcu_preempt_ctrlblk.gpnum;
  183. }
  184. /*
  185. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  186. * returning NULL if at the end of the list.
  187. */
  188. static struct list_head *rcu_next_node_entry(struct task_struct *t)
  189. {
  190. struct list_head *np;
  191. np = t->rcu_node_entry.next;
  192. if (np == &rcu_preempt_ctrlblk.blkd_tasks)
  193. np = NULL;
  194. return np;
  195. }
  196. #ifdef CONFIG_RCU_TRACE
  197. #ifdef CONFIG_RCU_BOOST
  198. static void rcu_initiate_boost_trace(void);
  199. #endif /* #ifdef CONFIG_RCU_BOOST */
  200. /*
  201. * Dump additional statistice for TINY_PREEMPT_RCU.
  202. */
  203. static void show_tiny_preempt_stats(struct seq_file *m)
  204. {
  205. seq_printf(m, "rcu_preempt: qlen=%ld gp=%lu g%u/p%u/c%u tasks=%c%c%c\n",
  206. rcu_preempt_ctrlblk.rcb.qlen,
  207. rcu_preempt_ctrlblk.n_grace_periods,
  208. rcu_preempt_ctrlblk.gpnum,
  209. rcu_preempt_ctrlblk.gpcpu,
  210. rcu_preempt_ctrlblk.completed,
  211. "T."[list_empty(&rcu_preempt_ctrlblk.blkd_tasks)],
  212. "N."[!rcu_preempt_ctrlblk.gp_tasks],
  213. "E."[!rcu_preempt_ctrlblk.exp_tasks]);
  214. #ifdef CONFIG_RCU_BOOST
  215. seq_printf(m, "%sttb=%c ntb=%lu neb=%lu nnb=%lu j=%04x bt=%04x\n",
  216. " ",
  217. "B."[!rcu_preempt_ctrlblk.boost_tasks],
  218. rcu_preempt_ctrlblk.n_tasks_boosted,
  219. rcu_preempt_ctrlblk.n_exp_boosts,
  220. rcu_preempt_ctrlblk.n_normal_boosts,
  221. (int)(jiffies & 0xffff),
  222. (int)(rcu_preempt_ctrlblk.boost_time & 0xffff));
  223. seq_printf(m, "%s: nt=%lu egt=%lu bt=%lu ny=%lu nos=%lu\n",
  224. " balk",
  225. rcu_preempt_ctrlblk.n_balk_blkd_tasks,
  226. rcu_preempt_ctrlblk.n_balk_exp_gp_tasks,
  227. rcu_preempt_ctrlblk.n_balk_boost_tasks,
  228. rcu_preempt_ctrlblk.n_balk_notyet,
  229. rcu_preempt_ctrlblk.n_balk_nos);
  230. #endif /* #ifdef CONFIG_RCU_BOOST */
  231. }
  232. #endif /* #ifdef CONFIG_RCU_TRACE */
  233. #ifdef CONFIG_RCU_BOOST
  234. #include "rtmutex_common.h"
  235. #define RCU_BOOST_PRIO CONFIG_RCU_BOOST_PRIO
  236. /* Controls for rcu_kthread() kthread. */
  237. static struct task_struct *rcu_kthread_task;
  238. static DECLARE_WAIT_QUEUE_HEAD(rcu_kthread_wq);
  239. static unsigned long have_rcu_kthread_work;
  240. /*
  241. * Carry out RCU priority boosting on the task indicated by ->boost_tasks,
  242. * and advance ->boost_tasks to the next task in the ->blkd_tasks list.
  243. */
  244. static int rcu_boost(void)
  245. {
  246. unsigned long flags;
  247. struct rt_mutex mtx;
  248. struct task_struct *t;
  249. struct list_head *tb;
  250. if (rcu_preempt_ctrlblk.boost_tasks == NULL &&
  251. rcu_preempt_ctrlblk.exp_tasks == NULL)
  252. return 0; /* Nothing to boost. */
  253. raw_local_irq_save(flags);
  254. /*
  255. * Recheck with irqs disabled: all tasks in need of boosting
  256. * might exit their RCU read-side critical sections on their own
  257. * if we are preempted just before disabling irqs.
  258. */
  259. if (rcu_preempt_ctrlblk.boost_tasks == NULL &&
  260. rcu_preempt_ctrlblk.exp_tasks == NULL) {
  261. raw_local_irq_restore(flags);
  262. return 0;
  263. }
  264. /*
  265. * Preferentially boost tasks blocking expedited grace periods.
  266. * This cannot starve the normal grace periods because a second
  267. * expedited grace period must boost all blocked tasks, including
  268. * those blocking the pre-existing normal grace period.
  269. */
  270. if (rcu_preempt_ctrlblk.exp_tasks != NULL) {
  271. tb = rcu_preempt_ctrlblk.exp_tasks;
  272. RCU_TRACE(rcu_preempt_ctrlblk.n_exp_boosts++);
  273. } else {
  274. tb = rcu_preempt_ctrlblk.boost_tasks;
  275. RCU_TRACE(rcu_preempt_ctrlblk.n_normal_boosts++);
  276. }
  277. RCU_TRACE(rcu_preempt_ctrlblk.n_tasks_boosted++);
  278. /*
  279. * We boost task t by manufacturing an rt_mutex that appears to
  280. * be held by task t. We leave a pointer to that rt_mutex where
  281. * task t can find it, and task t will release the mutex when it
  282. * exits its outermost RCU read-side critical section. Then
  283. * simply acquiring this artificial rt_mutex will boost task
  284. * t's priority. (Thanks to tglx for suggesting this approach!)
  285. */
  286. t = container_of(tb, struct task_struct, rcu_node_entry);
  287. rt_mutex_init_proxy_locked(&mtx, t);
  288. t->rcu_boost_mutex = &mtx;
  289. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BOOSTED;
  290. raw_local_irq_restore(flags);
  291. rt_mutex_lock(&mtx);
  292. rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
  293. return ACCESS_ONCE(rcu_preempt_ctrlblk.boost_tasks) != NULL ||
  294. ACCESS_ONCE(rcu_preempt_ctrlblk.exp_tasks) != NULL;
  295. }
  296. /*
  297. * Check to see if it is now time to start boosting RCU readers blocking
  298. * the current grace period, and, if so, tell the rcu_kthread_task to
  299. * start boosting them. If there is an expedited boost in progress,
  300. * we wait for it to complete.
  301. *
  302. * If there are no blocked readers blocking the current grace period,
  303. * return 0 to let the caller know, otherwise return 1. Note that this
  304. * return value is independent of whether or not boosting was done.
  305. */
  306. static int rcu_initiate_boost(void)
  307. {
  308. if (!rcu_preempt_blocked_readers_cgp() &&
  309. rcu_preempt_ctrlblk.exp_tasks == NULL) {
  310. RCU_TRACE(rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++);
  311. return 0;
  312. }
  313. if (rcu_preempt_ctrlblk.exp_tasks != NULL ||
  314. (rcu_preempt_ctrlblk.gp_tasks != NULL &&
  315. rcu_preempt_ctrlblk.boost_tasks == NULL &&
  316. ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))) {
  317. if (rcu_preempt_ctrlblk.exp_tasks == NULL)
  318. rcu_preempt_ctrlblk.boost_tasks =
  319. rcu_preempt_ctrlblk.gp_tasks;
  320. invoke_rcu_callbacks();
  321. } else
  322. RCU_TRACE(rcu_initiate_boost_trace());
  323. return 1;
  324. }
  325. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  326. /*
  327. * Do priority-boost accounting for the start of a new grace period.
  328. */
  329. static void rcu_preempt_boost_start_gp(void)
  330. {
  331. rcu_preempt_ctrlblk.boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  332. }
  333. #else /* #ifdef CONFIG_RCU_BOOST */
  334. /*
  335. * If there is no RCU priority boosting, we don't initiate boosting,
  336. * but we do indicate whether there are blocked readers blocking the
  337. * current grace period.
  338. */
  339. static int rcu_initiate_boost(void)
  340. {
  341. return rcu_preempt_blocked_readers_cgp();
  342. }
  343. /*
  344. * If there is no RCU priority boosting, nothing to do at grace-period start.
  345. */
  346. static void rcu_preempt_boost_start_gp(void)
  347. {
  348. }
  349. #endif /* else #ifdef CONFIG_RCU_BOOST */
  350. /*
  351. * Record a preemptible-RCU quiescent state for the specified CPU. Note
  352. * that this just means that the task currently running on the CPU is
  353. * in a quiescent state. There might be any number of tasks blocked
  354. * while in an RCU read-side critical section.
  355. *
  356. * Unlike the other rcu_*_qs() functions, callers to this function
  357. * must disable irqs in order to protect the assignment to
  358. * ->rcu_read_unlock_special.
  359. *
  360. * Because this is a single-CPU implementation, the only way a grace
  361. * period can end is if the CPU is in a quiescent state. The reason is
  362. * that a blocked preemptible-RCU reader can exit its critical section
  363. * only if the CPU is running it at the time. Therefore, when the
  364. * last task blocking the current grace period exits its RCU read-side
  365. * critical section, neither the CPU nor blocked tasks will be stopping
  366. * the current grace period. (In contrast, SMP implementations
  367. * might have CPUs running in RCU read-side critical sections that
  368. * block later grace periods -- but this is not possible given only
  369. * one CPU.)
  370. */
  371. static void rcu_preempt_cpu_qs(void)
  372. {
  373. /* Record both CPU and task as having responded to current GP. */
  374. rcu_preempt_ctrlblk.gpcpu = rcu_preempt_ctrlblk.gpnum;
  375. current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  376. /* If there is no GP then there is nothing more to do. */
  377. if (!rcu_preempt_gp_in_progress())
  378. return;
  379. /*
  380. * Check up on boosting. If there are readers blocking the
  381. * current grace period, leave.
  382. */
  383. if (rcu_initiate_boost())
  384. return;
  385. /* Advance callbacks. */
  386. rcu_preempt_ctrlblk.completed = rcu_preempt_ctrlblk.gpnum;
  387. rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.rcb.curtail;
  388. rcu_preempt_ctrlblk.rcb.curtail = rcu_preempt_ctrlblk.nexttail;
  389. /* If there are no blocked readers, next GP is done instantly. */
  390. if (!rcu_preempt_blocked_readers_any())
  391. rcu_preempt_ctrlblk.rcb.donetail = rcu_preempt_ctrlblk.nexttail;
  392. /* If there are done callbacks, cause them to be invoked. */
  393. if (*rcu_preempt_ctrlblk.rcb.donetail != NULL)
  394. invoke_rcu_callbacks();
  395. }
  396. /*
  397. * Start a new RCU grace period if warranted. Hard irqs must be disabled.
  398. */
  399. static void rcu_preempt_start_gp(void)
  400. {
  401. if (!rcu_preempt_gp_in_progress() && rcu_preempt_needs_another_gp()) {
  402. /* Official start of GP. */
  403. rcu_preempt_ctrlblk.gpnum++;
  404. RCU_TRACE(rcu_preempt_ctrlblk.n_grace_periods++);
  405. /* Any blocked RCU readers block new GP. */
  406. if (rcu_preempt_blocked_readers_any())
  407. rcu_preempt_ctrlblk.gp_tasks =
  408. rcu_preempt_ctrlblk.blkd_tasks.next;
  409. /* Set up for RCU priority boosting. */
  410. rcu_preempt_boost_start_gp();
  411. /* If there is no running reader, CPU is done with GP. */
  412. if (!rcu_preempt_running_reader())
  413. rcu_preempt_cpu_qs();
  414. }
  415. }
  416. /*
  417. * We have entered the scheduler, and the current task might soon be
  418. * context-switched away from. If this task is in an RCU read-side
  419. * critical section, we will no longer be able to rely on the CPU to
  420. * record that fact, so we enqueue the task on the blkd_tasks list.
  421. * If the task started after the current grace period began, as recorded
  422. * by ->gpcpu, we enqueue at the beginning of the list. Otherwise
  423. * before the element referenced by ->gp_tasks (or at the tail if
  424. * ->gp_tasks is NULL) and point ->gp_tasks at the newly added element.
  425. * The task will dequeue itself when it exits the outermost enclosing
  426. * RCU read-side critical section. Therefore, the current grace period
  427. * cannot be permitted to complete until the ->gp_tasks pointer becomes
  428. * NULL.
  429. *
  430. * Caller must disable preemption.
  431. */
  432. void rcu_preempt_note_context_switch(void)
  433. {
  434. struct task_struct *t = current;
  435. unsigned long flags;
  436. local_irq_save(flags); /* must exclude scheduler_tick(). */
  437. if (rcu_preempt_running_reader() > 0 &&
  438. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  439. /* Possibly blocking in an RCU read-side critical section. */
  440. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  441. /*
  442. * If this CPU has already checked in, then this task
  443. * will hold up the next grace period rather than the
  444. * current grace period. Queue the task accordingly.
  445. * If the task is queued for the current grace period
  446. * (i.e., this CPU has not yet passed through a quiescent
  447. * state for the current grace period), then as long
  448. * as that task remains queued, the current grace period
  449. * cannot end.
  450. */
  451. list_add(&t->rcu_node_entry, &rcu_preempt_ctrlblk.blkd_tasks);
  452. if (rcu_cpu_blocking_cur_gp())
  453. rcu_preempt_ctrlblk.gp_tasks = &t->rcu_node_entry;
  454. } else if (rcu_preempt_running_reader() < 0 &&
  455. t->rcu_read_unlock_special) {
  456. /*
  457. * Complete exit from RCU read-side critical section on
  458. * behalf of preempted instance of __rcu_read_unlock().
  459. */
  460. rcu_read_unlock_special(t);
  461. }
  462. /*
  463. * Either we were not in an RCU read-side critical section to
  464. * begin with, or we have now recorded that critical section
  465. * globally. Either way, we can now note a quiescent state
  466. * for this CPU. Again, if we were in an RCU read-side critical
  467. * section, and if that critical section was blocking the current
  468. * grace period, then the fact that the task has been enqueued
  469. * means that current grace period continues to be blocked.
  470. */
  471. rcu_preempt_cpu_qs();
  472. local_irq_restore(flags);
  473. }
  474. /*
  475. * Tiny-preemptible RCU implementation for rcu_read_lock().
  476. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  477. * if we block.
  478. */
  479. void __rcu_read_lock(void)
  480. {
  481. current->rcu_read_lock_nesting++;
  482. barrier(); /* needed if we ever invoke rcu_read_lock in rcutiny.c */
  483. }
  484. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  485. /*
  486. * Handle special cases during rcu_read_unlock(), such as needing to
  487. * notify RCU core processing or task having blocked during the RCU
  488. * read-side critical section.
  489. */
  490. static noinline void rcu_read_unlock_special(struct task_struct *t)
  491. {
  492. int empty;
  493. int empty_exp;
  494. unsigned long flags;
  495. struct list_head *np;
  496. int special;
  497. /*
  498. * NMI handlers cannot block and cannot safely manipulate state.
  499. * They therefore cannot possibly be special, so just leave.
  500. */
  501. if (in_nmi())
  502. return;
  503. local_irq_save(flags);
  504. /*
  505. * If RCU core is waiting for this CPU to exit critical section,
  506. * let it know that we have done so.
  507. */
  508. special = t->rcu_read_unlock_special;
  509. if (special & RCU_READ_UNLOCK_NEED_QS)
  510. rcu_preempt_cpu_qs();
  511. /* Hardware IRQ handlers cannot block. */
  512. if (in_irq()) {
  513. local_irq_restore(flags);
  514. return;
  515. }
  516. /* Clean up if blocked during RCU read-side critical section. */
  517. if (special & RCU_READ_UNLOCK_BLOCKED) {
  518. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  519. /*
  520. * Remove this task from the ->blkd_tasks list and adjust
  521. * any pointers that might have been referencing it.
  522. */
  523. empty = !rcu_preempt_blocked_readers_cgp();
  524. empty_exp = rcu_preempt_ctrlblk.exp_tasks == NULL;
  525. np = rcu_next_node_entry(t);
  526. list_del_init(&t->rcu_node_entry);
  527. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.gp_tasks)
  528. rcu_preempt_ctrlblk.gp_tasks = np;
  529. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.exp_tasks)
  530. rcu_preempt_ctrlblk.exp_tasks = np;
  531. #ifdef CONFIG_RCU_BOOST
  532. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.boost_tasks)
  533. rcu_preempt_ctrlblk.boost_tasks = np;
  534. #endif /* #ifdef CONFIG_RCU_BOOST */
  535. /*
  536. * If this was the last task on the current list, and if
  537. * we aren't waiting on the CPU, report the quiescent state
  538. * and start a new grace period if needed.
  539. */
  540. if (!empty && !rcu_preempt_blocked_readers_cgp()) {
  541. rcu_preempt_cpu_qs();
  542. rcu_preempt_start_gp();
  543. }
  544. /*
  545. * If this was the last task on the expedited lists,
  546. * then we need wake up the waiting task.
  547. */
  548. if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL)
  549. rcu_report_exp_done();
  550. }
  551. #ifdef CONFIG_RCU_BOOST
  552. /* Unboost self if was boosted. */
  553. if (special & RCU_READ_UNLOCK_BOOSTED) {
  554. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BOOSTED;
  555. rt_mutex_unlock(t->rcu_boost_mutex);
  556. t->rcu_boost_mutex = NULL;
  557. }
  558. #endif /* #ifdef CONFIG_RCU_BOOST */
  559. local_irq_restore(flags);
  560. }
  561. /*
  562. * Tiny-preemptible RCU implementation for rcu_read_unlock().
  563. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  564. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  565. * invoke rcu_read_unlock_special() to clean up after a context switch
  566. * in an RCU read-side critical section and other special cases.
  567. */
  568. void __rcu_read_unlock(void)
  569. {
  570. struct task_struct *t = current;
  571. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutiny.c */
  572. if (t->rcu_read_lock_nesting != 1)
  573. --t->rcu_read_lock_nesting;
  574. else {
  575. t->rcu_read_lock_nesting = INT_MIN;
  576. barrier(); /* assign before ->rcu_read_unlock_special load */
  577. if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  578. rcu_read_unlock_special(t);
  579. barrier(); /* ->rcu_read_unlock_special load before assign */
  580. t->rcu_read_lock_nesting = 0;
  581. }
  582. #ifdef CONFIG_PROVE_LOCKING
  583. {
  584. int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
  585. WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
  586. }
  587. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  588. }
  589. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  590. /*
  591. * Check for a quiescent state from the current CPU. When a task blocks,
  592. * the task is recorded in the rcu_preempt_ctrlblk structure, which is
  593. * checked elsewhere. This is called from the scheduling-clock interrupt.
  594. *
  595. * Caller must disable hard irqs.
  596. */
  597. static void rcu_preempt_check_callbacks(void)
  598. {
  599. struct task_struct *t = current;
  600. if (rcu_preempt_gp_in_progress() &&
  601. (!rcu_preempt_running_reader() ||
  602. !rcu_cpu_blocking_cur_gp()))
  603. rcu_preempt_cpu_qs();
  604. if (&rcu_preempt_ctrlblk.rcb.rcucblist !=
  605. rcu_preempt_ctrlblk.rcb.donetail)
  606. invoke_rcu_callbacks();
  607. if (rcu_preempt_gp_in_progress() &&
  608. rcu_cpu_blocking_cur_gp() &&
  609. rcu_preempt_running_reader() > 0)
  610. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  611. }
  612. /*
  613. * TINY_PREEMPT_RCU has an extra callback-list tail pointer to
  614. * update, so this is invoked from rcu_process_callbacks() to
  615. * handle that case. Of course, it is invoked for all flavors of
  616. * RCU, but RCU callbacks can appear only on one of the lists, and
  617. * neither ->nexttail nor ->donetail can possibly be NULL, so there
  618. * is no need for an explicit check.
  619. */
  620. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  621. {
  622. if (rcu_preempt_ctrlblk.nexttail == rcp->donetail)
  623. rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist;
  624. }
  625. /*
  626. * Process callbacks for preemptible RCU.
  627. */
  628. static void rcu_preempt_process_callbacks(void)
  629. {
  630. __rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb);
  631. }
  632. /*
  633. * Queue a preemptible -RCU callback for invocation after a grace period.
  634. */
  635. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  636. {
  637. unsigned long flags;
  638. debug_rcu_head_queue(head);
  639. head->func = func;
  640. head->next = NULL;
  641. local_irq_save(flags);
  642. *rcu_preempt_ctrlblk.nexttail = head;
  643. rcu_preempt_ctrlblk.nexttail = &head->next;
  644. RCU_TRACE(rcu_preempt_ctrlblk.rcb.qlen++);
  645. rcu_preempt_start_gp(); /* checks to see if GP needed. */
  646. local_irq_restore(flags);
  647. }
  648. EXPORT_SYMBOL_GPL(call_rcu);
  649. /*
  650. * synchronize_rcu - wait until a grace period has elapsed.
  651. *
  652. * Control will return to the caller some time after a full grace
  653. * period has elapsed, in other words after all currently executing RCU
  654. * read-side critical sections have completed. RCU read-side critical
  655. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  656. * and may be nested.
  657. */
  658. void synchronize_rcu(void)
  659. {
  660. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
  661. !lock_is_held(&rcu_lock_map) &&
  662. !lock_is_held(&rcu_sched_lock_map),
  663. "Illegal synchronize_rcu() in RCU read-side critical section");
  664. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  665. if (!rcu_scheduler_active)
  666. return;
  667. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  668. WARN_ON_ONCE(rcu_preempt_running_reader());
  669. if (!rcu_preempt_blocked_readers_any())
  670. return;
  671. /* Once we get past the fastpath checks, same code as rcu_barrier(). */
  672. rcu_barrier();
  673. }
  674. EXPORT_SYMBOL_GPL(synchronize_rcu);
  675. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  676. static unsigned long sync_rcu_preempt_exp_count;
  677. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  678. /*
  679. * Return non-zero if there are any tasks in RCU read-side critical
  680. * sections blocking the current preemptible-RCU expedited grace period.
  681. * If there is no preemptible-RCU expedited grace period currently in
  682. * progress, returns zero unconditionally.
  683. */
  684. static int rcu_preempted_readers_exp(void)
  685. {
  686. return rcu_preempt_ctrlblk.exp_tasks != NULL;
  687. }
  688. /*
  689. * Report the exit from RCU read-side critical section for the last task
  690. * that queued itself during or before the current expedited preemptible-RCU
  691. * grace period.
  692. */
  693. static void rcu_report_exp_done(void)
  694. {
  695. wake_up(&sync_rcu_preempt_exp_wq);
  696. }
  697. /*
  698. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  699. * is to rely in the fact that there is but one CPU, and that it is
  700. * illegal for a task to invoke synchronize_rcu_expedited() while in a
  701. * preemptible-RCU read-side critical section. Therefore, any such
  702. * critical sections must correspond to blocked tasks, which must therefore
  703. * be on the ->blkd_tasks list. So just record the current head of the
  704. * list in the ->exp_tasks pointer, and wait for all tasks including and
  705. * after the task pointed to by ->exp_tasks to drain.
  706. */
  707. void synchronize_rcu_expedited(void)
  708. {
  709. unsigned long flags;
  710. struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk;
  711. unsigned long snap;
  712. barrier(); /* ensure prior action seen before grace period. */
  713. WARN_ON_ONCE(rcu_preempt_running_reader());
  714. /*
  715. * Acquire lock so that there is only one preemptible RCU grace
  716. * period in flight. Of course, if someone does the expedited
  717. * grace period for us while we are acquiring the lock, just leave.
  718. */
  719. snap = sync_rcu_preempt_exp_count + 1;
  720. mutex_lock(&sync_rcu_preempt_exp_mutex);
  721. if (ULONG_CMP_LT(snap, sync_rcu_preempt_exp_count))
  722. goto unlock_mb_ret; /* Others did our work for us. */
  723. local_irq_save(flags);
  724. /*
  725. * All RCU readers have to already be on blkd_tasks because
  726. * we cannot legally be executing in an RCU read-side critical
  727. * section.
  728. */
  729. /* Snapshot current head of ->blkd_tasks list. */
  730. rpcp->exp_tasks = rpcp->blkd_tasks.next;
  731. if (rpcp->exp_tasks == &rpcp->blkd_tasks)
  732. rpcp->exp_tasks = NULL;
  733. /* Wait for tail of ->blkd_tasks list to drain. */
  734. if (!rcu_preempted_readers_exp())
  735. local_irq_restore(flags);
  736. else {
  737. rcu_initiate_boost();
  738. local_irq_restore(flags);
  739. wait_event(sync_rcu_preempt_exp_wq,
  740. !rcu_preempted_readers_exp());
  741. }
  742. /* Clean up and exit. */
  743. barrier(); /* ensure expedited GP seen before counter increment. */
  744. sync_rcu_preempt_exp_count++;
  745. unlock_mb_ret:
  746. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  747. barrier(); /* ensure subsequent action seen after grace period. */
  748. }
  749. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  750. /*
  751. * Does preemptible RCU need the CPU to stay out of dynticks mode?
  752. */
  753. int rcu_preempt_needs_cpu(void)
  754. {
  755. if (!rcu_preempt_running_reader())
  756. rcu_preempt_cpu_qs();
  757. return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
  758. }
  759. /*
  760. * Check for a task exiting while in a preemptible -RCU read-side
  761. * critical section, clean up if so. No need to issue warnings,
  762. * as debug_check_no_locks_held() already does this if lockdep
  763. * is enabled.
  764. */
  765. void exit_rcu(void)
  766. {
  767. struct task_struct *t = current;
  768. if (t->rcu_read_lock_nesting == 0)
  769. return;
  770. t->rcu_read_lock_nesting = 1;
  771. __rcu_read_unlock();
  772. }
  773. #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
  774. #ifdef CONFIG_RCU_TRACE
  775. /*
  776. * Because preemptible RCU does not exist, it is not necessary to
  777. * dump out its statistics.
  778. */
  779. static void show_tiny_preempt_stats(struct seq_file *m)
  780. {
  781. }
  782. #endif /* #ifdef CONFIG_RCU_TRACE */
  783. /*
  784. * Because preemptible RCU does not exist, it never has any callbacks
  785. * to check.
  786. */
  787. static void rcu_preempt_check_callbacks(void)
  788. {
  789. }
  790. /*
  791. * Because preemptible RCU does not exist, it never has any callbacks
  792. * to remove.
  793. */
  794. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  795. {
  796. }
  797. /*
  798. * Because preemptible RCU does not exist, it never has any callbacks
  799. * to process.
  800. */
  801. static void rcu_preempt_process_callbacks(void)
  802. {
  803. }
  804. #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
  805. #ifdef CONFIG_RCU_BOOST
  806. /*
  807. * Wake up rcu_kthread() to process callbacks now eligible for invocation
  808. * or to boost readers.
  809. */
  810. static void invoke_rcu_callbacks(void)
  811. {
  812. have_rcu_kthread_work = 1;
  813. wake_up(&rcu_kthread_wq);
  814. }
  815. #ifdef CONFIG_RCU_TRACE
  816. /*
  817. * Is the current CPU running the RCU-callbacks kthread?
  818. * Caller must have preemption disabled.
  819. */
  820. static bool rcu_is_callbacks_kthread(void)
  821. {
  822. return rcu_kthread_task == current;
  823. }
  824. #endif /* #ifdef CONFIG_RCU_TRACE */
  825. /*
  826. * This kthread invokes RCU callbacks whose grace periods have
  827. * elapsed. It is awakened as needed, and takes the place of the
  828. * RCU_SOFTIRQ that is used for this purpose when boosting is disabled.
  829. * This is a kthread, but it is never stopped, at least not until
  830. * the system goes down.
  831. */
  832. static int rcu_kthread(void *arg)
  833. {
  834. unsigned long work;
  835. unsigned long morework;
  836. unsigned long flags;
  837. for (;;) {
  838. wait_event_interruptible(rcu_kthread_wq,
  839. have_rcu_kthread_work != 0);
  840. morework = rcu_boost();
  841. local_irq_save(flags);
  842. work = have_rcu_kthread_work;
  843. have_rcu_kthread_work = morework;
  844. local_irq_restore(flags);
  845. if (work)
  846. rcu_process_callbacks(NULL);
  847. schedule_timeout_interruptible(1); /* Leave CPU for others. */
  848. }
  849. return 0; /* Not reached, but needed to shut gcc up. */
  850. }
  851. /*
  852. * Spawn the kthread that invokes RCU callbacks.
  853. */
  854. static int __init rcu_spawn_kthreads(void)
  855. {
  856. struct sched_param sp;
  857. rcu_kthread_task = kthread_run(rcu_kthread, NULL, "rcu_kthread");
  858. sp.sched_priority = RCU_BOOST_PRIO;
  859. sched_setscheduler_nocheck(rcu_kthread_task, SCHED_FIFO, &sp);
  860. return 0;
  861. }
  862. early_initcall(rcu_spawn_kthreads);
  863. #else /* #ifdef CONFIG_RCU_BOOST */
  864. /*
  865. * Start up softirq processing of callbacks.
  866. */
  867. void invoke_rcu_callbacks(void)
  868. {
  869. raise_softirq(RCU_SOFTIRQ);
  870. }
  871. #ifdef CONFIG_RCU_TRACE
  872. /*
  873. * There is no callback kthread, so this thread is never it.
  874. */
  875. static bool rcu_is_callbacks_kthread(void)
  876. {
  877. return false;
  878. }
  879. #endif /* #ifdef CONFIG_RCU_TRACE */
  880. void rcu_init(void)
  881. {
  882. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  883. }
  884. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  885. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  886. #include <linux/kernel_stat.h>
  887. /*
  888. * During boot, we forgive RCU lockdep issues. After this function is
  889. * invoked, we start taking RCU lockdep issues seriously.
  890. */
  891. void __init rcu_scheduler_starting(void)
  892. {
  893. WARN_ON(nr_context_switches() > 0);
  894. rcu_scheduler_active = 1;
  895. }
  896. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  897. #ifdef CONFIG_RCU_TRACE
  898. #ifdef CONFIG_RCU_BOOST
  899. static void rcu_initiate_boost_trace(void)
  900. {
  901. if (list_empty(&rcu_preempt_ctrlblk.blkd_tasks))
  902. rcu_preempt_ctrlblk.n_balk_blkd_tasks++;
  903. else if (rcu_preempt_ctrlblk.gp_tasks == NULL &&
  904. rcu_preempt_ctrlblk.exp_tasks == NULL)
  905. rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++;
  906. else if (rcu_preempt_ctrlblk.boost_tasks != NULL)
  907. rcu_preempt_ctrlblk.n_balk_boost_tasks++;
  908. else if (!ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))
  909. rcu_preempt_ctrlblk.n_balk_notyet++;
  910. else
  911. rcu_preempt_ctrlblk.n_balk_nos++;
  912. }
  913. #endif /* #ifdef CONFIG_RCU_BOOST */
  914. static void rcu_trace_sub_qlen(struct rcu_ctrlblk *rcp, int n)
  915. {
  916. unsigned long flags;
  917. raw_local_irq_save(flags);
  918. rcp->qlen -= n;
  919. raw_local_irq_restore(flags);
  920. }
  921. /*
  922. * Dump statistics for TINY_RCU, such as they are.
  923. */
  924. static int show_tiny_stats(struct seq_file *m, void *unused)
  925. {
  926. show_tiny_preempt_stats(m);
  927. seq_printf(m, "rcu_sched: qlen: %ld\n", rcu_sched_ctrlblk.qlen);
  928. seq_printf(m, "rcu_bh: qlen: %ld\n", rcu_bh_ctrlblk.qlen);
  929. return 0;
  930. }
  931. static int show_tiny_stats_open(struct inode *inode, struct file *file)
  932. {
  933. return single_open(file, show_tiny_stats, NULL);
  934. }
  935. static const struct file_operations show_tiny_stats_fops = {
  936. .owner = THIS_MODULE,
  937. .open = show_tiny_stats_open,
  938. .read = seq_read,
  939. .llseek = seq_lseek,
  940. .release = single_release,
  941. };
  942. static struct dentry *rcudir;
  943. static int __init rcutiny_trace_init(void)
  944. {
  945. struct dentry *retval;
  946. rcudir = debugfs_create_dir("rcu", NULL);
  947. if (!rcudir)
  948. goto free_out;
  949. retval = debugfs_create_file("rcudata", 0444, rcudir,
  950. NULL, &show_tiny_stats_fops);
  951. if (!retval)
  952. goto free_out;
  953. return 0;
  954. free_out:
  955. debugfs_remove_recursive(rcudir);
  956. return 1;
  957. }
  958. static void __exit rcutiny_trace_cleanup(void)
  959. {
  960. debugfs_remove_recursive(rcudir);
  961. }
  962. module_init(rcutiny_trace_init);
  963. module_exit(rcutiny_trace_cleanup);
  964. MODULE_AUTHOR("Paul E. McKenney");
  965. MODULE_DESCRIPTION("Read-Copy Update tracing for tiny implementation");
  966. MODULE_LICENSE("GPL");
  967. #endif /* #ifdef CONFIG_RCU_TRACE */