rcutiny_plugin.h 32 KB

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