xref: /kernel/linux/linux-5.10/lib/idr.c (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci#include <linux/bitmap.h>
38c2ecf20Sopenharmony_ci#include <linux/bug.h>
48c2ecf20Sopenharmony_ci#include <linux/export.h>
58c2ecf20Sopenharmony_ci#include <linux/idr.h>
68c2ecf20Sopenharmony_ci#include <linux/slab.h>
78c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
88c2ecf20Sopenharmony_ci#include <linux/xarray.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/**
118c2ecf20Sopenharmony_ci * idr_alloc_u32() - Allocate an ID.
128c2ecf20Sopenharmony_ci * @idr: IDR handle.
138c2ecf20Sopenharmony_ci * @ptr: Pointer to be associated with the new ID.
148c2ecf20Sopenharmony_ci * @nextid: Pointer to an ID.
158c2ecf20Sopenharmony_ci * @max: The maximum ID to allocate (inclusive).
168c2ecf20Sopenharmony_ci * @gfp: Memory allocation flags.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * Allocates an unused ID in the range specified by @nextid and @max.
198c2ecf20Sopenharmony_ci * Note that @max is inclusive whereas the @end parameter to idr_alloc()
208c2ecf20Sopenharmony_ci * is exclusive.  The new ID is assigned to @nextid before the pointer
218c2ecf20Sopenharmony_ci * is inserted into the IDR, so if @nextid points into the object pointed
228c2ecf20Sopenharmony_ci * to by @ptr, a concurrent lookup will not find an uninitialised ID.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * The caller should provide their own locking to ensure that two
258c2ecf20Sopenharmony_ci * concurrent modifications to the IDR are not possible.  Read-only
268c2ecf20Sopenharmony_ci * accesses to the IDR may be done under the RCU read lock or may
278c2ecf20Sopenharmony_ci * exclude simultaneous writers.
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed,
308c2ecf20Sopenharmony_ci * or -ENOSPC if no free IDs could be found.  If an error occurred,
318c2ecf20Sopenharmony_ci * @nextid is unchanged.
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ciint idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,
348c2ecf20Sopenharmony_ci			unsigned long max, gfp_t gfp)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	struct radix_tree_iter iter;
378c2ecf20Sopenharmony_ci	void __rcu **slot;
388c2ecf20Sopenharmony_ci	unsigned int base = idr->idr_base;
398c2ecf20Sopenharmony_ci	unsigned int id = *nextid;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR)))
428c2ecf20Sopenharmony_ci		idr->idr_rt.xa_flags |= IDR_RT_MARKER;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	id = (id < base) ? 0 : id - base;
458c2ecf20Sopenharmony_ci	radix_tree_iter_init(&iter, id);
468c2ecf20Sopenharmony_ci	slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);
478c2ecf20Sopenharmony_ci	if (IS_ERR(slot))
488c2ecf20Sopenharmony_ci		return PTR_ERR(slot);
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	*nextid = iter.index + base;
518c2ecf20Sopenharmony_ci	/* there is a memory barrier inside radix_tree_iter_replace() */
528c2ecf20Sopenharmony_ci	radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
538c2ecf20Sopenharmony_ci	radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	return 0;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(idr_alloc_u32);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci/**
608c2ecf20Sopenharmony_ci * idr_alloc() - Allocate an ID.
618c2ecf20Sopenharmony_ci * @idr: IDR handle.
628c2ecf20Sopenharmony_ci * @ptr: Pointer to be associated with the new ID.
638c2ecf20Sopenharmony_ci * @start: The minimum ID (inclusive).
648c2ecf20Sopenharmony_ci * @end: The maximum ID (exclusive).
658c2ecf20Sopenharmony_ci * @gfp: Memory allocation flags.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * Allocates an unused ID in the range specified by @start and @end.  If
688c2ecf20Sopenharmony_ci * @end is <= 0, it is treated as one larger than %INT_MAX.  This allows
698c2ecf20Sopenharmony_ci * callers to use @start + N as @end as long as N is within integer range.
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci * The caller should provide their own locking to ensure that two
728c2ecf20Sopenharmony_ci * concurrent modifications to the IDR are not possible.  Read-only
738c2ecf20Sopenharmony_ci * accesses to the IDR may be done under the RCU read lock or may
748c2ecf20Sopenharmony_ci * exclude simultaneous writers.
758c2ecf20Sopenharmony_ci *
768c2ecf20Sopenharmony_ci * Return: The newly allocated ID, -ENOMEM if memory allocation failed,
778c2ecf20Sopenharmony_ci * or -ENOSPC if no free IDs could be found.
788c2ecf20Sopenharmony_ci */
798c2ecf20Sopenharmony_ciint idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	u32 id = start;
828c2ecf20Sopenharmony_ci	int ret;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(start < 0))
858c2ecf20Sopenharmony_ci		return -EINVAL;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);
888c2ecf20Sopenharmony_ci	if (ret)
898c2ecf20Sopenharmony_ci		return ret;
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	return id;
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(idr_alloc);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci/**
968c2ecf20Sopenharmony_ci * idr_alloc_cyclic() - Allocate an ID cyclically.
978c2ecf20Sopenharmony_ci * @idr: IDR handle.
988c2ecf20Sopenharmony_ci * @ptr: Pointer to be associated with the new ID.
998c2ecf20Sopenharmony_ci * @start: The minimum ID (inclusive).
1008c2ecf20Sopenharmony_ci * @end: The maximum ID (exclusive).
1018c2ecf20Sopenharmony_ci * @gfp: Memory allocation flags.
1028c2ecf20Sopenharmony_ci *
1038c2ecf20Sopenharmony_ci * Allocates an unused ID in the range specified by @start and @end.  If
1048c2ecf20Sopenharmony_ci * @end is <= 0, it is treated as one larger than %INT_MAX.  This allows
1058c2ecf20Sopenharmony_ci * callers to use @start + N as @end as long as N is within integer range.
1068c2ecf20Sopenharmony_ci * The search for an unused ID will start at the last ID allocated and will
1078c2ecf20Sopenharmony_ci * wrap around to @start if no free IDs are found before reaching @end.
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * The caller should provide their own locking to ensure that two
1108c2ecf20Sopenharmony_ci * concurrent modifications to the IDR are not possible.  Read-only
1118c2ecf20Sopenharmony_ci * accesses to the IDR may be done under the RCU read lock or may
1128c2ecf20Sopenharmony_ci * exclude simultaneous writers.
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci * Return: The newly allocated ID, -ENOMEM if memory allocation failed,
1158c2ecf20Sopenharmony_ci * or -ENOSPC if no free IDs could be found.
1168c2ecf20Sopenharmony_ci */
1178c2ecf20Sopenharmony_ciint idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	u32 id = idr->idr_next;
1208c2ecf20Sopenharmony_ci	int err, max = end > 0 ? end - 1 : INT_MAX;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if ((int)id < start)
1238c2ecf20Sopenharmony_ci		id = start;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	err = idr_alloc_u32(idr, ptr, &id, max, gfp);
1268c2ecf20Sopenharmony_ci	if ((err == -ENOSPC) && (id > start)) {
1278c2ecf20Sopenharmony_ci		id = start;
1288c2ecf20Sopenharmony_ci		err = idr_alloc_u32(idr, ptr, &id, max, gfp);
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci	if (err)
1318c2ecf20Sopenharmony_ci		return err;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	idr->idr_next = id + 1;
1348c2ecf20Sopenharmony_ci	return id;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(idr_alloc_cyclic);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci/**
1398c2ecf20Sopenharmony_ci * idr_remove() - Remove an ID from the IDR.
1408c2ecf20Sopenharmony_ci * @idr: IDR handle.
1418c2ecf20Sopenharmony_ci * @id: Pointer ID.
1428c2ecf20Sopenharmony_ci *
1438c2ecf20Sopenharmony_ci * Removes this ID from the IDR.  If the ID was not previously in the IDR,
1448c2ecf20Sopenharmony_ci * this function returns %NULL.
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * Since this function modifies the IDR, the caller should provide their
1478c2ecf20Sopenharmony_ci * own locking to ensure that concurrent modification of the same IDR is
1488c2ecf20Sopenharmony_ci * not possible.
1498c2ecf20Sopenharmony_ci *
1508c2ecf20Sopenharmony_ci * Return: The pointer formerly associated with this ID.
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_civoid *idr_remove(struct idr *idr, unsigned long id)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	return radix_tree_delete_item(&idr->idr_rt, id - idr->idr_base, NULL);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(idr_remove);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci/**
1598c2ecf20Sopenharmony_ci * idr_find() - Return pointer for given ID.
1608c2ecf20Sopenharmony_ci * @idr: IDR handle.
1618c2ecf20Sopenharmony_ci * @id: Pointer ID.
1628c2ecf20Sopenharmony_ci *
1638c2ecf20Sopenharmony_ci * Looks up the pointer associated with this ID.  A %NULL pointer may
1648c2ecf20Sopenharmony_ci * indicate that @id is not allocated or that the %NULL pointer was
1658c2ecf20Sopenharmony_ci * associated with this ID.
1668c2ecf20Sopenharmony_ci *
1678c2ecf20Sopenharmony_ci * This function can be called under rcu_read_lock(), given that the leaf
1688c2ecf20Sopenharmony_ci * pointers lifetimes are correctly managed.
1698c2ecf20Sopenharmony_ci *
1708c2ecf20Sopenharmony_ci * Return: The pointer associated with this ID.
1718c2ecf20Sopenharmony_ci */
1728c2ecf20Sopenharmony_civoid *idr_find(const struct idr *idr, unsigned long id)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	return radix_tree_lookup(&idr->idr_rt, id - idr->idr_base);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(idr_find);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci/**
1798c2ecf20Sopenharmony_ci * idr_for_each() - Iterate through all stored pointers.
1808c2ecf20Sopenharmony_ci * @idr: IDR handle.
1818c2ecf20Sopenharmony_ci * @fn: Function to be called for each pointer.
1828c2ecf20Sopenharmony_ci * @data: Data passed to callback function.
1838c2ecf20Sopenharmony_ci *
1848c2ecf20Sopenharmony_ci * The callback function will be called for each entry in @idr, passing
1858c2ecf20Sopenharmony_ci * the ID, the entry and @data.
1868c2ecf20Sopenharmony_ci *
1878c2ecf20Sopenharmony_ci * If @fn returns anything other than %0, the iteration stops and that
1888c2ecf20Sopenharmony_ci * value is returned from this function.
1898c2ecf20Sopenharmony_ci *
1908c2ecf20Sopenharmony_ci * idr_for_each() can be called concurrently with idr_alloc() and
1918c2ecf20Sopenharmony_ci * idr_remove() if protected by RCU.  Newly added entries may not be
1928c2ecf20Sopenharmony_ci * seen and deleted entries may be seen, but adding and removing entries
1938c2ecf20Sopenharmony_ci * will not cause other entries to be skipped, nor spurious ones to be seen.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_ciint idr_for_each(const struct idr *idr,
1968c2ecf20Sopenharmony_ci		int (*fn)(int id, void *p, void *data), void *data)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	struct radix_tree_iter iter;
1998c2ecf20Sopenharmony_ci	void __rcu **slot;
2008c2ecf20Sopenharmony_ci	int base = idr->idr_base;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
2038c2ecf20Sopenharmony_ci		int ret;
2048c2ecf20Sopenharmony_ci		unsigned long id = iter.index + base;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(id > INT_MAX))
2078c2ecf20Sopenharmony_ci			break;
2088c2ecf20Sopenharmony_ci		ret = fn(id, rcu_dereference_raw(*slot), data);
2098c2ecf20Sopenharmony_ci		if (ret)
2108c2ecf20Sopenharmony_ci			return ret;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	return 0;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(idr_for_each);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci/**
2188c2ecf20Sopenharmony_ci * idr_get_next_ul() - Find next populated entry.
2198c2ecf20Sopenharmony_ci * @idr: IDR handle.
2208c2ecf20Sopenharmony_ci * @nextid: Pointer to an ID.
2218c2ecf20Sopenharmony_ci *
2228c2ecf20Sopenharmony_ci * Returns the next populated entry in the tree with an ID greater than
2238c2ecf20Sopenharmony_ci * or equal to the value pointed to by @nextid.  On exit, @nextid is updated
2248c2ecf20Sopenharmony_ci * to the ID of the found value.  To use in a loop, the value pointed to by
2258c2ecf20Sopenharmony_ci * nextid must be incremented by the user.
2268c2ecf20Sopenharmony_ci */
2278c2ecf20Sopenharmony_civoid *idr_get_next_ul(struct idr *idr, unsigned long *nextid)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	struct radix_tree_iter iter;
2308c2ecf20Sopenharmony_ci	void __rcu **slot;
2318c2ecf20Sopenharmony_ci	void *entry = NULL;
2328c2ecf20Sopenharmony_ci	unsigned long base = idr->idr_base;
2338c2ecf20Sopenharmony_ci	unsigned long id = *nextid;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	id = (id < base) ? 0 : id - base;
2368c2ecf20Sopenharmony_ci	radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, id) {
2378c2ecf20Sopenharmony_ci		entry = rcu_dereference_raw(*slot);
2388c2ecf20Sopenharmony_ci		if (!entry)
2398c2ecf20Sopenharmony_ci			continue;
2408c2ecf20Sopenharmony_ci		if (!xa_is_internal(entry))
2418c2ecf20Sopenharmony_ci			break;
2428c2ecf20Sopenharmony_ci		if (slot != &idr->idr_rt.xa_head && !xa_is_retry(entry))
2438c2ecf20Sopenharmony_ci			break;
2448c2ecf20Sopenharmony_ci		slot = radix_tree_iter_retry(&iter);
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci	if (!slot)
2478c2ecf20Sopenharmony_ci		return NULL;
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	*nextid = iter.index + base;
2508c2ecf20Sopenharmony_ci	return entry;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ciEXPORT_SYMBOL(idr_get_next_ul);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/**
2558c2ecf20Sopenharmony_ci * idr_get_next() - Find next populated entry.
2568c2ecf20Sopenharmony_ci * @idr: IDR handle.
2578c2ecf20Sopenharmony_ci * @nextid: Pointer to an ID.
2588c2ecf20Sopenharmony_ci *
2598c2ecf20Sopenharmony_ci * Returns the next populated entry in the tree with an ID greater than
2608c2ecf20Sopenharmony_ci * or equal to the value pointed to by @nextid.  On exit, @nextid is updated
2618c2ecf20Sopenharmony_ci * to the ID of the found value.  To use in a loop, the value pointed to by
2628c2ecf20Sopenharmony_ci * nextid must be incremented by the user.
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_civoid *idr_get_next(struct idr *idr, int *nextid)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	unsigned long id = *nextid;
2678c2ecf20Sopenharmony_ci	void *entry = idr_get_next_ul(idr, &id);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(id > INT_MAX))
2708c2ecf20Sopenharmony_ci		return NULL;
2718c2ecf20Sopenharmony_ci	*nextid = id;
2728c2ecf20Sopenharmony_ci	return entry;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(idr_get_next);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci/**
2778c2ecf20Sopenharmony_ci * idr_replace() - replace pointer for given ID.
2788c2ecf20Sopenharmony_ci * @idr: IDR handle.
2798c2ecf20Sopenharmony_ci * @ptr: New pointer to associate with the ID.
2808c2ecf20Sopenharmony_ci * @id: ID to change.
2818c2ecf20Sopenharmony_ci *
2828c2ecf20Sopenharmony_ci * Replace the pointer registered with an ID and return the old value.
2838c2ecf20Sopenharmony_ci * This function can be called under the RCU read lock concurrently with
2848c2ecf20Sopenharmony_ci * idr_alloc() and idr_remove() (as long as the ID being removed is not
2858c2ecf20Sopenharmony_ci * the one being replaced!).
2868c2ecf20Sopenharmony_ci *
2878c2ecf20Sopenharmony_ci * Returns: the old value on success.  %-ENOENT indicates that @id was not
2888c2ecf20Sopenharmony_ci * found.  %-EINVAL indicates that @ptr was not valid.
2898c2ecf20Sopenharmony_ci */
2908c2ecf20Sopenharmony_civoid *idr_replace(struct idr *idr, void *ptr, unsigned long id)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct radix_tree_node *node;
2938c2ecf20Sopenharmony_ci	void __rcu **slot = NULL;
2948c2ecf20Sopenharmony_ci	void *entry;
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	id -= idr->idr_base;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci	entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
2998c2ecf20Sopenharmony_ci	if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
3008c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOENT);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	__radix_tree_replace(&idr->idr_rt, node, slot, ptr);
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci	return entry;
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(idr_replace);
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/**
3098c2ecf20Sopenharmony_ci * DOC: IDA description
3108c2ecf20Sopenharmony_ci *
3118c2ecf20Sopenharmony_ci * The IDA is an ID allocator which does not provide the ability to
3128c2ecf20Sopenharmony_ci * associate an ID with a pointer.  As such, it only needs to store one
3138c2ecf20Sopenharmony_ci * bit per ID, and so is more space efficient than an IDR.  To use an IDA,
3148c2ecf20Sopenharmony_ci * define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
3158c2ecf20Sopenharmony_ci * then initialise it using ida_init()).  To allocate a new ID, call
3168c2ecf20Sopenharmony_ci * ida_alloc(), ida_alloc_min(), ida_alloc_max() or ida_alloc_range().
3178c2ecf20Sopenharmony_ci * To free an ID, call ida_free().
3188c2ecf20Sopenharmony_ci *
3198c2ecf20Sopenharmony_ci * ida_destroy() can be used to dispose of an IDA without needing to
3208c2ecf20Sopenharmony_ci * free the individual IDs in it.  You can use ida_is_empty() to find
3218c2ecf20Sopenharmony_ci * out whether the IDA has any IDs currently allocated.
3228c2ecf20Sopenharmony_ci *
3238c2ecf20Sopenharmony_ci * The IDA handles its own locking.  It is safe to call any of the IDA
3248c2ecf20Sopenharmony_ci * functions without synchronisation in your code.
3258c2ecf20Sopenharmony_ci *
3268c2ecf20Sopenharmony_ci * IDs are currently limited to the range [0-INT_MAX].  If this is an awkward
3278c2ecf20Sopenharmony_ci * limitation, it should be quite straightforward to raise the maximum.
3288c2ecf20Sopenharmony_ci */
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci/*
3318c2ecf20Sopenharmony_ci * Developer's notes:
3328c2ecf20Sopenharmony_ci *
3338c2ecf20Sopenharmony_ci * The IDA uses the functionality provided by the XArray to store bitmaps in
3348c2ecf20Sopenharmony_ci * each entry.  The XA_FREE_MARK is only cleared when all bits in the bitmap
3358c2ecf20Sopenharmony_ci * have been set.
3368c2ecf20Sopenharmony_ci *
3378c2ecf20Sopenharmony_ci * I considered telling the XArray that each slot is an order-10 node
3388c2ecf20Sopenharmony_ci * and indexing by bit number, but the XArray can't allow a single multi-index
3398c2ecf20Sopenharmony_ci * entry in the head, which would significantly increase memory consumption
3408c2ecf20Sopenharmony_ci * for the IDA.  So instead we divide the index by the number of bits in the
3418c2ecf20Sopenharmony_ci * leaf bitmap before doing a radix tree lookup.
3428c2ecf20Sopenharmony_ci *
3438c2ecf20Sopenharmony_ci * As an optimisation, if there are only a few low bits set in any given
3448c2ecf20Sopenharmony_ci * leaf, instead of allocating a 128-byte bitmap, we store the bits
3458c2ecf20Sopenharmony_ci * as a value entry.  Value entries never have the XA_FREE_MARK cleared
3468c2ecf20Sopenharmony_ci * because we can always convert them into a bitmap entry.
3478c2ecf20Sopenharmony_ci *
3488c2ecf20Sopenharmony_ci * It would be possible to optimise further; once we've run out of a
3498c2ecf20Sopenharmony_ci * single 128-byte bitmap, we currently switch to a 576-byte node, put
3508c2ecf20Sopenharmony_ci * the 128-byte bitmap in the first entry and then start allocating extra
3518c2ecf20Sopenharmony_ci * 128-byte entries.  We could instead use the 512 bytes of the node's
3528c2ecf20Sopenharmony_ci * data as a bitmap before moving to that scheme.  I do not believe this
3538c2ecf20Sopenharmony_ci * is a worthwhile optimisation; Rasmus Villemoes surveyed the current
3548c2ecf20Sopenharmony_ci * users of the IDA and almost none of them use more than 1024 entries.
3558c2ecf20Sopenharmony_ci * Those that do use more than the 8192 IDs that the 512 bytes would
3568c2ecf20Sopenharmony_ci * provide.
3578c2ecf20Sopenharmony_ci *
3588c2ecf20Sopenharmony_ci * The IDA always uses a lock to alloc/free.  If we add a 'test_bit'
3598c2ecf20Sopenharmony_ci * equivalent, it will still need locking.  Going to RCU lookup would require
3608c2ecf20Sopenharmony_ci * using RCU to free bitmaps, and that's not trivial without embedding an
3618c2ecf20Sopenharmony_ci * RCU head in the bitmap, which adds a 2-pointer overhead to each 128-byte
3628c2ecf20Sopenharmony_ci * bitmap, which is excessive.
3638c2ecf20Sopenharmony_ci */
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci/**
3668c2ecf20Sopenharmony_ci * ida_alloc_range() - Allocate an unused ID.
3678c2ecf20Sopenharmony_ci * @ida: IDA handle.
3688c2ecf20Sopenharmony_ci * @min: Lowest ID to allocate.
3698c2ecf20Sopenharmony_ci * @max: Highest ID to allocate.
3708c2ecf20Sopenharmony_ci * @gfp: Memory allocation flags.
3718c2ecf20Sopenharmony_ci *
3728c2ecf20Sopenharmony_ci * Allocate an ID between @min and @max, inclusive.  The allocated ID will
3738c2ecf20Sopenharmony_ci * not exceed %INT_MAX, even if @max is larger.
3748c2ecf20Sopenharmony_ci *
3758c2ecf20Sopenharmony_ci * Context: Any context. It is safe to call this function without
3768c2ecf20Sopenharmony_ci * locking in your code.
3778c2ecf20Sopenharmony_ci * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
3788c2ecf20Sopenharmony_ci * or %-ENOSPC if there are no free IDs.
3798c2ecf20Sopenharmony_ci */
3808c2ecf20Sopenharmony_ciint ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max,
3818c2ecf20Sopenharmony_ci			gfp_t gfp)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	XA_STATE(xas, &ida->xa, min / IDA_BITMAP_BITS);
3848c2ecf20Sopenharmony_ci	unsigned bit = min % IDA_BITMAP_BITS;
3858c2ecf20Sopenharmony_ci	unsigned long flags;
3868c2ecf20Sopenharmony_ci	struct ida_bitmap *bitmap, *alloc = NULL;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	if ((int)min < 0)
3898c2ecf20Sopenharmony_ci		return -ENOSPC;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if ((int)max < 0)
3928c2ecf20Sopenharmony_ci		max = INT_MAX;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ciretry:
3958c2ecf20Sopenharmony_ci	xas_lock_irqsave(&xas, flags);
3968c2ecf20Sopenharmony_cinext:
3978c2ecf20Sopenharmony_ci	bitmap = xas_find_marked(&xas, max / IDA_BITMAP_BITS, XA_FREE_MARK);
3988c2ecf20Sopenharmony_ci	if (xas.xa_index > min / IDA_BITMAP_BITS)
3998c2ecf20Sopenharmony_ci		bit = 0;
4008c2ecf20Sopenharmony_ci	if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
4018c2ecf20Sopenharmony_ci		goto nospc;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	if (xa_is_value(bitmap)) {
4048c2ecf20Sopenharmony_ci		unsigned long tmp = xa_to_value(bitmap);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci		if (bit < BITS_PER_XA_VALUE) {
4078c2ecf20Sopenharmony_ci			bit = find_next_zero_bit(&tmp, BITS_PER_XA_VALUE, bit);
4088c2ecf20Sopenharmony_ci			if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
4098c2ecf20Sopenharmony_ci				goto nospc;
4108c2ecf20Sopenharmony_ci			if (bit < BITS_PER_XA_VALUE) {
4118c2ecf20Sopenharmony_ci				tmp |= 1UL << bit;
4128c2ecf20Sopenharmony_ci				xas_store(&xas, xa_mk_value(tmp));
4138c2ecf20Sopenharmony_ci				goto out;
4148c2ecf20Sopenharmony_ci			}
4158c2ecf20Sopenharmony_ci		}
4168c2ecf20Sopenharmony_ci		bitmap = alloc;
4178c2ecf20Sopenharmony_ci		if (!bitmap)
4188c2ecf20Sopenharmony_ci			bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT);
4198c2ecf20Sopenharmony_ci		if (!bitmap)
4208c2ecf20Sopenharmony_ci			goto alloc;
4218c2ecf20Sopenharmony_ci		bitmap->bitmap[0] = tmp;
4228c2ecf20Sopenharmony_ci		xas_store(&xas, bitmap);
4238c2ecf20Sopenharmony_ci		if (xas_error(&xas)) {
4248c2ecf20Sopenharmony_ci			bitmap->bitmap[0] = 0;
4258c2ecf20Sopenharmony_ci			goto out;
4268c2ecf20Sopenharmony_ci		}
4278c2ecf20Sopenharmony_ci	}
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	if (bitmap) {
4308c2ecf20Sopenharmony_ci		bit = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, bit);
4318c2ecf20Sopenharmony_ci		if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
4328c2ecf20Sopenharmony_ci			goto nospc;
4338c2ecf20Sopenharmony_ci		if (bit == IDA_BITMAP_BITS)
4348c2ecf20Sopenharmony_ci			goto next;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		__set_bit(bit, bitmap->bitmap);
4378c2ecf20Sopenharmony_ci		if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
4388c2ecf20Sopenharmony_ci			xas_clear_mark(&xas, XA_FREE_MARK);
4398c2ecf20Sopenharmony_ci	} else {
4408c2ecf20Sopenharmony_ci		if (bit < BITS_PER_XA_VALUE) {
4418c2ecf20Sopenharmony_ci			bitmap = xa_mk_value(1UL << bit);
4428c2ecf20Sopenharmony_ci		} else {
4438c2ecf20Sopenharmony_ci			bitmap = alloc;
4448c2ecf20Sopenharmony_ci			if (!bitmap)
4458c2ecf20Sopenharmony_ci				bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT);
4468c2ecf20Sopenharmony_ci			if (!bitmap)
4478c2ecf20Sopenharmony_ci				goto alloc;
4488c2ecf20Sopenharmony_ci			__set_bit(bit, bitmap->bitmap);
4498c2ecf20Sopenharmony_ci		}
4508c2ecf20Sopenharmony_ci		xas_store(&xas, bitmap);
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ciout:
4538c2ecf20Sopenharmony_ci	xas_unlock_irqrestore(&xas, flags);
4548c2ecf20Sopenharmony_ci	if (xas_nomem(&xas, gfp)) {
4558c2ecf20Sopenharmony_ci		xas.xa_index = min / IDA_BITMAP_BITS;
4568c2ecf20Sopenharmony_ci		bit = min % IDA_BITMAP_BITS;
4578c2ecf20Sopenharmony_ci		goto retry;
4588c2ecf20Sopenharmony_ci	}
4598c2ecf20Sopenharmony_ci	if (bitmap != alloc)
4608c2ecf20Sopenharmony_ci		kfree(alloc);
4618c2ecf20Sopenharmony_ci	if (xas_error(&xas))
4628c2ecf20Sopenharmony_ci		return xas_error(&xas);
4638c2ecf20Sopenharmony_ci	return xas.xa_index * IDA_BITMAP_BITS + bit;
4648c2ecf20Sopenharmony_cialloc:
4658c2ecf20Sopenharmony_ci	xas_unlock_irqrestore(&xas, flags);
4668c2ecf20Sopenharmony_ci	alloc = kzalloc(sizeof(*bitmap), gfp);
4678c2ecf20Sopenharmony_ci	if (!alloc)
4688c2ecf20Sopenharmony_ci		return -ENOMEM;
4698c2ecf20Sopenharmony_ci	xas_set(&xas, min / IDA_BITMAP_BITS);
4708c2ecf20Sopenharmony_ci	bit = min % IDA_BITMAP_BITS;
4718c2ecf20Sopenharmony_ci	goto retry;
4728c2ecf20Sopenharmony_cinospc:
4738c2ecf20Sopenharmony_ci	xas_unlock_irqrestore(&xas, flags);
4748c2ecf20Sopenharmony_ci	kfree(alloc);
4758c2ecf20Sopenharmony_ci	return -ENOSPC;
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ida_alloc_range);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci/**
4808c2ecf20Sopenharmony_ci * ida_free() - Release an allocated ID.
4818c2ecf20Sopenharmony_ci * @ida: IDA handle.
4828c2ecf20Sopenharmony_ci * @id: Previously allocated ID.
4838c2ecf20Sopenharmony_ci *
4848c2ecf20Sopenharmony_ci * Context: Any context. It is safe to call this function without
4858c2ecf20Sopenharmony_ci * locking in your code.
4868c2ecf20Sopenharmony_ci */
4878c2ecf20Sopenharmony_civoid ida_free(struct ida *ida, unsigned int id)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	XA_STATE(xas, &ida->xa, id / IDA_BITMAP_BITS);
4908c2ecf20Sopenharmony_ci	unsigned bit = id % IDA_BITMAP_BITS;
4918c2ecf20Sopenharmony_ci	struct ida_bitmap *bitmap;
4928c2ecf20Sopenharmony_ci	unsigned long flags;
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci	if ((int)id < 0)
4958c2ecf20Sopenharmony_ci		return;
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	xas_lock_irqsave(&xas, flags);
4988c2ecf20Sopenharmony_ci	bitmap = xas_load(&xas);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	if (xa_is_value(bitmap)) {
5018c2ecf20Sopenharmony_ci		unsigned long v = xa_to_value(bitmap);
5028c2ecf20Sopenharmony_ci		if (bit >= BITS_PER_XA_VALUE)
5038c2ecf20Sopenharmony_ci			goto err;
5048c2ecf20Sopenharmony_ci		if (!(v & (1UL << bit)))
5058c2ecf20Sopenharmony_ci			goto err;
5068c2ecf20Sopenharmony_ci		v &= ~(1UL << bit);
5078c2ecf20Sopenharmony_ci		if (!v)
5088c2ecf20Sopenharmony_ci			goto delete;
5098c2ecf20Sopenharmony_ci		xas_store(&xas, xa_mk_value(v));
5108c2ecf20Sopenharmony_ci	} else {
5118c2ecf20Sopenharmony_ci		if (!bitmap || !test_bit(bit, bitmap->bitmap))
5128c2ecf20Sopenharmony_ci			goto err;
5138c2ecf20Sopenharmony_ci		__clear_bit(bit, bitmap->bitmap);
5148c2ecf20Sopenharmony_ci		xas_set_mark(&xas, XA_FREE_MARK);
5158c2ecf20Sopenharmony_ci		if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) {
5168c2ecf20Sopenharmony_ci			kfree(bitmap);
5178c2ecf20Sopenharmony_cidelete:
5188c2ecf20Sopenharmony_ci			xas_store(&xas, NULL);
5198c2ecf20Sopenharmony_ci		}
5208c2ecf20Sopenharmony_ci	}
5218c2ecf20Sopenharmony_ci	xas_unlock_irqrestore(&xas, flags);
5228c2ecf20Sopenharmony_ci	return;
5238c2ecf20Sopenharmony_ci err:
5248c2ecf20Sopenharmony_ci	xas_unlock_irqrestore(&xas, flags);
5258c2ecf20Sopenharmony_ci	WARN(1, "ida_free called for id=%d which is not allocated.\n", id);
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ida_free);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci/**
5308c2ecf20Sopenharmony_ci * ida_destroy() - Free all IDs.
5318c2ecf20Sopenharmony_ci * @ida: IDA handle.
5328c2ecf20Sopenharmony_ci *
5338c2ecf20Sopenharmony_ci * Calling this function frees all IDs and releases all resources used
5348c2ecf20Sopenharmony_ci * by an IDA.  When this call returns, the IDA is empty and can be reused
5358c2ecf20Sopenharmony_ci * or freed.  If the IDA is already empty, there is no need to call this
5368c2ecf20Sopenharmony_ci * function.
5378c2ecf20Sopenharmony_ci *
5388c2ecf20Sopenharmony_ci * Context: Any context. It is safe to call this function without
5398c2ecf20Sopenharmony_ci * locking in your code.
5408c2ecf20Sopenharmony_ci */
5418c2ecf20Sopenharmony_civoid ida_destroy(struct ida *ida)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	XA_STATE(xas, &ida->xa, 0);
5448c2ecf20Sopenharmony_ci	struct ida_bitmap *bitmap;
5458c2ecf20Sopenharmony_ci	unsigned long flags;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	xas_lock_irqsave(&xas, flags);
5488c2ecf20Sopenharmony_ci	xas_for_each(&xas, bitmap, ULONG_MAX) {
5498c2ecf20Sopenharmony_ci		if (!xa_is_value(bitmap))
5508c2ecf20Sopenharmony_ci			kfree(bitmap);
5518c2ecf20Sopenharmony_ci		xas_store(&xas, NULL);
5528c2ecf20Sopenharmony_ci	}
5538c2ecf20Sopenharmony_ci	xas_unlock_irqrestore(&xas, flags);
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ciEXPORT_SYMBOL(ida_destroy);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci#ifndef __KERNEL__
5588c2ecf20Sopenharmony_ciextern void xa_dump_index(unsigned long index, unsigned int shift);
5598c2ecf20Sopenharmony_ci#define IDA_CHUNK_SHIFT		ilog2(IDA_BITMAP_BITS)
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic void ida_dump_entry(void *entry, unsigned long index)
5628c2ecf20Sopenharmony_ci{
5638c2ecf20Sopenharmony_ci	unsigned long i;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	if (!entry)
5668c2ecf20Sopenharmony_ci		return;
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	if (xa_is_node(entry)) {
5698c2ecf20Sopenharmony_ci		struct xa_node *node = xa_to_node(entry);
5708c2ecf20Sopenharmony_ci		unsigned int shift = node->shift + IDA_CHUNK_SHIFT +
5718c2ecf20Sopenharmony_ci			XA_CHUNK_SHIFT;
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_ci		xa_dump_index(index * IDA_BITMAP_BITS, shift);
5748c2ecf20Sopenharmony_ci		xa_dump_node(node);
5758c2ecf20Sopenharmony_ci		for (i = 0; i < XA_CHUNK_SIZE; i++)
5768c2ecf20Sopenharmony_ci			ida_dump_entry(node->slots[i],
5778c2ecf20Sopenharmony_ci					index | (i << node->shift));
5788c2ecf20Sopenharmony_ci	} else if (xa_is_value(entry)) {
5798c2ecf20Sopenharmony_ci		xa_dump_index(index * IDA_BITMAP_BITS, ilog2(BITS_PER_LONG));
5808c2ecf20Sopenharmony_ci		pr_cont("value: data %lx [%px]\n", xa_to_value(entry), entry);
5818c2ecf20Sopenharmony_ci	} else {
5828c2ecf20Sopenharmony_ci		struct ida_bitmap *bitmap = entry;
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_ci		xa_dump_index(index * IDA_BITMAP_BITS, IDA_CHUNK_SHIFT);
5858c2ecf20Sopenharmony_ci		pr_cont("bitmap: %p data", bitmap);
5868c2ecf20Sopenharmony_ci		for (i = 0; i < IDA_BITMAP_LONGS; i++)
5878c2ecf20Sopenharmony_ci			pr_cont(" %lx", bitmap->bitmap[i]);
5888c2ecf20Sopenharmony_ci		pr_cont("\n");
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic void ida_dump(struct ida *ida)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	struct xarray *xa = &ida->xa;
5958c2ecf20Sopenharmony_ci	pr_debug("ida: %p node %p free %d\n", ida, xa->xa_head,
5968c2ecf20Sopenharmony_ci				xa->xa_flags >> ROOT_TAG_SHIFT);
5978c2ecf20Sopenharmony_ci	ida_dump_entry(xa->xa_head, 0);
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci#endif
600