blob: 9d4539c52e53e5f3635345260049999ba90d735e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2001 Momchil Velikov
3 * Portions Copyright (C) 2001 Christoph Hellwig
Nick Piggin7cf9c2c2006-12-06 20:33:44 -08004 * Copyright (C) 2006 Nick Piggin
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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 Piggin7cf9c2c2006-12-06 20:33:44 -080025#include <linux/kernel.h>
26#include <linux/rcupdate.h>
27
28/*
Nick Pigginc0bc9872007-10-16 01:24:42 -070029 * 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 Piggin7cf9c2c2006-12-06 20:33:44 -080032 *
Nick Pigginc0bc9872007-10-16 01:24:42 -070033 * 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 Piggin27d20fd2010-11-11 14:05:19 -080037 *
38 * Indirect pointer in fact is also used to tag the last pointer of a node
39 * when it is shrunk, before we rcu free the node. See shrink code for
40 * details.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080041 */
Hugh Dickins63286502011-08-03 16:21:18 -070042#define RADIX_TREE_INDIRECT_PTR 1
43/*
44 * A common use of the radix tree is to store pointers to struct pages;
45 * but shmem/tmpfs needs also to store swap entries in the same tree:
46 * those are marked as exceptional entries to distinguish them.
47 * EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it.
48 */
49#define RADIX_TREE_EXCEPTIONAL_ENTRY 2
50#define RADIX_TREE_EXCEPTIONAL_SHIFT 2
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080051
Arnd Bergmanna1115572010-02-25 23:43:52 +010052#define radix_tree_indirect_to_ptr(ptr) \
53 radix_tree_indirect_to_ptr((void __force *)(ptr))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080054
Nick Pigginc0bc9872007-10-16 01:24:42 -070055static inline int radix_tree_is_indirect_ptr(void *ptr)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080056{
Nick Pigginc0bc9872007-10-16 01:24:42 -070057 return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080058}
59
60/*** radix-tree API starts here ***/
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Jan Karaf446daae2010-08-09 17:19:12 -070062#define RADIX_TREE_MAX_TAGS 3
Nick Piggin612d6c12006-06-23 02:03:22 -070063
64/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065struct radix_tree_root {
66 unsigned int height;
Al Virofd4f2df2005-10-21 03:18:50 -040067 gfp_t gfp_mask;
Arnd Bergmanna1115572010-02-25 23:43:52 +010068 struct radix_tree_node __rcu *rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71#define RADIX_TREE_INIT(mask) { \
72 .height = 0, \
73 .gfp_mask = (mask), \
74 .rnode = NULL, \
75}
76
77#define RADIX_TREE(name, mask) \
78 struct radix_tree_root name = RADIX_TREE_INIT(mask)
79
80#define INIT_RADIX_TREE(root, mask) \
81do { \
82 (root)->height = 0; \
83 (root)->gfp_mask = (mask); \
84 (root)->rnode = NULL; \
85} while (0)
86
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080087/**
88 * Radix-tree synchronization
89 *
90 * The radix-tree API requires that users provide all synchronisation (with
91 * specific exceptions, noted below).
92 *
93 * Synchronization of access to the data items being stored in the tree, and
94 * management of their lifetimes must be completely managed by API users.
95 *
96 * For API usage, in general,
Michael Opdenacker59c51592007-05-09 08:57:56 +020097 * - any function _modifying_ the tree or tags (inserting or deleting
Tim Peppereb8dc5e2008-02-03 16:12:47 +020098 * items, setting or clearing tags) must exclude other modifications, and
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080099 * exclude any functions reading the tree.
Michael Opdenacker59c51592007-05-09 08:57:56 +0200100 * - any function _reading_ the tree or tags (looking up items or tags,
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800101 * gang lookups) must exclude modifications to the tree, but may occur
102 * concurrently with other readers.
103 *
104 * The notable exceptions to this rule are the following functions:
105 * radix_tree_lookup
Nick Piggin47feff22008-07-25 19:45:29 -0700106 * radix_tree_lookup_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800107 * radix_tree_tag_get
108 * radix_tree_gang_lookup
Nick Piggin47feff22008-07-25 19:45:29 -0700109 * radix_tree_gang_lookup_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800110 * radix_tree_gang_lookup_tag
Nick Piggin47feff22008-07-25 19:45:29 -0700111 * radix_tree_gang_lookup_tag_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800112 * radix_tree_tagged
113 *
Nick Piggin47feff22008-07-25 19:45:29 -0700114 * The first 7 functions are able to be called locklessly, using RCU. The
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800115 * caller must ensure calls to these functions are made within rcu_read_lock()
116 * regions. Other readers (lock-free or otherwise) and modifications may be
117 * running concurrently.
118 *
119 * It is still required that the caller manage the synchronization and lifetimes
120 * of the items. So if RCU lock-free lookups are used, typically this would mean
121 * that the items have their own locks, or are amenable to lock-free access; and
122 * that the items are freed by RCU (or only freed after having been deleted from
123 * the radix tree *and* a synchronize_rcu() grace period).
124 *
125 * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
126 * access to data items when inserting into or looking up from the radix tree)
127 *
David Howellsce826532010-04-06 22:36:20 +0100128 * Note that the value returned by radix_tree_tag_get() may not be relied upon
129 * if only the RCU read lock is held. Functions to set/clear tags and to
130 * delete nodes running concurrently with it may affect its result such that
131 * two consecutive reads in the same locked section may return different
132 * values. If reliability is required, modification functions must also be
133 * excluded from concurrency.
134 *
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800135 * radix_tree_tagged is able to be called without locking or RCU.
136 */
137
138/**
139 * radix_tree_deref_slot - dereference a slot
140 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
141 * Returns: item that was stored in that slot with any direct pointer flag
142 * removed.
143 *
144 * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
Nick Piggin27d20fd2010-11-11 14:05:19 -0800145 * locked across slot lookup and dereference. Not required if write lock is
146 * held (ie. items cannot be concurrently inserted).
147 *
148 * radix_tree_deref_retry must be used to confirm validity of the pointer if
149 * only the read lock is held.
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800150 */
151static inline void *radix_tree_deref_slot(void **pslot)
152{
Nick Piggin27d20fd2010-11-11 14:05:19 -0800153 return rcu_dereference(*pslot);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800154}
Nick Piggin27d20fd2010-11-11 14:05:19 -0800155
156/**
Mel Gorman29c1f672011-01-13 15:47:21 -0800157 * radix_tree_deref_slot_protected - dereference a slot without RCU lock but with tree lock held
158 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
159 * Returns: item that was stored in that slot with any direct pointer flag
160 * removed.
161 *
162 * Similar to radix_tree_deref_slot but only used during migration when a pages
163 * mapping is being moved. The caller does not hold the RCU read lock but it
164 * must hold the tree lock to prevent parallel updates.
165 */
166static inline void *radix_tree_deref_slot_protected(void **pslot,
167 spinlock_t *treelock)
168{
169 return rcu_dereference_protected(*pslot, lockdep_is_held(treelock));
170}
171
172/**
Nick Piggin27d20fd2010-11-11 14:05:19 -0800173 * radix_tree_deref_retry - check radix_tree_deref_slot
174 * @arg: pointer returned by radix_tree_deref_slot
175 * Returns: 0 if retry is not required, otherwise retry is required
176 *
177 * radix_tree_deref_retry must be used with radix_tree_deref_slot.
178 */
179static inline int radix_tree_deref_retry(void *arg)
180{
181 return unlikely((unsigned long)arg & RADIX_TREE_INDIRECT_PTR);
182}
183
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800184/**
Hugh Dickins63286502011-08-03 16:21:18 -0700185 * radix_tree_exceptional_entry - radix_tree_deref_slot gave exceptional entry?
186 * @arg: value returned by radix_tree_deref_slot
187 * Returns: 0 if well-aligned pointer, non-0 if exceptional entry.
188 */
189static inline int radix_tree_exceptional_entry(void *arg)
190{
191 /* Not unlikely because radix_tree_exception often tested first */
192 return (unsigned long)arg & RADIX_TREE_EXCEPTIONAL_ENTRY;
193}
194
195/**
196 * radix_tree_exception - radix_tree_deref_slot returned either exception?
197 * @arg: value returned by radix_tree_deref_slot
198 * Returns: 0 if well-aligned pointer, non-0 if either kind of exception.
199 */
200static inline int radix_tree_exception(void *arg)
201{
202 return unlikely((unsigned long)arg &
203 (RADIX_TREE_INDIRECT_PTR | RADIX_TREE_EXCEPTIONAL_ENTRY));
204}
205
206/**
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800207 * radix_tree_replace_slot - replace item in a slot
208 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
209 * @item: new item to store in the slot.
210 *
211 * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
212 * across slot lookup and replacement.
213 */
214static inline void radix_tree_replace_slot(void **pslot, void *item)
215{
Nick Pigginc0bc9872007-10-16 01:24:42 -0700216 BUG_ON(radix_tree_is_indirect_ptr(item));
217 rcu_assign_pointer(*pslot, item);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800218}
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
221void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
Hans Reisera4331362005-11-07 00:59:29 -0800222void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223void *radix_tree_delete(struct radix_tree_root *, unsigned long);
224unsigned int
225radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
226 unsigned long first_index, unsigned int max_items);
Hugh Dickins63286502011-08-03 16:21:18 -0700227unsigned int radix_tree_gang_lookup_slot(struct radix_tree_root *root,
228 void ***results, unsigned long *indices,
Nick Piggin47feff22008-07-25 19:45:29 -0700229 unsigned long first_index, unsigned int max_items);
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700230unsigned long radix_tree_next_hole(struct radix_tree_root *root,
231 unsigned long index, unsigned long max_scan);
Wu Fengguangdc566122009-06-16 15:31:32 -0700232unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
233 unsigned long index, unsigned long max_scan);
Al Virodd0fc662005-10-07 07:46:04 +0100234int radix_tree_preload(gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235void radix_tree_init(void);
236void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800237 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800239 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800241 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242unsigned int
243radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800244 unsigned long first_index, unsigned int max_items,
245 unsigned int tag);
Nick Piggin47feff22008-07-25 19:45:29 -0700246unsigned int
247radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
248 unsigned long first_index, unsigned int max_items,
249 unsigned int tag);
Jan Karaebf8aa42010-08-09 17:19:11 -0700250unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
251 unsigned long *first_indexp, unsigned long last_index,
252 unsigned long nr_to_tag,
253 unsigned int fromtag, unsigned int totag);
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800254int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
Hugh Dickinse504f3f2011-08-03 16:21:27 -0700255unsigned long radix_tree_locate_item(struct radix_tree_root *root, void *item);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257static inline void radix_tree_preload_end(void)
258{
259 preempt_enable();
260}
261
262#endif /* _LINUX_RADIX_TREE_H */