xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_radixtree.c (revision 2dd3c69f22dffee7a392601053c36baa36502a07)
1*2dd3c69fSriastradh /*	$NetBSD: linux_radixtree.c,v 1.4 2021/12/19 12:07:29 riastradh Exp $	*/
2c3320b62Sriastradh 
3c3320b62Sriastradh /*-
4c3320b62Sriastradh  * Copyright (c) 2021 The NetBSD Foundation, Inc.
5c3320b62Sriastradh  * All rights reserved.
6c3320b62Sriastradh  *
7c3320b62Sriastradh  * Redistribution and use in source and binary forms, with or without
8c3320b62Sriastradh  * modification, are permitted provided that the following conditions
9c3320b62Sriastradh  * are met:
10c3320b62Sriastradh  * 1. Redistributions of source code must retain the above copyright
11c3320b62Sriastradh  *    notice, this list of conditions and the following disclaimer.
12c3320b62Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
13c3320b62Sriastradh  *    notice, this list of conditions and the following disclaimer in the
14c3320b62Sriastradh  *    documentation and/or other materials provided with the distribution.
15c3320b62Sriastradh  *
16c3320b62Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17c3320b62Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18c3320b62Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19c3320b62Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20c3320b62Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21c3320b62Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22c3320b62Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23c3320b62Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24c3320b62Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25c3320b62Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26c3320b62Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
27c3320b62Sriastradh  */
28c3320b62Sriastradh 
29c3320b62Sriastradh #include <sys/cdefs.h>
30*2dd3c69fSriastradh __KERNEL_RCSID(0, "$NetBSD: linux_radixtree.c,v 1.4 2021/12/19 12:07:29 riastradh Exp $");
31c3320b62Sriastradh 
3228aad042Sriastradh #include <sys/atomic.h>
3328aad042Sriastradh #include <sys/kmem.h>
3428aad042Sriastradh #include <sys/lock.h>
35c3320b62Sriastradh #include <sys/radixtree.h>
36c3320b62Sriastradh 
37c3320b62Sriastradh #include <linux/gfp.h>
38c3320b62Sriastradh #include <linux/radix-tree.h>
3928aad042Sriastradh #include <linux/rcupdate.h>
4028aad042Sriastradh #include <linux/slab.h>
4128aad042Sriastradh 
4228aad042Sriastradh /* XXX mega-kludgerific */
4328aad042Sriastradh #define	__UNVOLANST(p)	((void *)(unsigned long)(const volatile void *)(p))
44c3320b62Sriastradh 
45c3320b62Sriastradh struct kludge {
46c3320b62Sriastradh 	uint64_t	k_key;
47c3320b62Sriastradh 	void		*k_datum;
4828aad042Sriastradh 	struct rcu_head	k_rcu;
49c3320b62Sriastradh };
50c3320b62Sriastradh 
51c3320b62Sriastradh void
INIT_RADIX_TREE(struct radix_tree_root * root,gfp_t gfp)52c3320b62Sriastradh INIT_RADIX_TREE(struct radix_tree_root *root, gfp_t gfp)
53c3320b62Sriastradh {
54c3320b62Sriastradh 
55c3320b62Sriastradh 	radix_tree_init_tree(&root->rtr_tree);
5628aad042Sriastradh 	__cpu_simple_lock_init(&root->rtr_lock);
57c3320b62Sriastradh }
58c3320b62Sriastradh 
59c3320b62Sriastradh int
radix_tree_insert(struct radix_tree_root * root,unsigned long key,void * datum)60c3320b62Sriastradh radix_tree_insert(struct radix_tree_root *root, unsigned long key, void *datum)
61c3320b62Sriastradh {
62c3320b62Sriastradh 	struct kludge *kludge;
6328aad042Sriastradh 	int error;
64c3320b62Sriastradh 
6528aad042Sriastradh 	/* XXX No way to know whether the caller can sleep or not...  */
66*2dd3c69fSriastradh 	if ((kludge = kzalloc(sizeof(*kludge), GFP_ATOMIC)) == NULL)
67c3320b62Sriastradh 		return -ENOMEM;
68c3320b62Sriastradh 
69c3320b62Sriastradh 	kludge->k_key = key;
70c3320b62Sriastradh 	kludge->k_datum = datum;
71c3320b62Sriastradh 
7228aad042Sriastradh 	__cpu_simple_lock(&root->rtr_lock);
7328aad042Sriastradh 	error = radix_tree_insert_node(&root->rtr_tree, key, kludge);
7428aad042Sriastradh 	__cpu_simple_unlock(&root->rtr_lock);
7528aad042Sriastradh 
76c3320b62Sriastradh 	/* XXX errno NetBSD->Linux */
7728aad042Sriastradh 	return -error;
78c3320b62Sriastradh }
79c3320b62Sriastradh 
80c3320b62Sriastradh void *
radix_tree_delete(struct radix_tree_root * root,unsigned long key)81c3320b62Sriastradh radix_tree_delete(struct radix_tree_root *root, unsigned long key)
82c3320b62Sriastradh {
83c3320b62Sriastradh 	struct kludge *kludge;
84c3320b62Sriastradh 	void *datum = NULL;
85c3320b62Sriastradh 
8628aad042Sriastradh 	__cpu_simple_lock(&root->rtr_lock);
8728aad042Sriastradh 	kludge = radix_tree_remove_node(&root->rtr_tree, key);
8828aad042Sriastradh 	__cpu_simple_unlock(&root->rtr_lock);
8928aad042Sriastradh 	if (kludge == NULL)
90c3320b62Sriastradh 		return NULL;
91c3320b62Sriastradh 
92c3320b62Sriastradh 	datum = kludge->k_datum;
9328aad042Sriastradh 	kfree_rcu(kludge, k_rcu);
94c3320b62Sriastradh 
95c3320b62Sriastradh 	return datum;
96c3320b62Sriastradh }
97c3320b62Sriastradh 
98c3320b62Sriastradh bool
radix_tree_empty(struct radix_tree_root * root)99c3320b62Sriastradh radix_tree_empty(struct radix_tree_root *root)
100c3320b62Sriastradh {
10128aad042Sriastradh 	bool empty;
102c3320b62Sriastradh 
10328aad042Sriastradh 	__cpu_simple_lock(&root->rtr_lock);
10428aad042Sriastradh 	empty = radix_tree_empty_tree_p(&root->rtr_tree);
10528aad042Sriastradh 	__cpu_simple_unlock(&root->rtr_lock);
10628aad042Sriastradh 
10728aad042Sriastradh 	return empty;
108c3320b62Sriastradh }
109c3320b62Sriastradh 
110c3320b62Sriastradh void *
radix_tree_lookup(const struct radix_tree_root * root,unsigned long key)111c3320b62Sriastradh radix_tree_lookup(const struct radix_tree_root *root, unsigned long key)
112c3320b62Sriastradh {
113c3320b62Sriastradh 	struct kludge *kludge;
114c3320b62Sriastradh 
11528aad042Sriastradh 	__cpu_simple_lock(__UNVOLANST(&root->rtr_lock));
11628aad042Sriastradh 	kludge = radix_tree_lookup_node(__UNVOLANST(&root->rtr_tree), key);
11728aad042Sriastradh 	__cpu_simple_unlock(__UNVOLANST(&root->rtr_lock));
118c3320b62Sriastradh 	if (kludge == NULL)
11928aad042Sriastradh 		return NULL;
120c3320b62Sriastradh 
121c3320b62Sriastradh 	return kludge->k_datum;
122c3320b62Sriastradh }
123c3320b62Sriastradh 
124c3320b62Sriastradh void *
radix_tree_deref_slot(void ** slot)125c3320b62Sriastradh radix_tree_deref_slot(void **slot)
126c3320b62Sriastradh {
127c3320b62Sriastradh 
128c3320b62Sriastradh 	return atomic_load_consume(slot);
129c3320b62Sriastradh }
130c3320b62Sriastradh 
131c3320b62Sriastradh void **
radix_tree_iter_init(struct radix_tree_iter * I,unsigned long start)132c3320b62Sriastradh radix_tree_iter_init(struct radix_tree_iter *I, unsigned long start)
133c3320b62Sriastradh {
134c3320b62Sriastradh 
135c3320b62Sriastradh 	I->index = start;
136c3320b62Sriastradh 	I->rti_tree = NULL;
137c3320b62Sriastradh 	return NULL;
138c3320b62Sriastradh }
139c3320b62Sriastradh 
140c3320b62Sriastradh void **
radix_tree_next_chunk(const struct radix_tree_root * root,struct radix_tree_iter * I,unsigned flags)141c3320b62Sriastradh radix_tree_next_chunk(const struct radix_tree_root *root,
142c3320b62Sriastradh     struct radix_tree_iter *I, unsigned flags)
143c3320b62Sriastradh {
144c3320b62Sriastradh 	void *result;
145c3320b62Sriastradh 	struct kludge *kludge;
14628aad042Sriastradh 	unsigned nresults;
147c3320b62Sriastradh 
148c3320b62Sriastradh 	KASSERT(flags == 0);
14928aad042Sriastradh 	__cpu_simple_lock(__UNVOLANST(&root->rtr_lock));
15028aad042Sriastradh 	nresults = radix_tree_gang_lookup_node(__UNVOLANST(&root->rtr_tree),
15128aad042Sriastradh 	    I->index, &result, /*maxresults*/1, /*dense*/false);
15228aad042Sriastradh 	__cpu_simple_unlock(__UNVOLANST(&root->rtr_lock));
15328aad042Sriastradh 	if (nresults == 0)
154c3320b62Sriastradh 		return NULL;
15528aad042Sriastradh 	KASSERT(nresults == 1);
156c3320b62Sriastradh 
157c3320b62Sriastradh 	kludge = result;
158c3320b62Sriastradh 
159c3320b62Sriastradh 	I->index = kludge->k_key;
16028aad042Sriastradh 	I->rti_tree = root;
161c3320b62Sriastradh 	return &kludge->k_datum;
162c3320b62Sriastradh }
163c3320b62Sriastradh 
164c3320b62Sriastradh void **
radix_tree_next_slot(void ** slot,struct radix_tree_iter * I,unsigned flags)165c3320b62Sriastradh radix_tree_next_slot(void **slot, struct radix_tree_iter *I, unsigned flags)
166c3320b62Sriastradh {
167c3320b62Sriastradh 	struct kludge *kludge;
168c3320b62Sriastradh 	void *result;
16928aad042Sriastradh 	unsigned nresults;
170c3320b62Sriastradh 
171c3320b62Sriastradh 	KASSERT(flags == 0);
17228aad042Sriastradh 
173c3320b62Sriastradh 	kludge = container_of(slot, struct kludge, k_datum);
17428aad042Sriastradh 
17528aad042Sriastradh 	__cpu_simple_lock(__UNVOLANST(&I->rti_tree->rtr_lock));
17628aad042Sriastradh 	nresults = radix_tree_gang_lookup_node(
17728aad042Sriastradh 	    __UNVOLANST(&I->rti_tree->rtr_tree),
17828aad042Sriastradh 	    kludge->k_key, &result, /*maxresults*/1, /*dense*/true);
17928aad042Sriastradh 	__cpu_simple_unlock(__UNVOLANST(&I->rti_tree->rtr_lock));
18028aad042Sriastradh 	if (nresults == 0)
181c3320b62Sriastradh 		return NULL;
18228aad042Sriastradh 	KASSERT(nresults == 1);
183c3320b62Sriastradh 
184c3320b62Sriastradh 	kludge = result;
185c3320b62Sriastradh 
186c3320b62Sriastradh 	I->index = kludge->k_key;
18728aad042Sriastradh 	/* XXX Hope the tree hasn't changed!  */
188c3320b62Sriastradh 	return &kludge->k_datum;
189c3320b62Sriastradh }
190c3320b62Sriastradh 
191c3320b62Sriastradh void
radix_tree_iter_delete(struct radix_tree_root * root,struct radix_tree_iter * I,void ** slot)192c3320b62Sriastradh radix_tree_iter_delete(struct radix_tree_root *root, struct radix_tree_iter *I,
193c3320b62Sriastradh     void **slot)
194c3320b62Sriastradh {
195c3320b62Sriastradh 	struct kludge *kludge = container_of(slot, struct kludge, k_datum);
196c3320b62Sriastradh 	struct kludge *kludge0 __diagused;
197c3320b62Sriastradh 
19828aad042Sriastradh 	__cpu_simple_lock(&root->rtr_lock);
199c3320b62Sriastradh 	kludge0 = radix_tree_remove_node(&root->rtr_tree, kludge->k_key);
20028aad042Sriastradh 	__cpu_simple_unlock(&root->rtr_lock);
20128aad042Sriastradh 
202c3320b62Sriastradh 	KASSERT(kludge0 == kludge);
203c3320b62Sriastradh }
204