srcutree.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Sleepable Read-Copy Update mechanism for mutual exclusion.
  4. *
  5. * Copyright (C) IBM Corporation, 2006
  6. * Copyright (C) Fujitsu, 2012
  7. *
  8. * Authors: Paul McKenney <paulmck@linux.ibm.com>
  9. * Lai Jiangshan <laijs@cn.fujitsu.com>
  10. *
  11. * For detailed explanation of Read-Copy Update mechanism see -
  12. * Documentation/RCU/ *.txt
  13. *
  14. */
  15. #define pr_fmt(fmt) "rcu: " fmt
  16. #include <linux/export.h>
  17. #include <linux/mutex.h>
  18. #include <linux/percpu.h>
  19. #include <linux/preempt.h>
  20. #include <linux/rcupdate_wait.h>
  21. #include <linux/sched.h>
  22. #include <linux/smp.h>
  23. #include <linux/delay.h>
  24. #include <linux/module.h>
  25. #include <linux/slab.h>
  26. #include <linux/srcu.h>
  27. #include "rcu.h"
  28. #include "rcu_segcblist.h"
  29. /* Holdoff in nanoseconds for auto-expediting. */
  30. #define DEFAULT_SRCU_EXP_HOLDOFF (25 * 1000)
  31. static ulong exp_holdoff = DEFAULT_SRCU_EXP_HOLDOFF;
  32. module_param(exp_holdoff, ulong, 0444);
  33. /* Overflow-check frequency. N bits roughly says every 2**N grace periods. */
  34. static ulong counter_wrap_check = (ULONG_MAX >> 2);
  35. module_param(counter_wrap_check, ulong, 0444);
  36. /*
  37. * Control conversion to SRCU_SIZE_BIG:
  38. * 0: Don't convert at all.
  39. * 1: Convert at init_srcu_struct() time.
  40. * 2: Convert when rcutorture invokes srcu_torture_stats_print().
  41. * 3: Decide at boot time based on system shape (default).
  42. * 0x1x: Convert when excessive contention encountered.
  43. */
  44. #define SRCU_SIZING_NONE 0
  45. #define SRCU_SIZING_INIT 1
  46. #define SRCU_SIZING_TORTURE 2
  47. #define SRCU_SIZING_AUTO 3
  48. #define SRCU_SIZING_CONTEND 0x10
  49. #define SRCU_SIZING_IS(x) ((convert_to_big & ~SRCU_SIZING_CONTEND) == x)
  50. #define SRCU_SIZING_IS_NONE() (SRCU_SIZING_IS(SRCU_SIZING_NONE))
  51. #define SRCU_SIZING_IS_INIT() (SRCU_SIZING_IS(SRCU_SIZING_INIT))
  52. #define SRCU_SIZING_IS_TORTURE() (SRCU_SIZING_IS(SRCU_SIZING_TORTURE))
  53. #define SRCU_SIZING_IS_CONTEND() (convert_to_big & SRCU_SIZING_CONTEND)
  54. static int convert_to_big = SRCU_SIZING_AUTO;
  55. module_param(convert_to_big, int, 0444);
  56. /* Number of CPUs to trigger init_srcu_struct()-time transition to big. */
  57. static int big_cpu_lim __read_mostly = 128;
  58. module_param(big_cpu_lim, int, 0444);
  59. /* Contention events per jiffy to initiate transition to big. */
  60. static int small_contention_lim __read_mostly = 100;
  61. module_param(small_contention_lim, int, 0444);
  62. /* Early-boot callback-management, so early that no lock is required! */
  63. static LIST_HEAD(srcu_boot_list);
  64. static bool __read_mostly srcu_init_done;
  65. static void srcu_invoke_callbacks(struct work_struct *work);
  66. static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay);
  67. static void process_srcu(struct work_struct *work);
  68. static void srcu_delay_timer(struct timer_list *t);
  69. /* Wrappers for lock acquisition and release, see raw_spin_lock_rcu_node(). */
  70. #define spin_lock_rcu_node(p) \
  71. do { \
  72. spin_lock(&ACCESS_PRIVATE(p, lock)); \
  73. smp_mb__after_unlock_lock(); \
  74. } while (0)
  75. #define spin_unlock_rcu_node(p) spin_unlock(&ACCESS_PRIVATE(p, lock))
  76. #define spin_lock_irq_rcu_node(p) \
  77. do { \
  78. spin_lock_irq(&ACCESS_PRIVATE(p, lock)); \
  79. smp_mb__after_unlock_lock(); \
  80. } while (0)
  81. #define spin_unlock_irq_rcu_node(p) \
  82. spin_unlock_irq(&ACCESS_PRIVATE(p, lock))
  83. #define spin_lock_irqsave_rcu_node(p, flags) \
  84. do { \
  85. spin_lock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
  86. smp_mb__after_unlock_lock(); \
  87. } while (0)
  88. #define spin_trylock_irqsave_rcu_node(p, flags) \
  89. ({ \
  90. bool ___locked = spin_trylock_irqsave(&ACCESS_PRIVATE(p, lock), flags); \
  91. \
  92. if (___locked) \
  93. smp_mb__after_unlock_lock(); \
  94. ___locked; \
  95. })
  96. #define spin_unlock_irqrestore_rcu_node(p, flags) \
  97. spin_unlock_irqrestore(&ACCESS_PRIVATE(p, lock), flags) \
  98. /*
  99. * Initialize SRCU per-CPU data. Note that statically allocated
  100. * srcu_struct structures might already have srcu_read_lock() and
  101. * srcu_read_unlock() running against them. So if the is_static parameter
  102. * is set, don't initialize ->srcu_lock_count[] and ->srcu_unlock_count[].
  103. */
  104. static void init_srcu_struct_data(struct srcu_struct *ssp)
  105. {
  106. int cpu;
  107. struct srcu_data *sdp;
  108. /*
  109. * Initialize the per-CPU srcu_data array, which feeds into the
  110. * leaves of the srcu_node tree.
  111. */
  112. WARN_ON_ONCE(ARRAY_SIZE(sdp->srcu_lock_count) !=
  113. ARRAY_SIZE(sdp->srcu_unlock_count));
  114. for_each_possible_cpu(cpu) {
  115. sdp = per_cpu_ptr(ssp->sda, cpu);
  116. spin_lock_init(&ACCESS_PRIVATE(sdp, lock));
  117. rcu_segcblist_init(&sdp->srcu_cblist);
  118. sdp->srcu_cblist_invoking = false;
  119. sdp->srcu_gp_seq_needed = ssp->srcu_sup->srcu_gp_seq;
  120. sdp->srcu_gp_seq_needed_exp = ssp->srcu_sup->srcu_gp_seq;
  121. sdp->srcu_barrier_head.next = &sdp->srcu_barrier_head;
  122. sdp->mynode = NULL;
  123. sdp->cpu = cpu;
  124. INIT_WORK(&sdp->work, srcu_invoke_callbacks);
  125. timer_setup(&sdp->delay_work, srcu_delay_timer, 0);
  126. sdp->ssp = ssp;
  127. }
  128. }
  129. /* Invalid seq state, used during snp node initialization */
  130. #define SRCU_SNP_INIT_SEQ 0x2
  131. /*
  132. * Check whether sequence number corresponding to snp node,
  133. * is invalid.
  134. */
  135. static inline bool srcu_invl_snp_seq(unsigned long s)
  136. {
  137. return s == SRCU_SNP_INIT_SEQ;
  138. }
  139. /*
  140. * Allocated and initialize SRCU combining tree. Returns @true if
  141. * allocation succeeded and @false otherwise.
  142. */
  143. static bool init_srcu_struct_nodes(struct srcu_struct *ssp, gfp_t gfp_flags)
  144. {
  145. int cpu;
  146. int i;
  147. int level = 0;
  148. int levelspread[RCU_NUM_LVLS];
  149. struct srcu_data *sdp;
  150. struct srcu_node *snp;
  151. struct srcu_node *snp_first;
  152. /* Initialize geometry if it has not already been initialized. */
  153. rcu_init_geometry();
  154. ssp->srcu_sup->node = kcalloc(rcu_num_nodes, sizeof(*ssp->srcu_sup->node), gfp_flags);
  155. if (!ssp->srcu_sup->node)
  156. return false;
  157. /* Work out the overall tree geometry. */
  158. ssp->srcu_sup->level[0] = &ssp->srcu_sup->node[0];
  159. for (i = 1; i < rcu_num_lvls; i++)
  160. ssp->srcu_sup->level[i] = ssp->srcu_sup->level[i - 1] + num_rcu_lvl[i - 1];
  161. rcu_init_levelspread(levelspread, num_rcu_lvl);
  162. /* Each pass through this loop initializes one srcu_node structure. */
  163. srcu_for_each_node_breadth_first(ssp, snp) {
  164. spin_lock_init(&ACCESS_PRIVATE(snp, lock));
  165. WARN_ON_ONCE(ARRAY_SIZE(snp->srcu_have_cbs) !=
  166. ARRAY_SIZE(snp->srcu_data_have_cbs));
  167. for (i = 0; i < ARRAY_SIZE(snp->srcu_have_cbs); i++) {
  168. snp->srcu_have_cbs[i] = SRCU_SNP_INIT_SEQ;
  169. snp->srcu_data_have_cbs[i] = 0;
  170. }
  171. snp->srcu_gp_seq_needed_exp = SRCU_SNP_INIT_SEQ;
  172. snp->grplo = -1;
  173. snp->grphi = -1;
  174. if (snp == &ssp->srcu_sup->node[0]) {
  175. /* Root node, special case. */
  176. snp->srcu_parent = NULL;
  177. continue;
  178. }
  179. /* Non-root node. */
  180. if (snp == ssp->srcu_sup->level[level + 1])
  181. level++;
  182. snp->srcu_parent = ssp->srcu_sup->level[level - 1] +
  183. (snp - ssp->srcu_sup->level[level]) /
  184. levelspread[level - 1];
  185. }
  186. /*
  187. * Initialize the per-CPU srcu_data array, which feeds into the
  188. * leaves of the srcu_node tree.
  189. */
  190. level = rcu_num_lvls - 1;
  191. snp_first = ssp->srcu_sup->level[level];
  192. for_each_possible_cpu(cpu) {
  193. sdp = per_cpu_ptr(ssp->sda, cpu);
  194. sdp->mynode = &snp_first[cpu / levelspread[level]];
  195. for (snp = sdp->mynode; snp != NULL; snp = snp->srcu_parent) {
  196. if (snp->grplo < 0)
  197. snp->grplo = cpu;
  198. snp->grphi = cpu;
  199. }
  200. sdp->grpmask = 1UL << (cpu - sdp->mynode->grplo);
  201. }
  202. smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_WAIT_BARRIER);
  203. return true;
  204. }
  205. /*
  206. * Initialize non-compile-time initialized fields, including the
  207. * associated srcu_node and srcu_data structures. The is_static parameter
  208. * tells us that ->sda has already been wired up to srcu_data.
  209. */
  210. static int init_srcu_struct_fields(struct srcu_struct *ssp, bool is_static)
  211. {
  212. if (!is_static)
  213. ssp->srcu_sup = kzalloc(sizeof(*ssp->srcu_sup), GFP_KERNEL);
  214. if (!ssp->srcu_sup)
  215. return -ENOMEM;
  216. if (!is_static)
  217. spin_lock_init(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
  218. ssp->srcu_sup->srcu_size_state = SRCU_SIZE_SMALL;
  219. ssp->srcu_sup->node = NULL;
  220. mutex_init(&ssp->srcu_sup->srcu_cb_mutex);
  221. mutex_init(&ssp->srcu_sup->srcu_gp_mutex);
  222. ssp->srcu_idx = 0;
  223. ssp->srcu_sup->srcu_gp_seq = SRCU_GP_SEQ_INITIAL_VAL;
  224. ssp->srcu_sup->srcu_barrier_seq = 0;
  225. mutex_init(&ssp->srcu_sup->srcu_barrier_mutex);
  226. atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 0);
  227. INIT_DELAYED_WORK(&ssp->srcu_sup->work, process_srcu);
  228. ssp->srcu_sup->sda_is_static = is_static;
  229. if (!is_static)
  230. ssp->sda = alloc_percpu(struct srcu_data);
  231. if (!ssp->sda)
  232. goto err_free_sup;
  233. init_srcu_struct_data(ssp);
  234. ssp->srcu_sup->srcu_gp_seq_needed_exp = SRCU_GP_SEQ_INITIAL_VAL;
  235. ssp->srcu_sup->srcu_last_gp_end = ktime_get_mono_fast_ns();
  236. if (READ_ONCE(ssp->srcu_sup->srcu_size_state) == SRCU_SIZE_SMALL && SRCU_SIZING_IS_INIT()) {
  237. if (!init_srcu_struct_nodes(ssp, GFP_ATOMIC))
  238. goto err_free_sda;
  239. WRITE_ONCE(ssp->srcu_sup->srcu_size_state, SRCU_SIZE_BIG);
  240. }
  241. ssp->srcu_sup->srcu_ssp = ssp;
  242. smp_store_release(&ssp->srcu_sup->srcu_gp_seq_needed,
  243. SRCU_GP_SEQ_INITIAL_VAL); /* Init done. */
  244. return 0;
  245. err_free_sda:
  246. if (!is_static) {
  247. free_percpu(ssp->sda);
  248. ssp->sda = NULL;
  249. }
  250. err_free_sup:
  251. if (!is_static) {
  252. kfree(ssp->srcu_sup);
  253. ssp->srcu_sup = NULL;
  254. }
  255. return -ENOMEM;
  256. }
  257. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  258. int __init_srcu_struct(struct srcu_struct *ssp, const char *name,
  259. struct lock_class_key *key)
  260. {
  261. /* Don't re-initialize a lock while it is held. */
  262. debug_check_no_locks_freed((void *)ssp, sizeof(*ssp));
  263. lockdep_init_map(&ssp->dep_map, name, key, 0);
  264. return init_srcu_struct_fields(ssp, false);
  265. }
  266. EXPORT_SYMBOL_GPL(__init_srcu_struct);
  267. #else /* #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  268. /**
  269. * init_srcu_struct - initialize a sleep-RCU structure
  270. * @ssp: structure to initialize.
  271. *
  272. * Must invoke this on a given srcu_struct before passing that srcu_struct
  273. * to any other function. Each srcu_struct represents a separate domain
  274. * of SRCU protection.
  275. */
  276. int init_srcu_struct(struct srcu_struct *ssp)
  277. {
  278. return init_srcu_struct_fields(ssp, false);
  279. }
  280. EXPORT_SYMBOL_GPL(init_srcu_struct);
  281. #endif /* #else #ifdef CONFIG_DEBUG_LOCK_ALLOC */
  282. /*
  283. * Initiate a transition to SRCU_SIZE_BIG with lock held.
  284. */
  285. static void __srcu_transition_to_big(struct srcu_struct *ssp)
  286. {
  287. lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
  288. smp_store_release(&ssp->srcu_sup->srcu_size_state, SRCU_SIZE_ALLOC);
  289. }
  290. /*
  291. * Initiate an idempotent transition to SRCU_SIZE_BIG.
  292. */
  293. static void srcu_transition_to_big(struct srcu_struct *ssp)
  294. {
  295. unsigned long flags;
  296. /* Double-checked locking on ->srcu_size-state. */
  297. if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL)
  298. return;
  299. spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);
  300. if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) != SRCU_SIZE_SMALL) {
  301. spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
  302. return;
  303. }
  304. __srcu_transition_to_big(ssp);
  305. spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
  306. }
  307. /*
  308. * Check to see if the just-encountered contention event justifies
  309. * a transition to SRCU_SIZE_BIG.
  310. */
  311. static void spin_lock_irqsave_check_contention(struct srcu_struct *ssp)
  312. {
  313. unsigned long j;
  314. if (!SRCU_SIZING_IS_CONTEND() || ssp->srcu_sup->srcu_size_state)
  315. return;
  316. j = jiffies;
  317. if (ssp->srcu_sup->srcu_size_jiffies != j) {
  318. ssp->srcu_sup->srcu_size_jiffies = j;
  319. ssp->srcu_sup->srcu_n_lock_retries = 0;
  320. }
  321. if (++ssp->srcu_sup->srcu_n_lock_retries <= small_contention_lim)
  322. return;
  323. __srcu_transition_to_big(ssp);
  324. }
  325. /*
  326. * Acquire the specified srcu_data structure's ->lock, but check for
  327. * excessive contention, which results in initiation of a transition
  328. * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module
  329. * parameter permits this.
  330. */
  331. static void spin_lock_irqsave_sdp_contention(struct srcu_data *sdp, unsigned long *flags)
  332. {
  333. struct srcu_struct *ssp = sdp->ssp;
  334. if (spin_trylock_irqsave_rcu_node(sdp, *flags))
  335. return;
  336. spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);
  337. spin_lock_irqsave_check_contention(ssp);
  338. spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, *flags);
  339. spin_lock_irqsave_rcu_node(sdp, *flags);
  340. }
  341. /*
  342. * Acquire the specified srcu_struct structure's ->lock, but check for
  343. * excessive contention, which results in initiation of a transition
  344. * to SRCU_SIZE_BIG. But only if the srcutree.convert_to_big module
  345. * parameter permits this.
  346. */
  347. static void spin_lock_irqsave_ssp_contention(struct srcu_struct *ssp, unsigned long *flags)
  348. {
  349. if (spin_trylock_irqsave_rcu_node(ssp->srcu_sup, *flags))
  350. return;
  351. spin_lock_irqsave_rcu_node(ssp->srcu_sup, *flags);
  352. spin_lock_irqsave_check_contention(ssp);
  353. }
  354. /*
  355. * First-use initialization of statically allocated srcu_struct
  356. * structure. Wiring up the combining tree is more than can be
  357. * done with compile-time initialization, so this check is added
  358. * to each update-side SRCU primitive. Use ssp->lock, which -is-
  359. * compile-time initialized, to resolve races involving multiple
  360. * CPUs trying to garner first-use privileges.
  361. */
  362. static void check_init_srcu_struct(struct srcu_struct *ssp)
  363. {
  364. unsigned long flags;
  365. /* The smp_load_acquire() pairs with the smp_store_release(). */
  366. if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed))) /*^^^*/
  367. return; /* Already initialized. */
  368. spin_lock_irqsave_rcu_node(ssp->srcu_sup, flags);
  369. if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq_needed)) {
  370. spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
  371. return;
  372. }
  373. init_srcu_struct_fields(ssp, true);
  374. spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
  375. }
  376. /*
  377. * Returns approximate total of the readers' ->srcu_lock_count[] values
  378. * for the rank of per-CPU counters specified by idx.
  379. */
  380. static unsigned long srcu_readers_lock_idx(struct srcu_struct *ssp, int idx)
  381. {
  382. int cpu;
  383. unsigned long sum = 0;
  384. for_each_possible_cpu(cpu) {
  385. struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
  386. sum += atomic_long_read(&cpuc->srcu_lock_count[idx]);
  387. }
  388. return sum;
  389. }
  390. /*
  391. * Returns approximate total of the readers' ->srcu_unlock_count[] values
  392. * for the rank of per-CPU counters specified by idx.
  393. */
  394. static unsigned long srcu_readers_unlock_idx(struct srcu_struct *ssp, int idx)
  395. {
  396. int cpu;
  397. unsigned long mask = 0;
  398. unsigned long sum = 0;
  399. for_each_possible_cpu(cpu) {
  400. struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
  401. sum += atomic_long_read(&cpuc->srcu_unlock_count[idx]);
  402. if (IS_ENABLED(CONFIG_PROVE_RCU))
  403. mask = mask | READ_ONCE(cpuc->srcu_nmi_safety);
  404. }
  405. WARN_ONCE(IS_ENABLED(CONFIG_PROVE_RCU) && (mask & (mask >> 1)),
  406. "Mixed NMI-safe readers for srcu_struct at %ps.\n", ssp);
  407. return sum;
  408. }
  409. /*
  410. * Return true if the number of pre-existing readers is determined to
  411. * be zero.
  412. */
  413. static bool srcu_readers_active_idx_check(struct srcu_struct *ssp, int idx)
  414. {
  415. unsigned long unlocks;
  416. unlocks = srcu_readers_unlock_idx(ssp, idx);
  417. /*
  418. * Make sure that a lock is always counted if the corresponding
  419. * unlock is counted. Needs to be a smp_mb() as the read side may
  420. * contain a read from a variable that is written to before the
  421. * synchronize_srcu() in the write side. In this case smp_mb()s
  422. * A and B act like the store buffering pattern.
  423. *
  424. * This smp_mb() also pairs with smp_mb() C to prevent accesses
  425. * after the synchronize_srcu() from being executed before the
  426. * grace period ends.
  427. */
  428. smp_mb(); /* A */
  429. /*
  430. * If the locks are the same as the unlocks, then there must have
  431. * been no readers on this index at some point in this function.
  432. * But there might be more readers, as a task might have read
  433. * the current ->srcu_idx but not yet have incremented its CPU's
  434. * ->srcu_lock_count[idx] counter. In fact, it is possible
  435. * that most of the tasks have been preempted between fetching
  436. * ->srcu_idx and incrementing ->srcu_lock_count[idx]. And there
  437. * could be almost (ULONG_MAX / sizeof(struct task_struct)) tasks
  438. * in a system whose address space was fully populated with memory.
  439. * Call this quantity Nt.
  440. *
  441. * So suppose that the updater is preempted at this point in the
  442. * code for a long time. That now-preempted updater has already
  443. * flipped ->srcu_idx (possibly during the preceding grace period),
  444. * done an smp_mb() (again, possibly during the preceding grace
  445. * period), and summed up the ->srcu_unlock_count[idx] counters.
  446. * How many times can a given one of the aforementioned Nt tasks
  447. * increment the old ->srcu_idx value's ->srcu_lock_count[idx]
  448. * counter, in the absence of nesting?
  449. *
  450. * It can clearly do so once, given that it has already fetched
  451. * the old value of ->srcu_idx and is just about to use that value
  452. * to index its increment of ->srcu_lock_count[idx]. But as soon as
  453. * it leaves that SRCU read-side critical section, it will increment
  454. * ->srcu_unlock_count[idx], which must follow the updater's above
  455. * read from that same value. Thus, as soon the reading task does
  456. * an smp_mb() and a later fetch from ->srcu_idx, that task will be
  457. * guaranteed to get the new index. Except that the increment of
  458. * ->srcu_unlock_count[idx] in __srcu_read_unlock() is after the
  459. * smp_mb(), and the fetch from ->srcu_idx in __srcu_read_lock()
  460. * is before the smp_mb(). Thus, that task might not see the new
  461. * value of ->srcu_idx until the -second- __srcu_read_lock(),
  462. * which in turn means that this task might well increment
  463. * ->srcu_lock_count[idx] for the old value of ->srcu_idx twice,
  464. * not just once.
  465. *
  466. * However, it is important to note that a given smp_mb() takes
  467. * effect not just for the task executing it, but also for any
  468. * later task running on that same CPU.
  469. *
  470. * That is, there can be almost Nt + Nc further increments of
  471. * ->srcu_lock_count[idx] for the old index, where Nc is the number
  472. * of CPUs. But this is OK because the size of the task_struct
  473. * structure limits the value of Nt and current systems limit Nc
  474. * to a few thousand.
  475. *
  476. * OK, but what about nesting? This does impose a limit on
  477. * nesting of half of the size of the task_struct structure
  478. * (measured in bytes), which should be sufficient. A late 2022
  479. * TREE01 rcutorture run reported this size to be no less than
  480. * 9408 bytes, allowing up to 4704 levels of nesting, which is
  481. * comfortably beyond excessive. Especially on 64-bit systems,
  482. * which are unlikely to be configured with an address space fully
  483. * populated with memory, at least not anytime soon.
  484. */
  485. return srcu_readers_lock_idx(ssp, idx) == unlocks;
  486. }
  487. /**
  488. * srcu_readers_active - returns true if there are readers. and false
  489. * otherwise
  490. * @ssp: which srcu_struct to count active readers (holding srcu_read_lock).
  491. *
  492. * Note that this is not an atomic primitive, and can therefore suffer
  493. * severe errors when invoked on an active srcu_struct. That said, it
  494. * can be useful as an error check at cleanup time.
  495. */
  496. static bool srcu_readers_active(struct srcu_struct *ssp)
  497. {
  498. int cpu;
  499. unsigned long sum = 0;
  500. for_each_possible_cpu(cpu) {
  501. struct srcu_data *cpuc = per_cpu_ptr(ssp->sda, cpu);
  502. sum += atomic_long_read(&cpuc->srcu_lock_count[0]);
  503. sum += atomic_long_read(&cpuc->srcu_lock_count[1]);
  504. sum -= atomic_long_read(&cpuc->srcu_unlock_count[0]);
  505. sum -= atomic_long_read(&cpuc->srcu_unlock_count[1]);
  506. }
  507. return sum;
  508. }
  509. /*
  510. * We use an adaptive strategy for synchronize_srcu() and especially for
  511. * synchronize_srcu_expedited(). We spin for a fixed time period
  512. * (defined below, boot time configurable) to allow SRCU readers to exit
  513. * their read-side critical sections. If there are still some readers
  514. * after one jiffy, we repeatedly block for one jiffy time periods.
  515. * The blocking time is increased as the grace-period age increases,
  516. * with max blocking time capped at 10 jiffies.
  517. */
  518. #define SRCU_DEFAULT_RETRY_CHECK_DELAY 5
  519. static ulong srcu_retry_check_delay = SRCU_DEFAULT_RETRY_CHECK_DELAY;
  520. module_param(srcu_retry_check_delay, ulong, 0444);
  521. #define SRCU_INTERVAL 1 // Base delay if no expedited GPs pending.
  522. #define SRCU_MAX_INTERVAL 10 // Maximum incremental delay from slow readers.
  523. #define SRCU_DEFAULT_MAX_NODELAY_PHASE_LO 3UL // Lowmark on default per-GP-phase
  524. // no-delay instances.
  525. #define SRCU_DEFAULT_MAX_NODELAY_PHASE_HI 1000UL // Highmark on default per-GP-phase
  526. // no-delay instances.
  527. #define SRCU_UL_CLAMP_LO(val, low) ((val) > (low) ? (val) : (low))
  528. #define SRCU_UL_CLAMP_HI(val, high) ((val) < (high) ? (val) : (high))
  529. #define SRCU_UL_CLAMP(val, low, high) SRCU_UL_CLAMP_HI(SRCU_UL_CLAMP_LO((val), (low)), (high))
  530. // per-GP-phase no-delay instances adjusted to allow non-sleeping poll upto
  531. // one jiffies time duration. Mult by 2 is done to factor in the srcu_get_delay()
  532. // called from process_srcu().
  533. #define SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED \
  534. (2UL * USEC_PER_SEC / HZ / SRCU_DEFAULT_RETRY_CHECK_DELAY)
  535. // Maximum per-GP-phase consecutive no-delay instances.
  536. #define SRCU_DEFAULT_MAX_NODELAY_PHASE \
  537. SRCU_UL_CLAMP(SRCU_DEFAULT_MAX_NODELAY_PHASE_ADJUSTED, \
  538. SRCU_DEFAULT_MAX_NODELAY_PHASE_LO, \
  539. SRCU_DEFAULT_MAX_NODELAY_PHASE_HI)
  540. static ulong srcu_max_nodelay_phase = SRCU_DEFAULT_MAX_NODELAY_PHASE;
  541. module_param(srcu_max_nodelay_phase, ulong, 0444);
  542. // Maximum consecutive no-delay instances.
  543. #define SRCU_DEFAULT_MAX_NODELAY (SRCU_DEFAULT_MAX_NODELAY_PHASE > 100 ? \
  544. SRCU_DEFAULT_MAX_NODELAY_PHASE : 100)
  545. static ulong srcu_max_nodelay = SRCU_DEFAULT_MAX_NODELAY;
  546. module_param(srcu_max_nodelay, ulong, 0444);
  547. /*
  548. * Return grace-period delay, zero if there are expedited grace
  549. * periods pending, SRCU_INTERVAL otherwise.
  550. */
  551. static unsigned long srcu_get_delay(struct srcu_struct *ssp)
  552. {
  553. unsigned long gpstart;
  554. unsigned long j;
  555. unsigned long jbase = SRCU_INTERVAL;
  556. struct srcu_usage *sup = ssp->srcu_sup;
  557. if (ULONG_CMP_LT(READ_ONCE(sup->srcu_gp_seq), READ_ONCE(sup->srcu_gp_seq_needed_exp)))
  558. jbase = 0;
  559. if (rcu_seq_state(READ_ONCE(sup->srcu_gp_seq))) {
  560. j = jiffies - 1;
  561. gpstart = READ_ONCE(sup->srcu_gp_start);
  562. if (time_after(j, gpstart))
  563. jbase += j - gpstart;
  564. if (!jbase) {
  565. ASSERT_EXCLUSIVE_WRITER(sup->srcu_n_exp_nodelay);
  566. WRITE_ONCE(sup->srcu_n_exp_nodelay, READ_ONCE(sup->srcu_n_exp_nodelay) + 1);
  567. if (READ_ONCE(sup->srcu_n_exp_nodelay) > srcu_max_nodelay_phase)
  568. jbase = 1;
  569. }
  570. }
  571. return jbase > SRCU_MAX_INTERVAL ? SRCU_MAX_INTERVAL : jbase;
  572. }
  573. /**
  574. * cleanup_srcu_struct - deconstruct a sleep-RCU structure
  575. * @ssp: structure to clean up.
  576. *
  577. * Must invoke this after you are finished using a given srcu_struct that
  578. * was initialized via init_srcu_struct(), else you leak memory.
  579. */
  580. void cleanup_srcu_struct(struct srcu_struct *ssp)
  581. {
  582. int cpu;
  583. struct srcu_usage *sup = ssp->srcu_sup;
  584. if (WARN_ON(!srcu_get_delay(ssp)))
  585. return; /* Just leak it! */
  586. if (WARN_ON(srcu_readers_active(ssp)))
  587. return; /* Just leak it! */
  588. flush_delayed_work(&sup->work);
  589. for_each_possible_cpu(cpu) {
  590. struct srcu_data *sdp = per_cpu_ptr(ssp->sda, cpu);
  591. del_timer_sync(&sdp->delay_work);
  592. flush_work(&sdp->work);
  593. if (WARN_ON(rcu_segcblist_n_cbs(&sdp->srcu_cblist)))
  594. return; /* Forgot srcu_barrier(), so just leak it! */
  595. }
  596. if (WARN_ON(rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)) != SRCU_STATE_IDLE) ||
  597. WARN_ON(rcu_seq_current(&sup->srcu_gp_seq) != sup->srcu_gp_seq_needed) ||
  598. WARN_ON(srcu_readers_active(ssp))) {
  599. pr_info("%s: Active srcu_struct %p read state: %d gp state: %lu/%lu\n",
  600. __func__, ssp, rcu_seq_state(READ_ONCE(sup->srcu_gp_seq)),
  601. rcu_seq_current(&sup->srcu_gp_seq), sup->srcu_gp_seq_needed);
  602. return; // Caller forgot to stop doing call_srcu()?
  603. // Or caller invoked start_poll_synchronize_srcu()
  604. // and then cleanup_srcu_struct() before that grace
  605. // period ended?
  606. }
  607. kfree(sup->node);
  608. sup->node = NULL;
  609. sup->srcu_size_state = SRCU_SIZE_SMALL;
  610. if (!sup->sda_is_static) {
  611. free_percpu(ssp->sda);
  612. ssp->sda = NULL;
  613. kfree(sup);
  614. ssp->srcu_sup = NULL;
  615. }
  616. }
  617. EXPORT_SYMBOL_GPL(cleanup_srcu_struct);
  618. #ifdef CONFIG_PROVE_RCU
  619. /*
  620. * Check for consistent NMI safety.
  621. */
  622. void srcu_check_nmi_safety(struct srcu_struct *ssp, bool nmi_safe)
  623. {
  624. int nmi_safe_mask = 1 << nmi_safe;
  625. int old_nmi_safe_mask;
  626. struct srcu_data *sdp;
  627. /* NMI-unsafe use in NMI is a bad sign */
  628. WARN_ON_ONCE(!nmi_safe && in_nmi());
  629. sdp = raw_cpu_ptr(ssp->sda);
  630. old_nmi_safe_mask = READ_ONCE(sdp->srcu_nmi_safety);
  631. if (!old_nmi_safe_mask) {
  632. WRITE_ONCE(sdp->srcu_nmi_safety, nmi_safe_mask);
  633. return;
  634. }
  635. WARN_ONCE(old_nmi_safe_mask != nmi_safe_mask, "CPU %d old state %d new state %d\n", sdp->cpu, old_nmi_safe_mask, nmi_safe_mask);
  636. }
  637. EXPORT_SYMBOL_GPL(srcu_check_nmi_safety);
  638. #endif /* CONFIG_PROVE_RCU */
  639. /*
  640. * Counts the new reader in the appropriate per-CPU element of the
  641. * srcu_struct.
  642. * Returns an index that must be passed to the matching srcu_read_unlock().
  643. */
  644. int __srcu_read_lock(struct srcu_struct *ssp)
  645. {
  646. int idx;
  647. idx = READ_ONCE(ssp->srcu_idx) & 0x1;
  648. this_cpu_inc(ssp->sda->srcu_lock_count[idx].counter);
  649. smp_mb(); /* B */ /* Avoid leaking the critical section. */
  650. return idx;
  651. }
  652. EXPORT_SYMBOL_GPL(__srcu_read_lock);
  653. /*
  654. * Removes the count for the old reader from the appropriate per-CPU
  655. * element of the srcu_struct. Note that this may well be a different
  656. * CPU than that which was incremented by the corresponding srcu_read_lock().
  657. */
  658. void __srcu_read_unlock(struct srcu_struct *ssp, int idx)
  659. {
  660. smp_mb(); /* C */ /* Avoid leaking the critical section. */
  661. this_cpu_inc(ssp->sda->srcu_unlock_count[idx].counter);
  662. }
  663. EXPORT_SYMBOL_GPL(__srcu_read_unlock);
  664. #ifdef CONFIG_NEED_SRCU_NMI_SAFE
  665. /*
  666. * Counts the new reader in the appropriate per-CPU element of the
  667. * srcu_struct, but in an NMI-safe manner using RMW atomics.
  668. * Returns an index that must be passed to the matching srcu_read_unlock().
  669. */
  670. int __srcu_read_lock_nmisafe(struct srcu_struct *ssp)
  671. {
  672. int idx;
  673. struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
  674. idx = READ_ONCE(ssp->srcu_idx) & 0x1;
  675. atomic_long_inc(&sdp->srcu_lock_count[idx]);
  676. smp_mb__after_atomic(); /* B */ /* Avoid leaking the critical section. */
  677. return idx;
  678. }
  679. EXPORT_SYMBOL_GPL(__srcu_read_lock_nmisafe);
  680. /*
  681. * Removes the count for the old reader from the appropriate per-CPU
  682. * element of the srcu_struct. Note that this may well be a different
  683. * CPU than that which was incremented by the corresponding srcu_read_lock().
  684. */
  685. void __srcu_read_unlock_nmisafe(struct srcu_struct *ssp, int idx)
  686. {
  687. struct srcu_data *sdp = raw_cpu_ptr(ssp->sda);
  688. smp_mb__before_atomic(); /* C */ /* Avoid leaking the critical section. */
  689. atomic_long_inc(&sdp->srcu_unlock_count[idx]);
  690. }
  691. EXPORT_SYMBOL_GPL(__srcu_read_unlock_nmisafe);
  692. #endif // CONFIG_NEED_SRCU_NMI_SAFE
  693. /*
  694. * Start an SRCU grace period.
  695. */
  696. static void srcu_gp_start(struct srcu_struct *ssp)
  697. {
  698. int state;
  699. lockdep_assert_held(&ACCESS_PRIVATE(ssp->srcu_sup, lock));
  700. WARN_ON_ONCE(ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed));
  701. WRITE_ONCE(ssp->srcu_sup->srcu_gp_start, jiffies);
  702. WRITE_ONCE(ssp->srcu_sup->srcu_n_exp_nodelay, 0);
  703. smp_mb(); /* Order prior store to ->srcu_gp_seq_needed vs. GP start. */
  704. rcu_seq_start(&ssp->srcu_sup->srcu_gp_seq);
  705. state = rcu_seq_state(ssp->srcu_sup->srcu_gp_seq);
  706. WARN_ON_ONCE(state != SRCU_STATE_SCAN1);
  707. }
  708. static void srcu_delay_timer(struct timer_list *t)
  709. {
  710. struct srcu_data *sdp = container_of(t, struct srcu_data, delay_work);
  711. queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
  712. }
  713. static void srcu_queue_delayed_work_on(struct srcu_data *sdp,
  714. unsigned long delay)
  715. {
  716. if (!delay) {
  717. queue_work_on(sdp->cpu, rcu_gp_wq, &sdp->work);
  718. return;
  719. }
  720. timer_reduce(&sdp->delay_work, jiffies + delay);
  721. }
  722. /*
  723. * Schedule callback invocation for the specified srcu_data structure,
  724. * if possible, on the corresponding CPU.
  725. */
  726. static void srcu_schedule_cbs_sdp(struct srcu_data *sdp, unsigned long delay)
  727. {
  728. srcu_queue_delayed_work_on(sdp, delay);
  729. }
  730. /*
  731. * Schedule callback invocation for all srcu_data structures associated
  732. * with the specified srcu_node structure that have callbacks for the
  733. * just-completed grace period, the one corresponding to idx. If possible,
  734. * schedule this invocation on the corresponding CPUs.
  735. */
  736. static void srcu_schedule_cbs_snp(struct srcu_struct *ssp, struct srcu_node *snp,
  737. unsigned long mask, unsigned long delay)
  738. {
  739. int cpu;
  740. for (cpu = snp->grplo; cpu <= snp->grphi; cpu++) {
  741. if (!(mask & (1UL << (cpu - snp->grplo))))
  742. continue;
  743. srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, cpu), delay);
  744. }
  745. }
  746. /*
  747. * Note the end of an SRCU grace period. Initiates callback invocation
  748. * and starts a new grace period if needed.
  749. *
  750. * The ->srcu_cb_mutex acquisition does not protect any data, but
  751. * instead prevents more than one grace period from starting while we
  752. * are initiating callback invocation. This allows the ->srcu_have_cbs[]
  753. * array to have a finite number of elements.
  754. */
  755. static void srcu_gp_end(struct srcu_struct *ssp)
  756. {
  757. unsigned long cbdelay = 1;
  758. bool cbs;
  759. bool last_lvl;
  760. int cpu;
  761. unsigned long gpseq;
  762. int idx;
  763. unsigned long mask;
  764. struct srcu_data *sdp;
  765. unsigned long sgsne;
  766. struct srcu_node *snp;
  767. int ss_state;
  768. struct srcu_usage *sup = ssp->srcu_sup;
  769. /* Prevent more than one additional grace period. */
  770. mutex_lock(&sup->srcu_cb_mutex);
  771. /* End the current grace period. */
  772. spin_lock_irq_rcu_node(sup);
  773. idx = rcu_seq_state(sup->srcu_gp_seq);
  774. WARN_ON_ONCE(idx != SRCU_STATE_SCAN2);
  775. if (ULONG_CMP_LT(READ_ONCE(sup->srcu_gp_seq), READ_ONCE(sup->srcu_gp_seq_needed_exp)))
  776. cbdelay = 0;
  777. WRITE_ONCE(sup->srcu_last_gp_end, ktime_get_mono_fast_ns());
  778. rcu_seq_end(&sup->srcu_gp_seq);
  779. gpseq = rcu_seq_current(&sup->srcu_gp_seq);
  780. if (ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, gpseq))
  781. WRITE_ONCE(sup->srcu_gp_seq_needed_exp, gpseq);
  782. spin_unlock_irq_rcu_node(sup);
  783. mutex_unlock(&sup->srcu_gp_mutex);
  784. /* A new grace period can start at this point. But only one. */
  785. /* Initiate callback invocation as needed. */
  786. ss_state = smp_load_acquire(&sup->srcu_size_state);
  787. if (ss_state < SRCU_SIZE_WAIT_BARRIER) {
  788. srcu_schedule_cbs_sdp(per_cpu_ptr(ssp->sda, get_boot_cpu_id()),
  789. cbdelay);
  790. } else {
  791. idx = rcu_seq_ctr(gpseq) % ARRAY_SIZE(snp->srcu_have_cbs);
  792. srcu_for_each_node_breadth_first(ssp, snp) {
  793. spin_lock_irq_rcu_node(snp);
  794. cbs = false;
  795. last_lvl = snp >= sup->level[rcu_num_lvls - 1];
  796. if (last_lvl)
  797. cbs = ss_state < SRCU_SIZE_BIG || snp->srcu_have_cbs[idx] == gpseq;
  798. snp->srcu_have_cbs[idx] = gpseq;
  799. rcu_seq_set_state(&snp->srcu_have_cbs[idx], 1);
  800. sgsne = snp->srcu_gp_seq_needed_exp;
  801. if (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, gpseq))
  802. WRITE_ONCE(snp->srcu_gp_seq_needed_exp, gpseq);
  803. if (ss_state < SRCU_SIZE_BIG)
  804. mask = ~0;
  805. else
  806. mask = snp->srcu_data_have_cbs[idx];
  807. snp->srcu_data_have_cbs[idx] = 0;
  808. spin_unlock_irq_rcu_node(snp);
  809. if (cbs)
  810. srcu_schedule_cbs_snp(ssp, snp, mask, cbdelay);
  811. }
  812. }
  813. /* Occasionally prevent srcu_data counter wrap. */
  814. if (!(gpseq & counter_wrap_check))
  815. for_each_possible_cpu(cpu) {
  816. sdp = per_cpu_ptr(ssp->sda, cpu);
  817. spin_lock_irq_rcu_node(sdp);
  818. if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed + 100))
  819. sdp->srcu_gp_seq_needed = gpseq;
  820. if (ULONG_CMP_GE(gpseq, sdp->srcu_gp_seq_needed_exp + 100))
  821. sdp->srcu_gp_seq_needed_exp = gpseq;
  822. spin_unlock_irq_rcu_node(sdp);
  823. }
  824. /* Callback initiation done, allow grace periods after next. */
  825. mutex_unlock(&sup->srcu_cb_mutex);
  826. /* Start a new grace period if needed. */
  827. spin_lock_irq_rcu_node(sup);
  828. gpseq = rcu_seq_current(&sup->srcu_gp_seq);
  829. if (!rcu_seq_state(gpseq) &&
  830. ULONG_CMP_LT(gpseq, sup->srcu_gp_seq_needed)) {
  831. srcu_gp_start(ssp);
  832. spin_unlock_irq_rcu_node(sup);
  833. srcu_reschedule(ssp, 0);
  834. } else {
  835. spin_unlock_irq_rcu_node(sup);
  836. }
  837. /* Transition to big if needed. */
  838. if (ss_state != SRCU_SIZE_SMALL && ss_state != SRCU_SIZE_BIG) {
  839. if (ss_state == SRCU_SIZE_ALLOC)
  840. init_srcu_struct_nodes(ssp, GFP_KERNEL);
  841. else
  842. smp_store_release(&sup->srcu_size_state, ss_state + 1);
  843. }
  844. }
  845. /*
  846. * Funnel-locking scheme to scalably mediate many concurrent expedited
  847. * grace-period requests. This function is invoked for the first known
  848. * expedited request for a grace period that has already been requested,
  849. * but without expediting. To start a completely new grace period,
  850. * whether expedited or not, use srcu_funnel_gp_start() instead.
  851. */
  852. static void srcu_funnel_exp_start(struct srcu_struct *ssp, struct srcu_node *snp,
  853. unsigned long s)
  854. {
  855. unsigned long flags;
  856. unsigned long sgsne;
  857. if (snp)
  858. for (; snp != NULL; snp = snp->srcu_parent) {
  859. sgsne = READ_ONCE(snp->srcu_gp_seq_needed_exp);
  860. if (WARN_ON_ONCE(rcu_seq_done(&ssp->srcu_sup->srcu_gp_seq, s)) ||
  861. (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)))
  862. return;
  863. spin_lock_irqsave_rcu_node(snp, flags);
  864. sgsne = snp->srcu_gp_seq_needed_exp;
  865. if (!srcu_invl_snp_seq(sgsne) && ULONG_CMP_GE(sgsne, s)) {
  866. spin_unlock_irqrestore_rcu_node(snp, flags);
  867. return;
  868. }
  869. WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
  870. spin_unlock_irqrestore_rcu_node(snp, flags);
  871. }
  872. spin_lock_irqsave_ssp_contention(ssp, &flags);
  873. if (ULONG_CMP_LT(ssp->srcu_sup->srcu_gp_seq_needed_exp, s))
  874. WRITE_ONCE(ssp->srcu_sup->srcu_gp_seq_needed_exp, s);
  875. spin_unlock_irqrestore_rcu_node(ssp->srcu_sup, flags);
  876. }
  877. /*
  878. * Funnel-locking scheme to scalably mediate many concurrent grace-period
  879. * requests. The winner has to do the work of actually starting grace
  880. * period s. Losers must either ensure that their desired grace-period
  881. * number is recorded on at least their leaf srcu_node structure, or they
  882. * must take steps to invoke their own callbacks.
  883. *
  884. * Note that this function also does the work of srcu_funnel_exp_start(),
  885. * in some cases by directly invoking it.
  886. *
  887. * The srcu read lock should be hold around this function. And s is a seq snap
  888. * after holding that lock.
  889. */
  890. static void srcu_funnel_gp_start(struct srcu_struct *ssp, struct srcu_data *sdp,
  891. unsigned long s, bool do_norm)
  892. {
  893. unsigned long flags;
  894. int idx = rcu_seq_ctr(s) % ARRAY_SIZE(sdp->mynode->srcu_have_cbs);
  895. unsigned long sgsne;
  896. struct srcu_node *snp;
  897. struct srcu_node *snp_leaf;
  898. unsigned long snp_seq;
  899. struct srcu_usage *sup = ssp->srcu_sup;
  900. /* Ensure that snp node tree is fully initialized before traversing it */
  901. if (smp_load_acquire(&sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
  902. snp_leaf = NULL;
  903. else
  904. snp_leaf = sdp->mynode;
  905. if (snp_leaf)
  906. /* Each pass through the loop does one level of the srcu_node tree. */
  907. for (snp = snp_leaf; snp != NULL; snp = snp->srcu_parent) {
  908. if (WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) && snp != snp_leaf)
  909. return; /* GP already done and CBs recorded. */
  910. spin_lock_irqsave_rcu_node(snp, flags);
  911. snp_seq = snp->srcu_have_cbs[idx];
  912. if (!srcu_invl_snp_seq(snp_seq) && ULONG_CMP_GE(snp_seq, s)) {
  913. if (snp == snp_leaf && snp_seq == s)
  914. snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
  915. spin_unlock_irqrestore_rcu_node(snp, flags);
  916. if (snp == snp_leaf && snp_seq != s) {
  917. srcu_schedule_cbs_sdp(sdp, do_norm ? SRCU_INTERVAL : 0);
  918. return;
  919. }
  920. if (!do_norm)
  921. srcu_funnel_exp_start(ssp, snp, s);
  922. return;
  923. }
  924. snp->srcu_have_cbs[idx] = s;
  925. if (snp == snp_leaf)
  926. snp->srcu_data_have_cbs[idx] |= sdp->grpmask;
  927. sgsne = snp->srcu_gp_seq_needed_exp;
  928. if (!do_norm && (srcu_invl_snp_seq(sgsne) || ULONG_CMP_LT(sgsne, s)))
  929. WRITE_ONCE(snp->srcu_gp_seq_needed_exp, s);
  930. spin_unlock_irqrestore_rcu_node(snp, flags);
  931. }
  932. /* Top of tree, must ensure the grace period will be started. */
  933. spin_lock_irqsave_ssp_contention(ssp, &flags);
  934. if (ULONG_CMP_LT(sup->srcu_gp_seq_needed, s)) {
  935. /*
  936. * Record need for grace period s. Pair with load
  937. * acquire setting up for initialization.
  938. */
  939. smp_store_release(&sup->srcu_gp_seq_needed, s); /*^^^*/
  940. }
  941. if (!do_norm && ULONG_CMP_LT(sup->srcu_gp_seq_needed_exp, s))
  942. WRITE_ONCE(sup->srcu_gp_seq_needed_exp, s);
  943. /* If grace period not already in progress, start it. */
  944. if (!WARN_ON_ONCE(rcu_seq_done(&sup->srcu_gp_seq, s)) &&
  945. rcu_seq_state(sup->srcu_gp_seq) == SRCU_STATE_IDLE) {
  946. WARN_ON_ONCE(ULONG_CMP_GE(sup->srcu_gp_seq, sup->srcu_gp_seq_needed));
  947. srcu_gp_start(ssp);
  948. // And how can that list_add() in the "else" clause
  949. // possibly be safe for concurrent execution? Well,
  950. // it isn't. And it does not have to be. After all, it
  951. // can only be executed during early boot when there is only
  952. // the one boot CPU running with interrupts still disabled.
  953. if (likely(srcu_init_done))
  954. queue_delayed_work(rcu_gp_wq, &sup->work,
  955. !!srcu_get_delay(ssp));
  956. else if (list_empty(&sup->work.work.entry))
  957. list_add(&sup->work.work.entry, &srcu_boot_list);
  958. }
  959. spin_unlock_irqrestore_rcu_node(sup, flags);
  960. }
  961. /*
  962. * Wait until all readers counted by array index idx complete, but
  963. * loop an additional time if there is an expedited grace period pending.
  964. * The caller must ensure that ->srcu_idx is not changed while checking.
  965. */
  966. static bool try_check_zero(struct srcu_struct *ssp, int idx, int trycount)
  967. {
  968. unsigned long curdelay;
  969. curdelay = !srcu_get_delay(ssp);
  970. for (;;) {
  971. if (srcu_readers_active_idx_check(ssp, idx))
  972. return true;
  973. if ((--trycount + curdelay) <= 0)
  974. return false;
  975. udelay(srcu_retry_check_delay);
  976. }
  977. }
  978. /*
  979. * Increment the ->srcu_idx counter so that future SRCU readers will
  980. * use the other rank of the ->srcu_(un)lock_count[] arrays. This allows
  981. * us to wait for pre-existing readers in a starvation-free manner.
  982. */
  983. static void srcu_flip(struct srcu_struct *ssp)
  984. {
  985. /*
  986. * Because the flip of ->srcu_idx is executed only if the
  987. * preceding call to srcu_readers_active_idx_check() found that
  988. * the ->srcu_unlock_count[] and ->srcu_lock_count[] sums matched
  989. * and because that summing uses atomic_long_read(), there is
  990. * ordering due to a control dependency between that summing and
  991. * the WRITE_ONCE() in this call to srcu_flip(). This ordering
  992. * ensures that if this updater saw a given reader's increment from
  993. * __srcu_read_lock(), that reader was using a value of ->srcu_idx
  994. * from before the previous call to srcu_flip(), which should be
  995. * quite rare. This ordering thus helps forward progress because
  996. * the grace period could otherwise be delayed by additional
  997. * calls to __srcu_read_lock() using that old (soon to be new)
  998. * value of ->srcu_idx.
  999. *
  1000. * This sum-equality check and ordering also ensures that if
  1001. * a given call to __srcu_read_lock() uses the new value of
  1002. * ->srcu_idx, this updater's earlier scans cannot have seen
  1003. * that reader's increments, which is all to the good, because
  1004. * this grace period need not wait on that reader. After all,
  1005. * if those earlier scans had seen that reader, there would have
  1006. * been a sum mismatch and this code would not be reached.
  1007. *
  1008. * This means that the following smp_mb() is redundant, but
  1009. * it stays until either (1) Compilers learn about this sort of
  1010. * control dependency or (2) Some production workload running on
  1011. * a production system is unduly delayed by this slowpath smp_mb().
  1012. */
  1013. smp_mb(); /* E */ /* Pairs with B and C. */
  1014. WRITE_ONCE(ssp->srcu_idx, ssp->srcu_idx + 1); // Flip the counter.
  1015. /*
  1016. * Ensure that if the updater misses an __srcu_read_unlock()
  1017. * increment, that task's __srcu_read_lock() following its next
  1018. * __srcu_read_lock() or __srcu_read_unlock() will see the above
  1019. * counter update. Note that both this memory barrier and the
  1020. * one in srcu_readers_active_idx_check() provide the guarantee
  1021. * for __srcu_read_lock().
  1022. */
  1023. smp_mb(); /* D */ /* Pairs with C. */
  1024. }
  1025. /*
  1026. * If SRCU is likely idle, return true, otherwise return false.
  1027. *
  1028. * Note that it is OK for several current from-idle requests for a new
  1029. * grace period from idle to specify expediting because they will all end
  1030. * up requesting the same grace period anyhow. So no loss.
  1031. *
  1032. * Note also that if any CPU (including the current one) is still invoking
  1033. * callbacks, this function will nevertheless say "idle". This is not
  1034. * ideal, but the overhead of checking all CPUs' callback lists is even
  1035. * less ideal, especially on large systems. Furthermore, the wakeup
  1036. * can happen before the callback is fully removed, so we have no choice
  1037. * but to accept this type of error.
  1038. *
  1039. * This function is also subject to counter-wrap errors, but let's face
  1040. * it, if this function was preempted for enough time for the counters
  1041. * to wrap, it really doesn't matter whether or not we expedite the grace
  1042. * period. The extra overhead of a needlessly expedited grace period is
  1043. * negligible when amortized over that time period, and the extra latency
  1044. * of a needlessly non-expedited grace period is similarly negligible.
  1045. */
  1046. static bool srcu_might_be_idle(struct srcu_struct *ssp)
  1047. {
  1048. unsigned long curseq;
  1049. unsigned long flags;
  1050. struct srcu_data *sdp;
  1051. unsigned long t;
  1052. unsigned long tlast;
  1053. check_init_srcu_struct(ssp);
  1054. /* If the local srcu_data structure has callbacks, not idle. */
  1055. sdp = raw_cpu_ptr(ssp->sda);
  1056. spin_lock_irqsave_rcu_node(sdp, flags);
  1057. if (rcu_segcblist_pend_cbs(&sdp->srcu_cblist)) {
  1058. spin_unlock_irqrestore_rcu_node(sdp, flags);
  1059. return false; /* Callbacks already present, so not idle. */
  1060. }
  1061. spin_unlock_irqrestore_rcu_node(sdp, flags);
  1062. /*
  1063. * No local callbacks, so probabilistically probe global state.
  1064. * Exact information would require acquiring locks, which would
  1065. * kill scalability, hence the probabilistic nature of the probe.
  1066. */
  1067. /* First, see if enough time has passed since the last GP. */
  1068. t = ktime_get_mono_fast_ns();
  1069. tlast = READ_ONCE(ssp->srcu_sup->srcu_last_gp_end);
  1070. if (exp_holdoff == 0 ||
  1071. time_in_range_open(t, tlast, tlast + exp_holdoff))
  1072. return false; /* Too soon after last GP. */
  1073. /* Next, check for probable idleness. */
  1074. curseq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq);
  1075. smp_mb(); /* Order ->srcu_gp_seq with ->srcu_gp_seq_needed. */
  1076. if (ULONG_CMP_LT(curseq, READ_ONCE(ssp->srcu_sup->srcu_gp_seq_needed)))
  1077. return false; /* Grace period in progress, so not idle. */
  1078. smp_mb(); /* Order ->srcu_gp_seq with prior access. */
  1079. if (curseq != rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq))
  1080. return false; /* GP # changed, so not idle. */
  1081. return true; /* With reasonable probability, idle! */
  1082. }
  1083. /*
  1084. * SRCU callback function to leak a callback.
  1085. */
  1086. static void srcu_leak_callback(struct rcu_head *rhp)
  1087. {
  1088. }
  1089. /*
  1090. * Start an SRCU grace period, and also queue the callback if non-NULL.
  1091. */
  1092. static unsigned long srcu_gp_start_if_needed(struct srcu_struct *ssp,
  1093. struct rcu_head *rhp, bool do_norm)
  1094. {
  1095. unsigned long flags;
  1096. int idx;
  1097. bool needexp = false;
  1098. bool needgp = false;
  1099. unsigned long s;
  1100. struct srcu_data *sdp;
  1101. struct srcu_node *sdp_mynode;
  1102. int ss_state;
  1103. check_init_srcu_struct(ssp);
  1104. /*
  1105. * While starting a new grace period, make sure we are in an
  1106. * SRCU read-side critical section so that the grace-period
  1107. * sequence number cannot wrap around in the meantime.
  1108. */
  1109. idx = __srcu_read_lock_nmisafe(ssp);
  1110. ss_state = smp_load_acquire(&ssp->srcu_sup->srcu_size_state);
  1111. if (ss_state < SRCU_SIZE_WAIT_CALL)
  1112. sdp = per_cpu_ptr(ssp->sda, get_boot_cpu_id());
  1113. else
  1114. sdp = raw_cpu_ptr(ssp->sda);
  1115. spin_lock_irqsave_sdp_contention(sdp, &flags);
  1116. if (rhp)
  1117. rcu_segcblist_enqueue(&sdp->srcu_cblist, rhp);
  1118. /*
  1119. * It's crucial to capture the snapshot 's' for acceleration before
  1120. * reading the current gp_seq that is used for advancing. This is
  1121. * essential because if the acceleration snapshot is taken after a
  1122. * failed advancement attempt, there's a risk that a grace period may
  1123. * conclude and a new one may start in the interim. If the snapshot is
  1124. * captured after this sequence of events, the acceleration snapshot 's'
  1125. * could be excessively advanced, leading to acceleration failure.
  1126. * In such a scenario, an 'acceleration leak' can occur, where new
  1127. * callbacks become indefinitely stuck in the RCU_NEXT_TAIL segment.
  1128. * Also note that encountering advancing failures is a normal
  1129. * occurrence when the grace period for RCU_WAIT_TAIL is in progress.
  1130. *
  1131. * To see this, consider the following events which occur if
  1132. * rcu_seq_snap() were to be called after advance:
  1133. *
  1134. * 1) The RCU_WAIT_TAIL segment has callbacks (gp_num = X + 4) and the
  1135. * RCU_NEXT_READY_TAIL also has callbacks (gp_num = X + 8).
  1136. *
  1137. * 2) The grace period for RCU_WAIT_TAIL is seen as started but not
  1138. * completed so rcu_seq_current() returns X + SRCU_STATE_SCAN1.
  1139. *
  1140. * 3) This value is passed to rcu_segcblist_advance() which can't move
  1141. * any segment forward and fails.
  1142. *
  1143. * 4) srcu_gp_start_if_needed() still proceeds with callback acceleration.
  1144. * But then the call to rcu_seq_snap() observes the grace period for the
  1145. * RCU_WAIT_TAIL segment as completed and the subsequent one for the
  1146. * RCU_NEXT_READY_TAIL segment as started (ie: X + 4 + SRCU_STATE_SCAN1)
  1147. * so it returns a snapshot of the next grace period, which is X + 12.
  1148. *
  1149. * 5) The value of X + 12 is passed to rcu_segcblist_accelerate() but the
  1150. * freshly enqueued callback in RCU_NEXT_TAIL can't move to
  1151. * RCU_NEXT_READY_TAIL which already has callbacks for a previous grace
  1152. * period (gp_num = X + 8). So acceleration fails.
  1153. */
  1154. s = rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq);
  1155. if (rhp) {
  1156. rcu_segcblist_advance(&sdp->srcu_cblist,
  1157. rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
  1158. /*
  1159. * Acceleration can never fail because the base current gp_seq
  1160. * used for acceleration is <= the value of gp_seq used for
  1161. * advancing. This means that RCU_NEXT_TAIL segment will
  1162. * always be able to be emptied by the acceleration into the
  1163. * RCU_NEXT_READY_TAIL or RCU_WAIT_TAIL segments.
  1164. */
  1165. WARN_ON_ONCE(!rcu_segcblist_accelerate(&sdp->srcu_cblist, s));
  1166. }
  1167. if (ULONG_CMP_LT(sdp->srcu_gp_seq_needed, s)) {
  1168. sdp->srcu_gp_seq_needed = s;
  1169. needgp = true;
  1170. }
  1171. if (!do_norm && ULONG_CMP_LT(sdp->srcu_gp_seq_needed_exp, s)) {
  1172. sdp->srcu_gp_seq_needed_exp = s;
  1173. needexp = true;
  1174. }
  1175. spin_unlock_irqrestore_rcu_node(sdp, flags);
  1176. /* Ensure that snp node tree is fully initialized before traversing it */
  1177. if (ss_state < SRCU_SIZE_WAIT_BARRIER)
  1178. sdp_mynode = NULL;
  1179. else
  1180. sdp_mynode = sdp->mynode;
  1181. if (needgp)
  1182. srcu_funnel_gp_start(ssp, sdp, s, do_norm);
  1183. else if (needexp)
  1184. srcu_funnel_exp_start(ssp, sdp_mynode, s);
  1185. __srcu_read_unlock_nmisafe(ssp, idx);
  1186. return s;
  1187. }
  1188. /*
  1189. * Enqueue an SRCU callback on the srcu_data structure associated with
  1190. * the current CPU and the specified srcu_struct structure, initiating
  1191. * grace-period processing if it is not already running.
  1192. *
  1193. * Note that all CPUs must agree that the grace period extended beyond
  1194. * all pre-existing SRCU read-side critical section. On systems with
  1195. * more than one CPU, this means that when "func()" is invoked, each CPU
  1196. * is guaranteed to have executed a full memory barrier since the end of
  1197. * its last corresponding SRCU read-side critical section whose beginning
  1198. * preceded the call to call_srcu(). It also means that each CPU executing
  1199. * an SRCU read-side critical section that continues beyond the start of
  1200. * "func()" must have executed a memory barrier after the call_srcu()
  1201. * but before the beginning of that SRCU read-side critical section.
  1202. * Note that these guarantees include CPUs that are offline, idle, or
  1203. * executing in user mode, as well as CPUs that are executing in the kernel.
  1204. *
  1205. * Furthermore, if CPU A invoked call_srcu() and CPU B invoked the
  1206. * resulting SRCU callback function "func()", then both CPU A and CPU
  1207. * B are guaranteed to execute a full memory barrier during the time
  1208. * interval between the call to call_srcu() and the invocation of "func()".
  1209. * This guarantee applies even if CPU A and CPU B are the same CPU (but
  1210. * again only if the system has more than one CPU).
  1211. *
  1212. * Of course, these guarantees apply only for invocations of call_srcu(),
  1213. * srcu_read_lock(), and srcu_read_unlock() that are all passed the same
  1214. * srcu_struct structure.
  1215. */
  1216. static void __call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
  1217. rcu_callback_t func, bool do_norm)
  1218. {
  1219. if (debug_rcu_head_queue(rhp)) {
  1220. /* Probable double call_srcu(), so leak the callback. */
  1221. WRITE_ONCE(rhp->func, srcu_leak_callback);
  1222. WARN_ONCE(1, "call_srcu(): Leaked duplicate callback\n");
  1223. return;
  1224. }
  1225. rhp->func = func;
  1226. (void)srcu_gp_start_if_needed(ssp, rhp, do_norm);
  1227. }
  1228. /**
  1229. * call_srcu() - Queue a callback for invocation after an SRCU grace period
  1230. * @ssp: srcu_struct in queue the callback
  1231. * @rhp: structure to be used for queueing the SRCU callback.
  1232. * @func: function to be invoked after the SRCU grace period
  1233. *
  1234. * The callback function will be invoked some time after a full SRCU
  1235. * grace period elapses, in other words after all pre-existing SRCU
  1236. * read-side critical sections have completed. However, the callback
  1237. * function might well execute concurrently with other SRCU read-side
  1238. * critical sections that started after call_srcu() was invoked. SRCU
  1239. * read-side critical sections are delimited by srcu_read_lock() and
  1240. * srcu_read_unlock(), and may be nested.
  1241. *
  1242. * The callback will be invoked from process context, but must nevertheless
  1243. * be fast and must not block.
  1244. */
  1245. void call_srcu(struct srcu_struct *ssp, struct rcu_head *rhp,
  1246. rcu_callback_t func)
  1247. {
  1248. __call_srcu(ssp, rhp, func, true);
  1249. }
  1250. EXPORT_SYMBOL_GPL(call_srcu);
  1251. /*
  1252. * Helper function for synchronize_srcu() and synchronize_srcu_expedited().
  1253. */
  1254. static void __synchronize_srcu(struct srcu_struct *ssp, bool do_norm)
  1255. {
  1256. struct rcu_synchronize rcu;
  1257. srcu_lock_sync(&ssp->dep_map);
  1258. RCU_LOCKDEP_WARN(lockdep_is_held(ssp) ||
  1259. lock_is_held(&rcu_bh_lock_map) ||
  1260. lock_is_held(&rcu_lock_map) ||
  1261. lock_is_held(&rcu_sched_lock_map),
  1262. "Illegal synchronize_srcu() in same-type SRCU (or in RCU) read-side critical section");
  1263. if (rcu_scheduler_active == RCU_SCHEDULER_INACTIVE)
  1264. return;
  1265. might_sleep();
  1266. check_init_srcu_struct(ssp);
  1267. init_completion(&rcu.completion);
  1268. init_rcu_head_on_stack(&rcu.head);
  1269. __call_srcu(ssp, &rcu.head, wakeme_after_rcu, do_norm);
  1270. wait_for_completion(&rcu.completion);
  1271. destroy_rcu_head_on_stack(&rcu.head);
  1272. /*
  1273. * Make sure that later code is ordered after the SRCU grace
  1274. * period. This pairs with the spin_lock_irq_rcu_node()
  1275. * in srcu_invoke_callbacks(). Unlike Tree RCU, this is needed
  1276. * because the current CPU might have been totally uninvolved with
  1277. * (and thus unordered against) that grace period.
  1278. */
  1279. smp_mb();
  1280. }
  1281. /**
  1282. * synchronize_srcu_expedited - Brute-force SRCU grace period
  1283. * @ssp: srcu_struct with which to synchronize.
  1284. *
  1285. * Wait for an SRCU grace period to elapse, but be more aggressive about
  1286. * spinning rather than blocking when waiting.
  1287. *
  1288. * Note that synchronize_srcu_expedited() has the same deadlock and
  1289. * memory-ordering properties as does synchronize_srcu().
  1290. */
  1291. void synchronize_srcu_expedited(struct srcu_struct *ssp)
  1292. {
  1293. __synchronize_srcu(ssp, rcu_gp_is_normal());
  1294. }
  1295. EXPORT_SYMBOL_GPL(synchronize_srcu_expedited);
  1296. /**
  1297. * synchronize_srcu - wait for prior SRCU read-side critical-section completion
  1298. * @ssp: srcu_struct with which to synchronize.
  1299. *
  1300. * Wait for the count to drain to zero of both indexes. To avoid the
  1301. * possible starvation of synchronize_srcu(), it waits for the count of
  1302. * the index=((->srcu_idx & 1) ^ 1) to drain to zero at first,
  1303. * and then flip the srcu_idx and wait for the count of the other index.
  1304. *
  1305. * Can block; must be called from process context.
  1306. *
  1307. * Note that it is illegal to call synchronize_srcu() from the corresponding
  1308. * SRCU read-side critical section; doing so will result in deadlock.
  1309. * However, it is perfectly legal to call synchronize_srcu() on one
  1310. * srcu_struct from some other srcu_struct's read-side critical section,
  1311. * as long as the resulting graph of srcu_structs is acyclic.
  1312. *
  1313. * There are memory-ordering constraints implied by synchronize_srcu().
  1314. * On systems with more than one CPU, when synchronize_srcu() returns,
  1315. * each CPU is guaranteed to have executed a full memory barrier since
  1316. * the end of its last corresponding SRCU read-side critical section
  1317. * whose beginning preceded the call to synchronize_srcu(). In addition,
  1318. * each CPU having an SRCU read-side critical section that extends beyond
  1319. * the return from synchronize_srcu() is guaranteed to have executed a
  1320. * full memory barrier after the beginning of synchronize_srcu() and before
  1321. * the beginning of that SRCU read-side critical section. Note that these
  1322. * guarantees include CPUs that are offline, idle, or executing in user mode,
  1323. * as well as CPUs that are executing in the kernel.
  1324. *
  1325. * Furthermore, if CPU A invoked synchronize_srcu(), which returned
  1326. * to its caller on CPU B, then both CPU A and CPU B are guaranteed
  1327. * to have executed a full memory barrier during the execution of
  1328. * synchronize_srcu(). This guarantee applies even if CPU A and CPU B
  1329. * are the same CPU, but again only if the system has more than one CPU.
  1330. *
  1331. * Of course, these memory-ordering guarantees apply only when
  1332. * synchronize_srcu(), srcu_read_lock(), and srcu_read_unlock() are
  1333. * passed the same srcu_struct structure.
  1334. *
  1335. * Implementation of these memory-ordering guarantees is similar to
  1336. * that of synchronize_rcu().
  1337. *
  1338. * If SRCU is likely idle, expedite the first request. This semantic
  1339. * was provided by Classic SRCU, and is relied upon by its users, so TREE
  1340. * SRCU must also provide it. Note that detecting idleness is heuristic
  1341. * and subject to both false positives and negatives.
  1342. */
  1343. void synchronize_srcu(struct srcu_struct *ssp)
  1344. {
  1345. if (srcu_might_be_idle(ssp) || rcu_gp_is_expedited())
  1346. synchronize_srcu_expedited(ssp);
  1347. else
  1348. __synchronize_srcu(ssp, true);
  1349. }
  1350. EXPORT_SYMBOL_GPL(synchronize_srcu);
  1351. /**
  1352. * get_state_synchronize_srcu - Provide an end-of-grace-period cookie
  1353. * @ssp: srcu_struct to provide cookie for.
  1354. *
  1355. * This function returns a cookie that can be passed to
  1356. * poll_state_synchronize_srcu(), which will return true if a full grace
  1357. * period has elapsed in the meantime. It is the caller's responsibility
  1358. * to make sure that grace period happens, for example, by invoking
  1359. * call_srcu() after return from get_state_synchronize_srcu().
  1360. */
  1361. unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp)
  1362. {
  1363. // Any prior manipulation of SRCU-protected data must happen
  1364. // before the load from ->srcu_gp_seq.
  1365. smp_mb();
  1366. return rcu_seq_snap(&ssp->srcu_sup->srcu_gp_seq);
  1367. }
  1368. EXPORT_SYMBOL_GPL(get_state_synchronize_srcu);
  1369. /**
  1370. * start_poll_synchronize_srcu - Provide cookie and start grace period
  1371. * @ssp: srcu_struct to provide cookie for.
  1372. *
  1373. * This function returns a cookie that can be passed to
  1374. * poll_state_synchronize_srcu(), which will return true if a full grace
  1375. * period has elapsed in the meantime. Unlike get_state_synchronize_srcu(),
  1376. * this function also ensures that any needed SRCU grace period will be
  1377. * started. This convenience does come at a cost in terms of CPU overhead.
  1378. */
  1379. unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp)
  1380. {
  1381. return srcu_gp_start_if_needed(ssp, NULL, true);
  1382. }
  1383. EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu);
  1384. /**
  1385. * poll_state_synchronize_srcu - Has cookie's grace period ended?
  1386. * @ssp: srcu_struct to provide cookie for.
  1387. * @cookie: Return value from get_state_synchronize_srcu() or start_poll_synchronize_srcu().
  1388. *
  1389. * This function takes the cookie that was returned from either
  1390. * get_state_synchronize_srcu() or start_poll_synchronize_srcu(), and
  1391. * returns @true if an SRCU grace period elapsed since the time that the
  1392. * cookie was created.
  1393. *
  1394. * Because cookies are finite in size, wrapping/overflow is possible.
  1395. * This is more pronounced on 32-bit systems where cookies are 32 bits,
  1396. * where in theory wrapping could happen in about 14 hours assuming
  1397. * 25-microsecond expedited SRCU grace periods. However, a more likely
  1398. * overflow lower bound is on the order of 24 days in the case of
  1399. * one-millisecond SRCU grace periods. Of course, wrapping in a 64-bit
  1400. * system requires geologic timespans, as in more than seven million years
  1401. * even for expedited SRCU grace periods.
  1402. *
  1403. * Wrapping/overflow is much more of an issue for CONFIG_SMP=n systems
  1404. * that also have CONFIG_PREEMPTION=n, which selects Tiny SRCU. This uses
  1405. * a 16-bit cookie, which rcutorture routinely wraps in a matter of a
  1406. * few minutes. If this proves to be a problem, this counter will be
  1407. * expanded to the same size as for Tree SRCU.
  1408. */
  1409. bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie)
  1410. {
  1411. if (cookie != SRCU_GET_STATE_COMPLETED &&
  1412. !rcu_seq_done(&ssp->srcu_sup->srcu_gp_seq, cookie))
  1413. return false;
  1414. // Ensure that the end of the SRCU grace period happens before
  1415. // any subsequent code that the caller might execute.
  1416. smp_mb(); // ^^^
  1417. return true;
  1418. }
  1419. EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu);
  1420. /*
  1421. * Callback function for srcu_barrier() use.
  1422. */
  1423. static void srcu_barrier_cb(struct rcu_head *rhp)
  1424. {
  1425. struct srcu_data *sdp;
  1426. struct srcu_struct *ssp;
  1427. rhp->next = rhp; // Mark the callback as having been invoked.
  1428. sdp = container_of(rhp, struct srcu_data, srcu_barrier_head);
  1429. ssp = sdp->ssp;
  1430. if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt))
  1431. complete(&ssp->srcu_sup->srcu_barrier_completion);
  1432. }
  1433. /*
  1434. * Enqueue an srcu_barrier() callback on the specified srcu_data
  1435. * structure's ->cblist. but only if that ->cblist already has at least one
  1436. * callback enqueued. Note that if a CPU already has callbacks enqueue,
  1437. * it must have already registered the need for a future grace period,
  1438. * so all we need do is enqueue a callback that will use the same grace
  1439. * period as the last callback already in the queue.
  1440. */
  1441. static void srcu_barrier_one_cpu(struct srcu_struct *ssp, struct srcu_data *sdp)
  1442. {
  1443. spin_lock_irq_rcu_node(sdp);
  1444. atomic_inc(&ssp->srcu_sup->srcu_barrier_cpu_cnt);
  1445. sdp->srcu_barrier_head.func = srcu_barrier_cb;
  1446. debug_rcu_head_queue(&sdp->srcu_barrier_head);
  1447. if (!rcu_segcblist_entrain(&sdp->srcu_cblist,
  1448. &sdp->srcu_barrier_head)) {
  1449. debug_rcu_head_unqueue(&sdp->srcu_barrier_head);
  1450. atomic_dec(&ssp->srcu_sup->srcu_barrier_cpu_cnt);
  1451. }
  1452. spin_unlock_irq_rcu_node(sdp);
  1453. }
  1454. /**
  1455. * srcu_barrier - Wait until all in-flight call_srcu() callbacks complete.
  1456. * @ssp: srcu_struct on which to wait for in-flight callbacks.
  1457. */
  1458. void srcu_barrier(struct srcu_struct *ssp)
  1459. {
  1460. int cpu;
  1461. int idx;
  1462. unsigned long s = rcu_seq_snap(&ssp->srcu_sup->srcu_barrier_seq);
  1463. check_init_srcu_struct(ssp);
  1464. mutex_lock(&ssp->srcu_sup->srcu_barrier_mutex);
  1465. if (rcu_seq_done(&ssp->srcu_sup->srcu_barrier_seq, s)) {
  1466. smp_mb(); /* Force ordering following return. */
  1467. mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex);
  1468. return; /* Someone else did our work for us. */
  1469. }
  1470. rcu_seq_start(&ssp->srcu_sup->srcu_barrier_seq);
  1471. init_completion(&ssp->srcu_sup->srcu_barrier_completion);
  1472. /* Initial count prevents reaching zero until all CBs are posted. */
  1473. atomic_set(&ssp->srcu_sup->srcu_barrier_cpu_cnt, 1);
  1474. idx = __srcu_read_lock_nmisafe(ssp);
  1475. if (smp_load_acquire(&ssp->srcu_sup->srcu_size_state) < SRCU_SIZE_WAIT_BARRIER)
  1476. srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, get_boot_cpu_id()));
  1477. else
  1478. for_each_possible_cpu(cpu)
  1479. srcu_barrier_one_cpu(ssp, per_cpu_ptr(ssp->sda, cpu));
  1480. __srcu_read_unlock_nmisafe(ssp, idx);
  1481. /* Remove the initial count, at which point reaching zero can happen. */
  1482. if (atomic_dec_and_test(&ssp->srcu_sup->srcu_barrier_cpu_cnt))
  1483. complete(&ssp->srcu_sup->srcu_barrier_completion);
  1484. wait_for_completion(&ssp->srcu_sup->srcu_barrier_completion);
  1485. rcu_seq_end(&ssp->srcu_sup->srcu_barrier_seq);
  1486. mutex_unlock(&ssp->srcu_sup->srcu_barrier_mutex);
  1487. }
  1488. EXPORT_SYMBOL_GPL(srcu_barrier);
  1489. /**
  1490. * srcu_batches_completed - return batches completed.
  1491. * @ssp: srcu_struct on which to report batch completion.
  1492. *
  1493. * Report the number of batches, correlated with, but not necessarily
  1494. * precisely the same as, the number of grace periods that have elapsed.
  1495. */
  1496. unsigned long srcu_batches_completed(struct srcu_struct *ssp)
  1497. {
  1498. return READ_ONCE(ssp->srcu_idx);
  1499. }
  1500. EXPORT_SYMBOL_GPL(srcu_batches_completed);
  1501. /*
  1502. * Core SRCU state machine. Push state bits of ->srcu_gp_seq
  1503. * to SRCU_STATE_SCAN2, and invoke srcu_gp_end() when scan has
  1504. * completed in that state.
  1505. */
  1506. static void srcu_advance_state(struct srcu_struct *ssp)
  1507. {
  1508. int idx;
  1509. mutex_lock(&ssp->srcu_sup->srcu_gp_mutex);
  1510. /*
  1511. * Because readers might be delayed for an extended period after
  1512. * fetching ->srcu_idx for their index, at any point in time there
  1513. * might well be readers using both idx=0 and idx=1. We therefore
  1514. * need to wait for readers to clear from both index values before
  1515. * invoking a callback.
  1516. *
  1517. * The load-acquire ensures that we see the accesses performed
  1518. * by the prior grace period.
  1519. */
  1520. idx = rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq)); /* ^^^ */
  1521. if (idx == SRCU_STATE_IDLE) {
  1522. spin_lock_irq_rcu_node(ssp->srcu_sup);
  1523. if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) {
  1524. WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq));
  1525. spin_unlock_irq_rcu_node(ssp->srcu_sup);
  1526. mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
  1527. return;
  1528. }
  1529. idx = rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq));
  1530. if (idx == SRCU_STATE_IDLE)
  1531. srcu_gp_start(ssp);
  1532. spin_unlock_irq_rcu_node(ssp->srcu_sup);
  1533. if (idx != SRCU_STATE_IDLE) {
  1534. mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
  1535. return; /* Someone else started the grace period. */
  1536. }
  1537. }
  1538. if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN1) {
  1539. idx = 1 ^ (ssp->srcu_idx & 1);
  1540. if (!try_check_zero(ssp, idx, 1)) {
  1541. mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
  1542. return; /* readers present, retry later. */
  1543. }
  1544. srcu_flip(ssp);
  1545. spin_lock_irq_rcu_node(ssp->srcu_sup);
  1546. rcu_seq_set_state(&ssp->srcu_sup->srcu_gp_seq, SRCU_STATE_SCAN2);
  1547. ssp->srcu_sup->srcu_n_exp_nodelay = 0;
  1548. spin_unlock_irq_rcu_node(ssp->srcu_sup);
  1549. }
  1550. if (rcu_seq_state(READ_ONCE(ssp->srcu_sup->srcu_gp_seq)) == SRCU_STATE_SCAN2) {
  1551. /*
  1552. * SRCU read-side critical sections are normally short,
  1553. * so check at least twice in quick succession after a flip.
  1554. */
  1555. idx = 1 ^ (ssp->srcu_idx & 1);
  1556. if (!try_check_zero(ssp, idx, 2)) {
  1557. mutex_unlock(&ssp->srcu_sup->srcu_gp_mutex);
  1558. return; /* readers present, retry later. */
  1559. }
  1560. ssp->srcu_sup->srcu_n_exp_nodelay = 0;
  1561. srcu_gp_end(ssp); /* Releases ->srcu_gp_mutex. */
  1562. }
  1563. }
  1564. /*
  1565. * Invoke a limited number of SRCU callbacks that have passed through
  1566. * their grace period. If there are more to do, SRCU will reschedule
  1567. * the workqueue. Note that needed memory barriers have been executed
  1568. * in this task's context by srcu_readers_active_idx_check().
  1569. */
  1570. static void srcu_invoke_callbacks(struct work_struct *work)
  1571. {
  1572. long len;
  1573. bool more;
  1574. struct rcu_cblist ready_cbs;
  1575. struct rcu_head *rhp;
  1576. struct srcu_data *sdp;
  1577. struct srcu_struct *ssp;
  1578. sdp = container_of(work, struct srcu_data, work);
  1579. ssp = sdp->ssp;
  1580. rcu_cblist_init(&ready_cbs);
  1581. spin_lock_irq_rcu_node(sdp);
  1582. WARN_ON_ONCE(!rcu_segcblist_segempty(&sdp->srcu_cblist, RCU_NEXT_TAIL));
  1583. rcu_segcblist_advance(&sdp->srcu_cblist,
  1584. rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq));
  1585. /*
  1586. * Although this function is theoretically re-entrant, concurrent
  1587. * callbacks invocation is disallowed to avoid executing an SRCU barrier
  1588. * too early.
  1589. */
  1590. if (sdp->srcu_cblist_invoking ||
  1591. !rcu_segcblist_ready_cbs(&sdp->srcu_cblist)) {
  1592. spin_unlock_irq_rcu_node(sdp);
  1593. return; /* Someone else on the job or nothing to do. */
  1594. }
  1595. /* We are on the job! Extract and invoke ready callbacks. */
  1596. sdp->srcu_cblist_invoking = true;
  1597. rcu_segcblist_extract_done_cbs(&sdp->srcu_cblist, &ready_cbs);
  1598. len = ready_cbs.len;
  1599. spin_unlock_irq_rcu_node(sdp);
  1600. rhp = rcu_cblist_dequeue(&ready_cbs);
  1601. for (; rhp != NULL; rhp = rcu_cblist_dequeue(&ready_cbs)) {
  1602. debug_rcu_head_unqueue(rhp);
  1603. debug_rcu_head_callback(rhp);
  1604. local_bh_disable();
  1605. rhp->func(rhp);
  1606. local_bh_enable();
  1607. }
  1608. WARN_ON_ONCE(ready_cbs.len);
  1609. /*
  1610. * Update counts, accelerate new callbacks, and if needed,
  1611. * schedule another round of callback invocation.
  1612. */
  1613. spin_lock_irq_rcu_node(sdp);
  1614. rcu_segcblist_add_len(&sdp->srcu_cblist, -len);
  1615. sdp->srcu_cblist_invoking = false;
  1616. more = rcu_segcblist_ready_cbs(&sdp->srcu_cblist);
  1617. spin_unlock_irq_rcu_node(sdp);
  1618. /* An SRCU barrier or callbacks from previous nesting work pending */
  1619. if (more)
  1620. srcu_schedule_cbs_sdp(sdp, 0);
  1621. }
  1622. /*
  1623. * Finished one round of SRCU grace period. Start another if there are
  1624. * more SRCU callbacks queued, otherwise put SRCU into not-running state.
  1625. */
  1626. static void srcu_reschedule(struct srcu_struct *ssp, unsigned long delay)
  1627. {
  1628. bool pushgp = true;
  1629. spin_lock_irq_rcu_node(ssp->srcu_sup);
  1630. if (ULONG_CMP_GE(ssp->srcu_sup->srcu_gp_seq, ssp->srcu_sup->srcu_gp_seq_needed)) {
  1631. if (!WARN_ON_ONCE(rcu_seq_state(ssp->srcu_sup->srcu_gp_seq))) {
  1632. /* All requests fulfilled, time to go idle. */
  1633. pushgp = false;
  1634. }
  1635. } else if (!rcu_seq_state(ssp->srcu_sup->srcu_gp_seq)) {
  1636. /* Outstanding request and no GP. Start one. */
  1637. srcu_gp_start(ssp);
  1638. }
  1639. spin_unlock_irq_rcu_node(ssp->srcu_sup);
  1640. if (pushgp)
  1641. queue_delayed_work(rcu_gp_wq, &ssp->srcu_sup->work, delay);
  1642. }
  1643. /*
  1644. * This is the work-queue function that handles SRCU grace periods.
  1645. */
  1646. static void process_srcu(struct work_struct *work)
  1647. {
  1648. unsigned long curdelay;
  1649. unsigned long j;
  1650. struct srcu_struct *ssp;
  1651. struct srcu_usage *sup;
  1652. sup = container_of(work, struct srcu_usage, work.work);
  1653. ssp = sup->srcu_ssp;
  1654. srcu_advance_state(ssp);
  1655. curdelay = srcu_get_delay(ssp);
  1656. if (curdelay) {
  1657. WRITE_ONCE(sup->reschedule_count, 0);
  1658. } else {
  1659. j = jiffies;
  1660. if (READ_ONCE(sup->reschedule_jiffies) == j) {
  1661. ASSERT_EXCLUSIVE_WRITER(sup->reschedule_count);
  1662. WRITE_ONCE(sup->reschedule_count, READ_ONCE(sup->reschedule_count) + 1);
  1663. if (READ_ONCE(sup->reschedule_count) > srcu_max_nodelay)
  1664. curdelay = 1;
  1665. } else {
  1666. WRITE_ONCE(sup->reschedule_count, 1);
  1667. WRITE_ONCE(sup->reschedule_jiffies, j);
  1668. }
  1669. }
  1670. srcu_reschedule(ssp, curdelay);
  1671. }
  1672. void srcutorture_get_gp_data(struct srcu_struct *ssp, int *flags,
  1673. unsigned long *gp_seq)
  1674. {
  1675. *flags = 0;
  1676. *gp_seq = rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq);
  1677. }
  1678. EXPORT_SYMBOL_GPL(srcutorture_get_gp_data);
  1679. static const char * const srcu_size_state_name[] = {
  1680. "SRCU_SIZE_SMALL",
  1681. "SRCU_SIZE_ALLOC",
  1682. "SRCU_SIZE_WAIT_BARRIER",
  1683. "SRCU_SIZE_WAIT_CALL",
  1684. "SRCU_SIZE_WAIT_CBS1",
  1685. "SRCU_SIZE_WAIT_CBS2",
  1686. "SRCU_SIZE_WAIT_CBS3",
  1687. "SRCU_SIZE_WAIT_CBS4",
  1688. "SRCU_SIZE_BIG",
  1689. "SRCU_SIZE_???",
  1690. };
  1691. void srcu_torture_stats_print(struct srcu_struct *ssp, char *tt, char *tf)
  1692. {
  1693. int cpu;
  1694. int idx;
  1695. unsigned long s0 = 0, s1 = 0;
  1696. int ss_state = READ_ONCE(ssp->srcu_sup->srcu_size_state);
  1697. int ss_state_idx = ss_state;
  1698. idx = ssp->srcu_idx & 0x1;
  1699. if (ss_state < 0 || ss_state >= ARRAY_SIZE(srcu_size_state_name))
  1700. ss_state_idx = ARRAY_SIZE(srcu_size_state_name) - 1;
  1701. pr_alert("%s%s Tree SRCU g%ld state %d (%s)",
  1702. tt, tf, rcu_seq_current(&ssp->srcu_sup->srcu_gp_seq), ss_state,
  1703. srcu_size_state_name[ss_state_idx]);
  1704. if (!ssp->sda) {
  1705. // Called after cleanup_srcu_struct(), perhaps.
  1706. pr_cont(" No per-CPU srcu_data structures (->sda == NULL).\n");
  1707. } else {
  1708. pr_cont(" per-CPU(idx=%d):", idx);
  1709. for_each_possible_cpu(cpu) {
  1710. unsigned long l0, l1;
  1711. unsigned long u0, u1;
  1712. long c0, c1;
  1713. struct srcu_data *sdp;
  1714. sdp = per_cpu_ptr(ssp->sda, cpu);
  1715. u0 = data_race(atomic_long_read(&sdp->srcu_unlock_count[!idx]));
  1716. u1 = data_race(atomic_long_read(&sdp->srcu_unlock_count[idx]));
  1717. /*
  1718. * Make sure that a lock is always counted if the corresponding
  1719. * unlock is counted.
  1720. */
  1721. smp_rmb();
  1722. l0 = data_race(atomic_long_read(&sdp->srcu_lock_count[!idx]));
  1723. l1 = data_race(atomic_long_read(&sdp->srcu_lock_count[idx]));
  1724. c0 = l0 - u0;
  1725. c1 = l1 - u1;
  1726. pr_cont(" %d(%ld,%ld %c)",
  1727. cpu, c0, c1,
  1728. "C."[rcu_segcblist_empty(&sdp->srcu_cblist)]);
  1729. s0 += c0;
  1730. s1 += c1;
  1731. }
  1732. pr_cont(" T(%ld,%ld)\n", s0, s1);
  1733. }
  1734. if (SRCU_SIZING_IS_TORTURE())
  1735. srcu_transition_to_big(ssp);
  1736. }
  1737. EXPORT_SYMBOL_GPL(srcu_torture_stats_print);
  1738. static int __init srcu_bootup_announce(void)
  1739. {
  1740. pr_info("Hierarchical SRCU implementation.\n");
  1741. if (exp_holdoff != DEFAULT_SRCU_EXP_HOLDOFF)
  1742. pr_info("\tNon-default auto-expedite holdoff of %lu ns.\n", exp_holdoff);
  1743. if (srcu_retry_check_delay != SRCU_DEFAULT_RETRY_CHECK_DELAY)
  1744. pr_info("\tNon-default retry check delay of %lu us.\n", srcu_retry_check_delay);
  1745. if (srcu_max_nodelay != SRCU_DEFAULT_MAX_NODELAY)
  1746. pr_info("\tNon-default max no-delay of %lu.\n", srcu_max_nodelay);
  1747. pr_info("\tMax phase no-delay instances is %lu.\n", srcu_max_nodelay_phase);
  1748. return 0;
  1749. }
  1750. early_initcall(srcu_bootup_announce);
  1751. void __init srcu_init(void)
  1752. {
  1753. struct srcu_usage *sup;
  1754. /* Decide on srcu_struct-size strategy. */
  1755. if (SRCU_SIZING_IS(SRCU_SIZING_AUTO)) {
  1756. if (nr_cpu_ids >= big_cpu_lim) {
  1757. convert_to_big = SRCU_SIZING_INIT; // Don't bother waiting for contention.
  1758. pr_info("%s: Setting srcu_struct sizes to big.\n", __func__);
  1759. } else {
  1760. convert_to_big = SRCU_SIZING_NONE | SRCU_SIZING_CONTEND;
  1761. pr_info("%s: Setting srcu_struct sizes based on contention.\n", __func__);
  1762. }
  1763. }
  1764. /*
  1765. * Once that is set, call_srcu() can follow the normal path and
  1766. * queue delayed work. This must follow RCU workqueues creation
  1767. * and timers initialization.
  1768. */
  1769. srcu_init_done = true;
  1770. while (!list_empty(&srcu_boot_list)) {
  1771. sup = list_first_entry(&srcu_boot_list, struct srcu_usage,
  1772. work.work.entry);
  1773. list_del_init(&sup->work.work.entry);
  1774. if (SRCU_SIZING_IS(SRCU_SIZING_INIT) &&
  1775. sup->srcu_size_state == SRCU_SIZE_SMALL)
  1776. sup->srcu_size_state = SRCU_SIZE_ALLOC;
  1777. queue_work(rcu_gp_wq, &sup->work.work);
  1778. }
  1779. }
  1780. #ifdef CONFIG_MODULES
  1781. /* Initialize any global-scope srcu_struct structures used by this module. */
  1782. static int srcu_module_coming(struct module *mod)
  1783. {
  1784. int i;
  1785. struct srcu_struct *ssp;
  1786. struct srcu_struct **sspp = mod->srcu_struct_ptrs;
  1787. for (i = 0; i < mod->num_srcu_structs; i++) {
  1788. ssp = *(sspp++);
  1789. ssp->sda = alloc_percpu(struct srcu_data);
  1790. if (WARN_ON_ONCE(!ssp->sda))
  1791. return -ENOMEM;
  1792. }
  1793. return 0;
  1794. }
  1795. /* Clean up any global-scope srcu_struct structures used by this module. */
  1796. static void srcu_module_going(struct module *mod)
  1797. {
  1798. int i;
  1799. struct srcu_struct *ssp;
  1800. struct srcu_struct **sspp = mod->srcu_struct_ptrs;
  1801. for (i = 0; i < mod->num_srcu_structs; i++) {
  1802. ssp = *(sspp++);
  1803. if (!rcu_seq_state(smp_load_acquire(&ssp->srcu_sup->srcu_gp_seq_needed)) &&
  1804. !WARN_ON_ONCE(!ssp->srcu_sup->sda_is_static))
  1805. cleanup_srcu_struct(ssp);
  1806. if (!WARN_ON(srcu_readers_active(ssp)))
  1807. free_percpu(ssp->sda);
  1808. }
  1809. }
  1810. /* Handle one module, either coming or going. */
  1811. static int srcu_module_notify(struct notifier_block *self,
  1812. unsigned long val, void *data)
  1813. {
  1814. struct module *mod = data;
  1815. int ret = 0;
  1816. switch (val) {
  1817. case MODULE_STATE_COMING:
  1818. ret = srcu_module_coming(mod);
  1819. break;
  1820. case MODULE_STATE_GOING:
  1821. srcu_module_going(mod);
  1822. break;
  1823. default:
  1824. break;
  1825. }
  1826. return ret;
  1827. }
  1828. static struct notifier_block srcu_module_nb = {
  1829. .notifier_call = srcu_module_notify,
  1830. .priority = 0,
  1831. };
  1832. static __init int init_srcu_module_notifier(void)
  1833. {
  1834. int ret;
  1835. ret = register_module_notifier(&srcu_module_nb);
  1836. if (ret)
  1837. pr_warn("Failed to register srcu module notifier\n");
  1838. return ret;
  1839. }
  1840. late_initcall(init_srcu_module_notifier);
  1841. #endif /* #ifdef CONFIG_MODULES */