xarray.h 42 KB

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