blob: 40f3091c5a6becffc408d2bcdc5424b2de52c55d [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 Wilcoxe157b552016-12-14 15:09:01 -080025#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/errno.h>
27#include <linux/init.h>
28#include <linux/kernel.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/radix-tree.h>
31#include <linux/percpu.h>
32#include <linux/slab.h>
Catalin Marinasce80b062014-06-06 14:38:18 -070033#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/string.h>
36#include <linux/bitops.h>
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080037#include <linux/rcupdate.h>
Frederic Weisbecker92cf2112015-05-12 16:41:46 +020038#include <linux/preempt.h> /* in_interrupt() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -070041/* Number of nodes in fully populated tree of given height */
42static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;
43
Jeff Moyer26fb1582007-10-16 01:24:49 -070044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * Radix tree node cache.
46 */
Christoph Lametere18b8902006-12-06 20:33:20 -080047static struct kmem_cache *radix_tree_node_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/*
Nick Piggin55368052012-05-29 15:07:34 -070050 * The radix tree is variable-height, so an insert operation not only has
51 * to build the branch to its corresponding item, it also has to build the
52 * branch to existing items if the size has to be increased (by
53 * radix_tree_extend).
54 *
55 * The worst case is a zero height tree with just a single item at index 0,
56 * and then inserting an item at index ULONG_MAX. This requires 2 new branches
57 * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared.
58 * Hence:
59 */
60#define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1)
61
62/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 * Per-cpu pool of preloaded nodes
64 */
65struct radix_tree_preload {
Matthew Wilcox2fcd9002016-05-20 17:03:04 -070066 unsigned nr;
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -070067 /* nodes->private_data points to next preallocated node */
68 struct radix_tree_node *nodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
Harvey Harrison8cef7d52009-01-06 14:40:50 -080070static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, };
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Matthew Wilcox148deab2016-12-14 15:08:49 -080072static inline struct radix_tree_node *entry_to_node(void *ptr)
73{
74 return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE);
75}
76
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070077static inline void *node_to_entry(void *ptr)
Nick Piggin27d20fd2010-11-11 14:05:19 -080078{
Matthew Wilcox30ff46cc2016-05-20 17:03:22 -070079 return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE);
Nick Piggin27d20fd2010-11-11 14:05:19 -080080}
81
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -070082#define RADIX_TREE_RETRY node_to_entry(NULL)
Matthew Wilcoxafe0e392016-05-20 17:02:17 -070083
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070084#ifdef CONFIG_RADIX_TREE_MULTIORDER
85/* Sibling slots point directly to another slot in the same node */
Matthew Wilcox35534c82016-12-19 17:43:19 -050086static inline
87bool is_sibling_entry(const struct radix_tree_node *parent, void *node)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070088{
89 void **ptr = node;
90 return (parent->slots <= ptr) &&
91 (ptr < parent->slots + RADIX_TREE_MAP_SIZE);
92}
93#else
Matthew Wilcox35534c82016-12-19 17:43:19 -050094static inline
95bool is_sibling_entry(const struct radix_tree_node *parent, void *node)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -070096{
97 return false;
98}
99#endif
100
Matthew Wilcox35534c82016-12-19 17:43:19 -0500101static inline
102unsigned long get_slot_offset(const struct radix_tree_node *parent, void **slot)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700103{
104 return slot - parent->slots;
105}
106
Matthew Wilcox35534c82016-12-19 17:43:19 -0500107static unsigned int radix_tree_descend(const struct radix_tree_node *parent,
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700108 struct radix_tree_node **nodep, unsigned long index)
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700109{
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700110 unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK;
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700111 void **entry = rcu_dereference_raw(parent->slots[offset]);
112
113#ifdef CONFIG_RADIX_TREE_MULTIORDER
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700114 if (radix_tree_is_internal_node(entry)) {
Linus Torvalds8d2c0d32016-09-25 13:32:46 -0700115 if (is_sibling_entry(parent, entry)) {
116 void **sibentry = (void **) entry_to_node(entry);
117 offset = get_slot_offset(parent, sibentry);
118 entry = rcu_dereference_raw(*sibentry);
Matthew Wilcoxdb050f22016-05-20 17:01:57 -0700119 }
120 }
121#endif
122
123 *nodep = (void *)entry;
124 return offset;
125}
126
Matthew Wilcox35534c82016-12-19 17:43:19 -0500127static inline gfp_t root_gfp_mask(const struct radix_tree_root *root)
Nick Piggin612d6c12006-06-23 02:03:22 -0700128{
129 return root->gfp_mask & __GFP_BITS_MASK;
130}
131
Nick Piggin643b52b2008-06-12 15:21:52 -0700132static inline void tag_set(struct radix_tree_node *node, unsigned int tag,
133 int offset)
134{
135 __set_bit(offset, node->tags[tag]);
136}
137
138static inline void tag_clear(struct radix_tree_node *node, unsigned int tag,
139 int offset)
140{
141 __clear_bit(offset, node->tags[tag]);
142}
143
Matthew Wilcox35534c82016-12-19 17:43:19 -0500144static inline int tag_get(const struct radix_tree_node *node, unsigned int tag,
Nick Piggin643b52b2008-06-12 15:21:52 -0700145 int offset)
146{
147 return test_bit(offset, node->tags[tag]);
148}
149
Matthew Wilcox35534c82016-12-19 17:43:19 -0500150static inline void root_tag_set(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700151{
152 root->gfp_mask |= (__force gfp_t)(1 << (tag + __GFP_BITS_SHIFT));
153}
154
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700155static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700156{
157 root->gfp_mask &= (__force gfp_t)~(1 << (tag + __GFP_BITS_SHIFT));
158}
159
160static inline void root_tag_clear_all(struct radix_tree_root *root)
161{
162 root->gfp_mask &= __GFP_BITS_MASK;
163}
164
Matthew Wilcox35534c82016-12-19 17:43:19 -0500165static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700166{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700167 return (__force int)root->gfp_mask & (1 << (tag + __GFP_BITS_SHIFT));
Nick Piggin643b52b2008-06-12 15:21:52 -0700168}
169
Matthew Wilcox35534c82016-12-19 17:43:19 -0500170static inline unsigned root_tags_get(const struct radix_tree_root *root)
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700171{
172 return (__force unsigned)root->gfp_mask >> __GFP_BITS_SHIFT;
173}
174
Nick Piggin643b52b2008-06-12 15:21:52 -0700175/*
176 * Returns 1 if any slot in the node has this tag set.
177 * Otherwise returns 0.
178 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500179static inline int any_tag_set(const struct radix_tree_node *node,
180 unsigned int tag)
Nick Piggin643b52b2008-06-12 15:21:52 -0700181{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700182 unsigned idx;
Nick Piggin643b52b2008-06-12 15:21:52 -0700183 for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) {
184 if (node->tags[tag][idx])
185 return 1;
186 }
187 return 0;
188}
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700189
190/**
191 * radix_tree_find_next_bit - find the next set bit in a memory region
192 *
193 * @addr: The address to base the search on
194 * @size: The bitmap size in bits
195 * @offset: The bitnumber to start searching at
196 *
197 * Unrollable variant of find_next_bit() for constant size arrays.
198 * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero.
199 * Returns next bit offset, or size if nothing found.
200 */
201static __always_inline unsigned long
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800202radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag,
203 unsigned long offset)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700204{
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800205 const unsigned long *addr = node->tags[tag];
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700206
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800207 if (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700208 unsigned long tmp;
209
210 addr += offset / BITS_PER_LONG;
211 tmp = *addr >> (offset % BITS_PER_LONG);
212 if (tmp)
213 return __ffs(tmp) + offset;
214 offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1);
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800215 while (offset < RADIX_TREE_MAP_SIZE) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700216 tmp = *++addr;
217 if (tmp)
218 return __ffs(tmp) + offset;
219 offset += BITS_PER_LONG;
220 }
221 }
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -0800222 return RADIX_TREE_MAP_SIZE;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -0700223}
224
Matthew Wilcox268f42d2016-12-14 15:08:55 -0800225static unsigned int iter_offset(const struct radix_tree_iter *iter)
226{
227 return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK;
228}
229
Matthew Wilcox218ed752016-12-14 15:08:43 -0800230/*
231 * The maximum index which can be stored in a radix tree
232 */
233static inline unsigned long shift_maxindex(unsigned int shift)
234{
235 return (RADIX_TREE_MAP_SIZE << shift) - 1;
236}
237
Matthew Wilcox35534c82016-12-19 17:43:19 -0500238static inline unsigned long node_maxindex(const struct radix_tree_node *node)
Matthew Wilcox218ed752016-12-14 15:08:43 -0800239{
240 return shift_maxindex(node->shift);
241}
242
Ross Zwisler0796c582016-05-20 17:02:55 -0700243#ifndef __KERNEL__
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700244static void dump_node(struct radix_tree_node *node, unsigned long index)
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700245{
Ross Zwisler0796c582016-05-20 17:02:55 -0700246 unsigned long i;
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700247
Matthew Wilcox218ed752016-12-14 15:08:43 -0800248 pr_debug("radix node: %p offset %d indices %lu-%lu parent %p tags %lx %lx %lx shift %d count %d exceptional %d\n",
249 node, node->offset, index, index | node_maxindex(node),
250 node->parent,
Ross Zwisler0796c582016-05-20 17:02:55 -0700251 node->tags[0][0], node->tags[1][0], node->tags[2][0],
Matthew Wilcox218ed752016-12-14 15:08:43 -0800252 node->shift, node->count, node->exceptional);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700253
Ross Zwisler0796c582016-05-20 17:02:55 -0700254 for (i = 0; i < RADIX_TREE_MAP_SIZE; i++) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700255 unsigned long first = index | (i << node->shift);
256 unsigned long last = first | ((1UL << node->shift) - 1);
Ross Zwisler0796c582016-05-20 17:02:55 -0700257 void *entry = node->slots[i];
258 if (!entry)
259 continue;
Matthew Wilcox218ed752016-12-14 15:08:43 -0800260 if (entry == RADIX_TREE_RETRY) {
261 pr_debug("radix retry offset %ld indices %lu-%lu parent %p\n",
262 i, first, last, node);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700263 } else if (!radix_tree_is_internal_node(entry)) {
Matthew Wilcox218ed752016-12-14 15:08:43 -0800264 pr_debug("radix entry %p offset %ld indices %lu-%lu parent %p\n",
265 entry, i, first, last, node);
266 } else if (is_sibling_entry(node, entry)) {
267 pr_debug("radix sblng %p offset %ld indices %lu-%lu parent %p val %p\n",
268 entry, i, first, last, node,
269 *(void **)entry_to_node(entry));
Ross Zwisler0796c582016-05-20 17:02:55 -0700270 } else {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700271 dump_node(entry_to_node(entry), first);
Ross Zwisler0796c582016-05-20 17:02:55 -0700272 }
273 }
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700274}
275
276/* For debug */
277static void radix_tree_dump(struct radix_tree_root *root)
278{
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700279 pr_debug("radix root: %p rnode %p tags %x\n",
280 root, root->rnode,
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700281 root->gfp_mask >> __GFP_BITS_SHIFT);
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700282 if (!radix_tree_is_internal_node(root->rnode))
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700283 return;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700284 dump_node(entry_to_node(root->rnode), 0);
Matthew Wilcox7cf19af2016-03-17 14:21:57 -0700285}
286#endif
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288/*
289 * This assumes that the caller has performed appropriate preallocation, and
290 * that the caller has pinned this thread of control to the current CPU.
291 */
292static struct radix_tree_node *
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800293radix_tree_node_alloc(struct radix_tree_root *root,
294 struct radix_tree_node *parent,
295 unsigned int shift, unsigned int offset,
296 unsigned int count, unsigned int exceptional)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
Nick Piggine2848a02008-02-04 22:29:10 -0800298 struct radix_tree_node *ret = NULL;
Nick Piggin612d6c12006-06-23 02:03:22 -0700299 gfp_t gfp_mask = root_gfp_mask(root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Jan Kara5e4c0d972013-09-11 14:26:05 -0700301 /*
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700302 * Preload code isn't irq safe and it doesn't make sense to use
303 * preloading during an interrupt anyway as all the allocations have
304 * to be atomic. So just do normal allocation when in interrupt.
Jan Kara5e4c0d972013-09-11 14:26:05 -0700305 */
Mel Gormand0164ad2015-11-06 16:28:21 -0800306 if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct radix_tree_preload *rtp;
308
Nick Piggine2848a02008-02-04 22:29:10 -0800309 /*
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700310 * Even if the caller has preloaded, try to allocate from the
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700311 * cache first for the new node to get accounted to the memory
312 * cgroup.
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700313 */
314 ret = kmem_cache_alloc(radix_tree_node_cachep,
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700315 gfp_mask | __GFP_NOWARN);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700316 if (ret)
317 goto out;
318
319 /*
Nick Piggine2848a02008-02-04 22:29:10 -0800320 * Provided the caller has preloaded here, we will always
321 * succeed in getting a node here (and never reach
322 * kmem_cache_alloc)
323 */
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700324 rtp = this_cpu_ptr(&radix_tree_preloads);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (rtp->nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700326 ret = rtp->nodes;
327 rtp->nodes = ret->private_data;
328 ret->private_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 rtp->nr--;
330 }
Catalin Marinasce80b062014-06-06 14:38:18 -0700331 /*
332 * Update the allocation stack trace as this is more useful
333 * for debugging.
334 */
335 kmemleak_update_trace(ret);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700336 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700338 ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Vladimir Davydov58e698a2016-03-17 14:18:36 -0700339out:
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700340 BUG_ON(radix_tree_is_internal_node(ret));
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800341 if (ret) {
342 ret->parent = parent;
343 ret->shift = shift;
344 ret->offset = offset;
345 ret->count = count;
346 ret->exceptional = exceptional;
347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return ret;
349}
350
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800351static void radix_tree_node_rcu_free(struct rcu_head *head)
352{
353 struct radix_tree_node *node =
354 container_of(head, struct radix_tree_node, rcu_head);
Nick Piggin643b52b2008-06-12 15:21:52 -0700355
356 /*
Matthew Wilcox175542f2016-12-14 15:08:58 -0800357 * Must only free zeroed nodes into the slab. We can be left with
358 * non-NULL entries by radix_tree_free_nodes, so clear the entries
359 * and tags here.
Nick Piggin643b52b2008-06-12 15:21:52 -0700360 */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800361 memset(node->slots, 0, sizeof(node->slots));
362 memset(node->tags, 0, sizeof(node->tags));
Matthew Wilcox91d9c052016-12-14 15:08:34 -0800363 INIT_LIST_HEAD(&node->private_list);
Nick Piggin643b52b2008-06-12 15:21:52 -0700364
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800365 kmem_cache_free(radix_tree_node_cachep, node);
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368static inline void
369radix_tree_node_free(struct radix_tree_node *node)
370{
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800371 call_rcu(&node->rcu_head, radix_tree_node_rcu_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374/*
375 * Load up this CPU's radix_tree_node buffer with sufficient objects to
376 * ensure that the addition of a single element in the tree cannot fail. On
377 * success, return zero, with preemption disabled. On error, return -ENOMEM
378 * with preemption not disabled.
David Howellsb34df792009-11-19 18:11:14 +0000379 *
380 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800381 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 */
Matthew Wilcox27916532016-12-14 15:09:04 -0800383static int __radix_tree_preload(gfp_t gfp_mask, unsigned nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 struct radix_tree_preload *rtp;
386 struct radix_tree_node *node;
387 int ret = -ENOMEM;
388
Vladimir Davydov05eb6e72016-08-02 14:03:01 -0700389 /*
390 * Nodes preloaded by one cgroup can be be used by another cgroup, so
391 * they should never be accounted to any particular memory cgroup.
392 */
393 gfp_mask &= ~__GFP_ACCOUNT;
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700396 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700397 while (rtp->nr < nr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 preempt_enable();
Christoph Lameter488514d2008-04-28 02:12:05 -0700399 node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 if (node == NULL)
401 goto out;
402 preempt_disable();
Christoph Lameter7c8e0182014-06-04 16:07:56 -0700403 rtp = this_cpu_ptr(&radix_tree_preloads);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700404 if (rtp->nr < nr) {
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700405 node->private_data = rtp->nodes;
406 rtp->nodes = node;
407 rtp->nr++;
408 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 kmem_cache_free(radix_tree_node_cachep, node);
Kirill A. Shutemov9d2a8da2015-06-25 15:02:19 -0700410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412 ret = 0;
413out:
414 return ret;
415}
Jan Kara5e4c0d972013-09-11 14:26:05 -0700416
417/*
418 * Load up this CPU's radix_tree_node buffer with sufficient objects to
419 * ensure that the addition of a single element in the tree cannot fail. On
420 * success, return zero, with preemption disabled. On error, return -ENOMEM
421 * with preemption not disabled.
422 *
423 * To make use of this facility, the radix tree must be initialised without
Mel Gormand0164ad2015-11-06 16:28:21 -0800424 * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE().
Jan Kara5e4c0d972013-09-11 14:26:05 -0700425 */
426int radix_tree_preload(gfp_t gfp_mask)
427{
428 /* Warn on non-sensical use... */
Mel Gormand0164ad2015-11-06 16:28:21 -0800429 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700430 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700431}
David Chinnerd7f09232007-07-14 16:05:04 +1000432EXPORT_SYMBOL(radix_tree_preload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Nick Piggin6e954b92006-01-08 01:01:40 -0800434/*
Jan Kara5e4c0d972013-09-11 14:26:05 -0700435 * The same as above function, except we don't guarantee preloading happens.
436 * We do it, if we decide it helps. On success, return zero with preemption
437 * disabled. On error, return -ENOMEM with preemption not disabled.
438 */
439int radix_tree_maybe_preload(gfp_t gfp_mask)
440{
Mel Gormand0164ad2015-11-06 16:28:21 -0800441 if (gfpflags_allow_blocking(gfp_mask))
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700442 return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE);
Jan Kara5e4c0d972013-09-11 14:26:05 -0700443 /* Preloading doesn't help anything with this gfp mask, skip it */
444 preempt_disable();
445 return 0;
446}
447EXPORT_SYMBOL(radix_tree_maybe_preload);
448
Matthew Wilcox27916532016-12-14 15:09:04 -0800449#ifdef CONFIG_RADIX_TREE_MULTIORDER
450/*
451 * Preload with enough objects to ensure that we can split a single entry
452 * of order @old_order into many entries of size @new_order
453 */
454int radix_tree_split_preload(unsigned int old_order, unsigned int new_order,
455 gfp_t gfp_mask)
456{
457 unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT);
458 unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) -
459 (new_order / RADIX_TREE_MAP_SHIFT);
460 unsigned nr = 0;
461
462 WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask));
463 BUG_ON(new_order >= old_order);
464
465 while (layers--)
466 nr = nr * RADIX_TREE_MAP_SIZE + 1;
467 return __radix_tree_preload(gfp_mask, top * nr);
468}
469#endif
470
Jan Kara5e4c0d972013-09-11 14:26:05 -0700471/*
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -0700472 * The same as function above, but preload number of nodes required to insert
473 * (1 << order) continuous naturally-aligned elements.
474 */
475int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order)
476{
477 unsigned long nr_subtrees;
478 int nr_nodes, subtree_height;
479
480 /* Preloading doesn't help anything with this gfp mask, skip it */
481 if (!gfpflags_allow_blocking(gfp_mask)) {
482 preempt_disable();
483 return 0;
484 }
485
486 /*
487 * Calculate number and height of fully populated subtrees it takes to
488 * store (1 << order) elements.
489 */
490 nr_subtrees = 1 << order;
491 for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE;
492 subtree_height++)
493 nr_subtrees >>= RADIX_TREE_MAP_SHIFT;
494
495 /*
496 * The worst case is zero height tree with a single item at index 0 and
497 * then inserting items starting at ULONG_MAX - (1 << order).
498 *
499 * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to
500 * 0-index item.
501 */
502 nr_nodes = RADIX_TREE_MAX_PATH;
503
504 /* Plus branch to fully populated subtrees. */
505 nr_nodes += RADIX_TREE_MAX_PATH - subtree_height;
506
507 /* Root node is shared. */
508 nr_nodes--;
509
510 /* Plus nodes required to build subtrees. */
511 nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height];
512
513 return __radix_tree_preload(gfp_mask, nr_nodes);
514}
515
Matthew Wilcox35534c82016-12-19 17:43:19 -0500516static unsigned radix_tree_load_root(const struct radix_tree_root *root,
Matthew Wilcox1456a432016-05-20 17:02:08 -0700517 struct radix_tree_node **nodep, unsigned long *maxindex)
518{
519 struct radix_tree_node *node = rcu_dereference_raw(root->rnode);
520
521 *nodep = node;
522
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700523 if (likely(radix_tree_is_internal_node(node))) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700524 node = entry_to_node(node);
Matthew Wilcox1456a432016-05-20 17:02:08 -0700525 *maxindex = node_maxindex(node);
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700526 return node->shift + RADIX_TREE_MAP_SHIFT;
Matthew Wilcox1456a432016-05-20 17:02:08 -0700527 }
528
529 *maxindex = 0;
530 return 0;
531}
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533/*
534 * Extend a radix tree so it can store key @index.
535 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700536static int radix_tree_extend(struct radix_tree_root *root,
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700537 unsigned long index, unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800539 struct radix_tree_node *slot;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700540 unsigned int maxshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 int tag;
542
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700543 /* Figure out what the shift should be. */
544 maxshift = shift;
545 while (index > shift_maxindex(maxshift))
546 maxshift += RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700548 slot = root->rnode;
549 if (!slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 do {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800553 struct radix_tree_node *node = radix_tree_node_alloc(root,
554 NULL, shift, 0, 1, 0);
Matthew Wilcox2fcd9002016-05-20 17:03:04 -0700555 if (!node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -ENOMEM;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* Propagate the aggregated tag info into the new root */
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800559 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) {
Nick Piggin612d6c12006-06-23 02:03:22 -0700560 if (root_tag_get(root, tag))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 tag_set(node, tag, 0);
562 }
563
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700564 BUG_ON(shift > BITS_PER_LONG);
Johannes Weinerf7942432016-12-12 16:43:41 -0800565 if (radix_tree_is_internal_node(slot)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700566 entry_to_node(slot)->parent = node;
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800567 } else if (radix_tree_exceptional_entry(slot)) {
Johannes Weinerf7942432016-12-12 16:43:41 -0800568 /* Moving an exceptional root->rnode to a node */
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800569 node->exceptional = 1;
Johannes Weinerf7942432016-12-12 16:43:41 -0800570 }
Hugh Dickinse2bdb932012-01-12 17:20:41 -0800571 node->slots[0] = slot;
Matthew Wilcoxa4db4dc2016-05-20 17:03:24 -0700572 slot = node_to_entry(node);
573 rcu_assign_pointer(root->rnode, slot);
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700574 shift += RADIX_TREE_MAP_SHIFT;
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700575 } while (shift <= maxshift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576out:
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700577 return maxshift + RADIX_TREE_MAP_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/**
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800581 * radix_tree_shrink - shrink radix tree to minimum height
582 * @root radix tree root
583 */
Johannes Weiner14b46872016-12-12 16:43:52 -0800584static inline void radix_tree_shrink(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800585 radix_tree_update_node_t update_node,
586 void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800587{
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800588 for (;;) {
589 struct radix_tree_node *node = root->rnode;
590 struct radix_tree_node *child;
591
592 if (!radix_tree_is_internal_node(node))
593 break;
594 node = entry_to_node(node);
595
596 /*
597 * The candidate node has more than one child, or its child
598 * is not at the leftmost slot, or the child is a multiorder
599 * entry, we cannot shrink.
600 */
601 if (node->count != 1)
602 break;
603 child = node->slots[0];
604 if (!child)
605 break;
606 if (!radix_tree_is_internal_node(child) && node->shift)
607 break;
608
609 if (radix_tree_is_internal_node(child))
610 entry_to_node(child)->parent = NULL;
611
612 /*
613 * We don't need rcu_assign_pointer(), since we are simply
614 * moving the node from one part of the tree to another: if it
615 * was safe to dereference the old pointer to it
616 * (node->slots[0]), it will be safe to dereference the new
617 * one (root->rnode) as far as dependent read barriers go.
618 */
619 root->rnode = child;
620
621 /*
622 * We have a dilemma here. The node's slot[0] must not be
623 * NULLed in case there are concurrent lookups expecting to
624 * find the item. However if this was a bottom-level node,
625 * then it may be subject to the slot pointer being visible
626 * to callers dereferencing it. If item corresponding to
627 * slot[0] is subsequently deleted, these callers would expect
628 * their slot to become empty sooner or later.
629 *
630 * For example, lockless pagecache will look up a slot, deref
631 * the page pointer, and if the page has 0 refcount it means it
632 * was concurrently deleted from pagecache so try the deref
633 * again. Fortunately there is already a requirement for logic
634 * to retry the entire slot lookup -- the indirect pointer
635 * problem (replacing direct root node with an indirect pointer
636 * also results in a stale slot). So tag the slot as indirect
637 * to force callers to retry.
638 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800639 node->count = 0;
640 if (!radix_tree_is_internal_node(child)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800641 node->slots[0] = RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800642 if (update_node)
643 update_node(node, private);
644 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800645
Johannes Weinerea07b862017-01-06 19:21:43 -0500646 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800647 radix_tree_node_free(node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800648 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800649}
650
Johannes Weiner14b46872016-12-12 16:43:52 -0800651static void delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800652 struct radix_tree_node *node,
653 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800654{
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800655 do {
656 struct radix_tree_node *parent;
657
658 if (node->count) {
659 if (node == entry_to_node(root->rnode))
Johannes Weiner14b46872016-12-12 16:43:52 -0800660 radix_tree_shrink(root, update_node, private);
661 return;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800662 }
663
664 parent = node->parent;
665 if (parent) {
666 parent->slots[node->offset] = NULL;
667 parent->count--;
668 } else {
669 root_tag_clear_all(root);
670 root->rnode = NULL;
671 }
672
Johannes Weinerea07b862017-01-06 19:21:43 -0500673 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800674 radix_tree_node_free(node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800675
676 node = parent;
677 } while (node);
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800678}
679
680/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700681 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * @root: radix tree root
683 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700684 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700685 * @nodep: returns node
686 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700688 * Create, if necessary, and return the node and slot for an item
689 * at position @index in the radix tree @root.
690 *
691 * Until there is more than one item in the tree, no nodes are
692 * allocated and @root->rnode is used as a direct slot instead of
693 * pointing to a node, in which case *@nodep will be NULL.
694 *
695 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700697int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700698 unsigned order, struct radix_tree_node **nodep,
699 void ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700701 struct radix_tree_node *node = NULL, *child;
702 void **slot = (void **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700703 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700704 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700705 unsigned long max = index | ((1UL << order) - 1);
706
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700707 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800710 if (order > 0 && max == ((1UL << order) - 1))
711 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700712 if (max > maxindex) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700713 int error = radix_tree_extend(root, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700714 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700716 shift = error;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700717 child = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700720 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700721 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700722 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 /* Have to add a child node. */
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800724 child = radix_tree_node_alloc(root, node, shift,
725 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700726 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700728 rcu_assign_pointer(*slot, node_to_entry(child));
729 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700731 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700732 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700735 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700736 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700737 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700738 }
739
Johannes Weiner139e5612014-04-03 14:47:54 -0700740 if (nodep)
741 *nodep = node;
742 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700743 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700744 return 0;
745}
746
Matthew Wilcox175542f2016-12-14 15:08:58 -0800747#ifdef CONFIG_RADIX_TREE_MULTIORDER
748/*
749 * Free any nodes below this node. The tree is presumed to not need
750 * shrinking, and any user data in the tree is presumed to not need a
751 * destructor called on it. If we need to add a destructor, we can
752 * add that functionality later. Note that we may not clear tags or
753 * slots from the tree as an RCU walker may still have a pointer into
754 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
755 * but we'll still have to clear those in rcu_free.
756 */
757static void radix_tree_free_nodes(struct radix_tree_node *node)
758{
759 unsigned offset = 0;
760 struct radix_tree_node *child = entry_to_node(node);
761
762 for (;;) {
763 void *entry = child->slots[offset];
764 if (radix_tree_is_internal_node(entry) &&
765 !is_sibling_entry(child, entry)) {
766 child = entry_to_node(entry);
767 offset = 0;
768 continue;
769 }
770 offset++;
771 while (offset == RADIX_TREE_MAP_SIZE) {
772 struct radix_tree_node *old = child;
773 offset = child->offset + 1;
774 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800775 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800776 radix_tree_node_free(old);
777 if (old == entry_to_node(node))
778 return;
779 }
780 }
781}
782
783static inline int insert_entries(struct radix_tree_node *node, void **slot,
784 void *item, unsigned order, bool replace)
785{
786 struct radix_tree_node *child;
787 unsigned i, n, tag, offset, tags = 0;
788
789 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800790 if (order > node->shift)
791 n = 1 << (order - node->shift);
792 else
793 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800794 offset = get_slot_offset(node, slot);
795 } else {
796 n = 1;
797 offset = 0;
798 }
799
800 if (n > 1) {
801 offset = offset & ~(n - 1);
802 slot = &node->slots[offset];
803 }
804 child = node_to_entry(slot);
805
806 for (i = 0; i < n; i++) {
807 if (slot[i]) {
808 if (replace) {
809 node->count--;
810 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
811 if (tag_get(node, tag, offset + i))
812 tags |= 1 << tag;
813 } else
814 return -EEXIST;
815 }
816 }
817
818 for (i = 0; i < n; i++) {
819 struct radix_tree_node *old = slot[i];
820 if (i) {
821 rcu_assign_pointer(slot[i], child);
822 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
823 if (tags & (1 << tag))
824 tag_clear(node, tag, offset + i);
825 } else {
826 rcu_assign_pointer(slot[i], item);
827 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
828 if (tags & (1 << tag))
829 tag_set(node, tag, offset);
830 }
831 if (radix_tree_is_internal_node(old) &&
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800832 !is_sibling_entry(node, old) &&
833 (old != RADIX_TREE_RETRY))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800834 radix_tree_free_nodes(old);
835 if (radix_tree_exceptional_entry(old))
836 node->exceptional--;
837 }
838 if (node) {
839 node->count += n;
840 if (radix_tree_exceptional_entry(item))
841 node->exceptional += n;
842 }
843 return n;
844}
845#else
846static inline int insert_entries(struct radix_tree_node *node, void **slot,
847 void *item, unsigned order, bool replace)
848{
849 if (*slot)
850 return -EEXIST;
851 rcu_assign_pointer(*slot, item);
852 if (node) {
853 node->count++;
854 if (radix_tree_exceptional_entry(item))
855 node->exceptional++;
856 }
857 return 1;
858}
859#endif
860
Johannes Weiner139e5612014-04-03 14:47:54 -0700861/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700862 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700863 * @root: radix tree root
864 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700865 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700866 * @item: item to insert
867 *
868 * Insert an item into the radix tree at position @index.
869 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700870int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
871 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700872{
873 struct radix_tree_node *node;
874 void **slot;
875 int error;
876
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700877 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700878
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700879 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700880 if (error)
881 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800882
883 error = insert_entries(node, slot, item, order, false);
884 if (error < 0)
885 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700886
Nick Piggin612d6c12006-06-23 02:03:22 -0700887 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700888 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700889 BUG_ON(tag_get(node, 0, offset));
890 BUG_ON(tag_get(node, 1, offset));
891 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700892 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700893 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 0;
897}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700898EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Johannes Weiner139e5612014-04-03 14:47:54 -0700900/**
901 * __radix_tree_lookup - lookup an item in a radix tree
902 * @root: radix tree root
903 * @index: index key
904 * @nodep: returns node
905 * @slotp: returns slot
906 *
907 * Lookup and return the item at position @index in the radix
908 * tree @root.
909 *
910 * Until there is more than one item in the tree, no nodes are
911 * allocated and @root->rnode is used as a direct slot instead of
912 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800913 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500914void *__radix_tree_lookup(const struct radix_tree_root *root,
915 unsigned long index, struct radix_tree_node **nodep,
916 void ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800917{
Johannes Weiner139e5612014-04-03 14:47:54 -0700918 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700919 unsigned long maxindex;
Johannes Weiner139e5612014-04-03 14:47:54 -0700920 void **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800921
Matthew Wilcox85829952016-05-20 17:02:20 -0700922 restart:
923 parent = NULL;
924 slot = (void **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700925 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700926 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800927 return NULL;
928
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700929 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700930 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700931
Matthew Wilcox85829952016-05-20 17:02:20 -0700932 if (node == RADIX_TREE_RETRY)
933 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700934 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700935 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700936 slot = parent->slots + offset;
937 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800938
Johannes Weiner139e5612014-04-03 14:47:54 -0700939 if (nodep)
940 *nodep = parent;
941 if (slotp)
942 *slotp = slot;
943 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700944}
945
946/**
947 * radix_tree_lookup_slot - lookup a slot in a radix tree
948 * @root: radix tree root
949 * @index: index key
950 *
951 * Returns: the slot corresponding to the position @index in the
952 * radix tree @root. This is useful for update-if-exists operations.
953 *
954 * This function can be called under rcu_read_lock iff the slot is not
955 * modified by radix_tree_replace_slot, otherwise it must be called
956 * exclusive from other writers. Any dereference of the slot must be done
957 * using radix_tree_deref_slot.
958 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500959void **radix_tree_lookup_slot(const struct radix_tree_root *root,
960 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -0700961{
Johannes Weiner139e5612014-04-03 14:47:54 -0700962 void **slot;
963
964 if (!__radix_tree_lookup(root, index, NULL, &slot))
965 return NULL;
966 return slot;
Hans Reisera4331362005-11-07 00:59:29 -0800967}
968EXPORT_SYMBOL(radix_tree_lookup_slot);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970/**
971 * radix_tree_lookup - perform lookup operation on a radix tree
972 * @root: radix tree root
973 * @index: index key
974 *
975 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800976 *
977 * This function can be called under rcu_read_lock, however the caller
978 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
979 * them safely). No RCU barriers are required to access or modify the
980 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500982void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Johannes Weiner139e5612014-04-03 14:47:54 -0700984 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986EXPORT_SYMBOL(radix_tree_lookup);
987
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800988static inline int slot_count(struct radix_tree_node *node,
989 void **slot)
990{
991 int n = 1;
992#ifdef CONFIG_RADIX_TREE_MULTIORDER
993 void *ptr = node_to_entry(slot);
994 unsigned offset = get_slot_offset(node, slot);
995 int i;
996
997 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
998 if (node->slots[offset + i] != ptr)
999 break;
1000 n++;
1001 }
1002#endif
1003 return n;
1004}
1005
Johannes Weiner6d75f362016-12-12 16:43:43 -08001006static void replace_slot(struct radix_tree_root *root,
1007 struct radix_tree_node *node,
1008 void **slot, void *item,
1009 bool warn_typeswitch)
1010{
1011 void *old = rcu_dereference_raw(*slot);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001012 int count, exceptional;
Johannes Weiner6d75f362016-12-12 16:43:43 -08001013
1014 WARN_ON_ONCE(radix_tree_is_internal_node(item));
Johannes Weiner6d75f362016-12-12 16:43:43 -08001015
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001016 count = !!item - !!old;
Johannes Weiner6d75f362016-12-12 16:43:43 -08001017 exceptional = !!radix_tree_exceptional_entry(item) -
1018 !!radix_tree_exceptional_entry(old);
1019
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001020 WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
Johannes Weiner6d75f362016-12-12 16:43:43 -08001021
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001022 if (node) {
1023 node->count += count;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001024 if (exceptional) {
1025 exceptional *= slot_count(node, slot);
1026 node->exceptional += exceptional;
1027 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001028 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001029
1030 rcu_assign_pointer(*slot, item);
1031}
1032
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001033static inline void delete_sibling_entries(struct radix_tree_node *node,
1034 void **slot)
1035{
1036#ifdef CONFIG_RADIX_TREE_MULTIORDER
1037 bool exceptional = radix_tree_exceptional_entry(*slot);
1038 void *ptr = node_to_entry(slot);
1039 unsigned offset = get_slot_offset(node, slot);
1040 int i;
1041
1042 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1043 if (node->slots[offset + i] != ptr)
1044 break;
1045 node->slots[offset + i] = NULL;
1046 node->count--;
1047 if (exceptional)
1048 node->exceptional--;
1049 }
1050#endif
1051}
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053/**
Johannes Weinerf7942432016-12-12 16:43:41 -08001054 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -08001055 * @root: radix tree root
1056 * @node: pointer to tree node
1057 * @slot: pointer to slot in @node
1058 * @item: new item to store in the slot.
1059 * @update_node: callback for changing leaf nodes
1060 * @private: private data to pass to @update_node
Johannes Weinerf7942432016-12-12 16:43:41 -08001061 *
1062 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1063 * across slot lookup and replacement.
1064 */
1065void __radix_tree_replace(struct radix_tree_root *root,
1066 struct radix_tree_node *node,
Johannes Weiner4d693d02016-12-12 16:43:49 -08001067 void **slot, void *item,
1068 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf7942432016-12-12 16:43:41 -08001069{
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001070 if (!item)
1071 delete_sibling_entries(node, slot);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001072 /*
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001073 * This function supports replacing exceptional entries and
1074 * deleting entries, but that needs accounting against the
1075 * node unless the slot is root->rnode.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001076 */
1077 replace_slot(root, node, slot, item,
1078 !node && slot != (void **)&root->rnode);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001079
Johannes Weiner4d693d02016-12-12 16:43:49 -08001080 if (!node)
1081 return;
1082
1083 if (update_node)
1084 update_node(node, private);
1085
1086 delete_node(root, node, update_node, private);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001087}
Johannes Weinerf7942432016-12-12 16:43:41 -08001088
Johannes Weiner6d75f362016-12-12 16:43:43 -08001089/**
1090 * radix_tree_replace_slot - replace item in a slot
1091 * @root: radix tree root
1092 * @slot: pointer to slot
1093 * @item: new item to store in the slot.
1094 *
1095 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1096 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1097 * across slot lookup and replacement.
1098 *
1099 * NOTE: This cannot be used to switch between non-entries (empty slots),
1100 * regular entries, and exceptional entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001101 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001102 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1103 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001104 */
1105void radix_tree_replace_slot(struct radix_tree_root *root,
1106 void **slot, void *item)
1107{
1108 replace_slot(root, NULL, slot, item, true);
Johannes Weinerf7942432016-12-12 16:43:41 -08001109}
1110
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001111/**
1112 * radix_tree_iter_replace - replace item in a slot
1113 * @root: radix tree root
1114 * @slot: pointer to slot
1115 * @item: new item to store in the slot.
1116 *
1117 * For use with radix_tree_split() and radix_tree_for_each_slot().
1118 * Caller must hold tree write locked across split and replacement.
1119 */
1120void radix_tree_iter_replace(struct radix_tree_root *root,
1121 const struct radix_tree_iter *iter, void **slot, void *item)
1122{
1123 __radix_tree_replace(root, iter->node, slot, item, NULL, NULL);
1124}
1125
Matthew Wilcox175542f2016-12-14 15:08:58 -08001126#ifdef CONFIG_RADIX_TREE_MULTIORDER
1127/**
1128 * radix_tree_join - replace multiple entries with one multiorder entry
1129 * @root: radix tree root
1130 * @index: an index inside the new entry
1131 * @order: order of the new entry
1132 * @item: new entry
1133 *
1134 * Call this function to replace several entries with one larger entry.
1135 * The existing entries are presumed to not need freeing as a result of
1136 * this call.
1137 *
1138 * The replacement entry will have all the tags set on it that were set
1139 * on any of the entries it is replacing.
1140 */
1141int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1142 unsigned order, void *item)
1143{
1144 struct radix_tree_node *node;
1145 void **slot;
1146 int error;
1147
1148 BUG_ON(radix_tree_is_internal_node(item));
1149
1150 error = __radix_tree_create(root, index, order, &node, &slot);
1151 if (!error)
1152 error = insert_entries(node, slot, item, order, true);
1153 if (error > 0)
1154 error = 0;
1155
1156 return error;
1157}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001158
1159/**
1160 * radix_tree_split - Split an entry into smaller entries
1161 * @root: radix tree root
1162 * @index: An index within the large entry
1163 * @order: Order of new entries
1164 *
1165 * Call this function as the first step in replacing a multiorder entry
1166 * with several entries of lower order. After this function returns,
1167 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1168 * and call radix_tree_iter_replace() to set up each new entry.
1169 *
1170 * The tags from this entry are replicated to all the new entries.
1171 *
1172 * The radix tree should be locked against modification during the entire
1173 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1174 * should prompt RCU walkers to restart the lookup from the root.
1175 */
1176int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1177 unsigned order)
1178{
1179 struct radix_tree_node *parent, *node, *child;
1180 void **slot;
1181 unsigned int offset, end;
1182 unsigned n, tag, tags = 0;
1183
1184 if (!__radix_tree_lookup(root, index, &parent, &slot))
1185 return -ENOENT;
1186 if (!parent)
1187 return -ENOENT;
1188
1189 offset = get_slot_offset(parent, slot);
1190
1191 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1192 if (tag_get(parent, tag, offset))
1193 tags |= 1 << tag;
1194
1195 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
1196 if (!is_sibling_entry(parent, parent->slots[end]))
1197 break;
1198 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1199 if (tags & (1 << tag))
1200 tag_set(parent, tag, end);
1201 /* rcu_assign_pointer ensures tags are set before RETRY */
1202 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1203 }
1204 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
1205 parent->exceptional -= (end - offset);
1206
1207 if (order == parent->shift)
1208 return 0;
1209 if (order > parent->shift) {
1210 while (offset < end)
1211 offset += insert_entries(parent, &parent->slots[offset],
1212 RADIX_TREE_RETRY, order, true);
1213 return 0;
1214 }
1215
1216 node = parent;
1217
1218 for (;;) {
1219 if (node->shift > order) {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -08001220 child = radix_tree_node_alloc(root, node,
1221 node->shift - RADIX_TREE_MAP_SHIFT,
1222 offset, 0, 0);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001223 if (!child)
1224 goto nomem;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001225 if (node != parent) {
1226 node->count++;
1227 node->slots[offset] = node_to_entry(child);
1228 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1229 if (tags & (1 << tag))
1230 tag_set(node, tag, offset);
1231 }
1232
1233 node = child;
1234 offset = 0;
1235 continue;
1236 }
1237
1238 n = insert_entries(node, &node->slots[offset],
1239 RADIX_TREE_RETRY, order, false);
1240 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1241
1242 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1243 if (tags & (1 << tag))
1244 tag_set(node, tag, offset);
1245 offset += n;
1246
1247 while (offset == RADIX_TREE_MAP_SIZE) {
1248 if (node == parent)
1249 break;
1250 offset = node->offset;
1251 child = node;
1252 node = node->parent;
1253 rcu_assign_pointer(node->slots[offset],
1254 node_to_entry(child));
1255 offset++;
1256 }
1257 if ((node == parent) && (offset == end))
1258 return 0;
1259 }
1260
1261 nomem:
1262 /* Shouldn't happen; did user forget to preload? */
1263 /* TODO: free all the allocated nodes */
1264 WARN_ON(1);
1265 return -ENOMEM;
1266}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001267#endif
1268
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001269static void node_tag_set(struct radix_tree_root *root,
1270 struct radix_tree_node *node,
1271 unsigned int tag, unsigned int offset)
1272{
1273 while (node) {
1274 if (tag_get(node, tag, offset))
1275 return;
1276 tag_set(node, tag, offset);
1277 offset = node->offset;
1278 node = node->parent;
1279 }
1280
1281 if (!root_tag_get(root, tag))
1282 root_tag_set(root, tag);
1283}
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285/**
1286 * radix_tree_tag_set - set a tag on a radix tree node
1287 * @root: radix tree root
1288 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001289 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001291 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1292 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * the root all the way down to the leaf node.
1294 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001295 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 * item is a bug.
1297 */
1298void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001299 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Ross Zwislerfb969902016-05-20 17:02:32 -07001301 struct radix_tree_node *node, *parent;
1302 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001304 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001305 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001307 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001308 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001310 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001311 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001312 BUG_ON(!node);
1313
1314 if (!tag_get(parent, tag, offset))
1315 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Nick Piggin612d6c12006-06-23 02:03:22 -07001318 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001319 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001320 root_tag_set(root, tag);
1321
Ross Zwislerfb969902016-05-20 17:02:32 -07001322 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324EXPORT_SYMBOL(radix_tree_tag_set);
1325
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001326/**
1327 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1328 * @root: radix tree root
1329 * @iter: iterator state
1330 * @tag: tag to set
1331 */
1332void radix_tree_iter_tag_set(struct radix_tree_root *root,
1333 const struct radix_tree_iter *iter, unsigned int tag)
1334{
1335 node_tag_set(root, iter->node, tag, iter_offset(iter));
1336}
1337
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001338static void node_tag_clear(struct radix_tree_root *root,
1339 struct radix_tree_node *node,
1340 unsigned int tag, unsigned int offset)
1341{
1342 while (node) {
1343 if (!tag_get(node, tag, offset))
1344 return;
1345 tag_clear(node, tag, offset);
1346 if (any_tag_set(node, tag))
1347 return;
1348
1349 offset = node->offset;
1350 node = node->parent;
1351 }
1352
1353 /* clear the root's tag bit */
1354 if (root_tag_get(root, tag))
1355 root_tag_clear(root, tag);
1356}
1357
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001358/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * radix_tree_tag_clear - clear a tag on a radix tree node
1360 * @root: radix tree root
1361 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001362 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001364 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001365 * corresponding to @index in the radix tree. If this causes
1366 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 * next-to-leaf node, etc.
1368 *
1369 * Returns the address of the tagged item on success, else NULL. ie:
1370 * has the same return value and semantics as radix_tree_lookup().
1371 */
1372void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001373 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001375 struct radix_tree_node *node, *parent;
1376 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001377 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001379 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001380 if (index > maxindex)
1381 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Ross Zwisler00f47b52016-05-20 17:02:35 -07001383 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001385 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001386 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001387 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001390 if (node)
1391 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Ross Zwisler00f47b52016-05-20 17:02:35 -07001393 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395EXPORT_SYMBOL(radix_tree_tag_clear);
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001398 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1399 * @root: radix tree root
1400 * @iter: iterator state
1401 * @tag: tag to clear
1402 */
1403void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1404 const struct radix_tree_iter *iter, unsigned int tag)
1405{
1406 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1407}
1408
1409/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001410 * radix_tree_tag_get - get a tag on a radix tree node
1411 * @root: radix tree root
1412 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001413 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001415 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001417 * 0: tag not present or not set
1418 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001419 *
1420 * Note that the return value of this function may not be relied on, even if
1421 * the RCU lock is held, unless tag modification and node deletion are excluded
1422 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001424int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001425 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001427 struct radix_tree_node *node, *parent;
1428 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Nick Piggin612d6c12006-06-23 02:03:22 -07001430 if (!root_tag_get(root, tag))
1431 return 0;
1432
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001433 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001434 if (index > maxindex)
1435 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001436 if (node == NULL)
1437 return 0;
1438
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001439 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001440 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001441
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001442 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001443 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001444
1445 if (!node)
1446 return 0;
1447 if (!tag_get(parent, tag, offset))
1448 return 0;
1449 if (node == RADIX_TREE_RETRY)
1450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001452
1453 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Ross Zwisler21ef5332016-05-20 17:02:26 -07001457static inline void __set_iter_shift(struct radix_tree_iter *iter,
1458 unsigned int shift)
1459{
1460#ifdef CONFIG_RADIX_TREE_MULTIORDER
1461 iter->shift = shift;
1462#endif
1463}
1464
Matthew Wilcox148deab2016-12-14 15:08:49 -08001465/* Construct iter->tags bit-mask from node->tags[tag] array */
1466static void set_iter_tags(struct radix_tree_iter *iter,
1467 struct radix_tree_node *node, unsigned offset,
1468 unsigned tag)
1469{
1470 unsigned tag_long = offset / BITS_PER_LONG;
1471 unsigned tag_bit = offset % BITS_PER_LONG;
1472
1473 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1474
1475 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1476 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1477 /* Pick tags from next element */
1478 if (tag_bit)
1479 iter->tags |= node->tags[tag][tag_long + 1] <<
1480 (BITS_PER_LONG - tag_bit);
1481 /* Clip chunk size, here only BITS_PER_LONG tags */
1482 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1483 }
1484}
1485
1486#ifdef CONFIG_RADIX_TREE_MULTIORDER
1487static void **skip_siblings(struct radix_tree_node **nodep,
1488 void **slot, struct radix_tree_iter *iter)
1489{
1490 void *sib = node_to_entry(slot - 1);
1491
1492 while (iter->index < iter->next_index) {
1493 *nodep = rcu_dereference_raw(*slot);
1494 if (*nodep && *nodep != sib)
1495 return slot;
1496 slot++;
1497 iter->index = __radix_tree_iter_add(iter, 1);
1498 iter->tags >>= 1;
1499 }
1500
1501 *nodep = NULL;
1502 return NULL;
1503}
1504
1505void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
1506 unsigned flags)
1507{
1508 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1509 struct radix_tree_node *node = rcu_dereference_raw(*slot);
1510
1511 slot = skip_siblings(&node, slot, iter);
1512
1513 while (radix_tree_is_internal_node(node)) {
1514 unsigned offset;
1515 unsigned long next_index;
1516
1517 if (node == RADIX_TREE_RETRY)
1518 return slot;
1519 node = entry_to_node(node);
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001520 iter->node = node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001521 iter->shift = node->shift;
1522
1523 if (flags & RADIX_TREE_ITER_TAGGED) {
1524 offset = radix_tree_find_next_bit(node, tag, 0);
1525 if (offset == RADIX_TREE_MAP_SIZE)
1526 return NULL;
1527 slot = &node->slots[offset];
1528 iter->index = __radix_tree_iter_add(iter, offset);
1529 set_iter_tags(iter, node, offset, tag);
1530 node = rcu_dereference_raw(*slot);
1531 } else {
1532 offset = 0;
1533 slot = &node->slots[0];
1534 for (;;) {
1535 node = rcu_dereference_raw(*slot);
1536 if (node)
1537 break;
1538 slot++;
1539 offset++;
1540 if (offset == RADIX_TREE_MAP_SIZE)
1541 return NULL;
1542 }
1543 iter->index = __radix_tree_iter_add(iter, offset);
1544 }
1545 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1546 goto none;
1547 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1548 if (next_index < iter->next_index)
1549 iter->next_index = next_index;
1550 }
1551
1552 return slot;
1553 none:
1554 iter->next_index = 0;
1555 return NULL;
1556}
1557EXPORT_SYMBOL(__radix_tree_next_slot);
1558#else
1559static void **skip_siblings(struct radix_tree_node **nodep,
1560 void **slot, struct radix_tree_iter *iter)
1561{
1562 return slot;
1563}
1564#endif
1565
1566void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
1567{
1568 struct radix_tree_node *node;
1569
1570 slot++;
1571 iter->index = __radix_tree_iter_add(iter, 1);
1572 node = rcu_dereference_raw(*slot);
1573 skip_siblings(&node, slot, iter);
1574 iter->next_index = iter->index;
1575 iter->tags = 0;
1576 return NULL;
1577}
1578EXPORT_SYMBOL(radix_tree_iter_resume);
1579
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001580/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001581 * radix_tree_next_chunk - find next chunk of slots for iteration
1582 *
1583 * @root: radix tree root
1584 * @iter: iterator state
1585 * @flags: RADIX_TREE_ITER_* flags and tag index
1586 * Returns: pointer to chunk first slot, or NULL if iteration is over
1587 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001588void **radix_tree_next_chunk(const struct radix_tree_root *root,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001589 struct radix_tree_iter *iter, unsigned flags)
1590{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001591 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001592 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001593 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001594
1595 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1596 return NULL;
1597
1598 /*
1599 * Catch next_index overflow after ~0UL. iter->index never overflows
1600 * during iterating; it can be zero only at the beginning.
1601 * And we cannot overflow iter->next_index in a single step,
1602 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001603 *
1604 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001605 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001606 */
1607 index = iter->next_index;
1608 if (!index && iter->index)
1609 return NULL;
1610
Ross Zwisler21ef5332016-05-20 17:02:26 -07001611 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001612 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001613 if (index > maxindex)
1614 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001615 if (!child)
1616 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001617
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001618 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001619 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001620 iter->index = index;
1621 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001622 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001623 iter->node = NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001624 __set_iter_shift(iter, 0);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001625 return (void **)&root->rnode;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001626 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001627
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001628 do {
1629 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001630 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001631
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001632 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001633 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001634 /* Hole detected */
1635 if (flags & RADIX_TREE_ITER_CONTIG)
1636 return NULL;
1637
1638 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001639 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001640 offset + 1);
1641 else
1642 while (++offset < RADIX_TREE_MAP_SIZE) {
Ross Zwisler21ef5332016-05-20 17:02:26 -07001643 void *slot = node->slots[offset];
1644 if (is_sibling_entry(node, slot))
1645 continue;
1646 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001647 break;
1648 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001649 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001650 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001651 /* Overflow after ~0UL */
1652 if (!index)
1653 return NULL;
1654 if (offset == RADIX_TREE_MAP_SIZE)
1655 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001656 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001657 }
1658
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001659 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001660 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001661 if (child == RADIX_TREE_RETRY)
1662 break;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001663 } while (radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001664
1665 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001666 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1667 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001668 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001669 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001670
Matthew Wilcox148deab2016-12-14 15:08:49 -08001671 if (flags & RADIX_TREE_ITER_TAGGED)
1672 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001673
1674 return node->slots + offset;
1675}
1676EXPORT_SYMBOL(radix_tree_next_chunk);
1677
1678/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1680 * @root: radix tree root
1681 * @results: where the results of the lookup are placed
1682 * @first_index: start the lookup from this key
1683 * @max_items: place up to this many items at *results
1684 *
1685 * Performs an index-ascending scan of the tree for present items. Places
1686 * them at *@results and returns the number of items which were placed at
1687 * *@results.
1688 *
1689 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001690 *
1691 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1692 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001693 * an atomic snapshot of the tree at a single point in time, the
1694 * semantics of an RCU protected gang lookup are as though multiple
1695 * radix_tree_lookups have been issued in individual locks, and results
1696 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 */
1698unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001699radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 unsigned long first_index, unsigned int max_items)
1701{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001702 struct radix_tree_iter iter;
1703 void **slot;
1704 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001706 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001707 return 0;
1708
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001709 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001710 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001711 if (!results[ret])
1712 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001713 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001714 slot = radix_tree_iter_retry(&iter);
1715 continue;
1716 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001717 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return ret;
1722}
1723EXPORT_SYMBOL(radix_tree_gang_lookup);
1724
Nick Piggin47feff22008-07-25 19:45:29 -07001725/**
1726 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1727 * @root: radix tree root
1728 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001729 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001730 * @first_index: start the lookup from this key
1731 * @max_items: place up to this many items at *results
1732 *
1733 * Performs an index-ascending scan of the tree for present items. Places
1734 * their slots at *@results and returns the number of items which were
1735 * placed at *@results.
1736 *
1737 * The implementation is naive.
1738 *
1739 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1740 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1741 * protection, radix_tree_deref_slot may fail requiring a retry.
1742 */
1743unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001744radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
Hugh Dickins63286502011-08-03 16:21:18 -07001745 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001746 unsigned long first_index, unsigned int max_items)
1747{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001748 struct radix_tree_iter iter;
1749 void **slot;
1750 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001751
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001752 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001753 return 0;
1754
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001755 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1756 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001757 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001758 indices[ret] = iter.index;
1759 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001760 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001761 }
1762
1763 return ret;
1764}
1765EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/**
1768 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1769 * based on a tag
1770 * @root: radix tree root
1771 * @results: where the results of the lookup are placed
1772 * @first_index: start the lookup from this key
1773 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001774 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 *
1776 * Performs an index-ascending scan of the tree for present items which
1777 * have the tag indexed by @tag set. Places the items at *@results and
1778 * returns the number of items which were placed at *@results.
1779 */
1780unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001781radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001782 unsigned long first_index, unsigned int max_items,
1783 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001785 struct radix_tree_iter iter;
1786 void **slot;
1787 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001789 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001790 return 0;
1791
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001792 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001793 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001794 if (!results[ret])
1795 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001796 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001797 slot = radix_tree_iter_retry(&iter);
1798 continue;
1799 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001800 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 return ret;
1805}
1806EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1807
1808/**
Nick Piggin47feff22008-07-25 19:45:29 -07001809 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1810 * radix tree based on a tag
1811 * @root: radix tree root
1812 * @results: where the results of the lookup are placed
1813 * @first_index: start the lookup from this key
1814 * @max_items: place up to this many items at *results
1815 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1816 *
1817 * Performs an index-ascending scan of the tree for present items which
1818 * have the tag indexed by @tag set. Places the slots at *@results and
1819 * returns the number of slots which were placed at *@results.
1820 */
1821unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001822radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
1823 void ***results, unsigned long first_index,
1824 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001825{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001826 struct radix_tree_iter iter;
1827 void **slot;
1828 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001829
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001830 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001831 return 0;
1832
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001833 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1834 results[ret] = slot;
1835 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001836 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001837 }
1838
1839 return ret;
1840}
1841EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1842
Nick Piggin47feff22008-07-25 19:45:29 -07001843/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001844 * __radix_tree_delete_node - try to free node after clearing a slot
1845 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001846 * @node: node containing @index
Johannes Weinerea07b862017-01-06 19:21:43 -05001847 * @update_node: callback for changing leaf nodes
1848 * @private: private data to pass to @update_node
Johannes Weiner139e5612014-04-03 14:47:54 -07001849 *
1850 * After clearing the slot at @index in @node from radix tree
1851 * rooted at @root, call this function to attempt freeing the
1852 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001853 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001854void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weinerea07b862017-01-06 19:21:43 -05001855 struct radix_tree_node *node,
1856 radix_tree_update_node_t update_node,
1857 void *private)
Johannes Weiner139e5612014-04-03 14:47:54 -07001858{
Johannes Weinerea07b862017-01-06 19:21:43 -05001859 delete_node(root, node, update_node, private);
Johannes Weiner139e5612014-04-03 14:47:54 -07001860}
1861
1862/**
Johannes Weiner53c59f22014-04-03 14:47:39 -07001863 * radix_tree_delete_item - delete an item from a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 * @root: radix tree root
1865 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001866 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001868 * Remove @item at @index from the radix tree rooted at @root.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 *
Johannes Weiner53c59f22014-04-03 14:47:39 -07001870 * Returns the address of the deleted item, or NULL if it was not present
1871 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001873void *radix_tree_delete_item(struct radix_tree_root *root,
1874 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Johannes Weiner139e5612014-04-03 14:47:54 -07001876 struct radix_tree_node *node;
Matthew Wilcox57578c22016-05-20 17:01:54 -07001877 unsigned int offset;
Johannes Weiner139e5612014-04-03 14:47:54 -07001878 void **slot;
1879 void *entry;
Nick Piggind5274262006-01-08 01:01:41 -08001880 int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Johannes Weiner139e5612014-04-03 14:47:54 -07001882 entry = __radix_tree_lookup(root, index, &node, &slot);
1883 if (!entry)
1884 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Johannes Weiner139e5612014-04-03 14:47:54 -07001886 if (item && entry != item)
1887 return NULL;
1888
1889 if (!node) {
Nick Piggin612d6c12006-06-23 02:03:22 -07001890 root_tag_clear_all(root);
1891 root->rnode = NULL;
Johannes Weiner139e5612014-04-03 14:47:54 -07001892 return entry;
Nick Piggin612d6c12006-06-23 02:03:22 -07001893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Matthew Wilcox29e09672016-05-20 17:02:02 -07001895 offset = get_slot_offset(node, slot);
Johannes Weiner53c59f22014-04-03 14:47:39 -07001896
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001897 /* Clear all tags associated with the item to be deleted. */
1898 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1899 node_tag_clear(root, node, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Johannes Weiner4d693d02016-12-12 16:43:49 -08001901 __radix_tree_replace(root, node, slot, NULL, NULL, NULL);
Christoph Lameter201b6262005-09-06 15:16:46 -07001902
Johannes Weiner139e5612014-04-03 14:47:54 -07001903 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001905EXPORT_SYMBOL(radix_tree_delete_item);
1906
1907/**
1908 * radix_tree_delete - delete an item from a radix tree
1909 * @root: radix tree root
1910 * @index: index key
1911 *
1912 * Remove the item at @index from the radix tree rooted at @root.
1913 *
1914 * Returns the address of the deleted item, or NULL if it was not present.
1915 */
1916void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1917{
1918 return radix_tree_delete_item(root, index, NULL);
1919}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920EXPORT_SYMBOL(radix_tree_delete);
1921
Johannes Weinerd3798ae2016-10-04 22:02:08 +02001922void radix_tree_clear_tags(struct radix_tree_root *root,
1923 struct radix_tree_node *node,
1924 void **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001925{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001926 if (node) {
1927 unsigned int tag, offset = get_slot_offset(node, slot);
1928 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1929 node_tag_clear(root, node, tag, offset);
1930 } else {
1931 /* Clear root node tags */
1932 root->gfp_mask &= __GFP_BITS_MASK;
1933 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/**
1937 * radix_tree_tagged - test whether any items in the tree are tagged
1938 * @root: radix tree root
1939 * @tag: tag to test
1940 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001941int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Nick Piggin612d6c12006-06-23 02:03:22 -07001943 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945EXPORT_SYMBOL(radix_tree_tagged);
1946
1947static void
Johannes Weiner449dd692014-04-03 14:47:56 -07001948radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Johannes Weiner449dd692014-04-03 14:47:56 -07001950 struct radix_tree_node *node = arg;
1951
1952 memset(node, 0, sizeof(*node));
1953 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001956static __init unsigned long __maxindex(unsigned int height)
1957{
1958 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1959 int shift = RADIX_TREE_INDEX_BITS - width;
1960
1961 if (shift < 0)
1962 return ~0UL;
1963 if (shift >= BITS_PER_LONG)
1964 return 0UL;
1965 return ~0UL >> shift;
1966}
1967
1968static __init void radix_tree_init_maxnodes(void)
1969{
1970 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
1971 unsigned int i, j;
1972
1973 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
1974 height_to_maxindex[i] = __maxindex(i);
1975 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
1976 for (j = i; j > 0; j--)
1977 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
1978 }
1979}
1980
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001981static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001983 struct radix_tree_preload *rtp;
1984 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001986 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001987 rtp = &per_cpu(radix_tree_preloads, cpu);
1988 while (rtp->nr) {
1989 node = rtp->nodes;
1990 rtp->nodes = node->private_data;
1991 kmem_cache_free(radix_tree_node_cachep, node);
1992 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001993 }
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997void __init radix_tree_init(void)
1998{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01001999 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2001 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07002002 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2003 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002004 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002005 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2006 NULL, radix_tree_cpu_dead);
2007 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008}