Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 1 | #include <linux/bitmap.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 2 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | #include <linux/idr.h> |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 4 | #include <linux/slab.h> |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 5 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 7 | static DEFINE_SPINLOCK(simple_ida_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 9 | /** |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 10 | * idr_alloc - allocate an id |
| 11 | * @idr: idr handle |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 12 | * @ptr: pointer to be associated with the new id |
| 13 | * @start: the minimum id (inclusive) |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 14 | * @end: the maximum id (exclusive) |
| 15 | * @gfp: memory allocation flags |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 16 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 17 | * Allocates an unused ID in the range [start, end). Returns -ENOSPC |
| 18 | * if there are no unused IDs in that range. |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 19 | * |
| 20 | * Note that @end is treated as max when <= 0. This is to always allow |
| 21 | * using @start + N as @end as long as N is inside integer range. |
| 22 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 23 | * Simultaneous modifications to the @idr are not allowed and should be |
| 24 | * prevented by the user, usually with a lock. idr_alloc() may be called |
| 25 | * concurrently with read-only accesses to the @idr, such as idr_find() and |
| 26 | * idr_for_each_entry(). |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 27 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 28 | int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 29 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 30 | void **slot; |
| 31 | struct radix_tree_iter iter; |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 32 | |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 33 | if (WARN_ON_ONCE(start < 0)) |
| 34 | return -EINVAL; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 35 | if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr))) |
| 36 | return -EINVAL; |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 37 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 38 | radix_tree_iter_init(&iter, start); |
| 39 | slot = idr_get_free(&idr->idr_rt, &iter, gfp, end); |
| 40 | if (IS_ERR(slot)) |
| 41 | return PTR_ERR(slot); |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 42 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 43 | radix_tree_iter_replace(&idr->idr_rt, &iter, slot, ptr); |
| 44 | radix_tree_iter_tag_clear(&idr->idr_rt, &iter, IDR_FREE); |
| 45 | return iter.index; |
Tejun Heo | d5c7409 | 2013-02-27 17:03:55 -0800 | [diff] [blame] | 46 | } |
| 47 | EXPORT_SYMBOL_GPL(idr_alloc); |
| 48 | |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 49 | /** |
| 50 | * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 51 | * @idr: idr handle |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 52 | * @ptr: pointer to be associated with the new id |
| 53 | * @start: the minimum id (inclusive) |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 54 | * @end: the maximum id (exclusive) |
| 55 | * @gfp: memory allocation flags |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 56 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 57 | * Allocates an ID larger than the last ID allocated if one is available. |
| 58 | * If not, it will attempt to allocate the smallest ID that is larger or |
| 59 | * equal to @start. |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 60 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 61 | int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp) |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 62 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 63 | int id, curr = idr->idr_next; |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 64 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 65 | if (curr < start) |
| 66 | curr = start; |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 67 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 68 | id = idr_alloc(idr, ptr, curr, end, gfp); |
| 69 | if ((id == -ENOSPC) && (curr > start)) |
| 70 | id = idr_alloc(idr, ptr, start, curr, gfp); |
| 71 | |
| 72 | if (id >= 0) |
| 73 | idr->idr_next = id + 1U; |
| 74 | |
Jeff Layton | 3e6628c4 | 2013-04-29 16:21:16 -0700 | [diff] [blame] | 75 | return id; |
| 76 | } |
| 77 | EXPORT_SYMBOL(idr_alloc_cyclic); |
| 78 | |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 79 | /** |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 80 | * idr_for_each - iterate through all stored pointers |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 81 | * @idr: idr handle |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 82 | * @fn: function to be called for each pointer |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 83 | * @data: data passed to callback function |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 84 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 85 | * The callback function will be called for each entry in @idr, passing |
| 86 | * the id, the pointer and the data pointer passed to this function. |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 87 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 88 | * If @fn returns anything other than %0, the iteration stops and that |
| 89 | * value is returned from this function. |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 90 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 91 | * idr_for_each() can be called concurrently with idr_alloc() and |
| 92 | * idr_remove() if protected by RCU. Newly added entries may not be |
| 93 | * seen and deleted entries may be seen, but adding and removing entries |
| 94 | * will not cause other entries to be skipped, nor spurious ones to be seen. |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 95 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 96 | int idr_for_each(const struct idr *idr, |
| 97 | int (*fn)(int id, void *p, void *data), void *data) |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 98 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 99 | struct radix_tree_iter iter; |
| 100 | void **slot; |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 101 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 102 | radix_tree_for_each_slot(slot, &idr->idr_rt, &iter, 0) { |
| 103 | int ret = fn(iter.index, rcu_dereference_raw(*slot), data); |
| 104 | if (ret) |
| 105 | return ret; |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 108 | return 0; |
Kristian Hoegsberg | 96d7fa4 | 2007-07-15 23:37:24 -0700 | [diff] [blame] | 109 | } |
| 110 | EXPORT_SYMBOL(idr_for_each); |
| 111 | |
| 112 | /** |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 113 | * idr_get_next - Find next populated entry |
| 114 | * @idr: idr handle |
| 115 | * @nextid: Pointer to lowest possible ID to return |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 116 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 117 | * Returns the next populated entry in the tree with an ID greater than |
| 118 | * or equal to the value pointed to by @nextid. On exit, @nextid is updated |
| 119 | * to the ID of the found value. To use in a loop, the value pointed to by |
| 120 | * nextid must be incremented by the user. |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 121 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 122 | void *idr_get_next(struct idr *idr, int *nextid) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 123 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 124 | struct radix_tree_iter iter; |
| 125 | void **slot; |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 126 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 127 | slot = radix_tree_iter_find(&idr->idr_rt, &iter, *nextid); |
| 128 | if (!slot) |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 129 | return NULL; |
| 130 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 131 | *nextid = iter.index; |
| 132 | return rcu_dereference_raw(*slot); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 133 | } |
Ben Hutchings | 4d1ee80 | 2010-01-29 20:59:17 +0000 | [diff] [blame] | 134 | EXPORT_SYMBOL(idr_get_next); |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 135 | |
KAMEZAWA Hiroyuki | 38460b4 | 2009-04-02 16:57:25 -0700 | [diff] [blame] | 136 | /** |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 137 | * idr_replace - replace pointer for given id |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 138 | * @idr: idr handle |
| 139 | * @ptr: New pointer to associate with the ID |
| 140 | * @id: Lookup key |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 141 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 142 | * Replace the pointer registered with an ID and return the old value. |
| 143 | * This function can be called under the RCU read lock concurrently with |
| 144 | * idr_alloc() and idr_remove() (as long as the ID being removed is not |
| 145 | * the one being replaced!). |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 146 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 147 | * Returns: 0 on success. %-ENOENT indicates that @id was not found. |
| 148 | * %-EINVAL indicates that @id or @ptr were not valid. |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 149 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 150 | void *idr_replace(struct idr *idr, void *ptr, int id) |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 151 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 152 | struct radix_tree_node *node; |
| 153 | void **slot = NULL; |
| 154 | void *entry; |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 155 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 156 | if (WARN_ON_ONCE(id < 0)) |
| 157 | return ERR_PTR(-EINVAL); |
| 158 | if (WARN_ON_ONCE(radix_tree_is_internal_node(ptr))) |
Tejun Heo | e8c8d1b | 2013-02-27 17:05:04 -0800 | [diff] [blame] | 159 | return ERR_PTR(-EINVAL); |
| 160 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 161 | entry = __radix_tree_lookup(&idr->idr_rt, id, &node, &slot); |
| 162 | if (!slot || radix_tree_tag_get(&idr->idr_rt, id, IDR_FREE)) |
Lai Jiangshan | b93804b | 2014-06-06 14:37:13 -0700 | [diff] [blame] | 163 | return ERR_PTR(-ENOENT); |
Manfred Spraul | 6ff2d39 | 2008-12-01 13:14:02 -0800 | [diff] [blame] | 164 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 165 | __radix_tree_replace(&idr->idr_rt, node, slot, ptr, NULL, NULL); |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 166 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 167 | return entry; |
Jeff Mahoney | 5806f07 | 2006-06-26 00:27:19 -0700 | [diff] [blame] | 168 | } |
| 169 | EXPORT_SYMBOL(idr_replace); |
| 170 | |
Randy Dunlap | 56083ab | 2010-10-26 14:19:08 -0700 | [diff] [blame] | 171 | /** |
| 172 | * DOC: IDA description |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 173 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 174 | * The IDA is an ID allocator which does not provide the ability to |
| 175 | * associate an ID with a pointer. As such, it only needs to store one |
| 176 | * bit per ID, and so is more space efficient than an IDR. To use an IDA, |
| 177 | * define it using DEFINE_IDA() (or embed a &struct ida in a data structure, |
| 178 | * then initialise it using ida_init()). To allocate a new ID, call |
| 179 | * ida_simple_get(). To free an ID, call ida_simple_remove(). |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 180 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 181 | * If you have more complex locking requirements, use a loop around |
| 182 | * ida_pre_get() and ida_get_new() to allocate a new ID. Then use |
| 183 | * ida_remove() to free an ID. You must make sure that ida_get_new() and |
| 184 | * ida_remove() cannot be called at the same time as each other for the |
| 185 | * same IDA. |
| 186 | * |
| 187 | * You can also use ida_get_new_above() if you need an ID to be allocated |
| 188 | * above a particular number. ida_destroy() can be used to dispose of an |
| 189 | * IDA without needing to free the individual IDs in it. You can use |
| 190 | * ida_is_empty() to find out whether the IDA has any IDs currently allocated. |
| 191 | * |
| 192 | * IDs are currently limited to the range [0-INT_MAX]. If this is an awkward |
| 193 | * limitation, it should be quite straightforward to raise the maximum. |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 194 | */ |
| 195 | |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 196 | /** |
| 197 | * ida_pre_get - reserve resources for ida allocation |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 198 | * @ida: ida handle |
| 199 | * @gfp: memory allocation flags |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 200 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 201 | * This function should be called before calling ida_get_new_above(). If it |
| 202 | * is unable to allocate memory, it will return %0. On success, it returns %1. |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 203 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 204 | int ida_pre_get(struct ida *ida, gfp_t gfp) |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 205 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 206 | struct ida_bitmap *bitmap; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 207 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 208 | /* |
| 209 | * This looks weird, but the IDA API has no preload_end() equivalent. |
| 210 | * Instead, ida_get_new() can return -EAGAIN, prompting the caller |
| 211 | * to return to the ida_pre_get() step. |
| 212 | */ |
| 213 | idr_preload(gfp); |
| 214 | idr_preload_end(); |
| 215 | |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 216 | if (!ida->free_bitmap) { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 217 | bitmap = kmalloc(sizeof(struct ida_bitmap), gfp); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 218 | if (!bitmap) |
| 219 | return 0; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 220 | bitmap = xchg(&ida->free_bitmap, bitmap); |
| 221 | kfree(bitmap); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | return 1; |
| 225 | } |
| 226 | EXPORT_SYMBOL(ida_pre_get); |
| 227 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 228 | #define IDA_MAX (0x80000000U / IDA_BITMAP_BITS) |
| 229 | |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 230 | /** |
| 231 | * ida_get_new_above - allocate new ID above or equal to a start id |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 232 | * @ida: ida handle |
| 233 | * @start: id to start search at |
| 234 | * @id: pointer to the allocated handle |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 235 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 236 | * Allocate new ID above or equal to @start. It should be called |
| 237 | * with any required locks to ensure that concurrent calls to |
| 238 | * ida_get_new_above() / ida_get_new() / ida_remove() are not allowed. |
| 239 | * Consider using ida_simple_get() if you do not have complex locking |
| 240 | * requirements. |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 241 | * |
Randy Dunlap | 56083ab | 2010-10-26 14:19:08 -0700 | [diff] [blame] | 242 | * If memory is required, it will return %-EAGAIN, you should unlock |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 243 | * and go back to the ida_pre_get() call. If the ida is full, it will |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 244 | * return %-ENOSPC. On success, it will return 0. |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 245 | * |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 246 | * @id returns a value in the range @start ... %0x7fffffff. |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 247 | */ |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 248 | int ida_get_new_above(struct ida *ida, int start, int *id) |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 249 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 250 | struct radix_tree_root *root = &ida->ida_rt; |
| 251 | void **slot; |
| 252 | struct radix_tree_iter iter; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 253 | struct ida_bitmap *bitmap; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 254 | unsigned long index; |
| 255 | unsigned bit; |
| 256 | int new; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 257 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 258 | index = start / IDA_BITMAP_BITS; |
| 259 | bit = start % IDA_BITMAP_BITS; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 260 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 261 | slot = radix_tree_iter_init(&iter, index); |
| 262 | for (;;) { |
| 263 | if (slot) |
| 264 | slot = radix_tree_next_slot(slot, &iter, |
| 265 | RADIX_TREE_ITER_TAGGED); |
| 266 | if (!slot) { |
| 267 | slot = idr_get_free(root, &iter, GFP_NOWAIT, IDA_MAX); |
| 268 | if (IS_ERR(slot)) { |
| 269 | if (slot == ERR_PTR(-ENOMEM)) |
| 270 | return -EAGAIN; |
| 271 | return PTR_ERR(slot); |
| 272 | } |
| 273 | } |
| 274 | if (iter.index > index) |
| 275 | bit = 0; |
| 276 | new = iter.index * IDA_BITMAP_BITS; |
| 277 | bitmap = rcu_dereference_raw(*slot); |
| 278 | if (bitmap) { |
| 279 | bit = find_next_zero_bit(bitmap->bitmap, |
| 280 | IDA_BITMAP_BITS, bit); |
| 281 | new += bit; |
| 282 | if (new < 0) |
| 283 | return -ENOSPC; |
| 284 | if (bit == IDA_BITMAP_BITS) |
| 285 | continue; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 286 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 287 | __set_bit(bit, bitmap->bitmap); |
| 288 | if (bitmap_full(bitmap->bitmap, IDA_BITMAP_BITS)) |
| 289 | radix_tree_iter_tag_clear(root, &iter, |
| 290 | IDR_FREE); |
| 291 | } else { |
| 292 | new += bit; |
| 293 | if (new < 0) |
| 294 | return -ENOSPC; |
| 295 | bitmap = ida->free_bitmap; |
| 296 | if (!bitmap) |
| 297 | return -EAGAIN; |
| 298 | ida->free_bitmap = NULL; |
| 299 | memset(bitmap, 0, sizeof(*bitmap)); |
| 300 | __set_bit(bit, bitmap->bitmap); |
| 301 | radix_tree_iter_replace(root, &iter, slot, bitmap); |
| 302 | } |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 303 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 304 | *id = new; |
| 305 | return 0; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 306 | } |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 307 | } |
| 308 | EXPORT_SYMBOL(ida_get_new_above); |
| 309 | |
| 310 | /** |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 311 | * ida_remove - Free the given ID |
| 312 | * @ida: ida handle |
| 313 | * @id: ID to free |
| 314 | * |
| 315 | * This function should not be called at the same time as ida_get_new_above(). |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 316 | */ |
| 317 | void ida_remove(struct ida *ida, int id) |
| 318 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 319 | unsigned long index = id / IDA_BITMAP_BITS; |
| 320 | unsigned offset = id % IDA_BITMAP_BITS; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 321 | struct ida_bitmap *bitmap; |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 322 | struct radix_tree_iter iter; |
| 323 | void **slot; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 324 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 325 | slot = radix_tree_iter_lookup(&ida->ida_rt, &iter, index); |
| 326 | if (!slot) |
Lai Jiangshan | 8f9f665 | 2014-06-06 14:37:11 -0700 | [diff] [blame] | 327 | goto err; |
| 328 | |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 329 | bitmap = rcu_dereference_raw(*slot); |
| 330 | if (!test_bit(offset, bitmap->bitmap)) |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 331 | goto err; |
| 332 | |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 333 | __clear_bit(offset, bitmap->bitmap); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 334 | radix_tree_iter_tag_set(&ida->ida_rt, &iter, IDR_FREE); |
| 335 | if (bitmap_empty(bitmap->bitmap, IDA_BITMAP_BITS)) { |
| 336 | kfree(bitmap); |
| 337 | radix_tree_iter_delete(&ida->ida_rt, &iter, slot); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 338 | } |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 339 | return; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 340 | err: |
Jean Delvare | dd04b45 | 2013-07-03 15:08:47 -0700 | [diff] [blame] | 341 | WARN(1, "ida_remove called for id=%d which is not allocated.\n", id); |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 342 | } |
| 343 | EXPORT_SYMBOL(ida_remove); |
| 344 | |
| 345 | /** |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 346 | * ida_destroy - Free the contents of an ida |
| 347 | * @ida: ida handle |
| 348 | * |
| 349 | * Calling this function releases all resources associated with an IDA. When |
| 350 | * this call returns, the IDA is empty and can be reused or freed. The caller |
| 351 | * should not allow ida_remove() or ida_get_new_above() to be called at the |
| 352 | * same time. |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 353 | */ |
| 354 | void ida_destroy(struct ida *ida) |
| 355 | { |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 356 | struct radix_tree_iter iter; |
| 357 | void **slot; |
| 358 | |
| 359 | radix_tree_for_each_slot(slot, &ida->ida_rt, &iter, 0) { |
| 360 | struct ida_bitmap *bitmap = rcu_dereference_raw(*slot); |
| 361 | kfree(bitmap); |
| 362 | radix_tree_iter_delete(&ida->ida_rt, &iter, slot); |
| 363 | } |
| 364 | |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 365 | kfree(ida->free_bitmap); |
Matthew Wilcox | 0a835c4 | 2016-12-20 10:27:56 -0500 | [diff] [blame^] | 366 | ida->free_bitmap = NULL; |
Tejun Heo | 72dba58 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 367 | } |
| 368 | EXPORT_SYMBOL(ida_destroy); |
| 369 | |
| 370 | /** |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 371 | * ida_simple_get - get a new id. |
| 372 | * @ida: the (initialized) ida. |
| 373 | * @start: the minimum id (inclusive, < 0x8000000) |
| 374 | * @end: the maximum id (exclusive, < 0x8000000 or 0) |
| 375 | * @gfp_mask: memory allocation flags |
| 376 | * |
| 377 | * Allocates an id in the range start <= id < end, or returns -ENOSPC. |
| 378 | * On memory allocation failure, returns -ENOMEM. |
| 379 | * |
Daniel Vetter | a2ef947 | 2016-12-12 16:46:20 -0800 | [diff] [blame] | 380 | * Compared to ida_get_new_above() this function does its own locking, and |
| 381 | * should be used unless there are special requirements. |
| 382 | * |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 383 | * Use ida_simple_remove() to get rid of an id. |
| 384 | */ |
| 385 | int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end, |
| 386 | gfp_t gfp_mask) |
| 387 | { |
| 388 | int ret, id; |
| 389 | unsigned int max; |
Tejun Heo | 46cbc1d | 2011-11-02 13:38:46 -0700 | [diff] [blame] | 390 | unsigned long flags; |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 391 | |
| 392 | BUG_ON((int)start < 0); |
| 393 | BUG_ON((int)end < 0); |
| 394 | |
| 395 | if (end == 0) |
| 396 | max = 0x80000000; |
| 397 | else { |
| 398 | BUG_ON(end < start); |
| 399 | max = end - 1; |
| 400 | } |
| 401 | |
| 402 | again: |
| 403 | if (!ida_pre_get(ida, gfp_mask)) |
| 404 | return -ENOMEM; |
| 405 | |
Tejun Heo | 46cbc1d | 2011-11-02 13:38:46 -0700 | [diff] [blame] | 406 | spin_lock_irqsave(&simple_ida_lock, flags); |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 407 | ret = ida_get_new_above(ida, start, &id); |
| 408 | if (!ret) { |
| 409 | if (id > max) { |
| 410 | ida_remove(ida, id); |
| 411 | ret = -ENOSPC; |
| 412 | } else { |
| 413 | ret = id; |
| 414 | } |
| 415 | } |
Tejun Heo | 46cbc1d | 2011-11-02 13:38:46 -0700 | [diff] [blame] | 416 | spin_unlock_irqrestore(&simple_ida_lock, flags); |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 417 | |
| 418 | if (unlikely(ret == -EAGAIN)) |
| 419 | goto again; |
| 420 | |
| 421 | return ret; |
| 422 | } |
| 423 | EXPORT_SYMBOL(ida_simple_get); |
| 424 | |
| 425 | /** |
| 426 | * ida_simple_remove - remove an allocated id. |
| 427 | * @ida: the (initialized) ida. |
| 428 | * @id: the id returned by ida_simple_get. |
Daniel Vetter | a2ef947 | 2016-12-12 16:46:20 -0800 | [diff] [blame] | 429 | * |
| 430 | * Use to release an id allocated with ida_simple_get(). |
| 431 | * |
| 432 | * Compared to ida_remove() this function does its own locking, and should be |
| 433 | * used unless there are special requirements. |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 434 | */ |
| 435 | void ida_simple_remove(struct ida *ida, unsigned int id) |
| 436 | { |
Tejun Heo | 46cbc1d | 2011-11-02 13:38:46 -0700 | [diff] [blame] | 437 | unsigned long flags; |
| 438 | |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 439 | BUG_ON((int)id < 0); |
Tejun Heo | 46cbc1d | 2011-11-02 13:38:46 -0700 | [diff] [blame] | 440 | spin_lock_irqsave(&simple_ida_lock, flags); |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 441 | ida_remove(ida, id); |
Tejun Heo | 46cbc1d | 2011-11-02 13:38:46 -0700 | [diff] [blame] | 442 | spin_unlock_irqrestore(&simple_ida_lock, flags); |
Rusty Russell | 88eca02 | 2011-08-03 16:21:06 -0700 | [diff] [blame] | 443 | } |
| 444 | EXPORT_SYMBOL(ida_simple_remove); |