aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorMatthew Wilcox <mawilcox@microsoft.com>2017-11-28 10:14:27 -0500
committerMatthew Wilcox <mawilcox@microsoft.com>2018-02-06 16:40:32 -0500
commite096f6a762bc54d0e5d44ba8b196e70b58e04367 (patch)
tree7565cca4a67e46001abbf0125501acf4e39a4a3c /lib
parent322d884ba731e05ca79ae58e9dee1ef7dc4de504 (diff)
idr: Add idr_alloc_u32 helper
All current users of idr_alloc_ext() actually want to allocate a u32 and idr_alloc_u32() fits their needs better. Like idr_get_next(), it uses a 'nextid' argument which serves as both a pointer to the start ID and the assigned ID (instead of a separate minimum and pointer-to-assigned-ID argument). It uses a 'max' argument rather than 'end' because the semantics that idr_alloc has for 'end' don't work well for unsigned types. Since idr_alloc_u32() returns an errno instead of the allocated ID, mark it as __must_check to help callers use it correctly. Include copious kernel-doc. Chris Mi <chrism@mellanox.com> has promised to contribute test-cases for idr_alloc_u32. Signed-off-by: Matthew Wilcox <mawilcox@microsoft.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/idr.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/idr.c b/lib/idr.c
index 577bfd4fe5c2..6d4ec2c2982b 100644
--- a/lib/idr.c
+++ b/lib/idr.c
@@ -7,6 +7,37 @@
7DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap); 7DEFINE_PER_CPU(struct ida_bitmap *, ida_bitmap);
8static DEFINE_SPINLOCK(simple_ida_lock); 8static DEFINE_SPINLOCK(simple_ida_lock);
9 9
10/**
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()
20 * is exclusive.
21 *
22 * The caller should provide their own locking to ensure that two
23 * concurrent modifications to the IDR are not possible. Read-only
24 * accesses to the IDR may be done under the RCU read lock or may
25 * exclude simultaneous writers.
26 *
27 * Return: 0 if an ID was allocated, -ENOMEM if memory allocation failed,
28 * or -ENOSPC if no free IDs could be found. If an error occurred,
29 * @nextid is unchanged.
30 */
31int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,
32 unsigned long max, gfp_t gfp)
33{
34 unsigned long tmp = *nextid;
35 int ret = idr_alloc_ext(idr, ptr, &tmp, tmp, max + 1, gfp);
36 *nextid = tmp;
37 return ret;
38}
39EXPORT_SYMBOL_GPL(idr_alloc_u32);
40
10int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index, 41int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index,
11 unsigned long start, unsigned long end, gfp_t gfp, 42 unsigned long start, unsigned long end, gfp_t gfp,
12 bool ext) 43 bool ext)