xarray.c 47 KB

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