xref: /openbmc/linux/lib/idr.c (revision b93804b2)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * 2002-10-18  written by Jim Houston jim.houston@ccur.com
31da177e4SLinus Torvalds  *	Copyright (C) 2002 by Concurrent Computer Corporation
41da177e4SLinus Torvalds  *	Distributed under the GNU GPL license version 2.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Modified by George Anzinger to reuse immediately and to use
71da177e4SLinus Torvalds  * find bit instructions.  Also removed _irq on spinlocks.
81da177e4SLinus Torvalds  *
93219b3b7SNadia Derbey  * Modified by Nadia Derbey to make it RCU safe.
103219b3b7SNadia Derbey  *
111da177e4SLinus Torvalds  * Small id to pointer translation service.
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * It uses a radix tree like structure as a sparse array indexed
141da177e4SLinus Torvalds  * by the id to obtain the pointer.  The bitmap makes allocating
151da177e4SLinus Torvalds  * a new id quick.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * You call it to allocate an id (an int) an associate with that id a
181da177e4SLinus Torvalds  * pointer or what ever, we treat it as a (void *).  You can pass this
191da177e4SLinus Torvalds  * id to a user for him to pass back at a later time.  You then pass
201da177e4SLinus Torvalds  * that id to this code and it returns your pointer.
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds  * You can release ids at any time. When all ids are released, most of
23125c4c70SFengguang Wu  * the memory is returned (we keep MAX_IDR_FREE) in a local pool so we
241da177e4SLinus Torvalds  * don't need to go to the memory "store" during an id allocate, just
251da177e4SLinus Torvalds  * so you don't need to be too concerned about locking and conflicts
261da177e4SLinus Torvalds  * with the slab allocator.
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #ifndef TEST                        // to test in user space...
301da177e4SLinus Torvalds #include <linux/slab.h>
311da177e4SLinus Torvalds #include <linux/init.h>
328bc3bcc9SPaul Gortmaker #include <linux/export.h>
331da177e4SLinus Torvalds #endif
345806f07cSJeff Mahoney #include <linux/err.h>
351da177e4SLinus Torvalds #include <linux/string.h>
361da177e4SLinus Torvalds #include <linux/idr.h>
3788eca020SRusty Russell #include <linux/spinlock.h>
38d5c7409fSTejun Heo #include <linux/percpu.h>
39d5c7409fSTejun Heo #include <linux/hardirq.h>
401da177e4SLinus Torvalds 
41e8c8d1bcSTejun Heo #define MAX_IDR_SHIFT		(sizeof(int) * 8 - 1)
42e8c8d1bcSTejun Heo #define MAX_IDR_BIT		(1U << MAX_IDR_SHIFT)
43e8c8d1bcSTejun Heo 
44e8c8d1bcSTejun Heo /* Leave the possibility of an incomplete final layer */
45e8c8d1bcSTejun Heo #define MAX_IDR_LEVEL ((MAX_IDR_SHIFT + IDR_BITS - 1) / IDR_BITS)
46e8c8d1bcSTejun Heo 
47e8c8d1bcSTejun Heo /* Number of id_layer structs to leave in free list */
48e8c8d1bcSTejun Heo #define MAX_IDR_FREE (MAX_IDR_LEVEL * 2)
49e8c8d1bcSTejun Heo 
50e18b890bSChristoph Lameter static struct kmem_cache *idr_layer_cache;
51d5c7409fSTejun Heo static DEFINE_PER_CPU(struct idr_layer *, idr_preload_head);
52d5c7409fSTejun Heo static DEFINE_PER_CPU(int, idr_preload_cnt);
5388eca020SRusty Russell static DEFINE_SPINLOCK(simple_ida_lock);
541da177e4SLinus Torvalds 
55326cf0f0STejun Heo /* the maximum ID which can be allocated given idr->layers */
56326cf0f0STejun Heo static int idr_max(int layers)
57326cf0f0STejun Heo {
58326cf0f0STejun Heo 	int bits = min_t(int, layers * IDR_BITS, MAX_IDR_SHIFT);
59326cf0f0STejun Heo 
60326cf0f0STejun Heo 	return (1 << bits) - 1;
61326cf0f0STejun Heo }
62326cf0f0STejun Heo 
6354616283STejun Heo /*
6454616283STejun Heo  * Prefix mask for an idr_layer at @layer.  For layer 0, the prefix mask is
6554616283STejun Heo  * all bits except for the lower IDR_BITS.  For layer 1, 2 * IDR_BITS, and
6654616283STejun Heo  * so on.
6754616283STejun Heo  */
6854616283STejun Heo static int idr_layer_prefix_mask(int layer)
6954616283STejun Heo {
7054616283STejun Heo 	return ~idr_max(layer + 1);
7154616283STejun Heo }
7254616283STejun Heo 
734ae53789SNadia Derbey static struct idr_layer *get_from_free_list(struct idr *idp)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds 	struct idr_layer *p;
76c259cc28SRoland Dreier 	unsigned long flags;
771da177e4SLinus Torvalds 
78c259cc28SRoland Dreier 	spin_lock_irqsave(&idp->lock, flags);
791da177e4SLinus Torvalds 	if ((p = idp->id_free)) {
801da177e4SLinus Torvalds 		idp->id_free = p->ary[0];
811da177e4SLinus Torvalds 		idp->id_free_cnt--;
821da177e4SLinus Torvalds 		p->ary[0] = NULL;
831da177e4SLinus Torvalds 	}
84c259cc28SRoland Dreier 	spin_unlock_irqrestore(&idp->lock, flags);
851da177e4SLinus Torvalds 	return(p);
861da177e4SLinus Torvalds }
871da177e4SLinus Torvalds 
88d5c7409fSTejun Heo /**
89d5c7409fSTejun Heo  * idr_layer_alloc - allocate a new idr_layer
90d5c7409fSTejun Heo  * @gfp_mask: allocation mask
91d5c7409fSTejun Heo  * @layer_idr: optional idr to allocate from
92d5c7409fSTejun Heo  *
93d5c7409fSTejun Heo  * If @layer_idr is %NULL, directly allocate one using @gfp_mask or fetch
94d5c7409fSTejun Heo  * one from the per-cpu preload buffer.  If @layer_idr is not %NULL, fetch
95d5c7409fSTejun Heo  * an idr_layer from @idr->id_free.
96d5c7409fSTejun Heo  *
97d5c7409fSTejun Heo  * @layer_idr is to maintain backward compatibility with the old alloc
98d5c7409fSTejun Heo  * interface - idr_pre_get() and idr_get_new*() - and will be removed
99d5c7409fSTejun Heo  * together with per-pool preload buffer.
100d5c7409fSTejun Heo  */
101d5c7409fSTejun Heo static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
102d5c7409fSTejun Heo {
103d5c7409fSTejun Heo 	struct idr_layer *new;
104d5c7409fSTejun Heo 
105d5c7409fSTejun Heo 	/* this is the old path, bypass to get_from_free_list() */
106d5c7409fSTejun Heo 	if (layer_idr)
107d5c7409fSTejun Heo 		return get_from_free_list(layer_idr);
108d5c7409fSTejun Heo 
10959bfbcf0STejun Heo 	/*
11059bfbcf0STejun Heo 	 * Try to allocate directly from kmem_cache.  We want to try this
11159bfbcf0STejun Heo 	 * before preload buffer; otherwise, non-preloading idr_alloc()
11259bfbcf0STejun Heo 	 * users will end up taking advantage of preloading ones.  As the
11359bfbcf0STejun Heo 	 * following is allowed to fail for preloaded cases, suppress
11459bfbcf0STejun Heo 	 * warning this time.
11559bfbcf0STejun Heo 	 */
11659bfbcf0STejun Heo 	new = kmem_cache_zalloc(idr_layer_cache, gfp_mask | __GFP_NOWARN);
117d5c7409fSTejun Heo 	if (new)
118d5c7409fSTejun Heo 		return new;
119d5c7409fSTejun Heo 
120d5c7409fSTejun Heo 	/*
121d5c7409fSTejun Heo 	 * Try to fetch one from the per-cpu preload buffer if in process
122d5c7409fSTejun Heo 	 * context.  See idr_preload() for details.
123d5c7409fSTejun Heo 	 */
12459bfbcf0STejun Heo 	if (!in_interrupt()) {
125d5c7409fSTejun Heo 		preempt_disable();
126d5c7409fSTejun Heo 		new = __this_cpu_read(idr_preload_head);
127d5c7409fSTejun Heo 		if (new) {
128d5c7409fSTejun Heo 			__this_cpu_write(idr_preload_head, new->ary[0]);
129d5c7409fSTejun Heo 			__this_cpu_dec(idr_preload_cnt);
130d5c7409fSTejun Heo 			new->ary[0] = NULL;
131d5c7409fSTejun Heo 		}
132d5c7409fSTejun Heo 		preempt_enable();
13359bfbcf0STejun Heo 		if (new)
134d5c7409fSTejun Heo 			return new;
135d5c7409fSTejun Heo 	}
136d5c7409fSTejun Heo 
13759bfbcf0STejun Heo 	/*
13859bfbcf0STejun Heo 	 * Both failed.  Try kmem_cache again w/o adding __GFP_NOWARN so
13959bfbcf0STejun Heo 	 * that memory allocation failure warning is printed as intended.
14059bfbcf0STejun Heo 	 */
14159bfbcf0STejun Heo 	return kmem_cache_zalloc(idr_layer_cache, gfp_mask);
14259bfbcf0STejun Heo }
14359bfbcf0STejun Heo 
144cf481c20SNadia Derbey static void idr_layer_rcu_free(struct rcu_head *head)
145cf481c20SNadia Derbey {
146cf481c20SNadia Derbey 	struct idr_layer *layer;
147cf481c20SNadia Derbey 
148cf481c20SNadia Derbey 	layer = container_of(head, struct idr_layer, rcu_head);
149cf481c20SNadia Derbey 	kmem_cache_free(idr_layer_cache, layer);
150cf481c20SNadia Derbey }
151cf481c20SNadia Derbey 
1520ffc2a9cSTejun Heo static inline void free_layer(struct idr *idr, struct idr_layer *p)
153cf481c20SNadia Derbey {
1540ffc2a9cSTejun Heo 	if (idr->hint && idr->hint == p)
1550ffc2a9cSTejun Heo 		RCU_INIT_POINTER(idr->hint, NULL);
156cf481c20SNadia Derbey 	call_rcu(&p->rcu_head, idr_layer_rcu_free);
157cf481c20SNadia Derbey }
158cf481c20SNadia Derbey 
1591eec0056SSonny Rao /* only called when idp->lock is held */
1604ae53789SNadia Derbey static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
1611eec0056SSonny Rao {
1621eec0056SSonny Rao 	p->ary[0] = idp->id_free;
1631eec0056SSonny Rao 	idp->id_free = p;
1641eec0056SSonny Rao 	idp->id_free_cnt++;
1651eec0056SSonny Rao }
1661eec0056SSonny Rao 
1674ae53789SNadia Derbey static void move_to_free_list(struct idr *idp, struct idr_layer *p)
1681da177e4SLinus Torvalds {
169c259cc28SRoland Dreier 	unsigned long flags;
170c259cc28SRoland Dreier 
1711da177e4SLinus Torvalds 	/*
1721da177e4SLinus Torvalds 	 * Depends on the return element being zeroed.
1731da177e4SLinus Torvalds 	 */
174c259cc28SRoland Dreier 	spin_lock_irqsave(&idp->lock, flags);
1754ae53789SNadia Derbey 	__move_to_free_list(idp, p);
176c259cc28SRoland Dreier 	spin_unlock_irqrestore(&idp->lock, flags);
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds 
179e33ac8bdSTejun Heo static void idr_mark_full(struct idr_layer **pa, int id)
180e33ac8bdSTejun Heo {
181e33ac8bdSTejun Heo 	struct idr_layer *p = pa[0];
182e33ac8bdSTejun Heo 	int l = 0;
183e33ac8bdSTejun Heo 
1841d9b2e1eSTejun Heo 	__set_bit(id & IDR_MASK, p->bitmap);
185e33ac8bdSTejun Heo 	/*
186e33ac8bdSTejun Heo 	 * If this layer is full mark the bit in the layer above to
187e33ac8bdSTejun Heo 	 * show that this part of the radix tree is full.  This may
188e33ac8bdSTejun Heo 	 * complete the layer above and require walking up the radix
189e33ac8bdSTejun Heo 	 * tree.
190e33ac8bdSTejun Heo 	 */
1911d9b2e1eSTejun Heo 	while (bitmap_full(p->bitmap, IDR_SIZE)) {
192e33ac8bdSTejun Heo 		if (!(p = pa[++l]))
193e33ac8bdSTejun Heo 			break;
194e33ac8bdSTejun Heo 		id = id >> IDR_BITS;
1951d9b2e1eSTejun Heo 		__set_bit((id & IDR_MASK), p->bitmap);
196e33ac8bdSTejun Heo 	}
197e33ac8bdSTejun Heo }
198e33ac8bdSTejun Heo 
19990ae3ae5SStephen Hemminger static int __idr_pre_get(struct idr *idp, gfp_t gfp_mask)
2001da177e4SLinus Torvalds {
201125c4c70SFengguang Wu 	while (idp->id_free_cnt < MAX_IDR_FREE) {
2021da177e4SLinus Torvalds 		struct idr_layer *new;
2035b019e99SAndrew Morton 		new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
2041da177e4SLinus Torvalds 		if (new == NULL)
2051da177e4SLinus Torvalds 			return (0);
2064ae53789SNadia Derbey 		move_to_free_list(idp, new);
2071da177e4SLinus Torvalds 	}
2081da177e4SLinus Torvalds 	return 1;
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
21112d1b439STejun Heo /**
21212d1b439STejun Heo  * sub_alloc - try to allocate an id without growing the tree depth
21312d1b439STejun Heo  * @idp: idr handle
21412d1b439STejun Heo  * @starting_id: id to start search at
21512d1b439STejun Heo  * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer
216d5c7409fSTejun Heo  * @gfp_mask: allocation mask for idr_layer_alloc()
217d5c7409fSTejun Heo  * @layer_idr: optional idr passed to idr_layer_alloc()
21812d1b439STejun Heo  *
21912d1b439STejun Heo  * Allocate an id in range [@starting_id, INT_MAX] from @idp without
22012d1b439STejun Heo  * growing its depth.  Returns
22112d1b439STejun Heo  *
22212d1b439STejun Heo  *  the allocated id >= 0 if successful,
22312d1b439STejun Heo  *  -EAGAIN if the tree needs to grow for allocation to succeed,
22412d1b439STejun Heo  *  -ENOSPC if the id space is exhausted,
22512d1b439STejun Heo  *  -ENOMEM if more idr_layers need to be allocated.
22612d1b439STejun Heo  */
227d5c7409fSTejun Heo static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
228d5c7409fSTejun Heo 		     gfp_t gfp_mask, struct idr *layer_idr)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	int n, m, sh;
2311da177e4SLinus Torvalds 	struct idr_layer *p, *new;
2327aae6dd8STejun Heo 	int l, id, oid;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	id = *starting_id;
2357aae6dd8STejun Heo  restart:
2361da177e4SLinus Torvalds 	p = idp->top;
2371da177e4SLinus Torvalds 	l = idp->layers;
2381da177e4SLinus Torvalds 	pa[l--] = NULL;
2391da177e4SLinus Torvalds 	while (1) {
2401da177e4SLinus Torvalds 		/*
2411da177e4SLinus Torvalds 		 * We run around this while until we reach the leaf node...
2421da177e4SLinus Torvalds 		 */
2431da177e4SLinus Torvalds 		n = (id >> (IDR_BITS*l)) & IDR_MASK;
2441d9b2e1eSTejun Heo 		m = find_next_zero_bit(p->bitmap, IDR_SIZE, n);
2451da177e4SLinus Torvalds 		if (m == IDR_SIZE) {
2461da177e4SLinus Torvalds 			/* no space available go back to previous layer. */
2471da177e4SLinus Torvalds 			l++;
2487aae6dd8STejun Heo 			oid = id;
2491da177e4SLinus Torvalds 			id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
2507aae6dd8STejun Heo 
2517aae6dd8STejun Heo 			/* if already at the top layer, we need to grow */
2523afb69cbSLai Jiangshan 			if (id > idr_max(idp->layers)) {
2531da177e4SLinus Torvalds 				*starting_id = id;
25412d1b439STejun Heo 				return -EAGAIN;
2551da177e4SLinus Torvalds 			}
256d2e7276bSTejun Heo 			p = pa[l];
257d2e7276bSTejun Heo 			BUG_ON(!p);
2587aae6dd8STejun Heo 
2597aae6dd8STejun Heo 			/* If we need to go up one layer, continue the
2607aae6dd8STejun Heo 			 * loop; otherwise, restart from the top.
2617aae6dd8STejun Heo 			 */
2627aae6dd8STejun Heo 			sh = IDR_BITS * (l + 1);
2637aae6dd8STejun Heo 			if (oid >> sh == id >> sh)
2641da177e4SLinus Torvalds 				continue;
2657aae6dd8STejun Heo 			else
2667aae6dd8STejun Heo 				goto restart;
2671da177e4SLinus Torvalds 		}
2681da177e4SLinus Torvalds 		if (m != n) {
2691da177e4SLinus Torvalds 			sh = IDR_BITS*l;
2701da177e4SLinus Torvalds 			id = ((id >> sh) ^ n ^ m) << sh;
2711da177e4SLinus Torvalds 		}
272125c4c70SFengguang Wu 		if ((id >= MAX_IDR_BIT) || (id < 0))
27312d1b439STejun Heo 			return -ENOSPC;
2741da177e4SLinus Torvalds 		if (l == 0)
2751da177e4SLinus Torvalds 			break;
2761da177e4SLinus Torvalds 		/*
2771da177e4SLinus Torvalds 		 * Create the layer below if it is missing.
2781da177e4SLinus Torvalds 		 */
2791da177e4SLinus Torvalds 		if (!p->ary[m]) {
280d5c7409fSTejun Heo 			new = idr_layer_alloc(gfp_mask, layer_idr);
2814ae53789SNadia Derbey 			if (!new)
28212d1b439STejun Heo 				return -ENOMEM;
2836ff2d39bSManfred Spraul 			new->layer = l-1;
28454616283STejun Heo 			new->prefix = id & idr_layer_prefix_mask(new->layer);
2853219b3b7SNadia Derbey 			rcu_assign_pointer(p->ary[m], new);
2861da177e4SLinus Torvalds 			p->count++;
2871da177e4SLinus Torvalds 		}
2881da177e4SLinus Torvalds 		pa[l--] = p;
2891da177e4SLinus Torvalds 		p = p->ary[m];
2901da177e4SLinus Torvalds 	}
291e33ac8bdSTejun Heo 
292e33ac8bdSTejun Heo 	pa[l] = p;
293e33ac8bdSTejun Heo 	return id;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
296e33ac8bdSTejun Heo static int idr_get_empty_slot(struct idr *idp, int starting_id,
297d5c7409fSTejun Heo 			      struct idr_layer **pa, gfp_t gfp_mask,
298d5c7409fSTejun Heo 			      struct idr *layer_idr)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct idr_layer *p, *new;
3011da177e4SLinus Torvalds 	int layers, v, id;
302c259cc28SRoland Dreier 	unsigned long flags;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	id = starting_id;
3051da177e4SLinus Torvalds build_up:
3061da177e4SLinus Torvalds 	p = idp->top;
3071da177e4SLinus Torvalds 	layers = idp->layers;
3081da177e4SLinus Torvalds 	if (unlikely(!p)) {
309d5c7409fSTejun Heo 		if (!(p = idr_layer_alloc(gfp_mask, layer_idr)))
31012d1b439STejun Heo 			return -ENOMEM;
3116ff2d39bSManfred Spraul 		p->layer = 0;
3121da177e4SLinus Torvalds 		layers = 1;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 	/*
3151da177e4SLinus Torvalds 	 * Add a new layer to the top of the tree if the requested
3161da177e4SLinus Torvalds 	 * id is larger than the currently allocated space.
3171da177e4SLinus Torvalds 	 */
318326cf0f0STejun Heo 	while (id > idr_max(layers)) {
3191da177e4SLinus Torvalds 		layers++;
320711a49a0SManfred Spraul 		if (!p->count) {
321711a49a0SManfred Spraul 			/* special case: if the tree is currently empty,
322711a49a0SManfred Spraul 			 * then we grow the tree by moving the top node
323711a49a0SManfred Spraul 			 * upwards.
324711a49a0SManfred Spraul 			 */
325711a49a0SManfred Spraul 			p->layer++;
32654616283STejun Heo 			WARN_ON_ONCE(p->prefix);
3271da177e4SLinus Torvalds 			continue;
328711a49a0SManfred Spraul 		}
329d5c7409fSTejun Heo 		if (!(new = idr_layer_alloc(gfp_mask, layer_idr))) {
3301da177e4SLinus Torvalds 			/*
3311da177e4SLinus Torvalds 			 * The allocation failed.  If we built part of
3321da177e4SLinus Torvalds 			 * the structure tear it down.
3331da177e4SLinus Torvalds 			 */
334c259cc28SRoland Dreier 			spin_lock_irqsave(&idp->lock, flags);
3351da177e4SLinus Torvalds 			for (new = p; p && p != idp->top; new = p) {
3361da177e4SLinus Torvalds 				p = p->ary[0];
3371da177e4SLinus Torvalds 				new->ary[0] = NULL;
3381d9b2e1eSTejun Heo 				new->count = 0;
3391d9b2e1eSTejun Heo 				bitmap_clear(new->bitmap, 0, IDR_SIZE);
3404ae53789SNadia Derbey 				__move_to_free_list(idp, new);
3411da177e4SLinus Torvalds 			}
342c259cc28SRoland Dreier 			spin_unlock_irqrestore(&idp->lock, flags);
34312d1b439STejun Heo 			return -ENOMEM;
3441da177e4SLinus Torvalds 		}
3451da177e4SLinus Torvalds 		new->ary[0] = p;
3461da177e4SLinus Torvalds 		new->count = 1;
3476ff2d39bSManfred Spraul 		new->layer = layers-1;
34854616283STejun Heo 		new->prefix = id & idr_layer_prefix_mask(new->layer);
3491d9b2e1eSTejun Heo 		if (bitmap_full(p->bitmap, IDR_SIZE))
3501d9b2e1eSTejun Heo 			__set_bit(0, new->bitmap);
3511da177e4SLinus Torvalds 		p = new;
3521da177e4SLinus Torvalds 	}
3533219b3b7SNadia Derbey 	rcu_assign_pointer(idp->top, p);
3541da177e4SLinus Torvalds 	idp->layers = layers;
355d5c7409fSTejun Heo 	v = sub_alloc(idp, &id, pa, gfp_mask, layer_idr);
35612d1b439STejun Heo 	if (v == -EAGAIN)
3571da177e4SLinus Torvalds 		goto build_up;
3581da177e4SLinus Torvalds 	return(v);
3591da177e4SLinus Torvalds }
3601da177e4SLinus Torvalds 
361e33ac8bdSTejun Heo /*
3623594eb28STejun Heo  * @id and @pa are from a successful allocation from idr_get_empty_slot().
3633594eb28STejun Heo  * Install the user pointer @ptr and mark the slot full.
364e33ac8bdSTejun Heo  */
3650ffc2a9cSTejun Heo static void idr_fill_slot(struct idr *idr, void *ptr, int id,
3660ffc2a9cSTejun Heo 			  struct idr_layer **pa)
3673594eb28STejun Heo {
3680ffc2a9cSTejun Heo 	/* update hint used for lookup, cleared from free_layer() */
3690ffc2a9cSTejun Heo 	rcu_assign_pointer(idr->hint, pa[0]);
3700ffc2a9cSTejun Heo 
3713594eb28STejun Heo 	rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
372e33ac8bdSTejun Heo 	pa[0]->count++;
373e33ac8bdSTejun Heo 	idr_mark_full(pa, id);
374e33ac8bdSTejun Heo }
375e33ac8bdSTejun Heo 
3761da177e4SLinus Torvalds 
377d5c7409fSTejun Heo /**
378d5c7409fSTejun Heo  * idr_preload - preload for idr_alloc()
379d5c7409fSTejun Heo  * @gfp_mask: allocation mask to use for preloading
380d5c7409fSTejun Heo  *
381d5c7409fSTejun Heo  * Preload per-cpu layer buffer for idr_alloc().  Can only be used from
382d5c7409fSTejun Heo  * process context and each idr_preload() invocation should be matched with
383d5c7409fSTejun Heo  * idr_preload_end().  Note that preemption is disabled while preloaded.
384d5c7409fSTejun Heo  *
385d5c7409fSTejun Heo  * The first idr_alloc() in the preloaded section can be treated as if it
386d5c7409fSTejun Heo  * were invoked with @gfp_mask used for preloading.  This allows using more
387d5c7409fSTejun Heo  * permissive allocation masks for idrs protected by spinlocks.
388d5c7409fSTejun Heo  *
389d5c7409fSTejun Heo  * For example, if idr_alloc() below fails, the failure can be treated as
390d5c7409fSTejun Heo  * if idr_alloc() were called with GFP_KERNEL rather than GFP_NOWAIT.
391d5c7409fSTejun Heo  *
392d5c7409fSTejun Heo  *	idr_preload(GFP_KERNEL);
393d5c7409fSTejun Heo  *	spin_lock(lock);
394d5c7409fSTejun Heo  *
395d5c7409fSTejun Heo  *	id = idr_alloc(idr, ptr, start, end, GFP_NOWAIT);
396d5c7409fSTejun Heo  *
397d5c7409fSTejun Heo  *	spin_unlock(lock);
398d5c7409fSTejun Heo  *	idr_preload_end();
399d5c7409fSTejun Heo  *	if (id < 0)
400d5c7409fSTejun Heo  *		error;
401d5c7409fSTejun Heo  */
402d5c7409fSTejun Heo void idr_preload(gfp_t gfp_mask)
403d5c7409fSTejun Heo {
404d5c7409fSTejun Heo 	/*
405d5c7409fSTejun Heo 	 * Consuming preload buffer from non-process context breaks preload
406d5c7409fSTejun Heo 	 * allocation guarantee.  Disallow usage from those contexts.
407d5c7409fSTejun Heo 	 */
408d5c7409fSTejun Heo 	WARN_ON_ONCE(in_interrupt());
409d5c7409fSTejun Heo 	might_sleep_if(gfp_mask & __GFP_WAIT);
410d5c7409fSTejun Heo 
411d5c7409fSTejun Heo 	preempt_disable();
412d5c7409fSTejun Heo 
413d5c7409fSTejun Heo 	/*
414d5c7409fSTejun Heo 	 * idr_alloc() is likely to succeed w/o full idr_layer buffer and
415d5c7409fSTejun Heo 	 * return value from idr_alloc() needs to be checked for failure
416d5c7409fSTejun Heo 	 * anyway.  Silently give up if allocation fails.  The caller can
417d5c7409fSTejun Heo 	 * treat failures from idr_alloc() as if idr_alloc() were called
418d5c7409fSTejun Heo 	 * with @gfp_mask which should be enough.
419d5c7409fSTejun Heo 	 */
420d5c7409fSTejun Heo 	while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
421d5c7409fSTejun Heo 		struct idr_layer *new;
422d5c7409fSTejun Heo 
423d5c7409fSTejun Heo 		preempt_enable();
424d5c7409fSTejun Heo 		new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
425d5c7409fSTejun Heo 		preempt_disable();
426d5c7409fSTejun Heo 		if (!new)
427d5c7409fSTejun Heo 			break;
428d5c7409fSTejun Heo 
429d5c7409fSTejun Heo 		/* link the new one to per-cpu preload list */
430d5c7409fSTejun Heo 		new->ary[0] = __this_cpu_read(idr_preload_head);
431d5c7409fSTejun Heo 		__this_cpu_write(idr_preload_head, new);
432d5c7409fSTejun Heo 		__this_cpu_inc(idr_preload_cnt);
433d5c7409fSTejun Heo 	}
434d5c7409fSTejun Heo }
435d5c7409fSTejun Heo EXPORT_SYMBOL(idr_preload);
436d5c7409fSTejun Heo 
437d5c7409fSTejun Heo /**
438d5c7409fSTejun Heo  * idr_alloc - allocate new idr entry
439d5c7409fSTejun Heo  * @idr: the (initialized) idr
440d5c7409fSTejun Heo  * @ptr: pointer to be associated with the new id
441d5c7409fSTejun Heo  * @start: the minimum id (inclusive)
442d5c7409fSTejun Heo  * @end: the maximum id (exclusive, <= 0 for max)
443d5c7409fSTejun Heo  * @gfp_mask: memory allocation flags
444d5c7409fSTejun Heo  *
445d5c7409fSTejun Heo  * Allocate an id in [start, end) and associate it with @ptr.  If no ID is
446d5c7409fSTejun Heo  * available in the specified range, returns -ENOSPC.  On memory allocation
447d5c7409fSTejun Heo  * failure, returns -ENOMEM.
448d5c7409fSTejun Heo  *
449d5c7409fSTejun Heo  * Note that @end is treated as max when <= 0.  This is to always allow
450d5c7409fSTejun Heo  * using @start + N as @end as long as N is inside integer range.
451d5c7409fSTejun Heo  *
452d5c7409fSTejun Heo  * The user is responsible for exclusively synchronizing all operations
453d5c7409fSTejun Heo  * which may modify @idr.  However, read-only accesses such as idr_find()
454d5c7409fSTejun Heo  * or iteration can be performed under RCU read lock provided the user
455d5c7409fSTejun Heo  * destroys @ptr in RCU-safe way after removal from idr.
456d5c7409fSTejun Heo  */
457d5c7409fSTejun Heo int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
458d5c7409fSTejun Heo {
459d5c7409fSTejun Heo 	int max = end > 0 ? end - 1 : INT_MAX;	/* inclusive upper limit */
460326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
461d5c7409fSTejun Heo 	int id;
462d5c7409fSTejun Heo 
463d5c7409fSTejun Heo 	might_sleep_if(gfp_mask & __GFP_WAIT);
464d5c7409fSTejun Heo 
465d5c7409fSTejun Heo 	/* sanity checks */
466d5c7409fSTejun Heo 	if (WARN_ON_ONCE(start < 0))
467d5c7409fSTejun Heo 		return -EINVAL;
468d5c7409fSTejun Heo 	if (unlikely(max < start))
469d5c7409fSTejun Heo 		return -ENOSPC;
470d5c7409fSTejun Heo 
471d5c7409fSTejun Heo 	/* allocate id */
472d5c7409fSTejun Heo 	id = idr_get_empty_slot(idr, start, pa, gfp_mask, NULL);
473d5c7409fSTejun Heo 	if (unlikely(id < 0))
474d5c7409fSTejun Heo 		return id;
475d5c7409fSTejun Heo 	if (unlikely(id > max))
476d5c7409fSTejun Heo 		return -ENOSPC;
477d5c7409fSTejun Heo 
4780ffc2a9cSTejun Heo 	idr_fill_slot(idr, ptr, id, pa);
479d5c7409fSTejun Heo 	return id;
480d5c7409fSTejun Heo }
481d5c7409fSTejun Heo EXPORT_SYMBOL_GPL(idr_alloc);
482d5c7409fSTejun Heo 
4833e6628c4SJeff Layton /**
4843e6628c4SJeff Layton  * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
4853e6628c4SJeff Layton  * @idr: the (initialized) idr
4863e6628c4SJeff Layton  * @ptr: pointer to be associated with the new id
4873e6628c4SJeff Layton  * @start: the minimum id (inclusive)
4883e6628c4SJeff Layton  * @end: the maximum id (exclusive, <= 0 for max)
4893e6628c4SJeff Layton  * @gfp_mask: memory allocation flags
4903e6628c4SJeff Layton  *
4913e6628c4SJeff Layton  * Essentially the same as idr_alloc, but prefers to allocate progressively
4923e6628c4SJeff Layton  * higher ids if it can. If the "cur" counter wraps, then it will start again
4933e6628c4SJeff Layton  * at the "start" end of the range and allocate one that has already been used.
4943e6628c4SJeff Layton  */
4953e6628c4SJeff Layton int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end,
4963e6628c4SJeff Layton 			gfp_t gfp_mask)
4973e6628c4SJeff Layton {
4983e6628c4SJeff Layton 	int id;
4993e6628c4SJeff Layton 
5003e6628c4SJeff Layton 	id = idr_alloc(idr, ptr, max(start, idr->cur), end, gfp_mask);
5013e6628c4SJeff Layton 	if (id == -ENOSPC)
5023e6628c4SJeff Layton 		id = idr_alloc(idr, ptr, start, end, gfp_mask);
5033e6628c4SJeff Layton 
5043e6628c4SJeff Layton 	if (likely(id >= 0))
5053e6628c4SJeff Layton 		idr->cur = id + 1;
5063e6628c4SJeff Layton 	return id;
5073e6628c4SJeff Layton }
5083e6628c4SJeff Layton EXPORT_SYMBOL(idr_alloc_cyclic);
5093e6628c4SJeff Layton 
5101da177e4SLinus Torvalds static void idr_remove_warning(int id)
5111da177e4SLinus Torvalds {
512dd04b452SJean Delvare 	WARN(1, "idr_remove called for id=%d which is not allocated.\n", id);
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds static void sub_remove(struct idr *idp, int shift, int id)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	struct idr_layer *p = idp->top;
518326cf0f0STejun Heo 	struct idr_layer **pa[MAX_IDR_LEVEL + 1];
5191da177e4SLinus Torvalds 	struct idr_layer ***paa = &pa[0];
520cf481c20SNadia Derbey 	struct idr_layer *to_free;
5211da177e4SLinus Torvalds 	int n;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	*paa = NULL;
5241da177e4SLinus Torvalds 	*++paa = &idp->top;
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds 	while ((shift > 0) && p) {
5271da177e4SLinus Torvalds 		n = (id >> shift) & IDR_MASK;
5281d9b2e1eSTejun Heo 		__clear_bit(n, p->bitmap);
5291da177e4SLinus Torvalds 		*++paa = &p->ary[n];
5301da177e4SLinus Torvalds 		p = p->ary[n];
5311da177e4SLinus Torvalds 		shift -= IDR_BITS;
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds 	n = id & IDR_MASK;
5341d9b2e1eSTejun Heo 	if (likely(p != NULL && test_bit(n, p->bitmap))) {
5351d9b2e1eSTejun Heo 		__clear_bit(n, p->bitmap);
5363f59b067SMonam Agarwal 		RCU_INIT_POINTER(p->ary[n], NULL);
537cf481c20SNadia Derbey 		to_free = NULL;
5381da177e4SLinus Torvalds 		while(*paa && ! --((**paa)->count)){
539cf481c20SNadia Derbey 			if (to_free)
5400ffc2a9cSTejun Heo 				free_layer(idp, to_free);
541cf481c20SNadia Derbey 			to_free = **paa;
5421da177e4SLinus Torvalds 			**paa-- = NULL;
5431da177e4SLinus Torvalds 		}
5441da177e4SLinus Torvalds 		if (!*paa)
5451da177e4SLinus Torvalds 			idp->layers = 0;
546cf481c20SNadia Derbey 		if (to_free)
5470ffc2a9cSTejun Heo 			free_layer(idp, to_free);
548e15ae2ddSJesper Juhl 	} else
5491da177e4SLinus Torvalds 		idr_remove_warning(id);
5501da177e4SLinus Torvalds }
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds /**
55356083ab1SRandy Dunlap  * idr_remove - remove the given id and free its slot
55472fd4a35SRobert P. J. Day  * @idp: idr handle
55572fd4a35SRobert P. J. Day  * @id: unique key
5561da177e4SLinus Torvalds  */
5571da177e4SLinus Torvalds void idr_remove(struct idr *idp, int id)
5581da177e4SLinus Torvalds {
5591da177e4SLinus Torvalds 	struct idr_layer *p;
560cf481c20SNadia Derbey 	struct idr_layer *to_free;
5611da177e4SLinus Torvalds 
5622e1c9b28STejun Heo 	if (id < 0)
563e8c8d1bcSTejun Heo 		return;
5641da177e4SLinus Torvalds 
5658f9f665aSLai Jiangshan 	if (id > idr_max(idp->layers)) {
5668f9f665aSLai Jiangshan 		idr_remove_warning(id);
5678f9f665aSLai Jiangshan 		return;
5688f9f665aSLai Jiangshan 	}
5698f9f665aSLai Jiangshan 
5701da177e4SLinus Torvalds 	sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
571e15ae2ddSJesper Juhl 	if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
572cf481c20SNadia Derbey 	    idp->top->ary[0]) {
573cf481c20SNadia Derbey 		/*
574cf481c20SNadia Derbey 		 * Single child at leftmost slot: we can shrink the tree.
575cf481c20SNadia Derbey 		 * This level is not needed anymore since when layers are
576cf481c20SNadia Derbey 		 * inserted, they are inserted at the top of the existing
577cf481c20SNadia Derbey 		 * tree.
578cf481c20SNadia Derbey 		 */
579cf481c20SNadia Derbey 		to_free = idp->top;
5801da177e4SLinus Torvalds 		p = idp->top->ary[0];
581cf481c20SNadia Derbey 		rcu_assign_pointer(idp->top, p);
5821da177e4SLinus Torvalds 		--idp->layers;
5831d9b2e1eSTejun Heo 		to_free->count = 0;
5841d9b2e1eSTejun Heo 		bitmap_clear(to_free->bitmap, 0, IDR_SIZE);
5850ffc2a9cSTejun Heo 		free_layer(idp, to_free);
5861da177e4SLinus Torvalds 	}
587125c4c70SFengguang Wu 	while (idp->id_free_cnt >= MAX_IDR_FREE) {
5884ae53789SNadia Derbey 		p = get_from_free_list(idp);
589cf481c20SNadia Derbey 		/*
590cf481c20SNadia Derbey 		 * Note: we don't call the rcu callback here, since the only
591cf481c20SNadia Derbey 		 * layers that fall into the freelist are those that have been
592cf481c20SNadia Derbey 		 * preallocated.
593cf481c20SNadia Derbey 		 */
5941da177e4SLinus Torvalds 		kmem_cache_free(idr_layer_cache, p);
5951da177e4SLinus Torvalds 	}
596af8e2a4cSNadia Derbey 	return;
5971da177e4SLinus Torvalds }
5981da177e4SLinus Torvalds EXPORT_SYMBOL(idr_remove);
5991da177e4SLinus Torvalds 
60090ae3ae5SStephen Hemminger static void __idr_remove_all(struct idr *idp)
60123936cc0SKristian Hoegsberg {
6026ace06dcSOleg Nesterov 	int n, id, max;
6032dcb22b3SImre Deak 	int bt_mask;
60423936cc0SKristian Hoegsberg 	struct idr_layer *p;
605326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
60623936cc0SKristian Hoegsberg 	struct idr_layer **paa = &pa[0];
60723936cc0SKristian Hoegsberg 
60823936cc0SKristian Hoegsberg 	n = idp->layers * IDR_BITS;
60923936cc0SKristian Hoegsberg 	p = idp->top;
6103f59b067SMonam Agarwal 	RCU_INIT_POINTER(idp->top, NULL);
611326cf0f0STejun Heo 	max = idr_max(idp->layers);
61223936cc0SKristian Hoegsberg 
61323936cc0SKristian Hoegsberg 	id = 0;
614326cf0f0STejun Heo 	while (id >= 0 && id <= max) {
61523936cc0SKristian Hoegsberg 		while (n > IDR_BITS && p) {
61623936cc0SKristian Hoegsberg 			n -= IDR_BITS;
61723936cc0SKristian Hoegsberg 			*paa++ = p;
61823936cc0SKristian Hoegsberg 			p = p->ary[(id >> n) & IDR_MASK];
61923936cc0SKristian Hoegsberg 		}
62023936cc0SKristian Hoegsberg 
6212dcb22b3SImre Deak 		bt_mask = id;
62223936cc0SKristian Hoegsberg 		id += 1 << n;
6232dcb22b3SImre Deak 		/* Get the highest bit that the above add changed from 0->1. */
6242dcb22b3SImre Deak 		while (n < fls(id ^ bt_mask)) {
625cf481c20SNadia Derbey 			if (p)
6260ffc2a9cSTejun Heo 				free_layer(idp, p);
62723936cc0SKristian Hoegsberg 			n += IDR_BITS;
62823936cc0SKristian Hoegsberg 			p = *--paa;
62923936cc0SKristian Hoegsberg 		}
63023936cc0SKristian Hoegsberg 	}
63123936cc0SKristian Hoegsberg 	idp->layers = 0;
63223936cc0SKristian Hoegsberg }
63323936cc0SKristian Hoegsberg 
63423936cc0SKristian Hoegsberg /**
6358d3b3591SAndrew Morton  * idr_destroy - release all cached layers within an idr tree
636ea24ea85SNaohiro Aota  * @idp: idr handle
6379bb26bc1STejun Heo  *
6389bb26bc1STejun Heo  * Free all id mappings and all idp_layers.  After this function, @idp is
6399bb26bc1STejun Heo  * completely unused and can be freed / recycled.  The caller is
6409bb26bc1STejun Heo  * responsible for ensuring that no one else accesses @idp during or after
6419bb26bc1STejun Heo  * idr_destroy().
6429bb26bc1STejun Heo  *
6439bb26bc1STejun Heo  * A typical clean-up sequence for objects stored in an idr tree will use
6449bb26bc1STejun Heo  * idr_for_each() to free all objects, if necessay, then idr_destroy() to
6459bb26bc1STejun Heo  * free up the id mappings and cached idr_layers.
6468d3b3591SAndrew Morton  */
6478d3b3591SAndrew Morton void idr_destroy(struct idr *idp)
6488d3b3591SAndrew Morton {
649fe6e24ecSTejun Heo 	__idr_remove_all(idp);
6509bb26bc1STejun Heo 
6518d3b3591SAndrew Morton 	while (idp->id_free_cnt) {
6524ae53789SNadia Derbey 		struct idr_layer *p = get_from_free_list(idp);
6538d3b3591SAndrew Morton 		kmem_cache_free(idr_layer_cache, p);
6548d3b3591SAndrew Morton 	}
6558d3b3591SAndrew Morton }
6568d3b3591SAndrew Morton EXPORT_SYMBOL(idr_destroy);
6578d3b3591SAndrew Morton 
6580ffc2a9cSTejun Heo void *idr_find_slowpath(struct idr *idp, int id)
6591da177e4SLinus Torvalds {
6601da177e4SLinus Torvalds 	int n;
6611da177e4SLinus Torvalds 	struct idr_layer *p;
6621da177e4SLinus Torvalds 
6632e1c9b28STejun Heo 	if (id < 0)
664e8c8d1bcSTejun Heo 		return NULL;
665e8c8d1bcSTejun Heo 
66696be753aSPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
6676ff2d39bSManfred Spraul 	if (!p)
6686ff2d39bSManfred Spraul 		return NULL;
6696ff2d39bSManfred Spraul 	n = (p->layer+1) * IDR_BITS;
6701da177e4SLinus Torvalds 
671326cf0f0STejun Heo 	if (id > idr_max(p->layer + 1))
6721da177e4SLinus Torvalds 		return NULL;
6736ff2d39bSManfred Spraul 	BUG_ON(n == 0);
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	while (n > 0 && p) {
6761da177e4SLinus Torvalds 		n -= IDR_BITS;
6776ff2d39bSManfred Spraul 		BUG_ON(n != p->layer*IDR_BITS);
67896be753aSPaul E. McKenney 		p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
6791da177e4SLinus Torvalds 	}
6801da177e4SLinus Torvalds 	return((void *)p);
6811da177e4SLinus Torvalds }
6820ffc2a9cSTejun Heo EXPORT_SYMBOL(idr_find_slowpath);
6831da177e4SLinus Torvalds 
6845806f07cSJeff Mahoney /**
68596d7fa42SKristian Hoegsberg  * idr_for_each - iterate through all stored pointers
68696d7fa42SKristian Hoegsberg  * @idp: idr handle
68796d7fa42SKristian Hoegsberg  * @fn: function to be called for each pointer
68896d7fa42SKristian Hoegsberg  * @data: data passed back to callback function
68996d7fa42SKristian Hoegsberg  *
69096d7fa42SKristian Hoegsberg  * Iterate over the pointers registered with the given idr.  The
69196d7fa42SKristian Hoegsberg  * callback function will be called for each pointer currently
69296d7fa42SKristian Hoegsberg  * registered, passing the id, the pointer and the data pointer passed
69396d7fa42SKristian Hoegsberg  * to this function.  It is not safe to modify the idr tree while in
69496d7fa42SKristian Hoegsberg  * the callback, so functions such as idr_get_new and idr_remove are
69596d7fa42SKristian Hoegsberg  * not allowed.
69696d7fa42SKristian Hoegsberg  *
69796d7fa42SKristian Hoegsberg  * We check the return of @fn each time. If it returns anything other
69856083ab1SRandy Dunlap  * than %0, we break out and return that value.
69996d7fa42SKristian Hoegsberg  *
70096d7fa42SKristian Hoegsberg  * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
70196d7fa42SKristian Hoegsberg  */
70296d7fa42SKristian Hoegsberg int idr_for_each(struct idr *idp,
70396d7fa42SKristian Hoegsberg 		 int (*fn)(int id, void *p, void *data), void *data)
70496d7fa42SKristian Hoegsberg {
70596d7fa42SKristian Hoegsberg 	int n, id, max, error = 0;
70696d7fa42SKristian Hoegsberg 	struct idr_layer *p;
707326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
70896d7fa42SKristian Hoegsberg 	struct idr_layer **paa = &pa[0];
70996d7fa42SKristian Hoegsberg 
71096d7fa42SKristian Hoegsberg 	n = idp->layers * IDR_BITS;
71196be753aSPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
712326cf0f0STejun Heo 	max = idr_max(idp->layers);
71396d7fa42SKristian Hoegsberg 
71496d7fa42SKristian Hoegsberg 	id = 0;
715326cf0f0STejun Heo 	while (id >= 0 && id <= max) {
71696d7fa42SKristian Hoegsberg 		while (n > 0 && p) {
71796d7fa42SKristian Hoegsberg 			n -= IDR_BITS;
71896d7fa42SKristian Hoegsberg 			*paa++ = p;
71996be753aSPaul E. McKenney 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
72096d7fa42SKristian Hoegsberg 		}
72196d7fa42SKristian Hoegsberg 
72296d7fa42SKristian Hoegsberg 		if (p) {
72396d7fa42SKristian Hoegsberg 			error = fn(id, (void *)p, data);
72496d7fa42SKristian Hoegsberg 			if (error)
72596d7fa42SKristian Hoegsberg 				break;
72696d7fa42SKristian Hoegsberg 		}
72796d7fa42SKristian Hoegsberg 
72896d7fa42SKristian Hoegsberg 		id += 1 << n;
72996d7fa42SKristian Hoegsberg 		while (n < fls(id)) {
73096d7fa42SKristian Hoegsberg 			n += IDR_BITS;
73196d7fa42SKristian Hoegsberg 			p = *--paa;
73296d7fa42SKristian Hoegsberg 		}
73396d7fa42SKristian Hoegsberg 	}
73496d7fa42SKristian Hoegsberg 
73596d7fa42SKristian Hoegsberg 	return error;
73696d7fa42SKristian Hoegsberg }
73796d7fa42SKristian Hoegsberg EXPORT_SYMBOL(idr_for_each);
73896d7fa42SKristian Hoegsberg 
73996d7fa42SKristian Hoegsberg /**
74038460b48SKAMEZAWA Hiroyuki  * idr_get_next - lookup next object of id to given id.
74138460b48SKAMEZAWA Hiroyuki  * @idp: idr handle
742ea24ea85SNaohiro Aota  * @nextidp:  pointer to lookup key
74338460b48SKAMEZAWA Hiroyuki  *
74438460b48SKAMEZAWA Hiroyuki  * Returns pointer to registered object with id, which is next number to
7451458ce16SNaohiro Aota  * given id. After being looked up, *@nextidp will be updated for the next
7461458ce16SNaohiro Aota  * iteration.
7479f7de827SHugh Dickins  *
7489f7de827SHugh Dickins  * This function can be called under rcu_read_lock(), given that the leaf
7499f7de827SHugh Dickins  * pointers lifetimes are correctly managed.
75038460b48SKAMEZAWA Hiroyuki  */
75138460b48SKAMEZAWA Hiroyuki void *idr_get_next(struct idr *idp, int *nextidp)
75238460b48SKAMEZAWA Hiroyuki {
753326cf0f0STejun Heo 	struct idr_layer *p, *pa[MAX_IDR_LEVEL + 1];
75438460b48SKAMEZAWA Hiroyuki 	struct idr_layer **paa = &pa[0];
75538460b48SKAMEZAWA Hiroyuki 	int id = *nextidp;
75638460b48SKAMEZAWA Hiroyuki 	int n, max;
75738460b48SKAMEZAWA Hiroyuki 
75838460b48SKAMEZAWA Hiroyuki 	/* find first ent */
75994bfa3b6SPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
76038460b48SKAMEZAWA Hiroyuki 	if (!p)
76138460b48SKAMEZAWA Hiroyuki 		return NULL;
7629f7de827SHugh Dickins 	n = (p->layer + 1) * IDR_BITS;
763326cf0f0STejun Heo 	max = idr_max(p->layer + 1);
76438460b48SKAMEZAWA Hiroyuki 
765326cf0f0STejun Heo 	while (id >= 0 && id <= max) {
76638460b48SKAMEZAWA Hiroyuki 		while (n > 0 && p) {
76738460b48SKAMEZAWA Hiroyuki 			n -= IDR_BITS;
76838460b48SKAMEZAWA Hiroyuki 			*paa++ = p;
76994bfa3b6SPaul E. McKenney 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
77038460b48SKAMEZAWA Hiroyuki 		}
77138460b48SKAMEZAWA Hiroyuki 
77238460b48SKAMEZAWA Hiroyuki 		if (p) {
77338460b48SKAMEZAWA Hiroyuki 			*nextidp = id;
77438460b48SKAMEZAWA Hiroyuki 			return p;
77538460b48SKAMEZAWA Hiroyuki 		}
77638460b48SKAMEZAWA Hiroyuki 
7776cdae741STejun Heo 		/*
7786cdae741STejun Heo 		 * Proceed to the next layer at the current level.  Unlike
7796cdae741STejun Heo 		 * idr_for_each(), @id isn't guaranteed to be aligned to
7806cdae741STejun Heo 		 * layer boundary at this point and adding 1 << n may
7816cdae741STejun Heo 		 * incorrectly skip IDs.  Make sure we jump to the
7826cdae741STejun Heo 		 * beginning of the next layer using round_up().
7836cdae741STejun Heo 		 */
7846cdae741STejun Heo 		id = round_up(id + 1, 1 << n);
78538460b48SKAMEZAWA Hiroyuki 		while (n < fls(id)) {
78638460b48SKAMEZAWA Hiroyuki 			n += IDR_BITS;
78738460b48SKAMEZAWA Hiroyuki 			p = *--paa;
78838460b48SKAMEZAWA Hiroyuki 		}
78938460b48SKAMEZAWA Hiroyuki 	}
79038460b48SKAMEZAWA Hiroyuki 	return NULL;
79138460b48SKAMEZAWA Hiroyuki }
7924d1ee80fSBen Hutchings EXPORT_SYMBOL(idr_get_next);
79338460b48SKAMEZAWA Hiroyuki 
79438460b48SKAMEZAWA Hiroyuki 
79538460b48SKAMEZAWA Hiroyuki /**
7965806f07cSJeff Mahoney  * idr_replace - replace pointer for given id
7975806f07cSJeff Mahoney  * @idp: idr handle
7985806f07cSJeff Mahoney  * @ptr: pointer you want associated with the id
7995806f07cSJeff Mahoney  * @id: lookup key
8005806f07cSJeff Mahoney  *
8015806f07cSJeff Mahoney  * Replace the pointer registered with an id and return the old value.
80256083ab1SRandy Dunlap  * A %-ENOENT return indicates that @id was not found.
80356083ab1SRandy Dunlap  * A %-EINVAL return indicates that @id was not within valid constraints.
8045806f07cSJeff Mahoney  *
805cf481c20SNadia Derbey  * The caller must serialize with writers.
8065806f07cSJeff Mahoney  */
8075806f07cSJeff Mahoney void *idr_replace(struct idr *idp, void *ptr, int id)
8085806f07cSJeff Mahoney {
8095806f07cSJeff Mahoney 	int n;
8105806f07cSJeff Mahoney 	struct idr_layer *p, *old_p;
8115806f07cSJeff Mahoney 
8122e1c9b28STejun Heo 	if (id < 0)
813e8c8d1bcSTejun Heo 		return ERR_PTR(-EINVAL);
814e8c8d1bcSTejun Heo 
8155806f07cSJeff Mahoney 	p = idp->top;
8166ff2d39bSManfred Spraul 	if (!p)
817b93804b2SLai Jiangshan 		return ERR_PTR(-ENOENT);
8186ff2d39bSManfred Spraul 
8193afb69cbSLai Jiangshan 	if (id > idr_max(p->layer + 1))
820b93804b2SLai Jiangshan 		return ERR_PTR(-ENOENT);
8215806f07cSJeff Mahoney 
8223afb69cbSLai Jiangshan 	n = p->layer * IDR_BITS;
8235806f07cSJeff Mahoney 	while ((n > 0) && p) {
8245806f07cSJeff Mahoney 		p = p->ary[(id >> n) & IDR_MASK];
8255806f07cSJeff Mahoney 		n -= IDR_BITS;
8265806f07cSJeff Mahoney 	}
8275806f07cSJeff Mahoney 
8285806f07cSJeff Mahoney 	n = id & IDR_MASK;
8291d9b2e1eSTejun Heo 	if (unlikely(p == NULL || !test_bit(n, p->bitmap)))
8305806f07cSJeff Mahoney 		return ERR_PTR(-ENOENT);
8315806f07cSJeff Mahoney 
8325806f07cSJeff Mahoney 	old_p = p->ary[n];
833cf481c20SNadia Derbey 	rcu_assign_pointer(p->ary[n], ptr);
8345806f07cSJeff Mahoney 
8355806f07cSJeff Mahoney 	return old_p;
8365806f07cSJeff Mahoney }
8375806f07cSJeff Mahoney EXPORT_SYMBOL(idr_replace);
8385806f07cSJeff Mahoney 
839199f0ca5SAkinobu Mita void __init idr_init_cache(void)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	idr_layer_cache = kmem_cache_create("idr_layer_cache",
8425b019e99SAndrew Morton 				sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds /**
8461da177e4SLinus Torvalds  * idr_init - initialize idr handle
8471da177e4SLinus Torvalds  * @idp:	idr handle
8481da177e4SLinus Torvalds  *
8491da177e4SLinus Torvalds  * This function is use to set up the handle (@idp) that you will pass
8501da177e4SLinus Torvalds  * to the rest of the functions.
8511da177e4SLinus Torvalds  */
8521da177e4SLinus Torvalds void idr_init(struct idr *idp)
8531da177e4SLinus Torvalds {
8541da177e4SLinus Torvalds 	memset(idp, 0, sizeof(struct idr));
8551da177e4SLinus Torvalds 	spin_lock_init(&idp->lock);
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds EXPORT_SYMBOL(idr_init);
85872dba584STejun Heo 
85905f7a7d6SAndreas Gruenbacher static int idr_has_entry(int id, void *p, void *data)
86005f7a7d6SAndreas Gruenbacher {
86105f7a7d6SAndreas Gruenbacher 	return 1;
86205f7a7d6SAndreas Gruenbacher }
86305f7a7d6SAndreas Gruenbacher 
86405f7a7d6SAndreas Gruenbacher bool idr_is_empty(struct idr *idp)
86505f7a7d6SAndreas Gruenbacher {
86605f7a7d6SAndreas Gruenbacher 	return !idr_for_each(idp, idr_has_entry, NULL);
86705f7a7d6SAndreas Gruenbacher }
86805f7a7d6SAndreas Gruenbacher EXPORT_SYMBOL(idr_is_empty);
86972dba584STejun Heo 
87056083ab1SRandy Dunlap /**
87156083ab1SRandy Dunlap  * DOC: IDA description
87272dba584STejun Heo  * IDA - IDR based ID allocator
87372dba584STejun Heo  *
87456083ab1SRandy Dunlap  * This is id allocator without id -> pointer translation.  Memory
87572dba584STejun Heo  * usage is much lower than full blown idr because each id only
87672dba584STejun Heo  * occupies a bit.  ida uses a custom leaf node which contains
87772dba584STejun Heo  * IDA_BITMAP_BITS slots.
87872dba584STejun Heo  *
87972dba584STejun Heo  * 2007-04-25  written by Tejun Heo <htejun@gmail.com>
88072dba584STejun Heo  */
88172dba584STejun Heo 
88272dba584STejun Heo static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
88372dba584STejun Heo {
88472dba584STejun Heo 	unsigned long flags;
88572dba584STejun Heo 
88672dba584STejun Heo 	if (!ida->free_bitmap) {
88772dba584STejun Heo 		spin_lock_irqsave(&ida->idr.lock, flags);
88872dba584STejun Heo 		if (!ida->free_bitmap) {
88972dba584STejun Heo 			ida->free_bitmap = bitmap;
89072dba584STejun Heo 			bitmap = NULL;
89172dba584STejun Heo 		}
89272dba584STejun Heo 		spin_unlock_irqrestore(&ida->idr.lock, flags);
89372dba584STejun Heo 	}
89472dba584STejun Heo 
89572dba584STejun Heo 	kfree(bitmap);
89672dba584STejun Heo }
89772dba584STejun Heo 
89872dba584STejun Heo /**
89972dba584STejun Heo  * ida_pre_get - reserve resources for ida allocation
90072dba584STejun Heo  * @ida:	ida handle
90172dba584STejun Heo  * @gfp_mask:	memory allocation flag
90272dba584STejun Heo  *
90372dba584STejun Heo  * This function should be called prior to locking and calling the
90472dba584STejun Heo  * following function.  It preallocates enough memory to satisfy the
90572dba584STejun Heo  * worst possible allocation.
90672dba584STejun Heo  *
90756083ab1SRandy Dunlap  * If the system is REALLY out of memory this function returns %0,
90856083ab1SRandy Dunlap  * otherwise %1.
90972dba584STejun Heo  */
91072dba584STejun Heo int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
91172dba584STejun Heo {
91272dba584STejun Heo 	/* allocate idr_layers */
913c8615d37STejun Heo 	if (!__idr_pre_get(&ida->idr, gfp_mask))
91472dba584STejun Heo 		return 0;
91572dba584STejun Heo 
91672dba584STejun Heo 	/* allocate free_bitmap */
91772dba584STejun Heo 	if (!ida->free_bitmap) {
91872dba584STejun Heo 		struct ida_bitmap *bitmap;
91972dba584STejun Heo 
92072dba584STejun Heo 		bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
92172dba584STejun Heo 		if (!bitmap)
92272dba584STejun Heo 			return 0;
92372dba584STejun Heo 
92472dba584STejun Heo 		free_bitmap(ida, bitmap);
92572dba584STejun Heo 	}
92672dba584STejun Heo 
92772dba584STejun Heo 	return 1;
92872dba584STejun Heo }
92972dba584STejun Heo EXPORT_SYMBOL(ida_pre_get);
93072dba584STejun Heo 
93172dba584STejun Heo /**
93272dba584STejun Heo  * ida_get_new_above - allocate new ID above or equal to a start id
93372dba584STejun Heo  * @ida:	ida handle
934ea24ea85SNaohiro Aota  * @starting_id: id to start search at
93572dba584STejun Heo  * @p_id:	pointer to the allocated handle
93672dba584STejun Heo  *
937e3816c54SWang Sheng-Hui  * Allocate new ID above or equal to @starting_id.  It should be called
938e3816c54SWang Sheng-Hui  * with any required locks.
93972dba584STejun Heo  *
94056083ab1SRandy Dunlap  * If memory is required, it will return %-EAGAIN, you should unlock
94172dba584STejun Heo  * and go back to the ida_pre_get() call.  If the ida is full, it will
94256083ab1SRandy Dunlap  * return %-ENOSPC.
94372dba584STejun Heo  *
94456083ab1SRandy Dunlap  * @p_id returns a value in the range @starting_id ... %0x7fffffff.
94572dba584STejun Heo  */
94672dba584STejun Heo int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
94772dba584STejun Heo {
948326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
94972dba584STejun Heo 	struct ida_bitmap *bitmap;
95072dba584STejun Heo 	unsigned long flags;
95172dba584STejun Heo 	int idr_id = starting_id / IDA_BITMAP_BITS;
95272dba584STejun Heo 	int offset = starting_id % IDA_BITMAP_BITS;
95372dba584STejun Heo 	int t, id;
95472dba584STejun Heo 
95572dba584STejun Heo  restart:
95672dba584STejun Heo 	/* get vacant slot */
957d5c7409fSTejun Heo 	t = idr_get_empty_slot(&ida->idr, idr_id, pa, 0, &ida->idr);
958944ca05cSNadia Derbey 	if (t < 0)
95912d1b439STejun Heo 		return t == -ENOMEM ? -EAGAIN : t;
96072dba584STejun Heo 
961125c4c70SFengguang Wu 	if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT)
96272dba584STejun Heo 		return -ENOSPC;
96372dba584STejun Heo 
96472dba584STejun Heo 	if (t != idr_id)
96572dba584STejun Heo 		offset = 0;
96672dba584STejun Heo 	idr_id = t;
96772dba584STejun Heo 
96872dba584STejun Heo 	/* if bitmap isn't there, create a new one */
96972dba584STejun Heo 	bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
97072dba584STejun Heo 	if (!bitmap) {
97172dba584STejun Heo 		spin_lock_irqsave(&ida->idr.lock, flags);
97272dba584STejun Heo 		bitmap = ida->free_bitmap;
97372dba584STejun Heo 		ida->free_bitmap = NULL;
97472dba584STejun Heo 		spin_unlock_irqrestore(&ida->idr.lock, flags);
97572dba584STejun Heo 
97672dba584STejun Heo 		if (!bitmap)
97772dba584STejun Heo 			return -EAGAIN;
97872dba584STejun Heo 
97972dba584STejun Heo 		memset(bitmap, 0, sizeof(struct ida_bitmap));
9803219b3b7SNadia Derbey 		rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
9813219b3b7SNadia Derbey 				(void *)bitmap);
98272dba584STejun Heo 		pa[0]->count++;
98372dba584STejun Heo 	}
98472dba584STejun Heo 
98572dba584STejun Heo 	/* lookup for empty slot */
98672dba584STejun Heo 	t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
98772dba584STejun Heo 	if (t == IDA_BITMAP_BITS) {
98872dba584STejun Heo 		/* no empty slot after offset, continue to the next chunk */
98972dba584STejun Heo 		idr_id++;
99072dba584STejun Heo 		offset = 0;
99172dba584STejun Heo 		goto restart;
99272dba584STejun Heo 	}
99372dba584STejun Heo 
99472dba584STejun Heo 	id = idr_id * IDA_BITMAP_BITS + t;
995125c4c70SFengguang Wu 	if (id >= MAX_IDR_BIT)
99672dba584STejun Heo 		return -ENOSPC;
99772dba584STejun Heo 
99872dba584STejun Heo 	__set_bit(t, bitmap->bitmap);
99972dba584STejun Heo 	if (++bitmap->nr_busy == IDA_BITMAP_BITS)
100072dba584STejun Heo 		idr_mark_full(pa, idr_id);
100172dba584STejun Heo 
100272dba584STejun Heo 	*p_id = id;
100372dba584STejun Heo 
100472dba584STejun Heo 	/* Each leaf node can handle nearly a thousand slots and the
100572dba584STejun Heo 	 * whole idea of ida is to have small memory foot print.
100672dba584STejun Heo 	 * Throw away extra resources one by one after each successful
100772dba584STejun Heo 	 * allocation.
100872dba584STejun Heo 	 */
100972dba584STejun Heo 	if (ida->idr.id_free_cnt || ida->free_bitmap) {
10104ae53789SNadia Derbey 		struct idr_layer *p = get_from_free_list(&ida->idr);
101172dba584STejun Heo 		if (p)
101272dba584STejun Heo 			kmem_cache_free(idr_layer_cache, p);
101372dba584STejun Heo 	}
101472dba584STejun Heo 
101572dba584STejun Heo 	return 0;
101672dba584STejun Heo }
101772dba584STejun Heo EXPORT_SYMBOL(ida_get_new_above);
101872dba584STejun Heo 
101972dba584STejun Heo /**
102072dba584STejun Heo  * ida_remove - remove the given ID
102172dba584STejun Heo  * @ida:	ida handle
102272dba584STejun Heo  * @id:		ID to free
102372dba584STejun Heo  */
102472dba584STejun Heo void ida_remove(struct ida *ida, int id)
102572dba584STejun Heo {
102672dba584STejun Heo 	struct idr_layer *p = ida->idr.top;
102772dba584STejun Heo 	int shift = (ida->idr.layers - 1) * IDR_BITS;
102872dba584STejun Heo 	int idr_id = id / IDA_BITMAP_BITS;
102972dba584STejun Heo 	int offset = id % IDA_BITMAP_BITS;
103072dba584STejun Heo 	int n;
103172dba584STejun Heo 	struct ida_bitmap *bitmap;
103272dba584STejun Heo 
10338f9f665aSLai Jiangshan 	if (idr_id > idr_max(ida->idr.layers))
10348f9f665aSLai Jiangshan 		goto err;
10358f9f665aSLai Jiangshan 
103672dba584STejun Heo 	/* clear full bits while looking up the leaf idr_layer */
103772dba584STejun Heo 	while ((shift > 0) && p) {
103872dba584STejun Heo 		n = (idr_id >> shift) & IDR_MASK;
10391d9b2e1eSTejun Heo 		__clear_bit(n, p->bitmap);
104072dba584STejun Heo 		p = p->ary[n];
104172dba584STejun Heo 		shift -= IDR_BITS;
104272dba584STejun Heo 	}
104372dba584STejun Heo 
104472dba584STejun Heo 	if (p == NULL)
104572dba584STejun Heo 		goto err;
104672dba584STejun Heo 
104772dba584STejun Heo 	n = idr_id & IDR_MASK;
10481d9b2e1eSTejun Heo 	__clear_bit(n, p->bitmap);
104972dba584STejun Heo 
105072dba584STejun Heo 	bitmap = (void *)p->ary[n];
1051aef0f62eSLai Jiangshan 	if (!bitmap || !test_bit(offset, bitmap->bitmap))
105272dba584STejun Heo 		goto err;
105372dba584STejun Heo 
105472dba584STejun Heo 	/* update bitmap and remove it if empty */
105572dba584STejun Heo 	__clear_bit(offset, bitmap->bitmap);
105672dba584STejun Heo 	if (--bitmap->nr_busy == 0) {
10571d9b2e1eSTejun Heo 		__set_bit(n, p->bitmap);	/* to please idr_remove() */
105872dba584STejun Heo 		idr_remove(&ida->idr, idr_id);
105972dba584STejun Heo 		free_bitmap(ida, bitmap);
106072dba584STejun Heo 	}
106172dba584STejun Heo 
106272dba584STejun Heo 	return;
106372dba584STejun Heo 
106472dba584STejun Heo  err:
1065dd04b452SJean Delvare 	WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
106672dba584STejun Heo }
106772dba584STejun Heo EXPORT_SYMBOL(ida_remove);
106872dba584STejun Heo 
106972dba584STejun Heo /**
107072dba584STejun Heo  * ida_destroy - release all cached layers within an ida tree
1071ea24ea85SNaohiro Aota  * @ida:		ida handle
107272dba584STejun Heo  */
107372dba584STejun Heo void ida_destroy(struct ida *ida)
107472dba584STejun Heo {
107572dba584STejun Heo 	idr_destroy(&ida->idr);
107672dba584STejun Heo 	kfree(ida->free_bitmap);
107772dba584STejun Heo }
107872dba584STejun Heo EXPORT_SYMBOL(ida_destroy);
107972dba584STejun Heo 
108072dba584STejun Heo /**
108188eca020SRusty Russell  * ida_simple_get - get a new id.
108288eca020SRusty Russell  * @ida: the (initialized) ida.
108388eca020SRusty Russell  * @start: the minimum id (inclusive, < 0x8000000)
108488eca020SRusty Russell  * @end: the maximum id (exclusive, < 0x8000000 or 0)
108588eca020SRusty Russell  * @gfp_mask: memory allocation flags
108688eca020SRusty Russell  *
108788eca020SRusty Russell  * Allocates an id in the range start <= id < end, or returns -ENOSPC.
108888eca020SRusty Russell  * On memory allocation failure, returns -ENOMEM.
108988eca020SRusty Russell  *
109088eca020SRusty Russell  * Use ida_simple_remove() to get rid of an id.
109188eca020SRusty Russell  */
109288eca020SRusty Russell int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
109388eca020SRusty Russell 		   gfp_t gfp_mask)
109488eca020SRusty Russell {
109588eca020SRusty Russell 	int ret, id;
109688eca020SRusty Russell 	unsigned int max;
109746cbc1d3STejun Heo 	unsigned long flags;
109888eca020SRusty Russell 
109988eca020SRusty Russell 	BUG_ON((int)start < 0);
110088eca020SRusty Russell 	BUG_ON((int)end < 0);
110188eca020SRusty Russell 
110288eca020SRusty Russell 	if (end == 0)
110388eca020SRusty Russell 		max = 0x80000000;
110488eca020SRusty Russell 	else {
110588eca020SRusty Russell 		BUG_ON(end < start);
110688eca020SRusty Russell 		max = end - 1;
110788eca020SRusty Russell 	}
110888eca020SRusty Russell 
110988eca020SRusty Russell again:
111088eca020SRusty Russell 	if (!ida_pre_get(ida, gfp_mask))
111188eca020SRusty Russell 		return -ENOMEM;
111288eca020SRusty Russell 
111346cbc1d3STejun Heo 	spin_lock_irqsave(&simple_ida_lock, flags);
111488eca020SRusty Russell 	ret = ida_get_new_above(ida, start, &id);
111588eca020SRusty Russell 	if (!ret) {
111688eca020SRusty Russell 		if (id > max) {
111788eca020SRusty Russell 			ida_remove(ida, id);
111888eca020SRusty Russell 			ret = -ENOSPC;
111988eca020SRusty Russell 		} else {
112088eca020SRusty Russell 			ret = id;
112188eca020SRusty Russell 		}
112288eca020SRusty Russell 	}
112346cbc1d3STejun Heo 	spin_unlock_irqrestore(&simple_ida_lock, flags);
112488eca020SRusty Russell 
112588eca020SRusty Russell 	if (unlikely(ret == -EAGAIN))
112688eca020SRusty Russell 		goto again;
112788eca020SRusty Russell 
112888eca020SRusty Russell 	return ret;
112988eca020SRusty Russell }
113088eca020SRusty Russell EXPORT_SYMBOL(ida_simple_get);
113188eca020SRusty Russell 
113288eca020SRusty Russell /**
113388eca020SRusty Russell  * ida_simple_remove - remove an allocated id.
113488eca020SRusty Russell  * @ida: the (initialized) ida.
113588eca020SRusty Russell  * @id: the id returned by ida_simple_get.
113688eca020SRusty Russell  */
113788eca020SRusty Russell void ida_simple_remove(struct ida *ida, unsigned int id)
113888eca020SRusty Russell {
113946cbc1d3STejun Heo 	unsigned long flags;
114046cbc1d3STejun Heo 
114188eca020SRusty Russell 	BUG_ON((int)id < 0);
114246cbc1d3STejun Heo 	spin_lock_irqsave(&simple_ida_lock, flags);
114388eca020SRusty Russell 	ida_remove(ida, id);
114446cbc1d3STejun Heo 	spin_unlock_irqrestore(&simple_ida_lock, flags);
114588eca020SRusty Russell }
114688eca020SRusty Russell EXPORT_SYMBOL(ida_simple_remove);
114788eca020SRusty Russell 
114888eca020SRusty Russell /**
114972dba584STejun Heo  * ida_init - initialize ida handle
115072dba584STejun Heo  * @ida:	ida handle
115172dba584STejun Heo  *
115272dba584STejun Heo  * This function is use to set up the handle (@ida) that you will pass
115372dba584STejun Heo  * to the rest of the functions.
115472dba584STejun Heo  */
115572dba584STejun Heo void ida_init(struct ida *ida)
115672dba584STejun Heo {
115772dba584STejun Heo 	memset(ida, 0, sizeof(struct ida));
115872dba584STejun Heo 	idr_init(&ida->idr);
115972dba584STejun Heo 
116072dba584STejun Heo }
116172dba584STejun Heo EXPORT_SYMBOL(ida_init);
1162