rcutree_plugin.h 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion (tree-based version)
  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 Red Hat, 2009
  21. * Copyright IBM Corporation, 2009
  22. *
  23. * Author: Ingo Molnar <mingo@elte.hu>
  24. * Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  25. */
  26. #include <linux/delay.h>
  27. #include <linux/stop_machine.h>
  28. /*
  29. * Check the RCU kernel configuration parameters and print informative
  30. * messages about anything out of the ordinary. If you like #ifdef, you
  31. * will love this function.
  32. */
  33. static void __init rcu_bootup_announce_oddness(void)
  34. {
  35. #ifdef CONFIG_RCU_TRACE
  36. printk(KERN_INFO "\tRCU debugfs-based tracing is enabled.\n");
  37. #endif
  38. #if (defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 64) || (!defined(CONFIG_64BIT) && CONFIG_RCU_FANOUT != 32)
  39. printk(KERN_INFO "\tCONFIG_RCU_FANOUT set to non-default value of %d\n",
  40. CONFIG_RCU_FANOUT);
  41. #endif
  42. #ifdef CONFIG_RCU_FANOUT_EXACT
  43. printk(KERN_INFO "\tHierarchical RCU autobalancing is disabled.\n");
  44. #endif
  45. #ifdef CONFIG_RCU_FAST_NO_HZ
  46. printk(KERN_INFO
  47. "\tRCU dyntick-idle grace-period acceleration is enabled.\n");
  48. #endif
  49. #ifdef CONFIG_PROVE_RCU
  50. printk(KERN_INFO "\tRCU lockdep checking is enabled.\n");
  51. #endif
  52. #ifdef CONFIG_RCU_TORTURE_TEST_RUNNABLE
  53. printk(KERN_INFO "\tRCU torture testing starts during boot.\n");
  54. #endif
  55. #if defined(CONFIG_TREE_PREEMPT_RCU) && !defined(CONFIG_RCU_CPU_STALL_VERBOSE)
  56. printk(KERN_INFO "\tVerbose stalled-CPUs detection is disabled.\n");
  57. #endif
  58. #if NUM_RCU_LVL_4 != 0
  59. printk(KERN_INFO "\tExperimental four-level hierarchy is enabled.\n");
  60. #endif
  61. }
  62. #ifdef CONFIG_TREE_PREEMPT_RCU
  63. struct rcu_state rcu_preempt_state = RCU_STATE_INITIALIZER(rcu_preempt_state);
  64. DEFINE_PER_CPU(struct rcu_data, rcu_preempt_data);
  65. static struct rcu_state *rcu_state = &rcu_preempt_state;
  66. static int rcu_preempted_readers_exp(struct rcu_node *rnp);
  67. /*
  68. * Tell them what RCU they are running.
  69. */
  70. static void __init rcu_bootup_announce(void)
  71. {
  72. printk(KERN_INFO "Preemptible hierarchical RCU implementation.\n");
  73. rcu_bootup_announce_oddness();
  74. }
  75. /*
  76. * Return the number of RCU-preempt batches processed thus far
  77. * for debug and statistics.
  78. */
  79. long rcu_batches_completed_preempt(void)
  80. {
  81. return rcu_preempt_state.completed;
  82. }
  83. EXPORT_SYMBOL_GPL(rcu_batches_completed_preempt);
  84. /*
  85. * Return the number of RCU batches processed thus far for debug & stats.
  86. */
  87. long rcu_batches_completed(void)
  88. {
  89. return rcu_batches_completed_preempt();
  90. }
  91. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  92. /*
  93. * Force a quiescent state for preemptible RCU.
  94. */
  95. void rcu_force_quiescent_state(void)
  96. {
  97. force_quiescent_state(&rcu_preempt_state, 0);
  98. }
  99. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  100. /*
  101. * Record a preemptible-RCU quiescent state for the specified CPU. Note
  102. * that this just means that the task currently running on the CPU is
  103. * not in a quiescent state. There might be any number of tasks blocked
  104. * while in an RCU read-side critical section.
  105. *
  106. * Unlike the other rcu_*_qs() functions, callers to this function
  107. * must disable irqs in order to protect the assignment to
  108. * ->rcu_read_unlock_special.
  109. */
  110. static void rcu_preempt_qs(int cpu)
  111. {
  112. struct rcu_data *rdp = &per_cpu(rcu_preempt_data, cpu);
  113. rdp->passed_quiesc_completed = rdp->gpnum - 1;
  114. barrier();
  115. rdp->passed_quiesc = 1;
  116. current->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_NEED_QS;
  117. }
  118. /*
  119. * We have entered the scheduler, and the current task might soon be
  120. * context-switched away from. If this task is in an RCU read-side
  121. * critical section, we will no longer be able to rely on the CPU to
  122. * record that fact, so we enqueue the task on the blkd_tasks list.
  123. * The task will dequeue itself when it exits the outermost enclosing
  124. * RCU read-side critical section. Therefore, the current grace period
  125. * cannot be permitted to complete until the blkd_tasks list entries
  126. * predating the current grace period drain, in other words, until
  127. * rnp->gp_tasks becomes NULL.
  128. *
  129. * Caller must disable preemption.
  130. */
  131. static void rcu_preempt_note_context_switch(int cpu)
  132. {
  133. struct task_struct *t = current;
  134. unsigned long flags;
  135. struct rcu_data *rdp;
  136. struct rcu_node *rnp;
  137. if (t->rcu_read_lock_nesting &&
  138. (t->rcu_read_unlock_special & RCU_READ_UNLOCK_BLOCKED) == 0) {
  139. /* Possibly blocking in an RCU read-side critical section. */
  140. rdp = per_cpu_ptr(rcu_preempt_state.rda, cpu);
  141. rnp = rdp->mynode;
  142. raw_spin_lock_irqsave(&rnp->lock, flags);
  143. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BLOCKED;
  144. t->rcu_blocked_node = rnp;
  145. /*
  146. * If this CPU has already checked in, then this task
  147. * will hold up the next grace period rather than the
  148. * current grace period. Queue the task accordingly.
  149. * If the task is queued for the current grace period
  150. * (i.e., this CPU has not yet passed through a quiescent
  151. * state for the current grace period), then as long
  152. * as that task remains queued, the current grace period
  153. * cannot end. Note that there is some uncertainty as
  154. * to exactly when the current grace period started.
  155. * We take a conservative approach, which can result
  156. * in unnecessarily waiting on tasks that started very
  157. * slightly after the current grace period began. C'est
  158. * la vie!!!
  159. *
  160. * But first, note that the current CPU must still be
  161. * on line!
  162. */
  163. WARN_ON_ONCE((rdp->grpmask & rnp->qsmaskinit) == 0);
  164. WARN_ON_ONCE(!list_empty(&t->rcu_node_entry));
  165. if ((rnp->qsmask & rdp->grpmask) && rnp->gp_tasks != NULL) {
  166. list_add(&t->rcu_node_entry, rnp->gp_tasks->prev);
  167. rnp->gp_tasks = &t->rcu_node_entry;
  168. #ifdef CONFIG_RCU_BOOST
  169. if (rnp->boost_tasks != NULL)
  170. rnp->boost_tasks = rnp->gp_tasks;
  171. #endif /* #ifdef CONFIG_RCU_BOOST */
  172. } else {
  173. list_add(&t->rcu_node_entry, &rnp->blkd_tasks);
  174. if (rnp->qsmask & rdp->grpmask)
  175. rnp->gp_tasks = &t->rcu_node_entry;
  176. }
  177. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  178. }
  179. /*
  180. * Either we were not in an RCU read-side critical section to
  181. * begin with, or we have now recorded that critical section
  182. * globally. Either way, we can now note a quiescent state
  183. * for this CPU. Again, if we were in an RCU read-side critical
  184. * section, and if that critical section was blocking the current
  185. * grace period, then the fact that the task has been enqueued
  186. * means that we continue to block the current grace period.
  187. */
  188. local_irq_save(flags);
  189. rcu_preempt_qs(cpu);
  190. local_irq_restore(flags);
  191. }
  192. /*
  193. * Tree-preemptible RCU implementation for rcu_read_lock().
  194. * Just increment ->rcu_read_lock_nesting, shared state will be updated
  195. * if we block.
  196. */
  197. void __rcu_read_lock(void)
  198. {
  199. current->rcu_read_lock_nesting++;
  200. barrier(); /* needed if we ever invoke rcu_read_lock in rcutree.c */
  201. }
  202. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  203. /*
  204. * Check for preempted RCU readers blocking the current grace period
  205. * for the specified rcu_node structure. If the caller needs a reliable
  206. * answer, it must hold the rcu_node's ->lock.
  207. */
  208. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  209. {
  210. return rnp->gp_tasks != NULL;
  211. }
  212. /*
  213. * Record a quiescent state for all tasks that were previously queued
  214. * on the specified rcu_node structure and that were blocking the current
  215. * RCU grace period. The caller must hold the specified rnp->lock with
  216. * irqs disabled, and this lock is released upon return, but irqs remain
  217. * disabled.
  218. */
  219. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  220. __releases(rnp->lock)
  221. {
  222. unsigned long mask;
  223. struct rcu_node *rnp_p;
  224. if (rnp->qsmask != 0 || rcu_preempt_blocked_readers_cgp(rnp)) {
  225. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  226. return; /* Still need more quiescent states! */
  227. }
  228. rnp_p = rnp->parent;
  229. if (rnp_p == NULL) {
  230. /*
  231. * Either there is only one rcu_node in the tree,
  232. * or tasks were kicked up to root rcu_node due to
  233. * CPUs going offline.
  234. */
  235. rcu_report_qs_rsp(&rcu_preempt_state, flags);
  236. return;
  237. }
  238. /* Report up the rest of the hierarchy. */
  239. mask = rnp->grpmask;
  240. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  241. raw_spin_lock(&rnp_p->lock); /* irqs already disabled. */
  242. rcu_report_qs_rnp(mask, &rcu_preempt_state, rnp_p, flags);
  243. }
  244. /*
  245. * Advance a ->blkd_tasks-list pointer to the next entry, instead
  246. * returning NULL if at the end of the list.
  247. */
  248. static struct list_head *rcu_next_node_entry(struct task_struct *t,
  249. struct rcu_node *rnp)
  250. {
  251. struct list_head *np;
  252. np = t->rcu_node_entry.next;
  253. if (np == &rnp->blkd_tasks)
  254. np = NULL;
  255. return np;
  256. }
  257. /*
  258. * Handle special cases during rcu_read_unlock(), such as needing to
  259. * notify RCU core processing or task having blocked during the RCU
  260. * read-side critical section.
  261. */
  262. static void rcu_read_unlock_special(struct task_struct *t)
  263. {
  264. int empty;
  265. int empty_exp;
  266. unsigned long flags;
  267. struct list_head *np;
  268. struct rcu_node *rnp;
  269. int special;
  270. /* NMI handlers cannot block and cannot safely manipulate state. */
  271. if (in_nmi())
  272. return;
  273. local_irq_save(flags);
  274. /*
  275. * If RCU core is waiting for this CPU to exit critical section,
  276. * let it know that we have done so.
  277. */
  278. special = t->rcu_read_unlock_special;
  279. if (special & RCU_READ_UNLOCK_NEED_QS) {
  280. rcu_preempt_qs(smp_processor_id());
  281. }
  282. /* Hardware IRQ handlers cannot block. */
  283. if (in_irq()) {
  284. local_irq_restore(flags);
  285. return;
  286. }
  287. /* Clean up if blocked during RCU read-side critical section. */
  288. if (special & RCU_READ_UNLOCK_BLOCKED) {
  289. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BLOCKED;
  290. /*
  291. * Remove this task from the list it blocked on. The
  292. * task can migrate while we acquire the lock, but at
  293. * most one time. So at most two passes through loop.
  294. */
  295. for (;;) {
  296. rnp = t->rcu_blocked_node;
  297. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  298. if (rnp == t->rcu_blocked_node)
  299. break;
  300. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  301. }
  302. empty = !rcu_preempt_blocked_readers_cgp(rnp);
  303. empty_exp = !rcu_preempted_readers_exp(rnp);
  304. smp_mb(); /* ensure expedited fastpath sees end of RCU c-s. */
  305. np = rcu_next_node_entry(t, rnp);
  306. list_del_init(&t->rcu_node_entry);
  307. if (&t->rcu_node_entry == rnp->gp_tasks)
  308. rnp->gp_tasks = np;
  309. if (&t->rcu_node_entry == rnp->exp_tasks)
  310. rnp->exp_tasks = np;
  311. #ifdef CONFIG_RCU_BOOST
  312. if (&t->rcu_node_entry == rnp->boost_tasks)
  313. rnp->boost_tasks = np;
  314. #endif /* #ifdef CONFIG_RCU_BOOST */
  315. t->rcu_blocked_node = NULL;
  316. /*
  317. * If this was the last task on the current list, and if
  318. * we aren't waiting on any CPUs, report the quiescent state.
  319. * Note that rcu_report_unblock_qs_rnp() releases rnp->lock.
  320. */
  321. if (empty)
  322. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  323. else
  324. rcu_report_unblock_qs_rnp(rnp, flags);
  325. #ifdef CONFIG_RCU_BOOST
  326. /* Unboost if we were boosted. */
  327. if (special & RCU_READ_UNLOCK_BOOSTED) {
  328. t->rcu_read_unlock_special &= ~RCU_READ_UNLOCK_BOOSTED;
  329. rt_mutex_unlock(t->rcu_boost_mutex);
  330. t->rcu_boost_mutex = NULL;
  331. }
  332. #endif /* #ifdef CONFIG_RCU_BOOST */
  333. /*
  334. * If this was the last task on the expedited lists,
  335. * then we need to report up the rcu_node hierarchy.
  336. */
  337. if (!empty_exp && !rcu_preempted_readers_exp(rnp))
  338. rcu_report_exp_rnp(&rcu_preempt_state, rnp);
  339. } else {
  340. local_irq_restore(flags);
  341. }
  342. }
  343. /*
  344. * Tree-preemptible RCU implementation for rcu_read_unlock().
  345. * Decrement ->rcu_read_lock_nesting. If the result is zero (outermost
  346. * rcu_read_unlock()) and ->rcu_read_unlock_special is non-zero, then
  347. * invoke rcu_read_unlock_special() to clean up after a context switch
  348. * in an RCU read-side critical section and other special cases.
  349. */
  350. void __rcu_read_unlock(void)
  351. {
  352. struct task_struct *t = current;
  353. barrier(); /* needed if we ever invoke rcu_read_unlock in rcutree.c */
  354. --t->rcu_read_lock_nesting;
  355. barrier(); /* decrement before load of ->rcu_read_unlock_special */
  356. if (t->rcu_read_lock_nesting == 0 &&
  357. unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
  358. rcu_read_unlock_special(t);
  359. #ifdef CONFIG_PROVE_LOCKING
  360. WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
  361. #endif /* #ifdef CONFIG_PROVE_LOCKING */
  362. }
  363. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  364. #ifdef CONFIG_RCU_CPU_STALL_VERBOSE
  365. /*
  366. * Dump detailed information for all tasks blocking the current RCU
  367. * grace period on the specified rcu_node structure.
  368. */
  369. static void rcu_print_detail_task_stall_rnp(struct rcu_node *rnp)
  370. {
  371. unsigned long flags;
  372. struct task_struct *t;
  373. if (!rcu_preempt_blocked_readers_cgp(rnp))
  374. return;
  375. raw_spin_lock_irqsave(&rnp->lock, flags);
  376. t = list_entry(rnp->gp_tasks,
  377. struct task_struct, rcu_node_entry);
  378. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  379. sched_show_task(t);
  380. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  381. }
  382. /*
  383. * Dump detailed information for all tasks blocking the current RCU
  384. * grace period.
  385. */
  386. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  387. {
  388. struct rcu_node *rnp = rcu_get_root(rsp);
  389. rcu_print_detail_task_stall_rnp(rnp);
  390. rcu_for_each_leaf_node(rsp, rnp)
  391. rcu_print_detail_task_stall_rnp(rnp);
  392. }
  393. #else /* #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  394. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  395. {
  396. }
  397. #endif /* #else #ifdef CONFIG_RCU_CPU_STALL_VERBOSE */
  398. /*
  399. * Scan the current list of tasks blocked within RCU read-side critical
  400. * sections, printing out the tid of each.
  401. */
  402. static void rcu_print_task_stall(struct rcu_node *rnp)
  403. {
  404. struct task_struct *t;
  405. if (!rcu_preempt_blocked_readers_cgp(rnp))
  406. return;
  407. t = list_entry(rnp->gp_tasks,
  408. struct task_struct, rcu_node_entry);
  409. list_for_each_entry_continue(t, &rnp->blkd_tasks, rcu_node_entry)
  410. printk(" P%d", t->pid);
  411. }
  412. /*
  413. * Suppress preemptible RCU's CPU stall warnings by pushing the
  414. * time of the next stall-warning message comfortably far into the
  415. * future.
  416. */
  417. static void rcu_preempt_stall_reset(void)
  418. {
  419. rcu_preempt_state.jiffies_stall = jiffies + ULONG_MAX / 2;
  420. }
  421. /*
  422. * Check that the list of blocked tasks for the newly completed grace
  423. * period is in fact empty. It is a serious bug to complete a grace
  424. * period that still has RCU readers blocked! This function must be
  425. * invoked -before- updating this rnp's ->gpnum, and the rnp's ->lock
  426. * must be held by the caller.
  427. *
  428. * Also, if there are blocked tasks on the list, they automatically
  429. * block the newly created grace period, so set up ->gp_tasks accordingly.
  430. */
  431. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  432. {
  433. WARN_ON_ONCE(rcu_preempt_blocked_readers_cgp(rnp));
  434. if (!list_empty(&rnp->blkd_tasks))
  435. rnp->gp_tasks = rnp->blkd_tasks.next;
  436. WARN_ON_ONCE(rnp->qsmask);
  437. }
  438. #ifdef CONFIG_HOTPLUG_CPU
  439. /*
  440. * Handle tasklist migration for case in which all CPUs covered by the
  441. * specified rcu_node have gone offline. Move them up to the root
  442. * rcu_node. The reason for not just moving them to the immediate
  443. * parent is to remove the need for rcu_read_unlock_special() to
  444. * make more than two attempts to acquire the target rcu_node's lock.
  445. * Returns true if there were tasks blocking the current RCU grace
  446. * period.
  447. *
  448. * Returns 1 if there was previously a task blocking the current grace
  449. * period on the specified rcu_node structure.
  450. *
  451. * The caller must hold rnp->lock with irqs disabled.
  452. */
  453. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  454. struct rcu_node *rnp,
  455. struct rcu_data *rdp)
  456. {
  457. struct list_head *lp;
  458. struct list_head *lp_root;
  459. int retval = 0;
  460. struct rcu_node *rnp_root = rcu_get_root(rsp);
  461. struct task_struct *t;
  462. if (rnp == rnp_root) {
  463. WARN_ONCE(1, "Last CPU thought to be offlined?");
  464. return 0; /* Shouldn't happen: at least one CPU online. */
  465. }
  466. /* If we are on an internal node, complain bitterly. */
  467. WARN_ON_ONCE(rnp != rdp->mynode);
  468. /*
  469. * Move tasks up to root rcu_node. Don't try to get fancy for
  470. * this corner-case operation -- just put this node's tasks
  471. * at the head of the root node's list, and update the root node's
  472. * ->gp_tasks and ->exp_tasks pointers to those of this node's,
  473. * if non-NULL. This might result in waiting for more tasks than
  474. * absolutely necessary, but this is a good performance/complexity
  475. * tradeoff.
  476. */
  477. if (rcu_preempt_blocked_readers_cgp(rnp))
  478. retval |= RCU_OFL_TASKS_NORM_GP;
  479. if (rcu_preempted_readers_exp(rnp))
  480. retval |= RCU_OFL_TASKS_EXP_GP;
  481. lp = &rnp->blkd_tasks;
  482. lp_root = &rnp_root->blkd_tasks;
  483. while (!list_empty(lp)) {
  484. t = list_entry(lp->next, typeof(*t), rcu_node_entry);
  485. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  486. list_del(&t->rcu_node_entry);
  487. t->rcu_blocked_node = rnp_root;
  488. list_add(&t->rcu_node_entry, lp_root);
  489. if (&t->rcu_node_entry == rnp->gp_tasks)
  490. rnp_root->gp_tasks = rnp->gp_tasks;
  491. if (&t->rcu_node_entry == rnp->exp_tasks)
  492. rnp_root->exp_tasks = rnp->exp_tasks;
  493. #ifdef CONFIG_RCU_BOOST
  494. if (&t->rcu_node_entry == rnp->boost_tasks)
  495. rnp_root->boost_tasks = rnp->boost_tasks;
  496. #endif /* #ifdef CONFIG_RCU_BOOST */
  497. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  498. }
  499. #ifdef CONFIG_RCU_BOOST
  500. /* In case root is being boosted and leaf is not. */
  501. raw_spin_lock(&rnp_root->lock); /* irqs already disabled */
  502. if (rnp_root->boost_tasks != NULL &&
  503. rnp_root->boost_tasks != rnp_root->gp_tasks)
  504. rnp_root->boost_tasks = rnp_root->gp_tasks;
  505. raw_spin_unlock(&rnp_root->lock); /* irqs still disabled */
  506. #endif /* #ifdef CONFIG_RCU_BOOST */
  507. rnp->gp_tasks = NULL;
  508. rnp->exp_tasks = NULL;
  509. return retval;
  510. }
  511. /*
  512. * Do CPU-offline processing for preemptible RCU.
  513. */
  514. static void rcu_preempt_offline_cpu(int cpu)
  515. {
  516. __rcu_offline_cpu(cpu, &rcu_preempt_state);
  517. }
  518. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  519. /*
  520. * Check for a quiescent state from the current CPU. When a task blocks,
  521. * the task is recorded in the corresponding CPU's rcu_node structure,
  522. * which is checked elsewhere.
  523. *
  524. * Caller must disable hard irqs.
  525. */
  526. static void rcu_preempt_check_callbacks(int cpu)
  527. {
  528. struct task_struct *t = current;
  529. if (t->rcu_read_lock_nesting == 0) {
  530. rcu_preempt_qs(cpu);
  531. return;
  532. }
  533. if (per_cpu(rcu_preempt_data, cpu).qs_pending)
  534. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_NEED_QS;
  535. }
  536. /*
  537. * Process callbacks for preemptible RCU.
  538. */
  539. static void rcu_preempt_process_callbacks(void)
  540. {
  541. __rcu_process_callbacks(&rcu_preempt_state,
  542. &__get_cpu_var(rcu_preempt_data));
  543. }
  544. /*
  545. * Queue a preemptible-RCU callback for invocation after a grace period.
  546. */
  547. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  548. {
  549. __call_rcu(head, func, &rcu_preempt_state);
  550. }
  551. EXPORT_SYMBOL_GPL(call_rcu);
  552. /**
  553. * synchronize_rcu - wait until a grace period has elapsed.
  554. *
  555. * Control will return to the caller some time after a full grace
  556. * period has elapsed, in other words after all currently executing RCU
  557. * read-side critical sections have completed. Note, however, that
  558. * upon return from synchronize_rcu(), the caller might well be executing
  559. * concurrently with new RCU read-side critical sections that began while
  560. * synchronize_rcu() was waiting. RCU read-side critical sections are
  561. * delimited by rcu_read_lock() and rcu_read_unlock(), and may be nested.
  562. */
  563. void synchronize_rcu(void)
  564. {
  565. struct rcu_synchronize rcu;
  566. if (!rcu_scheduler_active)
  567. return;
  568. init_rcu_head_on_stack(&rcu.head);
  569. init_completion(&rcu.completion);
  570. /* Will wake me after RCU finished. */
  571. call_rcu(&rcu.head, wakeme_after_rcu);
  572. /* Wait for it. */
  573. wait_for_completion(&rcu.completion);
  574. destroy_rcu_head_on_stack(&rcu.head);
  575. }
  576. EXPORT_SYMBOL_GPL(synchronize_rcu);
  577. static DECLARE_WAIT_QUEUE_HEAD(sync_rcu_preempt_exp_wq);
  578. static long sync_rcu_preempt_exp_count;
  579. static DEFINE_MUTEX(sync_rcu_preempt_exp_mutex);
  580. /*
  581. * Return non-zero if there are any tasks in RCU read-side critical
  582. * sections blocking the current preemptible-RCU expedited grace period.
  583. * If there is no preemptible-RCU expedited grace period currently in
  584. * progress, returns zero unconditionally.
  585. */
  586. static int rcu_preempted_readers_exp(struct rcu_node *rnp)
  587. {
  588. return rnp->exp_tasks != NULL;
  589. }
  590. /*
  591. * return non-zero if there is no RCU expedited grace period in progress
  592. * for the specified rcu_node structure, in other words, if all CPUs and
  593. * tasks covered by the specified rcu_node structure have done their bit
  594. * for the current expedited grace period. Works only for preemptible
  595. * RCU -- other RCU implementation use other means.
  596. *
  597. * Caller must hold sync_rcu_preempt_exp_mutex.
  598. */
  599. static int sync_rcu_preempt_exp_done(struct rcu_node *rnp)
  600. {
  601. return !rcu_preempted_readers_exp(rnp) &&
  602. ACCESS_ONCE(rnp->expmask) == 0;
  603. }
  604. /*
  605. * Report the exit from RCU read-side critical section for the last task
  606. * that queued itself during or before the current expedited preemptible-RCU
  607. * grace period. This event is reported either to the rcu_node structure on
  608. * which the task was queued or to one of that rcu_node structure's ancestors,
  609. * recursively up the tree. (Calm down, calm down, we do the recursion
  610. * iteratively!)
  611. *
  612. * Caller must hold sync_rcu_preempt_exp_mutex.
  613. */
  614. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  615. {
  616. unsigned long flags;
  617. unsigned long mask;
  618. raw_spin_lock_irqsave(&rnp->lock, flags);
  619. for (;;) {
  620. if (!sync_rcu_preempt_exp_done(rnp))
  621. break;
  622. if (rnp->parent == NULL) {
  623. wake_up(&sync_rcu_preempt_exp_wq);
  624. break;
  625. }
  626. mask = rnp->grpmask;
  627. raw_spin_unlock(&rnp->lock); /* irqs remain disabled */
  628. rnp = rnp->parent;
  629. raw_spin_lock(&rnp->lock); /* irqs already disabled */
  630. rnp->expmask &= ~mask;
  631. }
  632. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  633. }
  634. /*
  635. * Snapshot the tasks blocking the newly started preemptible-RCU expedited
  636. * grace period for the specified rcu_node structure. If there are no such
  637. * tasks, report it up the rcu_node hierarchy.
  638. *
  639. * Caller must hold sync_rcu_preempt_exp_mutex and rsp->onofflock.
  640. */
  641. static void
  642. sync_rcu_preempt_exp_init(struct rcu_state *rsp, struct rcu_node *rnp)
  643. {
  644. unsigned long flags;
  645. int must_wait = 0;
  646. raw_spin_lock_irqsave(&rnp->lock, flags);
  647. if (list_empty(&rnp->blkd_tasks))
  648. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  649. else {
  650. rnp->exp_tasks = rnp->blkd_tasks.next;
  651. rcu_initiate_boost(rnp, flags); /* releases rnp->lock */
  652. must_wait = 1;
  653. }
  654. if (!must_wait)
  655. rcu_report_exp_rnp(rsp, rnp);
  656. }
  657. /*
  658. * Wait for an rcu-preempt grace period, but expedite it. The basic idea
  659. * is to invoke synchronize_sched_expedited() to push all the tasks to
  660. * the ->blkd_tasks lists and wait for this list to drain.
  661. */
  662. void synchronize_rcu_expedited(void)
  663. {
  664. unsigned long flags;
  665. struct rcu_node *rnp;
  666. struct rcu_state *rsp = &rcu_preempt_state;
  667. long snap;
  668. int trycount = 0;
  669. smp_mb(); /* Caller's modifications seen first by other CPUs. */
  670. snap = ACCESS_ONCE(sync_rcu_preempt_exp_count) + 1;
  671. smp_mb(); /* Above access cannot bleed into critical section. */
  672. /*
  673. * Acquire lock, falling back to synchronize_rcu() if too many
  674. * lock-acquisition failures. Of course, if someone does the
  675. * expedited grace period for us, just leave.
  676. */
  677. while (!mutex_trylock(&sync_rcu_preempt_exp_mutex)) {
  678. if (trycount++ < 10)
  679. udelay(trycount * num_online_cpus());
  680. else {
  681. synchronize_rcu();
  682. return;
  683. }
  684. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  685. goto mb_ret; /* Others did our work for us. */
  686. }
  687. if ((ACCESS_ONCE(sync_rcu_preempt_exp_count) - snap) > 0)
  688. goto unlock_mb_ret; /* Others did our work for us. */
  689. /* force all RCU readers onto ->blkd_tasks lists. */
  690. synchronize_sched_expedited();
  691. raw_spin_lock_irqsave(&rsp->onofflock, flags);
  692. /* Initialize ->expmask for all non-leaf rcu_node structures. */
  693. rcu_for_each_nonleaf_node_breadth_first(rsp, rnp) {
  694. raw_spin_lock(&rnp->lock); /* irqs already disabled. */
  695. rnp->expmask = rnp->qsmaskinit;
  696. raw_spin_unlock(&rnp->lock); /* irqs remain disabled. */
  697. }
  698. /* Snapshot current state of ->blkd_tasks lists. */
  699. rcu_for_each_leaf_node(rsp, rnp)
  700. sync_rcu_preempt_exp_init(rsp, rnp);
  701. if (NUM_RCU_NODES > 1)
  702. sync_rcu_preempt_exp_init(rsp, rcu_get_root(rsp));
  703. raw_spin_unlock_irqrestore(&rsp->onofflock, flags);
  704. /* Wait for snapshotted ->blkd_tasks lists to drain. */
  705. rnp = rcu_get_root(rsp);
  706. wait_event(sync_rcu_preempt_exp_wq,
  707. sync_rcu_preempt_exp_done(rnp));
  708. /* Clean up and exit. */
  709. smp_mb(); /* ensure expedited GP seen before counter increment. */
  710. ACCESS_ONCE(sync_rcu_preempt_exp_count)++;
  711. unlock_mb_ret:
  712. mutex_unlock(&sync_rcu_preempt_exp_mutex);
  713. mb_ret:
  714. smp_mb(); /* ensure subsequent action seen after grace period. */
  715. }
  716. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  717. /*
  718. * Check to see if there is any immediate preemptible-RCU-related work
  719. * to be done.
  720. */
  721. static int rcu_preempt_pending(int cpu)
  722. {
  723. return __rcu_pending(&rcu_preempt_state,
  724. &per_cpu(rcu_preempt_data, cpu));
  725. }
  726. /*
  727. * Does preemptible RCU need the CPU to stay out of dynticks mode?
  728. */
  729. static int rcu_preempt_needs_cpu(int cpu)
  730. {
  731. return !!per_cpu(rcu_preempt_data, cpu).nxtlist;
  732. }
  733. /**
  734. * rcu_barrier - Wait until all in-flight call_rcu() callbacks complete.
  735. */
  736. void rcu_barrier(void)
  737. {
  738. _rcu_barrier(&rcu_preempt_state, call_rcu);
  739. }
  740. EXPORT_SYMBOL_GPL(rcu_barrier);
  741. /*
  742. * Initialize preemptible RCU's per-CPU data.
  743. */
  744. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  745. {
  746. rcu_init_percpu_data(cpu, &rcu_preempt_state, 1);
  747. }
  748. /*
  749. * Move preemptible RCU's callbacks from dying CPU to other online CPU.
  750. */
  751. static void rcu_preempt_send_cbs_to_online(void)
  752. {
  753. rcu_send_cbs_to_online(&rcu_preempt_state);
  754. }
  755. /*
  756. * Initialize preemptible RCU's state structures.
  757. */
  758. static void __init __rcu_init_preempt(void)
  759. {
  760. rcu_init_one(&rcu_preempt_state, &rcu_preempt_data);
  761. }
  762. /*
  763. * Check for a task exiting while in a preemptible-RCU read-side
  764. * critical section, clean up if so. No need to issue warnings,
  765. * as debug_check_no_locks_held() already does this if lockdep
  766. * is enabled.
  767. */
  768. void exit_rcu(void)
  769. {
  770. struct task_struct *t = current;
  771. if (t->rcu_read_lock_nesting == 0)
  772. return;
  773. t->rcu_read_lock_nesting = 1;
  774. __rcu_read_unlock();
  775. }
  776. #else /* #ifdef CONFIG_TREE_PREEMPT_RCU */
  777. static struct rcu_state *rcu_state = &rcu_sched_state;
  778. /*
  779. * Tell them what RCU they are running.
  780. */
  781. static void __init rcu_bootup_announce(void)
  782. {
  783. printk(KERN_INFO "Hierarchical RCU implementation.\n");
  784. rcu_bootup_announce_oddness();
  785. }
  786. /*
  787. * Return the number of RCU batches processed thus far for debug & stats.
  788. */
  789. long rcu_batches_completed(void)
  790. {
  791. return rcu_batches_completed_sched();
  792. }
  793. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  794. /*
  795. * Force a quiescent state for RCU, which, because there is no preemptible
  796. * RCU, becomes the same as rcu-sched.
  797. */
  798. void rcu_force_quiescent_state(void)
  799. {
  800. rcu_sched_force_quiescent_state();
  801. }
  802. EXPORT_SYMBOL_GPL(rcu_force_quiescent_state);
  803. /*
  804. * Because preemptible RCU does not exist, we never have to check for
  805. * CPUs being in quiescent states.
  806. */
  807. static void rcu_preempt_note_context_switch(int cpu)
  808. {
  809. }
  810. /*
  811. * Because preemptible RCU does not exist, there are never any preempted
  812. * RCU readers.
  813. */
  814. static int rcu_preempt_blocked_readers_cgp(struct rcu_node *rnp)
  815. {
  816. return 0;
  817. }
  818. #ifdef CONFIG_HOTPLUG_CPU
  819. /* Because preemptible RCU does not exist, no quieting of tasks. */
  820. static void rcu_report_unblock_qs_rnp(struct rcu_node *rnp, unsigned long flags)
  821. {
  822. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  823. }
  824. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  825. /*
  826. * Because preemptible RCU does not exist, we never have to check for
  827. * tasks blocked within RCU read-side critical sections.
  828. */
  829. static void rcu_print_detail_task_stall(struct rcu_state *rsp)
  830. {
  831. }
  832. /*
  833. * Because preemptible RCU does not exist, we never have to check for
  834. * tasks blocked within RCU read-side critical sections.
  835. */
  836. static void rcu_print_task_stall(struct rcu_node *rnp)
  837. {
  838. }
  839. /*
  840. * Because preemptible RCU does not exist, there is no need to suppress
  841. * its CPU stall warnings.
  842. */
  843. static void rcu_preempt_stall_reset(void)
  844. {
  845. }
  846. /*
  847. * Because there is no preemptible RCU, there can be no readers blocked,
  848. * so there is no need to check for blocked tasks. So check only for
  849. * bogus qsmask values.
  850. */
  851. static void rcu_preempt_check_blocked_tasks(struct rcu_node *rnp)
  852. {
  853. WARN_ON_ONCE(rnp->qsmask);
  854. }
  855. #ifdef CONFIG_HOTPLUG_CPU
  856. /*
  857. * Because preemptible RCU does not exist, it never needs to migrate
  858. * tasks that were blocked within RCU read-side critical sections, and
  859. * such non-existent tasks cannot possibly have been blocking the current
  860. * grace period.
  861. */
  862. static int rcu_preempt_offline_tasks(struct rcu_state *rsp,
  863. struct rcu_node *rnp,
  864. struct rcu_data *rdp)
  865. {
  866. return 0;
  867. }
  868. /*
  869. * Because preemptible RCU does not exist, it never needs CPU-offline
  870. * processing.
  871. */
  872. static void rcu_preempt_offline_cpu(int cpu)
  873. {
  874. }
  875. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  876. /*
  877. * Because preemptible RCU does not exist, it never has any callbacks
  878. * to check.
  879. */
  880. static void rcu_preempt_check_callbacks(int cpu)
  881. {
  882. }
  883. /*
  884. * Because preemptible RCU does not exist, it never has any callbacks
  885. * to process.
  886. */
  887. static void rcu_preempt_process_callbacks(void)
  888. {
  889. }
  890. /*
  891. * Wait for an rcu-preempt grace period, but make it happen quickly.
  892. * But because preemptible RCU does not exist, map to rcu-sched.
  893. */
  894. void synchronize_rcu_expedited(void)
  895. {
  896. synchronize_sched_expedited();
  897. }
  898. EXPORT_SYMBOL_GPL(synchronize_rcu_expedited);
  899. #ifdef CONFIG_HOTPLUG_CPU
  900. /*
  901. * Because preemptible RCU does not exist, there is never any need to
  902. * report on tasks preempted in RCU read-side critical sections during
  903. * expedited RCU grace periods.
  904. */
  905. static void rcu_report_exp_rnp(struct rcu_state *rsp, struct rcu_node *rnp)
  906. {
  907. return;
  908. }
  909. #endif /* #ifdef CONFIG_HOTPLUG_CPU */
  910. /*
  911. * Because preemptible RCU does not exist, it never has any work to do.
  912. */
  913. static int rcu_preempt_pending(int cpu)
  914. {
  915. return 0;
  916. }
  917. /*
  918. * Because preemptible RCU does not exist, it never needs any CPU.
  919. */
  920. static int rcu_preempt_needs_cpu(int cpu)
  921. {
  922. return 0;
  923. }
  924. /*
  925. * Because preemptible RCU does not exist, rcu_barrier() is just
  926. * another name for rcu_barrier_sched().
  927. */
  928. void rcu_barrier(void)
  929. {
  930. rcu_barrier_sched();
  931. }
  932. EXPORT_SYMBOL_GPL(rcu_barrier);
  933. /*
  934. * Because preemptible RCU does not exist, there is no per-CPU
  935. * data to initialize.
  936. */
  937. static void __cpuinit rcu_preempt_init_percpu_data(int cpu)
  938. {
  939. }
  940. /*
  941. * Because there is no preemptible RCU, there are no callbacks to move.
  942. */
  943. static void rcu_preempt_send_cbs_to_online(void)
  944. {
  945. }
  946. /*
  947. * Because preemptible RCU does not exist, it need not be initialized.
  948. */
  949. static void __init __rcu_init_preempt(void)
  950. {
  951. }
  952. #endif /* #else #ifdef CONFIG_TREE_PREEMPT_RCU */
  953. #ifdef CONFIG_RCU_BOOST
  954. #include "rtmutex_common.h"
  955. #ifdef CONFIG_RCU_TRACE
  956. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  957. {
  958. if (list_empty(&rnp->blkd_tasks))
  959. rnp->n_balk_blkd_tasks++;
  960. else if (rnp->exp_tasks == NULL && rnp->gp_tasks == NULL)
  961. rnp->n_balk_exp_gp_tasks++;
  962. else if (rnp->gp_tasks != NULL && rnp->boost_tasks != NULL)
  963. rnp->n_balk_boost_tasks++;
  964. else if (rnp->gp_tasks != NULL && rnp->qsmask != 0)
  965. rnp->n_balk_notblocked++;
  966. else if (rnp->gp_tasks != NULL &&
  967. ULONG_CMP_LT(jiffies, rnp->boost_time))
  968. rnp->n_balk_notyet++;
  969. else
  970. rnp->n_balk_nos++;
  971. }
  972. #else /* #ifdef CONFIG_RCU_TRACE */
  973. static void rcu_initiate_boost_trace(struct rcu_node *rnp)
  974. {
  975. }
  976. #endif /* #else #ifdef CONFIG_RCU_TRACE */
  977. /*
  978. * Carry out RCU priority boosting on the task indicated by ->exp_tasks
  979. * or ->boost_tasks, advancing the pointer to the next task in the
  980. * ->blkd_tasks list.
  981. *
  982. * Note that irqs must be enabled: boosting the task can block.
  983. * Returns 1 if there are more tasks needing to be boosted.
  984. */
  985. static int rcu_boost(struct rcu_node *rnp)
  986. {
  987. unsigned long flags;
  988. struct rt_mutex mtx;
  989. struct task_struct *t;
  990. struct list_head *tb;
  991. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL)
  992. return 0; /* Nothing left to boost. */
  993. raw_spin_lock_irqsave(&rnp->lock, flags);
  994. /*
  995. * Recheck under the lock: all tasks in need of boosting
  996. * might exit their RCU read-side critical sections on their own.
  997. */
  998. if (rnp->exp_tasks == NULL && rnp->boost_tasks == NULL) {
  999. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1000. return 0;
  1001. }
  1002. /*
  1003. * Preferentially boost tasks blocking expedited grace periods.
  1004. * This cannot starve the normal grace periods because a second
  1005. * expedited grace period must boost all blocked tasks, including
  1006. * those blocking the pre-existing normal grace period.
  1007. */
  1008. if (rnp->exp_tasks != NULL) {
  1009. tb = rnp->exp_tasks;
  1010. rnp->n_exp_boosts++;
  1011. } else {
  1012. tb = rnp->boost_tasks;
  1013. rnp->n_normal_boosts++;
  1014. }
  1015. rnp->n_tasks_boosted++;
  1016. /*
  1017. * We boost task t by manufacturing an rt_mutex that appears to
  1018. * be held by task t. We leave a pointer to that rt_mutex where
  1019. * task t can find it, and task t will release the mutex when it
  1020. * exits its outermost RCU read-side critical section. Then
  1021. * simply acquiring this artificial rt_mutex will boost task
  1022. * t's priority. (Thanks to tglx for suggesting this approach!)
  1023. *
  1024. * Note that task t must acquire rnp->lock to remove itself from
  1025. * the ->blkd_tasks list, which it will do from exit() if from
  1026. * nowhere else. We therefore are guaranteed that task t will
  1027. * stay around at least until we drop rnp->lock. Note that
  1028. * rnp->lock also resolves races between our priority boosting
  1029. * and task t's exiting its outermost RCU read-side critical
  1030. * section.
  1031. */
  1032. t = container_of(tb, struct task_struct, rcu_node_entry);
  1033. rt_mutex_init_proxy_locked(&mtx, t);
  1034. t->rcu_boost_mutex = &mtx;
  1035. t->rcu_read_unlock_special |= RCU_READ_UNLOCK_BOOSTED;
  1036. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1037. rt_mutex_lock(&mtx); /* Side effect: boosts task t's priority. */
  1038. rt_mutex_unlock(&mtx); /* Keep lockdep happy. */
  1039. return rnp->exp_tasks != NULL || rnp->boost_tasks != NULL;
  1040. }
  1041. /*
  1042. * Timer handler to initiate waking up of boost kthreads that
  1043. * have yielded the CPU due to excessive numbers of tasks to
  1044. * boost. We wake up the per-rcu_node kthread, which in turn
  1045. * will wake up the booster kthread.
  1046. */
  1047. static void rcu_boost_kthread_timer(unsigned long arg)
  1048. {
  1049. invoke_rcu_node_kthread((struct rcu_node *)arg);
  1050. }
  1051. /*
  1052. * Priority-boosting kthread. One per leaf rcu_node and one for the
  1053. * root rcu_node.
  1054. */
  1055. static int rcu_boost_kthread(void *arg)
  1056. {
  1057. struct rcu_node *rnp = (struct rcu_node *)arg;
  1058. int spincnt = 0;
  1059. int more2boost;
  1060. for (;;) {
  1061. rnp->boost_kthread_status = RCU_KTHREAD_WAITING;
  1062. rcu_wait(rnp->boost_tasks || rnp->exp_tasks);
  1063. rnp->boost_kthread_status = RCU_KTHREAD_RUNNING;
  1064. more2boost = rcu_boost(rnp);
  1065. if (more2boost)
  1066. spincnt++;
  1067. else
  1068. spincnt = 0;
  1069. if (spincnt > 10) {
  1070. rcu_yield(rcu_boost_kthread_timer, (unsigned long)rnp);
  1071. spincnt = 0;
  1072. }
  1073. }
  1074. /* NOTREACHED */
  1075. return 0;
  1076. }
  1077. /*
  1078. * Check to see if it is time to start boosting RCU readers that are
  1079. * blocking the current grace period, and, if so, tell the per-rcu_node
  1080. * kthread to start boosting them. If there is an expedited grace
  1081. * period in progress, it is always time to boost.
  1082. *
  1083. * The caller must hold rnp->lock, which this function releases,
  1084. * but irqs remain disabled. The ->boost_kthread_task is immortal,
  1085. * so we don't need to worry about it going away.
  1086. */
  1087. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1088. {
  1089. struct task_struct *t;
  1090. if (!rcu_preempt_blocked_readers_cgp(rnp) && rnp->exp_tasks == NULL) {
  1091. rnp->n_balk_exp_gp_tasks++;
  1092. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1093. return;
  1094. }
  1095. if (rnp->exp_tasks != NULL ||
  1096. (rnp->gp_tasks != NULL &&
  1097. rnp->boost_tasks == NULL &&
  1098. rnp->qsmask == 0 &&
  1099. ULONG_CMP_GE(jiffies, rnp->boost_time))) {
  1100. if (rnp->exp_tasks == NULL)
  1101. rnp->boost_tasks = rnp->gp_tasks;
  1102. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1103. t = rnp->boost_kthread_task;
  1104. if (t != NULL)
  1105. wake_up_process(t);
  1106. } else {
  1107. rcu_initiate_boost_trace(rnp);
  1108. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1109. }
  1110. }
  1111. /*
  1112. * Set the affinity of the boost kthread. The CPU-hotplug locks are
  1113. * held, so no one should be messing with the existence of the boost
  1114. * kthread.
  1115. */
  1116. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
  1117. cpumask_var_t cm)
  1118. {
  1119. struct task_struct *t;
  1120. t = rnp->boost_kthread_task;
  1121. if (t != NULL)
  1122. set_cpus_allowed_ptr(rnp->boost_kthread_task, cm);
  1123. }
  1124. #define RCU_BOOST_DELAY_JIFFIES DIV_ROUND_UP(CONFIG_RCU_BOOST_DELAY * HZ, 1000)
  1125. /*
  1126. * Do priority-boost accounting for the start of a new grace period.
  1127. */
  1128. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1129. {
  1130. rnp->boost_time = jiffies + RCU_BOOST_DELAY_JIFFIES;
  1131. }
  1132. /*
  1133. * Create an RCU-boost kthread for the specified node if one does not
  1134. * already exist. We only create this kthread for preemptible RCU.
  1135. * Returns zero if all is well, a negated errno otherwise.
  1136. */
  1137. static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1138. struct rcu_node *rnp,
  1139. int rnp_index)
  1140. {
  1141. unsigned long flags;
  1142. struct sched_param sp;
  1143. struct task_struct *t;
  1144. if (&rcu_preempt_state != rsp)
  1145. return 0;
  1146. if (rnp->boost_kthread_task != NULL)
  1147. return 0;
  1148. t = kthread_create(rcu_boost_kthread, (void *)rnp,
  1149. "rcub%d", rnp_index);
  1150. if (IS_ERR(t))
  1151. return PTR_ERR(t);
  1152. raw_spin_lock_irqsave(&rnp->lock, flags);
  1153. rnp->boost_kthread_task = t;
  1154. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1155. sp.sched_priority = RCU_KTHREAD_PRIO;
  1156. sched_setscheduler_nocheck(t, SCHED_FIFO, &sp);
  1157. return 0;
  1158. }
  1159. static void __cpuinit rcu_wake_one_boost_kthread(struct rcu_node *rnp)
  1160. {
  1161. if (rnp->boost_kthread_task)
  1162. wake_up_process(rnp->boost_kthread_task);
  1163. }
  1164. #else /* #ifdef CONFIG_RCU_BOOST */
  1165. static void rcu_initiate_boost(struct rcu_node *rnp, unsigned long flags)
  1166. {
  1167. raw_spin_unlock_irqrestore(&rnp->lock, flags);
  1168. }
  1169. static void rcu_boost_kthread_setaffinity(struct rcu_node *rnp,
  1170. cpumask_var_t cm)
  1171. {
  1172. }
  1173. static void rcu_preempt_boost_start_gp(struct rcu_node *rnp)
  1174. {
  1175. }
  1176. static int __cpuinit rcu_spawn_one_boost_kthread(struct rcu_state *rsp,
  1177. struct rcu_node *rnp,
  1178. int rnp_index)
  1179. {
  1180. return 0;
  1181. }
  1182. static void __cpuinit rcu_wake_one_boost_kthread(struct rcu_node *rnp)
  1183. {
  1184. }
  1185. #endif /* #else #ifdef CONFIG_RCU_BOOST */
  1186. #ifndef CONFIG_SMP
  1187. void synchronize_sched_expedited(void)
  1188. {
  1189. cond_resched();
  1190. }
  1191. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  1192. #else /* #ifndef CONFIG_SMP */
  1193. static atomic_t sync_sched_expedited_started = ATOMIC_INIT(0);
  1194. static atomic_t sync_sched_expedited_done = ATOMIC_INIT(0);
  1195. static int synchronize_sched_expedited_cpu_stop(void *data)
  1196. {
  1197. /*
  1198. * There must be a full memory barrier on each affected CPU
  1199. * between the time that try_stop_cpus() is called and the
  1200. * time that it returns.
  1201. *
  1202. * In the current initial implementation of cpu_stop, the
  1203. * above condition is already met when the control reaches
  1204. * this point and the following smp_mb() is not strictly
  1205. * necessary. Do smp_mb() anyway for documentation and
  1206. * robustness against future implementation changes.
  1207. */
  1208. smp_mb(); /* See above comment block. */
  1209. return 0;
  1210. }
  1211. /*
  1212. * Wait for an rcu-sched grace period to elapse, but use "big hammer"
  1213. * approach to force grace period to end quickly. This consumes
  1214. * significant time on all CPUs, and is thus not recommended for
  1215. * any sort of common-case code.
  1216. *
  1217. * Note that it is illegal to call this function while holding any
  1218. * lock that is acquired by a CPU-hotplug notifier. Failing to
  1219. * observe this restriction will result in deadlock.
  1220. *
  1221. * This implementation can be thought of as an application of ticket
  1222. * locking to RCU, with sync_sched_expedited_started and
  1223. * sync_sched_expedited_done taking on the roles of the halves
  1224. * of the ticket-lock word. Each task atomically increments
  1225. * sync_sched_expedited_started upon entry, snapshotting the old value,
  1226. * then attempts to stop all the CPUs. If this succeeds, then each
  1227. * CPU will have executed a context switch, resulting in an RCU-sched
  1228. * grace period. We are then done, so we use atomic_cmpxchg() to
  1229. * update sync_sched_expedited_done to match our snapshot -- but
  1230. * only if someone else has not already advanced past our snapshot.
  1231. *
  1232. * On the other hand, if try_stop_cpus() fails, we check the value
  1233. * of sync_sched_expedited_done. If it has advanced past our
  1234. * initial snapshot, then someone else must have forced a grace period
  1235. * some time after we took our snapshot. In this case, our work is
  1236. * done for us, and we can simply return. Otherwise, we try again,
  1237. * but keep our initial snapshot for purposes of checking for someone
  1238. * doing our work for us.
  1239. *
  1240. * If we fail too many times in a row, we fall back to synchronize_sched().
  1241. */
  1242. void synchronize_sched_expedited(void)
  1243. {
  1244. int firstsnap, s, snap, trycount = 0;
  1245. /* Note that atomic_inc_return() implies full memory barrier. */
  1246. firstsnap = snap = atomic_inc_return(&sync_sched_expedited_started);
  1247. get_online_cpus();
  1248. /*
  1249. * Each pass through the following loop attempts to force a
  1250. * context switch on each CPU.
  1251. */
  1252. while (try_stop_cpus(cpu_online_mask,
  1253. synchronize_sched_expedited_cpu_stop,
  1254. NULL) == -EAGAIN) {
  1255. put_online_cpus();
  1256. /* No joy, try again later. Or just synchronize_sched(). */
  1257. if (trycount++ < 10)
  1258. udelay(trycount * num_online_cpus());
  1259. else {
  1260. synchronize_sched();
  1261. return;
  1262. }
  1263. /* Check to see if someone else did our work for us. */
  1264. s = atomic_read(&sync_sched_expedited_done);
  1265. if (UINT_CMP_GE((unsigned)s, (unsigned)firstsnap)) {
  1266. smp_mb(); /* ensure test happens before caller kfree */
  1267. return;
  1268. }
  1269. /*
  1270. * Refetching sync_sched_expedited_started allows later
  1271. * callers to piggyback on our grace period. We subtract
  1272. * 1 to get the same token that the last incrementer got.
  1273. * We retry after they started, so our grace period works
  1274. * for them, and they started after our first try, so their
  1275. * grace period works for us.
  1276. */
  1277. get_online_cpus();
  1278. snap = atomic_read(&sync_sched_expedited_started) - 1;
  1279. smp_mb(); /* ensure read is before try_stop_cpus(). */
  1280. }
  1281. /*
  1282. * Everyone up to our most recent fetch is covered by our grace
  1283. * period. Update the counter, but only if our work is still
  1284. * relevant -- which it won't be if someone who started later
  1285. * than we did beat us to the punch.
  1286. */
  1287. do {
  1288. s = atomic_read(&sync_sched_expedited_done);
  1289. if (UINT_CMP_GE((unsigned)s, (unsigned)snap)) {
  1290. smp_mb(); /* ensure test happens before caller kfree */
  1291. break;
  1292. }
  1293. } while (atomic_cmpxchg(&sync_sched_expedited_done, s, snap) != s);
  1294. put_online_cpus();
  1295. }
  1296. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  1297. #endif /* #else #ifndef CONFIG_SMP */
  1298. #if !defined(CONFIG_RCU_FAST_NO_HZ)
  1299. /*
  1300. * Check to see if any future RCU-related work will need to be done
  1301. * by the current CPU, even if none need be done immediately, returning
  1302. * 1 if so. This function is part of the RCU implementation; it is -not-
  1303. * an exported member of the RCU API.
  1304. *
  1305. * Because we have preemptible RCU, just check whether this CPU needs
  1306. * any flavor of RCU. Do not chew up lots of CPU cycles with preemption
  1307. * disabled in a most-likely vain attempt to cause RCU not to need this CPU.
  1308. */
  1309. int rcu_needs_cpu(int cpu)
  1310. {
  1311. return rcu_needs_cpu_quick_check(cpu);
  1312. }
  1313. /*
  1314. * Check to see if we need to continue a callback-flush operations to
  1315. * allow the last CPU to enter dyntick-idle mode. But fast dyntick-idle
  1316. * entry is not configured, so we never do need to.
  1317. */
  1318. static void rcu_needs_cpu_flush(void)
  1319. {
  1320. }
  1321. #else /* #if !defined(CONFIG_RCU_FAST_NO_HZ) */
  1322. #define RCU_NEEDS_CPU_FLUSHES 5
  1323. static DEFINE_PER_CPU(int, rcu_dyntick_drain);
  1324. static DEFINE_PER_CPU(unsigned long, rcu_dyntick_holdoff);
  1325. /*
  1326. * Check to see if any future RCU-related work will need to be done
  1327. * by the current CPU, even if none need be done immediately, returning
  1328. * 1 if so. This function is part of the RCU implementation; it is -not-
  1329. * an exported member of the RCU API.
  1330. *
  1331. * Because we are not supporting preemptible RCU, attempt to accelerate
  1332. * any current grace periods so that RCU no longer needs this CPU, but
  1333. * only if all other CPUs are already in dynticks-idle mode. This will
  1334. * allow the CPU cores to be powered down immediately, as opposed to after
  1335. * waiting many milliseconds for grace periods to elapse.
  1336. *
  1337. * Because it is not legal to invoke rcu_process_callbacks() with irqs
  1338. * disabled, we do one pass of force_quiescent_state(), then do a
  1339. * invoke_rcu_cpu_kthread() to cause rcu_process_callbacks() to be invoked
  1340. * later. The per-cpu rcu_dyntick_drain variable controls the sequencing.
  1341. */
  1342. int rcu_needs_cpu(int cpu)
  1343. {
  1344. int c = 0;
  1345. int snap;
  1346. int thatcpu;
  1347. /* Check for being in the holdoff period. */
  1348. if (per_cpu(rcu_dyntick_holdoff, cpu) == jiffies)
  1349. return rcu_needs_cpu_quick_check(cpu);
  1350. /* Don't bother unless we are the last non-dyntick-idle CPU. */
  1351. for_each_online_cpu(thatcpu) {
  1352. if (thatcpu == cpu)
  1353. continue;
  1354. snap = atomic_add_return(0, &per_cpu(rcu_dynticks,
  1355. thatcpu).dynticks);
  1356. smp_mb(); /* Order sampling of snap with end of grace period. */
  1357. if ((snap & 0x1) != 0) {
  1358. per_cpu(rcu_dyntick_drain, cpu) = 0;
  1359. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies - 1;
  1360. return rcu_needs_cpu_quick_check(cpu);
  1361. }
  1362. }
  1363. /* Check and update the rcu_dyntick_drain sequencing. */
  1364. if (per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  1365. /* First time through, initialize the counter. */
  1366. per_cpu(rcu_dyntick_drain, cpu) = RCU_NEEDS_CPU_FLUSHES;
  1367. } else if (--per_cpu(rcu_dyntick_drain, cpu) <= 0) {
  1368. /* We have hit the limit, so time to give up. */
  1369. per_cpu(rcu_dyntick_holdoff, cpu) = jiffies;
  1370. return rcu_needs_cpu_quick_check(cpu);
  1371. }
  1372. /* Do one step pushing remaining RCU callbacks through. */
  1373. if (per_cpu(rcu_sched_data, cpu).nxtlist) {
  1374. rcu_sched_qs(cpu);
  1375. force_quiescent_state(&rcu_sched_state, 0);
  1376. c = c || per_cpu(rcu_sched_data, cpu).nxtlist;
  1377. }
  1378. if (per_cpu(rcu_bh_data, cpu).nxtlist) {
  1379. rcu_bh_qs(cpu);
  1380. force_quiescent_state(&rcu_bh_state, 0);
  1381. c = c || per_cpu(rcu_bh_data, cpu).nxtlist;
  1382. }
  1383. /* If RCU callbacks are still pending, RCU still needs this CPU. */
  1384. if (c)
  1385. invoke_rcu_cpu_kthread();
  1386. return c;
  1387. }
  1388. /*
  1389. * Check to see if we need to continue a callback-flush operations to
  1390. * allow the last CPU to enter dyntick-idle mode.
  1391. */
  1392. static void rcu_needs_cpu_flush(void)
  1393. {
  1394. int cpu = smp_processor_id();
  1395. unsigned long flags;
  1396. if (per_cpu(rcu_dyntick_drain, cpu) <= 0)
  1397. return;
  1398. local_irq_save(flags);
  1399. (void)rcu_needs_cpu(cpu);
  1400. local_irq_restore(flags);
  1401. }
  1402. #endif /* #else #if !defined(CONFIG_RCU_FAST_NO_HZ) */