xarray.h 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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_cmpxchg_bh() - Conditionally replace an entry in the XArray.
  511. * @xa: XArray.
  512. * @index: Index into array.
  513. * @old: Old value to test against.
  514. * @entry: New value to place in array.
  515. * @gfp: Memory allocation flags.
  516. *
  517. * This function is like calling xa_cmpxchg() except it disables softirqs
  518. * while holding the array lock.
  519. *
  520. * Context: Any context. Takes and releases the xa_lock while
  521. * disabling softirqs. May sleep if the @gfp flags permit.
  522. * Return: The old value at this index or xa_err() if an error happened.
  523. */
  524. static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
  525. void *old, void *entry, gfp_t gfp)
  526. {
  527. void *curr;
  528. xa_lock_bh(xa);
  529. curr = __xa_cmpxchg(xa, index, old, entry, gfp);
  530. xa_unlock_bh(xa);
  531. return curr;
  532. }
  533. /**
  534. * xa_cmpxchg_irq() - Conditionally replace an entry in the XArray.
  535. * @xa: XArray.
  536. * @index: Index into array.
  537. * @old: Old value to test against.
  538. * @entry: New value to place in array.
  539. * @gfp: Memory allocation flags.
  540. *
  541. * This function is like calling xa_cmpxchg() except it disables interrupts
  542. * while holding the array lock.
  543. *
  544. * Context: Process context. Takes and releases the xa_lock while
  545. * disabling interrupts. May sleep if the @gfp flags permit.
  546. * Return: The old value at this index or xa_err() if an error happened.
  547. */
  548. static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
  549. void *old, void *entry, gfp_t gfp)
  550. {
  551. void *curr;
  552. xa_lock_irq(xa);
  553. curr = __xa_cmpxchg(xa, index, old, entry, gfp);
  554. xa_unlock_irq(xa);
  555. return curr;
  556. }
  557. /**
  558. * xa_insert() - Store this entry in the XArray unless another entry is
  559. * already present.
  560. * @xa: XArray.
  561. * @index: Index into array.
  562. * @entry: New entry.
  563. * @gfp: Memory allocation flags.
  564. *
  565. * If you would rather see the existing entry in the array, use xa_cmpxchg().
  566. * This function is for users who don't care what the entry is, only that
  567. * one is present.
  568. *
  569. * Context: Process context. Takes and releases the xa_lock.
  570. * May sleep if the @gfp flags permit.
  571. * Return: 0 if the store succeeded. -EEXIST if another entry was present.
  572. * -ENOMEM if memory could not be allocated.
  573. */
  574. static inline int xa_insert(struct xarray *xa, unsigned long index,
  575. void *entry, gfp_t gfp)
  576. {
  577. void *curr = xa_cmpxchg(xa, index, NULL, entry, gfp);
  578. if (!curr)
  579. return 0;
  580. if (xa_is_err(curr))
  581. return xa_err(curr);
  582. return -EEXIST;
  583. }
  584. /**
  585. * xa_alloc() - Find somewhere to store this entry in the XArray.
  586. * @xa: XArray.
  587. * @id: Pointer to ID.
  588. * @max: Maximum ID to allocate (inclusive).
  589. * @entry: New entry.
  590. * @gfp: Memory allocation flags.
  591. *
  592. * Allocates an unused ID in the range specified by @id and @max.
  593. * Updates the @id pointer with the index, then stores the entry at that
  594. * index. A concurrent lookup will not see an uninitialised @id.
  595. *
  596. * Context: Process context. Takes and releases the xa_lock. May sleep if
  597. * the @gfp flags permit.
  598. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  599. * there is no more space in the XArray.
  600. */
  601. static inline int xa_alloc(struct xarray *xa, u32 *id, u32 max, void *entry,
  602. gfp_t gfp)
  603. {
  604. int err;
  605. xa_lock(xa);
  606. err = __xa_alloc(xa, id, max, entry, gfp);
  607. xa_unlock(xa);
  608. return err;
  609. }
  610. /**
  611. * xa_alloc_bh() - Find somewhere to store this entry in the XArray.
  612. * @xa: XArray.
  613. * @id: Pointer to ID.
  614. * @max: Maximum ID to allocate (inclusive).
  615. * @entry: New entry.
  616. * @gfp: Memory allocation flags.
  617. *
  618. * Allocates an unused ID in the range specified by @id and @max.
  619. * Updates the @id pointer with the index, then stores the entry at that
  620. * index. A concurrent lookup will not see an uninitialised @id.
  621. *
  622. * Context: Any context. Takes and releases the xa_lock while
  623. * disabling softirqs. May sleep if the @gfp flags permit.
  624. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  625. * there is no more space in the XArray.
  626. */
  627. static inline int xa_alloc_bh(struct xarray *xa, u32 *id, u32 max, void *entry,
  628. gfp_t gfp)
  629. {
  630. int err;
  631. xa_lock_bh(xa);
  632. err = __xa_alloc(xa, id, max, entry, gfp);
  633. xa_unlock_bh(xa);
  634. return err;
  635. }
  636. /**
  637. * xa_alloc_irq() - Find somewhere to store this entry in the XArray.
  638. * @xa: XArray.
  639. * @id: Pointer to ID.
  640. * @max: Maximum ID to allocate (inclusive).
  641. * @entry: New entry.
  642. * @gfp: Memory allocation flags.
  643. *
  644. * Allocates an unused ID in the range specified by @id and @max.
  645. * Updates the @id pointer with the index, then stores the entry at that
  646. * index. A concurrent lookup will not see an uninitialised @id.
  647. *
  648. * Context: Process context. Takes and releases the xa_lock while
  649. * disabling interrupts. May sleep if the @gfp flags permit.
  650. * Return: 0 on success, -ENOMEM if memory allocation fails or -ENOSPC if
  651. * there is no more space in the XArray.
  652. */
  653. static inline int xa_alloc_irq(struct xarray *xa, u32 *id, u32 max, void *entry,
  654. gfp_t gfp)
  655. {
  656. int err;
  657. xa_lock_irq(xa);
  658. err = __xa_alloc(xa, id, max, entry, gfp);
  659. xa_unlock_irq(xa);
  660. return err;
  661. }
  662. /**
  663. * xa_reserve() - Reserve this index in the XArray.
  664. * @xa: XArray.
  665. * @index: Index into array.
  666. * @gfp: Memory allocation flags.
  667. *
  668. * Ensures there is somewhere to store an entry at @index in the array.
  669. * If there is already something stored at @index, this function does
  670. * nothing. If there was nothing there, the entry is marked as reserved.
  671. * Loading from a reserved entry returns a %NULL pointer.
  672. *
  673. * If you do not use the entry that you have reserved, call xa_release()
  674. * or xa_erase() to free any unnecessary memory.
  675. *
  676. * Context: Any context. Takes and releases the xa_lock.
  677. * May sleep if the @gfp flags permit.
  678. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  679. */
  680. static inline
  681. int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
  682. {
  683. int ret;
  684. xa_lock(xa);
  685. ret = __xa_reserve(xa, index, gfp);
  686. xa_unlock(xa);
  687. return ret;
  688. }
  689. /**
  690. * xa_reserve_bh() - Reserve this index in the XArray.
  691. * @xa: XArray.
  692. * @index: Index into array.
  693. * @gfp: Memory allocation flags.
  694. *
  695. * A softirq-disabling version of xa_reserve().
  696. *
  697. * Context: Any context. Takes and releases the xa_lock while
  698. * disabling softirqs.
  699. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  700. */
  701. static inline
  702. int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
  703. {
  704. int ret;
  705. xa_lock_bh(xa);
  706. ret = __xa_reserve(xa, index, gfp);
  707. xa_unlock_bh(xa);
  708. return ret;
  709. }
  710. /**
  711. * xa_reserve_irq() - Reserve this index in the XArray.
  712. * @xa: XArray.
  713. * @index: Index into array.
  714. * @gfp: Memory allocation flags.
  715. *
  716. * An interrupt-disabling version of xa_reserve().
  717. *
  718. * Context: Process context. Takes and releases the xa_lock while
  719. * disabling interrupts.
  720. * Return: 0 if the reservation succeeded or -ENOMEM if it failed.
  721. */
  722. static inline
  723. int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
  724. {
  725. int ret;
  726. xa_lock_irq(xa);
  727. ret = __xa_reserve(xa, index, gfp);
  728. xa_unlock_irq(xa);
  729. return ret;
  730. }
  731. /**
  732. * xa_release() - Release a reserved entry.
  733. * @xa: XArray.
  734. * @index: Index of entry.
  735. *
  736. * After calling xa_reserve(), you can call this function to release the
  737. * reservation. If the entry at @index has been stored to, this function
  738. * will do nothing.
  739. */
  740. static inline void xa_release(struct xarray *xa, unsigned long index)
  741. {
  742. xa_cmpxchg(xa, index, NULL, NULL, 0);
  743. }
  744. /* Everything below here is the Advanced API. Proceed with caution. */
  745. /*
  746. * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
  747. * the best chunk size requires some tradeoffs. A power of two recommends
  748. * itself so that we can walk the tree based purely on shifts and masks.
  749. * Generally, the larger the better; as the number of slots per level of the
  750. * tree increases, the less tall the tree needs to be. But that needs to be
  751. * balanced against the memory consumption of each node. On a 64-bit system,
  752. * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
  753. * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
  754. */
  755. #ifndef XA_CHUNK_SHIFT
  756. #define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6)
  757. #endif
  758. #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
  759. #define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
  760. #define XA_MAX_MARKS 3
  761. #define XA_MARK_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG)
  762. /*
  763. * @count is the count of every non-NULL element in the ->slots array
  764. * whether that is a value entry, a retry entry, a user pointer,
  765. * a sibling entry or a pointer to the next level of the tree.
  766. * @nr_values is the count of every element in ->slots which is
  767. * either a value entry or a sibling of a value entry.
  768. */
  769. struct xa_node {
  770. unsigned char shift; /* Bits remaining in each slot */
  771. unsigned char offset; /* Slot offset in parent */
  772. unsigned char count; /* Total entry count */
  773. unsigned char nr_values; /* Value entry count */
  774. struct xa_node __rcu *parent; /* NULL at top of tree */
  775. struct xarray *array; /* The array we belong to */
  776. union {
  777. struct list_head private_list; /* For tree user */
  778. struct rcu_head rcu_head; /* Used when freeing node */
  779. };
  780. void __rcu *slots[XA_CHUNK_SIZE];
  781. union {
  782. unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS];
  783. unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS];
  784. };
  785. };
  786. void xa_dump(const struct xarray *);
  787. void xa_dump_node(const struct xa_node *);
  788. #ifdef XA_DEBUG
  789. #define XA_BUG_ON(xa, x) do { \
  790. if (x) { \
  791. xa_dump(xa); \
  792. BUG(); \
  793. } \
  794. } while (0)
  795. #define XA_NODE_BUG_ON(node, x) do { \
  796. if (x) { \
  797. if (node) xa_dump_node(node); \
  798. BUG(); \
  799. } \
  800. } while (0)
  801. #else
  802. #define XA_BUG_ON(xa, x) do { } while (0)
  803. #define XA_NODE_BUG_ON(node, x) do { } while (0)
  804. #endif
  805. /* Private */
  806. static inline void *xa_head(const struct xarray *xa)
  807. {
  808. return rcu_dereference_check(xa->xa_head,
  809. lockdep_is_held(&xa->xa_lock));
  810. }
  811. /* Private */
  812. static inline void *xa_head_locked(const struct xarray *xa)
  813. {
  814. return rcu_dereference_protected(xa->xa_head,
  815. lockdep_is_held(&xa->xa_lock));
  816. }
  817. /* Private */
  818. static inline void *xa_entry(const struct xarray *xa,
  819. const struct xa_node *node, unsigned int offset)
  820. {
  821. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  822. return rcu_dereference_check(node->slots[offset],
  823. lockdep_is_held(&xa->xa_lock));
  824. }
  825. /* Private */
  826. static inline void *xa_entry_locked(const struct xarray *xa,
  827. const struct xa_node *node, unsigned int offset)
  828. {
  829. XA_NODE_BUG_ON(node, offset >= XA_CHUNK_SIZE);
  830. return rcu_dereference_protected(node->slots[offset],
  831. lockdep_is_held(&xa->xa_lock));
  832. }
  833. /* Private */
  834. static inline struct xa_node *xa_parent(const struct xarray *xa,
  835. const struct xa_node *node)
  836. {
  837. return rcu_dereference_check(node->parent,
  838. lockdep_is_held(&xa->xa_lock));
  839. }
  840. /* Private */
  841. static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
  842. const struct xa_node *node)
  843. {
  844. return rcu_dereference_protected(node->parent,
  845. lockdep_is_held(&xa->xa_lock));
  846. }
  847. /* Private */
  848. static inline void *xa_mk_node(const struct xa_node *node)
  849. {
  850. return (void *)((unsigned long)node | 2);
  851. }
  852. /* Private */
  853. static inline struct xa_node *xa_to_node(const void *entry)
  854. {
  855. return (struct xa_node *)((unsigned long)entry - 2);
  856. }
  857. /* Private */
  858. static inline bool xa_is_node(const void *entry)
  859. {
  860. return xa_is_internal(entry) && (unsigned long)entry > 4096;
  861. }
  862. /* Private */
  863. static inline void *xa_mk_sibling(unsigned int offset)
  864. {
  865. return xa_mk_internal(offset);
  866. }
  867. /* Private */
  868. static inline unsigned long xa_to_sibling(const void *entry)
  869. {
  870. return xa_to_internal(entry);
  871. }
  872. /**
  873. * xa_is_sibling() - Is the entry a sibling entry?
  874. * @entry: Entry retrieved from the XArray
  875. *
  876. * Return: %true if the entry is a sibling entry.
  877. */
  878. static inline bool xa_is_sibling(const void *entry)
  879. {
  880. return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
  881. (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
  882. }
  883. #define XA_ZERO_ENTRY xa_mk_internal(256)
  884. #define XA_RETRY_ENTRY xa_mk_internal(257)
  885. /**
  886. * xa_is_zero() - Is the entry a zero entry?
  887. * @entry: Entry retrieved from the XArray
  888. *
  889. * Return: %true if the entry is a zero entry.
  890. */
  891. static inline bool xa_is_zero(const void *entry)
  892. {
  893. return unlikely(entry == XA_ZERO_ENTRY);
  894. }
  895. /**
  896. * xa_is_retry() - Is the entry a retry entry?
  897. * @entry: Entry retrieved from the XArray
  898. *
  899. * Return: %true if the entry is a retry entry.
  900. */
  901. static inline bool xa_is_retry(const void *entry)
  902. {
  903. return unlikely(entry == XA_RETRY_ENTRY);
  904. }
  905. /**
  906. * typedef xa_update_node_t - A callback function from the XArray.
  907. * @node: The node which is being processed
  908. *
  909. * This function is called every time the XArray updates the count of
  910. * present and value entries in a node. It allows advanced users to
  911. * maintain the private_list in the node.
  912. *
  913. * Context: The xa_lock is held and interrupts may be disabled.
  914. * Implementations should not drop the xa_lock, nor re-enable
  915. * interrupts.
  916. */
  917. typedef void (*xa_update_node_t)(struct xa_node *node);
  918. /*
  919. * The xa_state is opaque to its users. It contains various different pieces
  920. * of state involved in the current operation on the XArray. It should be
  921. * declared on the stack and passed between the various internal routines.
  922. * The various elements in it should not be accessed directly, but only
  923. * through the provided accessor functions. The below documentation is for
  924. * the benefit of those working on the code, not for users of the XArray.
  925. *
  926. * @xa_node usually points to the xa_node containing the slot we're operating
  927. * on (and @xa_offset is the offset in the slots array). If there is a
  928. * single entry in the array at index 0, there are no allocated xa_nodes to
  929. * point to, and so we store %NULL in @xa_node. @xa_node is set to
  930. * the value %XAS_RESTART if the xa_state is not walked to the correct
  931. * position in the tree of nodes for this operation. If an error occurs
  932. * during an operation, it is set to an %XAS_ERROR value. If we run off the
  933. * end of the allocated nodes, it is set to %XAS_BOUNDS.
  934. */
  935. struct xa_state {
  936. struct xarray *xa;
  937. unsigned long xa_index;
  938. unsigned char xa_shift;
  939. unsigned char xa_sibs;
  940. unsigned char xa_offset;
  941. unsigned char xa_pad; /* Helps gcc generate better code */
  942. struct xa_node *xa_node;
  943. struct xa_node *xa_alloc;
  944. xa_update_node_t xa_update;
  945. };
  946. /*
  947. * We encode errnos in the xas->xa_node. If an error has happened, we need to
  948. * drop the lock to fix it, and once we've done so the xa_state is invalid.
  949. */
  950. #define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
  951. #define XAS_BOUNDS ((struct xa_node *)1UL)
  952. #define XAS_RESTART ((struct xa_node *)3UL)
  953. #define __XA_STATE(array, index, shift, sibs) { \
  954. .xa = array, \
  955. .xa_index = index, \
  956. .xa_shift = shift, \
  957. .xa_sibs = sibs, \
  958. .xa_offset = 0, \
  959. .xa_pad = 0, \
  960. .xa_node = XAS_RESTART, \
  961. .xa_alloc = NULL, \
  962. .xa_update = NULL \
  963. }
  964. /**
  965. * XA_STATE() - Declare an XArray operation state.
  966. * @name: Name of this operation state (usually xas).
  967. * @array: Array to operate on.
  968. * @index: Initial index of interest.
  969. *
  970. * Declare and initialise an xa_state on the stack.
  971. */
  972. #define XA_STATE(name, array, index) \
  973. struct xa_state name = __XA_STATE(array, index, 0, 0)
  974. /**
  975. * XA_STATE_ORDER() - Declare an XArray operation state.
  976. * @name: Name of this operation state (usually xas).
  977. * @array: Array to operate on.
  978. * @index: Initial index of interest.
  979. * @order: Order of entry.
  980. *
  981. * Declare and initialise an xa_state on the stack. This variant of
  982. * XA_STATE() allows you to specify the 'order' of the element you
  983. * want to operate on.`
  984. */
  985. #define XA_STATE_ORDER(name, array, index, order) \
  986. struct xa_state name = __XA_STATE(array, \
  987. (index >> order) << order, \
  988. order - (order % XA_CHUNK_SHIFT), \
  989. (1U << (order % XA_CHUNK_SHIFT)) - 1)
  990. #define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
  991. #define xas_trylock(xas) xa_trylock((xas)->xa)
  992. #define xas_lock(xas) xa_lock((xas)->xa)
  993. #define xas_unlock(xas) xa_unlock((xas)->xa)
  994. #define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
  995. #define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
  996. #define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
  997. #define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
  998. #define xas_lock_irqsave(xas, flags) \
  999. xa_lock_irqsave((xas)->xa, flags)
  1000. #define xas_unlock_irqrestore(xas, flags) \
  1001. xa_unlock_irqrestore((xas)->xa, flags)
  1002. /**
  1003. * xas_error() - Return an errno stored in the xa_state.
  1004. * @xas: XArray operation state.
  1005. *
  1006. * Return: 0 if no error has been noted. A negative errno if one has.
  1007. */
  1008. static inline int xas_error(const struct xa_state *xas)
  1009. {
  1010. return xa_err(xas->xa_node);
  1011. }
  1012. /**
  1013. * xas_set_err() - Note an error in the xa_state.
  1014. * @xas: XArray operation state.
  1015. * @err: Negative error number.
  1016. *
  1017. * Only call this function with a negative @err; zero or positive errors
  1018. * will probably not behave the way you think they should. If you want
  1019. * to clear the error from an xa_state, use xas_reset().
  1020. */
  1021. static inline void xas_set_err(struct xa_state *xas, long err)
  1022. {
  1023. xas->xa_node = XA_ERROR(err);
  1024. }
  1025. /**
  1026. * xas_invalid() - Is the xas in a retry or error state?
  1027. * @xas: XArray operation state.
  1028. *
  1029. * Return: %true if the xas cannot be used for operations.
  1030. */
  1031. static inline bool xas_invalid(const struct xa_state *xas)
  1032. {
  1033. return (unsigned long)xas->xa_node & 3;
  1034. }
  1035. /**
  1036. * xas_valid() - Is the xas a valid cursor into the array?
  1037. * @xas: XArray operation state.
  1038. *
  1039. * Return: %true if the xas can be used for operations.
  1040. */
  1041. static inline bool xas_valid(const struct xa_state *xas)
  1042. {
  1043. return !xas_invalid(xas);
  1044. }
  1045. /**
  1046. * xas_is_node() - Does the xas point to a node?
  1047. * @xas: XArray operation state.
  1048. *
  1049. * Return: %true if the xas currently references a node.
  1050. */
  1051. static inline bool xas_is_node(const struct xa_state *xas)
  1052. {
  1053. return xas_valid(xas) && xas->xa_node;
  1054. }
  1055. /* True if the pointer is something other than a node */
  1056. static inline bool xas_not_node(struct xa_node *node)
  1057. {
  1058. return ((unsigned long)node & 3) || !node;
  1059. }
  1060. /* True if the node represents RESTART or an error */
  1061. static inline bool xas_frozen(struct xa_node *node)
  1062. {
  1063. return (unsigned long)node & 2;
  1064. }
  1065. /* True if the node represents head-of-tree, RESTART or BOUNDS */
  1066. static inline bool xas_top(struct xa_node *node)
  1067. {
  1068. return node <= XAS_RESTART;
  1069. }
  1070. /**
  1071. * xas_reset() - Reset an XArray operation state.
  1072. * @xas: XArray operation state.
  1073. *
  1074. * Resets the error or walk state of the @xas so future walks of the
  1075. * array will start from the root. Use this if you have dropped the
  1076. * xarray lock and want to reuse the xa_state.
  1077. *
  1078. * Context: Any context.
  1079. */
  1080. static inline void xas_reset(struct xa_state *xas)
  1081. {
  1082. xas->xa_node = XAS_RESTART;
  1083. }
  1084. /**
  1085. * xas_retry() - Retry the operation if appropriate.
  1086. * @xas: XArray operation state.
  1087. * @entry: Entry from xarray.
  1088. *
  1089. * The advanced functions may sometimes return an internal entry, such as
  1090. * a retry entry or a zero entry. This function sets up the @xas to restart
  1091. * the walk from the head of the array if needed.
  1092. *
  1093. * Context: Any context.
  1094. * Return: true if the operation needs to be retried.
  1095. */
  1096. static inline bool xas_retry(struct xa_state *xas, const void *entry)
  1097. {
  1098. if (xa_is_zero(entry))
  1099. return true;
  1100. if (!xa_is_retry(entry))
  1101. return false;
  1102. xas_reset(xas);
  1103. return true;
  1104. }
  1105. void *xas_load(struct xa_state *);
  1106. void *xas_store(struct xa_state *, void *entry);
  1107. void *xas_find(struct xa_state *, unsigned long max);
  1108. void *xas_find_conflict(struct xa_state *);
  1109. bool xas_get_mark(const struct xa_state *, xa_mark_t);
  1110. void xas_set_mark(const struct xa_state *, xa_mark_t);
  1111. void xas_clear_mark(const struct xa_state *, xa_mark_t);
  1112. void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
  1113. void xas_init_marks(const struct xa_state *);
  1114. bool xas_nomem(struct xa_state *, gfp_t);
  1115. void xas_pause(struct xa_state *);
  1116. void xas_create_range(struct xa_state *);
  1117. /**
  1118. * xas_reload() - Refetch an entry from the xarray.
  1119. * @xas: XArray operation state.
  1120. *
  1121. * Use this function to check that a previously loaded entry still has
  1122. * the same value. This is useful for the lockless pagecache lookup where
  1123. * we walk the array with only the RCU lock to protect us, lock the page,
  1124. * then check that the page hasn't moved since we looked it up.
  1125. *
  1126. * The caller guarantees that @xas is still valid. If it may be in an
  1127. * error or restart state, call xas_load() instead.
  1128. *
  1129. * Return: The entry at this location in the xarray.
  1130. */
  1131. static inline void *xas_reload(struct xa_state *xas)
  1132. {
  1133. struct xa_node *node = xas->xa_node;
  1134. if (node)
  1135. return xa_entry(xas->xa, node, xas->xa_offset);
  1136. return xa_head(xas->xa);
  1137. }
  1138. /**
  1139. * xas_set() - Set up XArray operation state for a different index.
  1140. * @xas: XArray operation state.
  1141. * @index: New index into the XArray.
  1142. *
  1143. * Move the operation state to refer to a different index. This will
  1144. * have the effect of starting a walk from the top; see xas_next()
  1145. * to move to an adjacent index.
  1146. */
  1147. static inline void xas_set(struct xa_state *xas, unsigned long index)
  1148. {
  1149. xas->xa_index = index;
  1150. xas->xa_node = XAS_RESTART;
  1151. }
  1152. /**
  1153. * xas_set_order() - Set up XArray operation state for a multislot entry.
  1154. * @xas: XArray operation state.
  1155. * @index: Target of the operation.
  1156. * @order: Entry occupies 2^@order indices.
  1157. */
  1158. static inline void xas_set_order(struct xa_state *xas, unsigned long index,
  1159. unsigned int order)
  1160. {
  1161. #ifdef CONFIG_XARRAY_MULTI
  1162. xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
  1163. xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
  1164. xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
  1165. xas->xa_node = XAS_RESTART;
  1166. #else
  1167. BUG_ON(order > 0);
  1168. xas_set(xas, index);
  1169. #endif
  1170. }
  1171. /**
  1172. * xas_set_update() - Set up XArray operation state for a callback.
  1173. * @xas: XArray operation state.
  1174. * @update: Function to call when updating a node.
  1175. *
  1176. * The XArray can notify a caller after it has updated an xa_node.
  1177. * This is advanced functionality and is only needed by the page cache.
  1178. */
  1179. static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
  1180. {
  1181. xas->xa_update = update;
  1182. }
  1183. /**
  1184. * xas_next_entry() - Advance iterator to next present entry.
  1185. * @xas: XArray operation state.
  1186. * @max: Highest index to return.
  1187. *
  1188. * xas_next_entry() is an inline function to optimise xarray traversal for
  1189. * speed. It is equivalent to calling xas_find(), and will call xas_find()
  1190. * for all the hard cases.
  1191. *
  1192. * Return: The next present entry after the one currently referred to by @xas.
  1193. */
  1194. static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
  1195. {
  1196. struct xa_node *node = xas->xa_node;
  1197. void *entry;
  1198. if (unlikely(xas_not_node(node) || node->shift ||
  1199. xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
  1200. return xas_find(xas, max);
  1201. do {
  1202. if (unlikely(xas->xa_index >= max))
  1203. return xas_find(xas, max);
  1204. if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
  1205. return xas_find(xas, max);
  1206. entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
  1207. if (unlikely(xa_is_internal(entry)))
  1208. return xas_find(xas, max);
  1209. xas->xa_offset++;
  1210. xas->xa_index++;
  1211. } while (!entry);
  1212. return entry;
  1213. }
  1214. /* Private */
  1215. static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
  1216. xa_mark_t mark)
  1217. {
  1218. unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
  1219. unsigned int offset = xas->xa_offset;
  1220. if (advance)
  1221. offset++;
  1222. if (XA_CHUNK_SIZE == BITS_PER_LONG) {
  1223. if (offset < XA_CHUNK_SIZE) {
  1224. unsigned long data = *addr & (~0UL << offset);
  1225. if (data)
  1226. return __ffs(data);
  1227. }
  1228. return XA_CHUNK_SIZE;
  1229. }
  1230. return find_next_bit(addr, XA_CHUNK_SIZE, offset);
  1231. }
  1232. /**
  1233. * xas_next_marked() - Advance iterator to next marked entry.
  1234. * @xas: XArray operation state.
  1235. * @max: Highest index to return.
  1236. * @mark: Mark to search for.
  1237. *
  1238. * xas_next_marked() is an inline function to optimise xarray traversal for
  1239. * speed. It is equivalent to calling xas_find_marked(), and will call
  1240. * xas_find_marked() for all the hard cases.
  1241. *
  1242. * Return: The next marked entry after the one currently referred to by @xas.
  1243. */
  1244. static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
  1245. xa_mark_t mark)
  1246. {
  1247. struct xa_node *node = xas->xa_node;
  1248. unsigned int offset;
  1249. if (unlikely(xas_not_node(node) || node->shift))
  1250. return xas_find_marked(xas, max, mark);
  1251. offset = xas_find_chunk(xas, true, mark);
  1252. xas->xa_offset = offset;
  1253. xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
  1254. if (xas->xa_index > max)
  1255. return NULL;
  1256. if (offset == XA_CHUNK_SIZE)
  1257. return xas_find_marked(xas, max, mark);
  1258. return xa_entry(xas->xa, node, offset);
  1259. }
  1260. /*
  1261. * If iterating while holding a lock, drop the lock and reschedule
  1262. * every %XA_CHECK_SCHED loops.
  1263. */
  1264. enum {
  1265. XA_CHECK_SCHED = 4096,
  1266. };
  1267. /**
  1268. * xas_for_each() - Iterate over a range of an XArray.
  1269. * @xas: XArray operation state.
  1270. * @entry: Entry retrieved from the array.
  1271. * @max: Maximum index to retrieve from array.
  1272. *
  1273. * The loop body will be executed for each entry present in the xarray
  1274. * between the current xas position and @max. @entry will be set to
  1275. * the entry retrieved from the xarray. It is safe to delete entries
  1276. * from the array in the loop body. You should hold either the RCU lock
  1277. * or the xa_lock while iterating. If you need to drop the lock, call
  1278. * xas_pause() first.
  1279. */
  1280. #define xas_for_each(xas, entry, max) \
  1281. for (entry = xas_find(xas, max); entry; \
  1282. entry = xas_next_entry(xas, max))
  1283. /**
  1284. * xas_for_each_marked() - Iterate over a range of an XArray.
  1285. * @xas: XArray operation state.
  1286. * @entry: Entry retrieved from the array.
  1287. * @max: Maximum index to retrieve from array.
  1288. * @mark: Mark to search for.
  1289. *
  1290. * The loop body will be executed for each marked entry in the xarray
  1291. * between the current xas position and @max. @entry will be set to
  1292. * the entry retrieved from the xarray. It is safe to delete entries
  1293. * from the array in the loop body. You should hold either the RCU lock
  1294. * or the xa_lock while iterating. If you need to drop the lock, call
  1295. * xas_pause() first.
  1296. */
  1297. #define xas_for_each_marked(xas, entry, max, mark) \
  1298. for (entry = xas_find_marked(xas, max, mark); entry; \
  1299. entry = xas_next_marked(xas, max, mark))
  1300. /**
  1301. * xas_for_each_conflict() - Iterate over a range of an XArray.
  1302. * @xas: XArray operation state.
  1303. * @entry: Entry retrieved from the array.
  1304. *
  1305. * The loop body will be executed for each entry in the XArray that lies
  1306. * within the range specified by @xas. If the loop completes successfully,
  1307. * any entries that lie in this range will be replaced by @entry. The caller
  1308. * may break out of the loop; if they do so, the contents of the XArray will
  1309. * be unchanged. The operation may fail due to an out of memory condition.
  1310. * The caller may also call xa_set_err() to exit the loop while setting an
  1311. * error to record the reason.
  1312. */
  1313. #define xas_for_each_conflict(xas, entry) \
  1314. while ((entry = xas_find_conflict(xas)))
  1315. void *__xas_next(struct xa_state *);
  1316. void *__xas_prev(struct xa_state *);
  1317. /**
  1318. * xas_prev() - Move iterator to previous index.
  1319. * @xas: XArray operation state.
  1320. *
  1321. * If the @xas was in an error state, it will remain in an error state
  1322. * and this function will return %NULL. If the @xas has never been walked,
  1323. * it will have the effect of calling xas_load(). Otherwise one will be
  1324. * subtracted from the index and the state will be walked to the correct
  1325. * location in the array for the next operation.
  1326. *
  1327. * If the iterator was referencing index 0, this function wraps
  1328. * around to %ULONG_MAX.
  1329. *
  1330. * Return: The entry at the new index. This may be %NULL or an internal
  1331. * entry.
  1332. */
  1333. static inline void *xas_prev(struct xa_state *xas)
  1334. {
  1335. struct xa_node *node = xas->xa_node;
  1336. if (unlikely(xas_not_node(node) || node->shift ||
  1337. xas->xa_offset == 0))
  1338. return __xas_prev(xas);
  1339. xas->xa_index--;
  1340. xas->xa_offset--;
  1341. return xa_entry(xas->xa, node, xas->xa_offset);
  1342. }
  1343. /**
  1344. * xas_next() - Move state to next index.
  1345. * @xas: XArray operation state.
  1346. *
  1347. * If the @xas was in an error state, it will remain in an error state
  1348. * and this function will return %NULL. If the @xas has never been walked,
  1349. * it will have the effect of calling xas_load(). Otherwise one will be
  1350. * added to the index and the state will be walked to the correct
  1351. * location in the array for the next operation.
  1352. *
  1353. * If the iterator was referencing index %ULONG_MAX, this function wraps
  1354. * around to 0.
  1355. *
  1356. * Return: The entry at the new index. This may be %NULL or an internal
  1357. * entry.
  1358. */
  1359. static inline void *xas_next(struct xa_state *xas)
  1360. {
  1361. struct xa_node *node = xas->xa_node;
  1362. if (unlikely(xas_not_node(node) || node->shift ||
  1363. xas->xa_offset == XA_CHUNK_MASK))
  1364. return __xas_next(xas);
  1365. xas->xa_index++;
  1366. xas->xa_offset++;
  1367. return xa_entry(xas->xa, node, xas->xa_offset);
  1368. }
  1369. #endif /* _LINUX_XARRAY_H */