blob: 14d51548bea6414f9b256dd0179830d07dce1581 [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
Jeff Moyer26fb1582007-10-16 01:24:49 -070044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Radix tree node cache.
46 */
Matthew Wilcox58d6ea32017-11-10 15:15:08 -050047struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
Nick Piggin55368052012-05-29 15:07:34 -070050 * The radix tree is variable-height, so an insert operation not only has
51 * to build the branch to its corresponding item, it also has to build the
52 * branch to existing items if the size has to be increased (by
53 * radix_tree_extend).
54 *
55 * The worst case is a zero height tree with just a single item at index 0,
56 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
57 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
58 * Hence:
59 */
60#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
61
62/*
Matthew Wilcox0a835c42016-12-20 10:27:56 -050063 * The IDR does not have to be as high as the radix tree since it uses
64 * signed integers, not unsigned longs.
65 */
66#define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1)
67#define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \
68 RADIX_TREE_MAP_SHIFT))
69#define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1)
70
71/*
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -050072 * The IDA is even shorter since it uses a bitmap at the last level.
73 */
74#define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS))
75#define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \
76 RADIX_TREE_MAP_SHIFT))
77#define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1)
78
79/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * Per-cpu pool of preloaded nodes
81 */
82struct radix_tree_preload {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -070083 unsigned nr;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050084 /* nodes->parent points to next preallocated node */
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -070085 struct radix_tree_node *nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080087static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Matthew Wilcox148deab2016-12-14 15:08:49 -080089static inline struct radix_tree_node *entry_to_node(void *ptr)
90{
91 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
92}
93
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070094static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080095{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070096 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -080097}
98
Matthew Wilcox02c02bf2017-11-03 23:09:45 -040099#define RADIX_TREE_RETRY XA_RETRY_ENTRY
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700100
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500101static inline unsigned long
102get_slot_offset(const struct radix_tree_node *parent, void __rcu **slot)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700103{
Matthew Wilcox76f070b2018-08-18 07:05:50 -0400104 return parent ? slot - parent->slots : 0;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700105}
106
Matthew Wilcox35534c82016-12-19 17:43:19 -0500107static unsigned int radix_tree_descend(const struct radix_tree_node *parent,
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700108 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700109{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700110 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500111 void __rcu **entry = rcu_dereference_raw(parent->slots[offset]);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700112
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700113 *nodep = (void *)entry;
114 return offset;
115}
116
Matthew Wilcox35534c82016-12-19 17:43:19 -0500117static inline gfp_t root_gfp_mask(const struct radix_tree_root *root)
Nick Piggin612d6c12006-06-23 02:03:22 -0700118{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500119 return root->xa_flags & (__GFP_BITS_MASK & ~GFP_ZONEMASK);
Nick Piggin612d6c12006-06-23 02:03:22 -0700120}
121
Nick Piggin643b52b2008-06-12 15:21:52 -0700122static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
123 int offset)
124{
125 __set_bit(offset, node->tags[tag]);
126}
127
128static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
129 int offset)
130{
131 __clear_bit(offset, node->tags[tag]);
132}
133
Matthew Wilcox35534c82016-12-19 17:43:19 -0500134static inline int tag_get(const struct radix_tree_node *node, unsigned int tag,
Nick Piggin643b52b2008-06-12 15:21:52 -0700135 int offset)
136{
137 return test_bit(offset, node->tags[tag]);
138}
139
Matthew Wilcox35534c82016-12-19 17:43:19 -0500140static inline void root_tag_set(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700141{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500142 root->xa_flags |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700143}
144
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700145static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700146{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500147 root->xa_flags &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700148}
149
150static inline void root_tag_clear_all(struct radix_tree_root *root)
151{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500152 root->xa_flags &= (__force gfp_t)((1 << ROOT_TAG_SHIFT) - 1);
Nick Piggin643b52b2008-06-12 15:21:52 -0700153}
154
Matthew Wilcox35534c82016-12-19 17:43:19 -0500155static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700156{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500157 return (__force int)root->xa_flags & (1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700158}
159
Matthew Wilcox35534c82016-12-19 17:43:19 -0500160static inline unsigned root_tags_get(const struct radix_tree_root *root)
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700161{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500162 return (__force unsigned)root->xa_flags >> ROOT_TAG_SHIFT;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500163}
164
165static inline bool is_idr(const struct radix_tree_root *root)
166{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500167 return !!(root->xa_flags & ROOT_IS_IDR);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700168}
169
Nick Piggin643b52b2008-06-12 15:21:52 -0700170/*
171 * Returns 1 if any slot in the node has this tag set.
172 * Otherwise returns 0.
173 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500174static inline int any_tag_set(const struct radix_tree_node *node,
175 unsigned int tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700176{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700177 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700178 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
179 if (node->tags[tag][idx])
180 return 1;
181 }
182 return 0;
183}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700184
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500185static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag)
186{
187 bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE);
188}
189
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700190/**
191 * radix_tree_find_next_bit - find the next set bit in a memory region
192 *
193 * @addr: The address to base the search on
194 * @size: The bitmap size in bits
195 * @offset: The bitnumber to start searching at
196 *
197 * Unrollable variant of find_next_bit() for constant size arrays.
198 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
199 * Returns next bit offset, or size if nothing found.
200 */
201static __always_inline unsigned long
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800202radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
203 unsigned long offset)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700204{
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800205 const unsigned long *addr = node->tags[tag];
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700206
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800207 if (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700208 unsigned long tmp;
209
210 addr += offset / BITS_PER_LONG;
211 tmp = *addr >> (offset % BITS_PER_LONG);
212 if (tmp)
213 return __ffs(tmp) + offset;
214 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800215 while (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700216 tmp = *++addr;
217 if (tmp)
218 return __ffs(tmp) + offset;
219 offset += BITS_PER_LONG;
220 }
221 }
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800222 return RADIX_TREE_MAP_SIZE;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700223}
224
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800225static unsigned int iter_offset(const struct radix_tree_iter *iter)
226{
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400227 return iter->index & RADIX_TREE_MAP_MASK;
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800228}
229
Matthew Wilcox218ed752016-12-14 15:08:43 -0800230/*
231 * The maximum index which can be stored in a radix tree
232 */
233static inline unsigned long shift_maxindex(unsigned int shift)
234{
235 return (RADIX_TREE_MAP_SIZE << shift) - 1;
236}
237
Matthew Wilcox35534c82016-12-19 17:43:19 -0500238static inline unsigned long node_maxindex(const struct radix_tree_node *node)
Matthew Wilcox218ed752016-12-14 15:08:43 -0800239{
240 return shift_maxindex(node->shift);
241}
242
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500243static unsigned long next_index(unsigned long index,
244 const struct radix_tree_node *node,
245 unsigned long offset)
246{
247 return (index & ~node_maxindex(node)) + (offset << node->shift);
248}
249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250/*
251 * This assumes that the caller has performed appropriate preallocation, and
252 * that the caller has pinned this thread of control to the current CPU.
253 */
254static struct radix_tree_node *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500255radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500256 struct radix_tree_root *root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800257 unsigned int shift, unsigned int offset,
Matthew Wilcox01959df2017-11-09 09:23:56 -0500258 unsigned int count, unsigned int nr_values)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Nick Piggine2848a02008-02-04 22:29:10 -0800260 struct radix_tree_node *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Jan Kara5e4c0d972013-09-11 14:26:05 -0700262 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700263 * Preload code isn't irq safe and it doesn't make sense to use
264 * preloading during an interrupt anyway as all the allocations have
265 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700266 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800267 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct radix_tree_preload *rtp;
269
Nick Piggine2848a02008-02-04 22:29:10 -0800270 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700271 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700272 * cache first for the new node to get accounted to the memory
273 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700274 */
275 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700276 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700277 if (ret)
278 goto out;
279
280 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800281 * Provided the caller has preloaded here, we will always
282 * succeed in getting a node here (and never reach
283 * kmem_cache_alloc)
284 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700285 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700287 ret = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500288 rtp->nodes = ret->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 rtp->nr--;
290 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700291 /*
292 * Update the allocation stack trace as this is more useful
293 * for debugging.
294 */
295 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700298 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700299out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700300 BUG_ON(radix_tree_is_internal_node(ret));
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800301 if (ret) {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800302 ret->shift = shift;
303 ret->offset = offset;
304 ret->count = count;
Matthew Wilcox01959df2017-11-09 09:23:56 -0500305 ret->nr_values = nr_values;
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500306 ret->parent = parent;
Matthew Wilcox01959df2017-11-09 09:23:56 -0500307 ret->array = root;
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return ret;
310}
311
Matthew Wilcox58d6ea32017-11-10 15:15:08 -0500312void radix_tree_node_rcu_free(struct rcu_head *head)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800313{
314 struct radix_tree_node *node =
315 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700316
317 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800318 * Must only free zeroed nodes into the slab. We can be left with
319 * non-NULL entries by radix_tree_free_nodes, so clear the entries
320 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700321 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800322 memset(node->slots, 0, sizeof(node->slots));
323 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800324 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700325
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800326 kmem_cache_free(radix_tree_node_cachep, node);
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static inline void
330radix_tree_node_free(struct radix_tree_node *node)
331{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800332 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335/*
336 * Load up this CPU's radix_tree_node buffer with sufficient objects to
337 * ensure that the addition of a single element in the tree cannot fail. On
338 * success, return zero, with preemption disabled. On error, return -ENOMEM
339 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000340 *
341 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800342 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -0700344static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 struct radix_tree_preload *rtp;
347 struct radix_tree_node *node;
348 int ret = -ENOMEM;
349
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700350 /*
351 * Nodes preloaded by one cgroup can be be used by another cgroup, so
352 * they should never be accounted to any particular memory cgroup.
353 */
354 gfp_mask &= ~__GFP_ACCOUNT;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700357 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700358 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700360 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (node == NULL)
362 goto out;
363 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700364 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700365 if (rtp->nr < nr) {
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500366 node->parent = rtp->nodes;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700367 rtp->nodes = node;
368 rtp->nr++;
369 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 ret = 0;
374out:
375 return ret;
376}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700377
378/*
379 * Load up this CPU's radix_tree_node buffer with sufficient objects to
380 * ensure that the addition of a single element in the tree cannot fail. On
381 * success, return zero, with preemption disabled. On error, return -ENOMEM
382 * with preemption not disabled.
383 *
384 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800385 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700386 */
387int radix_tree_preload(gfp_t gfp_mask)
388{
389 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800390 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700391 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700392}
David Chinnerd7f09232007-07-14 16:05:04 +1000393EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Nick Piggin6e954b92006-01-08 01:01:40 -0800395/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700396 * The same as above function, except we don't guarantee preloading happens.
397 * We do it, if we decide it helps. On success, return zero with preemption
398 * disabled. On error, return -ENOMEM with preemption not disabled.
399 */
400int radix_tree_maybe_preload(gfp_t gfp_mask)
401{
Mel Gormand0164ad2015-11-06 16:28:21 -0800402 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700403 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700404 /* Preloading doesn't help anything with this gfp mask, skip it */
405 preempt_disable();
406 return 0;
407}
408EXPORT_SYMBOL(radix_tree_maybe_preload);
409
Matthew Wilcox35534c82016-12-19 17:43:19 -0500410static unsigned radix_tree_load_root(const struct radix_tree_root *root,
Matthew Wilcox1456a432016-05-20 17:02:08 -0700411 struct radix_tree_node **nodep, unsigned long *maxindex)
412{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500413 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700414
415 *nodep = node;
416
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700417 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700418 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700419 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700420 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700421 }
422
423 *maxindex = 0;
424 return 0;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
428 * Extend a radix tree so it can store key @index.
429 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500430static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700431 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500433 void *entry;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700434 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int tag;
436
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700437 /* Figure out what the shift should be. */
438 maxshift = shift;
439 while (index > shift_maxindex(maxshift))
440 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500442 entry = rcu_dereference_raw(root->xa_head);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500443 if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 do {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500447 struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500448 root, shift, 0, 1, 0);
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700449 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return -ENOMEM;
451
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500452 if (is_idr(root)) {
453 all_tag_set(node, IDR_FREE);
454 if (!root_tag_get(root, IDR_FREE)) {
455 tag_clear(node, IDR_FREE, 0);
456 root_tag_set(root, IDR_FREE);
457 }
458 } else {
459 /* Propagate the aggregated tag info to the new child */
460 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
461 if (root_tag_get(root, tag))
462 tag_set(node, tag, 0);
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700466 BUG_ON(shift > BITS_PER_LONG);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500467 if (radix_tree_is_internal_node(entry)) {
468 entry_to_node(entry)->parent = node;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400469 } else if (xa_is_value(entry)) {
Matthew Wilcox01959df2017-11-09 09:23:56 -0500470 /* Moving a value entry root->xa_head to a node */
471 node->nr_values = 1;
Johannes Weinerf7942432016-12-12 16:43:41 -0800472 }
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500473 /*
474 * entry was already in the radix tree, so we do not need
475 * rcu_assign_pointer here
476 */
477 node->slots[0] = (void __rcu *)entry;
478 entry = node_to_entry(node);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500479 rcu_assign_pointer(root->xa_head, entry);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700480 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700481 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700483 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
486/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800487 * radix_tree_shrink - shrink radix tree to minimum height
488 * @root radix tree root
489 */
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400490static inline bool radix_tree_shrink(struct radix_tree_root *root)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800491{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500492 bool shrunk = false;
493
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800494 for (;;) {
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500495 struct radix_tree_node *node = rcu_dereference_raw(root->xa_head);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800496 struct radix_tree_node *child;
497
498 if (!radix_tree_is_internal_node(node))
499 break;
500 node = entry_to_node(node);
501
502 /*
503 * The candidate node has more than one child, or its child
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400504 * is not at the leftmost slot, we cannot shrink.
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800505 */
506 if (node->count != 1)
507 break;
Matthew Wilcox12320d02017-02-13 15:22:48 -0500508 child = rcu_dereference_raw(node->slots[0]);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800509 if (!child)
510 break;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800511
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400512 /*
513 * For an IDR, we must not shrink entry 0 into the root in
514 * case somebody calls idr_replace() with a pointer that
515 * appears to be an internal entry
516 */
517 if (!node->shift && is_idr(root))
518 break;
519
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800520 if (radix_tree_is_internal_node(child))
521 entry_to_node(child)->parent = NULL;
522
523 /*
524 * We don't need rcu_assign_pointer(), since we are simply
525 * moving the node from one part of the tree to another: if it
526 * was safe to dereference the old pointer to it
527 * (node->slots[0]), it will be safe to dereference the new
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500528 * one (root->xa_head) as far as dependent read barriers go.
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800529 */
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500530 root->xa_head = (void __rcu *)child;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500531 if (is_idr(root) && !tag_get(node, IDR_FREE, 0))
532 root_tag_clear(root, IDR_FREE);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800533
534 /*
535 * We have a dilemma here. The node's slot[0] must not be
536 * NULLed in case there are concurrent lookups expecting to
537 * find the item. However if this was a bottom-level node,
538 * then it may be subject to the slot pointer being visible
539 * to callers dereferencing it. If item corresponding to
540 * slot[0] is subsequently deleted, these callers would expect
541 * their slot to become empty sooner or later.
542 *
543 * For example, lockless pagecache will look up a slot, deref
544 * the page pointer, and if the page has 0 refcount it means it
545 * was concurrently deleted from pagecache so try the deref
546 * again. Fortunately there is already a requirement for logic
547 * to retry the entire slot lookup -- the indirect pointer
548 * problem (replacing direct root node with an indirect pointer
549 * also results in a stale slot). So tag the slot as indirect
550 * to force callers to retry.
551 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800552 node->count = 0;
553 if (!radix_tree_is_internal_node(child)) {
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500554 node->slots[0] = (void __rcu *)RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800555 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800556
Johannes Weinerea07b862017-01-06 19:21:43 -0500557 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800558 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500559 shrunk = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800560 }
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500561
562 return shrunk;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800563}
564
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500565static bool delete_node(struct radix_tree_root *root,
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400566 struct radix_tree_node *node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800567{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500568 bool deleted = false;
569
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800570 do {
571 struct radix_tree_node *parent;
572
573 if (node->count) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500574 if (node_to_entry(node) ==
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500575 rcu_dereference_raw(root->xa_head))
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400576 deleted |= radix_tree_shrink(root);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500577 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800578 }
579
580 parent = node->parent;
581 if (parent) {
582 parent->slots[node->offset] = NULL;
583 parent->count--;
584 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500585 /*
586 * Shouldn't the tags already have all been cleared
587 * by the caller?
588 */
589 if (!is_idr(root))
590 root_tag_clear_all(root);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500591 root->xa_head = NULL;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800592 }
593
Johannes Weinerea07b862017-01-06 19:21:43 -0500594 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800595 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500596 deleted = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800597
598 node = parent;
599 } while (node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500600
601 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800602}
603
604/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700605 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * @root: radix tree root
607 * @index: index key
Johannes Weiner139e5612014-04-03 14:47:54 -0700608 * @nodep: returns node
609 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700611 * Create, if necessary, and return the node and slot for an item
612 * at position @index in the radix tree @root.
613 *
614 * Until there is more than one item in the tree, no nodes are
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500615 * allocated and @root->xa_head is used as a direct slot instead of
Johannes Weiner139e5612014-04-03 14:47:54 -0700616 * pointing to a node, in which case *@nodep will be NULL.
617 *
618 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
Matthew Wilcox74d60952017-11-17 10:01:45 -0500620static int __radix_tree_create(struct radix_tree_root *root,
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400621 unsigned long index, struct radix_tree_node **nodep,
622 void __rcu ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700624 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500625 void __rcu **slot = (void __rcu **)&root->xa_head;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700626 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700627 unsigned int shift, offset = 0;
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400628 unsigned long max = index;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500629 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700630
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700631 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* Make sure the tree is high enough. */
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700634 if (max > maxindex) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500635 int error = radix_tree_extend(root, gfp, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700636 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700638 shift = error;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500639 child = rcu_dereference_raw(root->xa_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 }
641
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400642 while (shift > 0) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700643 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700644 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500646 child = radix_tree_node_alloc(gfp, node, root, shift,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800647 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700648 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700650 rcu_assign_pointer(*slot, node_to_entry(child));
651 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700653 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700654 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700657 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700658 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700659 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700660 }
661
Johannes Weiner139e5612014-04-03 14:47:54 -0700662 if (nodep)
663 *nodep = node;
664 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700665 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700666 return 0;
667}
668
Matthew Wilcox175542f2016-12-14 15:08:58 -0800669/*
670 * Free any nodes below this node. The tree is presumed to not need
671 * shrinking, and any user data in the tree is presumed to not need a
672 * destructor called on it. If we need to add a destructor, we can
673 * add that functionality later. Note that we may not clear tags or
674 * slots from the tree as an RCU walker may still have a pointer into
675 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
676 * but we'll still have to clear those in rcu_free.
677 */
678static void radix_tree_free_nodes(struct radix_tree_node *node)
679{
680 unsigned offset = 0;
681 struct radix_tree_node *child = entry_to_node(node);
682
683 for (;;) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500684 void *entry = rcu_dereference_raw(child->slots[offset]);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -0400685 if (xa_is_node(entry) && child->shift) {
Matthew Wilcox175542f2016-12-14 15:08:58 -0800686 child = entry_to_node(entry);
687 offset = 0;
688 continue;
689 }
690 offset++;
691 while (offset == RADIX_TREE_MAP_SIZE) {
692 struct radix_tree_node *old = child;
693 offset = child->offset + 1;
694 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800695 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800696 radix_tree_node_free(old);
697 if (old == entry_to_node(node))
698 return;
699 }
700 }
701}
702
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500703static inline int insert_entries(struct radix_tree_node *node,
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400704 void __rcu **slot, void *item, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800705{
706 if (*slot)
707 return -EEXIST;
708 rcu_assign_pointer(*slot, item);
709 if (node) {
710 node->count++;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400711 if (xa_is_value(item))
Matthew Wilcox01959df2017-11-09 09:23:56 -0500712 node->nr_values++;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800713 }
714 return 1;
715}
Matthew Wilcox175542f2016-12-14 15:08:58 -0800716
Johannes Weiner139e5612014-04-03 14:47:54 -0700717/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700718 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700719 * @root: radix tree root
720 * @index: index key
721 * @item: item to insert
722 *
723 * Insert an item into the radix tree at position @index.
724 */
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400725int radix_tree_insert(struct radix_tree_root *root, unsigned long index,
726 void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700727{
728 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500729 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700730 int error;
731
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700732 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700733
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400734 error = __radix_tree_create(root, index, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700735 if (error)
736 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800737
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400738 error = insert_entries(node, slot, item, false);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800739 if (error < 0)
740 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700741
Nick Piggin612d6c12006-06-23 02:03:22 -0700742 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700743 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700744 BUG_ON(tag_get(node, 0, offset));
745 BUG_ON(tag_get(node, 1, offset));
746 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700747 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700748 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return 0;
752}
Matthew Wilcox3a08cd52018-09-22 16:14:30 -0400753EXPORT_SYMBOL(radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Johannes Weiner139e5612014-04-03 14:47:54 -0700755/**
756 * __radix_tree_lookup - lookup an item in a radix tree
757 * @root: radix tree root
758 * @index: index key
759 * @nodep: returns node
760 * @slotp: returns slot
761 *
762 * Lookup and return the item at position @index in the radix
763 * tree @root.
764 *
765 * Until there is more than one item in the tree, no nodes are
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500766 * allocated and @root->xa_head is used as a direct slot instead of
Johannes Weiner139e5612014-04-03 14:47:54 -0700767 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800768 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500769void *__radix_tree_lookup(const struct radix_tree_root *root,
770 unsigned long index, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500771 void __rcu ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800772{
Johannes Weiner139e5612014-04-03 14:47:54 -0700773 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700774 unsigned long maxindex;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500775 void __rcu **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800776
Matthew Wilcox85829952016-05-20 17:02:20 -0700777 restart:
778 parent = NULL;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500779 slot = (void __rcu **)&root->xa_head;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700780 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700781 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800782 return NULL;
783
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700784 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700785 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700786
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700787 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700788 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700789 slot = parent->slots + offset;
Matthew Wilcoxeff38602018-12-06 08:19:13 -0500790 if (node == RADIX_TREE_RETRY)
791 goto restart;
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400792 if (parent->shift == 0)
793 break;
Matthew Wilcox85829952016-05-20 17:02:20 -0700794 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800795
Johannes Weiner139e5612014-04-03 14:47:54 -0700796 if (nodep)
797 *nodep = parent;
798 if (slotp)
799 *slotp = slot;
800 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700801}
802
803/**
804 * radix_tree_lookup_slot - lookup a slot in a radix tree
805 * @root: radix tree root
806 * @index: index key
807 *
808 * Returns: the slot corresponding to the position @index in the
809 * radix tree @root. This is useful for update-if-exists operations.
810 *
811 * This function can be called under rcu_read_lock iff the slot is not
812 * modified by radix_tree_replace_slot, otherwise it must be called
813 * exclusive from other writers. Any dereference of the slot must be done
814 * using radix_tree_deref_slot.
815 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500816void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcox35534c82016-12-19 17:43:19 -0500817 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -0700818{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500819 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700820
821 if (!__radix_tree_lookup(root, index, NULL, &slot))
822 return NULL;
823 return slot;
Hans Reisera4331362005-11-07 00:59:29 -0800824}
825EXPORT_SYMBOL(radix_tree_lookup_slot);
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/**
828 * radix_tree_lookup - perform lookup operation on a radix tree
829 * @root: radix tree root
830 * @index: index key
831 *
832 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800833 *
834 * This function can be called under rcu_read_lock, however the caller
835 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
836 * them safely). No RCU barriers are required to access or modify the
837 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500839void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Johannes Weiner139e5612014-04-03 14:47:54 -0700841 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843EXPORT_SYMBOL(radix_tree_lookup);
844
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500845static void replace_slot(void __rcu **slot, void *item,
Matthew Wilcox01959df2017-11-09 09:23:56 -0500846 struct radix_tree_node *node, int count, int values)
Johannes Weiner6d75f362016-12-12 16:43:43 -0800847{
Matthew Wilcox01959df2017-11-09 09:23:56 -0500848 if (node && (count || values)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800849 node->count += count;
Matthew Wilcox01959df2017-11-09 09:23:56 -0500850 node->nr_values += values;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800851 }
Johannes Weiner6d75f362016-12-12 16:43:43 -0800852
853 rcu_assign_pointer(*slot, item);
854}
855
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500856static bool node_tag_get(const struct radix_tree_root *root,
857 const struct radix_tree_node *node,
858 unsigned int tag, unsigned int offset)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800859{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500860 if (node)
861 return tag_get(node, tag, offset);
862 return root_tag_get(root, tag);
863}
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800864
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500865/*
866 * IDR users want to be able to store NULL in the tree, so if the slot isn't
867 * free, don't adjust the count, even if it's transitioning between NULL and
868 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
869 * have empty bits, but it only stores NULL in slots when they're being
870 * deleted.
871 */
872static int calculate_count(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500873 struct radix_tree_node *node, void __rcu **slot,
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500874 void *item, void *old)
875{
876 if (is_idr(root)) {
877 unsigned offset = get_slot_offset(node, slot);
878 bool free = node_tag_get(root, node, IDR_FREE, offset);
879 if (!free)
880 return 0;
881 if (!old)
882 return 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800883 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500884 return !!item - !!old;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800885}
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887/**
Johannes Weinerf7942432016-12-12 16:43:41 -0800888 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -0800889 * @root: radix tree root
890 * @node: pointer to tree node
891 * @slot: pointer to slot in @node
892 * @item: new item to store in the slot.
Johannes Weinerf7942432016-12-12 16:43:41 -0800893 *
894 * For use with __radix_tree_lookup(). Caller must hold tree write locked
895 * across slot lookup and replacement.
896 */
897void __radix_tree_replace(struct radix_tree_root *root,
898 struct radix_tree_node *node,
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400899 void __rcu **slot, void *item)
Johannes Weinerf7942432016-12-12 16:43:41 -0800900{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500901 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox01959df2017-11-09 09:23:56 -0500902 int values = !!xa_is_value(item) - !!xa_is_value(old);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500903 int count = calculate_count(root, node, slot, item, old);
904
Johannes Weiner6d75f362016-12-12 16:43:43 -0800905 /*
Matthew Wilcox01959df2017-11-09 09:23:56 -0500906 * This function supports replacing value entries and
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800907 * deleting entries, but that needs accounting against the
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500908 * node unless the slot is root->xa_head.
Johannes Weiner6d75f362016-12-12 16:43:43 -0800909 */
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -0500910 WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->xa_head) &&
Matthew Wilcox01959df2017-11-09 09:23:56 -0500911 (count || values));
912 replace_slot(slot, item, node, count, values);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800913
Johannes Weiner4d693d02016-12-12 16:43:49 -0800914 if (!node)
915 return;
916
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400917 delete_node(root, node);
Johannes Weiner6d75f362016-12-12 16:43:43 -0800918}
Johannes Weinerf7942432016-12-12 16:43:41 -0800919
Johannes Weiner6d75f362016-12-12 16:43:43 -0800920/**
921 * radix_tree_replace_slot - replace item in a slot
922 * @root: radix tree root
923 * @slot: pointer to slot
924 * @item: new item to store in the slot.
925 *
Matthew Wilcox7b8d0462017-12-01 22:13:06 -0500926 * For use with radix_tree_lookup_slot() and
Johannes Weiner6d75f362016-12-12 16:43:43 -0800927 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
928 * across slot lookup and replacement.
929 *
930 * NOTE: This cannot be used to switch between non-entries (empty slots),
Matthew Wilcox01959df2017-11-09 09:23:56 -0500931 * regular entries, and value entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800932 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800933 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
934 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -0800935 */
936void radix_tree_replace_slot(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500937 void __rcu **slot, void *item)
Johannes Weiner6d75f362016-12-12 16:43:43 -0800938{
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400939 __radix_tree_replace(root, NULL, slot, item);
Johannes Weinerf7942432016-12-12 16:43:41 -0800940}
Song Liu10257d72017-01-11 10:00:51 -0800941EXPORT_SYMBOL(radix_tree_replace_slot);
Johannes Weinerf7942432016-12-12 16:43:41 -0800942
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800943/**
944 * radix_tree_iter_replace - replace item in a slot
945 * @root: radix tree root
946 * @slot: pointer to slot
947 * @item: new item to store in the slot.
948 *
Matthew Wilcox2956c662018-05-19 16:47:47 -0400949 * For use with radix_tree_for_each_slot().
950 * Caller must hold tree write locked.
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800951 */
952void radix_tree_iter_replace(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500953 const struct radix_tree_iter *iter,
954 void __rcu **slot, void *item)
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800955{
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400956 __radix_tree_replace(root, iter->node, slot, item);
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800957}
958
Matthew Wilcox30b888b2017-01-28 09:55:20 -0500959static void node_tag_set(struct radix_tree_root *root,
960 struct radix_tree_node *node,
961 unsigned int tag, unsigned int offset)
962{
963 while (node) {
964 if (tag_get(node, tag, offset))
965 return;
966 tag_set(node, tag, offset);
967 offset = node->offset;
968 node = node->parent;
969 }
970
971 if (!root_tag_get(root, tag))
972 root_tag_set(root, tag);
973}
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975/**
976 * radix_tree_tag_set - set a tag on a radix tree node
977 * @root: radix tree root
978 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700979 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800981 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
982 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * the root all the way down to the leaf node.
984 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700985 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * item is a bug.
987 */
988void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800989 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Ross Zwislerfb969902016-05-20 17:02:32 -0700991 struct radix_tree_node *node, *parent;
992 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700994 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -0700995 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700997 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -0700998 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001000 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001001 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001002 BUG_ON(!node);
1003
1004 if (!tag_get(parent, tag, offset))
1005 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 }
1007
Nick Piggin612d6c12006-06-23 02:03:22 -07001008 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001009 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001010 root_tag_set(root, tag);
1011
Ross Zwislerfb969902016-05-20 17:02:32 -07001012 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014EXPORT_SYMBOL(radix_tree_tag_set);
1015
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001016static void node_tag_clear(struct radix_tree_root *root,
1017 struct radix_tree_node *node,
1018 unsigned int tag, unsigned int offset)
1019{
1020 while (node) {
1021 if (!tag_get(node, tag, offset))
1022 return;
1023 tag_clear(node, tag, offset);
1024 if (any_tag_set(node, tag))
1025 return;
1026
1027 offset = node->offset;
1028 node = node->parent;
1029 }
1030
1031 /* clear the root's tag bit */
1032 if (root_tag_get(root, tag))
1033 root_tag_clear(root, tag);
1034}
1035
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001036/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 * radix_tree_tag_clear - clear a tag on a radix tree node
1038 * @root: radix tree root
1039 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001040 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001042 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001043 * corresponding to @index in the radix tree. If this causes
1044 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * next-to-leaf node, etc.
1046 *
1047 * Returns the address of the tagged item on success, else NULL. ie:
1048 * has the same return value and semantics as radix_tree_lookup().
1049 */
1050void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001051 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001053 struct radix_tree_node *node, *parent;
1054 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001055 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001057 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001058 if (index > maxindex)
1059 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Ross Zwisler00f47b52016-05-20 17:02:35 -07001061 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001063 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001064 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001065 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
1067
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001068 if (node)
1069 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Ross Zwisler00f47b52016-05-20 17:02:35 -07001071 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073EXPORT_SYMBOL(radix_tree_tag_clear);
1074
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001076 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1077 * @root: radix tree root
1078 * @iter: iterator state
1079 * @tag: tag to clear
1080 */
1081void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1082 const struct radix_tree_iter *iter, unsigned int tag)
1083{
1084 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1085}
1086
1087/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001088 * radix_tree_tag_get - get a tag on a radix tree node
1089 * @root: radix tree root
1090 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001091 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001093 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001095 * 0: tag not present or not set
1096 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001097 *
1098 * Note that the return value of this function may not be relied on, even if
1099 * the RCU lock is held, unless tag modification and node deletion are excluded
1100 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001102int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001103 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001105 struct radix_tree_node *node, *parent;
1106 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Nick Piggin612d6c12006-06-23 02:03:22 -07001108 if (!root_tag_get(root, tag))
1109 return 0;
1110
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001111 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001112 if (index > maxindex)
1113 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001114
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001115 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001116 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001117
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001118 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001119 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001120
Ross Zwisler4589ba62016-05-20 17:02:38 -07001121 if (!tag_get(parent, tag, offset))
1122 return 0;
1123 if (node == RADIX_TREE_RETRY)
1124 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001126
1127 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Matthew Wilcox148deab2016-12-14 15:08:49 -08001131/* Construct iter->tags bit-mask from node->tags[tag] array */
1132static void set_iter_tags(struct radix_tree_iter *iter,
1133 struct radix_tree_node *node, unsigned offset,
1134 unsigned tag)
1135{
1136 unsigned tag_long = offset / BITS_PER_LONG;
1137 unsigned tag_bit = offset % BITS_PER_LONG;
1138
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001139 if (!node) {
1140 iter->tags = 1;
1141 return;
1142 }
1143
Matthew Wilcox148deab2016-12-14 15:08:49 -08001144 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1145
1146 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1147 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1148 /* Pick tags from next element */
1149 if (tag_bit)
1150 iter->tags |= node->tags[tag][tag_long + 1] <<
1151 (BITS_PER_LONG - tag_bit);
1152 /* Clip chunk size, here only BITS_PER_LONG tags */
1153 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1154 }
1155}
1156
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001157void __rcu **radix_tree_iter_resume(void __rcu **slot,
1158 struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001159{
Matthew Wilcox148deab2016-12-14 15:08:49 -08001160 slot++;
1161 iter->index = __radix_tree_iter_add(iter, 1);
Matthew Wilcox148deab2016-12-14 15:08:49 -08001162 iter->next_index = iter->index;
1163 iter->tags = 0;
1164 return NULL;
1165}
1166EXPORT_SYMBOL(radix_tree_iter_resume);
1167
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001168/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001169 * radix_tree_next_chunk - find next chunk of slots for iteration
1170 *
1171 * @root: radix tree root
1172 * @iter: iterator state
1173 * @flags: RADIX_TREE_ITER_* flags and tag index
1174 * Returns: pointer to chunk first slot, or NULL if iteration is over
1175 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001176void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001177 struct radix_tree_iter *iter, unsigned flags)
1178{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001179 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001180 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001181 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001182
1183 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1184 return NULL;
1185
1186 /*
1187 * Catch next_index overflow after ~0UL. iter->index never overflows
1188 * during iterating; it can be zero only at the beginning.
1189 * And we cannot overflow iter->next_index in a single step,
1190 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001191 *
1192 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001193 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001194 */
1195 index = iter->next_index;
1196 if (!index && iter->index)
1197 return NULL;
1198
Ross Zwisler21ef5332016-05-20 17:02:26 -07001199 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001200 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001201 if (index > maxindex)
1202 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001203 if (!child)
1204 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001205
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001206 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001207 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001208 iter->index = index;
1209 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001210 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001211 iter->node = NULL;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001212 return (void __rcu **)&root->xa_head;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001213 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001214
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001215 do {
1216 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001217 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001218
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001219 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001220 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001221 /* Hole detected */
1222 if (flags & RADIX_TREE_ITER_CONTIG)
1223 return NULL;
1224
1225 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001226 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001227 offset + 1);
1228 else
1229 while (++offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001230 void *slot = rcu_dereference_raw(
1231 node->slots[offset]);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001232 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001233 break;
1234 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001235 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001236 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001237 /* Overflow after ~0UL */
1238 if (!index)
1239 return NULL;
1240 if (offset == RADIX_TREE_MAP_SIZE)
1241 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001242 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001243 }
1244
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001245 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001246 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001247 if (child == RADIX_TREE_RETRY)
1248 break;
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001249 } while (node->shift && radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001250
1251 /* Update the iterator state */
Matthew Wilcox3a08cd52018-09-22 16:14:30 -04001252 iter->index = (index &~ node_maxindex(node)) | offset;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001253 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001254 iter->node = node;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001255
Matthew Wilcox148deab2016-12-14 15:08:49 -08001256 if (flags & RADIX_TREE_ITER_TAGGED)
1257 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001258
1259 return node->slots + offset;
1260}
1261EXPORT_SYMBOL(radix_tree_next_chunk);
1262
1263/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1265 * @root: radix tree root
1266 * @results: where the results of the lookup are placed
1267 * @first_index: start the lookup from this key
1268 * @max_items: place up to this many items at *results
1269 *
1270 * Performs an index-ascending scan of the tree for present items. Places
1271 * them at *@results and returns the number of items which were placed at
1272 * *@results.
1273 *
1274 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001275 *
1276 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1277 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001278 * an atomic snapshot of the tree at a single point in time, the
1279 * semantics of an RCU protected gang lookup are as though multiple
1280 * radix_tree_lookups have been issued in individual locks, and results
1281 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
1283unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001284radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 unsigned long first_index, unsigned int max_items)
1286{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001287 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001288 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001289 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001291 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001292 return 0;
1293
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001294 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001295 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001296 if (!results[ret])
1297 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001298 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001299 slot = radix_tree_iter_retry(&iter);
1300 continue;
1301 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001302 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return ret;
1307}
1308EXPORT_SYMBOL(radix_tree_gang_lookup);
1309
Nick Piggin47feff22008-07-25 19:45:29 -07001310/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1312 * based on a tag
1313 * @root: radix tree root
1314 * @results: where the results of the lookup are placed
1315 * @first_index: start the lookup from this key
1316 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001317 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 *
1319 * Performs an index-ascending scan of the tree for present items which
1320 * have the tag indexed by @tag set. Places the items at *@results and
1321 * returns the number of items which were placed at *@results.
1322 */
1323unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001324radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001325 unsigned long first_index, unsigned int max_items,
1326 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001328 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001329 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001330 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001332 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001333 return 0;
1334
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001335 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001336 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001337 if (!results[ret])
1338 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001339 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001340 slot = radix_tree_iter_retry(&iter);
1341 continue;
1342 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001343 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return ret;
1348}
1349EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1350
1351/**
Nick Piggin47feff22008-07-25 19:45:29 -07001352 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1353 * radix tree based on a tag
1354 * @root: radix tree root
1355 * @results: where the results of the lookup are placed
1356 * @first_index: start the lookup from this key
1357 * @max_items: place up to this many items at *results
1358 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1359 *
1360 * Performs an index-ascending scan of the tree for present items which
1361 * have the tag indexed by @tag set. Places the slots at *@results and
1362 * returns the number of slots which were placed at *@results.
1363 */
1364unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001365radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001366 void __rcu ***results, unsigned long first_index,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001367 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001368{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001369 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001370 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001371 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001372
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001373 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001374 return 0;
1375
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001376 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1377 results[ret] = slot;
1378 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001379 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001380 }
1381
1382 return ret;
1383}
1384EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1385
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001386static bool __radix_tree_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001387 struct radix_tree_node *node, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001388{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001389 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox01959df2017-11-09 09:23:56 -05001390 int values = xa_is_value(old) ? -1 : 0;
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001391 unsigned offset = get_slot_offset(node, slot);
1392 int tag;
1393
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001394 if (is_idr(root))
1395 node_tag_set(root, node, IDR_FREE, offset);
1396 else
1397 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1398 node_tag_clear(root, node, tag, offset);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001399
Matthew Wilcox01959df2017-11-09 09:23:56 -05001400 replace_slot(slot, NULL, node, -1, values);
Matthew Wilcox1cf56f92018-04-09 16:24:45 -04001401 return node && delete_node(root, node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001402}
1403
Johannes Weiner139e5612014-04-03 14:47:54 -07001404/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001405 * radix_tree_iter_delete - delete the entry at this iterator position
1406 * @root: radix tree root
1407 * @iter: iterator state
1408 * @slot: pointer to slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001410 * Delete the entry at the position currently pointed to by the iterator.
1411 * This may result in the current node being freed; if it is, the iterator
1412 * is advanced so that it will not reference the freed memory. This
1413 * function may be called without any locking if there are no other threads
1414 * which can access this tree.
1415 */
1416void radix_tree_iter_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001417 struct radix_tree_iter *iter, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001418{
1419 if (__radix_tree_delete(root, iter->node, slot))
1420 iter->index = iter->next_index;
1421}
Chris Wilsond1b48c12017-08-16 09:52:08 +01001422EXPORT_SYMBOL(radix_tree_iter_delete);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001423
1424/**
1425 * radix_tree_delete_item - delete an item from a radix tree
1426 * @root: radix tree root
1427 * @index: index key
1428 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001430 * Remove @item at @index from the radix tree rooted at @root.
1431 *
1432 * Return: the deleted entry, or %NULL if it was not present
1433 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001435void *radix_tree_delete_item(struct radix_tree_root *root,
1436 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001438 struct radix_tree_node *node = NULL;
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07001439 void __rcu **slot = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001440 void *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Johannes Weiner139e5612014-04-03 14:47:54 -07001442 entry = __radix_tree_lookup(root, index, &node, &slot);
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07001443 if (!slot)
1444 return NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001445 if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE,
1446 get_slot_offset(node, slot))))
Johannes Weiner139e5612014-04-03 14:47:54 -07001447 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Johannes Weiner139e5612014-04-03 14:47:54 -07001449 if (item && entry != item)
1450 return NULL;
1451
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001452 __radix_tree_delete(root, node, slot);
Christoph Lameter201b6262005-09-06 15:16:46 -07001453
Johannes Weiner139e5612014-04-03 14:47:54 -07001454 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001456EXPORT_SYMBOL(radix_tree_delete_item);
1457
1458/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001459 * radix_tree_delete - delete an entry from a radix tree
1460 * @root: radix tree root
1461 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001462 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001463 * Remove the entry at @index from the radix tree rooted at @root.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001464 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001465 * Return: The deleted entry, or %NULL if it was not present.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001466 */
1467void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1468{
1469 return radix_tree_delete_item(root, index, NULL);
1470}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471EXPORT_SYMBOL(radix_tree_delete);
1472
1473/**
1474 * radix_tree_tagged - test whether any items in the tree are tagged
1475 * @root: radix tree root
1476 * @tag: tag to test
1477 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001478int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Nick Piggin612d6c12006-06-23 02:03:22 -07001480 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482EXPORT_SYMBOL(radix_tree_tagged);
1483
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001484/**
1485 * idr_preload - preload for idr_alloc()
1486 * @gfp_mask: allocation mask to use for preloading
1487 *
1488 * Preallocate memory to use for the next call to idr_alloc(). This function
1489 * returns with preemption disabled. It will be enabled by idr_preload_end().
1490 */
1491void idr_preload(gfp_t gfp_mask)
1492{
Eric Dumazetbc9ae222017-09-08 16:15:54 -07001493 if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE))
1494 preempt_disable();
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001495}
1496EXPORT_SYMBOL(idr_preload);
1497
Matthew Wilcox460488c2017-11-28 15:16:24 -05001498void __rcu **idr_get_free(struct radix_tree_root *root,
Chris Mi388f79f2017-08-30 02:31:57 -04001499 struct radix_tree_iter *iter, gfp_t gfp,
1500 unsigned long max)
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001501{
1502 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001503 void __rcu **slot = (void __rcu **)&root->xa_head;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001504 unsigned long maxindex, start = iter->next_index;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001505 unsigned int shift, offset = 0;
1506
1507 grow:
1508 shift = radix_tree_load_root(root, &child, &maxindex);
1509 if (!radix_tree_tagged(root, IDR_FREE))
1510 start = max(start, maxindex + 1);
1511 if (start > max)
1512 return ERR_PTR(-ENOSPC);
1513
1514 if (start > maxindex) {
1515 int error = radix_tree_extend(root, gfp, start, shift);
1516 if (error < 0)
1517 return ERR_PTR(error);
1518 shift = error;
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001519 child = rcu_dereference_raw(root->xa_head);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001520 }
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001521 if (start == 0 && shift == 0)
1522 shift = RADIX_TREE_MAP_SHIFT;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001523
1524 while (shift) {
1525 shift -= RADIX_TREE_MAP_SHIFT;
1526 if (child == NULL) {
1527 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05001528 child = radix_tree_node_alloc(gfp, node, root, shift,
1529 offset, 0, 0);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001530 if (!child)
1531 return ERR_PTR(-ENOMEM);
1532 all_tag_set(child, IDR_FREE);
1533 rcu_assign_pointer(*slot, node_to_entry(child));
1534 if (node)
1535 node->count++;
1536 } else if (!radix_tree_is_internal_node(child))
1537 break;
1538
1539 node = entry_to_node(child);
1540 offset = radix_tree_descend(node, &child, start);
1541 if (!tag_get(node, IDR_FREE, offset)) {
1542 offset = radix_tree_find_next_bit(node, IDR_FREE,
1543 offset + 1);
1544 start = next_index(start, node, offset);
1545 if (start > max)
1546 return ERR_PTR(-ENOSPC);
1547 while (offset == RADIX_TREE_MAP_SIZE) {
1548 offset = node->offset + 1;
1549 node = node->parent;
1550 if (!node)
1551 goto grow;
1552 shift = node->shift;
1553 }
1554 child = rcu_dereference_raw(node->slots[offset]);
1555 }
1556 slot = &node->slots[offset];
1557 }
1558
1559 iter->index = start;
1560 if (node)
1561 iter->next_index = 1 + min(max, (start | node_maxindex(node)));
1562 else
1563 iter->next_index = 1;
1564 iter->node = node;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001565 set_iter_tags(iter, node, offset, IDR_FREE);
1566
1567 return slot;
1568}
1569
1570/**
1571 * idr_destroy - release all internal memory from an IDR
1572 * @idr: idr handle
1573 *
1574 * After this function is called, the IDR is empty, and may be reused or
1575 * the data structure containing it may be freed.
1576 *
1577 * A typical clean-up sequence for objects stored in an idr tree will use
1578 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
1579 * free the memory used to keep track of those objects.
1580 */
1581void idr_destroy(struct idr *idr)
1582{
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001583 struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.xa_head);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001584 if (radix_tree_is_internal_node(node))
1585 radix_tree_free_nodes(node);
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -05001586 idr->idr_rt.xa_head = NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001587 root_tag_set(&idr->idr_rt, IDR_FREE);
1588}
1589EXPORT_SYMBOL(idr_destroy);
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591static void
Johannes Weiner449dd692014-04-03 14:47:56 -07001592radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
Johannes Weiner449dd692014-04-03 14:47:56 -07001594 struct radix_tree_node *node = arg;
1595
1596 memset(node, 0, sizeof(*node));
1597 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01001600static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001602 struct radix_tree_preload *rtp;
1603 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001605 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01001606 rtp = &per_cpu(radix_tree_preloads, cpu);
1607 while (rtp->nr) {
1608 node = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -05001609 rtp->nodes = node->parent;
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01001610 kmem_cache_free(radix_tree_node_cachep, node);
1611 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001612 }
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01001613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616void __init radix_tree_init(void)
1617{
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01001618 int ret;
Michal Hocko7e784422017-05-03 14:53:09 -07001619
1620 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32);
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -07001621 BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK);
Matthew Wilcox02c02bf2017-11-03 23:09:45 -04001622 BUILD_BUG_ON(XA_CHUNK_SIZE > 255);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
1624 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07001625 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
1626 radix_tree_node_ctor);
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01001627 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
1628 NULL, radix_tree_cpu_dead);
1629 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}