rcutiny_plugin.h 29 KB

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