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