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 |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 4 | * Copyright (C) 2006 Nick Piggin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2, or (at |
| 9 | * your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | #ifndef _LINUX_RADIX_TREE_H |
| 21 | #define _LINUX_RADIX_TREE_H |
| 22 | |
| 23 | #include <linux/preempt.h> |
| 24 | #include <linux/types.h> |
Paul Gortmaker | 187f188 | 2011-11-23 20:12:59 -0500 | [diff] [blame^] | 25 | #include <linux/bug.h> |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/rcupdate.h> |
| 28 | |
| 29 | /* |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 30 | * An indirect pointer (root->rnode pointing to a radix_tree_node, rather |
| 31 | * than a data item) is signalled by the low bit set in the root->rnode |
| 32 | * pointer. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 33 | * |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 34 | * In this case root->height is > 0, but the indirect pointer tests are |
| 35 | * needed for RCU lookups (because root->height is unreliable). The only |
| 36 | * time callers need worry about this is when doing a lookup_slot under |
| 37 | * RCU. |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 38 | * |
| 39 | * Indirect pointer in fact is also used to tag the last pointer of a node |
| 40 | * when it is shrunk, before we rcu free the node. See shrink code for |
| 41 | * details. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 42 | */ |
Hugh Dickins | 6328650 | 2011-08-03 16:21:18 -0700 | [diff] [blame] | 43 | #define RADIX_TREE_INDIRECT_PTR 1 |
| 44 | /* |
| 45 | * A common use of the radix tree is to store pointers to struct pages; |
| 46 | * but shmem/tmpfs needs also to store swap entries in the same tree: |
| 47 | * those are marked as exceptional entries to distinguish them. |
| 48 | * EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it. |
| 49 | */ |
| 50 | #define RADIX_TREE_EXCEPTIONAL_ENTRY 2 |
| 51 | #define RADIX_TREE_EXCEPTIONAL_SHIFT 2 |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 52 | |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 53 | static inline int radix_tree_is_indirect_ptr(void *ptr) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 54 | { |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 55 | return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /*** radix-tree API starts here ***/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Jan Kara | f446daae | 2010-08-09 17:19:12 -0700 | [diff] [blame] | 60 | #define RADIX_TREE_MAX_TAGS 3 |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 61 | |
| 62 | /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | struct radix_tree_root { |
| 64 | unsigned int height; |
Al Viro | fd4f2df | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 65 | gfp_t gfp_mask; |
Arnd Bergmann | a111557 | 2010-02-25 23:43:52 +0100 | [diff] [blame] | 66 | struct radix_tree_node __rcu *rnode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | #define RADIX_TREE_INIT(mask) { \ |
| 70 | .height = 0, \ |
| 71 | .gfp_mask = (mask), \ |
| 72 | .rnode = NULL, \ |
| 73 | } |
| 74 | |
| 75 | #define RADIX_TREE(name, mask) \ |
| 76 | struct radix_tree_root name = RADIX_TREE_INIT(mask) |
| 77 | |
| 78 | #define INIT_RADIX_TREE(root, mask) \ |
| 79 | do { \ |
| 80 | (root)->height = 0; \ |
| 81 | (root)->gfp_mask = (mask); \ |
| 82 | (root)->rnode = NULL; \ |
| 83 | } while (0) |
| 84 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Radix-tree synchronization |
| 87 | * |
| 88 | * The radix-tree API requires that users provide all synchronisation (with |
| 89 | * specific exceptions, noted below). |
| 90 | * |
| 91 | * Synchronization of access to the data items being stored in the tree, and |
| 92 | * management of their lifetimes must be completely managed by API users. |
| 93 | * |
| 94 | * For API usage, in general, |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 95 | * - any function _modifying_ the tree or tags (inserting or deleting |
Tim Pepper | eb8dc5e | 2008-02-03 16:12:47 +0200 | [diff] [blame] | 96 | * items, setting or clearing tags) must exclude other modifications, and |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 97 | * exclude any functions reading the tree. |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 98 | * - any function _reading_ the tree or tags (looking up items or tags, |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 99 | * gang lookups) must exclude modifications to the tree, but may occur |
| 100 | * concurrently with other readers. |
| 101 | * |
| 102 | * The notable exceptions to this rule are the following functions: |
| 103 | * radix_tree_lookup |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 104 | * radix_tree_lookup_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 105 | * radix_tree_tag_get |
| 106 | * radix_tree_gang_lookup |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 107 | * radix_tree_gang_lookup_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 108 | * radix_tree_gang_lookup_tag |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 109 | * radix_tree_gang_lookup_tag_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 110 | * radix_tree_tagged |
| 111 | * |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 112 | * 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] | 113 | * caller must ensure calls to these functions are made within rcu_read_lock() |
| 114 | * regions. Other readers (lock-free or otherwise) and modifications may be |
| 115 | * running concurrently. |
| 116 | * |
| 117 | * It is still required that the caller manage the synchronization and lifetimes |
| 118 | * of the items. So if RCU lock-free lookups are used, typically this would mean |
| 119 | * that the items have their own locks, or are amenable to lock-free access; and |
| 120 | * that the items are freed by RCU (or only freed after having been deleted from |
| 121 | * the radix tree *and* a synchronize_rcu() grace period). |
| 122 | * |
| 123 | * (Note, rcu_assign_pointer and rcu_dereference are not needed to control |
| 124 | * access to data items when inserting into or looking up from the radix tree) |
| 125 | * |
David Howells | ce82653 | 2010-04-06 22:36:20 +0100 | [diff] [blame] | 126 | * Note that the value returned by radix_tree_tag_get() may not be relied upon |
| 127 | * if only the RCU read lock is held. Functions to set/clear tags and to |
| 128 | * delete nodes running concurrently with it may affect its result such that |
| 129 | * two consecutive reads in the same locked section may return different |
| 130 | * values. If reliability is required, modification functions must also be |
| 131 | * excluded from concurrency. |
| 132 | * |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 133 | * radix_tree_tagged is able to be called without locking or RCU. |
| 134 | */ |
| 135 | |
| 136 | /** |
| 137 | * radix_tree_deref_slot - dereference a slot |
| 138 | * @pslot: pointer to slot, returned by radix_tree_lookup_slot |
| 139 | * Returns: item that was stored in that slot with any direct pointer flag |
| 140 | * removed. |
| 141 | * |
| 142 | * 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] | 143 | * locked across slot lookup and dereference. Not required if write lock is |
| 144 | * held (ie. items cannot be concurrently inserted). |
| 145 | * |
| 146 | * radix_tree_deref_retry must be used to confirm validity of the pointer if |
| 147 | * only the read lock is held. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 148 | */ |
| 149 | static inline void *radix_tree_deref_slot(void **pslot) |
| 150 | { |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 151 | return rcu_dereference(*pslot); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 152 | } |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
Mel Gorman | 29c1f67 | 2011-01-13 15:47:21 -0800 | [diff] [blame] | 155 | * radix_tree_deref_slot_protected - dereference a slot without RCU lock but with tree lock held |
| 156 | * @pslot: pointer to slot, returned by radix_tree_lookup_slot |
| 157 | * Returns: item that was stored in that slot with any direct pointer flag |
| 158 | * removed. |
| 159 | * |
| 160 | * Similar to radix_tree_deref_slot but only used during migration when a pages |
| 161 | * mapping is being moved. The caller does not hold the RCU read lock but it |
| 162 | * must hold the tree lock to prevent parallel updates. |
| 163 | */ |
| 164 | static inline void *radix_tree_deref_slot_protected(void **pslot, |
| 165 | spinlock_t *treelock) |
| 166 | { |
| 167 | return rcu_dereference_protected(*pslot, lockdep_is_held(treelock)); |
| 168 | } |
| 169 | |
| 170 | /** |
Nick Piggin | 27d20fd | 2010-11-11 14:05:19 -0800 | [diff] [blame] | 171 | * radix_tree_deref_retry - check radix_tree_deref_slot |
| 172 | * @arg: pointer returned by radix_tree_deref_slot |
| 173 | * Returns: 0 if retry is not required, otherwise retry is required |
| 174 | * |
| 175 | * radix_tree_deref_retry must be used with radix_tree_deref_slot. |
| 176 | */ |
| 177 | static inline int radix_tree_deref_retry(void *arg) |
| 178 | { |
| 179 | return unlikely((unsigned long)arg & RADIX_TREE_INDIRECT_PTR); |
| 180 | } |
| 181 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 182 | /** |
Hugh Dickins | 6328650 | 2011-08-03 16:21:18 -0700 | [diff] [blame] | 183 | * radix_tree_exceptional_entry - radix_tree_deref_slot gave exceptional entry? |
| 184 | * @arg: value returned by radix_tree_deref_slot |
| 185 | * Returns: 0 if well-aligned pointer, non-0 if exceptional entry. |
| 186 | */ |
| 187 | static inline int radix_tree_exceptional_entry(void *arg) |
| 188 | { |
| 189 | /* Not unlikely because radix_tree_exception often tested first */ |
| 190 | return (unsigned long)arg & RADIX_TREE_EXCEPTIONAL_ENTRY; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * radix_tree_exception - radix_tree_deref_slot returned either exception? |
| 195 | * @arg: value returned by radix_tree_deref_slot |
| 196 | * Returns: 0 if well-aligned pointer, non-0 if either kind of exception. |
| 197 | */ |
| 198 | static inline int radix_tree_exception(void *arg) |
| 199 | { |
| 200 | return unlikely((unsigned long)arg & |
| 201 | (RADIX_TREE_INDIRECT_PTR | RADIX_TREE_EXCEPTIONAL_ENTRY)); |
| 202 | } |
| 203 | |
| 204 | /** |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 205 | * radix_tree_replace_slot - replace item in a slot |
| 206 | * @pslot: pointer to slot, returned by radix_tree_lookup_slot |
| 207 | * @item: new item to store in the slot. |
| 208 | * |
| 209 | * For use with radix_tree_lookup_slot(). Caller must hold tree write locked |
| 210 | * across slot lookup and replacement. |
| 211 | */ |
| 212 | static inline void radix_tree_replace_slot(void **pslot, void *item) |
| 213 | { |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 214 | BUG_ON(radix_tree_is_indirect_ptr(item)); |
| 215 | rcu_assign_pointer(*pslot, item); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | int radix_tree_insert(struct radix_tree_root *, unsigned long, void *); |
| 219 | void *radix_tree_lookup(struct radix_tree_root *, unsigned long); |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 220 | void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | void *radix_tree_delete(struct radix_tree_root *, unsigned long); |
| 222 | unsigned int |
| 223 | radix_tree_gang_lookup(struct radix_tree_root *root, void **results, |
| 224 | unsigned long first_index, unsigned int max_items); |
Hugh Dickins | 6328650 | 2011-08-03 16:21:18 -0700 | [diff] [blame] | 225 | unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root, |
| 226 | void ***results, unsigned long *indices, |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 227 | unsigned long first_index, unsigned int max_items); |
Fengguang Wu | 6df8ba4 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 228 | unsigned long radix_tree_next_hole(struct radix_tree_root *root, |
| 229 | unsigned long index, unsigned long max_scan); |
Wu Fengguang | dc56612 | 2009-06-16 15:31:32 -0700 | [diff] [blame] | 230 | unsigned long radix_tree_prev_hole(struct radix_tree_root *root, |
| 231 | unsigned long index, unsigned long max_scan); |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 232 | int radix_tree_preload(gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | void radix_tree_init(void); |
| 234 | void *radix_tree_tag_set(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 235 | unsigned long index, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | void *radix_tree_tag_clear(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 237 | unsigned long index, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | int radix_tree_tag_get(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 239 | unsigned long index, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | unsigned int |
| 241 | radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 242 | unsigned long first_index, unsigned int max_items, |
| 243 | unsigned int tag); |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 244 | unsigned int |
| 245 | radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results, |
| 246 | unsigned long first_index, unsigned int max_items, |
| 247 | unsigned int tag); |
Jan Kara | ebf8aa4 | 2010-08-09 17:19:11 -0700 | [diff] [blame] | 248 | unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root, |
| 249 | unsigned long *first_indexp, unsigned long last_index, |
| 250 | unsigned long nr_to_tag, |
| 251 | unsigned int fromtag, unsigned int totag); |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 252 | int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag); |
Hugh Dickins | e504f3f | 2011-08-03 16:21:27 -0700 | [diff] [blame] | 253 | unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
| 255 | static inline void radix_tree_preload_end(void) |
| 256 | { |
| 257 | preempt_enable(); |
| 258 | } |
| 259 | |
| 260 | #endif /* _LINUX_RADIX_TREE_H */ |