xarray.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * XArray implementation
  4. * Copyright (c) 2017 Microsoft Corporation
  5. * Author: Matthew Wilcox <willy@infradead.org>
  6. */
  7. #include <linux/bitmap.h>
  8. #include <linux/export.h>
  9. #include <linux/list.h>
  10. #include <linux/slab.h>
  11. #include <linux/xarray.h>
  12. /*
  13. * Coding conventions in this file:
  14. *
  15. * @xa is used to refer to the entire xarray.
  16. * @xas is the 'xarray operation state'. It may be either a pointer to
  17. * an xa_state, or an xa_state stored on the stack. This is an unfortunate
  18. * ambiguity.
  19. * @index is the index of the entry being operated on
  20. * @mark is an xa_mark_t; a small number indicating one of the mark bits.
  21. * @node refers to an xa_node; usually the primary one being operated on by
  22. * this function.
  23. * @offset is the index into the slots array inside an xa_node.
  24. * @parent refers to the @xa_node closer to the head than @node.
  25. * @entry refers to something stored in a slot in the xarray
  26. */
  27. static inline unsigned int xa_lock_type(const struct xarray *xa)
  28. {
  29. return (__force unsigned int)xa->xa_flags & 3;
  30. }
  31. static inline void xas_lock_type(struct xa_state *xas, unsigned int lock_type)
  32. {
  33. if (lock_type == XA_LOCK_IRQ)
  34. xas_lock_irq(xas);
  35. else if (lock_type == XA_LOCK_BH)
  36. xas_lock_bh(xas);
  37. else
  38. xas_lock(xas);
  39. }
  40. static inline void xas_unlock_type(struct xa_state *xas, unsigned int lock_type)
  41. {
  42. if (lock_type == XA_LOCK_IRQ)
  43. xas_unlock_irq(xas);
  44. else if (lock_type == XA_LOCK_BH)
  45. xas_unlock_bh(xas);
  46. else
  47. xas_unlock(xas);
  48. }
  49. static inline void xa_mark_set(struct xarray *xa, xa_mark_t mark)
  50. {
  51. if (!(xa->xa_flags & XA_FLAGS_MARK(mark)))
  52. xa->xa_flags |= XA_FLAGS_MARK(mark);
  53. }
  54. static inline void xa_mark_clear(struct xarray *xa, xa_mark_t mark)
  55. {
  56. if (xa->xa_flags & XA_FLAGS_MARK(mark))
  57. xa->xa_flags &= ~(XA_FLAGS_MARK(mark));
  58. }
  59. static inline unsigned long *node_marks(struct xa_node *node, xa_mark_t mark)
  60. {
  61. return node->marks[(__force unsigned)mark];
  62. }
  63. static inline bool node_get_mark(struct xa_node *node,
  64. unsigned int offset, xa_mark_t mark)
  65. {
  66. return test_bit(offset, node_marks(node, mark));
  67. }
  68. /* returns true if the bit was set */
  69. static inline bool node_set_mark(struct xa_node *node, unsigned int offset,
  70. xa_mark_t mark)
  71. {
  72. return __test_and_set_bit(offset, node_marks(node, mark));
  73. }
  74. /* returns true if the bit was set */
  75. static inline bool node_clear_mark(struct xa_node *node, unsigned int offset,
  76. xa_mark_t mark)
  77. {
  78. return __test_and_clear_bit(offset, node_marks(node, mark));
  79. }
  80. static inline bool node_any_mark(struct xa_node *node, xa_mark_t mark)
  81. {
  82. return !bitmap_empty(node_marks(node, mark), XA_CHUNK_SIZE);
  83. }
  84. #define mark_inc(mark) do { \
  85. mark = (__force xa_mark_t)((__force unsigned)(mark) + 1); \
  86. } while (0)
  87. /*
  88. * xas_squash_marks() - Merge all marks to the first entry
  89. * @xas: Array operation state.
  90. *
  91. * Set a mark on the first entry if any entry has it set. Clear marks on
  92. * all sibling entries.
  93. */
  94. static void xas_squash_marks(const struct xa_state *xas)
  95. {
  96. unsigned int mark = 0;
  97. unsigned int limit = xas->xa_offset + xas->xa_sibs + 1;
  98. if (!xas->xa_sibs)
  99. return;
  100. do {
  101. unsigned long *marks = xas->xa_node->marks[mark];
  102. if (find_next_bit(marks, limit, xas->xa_offset + 1) == limit)
  103. continue;
  104. __set_bit(xas->xa_offset, marks);
  105. bitmap_clear(marks, xas->xa_offset + 1, xas->xa_sibs);
  106. } while (mark++ != (__force unsigned)XA_MARK_MAX);
  107. }
  108. /* extracts the offset within this node from the index */
  109. static unsigned int get_offset(unsigned long index, struct xa_node *node)
  110. {
  111. return (index >> node->shift) & XA_CHUNK_MASK;
  112. }
  113. /* move the index either forwards (find) or backwards (sibling slot) */
  114. static void xas_move_index(struct xa_state *xas, unsigned long offset)
  115. {
  116. unsigned int shift = xas->xa_node->shift;
  117. xas->xa_index &= ~XA_CHUNK_MASK << shift;
  118. xas->xa_index += offset << shift;
  119. }
  120. static void *set_bounds(struct xa_state *xas)
  121. {
  122. xas->xa_node = XAS_BOUNDS;
  123. return NULL;
  124. }
  125. /*
  126. * Starts a walk. If the @xas is already valid, we assume that it's on
  127. * the right path and just return where we've got to. If we're in an
  128. * error state, return NULL. If the index is outside the current scope
  129. * of the xarray, return NULL without changing @xas->xa_node. Otherwise
  130. * set @xas->xa_node to NULL and return the current head of the array.
  131. */
  132. static void *xas_start(struct xa_state *xas)
  133. {
  134. void *entry;
  135. if (xas_valid(xas))
  136. return xas_reload(xas);
  137. if (xas_error(xas))
  138. return NULL;
  139. entry = xa_head(xas->xa);
  140. if (!xa_is_node(entry)) {
  141. if (xas->xa_index)
  142. return set_bounds(xas);
  143. } else {
  144. if ((xas->xa_index >> xa_to_node(entry)->shift) > XA_CHUNK_MASK)
  145. return set_bounds(xas);
  146. }
  147. xas->xa_node = NULL;
  148. return entry;
  149. }
  150. static void *xas_descend(struct xa_state *xas, struct xa_node *node)
  151. {
  152. unsigned int offset = get_offset(xas->xa_index, node);
  153. void *entry = xa_entry(xas->xa, node, offset);
  154. xas->xa_node = node;
  155. if (xa_is_sibling(entry)) {
  156. offset = xa_to_sibling(entry);
  157. entry = xa_entry(xas->xa, node, offset);
  158. }
  159. xas->xa_offset = offset;
  160. return entry;
  161. }
  162. /**
  163. * xas_load() - Load an entry from the XArray (advanced).
  164. * @xas: XArray operation state.
  165. *
  166. * Usually walks the @xas to the appropriate state to load the entry
  167. * stored at xa_index. However, it will do nothing and return %NULL if
  168. * @xas is in an error state. xas_load() will never expand the tree.
  169. *
  170. * If the xa_state is set up to operate on a multi-index entry, xas_load()
  171. * may return %NULL or an internal entry, even if there are entries
  172. * present within the range specified by @xas.
  173. *
  174. * Context: Any context. The caller should hold the xa_lock or the RCU lock.
  175. * Return: Usually an entry in the XArray, but see description for exceptions.
  176. */
  177. void *xas_load(struct xa_state *xas)
  178. {
  179. void *entry = xas_start(xas);
  180. while (xa_is_node(entry)) {
  181. struct xa_node *node = xa_to_node(entry);
  182. if (xas->xa_shift > node->shift)
  183. break;
  184. entry = xas_descend(xas, node);
  185. }
  186. return entry;
  187. }
  188. EXPORT_SYMBOL_GPL(xas_load);
  189. /* Move the radix tree node cache here */
  190. extern struct kmem_cache *radix_tree_node_cachep;
  191. extern void radix_tree_node_rcu_free(struct rcu_head *head);
  192. #define XA_RCU_FREE ((struct xarray *)1)
  193. static void xa_node_free(struct xa_node *node)
  194. {
  195. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  196. node->array = XA_RCU_FREE;
  197. call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
  198. }
  199. /*
  200. * xas_destroy() - Free any resources allocated during the XArray operation.
  201. * @xas: XArray operation state.
  202. *
  203. * This function is now internal-only.
  204. */
  205. static void xas_destroy(struct xa_state *xas)
  206. {
  207. struct xa_node *node = xas->xa_alloc;
  208. if (!node)
  209. return;
  210. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  211. kmem_cache_free(radix_tree_node_cachep, node);
  212. xas->xa_alloc = NULL;
  213. }
  214. /**
  215. * xas_nomem() - Allocate memory if needed.
  216. * @xas: XArray operation state.
  217. * @gfp: Memory allocation flags.
  218. *
  219. * If we need to add new nodes to the XArray, we try to allocate memory
  220. * with GFP_NOWAIT while holding the lock, which will usually succeed.
  221. * If it fails, @xas is flagged as needing memory to continue. The caller
  222. * should drop the lock and call xas_nomem(). If xas_nomem() succeeds,
  223. * the caller should retry the operation.
  224. *
  225. * Forward progress is guaranteed as one node is allocated here and
  226. * stored in the xa_state where it will be found by xas_alloc(). More
  227. * nodes will likely be found in the slab allocator, but we do not tie
  228. * them up here.
  229. *
  230. * Return: true if memory was needed, and was successfully allocated.
  231. */
  232. bool xas_nomem(struct xa_state *xas, gfp_t gfp)
  233. {
  234. if (xas->xa_node != XA_ERROR(-ENOMEM)) {
  235. xas_destroy(xas);
  236. return false;
  237. }
  238. xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  239. if (!xas->xa_alloc)
  240. return false;
  241. XA_NODE_BUG_ON(xas->xa_alloc, !list_empty(&xas->xa_alloc->private_list));
  242. xas->xa_node = XAS_RESTART;
  243. return true;
  244. }
  245. EXPORT_SYMBOL_GPL(xas_nomem);
  246. /*
  247. * __xas_nomem() - Drop locks and allocate memory if needed.
  248. * @xas: XArray operation state.
  249. * @gfp: Memory allocation flags.
  250. *
  251. * Internal variant of xas_nomem().
  252. *
  253. * Return: true if memory was needed, and was successfully allocated.
  254. */
  255. static bool __xas_nomem(struct xa_state *xas, gfp_t gfp)
  256. __must_hold(xas->xa->xa_lock)
  257. {
  258. unsigned int lock_type = xa_lock_type(xas->xa);
  259. if (xas->xa_node != XA_ERROR(-ENOMEM)) {
  260. xas_destroy(xas);
  261. return false;
  262. }
  263. if (gfpflags_allow_blocking(gfp)) {
  264. xas_unlock_type(xas, lock_type);
  265. xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  266. xas_lock_type(xas, lock_type);
  267. } else {
  268. xas->xa_alloc = kmem_cache_alloc(radix_tree_node_cachep, gfp);
  269. }
  270. if (!xas->xa_alloc)
  271. return false;
  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. static void xas_update(struct xa_state *xas, struct xa_node *node)
  277. {
  278. if (xas->xa_update)
  279. xas->xa_update(node);
  280. else
  281. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  282. }
  283. static void *xas_alloc(struct xa_state *xas, unsigned int shift)
  284. {
  285. struct xa_node *parent = xas->xa_node;
  286. struct xa_node *node = xas->xa_alloc;
  287. if (xas_invalid(xas))
  288. return NULL;
  289. if (node) {
  290. xas->xa_alloc = NULL;
  291. } else {
  292. node = kmem_cache_alloc(radix_tree_node_cachep,
  293. GFP_NOWAIT | __GFP_NOWARN);
  294. if (!node) {
  295. xas_set_err(xas, -ENOMEM);
  296. return NULL;
  297. }
  298. }
  299. if (parent) {
  300. node->offset = xas->xa_offset;
  301. parent->count++;
  302. XA_NODE_BUG_ON(node, parent->count > XA_CHUNK_SIZE);
  303. xas_update(xas, parent);
  304. }
  305. XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
  306. XA_NODE_BUG_ON(node, !list_empty(&node->private_list));
  307. node->shift = shift;
  308. node->count = 0;
  309. node->nr_values = 0;
  310. RCU_INIT_POINTER(node->parent, xas->xa_node);
  311. node->array = xas->xa;
  312. return node;
  313. }
  314. /*
  315. * Use this to calculate the maximum index that will need to be created
  316. * in order to add the entry described by @xas. Because we cannot store a
  317. * multiple-index entry at index 0, the calculation is a little more complex
  318. * than you might expect.
  319. */
  320. static unsigned long xas_max(struct xa_state *xas)
  321. {
  322. unsigned long max = xas->xa_index;
  323. #ifdef CONFIG_XARRAY_MULTI
  324. if (xas->xa_shift || xas->xa_sibs) {
  325. unsigned long mask;
  326. mask = (((xas->xa_sibs + 1UL) << xas->xa_shift) - 1);
  327. max |= mask;
  328. if (mask == max)
  329. max++;
  330. }
  331. #endif
  332. return max;
  333. }
  334. /* The maximum index that can be contained in the array without expanding it */
  335. static unsigned long max_index(void *entry)
  336. {
  337. if (!xa_is_node(entry))
  338. return 0;
  339. return (XA_CHUNK_SIZE << xa_to_node(entry)->shift) - 1;
  340. }
  341. static void xas_shrink(struct xa_state *xas)
  342. {
  343. struct xarray *xa = xas->xa;
  344. struct xa_node *node = xas->xa_node;
  345. for (;;) {
  346. void *entry;
  347. XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
  348. if (node->count != 1)
  349. break;
  350. entry = xa_entry_locked(xa, node, 0);
  351. if (!entry)
  352. break;
  353. if (!xa_is_node(entry) && node->shift)
  354. break;
  355. xas->xa_node = XAS_BOUNDS;
  356. RCU_INIT_POINTER(xa->xa_head, entry);
  357. node->count = 0;
  358. node->nr_values = 0;
  359. if (!xa_is_node(entry))
  360. RCU_INIT_POINTER(node->slots[0], XA_RETRY_ENTRY);
  361. xas_update(xas, node);
  362. xa_node_free(node);
  363. if (!xa_is_node(entry))
  364. break;
  365. node = xa_to_node(entry);
  366. node->parent = NULL;
  367. }
  368. }
  369. /*
  370. * xas_delete_node() - Attempt to delete an xa_node
  371. * @xas: Array operation state.
  372. *
  373. * Attempts to delete the @xas->xa_node. This will fail if xa->node has
  374. * a non-zero reference count.
  375. */
  376. static void xas_delete_node(struct xa_state *xas)
  377. {
  378. struct xa_node *node = xas->xa_node;
  379. for (;;) {
  380. struct xa_node *parent;
  381. XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
  382. if (node->count)
  383. break;
  384. parent = xa_parent_locked(xas->xa, node);
  385. xas->xa_node = parent;
  386. xas->xa_offset = node->offset;
  387. xa_node_free(node);
  388. if (!parent) {
  389. xas->xa->xa_head = NULL;
  390. xas->xa_node = XAS_BOUNDS;
  391. return;
  392. }
  393. parent->slots[xas->xa_offset] = NULL;
  394. parent->count--;
  395. XA_NODE_BUG_ON(parent, parent->count > XA_CHUNK_SIZE);
  396. node = parent;
  397. xas_update(xas, node);
  398. }
  399. if (!node->parent)
  400. xas_shrink(xas);
  401. }
  402. /**
  403. * xas_free_nodes() - Free this node and all nodes that it references
  404. * @xas: Array operation state.
  405. * @top: Node to free
  406. *
  407. * This node has been removed from the tree. We must now free it and all
  408. * of its subnodes. There may be RCU walkers with references into the tree,
  409. * so we must replace all entries with retry markers.
  410. */
  411. static void xas_free_nodes(struct xa_state *xas, struct xa_node *top)
  412. {
  413. unsigned int offset = 0;
  414. struct xa_node *node = top;
  415. for (;;) {
  416. void *entry = xa_entry_locked(xas->xa, node, offset);
  417. if (xa_is_node(entry)) {
  418. node = xa_to_node(entry);
  419. offset = 0;
  420. continue;
  421. }
  422. if (entry)
  423. RCU_INIT_POINTER(node->slots[offset], XA_RETRY_ENTRY);
  424. offset++;
  425. while (offset == XA_CHUNK_SIZE) {
  426. struct xa_node *parent;
  427. parent = xa_parent_locked(xas->xa, node);
  428. offset = node->offset + 1;
  429. node->count = 0;
  430. node->nr_values = 0;
  431. xas_update(xas, node);
  432. xa_node_free(node);
  433. if (node == top)
  434. return;
  435. node = parent;
  436. }
  437. }
  438. }
  439. /*
  440. * xas_expand adds nodes to the head of the tree until it has reached
  441. * sufficient height to be able to contain @xas->xa_index
  442. */
  443. static int xas_expand(struct xa_state *xas, void *head)
  444. {
  445. struct xarray *xa = xas->xa;
  446. struct xa_node *node = NULL;
  447. unsigned int shift = 0;
  448. unsigned long max = xas_max(xas);
  449. if (!head) {
  450. if (max == 0)
  451. return 0;
  452. while ((max >> shift) >= XA_CHUNK_SIZE)
  453. shift += XA_CHUNK_SHIFT;
  454. return shift + XA_CHUNK_SHIFT;
  455. } else if (xa_is_node(head)) {
  456. node = xa_to_node(head);
  457. shift = node->shift + XA_CHUNK_SHIFT;
  458. }
  459. xas->xa_node = NULL;
  460. while (max > max_index(head)) {
  461. xa_mark_t mark = 0;
  462. XA_NODE_BUG_ON(node, shift > BITS_PER_LONG);
  463. node = xas_alloc(xas, shift);
  464. if (!node)
  465. return -ENOMEM;
  466. node->count = 1;
  467. if (xa_is_value(head))
  468. node->nr_values = 1;
  469. RCU_INIT_POINTER(node->slots[0], head);
  470. /* Propagate the aggregated mark info to the new child */
  471. for (;;) {
  472. if (xa_marked(xa, mark))
  473. node_set_mark(node, 0, mark);
  474. if (mark == XA_MARK_MAX)
  475. break;
  476. mark_inc(mark);
  477. }
  478. /*
  479. * Now that the new node is fully initialised, we can add
  480. * it to the tree
  481. */
  482. if (xa_is_node(head)) {
  483. xa_to_node(head)->offset = 0;
  484. rcu_assign_pointer(xa_to_node(head)->parent, node);
  485. }
  486. head = xa_mk_node(node);
  487. rcu_assign_pointer(xa->xa_head, head);
  488. xas_update(xas, node);
  489. shift += XA_CHUNK_SHIFT;
  490. }
  491. xas->xa_node = node;
  492. return shift;
  493. }
  494. /*
  495. * xas_create() - Create a slot to store an entry in.
  496. * @xas: XArray operation state.
  497. *
  498. * Most users will not need to call this function directly, as it is called
  499. * by xas_store(). It is useful for doing conditional store operations
  500. * (see the xa_cmpxchg() implementation for an example).
  501. *
  502. * Return: If the slot already existed, returns the contents of this slot.
  503. * If the slot was newly created, returns NULL. If it failed to create the
  504. * slot, returns NULL and indicates the error in @xas.
  505. */
  506. static void *xas_create(struct xa_state *xas)
  507. {
  508. struct xarray *xa = xas->xa;
  509. void *entry;
  510. void __rcu **slot;
  511. struct xa_node *node = xas->xa_node;
  512. int shift;
  513. unsigned int order = xas->xa_shift;
  514. if (xas_top(node)) {
  515. entry = xa_head_locked(xa);
  516. xas->xa_node = NULL;
  517. shift = xas_expand(xas, entry);
  518. if (shift < 0)
  519. return NULL;
  520. entry = xa_head_locked(xa);
  521. slot = &xa->xa_head;
  522. } else if (xas_error(xas)) {
  523. return NULL;
  524. } else if (node) {
  525. unsigned int offset = xas->xa_offset;
  526. shift = node->shift;
  527. entry = xa_entry_locked(xa, node, offset);
  528. slot = &node->slots[offset];
  529. } else {
  530. shift = 0;
  531. entry = xa_head_locked(xa);
  532. slot = &xa->xa_head;
  533. }
  534. while (shift > order) {
  535. shift -= XA_CHUNK_SHIFT;
  536. if (!entry) {
  537. node = xas_alloc(xas, shift);
  538. if (!node)
  539. break;
  540. rcu_assign_pointer(*slot, xa_mk_node(node));
  541. } else if (xa_is_node(entry)) {
  542. node = xa_to_node(entry);
  543. } else {
  544. break;
  545. }
  546. entry = xas_descend(xas, node);
  547. slot = &node->slots[xas->xa_offset];
  548. }
  549. return entry;
  550. }
  551. static void update_node(struct xa_state *xas, struct xa_node *node,
  552. int count, int values)
  553. {
  554. if (!node || (!count && !values))
  555. return;
  556. node->count += count;
  557. node->nr_values += values;
  558. XA_NODE_BUG_ON(node, node->count > XA_CHUNK_SIZE);
  559. XA_NODE_BUG_ON(node, node->nr_values > XA_CHUNK_SIZE);
  560. xas_update(xas, node);
  561. if (count < 0)
  562. xas_delete_node(xas);
  563. }
  564. /**
  565. * xas_store() - Store this entry in the XArray.
  566. * @xas: XArray operation state.
  567. * @entry: New entry.
  568. *
  569. * If @xas is operating on a multi-index entry, the entry returned by this
  570. * function is essentially meaningless (it may be an internal entry or it
  571. * may be %NULL, even if there are non-NULL entries at some of the indices
  572. * covered by the range). This is not a problem for any current users,
  573. * and can be changed if needed.
  574. *
  575. * Return: The old entry at this index.
  576. */
  577. void *xas_store(struct xa_state *xas, void *entry)
  578. {
  579. struct xa_node *node;
  580. void __rcu **slot = &xas->xa->xa_head;
  581. unsigned int offset, max;
  582. int count = 0;
  583. int values = 0;
  584. void *first, *next;
  585. bool value = xa_is_value(entry);
  586. if (entry)
  587. first = xas_create(xas);
  588. else
  589. first = xas_load(xas);
  590. if (xas_invalid(xas))
  591. return first;
  592. node = xas->xa_node;
  593. if (node && (xas->xa_shift < node->shift))
  594. xas->xa_sibs = 0;
  595. if ((first == entry) && !xas->xa_sibs)
  596. return first;
  597. next = first;
  598. offset = xas->xa_offset;
  599. max = xas->xa_offset + xas->xa_sibs;
  600. if (node) {
  601. slot = &node->slots[offset];
  602. if (xas->xa_sibs)
  603. xas_squash_marks(xas);
  604. }
  605. if (!entry)
  606. xas_init_marks(xas);
  607. for (;;) {
  608. /*
  609. * Must clear the marks before setting the entry to NULL,
  610. * otherwise xas_for_each_marked may find a NULL entry and
  611. * stop early. rcu_assign_pointer contains a release barrier
  612. * so the mark clearing will appear to happen before the
  613. * entry is set to NULL.
  614. */
  615. rcu_assign_pointer(*slot, entry);
  616. if (xa_is_node(next))
  617. xas_free_nodes(xas, xa_to_node(next));
  618. if (!node)
  619. break;
  620. count += !next - !entry;
  621. values += !xa_is_value(first) - !value;
  622. if (entry) {
  623. if (offset == max)
  624. break;
  625. if (!xa_is_sibling(entry))
  626. entry = xa_mk_sibling(xas->xa_offset);
  627. } else {
  628. if (offset == XA_CHUNK_MASK)
  629. break;
  630. }
  631. next = xa_entry_locked(xas->xa, node, ++offset);
  632. if (!xa_is_sibling(next)) {
  633. if (!entry && (offset > max))
  634. break;
  635. first = next;
  636. }
  637. slot++;
  638. }
  639. update_node(xas, node, count, values);
  640. return first;
  641. }
  642. EXPORT_SYMBOL_GPL(xas_store);
  643. /**
  644. * xas_get_mark() - Returns the state of this mark.
  645. * @xas: XArray operation state.
  646. * @mark: Mark number.
  647. *
  648. * Return: true if the mark is set, false if the mark is clear or @xas
  649. * is in an error state.
  650. */
  651. bool xas_get_mark(const struct xa_state *xas, xa_mark_t mark)
  652. {
  653. if (xas_invalid(xas))
  654. return false;
  655. if (!xas->xa_node)
  656. return xa_marked(xas->xa, mark);
  657. return node_get_mark(xas->xa_node, xas->xa_offset, mark);
  658. }
  659. EXPORT_SYMBOL_GPL(xas_get_mark);
  660. /**
  661. * xas_set_mark() - Sets the mark on this entry and its parents.
  662. * @xas: XArray operation state.
  663. * @mark: Mark number.
  664. *
  665. * Sets the specified mark on this entry, and walks up the tree setting it
  666. * on all the ancestor entries. Does nothing if @xas has not been walked to
  667. * an entry, or is in an error state.
  668. */
  669. void xas_set_mark(const struct xa_state *xas, xa_mark_t mark)
  670. {
  671. struct xa_node *node = xas->xa_node;
  672. unsigned int offset = xas->xa_offset;
  673. if (xas_invalid(xas))
  674. return;
  675. while (node) {
  676. if (node_set_mark(node, offset, mark))
  677. return;
  678. offset = node->offset;
  679. node = xa_parent_locked(xas->xa, node);
  680. }
  681. if (!xa_marked(xas->xa, mark))
  682. xa_mark_set(xas->xa, mark);
  683. }
  684. EXPORT_SYMBOL_GPL(xas_set_mark);
  685. /**
  686. * xas_clear_mark() - Clears the mark on this entry and its parents.
  687. * @xas: XArray operation state.
  688. * @mark: Mark number.
  689. *
  690. * Clears the specified mark on this entry, and walks back to the head
  691. * attempting to clear it on all the ancestor entries. Does nothing if
  692. * @xas has not been walked to an entry, or is in an error state.
  693. */
  694. void xas_clear_mark(const struct xa_state *xas, xa_mark_t mark)
  695. {
  696. struct xa_node *node = xas->xa_node;
  697. unsigned int offset = xas->xa_offset;
  698. if (xas_invalid(xas))
  699. return;
  700. while (node) {
  701. if (!node_clear_mark(node, offset, mark))
  702. return;
  703. if (node_any_mark(node, mark))
  704. return;
  705. offset = node->offset;
  706. node = xa_parent_locked(xas->xa, node);
  707. }
  708. if (xa_marked(xas->xa, mark))
  709. xa_mark_clear(xas->xa, mark);
  710. }
  711. EXPORT_SYMBOL_GPL(xas_clear_mark);
  712. /**
  713. * xas_init_marks() - Initialise all marks for the entry
  714. * @xas: Array operations state.
  715. *
  716. * Initialise all marks for the entry specified by @xas. If we're tracking
  717. * free entries with a mark, we need to set it on all entries. All other
  718. * marks are cleared.
  719. *
  720. * This implementation is not as efficient as it could be; we may walk
  721. * up the tree multiple times.
  722. */
  723. void xas_init_marks(const struct xa_state *xas)
  724. {
  725. xa_mark_t mark = 0;
  726. for (;;) {
  727. xas_clear_mark(xas, mark);
  728. if (mark == XA_MARK_MAX)
  729. break;
  730. mark_inc(mark);
  731. }
  732. }
  733. EXPORT_SYMBOL_GPL(xas_init_marks);
  734. /**
  735. * xa_init_flags() - Initialise an empty XArray with flags.
  736. * @xa: XArray.
  737. * @flags: XA_FLAG values.
  738. *
  739. * If you need to initialise an XArray with special flags (eg you need
  740. * to take the lock from interrupt context), use this function instead
  741. * of xa_init().
  742. *
  743. * Context: Any context.
  744. */
  745. void xa_init_flags(struct xarray *xa, gfp_t flags)
  746. {
  747. unsigned int lock_type;
  748. static struct lock_class_key xa_lock_irq;
  749. static struct lock_class_key xa_lock_bh;
  750. spin_lock_init(&xa->xa_lock);
  751. xa->xa_flags = flags;
  752. xa->xa_head = NULL;
  753. lock_type = xa_lock_type(xa);
  754. if (lock_type == XA_LOCK_IRQ)
  755. lockdep_set_class(&xa->xa_lock, &xa_lock_irq);
  756. else if (lock_type == XA_LOCK_BH)
  757. lockdep_set_class(&xa->xa_lock, &xa_lock_bh);
  758. }
  759. EXPORT_SYMBOL(xa_init_flags);
  760. /**
  761. * xa_load() - Load an entry from an XArray.
  762. * @xa: XArray.
  763. * @index: index into array.
  764. *
  765. * Context: Any context. Takes and releases the RCU lock.
  766. * Return: The entry at @index in @xa.
  767. */
  768. void *xa_load(struct xarray *xa, unsigned long index)
  769. {
  770. XA_STATE(xas, xa, index);
  771. void *entry;
  772. rcu_read_lock();
  773. do {
  774. entry = xas_load(&xas);
  775. } while (xas_retry(&xas, entry));
  776. rcu_read_unlock();
  777. return entry;
  778. }
  779. EXPORT_SYMBOL(xa_load);
  780. static void *xas_result(struct xa_state *xas, void *curr)
  781. {
  782. XA_NODE_BUG_ON(xas->xa_node, xa_is_internal(curr));
  783. if (xas_error(xas))
  784. curr = xas->xa_node;
  785. return curr;
  786. }
  787. /**
  788. * __xa_erase() - Erase this entry from the XArray while locked.
  789. * @xa: XArray.
  790. * @index: Index into array.
  791. *
  792. * If the entry at this index is a multi-index entry then all indices will
  793. * be erased, and the entry will no longer be a multi-index entry.
  794. * This function expects the xa_lock to be held on entry.
  795. *
  796. * Context: Any context. Expects xa_lock to be held on entry. May
  797. * release and reacquire xa_lock if @gfp flags permit.
  798. * Return: The old entry at this index.
  799. */
  800. void *__xa_erase(struct xarray *xa, unsigned long index)
  801. {
  802. XA_STATE(xas, xa, index);
  803. return xas_result(&xas, xas_store(&xas, NULL));
  804. }
  805. EXPORT_SYMBOL_GPL(__xa_erase);
  806. /**
  807. * xa_store() - Store this entry in the XArray.
  808. * @xa: XArray.
  809. * @index: Index into array.
  810. * @entry: New entry.
  811. * @gfp: Memory allocation flags.
  812. *
  813. * After this function returns, loads from this index will return @entry.
  814. * Storing into an existing multislot entry updates the entry of every index.
  815. * The marks associated with @index are unaffected unless @entry is %NULL.
  816. *
  817. * Context: Process context. Takes and releases the xa_lock. May sleep
  818. * if the @gfp flags permit.
  819. * Return: The old entry at this index on success, xa_err(-EINVAL) if @entry
  820. * cannot be stored in an XArray, or xa_err(-ENOMEM) if memory allocation
  821. * failed.
  822. */
  823. void *xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
  824. {
  825. XA_STATE(xas, xa, index);
  826. void *curr;
  827. if (WARN_ON_ONCE(xa_is_internal(entry)))
  828. return XA_ERROR(-EINVAL);
  829. do {
  830. xas_lock(&xas);
  831. curr = xas_store(&xas, entry);
  832. xas_unlock(&xas);
  833. } while (xas_nomem(&xas, gfp));
  834. return xas_result(&xas, curr);
  835. }
  836. EXPORT_SYMBOL(xa_store);
  837. /**
  838. * __xa_store() - Store this entry in the XArray.
  839. * @xa: XArray.
  840. * @index: Index into array.
  841. * @entry: New entry.
  842. * @gfp: Memory allocation flags.
  843. *
  844. * You must already be holding the xa_lock when calling this function.
  845. * It will drop the lock if needed to allocate memory, and then reacquire
  846. * it afterwards.
  847. *
  848. * Context: Any context. Expects xa_lock to be held on entry. May
  849. * release and reacquire xa_lock if @gfp flags permit.
  850. * Return: The old entry at this index or xa_err() if an error happened.
  851. */
  852. void *__xa_store(struct xarray *xa, unsigned long index, void *entry, gfp_t gfp)
  853. {
  854. XA_STATE(xas, xa, index);
  855. void *curr;
  856. if (WARN_ON_ONCE(xa_is_internal(entry)))
  857. return XA_ERROR(-EINVAL);
  858. do {
  859. curr = xas_store(&xas, entry);
  860. } while (__xas_nomem(&xas, gfp));
  861. return xas_result(&xas, curr);
  862. }
  863. EXPORT_SYMBOL(__xa_store);
  864. /**
  865. * __xa_set_mark() - Set this mark on this entry while locked.
  866. * @xa: XArray.
  867. * @index: Index of entry.
  868. * @mark: Mark number.
  869. *
  870. * Attempting to set a mark on a NULL entry does not succeed.
  871. *
  872. * Context: Any context. Expects xa_lock to be held on entry.
  873. */
  874. void __xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  875. {
  876. XA_STATE(xas, xa, index);
  877. void *entry = xas_load(&xas);
  878. if (entry)
  879. xas_set_mark(&xas, mark);
  880. }
  881. EXPORT_SYMBOL_GPL(__xa_set_mark);
  882. /**
  883. * __xa_clear_mark() - Clear this mark on this entry while locked.
  884. * @xa: XArray.
  885. * @index: Index of entry.
  886. * @mark: Mark number.
  887. *
  888. * Context: Any context. Expects xa_lock to be held on entry.
  889. */
  890. void __xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  891. {
  892. XA_STATE(xas, xa, index);
  893. void *entry = xas_load(&xas);
  894. if (entry)
  895. xas_clear_mark(&xas, mark);
  896. }
  897. EXPORT_SYMBOL_GPL(__xa_clear_mark);
  898. /**
  899. * xa_get_mark() - Inquire whether this mark is set on this entry.
  900. * @xa: XArray.
  901. * @index: Index of entry.
  902. * @mark: Mark number.
  903. *
  904. * This function uses the RCU read lock, so the result may be out of date
  905. * by the time it returns. If you need the result to be stable, use a lock.
  906. *
  907. * Context: Any context. Takes and releases the RCU lock.
  908. * Return: True if the entry at @index has this mark set, false if it doesn't.
  909. */
  910. bool xa_get_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  911. {
  912. XA_STATE(xas, xa, index);
  913. void *entry;
  914. rcu_read_lock();
  915. entry = xas_start(&xas);
  916. while (xas_get_mark(&xas, mark)) {
  917. if (!xa_is_node(entry))
  918. goto found;
  919. entry = xas_descend(&xas, xa_to_node(entry));
  920. }
  921. rcu_read_unlock();
  922. return false;
  923. found:
  924. rcu_read_unlock();
  925. return true;
  926. }
  927. EXPORT_SYMBOL(xa_get_mark);
  928. /**
  929. * xa_set_mark() - Set this mark on this entry.
  930. * @xa: XArray.
  931. * @index: Index of entry.
  932. * @mark: Mark number.
  933. *
  934. * Attempting to set a mark on a NULL entry does not succeed.
  935. *
  936. * Context: Process context. Takes and releases the xa_lock.
  937. */
  938. void xa_set_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  939. {
  940. xa_lock(xa);
  941. __xa_set_mark(xa, index, mark);
  942. xa_unlock(xa);
  943. }
  944. EXPORT_SYMBOL(xa_set_mark);
  945. /**
  946. * xa_clear_mark() - Clear this mark on this entry.
  947. * @xa: XArray.
  948. * @index: Index of entry.
  949. * @mark: Mark number.
  950. *
  951. * Clearing a mark always succeeds.
  952. *
  953. * Context: Process context. Takes and releases the xa_lock.
  954. */
  955. void xa_clear_mark(struct xarray *xa, unsigned long index, xa_mark_t mark)
  956. {
  957. xa_lock(xa);
  958. __xa_clear_mark(xa, index, mark);
  959. xa_unlock(xa);
  960. }
  961. EXPORT_SYMBOL(xa_clear_mark);
  962. #ifdef XA_DEBUG
  963. void xa_dump_node(const struct xa_node *node)
  964. {
  965. unsigned i, j;
  966. if (!node)
  967. return;
  968. if ((unsigned long)node & 3) {
  969. pr_cont("node %px\n", node);
  970. return;
  971. }
  972. pr_cont("node %px %s %d parent %px shift %d count %d values %d "
  973. "array %px list %px %px marks",
  974. node, node->parent ? "offset" : "max", node->offset,
  975. node->parent, node->shift, node->count, node->nr_values,
  976. node->array, node->private_list.prev, node->private_list.next);
  977. for (i = 0; i < XA_MAX_MARKS; i++)
  978. for (j = 0; j < XA_MARK_LONGS; j++)
  979. pr_cont(" %lx", node->marks[i][j]);
  980. pr_cont("\n");
  981. }
  982. void xa_dump_index(unsigned long index, unsigned int shift)
  983. {
  984. if (!shift)
  985. pr_info("%lu: ", index);
  986. else if (shift >= BITS_PER_LONG)
  987. pr_info("0-%lu: ", ~0UL);
  988. else
  989. pr_info("%lu-%lu: ", index, index | ((1UL << shift) - 1));
  990. }
  991. void xa_dump_entry(const void *entry, unsigned long index, unsigned long shift)
  992. {
  993. if (!entry)
  994. return;
  995. xa_dump_index(index, shift);
  996. if (xa_is_node(entry)) {
  997. if (shift == 0) {
  998. pr_cont("%px\n", entry);
  999. } else {
  1000. unsigned long i;
  1001. struct xa_node *node = xa_to_node(entry);
  1002. xa_dump_node(node);
  1003. for (i = 0; i < XA_CHUNK_SIZE; i++)
  1004. xa_dump_entry(node->slots[i],
  1005. index + (i << node->shift), node->shift);
  1006. }
  1007. } else if (xa_is_value(entry))
  1008. pr_cont("value %ld (0x%lx) [%px]\n", xa_to_value(entry),
  1009. xa_to_value(entry), entry);
  1010. else if (!xa_is_internal(entry))
  1011. pr_cont("%px\n", entry);
  1012. else if (xa_is_retry(entry))
  1013. pr_cont("retry (%ld)\n", xa_to_internal(entry));
  1014. else if (xa_is_sibling(entry))
  1015. pr_cont("sibling (slot %ld)\n", xa_to_sibling(entry));
  1016. else
  1017. pr_cont("UNKNOWN ENTRY (%px)\n", entry);
  1018. }
  1019. void xa_dump(const struct xarray *xa)
  1020. {
  1021. void *entry = xa->xa_head;
  1022. unsigned int shift = 0;
  1023. pr_info("xarray: %px head %px flags %x marks %d %d %d\n", xa, entry,
  1024. xa->xa_flags, xa_marked(xa, XA_MARK_0),
  1025. xa_marked(xa, XA_MARK_1), xa_marked(xa, XA_MARK_2));
  1026. if (xa_is_node(entry))
  1027. shift = xa_to_node(entry)->shift + XA_CHUNK_SHIFT;
  1028. xa_dump_entry(entry, 0, shift);
  1029. }
  1030. #endif