Thomas Gleixner | 68cf618 | 2019-05-28 10:10:22 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * include/linux/idr.h |
| 4 | * |
| 5 | * 2002-10-18 written by Jim Houston jim.houston@ccur.com |
| 6 | * Copyright (C) 2002 by Concurrent Computer Corporation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Small id to pointer translation service avoiding fixed sized |
| 9 | * tables. |
| 10 | */ |
Luben Tuikov | f668ab1 | 2005-11-08 17:14:08 +0100 | [diff] [blame] | 11 | |
| 12 | #ifndef __IDR_H__ |
| 13 | #define __IDR_H__ |
| 14 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 15 | #include <linux/radix-tree.h> |
| 16 | #include <linux/gfp.h> |
Matthew Wilcox | 7ad3d4d | 2016-12-16 11:55:56 -0500 | [diff] [blame] | 17 | #include <linux/percpu.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | struct idr { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 20 | struct radix_tree_root idr_rt; |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 21 | unsigned int idr_base; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 22 | unsigned int idr_next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 25 | /* |
| 26 | * The IDR API does not expose the tagging functionality of the radix tree |
| 27 | * to users. Use tag 0 to track whether a node has free space below it. |
| 28 | */ |
| 29 | #define IDR_FREE 0 |
| 30 | |
| 31 | /* Set the IDR flag and the IDR_FREE tag */ |
Matthew Wilcox | fa290cd | 2018-04-10 16:36:28 -0700 | [diff] [blame] | 32 | #define IDR_RT_MARKER (ROOT_IS_IDR | (__force gfp_t) \ |
| 33 | (1 << (ROOT_TAG_SHIFT + IDR_FREE))) |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 34 | |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 35 | #define IDR_INIT_BASE(name, base) { \ |
| 36 | .idr_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER), \ |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 37 | .idr_base = (base), \ |
| 38 | .idr_next = 0, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Nadia Derbey | f9c46d6 | 2008-07-25 01:48:01 -0700 | [diff] [blame] | 41 | /** |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 42 | * IDR_INIT() - Initialise an IDR. |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 43 | * @name: Name of IDR. |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 44 | * |
| 45 | * A freshly-initialised IDR contains no IDs. |
| 46 | */ |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 47 | #define IDR_INIT(name) IDR_INIT_BASE(name, 0) |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 48 | |
| 49 | /** |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 50 | * DEFINE_IDR() - Define a statically-allocated IDR. |
| 51 | * @name: Name of IDR. |
Matthew Wilcox | ac665d9 | 2018-02-06 15:05:49 -0500 | [diff] [blame] | 52 | * |
| 53 | * An IDR defined using this macro is ready for use with no additional |
| 54 | * initialisation required. It contains no IDs. |
| 55 | */ |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 56 | #define DEFINE_IDR(name) struct idr name = IDR_INIT(name) |
Matthew Wilcox | ac665d9 | 2018-02-06 15:05:49 -0500 | [diff] [blame] | 57 | |
| 58 | /** |
Matthew Wilcox | 4443061 | 2016-12-14 15:09:19 -0800 | [diff] [blame] | 59 | * idr_get_cursor - Return the current position of the cyclic allocator |
| 60 | * @idr: idr handle |
| 61 | * |
| 62 | * The value returned is the value that will be next returned from |
| 63 | * idr_alloc_cyclic() if it is free (otherwise the search will start from |
| 64 | * this position). |
| 65 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 66 | static inline unsigned int idr_get_cursor(const struct idr *idr) |
Matthew Wilcox | 4443061 | 2016-12-14 15:09:19 -0800 | [diff] [blame] | 67 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 68 | return READ_ONCE(idr->idr_next); |
Matthew Wilcox | 4443061 | 2016-12-14 15:09:19 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * idr_set_cursor - Set the current position of the cyclic allocator |
| 73 | * @idr: idr handle |
| 74 | * @val: new position |
| 75 | * |
| 76 | * The next call to idr_alloc_cyclic() will return @val if it is free |
| 77 | * (otherwise the search will start from this position). |
| 78 | */ |
| 79 | static inline void idr_set_cursor(struct idr *idr, unsigned int val) |
| 80 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 81 | WRITE_ONCE(idr->idr_next, val); |
Matthew Wilcox | 4443061 | 2016-12-14 15:09:19 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** |
Randy Dunlap | 56083ab | 2010-10-26 14:19:08 -0700 | [diff] [blame] | 85 | * DOC: idr sync |
Nadia Derbey | f9c46d6 | 2008-07-25 01:48:01 -0700 | [diff] [blame] | 86 | * idr synchronization (stolen from radix-tree.h) |
| 87 | * |
| 88 | * idr_find() is able to be called locklessly, using RCU. The caller must |
| 89 | * ensure calls to this function are made within rcu_read_lock() regions. |
| 90 | * Other readers (lock-free or otherwise) and modifications may be running |
| 91 | * concurrently. |
| 92 | * |
| 93 | * It is still required that the caller manage the synchronization and |
| 94 | * lifetimes of the items. So if RCU lock-free lookups are used, typically |
| 95 | * this would mean that the items have their own locks, or are amenable to |
| 96 | * lock-free access; and that the items are freed by RCU (or only freed after |
| 97 | * having been deleted from the idr tree *and* a synchronize_rcu() grace |
| 98 | * period). |
| 99 | */ |
| 100 | |
willy@infradead.org | 3c60e86 | 2018-06-13 11:45:55 -0700 | [diff] [blame] | 101 | #define idr_lock(idr) xa_lock(&(idr)->idr_rt) |
| 102 | #define idr_unlock(idr) xa_unlock(&(idr)->idr_rt) |
| 103 | #define idr_lock_bh(idr) xa_lock_bh(&(idr)->idr_rt) |
| 104 | #define idr_unlock_bh(idr) xa_unlock_bh(&(idr)->idr_rt) |
| 105 | #define idr_lock_irq(idr) xa_lock_irq(&(idr)->idr_rt) |
| 106 | #define idr_unlock_irq(idr) xa_unlock_irq(&(idr)->idr_rt) |
| 107 | #define idr_lock_irqsave(idr, flags) \ |
| 108 | xa_lock_irqsave(&(idr)->idr_rt, flags) |
| 109 | #define idr_unlock_irqrestore(idr, flags) \ |
| 110 | xa_unlock_irqrestore(&(idr)->idr_rt, flags) |
| 111 | |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 112 | void idr_preload(gfp_t gfp_mask); |
Chris Mi | 388f79f | 2017-08-30 02:31:57 -0400 | [diff] [blame] | 113 | |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 114 | int idr_alloc(struct idr *, void *ptr, int start, int end, gfp_t); |
| 115 | int __must_check idr_alloc_u32(struct idr *, void *ptr, u32 *id, |
Matthew Wilcox | e096f6a | 2017-11-28 10:14:27 -0500 | [diff] [blame] | 116 | unsigned long max, gfp_t); |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 117 | int idr_alloc_cyclic(struct idr *, void *ptr, int start, int end, gfp_t); |
| 118 | void *idr_remove(struct idr *, unsigned long id); |
| 119 | void *idr_find(const struct idr *, unsigned long id); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 120 | int idr_for_each(const struct idr *, |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 121 | int (*fn)(int id, void *p, void *data), void *data); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 122 | void *idr_get_next(struct idr *, int *nextid); |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 123 | void *idr_get_next_ul(struct idr *, unsigned long *nextid); |
Matthew Wilcox | 234a462 | 2017-11-28 09:56:36 -0500 | [diff] [blame] | 124 | void *idr_replace(struct idr *, void *, unsigned long id); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 125 | void idr_destroy(struct idr *); |
| 126 | |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 127 | /** |
| 128 | * idr_init_base() - Initialise an IDR. |
| 129 | * @idr: IDR handle. |
| 130 | * @base: The base value for the IDR. |
| 131 | * |
| 132 | * This variation of idr_init() creates an IDR which will allocate IDs |
| 133 | * starting at %base. |
| 134 | */ |
| 135 | static inline void idr_init_base(struct idr *idr, int base) |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 136 | { |
| 137 | INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER); |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 138 | idr->idr_base = base; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 139 | idr->idr_next = 0; |
| 140 | } |
| 141 | |
Matthew Wilcox | 6ce711f | 2017-11-30 13:45:11 -0500 | [diff] [blame] | 142 | /** |
| 143 | * idr_init() - Initialise an IDR. |
| 144 | * @idr: IDR handle. |
| 145 | * |
| 146 | * Initialise a dynamically allocated IDR. To initialise a |
| 147 | * statically allocated IDR, use DEFINE_IDR(). |
| 148 | */ |
| 149 | static inline void idr_init(struct idr *idr) |
| 150 | { |
| 151 | idr_init_base(idr, 0); |
| 152 | } |
| 153 | |
Matthew Wilcox | ac665d9 | 2018-02-06 15:05:49 -0500 | [diff] [blame] | 154 | /** |
| 155 | * idr_is_empty() - Are there any IDs allocated? |
| 156 | * @idr: IDR handle. |
| 157 | * |
| 158 | * Return: %true if any IDs have been allocated from this IDR. |
| 159 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 160 | static inline bool idr_is_empty(const struct idr *idr) |
| 161 | { |
| 162 | return radix_tree_empty(&idr->idr_rt) && |
| 163 | radix_tree_tagged(&idr->idr_rt, IDR_FREE); |
| 164 | } |
Luben Tuikov | f668ab1 | 2005-11-08 17:14:08 +0100 | [diff] [blame] | 165 | |
Tejun Heo | 49038ef | 2013-02-27 17:03:52 -0800 | [diff] [blame] | 166 | /** |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 167 | * idr_preload_end - end preload section started with idr_preload() |
| 168 | * |
| 169 | * Each idr_preload() should be matched with an invocation of this |
| 170 | * function. See idr_preload() for details. |
| 171 | */ |
| 172 | static inline void idr_preload_end(void) |
| 173 | { |
| 174 | preempt_enable(); |
| 175 | } |
| 176 | |
| 177 | /** |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 178 | * idr_for_each_entry() - Iterate over an IDR's elements of a given type. |
| 179 | * @idr: IDR handle. |
| 180 | * @entry: The type * to use as cursor |
| 181 | * @id: Entry ID. |
George Spelvin | b949be5 | 2013-03-27 14:08:33 +0100 | [diff] [blame] | 182 | * |
| 183 | * @entry and @id do not need to be initialized before the loop, and |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 184 | * after normal termination @entry is left with the value NULL. This |
George Spelvin | b949be5 | 2013-03-27 14:08:33 +0100 | [diff] [blame] | 185 | * is convenient for a "not found" value. |
Tejun Heo | 49038ef | 2013-02-27 17:03:52 -0800 | [diff] [blame] | 186 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 187 | #define idr_for_each_entry(idr, entry, id) \ |
| 188 | for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id) |
Tejun Heo | 49038ef | 2013-02-27 17:03:52 -0800 | [diff] [blame] | 189 | |
Andreas Gruenbacher | a55bbd3 | 2014-08-28 13:31:14 +0200 | [diff] [blame] | 190 | /** |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 191 | * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type. |
| 192 | * @idr: IDR handle. |
| 193 | * @entry: The type * to use as cursor. |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 194 | * @tmp: A temporary placeholder for ID. |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 195 | * @id: Entry ID. |
Andreas Gruenbacher | a55bbd3 | 2014-08-28 13:31:14 +0200 | [diff] [blame] | 196 | * |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 197 | * @entry and @id do not need to be initialized before the loop, and |
| 198 | * after normal termination @entry is left with the value NULL. This |
| 199 | * is convenient for a "not found" value. |
| 200 | */ |
Cong Wang | e33d2b7 | 2019-06-28 11:03:41 -0700 | [diff] [blame] | 201 | #define idr_for_each_entry_ul(idr, entry, tmp, id) \ |
| 202 | for (tmp = 0, id = 0; \ |
| 203 | tmp <= id && ((entry) = idr_get_next_ul(idr, &(id))) != NULL; \ |
| 204 | tmp = id, ++id) |
Matthew Wilcox | 7a45757 | 2017-11-28 15:39:51 -0500 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type |
| 208 | * @idr: IDR handle. |
| 209 | * @entry: The type * to use as a cursor. |
| 210 | * @id: Entry ID. |
| 211 | * |
| 212 | * Continue to iterate over entries, continuing after the current position. |
Andreas Gruenbacher | a55bbd3 | 2014-08-28 13:31:14 +0200 | [diff] [blame] | 213 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 214 | #define idr_for_each_entry_continue(idr, entry, id) \ |
| 215 | for ((entry) = idr_get_next((idr), &(id)); \ |
Andreas Gruenbacher | a55bbd3 | 2014-08-28 13:31:14 +0200 | [diff] [blame] | 216 | entry; \ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 217 | ++id, (entry) = idr_get_next((idr), &(id))) |
Andreas Gruenbacher | a55bbd3 | 2014-08-28 13:31:14 +0200 | [diff] [blame] | 218 | |
Cong Wang | d39d714 | 2019-06-28 11:03:42 -0700 | [diff] [blame] | 219 | /** |
| 220 | * idr_for_each_entry_continue_ul() - Continue iteration over an IDR's elements of a given type |
| 221 | * @idr: IDR handle. |
| 222 | * @entry: The type * to use as a cursor. |
| 223 | * @tmp: A temporary placeholder for ID. |
| 224 | * @id: Entry ID. |
| 225 | * |
| 226 | * Continue to iterate over entries, continuing after the current position. |
| 227 | */ |
| 228 | #define idr_for_each_entry_continue_ul(idr, entry, tmp, id) \ |
| 229 | for (tmp = id; \ |
| 230 | tmp <= id && ((entry) = idr_get_next_ul(idr, &(id))) != NULL; \ |
| 231 | tmp = id, ++id) |
| 232 | |
Tejun Heo | c8615d3 | 2013-03-13 14:59:42 -0700 | [diff] [blame] | 233 | /* |
Matthew Wilcox | f32f004 | 2018-07-04 15:42:46 -0400 | [diff] [blame] | 234 | * IDA - ID Allocator, use when translation from id to pointer isn't necessary. |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 235 | */ |
| 236 | #define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 237 | #define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long)) |
Namhyung Kim | ed9f524 | 2010-09-16 01:30:19 +0900 | [diff] [blame] | 238 | #define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8) |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 239 | |
| 240 | struct ida_bitmap { |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 241 | unsigned long bitmap[IDA_BITMAP_LONGS]; |
| 242 | }; |
| 243 | |
| 244 | struct ida { |
Matthew Wilcox | f32f004 | 2018-07-04 15:42:46 -0400 | [diff] [blame] | 245 | struct xarray xa; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 246 | }; |
| 247 | |
Matthew Wilcox | f32f004 | 2018-07-04 15:42:46 -0400 | [diff] [blame] | 248 | #define IDA_INIT_FLAGS (XA_FLAGS_LOCK_IRQ | XA_FLAGS_ALLOC) |
| 249 | |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 250 | #define IDA_INIT(name) { \ |
Matthew Wilcox | f32f004 | 2018-07-04 15:42:46 -0400 | [diff] [blame] | 251 | .xa = XARRAY_INIT(name, IDA_INIT_FLAGS) \ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 252 | } |
Matthew Wilcox | f6bb2a2 | 2018-04-10 16:36:52 -0700 | [diff] [blame] | 253 | #define DEFINE_IDA(name) struct ida name = IDA_INIT(name) |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 254 | |
Matthew Wilcox | 5ade60d | 2018-03-20 17:07:11 -0400 | [diff] [blame] | 255 | int ida_alloc_range(struct ida *, unsigned int min, unsigned int max, gfp_t); |
| 256 | void ida_free(struct ida *, unsigned int id); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 257 | void ida_destroy(struct ida *ida); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 258 | |
Matthew Wilcox | 5ade60d | 2018-03-20 17:07:11 -0400 | [diff] [blame] | 259 | /** |
| 260 | * ida_alloc() - Allocate an unused ID. |
| 261 | * @ida: IDA handle. |
| 262 | * @gfp: Memory allocation flags. |
| 263 | * |
| 264 | * Allocate an ID between 0 and %INT_MAX, inclusive. |
| 265 | * |
| 266 | * Context: Any context. |
| 267 | * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, |
| 268 | * or %-ENOSPC if there are no free IDs. |
| 269 | */ |
| 270 | static inline int ida_alloc(struct ida *ida, gfp_t gfp) |
| 271 | { |
| 272 | return ida_alloc_range(ida, 0, ~0, gfp); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * ida_alloc_min() - Allocate an unused ID. |
| 277 | * @ida: IDA handle. |
| 278 | * @min: Lowest ID to allocate. |
| 279 | * @gfp: Memory allocation flags. |
| 280 | * |
| 281 | * Allocate an ID between @min and %INT_MAX, inclusive. |
| 282 | * |
| 283 | * Context: Any context. |
| 284 | * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, |
| 285 | * or %-ENOSPC if there are no free IDs. |
| 286 | */ |
| 287 | static inline int ida_alloc_min(struct ida *ida, unsigned int min, gfp_t gfp) |
| 288 | { |
| 289 | return ida_alloc_range(ida, min, ~0, gfp); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * ida_alloc_max() - Allocate an unused ID. |
| 294 | * @ida: IDA handle. |
| 295 | * @max: Highest ID to allocate. |
| 296 | * @gfp: Memory allocation flags. |
| 297 | * |
| 298 | * Allocate an ID between 0 and @max, inclusive. |
| 299 | * |
| 300 | * Context: Any context. |
| 301 | * Return: The allocated ID, or %-ENOMEM if memory could not be allocated, |
| 302 | * or %-ENOSPC if there are no free IDs. |
| 303 | */ |
| 304 | static inline int ida_alloc_max(struct ida *ida, unsigned int max, gfp_t gfp) |
| 305 | { |
| 306 | return ida_alloc_range(ida, 0, max, gfp); |
| 307 | } |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 308 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 309 | static inline void ida_init(struct ida *ida) |
| 310 | { |
Matthew Wilcox | f32f004 | 2018-07-04 15:42:46 -0400 | [diff] [blame] | 311 | xa_init_flags(&ida->xa, IDA_INIT_FLAGS); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 312 | } |
| 313 | |
Matthew Wilcox | 5ade60d | 2018-03-20 17:07:11 -0400 | [diff] [blame] | 314 | #define ida_simple_get(ida, start, end, gfp) \ |
| 315 | ida_alloc_range(ida, start, (end) - 1, gfp) |
| 316 | #define ida_simple_remove(ida, id) ida_free(ida, id) |
Tejun Heo | 49038ef | 2013-02-27 17:03:52 -0800 | [diff] [blame] | 317 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame] | 318 | static inline bool ida_is_empty(const struct ida *ida) |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 319 | { |
Matthew Wilcox | f32f004 | 2018-07-04 15:42:46 -0400 | [diff] [blame] | 320 | return xa_empty(&ida->xa); |
Matthew Wilcox | 99c4940 | 2016-12-14 15:09:13 -0800 | [diff] [blame] | 321 | } |
Luben Tuikov | f668ab1 | 2005-11-08 17:14:08 +0100 | [diff] [blame] | 322 | #endif /* __IDR_H__ */ |