rcutiny_plugin.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. if (!rcu_preempt_running_reader())
  761. rcu_preempt_cpu_qs();
  762. return rcu_preempt_ctrlblk.rcb.rcucblist != NULL;
  763. }
  764. #else /* #ifdef CONFIG_TINY_PREEMPT_RCU */
  765. #ifdef CONFIG_RCU_TRACE
  766. /*
  767. * Because preemptible RCU does not exist, it is not necessary to
  768. * dump out its statistics.
  769. */
  770. static void show_tiny_preempt_stats(struct seq_file *m)
  771. {
  772. }
  773. #endif /* #ifdef CONFIG_RCU_TRACE */
  774. /*
  775. * Because preemptible RCU does not exist, it never has any callbacks
  776. * to check.
  777. */
  778. static void rcu_preempt_check_callbacks(void)
  779. {
  780. }
  781. /*
  782. * Because preemptible RCU does not exist, it never has any callbacks
  783. * to remove.
  784. */
  785. static void rcu_preempt_remove_callbacks(struct rcu_ctrlblk *rcp)
  786. {
  787. }
  788. /*
  789. * Because preemptible RCU does not exist, it never has any callbacks
  790. * to process.
  791. */
  792. static void rcu_preempt_process_callbacks(void)
  793. {
  794. }
  795. #endif /* #else #ifdef CONFIG_TINY_PREEMPT_RCU */
  796. #ifdef CONFIG_RCU_BOOST
  797. /*
  798. * Wake up rcu_kthread() to process callbacks now eligible for invocation
  799. * or to boost readers.
  800. */
  801. static void invoke_rcu_callbacks(void)
  802. {
  803. have_rcu_kthread_work = 1;
  804. if (rcu_kthread_task != NULL)
  805. wake_up(&rcu_kthread_wq);
  806. }
  807. #ifdef CONFIG_RCU_TRACE
  808. /*
  809. * Is the current CPU running the RCU-callbacks kthread?
  810. * Caller must have preemption disabled.
  811. */
  812. static bool rcu_is_callbacks_kthread(void)
  813. {
  814. return rcu_kthread_task == current;
  815. }
  816. #endif /* #ifdef CONFIG_RCU_TRACE */
  817. /*
  818. * This kthread invokes RCU callbacks whose grace periods have
  819. * elapsed. It is awakened as needed, and takes the place of the
  820. * RCU_SOFTIRQ that is used for this purpose when boosting is disabled.
  821. * This is a kthread, but it is never stopped, at least not until
  822. * the system goes down.
  823. */
  824. static int rcu_kthread(void *arg)
  825. {
  826. unsigned long work;
  827. unsigned long morework;
  828. unsigned long flags;
  829. for (;;) {
  830. wait_event_interruptible(rcu_kthread_wq,
  831. have_rcu_kthread_work != 0);
  832. morework = rcu_boost();
  833. local_irq_save(flags);
  834. work = have_rcu_kthread_work;
  835. have_rcu_kthread_work = morework;
  836. local_irq_restore(flags);
  837. if (work)
  838. rcu_process_callbacks(NULL);
  839. schedule_timeout_interruptible(1); /* Leave CPU for others. */
  840. }
  841. return 0; /* Not reached, but needed to shut gcc up. */
  842. }
  843. /*
  844. * Spawn the kthread that invokes RCU callbacks.
  845. */
  846. static int __init rcu_spawn_kthreads(void)
  847. {
  848. struct sched_param sp;
  849. rcu_kthread_task = kthread_run(rcu_kthread, NULL, "rcu_kthread");
  850. sp.sched_priority = RCU_BOOST_PRIO;
  851. sched_setscheduler_nocheck(rcu_kthread_task, SCHED_FIFO, &sp);
  852. return 0;
  853. }
  854. early_initcall(rcu_spawn_kthreads);
  855. #else /* #ifdef CONFIG_RCU_BOOST */
  856. /* Hold off callback invocation until early_initcall() time. */
  857. static int rcu_scheduler_fully_active __read_mostly;
  858. /*
  859. * Start up softirq processing of callbacks.
  860. */
  861. void invoke_rcu_callbacks(void)
  862. {
  863. if (rcu_scheduler_fully_active)
  864. raise_softirq(RCU_SOFTIRQ);
  865. }
  866. #ifdef CONFIG_RCU_TRACE
  867. /*
  868. * There is no callback kthread, so this thread is never it.
  869. */
  870. static bool rcu_is_callbacks_kthread(void)
  871. {
  872. return false;
  873. }
  874. #endif /* #ifdef CONFIG_RCU_TRACE */
  875. static int __init rcu_scheduler_really_started(void)
  876. {
  877. rcu_scheduler_fully_active = 1;
  878. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  879. raise_softirq(RCU_SOFTIRQ); /* Invoke any callbacks from early boot. */
  880. return 0;
  881. }
  882. early_initcall(rcu_scheduler_really_started);
  883. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  884. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  885. #include <linux/kernel_stat.h>
  886. /*
  887. * During boot, we forgive RCU lockdep issues. After this function is
  888. * invoked, we start taking RCU lockdep issues seriously.
  889. */
  890. void __init rcu_scheduler_starting(void)
  891. {
  892. WARN_ON(nr_context_switches() > 0);
  893. rcu_scheduler_active = 1;
  894. }
  895. #endif /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  896. #ifdef CONFIG_RCU_TRACE
  897. #ifdef CONFIG_RCU_BOOST
  898. static void rcu_initiate_boost_trace(void)
  899. {
  900. if (list_empty(&rcu_preempt_ctrlblk.blkd_tasks))
  901. rcu_preempt_ctrlblk.n_balk_blkd_tasks++;
  902. else if (rcu_preempt_ctrlblk.gp_tasks == NULL &&
  903. rcu_preempt_ctrlblk.exp_tasks == NULL)
  904. rcu_preempt_ctrlblk.n_balk_exp_gp_tasks++;
  905. else if (rcu_preempt_ctrlblk.boost_tasks != NULL)
  906. rcu_preempt_ctrlblk.n_balk_boost_tasks++;
  907. else if (!ULONG_CMP_GE(jiffies, rcu_preempt_ctrlblk.boost_time))
  908. rcu_preempt_ctrlblk.n_balk_notyet++;
  909. else
  910. rcu_preempt_ctrlblk.n_balk_nos++;
  911. }
  912. #endif /* #ifdef CONFIG_RCU_BOOST */
  913. static void rcu_trace_sub_qlen(struct rcu_ctrlblk *rcp, int n)
  914. {
  915. unsigned long flags;
  916. raw_local_irq_save(flags);
  917. rcp->qlen -= n;
  918. raw_local_irq_restore(flags);
  919. }
  920. /*
  921. * Dump statistics for TINY_RCU, such as they are.
  922. */
  923. static int show_tiny_stats(struct seq_file *m, void *unused)
  924. {
  925. show_tiny_preempt_stats(m);
  926. seq_printf(m, "rcu_sched: qlen: %ld\n", rcu_sched_ctrlblk.qlen);
  927. seq_printf(m, "rcu_bh: qlen: %ld\n", rcu_bh_ctrlblk.qlen);
  928. return 0;
  929. }
  930. static int show_tiny_stats_open(struct inode *inode, struct file *file)
  931. {
  932. return single_open(file, show_tiny_stats, NULL);
  933. }
  934. static const struct file_operations show_tiny_stats_fops = {
  935. .owner = THIS_MODULE,
  936. .open = show_tiny_stats_open,
  937. .read = seq_read,
  938. .llseek = seq_lseek,
  939. .release = single_release,
  940. };
  941. static struct dentry *rcudir;
  942. static int __init rcutiny_trace_init(void)
  943. {
  944. struct dentry *retval;
  945. rcudir = debugfs_create_dir("rcu", NULL);
  946. if (!rcudir)
  947. goto free_out;
  948. retval = debugfs_create_file("rcudata", 0444, rcudir,
  949. NULL, &show_tiny_stats_fops);
  950. if (!retval)
  951. goto free_out;
  952. return 0;
  953. free_out:
  954. debugfs_remove_recursive(rcudir);
  955. return 1;
  956. }
  957. static void __exit rcutiny_trace_cleanup(void)
  958. {
  959. debugfs_remove_recursive(rcudir);
  960. }
  961. module_init(rcutiny_trace_init);
  962. module_exit(rcutiny_trace_cleanup);
  963. MODULE_AUTHOR("Paul E. McKenney");
  964. MODULE_DESCRIPTION("Read-Copy Update tracing for tiny implementation");
  965. MODULE_LICENSE("GPL");
  966. #endif /* #ifdef CONFIG_RCU_TRACE */