1 /*
2  * Copyright (C) 2001 Momchil Velikov
3  * Portions Copyright (C) 2001 Christoph Hellwig
4  * Copyright (C) 2005 SGI, Christoph Lameter
5  * Copyright (C) 2006 Nick Piggin
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2, or (at
10  * your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <xen/init.h>
22 #include <xen/radix-tree.h>
23 #include <xen/errno.h>
24 #include <xen/sections.h>
25 
26 struct radix_tree_path {
27 	struct radix_tree_node *node;
28 	int offset;
29 };
30 
31 #define RADIX_TREE_INDEX_BITS  (8 /* CHAR_BIT */ * sizeof(unsigned long))
32 #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \
33 					  RADIX_TREE_MAP_SHIFT))
34 
35 /*
36  * The height_to_maxindex array needs to be one deeper than the maximum
37  * path as height 0 holds only 1 entry.
38  */
39 static unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1] __read_mostly;
40 
ptr_to_indirect(void * ptr)41 static inline void *ptr_to_indirect(void *ptr)
42 {
43 	return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
44 }
45 
indirect_to_ptr(void * ptr)46 static inline void *indirect_to_ptr(void *ptr)
47 {
48 	return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
49 }
50 
51 struct rcu_node {
52 	struct radix_tree_node node;
53 	struct rcu_head rcu_head;
54 };
55 
_rcu_node_free(struct rcu_head * head)56 static void cf_check _rcu_node_free(struct rcu_head *head)
57 {
58 	struct rcu_node *rcu_node =
59 		container_of(head, struct rcu_node, rcu_head);
60 	xfree(rcu_node);
61 }
62 
radix_tree_node_alloc(void)63 static struct radix_tree_node *radix_tree_node_alloc(void)
64 {
65 	struct rcu_node *rcu_node = xzalloc(struct rcu_node);
66 
67 	return rcu_node ? &rcu_node->node : NULL;
68 }
69 
radix_tree_node_free(struct radix_tree_node * node)70 static void radix_tree_node_free(struct radix_tree_node *node)
71 {
72 	struct rcu_node *rcu_node = container_of(node, struct rcu_node, node);
73 
74 	call_rcu(&rcu_node->rcu_head, _rcu_node_free);
75 }
76 
77 /*
78  *	Return the maximum key which can be store into a
79  *	radix tree with height HEIGHT.
80  */
radix_tree_maxindex(unsigned int height)81 static inline unsigned long radix_tree_maxindex(unsigned int height)
82 {
83 	return height_to_maxindex[height];
84 }
85 
86 /*
87  *	Extend a radix tree so it can store key @index.
88  */
radix_tree_extend(struct radix_tree_root * root,unsigned long index)89 static int radix_tree_extend(struct radix_tree_root *root, unsigned long index)
90 {
91 	struct radix_tree_node *node;
92 	unsigned int height;
93 
94 	/* Figure out what the height should be.  */
95 	height = root->height + 1;
96 	while (index > radix_tree_maxindex(height))
97 		height++;
98 
99 	if (root->rnode == NULL) {
100 		root->height = height;
101 		goto out;
102 	}
103 
104 	do {
105 		unsigned int newheight;
106 		if (!(node = radix_tree_node_alloc()))
107 			return -ENOMEM;
108 
109 		/* Increase the height.  */
110 		node->slots[0] = indirect_to_ptr(root->rnode);
111 
112 		newheight = root->height+1;
113 		node->height = newheight;
114 		node->count = 1;
115 		node = ptr_to_indirect(node);
116 		rcu_assign_pointer(root->rnode, node);
117 		root->height = newheight;
118 	} while (height > root->height);
119 out:
120 	return 0;
121 }
122 
123 /**
124  *	radix_tree_insert    -    insert into a radix tree
125  *	@root:		radix tree root
126  *	@index:		index key
127  *	@item:		item to insert
128  *
129  *	Insert an item into the radix tree at position @index.
130  */
radix_tree_insert(struct radix_tree_root * root,unsigned long index,void * item)131 int radix_tree_insert(struct radix_tree_root *root,
132 			unsigned long index, void *item)
133 {
134 	struct radix_tree_node *node = NULL, *slot;
135 	unsigned int height, shift;
136 	int offset;
137 	int error;
138 
139 	BUG_ON(radix_tree_is_indirect_ptr(item));
140 
141 	/* Make sure the tree is high enough.  */
142 	if (index > radix_tree_maxindex(root->height)) {
143 		error = radix_tree_extend(root, index);
144 		if (error)
145 			return error;
146 	}
147 
148 	slot = indirect_to_ptr(root->rnode);
149 
150 	height = root->height;
151 	shift = (height-1) * RADIX_TREE_MAP_SHIFT;
152 
153 	offset = 0;			/* uninitialised var warning */
154 	while (height > 0) {
155 		if (slot == NULL) {
156 			/* Have to add a child node.  */
157 			if (!(slot = radix_tree_node_alloc()))
158 				return -ENOMEM;
159 			slot->height = height;
160 			if (node) {
161 				rcu_assign_pointer(node->slots[offset], slot);
162 				node->count++;
163 			} else
164 				rcu_assign_pointer(root->rnode, ptr_to_indirect(slot));
165 		}
166 
167 		/* Go a level down */
168 		offset = (index >> shift) & RADIX_TREE_MAP_MASK;
169 		node = slot;
170 		slot = node->slots[offset];
171 		shift -= RADIX_TREE_MAP_SHIFT;
172 		height--;
173 	}
174 
175 	if (slot != NULL)
176 		return -EEXIST;
177 
178 	if (node) {
179 		node->count++;
180 		rcu_assign_pointer(node->slots[offset], item);
181 	} else {
182 		rcu_assign_pointer(root->rnode, item);
183 	}
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL(radix_tree_insert);
188 
189 /*
190  * is_slot == 1 : search for the slot.
191  * is_slot == 0 : search for the node.
192  */
radix_tree_lookup_element(struct radix_tree_root * root,unsigned long index,int is_slot)193 static void *radix_tree_lookup_element(struct radix_tree_root *root,
194 				unsigned long index, int is_slot)
195 {
196 	unsigned int height, shift;
197 	struct radix_tree_node *node, **slot;
198 
199 	node = rcu_dereference(root->rnode);
200 	if (node == NULL)
201 		return NULL;
202 
203 	if (!radix_tree_is_indirect_ptr(node)) {
204 		if (index > 0)
205 			return NULL;
206 		return is_slot ? (void *)&root->rnode : node;
207 	}
208 	node = indirect_to_ptr(node);
209 
210 	height = node->height;
211 	if (index > radix_tree_maxindex(height))
212 		return NULL;
213 
214 	shift = (height-1) * RADIX_TREE_MAP_SHIFT;
215 
216 	do {
217 		slot = (struct radix_tree_node **)
218 			(node->slots + ((index>>shift) & RADIX_TREE_MAP_MASK));
219 		node = rcu_dereference(*slot);
220 		if (node == NULL)
221 			return NULL;
222 
223 		shift -= RADIX_TREE_MAP_SHIFT;
224 		height--;
225 	} while (height > 0);
226 
227 	return is_slot ? (void *)slot : indirect_to_ptr(node);
228 }
229 
230 /**
231  *	radix_tree_lookup_slot    -    lookup a slot in a radix tree
232  *	@root:		radix tree root
233  *	@index:		index key
234  *
235  *	Returns:  the slot corresponding to the position @index in the
236  *	radix tree @root. This is useful for update-if-exists operations.
237  *
238  *	This function can be called under rcu_read_lock iff the slot is not
239  *	modified by radix_tree_replace_slot, otherwise it must be called
240  *	exclusive from other writers. Any dereference of the slot must be done
241  *	using radix_tree_deref_slot.
242  */
radix_tree_lookup_slot(struct radix_tree_root * root,unsigned long index)243 void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
244 {
245 	return (void **)radix_tree_lookup_element(root, index, 1);
246 }
247 EXPORT_SYMBOL(radix_tree_lookup_slot);
248 
249 /**
250  *	radix_tree_lookup    -    perform lookup operation on a radix tree
251  *	@root:		radix tree root
252  *	@index:		index key
253  *
254  *	Lookup the item at the position @index in the radix tree @root.
255  *
256  *	This function can be called under rcu_read_lock, however the caller
257  *	must manage lifetimes of leaf nodes (eg. RCU may also be used to free
258  *	them safely). No RCU barriers are required to access or modify the
259  *	returned item, however.
260  */
radix_tree_lookup(struct radix_tree_root * root,unsigned long index)261 void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
262 {
263 	return radix_tree_lookup_element(root, index, 0);
264 }
265 EXPORT_SYMBOL(radix_tree_lookup);
266 
267 /**
268  *	radix_tree_next_hole    -    find the next hole (not-present entry)
269  *	@root:		tree root
270  *	@index:		index key
271  *	@max_scan:	maximum range to search
272  *
273  *	Search the set [index, min(index+max_scan-1, MAX_INDEX)] for the lowest
274  *	indexed hole.
275  *
276  *	Returns: the index of the hole if found, otherwise returns an index
277  *	outside of the set specified (in which case 'return - index >= max_scan'
278  *	will be true). In rare cases of index wrap-around, 0 will be returned.
279  *
280  *	radix_tree_next_hole may be called under rcu_read_lock. However, like
281  *	radix_tree_gang_lookup, this will not atomically search a snapshot of
282  *	the tree at a single point in time. For example, if a hole is created
283  *	at index 5, then subsequently a hole is created at index 10,
284  *	radix_tree_next_hole covering both indexes may return 10 if called
285  *	under rcu_read_lock.
286  */
radix_tree_next_hole(struct radix_tree_root * root,unsigned long index,unsigned long max_scan)287 unsigned long radix_tree_next_hole(struct radix_tree_root *root,
288 				unsigned long index, unsigned long max_scan)
289 {
290 	unsigned long i;
291 
292 	for (i = 0; i < max_scan; i++) {
293 		if (!radix_tree_lookup(root, index))
294 			break;
295 		index++;
296 		if (index == 0)
297 			break;
298 	}
299 
300 	return index;
301 }
302 EXPORT_SYMBOL(radix_tree_next_hole);
303 
304 /**
305  *	radix_tree_prev_hole    -    find the prev hole (not-present entry)
306  *	@root:		tree root
307  *	@index:		index key
308  *	@max_scan:	maximum range to search
309  *
310  *	Search backwards in the range [max(index-max_scan+1, 0), index]
311  *	for the first hole.
312  *
313  *	Returns: the index of the hole if found, otherwise returns an index
314  *	outside of the set specified (in which case 'index - return >= max_scan'
315  *	will be true). In rare cases of wrap-around, ULONG_MAX will be returned.
316  *
317  *	radix_tree_next_hole may be called under rcu_read_lock. However, like
318  *	radix_tree_gang_lookup, this will not atomically search a snapshot of
319  *	the tree at a single point in time. For example, if a hole is created
320  *	at index 10, then subsequently a hole is created at index 5,
321  *	radix_tree_prev_hole covering both indexes may return 5 if called under
322  *	rcu_read_lock.
323  */
radix_tree_prev_hole(struct radix_tree_root * root,unsigned long index,unsigned long max_scan)324 unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
325 				   unsigned long index, unsigned long max_scan)
326 {
327 	unsigned long i;
328 
329 	for (i = 0; i < max_scan; i++) {
330 		if (!radix_tree_lookup(root, index))
331 			break;
332 		index--;
333 		if (index == ULONG_MAX)
334 			break;
335 	}
336 
337 	return index;
338 }
339 EXPORT_SYMBOL(radix_tree_prev_hole);
340 
341 static unsigned int
__lookup(struct radix_tree_node * slot,void *** results,unsigned long index,unsigned int max_items,unsigned long * next_index)342 __lookup(struct radix_tree_node *slot, void ***results, unsigned long index,
343 	unsigned int max_items, unsigned long *next_index)
344 {
345 	unsigned int nr_found = 0;
346 	unsigned int shift, height;
347 	unsigned long i;
348 
349 	height = slot->height;
350 	if (height == 0)
351 		goto out;
352 	shift = (height-1) * RADIX_TREE_MAP_SHIFT;
353 
354 	for ( ; height > 1; height--) {
355 		i = (index >> shift) & RADIX_TREE_MAP_MASK;
356 		for (;;) {
357 			if (slot->slots[i] != NULL)
358 				break;
359 			index &= ~((1UL << shift) - 1);
360 			index += 1UL << shift;
361 			if (index == 0)
362 				goto out;	/* 32-bit wraparound */
363 			i++;
364 			if (i == RADIX_TREE_MAP_SIZE)
365 				goto out;
366 		}
367 
368 		shift -= RADIX_TREE_MAP_SHIFT;
369 		slot = rcu_dereference(slot->slots[i]);
370 		if (slot == NULL)
371 			goto out;
372 	}
373 
374 	/* Bottom level: grab some items */
375 	for (i = index & RADIX_TREE_MAP_MASK; i < RADIX_TREE_MAP_SIZE; i++) {
376 		index++;
377 		if (slot->slots[i]) {
378 			results[nr_found++] = &(slot->slots[i]);
379 			if (nr_found == max_items)
380 				goto out;
381 		}
382 	}
383 out:
384 	*next_index = index;
385 	return nr_found;
386 }
387 
388 /**
389  *	radix_tree_gang_lookup - perform multiple lookup on a radix tree
390  *	@root:		radix tree root
391  *	@results:	where the results of the lookup are placed
392  *	@first_index:	start the lookup from this key
393  *	@max_items:	place up to this many items at *results
394  *
395  *	Performs an index-ascending scan of the tree for present items.  Places
396  *	them at *@results and returns the number of items which were placed at
397  *	*@results.
398  *
399  *	The implementation is naive.
400  *
401  *	Like radix_tree_lookup, radix_tree_gang_lookup may be called under
402  *	rcu_read_lock. In this case, rather than the returned results being
403  *	an atomic snapshot of the tree at a single point in time, the semantics
404  *	of an RCU protected gang lookup are as though multiple radix_tree_lookups
405  *	have been issued in individual locks, and results stored in 'results'.
406  */
407 unsigned int
radix_tree_gang_lookup(struct radix_tree_root * root,void ** results,unsigned long first_index,unsigned int max_items)408 radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
409 			unsigned long first_index, unsigned int max_items)
410 {
411 	unsigned long max_index;
412 	struct radix_tree_node *node;
413 	unsigned long cur_index = first_index;
414 	unsigned int ret;
415 
416 	node = rcu_dereference(root->rnode);
417 	if (!node)
418 		return 0;
419 
420 	if (!radix_tree_is_indirect_ptr(node)) {
421 		if (first_index > 0)
422 			return 0;
423 		results[0] = node;
424 		return 1;
425 	}
426 	node = indirect_to_ptr(node);
427 
428 	max_index = radix_tree_maxindex(node->height);
429 
430 	ret = 0;
431 	while (ret < max_items) {
432 		unsigned int nr_found, slots_found, i;
433 		unsigned long next_index;	/* Index of next search */
434 
435 		if (cur_index > max_index)
436 			break;
437 		slots_found = __lookup(node, (void ***)results + ret, cur_index,
438 					max_items - ret, &next_index);
439 		nr_found = 0;
440 		for (i = 0; i < slots_found; i++) {
441 			struct radix_tree_node *slot;
442 			slot = *(((void ***)results)[ret + i]);
443 			if (!slot)
444 				continue;
445 			results[ret + nr_found] =
446 				indirect_to_ptr(rcu_dereference(slot));
447 			nr_found++;
448 		}
449 		ret += nr_found;
450 		if (next_index == 0)
451 			break;
452 		cur_index = next_index;
453 	}
454 
455 	return ret;
456 }
457 EXPORT_SYMBOL(radix_tree_gang_lookup);
458 
459 /**
460  *	radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
461  *	@root:		radix tree root
462  *	@results:	where the results of the lookup are placed
463  *	@first_index:	start the lookup from this key
464  *	@max_items:	place up to this many items at *results
465  *
466  *	Performs an index-ascending scan of the tree for present items.  Places
467  *	their slots at *@results and returns the number of items which were
468  *	placed at *@results.
469  *
470  *	The implementation is naive.
471  *
472  *	Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
473  *	be dereferenced with radix_tree_deref_slot, and if using only RCU
474  *	protection, radix_tree_deref_slot may fail requiring a retry.
475  */
476 unsigned int
radix_tree_gang_lookup_slot(struct radix_tree_root * root,void *** results,unsigned long first_index,unsigned int max_items)477 radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
478 			unsigned long first_index, unsigned int max_items)
479 {
480 	unsigned long max_index;
481 	struct radix_tree_node *node;
482 	unsigned long cur_index = first_index;
483 	unsigned int ret;
484 
485 	node = rcu_dereference(root->rnode);
486 	if (!node)
487 		return 0;
488 
489 	if (!radix_tree_is_indirect_ptr(node)) {
490 		if (first_index > 0)
491 			return 0;
492 		results[0] = (void **)&root->rnode;
493 		return 1;
494 	}
495 	node = indirect_to_ptr(node);
496 
497 	max_index = radix_tree_maxindex(node->height);
498 
499 	ret = 0;
500 	while (ret < max_items) {
501 		unsigned int slots_found;
502 		unsigned long next_index;	/* Index of next search */
503 
504 		if (cur_index > max_index)
505 			break;
506 		slots_found = __lookup(node, results + ret, cur_index,
507 					max_items - ret, &next_index);
508 		ret += slots_found;
509 		if (next_index == 0)
510 			break;
511 		cur_index = next_index;
512 	}
513 
514 	return ret;
515 }
516 EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
517 
518 /**
519  *	radix_tree_shrink    -    shrink height of a radix tree to minimal
520  *	@root		radix tree root
521  */
radix_tree_shrink(struct radix_tree_root * root)522 static inline void radix_tree_shrink(struct radix_tree_root *root)
523 {
524 	/* try to shrink tree height */
525 	while (root->height > 0) {
526 		struct radix_tree_node *to_free = root->rnode;
527 		void *newptr;
528 
529 		BUG_ON(!radix_tree_is_indirect_ptr(to_free));
530 		to_free = indirect_to_ptr(to_free);
531 
532 		/*
533 		 * The candidate node has more than one child, or its child
534 		 * is not at the leftmost slot, we cannot shrink.
535 		 */
536 		if (to_free->count != 1)
537 			break;
538 		if (!to_free->slots[0])
539 			break;
540 
541 		/*
542 		 * We don't need rcu_assign_pointer(), since we are simply
543 		 * moving the node from one part of the tree to another: if it
544 		 * was safe to dereference the old pointer to it
545 		 * (to_free->slots[0]), it will be safe to dereference the new
546 		 * one (root->rnode) as far as dependent read barriers go.
547 		 */
548 		newptr = to_free->slots[0];
549 		if (root->height > 1)
550 			newptr = ptr_to_indirect(newptr);
551 		root->rnode = newptr;
552 		root->height--;
553 
554 		/*
555 		 * We have a dilemma here. The node's slot[0] must not be
556 		 * NULLed in case there are concurrent lookups expecting to
557 		 * find the item. However if this was a bottom-level node,
558 		 * then it may be subject to the slot pointer being visible
559 		 * to callers dereferencing it. If item corresponding to
560 		 * slot[0] is subsequently deleted, these callers would expect
561 		 * their slot to become empty sooner or later.
562 		 *
563 		 * For example, lockless pagecache will look up a slot, deref
564 		 * the page pointer, and if the page is 0 refcount it means it
565 		 * was concurrently deleted from pagecache so try the deref
566 		 * again. Fortunately there is already a requirement for logic
567 		 * to retry the entire slot lookup -- the indirect pointer
568 		 * problem (replacing direct root node with an indirect pointer
569 		 * also results in a stale slot). So tag the slot as indirect
570 		 * to force callers to retry.
571 		 */
572 		if (root->height == 0)
573 			*((unsigned long *)&to_free->slots[0]) |=
574 						RADIX_TREE_INDIRECT_PTR;
575 
576 		radix_tree_node_free(to_free);
577 	}
578 }
579 
580 /**
581  *	radix_tree_delete    -    delete an item from a radix tree
582  *	@root:		radix tree root
583  *	@index:		index key
584  *
585  *	Remove the item at @index from the radix tree rooted at @root.
586  *
587  *	Returns the address of the deleted item, or NULL if it was not present.
588  */
radix_tree_delete(struct radix_tree_root * root,unsigned long index)589 void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
590 {
591 	/*
592 	 * The radix tree path needs to be one longer than the maximum path
593 	 * since the "list" is null terminated.
594 	 */
595 	struct radix_tree_path path[RADIX_TREE_MAX_PATH + 1], *pathp = path;
596 	struct radix_tree_node *slot = NULL;
597 	struct radix_tree_node *to_free;
598 	unsigned int height, shift;
599 	int offset;
600 
601 	height = root->height;
602 	if (index > radix_tree_maxindex(height))
603 		goto out;
604 
605 	slot = root->rnode;
606 	if (height == 0) {
607 		root->rnode = NULL;
608 		goto out;
609 	}
610 	slot = indirect_to_ptr(slot);
611 
612 	shift = (height - 1) * RADIX_TREE_MAP_SHIFT;
613 	pathp->node = NULL;
614 
615 	do {
616 		if (slot == NULL)
617 			goto out;
618 
619 		pathp++;
620 		offset = (index >> shift) & RADIX_TREE_MAP_MASK;
621 		pathp->offset = offset;
622 		pathp->node = slot;
623 		slot = slot->slots[offset];
624 		shift -= RADIX_TREE_MAP_SHIFT;
625 		height--;
626 	} while (height > 0);
627 
628 	if (slot == NULL)
629 		goto out;
630 
631 	to_free = NULL;
632 	/* Now free the nodes we do not need anymore */
633 	while (pathp->node) {
634 		pathp->node->slots[pathp->offset] = NULL;
635 		pathp->node->count--;
636 		/*
637 		 * Queue the node for deferred freeing after the
638 		 * last reference to it disappears (set NULL, above).
639 		 */
640 		if (to_free)
641 			radix_tree_node_free(to_free);
642 
643 		if (pathp->node->count) {
644 			if (pathp->node == indirect_to_ptr(root->rnode))
645 				radix_tree_shrink(root);
646 			goto out;
647 		}
648 
649 		/* Node with zero slots in use so free it */
650 		to_free = pathp->node;
651 		pathp--;
652 
653 	}
654 	root->height = 0;
655 	root->rnode = NULL;
656 	if (to_free)
657 		radix_tree_node_free(to_free);
658 
659 out:
660 	return slot;
661 }
662 EXPORT_SYMBOL(radix_tree_delete);
663 
664 static void
radix_tree_node_destroy(struct radix_tree_root * root,struct radix_tree_node * node,void (* slot_free)(void *))665 radix_tree_node_destroy(
666 	struct radix_tree_root *root, struct radix_tree_node *node,
667 	void (*slot_free)(void *))
668 {
669 	int i;
670 
671 	for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
672 		struct radix_tree_node *slot = node->slots[i];
673 		BUG_ON(radix_tree_is_indirect_ptr(slot));
674 		if (slot == NULL)
675 			continue;
676 		if (node->height == 1) {
677 			if (slot_free)
678 				slot_free(slot);
679 		} else {
680 			radix_tree_node_destroy(root, slot, slot_free);
681 		}
682 	}
683 
684 	radix_tree_node_free(node);
685 }
686 
radix_tree_destroy(struct radix_tree_root * root,void (* slot_free)(void *))687 void radix_tree_destroy(
688 	struct radix_tree_root *root,
689 	void (*slot_free)(void *))
690 {
691 	struct radix_tree_node *node = root->rnode;
692 	if (node == NULL)
693 		return;
694 	if (!radix_tree_is_indirect_ptr(node)) {
695 		if (slot_free)
696 			slot_free(node);
697 	} else {
698 		node = indirect_to_ptr(node);
699 		radix_tree_node_destroy(root, node, slot_free);
700 	}
701 	radix_tree_init(root);
702 }
703 
radix_tree_init(struct radix_tree_root * root)704 void radix_tree_init(struct radix_tree_root *root)
705 {
706 	*root = (struct radix_tree_root)RADIX_TREE_INIT();
707 }
708 
__maxindex(unsigned int height)709 static __init unsigned long __maxindex(unsigned int height)
710 {
711 	unsigned int width = height * RADIX_TREE_MAP_SHIFT;
712 	int shift = RADIX_TREE_INDEX_BITS - width;
713 
714 	if (shift < 0)
715 		return ~0UL;
716 	if (shift >= BITS_PER_LONG)
717 		return 0UL;
718 	return ~0UL >> shift;
719 }
720 
radix_tree_init_maxindex(void)721 static int __init cf_check radix_tree_init_maxindex(void)
722 {
723 	unsigned int i;
724 
725 	for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
726 		height_to_maxindex[i] = __maxindex(i);
727 
728 	return 0;
729 }
730 /* pre-SMP just so it runs before 'normal' initcalls */
731 presmp_initcall(radix_tree_init_maxindex);
732