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