blob: 0a7ac0fb4a65570dd19b2bdccd2ac4855d1e3cc1 [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 Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/notifier.h>
34#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() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
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, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Matthew Wilcox148deab2016-12-14 15:08:49 -080072static inline struct radix_tree_node *entry_to_node(void *ptr)
73{
74 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
75}
76
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070077static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080078{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070079 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -080080}
81
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070082#define RADIX_TREE_RETRY node_to_entry(NULL)
Matthew Wilcoxafe0e392016-05-20 17:02:17 -070083
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070084#ifdef CONFIG_RADIX_TREE_MULTIORDER
85/* Sibling slots point directly to another slot in the same node */
86static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
87{
88 void **ptr = node;
89 return (parent->slots <= ptr) &&
90 (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
91}
92#else
93static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
94{
95 return false;
96}
97#endif
98
99static inline unsigned long get_slot_offset(struct radix_tree_node *parent,
100 void **slot)
101{
102 return slot - parent->slots;
103}
104
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700105static unsigned int radix_tree_descend(struct radix_tree_node *parent,
106 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700107{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700108 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700109 void **entry = rcu_dereference_raw(parent->slots[offset]);
110
111#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700112 if (radix_tree_is_internal_node(entry)) {
Linus Torvalds8d2c0d32016-09-25 13:32:46 -0700113 if (is_sibling_entry(parent, entry)) {
114 void **sibentry = (void **) entry_to_node(entry);
115 offset = get_slot_offset(parent, sibentry);
116 entry = rcu_dereference_raw(*sibentry);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700117 }
118 }
119#endif
120
121 *nodep = (void *)entry;
122 return offset;
123}
124
Nick Piggin612d6c12006-06-23 02:03:22 -0700125static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
126{
127 return root->gfp_mask & __GFP_BITS_MASK;
128}
129
Nick Piggin643b52b2008-06-12 15:21:52 -0700130static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
131 int offset)
132{
133 __set_bit(offset, node->tags[tag]);
134}
135
136static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
137 int offset)
138{
139 __clear_bit(offset, node->tags[tag]);
140}
141
142static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
143 int offset)
144{
145 return test_bit(offset, node->tags[tag]);
146}
147
148static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
149{
150 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
151}
152
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700153static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700154{
155 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
156}
157
158static inline void root_tag_clear_all(struct radix_tree_root *root)
159{
160 root->gfp_mask &= __GFP_BITS_MASK;
161}
162
163static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
164{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700165 return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700166}
167
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700168static inline unsigned root_tags_get(struct radix_tree_root *root)
169{
170 return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT;
171}
172
Nick Piggin643b52b2008-06-12 15:21:52 -0700173/*
174 * Returns 1 if any slot in the node has this tag set.
175 * Otherwise returns 0.
176 */
177static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
178{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700179 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700180 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
181 if (node->tags[tag][idx])
182 return 1;
183 }
184 return 0;
185}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700186
187/**
188 * radix_tree_find_next_bit - find the next set bit in a memory region
189 *
190 * @addr: The address to base the search on
191 * @size: The bitmap size in bits
192 * @offset: The bitnumber to start searching at
193 *
194 * Unrollable variant of find_next_bit() for constant size arrays.
195 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
196 * Returns next bit offset, or size if nothing found.
197 */
198static __always_inline unsigned long
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800199radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
200 unsigned long offset)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700201{
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800202 const unsigned long *addr = node->tags[tag];
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700203
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800204 if (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700205 unsigned long tmp;
206
207 addr += offset / BITS_PER_LONG;
208 tmp = *addr >> (offset % BITS_PER_LONG);
209 if (tmp)
210 return __ffs(tmp) + offset;
211 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800212 while (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700213 tmp = *++addr;
214 if (tmp)
215 return __ffs(tmp) + offset;
216 offset += BITS_PER_LONG;
217 }
218 }
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800219 return RADIX_TREE_MAP_SIZE;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700220}
221
Matthew Wilcox218ed752016-12-14 15:08:43 -0800222/*
223 * The maximum index which can be stored in a radix tree
224 */
225static inline unsigned long shift_maxindex(unsigned int shift)
226{
227 return (RADIX_TREE_MAP_SIZE << shift) - 1;
228}
229
230static inline unsigned long node_maxindex(struct radix_tree_node *node)
231{
232 return shift_maxindex(node->shift);
233}
234
Ross Zwisler0796c582016-05-20 17:02:55 -0700235#ifndef __KERNEL__
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700236static void dump_node(struct radix_tree_node *node, unsigned long index)
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700237{
Ross Zwisler0796c582016-05-20 17:02:55 -0700238 unsigned long i;
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700239
Matthew Wilcox218ed752016-12-14 15:08:43 -0800240 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
241 node, node->offset, index, index | node_maxindex(node),
242 node->parent,
Ross Zwisler0796c582016-05-20 17:02:55 -0700243 node->tags[0][0], node->tags[1][0], node->tags[2][0],
Matthew Wilcox218ed752016-12-14 15:08:43 -0800244 node->shift, node->count, node->exceptional);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700245
Ross Zwisler0796c582016-05-20 17:02:55 -0700246 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700247 unsigned long first = index | (i << node->shift);
248 unsigned long last = first | ((1UL << node->shift) - 1);
Ross Zwisler0796c582016-05-20 17:02:55 -0700249 void *entry = node->slots[i];
250 if (!entry)
251 continue;
Matthew Wilcox218ed752016-12-14 15:08:43 -0800252 if (entry == RADIX_TREE_RETRY) {
253 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
254 i, first, last, node);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700255 } else if (!radix_tree_is_internal_node(entry)) {
Matthew Wilcox218ed752016-12-14 15:08:43 -0800256 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
257 entry, i, first, last, node);
258 } else if (is_sibling_entry(node, entry)) {
259 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
260 entry, i, first, last, node,
261 *(void **)entry_to_node(entry));
Ross Zwisler0796c582016-05-20 17:02:55 -0700262 } else {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700263 dump_node(entry_to_node(entry), first);
Ross Zwisler0796c582016-05-20 17:02:55 -0700264 }
265 }
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700266}
267
268/* For debug */
269static void radix_tree_dump(struct radix_tree_root *root)
270{
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700271 pr_debug("radix root: %p rnode %p tags %x\n",
272 root, root->rnode,
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700273 root->gfp_mask >> __GFP_BITS_SHIFT);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700274 if (!radix_tree_is_internal_node(root->rnode))
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700275 return;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700276 dump_node(entry_to_node(root->rnode), 0);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700277}
278#endif
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280/*
281 * This assumes that the caller has performed appropriate preallocation, and
282 * that the caller has pinned this thread of control to the current CPU.
283 */
284static struct radix_tree_node *
285radix_tree_node_alloc(struct radix_tree_root *root)
286{
Nick Piggine2848a02008-02-04 22:29:10 -0800287 struct radix_tree_node *ret = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700288 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Jan Kara5e4c0d972013-09-11 14:26:05 -0700290 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700291 * Preload code isn't irq safe and it doesn't make sense to use
292 * preloading during an interrupt anyway as all the allocations have
293 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700294 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800295 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct radix_tree_preload *rtp;
297
Nick Piggine2848a02008-02-04 22:29:10 -0800298 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700299 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700300 * cache first for the new node to get accounted to the memory
301 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700302 */
303 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700304 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700305 if (ret)
306 goto out;
307
308 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800309 * Provided the caller has preloaded here, we will always
310 * succeed in getting a node here (and never reach
311 * kmem_cache_alloc)
312 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700313 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700315 ret = rtp->nodes;
316 rtp->nodes = ret->private_data;
317 ret->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 rtp->nr--;
319 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700320 /*
321 * Update the allocation stack trace as this is more useful
322 * for debugging.
323 */
324 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700327 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700328out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700329 BUG_ON(radix_tree_is_internal_node(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return ret;
331}
332
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800333static void radix_tree_node_rcu_free(struct rcu_head *head)
334{
335 struct radix_tree_node *node =
336 container_of(head, struct radix_tree_node, rcu_head);
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000337 int i;
Nick Piggin643b52b2008-06-12 15:21:52 -0700338
339 /*
340 * must only free zeroed nodes into the slab. radix_tree_shrink
341 * can leave us with a non-NULL entry in the first slot, so clear
342 * that here to make sure.
343 */
Dave Chinnerb6dd0862010-08-23 10:33:19 +1000344 for (i = 0; i < RADIX_TREE_MAX_TAGS; i++)
345 tag_clear(node, i, 0);
346
Nick Piggin643b52b2008-06-12 15:21:52 -0700347 node->slots[0] = NULL;
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800348 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700349
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800350 kmem_cache_free(radix_tree_node_cachep, node);
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static inline void
354radix_tree_node_free(struct radix_tree_node *node)
355{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800356 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
359/*
360 * Load up this CPU's radix_tree_node buffer with sufficient objects to
361 * ensure that the addition of a single element in the tree cannot fail. On
362 * success, return zero, with preemption disabled. On error, return -ENOMEM
363 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000364 *
365 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800366 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 */
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700368static int __radix_tree_preload(gfp_t gfp_mask, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 struct radix_tree_preload *rtp;
371 struct radix_tree_node *node;
372 int ret = -ENOMEM;
373
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700374 /*
375 * Nodes preloaded by one cgroup can be be used by another cgroup, so
376 * they should never be accounted to any particular memory cgroup.
377 */
378 gfp_mask &= ~__GFP_ACCOUNT;
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700381 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700382 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700384 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (node == NULL)
386 goto out;
387 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700388 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700389 if (rtp->nr < nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700390 node->private_data = rtp->nodes;
391 rtp->nodes = node;
392 rtp->nr++;
393 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 ret = 0;
398out:
399 return ret;
400}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700401
402/*
403 * Load up this CPU's radix_tree_node buffer with sufficient objects to
404 * ensure that the addition of a single element in the tree cannot fail. On
405 * success, return zero, with preemption disabled. On error, return -ENOMEM
406 * with preemption not disabled.
407 *
408 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800409 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700410 */
411int radix_tree_preload(gfp_t gfp_mask)
412{
413 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800414 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700415 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700416}
David Chinnerd7f09232007-07-14 16:05:04 +1000417EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Nick Piggin6e954b92006-01-08 01:01:40 -0800419/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700420 * The same as above function, except we don't guarantee preloading happens.
421 * We do it, if we decide it helps. On success, return zero with preemption
422 * disabled. On error, return -ENOMEM with preemption not disabled.
423 */
424int radix_tree_maybe_preload(gfp_t gfp_mask)
425{
Mel Gormand0164ad2015-11-06 16:28:21 -0800426 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700427 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700428 /* Preloading doesn't help anything with this gfp mask, skip it */
429 preempt_disable();
430 return 0;
431}
432EXPORT_SYMBOL(radix_tree_maybe_preload);
433
434/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700435 * The same as function above, but preload number of nodes required to insert
436 * (1 << order) continuous naturally-aligned elements.
437 */
438int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
439{
440 unsigned long nr_subtrees;
441 int nr_nodes, subtree_height;
442
443 /* Preloading doesn't help anything with this gfp mask, skip it */
444 if (!gfpflags_allow_blocking(gfp_mask)) {
445 preempt_disable();
446 return 0;
447 }
448
449 /*
450 * Calculate number and height of fully populated subtrees it takes to
451 * store (1 << order) elements.
452 */
453 nr_subtrees = 1 << order;
454 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
455 subtree_height++)
456 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
457
458 /*
459 * The worst case is zero height tree with a single item at index 0 and
460 * then inserting items starting at ULONG_MAX - (1 << order).
461 *
462 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
463 * 0-index item.
464 */
465 nr_nodes = RADIX_TREE_MAX_PATH;
466
467 /* Plus branch to fully populated subtrees. */
468 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
469
470 /* Root node is shared. */
471 nr_nodes--;
472
473 /* Plus nodes required to build subtrees. */
474 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
475
476 return __radix_tree_preload(gfp_mask, nr_nodes);
477}
478
Matthew Wilcox1456a432016-05-20 17:02:08 -0700479static unsigned radix_tree_load_root(struct radix_tree_root *root,
480 struct radix_tree_node **nodep, unsigned long *maxindex)
481{
482 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
483
484 *nodep = node;
485
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700486 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700487 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700488 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700489 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700490 }
491
492 *maxindex = 0;
493 return 0;
494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
497 * Extend a radix tree so it can store key @index.
498 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700499static int radix_tree_extend(struct radix_tree_root *root,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700500 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800502 struct radix_tree_node *slot;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700503 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 int tag;
505
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700506 /* Figure out what the shift should be. */
507 maxshift = shift;
508 while (index > shift_maxindex(maxshift))
509 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700511 slot = root->rnode;
512 if (!slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 do {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700516 struct radix_tree_node *node = radix_tree_node_alloc(root);
517
518 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -ENOMEM;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800522 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700523 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 tag_set(node, tag, 0);
525 }
526
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700527 BUG_ON(shift > BITS_PER_LONG);
528 node->shift = shift;
Matthew Wilcox0c7fa0a2016-05-20 17:03:07 -0700529 node->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 node->count = 1;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800531 node->parent = NULL;
Johannes Weinerf7942432016-12-12 16:43:41 -0800532 if (radix_tree_is_internal_node(slot)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700533 entry_to_node(slot)->parent = node;
Johannes Weinerf7942432016-12-12 16:43:41 -0800534 } else {
535 /* Moving an exceptional root->rnode to a node */
536 if (radix_tree_exceptional_entry(slot))
537 node->exceptional = 1;
538 }
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800539 node->slots[0] = slot;
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -0700540 slot = node_to_entry(node);
541 rcu_assign_pointer(root->rnode, slot);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700542 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700543 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700545 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
548/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800549 * radix_tree_shrink - shrink radix tree to minimum height
550 * @root radix tree root
551 */
Johannes Weiner14b46872016-12-12 16:43:52 -0800552static inline void radix_tree_shrink(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800553 radix_tree_update_node_t update_node,
554 void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800555{
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800556 for (;;) {
557 struct radix_tree_node *node = root->rnode;
558 struct radix_tree_node *child;
559
560 if (!radix_tree_is_internal_node(node))
561 break;
562 node = entry_to_node(node);
563
564 /*
565 * The candidate node has more than one child, or its child
566 * is not at the leftmost slot, or the child is a multiorder
567 * entry, we cannot shrink.
568 */
569 if (node->count != 1)
570 break;
571 child = node->slots[0];
572 if (!child)
573 break;
574 if (!radix_tree_is_internal_node(child) && node->shift)
575 break;
576
577 if (radix_tree_is_internal_node(child))
578 entry_to_node(child)->parent = NULL;
579
580 /*
581 * We don't need rcu_assign_pointer(), since we are simply
582 * moving the node from one part of the tree to another: if it
583 * was safe to dereference the old pointer to it
584 * (node->slots[0]), it will be safe to dereference the new
585 * one (root->rnode) as far as dependent read barriers go.
586 */
587 root->rnode = child;
588
589 /*
590 * We have a dilemma here. The node's slot[0] must not be
591 * NULLed in case there are concurrent lookups expecting to
592 * find the item. However if this was a bottom-level node,
593 * then it may be subject to the slot pointer being visible
594 * to callers dereferencing it. If item corresponding to
595 * slot[0] is subsequently deleted, these callers would expect
596 * their slot to become empty sooner or later.
597 *
598 * For example, lockless pagecache will look up a slot, deref
599 * the page pointer, and if the page has 0 refcount it means it
600 * was concurrently deleted from pagecache so try the deref
601 * again. Fortunately there is already a requirement for logic
602 * to retry the entire slot lookup -- the indirect pointer
603 * problem (replacing direct root node with an indirect pointer
604 * also results in a stale slot). So tag the slot as indirect
605 * to force callers to retry.
606 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800607 node->count = 0;
608 if (!radix_tree_is_internal_node(child)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800609 node->slots[0] = RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800610 if (update_node)
611 update_node(node, private);
612 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800613
614 radix_tree_node_free(node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800615 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800616}
617
Johannes Weiner14b46872016-12-12 16:43:52 -0800618static void delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800619 struct radix_tree_node *node,
620 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800621{
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800622 do {
623 struct radix_tree_node *parent;
624
625 if (node->count) {
626 if (node == entry_to_node(root->rnode))
Johannes Weiner14b46872016-12-12 16:43:52 -0800627 radix_tree_shrink(root, update_node, private);
628 return;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800629 }
630
631 parent = node->parent;
632 if (parent) {
633 parent->slots[node->offset] = NULL;
634 parent->count--;
635 } else {
636 root_tag_clear_all(root);
637 root->rnode = NULL;
638 }
639
640 radix_tree_node_free(node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800641
642 node = parent;
643 } while (node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800644}
645
646/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700647 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * @root: radix tree root
649 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700650 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700651 * @nodep: returns node
652 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700654 * Create, if necessary, and return the node and slot for an item
655 * at position @index in the radix tree @root.
656 *
657 * Until there is more than one item in the tree, no nodes are
658 * allocated and @root->rnode is used as a direct slot instead of
659 * pointing to a node, in which case *@nodep will be NULL.
660 *
661 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700663int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700664 unsigned order, struct radix_tree_node **nodep,
665 void ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700667 struct radix_tree_node *node = NULL, *child;
668 void **slot = (void **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700669 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700670 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700671 unsigned long max = index | ((1UL << order) - 1);
672
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700673 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /* Make sure the tree is high enough. */
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700676 if (max > maxindex) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700677 int error = radix_tree_extend(root, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700678 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700680 shift = error;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700681 child = root->rnode;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700682 if (order == shift)
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700683 shift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700686 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700687 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700688 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Have to add a child node. */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700690 child = radix_tree_node_alloc(root);
691 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700693 child->shift = shift;
694 child->offset = offset;
695 child->parent = node;
696 rcu_assign_pointer(*slot, node_to_entry(child));
697 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700699 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700700 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700703 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700704 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700705 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700706 }
707
Matthew Wilcox57578c22016-05-20 17:01:54 -0700708#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700709 /* Insert pointers to the canonical entry */
Matthew Wilcox3b8c00f2016-05-20 17:01:59 -0700710 if (order > shift) {
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700711 unsigned i, n = 1 << (order - shift);
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700712 offset = offset & ~(n - 1);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700713 slot = &node->slots[offset];
714 child = node_to_entry(slot);
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700715 for (i = 0; i < n; i++) {
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700716 if (slot[i])
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700717 return -EEXIST;
718 }
719
720 for (i = 1; i < n; i++) {
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700721 rcu_assign_pointer(slot[i], child);
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700722 node->count++;
723 }
Nick Piggin612d6c12006-06-23 02:03:22 -0700724 }
Matthew Wilcox57578c22016-05-20 17:01:54 -0700725#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Johannes Weiner139e5612014-04-03 14:47:54 -0700727 if (nodep)
728 *nodep = node;
729 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700730 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700731 return 0;
732}
733
734/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700735 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700736 * @root: radix tree root
737 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700738 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700739 * @item: item to insert
740 *
741 * Insert an item into the radix tree at position @index.
742 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700743int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
744 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700745{
746 struct radix_tree_node *node;
747 void **slot;
748 int error;
749
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700750 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700751
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700752 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700753 if (error)
754 return error;
755 if (*slot != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -EEXIST;
Johannes Weiner139e5612014-04-03 14:47:54 -0700757 rcu_assign_pointer(*slot, item);
Christoph Lameter201b6262005-09-06 15:16:46 -0700758
Nick Piggin612d6c12006-06-23 02:03:22 -0700759 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700760 unsigned offset = get_slot_offset(node, slot);
Nick Piggin612d6c12006-06-23 02:03:22 -0700761 node->count++;
Johannes Weinerf7942432016-12-12 16:43:41 -0800762 if (radix_tree_exceptional_entry(item))
763 node->exceptional++;
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700764 BUG_ON(tag_get(node, 0, offset));
765 BUG_ON(tag_get(node, 1, offset));
766 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700767 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700768 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
772}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700773EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Johannes Weiner139e5612014-04-03 14:47:54 -0700775/**
776 * __radix_tree_lookup - lookup an item in a radix tree
777 * @root: radix tree root
778 * @index: index key
779 * @nodep: returns node
780 * @slotp: returns slot
781 *
782 * Lookup and return the item at position @index in the radix
783 * tree @root.
784 *
785 * Until there is more than one item in the tree, no nodes are
786 * allocated and @root->rnode is used as a direct slot instead of
787 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800788 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700789void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
790 struct radix_tree_node **nodep, void ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800791{
Johannes Weiner139e5612014-04-03 14:47:54 -0700792 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700793 unsigned long maxindex;
Johannes Weiner139e5612014-04-03 14:47:54 -0700794 void **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800795
Matthew Wilcox85829952016-05-20 17:02:20 -0700796 restart:
797 parent = NULL;
798 slot = (void **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700799 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700800 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800801 return NULL;
802
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700803 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700804 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700805
Matthew Wilcox85829952016-05-20 17:02:20 -0700806 if (node == RADIX_TREE_RETRY)
807 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700808 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700809 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700810 slot = parent->slots + offset;
811 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800812
Johannes Weiner139e5612014-04-03 14:47:54 -0700813 if (nodep)
814 *nodep = parent;
815 if (slotp)
816 *slotp = slot;
817 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700818}
819
820/**
821 * radix_tree_lookup_slot - lookup a slot in a radix tree
822 * @root: radix tree root
823 * @index: index key
824 *
825 * Returns: the slot corresponding to the position @index in the
826 * radix tree @root. This is useful for update-if-exists operations.
827 *
828 * This function can be called under rcu_read_lock iff the slot is not
829 * modified by radix_tree_replace_slot, otherwise it must be called
830 * exclusive from other writers. Any dereference of the slot must be done
831 * using radix_tree_deref_slot.
832 */
833void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
834{
Johannes Weiner139e5612014-04-03 14:47:54 -0700835 void **slot;
836
837 if (!__radix_tree_lookup(root, index, NULL, &slot))
838 return NULL;
839 return slot;
Hans Reisera4331362005-11-07 00:59:29 -0800840}
841EXPORT_SYMBOL(radix_tree_lookup_slot);
842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843/**
844 * radix_tree_lookup - perform lookup operation on a radix tree
845 * @root: radix tree root
846 * @index: index key
847 *
848 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800849 *
850 * This function can be called under rcu_read_lock, however the caller
851 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
852 * them safely). No RCU barriers are required to access or modify the
853 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 */
855void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
856{
Johannes Weiner139e5612014-04-03 14:47:54 -0700857 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859EXPORT_SYMBOL(radix_tree_lookup);
860
Johannes Weiner6d75f362016-12-12 16:43:43 -0800861static void replace_slot(struct radix_tree_root *root,
862 struct radix_tree_node *node,
863 void **slot, void *item,
864 bool warn_typeswitch)
865{
866 void *old = rcu_dereference_raw(*slot);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800867 int count, exceptional;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800868
869 WARN_ON_ONCE(radix_tree_is_internal_node(item));
Johannes Weiner6d75f362016-12-12 16:43:43 -0800870
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800871 count = !!item - !!old;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800872 exceptional = !!radix_tree_exceptional_entry(item) -
873 !!radix_tree_exceptional_entry(old);
874
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800875 WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
Johannes Weiner6d75f362016-12-12 16:43:43 -0800876
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800877 if (node) {
878 node->count += count;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800879 node->exceptional += exceptional;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800880 }
Johannes Weiner6d75f362016-12-12 16:43:43 -0800881
882 rcu_assign_pointer(*slot, item);
883}
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885/**
Johannes Weinerf7942432016-12-12 16:43:41 -0800886 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -0800887 * @root: radix tree root
888 * @node: pointer to tree node
889 * @slot: pointer to slot in @node
890 * @item: new item to store in the slot.
891 * @update_node: callback for changing leaf nodes
892 * @private: private data to pass to @update_node
Johannes Weinerf7942432016-12-12 16:43:41 -0800893 *
894 * For use with __radix_tree_lookup(). Caller must hold tree write locked
895 * across slot lookup and replacement.
896 */
897void __radix_tree_replace(struct radix_tree_root *root,
898 struct radix_tree_node *node,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800899 void **slot, void *item,
900 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf7942432016-12-12 16:43:41 -0800901{
Johannes Weiner6d75f362016-12-12 16:43:43 -0800902 /*
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800903 * This function supports replacing exceptional entries and
904 * deleting entries, but that needs accounting against the
905 * node unless the slot is root->rnode.
Johannes Weiner6d75f362016-12-12 16:43:43 -0800906 */
907 replace_slot(root, node, slot, item,
908 !node && slot != (void **)&root->rnode);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800909
Johannes Weiner4d693d02016-12-12 16:43:49 -0800910 if (!node)
911 return;
912
913 if (update_node)
914 update_node(node, private);
915
916 delete_node(root, node, update_node, private);
Johannes Weiner6d75f362016-12-12 16:43:43 -0800917}
Johannes Weinerf7942432016-12-12 16:43:41 -0800918
Johannes Weiner6d75f362016-12-12 16:43:43 -0800919/**
920 * radix_tree_replace_slot - replace item in a slot
921 * @root: radix tree root
922 * @slot: pointer to slot
923 * @item: new item to store in the slot.
924 *
925 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
926 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
927 * across slot lookup and replacement.
928 *
929 * NOTE: This cannot be used to switch between non-entries (empty slots),
930 * regular entries, and exceptional entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800931 * inside the radix tree node. When switching from one type of entry or
932 * deleting, use __radix_tree_lookup() and __radix_tree_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -0800933 */
934void radix_tree_replace_slot(struct radix_tree_root *root,
935 void **slot, void *item)
936{
937 replace_slot(root, NULL, slot, item, true);
Johannes Weinerf7942432016-12-12 16:43:41 -0800938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/**
941 * radix_tree_tag_set - set a tag on a radix tree node
942 * @root: radix tree root
943 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700944 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800946 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
947 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * the root all the way down to the leaf node.
949 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700950 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * item is a bug.
952 */
953void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800954 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955{
Ross Zwislerfb969902016-05-20 17:02:32 -0700956 struct radix_tree_node *node, *parent;
957 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700959 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -0700960 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700962 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -0700963 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700965 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700966 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -0700967 BUG_ON(!node);
968
969 if (!tag_get(parent, tag, offset))
970 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Nick Piggin612d6c12006-06-23 02:03:22 -0700973 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -0700974 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -0700975 root_tag_set(root, tag);
976
Ross Zwislerfb969902016-05-20 17:02:32 -0700977 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979EXPORT_SYMBOL(radix_tree_tag_set);
980
Matthew Wilcoxd604c322016-05-20 17:03:45 -0700981static void node_tag_clear(struct radix_tree_root *root,
982 struct radix_tree_node *node,
983 unsigned int tag, unsigned int offset)
984{
985 while (node) {
986 if (!tag_get(node, tag, offset))
987 return;
988 tag_clear(node, tag, offset);
989 if (any_tag_set(node, tag))
990 return;
991
992 offset = node->offset;
993 node = node->parent;
994 }
995
996 /* clear the root's tag bit */
997 if (root_tag_get(root, tag))
998 root_tag_clear(root, tag);
999}
1000
Matthew Wilcox9498d2b2016-12-14 15:08:37 -08001001static void node_tag_set(struct radix_tree_root *root,
1002 struct radix_tree_node *node,
1003 unsigned int tag, unsigned int offset)
1004{
1005 while (node) {
1006 if (tag_get(node, tag, offset))
1007 return;
1008 tag_set(node, tag, offset);
1009 offset = node->offset;
1010 node = node->parent;
1011 }
1012
1013 if (!root_tag_get(root, tag))
1014 root_tag_set(root, tag);
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017/**
1018 * radix_tree_tag_clear - clear a tag on a radix tree node
1019 * @root: radix tree root
1020 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001021 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001023 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001024 * corresponding to @index in the radix tree. If this causes
1025 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 * next-to-leaf node, etc.
1027 *
1028 * Returns the address of the tagged item on success, else NULL. ie:
1029 * has the same return value and semantics as radix_tree_lookup().
1030 */
1031void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001032 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001034 struct radix_tree_node *node, *parent;
1035 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001036 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001038 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001039 if (index > maxindex)
1040 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ross Zwisler00f47b52016-05-20 17:02:35 -07001042 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001044 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001045 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001046 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001049 if (node)
1050 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Ross Zwisler00f47b52016-05-20 17:02:35 -07001052 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053}
1054EXPORT_SYMBOL(radix_tree_tag_clear);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001057 * radix_tree_tag_get - get a tag on a radix tree node
1058 * @root: radix tree root
1059 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001060 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001062 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001064 * 0: tag not present or not set
1065 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001066 *
1067 * Note that the return value of this function may not be relied on, even if
1068 * the RCU lock is held, unless tag modification and node deletion are excluded
1069 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 */
1071int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001072 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001074 struct radix_tree_node *node, *parent;
1075 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Nick Piggin612d6c12006-06-23 02:03:22 -07001077 if (!root_tag_get(root, tag))
1078 return 0;
1079
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001080 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001081 if (index > maxindex)
1082 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001083 if (node == NULL)
1084 return 0;
1085
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001086 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001087 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001088
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001089 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001090 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001091
1092 if (!node)
1093 return 0;
1094 if (!tag_get(parent, tag, offset))
1095 return 0;
1096 if (node == RADIX_TREE_RETRY)
1097 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001099
1100 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Ross Zwisler21ef5332016-05-20 17:02:26 -07001104static inline void __set_iter_shift(struct radix_tree_iter *iter,
1105 unsigned int shift)
1106{
1107#ifdef CONFIG_RADIX_TREE_MULTIORDER
1108 iter->shift = shift;
1109#endif
1110}
1111
Matthew Wilcox148deab2016-12-14 15:08:49 -08001112/* Construct iter->tags bit-mask from node->tags[tag] array */
1113static void set_iter_tags(struct radix_tree_iter *iter,
1114 struct radix_tree_node *node, unsigned offset,
1115 unsigned tag)
1116{
1117 unsigned tag_long = offset / BITS_PER_LONG;
1118 unsigned tag_bit = offset % BITS_PER_LONG;
1119
1120 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1121
1122 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1123 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1124 /* Pick tags from next element */
1125 if (tag_bit)
1126 iter->tags |= node->tags[tag][tag_long + 1] <<
1127 (BITS_PER_LONG - tag_bit);
1128 /* Clip chunk size, here only BITS_PER_LONG tags */
1129 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1130 }
1131}
1132
1133#ifdef CONFIG_RADIX_TREE_MULTIORDER
1134static void **skip_siblings(struct radix_tree_node **nodep,
1135 void **slot, struct radix_tree_iter *iter)
1136{
1137 void *sib = node_to_entry(slot - 1);
1138
1139 while (iter->index < iter->next_index) {
1140 *nodep = rcu_dereference_raw(*slot);
1141 if (*nodep && *nodep != sib)
1142 return slot;
1143 slot++;
1144 iter->index = __radix_tree_iter_add(iter, 1);
1145 iter->tags >>= 1;
1146 }
1147
1148 *nodep = NULL;
1149 return NULL;
1150}
1151
1152void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
1153 unsigned flags)
1154{
1155 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1156 struct radix_tree_node *node = rcu_dereference_raw(*slot);
1157
1158 slot = skip_siblings(&node, slot, iter);
1159
1160 while (radix_tree_is_internal_node(node)) {
1161 unsigned offset;
1162 unsigned long next_index;
1163
1164 if (node == RADIX_TREE_RETRY)
1165 return slot;
1166 node = entry_to_node(node);
1167 iter->shift = node->shift;
1168
1169 if (flags & RADIX_TREE_ITER_TAGGED) {
1170 offset = radix_tree_find_next_bit(node, tag, 0);
1171 if (offset == RADIX_TREE_MAP_SIZE)
1172 return NULL;
1173 slot = &node->slots[offset];
1174 iter->index = __radix_tree_iter_add(iter, offset);
1175 set_iter_tags(iter, node, offset, tag);
1176 node = rcu_dereference_raw(*slot);
1177 } else {
1178 offset = 0;
1179 slot = &node->slots[0];
1180 for (;;) {
1181 node = rcu_dereference_raw(*slot);
1182 if (node)
1183 break;
1184 slot++;
1185 offset++;
1186 if (offset == RADIX_TREE_MAP_SIZE)
1187 return NULL;
1188 }
1189 iter->index = __radix_tree_iter_add(iter, offset);
1190 }
1191 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1192 goto none;
1193 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1194 if (next_index < iter->next_index)
1195 iter->next_index = next_index;
1196 }
1197
1198 return slot;
1199 none:
1200 iter->next_index = 0;
1201 return NULL;
1202}
1203EXPORT_SYMBOL(__radix_tree_next_slot);
1204#else
1205static void **skip_siblings(struct radix_tree_node **nodep,
1206 void **slot, struct radix_tree_iter *iter)
1207{
1208 return slot;
1209}
1210#endif
1211
1212void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
1213{
1214 struct radix_tree_node *node;
1215
1216 slot++;
1217 iter->index = __radix_tree_iter_add(iter, 1);
1218 node = rcu_dereference_raw(*slot);
1219 skip_siblings(&node, slot, iter);
1220 iter->next_index = iter->index;
1221 iter->tags = 0;
1222 return NULL;
1223}
1224EXPORT_SYMBOL(radix_tree_iter_resume);
1225
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001226/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001227 * radix_tree_next_chunk - find next chunk of slots for iteration
1228 *
1229 * @root: radix tree root
1230 * @iter: iterator state
1231 * @flags: RADIX_TREE_ITER_* flags and tag index
1232 * Returns: pointer to chunk first slot, or NULL if iteration is over
1233 */
1234void **radix_tree_next_chunk(struct radix_tree_root *root,
1235 struct radix_tree_iter *iter, unsigned flags)
1236{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001237 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001238 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001239 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001240
1241 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1242 return NULL;
1243
1244 /*
1245 * Catch next_index overflow after ~0UL. iter->index never overflows
1246 * during iterating; it can be zero only at the beginning.
1247 * And we cannot overflow iter->next_index in a single step,
1248 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001249 *
1250 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001251 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001252 */
1253 index = iter->next_index;
1254 if (!index && iter->index)
1255 return NULL;
1256
Ross Zwisler21ef5332016-05-20 17:02:26 -07001257 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001258 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001259 if (index > maxindex)
1260 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001261 if (!child)
1262 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001263
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001264 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001265 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001266 iter->index = index;
1267 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001268 iter->tags = 1;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001269 __set_iter_shift(iter, 0);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001270 return (void **)&root->rnode;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001271 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001272
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001273 do {
1274 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001275 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001276
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001277 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001278 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001279 /* Hole detected */
1280 if (flags & RADIX_TREE_ITER_CONTIG)
1281 return NULL;
1282
1283 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001284 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001285 offset + 1);
1286 else
1287 while (++offset < RADIX_TREE_MAP_SIZE) {
Ross Zwisler21ef5332016-05-20 17:02:26 -07001288 void *slot = node->slots[offset];
1289 if (is_sibling_entry(node, slot))
1290 continue;
1291 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001292 break;
1293 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001294 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001295 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001296 /* Overflow after ~0UL */
1297 if (!index)
1298 return NULL;
1299 if (offset == RADIX_TREE_MAP_SIZE)
1300 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001301 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001302 }
1303
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001304 if ((child == NULL) || (child == RADIX_TREE_RETRY))
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001305 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001306 } while (radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001307
1308 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001309 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1310 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001311 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001312
Matthew Wilcox148deab2016-12-14 15:08:49 -08001313 if (flags & RADIX_TREE_ITER_TAGGED)
1314 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001315
1316 return node->slots + offset;
1317}
1318EXPORT_SYMBOL(radix_tree_next_chunk);
1319
1320/**
Jan Karaebf8aa42010-08-09 17:19:11 -07001321 * radix_tree_range_tag_if_tagged - for each item in given range set given
1322 * tag if item has another tag set
1323 * @root: radix tree root
1324 * @first_indexp: pointer to a starting index of a range to scan
1325 * @last_index: last index of a range to scan
1326 * @nr_to_tag: maximum number items to tag
1327 * @iftag: tag index to test
1328 * @settag: tag index to set if tested tag is set
1329 *
1330 * This function scans range of radix tree from first_index to last_index
1331 * (inclusive). For each item in the range if iftag is set, the function sets
1332 * also settag. The function stops either after tagging nr_to_tag items or
1333 * after reaching last_index.
1334 *
Dave Chinner144dcfc2010-08-23 10:33:53 +10001335 * The tags must be set from the leaf level only and propagated back up the
1336 * path to the root. We must do this so that we resolve the full path before
1337 * setting any tags on intermediate nodes. If we set tags as we descend, then
1338 * we can get to the leaf node and find that the index that has the iftag
1339 * set is outside the range we are scanning. This reults in dangling tags and
1340 * can lead to problems with later tag operations (e.g. livelocks on lookups).
1341 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001342 * The function returns the number of leaves where the tag was set and sets
Jan Karaebf8aa42010-08-09 17:19:11 -07001343 * *first_indexp to the first unscanned index.
Jan Karad5ed3a42010-08-19 14:13:33 -07001344 * WARNING! *first_indexp can wrap if last_index is ULONG_MAX. Caller must
1345 * be prepared to handle that.
Jan Karaebf8aa42010-08-09 17:19:11 -07001346 */
1347unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
1348 unsigned long *first_indexp, unsigned long last_index,
1349 unsigned long nr_to_tag,
1350 unsigned int iftag, unsigned int settag)
1351{
Matthew Wilcox9498d2b2016-12-14 15:08:37 -08001352 struct radix_tree_node *node, *child;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001353 unsigned long maxindex;
Dave Chinner144dcfc2010-08-23 10:33:53 +10001354 unsigned long tagged = 0;
1355 unsigned long index = *first_indexp;
Jan Karaebf8aa42010-08-09 17:19:11 -07001356
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001357 radix_tree_load_root(root, &child, &maxindex);
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001358 last_index = min(last_index, maxindex);
Jan Karaebf8aa42010-08-09 17:19:11 -07001359 if (index > last_index)
1360 return 0;
1361 if (!nr_to_tag)
1362 return 0;
1363 if (!root_tag_get(root, iftag)) {
1364 *first_indexp = last_index + 1;
1365 return 0;
1366 }
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001367 if (!radix_tree_is_internal_node(child)) {
Jan Karaebf8aa42010-08-09 17:19:11 -07001368 *first_indexp = last_index + 1;
1369 root_tag_set(root, settag);
1370 return 1;
1371 }
1372
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001373 node = entry_to_node(child);
Jan Karaebf8aa42010-08-09 17:19:11 -07001374
1375 for (;;) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001376 unsigned offset = radix_tree_descend(node, &child, index);
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001377 if (!child)
Jan Karaebf8aa42010-08-09 17:19:11 -07001378 goto next;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001379 if (!tag_get(node, iftag, offset))
Jan Karaebf8aa42010-08-09 17:19:11 -07001380 goto next;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001381 /* Sibling slots never have tags set on them */
Matthew Wilcoxa8e4da22016-05-20 17:03:39 -07001382 if (radix_tree_is_internal_node(child)) {
1383 node = entry_to_node(child);
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001384 continue;
Jan Karaebf8aa42010-08-09 17:19:11 -07001385 }
Dave Chinner144dcfc2010-08-23 10:33:53 +10001386
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001387 tagged++;
Matthew Wilcox9498d2b2016-12-14 15:08:37 -08001388 node_tag_set(root, node, settag, offset);
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001389 next:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001390 /* Go to next entry in node */
1391 index = ((index >> node->shift) + 1) << node->shift;
Jan Karad5ed3a42010-08-19 14:13:33 -07001392 /* Overflow can happen when last_index is ~0UL... */
1393 if (index > last_index || !index)
Jan Karaebf8aa42010-08-09 17:19:11 -07001394 break;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001395 offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001396 while (offset == 0) {
Jan Karaebf8aa42010-08-09 17:19:11 -07001397 /*
1398 * We've fully scanned this node. Go up. Because
1399 * last_index is guaranteed to be in the tree, what
1400 * we do below cannot wander astray.
1401 */
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001402 node = node->parent;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001403 offset = (index >> node->shift) & RADIX_TREE_MAP_MASK;
Jan Karaebf8aa42010-08-09 17:19:11 -07001404 }
Matthew Wilcox070c5ac2016-05-20 17:02:52 -07001405 if (is_sibling_entry(node, node->slots[offset]))
1406 goto next;
1407 if (tagged >= nr_to_tag)
1408 break;
Jan Karaebf8aa42010-08-09 17:19:11 -07001409 }
Matthew Wilcox9498d2b2016-12-14 15:08:37 -08001410
Jan Karaebf8aa42010-08-09 17:19:11 -07001411 *first_indexp = index;
1412
1413 return tagged;
1414}
1415EXPORT_SYMBOL(radix_tree_range_tag_if_tagged);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/**
1418 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1419 * @root: radix tree root
1420 * @results: where the results of the lookup are placed
1421 * @first_index: start the lookup from this key
1422 * @max_items: place up to this many items at *results
1423 *
1424 * Performs an index-ascending scan of the tree for present items. Places
1425 * them at *@results and returns the number of items which were placed at
1426 * *@results.
1427 *
1428 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001429 *
1430 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1431 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001432 * an atomic snapshot of the tree at a single point in time, the
1433 * semantics of an RCU protected gang lookup are as though multiple
1434 * radix_tree_lookups have been issued in individual locks, and results
1435 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
1437unsigned int
1438radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1439 unsigned long first_index, unsigned int max_items)
1440{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001441 struct radix_tree_iter iter;
1442 void **slot;
1443 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001445 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001446 return 0;
1447
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001448 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001449 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001450 if (!results[ret])
1451 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001452 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001453 slot = radix_tree_iter_retry(&iter);
1454 continue;
1455 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001456 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 return ret;
1461}
1462EXPORT_SYMBOL(radix_tree_gang_lookup);
1463
Nick Piggin47feff22008-07-25 19:45:29 -07001464/**
1465 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1466 * @root: radix tree root
1467 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001468 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001469 * @first_index: start the lookup from this key
1470 * @max_items: place up to this many items at *results
1471 *
1472 * Performs an index-ascending scan of the tree for present items. Places
1473 * their slots at *@results and returns the number of items which were
1474 * placed at *@results.
1475 *
1476 * The implementation is naive.
1477 *
1478 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1479 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1480 * protection, radix_tree_deref_slot may fail requiring a retry.
1481 */
1482unsigned int
Hugh Dickins63286502011-08-03 16:21:18 -07001483radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1484 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001485 unsigned long first_index, unsigned int max_items)
1486{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001487 struct radix_tree_iter iter;
1488 void **slot;
1489 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001490
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001491 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001492 return 0;
1493
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001494 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1495 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001496 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001497 indices[ret] = iter.index;
1498 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001499 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001500 }
1501
1502 return ret;
1503}
1504EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506/**
1507 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1508 * based on a tag
1509 * @root: radix tree root
1510 * @results: where the results of the lookup are placed
1511 * @first_index: start the lookup from this key
1512 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001513 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 *
1515 * Performs an index-ascending scan of the tree for present items which
1516 * have the tag indexed by @tag set. Places the items at *@results and
1517 * returns the number of items which were placed at *@results.
1518 */
1519unsigned int
1520radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001521 unsigned long first_index, unsigned int max_items,
1522 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001524 struct radix_tree_iter iter;
1525 void **slot;
1526 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001528 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001529 return 0;
1530
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001531 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001532 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001533 if (!results[ret])
1534 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001535 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001536 slot = radix_tree_iter_retry(&iter);
1537 continue;
1538 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001539 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return ret;
1544}
1545EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1546
1547/**
Nick Piggin47feff22008-07-25 19:45:29 -07001548 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1549 * radix tree based on a tag
1550 * @root: radix tree root
1551 * @results: where the results of the lookup are placed
1552 * @first_index: start the lookup from this key
1553 * @max_items: place up to this many items at *results
1554 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1555 *
1556 * Performs an index-ascending scan of the tree for present items which
1557 * have the tag indexed by @tag set. Places the slots at *@results and
1558 * returns the number of slots which were placed at *@results.
1559 */
1560unsigned int
1561radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1562 unsigned long first_index, unsigned int max_items,
1563 unsigned int tag)
1564{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001565 struct radix_tree_iter iter;
1566 void **slot;
1567 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001568
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001569 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001570 return 0;
1571
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001572 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1573 results[ret] = slot;
1574 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001575 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001576 }
1577
1578 return ret;
1579}
1580EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1581
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001582#if defined(CONFIG_SHMEM) && defined(CONFIG_SWAP)
1583#include <linux/sched.h> /* for cond_resched() */
1584
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001585struct locate_info {
1586 unsigned long found_index;
1587 bool stop;
1588};
1589
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001590/*
1591 * This linear search is at present only useful to shmem_unuse_inode().
1592 */
1593static unsigned long __locate(struct radix_tree_node *slot, void *item,
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001594 unsigned long index, struct locate_info *info)
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001595{
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001596 unsigned long i;
1597
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001598 do {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001599 unsigned int shift = slot->shift;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001600
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001601 for (i = (index >> shift) & RADIX_TREE_MAP_MASK;
1602 i < RADIX_TREE_MAP_SIZE;
1603 i++, index += (1UL << shift)) {
1604 struct radix_tree_node *node =
1605 rcu_dereference_raw(slot->slots[i]);
1606 if (node == RADIX_TREE_RETRY)
1607 goto out;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001608 if (!radix_tree_is_internal_node(node)) {
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001609 if (node == item) {
1610 info->found_index = index;
1611 info->stop = true;
1612 goto out;
1613 }
1614 continue;
1615 }
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001616 node = entry_to_node(node);
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001617 if (is_sibling_entry(slot, node))
1618 continue;
1619 slot = node;
1620 break;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001621 }
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001622 } while (i < RADIX_TREE_MAP_SIZE);
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001623
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001624out:
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001625 if ((index == 0) && (i == RADIX_TREE_MAP_SIZE))
1626 info->stop = true;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001627 return index;
1628}
1629
1630/**
1631 * radix_tree_locate_item - search through radix tree for item
1632 * @root: radix tree root
1633 * @item: item to be found
1634 *
1635 * Returns index where item was found, or -1 if not found.
1636 * Caller must hold no lock (since this time-consuming function needs
1637 * to be preemptible), and must check afterwards if item is still there.
1638 */
1639unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1640{
1641 struct radix_tree_node *node;
1642 unsigned long max_index;
1643 unsigned long cur_index = 0;
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001644 struct locate_info info = {
1645 .found_index = -1,
1646 .stop = false,
1647 };
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001648
1649 do {
1650 rcu_read_lock();
1651 node = rcu_dereference_raw(root->rnode);
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001652 if (!radix_tree_is_internal_node(node)) {
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001653 rcu_read_unlock();
1654 if (node == item)
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001655 info.found_index = 0;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001656 break;
1657 }
1658
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001659 node = entry_to_node(node);
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001660
1661 max_index = node_maxindex(node);
Hugh Dickins5f30fc92014-03-03 15:38:23 -08001662 if (cur_index > max_index) {
1663 rcu_read_unlock();
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001664 break;
Hugh Dickins5f30fc92014-03-03 15:38:23 -08001665 }
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001666
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001667 cur_index = __locate(node, item, cur_index, &info);
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001668 rcu_read_unlock();
1669 cond_resched();
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001670 } while (!info.stop && cur_index <= max_index);
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001671
Matthew Wilcox0a2efc62016-05-20 17:02:46 -07001672 return info.found_index;
Hugh Dickinse504f3f2011-08-03 16:21:27 -07001673}
1674#else
1675unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item)
1676{
1677 return -1;
1678}
1679#endif /* CONFIG_SHMEM && CONFIG_SWAP */
Nick Piggin47feff22008-07-25 19:45:29 -07001680
1681/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001682 * __radix_tree_delete_node - try to free node after clearing a slot
1683 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001684 * @node: node containing @index
1685 *
1686 * After clearing the slot at @index in @node from radix tree
1687 * rooted at @root, call this function to attempt freeing the
1688 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001689 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001690void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weiner139e5612014-04-03 14:47:54 -07001691 struct radix_tree_node *node)
1692{
Johannes Weiner14b46872016-12-12 16:43:52 -08001693 delete_node(root, node, NULL, NULL);
Johannes Weiner139e5612014-04-03 14:47:54 -07001694}
1695
Matthew Wilcox57578c22016-05-20 17:01:54 -07001696static inline void delete_sibling_entries(struct radix_tree_node *node,
1697 void *ptr, unsigned offset)
1698{
1699#ifdef CONFIG_RADIX_TREE_MULTIORDER
1700 int i;
1701 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1702 if (node->slots[offset + i] != ptr)
1703 break;
1704 node->slots[offset + i] = NULL;
1705 node->count--;
1706 }
1707#endif
1708}
1709
Johannes Weiner139e5612014-04-03 14:47:54 -07001710/**
Johannes Weiner53c59f22014-04-03 14:47:39 -07001711 * radix_tree_delete_item - delete an item from a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 * @root: radix tree root
1713 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001714 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001716 * Remove @item at @index from the radix tree rooted at @root.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001718 * Returns the address of the deleted item, or NULL if it was not present
1719 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001721void *radix_tree_delete_item(struct radix_tree_root *root,
1722 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
Johannes Weiner139e5612014-04-03 14:47:54 -07001724 struct radix_tree_node *node;
Matthew Wilcox57578c22016-05-20 17:01:54 -07001725 unsigned int offset;
Johannes Weiner139e5612014-04-03 14:47:54 -07001726 void **slot;
1727 void *entry;
Nick Piggind5274262006-01-08 01:01:41 -08001728 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Johannes Weiner139e5612014-04-03 14:47:54 -07001730 entry = __radix_tree_lookup(root, index, &node, &slot);
1731 if (!entry)
1732 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Johannes Weiner139e5612014-04-03 14:47:54 -07001734 if (item && entry != item)
1735 return NULL;
1736
1737 if (!node) {
Nick Piggin612d6c12006-06-23 02:03:22 -07001738 root_tag_clear_all(root);
1739 root->rnode = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001740 return entry;
Nick Piggin612d6c12006-06-23 02:03:22 -07001741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Matthew Wilcox29e09672016-05-20 17:02:02 -07001743 offset = get_slot_offset(node, slot);
Johannes Weiner53c59f22014-04-03 14:47:39 -07001744
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001745 /* Clear all tags associated with the item to be deleted. */
1746 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1747 node_tag_clear(root, node, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -07001749 delete_sibling_entries(node, node_to_entry(slot), offset);
Johannes Weiner4d693d02016-12-12 16:43:49 -08001750 __radix_tree_replace(root, node, slot, NULL, NULL, NULL);
Christoph Lameter201b6262005-09-06 15:16:46 -07001751
Johannes Weiner139e5612014-04-03 14:47:54 -07001752 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001754EXPORT_SYMBOL(radix_tree_delete_item);
1755
1756/**
1757 * radix_tree_delete - delete an item from a radix tree
1758 * @root: radix tree root
1759 * @index: index key
1760 *
1761 * Remove the item at @index from the radix tree rooted at @root.
1762 *
1763 * Returns the address of the deleted item, or NULL if it was not present.
1764 */
1765void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1766{
1767 return radix_tree_delete_item(root, index, NULL);
1768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769EXPORT_SYMBOL(radix_tree_delete);
1770
Johannes Weinerd3798ae2016-10-04 22:02:08 +02001771void radix_tree_clear_tags(struct radix_tree_root *root,
1772 struct radix_tree_node *node,
1773 void **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001774{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001775 if (node) {
1776 unsigned int tag, offset = get_slot_offset(node, slot);
1777 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1778 node_tag_clear(root, node, tag, offset);
1779 } else {
1780 /* Clear root node tags */
1781 root->gfp_mask &= __GFP_BITS_MASK;
1782 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001783}
1784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785/**
1786 * radix_tree_tagged - test whether any items in the tree are tagged
1787 * @root: radix tree root
1788 * @tag: tag to test
1789 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001790int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Nick Piggin612d6c12006-06-23 02:03:22 -07001792 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794EXPORT_SYMBOL(radix_tree_tagged);
1795
1796static void
Johannes Weiner449dd692014-04-03 14:47:56 -07001797radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Johannes Weiner449dd692014-04-03 14:47:56 -07001799 struct radix_tree_node *node = arg;
1800
1801 memset(node, 0, sizeof(*node));
1802 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001805static __init unsigned long __maxindex(unsigned int height)
1806{
1807 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1808 int shift = RADIX_TREE_INDEX_BITS - width;
1809
1810 if (shift < 0)
1811 return ~0UL;
1812 if (shift >= BITS_PER_LONG)
1813 return 0UL;
1814 return ~0UL >> shift;
1815}
1816
1817static __init void radix_tree_init_maxnodes(void)
1818{
1819 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1820 unsigned int i, j;
1821
1822 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1823 height_to_maxindex[i] = __maxindex(i);
1824 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1825 for (j = i; j > 0; j--)
1826 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1827 }
1828}
1829
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001830static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001832 struct radix_tree_preload *rtp;
1833 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001835 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001836 rtp = &per_cpu(radix_tree_preloads, cpu);
1837 while (rtp->nr) {
1838 node = rtp->nodes;
1839 rtp->nodes = node->private_data;
1840 kmem_cache_free(radix_tree_node_cachep, node);
1841 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001842 }
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001843 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
1846void __init radix_tree_init(void)
1847{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001848 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1850 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07001851 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1852 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001853 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001854 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
1855 NULL, radix_tree_cpu_dead);
1856 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}