This source file includes following definitions.
- radix_tree_is_internal_node
- radix_tree_empty
- radix_tree_deref_slot
- radix_tree_deref_slot_protected
- radix_tree_deref_retry
- radix_tree_exception
- radix_tree_preload_end
- radix_tree_iter_init
- radix_tree_iter_lookup
- radix_tree_iter_retry
- __radix_tree_iter_add
- radix_tree_chunk_size
- radix_tree_next_slot
1
2
3
4
5
6
7
8 #ifndef _LINUX_RADIX_TREE_H
9 #define _LINUX_RADIX_TREE_H
10
11 #include <linux/bitops.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/preempt.h>
15 #include <linux/rcupdate.h>
16 #include <linux/spinlock.h>
17 #include <linux/types.h>
18 #include <linux/xarray.h>
19
20
21 #define radix_tree_root xarray
22 #define radix_tree_node xa_node
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 #define RADIX_TREE_ENTRY_MASK 3UL
41 #define RADIX_TREE_INTERNAL_NODE 2UL
42
43 static inline bool radix_tree_is_internal_node(void *ptr)
44 {
45 return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) ==
46 RADIX_TREE_INTERNAL_NODE;
47 }
48
49
50
51 #define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT
52 #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)
53 #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
54
55 #define RADIX_TREE_MAX_TAGS XA_MAX_MARKS
56 #define RADIX_TREE_TAG_LONGS XA_MARK_LONGS
57
58 #define RADIX_TREE_INDEX_BITS (8 * sizeof(unsigned long))
59 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
60 RADIX_TREE_MAP_SHIFT))
61
62
63 #define ROOT_IS_IDR ((__force gfp_t)4)
64
65 #define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT)
66
67 #define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask)
68
69 #define RADIX_TREE(name, mask) \
70 struct radix_tree_root name = RADIX_TREE_INIT(name, mask)
71
72 #define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask)
73
74 static inline bool radix_tree_empty(const struct radix_tree_root *root)
75 {
76 return root->xa_head == NULL;
77 }
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94 struct radix_tree_iter {
95 unsigned long index;
96 unsigned long next_index;
97 unsigned long tags;
98 struct radix_tree_node *node;
99 };
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 static inline void *radix_tree_deref_slot(void __rcu **slot)
166 {
167 return rcu_dereference(*slot);
168 }
169
170
171
172
173
174
175
176
177
178
179 static inline void *radix_tree_deref_slot_protected(void __rcu **slot,
180 spinlock_t *treelock)
181 {
182 return rcu_dereference_protected(*slot, lockdep_is_held(treelock));
183 }
184
185
186
187
188
189
190
191
192 static inline int radix_tree_deref_retry(void *arg)
193 {
194 return unlikely(radix_tree_is_internal_node(arg));
195 }
196
197
198
199
200
201
202 static inline int radix_tree_exception(void *arg)
203 {
204 return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK);
205 }
206
207 int radix_tree_insert(struct radix_tree_root *, unsigned long index,
208 void *);
209 void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index,
210 struct radix_tree_node **nodep, void __rcu ***slotp);
211 void *radix_tree_lookup(const struct radix_tree_root *, unsigned long);
212 void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *,
213 unsigned long index);
214 void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *,
215 void __rcu **slot, void *entry);
216 void radix_tree_iter_replace(struct radix_tree_root *,
217 const struct radix_tree_iter *, void __rcu **slot, void *entry);
218 void radix_tree_replace_slot(struct radix_tree_root *,
219 void __rcu **slot, void *entry);
220 void radix_tree_iter_delete(struct radix_tree_root *,
221 struct radix_tree_iter *iter, void __rcu **slot);
222 void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *);
223 void *radix_tree_delete(struct radix_tree_root *, unsigned long);
224 unsigned int radix_tree_gang_lookup(const struct radix_tree_root *,
225 void **results, unsigned long first_index,
226 unsigned int max_items);
227 int radix_tree_preload(gfp_t gfp_mask);
228 int radix_tree_maybe_preload(gfp_t gfp_mask);
229 void radix_tree_init(void);
230 void *radix_tree_tag_set(struct radix_tree_root *,
231 unsigned long index, unsigned int tag);
232 void *radix_tree_tag_clear(struct radix_tree_root *,
233 unsigned long index, unsigned int tag);
234 int radix_tree_tag_get(const struct radix_tree_root *,
235 unsigned long index, unsigned int tag);
236 void radix_tree_iter_tag_clear(struct radix_tree_root *,
237 const struct radix_tree_iter *iter, unsigned int tag);
238 unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root *,
239 void **results, unsigned long first_index,
240 unsigned int max_items, unsigned int tag);
241 unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *,
242 void __rcu ***results, unsigned long first_index,
243 unsigned int max_items, unsigned int tag);
244 int radix_tree_tagged(const struct radix_tree_root *, unsigned int tag);
245
246 static inline void radix_tree_preload_end(void)
247 {
248 preempt_enable();
249 }
250
251 void __rcu **idr_get_free(struct radix_tree_root *root,
252 struct radix_tree_iter *iter, gfp_t gfp,
253 unsigned long max);
254
255 enum {
256 RADIX_TREE_ITER_TAG_MASK = 0x0f,
257 RADIX_TREE_ITER_TAGGED = 0x10,
258 RADIX_TREE_ITER_CONTIG = 0x20,
259 };
260
261
262
263
264
265
266
267
268 static __always_inline void __rcu **
269 radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start)
270 {
271
272
273
274
275
276
277
278
279 iter->index = 0;
280 iter->next_index = start;
281 return NULL;
282 }
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297 void __rcu **radix_tree_next_chunk(const struct radix_tree_root *,
298 struct radix_tree_iter *iter, unsigned flags);
299
300
301
302
303
304
305
306
307
308
309
310 static inline void __rcu **
311 radix_tree_iter_lookup(const struct radix_tree_root *root,
312 struct radix_tree_iter *iter, unsigned long index)
313 {
314 radix_tree_iter_init(iter, index);
315 return radix_tree_next_chunk(root, iter, RADIX_TREE_ITER_CONTIG);
316 }
317
318
319
320
321
322
323
324
325
326
327 static inline __must_check
328 void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter)
329 {
330 iter->next_index = iter->index;
331 iter->tags = 0;
332 return NULL;
333 }
334
335 static inline unsigned long
336 __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots)
337 {
338 return iter->index + slots;
339 }
340
341
342
343
344
345
346
347
348
349
350
351 void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot,
352 struct radix_tree_iter *iter);
353
354
355
356
357
358
359
360 static __always_inline long
361 radix_tree_chunk_size(struct radix_tree_iter *iter)
362 {
363 return iter->next_index - iter->index;
364 }
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385 static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot,
386 struct radix_tree_iter *iter, unsigned flags)
387 {
388 if (flags & RADIX_TREE_ITER_TAGGED) {
389 iter->tags >>= 1;
390 if (unlikely(!iter->tags))
391 return NULL;
392 if (likely(iter->tags & 1ul)) {
393 iter->index = __radix_tree_iter_add(iter, 1);
394 slot++;
395 goto found;
396 }
397 if (!(flags & RADIX_TREE_ITER_CONTIG)) {
398 unsigned offset = __ffs(iter->tags);
399
400 iter->tags >>= offset++;
401 iter->index = __radix_tree_iter_add(iter, offset);
402 slot += offset;
403 goto found;
404 }
405 } else {
406 long count = radix_tree_chunk_size(iter);
407
408 while (--count > 0) {
409 slot++;
410 iter->index = __radix_tree_iter_add(iter, 1);
411
412 if (likely(*slot))
413 goto found;
414 if (flags & RADIX_TREE_ITER_CONTIG) {
415
416 iter->next_index = 0;
417 break;
418 }
419 }
420 }
421 return NULL;
422
423 found:
424 return slot;
425 }
426
427
428
429
430
431
432
433
434
435
436
437 #define radix_tree_for_each_slot(slot, root, iter, start) \
438 for (slot = radix_tree_iter_init(iter, start) ; \
439 slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \
440 slot = radix_tree_next_slot(slot, iter, 0))
441
442
443
444
445
446
447
448
449
450
451
452
453 #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \
454 for (slot = radix_tree_iter_init(iter, start) ; \
455 slot || (slot = radix_tree_next_chunk(root, iter, \
456 RADIX_TREE_ITER_TAGGED | tag)) ; \
457 slot = radix_tree_next_slot(slot, iter, \
458 RADIX_TREE_ITER_TAGGED | tag))
459
460 #endif