blob: f9e77d2ee3201ea3565a409216c601734fc17140 [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/*
29 * A direct pointer (root->rnode pointing directly to a data item,
30 * rather than another radix_tree_node) is signalled by the low bit
31 * set in the root->rnode pointer.
32 *
33 * In this case root->height is also NULL, but the direct pointer tests are
34 * needed for RCU lookups when root->height is unreliable.
35 */
36#define RADIX_TREE_DIRECT_PTR 1
37
38static inline void *radix_tree_ptr_to_direct(void *ptr)
39{
40 return (void *)((unsigned long)ptr | RADIX_TREE_DIRECT_PTR);
41}
42
43static inline void *radix_tree_direct_to_ptr(void *ptr)
44{
45 return (void *)((unsigned long)ptr & ~RADIX_TREE_DIRECT_PTR);
46}
47
48static inline int radix_tree_is_direct_ptr(void *ptr)
49{
50 return (int)((unsigned long)ptr & RADIX_TREE_DIRECT_PTR);
51}
52
53/*** radix-tree API starts here ***/
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Nick Piggin612d6c12006-06-23 02:03:22 -070055#define RADIX_TREE_MAX_TAGS 2
56
57/* root tags are stored in gfp_mask, shifted by __GFP_BITS_SHIFT */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct radix_tree_root {
59 unsigned int height;
Al Virofd4f2df2005-10-21 03:18:50 -040060 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct radix_tree_node *rnode;
62};
63
64#define RADIX_TREE_INIT(mask) { \
65 .height = 0, \
66 .gfp_mask = (mask), \
67 .rnode = NULL, \
68}
69
70#define RADIX_TREE(name, mask) \
71 struct radix_tree_root name = RADIX_TREE_INIT(mask)
72
73#define INIT_RADIX_TREE(root, mask) \
74do { \
75 (root)->height = 0; \
76 (root)->gfp_mask = (mask); \
77 (root)->rnode = NULL; \
78} while (0)
79
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080080/**
81 * Radix-tree synchronization
82 *
83 * The radix-tree API requires that users provide all synchronisation (with
84 * specific exceptions, noted below).
85 *
86 * Synchronization of access to the data items being stored in the tree, and
87 * management of their lifetimes must be completely managed by API users.
88 *
89 * For API usage, in general,
Michael Opdenacker59c51592007-05-09 08:57:56 +020090 * - any function _modifying_ the tree or tags (inserting or deleting
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080091 * items, setting or clearing tags must exclude other modifications, and
92 * exclude any functions reading the tree.
Michael Opdenacker59c51592007-05-09 08:57:56 +020093 * - any function _reading_ the tree or tags (looking up items or tags,
Nick Piggin7cf9c2c2006-12-06 20:33:44 -080094 * gang lookups) must exclude modifications to the tree, but may occur
95 * concurrently with other readers.
96 *
97 * The notable exceptions to this rule are the following functions:
98 * radix_tree_lookup
99 * radix_tree_tag_get
100 * radix_tree_gang_lookup
101 * radix_tree_gang_lookup_tag
102 * radix_tree_tagged
103 *
104 * The first 4 functions are able to be called locklessly, using RCU. The
105 * caller must ensure calls to these functions are made within rcu_read_lock()
106 * regions. Other readers (lock-free or otherwise) and modifications may be
107 * running concurrently.
108 *
109 * It is still required that the caller manage the synchronization and lifetimes
110 * of the items. So if RCU lock-free lookups are used, typically this would mean
111 * that the items have their own locks, or are amenable to lock-free access; and
112 * that the items are freed by RCU (or only freed after having been deleted from
113 * the radix tree *and* a synchronize_rcu() grace period).
114 *
115 * (Note, rcu_assign_pointer and rcu_dereference are not needed to control
116 * access to data items when inserting into or looking up from the radix tree)
117 *
118 * radix_tree_tagged is able to be called without locking or RCU.
119 */
120
121/**
122 * radix_tree_deref_slot - dereference a slot
123 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
124 * Returns: item that was stored in that slot with any direct pointer flag
125 * removed.
126 *
127 * For use with radix_tree_lookup_slot(). Caller must hold tree at least read
128 * locked across slot lookup and dereference. More likely, will be used with
129 * radix_tree_replace_slot(), as well, so caller will hold tree write locked.
130 */
131static inline void *radix_tree_deref_slot(void **pslot)
132{
133 return radix_tree_direct_to_ptr(*pslot);
134}
135/**
136 * radix_tree_replace_slot - replace item in a slot
137 * @pslot: pointer to slot, returned by radix_tree_lookup_slot
138 * @item: new item to store in the slot.
139 *
140 * For use with radix_tree_lookup_slot(). Caller must hold tree write locked
141 * across slot lookup and replacement.
142 */
143static inline void radix_tree_replace_slot(void **pslot, void *item)
144{
145 BUG_ON(radix_tree_is_direct_ptr(item));
146 rcu_assign_pointer(*pslot,
147 (void *)((unsigned long)item |
148 ((unsigned long)*pslot & RADIX_TREE_DIRECT_PTR)));
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151int radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
152void *radix_tree_lookup(struct radix_tree_root *, unsigned long);
Hans Reisera4331362005-11-07 00:59:29 -0800153void **radix_tree_lookup_slot(struct radix_tree_root *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154void *radix_tree_delete(struct radix_tree_root *, unsigned long);
155unsigned int
156radix_tree_gang_lookup(struct radix_tree_root *root, void **results,
157 unsigned long first_index, unsigned int max_items);
Al Virodd0fc662005-10-07 07:46:04 +0100158int radix_tree_preload(gfp_t gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159void radix_tree_init(void);
160void *radix_tree_tag_set(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800161 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162void *radix_tree_tag_clear(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800163 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164int radix_tree_tag_get(struct radix_tree_root *root,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800165 unsigned long index, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166unsigned int
167radix_tree_gang_lookup_tag(struct radix_tree_root *root, void **results,
Jonathan Corbetdaff89f2006-03-25 03:08:05 -0800168 unsigned long first_index, unsigned int max_items,
169 unsigned int tag);
170int radix_tree_tagged(struct radix_tree_root *root, unsigned int tag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172static inline void radix_tree_preload_end(void)
173{
174 preempt_enable();
175}
176
177#endif /* _LINUX_RADIX_TREE_H */