xarray.c 64 KB

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