1 #ifndef _LINUX_GENERIC_RADIX_TREE_H
2 #define _LINUX_GENERIC_RADIX_TREE_H
3 
4 /**
5  * DOC: Generic radix trees/sparse arrays
6  *
7  * Very simple and minimalistic, supporting arbitrary size entries up to
8  * PAGE_SIZE.
9  *
10  * A genradix is defined with the type it will store, like so:
11  *
12  * static GENRADIX(struct foo) foo_genradix;
13  *
14  * The main operations are:
15  *
16  * - genradix_init(radix) - initialize an empty genradix
17  *
18  * - genradix_free(radix) - free all memory owned by the genradix and
19  *   reinitialize it
20  *
21  * - genradix_ptr(radix, idx) - gets a pointer to the entry at idx, returning
22  *   NULL if that entry does not exist
23  *
24  * - genradix_ptr_alloc(radix, idx, gfp) - gets a pointer to an entry,
25  *   allocating it if necessary
26  *
27  * - genradix_for_each(radix, iter, p) - iterate over each entry in a genradix
28  *
29  * The radix tree allocates one page of entries at a time, so entries may exist
30  * that were never explicitly allocated - they will be initialized to all
31  * zeroes.
32  *
33  * Internally, a genradix is just a radix tree of pages, and indexing works in
34  * terms of byte offsets. The wrappers in this header file use sizeof on the
35  * type the radix contains to calculate a byte offset from the index - see
36  * __idx_to_offset.
37  */
38 
39 #include <asm/page.h>
40 #include <linux/bug.h>
41 #include <linux/limits.h>
42 #include <linux/log2.h>
43 #include <linux/math.h>
44 #include <linux/types.h>
45 
46 struct genradix_root;
47 
48 struct __genradix {
49 	struct genradix_root		*root;
50 };
51 
52 /*
53  * NOTE: currently, sizeof(_type) must not be larger than PAGE_SIZE:
54  */
55 
56 #define __GENRADIX_INITIALIZER					\
57 	{							\
58 		.tree = {					\
59 			.root = NULL,				\
60 		}						\
61 	}
62 
63 /*
64  * We use a 0 size array to stash the type we're storing without taking any
65  * space at runtime - then the various accessor macros can use typeof() to get
66  * to it for casts/sizeof - we also force the alignment so that storing a type
67  * with a ridiculous alignment doesn't blow up the alignment or size of the
68  * genradix.
69  */
70 
71 #define GENRADIX(_type)						\
72 struct {							\
73 	struct __genradix	tree;				\
74 	_type			type[0] __aligned(1);		\
75 }
76 
77 #define DEFINE_GENRADIX(_name, _type)				\
78 	GENRADIX(_type) _name = __GENRADIX_INITIALIZER
79 
80 /**
81  * genradix_init - initialize a genradix
82  * @_radix:	genradix to initialize
83  *
84  * Does not fail
85  */
86 #define genradix_init(_radix)					\
87 do {								\
88 	*(_radix) = (typeof(*_radix)) __GENRADIX_INITIALIZER;	\
89 } while (0)
90 
91 void __genradix_free(struct __genradix *);
92 
93 /**
94  * genradix_free: free all memory owned by a genradix
95  * @_radix: the genradix to free
96  *
97  * After freeing, @_radix will be reinitialized and empty
98  */
99 #define genradix_free(_radix)	__genradix_free(&(_radix)->tree)
100 
__idx_to_offset(size_t idx,size_t obj_size)101 static inline size_t __idx_to_offset(size_t idx, size_t obj_size)
102 {
103 	if (__builtin_constant_p(obj_size))
104 		BUILD_BUG_ON(obj_size > PAGE_SIZE);
105 	else
106 		BUG_ON(obj_size > PAGE_SIZE);
107 
108 	if (!is_power_of_2(obj_size)) {
109 		size_t objs_per_page = PAGE_SIZE / obj_size;
110 
111 		return (idx / objs_per_page) * PAGE_SIZE +
112 			(idx % objs_per_page) * obj_size;
113 	} else {
114 		return idx * obj_size;
115 	}
116 }
117 
118 #define __genradix_cast(_radix)		(typeof((_radix)->type[0]) *)
119 #define __genradix_obj_size(_radix)	sizeof((_radix)->type[0])
120 #define __genradix_idx_to_offset(_radix, _idx)			\
121 	__idx_to_offset(_idx, __genradix_obj_size(_radix))
122 
123 void *__genradix_ptr(struct __genradix *, size_t);
124 
125 /**
126  * genradix_ptr - get a pointer to a genradix entry
127  * @_radix:	genradix to access
128  * @_idx:	index to fetch
129  *
130  * Returns a pointer to entry at @_idx, or NULL if that entry does not exist.
131  */
132 #define genradix_ptr(_radix, _idx)				\
133 	(__genradix_cast(_radix)				\
134 	 __genradix_ptr(&(_radix)->tree,			\
135 			__genradix_idx_to_offset(_radix, _idx)))
136 
137 void *__genradix_ptr_alloc(struct __genradix *, size_t, gfp_t);
138 
139 /**
140  * genradix_ptr_alloc - get a pointer to a genradix entry, allocating it
141  *			if necessary
142  * @_radix:	genradix to access
143  * @_idx:	index to fetch
144  * @_gfp:	gfp mask
145  *
146  * Returns a pointer to entry at @_idx, or NULL on allocation failure
147  */
148 #define genradix_ptr_alloc(_radix, _idx, _gfp)			\
149 	(__genradix_cast(_radix)				\
150 	 __genradix_ptr_alloc(&(_radix)->tree,			\
151 			__genradix_idx_to_offset(_radix, _idx),	\
152 			_gfp))
153 
154 struct genradix_iter {
155 	size_t			offset;
156 	size_t			pos;
157 };
158 
159 /**
160  * genradix_iter_init - initialize a genradix_iter
161  * @_radix:	genradix that will be iterated over
162  * @_idx:	index to start iterating from
163  */
164 #define genradix_iter_init(_radix, _idx)			\
165 	((struct genradix_iter) {				\
166 		.pos	= (_idx),				\
167 		.offset	= __genradix_idx_to_offset((_radix), (_idx)),\
168 	})
169 
170 void *__genradix_iter_peek(struct genradix_iter *, struct __genradix *, size_t);
171 
172 /**
173  * genradix_iter_peek - get first entry at or above iterator's current
174  *			position
175  * @_iter:	a genradix_iter
176  * @_radix:	genradix being iterated over
177  *
178  * If no more entries exist at or above @_iter's current position, returns NULL
179  */
180 #define genradix_iter_peek(_iter, _radix)			\
181 	(__genradix_cast(_radix)				\
182 	 __genradix_iter_peek(_iter, &(_radix)->tree,		\
183 			      PAGE_SIZE / __genradix_obj_size(_radix)))
184 
__genradix_iter_advance(struct genradix_iter * iter,size_t obj_size)185 static inline void __genradix_iter_advance(struct genradix_iter *iter,
186 					   size_t obj_size)
187 {
188 	if (iter->offset + obj_size < iter->offset) {
189 		iter->offset	= SIZE_MAX;
190 		iter->pos	= SIZE_MAX;
191 		return;
192 	}
193 
194 	iter->offset += obj_size;
195 
196 	if (!is_power_of_2(obj_size) &&
197 	    (iter->offset & (PAGE_SIZE - 1)) + obj_size > PAGE_SIZE)
198 		iter->offset = round_up(iter->offset, PAGE_SIZE);
199 
200 	iter->pos++;
201 }
202 
203 #define genradix_iter_advance(_iter, _radix)			\
204 	__genradix_iter_advance(_iter, __genradix_obj_size(_radix))
205 
206 #define genradix_for_each_from(_radix, _iter, _p, _start)	\
207 	for (_iter = genradix_iter_init(_radix, _start);	\
208 	     (_p = genradix_iter_peek(&_iter, _radix)) != NULL;	\
209 	     genradix_iter_advance(&_iter, _radix))
210 
211 /**
212  * genradix_for_each - iterate over entry in a genradix
213  * @_radix:	genradix to iterate over
214  * @_iter:	a genradix_iter to track current position
215  * @_p:		pointer to genradix entry type
216  *
217  * On every iteration, @_p will point to the current entry, and @_iter.pos
218  * will be the current entry's index.
219  */
220 #define genradix_for_each(_radix, _iter, _p)			\
221 	genradix_for_each_from(_radix, _iter, _p, 0)
222 
223 int __genradix_prealloc(struct __genradix *, size_t, gfp_t);
224 
225 /**
226  * genradix_prealloc - preallocate entries in a generic radix tree
227  * @_radix:	genradix to preallocate
228  * @_nr:	number of entries to preallocate
229  * @_gfp:	gfp mask
230  *
231  * Returns 0 on success, -ENOMEM on failure
232  */
233 #define genradix_prealloc(_radix, _nr, _gfp)			\
234 	 __genradix_prealloc(&(_radix)->tree,			\
235 			__genradix_idx_to_offset(_radix, _nr + 1),\
236 			_gfp)
237 
238 
239 #endif /* _LINUX_GENERIC_RADIX_TREE_H */
240