blob: eaea14b8f2ca9ae461e9d63a9bf040ecfa52fb1b [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 Wilcox0a835c42016-12-20 10:27:56 -050025#include <linux/bitmap.h>
26#include <linux/bitops.h>
Matthew Wilcoxe157b552016-12-14 15:09:01 -080027#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -050029#include <linux/export.h>
30#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/init.h>
32#include <linux/kernel.h>
Catalin Marinasce80b062014-06-06 14:38:18 -070033#include <linux/kmemleak.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -050034#include <linux/percpu.h>
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020035#include <linux/preempt.h> /* in_interrupt() */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050036#include <linux/radix-tree.h>
37#include <linux/rcupdate.h>
38#include <linux/slab.h>
39#include <linux/string.h>
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/*
Matthew Wilcox0a835c42016-12-20 10:27:56 -050064 * The IDR does not have to be as high as the radix tree since it uses
65 * signed integers, not unsigned longs.
66 */
67#define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1)
68#define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \
69 RADIX_TREE_MAP_SHIFT))
70#define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1)
71
72/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 * Per-cpu pool of preloaded nodes
74 */
75struct radix_tree_preload {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -070076 unsigned nr;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -070077 /* nodes->private_data points to next preallocated node */
78 struct radix_tree_node *nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080080static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Matthew Wilcox148deab2016-12-14 15:08:49 -080082static inline struct radix_tree_node *entry_to_node(void *ptr)
83{
84 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
85}
86
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070087static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080088{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070089 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -080090}
91
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070092#define RADIX_TREE_RETRY node_to_entry(NULL)
Matthew Wilcoxafe0e392016-05-20 17:02:17 -070093
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070094#ifdef CONFIG_RADIX_TREE_MULTIORDER
95/* Sibling slots point directly to another slot in the same node */
Matthew Wilcox35534c82016-12-19 17:43:19 -050096static inline
97bool is_sibling_entry(const struct radix_tree_node *parent, void *node)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070098{
99 void **ptr = node;
100 return (parent->slots <= ptr) &&
101 (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
102}
103#else
Matthew Wilcox35534c82016-12-19 17:43:19 -0500104static inline
105bool is_sibling_entry(const struct radix_tree_node *parent, void *node)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700106{
107 return false;
108}
109#endif
110
Matthew Wilcox35534c82016-12-19 17:43:19 -0500111static inline
112unsigned long get_slot_offset(const struct radix_tree_node *parent, void **slot)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700113{
114 return slot - parent->slots;
115}
116
Matthew Wilcox35534c82016-12-19 17:43:19 -0500117static unsigned int radix_tree_descend(const struct radix_tree_node *parent,
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700118 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700119{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700120 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700121 void **entry = rcu_dereference_raw(parent->slots[offset]);
122
123#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700124 if (radix_tree_is_internal_node(entry)) {
Linus Torvalds8d2c0d32016-09-25 13:32:46 -0700125 if (is_sibling_entry(parent, entry)) {
126 void **sibentry = (void **) entry_to_node(entry);
127 offset = get_slot_offset(parent, sibentry);
128 entry = rcu_dereference_raw(*sibentry);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700129 }
130 }
131#endif
132
133 *nodep = (void *)entry;
134 return offset;
135}
136
Matthew Wilcox35534c82016-12-19 17:43:19 -0500137static inline gfp_t root_gfp_mask(const struct radix_tree_root *root)
Nick Piggin612d6c12006-06-23 02:03:22 -0700138{
139 return root->gfp_mask & __GFP_BITS_MASK;
140}
141
Nick Piggin643b52b2008-06-12 15:21:52 -0700142static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
143 int offset)
144{
145 __set_bit(offset, node->tags[tag]);
146}
147
148static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
149 int offset)
150{
151 __clear_bit(offset, node->tags[tag]);
152}
153
Matthew Wilcox35534c82016-12-19 17:43:19 -0500154static inline int tag_get(const struct radix_tree_node *node, unsigned int tag,
Nick Piggin643b52b2008-06-12 15:21:52 -0700155 int offset)
156{
157 return test_bit(offset, node->tags[tag]);
158}
159
Matthew Wilcox35534c82016-12-19 17:43:19 -0500160static inline void root_tag_set(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700161{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500162 root->gfp_mask |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700163}
164
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700165static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700166{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500167 root->gfp_mask &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700168}
169
170static inline void root_tag_clear_all(struct radix_tree_root *root)
171{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500172 root->gfp_mask &= (1 << ROOT_TAG_SHIFT) - 1;
Nick Piggin643b52b2008-06-12 15:21:52 -0700173}
174
Matthew Wilcox35534c82016-12-19 17:43:19 -0500175static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700176{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500177 return (__force int)root->gfp_mask & (1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700178}
179
Matthew Wilcox35534c82016-12-19 17:43:19 -0500180static inline unsigned root_tags_get(const struct radix_tree_root *root)
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700181{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500182 return (__force unsigned)root->gfp_mask >> ROOT_TAG_SHIFT;
183}
184
185static inline bool is_idr(const struct radix_tree_root *root)
186{
187 return !!(root->gfp_mask & ROOT_IS_IDR);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700188}
189
Nick Piggin643b52b2008-06-12 15:21:52 -0700190/*
191 * Returns 1 if any slot in the node has this tag set.
192 * Otherwise returns 0.
193 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500194static inline int any_tag_set(const struct radix_tree_node *node,
195 unsigned int tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700196{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700197 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700198 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
199 if (node->tags[tag][idx])
200 return 1;
201 }
202 return 0;
203}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700204
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500205static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag)
206{
207 bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE);
208}
209
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700210/**
211 * radix_tree_find_next_bit - find the next set bit in a memory region
212 *
213 * @addr: The address to base the search on
214 * @size: The bitmap size in bits
215 * @offset: The bitnumber to start searching at
216 *
217 * Unrollable variant of find_next_bit() for constant size arrays.
218 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
219 * Returns next bit offset, or size if nothing found.
220 */
221static __always_inline unsigned long
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800222radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
223 unsigned long offset)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700224{
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800225 const unsigned long *addr = node->tags[tag];
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700226
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800227 if (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700228 unsigned long tmp;
229
230 addr += offset / BITS_PER_LONG;
231 tmp = *addr >> (offset % BITS_PER_LONG);
232 if (tmp)
233 return __ffs(tmp) + offset;
234 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800235 while (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700236 tmp = *++addr;
237 if (tmp)
238 return __ffs(tmp) + offset;
239 offset += BITS_PER_LONG;
240 }
241 }
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800242 return RADIX_TREE_MAP_SIZE;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700243}
244
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800245static unsigned int iter_offset(const struct radix_tree_iter *iter)
246{
247 return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
248}
249
Matthew Wilcox218ed752016-12-14 15:08:43 -0800250/*
251 * The maximum index which can be stored in a radix tree
252 */
253static inline unsigned long shift_maxindex(unsigned int shift)
254{
255 return (RADIX_TREE_MAP_SIZE << shift) - 1;
256}
257
Matthew Wilcox35534c82016-12-19 17:43:19 -0500258static inline unsigned long node_maxindex(const struct radix_tree_node *node)
Matthew Wilcox218ed752016-12-14 15:08:43 -0800259{
260 return shift_maxindex(node->shift);
261}
262
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500263static unsigned long next_index(unsigned long index,
264 const struct radix_tree_node *node,
265 unsigned long offset)
266{
267 return (index & ~node_maxindex(node)) + (offset << node->shift);
268}
269
Ross Zwisler0796c582016-05-20 17:02:55 -0700270#ifndef __KERNEL__
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700271static void dump_node(struct radix_tree_node *node, unsigned long index)
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700272{
Ross Zwisler0796c582016-05-20 17:02:55 -0700273 unsigned long i;
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700274
Matthew Wilcox218ed752016-12-14 15:08:43 -0800275 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
276 node, node->offset, index, index | node_maxindex(node),
277 node->parent,
Ross Zwisler0796c582016-05-20 17:02:55 -0700278 node->tags[0][0], node->tags[1][0], node->tags[2][0],
Matthew Wilcox218ed752016-12-14 15:08:43 -0800279 node->shift, node->count, node->exceptional);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700280
Ross Zwisler0796c582016-05-20 17:02:55 -0700281 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700282 unsigned long first = index | (i << node->shift);
283 unsigned long last = first | ((1UL << node->shift) - 1);
Ross Zwisler0796c582016-05-20 17:02:55 -0700284 void *entry = node->slots[i];
285 if (!entry)
286 continue;
Matthew Wilcox218ed752016-12-14 15:08:43 -0800287 if (entry == RADIX_TREE_RETRY) {
288 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
289 i, first, last, node);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700290 } else if (!radix_tree_is_internal_node(entry)) {
Matthew Wilcox218ed752016-12-14 15:08:43 -0800291 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
292 entry, i, first, last, node);
293 } else if (is_sibling_entry(node, entry)) {
294 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
295 entry, i, first, last, node,
296 *(void **)entry_to_node(entry));
Ross Zwisler0796c582016-05-20 17:02:55 -0700297 } else {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700298 dump_node(entry_to_node(entry), first);
Ross Zwisler0796c582016-05-20 17:02:55 -0700299 }
300 }
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700301}
302
303/* For debug */
304static void radix_tree_dump(struct radix_tree_root *root)
305{
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700306 pr_debug("radix root: %p rnode %p tags %x\n",
307 root, root->rnode,
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500308 root->gfp_mask >> ROOT_TAG_SHIFT);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700309 if (!radix_tree_is_internal_node(root->rnode))
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700310 return;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700311 dump_node(entry_to_node(root->rnode), 0);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700312}
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500313
314static void dump_ida_node(void *entry, unsigned long index)
315{
316 unsigned long i;
317
318 if (!entry)
319 return;
320
321 if (radix_tree_is_internal_node(entry)) {
322 struct radix_tree_node *node = entry_to_node(entry);
323
324 pr_debug("ida node: %p offset %d indices %lu-%lu parent %p free %lx shift %d count %d\n",
325 node, node->offset, index * IDA_BITMAP_BITS,
326 ((index | node_maxindex(node)) + 1) *
327 IDA_BITMAP_BITS - 1,
328 node->parent, node->tags[0][0], node->shift,
329 node->count);
330 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
331 dump_ida_node(node->slots[i],
332 index | (i << node->shift));
333 } else {
334 struct ida_bitmap *bitmap = entry;
335
336 pr_debug("ida btmp: %p offset %d indices %lu-%lu data", bitmap,
337 (int)(index & RADIX_TREE_MAP_MASK),
338 index * IDA_BITMAP_BITS,
339 (index + 1) * IDA_BITMAP_BITS - 1);
340 for (i = 0; i < IDA_BITMAP_LONGS; i++)
341 pr_cont(" %lx", bitmap->bitmap[i]);
342 pr_cont("\n");
343 }
344}
345
346static void ida_dump(struct ida *ida)
347{
348 struct radix_tree_root *root = &ida->ida_rt;
349 pr_debug("ida: %p %p free %d bitmap %p\n", ida, root->rnode,
350 root->gfp_mask >> ROOT_TAG_SHIFT,
351 ida->free_bitmap);
352 dump_ida_node(root->rnode, 0);
353}
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700354#endif
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/*
357 * This assumes that the caller has performed appropriate preallocation, and
358 * that the caller has pinned this thread of control to the current CPU.
359 */
360static struct radix_tree_node *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500361radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800362 unsigned int shift, unsigned int offset,
363 unsigned int count, unsigned int exceptional)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Nick Piggine2848a02008-02-04 22:29:10 -0800365 struct radix_tree_node *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Jan Kara5e4c0d972013-09-11 14:26:05 -0700367 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700368 * Preload code isn't irq safe and it doesn't make sense to use
369 * preloading during an interrupt anyway as all the allocations have
370 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700371 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800372 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct radix_tree_preload *rtp;
374
Nick Piggine2848a02008-02-04 22:29:10 -0800375 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700376 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700377 * cache first for the new node to get accounted to the memory
378 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700379 */
380 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700381 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700382 if (ret)
383 goto out;
384
385 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800386 * Provided the caller has preloaded here, we will always
387 * succeed in getting a node here (and never reach
388 * kmem_cache_alloc)
389 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700390 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700392 ret = rtp->nodes;
393 rtp->nodes = ret->private_data;
394 ret->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 rtp->nr--;
396 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700397 /*
398 * Update the allocation stack trace as this is more useful
399 * for debugging.
400 */
401 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700402 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700404 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700405out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700406 BUG_ON(radix_tree_is_internal_node(ret));
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800407 if (ret) {
408 ret->parent = parent;
409 ret->shift = shift;
410 ret->offset = offset;
411 ret->count = count;
412 ret->exceptional = exceptional;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return ret;
415}
416
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800417static void radix_tree_node_rcu_free(struct rcu_head *head)
418{
419 struct radix_tree_node *node =
420 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700421
422 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800423 * Must only free zeroed nodes into the slab. We can be left with
424 * non-NULL entries by radix_tree_free_nodes, so clear the entries
425 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700426 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800427 memset(node->slots, 0, sizeof(node->slots));
428 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800429 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700430
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800431 kmem_cache_free(radix_tree_node_cachep, node);
432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434static inline void
435radix_tree_node_free(struct radix_tree_node *node)
436{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800437 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
440/*
441 * Load up this CPU's radix_tree_node buffer with sufficient objects to
442 * ensure that the addition of a single element in the tree cannot fail. On
443 * success, return zero, with preemption disabled. On error, return -ENOMEM
444 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000445 *
446 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800447 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Matthew Wilcox2791653a2016-12-14 15:09:04 -0800449static int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 struct radix_tree_preload *rtp;
452 struct radix_tree_node *node;
453 int ret = -ENOMEM;
454
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700455 /*
456 * Nodes preloaded by one cgroup can be be used by another cgroup, so
457 * they should never be accounted to any particular memory cgroup.
458 */
459 gfp_mask &= ~__GFP_ACCOUNT;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700462 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700463 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700465 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 if (node == NULL)
467 goto out;
468 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700469 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700470 if (rtp->nr < nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700471 node->private_data = rtp->nodes;
472 rtp->nodes = node;
473 rtp->nr++;
474 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 ret = 0;
479out:
480 return ret;
481}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700482
483/*
484 * Load up this CPU's radix_tree_node buffer with sufficient objects to
485 * ensure that the addition of a single element in the tree cannot fail. On
486 * success, return zero, with preemption disabled. On error, return -ENOMEM
487 * with preemption not disabled.
488 *
489 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800490 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700491 */
492int radix_tree_preload(gfp_t gfp_mask)
493{
494 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800495 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700496 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700497}
David Chinnerd7f09232007-07-14 16:05:04 +1000498EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Nick Piggin6e954b92006-01-08 01:01:40 -0800500/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700501 * The same as above function, except we don't guarantee preloading happens.
502 * We do it, if we decide it helps. On success, return zero with preemption
503 * disabled. On error, return -ENOMEM with preemption not disabled.
504 */
505int radix_tree_maybe_preload(gfp_t gfp_mask)
506{
Mel Gormand0164ad2015-11-06 16:28:21 -0800507 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700508 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700509 /* Preloading doesn't help anything with this gfp mask, skip it */
510 preempt_disable();
511 return 0;
512}
513EXPORT_SYMBOL(radix_tree_maybe_preload);
514
Matthew Wilcox2791653a2016-12-14 15:09:04 -0800515#ifdef CONFIG_RADIX_TREE_MULTIORDER
516/*
517 * Preload with enough objects to ensure that we can split a single entry
518 * of order @old_order into many entries of size @new_order
519 */
520int radix_tree_split_preload(unsigned int old_order, unsigned int new_order,
521 gfp_t gfp_mask)
522{
523 unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT);
524 unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) -
525 (new_order / RADIX_TREE_MAP_SHIFT);
526 unsigned nr = 0;
527
528 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
529 BUG_ON(new_order >= old_order);
530
531 while (layers--)
532 nr = nr * RADIX_TREE_MAP_SIZE + 1;
533 return __radix_tree_preload(gfp_mask, top * nr);
534}
535#endif
536
Jan Kara5e4c0d972013-09-11 14:26:05 -0700537/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700538 * The same as function above, but preload number of nodes required to insert
539 * (1 << order) continuous naturally-aligned elements.
540 */
541int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
542{
543 unsigned long nr_subtrees;
544 int nr_nodes, subtree_height;
545
546 /* Preloading doesn't help anything with this gfp mask, skip it */
547 if (!gfpflags_allow_blocking(gfp_mask)) {
548 preempt_disable();
549 return 0;
550 }
551
552 /*
553 * Calculate number and height of fully populated subtrees it takes to
554 * store (1 << order) elements.
555 */
556 nr_subtrees = 1 << order;
557 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
558 subtree_height++)
559 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
560
561 /*
562 * The worst case is zero height tree with a single item at index 0 and
563 * then inserting items starting at ULONG_MAX - (1 << order).
564 *
565 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
566 * 0-index item.
567 */
568 nr_nodes = RADIX_TREE_MAX_PATH;
569
570 /* Plus branch to fully populated subtrees. */
571 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
572
573 /* Root node is shared. */
574 nr_nodes--;
575
576 /* Plus nodes required to build subtrees. */
577 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
578
579 return __radix_tree_preload(gfp_mask, nr_nodes);
580}
581
Matthew Wilcox35534c82016-12-19 17:43:19 -0500582static unsigned radix_tree_load_root(const struct radix_tree_root *root,
Matthew Wilcox1456a432016-05-20 17:02:08 -0700583 struct radix_tree_node **nodep, unsigned long *maxindex)
584{
585 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
586
587 *nodep = node;
588
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700589 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700590 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700591 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700592 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700593 }
594
595 *maxindex = 0;
596 return 0;
597}
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599/*
600 * Extend a radix tree so it can store key @index.
601 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500602static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700603 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800605 struct radix_tree_node *slot;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700606 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 int tag;
608
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700609 /* Figure out what the shift should be. */
610 maxshift = shift;
611 while (index > shift_maxindex(maxshift))
612 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700614 slot = root->rnode;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500615 if (!slot && (!is_idr(root) || root_tag_get(root, IDR_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 do {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500619 struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL,
620 shift, 0, 1, 0);
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700621 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -ENOMEM;
623
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500624 if (is_idr(root)) {
625 all_tag_set(node, IDR_FREE);
626 if (!root_tag_get(root, IDR_FREE)) {
627 tag_clear(node, IDR_FREE, 0);
628 root_tag_set(root, IDR_FREE);
629 }
630 } else {
631 /* Propagate the aggregated tag info to the new child */
632 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
633 if (root_tag_get(root, tag))
634 tag_set(node, tag, 0);
635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 }
637
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700638 BUG_ON(shift > BITS_PER_LONG);
Johannes Weinerf7942432016-12-12 16:43:41 -0800639 if (radix_tree_is_internal_node(slot)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700640 entry_to_node(slot)->parent = node;
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800641 } else if (radix_tree_exceptional_entry(slot)) {
Johannes Weinerf7942432016-12-12 16:43:41 -0800642 /* Moving an exceptional root->rnode to a node */
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800643 node->exceptional = 1;
Johannes Weinerf7942432016-12-12 16:43:41 -0800644 }
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800645 node->slots[0] = slot;
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -0700646 slot = node_to_entry(node);
647 rcu_assign_pointer(root->rnode, slot);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700648 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700649 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700651 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800655 * radix_tree_shrink - shrink radix tree to minimum height
656 * @root radix tree root
657 */
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500658static inline bool radix_tree_shrink(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800659 radix_tree_update_node_t update_node,
660 void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800661{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500662 bool shrunk = false;
663
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800664 for (;;) {
665 struct radix_tree_node *node = root->rnode;
666 struct radix_tree_node *child;
667
668 if (!radix_tree_is_internal_node(node))
669 break;
670 node = entry_to_node(node);
671
672 /*
673 * The candidate node has more than one child, or its child
674 * is not at the leftmost slot, or the child is a multiorder
675 * entry, we cannot shrink.
676 */
677 if (node->count != 1)
678 break;
679 child = node->slots[0];
680 if (!child)
681 break;
682 if (!radix_tree_is_internal_node(child) && node->shift)
683 break;
684
685 if (radix_tree_is_internal_node(child))
686 entry_to_node(child)->parent = NULL;
687
688 /*
689 * We don't need rcu_assign_pointer(), since we are simply
690 * moving the node from one part of the tree to another: if it
691 * was safe to dereference the old pointer to it
692 * (node->slots[0]), it will be safe to dereference the new
693 * one (root->rnode) as far as dependent read barriers go.
694 */
695 root->rnode = child;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500696 if (is_idr(root) && !tag_get(node, IDR_FREE, 0))
697 root_tag_clear(root, IDR_FREE);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800698
699 /*
700 * We have a dilemma here. The node's slot[0] must not be
701 * NULLed in case there are concurrent lookups expecting to
702 * find the item. However if this was a bottom-level node,
703 * then it may be subject to the slot pointer being visible
704 * to callers dereferencing it. If item corresponding to
705 * slot[0] is subsequently deleted, these callers would expect
706 * their slot to become empty sooner or later.
707 *
708 * For example, lockless pagecache will look up a slot, deref
709 * the page pointer, and if the page has 0 refcount it means it
710 * was concurrently deleted from pagecache so try the deref
711 * again. Fortunately there is already a requirement for logic
712 * to retry the entire slot lookup -- the indirect pointer
713 * problem (replacing direct root node with an indirect pointer
714 * also results in a stale slot). So tag the slot as indirect
715 * to force callers to retry.
716 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800717 node->count = 0;
718 if (!radix_tree_is_internal_node(child)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800719 node->slots[0] = RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800720 if (update_node)
721 update_node(node, private);
722 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800723
Johannes Weinerea07b862017-01-06 19:21:43 -0500724 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800725 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500726 shrunk = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800727 }
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500728
729 return shrunk;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800730}
731
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500732static bool delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800733 struct radix_tree_node *node,
734 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800735{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500736 bool deleted = false;
737
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800738 do {
739 struct radix_tree_node *parent;
740
741 if (node->count) {
742 if (node == entry_to_node(root->rnode))
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500743 deleted |= radix_tree_shrink(root, update_node,
744 private);
745 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800746 }
747
748 parent = node->parent;
749 if (parent) {
750 parent->slots[node->offset] = NULL;
751 parent->count--;
752 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500753 /*
754 * Shouldn't the tags already have all been cleared
755 * by the caller?
756 */
757 if (!is_idr(root))
758 root_tag_clear_all(root);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800759 root->rnode = NULL;
760 }
761
Johannes Weinerea07b862017-01-06 19:21:43 -0500762 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800763 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500764 deleted = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800765
766 node = parent;
767 } while (node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500768
769 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800770}
771
772/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700773 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 * @root: radix tree root
775 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700776 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700777 * @nodep: returns node
778 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700780 * Create, if necessary, and return the node and slot for an item
781 * at position @index in the radix tree @root.
782 *
783 * Until there is more than one item in the tree, no nodes are
784 * allocated and @root->rnode is used as a direct slot instead of
785 * pointing to a node, in which case *@nodep will be NULL.
786 *
787 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700789int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700790 unsigned order, struct radix_tree_node **nodep,
791 void ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700793 struct radix_tree_node *node = NULL, *child;
794 void **slot = (void **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700795 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700796 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700797 unsigned long max = index | ((1UL << order) - 1);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500798 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700799
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700800 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800803 if (order > 0 && max == ((1UL << order) - 1))
804 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700805 if (max > maxindex) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500806 int error = radix_tree_extend(root, gfp, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700807 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700809 shift = error;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700810 child = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700813 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700814 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700815 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 /* Have to add a child node. */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500817 child = radix_tree_node_alloc(gfp, node, shift,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800818 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700819 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700821 rcu_assign_pointer(*slot, node_to_entry(child));
822 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700824 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700825 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700828 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700829 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700830 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700831 }
832
Johannes Weiner139e5612014-04-03 14:47:54 -0700833 if (nodep)
834 *nodep = node;
835 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700836 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700837 return 0;
838}
839
Matthew Wilcox175542f2016-12-14 15:08:58 -0800840/*
841 * Free any nodes below this node. The tree is presumed to not need
842 * shrinking, and any user data in the tree is presumed to not need a
843 * destructor called on it. If we need to add a destructor, we can
844 * add that functionality later. Note that we may not clear tags or
845 * slots from the tree as an RCU walker may still have a pointer into
846 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
847 * but we'll still have to clear those in rcu_free.
848 */
849static void radix_tree_free_nodes(struct radix_tree_node *node)
850{
851 unsigned offset = 0;
852 struct radix_tree_node *child = entry_to_node(node);
853
854 for (;;) {
855 void *entry = child->slots[offset];
856 if (radix_tree_is_internal_node(entry) &&
857 !is_sibling_entry(child, entry)) {
858 child = entry_to_node(entry);
859 offset = 0;
860 continue;
861 }
862 offset++;
863 while (offset == RADIX_TREE_MAP_SIZE) {
864 struct radix_tree_node *old = child;
865 offset = child->offset + 1;
866 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800867 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800868 radix_tree_node_free(old);
869 if (old == entry_to_node(node))
870 return;
871 }
872 }
873}
874
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500875#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcox175542f2016-12-14 15:08:58 -0800876static inline int insert_entries(struct radix_tree_node *node, void **slot,
877 void *item, unsigned order, bool replace)
878{
879 struct radix_tree_node *child;
880 unsigned i, n, tag, offset, tags = 0;
881
882 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800883 if (order > node->shift)
884 n = 1 << (order - node->shift);
885 else
886 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800887 offset = get_slot_offset(node, slot);
888 } else {
889 n = 1;
890 offset = 0;
891 }
892
893 if (n > 1) {
894 offset = offset & ~(n - 1);
895 slot = &node->slots[offset];
896 }
897 child = node_to_entry(slot);
898
899 for (i = 0; i < n; i++) {
900 if (slot[i]) {
901 if (replace) {
902 node->count--;
903 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
904 if (tag_get(node, tag, offset + i))
905 tags |= 1 << tag;
906 } else
907 return -EEXIST;
908 }
909 }
910
911 for (i = 0; i < n; i++) {
912 struct radix_tree_node *old = slot[i];
913 if (i) {
914 rcu_assign_pointer(slot[i], child);
915 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
916 if (tags & (1 << tag))
917 tag_clear(node, tag, offset + i);
918 } else {
919 rcu_assign_pointer(slot[i], item);
920 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
921 if (tags & (1 << tag))
922 tag_set(node, tag, offset);
923 }
924 if (radix_tree_is_internal_node(old) &&
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800925 !is_sibling_entry(node, old) &&
926 (old != RADIX_TREE_RETRY))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800927 radix_tree_free_nodes(old);
928 if (radix_tree_exceptional_entry(old))
929 node->exceptional--;
930 }
931 if (node) {
932 node->count += n;
933 if (radix_tree_exceptional_entry(item))
934 node->exceptional += n;
935 }
936 return n;
937}
938#else
939static inline int insert_entries(struct radix_tree_node *node, void **slot,
940 void *item, unsigned order, bool replace)
941{
942 if (*slot)
943 return -EEXIST;
944 rcu_assign_pointer(*slot, item);
945 if (node) {
946 node->count++;
947 if (radix_tree_exceptional_entry(item))
948 node->exceptional++;
949 }
950 return 1;
951}
952#endif
953
Johannes Weiner139e5612014-04-03 14:47:54 -0700954/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700955 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700956 * @root: radix tree root
957 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700958 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700959 * @item: item to insert
960 *
961 * Insert an item into the radix tree at position @index.
962 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700963int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
964 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700965{
966 struct radix_tree_node *node;
967 void **slot;
968 int error;
969
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700970 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700971
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700972 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700973 if (error)
974 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800975
976 error = insert_entries(node, slot, item, order, false);
977 if (error < 0)
978 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700979
Nick Piggin612d6c12006-06-23 02:03:22 -0700980 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700981 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700982 BUG_ON(tag_get(node, 0, offset));
983 BUG_ON(tag_get(node, 1, offset));
984 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700985 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700986 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 0;
990}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700991EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Johannes Weiner139e5612014-04-03 14:47:54 -0700993/**
994 * __radix_tree_lookup - lookup an item in a radix tree
995 * @root: radix tree root
996 * @index: index key
997 * @nodep: returns node
998 * @slotp: returns slot
999 *
1000 * Lookup and return the item at position @index in the radix
1001 * tree @root.
1002 *
1003 * Until there is more than one item in the tree, no nodes are
1004 * allocated and @root->rnode is used as a direct slot instead of
1005 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -08001006 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001007void *__radix_tree_lookup(const struct radix_tree_root *root,
1008 unsigned long index, struct radix_tree_node **nodep,
1009 void ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -08001010{
Johannes Weiner139e5612014-04-03 14:47:54 -07001011 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -07001012 unsigned long maxindex;
Johannes Weiner139e5612014-04-03 14:47:54 -07001013 void **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001014
Matthew Wilcox85829952016-05-20 17:02:20 -07001015 restart:
1016 parent = NULL;
1017 slot = (void **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001018 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -07001019 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001020 return NULL;
1021
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001022 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -07001023 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -07001024
Matthew Wilcox85829952016-05-20 17:02:20 -07001025 if (node == RADIX_TREE_RETRY)
1026 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001027 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001028 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -07001029 slot = parent->slots + offset;
1030 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001031
Johannes Weiner139e5612014-04-03 14:47:54 -07001032 if (nodep)
1033 *nodep = parent;
1034 if (slotp)
1035 *slotp = slot;
1036 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -07001037}
1038
1039/**
1040 * radix_tree_lookup_slot - lookup a slot in a radix tree
1041 * @root: radix tree root
1042 * @index: index key
1043 *
1044 * Returns: the slot corresponding to the position @index in the
1045 * radix tree @root. This is useful for update-if-exists operations.
1046 *
1047 * This function can be called under rcu_read_lock iff the slot is not
1048 * modified by radix_tree_replace_slot, otherwise it must be called
1049 * exclusive from other writers. Any dereference of the slot must be done
1050 * using radix_tree_deref_slot.
1051 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001052void **radix_tree_lookup_slot(const struct radix_tree_root *root,
1053 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -07001054{
Johannes Weiner139e5612014-04-03 14:47:54 -07001055 void **slot;
1056
1057 if (!__radix_tree_lookup(root, index, NULL, &slot))
1058 return NULL;
1059 return slot;
Hans Reisera4331362005-11-07 00:59:29 -08001060}
1061EXPORT_SYMBOL(radix_tree_lookup_slot);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/**
1064 * radix_tree_lookup - perform lookup operation on a radix tree
1065 * @root: radix tree root
1066 * @index: index key
1067 *
1068 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001069 *
1070 * This function can be called under rcu_read_lock, however the caller
1071 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
1072 * them safely). No RCU barriers are required to access or modify the
1073 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001075void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
Johannes Weiner139e5612014-04-03 14:47:54 -07001077 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079EXPORT_SYMBOL(radix_tree_lookup);
1080
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001081static inline void replace_sibling_entries(struct radix_tree_node *node,
1082 void **slot, int count, int exceptional)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001083{
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001084#ifdef CONFIG_RADIX_TREE_MULTIORDER
1085 void *ptr = node_to_entry(slot);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001086 unsigned offset = get_slot_offset(node, slot) + 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001087
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001088 while (offset < RADIX_TREE_MAP_SIZE) {
1089 if (node->slots[offset] != ptr)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001090 break;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001091 if (count < 0) {
1092 node->slots[offset] = NULL;
1093 node->count--;
1094 }
1095 node->exceptional += exceptional;
1096 offset++;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001097 }
1098#endif
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001099}
1100
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001101static void replace_slot(void **slot, void *item, struct radix_tree_node *node,
1102 int count, int exceptional)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001103{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001104 if (WARN_ON_ONCE(radix_tree_is_internal_node(item)))
1105 return;
Johannes Weiner6d75f362016-12-12 16:43:43 -08001106
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001107 if (node && (count || exceptional)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001108 node->count += count;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001109 node->exceptional += exceptional;
1110 replace_sibling_entries(node, slot, count, exceptional);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001111 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001112
1113 rcu_assign_pointer(*slot, item);
1114}
1115
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001116static bool node_tag_get(const struct radix_tree_root *root,
1117 const struct radix_tree_node *node,
1118 unsigned int tag, unsigned int offset)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001119{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001120 if (node)
1121 return tag_get(node, tag, offset);
1122 return root_tag_get(root, tag);
1123}
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001124
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001125/*
1126 * IDR users want to be able to store NULL in the tree, so if the slot isn't
1127 * free, don't adjust the count, even if it's transitioning between NULL and
1128 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
1129 * have empty bits, but it only stores NULL in slots when they're being
1130 * deleted.
1131 */
1132static int calculate_count(struct radix_tree_root *root,
1133 struct radix_tree_node *node, void **slot,
1134 void *item, void *old)
1135{
1136 if (is_idr(root)) {
1137 unsigned offset = get_slot_offset(node, slot);
1138 bool free = node_tag_get(root, node, IDR_FREE, offset);
1139 if (!free)
1140 return 0;
1141 if (!old)
1142 return 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001143 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001144 return !!item - !!old;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147/**
Johannes Weinerf7942432016-12-12 16:43:41 -08001148 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -08001149 * @root: radix tree root
1150 * @node: pointer to tree node
1151 * @slot: pointer to slot in @node
1152 * @item: new item to store in the slot.
1153 * @update_node: callback for changing leaf nodes
1154 * @private: private data to pass to @update_node
Johannes Weinerf7942432016-12-12 16:43:41 -08001155 *
1156 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1157 * across slot lookup and replacement.
1158 */
1159void __radix_tree_replace(struct radix_tree_root *root,
1160 struct radix_tree_node *node,
Johannes Weiner4d693d02016-12-12 16:43:49 -08001161 void **slot, void *item,
1162 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf7942432016-12-12 16:43:41 -08001163{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001164 void *old = rcu_dereference_raw(*slot);
1165 int exceptional = !!radix_tree_exceptional_entry(item) -
1166 !!radix_tree_exceptional_entry(old);
1167 int count = calculate_count(root, node, slot, item, old);
1168
Johannes Weiner6d75f362016-12-12 16:43:43 -08001169 /*
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001170 * This function supports replacing exceptional entries and
1171 * deleting entries, but that needs accounting against the
1172 * node unless the slot is root->rnode.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001173 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001174 WARN_ON_ONCE(!node && (slot != (void **)&root->rnode) &&
1175 (count || exceptional));
1176 replace_slot(slot, item, node, count, exceptional);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001177
Johannes Weiner4d693d02016-12-12 16:43:49 -08001178 if (!node)
1179 return;
1180
1181 if (update_node)
1182 update_node(node, private);
1183
1184 delete_node(root, node, update_node, private);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001185}
Johannes Weinerf7942432016-12-12 16:43:41 -08001186
Johannes Weiner6d75f362016-12-12 16:43:43 -08001187/**
1188 * radix_tree_replace_slot - replace item in a slot
1189 * @root: radix tree root
1190 * @slot: pointer to slot
1191 * @item: new item to store in the slot.
1192 *
1193 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1194 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1195 * across slot lookup and replacement.
1196 *
1197 * NOTE: This cannot be used to switch between non-entries (empty slots),
1198 * regular entries, and exceptional entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001199 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001200 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1201 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001202 */
1203void radix_tree_replace_slot(struct radix_tree_root *root,
1204 void **slot, void *item)
1205{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001206 __radix_tree_replace(root, NULL, slot, item, NULL, NULL);
Johannes Weinerf7942432016-12-12 16:43:41 -08001207}
1208
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001209/**
1210 * radix_tree_iter_replace - replace item in a slot
1211 * @root: radix tree root
1212 * @slot: pointer to slot
1213 * @item: new item to store in the slot.
1214 *
1215 * For use with radix_tree_split() and radix_tree_for_each_slot().
1216 * Caller must hold tree write locked across split and replacement.
1217 */
1218void radix_tree_iter_replace(struct radix_tree_root *root,
1219 const struct radix_tree_iter *iter, void **slot, void *item)
1220{
1221 __radix_tree_replace(root, iter->node, slot, item, NULL, NULL);
1222}
1223
Matthew Wilcox175542f2016-12-14 15:08:58 -08001224#ifdef CONFIG_RADIX_TREE_MULTIORDER
1225/**
1226 * radix_tree_join - replace multiple entries with one multiorder entry
1227 * @root: radix tree root
1228 * @index: an index inside the new entry
1229 * @order: order of the new entry
1230 * @item: new entry
1231 *
1232 * Call this function to replace several entries with one larger entry.
1233 * The existing entries are presumed to not need freeing as a result of
1234 * this call.
1235 *
1236 * The replacement entry will have all the tags set on it that were set
1237 * on any of the entries it is replacing.
1238 */
1239int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1240 unsigned order, void *item)
1241{
1242 struct radix_tree_node *node;
1243 void **slot;
1244 int error;
1245
1246 BUG_ON(radix_tree_is_internal_node(item));
1247
1248 error = __radix_tree_create(root, index, order, &node, &slot);
1249 if (!error)
1250 error = insert_entries(node, slot, item, order, true);
1251 if (error > 0)
1252 error = 0;
1253
1254 return error;
1255}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001256
1257/**
1258 * radix_tree_split - Split an entry into smaller entries
1259 * @root: radix tree root
1260 * @index: An index within the large entry
1261 * @order: Order of new entries
1262 *
1263 * Call this function as the first step in replacing a multiorder entry
1264 * with several entries of lower order. After this function returns,
1265 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1266 * and call radix_tree_iter_replace() to set up each new entry.
1267 *
1268 * The tags from this entry are replicated to all the new entries.
1269 *
1270 * The radix tree should be locked against modification during the entire
1271 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1272 * should prompt RCU walkers to restart the lookup from the root.
1273 */
1274int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1275 unsigned order)
1276{
1277 struct radix_tree_node *parent, *node, *child;
1278 void **slot;
1279 unsigned int offset, end;
1280 unsigned n, tag, tags = 0;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001281 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001282
1283 if (!__radix_tree_lookup(root, index, &parent, &slot))
1284 return -ENOENT;
1285 if (!parent)
1286 return -ENOENT;
1287
1288 offset = get_slot_offset(parent, slot);
1289
1290 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1291 if (tag_get(parent, tag, offset))
1292 tags |= 1 << tag;
1293
1294 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
1295 if (!is_sibling_entry(parent, parent->slots[end]))
1296 break;
1297 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1298 if (tags & (1 << tag))
1299 tag_set(parent, tag, end);
1300 /* rcu_assign_pointer ensures tags are set before RETRY */
1301 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1302 }
1303 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
1304 parent->exceptional -= (end - offset);
1305
1306 if (order == parent->shift)
1307 return 0;
1308 if (order > parent->shift) {
1309 while (offset < end)
1310 offset += insert_entries(parent, &parent->slots[offset],
1311 RADIX_TREE_RETRY, order, true);
1312 return 0;
1313 }
1314
1315 node = parent;
1316
1317 for (;;) {
1318 if (node->shift > order) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001319 child = radix_tree_node_alloc(gfp, node,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -08001320 node->shift - RADIX_TREE_MAP_SHIFT,
1321 offset, 0, 0);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001322 if (!child)
1323 goto nomem;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001324 if (node != parent) {
1325 node->count++;
1326 node->slots[offset] = node_to_entry(child);
1327 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1328 if (tags & (1 << tag))
1329 tag_set(node, tag, offset);
1330 }
1331
1332 node = child;
1333 offset = 0;
1334 continue;
1335 }
1336
1337 n = insert_entries(node, &node->slots[offset],
1338 RADIX_TREE_RETRY, order, false);
1339 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1340
1341 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1342 if (tags & (1 << tag))
1343 tag_set(node, tag, offset);
1344 offset += n;
1345
1346 while (offset == RADIX_TREE_MAP_SIZE) {
1347 if (node == parent)
1348 break;
1349 offset = node->offset;
1350 child = node;
1351 node = node->parent;
1352 rcu_assign_pointer(node->slots[offset],
1353 node_to_entry(child));
1354 offset++;
1355 }
1356 if ((node == parent) && (offset == end))
1357 return 0;
1358 }
1359
1360 nomem:
1361 /* Shouldn't happen; did user forget to preload? */
1362 /* TODO: free all the allocated nodes */
1363 WARN_ON(1);
1364 return -ENOMEM;
1365}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001366#endif
1367
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001368static void node_tag_set(struct radix_tree_root *root,
1369 struct radix_tree_node *node,
1370 unsigned int tag, unsigned int offset)
1371{
1372 while (node) {
1373 if (tag_get(node, tag, offset))
1374 return;
1375 tag_set(node, tag, offset);
1376 offset = node->offset;
1377 node = node->parent;
1378 }
1379
1380 if (!root_tag_get(root, tag))
1381 root_tag_set(root, tag);
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384/**
1385 * radix_tree_tag_set - set a tag on a radix tree node
1386 * @root: radix tree root
1387 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001388 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001390 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1391 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 * the root all the way down to the leaf node.
1393 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001394 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 * item is a bug.
1396 */
1397void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001398 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Ross Zwislerfb969902016-05-20 17:02:32 -07001400 struct radix_tree_node *node, *parent;
1401 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001403 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001404 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001406 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001407 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001409 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001410 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001411 BUG_ON(!node);
1412
1413 if (!tag_get(parent, tag, offset))
1414 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
Nick Piggin612d6c12006-06-23 02:03:22 -07001417 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001418 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001419 root_tag_set(root, tag);
1420
Ross Zwislerfb969902016-05-20 17:02:32 -07001421 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422}
1423EXPORT_SYMBOL(radix_tree_tag_set);
1424
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001425/**
1426 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1427 * @root: radix tree root
1428 * @iter: iterator state
1429 * @tag: tag to set
1430 */
1431void radix_tree_iter_tag_set(struct radix_tree_root *root,
1432 const struct radix_tree_iter *iter, unsigned int tag)
1433{
1434 node_tag_set(root, iter->node, tag, iter_offset(iter));
1435}
1436
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001437static void node_tag_clear(struct radix_tree_root *root,
1438 struct radix_tree_node *node,
1439 unsigned int tag, unsigned int offset)
1440{
1441 while (node) {
1442 if (!tag_get(node, tag, offset))
1443 return;
1444 tag_clear(node, tag, offset);
1445 if (any_tag_set(node, tag))
1446 return;
1447
1448 offset = node->offset;
1449 node = node->parent;
1450 }
1451
1452 /* clear the root's tag bit */
1453 if (root_tag_get(root, tag))
1454 root_tag_clear(root, tag);
1455}
1456
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001457/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 * radix_tree_tag_clear - clear a tag on a radix tree node
1459 * @root: radix tree root
1460 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001461 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001463 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001464 * corresponding to @index in the radix tree. If this causes
1465 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 * next-to-leaf node, etc.
1467 *
1468 * Returns the address of the tagged item on success, else NULL. ie:
1469 * has the same return value and semantics as radix_tree_lookup().
1470 */
1471void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001472 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001474 struct radix_tree_node *node, *parent;
1475 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001476 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001478 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001479 if (index > maxindex)
1480 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Ross Zwisler00f47b52016-05-20 17:02:35 -07001482 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001484 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001485 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001486 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001489 if (node)
1490 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Ross Zwisler00f47b52016-05-20 17:02:35 -07001492 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
1494EXPORT_SYMBOL(radix_tree_tag_clear);
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001497 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1498 * @root: radix tree root
1499 * @iter: iterator state
1500 * @tag: tag to clear
1501 */
1502void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1503 const struct radix_tree_iter *iter, unsigned int tag)
1504{
1505 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1506}
1507
1508/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001509 * radix_tree_tag_get - get a tag on a radix tree node
1510 * @root: radix tree root
1511 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001512 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001514 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001516 * 0: tag not present or not set
1517 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001518 *
1519 * Note that the return value of this function may not be relied on, even if
1520 * the RCU lock is held, unless tag modification and node deletion are excluded
1521 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001523int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001524 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001526 struct radix_tree_node *node, *parent;
1527 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Nick Piggin612d6c12006-06-23 02:03:22 -07001529 if (!root_tag_get(root, tag))
1530 return 0;
1531
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001532 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001533 if (index > maxindex)
1534 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001535
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001536 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001537 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001538
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001539 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001540 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001541
Ross Zwisler4589ba62016-05-20 17:02:38 -07001542 if (!tag_get(parent, tag, offset))
1543 return 0;
1544 if (node == RADIX_TREE_RETRY)
1545 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001547
1548 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Ross Zwisler21ef5332016-05-20 17:02:26 -07001552static inline void __set_iter_shift(struct radix_tree_iter *iter,
1553 unsigned int shift)
1554{
1555#ifdef CONFIG_RADIX_TREE_MULTIORDER
1556 iter->shift = shift;
1557#endif
1558}
1559
Matthew Wilcox148deab2016-12-14 15:08:49 -08001560/* Construct iter->tags bit-mask from node->tags[tag] array */
1561static void set_iter_tags(struct radix_tree_iter *iter,
1562 struct radix_tree_node *node, unsigned offset,
1563 unsigned tag)
1564{
1565 unsigned tag_long = offset / BITS_PER_LONG;
1566 unsigned tag_bit = offset % BITS_PER_LONG;
1567
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001568 if (!node) {
1569 iter->tags = 1;
1570 return;
1571 }
1572
Matthew Wilcox148deab2016-12-14 15:08:49 -08001573 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1574
1575 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1576 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1577 /* Pick tags from next element */
1578 if (tag_bit)
1579 iter->tags |= node->tags[tag][tag_long + 1] <<
1580 (BITS_PER_LONG - tag_bit);
1581 /* Clip chunk size, here only BITS_PER_LONG tags */
1582 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1583 }
1584}
1585
1586#ifdef CONFIG_RADIX_TREE_MULTIORDER
1587static void **skip_siblings(struct radix_tree_node **nodep,
1588 void **slot, struct radix_tree_iter *iter)
1589{
1590 void *sib = node_to_entry(slot - 1);
1591
1592 while (iter->index < iter->next_index) {
1593 *nodep = rcu_dereference_raw(*slot);
1594 if (*nodep && *nodep != sib)
1595 return slot;
1596 slot++;
1597 iter->index = __radix_tree_iter_add(iter, 1);
1598 iter->tags >>= 1;
1599 }
1600
1601 *nodep = NULL;
1602 return NULL;
1603}
1604
1605void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
1606 unsigned flags)
1607{
1608 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1609 struct radix_tree_node *node = rcu_dereference_raw(*slot);
1610
1611 slot = skip_siblings(&node, slot, iter);
1612
1613 while (radix_tree_is_internal_node(node)) {
1614 unsigned offset;
1615 unsigned long next_index;
1616
1617 if (node == RADIX_TREE_RETRY)
1618 return slot;
1619 node = entry_to_node(node);
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001620 iter->node = node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001621 iter->shift = node->shift;
1622
1623 if (flags & RADIX_TREE_ITER_TAGGED) {
1624 offset = radix_tree_find_next_bit(node, tag, 0);
1625 if (offset == RADIX_TREE_MAP_SIZE)
1626 return NULL;
1627 slot = &node->slots[offset];
1628 iter->index = __radix_tree_iter_add(iter, offset);
1629 set_iter_tags(iter, node, offset, tag);
1630 node = rcu_dereference_raw(*slot);
1631 } else {
1632 offset = 0;
1633 slot = &node->slots[0];
1634 for (;;) {
1635 node = rcu_dereference_raw(*slot);
1636 if (node)
1637 break;
1638 slot++;
1639 offset++;
1640 if (offset == RADIX_TREE_MAP_SIZE)
1641 return NULL;
1642 }
1643 iter->index = __radix_tree_iter_add(iter, offset);
1644 }
1645 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1646 goto none;
1647 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1648 if (next_index < iter->next_index)
1649 iter->next_index = next_index;
1650 }
1651
1652 return slot;
1653 none:
1654 iter->next_index = 0;
1655 return NULL;
1656}
1657EXPORT_SYMBOL(__radix_tree_next_slot);
1658#else
1659static void **skip_siblings(struct radix_tree_node **nodep,
1660 void **slot, struct radix_tree_iter *iter)
1661{
1662 return slot;
1663}
1664#endif
1665
1666void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
1667{
1668 struct radix_tree_node *node;
1669
1670 slot++;
1671 iter->index = __radix_tree_iter_add(iter, 1);
1672 node = rcu_dereference_raw(*slot);
1673 skip_siblings(&node, slot, iter);
1674 iter->next_index = iter->index;
1675 iter->tags = 0;
1676 return NULL;
1677}
1678EXPORT_SYMBOL(radix_tree_iter_resume);
1679
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001680/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001681 * radix_tree_next_chunk - find next chunk of slots for iteration
1682 *
1683 * @root: radix tree root
1684 * @iter: iterator state
1685 * @flags: RADIX_TREE_ITER_* flags and tag index
1686 * Returns: pointer to chunk first slot, or NULL if iteration is over
1687 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001688void **radix_tree_next_chunk(const struct radix_tree_root *root,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001689 struct radix_tree_iter *iter, unsigned flags)
1690{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001691 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001692 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001693 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001694
1695 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1696 return NULL;
1697
1698 /*
1699 * Catch next_index overflow after ~0UL. iter->index never overflows
1700 * during iterating; it can be zero only at the beginning.
1701 * And we cannot overflow iter->next_index in a single step,
1702 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001703 *
1704 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001705 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001706 */
1707 index = iter->next_index;
1708 if (!index && iter->index)
1709 return NULL;
1710
Ross Zwisler21ef5332016-05-20 17:02:26 -07001711 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001712 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001713 if (index > maxindex)
1714 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001715 if (!child)
1716 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001717
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001718 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001719 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001720 iter->index = index;
1721 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001722 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001723 iter->node = NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001724 __set_iter_shift(iter, 0);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001725 return (void **)&root->rnode;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001726 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001727
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001728 do {
1729 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001730 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001731
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001732 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001733 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001734 /* Hole detected */
1735 if (flags & RADIX_TREE_ITER_CONTIG)
1736 return NULL;
1737
1738 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001739 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001740 offset + 1);
1741 else
1742 while (++offset < RADIX_TREE_MAP_SIZE) {
Ross Zwisler21ef5332016-05-20 17:02:26 -07001743 void *slot = node->slots[offset];
1744 if (is_sibling_entry(node, slot))
1745 continue;
1746 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001747 break;
1748 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001749 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001750 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001751 /* Overflow after ~0UL */
1752 if (!index)
1753 return NULL;
1754 if (offset == RADIX_TREE_MAP_SIZE)
1755 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001756 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001757 }
1758
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001759 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001760 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001761 if (child == RADIX_TREE_RETRY)
1762 break;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001763 } while (radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001764
1765 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001766 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1767 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001768 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001769 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001770
Matthew Wilcox148deab2016-12-14 15:08:49 -08001771 if (flags & RADIX_TREE_ITER_TAGGED)
1772 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001773
1774 return node->slots + offset;
1775}
1776EXPORT_SYMBOL(radix_tree_next_chunk);
1777
1778/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1780 * @root: radix tree root
1781 * @results: where the results of the lookup are placed
1782 * @first_index: start the lookup from this key
1783 * @max_items: place up to this many items at *results
1784 *
1785 * Performs an index-ascending scan of the tree for present items. Places
1786 * them at *@results and returns the number of items which were placed at
1787 * *@results.
1788 *
1789 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001790 *
1791 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1792 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001793 * an atomic snapshot of the tree at a single point in time, the
1794 * semantics of an RCU protected gang lookup are as though multiple
1795 * radix_tree_lookups have been issued in individual locks, and results
1796 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
1798unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001799radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 unsigned long first_index, unsigned int max_items)
1801{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001802 struct radix_tree_iter iter;
1803 void **slot;
1804 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001806 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001807 return 0;
1808
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001809 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001810 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001811 if (!results[ret])
1812 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001813 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001814 slot = radix_tree_iter_retry(&iter);
1815 continue;
1816 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001817 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 return ret;
1822}
1823EXPORT_SYMBOL(radix_tree_gang_lookup);
1824
Nick Piggin47feff22008-07-25 19:45:29 -07001825/**
1826 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1827 * @root: radix tree root
1828 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001829 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001830 * @first_index: start the lookup from this key
1831 * @max_items: place up to this many items at *results
1832 *
1833 * Performs an index-ascending scan of the tree for present items. Places
1834 * their slots at *@results and returns the number of items which were
1835 * placed at *@results.
1836 *
1837 * The implementation is naive.
1838 *
1839 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1840 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1841 * protection, radix_tree_deref_slot may fail requiring a retry.
1842 */
1843unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001844radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
Hugh Dickins63286502011-08-03 16:21:18 -07001845 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001846 unsigned long first_index, unsigned int max_items)
1847{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001848 struct radix_tree_iter iter;
1849 void **slot;
1850 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001851
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001852 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001853 return 0;
1854
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001855 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1856 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001857 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001858 indices[ret] = iter.index;
1859 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001860 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001861 }
1862
1863 return ret;
1864}
1865EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867/**
1868 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1869 * based on a tag
1870 * @root: radix tree root
1871 * @results: where the results of the lookup are placed
1872 * @first_index: start the lookup from this key
1873 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001874 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 *
1876 * Performs an index-ascending scan of the tree for present items which
1877 * have the tag indexed by @tag set. Places the items at *@results and
1878 * returns the number of items which were placed at *@results.
1879 */
1880unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001881radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001882 unsigned long first_index, unsigned int max_items,
1883 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001885 struct radix_tree_iter iter;
1886 void **slot;
1887 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001889 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001890 return 0;
1891
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001892 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001893 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001894 if (!results[ret])
1895 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001896 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001897 slot = radix_tree_iter_retry(&iter);
1898 continue;
1899 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001900 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001903
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return ret;
1905}
1906EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1907
1908/**
Nick Piggin47feff22008-07-25 19:45:29 -07001909 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1910 * radix tree based on a tag
1911 * @root: radix tree root
1912 * @results: where the results of the lookup are placed
1913 * @first_index: start the lookup from this key
1914 * @max_items: place up to this many items at *results
1915 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1916 *
1917 * Performs an index-ascending scan of the tree for present items which
1918 * have the tag indexed by @tag set. Places the slots at *@results and
1919 * returns the number of slots which were placed at *@results.
1920 */
1921unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001922radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
1923 void ***results, unsigned long first_index,
1924 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001925{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001926 struct radix_tree_iter iter;
1927 void **slot;
1928 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001929
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001930 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001931 return 0;
1932
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001933 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1934 results[ret] = slot;
1935 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001936 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001937 }
1938
1939 return ret;
1940}
1941EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1942
Nick Piggin47feff22008-07-25 19:45:29 -07001943/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001944 * __radix_tree_delete_node - try to free node after clearing a slot
1945 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001946 * @node: node containing @index
Johannes Weinerea07b862017-01-06 19:21:43 -05001947 * @update_node: callback for changing leaf nodes
1948 * @private: private data to pass to @update_node
Johannes Weiner139e5612014-04-03 14:47:54 -07001949 *
1950 * After clearing the slot at @index in @node from radix tree
1951 * rooted at @root, call this function to attempt freeing the
1952 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001953 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001954void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weinerea07b862017-01-06 19:21:43 -05001955 struct radix_tree_node *node,
1956 radix_tree_update_node_t update_node,
1957 void *private)
Johannes Weiner139e5612014-04-03 14:47:54 -07001958{
Johannes Weinerea07b862017-01-06 19:21:43 -05001959 delete_node(root, node, update_node, private);
Johannes Weiner139e5612014-04-03 14:47:54 -07001960}
1961
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001962static bool __radix_tree_delete(struct radix_tree_root *root,
1963 struct radix_tree_node *node, void **slot)
1964{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001965 void *old = rcu_dereference_raw(*slot);
1966 int exceptional = radix_tree_exceptional_entry(old) ? -1 : 0;
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001967 unsigned offset = get_slot_offset(node, slot);
1968 int tag;
1969
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001970 if (is_idr(root))
1971 node_tag_set(root, node, IDR_FREE, offset);
1972 else
1973 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1974 node_tag_clear(root, node, tag, offset);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001975
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001976 replace_slot(slot, NULL, node, -1, exceptional);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001977 return node && delete_node(root, node, NULL, NULL);
1978}
1979
Johannes Weiner139e5612014-04-03 14:47:54 -07001980/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001981 * radix_tree_iter_delete - delete the entry at this iterator position
1982 * @root: radix tree root
1983 * @iter: iterator state
1984 * @slot: pointer to slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001986 * Delete the entry at the position currently pointed to by the iterator.
1987 * This may result in the current node being freed; if it is, the iterator
1988 * is advanced so that it will not reference the freed memory. This
1989 * function may be called without any locking if there are no other threads
1990 * which can access this tree.
1991 */
1992void radix_tree_iter_delete(struct radix_tree_root *root,
1993 struct radix_tree_iter *iter, void **slot)
1994{
1995 if (__radix_tree_delete(root, iter->node, slot))
1996 iter->index = iter->next_index;
1997}
1998
1999/**
2000 * radix_tree_delete_item - delete an item from a radix tree
2001 * @root: radix tree root
2002 * @index: index key
2003 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002005 * Remove @item at @index from the radix tree rooted at @root.
2006 *
2007 * Return: the deleted entry, or %NULL if it was not present
2008 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07002010void *radix_tree_delete_item(struct radix_tree_root *root,
2011 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002013 struct radix_tree_node *node = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07002014 void **slot;
2015 void *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Johannes Weiner139e5612014-04-03 14:47:54 -07002017 entry = __radix_tree_lookup(root, index, &node, &slot);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002018 if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE,
2019 get_slot_offset(node, slot))))
Johannes Weiner139e5612014-04-03 14:47:54 -07002020 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Johannes Weiner139e5612014-04-03 14:47:54 -07002022 if (item && entry != item)
2023 return NULL;
2024
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002025 __radix_tree_delete(root, node, slot);
Christoph Lameter201b6262005-09-06 15:16:46 -07002026
Johannes Weiner139e5612014-04-03 14:47:54 -07002027 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
Johannes Weiner53c59f22014-04-03 14:47:39 -07002029EXPORT_SYMBOL(radix_tree_delete_item);
2030
2031/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002032 * radix_tree_delete - delete an entry from a radix tree
2033 * @root: radix tree root
2034 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07002035 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002036 * Remove the entry at @index from the radix tree rooted at @root.
Johannes Weiner53c59f22014-04-03 14:47:39 -07002037 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002038 * Return: The deleted entry, or %NULL if it was not present.
Johannes Weiner53c59f22014-04-03 14:47:39 -07002039 */
2040void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
2041{
2042 return radix_tree_delete_item(root, index, NULL);
2043}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044EXPORT_SYMBOL(radix_tree_delete);
2045
Johannes Weinerd3798ae2016-10-04 22:02:08 +02002046void radix_tree_clear_tags(struct radix_tree_root *root,
2047 struct radix_tree_node *node,
2048 void **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002049{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002050 if (node) {
2051 unsigned int tag, offset = get_slot_offset(node, slot);
2052 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
2053 node_tag_clear(root, node, tag, offset);
2054 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002055 root_tag_clear_all(root);
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002056 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002057}
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059/**
2060 * radix_tree_tagged - test whether any items in the tree are tagged
2061 * @root: radix tree root
2062 * @tag: tag to test
2063 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05002064int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Nick Piggin612d6c12006-06-23 02:03:22 -07002066 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068EXPORT_SYMBOL(radix_tree_tagged);
2069
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002070/**
2071 * idr_preload - preload for idr_alloc()
2072 * @gfp_mask: allocation mask to use for preloading
2073 *
2074 * Preallocate memory to use for the next call to idr_alloc(). This function
2075 * returns with preemption disabled. It will be enabled by idr_preload_end().
2076 */
2077void idr_preload(gfp_t gfp_mask)
2078{
2079 __radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE);
2080}
2081EXPORT_SYMBOL(idr_preload);
2082
2083void **idr_get_free(struct radix_tree_root *root,
2084 struct radix_tree_iter *iter, gfp_t gfp, int end)
2085{
2086 struct radix_tree_node *node = NULL, *child;
2087 void **slot = (void **)&root->rnode;
2088 unsigned long maxindex, start = iter->next_index;
2089 unsigned long max = end > 0 ? end - 1 : INT_MAX;
2090 unsigned int shift, offset = 0;
2091
2092 grow:
2093 shift = radix_tree_load_root(root, &child, &maxindex);
2094 if (!radix_tree_tagged(root, IDR_FREE))
2095 start = max(start, maxindex + 1);
2096 if (start > max)
2097 return ERR_PTR(-ENOSPC);
2098
2099 if (start > maxindex) {
2100 int error = radix_tree_extend(root, gfp, start, shift);
2101 if (error < 0)
2102 return ERR_PTR(error);
2103 shift = error;
2104 child = rcu_dereference_raw(root->rnode);
2105 }
2106
2107 while (shift) {
2108 shift -= RADIX_TREE_MAP_SHIFT;
2109 if (child == NULL) {
2110 /* Have to add a child node. */
2111 child = radix_tree_node_alloc(gfp, node, shift, offset,
2112 0, 0);
2113 if (!child)
2114 return ERR_PTR(-ENOMEM);
2115 all_tag_set(child, IDR_FREE);
2116 rcu_assign_pointer(*slot, node_to_entry(child));
2117 if (node)
2118 node->count++;
2119 } else if (!radix_tree_is_internal_node(child))
2120 break;
2121
2122 node = entry_to_node(child);
2123 offset = radix_tree_descend(node, &child, start);
2124 if (!tag_get(node, IDR_FREE, offset)) {
2125 offset = radix_tree_find_next_bit(node, IDR_FREE,
2126 offset + 1);
2127 start = next_index(start, node, offset);
2128 if (start > max)
2129 return ERR_PTR(-ENOSPC);
2130 while (offset == RADIX_TREE_MAP_SIZE) {
2131 offset = node->offset + 1;
2132 node = node->parent;
2133 if (!node)
2134 goto grow;
2135 shift = node->shift;
2136 }
2137 child = rcu_dereference_raw(node->slots[offset]);
2138 }
2139 slot = &node->slots[offset];
2140 }
2141
2142 iter->index = start;
2143 if (node)
2144 iter->next_index = 1 + min(max, (start | node_maxindex(node)));
2145 else
2146 iter->next_index = 1;
2147 iter->node = node;
2148 __set_iter_shift(iter, shift);
2149 set_iter_tags(iter, node, offset, IDR_FREE);
2150
2151 return slot;
2152}
2153
2154/**
2155 * idr_destroy - release all internal memory from an IDR
2156 * @idr: idr handle
2157 *
2158 * After this function is called, the IDR is empty, and may be reused or
2159 * the data structure containing it may be freed.
2160 *
2161 * A typical clean-up sequence for objects stored in an idr tree will use
2162 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
2163 * free the memory used to keep track of those objects.
2164 */
2165void idr_destroy(struct idr *idr)
2166{
2167 struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.rnode);
2168 if (radix_tree_is_internal_node(node))
2169 radix_tree_free_nodes(node);
2170 idr->idr_rt.rnode = NULL;
2171 root_tag_set(&idr->idr_rt, IDR_FREE);
2172}
2173EXPORT_SYMBOL(idr_destroy);
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175static void
Johannes Weiner449dd692014-04-03 14:47:56 -07002176radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Johannes Weiner449dd692014-04-03 14:47:56 -07002178 struct radix_tree_node *node = arg;
2179
2180 memset(node, 0, sizeof(*node));
2181 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182}
2183
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002184static __init unsigned long __maxindex(unsigned int height)
2185{
2186 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
2187 int shift = RADIX_TREE_INDEX_BITS - width;
2188
2189 if (shift < 0)
2190 return ~0UL;
2191 if (shift >= BITS_PER_LONG)
2192 return 0UL;
2193 return ~0UL >> shift;
2194}
2195
2196static __init void radix_tree_init_maxnodes(void)
2197{
2198 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
2199 unsigned int i, j;
2200
2201 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
2202 height_to_maxindex[i] = __maxindex(i);
2203 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
2204 for (j = i; j > 0; j--)
2205 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
2206 }
2207}
2208
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002209static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002211 struct radix_tree_preload *rtp;
2212 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002214 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002215 rtp = &per_cpu(radix_tree_preloads, cpu);
2216 while (rtp->nr) {
2217 node = rtp->nodes;
2218 rtp->nodes = node->private_data;
2219 kmem_cache_free(radix_tree_node_cachep, node);
2220 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002221 }
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002222 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225void __init radix_tree_init(void)
2226{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2229 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07002230 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2231 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002232 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002233 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2234 NULL, radix_tree_cpu_dead);
2235 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}