xref: /openbmc/linux/lib/idr.c (revision c8615d37)
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 
109d5c7409fSTejun Heo 	/* try to allocate directly from kmem_cache */
110d5c7409fSTejun Heo 	new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
111d5c7409fSTejun Heo 	if (new)
112d5c7409fSTejun Heo 		return new;
113d5c7409fSTejun Heo 
114d5c7409fSTejun Heo 	/*
115d5c7409fSTejun Heo 	 * Try to fetch one from the per-cpu preload buffer if in process
116d5c7409fSTejun Heo 	 * context.  See idr_preload() for details.
117d5c7409fSTejun Heo 	 */
118d5c7409fSTejun Heo 	if (in_interrupt())
119d5c7409fSTejun Heo 		return NULL;
120d5c7409fSTejun Heo 
121d5c7409fSTejun Heo 	preempt_disable();
122d5c7409fSTejun Heo 	new = __this_cpu_read(idr_preload_head);
123d5c7409fSTejun Heo 	if (new) {
124d5c7409fSTejun Heo 		__this_cpu_write(idr_preload_head, new->ary[0]);
125d5c7409fSTejun Heo 		__this_cpu_dec(idr_preload_cnt);
126d5c7409fSTejun Heo 		new->ary[0] = NULL;
127d5c7409fSTejun Heo 	}
128d5c7409fSTejun Heo 	preempt_enable();
129d5c7409fSTejun Heo 	return new;
130d5c7409fSTejun Heo }
131d5c7409fSTejun Heo 
132cf481c20SNadia Derbey static void idr_layer_rcu_free(struct rcu_head *head)
133cf481c20SNadia Derbey {
134cf481c20SNadia Derbey 	struct idr_layer *layer;
135cf481c20SNadia Derbey 
136cf481c20SNadia Derbey 	layer = container_of(head, struct idr_layer, rcu_head);
137cf481c20SNadia Derbey 	kmem_cache_free(idr_layer_cache, layer);
138cf481c20SNadia Derbey }
139cf481c20SNadia Derbey 
1400ffc2a9cSTejun Heo static inline void free_layer(struct idr *idr, struct idr_layer *p)
141cf481c20SNadia Derbey {
1420ffc2a9cSTejun Heo 	if (idr->hint && idr->hint == p)
1430ffc2a9cSTejun Heo 		RCU_INIT_POINTER(idr->hint, NULL);
144cf481c20SNadia Derbey 	call_rcu(&p->rcu_head, idr_layer_rcu_free);
145cf481c20SNadia Derbey }
146cf481c20SNadia Derbey 
1471eec0056SSonny Rao /* only called when idp->lock is held */
1484ae53789SNadia Derbey static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
1491eec0056SSonny Rao {
1501eec0056SSonny Rao 	p->ary[0] = idp->id_free;
1511eec0056SSonny Rao 	idp->id_free = p;
1521eec0056SSonny Rao 	idp->id_free_cnt++;
1531eec0056SSonny Rao }
1541eec0056SSonny Rao 
1554ae53789SNadia Derbey static void move_to_free_list(struct idr *idp, struct idr_layer *p)
1561da177e4SLinus Torvalds {
157c259cc28SRoland Dreier 	unsigned long flags;
158c259cc28SRoland Dreier 
1591da177e4SLinus Torvalds 	/*
1601da177e4SLinus Torvalds 	 * Depends on the return element being zeroed.
1611da177e4SLinus Torvalds 	 */
162c259cc28SRoland Dreier 	spin_lock_irqsave(&idp->lock, flags);
1634ae53789SNadia Derbey 	__move_to_free_list(idp, p);
164c259cc28SRoland Dreier 	spin_unlock_irqrestore(&idp->lock, flags);
1651da177e4SLinus Torvalds }
1661da177e4SLinus Torvalds 
167e33ac8bdSTejun Heo static void idr_mark_full(struct idr_layer **pa, int id)
168e33ac8bdSTejun Heo {
169e33ac8bdSTejun Heo 	struct idr_layer *p = pa[0];
170e33ac8bdSTejun Heo 	int l = 0;
171e33ac8bdSTejun Heo 
1721d9b2e1eSTejun Heo 	__set_bit(id & IDR_MASK, p->bitmap);
173e33ac8bdSTejun Heo 	/*
174e33ac8bdSTejun Heo 	 * If this layer is full mark the bit in the layer above to
175e33ac8bdSTejun Heo 	 * show that this part of the radix tree is full.  This may
176e33ac8bdSTejun Heo 	 * complete the layer above and require walking up the radix
177e33ac8bdSTejun Heo 	 * tree.
178e33ac8bdSTejun Heo 	 */
1791d9b2e1eSTejun Heo 	while (bitmap_full(p->bitmap, IDR_SIZE)) {
180e33ac8bdSTejun Heo 		if (!(p = pa[++l]))
181e33ac8bdSTejun Heo 			break;
182e33ac8bdSTejun Heo 		id = id >> IDR_BITS;
1831d9b2e1eSTejun Heo 		__set_bit((id & IDR_MASK), p->bitmap);
184e33ac8bdSTejun Heo 	}
185e33ac8bdSTejun Heo }
186e33ac8bdSTejun Heo 
187c8615d37STejun Heo int __idr_pre_get(struct idr *idp, gfp_t gfp_mask)
1881da177e4SLinus Torvalds {
189125c4c70SFengguang Wu 	while (idp->id_free_cnt < MAX_IDR_FREE) {
1901da177e4SLinus Torvalds 		struct idr_layer *new;
1915b019e99SAndrew Morton 		new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
1921da177e4SLinus Torvalds 		if (new == NULL)
1931da177e4SLinus Torvalds 			return (0);
1944ae53789SNadia Derbey 		move_to_free_list(idp, new);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 	return 1;
1971da177e4SLinus Torvalds }
198c8615d37STejun Heo EXPORT_SYMBOL(__idr_pre_get);
1991da177e4SLinus Torvalds 
20012d1b439STejun Heo /**
20112d1b439STejun Heo  * sub_alloc - try to allocate an id without growing the tree depth
20212d1b439STejun Heo  * @idp: idr handle
20312d1b439STejun Heo  * @starting_id: id to start search at
20412d1b439STejun Heo  * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer
205d5c7409fSTejun Heo  * @gfp_mask: allocation mask for idr_layer_alloc()
206d5c7409fSTejun Heo  * @layer_idr: optional idr passed to idr_layer_alloc()
20712d1b439STejun Heo  *
20812d1b439STejun Heo  * Allocate an id in range [@starting_id, INT_MAX] from @idp without
20912d1b439STejun Heo  * growing its depth.  Returns
21012d1b439STejun Heo  *
21112d1b439STejun Heo  *  the allocated id >= 0 if successful,
21212d1b439STejun Heo  *  -EAGAIN if the tree needs to grow for allocation to succeed,
21312d1b439STejun Heo  *  -ENOSPC if the id space is exhausted,
21412d1b439STejun Heo  *  -ENOMEM if more idr_layers need to be allocated.
21512d1b439STejun Heo  */
216d5c7409fSTejun Heo static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
217d5c7409fSTejun Heo 		     gfp_t gfp_mask, struct idr *layer_idr)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds 	int n, m, sh;
2201da177e4SLinus Torvalds 	struct idr_layer *p, *new;
2217aae6dd8STejun Heo 	int l, id, oid;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	id = *starting_id;
2247aae6dd8STejun Heo  restart:
2251da177e4SLinus Torvalds 	p = idp->top;
2261da177e4SLinus Torvalds 	l = idp->layers;
2271da177e4SLinus Torvalds 	pa[l--] = NULL;
2281da177e4SLinus Torvalds 	while (1) {
2291da177e4SLinus Torvalds 		/*
2301da177e4SLinus Torvalds 		 * We run around this while until we reach the leaf node...
2311da177e4SLinus Torvalds 		 */
2321da177e4SLinus Torvalds 		n = (id >> (IDR_BITS*l)) & IDR_MASK;
2331d9b2e1eSTejun Heo 		m = find_next_zero_bit(p->bitmap, IDR_SIZE, n);
2341da177e4SLinus Torvalds 		if (m == IDR_SIZE) {
2351da177e4SLinus Torvalds 			/* no space available go back to previous layer. */
2361da177e4SLinus Torvalds 			l++;
2377aae6dd8STejun Heo 			oid = id;
2381da177e4SLinus Torvalds 			id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
2397aae6dd8STejun Heo 
2407aae6dd8STejun Heo 			/* if already at the top layer, we need to grow */
241d2e7276bSTejun Heo 			if (id >= 1 << (idp->layers * IDR_BITS)) {
2421da177e4SLinus Torvalds 				*starting_id = id;
24312d1b439STejun Heo 				return -EAGAIN;
2441da177e4SLinus Torvalds 			}
245d2e7276bSTejun Heo 			p = pa[l];
246d2e7276bSTejun Heo 			BUG_ON(!p);
2477aae6dd8STejun Heo 
2487aae6dd8STejun Heo 			/* If we need to go up one layer, continue the
2497aae6dd8STejun Heo 			 * loop; otherwise, restart from the top.
2507aae6dd8STejun Heo 			 */
2517aae6dd8STejun Heo 			sh = IDR_BITS * (l + 1);
2527aae6dd8STejun Heo 			if (oid >> sh == id >> sh)
2531da177e4SLinus Torvalds 				continue;
2547aae6dd8STejun Heo 			else
2557aae6dd8STejun Heo 				goto restart;
2561da177e4SLinus Torvalds 		}
2571da177e4SLinus Torvalds 		if (m != n) {
2581da177e4SLinus Torvalds 			sh = IDR_BITS*l;
2591da177e4SLinus Torvalds 			id = ((id >> sh) ^ n ^ m) << sh;
2601da177e4SLinus Torvalds 		}
261125c4c70SFengguang Wu 		if ((id >= MAX_IDR_BIT) || (id < 0))
26212d1b439STejun Heo 			return -ENOSPC;
2631da177e4SLinus Torvalds 		if (l == 0)
2641da177e4SLinus Torvalds 			break;
2651da177e4SLinus Torvalds 		/*
2661da177e4SLinus Torvalds 		 * Create the layer below if it is missing.
2671da177e4SLinus Torvalds 		 */
2681da177e4SLinus Torvalds 		if (!p->ary[m]) {
269d5c7409fSTejun Heo 			new = idr_layer_alloc(gfp_mask, layer_idr);
2704ae53789SNadia Derbey 			if (!new)
27112d1b439STejun Heo 				return -ENOMEM;
2726ff2d39bSManfred Spraul 			new->layer = l-1;
27354616283STejun Heo 			new->prefix = id & idr_layer_prefix_mask(new->layer);
2743219b3b7SNadia Derbey 			rcu_assign_pointer(p->ary[m], new);
2751da177e4SLinus Torvalds 			p->count++;
2761da177e4SLinus Torvalds 		}
2771da177e4SLinus Torvalds 		pa[l--] = p;
2781da177e4SLinus Torvalds 		p = p->ary[m];
2791da177e4SLinus Torvalds 	}
280e33ac8bdSTejun Heo 
281e33ac8bdSTejun Heo 	pa[l] = p;
282e33ac8bdSTejun Heo 	return id;
2831da177e4SLinus Torvalds }
2841da177e4SLinus Torvalds 
285e33ac8bdSTejun Heo static int idr_get_empty_slot(struct idr *idp, int starting_id,
286d5c7409fSTejun Heo 			      struct idr_layer **pa, gfp_t gfp_mask,
287d5c7409fSTejun Heo 			      struct idr *layer_idr)
2881da177e4SLinus Torvalds {
2891da177e4SLinus Torvalds 	struct idr_layer *p, *new;
2901da177e4SLinus Torvalds 	int layers, v, id;
291c259cc28SRoland Dreier 	unsigned long flags;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	id = starting_id;
2941da177e4SLinus Torvalds build_up:
2951da177e4SLinus Torvalds 	p = idp->top;
2961da177e4SLinus Torvalds 	layers = idp->layers;
2971da177e4SLinus Torvalds 	if (unlikely(!p)) {
298d5c7409fSTejun Heo 		if (!(p = idr_layer_alloc(gfp_mask, layer_idr)))
29912d1b439STejun Heo 			return -ENOMEM;
3006ff2d39bSManfred Spraul 		p->layer = 0;
3011da177e4SLinus Torvalds 		layers = 1;
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 	/*
3041da177e4SLinus Torvalds 	 * Add a new layer to the top of the tree if the requested
3051da177e4SLinus Torvalds 	 * id is larger than the currently allocated space.
3061da177e4SLinus Torvalds 	 */
307326cf0f0STejun Heo 	while (id > idr_max(layers)) {
3081da177e4SLinus Torvalds 		layers++;
309711a49a0SManfred Spraul 		if (!p->count) {
310711a49a0SManfred Spraul 			/* special case: if the tree is currently empty,
311711a49a0SManfred Spraul 			 * then we grow the tree by moving the top node
312711a49a0SManfred Spraul 			 * upwards.
313711a49a0SManfred Spraul 			 */
314711a49a0SManfred Spraul 			p->layer++;
31554616283STejun Heo 			WARN_ON_ONCE(p->prefix);
3161da177e4SLinus Torvalds 			continue;
317711a49a0SManfred Spraul 		}
318d5c7409fSTejun Heo 		if (!(new = idr_layer_alloc(gfp_mask, layer_idr))) {
3191da177e4SLinus Torvalds 			/*
3201da177e4SLinus Torvalds 			 * The allocation failed.  If we built part of
3211da177e4SLinus Torvalds 			 * the structure tear it down.
3221da177e4SLinus Torvalds 			 */
323c259cc28SRoland Dreier 			spin_lock_irqsave(&idp->lock, flags);
3241da177e4SLinus Torvalds 			for (new = p; p && p != idp->top; new = p) {
3251da177e4SLinus Torvalds 				p = p->ary[0];
3261da177e4SLinus Torvalds 				new->ary[0] = NULL;
3271d9b2e1eSTejun Heo 				new->count = 0;
3281d9b2e1eSTejun Heo 				bitmap_clear(new->bitmap, 0, IDR_SIZE);
3294ae53789SNadia Derbey 				__move_to_free_list(idp, new);
3301da177e4SLinus Torvalds 			}
331c259cc28SRoland Dreier 			spin_unlock_irqrestore(&idp->lock, flags);
33212d1b439STejun Heo 			return -ENOMEM;
3331da177e4SLinus Torvalds 		}
3341da177e4SLinus Torvalds 		new->ary[0] = p;
3351da177e4SLinus Torvalds 		new->count = 1;
3366ff2d39bSManfred Spraul 		new->layer = layers-1;
33754616283STejun Heo 		new->prefix = id & idr_layer_prefix_mask(new->layer);
3381d9b2e1eSTejun Heo 		if (bitmap_full(p->bitmap, IDR_SIZE))
3391d9b2e1eSTejun Heo 			__set_bit(0, new->bitmap);
3401da177e4SLinus Torvalds 		p = new;
3411da177e4SLinus Torvalds 	}
3423219b3b7SNadia Derbey 	rcu_assign_pointer(idp->top, p);
3431da177e4SLinus Torvalds 	idp->layers = layers;
344d5c7409fSTejun Heo 	v = sub_alloc(idp, &id, pa, gfp_mask, layer_idr);
34512d1b439STejun Heo 	if (v == -EAGAIN)
3461da177e4SLinus Torvalds 		goto build_up;
3471da177e4SLinus Torvalds 	return(v);
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
350e33ac8bdSTejun Heo /*
3513594eb28STejun Heo  * @id and @pa are from a successful allocation from idr_get_empty_slot().
3523594eb28STejun Heo  * Install the user pointer @ptr and mark the slot full.
353e33ac8bdSTejun Heo  */
3540ffc2a9cSTejun Heo static void idr_fill_slot(struct idr *idr, void *ptr, int id,
3550ffc2a9cSTejun Heo 			  struct idr_layer **pa)
3563594eb28STejun Heo {
3570ffc2a9cSTejun Heo 	/* update hint used for lookup, cleared from free_layer() */
3580ffc2a9cSTejun Heo 	rcu_assign_pointer(idr->hint, pa[0]);
3590ffc2a9cSTejun Heo 
3603594eb28STejun Heo 	rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
361e33ac8bdSTejun Heo 	pa[0]->count++;
362e33ac8bdSTejun Heo 	idr_mark_full(pa, id);
363e33ac8bdSTejun Heo }
364e33ac8bdSTejun Heo 
365c8615d37STejun Heo int __idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
3661da177e4SLinus Torvalds {
367326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
3681da177e4SLinus Torvalds 	int rv;
369e15ae2ddSJesper Juhl 
370d5c7409fSTejun Heo 	rv = idr_get_empty_slot(idp, starting_id, pa, 0, idp);
371944ca05cSNadia Derbey 	if (rv < 0)
37212d1b439STejun Heo 		return rv == -ENOMEM ? -EAGAIN : rv;
3733594eb28STejun Heo 
3740ffc2a9cSTejun Heo 	idr_fill_slot(idp, ptr, rv, pa);
3751da177e4SLinus Torvalds 	*id = rv;
3761da177e4SLinus Torvalds 	return 0;
3771da177e4SLinus Torvalds }
378c8615d37STejun Heo EXPORT_SYMBOL(__idr_get_new_above);
3791da177e4SLinus Torvalds 
380d5c7409fSTejun Heo /**
381d5c7409fSTejun Heo  * idr_preload - preload for idr_alloc()
382d5c7409fSTejun Heo  * @gfp_mask: allocation mask to use for preloading
383d5c7409fSTejun Heo  *
384d5c7409fSTejun Heo  * Preload per-cpu layer buffer for idr_alloc().  Can only be used from
385d5c7409fSTejun Heo  * process context and each idr_preload() invocation should be matched with
386d5c7409fSTejun Heo  * idr_preload_end().  Note that preemption is disabled while preloaded.
387d5c7409fSTejun Heo  *
388d5c7409fSTejun Heo  * The first idr_alloc() in the preloaded section can be treated as if it
389d5c7409fSTejun Heo  * were invoked with @gfp_mask used for preloading.  This allows using more
390d5c7409fSTejun Heo  * permissive allocation masks for idrs protected by spinlocks.
391d5c7409fSTejun Heo  *
392d5c7409fSTejun Heo  * For example, if idr_alloc() below fails, the failure can be treated as
393d5c7409fSTejun Heo  * if idr_alloc() were called with GFP_KERNEL rather than GFP_NOWAIT.
394d5c7409fSTejun Heo  *
395d5c7409fSTejun Heo  *	idr_preload(GFP_KERNEL);
396d5c7409fSTejun Heo  *	spin_lock(lock);
397d5c7409fSTejun Heo  *
398d5c7409fSTejun Heo  *	id = idr_alloc(idr, ptr, start, end, GFP_NOWAIT);
399d5c7409fSTejun Heo  *
400d5c7409fSTejun Heo  *	spin_unlock(lock);
401d5c7409fSTejun Heo  *	idr_preload_end();
402d5c7409fSTejun Heo  *	if (id < 0)
403d5c7409fSTejun Heo  *		error;
404d5c7409fSTejun Heo  */
405d5c7409fSTejun Heo void idr_preload(gfp_t gfp_mask)
406d5c7409fSTejun Heo {
407d5c7409fSTejun Heo 	/*
408d5c7409fSTejun Heo 	 * Consuming preload buffer from non-process context breaks preload
409d5c7409fSTejun Heo 	 * allocation guarantee.  Disallow usage from those contexts.
410d5c7409fSTejun Heo 	 */
411d5c7409fSTejun Heo 	WARN_ON_ONCE(in_interrupt());
412d5c7409fSTejun Heo 	might_sleep_if(gfp_mask & __GFP_WAIT);
413d5c7409fSTejun Heo 
414d5c7409fSTejun Heo 	preempt_disable();
415d5c7409fSTejun Heo 
416d5c7409fSTejun Heo 	/*
417d5c7409fSTejun Heo 	 * idr_alloc() is likely to succeed w/o full idr_layer buffer and
418d5c7409fSTejun Heo 	 * return value from idr_alloc() needs to be checked for failure
419d5c7409fSTejun Heo 	 * anyway.  Silently give up if allocation fails.  The caller can
420d5c7409fSTejun Heo 	 * treat failures from idr_alloc() as if idr_alloc() were called
421d5c7409fSTejun Heo 	 * with @gfp_mask which should be enough.
422d5c7409fSTejun Heo 	 */
423d5c7409fSTejun Heo 	while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
424d5c7409fSTejun Heo 		struct idr_layer *new;
425d5c7409fSTejun Heo 
426d5c7409fSTejun Heo 		preempt_enable();
427d5c7409fSTejun Heo 		new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
428d5c7409fSTejun Heo 		preempt_disable();
429d5c7409fSTejun Heo 		if (!new)
430d5c7409fSTejun Heo 			break;
431d5c7409fSTejun Heo 
432d5c7409fSTejun Heo 		/* link the new one to per-cpu preload list */
433d5c7409fSTejun Heo 		new->ary[0] = __this_cpu_read(idr_preload_head);
434d5c7409fSTejun Heo 		__this_cpu_write(idr_preload_head, new);
435d5c7409fSTejun Heo 		__this_cpu_inc(idr_preload_cnt);
436d5c7409fSTejun Heo 	}
437d5c7409fSTejun Heo }
438d5c7409fSTejun Heo EXPORT_SYMBOL(idr_preload);
439d5c7409fSTejun Heo 
440d5c7409fSTejun Heo /**
441d5c7409fSTejun Heo  * idr_alloc - allocate new idr entry
442d5c7409fSTejun Heo  * @idr: the (initialized) idr
443d5c7409fSTejun Heo  * @ptr: pointer to be associated with the new id
444d5c7409fSTejun Heo  * @start: the minimum id (inclusive)
445d5c7409fSTejun Heo  * @end: the maximum id (exclusive, <= 0 for max)
446d5c7409fSTejun Heo  * @gfp_mask: memory allocation flags
447d5c7409fSTejun Heo  *
448d5c7409fSTejun Heo  * Allocate an id in [start, end) and associate it with @ptr.  If no ID is
449d5c7409fSTejun Heo  * available in the specified range, returns -ENOSPC.  On memory allocation
450d5c7409fSTejun Heo  * failure, returns -ENOMEM.
451d5c7409fSTejun Heo  *
452d5c7409fSTejun Heo  * Note that @end is treated as max when <= 0.  This is to always allow
453d5c7409fSTejun Heo  * using @start + N as @end as long as N is inside integer range.
454d5c7409fSTejun Heo  *
455d5c7409fSTejun Heo  * The user is responsible for exclusively synchronizing all operations
456d5c7409fSTejun Heo  * which may modify @idr.  However, read-only accesses such as idr_find()
457d5c7409fSTejun Heo  * or iteration can be performed under RCU read lock provided the user
458d5c7409fSTejun Heo  * destroys @ptr in RCU-safe way after removal from idr.
459d5c7409fSTejun Heo  */
460d5c7409fSTejun Heo int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
461d5c7409fSTejun Heo {
462d5c7409fSTejun Heo 	int max = end > 0 ? end - 1 : INT_MAX;	/* inclusive upper limit */
463326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
464d5c7409fSTejun Heo 	int id;
465d5c7409fSTejun Heo 
466d5c7409fSTejun Heo 	might_sleep_if(gfp_mask & __GFP_WAIT);
467d5c7409fSTejun Heo 
468d5c7409fSTejun Heo 	/* sanity checks */
469d5c7409fSTejun Heo 	if (WARN_ON_ONCE(start < 0))
470d5c7409fSTejun Heo 		return -EINVAL;
471d5c7409fSTejun Heo 	if (unlikely(max < start))
472d5c7409fSTejun Heo 		return -ENOSPC;
473d5c7409fSTejun Heo 
474d5c7409fSTejun Heo 	/* allocate id */
475d5c7409fSTejun Heo 	id = idr_get_empty_slot(idr, start, pa, gfp_mask, NULL);
476d5c7409fSTejun Heo 	if (unlikely(id < 0))
477d5c7409fSTejun Heo 		return id;
478d5c7409fSTejun Heo 	if (unlikely(id > max))
479d5c7409fSTejun Heo 		return -ENOSPC;
480d5c7409fSTejun Heo 
4810ffc2a9cSTejun Heo 	idr_fill_slot(idr, ptr, id, pa);
482d5c7409fSTejun Heo 	return id;
483d5c7409fSTejun Heo }
484d5c7409fSTejun Heo EXPORT_SYMBOL_GPL(idr_alloc);
485d5c7409fSTejun Heo 
4861da177e4SLinus Torvalds static void idr_remove_warning(int id)
4871da177e4SLinus Torvalds {
488f098ad65SNadia Derbey 	printk(KERN_WARNING
489f098ad65SNadia Derbey 		"idr_remove called for id=%d which is not allocated.\n", id);
4901da177e4SLinus Torvalds 	dump_stack();
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds static void sub_remove(struct idr *idp, int shift, int id)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct idr_layer *p = idp->top;
496326cf0f0STejun Heo 	struct idr_layer **pa[MAX_IDR_LEVEL + 1];
4971da177e4SLinus Torvalds 	struct idr_layer ***paa = &pa[0];
498cf481c20SNadia Derbey 	struct idr_layer *to_free;
4991da177e4SLinus Torvalds 	int n;
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	*paa = NULL;
5021da177e4SLinus Torvalds 	*++paa = &idp->top;
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds 	while ((shift > 0) && p) {
5051da177e4SLinus Torvalds 		n = (id >> shift) & IDR_MASK;
5061d9b2e1eSTejun Heo 		__clear_bit(n, p->bitmap);
5071da177e4SLinus Torvalds 		*++paa = &p->ary[n];
5081da177e4SLinus Torvalds 		p = p->ary[n];
5091da177e4SLinus Torvalds 		shift -= IDR_BITS;
5101da177e4SLinus Torvalds 	}
5111da177e4SLinus Torvalds 	n = id & IDR_MASK;
5121d9b2e1eSTejun Heo 	if (likely(p != NULL && test_bit(n, p->bitmap))) {
5131d9b2e1eSTejun Heo 		__clear_bit(n, p->bitmap);
514cf481c20SNadia Derbey 		rcu_assign_pointer(p->ary[n], NULL);
515cf481c20SNadia Derbey 		to_free = NULL;
5161da177e4SLinus Torvalds 		while(*paa && ! --((**paa)->count)){
517cf481c20SNadia Derbey 			if (to_free)
5180ffc2a9cSTejun Heo 				free_layer(idp, to_free);
519cf481c20SNadia Derbey 			to_free = **paa;
5201da177e4SLinus Torvalds 			**paa-- = NULL;
5211da177e4SLinus Torvalds 		}
5221da177e4SLinus Torvalds 		if (!*paa)
5231da177e4SLinus Torvalds 			idp->layers = 0;
524cf481c20SNadia Derbey 		if (to_free)
5250ffc2a9cSTejun Heo 			free_layer(idp, to_free);
526e15ae2ddSJesper Juhl 	} else
5271da177e4SLinus Torvalds 		idr_remove_warning(id);
5281da177e4SLinus Torvalds }
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds /**
53156083ab1SRandy Dunlap  * idr_remove - remove the given id and free its slot
53272fd4a35SRobert P. J. Day  * @idp: idr handle
53372fd4a35SRobert P. J. Day  * @id: unique key
5341da177e4SLinus Torvalds  */
5351da177e4SLinus Torvalds void idr_remove(struct idr *idp, int id)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct idr_layer *p;
538cf481c20SNadia Derbey 	struct idr_layer *to_free;
5391da177e4SLinus Torvalds 
5402e1c9b28STejun Heo 	if (id < 0)
541e8c8d1bcSTejun Heo 		return;
5421da177e4SLinus Torvalds 
5431da177e4SLinus Torvalds 	sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
544e15ae2ddSJesper Juhl 	if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
545cf481c20SNadia Derbey 	    idp->top->ary[0]) {
546cf481c20SNadia Derbey 		/*
547cf481c20SNadia Derbey 		 * Single child at leftmost slot: we can shrink the tree.
548cf481c20SNadia Derbey 		 * This level is not needed anymore since when layers are
549cf481c20SNadia Derbey 		 * inserted, they are inserted at the top of the existing
550cf481c20SNadia Derbey 		 * tree.
551cf481c20SNadia Derbey 		 */
552cf481c20SNadia Derbey 		to_free = idp->top;
5531da177e4SLinus Torvalds 		p = idp->top->ary[0];
554cf481c20SNadia Derbey 		rcu_assign_pointer(idp->top, p);
5551da177e4SLinus Torvalds 		--idp->layers;
5561d9b2e1eSTejun Heo 		to_free->count = 0;
5571d9b2e1eSTejun Heo 		bitmap_clear(to_free->bitmap, 0, IDR_SIZE);
5580ffc2a9cSTejun Heo 		free_layer(idp, to_free);
5591da177e4SLinus Torvalds 	}
560125c4c70SFengguang Wu 	while (idp->id_free_cnt >= MAX_IDR_FREE) {
5614ae53789SNadia Derbey 		p = get_from_free_list(idp);
562cf481c20SNadia Derbey 		/*
563cf481c20SNadia Derbey 		 * Note: we don't call the rcu callback here, since the only
564cf481c20SNadia Derbey 		 * layers that fall into the freelist are those that have been
565cf481c20SNadia Derbey 		 * preallocated.
566cf481c20SNadia Derbey 		 */
5671da177e4SLinus Torvalds 		kmem_cache_free(idr_layer_cache, p);
5681da177e4SLinus Torvalds 	}
569af8e2a4cSNadia Derbey 	return;
5701da177e4SLinus Torvalds }
5711da177e4SLinus Torvalds EXPORT_SYMBOL(idr_remove);
5721da177e4SLinus Torvalds 
573fe6e24ecSTejun Heo void __idr_remove_all(struct idr *idp)
57423936cc0SKristian Hoegsberg {
5756ace06dcSOleg Nesterov 	int n, id, max;
5762dcb22b3SImre Deak 	int bt_mask;
57723936cc0SKristian Hoegsberg 	struct idr_layer *p;
578326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
57923936cc0SKristian Hoegsberg 	struct idr_layer **paa = &pa[0];
58023936cc0SKristian Hoegsberg 
58123936cc0SKristian Hoegsberg 	n = idp->layers * IDR_BITS;
58223936cc0SKristian Hoegsberg 	p = idp->top;
5831b23336aSPaul E. McKenney 	rcu_assign_pointer(idp->top, NULL);
584326cf0f0STejun Heo 	max = idr_max(idp->layers);
58523936cc0SKristian Hoegsberg 
58623936cc0SKristian Hoegsberg 	id = 0;
587326cf0f0STejun Heo 	while (id >= 0 && id <= max) {
58823936cc0SKristian Hoegsberg 		while (n > IDR_BITS && p) {
58923936cc0SKristian Hoegsberg 			n -= IDR_BITS;
59023936cc0SKristian Hoegsberg 			*paa++ = p;
59123936cc0SKristian Hoegsberg 			p = p->ary[(id >> n) & IDR_MASK];
59223936cc0SKristian Hoegsberg 		}
59323936cc0SKristian Hoegsberg 
5942dcb22b3SImre Deak 		bt_mask = id;
59523936cc0SKristian Hoegsberg 		id += 1 << n;
5962dcb22b3SImre Deak 		/* Get the highest bit that the above add changed from 0->1. */
5972dcb22b3SImre Deak 		while (n < fls(id ^ bt_mask)) {
598cf481c20SNadia Derbey 			if (p)
5990ffc2a9cSTejun Heo 				free_layer(idp, p);
60023936cc0SKristian Hoegsberg 			n += IDR_BITS;
60123936cc0SKristian Hoegsberg 			p = *--paa;
60223936cc0SKristian Hoegsberg 		}
60323936cc0SKristian Hoegsberg 	}
60423936cc0SKristian Hoegsberg 	idp->layers = 0;
60523936cc0SKristian Hoegsberg }
606fe6e24ecSTejun Heo EXPORT_SYMBOL(__idr_remove_all);
60723936cc0SKristian Hoegsberg 
60823936cc0SKristian Hoegsberg /**
6098d3b3591SAndrew Morton  * idr_destroy - release all cached layers within an idr tree
610ea24ea85SNaohiro Aota  * @idp: idr handle
6119bb26bc1STejun Heo  *
6129bb26bc1STejun Heo  * Free all id mappings and all idp_layers.  After this function, @idp is
6139bb26bc1STejun Heo  * completely unused and can be freed / recycled.  The caller is
6149bb26bc1STejun Heo  * responsible for ensuring that no one else accesses @idp during or after
6159bb26bc1STejun Heo  * idr_destroy().
6169bb26bc1STejun Heo  *
6179bb26bc1STejun Heo  * A typical clean-up sequence for objects stored in an idr tree will use
6189bb26bc1STejun Heo  * idr_for_each() to free all objects, if necessay, then idr_destroy() to
6199bb26bc1STejun Heo  * free up the id mappings and cached idr_layers.
6208d3b3591SAndrew Morton  */
6218d3b3591SAndrew Morton void idr_destroy(struct idr *idp)
6228d3b3591SAndrew Morton {
623fe6e24ecSTejun Heo 	__idr_remove_all(idp);
6249bb26bc1STejun Heo 
6258d3b3591SAndrew Morton 	while (idp->id_free_cnt) {
6264ae53789SNadia Derbey 		struct idr_layer *p = get_from_free_list(idp);
6278d3b3591SAndrew Morton 		kmem_cache_free(idr_layer_cache, p);
6288d3b3591SAndrew Morton 	}
6298d3b3591SAndrew Morton }
6308d3b3591SAndrew Morton EXPORT_SYMBOL(idr_destroy);
6318d3b3591SAndrew Morton 
6320ffc2a9cSTejun Heo void *idr_find_slowpath(struct idr *idp, int id)
6331da177e4SLinus Torvalds {
6341da177e4SLinus Torvalds 	int n;
6351da177e4SLinus Torvalds 	struct idr_layer *p;
6361da177e4SLinus Torvalds 
6372e1c9b28STejun Heo 	if (id < 0)
638e8c8d1bcSTejun Heo 		return NULL;
639e8c8d1bcSTejun Heo 
64096be753aSPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
6416ff2d39bSManfred Spraul 	if (!p)
6426ff2d39bSManfred Spraul 		return NULL;
6436ff2d39bSManfred Spraul 	n = (p->layer+1) * IDR_BITS;
6441da177e4SLinus Torvalds 
645326cf0f0STejun Heo 	if (id > idr_max(p->layer + 1))
6461da177e4SLinus Torvalds 		return NULL;
6476ff2d39bSManfred Spraul 	BUG_ON(n == 0);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	while (n > 0 && p) {
6501da177e4SLinus Torvalds 		n -= IDR_BITS;
6516ff2d39bSManfred Spraul 		BUG_ON(n != p->layer*IDR_BITS);
65296be753aSPaul E. McKenney 		p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 	return((void *)p);
6551da177e4SLinus Torvalds }
6560ffc2a9cSTejun Heo EXPORT_SYMBOL(idr_find_slowpath);
6571da177e4SLinus Torvalds 
6585806f07cSJeff Mahoney /**
65996d7fa42SKristian Hoegsberg  * idr_for_each - iterate through all stored pointers
66096d7fa42SKristian Hoegsberg  * @idp: idr handle
66196d7fa42SKristian Hoegsberg  * @fn: function to be called for each pointer
66296d7fa42SKristian Hoegsberg  * @data: data passed back to callback function
66396d7fa42SKristian Hoegsberg  *
66496d7fa42SKristian Hoegsberg  * Iterate over the pointers registered with the given idr.  The
66596d7fa42SKristian Hoegsberg  * callback function will be called for each pointer currently
66696d7fa42SKristian Hoegsberg  * registered, passing the id, the pointer and the data pointer passed
66796d7fa42SKristian Hoegsberg  * to this function.  It is not safe to modify the idr tree while in
66896d7fa42SKristian Hoegsberg  * the callback, so functions such as idr_get_new and idr_remove are
66996d7fa42SKristian Hoegsberg  * not allowed.
67096d7fa42SKristian Hoegsberg  *
67196d7fa42SKristian Hoegsberg  * We check the return of @fn each time. If it returns anything other
67256083ab1SRandy Dunlap  * than %0, we break out and return that value.
67396d7fa42SKristian Hoegsberg  *
67496d7fa42SKristian Hoegsberg  * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
67596d7fa42SKristian Hoegsberg  */
67696d7fa42SKristian Hoegsberg int idr_for_each(struct idr *idp,
67796d7fa42SKristian Hoegsberg 		 int (*fn)(int id, void *p, void *data), void *data)
67896d7fa42SKristian Hoegsberg {
67996d7fa42SKristian Hoegsberg 	int n, id, max, error = 0;
68096d7fa42SKristian Hoegsberg 	struct idr_layer *p;
681326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
68296d7fa42SKristian Hoegsberg 	struct idr_layer **paa = &pa[0];
68396d7fa42SKristian Hoegsberg 
68496d7fa42SKristian Hoegsberg 	n = idp->layers * IDR_BITS;
68596be753aSPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
686326cf0f0STejun Heo 	max = idr_max(idp->layers);
68796d7fa42SKristian Hoegsberg 
68896d7fa42SKristian Hoegsberg 	id = 0;
689326cf0f0STejun Heo 	while (id >= 0 && id <= max) {
69096d7fa42SKristian Hoegsberg 		while (n > 0 && p) {
69196d7fa42SKristian Hoegsberg 			n -= IDR_BITS;
69296d7fa42SKristian Hoegsberg 			*paa++ = p;
69396be753aSPaul E. McKenney 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
69496d7fa42SKristian Hoegsberg 		}
69596d7fa42SKristian Hoegsberg 
69696d7fa42SKristian Hoegsberg 		if (p) {
69796d7fa42SKristian Hoegsberg 			error = fn(id, (void *)p, data);
69896d7fa42SKristian Hoegsberg 			if (error)
69996d7fa42SKristian Hoegsberg 				break;
70096d7fa42SKristian Hoegsberg 		}
70196d7fa42SKristian Hoegsberg 
70296d7fa42SKristian Hoegsberg 		id += 1 << n;
70396d7fa42SKristian Hoegsberg 		while (n < fls(id)) {
70496d7fa42SKristian Hoegsberg 			n += IDR_BITS;
70596d7fa42SKristian Hoegsberg 			p = *--paa;
70696d7fa42SKristian Hoegsberg 		}
70796d7fa42SKristian Hoegsberg 	}
70896d7fa42SKristian Hoegsberg 
70996d7fa42SKristian Hoegsberg 	return error;
71096d7fa42SKristian Hoegsberg }
71196d7fa42SKristian Hoegsberg EXPORT_SYMBOL(idr_for_each);
71296d7fa42SKristian Hoegsberg 
71396d7fa42SKristian Hoegsberg /**
71438460b48SKAMEZAWA Hiroyuki  * idr_get_next - lookup next object of id to given id.
71538460b48SKAMEZAWA Hiroyuki  * @idp: idr handle
716ea24ea85SNaohiro Aota  * @nextidp:  pointer to lookup key
71738460b48SKAMEZAWA Hiroyuki  *
71838460b48SKAMEZAWA Hiroyuki  * Returns pointer to registered object with id, which is next number to
7191458ce16SNaohiro Aota  * given id. After being looked up, *@nextidp will be updated for the next
7201458ce16SNaohiro Aota  * iteration.
7219f7de827SHugh Dickins  *
7229f7de827SHugh Dickins  * This function can be called under rcu_read_lock(), given that the leaf
7239f7de827SHugh Dickins  * pointers lifetimes are correctly managed.
72438460b48SKAMEZAWA Hiroyuki  */
72538460b48SKAMEZAWA Hiroyuki void *idr_get_next(struct idr *idp, int *nextidp)
72638460b48SKAMEZAWA Hiroyuki {
727326cf0f0STejun Heo 	struct idr_layer *p, *pa[MAX_IDR_LEVEL + 1];
72838460b48SKAMEZAWA Hiroyuki 	struct idr_layer **paa = &pa[0];
72938460b48SKAMEZAWA Hiroyuki 	int id = *nextidp;
73038460b48SKAMEZAWA Hiroyuki 	int n, max;
73138460b48SKAMEZAWA Hiroyuki 
73238460b48SKAMEZAWA Hiroyuki 	/* find first ent */
73394bfa3b6SPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
73438460b48SKAMEZAWA Hiroyuki 	if (!p)
73538460b48SKAMEZAWA Hiroyuki 		return NULL;
7369f7de827SHugh Dickins 	n = (p->layer + 1) * IDR_BITS;
737326cf0f0STejun Heo 	max = idr_max(p->layer + 1);
73838460b48SKAMEZAWA Hiroyuki 
739326cf0f0STejun Heo 	while (id >= 0 && id <= max) {
74038460b48SKAMEZAWA Hiroyuki 		while (n > 0 && p) {
74138460b48SKAMEZAWA Hiroyuki 			n -= IDR_BITS;
74238460b48SKAMEZAWA Hiroyuki 			*paa++ = p;
74394bfa3b6SPaul E. McKenney 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
74438460b48SKAMEZAWA Hiroyuki 		}
74538460b48SKAMEZAWA Hiroyuki 
74638460b48SKAMEZAWA Hiroyuki 		if (p) {
74738460b48SKAMEZAWA Hiroyuki 			*nextidp = id;
74838460b48SKAMEZAWA Hiroyuki 			return p;
74938460b48SKAMEZAWA Hiroyuki 		}
75038460b48SKAMEZAWA Hiroyuki 
7516cdae741STejun Heo 		/*
7526cdae741STejun Heo 		 * Proceed to the next layer at the current level.  Unlike
7536cdae741STejun Heo 		 * idr_for_each(), @id isn't guaranteed to be aligned to
7546cdae741STejun Heo 		 * layer boundary at this point and adding 1 << n may
7556cdae741STejun Heo 		 * incorrectly skip IDs.  Make sure we jump to the
7566cdae741STejun Heo 		 * beginning of the next layer using round_up().
7576cdae741STejun Heo 		 */
7586cdae741STejun Heo 		id = round_up(id + 1, 1 << n);
75938460b48SKAMEZAWA Hiroyuki 		while (n < fls(id)) {
76038460b48SKAMEZAWA Hiroyuki 			n += IDR_BITS;
76138460b48SKAMEZAWA Hiroyuki 			p = *--paa;
76238460b48SKAMEZAWA Hiroyuki 		}
76338460b48SKAMEZAWA Hiroyuki 	}
76438460b48SKAMEZAWA Hiroyuki 	return NULL;
76538460b48SKAMEZAWA Hiroyuki }
7664d1ee80fSBen Hutchings EXPORT_SYMBOL(idr_get_next);
76738460b48SKAMEZAWA Hiroyuki 
76838460b48SKAMEZAWA Hiroyuki 
76938460b48SKAMEZAWA Hiroyuki /**
7705806f07cSJeff Mahoney  * idr_replace - replace pointer for given id
7715806f07cSJeff Mahoney  * @idp: idr handle
7725806f07cSJeff Mahoney  * @ptr: pointer you want associated with the id
7735806f07cSJeff Mahoney  * @id: lookup key
7745806f07cSJeff Mahoney  *
7755806f07cSJeff Mahoney  * Replace the pointer registered with an id and return the old value.
77656083ab1SRandy Dunlap  * A %-ENOENT return indicates that @id was not found.
77756083ab1SRandy Dunlap  * A %-EINVAL return indicates that @id was not within valid constraints.
7785806f07cSJeff Mahoney  *
779cf481c20SNadia Derbey  * The caller must serialize with writers.
7805806f07cSJeff Mahoney  */
7815806f07cSJeff Mahoney void *idr_replace(struct idr *idp, void *ptr, int id)
7825806f07cSJeff Mahoney {
7835806f07cSJeff Mahoney 	int n;
7845806f07cSJeff Mahoney 	struct idr_layer *p, *old_p;
7855806f07cSJeff Mahoney 
7862e1c9b28STejun Heo 	if (id < 0)
787e8c8d1bcSTejun Heo 		return ERR_PTR(-EINVAL);
788e8c8d1bcSTejun Heo 
7895806f07cSJeff Mahoney 	p = idp->top;
7906ff2d39bSManfred Spraul 	if (!p)
7916ff2d39bSManfred Spraul 		return ERR_PTR(-EINVAL);
7926ff2d39bSManfred Spraul 
7936ff2d39bSManfred Spraul 	n = (p->layer+1) * IDR_BITS;
7945806f07cSJeff Mahoney 
7955806f07cSJeff Mahoney 	if (id >= (1 << n))
7965806f07cSJeff Mahoney 		return ERR_PTR(-EINVAL);
7975806f07cSJeff Mahoney 
7985806f07cSJeff Mahoney 	n -= IDR_BITS;
7995806f07cSJeff Mahoney 	while ((n > 0) && p) {
8005806f07cSJeff Mahoney 		p = p->ary[(id >> n) & IDR_MASK];
8015806f07cSJeff Mahoney 		n -= IDR_BITS;
8025806f07cSJeff Mahoney 	}
8035806f07cSJeff Mahoney 
8045806f07cSJeff Mahoney 	n = id & IDR_MASK;
8051d9b2e1eSTejun Heo 	if (unlikely(p == NULL || !test_bit(n, p->bitmap)))
8065806f07cSJeff Mahoney 		return ERR_PTR(-ENOENT);
8075806f07cSJeff Mahoney 
8085806f07cSJeff Mahoney 	old_p = p->ary[n];
809cf481c20SNadia Derbey 	rcu_assign_pointer(p->ary[n], ptr);
8105806f07cSJeff Mahoney 
8115806f07cSJeff Mahoney 	return old_p;
8125806f07cSJeff Mahoney }
8135806f07cSJeff Mahoney EXPORT_SYMBOL(idr_replace);
8145806f07cSJeff Mahoney 
815199f0ca5SAkinobu Mita void __init idr_init_cache(void)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	idr_layer_cache = kmem_cache_create("idr_layer_cache",
8185b019e99SAndrew Morton 				sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds /**
8221da177e4SLinus Torvalds  * idr_init - initialize idr handle
8231da177e4SLinus Torvalds  * @idp:	idr handle
8241da177e4SLinus Torvalds  *
8251da177e4SLinus Torvalds  * This function is use to set up the handle (@idp) that you will pass
8261da177e4SLinus Torvalds  * to the rest of the functions.
8271da177e4SLinus Torvalds  */
8281da177e4SLinus Torvalds void idr_init(struct idr *idp)
8291da177e4SLinus Torvalds {
8301da177e4SLinus Torvalds 	memset(idp, 0, sizeof(struct idr));
8311da177e4SLinus Torvalds 	spin_lock_init(&idp->lock);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds EXPORT_SYMBOL(idr_init);
83472dba584STejun Heo 
83572dba584STejun Heo 
83656083ab1SRandy Dunlap /**
83756083ab1SRandy Dunlap  * DOC: IDA description
83872dba584STejun Heo  * IDA - IDR based ID allocator
83972dba584STejun Heo  *
84056083ab1SRandy Dunlap  * This is id allocator without id -> pointer translation.  Memory
84172dba584STejun Heo  * usage is much lower than full blown idr because each id only
84272dba584STejun Heo  * occupies a bit.  ida uses a custom leaf node which contains
84372dba584STejun Heo  * IDA_BITMAP_BITS slots.
84472dba584STejun Heo  *
84572dba584STejun Heo  * 2007-04-25  written by Tejun Heo <htejun@gmail.com>
84672dba584STejun Heo  */
84772dba584STejun Heo 
84872dba584STejun Heo static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
84972dba584STejun Heo {
85072dba584STejun Heo 	unsigned long flags;
85172dba584STejun Heo 
85272dba584STejun Heo 	if (!ida->free_bitmap) {
85372dba584STejun Heo 		spin_lock_irqsave(&ida->idr.lock, flags);
85472dba584STejun Heo 		if (!ida->free_bitmap) {
85572dba584STejun Heo 			ida->free_bitmap = bitmap;
85672dba584STejun Heo 			bitmap = NULL;
85772dba584STejun Heo 		}
85872dba584STejun Heo 		spin_unlock_irqrestore(&ida->idr.lock, flags);
85972dba584STejun Heo 	}
86072dba584STejun Heo 
86172dba584STejun Heo 	kfree(bitmap);
86272dba584STejun Heo }
86372dba584STejun Heo 
86472dba584STejun Heo /**
86572dba584STejun Heo  * ida_pre_get - reserve resources for ida allocation
86672dba584STejun Heo  * @ida:	ida handle
86772dba584STejun Heo  * @gfp_mask:	memory allocation flag
86872dba584STejun Heo  *
86972dba584STejun Heo  * This function should be called prior to locking and calling the
87072dba584STejun Heo  * following function.  It preallocates enough memory to satisfy the
87172dba584STejun Heo  * worst possible allocation.
87272dba584STejun Heo  *
87356083ab1SRandy Dunlap  * If the system is REALLY out of memory this function returns %0,
87456083ab1SRandy Dunlap  * otherwise %1.
87572dba584STejun Heo  */
87672dba584STejun Heo int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
87772dba584STejun Heo {
87872dba584STejun Heo 	/* allocate idr_layers */
879c8615d37STejun Heo 	if (!__idr_pre_get(&ida->idr, gfp_mask))
88072dba584STejun Heo 		return 0;
88172dba584STejun Heo 
88272dba584STejun Heo 	/* allocate free_bitmap */
88372dba584STejun Heo 	if (!ida->free_bitmap) {
88472dba584STejun Heo 		struct ida_bitmap *bitmap;
88572dba584STejun Heo 
88672dba584STejun Heo 		bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
88772dba584STejun Heo 		if (!bitmap)
88872dba584STejun Heo 			return 0;
88972dba584STejun Heo 
89072dba584STejun Heo 		free_bitmap(ida, bitmap);
89172dba584STejun Heo 	}
89272dba584STejun Heo 
89372dba584STejun Heo 	return 1;
89472dba584STejun Heo }
89572dba584STejun Heo EXPORT_SYMBOL(ida_pre_get);
89672dba584STejun Heo 
89772dba584STejun Heo /**
89872dba584STejun Heo  * ida_get_new_above - allocate new ID above or equal to a start id
89972dba584STejun Heo  * @ida:	ida handle
900ea24ea85SNaohiro Aota  * @starting_id: id to start search at
90172dba584STejun Heo  * @p_id:	pointer to the allocated handle
90272dba584STejun Heo  *
903e3816c54SWang Sheng-Hui  * Allocate new ID above or equal to @starting_id.  It should be called
904e3816c54SWang Sheng-Hui  * with any required locks.
90572dba584STejun Heo  *
90656083ab1SRandy Dunlap  * If memory is required, it will return %-EAGAIN, you should unlock
90772dba584STejun Heo  * and go back to the ida_pre_get() call.  If the ida is full, it will
90856083ab1SRandy Dunlap  * return %-ENOSPC.
90972dba584STejun Heo  *
91056083ab1SRandy Dunlap  * @p_id returns a value in the range @starting_id ... %0x7fffffff.
91172dba584STejun Heo  */
91272dba584STejun Heo int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
91372dba584STejun Heo {
914326cf0f0STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL + 1];
91572dba584STejun Heo 	struct ida_bitmap *bitmap;
91672dba584STejun Heo 	unsigned long flags;
91772dba584STejun Heo 	int idr_id = starting_id / IDA_BITMAP_BITS;
91872dba584STejun Heo 	int offset = starting_id % IDA_BITMAP_BITS;
91972dba584STejun Heo 	int t, id;
92072dba584STejun Heo 
92172dba584STejun Heo  restart:
92272dba584STejun Heo 	/* get vacant slot */
923d5c7409fSTejun Heo 	t = idr_get_empty_slot(&ida->idr, idr_id, pa, 0, &ida->idr);
924944ca05cSNadia Derbey 	if (t < 0)
92512d1b439STejun Heo 		return t == -ENOMEM ? -EAGAIN : t;
92672dba584STejun Heo 
927125c4c70SFengguang Wu 	if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT)
92872dba584STejun Heo 		return -ENOSPC;
92972dba584STejun Heo 
93072dba584STejun Heo 	if (t != idr_id)
93172dba584STejun Heo 		offset = 0;
93272dba584STejun Heo 	idr_id = t;
93372dba584STejun Heo 
93472dba584STejun Heo 	/* if bitmap isn't there, create a new one */
93572dba584STejun Heo 	bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
93672dba584STejun Heo 	if (!bitmap) {
93772dba584STejun Heo 		spin_lock_irqsave(&ida->idr.lock, flags);
93872dba584STejun Heo 		bitmap = ida->free_bitmap;
93972dba584STejun Heo 		ida->free_bitmap = NULL;
94072dba584STejun Heo 		spin_unlock_irqrestore(&ida->idr.lock, flags);
94172dba584STejun Heo 
94272dba584STejun Heo 		if (!bitmap)
94372dba584STejun Heo 			return -EAGAIN;
94472dba584STejun Heo 
94572dba584STejun Heo 		memset(bitmap, 0, sizeof(struct ida_bitmap));
9463219b3b7SNadia Derbey 		rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
9473219b3b7SNadia Derbey 				(void *)bitmap);
94872dba584STejun Heo 		pa[0]->count++;
94972dba584STejun Heo 	}
95072dba584STejun Heo 
95172dba584STejun Heo 	/* lookup for empty slot */
95272dba584STejun Heo 	t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
95372dba584STejun Heo 	if (t == IDA_BITMAP_BITS) {
95472dba584STejun Heo 		/* no empty slot after offset, continue to the next chunk */
95572dba584STejun Heo 		idr_id++;
95672dba584STejun Heo 		offset = 0;
95772dba584STejun Heo 		goto restart;
95872dba584STejun Heo 	}
95972dba584STejun Heo 
96072dba584STejun Heo 	id = idr_id * IDA_BITMAP_BITS + t;
961125c4c70SFengguang Wu 	if (id >= MAX_IDR_BIT)
96272dba584STejun Heo 		return -ENOSPC;
96372dba584STejun Heo 
96472dba584STejun Heo 	__set_bit(t, bitmap->bitmap);
96572dba584STejun Heo 	if (++bitmap->nr_busy == IDA_BITMAP_BITS)
96672dba584STejun Heo 		idr_mark_full(pa, idr_id);
96772dba584STejun Heo 
96872dba584STejun Heo 	*p_id = id;
96972dba584STejun Heo 
97072dba584STejun Heo 	/* Each leaf node can handle nearly a thousand slots and the
97172dba584STejun Heo 	 * whole idea of ida is to have small memory foot print.
97272dba584STejun Heo 	 * Throw away extra resources one by one after each successful
97372dba584STejun Heo 	 * allocation.
97472dba584STejun Heo 	 */
97572dba584STejun Heo 	if (ida->idr.id_free_cnt || ida->free_bitmap) {
9764ae53789SNadia Derbey 		struct idr_layer *p = get_from_free_list(&ida->idr);
97772dba584STejun Heo 		if (p)
97872dba584STejun Heo 			kmem_cache_free(idr_layer_cache, p);
97972dba584STejun Heo 	}
98072dba584STejun Heo 
98172dba584STejun Heo 	return 0;
98272dba584STejun Heo }
98372dba584STejun Heo EXPORT_SYMBOL(ida_get_new_above);
98472dba584STejun Heo 
98572dba584STejun Heo /**
98672dba584STejun Heo  * ida_remove - remove the given ID
98772dba584STejun Heo  * @ida:	ida handle
98872dba584STejun Heo  * @id:		ID to free
98972dba584STejun Heo  */
99072dba584STejun Heo void ida_remove(struct ida *ida, int id)
99172dba584STejun Heo {
99272dba584STejun Heo 	struct idr_layer *p = ida->idr.top;
99372dba584STejun Heo 	int shift = (ida->idr.layers - 1) * IDR_BITS;
99472dba584STejun Heo 	int idr_id = id / IDA_BITMAP_BITS;
99572dba584STejun Heo 	int offset = id % IDA_BITMAP_BITS;
99672dba584STejun Heo 	int n;
99772dba584STejun Heo 	struct ida_bitmap *bitmap;
99872dba584STejun Heo 
99972dba584STejun Heo 	/* clear full bits while looking up the leaf idr_layer */
100072dba584STejun Heo 	while ((shift > 0) && p) {
100172dba584STejun Heo 		n = (idr_id >> shift) & IDR_MASK;
10021d9b2e1eSTejun Heo 		__clear_bit(n, p->bitmap);
100372dba584STejun Heo 		p = p->ary[n];
100472dba584STejun Heo 		shift -= IDR_BITS;
100572dba584STejun Heo 	}
100672dba584STejun Heo 
100772dba584STejun Heo 	if (p == NULL)
100872dba584STejun Heo 		goto err;
100972dba584STejun Heo 
101072dba584STejun Heo 	n = idr_id & IDR_MASK;
10111d9b2e1eSTejun Heo 	__clear_bit(n, p->bitmap);
101272dba584STejun Heo 
101372dba584STejun Heo 	bitmap = (void *)p->ary[n];
101472dba584STejun Heo 	if (!test_bit(offset, bitmap->bitmap))
101572dba584STejun Heo 		goto err;
101672dba584STejun Heo 
101772dba584STejun Heo 	/* update bitmap and remove it if empty */
101872dba584STejun Heo 	__clear_bit(offset, bitmap->bitmap);
101972dba584STejun Heo 	if (--bitmap->nr_busy == 0) {
10201d9b2e1eSTejun Heo 		__set_bit(n, p->bitmap);	/* to please idr_remove() */
102172dba584STejun Heo 		idr_remove(&ida->idr, idr_id);
102272dba584STejun Heo 		free_bitmap(ida, bitmap);
102372dba584STejun Heo 	}
102472dba584STejun Heo 
102572dba584STejun Heo 	return;
102672dba584STejun Heo 
102772dba584STejun Heo  err:
102872dba584STejun Heo 	printk(KERN_WARNING
102972dba584STejun Heo 	       "ida_remove called for id=%d which is not allocated.\n", id);
103072dba584STejun Heo }
103172dba584STejun Heo EXPORT_SYMBOL(ida_remove);
103272dba584STejun Heo 
103372dba584STejun Heo /**
103472dba584STejun Heo  * ida_destroy - release all cached layers within an ida tree
1035ea24ea85SNaohiro Aota  * @ida:		ida handle
103672dba584STejun Heo  */
103772dba584STejun Heo void ida_destroy(struct ida *ida)
103872dba584STejun Heo {
103972dba584STejun Heo 	idr_destroy(&ida->idr);
104072dba584STejun Heo 	kfree(ida->free_bitmap);
104172dba584STejun Heo }
104272dba584STejun Heo EXPORT_SYMBOL(ida_destroy);
104372dba584STejun Heo 
104472dba584STejun Heo /**
104588eca020SRusty Russell  * ida_simple_get - get a new id.
104688eca020SRusty Russell  * @ida: the (initialized) ida.
104788eca020SRusty Russell  * @start: the minimum id (inclusive, < 0x8000000)
104888eca020SRusty Russell  * @end: the maximum id (exclusive, < 0x8000000 or 0)
104988eca020SRusty Russell  * @gfp_mask: memory allocation flags
105088eca020SRusty Russell  *
105188eca020SRusty Russell  * Allocates an id in the range start <= id < end, or returns -ENOSPC.
105288eca020SRusty Russell  * On memory allocation failure, returns -ENOMEM.
105388eca020SRusty Russell  *
105488eca020SRusty Russell  * Use ida_simple_remove() to get rid of an id.
105588eca020SRusty Russell  */
105688eca020SRusty Russell int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
105788eca020SRusty Russell 		   gfp_t gfp_mask)
105888eca020SRusty Russell {
105988eca020SRusty Russell 	int ret, id;
106088eca020SRusty Russell 	unsigned int max;
106146cbc1d3STejun Heo 	unsigned long flags;
106288eca020SRusty Russell 
106388eca020SRusty Russell 	BUG_ON((int)start < 0);
106488eca020SRusty Russell 	BUG_ON((int)end < 0);
106588eca020SRusty Russell 
106688eca020SRusty Russell 	if (end == 0)
106788eca020SRusty Russell 		max = 0x80000000;
106888eca020SRusty Russell 	else {
106988eca020SRusty Russell 		BUG_ON(end < start);
107088eca020SRusty Russell 		max = end - 1;
107188eca020SRusty Russell 	}
107288eca020SRusty Russell 
107388eca020SRusty Russell again:
107488eca020SRusty Russell 	if (!ida_pre_get(ida, gfp_mask))
107588eca020SRusty Russell 		return -ENOMEM;
107688eca020SRusty Russell 
107746cbc1d3STejun Heo 	spin_lock_irqsave(&simple_ida_lock, flags);
107888eca020SRusty Russell 	ret = ida_get_new_above(ida, start, &id);
107988eca020SRusty Russell 	if (!ret) {
108088eca020SRusty Russell 		if (id > max) {
108188eca020SRusty Russell 			ida_remove(ida, id);
108288eca020SRusty Russell 			ret = -ENOSPC;
108388eca020SRusty Russell 		} else {
108488eca020SRusty Russell 			ret = id;
108588eca020SRusty Russell 		}
108688eca020SRusty Russell 	}
108746cbc1d3STejun Heo 	spin_unlock_irqrestore(&simple_ida_lock, flags);
108888eca020SRusty Russell 
108988eca020SRusty Russell 	if (unlikely(ret == -EAGAIN))
109088eca020SRusty Russell 		goto again;
109188eca020SRusty Russell 
109288eca020SRusty Russell 	return ret;
109388eca020SRusty Russell }
109488eca020SRusty Russell EXPORT_SYMBOL(ida_simple_get);
109588eca020SRusty Russell 
109688eca020SRusty Russell /**
109788eca020SRusty Russell  * ida_simple_remove - remove an allocated id.
109888eca020SRusty Russell  * @ida: the (initialized) ida.
109988eca020SRusty Russell  * @id: the id returned by ida_simple_get.
110088eca020SRusty Russell  */
110188eca020SRusty Russell void ida_simple_remove(struct ida *ida, unsigned int id)
110288eca020SRusty Russell {
110346cbc1d3STejun Heo 	unsigned long flags;
110446cbc1d3STejun Heo 
110588eca020SRusty Russell 	BUG_ON((int)id < 0);
110646cbc1d3STejun Heo 	spin_lock_irqsave(&simple_ida_lock, flags);
110788eca020SRusty Russell 	ida_remove(ida, id);
110846cbc1d3STejun Heo 	spin_unlock_irqrestore(&simple_ida_lock, flags);
110988eca020SRusty Russell }
111088eca020SRusty Russell EXPORT_SYMBOL(ida_simple_remove);
111188eca020SRusty Russell 
111288eca020SRusty Russell /**
111372dba584STejun Heo  * ida_init - initialize ida handle
111472dba584STejun Heo  * @ida:	ida handle
111572dba584STejun Heo  *
111672dba584STejun Heo  * This function is use to set up the handle (@ida) that you will pass
111772dba584STejun Heo  * to the rest of the functions.
111872dba584STejun Heo  */
111972dba584STejun Heo void ida_init(struct ida *ida)
112072dba584STejun Heo {
112172dba584STejun Heo 	memset(ida, 0, sizeof(struct ida));
112272dba584STejun Heo 	idr_init(&ida->idr);
112372dba584STejun Heo 
112472dba584STejun Heo }
112572dba584STejun Heo EXPORT_SYMBOL(ida_init);
1126