Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001 Momchil Velikov |
| 3 | * Portions Copyright (C) 2001 Christoph Hellwig |
Christoph Lameter | cde5353 | 2008-07-04 09:59:22 -0700 | [diff] [blame] | 4 | * Copyright (C) 2005 SGI, Christoph Lameter |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 5 | * Copyright (C) 2006 Nick Piggin |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 6 | * Copyright (C) 2012 Konstantin Khlebnikov |
Matthew Wilcox | 6b053b8 | 2016-05-20 17:02:58 -0700 | [diff] [blame] | 7 | * Copyright (C) 2016 Intel, Matthew Wilcox |
| 8 | * Copyright (C) 2016 Intel, Ross Zwisler |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 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 Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 25 | #include <linux/bitmap.h> |
| 26 | #include <linux/bitops.h> |
Matthew Wilcox | 460488c | 2017-11-28 15:16:24 -0500 | [diff] [blame] | 27 | #include <linux/bug.h> |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 28 | #include <linux/cpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/errno.h> |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 30 | #include <linux/export.h> |
| 31 | #include <linux/idr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/init.h> |
| 33 | #include <linux/kernel.h> |
Catalin Marinas | ce80b06 | 2014-06-06 14:38:18 -0700 | [diff] [blame] | 34 | #include <linux/kmemleak.h> |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 35 | #include <linux/percpu.h> |
Frederic Weisbecker | 92cf211 | 2015-05-12 16:41:46 +0200 | [diff] [blame] | 36 | #include <linux/preempt.h> /* in_interrupt() */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 37 | #include <linux/radix-tree.h> |
| 38 | #include <linux/rcupdate.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/string.h> |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 41 | #include <linux/xarray.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 44 | /* Number of nodes in fully populated tree of given height */ |
| 45 | static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly; |
| 46 | |
Jeff Moyer | 26fb158 | 2007-10-16 01:24:49 -0700 | [diff] [blame] | 47 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * Radix tree node cache. |
| 49 | */ |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 50 | struct kmem_cache *radix_tree_node_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | /* |
Nick Piggin | 5536805 | 2012-05-29 15:07:34 -0700 | [diff] [blame] | 53 | * The radix tree is variable-height, so an insert operation not only has |
| 54 | * to build the branch to its corresponding item, it also has to build the |
| 55 | * branch to existing items if the size has to be increased (by |
| 56 | * radix_tree_extend). |
| 57 | * |
| 58 | * The worst case is a zero height tree with just a single item at index 0, |
| 59 | * and then inserting an item at index ULONG_MAX. This requires 2 new branches |
| 60 | * of RADIX_TREE_MAX_PATH size to be created, with only the root node shared. |
| 61 | * Hence: |
| 62 | */ |
| 63 | #define RADIX_TREE_PRELOAD_SIZE (RADIX_TREE_MAX_PATH * 2 - 1) |
| 64 | |
| 65 | /* |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 66 | * The IDR does not have to be as high as the radix tree since it uses |
| 67 | * signed integers, not unsigned longs. |
| 68 | */ |
| 69 | #define IDR_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(int) - 1) |
| 70 | #define IDR_MAX_PATH (DIV_ROUND_UP(IDR_INDEX_BITS, \ |
| 71 | RADIX_TREE_MAP_SHIFT)) |
| 72 | #define IDR_PRELOAD_SIZE (IDR_MAX_PATH * 2 - 1) |
| 73 | |
| 74 | /* |
Matthew Wilcox | 7ad3d4d | 2016-12-16 11:55:56 -0500 | [diff] [blame] | 75 | * The IDA is even shorter since it uses a bitmap at the last level. |
| 76 | */ |
| 77 | #define IDA_INDEX_BITS (8 * sizeof(int) - 1 - ilog2(IDA_BITMAP_BITS)) |
| 78 | #define IDA_MAX_PATH (DIV_ROUND_UP(IDA_INDEX_BITS, \ |
| 79 | RADIX_TREE_MAP_SHIFT)) |
| 80 | #define IDA_PRELOAD_SIZE (IDA_MAX_PATH * 2 - 1) |
| 81 | |
| 82 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | * Per-cpu pool of preloaded nodes |
| 84 | */ |
| 85 | struct radix_tree_preload { |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 86 | unsigned nr; |
Matthew Wilcox | 1293d5c | 2017-01-16 16:41:29 -0500 | [diff] [blame] | 87 | /* nodes->parent points to next preallocated node */ |
Kirill A. Shutemov | 9d2a8da | 2015-06-25 15:02:19 -0700 | [diff] [blame] | 88 | struct radix_tree_node *nodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | }; |
Harvey Harrison | 8cef7d5 | 2009-01-06 14:40:50 -0800 | [diff] [blame] | 90 | static DEFINE_PER_CPU(struct radix_tree_preload, radix_tree_preloads) = { 0, }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 92 | static inline struct radix_tree_node *entry_to_node(void *ptr) |
| 93 | { |
| 94 | return (void *)((unsigned long)ptr & ~RADIX_TREE_INTERNAL_NODE); |
| 95 | } |
| 96 | |
Matthew Wilcox | a4db4dc | 2016-05-20 17:03:24 -0700 | [diff] [blame] | 97 | static inline void *node_to_entry(void *ptr) |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 98 | { |
Matthew Wilcox | 30ff46cc | 2016-05-20 17:03:22 -0700 | [diff] [blame] | 99 | return (void *)((unsigned long)ptr | RADIX_TREE_INTERNAL_NODE); |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 102 | #define RADIX_TREE_RETRY XA_RETRY_ENTRY |
Matthew Wilcox | db050f2 | 2016-05-20 17:01:57 -0700 | [diff] [blame] | 103 | |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 104 | static inline unsigned long |
| 105 | get_slot_offset(const struct radix_tree_node *parent, void __rcu **slot) |
Matthew Wilcox | db050f2 | 2016-05-20 17:01:57 -0700 | [diff] [blame] | 106 | { |
Matthew Wilcox | 76f070b | 2018-08-18 07:05:50 -0400 | [diff] [blame] | 107 | return parent ? slot - parent->slots : 0; |
Matthew Wilcox | db050f2 | 2016-05-20 17:01:57 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 110 | static unsigned int radix_tree_descend(const struct radix_tree_node *parent, |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 111 | struct radix_tree_node **nodep, unsigned long index) |
Matthew Wilcox | db050f2 | 2016-05-20 17:01:57 -0700 | [diff] [blame] | 112 | { |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 113 | unsigned int offset = (index >> parent->shift) & RADIX_TREE_MAP_MASK; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 114 | void __rcu **entry = rcu_dereference_raw(parent->slots[offset]); |
Matthew Wilcox | db050f2 | 2016-05-20 17:01:57 -0700 | [diff] [blame] | 115 | |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 116 | if (xa_is_sibling(entry)) { |
| 117 | offset = xa_to_sibling(entry); |
| 118 | entry = rcu_dereference_raw(parent->slots[offset]); |
Matthew Wilcox | db050f2 | 2016-05-20 17:01:57 -0700 | [diff] [blame] | 119 | } |
Matthew Wilcox | db050f2 | 2016-05-20 17:01:57 -0700 | [diff] [blame] | 120 | |
| 121 | *nodep = (void *)entry; |
| 122 | return offset; |
| 123 | } |
| 124 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 125 | static inline gfp_t root_gfp_mask(const struct radix_tree_root *root) |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 126 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 127 | return root->xa_flags & (__GFP_BITS_MASK & ~GFP_ZONEMASK); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 130 | static inline void tag_set(struct radix_tree_node *node, unsigned int tag, |
| 131 | int offset) |
| 132 | { |
| 133 | __set_bit(offset, node->tags[tag]); |
| 134 | } |
| 135 | |
| 136 | static inline void tag_clear(struct radix_tree_node *node, unsigned int tag, |
| 137 | int offset) |
| 138 | { |
| 139 | __clear_bit(offset, node->tags[tag]); |
| 140 | } |
| 141 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 142 | static inline int tag_get(const struct radix_tree_node *node, unsigned int tag, |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 143 | int offset) |
| 144 | { |
| 145 | return test_bit(offset, node->tags[tag]); |
| 146 | } |
| 147 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 148 | static inline void root_tag_set(struct radix_tree_root *root, unsigned tag) |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 149 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 150 | root->xa_flags |= (__force gfp_t)(1 << (tag + ROOT_TAG_SHIFT)); |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 153 | static inline void root_tag_clear(struct radix_tree_root *root, unsigned tag) |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 154 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 155 | root->xa_flags &= (__force gfp_t)~(1 << (tag + ROOT_TAG_SHIFT)); |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static inline void root_tag_clear_all(struct radix_tree_root *root) |
| 159 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 160 | root->xa_flags &= (__force gfp_t)((1 << ROOT_TAG_SHIFT) - 1); |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 163 | static inline int root_tag_get(const struct radix_tree_root *root, unsigned tag) |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 164 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 165 | return (__force int)root->xa_flags & (1 << (tag + ROOT_TAG_SHIFT)); |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 168 | static inline unsigned root_tags_get(const struct radix_tree_root *root) |
Matthew Wilcox | 7b60e9a | 2016-05-20 17:02:23 -0700 | [diff] [blame] | 169 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 170 | return (__force unsigned)root->xa_flags >> ROOT_TAG_SHIFT; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static inline bool is_idr(const struct radix_tree_root *root) |
| 174 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 175 | return !!(root->xa_flags & ROOT_IS_IDR); |
Matthew Wilcox | 7b60e9a | 2016-05-20 17:02:23 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 178 | /* |
| 179 | * Returns 1 if any slot in the node has this tag set. |
| 180 | * Otherwise returns 0. |
| 181 | */ |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 182 | static inline int any_tag_set(const struct radix_tree_node *node, |
| 183 | unsigned int tag) |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 184 | { |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 185 | unsigned idx; |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 186 | for (idx = 0; idx < RADIX_TREE_TAG_LONGS; idx++) { |
| 187 | if (node->tags[tag][idx]) |
| 188 | return 1; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 192 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 193 | static inline void all_tag_set(struct radix_tree_node *node, unsigned int tag) |
| 194 | { |
| 195 | bitmap_fill(node->tags[tag], RADIX_TREE_MAP_SIZE); |
| 196 | } |
| 197 | |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 198 | /** |
| 199 | * radix_tree_find_next_bit - find the next set bit in a memory region |
| 200 | * |
| 201 | * @addr: The address to base the search on |
| 202 | * @size: The bitmap size in bits |
| 203 | * @offset: The bitnumber to start searching at |
| 204 | * |
| 205 | * Unrollable variant of find_next_bit() for constant size arrays. |
| 206 | * Tail bits starting from size to roundup(size, BITS_PER_LONG) must be zero. |
| 207 | * Returns next bit offset, or size if nothing found. |
| 208 | */ |
| 209 | static __always_inline unsigned long |
Matthew Wilcox | bc412fc | 2016-12-14 15:08:40 -0800 | [diff] [blame] | 210 | radix_tree_find_next_bit(struct radix_tree_node *node, unsigned int tag, |
| 211 | unsigned long offset) |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 212 | { |
Matthew Wilcox | bc412fc | 2016-12-14 15:08:40 -0800 | [diff] [blame] | 213 | const unsigned long *addr = node->tags[tag]; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 214 | |
Matthew Wilcox | bc412fc | 2016-12-14 15:08:40 -0800 | [diff] [blame] | 215 | if (offset < RADIX_TREE_MAP_SIZE) { |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 216 | unsigned long tmp; |
| 217 | |
| 218 | addr += offset / BITS_PER_LONG; |
| 219 | tmp = *addr >> (offset % BITS_PER_LONG); |
| 220 | if (tmp) |
| 221 | return __ffs(tmp) + offset; |
| 222 | offset = (offset + BITS_PER_LONG) & ~(BITS_PER_LONG - 1); |
Matthew Wilcox | bc412fc | 2016-12-14 15:08:40 -0800 | [diff] [blame] | 223 | while (offset < RADIX_TREE_MAP_SIZE) { |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 224 | tmp = *++addr; |
| 225 | if (tmp) |
| 226 | return __ffs(tmp) + offset; |
| 227 | offset += BITS_PER_LONG; |
| 228 | } |
| 229 | } |
Matthew Wilcox | bc412fc | 2016-12-14 15:08:40 -0800 | [diff] [blame] | 230 | return RADIX_TREE_MAP_SIZE; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 233 | static unsigned int iter_offset(const struct radix_tree_iter *iter) |
| 234 | { |
| 235 | return (iter->index >> iter_shift(iter)) & RADIX_TREE_MAP_MASK; |
| 236 | } |
| 237 | |
Matthew Wilcox | 218ed75 | 2016-12-14 15:08:43 -0800 | [diff] [blame] | 238 | /* |
| 239 | * The maximum index which can be stored in a radix tree |
| 240 | */ |
| 241 | static inline unsigned long shift_maxindex(unsigned int shift) |
| 242 | { |
| 243 | return (RADIX_TREE_MAP_SIZE << shift) - 1; |
| 244 | } |
| 245 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 246 | static inline unsigned long node_maxindex(const struct radix_tree_node *node) |
Matthew Wilcox | 218ed75 | 2016-12-14 15:08:43 -0800 | [diff] [blame] | 247 | { |
| 248 | return shift_maxindex(node->shift); |
| 249 | } |
| 250 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 251 | static unsigned long next_index(unsigned long index, |
| 252 | const struct radix_tree_node *node, |
| 253 | unsigned long offset) |
| 254 | { |
| 255 | return (index & ~node_maxindex(node)) + (offset << node->shift); |
| 256 | } |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | /* |
| 259 | * This assumes that the caller has performed appropriate preallocation, and |
| 260 | * that the caller has pinned this thread of control to the current CPU. |
| 261 | */ |
| 262 | static struct radix_tree_node * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 263 | radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent, |
Matthew Wilcox | d58275b | 2017-01-16 17:10:21 -0500 | [diff] [blame] | 264 | struct radix_tree_root *root, |
Matthew Wilcox | e8de434 | 2016-12-14 15:09:31 -0800 | [diff] [blame] | 265 | unsigned int shift, unsigned int offset, |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 266 | unsigned int count, unsigned int nr_values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | { |
Nick Piggin | e2848a0 | 2008-02-04 22:29:10 -0800 | [diff] [blame] | 268 | struct radix_tree_node *ret = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 270 | /* |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 271 | * Preload code isn't irq safe and it doesn't make sense to use |
| 272 | * preloading during an interrupt anyway as all the allocations have |
| 273 | * to be atomic. So just do normal allocation when in interrupt. |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 274 | */ |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 275 | if (!gfpflags_allow_blocking(gfp_mask) && !in_interrupt()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | struct radix_tree_preload *rtp; |
| 277 | |
Nick Piggin | e2848a0 | 2008-02-04 22:29:10 -0800 | [diff] [blame] | 278 | /* |
Vladimir Davydov | 58e698a | 2016-03-17 14:18:36 -0700 | [diff] [blame] | 279 | * Even if the caller has preloaded, try to allocate from the |
Vladimir Davydov | 05eb6e7 | 2016-08-02 14:03:01 -0700 | [diff] [blame] | 280 | * cache first for the new node to get accounted to the memory |
| 281 | * cgroup. |
Vladimir Davydov | 58e698a | 2016-03-17 14:18:36 -0700 | [diff] [blame] | 282 | */ |
| 283 | ret = kmem_cache_alloc(radix_tree_node_cachep, |
Vladimir Davydov | 05eb6e7 | 2016-08-02 14:03:01 -0700 | [diff] [blame] | 284 | gfp_mask | __GFP_NOWARN); |
Vladimir Davydov | 58e698a | 2016-03-17 14:18:36 -0700 | [diff] [blame] | 285 | if (ret) |
| 286 | goto out; |
| 287 | |
| 288 | /* |
Nick Piggin | e2848a0 | 2008-02-04 22:29:10 -0800 | [diff] [blame] | 289 | * Provided the caller has preloaded here, we will always |
| 290 | * succeed in getting a node here (and never reach |
| 291 | * kmem_cache_alloc) |
| 292 | */ |
Christoph Lameter | 7c8e018 | 2014-06-04 16:07:56 -0700 | [diff] [blame] | 293 | rtp = this_cpu_ptr(&radix_tree_preloads); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | if (rtp->nr) { |
Kirill A. Shutemov | 9d2a8da | 2015-06-25 15:02:19 -0700 | [diff] [blame] | 295 | ret = rtp->nodes; |
Matthew Wilcox | 1293d5c | 2017-01-16 16:41:29 -0500 | [diff] [blame] | 296 | rtp->nodes = ret->parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | rtp->nr--; |
| 298 | } |
Catalin Marinas | ce80b06 | 2014-06-06 14:38:18 -0700 | [diff] [blame] | 299 | /* |
| 300 | * Update the allocation stack trace as this is more useful |
| 301 | * for debugging. |
| 302 | */ |
| 303 | kmemleak_update_trace(ret); |
Vladimir Davydov | 58e698a | 2016-03-17 14:18:36 -0700 | [diff] [blame] | 304 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | } |
Vladimir Davydov | 05eb6e7 | 2016-08-02 14:03:01 -0700 | [diff] [blame] | 306 | ret = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
Vladimir Davydov | 58e698a | 2016-03-17 14:18:36 -0700 | [diff] [blame] | 307 | out: |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 308 | BUG_ON(radix_tree_is_internal_node(ret)); |
Matthew Wilcox | e8de434 | 2016-12-14 15:09:31 -0800 | [diff] [blame] | 309 | if (ret) { |
Matthew Wilcox | e8de434 | 2016-12-14 15:09:31 -0800 | [diff] [blame] | 310 | ret->shift = shift; |
| 311 | ret->offset = offset; |
| 312 | ret->count = count; |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 313 | ret->nr_values = nr_values; |
Matthew Wilcox | d58275b | 2017-01-16 17:10:21 -0500 | [diff] [blame] | 314 | ret->parent = parent; |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 315 | ret->array = root; |
Matthew Wilcox | e8de434 | 2016-12-14 15:09:31 -0800 | [diff] [blame] | 316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return ret; |
| 318 | } |
| 319 | |
Matthew Wilcox | 58d6ea3 | 2017-11-10 15:15:08 -0500 | [diff] [blame] | 320 | void radix_tree_node_rcu_free(struct rcu_head *head) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 321 | { |
| 322 | struct radix_tree_node *node = |
| 323 | container_of(head, struct radix_tree_node, rcu_head); |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 324 | |
| 325 | /* |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 326 | * Must only free zeroed nodes into the slab. We can be left with |
| 327 | * non-NULL entries by radix_tree_free_nodes, so clear the entries |
| 328 | * and tags here. |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 329 | */ |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 330 | memset(node->slots, 0, sizeof(node->slots)); |
| 331 | memset(node->tags, 0, sizeof(node->tags)); |
Matthew Wilcox | 91d9c05 | 2016-12-14 15:08:34 -0800 | [diff] [blame] | 332 | INIT_LIST_HEAD(&node->private_list); |
Nick Piggin | 643b52b | 2008-06-12 15:21:52 -0700 | [diff] [blame] | 333 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 334 | kmem_cache_free(radix_tree_node_cachep, node); |
| 335 | } |
| 336 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | static inline void |
| 338 | radix_tree_node_free(struct radix_tree_node *node) |
| 339 | { |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 340 | call_rcu(&node->rcu_head, radix_tree_node_rcu_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Load up this CPU's radix_tree_node buffer with sufficient objects to |
| 345 | * ensure that the addition of a single element in the tree cannot fail. On |
| 346 | * success, return zero, with preemption disabled. On error, return -ENOMEM |
| 347 | * with preemption not disabled. |
David Howells | b34df79 | 2009-11-19 18:11:14 +0000 | [diff] [blame] | 348 | * |
| 349 | * To make use of this facility, the radix tree must be initialised without |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 350 | * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | */ |
Eric Dumazet | bc9ae22 | 2017-09-08 16:15:54 -0700 | [diff] [blame] | 352 | static __must_check int __radix_tree_preload(gfp_t gfp_mask, unsigned nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct radix_tree_preload *rtp; |
| 355 | struct radix_tree_node *node; |
| 356 | int ret = -ENOMEM; |
| 357 | |
Vladimir Davydov | 05eb6e7 | 2016-08-02 14:03:01 -0700 | [diff] [blame] | 358 | /* |
| 359 | * Nodes preloaded by one cgroup can be be used by another cgroup, so |
| 360 | * they should never be accounted to any particular memory cgroup. |
| 361 | */ |
| 362 | gfp_mask &= ~__GFP_ACCOUNT; |
| 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | preempt_disable(); |
Christoph Lameter | 7c8e018 | 2014-06-04 16:07:56 -0700 | [diff] [blame] | 365 | rtp = this_cpu_ptr(&radix_tree_preloads); |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 366 | while (rtp->nr < nr) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | preempt_enable(); |
Christoph Lameter | 488514d | 2008-04-28 02:12:05 -0700 | [diff] [blame] | 368 | node = kmem_cache_alloc(radix_tree_node_cachep, gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | if (node == NULL) |
| 370 | goto out; |
| 371 | preempt_disable(); |
Christoph Lameter | 7c8e018 | 2014-06-04 16:07:56 -0700 | [diff] [blame] | 372 | rtp = this_cpu_ptr(&radix_tree_preloads); |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 373 | if (rtp->nr < nr) { |
Matthew Wilcox | 1293d5c | 2017-01-16 16:41:29 -0500 | [diff] [blame] | 374 | node->parent = rtp->nodes; |
Kirill A. Shutemov | 9d2a8da | 2015-06-25 15:02:19 -0700 | [diff] [blame] | 375 | rtp->nodes = node; |
| 376 | rtp->nr++; |
| 377 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | kmem_cache_free(radix_tree_node_cachep, node); |
Kirill A. Shutemov | 9d2a8da | 2015-06-25 15:02:19 -0700 | [diff] [blame] | 379 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | } |
| 381 | ret = 0; |
| 382 | out: |
| 383 | return ret; |
| 384 | } |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 385 | |
| 386 | /* |
| 387 | * Load up this CPU's radix_tree_node buffer with sufficient objects to |
| 388 | * ensure that the addition of a single element in the tree cannot fail. On |
| 389 | * success, return zero, with preemption disabled. On error, return -ENOMEM |
| 390 | * with preemption not disabled. |
| 391 | * |
| 392 | * To make use of this facility, the radix tree must be initialised without |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 393 | * __GFP_DIRECT_RECLAIM being passed to INIT_RADIX_TREE(). |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 394 | */ |
| 395 | int radix_tree_preload(gfp_t gfp_mask) |
| 396 | { |
| 397 | /* Warn on non-sensical use... */ |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 398 | WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask)); |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 399 | return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE); |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 400 | } |
David Chinner | d7f0923 | 2007-07-14 16:05:04 +1000 | [diff] [blame] | 401 | EXPORT_SYMBOL(radix_tree_preload); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Nick Piggin | 6e954b9 | 2006-01-08 01:01:40 -0800 | [diff] [blame] | 403 | /* |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 404 | * The same as above function, except we don't guarantee preloading happens. |
| 405 | * We do it, if we decide it helps. On success, return zero with preemption |
| 406 | * disabled. On error, return -ENOMEM with preemption not disabled. |
| 407 | */ |
| 408 | int radix_tree_maybe_preload(gfp_t gfp_mask) |
| 409 | { |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame] | 410 | if (gfpflags_allow_blocking(gfp_mask)) |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 411 | return __radix_tree_preload(gfp_mask, RADIX_TREE_PRELOAD_SIZE); |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 412 | /* Preloading doesn't help anything with this gfp mask, skip it */ |
| 413 | preempt_disable(); |
| 414 | return 0; |
| 415 | } |
| 416 | EXPORT_SYMBOL(radix_tree_maybe_preload); |
| 417 | |
Matthew Wilcox | 2791653 | 2016-12-14 15:09:04 -0800 | [diff] [blame] | 418 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
| 419 | /* |
| 420 | * Preload with enough objects to ensure that we can split a single entry |
| 421 | * of order @old_order into many entries of size @new_order |
| 422 | */ |
| 423 | int radix_tree_split_preload(unsigned int old_order, unsigned int new_order, |
| 424 | gfp_t gfp_mask) |
| 425 | { |
| 426 | unsigned top = 1 << (old_order % RADIX_TREE_MAP_SHIFT); |
| 427 | unsigned layers = (old_order / RADIX_TREE_MAP_SHIFT) - |
| 428 | (new_order / RADIX_TREE_MAP_SHIFT); |
| 429 | unsigned nr = 0; |
| 430 | |
| 431 | WARN_ON_ONCE(!gfpflags_allow_blocking(gfp_mask)); |
| 432 | BUG_ON(new_order >= old_order); |
| 433 | |
| 434 | while (layers--) |
| 435 | nr = nr * RADIX_TREE_MAP_SIZE + 1; |
| 436 | return __radix_tree_preload(gfp_mask, top * nr); |
| 437 | } |
| 438 | #endif |
| 439 | |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 440 | /* |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 441 | * The same as function above, but preload number of nodes required to insert |
| 442 | * (1 << order) continuous naturally-aligned elements. |
| 443 | */ |
| 444 | int radix_tree_maybe_preload_order(gfp_t gfp_mask, int order) |
| 445 | { |
| 446 | unsigned long nr_subtrees; |
| 447 | int nr_nodes, subtree_height; |
| 448 | |
| 449 | /* Preloading doesn't help anything with this gfp mask, skip it */ |
| 450 | if (!gfpflags_allow_blocking(gfp_mask)) { |
| 451 | preempt_disable(); |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Calculate number and height of fully populated subtrees it takes to |
| 457 | * store (1 << order) elements. |
| 458 | */ |
| 459 | nr_subtrees = 1 << order; |
| 460 | for (subtree_height = 0; nr_subtrees > RADIX_TREE_MAP_SIZE; |
| 461 | subtree_height++) |
| 462 | nr_subtrees >>= RADIX_TREE_MAP_SHIFT; |
| 463 | |
| 464 | /* |
| 465 | * The worst case is zero height tree with a single item at index 0 and |
| 466 | * then inserting items starting at ULONG_MAX - (1 << order). |
| 467 | * |
| 468 | * This requires RADIX_TREE_MAX_PATH nodes to build branch from root to |
| 469 | * 0-index item. |
| 470 | */ |
| 471 | nr_nodes = RADIX_TREE_MAX_PATH; |
| 472 | |
| 473 | /* Plus branch to fully populated subtrees. */ |
| 474 | nr_nodes += RADIX_TREE_MAX_PATH - subtree_height; |
| 475 | |
| 476 | /* Root node is shared. */ |
| 477 | nr_nodes--; |
| 478 | |
| 479 | /* Plus nodes required to build subtrees. */ |
| 480 | nr_nodes += nr_subtrees * height_to_maxnodes[subtree_height]; |
| 481 | |
| 482 | return __radix_tree_preload(gfp_mask, nr_nodes); |
| 483 | } |
| 484 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 485 | static unsigned radix_tree_load_root(const struct radix_tree_root *root, |
Matthew Wilcox | 1456a43 | 2016-05-20 17:02:08 -0700 | [diff] [blame] | 486 | struct radix_tree_node **nodep, unsigned long *maxindex) |
| 487 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 488 | struct radix_tree_node *node = rcu_dereference_raw(root->xa_head); |
Matthew Wilcox | 1456a43 | 2016-05-20 17:02:08 -0700 | [diff] [blame] | 489 | |
| 490 | *nodep = node; |
| 491 | |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 492 | if (likely(radix_tree_is_internal_node(node))) { |
Matthew Wilcox | 4dd6c09 | 2016-05-20 17:03:27 -0700 | [diff] [blame] | 493 | node = entry_to_node(node); |
Matthew Wilcox | 1456a43 | 2016-05-20 17:02:08 -0700 | [diff] [blame] | 494 | *maxindex = node_maxindex(node); |
Matthew Wilcox | c12e51b | 2016-05-20 17:03:10 -0700 | [diff] [blame] | 495 | return node->shift + RADIX_TREE_MAP_SHIFT; |
Matthew Wilcox | 1456a43 | 2016-05-20 17:02:08 -0700 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | *maxindex = 0; |
| 499 | return 0; |
| 500 | } |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | /* |
| 503 | * Extend a radix tree so it can store key @index. |
| 504 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 505 | static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp, |
Matthew Wilcox | d089126 | 2016-05-20 17:03:19 -0700 | [diff] [blame] | 506 | unsigned long index, unsigned int shift) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | { |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 508 | void *entry; |
Matthew Wilcox | d089126 | 2016-05-20 17:03:19 -0700 | [diff] [blame] | 509 | unsigned int maxshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | int tag; |
| 511 | |
Matthew Wilcox | d089126 | 2016-05-20 17:03:19 -0700 | [diff] [blame] | 512 | /* Figure out what the shift should be. */ |
| 513 | maxshift = shift; |
| 514 | while (index > shift_maxindex(maxshift)) |
| 515 | maxshift += RADIX_TREE_MAP_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 517 | entry = rcu_dereference_raw(root->xa_head); |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 518 | if (!entry && (!is_idr(root) || root_tag_get(root, IDR_FREE))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | do { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 522 | struct radix_tree_node *node = radix_tree_node_alloc(gfp, NULL, |
Matthew Wilcox | d58275b | 2017-01-16 17:10:21 -0500 | [diff] [blame] | 523 | root, shift, 0, 1, 0); |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 524 | if (!node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | return -ENOMEM; |
| 526 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 527 | if (is_idr(root)) { |
| 528 | all_tag_set(node, IDR_FREE); |
| 529 | if (!root_tag_get(root, IDR_FREE)) { |
| 530 | tag_clear(node, IDR_FREE, 0); |
| 531 | root_tag_set(root, IDR_FREE); |
| 532 | } |
| 533 | } else { |
| 534 | /* Propagate the aggregated tag info to the new child */ |
| 535 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) { |
| 536 | if (root_tag_get(root, tag)) |
| 537 | tag_set(node, tag, 0); |
| 538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Matthew Wilcox | d089126 | 2016-05-20 17:03:19 -0700 | [diff] [blame] | 541 | BUG_ON(shift > BITS_PER_LONG); |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 542 | if (radix_tree_is_internal_node(entry)) { |
| 543 | entry_to_node(entry)->parent = node; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 544 | } else if (xa_is_value(entry)) { |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 545 | /* Moving a value entry root->xa_head to a node */ |
| 546 | node->nr_values = 1; |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 547 | } |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 548 | /* |
| 549 | * entry was already in the radix tree, so we do not need |
| 550 | * rcu_assign_pointer here |
| 551 | */ |
| 552 | node->slots[0] = (void __rcu *)entry; |
| 553 | entry = node_to_entry(node); |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 554 | rcu_assign_pointer(root->xa_head, entry); |
Matthew Wilcox | d089126 | 2016-05-20 17:03:19 -0700 | [diff] [blame] | 555 | shift += RADIX_TREE_MAP_SHIFT; |
Matthew Wilcox | d089126 | 2016-05-20 17:03:19 -0700 | [diff] [blame] | 556 | } while (shift <= maxshift); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | out: |
Matthew Wilcox | d089126 | 2016-05-20 17:03:19 -0700 | [diff] [blame] | 558 | return maxshift + RADIX_TREE_MAP_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 562 | * radix_tree_shrink - shrink radix tree to minimum height |
| 563 | * @root radix tree root |
| 564 | */ |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 565 | static inline bool radix_tree_shrink(struct radix_tree_root *root) |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 566 | { |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 567 | bool shrunk = false; |
| 568 | |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 569 | for (;;) { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 570 | struct radix_tree_node *node = rcu_dereference_raw(root->xa_head); |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 571 | struct radix_tree_node *child; |
| 572 | |
| 573 | if (!radix_tree_is_internal_node(node)) |
| 574 | break; |
| 575 | node = entry_to_node(node); |
| 576 | |
| 577 | /* |
| 578 | * The candidate node has more than one child, or its child |
| 579 | * is not at the leftmost slot, or the child is a multiorder |
| 580 | * entry, we cannot shrink. |
| 581 | */ |
| 582 | if (node->count != 1) |
| 583 | break; |
Matthew Wilcox | 12320d0 | 2017-02-13 15:22:48 -0500 | [diff] [blame] | 584 | child = rcu_dereference_raw(node->slots[0]); |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 585 | if (!child) |
| 586 | break; |
| 587 | if (!radix_tree_is_internal_node(child) && node->shift) |
| 588 | break; |
| 589 | |
Matthew Wilcox | 66ee620 | 2018-06-25 06:56:50 -0400 | [diff] [blame] | 590 | /* |
| 591 | * For an IDR, we must not shrink entry 0 into the root in |
| 592 | * case somebody calls idr_replace() with a pointer that |
| 593 | * appears to be an internal entry |
| 594 | */ |
| 595 | if (!node->shift && is_idr(root)) |
| 596 | break; |
| 597 | |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 598 | if (radix_tree_is_internal_node(child)) |
| 599 | entry_to_node(child)->parent = NULL; |
| 600 | |
| 601 | /* |
| 602 | * We don't need rcu_assign_pointer(), since we are simply |
| 603 | * moving the node from one part of the tree to another: if it |
| 604 | * was safe to dereference the old pointer to it |
| 605 | * (node->slots[0]), it will be safe to dereference the new |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 606 | * one (root->xa_head) as far as dependent read barriers go. |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 607 | */ |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 608 | root->xa_head = (void __rcu *)child; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 609 | if (is_idr(root) && !tag_get(node, IDR_FREE, 0)) |
| 610 | root_tag_clear(root, IDR_FREE); |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | * We have a dilemma here. The node's slot[0] must not be |
| 614 | * NULLed in case there are concurrent lookups expecting to |
| 615 | * find the item. However if this was a bottom-level node, |
| 616 | * then it may be subject to the slot pointer being visible |
| 617 | * to callers dereferencing it. If item corresponding to |
| 618 | * slot[0] is subsequently deleted, these callers would expect |
| 619 | * their slot to become empty sooner or later. |
| 620 | * |
| 621 | * For example, lockless pagecache will look up a slot, deref |
| 622 | * the page pointer, and if the page has 0 refcount it means it |
| 623 | * was concurrently deleted from pagecache so try the deref |
| 624 | * again. Fortunately there is already a requirement for logic |
| 625 | * to retry the entire slot lookup -- the indirect pointer |
| 626 | * problem (replacing direct root node with an indirect pointer |
| 627 | * also results in a stale slot). So tag the slot as indirect |
| 628 | * to force callers to retry. |
| 629 | */ |
Johannes Weiner | 4d693d0 | 2016-12-12 16:43:49 -0800 | [diff] [blame] | 630 | node->count = 0; |
| 631 | if (!radix_tree_is_internal_node(child)) { |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 632 | node->slots[0] = (void __rcu *)RADIX_TREE_RETRY; |
Johannes Weiner | 4d693d0 | 2016-12-12 16:43:49 -0800 | [diff] [blame] | 633 | } |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 634 | |
Johannes Weiner | ea07b86 | 2017-01-06 19:21:43 -0500 | [diff] [blame] | 635 | WARN_ON_ONCE(!list_empty(&node->private_list)); |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 636 | radix_tree_node_free(node); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 637 | shrunk = true; |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 638 | } |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 639 | |
| 640 | return shrunk; |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 641 | } |
| 642 | |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 643 | static bool delete_node(struct radix_tree_root *root, |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 644 | struct radix_tree_node *node) |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 645 | { |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 646 | bool deleted = false; |
| 647 | |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 648 | do { |
| 649 | struct radix_tree_node *parent; |
| 650 | |
| 651 | if (node->count) { |
Matthew Wilcox | 12320d0 | 2017-02-13 15:22:48 -0500 | [diff] [blame] | 652 | if (node_to_entry(node) == |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 653 | rcu_dereference_raw(root->xa_head)) |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 654 | deleted |= radix_tree_shrink(root); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 655 | return deleted; |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | parent = node->parent; |
| 659 | if (parent) { |
| 660 | parent->slots[node->offset] = NULL; |
| 661 | parent->count--; |
| 662 | } else { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 663 | /* |
| 664 | * Shouldn't the tags already have all been cleared |
| 665 | * by the caller? |
| 666 | */ |
| 667 | if (!is_idr(root)) |
| 668 | root_tag_clear_all(root); |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 669 | root->xa_head = NULL; |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Johannes Weiner | ea07b86 | 2017-01-06 19:21:43 -0500 | [diff] [blame] | 672 | WARN_ON_ONCE(!list_empty(&node->private_list)); |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 673 | radix_tree_node_free(node); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 674 | deleted = true; |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 675 | |
| 676 | node = parent; |
| 677 | } while (node); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 678 | |
| 679 | return deleted; |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | /** |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 683 | * __radix_tree_create - create a slot in a radix tree |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | * @root: radix tree root |
| 685 | * @index: index key |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 686 | * @order: index occupies 2^order aligned slots |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 687 | * @nodep: returns node |
| 688 | * @slotp: returns slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | * |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 690 | * Create, if necessary, and return the node and slot for an item |
| 691 | * at position @index in the radix tree @root. |
| 692 | * |
| 693 | * Until there is more than one item in the tree, no nodes are |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 694 | * allocated and @root->xa_head is used as a direct slot instead of |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 695 | * pointing to a node, in which case *@nodep will be NULL. |
| 696 | * |
| 697 | * Returns -ENOMEM, or 0 for success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | */ |
Matthew Wilcox | 74d6095 | 2017-11-17 10:01:45 -0500 | [diff] [blame] | 699 | static int __radix_tree_create(struct radix_tree_root *root, |
| 700 | unsigned long index, unsigned order, |
| 701 | struct radix_tree_node **nodep, void __rcu ***slotp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 703 | struct radix_tree_node *node = NULL, *child; |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 704 | void __rcu **slot = (void __rcu **)&root->xa_head; |
Matthew Wilcox | 49ea6eb | 2016-05-20 17:02:11 -0700 | [diff] [blame] | 705 | unsigned long maxindex; |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 706 | unsigned int shift, offset = 0; |
Matthew Wilcox | 49ea6eb | 2016-05-20 17:02:11 -0700 | [diff] [blame] | 707 | unsigned long max = index | ((1UL << order) - 1); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 708 | gfp_t gfp = root_gfp_mask(root); |
Matthew Wilcox | 49ea6eb | 2016-05-20 17:02:11 -0700 | [diff] [blame] | 709 | |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 710 | shift = radix_tree_load_root(root, &child, &maxindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
| 712 | /* Make sure the tree is high enough. */ |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 713 | if (order > 0 && max == ((1UL << order) - 1)) |
| 714 | max++; |
Matthew Wilcox | 49ea6eb | 2016-05-20 17:02:11 -0700 | [diff] [blame] | 715 | if (max > maxindex) { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 716 | int error = radix_tree_extend(root, gfp, max, shift); |
Matthew Wilcox | 49ea6eb | 2016-05-20 17:02:11 -0700 | [diff] [blame] | 717 | if (error < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | return error; |
Matthew Wilcox | 49ea6eb | 2016-05-20 17:02:11 -0700 | [diff] [blame] | 719 | shift = error; |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 720 | child = rcu_dereference_raw(root->xa_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 723 | while (shift > order) { |
Matthew Wilcox | c12e51b | 2016-05-20 17:03:10 -0700 | [diff] [blame] | 724 | shift -= RADIX_TREE_MAP_SHIFT; |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 725 | if (child == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | /* Have to add a child node. */ |
Matthew Wilcox | d58275b | 2017-01-16 17:10:21 -0500 | [diff] [blame] | 727 | child = radix_tree_node_alloc(gfp, node, root, shift, |
Matthew Wilcox | e8de434 | 2016-12-14 15:09:31 -0800 | [diff] [blame] | 728 | offset, 0, 0); |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 729 | if (!child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | return -ENOMEM; |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 731 | rcu_assign_pointer(*slot, node_to_entry(child)); |
| 732 | if (node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | node->count++; |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 734 | } else if (!radix_tree_is_internal_node(child)) |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 735 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | |
| 737 | /* Go a level down */ |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 738 | node = entry_to_node(child); |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 739 | offset = radix_tree_descend(node, &child, index); |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 740 | slot = &node->slots[offset]; |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 743 | if (nodep) |
| 744 | *nodep = node; |
| 745 | if (slotp) |
Matthew Wilcox | 89148aa | 2016-05-20 17:03:42 -0700 | [diff] [blame] | 746 | *slotp = slot; |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 747 | return 0; |
| 748 | } |
| 749 | |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 750 | /* |
| 751 | * Free any nodes below this node. The tree is presumed to not need |
| 752 | * shrinking, and any user data in the tree is presumed to not need a |
| 753 | * destructor called on it. If we need to add a destructor, we can |
| 754 | * add that functionality later. Note that we may not clear tags or |
| 755 | * slots from the tree as an RCU walker may still have a pointer into |
| 756 | * this subtree. We could replace the entries with RADIX_TREE_RETRY, |
| 757 | * but we'll still have to clear those in rcu_free. |
| 758 | */ |
| 759 | static void radix_tree_free_nodes(struct radix_tree_node *node) |
| 760 | { |
| 761 | unsigned offset = 0; |
| 762 | struct radix_tree_node *child = entry_to_node(node); |
| 763 | |
| 764 | for (;;) { |
Matthew Wilcox | 12320d0 | 2017-02-13 15:22:48 -0500 | [diff] [blame] | 765 | void *entry = rcu_dereference_raw(child->slots[offset]); |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 766 | if (xa_is_node(entry) && child->shift) { |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 767 | child = entry_to_node(entry); |
| 768 | offset = 0; |
| 769 | continue; |
| 770 | } |
| 771 | offset++; |
| 772 | while (offset == RADIX_TREE_MAP_SIZE) { |
| 773 | struct radix_tree_node *old = child; |
| 774 | offset = child->offset + 1; |
| 775 | child = child->parent; |
Matthew Wilcox | dd040b6 | 2017-01-24 15:18:16 -0800 | [diff] [blame] | 776 | WARN_ON_ONCE(!list_empty(&old->private_list)); |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 777 | radix_tree_node_free(old); |
| 778 | if (old == entry_to_node(node)) |
| 779 | return; |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 784 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 785 | static inline int insert_entries(struct radix_tree_node *node, |
| 786 | void __rcu **slot, void *item, unsigned order, bool replace) |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 787 | { |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 788 | void *sibling; |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 789 | unsigned i, n, tag, offset, tags = 0; |
| 790 | |
| 791 | if (node) { |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 792 | if (order > node->shift) |
| 793 | n = 1 << (order - node->shift); |
| 794 | else |
| 795 | n = 1; |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 796 | offset = get_slot_offset(node, slot); |
| 797 | } else { |
| 798 | n = 1; |
| 799 | offset = 0; |
| 800 | } |
| 801 | |
| 802 | if (n > 1) { |
| 803 | offset = offset & ~(n - 1); |
| 804 | slot = &node->slots[offset]; |
| 805 | } |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 806 | sibling = xa_mk_sibling(offset); |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 807 | |
| 808 | for (i = 0; i < n; i++) { |
| 809 | if (slot[i]) { |
| 810 | if (replace) { |
| 811 | node->count--; |
| 812 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 813 | if (tag_get(node, tag, offset + i)) |
| 814 | tags |= 1 << tag; |
| 815 | } else |
| 816 | return -EEXIST; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | for (i = 0; i < n; i++) { |
Matthew Wilcox | 12320d0 | 2017-02-13 15:22:48 -0500 | [diff] [blame] | 821 | struct radix_tree_node *old = rcu_dereference_raw(slot[i]); |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 822 | if (i) { |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 823 | rcu_assign_pointer(slot[i], sibling); |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 824 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 825 | if (tags & (1 << tag)) |
| 826 | tag_clear(node, tag, offset + i); |
| 827 | } else { |
| 828 | rcu_assign_pointer(slot[i], item); |
| 829 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 830 | if (tags & (1 << tag)) |
| 831 | tag_set(node, tag, offset); |
| 832 | } |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 833 | if (xa_is_node(old)) |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 834 | radix_tree_free_nodes(old); |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 835 | if (xa_is_value(old)) |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 836 | node->nr_values--; |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 837 | } |
| 838 | if (node) { |
| 839 | node->count += n; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 840 | if (xa_is_value(item)) |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 841 | node->nr_values += n; |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 842 | } |
| 843 | return n; |
| 844 | } |
| 845 | #else |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 846 | static inline int insert_entries(struct radix_tree_node *node, |
| 847 | void __rcu **slot, void *item, unsigned order, bool replace) |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 848 | { |
| 849 | if (*slot) |
| 850 | return -EEXIST; |
| 851 | rcu_assign_pointer(*slot, item); |
| 852 | if (node) { |
| 853 | node->count++; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 854 | if (xa_is_value(item)) |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 855 | node->nr_values++; |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 856 | } |
| 857 | return 1; |
| 858 | } |
| 859 | #endif |
| 860 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 861 | /** |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 862 | * __radix_tree_insert - insert into a radix tree |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 863 | * @root: radix tree root |
| 864 | * @index: index key |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 865 | * @order: key covers the 2^order indices around index |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 866 | * @item: item to insert |
| 867 | * |
| 868 | * Insert an item into the radix tree at position @index. |
| 869 | */ |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 870 | int __radix_tree_insert(struct radix_tree_root *root, unsigned long index, |
| 871 | unsigned order, void *item) |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 872 | { |
| 873 | struct radix_tree_node *node; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 874 | void __rcu **slot; |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 875 | int error; |
| 876 | |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 877 | BUG_ON(radix_tree_is_internal_node(item)); |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 878 | |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 879 | error = __radix_tree_create(root, index, order, &node, &slot); |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 880 | if (error) |
| 881 | return error; |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 882 | |
| 883 | error = insert_entries(node, slot, item, order, false); |
| 884 | if (error < 0) |
| 885 | return error; |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 886 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 887 | if (node) { |
Matthew Wilcox | 7b60e9a | 2016-05-20 17:02:23 -0700 | [diff] [blame] | 888 | unsigned offset = get_slot_offset(node, slot); |
Matthew Wilcox | 7b60e9a | 2016-05-20 17:02:23 -0700 | [diff] [blame] | 889 | BUG_ON(tag_get(node, 0, offset)); |
| 890 | BUG_ON(tag_get(node, 1, offset)); |
| 891 | BUG_ON(tag_get(node, 2, offset)); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 892 | } else { |
Matthew Wilcox | 7b60e9a | 2016-05-20 17:02:23 -0700 | [diff] [blame] | 893 | BUG_ON(root_tags_get(root)); |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 894 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | return 0; |
| 897 | } |
Matthew Wilcox | e614523 | 2016-03-17 14:21:54 -0700 | [diff] [blame] | 898 | EXPORT_SYMBOL(__radix_tree_insert); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 900 | /** |
| 901 | * __radix_tree_lookup - lookup an item in a radix tree |
| 902 | * @root: radix tree root |
| 903 | * @index: index key |
| 904 | * @nodep: returns node |
| 905 | * @slotp: returns slot |
| 906 | * |
| 907 | * Lookup and return the item at position @index in the radix |
| 908 | * tree @root. |
| 909 | * |
| 910 | * Until there is more than one item in the tree, no nodes are |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 911 | * allocated and @root->xa_head is used as a direct slot instead of |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 912 | * pointing to a node, in which case *@nodep will be NULL. |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 913 | */ |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 914 | void *__radix_tree_lookup(const struct radix_tree_root *root, |
| 915 | unsigned long index, struct radix_tree_node **nodep, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 916 | void __rcu ***slotp) |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 917 | { |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 918 | struct radix_tree_node *node, *parent; |
Matthew Wilcox | 8582995 | 2016-05-20 17:02:20 -0700 | [diff] [blame] | 919 | unsigned long maxindex; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 920 | void __rcu **slot; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 921 | |
Matthew Wilcox | 8582995 | 2016-05-20 17:02:20 -0700 | [diff] [blame] | 922 | restart: |
| 923 | parent = NULL; |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 924 | slot = (void __rcu **)&root->xa_head; |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 925 | radix_tree_load_root(root, &node, &maxindex); |
Matthew Wilcox | 8582995 | 2016-05-20 17:02:20 -0700 | [diff] [blame] | 926 | if (index > maxindex) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 927 | return NULL; |
| 928 | |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 929 | while (radix_tree_is_internal_node(node)) { |
Matthew Wilcox | 8582995 | 2016-05-20 17:02:20 -0700 | [diff] [blame] | 930 | unsigned offset; |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 931 | |
Matthew Wilcox | 8582995 | 2016-05-20 17:02:20 -0700 | [diff] [blame] | 932 | if (node == RADIX_TREE_RETRY) |
| 933 | goto restart; |
Matthew Wilcox | 4dd6c09 | 2016-05-20 17:03:27 -0700 | [diff] [blame] | 934 | parent = entry_to_node(node); |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 935 | offset = radix_tree_descend(parent, &node, index); |
Matthew Wilcox | 8582995 | 2016-05-20 17:02:20 -0700 | [diff] [blame] | 936 | slot = parent->slots + offset; |
Matthew Wilcox | 66ee620 | 2018-06-25 06:56:50 -0400 | [diff] [blame] | 937 | if (parent->shift == 0) |
| 938 | break; |
Matthew Wilcox | 8582995 | 2016-05-20 17:02:20 -0700 | [diff] [blame] | 939 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 940 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 941 | if (nodep) |
| 942 | *nodep = parent; |
| 943 | if (slotp) |
| 944 | *slotp = slot; |
| 945 | return node; |
Huang Shijie | b72b71c | 2009-06-16 15:33:42 -0700 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | /** |
| 949 | * radix_tree_lookup_slot - lookup a slot in a radix tree |
| 950 | * @root: radix tree root |
| 951 | * @index: index key |
| 952 | * |
| 953 | * Returns: the slot corresponding to the position @index in the |
| 954 | * radix tree @root. This is useful for update-if-exists operations. |
| 955 | * |
| 956 | * This function can be called under rcu_read_lock iff the slot is not |
| 957 | * modified by radix_tree_replace_slot, otherwise it must be called |
| 958 | * exclusive from other writers. Any dereference of the slot must be done |
| 959 | * using radix_tree_deref_slot. |
| 960 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 961 | void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *root, |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 962 | unsigned long index) |
Huang Shijie | b72b71c | 2009-06-16 15:33:42 -0700 | [diff] [blame] | 963 | { |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 964 | void __rcu **slot; |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 965 | |
| 966 | if (!__radix_tree_lookup(root, index, NULL, &slot)) |
| 967 | return NULL; |
| 968 | return slot; |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 969 | } |
| 970 | EXPORT_SYMBOL(radix_tree_lookup_slot); |
| 971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | /** |
| 973 | * radix_tree_lookup - perform lookup operation on a radix tree |
| 974 | * @root: radix tree root |
| 975 | * @index: index key |
| 976 | * |
| 977 | * Lookup the item at the position @index in the radix tree @root. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 978 | * |
| 979 | * This function can be called under rcu_read_lock, however the caller |
| 980 | * must manage lifetimes of leaf nodes (eg. RCU may also be used to free |
| 981 | * them safely). No RCU barriers are required to access or modify the |
| 982 | * returned item, however. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | */ |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 984 | void *radix_tree_lookup(const struct radix_tree_root *root, unsigned long index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | { |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 986 | return __radix_tree_lookup(root, index, NULL, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | EXPORT_SYMBOL(radix_tree_lookup); |
| 989 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 990 | static inline void replace_sibling_entries(struct radix_tree_node *node, |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 991 | void __rcu **slot, int count, int values) |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 992 | { |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 993 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 994 | unsigned offset = get_slot_offset(node, slot); |
| 995 | void *ptr = xa_mk_sibling(offset); |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 996 | |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 997 | while (++offset < RADIX_TREE_MAP_SIZE) { |
Matthew Wilcox | 12320d0 | 2017-02-13 15:22:48 -0500 | [diff] [blame] | 998 | if (rcu_dereference_raw(node->slots[offset]) != ptr) |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 999 | break; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1000 | if (count < 0) { |
| 1001 | node->slots[offset] = NULL; |
| 1002 | node->count--; |
| 1003 | } |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1004 | node->nr_values += values; |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 1005 | } |
| 1006 | #endif |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1009 | static void replace_slot(void __rcu **slot, void *item, |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1010 | struct radix_tree_node *node, int count, int values) |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1011 | { |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1012 | if (node && (count || values)) { |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 1013 | node->count += count; |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1014 | node->nr_values += values; |
| 1015 | replace_sibling_entries(node, slot, count, values); |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 1016 | } |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1017 | |
| 1018 | rcu_assign_pointer(*slot, item); |
| 1019 | } |
| 1020 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1021 | static bool node_tag_get(const struct radix_tree_root *root, |
| 1022 | const struct radix_tree_node *node, |
| 1023 | unsigned int tag, unsigned int offset) |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 1024 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1025 | if (node) |
| 1026 | return tag_get(node, tag, offset); |
| 1027 | return root_tag_get(root, tag); |
| 1028 | } |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 1029 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1030 | /* |
| 1031 | * IDR users want to be able to store NULL in the tree, so if the slot isn't |
| 1032 | * free, don't adjust the count, even if it's transitioning between NULL and |
| 1033 | * non-NULL. For the IDA, we mark slots as being IDR_FREE while they still |
| 1034 | * have empty bits, but it only stores NULL in slots when they're being |
| 1035 | * deleted. |
| 1036 | */ |
| 1037 | static int calculate_count(struct radix_tree_root *root, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1038 | struct radix_tree_node *node, void __rcu **slot, |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1039 | void *item, void *old) |
| 1040 | { |
| 1041 | if (is_idr(root)) { |
| 1042 | unsigned offset = get_slot_offset(node, slot); |
| 1043 | bool free = node_tag_get(root, node, IDR_FREE, offset); |
| 1044 | if (!free) |
| 1045 | return 0; |
| 1046 | if (!old) |
| 1047 | return 1; |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 1048 | } |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1049 | return !!item - !!old; |
Matthew Wilcox | a90eb3a | 2016-12-14 15:09:07 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | /** |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 1053 | * __radix_tree_replace - replace item in a slot |
Johannes Weiner | 4d693d0 | 2016-12-12 16:43:49 -0800 | [diff] [blame] | 1054 | * @root: radix tree root |
| 1055 | * @node: pointer to tree node |
| 1056 | * @slot: pointer to slot in @node |
| 1057 | * @item: new item to store in the slot. |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 1058 | * |
| 1059 | * For use with __radix_tree_lookup(). Caller must hold tree write locked |
| 1060 | * across slot lookup and replacement. |
| 1061 | */ |
| 1062 | void __radix_tree_replace(struct radix_tree_root *root, |
| 1063 | struct radix_tree_node *node, |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 1064 | void __rcu **slot, void *item) |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 1065 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1066 | void *old = rcu_dereference_raw(*slot); |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1067 | int values = !!xa_is_value(item) - !!xa_is_value(old); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1068 | int count = calculate_count(root, node, slot, item, old); |
| 1069 | |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1070 | /* |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1071 | * This function supports replacing value entries and |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 1072 | * deleting entries, but that needs accounting against the |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1073 | * node unless the slot is root->xa_head. |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1074 | */ |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1075 | WARN_ON_ONCE(!node && (slot != (void __rcu **)&root->xa_head) && |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1076 | (count || values)); |
| 1077 | replace_slot(slot, item, node, count, values); |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 1078 | |
Johannes Weiner | 4d693d0 | 2016-12-12 16:43:49 -0800 | [diff] [blame] | 1079 | if (!node) |
| 1080 | return; |
| 1081 | |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 1082 | delete_node(root, node); |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1083 | } |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 1084 | |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1085 | /** |
| 1086 | * radix_tree_replace_slot - replace item in a slot |
| 1087 | * @root: radix tree root |
| 1088 | * @slot: pointer to slot |
| 1089 | * @item: new item to store in the slot. |
| 1090 | * |
Matthew Wilcox | 7b8d046 | 2017-12-01 22:13:06 -0500 | [diff] [blame] | 1091 | * For use with radix_tree_lookup_slot() and |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1092 | * radix_tree_gang_lookup_tag_slot(). Caller must hold tree write locked |
| 1093 | * across slot lookup and replacement. |
| 1094 | * |
| 1095 | * NOTE: This cannot be used to switch between non-entries (empty slots), |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1096 | * regular entries, and value entries, as that requires accounting |
Johannes Weiner | f4b109c | 2016-12-12 16:43:46 -0800 | [diff] [blame] | 1097 | * inside the radix tree node. When switching from one type of entry or |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1098 | * deleting, use __radix_tree_lookup() and __radix_tree_replace() or |
| 1099 | * radix_tree_iter_replace(). |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1100 | */ |
| 1101 | void radix_tree_replace_slot(struct radix_tree_root *root, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1102 | void __rcu **slot, void *item) |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 1103 | { |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 1104 | __radix_tree_replace(root, NULL, slot, item); |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 1105 | } |
Song Liu | 10257d7 | 2017-01-11 10:00:51 -0800 | [diff] [blame] | 1106 | EXPORT_SYMBOL(radix_tree_replace_slot); |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 1107 | |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1108 | /** |
| 1109 | * radix_tree_iter_replace - replace item in a slot |
| 1110 | * @root: radix tree root |
| 1111 | * @slot: pointer to slot |
| 1112 | * @item: new item to store in the slot. |
| 1113 | * |
| 1114 | * For use with radix_tree_split() and radix_tree_for_each_slot(). |
| 1115 | * Caller must hold tree write locked across split and replacement. |
| 1116 | */ |
| 1117 | void radix_tree_iter_replace(struct radix_tree_root *root, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1118 | const struct radix_tree_iter *iter, |
| 1119 | void __rcu **slot, void *item) |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1120 | { |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 1121 | __radix_tree_replace(root, iter->node, slot, item); |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1122 | } |
| 1123 | |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 1124 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
| 1125 | /** |
| 1126 | * radix_tree_join - replace multiple entries with one multiorder entry |
| 1127 | * @root: radix tree root |
| 1128 | * @index: an index inside the new entry |
| 1129 | * @order: order of the new entry |
| 1130 | * @item: new entry |
| 1131 | * |
| 1132 | * Call this function to replace several entries with one larger entry. |
| 1133 | * The existing entries are presumed to not need freeing as a result of |
| 1134 | * this call. |
| 1135 | * |
| 1136 | * The replacement entry will have all the tags set on it that were set |
| 1137 | * on any of the entries it is replacing. |
| 1138 | */ |
| 1139 | int radix_tree_join(struct radix_tree_root *root, unsigned long index, |
| 1140 | unsigned order, void *item) |
| 1141 | { |
| 1142 | struct radix_tree_node *node; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1143 | void __rcu **slot; |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 1144 | int error; |
| 1145 | |
| 1146 | BUG_ON(radix_tree_is_internal_node(item)); |
| 1147 | |
| 1148 | error = __radix_tree_create(root, index, order, &node, &slot); |
| 1149 | if (!error) |
| 1150 | error = insert_entries(node, slot, item, order, true); |
| 1151 | if (error > 0) |
| 1152 | error = 0; |
| 1153 | |
| 1154 | return error; |
| 1155 | } |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1156 | |
| 1157 | /** |
| 1158 | * radix_tree_split - Split an entry into smaller entries |
| 1159 | * @root: radix tree root |
| 1160 | * @index: An index within the large entry |
| 1161 | * @order: Order of new entries |
| 1162 | * |
| 1163 | * Call this function as the first step in replacing a multiorder entry |
| 1164 | * with several entries of lower order. After this function returns, |
| 1165 | * loop over the relevant portion of the tree using radix_tree_for_each_slot() |
| 1166 | * and call radix_tree_iter_replace() to set up each new entry. |
| 1167 | * |
| 1168 | * The tags from this entry are replicated to all the new entries. |
| 1169 | * |
| 1170 | * The radix tree should be locked against modification during the entire |
| 1171 | * replacement operation. Lock-free lookups will see RADIX_TREE_RETRY which |
| 1172 | * should prompt RCU walkers to restart the lookup from the root. |
| 1173 | */ |
| 1174 | int radix_tree_split(struct radix_tree_root *root, unsigned long index, |
| 1175 | unsigned order) |
| 1176 | { |
| 1177 | struct radix_tree_node *parent, *node, *child; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1178 | void __rcu **slot; |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1179 | unsigned int offset, end; |
| 1180 | unsigned n, tag, tags = 0; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1181 | gfp_t gfp = root_gfp_mask(root); |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1182 | |
| 1183 | if (!__radix_tree_lookup(root, index, &parent, &slot)) |
| 1184 | return -ENOENT; |
| 1185 | if (!parent) |
| 1186 | return -ENOENT; |
| 1187 | |
| 1188 | offset = get_slot_offset(parent, slot); |
| 1189 | |
| 1190 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 1191 | if (tag_get(parent, tag, offset)) |
| 1192 | tags |= 1 << tag; |
| 1193 | |
| 1194 | for (end = offset + 1; end < RADIX_TREE_MAP_SIZE; end++) { |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 1195 | if (!xa_is_sibling(rcu_dereference_raw(parent->slots[end]))) |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1196 | break; |
| 1197 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 1198 | if (tags & (1 << tag)) |
| 1199 | tag_set(parent, tag, end); |
| 1200 | /* rcu_assign_pointer ensures tags are set before RETRY */ |
| 1201 | rcu_assign_pointer(parent->slots[end], RADIX_TREE_RETRY); |
| 1202 | } |
| 1203 | rcu_assign_pointer(parent->slots[offset], RADIX_TREE_RETRY); |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1204 | parent->nr_values -= (end - offset); |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1205 | |
| 1206 | if (order == parent->shift) |
| 1207 | return 0; |
| 1208 | if (order > parent->shift) { |
| 1209 | while (offset < end) |
| 1210 | offset += insert_entries(parent, &parent->slots[offset], |
| 1211 | RADIX_TREE_RETRY, order, true); |
| 1212 | return 0; |
| 1213 | } |
| 1214 | |
| 1215 | node = parent; |
| 1216 | |
| 1217 | for (;;) { |
| 1218 | if (node->shift > order) { |
Matthew Wilcox | d58275b | 2017-01-16 17:10:21 -0500 | [diff] [blame] | 1219 | child = radix_tree_node_alloc(gfp, node, root, |
Matthew Wilcox | e8de434 | 2016-12-14 15:09:31 -0800 | [diff] [blame] | 1220 | node->shift - RADIX_TREE_MAP_SHIFT, |
| 1221 | offset, 0, 0); |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1222 | if (!child) |
| 1223 | goto nomem; |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1224 | if (node != parent) { |
| 1225 | node->count++; |
Matthew Wilcox | 12320d0 | 2017-02-13 15:22:48 -0500 | [diff] [blame] | 1226 | rcu_assign_pointer(node->slots[offset], |
| 1227 | node_to_entry(child)); |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1228 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 1229 | if (tags & (1 << tag)) |
| 1230 | tag_set(node, tag, offset); |
| 1231 | } |
| 1232 | |
| 1233 | node = child; |
| 1234 | offset = 0; |
| 1235 | continue; |
| 1236 | } |
| 1237 | |
| 1238 | n = insert_entries(node, &node->slots[offset], |
| 1239 | RADIX_TREE_RETRY, order, false); |
| 1240 | BUG_ON(n > RADIX_TREE_MAP_SIZE); |
| 1241 | |
| 1242 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 1243 | if (tags & (1 << tag)) |
| 1244 | tag_set(node, tag, offset); |
| 1245 | offset += n; |
| 1246 | |
| 1247 | while (offset == RADIX_TREE_MAP_SIZE) { |
| 1248 | if (node == parent) |
| 1249 | break; |
| 1250 | offset = node->offset; |
| 1251 | child = node; |
| 1252 | node = node->parent; |
| 1253 | rcu_assign_pointer(node->slots[offset], |
| 1254 | node_to_entry(child)); |
| 1255 | offset++; |
| 1256 | } |
| 1257 | if ((node == parent) && (offset == end)) |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
| 1261 | nomem: |
| 1262 | /* Shouldn't happen; did user forget to preload? */ |
| 1263 | /* TODO: free all the allocated nodes */ |
| 1264 | WARN_ON(1); |
| 1265 | return -ENOMEM; |
| 1266 | } |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 1267 | #endif |
| 1268 | |
Matthew Wilcox | 30b888b | 2017-01-28 09:55:20 -0500 | [diff] [blame] | 1269 | static void node_tag_set(struct radix_tree_root *root, |
| 1270 | struct radix_tree_node *node, |
| 1271 | unsigned int tag, unsigned int offset) |
| 1272 | { |
| 1273 | while (node) { |
| 1274 | if (tag_get(node, tag, offset)) |
| 1275 | return; |
| 1276 | tag_set(node, tag, offset); |
| 1277 | offset = node->offset; |
| 1278 | node = node->parent; |
| 1279 | } |
| 1280 | |
| 1281 | if (!root_tag_get(root, tag)) |
| 1282 | root_tag_set(root, tag); |
| 1283 | } |
| 1284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | /** |
| 1286 | * radix_tree_tag_set - set a tag on a radix tree node |
| 1287 | * @root: radix tree root |
| 1288 | * @index: index key |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 1289 | * @tag: tag index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | * |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1291 | * Set the search tag (which must be < RADIX_TREE_MAX_TAGS) |
| 1292 | * corresponding to @index in the radix tree. From |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | * the root all the way down to the leaf node. |
| 1294 | * |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 1295 | * Returns the address of the tagged item. Setting a tag on a not-present |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | * item is a bug. |
| 1297 | */ |
| 1298 | void *radix_tree_tag_set(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1299 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | { |
Ross Zwisler | fb96990 | 2016-05-20 17:02:32 -0700 | [diff] [blame] | 1301 | struct radix_tree_node *node, *parent; |
| 1302 | unsigned long maxindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1304 | radix_tree_load_root(root, &node, &maxindex); |
Ross Zwisler | fb96990 | 2016-05-20 17:02:32 -0700 | [diff] [blame] | 1305 | BUG_ON(index > maxindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 1307 | while (radix_tree_is_internal_node(node)) { |
Ross Zwisler | fb96990 | 2016-05-20 17:02:32 -0700 | [diff] [blame] | 1308 | unsigned offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
Matthew Wilcox | 4dd6c09 | 2016-05-20 17:03:27 -0700 | [diff] [blame] | 1310 | parent = entry_to_node(node); |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1311 | offset = radix_tree_descend(parent, &node, index); |
Ross Zwisler | fb96990 | 2016-05-20 17:02:32 -0700 | [diff] [blame] | 1312 | BUG_ON(!node); |
| 1313 | |
| 1314 | if (!tag_get(parent, tag, offset)) |
| 1315 | tag_set(parent, tag, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
| 1317 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 1318 | /* set the root's tag bit */ |
Ross Zwisler | fb96990 | 2016-05-20 17:02:32 -0700 | [diff] [blame] | 1319 | if (!root_tag_get(root, tag)) |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 1320 | root_tag_set(root, tag); |
| 1321 | |
Ross Zwisler | fb96990 | 2016-05-20 17:02:32 -0700 | [diff] [blame] | 1322 | return node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | } |
| 1324 | EXPORT_SYMBOL(radix_tree_tag_set); |
| 1325 | |
Matthew Wilcox | 30b888b | 2017-01-28 09:55:20 -0500 | [diff] [blame] | 1326 | /** |
| 1327 | * radix_tree_iter_tag_set - set a tag on the current iterator entry |
| 1328 | * @root: radix tree root |
| 1329 | * @iter: iterator state |
| 1330 | * @tag: tag to set |
| 1331 | */ |
| 1332 | void radix_tree_iter_tag_set(struct radix_tree_root *root, |
| 1333 | const struct radix_tree_iter *iter, unsigned int tag) |
| 1334 | { |
| 1335 | node_tag_set(root, iter->node, tag, iter_offset(iter)); |
| 1336 | } |
| 1337 | |
Matthew Wilcox | d604c32 | 2016-05-20 17:03:45 -0700 | [diff] [blame] | 1338 | static void node_tag_clear(struct radix_tree_root *root, |
| 1339 | struct radix_tree_node *node, |
| 1340 | unsigned int tag, unsigned int offset) |
| 1341 | { |
| 1342 | while (node) { |
| 1343 | if (!tag_get(node, tag, offset)) |
| 1344 | return; |
| 1345 | tag_clear(node, tag, offset); |
| 1346 | if (any_tag_set(node, tag)) |
| 1347 | return; |
| 1348 | |
| 1349 | offset = node->offset; |
| 1350 | node = node->parent; |
| 1351 | } |
| 1352 | |
| 1353 | /* clear the root's tag bit */ |
| 1354 | if (root_tag_get(root, tag)) |
| 1355 | root_tag_clear(root, tag); |
| 1356 | } |
| 1357 | |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 1358 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | * radix_tree_tag_clear - clear a tag on a radix tree node |
| 1360 | * @root: radix tree root |
| 1361 | * @index: index key |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 1362 | * @tag: tag index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | * |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1364 | * Clear the search tag (which must be < RADIX_TREE_MAX_TAGS) |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 1365 | * corresponding to @index in the radix tree. If this causes |
| 1366 | * the leaf node to have no tags set then clear the tag in the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | * next-to-leaf node, etc. |
| 1368 | * |
| 1369 | * Returns the address of the tagged item on success, else NULL. ie: |
| 1370 | * has the same return value and semantics as radix_tree_lookup(). |
| 1371 | */ |
| 1372 | void *radix_tree_tag_clear(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1373 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | { |
Ross Zwisler | 00f47b5 | 2016-05-20 17:02:35 -0700 | [diff] [blame] | 1375 | struct radix_tree_node *node, *parent; |
| 1376 | unsigned long maxindex; |
Hugh Dickins | e2bdb93 | 2012-01-12 17:20:41 -0800 | [diff] [blame] | 1377 | int uninitialized_var(offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1379 | radix_tree_load_root(root, &node, &maxindex); |
Ross Zwisler | 00f47b5 | 2016-05-20 17:02:35 -0700 | [diff] [blame] | 1380 | if (index > maxindex) |
| 1381 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
Ross Zwisler | 00f47b5 | 2016-05-20 17:02:35 -0700 | [diff] [blame] | 1383 | parent = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 1385 | while (radix_tree_is_internal_node(node)) { |
Matthew Wilcox | 4dd6c09 | 2016-05-20 17:03:27 -0700 | [diff] [blame] | 1386 | parent = entry_to_node(node); |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1387 | offset = radix_tree_descend(parent, &node, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Matthew Wilcox | d604c32 | 2016-05-20 17:03:45 -0700 | [diff] [blame] | 1390 | if (node) |
| 1391 | node_tag_clear(root, parent, tag, offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | |
Ross Zwisler | 00f47b5 | 2016-05-20 17:02:35 -0700 | [diff] [blame] | 1393 | return node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | } |
| 1395 | EXPORT_SYMBOL(radix_tree_tag_clear); |
| 1396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | /** |
Matthew Wilcox | 30b888b | 2017-01-28 09:55:20 -0500 | [diff] [blame] | 1398 | * radix_tree_iter_tag_clear - clear a tag on the current iterator entry |
| 1399 | * @root: radix tree root |
| 1400 | * @iter: iterator state |
| 1401 | * @tag: tag to clear |
| 1402 | */ |
| 1403 | void radix_tree_iter_tag_clear(struct radix_tree_root *root, |
| 1404 | const struct radix_tree_iter *iter, unsigned int tag) |
| 1405 | { |
| 1406 | node_tag_clear(root, iter->node, tag, iter_offset(iter)); |
| 1407 | } |
| 1408 | |
| 1409 | /** |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 1410 | * radix_tree_tag_get - get a tag on a radix tree node |
| 1411 | * @root: radix tree root |
| 1412 | * @index: index key |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 1413 | * @tag: tag index (< RADIX_TREE_MAX_TAGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | * |
Marcelo Tosatti | 32605a1 | 2005-09-06 15:16:48 -0700 | [diff] [blame] | 1415 | * Return values: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | * |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 1417 | * 0: tag not present or not set |
| 1418 | * 1: tag set |
David Howells | ce82653 | 2010-04-06 22:36:20 +0100 | [diff] [blame] | 1419 | * |
| 1420 | * Note that the return value of this function may not be relied on, even if |
| 1421 | * the RCU lock is held, unless tag modification and node deletion are excluded |
| 1422 | * from concurrency. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | */ |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 1424 | int radix_tree_tag_get(const struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1425 | unsigned long index, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | { |
Ross Zwisler | 4589ba6 | 2016-05-20 17:02:38 -0700 | [diff] [blame] | 1427 | struct radix_tree_node *node, *parent; |
| 1428 | unsigned long maxindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 1430 | if (!root_tag_get(root, tag)) |
| 1431 | return 0; |
| 1432 | |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1433 | radix_tree_load_root(root, &node, &maxindex); |
Ross Zwisler | 4589ba6 | 2016-05-20 17:02:38 -0700 | [diff] [blame] | 1434 | if (index > maxindex) |
| 1435 | return 0; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 1436 | |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 1437 | while (radix_tree_is_internal_node(node)) { |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1438 | unsigned offset; |
Ross Zwisler | 4589ba6 | 2016-05-20 17:02:38 -0700 | [diff] [blame] | 1439 | |
Matthew Wilcox | 4dd6c09 | 2016-05-20 17:03:27 -0700 | [diff] [blame] | 1440 | parent = entry_to_node(node); |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1441 | offset = radix_tree_descend(parent, &node, index); |
Ross Zwisler | 4589ba6 | 2016-05-20 17:02:38 -0700 | [diff] [blame] | 1442 | |
Ross Zwisler | 4589ba6 | 2016-05-20 17:02:38 -0700 | [diff] [blame] | 1443 | if (!tag_get(parent, tag, offset)) |
| 1444 | return 0; |
| 1445 | if (node == RADIX_TREE_RETRY) |
| 1446 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1447 | } |
Ross Zwisler | 4589ba6 | 2016-05-20 17:02:38 -0700 | [diff] [blame] | 1448 | |
| 1449 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | } |
| 1451 | EXPORT_SYMBOL(radix_tree_tag_get); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1453 | static inline void __set_iter_shift(struct radix_tree_iter *iter, |
| 1454 | unsigned int shift) |
| 1455 | { |
| 1456 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
| 1457 | iter->shift = shift; |
| 1458 | #endif |
| 1459 | } |
| 1460 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1461 | /* Construct iter->tags bit-mask from node->tags[tag] array */ |
| 1462 | static void set_iter_tags(struct radix_tree_iter *iter, |
| 1463 | struct radix_tree_node *node, unsigned offset, |
| 1464 | unsigned tag) |
| 1465 | { |
| 1466 | unsigned tag_long = offset / BITS_PER_LONG; |
| 1467 | unsigned tag_bit = offset % BITS_PER_LONG; |
| 1468 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1469 | if (!node) { |
| 1470 | iter->tags = 1; |
| 1471 | return; |
| 1472 | } |
| 1473 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1474 | iter->tags = node->tags[tag][tag_long] >> tag_bit; |
| 1475 | |
| 1476 | /* This never happens if RADIX_TREE_TAG_LONGS == 1 */ |
| 1477 | if (tag_long < RADIX_TREE_TAG_LONGS - 1) { |
| 1478 | /* Pick tags from next element */ |
| 1479 | if (tag_bit) |
| 1480 | iter->tags |= node->tags[tag][tag_long + 1] << |
| 1481 | (BITS_PER_LONG - tag_bit); |
| 1482 | /* Clip chunk size, here only BITS_PER_LONG tags */ |
| 1483 | iter->next_index = __radix_tree_iter_add(iter, BITS_PER_LONG); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | #ifdef CONFIG_RADIX_TREE_MULTIORDER |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1488 | static void __rcu **skip_siblings(struct radix_tree_node **nodep, |
| 1489 | void __rcu **slot, struct radix_tree_iter *iter) |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1490 | { |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1491 | while (iter->index < iter->next_index) { |
| 1492 | *nodep = rcu_dereference_raw(*slot); |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 1493 | if (*nodep && !xa_is_sibling(*nodep)) |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1494 | return slot; |
| 1495 | slot++; |
| 1496 | iter->index = __radix_tree_iter_add(iter, 1); |
| 1497 | iter->tags >>= 1; |
| 1498 | } |
| 1499 | |
| 1500 | *nodep = NULL; |
| 1501 | return NULL; |
| 1502 | } |
| 1503 | |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1504 | void __rcu **__radix_tree_next_slot(void __rcu **slot, |
| 1505 | struct radix_tree_iter *iter, unsigned flags) |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1506 | { |
| 1507 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; |
Ross Zwisler | 9f41822 | 2018-05-18 16:09:06 -0700 | [diff] [blame] | 1508 | struct radix_tree_node *node; |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1509 | |
| 1510 | slot = skip_siblings(&node, slot, iter); |
| 1511 | |
| 1512 | while (radix_tree_is_internal_node(node)) { |
| 1513 | unsigned offset; |
| 1514 | unsigned long next_index; |
| 1515 | |
| 1516 | if (node == RADIX_TREE_RETRY) |
| 1517 | return slot; |
| 1518 | node = entry_to_node(node); |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 1519 | iter->node = node; |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1520 | iter->shift = node->shift; |
| 1521 | |
| 1522 | if (flags & RADIX_TREE_ITER_TAGGED) { |
| 1523 | offset = radix_tree_find_next_bit(node, tag, 0); |
| 1524 | if (offset == RADIX_TREE_MAP_SIZE) |
| 1525 | return NULL; |
| 1526 | slot = &node->slots[offset]; |
| 1527 | iter->index = __radix_tree_iter_add(iter, offset); |
| 1528 | set_iter_tags(iter, node, offset, tag); |
| 1529 | node = rcu_dereference_raw(*slot); |
| 1530 | } else { |
| 1531 | offset = 0; |
| 1532 | slot = &node->slots[0]; |
| 1533 | for (;;) { |
| 1534 | node = rcu_dereference_raw(*slot); |
| 1535 | if (node) |
| 1536 | break; |
| 1537 | slot++; |
| 1538 | offset++; |
| 1539 | if (offset == RADIX_TREE_MAP_SIZE) |
| 1540 | return NULL; |
| 1541 | } |
| 1542 | iter->index = __radix_tree_iter_add(iter, offset); |
| 1543 | } |
| 1544 | if ((flags & RADIX_TREE_ITER_CONTIG) && (offset > 0)) |
| 1545 | goto none; |
| 1546 | next_index = (iter->index | shift_maxindex(iter->shift)) + 1; |
| 1547 | if (next_index < iter->next_index) |
| 1548 | iter->next_index = next_index; |
| 1549 | } |
| 1550 | |
| 1551 | return slot; |
| 1552 | none: |
| 1553 | iter->next_index = 0; |
| 1554 | return NULL; |
| 1555 | } |
| 1556 | EXPORT_SYMBOL(__radix_tree_next_slot); |
| 1557 | #else |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1558 | static void __rcu **skip_siblings(struct radix_tree_node **nodep, |
| 1559 | void __rcu **slot, struct radix_tree_iter *iter) |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1560 | { |
| 1561 | return slot; |
| 1562 | } |
| 1563 | #endif |
| 1564 | |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1565 | void __rcu **radix_tree_iter_resume(void __rcu **slot, |
| 1566 | struct radix_tree_iter *iter) |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1567 | { |
| 1568 | struct radix_tree_node *node; |
| 1569 | |
| 1570 | slot++; |
| 1571 | iter->index = __radix_tree_iter_add(iter, 1); |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1572 | skip_siblings(&node, slot, iter); |
| 1573 | iter->next_index = iter->index; |
| 1574 | iter->tags = 0; |
| 1575 | return NULL; |
| 1576 | } |
| 1577 | EXPORT_SYMBOL(radix_tree_iter_resume); |
| 1578 | |
Fengguang Wu | 6df8ba4 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 1579 | /** |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1580 | * radix_tree_next_chunk - find next chunk of slots for iteration |
| 1581 | * |
| 1582 | * @root: radix tree root |
| 1583 | * @iter: iterator state |
| 1584 | * @flags: RADIX_TREE_ITER_* flags and tag index |
| 1585 | * Returns: pointer to chunk first slot, or NULL if iteration is over |
| 1586 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1587 | void __rcu **radix_tree_next_chunk(const struct radix_tree_root *root, |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1588 | struct radix_tree_iter *iter, unsigned flags) |
| 1589 | { |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1590 | unsigned tag = flags & RADIX_TREE_ITER_TAG_MASK; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1591 | struct radix_tree_node *node, *child; |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1592 | unsigned long index, offset, maxindex; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1593 | |
| 1594 | if ((flags & RADIX_TREE_ITER_TAGGED) && !root_tag_get(root, tag)) |
| 1595 | return NULL; |
| 1596 | |
| 1597 | /* |
| 1598 | * Catch next_index overflow after ~0UL. iter->index never overflows |
| 1599 | * during iterating; it can be zero only at the beginning. |
| 1600 | * And we cannot overflow iter->next_index in a single step, |
| 1601 | * because RADIX_TREE_MAP_SHIFT < BITS_PER_LONG. |
Konstantin Khlebnikov | fffaee3 | 2012-06-05 21:36:33 +0400 | [diff] [blame] | 1602 | * |
| 1603 | * This condition also used by radix_tree_next_slot() to stop |
Matthew Wilcox | 91b9677c | 2016-12-14 15:08:31 -0800 | [diff] [blame] | 1604 | * contiguous iterating, and forbid switching to the next chunk. |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1605 | */ |
| 1606 | index = iter->next_index; |
| 1607 | if (!index && iter->index) |
| 1608 | return NULL; |
| 1609 | |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1610 | restart: |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1611 | radix_tree_load_root(root, &child, &maxindex); |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1612 | if (index > maxindex) |
| 1613 | return NULL; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1614 | if (!child) |
| 1615 | return NULL; |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1616 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1617 | if (!radix_tree_is_internal_node(child)) { |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1618 | /* Single-slot tree */ |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1619 | iter->index = index; |
| 1620 | iter->next_index = maxindex + 1; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1621 | iter->tags = 1; |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 1622 | iter->node = NULL; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1623 | __set_iter_shift(iter, 0); |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1624 | return (void __rcu **)&root->xa_head; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1625 | } |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1626 | |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1627 | do { |
| 1628 | node = entry_to_node(child); |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1629 | offset = radix_tree_descend(node, &child, index); |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1630 | |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1631 | if ((flags & RADIX_TREE_ITER_TAGGED) ? |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1632 | !tag_get(node, tag, offset) : !child) { |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1633 | /* Hole detected */ |
| 1634 | if (flags & RADIX_TREE_ITER_CONTIG) |
| 1635 | return NULL; |
| 1636 | |
| 1637 | if (flags & RADIX_TREE_ITER_TAGGED) |
Matthew Wilcox | bc412fc | 2016-12-14 15:08:40 -0800 | [diff] [blame] | 1638 | offset = radix_tree_find_next_bit(node, tag, |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1639 | offset + 1); |
| 1640 | else |
| 1641 | while (++offset < RADIX_TREE_MAP_SIZE) { |
Matthew Wilcox | 12320d0 | 2017-02-13 15:22:48 -0500 | [diff] [blame] | 1642 | void *slot = rcu_dereference_raw( |
| 1643 | node->slots[offset]); |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 1644 | if (xa_is_sibling(slot)) |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 1645 | continue; |
| 1646 | if (slot) |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1647 | break; |
| 1648 | } |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1649 | index &= ~node_maxindex(node); |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1650 | index += offset << node->shift; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1651 | /* Overflow after ~0UL */ |
| 1652 | if (!index) |
| 1653 | return NULL; |
| 1654 | if (offset == RADIX_TREE_MAP_SIZE) |
| 1655 | goto restart; |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1656 | child = rcu_dereference_raw(node->slots[offset]); |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1657 | } |
| 1658 | |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1659 | if (!child) |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1660 | goto restart; |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 1661 | if (child == RADIX_TREE_RETRY) |
| 1662 | break; |
Matthew Wilcox | 66ee620 | 2018-06-25 06:56:50 -0400 | [diff] [blame] | 1663 | } while (node->shift && radix_tree_is_internal_node(child)); |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1664 | |
| 1665 | /* Update the iterator state */ |
Matthew Wilcox | 8c1244d | 2016-05-20 17:03:36 -0700 | [diff] [blame] | 1666 | iter->index = (index &~ node_maxindex(node)) | (offset << node->shift); |
| 1667 | iter->next_index = (index | node_maxindex(node)) + 1; |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 1668 | iter->node = node; |
Matthew Wilcox | 9e85d81 | 2016-05-20 17:03:48 -0700 | [diff] [blame] | 1669 | __set_iter_shift(iter, node->shift); |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1670 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 1671 | if (flags & RADIX_TREE_ITER_TAGGED) |
| 1672 | set_iter_tags(iter, node, offset, tag); |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1673 | |
| 1674 | return node->slots + offset; |
| 1675 | } |
| 1676 | EXPORT_SYMBOL(radix_tree_next_chunk); |
| 1677 | |
| 1678 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | * radix_tree_gang_lookup - perform multiple lookup on a radix tree |
| 1680 | * @root: radix tree root |
| 1681 | * @results: where the results of the lookup are placed |
| 1682 | * @first_index: start the lookup from this key |
| 1683 | * @max_items: place up to this many items at *results |
| 1684 | * |
| 1685 | * Performs an index-ascending scan of the tree for present items. Places |
| 1686 | * them at *@results and returns the number of items which were placed at |
| 1687 | * *@results. |
| 1688 | * |
| 1689 | * The implementation is naive. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 1690 | * |
| 1691 | * Like radix_tree_lookup, radix_tree_gang_lookup may be called under |
| 1692 | * rcu_read_lock. In this case, rather than the returned results being |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 1693 | * an atomic snapshot of the tree at a single point in time, the |
| 1694 | * semantics of an RCU protected gang lookup are as though multiple |
| 1695 | * radix_tree_lookups have been issued in individual locks, and results |
| 1696 | * stored in 'results'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | */ |
| 1698 | unsigned int |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 1699 | radix_tree_gang_lookup(const struct radix_tree_root *root, void **results, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | unsigned long first_index, unsigned int max_items) |
| 1701 | { |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1702 | struct radix_tree_iter iter; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1703 | void __rcu **slot; |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1704 | unsigned int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1706 | if (unlikely(!max_items)) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 1707 | return 0; |
| 1708 | |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1709 | radix_tree_for_each_slot(slot, root, &iter, first_index) { |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 1710 | results[ret] = rcu_dereference_raw(*slot); |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1711 | if (!results[ret]) |
| 1712 | continue; |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 1713 | if (radix_tree_is_internal_node(results[ret])) { |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 1714 | slot = radix_tree_iter_retry(&iter); |
| 1715 | continue; |
| 1716 | } |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1717 | if (++ret == max_items) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 1720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | return ret; |
| 1722 | } |
| 1723 | EXPORT_SYMBOL(radix_tree_gang_lookup); |
| 1724 | |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 1725 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | * radix_tree_gang_lookup_tag - perform multiple lookup on a radix tree |
| 1727 | * based on a tag |
| 1728 | * @root: radix tree root |
| 1729 | * @results: where the results of the lookup are placed |
| 1730 | * @first_index: start the lookup from this key |
| 1731 | * @max_items: place up to this many items at *results |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1732 | * @tag: the tag index (< RADIX_TREE_MAX_TAGS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | * |
| 1734 | * Performs an index-ascending scan of the tree for present items which |
| 1735 | * have the tag indexed by @tag set. Places the items at *@results and |
| 1736 | * returns the number of items which were placed at *@results. |
| 1737 | */ |
| 1738 | unsigned int |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 1739 | radix_tree_gang_lookup_tag(const struct radix_tree_root *root, void **results, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 1740 | unsigned long first_index, unsigned int max_items, |
| 1741 | unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | { |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1743 | struct radix_tree_iter iter; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1744 | void __rcu **slot; |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1745 | unsigned int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1747 | if (unlikely(!max_items)) |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 1748 | return 0; |
| 1749 | |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1750 | radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) { |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 1751 | results[ret] = rcu_dereference_raw(*slot); |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1752 | if (!results[ret]) |
| 1753 | continue; |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 1754 | if (radix_tree_is_internal_node(results[ret])) { |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 1755 | slot = radix_tree_iter_retry(&iter); |
| 1756 | continue; |
| 1757 | } |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1758 | if (++ret == max_items) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1760 | } |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 1761 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | return ret; |
| 1763 | } |
| 1764 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag); |
| 1765 | |
| 1766 | /** |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 1767 | * radix_tree_gang_lookup_tag_slot - perform multiple slot lookup on a |
| 1768 | * radix tree based on a tag |
| 1769 | * @root: radix tree root |
| 1770 | * @results: where the results of the lookup are placed |
| 1771 | * @first_index: start the lookup from this key |
| 1772 | * @max_items: place up to this many items at *results |
| 1773 | * @tag: the tag index (< RADIX_TREE_MAX_TAGS) |
| 1774 | * |
| 1775 | * Performs an index-ascending scan of the tree for present items which |
| 1776 | * have the tag indexed by @tag set. Places the slots at *@results and |
| 1777 | * returns the number of slots which were placed at *@results. |
| 1778 | */ |
| 1779 | unsigned int |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 1780 | radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *root, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1781 | void __rcu ***results, unsigned long first_index, |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 1782 | unsigned int max_items, unsigned int tag) |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 1783 | { |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1784 | struct radix_tree_iter iter; |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1785 | void __rcu **slot; |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1786 | unsigned int ret = 0; |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 1787 | |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1788 | if (unlikely(!max_items)) |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 1789 | return 0; |
| 1790 | |
Konstantin Khlebnikov | cebbd29 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 1791 | radix_tree_for_each_tagged(slot, root, &iter, first_index, tag) { |
| 1792 | results[ret] = slot; |
| 1793 | if (++ret == max_items) |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 1794 | break; |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | return ret; |
| 1798 | } |
| 1799 | EXPORT_SYMBOL(radix_tree_gang_lookup_tag_slot); |
| 1800 | |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1801 | static bool __radix_tree_delete(struct radix_tree_root *root, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1802 | struct radix_tree_node *node, void __rcu **slot) |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1803 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1804 | void *old = rcu_dereference_raw(*slot); |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1805 | int values = xa_is_value(old) ? -1 : 0; |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1806 | unsigned offset = get_slot_offset(node, slot); |
| 1807 | int tag; |
| 1808 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1809 | if (is_idr(root)) |
| 1810 | node_tag_set(root, node, IDR_FREE, offset); |
| 1811 | else |
| 1812 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 1813 | node_tag_clear(root, node, tag, offset); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1814 | |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 1815 | replace_slot(slot, NULL, node, -1, values); |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame^] | 1816 | return node && delete_node(root, node); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1817 | } |
| 1818 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 1819 | /** |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1820 | * radix_tree_iter_delete - delete the entry at this iterator position |
| 1821 | * @root: radix tree root |
| 1822 | * @iter: iterator state |
| 1823 | * @slot: pointer to slot |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | * |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1825 | * Delete the entry at the position currently pointed to by the iterator. |
| 1826 | * This may result in the current node being freed; if it is, the iterator |
| 1827 | * is advanced so that it will not reference the freed memory. This |
| 1828 | * function may be called without any locking if there are no other threads |
| 1829 | * which can access this tree. |
| 1830 | */ |
| 1831 | void radix_tree_iter_delete(struct radix_tree_root *root, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1832 | struct radix_tree_iter *iter, void __rcu **slot) |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1833 | { |
| 1834 | if (__radix_tree_delete(root, iter->node, slot)) |
| 1835 | iter->index = iter->next_index; |
| 1836 | } |
Chris Wilson | d1b48c1 | 2017-08-16 09:52:08 +0100 | [diff] [blame] | 1837 | EXPORT_SYMBOL(radix_tree_iter_delete); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1838 | |
| 1839 | /** |
| 1840 | * radix_tree_delete_item - delete an item from a radix tree |
| 1841 | * @root: radix tree root |
| 1842 | * @index: index key |
| 1843 | * @item: expected item |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | * |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1845 | * Remove @item at @index from the radix tree rooted at @root. |
| 1846 | * |
| 1847 | * Return: the deleted entry, or %NULL if it was not present |
| 1848 | * or the entry at the given @index was not @item. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | */ |
Johannes Weiner | 53c59f2 | 2014-04-03 14:47:39 -0700 | [diff] [blame] | 1850 | void *radix_tree_delete_item(struct radix_tree_root *root, |
| 1851 | unsigned long index, void *item) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1853 | struct radix_tree_node *node = NULL; |
Matthew Wilcox | 7a4deea | 2018-05-25 14:47:24 -0700 | [diff] [blame] | 1854 | void __rcu **slot = NULL; |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 1855 | void *entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1856 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 1857 | entry = __radix_tree_lookup(root, index, &node, &slot); |
Matthew Wilcox | 7a4deea | 2018-05-25 14:47:24 -0700 | [diff] [blame] | 1858 | if (!slot) |
| 1859 | return NULL; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1860 | if (!entry && (!is_idr(root) || node_tag_get(root, node, IDR_FREE, |
| 1861 | get_slot_offset(node, slot)))) |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 1862 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 1864 | if (item && entry != item) |
| 1865 | return NULL; |
| 1866 | |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1867 | __radix_tree_delete(root, node, slot); |
Christoph Lameter | 201b626 | 2005-09-06 15:16:46 -0700 | [diff] [blame] | 1868 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 1869 | return entry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } |
Johannes Weiner | 53c59f2 | 2014-04-03 14:47:39 -0700 | [diff] [blame] | 1871 | EXPORT_SYMBOL(radix_tree_delete_item); |
| 1872 | |
| 1873 | /** |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1874 | * radix_tree_delete - delete an entry from a radix tree |
| 1875 | * @root: radix tree root |
| 1876 | * @index: index key |
Johannes Weiner | 53c59f2 | 2014-04-03 14:47:39 -0700 | [diff] [blame] | 1877 | * |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1878 | * Remove the entry at @index from the radix tree rooted at @root. |
Johannes Weiner | 53c59f2 | 2014-04-03 14:47:39 -0700 | [diff] [blame] | 1879 | * |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 1880 | * Return: The deleted entry, or %NULL if it was not present. |
Johannes Weiner | 53c59f2 | 2014-04-03 14:47:39 -0700 | [diff] [blame] | 1881 | */ |
| 1882 | void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) |
| 1883 | { |
| 1884 | return radix_tree_delete_item(root, index, NULL); |
| 1885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1886 | EXPORT_SYMBOL(radix_tree_delete); |
| 1887 | |
Johannes Weiner | d3798ae | 2016-10-04 22:02:08 +0200 | [diff] [blame] | 1888 | void radix_tree_clear_tags(struct radix_tree_root *root, |
| 1889 | struct radix_tree_node *node, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 1890 | void __rcu **slot) |
Matthew Wilcox | d604c32 | 2016-05-20 17:03:45 -0700 | [diff] [blame] | 1891 | { |
Matthew Wilcox | d604c32 | 2016-05-20 17:03:45 -0700 | [diff] [blame] | 1892 | if (node) { |
| 1893 | unsigned int tag, offset = get_slot_offset(node, slot); |
| 1894 | for (tag = 0; tag < RADIX_TREE_MAX_TAGS; tag++) |
| 1895 | node_tag_clear(root, node, tag, offset); |
| 1896 | } else { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1897 | root_tag_clear_all(root); |
Matthew Wilcox | d604c32 | 2016-05-20 17:03:45 -0700 | [diff] [blame] | 1898 | } |
Matthew Wilcox | d604c32 | 2016-05-20 17:03:45 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | /** |
| 1902 | * radix_tree_tagged - test whether any items in the tree are tagged |
| 1903 | * @root: radix tree root |
| 1904 | * @tag: tag to test |
| 1905 | */ |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 1906 | int radix_tree_tagged(const struct radix_tree_root *root, unsigned int tag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | { |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 1908 | return root_tag_get(root, tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | EXPORT_SYMBOL(radix_tree_tagged); |
| 1911 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1912 | /** |
| 1913 | * idr_preload - preload for idr_alloc() |
| 1914 | * @gfp_mask: allocation mask to use for preloading |
| 1915 | * |
| 1916 | * Preallocate memory to use for the next call to idr_alloc(). This function |
| 1917 | * returns with preemption disabled. It will be enabled by idr_preload_end(). |
| 1918 | */ |
| 1919 | void idr_preload(gfp_t gfp_mask) |
| 1920 | { |
Eric Dumazet | bc9ae22 | 2017-09-08 16:15:54 -0700 | [diff] [blame] | 1921 | if (__radix_tree_preload(gfp_mask, IDR_PRELOAD_SIZE)) |
| 1922 | preempt_disable(); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1923 | } |
| 1924 | EXPORT_SYMBOL(idr_preload); |
| 1925 | |
Matthew Wilcox | 460488c | 2017-11-28 15:16:24 -0500 | [diff] [blame] | 1926 | void __rcu **idr_get_free(struct radix_tree_root *root, |
Chris Mi | 388f79f | 2017-08-30 02:31:57 -0400 | [diff] [blame] | 1927 | struct radix_tree_iter *iter, gfp_t gfp, |
| 1928 | unsigned long max) |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1929 | { |
| 1930 | struct radix_tree_node *node = NULL, *child; |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1931 | void __rcu **slot = (void __rcu **)&root->xa_head; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1932 | unsigned long maxindex, start = iter->next_index; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1933 | unsigned int shift, offset = 0; |
| 1934 | |
| 1935 | grow: |
| 1936 | shift = radix_tree_load_root(root, &child, &maxindex); |
| 1937 | if (!radix_tree_tagged(root, IDR_FREE)) |
| 1938 | start = max(start, maxindex + 1); |
| 1939 | if (start > max) |
| 1940 | return ERR_PTR(-ENOSPC); |
| 1941 | |
| 1942 | if (start > maxindex) { |
| 1943 | int error = radix_tree_extend(root, gfp, start, shift); |
| 1944 | if (error < 0) |
| 1945 | return ERR_PTR(error); |
| 1946 | shift = error; |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 1947 | child = rcu_dereference_raw(root->xa_head); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1948 | } |
Matthew Wilcox | 66ee620 | 2018-06-25 06:56:50 -0400 | [diff] [blame] | 1949 | if (start == 0 && shift == 0) |
| 1950 | shift = RADIX_TREE_MAP_SHIFT; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1951 | |
| 1952 | while (shift) { |
| 1953 | shift -= RADIX_TREE_MAP_SHIFT; |
| 1954 | if (child == NULL) { |
| 1955 | /* Have to add a child node. */ |
Matthew Wilcox | d58275b | 2017-01-16 17:10:21 -0500 | [diff] [blame] | 1956 | child = radix_tree_node_alloc(gfp, node, root, shift, |
| 1957 | offset, 0, 0); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 1958 | if (!child) |
| 1959 | return ERR_PTR(-ENOMEM); |
| 1960 | all_tag_set(child, IDR_FREE); |
| 1961 | rcu_assign_pointer(*slot, node_to_entry(child)); |
| 1962 | if (node) |
| 1963 | node->count++; |
| 1964 | } else if (!radix_tree_is_internal_node(child)) |
| 1965 | break; |
| 1966 | |
| 1967 | node = entry_to_node(child); |
| 1968 | offset = radix_tree_descend(node, &child, start); |
| 1969 | if (!tag_get(node, IDR_FREE, offset)) { |
| 1970 | offset = radix_tree_find_next_bit(node, IDR_FREE, |
| 1971 | offset + 1); |
| 1972 | start = next_index(start, node, offset); |
| 1973 | if (start > max) |
| 1974 | return ERR_PTR(-ENOSPC); |
| 1975 | while (offset == RADIX_TREE_MAP_SIZE) { |
| 1976 | offset = node->offset + 1; |
| 1977 | node = node->parent; |
| 1978 | if (!node) |
| 1979 | goto grow; |
| 1980 | shift = node->shift; |
| 1981 | } |
| 1982 | child = rcu_dereference_raw(node->slots[offset]); |
| 1983 | } |
| 1984 | slot = &node->slots[offset]; |
| 1985 | } |
| 1986 | |
| 1987 | iter->index = start; |
| 1988 | if (node) |
| 1989 | iter->next_index = 1 + min(max, (start | node_maxindex(node))); |
| 1990 | else |
| 1991 | iter->next_index = 1; |
| 1992 | iter->node = node; |
| 1993 | __set_iter_shift(iter, shift); |
| 1994 | set_iter_tags(iter, node, offset, IDR_FREE); |
| 1995 | |
| 1996 | return slot; |
| 1997 | } |
| 1998 | |
| 1999 | /** |
| 2000 | * idr_destroy - release all internal memory from an IDR |
| 2001 | * @idr: idr handle |
| 2002 | * |
| 2003 | * After this function is called, the IDR is empty, and may be reused or |
| 2004 | * the data structure containing it may be freed. |
| 2005 | * |
| 2006 | * A typical clean-up sequence for objects stored in an idr tree will use |
| 2007 | * idr_for_each() to free all objects, if necessary, then idr_destroy() to |
| 2008 | * free the memory used to keep track of those objects. |
| 2009 | */ |
| 2010 | void idr_destroy(struct idr *idr) |
| 2011 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 2012 | struct radix_tree_node *node = rcu_dereference_raw(idr->idr_rt.xa_head); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 2013 | if (radix_tree_is_internal_node(node)) |
| 2014 | radix_tree_free_nodes(node); |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 2015 | idr->idr_rt.xa_head = NULL; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 2016 | root_tag_set(&idr->idr_rt, IDR_FREE); |
| 2017 | } |
| 2018 | EXPORT_SYMBOL(idr_destroy); |
| 2019 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | static void |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 2021 | radix_tree_node_ctor(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | { |
Johannes Weiner | 449dd69 | 2014-04-03 14:47:56 -0700 | [diff] [blame] | 2023 | struct radix_tree_node *node = arg; |
| 2024 | |
| 2025 | memset(node, 0, sizeof(*node)); |
| 2026 | INIT_LIST_HEAD(&node->private_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | } |
| 2028 | |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 2029 | static __init unsigned long __maxindex(unsigned int height) |
| 2030 | { |
| 2031 | unsigned int width = height * RADIX_TREE_MAP_SHIFT; |
| 2032 | int shift = RADIX_TREE_INDEX_BITS - width; |
| 2033 | |
| 2034 | if (shift < 0) |
| 2035 | return ~0UL; |
| 2036 | if (shift >= BITS_PER_LONG) |
| 2037 | return 0UL; |
| 2038 | return ~0UL >> shift; |
| 2039 | } |
| 2040 | |
| 2041 | static __init void radix_tree_init_maxnodes(void) |
| 2042 | { |
| 2043 | unsigned long height_to_maxindex[RADIX_TREE_MAX_PATH + 1]; |
| 2044 | unsigned int i, j; |
| 2045 | |
| 2046 | for (i = 0; i < ARRAY_SIZE(height_to_maxindex); i++) |
| 2047 | height_to_maxindex[i] = __maxindex(i); |
| 2048 | for (i = 0; i < ARRAY_SIZE(height_to_maxnodes); i++) { |
| 2049 | for (j = i; j > 0; j--) |
| 2050 | height_to_maxnodes[i] += height_to_maxindex[j - 1] + 1; |
| 2051 | } |
| 2052 | } |
| 2053 | |
Sebastian Andrzej Siewior | d544abd | 2016-11-03 15:50:01 +0100 | [diff] [blame] | 2054 | static int radix_tree_cpu_dead(unsigned int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | { |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 2056 | struct radix_tree_preload *rtp; |
| 2057 | struct radix_tree_node *node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 2059 | /* Free per-cpu pool of preloaded nodes */ |
Sebastian Andrzej Siewior | d544abd | 2016-11-03 15:50:01 +0100 | [diff] [blame] | 2060 | rtp = &per_cpu(radix_tree_preloads, cpu); |
| 2061 | while (rtp->nr) { |
| 2062 | node = rtp->nodes; |
Matthew Wilcox | 1293d5c | 2017-01-16 16:41:29 -0500 | [diff] [blame] | 2063 | rtp->nodes = node->parent; |
Sebastian Andrzej Siewior | d544abd | 2016-11-03 15:50:01 +0100 | [diff] [blame] | 2064 | kmem_cache_free(radix_tree_node_cachep, node); |
| 2065 | rtp->nr--; |
Matthew Wilcox | 2fcd900 | 2016-05-20 17:03:04 -0700 | [diff] [blame] | 2066 | } |
Sebastian Andrzej Siewior | d544abd | 2016-11-03 15:50:01 +0100 | [diff] [blame] | 2067 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
| 2070 | void __init radix_tree_init(void) |
| 2071 | { |
Sebastian Andrzej Siewior | d544abd | 2016-11-03 15:50:01 +0100 | [diff] [blame] | 2072 | int ret; |
Michal Hocko | 7e78442 | 2017-05-03 14:53:09 -0700 | [diff] [blame] | 2073 | |
| 2074 | BUILD_BUG_ON(RADIX_TREE_MAX_TAGS + __GFP_BITS_SHIFT > 32); |
Matthew Wilcox | fa290cd | 2018-04-10 16:36:28 -0700 | [diff] [blame] | 2075 | BUILD_BUG_ON(ROOT_IS_IDR & ~GFP_ZONEMASK); |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 2076 | BUILD_BUG_ON(XA_CHUNK_SIZE > 255); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | radix_tree_node_cachep = kmem_cache_create("radix_tree_node", |
| 2078 | sizeof(struct radix_tree_node), 0, |
Christoph Lameter | 488514d | 2008-04-28 02:12:05 -0700 | [diff] [blame] | 2079 | SLAB_PANIC | SLAB_RECLAIM_ACCOUNT, |
| 2080 | radix_tree_node_ctor); |
Kirill A. Shutemov | c78c66d | 2016-07-26 15:26:02 -0700 | [diff] [blame] | 2081 | radix_tree_init_maxnodes(); |
Sebastian Andrzej Siewior | d544abd | 2016-11-03 15:50:01 +0100 | [diff] [blame] | 2082 | ret = cpuhp_setup_state_nocalls(CPUHP_RADIX_DEAD, "lib/radix:dead", |
| 2083 | NULL, radix_tree_cpu_dead); |
| 2084 | WARN_ON(ret < 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | } |