rcupreempt.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*
  2. * Read-Copy Update mechanism for mutual exclusion, realtime implementation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright IBM Corporation, 2006
  19. *
  20. * Authors: Paul E. McKenney <paulmck@us.ibm.com>
  21. * With thanks to Esben Nielsen, Bill Huey, and Ingo Molnar
  22. * for pushing me away from locks and towards counters, and
  23. * to Suparna Bhattacharya for pushing me completely away
  24. * from atomic instructions on the read side.
  25. *
  26. * - Added handling of Dynamic Ticks
  27. * Copyright 2007 - Paul E. Mckenney <paulmck@us.ibm.com>
  28. * - Steven Rostedt <srostedt@redhat.com>
  29. *
  30. * Papers: http://www.rdrop.com/users/paulmck/RCU
  31. *
  32. * Design Document: http://lwn.net/Articles/253651/
  33. *
  34. * For detailed explanation of Read-Copy Update mechanism see -
  35. * Documentation/RCU/ *.txt
  36. *
  37. */
  38. #include <linux/types.h>
  39. #include <linux/kernel.h>
  40. #include <linux/init.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/smp.h>
  43. #include <linux/rcupdate.h>
  44. #include <linux/interrupt.h>
  45. #include <linux/sched.h>
  46. #include <asm/atomic.h>
  47. #include <linux/bitops.h>
  48. #include <linux/module.h>
  49. #include <linux/kthread.h>
  50. #include <linux/completion.h>
  51. #include <linux/moduleparam.h>
  52. #include <linux/percpu.h>
  53. #include <linux/notifier.h>
  54. #include <linux/cpu.h>
  55. #include <linux/random.h>
  56. #include <linux/delay.h>
  57. #include <linux/cpumask.h>
  58. #include <linux/rcupreempt_trace.h>
  59. #include <asm/byteorder.h>
  60. /*
  61. * PREEMPT_RCU data structures.
  62. */
  63. /*
  64. * GP_STAGES specifies the number of times the state machine has
  65. * to go through the all the rcu_try_flip_states (see below)
  66. * in a single Grace Period.
  67. *
  68. * GP in GP_STAGES stands for Grace Period ;)
  69. */
  70. #define GP_STAGES 2
  71. struct rcu_data {
  72. spinlock_t lock; /* Protect rcu_data fields. */
  73. long completed; /* Number of last completed batch. */
  74. int waitlistcount;
  75. struct rcu_head *nextlist;
  76. struct rcu_head **nexttail;
  77. struct rcu_head *waitlist[GP_STAGES];
  78. struct rcu_head **waittail[GP_STAGES];
  79. struct rcu_head *donelist; /* from waitlist & waitschedlist */
  80. struct rcu_head **donetail;
  81. long rcu_flipctr[2];
  82. struct rcu_head *nextschedlist;
  83. struct rcu_head **nextschedtail;
  84. struct rcu_head *waitschedlist;
  85. struct rcu_head **waitschedtail;
  86. int rcu_sched_sleeping;
  87. #ifdef CONFIG_RCU_TRACE
  88. struct rcupreempt_trace trace;
  89. #endif /* #ifdef CONFIG_RCU_TRACE */
  90. };
  91. /*
  92. * States for rcu_try_flip() and friends.
  93. */
  94. enum rcu_try_flip_states {
  95. /*
  96. * Stay here if nothing is happening. Flip the counter if somthing
  97. * starts happening. Denoted by "I"
  98. */
  99. rcu_try_flip_idle_state,
  100. /*
  101. * Wait here for all CPUs to notice that the counter has flipped. This
  102. * prevents the old set of counters from ever being incremented once
  103. * we leave this state, which in turn is necessary because we cannot
  104. * test any individual counter for zero -- we can only check the sum.
  105. * Denoted by "A".
  106. */
  107. rcu_try_flip_waitack_state,
  108. /*
  109. * Wait here for the sum of the old per-CPU counters to reach zero.
  110. * Denoted by "Z".
  111. */
  112. rcu_try_flip_waitzero_state,
  113. /*
  114. * Wait here for each of the other CPUs to execute a memory barrier.
  115. * This is necessary to ensure that these other CPUs really have
  116. * completed executing their RCU read-side critical sections, despite
  117. * their CPUs wildly reordering memory. Denoted by "M".
  118. */
  119. rcu_try_flip_waitmb_state,
  120. };
  121. /*
  122. * States for rcu_ctrlblk.rcu_sched_sleep.
  123. */
  124. enum rcu_sched_sleep_states {
  125. rcu_sched_not_sleeping, /* Not sleeping, callbacks need GP. */
  126. rcu_sched_sleep_prep, /* Thinking of sleeping, rechecking. */
  127. rcu_sched_sleeping, /* Sleeping, awaken if GP needed. */
  128. };
  129. struct rcu_ctrlblk {
  130. spinlock_t fliplock; /* Protect state-machine transitions. */
  131. long completed; /* Number of last completed batch. */
  132. enum rcu_try_flip_states rcu_try_flip_state; /* The current state of
  133. the rcu state machine */
  134. spinlock_t schedlock; /* Protect rcu_sched sleep state. */
  135. enum rcu_sched_sleep_states sched_sleep; /* rcu_sched state. */
  136. wait_queue_head_t sched_wq; /* Place for rcu_sched to sleep. */
  137. };
  138. static DEFINE_PER_CPU(struct rcu_data, rcu_data);
  139. static struct rcu_ctrlblk rcu_ctrlblk = {
  140. .fliplock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.fliplock),
  141. .completed = 0,
  142. .rcu_try_flip_state = rcu_try_flip_idle_state,
  143. .schedlock = __SPIN_LOCK_UNLOCKED(rcu_ctrlblk.schedlock),
  144. .sched_sleep = rcu_sched_not_sleeping,
  145. .sched_wq = __WAIT_QUEUE_HEAD_INITIALIZER(rcu_ctrlblk.sched_wq),
  146. };
  147. static struct task_struct *rcu_sched_grace_period_task;
  148. #ifdef CONFIG_RCU_TRACE
  149. static char *rcu_try_flip_state_names[] =
  150. { "idle", "waitack", "waitzero", "waitmb" };
  151. #endif /* #ifdef CONFIG_RCU_TRACE */
  152. static DECLARE_BITMAP(rcu_cpu_online_map, NR_CPUS) __read_mostly
  153. = CPU_BITS_NONE;
  154. /*
  155. * Enum and per-CPU flag to determine when each CPU has seen
  156. * the most recent counter flip.
  157. */
  158. enum rcu_flip_flag_values {
  159. rcu_flip_seen, /* Steady/initial state, last flip seen. */
  160. /* Only GP detector can update. */
  161. rcu_flipped /* Flip just completed, need confirmation. */
  162. /* Only corresponding CPU can update. */
  163. };
  164. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_flip_flag_values, rcu_flip_flag)
  165. = rcu_flip_seen;
  166. /*
  167. * Enum and per-CPU flag to determine when each CPU has executed the
  168. * needed memory barrier to fence in memory references from its last RCU
  169. * read-side critical section in the just-completed grace period.
  170. */
  171. enum rcu_mb_flag_values {
  172. rcu_mb_done, /* Steady/initial state, no mb()s required. */
  173. /* Only GP detector can update. */
  174. rcu_mb_needed /* Flip just completed, need an mb(). */
  175. /* Only corresponding CPU can update. */
  176. };
  177. static DEFINE_PER_CPU_SHARED_ALIGNED(enum rcu_mb_flag_values, rcu_mb_flag)
  178. = rcu_mb_done;
  179. /*
  180. * RCU_DATA_ME: find the current CPU's rcu_data structure.
  181. * RCU_DATA_CPU: find the specified CPU's rcu_data structure.
  182. */
  183. #define RCU_DATA_ME() (&__get_cpu_var(rcu_data))
  184. #define RCU_DATA_CPU(cpu) (&per_cpu(rcu_data, cpu))
  185. /*
  186. * Helper macro for tracing when the appropriate rcu_data is not
  187. * cached in a local variable, but where the CPU number is so cached.
  188. */
  189. #define RCU_TRACE_CPU(f, cpu) RCU_TRACE(f, &(RCU_DATA_CPU(cpu)->trace));
  190. /*
  191. * Helper macro for tracing when the appropriate rcu_data is not
  192. * cached in a local variable.
  193. */
  194. #define RCU_TRACE_ME(f) RCU_TRACE(f, &(RCU_DATA_ME()->trace));
  195. /*
  196. * Helper macro for tracing when the appropriate rcu_data is pointed
  197. * to by a local variable.
  198. */
  199. #define RCU_TRACE_RDP(f, rdp) RCU_TRACE(f, &((rdp)->trace));
  200. #define RCU_SCHED_BATCH_TIME (HZ / 50)
  201. /*
  202. * Return the number of RCU batches processed thus far. Useful
  203. * for debug and statistics.
  204. */
  205. long rcu_batches_completed(void)
  206. {
  207. return rcu_ctrlblk.completed;
  208. }
  209. EXPORT_SYMBOL_GPL(rcu_batches_completed);
  210. void __rcu_read_lock(void)
  211. {
  212. int idx;
  213. struct task_struct *t = current;
  214. int nesting;
  215. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  216. if (nesting != 0) {
  217. /* An earlier rcu_read_lock() covers us, just count it. */
  218. t->rcu_read_lock_nesting = nesting + 1;
  219. } else {
  220. unsigned long flags;
  221. /*
  222. * We disable interrupts for the following reasons:
  223. * - If we get scheduling clock interrupt here, and we
  224. * end up acking the counter flip, it's like a promise
  225. * that we will never increment the old counter again.
  226. * Thus we will break that promise if that
  227. * scheduling clock interrupt happens between the time
  228. * we pick the .completed field and the time that we
  229. * increment our counter.
  230. *
  231. * - We don't want to be preempted out here.
  232. *
  233. * NMIs can still occur, of course, and might themselves
  234. * contain rcu_read_lock().
  235. */
  236. local_irq_save(flags);
  237. /*
  238. * Outermost nesting of rcu_read_lock(), so increment
  239. * the current counter for the current CPU. Use volatile
  240. * casts to prevent the compiler from reordering.
  241. */
  242. idx = ACCESS_ONCE(rcu_ctrlblk.completed) & 0x1;
  243. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])++;
  244. /*
  245. * Now that the per-CPU counter has been incremented, we
  246. * are protected from races with rcu_read_lock() invoked
  247. * from NMI handlers on this CPU. We can therefore safely
  248. * increment the nesting counter, relieving further NMIs
  249. * of the need to increment the per-CPU counter.
  250. */
  251. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting + 1;
  252. /*
  253. * Now that we have preventing any NMIs from storing
  254. * to the ->rcu_flipctr_idx, we can safely use it to
  255. * remember which counter to decrement in the matching
  256. * rcu_read_unlock().
  257. */
  258. ACCESS_ONCE(t->rcu_flipctr_idx) = idx;
  259. local_irq_restore(flags);
  260. }
  261. }
  262. EXPORT_SYMBOL_GPL(__rcu_read_lock);
  263. void __rcu_read_unlock(void)
  264. {
  265. int idx;
  266. struct task_struct *t = current;
  267. int nesting;
  268. nesting = ACCESS_ONCE(t->rcu_read_lock_nesting);
  269. if (nesting > 1) {
  270. /*
  271. * We are still protected by the enclosing rcu_read_lock(),
  272. * so simply decrement the counter.
  273. */
  274. t->rcu_read_lock_nesting = nesting - 1;
  275. } else {
  276. unsigned long flags;
  277. /*
  278. * Disable local interrupts to prevent the grace-period
  279. * detection state machine from seeing us half-done.
  280. * NMIs can still occur, of course, and might themselves
  281. * contain rcu_read_lock() and rcu_read_unlock().
  282. */
  283. local_irq_save(flags);
  284. /*
  285. * Outermost nesting of rcu_read_unlock(), so we must
  286. * decrement the current counter for the current CPU.
  287. * This must be done carefully, because NMIs can
  288. * occur at any point in this code, and any rcu_read_lock()
  289. * and rcu_read_unlock() pairs in the NMI handlers
  290. * must interact non-destructively with this code.
  291. * Lots of volatile casts, and -very- careful ordering.
  292. *
  293. * Changes to this code, including this one, must be
  294. * inspected, validated, and tested extremely carefully!!!
  295. */
  296. /*
  297. * First, pick up the index.
  298. */
  299. idx = ACCESS_ONCE(t->rcu_flipctr_idx);
  300. /*
  301. * Now that we have fetched the counter index, it is
  302. * safe to decrement the per-task RCU nesting counter.
  303. * After this, any interrupts or NMIs will increment and
  304. * decrement the per-CPU counters.
  305. */
  306. ACCESS_ONCE(t->rcu_read_lock_nesting) = nesting - 1;
  307. /*
  308. * It is now safe to decrement this task's nesting count.
  309. * NMIs that occur after this statement will route their
  310. * rcu_read_lock() calls through this "else" clause, and
  311. * will thus start incrementing the per-CPU counter on
  312. * their own. They will also clobber ->rcu_flipctr_idx,
  313. * but that is OK, since we have already fetched it.
  314. */
  315. ACCESS_ONCE(RCU_DATA_ME()->rcu_flipctr[idx])--;
  316. local_irq_restore(flags);
  317. }
  318. }
  319. EXPORT_SYMBOL_GPL(__rcu_read_unlock);
  320. /*
  321. * If a global counter flip has occurred since the last time that we
  322. * advanced callbacks, advance them. Hardware interrupts must be
  323. * disabled when calling this function.
  324. */
  325. static void __rcu_advance_callbacks(struct rcu_data *rdp)
  326. {
  327. int cpu;
  328. int i;
  329. int wlc = 0;
  330. if (rdp->completed != rcu_ctrlblk.completed) {
  331. if (rdp->waitlist[GP_STAGES - 1] != NULL) {
  332. *rdp->donetail = rdp->waitlist[GP_STAGES - 1];
  333. rdp->donetail = rdp->waittail[GP_STAGES - 1];
  334. RCU_TRACE_RDP(rcupreempt_trace_move2done, rdp);
  335. }
  336. for (i = GP_STAGES - 2; i >= 0; i--) {
  337. if (rdp->waitlist[i] != NULL) {
  338. rdp->waitlist[i + 1] = rdp->waitlist[i];
  339. rdp->waittail[i + 1] = rdp->waittail[i];
  340. wlc++;
  341. } else {
  342. rdp->waitlist[i + 1] = NULL;
  343. rdp->waittail[i + 1] =
  344. &rdp->waitlist[i + 1];
  345. }
  346. }
  347. if (rdp->nextlist != NULL) {
  348. rdp->waitlist[0] = rdp->nextlist;
  349. rdp->waittail[0] = rdp->nexttail;
  350. wlc++;
  351. rdp->nextlist = NULL;
  352. rdp->nexttail = &rdp->nextlist;
  353. RCU_TRACE_RDP(rcupreempt_trace_move2wait, rdp);
  354. } else {
  355. rdp->waitlist[0] = NULL;
  356. rdp->waittail[0] = &rdp->waitlist[0];
  357. }
  358. rdp->waitlistcount = wlc;
  359. rdp->completed = rcu_ctrlblk.completed;
  360. }
  361. /*
  362. * Check to see if this CPU needs to report that it has seen
  363. * the most recent counter flip, thereby declaring that all
  364. * subsequent rcu_read_lock() invocations will respect this flip.
  365. */
  366. cpu = raw_smp_processor_id();
  367. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  368. smp_mb(); /* Subsequent counter accesses must see new value */
  369. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  370. smp_mb(); /* Subsequent RCU read-side critical sections */
  371. /* seen -after- acknowledgement. */
  372. }
  373. }
  374. DEFINE_PER_CPU_SHARED_ALIGNED(struct rcu_dyntick_sched, rcu_dyntick_sched) = {
  375. .dynticks = 1,
  376. };
  377. #ifdef CONFIG_NO_HZ
  378. static DEFINE_PER_CPU(int, rcu_update_flag);
  379. /**
  380. * rcu_irq_enter - Called from Hard irq handlers and NMI/SMI.
  381. *
  382. * If the CPU was idle with dynamic ticks active, this updates the
  383. * rcu_dyntick_sched.dynticks to let the RCU handling know that the
  384. * CPU is active.
  385. */
  386. void rcu_irq_enter(void)
  387. {
  388. int cpu = smp_processor_id();
  389. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  390. if (per_cpu(rcu_update_flag, cpu))
  391. per_cpu(rcu_update_flag, cpu)++;
  392. /*
  393. * Only update if we are coming from a stopped ticks mode
  394. * (rcu_dyntick_sched.dynticks is even).
  395. */
  396. if (!in_interrupt() &&
  397. (rdssp->dynticks & 0x1) == 0) {
  398. /*
  399. * The following might seem like we could have a race
  400. * with NMI/SMIs. But this really isn't a problem.
  401. * Here we do a read/modify/write, and the race happens
  402. * when an NMI/SMI comes in after the read and before
  403. * the write. But NMI/SMIs will increment this counter
  404. * twice before returning, so the zero bit will not
  405. * be corrupted by the NMI/SMI which is the most important
  406. * part.
  407. *
  408. * The only thing is that we would bring back the counter
  409. * to a postion that it was in during the NMI/SMI.
  410. * But the zero bit would be set, so the rest of the
  411. * counter would again be ignored.
  412. *
  413. * On return from the IRQ, the counter may have the zero
  414. * bit be 0 and the counter the same as the return from
  415. * the NMI/SMI. If the state machine was so unlucky to
  416. * see that, it still doesn't matter, since all
  417. * RCU read-side critical sections on this CPU would
  418. * have already completed.
  419. */
  420. rdssp->dynticks++;
  421. /*
  422. * The following memory barrier ensures that any
  423. * rcu_read_lock() primitives in the irq handler
  424. * are seen by other CPUs to follow the above
  425. * increment to rcu_dyntick_sched.dynticks. This is
  426. * required in order for other CPUs to correctly
  427. * determine when it is safe to advance the RCU
  428. * grace-period state machine.
  429. */
  430. smp_mb(); /* see above block comment. */
  431. /*
  432. * Since we can't determine the dynamic tick mode from
  433. * the rcu_dyntick_sched.dynticks after this routine,
  434. * we use a second flag to acknowledge that we came
  435. * from an idle state with ticks stopped.
  436. */
  437. per_cpu(rcu_update_flag, cpu)++;
  438. /*
  439. * If we take an NMI/SMI now, they will also increment
  440. * the rcu_update_flag, and will not update the
  441. * rcu_dyntick_sched.dynticks on exit. That is for
  442. * this IRQ to do.
  443. */
  444. }
  445. }
  446. /**
  447. * rcu_irq_exit - Called from exiting Hard irq context.
  448. *
  449. * If the CPU was idle with dynamic ticks active, update the
  450. * rcu_dyntick_sched.dynticks to put let the RCU handling be
  451. * aware that the CPU is going back to idle with no ticks.
  452. */
  453. void rcu_irq_exit(void)
  454. {
  455. int cpu = smp_processor_id();
  456. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  457. /*
  458. * rcu_update_flag is set if we interrupted the CPU
  459. * when it was idle with ticks stopped.
  460. * Once this occurs, we keep track of interrupt nesting
  461. * because a NMI/SMI could also come in, and we still
  462. * only want the IRQ that started the increment of the
  463. * rcu_dyntick_sched.dynticks to be the one that modifies
  464. * it on exit.
  465. */
  466. if (per_cpu(rcu_update_flag, cpu)) {
  467. if (--per_cpu(rcu_update_flag, cpu))
  468. return;
  469. /* This must match the interrupt nesting */
  470. WARN_ON(in_interrupt());
  471. /*
  472. * If an NMI/SMI happens now we are still
  473. * protected by the rcu_dyntick_sched.dynticks being odd.
  474. */
  475. /*
  476. * The following memory barrier ensures that any
  477. * rcu_read_unlock() primitives in the irq handler
  478. * are seen by other CPUs to preceed the following
  479. * increment to rcu_dyntick_sched.dynticks. This
  480. * is required in order for other CPUs to determine
  481. * when it is safe to advance the RCU grace-period
  482. * state machine.
  483. */
  484. smp_mb(); /* see above block comment. */
  485. rdssp->dynticks++;
  486. WARN_ON(rdssp->dynticks & 0x1);
  487. }
  488. }
  489. void rcu_nmi_enter(void)
  490. {
  491. rcu_irq_enter();
  492. }
  493. void rcu_nmi_exit(void)
  494. {
  495. rcu_irq_exit();
  496. }
  497. static void dyntick_save_progress_counter(int cpu)
  498. {
  499. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  500. rdssp->dynticks_snap = rdssp->dynticks;
  501. }
  502. static inline int
  503. rcu_try_flip_waitack_needed(int cpu)
  504. {
  505. long curr;
  506. long snap;
  507. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  508. curr = rdssp->dynticks;
  509. snap = rdssp->dynticks_snap;
  510. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  511. /*
  512. * If the CPU remained in dynticks mode for the entire time
  513. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  514. * then it cannot be in the middle of an rcu_read_lock(), so
  515. * the next rcu_read_lock() it executes must use the new value
  516. * of the counter. So we can safely pretend that this CPU
  517. * already acknowledged the counter.
  518. */
  519. if ((curr == snap) && ((curr & 0x1) == 0))
  520. return 0;
  521. /*
  522. * If the CPU passed through or entered a dynticks idle phase with
  523. * no active irq handlers, then, as above, we can safely pretend
  524. * that this CPU already acknowledged the counter.
  525. */
  526. if ((curr - snap) > 2 || (curr & 0x1) == 0)
  527. return 0;
  528. /* We need this CPU to explicitly acknowledge the counter flip. */
  529. return 1;
  530. }
  531. static inline int
  532. rcu_try_flip_waitmb_needed(int cpu)
  533. {
  534. long curr;
  535. long snap;
  536. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  537. curr = rdssp->dynticks;
  538. snap = rdssp->dynticks_snap;
  539. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  540. /*
  541. * If the CPU remained in dynticks mode for the entire time
  542. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  543. * then it cannot have executed an RCU read-side critical section
  544. * during that time, so there is no need for it to execute a
  545. * memory barrier.
  546. */
  547. if ((curr == snap) && ((curr & 0x1) == 0))
  548. return 0;
  549. /*
  550. * If the CPU either entered or exited an outermost interrupt,
  551. * SMI, NMI, or whatever handler, then we know that it executed
  552. * a memory barrier when doing so. So we don't need another one.
  553. */
  554. if (curr != snap)
  555. return 0;
  556. /* We need the CPU to execute a memory barrier. */
  557. return 1;
  558. }
  559. static void dyntick_save_progress_counter_sched(int cpu)
  560. {
  561. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  562. rdssp->sched_dynticks_snap = rdssp->dynticks;
  563. }
  564. static int rcu_qsctr_inc_needed_dyntick(int cpu)
  565. {
  566. long curr;
  567. long snap;
  568. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  569. curr = rdssp->dynticks;
  570. snap = rdssp->sched_dynticks_snap;
  571. smp_mb(); /* force ordering with cpu entering/leaving dynticks. */
  572. /*
  573. * If the CPU remained in dynticks mode for the entire time
  574. * and didn't take any interrupts, NMIs, SMIs, or whatever,
  575. * then it cannot be in the middle of an rcu_read_lock(), so
  576. * the next rcu_read_lock() it executes must use the new value
  577. * of the counter. Therefore, this CPU has been in a quiescent
  578. * state the entire time, and we don't need to wait for it.
  579. */
  580. if ((curr == snap) && ((curr & 0x1) == 0))
  581. return 0;
  582. /*
  583. * If the CPU passed through or entered a dynticks idle phase with
  584. * no active irq handlers, then, as above, this CPU has already
  585. * passed through a quiescent state.
  586. */
  587. if ((curr - snap) > 2 || (snap & 0x1) == 0)
  588. return 0;
  589. /* We need this CPU to go through a quiescent state. */
  590. return 1;
  591. }
  592. #else /* !CONFIG_NO_HZ */
  593. # define dyntick_save_progress_counter(cpu) do { } while (0)
  594. # define rcu_try_flip_waitack_needed(cpu) (1)
  595. # define rcu_try_flip_waitmb_needed(cpu) (1)
  596. # define dyntick_save_progress_counter_sched(cpu) do { } while (0)
  597. # define rcu_qsctr_inc_needed_dyntick(cpu) (1)
  598. #endif /* CONFIG_NO_HZ */
  599. static void save_qsctr_sched(int cpu)
  600. {
  601. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  602. rdssp->sched_qs_snap = rdssp->sched_qs;
  603. }
  604. static inline int rcu_qsctr_inc_needed(int cpu)
  605. {
  606. struct rcu_dyntick_sched *rdssp = &per_cpu(rcu_dyntick_sched, cpu);
  607. /*
  608. * If there has been a quiescent state, no more need to wait
  609. * on this CPU.
  610. */
  611. if (rdssp->sched_qs != rdssp->sched_qs_snap) {
  612. smp_mb(); /* force ordering with cpu entering schedule(). */
  613. return 0;
  614. }
  615. /* We need this CPU to go through a quiescent state. */
  616. return 1;
  617. }
  618. /*
  619. * Get here when RCU is idle. Decide whether we need to
  620. * move out of idle state, and return non-zero if so.
  621. * "Straightforward" approach for the moment, might later
  622. * use callback-list lengths, grace-period duration, or
  623. * some such to determine when to exit idle state.
  624. * Might also need a pre-idle test that does not acquire
  625. * the lock, but let's get the simple case working first...
  626. */
  627. static int
  628. rcu_try_flip_idle(void)
  629. {
  630. int cpu;
  631. RCU_TRACE_ME(rcupreempt_trace_try_flip_i1);
  632. if (!rcu_pending(smp_processor_id())) {
  633. RCU_TRACE_ME(rcupreempt_trace_try_flip_ie1);
  634. return 0;
  635. }
  636. /*
  637. * Do the flip.
  638. */
  639. RCU_TRACE_ME(rcupreempt_trace_try_flip_g1);
  640. rcu_ctrlblk.completed++; /* stands in for rcu_try_flip_g2 */
  641. /*
  642. * Need a memory barrier so that other CPUs see the new
  643. * counter value before they see the subsequent change of all
  644. * the rcu_flip_flag instances to rcu_flipped.
  645. */
  646. smp_mb(); /* see above block comment. */
  647. /* Now ask each CPU for acknowledgement of the flip. */
  648. for_each_cpu(cpu, to_cpumask(rcu_cpu_online_map)) {
  649. per_cpu(rcu_flip_flag, cpu) = rcu_flipped;
  650. dyntick_save_progress_counter(cpu);
  651. }
  652. return 1;
  653. }
  654. /*
  655. * Wait for CPUs to acknowledge the flip.
  656. */
  657. static int
  658. rcu_try_flip_waitack(void)
  659. {
  660. int cpu;
  661. RCU_TRACE_ME(rcupreempt_trace_try_flip_a1);
  662. for_each_cpu(cpu, to_cpumask(rcu_cpu_online_map))
  663. if (rcu_try_flip_waitack_needed(cpu) &&
  664. per_cpu(rcu_flip_flag, cpu) != rcu_flip_seen) {
  665. RCU_TRACE_ME(rcupreempt_trace_try_flip_ae1);
  666. return 0;
  667. }
  668. /*
  669. * Make sure our checks above don't bleed into subsequent
  670. * waiting for the sum of the counters to reach zero.
  671. */
  672. smp_mb(); /* see above block comment. */
  673. RCU_TRACE_ME(rcupreempt_trace_try_flip_a2);
  674. return 1;
  675. }
  676. /*
  677. * Wait for collective ``last'' counter to reach zero,
  678. * then tell all CPUs to do an end-of-grace-period memory barrier.
  679. */
  680. static int
  681. rcu_try_flip_waitzero(void)
  682. {
  683. int cpu;
  684. int lastidx = !(rcu_ctrlblk.completed & 0x1);
  685. int sum = 0;
  686. /* Check to see if the sum of the "last" counters is zero. */
  687. RCU_TRACE_ME(rcupreempt_trace_try_flip_z1);
  688. for_each_cpu(cpu, to_cpumask(rcu_cpu_online_map))
  689. sum += RCU_DATA_CPU(cpu)->rcu_flipctr[lastidx];
  690. if (sum != 0) {
  691. RCU_TRACE_ME(rcupreempt_trace_try_flip_ze1);
  692. return 0;
  693. }
  694. /*
  695. * This ensures that the other CPUs see the call for
  696. * memory barriers -after- the sum to zero has been
  697. * detected here
  698. */
  699. smp_mb(); /* ^^^^^^^^^^^^ */
  700. /* Call for a memory barrier from each CPU. */
  701. for_each_cpu(cpu, to_cpumask(rcu_cpu_online_map)) {
  702. per_cpu(rcu_mb_flag, cpu) = rcu_mb_needed;
  703. dyntick_save_progress_counter(cpu);
  704. }
  705. RCU_TRACE_ME(rcupreempt_trace_try_flip_z2);
  706. return 1;
  707. }
  708. /*
  709. * Wait for all CPUs to do their end-of-grace-period memory barrier.
  710. * Return 0 once all CPUs have done so.
  711. */
  712. static int
  713. rcu_try_flip_waitmb(void)
  714. {
  715. int cpu;
  716. RCU_TRACE_ME(rcupreempt_trace_try_flip_m1);
  717. for_each_cpu(cpu, to_cpumask(rcu_cpu_online_map))
  718. if (rcu_try_flip_waitmb_needed(cpu) &&
  719. per_cpu(rcu_mb_flag, cpu) != rcu_mb_done) {
  720. RCU_TRACE_ME(rcupreempt_trace_try_flip_me1);
  721. return 0;
  722. }
  723. smp_mb(); /* Ensure that the above checks precede any following flip. */
  724. RCU_TRACE_ME(rcupreempt_trace_try_flip_m2);
  725. return 1;
  726. }
  727. /*
  728. * Attempt a single flip of the counters. Remember, a single flip does
  729. * -not- constitute a grace period. Instead, the interval between
  730. * at least GP_STAGES consecutive flips is a grace period.
  731. *
  732. * If anyone is nuts enough to run this CONFIG_PREEMPT_RCU implementation
  733. * on a large SMP, they might want to use a hierarchical organization of
  734. * the per-CPU-counter pairs.
  735. */
  736. static void rcu_try_flip(void)
  737. {
  738. unsigned long flags;
  739. RCU_TRACE_ME(rcupreempt_trace_try_flip_1);
  740. if (unlikely(!spin_trylock_irqsave(&rcu_ctrlblk.fliplock, flags))) {
  741. RCU_TRACE_ME(rcupreempt_trace_try_flip_e1);
  742. return;
  743. }
  744. /*
  745. * Take the next transition(s) through the RCU grace-period
  746. * flip-counter state machine.
  747. */
  748. switch (rcu_ctrlblk.rcu_try_flip_state) {
  749. case rcu_try_flip_idle_state:
  750. if (rcu_try_flip_idle())
  751. rcu_ctrlblk.rcu_try_flip_state =
  752. rcu_try_flip_waitack_state;
  753. break;
  754. case rcu_try_flip_waitack_state:
  755. if (rcu_try_flip_waitack())
  756. rcu_ctrlblk.rcu_try_flip_state =
  757. rcu_try_flip_waitzero_state;
  758. break;
  759. case rcu_try_flip_waitzero_state:
  760. if (rcu_try_flip_waitzero())
  761. rcu_ctrlblk.rcu_try_flip_state =
  762. rcu_try_flip_waitmb_state;
  763. break;
  764. case rcu_try_flip_waitmb_state:
  765. if (rcu_try_flip_waitmb())
  766. rcu_ctrlblk.rcu_try_flip_state =
  767. rcu_try_flip_idle_state;
  768. }
  769. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  770. }
  771. /*
  772. * Check to see if this CPU needs to do a memory barrier in order to
  773. * ensure that any prior RCU read-side critical sections have committed
  774. * their counter manipulations and critical-section memory references
  775. * before declaring the grace period to be completed.
  776. */
  777. static void rcu_check_mb(int cpu)
  778. {
  779. if (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed) {
  780. smp_mb(); /* Ensure RCU read-side accesses are visible. */
  781. per_cpu(rcu_mb_flag, cpu) = rcu_mb_done;
  782. }
  783. }
  784. void rcu_check_callbacks(int cpu, int user)
  785. {
  786. unsigned long flags;
  787. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  788. /*
  789. * If this CPU took its interrupt from user mode or from the
  790. * idle loop, and this is not a nested interrupt, then
  791. * this CPU has to have exited all prior preept-disable
  792. * sections of code. So increment the counter to note this.
  793. *
  794. * The memory barrier is needed to handle the case where
  795. * writes from a preempt-disable section of code get reordered
  796. * into schedule() by this CPU's write buffer. So the memory
  797. * barrier makes sure that the rcu_qsctr_inc() is seen by other
  798. * CPUs to happen after any such write.
  799. */
  800. if (user ||
  801. (idle_cpu(cpu) && !in_softirq() &&
  802. hardirq_count() <= (1 << HARDIRQ_SHIFT))) {
  803. smp_mb(); /* Guard against aggressive schedule(). */
  804. rcu_qsctr_inc(cpu);
  805. }
  806. rcu_check_mb(cpu);
  807. if (rcu_ctrlblk.completed == rdp->completed)
  808. rcu_try_flip();
  809. spin_lock_irqsave(&rdp->lock, flags);
  810. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  811. __rcu_advance_callbacks(rdp);
  812. if (rdp->donelist == NULL) {
  813. spin_unlock_irqrestore(&rdp->lock, flags);
  814. } else {
  815. spin_unlock_irqrestore(&rdp->lock, flags);
  816. raise_softirq(RCU_SOFTIRQ);
  817. }
  818. }
  819. /*
  820. * Needed by dynticks, to make sure all RCU processing has finished
  821. * when we go idle:
  822. */
  823. void rcu_advance_callbacks(int cpu, int user)
  824. {
  825. unsigned long flags;
  826. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  827. if (rcu_ctrlblk.completed == rdp->completed) {
  828. rcu_try_flip();
  829. if (rcu_ctrlblk.completed == rdp->completed)
  830. return;
  831. }
  832. spin_lock_irqsave(&rdp->lock, flags);
  833. RCU_TRACE_RDP(rcupreempt_trace_check_callbacks, rdp);
  834. __rcu_advance_callbacks(rdp);
  835. spin_unlock_irqrestore(&rdp->lock, flags);
  836. }
  837. #ifdef CONFIG_HOTPLUG_CPU
  838. #define rcu_offline_cpu_enqueue(srclist, srctail, dstlist, dsttail) do { \
  839. *dsttail = srclist; \
  840. if (srclist != NULL) { \
  841. dsttail = srctail; \
  842. srclist = NULL; \
  843. srctail = &srclist;\
  844. } \
  845. } while (0)
  846. void rcu_offline_cpu(int cpu)
  847. {
  848. int i;
  849. struct rcu_head *list = NULL;
  850. unsigned long flags;
  851. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  852. struct rcu_head *schedlist = NULL;
  853. struct rcu_head **schedtail = &schedlist;
  854. struct rcu_head **tail = &list;
  855. /*
  856. * Remove all callbacks from the newly dead CPU, retaining order.
  857. * Otherwise rcu_barrier() will fail
  858. */
  859. spin_lock_irqsave(&rdp->lock, flags);
  860. rcu_offline_cpu_enqueue(rdp->donelist, rdp->donetail, list, tail);
  861. for (i = GP_STAGES - 1; i >= 0; i--)
  862. rcu_offline_cpu_enqueue(rdp->waitlist[i], rdp->waittail[i],
  863. list, tail);
  864. rcu_offline_cpu_enqueue(rdp->nextlist, rdp->nexttail, list, tail);
  865. rcu_offline_cpu_enqueue(rdp->waitschedlist, rdp->waitschedtail,
  866. schedlist, schedtail);
  867. rcu_offline_cpu_enqueue(rdp->nextschedlist, rdp->nextschedtail,
  868. schedlist, schedtail);
  869. rdp->rcu_sched_sleeping = 0;
  870. spin_unlock_irqrestore(&rdp->lock, flags);
  871. rdp->waitlistcount = 0;
  872. /* Disengage the newly dead CPU from the grace-period computation. */
  873. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  874. rcu_check_mb(cpu);
  875. if (per_cpu(rcu_flip_flag, cpu) == rcu_flipped) {
  876. smp_mb(); /* Subsequent counter accesses must see new value */
  877. per_cpu(rcu_flip_flag, cpu) = rcu_flip_seen;
  878. smp_mb(); /* Subsequent RCU read-side critical sections */
  879. /* seen -after- acknowledgement. */
  880. }
  881. RCU_DATA_ME()->rcu_flipctr[0] += RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  882. RCU_DATA_ME()->rcu_flipctr[1] += RCU_DATA_CPU(cpu)->rcu_flipctr[1];
  883. RCU_DATA_CPU(cpu)->rcu_flipctr[0] = 0;
  884. RCU_DATA_CPU(cpu)->rcu_flipctr[1] = 0;
  885. cpumask_clear_cpu(cpu, to_cpumask(rcu_cpu_online_map));
  886. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  887. /*
  888. * Place the removed callbacks on the current CPU's queue.
  889. * Make them all start a new grace period: simple approach,
  890. * in theory could starve a given set of callbacks, but
  891. * you would need to be doing some serious CPU hotplugging
  892. * to make this happen. If this becomes a problem, adding
  893. * a synchronize_rcu() to the hotplug path would be a simple
  894. * fix.
  895. */
  896. local_irq_save(flags); /* disable preempt till we know what lock. */
  897. rdp = RCU_DATA_ME();
  898. spin_lock(&rdp->lock);
  899. *rdp->nexttail = list;
  900. if (list)
  901. rdp->nexttail = tail;
  902. *rdp->nextschedtail = schedlist;
  903. if (schedlist)
  904. rdp->nextschedtail = schedtail;
  905. spin_unlock_irqrestore(&rdp->lock, flags);
  906. }
  907. #else /* #ifdef CONFIG_HOTPLUG_CPU */
  908. void rcu_offline_cpu(int cpu)
  909. {
  910. }
  911. #endif /* #else #ifdef CONFIG_HOTPLUG_CPU */
  912. void __cpuinit rcu_online_cpu(int cpu)
  913. {
  914. unsigned long flags;
  915. struct rcu_data *rdp;
  916. spin_lock_irqsave(&rcu_ctrlblk.fliplock, flags);
  917. cpumask_set_cpu(cpu, to_cpumask(rcu_cpu_online_map));
  918. spin_unlock_irqrestore(&rcu_ctrlblk.fliplock, flags);
  919. /*
  920. * The rcu_sched grace-period processing might have bypassed
  921. * this CPU, given that it was not in the rcu_cpu_online_map
  922. * when the grace-period scan started. This means that the
  923. * grace-period task might sleep. So make sure that if this
  924. * should happen, the first callback posted to this CPU will
  925. * wake up the grace-period task if need be.
  926. */
  927. rdp = RCU_DATA_CPU(cpu);
  928. spin_lock_irqsave(&rdp->lock, flags);
  929. rdp->rcu_sched_sleeping = 1;
  930. spin_unlock_irqrestore(&rdp->lock, flags);
  931. }
  932. static void rcu_process_callbacks(struct softirq_action *unused)
  933. {
  934. unsigned long flags;
  935. struct rcu_head *next, *list;
  936. struct rcu_data *rdp;
  937. local_irq_save(flags);
  938. rdp = RCU_DATA_ME();
  939. spin_lock(&rdp->lock);
  940. list = rdp->donelist;
  941. if (list == NULL) {
  942. spin_unlock_irqrestore(&rdp->lock, flags);
  943. return;
  944. }
  945. rdp->donelist = NULL;
  946. rdp->donetail = &rdp->donelist;
  947. RCU_TRACE_RDP(rcupreempt_trace_done_remove, rdp);
  948. spin_unlock_irqrestore(&rdp->lock, flags);
  949. while (list) {
  950. next = list->next;
  951. list->func(list);
  952. list = next;
  953. RCU_TRACE_ME(rcupreempt_trace_invoke);
  954. }
  955. }
  956. void call_rcu(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  957. {
  958. unsigned long flags;
  959. struct rcu_data *rdp;
  960. head->func = func;
  961. head->next = NULL;
  962. local_irq_save(flags);
  963. rdp = RCU_DATA_ME();
  964. spin_lock(&rdp->lock);
  965. __rcu_advance_callbacks(rdp);
  966. *rdp->nexttail = head;
  967. rdp->nexttail = &head->next;
  968. RCU_TRACE_RDP(rcupreempt_trace_next_add, rdp);
  969. spin_unlock_irqrestore(&rdp->lock, flags);
  970. }
  971. EXPORT_SYMBOL_GPL(call_rcu);
  972. void call_rcu_sched(struct rcu_head *head, void (*func)(struct rcu_head *rcu))
  973. {
  974. unsigned long flags;
  975. struct rcu_data *rdp;
  976. int wake_gp = 0;
  977. head->func = func;
  978. head->next = NULL;
  979. local_irq_save(flags);
  980. rdp = RCU_DATA_ME();
  981. spin_lock(&rdp->lock);
  982. *rdp->nextschedtail = head;
  983. rdp->nextschedtail = &head->next;
  984. if (rdp->rcu_sched_sleeping) {
  985. /* Grace-period processing might be sleeping... */
  986. rdp->rcu_sched_sleeping = 0;
  987. wake_gp = 1;
  988. }
  989. spin_unlock_irqrestore(&rdp->lock, flags);
  990. if (wake_gp) {
  991. /* Wake up grace-period processing, unless someone beat us. */
  992. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  993. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleeping)
  994. wake_gp = 0;
  995. rcu_ctrlblk.sched_sleep = rcu_sched_not_sleeping;
  996. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  997. if (wake_gp)
  998. wake_up_interruptible(&rcu_ctrlblk.sched_wq);
  999. }
  1000. }
  1001. EXPORT_SYMBOL_GPL(call_rcu_sched);
  1002. /*
  1003. * Wait until all currently running preempt_disable() code segments
  1004. * (including hardware-irq-disable segments) complete. Note that
  1005. * in -rt this does -not- necessarily result in all currently executing
  1006. * interrupt -handlers- having completed.
  1007. */
  1008. void __synchronize_sched(void)
  1009. {
  1010. struct rcu_synchronize rcu;
  1011. init_completion(&rcu.completion);
  1012. /* Will wake me after RCU finished. */
  1013. call_rcu_sched(&rcu.head, wakeme_after_rcu);
  1014. /* Wait for it. */
  1015. wait_for_completion(&rcu.completion);
  1016. }
  1017. EXPORT_SYMBOL_GPL(__synchronize_sched);
  1018. /*
  1019. * kthread function that manages call_rcu_sched grace periods.
  1020. */
  1021. static int rcu_sched_grace_period(void *arg)
  1022. {
  1023. int couldsleep; /* might sleep after current pass. */
  1024. int couldsleepnext = 0; /* might sleep after next pass. */
  1025. int cpu;
  1026. unsigned long flags;
  1027. struct rcu_data *rdp;
  1028. int ret;
  1029. /*
  1030. * Each pass through the following loop handles one
  1031. * rcu_sched grace period cycle.
  1032. */
  1033. do {
  1034. /* Save each CPU's current state. */
  1035. for_each_online_cpu(cpu) {
  1036. dyntick_save_progress_counter_sched(cpu);
  1037. save_qsctr_sched(cpu);
  1038. }
  1039. /*
  1040. * Sleep for about an RCU grace-period's worth to
  1041. * allow better batching and to consume less CPU.
  1042. */
  1043. schedule_timeout_interruptible(RCU_SCHED_BATCH_TIME);
  1044. /*
  1045. * If there was nothing to do last time, prepare to
  1046. * sleep at the end of the current grace period cycle.
  1047. */
  1048. couldsleep = couldsleepnext;
  1049. couldsleepnext = 1;
  1050. if (couldsleep) {
  1051. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1052. rcu_ctrlblk.sched_sleep = rcu_sched_sleep_prep;
  1053. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1054. }
  1055. /*
  1056. * Wait on each CPU in turn to have either visited
  1057. * a quiescent state or been in dynticks-idle mode.
  1058. */
  1059. for_each_online_cpu(cpu) {
  1060. while (rcu_qsctr_inc_needed(cpu) &&
  1061. rcu_qsctr_inc_needed_dyntick(cpu)) {
  1062. /* resched_cpu(cpu); @@@ */
  1063. schedule_timeout_interruptible(1);
  1064. }
  1065. }
  1066. /* Advance callbacks for each CPU. */
  1067. for_each_online_cpu(cpu) {
  1068. rdp = RCU_DATA_CPU(cpu);
  1069. spin_lock_irqsave(&rdp->lock, flags);
  1070. /*
  1071. * We are running on this CPU irq-disabled, so no
  1072. * CPU can go offline until we re-enable irqs.
  1073. * The current CPU might have already gone
  1074. * offline (between the for_each_offline_cpu and
  1075. * the spin_lock_irqsave), but in that case all its
  1076. * callback lists will be empty, so no harm done.
  1077. *
  1078. * Advance the callbacks! We share normal RCU's
  1079. * donelist, since callbacks are invoked the
  1080. * same way in either case.
  1081. */
  1082. if (rdp->waitschedlist != NULL) {
  1083. *rdp->donetail = rdp->waitschedlist;
  1084. rdp->donetail = rdp->waitschedtail;
  1085. /*
  1086. * Next rcu_check_callbacks() will
  1087. * do the required raise_softirq().
  1088. */
  1089. }
  1090. if (rdp->nextschedlist != NULL) {
  1091. rdp->waitschedlist = rdp->nextschedlist;
  1092. rdp->waitschedtail = rdp->nextschedtail;
  1093. couldsleep = 0;
  1094. couldsleepnext = 0;
  1095. } else {
  1096. rdp->waitschedlist = NULL;
  1097. rdp->waitschedtail = &rdp->waitschedlist;
  1098. }
  1099. rdp->nextschedlist = NULL;
  1100. rdp->nextschedtail = &rdp->nextschedlist;
  1101. /* Mark sleep intention. */
  1102. rdp->rcu_sched_sleeping = couldsleep;
  1103. spin_unlock_irqrestore(&rdp->lock, flags);
  1104. }
  1105. /* If we saw callbacks on the last scan, go deal with them. */
  1106. if (!couldsleep)
  1107. continue;
  1108. /* Attempt to block... */
  1109. spin_lock_irqsave(&rcu_ctrlblk.schedlock, flags);
  1110. if (rcu_ctrlblk.sched_sleep != rcu_sched_sleep_prep) {
  1111. /*
  1112. * Someone posted a callback after we scanned.
  1113. * Go take care of it.
  1114. */
  1115. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1116. couldsleepnext = 0;
  1117. continue;
  1118. }
  1119. /* Block until the next person posts a callback. */
  1120. rcu_ctrlblk.sched_sleep = rcu_sched_sleeping;
  1121. spin_unlock_irqrestore(&rcu_ctrlblk.schedlock, flags);
  1122. ret = 0;
  1123. __wait_event_interruptible(rcu_ctrlblk.sched_wq,
  1124. rcu_ctrlblk.sched_sleep != rcu_sched_sleeping,
  1125. ret);
  1126. /*
  1127. * Signals would prevent us from sleeping, and we cannot
  1128. * do much with them in any case. So flush them.
  1129. */
  1130. if (ret)
  1131. flush_signals(current);
  1132. couldsleepnext = 0;
  1133. } while (!kthread_should_stop());
  1134. return (0);
  1135. }
  1136. /*
  1137. * Check to see if any future RCU-related work will need to be done
  1138. * by the current CPU, even if none need be done immediately, returning
  1139. * 1 if so. Assumes that notifiers would take care of handling any
  1140. * outstanding requests from the RCU core.
  1141. *
  1142. * This function is part of the RCU implementation; it is -not-
  1143. * an exported member of the RCU API.
  1144. */
  1145. int rcu_needs_cpu(int cpu)
  1146. {
  1147. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1148. return (rdp->donelist != NULL ||
  1149. !!rdp->waitlistcount ||
  1150. rdp->nextlist != NULL ||
  1151. rdp->nextschedlist != NULL ||
  1152. rdp->waitschedlist != NULL);
  1153. }
  1154. int rcu_pending(int cpu)
  1155. {
  1156. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1157. /* The CPU has at least one callback queued somewhere. */
  1158. if (rdp->donelist != NULL ||
  1159. !!rdp->waitlistcount ||
  1160. rdp->nextlist != NULL ||
  1161. rdp->nextschedlist != NULL ||
  1162. rdp->waitschedlist != NULL)
  1163. return 1;
  1164. /* The RCU core needs an acknowledgement from this CPU. */
  1165. if ((per_cpu(rcu_flip_flag, cpu) == rcu_flipped) ||
  1166. (per_cpu(rcu_mb_flag, cpu) == rcu_mb_needed))
  1167. return 1;
  1168. /* This CPU has fallen behind the global grace-period number. */
  1169. if (rdp->completed != rcu_ctrlblk.completed)
  1170. return 1;
  1171. /* Nothing needed from this CPU. */
  1172. return 0;
  1173. }
  1174. static int __cpuinit rcu_cpu_notify(struct notifier_block *self,
  1175. unsigned long action, void *hcpu)
  1176. {
  1177. long cpu = (long)hcpu;
  1178. switch (action) {
  1179. case CPU_UP_PREPARE:
  1180. case CPU_UP_PREPARE_FROZEN:
  1181. rcu_online_cpu(cpu);
  1182. break;
  1183. case CPU_UP_CANCELED:
  1184. case CPU_UP_CANCELED_FROZEN:
  1185. case CPU_DEAD:
  1186. case CPU_DEAD_FROZEN:
  1187. rcu_offline_cpu(cpu);
  1188. break;
  1189. default:
  1190. break;
  1191. }
  1192. return NOTIFY_OK;
  1193. }
  1194. static struct notifier_block __cpuinitdata rcu_nb = {
  1195. .notifier_call = rcu_cpu_notify,
  1196. };
  1197. void __init __rcu_init(void)
  1198. {
  1199. int cpu;
  1200. int i;
  1201. struct rcu_data *rdp;
  1202. printk(KERN_NOTICE "Preemptible RCU implementation.\n");
  1203. for_each_possible_cpu(cpu) {
  1204. rdp = RCU_DATA_CPU(cpu);
  1205. spin_lock_init(&rdp->lock);
  1206. rdp->completed = 0;
  1207. rdp->waitlistcount = 0;
  1208. rdp->nextlist = NULL;
  1209. rdp->nexttail = &rdp->nextlist;
  1210. for (i = 0; i < GP_STAGES; i++) {
  1211. rdp->waitlist[i] = NULL;
  1212. rdp->waittail[i] = &rdp->waitlist[i];
  1213. }
  1214. rdp->donelist = NULL;
  1215. rdp->donetail = &rdp->donelist;
  1216. rdp->rcu_flipctr[0] = 0;
  1217. rdp->rcu_flipctr[1] = 0;
  1218. rdp->nextschedlist = NULL;
  1219. rdp->nextschedtail = &rdp->nextschedlist;
  1220. rdp->waitschedlist = NULL;
  1221. rdp->waitschedtail = &rdp->waitschedlist;
  1222. rdp->rcu_sched_sleeping = 0;
  1223. }
  1224. register_cpu_notifier(&rcu_nb);
  1225. /*
  1226. * We don't need protection against CPU-Hotplug here
  1227. * since
  1228. * a) If a CPU comes online while we are iterating over the
  1229. * cpu_online_mask below, we would only end up making a
  1230. * duplicate call to rcu_online_cpu() which sets the corresponding
  1231. * CPU's mask in the rcu_cpu_online_map.
  1232. *
  1233. * b) A CPU cannot go offline at this point in time since the user
  1234. * does not have access to the sysfs interface, nor do we
  1235. * suspend the system.
  1236. */
  1237. for_each_online_cpu(cpu)
  1238. rcu_cpu_notify(&rcu_nb, CPU_UP_PREPARE, (void *)(long) cpu);
  1239. open_softirq(RCU_SOFTIRQ, rcu_process_callbacks);
  1240. }
  1241. /*
  1242. * Late-boot-time RCU initialization that must wait until after scheduler
  1243. * has been initialized.
  1244. */
  1245. void __init rcu_init_sched(void)
  1246. {
  1247. rcu_sched_grace_period_task = kthread_run(rcu_sched_grace_period,
  1248. NULL,
  1249. "rcu_sched_grace_period");
  1250. WARN_ON(IS_ERR(rcu_sched_grace_period_task));
  1251. }
  1252. #ifdef CONFIG_RCU_TRACE
  1253. long *rcupreempt_flipctr(int cpu)
  1254. {
  1255. return &RCU_DATA_CPU(cpu)->rcu_flipctr[0];
  1256. }
  1257. EXPORT_SYMBOL_GPL(rcupreempt_flipctr);
  1258. int rcupreempt_flip_flag(int cpu)
  1259. {
  1260. return per_cpu(rcu_flip_flag, cpu);
  1261. }
  1262. EXPORT_SYMBOL_GPL(rcupreempt_flip_flag);
  1263. int rcupreempt_mb_flag(int cpu)
  1264. {
  1265. return per_cpu(rcu_mb_flag, cpu);
  1266. }
  1267. EXPORT_SYMBOL_GPL(rcupreempt_mb_flag);
  1268. char *rcupreempt_try_flip_state_name(void)
  1269. {
  1270. return rcu_try_flip_state_names[rcu_ctrlblk.rcu_try_flip_state];
  1271. }
  1272. EXPORT_SYMBOL_GPL(rcupreempt_try_flip_state_name);
  1273. struct rcupreempt_trace *rcupreempt_trace_cpu(int cpu)
  1274. {
  1275. struct rcu_data *rdp = RCU_DATA_CPU(cpu);
  1276. return &rdp->trace;
  1277. }
  1278. EXPORT_SYMBOL_GPL(rcupreempt_trace_cpu);
  1279. #endif /* #ifdef RCU_TRACE */