rcutiny_plugin.h 32 KB

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