xarray.h 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  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_erase(struct xarray *, unsigned long index);
  269. void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
  270. void *entry, gfp_t);
  271. bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
  272. void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
  273. void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
  274. void *xa_find(struct xarray *xa, unsigned long *index,
  275. unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
  276. void *xa_find_after(struct xarray *xa, unsigned long *index,
  277. unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
  278. unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
  279. unsigned long max, unsigned int n, xa_mark_t);
  280. void xa_destroy(struct xarray *);
  281. /**
  282. * xa_init() - Initialise an empty XArray.
  283. * @xa: XArray.
  284. *
  285. * An empty XArray is full of NULL entries.
  286. *
  287. * Context: Any context.
  288. */
  289. static inline void xa_init(struct xarray *xa)
  290. {
  291. xa_init_flags(xa, 0);
  292. }
  293. /**
  294. * xa_empty() - Determine if an array has any present entries.
  295. * @xa: XArray.
  296. *
  297. * Context: Any context.
  298. * Return: %true if the array contains only NULL pointers.
  299. */
  300. static inline bool xa_empty(const struct xarray *xa)
  301. {
  302. return xa->xa_head == NULL;
  303. }
  304. /**
  305. * xa_marked() - Inquire whether any entry in this array has a mark set
  306. * @xa: Array
  307. * @mark: Mark value
  308. *
  309. * Context: Any context.
  310. * Return: %true if any entry has this mark set.
  311. */
  312. static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
  313. {
  314. return xa->xa_flags & XA_FLAGS_MARK(mark);
  315. }
  316. /**
  317. * xa_for_each() - Iterate over a portion of an XArray.
  318. * @xa: XArray.
  319. * @entry: Entry retrieved from array.
  320. * @index: Index of @entry.
  321. * @max: Maximum index to retrieve from array.
  322. * @filter: Selection criterion.
  323. *
  324. * Initialise @index to the lowest index you want to retrieve from the
  325. * array. During the iteration, @entry will have the value of the entry
  326. * stored in @xa at @index. The iteration will skip all entries in the
  327. * array which do not match @filter. You may modify @index during the
  328. * iteration if you want to skip or reprocess indices. It is safe to modify
  329. * the array during the iteration. At the end of the iteration, @entry will
  330. * be set to NULL and @index will have a value less than or equal to max.
  331. *
  332. * xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have
  333. * to handle your own locking with xas_for_each(), and if you have to unlock
  334. * after each iteration, it will also end up being O(n.log(n)). xa_for_each()
  335. * will spin if it hits a retry entry; if you intend to see retry entries,
  336. * you should use the xas_for_each() iterator instead. The xas_for_each()
  337. * iterator will expand into more inline code than xa_for_each().
  338. *
  339. * Context: Any context. Takes and releases the RCU lock.
  340. */
  341. #define xa_for_each(xa, entry, index, max, filter) \
  342. for (entry = xa_find(xa, &index, max, filter); entry; \
  343. entry = xa_find_after(xa, &index, max, filter))
  344. #define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
  345. #define xa_lock(xa) spin_lock(&(xa)->xa_lock)
  346. #define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
  347. #define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
  348. #define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
  349. #define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
  350. #define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
  351. #define xa_lock_irqsave(xa, flags) \
  352. spin_lock_irqsave(&(xa)->xa_lock, flags)
  353. #define xa_unlock_irqrestore(xa, flags) \
  354. spin_unlock_irqrestore(&(xa)->xa_lock, flags)
  355. /*
  356. * Versions of the normal API which require the caller to hold the
  357. * xa_lock. If the GFP flags allow it, they will drop the lock to
  358. * allocate memory, then reacquire it afterwards. These functions
  359. * may also re-enable interrupts if the XArray flags indicate the
  360. * locking should be interrupt safe.
  361. */
  362. void *__xa_erase(struct xarray *, unsigned long index);
  363. void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
  364. void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
  365. void *entry, gfp_t);
  366. int __xa_alloc(struct xarray *, u32 *id, u32 max, void *entry, gfp_t);
  367. int __xa_reserve(struct xarray *, unsigned long index, gfp_t);
  368. void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
  369. void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
  370. /**
  371. * __xa_insert() - Store this entry in the XArray unless another entry is
  372. * already present.
  373. * @xa: XArray.
  374. * @index: Index into array.
  375. * @entry: New entry.
  376. * @gfp: Memory allocation flags.
  377. *
  378. * If you would rather see the existing entry in the array, use __xa_cmpxchg().
  379. * This function is for users who don't care what the entry is, only that
  380. * one is present.
  381. *
  382. * Context: Any context. Expects xa_lock to be held on entry. May
  383. * release and reacquire xa_lock if the @gfp flags permit.
  384. * Return: 0 if the store succeeded. -EEXIST if another entry was present.
  385. * -ENOMEM if memory could not be allocated.
  386. */
  387. static inline int __xa_insert(struct xarray *xa, unsigned long index,
  388. void *entry, gfp_t gfp)
  389. {
  390. void *curr = __xa_cmpxchg(xa, index, NULL, entry, gfp);
  391. if (!curr)
  392. return 0;
  393. if (xa_is_err(curr))
  394. return xa_err(curr);
  395. return -EEXIST;
  396. }
  397. /**
  398. * xa_store_bh() - Store this entry in the XArray.
  399. * @xa: XArray.
  400. * @index: Index into array.
  401. * @entry: New entry.
  402. * @gfp: Memory allocation flags.
  403. *
  404. * This function is like calling xa_store() except it disables softirqs
  405. * while holding the array lock.
  406. *
  407. * Context: Any context. Takes and releases the xa_lock while
  408. * disabling softirqs.
  409. * Return: The entry which used to be at this index.
  410. */
  411. static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
  412. void *entry, gfp_t gfp)
  413. {
  414. void *curr;
  415. xa_lock_bh(xa);
  416. curr = __xa_store(xa, index, entry, gfp);
  417. xa_unlock_bh(xa);
  418. return curr;
  419. }
  420. /**
  421. * xa_store_irq() - Erase this entry from the XArray.
  422. * @xa: XArray.
  423. * @index: Index into array.
  424. * @entry: New entry.
  425. * @gfp: Memory allocation flags.
  426. *
  427. * This function is like calling xa_store() except it disables interrupts
  428. * while holding the array lock.
  429. *
  430. * Context: Process context. Takes and releases the xa_lock while
  431. * disabling interrupts.
  432. * Return: The entry which used to be at this index.
  433. */
  434. static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
  435. void *entry, gfp_t gfp)
  436. {
  437. void *curr;
  438. xa_lock_irq(xa);
  439. curr = __xa_store(xa, index, entry, gfp);
  440. xa_unlock_irq(xa);
  441. return curr;
  442. }
  443. /**
  444. * xa_erase_bh() - Erase this entry from the XArray.
  445. * @xa: XArray.
  446. * @index: Index of entry.
  447. *
  448. * This function is the equivalent of calling xa_store() with %NULL as
  449. * the third argument. The XArray does not need to allocate memory, so
  450. * the user does not need to provide GFP flags.
  451. *
  452. * Context: Any context. Takes and releases the xa_lock while
  453. * disabling softirqs.
  454. * Return: The entry which used to be at this index.
  455. */
  456. static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
  457. {
  458. void *entry;
  459. xa_lock_bh(xa);
  460. entry = __xa_erase(xa, index);
  461. xa_unlock_bh(xa);
  462. return entry;
  463. }
  464. /**
  465. * xa_erase_irq() - Erase this entry from the XArray.
  466. * @xa: XArray.
  467. * @index: Index of entry.
  468. *
  469. * This function is the equivalent of calling xa_store() with %NULL as
  470. * the third argument. The XArray does not need to allocate memory, so
  471. * the user does not need to provide GFP flags.
  472. *
  473. * Context: Process context. Takes and releases the xa_lock while
  474. * disabling interrupts.
  475. * Return: The entry which used to be at this index.
  476. */
  477. static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
  478. {
  479. void *entry;
  480. xa_lock_irq(xa);
  481. entry = __xa_erase(xa, index);
  482. xa_unlock_irq(xa);
  483. return entry;
  484. }
  485. /**
  486. * xa_cmpxchg() - Conditionally replace an entry in the XArray.
  487. * @xa: XArray.
  488. * @index: Index into array.
  489. * @old: Old value to test against.
  490. * @entry: New value to place in array.
  491. * @gfp: Memory allocation flags.
  492. *
  493. * If the entry at @index is the same as @old, replace it with @entry.
  494. * If the return value is equal to @old, then the exchange was successful.
  495. *
  496. * Context: Any context. Takes and releases the xa_lock. May sleep
  497. * if the @gfp flags permit.
  498. * Return: The old value at this index or xa_err() if an error happened.
  499. */
  500. static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
  501. void *old, void *entry, gfp_t gfp)
  502. {
  503. void *curr;
  504. xa_lock(xa);
  505. curr = __xa_cmpxchg(xa, index, old, entry, gfp);
  506. xa_unlock(xa);
  507. return curr;
  508. }
  509. /**
  510. * xa_insert() - Store this entry in the XArray unless another entry is
  511. * already present.
  512. * @xa: XArray.
  513. * @index: Index into array.
  514. * @entry: New entry.
  515. * @gfp: Memory allocation flags.
  516. *
  517. * If you would rather see the existing entry in the array, use xa_cmpxchg().
  518. * This function is for users who don't care what the entry is, only that
  519. * one is present.
  520. *
  521. * Context: Process context. Takes and releases the xa_lock.
  522. * May sleep if the @gfp flags permit.
  523. * Return: 0 if the store succeeded. -EEXIST if another entry was present.
  524. * -ENOMEM if memory could not be allocated.
  525. */
  526. static inline int xa_insert(struct xarray *xa, unsigned long index,
  527. void *entry, gfp_t gfp)
  528. {
  529. void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
  530. if (!curr)
  531. return 0;
  532. if (xa_is_err(curr))
  533. return xa_err(curr);
  534. return -EEXIST;
  535. }
  536. /**
  537. * xa_alloc() - Find somewhere to store this entry in the XArray.
  538. * @xa: XArray.
  539. * @id: Pointer to ID.
  540. * @max: Maximum ID to allocate (inclusive).
  541. * @entry: New entry.
  542. * @gfp: Memory allocation flags.
  543. *
  544. * Allocates an unused ID in the range specified by @id and @max.
  545. * Updates the @id pointer with the index, then stores the entry at that
  546. * index. A concurrent lookup will not see an uninitialised @id.
  547. *
  548. * Context: Process context. Takes and releases the xa_lock. May sleep if
  549. * the @gfp flags permit.
  550. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  551. * there is no more space in the XArray.
  552. */
  553. static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
  554. gfp_t gfp)
  555. {
  556. int err;
  557. xa_lock(xa);
  558. err = __xa_alloc(xa, id, max, entry, gfp);
  559. xa_unlock(xa);
  560. return err;
  561. }
  562. /**
  563. * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
  564. * @xa: XArray.
  565. * @id: Pointer to ID.
  566. * @max: Maximum ID to allocate (inclusive).
  567. * @entry: New entry.
  568. * @gfp: Memory allocation flags.
  569. *
  570. * Allocates an unused ID in the range specified by @id and @max.
  571. * Updates the @id pointer with the index, then stores the entry at that
  572. * index. A concurrent lookup will not see an uninitialised @id.
  573. *
  574. * Context: Any context. Takes and releases the xa_lock while
  575. * disabling softirqs. May sleep if the @gfp flags permit.
  576. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  577. * there is no more space in the XArray.
  578. */
  579. static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
  580. gfp_t gfp)
  581. {
  582. int err;
  583. xa_lock_bh(xa);
  584. err = __xa_alloc(xa, id, max, entry, gfp);
  585. xa_unlock_bh(xa);
  586. return err;
  587. }
  588. /**
  589. * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
  590. * @xa: XArray.
  591. * @id: Pointer to ID.
  592. * @max: Maximum ID to allocate (inclusive).
  593. * @entry: New entry.
  594. * @gfp: Memory allocation flags.
  595. *
  596. * Allocates an unused ID in the range specified by @id and @max.
  597. * Updates the @id pointer with the index, then stores the entry at that
  598. * index. A concurrent lookup will not see an uninitialised @id.
  599. *
  600. * Context: Process context. Takes and releases the xa_lock while
  601. * disabling interrupts. May sleep if the @gfp flags permit.
  602. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  603. * there is no more space in the XArray.
  604. */
  605. static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
  606. gfp_t gfp)
  607. {
  608. int err;
  609. xa_lock_irq(xa);
  610. err = __xa_alloc(xa, id, max, entry, gfp);
  611. xa_unlock_irq(xa);
  612. return err;
  613. }
  614. /**
  615. * xa_reserve() - Reserve this index in the XArray.
  616. * @xa: XArray.
  617. * @index: Index into array.
  618. * @gfp: Memory allocation flags.
  619. *
  620. * Ensures there is somewhere to store an entry at @index in the array.
  621. * If there is already something stored at @index, this function does
  622. * nothing. If there was nothing there, the entry is marked as reserved.
  623. * Loading from a reserved entry returns a %NULL pointer.
  624. *
  625. * If you do not use the entry that you have reserved, call xa_release()
  626. * or xa_erase() to free any unnecessary memory.
  627. *
  628. * Context: Any context. Takes and releases the xa_lock.
  629. * May sleep if the @gfp flags permit.
  630. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  631. */
  632. static inline
  633. int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
  634. {
  635. int ret;
  636. xa_lock(xa);
  637. ret = __xa_reserve(xa, index, gfp);
  638. xa_unlock(xa);
  639. return ret;
  640. }
  641. /**
  642. * xa_reserve_bh() - Reserve this index in the XArray.
  643. * @xa: XArray.
  644. * @index: Index into array.
  645. * @gfp: Memory allocation flags.
  646. *
  647. * A softirq-disabling version of xa_reserve().
  648. *
  649. * Context: Any context. Takes and releases the xa_lock while
  650. * disabling softirqs.
  651. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  652. */
  653. static inline
  654. int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
  655. {
  656. int ret;
  657. xa_lock_bh(xa);
  658. ret = __xa_reserve(xa, index, gfp);
  659. xa_unlock_bh(xa);
  660. return ret;
  661. }
  662. /**
  663. * xa_reserve_irq() - Reserve this index in the XArray.
  664. * @xa: XArray.
  665. * @index: Index into array.
  666. * @gfp: Memory allocation flags.
  667. *
  668. * An interrupt-disabling version of xa_reserve().
  669. *
  670. * Context: Process context. Takes and releases the xa_lock while
  671. * disabling interrupts.
  672. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  673. */
  674. static inline
  675. int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
  676. {
  677. int ret;
  678. xa_lock_irq(xa);
  679. ret = __xa_reserve(xa, index, gfp);
  680. xa_unlock_irq(xa);
  681. return ret;
  682. }
  683. /**
  684. * xa_release() - Release a reserved entry.
  685. * @xa: XArray.
  686. * @index: Index of entry.
  687. *
  688. * After calling xa_reserve(), you can call this function to release the
  689. * reservation. If the entry at @index has been stored to, this function
  690. * will do nothing.
  691. */
  692. static inline void xa_release(struct xarray *xa, unsigned long index)
  693. {
  694. xa_cmpxchg(xa, index, NULL, NULL, 0);
  695. }
  696. /* Everything below here is the Advanced API. Proceed with caution. */
  697. /*
  698. * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
  699. * the best chunk size requires some tradeoffs. A power of two recommends
  700. * itself so that we can walk the tree based purely on shifts and masks.
  701. * Generally, the larger the better; as the number of slots per level of the
  702. * tree increases, the less tall the tree needs to be. But that needs to be
  703. * balanced against the memory consumption of each node. On a 64-bit system,
  704. * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
  705. * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
  706. */
  707. #ifndef XA_CHUNK_SHIFT
  708. #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  709. #endif
  710. #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
  711. #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
  712. #define XA_MAX_MARKS 3
  713. #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
  714. /*
  715. * @count is the count of every non-NULL element in the ->slots array
  716. * whether that is a value entry, a retry entry, a user pointer,
  717. * a sibling entry or a pointer to the next level of the tree.
  718. * @nr_values is the count of every element in ->slots which is
  719. * either a value entry or a sibling of a value entry.
  720. */
  721. struct xa_node {
  722. unsigned char shift; /* Bits remaining in each slot */
  723. unsigned char offset; /* Slot offset in parent */
  724. unsigned char count; /* Total entry count */
  725. unsigned char nr_values; /* Value entry count */
  726. struct xa_node __rcu *parent; /* NULL at top of tree */
  727. struct xarray *array; /* The array we belong to */
  728. union {
  729. struct list_head private_list; /* For tree user */
  730. struct rcu_head rcu_head; /* Used when freeing node */
  731. };
  732. void __rcu *slots[XA_CHUNK_SIZE];
  733. union {
  734. unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
  735. unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
  736. };
  737. };
  738. void xa_dump(const struct xarray *);
  739. void xa_dump_node(const struct xa_node *);
  740. #ifdef XA_DEBUG
  741. #define XA_BUG_ON(xa, x) do { \
  742. if (x) { \
  743. xa_dump(xa); \
  744. BUG(); \
  745. } \
  746. } while (0)
  747. #define XA_NODE_BUG_ON(node, x) do { \
  748. if (x) { \
  749. if (node) xa_dump_node(node); \
  750. BUG(); \
  751. } \
  752. } while (0)
  753. #else
  754. #define XA_BUG_ON(xa, x) do { } while (0)
  755. #define XA_NODE_BUG_ON(node, x) do { } while (0)
  756. #endif
  757. /* Private */
  758. static inline void *xa_head(const struct xarray *xa)
  759. {
  760. return rcu_dereference_check(xa->xa_head,
  761. lockdep_is_held(&xa->xa_lock));
  762. }
  763. /* Private */
  764. static inline void *xa_head_locked(const struct xarray *xa)
  765. {
  766. return rcu_dereference_protected(xa->xa_head,
  767. lockdep_is_held(&xa->xa_lock));
  768. }
  769. /* Private */
  770. static inline void *xa_entry(const struct xarray *xa,
  771. const struct xa_node *node, unsigned int offset)
  772. {
  773. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  774. return rcu_dereference_check(node->slots[offset],
  775. lockdep_is_held(&xa->xa_lock));
  776. }
  777. /* Private */
  778. static inline void *xa_entry_locked(const struct xarray *xa,
  779. const struct xa_node *node, unsigned int offset)
  780. {
  781. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  782. return rcu_dereference_protected(node->slots[offset],
  783. lockdep_is_held(&xa->xa_lock));
  784. }
  785. /* Private */
  786. static inline struct xa_node *xa_parent(const struct xarray *xa,
  787. const struct xa_node *node)
  788. {
  789. return rcu_dereference_check(node->parent,
  790. lockdep_is_held(&xa->xa_lock));
  791. }
  792. /* Private */
  793. static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
  794. const struct xa_node *node)
  795. {
  796. return rcu_dereference_protected(node->parent,
  797. lockdep_is_held(&xa->xa_lock));
  798. }
  799. /* Private */
  800. static inline void *xa_mk_node(const struct xa_node *node)
  801. {
  802. return (void *)((unsigned long)node | 2);
  803. }
  804. /* Private */
  805. static inline struct xa_node *xa_to_node(const void *entry)
  806. {
  807. return (struct xa_node *)((unsigned long)entry - 2);
  808. }
  809. /* Private */
  810. static inline bool xa_is_node(const void *entry)
  811. {
  812. return xa_is_internal(entry) && (unsigned long)entry > 4096;
  813. }
  814. /* Private */
  815. static inline void *xa_mk_sibling(unsigned int offset)
  816. {
  817. return xa_mk_internal(offset);
  818. }
  819. /* Private */
  820. static inline unsigned long xa_to_sibling(const void *entry)
  821. {
  822. return xa_to_internal(entry);
  823. }
  824. /**
  825. * xa_is_sibling() - Is the entry a sibling entry?
  826. * @entry: Entry retrieved from the XArray
  827. *
  828. * Return: %true if the entry is a sibling entry.
  829. */
  830. static inline bool xa_is_sibling(const void *entry)
  831. {
  832. return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
  833. (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
  834. }
  835. #define XA_ZERO_ENTRY xa_mk_internal(256)
  836. #define XA_RETRY_ENTRY xa_mk_internal(257)
  837. /**
  838. * xa_is_zero() - Is the entry a zero entry?
  839. * @entry: Entry retrieved from the XArray
  840. *
  841. * Return: %true if the entry is a zero entry.
  842. */
  843. static inline bool xa_is_zero(const void *entry)
  844. {
  845. return unlikely(entry == XA_ZERO_ENTRY);
  846. }
  847. /**
  848. * xa_is_retry() - Is the entry a retry entry?
  849. * @entry: Entry retrieved from the XArray
  850. *
  851. * Return: %true if the entry is a retry entry.
  852. */
  853. static inline bool xa_is_retry(const void *entry)
  854. {
  855. return unlikely(entry == XA_RETRY_ENTRY);
  856. }
  857. /**
  858. * typedef xa_update_node_t - A callback function from the XArray.
  859. * @node: The node which is being processed
  860. *
  861. * This function is called every time the XArray updates the count of
  862. * present and value entries in a node. It allows advanced users to
  863. * maintain the private_list in the node.
  864. *
  865. * Context: The xa_lock is held and interrupts may be disabled.
  866. * Implementations should not drop the xa_lock, nor re-enable
  867. * interrupts.
  868. */
  869. typedef void (*xa_update_node_t)(struct xa_node *node);
  870. /*
  871. * The xa_state is opaque to its users. It contains various different pieces
  872. * of state involved in the current operation on the XArray. It should be
  873. * declared on the stack and passed between the various internal routines.
  874. * The various elements in it should not be accessed directly, but only
  875. * through the provided accessor functions. The below documentation is for
  876. * the benefit of those working on the code, not for users of the XArray.
  877. *
  878. * @xa_node usually points to the xa_node containing the slot we're operating
  879. * on (and @xa_offset is the offset in the slots array). If there is a
  880. * single entry in the array at index 0, there are no allocated xa_nodes to
  881. * point to, and so we store %NULL in @xa_node. @xa_node is set to
  882. * the value %XAS_RESTART if the xa_state is not walked to the correct
  883. * position in the tree of nodes for this operation. If an error occurs
  884. * during an operation, it is set to an %XAS_ERROR value. If we run off the
  885. * end of the allocated nodes, it is set to %XAS_BOUNDS.
  886. */
  887. struct xa_state {
  888. struct xarray *xa;
  889. unsigned long xa_index;
  890. unsigned char xa_shift;
  891. unsigned char xa_sibs;
  892. unsigned char xa_offset;
  893. unsigned char xa_pad; /* Helps gcc generate better code */
  894. struct xa_node *xa_node;
  895. struct xa_node *xa_alloc;
  896. xa_update_node_t xa_update;
  897. };
  898. /*
  899. * We encode errnos in the xas->xa_node. If an error has happened, we need to
  900. * drop the lock to fix it, and once we've done so the xa_state is invalid.
  901. */
  902. #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
  903. #define XAS_BOUNDS ((struct xa_node *)1UL)
  904. #define XAS_RESTART ((struct xa_node *)3UL)
  905. #define __XA_STATE(array, index, shift, sibs) { \
  906. .xa = array, \
  907. .xa_index = index, \
  908. .xa_shift = shift, \
  909. .xa_sibs = sibs, \
  910. .xa_offset = 0, \
  911. .xa_pad = 0, \
  912. .xa_node = XAS_RESTART, \
  913. .xa_alloc = NULL, \
  914. .xa_update = NULL \
  915. }
  916. /**
  917. * XA_STATE() - Declare an XArray operation state.
  918. * @name: Name of this operation state (usually xas).
  919. * @array: Array to operate on.
  920. * @index: Initial index of interest.
  921. *
  922. * Declare and initialise an xa_state on the stack.
  923. */
  924. #define XA_STATE(name, array, index) \
  925. struct xa_state name = __XA_STATE(array, index, 0, 0)
  926. /**
  927. * XA_STATE_ORDER() - Declare an XArray operation state.
  928. * @name: Name of this operation state (usually xas).
  929. * @array: Array to operate on.
  930. * @index: Initial index of interest.
  931. * @order: Order of entry.
  932. *
  933. * Declare and initialise an xa_state on the stack. This variant of
  934. * XA_STATE() allows you to specify the 'order' of the element you
  935. * want to operate on.`
  936. */
  937. #define XA_STATE_ORDER(name, array, index, order) \
  938. struct xa_state name = __XA_STATE(array, \
  939. (index >> order) << order, \
  940. order - (order % XA_CHUNK_SHIFT), \
  941. (1U << (order % XA_CHUNK_SHIFT)) - 1)
  942. #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
  943. #define xas_trylock(xas) xa_trylock((xas)->xa)
  944. #define xas_lock(xas) xa_lock((xas)->xa)
  945. #define xas_unlock(xas) xa_unlock((xas)->xa)
  946. #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
  947. #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
  948. #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
  949. #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
  950. #define xas_lock_irqsave(xas, flags) \
  951. xa_lock_irqsave((xas)->xa, flags)
  952. #define xas_unlock_irqrestore(xas, flags) \
  953. xa_unlock_irqrestore((xas)->xa, flags)
  954. /**
  955. * xas_error() - Return an errno stored in the xa_state.
  956. * @xas: XArray operation state.
  957. *
  958. * Return: 0 if no error has been noted. A negative errno if one has.
  959. */
  960. static inline int xas_error(const struct xa_state *xas)
  961. {
  962. return xa_err(xas->xa_node);
  963. }
  964. /**
  965. * xas_set_err() - Note an error in the xa_state.
  966. * @xas: XArray operation state.
  967. * @err: Negative error number.
  968. *
  969. * Only call this function with a negative @err; zero or positive errors
  970. * will probably not behave the way you think they should. If you want
  971. * to clear the error from an xa_state, use xas_reset().
  972. */
  973. static inline void xas_set_err(struct xa_state *xas, long err)
  974. {
  975. xas->xa_node = XA_ERROR(err);
  976. }
  977. /**
  978. * xas_invalid() - Is the xas in a retry or error state?
  979. * @xas: XArray operation state.
  980. *
  981. * Return: %true if the xas cannot be used for operations.
  982. */
  983. static inline bool xas_invalid(const struct xa_state *xas)
  984. {
  985. return (unsigned long)xas->xa_node & 3;
  986. }
  987. /**
  988. * xas_valid() - Is the xas a valid cursor into the array?
  989. * @xas: XArray operation state.
  990. *
  991. * Return: %true if the xas can be used for operations.
  992. */
  993. static inline bool xas_valid(const struct xa_state *xas)
  994. {
  995. return !xas_invalid(xas);
  996. }
  997. /**
  998. * xas_is_node() - Does the xas point to a node?
  999. * @xas: XArray operation state.
  1000. *
  1001. * Return: %true if the xas currently references a node.
  1002. */
  1003. static inline bool xas_is_node(const struct xa_state *xas)
  1004. {
  1005. return xas_valid(xas) && xas->xa_node;
  1006. }
  1007. /* True if the pointer is something other than a node */
  1008. static inline bool xas_not_node(struct xa_node *node)
  1009. {
  1010. return ((unsigned long)node & 3) || !node;
  1011. }
  1012. /* True if the node represents RESTART or an error */
  1013. static inline bool xas_frozen(struct xa_node *node)
  1014. {
  1015. return (unsigned long)node & 2;
  1016. }
  1017. /* True if the node represents head-of-tree, RESTART or BOUNDS */
  1018. static inline bool xas_top(struct xa_node *node)
  1019. {
  1020. return node <= XAS_RESTART;
  1021. }
  1022. /**
  1023. * xas_reset() - Reset an XArray operation state.
  1024. * @xas: XArray operation state.
  1025. *
  1026. * Resets the error or walk state of the @xas so future walks of the
  1027. * array will start from the root. Use this if you have dropped the
  1028. * xarray lock and want to reuse the xa_state.
  1029. *
  1030. * Context: Any context.
  1031. */
  1032. static inline void xas_reset(struct xa_state *xas)
  1033. {
  1034. xas->xa_node = XAS_RESTART;
  1035. }
  1036. /**
  1037. * xas_retry() - Retry the operation if appropriate.
  1038. * @xas: XArray operation state.
  1039. * @entry: Entry from xarray.
  1040. *
  1041. * The advanced functions may sometimes return an internal entry, such as
  1042. * a retry entry or a zero entry. This function sets up the @xas to restart
  1043. * the walk from the head of the array if needed.
  1044. *
  1045. * Context: Any context.
  1046. * Return: true if the operation needs to be retried.
  1047. */
  1048. static inline bool xas_retry(struct xa_state *xas, const void *entry)
  1049. {
  1050. if (xa_is_zero(entry))
  1051. return true;
  1052. if (!xa_is_retry(entry))
  1053. return false;
  1054. xas_reset(xas);
  1055. return true;
  1056. }
  1057. void *xas_load(struct xa_state *);
  1058. void *xas_store(struct xa_state *, void *entry);
  1059. void *xas_find(struct xa_state *, unsigned long max);
  1060. void *xas_find_conflict(struct xa_state *);
  1061. bool xas_get_mark(const struct xa_state *, xa_mark_t);
  1062. void xas_set_mark(const struct xa_state *, xa_mark_t);
  1063. void xas_clear_mark(const struct xa_state *, xa_mark_t);
  1064. void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
  1065. void xas_init_marks(const struct xa_state *);
  1066. bool xas_nomem(struct xa_state *, gfp_t);
  1067. void xas_pause(struct xa_state *);
  1068. void xas_create_range(struct xa_state *);
  1069. /**
  1070. * xas_reload() - Refetch an entry from the xarray.
  1071. * @xas: XArray operation state.
  1072. *
  1073. * Use this function to check that a previously loaded entry still has
  1074. * the same value. This is useful for the lockless pagecache lookup where
  1075. * we walk the array with only the RCU lock to protect us, lock the page,
  1076. * then check that the page hasn't moved since we looked it up.
  1077. *
  1078. * The caller guarantees that @xas is still valid. If it may be in an
  1079. * error or restart state, call xas_load() instead.
  1080. *
  1081. * Return: The entry at this location in the xarray.
  1082. */
  1083. static inline void *xas_reload(struct xa_state *xas)
  1084. {
  1085. struct xa_node *node = xas->xa_node;
  1086. if (node)
  1087. return xa_entry(xas->xa, node, xas->xa_offset);
  1088. return xa_head(xas->xa);
  1089. }
  1090. /**
  1091. * xas_set() - Set up XArray operation state for a different index.
  1092. * @xas: XArray operation state.
  1093. * @index: New index into the XArray.
  1094. *
  1095. * Move the operation state to refer to a different index. This will
  1096. * have the effect of starting a walk from the top; see xas_next()
  1097. * to move to an adjacent index.
  1098. */
  1099. static inline void xas_set(struct xa_state *xas, unsigned long index)
  1100. {
  1101. xas->xa_index = index;
  1102. xas->xa_node = XAS_RESTART;
  1103. }
  1104. /**
  1105. * xas_set_order() - Set up XArray operation state for a multislot entry.
  1106. * @xas: XArray operation state.
  1107. * @index: Target of the operation.
  1108. * @order: Entry occupies 2^@order indices.
  1109. */
  1110. static inline void xas_set_order(struct xa_state *xas, unsigned long index,
  1111. unsigned int order)
  1112. {
  1113. #ifdef CONFIG_XARRAY_MULTI
  1114. xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
  1115. xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
  1116. xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
  1117. xas->xa_node = XAS_RESTART;
  1118. #else
  1119. BUG_ON(order > 0);
  1120. xas_set(xas, index);
  1121. #endif
  1122. }
  1123. /**
  1124. * xas_set_update() - Set up XArray operation state for a callback.
  1125. * @xas: XArray operation state.
  1126. * @update: Function to call when updating a node.
  1127. *
  1128. * The XArray can notify a caller after it has updated an xa_node.
  1129. * This is advanced functionality and is only needed by the page cache.
  1130. */
  1131. static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
  1132. {
  1133. xas->xa_update = update;
  1134. }
  1135. /**
  1136. * xas_next_entry() - Advance iterator to next present entry.
  1137. * @xas: XArray operation state.
  1138. * @max: Highest index to return.
  1139. *
  1140. * xas_next_entry() is an inline function to optimise xarray traversal for
  1141. * speed. It is equivalent to calling xas_find(), and will call xas_find()
  1142. * for all the hard cases.
  1143. *
  1144. * Return: The next present entry after the one currently referred to by @xas.
  1145. */
  1146. static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
  1147. {
  1148. struct xa_node *node = xas->xa_node;
  1149. void *entry;
  1150. if (unlikely(xas_not_node(node) || node->shift ||
  1151. xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
  1152. return xas_find(xas, max);
  1153. do {
  1154. if (unlikely(xas->xa_index >= max))
  1155. return xas_find(xas, max);
  1156. if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
  1157. return xas_find(xas, max);
  1158. entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
  1159. if (unlikely(xa_is_internal(entry)))
  1160. return xas_find(xas, max);
  1161. xas->xa_offset++;
  1162. xas->xa_index++;
  1163. } while (!entry);
  1164. return entry;
  1165. }
  1166. /* Private */
  1167. static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
  1168. xa_mark_t mark)
  1169. {
  1170. unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
  1171. unsigned int offset = xas->xa_offset;
  1172. if (advance)
  1173. offset++;
  1174. if (XA_CHUNK_SIZE == BITS_PER_LONG) {
  1175. if (offset < XA_CHUNK_SIZE) {
  1176. unsigned long data = *addr & (~0UL << offset);
  1177. if (data)
  1178. return __ffs(data);
  1179. }
  1180. return XA_CHUNK_SIZE;
  1181. }
  1182. return find_next_bit(addr, XA_CHUNK_SIZE, offset);
  1183. }
  1184. /**
  1185. * xas_next_marked() - Advance iterator to next marked entry.
  1186. * @xas: XArray operation state.
  1187. * @max: Highest index to return.
  1188. * @mark: Mark to search for.
  1189. *
  1190. * xas_next_marked() is an inline function to optimise xarray traversal for
  1191. * speed. It is equivalent to calling xas_find_marked(), and will call
  1192. * xas_find_marked() for all the hard cases.
  1193. *
  1194. * Return: The next marked entry after the one currently referred to by @xas.
  1195. */
  1196. static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
  1197. xa_mark_t mark)
  1198. {
  1199. struct xa_node *node = xas->xa_node;
  1200. unsigned int offset;
  1201. if (unlikely(xas_not_node(node) || node->shift))
  1202. return xas_find_marked(xas, max, mark);
  1203. offset = xas_find_chunk(xas, true, mark);
  1204. xas->xa_offset = offset;
  1205. xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
  1206. if (xas->xa_index > max)
  1207. return NULL;
  1208. if (offset == XA_CHUNK_SIZE)
  1209. return xas_find_marked(xas, max, mark);
  1210. return xa_entry(xas->xa, node, offset);
  1211. }
  1212. /*
  1213. * If iterating while holding a lock, drop the lock and reschedule
  1214. * every %XA_CHECK_SCHED loops.
  1215. */
  1216. enum {
  1217. XA_CHECK_SCHED = 4096,
  1218. };
  1219. /**
  1220. * xas_for_each() - Iterate over a range of an XArray.
  1221. * @xas: XArray operation state.
  1222. * @entry: Entry retrieved from the array.
  1223. * @max: Maximum index to retrieve from array.
  1224. *
  1225. * The loop body will be executed for each entry present in the xarray
  1226. * between the current xas position and @max. @entry will be set to
  1227. * the entry retrieved from the xarray. It is safe to delete entries
  1228. * from the array in the loop body. You should hold either the RCU lock
  1229. * or the xa_lock while iterating. If you need to drop the lock, call
  1230. * xas_pause() first.
  1231. */
  1232. #define xas_for_each(xas, entry, max) \
  1233. for (entry = xas_find(xas, max); entry; \
  1234. entry = xas_next_entry(xas, max))
  1235. /**
  1236. * xas_for_each_marked() - Iterate over a range of an XArray.
  1237. * @xas: XArray operation state.
  1238. * @entry: Entry retrieved from the array.
  1239. * @max: Maximum index to retrieve from array.
  1240. * @mark: Mark to search for.
  1241. *
  1242. * The loop body will be executed for each marked entry in the xarray
  1243. * between the current xas position and @max. @entry will be set to
  1244. * the entry retrieved from the xarray. It is safe to delete entries
  1245. * from the array in the loop body. You should hold either the RCU lock
  1246. * or the xa_lock while iterating. If you need to drop the lock, call
  1247. * xas_pause() first.
  1248. */
  1249. #define xas_for_each_marked(xas, entry, max, mark) \
  1250. for (entry = xas_find_marked(xas, max, mark); entry; \
  1251. entry = xas_next_marked(xas, max, mark))
  1252. /**
  1253. * xas_for_each_conflict() - Iterate over a range of an XArray.
  1254. * @xas: XArray operation state.
  1255. * @entry: Entry retrieved from the array.
  1256. *
  1257. * The loop body will be executed for each entry in the XArray that lies
  1258. * within the range specified by @xas. If the loop completes successfully,
  1259. * any entries that lie in this range will be replaced by @entry. The caller
  1260. * may break out of the loop; if they do so, the contents of the XArray will
  1261. * be unchanged. The operation may fail due to an out of memory condition.
  1262. * The caller may also call xa_set_err() to exit the loop while setting an
  1263. * error to record the reason.
  1264. */
  1265. #define xas_for_each_conflict(xas, entry) \
  1266. while ((entry = xas_find_conflict(xas)))
  1267. void *__xas_next(struct xa_state *);
  1268. void *__xas_prev(struct xa_state *);
  1269. /**
  1270. * xas_prev() - Move iterator to previous index.
  1271. * @xas: XArray operation state.
  1272. *
  1273. * If the @xas was in an error state, it will remain in an error state
  1274. * and this function will return %NULL. If the @xas has never been walked,
  1275. * it will have the effect of calling xas_load(). Otherwise one will be
  1276. * subtracted from the index and the state will be walked to the correct
  1277. * location in the array for the next operation.
  1278. *
  1279. * If the iterator was referencing index 0, this function wraps
  1280. * around to %ULONG_MAX.
  1281. *
  1282. * Return: The entry at the new index. This may be %NULL or an internal
  1283. * entry.
  1284. */
  1285. static inline void *xas_prev(struct xa_state *xas)
  1286. {
  1287. struct xa_node *node = xas->xa_node;
  1288. if (unlikely(xas_not_node(node) || node->shift ||
  1289. xas->xa_offset == 0))
  1290. return __xas_prev(xas);
  1291. xas->xa_index--;
  1292. xas->xa_offset--;
  1293. return xa_entry(xas->xa, node, xas->xa_offset);
  1294. }
  1295. /**
  1296. * xas_next() - Move state to next index.
  1297. * @xas: XArray operation state.
  1298. *
  1299. * If the @xas was in an error state, it will remain in an error state
  1300. * and this function will return %NULL. If the @xas has never been walked,
  1301. * it will have the effect of calling xas_load(). Otherwise one will be
  1302. * added to the index and the state will be walked to the correct
  1303. * location in the array for the next operation.
  1304. *
  1305. * If the iterator was referencing index %ULONG_MAX, this function wraps
  1306. * around to 0.
  1307. *
  1308. * Return: The entry at the new index. This may be %NULL or an internal
  1309. * entry.
  1310. */
  1311. static inline void *xas_next(struct xa_state *xas)
  1312. {
  1313. struct xa_node *node = xas->xa_node;
  1314. if (unlikely(xas_not_node(node) || node->shift ||
  1315. xas->xa_offset == XA_CHUNK_MASK))
  1316. return __xas_next(xas);
  1317. xas->xa_index++;
  1318. xas->xa_offset++;
  1319. return xa_entry(xas->xa, node, xas->xa_offset);
  1320. }
  1321. #endif /* _LINUX_XARRAY_H */