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> |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 25 | #include <linux/kernel.h> |
| 26 | #include <linux/rcupdate.h> |
| 27 | |
| 28 | /* |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 29 | * An indirect pointer (root->rnode pointing to a radix_tree_node, rather |
| 30 | * than a data item) is signalled by the low bit set in the root->rnode |
| 31 | * pointer. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 32 | * |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 33 | * In this case root->height is > 0, but the indirect pointer tests are |
| 34 | * needed for RCU lookups (because root->height is unreliable). The only |
| 35 | * time callers need worry about this is when doing a lookup_slot under |
| 36 | * RCU. |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 37 | */ |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 38 | #define RADIX_TREE_INDIRECT_PTR 1 |
| 39 | #define RADIX_TREE_RETRY ((void *)-1UL) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 40 | |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 41 | static inline void *radix_tree_ptr_to_indirect(void *ptr) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 42 | { |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 43 | return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 46 | static inline void *radix_tree_indirect_to_ptr(void *ptr) |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 47 | { |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 48 | return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 51 | static inline int radix_tree_is_indirect_ptr(void *ptr) |
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 | return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /*** radix-tree API starts here ***/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | |
Nick Piggin | 612d6c1 | 2006-06-23 02:03:22 -0700 | [diff] [blame] | 58 | #define RADIX_TREE_MAX_TAGS 2 |
| 59 | |
| 60 | /* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | struct radix_tree_root { |
| 62 | unsigned int height; |
Al Viro | fd4f2df | 2005-10-21 03:18:50 -0400 | [diff] [blame] | 63 | gfp_t gfp_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | struct radix_tree_node *rnode; |
| 65 | }; |
| 66 | |
| 67 | #define RADIX_TREE_INIT(mask) { \ |
| 68 | .height = 0, \ |
| 69 | .gfp_mask = (mask), \ |
| 70 | .rnode = NULL, \ |
| 71 | } |
| 72 | |
| 73 | #define RADIX_TREE(name, mask) \ |
| 74 | struct radix_tree_root name = RADIX_TREE_INIT(mask) |
| 75 | |
| 76 | #define INIT_RADIX_TREE(root, mask) \ |
| 77 | do { \ |
| 78 | (root)->height = 0; \ |
| 79 | (root)->gfp_mask = (mask); \ |
| 80 | (root)->rnode = NULL; \ |
| 81 | } while (0) |
| 82 | |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Radix-tree synchronization |
| 85 | * |
| 86 | * The radix-tree API requires that users provide all synchronisation (with |
| 87 | * specific exceptions, noted below). |
| 88 | * |
| 89 | * Synchronization of access to the data items being stored in the tree, and |
| 90 | * management of their lifetimes must be completely managed by API users. |
| 91 | * |
| 92 | * For API usage, in general, |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 93 | * - any function _modifying_ the tree or tags (inserting or deleting |
Tim Pepper | eb8dc5e | 2008-02-03 16:12:47 +0200 | [diff] [blame] | 94 | * items, setting or clearing tags) must exclude other modifications, and |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 95 | * exclude any functions reading the tree. |
Michael Opdenacker | 59c5159 | 2007-05-09 08:57:56 +0200 | [diff] [blame] | 96 | * - any function _reading_ the tree or tags (looking up items or tags, |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 97 | * gang lookups) must exclude modifications to the tree, but may occur |
| 98 | * concurrently with other readers. |
| 99 | * |
| 100 | * The notable exceptions to this rule are the following functions: |
| 101 | * radix_tree_lookup |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 102 | * radix_tree_lookup_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 103 | * radix_tree_tag_get |
| 104 | * radix_tree_gang_lookup |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 105 | * radix_tree_gang_lookup_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 106 | * radix_tree_gang_lookup_tag |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 107 | * radix_tree_gang_lookup_tag_slot |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 108 | * radix_tree_tagged |
| 109 | * |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 110 | * 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] | 111 | * caller must ensure calls to these functions are made within rcu_read_lock() |
| 112 | * regions. Other readers (lock-free or otherwise) and modifications may be |
| 113 | * running concurrently. |
| 114 | * |
| 115 | * It is still required that the caller manage the synchronization and lifetimes |
| 116 | * of the items. So if RCU lock-free lookups are used, typically this would mean |
| 117 | * that the items have their own locks, or are amenable to lock-free access; and |
| 118 | * that the items are freed by RCU (or only freed after having been deleted from |
| 119 | * the radix tree *and* a synchronize_rcu() grace period). |
| 120 | * |
| 121 | * (Note, rcu_assign_pointer and rcu_dereference are not needed to control |
| 122 | * access to data items when inserting into or looking up from the radix tree) |
| 123 | * |
David Howells | ce82653 | 2010-04-06 22:36:20 +0100 | [diff] [blame^] | 124 | * Note that the value returned by radix_tree_tag_get() may not be relied upon |
| 125 | * if only the RCU read lock is held. Functions to set/clear tags and to |
| 126 | * delete nodes running concurrently with it may affect its result such that |
| 127 | * two consecutive reads in the same locked section may return different |
| 128 | * values. If reliability is required, modification functions must also be |
| 129 | * excluded from concurrency. |
| 130 | * |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 131 | * radix_tree_tagged is able to be called without locking or RCU. |
| 132 | */ |
| 133 | |
| 134 | /** |
| 135 | * radix_tree_deref_slot - dereference a slot |
| 136 | * @pslot: pointer to slot, returned by radix_tree_lookup_slot |
| 137 | * Returns: item that was stored in that slot with any direct pointer flag |
| 138 | * removed. |
| 139 | * |
| 140 | * For use with radix_tree_lookup_slot(). Caller must hold tree at least read |
| 141 | * locked across slot lookup and dereference. More likely, will be used with |
| 142 | * radix_tree_replace_slot(), as well, so caller will hold tree write locked. |
| 143 | */ |
| 144 | static inline void *radix_tree_deref_slot(void **pslot) |
| 145 | { |
Nick Piggin | e8c82c2 | 2009-01-06 03:05:50 +0100 | [diff] [blame] | 146 | void *ret = rcu_dereference(*pslot); |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 147 | if (unlikely(radix_tree_is_indirect_ptr(ret))) |
| 148 | ret = RADIX_TREE_RETRY; |
| 149 | return ret; |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 150 | } |
| 151 | /** |
| 152 | * radix_tree_replace_slot - replace item in a slot |
| 153 | * @pslot: pointer to slot, returned by radix_tree_lookup_slot |
| 154 | * @item: new item to store in the slot. |
| 155 | * |
| 156 | * For use with radix_tree_lookup_slot(). Caller must hold tree write locked |
| 157 | * across slot lookup and replacement. |
| 158 | */ |
| 159 | static inline void radix_tree_replace_slot(void **pslot, void *item) |
| 160 | { |
Nick Piggin | c0bc987 | 2007-10-16 01:24:42 -0700 | [diff] [blame] | 161 | BUG_ON(radix_tree_is_indirect_ptr(item)); |
| 162 | rcu_assign_pointer(*pslot, item); |
Nick Piggin | 7cf9c2c | 2006-12-06 20:33:44 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | int radix_tree_insert(struct radix_tree_root *, unsigned long, void *); |
| 166 | void *radix_tree_lookup(struct radix_tree_root *, unsigned long); |
Hans Reiser | a433136 | 2005-11-07 00:59:29 -0800 | [diff] [blame] | 167 | void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | void *radix_tree_delete(struct radix_tree_root *, unsigned long); |
| 169 | unsigned int |
| 170 | radix_tree_gang_lookup(struct radix_tree_root *root, void **results, |
| 171 | unsigned long first_index, unsigned int max_items); |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 172 | unsigned int |
| 173 | radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results, |
| 174 | unsigned long first_index, unsigned int max_items); |
Fengguang Wu | 6df8ba4 | 2007-10-16 01:24:33 -0700 | [diff] [blame] | 175 | unsigned long radix_tree_next_hole(struct radix_tree_root *root, |
| 176 | unsigned long index, unsigned long max_scan); |
Wu Fengguang | dc56612 | 2009-06-16 15:31:32 -0700 | [diff] [blame] | 177 | unsigned long radix_tree_prev_hole(struct radix_tree_root *root, |
| 178 | unsigned long index, unsigned long max_scan); |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 179 | int radix_tree_preload(gfp_t gfp_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | void radix_tree_init(void); |
| 181 | void *radix_tree_tag_set(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 182 | unsigned long index, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | void *radix_tree_tag_clear(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 184 | unsigned long index, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | int radix_tree_tag_get(struct radix_tree_root *root, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 186 | unsigned long index, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | unsigned int |
| 188 | radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results, |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 189 | unsigned long first_index, unsigned int max_items, |
| 190 | unsigned int tag); |
Nick Piggin | 47feff2 | 2008-07-25 19:45:29 -0700 | [diff] [blame] | 191 | unsigned int |
| 192 | radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results, |
| 193 | unsigned long first_index, unsigned int max_items, |
| 194 | unsigned int tag); |
Jonathan Corbet | daff89f | 2006-03-25 03:08:05 -0800 | [diff] [blame] | 195 | int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | static inline void radix_tree_preload_end(void) |
| 198 | { |
| 199 | preempt_enable(); |
| 200 | } |
| 201 | |
| 202 | #endif /* _LINUX_RADIX_TREE_H */ |