blob: b6c0e7f3a8949d236c0b7deb9fb7fe758684fdf9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -070043/* Number of nodes in fully populated tree of given height */
44static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
45
Jeff Moyer26fb1582007-10-16 01:24:49 -070046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * Radix tree node cache.
48 */
Christoph Lametere18b8902006-12-06 20:33:20 -080049static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
Nick Piggin55368052012-05-29 15:07:34 -070052 * The radix tree is variable-height, so an insert operation not only has
53 * to build the branch to its corresponding item, it also has to build the
54 * branch to existing items if the size has to be increased (by
55 * radix_tree_extend).
56 *
57 * The worst case is a zero height tree with just a single item at index 0,
58 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
59 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
60 * Hence:
61 */
62#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
63
64/*
Matthew Wilcox0a835c42016-12-20 10:27:56 -050065 * The IDR does not have to be as high as the radix tree since it uses
66 * signed integers, not unsigned longs.
67 */
68#define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1)
69#define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \
70 RADIX_TREE_MAP_SHIFT))
71#define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1)
72
73/*
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -050074 * The IDA is even shorter since it uses a bitmap at the last level.
75 */
76#define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS))
77#define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \
78 RADIX_TREE_MAP_SHIFT))
79#define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1)
80
81/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 * Per-cpu pool of preloaded nodes
83 */
84struct radix_tree_preload {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -070085 unsigned nr;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -050086 /* nodes->parent points to next preallocated node */
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -070087 struct radix_tree_node *nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080089static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Matthew Wilcox148deab2016-12-14 15:08:49 -080091static inline struct radix_tree_node *entry_to_node(void *ptr)
92{
93 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
94}
95
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070096static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080097{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070098 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -080099}
100
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -0700101#define RADIX_TREE_RETRY node_to_entry(NULL)
Matthew Wilcoxafe0e392016-05-20 17:02:17 -0700102
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700103#ifdef CONFIG_RADIX_TREE_MULTIORDER
104/* Sibling slots point directly to another slot in the same node */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500105static inline
106bool is_sibling_entry(const struct radix_tree_node *parent, void *node)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700107{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500108 void __rcu **ptr = node;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700109 return (parent->slots <= ptr) &&
110 (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
111}
112#else
Matthew Wilcox35534c82016-12-19 17:43:19 -0500113static inline
114bool is_sibling_entry(const struct radix_tree_node *parent, void *node)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700115{
116 return false;
117}
118#endif
119
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500120static inline unsigned long
121get_slot_offset(const struct radix_tree_node *parent, void __rcu **slot)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700122{
Matthew Wilcox76f070b2018-08-18 07:05:50 -0400123 return parent ? slot - parent->slots : 0;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700124}
125
Matthew Wilcox35534c82016-12-19 17:43:19 -0500126static unsigned int radix_tree_descend(const struct radix_tree_node *parent,
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700127 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700128{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700129 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500130 void __rcu **entry = rcu_dereference_raw(parent->slots[offset]);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700131
132#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700133 if (radix_tree_is_internal_node(entry)) {
Linus Torvalds8d2c0d32016-09-25 13:32:46 -0700134 if (is_sibling_entry(parent, entry)) {
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500135 void __rcu **sibentry;
136 sibentry = (void __rcu **) entry_to_node(entry);
Linus Torvalds8d2c0d32016-09-25 13:32:46 -0700137 offset = get_slot_offset(parent, sibentry);
138 entry = rcu_dereference_raw(*sibentry);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700139 }
140 }
141#endif
142
143 *nodep = (void *)entry;
144 return offset;
145}
146
Matthew Wilcox35534c82016-12-19 17:43:19 -0500147static inline gfp_t root_gfp_mask(const struct radix_tree_root *root)
Nick Piggin612d6c12006-06-23 02:03:22 -0700148{
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -0700149 return root->gfp_mask & (__GFP_BITS_MASK & ~GFP_ZONEMASK);
Nick Piggin612d6c12006-06-23 02:03:22 -0700150}
151
Nick Piggin643b52b2008-06-12 15:21:52 -0700152static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
153 int offset)
154{
155 __set_bit(offset, node->tags[tag]);
156}
157
158static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
159 int offset)
160{
161 __clear_bit(offset, node->tags[tag]);
162}
163
Matthew Wilcox35534c82016-12-19 17:43:19 -0500164static inline int tag_get(const struct radix_tree_node *node, unsigned int tag,
Nick Piggin643b52b2008-06-12 15:21:52 -0700165 int offset)
166{
167 return test_bit(offset, node->tags[tag]);
168}
169
Matthew Wilcox35534c82016-12-19 17:43:19 -0500170static inline void root_tag_set(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700171{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500172 root->gfp_mask |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700173}
174
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700175static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700176{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500177 root->gfp_mask &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700178}
179
180static inline void root_tag_clear_all(struct radix_tree_root *root)
181{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500182 root->gfp_mask &= (1 << ROOT_TAG_SHIFT) - 1;
Nick Piggin643b52b2008-06-12 15:21:52 -0700183}
184
Matthew Wilcox35534c82016-12-19 17:43:19 -0500185static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700186{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500187 return (__force int)root->gfp_mask & (1 << (tag + ROOT_TAG_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700188}
189
Matthew Wilcox35534c82016-12-19 17:43:19 -0500190static inline unsigned root_tags_get(const struct radix_tree_root *root)
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700191{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500192 return (__force unsigned)root->gfp_mask >> ROOT_TAG_SHIFT;
193}
194
195static inline bool is_idr(const struct radix_tree_root *root)
196{
197 return !!(root->gfp_mask & ROOT_IS_IDR);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700198}
199
Nick Piggin643b52b2008-06-12 15:21:52 -0700200/*
201 * Returns 1 if any slot in the node has this tag set.
202 * Otherwise returns 0.
203 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500204static inline int any_tag_set(const struct radix_tree_node *node,
205 unsigned int tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700206{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700207 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700208 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
209 if (node->tags[tag][idx])
210 return 1;
211 }
212 return 0;
213}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700214
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500215static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag)
216{
217 bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE);
218}
219
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700220/**
221 * radix_tree_find_next_bit - find the next set bit in a memory region
222 *
223 * @addr: The address to base the search on
224 * @size: The bitmap size in bits
225 * @offset: The bitnumber to start searching at
226 *
227 * Unrollable variant of find_next_bit() for constant size arrays.
228 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
229 * Returns next bit offset, or size if nothing found.
230 */
231static __always_inline unsigned long
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800232radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
233 unsigned long offset)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700234{
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800235 const unsigned long *addr = node->tags[tag];
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700236
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800237 if (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700238 unsigned long tmp;
239
240 addr += offset / BITS_PER_LONG;
241 tmp = *addr >> (offset % BITS_PER_LONG);
242 if (tmp)
243 return __ffs(tmp) + offset;
244 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800245 while (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700246 tmp = *++addr;
247 if (tmp)
248 return __ffs(tmp) + offset;
249 offset += BITS_PER_LONG;
250 }
251 }
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800252 return RADIX_TREE_MAP_SIZE;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700253}
254
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800255static unsigned int iter_offset(const struct radix_tree_iter *iter)
256{
257 return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
258}
259
Matthew Wilcox218ed752016-12-14 15:08:43 -0800260/*
261 * The maximum index which can be stored in a radix tree
262 */
263static inline unsigned long shift_maxindex(unsigned int shift)
264{
265 return (RADIX_TREE_MAP_SIZE << shift) - 1;
266}
267
Matthew Wilcox35534c82016-12-19 17:43:19 -0500268static inline unsigned long node_maxindex(const struct radix_tree_node *node)
Matthew Wilcox218ed752016-12-14 15:08:43 -0800269{
270 return shift_maxindex(node->shift);
271}
272
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500273static unsigned long next_index(unsigned long index,
274 const struct radix_tree_node *node,
275 unsigned long offset)
276{
277 return (index & ~node_maxindex(node)) + (offset << node->shift);
278}
279
Ross Zwisler0796c582016-05-20 17:02:55 -0700280#ifndef __KERNEL__
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700281static void dump_node(struct radix_tree_node *node, unsigned long index)
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700282{
Ross Zwisler0796c582016-05-20 17:02:55 -0700283 unsigned long i;
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700284
Matthew Wilcox218ed752016-12-14 15:08:43 -0800285 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
286 node, node->offset, index, index | node_maxindex(node),
287 node->parent,
Ross Zwisler0796c582016-05-20 17:02:55 -0700288 node->tags[0][0], node->tags[1][0], node->tags[2][0],
Matthew Wilcox218ed752016-12-14 15:08:43 -0800289 node->shift, node->count, node->exceptional);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700290
Ross Zwisler0796c582016-05-20 17:02:55 -0700291 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700292 unsigned long first = index | (i << node->shift);
293 unsigned long last = first | ((1UL << node->shift) - 1);
Ross Zwisler0796c582016-05-20 17:02:55 -0700294 void *entry = node->slots[i];
295 if (!entry)
296 continue;
Matthew Wilcox218ed752016-12-14 15:08:43 -0800297 if (entry == RADIX_TREE_RETRY) {
298 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
299 i, first, last, node);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700300 } else if (!radix_tree_is_internal_node(entry)) {
Matthew Wilcox218ed752016-12-14 15:08:43 -0800301 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
302 entry, i, first, last, node);
303 } else if (is_sibling_entry(node, entry)) {
304 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
305 entry, i, first, last, node,
306 *(void **)entry_to_node(entry));
Ross Zwisler0796c582016-05-20 17:02:55 -0700307 } else {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700308 dump_node(entry_to_node(entry), first);
Ross Zwisler0796c582016-05-20 17:02:55 -0700309 }
310 }
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700311}
312
313/* For debug */
314static void radix_tree_dump(struct radix_tree_root *root)
315{
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700316 pr_debug("radix root: %p rnode %p tags %x\n",
317 root, root->rnode,
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500318 root->gfp_mask >> ROOT_TAG_SHIFT);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700319 if (!radix_tree_is_internal_node(root->rnode))
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700320 return;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700321 dump_node(entry_to_node(root->rnode), 0);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700322}
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500323
324static void dump_ida_node(void *entry, unsigned long index)
325{
326 unsigned long i;
327
328 if (!entry)
329 return;
330
331 if (radix_tree_is_internal_node(entry)) {
332 struct radix_tree_node *node = entry_to_node(entry);
333
334 pr_debug("ida node: %p offset %d indices %lu-%lu parent %p free %lx shift %d count %d\n",
335 node, node->offset, index * IDA_BITMAP_BITS,
336 ((index | node_maxindex(node)) + 1) *
337 IDA_BITMAP_BITS - 1,
338 node->parent, node->tags[0][0], node->shift,
339 node->count);
340 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++)
341 dump_ida_node(node->slots[i],
342 index | (i << node->shift));
Matthew Wilcox3159f942017-11-03 13:30:42 -0400343 } else if (xa_is_value(entry)) {
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500344 pr_debug("ida excp: %p offset %d indices %lu-%lu data %lx\n",
345 entry, (int)(index & RADIX_TREE_MAP_MASK),
346 index * IDA_BITMAP_BITS,
Matthew Wilcox3159f942017-11-03 13:30:42 -0400347 index * IDA_BITMAP_BITS + BITS_PER_XA_VALUE,
348 xa_to_value(entry));
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500349 } else {
350 struct ida_bitmap *bitmap = entry;
351
352 pr_debug("ida btmp: %p offset %d indices %lu-%lu data", bitmap,
353 (int)(index & RADIX_TREE_MAP_MASK),
354 index * IDA_BITMAP_BITS,
355 (index + 1) * IDA_BITMAP_BITS - 1);
356 for (i = 0; i < IDA_BITMAP_LONGS; i++)
357 pr_cont(" %lx", bitmap->bitmap[i]);
358 pr_cont("\n");
359 }
360}
361
362static void ida_dump(struct ida *ida)
363{
364 struct radix_tree_root *root = &ida->ida_rt;
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500365 pr_debug("ida: %p node %p free %d\n", ida, root->rnode,
366 root->gfp_mask >> ROOT_TAG_SHIFT);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500367 dump_ida_node(root->rnode, 0);
368}
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700369#endif
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371/*
372 * This assumes that the caller has performed appropriate preallocation, and
373 * that the caller has pinned this thread of control to the current CPU.
374 */
375static struct radix_tree_node *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500376radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500377 struct radix_tree_root *root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800378 unsigned int shift, unsigned int offset,
379 unsigned int count, unsigned int exceptional)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Nick Piggine2848a02008-02-04 22:29:10 -0800381 struct radix_tree_node *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Jan Kara5e4c0d972013-09-11 14:26:05 -0700383 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700384 * Preload code isn't irq safe and it doesn't make sense to use
385 * preloading during an interrupt anyway as all the allocations have
386 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700387 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800388 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct radix_tree_preload *rtp;
390
Nick Piggine2848a02008-02-04 22:29:10 -0800391 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700392 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700393 * cache first for the new node to get accounted to the memory
394 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700395 */
396 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700397 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700398 if (ret)
399 goto out;
400
401 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800402 * Provided the caller has preloaded here, we will always
403 * succeed in getting a node here (and never reach
404 * kmem_cache_alloc)
405 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700406 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700408 ret = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500409 rtp->nodes = ret->parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rtp->nr--;
411 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700412 /*
413 * Update the allocation stack trace as this is more useful
414 * for debugging.
415 */
416 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700417 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700419 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700420out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700421 BUG_ON(radix_tree_is_internal_node(ret));
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800422 if (ret) {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800423 ret->shift = shift;
424 ret->offset = offset;
425 ret->count = count;
426 ret->exceptional = exceptional;
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500427 ret->parent = parent;
428 ret->root = root;
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return ret;
431}
432
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800433static void radix_tree_node_rcu_free(struct rcu_head *head)
434{
435 struct radix_tree_node *node =
436 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700437
438 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800439 * Must only free zeroed nodes into the slab. We can be left with
440 * non-NULL entries by radix_tree_free_nodes, so clear the entries
441 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700442 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800443 memset(node->slots, 0, sizeof(node->slots));
444 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800445 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700446
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800447 kmem_cache_free(radix_tree_node_cachep, node);
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static inline void
451radix_tree_node_free(struct radix_tree_node *node)
452{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800453 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456/*
457 * Load up this CPU's radix_tree_node buffer with sufficient objects to
458 * ensure that the addition of a single element in the tree cannot fail. On
459 * success, return zero, with preemption disabled. On error, return -ENOMEM
460 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000461 *
462 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800463 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -0700465static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct radix_tree_preload *rtp;
468 struct radix_tree_node *node;
469 int ret = -ENOMEM;
470
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700471 /*
472 * Nodes preloaded by one cgroup can be be used by another cgroup, so
473 * they should never be accounted to any particular memory cgroup.
474 */
475 gfp_mask &= ~__GFP_ACCOUNT;
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700478 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700479 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700481 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (node == NULL)
483 goto out;
484 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700485 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700486 if (rtp->nr < nr) {
Matthew Wilcox1293d5c2017-01-16 16:41:29 -0500487 node->parent = rtp->nodes;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700488 rtp->nodes = node;
489 rtp->nr++;
490 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 ret = 0;
495out:
496 return ret;
497}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700498
499/*
500 * Load up this CPU's radix_tree_node buffer with sufficient objects to
501 * ensure that the addition of a single element in the tree cannot fail. On
502 * success, return zero, with preemption disabled. On error, return -ENOMEM
503 * with preemption not disabled.
504 *
505 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800506 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700507 */
508int radix_tree_preload(gfp_t gfp_mask)
509{
510 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800511 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700512 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700513}
David Chinnerd7f09232007-07-14 16:05:04 +1000514EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Nick Piggin6e954b92006-01-08 01:01:40 -0800516/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700517 * The same as above function, except we don't guarantee preloading happens.
518 * We do it, if we decide it helps. On success, return zero with preemption
519 * disabled. On error, return -ENOMEM with preemption not disabled.
520 */
521int radix_tree_maybe_preload(gfp_t gfp_mask)
522{
Mel Gormand0164ad2015-11-06 16:28:21 -0800523 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700524 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700525 /* Preloading doesn't help anything with this gfp mask, skip it */
526 preempt_disable();
527 return 0;
528}
529EXPORT_SYMBOL(radix_tree_maybe_preload);
530
Matthew Wilcox2791653a2016-12-14 15:09:04 -0800531#ifdef CONFIG_RADIX_TREE_MULTIORDER
532/*
533 * Preload with enough objects to ensure that we can split a single entry
534 * of order @old_order into many entries of size @new_order
535 */
536int radix_tree_split_preload(unsigned int old_order, unsigned int new_order,
537 gfp_t gfp_mask)
538{
539 unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT);
540 unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) -
541 (new_order / RADIX_TREE_MAP_SHIFT);
542 unsigned nr = 0;
543
544 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
545 BUG_ON(new_order >= old_order);
546
547 while (layers--)
548 nr = nr * RADIX_TREE_MAP_SIZE + 1;
549 return __radix_tree_preload(gfp_mask, top * nr);
550}
551#endif
552
Jan Kara5e4c0d972013-09-11 14:26:05 -0700553/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700554 * The same as function above, but preload number of nodes required to insert
555 * (1 << order) continuous naturally-aligned elements.
556 */
557int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
558{
559 unsigned long nr_subtrees;
560 int nr_nodes, subtree_height;
561
562 /* Preloading doesn't help anything with this gfp mask, skip it */
563 if (!gfpflags_allow_blocking(gfp_mask)) {
564 preempt_disable();
565 return 0;
566 }
567
568 /*
569 * Calculate number and height of fully populated subtrees it takes to
570 * store (1 << order) elements.
571 */
572 nr_subtrees = 1 << order;
573 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
574 subtree_height++)
575 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
576
577 /*
578 * The worst case is zero height tree with a single item at index 0 and
579 * then inserting items starting at ULONG_MAX - (1 << order).
580 *
581 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
582 * 0-index item.
583 */
584 nr_nodes = RADIX_TREE_MAX_PATH;
585
586 /* Plus branch to fully populated subtrees. */
587 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
588
589 /* Root node is shared. */
590 nr_nodes--;
591
592 /* Plus nodes required to build subtrees. */
593 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
594
595 return __radix_tree_preload(gfp_mask, nr_nodes);
596}
597
Matthew Wilcox35534c82016-12-19 17:43:19 -0500598static unsigned radix_tree_load_root(const struct radix_tree_root *root,
Matthew Wilcox1456a432016-05-20 17:02:08 -0700599 struct radix_tree_node **nodep, unsigned long *maxindex)
600{
601 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
602
603 *nodep = node;
604
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700605 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700606 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700607 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700608 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700609 }
610
611 *maxindex = 0;
612 return 0;
613}
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/*
616 * Extend a radix tree so it can store key @index.
617 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500618static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700619 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500621 void *entry;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700622 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int tag;
624
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700625 /* Figure out what the shift should be. */
626 maxshift = shift;
627 while (index > shift_maxindex(maxshift))
628 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500630 entry = rcu_dereference_raw(root->rnode);
631 if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 do {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500635 struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL,
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500636 root, shift, 0, 1, 0);
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700637 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return -ENOMEM;
639
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500640 if (is_idr(root)) {
641 all_tag_set(node, IDR_FREE);
642 if (!root_tag_get(root, IDR_FREE)) {
643 tag_clear(node, IDR_FREE, 0);
644 root_tag_set(root, IDR_FREE);
645 }
646 } else {
647 /* Propagate the aggregated tag info to the new child */
648 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
649 if (root_tag_get(root, tag))
650 tag_set(node, tag, 0);
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700654 BUG_ON(shift > BITS_PER_LONG);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500655 if (radix_tree_is_internal_node(entry)) {
656 entry_to_node(entry)->parent = node;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400657 } else if (xa_is_value(entry)) {
Johannes Weinerf7942432016-12-12 16:43:41 -0800658 /* Moving an exceptional root->rnode to a node */
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800659 node->exceptional = 1;
Johannes Weinerf7942432016-12-12 16:43:41 -0800660 }
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500661 /*
662 * entry was already in the radix tree, so we do not need
663 * rcu_assign_pointer here
664 */
665 node->slots[0] = (void __rcu *)entry;
666 entry = node_to_entry(node);
667 rcu_assign_pointer(root->rnode, entry);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700668 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700669 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700671 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800675 * radix_tree_shrink - shrink radix tree to minimum height
676 * @root radix tree root
677 */
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500678static inline bool radix_tree_shrink(struct radix_tree_root *root,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800679 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800680{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500681 bool shrunk = false;
682
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800683 for (;;) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500684 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800685 struct radix_tree_node *child;
686
687 if (!radix_tree_is_internal_node(node))
688 break;
689 node = entry_to_node(node);
690
691 /*
692 * The candidate node has more than one child, or its child
693 * is not at the leftmost slot, or the child is a multiorder
694 * entry, we cannot shrink.
695 */
696 if (node->count != 1)
697 break;
Matthew Wilcox12320d02017-02-13 15:22:48 -0500698 child = rcu_dereference_raw(node->slots[0]);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800699 if (!child)
700 break;
701 if (!radix_tree_is_internal_node(child) && node->shift)
702 break;
703
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400704 /*
705 * For an IDR, we must not shrink entry 0 into the root in
706 * case somebody calls idr_replace() with a pointer that
707 * appears to be an internal entry
708 */
709 if (!node->shift && is_idr(root))
710 break;
711
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800712 if (radix_tree_is_internal_node(child))
713 entry_to_node(child)->parent = NULL;
714
715 /*
716 * We don't need rcu_assign_pointer(), since we are simply
717 * moving the node from one part of the tree to another: if it
718 * was safe to dereference the old pointer to it
719 * (node->slots[0]), it will be safe to dereference the new
720 * one (root->rnode) as far as dependent read barriers go.
721 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500722 root->rnode = (void __rcu *)child;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500723 if (is_idr(root) && !tag_get(node, IDR_FREE, 0))
724 root_tag_clear(root, IDR_FREE);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800725
726 /*
727 * We have a dilemma here. The node's slot[0] must not be
728 * NULLed in case there are concurrent lookups expecting to
729 * find the item. However if this was a bottom-level node,
730 * then it may be subject to the slot pointer being visible
731 * to callers dereferencing it. If item corresponding to
732 * slot[0] is subsequently deleted, these callers would expect
733 * their slot to become empty sooner or later.
734 *
735 * For example, lockless pagecache will look up a slot, deref
736 * the page pointer, and if the page has 0 refcount it means it
737 * was concurrently deleted from pagecache so try the deref
738 * again. Fortunately there is already a requirement for logic
739 * to retry the entire slot lookup -- the indirect pointer
740 * problem (replacing direct root node with an indirect pointer
741 * also results in a stale slot). So tag the slot as indirect
742 * to force callers to retry.
743 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800744 node->count = 0;
745 if (!radix_tree_is_internal_node(child)) {
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500746 node->slots[0] = (void __rcu *)RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800747 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800748 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800749 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800750
Johannes Weinerea07b862017-01-06 19:21:43 -0500751 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800752 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500753 shrunk = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800754 }
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500755
756 return shrunk;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800757}
758
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500759static bool delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800760 struct radix_tree_node *node,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800761 radix_tree_update_node_t update_node)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800762{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500763 bool deleted = false;
764
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800765 do {
766 struct radix_tree_node *parent;
767
768 if (node->count) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500769 if (node_to_entry(node) ==
770 rcu_dereference_raw(root->rnode))
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800771 deleted |= radix_tree_shrink(root,
772 update_node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500773 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800774 }
775
776 parent = node->parent;
777 if (parent) {
778 parent->slots[node->offset] = NULL;
779 parent->count--;
780 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500781 /*
782 * Shouldn't the tags already have all been cleared
783 * by the caller?
784 */
785 if (!is_idr(root))
786 root_tag_clear_all(root);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800787 root->rnode = NULL;
788 }
789
Johannes Weinerea07b862017-01-06 19:21:43 -0500790 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800791 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500792 deleted = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800793
794 node = parent;
795 } while (node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500796
797 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800798}
799
800/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700801 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 * @root: radix tree root
803 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700804 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700805 * @nodep: returns node
806 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700808 * Create, if necessary, and return the node and slot for an item
809 * at position @index in the radix tree @root.
810 *
811 * Until there is more than one item in the tree, no nodes are
812 * allocated and @root->rnode is used as a direct slot instead of
813 * pointing to a node, in which case *@nodep will be NULL.
814 *
815 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700817int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700818 unsigned order, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500819 void __rcu ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700821 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500822 void __rcu **slot = (void __rcu **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700823 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700824 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700825 unsigned long max = index | ((1UL << order) - 1);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500826 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700827
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700828 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800831 if (order > 0 && max == ((1UL << order) - 1))
832 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700833 if (max > maxindex) {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500834 int error = radix_tree_extend(root, gfp, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700835 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700837 shift = error;
Matthew Wilcox12320d02017-02-13 15:22:48 -0500838 child = rcu_dereference_raw(root->rnode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700841 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700842 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700843 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -0500845 child = radix_tree_node_alloc(gfp, node, root, shift,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800846 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700847 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700849 rcu_assign_pointer(*slot, node_to_entry(child));
850 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700852 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700853 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700856 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700857 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700858 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700859 }
860
Johannes Weiner139e5612014-04-03 14:47:54 -0700861 if (nodep)
862 *nodep = node;
863 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700864 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700865 return 0;
866}
867
Matthew Wilcox175542f2016-12-14 15:08:58 -0800868/*
869 * Free any nodes below this node. The tree is presumed to not need
870 * shrinking, and any user data in the tree is presumed to not need a
871 * destructor called on it. If we need to add a destructor, we can
872 * add that functionality later. Note that we may not clear tags or
873 * slots from the tree as an RCU walker may still have a pointer into
874 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
875 * but we'll still have to clear those in rcu_free.
876 */
877static void radix_tree_free_nodes(struct radix_tree_node *node)
878{
879 unsigned offset = 0;
880 struct radix_tree_node *child = entry_to_node(node);
881
882 for (;;) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500883 void *entry = rcu_dereference_raw(child->slots[offset]);
Matthew Wilcox66ee6202018-06-25 06:56:50 -0400884 if (radix_tree_is_internal_node(entry) && child->shift &&
885 !is_sibling_entry(child, entry)) {
Matthew Wilcox175542f2016-12-14 15:08:58 -0800886 child = entry_to_node(entry);
887 offset = 0;
888 continue;
889 }
890 offset++;
891 while (offset == RADIX_TREE_MAP_SIZE) {
892 struct radix_tree_node *old = child;
893 offset = child->offset + 1;
894 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800895 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800896 radix_tree_node_free(old);
897 if (old == entry_to_node(node))
898 return;
899 }
900 }
901}
902
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500903#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500904static inline int insert_entries(struct radix_tree_node *node,
905 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800906{
907 struct radix_tree_node *child;
908 unsigned i, n, tag, offset, tags = 0;
909
910 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800911 if (order > node->shift)
912 n = 1 << (order - node->shift);
913 else
914 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800915 offset = get_slot_offset(node, slot);
916 } else {
917 n = 1;
918 offset = 0;
919 }
920
921 if (n > 1) {
922 offset = offset & ~(n - 1);
923 slot = &node->slots[offset];
924 }
925 child = node_to_entry(slot);
926
927 for (i = 0; i < n; i++) {
928 if (slot[i]) {
929 if (replace) {
930 node->count--;
931 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
932 if (tag_get(node, tag, offset + i))
933 tags |= 1 << tag;
934 } else
935 return -EEXIST;
936 }
937 }
938
939 for (i = 0; i < n; i++) {
Matthew Wilcox12320d02017-02-13 15:22:48 -0500940 struct radix_tree_node *old = rcu_dereference_raw(slot[i]);
Matthew Wilcox175542f2016-12-14 15:08:58 -0800941 if (i) {
942 rcu_assign_pointer(slot[i], child);
943 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
944 if (tags & (1 << tag))
945 tag_clear(node, tag, offset + i);
946 } else {
947 rcu_assign_pointer(slot[i], item);
948 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
949 if (tags & (1 << tag))
950 tag_set(node, tag, offset);
951 }
952 if (radix_tree_is_internal_node(old) &&
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800953 !is_sibling_entry(node, old) &&
954 (old != RADIX_TREE_RETRY))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800955 radix_tree_free_nodes(old);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400956 if (xa_is_value(old))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800957 node->exceptional--;
958 }
959 if (node) {
960 node->count += n;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400961 if (xa_is_value(item))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800962 node->exceptional += n;
963 }
964 return n;
965}
966#else
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500967static inline int insert_entries(struct radix_tree_node *node,
968 void __rcu **slot, void *item, unsigned order, bool replace)
Matthew Wilcox175542f2016-12-14 15:08:58 -0800969{
970 if (*slot)
971 return -EEXIST;
972 rcu_assign_pointer(*slot, item);
973 if (node) {
974 node->count++;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400975 if (xa_is_value(item))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800976 node->exceptional++;
977 }
978 return 1;
979}
980#endif
981
Johannes Weiner139e5612014-04-03 14:47:54 -0700982/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700983 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700984 * @root: radix tree root
985 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700986 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700987 * @item: item to insert
988 *
989 * Insert an item into the radix tree at position @index.
990 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700991int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
992 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700993{
994 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -0500995 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700996 int error;
997
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700998 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700999
Matthew Wilcoxe6145232016-03-17 14:21:54 -07001000 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -07001001 if (error)
1002 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -08001003
1004 error = insert_entries(node, slot, item, order, false);
1005 if (error < 0)
1006 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -07001007
Nick Piggin612d6c12006-06-23 02:03:22 -07001008 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -07001009 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -07001010 BUG_ON(tag_get(node, 0, offset));
1011 BUG_ON(tag_get(node, 1, offset));
1012 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -07001013 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -07001014 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -07001015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 return 0;
1018}
Matthew Wilcoxe6145232016-03-17 14:21:54 -07001019EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Johannes Weiner139e5612014-04-03 14:47:54 -07001021/**
1022 * __radix_tree_lookup - lookup an item in a radix tree
1023 * @root: radix tree root
1024 * @index: index key
1025 * @nodep: returns node
1026 * @slotp: returns slot
1027 *
1028 * Lookup and return the item at position @index in the radix
1029 * tree @root.
1030 *
1031 * Until there is more than one item in the tree, no nodes are
1032 * allocated and @root->rnode is used as a direct slot instead of
1033 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -08001034 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001035void *__radix_tree_lookup(const struct radix_tree_root *root,
1036 unsigned long index, struct radix_tree_node **nodep,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001037 void __rcu ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -08001038{
Johannes Weiner139e5612014-04-03 14:47:54 -07001039 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -07001040 unsigned long maxindex;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001041 void __rcu **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001042
Matthew Wilcox85829952016-05-20 17:02:20 -07001043 restart:
1044 parent = NULL;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001045 slot = (void __rcu **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001046 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -07001047 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001048 return NULL;
1049
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001050 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -07001051 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -07001052
Matthew Wilcox85829952016-05-20 17:02:20 -07001053 if (node == RADIX_TREE_RETRY)
1054 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001055 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001056 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -07001057 slot = parent->slots + offset;
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001058 if (parent->shift == 0)
1059 break;
Matthew Wilcox85829952016-05-20 17:02:20 -07001060 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001061
Johannes Weiner139e5612014-04-03 14:47:54 -07001062 if (nodep)
1063 *nodep = parent;
1064 if (slotp)
1065 *slotp = slot;
1066 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -07001067}
1068
1069/**
1070 * radix_tree_lookup_slot - lookup a slot in a radix tree
1071 * @root: radix tree root
1072 * @index: index key
1073 *
1074 * Returns: the slot corresponding to the position @index in the
1075 * radix tree @root. This is useful for update-if-exists operations.
1076 *
1077 * This function can be called under rcu_read_lock iff the slot is not
1078 * modified by radix_tree_replace_slot, otherwise it must be called
1079 * exclusive from other writers. Any dereference of the slot must be done
1080 * using radix_tree_deref_slot.
1081 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001082void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001083 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -07001084{
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001085 void __rcu **slot;
Johannes Weiner139e5612014-04-03 14:47:54 -07001086
1087 if (!__radix_tree_lookup(root, index, NULL, &slot))
1088 return NULL;
1089 return slot;
Hans Reisera4331362005-11-07 00:59:29 -08001090}
1091EXPORT_SYMBOL(radix_tree_lookup_slot);
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093/**
1094 * radix_tree_lookup - perform lookup operation on a radix tree
1095 * @root: radix tree root
1096 * @index: index key
1097 *
1098 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001099 *
1100 * This function can be called under rcu_read_lock, however the caller
1101 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
1102 * them safely). No RCU barriers are required to access or modify the
1103 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001105void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Johannes Weiner139e5612014-04-03 14:47:54 -07001107 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109EXPORT_SYMBOL(radix_tree_lookup);
1110
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001111static inline void replace_sibling_entries(struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001112 void __rcu **slot, int count, int exceptional)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001113{
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001114#ifdef CONFIG_RADIX_TREE_MULTIORDER
1115 void *ptr = node_to_entry(slot);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001116 unsigned offset = get_slot_offset(node, slot) + 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001117
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001118 while (offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001119 if (rcu_dereference_raw(node->slots[offset]) != ptr)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001120 break;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001121 if (count < 0) {
1122 node->slots[offset] = NULL;
1123 node->count--;
1124 }
1125 node->exceptional += exceptional;
1126 offset++;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001127 }
1128#endif
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001129}
1130
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001131static void replace_slot(void __rcu **slot, void *item,
1132 struct radix_tree_node *node, int count, int exceptional)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001133{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001134 if (node && (count || exceptional)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001135 node->count += count;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001136 node->exceptional += exceptional;
1137 replace_sibling_entries(node, slot, count, exceptional);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001138 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001139
1140 rcu_assign_pointer(*slot, item);
1141}
1142
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001143static bool node_tag_get(const struct radix_tree_root *root,
1144 const struct radix_tree_node *node,
1145 unsigned int tag, unsigned int offset)
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001146{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001147 if (node)
1148 return tag_get(node, tag, offset);
1149 return root_tag_get(root, tag);
1150}
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001151
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001152/*
1153 * IDR users want to be able to store NULL in the tree, so if the slot isn't
1154 * free, don't adjust the count, even if it's transitioning between NULL and
1155 * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still
1156 * have empty bits, but it only stores NULL in slots when they're being
1157 * deleted.
1158 */
1159static int calculate_count(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001160 struct radix_tree_node *node, void __rcu **slot,
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001161 void *item, void *old)
1162{
1163 if (is_idr(root)) {
1164 unsigned offset = get_slot_offset(node, slot);
1165 bool free = node_tag_get(root, node, IDR_FREE, offset);
1166 if (!free)
1167 return 0;
1168 if (!old)
1169 return 1;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001170 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001171 return !!item - !!old;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001172}
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174/**
Johannes Weinerf7942432016-12-12 16:43:41 -08001175 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -08001176 * @root: radix tree root
1177 * @node: pointer to tree node
1178 * @slot: pointer to slot in @node
1179 * @item: new item to store in the slot.
1180 * @update_node: callback for changing leaf nodes
Johannes Weinerf7942432016-12-12 16:43:41 -08001181 *
1182 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1183 * across slot lookup and replacement.
1184 */
1185void __radix_tree_replace(struct radix_tree_root *root,
1186 struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001187 void __rcu **slot, void *item,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001188 radix_tree_update_node_t update_node)
Johannes Weinerf7942432016-12-12 16:43:41 -08001189{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001190 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001191 int exceptional = !!xa_is_value(item) - !!xa_is_value(old);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001192 int count = calculate_count(root, node, slot, item, old);
1193
Johannes Weiner6d75f362016-12-12 16:43:43 -08001194 /*
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001195 * This function supports replacing exceptional entries and
1196 * deleting entries, but that needs accounting against the
1197 * node unless the slot is root->rnode.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001198 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001199 WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->rnode) &&
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001200 (count || exceptional));
1201 replace_slot(slot, item, node, count, exceptional);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001202
Johannes Weiner4d693d02016-12-12 16:43:49 -08001203 if (!node)
1204 return;
1205
1206 if (update_node)
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001207 update_node(node);
Johannes Weiner4d693d02016-12-12 16:43:49 -08001208
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001209 delete_node(root, node, update_node);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001210}
Johannes Weinerf7942432016-12-12 16:43:41 -08001211
Johannes Weiner6d75f362016-12-12 16:43:43 -08001212/**
1213 * radix_tree_replace_slot - replace item in a slot
1214 * @root: radix tree root
1215 * @slot: pointer to slot
1216 * @item: new item to store in the slot.
1217 *
1218 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1219 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1220 * across slot lookup and replacement.
1221 *
1222 * NOTE: This cannot be used to switch between non-entries (empty slots),
1223 * regular entries, and exceptional entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001224 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001225 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1226 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001227 */
1228void radix_tree_replace_slot(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001229 void __rcu **slot, void *item)
Johannes Weiner6d75f362016-12-12 16:43:43 -08001230{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001231 __radix_tree_replace(root, NULL, slot, item, NULL);
Johannes Weinerf7942432016-12-12 16:43:41 -08001232}
Song Liu10257d72017-01-11 10:00:51 -08001233EXPORT_SYMBOL(radix_tree_replace_slot);
Johannes Weinerf7942432016-12-12 16:43:41 -08001234
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001235/**
1236 * radix_tree_iter_replace - replace item in a slot
1237 * @root: radix tree root
1238 * @slot: pointer to slot
1239 * @item: new item to store in the slot.
1240 *
1241 * For use with radix_tree_split() and radix_tree_for_each_slot().
1242 * Caller must hold tree write locked across split and replacement.
1243 */
1244void radix_tree_iter_replace(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001245 const struct radix_tree_iter *iter,
1246 void __rcu **slot, void *item)
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001247{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001248 __radix_tree_replace(root, iter->node, slot, item, NULL);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001249}
1250
Matthew Wilcox175542f2016-12-14 15:08:58 -08001251#ifdef CONFIG_RADIX_TREE_MULTIORDER
1252/**
1253 * radix_tree_join - replace multiple entries with one multiorder entry
1254 * @root: radix tree root
1255 * @index: an index inside the new entry
1256 * @order: order of the new entry
1257 * @item: new entry
1258 *
1259 * Call this function to replace several entries with one larger entry.
1260 * The existing entries are presumed to not need freeing as a result of
1261 * this call.
1262 *
1263 * The replacement entry will have all the tags set on it that were set
1264 * on any of the entries it is replacing.
1265 */
1266int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1267 unsigned order, void *item)
1268{
1269 struct radix_tree_node *node;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001270 void __rcu **slot;
Matthew Wilcox175542f2016-12-14 15:08:58 -08001271 int error;
1272
1273 BUG_ON(radix_tree_is_internal_node(item));
1274
1275 error = __radix_tree_create(root, index, order, &node, &slot);
1276 if (!error)
1277 error = insert_entries(node, slot, item, order, true);
1278 if (error > 0)
1279 error = 0;
1280
1281 return error;
1282}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001283
1284/**
1285 * radix_tree_split - Split an entry into smaller entries
1286 * @root: radix tree root
1287 * @index: An index within the large entry
1288 * @order: Order of new entries
1289 *
1290 * Call this function as the first step in replacing a multiorder entry
1291 * with several entries of lower order. After this function returns,
1292 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1293 * and call radix_tree_iter_replace() to set up each new entry.
1294 *
1295 * The tags from this entry are replicated to all the new entries.
1296 *
1297 * The radix tree should be locked against modification during the entire
1298 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1299 * should prompt RCU walkers to restart the lookup from the root.
1300 */
1301int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1302 unsigned order)
1303{
1304 struct radix_tree_node *parent, *node, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001305 void __rcu **slot;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001306 unsigned int offset, end;
1307 unsigned n, tag, tags = 0;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001308 gfp_t gfp = root_gfp_mask(root);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001309
1310 if (!__radix_tree_lookup(root, index, &parent, &slot))
1311 return -ENOENT;
1312 if (!parent)
1313 return -ENOENT;
1314
1315 offset = get_slot_offset(parent, slot);
1316
1317 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1318 if (tag_get(parent, tag, offset))
1319 tags |= 1 << tag;
1320
1321 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001322 if (!is_sibling_entry(parent,
1323 rcu_dereference_raw(parent->slots[end])))
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001324 break;
1325 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1326 if (tags & (1 << tag))
1327 tag_set(parent, tag, end);
1328 /* rcu_assign_pointer ensures tags are set before RETRY */
1329 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1330 }
1331 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
1332 parent->exceptional -= (end - offset);
1333
1334 if (order == parent->shift)
1335 return 0;
1336 if (order > parent->shift) {
1337 while (offset < end)
1338 offset += insert_entries(parent, &parent->slots[offset],
1339 RADIX_TREE_RETRY, order, true);
1340 return 0;
1341 }
1342
1343 node = parent;
1344
1345 for (;;) {
1346 if (node->shift > order) {
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05001347 child = radix_tree_node_alloc(gfp, node, root,
Matthew Wilcoxe8de4342016-12-14 15:09:31 -08001348 node->shift - RADIX_TREE_MAP_SHIFT,
1349 offset, 0, 0);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001350 if (!child)
1351 goto nomem;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001352 if (node != parent) {
1353 node->count++;
Matthew Wilcox12320d02017-02-13 15:22:48 -05001354 rcu_assign_pointer(node->slots[offset],
1355 node_to_entry(child));
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001356 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1357 if (tags & (1 << tag))
1358 tag_set(node, tag, offset);
1359 }
1360
1361 node = child;
1362 offset = 0;
1363 continue;
1364 }
1365
1366 n = insert_entries(node, &node->slots[offset],
1367 RADIX_TREE_RETRY, order, false);
1368 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1369
1370 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1371 if (tags & (1 << tag))
1372 tag_set(node, tag, offset);
1373 offset += n;
1374
1375 while (offset == RADIX_TREE_MAP_SIZE) {
1376 if (node == parent)
1377 break;
1378 offset = node->offset;
1379 child = node;
1380 node = node->parent;
1381 rcu_assign_pointer(node->slots[offset],
1382 node_to_entry(child));
1383 offset++;
1384 }
1385 if ((node == parent) && (offset == end))
1386 return 0;
1387 }
1388
1389 nomem:
1390 /* Shouldn't happen; did user forget to preload? */
1391 /* TODO: free all the allocated nodes */
1392 WARN_ON(1);
1393 return -ENOMEM;
1394}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001395#endif
1396
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001397static void node_tag_set(struct radix_tree_root *root,
1398 struct radix_tree_node *node,
1399 unsigned int tag, unsigned int offset)
1400{
1401 while (node) {
1402 if (tag_get(node, tag, offset))
1403 return;
1404 tag_set(node, tag, offset);
1405 offset = node->offset;
1406 node = node->parent;
1407 }
1408
1409 if (!root_tag_get(root, tag))
1410 root_tag_set(root, tag);
1411}
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413/**
1414 * radix_tree_tag_set - set a tag on a radix tree node
1415 * @root: radix tree root
1416 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001417 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001419 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1420 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * the root all the way down to the leaf node.
1422 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001423 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 * item is a bug.
1425 */
1426void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001427 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Ross Zwislerfb969902016-05-20 17:02:32 -07001429 struct radix_tree_node *node, *parent;
1430 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001432 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001433 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001435 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001436 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001438 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001439 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001440 BUG_ON(!node);
1441
1442 if (!tag_get(parent, tag, offset))
1443 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445
Nick Piggin612d6c12006-06-23 02:03:22 -07001446 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001447 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001448 root_tag_set(root, tag);
1449
Ross Zwislerfb969902016-05-20 17:02:32 -07001450 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452EXPORT_SYMBOL(radix_tree_tag_set);
1453
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001454/**
1455 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1456 * @root: radix tree root
1457 * @iter: iterator state
1458 * @tag: tag to set
1459 */
1460void radix_tree_iter_tag_set(struct radix_tree_root *root,
1461 const struct radix_tree_iter *iter, unsigned int tag)
1462{
1463 node_tag_set(root, iter->node, tag, iter_offset(iter));
1464}
1465
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001466static void node_tag_clear(struct radix_tree_root *root,
1467 struct radix_tree_node *node,
1468 unsigned int tag, unsigned int offset)
1469{
1470 while (node) {
1471 if (!tag_get(node, tag, offset))
1472 return;
1473 tag_clear(node, tag, offset);
1474 if (any_tag_set(node, tag))
1475 return;
1476
1477 offset = node->offset;
1478 node = node->parent;
1479 }
1480
1481 /* clear the root's tag bit */
1482 if (root_tag_get(root, tag))
1483 root_tag_clear(root, tag);
1484}
1485
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001486/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 * radix_tree_tag_clear - clear a tag on a radix tree node
1488 * @root: radix tree root
1489 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001490 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001492 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001493 * corresponding to @index in the radix tree. If this causes
1494 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 * next-to-leaf node, etc.
1496 *
1497 * Returns the address of the tagged item on success, else NULL. ie:
1498 * has the same return value and semantics as radix_tree_lookup().
1499 */
1500void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001501 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001503 struct radix_tree_node *node, *parent;
1504 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001505 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001507 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001508 if (index > maxindex)
1509 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Ross Zwisler00f47b52016-05-20 17:02:35 -07001511 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001513 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001514 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001515 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 }
1517
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001518 if (node)
1519 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Ross Zwisler00f47b52016-05-20 17:02:35 -07001521 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523EXPORT_SYMBOL(radix_tree_tag_clear);
1524
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001526 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1527 * @root: radix tree root
1528 * @iter: iterator state
1529 * @tag: tag to clear
1530 */
1531void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1532 const struct radix_tree_iter *iter, unsigned int tag)
1533{
1534 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1535}
1536
1537/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001538 * radix_tree_tag_get - get a tag on a radix tree node
1539 * @root: radix tree root
1540 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001541 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001543 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001545 * 0: tag not present or not set
1546 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001547 *
1548 * Note that the return value of this function may not be relied on, even if
1549 * the RCU lock is held, unless tag modification and node deletion are excluded
1550 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001552int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001553 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001555 struct radix_tree_node *node, *parent;
1556 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Nick Piggin612d6c12006-06-23 02:03:22 -07001558 if (!root_tag_get(root, tag))
1559 return 0;
1560
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001561 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001562 if (index > maxindex)
1563 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001564
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001565 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001566 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001567
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001568 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001569 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001570
Ross Zwisler4589ba62016-05-20 17:02:38 -07001571 if (!tag_get(parent, tag, offset))
1572 return 0;
1573 if (node == RADIX_TREE_RETRY)
1574 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001576
1577 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Ross Zwisler21ef5332016-05-20 17:02:26 -07001581static inline void __set_iter_shift(struct radix_tree_iter *iter,
1582 unsigned int shift)
1583{
1584#ifdef CONFIG_RADIX_TREE_MULTIORDER
1585 iter->shift = shift;
1586#endif
1587}
1588
Matthew Wilcox148deab2016-12-14 15:08:49 -08001589/* Construct iter->tags bit-mask from node->tags[tag] array */
1590static void set_iter_tags(struct radix_tree_iter *iter,
1591 struct radix_tree_node *node, unsigned offset,
1592 unsigned tag)
1593{
1594 unsigned tag_long = offset / BITS_PER_LONG;
1595 unsigned tag_bit = offset % BITS_PER_LONG;
1596
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001597 if (!node) {
1598 iter->tags = 1;
1599 return;
1600 }
1601
Matthew Wilcox148deab2016-12-14 15:08:49 -08001602 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1603
1604 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1605 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1606 /* Pick tags from next element */
1607 if (tag_bit)
1608 iter->tags |= node->tags[tag][tag_long + 1] <<
1609 (BITS_PER_LONG - tag_bit);
1610 /* Clip chunk size, here only BITS_PER_LONG tags */
1611 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1612 }
1613}
1614
1615#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001616static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1617 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001618{
Matthew Wilcox148deab2016-12-14 15:08:49 -08001619 while (iter->index < iter->next_index) {
1620 *nodep = rcu_dereference_raw(*slot);
Ross Zwisler9f418222018-05-18 16:09:06 -07001621 if (*nodep && !is_sibling_entry(iter->node, *nodep))
Matthew Wilcox148deab2016-12-14 15:08:49 -08001622 return slot;
1623 slot++;
1624 iter->index = __radix_tree_iter_add(iter, 1);
1625 iter->tags >>= 1;
1626 }
1627
1628 *nodep = NULL;
1629 return NULL;
1630}
1631
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001632void __rcu **__radix_tree_next_slot(void __rcu **slot,
1633 struct radix_tree_iter *iter, unsigned flags)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001634{
1635 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Ross Zwisler9f418222018-05-18 16:09:06 -07001636 struct radix_tree_node *node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001637
1638 slot = skip_siblings(&node, slot, iter);
1639
1640 while (radix_tree_is_internal_node(node)) {
1641 unsigned offset;
1642 unsigned long next_index;
1643
1644 if (node == RADIX_TREE_RETRY)
1645 return slot;
1646 node = entry_to_node(node);
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001647 iter->node = node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001648 iter->shift = node->shift;
1649
1650 if (flags & RADIX_TREE_ITER_TAGGED) {
1651 offset = radix_tree_find_next_bit(node, tag, 0);
1652 if (offset == RADIX_TREE_MAP_SIZE)
1653 return NULL;
1654 slot = &node->slots[offset];
1655 iter->index = __radix_tree_iter_add(iter, offset);
1656 set_iter_tags(iter, node, offset, tag);
1657 node = rcu_dereference_raw(*slot);
1658 } else {
1659 offset = 0;
1660 slot = &node->slots[0];
1661 for (;;) {
1662 node = rcu_dereference_raw(*slot);
1663 if (node)
1664 break;
1665 slot++;
1666 offset++;
1667 if (offset == RADIX_TREE_MAP_SIZE)
1668 return NULL;
1669 }
1670 iter->index = __radix_tree_iter_add(iter, offset);
1671 }
1672 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1673 goto none;
1674 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1675 if (next_index < iter->next_index)
1676 iter->next_index = next_index;
1677 }
1678
1679 return slot;
1680 none:
1681 iter->next_index = 0;
1682 return NULL;
1683}
1684EXPORT_SYMBOL(__radix_tree_next_slot);
1685#else
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001686static void __rcu **skip_siblings(struct radix_tree_node **nodep,
1687 void __rcu **slot, struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001688{
1689 return slot;
1690}
1691#endif
1692
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001693void __rcu **radix_tree_iter_resume(void __rcu **slot,
1694 struct radix_tree_iter *iter)
Matthew Wilcox148deab2016-12-14 15:08:49 -08001695{
1696 struct radix_tree_node *node;
1697
1698 slot++;
1699 iter->index = __radix_tree_iter_add(iter, 1);
Matthew Wilcox148deab2016-12-14 15:08:49 -08001700 skip_siblings(&node, slot, iter);
1701 iter->next_index = iter->index;
1702 iter->tags = 0;
1703 return NULL;
1704}
1705EXPORT_SYMBOL(radix_tree_iter_resume);
1706
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001707/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001708 * radix_tree_next_chunk - find next chunk of slots for iteration
1709 *
1710 * @root: radix tree root
1711 * @iter: iterator state
1712 * @flags: RADIX_TREE_ITER_* flags and tag index
1713 * Returns: pointer to chunk first slot, or NULL if iteration is over
1714 */
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001715void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001716 struct radix_tree_iter *iter, unsigned flags)
1717{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001718 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001719 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001720 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001721
1722 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1723 return NULL;
1724
1725 /*
1726 * Catch next_index overflow after ~0UL. iter->index never overflows
1727 * during iterating; it can be zero only at the beginning.
1728 * And we cannot overflow iter->next_index in a single step,
1729 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001730 *
1731 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001732 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001733 */
1734 index = iter->next_index;
1735 if (!index && iter->index)
1736 return NULL;
1737
Ross Zwisler21ef5332016-05-20 17:02:26 -07001738 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001739 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001740 if (index > maxindex)
1741 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001742 if (!child)
1743 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001744
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001745 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001746 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001747 iter->index = index;
1748 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001749 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001750 iter->node = NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001751 __set_iter_shift(iter, 0);
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001752 return (void __rcu **)&root->rnode;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001753 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001754
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001755 do {
1756 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001757 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001758
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001759 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001760 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001761 /* Hole detected */
1762 if (flags & RADIX_TREE_ITER_CONTIG)
1763 return NULL;
1764
1765 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001766 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001767 offset + 1);
1768 else
1769 while (++offset < RADIX_TREE_MAP_SIZE) {
Matthew Wilcox12320d02017-02-13 15:22:48 -05001770 void *slot = rcu_dereference_raw(
1771 node->slots[offset]);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001772 if (is_sibling_entry(node, slot))
1773 continue;
1774 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001775 break;
1776 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001777 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001778 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001779 /* Overflow after ~0UL */
1780 if (!index)
1781 return NULL;
1782 if (offset == RADIX_TREE_MAP_SIZE)
1783 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001784 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001785 }
1786
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001787 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001788 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001789 if (child == RADIX_TREE_RETRY)
1790 break;
Matthew Wilcox66ee6202018-06-25 06:56:50 -04001791 } while (node->shift && radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001792
1793 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001794 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1795 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001796 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001797 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001798
Matthew Wilcox148deab2016-12-14 15:08:49 -08001799 if (flags & RADIX_TREE_ITER_TAGGED)
1800 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001801
1802 return node->slots + offset;
1803}
1804EXPORT_SYMBOL(radix_tree_next_chunk);
1805
1806/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1808 * @root: radix tree root
1809 * @results: where the results of the lookup are placed
1810 * @first_index: start the lookup from this key
1811 * @max_items: place up to this many items at *results
1812 *
1813 * Performs an index-ascending scan of the tree for present items. Places
1814 * them at *@results and returns the number of items which were placed at
1815 * *@results.
1816 *
1817 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001818 *
1819 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1820 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001821 * an atomic snapshot of the tree at a single point in time, the
1822 * semantics of an RCU protected gang lookup are as though multiple
1823 * radix_tree_lookups have been issued in individual locks, and results
1824 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 */
1826unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001827radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 unsigned long first_index, unsigned int max_items)
1829{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001830 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001831 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001832 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001834 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001835 return 0;
1836
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001837 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001838 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001839 if (!results[ret])
1840 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001841 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001842 slot = radix_tree_iter_retry(&iter);
1843 continue;
1844 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001845 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001848
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 return ret;
1850}
1851EXPORT_SYMBOL(radix_tree_gang_lookup);
1852
Nick Piggin47feff22008-07-25 19:45:29 -07001853/**
1854 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1855 * @root: radix tree root
1856 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001857 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001858 * @first_index: start the lookup from this key
1859 * @max_items: place up to this many items at *results
1860 *
1861 * Performs an index-ascending scan of the tree for present items. Places
1862 * their slots at *@results and returns the number of items which were
1863 * placed at *@results.
1864 *
1865 * The implementation is naive.
1866 *
1867 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1868 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1869 * protection, radix_tree_deref_slot may fail requiring a retry.
1870 */
1871unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001872radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001873 void __rcu ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001874 unsigned long first_index, unsigned int max_items)
1875{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001876 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001877 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001878 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001879
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001880 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001881 return 0;
1882
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001883 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1884 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001885 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001886 indices[ret] = iter.index;
1887 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001888 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001889 }
1890
1891 return ret;
1892}
1893EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895/**
1896 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1897 * based on a tag
1898 * @root: radix tree root
1899 * @results: where the results of the lookup are placed
1900 * @first_index: start the lookup from this key
1901 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001902 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 *
1904 * Performs an index-ascending scan of the tree for present items which
1905 * have the tag indexed by @tag set. Places the items at *@results and
1906 * returns the number of items which were placed at *@results.
1907 */
1908unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001909radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001910 unsigned long first_index, unsigned int max_items,
1911 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001913 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001914 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001915 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001917 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001918 return 0;
1919
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001920 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001921 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001922 if (!results[ret])
1923 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001924 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001925 slot = radix_tree_iter_retry(&iter);
1926 continue;
1927 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001928 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return ret;
1933}
1934EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1935
1936/**
Nick Piggin47feff22008-07-25 19:45:29 -07001937 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1938 * radix tree based on a tag
1939 * @root: radix tree root
1940 * @results: where the results of the lookup are placed
1941 * @first_index: start the lookup from this key
1942 * @max_items: place up to this many items at *results
1943 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1944 *
1945 * Performs an index-ascending scan of the tree for present items which
1946 * have the tag indexed by @tag set. Places the slots at *@results and
1947 * returns the number of slots which were placed at *@results.
1948 */
1949unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001950radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001951 void __rcu ***results, unsigned long first_index,
Matthew Wilcox35534c82016-12-19 17:43:19 -05001952 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001953{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001954 struct radix_tree_iter iter;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001955 void __rcu **slot;
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001956 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001957
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001958 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001959 return 0;
1960
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001961 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1962 results[ret] = slot;
1963 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001964 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001965 }
1966
1967 return ret;
1968}
1969EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1970
Nick Piggin47feff22008-07-25 19:45:29 -07001971/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001972 * __radix_tree_delete_node - try to free node after clearing a slot
1973 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001974 * @node: node containing @index
Johannes Weinerea07b862017-01-06 19:21:43 -05001975 * @update_node: callback for changing leaf nodes
Johannes Weiner139e5612014-04-03 14:47:54 -07001976 *
1977 * After clearing the slot at @index in @node from radix tree
1978 * rooted at @root, call this function to attempt freeing the
1979 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001980 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001981void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weinerea07b862017-01-06 19:21:43 -05001982 struct radix_tree_node *node,
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001983 radix_tree_update_node_t update_node)
Johannes Weiner139e5612014-04-03 14:47:54 -07001984{
Mel Gormanc7df8ad2017-11-15 17:37:41 -08001985 delete_node(root, node, update_node);
Johannes Weiner139e5612014-04-03 14:47:54 -07001986}
1987
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001988static bool __radix_tree_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05001989 struct radix_tree_node *node, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001990{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001991 void *old = rcu_dereference_raw(*slot);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001992 int exceptional = xa_is_value(old) ? -1 : 0;
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001993 unsigned offset = get_slot_offset(node, slot);
1994 int tag;
1995
Matthew Wilcox0a835c42016-12-20 10:27:56 -05001996 if (is_idr(root))
1997 node_tag_set(root, node, IDR_FREE, offset);
1998 else
1999 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
2000 node_tag_clear(root, node, tag, offset);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002001
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002002 replace_slot(slot, NULL, node, -1, exceptional);
Mel Gormanc7df8ad2017-11-15 17:37:41 -08002003 return node && delete_node(root, node, NULL);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002004}
2005
Johannes Weiner139e5612014-04-03 14:47:54 -07002006/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002007 * radix_tree_iter_delete - delete the entry at this iterator position
2008 * @root: radix tree root
2009 * @iter: iterator state
2010 * @slot: pointer to slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002012 * Delete the entry at the position currently pointed to by the iterator.
2013 * This may result in the current node being freed; if it is, the iterator
2014 * is advanced so that it will not reference the freed memory. This
2015 * function may be called without any locking if there are no other threads
2016 * which can access this tree.
2017 */
2018void radix_tree_iter_delete(struct radix_tree_root *root,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05002019 struct radix_tree_iter *iter, void __rcu **slot)
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002020{
2021 if (__radix_tree_delete(root, iter->node, slot))
2022 iter->index = iter->next_index;
2023}
Chris Wilsond1b48c12017-08-16 09:52:08 +01002024EXPORT_SYMBOL(radix_tree_iter_delete);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002025
2026/**
2027 * radix_tree_delete_item - delete an item from a radix tree
2028 * @root: radix tree root
2029 * @index: index key
2030 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002032 * Remove @item at @index from the radix tree rooted at @root.
2033 *
2034 * Return: the deleted entry, or %NULL if it was not present
2035 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07002037void *radix_tree_delete_item(struct radix_tree_root *root,
2038 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002040 struct radix_tree_node *node = NULL;
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07002041 void __rcu **slot = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07002042 void *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Johannes Weiner139e5612014-04-03 14:47:54 -07002044 entry = __radix_tree_lookup(root, index, &node, &slot);
Matthew Wilcox7a4deea2018-05-25 14:47:24 -07002045 if (!slot)
2046 return NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002047 if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE,
2048 get_slot_offset(node, slot))))
Johannes Weiner139e5612014-04-03 14:47:54 -07002049 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Johannes Weiner139e5612014-04-03 14:47:54 -07002051 if (item && entry != item)
2052 return NULL;
2053
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002054 __radix_tree_delete(root, node, slot);
Christoph Lameter201b6262005-09-06 15:16:46 -07002055
Johannes Weiner139e5612014-04-03 14:47:54 -07002056 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057}
Johannes Weiner53c59f22014-04-03 14:47:39 -07002058EXPORT_SYMBOL(radix_tree_delete_item);
2059
2060/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002061 * radix_tree_delete - delete an entry from a radix tree
2062 * @root: radix tree root
2063 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07002064 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002065 * Remove the entry at @index from the radix tree rooted at @root.
Johannes Weiner53c59f22014-04-03 14:47:39 -07002066 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05002067 * Return: The deleted entry, or %NULL if it was not present.
Johannes Weiner53c59f22014-04-03 14:47:39 -07002068 */
2069void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
2070{
2071 return radix_tree_delete_item(root, index, NULL);
2072}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073EXPORT_SYMBOL(radix_tree_delete);
2074
Johannes Weinerd3798ae2016-10-04 22:02:08 +02002075void radix_tree_clear_tags(struct radix_tree_root *root,
2076 struct radix_tree_node *node,
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05002077 void __rcu **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002078{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002079 if (node) {
2080 unsigned int tag, offset = get_slot_offset(node, slot);
2081 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
2082 node_tag_clear(root, node, tag, offset);
2083 } else {
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002084 root_tag_clear_all(root);
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002085 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07002086}
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088/**
2089 * radix_tree_tagged - test whether any items in the tree are tagged
2090 * @root: radix tree root
2091 * @tag: tag to test
2092 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05002093int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094{
Nick Piggin612d6c12006-06-23 02:03:22 -07002095 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096}
2097EXPORT_SYMBOL(radix_tree_tagged);
2098
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002099/**
2100 * idr_preload - preload for idr_alloc()
2101 * @gfp_mask: allocation mask to use for preloading
2102 *
2103 * Preallocate memory to use for the next call to idr_alloc(). This function
2104 * returns with preemption disabled. It will be enabled by idr_preload_end().
2105 */
2106void idr_preload(gfp_t gfp_mask)
2107{
Eric Dumazetbc9ae222017-09-08 16:15:54 -07002108 if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE))
2109 preempt_disable();
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002110}
2111EXPORT_SYMBOL(idr_preload);
2112
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002113int ida_pre_get(struct ida *ida, gfp_t gfp)
2114{
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002115 /*
2116 * The IDA API has no preload_end() equivalent. Instead,
2117 * ida_get_new() can return -EAGAIN, prompting the caller
2118 * to return to the ida_pre_get() step.
2119 */
Eric Dumazetbc9ae222017-09-08 16:15:54 -07002120 if (!__radix_tree_preload(gfp, IDA_PRELOAD_SIZE))
2121 preempt_enable();
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002122
2123 if (!this_cpu_read(ida_bitmap)) {
Rasmus Villemoesb1a8a7a2018-02-21 14:45:43 -08002124 struct ida_bitmap *bitmap = kzalloc(sizeof(*bitmap), gfp);
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002125 if (!bitmap)
2126 return 0;
Matthew Wilcox4ecd9542017-03-03 12:16:10 -05002127 if (this_cpu_cmpxchg(ida_bitmap, NULL, bitmap))
2128 kfree(bitmap);
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002129 }
2130
2131 return 1;
2132}
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002133
Matthew Wilcox460488c2017-11-28 15:16:24 -05002134void __rcu **idr_get_free(struct radix_tree_root *root,
Chris Mi388f79f2017-08-30 02:31:57 -04002135 struct radix_tree_iter *iter, gfp_t gfp,
2136 unsigned long max)
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002137{
2138 struct radix_tree_node *node = NULL, *child;
Matthew Wilcoxd7b62722017-02-13 15:58:24 -05002139 void __rcu **slot = (void __rcu **)&root->rnode;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002140 unsigned long maxindex, start = iter->next_index;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002141 unsigned int shift, offset = 0;
2142
2143 grow:
2144 shift = radix_tree_load_root(root, &child, &maxindex);
2145 if (!radix_tree_tagged(root, IDR_FREE))
2146 start = max(start, maxindex + 1);
2147 if (start > max)
2148 return ERR_PTR(-ENOSPC);
2149
2150 if (start > maxindex) {
2151 int error = radix_tree_extend(root, gfp, start, shift);
2152 if (error < 0)
2153 return ERR_PTR(error);
2154 shift = error;
2155 child = rcu_dereference_raw(root->rnode);
2156 }
Matthew Wilcox66ee6202018-06-25 06:56:50 -04002157 if (start == 0 && shift == 0)
2158 shift = RADIX_TREE_MAP_SHIFT;
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002159
2160 while (shift) {
2161 shift -= RADIX_TREE_MAP_SHIFT;
2162 if (child == NULL) {
2163 /* Have to add a child node. */
Matthew Wilcoxd58275b2017-01-16 17:10:21 -05002164 child = radix_tree_node_alloc(gfp, node, root, shift,
2165 offset, 0, 0);
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002166 if (!child)
2167 return ERR_PTR(-ENOMEM);
2168 all_tag_set(child, IDR_FREE);
2169 rcu_assign_pointer(*slot, node_to_entry(child));
2170 if (node)
2171 node->count++;
2172 } else if (!radix_tree_is_internal_node(child))
2173 break;
2174
2175 node = entry_to_node(child);
2176 offset = radix_tree_descend(node, &child, start);
2177 if (!tag_get(node, IDR_FREE, offset)) {
2178 offset = radix_tree_find_next_bit(node, IDR_FREE,
2179 offset + 1);
2180 start = next_index(start, node, offset);
2181 if (start > max)
2182 return ERR_PTR(-ENOSPC);
2183 while (offset == RADIX_TREE_MAP_SIZE) {
2184 offset = node->offset + 1;
2185 node = node->parent;
2186 if (!node)
2187 goto grow;
2188 shift = node->shift;
2189 }
2190 child = rcu_dereference_raw(node->slots[offset]);
2191 }
2192 slot = &node->slots[offset];
2193 }
2194
2195 iter->index = start;
2196 if (node)
2197 iter->next_index = 1 + min(max, (start | node_maxindex(node)));
2198 else
2199 iter->next_index = 1;
2200 iter->node = node;
2201 __set_iter_shift(iter, shift);
2202 set_iter_tags(iter, node, offset, IDR_FREE);
2203
2204 return slot;
2205}
2206
2207/**
2208 * idr_destroy - release all internal memory from an IDR
2209 * @idr: idr handle
2210 *
2211 * After this function is called, the IDR is empty, and may be reused or
2212 * the data structure containing it may be freed.
2213 *
2214 * A typical clean-up sequence for objects stored in an idr tree will use
2215 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
2216 * free the memory used to keep track of those objects.
2217 */
2218void idr_destroy(struct idr *idr)
2219{
2220 struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.rnode);
2221 if (radix_tree_is_internal_node(node))
2222 radix_tree_free_nodes(node);
2223 idr->idr_rt.rnode = NULL;
2224 root_tag_set(&idr->idr_rt, IDR_FREE);
2225}
2226EXPORT_SYMBOL(idr_destroy);
2227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228static void
Johannes Weiner449dd692014-04-03 14:47:56 -07002229radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Johannes Weiner449dd692014-04-03 14:47:56 -07002231 struct radix_tree_node *node = arg;
2232
2233 memset(node, 0, sizeof(*node));
2234 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002237static __init unsigned long __maxindex(unsigned int height)
2238{
2239 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
2240 int shift = RADIX_TREE_INDEX_BITS - width;
2241
2242 if (shift < 0)
2243 return ~0UL;
2244 if (shift >= BITS_PER_LONG)
2245 return 0UL;
2246 return ~0UL >> shift;
2247}
2248
2249static __init void radix_tree_init_maxnodes(void)
2250{
2251 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
2252 unsigned int i, j;
2253
2254 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
2255 height_to_maxindex[i] = __maxindex(i);
2256 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
2257 for (j = i; j > 0; j--)
2258 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
2259 }
2260}
2261
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01002262static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002264 struct radix_tree_preload *rtp;
2265 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002267 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01002268 rtp = &per_cpu(radix_tree_preloads, cpu);
2269 while (rtp->nr) {
2270 node = rtp->nodes;
Matthew Wilcox1293d5c2017-01-16 16:41:29 -05002271 rtp->nodes = node->parent;
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01002272 kmem_cache_free(radix_tree_node_cachep, node);
2273 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002274 }
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -05002275 kfree(per_cpu(ida_bitmap, cpu));
2276 per_cpu(ida_bitmap, cpu) = NULL;
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01002277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280void __init radix_tree_init(void)
2281{
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01002282 int ret;
Michal Hocko7e784422017-05-03 14:53:09 -07002283
2284 BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32);
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -07002285 BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2287 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07002288 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2289 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002290 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd52016-11-03 15:50:01 +01002291 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2292 NULL, radix_tree_cpu_dead);
2293 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294}