xarray.c 59 KB

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