blob: 66a3748924828dcf4be6f3ffb06ef4a785600f7d [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Matthew Wilcox0a835c42016-12-20 10:27:56 -05002#include <linux/bitmap.h>
Matthew Wilcox460488c2017-11-28 15:16:24 -05003#include <linux/bug.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05004#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/idr.h>
Matthew Wilcox0a835c42016-12-20 10:27:56 -05006#include <linux/slab.h>
Rusty Russell88eca022011-08-03 16:21:06 -07007#include <linux/spinlock.h>
Matthew Wilcoxb94078e2018-06-07 17:10:45 -07008#include <linux/xarray.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
Matthew Wilcoxe096f6a2017-11-28 10:14:27 -050010/**
11 * idr_alloc_u32() - Allocate an ID.
12 * @idr: IDR handle.
13 * @ptr: Pointer to be associated with the new ID.
14 * @nextid: Pointer to an ID.
15 * @max: The maximum ID to allocate (inclusive).
16 * @gfp: Memory allocation flags.
17 *
18 * Allocates an unused ID in the range specified by @nextid and @max.
19 * Note that @max is inclusive whereas the @end parameter to idr_alloc()
Matthew Wilcox460488c2017-11-28 15:16:24 -050020 * is exclusive. The new ID is assigned to @nextid before the pointer
21 * is inserted into the IDR, so if @nextid points into the object pointed
22 * to by @ptr, a concurrent lookup will not find an uninitialised ID.
Matthew Wilcoxe096f6a2017-11-28 10:14:27 -050023 *
24 * The caller should provide their own locking to ensure that two
25 * concurrent modifications to the IDR are not possible. Read-only
26 * accesses to the IDR may be done under the RCU read lock or may
27 * exclude simultaneous writers.
28 *
29 * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed,
30 * or -ENOSPC if no free IDs could be found. If an error occurred,
31 * @nextid is unchanged.
32 */
33int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,
34 unsigned long max, gfp_t gfp)
35{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050036 struct radix_tree_iter iter;
Chris Mi388f79f2017-08-30 02:31:57 -040037 void __rcu **slot;
Matthew Wilcox4b0ad072018-02-26 14:39:30 -050038 unsigned int base = idr->idr_base;
39 unsigned int id = *nextid;
Tejun Heod5c74092013-02-27 17:03:55 -080040
Matthew Wilcoxf8d5d0c2017-11-07 16:30:10 -050041 if (WARN_ON_ONCE(!(idr->idr_rt.xa_flags & ROOT_IS_IDR)))
42 idr->idr_rt.xa_flags |= IDR_RT_MARKER;
Tejun Heod5c74092013-02-27 17:03:55 -080043
Matthew Wilcox6ce711f2017-11-30 13:45:11 -050044 id = (id < base) ? 0 : id - base;
45 radix_tree_iter_init(&iter, id);
46 slot = idr_get_free(&idr->idr_rt, &iter, gfp, max - base);
Matthew Wilcox0a835c42016-12-20 10:27:56 -050047 if (IS_ERR(slot))
48 return PTR_ERR(slot);
Tejun Heod5c74092013-02-27 17:03:55 -080049
Matthew Wilcox6ce711f2017-11-30 13:45:11 -050050 *nextid = iter.index + base;
Matthew Wilcox460488c2017-11-28 15:16:24 -050051 /* there is a memory barrier inside radix_tree_iter_replace() */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050052 radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr);
53 radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE);
Chris Mi388f79f2017-08-30 02:31:57 -040054
Chris Mi388f79f2017-08-30 02:31:57 -040055 return 0;
Tejun Heod5c74092013-02-27 17:03:55 -080056}
Matthew Wilcox460488c2017-11-28 15:16:24 -050057EXPORT_SYMBOL_GPL(idr_alloc_u32);
Tejun Heod5c74092013-02-27 17:03:55 -080058
Jeff Layton3e6628c42013-04-29 16:21:16 -070059/**
Matthew Wilcox460488c2017-11-28 15:16:24 -050060 * idr_alloc() - Allocate an ID.
61 * @idr: IDR handle.
62 * @ptr: Pointer to be associated with the new ID.
63 * @start: The minimum ID (inclusive).
64 * @end: The maximum ID (exclusive).
65 * @gfp: Memory allocation flags.
Jeff Layton3e6628c42013-04-29 16:21:16 -070066 *
Matthew Wilcox460488c2017-11-28 15:16:24 -050067 * Allocates an unused ID in the range specified by @start and @end. If
68 * @end is <= 0, it is treated as one larger than %INT_MAX. This allows
69 * callers to use @start + N as @end as long as N is within integer range.
70 *
71 * The caller should provide their own locking to ensure that two
72 * concurrent modifications to the IDR are not possible. Read-only
73 * accesses to the IDR may be done under the RCU read lock or may
74 * exclude simultaneous writers.
75 *
76 * Return: The newly allocated ID, -ENOMEM if memory allocation failed,
77 * or -ENOSPC if no free IDs could be found.
78 */
79int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
80{
81 u32 id = start;
82 int ret;
83
84 if (WARN_ON_ONCE(start < 0))
85 return -EINVAL;
86
87 ret = idr_alloc_u32(idr, ptr, &id, end > 0 ? end - 1 : INT_MAX, gfp);
88 if (ret)
89 return ret;
90
91 return id;
92}
93EXPORT_SYMBOL_GPL(idr_alloc);
94
95/**
96 * idr_alloc_cyclic() - Allocate an ID cyclically.
97 * @idr: IDR handle.
98 * @ptr: Pointer to be associated with the new ID.
99 * @start: The minimum ID (inclusive).
100 * @end: The maximum ID (exclusive).
101 * @gfp: Memory allocation flags.
102 *
103 * Allocates an unused ID in the range specified by @nextid and @end. If
104 * @end is <= 0, it is treated as one larger than %INT_MAX. This allows
105 * callers to use @start + N as @end as long as N is within integer range.
106 * The search for an unused ID will start at the last ID allocated and will
107 * wrap around to @start if no free IDs are found before reaching @end.
108 *
109 * The caller should provide their own locking to ensure that two
110 * concurrent modifications to the IDR are not possible. Read-only
111 * accesses to the IDR may be done under the RCU read lock or may
112 * exclude simultaneous writers.
113 *
114 * Return: The newly allocated ID, -ENOMEM if memory allocation failed,
115 * or -ENOSPC if no free IDs could be found.
Jeff Layton3e6628c42013-04-29 16:21:16 -0700116 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500117int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp)
Jeff Layton3e6628c42013-04-29 16:21:16 -0700118{
Matthew Wilcox460488c2017-11-28 15:16:24 -0500119 u32 id = idr->idr_next;
120 int err, max = end > 0 ? end - 1 : INT_MAX;
Jeff Layton3e6628c42013-04-29 16:21:16 -0700121
Matthew Wilcox460488c2017-11-28 15:16:24 -0500122 if ((int)id < start)
123 id = start;
Jeff Layton3e6628c42013-04-29 16:21:16 -0700124
Matthew Wilcox460488c2017-11-28 15:16:24 -0500125 err = idr_alloc_u32(idr, ptr, &id, max, gfp);
126 if ((err == -ENOSPC) && (id > start)) {
127 id = start;
128 err = idr_alloc_u32(idr, ptr, &id, max, gfp);
129 }
130 if (err)
131 return err;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500132
Matthew Wilcox460488c2017-11-28 15:16:24 -0500133 idr->idr_next = id + 1;
Jeff Layton3e6628c42013-04-29 16:21:16 -0700134 return id;
135}
136EXPORT_SYMBOL(idr_alloc_cyclic);
137
Jeff Mahoney5806f072006-06-26 00:27:19 -0700138/**
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500139 * idr_remove() - Remove an ID from the IDR.
140 * @idr: IDR handle.
141 * @id: Pointer ID.
142 *
143 * Removes this ID from the IDR. If the ID was not previously in the IDR,
144 * this function returns %NULL.
145 *
146 * Since this function modifies the IDR, the caller should provide their
147 * own locking to ensure that concurrent modification of the same IDR is
148 * not possible.
149 *
150 * Return: The pointer formerly associated with this ID.
151 */
152void *idr_remove(struct idr *idr, unsigned long id)
153{
154 return radix_tree_delete_item(&idr->idr_rt, id - idr->idr_base, NULL);
155}
156EXPORT_SYMBOL_GPL(idr_remove);
157
158/**
159 * idr_find() - Return pointer for given ID.
160 * @idr: IDR handle.
161 * @id: Pointer ID.
162 *
163 * Looks up the pointer associated with this ID. A %NULL pointer may
164 * indicate that @id is not allocated or that the %NULL pointer was
165 * associated with this ID.
166 *
167 * This function can be called under rcu_read_lock(), given that the leaf
168 * pointers lifetimes are correctly managed.
169 *
170 * Return: The pointer associated with this ID.
171 */
172void *idr_find(const struct idr *idr, unsigned long id)
173{
174 return radix_tree_lookup(&idr->idr_rt, id - idr->idr_base);
175}
176EXPORT_SYMBOL_GPL(idr_find);
177
178/**
Matthew Wilcox7a457572017-11-28 15:39:51 -0500179 * idr_for_each() - Iterate through all stored pointers.
180 * @idr: IDR handle.
181 * @fn: Function to be called for each pointer.
182 * @data: Data passed to callback function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700183 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500184 * The callback function will be called for each entry in @idr, passing
Matthew Wilcox7a457572017-11-28 15:39:51 -0500185 * the ID, the entry and @data.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700186 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500187 * If @fn returns anything other than %0, the iteration stops and that
188 * value is returned from this function.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700189 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500190 * idr_for_each() can be called concurrently with idr_alloc() and
191 * idr_remove() if protected by RCU. Newly added entries may not be
192 * seen and deleted entries may be seen, but adding and removing entries
193 * will not cause other entries to be skipped, nor spurious ones to be seen.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700194 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500195int idr_for_each(const struct idr *idr,
196 int (*fn)(int id, void *p, void *data), void *data)
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700197{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500198 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500199 void __rcu **slot;
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500200 int base = idr->idr_base;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700201
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500202 radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) {
Matthew Wilcox72fd6c72017-11-28 15:50:12 -0500203 int ret;
Matthew Wilcox4b0ad072018-02-26 14:39:30 -0500204 unsigned long id = iter.index + base;
Matthew Wilcox72fd6c72017-11-28 15:50:12 -0500205
Matthew Wilcox4b0ad072018-02-26 14:39:30 -0500206 if (WARN_ON_ONCE(id > INT_MAX))
Matthew Wilcox72fd6c72017-11-28 15:50:12 -0500207 break;
Matthew Wilcox4b0ad072018-02-26 14:39:30 -0500208 ret = fn(id, rcu_dereference_raw(*slot), data);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500209 if (ret)
210 return ret;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700211 }
212
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500213 return 0;
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700214}
215EXPORT_SYMBOL(idr_for_each);
216
217/**
Matthew Wilcox7a457572017-11-28 15:39:51 -0500218 * idr_get_next() - Find next populated entry.
219 * @idr: IDR handle.
220 * @nextid: Pointer to an ID.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700221 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500222 * Returns the next populated entry in the tree with an ID greater than
223 * or equal to the value pointed to by @nextid. On exit, @nextid is updated
224 * to the ID of the found value. To use in a loop, the value pointed to by
225 * nextid must be incremented by the user.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700226 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500227void *idr_get_next(struct idr *idr, int *nextid)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700228{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500229 struct radix_tree_iter iter;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500230 void __rcu **slot;
Matthew Wilcox (Oracle)5c089fd2019-05-14 16:05:45 -0400231 void *entry = NULL;
Matthew Wilcox4b0ad072018-02-26 14:39:30 -0500232 unsigned long base = idr->idr_base;
233 unsigned long id = *nextid;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700234
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500235 id = (id < base) ? 0 : id - base;
Matthew Wilcox (Oracle)5c089fd2019-05-14 16:05:45 -0400236 radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, id) {
237 entry = rcu_dereference_raw(*slot);
238 if (!entry)
239 continue;
240 if (!xa_is_internal(entry))
241 break;
242 if (slot != &idr->idr_rt.xa_head && !xa_is_retry(entry))
243 break;
244 slot = radix_tree_iter_retry(&iter);
245 }
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500246 if (!slot)
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700247 return NULL;
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500248 id = iter.index + base;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700249
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500250 if (WARN_ON_ONCE(id > INT_MAX))
Matthew Wilcox72fd6c72017-11-28 15:50:12 -0500251 return NULL;
252
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500253 *nextid = id;
Matthew Wilcox (Oracle)5c089fd2019-05-14 16:05:45 -0400254 return entry;
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700255}
Ben Hutchings4d1ee802010-01-29 20:59:17 +0000256EXPORT_SYMBOL(idr_get_next);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700257
Matthew Wilcox7a457572017-11-28 15:39:51 -0500258/**
259 * idr_get_next_ul() - Find next populated entry.
260 * @idr: IDR handle.
261 * @nextid: Pointer to an ID.
262 *
263 * Returns the next populated entry in the tree with an ID greater than
264 * or equal to the value pointed to by @nextid. On exit, @nextid is updated
265 * to the ID of the found value. To use in a loop, the value pointed to by
266 * nextid must be incremented by the user.
267 */
268void *idr_get_next_ul(struct idr *idr, unsigned long *nextid)
Chris Mi388f79f2017-08-30 02:31:57 -0400269{
270 struct radix_tree_iter iter;
271 void __rcu **slot;
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500272 unsigned long base = idr->idr_base;
273 unsigned long id = *nextid;
Chris Mi388f79f2017-08-30 02:31:57 -0400274
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500275 id = (id < base) ? 0 : id - base;
276 slot = radix_tree_iter_find(&idr->idr_rt, &iter, id);
Chris Mi388f79f2017-08-30 02:31:57 -0400277 if (!slot)
278 return NULL;
279
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500280 *nextid = iter.index + base;
Chris Mi388f79f2017-08-30 02:31:57 -0400281 return rcu_dereference_raw(*slot);
282}
Matthew Wilcox7a457572017-11-28 15:39:51 -0500283EXPORT_SYMBOL(idr_get_next_ul);
Chris Mi388f79f2017-08-30 02:31:57 -0400284
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700285/**
Matthew Wilcox460488c2017-11-28 15:16:24 -0500286 * idr_replace() - replace pointer for given ID.
287 * @idr: IDR handle.
288 * @ptr: New pointer to associate with the ID.
289 * @id: ID to change.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700290 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500291 * Replace the pointer registered with an ID and return the old value.
292 * This function can be called under the RCU read lock concurrently with
293 * idr_alloc() and idr_remove() (as long as the ID being removed is not
294 * the one being replaced!).
Jeff Mahoney5806f072006-06-26 00:27:19 -0700295 *
Eric Biggersa70e43a2017-10-03 16:16:13 -0700296 * Returns: the old value on success. %-ENOENT indicates that @id was not
Matthew Wilcox234a4622017-11-28 09:56:36 -0500297 * found. %-EINVAL indicates that @ptr was not valid.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700298 */
Matthew Wilcox234a4622017-11-28 09:56:36 -0500299void *idr_replace(struct idr *idr, void *ptr, unsigned long id)
Chris Mi388f79f2017-08-30 02:31:57 -0400300{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500301 struct radix_tree_node *node;
Matthew Wilcox7e73eb02017-02-13 16:03:55 -0500302 void __rcu **slot = NULL;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500303 void *entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700304
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500305 id -= idr->idr_base;
Tejun Heoe8c8d1b2013-02-27 17:05:04 -0800306
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500307 entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot);
308 if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE))
Lai Jiangshanb93804b2014-06-06 14:37:13 -0700309 return ERR_PTR(-ENOENT);
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800310
Matthew Wilcox1cf56f92018-04-09 16:24:45 -0400311 __radix_tree_replace(&idr->idr_rt, node, slot, ptr);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700312
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500313 return entry;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700314}
Matthew Wilcox234a4622017-11-28 09:56:36 -0500315EXPORT_SYMBOL(idr_replace);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700316
Randy Dunlap56083ab2010-10-26 14:19:08 -0700317/**
318 * DOC: IDA description
Tejun Heo72dba582007-06-14 03:45:13 +0900319 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500320 * The IDA is an ID allocator which does not provide the ability to
321 * associate an ID with a pointer. As such, it only needs to store one
322 * bit per ID, and so is more space efficient than an IDR. To use an IDA,
323 * define it using DEFINE_IDA() (or embed a &struct ida in a data structure,
324 * then initialise it using ida_init()). To allocate a new ID, call
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400325 * ida_alloc(), ida_alloc_min(), ida_alloc_max() or ida_alloc_range().
326 * To free an ID, call ida_free().
Tejun Heo72dba582007-06-14 03:45:13 +0900327 *
Matthew Wilcoxb03f8e42018-06-18 19:02:48 -0400328 * ida_destroy() can be used to dispose of an IDA without needing to
329 * free the individual IDs in it. You can use ida_is_empty() to find
330 * out whether the IDA has any IDs currently allocated.
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500331 *
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400332 * The IDA handles its own locking. It is safe to call any of the IDA
333 * functions without synchronisation in your code.
334 *
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500335 * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward
336 * limitation, it should be quite straightforward to raise the maximum.
Tejun Heo72dba582007-06-14 03:45:13 +0900337 */
338
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500339/*
340 * Developer's notes:
341 *
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400342 * The IDA uses the functionality provided by the XArray to store bitmaps in
343 * each entry. The XA_FREE_MARK is only cleared when all bits in the bitmap
344 * have been set.
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500345 *
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400346 * I considered telling the XArray that each slot is an order-10 node
347 * and indexing by bit number, but the XArray can't allow a single multi-index
348 * entry in the head, which would significantly increase memory consumption
349 * for the IDA. So instead we divide the index by the number of bits in the
350 * leaf bitmap before doing a radix tree lookup.
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500351 *
352 * As an optimisation, if there are only a few low bits set in any given
Matthew Wilcox3159f942017-11-03 13:30:42 -0400353 * leaf, instead of allocating a 128-byte bitmap, we store the bits
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400354 * as a value entry. Value entries never have the XA_FREE_MARK cleared
355 * because we can always convert them into a bitmap entry.
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500356 *
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400357 * It would be possible to optimise further; once we've run out of a
358 * single 128-byte bitmap, we currently switch to a 576-byte node, put
359 * the 128-byte bitmap in the first entry and then start allocating extra
360 * 128-byte entries. We could instead use the 512 bytes of the node's
361 * data as a bitmap before moving to that scheme. I do not believe this
362 * is a worthwhile optimisation; Rasmus Villemoes surveyed the current
363 * users of the IDA and almost none of them use more than 1024 entries.
364 * Those that do use more than the 8192 IDs that the 512 bytes would
365 * provide.
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500366 *
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400367 * The IDA always uses a lock to alloc/free. If we add a 'test_bit'
Matthew Wilcoxd37cacc2016-12-17 08:18:17 -0500368 * equivalent, it will still need locking. Going to RCU lookup would require
369 * using RCU to free bitmaps, and that's not trivial without embedding an
370 * RCU head in the bitmap, which adds a 2-pointer overhead to each 128-byte
371 * bitmap, which is excessive.
372 */
373
Tejun Heo72dba582007-06-14 03:45:13 +0900374/**
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400375 * ida_alloc_range() - Allocate an unused ID.
376 * @ida: IDA handle.
377 * @min: Lowest ID to allocate.
378 * @max: Highest ID to allocate.
379 * @gfp: Memory allocation flags.
Rusty Russell88eca022011-08-03 16:21:06 -0700380 *
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400381 * Allocate an ID between @min and @max, inclusive. The allocated ID will
382 * not exceed %INT_MAX, even if @max is larger.
Rusty Russell88eca022011-08-03 16:21:06 -0700383 *
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400384 * Context: Any context.
385 * Return: The allocated ID, or %-ENOMEM if memory could not be allocated,
386 * or %-ENOSPC if there are no free IDs.
Rusty Russell88eca022011-08-03 16:21:06 -0700387 */
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400388int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max,
389 gfp_t gfp)
Rusty Russell88eca022011-08-03 16:21:06 -0700390{
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400391 XA_STATE(xas, &ida->xa, min / IDA_BITMAP_BITS);
392 unsigned bit = min % IDA_BITMAP_BITS;
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700393 unsigned long flags;
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400394 struct ida_bitmap *bitmap, *alloc = NULL;
Rusty Russell88eca022011-08-03 16:21:06 -0700395
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400396 if ((int)min < 0)
397 return -ENOSPC;
Rusty Russell88eca022011-08-03 16:21:06 -0700398
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400399 if ((int)max < 0)
400 max = INT_MAX;
Rusty Russell88eca022011-08-03 16:21:06 -0700401
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400402retry:
403 xas_lock_irqsave(&xas, flags);
404next:
405 bitmap = xas_find_marked(&xas, max / IDA_BITMAP_BITS, XA_FREE_MARK);
406 if (xas.xa_index > min / IDA_BITMAP_BITS)
407 bit = 0;
408 if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
409 goto nospc;
Rusty Russell88eca022011-08-03 16:21:06 -0700410
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400411 if (xa_is_value(bitmap)) {
412 unsigned long tmp = xa_to_value(bitmap);
413
414 if (bit < BITS_PER_XA_VALUE) {
415 bit = find_next_zero_bit(&tmp, BITS_PER_XA_VALUE, bit);
416 if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
417 goto nospc;
418 if (bit < BITS_PER_XA_VALUE) {
419 tmp |= 1UL << bit;
420 xas_store(&xas, xa_mk_value(tmp));
421 goto out;
422 }
423 }
424 bitmap = alloc;
425 if (!bitmap)
426 bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT);
427 if (!bitmap)
428 goto alloc;
429 bitmap->bitmap[0] = tmp;
430 xas_store(&xas, bitmap);
431 if (xas_error(&xas)) {
432 bitmap->bitmap[0] = 0;
433 goto out;
434 }
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400435 }
Rusty Russell88eca022011-08-03 16:21:06 -0700436
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400437 if (bitmap) {
438 bit = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, bit);
439 if (xas.xa_index * IDA_BITMAP_BITS + bit > max)
440 goto nospc;
441 if (bit == IDA_BITMAP_BITS)
442 goto next;
443
444 __set_bit(bit, bitmap->bitmap);
445 if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS))
446 xas_clear_mark(&xas, XA_FREE_MARK);
447 } else {
448 if (bit < BITS_PER_XA_VALUE) {
449 bitmap = xa_mk_value(1UL << bit);
450 } else {
451 bitmap = alloc;
452 if (!bitmap)
453 bitmap = kzalloc(sizeof(*bitmap), GFP_NOWAIT);
454 if (!bitmap)
455 goto alloc;
456 __set_bit(bit, bitmap->bitmap);
457 }
458 xas_store(&xas, bitmap);
459 }
460out:
461 xas_unlock_irqrestore(&xas, flags);
462 if (xas_nomem(&xas, gfp)) {
463 xas.xa_index = min / IDA_BITMAP_BITS;
464 bit = min % IDA_BITMAP_BITS;
465 goto retry;
466 }
467 if (bitmap != alloc)
468 kfree(alloc);
469 if (xas_error(&xas))
470 return xas_error(&xas);
471 return xas.xa_index * IDA_BITMAP_BITS + bit;
472alloc:
473 xas_unlock_irqrestore(&xas, flags);
474 alloc = kzalloc(sizeof(*bitmap), gfp);
475 if (!alloc)
476 return -ENOMEM;
477 xas_set(&xas, min / IDA_BITMAP_BITS);
478 bit = min % IDA_BITMAP_BITS;
479 goto retry;
480nospc:
481 xas_unlock_irqrestore(&xas, flags);
482 return -ENOSPC;
Rusty Russell88eca022011-08-03 16:21:06 -0700483}
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400484EXPORT_SYMBOL(ida_alloc_range);
Rusty Russell88eca022011-08-03 16:21:06 -0700485
486/**
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400487 * ida_free() - Release an allocated ID.
488 * @ida: IDA handle.
489 * @id: Previously allocated ID.
Daniel Vettera2ef9472016-12-12 16:46:20 -0800490 *
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400491 * Context: Any context.
Rusty Russell88eca022011-08-03 16:21:06 -0700492 */
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400493void ida_free(struct ida *ida, unsigned int id)
Rusty Russell88eca022011-08-03 16:21:06 -0700494{
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400495 XA_STATE(xas, &ida->xa, id / IDA_BITMAP_BITS);
496 unsigned bit = id % IDA_BITMAP_BITS;
497 struct ida_bitmap *bitmap;
Tejun Heo46cbc1d2011-11-02 13:38:46 -0700498 unsigned long flags;
499
Rusty Russell88eca022011-08-03 16:21:06 -0700500 BUG_ON((int)id < 0);
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400501
502 xas_lock_irqsave(&xas, flags);
503 bitmap = xas_load(&xas);
504
505 if (xa_is_value(bitmap)) {
506 unsigned long v = xa_to_value(bitmap);
507 if (bit >= BITS_PER_XA_VALUE)
508 goto err;
509 if (!(v & (1UL << bit)))
510 goto err;
511 v &= ~(1UL << bit);
512 if (!v)
513 goto delete;
514 xas_store(&xas, xa_mk_value(v));
515 } else {
516 if (!test_bit(bit, bitmap->bitmap))
517 goto err;
518 __clear_bit(bit, bitmap->bitmap);
519 xas_set_mark(&xas, XA_FREE_MARK);
520 if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) {
521 kfree(bitmap);
522delete:
523 xas_store(&xas, NULL);
524 }
525 }
526 xas_unlock_irqrestore(&xas, flags);
527 return;
528 err:
529 xas_unlock_irqrestore(&xas, flags);
530 WARN(1, "ida_free called for id=%d which is not allocated.\n", id);
Rusty Russell88eca022011-08-03 16:21:06 -0700531}
Matthew Wilcox5ade60d2018-03-20 17:07:11 -0400532EXPORT_SYMBOL(ida_free);
Matthew Wilcoxf32f0042018-07-04 15:42:46 -0400533
534/**
535 * ida_destroy() - Free all IDs.
536 * @ida: IDA handle.
537 *
538 * Calling this function frees all IDs and releases all resources used
539 * by an IDA. When this call returns, the IDA is empty and can be reused
540 * or freed. If the IDA is already empty, there is no need to call this
541 * function.
542 *
543 * Context: Any context.
544 */
545void ida_destroy(struct ida *ida)
546{
547 XA_STATE(xas, &ida->xa, 0);
548 struct ida_bitmap *bitmap;
549 unsigned long flags;
550
551 xas_lock_irqsave(&xas, flags);
552 xas_for_each(&xas, bitmap, ULONG_MAX) {
553 if (!xa_is_value(bitmap))
554 kfree(bitmap);
555 xas_store(&xas, NULL);
556 }
557 xas_unlock_irqrestore(&xas, flags);
558}
559EXPORT_SYMBOL(ida_destroy);
560
561#ifndef __KERNEL__
562extern void xa_dump_index(unsigned long index, unsigned int shift);
563#define IDA_CHUNK_SHIFT ilog2(IDA_BITMAP_BITS)
564
565static void ida_dump_entry(void *entry, unsigned long index)
566{
567 unsigned long i;
568
569 if (!entry)
570 return;
571
572 if (xa_is_node(entry)) {
573 struct xa_node *node = xa_to_node(entry);
574 unsigned int shift = node->shift + IDA_CHUNK_SHIFT +
575 XA_CHUNK_SHIFT;
576
577 xa_dump_index(index * IDA_BITMAP_BITS, shift);
578 xa_dump_node(node);
579 for (i = 0; i < XA_CHUNK_SIZE; i++)
580 ida_dump_entry(node->slots[i],
581 index | (i << node->shift));
582 } else if (xa_is_value(entry)) {
583 xa_dump_index(index * IDA_BITMAP_BITS, ilog2(BITS_PER_LONG));
584 pr_cont("value: data %lx [%px]\n", xa_to_value(entry), entry);
585 } else {
586 struct ida_bitmap *bitmap = entry;
587
588 xa_dump_index(index * IDA_BITMAP_BITS, IDA_CHUNK_SHIFT);
589 pr_cont("bitmap: %p data", bitmap);
590 for (i = 0; i < IDA_BITMAP_LONGS; i++)
591 pr_cont(" %lx", bitmap->bitmap[i]);
592 pr_cont("\n");
593 }
594}
595
596static void ida_dump(struct ida *ida)
597{
598 struct xarray *xa = &ida->xa;
599 pr_debug("ida: %p node %p free %d\n", ida, xa->xa_head,
600 xa->xa_flags >> ROOT_TAG_SHIFT);
601 ida_dump_entry(xa->xa_head, 0);
602}
603#endif