Home | History | Annotate | Line # | Download | only in linux
linux_xa.c revision 1.3
      1 /*	$NetBSD: linux_xa.c,v 1.3 2021/12/19 12:05:25 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  * POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: linux_xa.c,v 1.3 2021/12/19 12:05:25 riastradh Exp $");
     31 
     32 /*
     33  * This is a lame-o implementation of the Linux xarray data type, which
     34  * implements a map from 64-bit integers to pointers.  The operations
     35  * it supports are designed to be implemented by a radix tree, but
     36  * NetBSD's radixtree(9) doesn't quite support them all, and it's a bit
     37  * of work to implement them, so this just uses a red/black tree
     38  * instead at the cost of some performance in certain types of lookups
     39  * (and negative-lookups -- finding a free key).
     40  */
     41 
     42 #include <sys/rbtree.h>
     43 
     44 #include <linux/xarray.h>
     45 
     46 struct node {
     47 	struct rb_node	n_rb;
     48 	uint64_t	n_key;
     49 	void		*n_datum;
     50 };
     51 
     52 static int
     53 compare_nodes(void *cookie, const void *va, const void *vb)
     54 {
     55 	const struct node *a = va, *b = vb;
     56 
     57 	if (a->n_key < b->n_key)
     58 		return -1;
     59 	if (a->n_key > b->n_key)
     60 		return +1;
     61 	return 0;
     62 }
     63 
     64 static int
     65 compare_node_key(void *cookie, const void *vn, const void *vk)
     66 {
     67 	const struct node *n = vn;
     68 	const uint64_t *k = vk;
     69 
     70 	if (n->n_key < *k)
     71 		return -1;
     72 	if (n->n_key > *k)
     73 		return +1;
     74 	return 0;
     75 }
     76 
     77 static const rb_tree_ops_t xa_rb_ops = {
     78 	.rbto_compare_nodes = compare_nodes,
     79 	.rbto_compare_key = compare_node_key,
     80 	.rbto_node_offset = offsetof(struct node, n_rb),
     81 };
     82 
     83 const struct xa_limit xa_limit_32b = { .min = 0, .max = UINT32_MAX };
     84 
     85 void
     86 xa_init_flags(struct xarray *xa, gfp_t gfp)
     87 {
     88 
     89 	mutex_init(&xa->xa_lock, MUTEX_DEFAULT, IPL_VM);
     90 	rb_tree_init(&xa->xa_tree, &xa_rb_ops);
     91 	xa->xa_gfp = gfp;
     92 }
     93 
     94 void
     95 xa_destroy(struct xarray *xa)
     96 {
     97 	struct node *n;
     98 
     99 	/*
    100 	 * Linux allows xa to remain populated on destruction; it is
    101 	 * our job to free any internal node structures.
    102 	 */
    103 	while ((n = RB_TREE_MIN(&xa->xa_tree)) != NULL) {
    104 		rb_tree_remove_node(&xa->xa_tree, n);
    105 		kmem_free(n, sizeof(*n));
    106 	}
    107 	mutex_destroy(&xa->xa_lock);
    108 }
    109 
    110 void *
    111 xa_load(struct xarray *xa, unsigned long key)
    112 {
    113 	const uint64_t key64 = key;
    114 	struct node *n;
    115 
    116 	/* XXX pserialize */
    117 	mutex_enter(&xa->xa_lock);
    118 	n = rb_tree_find_node(&xa->xa_tree, &key64);
    119 	mutex_exit(&xa->xa_lock);
    120 
    121 	return n ? n->n_datum : NULL;
    122 }
    123 
    124 void *
    125 xa_store(struct xarray *xa, unsigned long key, void *datum, gfp_t gfp)
    126 {
    127 	struct node *n, *collision;
    128 
    129 	KASSERT(datum != NULL);
    130 	KASSERT(((uintptr_t)datum & 0x3) == 0);
    131 
    132 	n = kmem_zalloc(sizeof(*n), gfp & __GFP_WAIT ? KM_SLEEP : KM_NOSLEEP);
    133 	if (n == NULL)
    134 		return XA_ERROR(-ENOMEM);
    135 	n->n_key = key;
    136 	n->n_datum = datum;
    137 
    138 	mutex_enter(&xa->xa_lock);
    139 	collision = rb_tree_insert_node(&xa->xa_tree, n);
    140 	mutex_exit(&xa->xa_lock);
    141 
    142 	if (collision != n) {
    143 		datum = collision->n_datum;
    144 		kmem_free(collision, sizeof(*collision));
    145 	}
    146 	return datum;
    147 }
    148 
    149 int
    150 xa_alloc(struct xarray *xa, uint32_t *idp, void *datum, struct xa_limit limit,
    151     gfp_t gfp)
    152 {
    153 	uint64_t key64 = limit.min;
    154 	struct node *n, *n1, *collision __diagused;
    155 	int error;
    156 
    157 	KASSERTMSG(limit.min < limit.max, "min=%"PRIu32" max=%"PRIu32,
    158 	    limit.min, limit.max);
    159 
    160 	n = kmem_zalloc(sizeof(*n), gfp & __GFP_WAIT ? KM_SLEEP : KM_NOSLEEP);
    161 	if (n == NULL)
    162 		return -ENOMEM;
    163 	n->n_datum = datum;
    164 
    165 	mutex_enter(&xa->xa_lock);
    166 	while ((n1 = rb_tree_find_node_geq(&xa->xa_tree, &key64)) != NULL &&
    167 	    n1->n_key == key64) {
    168 		if (key64 == limit.max) {
    169 			error = -EBUSY;
    170 			goto out;
    171 		}
    172 		KASSERT(key64 < UINT32_MAX);
    173 		key64++;
    174 	}
    175 	/* Found a hole -- insert in it.  */
    176 	KASSERT(n1 == NULL || n1->n_key > key64);
    177 	n->n_key = key64;
    178 	collision = rb_tree_insert_node(&xa->xa_tree, n);
    179 	KASSERT(collision == n);
    180 	error = 0;
    181 out:	mutex_exit(&xa->xa_lock);
    182 
    183 	if (error)
    184 		return error;
    185 	*idp = key64;
    186 	return 0;
    187 }
    188 
    189 void *
    190 xa_find(struct xarray *xa, unsigned long *startp, unsigned long max,
    191     unsigned tagmask)
    192 {
    193 	uint64_t key64 = *startp;
    194 	struct node *n = NULL;
    195 
    196 	KASSERT(tagmask == -1);	/* not yet supported */
    197 
    198 	mutex_enter(&xa->xa_lock);
    199 	n = rb_tree_find_node_geq(&xa->xa_tree, &key64);
    200 	mutex_exit(&xa->xa_lock);
    201 
    202 	if (n == NULL || n->n_key > max)
    203 		return NULL;
    204 
    205 	*startp = n->n_key;
    206 	return n->n_datum;
    207 }
    208 
    209 void *
    210 xa_find_after(struct xarray *xa, unsigned long *startp, unsigned long max,
    211     unsigned tagmask)
    212 {
    213 	unsigned long start = *startp + 1;
    214 	void *found;
    215 
    216 	if (start == max)
    217 		return NULL;
    218 	found = xa_find(xa, &start, max, tagmask);
    219 	if (found)
    220 		*startp = start;
    221 	return found;
    222 }
    223 
    224 void *
    225 xa_erase(struct xarray *xa, unsigned long key)
    226 {
    227 	uint64_t key64 = key;
    228 	struct node *n;
    229 	void *datum = NULL;
    230 
    231 	mutex_enter(&xa->xa_lock);
    232 	n = rb_tree_find_node(&xa->xa_tree, &key64);
    233 	if (n)
    234 		rb_tree_remove_node(&xa->xa_tree, n);
    235 	mutex_exit(&xa->xa_lock);
    236 
    237 	if (n) {
    238 		datum = n->n_datum;
    239 		kmem_free(n, sizeof(*n));
    240 	}
    241 	return datum;
    242 }
    243