blob: 741da5a77fd5247ec9390b3312de57e423ea7b24 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
Christoph Lametercde53532008-07-04 09:59:22 -07004 * Copyright (C) 2005 SGI, Christoph Lameter
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08005 * Copyright (C) 2006 Nick Piggin
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07006 * Copyright (C) 2012 Konstantin Khlebnikov
Matthew Wilcox6b053b82016-05-20 17:02:58 -07007 * Copyright (C) 2016 Intel, Matthew Wilcox
8 * Copyright (C) 2016 Intel, Ross Zwisler
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25#include <linux/errno.h>
26#include <linux/init.h>
27#include <linux/kernel.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050028#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/radix-tree.h>
30#include <linux/percpu.h>
31#include <linux/slab.h>
Catalin Marinasce80b062014-06-06 14:38:18 -070032#include <linux/kmemleak.h>
Linus Torvalds2b412262016-12-09 10:41:42 -080033#include <linux/notifier.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/string.h>
36#include <linux/bitops.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080037#include <linux/rcupdate.h>
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020038#include <linux/preempt.h> /* in_interrupt() */
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +010039#include <linux/locallock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -070041/* Number of nodes in fully populated tree of given height */
42static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
43
Jeff Moyer26fb1582007-10-16 01:24:49 -070044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Radix tree node cache.
46 */
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
Nick Piggin55368052012-05-29 15:07:34 -070050 * The radix tree is variable-height, so an insert operation not only has
51 * to build the branch to its corresponding item, it also has to build the
52 * branch to existing items if the size has to be increased (by
53 * radix_tree_extend).
54 *
55 * The worst case is a zero height tree with just a single item at index 0,
56 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
57 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
58 * Hence:
59 */
60#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
61
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * Per-cpu pool of preloaded nodes
64 */
65struct radix_tree_preload {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -070066 unsigned nr;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -070067 /* nodes->private_data points to next preallocated node */
68 struct radix_tree_node *nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080070static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +010071static DEFINE_LOCAL_IRQ_LOCK(radix_tree_preloads_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070073static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080074{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070075 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -080076}
77
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070078#define RADIX_TREE_RETRY node_to_entry(NULL)
Matthew Wilcoxafe0e392016-05-20 17:02:17 -070079
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070080#ifdef CONFIG_RADIX_TREE_MULTIORDER
81/* Sibling slots point directly to another slot in the same node */
82static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
83{
84 void **ptr = node;
85 return (parent->slots <= ptr) &&
86 (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
87}
88#else
89static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
90{
91 return false;
92}
93#endif
94
95static inline unsigned long get_slot_offset(struct radix_tree_node *parent,
96 void **slot)
97{
98 return slot - parent->slots;
99}
100
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700101static unsigned int radix_tree_descend(struct radix_tree_node *parent,
102 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700103{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700104 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700105 void **entry = rcu_dereference_raw(parent->slots[offset]);
106
107#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700108 if (radix_tree_is_internal_node(entry)) {
Linus Torvalds8d2c0d32016-09-25 13:32:46 -0700109 if (is_sibling_entry(parent, entry)) {
110 void **sibentry = (void **) entry_to_node(entry);
111 offset = get_slot_offset(parent, sibentry);
112 entry = rcu_dereference_raw(*sibentry);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700113 }
114 }
115#endif
116
117 *nodep = (void *)entry;
118 return offset;
119}
120
Nick Piggin612d6c12006-06-23 02:03:22 -0700121static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
122{
123 return root->gfp_mask & __GFP_BITS_MASK;
124}
125
Nick Piggin643b52b2008-06-12 15:21:52 -0700126static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
127 int offset)
128{
129 __set_bit(offset, node->tags[tag]);
130}
131
132static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
133 int offset)
134{
135 __clear_bit(offset, node->tags[tag]);
136}
137
138static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
139 int offset)
140{
141 return test_bit(offset, node->tags[tag]);
142}
143
144static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
145{
146 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
147}
148
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700149static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700150{
151 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
152}
153
154static inline void root_tag_clear_all(struct radix_tree_root *root)
155{
156 root->gfp_mask &= __GFP_BITS_MASK;
157}
158
159static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
160{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700161 return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700162}
163
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700164static inline unsigned root_tags_get(struct radix_tree_root *root)
165{
166 return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT;
167}
168
Nick Piggin643b52b2008-06-12 15:21:52 -0700169/*
170 * Returns 1 if any slot in the node has this tag set.
171 * Otherwise returns 0.
172 */
173static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
174{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700175 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700176 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
177 if (node->tags[tag][idx])
178 return 1;
179 }
180 return 0;
181}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700182
183/**
184 * radix_tree_find_next_bit - find the next set bit in a memory region
185 *
186 * @addr: The address to base the search on
187 * @size: The bitmap size in bits
188 * @offset: The bitnumber to start searching at
189 *
190 * Unrollable variant of find_next_bit() for constant size arrays.
191 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
192 * Returns next bit offset, or size if nothing found.
193 */
194static __always_inline unsigned long
195radix_tree_find_next_bit(const unsigned long *addr,
196 unsigned long size, unsigned long offset)
197{
198 if (!__builtin_constant_p(size))
199 return find_next_bit(addr, size, offset);
200
201 if (offset < size) {
202 unsigned long tmp;
203
204 addr += offset / BITS_PER_LONG;
205 tmp = *addr >> (offset % BITS_PER_LONG);
206 if (tmp)
207 return __ffs(tmp) + offset;
208 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
209 while (offset < size) {
210 tmp = *++addr;
211 if (tmp)
212 return __ffs(tmp) + offset;
213 offset += BITS_PER_LONG;
214 }
215 }
216 return size;
217}
218
Ross Zwisler0796c582016-05-20 17:02:55 -0700219#ifndef __KERNEL__
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700220static void dump_node(struct radix_tree_node *node, unsigned long index)
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700221{
Ross Zwisler0796c582016-05-20 17:02:55 -0700222 unsigned long i;
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700223
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700224 pr_debug("radix node: %p offset %d tags %lx %lx %lx shift %d count %d parent %p\n",
Matthew Wilcox0c7fa0a2016-05-20 17:03:07 -0700225 node, node->offset,
Ross Zwisler0796c582016-05-20 17:02:55 -0700226 node->tags[0][0], node->tags[1][0], node->tags[2][0],
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700227 node->shift, node->count, node->parent);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700228
Ross Zwisler0796c582016-05-20 17:02:55 -0700229 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700230 unsigned long first = index | (i << node->shift);
231 unsigned long last = first | ((1UL << node->shift) - 1);
Ross Zwisler0796c582016-05-20 17:02:55 -0700232 void *entry = node->slots[i];
233 if (!entry)
234 continue;
235 if (is_sibling_entry(node, entry)) {
236 pr_debug("radix sblng %p offset %ld val %p indices %ld-%ld\n",
237 entry, i,
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700238 *(void **)entry_to_node(entry),
Ross Zwisler0796c582016-05-20 17:02:55 -0700239 first, last);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700240 } else if (!radix_tree_is_internal_node(entry)) {
Ross Zwisler0796c582016-05-20 17:02:55 -0700241 pr_debug("radix entry %p offset %ld indices %ld-%ld\n",
242 entry, i, first, last);
243 } else {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700244 dump_node(entry_to_node(entry), first);
Ross Zwisler0796c582016-05-20 17:02:55 -0700245 }
246 }
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700247}
248
249/* For debug */
250static void radix_tree_dump(struct radix_tree_root *root)
251{
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700252 pr_debug("radix root: %p rnode %p tags %x\n",
253 root, root->rnode,
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700254 root->gfp_mask >> __GFP_BITS_SHIFT);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700255 if (!radix_tree_is_internal_node(root->rnode))
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700256 return;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700257 dump_node(entry_to_node(root->rnode), 0);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700258}
259#endif
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261/*
262 * This assumes that the caller has performed appropriate preallocation, and
263 * that the caller has pinned this thread of control to the current CPU.
264 */
265static struct radix_tree_node *
266radix_tree_node_alloc(struct radix_tree_root *root)
267{
Nick Piggine2848a02008-02-04 22:29:10 -0800268 struct radix_tree_node *ret = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700269 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jan Kara5e4c0d972013-09-11 14:26:05 -0700271 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700272 * Preload code isn't irq safe and it doesn't make sense to use
273 * preloading during an interrupt anyway as all the allocations have
274 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700275 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800276 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 struct radix_tree_preload *rtp;
278
Nick Piggine2848a02008-02-04 22:29:10 -0800279 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700280 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700281 * cache first for the new node to get accounted to the memory
282 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700283 */
284 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700285 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700286 if (ret)
287 goto out;
288
289 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800290 * Provided the caller has preloaded here, we will always
291 * succeed in getting a node here (and never reach
292 * kmem_cache_alloc)
293 */
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100294 rtp = &get_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700296 ret = rtp->nodes;
297 rtp->nodes = ret->private_data;
298 ret->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 rtp->nr--;
300 }
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100301 put_locked_var(radix_tree_preloads_lock, radix_tree_preloads);
Catalin Marinasce80b062014-06-06 14:38:18 -0700302 /*
303 * Update the allocation stack trace as this is more useful
304 * for debugging.
305 */
306 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700307 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700309 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700310out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700311 BUG_ON(radix_tree_is_internal_node(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return ret;
313}
314
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800315static void radix_tree_node_rcu_free(struct rcu_head *head)
316{
317 struct radix_tree_node *node =
318 container_of(head, struct radix_tree_node, rcu_head);
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000319 int i;
Nick Piggin643b52b2008-06-12 15:21:52 -0700320
321 /*
322 * must only free zeroed nodes into the slab. radix_tree_shrink
323 * can leave us with a non-NULL entry in the first slot, so clear
324 * that here to make sure.
325 */
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000326 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
327 tag_clear(node, i, 0);
328
Nick Piggin643b52b2008-06-12 15:21:52 -0700329 node->slots[0] = NULL;
330 node->count = 0;
331
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800332 kmem_cache_free(radix_tree_node_cachep, node);
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static inline void
336radix_tree_node_free(struct radix_tree_node *node)
337{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800338 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
342 * Load up this CPU's radix_tree_node buffer with sufficient objects to
343 * ensure that the addition of a single element in the tree cannot fail. On
344 * success, return zero, with preemption disabled. On error, return -ENOMEM
345 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000346 *
347 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800348 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 */
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700350static int __radix_tree_preload(gfp_t gfp_mask, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 struct radix_tree_preload *rtp;
353 struct radix_tree_node *node;
354 int ret = -ENOMEM;
355
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700356 /*
357 * Nodes preloaded by one cgroup can be be used by another cgroup, so
358 * they should never be accounted to any particular memory cgroup.
359 */
360 gfp_mask &= ~__GFP_ACCOUNT;
361
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100362 local_lock(radix_tree_preloads_lock);
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700363 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700364 while (rtp->nr < nr) {
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100365 local_unlock(radix_tree_preloads_lock);
Christoph Lameter488514d2008-04-28 02:12:05 -0700366 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (node == NULL)
368 goto out;
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100369 local_lock(radix_tree_preloads_lock);
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700370 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700371 if (rtp->nr < nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700372 node->private_data = rtp->nodes;
373 rtp->nodes = node;
374 rtp->nr++;
375 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 ret = 0;
380out:
381 return ret;
382}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700383
384/*
385 * Load up this CPU's radix_tree_node buffer with sufficient objects to
386 * ensure that the addition of a single element in the tree cannot fail. On
387 * success, return zero, with preemption disabled. On error, return -ENOMEM
388 * with preemption not disabled.
389 *
390 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800391 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700392 */
393int radix_tree_preload(gfp_t gfp_mask)
394{
395 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800396 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700397 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700398}
David Chinnerd7f09232007-07-14 16:05:04 +1000399EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Nick Piggin6e954b92006-01-08 01:01:40 -0800401/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700402 * The same as above function, except we don't guarantee preloading happens.
403 * We do it, if we decide it helps. On success, return zero with preemption
404 * disabled. On error, return -ENOMEM with preemption not disabled.
405 */
406int radix_tree_maybe_preload(gfp_t gfp_mask)
407{
Mel Gormand0164ad2015-11-06 16:28:21 -0800408 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700409 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700410 /* Preloading doesn't help anything with this gfp mask, skip it */
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100411 local_lock(radix_tree_preloads_lock);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700412 return 0;
413}
414EXPORT_SYMBOL(radix_tree_maybe_preload);
415
416/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700417 * The same as function above, but preload number of nodes required to insert
418 * (1 << order) continuous naturally-aligned elements.
419 */
420int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
421{
422 unsigned long nr_subtrees;
423 int nr_nodes, subtree_height;
424
425 /* Preloading doesn't help anything with this gfp mask, skip it */
426 if (!gfpflags_allow_blocking(gfp_mask)) {
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100427 local_lock(radix_tree_preloads_lock);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700428 return 0;
429 }
430
431 /*
432 * Calculate number and height of fully populated subtrees it takes to
433 * store (1 << order) elements.
434 */
435 nr_subtrees = 1 << order;
436 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
437 subtree_height++)
438 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
439
440 /*
441 * The worst case is zero height tree with a single item at index 0 and
442 * then inserting items starting at ULONG_MAX - (1 << order).
443 *
444 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
445 * 0-index item.
446 */
447 nr_nodes = RADIX_TREE_MAX_PATH;
448
449 /* Plus branch to fully populated subtrees. */
450 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
451
452 /* Root node is shared. */
453 nr_nodes--;
454
455 /* Plus nodes required to build subtrees. */
456 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
457
458 return __radix_tree_preload(gfp_mask, nr_nodes);
459}
Sebastian Andrzej Siewior682d5892017-01-25 16:34:27 +0100460
461void radix_tree_preload_end(void)
462{
463 local_unlock(radix_tree_preloads_lock);
464}
465EXPORT_SYMBOL(radix_tree_preload_end);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700466
467/*
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700468 * The maximum index which can be stored in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700470static inline unsigned long shift_maxindex(unsigned int shift)
471{
472 return (RADIX_TREE_MAP_SIZE << shift) - 1;
473}
474
Matthew Wilcox1456a432016-05-20 17:02:08 -0700475static inline unsigned long node_maxindex(struct radix_tree_node *node)
476{
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700477 return shift_maxindex(node->shift);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700478}
479
480static unsigned radix_tree_load_root(struct radix_tree_root *root,
481 struct radix_tree_node **nodep, unsigned long *maxindex)
482{
483 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
484
485 *nodep = node;
486
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700487 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700488 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700489 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700490 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700491 }
492
493 *maxindex = 0;
494 return 0;
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*
498 * Extend a radix tree so it can store key @index.
499 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700500static int radix_tree_extend(struct radix_tree_root *root,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700501 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800503 struct radix_tree_node *slot;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700504 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int tag;
506
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700507 /* Figure out what the shift should be. */
508 maxshift = shift;
509 while (index > shift_maxindex(maxshift))
510 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700512 slot = root->rnode;
513 if (!slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 do {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700517 struct radix_tree_node *node = radix_tree_node_alloc(root);
518
519 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 return -ENOMEM;
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800523 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700524 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 tag_set(node, tag, 0);
526 }
527
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700528 BUG_ON(shift > BITS_PER_LONG);
529 node->shift = shift;
Matthew Wilcox0c7fa0a2016-05-20 17:03:07 -0700530 node->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 node->count = 1;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800532 node->parent = NULL;
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700533 if (radix_tree_is_internal_node(slot))
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700534 entry_to_node(slot)->parent = node;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800535 node->slots[0] = slot;
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -0700536 slot = node_to_entry(node);
537 rcu_assign_pointer(root->rnode, slot);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700538 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700539 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700541 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700545 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 * @root: radix tree root
547 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700548 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700549 * @nodep: returns node
550 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700552 * Create, if necessary, and return the node and slot for an item
553 * at position @index in the radix tree @root.
554 *
555 * Until there is more than one item in the tree, no nodes are
556 * allocated and @root->rnode is used as a direct slot instead of
557 * pointing to a node, in which case *@nodep will be NULL.
558 *
559 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700561int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700562 unsigned order, struct radix_tree_node **nodep,
563 void ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700565 struct radix_tree_node *node = NULL, *child;
566 void **slot = (void **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700567 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700568 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700569 unsigned long max = index | ((1UL << order) - 1);
570
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700571 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 /* Make sure the tree is high enough. */
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700574 if (max > maxindex) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700575 int error = radix_tree_extend(root, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700576 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700578 shift = error;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700579 child = root->rnode;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700580 if (order == shift)
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700581 shift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700584 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700585 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700586 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Have to add a child node. */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700588 child = radix_tree_node_alloc(root);
589 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700591 child->shift = shift;
592 child->offset = offset;
593 child->parent = node;
594 rcu_assign_pointer(*slot, node_to_entry(child));
595 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700597 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700598 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700601 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700602 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700603 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700604 }
605
Matthew Wilcox57578c22016-05-20 17:01:54 -0700606#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700607 /* Insert pointers to the canonical entry */
Matthew Wilcox3b8c00f2016-05-20 17:01:59 -0700608 if (order > shift) {
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700609 unsigned i, n = 1 << (order - shift);
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700610 offset = offset & ~(n - 1);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700611 slot = &node->slots[offset];
612 child = node_to_entry(slot);
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700613 for (i = 0; i < n; i++) {
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700614 if (slot[i])
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700615 return -EEXIST;
616 }
617
618 for (i = 1; i < n; i++) {
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700619 rcu_assign_pointer(slot[i], child);
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700620 node->count++;
621 }
Nick Piggin612d6c12006-06-23 02:03:22 -0700622 }
Matthew Wilcox57578c22016-05-20 17:01:54 -0700623#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Johannes Weiner139e5612014-04-03 14:47:54 -0700625 if (nodep)
626 *nodep = node;
627 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700628 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700629 return 0;
630}
631
632/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700633 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700634 * @root: radix tree root
635 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700636 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700637 * @item: item to insert
638 *
639 * Insert an item into the radix tree at position @index.
640 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700641int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
642 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700643{
644 struct radix_tree_node *node;
645 void **slot;
646 int error;
647
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700648 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700649
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700650 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700651 if (error)
652 return error;
653 if (*slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 return -EEXIST;
Johannes Weiner139e5612014-04-03 14:47:54 -0700655 rcu_assign_pointer(*slot, item);
Christoph Lameter201b6262005-09-06 15:16:46 -0700656
Nick Piggin612d6c12006-06-23 02:03:22 -0700657 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700658 unsigned offset = get_slot_offset(node, slot);
Nick Piggin612d6c12006-06-23 02:03:22 -0700659 node->count++;
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700660 BUG_ON(tag_get(node, 0, offset));
661 BUG_ON(tag_get(node, 1, offset));
662 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700663 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700664 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return 0;
668}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700669EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Johannes Weiner139e5612014-04-03 14:47:54 -0700671/**
672 * __radix_tree_lookup - lookup an item in a radix tree
673 * @root: radix tree root
674 * @index: index key
675 * @nodep: returns node
676 * @slotp: returns slot
677 *
678 * Lookup and return the item at position @index in the radix
679 * tree @root.
680 *
681 * Until there is more than one item in the tree, no nodes are
682 * allocated and @root->rnode is used as a direct slot instead of
683 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800684 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700685void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
686 struct radix_tree_node **nodep, void ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800687{
Johannes Weiner139e5612014-04-03 14:47:54 -0700688 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700689 unsigned long maxindex;
Johannes Weiner139e5612014-04-03 14:47:54 -0700690 void **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800691
Matthew Wilcox85829952016-05-20 17:02:20 -0700692 restart:
693 parent = NULL;
694 slot = (void **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700695 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700696 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800697 return NULL;
698
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700699 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700700 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700701
Matthew Wilcox85829952016-05-20 17:02:20 -0700702 if (node == RADIX_TREE_RETRY)
703 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700704 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700705 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700706 slot = parent->slots + offset;
707 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800708
Johannes Weiner139e5612014-04-03 14:47:54 -0700709 if (nodep)
710 *nodep = parent;
711 if (slotp)
712 *slotp = slot;
713 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700714}
715
716/**
717 * radix_tree_lookup_slot - lookup a slot in a radix tree
718 * @root: radix tree root
719 * @index: index key
720 *
721 * Returns: the slot corresponding to the position @index in the
722 * radix tree @root. This is useful for update-if-exists operations.
723 *
724 * This function can be called under rcu_read_lock iff the slot is not
725 * modified by radix_tree_replace_slot, otherwise it must be called
726 * exclusive from other writers. Any dereference of the slot must be done
727 * using radix_tree_deref_slot.
728 */
729void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
730{
Johannes Weiner139e5612014-04-03 14:47:54 -0700731 void **slot;
732
733 if (!__radix_tree_lookup(root, index, NULL, &slot))
734 return NULL;
735 return slot;
Hans Reisera4331362005-11-07 00:59:29 -0800736}
737EXPORT_SYMBOL(radix_tree_lookup_slot);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/**
740 * radix_tree_lookup - perform lookup operation on a radix tree
741 * @root: radix tree root
742 * @index: index key
743 *
744 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800745 *
746 * This function can be called under rcu_read_lock, however the caller
747 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
748 * them safely). No RCU barriers are required to access or modify the
749 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
751void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
752{
Johannes Weiner139e5612014-04-03 14:47:54 -0700753 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755EXPORT_SYMBOL(radix_tree_lookup);
756
757/**
758 * radix_tree_tag_set - set a tag on a radix tree node
759 * @root: radix tree root
760 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700761 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800763 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
764 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 * the root all the way down to the leaf node.
766 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700767 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 * item is a bug.
769 */
770void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800771 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Ross Zwislerfb969902016-05-20 17:02:32 -0700773 struct radix_tree_node *node, *parent;
774 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700776 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -0700777 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700779 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -0700780 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700782 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700783 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -0700784 BUG_ON(!node);
785
786 if (!tag_get(parent, tag, offset))
787 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Nick Piggin612d6c12006-06-23 02:03:22 -0700790 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -0700791 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -0700792 root_tag_set(root, tag);
793
Ross Zwislerfb969902016-05-20 17:02:32 -0700794 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796EXPORT_SYMBOL(radix_tree_tag_set);
797
Matthew Wilcoxd604c322016-05-20 17:03:45 -0700798static void node_tag_clear(struct radix_tree_root *root,
799 struct radix_tree_node *node,
800 unsigned int tag, unsigned int offset)
801{
802 while (node) {
803 if (!tag_get(node, tag, offset))
804 return;
805 tag_clear(node, tag, offset);
806 if (any_tag_set(node, tag))
807 return;
808
809 offset = node->offset;
810 node = node->parent;
811 }
812
813 /* clear the root's tag bit */
814 if (root_tag_get(root, tag))
815 root_tag_clear(root, tag);
816}
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/**
819 * radix_tree_tag_clear - clear a tag on a radix tree node
820 * @root: radix tree root
821 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700822 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800824 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700825 * corresponding to @index in the radix tree. If this causes
826 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * next-to-leaf node, etc.
828 *
829 * Returns the address of the tagged item on success, else NULL. ie:
830 * has the same return value and semantics as radix_tree_lookup().
831 */
832void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800833 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Ross Zwisler00f47b52016-05-20 17:02:35 -0700835 struct radix_tree_node *node, *parent;
836 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800837 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700839 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -0700840 if (index > maxindex)
841 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Ross Zwisler00f47b52016-05-20 17:02:35 -0700843 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700845 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700846 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700847 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849
Matthew Wilcoxd604c322016-05-20 17:03:45 -0700850 if (node)
851 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Ross Zwisler00f47b52016-05-20 17:02:35 -0700853 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855EXPORT_SYMBOL(radix_tree_tag_clear);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700858 * radix_tree_tag_get - get a tag on a radix tree node
859 * @root: radix tree root
860 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700861 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -0700863 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
Nick Piggin612d6c12006-06-23 02:03:22 -0700865 * 0: tag not present or not set
866 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +0100867 *
868 * Note that the return value of this function may not be relied on, even if
869 * the RCU lock is held, unless tag modification and node deletion are excluded
870 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800873 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Ross Zwisler4589ba62016-05-20 17:02:38 -0700875 struct radix_tree_node *node, *parent;
876 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Nick Piggin612d6c12006-06-23 02:03:22 -0700878 if (!root_tag_get(root, tag))
879 return 0;
880
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700881 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -0700882 if (index > maxindex)
883 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800884 if (node == NULL)
885 return 0;
886
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700887 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700888 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -0700889
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700890 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700891 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -0700892
893 if (!node)
894 return 0;
895 if (!tag_get(parent, tag, offset))
896 return 0;
897 if (node == RADIX_TREE_RETRY)
898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Ross Zwisler4589ba62016-05-20 17:02:38 -0700900
901 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Ross Zwisler21ef5332016-05-20 17:02:26 -0700905static inline void __set_iter_shift(struct radix_tree_iter *iter,
906 unsigned int shift)
907{
908#ifdef CONFIG_RADIX_TREE_MULTIORDER
909 iter->shift = shift;
910#endif
911}
912
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700913/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700914 * radix_tree_next_chunk - find next chunk of slots for iteration
915 *
916 * @root: radix tree root
917 * @iter: iterator state
918 * @flags: RADIX_TREE_ITER_* flags and tag index
919 * Returns: pointer to chunk first slot, or NULL if iteration is over
920 */
921void **radix_tree_next_chunk(struct radix_tree_root *root,
922 struct radix_tree_iter *iter, unsigned flags)
923{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700924 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700925 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -0700926 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700927
928 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
929 return NULL;
930
931 /*
932 * Catch next_index overflow after ~0UL. iter->index never overflows
933 * during iterating; it can be zero only at the beginning.
934 * And we cannot overflow iter->next_index in a single step,
935 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +0400936 *
937 * This condition also used by radix_tree_next_slot() to stop
938 * contiguous iterating, and forbid swithing to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700939 */
940 index = iter->next_index;
941 if (!index && iter->index)
942 return NULL;
943
Ross Zwisler21ef5332016-05-20 17:02:26 -0700944 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700945 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -0700946 if (index > maxindex)
947 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700948 if (!child)
949 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -0700950
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700951 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700952 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -0700953 iter->index = index;
954 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700955 iter->tags = 1;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700956 __set_iter_shift(iter, 0);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700957 return (void **)&root->rnode;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700958 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700959
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700960 do {
961 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700962 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -0700963
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700964 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700965 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700966 /* Hole detected */
967 if (flags & RADIX_TREE_ITER_CONTIG)
968 return NULL;
969
970 if (flags & RADIX_TREE_ITER_TAGGED)
971 offset = radix_tree_find_next_bit(
972 node->tags[tag],
973 RADIX_TREE_MAP_SIZE,
974 offset + 1);
975 else
976 while (++offset < RADIX_TREE_MAP_SIZE) {
Ross Zwisler21ef5332016-05-20 17:02:26 -0700977 void *slot = node->slots[offset];
978 if (is_sibling_entry(node, slot))
979 continue;
980 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700981 break;
982 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700983 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700984 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700985 /* Overflow after ~0UL */
986 if (!index)
987 return NULL;
988 if (offset == RADIX_TREE_MAP_SIZE)
989 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700990 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700991 }
992
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700993 if ((child == NULL) || (child == RADIX_TREE_RETRY))
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700994 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700995 } while (radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700996
997 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -0700998 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
999 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001000 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001001
1002 /* Construct iter->tags bit-mask from node->tags[tag] array */
1003 if (flags & RADIX_TREE_ITER_TAGGED) {
1004 unsigned tag_long, tag_bit;
1005
1006 tag_long = offset / BITS_PER_LONG;
1007 tag_bit = offset % BITS_PER_LONG;
1008 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1009 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1010 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1011 /* Pick tags from next element */
1012 if (tag_bit)
1013 iter->tags |= node->tags[tag][tag_long + 1] <<
1014 (BITS_PER_LONG - tag_bit);
1015 /* Clip chunk size, here only BITS_PER_LONG tags */
1016 iter->next_index = index + BITS_PER_LONG;
1017 }
1018 }
1019
1020 return node->slots + offset;
1021}
1022EXPORT_SYMBOL(radix_tree_next_chunk);
1023
1024/**
Jan Karaebf8aa42010-08-09 17:19:11 -07001025 * radix_tree_range_tag_if_tagged - for each item in given range set given
1026 * tag if item has another tag set
1027 * @root: radix tree root
1028 * @first_indexp: pointer to a starting index of a range to scan
1029 * @last_index: last index of a range to scan
1030 * @nr_to_tag: maximum number items to tag
1031 * @iftag: tag index to test
1032 * @settag: tag index to set if tested tag is set
1033 *
1034 * This function scans range of radix tree from first_index to last_index
1035 * (inclusive). For each item in the range if iftag is set, the function sets
1036 * also settag. The function stops either after tagging nr_to_tag items or
1037 * after reaching last_index.
1038 *
Dave Chinner144dcfc2010-08-23 10:33:53 +10001039 * The tags must be set from the leaf level only and propagated back up the
1040 * path to the root. We must do this so that we resolve the full path before
1041 * setting any tags on intermediate nodes. If we set tags as we descend, then
1042 * we can get to the leaf node and find that the index that has the iftag
1043 * set is outside the range we are scanning. This reults in dangling tags and
1044 * can lead to problems with later tag operations (e.g. livelocks on lookups).
1045 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001046 * The function returns the number of leaves where the tag was set and sets
Jan Karaebf8aa42010-08-09 17:19:11 -07001047 * *first_indexp to the first unscanned index.
Jan Karad5ed3a42010-08-19 14:13:33 -07001048 * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
1049 * be prepared to handle that.
Jan Karaebf8aa42010-08-09 17:19:11 -07001050 */
1051unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
1052 unsigned long *first_indexp, unsigned long last_index,
1053 unsigned long nr_to_tag,
1054 unsigned int iftag, unsigned int settag)
1055{
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001056 struct radix_tree_node *parent, *node, *child;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001057 unsigned long maxindex;
Dave Chinner144dcfc2010-08-23 10:33:53 +10001058 unsigned long tagged = 0;
1059 unsigned long index = *first_indexp;
Jan Karaebf8aa42010-08-09 17:19:11 -07001060
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001061 radix_tree_load_root(root, &child, &maxindex);
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001062 last_index = min(last_index, maxindex);
Jan Karaebf8aa42010-08-09 17:19:11 -07001063 if (index > last_index)
1064 return 0;
1065 if (!nr_to_tag)
1066 return 0;
1067 if (!root_tag_get(root, iftag)) {
1068 *first_indexp = last_index + 1;
1069 return 0;
1070 }
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001071 if (!radix_tree_is_internal_node(child)) {
Jan Karaebf8aa42010-08-09 17:19:11 -07001072 *first_indexp = last_index + 1;
1073 root_tag_set(root, settag);
1074 return 1;
1075 }
1076
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001077 node = entry_to_node(child);
Jan Karaebf8aa42010-08-09 17:19:11 -07001078
1079 for (;;) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001080 unsigned offset = radix_tree_descend(node, &child, index);
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001081 if (!child)
Jan Karaebf8aa42010-08-09 17:19:11 -07001082 goto next;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001083 if (!tag_get(node, iftag, offset))
Jan Karaebf8aa42010-08-09 17:19:11 -07001084 goto next;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001085 /* Sibling slots never have tags set on them */
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001086 if (radix_tree_is_internal_node(child)) {
1087 node = entry_to_node(child);
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001088 continue;
Jan Karaebf8aa42010-08-09 17:19:11 -07001089 }
Dave Chinner144dcfc2010-08-23 10:33:53 +10001090
1091 /* tag the leaf */
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001092 tagged++;
1093 tag_set(node, settag, offset);
Dave Chinner144dcfc2010-08-23 10:33:53 +10001094
1095 /* walk back up the path tagging interior nodes */
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001096 parent = node;
1097 for (;;) {
1098 offset = parent->offset;
1099 parent = parent->parent;
1100 if (!parent)
Dave Chinner144dcfc2010-08-23 10:33:53 +10001101 break;
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001102 /* stop if we find a node with the tag already set */
1103 if (tag_get(parent, settag, offset))
1104 break;
1105 tag_set(parent, settag, offset);
Dave Chinner144dcfc2010-08-23 10:33:53 +10001106 }
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001107 next:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001108 /* Go to next entry in node */
1109 index = ((index >> node->shift) + 1) << node->shift;
Jan Karad5ed3a42010-08-19 14:13:33 -07001110 /* Overflow can happen when last_index is ~0UL... */
1111 if (index > last_index || !index)
Jan Karaebf8aa42010-08-09 17:19:11 -07001112 break;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001113 offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001114 while (offset == 0) {
Jan Karaebf8aa42010-08-09 17:19:11 -07001115 /*
1116 * We've fully scanned this node. Go up. Because
1117 * last_index is guaranteed to be in the tree, what
1118 * we do below cannot wander astray.
1119 */
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001120 node = node->parent;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001121 offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
Jan Karaebf8aa42010-08-09 17:19:11 -07001122 }
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001123 if (is_sibling_entry(node, node->slots[offset]))
1124 goto next;
1125 if (tagged >= nr_to_tag)
1126 break;
Jan Karaebf8aa42010-08-09 17:19:11 -07001127 }
1128 /*
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -08001129 * We need not to tag the root tag if there is no tag which is set with
1130 * settag within the range from *first_indexp to last_index.
Jan Karaebf8aa42010-08-09 17:19:11 -07001131 */
Toshiyuki Okajimaac15ee62011-01-25 15:07:32 -08001132 if (tagged > 0)
1133 root_tag_set(root, settag);
Jan Karaebf8aa42010-08-09 17:19:11 -07001134 *first_indexp = index;
1135
1136 return tagged;
1137}
1138EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140/**
1141 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1142 * @root: radix tree root
1143 * @results: where the results of the lookup are placed
1144 * @first_index: start the lookup from this key
1145 * @max_items: place up to this many items at *results
1146 *
1147 * Performs an index-ascending scan of the tree for present items. Places
1148 * them at *@results and returns the number of items which were placed at
1149 * *@results.
1150 *
1151 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001152 *
1153 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1154 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001155 * an atomic snapshot of the tree at a single point in time, the
1156 * semantics of an RCU protected gang lookup are as though multiple
1157 * radix_tree_lookups have been issued in individual locks, and results
1158 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 */
1160unsigned int
1161radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1162 unsigned long first_index, unsigned int max_items)
1163{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001164 struct radix_tree_iter iter;
1165 void **slot;
1166 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001168 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001169 return 0;
1170
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001171 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001172 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001173 if (!results[ret])
1174 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001175 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001176 slot = radix_tree_iter_retry(&iter);
1177 continue;
1178 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001179 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return ret;
1184}
1185EXPORT_SYMBOL(radix_tree_gang_lookup);
1186
Nick Piggin47feff22008-07-25 19:45:29 -07001187/**
1188 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1189 * @root: radix tree root
1190 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001191 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001192 * @first_index: start the lookup from this key
1193 * @max_items: place up to this many items at *results
1194 *
1195 * Performs an index-ascending scan of the tree for present items. Places
1196 * their slots at *@results and returns the number of items which were
1197 * placed at *@results.
1198 *
1199 * The implementation is naive.
1200 *
1201 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1202 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1203 * protection, radix_tree_deref_slot may fail requiring a retry.
1204 */
1205unsigned int
Hugh Dickins63286502011-08-03 16:21:18 -07001206radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1207 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001208 unsigned long first_index, unsigned int max_items)
1209{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001210 struct radix_tree_iter iter;
1211 void **slot;
1212 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001213
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001214 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001215 return 0;
1216
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001217 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1218 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001219 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001220 indices[ret] = iter.index;
1221 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001222 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001223 }
1224
1225 return ret;
1226}
1227EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229/**
1230 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1231 * based on a tag
1232 * @root: radix tree root
1233 * @results: where the results of the lookup are placed
1234 * @first_index: start the lookup from this key
1235 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001236 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 *
1238 * Performs an index-ascending scan of the tree for present items which
1239 * have the tag indexed by @tag set. Places the items at *@results and
1240 * returns the number of items which were placed at *@results.
1241 */
1242unsigned int
1243radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001244 unsigned long first_index, unsigned int max_items,
1245 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001247 struct radix_tree_iter iter;
1248 void **slot;
1249 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001251 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001252 return 0;
1253
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001254 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001255 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001256 if (!results[ret])
1257 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001258 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001259 slot = radix_tree_iter_retry(&iter);
1260 continue;
1261 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001262 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return ret;
1267}
1268EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1269
1270/**
Nick Piggin47feff22008-07-25 19:45:29 -07001271 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1272 * radix tree based on a tag
1273 * @root: radix tree root
1274 * @results: where the results of the lookup are placed
1275 * @first_index: start the lookup from this key
1276 * @max_items: place up to this many items at *results
1277 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1278 *
1279 * Performs an index-ascending scan of the tree for present items which
1280 * have the tag indexed by @tag set. Places the slots at *@results and
1281 * returns the number of slots which were placed at *@results.
1282 */
1283unsigned int
1284radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1285 unsigned long first_index, unsigned int max_items,
1286 unsigned int tag)
1287{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001288 struct radix_tree_iter iter;
1289 void **slot;
1290 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001291
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001292 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001293 return 0;
1294
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001295 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1296 results[ret] = slot;
1297 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001298 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001299 }
1300
1301 return ret;
1302}
1303EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1304
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001305#if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
1306#include <linux/sched.h> /* for cond_resched() */
1307
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001308struct locate_info {
1309 unsigned long found_index;
1310 bool stop;
1311};
1312
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001313/*
1314 * This linear search is at present only useful to shmem_unuse_inode().
1315 */
1316static unsigned long __locate(struct radix_tree_node *slot, void *item,
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001317 unsigned long index, struct locate_info *info)
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001318{
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001319 unsigned long i;
1320
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001321 do {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001322 unsigned int shift = slot->shift;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001323
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001324 for (i = (index >> shift) & RADIX_TREE_MAP_MASK;
1325 i < RADIX_TREE_MAP_SIZE;
1326 i++, index += (1UL << shift)) {
1327 struct radix_tree_node *node =
1328 rcu_dereference_raw(slot->slots[i]);
1329 if (node == RADIX_TREE_RETRY)
1330 goto out;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001331 if (!radix_tree_is_internal_node(node)) {
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001332 if (node == item) {
1333 info->found_index = index;
1334 info->stop = true;
1335 goto out;
1336 }
1337 continue;
1338 }
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001339 node = entry_to_node(node);
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001340 if (is_sibling_entry(slot, node))
1341 continue;
1342 slot = node;
1343 break;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001344 }
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001345 } while (i < RADIX_TREE_MAP_SIZE);
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001346
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001347out:
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001348 if ((index == 0) && (i == RADIX_TREE_MAP_SIZE))
1349 info->stop = true;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001350 return index;
1351}
1352
1353/**
1354 * radix_tree_locate_item - search through radix tree for item
1355 * @root: radix tree root
1356 * @item: item to be found
1357 *
1358 * Returns index where item was found, or -1 if not found.
1359 * Caller must hold no lock (since this time-consuming function needs
1360 * to be preemptible), and must check afterwards if item is still there.
1361 */
1362unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1363{
1364 struct radix_tree_node *node;
1365 unsigned long max_index;
1366 unsigned long cur_index = 0;
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001367 struct locate_info info = {
1368 .found_index = -1,
1369 .stop = false,
1370 };
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001371
1372 do {
1373 rcu_read_lock();
1374 node = rcu_dereference_raw(root->rnode);
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001375 if (!radix_tree_is_internal_node(node)) {
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001376 rcu_read_unlock();
1377 if (node == item)
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001378 info.found_index = 0;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001379 break;
1380 }
1381
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001382 node = entry_to_node(node);
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001383
1384 max_index = node_maxindex(node);
Hugh Dickins5f30fc92014-03-03 15:38:23 -08001385 if (cur_index > max_index) {
1386 rcu_read_unlock();
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001387 break;
Hugh Dickins5f30fc92014-03-03 15:38:23 -08001388 }
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001389
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001390 cur_index = __locate(node, item, cur_index, &info);
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001391 rcu_read_unlock();
1392 cond_resched();
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001393 } while (!info.stop && cur_index <= max_index);
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001394
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001395 return info.found_index;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001396}
1397#else
1398unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1399{
1400 return -1;
1401}
1402#endif /* CONFIG_SHMEM && CONFIG_SWAP */
Nick Piggin47feff22008-07-25 19:45:29 -07001403
1404/**
Matthew Wilcoxd0891262016-05-20 17:03:19 -07001405 * radix_tree_shrink - shrink radix tree to minimum height
Nick Piggina5f51c92006-01-08 01:01:41 -08001406 * @root radix tree root
1407 */
Matthew Wilcoxfb209012016-05-20 17:03:13 -07001408static inline bool radix_tree_shrink(struct radix_tree_root *root)
Nick Piggina5f51c92006-01-08 01:01:41 -08001409{
Matthew Wilcoxfb209012016-05-20 17:03:13 -07001410 bool shrunk = false;
1411
Matthew Wilcoxd0891262016-05-20 17:03:19 -07001412 for (;;) {
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001413 struct radix_tree_node *node = root->rnode;
1414 struct radix_tree_node *child;
Nick Piggina5f51c92006-01-08 01:01:41 -08001415
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001416 if (!radix_tree_is_internal_node(node))
Matthew Wilcoxd0891262016-05-20 17:03:19 -07001417 break;
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001418 node = entry_to_node(node);
Nick Pigginc0bc9872007-10-16 01:24:42 -07001419
1420 /*
1421 * The candidate node has more than one child, or its child
Matthew Wilcoxd0891262016-05-20 17:03:19 -07001422 * is not at the leftmost slot, or the child is a multiorder
1423 * entry, we cannot shrink.
Nick Pigginc0bc9872007-10-16 01:24:42 -07001424 */
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001425 if (node->count != 1)
Nick Pigginc0bc9872007-10-16 01:24:42 -07001426 break;
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001427 child = node->slots[0];
1428 if (!child)
Nick Pigginc0bc9872007-10-16 01:24:42 -07001429 break;
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001430 if (!radix_tree_is_internal_node(child) && node->shift)
Matthew Wilcoxafe0e392016-05-20 17:02:17 -07001431 break;
1432
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001433 if (radix_tree_is_internal_node(child))
1434 entry_to_node(child)->parent = NULL;
Nick Pigginc0bc9872007-10-16 01:24:42 -07001435
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001436 /*
1437 * We don't need rcu_assign_pointer(), since we are simply
Nick Piggin27d20fd2010-11-11 14:05:19 -08001438 * moving the node from one part of the tree to another: if it
1439 * was safe to dereference the old pointer to it
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001440 * (node->slots[0]), it will be safe to dereference the new
Nick Piggin27d20fd2010-11-11 14:05:19 -08001441 * one (root->rnode) as far as dependent read barriers go.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001442 */
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001443 root->rnode = child;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001444
1445 /*
1446 * We have a dilemma here. The node's slot[0] must not be
1447 * NULLed in case there are concurrent lookups expecting to
1448 * find the item. However if this was a bottom-level node,
1449 * then it may be subject to the slot pointer being visible
1450 * to callers dereferencing it. If item corresponding to
1451 * slot[0] is subsequently deleted, these callers would expect
1452 * their slot to become empty sooner or later.
1453 *
1454 * For example, lockless pagecache will look up a slot, deref
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001455 * the page pointer, and if the page has 0 refcount it means it
Nick Piggin27d20fd2010-11-11 14:05:19 -08001456 * was concurrently deleted from pagecache so try the deref
1457 * again. Fortunately there is already a requirement for logic
1458 * to retry the entire slot lookup -- the indirect pointer
1459 * problem (replacing direct root node with an indirect pointer
1460 * also results in a stale slot). So tag the slot as indirect
1461 * to force callers to retry.
1462 */
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001463 if (!radix_tree_is_internal_node(child))
1464 node->slots[0] = RADIX_TREE_RETRY;
Nick Piggin27d20fd2010-11-11 14:05:19 -08001465
Matthew Wilcoxaf49a632016-05-20 17:03:33 -07001466 radix_tree_node_free(node);
Matthew Wilcoxfb209012016-05-20 17:03:13 -07001467 shrunk = true;
Nick Piggina5f51c92006-01-08 01:01:41 -08001468 }
Matthew Wilcoxfb209012016-05-20 17:03:13 -07001469
1470 return shrunk;
Nick Piggina5f51c92006-01-08 01:01:41 -08001471}
1472
1473/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001474 * __radix_tree_delete_node - try to free node after clearing a slot
1475 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001476 * @node: node containing @index
1477 *
1478 * After clearing the slot at @index in @node from radix tree
1479 * rooted at @root, call this function to attempt freeing the
1480 * node and shrinking the tree.
1481 *
1482 * Returns %true if @node was freed, %false otherwise.
1483 */
Johannes Weiner449dd692014-04-03 14:47:56 -07001484bool __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weiner139e5612014-04-03 14:47:54 -07001485 struct radix_tree_node *node)
1486{
1487 bool deleted = false;
1488
1489 do {
1490 struct radix_tree_node *parent;
1491
1492 if (node->count) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001493 if (node == entry_to_node(root->rnode))
Matthew Wilcoxfb209012016-05-20 17:03:13 -07001494 deleted |= radix_tree_shrink(root);
Johannes Weiner139e5612014-04-03 14:47:54 -07001495 return deleted;
1496 }
1497
1498 parent = node->parent;
1499 if (parent) {
Matthew Wilcox0c7fa0a2016-05-20 17:03:07 -07001500 parent->slots[node->offset] = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001501 parent->count--;
1502 } else {
1503 root_tag_clear_all(root);
Johannes Weiner139e5612014-04-03 14:47:54 -07001504 root->rnode = NULL;
1505 }
1506
1507 radix_tree_node_free(node);
1508 deleted = true;
1509
1510 node = parent;
1511 } while (node);
1512
1513 return deleted;
1514}
1515
Matthew Wilcox57578c22016-05-20 17:01:54 -07001516static inline void delete_sibling_entries(struct radix_tree_node *node,
1517 void *ptr, unsigned offset)
1518{
1519#ifdef CONFIG_RADIX_TREE_MULTIORDER
1520 int i;
1521 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1522 if (node->slots[offset + i] != ptr)
1523 break;
1524 node->slots[offset + i] = NULL;
1525 node->count--;
1526 }
1527#endif
1528}
1529
Johannes Weiner139e5612014-04-03 14:47:54 -07001530/**
Johannes Weiner53c59f22014-04-03 14:47:39 -07001531 * radix_tree_delete_item - delete an item from a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * @root: radix tree root
1533 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001534 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001536 * Remove @item at @index from the radix tree rooted at @root.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001538 * Returns the address of the deleted item, or NULL if it was not present
1539 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001541void *radix_tree_delete_item(struct radix_tree_root *root,
1542 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
Johannes Weiner139e5612014-04-03 14:47:54 -07001544 struct radix_tree_node *node;
Matthew Wilcox57578c22016-05-20 17:01:54 -07001545 unsigned int offset;
Johannes Weiner139e5612014-04-03 14:47:54 -07001546 void **slot;
1547 void *entry;
Nick Piggind5274262006-01-08 01:01:41 -08001548 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Johannes Weiner139e5612014-04-03 14:47:54 -07001550 entry = __radix_tree_lookup(root, index, &node, &slot);
1551 if (!entry)
1552 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Johannes Weiner139e5612014-04-03 14:47:54 -07001554 if (item && entry != item)
1555 return NULL;
1556
1557 if (!node) {
Nick Piggin612d6c12006-06-23 02:03:22 -07001558 root_tag_clear_all(root);
1559 root->rnode = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001560 return entry;
Nick Piggin612d6c12006-06-23 02:03:22 -07001561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Matthew Wilcox29e09672016-05-20 17:02:02 -07001563 offset = get_slot_offset(node, slot);
Johannes Weiner53c59f22014-04-03 14:47:39 -07001564
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001565 /* Clear all tags associated with the item to be deleted. */
1566 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1567 node_tag_clear(root, node, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -07001569 delete_sibling_entries(node, node_to_entry(slot), offset);
Johannes Weiner139e5612014-04-03 14:47:54 -07001570 node->slots[offset] = NULL;
1571 node->count--;
Nick Piggina5f51c92006-01-08 01:01:41 -08001572
Johannes Weiner449dd692014-04-03 14:47:56 -07001573 __radix_tree_delete_node(root, node);
Christoph Lameter201b6262005-09-06 15:16:46 -07001574
Johannes Weiner139e5612014-04-03 14:47:54 -07001575 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001577EXPORT_SYMBOL(radix_tree_delete_item);
1578
1579/**
1580 * radix_tree_delete - delete an item from a radix tree
1581 * @root: radix tree root
1582 * @index: index key
1583 *
1584 * Remove the item at @index from the radix tree rooted at @root.
1585 *
1586 * Returns the address of the deleted item, or NULL if it was not present.
1587 */
1588void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1589{
1590 return radix_tree_delete_item(root, index, NULL);
1591}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592EXPORT_SYMBOL(radix_tree_delete);
1593
Johannes Weinerd3798ae2016-10-04 22:02:08 +02001594void radix_tree_clear_tags(struct radix_tree_root *root,
1595 struct radix_tree_node *node,
1596 void **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001597{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001598 if (node) {
1599 unsigned int tag, offset = get_slot_offset(node, slot);
1600 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1601 node_tag_clear(root, node, tag, offset);
1602 } else {
1603 /* Clear root node tags */
1604 root->gfp_mask &= __GFP_BITS_MASK;
1605 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001606}
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608/**
1609 * radix_tree_tagged - test whether any items in the tree are tagged
1610 * @root: radix tree root
1611 * @tag: tag to test
1612 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001613int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Nick Piggin612d6c12006-06-23 02:03:22 -07001615 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617EXPORT_SYMBOL(radix_tree_tagged);
1618
1619static void
Johannes Weiner449dd692014-04-03 14:47:56 -07001620radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Johannes Weiner449dd692014-04-03 14:47:56 -07001622 struct radix_tree_node *node = arg;
1623
1624 memset(node, 0, sizeof(*node));
1625 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001628static __init unsigned long __maxindex(unsigned int height)
1629{
1630 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1631 int shift = RADIX_TREE_INDEX_BITS - width;
1632
1633 if (shift < 0)
1634 return ~0UL;
1635 if (shift >= BITS_PER_LONG)
1636 return 0UL;
1637 return ~0UL >> shift;
1638}
1639
1640static __init void radix_tree_init_maxnodes(void)
1641{
1642 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1643 unsigned int i, j;
1644
1645 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1646 height_to_maxindex[i] = __maxindex(i);
1647 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1648 for (j = i; j > 0; j--)
1649 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1650 }
1651}
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653static int radix_tree_callback(struct notifier_block *nfb,
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001654 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001656 int cpu = (long)hcpu;
1657 struct radix_tree_preload *rtp;
1658 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001660 /* Free per-cpu pool of preloaded nodes */
1661 if (action == CPU_DEAD || action == CPU_DEAD_FROZEN) {
1662 rtp = &per_cpu(radix_tree_preloads, cpu);
1663 while (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -07001664 node = rtp->nodes;
1665 rtp->nodes = node->private_data;
1666 kmem_cache_free(radix_tree_node_cachep, node);
1667 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001668 }
1669 }
1670 return NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673void __init radix_tree_init(void)
1674{
1675 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1676 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07001677 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1678 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001679 radix_tree_init_maxnodes();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 hotcpu_notifier(radix_tree_callback, 0);
1681}