rcu_segcblist.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * RCU segmented callback lists, function definitions
  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, you can access it online at
  16. * http://www.gnu.org/licenses/gpl-2.0.html.
  17. *
  18. * Copyright IBM Corporation, 2017
  19. *
  20. * Authors: Paul E. McKenney <paulmck@linux.vnet.ibm.com>
  21. */
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/interrupt.h>
  25. #include "rcu_segcblist.h"
  26. /* Initialize simple callback list. */
  27. void rcu_cblist_init(struct rcu_cblist *rclp)
  28. {
  29. rclp->head = NULL;
  30. rclp->tail = &rclp->head;
  31. rclp->len = 0;
  32. rclp->len_lazy = 0;
  33. }
  34. /*
  35. * Debug function to actually count the number of callbacks.
  36. * If the number exceeds the limit specified, return -1.
  37. */
  38. long rcu_cblist_count_cbs(struct rcu_cblist *rclp, long lim)
  39. {
  40. int cnt = 0;
  41. struct rcu_head **rhpp = &rclp->head;
  42. for (;;) {
  43. if (!*rhpp)
  44. return cnt;
  45. if (++cnt > lim)
  46. return -1;
  47. rhpp = &(*rhpp)->next;
  48. }
  49. }
  50. /*
  51. * Dequeue the oldest rcu_head structure from the specified callback
  52. * list. This function assumes that the callback is non-lazy, but
  53. * the caller can later invoke rcu_cblist_dequeued_lazy() if it
  54. * finds otherwise (and if it cares about laziness). This allows
  55. * different users to have different ways of determining laziness.
  56. */
  57. struct rcu_head *rcu_cblist_dequeue(struct rcu_cblist *rclp)
  58. {
  59. struct rcu_head *rhp;
  60. rhp = rclp->head;
  61. if (!rhp)
  62. return NULL;
  63. rclp->len--;
  64. rclp->head = rhp->next;
  65. if (!rclp->head)
  66. rclp->tail = &rclp->head;
  67. return rhp;
  68. }
  69. /*
  70. * Initialize an rcu_segcblist structure.
  71. */
  72. void rcu_segcblist_init(struct rcu_segcblist *rsclp)
  73. {
  74. int i;
  75. BUILD_BUG_ON(RCU_NEXT_TAIL + 1 != ARRAY_SIZE(rsclp->gp_seq));
  76. BUILD_BUG_ON(ARRAY_SIZE(rsclp->tails) != ARRAY_SIZE(rsclp->gp_seq));
  77. rsclp->head = NULL;
  78. for (i = 0; i < RCU_CBLIST_NSEGS; i++)
  79. rsclp->tails[i] = &rsclp->head;
  80. rsclp->len = 0;
  81. rsclp->len_lazy = 0;
  82. }
  83. /*
  84. * Disable the specified rcu_segcblist structure, so that callbacks can
  85. * no longer be posted to it. This structure must be empty.
  86. */
  87. void rcu_segcblist_disable(struct rcu_segcblist *rsclp)
  88. {
  89. WARN_ON_ONCE(!rcu_segcblist_empty(rsclp));
  90. WARN_ON_ONCE(rcu_segcblist_n_cbs(rsclp));
  91. WARN_ON_ONCE(rcu_segcblist_n_lazy_cbs(rsclp));
  92. rsclp->tails[RCU_NEXT_TAIL] = NULL;
  93. }
  94. /*
  95. * Is the specified segment of the specified rcu_segcblist structure
  96. * empty of callbacks?
  97. */
  98. bool rcu_segcblist_segempty(struct rcu_segcblist *rsclp, int seg)
  99. {
  100. if (seg == RCU_DONE_TAIL)
  101. return &rsclp->head == rsclp->tails[RCU_DONE_TAIL];
  102. return rsclp->tails[seg - 1] == rsclp->tails[seg];
  103. }
  104. /*
  105. * Does the specified rcu_segcblist structure contain callbacks that
  106. * are ready to be invoked?
  107. */
  108. bool rcu_segcblist_ready_cbs(struct rcu_segcblist *rsclp)
  109. {
  110. return rcu_segcblist_is_enabled(rsclp) &&
  111. &rsclp->head != rsclp->tails[RCU_DONE_TAIL];
  112. }
  113. /*
  114. * Does the specified rcu_segcblist structure contain callbacks that
  115. * are still pending, that is, not yet ready to be invoked?
  116. */
  117. bool rcu_segcblist_pend_cbs(struct rcu_segcblist *rsclp)
  118. {
  119. return rcu_segcblist_is_enabled(rsclp) &&
  120. !rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL);
  121. }
  122. /*
  123. * Dequeue and return the first ready-to-invoke callback. If there
  124. * are no ready-to-invoke callbacks, return NULL. Disables interrupts
  125. * to avoid interference. Does not protect from interference from other
  126. * CPUs or tasks.
  127. */
  128. struct rcu_head *rcu_segcblist_dequeue(struct rcu_segcblist *rsclp)
  129. {
  130. unsigned long flags;
  131. int i;
  132. struct rcu_head *rhp;
  133. local_irq_save(flags);
  134. if (!rcu_segcblist_ready_cbs(rsclp)) {
  135. local_irq_restore(flags);
  136. return NULL;
  137. }
  138. rhp = rsclp->head;
  139. BUG_ON(!rhp);
  140. rsclp->head = rhp->next;
  141. for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++) {
  142. if (rsclp->tails[i] != &rhp->next)
  143. break;
  144. rsclp->tails[i] = &rsclp->head;
  145. }
  146. smp_mb(); /* Dequeue before decrement for rcu_barrier(). */
  147. WRITE_ONCE(rsclp->len, rsclp->len - 1);
  148. local_irq_restore(flags);
  149. return rhp;
  150. }
  151. /*
  152. * Account for the fact that a previously dequeued callback turned out
  153. * to be marked as lazy.
  154. */
  155. void rcu_segcblist_dequeued_lazy(struct rcu_segcblist *rsclp)
  156. {
  157. unsigned long flags;
  158. local_irq_save(flags);
  159. rsclp->len_lazy--;
  160. local_irq_restore(flags);
  161. }
  162. /*
  163. * Return a pointer to the first callback in the specified rcu_segcblist
  164. * structure. This is useful for diagnostics.
  165. */
  166. struct rcu_head *rcu_segcblist_first_cb(struct rcu_segcblist *rsclp)
  167. {
  168. if (rcu_segcblist_is_enabled(rsclp))
  169. return rsclp->head;
  170. return NULL;
  171. }
  172. /*
  173. * Return a pointer to the first pending callback in the specified
  174. * rcu_segcblist structure. This is useful just after posting a given
  175. * callback -- if that callback is the first pending callback, then
  176. * you cannot rely on someone else having already started up the required
  177. * grace period.
  178. */
  179. struct rcu_head *rcu_segcblist_first_pend_cb(struct rcu_segcblist *rsclp)
  180. {
  181. if (rcu_segcblist_is_enabled(rsclp))
  182. return *rsclp->tails[RCU_DONE_TAIL];
  183. return NULL;
  184. }
  185. /*
  186. * Does the specified rcu_segcblist structure contain callbacks that
  187. * have not yet been processed beyond having been posted, that is,
  188. * does it contain callbacks in its last segment?
  189. */
  190. bool rcu_segcblist_new_cbs(struct rcu_segcblist *rsclp)
  191. {
  192. return rcu_segcblist_is_enabled(rsclp) &&
  193. !rcu_segcblist_restempty(rsclp, RCU_NEXT_READY_TAIL);
  194. }
  195. /*
  196. * Enqueue the specified callback onto the specified rcu_segcblist
  197. * structure, updating accounting as needed. Note that the ->len
  198. * field may be accessed locklessly, hence the WRITE_ONCE().
  199. * The ->len field is used by rcu_barrier() and friends to determine
  200. * if it must post a callback on this structure, and it is OK
  201. * for rcu_barrier() to sometimes post callbacks needlessly, but
  202. * absolutely not OK for it to ever miss posting a callback.
  203. */
  204. void rcu_segcblist_enqueue(struct rcu_segcblist *rsclp,
  205. struct rcu_head *rhp, bool lazy)
  206. {
  207. WRITE_ONCE(rsclp->len, rsclp->len + 1); /* ->len sampled locklessly. */
  208. if (lazy)
  209. rsclp->len_lazy++;
  210. smp_mb(); /* Ensure counts are updated before callback is enqueued. */
  211. rhp->next = NULL;
  212. *rsclp->tails[RCU_NEXT_TAIL] = rhp;
  213. rsclp->tails[RCU_NEXT_TAIL] = &rhp->next;
  214. }
  215. /*
  216. * Entrain the specified callback onto the specified rcu_segcblist at
  217. * the end of the last non-empty segment. If the entire rcu_segcblist
  218. * is empty, make no change, but return false.
  219. *
  220. * This is intended for use by rcu_barrier()-like primitives, -not-
  221. * for normal grace-period use. IMPORTANT: The callback you enqueue
  222. * will wait for all prior callbacks, NOT necessarily for a grace
  223. * period. You have been warned.
  224. */
  225. bool rcu_segcblist_entrain(struct rcu_segcblist *rsclp,
  226. struct rcu_head *rhp, bool lazy)
  227. {
  228. int i;
  229. if (rcu_segcblist_n_cbs(rsclp) == 0)
  230. return false;
  231. WRITE_ONCE(rsclp->len, rsclp->len + 1);
  232. if (lazy)
  233. rsclp->len_lazy++;
  234. smp_mb(); /* Ensure counts are updated before callback is entrained. */
  235. rhp->next = NULL;
  236. for (i = RCU_NEXT_TAIL; i > RCU_DONE_TAIL; i--)
  237. if (rsclp->tails[i] != rsclp->tails[i - 1])
  238. break;
  239. *rsclp->tails[i] = rhp;
  240. for (; i <= RCU_NEXT_TAIL; i++)
  241. rsclp->tails[i] = &rhp->next;
  242. return true;
  243. }
  244. /*
  245. * Extract only the counts from the specified rcu_segcblist structure,
  246. * and place them in the specified rcu_cblist structure. This function
  247. * supports both callback orphaning and invocation, hence the separation
  248. * of counts and callbacks. (Callbacks ready for invocation must be
  249. * orphaned and adopted separately from pending callbacks, but counts
  250. * apply to all callbacks. Locking must be used to make sure that
  251. * both orphaned-callbacks lists are consistent.)
  252. */
  253. void rcu_segcblist_extract_count(struct rcu_segcblist *rsclp,
  254. struct rcu_cblist *rclp)
  255. {
  256. rclp->len_lazy += rsclp->len_lazy;
  257. rclp->len += rsclp->len;
  258. rsclp->len_lazy = 0;
  259. WRITE_ONCE(rsclp->len, 0); /* ->len sampled locklessly. */
  260. }
  261. /*
  262. * Extract only those callbacks ready to be invoked from the specified
  263. * rcu_segcblist structure and place them in the specified rcu_cblist
  264. * structure.
  265. */
  266. void rcu_segcblist_extract_done_cbs(struct rcu_segcblist *rsclp,
  267. struct rcu_cblist *rclp)
  268. {
  269. int i;
  270. if (!rcu_segcblist_ready_cbs(rsclp))
  271. return; /* Nothing to do. */
  272. *rclp->tail = rsclp->head;
  273. rsclp->head = *rsclp->tails[RCU_DONE_TAIL];
  274. *rsclp->tails[RCU_DONE_TAIL] = NULL;
  275. rclp->tail = rsclp->tails[RCU_DONE_TAIL];
  276. for (i = RCU_CBLIST_NSEGS - 1; i >= RCU_DONE_TAIL; i--)
  277. if (rsclp->tails[i] == rsclp->tails[RCU_DONE_TAIL])
  278. rsclp->tails[i] = &rsclp->head;
  279. }
  280. /*
  281. * Extract only those callbacks still pending (not yet ready to be
  282. * invoked) from the specified rcu_segcblist structure and place them in
  283. * the specified rcu_cblist structure. Note that this loses information
  284. * about any callbacks that might have been partway done waiting for
  285. * their grace period. Too bad! They will have to start over.
  286. */
  287. void rcu_segcblist_extract_pend_cbs(struct rcu_segcblist *rsclp,
  288. struct rcu_cblist *rclp)
  289. {
  290. int i;
  291. if (!rcu_segcblist_pend_cbs(rsclp))
  292. return; /* Nothing to do. */
  293. *rclp->tail = *rsclp->tails[RCU_DONE_TAIL];
  294. rclp->tail = rsclp->tails[RCU_NEXT_TAIL];
  295. *rsclp->tails[RCU_DONE_TAIL] = NULL;
  296. for (i = RCU_DONE_TAIL + 1; i < RCU_CBLIST_NSEGS; i++)
  297. rsclp->tails[i] = rsclp->tails[RCU_DONE_TAIL];
  298. }
  299. /*
  300. * Insert counts from the specified rcu_cblist structure in the
  301. * specified rcu_segcblist structure.
  302. */
  303. void rcu_segcblist_insert_count(struct rcu_segcblist *rsclp,
  304. struct rcu_cblist *rclp)
  305. {
  306. rsclp->len_lazy += rclp->len_lazy;
  307. /* ->len sampled locklessly. */
  308. WRITE_ONCE(rsclp->len, rsclp->len + rclp->len);
  309. rclp->len_lazy = 0;
  310. rclp->len = 0;
  311. }
  312. /*
  313. * Move callbacks from the specified rcu_cblist to the beginning of the
  314. * done-callbacks segment of the specified rcu_segcblist.
  315. */
  316. void rcu_segcblist_insert_done_cbs(struct rcu_segcblist *rsclp,
  317. struct rcu_cblist *rclp)
  318. {
  319. int i;
  320. if (!rclp->head)
  321. return; /* No callbacks to move. */
  322. *rclp->tail = rsclp->head;
  323. rsclp->head = rclp->head;
  324. for (i = RCU_DONE_TAIL; i < RCU_CBLIST_NSEGS; i++)
  325. if (&rsclp->head == rsclp->tails[i])
  326. rsclp->tails[i] = rclp->tail;
  327. else
  328. break;
  329. rclp->head = NULL;
  330. rclp->tail = &rclp->head;
  331. }
  332. /*
  333. * Move callbacks from the specified rcu_cblist to the end of the
  334. * new-callbacks segment of the specified rcu_segcblist.
  335. */
  336. void rcu_segcblist_insert_pend_cbs(struct rcu_segcblist *rsclp,
  337. struct rcu_cblist *rclp)
  338. {
  339. if (!rclp->head)
  340. return; /* Nothing to do. */
  341. *rsclp->tails[RCU_NEXT_TAIL] = rclp->head;
  342. rsclp->tails[RCU_NEXT_TAIL] = rclp->tail;
  343. rclp->head = NULL;
  344. rclp->tail = &rclp->head;
  345. }
  346. /*
  347. * Advance the callbacks in the specified rcu_segcblist structure based
  348. * on the current value passed in for the grace-period counter.
  349. */
  350. void rcu_segcblist_advance(struct rcu_segcblist *rsclp, unsigned long seq)
  351. {
  352. int i, j;
  353. WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
  354. if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
  355. return;
  356. /*
  357. * Find all callbacks whose ->gp_seq numbers indicate that they
  358. * are ready to invoke, and put them into the RCU_DONE_TAIL segment.
  359. */
  360. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++) {
  361. if (ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
  362. break;
  363. rsclp->tails[RCU_DONE_TAIL] = rsclp->tails[i];
  364. }
  365. /* If no callbacks moved, nothing more need be done. */
  366. if (i == RCU_WAIT_TAIL)
  367. return;
  368. /* Clean up tail pointers that might have been misordered above. */
  369. for (j = RCU_WAIT_TAIL; j < i; j++)
  370. rsclp->tails[j] = rsclp->tails[RCU_DONE_TAIL];
  371. /*
  372. * Callbacks moved, so clean up the misordered ->tails[] pointers
  373. * that now point into the middle of the list of ready-to-invoke
  374. * callbacks. The overall effect is to copy down the later pointers
  375. * into the gap that was created by the now-ready segments.
  376. */
  377. for (j = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++, j++) {
  378. if (rsclp->tails[j] == rsclp->tails[RCU_NEXT_TAIL])
  379. break; /* No more callbacks. */
  380. rsclp->tails[j] = rsclp->tails[i];
  381. rsclp->gp_seq[j] = rsclp->gp_seq[i];
  382. }
  383. }
  384. /*
  385. * "Accelerate" callbacks based on more-accurate grace-period information.
  386. * The reason for this is that RCU does not synchronize the beginnings and
  387. * ends of grace periods, and that callbacks are posted locally. This in
  388. * turn means that the callbacks must be labelled conservatively early
  389. * on, as getting exact information would degrade both performance and
  390. * scalability. When more accurate grace-period information becomes
  391. * available, previously posted callbacks can be "accelerated", marking
  392. * them to complete at the end of the earlier grace period.
  393. *
  394. * This function operates on an rcu_segcblist structure, and also the
  395. * grace-period sequence number seq at which new callbacks would become
  396. * ready to invoke. Returns true if there are callbacks that won't be
  397. * ready to invoke until seq, false otherwise.
  398. */
  399. bool rcu_segcblist_accelerate(struct rcu_segcblist *rsclp, unsigned long seq)
  400. {
  401. int i;
  402. WARN_ON_ONCE(!rcu_segcblist_is_enabled(rsclp));
  403. if (rcu_segcblist_restempty(rsclp, RCU_DONE_TAIL))
  404. return false;
  405. /*
  406. * Find the segment preceding the oldest segment of callbacks
  407. * whose ->gp_seq[] completion is at or after that passed in via
  408. * "seq", skipping any empty segments. This oldest segment, along
  409. * with any later segments, can be merged in with any newly arrived
  410. * callbacks in the RCU_NEXT_TAIL segment, and assigned "seq"
  411. * as their ->gp_seq[] grace-period completion sequence number.
  412. */
  413. for (i = RCU_NEXT_READY_TAIL; i > RCU_DONE_TAIL; i--)
  414. if (rsclp->tails[i] != rsclp->tails[i - 1] &&
  415. ULONG_CMP_LT(rsclp->gp_seq[i], seq))
  416. break;
  417. /*
  418. * If all the segments contain callbacks that correspond to
  419. * earlier grace-period sequence numbers than "seq", leave.
  420. * Assuming that the rcu_segcblist structure has enough
  421. * segments in its arrays, this can only happen if some of
  422. * the non-done segments contain callbacks that really are
  423. * ready to invoke. This situation will get straightened
  424. * out by the next call to rcu_segcblist_advance().
  425. *
  426. * Also advance to the oldest segment of callbacks whose
  427. * ->gp_seq[] completion is at or after that passed in via "seq",
  428. * skipping any empty segments.
  429. */
  430. if (++i >= RCU_NEXT_TAIL)
  431. return false;
  432. /*
  433. * Merge all later callbacks, including newly arrived callbacks,
  434. * into the segment located by the for-loop above. Assign "seq"
  435. * as the ->gp_seq[] value in order to correctly handle the case
  436. * where there were no pending callbacks in the rcu_segcblist
  437. * structure other than in the RCU_NEXT_TAIL segment.
  438. */
  439. for (; i < RCU_NEXT_TAIL; i++) {
  440. rsclp->tails[i] = rsclp->tails[RCU_NEXT_TAIL];
  441. rsclp->gp_seq[i] = seq;
  442. }
  443. return true;
  444. }
  445. /*
  446. * Scan the specified rcu_segcblist structure for callbacks that need
  447. * a grace period later than the one specified by "seq". We don't look
  448. * at the RCU_DONE_TAIL or RCU_NEXT_TAIL segments because they don't
  449. * have a grace-period sequence number.
  450. */
  451. bool rcu_segcblist_future_gp_needed(struct rcu_segcblist *rsclp,
  452. unsigned long seq)
  453. {
  454. int i;
  455. for (i = RCU_WAIT_TAIL; i < RCU_NEXT_TAIL; i++)
  456. if (rsclp->tails[i - 1] != rsclp->tails[i] &&
  457. ULONG_CMP_LT(seq, rsclp->gp_seq[i]))
  458. return true;
  459. return false;
  460. }