blob: 59b28111eabc63d638356e470315332f207cce77 [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 Wilcox460488c2017-11-28 15:16:24 -050027#include <linux/bug.h>
Matthew Wilcoxe157b552016-12-14 15:09:01 -080028#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/errno.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -050030#include <linux/export.h>
31#include <linux/idr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
33#include <linux/kernel.h>
Catalin Marinasce80b062014-06-06 14:38:18 -070034#include <linux/kmemleak.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -050035#include <linux/percpu.h>
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020036#include <linux/preempt.h> /* in_interrupt() */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050037#include <linux/radix-tree.h>
38#include <linux/rcupdate.h>
39#include <linux/slab.h>
40#include <linux/string.h>
Matthew Wilcox02c02bf2017-11-03 23:09:45 -040041#include <linux/xarray.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -070044/* Number of nodes in fully populated tree of given height */
45static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
46
Jeff Moyer26fb1582007-10-16 01:24:49 -070047/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * Radix tree node cache.
49 */
Christoph Lametere18b8902006-12-06 20:33:20 -080050static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52/*
Nick Piggin55368052012-05-29 15:07:34 -070053 * The radix tree is variable-height, so an insert operation not only has
54 * to build the branch to its corresponding item, it also has to build the
55 * branch to existing items if the size has to be increased (by
56 * radix_tree_extend).
57 *
58 * The worst case is a zero height tree with just a single item at index 0,
59 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
60 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
61 * Hence:
62 */
63#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
64
65/*
Matthew Wilcox0a835c42016-12-20 10:27:56 -050066 * The IDR does not have to be as high as the radix tree since it uses
67 * signed integers, not unsigned longs.
68 */
69#define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1)
70#define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \
71 RADIX_TREE_MAP_SHIFT))
72#define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1)
73
74/*
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -050075 * The IDA is even shorter since it uses a bitmap at the last level.
76 */
77#define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS))
78#define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \
79 RADIX_TREE_MAP_SHIFT))
80#define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1)
81
82/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 * Per-cpu pool of preloaded nodes
84 */
85struct radix_tree_preload {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -070086 unsigned nr;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050087 /* nodes->parent points to next preallocated node */
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -070088 struct radix_tree_node *nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080090static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Matthew Wilcox148deab2016-12-14 15:08:49 -080092static inline struct radix_tree_node *entry_to_node(void *ptr)
93{
94 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
95}
96
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070097static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080098{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070099 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -0800100}
101
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400102#define RADIX_TREE_RETRY XA_RETRY_ENTRY
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700103
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500104static inline unsigned long
105get_slot_offset(const struct radix_tree_node *parent, void __rcu **slot)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700106{
Matthew Wilcox76f070b2018-08-18 07:05:50 -0400107 return parent ? slot - parent->slots : 0;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700108}
109
Matthew Wilcox35534c82016-12-19 17:43:19 -0500110static unsigned int radix_tree_descend(const struct radix_tree_node *parent,
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700111 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700112{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700113 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500114 void __rcu **entry = rcu_dereference_raw(parent->slots[offset]);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700115
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400116 if (xa_is_sibling(entry)) {
117 offset = xa_to_sibling(entry);
118 entry = rcu_dereference_raw(parent->slots[offset]);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700119 }
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700120
121 *nodep = (void *)entry;
122 return offset;
123}
124
Matthew Wilcox35534c82016-12-19 17:43:19 -0500125static inline gfp_t root_gfp_mask(const struct radix_tree_root *root)
Nick Piggin612d6c12006-06-23 02:03:22 -0700126{
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -0700127 return root->gfp_mask & (__GFP_BITS_MASK & ~GFP_ZONEMASK);
Nick Piggin612d6c12006-06-23 02:03:22 -0700128}
129
Nick Piggin643b52b2008-06-12 15:21:52 -0700130static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
131 int offset)
132{
133 __set_bit(offset, node->tags[tag]);
134}
135
136static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
137 int offset)
138{
139 __clear_bit(offset, node->tags[tag]);
140}
141
Matthew Wilcox35534c82016-12-19 17:43:19 -0500142static inline int tag_get(const struct radix_tree_node *node, unsigned int tag,
Nick Piggin643b52b2008-06-12 15:21:52 -0700143 int offset)
144{
145 return test_bit(offset, node->tags[tag]);
146}
147
Matthew Wilcox35534c82016-12-19 17:43:19 -0500148static inline void root_tag_set(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700149{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500150 root->gfp_mask |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700151}
152
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700153static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700154{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500155 root->gfp_mask &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700156}
157
158static inline void root_tag_clear_all(struct radix_tree_root *root)
159{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500160 root->gfp_mask &= (1 << ROOT_TAG_SHIFT) - 1;
Nick Piggin643b52b2008-06-12 15:21:52 -0700161}
162
Matthew Wilcox35534c82016-12-19 17:43:19 -0500163static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700164{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500165 return (__force int)root->gfp_mask & (1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700166}
167
Matthew Wilcox35534c82016-12-19 17:43:19 -0500168static inline unsigned root_tags_get(const struct radix_tree_root *root)
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700169{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500170 return (__force unsigned)root->gfp_mask >> ROOT_TAG_SHIFT;
171}
172
173static inline bool is_idr(const struct radix_tree_root *root)
174{
175 return !!(root->gfp_mask & ROOT_IS_IDR);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700176}
177
Nick Piggin643b52b2008-06-12 15:21:52 -0700178/*
179 * Returns 1 if any slot in the node has this tag set.
180 * Otherwise returns 0.
181 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500182static inline int any_tag_set(const struct radix_tree_node *node,
183 unsigned int tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700184{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700185 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700186 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
187 if (node->tags[tag][idx])
188 return 1;
189 }
190 return 0;
191}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700192
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500193static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag)
194{
195 bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE);
196}
197
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700198/**
199 * radix_tree_find_next_bit - find the next set bit in a memory region
200 *
201 * @addr: The address to base the search on
202 * @size: The bitmap size in bits
203 * @offset: The bitnumber to start searching at
204 *
205 * Unrollable variant of find_next_bit() for constant size arrays.
206 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
207 * Returns next bit offset, or size if nothing found.
208 */
209static __always_inline unsigned long
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800210radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
211 unsigned long offset)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700212{
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800213 const unsigned long *addr = node->tags[tag];
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700214
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800215 if (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700216 unsigned long tmp;
217
218 addr += offset / BITS_PER_LONG;
219 tmp = *addr >> (offset % BITS_PER_LONG);
220 if (tmp)
221 return __ffs(tmp) + offset;
222 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800223 while (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700224 tmp = *++addr;
225 if (tmp)
226 return __ffs(tmp) + offset;
227 offset += BITS_PER_LONG;
228 }
229 }
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800230 return RADIX_TREE_MAP_SIZE;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700231}
232
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800233static unsigned int iter_offset(const struct radix_tree_iter *iter)
234{
235 return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
236}
237
Matthew Wilcox218ed752016-12-14 15:08:43 -0800238/*
239 * The maximum index which can be stored in a radix tree
240 */
241static inline unsigned long shift_maxindex(unsigned int shift)
242{
243 return (RADIX_TREE_MAP_SIZE << shift) - 1;
244}
245
Matthew Wilcox35534c82016-12-19 17:43:19 -0500246static inline unsigned long node_maxindex(const struct radix_tree_node *node)
Matthew Wilcox218ed752016-12-14 15:08:43 -0800247{
248 return shift_maxindex(node->shift);
249}
250
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500251static unsigned long next_index(unsigned long index,
252 const struct radix_tree_node *node,
253 unsigned long offset)
254{
255 return (index & ~node_maxindex(node)) + (offset << node->shift);
256}
257
Ross Zwisler0796c582016-05-20 17:02:55 -0700258#ifndef __KERNEL__
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700259static void dump_node(struct radix_tree_node *node, unsigned long index)
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700260{
Ross Zwisler0796c582016-05-20 17:02:55 -0700261 unsigned long i;
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700262
Matthew Wilcox218ed752016-12-14 15:08:43 -0800263 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
264 node, node->offset, index, index | node_maxindex(node),
265 node->parent,
Ross Zwisler0796c582016-05-20 17:02:55 -0700266 node->tags[0][0], node->tags[1][0], node->tags[2][0],
Matthew Wilcox218ed752016-12-14 15:08:43 -0800267 node->shift, node->count, node->exceptional);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700268
Ross Zwisler0796c582016-05-20 17:02:55 -0700269 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700270 unsigned long first = index | (i << node->shift);
271 unsigned long last = first | ((1UL << node->shift) - 1);
Ross Zwisler0796c582016-05-20 17:02:55 -0700272 void *entry = node->slots[i];
273 if (!entry)
274 continue;
Matthew Wilcox218ed752016-12-14 15:08:43 -0800275 if (entry == RADIX_TREE_RETRY) {
276 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
277 i, first, last, node);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700278 } else if (!radix_tree_is_internal_node(entry)) {
Matthew Wilcox218ed752016-12-14 15:08:43 -0800279 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
280 entry, i, first, last, node);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400281 } else if (xa_is_sibling(entry)) {
Matthew Wilcox218ed752016-12-14 15:08:43 -0800282 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
283 entry, i, first, last, node,
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400284 node->slots[xa_to_sibling(entry)]);
Ross Zwisler0796c582016-05-20 17:02:55 -0700285 } else {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700286 dump_node(entry_to_node(entry), first);
Ross Zwisler0796c582016-05-20 17:02:55 -0700287 }
288 }
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700289}
290
291/* For debug */
292static void radix_tree_dump(struct radix_tree_root *root)
293{
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700294 pr_debug("radix root: %p rnode %p tags %x\n",
295 root, root->rnode,
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500296 root->gfp_mask >> ROOT_TAG_SHIFT);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700297 if (!radix_tree_is_internal_node(root->rnode))
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700298 return;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700299 dump_node(entry_to_node(root->rnode), 0);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700300}
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500301
302static void dump_ida_node(void *entry, unsigned long index)
303{
304 unsigned long i;
305
306 if (!entry)
307 return;
308
309 if (radix_tree_is_internal_node(entry)) {
310 struct radix_tree_node *node = entry_to_node(entry);
311
312 pr_debug("ida node: %p offset %d indices %lu-%lu parent %p free %lx shift %d count %d\n",
313 node, node->offset, index * IDA_BITMAP_BITS,
314 ((index | node_maxindex(node)) + 1) *
315 IDA_BITMAP_BITS - 1,
316 node->parent, node->tags[0][0], node->shift,
317 node->count);
318 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
319 dump_ida_node(node->slots[i],
320 index | (i << node->shift));
Matthew Wilcox3159f942017-11-03 13:30:42 -0400321 } else if (xa_is_value(entry)) {
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500322 pr_debug("ida excp: %p offset %d indices %lu-%lu data %lx\n",
323 entry, (int)(index & RADIX_TREE_MAP_MASK),
324 index * IDA_BITMAP_BITS,
Matthew Wilcox3159f942017-11-03 13:30:42 -0400325 index * IDA_BITMAP_BITS + BITS_PER_XA_VALUE,
326 xa_to_value(entry));
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500327 } else {
328 struct ida_bitmap *bitmap = entry;
329
330 pr_debug("ida btmp: %p offset %d indices %lu-%lu data", bitmap,
331 (int)(index & RADIX_TREE_MAP_MASK),
332 index * IDA_BITMAP_BITS,
333 (index + 1) * IDA_BITMAP_BITS - 1);
334 for (i = 0; i < IDA_BITMAP_LONGS; i++)
335 pr_cont(" %lx", bitmap->bitmap[i]);
336 pr_cont("\n");
337 }
338}
339
340static void ida_dump(struct ida *ida)
341{
342 struct radix_tree_root *root = &ida->ida_rt;
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500343 pr_debug("ida: %p node %p free %d\n", ida, root->rnode,
344 root->gfp_mask >> ROOT_TAG_SHIFT);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500345 dump_ida_node(root->rnode, 0);
346}
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700347#endif
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/*
350 * This assumes that the caller has performed appropriate preallocation, and
351 * that the caller has pinned this thread of control to the current CPU.
352 */
353static struct radix_tree_node *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500354radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500355 struct radix_tree_root *root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800356 unsigned int shift, unsigned int offset,
357 unsigned int count, unsigned int exceptional)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Nick Piggine2848a02008-02-04 22:29:10 -0800359 struct radix_tree_node *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jan Kara5e4c0d972013-09-11 14:26:05 -0700361 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700362 * Preload code isn't irq safe and it doesn't make sense to use
363 * preloading during an interrupt anyway as all the allocations have
364 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700365 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800366 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct radix_tree_preload *rtp;
368
Nick Piggine2848a02008-02-04 22:29:10 -0800369 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700370 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700371 * cache first for the new node to get accounted to the memory
372 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700373 */
374 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700375 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700376 if (ret)
377 goto out;
378
379 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800380 * Provided the caller has preloaded here, we will always
381 * succeed in getting a node here (and never reach
382 * kmem_cache_alloc)
383 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700384 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700386 ret = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500387 rtp->nodes = ret->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 rtp->nr--;
389 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700390 /*
391 * Update the allocation stack trace as this is more useful
392 * for debugging.
393 */
394 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700397 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700398out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700399 BUG_ON(radix_tree_is_internal_node(ret));
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800400 if (ret) {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800401 ret->shift = shift;
402 ret->offset = offset;
403 ret->count = count;
404 ret->exceptional = exceptional;
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500405 ret->parent = parent;
406 ret->root = root;
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 return ret;
409}
410
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800411static void radix_tree_node_rcu_free(struct rcu_head *head)
412{
413 struct radix_tree_node *node =
414 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700415
416 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800417 * Must only free zeroed nodes into the slab. We can be left with
418 * non-NULL entries by radix_tree_free_nodes, so clear the entries
419 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700420 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800421 memset(node->slots, 0, sizeof(node->slots));
422 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800423 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700424
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800425 kmem_cache_free(radix_tree_node_cachep, node);
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428static inline void
429radix_tree_node_free(struct radix_tree_node *node)
430{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800431 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/*
435 * Load up this CPU's radix_tree_node buffer with sufficient objects to
436 * ensure that the addition of a single element in the tree cannot fail. On
437 * success, return zero, with preemption disabled. On error, return -ENOMEM
438 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000439 *
440 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800441 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -0700443static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 struct radix_tree_preload *rtp;
446 struct radix_tree_node *node;
447 int ret = -ENOMEM;
448
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700449 /*
450 * Nodes preloaded by one cgroup can be be used by another cgroup, so
451 * they should never be accounted to any particular memory cgroup.
452 */
453 gfp_mask &= ~__GFP_ACCOUNT;
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700456 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700457 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700459 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (node == NULL)
461 goto out;
462 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700463 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700464 if (rtp->nr < nr) {
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500465 node->parent = rtp->nodes;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700466 rtp->nodes = node;
467 rtp->nr++;
468 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472 ret = 0;
473out:
474 return ret;
475}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700476
477/*
478 * Load up this CPU's radix_tree_node buffer with sufficient objects to
479 * ensure that the addition of a single element in the tree cannot fail. On
480 * success, return zero, with preemption disabled. On error, return -ENOMEM
481 * with preemption not disabled.
482 *
483 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800484 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700485 */
486int radix_tree_preload(gfp_t gfp_mask)
487{
488 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800489 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700490 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700491}
David Chinnerd7f09232007-07-14 16:05:04 +1000492EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Nick Piggin6e954b92006-01-08 01:01:40 -0800494/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700495 * The same as above function, except we don't guarantee preloading happens.
496 * We do it, if we decide it helps. On success, return zero with preemption
497 * disabled. On error, return -ENOMEM with preemption not disabled.
498 */
499int radix_tree_maybe_preload(gfp_t gfp_mask)
500{
Mel Gormand0164ad2015-11-06 16:28:21 -0800501 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700502 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700503 /* Preloading doesn't help anything with this gfp mask, skip it */
504 preempt_disable();
505 return 0;
506}
507EXPORT_SYMBOL(radix_tree_maybe_preload);
508
Matthew Wilcox27916532016-12-14 15:09:04 -0800509#ifdef CONFIG_RADIX_TREE_MULTIORDER
510/*
511 * Preload with enough objects to ensure that we can split a single entry
512 * of order @old_order into many entries of size @new_order
513 */
514int radix_tree_split_preload(unsigned int old_order, unsigned int new_order,
515 gfp_t gfp_mask)
516{
517 unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT);
518 unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) -
519 (new_order / RADIX_TREE_MAP_SHIFT);
520 unsigned nr = 0;
521
522 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
523 BUG_ON(new_order >= old_order);
524
525 while (layers--)
526 nr = nr * RADIX_TREE_MAP_SIZE + 1;
527 return __radix_tree_preload(gfp_mask, top * nr);
528}
529#endif
530
Jan Kara5e4c0d972013-09-11 14:26:05 -0700531/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700532 * The same as function above, but preload number of nodes required to insert
533 * (1 << order) continuous naturally-aligned elements.
534 */
535int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
536{
537 unsigned long nr_subtrees;
538 int nr_nodes, subtree_height;
539
540 /* Preloading doesn't help anything with this gfp mask, skip it */
541 if (!gfpflags_allow_blocking(gfp_mask)) {
542 preempt_disable();
543 return 0;
544 }
545
546 /*
547 * Calculate number and height of fully populated subtrees it takes to
548 * store (1 << order) elements.
549 */
550 nr_subtrees = 1 << order;
551 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
552 subtree_height++)
553 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
554
555 /*
556 * The worst case is zero height tree with a single item at index 0 and
557 * then inserting items starting at ULONG_MAX - (1 << order).
558 *
559 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
560 * 0-index item.
561 */
562 nr_nodes = RADIX_TREE_MAX_PATH;
563
564 /* Plus branch to fully populated subtrees. */
565 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
566
567 /* Root node is shared. */
568 nr_nodes--;
569
570 /* Plus nodes required to build subtrees. */
571 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
572
573 return __radix_tree_preload(gfp_mask, nr_nodes);
574}
575
Matthew Wilcox35534c82016-12-19 17:43:19 -0500576static unsigned radix_tree_load_root(const struct radix_tree_root *root,
Matthew Wilcox1456a432016-05-20 17:02:08 -0700577 struct radix_tree_node **nodep, unsigned long *maxindex)
578{
579 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
580
581 *nodep = node;
582
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700583 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700584 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700585 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700586 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700587 }
588
589 *maxindex = 0;
590 return 0;
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * Extend a radix tree so it can store key @index.
595 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500596static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700597 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500599 void *entry;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700600 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 int tag;
602
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700603 /* Figure out what the shift should be. */
604 maxshift = shift;
605 while (index > shift_maxindex(maxshift))
606 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500608 entry = rcu_dereference_raw(root->rnode);
609 if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 do {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500613 struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500614 root, shift, 0, 1, 0);
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700615 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -ENOMEM;
617
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500618 if (is_idr(root)) {
619 all_tag_set(node, IDR_FREE);
620 if (!root_tag_get(root, IDR_FREE)) {
621 tag_clear(node, IDR_FREE, 0);
622 root_tag_set(root, IDR_FREE);
623 }
624 } else {
625 /* Propagate the aggregated tag info to the new child */
626 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
627 if (root_tag_get(root, tag))
628 tag_set(node, tag, 0);
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700632 BUG_ON(shift > BITS_PER_LONG);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500633 if (radix_tree_is_internal_node(entry)) {
634 entry_to_node(entry)->parent = node;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400635 } else if (xa_is_value(entry)) {
Johannes Weinerf7942432016-12-12 16:43:41 -0800636 /* Moving an exceptional root->rnode to a node */
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800637 node->exceptional = 1;
Johannes Weinerf7942432016-12-12 16:43:41 -0800638 }
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500639 /*
640 * entry was already in the radix tree, so we do not need
641 * rcu_assign_pointer here
642 */
643 node->slots[0] = (void __rcu *)entry;
644 entry = node_to_entry(node);
645 rcu_assign_pointer(root->rnode, entry);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700646 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700647 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700649 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
652/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800653 * radix_tree_shrink - shrink radix tree to minimum height
654 * @root radix tree root
655 */
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500656static inline bool radix_tree_shrink(struct radix_tree_root *root,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800657 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800658{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500659 bool shrunk = false;
660
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800661 for (;;) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500662 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800663 struct radix_tree_node *child;
664
665 if (!radix_tree_is_internal_node(node))
666 break;
667 node = entry_to_node(node);
668
669 /*
670 * The candidate node has more than one child, or its child
671 * is not at the leftmost slot, or the child is a multiorder
672 * entry, we cannot shrink.
673 */
674 if (node->count != 1)
675 break;
Matthew Wilcox12320d02017-02-13 15:22:48 -0500676 child = rcu_dereference_raw(node->slots[0]);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800677 if (!child)
678 break;
679 if (!radix_tree_is_internal_node(child) && node->shift)
680 break;
681
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400682 /*
683 * For an IDR, we must not shrink entry 0 into the root in
684 * case somebody calls idr_replace() with a pointer that
685 * appears to be an internal entry
686 */
687 if (!node->shift && is_idr(root))
688 break;
689
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800690 if (radix_tree_is_internal_node(child))
691 entry_to_node(child)->parent = NULL;
692
693 /*
694 * We don't need rcu_assign_pointer(), since we are simply
695 * moving the node from one part of the tree to another: if it
696 * was safe to dereference the old pointer to it
697 * (node->slots[0]), it will be safe to dereference the new
698 * one (root->rnode) as far as dependent read barriers go.
699 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500700 root->rnode = (void __rcu *)child;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500701 if (is_idr(root) && !tag_get(node, IDR_FREE, 0))
702 root_tag_clear(root, IDR_FREE);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800703
704 /*
705 * We have a dilemma here. The node's slot[0] must not be
706 * NULLed in case there are concurrent lookups expecting to
707 * find the item. However if this was a bottom-level node,
708 * then it may be subject to the slot pointer being visible
709 * to callers dereferencing it. If item corresponding to
710 * slot[0] is subsequently deleted, these callers would expect
711 * their slot to become empty sooner or later.
712 *
713 * For example, lockless pagecache will look up a slot, deref
714 * the page pointer, and if the page has 0 refcount it means it
715 * was concurrently deleted from pagecache so try the deref
716 * again. Fortunately there is already a requirement for logic
717 * to retry the entire slot lookup -- the indirect pointer
718 * problem (replacing direct root node with an indirect pointer
719 * also results in a stale slot). So tag the slot as indirect
720 * to force callers to retry.
721 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800722 node->count = 0;
723 if (!radix_tree_is_internal_node(child)) {
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500724 node->slots[0] = (void __rcu *)RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800725 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800726 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800727 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800728
Johannes Weinerea07b862017-01-06 19:21:43 -0500729 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800730 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500731 shrunk = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800732 }
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500733
734 return shrunk;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800735}
736
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500737static bool delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800738 struct radix_tree_node *node,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800739 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800740{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500741 bool deleted = false;
742
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800743 do {
744 struct radix_tree_node *parent;
745
746 if (node->count) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500747 if (node_to_entry(node) ==
748 rcu_dereference_raw(root->rnode))
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800749 deleted |= radix_tree_shrink(root,
750 update_node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500751 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800752 }
753
754 parent = node->parent;
755 if (parent) {
756 parent->slots[node->offset] = NULL;
757 parent->count--;
758 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500759 /*
760 * Shouldn't the tags already have all been cleared
761 * by the caller?
762 */
763 if (!is_idr(root))
764 root_tag_clear_all(root);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800765 root->rnode = NULL;
766 }
767
Johannes Weinerea07b862017-01-06 19:21:43 -0500768 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800769 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500770 deleted = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800771
772 node = parent;
773 } while (node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500774
775 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800776}
777
778/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700779 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 * @root: radix tree root
781 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700782 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700783 * @nodep: returns node
784 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700786 * Create, if necessary, and return the node and slot for an item
787 * at position @index in the radix tree @root.
788 *
789 * Until there is more than one item in the tree, no nodes are
790 * allocated and @root->rnode is used as a direct slot instead of
791 * pointing to a node, in which case *@nodep will be NULL.
792 *
793 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700795int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700796 unsigned order, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500797 void __rcu ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700799 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500800 void __rcu **slot = (void __rcu **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700801 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700802 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700803 unsigned long max = index | ((1UL << order) - 1);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500804 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700805
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700806 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800809 if (order > 0 && max == ((1UL << order) - 1))
810 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700811 if (max > maxindex) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500812 int error = radix_tree_extend(root, gfp, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700813 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700815 shift = error;
Matthew Wilcox12320d02017-02-13 15:22:48 -0500816 child = rcu_dereference_raw(root->rnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700819 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700820 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700821 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500823 child = radix_tree_node_alloc(gfp, node, root, shift,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800824 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700825 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700827 rcu_assign_pointer(*slot, node_to_entry(child));
828 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700830 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700831 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700834 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700835 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700836 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700837 }
838
Johannes Weiner139e5612014-04-03 14:47:54 -0700839 if (nodep)
840 *nodep = node;
841 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700842 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700843 return 0;
844}
845
Matthew Wilcox175542f2016-12-14 15:08:58 -0800846/*
847 * Free any nodes below this node. The tree is presumed to not need
848 * shrinking, and any user data in the tree is presumed to not need a
849 * destructor called on it. If we need to add a destructor, we can
850 * add that functionality later. Note that we may not clear tags or
851 * slots from the tree as an RCU walker may still have a pointer into
852 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
853 * but we'll still have to clear those in rcu_free.
854 */
855static void radix_tree_free_nodes(struct radix_tree_node *node)
856{
857 unsigned offset = 0;
858 struct radix_tree_node *child = entry_to_node(node);
859
860 for (;;) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500861 void *entry = rcu_dereference_raw(child->slots[offset]);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400862 if (xa_is_node(entry) && child->shift) {
Matthew Wilcox175542f2016-12-14 15:08:58 -0800863 child = entry_to_node(entry);
864 offset = 0;
865 continue;
866 }
867 offset++;
868 while (offset == RADIX_TREE_MAP_SIZE) {
869 struct radix_tree_node *old = child;
870 offset = child->offset + 1;
871 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800872 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800873 radix_tree_node_free(old);
874 if (old == entry_to_node(node))
875 return;
876 }
877 }
878}
879
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500880#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500881static inline int insert_entries(struct radix_tree_node *node,
882 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800883{
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400884 void *sibling;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800885 unsigned i, n, tag, offset, tags = 0;
886
887 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800888 if (order > node->shift)
889 n = 1 << (order - node->shift);
890 else
891 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800892 offset = get_slot_offset(node, slot);
893 } else {
894 n = 1;
895 offset = 0;
896 }
897
898 if (n > 1) {
899 offset = offset & ~(n - 1);
900 slot = &node->slots[offset];
901 }
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400902 sibling = xa_mk_sibling(offset);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800903
904 for (i = 0; i < n; i++) {
905 if (slot[i]) {
906 if (replace) {
907 node->count--;
908 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
909 if (tag_get(node, tag, offset + i))
910 tags |= 1 << tag;
911 } else
912 return -EEXIST;
913 }
914 }
915
916 for (i = 0; i < n; i++) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500917 struct radix_tree_node *old = rcu_dereference_raw(slot[i]);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800918 if (i) {
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400919 rcu_assign_pointer(slot[i], sibling);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800920 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
921 if (tags & (1 << tag))
922 tag_clear(node, tag, offset + i);
923 } else {
924 rcu_assign_pointer(slot[i], item);
925 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
926 if (tags & (1 << tag))
927 tag_set(node, tag, offset);
928 }
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400929 if (xa_is_node(old))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800930 radix_tree_free_nodes(old);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400931 if (xa_is_value(old))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800932 node->exceptional--;
933 }
934 if (node) {
935 node->count += n;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400936 if (xa_is_value(item))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800937 node->exceptional += n;
938 }
939 return n;
940}
941#else
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500942static inline int insert_entries(struct radix_tree_node *node,
943 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800944{
945 if (*slot)
946 return -EEXIST;
947 rcu_assign_pointer(*slot, item);
948 if (node) {
949 node->count++;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400950 if (xa_is_value(item))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800951 node->exceptional++;
952 }
953 return 1;
954}
955#endif
956
Johannes Weiner139e5612014-04-03 14:47:54 -0700957/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700958 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700959 * @root: radix tree root
960 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700961 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700962 * @item: item to insert
963 *
964 * Insert an item into the radix tree at position @index.
965 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700966int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
967 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700968{
969 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500970 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700971 int error;
972
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700973 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700974
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700975 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700976 if (error)
977 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800978
979 error = insert_entries(node, slot, item, order, false);
980 if (error < 0)
981 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700982
Nick Piggin612d6c12006-06-23 02:03:22 -0700983 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700984 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700985 BUG_ON(tag_get(node, 0, offset));
986 BUG_ON(tag_get(node, 1, offset));
987 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700988 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700989 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return 0;
993}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700994EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Johannes Weiner139e5612014-04-03 14:47:54 -0700996/**
997 * __radix_tree_lookup - lookup an item in a radix tree
998 * @root: radix tree root
999 * @index: index key
1000 * @nodep: returns node
1001 * @slotp: returns slot
1002 *
1003 * Lookup and return the item at position @index in the radix
1004 * tree @root.
1005 *
1006 * Until there is more than one item in the tree, no nodes are
1007 * allocated and @root->rnode is used as a direct slot instead of
1008 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -08001009 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001010void *__radix_tree_lookup(const struct radix_tree_root *root,
1011 unsigned long index, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001012 void __rcu ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -08001013{
Johannes Weiner139e5612014-04-03 14:47:54 -07001014 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -07001015 unsigned long maxindex;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001016 void __rcu **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001017
Matthew Wilcox85829952016-05-20 17:02:20 -07001018 restart:
1019 parent = NULL;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001020 slot = (void __rcu **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001021 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -07001022 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001023 return NULL;
1024
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001025 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -07001026 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -07001027
Matthew Wilcox85829952016-05-20 17:02:20 -07001028 if (node == RADIX_TREE_RETRY)
1029 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001030 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001031 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -07001032 slot = parent->slots + offset;
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001033 if (parent->shift == 0)
1034 break;
Matthew Wilcox85829952016-05-20 17:02:20 -07001035 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001036
Johannes Weiner139e5612014-04-03 14:47:54 -07001037 if (nodep)
1038 *nodep = parent;
1039 if (slotp)
1040 *slotp = slot;
1041 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -07001042}
1043
1044/**
1045 * radix_tree_lookup_slot - lookup a slot in a radix tree
1046 * @root: radix tree root
1047 * @index: index key
1048 *
1049 * Returns: the slot corresponding to the position @index in the
1050 * radix tree @root. This is useful for update-if-exists operations.
1051 *
1052 * This function can be called under rcu_read_lock iff the slot is not
1053 * modified by radix_tree_replace_slot, otherwise it must be called
1054 * exclusive from other writers. Any dereference of the slot must be done
1055 * using radix_tree_deref_slot.
1056 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001057void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001058 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -07001059{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001060 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -07001061
1062 if (!__radix_tree_lookup(root, index, NULL, &slot))
1063 return NULL;
1064 return slot;
Hans Reisera4331362005-11-07 00:59:29 -08001065}
1066EXPORT_SYMBOL(radix_tree_lookup_slot);
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/**
1069 * radix_tree_lookup - perform lookup operation on a radix tree
1070 * @root: radix tree root
1071 * @index: index key
1072 *
1073 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001074 *
1075 * This function can be called under rcu_read_lock, however the caller
1076 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
1077 * them safely). No RCU barriers are required to access or modify the
1078 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001080void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Johannes Weiner139e5612014-04-03 14:47:54 -07001082 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084EXPORT_SYMBOL(radix_tree_lookup);
1085
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001086static inline void replace_sibling_entries(struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001087 void __rcu **slot, int count, int exceptional)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001088{
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001089#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001090 unsigned offset = get_slot_offset(node, slot);
1091 void *ptr = xa_mk_sibling(offset);
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001092
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001093 while (++offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001094 if (rcu_dereference_raw(node->slots[offset]) != ptr)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001095 break;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001096 if (count < 0) {
1097 node->slots[offset] = NULL;
1098 node->count--;
1099 }
1100 node->exceptional += exceptional;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001101 }
1102#endif
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001103}
1104
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001105static void replace_slot(void __rcu **slot, void *item,
1106 struct radix_tree_node *node, int count, int exceptional)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001107{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001108 if (node && (count || exceptional)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001109 node->count += count;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001110 node->exceptional += exceptional;
1111 replace_sibling_entries(node, slot, count, exceptional);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001112 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001113
1114 rcu_assign_pointer(*slot, item);
1115}
1116
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001117static bool node_tag_get(const struct radix_tree_root *root,
1118 const struct radix_tree_node *node,
1119 unsigned int tag, unsigned int offset)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001120{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001121 if (node)
1122 return tag_get(node, tag, offset);
1123 return root_tag_get(root, tag);
1124}
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001125
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001126/*
1127 * IDR users want to be able to store NULL in the tree, so if the slot isn't
1128 * free, don't adjust the count, even if it's transitioning between NULL and
1129 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
1130 * have empty bits, but it only stores NULL in slots when they're being
1131 * deleted.
1132 */
1133static int calculate_count(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001134 struct radix_tree_node *node, void __rcu **slot,
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001135 void *item, void *old)
1136{
1137 if (is_idr(root)) {
1138 unsigned offset = get_slot_offset(node, slot);
1139 bool free = node_tag_get(root, node, IDR_FREE, offset);
1140 if (!free)
1141 return 0;
1142 if (!old)
1143 return 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001144 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001145 return !!item - !!old;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/**
Johannes Weinerf7942432016-12-12 16:43:41 -08001149 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -08001150 * @root: radix tree root
1151 * @node: pointer to tree node
1152 * @slot: pointer to slot in @node
1153 * @item: new item to store in the slot.
1154 * @update_node: callback for changing leaf nodes
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,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001161 void __rcu **slot, void *item,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001162 radix_tree_update_node_t update_node)
Johannes Weinerf7942432016-12-12 16:43:41 -08001163{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001164 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001165 int exceptional = !!xa_is_value(item) - !!xa_is_value(old);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001166 int count = calculate_count(root, node, slot, item, old);
1167
Johannes Weiner6d75f362016-12-12 16:43:43 -08001168 /*
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001169 * This function supports replacing exceptional entries and
1170 * deleting entries, but that needs accounting against the
1171 * node unless the slot is root->rnode.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001172 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001173 WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->rnode) &&
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001174 (count || exceptional));
1175 replace_slot(slot, item, node, count, exceptional);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001176
Johannes Weiner4d693d02016-12-12 16:43:49 -08001177 if (!node)
1178 return;
1179
1180 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001181 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -08001182
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001183 delete_node(root, node, update_node);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001184}
Johannes Weinerf7942432016-12-12 16:43:41 -08001185
Johannes Weiner6d75f362016-12-12 16:43:43 -08001186/**
1187 * radix_tree_replace_slot - replace item in a slot
1188 * @root: radix tree root
1189 * @slot: pointer to slot
1190 * @item: new item to store in the slot.
1191 *
1192 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1193 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1194 * across slot lookup and replacement.
1195 *
1196 * NOTE: This cannot be used to switch between non-entries (empty slots),
1197 * regular entries, and exceptional entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001198 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001199 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1200 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001201 */
1202void radix_tree_replace_slot(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001203 void __rcu **slot, void *item)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001204{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001205 __radix_tree_replace(root, NULL, slot, item, NULL);
Johannes Weinerf7942432016-12-12 16:43:41 -08001206}
Song Liu10257d72017-01-11 10:00:51 -08001207EXPORT_SYMBOL(radix_tree_replace_slot);
Johannes Weinerf7942432016-12-12 16:43:41 -08001208
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,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001219 const struct radix_tree_iter *iter,
1220 void __rcu **slot, void *item)
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001221{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001222 __radix_tree_replace(root, iter->node, slot, item, NULL);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001223}
1224
Matthew Wilcox175542f2016-12-14 15:08:58 -08001225#ifdef CONFIG_RADIX_TREE_MULTIORDER
1226/**
1227 * radix_tree_join - replace multiple entries with one multiorder entry
1228 * @root: radix tree root
1229 * @index: an index inside the new entry
1230 * @order: order of the new entry
1231 * @item: new entry
1232 *
1233 * Call this function to replace several entries with one larger entry.
1234 * The existing entries are presumed to not need freeing as a result of
1235 * this call.
1236 *
1237 * The replacement entry will have all the tags set on it that were set
1238 * on any of the entries it is replacing.
1239 */
1240int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1241 unsigned order, void *item)
1242{
1243 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001244 void __rcu **slot;
Matthew Wilcox175542f2016-12-14 15:08:58 -08001245 int error;
1246
1247 BUG_ON(radix_tree_is_internal_node(item));
1248
1249 error = __radix_tree_create(root, index, order, &node, &slot);
1250 if (!error)
1251 error = insert_entries(node, slot, item, order, true);
1252 if (error > 0)
1253 error = 0;
1254
1255 return error;
1256}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001257
1258/**
1259 * radix_tree_split - Split an entry into smaller entries
1260 * @root: radix tree root
1261 * @index: An index within the large entry
1262 * @order: Order of new entries
1263 *
1264 * Call this function as the first step in replacing a multiorder entry
1265 * with several entries of lower order. After this function returns,
1266 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1267 * and call radix_tree_iter_replace() to set up each new entry.
1268 *
1269 * The tags from this entry are replicated to all the new entries.
1270 *
1271 * The radix tree should be locked against modification during the entire
1272 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1273 * should prompt RCU walkers to restart the lookup from the root.
1274 */
1275int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1276 unsigned order)
1277{
1278 struct radix_tree_node *parent, *node, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001279 void __rcu **slot;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001280 unsigned int offset, end;
1281 unsigned n, tag, tags = 0;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001282 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001283
1284 if (!__radix_tree_lookup(root, index, &parent, &slot))
1285 return -ENOENT;
1286 if (!parent)
1287 return -ENOENT;
1288
1289 offset = get_slot_offset(parent, slot);
1290
1291 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1292 if (tag_get(parent, tag, offset))
1293 tags |= 1 << tag;
1294
1295 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001296 if (!xa_is_sibling(rcu_dereference_raw(parent->slots[end])))
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001297 break;
1298 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1299 if (tags & (1 << tag))
1300 tag_set(parent, tag, end);
1301 /* rcu_assign_pointer ensures tags are set before RETRY */
1302 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1303 }
1304 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
1305 parent->exceptional -= (end - offset);
1306
1307 if (order == parent->shift)
1308 return 0;
1309 if (order > parent->shift) {
1310 while (offset < end)
1311 offset += insert_entries(parent, &parent->slots[offset],
1312 RADIX_TREE_RETRY, order, true);
1313 return 0;
1314 }
1315
1316 node = parent;
1317
1318 for (;;) {
1319 if (node->shift > order) {
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05001320 child = radix_tree_node_alloc(gfp, node, root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -08001321 node->shift - RADIX_TREE_MAP_SHIFT,
1322 offset, 0, 0);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001323 if (!child)
1324 goto nomem;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001325 if (node != parent) {
1326 node->count++;
Matthew Wilcox12320d02017-02-13 15:22:48 -05001327 rcu_assign_pointer(node->slots[offset],
1328 node_to_entry(child));
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001329 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1330 if (tags & (1 << tag))
1331 tag_set(node, tag, offset);
1332 }
1333
1334 node = child;
1335 offset = 0;
1336 continue;
1337 }
1338
1339 n = insert_entries(node, &node->slots[offset],
1340 RADIX_TREE_RETRY, order, false);
1341 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1342
1343 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1344 if (tags & (1 << tag))
1345 tag_set(node, tag, offset);
1346 offset += n;
1347
1348 while (offset == RADIX_TREE_MAP_SIZE) {
1349 if (node == parent)
1350 break;
1351 offset = node->offset;
1352 child = node;
1353 node = node->parent;
1354 rcu_assign_pointer(node->slots[offset],
1355 node_to_entry(child));
1356 offset++;
1357 }
1358 if ((node == parent) && (offset == end))
1359 return 0;
1360 }
1361
1362 nomem:
1363 /* Shouldn't happen; did user forget to preload? */
1364 /* TODO: free all the allocated nodes */
1365 WARN_ON(1);
1366 return -ENOMEM;
1367}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001368#endif
1369
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001370static void node_tag_set(struct radix_tree_root *root,
1371 struct radix_tree_node *node,
1372 unsigned int tag, unsigned int offset)
1373{
1374 while (node) {
1375 if (tag_get(node, tag, offset))
1376 return;
1377 tag_set(node, tag, offset);
1378 offset = node->offset;
1379 node = node->parent;
1380 }
1381
1382 if (!root_tag_get(root, tag))
1383 root_tag_set(root, tag);
1384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/**
1387 * radix_tree_tag_set - set a tag on a radix tree node
1388 * @root: radix tree root
1389 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001390 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001392 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1393 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 * the root all the way down to the leaf node.
1395 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001396 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 * item is a bug.
1398 */
1399void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001400 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Ross Zwislerfb969902016-05-20 17:02:32 -07001402 struct radix_tree_node *node, *parent;
1403 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001405 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001406 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001408 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001409 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001411 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001412 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001413 BUG_ON(!node);
1414
1415 if (!tag_get(parent, tag, offset))
1416 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418
Nick Piggin612d6c12006-06-23 02:03:22 -07001419 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001420 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001421 root_tag_set(root, tag);
1422
Ross Zwislerfb969902016-05-20 17:02:32 -07001423 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425EXPORT_SYMBOL(radix_tree_tag_set);
1426
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001427/**
1428 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1429 * @root: radix tree root
1430 * @iter: iterator state
1431 * @tag: tag to set
1432 */
1433void radix_tree_iter_tag_set(struct radix_tree_root *root,
1434 const struct radix_tree_iter *iter, unsigned int tag)
1435{
1436 node_tag_set(root, iter->node, tag, iter_offset(iter));
1437}
1438
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001439static void node_tag_clear(struct radix_tree_root *root,
1440 struct radix_tree_node *node,
1441 unsigned int tag, unsigned int offset)
1442{
1443 while (node) {
1444 if (!tag_get(node, tag, offset))
1445 return;
1446 tag_clear(node, tag, offset);
1447 if (any_tag_set(node, tag))
1448 return;
1449
1450 offset = node->offset;
1451 node = node->parent;
1452 }
1453
1454 /* clear the root's tag bit */
1455 if (root_tag_get(root, tag))
1456 root_tag_clear(root, tag);
1457}
1458
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001459/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 * radix_tree_tag_clear - clear a tag on a radix tree node
1461 * @root: radix tree root
1462 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001463 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001465 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001466 * corresponding to @index in the radix tree. If this causes
1467 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 * next-to-leaf node, etc.
1469 *
1470 * Returns the address of the tagged item on success, else NULL. ie:
1471 * has the same return value and semantics as radix_tree_lookup().
1472 */
1473void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001474 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001476 struct radix_tree_node *node, *parent;
1477 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001478 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001480 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001481 if (index > maxindex)
1482 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Ross Zwisler00f47b52016-05-20 17:02:35 -07001484 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001486 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001487 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001488 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
1490
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001491 if (node)
1492 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Ross Zwisler00f47b52016-05-20 17:02:35 -07001494 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496EXPORT_SYMBOL(radix_tree_tag_clear);
1497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001499 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1500 * @root: radix tree root
1501 * @iter: iterator state
1502 * @tag: tag to clear
1503 */
1504void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1505 const struct radix_tree_iter *iter, unsigned int tag)
1506{
1507 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1508}
1509
1510/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001511 * radix_tree_tag_get - get a tag on a radix tree node
1512 * @root: radix tree root
1513 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001514 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001516 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001518 * 0: tag not present or not set
1519 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001520 *
1521 * Note that the return value of this function may not be relied on, even if
1522 * the RCU lock is held, unless tag modification and node deletion are excluded
1523 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001525int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001526 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001528 struct radix_tree_node *node, *parent;
1529 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Nick Piggin612d6c12006-06-23 02:03:22 -07001531 if (!root_tag_get(root, tag))
1532 return 0;
1533
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001534 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001535 if (index > maxindex)
1536 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001537
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001538 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001539 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001540
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001541 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001542 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001543
Ross Zwisler4589ba62016-05-20 17:02:38 -07001544 if (!tag_get(parent, tag, offset))
1545 return 0;
1546 if (node == RADIX_TREE_RETRY)
1547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001549
1550 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Ross Zwisler21ef5332016-05-20 17:02:26 -07001554static inline void __set_iter_shift(struct radix_tree_iter *iter,
1555 unsigned int shift)
1556{
1557#ifdef CONFIG_RADIX_TREE_MULTIORDER
1558 iter->shift = shift;
1559#endif
1560}
1561
Matthew Wilcox148deab2016-12-14 15:08:49 -08001562/* Construct iter->tags bit-mask from node->tags[tag] array */
1563static void set_iter_tags(struct radix_tree_iter *iter,
1564 struct radix_tree_node *node, unsigned offset,
1565 unsigned tag)
1566{
1567 unsigned tag_long = offset / BITS_PER_LONG;
1568 unsigned tag_bit = offset % BITS_PER_LONG;
1569
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001570 if (!node) {
1571 iter->tags = 1;
1572 return;
1573 }
1574
Matthew Wilcox148deab2016-12-14 15:08:49 -08001575 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1576
1577 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1578 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1579 /* Pick tags from next element */
1580 if (tag_bit)
1581 iter->tags |= node->tags[tag][tag_long + 1] <<
1582 (BITS_PER_LONG - tag_bit);
1583 /* Clip chunk size, here only BITS_PER_LONG tags */
1584 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1585 }
1586}
1587
1588#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001589static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1590 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001591{
Matthew Wilcox148deab2016-12-14 15:08:49 -08001592 while (iter->index < iter->next_index) {
1593 *nodep = rcu_dereference_raw(*slot);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001594 if (*nodep && !xa_is_sibling(*nodep))
Matthew Wilcox148deab2016-12-14 15:08:49 -08001595 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
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001605void __rcu **__radix_tree_next_slot(void __rcu **slot,
1606 struct radix_tree_iter *iter, unsigned flags)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001607{
1608 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Ross Zwisler9f418222018-05-18 16:09:06 -07001609 struct radix_tree_node *node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001610
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
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001659static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1660 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001661{
1662 return slot;
1663}
1664#endif
1665
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001666void __rcu **radix_tree_iter_resume(void __rcu **slot,
1667 struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001668{
1669 struct radix_tree_node *node;
1670
1671 slot++;
1672 iter->index = __radix_tree_iter_add(iter, 1);
Matthew Wilcox148deab2016-12-14 15:08:49 -08001673 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 Wilcoxd7b62722017-02-13 15:58:24 -05001688void __rcu **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);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001725 return (void __rcu **)&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) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001743 void *slot = rcu_dereference_raw(
1744 node->slots[offset]);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001745 if (xa_is_sibling(slot))
Ross Zwisler21ef5332016-05-20 17:02:26 -07001746 continue;
1747 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001748 break;
1749 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001750 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001751 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001752 /* Overflow after ~0UL */
1753 if (!index)
1754 return NULL;
1755 if (offset == RADIX_TREE_MAP_SIZE)
1756 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001757 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001758 }
1759
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001760 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001761 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001762 if (child == RADIX_TREE_RETRY)
1763 break;
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001764 } while (node->shift && radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001765
1766 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001767 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1768 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001769 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001770 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001771
Matthew Wilcox148deab2016-12-14 15:08:49 -08001772 if (flags & RADIX_TREE_ITER_TAGGED)
1773 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001774
1775 return node->slots + offset;
1776}
1777EXPORT_SYMBOL(radix_tree_next_chunk);
1778
1779/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1781 * @root: radix tree root
1782 * @results: where the results of the lookup are placed
1783 * @first_index: start the lookup from this key
1784 * @max_items: place up to this many items at *results
1785 *
1786 * Performs an index-ascending scan of the tree for present items. Places
1787 * them at *@results and returns the number of items which were placed at
1788 * *@results.
1789 *
1790 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001791 *
1792 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1793 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001794 * an atomic snapshot of the tree at a single point in time, the
1795 * semantics of an RCU protected gang lookup are as though multiple
1796 * radix_tree_lookups have been issued in individual locks, and results
1797 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 */
1799unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001800radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 unsigned long first_index, unsigned int max_items)
1802{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001803 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001804 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001805 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001807 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001808 return 0;
1809
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001810 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001811 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001812 if (!results[ret])
1813 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001814 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001815 slot = radix_tree_iter_retry(&iter);
1816 continue;
1817 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001818 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 return ret;
1823}
1824EXPORT_SYMBOL(radix_tree_gang_lookup);
1825
Nick Piggin47feff22008-07-25 19:45:29 -07001826/**
1827 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1828 * @root: radix tree root
1829 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001830 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001831 * @first_index: start the lookup from this key
1832 * @max_items: place up to this many items at *results
1833 *
1834 * Performs an index-ascending scan of the tree for present items. Places
1835 * their slots at *@results and returns the number of items which were
1836 * placed at *@results.
1837 *
1838 * The implementation is naive.
1839 *
1840 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1841 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1842 * protection, radix_tree_deref_slot may fail requiring a retry.
1843 */
1844unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001845radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001846 void __rcu ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001847 unsigned long first_index, unsigned int max_items)
1848{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001849 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001850 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001851 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001852
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001853 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001854 return 0;
1855
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001856 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1857 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001858 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001859 indices[ret] = iter.index;
1860 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001861 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001862 }
1863
1864 return ret;
1865}
1866EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868/**
1869 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1870 * based on a tag
1871 * @root: radix tree root
1872 * @results: where the results of the lookup are placed
1873 * @first_index: start the lookup from this key
1874 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001875 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 *
1877 * Performs an index-ascending scan of the tree for present items which
1878 * have the tag indexed by @tag set. Places the items at *@results and
1879 * returns the number of items which were placed at *@results.
1880 */
1881unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001882radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001883 unsigned long first_index, unsigned int max_items,
1884 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001886 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001887 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001888 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001890 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001891 return 0;
1892
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001893 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001894 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001895 if (!results[ret])
1896 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001897 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001898 slot = radix_tree_iter_retry(&iter);
1899 continue;
1900 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001901 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 return ret;
1906}
1907EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1908
1909/**
Nick Piggin47feff22008-07-25 19:45:29 -07001910 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1911 * radix tree based on a tag
1912 * @root: radix tree root
1913 * @results: where the results of the lookup are placed
1914 * @first_index: start the lookup from this key
1915 * @max_items: place up to this many items at *results
1916 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1917 *
1918 * Performs an index-ascending scan of the tree for present items which
1919 * have the tag indexed by @tag set. Places the slots at *@results and
1920 * returns the number of slots which were placed at *@results.
1921 */
1922unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001923radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001924 void __rcu ***results, unsigned long first_index,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001925 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001926{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001927 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001928 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001929 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001930
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001931 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001932 return 0;
1933
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001934 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1935 results[ret] = slot;
1936 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001937 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001938 }
1939
1940 return ret;
1941}
1942EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1943
Nick Piggin47feff22008-07-25 19:45:29 -07001944/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001945 * __radix_tree_delete_node - try to free node after clearing a slot
1946 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001947 * @node: node containing @index
Johannes Weinerea07b862017-01-06 19:21:43 -05001948 * @update_node: callback for changing leaf nodes
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,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001956 radix_tree_update_node_t update_node)
Johannes Weiner139e5612014-04-03 14:47:54 -07001957{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001958 delete_node(root, node, update_node);
Johannes Weiner139e5612014-04-03 14:47:54 -07001959}
1960
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001961static bool __radix_tree_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001962 struct radix_tree_node *node, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001963{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001964 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001965 int exceptional = xa_is_value(old) ? -1 : 0;
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001966 unsigned offset = get_slot_offset(node, slot);
1967 int tag;
1968
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001969 if (is_idr(root))
1970 node_tag_set(root, node, IDR_FREE, offset);
1971 else
1972 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1973 node_tag_clear(root, node, tag, offset);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001974
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001975 replace_slot(slot, NULL, node, -1, exceptional);
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001976 return node && delete_node(root, node, NULL);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001977}
1978
Johannes Weiner139e5612014-04-03 14:47:54 -07001979/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001980 * radix_tree_iter_delete - delete the entry at this iterator position
1981 * @root: radix tree root
1982 * @iter: iterator state
1983 * @slot: pointer to slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001985 * Delete the entry at the position currently pointed to by the iterator.
1986 * This may result in the current node being freed; if it is, the iterator
1987 * is advanced so that it will not reference the freed memory. This
1988 * function may be called without any locking if there are no other threads
1989 * which can access this tree.
1990 */
1991void radix_tree_iter_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001992 struct radix_tree_iter *iter, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001993{
1994 if (__radix_tree_delete(root, iter->node, slot))
1995 iter->index = iter->next_index;
1996}
Chris Wilsond1b48c12017-08-16 09:52:08 +01001997EXPORT_SYMBOL(radix_tree_iter_delete);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001998
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;
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07002014 void __rcu **slot = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07002015 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 Wilcox7a4deea2018-05-25 14:47:24 -07002018 if (!slot)
2019 return NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002020 if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE,
2021 get_slot_offset(node, slot))))
Johannes Weiner139e5612014-04-03 14:47:54 -07002022 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
Johannes Weiner139e5612014-04-03 14:47:54 -07002024 if (item && entry != item)
2025 return NULL;
2026
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002027 __radix_tree_delete(root, node, slot);
Christoph Lameter201b6262005-09-06 15:16:46 -07002028
Johannes Weiner139e5612014-04-03 14:47:54 -07002029 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
Johannes Weiner53c59f22014-04-03 14:47:39 -07002031EXPORT_SYMBOL(radix_tree_delete_item);
2032
2033/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002034 * radix_tree_delete - delete an entry from a radix tree
2035 * @root: radix tree root
2036 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07002037 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002038 * Remove the entry at @index from the radix tree rooted at @root.
Johannes Weiner53c59f22014-04-03 14:47:39 -07002039 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002040 * Return: The deleted entry, or %NULL if it was not present.
Johannes Weiner53c59f22014-04-03 14:47:39 -07002041 */
2042void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
2043{
2044 return radix_tree_delete_item(root, index, NULL);
2045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046EXPORT_SYMBOL(radix_tree_delete);
2047
Johannes Weinerd3798ae2016-10-04 22:02:08 +02002048void radix_tree_clear_tags(struct radix_tree_root *root,
2049 struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05002050 void __rcu **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002051{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002052 if (node) {
2053 unsigned int tag, offset = get_slot_offset(node, slot);
2054 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
2055 node_tag_clear(root, node, tag, offset);
2056 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002057 root_tag_clear_all(root);
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002058 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002059}
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061/**
2062 * radix_tree_tagged - test whether any items in the tree are tagged
2063 * @root: radix tree root
2064 * @tag: tag to test
2065 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05002066int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Nick Piggin612d6c12006-06-23 02:03:22 -07002068 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070EXPORT_SYMBOL(radix_tree_tagged);
2071
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002072/**
2073 * idr_preload - preload for idr_alloc()
2074 * @gfp_mask: allocation mask to use for preloading
2075 *
2076 * Preallocate memory to use for the next call to idr_alloc(). This function
2077 * returns with preemption disabled. It will be enabled by idr_preload_end().
2078 */
2079void idr_preload(gfp_t gfp_mask)
2080{
Eric Dumazetbc9ae222017-09-08 16:15:54 -07002081 if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE))
2082 preempt_disable();
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002083}
2084EXPORT_SYMBOL(idr_preload);
2085
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002086int ida_pre_get(struct ida *ida, gfp_t gfp)
2087{
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002088 /*
2089 * The IDA API has no preload_end() equivalent. Instead,
2090 * ida_get_new() can return -EAGAIN, prompting the caller
2091 * to return to the ida_pre_get() step.
2092 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -07002093 if (!__radix_tree_preload(gfp, IDA_PRELOAD_SIZE))
2094 preempt_enable();
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002095
2096 if (!this_cpu_read(ida_bitmap)) {
Rasmus Villemoesb1a8a7a2018-02-21 14:45:43 -08002097 struct ida_bitmap *bitmap = kzalloc(sizeof(*bitmap), gfp);
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002098 if (!bitmap)
2099 return 0;
Matthew Wilcox4ecd9542017-03-03 12:16:10 -05002100 if (this_cpu_cmpxchg(ida_bitmap, NULL, bitmap))
2101 kfree(bitmap);
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002102 }
2103
2104 return 1;
2105}
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002106
Matthew Wilcox460488c2017-11-28 15:16:24 -05002107void __rcu **idr_get_free(struct radix_tree_root *root,
Chris Mi388f79f2017-08-30 02:31:57 -04002108 struct radix_tree_iter *iter, gfp_t gfp,
2109 unsigned long max)
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002110{
2111 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05002112 void __rcu **slot = (void __rcu **)&root->rnode;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002113 unsigned long maxindex, start = iter->next_index;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002114 unsigned int shift, offset = 0;
2115
2116 grow:
2117 shift = radix_tree_load_root(root, &child, &maxindex);
2118 if (!radix_tree_tagged(root, IDR_FREE))
2119 start = max(start, maxindex + 1);
2120 if (start > max)
2121 return ERR_PTR(-ENOSPC);
2122
2123 if (start > maxindex) {
2124 int error = radix_tree_extend(root, gfp, start, shift);
2125 if (error < 0)
2126 return ERR_PTR(error);
2127 shift = error;
2128 child = rcu_dereference_raw(root->rnode);
2129 }
Matthew Wilcox66ee6202018-06-25 06:56:50 -04002130 if (start == 0 && shift == 0)
2131 shift = RADIX_TREE_MAP_SHIFT;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002132
2133 while (shift) {
2134 shift -= RADIX_TREE_MAP_SHIFT;
2135 if (child == NULL) {
2136 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05002137 child = radix_tree_node_alloc(gfp, node, root, shift,
2138 offset, 0, 0);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002139 if (!child)
2140 return ERR_PTR(-ENOMEM);
2141 all_tag_set(child, IDR_FREE);
2142 rcu_assign_pointer(*slot, node_to_entry(child));
2143 if (node)
2144 node->count++;
2145 } else if (!radix_tree_is_internal_node(child))
2146 break;
2147
2148 node = entry_to_node(child);
2149 offset = radix_tree_descend(node, &child, start);
2150 if (!tag_get(node, IDR_FREE, offset)) {
2151 offset = radix_tree_find_next_bit(node, IDR_FREE,
2152 offset + 1);
2153 start = next_index(start, node, offset);
2154 if (start > max)
2155 return ERR_PTR(-ENOSPC);
2156 while (offset == RADIX_TREE_MAP_SIZE) {
2157 offset = node->offset + 1;
2158 node = node->parent;
2159 if (!node)
2160 goto grow;
2161 shift = node->shift;
2162 }
2163 child = rcu_dereference_raw(node->slots[offset]);
2164 }
2165 slot = &node->slots[offset];
2166 }
2167
2168 iter->index = start;
2169 if (node)
2170 iter->next_index = 1 + min(max, (start | node_maxindex(node)));
2171 else
2172 iter->next_index = 1;
2173 iter->node = node;
2174 __set_iter_shift(iter, shift);
2175 set_iter_tags(iter, node, offset, IDR_FREE);
2176
2177 return slot;
2178}
2179
2180/**
2181 * idr_destroy - release all internal memory from an IDR
2182 * @idr: idr handle
2183 *
2184 * After this function is called, the IDR is empty, and may be reused or
2185 * the data structure containing it may be freed.
2186 *
2187 * A typical clean-up sequence for objects stored in an idr tree will use
2188 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
2189 * free the memory used to keep track of those objects.
2190 */
2191void idr_destroy(struct idr *idr)
2192{
2193 struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.rnode);
2194 if (radix_tree_is_internal_node(node))
2195 radix_tree_free_nodes(node);
2196 idr->idr_rt.rnode = NULL;
2197 root_tag_set(&idr->idr_rt, IDR_FREE);
2198}
2199EXPORT_SYMBOL(idr_destroy);
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201static void
Johannes Weiner449dd692014-04-03 14:47:56 -07002202radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Johannes Weiner449dd692014-04-03 14:47:56 -07002204 struct radix_tree_node *node = arg;
2205
2206 memset(node, 0, sizeof(*node));
2207 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002210static __init unsigned long __maxindex(unsigned int height)
2211{
2212 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
2213 int shift = RADIX_TREE_INDEX_BITS - width;
2214
2215 if (shift < 0)
2216 return ~0UL;
2217 if (shift >= BITS_PER_LONG)
2218 return 0UL;
2219 return ~0UL >> shift;
2220}
2221
2222static __init void radix_tree_init_maxnodes(void)
2223{
2224 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
2225 unsigned int i, j;
2226
2227 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
2228 height_to_maxindex[i] = __maxindex(i);
2229 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
2230 for (j = i; j > 0; j--)
2231 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
2232 }
2233}
2234
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002235static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002237 struct radix_tree_preload *rtp;
2238 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002240 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002241 rtp = &per_cpu(radix_tree_preloads, cpu);
2242 while (rtp->nr) {
2243 node = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -05002244 rtp->nodes = node->parent;
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002245 kmem_cache_free(radix_tree_node_cachep, node);
2246 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002247 }
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002248 kfree(per_cpu(ida_bitmap, cpu));
2249 per_cpu(ida_bitmap, cpu) = NULL;
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253void __init radix_tree_init(void)
2254{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002255 int ret;
Michal Hocko7e784422017-05-03 14:53:09 -07002256
2257 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32);
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -07002258 BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04002259 BUILD_BUG_ON(XA_CHUNK_SIZE > 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2261 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07002262 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2263 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002264 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002265 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2266 NULL, radix_tree_cpu_dead);
2267 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}