blob: a39cbed9ee17a5d771f7c3e7ca129e3a05171220 [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 Piggin7cf9c2c2006-12-06 20:33:44 -080037 */
Nick Pigginc0bc9872007-10-16 01:24:42 -070038#define RADIX_TREE_INDIRECT_PTR 1
39#define RADIX_TREE_RETRY ((void *)-1UL)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080040
Nick Pigginc0bc9872007-10-16 01:24:42 -070041static inline void *radix_tree_ptr_to_indirect(void *ptr)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080042{
Nick Pigginc0bc9872007-10-16 01:24:42 -070043 return (void *)((unsigned long)ptr | RADIX_TREE_INDIRECT_PTR);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080044}
45
Nick Pigginc0bc9872007-10-16 01:24:42 -070046static inline void *radix_tree_indirect_to_ptr(void *ptr)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080047{
Nick Pigginc0bc9872007-10-16 01:24:42 -070048 return (void *)((unsigned long)ptr & ~RADIX_TREE_INDIRECT_PTR);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080049}
Arnd Bergmanna1115572010-02-25 23:43:52 +010050#define radix_tree_indirect_to_ptr(ptr) \
51 radix_tree_indirect_to_ptr((void __force *)(ptr))
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080052
Nick Pigginc0bc9872007-10-16 01:24:42 -070053static inline int radix_tree_is_indirect_ptr(void *ptr)
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080054{
Nick Pigginc0bc9872007-10-16 01:24:42 -070055 return (int)((unsigned long)ptr & RADIX_TREE_INDIRECT_PTR);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080056}
57
58/*** radix-tree API starts here ***/
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jan Karaf446daae2010-08-09 17:19:12 -070060#define RADIX_TREE_MAX_TAGS 3
Nick Piggin612d6c12006-06-23 02:03:22 -070061
62/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063struct radix_tree_root {
64 unsigned int height;
Al Virofd4f2df2005-10-21 03:18:50 -040065 gfp_t gfp_mask;
Arnd Bergmanna1115572010-02-25 23:43:52 +010066 struct radix_tree_node __rcu *rnode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067};
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) \
79do { \
80 (root)->height = 0; \
81 (root)->gfp_mask = (mask); \
82 (root)->rnode = NULL; \
83} while (0)
84
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080085/**
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 Opdenacker59c51592007-05-09 08:57:56 +020095 * - any function _modifying_ the tree or tags (inserting or deleting
Tim Peppereb8dc5e2008-02-03 16:12:47 +020096 * items, setting or clearing tags) must exclude other modifications, and
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080097 * exclude any functions reading the tree.
Michael Opdenacker59c51592007-05-09 08:57:56 +020098 * - any function _reading_ the tree or tags (looking up items or tags,
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080099 * 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 Piggin47feff22008-07-25 19:45:29 -0700104 * radix_tree_lookup_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800105 * radix_tree_tag_get
106 * radix_tree_gang_lookup
Nick Piggin47feff22008-07-25 19:45:29 -0700107 * radix_tree_gang_lookup_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800108 * radix_tree_gang_lookup_tag
Nick Piggin47feff22008-07-25 19:45:29 -0700109 * radix_tree_gang_lookup_tag_slot
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800110 * radix_tree_tagged
111 *
Nick Piggin47feff22008-07-25 19:45:29 -0700112 * The first 7 functions are able to be called locklessly, using RCU. The
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800113 * 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 Howellsce826532010-04-06 22:36:20 +0100126 * 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 Piggin7cf9c2c2006-12-06 20:33:44 -0800133 * 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
143 * locked across slot lookup and dereference. More likely, will be used with
144 * radix_tree_replace_slot(), as well, so caller will hold tree write locked.
145 */
146static inline void *radix_tree_deref_slot(void **pslot)
147{
Nick Piggine8c82c22009-01-06 03:05:50 +0100148 void *ret = rcu_dereference(*pslot);
Nick Pigginc0bc9872007-10-16 01:24:42 -0700149 if (unlikely(radix_tree_is_indirect_ptr(ret)))
150 ret = RADIX_TREE_RETRY;
151 return ret;
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800152}
153/**
154 * radix_tree_replace_slot - replace item in a slot
155 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
156 * @item: new item to store in the slot.
157 *
158 * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
159 * across slot lookup and replacement.
160 */
161static inline void radix_tree_replace_slot(void **pslot, void *item)
162{
Nick Pigginc0bc9872007-10-16 01:24:42 -0700163 BUG_ON(radix_tree_is_indirect_ptr(item));
164 rcu_assign_pointer(*pslot, item);
Nick Piggin7cf9c2c2006-12-06 20:33:44 -0800165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
168void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
Hans Reisera4331362005-11-07 00:59:29 -0800169void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170void *radix_tree_delete(struct radix_tree_root *, unsigned long);
171unsigned int
172radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
173 unsigned long first_index, unsigned int max_items);
Nick Piggin47feff22008-07-25 19:45:29 -0700174unsigned int
175radix_tree_gang_lookup_slot(struct radix_tree_root *root, void ***results,
176 unsigned long first_index, unsigned int max_items);
Fengguang Wu6df8ba42007-10-16 01:24:33 -0700177unsigned long radix_tree_next_hole(struct radix_tree_root *root,
178 unsigned long index, unsigned long max_scan);
Wu Fengguangdc566122009-06-16 15:31:32 -0700179unsigned long radix_tree_prev_hole(struct radix_tree_root *root,
180 unsigned long index, unsigned long max_scan);
Al Virodd0fc662005-10-07 07:46:04 +0100181int radix_tree_preload(gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182void radix_tree_init(void);
183void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800184 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800186 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800188 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189unsigned int
190radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800191 unsigned long first_index, unsigned int max_items,
192 unsigned int tag);
Nick Piggin47feff22008-07-25 19:45:29 -0700193unsigned int
194radix_tree_gang_lookup_tag_slot(struct radix_tree_root *root, void ***results,
195 unsigned long first_index, unsigned int max_items,
196 unsigned int tag);
Jan Karaebf8aa42010-08-09 17:19:11 -0700197unsigned long radix_tree_range_tag_if_tagged(struct radix_tree_root *root,
198 unsigned long *first_indexp, unsigned long last_index,
199 unsigned long nr_to_tag,
200 unsigned int fromtag, unsigned int totag);
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800201int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203static inline void radix_tree_preload_end(void)
204{
205 preempt_enable();
206}
207
208#endif /* _LINUX_RADIX_TREE_H */