xarray.c 51 KB

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