Thomas Gleixner | de6cc65 | 2019-05-27 08:55:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2001 Momchil Velikov |
| 4 | * Portions Copyright (C) 2001 Christoph Hellwig |
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 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | #ifndef _LINUX_RADIX_TREE_H |
| 9 | #define _LINUX_RADIX_TREE_H |
| 10 | |
Matthew Wilcox | f67c07f | 2016-03-17 14:21:42 -0700 | [diff] [blame] | 11 | #include <linux/bitops.h> |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 12 | #include <linux/kernel.h> |
Matthew Wilcox | 15f2e88d | 2016-12-16 14:46:09 -0500 | [diff] [blame] | 13 | #include <linux/list.h> |
| 14 | #include <linux/preempt.h> |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 15 | #include <linux/rcupdate.h> |
Matthew Wilcox | 15f2e88d | 2016-12-16 14:46:09 -0500 | [diff] [blame] | 16 | #include <linux/spinlock.h> |
| 17 | #include <linux/types.h> |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 18 | #include <linux/xarray.h> |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 19 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 20 | /* Keep unconverted code working */ |
| 21 | #define radix_tree_root xarray |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 22 | #define radix_tree_node xa_node |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 23 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 24 | /* |
Matthew Wilcox | 3bcadd6 | 2016-05-20 17:03:54 -0700 | [diff] [blame] | 25 | * The bottom two bits of the slot determine how the remaining bits in the |
| 26 | * slot are interpreted: |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 27 | * |
Matthew Wilcox | 3bcadd6 | 2016-05-20 17:03:54 -0700 | [diff] [blame] | 28 | * 00 - data pointer |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 29 | * 10 - internal entry |
| 30 | * x1 - value entry |
Matthew Wilcox | 3bcadd6 | 2016-05-20 17:03:54 -0700 | [diff] [blame] | 31 | * |
| 32 | * The internal entry may be a pointer to the next level in the tree, a |
| 33 | * sibling entry, or an indicator that the entry in this slot has been moved |
| 34 | * to another location in the tree and the lookup should be restarted. While |
| 35 | * NULL fits the 'data pointer' pattern, it means that there is no entry in |
| 36 | * the tree for this index (no matter what level of the tree it is found at). |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 37 | * This means that storing a NULL entry in the tree is the same as deleting |
| 38 | * the entry from the tree. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 39 | */ |
Matthew Wilcox | 3bcadd6 | 2016-05-20 17:03:54 -0700 | [diff] [blame] | 40 | #define RADIX_TREE_ENTRY_MASK 3UL |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 41 | #define RADIX_TREE_INTERNAL_NODE 2UL |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 42 | |
Matthew Wilcox | 3bcadd6 | 2016-05-20 17:03:54 -0700 | [diff] [blame] | 43 | static inline bool radix_tree_is_internal_node(void *ptr) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 44 | { |
Matthew Wilcox | 3bcadd6 | 2016-05-20 17:03:54 -0700 | [diff] [blame] | 45 | return ((unsigned long)ptr & RADIX_TREE_ENTRY_MASK) == |
| 46 | RADIX_TREE_INTERNAL_NODE; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /*** radix-tree API starts here ***/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Matthew Wilcox | 02c02bf | 2017-11-03 23:09:45 -0400 | [diff] [blame] | 51 | #define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 52 | #define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT) |
| 53 | #define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1) |
| 54 | |
Matthew Wilcox | 01959df | 2017-11-09 09:23:56 -0500 | [diff] [blame] | 55 | #define RADIX_TREE_MAX_TAGS XA_MAX_MARKS |
| 56 | #define RADIX_TREE_TAG_LONGS XA_MARK_LONGS |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 57 | |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 58 | #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long)) |
| 59 | #define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \ |
| 60 | RADIX_TREE_MAP_SHIFT)) |
| 61 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 62 | /* The IDR tag is stored in the low bits of xa_flags */ |
Matthew Wilcox | fa290cd | 2018-04-10 16:36:28 -0700 | [diff] [blame] | 63 | #define ROOT_IS_IDR ((__force gfp_t)4) |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 64 | /* The top bits of xa_flags are used to store the root tags */ |
Matthew Wilcox | fa290cd | 2018-04-10 16:36:28 -0700 | [diff] [blame] | 65 | #define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT) |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 66 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 67 | #define RADIX_TREE_INIT(name, mask) XARRAY_INIT(name, mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | #define RADIX_TREE(name, mask) \ |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 70 | struct radix_tree_root name = RADIX_TREE_INIT(name, mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 72 | #define INIT_RADIX_TREE(root, mask) xa_init_flags(root, mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 74 | static inline bool radix_tree_empty(const struct radix_tree_root *root) |
Matthew Wilcox | e9256ef | 2016-05-20 17:01:33 -0700 | [diff] [blame] | 75 | { |
Matthew Wilcox | f8d5d0c | 2017-11-07 16:30:10 -0500 | [diff] [blame] | 76 | return root->xa_head == NULL; |
Matthew Wilcox | e9256ef | 2016-05-20 17:01:33 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 79 | /** |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 80 | * struct radix_tree_iter - radix tree iterator state |
| 81 | * |
| 82 | * @index: index of current slot |
| 83 | * @next_index: one beyond the last index for this chunk |
| 84 | * @tags: bit-mask for tag-iterating |
| 85 | * @node: node that contains current slot |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 86 | * |
| 87 | * This radix tree iterator works in terms of "chunks" of slots. A chunk is a |
| 88 | * subinterval of slots contained within one radix tree leaf node. It is |
| 89 | * described by a pointer to its first slot and a struct radix_tree_iter |
| 90 | * which holds the chunk's position in the tree and its size. For tagged |
| 91 | * iteration radix_tree_iter also holds the slots' bit-mask for one chosen |
| 92 | * radix tree tag. |
| 93 | */ |
| 94 | struct radix_tree_iter { |
| 95 | unsigned long index; |
| 96 | unsigned long next_index; |
| 97 | unsigned long tags; |
| 98 | struct radix_tree_node *node; |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 99 | }; |
| 100 | |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 101 | /** |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 102 | * Radix-tree synchronization |
| 103 | * |
| 104 | * The radix-tree API requires that users provide all synchronisation (with |
| 105 | * specific exceptions, noted below). |
| 106 | * |
| 107 | * Synchronization of access to the data items being stored in the tree, and |
| 108 | * management of their lifetimes must be completely managed by API users. |
| 109 | * |
| 110 | * For API usage, in general, |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 111 | * - any function _modifying_ the tree or tags (inserting or deleting |
Tim Pepper | eb8dc5e | 2008-02-03 16:12:47 +0200 | [diff] [blame] | 112 | * items, setting or clearing tags) must exclude other modifications, and |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 113 | * exclude any functions reading the tree. |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 114 | * - any function _reading_ the tree or tags (looking up items or tags, |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 115 | * gang lookups) must exclude modifications to the tree, but may occur |
| 116 | * concurrently with other readers. |
| 117 | * |
| 118 | * The notable exceptions to this rule are the following functions: |
Johannes Weiner | 139e561 | 2014-04-03 14:47:54 -0700 | [diff] [blame] | 119 | * __radix_tree_lookup |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 120 | * radix_tree_lookup |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 121 | * radix_tree_lookup_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 122 | * radix_tree_tag_get |
| 123 | * radix_tree_gang_lookup |
| 124 | * radix_tree_gang_lookup_tag |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 125 | * radix_tree_gang_lookup_tag_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 126 | * radix_tree_tagged |
| 127 | * |
Matthew Wilcox | 7b8d046 | 2017-12-01 22:13:06 -0500 | [diff] [blame] | 128 | * The first 7 functions are able to be called locklessly, using RCU. The |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 129 | * caller must ensure calls to these functions are made within rcu_read_lock() |
| 130 | * regions. Other readers (lock-free or otherwise) and modifications may be |
| 131 | * running concurrently. |
| 132 | * |
| 133 | * It is still required that the caller manage the synchronization and lifetimes |
| 134 | * of the items. So if RCU lock-free lookups are used, typically this would mean |
| 135 | * that the items have their own locks, or are amenable to lock-free access; and |
| 136 | * that the items are freed by RCU (or only freed after having been deleted from |
| 137 | * the radix tree *and* a synchronize_rcu() grace period). |
| 138 | * |
| 139 | * (Note, rcu_assign_pointer and rcu_dereference are not needed to control |
| 140 | * access to data items when inserting into or looking up from the radix tree) |
| 141 | * |
David Howells | ce82653 | 2010-04-06 22:36:20 +0100 | [diff] [blame] | 142 | * Note that the value returned by radix_tree_tag_get() may not be relied upon |
| 143 | * if only the RCU read lock is held. Functions to set/clear tags and to |
| 144 | * delete nodes running concurrently with it may affect its result such that |
| 145 | * two consecutive reads in the same locked section may return different |
| 146 | * values. If reliability is required, modification functions must also be |
| 147 | * excluded from concurrency. |
| 148 | * |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 149 | * radix_tree_tagged is able to be called without locking or RCU. |
| 150 | */ |
| 151 | |
| 152 | /** |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 153 | * radix_tree_deref_slot - dereference a slot |
| 154 | * @slot: slot pointer, returned by radix_tree_lookup_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 155 | * |
| 156 | * For use with radix_tree_lookup_slot(). Caller must hold tree at least read |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 157 | * locked across slot lookup and dereference. Not required if write lock is |
| 158 | * held (ie. items cannot be concurrently inserted). |
| 159 | * |
| 160 | * radix_tree_deref_retry must be used to confirm validity of the pointer if |
| 161 | * only the read lock is held. |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 162 | * |
| 163 | * Return: entry stored in that slot. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 164 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 165 | static inline void *radix_tree_deref_slot(void __rcu **slot) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 166 | { |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 167 | return rcu_dereference(*slot); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 168 | } |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 169 | |
| 170 | /** |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 171 | * radix_tree_deref_slot_protected - dereference a slot with tree lock held |
| 172 | * @slot: slot pointer, returned by radix_tree_lookup_slot |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 173 | * |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 174 | * Similar to radix_tree_deref_slot. The caller does not hold the RCU read |
| 175 | * lock but it must hold the tree lock to prevent parallel updates. |
| 176 | * |
| 177 | * Return: entry stored in that slot. |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 178 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 179 | static inline void *radix_tree_deref_slot_protected(void __rcu **slot, |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 180 | spinlock_t *treelock) |
| 181 | { |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 182 | return rcu_dereference_protected(*slot, lockdep_is_held(treelock)); |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 186 | * radix_tree_deref_retry - check radix_tree_deref_slot |
| 187 | * @arg: pointer returned by radix_tree_deref_slot |
| 188 | * Returns: 0 if retry is not required, otherwise retry is required |
| 189 | * |
| 190 | * radix_tree_deref_retry must be used with radix_tree_deref_slot. |
| 191 | */ |
| 192 | static inline int radix_tree_deref_retry(void *arg) |
| 193 | { |
Matthew Wilcox | b194d16 | 2016-05-20 17:03:30 -0700 | [diff] [blame] | 194 | return unlikely(radix_tree_is_internal_node(arg)); |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 195 | } |
| 196 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 197 | /** |
Hugh Dickins | 6328650 | 2011-08-03 16:21:18 -0700 | [diff] [blame] | 198 | * radix_tree_exception - radix_tree_deref_slot returned either exception? |
| 199 | * @arg: value returned by radix_tree_deref_slot |
| 200 | * Returns: 0 if well-aligned pointer, non-0 if either kind of exception. |
| 201 | */ |
| 202 | static inline int radix_tree_exception(void *arg) |
| 203 | { |
Matthew Wilcox | 3bcadd6 | 2016-05-20 17:03:54 -0700 | [diff] [blame] | 204 | return unlikely((unsigned long)arg & RADIX_TREE_ENTRY_MASK); |
Hugh Dickins | 6328650 | 2011-08-03 16:21:18 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Matthew Wilcox | 3a08cd5 | 2018-09-22 16:14:30 -0400 | [diff] [blame] | 207 | int radix_tree_insert(struct radix_tree_root *, unsigned long index, |
| 208 | void *); |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 209 | void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 210 | struct radix_tree_node **nodep, void __rcu ***slotp); |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 211 | void *radix_tree_lookup(const struct radix_tree_root *, unsigned long); |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 212 | void __rcu **radix_tree_lookup_slot(const struct radix_tree_root *, |
| 213 | unsigned long index); |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 214 | void __radix_tree_replace(struct radix_tree_root *, struct radix_tree_node *, |
Matthew Wilcox | 1cf56f9 | 2018-04-09 16:24:45 -0400 | [diff] [blame] | 215 | void __rcu **slot, void *entry); |
Matthew Wilcox | e157b55 | 2016-12-14 15:09:01 -0800 | [diff] [blame] | 216 | void radix_tree_iter_replace(struct radix_tree_root *, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 217 | const struct radix_tree_iter *, void __rcu **slot, void *entry); |
| 218 | void radix_tree_replace_slot(struct radix_tree_root *, |
| 219 | void __rcu **slot, void *entry); |
Matthew Wilcox | 0ac398e | 2017-01-28 09:56:22 -0500 | [diff] [blame] | 220 | void radix_tree_iter_delete(struct radix_tree_root *, |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 221 | struct radix_tree_iter *iter, void __rcu **slot); |
Johannes Weiner | 53c59f2 | 2014-04-03 14:47:39 -0700 | [diff] [blame] | 222 | void *radix_tree_delete_item(struct radix_tree_root *, unsigned long, void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | void *radix_tree_delete(struct radix_tree_root *, unsigned long); |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 224 | unsigned int radix_tree_gang_lookup(const struct radix_tree_root *, |
Matthew Wilcox | d604c32 | 2016-05-20 17:03:45 -0700 | [diff] [blame] | 225 | void **results, unsigned long first_index, |
| 226 | unsigned int max_items); |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 227 | int radix_tree_preload(gfp_t gfp_mask); |
Jan Kara | 5e4c0d97 | 2013-09-11 14:26:05 -0700 | [diff] [blame] | 228 | int radix_tree_maybe_preload(gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | void radix_tree_init(void); |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 230 | void *radix_tree_tag_set(struct radix_tree_root *, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 231 | unsigned long index, unsigned int tag); |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 232 | void *radix_tree_tag_clear(struct radix_tree_root *, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 233 | unsigned long index, unsigned int tag); |
Matthew Wilcox | 35534c8 | 2016-12-19 17:43:19 -0500 | [diff] [blame] | 234 | int radix_tree_tag_get(const struct radix_tree_root *, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 235 | unsigned long index, unsigned int tag); |
Matthew Wilcox | 30b888b | 2017-01-28 09:55:20 -0500 | [diff] [blame] | 236 | void radix_tree_iter_tag_clear(struct radix_tree_root *, |
Matthew Wilcox | 268f42d | 2016-12-14 15:08:55 -0800 | [diff] [blame] | 237 | const struct radix_tree_iter *iter, unsigned int tag); |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 238 | unsigned int radix_tree_gang_lookup_tag(const struct radix_tree_root *, |
| 239 | void **results, unsigned long first_index, |
| 240 | unsigned int max_items, unsigned int tag); |
| 241 | unsigned int radix_tree_gang_lookup_tag_slot(const struct radix_tree_root *, |
| 242 | void __rcu ***results, unsigned long first_index, |
| 243 | unsigned int max_items, unsigned int tag); |
| 244 | int radix_tree_tagged(const struct radix_tree_root *, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | static inline void radix_tree_preload_end(void) |
| 247 | { |
| 248 | preempt_enable(); |
| 249 | } |
| 250 | |
Matthew Wilcox | 460488c | 2017-11-28 15:16:24 -0500 | [diff] [blame] | 251 | void __rcu **idr_get_free(struct radix_tree_root *root, |
Chris Mi | 388f79f | 2017-08-30 02:31:57 -0400 | [diff] [blame] | 252 | struct radix_tree_iter *iter, gfp_t gfp, |
| 253 | unsigned long max); |
Matthew Wilcox | 175542f | 2016-12-14 15:08:58 -0800 | [diff] [blame] | 254 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 255 | enum { |
| 256 | RADIX_TREE_ITER_TAG_MASK = 0x0f, /* tag index in lower nybble */ |
| 257 | RADIX_TREE_ITER_TAGGED = 0x10, /* lookup tagged slots */ |
| 258 | RADIX_TREE_ITER_CONTIG = 0x20, /* stop at first hole */ |
| 259 | }; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * radix_tree_iter_init - initialize radix tree iterator |
| 263 | * |
| 264 | * @iter: pointer to iterator state |
| 265 | * @start: iteration starting index |
| 266 | * Returns: NULL |
| 267 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 268 | static __always_inline void __rcu ** |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 269 | radix_tree_iter_init(struct radix_tree_iter *iter, unsigned long start) |
| 270 | { |
| 271 | /* |
| 272 | * Leave iter->tags uninitialized. radix_tree_next_chunk() will fill it |
| 273 | * in the case of a successful tagged chunk lookup. If the lookup was |
| 274 | * unsuccessful or non-tagged then nobody cares about ->tags. |
| 275 | * |
| 276 | * Set index to zero to bypass next_index overflow protection. |
| 277 | * See the comment in radix_tree_next_chunk() for details. |
| 278 | */ |
| 279 | iter->index = 0; |
| 280 | iter->next_index = start; |
| 281 | return NULL; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * radix_tree_next_chunk - find next chunk of slots for iteration |
| 286 | * |
| 287 | * @root: radix tree root |
| 288 | * @iter: iterator state |
| 289 | * @flags: RADIX_TREE_ITER_* flags and tag index |
| 290 | * Returns: pointer to chunk first slot, or NULL if there no more left |
| 291 | * |
| 292 | * This function looks up the next chunk in the radix tree starting from |
| 293 | * @iter->next_index. It returns a pointer to the chunk's first slot. |
| 294 | * Also it fills @iter with data about chunk: position in the tree (index), |
| 295 | * its end (next_index), and constructs a bit mask for tagged iterating (tags). |
| 296 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 297 | void __rcu **radix_tree_next_chunk(const struct radix_tree_root *, |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 298 | struct radix_tree_iter *iter, unsigned flags); |
| 299 | |
| 300 | /** |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 301 | * radix_tree_iter_lookup - look up an index in the radix tree |
| 302 | * @root: radix tree root |
| 303 | * @iter: iterator state |
| 304 | * @index: key to look up |
| 305 | * |
| 306 | * If @index is present in the radix tree, this function returns the slot |
| 307 | * containing it and updates @iter to describe the entry. If @index is not |
| 308 | * present, it returns NULL. |
| 309 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 310 | static inline void __rcu ** |
| 311 | radix_tree_iter_lookup(const struct radix_tree_root *root, |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 312 | struct radix_tree_iter *iter, unsigned long index) |
| 313 | { |
| 314 | radix_tree_iter_init(iter, index); |
| 315 | return radix_tree_next_chunk(root, iter, RADIX_TREE_ITER_CONTIG); |
| 316 | } |
| 317 | |
| 318 | /** |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 319 | * radix_tree_iter_retry - retry this chunk of the iteration |
| 320 | * @iter: iterator state |
| 321 | * |
| 322 | * If we iterate over a tree protected only by the RCU lock, a race |
| 323 | * against deletion or creation may result in seeing a slot for which |
| 324 | * radix_tree_deref_retry() returns true. If so, call this function |
| 325 | * and continue the iteration. |
| 326 | */ |
| 327 | static inline __must_check |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 328 | void __rcu **radix_tree_iter_retry(struct radix_tree_iter *iter) |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 329 | { |
| 330 | iter->next_index = iter->index; |
Andrey Ryabinin | 3cb9185 | 2016-07-20 15:45:00 -0700 | [diff] [blame] | 331 | iter->tags = 0; |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 332 | return NULL; |
| 333 | } |
| 334 | |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 335 | static inline unsigned long |
| 336 | __radix_tree_iter_add(struct radix_tree_iter *iter, unsigned long slots) |
| 337 | { |
Matthew Wilcox | 3a08cd5 | 2018-09-22 16:14:30 -0400 | [diff] [blame] | 338 | return iter->index + slots; |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Matthew Wilcox | 46437f9 | 2016-02-02 16:57:52 -0800 | [diff] [blame] | 341 | /** |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 342 | * radix_tree_iter_resume - resume iterating when the chunk may be invalid |
| 343 | * @slot: pointer to current slot |
| 344 | * @iter: iterator state |
| 345 | * Returns: New slot pointer |
Matthew Wilcox | 7165092 | 2016-03-17 14:22:06 -0700 | [diff] [blame] | 346 | * |
| 347 | * If the iterator needs to release then reacquire a lock, the chunk may |
| 348 | * have been invalidated by an insertion or deletion. Call this function |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 349 | * before releasing the lock to continue the iteration from the next index. |
Matthew Wilcox | 7165092 | 2016-03-17 14:22:06 -0700 | [diff] [blame] | 350 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 351 | void __rcu **__must_check radix_tree_iter_resume(void __rcu **slot, |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 352 | struct radix_tree_iter *iter); |
Matthew Wilcox | 7165092 | 2016-03-17 14:22:06 -0700 | [diff] [blame] | 353 | |
| 354 | /** |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 355 | * radix_tree_chunk_size - get current chunk size |
| 356 | * |
| 357 | * @iter: pointer to radix tree iterator |
| 358 | * Returns: current chunk size |
| 359 | */ |
Konstantin Khlebnikov | 7320428 | 2016-02-05 15:37:01 -0800 | [diff] [blame] | 360 | static __always_inline long |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 361 | radix_tree_chunk_size(struct radix_tree_iter *iter) |
| 362 | { |
Matthew Wilcox | 3a08cd5 | 2018-09-22 16:14:30 -0400 | [diff] [blame] | 363 | return iter->next_index - iter->index; |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 364 | } |
| 365 | |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 366 | /** |
| 367 | * radix_tree_next_slot - find next slot in chunk |
| 368 | * |
| 369 | * @slot: pointer to current slot |
| 370 | * @iter: pointer to interator state |
| 371 | * @flags: RADIX_TREE_ITER_*, should be constant |
| 372 | * Returns: pointer to next slot, or NULL if there no more left |
| 373 | * |
| 374 | * This function updates @iter->index in the case of a successful lookup. |
| 375 | * For tagged lookup it also eats @iter->tags. |
Ross Zwisler | 915045f | 2016-10-11 13:51:18 -0700 | [diff] [blame] | 376 | * |
| 377 | * There are several cases where 'slot' can be passed in as NULL to this |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 378 | * function. These cases result from the use of radix_tree_iter_resume() or |
Ross Zwisler | 915045f | 2016-10-11 13:51:18 -0700 | [diff] [blame] | 379 | * radix_tree_iter_retry(). In these cases we don't end up dereferencing |
| 380 | * 'slot' because either: |
| 381 | * a) we are doing tagged iteration and iter->tags has been set to 0, or |
| 382 | * b) we are doing non-tagged iteration, and iter->index and iter->next_index |
| 383 | * have been set up so that radix_tree_chunk_size() returns 1 or 0. |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 384 | */ |
Matthew Wilcox | d7b6272 | 2017-02-13 15:58:24 -0500 | [diff] [blame] | 385 | static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot, |
| 386 | struct radix_tree_iter *iter, unsigned flags) |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 387 | { |
| 388 | if (flags & RADIX_TREE_ITER_TAGGED) { |
| 389 | iter->tags >>= 1; |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 390 | if (unlikely(!iter->tags)) |
| 391 | return NULL; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 392 | if (likely(iter->tags & 1ul)) { |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 393 | iter->index = __radix_tree_iter_add(iter, 1); |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 394 | slot++; |
| 395 | goto found; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 396 | } |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 397 | if (!(flags & RADIX_TREE_ITER_CONTIG)) { |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 398 | unsigned offset = __ffs(iter->tags); |
| 399 | |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 400 | iter->tags >>= offset++; |
| 401 | iter->index = __radix_tree_iter_add(iter, offset); |
| 402 | slot += offset; |
| 403 | goto found; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 404 | } |
| 405 | } else { |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 406 | long count = radix_tree_chunk_size(iter); |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 407 | |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 408 | while (--count > 0) { |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 409 | slot++; |
Ross Zwisler | 21ef533 | 2016-05-20 17:02:26 -0700 | [diff] [blame] | 410 | iter->index = __radix_tree_iter_add(iter, 1); |
| 411 | |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 412 | if (likely(*slot)) |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 413 | goto found; |
Konstantin Khlebnikov | fffaee3 | 2012-06-05 21:36:33 +0400 | [diff] [blame] | 414 | if (flags & RADIX_TREE_ITER_CONTIG) { |
| 415 | /* forbid switching to the next chunk */ |
| 416 | iter->next_index = 0; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 417 | break; |
Konstantin Khlebnikov | fffaee3 | 2012-06-05 21:36:33 +0400 | [diff] [blame] | 418 | } |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | return NULL; |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 422 | |
| 423 | found: |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 424 | return slot; |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /** |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 428 | * radix_tree_for_each_slot - iterate over non-empty slots |
| 429 | * |
| 430 | * @slot: the void** variable for pointer to slot |
| 431 | * @root: the struct radix_tree_root pointer |
| 432 | * @iter: the struct radix_tree_iter pointer |
| 433 | * @start: iteration starting index |
| 434 | * |
| 435 | * @slot points to radix tree slot, @iter->index contains its index. |
| 436 | */ |
| 437 | #define radix_tree_for_each_slot(slot, root, iter, start) \ |
| 438 | for (slot = radix_tree_iter_init(iter, start) ; \ |
| 439 | slot || (slot = radix_tree_next_chunk(root, iter, 0)) ; \ |
| 440 | slot = radix_tree_next_slot(slot, iter, 0)) |
| 441 | |
| 442 | /** |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 443 | * radix_tree_for_each_tagged - iterate over tagged slots |
| 444 | * |
| 445 | * @slot: the void** variable for pointer to slot |
| 446 | * @root: the struct radix_tree_root pointer |
| 447 | * @iter: the struct radix_tree_iter pointer |
| 448 | * @start: iteration starting index |
| 449 | * @tag: tag index |
| 450 | * |
| 451 | * @slot points to radix tree slot, @iter->index contains its index. |
| 452 | */ |
| 453 | #define radix_tree_for_each_tagged(slot, root, iter, start, tag) \ |
| 454 | for (slot = radix_tree_iter_init(iter, start) ; \ |
| 455 | slot || (slot = radix_tree_next_chunk(root, iter, \ |
| 456 | RADIX_TREE_ITER_TAGGED | tag)) ; \ |
| 457 | slot = radix_tree_next_slot(slot, iter, \ |
Matthew Wilcox | 148deab | 2016-12-14 15:08:49 -0800 | [diff] [blame] | 458 | RADIX_TREE_ITER_TAGGED | tag)) |
Konstantin Khlebnikov | 78c1d78 | 2012-03-28 14:42:53 -0700 | [diff] [blame] | 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | #endif /* _LINUX_RADIX_TREE_H */ |