XenevaOS
Loading...
Searching...
No Matches
xarray.h
Go to the documentation of this file.
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
12#include <linux/bitmap.h>
13#include <linux/bug.h>
14#include <linux/compiler.h>
15#include <linux/err.h>
16#include <linux/gfp.h>
17#include <linux/kconfig.h>
18#include <linux/limits.h>
19#include <linux/lockdep.h>
20#include <linux/rcupdate.h>
21#include <linux/sched/mm.h>
22#include <linux/spinlock.h>
23#include <linux/types.h>
24
25struct list_lru;
26
27/*
28 * The bottom two bits of the entry determine how the XArray interprets
29 * the contents:
30 *
31 * 00: Pointer entry
32 * 10: Internal entry
33 * x1: Value entry or tagged pointer
34 *
35 * Attempting to store internal entries in the XArray is a bug.
36 *
37 * Most internal entries are pointers to the next node in the tree.
38 * The following internal entries have a special meaning:
39 *
40 * 0-62: Sibling entries
41 * 256: Retry entry
42 * 257: Zero entry
43 *
44 * Errors are also represented as internal entries, but use the negative
45 * space (-4094 to -2). They're never stored in the slots array; only
46 * returned by the normal API.
47 */
48
49#define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
50
58static inline void *xa_mk_value(unsigned long v)
59{
60 WARN_ON((long)v < 0);
61 return (void *)((v << 1) | 1);
62}
63
71static inline unsigned long xa_to_value(const void *entry)
72{
73 return (unsigned long)entry >> 1;
74}
75
83static inline bool xa_is_value(const void *entry)
84{
85 return (unsigned long)entry & 1;
86}
87
101/*
102 * DCL delta -- `uintptr_t` where mainline writes `unsigned long`.
103 *
104 * A tagged entry is a real address with its tag in the bottom two bits, so
105 * the integer in the middle of the round trip has to be pointer width.
106 * mainline gets that from `unsigned long` being 64 bits on arm64; this
107 * target is aarch64-unknown-windows, which is LLP64 (verified:
108 * sizeof(long) == 4), so mainline's spelling keeps the address's low 32
109 * bits and hands back a pointer into the low 4 GB that nothing maps.
110 * xa_tag_pointer()/xa_untag_pointer() here, and xa_mk_node()/xa_to_node()
111 * further down, are the two round-trip pairs; xa_is_node() compares a whole
112 * entry, so it widens too. `~(uintptr_t)3` rather than mainline's `~3UL`
113 * because the 32-bit mask would clear out the address half again.
114 *
115 * The casts that only read bits stay `unsigned long`: the tag in
116 * xa_pointer_tag(), xa_is_internal() and xa_is_value() lives in the low
117 * bits at either width, and the value entries (xa_mk_value()) are not
118 * addresses.
119 */
120static inline void *xa_tag_pointer(void *p, unsigned long tag)
121{
122 return (void *)((uintptr_t)p | tag);
123}
124
135static inline void *xa_untag_pointer(void *entry)
136{
137 return (void *)((uintptr_t)entry & ~(uintptr_t)3);
138}
139
150static inline unsigned int xa_pointer_tag(void *entry)
151{
152 return (unsigned long)entry & 3UL;
153}
154
155/*
156 * xa_mk_internal() - Create an internal entry.
157 * @v: Value to turn into an internal entry.
158 *
159 * Internal entries are used for a number of purposes. Entries 0-255 are
160 * used for sibling entries (only 0-62 are used by the current code). 256
161 * is used for the retry entry. 257 is used for the reserved / zero entry.
162 * Negative internal entries are used to represent errnos. Node pointers
163 * are also tagged as internal entries in some situations.
164 *
165 * Context: Any context.
166 * Return: An XArray internal entry corresponding to this value.
167 */
168static inline void *xa_mk_internal(unsigned long v)
169{
170 return (void *)((v << 2) | 2);
171}
172
173/*
174 * xa_to_internal() - Extract the value from an internal entry.
175 * @entry: XArray entry.
176 *
177 * Context: Any context.
178 * Return: The value which was stored in the internal entry.
179 */
180static inline unsigned long xa_to_internal(const void *entry)
181{
182 return (unsigned long)entry >> 2;
183}
184
185/*
186 * xa_is_internal() - Is the entry an internal entry?
187 * @entry: XArray entry.
188 *
189 * Context: Any context.
190 * Return: %true if the entry is an internal entry.
191 */
192static inline bool xa_is_internal(const void *entry)
193{
194 return ((unsigned long)entry & 3) == 2;
195}
196
197#define XA_ZERO_ENTRY xa_mk_internal(257)
198
208static inline bool xa_is_zero(const void *entry)
209{
210 return unlikely(entry == XA_ZERO_ENTRY);
211}
212
224static inline bool xa_is_err(const void *entry)
225{
226 return unlikely(xa_is_internal(entry) &&
227 entry >= xa_mk_internal(-MAX_ERRNO));
228}
229
242static inline int xa_err(void *entry)
243{
244 /* xa_to_internal() would not do sign extension. */
245 if (xa_is_err(entry))
246 return (long)entry >> 2;
247 return 0;
248}
249
262struct xa_limit {
265};
266
267#define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max }
268
269#define xa_limit_32b XA_LIMIT(0, UINT_MAX)
270#define xa_limit_31b XA_LIMIT(0, INT_MAX)
271#define xa_limit_16b XA_LIMIT(0, USHRT_MAX)
272
273typedef unsigned __bitwise xa_mark_t;
274#define XA_MARK_0 ((__force xa_mark_t)0U)
275#define XA_MARK_1 ((__force xa_mark_t)1U)
276#define XA_MARK_2 ((__force xa_mark_t)2U)
277#define XA_PRESENT ((__force xa_mark_t)8U)
278#define XA_MARK_MAX XA_MARK_2
279#define XA_FREE_MARK XA_MARK_0
280
285
286/*
287 * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags,
288 * and we remain compatible with that.
289 */
290#define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ)
291#define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)
292#define XA_FLAGS_TRACK_FREE ((__force gfp_t)4U)
293#define XA_FLAGS_ZERO_BUSY ((__force gfp_t)8U)
294#define XA_FLAGS_ALLOC_WRAPPED ((__force gfp_t)16U)
295#define XA_FLAGS_ACCOUNT ((__force gfp_t)32U)
296#define XA_FLAGS_MARK(mark) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \
297 (__force unsigned)(mark)))
298
299/* ALLOC is for a normal 0-based alloc. ALLOC1 is for an 1-based alloc */
300#define XA_FLAGS_ALLOC (XA_FLAGS_TRACK_FREE | XA_FLAGS_MARK(XA_FREE_MARK))
301#define XA_FLAGS_ALLOC1 (XA_FLAGS_TRACK_FREE | XA_FLAGS_ZERO_BUSY)
302
313/*
314 * If all of the entries in the array are NULL, @xa_head is a NULL pointer.
315 * If the only non-NULL entry in the array is at index 0, @xa_head is that
316 * entry. If any other entry in the array is non-NULL, @xa_head points
317 * to an @xa_node.
318 */
319struct xarray {
321/* private: The rest of the data structure is not to be used directly. */
324};
325
326#define XARRAY_INIT(name, flags) { \
327 .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
328 .xa_flags = flags, \
329 .xa_head = NULL, \
330}
331
342#define DEFINE_XARRAY_FLAGS(name, flags) \
343 struct xarray name = XARRAY_INIT(name, flags)
344
354#define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
355
363#define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
364
372#define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1)
373
374void *xa_load(struct xarray *, unsigned long index);
375void *xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
376void *xa_erase(struct xarray *, unsigned long index);
377void *xa_store_range(struct xarray *, unsigned long first, unsigned long last,
378 void *entry, gfp_t);
379bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t);
380void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
381void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
382void *xa_find(struct xarray *xa, unsigned long *index,
383 unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
384void *xa_find_after(struct xarray *xa, unsigned long *index,
385 unsigned long max, xa_mark_t) __attribute__((nonnull(2)));
386unsigned int xa_extract(struct xarray *, void **dst, unsigned long start,
387 unsigned long max, unsigned int n, xa_mark_t);
388void xa_destroy(struct xarray *);
389
401static inline void xa_init_flags(struct xarray *xa, gfp_t flags)
402{
404 xa->xa_flags = flags;
405 xa->xa_head = NULL;
406}
407
416static inline void xa_init(struct xarray *xa)
417{
418 xa_init_flags(xa, 0);
419}
420
428static inline bool xa_empty(const struct xarray *xa)
429{
430 return xa->xa_head == NULL;
431}
432
441static inline bool xa_marked(const struct xarray *xa, xa_mark_t mark)
442{
443 return xa->xa_flags & XA_FLAGS_MARK(mark);
444}
445
470#define xa_for_each_range(xa, index, entry, start, last) \
471 for (index = start, \
472 entry = xa_find(xa, &index, last, XA_PRESENT); \
473 entry; \
474 entry = xa_find_after(xa, &index, last, XA_PRESENT))
475
499#define xa_for_each_start(xa, index, entry, start) \
500 xa_for_each_range(xa, index, entry, start, ULONG_MAX)
501
523#define xa_for_each(xa, index, entry) \
524 xa_for_each_start(xa, index, entry, 0)
525
550#define xa_for_each_marked(xa, index, entry, filter) \
551 for (index = 0, entry = xa_find(xa, &index, ULONG_MAX, filter); \
552 entry; entry = xa_find_after(xa, &index, ULONG_MAX, filter))
553
554#define xa_trylock(xa) spin_trylock(&(xa)->xa_lock)
555#define xa_lock(xa) spin_lock(&(xa)->xa_lock)
556#define xa_unlock(xa) spin_unlock(&(xa)->xa_lock)
557#define xa_lock_bh(xa) spin_lock_bh(&(xa)->xa_lock)
558#define xa_unlock_bh(xa) spin_unlock_bh(&(xa)->xa_lock)
559#define xa_lock_irq(xa) spin_lock_irq(&(xa)->xa_lock)
560#define xa_unlock_irq(xa) spin_unlock_irq(&(xa)->xa_lock)
561#define xa_lock_irqsave(xa, flags) \
562 spin_lock_irqsave(&(xa)->xa_lock, flags)
563#define xa_unlock_irqrestore(xa, flags) \
564 spin_unlock_irqrestore(&(xa)->xa_lock, flags)
565#define xa_lock_nested(xa, subclass) \
566 spin_lock_nested(&(xa)->xa_lock, subclass)
567#define xa_lock_bh_nested(xa, subclass) \
568 spin_lock_bh_nested(&(xa)->xa_lock, subclass)
569#define xa_lock_irq_nested(xa, subclass) \
570 spin_lock_irq_nested(&(xa)->xa_lock, subclass)
571#define xa_lock_irqsave_nested(xa, flags, subclass) \
572 spin_lock_irqsave_nested(&(xa)->xa_lock, flags, subclass)
573
574/*
575 * Versions of the normal API which require the caller to hold the
576 * xa_lock. If the GFP flags allow it, they will drop the lock to
577 * allocate memory, then reacquire it afterwards. These functions
578 * may also re-enable interrupts if the XArray flags indicate the
579 * locking should be interrupt safe.
580 */
581void *__xa_erase(struct xarray *, unsigned long index);
582void *__xa_store(struct xarray *, unsigned long index, void *entry, gfp_t);
583void *__xa_cmpxchg(struct xarray *, unsigned long index, void *old,
584 void *entry, gfp_t);
585int __must_check __xa_insert(struct xarray *, unsigned long index,
586 void *entry, gfp_t);
587int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry,
588 struct xa_limit, gfp_t);
590 struct xa_limit, u32 *next, gfp_t);
591void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t);
592void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t);
593
608static inline void *xa_store_bh(struct xarray *xa, unsigned long index,
609 void *entry, gfp_t gfp)
610{
611 void *curr;
612
613 might_alloc(gfp);
614 xa_lock_bh(xa);
615 curr = __xa_store(xa, index, entry, gfp);
616 xa_unlock_bh(xa);
617
618 return curr;
619}
620
635static inline void *xa_store_irq(struct xarray *xa, unsigned long index,
636 void *entry, gfp_t gfp)
637{
638 void *curr;
639
640 might_alloc(gfp);
641 xa_lock_irq(xa);
642 curr = __xa_store(xa, index, entry, gfp);
643 xa_unlock_irq(xa);
644
645 return curr;
646}
647
661static inline void *xa_erase_bh(struct xarray *xa, unsigned long index)
662{
663 void *entry;
664
665 xa_lock_bh(xa);
666 entry = __xa_erase(xa, index);
667 xa_unlock_bh(xa);
668
669 return entry;
670}
671
685static inline void *xa_erase_irq(struct xarray *xa, unsigned long index)
686{
687 void *entry;
688
689 xa_lock_irq(xa);
690 entry = __xa_erase(xa, index);
691 xa_unlock_irq(xa);
692
693 return entry;
694}
695
711static inline void *xa_cmpxchg(struct xarray *xa, unsigned long index,
712 void *old, void *entry, gfp_t gfp)
713{
714 void *curr;
715
716 might_alloc(gfp);
717 xa_lock(xa);
718 curr = __xa_cmpxchg(xa, index, old, entry, gfp);
719 xa_unlock(xa);
720
721 return curr;
722}
723
739static inline void *xa_cmpxchg_bh(struct xarray *xa, unsigned long index,
740 void *old, void *entry, gfp_t gfp)
741{
742 void *curr;
743
744 might_alloc(gfp);
745 xa_lock_bh(xa);
746 curr = __xa_cmpxchg(xa, index, old, entry, gfp);
747 xa_unlock_bh(xa);
748
749 return curr;
750}
751
767static inline void *xa_cmpxchg_irq(struct xarray *xa, unsigned long index,
768 void *old, void *entry, gfp_t gfp)
769{
770 void *curr;
771
772 might_alloc(gfp);
773 xa_lock_irq(xa);
774 curr = __xa_cmpxchg(xa, index, old, entry, gfp);
775 xa_unlock_irq(xa);
776
777 return curr;
778}
779
797static inline int __must_check xa_insert(struct xarray *xa,
798 unsigned long index, void *entry, gfp_t gfp)
799{
800 int err;
801
802 might_alloc(gfp);
803 xa_lock(xa);
804 err = __xa_insert(xa, index, entry, gfp);
805 xa_unlock(xa);
806
807 return err;
808}
809
827static inline int __must_check xa_insert_bh(struct xarray *xa,
828 unsigned long index, void *entry, gfp_t gfp)
829{
830 int err;
831
832 might_alloc(gfp);
833 xa_lock_bh(xa);
834 err = __xa_insert(xa, index, entry, gfp);
835 xa_unlock_bh(xa);
836
837 return err;
838}
839
857static inline int __must_check xa_insert_irq(struct xarray *xa,
858 unsigned long index, void *entry, gfp_t gfp)
859{
860 int err;
861
862 might_alloc(gfp);
863 xa_lock_irq(xa);
864 err = __xa_insert(xa, index, entry, gfp);
865 xa_unlock_irq(xa);
866
867 return err;
868}
869
890static inline __must_check int xa_alloc(struct xarray *xa, u32 *id,
891 void *entry, struct xa_limit limit, gfp_t gfp)
892{
893 int err;
894
895 might_alloc(gfp);
896 xa_lock(xa);
897 err = __xa_alloc(xa, id, entry, limit, gfp);
898 xa_unlock(xa);
899
900 return err;
901}
902
923static inline int __must_check xa_alloc_bh(struct xarray *xa, u32 *id,
924 void *entry, struct xa_limit limit, gfp_t gfp)
925{
926 int err;
927
928 might_alloc(gfp);
929 xa_lock_bh(xa);
930 err = __xa_alloc(xa, id, entry, limit, gfp);
931 xa_unlock_bh(xa);
932
933 return err;
934}
935
956static inline int __must_check xa_alloc_irq(struct xarray *xa, u32 *id,
957 void *entry, struct xa_limit limit, gfp_t gfp)
958{
959 int err;
960
961 might_alloc(gfp);
962 xa_lock_irq(xa);
963 err = __xa_alloc(xa, id, entry, limit, gfp);
964 xa_unlock_irq(xa);
965
966 return err;
967}
968
995static inline int xa_alloc_cyclic(struct xarray *xa, u32 *id, void *entry,
996 struct xa_limit limit, u32 *next, gfp_t gfp)
997{
998 int err;
999
1000 might_alloc(gfp);
1001 xa_lock(xa);
1002 err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
1003 xa_unlock(xa);
1004
1005 return err < 0 ? err : 0;
1006}
1007
1034static inline int xa_alloc_cyclic_bh(struct xarray *xa, u32 *id, void *entry,
1035 struct xa_limit limit, u32 *next, gfp_t gfp)
1036{
1037 int err;
1038
1039 might_alloc(gfp);
1040 xa_lock_bh(xa);
1041 err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
1042 xa_unlock_bh(xa);
1043
1044 return err < 0 ? err : 0;
1045}
1046
1073static inline int xa_alloc_cyclic_irq(struct xarray *xa, u32 *id, void *entry,
1074 struct xa_limit limit, u32 *next, gfp_t gfp)
1075{
1076 int err;
1077
1078 might_alloc(gfp);
1079 xa_lock_irq(xa);
1080 err = __xa_alloc_cyclic(xa, id, entry, limit, next, gfp);
1081 xa_unlock_irq(xa);
1082
1083 return err < 0 ? err : 0;
1084}
1085
1104static inline __must_check
1105int xa_reserve(struct xarray *xa, unsigned long index, gfp_t gfp)
1106{
1107 return xa_err(xa_cmpxchg(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1108}
1109
1122static inline __must_check
1123int xa_reserve_bh(struct xarray *xa, unsigned long index, gfp_t gfp)
1124{
1125 return xa_err(xa_cmpxchg_bh(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1126}
1127
1140static inline __must_check
1141int xa_reserve_irq(struct xarray *xa, unsigned long index, gfp_t gfp)
1142{
1143 return xa_err(xa_cmpxchg_irq(xa, index, NULL, XA_ZERO_ENTRY, gfp));
1144}
1145
1155static inline void xa_release(struct xarray *xa, unsigned long index)
1156{
1157 xa_cmpxchg(xa, index, XA_ZERO_ENTRY, NULL, 0);
1158}
1159
1160/* Everything below here is the Advanced API. Proceed with caution. */
1161
1162/*
1163 * The xarray is constructed out of a set of 'chunks' of pointers. Choosing
1164 * the best chunk size requires some tradeoffs. A power of two recommends
1165 * itself so that we can walk the tree based purely on shifts and masks.
1166 * Generally, the larger the better; as the number of slots per level of the
1167 * tree increases, the less tall the tree needs to be. But that needs to be
1168 * balanced against the memory consumption of each node. On a 64-bit system,
1169 * xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we
1170 * doubled the number of slots per node, we'd get only 3 nodes per 4kB page.
1171 */
1172#ifndef XA_CHUNK_SHIFT
1173#define XA_CHUNK_SHIFT (IS_ENABLED(CONFIG_BASE_SMALL) ? 4 : 6)
1174#endif
1175#define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)
1176#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1)
1177#define XA_MAX_MARKS 3
1178#define XA_MARK_LONGS BITS_TO_LONGS(XA_CHUNK_SIZE)
1179
1180/*
1181 * @count is the count of every non-NULL element in the ->slots array
1182 * whether that is a value entry, a retry entry, a user pointer,
1183 * a sibling entry or a pointer to the next level of the tree.
1184 * @nr_values is the count of every element in ->slots which is
1185 * either a value entry or a sibling of a value entry.
1186 */
1187struct xa_node {
1188 unsigned char shift; /* Bits remaining in each slot */
1189 unsigned char offset; /* Slot offset in parent */
1190 unsigned char count; /* Total entry count */
1191 unsigned char nr_values; /* Value entry count */
1192 struct xa_node __rcu *parent; /* NULL at top of tree */
1193 struct xarray *array; /* The array we belong to */
1194 union {
1195 struct list_head private_list; /* For tree user */
1196 struct rcu_head rcu_head; /* Used when freeing node */
1197 };
1199 union {
1202 };
1203};
1204
1205void xa_dump(const struct xarray *);
1206void xa_dump_node(const struct xa_node *);
1207
1208#ifdef XA_DEBUG
1209#define XA_BUG_ON(xa, x) do { \
1210 if (x) { \
1211 xa_dump(xa); \
1212 BUG(); \
1213 } \
1214 } while (0)
1215#define XA_NODE_BUG_ON(node, x) do { \
1216 if (x) { \
1217 if (node) xa_dump_node(node); \
1218 BUG(); \
1219 } \
1220 } while (0)
1221#else
1222#define XA_BUG_ON(xa, x) do { } while (0)
1223#define XA_NODE_BUG_ON(node, x) do { } while (0)
1224#endif
1225
1226/* Private */
1227static inline void *xa_head(const struct xarray *xa)
1228{
1229 return rcu_dereference_check(xa->xa_head,
1230 lockdep_is_held(&xa->xa_lock));
1231}
1232
1233/* Private */
1234static inline void *xa_head_locked(const struct xarray *xa)
1235{
1237 lockdep_is_held(&xa->xa_lock));
1238}
1239
1240/* Private */
1241static inline void *xa_entry(const struct xarray *xa,
1242 const struct xa_node *node, unsigned int offset)
1243{
1245 return rcu_dereference_check(node->slots[offset],
1246 lockdep_is_held(&xa->xa_lock));
1247}
1248
1249/* Private */
1250static inline void *xa_entry_locked(const struct xarray *xa,
1251 const struct xa_node *node, unsigned int offset)
1252{
1254 return rcu_dereference_protected(node->slots[offset],
1255 lockdep_is_held(&xa->xa_lock));
1256}
1257
1258/* Private */
1259static inline struct xa_node *xa_parent(const struct xarray *xa,
1260 const struct xa_node *node)
1261{
1262 return rcu_dereference_check(node->parent,
1263 lockdep_is_held(&xa->xa_lock));
1264}
1265
1266/* Private */
1267static inline struct xa_node *xa_parent_locked(const struct xarray *xa,
1268 const struct xa_node *node)
1269{
1270 return rcu_dereference_protected(node->parent,
1271 lockdep_is_held(&xa->xa_lock));
1272}
1273
1274/* Private */
1275static inline void *xa_mk_node(const struct xa_node *node)
1276{
1277 return (void *)((uintptr_t)node | 2);
1278}
1279
1280/* Private */
1281static inline struct xa_node *xa_to_node(const void *entry)
1282{
1283 return (struct xa_node *)((uintptr_t)entry - 2);
1284}
1285
1286/* Private */
1287static inline bool xa_is_node(const void *entry)
1288{
1289 return xa_is_internal(entry) && (uintptr_t)entry > 4096;
1290}
1291
1292/* Private */
1293static inline void *xa_mk_sibling(unsigned int offset)
1294{
1295 return xa_mk_internal(offset);
1296}
1297
1298/* Private */
1299static inline unsigned long xa_to_sibling(const void *entry)
1300{
1301 return xa_to_internal(entry);
1302}
1303
1310static inline bool xa_is_sibling(const void *entry)
1311{
1312 return IS_ENABLED(CONFIG_XARRAY_MULTI) && xa_is_internal(entry) &&
1313 (entry < xa_mk_sibling(XA_CHUNK_SIZE - 1));
1314}
1315
1316#define XA_RETRY_ENTRY xa_mk_internal(256)
1317
1324static inline bool xa_is_retry(const void *entry)
1325{
1326 return unlikely(entry == XA_RETRY_ENTRY);
1327}
1328
1335static inline bool xa_is_advanced(const void *entry)
1336{
1337 return xa_is_internal(entry) && (entry <= XA_RETRY_ENTRY);
1338}
1339
1352typedef void (*xa_update_node_t)(struct xa_node *node);
1353
1355
1356/*
1357 * The xa_state is opaque to its users. It contains various different pieces
1358 * of state involved in the current operation on the XArray. It should be
1359 * declared on the stack and passed between the various internal routines.
1360 * The various elements in it should not be accessed directly, but only
1361 * through the provided accessor functions. The below documentation is for
1362 * the benefit of those working on the code, not for users of the XArray.
1363 *
1364 * @xa_node usually points to the xa_node containing the slot we're operating
1365 * on (and @xa_offset is the offset in the slots array). If there is a
1366 * single entry in the array at index 0, there are no allocated xa_nodes to
1367 * point to, and so we store %NULL in @xa_node. @xa_node is set to
1368 * the value %XAS_RESTART if the xa_state is not walked to the correct
1369 * position in the tree of nodes for this operation. If an error occurs
1370 * during an operation, it is set to an %XAS_ERROR value. If we run off the
1371 * end of the allocated nodes, it is set to %XAS_BOUNDS.
1372 */
1373struct xa_state {
1374 struct xarray *xa;
1375 unsigned long xa_index;
1376 unsigned char xa_shift;
1377 unsigned char xa_sibs;
1378 unsigned char xa_offset;
1379 unsigned char xa_pad; /* Helps gcc generate better code */
1383 struct list_lru *xa_lru;
1384};
1385
1386/*
1387 * We encode errnos in the xas->xa_node. If an error has happened, we need to
1388 * drop the lock to fix it, and once we've done so the xa_state is invalid.
1389 */
1390#define XA_ERROR(errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL))
1391#define XAS_BOUNDS ((struct xa_node *)1UL)
1392#define XAS_RESTART ((struct xa_node *)3UL)
1393
1394#define __XA_STATE(array, index, shift, sibs) { \
1395 .xa = array, \
1396 .xa_index = index, \
1397 .xa_shift = shift, \
1398 .xa_sibs = sibs, \
1399 .xa_offset = 0, \
1400 .xa_pad = 0, \
1401 .xa_node = XAS_RESTART, \
1402 .xa_alloc = NULL, \
1403 .xa_update = NULL, \
1404 .xa_lru = NULL, \
1405}
1406
1415#define XA_STATE(name, array, index) \
1416 struct xa_state name = __XA_STATE(array, index, 0, 0)
1417
1429#define XA_STATE_ORDER(name, array, index, order) \
1430 struct xa_state name = __XA_STATE(array, \
1431 (index >> order) << order, \
1432 order - (order % XA_CHUNK_SHIFT), \
1433 (1U << (order % XA_CHUNK_SHIFT)) - 1)
1434
1435#define xas_marked(xas, mark) xa_marked((xas)->xa, (mark))
1436#define xas_trylock(xas) xa_trylock((xas)->xa)
1437#define xas_lock(xas) xa_lock((xas)->xa)
1438#define xas_unlock(xas) xa_unlock((xas)->xa)
1439#define xas_lock_bh(xas) xa_lock_bh((xas)->xa)
1440#define xas_unlock_bh(xas) xa_unlock_bh((xas)->xa)
1441#define xas_lock_irq(xas) xa_lock_irq((xas)->xa)
1442#define xas_unlock_irq(xas) xa_unlock_irq((xas)->xa)
1443#define xas_lock_irqsave(xas, flags) \
1444 xa_lock_irqsave((xas)->xa, flags)
1445#define xas_unlock_irqrestore(xas, flags) \
1446 xa_unlock_irqrestore((xas)->xa, flags)
1447
1454static inline int xas_error(const struct xa_state *xas)
1455{
1456 return xa_err(xas->xa_node);
1457}
1458
1468static inline void xas_set_err(struct xa_state *xas, long err)
1469{
1470 xas->xa_node = XA_ERROR(err);
1471}
1472
1479static inline bool xas_invalid(const struct xa_state *xas)
1480{
1481 return (unsigned long)xas->xa_node & 3;
1482}
1483
1490static inline bool xas_valid(const struct xa_state *xas)
1491{
1492 return !xas_invalid(xas);
1493}
1494
1501static inline bool xas_is_node(const struct xa_state *xas)
1502{
1503 return xas_valid(xas) && xas->xa_node;
1504}
1505
1506/* True if the pointer is something other than a node */
1507static inline bool xas_not_node(struct xa_node *node)
1508{
1509 return ((unsigned long)node & 3) || !node;
1510}
1511
1512/* True if the node represents RESTART or an error */
1513static inline bool xas_frozen(struct xa_node *node)
1514{
1515 return (unsigned long)node & 2;
1516}
1517
1518/* True if the node represents head-of-tree, RESTART or BOUNDS */
1519static inline bool xas_top(struct xa_node *node)
1520{
1521 return node <= XAS_RESTART;
1522}
1523
1534static inline void xas_reset(struct xa_state *xas)
1535{
1536 xas->xa_node = XAS_RESTART;
1537}
1538
1551static inline bool xas_retry(struct xa_state *xas, const void *entry)
1552{
1553 if (xa_is_zero(entry))
1554 return true;
1555 if (!xa_is_retry(entry))
1556 return false;
1557 xas_reset(xas);
1558 return true;
1559}
1560
1561void *xas_load(struct xa_state *);
1562void *xas_store(struct xa_state *, void *entry);
1563void *xas_find(struct xa_state *, unsigned long max);
1565
1566bool xas_get_mark(const struct xa_state *, xa_mark_t);
1567void xas_set_mark(const struct xa_state *, xa_mark_t);
1568void xas_clear_mark(const struct xa_state *, xa_mark_t);
1569void *xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t);
1570void xas_init_marks(const struct xa_state *);
1571
1572bool xas_nomem(struct xa_state *, gfp_t);
1573void xas_destroy(struct xa_state *);
1574void xas_pause(struct xa_state *);
1575
1577
1578#ifdef CONFIG_XARRAY_MULTI
1579int xa_get_order(struct xarray *, unsigned long index);
1580int xas_get_order(struct xa_state *xas);
1581void xas_split(struct xa_state *, void *entry, unsigned int order);
1582void xas_split_alloc(struct xa_state *, void *entry, unsigned int order, gfp_t);
1583void xas_try_split(struct xa_state *xas, void *entry, unsigned int order);
1584unsigned int xas_try_split_min_order(unsigned int order);
1585#else
1586static inline int xa_get_order(struct xarray *xa, unsigned long index)
1587{
1588 return 0;
1589}
1590
1591static inline int xas_get_order(struct xa_state *xas)
1592{
1593 return 0;
1594}
1595
1596static inline void xas_split(struct xa_state *xas, void *entry,
1597 unsigned int order)
1598{
1599 xas_store(xas, entry);
1600}
1601
1602static inline void xas_split_alloc(struct xa_state *xas, void *entry,
1603 unsigned int order, gfp_t gfp)
1604{
1605}
1606
1607static inline void xas_try_split(struct xa_state *xas, void *entry,
1608 unsigned int order)
1609{
1610}
1611
1612static inline unsigned int xas_try_split_min_order(unsigned int order)
1613{
1614 return 0;
1615}
1616
1617#endif
1618
1633static inline void *xas_reload(struct xa_state *xas)
1634{
1635 struct xa_node *node = xas->xa_node;
1636 void *entry;
1637 char offset;
1638
1639 if (!node)
1640 return xa_head(xas->xa);
1642 offset = (xas->xa_index >> node->shift) & XA_CHUNK_MASK;
1643 entry = xa_entry(xas->xa, node, offset);
1644 if (!xa_is_sibling(entry))
1645 return entry;
1646 offset = xa_to_sibling(entry);
1647 } else {
1648 offset = xas->xa_offset;
1649 }
1650 return xa_entry(xas->xa, node, offset);
1651}
1652
1662static inline void xas_set(struct xa_state *xas, unsigned long index)
1663{
1664 xas->xa_index = index;
1665 xas->xa_node = XAS_RESTART;
1666}
1667
1678static inline void xas_advance(struct xa_state *xas, unsigned long index)
1679{
1680 unsigned char shift = xas_is_node(xas) ? xas->xa_node->shift : 0;
1681
1682 xas->xa_index = index;
1683 xas->xa_offset = (index >> shift) & XA_CHUNK_MASK;
1684}
1685
1692static inline void xas_set_order(struct xa_state *xas, unsigned long index,
1693 unsigned int order)
1694{
1695#ifdef CONFIG_XARRAY_MULTI
1696 xas->xa_index = order < BITS_PER_LONG ? (index >> order) << order : 0;
1697 xas->xa_shift = order - (order % XA_CHUNK_SHIFT);
1698 xas->xa_sibs = (1 << (order % XA_CHUNK_SHIFT)) - 1;
1699 xas->xa_node = XAS_RESTART;
1700#else
1701 BUG_ON(order > 0);
1702 xas_set(xas, index);
1703#endif
1704}
1705
1715static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update)
1716{
1717 xas->xa_update = update;
1718}
1719
1720static inline void xas_set_lru(struct xa_state *xas, struct list_lru *lru)
1721{
1722 xas->xa_lru = lru;
1723}
1724
1736static inline void *xas_next_entry(struct xa_state *xas, unsigned long max)
1737{
1738 struct xa_node *node = xas->xa_node;
1739 void *entry;
1740
1741 if (unlikely(xas_not_node(node) || node->shift ||
1742 xas->xa_offset != (xas->xa_index & XA_CHUNK_MASK)))
1743 return xas_find(xas, max);
1744
1745 do {
1746 if (unlikely(xas->xa_index >= max))
1747 return xas_find(xas, max);
1748 if (unlikely(xas->xa_offset == XA_CHUNK_MASK))
1749 return xas_find(xas, max);
1750 entry = xa_entry(xas->xa, node, xas->xa_offset + 1);
1751 if (unlikely(xa_is_internal(entry)))
1752 return xas_find(xas, max);
1753 xas->xa_offset++;
1754 xas->xa_index++;
1755 } while (!entry);
1756
1757 return entry;
1758}
1759
1760/* Private */
1761static inline unsigned int xas_find_chunk(struct xa_state *xas, bool advance,
1762 xa_mark_t mark)
1763{
1764 unsigned long *addr = xas->xa_node->marks[(__force unsigned)mark];
1765 unsigned int offset = xas->xa_offset;
1766
1767 if (advance)
1768 offset++;
1770 if (offset < XA_CHUNK_SIZE) {
1771 unsigned long data = *addr & (~0UL << offset);
1772 if (data)
1773 return __ffs(data);
1774 }
1775 return XA_CHUNK_SIZE;
1776 }
1777
1778 return find_next_bit(addr, XA_CHUNK_SIZE, offset);
1779}
1780
1793static inline void *xas_next_marked(struct xa_state *xas, unsigned long max,
1794 xa_mark_t mark)
1795{
1796 struct xa_node *node = xas->xa_node;
1797 void *entry;
1798 unsigned int offset;
1799
1800 if (unlikely(xas_not_node(node) || node->shift))
1801 return xas_find_marked(xas, max, mark);
1802 offset = xas_find_chunk(xas, true, mark);
1803 xas->xa_offset = offset;
1804 xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK) + offset;
1805 if (xas->xa_index > max)
1806 return NULL;
1807 if (offset == XA_CHUNK_SIZE)
1808 return xas_find_marked(xas, max, mark);
1809 entry = xa_entry(xas->xa, node, offset);
1810 if (!entry)
1811 return xas_find_marked(xas, max, mark);
1812 return entry;
1813}
1814
1815/*
1816 * If iterating while holding a lock, drop the lock and reschedule
1817 * every %XA_CHECK_SCHED loops.
1818 */
1819enum {
1821};
1822
1836#define xas_for_each(xas, entry, max) \
1837 for (entry = xas_find(xas, max); entry; \
1838 entry = xas_next_entry(xas, max))
1839
1854#define xas_for_each_marked(xas, entry, max, mark) \
1855 for (entry = xas_find_marked(xas, max, mark); entry; \
1856 entry = xas_next_marked(xas, max, mark))
1857
1870#define xas_for_each_conflict(xas, entry) \
1871 while ((entry = xas_find_conflict(xas)))
1872
1873void *__xas_next(struct xa_state *);
1874void *__xas_prev(struct xa_state *);
1875
1892static inline void *xas_prev(struct xa_state *xas)
1893{
1894 struct xa_node *node = xas->xa_node;
1895
1896 if (unlikely(xas_not_node(node) || node->shift ||
1897 xas->xa_offset == 0))
1898 return __xas_prev(xas);
1899
1900 xas->xa_index--;
1901 xas->xa_offset--;
1902 return xa_entry(xas->xa, node, xas->xa_offset);
1903}
1904
1921static inline void *xas_next(struct xa_state *xas)
1922{
1923 struct xa_node *node = xas->xa_node;
1924
1925 if (unlikely(xas_not_node(node) || node->shift ||
1926 xas->xa_offset == XA_CHUNK_MASK))
1927 return __xas_next(xas);
1928
1929 xas->xa_index++;
1930 xas->xa_offset++;
1931 return xa_entry(xas->xa, node, xas->xa_offset);
1932}
1933
1934#endif /* _LINUX_XARRAY_H */
unsigned int uintptr_t
Definition stdint.h:94
uint8_t flags
Definition virtio.h:0
typedef __attribute__
Definition list_sort.h:12
#define index(str, chr)
Definition string.h:66
#define NULL
Definition actypes.h:561
#define CONFIG_XARRAY_MULTI
Definition autoconf.h:164
#define BITS_PER_LONG
Definition bordoisila_bits.h:36
#define __bitwise
Definition compiler.h:57
#define __force
Definition compiler.h:60
#define unlikely(x)
Definition compiler.h:30
#define __must_check
Definition compiler.h:114
#define __rcu
Definition compiler_types.h:72
size_t size_t offset
Definition drm_bridge.h:1299
#define IS_ENABLED(option)
Definition kconfig.h:80
void * data
Definition kthread.h:30
void int node
Definition kthread.h:31
#define spin_lock_init(l)
Definition kernel.h:225
#define MAX_ERRNO
Definition kernel.h:304
unsigned int gfp_t
Definition kernel.h:56
#define BUG_ON(condition)
Definition kernel.h:730
int spinlock_t
Definition kernel.h:44
uint32_t u32
Definition kernel.h:16
#define lockdep_is_held(lock)
Definition lockdep.h:105
#define max(a, b)
Definition minmax.h:28
void(* entry)(void *)
Definition process.h:101
#define rcu_dereference_check(p, c)
Definition rcupdate.h:43
#define rcu_dereference_protected(p, c)
Definition rcupdate.h:44
Definition list.h:11
Definition rcupdate.h:18
Definition xarray.h:262
u32 min
Definition xarray.h:264
u32 max
Definition xarray.h:263
Definition xarray.h:1187
unsigned long marks[XA_MAX_MARKS][XA_MARK_LONGS]
Definition xarray.h:1201
unsigned char count
Definition xarray.h:1190
struct xarray * array
Definition xarray.h:1193
unsigned char nr_values
Definition xarray.h:1191
unsigned char offset
Definition xarray.h:1189
void __rcu * slots[XA_CHUNK_SIZE]
Definition xarray.h:1198
unsigned long tags[XA_MAX_MARKS][XA_MARK_LONGS]
Definition xarray.h:1200
unsigned char shift
Definition xarray.h:1188
struct xa_node __rcu * parent
Definition xarray.h:1192
Definition xarray.h:1373
unsigned char xa_offset
Definition xarray.h:1378
unsigned char xa_pad
Definition xarray.h:1379
unsigned char xa_sibs
Definition xarray.h:1377
struct xa_node * xa_alloc
Definition xarray.h:1381
struct list_lru * xa_lru
Definition xarray.h:1383
unsigned char xa_shift
Definition xarray.h:1376
unsigned long xa_index
Definition xarray.h:1375
struct xarray * xa
Definition xarray.h:1374
struct xa_node * xa_node
Definition xarray.h:1380
xa_update_node_t xa_update
Definition xarray.h:1382
Definition xarray.h:319
void __rcu * xa_head
Definition xarray.h:323
spinlock_t xa_lock
Definition xarray.h:320
gfp_t xa_flags
Definition xarray.h:322
TTY * last
Definition tty.cpp:48
#define UL(x)
Definition const.h:78
#define might_alloc(gfp)
Definition workqueue.h:29
void xas_pause(struct xa_state *)
xa_lock_type
Definition xarray.h:281
@ XA_LOCK_BH
Definition xarray.h:283
@ XA_LOCK_IRQ
Definition xarray.h:282
void * xas_store(struct xa_state *, void *entry)
void xas_destroy(struct xa_state *)
void * __xas_next(struct xa_state *)
bool xas_get_mark(const struct xa_state *, xa_mark_t)
void * xa_erase(struct xarray *, unsigned long index)
void * xa_find_after(struct xarray *xa, unsigned long *index, unsigned long max, xa_mark_t) __attribute__((nonnull(2)))
#define XA_ZERO_ENTRY
Definition xarray.h:197
void * xas_find_marked(struct xa_state *, unsigned long max, xa_mark_t)
#define XA_FLAGS_MARK(mark)
Definition xarray.h:296
int __must_check __xa_insert(struct xarray *, unsigned long index, void *entry, gfp_t)
#define XA_NODE_BUG_ON(node, x)
Definition xarray.h:1223
#define XA_ERROR(errno)
Definition xarray.h:1390
void * xa_load(struct xarray *, unsigned long index)
unsigned int xa_extract(struct xarray *, void **dst, unsigned long start, unsigned long max, unsigned int n, xa_mark_t)
unsigned __bitwise xa_mark_t
Definition xarray.h:273
bool xas_nomem(struct xa_state *, gfp_t)
void * xa_find(struct xarray *xa, unsigned long *index, unsigned long max, xa_mark_t) __attribute__((nonnull(2)))
#define xa_unlock(xa)
Definition xarray.h:556
#define XA_MARK_LONGS
Definition xarray.h:1178
#define XA_CHUNK_MASK
Definition xarray.h:1176
#define xa_lock_irq(xa)
Definition xarray.h:559
void xa_set_mark(struct xarray *, unsigned long index, xa_mark_t)
void * __xa_store(struct xarray *, unsigned long index, void *entry, gfp_t)
void xa_delete_node(struct xa_node *, xa_update_node_t)
void * xa_store(struct xarray *, unsigned long index, void *entry, gfp_t)
void xas_init_marks(const struct xa_state *)
#define XA_CHUNK_SHIFT
Definition xarray.h:1173
void __xa_set_mark(struct xarray *, unsigned long index, xa_mark_t)
@ XA_CHECK_SCHED
Definition xarray.h:1820
int __must_check __xa_alloc(struct xarray *, u32 *id, void *entry, struct xa_limit, gfp_t)
void xa_dump_node(const struct xa_node *)
void xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t)
#define XA_CHUNK_SIZE
Definition xarray.h:1175
void * xas_load(struct xa_state *)
void xa_destroy(struct xarray *)
#define XA_RETRY_ENTRY
Definition xarray.h:1316
void(* xa_update_node_t)(struct xa_node *node)
Definition xarray.h:1352
void * __xa_cmpxchg(struct xarray *, unsigned long index, void *old, void *entry, gfp_t)
void __xa_clear_mark(struct xarray *, unsigned long index, xa_mark_t)
#define xa_lock_bh(xa)
Definition xarray.h:557
void xa_dump(const struct xarray *)
int __must_check __xa_alloc_cyclic(struct xarray *, u32 *id, void *entry, struct xa_limit, u32 *next, gfp_t)
bool xa_get_mark(struct xarray *, unsigned long index, xa_mark_t)
void * xas_find(struct xa_state *, unsigned long max)
#define xa_unlock_irq(xa)
Definition xarray.h:560
#define xa_lock(xa)
Definition xarray.h:555
void * __xa_erase(struct xarray *, unsigned long index)
#define XA_MAX_MARKS
Definition xarray.h:1177
#define XAS_RESTART
Definition xarray.h:1392
void xas_clear_mark(const struct xa_state *, xa_mark_t)
void xas_set_mark(const struct xa_state *, xa_mark_t)
void xas_create_range(struct xa_state *)
void * xa_store_range(struct xarray *, unsigned long first, unsigned long last, void *entry, gfp_t)
void * __xas_prev(struct xa_state *)
#define xa_unlock_bh(xa)
Definition xarray.h:558
void * xas_find_conflict(struct xa_state *)