blob: 8a58051eb5b3aeeb7f6c19b54b7f6882997fdf38 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/*
259 * This assumes that the caller has performed appropriate preallocation, and
260 * that the caller has pinned this thread of control to the current CPU.
261 */
262static struct radix_tree_node *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500263radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500264 struct radix_tree_root *root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800265 unsigned int shift, unsigned int offset,
Matthew Wilcox01959df2017-11-09 09:23:56 -0500266 unsigned int count, unsigned int nr_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Nick Piggine2848a02008-02-04 22:29:10 -0800268 struct radix_tree_node *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Jan Kara5e4c0d972013-09-11 14:26:05 -0700270 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700271 * Preload code isn't irq safe and it doesn't make sense to use
272 * preloading during an interrupt anyway as all the allocations have
273 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700274 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800275 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 struct radix_tree_preload *rtp;
277
Nick Piggine2848a02008-02-04 22:29:10 -0800278 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700279 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700280 * cache first for the new node to get accounted to the memory
281 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700282 */
283 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700284 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700285 if (ret)
286 goto out;
287
288 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800289 * Provided the caller has preloaded here, we will always
290 * succeed in getting a node here (and never reach
291 * kmem_cache_alloc)
292 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700293 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700295 ret = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500296 rtp->nodes = ret->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 rtp->nr--;
298 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700299 /*
300 * Update the allocation stack trace as this is more useful
301 * for debugging.
302 */
303 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700304 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700306 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700307out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700308 BUG_ON(radix_tree_is_internal_node(ret));
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800309 if (ret) {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800310 ret->shift = shift;
311 ret->offset = offset;
312 ret->count = count;
Matthew Wilcox01959df2017-11-09 09:23:56 -0500313 ret->nr_values = nr_values;
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500314 ret->parent = parent;
Matthew Wilcox01959df2017-11-09 09:23:56 -0500315 ret->array = root;
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return ret;
318}
319
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500320void radix_tree_node_rcu_free(struct rcu_head *head)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800321{
322 struct radix_tree_node *node =
323 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700324
325 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800326 * Must only free zeroed nodes into the slab. We can be left with
327 * non-NULL entries by radix_tree_free_nodes, so clear the entries
328 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700329 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800330 memset(node->slots, 0, sizeof(node->slots));
331 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800332 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700333
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800334 kmem_cache_free(radix_tree_node_cachep, node);
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static inline void
338radix_tree_node_free(struct radix_tree_node *node)
339{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800340 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * Load up this CPU's radix_tree_node buffer with sufficient objects to
345 * ensure that the addition of a single element in the tree cannot fail. On
346 * success, return zero, with preemption disabled. On error, return -ENOMEM
347 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000348 *
349 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800350 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -0700352static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct radix_tree_preload *rtp;
355 struct radix_tree_node *node;
356 int ret = -ENOMEM;
357
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700358 /*
359 * Nodes preloaded by one cgroup can be be used by another cgroup, so
360 * they should never be accounted to any particular memory cgroup.
361 */
362 gfp_mask &= ~__GFP_ACCOUNT;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700365 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700366 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700368 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (node == NULL)
370 goto out;
371 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700372 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700373 if (rtp->nr < nr) {
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500374 node->parent = rtp->nodes;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700375 rtp->nodes = node;
376 rtp->nr++;
377 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381 ret = 0;
382out:
383 return ret;
384}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700385
386/*
387 * Load up this CPU's radix_tree_node buffer with sufficient objects to
388 * ensure that the addition of a single element in the tree cannot fail. On
389 * success, return zero, with preemption disabled. On error, return -ENOMEM
390 * with preemption not disabled.
391 *
392 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800393 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700394 */
395int radix_tree_preload(gfp_t gfp_mask)
396{
397 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800398 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700399 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700400}
David Chinnerd7f09232007-07-14 16:05:04 +1000401EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Nick Piggin6e954b92006-01-08 01:01:40 -0800403/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700404 * The same as above function, except we don't guarantee preloading happens.
405 * We do it, if we decide it helps. On success, return zero with preemption
406 * disabled. On error, return -ENOMEM with preemption not disabled.
407 */
408int radix_tree_maybe_preload(gfp_t gfp_mask)
409{
Mel Gormand0164ad2015-11-06 16:28:21 -0800410 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700411 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700412 /* Preloading doesn't help anything with this gfp mask, skip it */
413 preempt_disable();
414 return 0;
415}
416EXPORT_SYMBOL(radix_tree_maybe_preload);
417
Matthew Wilcox2791653a2016-12-14 15:09:04 -0800418#ifdef CONFIG_RADIX_TREE_MULTIORDER
419/*
420 * Preload with enough objects to ensure that we can split a single entry
421 * of order @old_order into many entries of size @new_order
422 */
423int radix_tree_split_preload(unsigned int old_order, unsigned int new_order,
424 gfp_t gfp_mask)
425{
426 unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT);
427 unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) -
428 (new_order / RADIX_TREE_MAP_SHIFT);
429 unsigned nr = 0;
430
431 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
432 BUG_ON(new_order >= old_order);
433
434 while (layers--)
435 nr = nr * RADIX_TREE_MAP_SIZE + 1;
436 return __radix_tree_preload(gfp_mask, top * nr);
437}
438#endif
439
Jan Kara5e4c0d972013-09-11 14:26:05 -0700440/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700441 * The same as function above, but preload number of nodes required to insert
442 * (1 << order) continuous naturally-aligned elements.
443 */
444int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
445{
446 unsigned long nr_subtrees;
447 int nr_nodes, subtree_height;
448
449 /* Preloading doesn't help anything with this gfp mask, skip it */
450 if (!gfpflags_allow_blocking(gfp_mask)) {
451 preempt_disable();
452 return 0;
453 }
454
455 /*
456 * Calculate number and height of fully populated subtrees it takes to
457 * store (1 << order) elements.
458 */
459 nr_subtrees = 1 << order;
460 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
461 subtree_height++)
462 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
463
464 /*
465 * The worst case is zero height tree with a single item at index 0 and
466 * then inserting items starting at ULONG_MAX - (1 << order).
467 *
468 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
469 * 0-index item.
470 */
471 nr_nodes = RADIX_TREE_MAX_PATH;
472
473 /* Plus branch to fully populated subtrees. */
474 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
475
476 /* Root node is shared. */
477 nr_nodes--;
478
479 /* Plus nodes required to build subtrees. */
480 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
481
482 return __radix_tree_preload(gfp_mask, nr_nodes);
483}
484
Matthew Wilcox35534c82016-12-19 17:43:19 -0500485static unsigned radix_tree_load_root(const struct radix_tree_root *root,
Matthew Wilcox1456a432016-05-20 17:02:08 -0700486 struct radix_tree_node **nodep, unsigned long *maxindex)
487{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500488 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700489
490 *nodep = node;
491
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700492 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700493 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700494 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700495 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700496 }
497
498 *maxindex = 0;
499 return 0;
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/*
503 * Extend a radix tree so it can store key @index.
504 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500505static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700506 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500508 void *entry;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700509 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int tag;
511
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700512 /* Figure out what the shift should be. */
513 maxshift = shift;
514 while (index > shift_maxindex(maxshift))
515 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500517 entry = rcu_dereference_raw(root->xa_head);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500518 if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 do {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500522 struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500523 root, shift, 0, 1, 0);
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700524 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return -ENOMEM;
526
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500527 if (is_idr(root)) {
528 all_tag_set(node, IDR_FREE);
529 if (!root_tag_get(root, IDR_FREE)) {
530 tag_clear(node, IDR_FREE, 0);
531 root_tag_set(root, IDR_FREE);
532 }
533 } else {
534 /* Propagate the aggregated tag info to the new child */
535 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
536 if (root_tag_get(root, tag))
537 tag_set(node, tag, 0);
538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700541 BUG_ON(shift > BITS_PER_LONG);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500542 if (radix_tree_is_internal_node(entry)) {
543 entry_to_node(entry)->parent = node;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400544 } else if (xa_is_value(entry)) {
Matthew Wilcox01959df2017-11-09 09:23:56 -0500545 /* Moving a value entry root->xa_head to a node */
546 node->nr_values = 1;
Johannes Weinerf7942432016-12-12 16:43:41 -0800547 }
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500548 /*
549 * entry was already in the radix tree, so we do not need
550 * rcu_assign_pointer here
551 */
552 node->slots[0] = (void __rcu *)entry;
553 entry = node_to_entry(node);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500554 rcu_assign_pointer(root->xa_head, entry);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700555 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700556 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700558 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800562 * radix_tree_shrink - shrink radix tree to minimum height
563 * @root radix tree root
564 */
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500565static inline bool radix_tree_shrink(struct radix_tree_root *root,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800566 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800567{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500568 bool shrunk = false;
569
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800570 for (;;) {
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500571 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800572 struct radix_tree_node *child;
573
574 if (!radix_tree_is_internal_node(node))
575 break;
576 node = entry_to_node(node);
577
578 /*
579 * The candidate node has more than one child, or its child
580 * is not at the leftmost slot, or the child is a multiorder
581 * entry, we cannot shrink.
582 */
583 if (node->count != 1)
584 break;
Matthew Wilcox12320d02017-02-13 15:22:48 -0500585 child = rcu_dereference_raw(node->slots[0]);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800586 if (!child)
587 break;
588 if (!radix_tree_is_internal_node(child) && node->shift)
589 break;
590
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400591 /*
592 * For an IDR, we must not shrink entry 0 into the root in
593 * case somebody calls idr_replace() with a pointer that
594 * appears to be an internal entry
595 */
596 if (!node->shift && is_idr(root))
597 break;
598
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800599 if (radix_tree_is_internal_node(child))
600 entry_to_node(child)->parent = NULL;
601
602 /*
603 * We don't need rcu_assign_pointer(), since we are simply
604 * moving the node from one part of the tree to another: if it
605 * was safe to dereference the old pointer to it
606 * (node->slots[0]), it will be safe to dereference the new
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500607 * one (root->xa_head) as far as dependent read barriers go.
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800608 */
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500609 root->xa_head = (void __rcu *)child;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500610 if (is_idr(root) && !tag_get(node, IDR_FREE, 0))
611 root_tag_clear(root, IDR_FREE);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800612
613 /*
614 * We have a dilemma here. The node's slot[0] must not be
615 * NULLed in case there are concurrent lookups expecting to
616 * find the item. However if this was a bottom-level node,
617 * then it may be subject to the slot pointer being visible
618 * to callers dereferencing it. If item corresponding to
619 * slot[0] is subsequently deleted, these callers would expect
620 * their slot to become empty sooner or later.
621 *
622 * For example, lockless pagecache will look up a slot, deref
623 * the page pointer, and if the page has 0 refcount it means it
624 * was concurrently deleted from pagecache so try the deref
625 * again. Fortunately there is already a requirement for logic
626 * to retry the entire slot lookup -- the indirect pointer
627 * problem (replacing direct root node with an indirect pointer
628 * also results in a stale slot). So tag the slot as indirect
629 * to force callers to retry.
630 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800631 node->count = 0;
632 if (!radix_tree_is_internal_node(child)) {
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500633 node->slots[0] = (void __rcu *)RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800634 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800635 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800636 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800637
Johannes Weinerea07b862017-01-06 19:21:43 -0500638 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800639 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500640 shrunk = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800641 }
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500642
643 return shrunk;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800644}
645
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500646static bool delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800647 struct radix_tree_node *node,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800648 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800649{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500650 bool deleted = false;
651
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800652 do {
653 struct radix_tree_node *parent;
654
655 if (node->count) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500656 if (node_to_entry(node) ==
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500657 rcu_dereference_raw(root->xa_head))
658 deleted |= radix_tree_shrink(root, update_node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500659 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800660 }
661
662 parent = node->parent;
663 if (parent) {
664 parent->slots[node->offset] = NULL;
665 parent->count--;
666 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500667 /*
668 * Shouldn't the tags already have all been cleared
669 * by the caller?
670 */
671 if (!is_idr(root))
672 root_tag_clear_all(root);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500673 root->xa_head = NULL;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800674 }
675
Johannes Weinerea07b862017-01-06 19:21:43 -0500676 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800677 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500678 deleted = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800679
680 node = parent;
681 } while (node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500682
683 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800684}
685
686/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700687 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * @root: radix tree root
689 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700690 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700691 * @nodep: returns node
692 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700694 * Create, if necessary, and return the node and slot for an item
695 * at position @index in the radix tree @root.
696 *
697 * Until there is more than one item in the tree, no nodes are
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500698 * allocated and @root->xa_head is used as a direct slot instead of
Johannes Weiner139e5612014-04-03 14:47:54 -0700699 * pointing to a node, in which case *@nodep will be NULL.
700 *
701 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Matthew Wilcox74d60952017-11-17 10:01:45 -0500703static int __radix_tree_create(struct radix_tree_root *root,
704 unsigned long index, unsigned order,
705 struct radix_tree_node **nodep, void __rcu ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700707 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500708 void __rcu **slot = (void __rcu **)&root->xa_head;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700709 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700710 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700711 unsigned long max = index | ((1UL << order) - 1);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500712 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700713
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700714 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800717 if (order > 0 && max == ((1UL << order) - 1))
718 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700719 if (max > maxindex) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500720 int error = radix_tree_extend(root, gfp, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700721 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700723 shift = error;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500724 child = rcu_dereference_raw(root->xa_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700727 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700728 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700729 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500731 child = radix_tree_node_alloc(gfp, node, root, shift,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800732 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700733 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700735 rcu_assign_pointer(*slot, node_to_entry(child));
736 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700738 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700742 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700743 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700744 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700745 }
746
Johannes Weiner139e5612014-04-03 14:47:54 -0700747 if (nodep)
748 *nodep = node;
749 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700750 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700751 return 0;
752}
753
Matthew Wilcox175542f2016-12-14 15:08:58 -0800754/*
755 * Free any nodes below this node. The tree is presumed to not need
756 * shrinking, and any user data in the tree is presumed to not need a
757 * destructor called on it. If we need to add a destructor, we can
758 * add that functionality later. Note that we may not clear tags or
759 * slots from the tree as an RCU walker may still have a pointer into
760 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
761 * but we'll still have to clear those in rcu_free.
762 */
763static void radix_tree_free_nodes(struct radix_tree_node *node)
764{
765 unsigned offset = 0;
766 struct radix_tree_node *child = entry_to_node(node);
767
768 for (;;) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500769 void *entry = rcu_dereference_raw(child->slots[offset]);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400770 if (xa_is_node(entry) && child->shift) {
Matthew Wilcox175542f2016-12-14 15:08:58 -0800771 child = entry_to_node(entry);
772 offset = 0;
773 continue;
774 }
775 offset++;
776 while (offset == RADIX_TREE_MAP_SIZE) {
777 struct radix_tree_node *old = child;
778 offset = child->offset + 1;
779 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800780 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800781 radix_tree_node_free(old);
782 if (old == entry_to_node(node))
783 return;
784 }
785 }
786}
787
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500788#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500789static inline int insert_entries(struct radix_tree_node *node,
790 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800791{
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400792 void *sibling;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800793 unsigned i, n, tag, offset, tags = 0;
794
795 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800796 if (order > node->shift)
797 n = 1 << (order - node->shift);
798 else
799 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800800 offset = get_slot_offset(node, slot);
801 } else {
802 n = 1;
803 offset = 0;
804 }
805
806 if (n > 1) {
807 offset = offset & ~(n - 1);
808 slot = &node->slots[offset];
809 }
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400810 sibling = xa_mk_sibling(offset);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800811
812 for (i = 0; i < n; i++) {
813 if (slot[i]) {
814 if (replace) {
815 node->count--;
816 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
817 if (tag_get(node, tag, offset + i))
818 tags |= 1 << tag;
819 } else
820 return -EEXIST;
821 }
822 }
823
824 for (i = 0; i < n; i++) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500825 struct radix_tree_node *old = rcu_dereference_raw(slot[i]);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800826 if (i) {
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400827 rcu_assign_pointer(slot[i], sibling);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800828 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
829 if (tags & (1 << tag))
830 tag_clear(node, tag, offset + i);
831 } else {
832 rcu_assign_pointer(slot[i], item);
833 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
834 if (tags & (1 << tag))
835 tag_set(node, tag, offset);
836 }
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400837 if (xa_is_node(old))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800838 radix_tree_free_nodes(old);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400839 if (xa_is_value(old))
Matthew Wilcox01959df2017-11-09 09:23:56 -0500840 node->nr_values--;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800841 }
842 if (node) {
843 node->count += n;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400844 if (xa_is_value(item))
Matthew Wilcox01959df2017-11-09 09:23:56 -0500845 node->nr_values += n;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800846 }
847 return n;
848}
849#else
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500850static inline int insert_entries(struct radix_tree_node *node,
851 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800852{
853 if (*slot)
854 return -EEXIST;
855 rcu_assign_pointer(*slot, item);
856 if (node) {
857 node->count++;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400858 if (xa_is_value(item))
Matthew Wilcox01959df2017-11-09 09:23:56 -0500859 node->nr_values++;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800860 }
861 return 1;
862}
863#endif
864
Johannes Weiner139e5612014-04-03 14:47:54 -0700865/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700866 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700867 * @root: radix tree root
868 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700869 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700870 * @item: item to insert
871 *
872 * Insert an item into the radix tree at position @index.
873 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700874int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
875 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700876{
877 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500878 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700879 int error;
880
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700881 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700882
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700883 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700884 if (error)
885 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800886
887 error = insert_entries(node, slot, item, order, false);
888 if (error < 0)
889 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700890
Nick Piggin612d6c12006-06-23 02:03:22 -0700891 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700892 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700893 BUG_ON(tag_get(node, 0, offset));
894 BUG_ON(tag_get(node, 1, offset));
895 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700896 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700897 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return 0;
901}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700902EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Johannes Weiner139e5612014-04-03 14:47:54 -0700904/**
905 * __radix_tree_lookup - lookup an item in a radix tree
906 * @root: radix tree root
907 * @index: index key
908 * @nodep: returns node
909 * @slotp: returns slot
910 *
911 * Lookup and return the item at position @index in the radix
912 * tree @root.
913 *
914 * Until there is more than one item in the tree, no nodes are
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500915 * allocated and @root->xa_head is used as a direct slot instead of
Johannes Weiner139e5612014-04-03 14:47:54 -0700916 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800917 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500918void *__radix_tree_lookup(const struct radix_tree_root *root,
919 unsigned long index, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500920 void __rcu ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800921{
Johannes Weiner139e5612014-04-03 14:47:54 -0700922 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700923 unsigned long maxindex;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500924 void __rcu **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800925
Matthew Wilcox85829952016-05-20 17:02:20 -0700926 restart:
927 parent = NULL;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500928 slot = (void __rcu **)&root->xa_head;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700929 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700930 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800931 return NULL;
932
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700933 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700934 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700935
Matthew Wilcox85829952016-05-20 17:02:20 -0700936 if (node == RADIX_TREE_RETRY)
937 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700938 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700939 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700940 slot = parent->slots + offset;
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400941 if (parent->shift == 0)
942 break;
Matthew Wilcox85829952016-05-20 17:02:20 -0700943 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800944
Johannes Weiner139e5612014-04-03 14:47:54 -0700945 if (nodep)
946 *nodep = parent;
947 if (slotp)
948 *slotp = slot;
949 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700950}
951
952/**
953 * radix_tree_lookup_slot - lookup a slot in a radix tree
954 * @root: radix tree root
955 * @index: index key
956 *
957 * Returns: the slot corresponding to the position @index in the
958 * radix tree @root. This is useful for update-if-exists operations.
959 *
960 * This function can be called under rcu_read_lock iff the slot is not
961 * modified by radix_tree_replace_slot, otherwise it must be called
962 * exclusive from other writers. Any dereference of the slot must be done
963 * using radix_tree_deref_slot.
964 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500965void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcox35534c82016-12-19 17:43:19 -0500966 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -0700967{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500968 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700969
970 if (!__radix_tree_lookup(root, index, NULL, &slot))
971 return NULL;
972 return slot;
Hans Reisera4331362005-11-07 00:59:29 -0800973}
974EXPORT_SYMBOL(radix_tree_lookup_slot);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976/**
977 * radix_tree_lookup - perform lookup operation on a radix tree
978 * @root: radix tree root
979 * @index: index key
980 *
981 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800982 *
983 * This function can be called under rcu_read_lock, however the caller
984 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
985 * them safely). No RCU barriers are required to access or modify the
986 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500988void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Johannes Weiner139e5612014-04-03 14:47:54 -0700990 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992EXPORT_SYMBOL(radix_tree_lookup);
993
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500994static inline void replace_sibling_entries(struct radix_tree_node *node,
Matthew Wilcox01959df2017-11-09 09:23:56 -0500995 void __rcu **slot, int count, int values)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800996{
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800997#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400998 unsigned offset = get_slot_offset(node, slot);
999 void *ptr = xa_mk_sibling(offset);
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001000
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001001 while (++offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001002 if (rcu_dereference_raw(node->slots[offset]) != ptr)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001003 break;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001004 if (count < 0) {
1005 node->slots[offset] = NULL;
1006 node->count--;
1007 }
Matthew Wilcox01959df2017-11-09 09:23:56 -05001008 node->nr_values += values;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001009 }
1010#endif
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001011}
1012
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001013static void replace_slot(void __rcu **slot, void *item,
Matthew Wilcox01959df2017-11-09 09:23:56 -05001014 struct radix_tree_node *node, int count, int values)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001015{
Matthew Wilcox01959df2017-11-09 09:23:56 -05001016 if (node && (count || values)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001017 node->count += count;
Matthew Wilcox01959df2017-11-09 09:23:56 -05001018 node->nr_values += values;
1019 replace_sibling_entries(node, slot, count, values);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001020 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001021
1022 rcu_assign_pointer(*slot, item);
1023}
1024
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001025static bool node_tag_get(const struct radix_tree_root *root,
1026 const struct radix_tree_node *node,
1027 unsigned int tag, unsigned int offset)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001028{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001029 if (node)
1030 return tag_get(node, tag, offset);
1031 return root_tag_get(root, tag);
1032}
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001033
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001034/*
1035 * IDR users want to be able to store NULL in the tree, so if the slot isn't
1036 * free, don't adjust the count, even if it's transitioning between NULL and
1037 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
1038 * have empty bits, but it only stores NULL in slots when they're being
1039 * deleted.
1040 */
1041static int calculate_count(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001042 struct radix_tree_node *node, void __rcu **slot,
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001043 void *item, void *old)
1044{
1045 if (is_idr(root)) {
1046 unsigned offset = get_slot_offset(node, slot);
1047 bool free = node_tag_get(root, node, IDR_FREE, offset);
1048 if (!free)
1049 return 0;
1050 if (!old)
1051 return 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001052 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001053 return !!item - !!old;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/**
Johannes Weinerf7942432016-12-12 16:43:41 -08001057 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -08001058 * @root: radix tree root
1059 * @node: pointer to tree node
1060 * @slot: pointer to slot in @node
1061 * @item: new item to store in the slot.
1062 * @update_node: callback for changing leaf nodes
Johannes Weinerf7942432016-12-12 16:43:41 -08001063 *
1064 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1065 * across slot lookup and replacement.
1066 */
1067void __radix_tree_replace(struct radix_tree_root *root,
1068 struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001069 void __rcu **slot, void *item,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001070 radix_tree_update_node_t update_node)
Johannes Weinerf7942432016-12-12 16:43:41 -08001071{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001072 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox01959df2017-11-09 09:23:56 -05001073 int values = !!xa_is_value(item) - !!xa_is_value(old);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001074 int count = calculate_count(root, node, slot, item, old);
1075
Johannes Weiner6d75f362016-12-12 16:43:43 -08001076 /*
Matthew Wilcox01959df2017-11-09 09:23:56 -05001077 * This function supports replacing value entries and
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001078 * deleting entries, but that needs accounting against the
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001079 * node unless the slot is root->xa_head.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001080 */
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001081 WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->xa_head) &&
Matthew Wilcox01959df2017-11-09 09:23:56 -05001082 (count || values));
1083 replace_slot(slot, item, node, count, values);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001084
Johannes Weiner4d693d02016-12-12 16:43:49 -08001085 if (!node)
1086 return;
1087
1088 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001089 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -08001090
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001091 delete_node(root, node, update_node);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001092}
Johannes Weinerf7942432016-12-12 16:43:41 -08001093
Johannes Weiner6d75f362016-12-12 16:43:43 -08001094/**
1095 * radix_tree_replace_slot - replace item in a slot
1096 * @root: radix tree root
1097 * @slot: pointer to slot
1098 * @item: new item to store in the slot.
1099 *
1100 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1101 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1102 * across slot lookup and replacement.
1103 *
1104 * NOTE: This cannot be used to switch between non-entries (empty slots),
Matthew Wilcox01959df2017-11-09 09:23:56 -05001105 * regular entries, and value entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001106 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001107 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1108 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001109 */
1110void radix_tree_replace_slot(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001111 void __rcu **slot, void *item)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001112{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001113 __radix_tree_replace(root, NULL, slot, item, NULL);
Johannes Weinerf7942432016-12-12 16:43:41 -08001114}
Song Liu10257d72017-01-11 10:00:51 -08001115EXPORT_SYMBOL(radix_tree_replace_slot);
Johannes Weinerf7942432016-12-12 16:43:41 -08001116
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001117/**
1118 * radix_tree_iter_replace - replace item in a slot
1119 * @root: radix tree root
1120 * @slot: pointer to slot
1121 * @item: new item to store in the slot.
1122 *
1123 * For use with radix_tree_split() and radix_tree_for_each_slot().
1124 * Caller must hold tree write locked across split and replacement.
1125 */
1126void radix_tree_iter_replace(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001127 const struct radix_tree_iter *iter,
1128 void __rcu **slot, void *item)
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001129{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001130 __radix_tree_replace(root, iter->node, slot, item, NULL);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001131}
1132
Matthew Wilcox175542f2016-12-14 15:08:58 -08001133#ifdef CONFIG_RADIX_TREE_MULTIORDER
1134/**
1135 * radix_tree_join - replace multiple entries with one multiorder entry
1136 * @root: radix tree root
1137 * @index: an index inside the new entry
1138 * @order: order of the new entry
1139 * @item: new entry
1140 *
1141 * Call this function to replace several entries with one larger entry.
1142 * The existing entries are presumed to not need freeing as a result of
1143 * this call.
1144 *
1145 * The replacement entry will have all the tags set on it that were set
1146 * on any of the entries it is replacing.
1147 */
1148int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1149 unsigned order, void *item)
1150{
1151 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001152 void __rcu **slot;
Matthew Wilcox175542f2016-12-14 15:08:58 -08001153 int error;
1154
1155 BUG_ON(radix_tree_is_internal_node(item));
1156
1157 error = __radix_tree_create(root, index, order, &node, &slot);
1158 if (!error)
1159 error = insert_entries(node, slot, item, order, true);
1160 if (error > 0)
1161 error = 0;
1162
1163 return error;
1164}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001165
1166/**
1167 * radix_tree_split - Split an entry into smaller entries
1168 * @root: radix tree root
1169 * @index: An index within the large entry
1170 * @order: Order of new entries
1171 *
1172 * Call this function as the first step in replacing a multiorder entry
1173 * with several entries of lower order. After this function returns,
1174 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1175 * and call radix_tree_iter_replace() to set up each new entry.
1176 *
1177 * The tags from this entry are replicated to all the new entries.
1178 *
1179 * The radix tree should be locked against modification during the entire
1180 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1181 * should prompt RCU walkers to restart the lookup from the root.
1182 */
1183int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1184 unsigned order)
1185{
1186 struct radix_tree_node *parent, *node, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001187 void __rcu **slot;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001188 unsigned int offset, end;
1189 unsigned n, tag, tags = 0;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001190 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001191
1192 if (!__radix_tree_lookup(root, index, &parent, &slot))
1193 return -ENOENT;
1194 if (!parent)
1195 return -ENOENT;
1196
1197 offset = get_slot_offset(parent, slot);
1198
1199 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1200 if (tag_get(parent, tag, offset))
1201 tags |= 1 << tag;
1202
1203 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001204 if (!xa_is_sibling(rcu_dereference_raw(parent->slots[end])))
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001205 break;
1206 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1207 if (tags & (1 << tag))
1208 tag_set(parent, tag, end);
1209 /* rcu_assign_pointer ensures tags are set before RETRY */
1210 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1211 }
1212 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
Matthew Wilcox01959df2017-11-09 09:23:56 -05001213 parent->nr_values -= (end - offset);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001214
1215 if (order == parent->shift)
1216 return 0;
1217 if (order > parent->shift) {
1218 while (offset < end)
1219 offset += insert_entries(parent, &parent->slots[offset],
1220 RADIX_TREE_RETRY, order, true);
1221 return 0;
1222 }
1223
1224 node = parent;
1225
1226 for (;;) {
1227 if (node->shift > order) {
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05001228 child = radix_tree_node_alloc(gfp, node, root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -08001229 node->shift - RADIX_TREE_MAP_SHIFT,
1230 offset, 0, 0);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001231 if (!child)
1232 goto nomem;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001233 if (node != parent) {
1234 node->count++;
Matthew Wilcox12320d02017-02-13 15:22:48 -05001235 rcu_assign_pointer(node->slots[offset],
1236 node_to_entry(child));
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001237 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1238 if (tags & (1 << tag))
1239 tag_set(node, tag, offset);
1240 }
1241
1242 node = child;
1243 offset = 0;
1244 continue;
1245 }
1246
1247 n = insert_entries(node, &node->slots[offset],
1248 RADIX_TREE_RETRY, order, false);
1249 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1250
1251 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1252 if (tags & (1 << tag))
1253 tag_set(node, tag, offset);
1254 offset += n;
1255
1256 while (offset == RADIX_TREE_MAP_SIZE) {
1257 if (node == parent)
1258 break;
1259 offset = node->offset;
1260 child = node;
1261 node = node->parent;
1262 rcu_assign_pointer(node->slots[offset],
1263 node_to_entry(child));
1264 offset++;
1265 }
1266 if ((node == parent) && (offset == end))
1267 return 0;
1268 }
1269
1270 nomem:
1271 /* Shouldn't happen; did user forget to preload? */
1272 /* TODO: free all the allocated nodes */
1273 WARN_ON(1);
1274 return -ENOMEM;
1275}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001276#endif
1277
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001278static void node_tag_set(struct radix_tree_root *root,
1279 struct radix_tree_node *node,
1280 unsigned int tag, unsigned int offset)
1281{
1282 while (node) {
1283 if (tag_get(node, tag, offset))
1284 return;
1285 tag_set(node, tag, offset);
1286 offset = node->offset;
1287 node = node->parent;
1288 }
1289
1290 if (!root_tag_get(root, tag))
1291 root_tag_set(root, tag);
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/**
1295 * radix_tree_tag_set - set a tag on a radix tree node
1296 * @root: radix tree root
1297 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001298 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001300 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1301 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 * the root all the way down to the leaf node.
1303 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001304 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 * item is a bug.
1306 */
1307void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001308 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309{
Ross Zwislerfb969902016-05-20 17:02:32 -07001310 struct radix_tree_node *node, *parent;
1311 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001313 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001314 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001316 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001317 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001319 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001320 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001321 BUG_ON(!node);
1322
1323 if (!tag_get(parent, tag, offset))
1324 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
Nick Piggin612d6c12006-06-23 02:03:22 -07001327 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001328 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001329 root_tag_set(root, tag);
1330
Ross Zwislerfb969902016-05-20 17:02:32 -07001331 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333EXPORT_SYMBOL(radix_tree_tag_set);
1334
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001335/**
1336 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1337 * @root: radix tree root
1338 * @iter: iterator state
1339 * @tag: tag to set
1340 */
1341void radix_tree_iter_tag_set(struct radix_tree_root *root,
1342 const struct radix_tree_iter *iter, unsigned int tag)
1343{
1344 node_tag_set(root, iter->node, tag, iter_offset(iter));
1345}
1346
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001347static void node_tag_clear(struct radix_tree_root *root,
1348 struct radix_tree_node *node,
1349 unsigned int tag, unsigned int offset)
1350{
1351 while (node) {
1352 if (!tag_get(node, tag, offset))
1353 return;
1354 tag_clear(node, tag, offset);
1355 if (any_tag_set(node, tag))
1356 return;
1357
1358 offset = node->offset;
1359 node = node->parent;
1360 }
1361
1362 /* clear the root's tag bit */
1363 if (root_tag_get(root, tag))
1364 root_tag_clear(root, tag);
1365}
1366
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001367/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 * radix_tree_tag_clear - clear a tag on a radix tree node
1369 * @root: radix tree root
1370 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001371 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001373 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001374 * corresponding to @index in the radix tree. If this causes
1375 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 * next-to-leaf node, etc.
1377 *
1378 * Returns the address of the tagged item on success, else NULL. ie:
1379 * has the same return value and semantics as radix_tree_lookup().
1380 */
1381void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001382 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001384 struct radix_tree_node *node, *parent;
1385 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001386 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001388 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001389 if (index > maxindex)
1390 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Ross Zwisler00f47b52016-05-20 17:02:35 -07001392 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001394 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001395 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001396 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 }
1398
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001399 if (node)
1400 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Ross Zwisler00f47b52016-05-20 17:02:35 -07001402 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403}
1404EXPORT_SYMBOL(radix_tree_tag_clear);
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001407 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1408 * @root: radix tree root
1409 * @iter: iterator state
1410 * @tag: tag to clear
1411 */
1412void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1413 const struct radix_tree_iter *iter, unsigned int tag)
1414{
1415 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1416}
1417
1418/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001419 * radix_tree_tag_get - get a tag on a radix tree node
1420 * @root: radix tree root
1421 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001422 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001424 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001426 * 0: tag not present or not set
1427 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001428 *
1429 * Note that the return value of this function may not be relied on, even if
1430 * the RCU lock is held, unless tag modification and node deletion are excluded
1431 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001433int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001434 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001436 struct radix_tree_node *node, *parent;
1437 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Nick Piggin612d6c12006-06-23 02:03:22 -07001439 if (!root_tag_get(root, tag))
1440 return 0;
1441
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001442 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001443 if (index > maxindex)
1444 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001445
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001446 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001447 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001448
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001449 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001450 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001451
Ross Zwisler4589ba62016-05-20 17:02:38 -07001452 if (!tag_get(parent, tag, offset))
1453 return 0;
1454 if (node == RADIX_TREE_RETRY)
1455 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001457
1458 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Ross Zwisler21ef5332016-05-20 17:02:26 -07001462static inline void __set_iter_shift(struct radix_tree_iter *iter,
1463 unsigned int shift)
1464{
1465#ifdef CONFIG_RADIX_TREE_MULTIORDER
1466 iter->shift = shift;
1467#endif
1468}
1469
Matthew Wilcox148deab2016-12-14 15:08:49 -08001470/* Construct iter->tags bit-mask from node->tags[tag] array */
1471static void set_iter_tags(struct radix_tree_iter *iter,
1472 struct radix_tree_node *node, unsigned offset,
1473 unsigned tag)
1474{
1475 unsigned tag_long = offset / BITS_PER_LONG;
1476 unsigned tag_bit = offset % BITS_PER_LONG;
1477
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001478 if (!node) {
1479 iter->tags = 1;
1480 return;
1481 }
1482
Matthew Wilcox148deab2016-12-14 15:08:49 -08001483 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1484
1485 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1486 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1487 /* Pick tags from next element */
1488 if (tag_bit)
1489 iter->tags |= node->tags[tag][tag_long + 1] <<
1490 (BITS_PER_LONG - tag_bit);
1491 /* Clip chunk size, here only BITS_PER_LONG tags */
1492 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1493 }
1494}
1495
1496#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001497static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1498 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001499{
Matthew Wilcox148deab2016-12-14 15:08:49 -08001500 while (iter->index < iter->next_index) {
1501 *nodep = rcu_dereference_raw(*slot);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001502 if (*nodep && !xa_is_sibling(*nodep))
Matthew Wilcox148deab2016-12-14 15:08:49 -08001503 return slot;
1504 slot++;
1505 iter->index = __radix_tree_iter_add(iter, 1);
1506 iter->tags >>= 1;
1507 }
1508
1509 *nodep = NULL;
1510 return NULL;
1511}
1512
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001513void __rcu **__radix_tree_next_slot(void __rcu **slot,
1514 struct radix_tree_iter *iter, unsigned flags)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001515{
1516 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Ross Zwisler9f418222018-05-18 16:09:06 -07001517 struct radix_tree_node *node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001518
1519 slot = skip_siblings(&node, slot, iter);
1520
1521 while (radix_tree_is_internal_node(node)) {
1522 unsigned offset;
1523 unsigned long next_index;
1524
1525 if (node == RADIX_TREE_RETRY)
1526 return slot;
1527 node = entry_to_node(node);
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001528 iter->node = node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001529 iter->shift = node->shift;
1530
1531 if (flags & RADIX_TREE_ITER_TAGGED) {
1532 offset = radix_tree_find_next_bit(node, tag, 0);
1533 if (offset == RADIX_TREE_MAP_SIZE)
1534 return NULL;
1535 slot = &node->slots[offset];
1536 iter->index = __radix_tree_iter_add(iter, offset);
1537 set_iter_tags(iter, node, offset, tag);
1538 node = rcu_dereference_raw(*slot);
1539 } else {
1540 offset = 0;
1541 slot = &node->slots[0];
1542 for (;;) {
1543 node = rcu_dereference_raw(*slot);
1544 if (node)
1545 break;
1546 slot++;
1547 offset++;
1548 if (offset == RADIX_TREE_MAP_SIZE)
1549 return NULL;
1550 }
1551 iter->index = __radix_tree_iter_add(iter, offset);
1552 }
1553 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1554 goto none;
1555 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1556 if (next_index < iter->next_index)
1557 iter->next_index = next_index;
1558 }
1559
1560 return slot;
1561 none:
1562 iter->next_index = 0;
1563 return NULL;
1564}
1565EXPORT_SYMBOL(__radix_tree_next_slot);
1566#else
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001567static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1568 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001569{
1570 return slot;
1571}
1572#endif
1573
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001574void __rcu **radix_tree_iter_resume(void __rcu **slot,
1575 struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001576{
1577 struct radix_tree_node *node;
1578
1579 slot++;
1580 iter->index = __radix_tree_iter_add(iter, 1);
Matthew Wilcox148deab2016-12-14 15:08:49 -08001581 skip_siblings(&node, slot, iter);
1582 iter->next_index = iter->index;
1583 iter->tags = 0;
1584 return NULL;
1585}
1586EXPORT_SYMBOL(radix_tree_iter_resume);
1587
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001588/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001589 * radix_tree_next_chunk - find next chunk of slots for iteration
1590 *
1591 * @root: radix tree root
1592 * @iter: iterator state
1593 * @flags: RADIX_TREE_ITER_* flags and tag index
1594 * Returns: pointer to chunk first slot, or NULL if iteration is over
1595 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001596void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001597 struct radix_tree_iter *iter, unsigned flags)
1598{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001599 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001600 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001601 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001602
1603 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1604 return NULL;
1605
1606 /*
1607 * Catch next_index overflow after ~0UL. iter->index never overflows
1608 * during iterating; it can be zero only at the beginning.
1609 * And we cannot overflow iter->next_index in a single step,
1610 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001611 *
1612 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001613 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001614 */
1615 index = iter->next_index;
1616 if (!index && iter->index)
1617 return NULL;
1618
Ross Zwisler21ef5332016-05-20 17:02:26 -07001619 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001620 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001621 if (index > maxindex)
1622 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001623 if (!child)
1624 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001625
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001626 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001627 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001628 iter->index = index;
1629 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001630 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001631 iter->node = NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001632 __set_iter_shift(iter, 0);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001633 return (void __rcu **)&root->xa_head;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001634 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001635
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001636 do {
1637 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001638 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001639
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001640 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001641 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001642 /* Hole detected */
1643 if (flags & RADIX_TREE_ITER_CONTIG)
1644 return NULL;
1645
1646 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001647 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001648 offset + 1);
1649 else
1650 while (++offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001651 void *slot = rcu_dereference_raw(
1652 node->slots[offset]);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001653 if (xa_is_sibling(slot))
Ross Zwisler21ef5332016-05-20 17:02:26 -07001654 continue;
1655 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001656 break;
1657 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001658 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001659 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001660 /* Overflow after ~0UL */
1661 if (!index)
1662 return NULL;
1663 if (offset == RADIX_TREE_MAP_SIZE)
1664 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001665 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001666 }
1667
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001668 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001669 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001670 if (child == RADIX_TREE_RETRY)
1671 break;
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001672 } while (node->shift && radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001673
1674 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001675 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1676 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001677 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001678 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001679
Matthew Wilcox148deab2016-12-14 15:08:49 -08001680 if (flags & RADIX_TREE_ITER_TAGGED)
1681 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001682
1683 return node->slots + offset;
1684}
1685EXPORT_SYMBOL(radix_tree_next_chunk);
1686
1687/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1689 * @root: radix tree root
1690 * @results: where the results of the lookup are placed
1691 * @first_index: start the lookup from this key
1692 * @max_items: place up to this many items at *results
1693 *
1694 * Performs an index-ascending scan of the tree for present items. Places
1695 * them at *@results and returns the number of items which were placed at
1696 * *@results.
1697 *
1698 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001699 *
1700 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1701 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001702 * an atomic snapshot of the tree at a single point in time, the
1703 * semantics of an RCU protected gang lookup are as though multiple
1704 * radix_tree_lookups have been issued in individual locks, and results
1705 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
1707unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001708radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 unsigned long first_index, unsigned int max_items)
1710{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001711 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001712 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001713 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001715 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001716 return 0;
1717
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001718 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001719 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001720 if (!results[ret])
1721 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001722 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001723 slot = radix_tree_iter_retry(&iter);
1724 continue;
1725 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001726 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return ret;
1731}
1732EXPORT_SYMBOL(radix_tree_gang_lookup);
1733
Nick Piggin47feff22008-07-25 19:45:29 -07001734/**
1735 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1736 * @root: radix tree root
1737 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001738 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001739 * @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 * their slots at *@results and returns the number of items which were
1744 * placed at *@results.
1745 *
1746 * The implementation is naive.
1747 *
1748 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1749 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1750 * protection, radix_tree_deref_slot may fail requiring a retry.
1751 */
1752unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001753radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001754 void __rcu ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001755 unsigned long first_index, unsigned int max_items)
1756{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001757 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001758 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001759 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001760
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001761 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001762 return 0;
1763
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001764 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1765 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001766 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001767 indices[ret] = iter.index;
1768 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001769 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001770 }
1771
1772 return ret;
1773}
1774EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776/**
1777 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1778 * based on a tag
1779 * @root: radix tree root
1780 * @results: where the results of the lookup are placed
1781 * @first_index: start the lookup from this key
1782 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001783 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 *
1785 * Performs an index-ascending scan of the tree for present items which
1786 * have the tag indexed by @tag set. Places the items at *@results and
1787 * returns the number of items which were placed at *@results.
1788 */
1789unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001790radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001791 unsigned long first_index, unsigned int max_items,
1792 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001794 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001795 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001796 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001798 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001799 return 0;
1800
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001801 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001802 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001803 if (!results[ret])
1804 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001805 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001806 slot = radix_tree_iter_retry(&iter);
1807 continue;
1808 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001809 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 return ret;
1814}
1815EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1816
1817/**
Nick Piggin47feff22008-07-25 19:45:29 -07001818 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1819 * radix tree based on a tag
1820 * @root: radix tree root
1821 * @results: where the results of the lookup are placed
1822 * @first_index: start the lookup from this key
1823 * @max_items: place up to this many items at *results
1824 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1825 *
1826 * Performs an index-ascending scan of the tree for present items which
1827 * have the tag indexed by @tag set. Places the slots at *@results and
1828 * returns the number of slots which were placed at *@results.
1829 */
1830unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001831radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001832 void __rcu ***results, unsigned long first_index,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001833 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001834{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001835 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001836 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001837 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001838
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001839 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001840 return 0;
1841
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001842 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1843 results[ret] = slot;
1844 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001845 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001846 }
1847
1848 return ret;
1849}
1850EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1851
Nick Piggin47feff22008-07-25 19:45:29 -07001852/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001853 * __radix_tree_delete_node - try to free node after clearing a slot
1854 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001855 * @node: node containing @index
Johannes Weinerea07b862017-01-06 19:21:43 -05001856 * @update_node: callback for changing leaf nodes
Johannes Weiner139e5612014-04-03 14:47:54 -07001857 *
1858 * After clearing the slot at @index in @node from radix tree
1859 * rooted at @root, call this function to attempt freeing the
1860 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001861 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001862void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weinerea07b862017-01-06 19:21:43 -05001863 struct radix_tree_node *node,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001864 radix_tree_update_node_t update_node)
Johannes Weiner139e5612014-04-03 14:47:54 -07001865{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001866 delete_node(root, node, update_node);
Johannes Weiner139e5612014-04-03 14:47:54 -07001867}
1868
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001869static bool __radix_tree_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001870 struct radix_tree_node *node, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001871{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001872 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox01959df2017-11-09 09:23:56 -05001873 int values = xa_is_value(old) ? -1 : 0;
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001874 unsigned offset = get_slot_offset(node, slot);
1875 int tag;
1876
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001877 if (is_idr(root))
1878 node_tag_set(root, node, IDR_FREE, offset);
1879 else
1880 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1881 node_tag_clear(root, node, tag, offset);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001882
Matthew Wilcox01959df2017-11-09 09:23:56 -05001883 replace_slot(slot, NULL, node, -1, values);
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001884 return node && delete_node(root, node, NULL);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001885}
1886
Johannes Weiner139e5612014-04-03 14:47:54 -07001887/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001888 * radix_tree_iter_delete - delete the entry at this iterator position
1889 * @root: radix tree root
1890 * @iter: iterator state
1891 * @slot: pointer to slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001893 * Delete the entry at the position currently pointed to by the iterator.
1894 * This may result in the current node being freed; if it is, the iterator
1895 * is advanced so that it will not reference the freed memory. This
1896 * function may be called without any locking if there are no other threads
1897 * which can access this tree.
1898 */
1899void radix_tree_iter_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001900 struct radix_tree_iter *iter, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001901{
1902 if (__radix_tree_delete(root, iter->node, slot))
1903 iter->index = iter->next_index;
1904}
Chris Wilsond1b48c12017-08-16 09:52:08 +01001905EXPORT_SYMBOL(radix_tree_iter_delete);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001906
1907/**
1908 * radix_tree_delete_item - delete an item from a radix tree
1909 * @root: radix tree root
1910 * @index: index key
1911 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001913 * Remove @item at @index from the radix tree rooted at @root.
1914 *
1915 * Return: the deleted entry, or %NULL if it was not present
1916 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001918void *radix_tree_delete_item(struct radix_tree_root *root,
1919 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001921 struct radix_tree_node *node = NULL;
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07001922 void __rcu **slot = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001923 void *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Johannes Weiner139e5612014-04-03 14:47:54 -07001925 entry = __radix_tree_lookup(root, index, &node, &slot);
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07001926 if (!slot)
1927 return NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001928 if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE,
1929 get_slot_offset(node, slot))))
Johannes Weiner139e5612014-04-03 14:47:54 -07001930 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Johannes Weiner139e5612014-04-03 14:47:54 -07001932 if (item && entry != item)
1933 return NULL;
1934
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001935 __radix_tree_delete(root, node, slot);
Christoph Lameter201b6262005-09-06 15:16:46 -07001936
Johannes Weiner139e5612014-04-03 14:47:54 -07001937 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001939EXPORT_SYMBOL(radix_tree_delete_item);
1940
1941/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001942 * radix_tree_delete - delete an entry from a radix tree
1943 * @root: radix tree root
1944 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001945 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001946 * Remove the entry at @index from the radix tree rooted at @root.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001947 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001948 * Return: The deleted entry, or %NULL if it was not present.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001949 */
1950void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1951{
1952 return radix_tree_delete_item(root, index, NULL);
1953}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954EXPORT_SYMBOL(radix_tree_delete);
1955
Johannes Weinerd3798ae2016-10-04 22:02:08 +02001956void radix_tree_clear_tags(struct radix_tree_root *root,
1957 struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001958 void __rcu **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001959{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001960 if (node) {
1961 unsigned int tag, offset = get_slot_offset(node, slot);
1962 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1963 node_tag_clear(root, node, tag, offset);
1964 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001965 root_tag_clear_all(root);
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001966 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/**
1970 * radix_tree_tagged - test whether any items in the tree are tagged
1971 * @root: radix tree root
1972 * @tag: tag to test
1973 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001974int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Nick Piggin612d6c12006-06-23 02:03:22 -07001976 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978EXPORT_SYMBOL(radix_tree_tagged);
1979
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001980/**
1981 * idr_preload - preload for idr_alloc()
1982 * @gfp_mask: allocation mask to use for preloading
1983 *
1984 * Preallocate memory to use for the next call to idr_alloc(). This function
1985 * returns with preemption disabled. It will be enabled by idr_preload_end().
1986 */
1987void idr_preload(gfp_t gfp_mask)
1988{
Eric Dumazetbc9ae222017-09-08 16:15:54 -07001989 if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE))
1990 preempt_disable();
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001991}
1992EXPORT_SYMBOL(idr_preload);
1993
Matthew Wilcox460488c2017-11-28 15:16:24 -05001994void __rcu **idr_get_free(struct radix_tree_root *root,
Chris Mi388f79f2017-08-30 02:31:57 -04001995 struct radix_tree_iter *iter, gfp_t gfp,
1996 unsigned long max)
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001997{
1998 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001999 void __rcu **slot = (void __rcu **)&root->xa_head;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002000 unsigned long maxindex, start = iter->next_index;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002001 unsigned int shift, offset = 0;
2002
2003 grow:
2004 shift = radix_tree_load_root(root, &child, &maxindex);
2005 if (!radix_tree_tagged(root, IDR_FREE))
2006 start = max(start, maxindex + 1);
2007 if (start > max)
2008 return ERR_PTR(-ENOSPC);
2009
2010 if (start > maxindex) {
2011 int error = radix_tree_extend(root, gfp, start, shift);
2012 if (error < 0)
2013 return ERR_PTR(error);
2014 shift = error;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05002015 child = rcu_dereference_raw(root->xa_head);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002016 }
Matthew Wilcox66ee6202018-06-25 06:56:50 -04002017 if (start == 0 && shift == 0)
2018 shift = RADIX_TREE_MAP_SHIFT;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002019
2020 while (shift) {
2021 shift -= RADIX_TREE_MAP_SHIFT;
2022 if (child == NULL) {
2023 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05002024 child = radix_tree_node_alloc(gfp, node, root, shift,
2025 offset, 0, 0);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002026 if (!child)
2027 return ERR_PTR(-ENOMEM);
2028 all_tag_set(child, IDR_FREE);
2029 rcu_assign_pointer(*slot, node_to_entry(child));
2030 if (node)
2031 node->count++;
2032 } else if (!radix_tree_is_internal_node(child))
2033 break;
2034
2035 node = entry_to_node(child);
2036 offset = radix_tree_descend(node, &child, start);
2037 if (!tag_get(node, IDR_FREE, offset)) {
2038 offset = radix_tree_find_next_bit(node, IDR_FREE,
2039 offset + 1);
2040 start = next_index(start, node, offset);
2041 if (start > max)
2042 return ERR_PTR(-ENOSPC);
2043 while (offset == RADIX_TREE_MAP_SIZE) {
2044 offset = node->offset + 1;
2045 node = node->parent;
2046 if (!node)
2047 goto grow;
2048 shift = node->shift;
2049 }
2050 child = rcu_dereference_raw(node->slots[offset]);
2051 }
2052 slot = &node->slots[offset];
2053 }
2054
2055 iter->index = start;
2056 if (node)
2057 iter->next_index = 1 + min(max, (start | node_maxindex(node)));
2058 else
2059 iter->next_index = 1;
2060 iter->node = node;
2061 __set_iter_shift(iter, shift);
2062 set_iter_tags(iter, node, offset, IDR_FREE);
2063
2064 return slot;
2065}
2066
2067/**
2068 * idr_destroy - release all internal memory from an IDR
2069 * @idr: idr handle
2070 *
2071 * After this function is called, the IDR is empty, and may be reused or
2072 * the data structure containing it may be freed.
2073 *
2074 * A typical clean-up sequence for objects stored in an idr tree will use
2075 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
2076 * free the memory used to keep track of those objects.
2077 */
2078void idr_destroy(struct idr *idr)
2079{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05002080 struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.xa_head);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002081 if (radix_tree_is_internal_node(node))
2082 radix_tree_free_nodes(node);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05002083 idr->idr_rt.xa_head = NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002084 root_tag_set(&idr->idr_rt, IDR_FREE);
2085}
2086EXPORT_SYMBOL(idr_destroy);
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static void
Johannes Weiner449dd692014-04-03 14:47:56 -07002089radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090{
Johannes Weiner449dd692014-04-03 14:47:56 -07002091 struct radix_tree_node *node = arg;
2092
2093 memset(node, 0, sizeof(*node));
2094 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095}
2096
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002097static __init unsigned long __maxindex(unsigned int height)
2098{
2099 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
2100 int shift = RADIX_TREE_INDEX_BITS - width;
2101
2102 if (shift < 0)
2103 return ~0UL;
2104 if (shift >= BITS_PER_LONG)
2105 return 0UL;
2106 return ~0UL >> shift;
2107}
2108
2109static __init void radix_tree_init_maxnodes(void)
2110{
2111 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
2112 unsigned int i, j;
2113
2114 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
2115 height_to_maxindex[i] = __maxindex(i);
2116 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
2117 for (j = i; j > 0; j--)
2118 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
2119 }
2120}
2121
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002122static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002124 struct radix_tree_preload *rtp;
2125 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002127 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002128 rtp = &per_cpu(radix_tree_preloads, cpu);
2129 while (rtp->nr) {
2130 node = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -05002131 rtp->nodes = node->parent;
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002132 kmem_cache_free(radix_tree_node_cachep, node);
2133 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002134 }
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138void __init radix_tree_init(void)
2139{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002140 int ret;
Michal Hocko7e784422017-05-03 14:53:09 -07002141
2142 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32);
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -07002143 BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04002144 BUILD_BUG_ON(XA_CHUNK_SIZE > 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2146 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07002147 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2148 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002149 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002150 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2151 NULL, radix_tree_cpu_dead);
2152 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}