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