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