blob: ade2ed3f5190ce6a25a4a6baec9a59f5f33a6b15 [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
Matthew Wilcoxe157b552016-12-14 15:09:01 -080025#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/radix-tree.h>
31#include <linux/percpu.h>
32#include <linux/slab.h>
Catalin Marinasce80b062014-06-06 14:38:18 -070033#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/notifier.h>
35#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/string.h>
37#include <linux/bitops.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080038#include <linux/rcupdate.h>
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020039#include <linux/preempt.h> /* in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -070042/* Number of nodes in fully populated tree of given height */
43static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
44
Jeff Moyer26fb1582007-10-16 01:24:49 -070045/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * Radix tree node cache.
47 */
Christoph Lametere18b8902006-12-06 20:33:20 -080048static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
Nick Piggin55368052012-05-29 15:07:34 -070051 * The radix tree is variable-height, so an insert operation not only has
52 * to build the branch to its corresponding item, it also has to build the
53 * branch to existing items if the size has to be increased (by
54 * radix_tree_extend).
55 *
56 * The worst case is a zero height tree with just a single item at index 0,
57 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
58 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
59 * Hence:
60 */
61#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
62
63/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * Per-cpu pool of preloaded nodes
65 */
66struct radix_tree_preload {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -070067 unsigned nr;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -070068 /* nodes->private_data points to next preallocated node */
69 struct radix_tree_node *nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080071static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Matthew Wilcox148deab2016-12-14 15:08:49 -080073static inline struct radix_tree_node *entry_to_node(void *ptr)
74{
75 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
76}
77
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070078static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080079{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070080 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -080081}
82
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070083#define RADIX_TREE_RETRY node_to_entry(NULL)
Matthew Wilcoxafe0e392016-05-20 17:02:17 -070084
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070085#ifdef CONFIG_RADIX_TREE_MULTIORDER
86/* Sibling slots point directly to another slot in the same node */
87static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
88{
89 void **ptr = node;
90 return (parent->slots <= ptr) &&
91 (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
92}
93#else
94static inline bool is_sibling_entry(struct radix_tree_node *parent, void *node)
95{
96 return false;
97}
98#endif
99
100static inline unsigned long get_slot_offset(struct radix_tree_node *parent,
101 void **slot)
102{
103 return slot - parent->slots;
104}
105
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700106static unsigned int radix_tree_descend(struct radix_tree_node *parent,
107 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700108{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700109 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700110 void **entry = rcu_dereference_raw(parent->slots[offset]);
111
112#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700113 if (radix_tree_is_internal_node(entry)) {
Linus Torvalds8d2c0d32016-09-25 13:32:46 -0700114 if (is_sibling_entry(parent, entry)) {
115 void **sibentry = (void **) entry_to_node(entry);
116 offset = get_slot_offset(parent, sibentry);
117 entry = rcu_dereference_raw(*sibentry);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700118 }
119 }
120#endif
121
122 *nodep = (void *)entry;
123 return offset;
124}
125
Nick Piggin612d6c12006-06-23 02:03:22 -0700126static inline gfp_t root_gfp_mask(struct radix_tree_root *root)
127{
128 return root->gfp_mask & __GFP_BITS_MASK;
129}
130
Nick Piggin643b52b2008-06-12 15:21:52 -0700131static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
132 int offset)
133{
134 __set_bit(offset, node->tags[tag]);
135}
136
137static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
138 int offset)
139{
140 __clear_bit(offset, node->tags[tag]);
141}
142
143static inline int tag_get(struct radix_tree_node *node, unsigned int tag,
144 int offset)
145{
146 return test_bit(offset, node->tags[tag]);
147}
148
149static inline void root_tag_set(struct radix_tree_root *root, unsigned int tag)
150{
151 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
152}
153
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700154static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700155{
156 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
157}
158
159static inline void root_tag_clear_all(struct radix_tree_root *root)
160{
161 root->gfp_mask &= __GFP_BITS_MASK;
162}
163
164static inline int root_tag_get(struct radix_tree_root *root, unsigned int tag)
165{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700166 return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700167}
168
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700169static inline unsigned root_tags_get(struct radix_tree_root *root)
170{
171 return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT;
172}
173
Nick Piggin643b52b2008-06-12 15:21:52 -0700174/*
175 * Returns 1 if any slot in the node has this tag set.
176 * Otherwise returns 0.
177 */
178static inline int any_tag_set(struct radix_tree_node *node, unsigned int tag)
179{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700180 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700181 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
182 if (node->tags[tag][idx])
183 return 1;
184 }
185 return 0;
186}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700187
188/**
189 * radix_tree_find_next_bit - find the next set bit in a memory region
190 *
191 * @addr: The address to base the search on
192 * @size: The bitmap size in bits
193 * @offset: The bitnumber to start searching at
194 *
195 * Unrollable variant of find_next_bit() for constant size arrays.
196 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
197 * Returns next bit offset, or size if nothing found.
198 */
199static __always_inline unsigned long
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800200radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
201 unsigned long offset)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700202{
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800203 const unsigned long *addr = node->tags[tag];
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700204
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800205 if (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700206 unsigned long tmp;
207
208 addr += offset / BITS_PER_LONG;
209 tmp = *addr >> (offset % BITS_PER_LONG);
210 if (tmp)
211 return __ffs(tmp) + offset;
212 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800213 while (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700214 tmp = *++addr;
215 if (tmp)
216 return __ffs(tmp) + offset;
217 offset += BITS_PER_LONG;
218 }
219 }
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800220 return RADIX_TREE_MAP_SIZE;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700221}
222
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800223static unsigned int iter_offset(const struct radix_tree_iter *iter)
224{
225 return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
226}
227
Matthew Wilcox218ed752016-12-14 15:08:43 -0800228/*
229 * The maximum index which can be stored in a radix tree
230 */
231static inline unsigned long shift_maxindex(unsigned int shift)
232{
233 return (RADIX_TREE_MAP_SIZE << shift) - 1;
234}
235
236static inline unsigned long node_maxindex(struct radix_tree_node *node)
237{
238 return shift_maxindex(node->shift);
239}
240
Ross Zwisler0796c582016-05-20 17:02:55 -0700241#ifndef __KERNEL__
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700242static void dump_node(struct radix_tree_node *node, unsigned long index)
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700243{
Ross Zwisler0796c582016-05-20 17:02:55 -0700244 unsigned long i;
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700245
Matthew Wilcox218ed752016-12-14 15:08:43 -0800246 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
247 node, node->offset, index, index | node_maxindex(node),
248 node->parent,
Ross Zwisler0796c582016-05-20 17:02:55 -0700249 node->tags[0][0], node->tags[1][0], node->tags[2][0],
Matthew Wilcox218ed752016-12-14 15:08:43 -0800250 node->shift, node->count, node->exceptional);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700251
Ross Zwisler0796c582016-05-20 17:02:55 -0700252 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700253 unsigned long first = index | (i << node->shift);
254 unsigned long last = first | ((1UL << node->shift) - 1);
Ross Zwisler0796c582016-05-20 17:02:55 -0700255 void *entry = node->slots[i];
256 if (!entry)
257 continue;
Matthew Wilcox218ed752016-12-14 15:08:43 -0800258 if (entry == RADIX_TREE_RETRY) {
259 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
260 i, first, last, node);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700261 } else if (!radix_tree_is_internal_node(entry)) {
Matthew Wilcox218ed752016-12-14 15:08:43 -0800262 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
263 entry, i, first, last, node);
264 } else if (is_sibling_entry(node, entry)) {
265 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
266 entry, i, first, last, node,
267 *(void **)entry_to_node(entry));
Ross Zwisler0796c582016-05-20 17:02:55 -0700268 } else {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700269 dump_node(entry_to_node(entry), first);
Ross Zwisler0796c582016-05-20 17:02:55 -0700270 }
271 }
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700272}
273
274/* For debug */
275static void radix_tree_dump(struct radix_tree_root *root)
276{
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700277 pr_debug("radix root: %p rnode %p tags %x\n",
278 root, root->rnode,
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700279 root->gfp_mask >> __GFP_BITS_SHIFT);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700280 if (!radix_tree_is_internal_node(root->rnode))
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700281 return;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700282 dump_node(entry_to_node(root->rnode), 0);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700283}
284#endif
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*
287 * This assumes that the caller has performed appropriate preallocation, and
288 * that the caller has pinned this thread of control to the current CPU.
289 */
290static struct radix_tree_node *
291radix_tree_node_alloc(struct radix_tree_root *root)
292{
Nick Piggine2848a02008-02-04 22:29:10 -0800293 struct radix_tree_node *ret = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700294 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Jan Kara5e4c0d972013-09-11 14:26:05 -0700296 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700297 * Preload code isn't irq safe and it doesn't make sense to use
298 * preloading during an interrupt anyway as all the allocations have
299 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700300 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800301 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 struct radix_tree_preload *rtp;
303
Nick Piggine2848a02008-02-04 22:29:10 -0800304 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700305 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700306 * cache first for the new node to get accounted to the memory
307 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700308 */
309 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700310 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700311 if (ret)
312 goto out;
313
314 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800315 * Provided the caller has preloaded here, we will always
316 * succeed in getting a node here (and never reach
317 * kmem_cache_alloc)
318 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700319 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700321 ret = rtp->nodes;
322 rtp->nodes = ret->private_data;
323 ret->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 rtp->nr--;
325 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700326 /*
327 * Update the allocation stack trace as this is more useful
328 * for debugging.
329 */
330 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700333 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700334out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700335 BUG_ON(radix_tree_is_internal_node(ret));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return ret;
337}
338
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800339static void radix_tree_node_rcu_free(struct rcu_head *head)
340{
341 struct radix_tree_node *node =
342 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700343
344 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800345 * Must only free zeroed nodes into the slab. We can be left with
346 * non-NULL entries by radix_tree_free_nodes, so clear the entries
347 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700348 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800349 memset(node->slots, 0, sizeof(node->slots));
350 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800351 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700352
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800353 kmem_cache_free(radix_tree_node_cachep, node);
354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static inline void
357radix_tree_node_free(struct radix_tree_node *node)
358{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800359 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
362/*
363 * Load up this CPU's radix_tree_node buffer with sufficient objects to
364 * ensure that the addition of a single element in the tree cannot fail. On
365 * success, return zero, with preemption disabled. On error, return -ENOMEM
366 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000367 *
368 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800369 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700371static int __radix_tree_preload(gfp_t gfp_mask, int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 struct radix_tree_preload *rtp;
374 struct radix_tree_node *node;
375 int ret = -ENOMEM;
376
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700377 /*
378 * Nodes preloaded by one cgroup can be be used by another cgroup, so
379 * they should never be accounted to any particular memory cgroup.
380 */
381 gfp_mask &= ~__GFP_ACCOUNT;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700384 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700385 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700387 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 if (node == NULL)
389 goto out;
390 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700391 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700392 if (rtp->nr < nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700393 node->private_data = rtp->nodes;
394 rtp->nodes = node;
395 rtp->nr++;
396 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
400 ret = 0;
401out:
402 return ret;
403}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700404
405/*
406 * Load up this CPU's radix_tree_node buffer with sufficient objects to
407 * ensure that the addition of a single element in the tree cannot fail. On
408 * success, return zero, with preemption disabled. On error, return -ENOMEM
409 * with preemption not disabled.
410 *
411 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800412 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700413 */
414int radix_tree_preload(gfp_t gfp_mask)
415{
416 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800417 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700418 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700419}
David Chinnerd7f09232007-07-14 16:05:04 +1000420EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Nick Piggin6e954b92006-01-08 01:01:40 -0800422/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700423 * The same as above function, except we don't guarantee preloading happens.
424 * We do it, if we decide it helps. On success, return zero with preemption
425 * disabled. On error, return -ENOMEM with preemption not disabled.
426 */
427int radix_tree_maybe_preload(gfp_t gfp_mask)
428{
Mel Gormand0164ad2015-11-06 16:28:21 -0800429 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700430 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700431 /* Preloading doesn't help anything with this gfp mask, skip it */
432 preempt_disable();
433 return 0;
434}
435EXPORT_SYMBOL(radix_tree_maybe_preload);
436
437/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700438 * The same as function above, but preload number of nodes required to insert
439 * (1 << order) continuous naturally-aligned elements.
440 */
441int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
442{
443 unsigned long nr_subtrees;
444 int nr_nodes, subtree_height;
445
446 /* Preloading doesn't help anything with this gfp mask, skip it */
447 if (!gfpflags_allow_blocking(gfp_mask)) {
448 preempt_disable();
449 return 0;
450 }
451
452 /*
453 * Calculate number and height of fully populated subtrees it takes to
454 * store (1 << order) elements.
455 */
456 nr_subtrees = 1 << order;
457 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
458 subtree_height++)
459 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
460
461 /*
462 * The worst case is zero height tree with a single item at index 0 and
463 * then inserting items starting at ULONG_MAX - (1 << order).
464 *
465 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
466 * 0-index item.
467 */
468 nr_nodes = RADIX_TREE_MAX_PATH;
469
470 /* Plus branch to fully populated subtrees. */
471 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
472
473 /* Root node is shared. */
474 nr_nodes--;
475
476 /* Plus nodes required to build subtrees. */
477 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
478
479 return __radix_tree_preload(gfp_mask, nr_nodes);
480}
481
Matthew Wilcox1456a432016-05-20 17:02:08 -0700482static unsigned radix_tree_load_root(struct radix_tree_root *root,
483 struct radix_tree_node **nodep, unsigned long *maxindex)
484{
485 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
486
487 *nodep = node;
488
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700489 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700490 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700491 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700492 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700493 }
494
495 *maxindex = 0;
496 return 0;
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/*
500 * Extend a radix tree so it can store key @index.
501 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700502static int radix_tree_extend(struct radix_tree_root *root,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700503 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800505 struct radix_tree_node *slot;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700506 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int tag;
508
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700509 /* Figure out what the shift should be. */
510 maxshift = shift;
511 while (index > shift_maxindex(maxshift))
512 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700514 slot = root->rnode;
515 if (!slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 do {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700519 struct radix_tree_node *node = radix_tree_node_alloc(root);
520
521 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return -ENOMEM;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800525 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700526 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 tag_set(node, tag, 0);
528 }
529
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700530 BUG_ON(shift > BITS_PER_LONG);
531 node->shift = shift;
Matthew Wilcox0c7fa0a2016-05-20 17:03:07 -0700532 node->offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 node->count = 1;
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800534 node->parent = NULL;
Johannes Weinerf7942432016-12-12 16:43:41 -0800535 if (radix_tree_is_internal_node(slot)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700536 entry_to_node(slot)->parent = node;
Johannes Weinerf7942432016-12-12 16:43:41 -0800537 } else {
538 /* Moving an exceptional root->rnode to a node */
539 if (radix_tree_exceptional_entry(slot))
540 node->exceptional = 1;
541 }
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800542 node->slots[0] = slot;
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -0700543 slot = node_to_entry(node);
544 rcu_assign_pointer(root->rnode, slot);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700545 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700546 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700548 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
551/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800552 * radix_tree_shrink - shrink radix tree to minimum height
553 * @root radix tree root
554 */
Johannes Weiner14b46872016-12-12 16:43:52 -0800555static inline void radix_tree_shrink(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800556 radix_tree_update_node_t update_node,
557 void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800558{
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800559 for (;;) {
560 struct radix_tree_node *node = root->rnode;
561 struct radix_tree_node *child;
562
563 if (!radix_tree_is_internal_node(node))
564 break;
565 node = entry_to_node(node);
566
567 /*
568 * The candidate node has more than one child, or its child
569 * is not at the leftmost slot, or the child is a multiorder
570 * entry, we cannot shrink.
571 */
572 if (node->count != 1)
573 break;
574 child = node->slots[0];
575 if (!child)
576 break;
577 if (!radix_tree_is_internal_node(child) && node->shift)
578 break;
579
580 if (radix_tree_is_internal_node(child))
581 entry_to_node(child)->parent = NULL;
582
583 /*
584 * We don't need rcu_assign_pointer(), since we are simply
585 * moving the node from one part of the tree to another: if it
586 * was safe to dereference the old pointer to it
587 * (node->slots[0]), it will be safe to dereference the new
588 * one (root->rnode) as far as dependent read barriers go.
589 */
590 root->rnode = child;
591
592 /*
593 * We have a dilemma here. The node's slot[0] must not be
594 * NULLed in case there are concurrent lookups expecting to
595 * find the item. However if this was a bottom-level node,
596 * then it may be subject to the slot pointer being visible
597 * to callers dereferencing it. If item corresponding to
598 * slot[0] is subsequently deleted, these callers would expect
599 * their slot to become empty sooner or later.
600 *
601 * For example, lockless pagecache will look up a slot, deref
602 * the page pointer, and if the page has 0 refcount it means it
603 * was concurrently deleted from pagecache so try the deref
604 * again. Fortunately there is already a requirement for logic
605 * to retry the entire slot lookup -- the indirect pointer
606 * problem (replacing direct root node with an indirect pointer
607 * also results in a stale slot). So tag the slot as indirect
608 * to force callers to retry.
609 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800610 node->count = 0;
611 if (!radix_tree_is_internal_node(child)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800612 node->slots[0] = RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800613 if (update_node)
614 update_node(node, private);
615 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800616
617 radix_tree_node_free(node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800618 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800619}
620
Johannes Weiner14b46872016-12-12 16:43:52 -0800621static void delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800622 struct radix_tree_node *node,
623 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800624{
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800625 do {
626 struct radix_tree_node *parent;
627
628 if (node->count) {
629 if (node == entry_to_node(root->rnode))
Johannes Weiner14b46872016-12-12 16:43:52 -0800630 radix_tree_shrink(root, update_node, private);
631 return;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800632 }
633
634 parent = node->parent;
635 if (parent) {
636 parent->slots[node->offset] = NULL;
637 parent->count--;
638 } else {
639 root_tag_clear_all(root);
640 root->rnode = NULL;
641 }
642
643 radix_tree_node_free(node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800644
645 node = parent;
646 } while (node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800647}
648
649/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700650 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 * @root: radix tree root
652 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700653 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700654 * @nodep: returns node
655 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700657 * Create, if necessary, and return the node and slot for an item
658 * at position @index in the radix tree @root.
659 *
660 * Until there is more than one item in the tree, no nodes are
661 * allocated and @root->rnode is used as a direct slot instead of
662 * pointing to a node, in which case *@nodep will be NULL.
663 *
664 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700666int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700667 unsigned order, struct radix_tree_node **nodep,
668 void ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700670 struct radix_tree_node *node = NULL, *child;
671 void **slot = (void **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700672 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700673 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700674 unsigned long max = index | ((1UL << order) - 1);
675
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700676 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800679 if (order > 0 && max == ((1UL << order) - 1))
680 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700681 if (max > maxindex) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700682 int error = radix_tree_extend(root, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700683 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700685 shift = error;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700686 child = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700689 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700690 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700691 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* Have to add a child node. */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700693 child = radix_tree_node_alloc(root);
694 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700696 child->shift = shift;
697 child->offset = offset;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800698 child->count = 0;
699 child->exceptional = 0;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700700 child->parent = node;
701 rcu_assign_pointer(*slot, node_to_entry(child));
702 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700704 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700705 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700708 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700709 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700710 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700711 }
712
Johannes Weiner139e5612014-04-03 14:47:54 -0700713 if (nodep)
714 *nodep = node;
715 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700716 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700717 return 0;
718}
719
Matthew Wilcox175542f2016-12-14 15:08:58 -0800720#ifdef CONFIG_RADIX_TREE_MULTIORDER
721/*
722 * Free any nodes below this node. The tree is presumed to not need
723 * shrinking, and any user data in the tree is presumed to not need a
724 * destructor called on it. If we need to add a destructor, we can
725 * add that functionality later. Note that we may not clear tags or
726 * slots from the tree as an RCU walker may still have a pointer into
727 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
728 * but we'll still have to clear those in rcu_free.
729 */
730static void radix_tree_free_nodes(struct radix_tree_node *node)
731{
732 unsigned offset = 0;
733 struct radix_tree_node *child = entry_to_node(node);
734
735 for (;;) {
736 void *entry = child->slots[offset];
737 if (radix_tree_is_internal_node(entry) &&
738 !is_sibling_entry(child, entry)) {
739 child = entry_to_node(entry);
740 offset = 0;
741 continue;
742 }
743 offset++;
744 while (offset == RADIX_TREE_MAP_SIZE) {
745 struct radix_tree_node *old = child;
746 offset = child->offset + 1;
747 child = child->parent;
748 radix_tree_node_free(old);
749 if (old == entry_to_node(node))
750 return;
751 }
752 }
753}
754
755static inline int insert_entries(struct radix_tree_node *node, void **slot,
756 void *item, unsigned order, bool replace)
757{
758 struct radix_tree_node *child;
759 unsigned i, n, tag, offset, tags = 0;
760
761 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800762 if (order > node->shift)
763 n = 1 << (order - node->shift);
764 else
765 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800766 offset = get_slot_offset(node, slot);
767 } else {
768 n = 1;
769 offset = 0;
770 }
771
772 if (n > 1) {
773 offset = offset & ~(n - 1);
774 slot = &node->slots[offset];
775 }
776 child = node_to_entry(slot);
777
778 for (i = 0; i < n; i++) {
779 if (slot[i]) {
780 if (replace) {
781 node->count--;
782 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
783 if (tag_get(node, tag, offset + i))
784 tags |= 1 << tag;
785 } else
786 return -EEXIST;
787 }
788 }
789
790 for (i = 0; i < n; i++) {
791 struct radix_tree_node *old = slot[i];
792 if (i) {
793 rcu_assign_pointer(slot[i], child);
794 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
795 if (tags & (1 << tag))
796 tag_clear(node, tag, offset + i);
797 } else {
798 rcu_assign_pointer(slot[i], item);
799 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
800 if (tags & (1 << tag))
801 tag_set(node, tag, offset);
802 }
803 if (radix_tree_is_internal_node(old) &&
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800804 !is_sibling_entry(node, old) &&
805 (old != RADIX_TREE_RETRY))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800806 radix_tree_free_nodes(old);
807 if (radix_tree_exceptional_entry(old))
808 node->exceptional--;
809 }
810 if (node) {
811 node->count += n;
812 if (radix_tree_exceptional_entry(item))
813 node->exceptional += n;
814 }
815 return n;
816}
817#else
818static inline int insert_entries(struct radix_tree_node *node, void **slot,
819 void *item, unsigned order, bool replace)
820{
821 if (*slot)
822 return -EEXIST;
823 rcu_assign_pointer(*slot, item);
824 if (node) {
825 node->count++;
826 if (radix_tree_exceptional_entry(item))
827 node->exceptional++;
828 }
829 return 1;
830}
831#endif
832
Johannes Weiner139e5612014-04-03 14:47:54 -0700833/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700834 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700835 * @root: radix tree root
836 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700837 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700838 * @item: item to insert
839 *
840 * Insert an item into the radix tree at position @index.
841 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700842int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
843 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700844{
845 struct radix_tree_node *node;
846 void **slot;
847 int error;
848
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700849 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700850
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700851 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700852 if (error)
853 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800854
855 error = insert_entries(node, slot, item, order, false);
856 if (error < 0)
857 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700858
Nick Piggin612d6c12006-06-23 02:03:22 -0700859 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700860 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700861 BUG_ON(tag_get(node, 0, offset));
862 BUG_ON(tag_get(node, 1, offset));
863 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700864 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700865 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700870EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Johannes Weiner139e5612014-04-03 14:47:54 -0700872/**
873 * __radix_tree_lookup - lookup an item in a radix tree
874 * @root: radix tree root
875 * @index: index key
876 * @nodep: returns node
877 * @slotp: returns slot
878 *
879 * Lookup and return the item at position @index in the radix
880 * tree @root.
881 *
882 * Until there is more than one item in the tree, no nodes are
883 * allocated and @root->rnode is used as a direct slot instead of
884 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800885 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700886void *__radix_tree_lookup(struct radix_tree_root *root, unsigned long index,
887 struct radix_tree_node **nodep, void ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800888{
Johannes Weiner139e5612014-04-03 14:47:54 -0700889 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700890 unsigned long maxindex;
Johannes Weiner139e5612014-04-03 14:47:54 -0700891 void **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800892
Matthew Wilcox85829952016-05-20 17:02:20 -0700893 restart:
894 parent = NULL;
895 slot = (void **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700896 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700897 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800898 return NULL;
899
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700900 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700901 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700902
Matthew Wilcox85829952016-05-20 17:02:20 -0700903 if (node == RADIX_TREE_RETRY)
904 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700905 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700906 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700907 slot = parent->slots + offset;
908 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800909
Johannes Weiner139e5612014-04-03 14:47:54 -0700910 if (nodep)
911 *nodep = parent;
912 if (slotp)
913 *slotp = slot;
914 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700915}
916
917/**
918 * radix_tree_lookup_slot - lookup a slot in a radix tree
919 * @root: radix tree root
920 * @index: index key
921 *
922 * Returns: the slot corresponding to the position @index in the
923 * radix tree @root. This is useful for update-if-exists operations.
924 *
925 * This function can be called under rcu_read_lock iff the slot is not
926 * modified by radix_tree_replace_slot, otherwise it must be called
927 * exclusive from other writers. Any dereference of the slot must be done
928 * using radix_tree_deref_slot.
929 */
930void **radix_tree_lookup_slot(struct radix_tree_root *root, unsigned long index)
931{
Johannes Weiner139e5612014-04-03 14:47:54 -0700932 void **slot;
933
934 if (!__radix_tree_lookup(root, index, NULL, &slot))
935 return NULL;
936 return slot;
Hans Reisera4331362005-11-07 00:59:29 -0800937}
938EXPORT_SYMBOL(radix_tree_lookup_slot);
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/**
941 * radix_tree_lookup - perform lookup operation on a radix tree
942 * @root: radix tree root
943 * @index: index key
944 *
945 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800946 *
947 * This function can be called under rcu_read_lock, however the caller
948 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
949 * them safely). No RCU barriers are required to access or modify the
950 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
952void *radix_tree_lookup(struct radix_tree_root *root, unsigned long index)
953{
Johannes Weiner139e5612014-04-03 14:47:54 -0700954 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956EXPORT_SYMBOL(radix_tree_lookup);
957
Johannes Weiner6d75f362016-12-12 16:43:43 -0800958static void replace_slot(struct radix_tree_root *root,
959 struct radix_tree_node *node,
960 void **slot, void *item,
961 bool warn_typeswitch)
962{
963 void *old = rcu_dereference_raw(*slot);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800964 int count, exceptional;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800965
966 WARN_ON_ONCE(radix_tree_is_internal_node(item));
Johannes Weiner6d75f362016-12-12 16:43:43 -0800967
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800968 count = !!item - !!old;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800969 exceptional = !!radix_tree_exceptional_entry(item) -
970 !!radix_tree_exceptional_entry(old);
971
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800972 WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
Johannes Weiner6d75f362016-12-12 16:43:43 -0800973
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800974 if (node) {
975 node->count += count;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800976 node->exceptional += exceptional;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800977 }
Johannes Weiner6d75f362016-12-12 16:43:43 -0800978
979 rcu_assign_pointer(*slot, item);
980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/**
Johannes Weinerf7942432016-12-12 16:43:41 -0800983 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -0800984 * @root: radix tree root
985 * @node: pointer to tree node
986 * @slot: pointer to slot in @node
987 * @item: new item to store in the slot.
988 * @update_node: callback for changing leaf nodes
989 * @private: private data to pass to @update_node
Johannes Weinerf7942432016-12-12 16:43:41 -0800990 *
991 * For use with __radix_tree_lookup(). Caller must hold tree write locked
992 * across slot lookup and replacement.
993 */
994void __radix_tree_replace(struct radix_tree_root *root,
995 struct radix_tree_node *node,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800996 void **slot, void *item,
997 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf7942432016-12-12 16:43:41 -0800998{
Johannes Weiner6d75f362016-12-12 16:43:43 -0800999 /*
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001000 * This function supports replacing exceptional entries and
1001 * deleting entries, but that needs accounting against the
1002 * node unless the slot is root->rnode.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001003 */
1004 replace_slot(root, node, slot, item,
1005 !node && slot != (void **)&root->rnode);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001006
Johannes Weiner4d693d02016-12-12 16:43:49 -08001007 if (!node)
1008 return;
1009
1010 if (update_node)
1011 update_node(node, private);
1012
1013 delete_node(root, node, update_node, private);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001014}
Johannes Weinerf7942432016-12-12 16:43:41 -08001015
Johannes Weiner6d75f362016-12-12 16:43:43 -08001016/**
1017 * radix_tree_replace_slot - replace item in a slot
1018 * @root: radix tree root
1019 * @slot: pointer to slot
1020 * @item: new item to store in the slot.
1021 *
1022 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1023 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1024 * across slot lookup and replacement.
1025 *
1026 * NOTE: This cannot be used to switch between non-entries (empty slots),
1027 * regular entries, and exceptional entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001028 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001029 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1030 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001031 */
1032void radix_tree_replace_slot(struct radix_tree_root *root,
1033 void **slot, void *item)
1034{
1035 replace_slot(root, NULL, slot, item, true);
Johannes Weinerf7942432016-12-12 16:43:41 -08001036}
1037
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001038/**
1039 * radix_tree_iter_replace - replace item in a slot
1040 * @root: radix tree root
1041 * @slot: pointer to slot
1042 * @item: new item to store in the slot.
1043 *
1044 * For use with radix_tree_split() and radix_tree_for_each_slot().
1045 * Caller must hold tree write locked across split and replacement.
1046 */
1047void radix_tree_iter_replace(struct radix_tree_root *root,
1048 const struct radix_tree_iter *iter, void **slot, void *item)
1049{
1050 __radix_tree_replace(root, iter->node, slot, item, NULL, NULL);
1051}
1052
Matthew Wilcox175542f2016-12-14 15:08:58 -08001053#ifdef CONFIG_RADIX_TREE_MULTIORDER
1054/**
1055 * radix_tree_join - replace multiple entries with one multiorder entry
1056 * @root: radix tree root
1057 * @index: an index inside the new entry
1058 * @order: order of the new entry
1059 * @item: new entry
1060 *
1061 * Call this function to replace several entries with one larger entry.
1062 * The existing entries are presumed to not need freeing as a result of
1063 * this call.
1064 *
1065 * The replacement entry will have all the tags set on it that were set
1066 * on any of the entries it is replacing.
1067 */
1068int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1069 unsigned order, void *item)
1070{
1071 struct radix_tree_node *node;
1072 void **slot;
1073 int error;
1074
1075 BUG_ON(radix_tree_is_internal_node(item));
1076
1077 error = __radix_tree_create(root, index, order, &node, &slot);
1078 if (!error)
1079 error = insert_entries(node, slot, item, order, true);
1080 if (error > 0)
1081 error = 0;
1082
1083 return error;
1084}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001085
1086/**
1087 * radix_tree_split - Split an entry into smaller entries
1088 * @root: radix tree root
1089 * @index: An index within the large entry
1090 * @order: Order of new entries
1091 *
1092 * Call this function as the first step in replacing a multiorder entry
1093 * with several entries of lower order. After this function returns,
1094 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1095 * and call radix_tree_iter_replace() to set up each new entry.
1096 *
1097 * The tags from this entry are replicated to all the new entries.
1098 *
1099 * The radix tree should be locked against modification during the entire
1100 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1101 * should prompt RCU walkers to restart the lookup from the root.
1102 */
1103int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1104 unsigned order)
1105{
1106 struct radix_tree_node *parent, *node, *child;
1107 void **slot;
1108 unsigned int offset, end;
1109 unsigned n, tag, tags = 0;
1110
1111 if (!__radix_tree_lookup(root, index, &parent, &slot))
1112 return -ENOENT;
1113 if (!parent)
1114 return -ENOENT;
1115
1116 offset = get_slot_offset(parent, slot);
1117
1118 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1119 if (tag_get(parent, tag, offset))
1120 tags |= 1 << tag;
1121
1122 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
1123 if (!is_sibling_entry(parent, parent->slots[end]))
1124 break;
1125 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1126 if (tags & (1 << tag))
1127 tag_set(parent, tag, end);
1128 /* rcu_assign_pointer ensures tags are set before RETRY */
1129 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1130 }
1131 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
1132 parent->exceptional -= (end - offset);
1133
1134 if (order == parent->shift)
1135 return 0;
1136 if (order > parent->shift) {
1137 while (offset < end)
1138 offset += insert_entries(parent, &parent->slots[offset],
1139 RADIX_TREE_RETRY, order, true);
1140 return 0;
1141 }
1142
1143 node = parent;
1144
1145 for (;;) {
1146 if (node->shift > order) {
1147 child = radix_tree_node_alloc(root);
1148 if (!child)
1149 goto nomem;
1150 child->shift = node->shift - RADIX_TREE_MAP_SHIFT;
1151 child->offset = offset;
1152 child->count = 0;
1153 child->parent = node;
1154 if (node != parent) {
1155 node->count++;
1156 node->slots[offset] = node_to_entry(child);
1157 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1158 if (tags & (1 << tag))
1159 tag_set(node, tag, offset);
1160 }
1161
1162 node = child;
1163 offset = 0;
1164 continue;
1165 }
1166
1167 n = insert_entries(node, &node->slots[offset],
1168 RADIX_TREE_RETRY, order, false);
1169 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1170
1171 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1172 if (tags & (1 << tag))
1173 tag_set(node, tag, offset);
1174 offset += n;
1175
1176 while (offset == RADIX_TREE_MAP_SIZE) {
1177 if (node == parent)
1178 break;
1179 offset = node->offset;
1180 child = node;
1181 node = node->parent;
1182 rcu_assign_pointer(node->slots[offset],
1183 node_to_entry(child));
1184 offset++;
1185 }
1186 if ((node == parent) && (offset == end))
1187 return 0;
1188 }
1189
1190 nomem:
1191 /* Shouldn't happen; did user forget to preload? */
1192 /* TODO: free all the allocated nodes */
1193 WARN_ON(1);
1194 return -ENOMEM;
1195}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001196#endif
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198/**
1199 * radix_tree_tag_set - set a tag on a radix tree node
1200 * @root: radix tree root
1201 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001202 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001204 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1205 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * the root all the way down to the leaf node.
1207 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001208 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 * item is a bug.
1210 */
1211void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001212 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Ross Zwislerfb969902016-05-20 17:02:32 -07001214 struct radix_tree_node *node, *parent;
1215 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001217 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001218 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001220 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001221 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001223 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001224 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001225 BUG_ON(!node);
1226
1227 if (!tag_get(parent, tag, offset))
1228 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
Nick Piggin612d6c12006-06-23 02:03:22 -07001231 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001232 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001233 root_tag_set(root, tag);
1234
Ross Zwislerfb969902016-05-20 17:02:32 -07001235 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236}
1237EXPORT_SYMBOL(radix_tree_tag_set);
1238
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001239static void node_tag_clear(struct radix_tree_root *root,
1240 struct radix_tree_node *node,
1241 unsigned int tag, unsigned int offset)
1242{
1243 while (node) {
1244 if (!tag_get(node, tag, offset))
1245 return;
1246 tag_clear(node, tag, offset);
1247 if (any_tag_set(node, tag))
1248 return;
1249
1250 offset = node->offset;
1251 node = node->parent;
1252 }
1253
1254 /* clear the root's tag bit */
1255 if (root_tag_get(root, tag))
1256 root_tag_clear(root, tag);
1257}
1258
Matthew Wilcox9498d2b2016-12-14 15:08:37 -08001259static void node_tag_set(struct radix_tree_root *root,
1260 struct radix_tree_node *node,
1261 unsigned int tag, unsigned int offset)
1262{
1263 while (node) {
1264 if (tag_get(node, tag, offset))
1265 return;
1266 tag_set(node, tag, offset);
1267 offset = node->offset;
1268 node = node->parent;
1269 }
1270
1271 if (!root_tag_get(root, tag))
1272 root_tag_set(root, tag);
1273}
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275/**
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001276 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1277 * @root: radix tree root
1278 * @iter: iterator state
1279 * @tag: tag to set
1280 */
1281void radix_tree_iter_tag_set(struct radix_tree_root *root,
1282 const struct radix_tree_iter *iter, unsigned int tag)
1283{
1284 node_tag_set(root, iter->node, tag, iter_offset(iter));
1285}
1286
1287/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * radix_tree_tag_clear - clear a tag on a radix tree node
1289 * @root: radix tree root
1290 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001291 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001293 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001294 * corresponding to @index in the radix tree. If this causes
1295 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * next-to-leaf node, etc.
1297 *
1298 * Returns the address of the tagged item on success, else NULL. ie:
1299 * has the same return value and semantics as radix_tree_lookup().
1300 */
1301void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001302 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001304 struct radix_tree_node *node, *parent;
1305 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001306 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001308 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001309 if (index > maxindex)
1310 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Ross Zwisler00f47b52016-05-20 17:02:35 -07001312 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001314 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001315 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001316 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001319 if (node)
1320 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Ross Zwisler00f47b52016-05-20 17:02:35 -07001322 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324EXPORT_SYMBOL(radix_tree_tag_clear);
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001327 * radix_tree_tag_get - get a tag on a radix tree node
1328 * @root: radix tree root
1329 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001330 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001332 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001334 * 0: tag not present or not set
1335 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001336 *
1337 * Note that the return value of this function may not be relied on, even if
1338 * the RCU lock is held, unless tag modification and node deletion are excluded
1339 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
1341int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001342 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001344 struct radix_tree_node *node, *parent;
1345 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
Nick Piggin612d6c12006-06-23 02:03:22 -07001347 if (!root_tag_get(root, tag))
1348 return 0;
1349
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001350 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001351 if (index > maxindex)
1352 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001353 if (node == NULL)
1354 return 0;
1355
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001356 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001357 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001358
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001359 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001360 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001361
1362 if (!node)
1363 return 0;
1364 if (!tag_get(parent, tag, offset))
1365 return 0;
1366 if (node == RADIX_TREE_RETRY)
1367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001369
1370 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
1372EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Ross Zwisler21ef5332016-05-20 17:02:26 -07001374static inline void __set_iter_shift(struct radix_tree_iter *iter,
1375 unsigned int shift)
1376{
1377#ifdef CONFIG_RADIX_TREE_MULTIORDER
1378 iter->shift = shift;
1379#endif
1380}
1381
Matthew Wilcox148deab2016-12-14 15:08:49 -08001382/* Construct iter->tags bit-mask from node->tags[tag] array */
1383static void set_iter_tags(struct radix_tree_iter *iter,
1384 struct radix_tree_node *node, unsigned offset,
1385 unsigned tag)
1386{
1387 unsigned tag_long = offset / BITS_PER_LONG;
1388 unsigned tag_bit = offset % BITS_PER_LONG;
1389
1390 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1391
1392 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1393 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1394 /* Pick tags from next element */
1395 if (tag_bit)
1396 iter->tags |= node->tags[tag][tag_long + 1] <<
1397 (BITS_PER_LONG - tag_bit);
1398 /* Clip chunk size, here only BITS_PER_LONG tags */
1399 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1400 }
1401}
1402
1403#ifdef CONFIG_RADIX_TREE_MULTIORDER
1404static void **skip_siblings(struct radix_tree_node **nodep,
1405 void **slot, struct radix_tree_iter *iter)
1406{
1407 void *sib = node_to_entry(slot - 1);
1408
1409 while (iter->index < iter->next_index) {
1410 *nodep = rcu_dereference_raw(*slot);
1411 if (*nodep && *nodep != sib)
1412 return slot;
1413 slot++;
1414 iter->index = __radix_tree_iter_add(iter, 1);
1415 iter->tags >>= 1;
1416 }
1417
1418 *nodep = NULL;
1419 return NULL;
1420}
1421
1422void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
1423 unsigned flags)
1424{
1425 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1426 struct radix_tree_node *node = rcu_dereference_raw(*slot);
1427
1428 slot = skip_siblings(&node, slot, iter);
1429
1430 while (radix_tree_is_internal_node(node)) {
1431 unsigned offset;
1432 unsigned long next_index;
1433
1434 if (node == RADIX_TREE_RETRY)
1435 return slot;
1436 node = entry_to_node(node);
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001437 iter->node = node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001438 iter->shift = node->shift;
1439
1440 if (flags & RADIX_TREE_ITER_TAGGED) {
1441 offset = radix_tree_find_next_bit(node, tag, 0);
1442 if (offset == RADIX_TREE_MAP_SIZE)
1443 return NULL;
1444 slot = &node->slots[offset];
1445 iter->index = __radix_tree_iter_add(iter, offset);
1446 set_iter_tags(iter, node, offset, tag);
1447 node = rcu_dereference_raw(*slot);
1448 } else {
1449 offset = 0;
1450 slot = &node->slots[0];
1451 for (;;) {
1452 node = rcu_dereference_raw(*slot);
1453 if (node)
1454 break;
1455 slot++;
1456 offset++;
1457 if (offset == RADIX_TREE_MAP_SIZE)
1458 return NULL;
1459 }
1460 iter->index = __radix_tree_iter_add(iter, offset);
1461 }
1462 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1463 goto none;
1464 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1465 if (next_index < iter->next_index)
1466 iter->next_index = next_index;
1467 }
1468
1469 return slot;
1470 none:
1471 iter->next_index = 0;
1472 return NULL;
1473}
1474EXPORT_SYMBOL(__radix_tree_next_slot);
1475#else
1476static void **skip_siblings(struct radix_tree_node **nodep,
1477 void **slot, struct radix_tree_iter *iter)
1478{
1479 return slot;
1480}
1481#endif
1482
1483void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
1484{
1485 struct radix_tree_node *node;
1486
1487 slot++;
1488 iter->index = __radix_tree_iter_add(iter, 1);
1489 node = rcu_dereference_raw(*slot);
1490 skip_siblings(&node, slot, iter);
1491 iter->next_index = iter->index;
1492 iter->tags = 0;
1493 return NULL;
1494}
1495EXPORT_SYMBOL(radix_tree_iter_resume);
1496
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001497/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001498 * radix_tree_next_chunk - find next chunk of slots for iteration
1499 *
1500 * @root: radix tree root
1501 * @iter: iterator state
1502 * @flags: RADIX_TREE_ITER_* flags and tag index
1503 * Returns: pointer to chunk first slot, or NULL if iteration is over
1504 */
1505void **radix_tree_next_chunk(struct radix_tree_root *root,
1506 struct radix_tree_iter *iter, unsigned flags)
1507{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001508 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001509 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001510 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001511
1512 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1513 return NULL;
1514
1515 /*
1516 * Catch next_index overflow after ~0UL. iter->index never overflows
1517 * during iterating; it can be zero only at the beginning.
1518 * And we cannot overflow iter->next_index in a single step,
1519 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001520 *
1521 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001522 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001523 */
1524 index = iter->next_index;
1525 if (!index && iter->index)
1526 return NULL;
1527
Ross Zwisler21ef5332016-05-20 17:02:26 -07001528 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001529 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001530 if (index > maxindex)
1531 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001532 if (!child)
1533 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001534
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001535 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001536 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001537 iter->index = index;
1538 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001539 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001540 iter->node = NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001541 __set_iter_shift(iter, 0);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001542 return (void **)&root->rnode;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001543 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001544
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001545 do {
1546 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001547 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001548
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001549 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001550 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001551 /* Hole detected */
1552 if (flags & RADIX_TREE_ITER_CONTIG)
1553 return NULL;
1554
1555 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001556 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001557 offset + 1);
1558 else
1559 while (++offset < RADIX_TREE_MAP_SIZE) {
Ross Zwisler21ef5332016-05-20 17:02:26 -07001560 void *slot = node->slots[offset];
1561 if (is_sibling_entry(node, slot))
1562 continue;
1563 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001564 break;
1565 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001566 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001567 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001568 /* Overflow after ~0UL */
1569 if (!index)
1570 return NULL;
1571 if (offset == RADIX_TREE_MAP_SIZE)
1572 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001573 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001574 }
1575
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001576 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001577 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001578 if (child == RADIX_TREE_RETRY)
1579 break;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001580 } while (radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001581
1582 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001583 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1584 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001585 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001586 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001587
Matthew Wilcox148deab2016-12-14 15:08:49 -08001588 if (flags & RADIX_TREE_ITER_TAGGED)
1589 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001590
1591 return node->slots + offset;
1592}
1593EXPORT_SYMBOL(radix_tree_next_chunk);
1594
1595/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1597 * @root: radix tree root
1598 * @results: where the results of the lookup are placed
1599 * @first_index: start the lookup from this key
1600 * @max_items: place up to this many items at *results
1601 *
1602 * Performs an index-ascending scan of the tree for present items. Places
1603 * them at *@results and returns the number of items which were placed at
1604 * *@results.
1605 *
1606 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001607 *
1608 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1609 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001610 * an atomic snapshot of the tree at a single point in time, the
1611 * semantics of an RCU protected gang lookup are as though multiple
1612 * radix_tree_lookups have been issued in individual locks, and results
1613 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615unsigned int
1616radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
1617 unsigned long first_index, unsigned int max_items)
1618{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001619 struct radix_tree_iter iter;
1620 void **slot;
1621 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001623 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001624 return 0;
1625
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001626 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001627 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001628 if (!results[ret])
1629 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001630 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001631 slot = radix_tree_iter_retry(&iter);
1632 continue;
1633 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001634 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 return ret;
1639}
1640EXPORT_SYMBOL(radix_tree_gang_lookup);
1641
Nick Piggin47feff22008-07-25 19:45:29 -07001642/**
1643 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1644 * @root: radix tree root
1645 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001646 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001647 * @first_index: start the lookup from this key
1648 * @max_items: place up to this many items at *results
1649 *
1650 * Performs an index-ascending scan of the tree for present items. Places
1651 * their slots at *@results and returns the number of items which were
1652 * placed at *@results.
1653 *
1654 * The implementation is naive.
1655 *
1656 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1657 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1658 * protection, radix_tree_deref_slot may fail requiring a retry.
1659 */
1660unsigned int
Hugh Dickins63286502011-08-03 16:21:18 -07001661radix_tree_gang_lookup_slot(struct radix_tree_root *root,
1662 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001663 unsigned long first_index, unsigned int max_items)
1664{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001665 struct radix_tree_iter iter;
1666 void **slot;
1667 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001668
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001669 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001670 return 0;
1671
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001672 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1673 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001674 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001675 indices[ret] = iter.index;
1676 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001677 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001678 }
1679
1680 return ret;
1681}
1682EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684/**
1685 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1686 * based on a tag
1687 * @root: radix tree root
1688 * @results: where the results of the lookup are placed
1689 * @first_index: start the lookup from this key
1690 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001691 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 *
1693 * Performs an index-ascending scan of the tree for present items which
1694 * have the tag indexed by @tag set. Places the items at *@results and
1695 * returns the number of items which were placed at *@results.
1696 */
1697unsigned int
1698radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001699 unsigned long first_index, unsigned int max_items,
1700 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001702 struct radix_tree_iter iter;
1703 void **slot;
1704 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001706 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001707 return 0;
1708
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001709 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001710 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001711 if (!results[ret])
1712 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001713 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001714 slot = radix_tree_iter_retry(&iter);
1715 continue;
1716 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001717 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return ret;
1722}
1723EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1724
1725/**
Nick Piggin47feff22008-07-25 19:45:29 -07001726 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1727 * radix tree based on a tag
1728 * @root: radix tree root
1729 * @results: where the results of the lookup are placed
1730 * @first_index: start the lookup from this key
1731 * @max_items: place up to this many items at *results
1732 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1733 *
1734 * Performs an index-ascending scan of the tree for present items which
1735 * have the tag indexed by @tag set. Places the slots at *@results and
1736 * returns the number of slots which were placed at *@results.
1737 */
1738unsigned int
1739radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
1740 unsigned long first_index, unsigned int max_items,
1741 unsigned int tag)
1742{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001743 struct radix_tree_iter iter;
1744 void **slot;
1745 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001746
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001747 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001748 return 0;
1749
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001750 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1751 results[ret] = slot;
1752 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001753 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001754 }
1755
1756 return ret;
1757}
1758EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1759
Nick Piggin47feff22008-07-25 19:45:29 -07001760/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001761 * __radix_tree_delete_node - try to free node after clearing a slot
1762 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001763 * @node: node containing @index
1764 *
1765 * After clearing the slot at @index in @node from radix tree
1766 * rooted at @root, call this function to attempt freeing the
1767 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001768 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001769void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weiner139e5612014-04-03 14:47:54 -07001770 struct radix_tree_node *node)
1771{
Johannes Weiner14b46872016-12-12 16:43:52 -08001772 delete_node(root, node, NULL, NULL);
Johannes Weiner139e5612014-04-03 14:47:54 -07001773}
1774
Matthew Wilcox57578c22016-05-20 17:01:54 -07001775static inline void delete_sibling_entries(struct radix_tree_node *node,
1776 void *ptr, unsigned offset)
1777{
1778#ifdef CONFIG_RADIX_TREE_MULTIORDER
1779 int i;
1780 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1781 if (node->slots[offset + i] != ptr)
1782 break;
1783 node->slots[offset + i] = NULL;
1784 node->count--;
1785 }
1786#endif
1787}
1788
Johannes Weiner139e5612014-04-03 14:47:54 -07001789/**
Johannes Weiner53c59f22014-04-03 14:47:39 -07001790 * radix_tree_delete_item - delete an item from a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * @root: radix tree root
1792 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001793 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001795 * Remove @item at @index from the radix tree rooted at @root.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001797 * Returns the address of the deleted item, or NULL if it was not present
1798 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001800void *radix_tree_delete_item(struct radix_tree_root *root,
1801 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Johannes Weiner139e5612014-04-03 14:47:54 -07001803 struct radix_tree_node *node;
Matthew Wilcox57578c22016-05-20 17:01:54 -07001804 unsigned int offset;
Johannes Weiner139e5612014-04-03 14:47:54 -07001805 void **slot;
1806 void *entry;
Nick Piggind5274262006-01-08 01:01:41 -08001807 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Johannes Weiner139e5612014-04-03 14:47:54 -07001809 entry = __radix_tree_lookup(root, index, &node, &slot);
1810 if (!entry)
1811 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812
Johannes Weiner139e5612014-04-03 14:47:54 -07001813 if (item && entry != item)
1814 return NULL;
1815
1816 if (!node) {
Nick Piggin612d6c12006-06-23 02:03:22 -07001817 root_tag_clear_all(root);
1818 root->rnode = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001819 return entry;
Nick Piggin612d6c12006-06-23 02:03:22 -07001820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Matthew Wilcox29e09672016-05-20 17:02:02 -07001822 offset = get_slot_offset(node, slot);
Johannes Weiner53c59f22014-04-03 14:47:39 -07001823
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001824 /* Clear all tags associated with the item to be deleted. */
1825 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1826 node_tag_clear(root, node, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -07001828 delete_sibling_entries(node, node_to_entry(slot), offset);
Johannes Weiner4d693d02016-12-12 16:43:49 -08001829 __radix_tree_replace(root, node, slot, NULL, NULL, NULL);
Christoph Lameter201b6262005-09-06 15:16:46 -07001830
Johannes Weiner139e5612014-04-03 14:47:54 -07001831 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001833EXPORT_SYMBOL(radix_tree_delete_item);
1834
1835/**
1836 * radix_tree_delete - delete an item from a radix tree
1837 * @root: radix tree root
1838 * @index: index key
1839 *
1840 * Remove the item at @index from the radix tree rooted at @root.
1841 *
1842 * Returns the address of the deleted item, or NULL if it was not present.
1843 */
1844void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1845{
1846 return radix_tree_delete_item(root, index, NULL);
1847}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848EXPORT_SYMBOL(radix_tree_delete);
1849
Johannes Weinerd3798ae2016-10-04 22:02:08 +02001850void radix_tree_clear_tags(struct radix_tree_root *root,
1851 struct radix_tree_node *node,
1852 void **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001853{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001854 if (node) {
1855 unsigned int tag, offset = get_slot_offset(node, slot);
1856 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1857 node_tag_clear(root, node, tag, offset);
1858 } else {
1859 /* Clear root node tags */
1860 root->gfp_mask &= __GFP_BITS_MASK;
1861 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001862}
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864/**
1865 * radix_tree_tagged - test whether any items in the tree are tagged
1866 * @root: radix tree root
1867 * @tag: tag to test
1868 */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001869int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870{
Nick Piggin612d6c12006-06-23 02:03:22 -07001871 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873EXPORT_SYMBOL(radix_tree_tagged);
1874
1875static void
Johannes Weiner449dd692014-04-03 14:47:56 -07001876radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Johannes Weiner449dd692014-04-03 14:47:56 -07001878 struct radix_tree_node *node = arg;
1879
1880 memset(node, 0, sizeof(*node));
1881 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
1883
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001884static __init unsigned long __maxindex(unsigned int height)
1885{
1886 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1887 int shift = RADIX_TREE_INDEX_BITS - width;
1888
1889 if (shift < 0)
1890 return ~0UL;
1891 if (shift >= BITS_PER_LONG)
1892 return 0UL;
1893 return ~0UL >> shift;
1894}
1895
1896static __init void radix_tree_init_maxnodes(void)
1897{
1898 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1899 unsigned int i, j;
1900
1901 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1902 height_to_maxindex[i] = __maxindex(i);
1903 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1904 for (j = i; j > 0; j--)
1905 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1906 }
1907}
1908
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001909static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001911 struct radix_tree_preload *rtp;
1912 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001914 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001915 rtp = &per_cpu(radix_tree_preloads, cpu);
1916 while (rtp->nr) {
1917 node = rtp->nodes;
1918 rtp->nodes = node->private_data;
1919 kmem_cache_free(radix_tree_node_cachep, node);
1920 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001921 }
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001922 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925void __init radix_tree_init(void)
1926{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001927 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1929 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07001930 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1931 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001932 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001933 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
1934 NULL, radix_tree_cpu_dead);
1935 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}