blob: 3479d93c32b99fbe9438eeec21caf3a26058b1de [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 */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -050050struct 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 Wilcoxf8d5d0c2017-11-07 16:30:10 -0500127 return root->xa_flags & (__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 Wilcoxf8d5d0c2017-11-07 16:30:10 -0500150 root->xa_flags |= (__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 Wilcoxf8d5d0c2017-11-07 16:30:10 -0500155 root->xa_flags &= (__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 Wilcoxf8d5d0c2017-11-07 16:30:10 -0500160 root->xa_flags &= (__force gfp_t)((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 Wilcoxf8d5d0c2017-11-07 16:30:10 -0500165 return (__force int)root->xa_flags & (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 Wilcoxf8d5d0c2017-11-07 16:30:10 -0500170 return (__force unsigned)root->xa_flags >> ROOT_TAG_SHIFT;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500171}
172
173static inline bool is_idr(const struct radix_tree_root *root)
174{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500175 return !!(root->xa_flags & 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 Wilcox0a835c42016-12-20 10:27:56 -0500259static void dump_ida_node(void *entry, unsigned long index)
260{
261 unsigned long i;
262
263 if (!entry)
264 return;
265
266 if (radix_tree_is_internal_node(entry)) {
267 struct radix_tree_node *node = entry_to_node(entry);
268
269 pr_debug("ida node: %p offset %d indices %lu-%lu parent %p free %lx shift %d count %d\n",
270 node, node->offset, index * IDA_BITMAP_BITS,
271 ((index | node_maxindex(node)) + 1) *
272 IDA_BITMAP_BITS - 1,
273 node->parent, node->tags[0][0], node->shift,
274 node->count);
275 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
276 dump_ida_node(node->slots[i],
277 index | (i << node->shift));
Matthew Wilcox3159f942017-11-03 13:30:42 -0400278 } else if (xa_is_value(entry)) {
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500279 pr_debug("ida excp: %p offset %d indices %lu-%lu data %lx\n",
280 entry, (int)(index & RADIX_TREE_MAP_MASK),
281 index * IDA_BITMAP_BITS,
Matthew Wilcox3159f942017-11-03 13:30:42 -0400282 index * IDA_BITMAP_BITS + BITS_PER_XA_VALUE,
283 xa_to_value(entry));
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500284 } else {
285 struct ida_bitmap *bitmap = entry;
286
287 pr_debug("ida btmp: %p offset %d indices %lu-%lu data", bitmap,
288 (int)(index & RADIX_TREE_MAP_MASK),
289 index * IDA_BITMAP_BITS,
290 (index + 1) * IDA_BITMAP_BITS - 1);
291 for (i = 0; i < IDA_BITMAP_LONGS; i++)
292 pr_cont(" %lx", bitmap->bitmap[i]);
293 pr_cont("\n");
294 }
295}
296
297static void ida_dump(struct ida *ida)
298{
299 struct radix_tree_root *root = &ida->ida_rt;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500300 pr_debug("ida: %p node %p free %d\n", ida, root->xa_head,
301 root->xa_flags >> ROOT_TAG_SHIFT);
302 dump_ida_node(root->xa_head, 0);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500303}
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700304#endif
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * This assumes that the caller has performed appropriate preallocation, and
308 * that the caller has pinned this thread of control to the current CPU.
309 */
310static struct radix_tree_node *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500311radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500312 struct radix_tree_root *root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800313 unsigned int shift, unsigned int offset,
Matthew Wilcox01959df2017-11-09 09:23:56 -0500314 unsigned int count, unsigned int nr_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Nick Piggine2848a02008-02-04 22:29:10 -0800316 struct radix_tree_node *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jan Kara5e4c0d972013-09-11 14:26:05 -0700318 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700319 * Preload code isn't irq safe and it doesn't make sense to use
320 * preloading during an interrupt anyway as all the allocations have
321 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700322 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800323 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct radix_tree_preload *rtp;
325
Nick Piggine2848a02008-02-04 22:29:10 -0800326 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700327 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700328 * cache first for the new node to get accounted to the memory
329 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700330 */
331 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700332 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700333 if (ret)
334 goto out;
335
336 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800337 * Provided the caller has preloaded here, we will always
338 * succeed in getting a node here (and never reach
339 * kmem_cache_alloc)
340 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700341 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700343 ret = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500344 rtp->nodes = ret->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 rtp->nr--;
346 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700347 /*
348 * Update the allocation stack trace as this is more useful
349 * for debugging.
350 */
351 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700354 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700355out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700356 BUG_ON(radix_tree_is_internal_node(ret));
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800357 if (ret) {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800358 ret->shift = shift;
359 ret->offset = offset;
360 ret->count = count;
Matthew Wilcox01959df2017-11-09 09:23:56 -0500361 ret->nr_values = nr_values;
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500362 ret->parent = parent;
Matthew Wilcox01959df2017-11-09 09:23:56 -0500363 ret->array = root;
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return ret;
366}
367
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500368void radix_tree_node_rcu_free(struct rcu_head *head)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800369{
370 struct radix_tree_node *node =
371 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700372
373 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800374 * Must only free zeroed nodes into the slab. We can be left with
375 * non-NULL entries by radix_tree_free_nodes, so clear the entries
376 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700377 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800378 memset(node->slots, 0, sizeof(node->slots));
379 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800380 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700381
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800382 kmem_cache_free(radix_tree_node_cachep, node);
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385static inline void
386radix_tree_node_free(struct radix_tree_node *node)
387{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800388 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391/*
392 * Load up this CPU's radix_tree_node buffer with sufficient objects to
393 * ensure that the addition of a single element in the tree cannot fail. On
394 * success, return zero, with preemption disabled. On error, return -ENOMEM
395 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000396 *
397 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800398 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -0700400static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 struct radix_tree_preload *rtp;
403 struct radix_tree_node *node;
404 int ret = -ENOMEM;
405
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700406 /*
407 * Nodes preloaded by one cgroup can be be used by another cgroup, so
408 * they should never be accounted to any particular memory cgroup.
409 */
410 gfp_mask &= ~__GFP_ACCOUNT;
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700413 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700414 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700416 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (node == NULL)
418 goto out;
419 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700420 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700421 if (rtp->nr < nr) {
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500422 node->parent = rtp->nodes;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700423 rtp->nodes = node;
424 rtp->nr++;
425 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 ret = 0;
430out:
431 return ret;
432}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700433
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.
439 *
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().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700442 */
443int radix_tree_preload(gfp_t gfp_mask)
444{
445 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800446 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700447 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700448}
David Chinnerd7f09232007-07-14 16:05:04 +1000449EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Nick Piggin6e954b92006-01-08 01:01:40 -0800451/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700452 * The same as above function, except we don't guarantee preloading happens.
453 * We do it, if we decide it helps. On success, return zero with preemption
454 * disabled. On error, return -ENOMEM with preemption not disabled.
455 */
456int radix_tree_maybe_preload(gfp_t gfp_mask)
457{
Mel Gormand0164ad2015-11-06 16:28:21 -0800458 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700459 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700460 /* Preloading doesn't help anything with this gfp mask, skip it */
461 preempt_disable();
462 return 0;
463}
464EXPORT_SYMBOL(radix_tree_maybe_preload);
465
Matthew Wilcox27916532016-12-14 15:09:04 -0800466#ifdef CONFIG_RADIX_TREE_MULTIORDER
467/*
468 * Preload with enough objects to ensure that we can split a single entry
469 * of order @old_order into many entries of size @new_order
470 */
471int radix_tree_split_preload(unsigned int old_order, unsigned int new_order,
472 gfp_t gfp_mask)
473{
474 unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT);
475 unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) -
476 (new_order / RADIX_TREE_MAP_SHIFT);
477 unsigned nr = 0;
478
479 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
480 BUG_ON(new_order >= old_order);
481
482 while (layers--)
483 nr = nr * RADIX_TREE_MAP_SIZE + 1;
484 return __radix_tree_preload(gfp_mask, top * nr);
485}
486#endif
487
Jan Kara5e4c0d972013-09-11 14:26:05 -0700488/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700489 * The same as function above, but preload number of nodes required to insert
490 * (1 << order) continuous naturally-aligned elements.
491 */
492int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
493{
494 unsigned long nr_subtrees;
495 int nr_nodes, subtree_height;
496
497 /* Preloading doesn't help anything with this gfp mask, skip it */
498 if (!gfpflags_allow_blocking(gfp_mask)) {
499 preempt_disable();
500 return 0;
501 }
502
503 /*
504 * Calculate number and height of fully populated subtrees it takes to
505 * store (1 << order) elements.
506 */
507 nr_subtrees = 1 << order;
508 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
509 subtree_height++)
510 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
511
512 /*
513 * The worst case is zero height tree with a single item at index 0 and
514 * then inserting items starting at ULONG_MAX - (1 << order).
515 *
516 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
517 * 0-index item.
518 */
519 nr_nodes = RADIX_TREE_MAX_PATH;
520
521 /* Plus branch to fully populated subtrees. */
522 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
523
524 /* Root node is shared. */
525 nr_nodes--;
526
527 /* Plus nodes required to build subtrees. */
528 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
529
530 return __radix_tree_preload(gfp_mask, nr_nodes);
531}
532
Matthew Wilcox35534c82016-12-19 17:43:19 -0500533static unsigned radix_tree_load_root(const struct radix_tree_root *root,
Matthew Wilcox1456a432016-05-20 17:02:08 -0700534 struct radix_tree_node **nodep, unsigned long *maxindex)
535{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500536 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700537
538 *nodep = node;
539
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700540 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700541 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700542 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700543 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700544 }
545
546 *maxindex = 0;
547 return 0;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/*
551 * Extend a radix tree so it can store key @index.
552 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500553static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700554 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500556 void *entry;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700557 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 int tag;
559
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700560 /* Figure out what the shift should be. */
561 maxshift = shift;
562 while (index > shift_maxindex(maxshift))
563 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500565 entry = rcu_dereference_raw(root->xa_head);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500566 if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 do {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500570 struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500571 root, shift, 0, 1, 0);
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700572 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return -ENOMEM;
574
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500575 if (is_idr(root)) {
576 all_tag_set(node, IDR_FREE);
577 if (!root_tag_get(root, IDR_FREE)) {
578 tag_clear(node, IDR_FREE, 0);
579 root_tag_set(root, IDR_FREE);
580 }
581 } else {
582 /* Propagate the aggregated tag info to the new child */
583 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
584 if (root_tag_get(root, tag))
585 tag_set(node, tag, 0);
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
588
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700589 BUG_ON(shift > BITS_PER_LONG);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500590 if (radix_tree_is_internal_node(entry)) {
591 entry_to_node(entry)->parent = node;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400592 } else if (xa_is_value(entry)) {
Matthew Wilcox01959df2017-11-09 09:23:56 -0500593 /* Moving a value entry root->xa_head to a node */
594 node->nr_values = 1;
Johannes Weinerf7942432016-12-12 16:43:41 -0800595 }
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500596 /*
597 * entry was already in the radix tree, so we do not need
598 * rcu_assign_pointer here
599 */
600 node->slots[0] = (void __rcu *)entry;
601 entry = node_to_entry(node);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500602 rcu_assign_pointer(root->xa_head, entry);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700603 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700604 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700606 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800610 * radix_tree_shrink - shrink radix tree to minimum height
611 * @root radix tree root
612 */
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500613static inline bool radix_tree_shrink(struct radix_tree_root *root,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800614 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800615{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500616 bool shrunk = false;
617
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800618 for (;;) {
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500619 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800620 struct radix_tree_node *child;
621
622 if (!radix_tree_is_internal_node(node))
623 break;
624 node = entry_to_node(node);
625
626 /*
627 * The candidate node has more than one child, or its child
628 * is not at the leftmost slot, or the child is a multiorder
629 * entry, we cannot shrink.
630 */
631 if (node->count != 1)
632 break;
Matthew Wilcox12320d02017-02-13 15:22:48 -0500633 child = rcu_dereference_raw(node->slots[0]);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800634 if (!child)
635 break;
636 if (!radix_tree_is_internal_node(child) && node->shift)
637 break;
638
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400639 /*
640 * For an IDR, we must not shrink entry 0 into the root in
641 * case somebody calls idr_replace() with a pointer that
642 * appears to be an internal entry
643 */
644 if (!node->shift && is_idr(root))
645 break;
646
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800647 if (radix_tree_is_internal_node(child))
648 entry_to_node(child)->parent = NULL;
649
650 /*
651 * We don't need rcu_assign_pointer(), since we are simply
652 * moving the node from one part of the tree to another: if it
653 * was safe to dereference the old pointer to it
654 * (node->slots[0]), it will be safe to dereference the new
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500655 * one (root->xa_head) as far as dependent read barriers go.
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800656 */
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500657 root->xa_head = (void __rcu *)child;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500658 if (is_idr(root) && !tag_get(node, IDR_FREE, 0))
659 root_tag_clear(root, IDR_FREE);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800660
661 /*
662 * We have a dilemma here. The node's slot[0] must not be
663 * NULLed in case there are concurrent lookups expecting to
664 * find the item. However if this was a bottom-level node,
665 * then it may be subject to the slot pointer being visible
666 * to callers dereferencing it. If item corresponding to
667 * slot[0] is subsequently deleted, these callers would expect
668 * their slot to become empty sooner or later.
669 *
670 * For example, lockless pagecache will look up a slot, deref
671 * the page pointer, and if the page has 0 refcount it means it
672 * was concurrently deleted from pagecache so try the deref
673 * again. Fortunately there is already a requirement for logic
674 * to retry the entire slot lookup -- the indirect pointer
675 * problem (replacing direct root node with an indirect pointer
676 * also results in a stale slot). So tag the slot as indirect
677 * to force callers to retry.
678 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800679 node->count = 0;
680 if (!radix_tree_is_internal_node(child)) {
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500681 node->slots[0] = (void __rcu *)RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800682 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800683 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800684 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800685
Johannes Weinerea07b862017-01-06 19:21:43 -0500686 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800687 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500688 shrunk = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800689 }
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500690
691 return shrunk;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800692}
693
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500694static bool delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800695 struct radix_tree_node *node,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800696 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800697{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500698 bool deleted = false;
699
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800700 do {
701 struct radix_tree_node *parent;
702
703 if (node->count) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500704 if (node_to_entry(node) ==
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500705 rcu_dereference_raw(root->xa_head))
706 deleted |= radix_tree_shrink(root, update_node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500707 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800708 }
709
710 parent = node->parent;
711 if (parent) {
712 parent->slots[node->offset] = NULL;
713 parent->count--;
714 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500715 /*
716 * Shouldn't the tags already have all been cleared
717 * by the caller?
718 */
719 if (!is_idr(root))
720 root_tag_clear_all(root);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500721 root->xa_head = NULL;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800722 }
723
Johannes Weinerea07b862017-01-06 19:21:43 -0500724 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800725 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500726 deleted = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800727
728 node = parent;
729 } while (node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500730
731 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800732}
733
734/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700735 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 * @root: radix tree root
737 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700738 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700739 * @nodep: returns node
740 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700742 * Create, if necessary, and return the node and slot for an item
743 * at position @index in the radix tree @root.
744 *
745 * Until there is more than one item in the tree, no nodes are
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500746 * allocated and @root->xa_head is used as a direct slot instead of
Johannes Weiner139e5612014-04-03 14:47:54 -0700747 * pointing to a node, in which case *@nodep will be NULL.
748 *
749 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700751int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700752 unsigned order, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500753 void __rcu ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700755 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500756 void __rcu **slot = (void __rcu **)&root->xa_head;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700757 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700758 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700759 unsigned long max = index | ((1UL << order) - 1);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500760 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700761
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700762 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800765 if (order > 0 && max == ((1UL << order) - 1))
766 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700767 if (max > maxindex) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500768 int error = radix_tree_extend(root, gfp, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700769 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700771 shift = error;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500772 child = rcu_dereference_raw(root->xa_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700775 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700776 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700777 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500779 child = radix_tree_node_alloc(gfp, node, root, shift,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800780 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700781 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700783 rcu_assign_pointer(*slot, node_to_entry(child));
784 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700786 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700790 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700791 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700792 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700793 }
794
Johannes Weiner139e5612014-04-03 14:47:54 -0700795 if (nodep)
796 *nodep = node;
797 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700798 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700799 return 0;
800}
801
Matthew Wilcox175542f2016-12-14 15:08:58 -0800802/*
803 * Free any nodes below this node. The tree is presumed to not need
804 * shrinking, and any user data in the tree is presumed to not need a
805 * destructor called on it. If we need to add a destructor, we can
806 * add that functionality later. Note that we may not clear tags or
807 * slots from the tree as an RCU walker may still have a pointer into
808 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
809 * but we'll still have to clear those in rcu_free.
810 */
811static void radix_tree_free_nodes(struct radix_tree_node *node)
812{
813 unsigned offset = 0;
814 struct radix_tree_node *child = entry_to_node(node);
815
816 for (;;) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500817 void *entry = rcu_dereference_raw(child->slots[offset]);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400818 if (xa_is_node(entry) && child->shift) {
Matthew Wilcox175542f2016-12-14 15:08:58 -0800819 child = entry_to_node(entry);
820 offset = 0;
821 continue;
822 }
823 offset++;
824 while (offset == RADIX_TREE_MAP_SIZE) {
825 struct radix_tree_node *old = child;
826 offset = child->offset + 1;
827 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800828 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800829 radix_tree_node_free(old);
830 if (old == entry_to_node(node))
831 return;
832 }
833 }
834}
835
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500836#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500837static inline int insert_entries(struct radix_tree_node *node,
838 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800839{
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400840 void *sibling;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800841 unsigned i, n, tag, offset, tags = 0;
842
843 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800844 if (order > node->shift)
845 n = 1 << (order - node->shift);
846 else
847 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800848 offset = get_slot_offset(node, slot);
849 } else {
850 n = 1;
851 offset = 0;
852 }
853
854 if (n > 1) {
855 offset = offset & ~(n - 1);
856 slot = &node->slots[offset];
857 }
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400858 sibling = xa_mk_sibling(offset);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800859
860 for (i = 0; i < n; i++) {
861 if (slot[i]) {
862 if (replace) {
863 node->count--;
864 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
865 if (tag_get(node, tag, offset + i))
866 tags |= 1 << tag;
867 } else
868 return -EEXIST;
869 }
870 }
871
872 for (i = 0; i < n; i++) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500873 struct radix_tree_node *old = rcu_dereference_raw(slot[i]);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800874 if (i) {
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400875 rcu_assign_pointer(slot[i], sibling);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800876 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
877 if (tags & (1 << tag))
878 tag_clear(node, tag, offset + i);
879 } else {
880 rcu_assign_pointer(slot[i], item);
881 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
882 if (tags & (1 << tag))
883 tag_set(node, tag, offset);
884 }
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400885 if (xa_is_node(old))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800886 radix_tree_free_nodes(old);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400887 if (xa_is_value(old))
Matthew Wilcox01959df2017-11-09 09:23:56 -0500888 node->nr_values--;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800889 }
890 if (node) {
891 node->count += n;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400892 if (xa_is_value(item))
Matthew Wilcox01959df2017-11-09 09:23:56 -0500893 node->nr_values += n;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800894 }
895 return n;
896}
897#else
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500898static inline int insert_entries(struct radix_tree_node *node,
899 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800900{
901 if (*slot)
902 return -EEXIST;
903 rcu_assign_pointer(*slot, item);
904 if (node) {
905 node->count++;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400906 if (xa_is_value(item))
Matthew Wilcox01959df2017-11-09 09:23:56 -0500907 node->nr_values++;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800908 }
909 return 1;
910}
911#endif
912
Johannes Weiner139e5612014-04-03 14:47:54 -0700913/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700914 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700915 * @root: radix tree root
916 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700917 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700918 * @item: item to insert
919 *
920 * Insert an item into the radix tree at position @index.
921 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700922int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
923 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700924{
925 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500926 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700927 int error;
928
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700929 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700930
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700931 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700932 if (error)
933 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800934
935 error = insert_entries(node, slot, item, order, false);
936 if (error < 0)
937 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700938
Nick Piggin612d6c12006-06-23 02:03:22 -0700939 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700940 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700941 BUG_ON(tag_get(node, 0, offset));
942 BUG_ON(tag_get(node, 1, offset));
943 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700944 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700945 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 return 0;
949}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700950EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Johannes Weiner139e5612014-04-03 14:47:54 -0700952/**
953 * __radix_tree_lookup - lookup an item in a radix tree
954 * @root: radix tree root
955 * @index: index key
956 * @nodep: returns node
957 * @slotp: returns slot
958 *
959 * Lookup and return the item at position @index in the radix
960 * tree @root.
961 *
962 * Until there is more than one item in the tree, no nodes are
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500963 * allocated and @root->xa_head is used as a direct slot instead of
Johannes Weiner139e5612014-04-03 14:47:54 -0700964 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800965 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500966void *__radix_tree_lookup(const struct radix_tree_root *root,
967 unsigned long index, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500968 void __rcu ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800969{
Johannes Weiner139e5612014-04-03 14:47:54 -0700970 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700971 unsigned long maxindex;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500972 void __rcu **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800973
Matthew Wilcox85829952016-05-20 17:02:20 -0700974 restart:
975 parent = NULL;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500976 slot = (void __rcu **)&root->xa_head;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700977 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700978 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800979 return NULL;
980
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700981 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700982 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700983
Matthew Wilcox85829952016-05-20 17:02:20 -0700984 if (node == RADIX_TREE_RETRY)
985 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700986 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700987 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700988 slot = parent->slots + offset;
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400989 if (parent->shift == 0)
990 break;
Matthew Wilcox85829952016-05-20 17:02:20 -0700991 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800992
Johannes Weiner139e5612014-04-03 14:47:54 -0700993 if (nodep)
994 *nodep = parent;
995 if (slotp)
996 *slotp = slot;
997 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700998}
999
1000/**
1001 * radix_tree_lookup_slot - lookup a slot in a radix tree
1002 * @root: radix tree root
1003 * @index: index key
1004 *
1005 * Returns: the slot corresponding to the position @index in the
1006 * radix tree @root. This is useful for update-if-exists operations.
1007 *
1008 * This function can be called under rcu_read_lock iff the slot is not
1009 * modified by radix_tree_replace_slot, otherwise it must be called
1010 * exclusive from other writers. Any dereference of the slot must be done
1011 * using radix_tree_deref_slot.
1012 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001013void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001014 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -07001015{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001016 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -07001017
1018 if (!__radix_tree_lookup(root, index, NULL, &slot))
1019 return NULL;
1020 return slot;
Hans Reisera4331362005-11-07 00:59:29 -08001021}
1022EXPORT_SYMBOL(radix_tree_lookup_slot);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024/**
1025 * radix_tree_lookup - perform lookup operation on a radix tree
1026 * @root: radix tree root
1027 * @index: index key
1028 *
1029 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001030 *
1031 * This function can be called under rcu_read_lock, however the caller
1032 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
1033 * them safely). No RCU barriers are required to access or modify the
1034 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001036void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Johannes Weiner139e5612014-04-03 14:47:54 -07001038 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040EXPORT_SYMBOL(radix_tree_lookup);
1041
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001042static inline void replace_sibling_entries(struct radix_tree_node *node,
Matthew Wilcox01959df2017-11-09 09:23:56 -05001043 void __rcu **slot, int count, int values)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001044{
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001045#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001046 unsigned offset = get_slot_offset(node, slot);
1047 void *ptr = xa_mk_sibling(offset);
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001048
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001049 while (++offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001050 if (rcu_dereference_raw(node->slots[offset]) != ptr)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001051 break;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001052 if (count < 0) {
1053 node->slots[offset] = NULL;
1054 node->count--;
1055 }
Matthew Wilcox01959df2017-11-09 09:23:56 -05001056 node->nr_values += values;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001057 }
1058#endif
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001059}
1060
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001061static void replace_slot(void __rcu **slot, void *item,
Matthew Wilcox01959df2017-11-09 09:23:56 -05001062 struct radix_tree_node *node, int count, int values)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001063{
Matthew Wilcox01959df2017-11-09 09:23:56 -05001064 if (node && (count || values)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001065 node->count += count;
Matthew Wilcox01959df2017-11-09 09:23:56 -05001066 node->nr_values += values;
1067 replace_sibling_entries(node, slot, count, values);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001068 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001069
1070 rcu_assign_pointer(*slot, item);
1071}
1072
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001073static bool node_tag_get(const struct radix_tree_root *root,
1074 const struct radix_tree_node *node,
1075 unsigned int tag, unsigned int offset)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001076{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001077 if (node)
1078 return tag_get(node, tag, offset);
1079 return root_tag_get(root, tag);
1080}
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001081
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001082/*
1083 * IDR users want to be able to store NULL in the tree, so if the slot isn't
1084 * free, don't adjust the count, even if it's transitioning between NULL and
1085 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
1086 * have empty bits, but it only stores NULL in slots when they're being
1087 * deleted.
1088 */
1089static int calculate_count(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001090 struct radix_tree_node *node, void __rcu **slot,
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001091 void *item, void *old)
1092{
1093 if (is_idr(root)) {
1094 unsigned offset = get_slot_offset(node, slot);
1095 bool free = node_tag_get(root, node, IDR_FREE, offset);
1096 if (!free)
1097 return 0;
1098 if (!old)
1099 return 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001100 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001101 return !!item - !!old;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104/**
Johannes Weinerf7942432016-12-12 16:43:41 -08001105 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -08001106 * @root: radix tree root
1107 * @node: pointer to tree node
1108 * @slot: pointer to slot in @node
1109 * @item: new item to store in the slot.
1110 * @update_node: callback for changing leaf nodes
Johannes Weinerf7942432016-12-12 16:43:41 -08001111 *
1112 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1113 * across slot lookup and replacement.
1114 */
1115void __radix_tree_replace(struct radix_tree_root *root,
1116 struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001117 void __rcu **slot, void *item,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001118 radix_tree_update_node_t update_node)
Johannes Weinerf7942432016-12-12 16:43:41 -08001119{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001120 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox01959df2017-11-09 09:23:56 -05001121 int values = !!xa_is_value(item) - !!xa_is_value(old);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001122 int count = calculate_count(root, node, slot, item, old);
1123
Johannes Weiner6d75f362016-12-12 16:43:43 -08001124 /*
Matthew Wilcox01959df2017-11-09 09:23:56 -05001125 * This function supports replacing value entries and
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001126 * deleting entries, but that needs accounting against the
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001127 * node unless the slot is root->xa_head.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001128 */
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001129 WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->xa_head) &&
Matthew Wilcox01959df2017-11-09 09:23:56 -05001130 (count || values));
1131 replace_slot(slot, item, node, count, values);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001132
Johannes Weiner4d693d02016-12-12 16:43:49 -08001133 if (!node)
1134 return;
1135
1136 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001137 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -08001138
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001139 delete_node(root, node, update_node);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001140}
Johannes Weinerf7942432016-12-12 16:43:41 -08001141
Johannes Weiner6d75f362016-12-12 16:43:43 -08001142/**
1143 * radix_tree_replace_slot - replace item in a slot
1144 * @root: radix tree root
1145 * @slot: pointer to slot
1146 * @item: new item to store in the slot.
1147 *
1148 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1149 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1150 * across slot lookup and replacement.
1151 *
1152 * NOTE: This cannot be used to switch between non-entries (empty slots),
Matthew Wilcox01959df2017-11-09 09:23:56 -05001153 * regular entries, and value entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001154 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001155 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1156 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001157 */
1158void radix_tree_replace_slot(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001159 void __rcu **slot, void *item)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001160{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001161 __radix_tree_replace(root, NULL, slot, item, NULL);
Johannes Weinerf7942432016-12-12 16:43:41 -08001162}
Song Liu10257d72017-01-11 10:00:51 -08001163EXPORT_SYMBOL(radix_tree_replace_slot);
Johannes Weinerf7942432016-12-12 16:43:41 -08001164
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001165/**
1166 * radix_tree_iter_replace - replace item in a slot
1167 * @root: radix tree root
1168 * @slot: pointer to slot
1169 * @item: new item to store in the slot.
1170 *
1171 * For use with radix_tree_split() and radix_tree_for_each_slot().
1172 * Caller must hold tree write locked across split and replacement.
1173 */
1174void radix_tree_iter_replace(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001175 const struct radix_tree_iter *iter,
1176 void __rcu **slot, void *item)
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001177{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001178 __radix_tree_replace(root, iter->node, slot, item, NULL);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001179}
1180
Matthew Wilcox175542f2016-12-14 15:08:58 -08001181#ifdef CONFIG_RADIX_TREE_MULTIORDER
1182/**
1183 * radix_tree_join - replace multiple entries with one multiorder entry
1184 * @root: radix tree root
1185 * @index: an index inside the new entry
1186 * @order: order of the new entry
1187 * @item: new entry
1188 *
1189 * Call this function to replace several entries with one larger entry.
1190 * The existing entries are presumed to not need freeing as a result of
1191 * this call.
1192 *
1193 * The replacement entry will have all the tags set on it that were set
1194 * on any of the entries it is replacing.
1195 */
1196int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1197 unsigned order, void *item)
1198{
1199 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001200 void __rcu **slot;
Matthew Wilcox175542f2016-12-14 15:08:58 -08001201 int error;
1202
1203 BUG_ON(radix_tree_is_internal_node(item));
1204
1205 error = __radix_tree_create(root, index, order, &node, &slot);
1206 if (!error)
1207 error = insert_entries(node, slot, item, order, true);
1208 if (error > 0)
1209 error = 0;
1210
1211 return error;
1212}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001213
1214/**
1215 * radix_tree_split - Split an entry into smaller entries
1216 * @root: radix tree root
1217 * @index: An index within the large entry
1218 * @order: Order of new entries
1219 *
1220 * Call this function as the first step in replacing a multiorder entry
1221 * with several entries of lower order. After this function returns,
1222 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1223 * and call radix_tree_iter_replace() to set up each new entry.
1224 *
1225 * The tags from this entry are replicated to all the new entries.
1226 *
1227 * The radix tree should be locked against modification during the entire
1228 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1229 * should prompt RCU walkers to restart the lookup from the root.
1230 */
1231int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1232 unsigned order)
1233{
1234 struct radix_tree_node *parent, *node, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001235 void __rcu **slot;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001236 unsigned int offset, end;
1237 unsigned n, tag, tags = 0;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001238 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001239
1240 if (!__radix_tree_lookup(root, index, &parent, &slot))
1241 return -ENOENT;
1242 if (!parent)
1243 return -ENOENT;
1244
1245 offset = get_slot_offset(parent, slot);
1246
1247 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1248 if (tag_get(parent, tag, offset))
1249 tags |= 1 << tag;
1250
1251 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001252 if (!xa_is_sibling(rcu_dereference_raw(parent->slots[end])))
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001253 break;
1254 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1255 if (tags & (1 << tag))
1256 tag_set(parent, tag, end);
1257 /* rcu_assign_pointer ensures tags are set before RETRY */
1258 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1259 }
1260 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
Matthew Wilcox01959df2017-11-09 09:23:56 -05001261 parent->nr_values -= (end - offset);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001262
1263 if (order == parent->shift)
1264 return 0;
1265 if (order > parent->shift) {
1266 while (offset < end)
1267 offset += insert_entries(parent, &parent->slots[offset],
1268 RADIX_TREE_RETRY, order, true);
1269 return 0;
1270 }
1271
1272 node = parent;
1273
1274 for (;;) {
1275 if (node->shift > order) {
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05001276 child = radix_tree_node_alloc(gfp, node, root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -08001277 node->shift - RADIX_TREE_MAP_SHIFT,
1278 offset, 0, 0);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001279 if (!child)
1280 goto nomem;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001281 if (node != parent) {
1282 node->count++;
Matthew Wilcox12320d02017-02-13 15:22:48 -05001283 rcu_assign_pointer(node->slots[offset],
1284 node_to_entry(child));
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001285 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1286 if (tags & (1 << tag))
1287 tag_set(node, tag, offset);
1288 }
1289
1290 node = child;
1291 offset = 0;
1292 continue;
1293 }
1294
1295 n = insert_entries(node, &node->slots[offset],
1296 RADIX_TREE_RETRY, order, false);
1297 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1298
1299 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1300 if (tags & (1 << tag))
1301 tag_set(node, tag, offset);
1302 offset += n;
1303
1304 while (offset == RADIX_TREE_MAP_SIZE) {
1305 if (node == parent)
1306 break;
1307 offset = node->offset;
1308 child = node;
1309 node = node->parent;
1310 rcu_assign_pointer(node->slots[offset],
1311 node_to_entry(child));
1312 offset++;
1313 }
1314 if ((node == parent) && (offset == end))
1315 return 0;
1316 }
1317
1318 nomem:
1319 /* Shouldn't happen; did user forget to preload? */
1320 /* TODO: free all the allocated nodes */
1321 WARN_ON(1);
1322 return -ENOMEM;
1323}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001324#endif
1325
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001326static void node_tag_set(struct radix_tree_root *root,
1327 struct radix_tree_node *node,
1328 unsigned int tag, unsigned int offset)
1329{
1330 while (node) {
1331 if (tag_get(node, tag, offset))
1332 return;
1333 tag_set(node, tag, offset);
1334 offset = node->offset;
1335 node = node->parent;
1336 }
1337
1338 if (!root_tag_get(root, tag))
1339 root_tag_set(root, tag);
1340}
1341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342/**
1343 * radix_tree_tag_set - set a tag on a radix tree node
1344 * @root: radix tree root
1345 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001346 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001348 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1349 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * the root all the way down to the leaf node.
1351 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001352 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * item is a bug.
1354 */
1355void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001356 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Ross Zwislerfb969902016-05-20 17:02:32 -07001358 struct radix_tree_node *node, *parent;
1359 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001361 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001362 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001364 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001365 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001367 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001368 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001369 BUG_ON(!node);
1370
1371 if (!tag_get(parent, tag, offset))
1372 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374
Nick Piggin612d6c12006-06-23 02:03:22 -07001375 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001376 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001377 root_tag_set(root, tag);
1378
Ross Zwislerfb969902016-05-20 17:02:32 -07001379 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381EXPORT_SYMBOL(radix_tree_tag_set);
1382
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001383/**
1384 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1385 * @root: radix tree root
1386 * @iter: iterator state
1387 * @tag: tag to set
1388 */
1389void radix_tree_iter_tag_set(struct radix_tree_root *root,
1390 const struct radix_tree_iter *iter, unsigned int tag)
1391{
1392 node_tag_set(root, iter->node, tag, iter_offset(iter));
1393}
1394
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001395static void node_tag_clear(struct radix_tree_root *root,
1396 struct radix_tree_node *node,
1397 unsigned int tag, unsigned int offset)
1398{
1399 while (node) {
1400 if (!tag_get(node, tag, offset))
1401 return;
1402 tag_clear(node, tag, offset);
1403 if (any_tag_set(node, tag))
1404 return;
1405
1406 offset = node->offset;
1407 node = node->parent;
1408 }
1409
1410 /* clear the root's tag bit */
1411 if (root_tag_get(root, tag))
1412 root_tag_clear(root, tag);
1413}
1414
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001415/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 * radix_tree_tag_clear - clear a tag on a radix tree node
1417 * @root: radix tree root
1418 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001419 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001421 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001422 * corresponding to @index in the radix tree. If this causes
1423 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * next-to-leaf node, etc.
1425 *
1426 * Returns the address of the tagged item on success, else NULL. ie:
1427 * has the same return value and semantics as radix_tree_lookup().
1428 */
1429void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001430 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001432 struct radix_tree_node *node, *parent;
1433 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001434 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001436 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001437 if (index > maxindex)
1438 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Ross Zwisler00f47b52016-05-20 17:02:35 -07001440 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001442 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001443 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001444 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001447 if (node)
1448 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Ross Zwisler00f47b52016-05-20 17:02:35 -07001450 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452EXPORT_SYMBOL(radix_tree_tag_clear);
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001455 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1456 * @root: radix tree root
1457 * @iter: iterator state
1458 * @tag: tag to clear
1459 */
1460void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1461 const struct radix_tree_iter *iter, unsigned int tag)
1462{
1463 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1464}
1465
1466/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001467 * radix_tree_tag_get - get a tag on a radix tree node
1468 * @root: radix tree root
1469 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001470 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001472 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001474 * 0: tag not present or not set
1475 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001476 *
1477 * Note that the return value of this function may not be relied on, even if
1478 * the RCU lock is held, unless tag modification and node deletion are excluded
1479 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001481int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001482 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001484 struct radix_tree_node *node, *parent;
1485 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Nick Piggin612d6c12006-06-23 02:03:22 -07001487 if (!root_tag_get(root, tag))
1488 return 0;
1489
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001490 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001491 if (index > maxindex)
1492 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001493
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001494 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001495 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001496
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001497 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001498 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001499
Ross Zwisler4589ba62016-05-20 17:02:38 -07001500 if (!tag_get(parent, tag, offset))
1501 return 0;
1502 if (node == RADIX_TREE_RETRY)
1503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001505
1506 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Ross Zwisler21ef5332016-05-20 17:02:26 -07001510static inline void __set_iter_shift(struct radix_tree_iter *iter,
1511 unsigned int shift)
1512{
1513#ifdef CONFIG_RADIX_TREE_MULTIORDER
1514 iter->shift = shift;
1515#endif
1516}
1517
Matthew Wilcox148deab2016-12-14 15:08:49 -08001518/* Construct iter->tags bit-mask from node->tags[tag] array */
1519static void set_iter_tags(struct radix_tree_iter *iter,
1520 struct radix_tree_node *node, unsigned offset,
1521 unsigned tag)
1522{
1523 unsigned tag_long = offset / BITS_PER_LONG;
1524 unsigned tag_bit = offset % BITS_PER_LONG;
1525
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001526 if (!node) {
1527 iter->tags = 1;
1528 return;
1529 }
1530
Matthew Wilcox148deab2016-12-14 15:08:49 -08001531 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1532
1533 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1534 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1535 /* Pick tags from next element */
1536 if (tag_bit)
1537 iter->tags |= node->tags[tag][tag_long + 1] <<
1538 (BITS_PER_LONG - tag_bit);
1539 /* Clip chunk size, here only BITS_PER_LONG tags */
1540 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1541 }
1542}
1543
1544#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001545static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1546 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001547{
Matthew Wilcox148deab2016-12-14 15:08:49 -08001548 while (iter->index < iter->next_index) {
1549 *nodep = rcu_dereference_raw(*slot);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001550 if (*nodep && !xa_is_sibling(*nodep))
Matthew Wilcox148deab2016-12-14 15:08:49 -08001551 return slot;
1552 slot++;
1553 iter->index = __radix_tree_iter_add(iter, 1);
1554 iter->tags >>= 1;
1555 }
1556
1557 *nodep = NULL;
1558 return NULL;
1559}
1560
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001561void __rcu **__radix_tree_next_slot(void __rcu **slot,
1562 struct radix_tree_iter *iter, unsigned flags)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001563{
1564 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Ross Zwisler9f418222018-05-18 16:09:06 -07001565 struct radix_tree_node *node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001566
1567 slot = skip_siblings(&node, slot, iter);
1568
1569 while (radix_tree_is_internal_node(node)) {
1570 unsigned offset;
1571 unsigned long next_index;
1572
1573 if (node == RADIX_TREE_RETRY)
1574 return slot;
1575 node = entry_to_node(node);
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001576 iter->node = node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001577 iter->shift = node->shift;
1578
1579 if (flags & RADIX_TREE_ITER_TAGGED) {
1580 offset = radix_tree_find_next_bit(node, tag, 0);
1581 if (offset == RADIX_TREE_MAP_SIZE)
1582 return NULL;
1583 slot = &node->slots[offset];
1584 iter->index = __radix_tree_iter_add(iter, offset);
1585 set_iter_tags(iter, node, offset, tag);
1586 node = rcu_dereference_raw(*slot);
1587 } else {
1588 offset = 0;
1589 slot = &node->slots[0];
1590 for (;;) {
1591 node = rcu_dereference_raw(*slot);
1592 if (node)
1593 break;
1594 slot++;
1595 offset++;
1596 if (offset == RADIX_TREE_MAP_SIZE)
1597 return NULL;
1598 }
1599 iter->index = __radix_tree_iter_add(iter, offset);
1600 }
1601 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1602 goto none;
1603 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1604 if (next_index < iter->next_index)
1605 iter->next_index = next_index;
1606 }
1607
1608 return slot;
1609 none:
1610 iter->next_index = 0;
1611 return NULL;
1612}
1613EXPORT_SYMBOL(__radix_tree_next_slot);
1614#else
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001615static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1616 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001617{
1618 return slot;
1619}
1620#endif
1621
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001622void __rcu **radix_tree_iter_resume(void __rcu **slot,
1623 struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001624{
1625 struct radix_tree_node *node;
1626
1627 slot++;
1628 iter->index = __radix_tree_iter_add(iter, 1);
Matthew Wilcox148deab2016-12-14 15:08:49 -08001629 skip_siblings(&node, slot, iter);
1630 iter->next_index = iter->index;
1631 iter->tags = 0;
1632 return NULL;
1633}
1634EXPORT_SYMBOL(radix_tree_iter_resume);
1635
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001636/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001637 * radix_tree_next_chunk - find next chunk of slots for iteration
1638 *
1639 * @root: radix tree root
1640 * @iter: iterator state
1641 * @flags: RADIX_TREE_ITER_* flags and tag index
1642 * Returns: pointer to chunk first slot, or NULL if iteration is over
1643 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001644void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001645 struct radix_tree_iter *iter, unsigned flags)
1646{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001647 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001648 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001649 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001650
1651 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1652 return NULL;
1653
1654 /*
1655 * Catch next_index overflow after ~0UL. iter->index never overflows
1656 * during iterating; it can be zero only at the beginning.
1657 * And we cannot overflow iter->next_index in a single step,
1658 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001659 *
1660 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001661 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001662 */
1663 index = iter->next_index;
1664 if (!index && iter->index)
1665 return NULL;
1666
Ross Zwisler21ef5332016-05-20 17:02:26 -07001667 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001668 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001669 if (index > maxindex)
1670 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001671 if (!child)
1672 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001673
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001674 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001675 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001676 iter->index = index;
1677 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001678 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001679 iter->node = NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001680 __set_iter_shift(iter, 0);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001681 return (void __rcu **)&root->xa_head;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001682 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001683
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001684 do {
1685 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001686 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001687
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001688 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001689 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001690 /* Hole detected */
1691 if (flags & RADIX_TREE_ITER_CONTIG)
1692 return NULL;
1693
1694 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001695 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001696 offset + 1);
1697 else
1698 while (++offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001699 void *slot = rcu_dereference_raw(
1700 node->slots[offset]);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001701 if (xa_is_sibling(slot))
Ross Zwisler21ef5332016-05-20 17:02:26 -07001702 continue;
1703 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001704 break;
1705 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001706 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001707 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001708 /* Overflow after ~0UL */
1709 if (!index)
1710 return NULL;
1711 if (offset == RADIX_TREE_MAP_SIZE)
1712 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001713 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001714 }
1715
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001716 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001717 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001718 if (child == RADIX_TREE_RETRY)
1719 break;
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001720 } while (node->shift && radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001721
1722 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001723 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1724 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001725 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001726 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001727
Matthew Wilcox148deab2016-12-14 15:08:49 -08001728 if (flags & RADIX_TREE_ITER_TAGGED)
1729 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001730
1731 return node->slots + offset;
1732}
1733EXPORT_SYMBOL(radix_tree_next_chunk);
1734
1735/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1737 * @root: radix tree root
1738 * @results: where the results of the lookup are placed
1739 * @first_index: start the lookup from this key
1740 * @max_items: place up to this many items at *results
1741 *
1742 * Performs an index-ascending scan of the tree for present items. Places
1743 * them at *@results and returns the number of items which were placed at
1744 * *@results.
1745 *
1746 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001747 *
1748 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1749 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001750 * an atomic snapshot of the tree at a single point in time, the
1751 * semantics of an RCU protected gang lookup are as though multiple
1752 * radix_tree_lookups have been issued in individual locks, and results
1753 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 */
1755unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001756radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 unsigned long first_index, unsigned int max_items)
1758{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001759 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001760 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001761 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001763 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001764 return 0;
1765
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001766 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001767 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001768 if (!results[ret])
1769 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001770 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001771 slot = radix_tree_iter_retry(&iter);
1772 continue;
1773 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001774 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return ret;
1779}
1780EXPORT_SYMBOL(radix_tree_gang_lookup);
1781
Nick Piggin47feff22008-07-25 19:45:29 -07001782/**
1783 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1784 * @root: radix tree root
1785 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001786 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001787 * @first_index: start the lookup from this key
1788 * @max_items: place up to this many items at *results
1789 *
1790 * Performs an index-ascending scan of the tree for present items. Places
1791 * their slots at *@results and returns the number of items which were
1792 * placed at *@results.
1793 *
1794 * The implementation is naive.
1795 *
1796 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1797 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1798 * protection, radix_tree_deref_slot may fail requiring a retry.
1799 */
1800unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001801radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001802 void __rcu ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001803 unsigned long first_index, unsigned int max_items)
1804{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001805 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001806 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001807 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001808
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001809 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001810 return 0;
1811
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001812 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1813 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001814 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001815 indices[ret] = iter.index;
1816 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001817 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001818 }
1819
1820 return ret;
1821}
1822EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824/**
1825 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1826 * based on a tag
1827 * @root: radix tree root
1828 * @results: where the results of the lookup are placed
1829 * @first_index: start the lookup from this key
1830 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001831 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 *
1833 * Performs an index-ascending scan of the tree for present items which
1834 * have the tag indexed by @tag set. Places the items at *@results and
1835 * returns the number of items which were placed at *@results.
1836 */
1837unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001838radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001839 unsigned long first_index, unsigned int max_items,
1840 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001842 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001843 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001844 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001846 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001847 return 0;
1848
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001849 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001850 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001851 if (!results[ret])
1852 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001853 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001854 slot = radix_tree_iter_retry(&iter);
1855 continue;
1856 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001857 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return ret;
1862}
1863EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1864
1865/**
Nick Piggin47feff22008-07-25 19:45:29 -07001866 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1867 * radix tree based on a tag
1868 * @root: radix tree root
1869 * @results: where the results of the lookup are placed
1870 * @first_index: start the lookup from this key
1871 * @max_items: place up to this many items at *results
1872 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1873 *
1874 * Performs an index-ascending scan of the tree for present items which
1875 * have the tag indexed by @tag set. Places the slots at *@results and
1876 * returns the number of slots which were placed at *@results.
1877 */
1878unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001879radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001880 void __rcu ***results, unsigned long first_index,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001881 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001882{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001883 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001884 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001885 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001886
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001887 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001888 return 0;
1889
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001890 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1891 results[ret] = slot;
1892 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001893 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001894 }
1895
1896 return ret;
1897}
1898EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1899
Nick Piggin47feff22008-07-25 19:45:29 -07001900/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001901 * __radix_tree_delete_node - try to free node after clearing a slot
1902 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001903 * @node: node containing @index
Johannes Weinerea07b862017-01-06 19:21:43 -05001904 * @update_node: callback for changing leaf nodes
Johannes Weiner139e5612014-04-03 14:47:54 -07001905 *
1906 * After clearing the slot at @index in @node from radix tree
1907 * rooted at @root, call this function to attempt freeing the
1908 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001909 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001910void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weinerea07b862017-01-06 19:21:43 -05001911 struct radix_tree_node *node,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001912 radix_tree_update_node_t update_node)
Johannes Weiner139e5612014-04-03 14:47:54 -07001913{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001914 delete_node(root, node, update_node);
Johannes Weiner139e5612014-04-03 14:47:54 -07001915}
1916
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001917static bool __radix_tree_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001918 struct radix_tree_node *node, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001919{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001920 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox01959df2017-11-09 09:23:56 -05001921 int values = xa_is_value(old) ? -1 : 0;
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001922 unsigned offset = get_slot_offset(node, slot);
1923 int tag;
1924
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001925 if (is_idr(root))
1926 node_tag_set(root, node, IDR_FREE, offset);
1927 else
1928 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1929 node_tag_clear(root, node, tag, offset);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001930
Matthew Wilcox01959df2017-11-09 09:23:56 -05001931 replace_slot(slot, NULL, node, -1, values);
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001932 return node && delete_node(root, node, NULL);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001933}
1934
Johannes Weiner139e5612014-04-03 14:47:54 -07001935/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001936 * radix_tree_iter_delete - delete the entry at this iterator position
1937 * @root: radix tree root
1938 * @iter: iterator state
1939 * @slot: pointer to slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001941 * Delete the entry at the position currently pointed to by the iterator.
1942 * This may result in the current node being freed; if it is, the iterator
1943 * is advanced so that it will not reference the freed memory. This
1944 * function may be called without any locking if there are no other threads
1945 * which can access this tree.
1946 */
1947void radix_tree_iter_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001948 struct radix_tree_iter *iter, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001949{
1950 if (__radix_tree_delete(root, iter->node, slot))
1951 iter->index = iter->next_index;
1952}
Chris Wilsond1b48c12017-08-16 09:52:08 +01001953EXPORT_SYMBOL(radix_tree_iter_delete);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001954
1955/**
1956 * radix_tree_delete_item - delete an item from a radix tree
1957 * @root: radix tree root
1958 * @index: index key
1959 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001961 * Remove @item at @index from the radix tree rooted at @root.
1962 *
1963 * Return: the deleted entry, or %NULL if it was not present
1964 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001966void *radix_tree_delete_item(struct radix_tree_root *root,
1967 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001969 struct radix_tree_node *node = NULL;
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07001970 void __rcu **slot = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001971 void *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Johannes Weiner139e5612014-04-03 14:47:54 -07001973 entry = __radix_tree_lookup(root, index, &node, &slot);
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07001974 if (!slot)
1975 return NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001976 if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE,
1977 get_slot_offset(node, slot))))
Johannes Weiner139e5612014-04-03 14:47:54 -07001978 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Johannes Weiner139e5612014-04-03 14:47:54 -07001980 if (item && entry != item)
1981 return NULL;
1982
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001983 __radix_tree_delete(root, node, slot);
Christoph Lameter201b6262005-09-06 15:16:46 -07001984
Johannes Weiner139e5612014-04-03 14:47:54 -07001985 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001987EXPORT_SYMBOL(radix_tree_delete_item);
1988
1989/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001990 * radix_tree_delete - delete an entry from a radix tree
1991 * @root: radix tree root
1992 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001993 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001994 * Remove the entry at @index from the radix tree rooted at @root.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001995 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001996 * Return: The deleted entry, or %NULL if it was not present.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001997 */
1998void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1999{
2000 return radix_tree_delete_item(root, index, NULL);
2001}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002EXPORT_SYMBOL(radix_tree_delete);
2003
Johannes Weinerd3798ae2016-10-04 22:02:08 +02002004void radix_tree_clear_tags(struct radix_tree_root *root,
2005 struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05002006 void __rcu **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002007{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002008 if (node) {
2009 unsigned int tag, offset = get_slot_offset(node, slot);
2010 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
2011 node_tag_clear(root, node, tag, offset);
2012 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002013 root_tag_clear_all(root);
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002014 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002015}
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017/**
2018 * radix_tree_tagged - test whether any items in the tree are tagged
2019 * @root: radix tree root
2020 * @tag: tag to test
2021 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05002022int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Nick Piggin612d6c12006-06-23 02:03:22 -07002024 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026EXPORT_SYMBOL(radix_tree_tagged);
2027
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002028/**
2029 * idr_preload - preload for idr_alloc()
2030 * @gfp_mask: allocation mask to use for preloading
2031 *
2032 * Preallocate memory to use for the next call to idr_alloc(). This function
2033 * returns with preemption disabled. It will be enabled by idr_preload_end().
2034 */
2035void idr_preload(gfp_t gfp_mask)
2036{
Eric Dumazetbc9ae222017-09-08 16:15:54 -07002037 if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE))
2038 preempt_disable();
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002039}
2040EXPORT_SYMBOL(idr_preload);
2041
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002042int ida_pre_get(struct ida *ida, gfp_t gfp)
2043{
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002044 /*
2045 * The IDA API has no preload_end() equivalent. Instead,
2046 * ida_get_new() can return -EAGAIN, prompting the caller
2047 * to return to the ida_pre_get() step.
2048 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -07002049 if (!__radix_tree_preload(gfp, IDA_PRELOAD_SIZE))
2050 preempt_enable();
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002051
2052 if (!this_cpu_read(ida_bitmap)) {
Rasmus Villemoesb1a8a7a2018-02-21 14:45:43 -08002053 struct ida_bitmap *bitmap = kzalloc(sizeof(*bitmap), gfp);
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002054 if (!bitmap)
2055 return 0;
Matthew Wilcox4ecd9542017-03-03 12:16:10 -05002056 if (this_cpu_cmpxchg(ida_bitmap, NULL, bitmap))
2057 kfree(bitmap);
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002058 }
2059
2060 return 1;
2061}
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002062
Matthew Wilcox460488c2017-11-28 15:16:24 -05002063void __rcu **idr_get_free(struct radix_tree_root *root,
Chris Mi388f79f2017-08-30 02:31:57 -04002064 struct radix_tree_iter *iter, gfp_t gfp,
2065 unsigned long max)
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002066{
2067 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05002068 void __rcu **slot = (void __rcu **)&root->xa_head;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002069 unsigned long maxindex, start = iter->next_index;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002070 unsigned int shift, offset = 0;
2071
2072 grow:
2073 shift = radix_tree_load_root(root, &child, &maxindex);
2074 if (!radix_tree_tagged(root, IDR_FREE))
2075 start = max(start, maxindex + 1);
2076 if (start > max)
2077 return ERR_PTR(-ENOSPC);
2078
2079 if (start > maxindex) {
2080 int error = radix_tree_extend(root, gfp, start, shift);
2081 if (error < 0)
2082 return ERR_PTR(error);
2083 shift = error;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05002084 child = rcu_dereference_raw(root->xa_head);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002085 }
Matthew Wilcox66ee6202018-06-25 06:56:50 -04002086 if (start == 0 && shift == 0)
2087 shift = RADIX_TREE_MAP_SHIFT;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002088
2089 while (shift) {
2090 shift -= RADIX_TREE_MAP_SHIFT;
2091 if (child == NULL) {
2092 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05002093 child = radix_tree_node_alloc(gfp, node, root, shift,
2094 offset, 0, 0);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002095 if (!child)
2096 return ERR_PTR(-ENOMEM);
2097 all_tag_set(child, IDR_FREE);
2098 rcu_assign_pointer(*slot, node_to_entry(child));
2099 if (node)
2100 node->count++;
2101 } else if (!radix_tree_is_internal_node(child))
2102 break;
2103
2104 node = entry_to_node(child);
2105 offset = radix_tree_descend(node, &child, start);
2106 if (!tag_get(node, IDR_FREE, offset)) {
2107 offset = radix_tree_find_next_bit(node, IDR_FREE,
2108 offset + 1);
2109 start = next_index(start, node, offset);
2110 if (start > max)
2111 return ERR_PTR(-ENOSPC);
2112 while (offset == RADIX_TREE_MAP_SIZE) {
2113 offset = node->offset + 1;
2114 node = node->parent;
2115 if (!node)
2116 goto grow;
2117 shift = node->shift;
2118 }
2119 child = rcu_dereference_raw(node->slots[offset]);
2120 }
2121 slot = &node->slots[offset];
2122 }
2123
2124 iter->index = start;
2125 if (node)
2126 iter->next_index = 1 + min(max, (start | node_maxindex(node)));
2127 else
2128 iter->next_index = 1;
2129 iter->node = node;
2130 __set_iter_shift(iter, shift);
2131 set_iter_tags(iter, node, offset, IDR_FREE);
2132
2133 return slot;
2134}
2135
2136/**
2137 * idr_destroy - release all internal memory from an IDR
2138 * @idr: idr handle
2139 *
2140 * After this function is called, the IDR is empty, and may be reused or
2141 * the data structure containing it may be freed.
2142 *
2143 * A typical clean-up sequence for objects stored in an idr tree will use
2144 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
2145 * free the memory used to keep track of those objects.
2146 */
2147void idr_destroy(struct idr *idr)
2148{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05002149 struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.xa_head);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002150 if (radix_tree_is_internal_node(node))
2151 radix_tree_free_nodes(node);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05002152 idr->idr_rt.xa_head = NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002153 root_tag_set(&idr->idr_rt, IDR_FREE);
2154}
2155EXPORT_SYMBOL(idr_destroy);
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157static void
Johannes Weiner449dd692014-04-03 14:47:56 -07002158radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
Johannes Weiner449dd692014-04-03 14:47:56 -07002160 struct radix_tree_node *node = arg;
2161
2162 memset(node, 0, sizeof(*node));
2163 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002166static __init unsigned long __maxindex(unsigned int height)
2167{
2168 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
2169 int shift = RADIX_TREE_INDEX_BITS - width;
2170
2171 if (shift < 0)
2172 return ~0UL;
2173 if (shift >= BITS_PER_LONG)
2174 return 0UL;
2175 return ~0UL >> shift;
2176}
2177
2178static __init void radix_tree_init_maxnodes(void)
2179{
2180 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
2181 unsigned int i, j;
2182
2183 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
2184 height_to_maxindex[i] = __maxindex(i);
2185 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
2186 for (j = i; j > 0; j--)
2187 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
2188 }
2189}
2190
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002191static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002193 struct radix_tree_preload *rtp;
2194 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002196 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002197 rtp = &per_cpu(radix_tree_preloads, cpu);
2198 while (rtp->nr) {
2199 node = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -05002200 rtp->nodes = node->parent;
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002201 kmem_cache_free(radix_tree_node_cachep, node);
2202 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002203 }
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002204 kfree(per_cpu(ida_bitmap, cpu));
2205 per_cpu(ida_bitmap, cpu) = NULL;
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209void __init radix_tree_init(void)
2210{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002211 int ret;
Michal Hocko7e784422017-05-03 14:53:09 -07002212
2213 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32);
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -07002214 BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04002215 BUILD_BUG_ON(XA_CHUNK_SIZE > 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2217 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07002218 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2219 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002220 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002221 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2222 NULL, radix_tree_cpu_dead);
2223 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}