blob: 7bf7d4e60e43b90aa15a7c1db6a8bd41561adf5b [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 */
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500584static inline bool 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{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500588 bool shrunk = false;
589
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800590 for (;;) {
591 struct radix_tree_node *node = root->rnode;
592 struct radix_tree_node *child;
593
594 if (!radix_tree_is_internal_node(node))
595 break;
596 node = entry_to_node(node);
597
598 /*
599 * The candidate node has more than one child, or its child
600 * is not at the leftmost slot, or the child is a multiorder
601 * entry, we cannot shrink.
602 */
603 if (node->count != 1)
604 break;
605 child = node->slots[0];
606 if (!child)
607 break;
608 if (!radix_tree_is_internal_node(child) && node->shift)
609 break;
610
611 if (radix_tree_is_internal_node(child))
612 entry_to_node(child)->parent = NULL;
613
614 /*
615 * We don't need rcu_assign_pointer(), since we are simply
616 * moving the node from one part of the tree to another: if it
617 * was safe to dereference the old pointer to it
618 * (node->slots[0]), it will be safe to dereference the new
619 * one (root->rnode) as far as dependent read barriers go.
620 */
621 root->rnode = child;
622
623 /*
624 * We have a dilemma here. The node's slot[0] must not be
625 * NULLed in case there are concurrent lookups expecting to
626 * find the item. However if this was a bottom-level node,
627 * then it may be subject to the slot pointer being visible
628 * to callers dereferencing it. If item corresponding to
629 * slot[0] is subsequently deleted, these callers would expect
630 * their slot to become empty sooner or later.
631 *
632 * For example, lockless pagecache will look up a slot, deref
633 * the page pointer, and if the page has 0 refcount it means it
634 * was concurrently deleted from pagecache so try the deref
635 * again. Fortunately there is already a requirement for logic
636 * to retry the entire slot lookup -- the indirect pointer
637 * problem (replacing direct root node with an indirect pointer
638 * also results in a stale slot). So tag the slot as indirect
639 * to force callers to retry.
640 */
Johannes Weiner4d693d02016-12-12 16:43:49 -0800641 node->count = 0;
642 if (!radix_tree_is_internal_node(child)) {
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800643 node->slots[0] = RADIX_TREE_RETRY;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800644 if (update_node)
645 update_node(node, private);
646 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800647
Johannes Weinerea07b862017-01-06 19:21:43 -0500648 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800649 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500650 shrunk = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800651 }
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500652
653 return shrunk;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800654}
655
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500656static bool delete_node(struct radix_tree_root *root,
Johannes Weiner4d693d02016-12-12 16:43:49 -0800657 struct radix_tree_node *node,
658 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800659{
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500660 bool deleted = false;
661
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800662 do {
663 struct radix_tree_node *parent;
664
665 if (node->count) {
666 if (node == entry_to_node(root->rnode))
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500667 deleted |= radix_tree_shrink(root, update_node,
668 private);
669 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800670 }
671
672 parent = node->parent;
673 if (parent) {
674 parent->slots[node->offset] = NULL;
675 parent->count--;
676 } else {
677 root_tag_clear_all(root);
678 root->rnode = NULL;
679 }
680
Johannes Weinerea07b862017-01-06 19:21:43 -0500681 WARN_ON_ONCE(!list_empty(&node->private_list));
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800682 radix_tree_node_free(node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500683 deleted = true;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800684
685 node = parent;
686 } while (node);
Matthew Wilcox0ac398e2017-01-28 09:56:22 -0500687
688 return deleted;
Johannes Weinerf4b109c2016-12-12 16:43:46 -0800689}
690
691/**
Johannes Weiner139e5612014-04-03 14:47:54 -0700692 * __radix_tree_create - create a slot in a radix tree
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 * @root: radix tree root
694 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700695 * @order: index occupies 2^order aligned slots
Johannes Weiner139e5612014-04-03 14:47:54 -0700696 * @nodep: returns node
697 * @slotp: returns slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
Johannes Weiner139e5612014-04-03 14:47:54 -0700699 * Create, if necessary, and return the node and slot for an item
700 * at position @index in the radix tree @root.
701 *
702 * Until there is more than one item in the tree, no nodes are
703 * allocated and @root->rnode is used as a direct slot instead of
704 * pointing to a node, in which case *@nodep will be NULL.
705 *
706 * Returns -ENOMEM, or 0 for success.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Johannes Weiner139e5612014-04-03 14:47:54 -0700708int __radix_tree_create(struct radix_tree_root *root, unsigned long index,
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700709 unsigned order, struct radix_tree_node **nodep,
710 void ***slotp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700712 struct radix_tree_node *node = NULL, *child;
713 void **slot = (void **)&root->rnode;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700714 unsigned long maxindex;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700715 unsigned int shift, offset = 0;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700716 unsigned long max = index | ((1UL << order) - 1);
717
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700718 shift = radix_tree_load_root(root, &child, &maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* Make sure the tree is high enough. */
Matthew Wilcox175542f2016-12-14 15:08:58 -0800721 if (order > 0 && max == ((1UL << order) - 1))
722 max++;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700723 if (max > maxindex) {
Matthew Wilcoxd0891262016-05-20 17:03:19 -0700724 int error = radix_tree_extend(root, max, shift);
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700725 if (error < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 return error;
Matthew Wilcox49ea6eb2016-05-20 17:02:11 -0700727 shift = error;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700728 child = root->rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700731 while (shift > order) {
Matthew Wilcoxc12e51b2016-05-20 17:03:10 -0700732 shift -= RADIX_TREE_MAP_SHIFT;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700733 if (child == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 /* Have to add a child node. */
Matthew Wilcoxe8de4342016-12-14 15:09:31 -0800735 child = radix_tree_node_alloc(root, node, shift,
736 offset, 0, 0);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700737 if (!child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return -ENOMEM;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700739 rcu_assign_pointer(*slot, node_to_entry(child));
740 if (node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 node->count++;
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700742 } else if (!radix_tree_is_internal_node(child))
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 /* Go a level down */
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700746 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700747 offset = radix_tree_descend(node, &child, index);
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700748 slot = &node->slots[offset];
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700749 }
750
Johannes Weiner139e5612014-04-03 14:47:54 -0700751 if (nodep)
752 *nodep = node;
753 if (slotp)
Matthew Wilcox89148aa2016-05-20 17:03:42 -0700754 *slotp = slot;
Johannes Weiner139e5612014-04-03 14:47:54 -0700755 return 0;
756}
757
Matthew Wilcox175542f2016-12-14 15:08:58 -0800758#ifdef CONFIG_RADIX_TREE_MULTIORDER
759/*
760 * Free any nodes below this node. The tree is presumed to not need
761 * shrinking, and any user data in the tree is presumed to not need a
762 * destructor called on it. If we need to add a destructor, we can
763 * add that functionality later. Note that we may not clear tags or
764 * slots from the tree as an RCU walker may still have a pointer into
765 * this subtree. We could replace the entries with RADIX_TREE_RETRY,
766 * but we'll still have to clear those in rcu_free.
767 */
768static void radix_tree_free_nodes(struct radix_tree_node *node)
769{
770 unsigned offset = 0;
771 struct radix_tree_node *child = entry_to_node(node);
772
773 for (;;) {
774 void *entry = child->slots[offset];
775 if (radix_tree_is_internal_node(entry) &&
776 !is_sibling_entry(child, entry)) {
777 child = entry_to_node(entry);
778 offset = 0;
779 continue;
780 }
781 offset++;
782 while (offset == RADIX_TREE_MAP_SIZE) {
783 struct radix_tree_node *old = child;
784 offset = child->offset + 1;
785 child = child->parent;
Matthew Wilcoxdd040b62017-01-24 15:18:16 -0800786 WARN_ON_ONCE(!list_empty(&old->private_list));
Matthew Wilcox175542f2016-12-14 15:08:58 -0800787 radix_tree_node_free(old);
788 if (old == entry_to_node(node))
789 return;
790 }
791 }
792}
793
794static inline int insert_entries(struct radix_tree_node *node, void **slot,
795 void *item, unsigned order, bool replace)
796{
797 struct radix_tree_node *child;
798 unsigned i, n, tag, offset, tags = 0;
799
800 if (node) {
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800801 if (order > node->shift)
802 n = 1 << (order - node->shift);
803 else
804 n = 1;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800805 offset = get_slot_offset(node, slot);
806 } else {
807 n = 1;
808 offset = 0;
809 }
810
811 if (n > 1) {
812 offset = offset & ~(n - 1);
813 slot = &node->slots[offset];
814 }
815 child = node_to_entry(slot);
816
817 for (i = 0; i < n; i++) {
818 if (slot[i]) {
819 if (replace) {
820 node->count--;
821 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
822 if (tag_get(node, tag, offset + i))
823 tags |= 1 << tag;
824 } else
825 return -EEXIST;
826 }
827 }
828
829 for (i = 0; i < n; i++) {
830 struct radix_tree_node *old = slot[i];
831 if (i) {
832 rcu_assign_pointer(slot[i], child);
833 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
834 if (tags & (1 << tag))
835 tag_clear(node, tag, offset + i);
836 } else {
837 rcu_assign_pointer(slot[i], item);
838 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
839 if (tags & (1 << tag))
840 tag_set(node, tag, offset);
841 }
842 if (radix_tree_is_internal_node(old) &&
Matthew Wilcoxe157b552016-12-14 15:09:01 -0800843 !is_sibling_entry(node, old) &&
844 (old != RADIX_TREE_RETRY))
Matthew Wilcox175542f2016-12-14 15:08:58 -0800845 radix_tree_free_nodes(old);
846 if (radix_tree_exceptional_entry(old))
847 node->exceptional--;
848 }
849 if (node) {
850 node->count += n;
851 if (radix_tree_exceptional_entry(item))
852 node->exceptional += n;
853 }
854 return n;
855}
856#else
857static inline int insert_entries(struct radix_tree_node *node, void **slot,
858 void *item, unsigned order, bool replace)
859{
860 if (*slot)
861 return -EEXIST;
862 rcu_assign_pointer(*slot, item);
863 if (node) {
864 node->count++;
865 if (radix_tree_exceptional_entry(item))
866 node->exceptional++;
867 }
868 return 1;
869}
870#endif
871
Johannes Weiner139e5612014-04-03 14:47:54 -0700872/**
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700873 * __radix_tree_insert - insert into a radix tree
Johannes Weiner139e5612014-04-03 14:47:54 -0700874 * @root: radix tree root
875 * @index: index key
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700876 * @order: key covers the 2^order indices around index
Johannes Weiner139e5612014-04-03 14:47:54 -0700877 * @item: item to insert
878 *
879 * Insert an item into the radix tree at position @index.
880 */
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700881int __radix_tree_insert(struct radix_tree_root *root, unsigned long index,
882 unsigned order, void *item)
Johannes Weiner139e5612014-04-03 14:47:54 -0700883{
884 struct radix_tree_node *node;
885 void **slot;
886 int error;
887
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700888 BUG_ON(radix_tree_is_internal_node(item));
Johannes Weiner139e5612014-04-03 14:47:54 -0700889
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700890 error = __radix_tree_create(root, index, order, &node, &slot);
Johannes Weiner139e5612014-04-03 14:47:54 -0700891 if (error)
892 return error;
Matthew Wilcox175542f2016-12-14 15:08:58 -0800893
894 error = insert_entries(node, slot, item, order, false);
895 if (error < 0)
896 return error;
Christoph Lameter201b6262005-09-06 15:16:46 -0700897
Nick Piggin612d6c12006-06-23 02:03:22 -0700898 if (node) {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700899 unsigned offset = get_slot_offset(node, slot);
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700900 BUG_ON(tag_get(node, 0, offset));
901 BUG_ON(tag_get(node, 1, offset));
902 BUG_ON(tag_get(node, 2, offset));
Nick Piggin612d6c12006-06-23 02:03:22 -0700903 } else {
Matthew Wilcox7b60e9a2016-05-20 17:02:23 -0700904 BUG_ON(root_tags_get(root));
Nick Piggin612d6c12006-06-23 02:03:22 -0700905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return 0;
908}
Matthew Wilcoxe6145232016-03-17 14:21:54 -0700909EXPORT_SYMBOL(__radix_tree_insert);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Johannes Weiner139e5612014-04-03 14:47:54 -0700911/**
912 * __radix_tree_lookup - lookup an item in a radix tree
913 * @root: radix tree root
914 * @index: index key
915 * @nodep: returns node
916 * @slotp: returns slot
917 *
918 * Lookup and return the item at position @index in the radix
919 * tree @root.
920 *
921 * Until there is more than one item in the tree, no nodes are
922 * allocated and @root->rnode is used as a direct slot instead of
923 * pointing to a node, in which case *@nodep will be NULL.
Hans Reisera4331362005-11-07 00:59:29 -0800924 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500925void *__radix_tree_lookup(const struct radix_tree_root *root,
926 unsigned long index, struct radix_tree_node **nodep,
927 void ***slotp)
Hans Reisera4331362005-11-07 00:59:29 -0800928{
Johannes Weiner139e5612014-04-03 14:47:54 -0700929 struct radix_tree_node *node, *parent;
Matthew Wilcox85829952016-05-20 17:02:20 -0700930 unsigned long maxindex;
Johannes Weiner139e5612014-04-03 14:47:54 -0700931 void **slot;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800932
Matthew Wilcox85829952016-05-20 17:02:20 -0700933 restart:
934 parent = NULL;
935 slot = (void **)&root->rnode;
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700936 radix_tree_load_root(root, &node, &maxindex);
Matthew Wilcox85829952016-05-20 17:02:20 -0700937 if (index > maxindex)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800938 return NULL;
939
Matthew Wilcoxb194d162016-05-20 17:03:30 -0700940 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox85829952016-05-20 17:02:20 -0700941 unsigned offset;
Johannes Weiner139e5612014-04-03 14:47:54 -0700942
Matthew Wilcox85829952016-05-20 17:02:20 -0700943 if (node == RADIX_TREE_RETRY)
944 goto restart;
Matthew Wilcox4dd6c092016-05-20 17:03:27 -0700945 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -0700946 offset = radix_tree_descend(parent, &node, index);
Matthew Wilcox85829952016-05-20 17:02:20 -0700947 slot = parent->slots + offset;
948 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800949
Johannes Weiner139e5612014-04-03 14:47:54 -0700950 if (nodep)
951 *nodep = parent;
952 if (slotp)
953 *slotp = slot;
954 return node;
Huang Shijieb72b71c2009-06-16 15:33:42 -0700955}
956
957/**
958 * radix_tree_lookup_slot - lookup a slot in a radix tree
959 * @root: radix tree root
960 * @index: index key
961 *
962 * Returns: the slot corresponding to the position @index in the
963 * radix tree @root. This is useful for update-if-exists operations.
964 *
965 * This function can be called under rcu_read_lock iff the slot is not
966 * modified by radix_tree_replace_slot, otherwise it must be called
967 * exclusive from other writers. Any dereference of the slot must be done
968 * using radix_tree_deref_slot.
969 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500970void **radix_tree_lookup_slot(const struct radix_tree_root *root,
971 unsigned long index)
Huang Shijieb72b71c2009-06-16 15:33:42 -0700972{
Johannes Weiner139e5612014-04-03 14:47:54 -0700973 void **slot;
974
975 if (!__radix_tree_lookup(root, index, NULL, &slot))
976 return NULL;
977 return slot;
Hans Reisera4331362005-11-07 00:59:29 -0800978}
979EXPORT_SYMBOL(radix_tree_lookup_slot);
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/**
982 * radix_tree_lookup - perform lookup operation on a radix tree
983 * @root: radix tree root
984 * @index: index key
985 *
986 * Lookup the item at the position @index in the radix tree @root.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800987 *
988 * This function can be called under rcu_read_lock, however the caller
989 * must manage lifetimes of leaf nodes (eg. RCU may also be used to free
990 * them safely). No RCU barriers are required to access or modify the
991 * returned item, however.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
Matthew Wilcox35534c82016-12-19 17:43:19 -0500993void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Johannes Weiner139e5612014-04-03 14:47:54 -0700995 return __radix_tree_lookup(root, index, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997EXPORT_SYMBOL(radix_tree_lookup);
998
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -0800999static inline int slot_count(struct radix_tree_node *node,
1000 void **slot)
1001{
1002 int n = 1;
1003#ifdef CONFIG_RADIX_TREE_MULTIORDER
1004 void *ptr = node_to_entry(slot);
1005 unsigned offset = get_slot_offset(node, slot);
1006 int i;
1007
1008 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1009 if (node->slots[offset + i] != ptr)
1010 break;
1011 n++;
1012 }
1013#endif
1014 return n;
1015}
1016
Johannes Weiner6d75f362016-12-12 16:43:43 -08001017static void replace_slot(struct radix_tree_root *root,
1018 struct radix_tree_node *node,
1019 void **slot, void *item,
1020 bool warn_typeswitch)
1021{
1022 void *old = rcu_dereference_raw(*slot);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001023 int count, exceptional;
Johannes Weiner6d75f362016-12-12 16:43:43 -08001024
1025 WARN_ON_ONCE(radix_tree_is_internal_node(item));
Johannes Weiner6d75f362016-12-12 16:43:43 -08001026
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001027 count = !!item - !!old;
Johannes Weiner6d75f362016-12-12 16:43:43 -08001028 exceptional = !!radix_tree_exceptional_entry(item) -
1029 !!radix_tree_exceptional_entry(old);
1030
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001031 WARN_ON_ONCE(warn_typeswitch && (count || exceptional));
Johannes Weiner6d75f362016-12-12 16:43:43 -08001032
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001033 if (node) {
1034 node->count += count;
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001035 if (exceptional) {
1036 exceptional *= slot_count(node, slot);
1037 node->exceptional += exceptional;
1038 }
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001039 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001040
1041 rcu_assign_pointer(*slot, item);
1042}
1043
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001044static inline void delete_sibling_entries(struct radix_tree_node *node,
1045 void **slot)
1046{
1047#ifdef CONFIG_RADIX_TREE_MULTIORDER
1048 bool exceptional = radix_tree_exceptional_entry(*slot);
1049 void *ptr = node_to_entry(slot);
1050 unsigned offset = get_slot_offset(node, slot);
1051 int i;
1052
1053 for (i = 1; offset + i < RADIX_TREE_MAP_SIZE; i++) {
1054 if (node->slots[offset + i] != ptr)
1055 break;
1056 node->slots[offset + i] = NULL;
1057 node->count--;
1058 if (exceptional)
1059 node->exceptional--;
1060 }
1061#endif
1062}
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064/**
Johannes Weinerf7942432016-12-12 16:43:41 -08001065 * __radix_tree_replace - replace item in a slot
Johannes Weiner4d693d02016-12-12 16:43:49 -08001066 * @root: radix tree root
1067 * @node: pointer to tree node
1068 * @slot: pointer to slot in @node
1069 * @item: new item to store in the slot.
1070 * @update_node: callback for changing leaf nodes
1071 * @private: private data to pass to @update_node
Johannes Weinerf7942432016-12-12 16:43:41 -08001072 *
1073 * For use with __radix_tree_lookup(). Caller must hold tree write locked
1074 * across slot lookup and replacement.
1075 */
1076void __radix_tree_replace(struct radix_tree_root *root,
1077 struct radix_tree_node *node,
Johannes Weiner4d693d02016-12-12 16:43:49 -08001078 void **slot, void *item,
1079 radix_tree_update_node_t update_node, void *private)
Johannes Weinerf7942432016-12-12 16:43:41 -08001080{
Matthew Wilcoxa90eb3a2016-12-14 15:09:07 -08001081 if (!item)
1082 delete_sibling_entries(node, slot);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001083 /*
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001084 * This function supports replacing exceptional entries and
1085 * deleting entries, but that needs accounting against the
1086 * node unless the slot is root->rnode.
Johannes Weiner6d75f362016-12-12 16:43:43 -08001087 */
1088 replace_slot(root, node, slot, item,
1089 !node && slot != (void **)&root->rnode);
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001090
Johannes Weiner4d693d02016-12-12 16:43:49 -08001091 if (!node)
1092 return;
1093
1094 if (update_node)
1095 update_node(node, private);
1096
1097 delete_node(root, node, update_node, private);
Johannes Weiner6d75f362016-12-12 16:43:43 -08001098}
Johannes Weinerf7942432016-12-12 16:43:41 -08001099
Johannes Weiner6d75f362016-12-12 16:43:43 -08001100/**
1101 * radix_tree_replace_slot - replace item in a slot
1102 * @root: radix tree root
1103 * @slot: pointer to slot
1104 * @item: new item to store in the slot.
1105 *
1106 * For use with radix_tree_lookup_slot(), radix_tree_gang_lookup_slot(),
1107 * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked
1108 * across slot lookup and replacement.
1109 *
1110 * NOTE: This cannot be used to switch between non-entries (empty slots),
1111 * regular entries, and exceptional entries, as that requires accounting
Johannes Weinerf4b109c2016-12-12 16:43:46 -08001112 * inside the radix tree node. When switching from one type of entry or
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001113 * deleting, use __radix_tree_lookup() and __radix_tree_replace() or
1114 * radix_tree_iter_replace().
Johannes Weiner6d75f362016-12-12 16:43:43 -08001115 */
1116void radix_tree_replace_slot(struct radix_tree_root *root,
1117 void **slot, void *item)
1118{
1119 replace_slot(root, NULL, slot, item, true);
Johannes Weinerf7942432016-12-12 16:43:41 -08001120}
1121
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001122/**
1123 * radix_tree_iter_replace - replace item in a slot
1124 * @root: radix tree root
1125 * @slot: pointer to slot
1126 * @item: new item to store in the slot.
1127 *
1128 * For use with radix_tree_split() and radix_tree_for_each_slot().
1129 * Caller must hold tree write locked across split and replacement.
1130 */
1131void radix_tree_iter_replace(struct radix_tree_root *root,
1132 const struct radix_tree_iter *iter, void **slot, void *item)
1133{
1134 __radix_tree_replace(root, iter->node, slot, item, NULL, NULL);
1135}
1136
Matthew Wilcox175542f2016-12-14 15:08:58 -08001137#ifdef CONFIG_RADIX_TREE_MULTIORDER
1138/**
1139 * radix_tree_join - replace multiple entries with one multiorder entry
1140 * @root: radix tree root
1141 * @index: an index inside the new entry
1142 * @order: order of the new entry
1143 * @item: new entry
1144 *
1145 * Call this function to replace several entries with one larger entry.
1146 * The existing entries are presumed to not need freeing as a result of
1147 * this call.
1148 *
1149 * The replacement entry will have all the tags set on it that were set
1150 * on any of the entries it is replacing.
1151 */
1152int radix_tree_join(struct radix_tree_root *root, unsigned long index,
1153 unsigned order, void *item)
1154{
1155 struct radix_tree_node *node;
1156 void **slot;
1157 int error;
1158
1159 BUG_ON(radix_tree_is_internal_node(item));
1160
1161 error = __radix_tree_create(root, index, order, &node, &slot);
1162 if (!error)
1163 error = insert_entries(node, slot, item, order, true);
1164 if (error > 0)
1165 error = 0;
1166
1167 return error;
1168}
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001169
1170/**
1171 * radix_tree_split - Split an entry into smaller entries
1172 * @root: radix tree root
1173 * @index: An index within the large entry
1174 * @order: Order of new entries
1175 *
1176 * Call this function as the first step in replacing a multiorder entry
1177 * with several entries of lower order. After this function returns,
1178 * loop over the relevant portion of the tree using radix_tree_for_each_slot()
1179 * and call radix_tree_iter_replace() to set up each new entry.
1180 *
1181 * The tags from this entry are replicated to all the new entries.
1182 *
1183 * The radix tree should be locked against modification during the entire
1184 * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which
1185 * should prompt RCU walkers to restart the lookup from the root.
1186 */
1187int radix_tree_split(struct radix_tree_root *root, unsigned long index,
1188 unsigned order)
1189{
1190 struct radix_tree_node *parent, *node, *child;
1191 void **slot;
1192 unsigned int offset, end;
1193 unsigned n, tag, tags = 0;
1194
1195 if (!__radix_tree_lookup(root, index, &parent, &slot))
1196 return -ENOENT;
1197 if (!parent)
1198 return -ENOENT;
1199
1200 offset = get_slot_offset(parent, slot);
1201
1202 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1203 if (tag_get(parent, tag, offset))
1204 tags |= 1 << tag;
1205
1206 for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) {
1207 if (!is_sibling_entry(parent, parent->slots[end]))
1208 break;
1209 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1210 if (tags & (1 << tag))
1211 tag_set(parent, tag, end);
1212 /* rcu_assign_pointer ensures tags are set before RETRY */
1213 rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY);
1214 }
1215 rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY);
1216 parent->exceptional -= (end - offset);
1217
1218 if (order == parent->shift)
1219 return 0;
1220 if (order > parent->shift) {
1221 while (offset < end)
1222 offset += insert_entries(parent, &parent->slots[offset],
1223 RADIX_TREE_RETRY, order, true);
1224 return 0;
1225 }
1226
1227 node = parent;
1228
1229 for (;;) {
1230 if (node->shift > order) {
Matthew Wilcoxe8de4342016-12-14 15:09:31 -08001231 child = radix_tree_node_alloc(root, node,
1232 node->shift - RADIX_TREE_MAP_SHIFT,
1233 offset, 0, 0);
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001234 if (!child)
1235 goto nomem;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001236 if (node != parent) {
1237 node->count++;
1238 node->slots[offset] = node_to_entry(child);
1239 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1240 if (tags & (1 << tag))
1241 tag_set(node, tag, offset);
1242 }
1243
1244 node = child;
1245 offset = 0;
1246 continue;
1247 }
1248
1249 n = insert_entries(node, &node->slots[offset],
1250 RADIX_TREE_RETRY, order, false);
1251 BUG_ON(n > RADIX_TREE_MAP_SIZE);
1252
1253 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1254 if (tags & (1 << tag))
1255 tag_set(node, tag, offset);
1256 offset += n;
1257
1258 while (offset == RADIX_TREE_MAP_SIZE) {
1259 if (node == parent)
1260 break;
1261 offset = node->offset;
1262 child = node;
1263 node = node->parent;
1264 rcu_assign_pointer(node->slots[offset],
1265 node_to_entry(child));
1266 offset++;
1267 }
1268 if ((node == parent) && (offset == end))
1269 return 0;
1270 }
1271
1272 nomem:
1273 /* Shouldn't happen; did user forget to preload? */
1274 /* TODO: free all the allocated nodes */
1275 WARN_ON(1);
1276 return -ENOMEM;
1277}
Matthew Wilcox175542f2016-12-14 15:08:58 -08001278#endif
1279
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001280static void node_tag_set(struct radix_tree_root *root,
1281 struct radix_tree_node *node,
1282 unsigned int tag, unsigned int offset)
1283{
1284 while (node) {
1285 if (tag_get(node, tag, offset))
1286 return;
1287 tag_set(node, tag, offset);
1288 offset = node->offset;
1289 node = node->parent;
1290 }
1291
1292 if (!root_tag_get(root, tag))
1293 root_tag_set(root, tag);
1294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296/**
1297 * radix_tree_tag_set - set a tag on a radix tree node
1298 * @root: radix tree root
1299 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001300 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001302 * Set the search tag (which must be < RADIX_TREE_MAX_TAGS)
1303 * corresponding to @index in the radix tree. From
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 * the root all the way down to the leaf node.
1305 *
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001306 * Returns the address of the tagged item. Setting a tag on a not-present
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 * item is a bug.
1308 */
1309void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001310 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
Ross Zwislerfb969902016-05-20 17:02:32 -07001312 struct radix_tree_node *node, *parent;
1313 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001315 radix_tree_load_root(root, &node, &maxindex);
Ross Zwislerfb969902016-05-20 17:02:32 -07001316 BUG_ON(index > maxindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001318 while (radix_tree_is_internal_node(node)) {
Ross Zwislerfb969902016-05-20 17:02:32 -07001319 unsigned offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001321 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001322 offset = radix_tree_descend(parent, &node, index);
Ross Zwislerfb969902016-05-20 17:02:32 -07001323 BUG_ON(!node);
1324
1325 if (!tag_get(parent, tag, offset))
1326 tag_set(parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
Nick Piggin612d6c12006-06-23 02:03:22 -07001329 /* set the root's tag bit */
Ross Zwislerfb969902016-05-20 17:02:32 -07001330 if (!root_tag_get(root, tag))
Nick Piggin612d6c12006-06-23 02:03:22 -07001331 root_tag_set(root, tag);
1332
Ross Zwislerfb969902016-05-20 17:02:32 -07001333 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335EXPORT_SYMBOL(radix_tree_tag_set);
1336
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001337/**
1338 * radix_tree_iter_tag_set - set a tag on the current iterator entry
1339 * @root: radix tree root
1340 * @iter: iterator state
1341 * @tag: tag to set
1342 */
1343void radix_tree_iter_tag_set(struct radix_tree_root *root,
1344 const struct radix_tree_iter *iter, unsigned int tag)
1345{
1346 node_tag_set(root, iter->node, tag, iter_offset(iter));
1347}
1348
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001349static void node_tag_clear(struct radix_tree_root *root,
1350 struct radix_tree_node *node,
1351 unsigned int tag, unsigned int offset)
1352{
1353 while (node) {
1354 if (!tag_get(node, tag, offset))
1355 return;
1356 tag_clear(node, tag, offset);
1357 if (any_tag_set(node, tag))
1358 return;
1359
1360 offset = node->offset;
1361 node = node->parent;
1362 }
1363
1364 /* clear the root's tag bit */
1365 if (root_tag_get(root, tag))
1366 root_tag_clear(root, tag);
1367}
1368
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001369/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 * radix_tree_tag_clear - clear a tag on a radix tree node
1371 * @root: radix tree root
1372 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001373 * @tag: tag index
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 *
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001375 * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS)
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001376 * corresponding to @index in the radix tree. If this causes
1377 * the leaf node to have no tags set then clear the tag in the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 * next-to-leaf node, etc.
1379 *
1380 * Returns the address of the tagged item on success, else NULL. ie:
1381 * has the same return value and semantics as radix_tree_lookup().
1382 */
1383void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001384 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385{
Ross Zwisler00f47b52016-05-20 17:02:35 -07001386 struct radix_tree_node *node, *parent;
1387 unsigned long maxindex;
Hugh Dickinse2bdb932012-01-12 17:20:41 -08001388 int uninitialized_var(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001390 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler00f47b52016-05-20 17:02:35 -07001391 if (index > maxindex)
1392 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Ross Zwisler00f47b52016-05-20 17:02:35 -07001394 parent = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001396 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001397 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001398 offset = radix_tree_descend(parent, &node, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001401 if (node)
1402 node_tag_clear(root, parent, tag, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Ross Zwisler00f47b52016-05-20 17:02:35 -07001404 return node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405}
1406EXPORT_SYMBOL(radix_tree_tag_clear);
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408/**
Matthew Wilcox30b888b2017-01-28 09:55:20 -05001409 * radix_tree_iter_tag_clear - clear a tag on the current iterator entry
1410 * @root: radix tree root
1411 * @iter: iterator state
1412 * @tag: tag to clear
1413 */
1414void radix_tree_iter_tag_clear(struct radix_tree_root *root,
1415 const struct radix_tree_iter *iter, unsigned int tag)
1416{
1417 node_tag_clear(root, iter->node, tag, iter_offset(iter));
1418}
1419
1420/**
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001421 * radix_tree_tag_get - get a tag on a radix tree node
1422 * @root: radix tree root
1423 * @index: index key
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001424 * @tag: tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 *
Marcelo Tosatti32605a12005-09-06 15:16:48 -07001426 * Return values:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 *
Nick Piggin612d6c12006-06-23 02:03:22 -07001428 * 0: tag not present or not set
1429 * 1: tag set
David Howellsce826532010-04-06 22:36:20 +01001430 *
1431 * Note that the return value of this function may not be relied on, even if
1432 * the RCU lock is held, unless tag modification and node deletion are excluded
1433 * from concurrency.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001435int radix_tree_tag_get(const struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001436 unsigned long index, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Ross Zwisler4589ba62016-05-20 17:02:38 -07001438 struct radix_tree_node *node, *parent;
1439 unsigned long maxindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Nick Piggin612d6c12006-06-23 02:03:22 -07001441 if (!root_tag_get(root, tag))
1442 return 0;
1443
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001444 radix_tree_load_root(root, &node, &maxindex);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001445 if (index > maxindex)
1446 return 0;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001447 if (node == NULL)
1448 return 0;
1449
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001450 while (radix_tree_is_internal_node(node)) {
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001451 unsigned offset;
Ross Zwisler4589ba62016-05-20 17:02:38 -07001452
Matthew Wilcox4dd6c092016-05-20 17:03:27 -07001453 parent = entry_to_node(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001454 offset = radix_tree_descend(parent, &node, index);
Ross Zwisler4589ba62016-05-20 17:02:38 -07001455
1456 if (!node)
1457 return 0;
1458 if (!tag_get(parent, tag, offset))
1459 return 0;
1460 if (node == RADIX_TREE_RETRY)
1461 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Ross Zwisler4589ba62016-05-20 17:02:38 -07001463
1464 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466EXPORT_SYMBOL(radix_tree_tag_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Ross Zwisler21ef5332016-05-20 17:02:26 -07001468static inline void __set_iter_shift(struct radix_tree_iter *iter,
1469 unsigned int shift)
1470{
1471#ifdef CONFIG_RADIX_TREE_MULTIORDER
1472 iter->shift = shift;
1473#endif
1474}
1475
Matthew Wilcox148deab2016-12-14 15:08:49 -08001476/* Construct iter->tags bit-mask from node->tags[tag] array */
1477static void set_iter_tags(struct radix_tree_iter *iter,
1478 struct radix_tree_node *node, unsigned offset,
1479 unsigned tag)
1480{
1481 unsigned tag_long = offset / BITS_PER_LONG;
1482 unsigned tag_bit = offset % BITS_PER_LONG;
1483
1484 iter->tags = node->tags[tag][tag_long] >> tag_bit;
1485
1486 /* This never happens if RADIX_TREE_TAG_LONGS == 1 */
1487 if (tag_long < RADIX_TREE_TAG_LONGS - 1) {
1488 /* Pick tags from next element */
1489 if (tag_bit)
1490 iter->tags |= node->tags[tag][tag_long + 1] <<
1491 (BITS_PER_LONG - tag_bit);
1492 /* Clip chunk size, here only BITS_PER_LONG tags */
1493 iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG);
1494 }
1495}
1496
1497#ifdef CONFIG_RADIX_TREE_MULTIORDER
1498static void **skip_siblings(struct radix_tree_node **nodep,
1499 void **slot, struct radix_tree_iter *iter)
1500{
1501 void *sib = node_to_entry(slot - 1);
1502
1503 while (iter->index < iter->next_index) {
1504 *nodep = rcu_dereference_raw(*slot);
1505 if (*nodep && *nodep != sib)
1506 return slot;
1507 slot++;
1508 iter->index = __radix_tree_iter_add(iter, 1);
1509 iter->tags >>= 1;
1510 }
1511
1512 *nodep = NULL;
1513 return NULL;
1514}
1515
1516void ** __radix_tree_next_slot(void **slot, struct radix_tree_iter *iter,
1517 unsigned flags)
1518{
1519 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
1520 struct radix_tree_node *node = rcu_dereference_raw(*slot);
1521
1522 slot = skip_siblings(&node, slot, iter);
1523
1524 while (radix_tree_is_internal_node(node)) {
1525 unsigned offset;
1526 unsigned long next_index;
1527
1528 if (node == RADIX_TREE_RETRY)
1529 return slot;
1530 node = entry_to_node(node);
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001531 iter->node = node;
Matthew Wilcox148deab2016-12-14 15:08:49 -08001532 iter->shift = node->shift;
1533
1534 if (flags & RADIX_TREE_ITER_TAGGED) {
1535 offset = radix_tree_find_next_bit(node, tag, 0);
1536 if (offset == RADIX_TREE_MAP_SIZE)
1537 return NULL;
1538 slot = &node->slots[offset];
1539 iter->index = __radix_tree_iter_add(iter, offset);
1540 set_iter_tags(iter, node, offset, tag);
1541 node = rcu_dereference_raw(*slot);
1542 } else {
1543 offset = 0;
1544 slot = &node->slots[0];
1545 for (;;) {
1546 node = rcu_dereference_raw(*slot);
1547 if (node)
1548 break;
1549 slot++;
1550 offset++;
1551 if (offset == RADIX_TREE_MAP_SIZE)
1552 return NULL;
1553 }
1554 iter->index = __radix_tree_iter_add(iter, offset);
1555 }
1556 if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0))
1557 goto none;
1558 next_index = (iter->index | shift_maxindex(iter->shift)) + 1;
1559 if (next_index < iter->next_index)
1560 iter->next_index = next_index;
1561 }
1562
1563 return slot;
1564 none:
1565 iter->next_index = 0;
1566 return NULL;
1567}
1568EXPORT_SYMBOL(__radix_tree_next_slot);
1569#else
1570static void **skip_siblings(struct radix_tree_node **nodep,
1571 void **slot, struct radix_tree_iter *iter)
1572{
1573 return slot;
1574}
1575#endif
1576
1577void **radix_tree_iter_resume(void **slot, struct radix_tree_iter *iter)
1578{
1579 struct radix_tree_node *node;
1580
1581 slot++;
1582 iter->index = __radix_tree_iter_add(iter, 1);
1583 node = rcu_dereference_raw(*slot);
1584 skip_siblings(&node, slot, iter);
1585 iter->next_index = iter->index;
1586 iter->tags = 0;
1587 return NULL;
1588}
1589EXPORT_SYMBOL(radix_tree_iter_resume);
1590
Fengguang Wu6df8ba42007-10-16 01:24:33 -07001591/**
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001592 * radix_tree_next_chunk - find next chunk of slots for iteration
1593 *
1594 * @root: radix tree root
1595 * @iter: iterator state
1596 * @flags: RADIX_TREE_ITER_* flags and tag index
1597 * Returns: pointer to chunk first slot, or NULL if iteration is over
1598 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001599void **radix_tree_next_chunk(const struct radix_tree_root *root,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001600 struct radix_tree_iter *iter, unsigned flags)
1601{
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001602 unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001603 struct radix_tree_node *node, *child;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001604 unsigned long index, offset, maxindex;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001605
1606 if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag))
1607 return NULL;
1608
1609 /*
1610 * Catch next_index overflow after ~0UL. iter->index never overflows
1611 * during iterating; it can be zero only at the beginning.
1612 * And we cannot overflow iter->next_index in a single step,
1613 * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG.
Konstantin Khlebnikovfffaee32012-06-05 21:36:33 +04001614 *
1615 * This condition also used by radix_tree_next_slot() to stop
Matthew Wilcox91b9677c2016-12-14 15:08:31 -08001616 * contiguous iterating, and forbid switching to the next chunk.
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001617 */
1618 index = iter->next_index;
1619 if (!index && iter->index)
1620 return NULL;
1621
Ross Zwisler21ef5332016-05-20 17:02:26 -07001622 restart:
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001623 radix_tree_load_root(root, &child, &maxindex);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001624 if (index > maxindex)
1625 return NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001626 if (!child)
1627 return NULL;
Ross Zwisler21ef5332016-05-20 17:02:26 -07001628
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001629 if (!radix_tree_is_internal_node(child)) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001630 /* Single-slot tree */
Ross Zwisler21ef5332016-05-20 17:02:26 -07001631 iter->index = index;
1632 iter->next_index = maxindex + 1;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001633 iter->tags = 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001634 iter->node = NULL;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001635 __set_iter_shift(iter, 0);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001636 return (void **)&root->rnode;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001637 }
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001638
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001639 do {
1640 node = entry_to_node(child);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001641 offset = radix_tree_descend(node, &child, index);
Ross Zwisler21ef5332016-05-20 17:02:26 -07001642
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001643 if ((flags & RADIX_TREE_ITER_TAGGED) ?
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001644 !tag_get(node, tag, offset) : !child) {
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001645 /* Hole detected */
1646 if (flags & RADIX_TREE_ITER_CONTIG)
1647 return NULL;
1648
1649 if (flags & RADIX_TREE_ITER_TAGGED)
Matthew Wilcoxbc412fc2016-12-14 15:08:40 -08001650 offset = radix_tree_find_next_bit(node, tag,
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001651 offset + 1);
1652 else
1653 while (++offset < RADIX_TREE_MAP_SIZE) {
Ross Zwisler21ef5332016-05-20 17:02:26 -07001654 void *slot = node->slots[offset];
1655 if (is_sibling_entry(node, slot))
1656 continue;
1657 if (slot)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001658 break;
1659 }
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001660 index &= ~node_maxindex(node);
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001661 index += offset << node->shift;
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001662 /* Overflow after ~0UL */
1663 if (!index)
1664 return NULL;
1665 if (offset == RADIX_TREE_MAP_SIZE)
1666 goto restart;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001667 child = rcu_dereference_raw(node->slots[offset]);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001668 }
1669
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001670 if (!child)
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001671 goto restart;
Matthew Wilcoxe157b552016-12-14 15:09:01 -08001672 if (child == RADIX_TREE_RETRY)
1673 break;
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001674 } while (radix_tree_is_internal_node(child));
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001675
1676 /* Update the iterator state */
Matthew Wilcox8c1244d2016-05-20 17:03:36 -07001677 iter->index = (index &~ node_maxindex(node)) | (offset << node->shift);
1678 iter->next_index = (index | node_maxindex(node)) + 1;
Matthew Wilcox268f42d2016-12-14 15:08:55 -08001679 iter->node = node;
Matthew Wilcox9e85d812016-05-20 17:03:48 -07001680 __set_iter_shift(iter, node->shift);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001681
Matthew Wilcox148deab2016-12-14 15:08:49 -08001682 if (flags & RADIX_TREE_ITER_TAGGED)
1683 set_iter_tags(iter, node, offset, tag);
Konstantin Khlebnikov78c1d782012-03-28 14:42:53 -07001684
1685 return node->slots + offset;
1686}
1687EXPORT_SYMBOL(radix_tree_next_chunk);
1688
1689/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 * radix_tree_gang_lookup - perform multiple lookup on a radix tree
1691 * @root: radix tree root
1692 * @results: where the results of the lookup are placed
1693 * @first_index: start the lookup from this key
1694 * @max_items: place up to this many items at *results
1695 *
1696 * Performs an index-ascending scan of the tree for present items. Places
1697 * them at *@results and returns the number of items which were placed at
1698 * *@results.
1699 *
1700 * The implementation is naive.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001701 *
1702 * Like radix_tree_lookup, radix_tree_gang_lookup may be called under
1703 * rcu_read_lock. In this case, rather than the returned results being
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07001704 * an atomic snapshot of the tree at a single point in time, the
1705 * semantics of an RCU protected gang lookup are as though multiple
1706 * radix_tree_lookups have been issued in individual locks, and results
1707 * stored in 'results'.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 */
1709unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001710radix_tree_gang_lookup(const struct radix_tree_root *root, void **results,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 unsigned long first_index, unsigned int max_items)
1712{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001713 struct radix_tree_iter iter;
1714 void **slot;
1715 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001717 if (unlikely(!max_items))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001718 return 0;
1719
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001720 radix_tree_for_each_slot(slot, root, &iter, first_index) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001721 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001722 if (!results[ret])
1723 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001724 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001725 slot = radix_tree_iter_retry(&iter);
1726 continue;
1727 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001728 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return ret;
1733}
1734EXPORT_SYMBOL(radix_tree_gang_lookup);
1735
Nick Piggin47feff22008-07-25 19:45:29 -07001736/**
1737 * radix_tree_gang_lookup_slot - perform multiple slot lookup on radix tree
1738 * @root: radix tree root
1739 * @results: where the results of the lookup are placed
Hugh Dickins63286502011-08-03 16:21:18 -07001740 * @indices: where their indices should be placed (but usually NULL)
Nick Piggin47feff22008-07-25 19:45:29 -07001741 * @first_index: start the lookup from this key
1742 * @max_items: place up to this many items at *results
1743 *
1744 * Performs an index-ascending scan of the tree for present items. Places
1745 * their slots at *@results and returns the number of items which were
1746 * placed at *@results.
1747 *
1748 * The implementation is naive.
1749 *
1750 * Like radix_tree_gang_lookup as far as RCU and locking goes. Slots must
1751 * be dereferenced with radix_tree_deref_slot, and if using only RCU
1752 * protection, radix_tree_deref_slot may fail requiring a retry.
1753 */
1754unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001755radix_tree_gang_lookup_slot(const struct radix_tree_root *root,
Hugh Dickins63286502011-08-03 16:21:18 -07001756 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -07001757 unsigned long first_index, unsigned int max_items)
1758{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001759 struct radix_tree_iter iter;
1760 void **slot;
1761 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001762
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001763 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001764 return 0;
1765
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001766 radix_tree_for_each_slot(slot, root, &iter, first_index) {
1767 results[ret] = slot;
Hugh Dickins63286502011-08-03 16:21:18 -07001768 if (indices)
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001769 indices[ret] = iter.index;
1770 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001771 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001772 }
1773
1774 return ret;
1775}
1776EXPORT_SYMBOL(radix_tree_gang_lookup_slot);
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/**
1779 * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree
1780 * based on a tag
1781 * @root: radix tree root
1782 * @results: where the results of the lookup are placed
1783 * @first_index: start the lookup from this key
1784 * @max_items: place up to this many items at *results
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001785 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 *
1787 * Performs an index-ascending scan of the tree for present items which
1788 * have the tag indexed by @tag set. Places the items at *@results and
1789 * returns the number of items which were placed at *@results.
1790 */
1791unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001792radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -08001793 unsigned long first_index, unsigned int max_items,
1794 unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001796 struct radix_tree_iter iter;
1797 void **slot;
1798 unsigned int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001800 if (unlikely(!max_items))
Nick Piggin612d6c12006-06-23 02:03:22 -07001801 return 0;
1802
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001803 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001804 results[ret] = rcu_dereference_raw(*slot);
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001805 if (!results[ret])
1806 continue;
Matthew Wilcoxb194d162016-05-20 17:03:30 -07001807 if (radix_tree_is_internal_node(results[ret])) {
Matthew Wilcox46437f92016-02-02 16:57:52 -08001808 slot = radix_tree_iter_retry(&iter);
1809 continue;
1810 }
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001811 if (++ret == max_items)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 }
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return ret;
1816}
1817EXPORT_SYMBOL(radix_tree_gang_lookup_tag);
1818
1819/**
Nick Piggin47feff22008-07-25 19:45:29 -07001820 * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a
1821 * radix tree based on a tag
1822 * @root: radix tree root
1823 * @results: where the results of the lookup are placed
1824 * @first_index: start the lookup from this key
1825 * @max_items: place up to this many items at *results
1826 * @tag: the tag index (< RADIX_TREE_MAX_TAGS)
1827 *
1828 * Performs an index-ascending scan of the tree for present items which
1829 * have the tag indexed by @tag set. Places the slots at *@results and
1830 * returns the number of slots which were placed at *@results.
1831 */
1832unsigned int
Matthew Wilcox35534c82016-12-19 17:43:19 -05001833radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root,
1834 void ***results, unsigned long first_index,
1835 unsigned int max_items, unsigned int tag)
Nick Piggin47feff22008-07-25 19:45:29 -07001836{
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001837 struct radix_tree_iter iter;
1838 void **slot;
1839 unsigned int ret = 0;
Nick Piggin47feff22008-07-25 19:45:29 -07001840
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001841 if (unlikely(!max_items))
Nick Piggin47feff22008-07-25 19:45:29 -07001842 return 0;
1843
Konstantin Khlebnikovcebbd292012-03-28 14:42:53 -07001844 radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) {
1845 results[ret] = slot;
1846 if (++ret == max_items)
Nick Piggin47feff22008-07-25 19:45:29 -07001847 break;
Nick Piggin47feff22008-07-25 19:45:29 -07001848 }
1849
1850 return ret;
1851}
1852EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot);
1853
Nick Piggin47feff22008-07-25 19:45:29 -07001854/**
Johannes Weiner139e5612014-04-03 14:47:54 -07001855 * __radix_tree_delete_node - try to free node after clearing a slot
1856 * @root: radix tree root
Johannes Weiner139e5612014-04-03 14:47:54 -07001857 * @node: node containing @index
Johannes Weinerea07b862017-01-06 19:21:43 -05001858 * @update_node: callback for changing leaf nodes
1859 * @private: private data to pass to @update_node
Johannes Weiner139e5612014-04-03 14:47:54 -07001860 *
1861 * After clearing the slot at @index in @node from radix tree
1862 * rooted at @root, call this function to attempt freeing the
1863 * node and shrinking the tree.
Johannes Weiner139e5612014-04-03 14:47:54 -07001864 */
Johannes Weiner14b46872016-12-12 16:43:52 -08001865void __radix_tree_delete_node(struct radix_tree_root *root,
Johannes Weinerea07b862017-01-06 19:21:43 -05001866 struct radix_tree_node *node,
1867 radix_tree_update_node_t update_node,
1868 void *private)
Johannes Weiner139e5612014-04-03 14:47:54 -07001869{
Johannes Weinerea07b862017-01-06 19:21:43 -05001870 delete_node(root, node, update_node, private);
Johannes Weiner139e5612014-04-03 14:47:54 -07001871}
1872
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001873static bool __radix_tree_delete(struct radix_tree_root *root,
1874 struct radix_tree_node *node, void **slot)
1875{
1876 unsigned offset = get_slot_offset(node, slot);
1877 int tag;
1878
1879 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1880 node_tag_clear(root, node, tag, offset);
1881
1882 replace_slot(root, node, slot, NULL, true);
1883 return node && delete_node(root, node, NULL, NULL);
1884}
1885
Johannes Weiner139e5612014-04-03 14:47:54 -07001886/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001887 * radix_tree_iter_delete - delete the entry at this iterator position
1888 * @root: radix tree root
1889 * @iter: iterator state
1890 * @slot: pointer to slot
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001892 * Delete the entry at the position currently pointed to by the iterator.
1893 * This may result in the current node being freed; if it is, the iterator
1894 * is advanced so that it will not reference the freed memory. This
1895 * function may be called without any locking if there are no other threads
1896 * which can access this tree.
1897 */
1898void radix_tree_iter_delete(struct radix_tree_root *root,
1899 struct radix_tree_iter *iter, void **slot)
1900{
1901 if (__radix_tree_delete(root, iter->node, slot))
1902 iter->index = iter->next_index;
1903}
1904
1905/**
1906 * radix_tree_delete_item - delete an item from a radix tree
1907 * @root: radix tree root
1908 * @index: index key
1909 * @item: expected item
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001911 * Remove @item at @index from the radix tree rooted at @root.
1912 *
1913 * Return: the deleted entry, or %NULL if it was not present
1914 * or the entry at the given @index was not @item.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
Johannes Weiner53c59f22014-04-03 14:47:39 -07001916void *radix_tree_delete_item(struct radix_tree_root *root,
1917 unsigned long index, void *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Johannes Weiner139e5612014-04-03 14:47:54 -07001919 struct radix_tree_node *node;
Johannes Weiner139e5612014-04-03 14:47:54 -07001920 void **slot;
1921 void *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Johannes Weiner139e5612014-04-03 14:47:54 -07001923 entry = __radix_tree_lookup(root, index, &node, &slot);
1924 if (!entry)
1925 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Johannes Weiner139e5612014-04-03 14:47:54 -07001927 if (item && entry != item)
1928 return NULL;
1929
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001930 __radix_tree_delete(root, node, slot);
Christoph Lameter201b6262005-09-06 15:16:46 -07001931
Johannes Weiner139e5612014-04-03 14:47:54 -07001932 return entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
Johannes Weiner53c59f22014-04-03 14:47:39 -07001934EXPORT_SYMBOL(radix_tree_delete_item);
1935
1936/**
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001937 * radix_tree_delete - delete an entry from a radix tree
1938 * @root: radix tree root
1939 * @index: index key
Johannes Weiner53c59f22014-04-03 14:47:39 -07001940 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001941 * Remove the entry at @index from the radix tree rooted at @root.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001942 *
Matthew Wilcox0ac398e2017-01-28 09:56:22 -05001943 * Return: The deleted entry, or %NULL if it was not present.
Johannes Weiner53c59f22014-04-03 14:47:39 -07001944 */
1945void *radix_tree_delete(struct radix_tree_root *root, unsigned long index)
1946{
1947 return radix_tree_delete_item(root, index, NULL);
1948}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949EXPORT_SYMBOL(radix_tree_delete);
1950
Johannes Weinerd3798ae2016-10-04 22:02:08 +02001951void radix_tree_clear_tags(struct radix_tree_root *root,
1952 struct radix_tree_node *node,
1953 void **slot)
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001954{
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001955 if (node) {
1956 unsigned int tag, offset = get_slot_offset(node, slot);
1957 for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++)
1958 node_tag_clear(root, node, tag, offset);
1959 } else {
1960 /* Clear root node tags */
1961 root->gfp_mask &= __GFP_BITS_MASK;
1962 }
Matthew Wilcoxd604c322016-05-20 17:03:45 -07001963}
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965/**
1966 * radix_tree_tagged - test whether any items in the tree are tagged
1967 * @root: radix tree root
1968 * @tag: tag to test
1969 */
Matthew Wilcox35534c82016-12-19 17:43:19 -05001970int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Nick Piggin612d6c12006-06-23 02:03:22 -07001972 return root_tag_get(root, tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974EXPORT_SYMBOL(radix_tree_tagged);
1975
1976static void
Johannes Weiner449dd692014-04-03 14:47:56 -07001977radix_tree_node_ctor(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
Johannes Weiner449dd692014-04-03 14:47:56 -07001979 struct radix_tree_node *node = arg;
1980
1981 memset(node, 0, sizeof(*node));
1982 INIT_LIST_HEAD(&node->private_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983}
1984
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07001985static __init unsigned long __maxindex(unsigned int height)
1986{
1987 unsigned int width = height * RADIX_TREE_MAP_SHIFT;
1988 int shift = RADIX_TREE_INDEX_BITS - width;
1989
1990 if (shift < 0)
1991 return ~0UL;
1992 if (shift >= BITS_PER_LONG)
1993 return 0UL;
1994 return ~0UL >> shift;
1995}
1996
1997static __init void radix_tree_init_maxnodes(void)
1998{
1999 unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1];
2000 unsigned int i, j;
2001
2002 for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++)
2003 height_to_maxindex[i] = __maxindex(i);
2004 for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) {
2005 for (j = i; j > 0; j--)
2006 height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1;
2007 }
2008}
2009
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002010static int radix_tree_cpu_dead(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002012 struct radix_tree_preload *rtp;
2013 struct radix_tree_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002015 /* Free per-cpu pool of preloaded nodes */
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002016 rtp = &per_cpu(radix_tree_preloads, cpu);
2017 while (rtp->nr) {
2018 node = rtp->nodes;
2019 rtp->nodes = node->private_data;
2020 kmem_cache_free(radix_tree_node_cachep, node);
2021 rtp->nr--;
Matthew Wilcox2fcd9002016-05-20 17:03:04 -07002022 }
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002023 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026void __init radix_tree_init(void)
2027{
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002028 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 radix_tree_node_cachep = kmem_cache_create("radix_tree_node",
2030 sizeof(struct radix_tree_node), 0,
Christoph Lameter488514d2008-04-28 02:12:05 -07002031 SLAB_PANIC | SLAB_RECLAIM_ACCOUNT,
2032 radix_tree_node_ctor);
Kirill A. Shutemovc78c66d2016-07-26 15:26:02 -07002033 radix_tree_init_maxnodes();
Sebastian Andrzej Siewiord544abd2016-11-03 15:50:01 +01002034 ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead",
2035 NULL, radix_tree_cpu_dead);
2036 WARN_ON(ret < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}