1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 20a835c4fSMatthew Wilcox #include <linux/bitmap.h> 3460488c5SMatthew Wilcox #include <linux/bug.h> 48bc3bcc9SPaul Gortmaker #include <linux/export.h> 51da177e4SLinus Torvalds #include <linux/idr.h> 60a835c4fSMatthew Wilcox #include <linux/slab.h> 788eca020SRusty Russell #include <linux/spinlock.h> 8b94078e6SMatthew Wilcox #include <linux/xarray.h> 91da177e4SLinus Torvalds 10e096f6a7SMatthew Wilcox /** 11e096f6a7SMatthew Wilcox * idr_alloc_u32() - Allocate an ID. 12e096f6a7SMatthew Wilcox * @idr: IDR handle. 13e096f6a7SMatthew Wilcox * @ptr: Pointer to be associated with the new ID. 14e096f6a7SMatthew Wilcox * @nextid: Pointer to an ID. 15e096f6a7SMatthew Wilcox * @max: The maximum ID to allocate (inclusive). 16e096f6a7SMatthew Wilcox * @gfp: Memory allocation flags. 17e096f6a7SMatthew Wilcox * 18e096f6a7SMatthew Wilcox * Allocates an unused ID in the range specified by @nextid and @max. 19e096f6a7SMatthew Wilcox * Note that @max is inclusive whereas the @end parameter to idr_alloc() 20460488c5SMatthew Wilcox * is exclusive. The new ID is assigned to @nextid before the pointer 21460488c5SMatthew Wilcox * is inserted into the IDR, so if @nextid points into the object pointed 22460488c5SMatthew Wilcox * to by @ptr, a concurrent lookup will not find an uninitialised ID. 23e096f6a7SMatthew Wilcox * 24e096f6a7SMatthew Wilcox * The caller should provide their own locking to ensure that two 25e096f6a7SMatthew Wilcox * concurrent modifications to the IDR are not possible. Read-only 26e096f6a7SMatthew Wilcox * accesses to the IDR may be done under the RCU read lock or may 27e096f6a7SMatthew Wilcox * exclude simultaneous writers. 28e096f6a7SMatthew Wilcox * 29e096f6a7SMatthew Wilcox * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed, 30e096f6a7SMatthew Wilcox * or -ENOSPC if no free IDs could be found. If an error occurred, 31e096f6a7SMatthew Wilcox * @nextid is unchanged. 32e096f6a7SMatthew Wilcox */ 33e096f6a7SMatthew Wilcox int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid, 34e096f6a7SMatthew Wilcox unsigned long max, gfp_t gfp) 35e096f6a7SMatthew Wilcox { 360a835c4fSMatthew Wilcox struct radix_tree_iter iter; 37388f79fdSChris Mi void __rcu **slot; 384b0ad076SMatthew Wilcox unsigned int base = idr->idr_base; 394b0ad076SMatthew Wilcox unsigned int id = *nextid; 40d5c7409fSTejun Heo 41f8d5d0ccSMatthew Wilcox if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR))) 42f8d5d0ccSMatthew Wilcox idr->idr_rt.xa_flags |= IDR_RT_MARKER; 43d5c7409fSTejun Heo 446ce711f2SMatthew Wilcox id = (id < base) ? 0 : id - base; 456ce711f2SMatthew Wilcox radix_tree_iter_init(&iter, id); 466ce711f2SMatthew Wilcox slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base); 470a835c4fSMatthew Wilcox if (IS_ERR(slot)) 480a835c4fSMatthew Wilcox return PTR_ERR(slot); 49d5c7409fSTejun Heo 506ce711f2SMatthew Wilcox *nextid = iter.index + base; 51460488c5SMatthew Wilcox /* there is a memory barrier inside radix_tree_iter_replace() */ 520a835c4fSMatthew Wilcox radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr); 530a835c4fSMatthew Wilcox radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE); 54388f79fdSChris Mi 55388f79fdSChris Mi return 0; 56d5c7409fSTejun Heo } 57460488c5SMatthew Wilcox EXPORT_SYMBOL_GPL(idr_alloc_u32); 58d5c7409fSTejun Heo 593e6628c4SJeff Layton /** 60460488c5SMatthew Wilcox * idr_alloc() - Allocate an ID. 61460488c5SMatthew Wilcox * @idr: IDR handle. 62460488c5SMatthew Wilcox * @ptr: Pointer to be associated with the new ID. 63460488c5SMatthew Wilcox * @start: The minimum ID (inclusive). 64460488c5SMatthew Wilcox * @end: The maximum ID (exclusive). 65460488c5SMatthew Wilcox * @gfp: Memory allocation flags. 663e6628c4SJeff Layton * 67460488c5SMatthew Wilcox * Allocates an unused ID in the range specified by @start and @end. If 68460488c5SMatthew Wilcox * @end is <= 0, it is treated as one larger than %INT_MAX. This allows 69460488c5SMatthew Wilcox * callers to use @start + N as @end as long as N is within integer range. 70460488c5SMatthew Wilcox * 71460488c5SMatthew Wilcox * The caller should provide their own locking to ensure that two 72460488c5SMatthew Wilcox * concurrent modifications to the IDR are not possible. Read-only 73460488c5SMatthew Wilcox * accesses to the IDR may be done under the RCU read lock or may 74460488c5SMatthew Wilcox * exclude simultaneous writers. 75460488c5SMatthew Wilcox * 76460488c5SMatthew Wilcox * Return: The newly allocated ID, -ENOMEM if memory allocation failed, 77460488c5SMatthew Wilcox * or -ENOSPC if no free IDs could be found. 78460488c5SMatthew Wilcox */ 79460488c5SMatthew Wilcox int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) 80460488c5SMatthew Wilcox { 81460488c5SMatthew Wilcox u32 id = start; 82460488c5SMatthew Wilcox int ret; 83460488c5SMatthew Wilcox 84460488c5SMatthew Wilcox if (WARN_ON_ONCE(start < 0)) 85460488c5SMatthew Wilcox return -EINVAL; 86460488c5SMatthew Wilcox 87460488c5SMatthew Wilcox ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp); 88460488c5SMatthew Wilcox if (ret) 89460488c5SMatthew Wilcox return ret; 90460488c5SMatthew Wilcox 91460488c5SMatthew Wilcox return id; 92460488c5SMatthew Wilcox } 93460488c5SMatthew Wilcox EXPORT_SYMBOL_GPL(idr_alloc); 94460488c5SMatthew Wilcox 95460488c5SMatthew Wilcox /** 96460488c5SMatthew Wilcox * idr_alloc_cyclic() - Allocate an ID cyclically. 97460488c5SMatthew Wilcox * @idr: IDR handle. 98460488c5SMatthew Wilcox * @ptr: Pointer to be associated with the new ID. 99460488c5SMatthew Wilcox * @start: The minimum ID (inclusive). 100460488c5SMatthew Wilcox * @end: The maximum ID (exclusive). 101460488c5SMatthew Wilcox * @gfp: Memory allocation flags. 102460488c5SMatthew Wilcox * 103460488c5SMatthew Wilcox * Allocates an unused ID in the range specified by @nextid and @end. If 104460488c5SMatthew Wilcox * @end is <= 0, it is treated as one larger than %INT_MAX. This allows 105460488c5SMatthew Wilcox * callers to use @start + N as @end as long as N is within integer range. 106460488c5SMatthew Wilcox * The search for an unused ID will start at the last ID allocated and will 107460488c5SMatthew Wilcox * wrap around to @start if no free IDs are found before reaching @end. 108460488c5SMatthew Wilcox * 109460488c5SMatthew Wilcox * The caller should provide their own locking to ensure that two 110460488c5SMatthew Wilcox * concurrent modifications to the IDR are not possible. Read-only 111460488c5SMatthew Wilcox * accesses to the IDR may be done under the RCU read lock or may 112460488c5SMatthew Wilcox * exclude simultaneous writers. 113460488c5SMatthew Wilcox * 114460488c5SMatthew Wilcox * Return: The newly allocated ID, -ENOMEM if memory allocation failed, 115460488c5SMatthew Wilcox * or -ENOSPC if no free IDs could be found. 1163e6628c4SJeff Layton */ 1170a835c4fSMatthew Wilcox int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) 1183e6628c4SJeff Layton { 119460488c5SMatthew Wilcox u32 id = idr->idr_next; 120460488c5SMatthew Wilcox int err, max = end > 0 ? end - 1 : INT_MAX; 1213e6628c4SJeff Layton 122460488c5SMatthew Wilcox if ((int)id < start) 123460488c5SMatthew Wilcox id = start; 1243e6628c4SJeff Layton 125460488c5SMatthew Wilcox err = idr_alloc_u32(idr, ptr, &id, max, gfp); 126460488c5SMatthew Wilcox if ((err == -ENOSPC) && (id > start)) { 127460488c5SMatthew Wilcox id = start; 128460488c5SMatthew Wilcox err = idr_alloc_u32(idr, ptr, &id, max, gfp); 129460488c5SMatthew Wilcox } 130460488c5SMatthew Wilcox if (err) 131460488c5SMatthew Wilcox return err; 1320a835c4fSMatthew Wilcox 133460488c5SMatthew Wilcox idr->idr_next = id + 1; 1343e6628c4SJeff Layton return id; 1353e6628c4SJeff Layton } 1363e6628c4SJeff Layton EXPORT_SYMBOL(idr_alloc_cyclic); 1373e6628c4SJeff Layton 1385806f07cSJeff Mahoney /** 1396ce711f2SMatthew Wilcox * idr_remove() - Remove an ID from the IDR. 1406ce711f2SMatthew Wilcox * @idr: IDR handle. 1416ce711f2SMatthew Wilcox * @id: Pointer ID. 1426ce711f2SMatthew Wilcox * 1436ce711f2SMatthew Wilcox * Removes this ID from the IDR. If the ID was not previously in the IDR, 1446ce711f2SMatthew Wilcox * this function returns %NULL. 1456ce711f2SMatthew Wilcox * 1466ce711f2SMatthew Wilcox * Since this function modifies the IDR, the caller should provide their 1476ce711f2SMatthew Wilcox * own locking to ensure that concurrent modification of the same IDR is 1486ce711f2SMatthew Wilcox * not possible. 1496ce711f2SMatthew Wilcox * 1506ce711f2SMatthew Wilcox * Return: The pointer formerly associated with this ID. 1516ce711f2SMatthew Wilcox */ 1526ce711f2SMatthew Wilcox void *idr_remove(struct idr *idr, unsigned long id) 1536ce711f2SMatthew Wilcox { 1546ce711f2SMatthew Wilcox return radix_tree_delete_item(&idr->idr_rt, id - idr->idr_base, NULL); 1556ce711f2SMatthew Wilcox } 1566ce711f2SMatthew Wilcox EXPORT_SYMBOL_GPL(idr_remove); 1576ce711f2SMatthew Wilcox 1586ce711f2SMatthew Wilcox /** 1596ce711f2SMatthew Wilcox * idr_find() - Return pointer for given ID. 1606ce711f2SMatthew Wilcox * @idr: IDR handle. 1616ce711f2SMatthew Wilcox * @id: Pointer ID. 1626ce711f2SMatthew Wilcox * 1636ce711f2SMatthew Wilcox * Looks up the pointer associated with this ID. A %NULL pointer may 1646ce711f2SMatthew Wilcox * indicate that @id is not allocated or that the %NULL pointer was 1656ce711f2SMatthew Wilcox * associated with this ID. 1666ce711f2SMatthew Wilcox * 1676ce711f2SMatthew Wilcox * This function can be called under rcu_read_lock(), given that the leaf 1686ce711f2SMatthew Wilcox * pointers lifetimes are correctly managed. 1696ce711f2SMatthew Wilcox * 1706ce711f2SMatthew Wilcox * Return: The pointer associated with this ID. 1716ce711f2SMatthew Wilcox */ 1726ce711f2SMatthew Wilcox void *idr_find(const struct idr *idr, unsigned long id) 1736ce711f2SMatthew Wilcox { 1746ce711f2SMatthew Wilcox return radix_tree_lookup(&idr->idr_rt, id - idr->idr_base); 1756ce711f2SMatthew Wilcox } 1766ce711f2SMatthew Wilcox EXPORT_SYMBOL_GPL(idr_find); 1776ce711f2SMatthew Wilcox 1786ce711f2SMatthew Wilcox /** 1797a457577SMatthew Wilcox * idr_for_each() - Iterate through all stored pointers. 1807a457577SMatthew Wilcox * @idr: IDR handle. 1817a457577SMatthew Wilcox * @fn: Function to be called for each pointer. 1827a457577SMatthew Wilcox * @data: Data passed to callback function. 18396d7fa42SKristian Hoegsberg * 1840a835c4fSMatthew Wilcox * The callback function will be called for each entry in @idr, passing 1857a457577SMatthew Wilcox * the ID, the entry and @data. 18696d7fa42SKristian Hoegsberg * 1870a835c4fSMatthew Wilcox * If @fn returns anything other than %0, the iteration stops and that 1880a835c4fSMatthew Wilcox * value is returned from this function. 18996d7fa42SKristian Hoegsberg * 1900a835c4fSMatthew Wilcox * idr_for_each() can be called concurrently with idr_alloc() and 1910a835c4fSMatthew Wilcox * idr_remove() if protected by RCU. Newly added entries may not be 1920a835c4fSMatthew Wilcox * seen and deleted entries may be seen, but adding and removing entries 1930a835c4fSMatthew Wilcox * will not cause other entries to be skipped, nor spurious ones to be seen. 19496d7fa42SKristian Hoegsberg */ 1950a835c4fSMatthew Wilcox int idr_for_each(const struct idr *idr, 19696d7fa42SKristian Hoegsberg int (*fn)(int id, void *p, void *data), void *data) 19796d7fa42SKristian Hoegsberg { 1980a835c4fSMatthew Wilcox struct radix_tree_iter iter; 1997e73eb0bSMatthew Wilcox void __rcu **slot; 2006ce711f2SMatthew Wilcox int base = idr->idr_base; 20196d7fa42SKristian Hoegsberg 2020a835c4fSMatthew Wilcox radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) { 20372fd6c7bSMatthew Wilcox int ret; 2044b0ad076SMatthew Wilcox unsigned long id = iter.index + base; 20572fd6c7bSMatthew Wilcox 2064b0ad076SMatthew Wilcox if (WARN_ON_ONCE(id > INT_MAX)) 20772fd6c7bSMatthew Wilcox break; 2084b0ad076SMatthew Wilcox ret = fn(id, rcu_dereference_raw(*slot), data); 2090a835c4fSMatthew Wilcox if (ret) 2100a835c4fSMatthew Wilcox return ret; 21196d7fa42SKristian Hoegsberg } 21296d7fa42SKristian Hoegsberg 2130a835c4fSMatthew Wilcox return 0; 21496d7fa42SKristian Hoegsberg } 21596d7fa42SKristian Hoegsberg EXPORT_SYMBOL(idr_for_each); 21696d7fa42SKristian Hoegsberg 21796d7fa42SKristian Hoegsberg /** 2185a74ac4cSMatthew Wilcox (Oracle) * idr_get_next_ul() - Find next populated entry. 2197a457577SMatthew Wilcox * @idr: IDR handle. 2207a457577SMatthew Wilcox * @nextid: Pointer to an ID. 22138460b48SKAMEZAWA Hiroyuki * 2220a835c4fSMatthew Wilcox * Returns the next populated entry in the tree with an ID greater than 2230a835c4fSMatthew Wilcox * or equal to the value pointed to by @nextid. On exit, @nextid is updated 2240a835c4fSMatthew Wilcox * to the ID of the found value. To use in a loop, the value pointed to by 2250a835c4fSMatthew Wilcox * nextid must be incremented by the user. 22638460b48SKAMEZAWA Hiroyuki */ 2275a74ac4cSMatthew Wilcox (Oracle) void *idr_get_next_ul(struct idr *idr, unsigned long *nextid) 22838460b48SKAMEZAWA Hiroyuki { 2290a835c4fSMatthew Wilcox struct radix_tree_iter iter; 2307e73eb0bSMatthew Wilcox void __rcu **slot; 2315c089fd0SMatthew Wilcox (Oracle) void *entry = NULL; 2324b0ad076SMatthew Wilcox unsigned long base = idr->idr_base; 2334b0ad076SMatthew Wilcox unsigned long id = *nextid; 23438460b48SKAMEZAWA Hiroyuki 2356ce711f2SMatthew Wilcox id = (id < base) ? 0 : id - base; 2365c089fd0SMatthew Wilcox (Oracle) radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, id) { 2375c089fd0SMatthew Wilcox (Oracle) entry = rcu_dereference_raw(*slot); 2385c089fd0SMatthew Wilcox (Oracle) if (!entry) 2395c089fd0SMatthew Wilcox (Oracle) continue; 2405c089fd0SMatthew Wilcox (Oracle) if (!xa_is_internal(entry)) 2415c089fd0SMatthew Wilcox (Oracle) break; 2425c089fd0SMatthew Wilcox (Oracle) if (slot != &idr->idr_rt.xa_head && !xa_is_retry(entry)) 2435c089fd0SMatthew Wilcox (Oracle) break; 2445c089fd0SMatthew Wilcox (Oracle) slot = radix_tree_iter_retry(&iter); 2455c089fd0SMatthew Wilcox (Oracle) } 2460a835c4fSMatthew Wilcox if (!slot) 24738460b48SKAMEZAWA Hiroyuki return NULL; 24838460b48SKAMEZAWA Hiroyuki 2495a74ac4cSMatthew Wilcox (Oracle) *nextid = iter.index + base; 2505c089fd0SMatthew Wilcox (Oracle) return entry; 25138460b48SKAMEZAWA Hiroyuki } 2525a74ac4cSMatthew Wilcox (Oracle) EXPORT_SYMBOL(idr_get_next_ul); 25338460b48SKAMEZAWA Hiroyuki 2547a457577SMatthew Wilcox /** 2555a74ac4cSMatthew Wilcox (Oracle) * idr_get_next() - Find next populated entry. 2567a457577SMatthew Wilcox * @idr: IDR handle. 2577a457577SMatthew Wilcox * @nextid: Pointer to an ID. 2587a457577SMatthew Wilcox * 2597a457577SMatthew Wilcox * Returns the next populated entry in the tree with an ID greater than 2607a457577SMatthew Wilcox * or equal to the value pointed to by @nextid. On exit, @nextid is updated 2617a457577SMatthew Wilcox * to the ID of the found value. To use in a loop, the value pointed to by 2627a457577SMatthew Wilcox * nextid must be incremented by the user. 2637a457577SMatthew Wilcox */ 2645a74ac4cSMatthew Wilcox (Oracle) void *idr_get_next(struct idr *idr, int *nextid) 265388f79fdSChris Mi { 2666ce711f2SMatthew Wilcox unsigned long id = *nextid; 2675a74ac4cSMatthew Wilcox (Oracle) void *entry = idr_get_next_ul(idr, &id); 268388f79fdSChris Mi 2695a74ac4cSMatthew Wilcox (Oracle) if (WARN_ON_ONCE(id > INT_MAX)) 270388f79fdSChris Mi return NULL; 2715a74ac4cSMatthew Wilcox (Oracle) *nextid = id; 2725a74ac4cSMatthew Wilcox (Oracle) return entry; 273388f79fdSChris Mi } 2745a74ac4cSMatthew Wilcox (Oracle) EXPORT_SYMBOL(idr_get_next); 275388f79fdSChris Mi 27638460b48SKAMEZAWA Hiroyuki /** 277460488c5SMatthew Wilcox * idr_replace() - replace pointer for given ID. 278460488c5SMatthew Wilcox * @idr: IDR handle. 279460488c5SMatthew Wilcox * @ptr: New pointer to associate with the ID. 280460488c5SMatthew Wilcox * @id: ID to change. 2815806f07cSJeff Mahoney * 2820a835c4fSMatthew Wilcox * Replace the pointer registered with an ID and return the old value. 2830a835c4fSMatthew Wilcox * This function can be called under the RCU read lock concurrently with 2840a835c4fSMatthew Wilcox * idr_alloc() and idr_remove() (as long as the ID being removed is not 2850a835c4fSMatthew Wilcox * the one being replaced!). 2865806f07cSJeff Mahoney * 287a70e43a5SEric Biggers * Returns: the old value on success. %-ENOENT indicates that @id was not 288234a4624SMatthew Wilcox * found. %-EINVAL indicates that @ptr was not valid. 2895806f07cSJeff Mahoney */ 290234a4624SMatthew Wilcox void *idr_replace(struct idr *idr, void *ptr, unsigned long id) 291388f79fdSChris Mi { 2920a835c4fSMatthew Wilcox struct radix_tree_node *node; 2937e73eb0bSMatthew Wilcox void __rcu **slot = NULL; 2940a835c4fSMatthew Wilcox void *entry; 2955806f07cSJeff Mahoney 2966ce711f2SMatthew Wilcox id -= idr->idr_base; 297e8c8d1bcSTejun Heo 2980a835c4fSMatthew Wilcox entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot); 2990a835c4fSMatthew Wilcox if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE)) 300b93804b2SLai Jiangshan return ERR_PTR(-ENOENT); 3016ff2d39bSManfred Spraul 3021cf56f9dSMatthew Wilcox __radix_tree_replace(&idr->idr_rt, node, slot, ptr); 3035806f07cSJeff Mahoney 3040a835c4fSMatthew Wilcox return entry; 3055806f07cSJeff Mahoney } 306234a4624SMatthew Wilcox EXPORT_SYMBOL(idr_replace); 3075806f07cSJeff Mahoney 30856083ab1SRandy Dunlap /** 30956083ab1SRandy Dunlap * DOC: IDA description 31072dba584STejun Heo * 3110a835c4fSMatthew Wilcox * The IDA is an ID allocator which does not provide the ability to 3120a835c4fSMatthew Wilcox * associate an ID with a pointer. As such, it only needs to store one 3130a835c4fSMatthew Wilcox * bit per ID, and so is more space efficient than an IDR. To use an IDA, 3140a835c4fSMatthew Wilcox * define it using DEFINE_IDA() (or embed a &struct ida in a data structure, 3150a835c4fSMatthew Wilcox * then initialise it using ida_init()). To allocate a new ID, call 3165ade60ddSMatthew Wilcox * ida_alloc(), ida_alloc_min(), ida_alloc_max() or ida_alloc_range(). 3175ade60ddSMatthew Wilcox * To free an ID, call ida_free(). 31872dba584STejun Heo * 319b03f8e43SMatthew Wilcox * ida_destroy() can be used to dispose of an IDA without needing to 320b03f8e43SMatthew Wilcox * free the individual IDs in it. You can use ida_is_empty() to find 321b03f8e43SMatthew Wilcox * out whether the IDA has any IDs currently allocated. 3220a835c4fSMatthew Wilcox * 323f32f004cSMatthew Wilcox * The IDA handles its own locking. It is safe to call any of the IDA 324f32f004cSMatthew Wilcox * functions without synchronisation in your code. 325f32f004cSMatthew Wilcox * 3260a835c4fSMatthew Wilcox * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward 3270a835c4fSMatthew Wilcox * limitation, it should be quite straightforward to raise the maximum. 32872dba584STejun Heo */ 32972dba584STejun Heo 330d37cacc5SMatthew Wilcox /* 331d37cacc5SMatthew Wilcox * Developer's notes: 332d37cacc5SMatthew Wilcox * 333f32f004cSMatthew Wilcox * The IDA uses the functionality provided by the XArray to store bitmaps in 334f32f004cSMatthew Wilcox * each entry. The XA_FREE_MARK is only cleared when all bits in the bitmap 335f32f004cSMatthew Wilcox * have been set. 336d37cacc5SMatthew Wilcox * 337f32f004cSMatthew Wilcox * I considered telling the XArray that each slot is an order-10 node 338f32f004cSMatthew Wilcox * and indexing by bit number, but the XArray can't allow a single multi-index 339f32f004cSMatthew Wilcox * entry in the head, which would significantly increase memory consumption 340f32f004cSMatthew Wilcox * for the IDA. So instead we divide the index by the number of bits in the 341f32f004cSMatthew Wilcox * leaf bitmap before doing a radix tree lookup. 342d37cacc5SMatthew Wilcox * 343d37cacc5SMatthew Wilcox * As an optimisation, if there are only a few low bits set in any given 3443159f943SMatthew Wilcox * leaf, instead of allocating a 128-byte bitmap, we store the bits 345f32f004cSMatthew Wilcox * as a value entry. Value entries never have the XA_FREE_MARK cleared 346f32f004cSMatthew Wilcox * because we can always convert them into a bitmap entry. 347d37cacc5SMatthew Wilcox * 348f32f004cSMatthew Wilcox * It would be possible to optimise further; once we've run out of a 349f32f004cSMatthew Wilcox * single 128-byte bitmap, we currently switch to a 576-byte node, put 350f32f004cSMatthew Wilcox * the 128-byte bitmap in the first entry and then start allocating extra 351f32f004cSMatthew Wilcox * 128-byte entries. We could instead use the 512 bytes of the node's 352f32f004cSMatthew Wilcox * data as a bitmap before moving to that scheme. I do not believe this 353f32f004cSMatthew Wilcox * is a worthwhile optimisation; Rasmus Villemoes surveyed the current 354f32f004cSMatthew Wilcox * users of the IDA and almost none of them use more than 1024 entries. 355f32f004cSMatthew Wilcox * Those that do use more than the 8192 IDs that the 512 bytes would 356f32f004cSMatthew Wilcox * provide. 357d37cacc5SMatthew Wilcox * 358f32f004cSMatthew Wilcox * The IDA always uses a lock to alloc/free. If we add a 'test_bit' 359d37cacc5SMatthew Wilcox * equivalent, it will still need locking. Going to RCU lookup would require 360d37cacc5SMatthew Wilcox * using RCU to free bitmaps, and that's not trivial without embedding an 361d37cacc5SMatthew Wilcox * RCU head in the bitmap, which adds a 2-pointer overhead to each 128-byte 362d37cacc5SMatthew Wilcox * bitmap, which is excessive. 363d37cacc5SMatthew Wilcox */ 364d37cacc5SMatthew Wilcox 36572dba584STejun Heo /** 3665ade60ddSMatthew Wilcox * ida_alloc_range() - Allocate an unused ID. 3675ade60ddSMatthew Wilcox * @ida: IDA handle. 3685ade60ddSMatthew Wilcox * @min: Lowest ID to allocate. 3695ade60ddSMatthew Wilcox * @max: Highest ID to allocate. 3705ade60ddSMatthew Wilcox * @gfp: Memory allocation flags. 37188eca020SRusty Russell * 3725ade60ddSMatthew Wilcox * Allocate an ID between @min and @max, inclusive. The allocated ID will 3735ade60ddSMatthew Wilcox * not exceed %INT_MAX, even if @max is larger. 37488eca020SRusty Russell * 3753b674261SStephen Boyd * Context: Any context. It is safe to call this function without 3763b674261SStephen Boyd * locking in your code. 3775ade60ddSMatthew Wilcox * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, 3785ade60ddSMatthew Wilcox * or %-ENOSPC if there are no free IDs. 37988eca020SRusty Russell */ 3805ade60ddSMatthew Wilcox int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max, 3815ade60ddSMatthew Wilcox gfp_t gfp) 38288eca020SRusty Russell { 383f32f004cSMatthew Wilcox XA_STATE(xas, &ida->xa, min / IDA_BITMAP_BITS); 384f32f004cSMatthew Wilcox unsigned bit = min % IDA_BITMAP_BITS; 38546cbc1d3STejun Heo unsigned long flags; 386f32f004cSMatthew Wilcox struct ida_bitmap *bitmap, *alloc = NULL; 38788eca020SRusty Russell 3885ade60ddSMatthew Wilcox if ((int)min < 0) 3895ade60ddSMatthew Wilcox return -ENOSPC; 39088eca020SRusty Russell 3915ade60ddSMatthew Wilcox if ((int)max < 0) 3925ade60ddSMatthew Wilcox max = INT_MAX; 39388eca020SRusty Russell 394f32f004cSMatthew Wilcox retry: 395f32f004cSMatthew Wilcox xas_lock_irqsave(&xas, flags); 396f32f004cSMatthew Wilcox next: 397f32f004cSMatthew Wilcox bitmap = xas_find_marked(&xas, max / IDA_BITMAP_BITS, XA_FREE_MARK); 398f32f004cSMatthew Wilcox if (xas.xa_index > min / IDA_BITMAP_BITS) 399f32f004cSMatthew Wilcox bit = 0; 400f32f004cSMatthew Wilcox if (xas.xa_index * IDA_BITMAP_BITS + bit > max) 401f32f004cSMatthew Wilcox goto nospc; 40288eca020SRusty Russell 403f32f004cSMatthew Wilcox if (xa_is_value(bitmap)) { 404f32f004cSMatthew Wilcox unsigned long tmp = xa_to_value(bitmap); 405f32f004cSMatthew Wilcox 406f32f004cSMatthew Wilcox if (bit < BITS_PER_XA_VALUE) { 407f32f004cSMatthew Wilcox bit = find_next_zero_bit(&tmp, BITS_PER_XA_VALUE, bit); 408f32f004cSMatthew Wilcox if (xas.xa_index * IDA_BITMAP_BITS + bit > max) 409f32f004cSMatthew Wilcox goto nospc; 410f32f004cSMatthew Wilcox if (bit < BITS_PER_XA_VALUE) { 411f32f004cSMatthew Wilcox tmp |= 1UL << bit; 412f32f004cSMatthew Wilcox xas_store(&xas, xa_mk_value(tmp)); 413f32f004cSMatthew Wilcox goto out; 414f32f004cSMatthew Wilcox } 415f32f004cSMatthew Wilcox } 416f32f004cSMatthew Wilcox bitmap = alloc; 417f32f004cSMatthew Wilcox if (!bitmap) 418f32f004cSMatthew Wilcox bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT); 419f32f004cSMatthew Wilcox if (!bitmap) 420f32f004cSMatthew Wilcox goto alloc; 421f32f004cSMatthew Wilcox bitmap->bitmap[0] = tmp; 422f32f004cSMatthew Wilcox xas_store(&xas, bitmap); 423f32f004cSMatthew Wilcox if (xas_error(&xas)) { 424f32f004cSMatthew Wilcox bitmap->bitmap[0] = 0; 425f32f004cSMatthew Wilcox goto out; 426f32f004cSMatthew Wilcox } 427f32f004cSMatthew Wilcox } 428f32f004cSMatthew Wilcox 429f32f004cSMatthew Wilcox if (bitmap) { 430f32f004cSMatthew Wilcox bit = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, bit); 431f32f004cSMatthew Wilcox if (xas.xa_index * IDA_BITMAP_BITS + bit > max) 432f32f004cSMatthew Wilcox goto nospc; 433f32f004cSMatthew Wilcox if (bit == IDA_BITMAP_BITS) 434f32f004cSMatthew Wilcox goto next; 435f32f004cSMatthew Wilcox 436f32f004cSMatthew Wilcox __set_bit(bit, bitmap->bitmap); 437f32f004cSMatthew Wilcox if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS)) 438f32f004cSMatthew Wilcox xas_clear_mark(&xas, XA_FREE_MARK); 439f32f004cSMatthew Wilcox } else { 440f32f004cSMatthew Wilcox if (bit < BITS_PER_XA_VALUE) { 441f32f004cSMatthew Wilcox bitmap = xa_mk_value(1UL << bit); 442f32f004cSMatthew Wilcox } else { 443f32f004cSMatthew Wilcox bitmap = alloc; 444f32f004cSMatthew Wilcox if (!bitmap) 445f32f004cSMatthew Wilcox bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT); 446f32f004cSMatthew Wilcox if (!bitmap) 447f32f004cSMatthew Wilcox goto alloc; 448f32f004cSMatthew Wilcox __set_bit(bit, bitmap->bitmap); 449f32f004cSMatthew Wilcox } 450f32f004cSMatthew Wilcox xas_store(&xas, bitmap); 451f32f004cSMatthew Wilcox } 452f32f004cSMatthew Wilcox out: 453f32f004cSMatthew Wilcox xas_unlock_irqrestore(&xas, flags); 454f32f004cSMatthew Wilcox if (xas_nomem(&xas, gfp)) { 455f32f004cSMatthew Wilcox xas.xa_index = min / IDA_BITMAP_BITS; 456f32f004cSMatthew Wilcox bit = min % IDA_BITMAP_BITS; 457f32f004cSMatthew Wilcox goto retry; 458f32f004cSMatthew Wilcox } 459f32f004cSMatthew Wilcox if (bitmap != alloc) 460f32f004cSMatthew Wilcox kfree(alloc); 461f32f004cSMatthew Wilcox if (xas_error(&xas)) 462f32f004cSMatthew Wilcox return xas_error(&xas); 463f32f004cSMatthew Wilcox return xas.xa_index * IDA_BITMAP_BITS + bit; 464f32f004cSMatthew Wilcox alloc: 465f32f004cSMatthew Wilcox xas_unlock_irqrestore(&xas, flags); 466f32f004cSMatthew Wilcox alloc = kzalloc(sizeof(*bitmap), gfp); 467f32f004cSMatthew Wilcox if (!alloc) 4685ade60ddSMatthew Wilcox return -ENOMEM; 469f32f004cSMatthew Wilcox xas_set(&xas, min / IDA_BITMAP_BITS); 470f32f004cSMatthew Wilcox bit = min % IDA_BITMAP_BITS; 471f32f004cSMatthew Wilcox goto retry; 472f32f004cSMatthew Wilcox nospc: 473f32f004cSMatthew Wilcox xas_unlock_irqrestore(&xas, flags); 474a219b856SMatthew Wilcox (Oracle) kfree(alloc); 475f32f004cSMatthew Wilcox return -ENOSPC; 47688eca020SRusty Russell } 4775ade60ddSMatthew Wilcox EXPORT_SYMBOL(ida_alloc_range); 47888eca020SRusty Russell 47988eca020SRusty Russell /** 4805ade60ddSMatthew Wilcox * ida_free() - Release an allocated ID. 4815ade60ddSMatthew Wilcox * @ida: IDA handle. 4825ade60ddSMatthew Wilcox * @id: Previously allocated ID. 483a2ef9471SDaniel Vetter * 4843b674261SStephen Boyd * Context: Any context. It is safe to call this function without 4853b674261SStephen Boyd * locking in your code. 48688eca020SRusty Russell */ 4875ade60ddSMatthew Wilcox void ida_free(struct ida *ida, unsigned int id) 48888eca020SRusty Russell { 489f32f004cSMatthew Wilcox XA_STATE(xas, &ida->xa, id / IDA_BITMAP_BITS); 490f32f004cSMatthew Wilcox unsigned bit = id % IDA_BITMAP_BITS; 491f32f004cSMatthew Wilcox struct ida_bitmap *bitmap; 49246cbc1d3STejun Heo unsigned long flags; 49346cbc1d3STejun Heo 494*fc82bbf4SLinus Torvalds if ((int)id < 0) 495*fc82bbf4SLinus Torvalds return; 496f32f004cSMatthew Wilcox 497f32f004cSMatthew Wilcox xas_lock_irqsave(&xas, flags); 498f32f004cSMatthew Wilcox bitmap = xas_load(&xas); 499f32f004cSMatthew Wilcox 500f32f004cSMatthew Wilcox if (xa_is_value(bitmap)) { 501f32f004cSMatthew Wilcox unsigned long v = xa_to_value(bitmap); 502f32f004cSMatthew Wilcox if (bit >= BITS_PER_XA_VALUE) 503f32f004cSMatthew Wilcox goto err; 504f32f004cSMatthew Wilcox if (!(v & (1UL << bit))) 505f32f004cSMatthew Wilcox goto err; 506f32f004cSMatthew Wilcox v &= ~(1UL << bit); 507f32f004cSMatthew Wilcox if (!v) 508f32f004cSMatthew Wilcox goto delete; 509f32f004cSMatthew Wilcox xas_store(&xas, xa_mk_value(v)); 510f32f004cSMatthew Wilcox } else { 511f32f004cSMatthew Wilcox if (!test_bit(bit, bitmap->bitmap)) 512f32f004cSMatthew Wilcox goto err; 513f32f004cSMatthew Wilcox __clear_bit(bit, bitmap->bitmap); 514f32f004cSMatthew Wilcox xas_set_mark(&xas, XA_FREE_MARK); 515f32f004cSMatthew Wilcox if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) { 516f32f004cSMatthew Wilcox kfree(bitmap); 517f32f004cSMatthew Wilcox delete: 518f32f004cSMatthew Wilcox xas_store(&xas, NULL); 519f32f004cSMatthew Wilcox } 520f32f004cSMatthew Wilcox } 521f32f004cSMatthew Wilcox xas_unlock_irqrestore(&xas, flags); 522f32f004cSMatthew Wilcox return; 523f32f004cSMatthew Wilcox err: 524f32f004cSMatthew Wilcox xas_unlock_irqrestore(&xas, flags); 525f32f004cSMatthew Wilcox WARN(1, "ida_free called for id=%d which is not allocated.\n", id); 52688eca020SRusty Russell } 5275ade60ddSMatthew Wilcox EXPORT_SYMBOL(ida_free); 528f32f004cSMatthew Wilcox 529f32f004cSMatthew Wilcox /** 530f32f004cSMatthew Wilcox * ida_destroy() - Free all IDs. 531f32f004cSMatthew Wilcox * @ida: IDA handle. 532f32f004cSMatthew Wilcox * 533f32f004cSMatthew Wilcox * Calling this function frees all IDs and releases all resources used 534f32f004cSMatthew Wilcox * by an IDA. When this call returns, the IDA is empty and can be reused 535f32f004cSMatthew Wilcox * or freed. If the IDA is already empty, there is no need to call this 536f32f004cSMatthew Wilcox * function. 537f32f004cSMatthew Wilcox * 5383b674261SStephen Boyd * Context: Any context. It is safe to call this function without 5393b674261SStephen Boyd * locking in your code. 540f32f004cSMatthew Wilcox */ 541f32f004cSMatthew Wilcox void ida_destroy(struct ida *ida) 542f32f004cSMatthew Wilcox { 543f32f004cSMatthew Wilcox XA_STATE(xas, &ida->xa, 0); 544f32f004cSMatthew Wilcox struct ida_bitmap *bitmap; 545f32f004cSMatthew Wilcox unsigned long flags; 546f32f004cSMatthew Wilcox 547f32f004cSMatthew Wilcox xas_lock_irqsave(&xas, flags); 548f32f004cSMatthew Wilcox xas_for_each(&xas, bitmap, ULONG_MAX) { 549f32f004cSMatthew Wilcox if (!xa_is_value(bitmap)) 550f32f004cSMatthew Wilcox kfree(bitmap); 551f32f004cSMatthew Wilcox xas_store(&xas, NULL); 552f32f004cSMatthew Wilcox } 553f32f004cSMatthew Wilcox xas_unlock_irqrestore(&xas, flags); 554f32f004cSMatthew Wilcox } 555f32f004cSMatthew Wilcox EXPORT_SYMBOL(ida_destroy); 556f32f004cSMatthew Wilcox 557f32f004cSMatthew Wilcox #ifndef __KERNEL__ 558f32f004cSMatthew Wilcox extern void xa_dump_index(unsigned long index, unsigned int shift); 559f32f004cSMatthew Wilcox #define IDA_CHUNK_SHIFT ilog2(IDA_BITMAP_BITS) 560f32f004cSMatthew Wilcox 561f32f004cSMatthew Wilcox static void ida_dump_entry(void *entry, unsigned long index) 562f32f004cSMatthew Wilcox { 563f32f004cSMatthew Wilcox unsigned long i; 564f32f004cSMatthew Wilcox 565f32f004cSMatthew Wilcox if (!entry) 566f32f004cSMatthew Wilcox return; 567f32f004cSMatthew Wilcox 568f32f004cSMatthew Wilcox if (xa_is_node(entry)) { 569f32f004cSMatthew Wilcox struct xa_node *node = xa_to_node(entry); 570f32f004cSMatthew Wilcox unsigned int shift = node->shift + IDA_CHUNK_SHIFT + 571f32f004cSMatthew Wilcox XA_CHUNK_SHIFT; 572f32f004cSMatthew Wilcox 573f32f004cSMatthew Wilcox xa_dump_index(index * IDA_BITMAP_BITS, shift); 574f32f004cSMatthew Wilcox xa_dump_node(node); 575f32f004cSMatthew Wilcox for (i = 0; i < XA_CHUNK_SIZE; i++) 576f32f004cSMatthew Wilcox ida_dump_entry(node->slots[i], 577f32f004cSMatthew Wilcox index | (i << node->shift)); 578f32f004cSMatthew Wilcox } else if (xa_is_value(entry)) { 579f32f004cSMatthew Wilcox xa_dump_index(index * IDA_BITMAP_BITS, ilog2(BITS_PER_LONG)); 580f32f004cSMatthew Wilcox pr_cont("value: data %lx [%px]\n", xa_to_value(entry), entry); 581f32f004cSMatthew Wilcox } else { 582f32f004cSMatthew Wilcox struct ida_bitmap *bitmap = entry; 583f32f004cSMatthew Wilcox 584f32f004cSMatthew Wilcox xa_dump_index(index * IDA_BITMAP_BITS, IDA_CHUNK_SHIFT); 585f32f004cSMatthew Wilcox pr_cont("bitmap: %p data", bitmap); 586f32f004cSMatthew Wilcox for (i = 0; i < IDA_BITMAP_LONGS; i++) 587f32f004cSMatthew Wilcox pr_cont(" %lx", bitmap->bitmap[i]); 588f32f004cSMatthew Wilcox pr_cont("\n"); 589f32f004cSMatthew Wilcox } 590f32f004cSMatthew Wilcox } 591f32f004cSMatthew Wilcox 592f32f004cSMatthew Wilcox static void ida_dump(struct ida *ida) 593f32f004cSMatthew Wilcox { 594f32f004cSMatthew Wilcox struct xarray *xa = &ida->xa; 595f32f004cSMatthew Wilcox pr_debug("ida: %p node %p free %d\n", ida, xa->xa_head, 596f32f004cSMatthew Wilcox xa->xa_flags >> ROOT_TAG_SHIFT); 597f32f004cSMatthew Wilcox ida_dump_entry(xa->xa_head, 0); 598f32f004cSMatthew Wilcox } 599f32f004cSMatthew Wilcox #endif 600