xarray.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. #ifndef _LINUX_XARRAY_H
  3. #define _LINUX_XARRAY_H
  4. /*
  5. * eXtensible Arrays
  6. * Copyright (c) 2017 Microsoft Corporation
  7. * Author: Matthew Wilcox <willy@infradead.org>
  8. *
  9. * See Documentation/core-api/xarray.rst for how to use the XArray.
  10. */
  11. #include <linux/bug.h>
  12. #include <linux/compiler.h>
  13. #include <linux/gfp.h>
  14. #include <linux/kconfig.h>
  15. #include <linux/kernel.h>
  16. #include <linux/rcupdate.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/types.h>
  19. /*
  20. * The bottom two bits of the entry determine how the XArray interprets
  21. * the contents:
  22. *
  23. * 00: Pointer entry
  24. * 10: Internal entry
  25. * x1: Value entry or tagged pointer
  26. *
  27. * Attempting to store internal entries in the XArray is a bug.
  28. *
  29. * Most internal entries are pointers to the next node in the tree.
  30. * The following internal entries have a special meaning:
  31. *
  32. * 0-62: Sibling entries
  33. * 256: Zero entry
  34. * 257: Retry entry
  35. *
  36. * Errors are also represented as internal entries, but use the negative
  37. * space (-4094 to -2). They're never stored in the slots array; only
  38. * returned by the normal API.
  39. */
  40. #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
  41. /**
  42. * xa_mk_value() - Create an XArray entry from an integer.
  43. * @v: Value to store in XArray.
  44. *
  45. * Context: Any context.
  46. * Return: An entry suitable for storing in the XArray.
  47. */
  48. static inline void *xa_mk_value(unsigned long v)
  49. {
  50. WARN_ON((long)v < 0);
  51. return (void *)((v << 1) | 1);
  52. }
  53. /**
  54. * xa_to_value() - Get value stored in an XArray entry.
  55. * @entry: XArray entry.
  56. *
  57. * Context: Any context.
  58. * Return: The value stored in the XArray entry.
  59. */
  60. static inline unsigned long xa_to_value(const void *entry)
  61. {
  62. return (unsigned long)entry >> 1;
  63. }
  64. /**
  65. * xa_is_value() - Determine if an entry is a value.
  66. * @entry: XArray entry.
  67. *
  68. * Context: Any context.
  69. * Return: True if the entry is a value, false if it is a pointer.
  70. */
  71. static inline bool xa_is_value(const void *entry)
  72. {
  73. return (unsigned long)entry & 1;
  74. }
  75. /**
  76. * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
  77. * @p: Plain pointer.
  78. * @tag: Tag value (0, 1 or 3).
  79. *
  80. * If the user of the XArray prefers, they can tag their pointers instead
  81. * of storing value entries. Three tags are available (0, 1 and 3).
  82. * These are distinct from the xa_mark_t as they are not replicated up
  83. * through the array and cannot be searched for.
  84. *
  85. * Context: Any context.
  86. * Return: An XArray entry.
  87. */
  88. static inline void *xa_tag_pointer(void *p, unsigned long tag)
  89. {
  90. return (void *)((unsigned long)p | tag);
  91. }
  92. /**
  93. * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
  94. * @entry: XArray entry.
  95. *
  96. * If you have stored a tagged pointer in the XArray, call this function
  97. * to get the untagged version of the pointer.
  98. *
  99. * Context: Any context.
  100. * Return: A pointer.
  101. */
  102. static inline void *xa_untag_pointer(void *entry)
  103. {
  104. return (void *)((unsigned long)entry & ~3UL);
  105. }
  106. /**
  107. * xa_pointer_tag() - Get the tag stored in an XArray entry.
  108. * @entry: XArray entry.
  109. *
  110. * If you have stored a tagged pointer in the XArray, call this function
  111. * to get the tag of that pointer.
  112. *
  113. * Context: Any context.
  114. * Return: A tag.
  115. */
  116. static inline unsigned int xa_pointer_tag(void *entry)
  117. {
  118. return (unsigned long)entry & 3UL;
  119. }
  120. /*
  121. * xa_mk_internal() - Create an internal entry.
  122. * @v: Value to turn into an internal entry.
  123. *
  124. * Context: Any context.
  125. * Return: An XArray internal entry corresponding to this value.
  126. */
  127. static inline void *xa_mk_internal(unsigned long v)
  128. {
  129. return (void *)((v << 2) | 2);
  130. }
  131. /*
  132. * xa_to_internal() - Extract the value from an internal entry.
  133. * @entry: XArray entry.
  134. *
  135. * Context: Any context.
  136. * Return: The value which was stored in the internal entry.
  137. */
  138. static inline unsigned long xa_to_internal(const void *entry)
  139. {
  140. return (unsigned long)entry >> 2;
  141. }
  142. /*
  143. * xa_is_internal() - Is the entry an internal entry?
  144. * @entry: XArray entry.
  145. *
  146. * Context: Any context.
  147. * Return: %true if the entry is an internal entry.
  148. */
  149. static inline bool xa_is_internal(const void *entry)
  150. {
  151. return ((unsigned long)entry & 3) == 2;
  152. }
  153. /**
  154. * xa_is_err() - Report whether an XArray operation returned an error
  155. * @entry: Result from calling an XArray function
  156. *
  157. * If an XArray operation cannot complete an operation, it will return
  158. * a special value indicating an error. This function tells you
  159. * whether an error occurred; xa_err() tells you which error occurred.
  160. *
  161. * Context: Any context.
  162. * Return: %true if the entry indicates an error.
  163. */
  164. static inline bool xa_is_err(const void *entry)
  165. {
  166. return unlikely(xa_is_internal(entry));
  167. }
  168. /**
  169. * xa_err() - Turn an XArray result into an errno.
  170. * @entry: Result from calling an XArray function.
  171. *
  172. * If an XArray operation cannot complete an operation, it will return
  173. * a special pointer value which encodes an errno. This function extracts
  174. * the errno from the pointer value, or returns 0 if the pointer does not
  175. * represent an errno.
  176. *
  177. * Context: Any context.
  178. * Return: A negative errno or 0.
  179. */
  180. static inline int xa_err(void *entry)
  181. {
  182. /* xa_to_internal() would not do sign extension. */
  183. if (xa_is_err(entry))
  184. return (long)entry >> 2;
  185. return 0;
  186. }
  187. typedef unsigned __bitwise xa_mark_t;
  188. #define XA_MARK_0 ((__force xa_mark_t)0U)
  189. #define XA_MARK_1 ((__force xa_mark_t)1U)
  190. #define XA_MARK_2 ((__force xa_mark_t)2U)
  191. #define XA_PRESENT ((__force xa_mark_t)8U)
  192. #define XA_MARK_MAX XA_MARK_2
  193. #define XA_FREE_MARK XA_MARK_0
  194. enum xa_lock_type {
  195. XA_LOCK_IRQ = 1,
  196. XA_LOCK_BH = 2,
  197. };
  198. /*
  199. * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
  200. * and we remain compatible with that.
  201. */
  202. #define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
  203. #define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
  204. #define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U)
  205. #define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
  206. (__force unsigned)(mark)))
  207. #define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
  208. /**
  209. * struct xarray - The anchor of the XArray.
  210. * @xa_lock: Lock that protects the contents of the XArray.
  211. *
  212. * To use the xarray, define it statically or embed it in your data structure.
  213. * It is a very small data structure, so it does not usually make sense to
  214. * allocate it separately and keep a pointer to it in your data structure.
  215. *
  216. * You may use the xa_lock to protect your own data structures as well.
  217. */
  218. /*
  219. * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
  220. * If the only non-NULL entry in the array is at index 0, @xa_head is that
  221. * entry. If any other entry in the array is non-NULL, @xa_head points
  222. * to an @xa_node.
  223. */
  224. struct xarray {
  225. spinlock_t xa_lock;
  226. /* private: The rest of the data structure is not to be used directly. */
  227. gfp_t xa_flags;
  228. void __rcu * xa_head;
  229. };
  230. #define XARRAY_INIT(name, flags) { \
  231. .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
  232. .xa_flags = flags, \
  233. .xa_head = NULL, \
  234. }
  235. /**
  236. * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
  237. * @name: A string that names your XArray.
  238. * @flags: XA_FLAG values.
  239. *
  240. * This is intended for file scope definitions of XArrays. It declares
  241. * and initialises an empty XArray with the chosen name and flags. It is
  242. * equivalent to calling xa_init_flags() on the array, but it does the
  243. * initialisation at compiletime instead of runtime.
  244. */
  245. #define DEFINE_XARRAY_FLAGS(name, flags) \
  246. struct xarray name = XARRAY_INIT(name, flags)
  247. /**
  248. * DEFINE_XARRAY() - Define an XArray.
  249. * @name: A string that names your XArray.
  250. *
  251. * This is intended for file scope definitions of XArrays. It declares
  252. * and initialises an empty XArray with the chosen name. It is equivalent
  253. * to calling xa_init() on the array, but it does the initialisation at
  254. * compiletime instead of runtime.
  255. */
  256. #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
  257. /**
  258. * DEFINE_XARRAY_ALLOC() - Define an XArray which can allocate IDs.
  259. * @name: A string that names your XArray.
  260. *
  261. * This is intended for file scope definitions of allocating XArrays.
  262. * See also DEFINE_XARRAY().
  263. */
  264. #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
  265. void xa_init_flags(struct xarray *, gfp_t flags);
  266. void *xa_load(struct xarray *, unsigned long index);
  267. void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
  268. void *xa_cmpxchg(struct xarray *, unsigned long index,
  269. void *old, void *entry, gfp_t);
  270. int xa_reserve(struct xarray *, unsigned long index, gfp_t);
  271. void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
  272. void *entry, gfp_t);
  273. bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
  274. void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
  275. void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
  276. void *xa_find(struct xarray *xa, unsigned long *index,
  277. unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
  278. void *xa_find_after(struct xarray *xa, unsigned long *index,
  279. unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
  280. unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
  281. unsigned long max, unsigned int n, xa_mark_t);
  282. void xa_destroy(struct xarray *);
  283. /**
  284. * xa_init() - Initialise an empty XArray.
  285. * @xa: XArray.
  286. *
  287. * An empty XArray is full of NULL entries.
  288. *
  289. * Context: Any context.
  290. */
  291. static inline void xa_init(struct xarray *xa)
  292. {
  293. xa_init_flags(xa, 0);
  294. }
  295. /**
  296. * xa_empty() - Determine if an array has any present entries.
  297. * @xa: XArray.
  298. *
  299. * Context: Any context.
  300. * Return: %true if the array contains only NULL pointers.
  301. */
  302. static inline bool xa_empty(const struct xarray *xa)
  303. {
  304. return xa->xa_head == NULL;
  305. }
  306. /**
  307. * xa_marked() - Inquire whether any entry in this array has a mark set
  308. * @xa: Array
  309. * @mark: Mark value
  310. *
  311. * Context: Any context.
  312. * Return: %true if any entry has this mark set.
  313. */
  314. static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
  315. {
  316. return xa->xa_flags & XA_FLAGS_MARK(mark);
  317. }
  318. /**
  319. * xa_erase() - Erase this entry from the XArray.
  320. * @xa: XArray.
  321. * @index: Index of entry.
  322. *
  323. * This function is the equivalent of calling xa_store() with %NULL as
  324. * the third argument. The XArray does not need to allocate memory, so
  325. * the user does not need to provide GFP flags.
  326. *
  327. * Context: Process context. Takes and releases the xa_lock.
  328. * Return: The entry which used to be at this index.
  329. */
  330. static inline void *xa_erase(struct xarray *xa, unsigned long index)
  331. {
  332. return xa_store(xa, index, NULL, 0);
  333. }
  334. /**
  335. * xa_insert() - Store this entry in the XArray unless another entry is
  336. * already present.
  337. * @xa: XArray.
  338. * @index: Index into array.
  339. * @entry: New entry.
  340. * @gfp: Memory allocation flags.
  341. *
  342. * If you would rather see the existing entry in the array, use xa_cmpxchg().
  343. * This function is for users who don't care what the entry is, only that
  344. * one is present.
  345. *
  346. * Context: Process context. Takes and releases the xa_lock.
  347. * May sleep if the @gfp flags permit.
  348. * Return: 0 if the store succeeded. -EEXIST if another entry was present.
  349. * -ENOMEM if memory could not be allocated.
  350. */
  351. static inline int xa_insert(struct xarray *xa, unsigned long index,
  352. void *entry, gfp_t gfp)
  353. {
  354. void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
  355. if (!curr)
  356. return 0;
  357. if (xa_is_err(curr))
  358. return xa_err(curr);
  359. return -EEXIST;
  360. }
  361. /**
  362. * xa_release() - Release a reserved entry.
  363. * @xa: XArray.
  364. * @index: Index of entry.
  365. *
  366. * After calling xa_reserve(), you can call this function to release the
  367. * reservation. If the entry at @index has been stored to, this function
  368. * will do nothing.
  369. */
  370. static inline void xa_release(struct xarray *xa, unsigned long index)
  371. {
  372. xa_cmpxchg(xa, index, NULL, NULL, 0);
  373. }
  374. /**
  375. * xa_for_each() - Iterate over a portion of an XArray.
  376. * @xa: XArray.
  377. * @entry: Entry retrieved from array.
  378. * @index: Index of @entry.
  379. * @max: Maximum index to retrieve from array.
  380. * @filter: Selection criterion.
  381. *
  382. * Initialise @index to the lowest index you want to retrieve from the
  383. * array. During the iteration, @entry will have the value of the entry
  384. * stored in @xa at @index. The iteration will skip all entries in the
  385. * array which do not match @filter. You may modify @index during the
  386. * iteration if you want to skip or reprocess indices. It is safe to modify
  387. * the array during the iteration. At the end of the iteration, @entry will
  388. * be set to NULL and @index will have a value less than or equal to max.
  389. *
  390. * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
  391. * to handle your own locking with xas_for_each(), and if you have to unlock
  392. * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
  393. * will spin if it hits a retry entry; if you intend to see retry entries,
  394. * you should use the xas_for_each() iterator instead. The xas_for_each()
  395. * iterator will expand into more inline code than xa_for_each().
  396. *
  397. * Context: Any context. Takes and releases the RCU lock.
  398. */
  399. #define xa_for_each(xa, entry, index, max, filter) \
  400. for (entry = xa_find(xa, &index, max, filter); entry; \
  401. entry = xa_find_after(xa, &index, max, filter))
  402. #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
  403. #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
  404. #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
  405. #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
  406. #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
  407. #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
  408. #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
  409. #define xa_lock_irqsave(xa, flags) \
  410. spin_lock_irqsave(&(xa)->xa_lock, flags)
  411. #define xa_unlock_irqrestore(xa, flags) \
  412. spin_unlock_irqrestore(&(xa)->xa_lock, flags)
  413. /*
  414. * Versions of the normal API which require the caller to hold the
  415. * xa_lock. If the GFP flags allow it, they will drop the lock to
  416. * allocate memory, then reacquire it afterwards. These functions
  417. * may also re-enable interrupts if the XArray flags indicate the
  418. * locking should be interrupt safe.
  419. */
  420. void *__xa_erase(struct xarray *, unsigned long index);
  421. void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
  422. void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
  423. void *entry, gfp_t);
  424. int __xa_alloc(struct xarray *, u32 *id, u32 max, void *entry, gfp_t);
  425. void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
  426. void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
  427. /**
  428. * __xa_insert() - Store this entry in the XArray unless another entry is
  429. * already present.
  430. * @xa: XArray.
  431. * @index: Index into array.
  432. * @entry: New entry.
  433. * @gfp: Memory allocation flags.
  434. *
  435. * If you would rather see the existing entry in the array, use __xa_cmpxchg().
  436. * This function is for users who don't care what the entry is, only that
  437. * one is present.
  438. *
  439. * Context: Any context. Expects xa_lock to be held on entry. May
  440. * release and reacquire xa_lock if the @gfp flags permit.
  441. * Return: 0 if the store succeeded. -EEXIST if another entry was present.
  442. * -ENOMEM if memory could not be allocated.
  443. */
  444. static inline int __xa_insert(struct xarray *xa, unsigned long index,
  445. void *entry, gfp_t gfp)
  446. {
  447. void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
  448. if (!curr)
  449. return 0;
  450. if (xa_is_err(curr))
  451. return xa_err(curr);
  452. return -EEXIST;
  453. }
  454. /**
  455. * xa_erase_bh() - Erase this entry from the XArray.
  456. * @xa: XArray.
  457. * @index: Index of entry.
  458. *
  459. * This function is the equivalent of calling xa_store() with %NULL as
  460. * the third argument. The XArray does not need to allocate memory, so
  461. * the user does not need to provide GFP flags.
  462. *
  463. * Context: Process context. Takes and releases the xa_lock while
  464. * disabling softirqs.
  465. * Return: The entry which used to be at this index.
  466. */
  467. static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
  468. {
  469. void *entry;
  470. xa_lock_bh(xa);
  471. entry = __xa_erase(xa, index);
  472. xa_unlock_bh(xa);
  473. return entry;
  474. }
  475. /**
  476. * xa_erase_irq() - Erase this entry from the XArray.
  477. * @xa: XArray.
  478. * @index: Index of entry.
  479. *
  480. * This function is the equivalent of calling xa_store() with %NULL as
  481. * the third argument. The XArray does not need to allocate memory, so
  482. * the user does not need to provide GFP flags.
  483. *
  484. * Context: Process context. Takes and releases the xa_lock while
  485. * disabling interrupts.
  486. * Return: The entry which used to be at this index.
  487. */
  488. static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
  489. {
  490. void *entry;
  491. xa_lock_irq(xa);
  492. entry = __xa_erase(xa, index);
  493. xa_unlock_irq(xa);
  494. return entry;
  495. }
  496. /**
  497. * xa_alloc() - Find somewhere to store this entry in the XArray.
  498. * @xa: XArray.
  499. * @id: Pointer to ID.
  500. * @max: Maximum ID to allocate (inclusive).
  501. * @entry: New entry.
  502. * @gfp: Memory allocation flags.
  503. *
  504. * Allocates an unused ID in the range specified by @id and @max.
  505. * Updates the @id pointer with the index, then stores the entry at that
  506. * index. A concurrent lookup will not see an uninitialised @id.
  507. *
  508. * Context: Process context. Takes and releases the xa_lock. May sleep if
  509. * the @gfp flags permit.
  510. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  511. * there is no more space in the XArray.
  512. */
  513. static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
  514. gfp_t gfp)
  515. {
  516. int err;
  517. xa_lock(xa);
  518. err = __xa_alloc(xa, id, max, entry, gfp);
  519. xa_unlock(xa);
  520. return err;
  521. }
  522. /**
  523. * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
  524. * @xa: XArray.
  525. * @id: Pointer to ID.
  526. * @max: Maximum ID to allocate (inclusive).
  527. * @entry: New entry.
  528. * @gfp: Memory allocation flags.
  529. *
  530. * Allocates an unused ID in the range specified by @id and @max.
  531. * Updates the @id pointer with the index, then stores the entry at that
  532. * index. A concurrent lookup will not see an uninitialised @id.
  533. *
  534. * Context: Process context. Takes and releases the xa_lock while
  535. * disabling softirqs. May sleep if the @gfp flags permit.
  536. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  537. * there is no more space in the XArray.
  538. */
  539. static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
  540. gfp_t gfp)
  541. {
  542. int err;
  543. xa_lock_bh(xa);
  544. err = __xa_alloc(xa, id, max, entry, gfp);
  545. xa_unlock_bh(xa);
  546. return err;
  547. }
  548. /**
  549. * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
  550. * @xa: XArray.
  551. * @id: Pointer to ID.
  552. * @max: Maximum ID to allocate (inclusive).
  553. * @entry: New entry.
  554. * @gfp: Memory allocation flags.
  555. *
  556. * Allocates an unused ID in the range specified by @id and @max.
  557. * Updates the @id pointer with the index, then stores the entry at that
  558. * index. A concurrent lookup will not see an uninitialised @id.
  559. *
  560. * Context: Process context. Takes and releases the xa_lock while
  561. * disabling interrupts. May sleep if the @gfp flags permit.
  562. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  563. * there is no more space in the XArray.
  564. */
  565. static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
  566. gfp_t gfp)
  567. {
  568. int err;
  569. xa_lock_irq(xa);
  570. err = __xa_alloc(xa, id, max, entry, gfp);
  571. xa_unlock_irq(xa);
  572. return err;
  573. }
  574. /* Everything below here is the Advanced API. Proceed with caution. */
  575. /*
  576. * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
  577. * the best chunk size requires some tradeoffs. A power of two recommends
  578. * itself so that we can walk the tree based purely on shifts and masks.
  579. * Generally, the larger the better; as the number of slots per level of the
  580. * tree increases, the less tall the tree needs to be. But that needs to be
  581. * balanced against the memory consumption of each node. On a 64-bit system,
  582. * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
  583. * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
  584. */
  585. #ifndef XA_CHUNK_SHIFT
  586. #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  587. #endif
  588. #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
  589. #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
  590. #define XA_MAX_MARKS 3
  591. #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
  592. /*
  593. * @count is the count of every non-NULL element in the ->slots array
  594. * whether that is a value entry, a retry entry, a user pointer,
  595. * a sibling entry or a pointer to the next level of the tree.
  596. * @nr_values is the count of every element in ->slots which is
  597. * either a value entry or a sibling of a value entry.
  598. */
  599. struct xa_node {
  600. unsigned char shift; /* Bits remaining in each slot */
  601. unsigned char offset; /* Slot offset in parent */
  602. unsigned char count; /* Total entry count */
  603. unsigned char nr_values; /* Value entry count */
  604. struct xa_node __rcu *parent; /* NULL at top of tree */
  605. struct xarray *array; /* The array we belong to */
  606. union {
  607. struct list_head private_list; /* For tree user */
  608. struct rcu_head rcu_head; /* Used when freeing node */
  609. };
  610. void __rcu *slots[XA_CHUNK_SIZE];
  611. union {
  612. unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
  613. unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
  614. };
  615. };
  616. void xa_dump(const struct xarray *);
  617. void xa_dump_node(const struct xa_node *);
  618. #ifdef XA_DEBUG
  619. #define XA_BUG_ON(xa, x) do { \
  620. if (x) { \
  621. xa_dump(xa); \
  622. BUG(); \
  623. } \
  624. } while (0)
  625. #define XA_NODE_BUG_ON(node, x) do { \
  626. if (x) { \
  627. if (node) xa_dump_node(node); \
  628. BUG(); \
  629. } \
  630. } while (0)
  631. #else
  632. #define XA_BUG_ON(xa, x) do { } while (0)
  633. #define XA_NODE_BUG_ON(node, x) do { } while (0)
  634. #endif
  635. /* Private */
  636. static inline void *xa_head(const struct xarray *xa)
  637. {
  638. return rcu_dereference_check(xa->xa_head,
  639. lockdep_is_held(&xa->xa_lock));
  640. }
  641. /* Private */
  642. static inline void *xa_head_locked(const struct xarray *xa)
  643. {
  644. return rcu_dereference_protected(xa->xa_head,
  645. lockdep_is_held(&xa->xa_lock));
  646. }
  647. /* Private */
  648. static inline void *xa_entry(const struct xarray *xa,
  649. const struct xa_node *node, unsigned int offset)
  650. {
  651. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  652. return rcu_dereference_check(node->slots[offset],
  653. lockdep_is_held(&xa->xa_lock));
  654. }
  655. /* Private */
  656. static inline void *xa_entry_locked(const struct xarray *xa,
  657. const struct xa_node *node, unsigned int offset)
  658. {
  659. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  660. return rcu_dereference_protected(node->slots[offset],
  661. lockdep_is_held(&xa->xa_lock));
  662. }
  663. /* Private */
  664. static inline struct xa_node *xa_parent(const struct xarray *xa,
  665. const struct xa_node *node)
  666. {
  667. return rcu_dereference_check(node->parent,
  668. lockdep_is_held(&xa->xa_lock));
  669. }
  670. /* Private */
  671. static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
  672. const struct xa_node *node)
  673. {
  674. return rcu_dereference_protected(node->parent,
  675. lockdep_is_held(&xa->xa_lock));
  676. }
  677. /* Private */
  678. static inline void *xa_mk_node(const struct xa_node *node)
  679. {
  680. return (void *)((unsigned long)node | 2);
  681. }
  682. /* Private */
  683. static inline struct xa_node *xa_to_node(const void *entry)
  684. {
  685. return (struct xa_node *)((unsigned long)entry - 2);
  686. }
  687. /* Private */
  688. static inline bool xa_is_node(const void *entry)
  689. {
  690. return xa_is_internal(entry) && (unsigned long)entry > 4096;
  691. }
  692. /* Private */
  693. static inline void *xa_mk_sibling(unsigned int offset)
  694. {
  695. return xa_mk_internal(offset);
  696. }
  697. /* Private */
  698. static inline unsigned long xa_to_sibling(const void *entry)
  699. {
  700. return xa_to_internal(entry);
  701. }
  702. /**
  703. * xa_is_sibling() - Is the entry a sibling entry?
  704. * @entry: Entry retrieved from the XArray
  705. *
  706. * Return: %true if the entry is a sibling entry.
  707. */
  708. static inline bool xa_is_sibling(const void *entry)
  709. {
  710. return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
  711. (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
  712. }
  713. #define XA_ZERO_ENTRY xa_mk_internal(256)
  714. #define XA_RETRY_ENTRY xa_mk_internal(257)
  715. /**
  716. * xa_is_zero() - Is the entry a zero entry?
  717. * @entry: Entry retrieved from the XArray
  718. *
  719. * Return: %true if the entry is a zero entry.
  720. */
  721. static inline bool xa_is_zero(const void *entry)
  722. {
  723. return unlikely(entry == XA_ZERO_ENTRY);
  724. }
  725. /**
  726. * xa_is_retry() - Is the entry a retry entry?
  727. * @entry: Entry retrieved from the XArray
  728. *
  729. * Return: %true if the entry is a retry entry.
  730. */
  731. static inline bool xa_is_retry(const void *entry)
  732. {
  733. return unlikely(entry == XA_RETRY_ENTRY);
  734. }
  735. /**
  736. * typedef xa_update_node_t - A callback function from the XArray.
  737. * @node: The node which is being processed
  738. *
  739. * This function is called every time the XArray updates the count of
  740. * present and value entries in a node. It allows advanced users to
  741. * maintain the private_list in the node.
  742. *
  743. * Context: The xa_lock is held and interrupts may be disabled.
  744. * Implementations should not drop the xa_lock, nor re-enable
  745. * interrupts.
  746. */
  747. typedef void (*xa_update_node_t)(struct xa_node *node);
  748. /*
  749. * The xa_state is opaque to its users. It contains various different pieces
  750. * of state involved in the current operation on the XArray. It should be
  751. * declared on the stack and passed between the various internal routines.
  752. * The various elements in it should not be accessed directly, but only
  753. * through the provided accessor functions. The below documentation is for
  754. * the benefit of those working on the code, not for users of the XArray.
  755. *
  756. * @xa_node usually points to the xa_node containing the slot we're operating
  757. * on (and @xa_offset is the offset in the slots array). If there is a
  758. * single entry in the array at index 0, there are no allocated xa_nodes to
  759. * point to, and so we store %NULL in @xa_node. @xa_node is set to
  760. * the value %XAS_RESTART if the xa_state is not walked to the correct
  761. * position in the tree of nodes for this operation. If an error occurs
  762. * during an operation, it is set to an %XAS_ERROR value. If we run off the
  763. * end of the allocated nodes, it is set to %XAS_BOUNDS.
  764. */
  765. struct xa_state {
  766. struct xarray *xa;
  767. unsigned long xa_index;
  768. unsigned char xa_shift;
  769. unsigned char xa_sibs;
  770. unsigned char xa_offset;
  771. unsigned char xa_pad; /* Helps gcc generate better code */
  772. struct xa_node *xa_node;
  773. struct xa_node *xa_alloc;
  774. xa_update_node_t xa_update;
  775. };
  776. /*
  777. * We encode errnos in the xas->xa_node. If an error has happened, we need to
  778. * drop the lock to fix it, and once we've done so the xa_state is invalid.
  779. */
  780. #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
  781. #define XAS_BOUNDS ((struct xa_node *)1UL)
  782. #define XAS_RESTART ((struct xa_node *)3UL)
  783. #define __XA_STATE(array, index, shift, sibs) { \
  784. .xa = array, \
  785. .xa_index = index, \
  786. .xa_shift = shift, \
  787. .xa_sibs = sibs, \
  788. .xa_offset = 0, \
  789. .xa_pad = 0, \
  790. .xa_node = XAS_RESTART, \
  791. .xa_alloc = NULL, \
  792. .xa_update = NULL \
  793. }
  794. /**
  795. * XA_STATE() - Declare an XArray operation state.
  796. * @name: Name of this operation state (usually xas).
  797. * @array: Array to operate on.
  798. * @index: Initial index of interest.
  799. *
  800. * Declare and initialise an xa_state on the stack.
  801. */
  802. #define XA_STATE(name, array, index) \
  803. struct xa_state name = __XA_STATE(array, index, 0, 0)
  804. /**
  805. * XA_STATE_ORDER() - Declare an XArray operation state.
  806. * @name: Name of this operation state (usually xas).
  807. * @array: Array to operate on.
  808. * @index: Initial index of interest.
  809. * @order: Order of entry.
  810. *
  811. * Declare and initialise an xa_state on the stack. This variant of
  812. * XA_STATE() allows you to specify the 'order' of the element you
  813. * want to operate on.`
  814. */
  815. #define XA_STATE_ORDER(name, array, index, order) \
  816. struct xa_state name = __XA_STATE(array, \
  817. (index >> order) << order, \
  818. order - (order % XA_CHUNK_SHIFT), \
  819. (1U << (order % XA_CHUNK_SHIFT)) - 1)
  820. #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
  821. #define xas_trylock(xas) xa_trylock((xas)->xa)
  822. #define xas_lock(xas) xa_lock((xas)->xa)
  823. #define xas_unlock(xas) xa_unlock((xas)->xa)
  824. #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
  825. #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
  826. #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
  827. #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
  828. #define xas_lock_irqsave(xas, flags) \
  829. xa_lock_irqsave((xas)->xa, flags)
  830. #define xas_unlock_irqrestore(xas, flags) \
  831. xa_unlock_irqrestore((xas)->xa, flags)
  832. /**
  833. * xas_error() - Return an errno stored in the xa_state.
  834. * @xas: XArray operation state.
  835. *
  836. * Return: 0 if no error has been noted. A negative errno if one has.
  837. */
  838. static inline int xas_error(const struct xa_state *xas)
  839. {
  840. return xa_err(xas->xa_node);
  841. }
  842. /**
  843. * xas_set_err() - Note an error in the xa_state.
  844. * @xas: XArray operation state.
  845. * @err: Negative error number.
  846. *
  847. * Only call this function with a negative @err; zero or positive errors
  848. * will probably not behave the way you think they should. If you want
  849. * to clear the error from an xa_state, use xas_reset().
  850. */
  851. static inline void xas_set_err(struct xa_state *xas, long err)
  852. {
  853. xas->xa_node = XA_ERROR(err);
  854. }
  855. /**
  856. * xas_invalid() - Is the xas in a retry or error state?
  857. * @xas: XArray operation state.
  858. *
  859. * Return: %true if the xas cannot be used for operations.
  860. */
  861. static inline bool xas_invalid(const struct xa_state *xas)
  862. {
  863. return (unsigned long)xas->xa_node & 3;
  864. }
  865. /**
  866. * xas_valid() - Is the xas a valid cursor into the array?
  867. * @xas: XArray operation state.
  868. *
  869. * Return: %true if the xas can be used for operations.
  870. */
  871. static inline bool xas_valid(const struct xa_state *xas)
  872. {
  873. return !xas_invalid(xas);
  874. }
  875. /**
  876. * xas_is_node() - Does the xas point to a node?
  877. * @xas: XArray operation state.
  878. *
  879. * Return: %true if the xas currently references a node.
  880. */
  881. static inline bool xas_is_node(const struct xa_state *xas)
  882. {
  883. return xas_valid(xas) && xas->xa_node;
  884. }
  885. /* True if the pointer is something other than a node */
  886. static inline bool xas_not_node(struct xa_node *node)
  887. {
  888. return ((unsigned long)node & 3) || !node;
  889. }
  890. /* True if the node represents RESTART or an error */
  891. static inline bool xas_frozen(struct xa_node *node)
  892. {
  893. return (unsigned long)node & 2;
  894. }
  895. /* True if the node represents head-of-tree, RESTART or BOUNDS */
  896. static inline bool xas_top(struct xa_node *node)
  897. {
  898. return node <= XAS_RESTART;
  899. }
  900. /**
  901. * xas_reset() - Reset an XArray operation state.
  902. * @xas: XArray operation state.
  903. *
  904. * Resets the error or walk state of the @xas so future walks of the
  905. * array will start from the root. Use this if you have dropped the
  906. * xarray lock and want to reuse the xa_state.
  907. *
  908. * Context: Any context.
  909. */
  910. static inline void xas_reset(struct xa_state *xas)
  911. {
  912. xas->xa_node = XAS_RESTART;
  913. }
  914. /**
  915. * xas_retry() - Retry the operation if appropriate.
  916. * @xas: XArray operation state.
  917. * @entry: Entry from xarray.
  918. *
  919. * The advanced functions may sometimes return an internal entry, such as
  920. * a retry entry or a zero entry. This function sets up the @xas to restart
  921. * the walk from the head of the array if needed.
  922. *
  923. * Context: Any context.
  924. * Return: true if the operation needs to be retried.
  925. */
  926. static inline bool xas_retry(struct xa_state *xas, const void *entry)
  927. {
  928. if (xa_is_zero(entry))
  929. return true;
  930. if (!xa_is_retry(entry))
  931. return false;
  932. xas_reset(xas);
  933. return true;
  934. }
  935. void *xas_load(struct xa_state *);
  936. void *xas_store(struct xa_state *, void *entry);
  937. void *xas_find(struct xa_state *, unsigned long max);
  938. void *xas_find_conflict(struct xa_state *);
  939. bool xas_get_mark(const struct xa_state *, xa_mark_t);
  940. void xas_set_mark(const struct xa_state *, xa_mark_t);
  941. void xas_clear_mark(const struct xa_state *, xa_mark_t);
  942. void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
  943. void xas_init_marks(const struct xa_state *);
  944. bool xas_nomem(struct xa_state *, gfp_t);
  945. void xas_pause(struct xa_state *);
  946. void xas_create_range(struct xa_state *);
  947. /**
  948. * xas_reload() - Refetch an entry from the xarray.
  949. * @xas: XArray operation state.
  950. *
  951. * Use this function to check that a previously loaded entry still has
  952. * the same value. This is useful for the lockless pagecache lookup where
  953. * we walk the array with only the RCU lock to protect us, lock the page,
  954. * then check that the page hasn't moved since we looked it up.
  955. *
  956. * The caller guarantees that @xas is still valid. If it may be in an
  957. * error or restart state, call xas_load() instead.
  958. *
  959. * Return: The entry at this location in the xarray.
  960. */
  961. static inline void *xas_reload(struct xa_state *xas)
  962. {
  963. struct xa_node *node = xas->xa_node;
  964. if (node)
  965. return xa_entry(xas->xa, node, xas->xa_offset);
  966. return xa_head(xas->xa);
  967. }
  968. /**
  969. * xas_set() - Set up XArray operation state for a different index.
  970. * @xas: XArray operation state.
  971. * @index: New index into the XArray.
  972. *
  973. * Move the operation state to refer to a different index. This will
  974. * have the effect of starting a walk from the top; see xas_next()
  975. * to move to an adjacent index.
  976. */
  977. static inline void xas_set(struct xa_state *xas, unsigned long index)
  978. {
  979. xas->xa_index = index;
  980. xas->xa_node = XAS_RESTART;
  981. }
  982. /**
  983. * xas_set_order() - Set up XArray operation state for a multislot entry.
  984. * @xas: XArray operation state.
  985. * @index: Target of the operation.
  986. * @order: Entry occupies 2^@order indices.
  987. */
  988. static inline void xas_set_order(struct xa_state *xas, unsigned long index,
  989. unsigned int order)
  990. {
  991. #ifdef CONFIG_XARRAY_MULTI
  992. xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
  993. xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
  994. xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
  995. xas->xa_node = XAS_RESTART;
  996. #else
  997. BUG_ON(order > 0);
  998. xas_set(xas, index);
  999. #endif
  1000. }
  1001. /**
  1002. * xas_set_update() - Set up XArray operation state for a callback.
  1003. * @xas: XArray operation state.
  1004. * @update: Function to call when updating a node.
  1005. *
  1006. * The XArray can notify a caller after it has updated an xa_node.
  1007. * This is advanced functionality and is only needed by the page cache.
  1008. */
  1009. static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
  1010. {
  1011. xas->xa_update = update;
  1012. }
  1013. /**
  1014. * xas_next_entry() - Advance iterator to next present entry.
  1015. * @xas: XArray operation state.
  1016. * @max: Highest index to return.
  1017. *
  1018. * xas_next_entry() is an inline function to optimise xarray traversal for
  1019. * speed. It is equivalent to calling xas_find(), and will call xas_find()
  1020. * for all the hard cases.
  1021. *
  1022. * Return: The next present entry after the one currently referred to by @xas.
  1023. */
  1024. static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
  1025. {
  1026. struct xa_node *node = xas->xa_node;
  1027. void *entry;
  1028. if (unlikely(xas_not_node(node) || node->shift ||
  1029. xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
  1030. return xas_find(xas, max);
  1031. do {
  1032. if (unlikely(xas->xa_index >= max))
  1033. return xas_find(xas, max);
  1034. if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
  1035. return xas_find(xas, max);
  1036. entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
  1037. if (unlikely(xa_is_internal(entry)))
  1038. return xas_find(xas, max);
  1039. xas->xa_offset++;
  1040. xas->xa_index++;
  1041. } while (!entry);
  1042. return entry;
  1043. }
  1044. /* Private */
  1045. static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
  1046. xa_mark_t mark)
  1047. {
  1048. unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
  1049. unsigned int offset = xas->xa_offset;
  1050. if (advance)
  1051. offset++;
  1052. if (XA_CHUNK_SIZE == BITS_PER_LONG) {
  1053. if (offset < XA_CHUNK_SIZE) {
  1054. unsigned long data = *addr & (~0UL << offset);
  1055. if (data)
  1056. return __ffs(data);
  1057. }
  1058. return XA_CHUNK_SIZE;
  1059. }
  1060. return find_next_bit(addr, XA_CHUNK_SIZE, offset);
  1061. }
  1062. /**
  1063. * xas_next_marked() - Advance iterator to next marked entry.
  1064. * @xas: XArray operation state.
  1065. * @max: Highest index to return.
  1066. * @mark: Mark to search for.
  1067. *
  1068. * xas_next_marked() is an inline function to optimise xarray traversal for
  1069. * speed. It is equivalent to calling xas_find_marked(), and will call
  1070. * xas_find_marked() for all the hard cases.
  1071. *
  1072. * Return: The next marked entry after the one currently referred to by @xas.
  1073. */
  1074. static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
  1075. xa_mark_t mark)
  1076. {
  1077. struct xa_node *node = xas->xa_node;
  1078. unsigned int offset;
  1079. if (unlikely(xas_not_node(node) || node->shift))
  1080. return xas_find_marked(xas, max, mark);
  1081. offset = xas_find_chunk(xas, true, mark);
  1082. xas->xa_offset = offset;
  1083. xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
  1084. if (xas->xa_index > max)
  1085. return NULL;
  1086. if (offset == XA_CHUNK_SIZE)
  1087. return xas_find_marked(xas, max, mark);
  1088. return xa_entry(xas->xa, node, offset);
  1089. }
  1090. /*
  1091. * If iterating while holding a lock, drop the lock and reschedule
  1092. * every %XA_CHECK_SCHED loops.
  1093. */
  1094. enum {
  1095. XA_CHECK_SCHED = 4096,
  1096. };
  1097. /**
  1098. * xas_for_each() - Iterate over a range of an XArray.
  1099. * @xas: XArray operation state.
  1100. * @entry: Entry retrieved from the array.
  1101. * @max: Maximum index to retrieve from array.
  1102. *
  1103. * The loop body will be executed for each entry present in the xarray
  1104. * between the current xas position and @max. @entry will be set to
  1105. * the entry retrieved from the xarray. It is safe to delete entries
  1106. * from the array in the loop body. You should hold either the RCU lock
  1107. * or the xa_lock while iterating. If you need to drop the lock, call
  1108. * xas_pause() first.
  1109. */
  1110. #define xas_for_each(xas, entry, max) \
  1111. for (entry = xas_find(xas, max); entry; \
  1112. entry = xas_next_entry(xas, max))
  1113. /**
  1114. * xas_for_each_marked() - Iterate over a range of an XArray.
  1115. * @xas: XArray operation state.
  1116. * @entry: Entry retrieved from the array.
  1117. * @max: Maximum index to retrieve from array.
  1118. * @mark: Mark to search for.
  1119. *
  1120. * The loop body will be executed for each marked entry in the xarray
  1121. * between the current xas position and @max. @entry will be set to
  1122. * the entry retrieved from the xarray. It is safe to delete entries
  1123. * from the array in the loop body. You should hold either the RCU lock
  1124. * or the xa_lock while iterating. If you need to drop the lock, call
  1125. * xas_pause() first.
  1126. */
  1127. #define xas_for_each_marked(xas, entry, max, mark) \
  1128. for (entry = xas_find_marked(xas, max, mark); entry; \
  1129. entry = xas_next_marked(xas, max, mark))
  1130. /**
  1131. * xas_for_each_conflict() - Iterate over a range of an XArray.
  1132. * @xas: XArray operation state.
  1133. * @entry: Entry retrieved from the array.
  1134. *
  1135. * The loop body will be executed for each entry in the XArray that lies
  1136. * within the range specified by @xas. If the loop completes successfully,
  1137. * any entries that lie in this range will be replaced by @entry. The caller
  1138. * may break out of the loop; if they do so, the contents of the XArray will
  1139. * be unchanged. The operation may fail due to an out of memory condition.
  1140. * The caller may also call xa_set_err() to exit the loop while setting an
  1141. * error to record the reason.
  1142. */
  1143. #define xas_for_each_conflict(xas, entry) \
  1144. while ((entry = xas_find_conflict(xas)))
  1145. void *__xas_next(struct xa_state *);
  1146. void *__xas_prev(struct xa_state *);
  1147. /**
  1148. * xas_prev() - Move iterator to previous index.
  1149. * @xas: XArray operation state.
  1150. *
  1151. * If the @xas was in an error state, it will remain in an error state
  1152. * and this function will return %NULL. If the @xas has never been walked,
  1153. * it will have the effect of calling xas_load(). Otherwise one will be
  1154. * subtracted from the index and the state will be walked to the correct
  1155. * location in the array for the next operation.
  1156. *
  1157. * If the iterator was referencing index 0, this function wraps
  1158. * around to %ULONG_MAX.
  1159. *
  1160. * Return: The entry at the new index. This may be %NULL or an internal
  1161. * entry.
  1162. */
  1163. static inline void *xas_prev(struct xa_state *xas)
  1164. {
  1165. struct xa_node *node = xas->xa_node;
  1166. if (unlikely(xas_not_node(node) || node->shift ||
  1167. xas->xa_offset == 0))
  1168. return __xas_prev(xas);
  1169. xas->xa_index--;
  1170. xas->xa_offset--;
  1171. return xa_entry(xas->xa, node, xas->xa_offset);
  1172. }
  1173. /**
  1174. * xas_next() - Move state to next index.
  1175. * @xas: XArray operation state.
  1176. *
  1177. * If the @xas was in an error state, it will remain in an error state
  1178. * and this function will return %NULL. If the @xas has never been walked,
  1179. * it will have the effect of calling xas_load(). Otherwise one will be
  1180. * added to the index and the state will be walked to the correct
  1181. * location in the array for the next operation.
  1182. *
  1183. * If the iterator was referencing index %ULONG_MAX, this function wraps
  1184. * around to 0.
  1185. *
  1186. * Return: The entry at the new index. This may be %NULL or an internal
  1187. * entry.
  1188. */
  1189. static inline void *xas_next(struct xa_state *xas)
  1190. {
  1191. struct xa_node *node = xas->xa_node;
  1192. if (unlikely(xas_not_node(node) || node->shift ||
  1193. xas->xa_offset == XA_CHUNK_MASK))
  1194. return __xas_next(xas);
  1195. xas->xa_index++;
  1196. xas->xa_offset++;
  1197. return xa_entry(xas->xa, node, xas->xa_offset);
  1198. }
  1199. #endif /* _LINUX_XARRAY_H */