linux_radixtree.c revision 1.3 1 1.3 riastrad /* $NetBSD: linux_radixtree.c,v 1.3 2021/12/19 11:53:26 riastradh Exp $ */
2 1.1 riastrad
3 1.1 riastrad /*-
4 1.1 riastrad * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 riastrad * All rights reserved.
6 1.1 riastrad *
7 1.1 riastrad * Redistribution and use in source and binary forms, with or without
8 1.1 riastrad * modification, are permitted provided that the following conditions
9 1.1 riastrad * are met:
10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright
11 1.1 riastrad * notice, this list of conditions and the following disclaimer.
12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the
14 1.1 riastrad * documentation and/or other materials provided with the distribution.
15 1.1 riastrad *
16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
27 1.1 riastrad */
28 1.1 riastrad
29 1.1 riastrad #include <sys/cdefs.h>
30 1.3 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_radixtree.c,v 1.3 2021/12/19 11:53:26 riastradh Exp $");
31 1.1 riastrad
32 1.2 riastrad #include <sys/atomic.h>
33 1.2 riastrad #include <sys/kmem.h>
34 1.2 riastrad #include <sys/lock.h>
35 1.1 riastrad #include <sys/radixtree.h>
36 1.1 riastrad
37 1.1 riastrad #include <linux/gfp.h>
38 1.1 riastrad #include <linux/radix-tree.h>
39 1.2 riastrad #include <linux/rcupdate.h>
40 1.2 riastrad #include <linux/slab.h>
41 1.2 riastrad
42 1.2 riastrad /* XXX mega-kludgerific */
43 1.2 riastrad #define __UNVOLANST(p) ((void *)(unsigned long)(const volatile void *)(p))
44 1.1 riastrad
45 1.1 riastrad struct kludge {
46 1.1 riastrad uint64_t k_key;
47 1.1 riastrad void *k_datum;
48 1.2 riastrad struct rcu_head k_rcu;
49 1.1 riastrad };
50 1.1 riastrad
51 1.1 riastrad void
52 1.1 riastrad INIT_RADIX_TREE(struct radix_tree_root *root, gfp_t gfp)
53 1.1 riastrad {
54 1.1 riastrad
55 1.1 riastrad radix_tree_init_tree(&root->rtr_tree);
56 1.2 riastrad __cpu_simple_lock_init(&root->rtr_lock);
57 1.1 riastrad }
58 1.1 riastrad
59 1.1 riastrad int
60 1.1 riastrad radix_tree_insert(struct radix_tree_root *root, unsigned long key, void *datum)
61 1.1 riastrad {
62 1.1 riastrad struct kludge *kludge;
63 1.2 riastrad int error;
64 1.1 riastrad
65 1.2 riastrad /* XXX No way to know whether the caller can sleep or not... */
66 1.2 riastrad if ((kludge = kzalloc(sizeof(*kludge), GFP_NOWAIT)) == NULL)
67 1.1 riastrad return -ENOMEM;
68 1.1 riastrad
69 1.1 riastrad kludge->k_key = key;
70 1.1 riastrad kludge->k_datum = datum;
71 1.1 riastrad
72 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
73 1.2 riastrad error = radix_tree_insert_node(&root->rtr_tree, key, kludge);
74 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
75 1.2 riastrad
76 1.1 riastrad /* XXX errno NetBSD->Linux */
77 1.2 riastrad return -error;
78 1.1 riastrad }
79 1.1 riastrad
80 1.1 riastrad void *
81 1.1 riastrad radix_tree_delete(struct radix_tree_root *root, unsigned long key)
82 1.1 riastrad {
83 1.1 riastrad struct kludge *kludge;
84 1.1 riastrad void *datum = NULL;
85 1.1 riastrad
86 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
87 1.2 riastrad kludge = radix_tree_remove_node(&root->rtr_tree, key);
88 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
89 1.2 riastrad if (kludge == NULL)
90 1.1 riastrad return NULL;
91 1.1 riastrad
92 1.1 riastrad datum = kludge->k_datum;
93 1.2 riastrad kfree_rcu(kludge, k_rcu);
94 1.1 riastrad
95 1.1 riastrad return datum;
96 1.1 riastrad }
97 1.1 riastrad
98 1.1 riastrad bool
99 1.1 riastrad radix_tree_empty(struct radix_tree_root *root)
100 1.1 riastrad {
101 1.2 riastrad bool empty;
102 1.2 riastrad
103 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
104 1.2 riastrad empty = radix_tree_empty_tree_p(&root->rtr_tree);
105 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
106 1.1 riastrad
107 1.2 riastrad return empty;
108 1.1 riastrad }
109 1.1 riastrad
110 1.1 riastrad void *
111 1.1 riastrad radix_tree_lookup(const struct radix_tree_root *root, unsigned long key)
112 1.1 riastrad {
113 1.1 riastrad struct kludge *kludge;
114 1.1 riastrad
115 1.2 riastrad __cpu_simple_lock(__UNVOLANST(&root->rtr_lock));
116 1.2 riastrad kludge = radix_tree_lookup_node(__UNVOLANST(&root->rtr_tree), key);
117 1.2 riastrad __cpu_simple_unlock(__UNVOLANST(&root->rtr_lock));
118 1.1 riastrad if (kludge == NULL)
119 1.2 riastrad return NULL;
120 1.1 riastrad
121 1.1 riastrad return kludge->k_datum;
122 1.1 riastrad }
123 1.1 riastrad
124 1.1 riastrad void *
125 1.1 riastrad radix_tree_deref_slot(void **slot)
126 1.1 riastrad {
127 1.1 riastrad
128 1.1 riastrad return atomic_load_consume(slot);
129 1.1 riastrad }
130 1.1 riastrad
131 1.1 riastrad void **
132 1.1 riastrad radix_tree_iter_init(struct radix_tree_iter *I, unsigned long start)
133 1.1 riastrad {
134 1.1 riastrad
135 1.1 riastrad I->index = start;
136 1.1 riastrad I->rti_tree = NULL;
137 1.1 riastrad return NULL;
138 1.1 riastrad }
139 1.1 riastrad
140 1.1 riastrad void **
141 1.1 riastrad radix_tree_next_chunk(const struct radix_tree_root *root,
142 1.1 riastrad struct radix_tree_iter *I, unsigned flags)
143 1.1 riastrad {
144 1.1 riastrad void *result;
145 1.1 riastrad struct kludge *kludge;
146 1.2 riastrad unsigned nresults;
147 1.1 riastrad
148 1.1 riastrad KASSERT(flags == 0);
149 1.2 riastrad __cpu_simple_lock(__UNVOLANST(&root->rtr_lock));
150 1.2 riastrad nresults = radix_tree_gang_lookup_node(__UNVOLANST(&root->rtr_tree),
151 1.2 riastrad I->index, &result, /*maxresults*/1, /*dense*/false);
152 1.2 riastrad __cpu_simple_unlock(__UNVOLANST(&root->rtr_lock));
153 1.2 riastrad if (nresults == 0)
154 1.1 riastrad return NULL;
155 1.2 riastrad KASSERT(nresults == 1);
156 1.1 riastrad
157 1.1 riastrad kludge = result;
158 1.1 riastrad
159 1.1 riastrad I->index = kludge->k_key;
160 1.2 riastrad I->rti_tree = root;
161 1.1 riastrad return &kludge->k_datum;
162 1.1 riastrad }
163 1.1 riastrad
164 1.1 riastrad void **
165 1.1 riastrad radix_tree_next_slot(void **slot, struct radix_tree_iter *I, unsigned flags)
166 1.1 riastrad {
167 1.1 riastrad struct kludge *kludge;
168 1.1 riastrad void *result;
169 1.2 riastrad unsigned nresults;
170 1.1 riastrad
171 1.1 riastrad KASSERT(flags == 0);
172 1.2 riastrad
173 1.1 riastrad kludge = container_of(slot, struct kludge, k_datum);
174 1.2 riastrad
175 1.2 riastrad __cpu_simple_lock(__UNVOLANST(&I->rti_tree->rtr_lock));
176 1.2 riastrad nresults = radix_tree_gang_lookup_node(
177 1.2 riastrad __UNVOLANST(&I->rti_tree->rtr_tree),
178 1.2 riastrad kludge->k_key, &result, /*maxresults*/1, /*dense*/true);
179 1.2 riastrad __cpu_simple_unlock(__UNVOLANST(&I->rti_tree->rtr_lock));
180 1.2 riastrad if (nresults == 0)
181 1.1 riastrad return NULL;
182 1.2 riastrad KASSERT(nresults == 1);
183 1.1 riastrad
184 1.1 riastrad kludge = result;
185 1.1 riastrad
186 1.1 riastrad I->index = kludge->k_key;
187 1.2 riastrad /* XXX Hope the tree hasn't changed! */
188 1.1 riastrad return &kludge->k_datum;
189 1.1 riastrad }
190 1.1 riastrad
191 1.1 riastrad void
192 1.1 riastrad radix_tree_iter_delete(struct radix_tree_root *root, struct radix_tree_iter *I,
193 1.1 riastrad void **slot)
194 1.1 riastrad {
195 1.1 riastrad struct kludge *kludge = container_of(slot, struct kludge, k_datum);
196 1.1 riastrad struct kludge *kludge0 __diagused;
197 1.1 riastrad
198 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
199 1.1 riastrad kludge0 = radix_tree_remove_node(&root->rtr_tree, kludge->k_key);
200 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
201 1.2 riastrad
202 1.1 riastrad KASSERT(kludge0 == kludge);
203 1.1 riastrad }
204