xref: /openbmc/linux/lib/idr.c (revision 3594eb28)
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>
381da177e4SLinus Torvalds 
39e18b890bSChristoph Lameter static struct kmem_cache *idr_layer_cache;
4088eca020SRusty Russell static DEFINE_SPINLOCK(simple_ida_lock);
411da177e4SLinus Torvalds 
424ae53789SNadia Derbey static struct idr_layer *get_from_free_list(struct idr *idp)
431da177e4SLinus Torvalds {
441da177e4SLinus Torvalds 	struct idr_layer *p;
45c259cc28SRoland Dreier 	unsigned long flags;
461da177e4SLinus Torvalds 
47c259cc28SRoland Dreier 	spin_lock_irqsave(&idp->lock, flags);
481da177e4SLinus Torvalds 	if ((p = idp->id_free)) {
491da177e4SLinus Torvalds 		idp->id_free = p->ary[0];
501da177e4SLinus Torvalds 		idp->id_free_cnt--;
511da177e4SLinus Torvalds 		p->ary[0] = NULL;
521da177e4SLinus Torvalds 	}
53c259cc28SRoland Dreier 	spin_unlock_irqrestore(&idp->lock, flags);
541da177e4SLinus Torvalds 	return(p);
551da177e4SLinus Torvalds }
561da177e4SLinus Torvalds 
57cf481c20SNadia Derbey static void idr_layer_rcu_free(struct rcu_head *head)
58cf481c20SNadia Derbey {
59cf481c20SNadia Derbey 	struct idr_layer *layer;
60cf481c20SNadia Derbey 
61cf481c20SNadia Derbey 	layer = container_of(head, struct idr_layer, rcu_head);
62cf481c20SNadia Derbey 	kmem_cache_free(idr_layer_cache, layer);
63cf481c20SNadia Derbey }
64cf481c20SNadia Derbey 
65cf481c20SNadia Derbey static inline void free_layer(struct idr_layer *p)
66cf481c20SNadia Derbey {
67cf481c20SNadia Derbey 	call_rcu(&p->rcu_head, idr_layer_rcu_free);
68cf481c20SNadia Derbey }
69cf481c20SNadia Derbey 
701eec0056SSonny Rao /* only called when idp->lock is held */
714ae53789SNadia Derbey static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
721eec0056SSonny Rao {
731eec0056SSonny Rao 	p->ary[0] = idp->id_free;
741eec0056SSonny Rao 	idp->id_free = p;
751eec0056SSonny Rao 	idp->id_free_cnt++;
761eec0056SSonny Rao }
771eec0056SSonny Rao 
784ae53789SNadia Derbey static void move_to_free_list(struct idr *idp, struct idr_layer *p)
791da177e4SLinus Torvalds {
80c259cc28SRoland Dreier 	unsigned long flags;
81c259cc28SRoland Dreier 
821da177e4SLinus Torvalds 	/*
831da177e4SLinus Torvalds 	 * Depends on the return element being zeroed.
841da177e4SLinus Torvalds 	 */
85c259cc28SRoland Dreier 	spin_lock_irqsave(&idp->lock, flags);
864ae53789SNadia Derbey 	__move_to_free_list(idp, p);
87c259cc28SRoland Dreier 	spin_unlock_irqrestore(&idp->lock, flags);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
90e33ac8bdSTejun Heo static void idr_mark_full(struct idr_layer **pa, int id)
91e33ac8bdSTejun Heo {
92e33ac8bdSTejun Heo 	struct idr_layer *p = pa[0];
93e33ac8bdSTejun Heo 	int l = 0;
94e33ac8bdSTejun Heo 
95e33ac8bdSTejun Heo 	__set_bit(id & IDR_MASK, &p->bitmap);
96e33ac8bdSTejun Heo 	/*
97e33ac8bdSTejun Heo 	 * If this layer is full mark the bit in the layer above to
98e33ac8bdSTejun Heo 	 * show that this part of the radix tree is full.  This may
99e33ac8bdSTejun Heo 	 * complete the layer above and require walking up the radix
100e33ac8bdSTejun Heo 	 * tree.
101e33ac8bdSTejun Heo 	 */
102e33ac8bdSTejun Heo 	while (p->bitmap == IDR_FULL) {
103e33ac8bdSTejun Heo 		if (!(p = pa[++l]))
104e33ac8bdSTejun Heo 			break;
105e33ac8bdSTejun Heo 		id = id >> IDR_BITS;
106e33ac8bdSTejun Heo 		__set_bit((id & IDR_MASK), &p->bitmap);
107e33ac8bdSTejun Heo 	}
108e33ac8bdSTejun Heo }
109e33ac8bdSTejun Heo 
1101da177e4SLinus Torvalds /**
11156083ab1SRandy Dunlap  * idr_pre_get - reserve resources for idr allocation
1121da177e4SLinus Torvalds  * @idp:	idr handle
1131da177e4SLinus Torvalds  * @gfp_mask:	memory allocation flags
1141da177e4SLinus Torvalds  *
115066a9be6SNaohiro Aota  * This function should be called prior to calling the idr_get_new* functions.
116066a9be6SNaohiro Aota  * It preallocates enough memory to satisfy the worst possible allocation. The
117066a9be6SNaohiro Aota  * caller should pass in GFP_KERNEL if possible.  This of course requires that
118066a9be6SNaohiro Aota  * no spinning locks be held.
1191da177e4SLinus Torvalds  *
12056083ab1SRandy Dunlap  * If the system is REALLY out of memory this function returns %0,
12156083ab1SRandy Dunlap  * otherwise %1.
1221da177e4SLinus Torvalds  */
123fd4f2df2SAl Viro int idr_pre_get(struct idr *idp, gfp_t gfp_mask)
1241da177e4SLinus Torvalds {
125125c4c70SFengguang Wu 	while (idp->id_free_cnt < MAX_IDR_FREE) {
1261da177e4SLinus Torvalds 		struct idr_layer *new;
1275b019e99SAndrew Morton 		new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
1281da177e4SLinus Torvalds 		if (new == NULL)
1291da177e4SLinus Torvalds 			return (0);
1304ae53789SNadia Derbey 		move_to_free_list(idp, new);
1311da177e4SLinus Torvalds 	}
1321da177e4SLinus Torvalds 	return 1;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds EXPORT_SYMBOL(idr_pre_get);
1351da177e4SLinus Torvalds 
13612d1b439STejun Heo /**
13712d1b439STejun Heo  * sub_alloc - try to allocate an id without growing the tree depth
13812d1b439STejun Heo  * @idp: idr handle
13912d1b439STejun Heo  * @starting_id: id to start search at
14012d1b439STejun Heo  * @id: pointer to the allocated handle
14112d1b439STejun Heo  * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer
14212d1b439STejun Heo  *
14312d1b439STejun Heo  * Allocate an id in range [@starting_id, INT_MAX] from @idp without
14412d1b439STejun Heo  * growing its depth.  Returns
14512d1b439STejun Heo  *
14612d1b439STejun Heo  *  the allocated id >= 0 if successful,
14712d1b439STejun Heo  *  -EAGAIN if the tree needs to grow for allocation to succeed,
14812d1b439STejun Heo  *  -ENOSPC if the id space is exhausted,
14912d1b439STejun Heo  *  -ENOMEM if more idr_layers need to be allocated.
15012d1b439STejun Heo  */
151e33ac8bdSTejun Heo static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	int n, m, sh;
1541da177e4SLinus Torvalds 	struct idr_layer *p, *new;
1557aae6dd8STejun Heo 	int l, id, oid;
1565ba25331SAl Viro 	unsigned long bm;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	id = *starting_id;
1597aae6dd8STejun Heo  restart:
1601da177e4SLinus Torvalds 	p = idp->top;
1611da177e4SLinus Torvalds 	l = idp->layers;
1621da177e4SLinus Torvalds 	pa[l--] = NULL;
1631da177e4SLinus Torvalds 	while (1) {
1641da177e4SLinus Torvalds 		/*
1651da177e4SLinus Torvalds 		 * We run around this while until we reach the leaf node...
1661da177e4SLinus Torvalds 		 */
1671da177e4SLinus Torvalds 		n = (id >> (IDR_BITS*l)) & IDR_MASK;
1681da177e4SLinus Torvalds 		bm = ~p->bitmap;
1691da177e4SLinus Torvalds 		m = find_next_bit(&bm, IDR_SIZE, n);
1701da177e4SLinus Torvalds 		if (m == IDR_SIZE) {
1711da177e4SLinus Torvalds 			/* no space available go back to previous layer. */
1721da177e4SLinus Torvalds 			l++;
1737aae6dd8STejun Heo 			oid = id;
1741da177e4SLinus Torvalds 			id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
1757aae6dd8STejun Heo 
1767aae6dd8STejun Heo 			/* if already at the top layer, we need to grow */
177d2e7276bSTejun Heo 			if (id >= 1 << (idp->layers * IDR_BITS)) {
1781da177e4SLinus Torvalds 				*starting_id = id;
17912d1b439STejun Heo 				return -EAGAIN;
1801da177e4SLinus Torvalds 			}
181d2e7276bSTejun Heo 			p = pa[l];
182d2e7276bSTejun Heo 			BUG_ON(!p);
1837aae6dd8STejun Heo 
1847aae6dd8STejun Heo 			/* If we need to go up one layer, continue the
1857aae6dd8STejun Heo 			 * loop; otherwise, restart from the top.
1867aae6dd8STejun Heo 			 */
1877aae6dd8STejun Heo 			sh = IDR_BITS * (l + 1);
1887aae6dd8STejun Heo 			if (oid >> sh == id >> sh)
1891da177e4SLinus Torvalds 				continue;
1907aae6dd8STejun Heo 			else
1917aae6dd8STejun Heo 				goto restart;
1921da177e4SLinus Torvalds 		}
1931da177e4SLinus Torvalds 		if (m != n) {
1941da177e4SLinus Torvalds 			sh = IDR_BITS*l;
1951da177e4SLinus Torvalds 			id = ((id >> sh) ^ n ^ m) << sh;
1961da177e4SLinus Torvalds 		}
197125c4c70SFengguang Wu 		if ((id >= MAX_IDR_BIT) || (id < 0))
19812d1b439STejun Heo 			return -ENOSPC;
1991da177e4SLinus Torvalds 		if (l == 0)
2001da177e4SLinus Torvalds 			break;
2011da177e4SLinus Torvalds 		/*
2021da177e4SLinus Torvalds 		 * Create the layer below if it is missing.
2031da177e4SLinus Torvalds 		 */
2041da177e4SLinus Torvalds 		if (!p->ary[m]) {
2054ae53789SNadia Derbey 			new = get_from_free_list(idp);
2064ae53789SNadia Derbey 			if (!new)
20712d1b439STejun Heo 				return -ENOMEM;
2086ff2d39bSManfred Spraul 			new->layer = l-1;
2093219b3b7SNadia Derbey 			rcu_assign_pointer(p->ary[m], new);
2101da177e4SLinus Torvalds 			p->count++;
2111da177e4SLinus Torvalds 		}
2121da177e4SLinus Torvalds 		pa[l--] = p;
2131da177e4SLinus Torvalds 		p = p->ary[m];
2141da177e4SLinus Torvalds 	}
215e33ac8bdSTejun Heo 
216e33ac8bdSTejun Heo 	pa[l] = p;
217e33ac8bdSTejun Heo 	return id;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
220e33ac8bdSTejun Heo static int idr_get_empty_slot(struct idr *idp, int starting_id,
221e33ac8bdSTejun Heo 			      struct idr_layer **pa)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	struct idr_layer *p, *new;
2241da177e4SLinus Torvalds 	int layers, v, id;
225c259cc28SRoland Dreier 	unsigned long flags;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	id = starting_id;
2281da177e4SLinus Torvalds build_up:
2291da177e4SLinus Torvalds 	p = idp->top;
2301da177e4SLinus Torvalds 	layers = idp->layers;
2311da177e4SLinus Torvalds 	if (unlikely(!p)) {
2324ae53789SNadia Derbey 		if (!(p = get_from_free_list(idp)))
23312d1b439STejun Heo 			return -ENOMEM;
2346ff2d39bSManfred Spraul 		p->layer = 0;
2351da177e4SLinus Torvalds 		layers = 1;
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds 	/*
2381da177e4SLinus Torvalds 	 * Add a new layer to the top of the tree if the requested
2391da177e4SLinus Torvalds 	 * id is larger than the currently allocated space.
2401da177e4SLinus Torvalds 	 */
241125c4c70SFengguang Wu 	while ((layers < (MAX_IDR_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
2421da177e4SLinus Torvalds 		layers++;
243711a49a0SManfred Spraul 		if (!p->count) {
244711a49a0SManfred Spraul 			/* special case: if the tree is currently empty,
245711a49a0SManfred Spraul 			 * then we grow the tree by moving the top node
246711a49a0SManfred Spraul 			 * upwards.
247711a49a0SManfred Spraul 			 */
248711a49a0SManfred Spraul 			p->layer++;
2491da177e4SLinus Torvalds 			continue;
250711a49a0SManfred Spraul 		}
2514ae53789SNadia Derbey 		if (!(new = get_from_free_list(idp))) {
2521da177e4SLinus Torvalds 			/*
2531da177e4SLinus Torvalds 			 * The allocation failed.  If we built part of
2541da177e4SLinus Torvalds 			 * the structure tear it down.
2551da177e4SLinus Torvalds 			 */
256c259cc28SRoland Dreier 			spin_lock_irqsave(&idp->lock, flags);
2571da177e4SLinus Torvalds 			for (new = p; p && p != idp->top; new = p) {
2581da177e4SLinus Torvalds 				p = p->ary[0];
2591da177e4SLinus Torvalds 				new->ary[0] = NULL;
2601da177e4SLinus Torvalds 				new->bitmap = new->count = 0;
2614ae53789SNadia Derbey 				__move_to_free_list(idp, new);
2621da177e4SLinus Torvalds 			}
263c259cc28SRoland Dreier 			spin_unlock_irqrestore(&idp->lock, flags);
26412d1b439STejun Heo 			return -ENOMEM;
2651da177e4SLinus Torvalds 		}
2661da177e4SLinus Torvalds 		new->ary[0] = p;
2671da177e4SLinus Torvalds 		new->count = 1;
2686ff2d39bSManfred Spraul 		new->layer = layers-1;
2691da177e4SLinus Torvalds 		if (p->bitmap == IDR_FULL)
2701da177e4SLinus Torvalds 			__set_bit(0, &new->bitmap);
2711da177e4SLinus Torvalds 		p = new;
2721da177e4SLinus Torvalds 	}
2733219b3b7SNadia Derbey 	rcu_assign_pointer(idp->top, p);
2741da177e4SLinus Torvalds 	idp->layers = layers;
275e33ac8bdSTejun Heo 	v = sub_alloc(idp, &id, pa);
27612d1b439STejun Heo 	if (v == -EAGAIN)
2771da177e4SLinus Torvalds 		goto build_up;
2781da177e4SLinus Torvalds 	return(v);
2791da177e4SLinus Torvalds }
2801da177e4SLinus Torvalds 
281e33ac8bdSTejun Heo /*
2823594eb28STejun Heo  * @id and @pa are from a successful allocation from idr_get_empty_slot().
2833594eb28STejun Heo  * Install the user pointer @ptr and mark the slot full.
284e33ac8bdSTejun Heo  */
2853594eb28STejun Heo static void idr_fill_slot(void *ptr, int id, struct idr_layer **pa)
2863594eb28STejun Heo {
2873594eb28STejun Heo 	rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
288e33ac8bdSTejun Heo 	pa[0]->count++;
289e33ac8bdSTejun Heo 	idr_mark_full(pa, id);
290e33ac8bdSTejun Heo }
291e33ac8bdSTejun Heo 
2921da177e4SLinus Torvalds /**
2937c657f2fSJohn McCutchan  * idr_get_new_above - allocate new idr entry above or equal to a start id
2941da177e4SLinus Torvalds  * @idp: idr handle
29594e2bd68SThadeu Lima de Souza Cascardo  * @ptr: pointer you want associated with the id
296ea24ea85SNaohiro Aota  * @starting_id: id to start search at
2971da177e4SLinus Torvalds  * @id: pointer to the allocated handle
2981da177e4SLinus Torvalds  *
2991da177e4SLinus Torvalds  * This is the allocate id function.  It should be called with any
3001da177e4SLinus Torvalds  * required locks.
3011da177e4SLinus Torvalds  *
302066a9be6SNaohiro Aota  * If allocation from IDR's private freelist fails, idr_get_new_above() will
30356083ab1SRandy Dunlap  * return %-EAGAIN.  The caller should retry the idr_pre_get() call to refill
304066a9be6SNaohiro Aota  * IDR's preallocation and then retry the idr_get_new_above() call.
305066a9be6SNaohiro Aota  *
30656083ab1SRandy Dunlap  * If the idr is full idr_get_new_above() will return %-ENOSPC.
3071da177e4SLinus Torvalds  *
30856083ab1SRandy Dunlap  * @id returns a value in the range @starting_id ... %0x7fffffff
3091da177e4SLinus Torvalds  */
3101da177e4SLinus Torvalds int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
3111da177e4SLinus Torvalds {
3123594eb28STejun Heo 	struct idr_layer *pa[MAX_IDR_LEVEL];
3131da177e4SLinus Torvalds 	int rv;
314e15ae2ddSJesper Juhl 
3153594eb28STejun Heo 	rv = idr_get_empty_slot(idp, starting_id, pa);
316944ca05cSNadia Derbey 	if (rv < 0)
31712d1b439STejun Heo 		return rv == -ENOMEM ? -EAGAIN : rv;
3183594eb28STejun Heo 
3193594eb28STejun Heo 	idr_fill_slot(ptr, rv, pa);
3201da177e4SLinus Torvalds 	*id = rv;
3211da177e4SLinus Torvalds 	return 0;
3221da177e4SLinus Torvalds }
3231da177e4SLinus Torvalds EXPORT_SYMBOL(idr_get_new_above);
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static void idr_remove_warning(int id)
3261da177e4SLinus Torvalds {
327f098ad65SNadia Derbey 	printk(KERN_WARNING
328f098ad65SNadia Derbey 		"idr_remove called for id=%d which is not allocated.\n", id);
3291da177e4SLinus Torvalds 	dump_stack();
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds static void sub_remove(struct idr *idp, int shift, int id)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct idr_layer *p = idp->top;
335125c4c70SFengguang Wu 	struct idr_layer **pa[MAX_IDR_LEVEL];
3361da177e4SLinus Torvalds 	struct idr_layer ***paa = &pa[0];
337cf481c20SNadia Derbey 	struct idr_layer *to_free;
3381da177e4SLinus Torvalds 	int n;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	*paa = NULL;
3411da177e4SLinus Torvalds 	*++paa = &idp->top;
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	while ((shift > 0) && p) {
3441da177e4SLinus Torvalds 		n = (id >> shift) & IDR_MASK;
3451da177e4SLinus Torvalds 		__clear_bit(n, &p->bitmap);
3461da177e4SLinus Torvalds 		*++paa = &p->ary[n];
3471da177e4SLinus Torvalds 		p = p->ary[n];
3481da177e4SLinus Torvalds 		shift -= IDR_BITS;
3491da177e4SLinus Torvalds 	}
3501da177e4SLinus Torvalds 	n = id & IDR_MASK;
3511da177e4SLinus Torvalds 	if (likely(p != NULL && test_bit(n, &p->bitmap))){
3521da177e4SLinus Torvalds 		__clear_bit(n, &p->bitmap);
353cf481c20SNadia Derbey 		rcu_assign_pointer(p->ary[n], NULL);
354cf481c20SNadia Derbey 		to_free = NULL;
3551da177e4SLinus Torvalds 		while(*paa && ! --((**paa)->count)){
356cf481c20SNadia Derbey 			if (to_free)
357cf481c20SNadia Derbey 				free_layer(to_free);
358cf481c20SNadia Derbey 			to_free = **paa;
3591da177e4SLinus Torvalds 			**paa-- = NULL;
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 		if (!*paa)
3621da177e4SLinus Torvalds 			idp->layers = 0;
363cf481c20SNadia Derbey 		if (to_free)
364cf481c20SNadia Derbey 			free_layer(to_free);
365e15ae2ddSJesper Juhl 	} else
3661da177e4SLinus Torvalds 		idr_remove_warning(id);
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds /**
37056083ab1SRandy Dunlap  * idr_remove - remove the given id and free its slot
37172fd4a35SRobert P. J. Day  * @idp: idr handle
37272fd4a35SRobert P. J. Day  * @id: unique key
3731da177e4SLinus Torvalds  */
3741da177e4SLinus Torvalds void idr_remove(struct idr *idp, int id)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds 	struct idr_layer *p;
377cf481c20SNadia Derbey 	struct idr_layer *to_free;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	/* Mask off upper bits we don't use for the search. */
380125c4c70SFengguang Wu 	id &= MAX_IDR_MASK;
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds 	sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
383e15ae2ddSJesper Juhl 	if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
384cf481c20SNadia Derbey 	    idp->top->ary[0]) {
385cf481c20SNadia Derbey 		/*
386cf481c20SNadia Derbey 		 * Single child at leftmost slot: we can shrink the tree.
387cf481c20SNadia Derbey 		 * This level is not needed anymore since when layers are
388cf481c20SNadia Derbey 		 * inserted, they are inserted at the top of the existing
389cf481c20SNadia Derbey 		 * tree.
390cf481c20SNadia Derbey 		 */
391cf481c20SNadia Derbey 		to_free = idp->top;
3921da177e4SLinus Torvalds 		p = idp->top->ary[0];
393cf481c20SNadia Derbey 		rcu_assign_pointer(idp->top, p);
3941da177e4SLinus Torvalds 		--idp->layers;
395cf481c20SNadia Derbey 		to_free->bitmap = to_free->count = 0;
396cf481c20SNadia Derbey 		free_layer(to_free);
3971da177e4SLinus Torvalds 	}
398125c4c70SFengguang Wu 	while (idp->id_free_cnt >= MAX_IDR_FREE) {
3994ae53789SNadia Derbey 		p = get_from_free_list(idp);
400cf481c20SNadia Derbey 		/*
401cf481c20SNadia Derbey 		 * Note: we don't call the rcu callback here, since the only
402cf481c20SNadia Derbey 		 * layers that fall into the freelist are those that have been
403cf481c20SNadia Derbey 		 * preallocated.
404cf481c20SNadia Derbey 		 */
4051da177e4SLinus Torvalds 		kmem_cache_free(idr_layer_cache, p);
4061da177e4SLinus Torvalds 	}
407af8e2a4cSNadia Derbey 	return;
4081da177e4SLinus Torvalds }
4091da177e4SLinus Torvalds EXPORT_SYMBOL(idr_remove);
4101da177e4SLinus Torvalds 
411fe6e24ecSTejun Heo void __idr_remove_all(struct idr *idp)
41223936cc0SKristian Hoegsberg {
4136ace06dcSOleg Nesterov 	int n, id, max;
4142dcb22b3SImre Deak 	int bt_mask;
41523936cc0SKristian Hoegsberg 	struct idr_layer *p;
416125c4c70SFengguang Wu 	struct idr_layer *pa[MAX_IDR_LEVEL];
41723936cc0SKristian Hoegsberg 	struct idr_layer **paa = &pa[0];
41823936cc0SKristian Hoegsberg 
41923936cc0SKristian Hoegsberg 	n = idp->layers * IDR_BITS;
42023936cc0SKristian Hoegsberg 	p = idp->top;
4211b23336aSPaul E. McKenney 	rcu_assign_pointer(idp->top, NULL);
42223936cc0SKristian Hoegsberg 	max = 1 << n;
42323936cc0SKristian Hoegsberg 
42423936cc0SKristian Hoegsberg 	id = 0;
4256ace06dcSOleg Nesterov 	while (id < max) {
42623936cc0SKristian Hoegsberg 		while (n > IDR_BITS && p) {
42723936cc0SKristian Hoegsberg 			n -= IDR_BITS;
42823936cc0SKristian Hoegsberg 			*paa++ = p;
42923936cc0SKristian Hoegsberg 			p = p->ary[(id >> n) & IDR_MASK];
43023936cc0SKristian Hoegsberg 		}
43123936cc0SKristian Hoegsberg 
4322dcb22b3SImre Deak 		bt_mask = id;
43323936cc0SKristian Hoegsberg 		id += 1 << n;
4342dcb22b3SImre Deak 		/* Get the highest bit that the above add changed from 0->1. */
4352dcb22b3SImre Deak 		while (n < fls(id ^ bt_mask)) {
436cf481c20SNadia Derbey 			if (p)
437cf481c20SNadia Derbey 				free_layer(p);
43823936cc0SKristian Hoegsberg 			n += IDR_BITS;
43923936cc0SKristian Hoegsberg 			p = *--paa;
44023936cc0SKristian Hoegsberg 		}
44123936cc0SKristian Hoegsberg 	}
44223936cc0SKristian Hoegsberg 	idp->layers = 0;
44323936cc0SKristian Hoegsberg }
444fe6e24ecSTejun Heo EXPORT_SYMBOL(__idr_remove_all);
44523936cc0SKristian Hoegsberg 
44623936cc0SKristian Hoegsberg /**
4478d3b3591SAndrew Morton  * idr_destroy - release all cached layers within an idr tree
448ea24ea85SNaohiro Aota  * @idp: idr handle
4499bb26bc1STejun Heo  *
4509bb26bc1STejun Heo  * Free all id mappings and all idp_layers.  After this function, @idp is
4519bb26bc1STejun Heo  * completely unused and can be freed / recycled.  The caller is
4529bb26bc1STejun Heo  * responsible for ensuring that no one else accesses @idp during or after
4539bb26bc1STejun Heo  * idr_destroy().
4549bb26bc1STejun Heo  *
4559bb26bc1STejun Heo  * A typical clean-up sequence for objects stored in an idr tree will use
4569bb26bc1STejun Heo  * idr_for_each() to free all objects, if necessay, then idr_destroy() to
4579bb26bc1STejun Heo  * free up the id mappings and cached idr_layers.
4588d3b3591SAndrew Morton  */
4598d3b3591SAndrew Morton void idr_destroy(struct idr *idp)
4608d3b3591SAndrew Morton {
461fe6e24ecSTejun Heo 	__idr_remove_all(idp);
4629bb26bc1STejun Heo 
4638d3b3591SAndrew Morton 	while (idp->id_free_cnt) {
4644ae53789SNadia Derbey 		struct idr_layer *p = get_from_free_list(idp);
4658d3b3591SAndrew Morton 		kmem_cache_free(idr_layer_cache, p);
4668d3b3591SAndrew Morton 	}
4678d3b3591SAndrew Morton }
4688d3b3591SAndrew Morton EXPORT_SYMBOL(idr_destroy);
4698d3b3591SAndrew Morton 
4708d3b3591SAndrew Morton /**
4711da177e4SLinus Torvalds  * idr_find - return pointer for given id
4721da177e4SLinus Torvalds  * @idp: idr handle
4731da177e4SLinus Torvalds  * @id: lookup key
4741da177e4SLinus Torvalds  *
4751da177e4SLinus Torvalds  * Return the pointer given the id it has been registered with.  A %NULL
4761da177e4SLinus Torvalds  * return indicates that @id is not valid or you passed %NULL in
4771da177e4SLinus Torvalds  * idr_get_new().
4781da177e4SLinus Torvalds  *
479f9c46d6eSNadia Derbey  * This function can be called under rcu_read_lock(), given that the leaf
480f9c46d6eSNadia Derbey  * pointers lifetimes are correctly managed.
4811da177e4SLinus Torvalds  */
4821da177e4SLinus Torvalds void *idr_find(struct idr *idp, int id)
4831da177e4SLinus Torvalds {
4841da177e4SLinus Torvalds 	int n;
4851da177e4SLinus Torvalds 	struct idr_layer *p;
4861da177e4SLinus Torvalds 
48796be753aSPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
4886ff2d39bSManfred Spraul 	if (!p)
4896ff2d39bSManfred Spraul 		return NULL;
4906ff2d39bSManfred Spraul 	n = (p->layer+1) * IDR_BITS;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	/* Mask off upper bits we don't use for the search. */
493125c4c70SFengguang Wu 	id &= MAX_IDR_MASK;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	if (id >= (1 << n))
4961da177e4SLinus Torvalds 		return NULL;
4976ff2d39bSManfred Spraul 	BUG_ON(n == 0);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	while (n > 0 && p) {
5001da177e4SLinus Torvalds 		n -= IDR_BITS;
5016ff2d39bSManfred Spraul 		BUG_ON(n != p->layer*IDR_BITS);
50296be753aSPaul E. McKenney 		p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 	return((void *)p);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds EXPORT_SYMBOL(idr_find);
5071da177e4SLinus Torvalds 
5085806f07cSJeff Mahoney /**
50996d7fa42SKristian Hoegsberg  * idr_for_each - iterate through all stored pointers
51096d7fa42SKristian Hoegsberg  * @idp: idr handle
51196d7fa42SKristian Hoegsberg  * @fn: function to be called for each pointer
51296d7fa42SKristian Hoegsberg  * @data: data passed back to callback function
51396d7fa42SKristian Hoegsberg  *
51496d7fa42SKristian Hoegsberg  * Iterate over the pointers registered with the given idr.  The
51596d7fa42SKristian Hoegsberg  * callback function will be called for each pointer currently
51696d7fa42SKristian Hoegsberg  * registered, passing the id, the pointer and the data pointer passed
51796d7fa42SKristian Hoegsberg  * to this function.  It is not safe to modify the idr tree while in
51896d7fa42SKristian Hoegsberg  * the callback, so functions such as idr_get_new and idr_remove are
51996d7fa42SKristian Hoegsberg  * not allowed.
52096d7fa42SKristian Hoegsberg  *
52196d7fa42SKristian Hoegsberg  * We check the return of @fn each time. If it returns anything other
52256083ab1SRandy Dunlap  * than %0, we break out and return that value.
52396d7fa42SKristian Hoegsberg  *
52496d7fa42SKristian Hoegsberg  * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
52596d7fa42SKristian Hoegsberg  */
52696d7fa42SKristian Hoegsberg int idr_for_each(struct idr *idp,
52796d7fa42SKristian Hoegsberg 		 int (*fn)(int id, void *p, void *data), void *data)
52896d7fa42SKristian Hoegsberg {
52996d7fa42SKristian Hoegsberg 	int n, id, max, error = 0;
53096d7fa42SKristian Hoegsberg 	struct idr_layer *p;
531125c4c70SFengguang Wu 	struct idr_layer *pa[MAX_IDR_LEVEL];
53296d7fa42SKristian Hoegsberg 	struct idr_layer **paa = &pa[0];
53396d7fa42SKristian Hoegsberg 
53496d7fa42SKristian Hoegsberg 	n = idp->layers * IDR_BITS;
53596be753aSPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
53696d7fa42SKristian Hoegsberg 	max = 1 << n;
53796d7fa42SKristian Hoegsberg 
53896d7fa42SKristian Hoegsberg 	id = 0;
53996d7fa42SKristian Hoegsberg 	while (id < max) {
54096d7fa42SKristian Hoegsberg 		while (n > 0 && p) {
54196d7fa42SKristian Hoegsberg 			n -= IDR_BITS;
54296d7fa42SKristian Hoegsberg 			*paa++ = p;
54396be753aSPaul E. McKenney 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
54496d7fa42SKristian Hoegsberg 		}
54596d7fa42SKristian Hoegsberg 
54696d7fa42SKristian Hoegsberg 		if (p) {
54796d7fa42SKristian Hoegsberg 			error = fn(id, (void *)p, data);
54896d7fa42SKristian Hoegsberg 			if (error)
54996d7fa42SKristian Hoegsberg 				break;
55096d7fa42SKristian Hoegsberg 		}
55196d7fa42SKristian Hoegsberg 
55296d7fa42SKristian Hoegsberg 		id += 1 << n;
55396d7fa42SKristian Hoegsberg 		while (n < fls(id)) {
55496d7fa42SKristian Hoegsberg 			n += IDR_BITS;
55596d7fa42SKristian Hoegsberg 			p = *--paa;
55696d7fa42SKristian Hoegsberg 		}
55796d7fa42SKristian Hoegsberg 	}
55896d7fa42SKristian Hoegsberg 
55996d7fa42SKristian Hoegsberg 	return error;
56096d7fa42SKristian Hoegsberg }
56196d7fa42SKristian Hoegsberg EXPORT_SYMBOL(idr_for_each);
56296d7fa42SKristian Hoegsberg 
56396d7fa42SKristian Hoegsberg /**
56438460b48SKAMEZAWA Hiroyuki  * idr_get_next - lookup next object of id to given id.
56538460b48SKAMEZAWA Hiroyuki  * @idp: idr handle
566ea24ea85SNaohiro Aota  * @nextidp:  pointer to lookup key
56738460b48SKAMEZAWA Hiroyuki  *
56838460b48SKAMEZAWA Hiroyuki  * Returns pointer to registered object with id, which is next number to
5691458ce16SNaohiro Aota  * given id. After being looked up, *@nextidp will be updated for the next
5701458ce16SNaohiro Aota  * iteration.
5719f7de827SHugh Dickins  *
5729f7de827SHugh Dickins  * This function can be called under rcu_read_lock(), given that the leaf
5739f7de827SHugh Dickins  * pointers lifetimes are correctly managed.
57438460b48SKAMEZAWA Hiroyuki  */
57538460b48SKAMEZAWA Hiroyuki void *idr_get_next(struct idr *idp, int *nextidp)
57638460b48SKAMEZAWA Hiroyuki {
577125c4c70SFengguang Wu 	struct idr_layer *p, *pa[MAX_IDR_LEVEL];
57838460b48SKAMEZAWA Hiroyuki 	struct idr_layer **paa = &pa[0];
57938460b48SKAMEZAWA Hiroyuki 	int id = *nextidp;
58038460b48SKAMEZAWA Hiroyuki 	int n, max;
58138460b48SKAMEZAWA Hiroyuki 
58238460b48SKAMEZAWA Hiroyuki 	/* find first ent */
58394bfa3b6SPaul E. McKenney 	p = rcu_dereference_raw(idp->top);
58438460b48SKAMEZAWA Hiroyuki 	if (!p)
58538460b48SKAMEZAWA Hiroyuki 		return NULL;
5869f7de827SHugh Dickins 	n = (p->layer + 1) * IDR_BITS;
5879f7de827SHugh Dickins 	max = 1 << n;
58838460b48SKAMEZAWA Hiroyuki 
58938460b48SKAMEZAWA Hiroyuki 	while (id < max) {
59038460b48SKAMEZAWA Hiroyuki 		while (n > 0 && p) {
59138460b48SKAMEZAWA Hiroyuki 			n -= IDR_BITS;
59238460b48SKAMEZAWA Hiroyuki 			*paa++ = p;
59394bfa3b6SPaul E. McKenney 			p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
59438460b48SKAMEZAWA Hiroyuki 		}
59538460b48SKAMEZAWA Hiroyuki 
59638460b48SKAMEZAWA Hiroyuki 		if (p) {
59738460b48SKAMEZAWA Hiroyuki 			*nextidp = id;
59838460b48SKAMEZAWA Hiroyuki 			return p;
59938460b48SKAMEZAWA Hiroyuki 		}
60038460b48SKAMEZAWA Hiroyuki 
6016cdae741STejun Heo 		/*
6026cdae741STejun Heo 		 * Proceed to the next layer at the current level.  Unlike
6036cdae741STejun Heo 		 * idr_for_each(), @id isn't guaranteed to be aligned to
6046cdae741STejun Heo 		 * layer boundary at this point and adding 1 << n may
6056cdae741STejun Heo 		 * incorrectly skip IDs.  Make sure we jump to the
6066cdae741STejun Heo 		 * beginning of the next layer using round_up().
6076cdae741STejun Heo 		 */
6086cdae741STejun Heo 		id = round_up(id + 1, 1 << n);
60938460b48SKAMEZAWA Hiroyuki 		while (n < fls(id)) {
61038460b48SKAMEZAWA Hiroyuki 			n += IDR_BITS;
61138460b48SKAMEZAWA Hiroyuki 			p = *--paa;
61238460b48SKAMEZAWA Hiroyuki 		}
61338460b48SKAMEZAWA Hiroyuki 	}
61438460b48SKAMEZAWA Hiroyuki 	return NULL;
61538460b48SKAMEZAWA Hiroyuki }
6164d1ee80fSBen Hutchings EXPORT_SYMBOL(idr_get_next);
61738460b48SKAMEZAWA Hiroyuki 
61838460b48SKAMEZAWA Hiroyuki 
61938460b48SKAMEZAWA Hiroyuki /**
6205806f07cSJeff Mahoney  * idr_replace - replace pointer for given id
6215806f07cSJeff Mahoney  * @idp: idr handle
6225806f07cSJeff Mahoney  * @ptr: pointer you want associated with the id
6235806f07cSJeff Mahoney  * @id: lookup key
6245806f07cSJeff Mahoney  *
6255806f07cSJeff Mahoney  * Replace the pointer registered with an id and return the old value.
62656083ab1SRandy Dunlap  * A %-ENOENT return indicates that @id was not found.
62756083ab1SRandy Dunlap  * A %-EINVAL return indicates that @id was not within valid constraints.
6285806f07cSJeff Mahoney  *
629cf481c20SNadia Derbey  * The caller must serialize with writers.
6305806f07cSJeff Mahoney  */
6315806f07cSJeff Mahoney void *idr_replace(struct idr *idp, void *ptr, int id)
6325806f07cSJeff Mahoney {
6335806f07cSJeff Mahoney 	int n;
6345806f07cSJeff Mahoney 	struct idr_layer *p, *old_p;
6355806f07cSJeff Mahoney 
6365806f07cSJeff Mahoney 	p = idp->top;
6376ff2d39bSManfred Spraul 	if (!p)
6386ff2d39bSManfred Spraul 		return ERR_PTR(-EINVAL);
6396ff2d39bSManfred Spraul 
6406ff2d39bSManfred Spraul 	n = (p->layer+1) * IDR_BITS;
6415806f07cSJeff Mahoney 
642125c4c70SFengguang Wu 	id &= MAX_IDR_MASK;
6435806f07cSJeff Mahoney 
6445806f07cSJeff Mahoney 	if (id >= (1 << n))
6455806f07cSJeff Mahoney 		return ERR_PTR(-EINVAL);
6465806f07cSJeff Mahoney 
6475806f07cSJeff Mahoney 	n -= IDR_BITS;
6485806f07cSJeff Mahoney 	while ((n > 0) && p) {
6495806f07cSJeff Mahoney 		p = p->ary[(id >> n) & IDR_MASK];
6505806f07cSJeff Mahoney 		n -= IDR_BITS;
6515806f07cSJeff Mahoney 	}
6525806f07cSJeff Mahoney 
6535806f07cSJeff Mahoney 	n = id & IDR_MASK;
6545806f07cSJeff Mahoney 	if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
6555806f07cSJeff Mahoney 		return ERR_PTR(-ENOENT);
6565806f07cSJeff Mahoney 
6575806f07cSJeff Mahoney 	old_p = p->ary[n];
658cf481c20SNadia Derbey 	rcu_assign_pointer(p->ary[n], ptr);
6595806f07cSJeff Mahoney 
6605806f07cSJeff Mahoney 	return old_p;
6615806f07cSJeff Mahoney }
6625806f07cSJeff Mahoney EXPORT_SYMBOL(idr_replace);
6635806f07cSJeff Mahoney 
664199f0ca5SAkinobu Mita void __init idr_init_cache(void)
6651da177e4SLinus Torvalds {
6661da177e4SLinus Torvalds 	idr_layer_cache = kmem_cache_create("idr_layer_cache",
6675b019e99SAndrew Morton 				sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds /**
6711da177e4SLinus Torvalds  * idr_init - initialize idr handle
6721da177e4SLinus Torvalds  * @idp:	idr handle
6731da177e4SLinus Torvalds  *
6741da177e4SLinus Torvalds  * This function is use to set up the handle (@idp) that you will pass
6751da177e4SLinus Torvalds  * to the rest of the functions.
6761da177e4SLinus Torvalds  */
6771da177e4SLinus Torvalds void idr_init(struct idr *idp)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	memset(idp, 0, sizeof(struct idr));
6801da177e4SLinus Torvalds 	spin_lock_init(&idp->lock);
6811da177e4SLinus Torvalds }
6821da177e4SLinus Torvalds EXPORT_SYMBOL(idr_init);
68372dba584STejun Heo 
68472dba584STejun Heo 
68556083ab1SRandy Dunlap /**
68656083ab1SRandy Dunlap  * DOC: IDA description
68772dba584STejun Heo  * IDA - IDR based ID allocator
68872dba584STejun Heo  *
68956083ab1SRandy Dunlap  * This is id allocator without id -> pointer translation.  Memory
69072dba584STejun Heo  * usage is much lower than full blown idr because each id only
69172dba584STejun Heo  * occupies a bit.  ida uses a custom leaf node which contains
69272dba584STejun Heo  * IDA_BITMAP_BITS slots.
69372dba584STejun Heo  *
69472dba584STejun Heo  * 2007-04-25  written by Tejun Heo <htejun@gmail.com>
69572dba584STejun Heo  */
69672dba584STejun Heo 
69772dba584STejun Heo static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
69872dba584STejun Heo {
69972dba584STejun Heo 	unsigned long flags;
70072dba584STejun Heo 
70172dba584STejun Heo 	if (!ida->free_bitmap) {
70272dba584STejun Heo 		spin_lock_irqsave(&ida->idr.lock, flags);
70372dba584STejun Heo 		if (!ida->free_bitmap) {
70472dba584STejun Heo 			ida->free_bitmap = bitmap;
70572dba584STejun Heo 			bitmap = NULL;
70672dba584STejun Heo 		}
70772dba584STejun Heo 		spin_unlock_irqrestore(&ida->idr.lock, flags);
70872dba584STejun Heo 	}
70972dba584STejun Heo 
71072dba584STejun Heo 	kfree(bitmap);
71172dba584STejun Heo }
71272dba584STejun Heo 
71372dba584STejun Heo /**
71472dba584STejun Heo  * ida_pre_get - reserve resources for ida allocation
71572dba584STejun Heo  * @ida:	ida handle
71672dba584STejun Heo  * @gfp_mask:	memory allocation flag
71772dba584STejun Heo  *
71872dba584STejun Heo  * This function should be called prior to locking and calling the
71972dba584STejun Heo  * following function.  It preallocates enough memory to satisfy the
72072dba584STejun Heo  * worst possible allocation.
72172dba584STejun Heo  *
72256083ab1SRandy Dunlap  * If the system is REALLY out of memory this function returns %0,
72356083ab1SRandy Dunlap  * otherwise %1.
72472dba584STejun Heo  */
72572dba584STejun Heo int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
72672dba584STejun Heo {
72772dba584STejun Heo 	/* allocate idr_layers */
72872dba584STejun Heo 	if (!idr_pre_get(&ida->idr, gfp_mask))
72972dba584STejun Heo 		return 0;
73072dba584STejun Heo 
73172dba584STejun Heo 	/* allocate free_bitmap */
73272dba584STejun Heo 	if (!ida->free_bitmap) {
73372dba584STejun Heo 		struct ida_bitmap *bitmap;
73472dba584STejun Heo 
73572dba584STejun Heo 		bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
73672dba584STejun Heo 		if (!bitmap)
73772dba584STejun Heo 			return 0;
73872dba584STejun Heo 
73972dba584STejun Heo 		free_bitmap(ida, bitmap);
74072dba584STejun Heo 	}
74172dba584STejun Heo 
74272dba584STejun Heo 	return 1;
74372dba584STejun Heo }
74472dba584STejun Heo EXPORT_SYMBOL(ida_pre_get);
74572dba584STejun Heo 
74672dba584STejun Heo /**
74772dba584STejun Heo  * ida_get_new_above - allocate new ID above or equal to a start id
74872dba584STejun Heo  * @ida:	ida handle
749ea24ea85SNaohiro Aota  * @starting_id: id to start search at
75072dba584STejun Heo  * @p_id:	pointer to the allocated handle
75172dba584STejun Heo  *
752e3816c54SWang Sheng-Hui  * Allocate new ID above or equal to @starting_id.  It should be called
753e3816c54SWang Sheng-Hui  * with any required locks.
75472dba584STejun Heo  *
75556083ab1SRandy Dunlap  * If memory is required, it will return %-EAGAIN, you should unlock
75672dba584STejun Heo  * and go back to the ida_pre_get() call.  If the ida is full, it will
75756083ab1SRandy Dunlap  * return %-ENOSPC.
75872dba584STejun Heo  *
75956083ab1SRandy Dunlap  * @p_id returns a value in the range @starting_id ... %0x7fffffff.
76072dba584STejun Heo  */
76172dba584STejun Heo int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
76272dba584STejun Heo {
763125c4c70SFengguang Wu 	struct idr_layer *pa[MAX_IDR_LEVEL];
76472dba584STejun Heo 	struct ida_bitmap *bitmap;
76572dba584STejun Heo 	unsigned long flags;
76672dba584STejun Heo 	int idr_id = starting_id / IDA_BITMAP_BITS;
76772dba584STejun Heo 	int offset = starting_id % IDA_BITMAP_BITS;
76872dba584STejun Heo 	int t, id;
76972dba584STejun Heo 
77072dba584STejun Heo  restart:
77172dba584STejun Heo 	/* get vacant slot */
77272dba584STejun Heo 	t = idr_get_empty_slot(&ida->idr, idr_id, pa);
773944ca05cSNadia Derbey 	if (t < 0)
77412d1b439STejun Heo 		return t == -ENOMEM ? -EAGAIN : t;
77572dba584STejun Heo 
776125c4c70SFengguang Wu 	if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT)
77772dba584STejun Heo 		return -ENOSPC;
77872dba584STejun Heo 
77972dba584STejun Heo 	if (t != idr_id)
78072dba584STejun Heo 		offset = 0;
78172dba584STejun Heo 	idr_id = t;
78272dba584STejun Heo 
78372dba584STejun Heo 	/* if bitmap isn't there, create a new one */
78472dba584STejun Heo 	bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
78572dba584STejun Heo 	if (!bitmap) {
78672dba584STejun Heo 		spin_lock_irqsave(&ida->idr.lock, flags);
78772dba584STejun Heo 		bitmap = ida->free_bitmap;
78872dba584STejun Heo 		ida->free_bitmap = NULL;
78972dba584STejun Heo 		spin_unlock_irqrestore(&ida->idr.lock, flags);
79072dba584STejun Heo 
79172dba584STejun Heo 		if (!bitmap)
79272dba584STejun Heo 			return -EAGAIN;
79372dba584STejun Heo 
79472dba584STejun Heo 		memset(bitmap, 0, sizeof(struct ida_bitmap));
7953219b3b7SNadia Derbey 		rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
7963219b3b7SNadia Derbey 				(void *)bitmap);
79772dba584STejun Heo 		pa[0]->count++;
79872dba584STejun Heo 	}
79972dba584STejun Heo 
80072dba584STejun Heo 	/* lookup for empty slot */
80172dba584STejun Heo 	t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
80272dba584STejun Heo 	if (t == IDA_BITMAP_BITS) {
80372dba584STejun Heo 		/* no empty slot after offset, continue to the next chunk */
80472dba584STejun Heo 		idr_id++;
80572dba584STejun Heo 		offset = 0;
80672dba584STejun Heo 		goto restart;
80772dba584STejun Heo 	}
80872dba584STejun Heo 
80972dba584STejun Heo 	id = idr_id * IDA_BITMAP_BITS + t;
810125c4c70SFengguang Wu 	if (id >= MAX_IDR_BIT)
81172dba584STejun Heo 		return -ENOSPC;
81272dba584STejun Heo 
81372dba584STejun Heo 	__set_bit(t, bitmap->bitmap);
81472dba584STejun Heo 	if (++bitmap->nr_busy == IDA_BITMAP_BITS)
81572dba584STejun Heo 		idr_mark_full(pa, idr_id);
81672dba584STejun Heo 
81772dba584STejun Heo 	*p_id = id;
81872dba584STejun Heo 
81972dba584STejun Heo 	/* Each leaf node can handle nearly a thousand slots and the
82072dba584STejun Heo 	 * whole idea of ida is to have small memory foot print.
82172dba584STejun Heo 	 * Throw away extra resources one by one after each successful
82272dba584STejun Heo 	 * allocation.
82372dba584STejun Heo 	 */
82472dba584STejun Heo 	if (ida->idr.id_free_cnt || ida->free_bitmap) {
8254ae53789SNadia Derbey 		struct idr_layer *p = get_from_free_list(&ida->idr);
82672dba584STejun Heo 		if (p)
82772dba584STejun Heo 			kmem_cache_free(idr_layer_cache, p);
82872dba584STejun Heo 	}
82972dba584STejun Heo 
83072dba584STejun Heo 	return 0;
83172dba584STejun Heo }
83272dba584STejun Heo EXPORT_SYMBOL(ida_get_new_above);
83372dba584STejun Heo 
83472dba584STejun Heo /**
83572dba584STejun Heo  * ida_remove - remove the given ID
83672dba584STejun Heo  * @ida:	ida handle
83772dba584STejun Heo  * @id:		ID to free
83872dba584STejun Heo  */
83972dba584STejun Heo void ida_remove(struct ida *ida, int id)
84072dba584STejun Heo {
84172dba584STejun Heo 	struct idr_layer *p = ida->idr.top;
84272dba584STejun Heo 	int shift = (ida->idr.layers - 1) * IDR_BITS;
84372dba584STejun Heo 	int idr_id = id / IDA_BITMAP_BITS;
84472dba584STejun Heo 	int offset = id % IDA_BITMAP_BITS;
84572dba584STejun Heo 	int n;
84672dba584STejun Heo 	struct ida_bitmap *bitmap;
84772dba584STejun Heo 
84872dba584STejun Heo 	/* clear full bits while looking up the leaf idr_layer */
84972dba584STejun Heo 	while ((shift > 0) && p) {
85072dba584STejun Heo 		n = (idr_id >> shift) & IDR_MASK;
85172dba584STejun Heo 		__clear_bit(n, &p->bitmap);
85272dba584STejun Heo 		p = p->ary[n];
85372dba584STejun Heo 		shift -= IDR_BITS;
85472dba584STejun Heo 	}
85572dba584STejun Heo 
85672dba584STejun Heo 	if (p == NULL)
85772dba584STejun Heo 		goto err;
85872dba584STejun Heo 
85972dba584STejun Heo 	n = idr_id & IDR_MASK;
86072dba584STejun Heo 	__clear_bit(n, &p->bitmap);
86172dba584STejun Heo 
86272dba584STejun Heo 	bitmap = (void *)p->ary[n];
86372dba584STejun Heo 	if (!test_bit(offset, bitmap->bitmap))
86472dba584STejun Heo 		goto err;
86572dba584STejun Heo 
86672dba584STejun Heo 	/* update bitmap and remove it if empty */
86772dba584STejun Heo 	__clear_bit(offset, bitmap->bitmap);
86872dba584STejun Heo 	if (--bitmap->nr_busy == 0) {
86972dba584STejun Heo 		__set_bit(n, &p->bitmap);	/* to please idr_remove() */
87072dba584STejun Heo 		idr_remove(&ida->idr, idr_id);
87172dba584STejun Heo 		free_bitmap(ida, bitmap);
87272dba584STejun Heo 	}
87372dba584STejun Heo 
87472dba584STejun Heo 	return;
87572dba584STejun Heo 
87672dba584STejun Heo  err:
87772dba584STejun Heo 	printk(KERN_WARNING
87872dba584STejun Heo 	       "ida_remove called for id=%d which is not allocated.\n", id);
87972dba584STejun Heo }
88072dba584STejun Heo EXPORT_SYMBOL(ida_remove);
88172dba584STejun Heo 
88272dba584STejun Heo /**
88372dba584STejun Heo  * ida_destroy - release all cached layers within an ida tree
884ea24ea85SNaohiro Aota  * @ida:		ida handle
88572dba584STejun Heo  */
88672dba584STejun Heo void ida_destroy(struct ida *ida)
88772dba584STejun Heo {
88872dba584STejun Heo 	idr_destroy(&ida->idr);
88972dba584STejun Heo 	kfree(ida->free_bitmap);
89072dba584STejun Heo }
89172dba584STejun Heo EXPORT_SYMBOL(ida_destroy);
89272dba584STejun Heo 
89372dba584STejun Heo /**
89488eca020SRusty Russell  * ida_simple_get - get a new id.
89588eca020SRusty Russell  * @ida: the (initialized) ida.
89688eca020SRusty Russell  * @start: the minimum id (inclusive, < 0x8000000)
89788eca020SRusty Russell  * @end: the maximum id (exclusive, < 0x8000000 or 0)
89888eca020SRusty Russell  * @gfp_mask: memory allocation flags
89988eca020SRusty Russell  *
90088eca020SRusty Russell  * Allocates an id in the range start <= id < end, or returns -ENOSPC.
90188eca020SRusty Russell  * On memory allocation failure, returns -ENOMEM.
90288eca020SRusty Russell  *
90388eca020SRusty Russell  * Use ida_simple_remove() to get rid of an id.
90488eca020SRusty Russell  */
90588eca020SRusty Russell int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
90688eca020SRusty Russell 		   gfp_t gfp_mask)
90788eca020SRusty Russell {
90888eca020SRusty Russell 	int ret, id;
90988eca020SRusty Russell 	unsigned int max;
91046cbc1d3STejun Heo 	unsigned long flags;
91188eca020SRusty Russell 
91288eca020SRusty Russell 	BUG_ON((int)start < 0);
91388eca020SRusty Russell 	BUG_ON((int)end < 0);
91488eca020SRusty Russell 
91588eca020SRusty Russell 	if (end == 0)
91688eca020SRusty Russell 		max = 0x80000000;
91788eca020SRusty Russell 	else {
91888eca020SRusty Russell 		BUG_ON(end < start);
91988eca020SRusty Russell 		max = end - 1;
92088eca020SRusty Russell 	}
92188eca020SRusty Russell 
92288eca020SRusty Russell again:
92388eca020SRusty Russell 	if (!ida_pre_get(ida, gfp_mask))
92488eca020SRusty Russell 		return -ENOMEM;
92588eca020SRusty Russell 
92646cbc1d3STejun Heo 	spin_lock_irqsave(&simple_ida_lock, flags);
92788eca020SRusty Russell 	ret = ida_get_new_above(ida, start, &id);
92888eca020SRusty Russell 	if (!ret) {
92988eca020SRusty Russell 		if (id > max) {
93088eca020SRusty Russell 			ida_remove(ida, id);
93188eca020SRusty Russell 			ret = -ENOSPC;
93288eca020SRusty Russell 		} else {
93388eca020SRusty Russell 			ret = id;
93488eca020SRusty Russell 		}
93588eca020SRusty Russell 	}
93646cbc1d3STejun Heo 	spin_unlock_irqrestore(&simple_ida_lock, flags);
93788eca020SRusty Russell 
93888eca020SRusty Russell 	if (unlikely(ret == -EAGAIN))
93988eca020SRusty Russell 		goto again;
94088eca020SRusty Russell 
94188eca020SRusty Russell 	return ret;
94288eca020SRusty Russell }
94388eca020SRusty Russell EXPORT_SYMBOL(ida_simple_get);
94488eca020SRusty Russell 
94588eca020SRusty Russell /**
94688eca020SRusty Russell  * ida_simple_remove - remove an allocated id.
94788eca020SRusty Russell  * @ida: the (initialized) ida.
94888eca020SRusty Russell  * @id: the id returned by ida_simple_get.
94988eca020SRusty Russell  */
95088eca020SRusty Russell void ida_simple_remove(struct ida *ida, unsigned int id)
95188eca020SRusty Russell {
95246cbc1d3STejun Heo 	unsigned long flags;
95346cbc1d3STejun Heo 
95488eca020SRusty Russell 	BUG_ON((int)id < 0);
95546cbc1d3STejun Heo 	spin_lock_irqsave(&simple_ida_lock, flags);
95688eca020SRusty Russell 	ida_remove(ida, id);
95746cbc1d3STejun Heo 	spin_unlock_irqrestore(&simple_ida_lock, flags);
95888eca020SRusty Russell }
95988eca020SRusty Russell EXPORT_SYMBOL(ida_simple_remove);
96088eca020SRusty Russell 
96188eca020SRusty Russell /**
96272dba584STejun Heo  * ida_init - initialize ida handle
96372dba584STejun Heo  * @ida:	ida handle
96472dba584STejun Heo  *
96572dba584STejun Heo  * This function is use to set up the handle (@ida) that you will pass
96672dba584STejun Heo  * to the rest of the functions.
96772dba584STejun Heo  */
96872dba584STejun Heo void ida_init(struct ida *ida)
96972dba584STejun Heo {
97072dba584STejun Heo 	memset(ida, 0, sizeof(struct ida));
97172dba584STejun Heo 	idr_init(&ida->idr);
97272dba584STejun Heo 
97372dba584STejun Heo }
97472dba584STejun Heo EXPORT_SYMBOL(ida_init);
975