xref: /netbsd-src/sys/external/bsd/drm2/linux/linux_xa.c (revision 4efeea51c6ca104c9ec5c0747229c0c39926eea7)
1*4efeea51Sriastradh /*	$NetBSD: linux_xa.c,v 1.4 2024/05/22 15:59:25 riastradh Exp $	*/
29f5becc9Sriastradh 
39f5becc9Sriastradh /*-
49f5becc9Sriastradh  * Copyright (c) 2021 The NetBSD Foundation, Inc.
59f5becc9Sriastradh  * All rights reserved.
69f5becc9Sriastradh  *
79f5becc9Sriastradh  * Redistribution and use in source and binary forms, with or without
89f5becc9Sriastradh  * modification, are permitted provided that the following conditions
99f5becc9Sriastradh  * are met:
109f5becc9Sriastradh  * 1. Redistributions of source code must retain the above copyright
119f5becc9Sriastradh  *    notice, this list of conditions and the following disclaimer.
129f5becc9Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
139f5becc9Sriastradh  *    notice, this list of conditions and the following disclaimer in the
149f5becc9Sriastradh  *    documentation and/or other materials provided with the distribution.
159f5becc9Sriastradh  *
169f5becc9Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
179f5becc9Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
189f5becc9Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
199f5becc9Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
209f5becc9Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
219f5becc9Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
229f5becc9Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
239f5becc9Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
249f5becc9Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
259f5becc9Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
269f5becc9Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
279f5becc9Sriastradh  */
289f5becc9Sriastradh 
299f5becc9Sriastradh #include <sys/cdefs.h>
30*4efeea51Sriastradh __KERNEL_RCSID(0, "$NetBSD: linux_xa.c,v 1.4 2024/05/22 15:59:25 riastradh Exp $");
319f5becc9Sriastradh 
32e2654e7dSriastradh /*
33e2654e7dSriastradh  * This is a lame-o implementation of the Linux xarray data type, which
34e2654e7dSriastradh  * implements a map from 64-bit integers to pointers.  The operations
35e2654e7dSriastradh  * it supports are designed to be implemented by a radix tree, but
36e2654e7dSriastradh  * NetBSD's radixtree(9) doesn't quite support them all, and it's a bit
37e2654e7dSriastradh  * of work to implement them, so this just uses a red/black tree
38e2654e7dSriastradh  * instead at the cost of some performance in certain types of lookups
39e2654e7dSriastradh  * (and negative-lookups -- finding a free key).
40e2654e7dSriastradh  */
419f5becc9Sriastradh 
42e2654e7dSriastradh #include <sys/rbtree.h>
439f5becc9Sriastradh 
449f5becc9Sriastradh #include <linux/xarray.h>
459f5becc9Sriastradh 
46e2654e7dSriastradh struct node {
47e2654e7dSriastradh 	struct rb_node	n_rb;
48e2654e7dSriastradh 	uint64_t	n_key;
49e2654e7dSriastradh 	void		*n_datum;
50e2654e7dSriastradh };
51e2654e7dSriastradh 
52e2654e7dSriastradh static int
compare_nodes(void * cookie,const void * va,const void * vb)53e2654e7dSriastradh compare_nodes(void *cookie, const void *va, const void *vb)
54e2654e7dSriastradh {
55e2654e7dSriastradh 	const struct node *a = va, *b = vb;
56e2654e7dSriastradh 
57e2654e7dSriastradh 	if (a->n_key < b->n_key)
58e2654e7dSriastradh 		return -1;
59e2654e7dSriastradh 	if (a->n_key > b->n_key)
60e2654e7dSriastradh 		return +1;
61e2654e7dSriastradh 	return 0;
62e2654e7dSriastradh }
63e2654e7dSriastradh 
64e2654e7dSriastradh static int
compare_node_key(void * cookie,const void * vn,const void * vk)65e2654e7dSriastradh compare_node_key(void *cookie, const void *vn, const void *vk)
66e2654e7dSriastradh {
67e2654e7dSriastradh 	const struct node *n = vn;
68e2654e7dSriastradh 	const uint64_t *k = vk;
69e2654e7dSriastradh 
70e2654e7dSriastradh 	if (n->n_key < *k)
71e2654e7dSriastradh 		return -1;
72e2654e7dSriastradh 	if (n->n_key > *k)
73e2654e7dSriastradh 		return +1;
74e2654e7dSriastradh 	return 0;
75e2654e7dSriastradh }
76e2654e7dSriastradh 
77e2654e7dSriastradh static const rb_tree_ops_t xa_rb_ops = {
78e2654e7dSriastradh 	.rbto_compare_nodes = compare_nodes,
79e2654e7dSriastradh 	.rbto_compare_key = compare_node_key,
80e2654e7dSriastradh 	.rbto_node_offset = offsetof(struct node, n_rb),
81e2654e7dSriastradh };
82e2654e7dSriastradh 
839f5becc9Sriastradh const struct xa_limit xa_limit_32b = { .min = 0, .max = UINT32_MAX };
849f5becc9Sriastradh 
859f5becc9Sriastradh void
xa_init_flags(struct xarray * xa,gfp_t gfp)869f5becc9Sriastradh xa_init_flags(struct xarray *xa, gfp_t gfp)
879f5becc9Sriastradh {
889f5becc9Sriastradh 
899f5becc9Sriastradh 	mutex_init(&xa->xa_lock, MUTEX_DEFAULT, IPL_VM);
90e2654e7dSriastradh 	rb_tree_init(&xa->xa_tree, &xa_rb_ops);
919f5becc9Sriastradh 	xa->xa_gfp = gfp;
929f5becc9Sriastradh }
939f5becc9Sriastradh 
949f5becc9Sriastradh void
xa_destroy(struct xarray * xa)959f5becc9Sriastradh xa_destroy(struct xarray *xa)
969f5becc9Sriastradh {
97e2654e7dSriastradh 	struct node *n;
989f5becc9Sriastradh 
99e2654e7dSriastradh 	/*
100e2654e7dSriastradh 	 * Linux allows xa to remain populated on destruction; it is
101e2654e7dSriastradh 	 * our job to free any internal node structures.
102e2654e7dSriastradh 	 */
103e2654e7dSriastradh 	while ((n = RB_TREE_MIN(&xa->xa_tree)) != NULL) {
104e2654e7dSriastradh 		rb_tree_remove_node(&xa->xa_tree, n);
105e2654e7dSriastradh 		kmem_free(n, sizeof(*n));
106e2654e7dSriastradh 	}
1079f5becc9Sriastradh 	mutex_destroy(&xa->xa_lock);
1089f5becc9Sriastradh }
1099f5becc9Sriastradh 
1109f5becc9Sriastradh void *
xa_load(struct xarray * xa,unsigned long key)1119f5becc9Sriastradh xa_load(struct xarray *xa, unsigned long key)
1129f5becc9Sriastradh {
113e2654e7dSriastradh 	const uint64_t key64 = key;
114e2654e7dSriastradh 	struct node *n;
1159f5becc9Sriastradh 
1169f5becc9Sriastradh 	/* XXX pserialize */
1179f5becc9Sriastradh 	mutex_enter(&xa->xa_lock);
118e2654e7dSriastradh 	n = rb_tree_find_node(&xa->xa_tree, &key64);
1199f5becc9Sriastradh 	mutex_exit(&xa->xa_lock);
1209f5becc9Sriastradh 
121e2654e7dSriastradh 	return n ? n->n_datum : NULL;
1229f5becc9Sriastradh }
1239f5becc9Sriastradh 
1249f5becc9Sriastradh void *
xa_store(struct xarray * xa,unsigned long key,void * datum,gfp_t gfp)1259f5becc9Sriastradh xa_store(struct xarray *xa, unsigned long key, void *datum, gfp_t gfp)
1269f5becc9Sriastradh {
127*4efeea51Sriastradh 	struct node *n, *collision, *recollision;
1289f5becc9Sriastradh 
1299f5becc9Sriastradh 	KASSERT(datum != NULL);
1309f5becc9Sriastradh 	KASSERT(((uintptr_t)datum & 0x3) == 0);
1319f5becc9Sriastradh 
132e2654e7dSriastradh 	n = kmem_zalloc(sizeof(*n), gfp & __GFP_WAIT ? KM_SLEEP : KM_NOSLEEP);
133e2654e7dSriastradh 	if (n == NULL)
134e2654e7dSriastradh 		return XA_ERROR(-ENOMEM);
135e2654e7dSriastradh 	n->n_key = key;
136e2654e7dSriastradh 	n->n_datum = datum;
1379f5becc9Sriastradh 
138e2654e7dSriastradh 	mutex_enter(&xa->xa_lock);
139e2654e7dSriastradh 	collision = rb_tree_insert_node(&xa->xa_tree, n);
140*4efeea51Sriastradh 	if (collision != n) {
141*4efeea51Sriastradh 		rb_tree_remove_node(&xa->xa_tree, n);
142*4efeea51Sriastradh 		recollision = rb_tree_insert_node(&xa->xa_tree, n);
143*4efeea51Sriastradh 		KASSERT(recollision == n);
144*4efeea51Sriastradh 	}
145e2654e7dSriastradh 	mutex_exit(&xa->xa_lock);
146e2654e7dSriastradh 
147e2654e7dSriastradh 	if (collision != n) {
148e2654e7dSriastradh 		datum = collision->n_datum;
149e2654e7dSriastradh 		kmem_free(collision, sizeof(*collision));
150e2654e7dSriastradh 	}
151e2654e7dSriastradh 	return datum;
1529f5becc9Sriastradh }
1539f5becc9Sriastradh 
1549f5becc9Sriastradh int
xa_alloc(struct xarray * xa,uint32_t * idp,void * datum,struct xa_limit limit,gfp_t gfp)1559f5becc9Sriastradh xa_alloc(struct xarray *xa, uint32_t *idp, void *datum, struct xa_limit limit,
1569f5becc9Sriastradh     gfp_t gfp)
1579f5becc9Sriastradh {
158e2654e7dSriastradh 	uint64_t key64 = limit.min;
159e2654e7dSriastradh 	struct node *n, *n1, *collision __diagused;
1609f5becc9Sriastradh 	int error;
1619f5becc9Sriastradh 
1629f5becc9Sriastradh 	KASSERTMSG(limit.min < limit.max, "min=%"PRIu32" max=%"PRIu32,
1639f5becc9Sriastradh 	    limit.min, limit.max);
1649f5becc9Sriastradh 
165e2654e7dSriastradh 	n = kmem_zalloc(sizeof(*n), gfp & __GFP_WAIT ? KM_SLEEP : KM_NOSLEEP);
166e2654e7dSriastradh 	if (n == NULL)
167e2654e7dSriastradh 		return -ENOMEM;
168e2654e7dSriastradh 	n->n_datum = datum;
169e2654e7dSriastradh 
170e2654e7dSriastradh 	mutex_enter(&xa->xa_lock);
171e2654e7dSriastradh 	while ((n1 = rb_tree_find_node_geq(&xa->xa_tree, &key64)) != NULL &&
172e2654e7dSriastradh 	    n1->n_key == key64) {
173e2654e7dSriastradh 		if (key64 == limit.max) {
174e2654e7dSriastradh 			error = -EBUSY;
1759f5becc9Sriastradh 			goto out;
1769f5becc9Sriastradh 		}
177e2654e7dSriastradh 		KASSERT(key64 < UINT32_MAX);
178e2654e7dSriastradh 		key64++;
179e2654e7dSriastradh 	}
180e2654e7dSriastradh 	/* Found a hole -- insert in it.  */
181e2654e7dSriastradh 	KASSERT(n1 == NULL || n1->n_key > key64);
182e2654e7dSriastradh 	n->n_key = key64;
183e2654e7dSriastradh 	collision = rb_tree_insert_node(&xa->xa_tree, n);
184e2654e7dSriastradh 	KASSERT(collision == n);
185e2654e7dSriastradh 	error = 0;
1869f5becc9Sriastradh out:	mutex_exit(&xa->xa_lock);
1879f5becc9Sriastradh 
188e2654e7dSriastradh 	if (error)
189e2654e7dSriastradh 		return error;
190e2654e7dSriastradh 	*idp = key64;
1919f5becc9Sriastradh 	return 0;
1929f5becc9Sriastradh }
1939f5becc9Sriastradh 
1949f5becc9Sriastradh void *
xa_find(struct xarray * xa,unsigned long * startp,unsigned long max,unsigned tagmask)1959f5becc9Sriastradh xa_find(struct xarray *xa, unsigned long *startp, unsigned long max,
1969f5becc9Sriastradh     unsigned tagmask)
1979f5becc9Sriastradh {
198e2654e7dSriastradh 	uint64_t key64 = *startp;
199e2654e7dSriastradh 	struct node *n = NULL;
200e2654e7dSriastradh 
201e2654e7dSriastradh 	KASSERT(tagmask == -1);	/* not yet supported */
2029f5becc9Sriastradh 
2039f5becc9Sriastradh 	mutex_enter(&xa->xa_lock);
204e2654e7dSriastradh 	n = rb_tree_find_node_geq(&xa->xa_tree, &key64);
2059f5becc9Sriastradh 	mutex_exit(&xa->xa_lock);
2069f5becc9Sriastradh 
207e2654e7dSriastradh 	if (n == NULL || n->n_key > max)
208e2654e7dSriastradh 		return NULL;
209e2654e7dSriastradh 
210e2654e7dSriastradh 	*startp = n->n_key;
211e2654e7dSriastradh 	return n->n_datum;
2129f5becc9Sriastradh }
2139f5becc9Sriastradh 
2149f5becc9Sriastradh void *
xa_find_after(struct xarray * xa,unsigned long * startp,unsigned long max,unsigned tagmask)2159f5becc9Sriastradh xa_find_after(struct xarray *xa, unsigned long *startp, unsigned long max,
2169f5becc9Sriastradh     unsigned tagmask)
2179f5becc9Sriastradh {
2180f42433cSriastradh 	unsigned long start = *startp + 1;
2199f5becc9Sriastradh 	void *found;
2209f5becc9Sriastradh 
2219f5becc9Sriastradh 	if (start == max)
2229f5becc9Sriastradh 		return NULL;
2239f5becc9Sriastradh 	found = xa_find(xa, &start, max, tagmask);
2249f5becc9Sriastradh 	if (found)
2259f5becc9Sriastradh 		*startp = start;
2269f5becc9Sriastradh 	return found;
2279f5becc9Sriastradh }
2289f5becc9Sriastradh 
2299f5becc9Sriastradh void *
xa_erase(struct xarray * xa,unsigned long key)2309f5becc9Sriastradh xa_erase(struct xarray *xa, unsigned long key)
2319f5becc9Sriastradh {
232e2654e7dSriastradh 	uint64_t key64 = key;
233e2654e7dSriastradh 	struct node *n;
234e2654e7dSriastradh 	void *datum = NULL;
2359f5becc9Sriastradh 
2369f5becc9Sriastradh 	mutex_enter(&xa->xa_lock);
237e2654e7dSriastradh 	n = rb_tree_find_node(&xa->xa_tree, &key64);
238e2654e7dSriastradh 	if (n)
239e2654e7dSriastradh 		rb_tree_remove_node(&xa->xa_tree, n);
2409f5becc9Sriastradh 	mutex_exit(&xa->xa_lock);
2419f5becc9Sriastradh 
242e2654e7dSriastradh 	if (n) {
243e2654e7dSriastradh 		datum = n->n_datum;
244e2654e7dSriastradh 		kmem_free(n, sizeof(*n));
245e2654e7dSriastradh 	}
2469f5becc9Sriastradh 	return datum;
2479f5becc9Sriastradh }
248