xarray.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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 out;
  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 (xas->xa_index > max)
  1046. goto max;
  1047. return set_bounds(xas);
  1048. max:
  1049. xas->xa_node = XAS_RESTART;
  1050. return NULL;
  1051. }
  1052. EXPORT_SYMBOL_GPL(xas_find_marked);
  1053. /**
  1054. * xas_find_conflict() - Find the next present entry in a range.
  1055. * @xas: XArray operation state.
  1056. *
  1057. * The @xas describes both a range and a position within that range.
  1058. *
  1059. * Context: Any context. Expects xa_lock to be held.
  1060. * Return: The next entry in the range covered by @xas or %NULL.
  1061. */
  1062. void *xas_find_conflict(struct xa_state *xas)
  1063. {
  1064. void *curr;
  1065. if (xas_error(xas))
  1066. return NULL;
  1067. if (!xas->xa_node)
  1068. return NULL;
  1069. if (xas_top(xas->xa_node)) {
  1070. curr = xas_start(xas);
  1071. if (!curr)
  1072. return NULL;
  1073. while (xa_is_node(curr)) {
  1074. struct xa_node *node = xa_to_node(curr);
  1075. curr = xas_descend(xas, node);
  1076. }
  1077. if (curr)
  1078. return curr;
  1079. }
  1080. if (xas->xa_node->shift > xas->xa_shift)
  1081. return NULL;
  1082. for (;;) {
  1083. if (xas->xa_node->shift == xas->xa_shift) {
  1084. if ((xas->xa_offset & xas->xa_sibs) == xas->xa_sibs)
  1085. break;
  1086. } else if (xas->xa_offset == XA_CHUNK_MASK) {
  1087. xas->xa_offset = xas->xa_node->offset;
  1088. xas->xa_node = xa_parent_locked(xas->xa, xas->xa_node);
  1089. if (!xas->xa_node)
  1090. break;
  1091. continue;
  1092. }
  1093. curr = xa_entry_locked(xas->xa, xas->xa_node, ++xas->xa_offset);
  1094. if (xa_is_sibling(curr))
  1095. continue;
  1096. while (xa_is_node(curr)) {
  1097. xas->xa_node = xa_to_node(curr);
  1098. xas->xa_offset = 0;
  1099. curr = xa_entry_locked(xas->xa, xas->xa_node, 0);
  1100. }
  1101. if (curr)
  1102. return curr;
  1103. }
  1104. xas->xa_offset -= xas->xa_sibs;
  1105. return NULL;
  1106. }
  1107. EXPORT_SYMBOL_GPL(xas_find_conflict);
  1108. /**
  1109. * xa_init_flags() - Initialise an empty XArray with flags.
  1110. * @xa: XArray.
  1111. * @flags: XA_FLAG values.
  1112. *
  1113. * If you need to initialise an XArray with special flags (eg you need
  1114. * to take the lock from interrupt context), use this function instead
  1115. * of xa_init().
  1116. *
  1117. * Context: Any context.
  1118. */
  1119. void xa_init_flags(struct xarray *xa, gfp_t flags)
  1120. {
  1121. unsigned int lock_type;
  1122. static struct lock_class_key xa_lock_irq;
  1123. static struct lock_class_key xa_lock_bh;
  1124. spin_lock_init(&xa->xa_lock);
  1125. xa->xa_flags = flags;
  1126. xa->xa_head = NULL;
  1127. lock_type = xa_lock_type(xa);
  1128. if (lock_type == XA_LOCK_IRQ)
  1129. lockdep_set_class(&xa->xa_lock, &xa_lock_irq);
  1130. else if (lock_type == XA_LOCK_BH)
  1131. lockdep_set_class(&xa->xa_lock, &xa_lock_bh);
  1132. }
  1133. EXPORT_SYMBOL(xa_init_flags);
  1134. /**
  1135. * xa_load() - Load an entry from an XArray.
  1136. * @xa: XArray.
  1137. * @index: index into array.
  1138. *
  1139. * Context: Any context. Takes and releases the RCU lock.
  1140. * Return: The entry at @index in @xa.
  1141. */
  1142. void *xa_load(struct xarray *xa, unsigned long index)
  1143. {
  1144. XA_STATE(xas, xa, index);
  1145. void *entry;
  1146. rcu_read_lock();
  1147. do {
  1148. entry = xas_load(&xas);
  1149. if (xa_is_zero(entry))
  1150. entry = NULL;
  1151. } while (xas_retry(&xas, entry));
  1152. rcu_read_unlock();
  1153. return entry;
  1154. }
  1155. EXPORT_SYMBOL(xa_load);
  1156. static void *xas_result(struct xa_state *xas, void *curr)
  1157. {
  1158. if (xa_is_zero(curr))
  1159. return NULL;
  1160. XA_NODE_BUG_ON(xas->xa_node, xa_is_internal(curr));
  1161. if (xas_error(xas))
  1162. curr = xas->xa_node;
  1163. return curr;
  1164. }
  1165. /**
  1166. * __xa_erase() - Erase this entry from the XArray while locked.
  1167. * @xa: XArray.
  1168. * @index: Index into array.
  1169. *
  1170. * If the entry at this index is a multi-index entry then all indices will
  1171. * be erased, and the entry will no longer be a multi-index entry.
  1172. * This function expects the xa_lock to be held on entry.
  1173. *
  1174. * Context: Any context. Expects xa_lock to be held on entry. May
  1175. * release and reacquire xa_lock if @gfp flags permit.
  1176. * Return: The old entry at this index.
  1177. */
  1178. void *__xa_erase(struct xarray *xa, unsigned long index)
  1179. {
  1180. XA_STATE(xas, xa, index);
  1181. return xas_result(&xas, xas_store(&xas, NULL));
  1182. }
  1183. EXPORT_SYMBOL(__xa_erase);
  1184. /**
  1185. * xa_erase() - Erase this entry from the XArray.
  1186. * @xa: XArray.
  1187. * @index: Index of entry.
  1188. *
  1189. * This function is the equivalent of calling xa_store() with %NULL as
  1190. * the third argument. The XArray does not need to allocate memory, so
  1191. * the user does not need to provide GFP flags.
  1192. *
  1193. * Context: Any context. Takes and releases the xa_lock.
  1194. * Return: The entry which used to be at this index.
  1195. */
  1196. void *xa_erase(struct xarray *xa, unsigned long index)
  1197. {
  1198. void *entry;
  1199. xa_lock(xa);
  1200. entry = __xa_erase(xa, index);
  1201. xa_unlock(xa);
  1202. return entry;
  1203. }
  1204. EXPORT_SYMBOL(xa_erase);
  1205. /**
  1206. * __xa_store() - Store this entry in the XArray.
  1207. * @xa: XArray.
  1208. * @index: Index into array.
  1209. * @entry: New entry.
  1210. * @gfp: Memory allocation flags.
  1211. *
  1212. * You must already be holding the xa_lock when calling this function.
  1213. * It will drop the lock if needed to allocate memory, and then reacquire
  1214. * it afterwards.
  1215. *
  1216. * Context: Any context. Expects xa_lock to be held on entry. May
  1217. * release and reacquire xa_lock if @gfp flags permit.
  1218. * Return: The old entry at this index or xa_err() if an error happened.
  1219. */
  1220. void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
  1221. {
  1222. XA_STATE(xas, xa, index);
  1223. void *curr;
  1224. if (WARN_ON_ONCE(xa_is_internal(entry)))
  1225. return XA_ERROR(-EINVAL);
  1226. if (xa_track_free(xa) && !entry)
  1227. entry = XA_ZERO_ENTRY;
  1228. do {
  1229. curr = xas_store(&xas, entry);
  1230. if (xa_track_free(xa))
  1231. xas_clear_mark(&xas, XA_FREE_MARK);
  1232. } while (__xas_nomem(&xas, gfp));
  1233. return xas_result(&xas, curr);
  1234. }
  1235. EXPORT_SYMBOL(__xa_store);
  1236. /**
  1237. * xa_store() - Store this entry in the XArray.
  1238. * @xa: XArray.
  1239. * @index: Index into array.
  1240. * @entry: New entry.
  1241. * @gfp: Memory allocation flags.
  1242. *
  1243. * After this function returns, loads from this index will return @entry.
  1244. * Storing into an existing multislot entry updates the entry of every index.
  1245. * The marks associated with @index are unaffected unless @entry is %NULL.
  1246. *
  1247. * Context: Any context. Takes and releases the xa_lock.
  1248. * May sleep if the @gfp flags permit.
  1249. * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
  1250. * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
  1251. * failed.
  1252. */
  1253. void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
  1254. {
  1255. void *curr;
  1256. xa_lock(xa);
  1257. curr = __xa_store(xa, index, entry, gfp);
  1258. xa_unlock(xa);
  1259. return curr;
  1260. }
  1261. EXPORT_SYMBOL(xa_store);
  1262. /**
  1263. * __xa_cmpxchg() - Store this entry in the XArray.
  1264. * @xa: XArray.
  1265. * @index: Index into array.
  1266. * @old: Old value to test against.
  1267. * @entry: New entry.
  1268. * @gfp: Memory allocation flags.
  1269. *
  1270. * You must already be holding the xa_lock when calling this function.
  1271. * It will drop the lock if needed to allocate memory, and then reacquire
  1272. * it afterwards.
  1273. *
  1274. * Context: Any context. Expects xa_lock to be held on entry. May
  1275. * release and reacquire xa_lock if @gfp flags permit.
  1276. * Return: The old entry at this index or xa_err() if an error happened.
  1277. */
  1278. void *__xa_cmpxchg(struct xarray *xa, unsigned long index,
  1279. void *old, void *entry, gfp_t gfp)
  1280. {
  1281. XA_STATE(xas, xa, index);
  1282. void *curr;
  1283. if (WARN_ON_ONCE(xa_is_internal(entry)))
  1284. return XA_ERROR(-EINVAL);
  1285. if (xa_track_free(xa) && !entry)
  1286. entry = XA_ZERO_ENTRY;
  1287. do {
  1288. curr = xas_load(&xas);
  1289. if (curr == XA_ZERO_ENTRY)
  1290. curr = NULL;
  1291. if (curr == old) {
  1292. xas_store(&xas, entry);
  1293. if (xa_track_free(xa))
  1294. xas_clear_mark(&xas, XA_FREE_MARK);
  1295. }
  1296. } while (__xas_nomem(&xas, gfp));
  1297. return xas_result(&xas, curr);
  1298. }
  1299. EXPORT_SYMBOL(__xa_cmpxchg);
  1300. /**
  1301. * __xa_reserve() - Reserve this index in the XArray.
  1302. * @xa: XArray.
  1303. * @index: Index into array.
  1304. * @gfp: Memory allocation flags.
  1305. *
  1306. * Ensures there is somewhere to store an entry at @index in the array.
  1307. * If there is already something stored at @index, this function does
  1308. * nothing. If there was nothing there, the entry is marked as reserved.
  1309. * Loading from a reserved entry returns a %NULL pointer.
  1310. *
  1311. * If you do not use the entry that you have reserved, call xa_release()
  1312. * or xa_erase() to free any unnecessary memory.
  1313. *
  1314. * Context: Any context. Expects the xa_lock to be held on entry. May
  1315. * release the lock, sleep and reacquire the lock if the @gfp flags permit.
  1316. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  1317. */
  1318. int __xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
  1319. {
  1320. XA_STATE(xas, xa, index);
  1321. void *curr;
  1322. do {
  1323. curr = xas_load(&xas);
  1324. if (!curr) {
  1325. xas_store(&xas, XA_ZERO_ENTRY);
  1326. if (xa_track_free(xa))
  1327. xas_clear_mark(&xas, XA_FREE_MARK);
  1328. }
  1329. } while (__xas_nomem(&xas, gfp));
  1330. return xas_error(&xas);
  1331. }
  1332. EXPORT_SYMBOL(__xa_reserve);
  1333. #ifdef CONFIG_XARRAY_MULTI
  1334. static void xas_set_range(struct xa_state *xas, unsigned long first,
  1335. unsigned long last)
  1336. {
  1337. unsigned int shift = 0;
  1338. unsigned long sibs = last - first;
  1339. unsigned int offset = XA_CHUNK_MASK;
  1340. xas_set(xas, first);
  1341. while ((first & XA_CHUNK_MASK) == 0) {
  1342. if (sibs < XA_CHUNK_MASK)
  1343. break;
  1344. if ((sibs == XA_CHUNK_MASK) && (offset < XA_CHUNK_MASK))
  1345. break;
  1346. shift += XA_CHUNK_SHIFT;
  1347. if (offset == XA_CHUNK_MASK)
  1348. offset = sibs & XA_CHUNK_MASK;
  1349. sibs >>= XA_CHUNK_SHIFT;
  1350. first >>= XA_CHUNK_SHIFT;
  1351. }
  1352. offset = first & XA_CHUNK_MASK;
  1353. if (offset + sibs > XA_CHUNK_MASK)
  1354. sibs = XA_CHUNK_MASK - offset;
  1355. if ((((first + sibs + 1) << shift) - 1) > last)
  1356. sibs -= 1;
  1357. xas->xa_shift = shift;
  1358. xas->xa_sibs = sibs;
  1359. }
  1360. /**
  1361. * xa_store_range() - Store this entry at a range of indices in the XArray.
  1362. * @xa: XArray.
  1363. * @first: First index to affect.
  1364. * @last: Last index to affect.
  1365. * @entry: New entry.
  1366. * @gfp: Memory allocation flags.
  1367. *
  1368. * After this function returns, loads from any index between @first and @last,
  1369. * inclusive will return @entry.
  1370. * Storing into an existing multislot entry updates the entry of every index.
  1371. * The marks associated with @index are unaffected unless @entry is %NULL.
  1372. *
  1373. * Context: Process context. Takes and releases the xa_lock. May sleep
  1374. * if the @gfp flags permit.
  1375. * Return: %NULL on success, xa_err(-EINVAL) if @entry cannot be stored in
  1376. * an XArray, or xa_err(-ENOMEM) if memory allocation failed.
  1377. */
  1378. void *xa_store_range(struct xarray *xa, unsigned long first,
  1379. unsigned long last, void *entry, gfp_t gfp)
  1380. {
  1381. XA_STATE(xas, xa, 0);
  1382. if (WARN_ON_ONCE(xa_is_internal(entry)))
  1383. return XA_ERROR(-EINVAL);
  1384. if (last < first)
  1385. return XA_ERROR(-EINVAL);
  1386. do {
  1387. xas_lock(&xas);
  1388. if (entry) {
  1389. unsigned int order = BITS_PER_LONG;
  1390. if (last + 1)
  1391. order = __ffs(last + 1);
  1392. xas_set_order(&xas, last, order);
  1393. xas_create(&xas);
  1394. if (xas_error(&xas))
  1395. goto unlock;
  1396. }
  1397. do {
  1398. xas_set_range(&xas, first, last);
  1399. xas_store(&xas, entry);
  1400. if (xas_error(&xas))
  1401. goto unlock;
  1402. first += xas_size(&xas);
  1403. } while (first <= last);
  1404. unlock:
  1405. xas_unlock(&xas);
  1406. } while (xas_nomem(&xas, gfp));
  1407. return xas_result(&xas, NULL);
  1408. }
  1409. EXPORT_SYMBOL(xa_store_range);
  1410. #endif /* CONFIG_XARRAY_MULTI */
  1411. /**
  1412. * __xa_alloc() - Find somewhere to store this entry in the XArray.
  1413. * @xa: XArray.
  1414. * @id: Pointer to ID.
  1415. * @max: Maximum ID to allocate (inclusive).
  1416. * @entry: New entry.
  1417. * @gfp: Memory allocation flags.
  1418. *
  1419. * Allocates an unused ID in the range specified by @id and @max.
  1420. * Updates the @id pointer with the index, then stores the entry at that
  1421. * index. A concurrent lookup will not see an uninitialised @id.
  1422. *
  1423. * Context: Any context. Expects xa_lock to be held on entry. May
  1424. * release and reacquire xa_lock if @gfp flags permit.
  1425. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  1426. * there is no more space in the XArray.
  1427. */
  1428. int __xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry, gfp_t gfp)
  1429. {
  1430. XA_STATE(xas, xa, 0);
  1431. int err;
  1432. if (WARN_ON_ONCE(xa_is_internal(entry)))
  1433. return -EINVAL;
  1434. if (WARN_ON_ONCE(!xa_track_free(xa)))
  1435. return -EINVAL;
  1436. if (!entry)
  1437. entry = XA_ZERO_ENTRY;
  1438. do {
  1439. xas.xa_index = *id;
  1440. xas_find_marked(&xas, max, XA_FREE_MARK);
  1441. if (xas.xa_node == XAS_RESTART)
  1442. xas_set_err(&xas, -ENOSPC);
  1443. xas_store(&xas, entry);
  1444. xas_clear_mark(&xas, XA_FREE_MARK);
  1445. } while (__xas_nomem(&xas, gfp));
  1446. err = xas_error(&xas);
  1447. if (!err)
  1448. *id = xas.xa_index;
  1449. return err;
  1450. }
  1451. EXPORT_SYMBOL(__xa_alloc);
  1452. /**
  1453. * __xa_set_mark() - Set this mark on this entry while locked.
  1454. * @xa: XArray.
  1455. * @index: Index of entry.
  1456. * @mark: Mark number.
  1457. *
  1458. * Attempting to set a mark on a %NULL entry does not succeed.
  1459. *
  1460. * Context: Any context. Expects xa_lock to be held on entry.
  1461. */
  1462. void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1463. {
  1464. XA_STATE(xas, xa, index);
  1465. void *entry = xas_load(&xas);
  1466. if (entry)
  1467. xas_set_mark(&xas, mark);
  1468. }
  1469. EXPORT_SYMBOL(__xa_set_mark);
  1470. /**
  1471. * __xa_clear_mark() - Clear this mark on this entry while locked.
  1472. * @xa: XArray.
  1473. * @index: Index of entry.
  1474. * @mark: Mark number.
  1475. *
  1476. * Context: Any context. Expects xa_lock to be held on entry.
  1477. */
  1478. void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1479. {
  1480. XA_STATE(xas, xa, index);
  1481. void *entry = xas_load(&xas);
  1482. if (entry)
  1483. xas_clear_mark(&xas, mark);
  1484. }
  1485. EXPORT_SYMBOL(__xa_clear_mark);
  1486. /**
  1487. * xa_get_mark() - Inquire whether this mark is set on this entry.
  1488. * @xa: XArray.
  1489. * @index: Index of entry.
  1490. * @mark: Mark number.
  1491. *
  1492. * This function uses the RCU read lock, so the result may be out of date
  1493. * by the time it returns. If you need the result to be stable, use a lock.
  1494. *
  1495. * Context: Any context. Takes and releases the RCU lock.
  1496. * Return: True if the entry at @index has this mark set, false if it doesn't.
  1497. */
  1498. bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1499. {
  1500. XA_STATE(xas, xa, index);
  1501. void *entry;
  1502. rcu_read_lock();
  1503. entry = xas_start(&xas);
  1504. while (xas_get_mark(&xas, mark)) {
  1505. if (!xa_is_node(entry))
  1506. goto found;
  1507. entry = xas_descend(&xas, xa_to_node(entry));
  1508. }
  1509. rcu_read_unlock();
  1510. return false;
  1511. found:
  1512. rcu_read_unlock();
  1513. return true;
  1514. }
  1515. EXPORT_SYMBOL(xa_get_mark);
  1516. /**
  1517. * xa_set_mark() - Set this mark on this entry.
  1518. * @xa: XArray.
  1519. * @index: Index of entry.
  1520. * @mark: Mark number.
  1521. *
  1522. * Attempting to set a mark on a %NULL entry does not succeed.
  1523. *
  1524. * Context: Process context. Takes and releases the xa_lock.
  1525. */
  1526. void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1527. {
  1528. xa_lock(xa);
  1529. __xa_set_mark(xa, index, mark);
  1530. xa_unlock(xa);
  1531. }
  1532. EXPORT_SYMBOL(xa_set_mark);
  1533. /**
  1534. * xa_clear_mark() - Clear this mark on this entry.
  1535. * @xa: XArray.
  1536. * @index: Index of entry.
  1537. * @mark: Mark number.
  1538. *
  1539. * Clearing a mark always succeeds.
  1540. *
  1541. * Context: Process context. Takes and releases the xa_lock.
  1542. */
  1543. void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  1544. {
  1545. xa_lock(xa);
  1546. __xa_clear_mark(xa, index, mark);
  1547. xa_unlock(xa);
  1548. }
  1549. EXPORT_SYMBOL(xa_clear_mark);
  1550. /**
  1551. * xa_find() - Search the XArray for an entry.
  1552. * @xa: XArray.
  1553. * @indexp: Pointer to an index.
  1554. * @max: Maximum index to search to.
  1555. * @filter: Selection criterion.
  1556. *
  1557. * Finds the entry in @xa which matches the @filter, and has the lowest
  1558. * index that is at least @indexp and no more than @max.
  1559. * If an entry is found, @indexp is updated to be the index of the entry.
  1560. * This function is protected by the RCU read lock, so it may not find
  1561. * entries which are being simultaneously added. It will not return an
  1562. * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
  1563. *
  1564. * Context: Any context. Takes and releases the RCU lock.
  1565. * Return: The entry, if found, otherwise %NULL.
  1566. */
  1567. void *xa_find(struct xarray *xa, unsigned long *indexp,
  1568. unsigned long max, xa_mark_t filter)
  1569. {
  1570. XA_STATE(xas, xa, *indexp);
  1571. void *entry;
  1572. rcu_read_lock();
  1573. do {
  1574. if ((__force unsigned int)filter < XA_MAX_MARKS)
  1575. entry = xas_find_marked(&xas, max, filter);
  1576. else
  1577. entry = xas_find(&xas, max);
  1578. } while (xas_retry(&xas, entry));
  1579. rcu_read_unlock();
  1580. if (entry)
  1581. *indexp = xas.xa_index;
  1582. return entry;
  1583. }
  1584. EXPORT_SYMBOL(xa_find);
  1585. /**
  1586. * xa_find_after() - Search the XArray for a present entry.
  1587. * @xa: XArray.
  1588. * @indexp: Pointer to an index.
  1589. * @max: Maximum index to search to.
  1590. * @filter: Selection criterion.
  1591. *
  1592. * Finds the entry in @xa which matches the @filter and has the lowest
  1593. * index that is above @indexp and no more than @max.
  1594. * If an entry is found, @indexp is updated to be the index of the entry.
  1595. * This function is protected by the RCU read lock, so it may miss entries
  1596. * which are being simultaneously added. It will not return an
  1597. * %XA_RETRY_ENTRY; if you need to see retry entries, use xas_find().
  1598. *
  1599. * Context: Any context. Takes and releases the RCU lock.
  1600. * Return: The pointer, if found, otherwise %NULL.
  1601. */
  1602. void *xa_find_after(struct xarray *xa, unsigned long *indexp,
  1603. unsigned long max, xa_mark_t filter)
  1604. {
  1605. XA_STATE(xas, xa, *indexp + 1);
  1606. void *entry;
  1607. rcu_read_lock();
  1608. for (;;) {
  1609. if ((__force unsigned int)filter < XA_MAX_MARKS)
  1610. entry = xas_find_marked(&xas, max, filter);
  1611. else
  1612. entry = xas_find(&xas, max);
  1613. if (xas.xa_node == XAS_BOUNDS)
  1614. break;
  1615. if (xas.xa_shift) {
  1616. if (xas.xa_index & ((1UL << xas.xa_shift) - 1))
  1617. continue;
  1618. } else {
  1619. if (xas.xa_offset < (xas.xa_index & XA_CHUNK_MASK))
  1620. continue;
  1621. }
  1622. if (!xas_retry(&xas, entry))
  1623. break;
  1624. }
  1625. rcu_read_unlock();
  1626. if (entry)
  1627. *indexp = xas.xa_index;
  1628. return entry;
  1629. }
  1630. EXPORT_SYMBOL(xa_find_after);
  1631. static unsigned int xas_extract_present(struct xa_state *xas, void **dst,
  1632. unsigned long max, unsigned int n)
  1633. {
  1634. void *entry;
  1635. unsigned int i = 0;
  1636. rcu_read_lock();
  1637. xas_for_each(xas, entry, max) {
  1638. if (xas_retry(xas, entry))
  1639. continue;
  1640. dst[i++] = entry;
  1641. if (i == n)
  1642. break;
  1643. }
  1644. rcu_read_unlock();
  1645. return i;
  1646. }
  1647. static unsigned int xas_extract_marked(struct xa_state *xas, void **dst,
  1648. unsigned long max, unsigned int n, xa_mark_t mark)
  1649. {
  1650. void *entry;
  1651. unsigned int i = 0;
  1652. rcu_read_lock();
  1653. xas_for_each_marked(xas, entry, max, mark) {
  1654. if (xas_retry(xas, entry))
  1655. continue;
  1656. dst[i++] = entry;
  1657. if (i == n)
  1658. break;
  1659. }
  1660. rcu_read_unlock();
  1661. return i;
  1662. }
  1663. /**
  1664. * xa_extract() - Copy selected entries from the XArray into a normal array.
  1665. * @xa: The source XArray to copy from.
  1666. * @dst: The buffer to copy entries into.
  1667. * @start: The first index in the XArray eligible to be selected.
  1668. * @max: The last index in the XArray eligible to be selected.
  1669. * @n: The maximum number of entries to copy.
  1670. * @filter: Selection criterion.
  1671. *
  1672. * Copies up to @n entries that match @filter from the XArray. The
  1673. * copied entries will have indices between @start and @max, inclusive.
  1674. *
  1675. * The @filter may be an XArray mark value, in which case entries which are
  1676. * marked with that mark will be copied. It may also be %XA_PRESENT, in
  1677. * which case all entries which are not %NULL will be copied.
  1678. *
  1679. * The entries returned may not represent a snapshot of the XArray at a
  1680. * moment in time. For example, if another thread stores to index 5, then
  1681. * index 10, calling xa_extract() may return the old contents of index 5
  1682. * and the new contents of index 10. Indices not modified while this
  1683. * function is running will not be skipped.
  1684. *
  1685. * If you need stronger guarantees, holding the xa_lock across calls to this
  1686. * function will prevent concurrent modification.
  1687. *
  1688. * Context: Any context. Takes and releases the RCU lock.
  1689. * Return: The number of entries copied.
  1690. */
  1691. unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start,
  1692. unsigned long max, unsigned int n, xa_mark_t filter)
  1693. {
  1694. XA_STATE(xas, xa, start);
  1695. if (!n)
  1696. return 0;
  1697. if ((__force unsigned int)filter < XA_MAX_MARKS)
  1698. return xas_extract_marked(&xas, dst, max, n, filter);
  1699. return xas_extract_present(&xas, dst, max, n);
  1700. }
  1701. EXPORT_SYMBOL(xa_extract);
  1702. /**
  1703. * xa_destroy() - Free all internal data structures.
  1704. * @xa: XArray.
  1705. *
  1706. * After calling this function, the XArray is empty and has freed all memory
  1707. * allocated for its internal data structures. You are responsible for
  1708. * freeing the objects referenced by the XArray.
  1709. *
  1710. * Context: Any context. Takes and releases the xa_lock, interrupt-safe.
  1711. */
  1712. void xa_destroy(struct xarray *xa)
  1713. {
  1714. XA_STATE(xas, xa, 0);
  1715. unsigned long flags;
  1716. void *entry;
  1717. xas.xa_node = NULL;
  1718. xas_lock_irqsave(&xas, flags);
  1719. entry = xa_head_locked(xa);
  1720. RCU_INIT_POINTER(xa->xa_head, NULL);
  1721. xas_init_marks(&xas);
  1722. /* lockdep checks we're still holding the lock in xas_free_nodes() */
  1723. if (xa_is_node(entry))
  1724. xas_free_nodes(&xas, xa_to_node(entry));
  1725. xas_unlock_irqrestore(&xas, flags);
  1726. }
  1727. EXPORT_SYMBOL(xa_destroy);
  1728. #ifdef XA_DEBUG
  1729. void xa_dump_node(const struct xa_node *node)
  1730. {
  1731. unsigned i, j;
  1732. if (!node)
  1733. return;
  1734. if ((unsigned long)node & 3) {
  1735. pr_cont("node %px\n", node);
  1736. return;
  1737. }
  1738. pr_cont("node %px %s %d parent %px shift %d count %d values %d "
  1739. "array %px list %px %px marks",
  1740. node, node->parent ? "offset" : "max", node->offset,
  1741. node->parent, node->shift, node->count, node->nr_values,
  1742. node->array, node->private_list.prev, node->private_list.next);
  1743. for (i = 0; i < XA_MAX_MARKS; i++)
  1744. for (j = 0; j < XA_MARK_LONGS; j++)
  1745. pr_cont(" %lx", node->marks[i][j]);
  1746. pr_cont("\n");
  1747. }
  1748. void xa_dump_index(unsigned long index, unsigned int shift)
  1749. {
  1750. if (!shift)
  1751. pr_info("%lu: ", index);
  1752. else if (shift >= BITS_PER_LONG)
  1753. pr_info("0-%lu: ", ~0UL);
  1754. else
  1755. pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
  1756. }
  1757. void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
  1758. {
  1759. if (!entry)
  1760. return;
  1761. xa_dump_index(index, shift);
  1762. if (xa_is_node(entry)) {
  1763. if (shift == 0) {
  1764. pr_cont("%px\n", entry);
  1765. } else {
  1766. unsigned long i;
  1767. struct xa_node *node = xa_to_node(entry);
  1768. xa_dump_node(node);
  1769. for (i = 0; i < XA_CHUNK_SIZE; i++)
  1770. xa_dump_entry(node->slots[i],
  1771. index + (i << node->shift), node->shift);
  1772. }
  1773. } else if (xa_is_value(entry))
  1774. pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
  1775. xa_to_value(entry), entry);
  1776. else if (!xa_is_internal(entry))
  1777. pr_cont("%px\n", entry);
  1778. else if (xa_is_retry(entry))
  1779. pr_cont("retry (%ld)\n", xa_to_internal(entry));
  1780. else if (xa_is_sibling(entry))
  1781. pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
  1782. else if (xa_is_zero(entry))
  1783. pr_cont("zero (%ld)\n", xa_to_internal(entry));
  1784. else
  1785. pr_cont("UNKNOWN ENTRY (%px)\n", entry);
  1786. }
  1787. void xa_dump(const struct xarray *xa)
  1788. {
  1789. void *entry = xa->xa_head;
  1790. unsigned int shift = 0;
  1791. pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
  1792. xa->xa_flags, xa_marked(xa, XA_MARK_0),
  1793. xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
  1794. if (xa_is_node(entry))
  1795. shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
  1796. xa_dump_entry(entry, 0, shift);
  1797. }
  1798. #endif