xarray.h 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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 <[email protected]>
  8. *
  9. * See Documentation/core-api/xarray.rst for how to use the XArray.
  10. */
  11. #include <linux/bitmap.h>
  12. #include <linux/bug.h>
  13. #include <linux/compiler.h>
  14. #include <linux/gfp.h>
  15. #include <linux/kconfig.h>
  16. #include <linux/kernel.h>
  17. #include <linux/rcupdate.h>
  18. #include <linux/sched/mm.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/types.h>
  21. /*
  22. * The bottom two bits of the entry determine how the XArray interprets
  23. * the contents:
  24. *
  25. * 00: Pointer entry
  26. * 10: Internal entry
  27. * x1: Value entry or tagged pointer
  28. *
  29. * Attempting to store internal entries in the XArray is a bug.
  30. *
  31. * Most internal entries are pointers to the next node in the tree.
  32. * The following internal entries have a special meaning:
  33. *
  34. * 0-62: Sibling entries
  35. * 256: Retry entry
  36. * 257: Zero entry
  37. *
  38. * Errors are also represented as internal entries, but use the negative
  39. * space (-4094 to -2). They're never stored in the slots array; only
  40. * returned by the normal API.
  41. */
  42. #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
  43. /**
  44. * xa_mk_value() - Create an XArray entry from an integer.
  45. * @v: Value to store in XArray.
  46. *
  47. * Context: Any context.
  48. * Return: An entry suitable for storing in the XArray.
  49. */
  50. static inline void *xa_mk_value(unsigned long v)
  51. {
  52. WARN_ON((long)v < 0);
  53. return (void *)((v << 1) | 1);
  54. }
  55. /**
  56. * xa_to_value() - Get value stored in an XArray entry.
  57. * @entry: XArray entry.
  58. *
  59. * Context: Any context.
  60. * Return: The value stored in the XArray entry.
  61. */
  62. static inline unsigned long xa_to_value(const void *entry)
  63. {
  64. return (unsigned long)entry >> 1;
  65. }
  66. /**
  67. * xa_is_value() - Determine if an entry is a value.
  68. * @entry: XArray entry.
  69. *
  70. * Context: Any context.
  71. * Return: True if the entry is a value, false if it is a pointer.
  72. */
  73. static inline bool xa_is_value(const void *entry)
  74. {
  75. return (unsigned long)entry & 1;
  76. }
  77. /**
  78. * xa_tag_pointer() - Create an XArray entry for a tagged pointer.
  79. * @p: Plain pointer.
  80. * @tag: Tag value (0, 1 or 3).
  81. *
  82. * If the user of the XArray prefers, they can tag their pointers instead
  83. * of storing value entries. Three tags are available (0, 1 and 3).
  84. * These are distinct from the xa_mark_t as they are not replicated up
  85. * through the array and cannot be searched for.
  86. *
  87. * Context: Any context.
  88. * Return: An XArray entry.
  89. */
  90. static inline void *xa_tag_pointer(void *p, unsigned long tag)
  91. {
  92. return (void *)((unsigned long)p | tag);
  93. }
  94. /**
  95. * xa_untag_pointer() - Turn an XArray entry into a plain pointer.
  96. * @entry: XArray entry.
  97. *
  98. * If you have stored a tagged pointer in the XArray, call this function
  99. * to get the untagged version of the pointer.
  100. *
  101. * Context: Any context.
  102. * Return: A pointer.
  103. */
  104. static inline void *xa_untag_pointer(void *entry)
  105. {
  106. return (void *)((unsigned long)entry & ~3UL);
  107. }
  108. /**
  109. * xa_pointer_tag() - Get the tag stored in an XArray entry.
  110. * @entry: XArray entry.
  111. *
  112. * If you have stored a tagged pointer in the XArray, call this function
  113. * to get the tag of that pointer.
  114. *
  115. * Context: Any context.
  116. * Return: A tag.
  117. */
  118. static inline unsigned int xa_pointer_tag(void *entry)
  119. {
  120. return (unsigned long)entry & 3UL;
  121. }
  122. /*
  123. * xa_mk_internal() - Create an internal entry.
  124. * @v: Value to turn into an internal entry.
  125. *
  126. * Internal entries are used for a number of purposes. Entries 0-255 are
  127. * used for sibling entries (only 0-62 are used by the current code). 256
  128. * is used for the retry entry. 257 is used for the reserved / zero entry.
  129. * Negative internal entries are used to represent errnos. Node pointers
  130. * are also tagged as internal entries in some situations.
  131. *
  132. * Context: Any context.
  133. * Return: An XArray internal entry corresponding to this value.
  134. */
  135. static inline void *xa_mk_internal(unsigned long v)
  136. {
  137. return (void *)((v << 2) | 2);
  138. }
  139. /*
  140. * xa_to_internal() - Extract the value from an internal entry.
  141. * @entry: XArray entry.
  142. *
  143. * Context: Any context.
  144. * Return: The value which was stored in the internal entry.
  145. */
  146. static inline unsigned long xa_to_internal(const void *entry)
  147. {
  148. return (unsigned long)entry >> 2;
  149. }
  150. /*
  151. * xa_is_internal() - Is the entry an internal entry?
  152. * @entry: XArray entry.
  153. *
  154. * Context: Any context.
  155. * Return: %true if the entry is an internal entry.
  156. */
  157. static inline bool xa_is_internal(const void *entry)
  158. {
  159. return ((unsigned long)entry & 3) == 2;
  160. }
  161. #define XA_ZERO_ENTRY xa_mk_internal(257)
  162. /**
  163. * xa_is_zero() - Is the entry a zero entry?
  164. * @entry: Entry retrieved from the XArray
  165. *
  166. * The normal API will return NULL as the contents of a slot containing
  167. * a zero entry. You can only see zero entries by using the advanced API.
  168. *
  169. * Return: %true if the entry is a zero entry.
  170. */
  171. static inline bool xa_is_zero(const void *entry)
  172. {
  173. return unlikely(entry == XA_ZERO_ENTRY);
  174. }
  175. /**
  176. * xa_is_err() - Report whether an XArray operation returned an error
  177. * @entry: Result from calling an XArray function
  178. *
  179. * If an XArray operation cannot complete an operation, it will return
  180. * a special value indicating an error. This function tells you
  181. * whether an error occurred; xa_err() tells you which error occurred.
  182. *
  183. * Context: Any context.
  184. * Return: %true if the entry indicates an error.
  185. */
  186. static inline bool xa_is_err(const void *entry)
  187. {
  188. return unlikely(xa_is_internal(entry) &&
  189. entry >= xa_mk_internal(-MAX_ERRNO));
  190. }
  191. /**
  192. * xa_err() - Turn an XArray result into an errno.
  193. * @entry: Result from calling an XArray function.
  194. *
  195. * If an XArray operation cannot complete an operation, it will return
  196. * a special pointer value which encodes an errno. This function extracts
  197. * the errno from the pointer value, or returns 0 if the pointer does not
  198. * represent an errno.
  199. *
  200. * Context: Any context.
  201. * Return: A negative errno or 0.
  202. */
  203. static inline int xa_err(void *entry)
  204. {
  205. /* xa_to_internal() would not do sign extension. */
  206. if (xa_is_err(entry))
  207. return (long)entry >> 2;
  208. return 0;
  209. }
  210. /**
  211. * struct xa_limit - Represents a range of IDs.
  212. * @min: The lowest ID to allocate (inclusive).
  213. * @max: The maximum ID to allocate (inclusive).
  214. *
  215. * This structure is used either directly or via the XA_LIMIT() macro
  216. * to communicate the range of IDs that are valid for allocation.
  217. * Three common ranges are predefined for you:
  218. * * xa_limit_32b - [0 - UINT_MAX]
  219. * * xa_limit_31b - [0 - INT_MAX]
  220. * * xa_limit_16b - [0 - USHRT_MAX]
  221. */
  222. struct xa_limit {
  223. u32 max;
  224. u32 min;
  225. };
  226. #define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max }
  227. #define xa_limit_32b XA_LIMIT(0, UINT_MAX)
  228. #define xa_limit_31b XA_LIMIT(0, INT_MAX)
  229. #define xa_limit_16b XA_LIMIT(0, USHRT_MAX)
  230. typedef unsigned __bitwise xa_mark_t;
  231. #define XA_MARK_0 ((__force xa_mark_t)0U)
  232. #define XA_MARK_1 ((__force xa_mark_t)1U)
  233. #define XA_MARK_2 ((__force xa_mark_t)2U)
  234. #define XA_PRESENT ((__force xa_mark_t)8U)
  235. #define XA_MARK_MAX XA_MARK_2
  236. #define XA_FREE_MARK XA_MARK_0
  237. enum xa_lock_type {
  238. XA_LOCK_IRQ = 1,
  239. XA_LOCK_BH = 2,
  240. };
  241. /*
  242. * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
  243. * and we remain compatible with that.
  244. */
  245. #define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
  246. #define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
  247. #define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U)
  248. #define XA_FLAGS_ZERO_BUSY ((__force gfp_t)8U)
  249. #define XA_FLAGS_ALLOC_WRAPPED ((__force gfp_t)16U)
  250. #define XA_FLAGS_ACCOUNT ((__force gfp_t)32U)
  251. #define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
  252. (__force unsigned)(mark)))
  253. /* ALLOC is for a normal 0-based alloc. ALLOC1 is for an 1-based alloc */
  254. #define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
  255. #define XA_FLAGS_ALLOC1 (XA_FLAGS_TRACK_FREE | XA_FLAGS_ZERO_BUSY)
  256. /**
  257. * struct xarray - The anchor of the XArray.
  258. * @xa_lock: Lock that protects the contents of the XArray.
  259. *
  260. * To use the xarray, define it statically or embed it in your data structure.
  261. * It is a very small data structure, so it does not usually make sense to
  262. * allocate it separately and keep a pointer to it in your data structure.
  263. *
  264. * You may use the xa_lock to protect your own data structures as well.
  265. */
  266. /*
  267. * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
  268. * If the only non-NULL entry in the array is at index 0, @xa_head is that
  269. * entry. If any other entry in the array is non-NULL, @xa_head points
  270. * to an @xa_node.
  271. */
  272. struct xarray {
  273. spinlock_t xa_lock;
  274. /* private: The rest of the data structure is not to be used directly. */
  275. gfp_t xa_flags;
  276. void __rcu * xa_head;
  277. };
  278. #define XARRAY_INIT(name, flags) { \
  279. .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
  280. .xa_flags = flags, \
  281. .xa_head = NULL, \
  282. }
  283. /**
  284. * DEFINE_XARRAY_FLAGS() - Define an XArray with custom flags.
  285. * @name: A string that names your XArray.
  286. * @flags: XA_FLAG values.
  287. *
  288. * This is intended for file scope definitions of XArrays. It declares
  289. * and initialises an empty XArray with the chosen name and flags. It is
  290. * equivalent to calling xa_init_flags() on the array, but it does the
  291. * initialisation at compiletime instead of runtime.
  292. */
  293. #define DEFINE_XARRAY_FLAGS(name, flags) \
  294. struct xarray name = XARRAY_INIT(name, flags)
  295. /**
  296. * DEFINE_XARRAY() - Define an XArray.
  297. * @name: A string that names your XArray.
  298. *
  299. * This is intended for file scope definitions of XArrays. It declares
  300. * and initialises an empty XArray with the chosen name. It is equivalent
  301. * to calling xa_init() on the array, but it does the initialisation at
  302. * compiletime instead of runtime.
  303. */
  304. #define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
  305. /**
  306. * DEFINE_XARRAY_ALLOC() - Define an XArray which allocates IDs starting at 0.
  307. * @name: A string that names your XArray.
  308. *
  309. * This is intended for file scope definitions of allocating XArrays.
  310. * See also DEFINE_XARRAY().
  311. */
  312. #define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
  313. /**
  314. * DEFINE_XARRAY_ALLOC1() - Define an XArray which allocates IDs starting at 1.
  315. * @name: A string that names your XArray.
  316. *
  317. * This is intended for file scope definitions of allocating XArrays.
  318. * See also DEFINE_XARRAY().
  319. */
  320. #define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1)
  321. void *xa_load(struct xarray *, unsigned long index);
  322. void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
  323. void *xa_erase(struct xarray *, unsigned long index);
  324. void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
  325. void *entry, gfp_t);
  326. bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
  327. void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
  328. void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
  329. void *xa_find(struct xarray *xa, unsigned long *index,
  330. unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
  331. void *xa_find_after(struct xarray *xa, unsigned long *index,
  332. unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
  333. unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
  334. unsigned long max, unsigned int n, xa_mark_t);
  335. void xa_destroy(struct xarray *);
  336. /**
  337. * xa_init_flags() - Initialise an empty XArray with flags.
  338. * @xa: XArray.
  339. * @flags: XA_FLAG values.
  340. *
  341. * If you need to initialise an XArray with special flags (eg you need
  342. * to take the lock from interrupt context), use this function instead
  343. * of xa_init().
  344. *
  345. * Context: Any context.
  346. */
  347. static inline void xa_init_flags(struct xarray *xa, gfp_t flags)
  348. {
  349. spin_lock_init(&xa->xa_lock);
  350. xa->xa_flags = flags;
  351. xa->xa_head = NULL;
  352. }
  353. /**
  354. * xa_init() - Initialise an empty XArray.
  355. * @xa: XArray.
  356. *
  357. * An empty XArray is full of NULL entries.
  358. *
  359. * Context: Any context.
  360. */
  361. static inline void xa_init(struct xarray *xa)
  362. {
  363. xa_init_flags(xa, 0);
  364. }
  365. /**
  366. * xa_empty() - Determine if an array has any present entries.
  367. * @xa: XArray.
  368. *
  369. * Context: Any context.
  370. * Return: %true if the array contains only NULL pointers.
  371. */
  372. static inline bool xa_empty(const struct xarray *xa)
  373. {
  374. return xa->xa_head == NULL;
  375. }
  376. /**
  377. * xa_marked() - Inquire whether any entry in this array has a mark set
  378. * @xa: Array
  379. * @mark: Mark value
  380. *
  381. * Context: Any context.
  382. * Return: %true if any entry has this mark set.
  383. */
  384. static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
  385. {
  386. return xa->xa_flags & XA_FLAGS_MARK(mark);
  387. }
  388. /**
  389. * xa_for_each_range() - Iterate over a portion of an XArray.
  390. * @xa: XArray.
  391. * @index: Index of @entry.
  392. * @entry: Entry retrieved from array.
  393. * @start: First index to retrieve from array.
  394. * @last: Last index to retrieve from array.
  395. *
  396. * During the iteration, @entry will have the value of the entry stored
  397. * in @xa at @index. You may modify @index during the iteration if you
  398. * want to skip or reprocess indices. It is safe to modify the array
  399. * during the iteration. At the end of the iteration, @entry will be set
  400. * to NULL and @index will have a value less than or equal to max.
  401. *
  402. * xa_for_each_range() is O(n.log(n)) while xas_for_each() is O(n). You have
  403. * to handle your own locking with xas_for_each(), and if you have to unlock
  404. * after each iteration, it will also end up being O(n.log(n)).
  405. * xa_for_each_range() will spin if it hits a retry entry; if you intend to
  406. * see retry entries, you should use the xas_for_each() iterator instead.
  407. * The xas_for_each() iterator will expand into more inline code than
  408. * xa_for_each_range().
  409. *
  410. * Context: Any context. Takes and releases the RCU lock.
  411. */
  412. #define xa_for_each_range(xa, index, entry, start, last) \
  413. for (index = start, \
  414. entry = xa_find(xa, &index, last, XA_PRESENT); \
  415. entry; \
  416. entry = xa_find_after(xa, &index, last, XA_PRESENT))
  417. /**
  418. * xa_for_each_start() - Iterate over a portion of an XArray.
  419. * @xa: XArray.
  420. * @index: Index of @entry.
  421. * @entry: Entry retrieved from array.
  422. * @start: First index to retrieve from array.
  423. *
  424. * During the iteration, @entry will have the value of the entry stored
  425. * in @xa at @index. You may modify @index during the iteration if you
  426. * want to skip or reprocess indices. It is safe to modify the array
  427. * during the iteration. At the end of the iteration, @entry will be set
  428. * to NULL and @index will have a value less than or equal to max.
  429. *
  430. * xa_for_each_start() is O(n.log(n)) while xas_for_each() is O(n). You have
  431. * to handle your own locking with xas_for_each(), and if you have to unlock
  432. * after each iteration, it will also end up being O(n.log(n)).
  433. * xa_for_each_start() will spin if it hits a retry entry; if you intend to
  434. * see retry entries, you should use the xas_for_each() iterator instead.
  435. * The xas_for_each() iterator will expand into more inline code than
  436. * xa_for_each_start().
  437. *
  438. * Context: Any context. Takes and releases the RCU lock.
  439. */
  440. #define xa_for_each_start(xa, index, entry, start) \
  441. xa_for_each_range(xa, index, entry, start, ULONG_MAX)
  442. /**
  443. * xa_for_each() - Iterate over present entries in an XArray.
  444. * @xa: XArray.
  445. * @index: Index of @entry.
  446. * @entry: Entry retrieved from array.
  447. *
  448. * During the iteration, @entry will have the value of the entry stored
  449. * in @xa at @index. You may modify @index during the iteration if you want
  450. * to skip or reprocess indices. It is safe to modify the array during the
  451. * iteration. At the end of the iteration, @entry will be set to NULL and
  452. * @index will have a value less than or equal to max.
  453. *
  454. * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
  455. * to handle your own locking with xas_for_each(), and if you have to unlock
  456. * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
  457. * will spin if it hits a retry entry; if you intend to see retry entries,
  458. * you should use the xas_for_each() iterator instead. The xas_for_each()
  459. * iterator will expand into more inline code than xa_for_each().
  460. *
  461. * Context: Any context. Takes and releases the RCU lock.
  462. */
  463. #define xa_for_each(xa, index, entry) \
  464. xa_for_each_start(xa, index, entry, 0)
  465. /**
  466. * xa_for_each_marked() - Iterate over marked entries in an XArray.
  467. * @xa: XArray.
  468. * @index: Index of @entry.
  469. * @entry: Entry retrieved from array.
  470. * @filter: Selection criterion.
  471. *
  472. * During the iteration, @entry will have the value of the entry stored
  473. * in @xa at @index. The iteration will skip all entries in the array
  474. * which do not match @filter. You may modify @index during the iteration
  475. * if you want to skip or reprocess indices. It is safe to modify the array
  476. * during the iteration. At the end of the iteration, @entry will be set to
  477. * NULL and @index will have a value less than or equal to max.
  478. *
  479. * xa_for_each_marked() is O(n.log(n)) while xas_for_each_marked() is O(n).
  480. * You have to handle your own locking with xas_for_each(), and if you have
  481. * to unlock after each iteration, it will also end up being O(n.log(n)).
  482. * xa_for_each_marked() will spin if it hits a retry entry; if you intend to
  483. * see retry entries, you should use the xas_for_each_marked() iterator
  484. * instead. The xas_for_each_marked() iterator will expand into more inline
  485. * code than xa_for_each_marked().
  486. *
  487. * Context: Any context. Takes and releases the RCU lock.
  488. */
  489. #define xa_for_each_marked(xa, index, entry, filter) \
  490. for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
  491. entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter))
  492. #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
  493. #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
  494. #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
  495. #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
  496. #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
  497. #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
  498. #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
  499. #define xa_lock_irqsave(xa, flags) \
  500. spin_lock_irqsave(&(xa)->xa_lock, flags)
  501. #define xa_unlock_irqrestore(xa, flags) \
  502. spin_unlock_irqrestore(&(xa)->xa_lock, flags)
  503. #define xa_lock_nested(xa, subclass) \
  504. spin_lock_nested(&(xa)->xa_lock, subclass)
  505. #define xa_lock_bh_nested(xa, subclass) \
  506. spin_lock_bh_nested(&(xa)->xa_lock, subclass)
  507. #define xa_lock_irq_nested(xa, subclass) \
  508. spin_lock_irq_nested(&(xa)->xa_lock, subclass)
  509. #define xa_lock_irqsave_nested(xa, flags, subclass) \
  510. spin_lock_irqsave_nested(&(xa)->xa_lock, flags, subclass)
  511. /*
  512. * Versions of the normal API which require the caller to hold the
  513. * xa_lock. If the GFP flags allow it, they will drop the lock to
  514. * allocate memory, then reacquire it afterwards. These functions
  515. * may also re-enable interrupts if the XArray flags indicate the
  516. * locking should be interrupt safe.
  517. */
  518. void *__xa_erase(struct xarray *, unsigned long index);
  519. void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
  520. void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
  521. void *entry, gfp_t);
  522. int __must_check __xa_insert(struct xarray *, unsigned long index,
  523. void *entry, gfp_t);
  524. int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry,
  525. struct xa_limit, gfp_t);
  526. int __must_check __xa_alloc_cyclic(struct xarray *, u32 *id, void *entry,
  527. struct xa_limit, u32 *next, gfp_t);
  528. void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
  529. void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
  530. /**
  531. * xa_store_bh() - Store this entry in the XArray.
  532. * @xa: XArray.
  533. * @index: Index into array.
  534. * @entry: New entry.
  535. * @gfp: Memory allocation flags.
  536. *
  537. * This function is like calling xa_store() except it disables softirqs
  538. * while holding the array lock.
  539. *
  540. * Context: Any context. Takes and releases the xa_lock while
  541. * disabling softirqs.
  542. * Return: The old entry at this index or xa_err() if an error happened.
  543. */
  544. static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
  545. void *entry, gfp_t gfp)
  546. {
  547. void *curr;
  548. might_alloc(gfp);
  549. xa_lock_bh(xa);
  550. curr = __xa_store(xa, index, entry, gfp);
  551. xa_unlock_bh(xa);
  552. return curr;
  553. }
  554. /**
  555. * xa_store_irq() - Store this entry in the XArray.
  556. * @xa: XArray.
  557. * @index: Index into array.
  558. * @entry: New entry.
  559. * @gfp: Memory allocation flags.
  560. *
  561. * This function is like calling xa_store() except it disables interrupts
  562. * while holding the array lock.
  563. *
  564. * Context: Process context. Takes and releases the xa_lock while
  565. * disabling interrupts.
  566. * Return: The old entry at this index or xa_err() if an error happened.
  567. */
  568. static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
  569. void *entry, gfp_t gfp)
  570. {
  571. void *curr;
  572. might_alloc(gfp);
  573. xa_lock_irq(xa);
  574. curr = __xa_store(xa, index, entry, gfp);
  575. xa_unlock_irq(xa);
  576. return curr;
  577. }
  578. /**
  579. * xa_erase_bh() - Erase this entry from the XArray.
  580. * @xa: XArray.
  581. * @index: Index of entry.
  582. *
  583. * After this function returns, loading from @index will return %NULL.
  584. * If the index is part of a multi-index entry, all indices will be erased
  585. * and none of the entries will be part of a multi-index entry.
  586. *
  587. * Context: Any context. Takes and releases the xa_lock while
  588. * disabling softirqs.
  589. * Return: The entry which used to be at this index.
  590. */
  591. static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
  592. {
  593. void *entry;
  594. xa_lock_bh(xa);
  595. entry = __xa_erase(xa, index);
  596. xa_unlock_bh(xa);
  597. return entry;
  598. }
  599. /**
  600. * xa_erase_irq() - Erase this entry from the XArray.
  601. * @xa: XArray.
  602. * @index: Index of entry.
  603. *
  604. * After this function returns, loading from @index will return %NULL.
  605. * If the index is part of a multi-index entry, all indices will be erased
  606. * and none of the entries will be part of a multi-index entry.
  607. *
  608. * Context: Process context. Takes and releases the xa_lock while
  609. * disabling interrupts.
  610. * Return: The entry which used to be at this index.
  611. */
  612. static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
  613. {
  614. void *entry;
  615. xa_lock_irq(xa);
  616. entry = __xa_erase(xa, index);
  617. xa_unlock_irq(xa);
  618. return entry;
  619. }
  620. /**
  621. * xa_cmpxchg() - Conditionally replace an entry in the XArray.
  622. * @xa: XArray.
  623. * @index: Index into array.
  624. * @old: Old value to test against.
  625. * @entry: New value to place in array.
  626. * @gfp: Memory allocation flags.
  627. *
  628. * If the entry at @index is the same as @old, replace it with @entry.
  629. * If the return value is equal to @old, then the exchange was successful.
  630. *
  631. * Context: Any context. Takes and releases the xa_lock. May sleep
  632. * if the @gfp flags permit.
  633. * Return: The old value at this index or xa_err() if an error happened.
  634. */
  635. static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
  636. void *old, void *entry, gfp_t gfp)
  637. {
  638. void *curr;
  639. might_alloc(gfp);
  640. xa_lock(xa);
  641. curr = __xa_cmpxchg(xa, index, old, entry, gfp);
  642. xa_unlock(xa);
  643. return curr;
  644. }
  645. /**
  646. * xa_cmpxchg_bh() - Conditionally replace an entry in the XArray.
  647. * @xa: XArray.
  648. * @index: Index into array.
  649. * @old: Old value to test against.
  650. * @entry: New value to place in array.
  651. * @gfp: Memory allocation flags.
  652. *
  653. * This function is like calling xa_cmpxchg() except it disables softirqs
  654. * while holding the array lock.
  655. *
  656. * Context: Any context. Takes and releases the xa_lock while
  657. * disabling softirqs. May sleep if the @gfp flags permit.
  658. * Return: The old value at this index or xa_err() if an error happened.
  659. */
  660. static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
  661. void *old, void *entry, gfp_t gfp)
  662. {
  663. void *curr;
  664. might_alloc(gfp);
  665. xa_lock_bh(xa);
  666. curr = __xa_cmpxchg(xa, index, old, entry, gfp);
  667. xa_unlock_bh(xa);
  668. return curr;
  669. }
  670. /**
  671. * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
  672. * @xa: XArray.
  673. * @index: Index into array.
  674. * @old: Old value to test against.
  675. * @entry: New value to place in array.
  676. * @gfp: Memory allocation flags.
  677. *
  678. * This function is like calling xa_cmpxchg() except it disables interrupts
  679. * while holding the array lock.
  680. *
  681. * Context: Process context. Takes and releases the xa_lock while
  682. * disabling interrupts. May sleep if the @gfp flags permit.
  683. * Return: The old value at this index or xa_err() if an error happened.
  684. */
  685. static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
  686. void *old, void *entry, gfp_t gfp)
  687. {
  688. void *curr;
  689. might_alloc(gfp);
  690. xa_lock_irq(xa);
  691. curr = __xa_cmpxchg(xa, index, old, entry, gfp);
  692. xa_unlock_irq(xa);
  693. return curr;
  694. }
  695. /**
  696. * xa_insert() - Store this entry in the XArray unless another entry is
  697. * already present.
  698. * @xa: XArray.
  699. * @index: Index into array.
  700. * @entry: New entry.
  701. * @gfp: Memory allocation flags.
  702. *
  703. * Inserting a NULL entry will store a reserved entry (like xa_reserve())
  704. * if no entry is present. Inserting will fail if a reserved entry is
  705. * present, even though loading from this index will return NULL.
  706. *
  707. * Context: Any context. Takes and releases the xa_lock. May sleep if
  708. * the @gfp flags permit.
  709. * Return: 0 if the store succeeded. -EBUSY if another entry was present.
  710. * -ENOMEM if memory could not be allocated.
  711. */
  712. static inline int __must_check xa_insert(struct xarray *xa,
  713. unsigned long index, void *entry, gfp_t gfp)
  714. {
  715. int err;
  716. might_alloc(gfp);
  717. xa_lock(xa);
  718. err = __xa_insert(xa, index, entry, gfp);
  719. xa_unlock(xa);
  720. return err;
  721. }
  722. /**
  723. * xa_insert_bh() - Store this entry in the XArray unless another entry is
  724. * already present.
  725. * @xa: XArray.
  726. * @index: Index into array.
  727. * @entry: New entry.
  728. * @gfp: Memory allocation flags.
  729. *
  730. * Inserting a NULL entry will store a reserved entry (like xa_reserve())
  731. * if no entry is present. Inserting will fail if a reserved entry is
  732. * present, even though loading from this index will return NULL.
  733. *
  734. * Context: Any context. Takes and releases the xa_lock while
  735. * disabling softirqs. May sleep if the @gfp flags permit.
  736. * Return: 0 if the store succeeded. -EBUSY if another entry was present.
  737. * -ENOMEM if memory could not be allocated.
  738. */
  739. static inline int __must_check xa_insert_bh(struct xarray *xa,
  740. unsigned long index, void *entry, gfp_t gfp)
  741. {
  742. int err;
  743. might_alloc(gfp);
  744. xa_lock_bh(xa);
  745. err = __xa_insert(xa, index, entry, gfp);
  746. xa_unlock_bh(xa);
  747. return err;
  748. }
  749. /**
  750. * xa_insert_irq() - Store this entry in the XArray unless another entry is
  751. * already present.
  752. * @xa: XArray.
  753. * @index: Index into array.
  754. * @entry: New entry.
  755. * @gfp: Memory allocation flags.
  756. *
  757. * Inserting a NULL entry will store a reserved entry (like xa_reserve())
  758. * if no entry is present. Inserting will fail if a reserved entry is
  759. * present, even though loading from this index will return NULL.
  760. *
  761. * Context: Process context. Takes and releases the xa_lock while
  762. * disabling interrupts. May sleep if the @gfp flags permit.
  763. * Return: 0 if the store succeeded. -EBUSY if another entry was present.
  764. * -ENOMEM if memory could not be allocated.
  765. */
  766. static inline int __must_check xa_insert_irq(struct xarray *xa,
  767. unsigned long index, void *entry, gfp_t gfp)
  768. {
  769. int err;
  770. might_alloc(gfp);
  771. xa_lock_irq(xa);
  772. err = __xa_insert(xa, index, entry, gfp);
  773. xa_unlock_irq(xa);
  774. return err;
  775. }
  776. /**
  777. * xa_alloc() - Find somewhere to store this entry in the XArray.
  778. * @xa: XArray.
  779. * @id: Pointer to ID.
  780. * @entry: New entry.
  781. * @limit: Range of ID to allocate.
  782. * @gfp: Memory allocation flags.
  783. *
  784. * Finds an empty entry in @xa between @limit.min and @limit.max,
  785. * stores the index into the @id pointer, then stores the entry at
  786. * that index. A concurrent lookup will not see an uninitialised @id.
  787. *
  788. * Context: Any context. Takes and releases the xa_lock. May sleep if
  789. * the @gfp flags permit.
  790. * Return: 0 on success, -ENOMEM if memory could not be allocated or
  791. * -EBUSY if there are no free entries in @limit.
  792. */
  793. static inline __must_check int xa_alloc(struct xarray *xa, u32 *id,
  794. void *entry, struct xa_limit limit, gfp_t gfp)
  795. {
  796. int err;
  797. might_alloc(gfp);
  798. xa_lock(xa);
  799. err = __xa_alloc(xa, id, entry, limit, gfp);
  800. xa_unlock(xa);
  801. return err;
  802. }
  803. /**
  804. * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
  805. * @xa: XArray.
  806. * @id: Pointer to ID.
  807. * @entry: New entry.
  808. * @limit: Range of ID to allocate.
  809. * @gfp: Memory allocation flags.
  810. *
  811. * Finds an empty entry in @xa between @limit.min and @limit.max,
  812. * stores the index into the @id pointer, then stores the entry at
  813. * that index. A concurrent lookup will not see an uninitialised @id.
  814. *
  815. * Context: Any context. Takes and releases the xa_lock while
  816. * disabling softirqs. May sleep if the @gfp flags permit.
  817. * Return: 0 on success, -ENOMEM if memory could not be allocated or
  818. * -EBUSY if there are no free entries in @limit.
  819. */
  820. static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id,
  821. void *entry, struct xa_limit limit, gfp_t gfp)
  822. {
  823. int err;
  824. might_alloc(gfp);
  825. xa_lock_bh(xa);
  826. err = __xa_alloc(xa, id, entry, limit, gfp);
  827. xa_unlock_bh(xa);
  828. return err;
  829. }
  830. /**
  831. * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
  832. * @xa: XArray.
  833. * @id: Pointer to ID.
  834. * @entry: New entry.
  835. * @limit: Range of ID to allocate.
  836. * @gfp: Memory allocation flags.
  837. *
  838. * Finds an empty entry in @xa between @limit.min and @limit.max,
  839. * stores the index into the @id pointer, then stores the entry at
  840. * that index. A concurrent lookup will not see an uninitialised @id.
  841. *
  842. * Context: Process context. Takes and releases the xa_lock while
  843. * disabling interrupts. May sleep if the @gfp flags permit.
  844. * Return: 0 on success, -ENOMEM if memory could not be allocated or
  845. * -EBUSY if there are no free entries in @limit.
  846. */
  847. static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id,
  848. void *entry, struct xa_limit limit, gfp_t gfp)
  849. {
  850. int err;
  851. might_alloc(gfp);
  852. xa_lock_irq(xa);
  853. err = __xa_alloc(xa, id, entry, limit, gfp);
  854. xa_unlock_irq(xa);
  855. return err;
  856. }
  857. /**
  858. * xa_alloc_cyclic() - Find somewhere to store this entry in the XArray.
  859. * @xa: XArray.
  860. * @id: Pointer to ID.
  861. * @entry: New entry.
  862. * @limit: Range of allocated ID.
  863. * @next: Pointer to next ID to allocate.
  864. * @gfp: Memory allocation flags.
  865. *
  866. * Finds an empty entry in @xa between @limit.min and @limit.max,
  867. * stores the index into the @id pointer, then stores the entry at
  868. * that index. A concurrent lookup will not see an uninitialised @id.
  869. * The search for an empty entry will start at @next and will wrap
  870. * around if necessary.
  871. *
  872. * Context: Any context. Takes and releases the xa_lock. May sleep if
  873. * the @gfp flags permit.
  874. * Return: 0 if the allocation succeeded without wrapping. 1 if the
  875. * allocation succeeded after wrapping, -ENOMEM if memory could not be
  876. * allocated or -EBUSY if there are no free entries in @limit.
  877. */
  878. static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
  879. struct xa_limit limit, u32 *next, gfp_t gfp)
  880. {
  881. int err;
  882. might_alloc(gfp);
  883. xa_lock(xa);
  884. err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
  885. xa_unlock(xa);
  886. return err;
  887. }
  888. /**
  889. * xa_alloc_cyclic_bh() - Find somewhere to store this entry in the XArray.
  890. * @xa: XArray.
  891. * @id: Pointer to ID.
  892. * @entry: New entry.
  893. * @limit: Range of allocated ID.
  894. * @next: Pointer to next ID to allocate.
  895. * @gfp: Memory allocation flags.
  896. *
  897. * Finds an empty entry in @xa between @limit.min and @limit.max,
  898. * stores the index into the @id pointer, then stores the entry at
  899. * that index. A concurrent lookup will not see an uninitialised @id.
  900. * The search for an empty entry will start at @next and will wrap
  901. * around if necessary.
  902. *
  903. * Context: Any context. Takes and releases the xa_lock while
  904. * disabling softirqs. May sleep if the @gfp flags permit.
  905. * Return: 0 if the allocation succeeded without wrapping. 1 if the
  906. * allocation succeeded after wrapping, -ENOMEM if memory could not be
  907. * allocated or -EBUSY if there are no free entries in @limit.
  908. */
  909. static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry,
  910. struct xa_limit limit, u32 *next, gfp_t gfp)
  911. {
  912. int err;
  913. might_alloc(gfp);
  914. xa_lock_bh(xa);
  915. err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
  916. xa_unlock_bh(xa);
  917. return err;
  918. }
  919. /**
  920. * xa_alloc_cyclic_irq() - Find somewhere to store this entry in the XArray.
  921. * @xa: XArray.
  922. * @id: Pointer to ID.
  923. * @entry: New entry.
  924. * @limit: Range of allocated ID.
  925. * @next: Pointer to next ID to allocate.
  926. * @gfp: Memory allocation flags.
  927. *
  928. * Finds an empty entry in @xa between @limit.min and @limit.max,
  929. * stores the index into the @id pointer, then stores the entry at
  930. * that index. A concurrent lookup will not see an uninitialised @id.
  931. * The search for an empty entry will start at @next and will wrap
  932. * around if necessary.
  933. *
  934. * Context: Process context. Takes and releases the xa_lock while
  935. * disabling interrupts. May sleep if the @gfp flags permit.
  936. * Return: 0 if the allocation succeeded without wrapping. 1 if the
  937. * allocation succeeded after wrapping, -ENOMEM if memory could not be
  938. * allocated or -EBUSY if there are no free entries in @limit.
  939. */
  940. static inline int xa_alloc_cyclic_irq(struct xarray *xa, u32 *id, void *entry,
  941. struct xa_limit limit, u32 *next, gfp_t gfp)
  942. {
  943. int err;
  944. might_alloc(gfp);
  945. xa_lock_irq(xa);
  946. err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
  947. xa_unlock_irq(xa);
  948. return err;
  949. }
  950. /**
  951. * xa_reserve() - Reserve this index in the XArray.
  952. * @xa: XArray.
  953. * @index: Index into array.
  954. * @gfp: Memory allocation flags.
  955. *
  956. * Ensures there is somewhere to store an entry at @index in the array.
  957. * If there is already something stored at @index, this function does
  958. * nothing. If there was nothing there, the entry is marked as reserved.
  959. * Loading from a reserved entry returns a %NULL pointer.
  960. *
  961. * If you do not use the entry that you have reserved, call xa_release()
  962. * or xa_erase() to free any unnecessary memory.
  963. *
  964. * Context: Any context. Takes and releases the xa_lock.
  965. * May sleep if the @gfp flags permit.
  966. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  967. */
  968. static inline __must_check
  969. int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
  970. {
  971. return xa_err(xa_cmpxchg(xa, index, NULL, XA_ZERO_ENTRY, gfp));
  972. }
  973. /**
  974. * xa_reserve_bh() - Reserve this index in the XArray.
  975. * @xa: XArray.
  976. * @index: Index into array.
  977. * @gfp: Memory allocation flags.
  978. *
  979. * A softirq-disabling version of xa_reserve().
  980. *
  981. * Context: Any context. Takes and releases the xa_lock while
  982. * disabling softirqs.
  983. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  984. */
  985. static inline __must_check
  986. int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
  987. {
  988. return xa_err(xa_cmpxchg_bh(xa, index, NULL, XA_ZERO_ENTRY, gfp));
  989. }
  990. /**
  991. * xa_reserve_irq() - Reserve this index in the XArray.
  992. * @xa: XArray.
  993. * @index: Index into array.
  994. * @gfp: Memory allocation flags.
  995. *
  996. * An interrupt-disabling version of xa_reserve().
  997. *
  998. * Context: Process context. Takes and releases the xa_lock while
  999. * disabling interrupts.
  1000. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  1001. */
  1002. static inline __must_check
  1003. int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
  1004. {
  1005. return xa_err(xa_cmpxchg_irq(xa, index, NULL, XA_ZERO_ENTRY, gfp));
  1006. }
  1007. /**
  1008. * xa_release() - Release a reserved entry.
  1009. * @xa: XArray.
  1010. * @index: Index of entry.
  1011. *
  1012. * After calling xa_reserve(), you can call this function to release the
  1013. * reservation. If the entry at @index has been stored to, this function
  1014. * will do nothing.
  1015. */
  1016. static inline void xa_release(struct xarray *xa, unsigned long index)
  1017. {
  1018. xa_cmpxchg(xa, index, XA_ZERO_ENTRY, NULL, 0);
  1019. }
  1020. /* Everything below here is the Advanced API. Proceed with caution. */
  1021. /*
  1022. * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
  1023. * the best chunk size requires some tradeoffs. A power of two recommends
  1024. * itself so that we can walk the tree based purely on shifts and masks.
  1025. * Generally, the larger the better; as the number of slots per level of the
  1026. * tree increases, the less tall the tree needs to be. But that needs to be
  1027. * balanced against the memory consumption of each node. On a 64-bit system,
  1028. * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
  1029. * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
  1030. */
  1031. #ifndef XA_CHUNK_SHIFT
  1032. #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  1033. #endif
  1034. #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
  1035. #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
  1036. #define XA_MAX_MARKS 3
  1037. #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
  1038. /*
  1039. * @count is the count of every non-NULL element in the ->slots array
  1040. * whether that is a value entry, a retry entry, a user pointer,
  1041. * a sibling entry or a pointer to the next level of the tree.
  1042. * @nr_values is the count of every element in ->slots which is
  1043. * either a value entry or a sibling of a value entry.
  1044. */
  1045. struct xa_node {
  1046. unsigned char shift; /* Bits remaining in each slot */
  1047. unsigned char offset; /* Slot offset in parent */
  1048. unsigned char count; /* Total entry count */
  1049. unsigned char nr_values; /* Value entry count */
  1050. struct xa_node __rcu *parent; /* NULL at top of tree */
  1051. struct xarray *array; /* The array we belong to */
  1052. union {
  1053. struct list_head private_list; /* For tree user */
  1054. struct rcu_head rcu_head; /* Used when freeing node */
  1055. };
  1056. void __rcu *slots[XA_CHUNK_SIZE];
  1057. union {
  1058. unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
  1059. unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
  1060. };
  1061. };
  1062. void xa_dump(const struct xarray *);
  1063. void xa_dump_node(const struct xa_node *);
  1064. #ifdef XA_DEBUG
  1065. #define XA_BUG_ON(xa, x) do { \
  1066. if (x) { \
  1067. xa_dump(xa); \
  1068. BUG(); \
  1069. } \
  1070. } while (0)
  1071. #define XA_NODE_BUG_ON(node, x) do { \
  1072. if (x) { \
  1073. if (node) xa_dump_node(node); \
  1074. BUG(); \
  1075. } \
  1076. } while (0)
  1077. #else
  1078. #define XA_BUG_ON(xa, x) do { } while (0)
  1079. #define XA_NODE_BUG_ON(node, x) do { } while (0)
  1080. #endif
  1081. /* Private */
  1082. static inline void *xa_head(const struct xarray *xa)
  1083. {
  1084. return rcu_dereference_check(xa->xa_head,
  1085. lockdep_is_held(&xa->xa_lock));
  1086. }
  1087. /* Private */
  1088. static inline void *xa_head_locked(const struct xarray *xa)
  1089. {
  1090. return rcu_dereference_protected(xa->xa_head,
  1091. lockdep_is_held(&xa->xa_lock));
  1092. }
  1093. /* Private */
  1094. static inline void *xa_entry(const struct xarray *xa,
  1095. const struct xa_node *node, unsigned int offset)
  1096. {
  1097. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  1098. return rcu_dereference_check(node->slots[offset],
  1099. lockdep_is_held(&xa->xa_lock));
  1100. }
  1101. /* Private */
  1102. static inline void *xa_entry_locked(const struct xarray *xa,
  1103. const struct xa_node *node, unsigned int offset)
  1104. {
  1105. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  1106. return rcu_dereference_protected(node->slots[offset],
  1107. lockdep_is_held(&xa->xa_lock));
  1108. }
  1109. /* Private */
  1110. static inline struct xa_node *xa_parent(const struct xarray *xa,
  1111. const struct xa_node *node)
  1112. {
  1113. return rcu_dereference_check(node->parent,
  1114. lockdep_is_held(&xa->xa_lock));
  1115. }
  1116. /* Private */
  1117. static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
  1118. const struct xa_node *node)
  1119. {
  1120. return rcu_dereference_protected(node->parent,
  1121. lockdep_is_held(&xa->xa_lock));
  1122. }
  1123. /* Private */
  1124. static inline void *xa_mk_node(const struct xa_node *node)
  1125. {
  1126. return (void *)((unsigned long)node | 2);
  1127. }
  1128. /* Private */
  1129. static inline struct xa_node *xa_to_node(const void *entry)
  1130. {
  1131. return (struct xa_node *)((unsigned long)entry - 2);
  1132. }
  1133. /* Private */
  1134. static inline bool xa_is_node(const void *entry)
  1135. {
  1136. return xa_is_internal(entry) && (unsigned long)entry > 4096;
  1137. }
  1138. /* Private */
  1139. static inline void *xa_mk_sibling(unsigned int offset)
  1140. {
  1141. return xa_mk_internal(offset);
  1142. }
  1143. /* Private */
  1144. static inline unsigned long xa_to_sibling(const void *entry)
  1145. {
  1146. return xa_to_internal(entry);
  1147. }
  1148. /**
  1149. * xa_is_sibling() - Is the entry a sibling entry?
  1150. * @entry: Entry retrieved from the XArray
  1151. *
  1152. * Return: %true if the entry is a sibling entry.
  1153. */
  1154. static inline bool xa_is_sibling(const void *entry)
  1155. {
  1156. return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
  1157. (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
  1158. }
  1159. #define XA_RETRY_ENTRY xa_mk_internal(256)
  1160. /**
  1161. * xa_is_retry() - Is the entry a retry entry?
  1162. * @entry: Entry retrieved from the XArray
  1163. *
  1164. * Return: %true if the entry is a retry entry.
  1165. */
  1166. static inline bool xa_is_retry(const void *entry)
  1167. {
  1168. return unlikely(entry == XA_RETRY_ENTRY);
  1169. }
  1170. /**
  1171. * xa_is_advanced() - Is the entry only permitted for the advanced API?
  1172. * @entry: Entry to be stored in the XArray.
  1173. *
  1174. * Return: %true if the entry cannot be stored by the normal API.
  1175. */
  1176. static inline bool xa_is_advanced(const void *entry)
  1177. {
  1178. return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY);
  1179. }
  1180. /**
  1181. * typedef xa_update_node_t - A callback function from the XArray.
  1182. * @node: The node which is being processed
  1183. *
  1184. * This function is called every time the XArray updates the count of
  1185. * present and value entries in a node. It allows advanced users to
  1186. * maintain the private_list in the node.
  1187. *
  1188. * Context: The xa_lock is held and interrupts may be disabled.
  1189. * Implementations should not drop the xa_lock, nor re-enable
  1190. * interrupts.
  1191. */
  1192. typedef void (*xa_update_node_t)(struct xa_node *node);
  1193. void xa_delete_node(struct xa_node *, xa_update_node_t);
  1194. /*
  1195. * The xa_state is opaque to its users. It contains various different pieces
  1196. * of state involved in the current operation on the XArray. It should be
  1197. * declared on the stack and passed between the various internal routines.
  1198. * The various elements in it should not be accessed directly, but only
  1199. * through the provided accessor functions. The below documentation is for
  1200. * the benefit of those working on the code, not for users of the XArray.
  1201. *
  1202. * @xa_node usually points to the xa_node containing the slot we're operating
  1203. * on (and @xa_offset is the offset in the slots array). If there is a
  1204. * single entry in the array at index 0, there are no allocated xa_nodes to
  1205. * point to, and so we store %NULL in @xa_node. @xa_node is set to
  1206. * the value %XAS_RESTART if the xa_state is not walked to the correct
  1207. * position in the tree of nodes for this operation. If an error occurs
  1208. * during an operation, it is set to an %XAS_ERROR value. If we run off the
  1209. * end of the allocated nodes, it is set to %XAS_BOUNDS.
  1210. */
  1211. struct xa_state {
  1212. struct xarray *xa;
  1213. unsigned long xa_index;
  1214. unsigned char xa_shift;
  1215. unsigned char xa_sibs;
  1216. unsigned char xa_offset;
  1217. unsigned char xa_pad; /* Helps gcc generate better code */
  1218. struct xa_node *xa_node;
  1219. struct xa_node *xa_alloc;
  1220. xa_update_node_t xa_update;
  1221. struct list_lru *xa_lru;
  1222. };
  1223. /*
  1224. * We encode errnos in the xas->xa_node. If an error has happened, we need to
  1225. * drop the lock to fix it, and once we've done so the xa_state is invalid.
  1226. */
  1227. #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
  1228. #define XAS_BOUNDS ((struct xa_node *)1UL)
  1229. #define XAS_RESTART ((struct xa_node *)3UL)
  1230. #define __XA_STATE(array, index, shift, sibs) { \
  1231. .xa = array, \
  1232. .xa_index = index, \
  1233. .xa_shift = shift, \
  1234. .xa_sibs = sibs, \
  1235. .xa_offset = 0, \
  1236. .xa_pad = 0, \
  1237. .xa_node = XAS_RESTART, \
  1238. .xa_alloc = NULL, \
  1239. .xa_update = NULL, \
  1240. .xa_lru = NULL, \
  1241. }
  1242. /**
  1243. * XA_STATE() - Declare an XArray operation state.
  1244. * @name: Name of this operation state (usually xas).
  1245. * @array: Array to operate on.
  1246. * @index: Initial index of interest.
  1247. *
  1248. * Declare and initialise an xa_state on the stack.
  1249. */
  1250. #define XA_STATE(name, array, index) \
  1251. struct xa_state name = __XA_STATE(array, index, 0, 0)
  1252. /**
  1253. * XA_STATE_ORDER() - Declare an XArray operation state.
  1254. * @name: Name of this operation state (usually xas).
  1255. * @array: Array to operate on.
  1256. * @index: Initial index of interest.
  1257. * @order: Order of entry.
  1258. *
  1259. * Declare and initialise an xa_state on the stack. This variant of
  1260. * XA_STATE() allows you to specify the 'order' of the element you
  1261. * want to operate on.`
  1262. */
  1263. #define XA_STATE_ORDER(name, array, index, order) \
  1264. struct xa_state name = __XA_STATE(array, \
  1265. (index >> order) << order, \
  1266. order - (order % XA_CHUNK_SHIFT), \
  1267. (1U << (order % XA_CHUNK_SHIFT)) - 1)
  1268. #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
  1269. #define xas_trylock(xas) xa_trylock((xas)->xa)
  1270. #define xas_lock(xas) xa_lock((xas)->xa)
  1271. #define xas_unlock(xas) xa_unlock((xas)->xa)
  1272. #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
  1273. #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
  1274. #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
  1275. #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
  1276. #define xas_lock_irqsave(xas, flags) \
  1277. xa_lock_irqsave((xas)->xa, flags)
  1278. #define xas_unlock_irqrestore(xas, flags) \
  1279. xa_unlock_irqrestore((xas)->xa, flags)
  1280. /**
  1281. * xas_error() - Return an errno stored in the xa_state.
  1282. * @xas: XArray operation state.
  1283. *
  1284. * Return: 0 if no error has been noted. A negative errno if one has.
  1285. */
  1286. static inline int xas_error(const struct xa_state *xas)
  1287. {
  1288. return xa_err(xas->xa_node);
  1289. }
  1290. /**
  1291. * xas_set_err() - Note an error in the xa_state.
  1292. * @xas: XArray operation state.
  1293. * @err: Negative error number.
  1294. *
  1295. * Only call this function with a negative @err; zero or positive errors
  1296. * will probably not behave the way you think they should. If you want
  1297. * to clear the error from an xa_state, use xas_reset().
  1298. */
  1299. static inline void xas_set_err(struct xa_state *xas, long err)
  1300. {
  1301. xas->xa_node = XA_ERROR(err);
  1302. }
  1303. /**
  1304. * xas_invalid() - Is the xas in a retry or error state?
  1305. * @xas: XArray operation state.
  1306. *
  1307. * Return: %true if the xas cannot be used for operations.
  1308. */
  1309. static inline bool xas_invalid(const struct xa_state *xas)
  1310. {
  1311. return (unsigned long)xas->xa_node & 3;
  1312. }
  1313. /**
  1314. * xas_valid() - Is the xas a valid cursor into the array?
  1315. * @xas: XArray operation state.
  1316. *
  1317. * Return: %true if the xas can be used for operations.
  1318. */
  1319. static inline bool xas_valid(const struct xa_state *xas)
  1320. {
  1321. return !xas_invalid(xas);
  1322. }
  1323. /**
  1324. * xas_is_node() - Does the xas point to a node?
  1325. * @xas: XArray operation state.
  1326. *
  1327. * Return: %true if the xas currently references a node.
  1328. */
  1329. static inline bool xas_is_node(const struct xa_state *xas)
  1330. {
  1331. return xas_valid(xas) && xas->xa_node;
  1332. }
  1333. /* True if the pointer is something other than a node */
  1334. static inline bool xas_not_node(struct xa_node *node)
  1335. {
  1336. return ((unsigned long)node & 3) || !node;
  1337. }
  1338. /* True if the node represents RESTART or an error */
  1339. static inline bool xas_frozen(struct xa_node *node)
  1340. {
  1341. return (unsigned long)node & 2;
  1342. }
  1343. /* True if the node represents head-of-tree, RESTART or BOUNDS */
  1344. static inline bool xas_top(struct xa_node *node)
  1345. {
  1346. return node <= XAS_RESTART;
  1347. }
  1348. /**
  1349. * xas_reset() - Reset an XArray operation state.
  1350. * @xas: XArray operation state.
  1351. *
  1352. * Resets the error or walk state of the @xas so future walks of the
  1353. * array will start from the root. Use this if you have dropped the
  1354. * xarray lock and want to reuse the xa_state.
  1355. *
  1356. * Context: Any context.
  1357. */
  1358. static inline void xas_reset(struct xa_state *xas)
  1359. {
  1360. xas->xa_node = XAS_RESTART;
  1361. }
  1362. /**
  1363. * xas_retry() - Retry the operation if appropriate.
  1364. * @xas: XArray operation state.
  1365. * @entry: Entry from xarray.
  1366. *
  1367. * The advanced functions may sometimes return an internal entry, such as
  1368. * a retry entry or a zero entry. This function sets up the @xas to restart
  1369. * the walk from the head of the array if needed.
  1370. *
  1371. * Context: Any context.
  1372. * Return: true if the operation needs to be retried.
  1373. */
  1374. static inline bool xas_retry(struct xa_state *xas, const void *entry)
  1375. {
  1376. if (xa_is_zero(entry))
  1377. return true;
  1378. if (!xa_is_retry(entry))
  1379. return false;
  1380. xas_reset(xas);
  1381. return true;
  1382. }
  1383. void *xas_load(struct xa_state *);
  1384. void *xas_store(struct xa_state *, void *entry);
  1385. void *xas_find(struct xa_state *, unsigned long max);
  1386. void *xas_find_conflict(struct xa_state *);
  1387. bool xas_get_mark(const struct xa_state *, xa_mark_t);
  1388. void xas_set_mark(const struct xa_state *, xa_mark_t);
  1389. void xas_clear_mark(const struct xa_state *, xa_mark_t);
  1390. void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
  1391. void xas_init_marks(const struct xa_state *);
  1392. bool xas_nomem(struct xa_state *, gfp_t);
  1393. void xas_destroy(struct xa_state *);
  1394. void xas_pause(struct xa_state *);
  1395. void xas_create_range(struct xa_state *);
  1396. #ifdef CONFIG_XARRAY_MULTI
  1397. int xa_get_order(struct xarray *, unsigned long index);
  1398. void xas_split(struct xa_state *, void *entry, unsigned int order);
  1399. void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t);
  1400. #else
  1401. static inline int xa_get_order(struct xarray *xa, unsigned long index)
  1402. {
  1403. return 0;
  1404. }
  1405. static inline void xas_split(struct xa_state *xas, void *entry,
  1406. unsigned int order)
  1407. {
  1408. xas_store(xas, entry);
  1409. }
  1410. static inline void xas_split_alloc(struct xa_state *xas, void *entry,
  1411. unsigned int order, gfp_t gfp)
  1412. {
  1413. }
  1414. #endif
  1415. /**
  1416. * xas_reload() - Refetch an entry from the xarray.
  1417. * @xas: XArray operation state.
  1418. *
  1419. * Use this function to check that a previously loaded entry still has
  1420. * the same value. This is useful for the lockless pagecache lookup where
  1421. * we walk the array with only the RCU lock to protect us, lock the page,
  1422. * then check that the page hasn't moved since we looked it up.
  1423. *
  1424. * The caller guarantees that @xas is still valid. If it may be in an
  1425. * error or restart state, call xas_load() instead.
  1426. *
  1427. * Return: The entry at this location in the xarray.
  1428. */
  1429. static inline void *xas_reload(struct xa_state *xas)
  1430. {
  1431. struct xa_node *node = xas->xa_node;
  1432. void *entry;
  1433. char offset;
  1434. if (!node)
  1435. return xa_head(xas->xa);
  1436. if (IS_ENABLED(CONFIG_XARRAY_MULTI)) {
  1437. offset = (xas->xa_index >> node->shift) & XA_CHUNK_MASK;
  1438. entry = xa_entry(xas->xa, node, offset);
  1439. if (!xa_is_sibling(entry))
  1440. return entry;
  1441. offset = xa_to_sibling(entry);
  1442. } else {
  1443. offset = xas->xa_offset;
  1444. }
  1445. return xa_entry(xas->xa, node, offset);
  1446. }
  1447. /**
  1448. * xas_set() - Set up XArray operation state for a different index.
  1449. * @xas: XArray operation state.
  1450. * @index: New index into the XArray.
  1451. *
  1452. * Move the operation state to refer to a different index. This will
  1453. * have the effect of starting a walk from the top; see xas_next()
  1454. * to move to an adjacent index.
  1455. */
  1456. static inline void xas_set(struct xa_state *xas, unsigned long index)
  1457. {
  1458. xas->xa_index = index;
  1459. xas->xa_node = XAS_RESTART;
  1460. }
  1461. /**
  1462. * xas_advance() - Skip over sibling entries.
  1463. * @xas: XArray operation state.
  1464. * @index: Index of last sibling entry.
  1465. *
  1466. * Move the operation state to refer to the last sibling entry.
  1467. * This is useful for loops that normally want to see sibling
  1468. * entries but sometimes want to skip them. Use xas_set() if you
  1469. * want to move to an index which is not part of this entry.
  1470. */
  1471. static inline void xas_advance(struct xa_state *xas, unsigned long index)
  1472. {
  1473. unsigned char shift = xas_is_node(xas) ? xas->xa_node->shift : 0;
  1474. xas->xa_index = index;
  1475. xas->xa_offset = (index >> shift) & XA_CHUNK_MASK;
  1476. }
  1477. /**
  1478. * xas_set_order() - Set up XArray operation state for a multislot entry.
  1479. * @xas: XArray operation state.
  1480. * @index: Target of the operation.
  1481. * @order: Entry occupies 2^@order indices.
  1482. */
  1483. static inline void xas_set_order(struct xa_state *xas, unsigned long index,
  1484. unsigned int order)
  1485. {
  1486. #ifdef CONFIG_XARRAY_MULTI
  1487. xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
  1488. xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
  1489. xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
  1490. xas->xa_node = XAS_RESTART;
  1491. #else
  1492. BUG_ON(order > 0);
  1493. xas_set(xas, index);
  1494. #endif
  1495. }
  1496. /**
  1497. * xas_set_update() - Set up XArray operation state for a callback.
  1498. * @xas: XArray operation state.
  1499. * @update: Function to call when updating a node.
  1500. *
  1501. * The XArray can notify a caller after it has updated an xa_node.
  1502. * This is advanced functionality and is only needed by the page cache.
  1503. */
  1504. static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
  1505. {
  1506. xas->xa_update = update;
  1507. }
  1508. static inline void xas_set_lru(struct xa_state *xas, struct list_lru *lru)
  1509. {
  1510. xas->xa_lru = lru;
  1511. }
  1512. /**
  1513. * xas_next_entry() - Advance iterator to next present entry.
  1514. * @xas: XArray operation state.
  1515. * @max: Highest index to return.
  1516. *
  1517. * xas_next_entry() is an inline function to optimise xarray traversal for
  1518. * speed. It is equivalent to calling xas_find(), and will call xas_find()
  1519. * for all the hard cases.
  1520. *
  1521. * Return: The next present entry after the one currently referred to by @xas.
  1522. */
  1523. static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
  1524. {
  1525. struct xa_node *node = xas->xa_node;
  1526. void *entry;
  1527. if (unlikely(xas_not_node(node) || node->shift ||
  1528. xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
  1529. return xas_find(xas, max);
  1530. do {
  1531. if (unlikely(xas->xa_index >= max))
  1532. return xas_find(xas, max);
  1533. if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
  1534. return xas_find(xas, max);
  1535. entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
  1536. if (unlikely(xa_is_internal(entry)))
  1537. return xas_find(xas, max);
  1538. xas->xa_offset++;
  1539. xas->xa_index++;
  1540. } while (!entry);
  1541. return entry;
  1542. }
  1543. /* Private */
  1544. static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
  1545. xa_mark_t mark)
  1546. {
  1547. unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
  1548. unsigned int offset = xas->xa_offset;
  1549. if (advance)
  1550. offset++;
  1551. if (XA_CHUNK_SIZE == BITS_PER_LONG) {
  1552. if (offset < XA_CHUNK_SIZE) {
  1553. unsigned long data = *addr & (~0UL << offset);
  1554. if (data)
  1555. return __ffs(data);
  1556. }
  1557. return XA_CHUNK_SIZE;
  1558. }
  1559. return find_next_bit(addr, XA_CHUNK_SIZE, offset);
  1560. }
  1561. /**
  1562. * xas_next_marked() - Advance iterator to next marked entry.
  1563. * @xas: XArray operation state.
  1564. * @max: Highest index to return.
  1565. * @mark: Mark to search for.
  1566. *
  1567. * xas_next_marked() is an inline function to optimise xarray traversal for
  1568. * speed. It is equivalent to calling xas_find_marked(), and will call
  1569. * xas_find_marked() for all the hard cases.
  1570. *
  1571. * Return: The next marked entry after the one currently referred to by @xas.
  1572. */
  1573. static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
  1574. xa_mark_t mark)
  1575. {
  1576. struct xa_node *node = xas->xa_node;
  1577. void *entry;
  1578. unsigned int offset;
  1579. if (unlikely(xas_not_node(node) || node->shift))
  1580. return xas_find_marked(xas, max, mark);
  1581. offset = xas_find_chunk(xas, true, mark);
  1582. xas->xa_offset = offset;
  1583. xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
  1584. if (xas->xa_index > max)
  1585. return NULL;
  1586. if (offset == XA_CHUNK_SIZE)
  1587. return xas_find_marked(xas, max, mark);
  1588. entry = xa_entry(xas->xa, node, offset);
  1589. if (!entry)
  1590. return xas_find_marked(xas, max, mark);
  1591. return entry;
  1592. }
  1593. /*
  1594. * If iterating while holding a lock, drop the lock and reschedule
  1595. * every %XA_CHECK_SCHED loops.
  1596. */
  1597. enum {
  1598. XA_CHECK_SCHED = 4096,
  1599. };
  1600. /**
  1601. * xas_for_each() - Iterate over a range of an XArray.
  1602. * @xas: XArray operation state.
  1603. * @entry: Entry retrieved from the array.
  1604. * @max: Maximum index to retrieve from array.
  1605. *
  1606. * The loop body will be executed for each entry present in the xarray
  1607. * between the current xas position and @max. @entry will be set to
  1608. * the entry retrieved from the xarray. It is safe to delete entries
  1609. * from the array in the loop body. You should hold either the RCU lock
  1610. * or the xa_lock while iterating. If you need to drop the lock, call
  1611. * xas_pause() first.
  1612. */
  1613. #define xas_for_each(xas, entry, max) \
  1614. for (entry = xas_find(xas, max); entry; \
  1615. entry = xas_next_entry(xas, max))
  1616. /**
  1617. * xas_for_each_marked() - Iterate over a range of an XArray.
  1618. * @xas: XArray operation state.
  1619. * @entry: Entry retrieved from the array.
  1620. * @max: Maximum index to retrieve from array.
  1621. * @mark: Mark to search for.
  1622. *
  1623. * The loop body will be executed for each marked entry in the xarray
  1624. * between the current xas position and @max. @entry will be set to
  1625. * the entry retrieved from the xarray. It is safe to delete entries
  1626. * from the array in the loop body. You should hold either the RCU lock
  1627. * or the xa_lock while iterating. If you need to drop the lock, call
  1628. * xas_pause() first.
  1629. */
  1630. #define xas_for_each_marked(xas, entry, max, mark) \
  1631. for (entry = xas_find_marked(xas, max, mark); entry; \
  1632. entry = xas_next_marked(xas, max, mark))
  1633. /**
  1634. * xas_for_each_conflict() - Iterate over a range of an XArray.
  1635. * @xas: XArray operation state.
  1636. * @entry: Entry retrieved from the array.
  1637. *
  1638. * The loop body will be executed for each entry in the XArray that
  1639. * lies within the range specified by @xas. If the loop terminates
  1640. * normally, @entry will be %NULL. The user may break out of the loop,
  1641. * which will leave @entry set to the conflicting entry. The caller
  1642. * may also call xa_set_err() to exit the loop while setting an error
  1643. * to record the reason.
  1644. */
  1645. #define xas_for_each_conflict(xas, entry) \
  1646. while ((entry = xas_find_conflict(xas)))
  1647. void *__xas_next(struct xa_state *);
  1648. void *__xas_prev(struct xa_state *);
  1649. /**
  1650. * xas_prev() - Move iterator to previous index.
  1651. * @xas: XArray operation state.
  1652. *
  1653. * If the @xas was in an error state, it will remain in an error state
  1654. * and this function will return %NULL. If the @xas has never been walked,
  1655. * it will have the effect of calling xas_load(). Otherwise one will be
  1656. * subtracted from the index and the state will be walked to the correct
  1657. * location in the array for the next operation.
  1658. *
  1659. * If the iterator was referencing index 0, this function wraps
  1660. * around to %ULONG_MAX.
  1661. *
  1662. * Return: The entry at the new index. This may be %NULL or an internal
  1663. * entry.
  1664. */
  1665. static inline void *xas_prev(struct xa_state *xas)
  1666. {
  1667. struct xa_node *node = xas->xa_node;
  1668. if (unlikely(xas_not_node(node) || node->shift ||
  1669. xas->xa_offset == 0))
  1670. return __xas_prev(xas);
  1671. xas->xa_index--;
  1672. xas->xa_offset--;
  1673. return xa_entry(xas->xa, node, xas->xa_offset);
  1674. }
  1675. /**
  1676. * xas_next() - Move state to next index.
  1677. * @xas: XArray operation state.
  1678. *
  1679. * If the @xas was in an error state, it will remain in an error state
  1680. * and this function will return %NULL. If the @xas has never been walked,
  1681. * it will have the effect of calling xas_load(). Otherwise one will be
  1682. * added to the index and the state will be walked to the correct
  1683. * location in the array for the next operation.
  1684. *
  1685. * If the iterator was referencing index %ULONG_MAX, this function wraps
  1686. * around to 0.
  1687. *
  1688. * Return: The entry at the new index. This may be %NULL or an internal
  1689. * entry.
  1690. */
  1691. static inline void *xas_next(struct xa_state *xas)
  1692. {
  1693. struct xa_node *node = xas->xa_node;
  1694. if (unlikely(xas_not_node(node) || node->shift ||
  1695. xas->xa_offset == XA_CHUNK_MASK))
  1696. return __xas_next(xas);
  1697. xas->xa_index++;
  1698. xas->xa_offset++;
  1699. return xa_entry(xas->xa, node, xas->xa_offset);
  1700. }
  1701. #endif /* _LINUX_XARRAY_H */