rcutiny_plugin.h 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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. raw_local_irq_restore(flags);
  290. rt_mutex_lock(&mtx);
  291. rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
  292. return ACCESS_ONCE(rcu_preempt_ctrlblk.boost_tasks) != NULL ||
  293. ACCESS_ONCE(rcu_preempt_ctrlblk.exp_tasks) != NULL;
  294. }
  295. /*
  296. * Check to see if it is now time to start boosting RCU readers blocking
  297. * the current grace period, and, if so, tell the rcu_kthread_task to
  298. * start boosting them. If there is an expedited boost in progress,
  299. * we wait for it to complete.
  300. *
  301. * If there are no blocked readers blocking the current grace period,
  302. * return 0 to let the caller know, otherwise return 1. Note that this
  303. * return value is independent of whether or not boosting was done.
  304. */
  305. static int rcu_initiate_boost(void)
  306. {
  307. if (!rcu_preempt_blocked_readers_cgp() &&
  308. rcu_preempt_ctrlblk.exp_tasks == NULL) {
  309. RCU_TRACE(rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++);
  310. return 0;
  311. }
  312. if (rcu_preempt_ctrlblk.exp_tasks != NULL ||
  313. (rcu_preempt_ctrlblk.gp_tasks != NULL &&
  314. rcu_preempt_ctrlblk.boost_tasks == NULL &&
  315. ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))) {
  316. if (rcu_preempt_ctrlblk.exp_tasks == NULL)
  317. rcu_preempt_ctrlblk.boost_tasks =
  318. rcu_preempt_ctrlblk.gp_tasks;
  319. invoke_rcu_callbacks();
  320. } else
  321. RCU_TRACE(rcu_initiate_boost_trace());
  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. * Tiny-preemptible RCU implementation for rcu_read_lock().
  475. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  476. * if we block.
  477. */
  478. void __rcu_read_lock(void)
  479. {
  480. current->rcu_read_lock_nesting++;
  481. barrier(); /* needed if we ever invoke rcu_read_lock in rcutiny.c */
  482. }
  483. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  484. /*
  485. * Handle special cases during rcu_read_unlock(), such as needing to
  486. * notify RCU core processing or task having blocked during the RCU
  487. * read-side critical section.
  488. */
  489. static noinline void rcu_read_unlock_special(struct task_struct *t)
  490. {
  491. int empty;
  492. int empty_exp;
  493. unsigned long flags;
  494. struct list_head *np;
  495. #ifdef CONFIG_RCU_BOOST
  496. struct rt_mutex *rbmp = NULL;
  497. #endif /* #ifdef CONFIG_RCU_BOOST */
  498. int special;
  499. /*
  500. * NMI handlers cannot block and cannot safely manipulate state.
  501. * They therefore cannot possibly be special, so just leave.
  502. */
  503. if (in_nmi())
  504. return;
  505. local_irq_save(flags);
  506. /*
  507. * If RCU core is waiting for this CPU to exit critical section,
  508. * let it know that we have done so.
  509. */
  510. special = t->rcu_read_unlock_special;
  511. if (special & RCU_READ_UNLOCK_NEED_QS)
  512. rcu_preempt_cpu_qs();
  513. /* Hardware IRQ handlers cannot block. */
  514. if (in_irq() || in_serving_softirq()) {
  515. local_irq_restore(flags);
  516. return;
  517. }
  518. /* Clean up if blocked during RCU read-side critical section. */
  519. if (special & RCU_READ_UNLOCK_BLOCKED) {
  520. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  521. /*
  522. * Remove this task from the ->blkd_tasks list and adjust
  523. * any pointers that might have been referencing it.
  524. */
  525. empty = !rcu_preempt_blocked_readers_cgp();
  526. empty_exp = rcu_preempt_ctrlblk.exp_tasks == NULL;
  527. np = rcu_next_node_entry(t);
  528. list_del_init(&t->rcu_node_entry);
  529. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.gp_tasks)
  530. rcu_preempt_ctrlblk.gp_tasks = np;
  531. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.exp_tasks)
  532. rcu_preempt_ctrlblk.exp_tasks = np;
  533. #ifdef CONFIG_RCU_BOOST
  534. if (&t->rcu_node_entry == rcu_preempt_ctrlblk.boost_tasks)
  535. rcu_preempt_ctrlblk.boost_tasks = np;
  536. #endif /* #ifdef CONFIG_RCU_BOOST */
  537. /*
  538. * If this was the last task on the current list, and if
  539. * we aren't waiting on the CPU, report the quiescent state
  540. * and start a new grace period if needed.
  541. */
  542. if (!empty && !rcu_preempt_blocked_readers_cgp()) {
  543. rcu_preempt_cpu_qs();
  544. rcu_preempt_start_gp();
  545. }
  546. /*
  547. * If this was the last task on the expedited lists,
  548. * then we need wake up the waiting task.
  549. */
  550. if (!empty_exp && rcu_preempt_ctrlblk.exp_tasks == NULL)
  551. rcu_report_exp_done();
  552. }
  553. #ifdef CONFIG_RCU_BOOST
  554. /* Unboost self if was boosted. */
  555. if (t->rcu_boost_mutex != NULL) {
  556. rbmp = t->rcu_boost_mutex;
  557. t->rcu_boost_mutex = NULL;
  558. rt_mutex_unlock(rbmp);
  559. }
  560. #endif /* #ifdef CONFIG_RCU_BOOST */
  561. local_irq_restore(flags);
  562. }
  563. /*
  564. * Tiny-preemptible RCU implementation for rcu_read_unlock().
  565. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  566. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  567. * invoke rcu_read_unlock_special() to clean up after a context switch
  568. * in an RCU read-side critical section and other special cases.
  569. */
  570. void __rcu_read_unlock(void)
  571. {
  572. struct task_struct *t = current;
  573. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutiny.c */
  574. if (t->rcu_read_lock_nesting != 1)
  575. --t->rcu_read_lock_nesting;
  576. else {
  577. t->rcu_read_lock_nesting = INT_MIN;
  578. barrier(); /* assign before ->rcu_read_unlock_special load */
  579. if (unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  580. rcu_read_unlock_special(t);
  581. barrier(); /* ->rcu_read_unlock_special load before assign */
  582. t->rcu_read_lock_nesting = 0;
  583. }
  584. #ifdef CONFIG_PROVE_LOCKING
  585. {
  586. int rrln = ACCESS_ONCE(t->rcu_read_lock_nesting);
  587. WARN_ON_ONCE(rrln < 0 && rrln > INT_MIN / 2);
  588. }
  589. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  590. }
  591. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  592. /*
  593. * Check for a quiescent state from the current CPU. When a task blocks,
  594. * the task is recorded in the rcu_preempt_ctrlblk structure, which is
  595. * checked elsewhere. This is called from the scheduling-clock interrupt.
  596. *
  597. * Caller must disable hard irqs.
  598. */
  599. static void rcu_preempt_check_callbacks(void)
  600. {
  601. struct task_struct *t = current;
  602. if (rcu_preempt_gp_in_progress() &&
  603. (!rcu_preempt_running_reader() ||
  604. !rcu_cpu_blocking_cur_gp()))
  605. rcu_preempt_cpu_qs();
  606. if (&rcu_preempt_ctrlblk.rcb.rcucblist !=
  607. rcu_preempt_ctrlblk.rcb.donetail)
  608. invoke_rcu_callbacks();
  609. if (rcu_preempt_gp_in_progress() &&
  610. rcu_cpu_blocking_cur_gp() &&
  611. rcu_preempt_running_reader() > 0)
  612. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  613. }
  614. /*
  615. * TINY_PREEMPT_RCU has an extra callback-list tail pointer to
  616. * update, so this is invoked from rcu_process_callbacks() to
  617. * handle that case. Of course, it is invoked for all flavors of
  618. * RCU, but RCU callbacks can appear only on one of the lists, and
  619. * neither ->nexttail nor ->donetail can possibly be NULL, so there
  620. * is no need for an explicit check.
  621. */
  622. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  623. {
  624. if (rcu_preempt_ctrlblk.nexttail == rcp->donetail)
  625. rcu_preempt_ctrlblk.nexttail = &rcp->rcucblist;
  626. }
  627. /*
  628. * Process callbacks for preemptible RCU.
  629. */
  630. static void rcu_preempt_process_callbacks(void)
  631. {
  632. __rcu_process_callbacks(&rcu_preempt_ctrlblk.rcb);
  633. }
  634. /*
  635. * Queue a preemptible -RCU callback for invocation after a grace period.
  636. */
  637. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  638. {
  639. unsigned long flags;
  640. debug_rcu_head_queue(head);
  641. head->func = func;
  642. head->next = NULL;
  643. local_irq_save(flags);
  644. *rcu_preempt_ctrlblk.nexttail = head;
  645. rcu_preempt_ctrlblk.nexttail = &head->next;
  646. RCU_TRACE(rcu_preempt_ctrlblk.rcb.qlen++);
  647. rcu_preempt_start_gp(); /* checks to see if GP needed. */
  648. local_irq_restore(flags);
  649. }
  650. EXPORT_SYMBOL_GPL(call_rcu);
  651. /*
  652. * synchronize_rcu - wait until a grace period has elapsed.
  653. *
  654. * Control will return to the caller some time after a full grace
  655. * period has elapsed, in other words after all currently executing RCU
  656. * read-side critical sections have completed. RCU read-side critical
  657. * sections are delimited by rcu_read_lock() and rcu_read_unlock(),
  658. * and may be nested.
  659. */
  660. void synchronize_rcu(void)
  661. {
  662. rcu_lockdep_assert(!lock_is_held(&rcu_bh_lock_map) &&
  663. !lock_is_held(&rcu_lock_map) &&
  664. !lock_is_held(&rcu_sched_lock_map),
  665. "Illegal synchronize_rcu() in RCU read-side critical section");
  666. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  667. if (!rcu_scheduler_active)
  668. return;
  669. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  670. WARN_ON_ONCE(rcu_preempt_running_reader());
  671. if (!rcu_preempt_blocked_readers_any())
  672. return;
  673. /* Once we get past the fastpath checks, same code as rcu_barrier(). */
  674. if (rcu_expedited)
  675. synchronize_rcu_expedited();
  676. else
  677. rcu_barrier();
  678. }
  679. EXPORT_SYMBOL_GPL(synchronize_rcu);
  680. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  681. static unsigned long sync_rcu_preempt_exp_count;
  682. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  683. /*
  684. * Return non-zero if there are any tasks in RCU read-side critical
  685. * sections blocking the current preemptible-RCU expedited grace period.
  686. * If there is no preemptible-RCU expedited grace period currently in
  687. * progress, returns zero unconditionally.
  688. */
  689. static int rcu_preempted_readers_exp(void)
  690. {
  691. return rcu_preempt_ctrlblk.exp_tasks != NULL;
  692. }
  693. /*
  694. * Report the exit from RCU read-side critical section for the last task
  695. * that queued itself during or before the current expedited preemptible-RCU
  696. * grace period.
  697. */
  698. static void rcu_report_exp_done(void)
  699. {
  700. wake_up(&sync_rcu_preempt_exp_wq);
  701. }
  702. /*
  703. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  704. * is to rely in the fact that there is but one CPU, and that it is
  705. * illegal for a task to invoke synchronize_rcu_expedited() while in a
  706. * preemptible-RCU read-side critical section. Therefore, any such
  707. * critical sections must correspond to blocked tasks, which must therefore
  708. * be on the ->blkd_tasks list. So just record the current head of the
  709. * list in the ->exp_tasks pointer, and wait for all tasks including and
  710. * after the task pointed to by ->exp_tasks to drain.
  711. */
  712. void synchronize_rcu_expedited(void)
  713. {
  714. unsigned long flags;
  715. struct rcu_preempt_ctrlblk *rpcp = &rcu_preempt_ctrlblk;
  716. unsigned long snap;
  717. barrier(); /* ensure prior action seen before grace period. */
  718. WARN_ON_ONCE(rcu_preempt_running_reader());
  719. /*
  720. * Acquire lock so that there is only one preemptible RCU grace
  721. * period in flight. Of course, if someone does the expedited
  722. * grace period for us while we are acquiring the lock, just leave.
  723. */
  724. snap = sync_rcu_preempt_exp_count + 1;
  725. mutex_lock(&sync_rcu_preempt_exp_mutex);
  726. if (ULONG_CMP_LT(snap, sync_rcu_preempt_exp_count))
  727. goto unlock_mb_ret; /* Others did our work for us. */
  728. local_irq_save(flags);
  729. /*
  730. * All RCU readers have to already be on blkd_tasks because
  731. * we cannot legally be executing in an RCU read-side critical
  732. * section.
  733. */
  734. /* Snapshot current head of ->blkd_tasks list. */
  735. rpcp->exp_tasks = rpcp->blkd_tasks.next;
  736. if (rpcp->exp_tasks == &rpcp->blkd_tasks)
  737. rpcp->exp_tasks = NULL;
  738. /* Wait for tail of ->blkd_tasks list to drain. */
  739. if (!rcu_preempted_readers_exp())
  740. local_irq_restore(flags);
  741. else {
  742. rcu_initiate_boost();
  743. local_irq_restore(flags);
  744. wait_event(sync_rcu_preempt_exp_wq,
  745. !rcu_preempted_readers_exp());
  746. }
  747. /* Clean up and exit. */
  748. barrier(); /* ensure expedited GP seen before counter increment. */
  749. sync_rcu_preempt_exp_count++;
  750. unlock_mb_ret:
  751. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  752. barrier(); /* ensure subsequent action seen after grace period. */
  753. }
  754. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  755. /*
  756. * Does preemptible RCU need the CPU to stay out of dynticks mode?
  757. */
  758. int rcu_preempt_needs_cpu(void)
  759. {
  760. return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
  761. }
  762. #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
  763. #ifdef CONFIG_RCU_TRACE
  764. /*
  765. * Because preemptible RCU does not exist, it is not necessary to
  766. * dump out its statistics.
  767. */
  768. static void show_tiny_preempt_stats(struct seq_file *m)
  769. {
  770. }
  771. #endif /* #ifdef CONFIG_RCU_TRACE */
  772. /*
  773. * Because preemptible RCU does not exist, it never has any callbacks
  774. * to check.
  775. */
  776. static void rcu_preempt_check_callbacks(void)
  777. {
  778. }
  779. /*
  780. * Because preemptible RCU does not exist, it never has any callbacks
  781. * to remove.
  782. */
  783. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  784. {
  785. }
  786. /*
  787. * Because preemptible RCU does not exist, it never has any callbacks
  788. * to process.
  789. */
  790. static void rcu_preempt_process_callbacks(void)
  791. {
  792. }
  793. #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
  794. #ifdef CONFIG_RCU_BOOST
  795. /*
  796. * Wake up rcu_kthread() to process callbacks now eligible for invocation
  797. * or to boost readers.
  798. */
  799. static void invoke_rcu_callbacks(void)
  800. {
  801. have_rcu_kthread_work = 1;
  802. if (rcu_kthread_task != NULL)
  803. wake_up(&rcu_kthread_wq);
  804. }
  805. #ifdef CONFIG_RCU_TRACE
  806. /*
  807. * Is the current CPU running the RCU-callbacks kthread?
  808. * Caller must have preemption disabled.
  809. */
  810. static bool rcu_is_callbacks_kthread(void)
  811. {
  812. return rcu_kthread_task == current;
  813. }
  814. #endif /* #ifdef CONFIG_RCU_TRACE */
  815. /*
  816. * This kthread invokes RCU callbacks whose grace periods have
  817. * elapsed. It is awakened as needed, and takes the place of the
  818. * RCU_SOFTIRQ that is used for this purpose when boosting is disabled.
  819. * This is a kthread, but it is never stopped, at least not until
  820. * the system goes down.
  821. */
  822. static int rcu_kthread(void *arg)
  823. {
  824. unsigned long work;
  825. unsigned long morework;
  826. unsigned long flags;
  827. for (;;) {
  828. wait_event_interruptible(rcu_kthread_wq,
  829. have_rcu_kthread_work != 0);
  830. morework = rcu_boost();
  831. local_irq_save(flags);
  832. work = have_rcu_kthread_work;
  833. have_rcu_kthread_work = morework;
  834. local_irq_restore(flags);
  835. if (work)
  836. rcu_process_callbacks(NULL);
  837. schedule_timeout_interruptible(1); /* Leave CPU for others. */
  838. }
  839. return 0; /* Not reached, but needed to shut gcc up. */
  840. }
  841. /*
  842. * Spawn the kthread that invokes RCU callbacks.
  843. */
  844. static int __init rcu_spawn_kthreads(void)
  845. {
  846. struct sched_param sp;
  847. rcu_kthread_task = kthread_run(rcu_kthread, NULL, "rcu_kthread");
  848. sp.sched_priority = RCU_BOOST_PRIO;
  849. sched_setscheduler_nocheck(rcu_kthread_task, SCHED_FIFO, &sp);
  850. return 0;
  851. }
  852. early_initcall(rcu_spawn_kthreads);
  853. #else /* #ifdef CONFIG_RCU_BOOST */
  854. /* Hold off callback invocation until early_initcall() time. */
  855. static int rcu_scheduler_fully_active __read_mostly;
  856. /*
  857. * Start up softirq processing of callbacks.
  858. */
  859. void invoke_rcu_callbacks(void)
  860. {
  861. if (rcu_scheduler_fully_active)
  862. raise_softirq(RCU_SOFTIRQ);
  863. }
  864. #ifdef CONFIG_RCU_TRACE
  865. /*
  866. * There is no callback kthread, so this thread is never it.
  867. */
  868. static bool rcu_is_callbacks_kthread(void)
  869. {
  870. return false;
  871. }
  872. #endif /* #ifdef CONFIG_RCU_TRACE */
  873. static int __init rcu_scheduler_really_started(void)
  874. {
  875. rcu_scheduler_fully_active = 1;
  876. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  877. raise_softirq(RCU_SOFTIRQ); /* Invoke any callbacks from early boot. */
  878. return 0;
  879. }
  880. early_initcall(rcu_scheduler_really_started);
  881. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  882. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  883. #include <linux/kernel_stat.h>
  884. /*
  885. * During boot, we forgive RCU lockdep issues. After this function is
  886. * invoked, we start taking RCU lockdep issues seriously.
  887. */
  888. void __init rcu_scheduler_starting(void)
  889. {
  890. WARN_ON(nr_context_switches() > 0);
  891. rcu_scheduler_active = 1;
  892. }
  893. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  894. #ifdef CONFIG_RCU_TRACE
  895. #ifdef CONFIG_RCU_BOOST
  896. static void rcu_initiate_boost_trace(void)
  897. {
  898. if (list_empty(&rcu_preempt_ctrlblk.blkd_tasks))
  899. rcu_preempt_ctrlblk.n_balk_blkd_tasks++;
  900. else if (rcu_preempt_ctrlblk.gp_tasks == NULL &&
  901. rcu_preempt_ctrlblk.exp_tasks == NULL)
  902. rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++;
  903. else if (rcu_preempt_ctrlblk.boost_tasks != NULL)
  904. rcu_preempt_ctrlblk.n_balk_boost_tasks++;
  905. else if (!ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))
  906. rcu_preempt_ctrlblk.n_balk_notyet++;
  907. else
  908. rcu_preempt_ctrlblk.n_balk_nos++;
  909. }
  910. #endif /* #ifdef CONFIG_RCU_BOOST */
  911. static void rcu_trace_sub_qlen(struct rcu_ctrlblk *rcp, int n)
  912. {
  913. unsigned long flags;
  914. raw_local_irq_save(flags);
  915. rcp->qlen -= n;
  916. raw_local_irq_restore(flags);
  917. }
  918. /*
  919. * Dump statistics for TINY_RCU, such as they are.
  920. */
  921. static int show_tiny_stats(struct seq_file *m, void *unused)
  922. {
  923. show_tiny_preempt_stats(m);
  924. seq_printf(m, "rcu_sched: qlen: %ld\n", rcu_sched_ctrlblk.qlen);
  925. seq_printf(m, "rcu_bh: qlen: %ld\n", rcu_bh_ctrlblk.qlen);
  926. return 0;
  927. }
  928. static int show_tiny_stats_open(struct inode *inode, struct file *file)
  929. {
  930. return single_open(file, show_tiny_stats, NULL);
  931. }
  932. static const struct file_operations show_tiny_stats_fops = {
  933. .owner = THIS_MODULE,
  934. .open = show_tiny_stats_open,
  935. .read = seq_read,
  936. .llseek = seq_lseek,
  937. .release = single_release,
  938. };
  939. static struct dentry *rcudir;
  940. static int __init rcutiny_trace_init(void)
  941. {
  942. struct dentry *retval;
  943. rcudir = debugfs_create_dir("rcu", NULL);
  944. if (!rcudir)
  945. goto free_out;
  946. retval = debugfs_create_file("rcudata", 0444, rcudir,
  947. NULL, &show_tiny_stats_fops);
  948. if (!retval)
  949. goto free_out;
  950. return 0;
  951. free_out:
  952. debugfs_remove_recursive(rcudir);
  953. return 1;
  954. }
  955. static void __exit rcutiny_trace_cleanup(void)
  956. {
  957. debugfs_remove_recursive(rcudir);
  958. }
  959. module_init(rcutiny_trace_init);
  960. module_exit(rcutiny_trace_cleanup);
  961. MODULE_AUTHOR("Paul E. McKenney");
  962. MODULE_DESCRIPTION("Read-Copy Update tracing for tiny implementation");
  963. MODULE_LICENSE("GPL");
  964. #endif /* #ifdef CONFIG_RCU_TRACE */