blob: b6439f3bfca4b98d5a197357bc1364dbf9661d44 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * 2002-10-18 written by Jim Houston jim.houston@ccur.com
3 * Copyright (C) 2002 by Concurrent Computer Corporation
4 * Distributed under the GNU GPL license version 2.
5 *
6 * Modified by George Anzinger to reuse immediately and to use
7 * find bit instructions. Also removed _irq on spinlocks.
8 *
Nadia Derbey3219b3b2008-07-25 01:48:00 -07009 * Modified by Nadia Derbey to make it RCU safe.
10 *
Jesper Juhle15ae2d2005-10-30 15:02:14 -080011 * Small id to pointer translation service.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
Jesper Juhle15ae2d2005-10-30 15:02:14 -080013 * It uses a radix tree like structure as a sparse array indexed
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * by the id to obtain the pointer. The bitmap makes allocating
Jesper Juhle15ae2d2005-10-30 15:02:14 -080015 * a new id quick.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 *
17 * You call it to allocate an id (an int) an associate with that id a
18 * pointer or what ever, we treat it as a (void *). You can pass this
19 * id to a user for him to pass back at a later time. You then pass
20 * that id to this code and it returns your pointer.
21
Jesper Juhle15ae2d2005-10-30 15:02:14 -080022 * You can release ids at any time. When all ids are released, most of
Fengguang Wu125c4c72012-10-04 17:13:15 -070023 * the memory is returned (we keep MAX_IDR_FREE) in a local pool so we
Jesper Juhle15ae2d2005-10-30 15:02:14 -080024 * don't need to go to the memory "store" during an id allocate, just
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * so you don't need to be too concerned about locking and conflicts
26 * with the slab allocator.
27 */
28
29#ifndef TEST // to test in user space...
30#include <linux/slab.h>
31#include <linux/init.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050032#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
Jeff Mahoney5806f072006-06-26 00:27:19 -070034#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/string.h>
36#include <linux/idr.h>
Rusty Russell88eca022011-08-03 16:21:06 -070037#include <linux/spinlock.h>
Tejun Heod5c74092013-02-27 17:03:55 -080038#include <linux/percpu.h>
39#include <linux/hardirq.h>
Thomas Gleixner3f3545a2013-11-13 11:21:19 -050040#include <linux/locallock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Tejun Heoe8c8d1b2013-02-27 17:05:04 -080042#define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
43#define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
44
45/* Leave the possibility of an incomplete final layer */
46#define MAX_IDR_LEVEL ((MAX_IDR_SHIFT + IDR_BITS - 1) / IDR_BITS)
47
48/* Number of id_layer structs to leave in free list */
49#define MAX_IDR_FREE (MAX_IDR_LEVEL * 2)
50
Christoph Lametere18b8902006-12-06 20:33:20 -080051static struct kmem_cache *idr_layer_cache;
Tejun Heod5c74092013-02-27 17:03:55 -080052static DEFINE_PER_CPU(struct idr_layer *, idr_preload_head);
53static DEFINE_PER_CPU(int, idr_preload_cnt);
Rusty Russell88eca022011-08-03 16:21:06 -070054static DEFINE_SPINLOCK(simple_ida_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Tejun Heo326cf0f2013-02-27 17:05:02 -080056/* the maximum ID which can be allocated given idr->layers */
57static int idr_max(int layers)
58{
59 int bits = min_t(int, layers * IDR_BITS, MAX_IDR_SHIFT);
60
61 return (1 << bits) - 1;
62}
63
Tejun Heo54616282013-02-27 17:05:07 -080064/*
65 * Prefix mask for an idr_layer at @layer. For layer 0, the prefix mask is
66 * all bits except for the lower IDR_BITS. For layer 1, 2 * IDR_BITS, and
67 * so on.
68 */
69static int idr_layer_prefix_mask(int layer)
70{
71 return ~idr_max(layer + 1);
72}
73
Nadia Derbey4ae53782008-07-25 01:47:58 -070074static struct idr_layer *get_from_free_list(struct idr *idp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 struct idr_layer *p;
Roland Dreierc259cc22006-07-14 00:24:23 -070077 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Roland Dreierc259cc22006-07-14 00:24:23 -070079 spin_lock_irqsave(&idp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if ((p = idp->id_free)) {
81 idp->id_free = p->ary[0];
82 idp->id_free_cnt--;
83 p->ary[0] = NULL;
84 }
Roland Dreierc259cc22006-07-14 00:24:23 -070085 spin_unlock_irqrestore(&idp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return(p);
87}
88
Tejun Heod5c74092013-02-27 17:03:55 -080089/**
90 * idr_layer_alloc - allocate a new idr_layer
91 * @gfp_mask: allocation mask
92 * @layer_idr: optional idr to allocate from
93 *
94 * If @layer_idr is %NULL, directly allocate one using @gfp_mask or fetch
95 * one from the per-cpu preload buffer. If @layer_idr is not %NULL, fetch
96 * an idr_layer from @idr->id_free.
97 *
98 * @layer_idr is to maintain backward compatibility with the old alloc
99 * interface - idr_pre_get() and idr_get_new*() - and will be removed
100 * together with per-pool preload buffer.
101 */
102static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
103{
104 struct idr_layer *new;
105
106 /* this is the old path, bypass to get_from_free_list() */
107 if (layer_idr)
108 return get_from_free_list(layer_idr);
109
Tejun Heo59bfbcf2013-03-13 14:59:49 -0700110 /*
111 * Try to allocate directly from kmem_cache. We want to try this
112 * before preload buffer; otherwise, non-preloading idr_alloc()
113 * users will end up taking advantage of preloading ones. As the
114 * following is allowed to fail for preloaded cases, suppress
115 * warning this time.
116 */
117 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask | __GFP_NOWARN);
Tejun Heod5c74092013-02-27 17:03:55 -0800118 if (new)
119 return new;
120
121 /*
122 * Try to fetch one from the per-cpu preload buffer if in process
123 * context. See idr_preload() for details.
124 */
Tejun Heo59bfbcf2013-03-13 14:59:49 -0700125 if (!in_interrupt()) {
126 preempt_disable();
127 new = __this_cpu_read(idr_preload_head);
128 if (new) {
129 __this_cpu_write(idr_preload_head, new->ary[0]);
130 __this_cpu_dec(idr_preload_cnt);
131 new->ary[0] = NULL;
132 }
133 preempt_enable();
134 if (new)
135 return new;
Tejun Heod5c74092013-02-27 17:03:55 -0800136 }
Tejun Heo59bfbcf2013-03-13 14:59:49 -0700137
138 /*
139 * Both failed. Try kmem_cache again w/o adding __GFP_NOWARN so
140 * that memory allocation failure warning is printed as intended.
141 */
142 return kmem_cache_zalloc(idr_layer_cache, gfp_mask);
Tejun Heod5c74092013-02-27 17:03:55 -0800143}
144
Nadia Derbeycf481c22008-07-25 01:48:02 -0700145static void idr_layer_rcu_free(struct rcu_head *head)
146{
147 struct idr_layer *layer;
148
149 layer = container_of(head, struct idr_layer, rcu_head);
150 kmem_cache_free(idr_layer_cache, layer);
151}
152
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800153static inline void free_layer(struct idr *idr, struct idr_layer *p)
Nadia Derbeycf481c22008-07-25 01:48:02 -0700154{
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800155 if (idr->hint && idr->hint == p)
156 RCU_INIT_POINTER(idr->hint, NULL);
Nadia Derbeycf481c22008-07-25 01:48:02 -0700157 call_rcu(&p->rcu_head, idr_layer_rcu_free);
158}
159
Sonny Rao1eec0052006-06-25 05:49:34 -0700160/* only called when idp->lock is held */
Nadia Derbey4ae53782008-07-25 01:47:58 -0700161static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
Sonny Rao1eec0052006-06-25 05:49:34 -0700162{
163 p->ary[0] = idp->id_free;
164 idp->id_free = p;
165 idp->id_free_cnt++;
166}
167
Nadia Derbey4ae53782008-07-25 01:47:58 -0700168static void move_to_free_list(struct idr *idp, struct idr_layer *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Roland Dreierc259cc22006-07-14 00:24:23 -0700170 unsigned long flags;
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /*
173 * Depends on the return element being zeroed.
174 */
Roland Dreierc259cc22006-07-14 00:24:23 -0700175 spin_lock_irqsave(&idp->lock, flags);
Nadia Derbey4ae53782008-07-25 01:47:58 -0700176 __move_to_free_list(idp, p);
Roland Dreierc259cc22006-07-14 00:24:23 -0700177 spin_unlock_irqrestore(&idp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900180static void idr_mark_full(struct idr_layer **pa, int id)
181{
182 struct idr_layer *p = pa[0];
183 int l = 0;
184
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800185 __set_bit(id & IDR_MASK, p->bitmap);
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900186 /*
187 * If this layer is full mark the bit in the layer above to
188 * show that this part of the radix tree is full. This may
189 * complete the layer above and require walking up the radix
190 * tree.
191 */
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800192 while (bitmap_full(p->bitmap, IDR_SIZE)) {
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900193 if (!(p = pa[++l]))
194 break;
195 id = id >> IDR_BITS;
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800196 __set_bit((id & IDR_MASK), p->bitmap);
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900197 }
198}
199
Tejun Heoc8615d32013-03-13 14:59:42 -0700200int __idr_pre_get(struct idr *idp, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Fengguang Wu125c4c72012-10-04 17:13:15 -0700202 while (idp->id_free_cnt < MAX_IDR_FREE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 struct idr_layer *new;
Andrew Morton5b019e92009-01-15 13:51:21 -0800204 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800205 if (new == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return (0);
Nadia Derbey4ae53782008-07-25 01:47:58 -0700207 move_to_free_list(idp, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209 return 1;
210}
Tejun Heoc8615d32013-03-13 14:59:42 -0700211EXPORT_SYMBOL(__idr_pre_get);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Tejun Heo12d1b432013-02-27 17:03:53 -0800213/**
214 * sub_alloc - try to allocate an id without growing the tree depth
215 * @idp: idr handle
216 * @starting_id: id to start search at
Tejun Heo12d1b432013-02-27 17:03:53 -0800217 * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer
Tejun Heod5c74092013-02-27 17:03:55 -0800218 * @gfp_mask: allocation mask for idr_layer_alloc()
219 * @layer_idr: optional idr passed to idr_layer_alloc()
Tejun Heo12d1b432013-02-27 17:03:53 -0800220 *
221 * Allocate an id in range [@starting_id, INT_MAX] from @idp without
222 * growing its depth. Returns
223 *
224 * the allocated id >= 0 if successful,
225 * -EAGAIN if the tree needs to grow for allocation to succeed,
226 * -ENOSPC if the id space is exhausted,
227 * -ENOMEM if more idr_layers need to be allocated.
228 */
Tejun Heod5c74092013-02-27 17:03:55 -0800229static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
230 gfp_t gfp_mask, struct idr *layer_idr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int n, m, sh;
233 struct idr_layer *p, *new;
Tejun Heo7aae6dd2007-06-14 03:45:12 +0900234 int l, id, oid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 id = *starting_id;
Tejun Heo7aae6dd2007-06-14 03:45:12 +0900237 restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 p = idp->top;
239 l = idp->layers;
240 pa[l--] = NULL;
241 while (1) {
242 /*
243 * We run around this while until we reach the leaf node...
244 */
245 n = (id >> (IDR_BITS*l)) & IDR_MASK;
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800246 m = find_next_zero_bit(p->bitmap, IDR_SIZE, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (m == IDR_SIZE) {
248 /* no space available go back to previous layer. */
249 l++;
Tejun Heo7aae6dd2007-06-14 03:45:12 +0900250 oid = id;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800251 id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
Tejun Heo7aae6dd2007-06-14 03:45:12 +0900252
253 /* if already at the top layer, we need to grow */
Lai Jiangshan2ada32b2014-06-06 14:37:10 -0700254 if (id > idr_max(idp->layers)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 *starting_id = id;
Tejun Heo12d1b432013-02-27 17:03:53 -0800256 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Tejun Heod2e72762010-02-22 12:44:19 -0800258 p = pa[l];
259 BUG_ON(!p);
Tejun Heo7aae6dd2007-06-14 03:45:12 +0900260
261 /* If we need to go up one layer, continue the
262 * loop; otherwise, restart from the top.
263 */
264 sh = IDR_BITS * (l + 1);
265 if (oid >> sh == id >> sh)
266 continue;
267 else
268 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 if (m != n) {
271 sh = IDR_BITS*l;
272 id = ((id >> sh) ^ n ^ m) << sh;
273 }
Fengguang Wu125c4c72012-10-04 17:13:15 -0700274 if ((id >= MAX_IDR_BIT) || (id < 0))
Tejun Heo12d1b432013-02-27 17:03:53 -0800275 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if (l == 0)
277 break;
278 /*
279 * Create the layer below if it is missing.
280 */
281 if (!p->ary[m]) {
Tejun Heod5c74092013-02-27 17:03:55 -0800282 new = idr_layer_alloc(gfp_mask, layer_idr);
Nadia Derbey4ae53782008-07-25 01:47:58 -0700283 if (!new)
Tejun Heo12d1b432013-02-27 17:03:53 -0800284 return -ENOMEM;
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800285 new->layer = l-1;
Tejun Heo54616282013-02-27 17:05:07 -0800286 new->prefix = id & idr_layer_prefix_mask(new->layer);
Nadia Derbey3219b3b2008-07-25 01:48:00 -0700287 rcu_assign_pointer(p->ary[m], new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 p->count++;
289 }
290 pa[l--] = p;
291 p = p->ary[m];
292 }
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900293
294 pa[l] = p;
295 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900298static int idr_get_empty_slot(struct idr *idp, int starting_id,
Tejun Heod5c74092013-02-27 17:03:55 -0800299 struct idr_layer **pa, gfp_t gfp_mask,
300 struct idr *layer_idr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct idr_layer *p, *new;
303 int layers, v, id;
Roland Dreierc259cc22006-07-14 00:24:23 -0700304 unsigned long flags;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 id = starting_id;
307build_up:
308 p = idp->top;
309 layers = idp->layers;
310 if (unlikely(!p)) {
Tejun Heod5c74092013-02-27 17:03:55 -0800311 if (!(p = idr_layer_alloc(gfp_mask, layer_idr)))
Tejun Heo12d1b432013-02-27 17:03:53 -0800312 return -ENOMEM;
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800313 p->layer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 layers = 1;
315 }
316 /*
317 * Add a new layer to the top of the tree if the requested
318 * id is larger than the currently allocated space.
319 */
Tejun Heo326cf0f2013-02-27 17:05:02 -0800320 while (id > idr_max(layers)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 layers++;
Manfred Spraul711a49a2008-12-10 18:17:06 +0100322 if (!p->count) {
323 /* special case: if the tree is currently empty,
324 * then we grow the tree by moving the top node
325 * upwards.
326 */
327 p->layer++;
Tejun Heo54616282013-02-27 17:05:07 -0800328 WARN_ON_ONCE(p->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 continue;
Manfred Spraul711a49a2008-12-10 18:17:06 +0100330 }
Tejun Heod5c74092013-02-27 17:03:55 -0800331 if (!(new = idr_layer_alloc(gfp_mask, layer_idr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /*
333 * The allocation failed. If we built part of
334 * the structure tear it down.
335 */
Roland Dreierc259cc22006-07-14 00:24:23 -0700336 spin_lock_irqsave(&idp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 for (new = p; p && p != idp->top; new = p) {
338 p = p->ary[0];
339 new->ary[0] = NULL;
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800340 new->count = 0;
341 bitmap_clear(new->bitmap, 0, IDR_SIZE);
Nadia Derbey4ae53782008-07-25 01:47:58 -0700342 __move_to_free_list(idp, new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Roland Dreierc259cc22006-07-14 00:24:23 -0700344 spin_unlock_irqrestore(&idp->lock, flags);
Tejun Heo12d1b432013-02-27 17:03:53 -0800345 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 new->ary[0] = p;
348 new->count = 1;
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800349 new->layer = layers-1;
Tejun Heo54616282013-02-27 17:05:07 -0800350 new->prefix = id & idr_layer_prefix_mask(new->layer);
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800351 if (bitmap_full(p->bitmap, IDR_SIZE))
352 __set_bit(0, new->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 p = new;
354 }
Nadia Derbey3219b3b2008-07-25 01:48:00 -0700355 rcu_assign_pointer(idp->top, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 idp->layers = layers;
Tejun Heod5c74092013-02-27 17:03:55 -0800357 v = sub_alloc(idp, &id, pa, gfp_mask, layer_idr);
Tejun Heo12d1b432013-02-27 17:03:53 -0800358 if (v == -EAGAIN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto build_up;
360 return(v);
361}
362
Tejun Heo3594eb22013-02-27 17:03:54 -0800363/*
364 * @id and @pa are from a successful allocation from idr_get_empty_slot().
365 * Install the user pointer @ptr and mark the slot full.
366 */
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800367static void idr_fill_slot(struct idr *idr, void *ptr, int id,
368 struct idr_layer **pa)
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900369{
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800370 /* update hint used for lookup, cleared from free_layer() */
371 rcu_assign_pointer(idr->hint, pa[0]);
372
Tejun Heo3594eb22013-02-27 17:03:54 -0800373 rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
374 pa[0]->count++;
375 idr_mark_full(pa, id);
Tejun Heoe33ac8b2007-06-14 03:45:12 +0900376}
377
Tejun Heoc8615d32013-03-13 14:59:42 -0700378int __idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Tejun Heo326cf0f2013-02-27 17:05:02 -0800380 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 int rv;
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800382
Tejun Heod5c74092013-02-27 17:03:55 -0800383 rv = idr_get_empty_slot(idp, starting_id, pa, 0, idp);
Nadia Derbey944ca052008-07-25 01:47:59 -0700384 if (rv < 0)
Tejun Heo12d1b432013-02-27 17:03:53 -0800385 return rv == -ENOMEM ? -EAGAIN : rv;
Tejun Heo3594eb22013-02-27 17:03:54 -0800386
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800387 idr_fill_slot(idp, ptr, rv, pa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 *id = rv;
389 return 0;
390}
Tejun Heoc8615d32013-03-13 14:59:42 -0700391EXPORT_SYMBOL(__idr_get_new_above);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Thomas Gleixner3f3545a2013-11-13 11:21:19 -0500393#ifdef CONFIG_PREEMPT_RT_FULL
394static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
395
396static inline void idr_preload_lock(void)
397{
398 local_lock(idr_lock);
399}
400
401static inline void idr_preload_unlock(void)
402{
403 local_unlock(idr_lock);
404}
405
406void idr_preload_end(void)
407{
408 idr_preload_unlock();
409}
410EXPORT_SYMBOL(idr_preload_end);
411#else
412static inline void idr_preload_lock(void)
413{
414 preempt_disable();
415}
416
417static inline void idr_preload_unlock(void)
418{
419 preempt_enable();
420}
421#endif
422
Tejun Heod5c74092013-02-27 17:03:55 -0800423/**
424 * idr_preload - preload for idr_alloc()
425 * @gfp_mask: allocation mask to use for preloading
426 *
427 * Preload per-cpu layer buffer for idr_alloc(). Can only be used from
428 * process context and each idr_preload() invocation should be matched with
429 * idr_preload_end(). Note that preemption is disabled while preloaded.
430 *
431 * The first idr_alloc() in the preloaded section can be treated as if it
432 * were invoked with @gfp_mask used for preloading. This allows using more
433 * permissive allocation masks for idrs protected by spinlocks.
434 *
435 * For example, if idr_alloc() below fails, the failure can be treated as
436 * if idr_alloc() were called with GFP_KERNEL rather than GFP_NOWAIT.
437 *
438 * idr_preload(GFP_KERNEL);
439 * spin_lock(lock);
440 *
441 * id = idr_alloc(idr, ptr, start, end, GFP_NOWAIT);
442 *
443 * spin_unlock(lock);
444 * idr_preload_end();
445 * if (id < 0)
446 * error;
447 */
448void idr_preload(gfp_t gfp_mask)
449{
450 /*
451 * Consuming preload buffer from non-process context breaks preload
452 * allocation guarantee. Disallow usage from those contexts.
453 */
454 WARN_ON_ONCE(in_interrupt());
455 might_sleep_if(gfp_mask & __GFP_WAIT);
456
Thomas Gleixner3f3545a2013-11-13 11:21:19 -0500457 idr_preload_lock();
Tejun Heod5c74092013-02-27 17:03:55 -0800458
459 /*
460 * idr_alloc() is likely to succeed w/o full idr_layer buffer and
461 * return value from idr_alloc() needs to be checked for failure
462 * anyway. Silently give up if allocation fails. The caller can
463 * treat failures from idr_alloc() as if idr_alloc() were called
464 * with @gfp_mask which should be enough.
465 */
466 while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
467 struct idr_layer *new;
468
Thomas Gleixner3f3545a2013-11-13 11:21:19 -0500469 idr_preload_unlock();
Tejun Heod5c74092013-02-27 17:03:55 -0800470 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
Thomas Gleixner3f3545a2013-11-13 11:21:19 -0500471 idr_preload_lock();
Tejun Heod5c74092013-02-27 17:03:55 -0800472 if (!new)
473 break;
474
475 /* link the new one to per-cpu preload list */
476 new->ary[0] = __this_cpu_read(idr_preload_head);
477 __this_cpu_write(idr_preload_head, new);
478 __this_cpu_inc(idr_preload_cnt);
479 }
480}
481EXPORT_SYMBOL(idr_preload);
482
483/**
484 * idr_alloc - allocate new idr entry
485 * @idr: the (initialized) idr
486 * @ptr: pointer to be associated with the new id
487 * @start: the minimum id (inclusive)
488 * @end: the maximum id (exclusive, <= 0 for max)
489 * @gfp_mask: memory allocation flags
490 *
491 * Allocate an id in [start, end) and associate it with @ptr. If no ID is
492 * available in the specified range, returns -ENOSPC. On memory allocation
493 * failure, returns -ENOMEM.
494 *
495 * Note that @end is treated as max when <= 0. This is to always allow
496 * using @start + N as @end as long as N is inside integer range.
497 *
498 * The user is responsible for exclusively synchronizing all operations
499 * which may modify @idr. However, read-only accesses such as idr_find()
500 * or iteration can be performed under RCU read lock provided the user
501 * destroys @ptr in RCU-safe way after removal from idr.
502 */
503int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
504{
505 int max = end > 0 ? end - 1 : INT_MAX; /* inclusive upper limit */
Tejun Heo326cf0f2013-02-27 17:05:02 -0800506 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
Tejun Heod5c74092013-02-27 17:03:55 -0800507 int id;
508
509 might_sleep_if(gfp_mask & __GFP_WAIT);
510
511 /* sanity checks */
512 if (WARN_ON_ONCE(start < 0))
513 return -EINVAL;
514 if (unlikely(max < start))
515 return -ENOSPC;
516
517 /* allocate id */
518 id = idr_get_empty_slot(idr, start, pa, gfp_mask, NULL);
519 if (unlikely(id < 0))
520 return id;
521 if (unlikely(id > max))
522 return -ENOSPC;
523
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800524 idr_fill_slot(idr, ptr, id, pa);
Tejun Heod5c74092013-02-27 17:03:55 -0800525 return id;
526}
527EXPORT_SYMBOL_GPL(idr_alloc);
528
Jeff Layton3e6628c42013-04-29 16:21:16 -0700529/**
530 * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
531 * @idr: the (initialized) idr
532 * @ptr: pointer to be associated with the new id
533 * @start: the minimum id (inclusive)
534 * @end: the maximum id (exclusive, <= 0 for max)
535 * @gfp_mask: memory allocation flags
536 *
537 * Essentially the same as idr_alloc, but prefers to allocate progressively
538 * higher ids if it can. If the "cur" counter wraps, then it will start again
539 * at the "start" end of the range and allocate one that has already been used.
540 */
541int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end,
542 gfp_t gfp_mask)
543{
544 int id;
545
546 id = idr_alloc(idr, ptr, max(start, idr->cur), end, gfp_mask);
547 if (id == -ENOSPC)
548 id = idr_alloc(idr, ptr, start, end, gfp_mask);
549
550 if (likely(id >= 0))
551 idr->cur = id + 1;
552 return id;
553}
554EXPORT_SYMBOL(idr_alloc_cyclic);
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static void idr_remove_warning(int id)
557{
Nadia Derbeyf098ad62008-07-25 01:47:59 -0700558 printk(KERN_WARNING
559 "idr_remove called for id=%d which is not allocated.\n", id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dump_stack();
561}
562
563static void sub_remove(struct idr *idp, int shift, int id)
564{
565 struct idr_layer *p = idp->top;
Tejun Heo326cf0f2013-02-27 17:05:02 -0800566 struct idr_layer **pa[MAX_IDR_LEVEL + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct idr_layer ***paa = &pa[0];
Nadia Derbeycf481c22008-07-25 01:48:02 -0700568 struct idr_layer *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 int n;
570
571 *paa = NULL;
572 *++paa = &idp->top;
573
574 while ((shift > 0) && p) {
575 n = (id >> shift) & IDR_MASK;
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800576 __clear_bit(n, p->bitmap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *++paa = &p->ary[n];
578 p = p->ary[n];
579 shift -= IDR_BITS;
580 }
581 n = id & IDR_MASK;
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800582 if (likely(p != NULL && test_bit(n, p->bitmap))) {
583 __clear_bit(n, p->bitmap);
Nadia Derbeycf481c22008-07-25 01:48:02 -0700584 rcu_assign_pointer(p->ary[n], NULL);
585 to_free = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 while(*paa && ! --((**paa)->count)){
Nadia Derbeycf481c22008-07-25 01:48:02 -0700587 if (to_free)
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800588 free_layer(idp, to_free);
Nadia Derbeycf481c22008-07-25 01:48:02 -0700589 to_free = **paa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 **paa-- = NULL;
591 }
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800592 if (!*paa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 idp->layers = 0;
Nadia Derbeycf481c22008-07-25 01:48:02 -0700594 if (to_free)
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800595 free_layer(idp, to_free);
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800596 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 idr_remove_warning(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
600/**
Randy Dunlap56083ab2010-10-26 14:19:08 -0700601 * idr_remove - remove the given id and free its slot
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800602 * @idp: idr handle
603 * @id: unique key
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605void idr_remove(struct idr *idp, int id)
606{
607 struct idr_layer *p;
Nadia Derbeycf481c22008-07-25 01:48:02 -0700608 struct idr_layer *to_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Tejun Heo2e1c9b22013-03-08 12:43:30 -0800610 if (id < 0)
Tejun Heoe8c8d1b2013-02-27 17:05:04 -0800611 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
Jesper Juhle15ae2d2005-10-30 15:02:14 -0800614 if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
Nadia Derbeycf481c22008-07-25 01:48:02 -0700615 idp->top->ary[0]) {
616 /*
617 * Single child at leftmost slot: we can shrink the tree.
618 * This level is not needed anymore since when layers are
619 * inserted, they are inserted at the top of the existing
620 * tree.
621 */
622 to_free = idp->top;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 p = idp->top->ary[0];
Nadia Derbeycf481c22008-07-25 01:48:02 -0700624 rcu_assign_pointer(idp->top, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 --idp->layers;
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800626 to_free->count = 0;
627 bitmap_clear(to_free->bitmap, 0, IDR_SIZE);
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800628 free_layer(idp, to_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
Fengguang Wu125c4c72012-10-04 17:13:15 -0700630 while (idp->id_free_cnt >= MAX_IDR_FREE) {
Nadia Derbey4ae53782008-07-25 01:47:58 -0700631 p = get_from_free_list(idp);
Nadia Derbeycf481c22008-07-25 01:48:02 -0700632 /*
633 * Note: we don't call the rcu callback here, since the only
634 * layers that fall into the freelist are those that have been
635 * preallocated.
636 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 kmem_cache_free(idr_layer_cache, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Nadia Derbeyaf8e2a42008-05-01 04:34:57 -0700639 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641EXPORT_SYMBOL(idr_remove);
642
Tejun Heofe6e24e2013-02-27 17:03:50 -0800643void __idr_remove_all(struct idr *idp)
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700644{
Oleg Nesterov6ace06dc2007-07-31 00:39:19 -0700645 int n, id, max;
Imre Deak2dcb22b2010-05-26 14:43:38 -0700646 int bt_mask;
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700647 struct idr_layer *p;
Tejun Heo326cf0f2013-02-27 17:05:02 -0800648 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700649 struct idr_layer **paa = &pa[0];
650
651 n = idp->layers * IDR_BITS;
652 p = idp->top;
Paul E. McKenney1b233362009-03-10 12:55:52 -0700653 rcu_assign_pointer(idp->top, NULL);
Tejun Heo326cf0f2013-02-27 17:05:02 -0800654 max = idr_max(idp->layers);
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700655
656 id = 0;
Tejun Heo326cf0f2013-02-27 17:05:02 -0800657 while (id >= 0 && id <= max) {
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700658 while (n > IDR_BITS && p) {
659 n -= IDR_BITS;
660 *paa++ = p;
661 p = p->ary[(id >> n) & IDR_MASK];
662 }
663
Imre Deak2dcb22b2010-05-26 14:43:38 -0700664 bt_mask = id;
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700665 id += 1 << n;
Imre Deak2dcb22b2010-05-26 14:43:38 -0700666 /* Get the highest bit that the above add changed from 0->1. */
667 while (n < fls(id ^ bt_mask)) {
Nadia Derbeycf481c22008-07-25 01:48:02 -0700668 if (p)
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800669 free_layer(idp, p);
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700670 n += IDR_BITS;
671 p = *--paa;
672 }
673 }
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700674 idp->layers = 0;
675}
Tejun Heofe6e24e2013-02-27 17:03:50 -0800676EXPORT_SYMBOL(__idr_remove_all);
Kristian Hoegsberg23936cc2007-07-15 23:37:24 -0700677
678/**
Andrew Morton8d3b3592005-10-23 12:57:18 -0700679 * idr_destroy - release all cached layers within an idr tree
Naohiro Aotaea24ea82010-08-31 00:37:03 +0900680 * @idp: idr handle
Tejun Heo9bb26bc2013-02-27 17:03:35 -0800681 *
682 * Free all id mappings and all idp_layers. After this function, @idp is
683 * completely unused and can be freed / recycled. The caller is
684 * responsible for ensuring that no one else accesses @idp during or after
685 * idr_destroy().
686 *
687 * A typical clean-up sequence for objects stored in an idr tree will use
688 * idr_for_each() to free all objects, if necessay, then idr_destroy() to
689 * free up the id mappings and cached idr_layers.
Andrew Morton8d3b3592005-10-23 12:57:18 -0700690 */
691void idr_destroy(struct idr *idp)
692{
Tejun Heofe6e24e2013-02-27 17:03:50 -0800693 __idr_remove_all(idp);
Tejun Heo9bb26bc2013-02-27 17:03:35 -0800694
Andrew Morton8d3b3592005-10-23 12:57:18 -0700695 while (idp->id_free_cnt) {
Nadia Derbey4ae53782008-07-25 01:47:58 -0700696 struct idr_layer *p = get_from_free_list(idp);
Andrew Morton8d3b3592005-10-23 12:57:18 -0700697 kmem_cache_free(idr_layer_cache, p);
698 }
699}
700EXPORT_SYMBOL(idr_destroy);
701
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800702void *idr_find_slowpath(struct idr *idp, int id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 int n;
705 struct idr_layer *p;
706
Tejun Heo2e1c9b22013-03-08 12:43:30 -0800707 if (id < 0)
Tejun Heoe8c8d1b2013-02-27 17:05:04 -0800708 return NULL;
709
Paul E. McKenney96be7532010-02-22 17:04:55 -0800710 p = rcu_dereference_raw(idp->top);
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800711 if (!p)
712 return NULL;
713 n = (p->layer+1) * IDR_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Tejun Heo326cf0f2013-02-27 17:05:02 -0800715 if (id > idr_max(p->layer + 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return NULL;
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800717 BUG_ON(n == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 while (n > 0 && p) {
720 n -= IDR_BITS;
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800721 BUG_ON(n != p->layer*IDR_BITS);
Paul E. McKenney96be7532010-02-22 17:04:55 -0800722 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724 return((void *)p);
725}
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800726EXPORT_SYMBOL(idr_find_slowpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Jeff Mahoney5806f072006-06-26 00:27:19 -0700728/**
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700729 * idr_for_each - iterate through all stored pointers
730 * @idp: idr handle
731 * @fn: function to be called for each pointer
732 * @data: data passed back to callback function
733 *
734 * Iterate over the pointers registered with the given idr. The
735 * callback function will be called for each pointer currently
736 * registered, passing the id, the pointer and the data pointer passed
737 * to this function. It is not safe to modify the idr tree while in
738 * the callback, so functions such as idr_get_new and idr_remove are
739 * not allowed.
740 *
741 * We check the return of @fn each time. If it returns anything other
Randy Dunlap56083ab2010-10-26 14:19:08 -0700742 * than %0, we break out and return that value.
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700743 *
744 * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
745 */
746int idr_for_each(struct idr *idp,
747 int (*fn)(int id, void *p, void *data), void *data)
748{
749 int n, id, max, error = 0;
750 struct idr_layer *p;
Tejun Heo326cf0f2013-02-27 17:05:02 -0800751 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700752 struct idr_layer **paa = &pa[0];
753
754 n = idp->layers * IDR_BITS;
Paul E. McKenney96be7532010-02-22 17:04:55 -0800755 p = rcu_dereference_raw(idp->top);
Tejun Heo326cf0f2013-02-27 17:05:02 -0800756 max = idr_max(idp->layers);
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700757
758 id = 0;
Tejun Heo326cf0f2013-02-27 17:05:02 -0800759 while (id >= 0 && id <= max) {
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700760 while (n > 0 && p) {
761 n -= IDR_BITS;
762 *paa++ = p;
Paul E. McKenney96be7532010-02-22 17:04:55 -0800763 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700764 }
765
766 if (p) {
767 error = fn(id, (void *)p, data);
768 if (error)
769 break;
770 }
771
772 id += 1 << n;
773 while (n < fls(id)) {
774 n += IDR_BITS;
775 p = *--paa;
776 }
777 }
778
779 return error;
780}
781EXPORT_SYMBOL(idr_for_each);
782
783/**
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700784 * idr_get_next - lookup next object of id to given id.
785 * @idp: idr handle
Naohiro Aotaea24ea82010-08-31 00:37:03 +0900786 * @nextidp: pointer to lookup key
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700787 *
788 * Returns pointer to registered object with id, which is next number to
Naohiro Aota1458ce12010-08-27 17:43:46 +0900789 * given id. After being looked up, *@nextidp will be updated for the next
790 * iteration.
Hugh Dickins9f7de822012-03-21 16:34:20 -0700791 *
792 * This function can be called under rcu_read_lock(), given that the leaf
793 * pointers lifetimes are correctly managed.
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700794 */
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700795void *idr_get_next(struct idr *idp, int *nextidp)
796{
Tejun Heo326cf0f2013-02-27 17:05:02 -0800797 struct idr_layer *p, *pa[MAX_IDR_LEVEL + 1];
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700798 struct idr_layer **paa = &pa[0];
799 int id = *nextidp;
800 int n, max;
801
802 /* find first ent */
Paul E. McKenney94bfa3b2010-06-07 17:09:45 -0700803 p = rcu_dereference_raw(idp->top);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700804 if (!p)
805 return NULL;
Hugh Dickins9f7de822012-03-21 16:34:20 -0700806 n = (p->layer + 1) * IDR_BITS;
Tejun Heo326cf0f2013-02-27 17:05:02 -0800807 max = idr_max(p->layer + 1);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700808
Tejun Heo326cf0f2013-02-27 17:05:02 -0800809 while (id >= 0 && id <= max) {
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700810 while (n > 0 && p) {
811 n -= IDR_BITS;
812 *paa++ = p;
Paul E. McKenney94bfa3b2010-06-07 17:09:45 -0700813 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700814 }
815
816 if (p) {
817 *nextidp = id;
818 return p;
819 }
820
Tejun Heo6cdae742013-02-27 17:03:34 -0800821 /*
822 * Proceed to the next layer at the current level. Unlike
823 * idr_for_each(), @id isn't guaranteed to be aligned to
824 * layer boundary at this point and adding 1 << n may
825 * incorrectly skip IDs. Make sure we jump to the
826 * beginning of the next layer using round_up().
827 */
828 id = round_up(id + 1, 1 << n);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700829 while (n < fls(id)) {
830 n += IDR_BITS;
831 p = *--paa;
832 }
833 }
834 return NULL;
835}
Ben Hutchings4d1ee802010-01-29 20:59:17 +0000836EXPORT_SYMBOL(idr_get_next);
KAMEZAWA Hiroyuki38460b42009-04-02 16:57:25 -0700837
838
839/**
Jeff Mahoney5806f072006-06-26 00:27:19 -0700840 * idr_replace - replace pointer for given id
841 * @idp: idr handle
842 * @ptr: pointer you want associated with the id
843 * @id: lookup key
844 *
845 * Replace the pointer registered with an id and return the old value.
Randy Dunlap56083ab2010-10-26 14:19:08 -0700846 * A %-ENOENT return indicates that @id was not found.
847 * A %-EINVAL return indicates that @id was not within valid constraints.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700848 *
Nadia Derbeycf481c22008-07-25 01:48:02 -0700849 * The caller must serialize with writers.
Jeff Mahoney5806f072006-06-26 00:27:19 -0700850 */
851void *idr_replace(struct idr *idp, void *ptr, int id)
852{
853 int n;
854 struct idr_layer *p, *old_p;
855
Tejun Heo2e1c9b22013-03-08 12:43:30 -0800856 if (id < 0)
Tejun Heoe8c8d1b2013-02-27 17:05:04 -0800857 return ERR_PTR(-EINVAL);
858
Jeff Mahoney5806f072006-06-26 00:27:19 -0700859 p = idp->top;
Manfred Spraul6ff2d392008-12-01 13:14:02 -0800860 if (!p)
861 return ERR_PTR(-EINVAL);
862
Lai Jiangshan2ada32b2014-06-06 14:37:10 -0700863 if (id > idr_max(p->layer + 1))
Jeff Mahoney5806f072006-06-26 00:27:19 -0700864 return ERR_PTR(-EINVAL);
865
Lai Jiangshan2ada32b2014-06-06 14:37:10 -0700866 n = p->layer * IDR_BITS;
Jeff Mahoney5806f072006-06-26 00:27:19 -0700867 while ((n > 0) && p) {
868 p = p->ary[(id >> n) & IDR_MASK];
869 n -= IDR_BITS;
870 }
871
872 n = id & IDR_MASK;
Tejun Heo1d9b2e12013-02-27 17:05:05 -0800873 if (unlikely(p == NULL || !test_bit(n, p->bitmap)))
Jeff Mahoney5806f072006-06-26 00:27:19 -0700874 return ERR_PTR(-ENOENT);
875
876 old_p = p->ary[n];
Nadia Derbeycf481c22008-07-25 01:48:02 -0700877 rcu_assign_pointer(p->ary[n], ptr);
Jeff Mahoney5806f072006-06-26 00:27:19 -0700878
879 return old_p;
880}
881EXPORT_SYMBOL(idr_replace);
882
Akinobu Mita199f0ca2008-04-29 01:03:13 -0700883void __init idr_init_cache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884{
Akinobu Mita199f0ca2008-04-29 01:03:13 -0700885 idr_layer_cache = kmem_cache_create("idr_layer_cache",
Andrew Morton5b019e92009-01-15 13:51:21 -0800886 sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889/**
890 * idr_init - initialize idr handle
891 * @idp: idr handle
892 *
893 * This function is use to set up the handle (@idp) that you will pass
894 * to the rest of the functions.
895 */
896void idr_init(struct idr *idp)
897{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 memset(idp, 0, sizeof(struct idr));
899 spin_lock_init(&idp->lock);
900}
901EXPORT_SYMBOL(idr_init);
Tejun Heo72dba582007-06-14 03:45:13 +0900902
903
Randy Dunlap56083ab2010-10-26 14:19:08 -0700904/**
905 * DOC: IDA description
Tejun Heo72dba582007-06-14 03:45:13 +0900906 * IDA - IDR based ID allocator
907 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700908 * This is id allocator without id -> pointer translation. Memory
Tejun Heo72dba582007-06-14 03:45:13 +0900909 * usage is much lower than full blown idr because each id only
910 * occupies a bit. ida uses a custom leaf node which contains
911 * IDA_BITMAP_BITS slots.
912 *
913 * 2007-04-25 written by Tejun Heo <htejun@gmail.com>
914 */
915
916static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
917{
918 unsigned long flags;
919
920 if (!ida->free_bitmap) {
921 spin_lock_irqsave(&ida->idr.lock, flags);
922 if (!ida->free_bitmap) {
923 ida->free_bitmap = bitmap;
924 bitmap = NULL;
925 }
926 spin_unlock_irqrestore(&ida->idr.lock, flags);
927 }
928
929 kfree(bitmap);
930}
931
932/**
933 * ida_pre_get - reserve resources for ida allocation
934 * @ida: ida handle
935 * @gfp_mask: memory allocation flag
936 *
937 * This function should be called prior to locking and calling the
938 * following function. It preallocates enough memory to satisfy the
939 * worst possible allocation.
940 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700941 * If the system is REALLY out of memory this function returns %0,
942 * otherwise %1.
Tejun Heo72dba582007-06-14 03:45:13 +0900943 */
944int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
945{
946 /* allocate idr_layers */
Tejun Heoc8615d32013-03-13 14:59:42 -0700947 if (!__idr_pre_get(&ida->idr, gfp_mask))
Tejun Heo72dba582007-06-14 03:45:13 +0900948 return 0;
949
950 /* allocate free_bitmap */
951 if (!ida->free_bitmap) {
952 struct ida_bitmap *bitmap;
953
954 bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
955 if (!bitmap)
956 return 0;
957
958 free_bitmap(ida, bitmap);
959 }
960
961 return 1;
962}
963EXPORT_SYMBOL(ida_pre_get);
964
965/**
966 * ida_get_new_above - allocate new ID above or equal to a start id
967 * @ida: ida handle
Naohiro Aotaea24ea82010-08-31 00:37:03 +0900968 * @starting_id: id to start search at
Tejun Heo72dba582007-06-14 03:45:13 +0900969 * @p_id: pointer to the allocated handle
970 *
Wang Sheng-Huie3816c52011-10-31 17:12:36 -0700971 * Allocate new ID above or equal to @starting_id. It should be called
972 * with any required locks.
Tejun Heo72dba582007-06-14 03:45:13 +0900973 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700974 * If memory is required, it will return %-EAGAIN, you should unlock
Tejun Heo72dba582007-06-14 03:45:13 +0900975 * and go back to the ida_pre_get() call. If the ida is full, it will
Randy Dunlap56083ab2010-10-26 14:19:08 -0700976 * return %-ENOSPC.
Tejun Heo72dba582007-06-14 03:45:13 +0900977 *
Randy Dunlap56083ab2010-10-26 14:19:08 -0700978 * @p_id returns a value in the range @starting_id ... %0x7fffffff.
Tejun Heo72dba582007-06-14 03:45:13 +0900979 */
980int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
981{
Tejun Heo326cf0f2013-02-27 17:05:02 -0800982 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
Tejun Heo72dba582007-06-14 03:45:13 +0900983 struct ida_bitmap *bitmap;
984 unsigned long flags;
985 int idr_id = starting_id / IDA_BITMAP_BITS;
986 int offset = starting_id % IDA_BITMAP_BITS;
987 int t, id;
988
989 restart:
990 /* get vacant slot */
Tejun Heod5c74092013-02-27 17:03:55 -0800991 t = idr_get_empty_slot(&ida->idr, idr_id, pa, 0, &ida->idr);
Nadia Derbey944ca052008-07-25 01:47:59 -0700992 if (t < 0)
Tejun Heo12d1b432013-02-27 17:03:53 -0800993 return t == -ENOMEM ? -EAGAIN : t;
Tejun Heo72dba582007-06-14 03:45:13 +0900994
Fengguang Wu125c4c72012-10-04 17:13:15 -0700995 if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT)
Tejun Heo72dba582007-06-14 03:45:13 +0900996 return -ENOSPC;
997
998 if (t != idr_id)
999 offset = 0;
1000 idr_id = t;
1001
1002 /* if bitmap isn't there, create a new one */
1003 bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
1004 if (!bitmap) {
1005 spin_lock_irqsave(&ida->idr.lock, flags);
1006 bitmap = ida->free_bitmap;
1007 ida->free_bitmap = NULL;
1008 spin_unlock_irqrestore(&ida->idr.lock, flags);
1009
1010 if (!bitmap)
1011 return -EAGAIN;
1012
1013 memset(bitmap, 0, sizeof(struct ida_bitmap));
Nadia Derbey3219b3b2008-07-25 01:48:00 -07001014 rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
1015 (void *)bitmap);
Tejun Heo72dba582007-06-14 03:45:13 +09001016 pa[0]->count++;
1017 }
1018
1019 /* lookup for empty slot */
1020 t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
1021 if (t == IDA_BITMAP_BITS) {
1022 /* no empty slot after offset, continue to the next chunk */
1023 idr_id++;
1024 offset = 0;
1025 goto restart;
1026 }
1027
1028 id = idr_id * IDA_BITMAP_BITS + t;
Fengguang Wu125c4c72012-10-04 17:13:15 -07001029 if (id >= MAX_IDR_BIT)
Tejun Heo72dba582007-06-14 03:45:13 +09001030 return -ENOSPC;
1031
1032 __set_bit(t, bitmap->bitmap);
1033 if (++bitmap->nr_busy == IDA_BITMAP_BITS)
1034 idr_mark_full(pa, idr_id);
1035
1036 *p_id = id;
1037
1038 /* Each leaf node can handle nearly a thousand slots and the
1039 * whole idea of ida is to have small memory foot print.
1040 * Throw away extra resources one by one after each successful
1041 * allocation.
1042 */
1043 if (ida->idr.id_free_cnt || ida->free_bitmap) {
Nadia Derbey4ae53782008-07-25 01:47:58 -07001044 struct idr_layer *p = get_from_free_list(&ida->idr);
Tejun Heo72dba582007-06-14 03:45:13 +09001045 if (p)
1046 kmem_cache_free(idr_layer_cache, p);
1047 }
1048
1049 return 0;
1050}
1051EXPORT_SYMBOL(ida_get_new_above);
1052
1053/**
Tejun Heo72dba582007-06-14 03:45:13 +09001054 * ida_remove - remove the given ID
1055 * @ida: ida handle
1056 * @id: ID to free
1057 */
1058void ida_remove(struct ida *ida, int id)
1059{
1060 struct idr_layer *p = ida->idr.top;
1061 int shift = (ida->idr.layers - 1) * IDR_BITS;
1062 int idr_id = id / IDA_BITMAP_BITS;
1063 int offset = id % IDA_BITMAP_BITS;
1064 int n;
1065 struct ida_bitmap *bitmap;
1066
1067 /* clear full bits while looking up the leaf idr_layer */
1068 while ((shift > 0) && p) {
1069 n = (idr_id >> shift) & IDR_MASK;
Tejun Heo1d9b2e12013-02-27 17:05:05 -08001070 __clear_bit(n, p->bitmap);
Tejun Heo72dba582007-06-14 03:45:13 +09001071 p = p->ary[n];
1072 shift -= IDR_BITS;
1073 }
1074
1075 if (p == NULL)
1076 goto err;
1077
1078 n = idr_id & IDR_MASK;
Tejun Heo1d9b2e12013-02-27 17:05:05 -08001079 __clear_bit(n, p->bitmap);
Tejun Heo72dba582007-06-14 03:45:13 +09001080
1081 bitmap = (void *)p->ary[n];
1082 if (!test_bit(offset, bitmap->bitmap))
1083 goto err;
1084
1085 /* update bitmap and remove it if empty */
1086 __clear_bit(offset, bitmap->bitmap);
1087 if (--bitmap->nr_busy == 0) {
Tejun Heo1d9b2e12013-02-27 17:05:05 -08001088 __set_bit(n, p->bitmap); /* to please idr_remove() */
Tejun Heo72dba582007-06-14 03:45:13 +09001089 idr_remove(&ida->idr, idr_id);
1090 free_bitmap(ida, bitmap);
1091 }
1092
1093 return;
1094
1095 err:
1096 printk(KERN_WARNING
1097 "ida_remove called for id=%d which is not allocated.\n", id);
1098}
1099EXPORT_SYMBOL(ida_remove);
1100
1101/**
1102 * ida_destroy - release all cached layers within an ida tree
Naohiro Aotaea24ea82010-08-31 00:37:03 +09001103 * @ida: ida handle
Tejun Heo72dba582007-06-14 03:45:13 +09001104 */
1105void ida_destroy(struct ida *ida)
1106{
1107 idr_destroy(&ida->idr);
1108 kfree(ida->free_bitmap);
1109}
1110EXPORT_SYMBOL(ida_destroy);
1111
1112/**
Rusty Russell88eca022011-08-03 16:21:06 -07001113 * ida_simple_get - get a new id.
1114 * @ida: the (initialized) ida.
1115 * @start: the minimum id (inclusive, < 0x8000000)
1116 * @end: the maximum id (exclusive, < 0x8000000 or 0)
1117 * @gfp_mask: memory allocation flags
1118 *
1119 * Allocates an id in the range start <= id < end, or returns -ENOSPC.
1120 * On memory allocation failure, returns -ENOMEM.
1121 *
1122 * Use ida_simple_remove() to get rid of an id.
1123 */
1124int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
1125 gfp_t gfp_mask)
1126{
1127 int ret, id;
1128 unsigned int max;
Tejun Heo46cbc1d2011-11-02 13:38:46 -07001129 unsigned long flags;
Rusty Russell88eca022011-08-03 16:21:06 -07001130
1131 BUG_ON((int)start < 0);
1132 BUG_ON((int)end < 0);
1133
1134 if (end == 0)
1135 max = 0x80000000;
1136 else {
1137 BUG_ON(end < start);
1138 max = end - 1;
1139 }
1140
1141again:
1142 if (!ida_pre_get(ida, gfp_mask))
1143 return -ENOMEM;
1144
Tejun Heo46cbc1d2011-11-02 13:38:46 -07001145 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -07001146 ret = ida_get_new_above(ida, start, &id);
1147 if (!ret) {
1148 if (id > max) {
1149 ida_remove(ida, id);
1150 ret = -ENOSPC;
1151 } else {
1152 ret = id;
1153 }
1154 }
Tejun Heo46cbc1d2011-11-02 13:38:46 -07001155 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -07001156
1157 if (unlikely(ret == -EAGAIN))
1158 goto again;
1159
1160 return ret;
1161}
1162EXPORT_SYMBOL(ida_simple_get);
1163
1164/**
1165 * ida_simple_remove - remove an allocated id.
1166 * @ida: the (initialized) ida.
1167 * @id: the id returned by ida_simple_get.
1168 */
1169void ida_simple_remove(struct ida *ida, unsigned int id)
1170{
Tejun Heo46cbc1d2011-11-02 13:38:46 -07001171 unsigned long flags;
1172
Rusty Russell88eca022011-08-03 16:21:06 -07001173 BUG_ON((int)id < 0);
Tejun Heo46cbc1d2011-11-02 13:38:46 -07001174 spin_lock_irqsave(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -07001175 ida_remove(ida, id);
Tejun Heo46cbc1d2011-11-02 13:38:46 -07001176 spin_unlock_irqrestore(&simple_ida_lock, flags);
Rusty Russell88eca022011-08-03 16:21:06 -07001177}
1178EXPORT_SYMBOL(ida_simple_remove);
1179
1180/**
Tejun Heo72dba582007-06-14 03:45:13 +09001181 * ida_init - initialize ida handle
1182 * @ida: ida handle
1183 *
1184 * This function is use to set up the handle (@ida) that you will pass
1185 * to the rest of the functions.
1186 */
1187void ida_init(struct ida *ida)
1188{
1189 memset(ida, 0, sizeof(struct ida));
1190 idr_init(&ida->idr);
1191
1192}
1193EXPORT_SYMBOL(ida_init);