xarray.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * XArray implementation
  4. * Copyright (c) 2017-2018 Microsoft Corporation
  5. * Copyright (c) 2018-2020 Oracle
  6. * Author: Matthew Wilcox <willy@infradead.org>
  7. */
  8. #include <linux/bitmap.h>
  9. #include <linux/export.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/xarray.h>
  13. /*
  14. * Coding conventions in this file:
  15. *
  16. * @xa is used to refer to the entire xarray.
  17. * @xas is the 'xarray operation state'. It may be either a pointer to
  18. * an xa_state, or an xa_state stored on the stack. This is an unfortunate
  19. * ambiguity.
  20. * @index is the index of the entry being operated on
  21. * @mark is an xa_mark_t; a small number indicating one of the mark bits.
  22. * @node refers to an xa_node; usually the primary one being operated on by
  23. * this function.
  24. * @offset is the index into the slots array inside an xa_node.
  25. * @parent refers to the @xa_node closer to the head than @node.
  26. * @entry refers to something stored in a slot in the xarray
  27. */
  28. static inline unsigned int xa_lock_type(const struct xarray *xa)
  29. {
  30. return (__force unsigned int)xa->xa_flags & 3;
  31. }
  32. static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type)
  33. {
  34. if (lock_type == XA_LOCK_IRQ)
  35. xas_lock_irq(xas);
  36. else if (lock_type == XA_LOCK_BH)
  37. xas_lock_bh(xas);
  38. else
  39. xas_lock(xas);
  40. }
  41. static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type)
  42. {
  43. if (lock_type == XA_LOCK_IRQ)
  44. xas_unlock_irq(xas);
  45. else if (lock_type == XA_LOCK_BH)
  46. xas_unlock_bh(xas);
  47. else
  48. xas_unlock(xas);
  49. }
  50. static inline bool xa_track_free(const struct xarray *xa)
  51. {
  52. return xa->xa_flags & XA_FLAGS_TRACK_FREE;
  53. }
  54. static inline bool xa_zero_busy(const struct xarray *xa)
  55. {
  56. return xa->xa_flags & XA_FLAGS_ZERO_BUSY;
  57. }
  58. static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark)
  59. {
  60. if (!(xa->xa_flags & XA_FLAGS_MARK(mark)))
  61. xa->xa_flags |= XA_FLAGS_MARK(mark);
  62. }
  63. static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark)
  64. {
  65. if (xa->xa_flags & XA_FLAGS_MARK(mark))
  66. xa->xa_flags &= ~(XA_FLAGS_MARK(mark));
  67. }
  68. static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark)
  69. {
  70. return node->marks[(__force unsigned)mark];
  71. }
  72. static inline bool node_get_mark(struct xa_node *node,
  73. unsigned int offset, xa_mark_t mark)
  74. {
  75. return test_bit(offset, node_marks(node, mark));
  76. }
  77. /* returns true if the bit was set */
  78. static inline bool node_set_mark(struct xa_node *node, unsigned int offset,
  79. xa_mark_t mark)
  80. {
  81. return __test_and_set_bit(offset, node_marks(node, mark));
  82. }
  83. /* returns true if the bit was set */
  84. static inline bool node_clear_mark(struct xa_node *node, unsigned int offset,
  85. xa_mark_t mark)
  86. {
  87. return __test_and_clear_bit(offset, node_marks(node, mark));
  88. }
  89. static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark)
  90. {
  91. return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE);
  92. }
  93. static inline void node_mark_all(struct xa_node *node, xa_mark_t mark)
  94. {
  95. bitmap_fill(node_marks(node, mark), XA_CHUNK_SIZE);
  96. }
  97. #define mark_inc(mark) do { \
  98. mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
  99. } while (0)
  100. /*
  101. * xas_squash_marks() - Merge all marks to the first entry
  102. * @xas: Array operation state.
  103. *
  104. * Set a mark on the first entry if any entry has it set. Clear marks on
  105. * all sibling entries.
  106. */
  107. static void xas_squash_marks(const struct xa_state *xas)
  108. {
  109. unsigned int mark = 0;
  110. unsigned int limit = xas->xa_offset + xas->xa_sibs + 1;
  111. if (!xas->xa_sibs)
  112. return;
  113. do {
  114. unsigned long *marks = xas->xa_node->marks[mark];
  115. if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit)
  116. continue;
  117. __set_bit(xas->xa_offset, marks);
  118. bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs);
  119. } while (mark++ != (__force unsigned)XA_MARK_MAX);
  120. }
  121. /* extracts the offset within this node from the index */
  122. static unsigned int get_offset(unsigned long index, struct xa_node *node)
  123. {
  124. return (index >> node->shift) & XA_CHUNK_MASK;
  125. }
  126. static void xas_set_offset(struct xa_state *xas)
  127. {
  128. xas->xa_offset = get_offset(xas->xa_index, xas->xa_node);
  129. }
  130. /* move the index either forwards (find) or backwards (sibling slot) */
  131. static void xas_move_index(struct xa_state *xas, unsigned long offset)
  132. {
  133. unsigned int shift = xas->xa_node->shift;
  134. xas->xa_index &= ~XA_CHUNK_MASK << shift;
  135. xas->xa_index += offset << shift;
  136. }
  137. static void xas_advance(struct xa_state *xas)
  138. {
  139. xas->xa_offset++;
  140. xas_move_index(xas, xas->xa_offset);
  141. }
  142. static void *set_bounds(struct xa_state *xas)
  143. {
  144. xas->xa_node = XAS_BOUNDS;
  145. return NULL;
  146. }
  147. /*
  148. * Starts a walk. If the @xas is already valid, we assume that it's on
  149. * the right path and just return where we've got to. If we're in an
  150. * error state, return NULL. If the index is outside the current scope
  151. * of the xarray, return NULL without changing @xas->xa_node. Otherwise
  152. * set @xas->xa_node to NULL and return the current head of the array.
  153. */
  154. static void *xas_start(struct xa_state *xas)
  155. {
  156. void *entry;
  157. if (xas_valid(xas))
  158. return xas_reload(xas);
  159. if (xas_error(xas))
  160. return NULL;
  161. entry = xa_head(xas->xa);
  162. if (!xa_is_node(entry)) {
  163. if (xas->xa_index)
  164. return set_bounds(xas);
  165. } else {
  166. if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK)
  167. return set_bounds(xas);
  168. }
  169. xas->xa_node = NULL;
  170. return entry;
  171. }
  172. static void *xas_descend(struct xa_state *xas, struct xa_node *node)
  173. {
  174. unsigned int offset = get_offset(xas->xa_index, node);
  175. void *entry = xa_entry(xas->xa, node, offset);
  176. xas->xa_node = node;
  177. if (xa_is_sibling(entry)) {
  178. offset = xa_to_sibling(entry);
  179. entry = xa_entry(xas->xa, node, offset);
  180. }
  181. xas->xa_offset = offset;
  182. return entry;
  183. }
  184. /**
  185. * xas_load() - Load an entry from the XArray (advanced).
  186. * @xas: XArray operation state.
  187. *
  188. * Usually walks the @xas to the appropriate state to load the entry
  189. * stored at xa_index. However, it will do nothing and return %NULL if
  190. * @xas is in an error state. xas_load() will never expand the tree.
  191. *
  192. * If the xa_state is set up to operate on a multi-index entry, xas_load()
  193. * may return %NULL or an internal entry, even if there are entries
  194. * present within the range specified by @xas.
  195. *
  196. * Context: Any context. The caller should hold the xa_lock or the RCU lock.
  197. * Return: Usually an entry in the XArray, but see description for exceptions.
  198. */
  199. void *xas_load(struct xa_state *xas)
  200. {
  201. void *entry = xas_start(xas);
  202. while (xa_is_node(entry)) {
  203. struct xa_node *node = xa_to_node(entry);
  204. if (xas->xa_shift > node->shift)
  205. break;
  206. entry = xas_descend(xas, node);
  207. if (node->shift == 0)
  208. break;
  209. }
  210. return entry;
  211. }
  212. EXPORT_SYMBOL_GPL(xas_load);
  213. /* Move the radix tree node cache here */
  214. extern struct kmem_cache *radix_tree_node_cachep;
  215. extern void radix_tree_node_rcu_free(struct rcu_head *head);
  216. #define XA_RCU_FREE ((struct xarray *)1)
  217. static void xa_node_free(struct xa_node *node)
  218. {
  219. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  220. node->array = XA_RCU_FREE;
  221. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  222. }
  223. /*
  224. * xas_destroy() - Free any resources allocated during the XArray operation.
  225. * @xas: XArray operation state.
  226. *
  227. * This function is now internal-only.
  228. */
  229. static void xas_destroy(struct xa_state *xas)
  230. {
  231. struct xa_node *next, *node = xas->xa_alloc;
  232. while (node) {
  233. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  234. next = rcu_dereference_raw(node->parent);
  235. radix_tree_node_rcu_free(&node->rcu_head);
  236. xas->xa_alloc = node = next;
  237. }
  238. }
  239. /**
  240. * xas_nomem() - Allocate memory if needed.
  241. * @xas: XArray operation state.
  242. * @gfp: Memory allocation flags.
  243. *
  244. * If we need to add new nodes to the XArray, we try to allocate memory
  245. * with GFP_NOWAIT while holding the lock, which will usually succeed.
  246. * If it fails, @xas is flagged as needing memory to continue. The caller
  247. * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
  248. * the caller should retry the operation.
  249. *
  250. * Forward progress is guaranteed as one node is allocated here and
  251. * stored in the xa_state where it will be found by xas_alloc(). More
  252. * nodes will likely be found in the slab allocator, but we do not tie
  253. * them up here.
  254. *
  255. * Return: true if memory was needed, and was successfully allocated.
  256. */
  257. bool xas_nomem(struct xa_state *xas, gfp_t gfp)
  258. {
  259. if (xas->xa_node != XA_ERROR(-ENOMEM)) {
  260. xas_destroy(xas);
  261. return false;
  262. }
  263. if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
  264. gfp |= __GFP_ACCOUNT;
  265. xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  266. if (!xas->xa_alloc)
  267. return false;
  268. xas->xa_alloc->parent = NULL;
  269. XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
  270. xas->xa_node = XAS_RESTART;
  271. return true;
  272. }
  273. EXPORT_SYMBOL_GPL(xas_nomem);
  274. /*
  275. * __xas_nomem() - Drop locks and allocate memory if needed.
  276. * @xas: XArray operation state.
  277. * @gfp: Memory allocation flags.
  278. *
  279. * Internal variant of xas_nomem().
  280. *
  281. * Return: true if memory was needed, and was successfully allocated.
  282. */
  283. static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
  284. __must_hold(xas->xa->xa_lock)
  285. {
  286. unsigned int lock_type = xa_lock_type(xas->xa);
  287. if (xas->xa_node != XA_ERROR(-ENOMEM)) {
  288. xas_destroy(xas);
  289. return false;
  290. }
  291. if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
  292. gfp |= __GFP_ACCOUNT;
  293. if (gfpflags_allow_blocking(gfp)) {
  294. xas_unlock_type(xas, lock_type);
  295. xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  296. xas_lock_type(xas, lock_type);
  297. } else {
  298. xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  299. }
  300. if (!xas->xa_alloc)
  301. return false;
  302. xas->xa_alloc->parent = NULL;
  303. XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
  304. xas->xa_node = XAS_RESTART;
  305. return true;
  306. }
  307. static void xas_update(struct xa_state *xas, struct xa_node *node)
  308. {
  309. if (xas->xa_update)
  310. xas->xa_update(node);
  311. else
  312. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  313. }
  314. static void *xas_alloc(struct xa_state *xas, unsigned int shift)
  315. {
  316. struct xa_node *parent = xas->xa_node;
  317. struct xa_node *node = xas->xa_alloc;
  318. if (xas_invalid(xas))
  319. return NULL;
  320. if (node) {
  321. xas->xa_alloc = NULL;
  322. } else {
  323. gfp_t gfp = GFP_NOWAIT | __GFP_NOWARN;
  324. if (xas->xa->xa_flags & XA_FLAGS_ACCOUNT)
  325. gfp |= __GFP_ACCOUNT;
  326. node = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  327. if (!node) {
  328. xas_set_err(xas, -ENOMEM);
  329. return NULL;
  330. }
  331. }
  332. if (parent) {
  333. node->offset = xas->xa_offset;
  334. parent->count++;
  335. XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
  336. xas_update(xas, parent);
  337. }
  338. XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
  339. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  340. node->shift = shift;
  341. node->count = 0;
  342. node->nr_values = 0;
  343. RCU_INIT_POINTER(node->parent, xas->xa_node);
  344. node->array = xas->xa;
  345. return node;
  346. }
  347. #ifdef CONFIG_XARRAY_MULTI
  348. /* Returns the number of indices covered by a given xa_state */
  349. static unsigned long xas_size(const struct xa_state *xas)
  350. {
  351. return (xas->xa_sibs + 1UL) << xas->xa_shift;
  352. }
  353. #endif
  354. /*
  355. * Use this to calculate the maximum index that will need to be created
  356. * in order to add the entry described by @xas. Because we cannot store a
  357. * multi-index entry at index 0, the calculation is a little more complex
  358. * than you might expect.
  359. */
  360. static unsigned long xas_max(struct xa_state *xas)
  361. {
  362. unsigned long max = xas->xa_index;
  363. #ifdef CONFIG_XARRAY_MULTI
  364. if (xas->xa_shift || xas->xa_sibs) {
  365. unsigned long mask = xas_size(xas) - 1;
  366. max |= mask;
  367. if (mask == max)
  368. max++;
  369. }
  370. #endif
  371. return max;
  372. }
  373. /* The maximum index that can be contained in the array without expanding it */
  374. static unsigned long max_index(void *entry)
  375. {
  376. if (!xa_is_node(entry))
  377. return 0;
  378. return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
  379. }
  380. static void xas_shrink(struct xa_state *xas)
  381. {
  382. struct xarray *xa = xas->xa;
  383. struct xa_node *node = xas->xa_node;
  384. for (;;) {
  385. void *entry;
  386. XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
  387. if (node->count != 1)
  388. break;
  389. entry = xa_entry_locked(xa, node, 0);
  390. if (!entry)
  391. break;
  392. if (!xa_is_node(entry) && node->shift)
  393. break;
  394. if (xa_is_zero(entry) && xa_zero_busy(xa))
  395. entry = NULL;
  396. xas->xa_node = XAS_BOUNDS;
  397. RCU_INIT_POINTER(xa->xa_head, entry);
  398. if (xa_track_free(xa) && !node_get_mark(node, 0, XA_FREE_MARK))
  399. xa_mark_clear(xa, XA_FREE_MARK);
  400. node->count = 0;
  401. node->nr_values = 0;
  402. if (!xa_is_node(entry))
  403. RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
  404. xas_update(xas, node);
  405. xa_node_free(node);
  406. if (!xa_is_node(entry))
  407. break;
  408. node = xa_to_node(entry);
  409. node->parent = NULL;
  410. }
  411. }
  412. /*
  413. * xas_delete_node() - Attempt to delete an xa_node
  414. * @xas: Array operation state.
  415. *
  416. * Attempts to delete the @xas->xa_node. This will fail if xa->node has
  417. * a non-zero reference count.
  418. */
  419. static void xas_delete_node(struct xa_state *xas)
  420. {
  421. struct xa_node *node = xas->xa_node;
  422. for (;;) {
  423. struct xa_node *parent;
  424. XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
  425. if (node->count)
  426. break;
  427. parent = xa_parent_locked(xas->xa, node);
  428. xas->xa_node = parent;
  429. xas->xa_offset = node->offset;
  430. xa_node_free(node);
  431. if (!parent) {
  432. xas->xa->xa_head = NULL;
  433. xas->xa_node = XAS_BOUNDS;
  434. return;
  435. }
  436. parent->slots[xas->xa_offset] = NULL;
  437. parent->count--;
  438. XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
  439. node = parent;
  440. xas_update(xas, node);
  441. }
  442. if (!node->parent)
  443. xas_shrink(xas);
  444. }
  445. /**
  446. * xas_free_nodes() - Free this node and all nodes that it references
  447. * @xas: Array operation state.
  448. * @top: Node to free
  449. *
  450. * This node has been removed from the tree. We must now free it and all
  451. * of its subnodes. There may be RCU walkers with references into the tree,
  452. * so we must replace all entries with retry markers.
  453. */
  454. static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
  455. {
  456. unsigned int offset = 0;
  457. struct xa_node *node = top;
  458. for (;;) {
  459. void *entry = xa_entry_locked(xas->xa, node, offset);
  460. if (node->shift && xa_is_node(entry)) {
  461. node = xa_to_node(entry);
  462. offset = 0;
  463. continue;
  464. }
  465. if (entry)
  466. RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
  467. offset++;
  468. while (offset == XA_CHUNK_SIZE) {
  469. struct xa_node *parent;
  470. parent = xa_parent_locked(xas->xa, node);
  471. offset = node->offset + 1;
  472. node->count = 0;
  473. node->nr_values = 0;
  474. xas_update(xas, node);
  475. xa_node_free(node);
  476. if (node == top)
  477. return;
  478. node = parent;
  479. }
  480. }
  481. }
  482. /*
  483. * xas_expand adds nodes to the head of the tree until it has reached
  484. * sufficient height to be able to contain @xas->xa_index
  485. */
  486. static int xas_expand(struct xa_state *xas, void *head)
  487. {
  488. struct xarray *xa = xas->xa;
  489. struct xa_node *node = NULL;
  490. unsigned int shift = 0;
  491. unsigned long max = xas_max(xas);
  492. if (!head) {
  493. if (max == 0)
  494. return 0;
  495. while ((max >> shift) >= XA_CHUNK_SIZE)
  496. shift += XA_CHUNK_SHIFT;
  497. return shift + XA_CHUNK_SHIFT;
  498. } else if (xa_is_node(head)) {
  499. node = xa_to_node(head);
  500. shift = node->shift + XA_CHUNK_SHIFT;
  501. }
  502. xas->xa_node = NULL;
  503. while (max > max_index(head)) {
  504. xa_mark_t mark = 0;
  505. XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
  506. node = xas_alloc(xas, shift);
  507. if (!node)
  508. return -ENOMEM;
  509. node->count = 1;
  510. if (xa_is_value(head))
  511. node->nr_values = 1;
  512. RCU_INIT_POINTER(node->slots[0], head);
  513. /* Propagate the aggregated mark info to the new child */
  514. for (;;) {
  515. if (xa_track_free(xa) && mark == XA_FREE_MARK) {
  516. node_mark_all(node, XA_FREE_MARK);
  517. if (!xa_marked(xa, XA_FREE_MARK)) {
  518. node_clear_mark(node, 0, XA_FREE_MARK);
  519. xa_mark_set(xa, XA_FREE_MARK);
  520. }
  521. } else if (xa_marked(xa, mark)) {
  522. node_set_mark(node, 0, mark);
  523. }
  524. if (mark == XA_MARK_MAX)
  525. break;
  526. mark_inc(mark);
  527. }
  528. /*
  529. * Now that the new node is fully initialised, we can add
  530. * it to the tree
  531. */
  532. if (xa_is_node(head)) {
  533. xa_to_node(head)->offset = 0;
  534. rcu_assign_pointer(xa_to_node(head)->parent, node);
  535. }
  536. head = xa_mk_node(node);
  537. rcu_assign_pointer(xa->xa_head, head);
  538. xas_update(xas, node);
  539. shift += XA_CHUNK_SHIFT;
  540. }
  541. xas->xa_node = node;
  542. return shift;
  543. }
  544. /*
  545. * xas_create() - Create a slot to store an entry in.
  546. * @xas: XArray operation state.
  547. * @allow_root: %true if we can store the entry in the root directly
  548. *
  549. * Most users will not need to call this function directly, as it is called
  550. * by xas_store(). It is useful for doing conditional store operations
  551. * (see the xa_cmpxchg() implementation for an example).
  552. *
  553. * Return: If the slot already existed, returns the contents of this slot.
  554. * If the slot was newly created, returns %NULL. If it failed to create the
  555. * slot, returns %NULL and indicates the error in @xas.
  556. */
  557. static void *xas_create(struct xa_state *xas, bool allow_root)
  558. {
  559. struct xarray *xa = xas->xa;
  560. void *entry;
  561. void __rcu **slot;
  562. struct xa_node *node = xas->xa_node;
  563. int shift;
  564. unsigned int order = xas->xa_shift;
  565. if (xas_top(node)) {
  566. entry = xa_head_locked(xa);
  567. xas->xa_node = NULL;
  568. if (!entry && xa_zero_busy(xa))
  569. entry = XA_ZERO_ENTRY;
  570. shift = xas_expand(xas, entry);
  571. if (shift < 0)
  572. return NULL;
  573. if (!shift && !allow_root)
  574. shift = XA_CHUNK_SHIFT;
  575. entry = xa_head_locked(xa);
  576. slot = &xa->xa_head;
  577. } else if (xas_error(xas)) {
  578. return NULL;
  579. } else if (node) {
  580. unsigned int offset = xas->xa_offset;
  581. shift = node->shift;
  582. entry = xa_entry_locked(xa, node, offset);
  583. slot = &node->slots[offset];
  584. } else {
  585. shift = 0;
  586. entry = xa_head_locked(xa);
  587. slot = &xa->xa_head;
  588. }
  589. while (shift > order) {
  590. shift -= XA_CHUNK_SHIFT;
  591. if (!entry) {
  592. node = xas_alloc(xas, shift);
  593. if (!node)
  594. break;
  595. if (xa_track_free(xa))
  596. node_mark_all(node, XA_FREE_MARK);
  597. rcu_assign_pointer(*slot, xa_mk_node(node));
  598. } else if (xa_is_node(entry)) {
  599. node = xa_to_node(entry);
  600. } else {
  601. break;
  602. }
  603. entry = xas_descend(xas, node);
  604. slot = &node->slots[xas->xa_offset];
  605. }
  606. return entry;
  607. }
  608. /**
  609. * xas_create_range() - Ensure that stores to this range will succeed
  610. * @xas: XArray operation state.
  611. *
  612. * Creates all of the slots in the range covered by @xas. Sets @xas to
  613. * create single-index entries and positions it at the beginning of the
  614. * range. This is for the benefit of users which have not yet been
  615. * converted to use multi-index entries.
  616. */
  617. void xas_create_range(struct xa_state *xas)
  618. {
  619. unsigned long index = xas->xa_index;
  620. unsigned char shift = xas->xa_shift;
  621. unsigned char sibs = xas->xa_sibs;
  622. xas->xa_index |= ((sibs + 1UL) << shift) - 1;
  623. if (xas_is_node(xas) && xas->xa_node->shift == xas->xa_shift)
  624. xas->xa_offset |= sibs;
  625. xas->xa_shift = 0;
  626. xas->xa_sibs = 0;
  627. for (;;) {
  628. xas_create(xas, true);
  629. if (xas_error(xas))
  630. goto restore;
  631. if (xas->xa_index <= (index | XA_CHUNK_MASK))
  632. goto success;
  633. xas->xa_index -= XA_CHUNK_SIZE;
  634. for (;;) {
  635. struct xa_node *node = xas->xa_node;
  636. if (node->shift >= shift)
  637. break;
  638. xas->xa_node = xa_parent_locked(xas->xa, node);
  639. xas->xa_offset = node->offset - 1;
  640. if (node->offset != 0)
  641. break;
  642. }
  643. }
  644. restore:
  645. xas->xa_shift = shift;
  646. xas->xa_sibs = sibs;
  647. xas->xa_index = index;
  648. return;
  649. success:
  650. xas->xa_index = index;
  651. if (xas->xa_node)
  652. xas_set_offset(xas);
  653. }
  654. EXPORT_SYMBOL_GPL(xas_create_range);
  655. static void update_node(struct xa_state *xas, struct xa_node *node,
  656. int count, int values)
  657. {
  658. if (!node || (!count && !values))
  659. return;
  660. node->count += count;
  661. node->nr_values += values;
  662. XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
  663. XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
  664. xas_update(xas, node);
  665. if (count < 0)
  666. xas_delete_node(xas);
  667. }
  668. /**
  669. * xas_store() - Store this entry in the XArray.
  670. * @xas: XArray operation state.
  671. * @entry: New entry.
  672. *
  673. * If @xas is operating on a multi-index entry, the entry returned by this
  674. * function is essentially meaningless (it may be an internal entry or it
  675. * may be %NULL, even if there are non-NULL entries at some of the indices
  676. * covered by the range). This is not a problem for any current users,
  677. * and can be changed if needed.
  678. *
  679. * Return: The old entry at this index.
  680. */
  681. void *xas_store(struct xa_state *xas, void *entry)
  682. {
  683. struct xa_node *node;
  684. void __rcu **slot = &xas->xa->xa_head;
  685. unsigned int offset, max;
  686. int count = 0;
  687. int values = 0;
  688. void *first, *next;
  689. bool value = xa_is_value(entry);
  690. if (entry) {
  691. bool allow_root = !xa_is_node(entry) && !xa_is_zero(entry);
  692. first = xas_create(xas, allow_root);
  693. } else {
  694. first = xas_load(xas);
  695. }
  696. if (xas_invalid(xas))
  697. return first;
  698. node = xas->xa_node;
  699. if (node && (xas->xa_shift < node->shift))
  700. xas->xa_sibs = 0;
  701. if ((first == entry) && !xas->xa_sibs)
  702. return first;
  703. next = first;
  704. offset = xas->xa_offset;
  705. max = xas->xa_offset + xas->xa_sibs;
  706. if (node) {
  707. slot = &node->slots[offset];
  708. if (xas->xa_sibs)
  709. xas_squash_marks(xas);
  710. }
  711. if (!entry)
  712. xas_init_marks(xas);
  713. for (;;) {
  714. /*
  715. * Must clear the marks before setting the entry to NULL,
  716. * otherwise xas_for_each_marked may find a NULL entry and
  717. * stop early. rcu_assign_pointer contains a release barrier
  718. * so the mark clearing will appear to happen before the
  719. * entry is set to NULL.
  720. */
  721. rcu_assign_pointer(*slot, entry);
  722. if (xa_is_node(next) && (!node || node->shift))
  723. xas_free_nodes(xas, xa_to_node(next));
  724. if (!node)
  725. break;
  726. count += !next - !entry;
  727. values += !xa_is_value(first) - !value;
  728. if (entry) {
  729. if (offset == max)
  730. break;
  731. if (!xa_is_sibling(entry))
  732. entry = xa_mk_sibling(xas->xa_offset);
  733. } else {
  734. if (offset == XA_CHUNK_MASK)
  735. break;
  736. }
  737. next = xa_entry_locked(xas->xa, node, ++offset);
  738. if (!xa_is_sibling(next)) {
  739. if (!entry && (offset > max))
  740. break;
  741. first = next;
  742. }
  743. slot++;
  744. }
  745. update_node(xas, node, count, values);
  746. return first;
  747. }
  748. EXPORT_SYMBOL_GPL(xas_store);
  749. /**
  750. * xas_get_mark() - Returns the state of this mark.
  751. * @xas: XArray operation state.
  752. * @mark: Mark number.
  753. *
  754. * Return: true if the mark is set, false if the mark is clear or @xas
  755. * is in an error state.
  756. */
  757. bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
  758. {
  759. if (xas_invalid(xas))
  760. return false;
  761. if (!xas->xa_node)
  762. return xa_marked(xas->xa, mark);
  763. return node_get_mark(xas->xa_node, xas->xa_offset, mark);
  764. }
  765. EXPORT_SYMBOL_GPL(xas_get_mark);
  766. /**
  767. * xas_set_mark() - Sets the mark on this entry and its parents.
  768. * @xas: XArray operation state.
  769. * @mark: Mark number.
  770. *
  771. * Sets the specified mark on this entry, and walks up the tree setting it
  772. * on all the ancestor entries. Does nothing if @xas has not been walked to
  773. * an entry, or is in an error state.
  774. */
  775. void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
  776. {
  777. struct xa_node *node = xas->xa_node;
  778. unsigned int offset = xas->xa_offset;
  779. if (xas_invalid(xas))
  780. return;
  781. while (node) {
  782. if (node_set_mark(node, offset, mark))
  783. return;
  784. offset = node->offset;
  785. node = xa_parent_locked(xas->xa, node);
  786. }
  787. if (!xa_marked(xas->xa, mark))
  788. xa_mark_set(xas->xa, mark);
  789. }
  790. EXPORT_SYMBOL_GPL(xas_set_mark);
  791. /**
  792. * xas_clear_mark() - Clears the mark on this entry and its parents.
  793. * @xas: XArray operation state.
  794. * @mark: Mark number.
  795. *
  796. * Clears the specified mark on this entry, and walks back to the head
  797. * attempting to clear it on all the ancestor entries. Does nothing if
  798. * @xas has not been walked to an entry, or is in an error state.
  799. */
  800. void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
  801. {
  802. struct xa_node *node = xas->xa_node;
  803. unsigned int offset = xas->xa_offset;
  804. if (xas_invalid(xas))
  805. return;
  806. while (node) {
  807. if (!node_clear_mark(node, offset, mark))
  808. return;
  809. if (node_any_mark(node, mark))
  810. return;
  811. offset = node->offset;
  812. node = xa_parent_locked(xas->xa, node);
  813. }
  814. if (xa_marked(xas->xa, mark))
  815. xa_mark_clear(xas->xa, mark);
  816. }
  817. EXPORT_SYMBOL_GPL(xas_clear_mark);
  818. /**
  819. * xas_init_marks() - Initialise all marks for the entry
  820. * @xas: Array operations state.
  821. *
  822. * Initialise all marks for the entry specified by @xas. If we're tracking
  823. * free entries with a mark, we need to set it on all entries. All other
  824. * marks are cleared.
  825. *
  826. * This implementation is not as efficient as it could be; we may walk
  827. * up the tree multiple times.
  828. */
  829. void xas_init_marks(const struct xa_state *xas)
  830. {
  831. xa_mark_t mark = 0;
  832. for (;;) {
  833. if (xa_track_free(xas->xa) && mark == XA_FREE_MARK)
  834. xas_set_mark(xas, mark);
  835. else
  836. xas_clear_mark(xas, mark);
  837. if (mark == XA_MARK_MAX)
  838. break;
  839. mark_inc(mark);
  840. }
  841. }
  842. EXPORT_SYMBOL_GPL(xas_init_marks);
  843. #ifdef CONFIG_XARRAY_MULTI
  844. static unsigned int node_get_marks(struct xa_node *node, unsigned int offset)
  845. {
  846. unsigned int marks = 0;
  847. xa_mark_t mark = XA_MARK_0;
  848. for (;;) {
  849. if (node_get_mark(node, offset, mark))
  850. marks |= 1 << (__force unsigned int)mark;
  851. if (mark == XA_MARK_MAX)
  852. break;
  853. mark_inc(mark);
  854. }
  855. return marks;
  856. }
  857. static void node_set_marks(struct xa_node *node, unsigned int offset,
  858. struct xa_node *child, unsigned int marks)
  859. {
  860. xa_mark_t mark = XA_MARK_0;
  861. for (;;) {
  862. if (marks & (1 << (__force unsigned int)mark)) {
  863. node_set_mark(node, offset, mark);
  864. if (child)
  865. node_mark_all(child, mark);
  866. }
  867. if (mark == XA_MARK_MAX)
  868. break;
  869. mark_inc(mark);
  870. }
  871. }
  872. /**
  873. * xas_split_alloc() - Allocate memory for splitting an entry.
  874. * @xas: XArray operation state.
  875. * @entry: New entry which will be stored in the array.
  876. * @order: New entry order.
  877. * @gfp: Memory allocation flags.
  878. *
  879. * This function should be called before calling xas_split().
  880. * If necessary, it will allocate new nodes (and fill them with @entry)
  881. * to prepare for the upcoming split of an entry of @order size into
  882. * entries of the order stored in the @xas.
  883. *
  884. * Context: May sleep if @gfp flags permit.
  885. */
  886. void xas_split_alloc(struct xa_state *xas, void *entry, unsigned int order,
  887. gfp_t gfp)
  888. {
  889. unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
  890. unsigned int mask = xas->xa_sibs;
  891. /* XXX: no support for splitting really large entries yet */
  892. if (WARN_ON(xas->xa_shift + 2 * XA_CHUNK_SHIFT < order))
  893. goto nomem;
  894. if (xas->xa_shift + XA_CHUNK_SHIFT > order)
  895. return;
  896. do {
  897. unsigned int i;
  898. void *sibling = NULL;
  899. struct xa_node *node;
  900. node = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  901. if (!node)
  902. goto nomem;
  903. node->array = xas->xa;
  904. for (i = 0; i < XA_CHUNK_SIZE; i++) {
  905. if ((i & mask) == 0) {
  906. RCU_INIT_POINTER(node->slots[i], entry);
  907. sibling = xa_mk_sibling(i);
  908. } else {
  909. RCU_INIT_POINTER(node->slots[i], sibling);
  910. }
  911. }
  912. RCU_INIT_POINTER(node->parent, xas->xa_alloc);
  913. xas->xa_alloc = node;
  914. } while (sibs-- > 0);
  915. return;
  916. nomem:
  917. xas_destroy(xas);
  918. xas_set_err(xas, -ENOMEM);
  919. }
  920. EXPORT_SYMBOL_GPL(xas_split_alloc);
  921. /**
  922. * xas_split() - Split a multi-index entry into smaller entries.
  923. * @xas: XArray operation state.
  924. * @entry: New entry to store in the array.
  925. * @order: New entry order.
  926. *
  927. * The value in the entry is copied to all the replacement entries.
  928. *
  929. * Context: Any context. The caller should hold the xa_lock.
  930. */
  931. void xas_split(struct xa_state *xas, void *entry, unsigned int order)
  932. {
  933. unsigned int sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
  934. unsigned int offset, marks;
  935. struct xa_node *node;
  936. void *curr = xas_load(xas);
  937. int values = 0;
  938. node = xas->xa_node;
  939. if (xas_top(node))
  940. return;
  941. marks = node_get_marks(node, xas->xa_offset);
  942. offset = xas->xa_offset + sibs;
  943. do {
  944. if (xas->xa_shift < node->shift) {
  945. struct xa_node *child = xas->xa_alloc;
  946. xas->xa_alloc = rcu_dereference_raw(child->parent);
  947. child->shift = node->shift - XA_CHUNK_SHIFT;
  948. child->offset = offset;
  949. child->count = XA_CHUNK_SIZE;
  950. child->nr_values = xa_is_value(entry) ?
  951. XA_CHUNK_SIZE : 0;
  952. RCU_INIT_POINTER(child->parent, node);
  953. node_set_marks(node, offset, child, marks);
  954. rcu_assign_pointer(node->slots[offset],
  955. xa_mk_node(child));
  956. if (xa_is_value(curr))
  957. values--;
  958. xas_update(xas, child);
  959. } else {
  960. unsigned int canon = offset - xas->xa_sibs;
  961. node_set_marks(node, canon, NULL, marks);
  962. rcu_assign_pointer(node->slots[canon], entry);
  963. while (offset > canon)
  964. rcu_assign_pointer(node->slots[offset--],
  965. xa_mk_sibling(canon));
  966. values += (xa_is_value(entry) - xa_is_value(curr)) *
  967. (xas->xa_sibs + 1);
  968. }
  969. } while (offset-- > xas->xa_offset);
  970. node->nr_values += values;
  971. xas_update(xas, node);
  972. }
  973. EXPORT_SYMBOL_GPL(xas_split);
  974. #endif
  975. /**
  976. * xas_pause() - Pause a walk to drop a lock.
  977. * @xas: XArray operation state.
  978. *
  979. * Some users need to pause a walk and drop the lock they're holding in
  980. * order to yield to a higher priority thread or carry out an operation
  981. * on an entry. Those users should call this function before they drop
  982. * the lock. It resets the @xas to be suitable for the next iteration
  983. * of the loop after the user has reacquired the lock. If most entries
  984. * found during a walk require you to call xas_pause(), the xa_for_each()
  985. * iterator may be more appropriate.
  986. *
  987. * Note that xas_pause() only works for forward iteration. If a user needs
  988. * to pause a reverse iteration, we will need a xas_pause_rev().
  989. */
  990. void xas_pause(struct xa_state *xas)
  991. {
  992. struct xa_node *node = xas->xa_node;
  993. if (xas_invalid(xas))
  994. return;
  995. xas->xa_node = XAS_RESTART;
  996. if (node) {
  997. unsigned long offset = xas->xa_offset;
  998. while (++offset < XA_CHUNK_SIZE) {
  999. if (!xa_is_sibling(xa_entry(xas->xa, node, offset)))
  1000. break;
  1001. }
  1002. xas->xa_index += (offset - xas->xa_offset) << node->shift;
  1003. if (xas->xa_index == 0)
  1004. xas->xa_node = XAS_BOUNDS;
  1005. } else {
  1006. xas->xa_index++;
  1007. }
  1008. }
  1009. EXPORT_SYMBOL_GPL(xas_pause);
  1010. /*
  1011. * __xas_prev() - Find the previous entry in the XArray.
  1012. * @xas: XArray operation state.
  1013. *
  1014. * Helper function for xas_prev() which handles all the complex cases
  1015. * out of line.
  1016. */
  1017. void *__xas_prev(struct xa_state *xas)
  1018. {
  1019. void *entry;
  1020. if (!xas_frozen(xas->xa_node))
  1021. xas->xa_index--;
  1022. if (!xas->xa_node)
  1023. return set_bounds(xas);
  1024. if (xas_not_node(xas->xa_node))
  1025. return xas_load(xas);
  1026. if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
  1027. xas->xa_offset--;
  1028. while (xas->xa_offset == 255) {
  1029. xas->xa_offset = xas->xa_node->offset - 1;
  1030. xas->xa_node = xa_parent(xas->xa, xas->xa_node);
  1031. if (!xas->xa_node)
  1032. return set_bounds(xas);
  1033. }
  1034. for (;;) {
  1035. entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
  1036. if (!xa_is_node(entry))
  1037. return entry;
  1038. xas->xa_node = xa_to_node(entry);
  1039. xas_set_offset(xas);
  1040. }
  1041. }
  1042. EXPORT_SYMBOL_GPL(__xas_prev);
  1043. /*
  1044. * __xas_next() - Find the next entry in the XArray.
  1045. * @xas: XArray operation state.
  1046. *
  1047. * Helper function for xas_next() which handles all the complex cases
  1048. * out of line.
  1049. */
  1050. void *__xas_next(struct xa_state *xas)
  1051. {
  1052. void *entry;
  1053. if (!xas_frozen(xas->xa_node))
  1054. xas->xa_index++;
  1055. if (!xas->xa_node)
  1056. return set_bounds(xas);
  1057. if (xas_not_node(xas->xa_node))
  1058. return xas_load(xas);
  1059. if (xas->xa_offset != get_offset(xas->xa_index, xas->xa_node))
  1060. xas->xa_offset++;
  1061. while (xas->xa_offset == XA_CHUNK_SIZE) {
  1062. xas->xa_offset = xas->xa_node->offset + 1;
  1063. xas->xa_node = xa_parent(xas->xa, xas->xa_node);
  1064. if (!xas->xa_node)
  1065. return set_bounds(xas);
  1066. }
  1067. for (;;) {
  1068. entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
  1069. if (!xa_is_node(entry))
  1070. return entry;
  1071. xas->xa_node = xa_to_node(entry);
  1072. xas_set_offset(xas);
  1073. }
  1074. }
  1075. EXPORT_SYMBOL_GPL(__xas_next);
  1076. /**
  1077. * xas_find() - Find the next present entry in the XArray.
  1078. * @xas: XArray operation state.
  1079. * @max: Highest index to return.
  1080. *
  1081. * If the @xas has not yet been walked to an entry, return the entry
  1082. * which has an index >= xas.xa_index. If it has been walked, the entry
  1083. * currently being pointed at has been processed, and so we move to the
  1084. * next entry.
  1085. *
  1086. * If no entry is found and the array is smaller than @max, the iterator
  1087. * is set to the smallest index not yet in the array. This allows @xas
  1088. * to be immediately passed to xas_store().
  1089. *
  1090. * Return: The entry, if found, otherwise %NULL.
  1091. */
  1092. void *xas_find(struct xa_state *xas, unsigned long max)
  1093. {
  1094. void *entry;
  1095. if (xas_error(xas) || xas->xa_node == XAS_BOUNDS)
  1096. return NULL;
  1097. if (xas->xa_index > max)
  1098. return set_bounds(xas);
  1099. if (!xas->xa_node) {
  1100. xas->xa_index = 1;
  1101. return set_bounds(xas);
  1102. } else if (xas->xa_node == XAS_RESTART) {
  1103. entry = xas_load(xas);
  1104. if (entry || xas_not_node(xas->xa_node))
  1105. return entry;
  1106. } else if (!xas->xa_node->shift &&
  1107. xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)) {
  1108. xas->xa_offset = ((xas->xa_index - 1) & XA_CHUNK_MASK) + 1;
  1109. }
  1110. xas_advance(xas);
  1111. while (xas->xa_node && (xas->xa_index <= max)) {
  1112. if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
  1113. xas->xa_offset = xas->xa_node->offset + 1;
  1114. xas->xa_node = xa_parent(xas->xa, xas->xa_node);
  1115. continue;
  1116. }
  1117. entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
  1118. if (xa_is_node(entry)) {
  1119. xas->xa_node = xa_to_node(entry);
  1120. xas->xa_offset = 0;
  1121. continue;
  1122. }
  1123. if (entry && !xa_is_sibling(entry))
  1124. return entry;
  1125. xas_advance(xas);
  1126. }
  1127. if (!xas->xa_node)
  1128. xas->xa_node = XAS_BOUNDS;
  1129. return NULL;
  1130. }
  1131. EXPORT_SYMBOL_GPL(xas_find);
  1132. /**
  1133. * xas_find_marked() - Find the next marked entry in the XArray.
  1134. * @xas: XArray operation state.
  1135. * @max: Highest index to return.
  1136. * @mark: Mark number to search for.
  1137. *
  1138. * If the @xas has not yet been walked to an entry, return the marked entry
  1139. * which has an index >= xas.xa_index. If it has been walked, the entry
  1140. * currently being pointed at has been processed, and so we return the
  1141. * first marked entry with an index > xas.xa_index.
  1142. *
  1143. * If no marked entry is found and the array is smaller than @max, @xas is
  1144. * set to the bounds state and xas->xa_index is set to the smallest index
  1145. * not yet in the array. This allows @xas to be immediately passed to
  1146. * xas_store().
  1147. *
  1148. * If no entry is found before @max is reached, @xas is set to the restart
  1149. * state.
  1150. *
  1151. * Return: The entry, if found, otherwise %NULL.
  1152. */
  1153. void *xas_find_marked(struct xa_state *xas, unsigned long max, xa_mark_t mark)
  1154. {
  1155. bool advance = true;
  1156. unsigned int offset;
  1157. void *entry;
  1158. if (xas_error(xas))
  1159. return NULL;
  1160. if (xas->xa_index > max)
  1161. goto max;
  1162. if (!xas->xa_node) {
  1163. xas->xa_index = 1;
  1164. goto out;
  1165. } else if (xas_top(xas->xa_node)) {
  1166. advance = false;
  1167. entry = xa_head(xas->xa);
  1168. xas->xa_node = NULL;
  1169. if (xas->xa_index > max_index(entry))
  1170. goto out;
  1171. if (!xa_is_node(entry)) {
  1172. if (xa_marked(xas->xa, mark))
  1173. return entry;
  1174. xas->xa_index = 1;
  1175. goto out;
  1176. }
  1177. xas->xa_node = xa_to_node(entry);
  1178. xas->xa_offset = xas->xa_index >> xas->xa_node->shift;
  1179. }
  1180. while (xas->xa_index <= max) {
  1181. if (unlikely(xas->xa_offset == XA_CHUNK_SIZE)) {
  1182. xas->xa_offset = xas->xa_node->offset + 1;
  1183. xas->xa_node = xa_parent(xas->xa, xas->xa_node);
  1184. if (!xas->xa_node)
  1185. break;
  1186. advance = false;
  1187. continue;
  1188. }
  1189. if (!advance) {
  1190. entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
  1191. if (xa_is_sibling(entry)) {
  1192. xas->xa_offset = xa_to_sibling(entry);
  1193. xas_move_index(xas, xas->xa_offset);
  1194. }
  1195. }
  1196. offset = xas_find_chunk(xas, advance, mark);
  1197. if (offset > xas->xa_offset) {
  1198. advance = false;
  1199. xas_move_index(xas, offset);
  1200. /* Mind the wrap */
  1201. if ((xas->xa_index - 1) >= max)
  1202. goto max;
  1203. xas->xa_offset = offset;
  1204. if (offset == XA_CHUNK_SIZE)
  1205. continue;
  1206. }
  1207. entry = xa_entry(xas->xa, xas->xa_node, xas->xa_offset);
  1208. if (!entry && !(xa_track_free(xas->xa) && mark == XA_FREE_MARK))
  1209. continue;
  1210. if (!xa_is_node(entry))
  1211. return entry;
  1212. xas->xa_node = xa_to_node(entry);
  1213. xas_set_offset(xas);
  1214. }
  1215. out:
  1216. if (xas->xa_index > max)
  1217. goto max;
  1218. return set_bounds(xas);
  1219. max:
  1220. xas->xa_node = XAS_RESTART;
  1221. return NULL;
  1222. }
  1223. EXPORT_SYMBOL_GPL(xas_find_marked);
  1224. /**
  1225. * xas_find_conflict() - Find the next present entry in a range.
  1226. * @xas: XArray operation state.
  1227. *
  1228. * The @xas describes both a range and a position within that range.
  1229. *
  1230. * Context: Any context. Expects xa_lock to be held.
  1231. * Return: The next entry in the range covered by @xas or %NULL.
  1232. */
  1233. void *xas_find_conflict(struct xa_state *xas)
  1234. {
  1235. void *curr;
  1236. if (xas_error(xas))
  1237. return NULL;
  1238. if (!xas->xa_node)
  1239. return NULL;
  1240. if (xas_top(xas->xa_node)) {
  1241. curr = xas_start(xas);
  1242. if (!curr)
  1243. return NULL;
  1244. while (xa_is_node(curr)) {
  1245. struct xa_node *node = xa_to_node(curr);
  1246. curr = xas_descend(xas, node);
  1247. }
  1248. if (curr)
  1249. return curr;
  1250. }
  1251. if (xas->xa_node->shift > xas->xa_shift)
  1252. return NULL;
  1253. for (;;) {
  1254. if (xas->xa_node->shift == xas->xa_shift) {
  1255. if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
  1256. break;
  1257. } else if (xas->xa_offset == XA_CHUNK_MASK) {
  1258. xas->xa_offset = xas->xa_node->offset;
  1259. xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
  1260. if (!xas->xa_node)
  1261. break;
  1262. continue;
  1263. }
  1264. curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
  1265. if (xa_is_sibling(curr))
  1266. continue;
  1267. while (xa_is_node(curr)) {
  1268. xas->xa_node = xa_to_node(curr);
  1269. xas->xa_offset = 0;
  1270. curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
  1271. }
  1272. if (curr)
  1273. return curr;
  1274. }
  1275. xas->xa_offset -= xas->xa_sibs;
  1276. return NULL;
  1277. }
  1278. EXPORT_SYMBOL_GPL(xas_find_conflict);
  1279. /**
  1280. * xa_load() - Load an entry from an XArray.
  1281. * @xa: XArray.
  1282. * @index: index into array.
  1283. *
  1284. * Context: Any context. Takes and releases the RCU lock.
  1285. * Return: The entry at @index in @xa.
  1286. */
  1287. void *xa_load(struct xarray *xa, unsigned long index)
  1288. {
  1289. XA_STATE(xas, xa, index);
  1290. void *entry;
  1291. rcu_read_lock();
  1292. do {
  1293. entry = xas_load(&xas);
  1294. if (xa_is_zero(entry))
  1295. entry = NULL;
  1296. } while (xas_retry(&xas, entry));
  1297. rcu_read_unlock();
  1298. return entry;
  1299. }
  1300. EXPORT_SYMBOL(xa_load);
  1301. static void *xas_result(struct xa_state *xas, void *curr)
  1302. {
  1303. if (xa_is_zero(curr))
  1304. return NULL;
  1305. if (xas_error(xas))
  1306. curr = xas->xa_node;
  1307. return curr;
  1308. }
  1309. /**
  1310. * __xa_erase() - Erase this entry from the XArray while locked.
  1311. * @xa: XArray.
  1312. * @index: Index into array.
  1313. *
  1314. * After this function returns, loading from @index will return %NULL.
  1315. * If the index is part of a multi-index entry, all indices will be erased
  1316. * and none of the entries will be part of a multi-index entry.
  1317. *
  1318. * Context: Any context. Expects xa_lock to be held on entry.
  1319. * Return: The entry which used to be at this index.
  1320. */
  1321. void *__xa_erase(struct xarray *xa, unsigned long index)
  1322. {
  1323. XA_STATE(xas, xa, index);
  1324. return xas_result(&xas, xas_store(&xas, NULL));
  1325. }
  1326. EXPORT_SYMBOL(__xa_erase);
  1327. /**
  1328. * xa_erase() - Erase this entry from the XArray.
  1329. * @xa: XArray.
  1330. * @index: Index of entry.
  1331. *
  1332. * After this function returns, loading from @index will return %NULL.
  1333. * If the index is part of a multi-index entry, all indices will be erased
  1334. * and none of the entries will be part of a multi-index entry.
  1335. *
  1336. * Context: Any context. Takes and releases the xa_lock.
  1337. * Return: The entry which used to be at this index.
  1338. */
  1339. void *xa_erase(struct xarray *xa, unsigned long index)
  1340. {
  1341. void *entry;
  1342. xa_lock(xa);
  1343. entry = __xa_erase(xa, index);
  1344. xa_unlock(xa);
  1345. return entry;
  1346. }
  1347. EXPORT_SYMBOL(xa_erase);
  1348. /**
  1349. * __xa_store() - Store this entry in the XArray.
  1350. * @xa: XArray.
  1351. * @index: Index into array.
  1352. * @entry: New entry.
  1353. * @gfp: Memory allocation flags.
  1354. *
  1355. * You must already be holding the xa_lock when calling this function.
  1356. * It will drop the lock if needed to allocate memory, and then reacquire
  1357. * it afterwards.
  1358. *
  1359. * Context: Any context. Expects xa_lock to be held on entry. May
  1360. * release and reacquire xa_lock if @gfp flags permit.
  1361. * Return: The old entry at this index or xa_err() if an error happened.
  1362. */
  1363. void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
  1364. {
  1365. XA_STATE(xas, xa, index);
  1366. void *curr;
  1367. if (WARN_ON_ONCE(xa_is_advanced(entry)))
  1368. return XA_ERROR(-EINVAL);
  1369. if (xa_track_free(xa) && !entry)
  1370. entry = XA_ZERO_ENTRY;
  1371. do {
  1372. curr = xas_store(&xas, entry);
  1373. if (xa_track_free(xa))
  1374. xas_clear_mark(&xas, XA_FREE_MARK);
  1375. } while (__xas_nomem(&xas, gfp));
  1376. return xas_result(&xas, curr);
  1377. }
  1378. EXPORT_SYMBOL(__xa_store);
  1379. /**
  1380. * xa_store() - Store this entry in the XArray.
  1381. * @xa: XArray.
  1382. * @index: Index into array.
  1383. * @entry: New entry.
  1384. * @gfp: Memory allocation flags.
  1385. *
  1386. * After this function returns, loads from this index will return @entry.
  1387. * Storing into an existing multi-index entry updates the entry of every index.
  1388. * The marks associated with @index are unaffected unless @entry is %NULL.
  1389. *
  1390. * Context: Any context. Takes and releases the xa_lock.
  1391. * May sleep if the @gfp flags permit.
  1392. * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
  1393. * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
  1394. * failed.
  1395. */
  1396. void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
  1397. {
  1398. void *curr;
  1399. xa_lock(xa);
  1400. curr = __xa_store(xa, index, entry, gfp);
  1401. xa_unlock(xa);
  1402. return curr;
  1403. }
  1404. EXPORT_SYMBOL(xa_store);
  1405. /**
  1406. * __xa_cmpxchg() - Store this entry in the XArray.
  1407. * @xa: XArray.
  1408. * @index: Index into array.
  1409. * @old: Old value to test against.
  1410. * @entry: New entry.
  1411. * @gfp: Memory allocation flags.
  1412. *
  1413. * You must already be holding the xa_lock when calling this function.
  1414. * It will drop the lock if needed to allocate memory, and then reacquire
  1415. * it afterwards.
  1416. *
  1417. * Context: Any context. Expects xa_lock to be held on entry. May
  1418. * release and reacquire xa_lock if @gfp flags permit.
  1419. * Return: The old entry at this index or xa_err() if an error happened.
  1420. */
  1421. void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
  1422. void *old, void *entry, gfp_t gfp)
  1423. {
  1424. XA_STATE(xas, xa, index);
  1425. void *curr;
  1426. if (WARN_ON_ONCE(xa_is_advanced(entry)))
  1427. return XA_ERROR(-EINVAL);
  1428. do {
  1429. curr = xas_load(&xas);
  1430. if (curr == old) {
  1431. xas_store(&xas, entry);
  1432. if (xa_track_free(xa) && entry && !curr)
  1433. xas_clear_mark(&xas, XA_FREE_MARK);
  1434. }
  1435. } while (__xas_nomem(&xas, gfp));
  1436. return xas_result(&xas, curr);
  1437. }
  1438. EXPORT_SYMBOL(__xa_cmpxchg);
  1439. /**
  1440. * __xa_insert() - Store this entry in the XArray if no entry is present.
  1441. * @xa: XArray.
  1442. * @index: Index into array.
  1443. * @entry: New entry.
  1444. * @gfp: Memory allocation flags.
  1445. *
  1446. * Inserting a NULL entry will store a reserved entry (like xa_reserve())
  1447. * if no entry is present. Inserting will fail if a reserved entry is
  1448. * present, even though loading from this index will return NULL.
  1449. *
  1450. * Context: Any context. Expects xa_lock to be held on entry. May
  1451. * release and reacquire xa_lock if @gfp flags permit.
  1452. * Return: 0 if the store succeeded. -EBUSY if another entry was present.
  1453. * -ENOMEM if memory could not be allocated.
  1454. */
  1455. int __xa_insert(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
  1456. {
  1457. XA_STATE(xas, xa, index);
  1458. void *curr;
  1459. if (WARN_ON_ONCE(xa_is_advanced(entry)))
  1460. return -EINVAL;
  1461. if (!entry)
  1462. entry = XA_ZERO_ENTRY;
  1463. do {
  1464. curr = xas_load(&xas);
  1465. if (!curr) {
  1466. xas_store(&xas, entry);
  1467. if (xa_track_free(xa))
  1468. xas_clear_mark(&xas, XA_FREE_MARK);
  1469. } else {
  1470. xas_set_err(&xas, -EBUSY);
  1471. }
  1472. } while (__xas_nomem(&xas, gfp));
  1473. return xas_error(&xas);
  1474. }
  1475. EXPORT_SYMBOL(__xa_insert);
  1476. #ifdef CONFIG_XARRAY_MULTI
  1477. static void xas_set_range(struct xa_state *xas, unsigned long first,
  1478. unsigned long last)
  1479. {
  1480. unsigned int shift = 0;
  1481. unsigned long sibs = last - first;
  1482. unsigned int offset = XA_CHUNK_MASK;
  1483. xas_set(xas, first);
  1484. while ((first & XA_CHUNK_MASK) == 0) {
  1485. if (sibs < XA_CHUNK_MASK)
  1486. break;
  1487. if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK))
  1488. break;
  1489. shift += XA_CHUNK_SHIFT;
  1490. if (offset == XA_CHUNK_MASK)
  1491. offset = sibs & XA_CHUNK_MASK;
  1492. sibs >>= XA_CHUNK_SHIFT;
  1493. first >>= XA_CHUNK_SHIFT;
  1494. }
  1495. offset = first & XA_CHUNK_MASK;
  1496. if (offset + sibs > XA_CHUNK_MASK)
  1497. sibs = XA_CHUNK_MASK - offset;
  1498. if ((((first + sibs + 1) << shift) - 1) > last)
  1499. sibs -= 1;
  1500. xas->xa_shift = shift;
  1501. xas->xa_sibs = sibs;
  1502. }
  1503. /**
  1504. * xa_store_range() - Store this entry at a range of indices in the XArray.
  1505. * @xa: XArray.
  1506. * @first: First index to affect.
  1507. * @last: Last index to affect.
  1508. * @entry: New entry.
  1509. * @gfp: Memory allocation flags.
  1510. *
  1511. * After this function returns, loads from any index between @first and @last,
  1512. * inclusive will return @entry.
  1513. * Storing into an existing multi-index entry updates the entry of every index.
  1514. * The marks associated with @index are unaffected unless @entry is %NULL.
  1515. *
  1516. * Context: Process context. Takes and releases the xa_lock. May sleep
  1517. * if the @gfp flags permit.
  1518. * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
  1519. * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
  1520. */
  1521. void *xa_store_range(struct xarray *xa, unsigned long first,
  1522. unsigned long last, void *entry, gfp_t gfp)
  1523. {
  1524. XA_STATE(xas, xa, 0);
  1525. if (WARN_ON_ONCE(xa_is_internal(entry)))
  1526. return XA_ERROR(-EINVAL);
  1527. if (last < first)
  1528. return XA_ERROR(-EINVAL);
  1529. do {
  1530. xas_lock(&xas);
  1531. if (entry) {
  1532. unsigned int order = BITS_PER_LONG;
  1533. if (last + 1)
  1534. order = __ffs(last + 1);
  1535. xas_set_order(&xas, last, order);
  1536. xas_create(&xas, true);
  1537. if (xas_error(&xas))
  1538. goto unlock;
  1539. }
  1540. do {
  1541. xas_set_range(&xas, first, last);
  1542. xas_store(&xas, entry);
  1543. if (xas_error(&xas))
  1544. goto unlock;
  1545. first += xas_size(&xas);
  1546. } while (first <= last);
  1547. unlock:
  1548. xas_unlock(&xas);
  1549. } while (xas_nomem(&xas, gfp));
  1550. return xas_result(&xas, NULL);
  1551. }
  1552. EXPORT_SYMBOL(xa_store_range);
  1553. /**
  1554. * xa_get_order() - Get the order of an entry.
  1555. * @xa: XArray.
  1556. * @index: Index of the entry.
  1557. *
  1558. * Return: A number between 0 and 63 indicating the order of the entry.
  1559. */
  1560. int xa_get_order(struct xarray *xa, unsigned long index)
  1561. {
  1562. XA_STATE(xas, xa, index);
  1563. void *entry;
  1564. int order = 0;
  1565. rcu_read_lock();
  1566. entry = xas_load(&xas);
  1567. if (!entry)
  1568. goto unlock;
  1569. if (!xas.xa_node)
  1570. goto unlock;
  1571. for (;;) {
  1572. unsigned int slot = xas.xa_offset + (1 << order);
  1573. if (slot >= XA_CHUNK_SIZE)
  1574. break;
  1575. if (!xa_is_sibling(xas.xa_node->slots[slot]))
  1576. break;
  1577. order++;
  1578. }
  1579. order += xas.xa_node->shift;
  1580. unlock:
  1581. rcu_read_unlock();
  1582. return order;
  1583. }
  1584. EXPORT_SYMBOL(xa_get_order);
  1585. #endif /* CONFIG_XARRAY_MULTI */
  1586. /**
  1587. * __xa_alloc() - Find somewhere to store this entry in the XArray.
  1588. * @xa: XArray.
  1589. * @id: Pointer to ID.
  1590. * @limit: Range for allocated ID.
  1591. * @entry: New entry.
  1592. * @gfp: Memory allocation flags.
  1593. *
  1594. * Finds an empty entry in @xa between @limit.min and @limit.max,
  1595. * stores the index into the @id pointer, then stores the entry at
  1596. * that index. A concurrent lookup will not see an uninitialised @id.
  1597. *
  1598. * Context: Any context. Expects xa_lock to be held on entry. May
  1599. * release and reacquire xa_lock if @gfp flags permit.
  1600. * Return: 0 on success, -ENOMEM if memory could not be allocated or
  1601. * -EBUSY if there are no free entries in @limit.
  1602. */
  1603. int __xa_alloc(struct xarray *xa, u32 *id, void *entry,
  1604. struct xa_limit limit, gfp_t gfp)
  1605. {
  1606. XA_STATE(xas, xa, 0);
  1607. if (WARN_ON_ONCE(xa_is_advanced(entry)))
  1608. return -EINVAL;
  1609. if (WARN_ON_ONCE(!xa_track_free(xa)))
  1610. return -EINVAL;
  1611. if (!entry)
  1612. entry = XA_ZERO_ENTRY;
  1613. do {
  1614. xas.xa_index = limit.min;
  1615. xas_find_marked(&xas, limit.max, XA_FREE_MARK);
  1616. if (xas.xa_node == XAS_RESTART)
  1617. xas_set_err(&xas, -EBUSY);
  1618. else
  1619. *id = xas.xa_index;
  1620. xas_store(&xas, entry);
  1621. xas_clear_mark(&xas, XA_FREE_MARK);
  1622. } while (__xas_nomem(&xas, gfp));
  1623. return xas_error(&xas);
  1624. }
  1625. EXPORT_SYMBOL(__xa_alloc);
  1626. /**
  1627. * __xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
  1628. * @xa: XArray.
  1629. * @id: Pointer to ID.
  1630. * @entry: New entry.
  1631. * @limit: Range of allocated ID.
  1632. * @next: Pointer to next ID to allocate.
  1633. * @gfp: Memory allocation flags.
  1634. *
  1635. * Finds an empty entry in @xa between @limit.min and @limit.max,
  1636. * stores the index into the @id pointer, then stores the entry at
  1637. * that index. A concurrent lookup will not see an uninitialised @id.
  1638. * The search for an empty entry will start at @next and will wrap
  1639. * around if necessary.
  1640. *
  1641. * Context: Any context. Expects xa_lock to be held on entry. May
  1642. * release and reacquire xa_lock if @gfp flags permit.
  1643. * Return: 0 if the allocation succeeded without wrapping. 1 if the
  1644. * allocation succeeded after wrapping, -ENOMEM if memory could not be
  1645. * allocated or -EBUSY if there are no free entries in @limit.
  1646. */
  1647. int __xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
  1648. struct xa_limit limit, u32 *next, gfp_t gfp)
  1649. {
  1650. u32 min = limit.min;
  1651. int ret;
  1652. limit.min = max(min, *next);
  1653. ret = __xa_alloc(xa, id, entry, limit, gfp);
  1654. if ((xa->xa_flags & XA_FLAGS_ALLOC_WRAPPED) && ret == 0) {
  1655. xa->xa_flags &= ~XA_FLAGS_ALLOC_WRAPPED;
  1656. ret = 1;
  1657. }
  1658. if (ret < 0 && limit.min > min) {
  1659. limit.min = min;
  1660. ret = __xa_alloc(xa, id, entry, limit, gfp);
  1661. if (ret == 0)
  1662. ret = 1;
  1663. }
  1664. if (ret >= 0) {
  1665. *next = *id + 1;
  1666. if (*next == 0)
  1667. xa->xa_flags |= XA_FLAGS_ALLOC_WRAPPED;
  1668. }
  1669. return ret;
  1670. }
  1671. EXPORT_SYMBOL(__xa_alloc_cyclic);
  1672. /**
  1673. * __xa_set_mark() - Set this mark on this entry while locked.
  1674. * @xa: XArray.
  1675. * @index: Index of entry.
  1676. * @mark: Mark number.
  1677. *
  1678. * Attempting to set a mark on a %NULL entry does not succeed.
  1679. *
  1680. * Context: Any context. Expects xa_lock to be held on entry.
  1681. */
  1682. void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1683. {
  1684. XA_STATE(xas, xa, index);
  1685. void *entry = xas_load(&xas);
  1686. if (entry)
  1687. xas_set_mark(&xas, mark);
  1688. }
  1689. EXPORT_SYMBOL(__xa_set_mark);
  1690. /**
  1691. * __xa_clear_mark() - Clear this mark on this entry while locked.
  1692. * @xa: XArray.
  1693. * @index: Index of entry.
  1694. * @mark: Mark number.
  1695. *
  1696. * Context: Any context. Expects xa_lock to be held on entry.
  1697. */
  1698. void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1699. {
  1700. XA_STATE(xas, xa, index);
  1701. void *entry = xas_load(&xas);
  1702. if (entry)
  1703. xas_clear_mark(&xas, mark);
  1704. }
  1705. EXPORT_SYMBOL(__xa_clear_mark);
  1706. /**
  1707. * xa_get_mark() - Inquire whether this mark is set on this entry.
  1708. * @xa: XArray.
  1709. * @index: Index of entry.
  1710. * @mark: Mark number.
  1711. *
  1712. * This function uses the RCU read lock, so the result may be out of date
  1713. * by the time it returns. If you need the result to be stable, use a lock.
  1714. *
  1715. * Context: Any context. Takes and releases the RCU lock.
  1716. * Return: True if the entry at @index has this mark set, false if it doesn't.
  1717. */
  1718. bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1719. {
  1720. XA_STATE(xas, xa, index);
  1721. void *entry;
  1722. rcu_read_lock();
  1723. entry = xas_start(&xas);
  1724. while (xas_get_mark(&xas, mark)) {
  1725. if (!xa_is_node(entry))
  1726. goto found;
  1727. entry = xas_descend(&xas, xa_to_node(entry));
  1728. }
  1729. rcu_read_unlock();
  1730. return false;
  1731. found:
  1732. rcu_read_unlock();
  1733. return true;
  1734. }
  1735. EXPORT_SYMBOL(xa_get_mark);
  1736. /**
  1737. * xa_set_mark() - Set this mark on this entry.
  1738. * @xa: XArray.
  1739. * @index: Index of entry.
  1740. * @mark: Mark number.
  1741. *
  1742. * Attempting to set a mark on a %NULL entry does not succeed.
  1743. *
  1744. * Context: Process context. Takes and releases the xa_lock.
  1745. */
  1746. void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1747. {
  1748. xa_lock(xa);
  1749. __xa_set_mark(xa, index, mark);
  1750. xa_unlock(xa);
  1751. }
  1752. EXPORT_SYMBOL(xa_set_mark);
  1753. /**
  1754. * xa_clear_mark() - Clear this mark on this entry.
  1755. * @xa: XArray.
  1756. * @index: Index of entry.
  1757. * @mark: Mark number.
  1758. *
  1759. * Clearing a mark always succeeds.
  1760. *
  1761. * Context: Process context. Takes and releases the xa_lock.
  1762. */
  1763. void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1764. {
  1765. xa_lock(xa);
  1766. __xa_clear_mark(xa, index, mark);
  1767. xa_unlock(xa);
  1768. }
  1769. EXPORT_SYMBOL(xa_clear_mark);
  1770. /**
  1771. * xa_find() - Search the XArray for an entry.
  1772. * @xa: XArray.
  1773. * @indexp: Pointer to an index.
  1774. * @max: Maximum index to search to.
  1775. * @filter: Selection criterion.
  1776. *
  1777. * Finds the entry in @xa which matches the @filter, and has the lowest
  1778. * index that is at least @indexp and no more than @max.
  1779. * If an entry is found, @indexp is updated to be the index of the entry.
  1780. * This function is protected by the RCU read lock, so it may not find
  1781. * entries which are being simultaneously added. It will not return an
  1782. * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
  1783. *
  1784. * Context: Any context. Takes and releases the RCU lock.
  1785. * Return: The entry, if found, otherwise %NULL.
  1786. */
  1787. void *xa_find(struct xarray *xa, unsigned long *indexp,
  1788. unsigned long max, xa_mark_t filter)
  1789. {
  1790. XA_STATE(xas, xa, *indexp);
  1791. void *entry;
  1792. rcu_read_lock();
  1793. do {
  1794. if ((__force unsigned int)filter < XA_MAX_MARKS)
  1795. entry = xas_find_marked(&xas, max, filter);
  1796. else
  1797. entry = xas_find(&xas, max);
  1798. } while (xas_retry(&xas, entry));
  1799. rcu_read_unlock();
  1800. if (entry)
  1801. *indexp = xas.xa_index;
  1802. return entry;
  1803. }
  1804. EXPORT_SYMBOL(xa_find);
  1805. static bool xas_sibling(struct xa_state *xas)
  1806. {
  1807. struct xa_node *node = xas->xa_node;
  1808. unsigned long mask;
  1809. if (!IS_ENABLED(CONFIG_XARRAY_MULTI) || !node)
  1810. return false;
  1811. mask = (XA_CHUNK_SIZE << node->shift) - 1;
  1812. return (xas->xa_index & mask) >
  1813. ((unsigned long)xas->xa_offset << node->shift);
  1814. }
  1815. /**
  1816. * xa_find_after() - Search the XArray for a present entry.
  1817. * @xa: XArray.
  1818. * @indexp: Pointer to an index.
  1819. * @max: Maximum index to search to.
  1820. * @filter: Selection criterion.
  1821. *
  1822. * Finds the entry in @xa which matches the @filter and has the lowest
  1823. * index that is above @indexp and no more than @max.
  1824. * If an entry is found, @indexp is updated to be the index of the entry.
  1825. * This function is protected by the RCU read lock, so it may miss entries
  1826. * which are being simultaneously added. It will not return an
  1827. * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
  1828. *
  1829. * Context: Any context. Takes and releases the RCU lock.
  1830. * Return: The pointer, if found, otherwise %NULL.
  1831. */
  1832. void *xa_find_after(struct xarray *xa, unsigned long *indexp,
  1833. unsigned long max, xa_mark_t filter)
  1834. {
  1835. XA_STATE(xas, xa, *indexp + 1);
  1836. void *entry;
  1837. if (xas.xa_index == 0)
  1838. return NULL;
  1839. rcu_read_lock();
  1840. for (;;) {
  1841. if ((__force unsigned int)filter < XA_MAX_MARKS)
  1842. entry = xas_find_marked(&xas, max, filter);
  1843. else
  1844. entry = xas_find(&xas, max);
  1845. if (xas_invalid(&xas))
  1846. break;
  1847. if (xas_sibling(&xas))
  1848. continue;
  1849. if (!xas_retry(&xas, entry))
  1850. break;
  1851. }
  1852. rcu_read_unlock();
  1853. if (entry)
  1854. *indexp = xas.xa_index;
  1855. return entry;
  1856. }
  1857. EXPORT_SYMBOL(xa_find_after);
  1858. static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
  1859. unsigned long max, unsigned int n)
  1860. {
  1861. void *entry;
  1862. unsigned int i = 0;
  1863. rcu_read_lock();
  1864. xas_for_each(xas, entry, max) {
  1865. if (xas_retry(xas, entry))
  1866. continue;
  1867. dst[i++] = entry;
  1868. if (i == n)
  1869. break;
  1870. }
  1871. rcu_read_unlock();
  1872. return i;
  1873. }
  1874. static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
  1875. unsigned long max, unsigned int n, xa_mark_t mark)
  1876. {
  1877. void *entry;
  1878. unsigned int i = 0;
  1879. rcu_read_lock();
  1880. xas_for_each_marked(xas, entry, max, mark) {
  1881. if (xas_retry(xas, entry))
  1882. continue;
  1883. dst[i++] = entry;
  1884. if (i == n)
  1885. break;
  1886. }
  1887. rcu_read_unlock();
  1888. return i;
  1889. }
  1890. /**
  1891. * xa_extract() - Copy selected entries from the XArray into a normal array.
  1892. * @xa: The source XArray to copy from.
  1893. * @dst: The buffer to copy entries into.
  1894. * @start: The first index in the XArray eligible to be selected.
  1895. * @max: The last index in the XArray eligible to be selected.
  1896. * @n: The maximum number of entries to copy.
  1897. * @filter: Selection criterion.
  1898. *
  1899. * Copies up to @n entries that match @filter from the XArray. The
  1900. * copied entries will have indices between @start and @max, inclusive.
  1901. *
  1902. * The @filter may be an XArray mark value, in which case entries which are
  1903. * marked with that mark will be copied. It may also be %XA_PRESENT, in
  1904. * which case all entries which are not %NULL will be copied.
  1905. *
  1906. * The entries returned may not represent a snapshot of the XArray at a
  1907. * moment in time. For example, if another thread stores to index 5, then
  1908. * index 10, calling xa_extract() may return the old contents of index 5
  1909. * and the new contents of index 10. Indices not modified while this
  1910. * function is running will not be skipped.
  1911. *
  1912. * If you need stronger guarantees, holding the xa_lock across calls to this
  1913. * function will prevent concurrent modification.
  1914. *
  1915. * Context: Any context. Takes and releases the RCU lock.
  1916. * Return: The number of entries copied.
  1917. */
  1918. unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
  1919. unsigned long max, unsigned int n, xa_mark_t filter)
  1920. {
  1921. XA_STATE(xas, xa, start);
  1922. if (!n)
  1923. return 0;
  1924. if ((__force unsigned int)filter < XA_MAX_MARKS)
  1925. return xas_extract_marked(&xas, dst, max, n, filter);
  1926. return xas_extract_present(&xas, dst, max, n);
  1927. }
  1928. EXPORT_SYMBOL(xa_extract);
  1929. /**
  1930. * xa_delete_node() - Private interface for workingset code.
  1931. * @node: Node to be removed from the tree.
  1932. * @update: Function to call to update ancestor nodes.
  1933. *
  1934. * Context: xa_lock must be held on entry and will not be released.
  1935. */
  1936. void xa_delete_node(struct xa_node *node, xa_update_node_t update)
  1937. {
  1938. struct xa_state xas = {
  1939. .xa = node->array,
  1940. .xa_index = (unsigned long)node->offset <<
  1941. (node->shift + XA_CHUNK_SHIFT),
  1942. .xa_shift = node->shift + XA_CHUNK_SHIFT,
  1943. .xa_offset = node->offset,
  1944. .xa_node = xa_parent_locked(node->array, node),
  1945. .xa_update = update,
  1946. };
  1947. xas_store(&xas, NULL);
  1948. }
  1949. EXPORT_SYMBOL_GPL(xa_delete_node); /* For the benefit of the test suite */
  1950. /**
  1951. * xa_destroy() - Free all internal data structures.
  1952. * @xa: XArray.
  1953. *
  1954. * After calling this function, the XArray is empty and has freed all memory
  1955. * allocated for its internal data structures. You are responsible for
  1956. * freeing the objects referenced by the XArray.
  1957. *
  1958. * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
  1959. */
  1960. void xa_destroy(struct xarray *xa)
  1961. {
  1962. XA_STATE(xas, xa, 0);
  1963. unsigned long flags;
  1964. void *entry;
  1965. xas.xa_node = NULL;
  1966. xas_lock_irqsave(&xas, flags);
  1967. entry = xa_head_locked(xa);
  1968. RCU_INIT_POINTER(xa->xa_head, NULL);
  1969. xas_init_marks(&xas);
  1970. if (xa_zero_busy(xa))
  1971. xa_mark_clear(xa, XA_FREE_MARK);
  1972. /* lockdep checks we're still holding the lock in xas_free_nodes() */
  1973. if (xa_is_node(entry))
  1974. xas_free_nodes(&xas, xa_to_node(entry));
  1975. xas_unlock_irqrestore(&xas, flags);
  1976. }
  1977. EXPORT_SYMBOL(xa_destroy);
  1978. #ifdef XA_DEBUG
  1979. void xa_dump_node(const struct xa_node *node)
  1980. {
  1981. unsigned i, j;
  1982. if (!node)
  1983. return;
  1984. if ((unsigned long)node & 3) {
  1985. pr_cont("node %px\n", node);
  1986. return;
  1987. }
  1988. pr_cont("node %px %s %d parent %px shift %d count %d values %d "
  1989. "array %px list %px %px marks",
  1990. node, node->parent ? "offset" : "max", node->offset,
  1991. node->parent, node->shift, node->count, node->nr_values,
  1992. node->array, node->private_list.prev, node->private_list.next);
  1993. for (i = 0; i < XA_MAX_MARKS; i++)
  1994. for (j = 0; j < XA_MARK_LONGS; j++)
  1995. pr_cont(" %lx", node->marks[i][j]);
  1996. pr_cont("\n");
  1997. }
  1998. void xa_dump_index(unsigned long index, unsigned int shift)
  1999. {
  2000. if (!shift)
  2001. pr_info("%lu: ", index);
  2002. else if (shift >= BITS_PER_LONG)
  2003. pr_info("0-%lu: ", ~0UL);
  2004. else
  2005. pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
  2006. }
  2007. void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
  2008. {
  2009. if (!entry)
  2010. return;
  2011. xa_dump_index(index, shift);
  2012. if (xa_is_node(entry)) {
  2013. if (shift == 0) {
  2014. pr_cont("%px\n", entry);
  2015. } else {
  2016. unsigned long i;
  2017. struct xa_node *node = xa_to_node(entry);
  2018. xa_dump_node(node);
  2019. for (i = 0; i < XA_CHUNK_SIZE; i++)
  2020. xa_dump_entry(node->slots[i],
  2021. index + (i << node->shift), node->shift);
  2022. }
  2023. } else if (xa_is_value(entry))
  2024. pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
  2025. xa_to_value(entry), entry);
  2026. else if (!xa_is_internal(entry))
  2027. pr_cont("%px\n", entry);
  2028. else if (xa_is_retry(entry))
  2029. pr_cont("retry (%ld)\n", xa_to_internal(entry));
  2030. else if (xa_is_sibling(entry))
  2031. pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
  2032. else if (xa_is_zero(entry))
  2033. pr_cont("zero (%ld)\n", xa_to_internal(entry));
  2034. else
  2035. pr_cont("UNKNOWN ENTRY (%px)\n", entry);
  2036. }
  2037. void xa_dump(const struct xarray *xa)
  2038. {
  2039. void *entry = xa->xa_head;
  2040. unsigned int shift = 0;
  2041. pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
  2042. xa->xa_flags, xa_marked(xa, XA_MARK_0),
  2043. xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
  2044. if (xa_is_node(entry))
  2045. shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
  2046. xa_dump_entry(entry, 0, shift);
  2047. }
  2048. #endif