xref: /linux-6.15/include/linux/maple_tree.h (revision 7c8c76e4)
154a611b6SLiam R. Howlett /* SPDX-License-Identifier: GPL-2.0+ */
254a611b6SLiam R. Howlett #ifndef _LINUX_MAPLE_TREE_H
354a611b6SLiam R. Howlett #define _LINUX_MAPLE_TREE_H
454a611b6SLiam R. Howlett /*
554a611b6SLiam R. Howlett  * Maple Tree - An RCU-safe adaptive tree for storing ranges
654a611b6SLiam R. Howlett  * Copyright (c) 2018-2022 Oracle
754a611b6SLiam R. Howlett  * Authors:     Liam R. Howlett <[email protected]>
854a611b6SLiam R. Howlett  *              Matthew Wilcox <[email protected]>
954a611b6SLiam R. Howlett  */
1054a611b6SLiam R. Howlett 
1154a611b6SLiam R. Howlett #include <linux/kernel.h>
1254a611b6SLiam R. Howlett #include <linux/rcupdate.h>
1354a611b6SLiam R. Howlett #include <linux/spinlock.h>
1454a611b6SLiam R. Howlett /* #define CONFIG_MAPLE_RCU_DISABLED */
1554a611b6SLiam R. Howlett 
1654a611b6SLiam R. Howlett /*
1754a611b6SLiam R. Howlett  * Allocated nodes are mutable until they have been inserted into the tree,
1854a611b6SLiam R. Howlett  * at which time they cannot change their type until they have been removed
1954a611b6SLiam R. Howlett  * from the tree and an RCU grace period has passed.
2054a611b6SLiam R. Howlett  *
2154a611b6SLiam R. Howlett  * Removed nodes have their ->parent set to point to themselves.  RCU readers
2254a611b6SLiam R. Howlett  * check ->parent before relying on the value that they loaded from the
2354a611b6SLiam R. Howlett  * slots array.  This lets us reuse the slots array for the RCU head.
2454a611b6SLiam R. Howlett  *
2554a611b6SLiam R. Howlett  * Nodes in the tree point to their parent unless bit 0 is set.
2654a611b6SLiam R. Howlett  */
2754a611b6SLiam R. Howlett #if defined(CONFIG_64BIT) || defined(BUILD_VDSO32_64)
2854a611b6SLiam R. Howlett /* 64bit sizes */
2954a611b6SLiam R. Howlett #define MAPLE_NODE_SLOTS	31	/* 256 bytes including ->parent */
3054a611b6SLiam R. Howlett #define MAPLE_RANGE64_SLOTS	16	/* 256 bytes */
3154a611b6SLiam R. Howlett #define MAPLE_ARANGE64_SLOTS	10	/* 240 bytes */
3254a611b6SLiam R. Howlett #define MAPLE_ALLOC_SLOTS	(MAPLE_NODE_SLOTS - 1)
3354a611b6SLiam R. Howlett #else
3454a611b6SLiam R. Howlett /* 32bit sizes */
3554a611b6SLiam R. Howlett #define MAPLE_NODE_SLOTS	63	/* 256 bytes including ->parent */
3654a611b6SLiam R. Howlett #define MAPLE_RANGE64_SLOTS	32	/* 256 bytes */
3754a611b6SLiam R. Howlett #define MAPLE_ARANGE64_SLOTS	21	/* 240 bytes */
3854a611b6SLiam R. Howlett #define MAPLE_ALLOC_SLOTS	(MAPLE_NODE_SLOTS - 2)
3954a611b6SLiam R. Howlett #endif /* defined(CONFIG_64BIT) || defined(BUILD_VDSO32_64) */
4054a611b6SLiam R. Howlett 
4154a611b6SLiam R. Howlett #define MAPLE_NODE_MASK		255UL
4254a611b6SLiam R. Howlett 
4354a611b6SLiam R. Howlett /*
4454a611b6SLiam R. Howlett  * The node->parent of the root node has bit 0 set and the rest of the pointer
4554a611b6SLiam R. Howlett  * is a pointer to the tree itself.  No more bits are available in this pointer
4654a611b6SLiam R. Howlett  * (on m68k, the data structure may only be 2-byte aligned).
4754a611b6SLiam R. Howlett  *
4854a611b6SLiam R. Howlett  * Internal non-root nodes can only have maple_range_* nodes as parents.  The
4954a611b6SLiam R. Howlett  * parent pointer is 256B aligned like all other tree nodes.  When storing a 32
5054a611b6SLiam R. Howlett  * or 64 bit values, the offset can fit into 4 bits.  The 16 bit values need an
5154a611b6SLiam R. Howlett  * extra bit to store the offset.  This extra bit comes from a reuse of the last
5254a611b6SLiam R. Howlett  * bit in the node type.  This is possible by using bit 1 to indicate if bit 2
5354a611b6SLiam R. Howlett  * is part of the type or the slot.
5454a611b6SLiam R. Howlett  *
556050df6dSWei Yang  * Once the type is decided, the decision of an allocation range type or a
566050df6dSWei Yang  * range type is done by examining the immutable tree flag for the
576050df6dSWei Yang  * MT_FLAGS_ALLOC_RANGE flag.
5854a611b6SLiam R. Howlett  *
5954a611b6SLiam R. Howlett  *  Node types:
6054a611b6SLiam R. Howlett  *   0x??1 = Root
6154a611b6SLiam R. Howlett  *   0x?00 = 16 bit nodes
6254a611b6SLiam R. Howlett  *   0x010 = 32 bit nodes
6354a611b6SLiam R. Howlett  *   0x110 = 64 bit nodes
6454a611b6SLiam R. Howlett  *
6554a611b6SLiam R. Howlett  *  Slot size and location in the parent pointer:
6654a611b6SLiam R. Howlett  *   type  : slot location
6754a611b6SLiam R. Howlett  *   0x??1 : Root
6854a611b6SLiam R. Howlett  *   0x?00 : 16 bit values, type in 0-1, slot in 2-6
6954a611b6SLiam R. Howlett  *   0x010 : 32 bit values, type in 0-2, slot in 3-6
7054a611b6SLiam R. Howlett  *   0x110 : 64 bit values, type in 0-2, slot in 3-6
7154a611b6SLiam R. Howlett  */
7254a611b6SLiam R. Howlett 
7354a611b6SLiam R. Howlett /*
7454a611b6SLiam R. Howlett  * This metadata is used to optimize the gap updating code and in reverse
7554a611b6SLiam R. Howlett  * searching for gaps or any other code that needs to find the end of the data.
7654a611b6SLiam R. Howlett  */
7754a611b6SLiam R. Howlett struct maple_metadata {
7854a611b6SLiam R. Howlett 	unsigned char end;
7954a611b6SLiam R. Howlett 	unsigned char gap;
8054a611b6SLiam R. Howlett };
8154a611b6SLiam R. Howlett 
8254a611b6SLiam R. Howlett /*
8354a611b6SLiam R. Howlett  * Leaf nodes do not store pointers to nodes, they store user data.  Users may
8454a611b6SLiam R. Howlett  * store almost any bit pattern.  As noted above, the optimisation of storing an
8554a611b6SLiam R. Howlett  * entry at 0 in the root pointer cannot be done for data which have the bottom
8654a611b6SLiam R. Howlett  * two bits set to '10'.  We also reserve values with the bottom two bits set to
8754a611b6SLiam R. Howlett  * '10' which are below 4096 (ie 2, 6, 10 .. 4094) for internal use.  Some APIs
8854a611b6SLiam R. Howlett  * return errnos as a negative errno shifted right by two bits and the bottom
8954a611b6SLiam R. Howlett  * two bits set to '10', and while choosing to store these values in the array
9054a611b6SLiam R. Howlett  * is not an error, it may lead to confusion if you're testing for an error with
9154a611b6SLiam R. Howlett  * mas_is_err().
9254a611b6SLiam R. Howlett  *
9354a611b6SLiam R. Howlett  * Non-leaf nodes store the type of the node pointed to (enum maple_type in bits
9454a611b6SLiam R. Howlett  * 3-6), bit 2 is reserved.  That leaves bits 0-1 unused for now.
9554a611b6SLiam R. Howlett  *
9654a611b6SLiam R. Howlett  * In regular B-Tree terms, pivots are called keys.  The term pivot is used to
9754a611b6SLiam R. Howlett  * indicate that the tree is specifying ranges,  Pivots may appear in the
9854a611b6SLiam R. Howlett  * subtree with an entry attached to the value whereas keys are unique to a
9954a611b6SLiam R. Howlett  * specific position of a B-tree.  Pivot values are inclusive of the slot with
10054a611b6SLiam R. Howlett  * the same index.
10154a611b6SLiam R. Howlett  */
10254a611b6SLiam R. Howlett 
10354a611b6SLiam R. Howlett struct maple_range_64 {
10454a611b6SLiam R. Howlett 	struct maple_pnode *parent;
10554a611b6SLiam R. Howlett 	unsigned long pivot[MAPLE_RANGE64_SLOTS - 1];
10654a611b6SLiam R. Howlett 	union {
10754a611b6SLiam R. Howlett 		void __rcu *slot[MAPLE_RANGE64_SLOTS];
10854a611b6SLiam R. Howlett 		struct {
10954a611b6SLiam R. Howlett 			void __rcu *pad[MAPLE_RANGE64_SLOTS - 1];
11054a611b6SLiam R. Howlett 			struct maple_metadata meta;
11154a611b6SLiam R. Howlett 		};
11254a611b6SLiam R. Howlett 	};
11354a611b6SLiam R. Howlett };
11454a611b6SLiam R. Howlett 
11554a611b6SLiam R. Howlett /*
11654a611b6SLiam R. Howlett  * At tree creation time, the user can specify that they're willing to trade off
11754a611b6SLiam R. Howlett  * storing fewer entries in a tree in return for storing more information in
11854a611b6SLiam R. Howlett  * each node.
11954a611b6SLiam R. Howlett  *
12054a611b6SLiam R. Howlett  * The maple tree supports recording the largest range of NULL entries available
12154a611b6SLiam R. Howlett  * in this node, also called gaps.  This optimises the tree for allocating a
12254a611b6SLiam R. Howlett  * range.
12354a611b6SLiam R. Howlett  */
12454a611b6SLiam R. Howlett struct maple_arange_64 {
12554a611b6SLiam R. Howlett 	struct maple_pnode *parent;
12654a611b6SLiam R. Howlett 	unsigned long pivot[MAPLE_ARANGE64_SLOTS - 1];
12754a611b6SLiam R. Howlett 	void __rcu *slot[MAPLE_ARANGE64_SLOTS];
12854a611b6SLiam R. Howlett 	unsigned long gap[MAPLE_ARANGE64_SLOTS];
12954a611b6SLiam R. Howlett 	struct maple_metadata meta;
13054a611b6SLiam R. Howlett };
13154a611b6SLiam R. Howlett 
13254a611b6SLiam R. Howlett struct maple_alloc {
13354a611b6SLiam R. Howlett 	unsigned long total;
13454a611b6SLiam R. Howlett 	unsigned char node_count;
13554a611b6SLiam R. Howlett 	unsigned int request_count;
13654a611b6SLiam R. Howlett 	struct maple_alloc *slot[MAPLE_ALLOC_SLOTS];
13754a611b6SLiam R. Howlett };
13854a611b6SLiam R. Howlett 
13954a611b6SLiam R. Howlett struct maple_topiary {
14054a611b6SLiam R. Howlett 	struct maple_pnode *parent;
14154a611b6SLiam R. Howlett 	struct maple_enode *next; /* Overlaps the pivot */
14254a611b6SLiam R. Howlett };
14354a611b6SLiam R. Howlett 
14454a611b6SLiam R. Howlett enum maple_type {
14554a611b6SLiam R. Howlett 	maple_dense,
14654a611b6SLiam R. Howlett 	maple_leaf_64,
14754a611b6SLiam R. Howlett 	maple_range_64,
14854a611b6SLiam R. Howlett 	maple_arange_64,
14954a611b6SLiam R. Howlett };
15054a611b6SLiam R. Howlett 
151bd164d81SSidhartha Kumar enum store_type {
152bd164d81SSidhartha Kumar 	wr_invalid,
153bd164d81SSidhartha Kumar 	wr_new_root,
154bd164d81SSidhartha Kumar 	wr_store_root,
155bd164d81SSidhartha Kumar 	wr_exact_fit,
156bd164d81SSidhartha Kumar 	wr_spanning_store,
157bd164d81SSidhartha Kumar 	wr_split_store,
158bd164d81SSidhartha Kumar 	wr_rebalance,
159bd164d81SSidhartha Kumar 	wr_append,
160bd164d81SSidhartha Kumar 	wr_node_store,
161bd164d81SSidhartha Kumar 	wr_slot_store,
162bd164d81SSidhartha Kumar };
16354a611b6SLiam R. Howlett 
16454a611b6SLiam R. Howlett /**
16554a611b6SLiam R. Howlett  * DOC: Maple tree flags
16654a611b6SLiam R. Howlett  *
16754a611b6SLiam R. Howlett  * * MT_FLAGS_ALLOC_RANGE	- Track gaps in this tree
16854a611b6SLiam R. Howlett  * * MT_FLAGS_USE_RCU		- Operate in RCU mode
16954a611b6SLiam R. Howlett  * * MT_FLAGS_HEIGHT_OFFSET	- The position of the tree height in the flags
17054a611b6SLiam R. Howlett  * * MT_FLAGS_HEIGHT_MASK	- The mask for the maple tree height value
17154a611b6SLiam R. Howlett  * * MT_FLAGS_LOCK_MASK		- How the mt_lock is used
17254a611b6SLiam R. Howlett  * * MT_FLAGS_LOCK_IRQ		- Acquired irq-safe
17354a611b6SLiam R. Howlett  * * MT_FLAGS_LOCK_BH		- Acquired bh-safe
17454a611b6SLiam R. Howlett  * * MT_FLAGS_LOCK_EXTERN	- mt_lock is not used
17554a611b6SLiam R. Howlett  *
17654a611b6SLiam R. Howlett  * MAPLE_HEIGHT_MAX	The largest height that can be stored
17754a611b6SLiam R. Howlett  */
17854a611b6SLiam R. Howlett #define MT_FLAGS_ALLOC_RANGE	0x01
17954a611b6SLiam R. Howlett #define MT_FLAGS_USE_RCU	0x02
18054a611b6SLiam R. Howlett #define MT_FLAGS_HEIGHT_OFFSET	0x02
18154a611b6SLiam R. Howlett #define MT_FLAGS_HEIGHT_MASK	0x7C
18254a611b6SLiam R. Howlett #define MT_FLAGS_LOCK_MASK	0x300
18354a611b6SLiam R. Howlett #define MT_FLAGS_LOCK_IRQ	0x100
18454a611b6SLiam R. Howlett #define MT_FLAGS_LOCK_BH	0x200
18554a611b6SLiam R. Howlett #define MT_FLAGS_LOCK_EXTERN	0x300
1869b6713ccSChuck Lever #define MT_FLAGS_ALLOC_WRAPPED	0x0800
18754a611b6SLiam R. Howlett 
18854a611b6SLiam R. Howlett #define MAPLE_HEIGHT_MAX	31
18954a611b6SLiam R. Howlett 
19054a611b6SLiam R. Howlett 
19154a611b6SLiam R. Howlett #define MAPLE_NODE_TYPE_MASK	0x0F
19254a611b6SLiam R. Howlett #define MAPLE_NODE_TYPE_SHIFT	0x03
19354a611b6SLiam R. Howlett 
19454a611b6SLiam R. Howlett #define MAPLE_RESERVED_RANGE	4096
19554a611b6SLiam R. Howlett 
19654a611b6SLiam R. Howlett #ifdef CONFIG_LOCKDEP
19754a611b6SLiam R. Howlett typedef struct lockdep_map *lockdep_map_p;
198134d153cSLiam R. Howlett #define mt_lock_is_held(mt)                                             \
199134d153cSLiam R. Howlett 	(!(mt)->ma_external_lock || lock_is_held((mt)->ma_external_lock))
200134d153cSLiam R. Howlett 
20119a462f0SLiam R. Howlett #define mt_write_lock_is_held(mt)					\
20219a462f0SLiam R. Howlett 	(!(mt)->ma_external_lock ||					\
20319a462f0SLiam R. Howlett 	 lock_is_held_type((mt)->ma_external_lock, 0))
20419a462f0SLiam R. Howlett 
20554a611b6SLiam R. Howlett #define mt_set_external_lock(mt, lock)					\
20654a611b6SLiam R. Howlett 	(mt)->ma_external_lock = &(lock)->dep_map
20702fdb25fSLiam R. Howlett 
20802fdb25fSLiam R. Howlett #define mt_on_stack(mt)			(mt).ma_external_lock = NULL
20954a611b6SLiam R. Howlett #else
21054a611b6SLiam R. Howlett typedef struct { /* nothing */ } lockdep_map_p;
21154a611b6SLiam R. Howlett #define mt_lock_is_held(mt)		1
21219a462f0SLiam R. Howlett #define mt_write_lock_is_held(mt)	1
21354a611b6SLiam R. Howlett #define mt_set_external_lock(mt, lock)	do { } while (0)
21402fdb25fSLiam R. Howlett #define mt_on_stack(mt)			do { } while (0)
21554a611b6SLiam R. Howlett #endif
21654a611b6SLiam R. Howlett 
21754a611b6SLiam R. Howlett /*
21854a611b6SLiam R. Howlett  * If the tree contains a single entry at index 0, it is usually stored in
21954a611b6SLiam R. Howlett  * tree->ma_root.  To optimise for the page cache, an entry which ends in '00',
22054a611b6SLiam R. Howlett  * '01' or '11' is stored in the root, but an entry which ends in '10' will be
22154a611b6SLiam R. Howlett  * stored in a node.  Bits 3-6 are used to store enum maple_type.
22254a611b6SLiam R. Howlett  *
22354a611b6SLiam R. Howlett  * The flags are used both to store some immutable information about this tree
22454a611b6SLiam R. Howlett  * (set at tree creation time) and dynamic information set under the spinlock.
22554a611b6SLiam R. Howlett  *
22654a611b6SLiam R. Howlett  * Another use of flags are to indicate global states of the tree.  This is the
22778c018e3SJann Horn  * case with the MT_FLAGS_USE_RCU flag, which indicates the tree is currently in
22854a611b6SLiam R. Howlett  * RCU mode.  This mode was added to allow the tree to reuse nodes instead of
22954a611b6SLiam R. Howlett  * re-allocating and RCU freeing nodes when there is a single user.
23054a611b6SLiam R. Howlett  */
23154a611b6SLiam R. Howlett struct maple_tree {
23254a611b6SLiam R. Howlett 	union {
23354a611b6SLiam R. Howlett 		spinlock_t	ma_lock;
23454a611b6SLiam R. Howlett 		lockdep_map_p	ma_external_lock;
23554a611b6SLiam R. Howlett 	};
23654a611b6SLiam R. Howlett 	unsigned int	ma_flags;
23752ae298eSMateusz Guzik 	void __rcu      *ma_root;
23854a611b6SLiam R. Howlett };
23954a611b6SLiam R. Howlett 
24054a611b6SLiam R. Howlett /**
24154a611b6SLiam R. Howlett  * MTREE_INIT() - Initialize a maple tree
24254a611b6SLiam R. Howlett  * @name: The maple tree name
24354a611b6SLiam R. Howlett  * @__flags: The maple tree flags
24454a611b6SLiam R. Howlett  *
24554a611b6SLiam R. Howlett  */
24654a611b6SLiam R. Howlett #define MTREE_INIT(name, __flags) {					\
24754a611b6SLiam R. Howlett 	.ma_lock = __SPIN_LOCK_UNLOCKED((name).ma_lock),		\
24854a611b6SLiam R. Howlett 	.ma_flags = __flags,						\
24954a611b6SLiam R. Howlett 	.ma_root = NULL,						\
25054a611b6SLiam R. Howlett }
25154a611b6SLiam R. Howlett 
25254a611b6SLiam R. Howlett /**
25354a611b6SLiam R. Howlett  * MTREE_INIT_EXT() - Initialize a maple tree with an external lock.
25454a611b6SLiam R. Howlett  * @name: The tree name
25554a611b6SLiam R. Howlett  * @__flags: The maple tree flags
25654a611b6SLiam R. Howlett  * @__lock: The external lock
25754a611b6SLiam R. Howlett  */
25854a611b6SLiam R. Howlett #ifdef CONFIG_LOCKDEP
25954a611b6SLiam R. Howlett #define MTREE_INIT_EXT(name, __flags, __lock) {				\
26054a611b6SLiam R. Howlett 	.ma_external_lock = &(__lock).dep_map,				\
26154a611b6SLiam R. Howlett 	.ma_flags = (__flags),						\
26254a611b6SLiam R. Howlett 	.ma_root = NULL,						\
26354a611b6SLiam R. Howlett }
26454a611b6SLiam R. Howlett #else
26554a611b6SLiam R. Howlett #define MTREE_INIT_EXT(name, __flags, __lock)	MTREE_INIT(name, __flags)
26654a611b6SLiam R. Howlett #endif
26754a611b6SLiam R. Howlett 
26854a611b6SLiam R. Howlett #define DEFINE_MTREE(name)						\
26954a611b6SLiam R. Howlett 	struct maple_tree name = MTREE_INIT(name, 0)
27054a611b6SLiam R. Howlett 
27154a611b6SLiam R. Howlett #define mtree_lock(mt)		spin_lock((&(mt)->ma_lock))
272b2472efeSPeng Zhang #define mtree_lock_nested(mas, subclass) \
273b2472efeSPeng Zhang 		spin_lock_nested((&(mt)->ma_lock), subclass)
27454a611b6SLiam R. Howlett #define mtree_unlock(mt)	spin_unlock((&(mt)->ma_lock))
27554a611b6SLiam R. Howlett 
27654a611b6SLiam R. Howlett /*
27754a611b6SLiam R. Howlett  * The Maple Tree squeezes various bits in at various points which aren't
27854a611b6SLiam R. Howlett  * necessarily obvious.  Usually, this is done by observing that pointers are
27954a611b6SLiam R. Howlett  * N-byte aligned and thus the bottom log_2(N) bits are available for use.  We
28054a611b6SLiam R. Howlett  * don't use the high bits of pointers to store additional information because
28154a611b6SLiam R. Howlett  * we don't know what bits are unused on any given architecture.
28254a611b6SLiam R. Howlett  *
28354a611b6SLiam R. Howlett  * Nodes are 256 bytes in size and are also aligned to 256 bytes, giving us 8
28454a611b6SLiam R. Howlett  * low bits for our own purposes.  Nodes are currently of 4 types:
28554a611b6SLiam R. Howlett  * 1. Single pointer (Range is 0-0)
28654a611b6SLiam R. Howlett  * 2. Non-leaf Allocation Range nodes
28754a611b6SLiam R. Howlett  * 3. Non-leaf Range nodes
28854a611b6SLiam R. Howlett  * 4. Leaf Range nodes All nodes consist of a number of node slots,
28954a611b6SLiam R. Howlett  *    pivots, and a parent pointer.
29054a611b6SLiam R. Howlett  */
29154a611b6SLiam R. Howlett 
29254a611b6SLiam R. Howlett struct maple_node {
29354a611b6SLiam R. Howlett 	union {
29454a611b6SLiam R. Howlett 		struct {
29554a611b6SLiam R. Howlett 			struct maple_pnode *parent;
29654a611b6SLiam R. Howlett 			void __rcu *slot[MAPLE_NODE_SLOTS];
29754a611b6SLiam R. Howlett 		};
29854a611b6SLiam R. Howlett 		struct {
29954a611b6SLiam R. Howlett 			void *pad;
30054a611b6SLiam R. Howlett 			struct rcu_head rcu;
30154a611b6SLiam R. Howlett 			struct maple_enode *piv_parent;
30254a611b6SLiam R. Howlett 			unsigned char parent_slot;
30354a611b6SLiam R. Howlett 			enum maple_type type;
30454a611b6SLiam R. Howlett 			unsigned char slot_len;
30554a611b6SLiam R. Howlett 			unsigned int ma_flags;
30654a611b6SLiam R. Howlett 		};
30754a611b6SLiam R. Howlett 		struct maple_range_64 mr64;
30854a611b6SLiam R. Howlett 		struct maple_arange_64 ma64;
30954a611b6SLiam R. Howlett 		struct maple_alloc alloc;
31054a611b6SLiam R. Howlett 	};
31154a611b6SLiam R. Howlett };
31254a611b6SLiam R. Howlett 
31354a611b6SLiam R. Howlett /*
31454a611b6SLiam R. Howlett  * More complicated stores can cause two nodes to become one or three and
31554a611b6SLiam R. Howlett  * potentially alter the height of the tree.  Either half of the tree may need
31654a611b6SLiam R. Howlett  * to be rebalanced against the other.  The ma_topiary struct is used to track
31754a611b6SLiam R. Howlett  * which nodes have been 'cut' from the tree so that the change can be done
31854a611b6SLiam R. Howlett  * safely at a later date.  This is done to support RCU.
31954a611b6SLiam R. Howlett  */
32054a611b6SLiam R. Howlett struct ma_topiary {
32154a611b6SLiam R. Howlett 	struct maple_enode *head;
32254a611b6SLiam R. Howlett 	struct maple_enode *tail;
32354a611b6SLiam R. Howlett 	struct maple_tree *mtree;
32454a611b6SLiam R. Howlett };
32554a611b6SLiam R. Howlett 
32654a611b6SLiam R. Howlett void *mtree_load(struct maple_tree *mt, unsigned long index);
32754a611b6SLiam R. Howlett 
32854a611b6SLiam R. Howlett int mtree_insert(struct maple_tree *mt, unsigned long index,
32954a611b6SLiam R. Howlett 		void *entry, gfp_t gfp);
33054a611b6SLiam R. Howlett int mtree_insert_range(struct maple_tree *mt, unsigned long first,
33154a611b6SLiam R. Howlett 		unsigned long last, void *entry, gfp_t gfp);
33254a611b6SLiam R. Howlett int mtree_alloc_range(struct maple_tree *mt, unsigned long *startp,
33354a611b6SLiam R. Howlett 		void *entry, unsigned long size, unsigned long min,
33454a611b6SLiam R. Howlett 		unsigned long max, gfp_t gfp);
3359b6713ccSChuck Lever int mtree_alloc_cyclic(struct maple_tree *mt, unsigned long *startp,
3369b6713ccSChuck Lever 		void *entry, unsigned long range_lo, unsigned long range_hi,
3379b6713ccSChuck Lever 		unsigned long *next, gfp_t gfp);
33854a611b6SLiam R. Howlett int mtree_alloc_rrange(struct maple_tree *mt, unsigned long *startp,
33954a611b6SLiam R. Howlett 		void *entry, unsigned long size, unsigned long min,
34054a611b6SLiam R. Howlett 		unsigned long max, gfp_t gfp);
34154a611b6SLiam R. Howlett 
34254a611b6SLiam R. Howlett int mtree_store_range(struct maple_tree *mt, unsigned long first,
34354a611b6SLiam R. Howlett 		      unsigned long last, void *entry, gfp_t gfp);
34454a611b6SLiam R. Howlett int mtree_store(struct maple_tree *mt, unsigned long index,
34554a611b6SLiam R. Howlett 		void *entry, gfp_t gfp);
34654a611b6SLiam R. Howlett void *mtree_erase(struct maple_tree *mt, unsigned long index);
34754a611b6SLiam R. Howlett 
348fd32e4e9SPeng Zhang int mtree_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp);
349fd32e4e9SPeng Zhang int __mt_dup(struct maple_tree *mt, struct maple_tree *new, gfp_t gfp);
350fd32e4e9SPeng Zhang 
35154a611b6SLiam R. Howlett void mtree_destroy(struct maple_tree *mt);
35254a611b6SLiam R. Howlett void __mt_destroy(struct maple_tree *mt);
35354a611b6SLiam R. Howlett 
35454a611b6SLiam R. Howlett /**
35554a611b6SLiam R. Howlett  * mtree_empty() - Determine if a tree has any present entries.
35654a611b6SLiam R. Howlett  * @mt: Maple Tree.
35754a611b6SLiam R. Howlett  *
35854a611b6SLiam R. Howlett  * Context: Any context.
35954a611b6SLiam R. Howlett  * Return: %true if the tree contains only NULL pointers.
36054a611b6SLiam R. Howlett  */
mtree_empty(const struct maple_tree * mt)36154a611b6SLiam R. Howlett static inline bool mtree_empty(const struct maple_tree *mt)
36254a611b6SLiam R. Howlett {
36354a611b6SLiam R. Howlett 	return mt->ma_root == NULL;
36454a611b6SLiam R. Howlett }
36554a611b6SLiam R. Howlett 
36654a611b6SLiam R. Howlett /* Advanced API */
36754a611b6SLiam R. Howlett 
36854a611b6SLiam R. Howlett /*
369067311d3SLiam R. Howlett  * Maple State Status
370067311d3SLiam R. Howlett  * ma_active means the maple state is pointing to a node and offset and can
371067311d3SLiam R. Howlett  * continue operating on the tree.
372067311d3SLiam R. Howlett  * ma_start means we have not searched the tree.
373067311d3SLiam R. Howlett  * ma_root means we have searched the tree and the entry we found lives in
374067311d3SLiam R. Howlett  * the root of the tree (ie it has index 0, length 1 and is the only entry in
375067311d3SLiam R. Howlett  * the tree).
376067311d3SLiam R. Howlett  * ma_none means we have searched the tree and there is no node in the
377067311d3SLiam R. Howlett  * tree for this entry.  For example, we searched for index 1 in an empty
378067311d3SLiam R. Howlett  * tree.  Or we have a tree which points to a full leaf node and we
379067311d3SLiam R. Howlett  * searched for an entry which is larger than can be contained in that
380067311d3SLiam R. Howlett  * leaf node.
381067311d3SLiam R. Howlett  * ma_pause means the data within the maple state may be stale, restart the
382067311d3SLiam R. Howlett  * operation
383067311d3SLiam R. Howlett  * ma_overflow means the search has reached the upper limit of the search
384067311d3SLiam R. Howlett  * ma_underflow means the search has reached the lower limit of the search
385067311d3SLiam R. Howlett  * ma_error means there was an error, check the node for the error number.
386067311d3SLiam R. Howlett  */
387067311d3SLiam R. Howlett enum maple_status {
388067311d3SLiam R. Howlett 	ma_active,
389067311d3SLiam R. Howlett 	ma_start,
390067311d3SLiam R. Howlett 	ma_root,
391067311d3SLiam R. Howlett 	ma_none,
392067311d3SLiam R. Howlett 	ma_pause,
393067311d3SLiam R. Howlett 	ma_overflow,
394067311d3SLiam R. Howlett 	ma_underflow,
395067311d3SLiam R. Howlett 	ma_error,
396067311d3SLiam R. Howlett };
397067311d3SLiam R. Howlett 
398067311d3SLiam R. Howlett /*
39954a611b6SLiam R. Howlett  * The maple state is defined in the struct ma_state and is used to keep track
40054a611b6SLiam R. Howlett  * of information during operations, and even between operations when using the
40154a611b6SLiam R. Howlett  * advanced API.
40254a611b6SLiam R. Howlett  *
40354a611b6SLiam R. Howlett  * If state->node has bit 0 set then it references a tree location which is not
40454a611b6SLiam R. Howlett  * a node (eg the root).  If bit 1 is set, the rest of the bits are a negative
40554a611b6SLiam R. Howlett  * errno.  Bit 2 (the 'unallocated slots' bit) is clear.  Bits 3-6 indicate the
40654a611b6SLiam R. Howlett  * node type.
40754a611b6SLiam R. Howlett  *
40854a611b6SLiam R. Howlett  * state->alloc either has a request number of nodes or an allocated node.  If
40954a611b6SLiam R. Howlett  * stat->alloc has a requested number of nodes, the first bit will be set (0x1)
41054a611b6SLiam R. Howlett  * and the remaining bits are the value.  If state->alloc is a node, then the
41154a611b6SLiam R. Howlett  * node will be of type maple_alloc.  maple_alloc has MAPLE_NODE_SLOTS - 1 for
41254a611b6SLiam R. Howlett  * storing more allocated nodes, a total number of nodes allocated, and the
41354a611b6SLiam R. Howlett  * node_count in this node.  node_count is the number of allocated nodes in this
41454a611b6SLiam R. Howlett  * node.  The scaling beyond MAPLE_NODE_SLOTS - 1 is handled by storing further
41554a611b6SLiam R. Howlett  * nodes into state->alloc->slot[0]'s node.  Nodes are taken from state->alloc
41654a611b6SLiam R. Howlett  * by removing a node from the state->alloc node until state->alloc->node_count
41754a611b6SLiam R. Howlett  * is 1, when state->alloc is returned and the state->alloc->slot[0] is promoted
41854a611b6SLiam R. Howlett  * to state->alloc.  Nodes are pushed onto state->alloc by putting the current
41954a611b6SLiam R. Howlett  * state->alloc into the pushed node's slot[0].
42054a611b6SLiam R. Howlett  *
42154a611b6SLiam R. Howlett  * The state also contains the implied min/max of the state->node, the depth of
42254a611b6SLiam R. Howlett  * this search, and the offset. The implied min/max are either from the parent
42354a611b6SLiam R. Howlett  * node or are 0-oo for the root node.  The depth is incremented or decremented
42454a611b6SLiam R. Howlett  * every time a node is walked down or up.  The offset is the slot/pivot of
42554a611b6SLiam R. Howlett  * interest in the node - either for reading or writing.
42654a611b6SLiam R. Howlett  *
42754a611b6SLiam R. Howlett  * When returning a value the maple state index and last respectively contain
42854a611b6SLiam R. Howlett  * the start and end of the range for the entry.  Ranges are inclusive in the
42954a611b6SLiam R. Howlett  * Maple Tree.
430067311d3SLiam R. Howlett  *
431067311d3SLiam R. Howlett  * The status of the state is used to determine how the next action should treat
432067311d3SLiam R. Howlett  * the state.  For instance, if the status is ma_start then the next action
433067311d3SLiam R. Howlett  * should start at the root of the tree and walk down.  If the status is
434067311d3SLiam R. Howlett  * ma_pause then the node may be stale data and should be discarded.  If the
435067311d3SLiam R. Howlett  * status is ma_overflow, then the last action hit the upper limit.
436067311d3SLiam R. Howlett  *
43754a611b6SLiam R. Howlett  */
43854a611b6SLiam R. Howlett struct ma_state {
43954a611b6SLiam R. Howlett 	struct maple_tree *tree;	/* The tree we're operating in */
44054a611b6SLiam R. Howlett 	unsigned long index;		/* The index we're operating on - range start */
44154a611b6SLiam R. Howlett 	unsigned long last;		/* The last index we're operating on - range end */
44254a611b6SLiam R. Howlett 	struct maple_enode *node;	/* The node containing this entry */
44354a611b6SLiam R. Howlett 	unsigned long min;		/* The minimum index of this node - implied pivot min */
44454a611b6SLiam R. Howlett 	unsigned long max;		/* The maximum index of this node - implied pivot max */
44554a611b6SLiam R. Howlett 	struct maple_alloc *alloc;	/* Allocated nodes for this operation */
446067311d3SLiam R. Howlett 	enum maple_status status;	/* The status of the state (active, start, none, etc) */
44754a611b6SLiam R. Howlett 	unsigned char depth;		/* depth of tree descent during write */
44854a611b6SLiam R. Howlett 	unsigned char offset;
44954a611b6SLiam R. Howlett 	unsigned char mas_flags;
45031c532a8SLiam R. Howlett 	unsigned char end;		/* The end of the node */
451bd164d81SSidhartha Kumar 	enum store_type store_type;	/* The type of store needed for this operation */
45254a611b6SLiam R. Howlett };
45354a611b6SLiam R. Howlett 
45454a611b6SLiam R. Howlett struct ma_wr_state {
45554a611b6SLiam R. Howlett 	struct ma_state *mas;
45654a611b6SLiam R. Howlett 	struct maple_node *node;	/* Decoded mas->node */
45754a611b6SLiam R. Howlett 	unsigned long r_min;		/* range min */
45854a611b6SLiam R. Howlett 	unsigned long r_max;		/* range max */
45954a611b6SLiam R. Howlett 	enum maple_type type;		/* mas->node type */
46054a611b6SLiam R. Howlett 	unsigned char offset_end;	/* The offset where the write ends */
46154a611b6SLiam R. Howlett 	unsigned long *pivots;		/* mas->node->pivots pointer */
46254a611b6SLiam R. Howlett 	unsigned long end_piv;		/* The pivot at the offset end */
46354a611b6SLiam R. Howlett 	void __rcu **slots;		/* mas->node->slots pointer */
46454a611b6SLiam R. Howlett 	void *entry;			/* The entry to write */
46554a611b6SLiam R. Howlett 	void *content;			/* The existing entry that is being overwritten */
46654a611b6SLiam R. Howlett };
46754a611b6SLiam R. Howlett 
46854a611b6SLiam R. Howlett #define mas_lock(mas)           spin_lock(&((mas)->tree->ma_lock))
469b2472efeSPeng Zhang #define mas_lock_nested(mas, subclass) \
470b2472efeSPeng Zhang 		spin_lock_nested(&((mas)->tree->ma_lock), subclass)
47154a611b6SLiam R. Howlett #define mas_unlock(mas)         spin_unlock(&((mas)->tree->ma_lock))
47254a611b6SLiam R. Howlett 
47354a611b6SLiam R. Howlett /*
47454a611b6SLiam R. Howlett  * Special values for ma_state.node.
47554a611b6SLiam R. Howlett  * MA_ERROR represents an errno.  After dropping the lock and attempting
47654a611b6SLiam R. Howlett  * to resolve the error, the walk would have to be restarted from the
47754a611b6SLiam R. Howlett  * top of the tree as the tree may have been modified.
47854a611b6SLiam R. Howlett  */
47954a611b6SLiam R. Howlett #define MA_ERROR(err) \
48054a611b6SLiam R. Howlett 		((struct maple_enode *)(((unsigned long)err << 2) | 2UL))
48154a611b6SLiam R. Howlett 
48254a611b6SLiam R. Howlett #define MA_STATE(name, mt, first, end)					\
48354a611b6SLiam R. Howlett 	struct ma_state name = {					\
48454a611b6SLiam R. Howlett 		.tree = mt,						\
48554a611b6SLiam R. Howlett 		.index = first,						\
48654a611b6SLiam R. Howlett 		.last = end,						\
487067311d3SLiam R. Howlett 		.node = NULL,						\
488067311d3SLiam R. Howlett 		.status = ma_start,					\
48954a611b6SLiam R. Howlett 		.min = 0,						\
49054a611b6SLiam R. Howlett 		.max = ULONG_MAX,					\
49154a611b6SLiam R. Howlett 		.alloc = NULL,						\
492e7f43ca9SLiam R. Howlett 		.mas_flags = 0,						\
493bd164d81SSidhartha Kumar 		.store_type = wr_invalid,				\
49454a611b6SLiam R. Howlett 	}
49554a611b6SLiam R. Howlett 
49654a611b6SLiam R. Howlett #define MA_WR_STATE(name, ma_state, wr_entry)				\
49754a611b6SLiam R. Howlett 	struct ma_wr_state name = {					\
49854a611b6SLiam R. Howlett 		.mas = ma_state,					\
49954a611b6SLiam R. Howlett 		.content = NULL,					\
50054a611b6SLiam R. Howlett 		.entry = wr_entry,					\
50154a611b6SLiam R. Howlett 	}
50254a611b6SLiam R. Howlett 
50354a611b6SLiam R. Howlett #define MA_TOPIARY(name, tree)						\
50454a611b6SLiam R. Howlett 	struct ma_topiary name = {					\
50554a611b6SLiam R. Howlett 		.head = NULL,						\
50654a611b6SLiam R. Howlett 		.tail = NULL,						\
50754a611b6SLiam R. Howlett 		.mtree = tree,						\
50854a611b6SLiam R. Howlett 	}
50954a611b6SLiam R. Howlett 
51054a611b6SLiam R. Howlett void *mas_walk(struct ma_state *mas);
51154a611b6SLiam R. Howlett void *mas_store(struct ma_state *mas, void *entry);
51254a611b6SLiam R. Howlett void *mas_erase(struct ma_state *mas);
51354a611b6SLiam R. Howlett int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp);
51454a611b6SLiam R. Howlett void mas_store_prealloc(struct ma_state *mas, void *entry);
51554a611b6SLiam R. Howlett void *mas_find(struct ma_state *mas, unsigned long max);
5166169b553SLiam R. Howlett void *mas_find_range(struct ma_state *mas, unsigned long max);
51754a611b6SLiam R. Howlett void *mas_find_rev(struct ma_state *mas, unsigned long min);
5186b9e93e0SLiam R. Howlett void *mas_find_range_rev(struct ma_state *mas, unsigned long max);
519da089254SLiam R. Howlett int mas_preallocate(struct ma_state *mas, void *entry, gfp_t gfp);
5209b6713ccSChuck Lever int mas_alloc_cyclic(struct ma_state *mas, unsigned long *startp,
5219b6713ccSChuck Lever 		void *entry, unsigned long range_lo, unsigned long range_hi,
5229b6713ccSChuck Lever 		unsigned long *next, gfp_t gfp);
52354a611b6SLiam R. Howlett 
52454a611b6SLiam R. Howlett bool mas_nomem(struct ma_state *mas, gfp_t gfp);
52554a611b6SLiam R. Howlett void mas_pause(struct ma_state *mas);
52654a611b6SLiam R. Howlett void maple_tree_init(void);
52754a611b6SLiam R. Howlett void mas_destroy(struct ma_state *mas);
52854a611b6SLiam R. Howlett int mas_expected_entries(struct ma_state *mas, unsigned long nr_entries);
52954a611b6SLiam R. Howlett 
53054a611b6SLiam R. Howlett void *mas_prev(struct ma_state *mas, unsigned long min);
5316b9e93e0SLiam R. Howlett void *mas_prev_range(struct ma_state *mas, unsigned long max);
53254a611b6SLiam R. Howlett void *mas_next(struct ma_state *mas, unsigned long max);
5336169b553SLiam R. Howlett void *mas_next_range(struct ma_state *mas, unsigned long max);
53454a611b6SLiam R. Howlett 
53554a611b6SLiam R. Howlett int mas_empty_area(struct ma_state *mas, unsigned long min, unsigned long max,
53654a611b6SLiam R. Howlett 		   unsigned long size);
53706b27ce3SPeng Zhang /*
53806b27ce3SPeng Zhang  * This finds an empty area from the highest address to the lowest.
53906b27ce3SPeng Zhang  * AKA "Topdown" version,
54006b27ce3SPeng Zhang  */
54106b27ce3SPeng Zhang int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
54206b27ce3SPeng Zhang 		       unsigned long max, unsigned long size);
54354a611b6SLiam R. Howlett 
mas_init(struct ma_state * mas,struct maple_tree * tree,unsigned long addr)544e7f43ca9SLiam R. Howlett static inline void mas_init(struct ma_state *mas, struct maple_tree *tree,
545e7f43ca9SLiam R. Howlett 			    unsigned long addr)
546e7f43ca9SLiam R. Howlett {
547e7f43ca9SLiam R. Howlett 	memset(mas, 0, sizeof(struct ma_state));
548e7f43ca9SLiam R. Howlett 	mas->tree = tree;
549e7f43ca9SLiam R. Howlett 	mas->index = mas->last = addr;
550e7f43ca9SLiam R. Howlett 	mas->max = ULONG_MAX;
551067311d3SLiam R. Howlett 	mas->status = ma_start;
552067311d3SLiam R. Howlett 	mas->node = NULL;
553e7f43ca9SLiam R. Howlett }
554e7f43ca9SLiam R. Howlett 
mas_is_active(struct ma_state * mas)5555c590804SLiam R. Howlett static inline bool mas_is_active(struct ma_state *mas)
5565c590804SLiam R. Howlett {
557067311d3SLiam R. Howlett 	return mas->status == ma_active;
558067311d3SLiam R. Howlett }
5595c590804SLiam R. Howlett 
mas_is_err(struct ma_state * mas)560067311d3SLiam R. Howlett static inline bool mas_is_err(struct ma_state *mas)
561067311d3SLiam R. Howlett {
562067311d3SLiam R. Howlett 	return mas->status == ma_error;
5635c590804SLiam R. Howlett }
5645c590804SLiam R. Howlett 
56554a611b6SLiam R. Howlett /**
56654a611b6SLiam R. Howlett  * mas_reset() - Reset a Maple Tree operation state.
56754a611b6SLiam R. Howlett  * @mas: Maple Tree operation state.
56854a611b6SLiam R. Howlett  *
56954a611b6SLiam R. Howlett  * Resets the error or walk state of the @mas so future walks of the
57054a611b6SLiam R. Howlett  * array will start from the root.  Use this if you have dropped the
57154a611b6SLiam R. Howlett  * lock and want to reuse the ma_state.
57254a611b6SLiam R. Howlett  *
57354a611b6SLiam R. Howlett  * Context: Any context.
57454a611b6SLiam R. Howlett  */
mas_reset(struct ma_state * mas)575067311d3SLiam R. Howlett static __always_inline void mas_reset(struct ma_state *mas)
57654a611b6SLiam R. Howlett {
577067311d3SLiam R. Howlett 	mas->status = ma_start;
578067311d3SLiam R. Howlett 	mas->node = NULL;
57954a611b6SLiam R. Howlett }
58054a611b6SLiam R. Howlett 
58154a611b6SLiam R. Howlett /**
58254a611b6SLiam R. Howlett  * mas_for_each() - Iterate over a range of the maple tree.
58354a611b6SLiam R. Howlett  * @__mas: Maple Tree operation state (maple_state)
58454a611b6SLiam R. Howlett  * @__entry: Entry retrieved from the tree
58554a611b6SLiam R. Howlett  * @__max: maximum index to retrieve from the tree
58654a611b6SLiam R. Howlett  *
58754a611b6SLiam R. Howlett  * When returned, mas->index and mas->last will hold the entire range for the
58854a611b6SLiam R. Howlett  * entry.
58954a611b6SLiam R. Howlett  *
59054a611b6SLiam R. Howlett  * Note: may return the zero entry.
59154a611b6SLiam R. Howlett  */
59254a611b6SLiam R. Howlett #define mas_for_each(__mas, __entry, __max) \
59354a611b6SLiam R. Howlett 	while (((__entry) = mas_find((__mas), (__max))) != NULL)
59454a611b6SLiam R. Howlett 
595*7c8c76e4SSuren Baghdasaryan /**
596*7c8c76e4SSuren Baghdasaryan  * mas_for_each_rev() - Iterate over a range of the maple tree in reverse order.
597*7c8c76e4SSuren Baghdasaryan  * @__mas: Maple Tree operation state (maple_state)
598*7c8c76e4SSuren Baghdasaryan  * @__entry: Entry retrieved from the tree
599*7c8c76e4SSuren Baghdasaryan  * @__min: minimum index to retrieve from the tree
600*7c8c76e4SSuren Baghdasaryan  *
601*7c8c76e4SSuren Baghdasaryan  * When returned, mas->index and mas->last will hold the entire range for the
602*7c8c76e4SSuren Baghdasaryan  * entry.
603*7c8c76e4SSuren Baghdasaryan  *
604*7c8c76e4SSuren Baghdasaryan  * Note: may return the zero entry.
605*7c8c76e4SSuren Baghdasaryan  */
606*7c8c76e4SSuren Baghdasaryan #define mas_for_each_rev(__mas, __entry, __min) \
607*7c8c76e4SSuren Baghdasaryan 	while (((__entry) = mas_find_rev((__mas), (__min))) != NULL)
608*7c8c76e4SSuren Baghdasaryan 
60954a611b6SLiam R. Howlett #ifdef CONFIG_DEBUG_MAPLE_TREE
61089f499f3SLiam R. Howlett enum mt_dump_format {
61189f499f3SLiam R. Howlett 	mt_dump_dec,
61289f499f3SLiam R. Howlett 	mt_dump_hex,
61389f499f3SLiam R. Howlett };
61489f499f3SLiam R. Howlett 
61554a611b6SLiam R. Howlett extern atomic_t maple_tree_tests_run;
61654a611b6SLiam R. Howlett extern atomic_t maple_tree_tests_passed;
61754a611b6SLiam R. Howlett 
61889f499f3SLiam R. Howlett void mt_dump(const struct maple_tree *mt, enum mt_dump_format format);
619f0a1f866SLiam R. Howlett void mas_dump(const struct ma_state *mas);
620f0a1f866SLiam R. Howlett void mas_wr_dump(const struct ma_wr_state *wr_mas);
62154a611b6SLiam R. Howlett void mt_validate(struct maple_tree *mt);
622120b1162SLiam Howlett void mt_cache_shrink(void);
62354a611b6SLiam R. Howlett #define MT_BUG_ON(__tree, __x) do {					\
62454a611b6SLiam R. Howlett 	atomic_inc(&maple_tree_tests_run);				\
62554a611b6SLiam R. Howlett 	if (__x) {							\
62654a611b6SLiam R. Howlett 		pr_info("BUG at %s:%d (%u)\n",				\
62754a611b6SLiam R. Howlett 		__func__, __LINE__, __x);				\
62889f499f3SLiam R. Howlett 		mt_dump(__tree, mt_dump_hex);				\
62954a611b6SLiam R. Howlett 		pr_info("Pass: %u Run:%u\n",				\
63054a611b6SLiam R. Howlett 			atomic_read(&maple_tree_tests_passed),		\
63154a611b6SLiam R. Howlett 			atomic_read(&maple_tree_tests_run));		\
63254a611b6SLiam R. Howlett 		dump_stack();						\
63354a611b6SLiam R. Howlett 	} else {							\
63454a611b6SLiam R. Howlett 		atomic_inc(&maple_tree_tests_passed);			\
63554a611b6SLiam R. Howlett 	}								\
63654a611b6SLiam R. Howlett } while (0)
637f0a1f866SLiam R. Howlett 
638f0a1f866SLiam R. Howlett #define MAS_BUG_ON(__mas, __x) do {					\
639f0a1f866SLiam R. Howlett 	atomic_inc(&maple_tree_tests_run);				\
640f0a1f866SLiam R. Howlett 	if (__x) {							\
641f0a1f866SLiam R. Howlett 		pr_info("BUG at %s:%d (%u)\n",				\
642f0a1f866SLiam R. Howlett 		__func__, __LINE__, __x);				\
643f0a1f866SLiam R. Howlett 		mas_dump(__mas);					\
644f0a1f866SLiam R. Howlett 		mt_dump((__mas)->tree, mt_dump_hex);			\
645f0a1f866SLiam R. Howlett 		pr_info("Pass: %u Run:%u\n",				\
646f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_passed),		\
647f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_run));		\
648f0a1f866SLiam R. Howlett 		dump_stack();						\
649f0a1f866SLiam R. Howlett 	} else {							\
650f0a1f866SLiam R. Howlett 		atomic_inc(&maple_tree_tests_passed);			\
651f0a1f866SLiam R. Howlett 	}								\
652f0a1f866SLiam R. Howlett } while (0)
653f0a1f866SLiam R. Howlett 
654f0a1f866SLiam R. Howlett #define MAS_WR_BUG_ON(__wrmas, __x) do {				\
655f0a1f866SLiam R. Howlett 	atomic_inc(&maple_tree_tests_run);				\
656f0a1f866SLiam R. Howlett 	if (__x) {							\
657f0a1f866SLiam R. Howlett 		pr_info("BUG at %s:%d (%u)\n",				\
658f0a1f866SLiam R. Howlett 		__func__, __LINE__, __x);				\
659f0a1f866SLiam R. Howlett 		mas_wr_dump(__wrmas);					\
660f0a1f866SLiam R. Howlett 		mas_dump((__wrmas)->mas);				\
661f0a1f866SLiam R. Howlett 		mt_dump((__wrmas)->mas->tree, mt_dump_hex);		\
662f0a1f866SLiam R. Howlett 		pr_info("Pass: %u Run:%u\n",				\
663f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_passed),		\
664f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_run));		\
665f0a1f866SLiam R. Howlett 		dump_stack();						\
666f0a1f866SLiam R. Howlett 	} else {							\
667f0a1f866SLiam R. Howlett 		atomic_inc(&maple_tree_tests_passed);			\
668f0a1f866SLiam R. Howlett 	}								\
669f0a1f866SLiam R. Howlett } while (0)
670f0a1f866SLiam R. Howlett 
671f0a1f866SLiam R. Howlett #define MT_WARN_ON(__tree, __x)  ({					\
672f0a1f866SLiam R. Howlett 	int ret = !!(__x);						\
673f0a1f866SLiam R. Howlett 	atomic_inc(&maple_tree_tests_run);				\
674f0a1f866SLiam R. Howlett 	if (ret) {							\
675f0a1f866SLiam R. Howlett 		pr_info("WARN at %s:%d (%u)\n",				\
676f0a1f866SLiam R. Howlett 		__func__, __LINE__, __x);				\
677f0a1f866SLiam R. Howlett 		mt_dump(__tree, mt_dump_hex);				\
678f0a1f866SLiam R. Howlett 		pr_info("Pass: %u Run:%u\n",				\
679f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_passed),		\
680f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_run));		\
681f0a1f866SLiam R. Howlett 		dump_stack();						\
682f0a1f866SLiam R. Howlett 	} else {							\
683f0a1f866SLiam R. Howlett 		atomic_inc(&maple_tree_tests_passed);			\
684f0a1f866SLiam R. Howlett 	}								\
685f0a1f866SLiam R. Howlett 	unlikely(ret);							\
686f0a1f866SLiam R. Howlett })
687f0a1f866SLiam R. Howlett 
688f0a1f866SLiam R. Howlett #define MAS_WARN_ON(__mas, __x) ({					\
689f0a1f866SLiam R. Howlett 	int ret = !!(__x);						\
690f0a1f866SLiam R. Howlett 	atomic_inc(&maple_tree_tests_run);				\
691f0a1f866SLiam R. Howlett 	if (ret) {							\
692f0a1f866SLiam R. Howlett 		pr_info("WARN at %s:%d (%u)\n",				\
693f0a1f866SLiam R. Howlett 		__func__, __LINE__, __x);				\
694f0a1f866SLiam R. Howlett 		mas_dump(__mas);					\
695f0a1f866SLiam R. Howlett 		mt_dump((__mas)->tree, mt_dump_hex);			\
696f0a1f866SLiam R. Howlett 		pr_info("Pass: %u Run:%u\n",				\
697f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_passed),		\
698f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_run));		\
699f0a1f866SLiam R. Howlett 		dump_stack();						\
700f0a1f866SLiam R. Howlett 	} else {							\
701f0a1f866SLiam R. Howlett 		atomic_inc(&maple_tree_tests_passed);			\
702f0a1f866SLiam R. Howlett 	}								\
703f0a1f866SLiam R. Howlett 	unlikely(ret);							\
704f0a1f866SLiam R. Howlett })
705f0a1f866SLiam R. Howlett 
706f0a1f866SLiam R. Howlett #define MAS_WR_WARN_ON(__wrmas, __x) ({					\
707f0a1f866SLiam R. Howlett 	int ret = !!(__x);						\
708f0a1f866SLiam R. Howlett 	atomic_inc(&maple_tree_tests_run);				\
709f0a1f866SLiam R. Howlett 	if (ret) {							\
710f0a1f866SLiam R. Howlett 		pr_info("WARN at %s:%d (%u)\n",				\
711f0a1f866SLiam R. Howlett 		__func__, __LINE__, __x);				\
712f0a1f866SLiam R. Howlett 		mas_wr_dump(__wrmas);					\
713f0a1f866SLiam R. Howlett 		mas_dump((__wrmas)->mas);				\
714f0a1f866SLiam R. Howlett 		mt_dump((__wrmas)->mas->tree, mt_dump_hex);		\
715f0a1f866SLiam R. Howlett 		pr_info("Pass: %u Run:%u\n",				\
716f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_passed),		\
717f0a1f866SLiam R. Howlett 			atomic_read(&maple_tree_tests_run));		\
718f0a1f866SLiam R. Howlett 		dump_stack();						\
719f0a1f866SLiam R. Howlett 	} else {							\
720f0a1f866SLiam R. Howlett 		atomic_inc(&maple_tree_tests_passed);			\
721f0a1f866SLiam R. Howlett 	}								\
722f0a1f866SLiam R. Howlett 	unlikely(ret);							\
723f0a1f866SLiam R. Howlett })
72454a611b6SLiam R. Howlett #else
72554a611b6SLiam R. Howlett #define MT_BUG_ON(__tree, __x)		BUG_ON(__x)
726f0a1f866SLiam R. Howlett #define MAS_BUG_ON(__mas, __x)		BUG_ON(__x)
727f0a1f866SLiam R. Howlett #define MAS_WR_BUG_ON(__mas, __x)	BUG_ON(__x)
728f0a1f866SLiam R. Howlett #define MT_WARN_ON(__tree, __x)		WARN_ON(__x)
729f0a1f866SLiam R. Howlett #define MAS_WARN_ON(__mas, __x)		WARN_ON(__x)
730f0a1f866SLiam R. Howlett #define MAS_WR_WARN_ON(__mas, __x)	WARN_ON(__x)
73154a611b6SLiam R. Howlett #endif /* CONFIG_DEBUG_MAPLE_TREE */
73254a611b6SLiam R. Howlett 
733bf857dddSLiam R. Howlett /**
734bf857dddSLiam R. Howlett  * __mas_set_range() - Set up Maple Tree operation state to a sub-range of the
735bf857dddSLiam R. Howlett  * current location.
736bf857dddSLiam R. Howlett  * @mas: Maple Tree operation state.
737bf857dddSLiam R. Howlett  * @start: New start of range in the Maple Tree.
738bf857dddSLiam R. Howlett  * @last: New end of range in the Maple Tree.
739bf857dddSLiam R. Howlett  *
740bf857dddSLiam R. Howlett  * set the internal maple state values to a sub-range.
741bf857dddSLiam R. Howlett  * Please use mas_set_range() if you do not know where you are in the tree.
742bf857dddSLiam R. Howlett  */
__mas_set_range(struct ma_state * mas,unsigned long start,unsigned long last)743bf857dddSLiam R. Howlett static inline void __mas_set_range(struct ma_state *mas, unsigned long start,
744bf857dddSLiam R. Howlett 		unsigned long last)
745bf857dddSLiam R. Howlett {
746bf857dddSLiam R. Howlett 	/* Ensure the range starts within the current slot */
747bf857dddSLiam R. Howlett 	MAS_WARN_ON(mas, mas_is_active(mas) &&
748bf857dddSLiam R. Howlett 		   (mas->index > start || mas->last < start));
749bf857dddSLiam R. Howlett 	mas->index = start;
750bf857dddSLiam R. Howlett 	mas->last = last;
751bf857dddSLiam R. Howlett }
752bf857dddSLiam R. Howlett 
753bf857dddSLiam R. Howlett /**
754bf857dddSLiam R. Howlett  * mas_set_range() - Set up Maple Tree operation state for a different index.
755bf857dddSLiam R. Howlett  * @mas: Maple Tree operation state.
756bf857dddSLiam R. Howlett  * @start: New start of range in the Maple Tree.
757bf857dddSLiam R. Howlett  * @last: New end of range in the Maple Tree.
758bf857dddSLiam R. Howlett  *
759bf857dddSLiam R. Howlett  * Move the operation state to refer to a different range.  This will
760bf857dddSLiam R. Howlett  * have the effect of starting a walk from the top; see mas_next()
761bf857dddSLiam R. Howlett  * to move to an adjacent index.
762bf857dddSLiam R. Howlett  */
763bf857dddSLiam R. Howlett static inline
mas_set_range(struct ma_state * mas,unsigned long start,unsigned long last)764bf857dddSLiam R. Howlett void mas_set_range(struct ma_state *mas, unsigned long start, unsigned long last)
765bf857dddSLiam R. Howlett {
766067311d3SLiam R. Howlett 	mas_reset(mas);
767bf857dddSLiam R. Howlett 	__mas_set_range(mas, start, last);
768bf857dddSLiam R. Howlett }
769bf857dddSLiam R. Howlett 
770bf857dddSLiam R. Howlett /**
771bf857dddSLiam R. Howlett  * mas_set() - Set up Maple Tree operation state for a different index.
772bf857dddSLiam R. Howlett  * @mas: Maple Tree operation state.
773bf857dddSLiam R. Howlett  * @index: New index into the Maple Tree.
774bf857dddSLiam R. Howlett  *
775bf857dddSLiam R. Howlett  * Move the operation state to refer to a different index.  This will
776bf857dddSLiam R. Howlett  * have the effect of starting a walk from the top; see mas_next()
777bf857dddSLiam R. Howlett  * to move to an adjacent index.
778bf857dddSLiam R. Howlett  */
mas_set(struct ma_state * mas,unsigned long index)779bf857dddSLiam R. Howlett static inline void mas_set(struct ma_state *mas, unsigned long index)
780bf857dddSLiam R. Howlett {
781bf857dddSLiam R. Howlett 
782bf857dddSLiam R. Howlett 	mas_set_range(mas, index, index);
783bf857dddSLiam R. Howlett }
784bf857dddSLiam R. Howlett 
mt_external_lock(const struct maple_tree * mt)785bf857dddSLiam R. Howlett static inline bool mt_external_lock(const struct maple_tree *mt)
786bf857dddSLiam R. Howlett {
787bf857dddSLiam R. Howlett 	return (mt->ma_flags & MT_FLAGS_LOCK_MASK) == MT_FLAGS_LOCK_EXTERN;
788bf857dddSLiam R. Howlett }
789bf857dddSLiam R. Howlett 
790bf857dddSLiam R. Howlett /**
791bf857dddSLiam R. Howlett  * mt_init_flags() - Initialise an empty maple tree with flags.
792bf857dddSLiam R. Howlett  * @mt: Maple Tree
793bf857dddSLiam R. Howlett  * @flags: maple tree flags.
794bf857dddSLiam R. Howlett  *
795bf857dddSLiam R. Howlett  * If you need to initialise a Maple Tree with special flags (eg, an
796bf857dddSLiam R. Howlett  * allocation tree), use this function.
797bf857dddSLiam R. Howlett  *
798bf857dddSLiam R. Howlett  * Context: Any context.
799bf857dddSLiam R. Howlett  */
mt_init_flags(struct maple_tree * mt,unsigned int flags)800bf857dddSLiam R. Howlett static inline void mt_init_flags(struct maple_tree *mt, unsigned int flags)
801bf857dddSLiam R. Howlett {
802bf857dddSLiam R. Howlett 	mt->ma_flags = flags;
803bf857dddSLiam R. Howlett 	if (!mt_external_lock(mt))
804bf857dddSLiam R. Howlett 		spin_lock_init(&mt->ma_lock);
805bf857dddSLiam R. Howlett 	rcu_assign_pointer(mt->ma_root, NULL);
806bf857dddSLiam R. Howlett }
807bf857dddSLiam R. Howlett 
808bf857dddSLiam R. Howlett /**
809bf857dddSLiam R. Howlett  * mt_init() - Initialise an empty maple tree.
810bf857dddSLiam R. Howlett  * @mt: Maple Tree
811bf857dddSLiam R. Howlett  *
812bf857dddSLiam R. Howlett  * An empty Maple Tree.
813bf857dddSLiam R. Howlett  *
814bf857dddSLiam R. Howlett  * Context: Any context.
815bf857dddSLiam R. Howlett  */
mt_init(struct maple_tree * mt)816bf857dddSLiam R. Howlett static inline void mt_init(struct maple_tree *mt)
817bf857dddSLiam R. Howlett {
818bf857dddSLiam R. Howlett 	mt_init_flags(mt, 0);
819bf857dddSLiam R. Howlett }
820bf857dddSLiam R. Howlett 
mt_in_rcu(struct maple_tree * mt)821bf857dddSLiam R. Howlett static inline bool mt_in_rcu(struct maple_tree *mt)
822bf857dddSLiam R. Howlett {
823bf857dddSLiam R. Howlett #ifdef CONFIG_MAPLE_RCU_DISABLED
824bf857dddSLiam R. Howlett 	return false;
825bf857dddSLiam R. Howlett #endif
826bf857dddSLiam R. Howlett 	return mt->ma_flags & MT_FLAGS_USE_RCU;
827bf857dddSLiam R. Howlett }
828bf857dddSLiam R. Howlett 
829bf857dddSLiam R. Howlett /**
830bf857dddSLiam R. Howlett  * mt_clear_in_rcu() - Switch the tree to non-RCU mode.
831bf857dddSLiam R. Howlett  * @mt: The Maple Tree
832bf857dddSLiam R. Howlett  */
mt_clear_in_rcu(struct maple_tree * mt)833bf857dddSLiam R. Howlett static inline void mt_clear_in_rcu(struct maple_tree *mt)
834bf857dddSLiam R. Howlett {
835bf857dddSLiam R. Howlett 	if (!mt_in_rcu(mt))
836bf857dddSLiam R. Howlett 		return;
837bf857dddSLiam R. Howlett 
838bf857dddSLiam R. Howlett 	if (mt_external_lock(mt)) {
839bf857dddSLiam R. Howlett 		WARN_ON(!mt_lock_is_held(mt));
840bf857dddSLiam R. Howlett 		mt->ma_flags &= ~MT_FLAGS_USE_RCU;
841bf857dddSLiam R. Howlett 	} else {
842bf857dddSLiam R. Howlett 		mtree_lock(mt);
843bf857dddSLiam R. Howlett 		mt->ma_flags &= ~MT_FLAGS_USE_RCU;
844bf857dddSLiam R. Howlett 		mtree_unlock(mt);
845bf857dddSLiam R. Howlett 	}
846bf857dddSLiam R. Howlett }
847bf857dddSLiam R. Howlett 
848bf857dddSLiam R. Howlett /**
849bf857dddSLiam R. Howlett  * mt_set_in_rcu() - Switch the tree to RCU safe mode.
850bf857dddSLiam R. Howlett  * @mt: The Maple Tree
851bf857dddSLiam R. Howlett  */
mt_set_in_rcu(struct maple_tree * mt)852bf857dddSLiam R. Howlett static inline void mt_set_in_rcu(struct maple_tree *mt)
853bf857dddSLiam R. Howlett {
854bf857dddSLiam R. Howlett 	if (mt_in_rcu(mt))
855bf857dddSLiam R. Howlett 		return;
856bf857dddSLiam R. Howlett 
857bf857dddSLiam R. Howlett 	if (mt_external_lock(mt)) {
858bf857dddSLiam R. Howlett 		WARN_ON(!mt_lock_is_held(mt));
859bf857dddSLiam R. Howlett 		mt->ma_flags |= MT_FLAGS_USE_RCU;
860bf857dddSLiam R. Howlett 	} else {
861bf857dddSLiam R. Howlett 		mtree_lock(mt);
862bf857dddSLiam R. Howlett 		mt->ma_flags |= MT_FLAGS_USE_RCU;
863bf857dddSLiam R. Howlett 		mtree_unlock(mt);
864bf857dddSLiam R. Howlett 	}
865bf857dddSLiam R. Howlett }
866bf857dddSLiam R. Howlett 
mt_height(const struct maple_tree * mt)867bf857dddSLiam R. Howlett static inline unsigned int mt_height(const struct maple_tree *mt)
868bf857dddSLiam R. Howlett {
869bf857dddSLiam R. Howlett 	return (mt->ma_flags & MT_FLAGS_HEIGHT_MASK) >> MT_FLAGS_HEIGHT_OFFSET;
870bf857dddSLiam R. Howlett }
871bf857dddSLiam R. Howlett 
872bf857dddSLiam R. Howlett void *mt_find(struct maple_tree *mt, unsigned long *index, unsigned long max);
873bf857dddSLiam R. Howlett void *mt_find_after(struct maple_tree *mt, unsigned long *index,
874bf857dddSLiam R. Howlett 		    unsigned long max);
875bf857dddSLiam R. Howlett void *mt_prev(struct maple_tree *mt, unsigned long index,  unsigned long min);
876bf857dddSLiam R. Howlett void *mt_next(struct maple_tree *mt, unsigned long index, unsigned long max);
877bf857dddSLiam R. Howlett 
878bf857dddSLiam R. Howlett /**
879bf857dddSLiam R. Howlett  * mt_for_each - Iterate over each entry starting at index until max.
880bf857dddSLiam R. Howlett  * @__tree: The Maple Tree
881bf857dddSLiam R. Howlett  * @__entry: The current entry
882bf857dddSLiam R. Howlett  * @__index: The index to start the search from. Subsequently used as iterator.
883bf857dddSLiam R. Howlett  * @__max: The maximum limit for @index
884bf857dddSLiam R. Howlett  *
885bf857dddSLiam R. Howlett  * This iterator skips all entries, which resolve to a NULL pointer,
886bf857dddSLiam R. Howlett  * e.g. entries which has been reserved with XA_ZERO_ENTRY.
887bf857dddSLiam R. Howlett  */
888bf857dddSLiam R. Howlett #define mt_for_each(__tree, __entry, __index, __max) \
889bf857dddSLiam R. Howlett 	for (__entry = mt_find(__tree, &(__index), __max); \
890bf857dddSLiam R. Howlett 		__entry; __entry = mt_find_after(__tree, &(__index), __max))
891bf857dddSLiam R. Howlett 
89254a611b6SLiam R. Howlett #endif /*_LINUX_MAPLE_TREE_H */
893