xarray.h 56 KB

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