linux_radixtree.c revision 1.2 1 1.2 riastrad /* $NetBSD: linux_radixtree.c,v 1.2 2021/12/19 11:51:51 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.2 riastrad __KERNEL_RCSID(0, "$NetBSD: linux_radixtree.c,v 1.2 2021/12/19 11:51:51 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 membar_exit();
73 1.2 riastrad
74 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
75 1.2 riastrad error = radix_tree_insert_node(&root->rtr_tree, key, kludge);
76 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
77 1.2 riastrad
78 1.1 riastrad /* XXX errno NetBSD->Linux */
79 1.2 riastrad return -error;
80 1.1 riastrad }
81 1.1 riastrad
82 1.1 riastrad void *
83 1.1 riastrad radix_tree_delete(struct radix_tree_root *root, unsigned long key)
84 1.1 riastrad {
85 1.1 riastrad struct kludge *kludge;
86 1.1 riastrad void *datum = NULL;
87 1.1 riastrad
88 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
89 1.2 riastrad kludge = radix_tree_remove_node(&root->rtr_tree, key);
90 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
91 1.2 riastrad if (kludge == NULL)
92 1.1 riastrad return NULL;
93 1.1 riastrad
94 1.1 riastrad datum = kludge->k_datum;
95 1.2 riastrad kfree_rcu(kludge, k_rcu);
96 1.1 riastrad
97 1.1 riastrad return datum;
98 1.1 riastrad }
99 1.1 riastrad
100 1.1 riastrad bool
101 1.1 riastrad radix_tree_empty(struct radix_tree_root *root)
102 1.1 riastrad {
103 1.2 riastrad bool empty;
104 1.2 riastrad
105 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
106 1.2 riastrad empty = radix_tree_empty_tree_p(&root->rtr_tree);
107 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
108 1.1 riastrad
109 1.2 riastrad return empty;
110 1.1 riastrad }
111 1.1 riastrad
112 1.1 riastrad void *
113 1.1 riastrad radix_tree_lookup(const struct radix_tree_root *root, unsigned long key)
114 1.1 riastrad {
115 1.1 riastrad struct kludge *kludge;
116 1.1 riastrad
117 1.2 riastrad __cpu_simple_lock(__UNVOLANST(&root->rtr_lock));
118 1.2 riastrad kludge = radix_tree_lookup_node(__UNVOLANST(&root->rtr_tree), key);
119 1.2 riastrad __cpu_simple_unlock(__UNVOLANST(&root->rtr_lock));
120 1.1 riastrad if (kludge == NULL)
121 1.2 riastrad return NULL;
122 1.1 riastrad
123 1.1 riastrad return kludge->k_datum;
124 1.1 riastrad }
125 1.1 riastrad
126 1.1 riastrad void *
127 1.1 riastrad radix_tree_deref_slot(void **slot)
128 1.1 riastrad {
129 1.1 riastrad
130 1.1 riastrad return atomic_load_consume(slot);
131 1.1 riastrad }
132 1.1 riastrad
133 1.1 riastrad void **
134 1.1 riastrad radix_tree_iter_init(struct radix_tree_iter *I, unsigned long start)
135 1.1 riastrad {
136 1.1 riastrad
137 1.1 riastrad I->index = start;
138 1.1 riastrad I->rti_tree = NULL;
139 1.1 riastrad return NULL;
140 1.1 riastrad }
141 1.1 riastrad
142 1.1 riastrad void **
143 1.1 riastrad radix_tree_next_chunk(const struct radix_tree_root *root,
144 1.1 riastrad struct radix_tree_iter *I, unsigned flags)
145 1.1 riastrad {
146 1.1 riastrad void *result;
147 1.1 riastrad struct kludge *kludge;
148 1.2 riastrad unsigned nresults;
149 1.1 riastrad
150 1.1 riastrad KASSERT(flags == 0);
151 1.2 riastrad __cpu_simple_lock(__UNVOLANST(&root->rtr_lock));
152 1.2 riastrad nresults = radix_tree_gang_lookup_node(__UNVOLANST(&root->rtr_tree),
153 1.2 riastrad I->index, &result, /*maxresults*/1, /*dense*/false);
154 1.2 riastrad __cpu_simple_unlock(__UNVOLANST(&root->rtr_lock));
155 1.2 riastrad if (nresults == 0)
156 1.1 riastrad return NULL;
157 1.2 riastrad KASSERT(nresults == 1);
158 1.1 riastrad
159 1.1 riastrad kludge = result;
160 1.1 riastrad
161 1.1 riastrad I->index = kludge->k_key;
162 1.2 riastrad I->rti_tree = root;
163 1.1 riastrad return &kludge->k_datum;
164 1.1 riastrad }
165 1.1 riastrad
166 1.1 riastrad void **
167 1.1 riastrad radix_tree_next_slot(void **slot, struct radix_tree_iter *I, unsigned flags)
168 1.1 riastrad {
169 1.1 riastrad struct kludge *kludge;
170 1.1 riastrad void *result;
171 1.2 riastrad unsigned nresults;
172 1.1 riastrad
173 1.1 riastrad KASSERT(flags == 0);
174 1.2 riastrad
175 1.1 riastrad kludge = container_of(slot, struct kludge, k_datum);
176 1.2 riastrad
177 1.2 riastrad __cpu_simple_lock(__UNVOLANST(&I->rti_tree->rtr_lock));
178 1.2 riastrad nresults = radix_tree_gang_lookup_node(
179 1.2 riastrad __UNVOLANST(&I->rti_tree->rtr_tree),
180 1.2 riastrad kludge->k_key, &result, /*maxresults*/1, /*dense*/true);
181 1.2 riastrad __cpu_simple_unlock(__UNVOLANST(&I->rti_tree->rtr_lock));
182 1.2 riastrad if (nresults == 0)
183 1.1 riastrad return NULL;
184 1.2 riastrad KASSERT(nresults == 1);
185 1.1 riastrad
186 1.1 riastrad kludge = result;
187 1.1 riastrad
188 1.1 riastrad I->index = kludge->k_key;
189 1.2 riastrad /* XXX Hope the tree hasn't changed! */
190 1.1 riastrad return &kludge->k_datum;
191 1.1 riastrad }
192 1.1 riastrad
193 1.1 riastrad void
194 1.1 riastrad radix_tree_iter_delete(struct radix_tree_root *root, struct radix_tree_iter *I,
195 1.1 riastrad void **slot)
196 1.1 riastrad {
197 1.1 riastrad struct kludge *kludge = container_of(slot, struct kludge, k_datum);
198 1.1 riastrad struct kludge *kludge0 __diagused;
199 1.1 riastrad
200 1.2 riastrad __cpu_simple_lock(&root->rtr_lock);
201 1.1 riastrad kludge0 = radix_tree_remove_node(&root->rtr_tree, kludge->k_key);
202 1.2 riastrad __cpu_simple_unlock(&root->rtr_lock);
203 1.2 riastrad
204 1.1 riastrad KASSERT(kludge0 == kludge);
205 1.1 riastrad }
206