18#include <linux/limits.h>
49#define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
58static inline void *xa_mk_value(
unsigned long v)
61 return (
void *)((v << 1) | 1);
71static inline unsigned long xa_to_value(
const void *
entry)
73 return (
unsigned long)
entry >> 1;
83static inline bool xa_is_value(
const void *
entry)
85 return (
unsigned long)
entry & 1;
120static inline void *xa_tag_pointer(
void *p,
unsigned long tag)
135static inline void *xa_untag_pointer(
void *
entry)
150static inline unsigned int xa_pointer_tag(
void *
entry)
152 return (
unsigned long)
entry & 3UL;
168static inline void *xa_mk_internal(
unsigned long v)
170 return (
void *)((v << 2) | 2);
180static inline unsigned long xa_to_internal(
const void *
entry)
182 return (
unsigned long)
entry >> 2;
192static inline bool xa_is_internal(
const void *
entry)
194 return ((
unsigned long)
entry & 3) == 2;
197#define XA_ZERO_ENTRY xa_mk_internal(257)
208static inline bool xa_is_zero(
const void *
entry)
224static inline bool xa_is_err(
const void *
entry)
242static inline int xa_err(
void *
entry)
245 if (xa_is_err(
entry))
246 return (
long)
entry >> 2;
267#define XA_LIMIT(_min, _max) (struct xa_limit) { .min = _min, .max = _max }
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)
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
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)))
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)
326#define XARRAY_INIT(name, flags) { \
327 .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \
342#define DEFINE_XARRAY_FLAGS(name, flags) \
343 struct xarray name = XARRAY_INIT(name, flags)
354#define DEFINE_XARRAY(name) DEFINE_XARRAY_FLAGS(name, 0)
363#define DEFINE_XARRAY_ALLOC(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC)
372#define DEFINE_XARRAY_ALLOC1(name) DEFINE_XARRAY_FLAGS(name, XA_FLAGS_ALLOC1)
416static inline void xa_init(
struct xarray *xa)
418 xa_init_flags(xa, 0);
428static inline bool xa_empty(
const struct xarray *xa)
470#define xa_for_each_range(xa, index, entry, start, last) \
471 for (index = start, \
472 entry = xa_find(xa, &index, last, XA_PRESENT); \
474 entry = xa_find_after(xa, &index, last, XA_PRESENT))
499#define xa_for_each_start(xa, index, entry, start) \
500 xa_for_each_range(xa, index, entry, start, ULONG_MAX)
523#define xa_for_each(xa, index, entry) \
524 xa_for_each_start(xa, index, entry, 0)
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))
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)
608static inline void *xa_store_bh(
struct xarray *xa,
unsigned long index,
635static inline void *xa_store_irq(
struct xarray *xa,
unsigned long index,
661static inline void *xa_erase_bh(
struct xarray *xa,
unsigned long index)
685static inline void *xa_erase_irq(
struct xarray *xa,
unsigned long index)
711static inline void *xa_cmpxchg(
struct xarray *xa,
unsigned long index,
739static inline void *xa_cmpxchg_bh(
struct xarray *xa,
unsigned long index,
767static inline void *xa_cmpxchg_irq(
struct xarray *xa,
unsigned long index,
995static inline int xa_alloc_cyclic(
struct xarray *xa,
u32 *
id,
void *
entry,
1005 return err < 0 ? err : 0;
1034static inline int xa_alloc_cyclic_bh(
struct xarray *xa,
u32 *
id,
void *
entry,
1044 return err < 0 ? err : 0;
1073static inline int xa_alloc_cyclic_irq(
struct xarray *xa,
u32 *
id,
void *
entry,
1083 return err < 0 ? err : 0;
1155static inline void xa_release(
struct xarray *xa,
unsigned long index)
1172#ifndef XA_CHUNK_SHIFT
1173#define XA_CHUNK_SHIFT (IS_ENABLED(CONFIG_BASE_SMALL) ? 4 : 6)
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)
1209#define XA_BUG_ON(xa, x) do { \
1215#define XA_NODE_BUG_ON(node, x) do { \
1217 if (node) xa_dump_node(node); \
1222#define XA_BUG_ON(xa, x) do { } while (0)
1223#define XA_NODE_BUG_ON(node, x) do { } while (0)
1227static inline void *xa_head(
const struct xarray *xa)
1234static inline void *xa_head_locked(
const struct xarray *xa)
1241static inline void *xa_entry(
const struct xarray *xa,
1250static inline void *xa_entry_locked(
const struct xarray *xa,
1259static inline struct xa_node *xa_parent(
const struct xarray *xa,
1267static inline struct xa_node *xa_parent_locked(
const struct xarray *xa,
1275static inline void *xa_mk_node(
const struct xa_node *
node)
1281static inline struct xa_node *xa_to_node(
const void *
entry)
1287static inline bool xa_is_node(
const void *
entry)
1293static inline void *xa_mk_sibling(
unsigned int offset)
1295 return xa_mk_internal(
offset);
1299static inline unsigned long xa_to_sibling(
const void *
entry)
1301 return xa_to_internal(
entry);
1310static inline bool xa_is_sibling(
const void *
entry)
1316#define XA_RETRY_ENTRY xa_mk_internal(256)
1324static inline bool xa_is_retry(
const void *
entry)
1335static inline bool xa_is_advanced(
const void *
entry)
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)
1394#define __XA_STATE(array, index, shift, sibs) { \
1396 .xa_index = index, \
1397 .xa_shift = shift, \
1401 .xa_node = XAS_RESTART, \
1403 .xa_update = NULL, \
1415#define XA_STATE(name, array, index) \
1416 struct xa_state name = __XA_STATE(array, index, 0, 0)
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)
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)
1454static inline int xas_error(
const struct xa_state *xas)
1468static inline void xas_set_err(
struct xa_state *xas,
long err)
1479static inline bool xas_invalid(
const struct xa_state *xas)
1481 return (
unsigned long)xas->
xa_node & 3;
1490static inline bool xas_valid(
const struct xa_state *xas)
1492 return !xas_invalid(xas);
1501static inline bool xas_is_node(
const struct xa_state *xas)
1503 return xas_valid(xas) && xas->
xa_node;
1507static inline bool xas_not_node(
struct xa_node *
node)
1509 return ((
unsigned long)
node & 3) || !
node;
1513static inline bool xas_frozen(
struct xa_node *
node)
1515 return (
unsigned long)
node & 2;
1534static inline void xas_reset(
struct xa_state *xas)
1551static inline bool xas_retry(
struct xa_state *xas,
const void *
entry)
1553 if (xa_is_zero(
entry))
1555 if (!xa_is_retry(
entry))
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);
1583void xas_try_split(
struct xa_state *xas,
void *
entry,
unsigned int order);
1584unsigned int xas_try_split_min_order(
unsigned int order);
1586static inline int xa_get_order(
struct xarray *xa,
unsigned long index)
1591static inline int xas_get_order(
struct xa_state *xas)
1596static inline void xas_split(
struct xa_state *xas,
void *
entry,
1602static inline void xas_split_alloc(
struct xa_state *xas,
void *
entry,
1603 unsigned int order,
gfp_t gfp)
1607static inline void xas_try_split(
struct xa_state *xas,
void *
entry,
1612static inline unsigned int xas_try_split_min_order(
unsigned int order)
1633static inline void *xas_reload(
struct xa_state *xas)
1640 return xa_head(xas->
xa);
1644 if (!xa_is_sibling(
entry))
1662static inline void xas_set(
struct xa_state *xas,
unsigned long index)
1678static inline void xas_advance(
struct xa_state *xas,
unsigned long index)
1692static inline void xas_set_order(
struct xa_state *xas,
unsigned long index,
1695#ifdef CONFIG_XARRAY_MULTI
1702 xas_set(xas,
index);
1720static inline void xas_set_lru(
struct xa_state *xas,
struct list_lru *lru)
1736static inline void *xas_next_entry(
struct xa_state *xas,
unsigned long max)
1761static inline unsigned int xas_find_chunk(
struct xa_state *xas,
bool advance,
1793static inline void *xas_next_marked(
struct xa_state *xas,
unsigned long max,
1802 offset = xas_find_chunk(xas,
true, mark);
1836#define xas_for_each(xas, entry, max) \
1837 for (entry = xas_find(xas, max); entry; \
1838 entry = xas_next_entry(xas, max))
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))
1870#define xas_for_each_conflict(xas, entry) \
1871 while ((entry = xas_find_conflict(xas)))
1892static inline void *xas_prev(
struct xa_state *xas)
1921static inline void *xas_next(
struct xa_state *xas)
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
u32 min
Definition xarray.h:264
u32 max
Definition xarray.h:263
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
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
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 *)