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 634ae53789SNadia Derbey static struct idr_layer *get_from_free_list(struct idr *idp) 641da177e4SLinus Torvalds { 651da177e4SLinus Torvalds struct idr_layer *p; 66c259cc28SRoland Dreier unsigned long flags; 671da177e4SLinus Torvalds 68c259cc28SRoland Dreier spin_lock_irqsave(&idp->lock, flags); 691da177e4SLinus Torvalds if ((p = idp->id_free)) { 701da177e4SLinus Torvalds idp->id_free = p->ary[0]; 711da177e4SLinus Torvalds idp->id_free_cnt--; 721da177e4SLinus Torvalds p->ary[0] = NULL; 731da177e4SLinus Torvalds } 74c259cc28SRoland Dreier spin_unlock_irqrestore(&idp->lock, flags); 751da177e4SLinus Torvalds return(p); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 78d5c7409fSTejun Heo /** 79d5c7409fSTejun Heo * idr_layer_alloc - allocate a new idr_layer 80d5c7409fSTejun Heo * @gfp_mask: allocation mask 81d5c7409fSTejun Heo * @layer_idr: optional idr to allocate from 82d5c7409fSTejun Heo * 83d5c7409fSTejun Heo * If @layer_idr is %NULL, directly allocate one using @gfp_mask or fetch 84d5c7409fSTejun Heo * one from the per-cpu preload buffer. If @layer_idr is not %NULL, fetch 85d5c7409fSTejun Heo * an idr_layer from @idr->id_free. 86d5c7409fSTejun Heo * 87d5c7409fSTejun Heo * @layer_idr is to maintain backward compatibility with the old alloc 88d5c7409fSTejun Heo * interface - idr_pre_get() and idr_get_new*() - and will be removed 89d5c7409fSTejun Heo * together with per-pool preload buffer. 90d5c7409fSTejun Heo */ 91d5c7409fSTejun Heo static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr) 92d5c7409fSTejun Heo { 93d5c7409fSTejun Heo struct idr_layer *new; 94d5c7409fSTejun Heo 95d5c7409fSTejun Heo /* this is the old path, bypass to get_from_free_list() */ 96d5c7409fSTejun Heo if (layer_idr) 97d5c7409fSTejun Heo return get_from_free_list(layer_idr); 98d5c7409fSTejun Heo 99d5c7409fSTejun Heo /* try to allocate directly from kmem_cache */ 100d5c7409fSTejun Heo new = kmem_cache_zalloc(idr_layer_cache, gfp_mask); 101d5c7409fSTejun Heo if (new) 102d5c7409fSTejun Heo return new; 103d5c7409fSTejun Heo 104d5c7409fSTejun Heo /* 105d5c7409fSTejun Heo * Try to fetch one from the per-cpu preload buffer if in process 106d5c7409fSTejun Heo * context. See idr_preload() for details. 107d5c7409fSTejun Heo */ 108d5c7409fSTejun Heo if (in_interrupt()) 109d5c7409fSTejun Heo return NULL; 110d5c7409fSTejun Heo 111d5c7409fSTejun Heo preempt_disable(); 112d5c7409fSTejun Heo new = __this_cpu_read(idr_preload_head); 113d5c7409fSTejun Heo if (new) { 114d5c7409fSTejun Heo __this_cpu_write(idr_preload_head, new->ary[0]); 115d5c7409fSTejun Heo __this_cpu_dec(idr_preload_cnt); 116d5c7409fSTejun Heo new->ary[0] = NULL; 117d5c7409fSTejun Heo } 118d5c7409fSTejun Heo preempt_enable(); 119d5c7409fSTejun Heo return new; 120d5c7409fSTejun Heo } 121d5c7409fSTejun Heo 122cf481c20SNadia Derbey static void idr_layer_rcu_free(struct rcu_head *head) 123cf481c20SNadia Derbey { 124cf481c20SNadia Derbey struct idr_layer *layer; 125cf481c20SNadia Derbey 126cf481c20SNadia Derbey layer = container_of(head, struct idr_layer, rcu_head); 127cf481c20SNadia Derbey kmem_cache_free(idr_layer_cache, layer); 128cf481c20SNadia Derbey } 129cf481c20SNadia Derbey 130cf481c20SNadia Derbey static inline void free_layer(struct idr_layer *p) 131cf481c20SNadia Derbey { 132cf481c20SNadia Derbey call_rcu(&p->rcu_head, idr_layer_rcu_free); 133cf481c20SNadia Derbey } 134cf481c20SNadia Derbey 1351eec0056SSonny Rao /* only called when idp->lock is held */ 1364ae53789SNadia Derbey static void __move_to_free_list(struct idr *idp, struct idr_layer *p) 1371eec0056SSonny Rao { 1381eec0056SSonny Rao p->ary[0] = idp->id_free; 1391eec0056SSonny Rao idp->id_free = p; 1401eec0056SSonny Rao idp->id_free_cnt++; 1411eec0056SSonny Rao } 1421eec0056SSonny Rao 1434ae53789SNadia Derbey static void move_to_free_list(struct idr *idp, struct idr_layer *p) 1441da177e4SLinus Torvalds { 145c259cc28SRoland Dreier unsigned long flags; 146c259cc28SRoland Dreier 1471da177e4SLinus Torvalds /* 1481da177e4SLinus Torvalds * Depends on the return element being zeroed. 1491da177e4SLinus Torvalds */ 150c259cc28SRoland Dreier spin_lock_irqsave(&idp->lock, flags); 1514ae53789SNadia Derbey __move_to_free_list(idp, p); 152c259cc28SRoland Dreier spin_unlock_irqrestore(&idp->lock, flags); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 155e33ac8bdSTejun Heo static void idr_mark_full(struct idr_layer **pa, int id) 156e33ac8bdSTejun Heo { 157e33ac8bdSTejun Heo struct idr_layer *p = pa[0]; 158e33ac8bdSTejun Heo int l = 0; 159e33ac8bdSTejun Heo 160e33ac8bdSTejun Heo __set_bit(id & IDR_MASK, &p->bitmap); 161e33ac8bdSTejun Heo /* 162e33ac8bdSTejun Heo * If this layer is full mark the bit in the layer above to 163e33ac8bdSTejun Heo * show that this part of the radix tree is full. This may 164e33ac8bdSTejun Heo * complete the layer above and require walking up the radix 165e33ac8bdSTejun Heo * tree. 166e33ac8bdSTejun Heo */ 167e33ac8bdSTejun Heo while (p->bitmap == IDR_FULL) { 168e33ac8bdSTejun Heo if (!(p = pa[++l])) 169e33ac8bdSTejun Heo break; 170e33ac8bdSTejun Heo id = id >> IDR_BITS; 171e33ac8bdSTejun Heo __set_bit((id & IDR_MASK), &p->bitmap); 172e33ac8bdSTejun Heo } 173e33ac8bdSTejun Heo } 174e33ac8bdSTejun Heo 1751da177e4SLinus Torvalds /** 17656083ab1SRandy Dunlap * idr_pre_get - reserve resources for idr allocation 1771da177e4SLinus Torvalds * @idp: idr handle 1781da177e4SLinus Torvalds * @gfp_mask: memory allocation flags 1791da177e4SLinus Torvalds * 180066a9be6SNaohiro Aota * This function should be called prior to calling the idr_get_new* functions. 181066a9be6SNaohiro Aota * It preallocates enough memory to satisfy the worst possible allocation. The 182066a9be6SNaohiro Aota * caller should pass in GFP_KERNEL if possible. This of course requires that 183066a9be6SNaohiro Aota * no spinning locks be held. 1841da177e4SLinus Torvalds * 18556083ab1SRandy Dunlap * If the system is REALLY out of memory this function returns %0, 18656083ab1SRandy Dunlap * otherwise %1. 1871da177e4SLinus Torvalds */ 188fd4f2df2SAl Viro int idr_pre_get(struct idr *idp, gfp_t gfp_mask) 1891da177e4SLinus Torvalds { 190125c4c70SFengguang Wu while (idp->id_free_cnt < MAX_IDR_FREE) { 1911da177e4SLinus Torvalds struct idr_layer *new; 1925b019e99SAndrew Morton new = kmem_cache_zalloc(idr_layer_cache, gfp_mask); 1931da177e4SLinus Torvalds if (new == NULL) 1941da177e4SLinus Torvalds return (0); 1954ae53789SNadia Derbey move_to_free_list(idp, new); 1961da177e4SLinus Torvalds } 1971da177e4SLinus Torvalds return 1; 1981da177e4SLinus Torvalds } 1991da177e4SLinus Torvalds EXPORT_SYMBOL(idr_pre_get); 2001da177e4SLinus Torvalds 20112d1b439STejun Heo /** 20212d1b439STejun Heo * sub_alloc - try to allocate an id without growing the tree depth 20312d1b439STejun Heo * @idp: idr handle 20412d1b439STejun Heo * @starting_id: id to start search at 20512d1b439STejun Heo * @id: pointer to the allocated handle 20612d1b439STejun Heo * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer 207d5c7409fSTejun Heo * @gfp_mask: allocation mask for idr_layer_alloc() 208d5c7409fSTejun Heo * @layer_idr: optional idr passed to idr_layer_alloc() 20912d1b439STejun Heo * 21012d1b439STejun Heo * Allocate an id in range [@starting_id, INT_MAX] from @idp without 21112d1b439STejun Heo * growing its depth. Returns 21212d1b439STejun Heo * 21312d1b439STejun Heo * the allocated id >= 0 if successful, 21412d1b439STejun Heo * -EAGAIN if the tree needs to grow for allocation to succeed, 21512d1b439STejun Heo * -ENOSPC if the id space is exhausted, 21612d1b439STejun Heo * -ENOMEM if more idr_layers need to be allocated. 21712d1b439STejun Heo */ 218d5c7409fSTejun Heo static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa, 219d5c7409fSTejun Heo gfp_t gfp_mask, struct idr *layer_idr) 2201da177e4SLinus Torvalds { 2211da177e4SLinus Torvalds int n, m, sh; 2221da177e4SLinus Torvalds struct idr_layer *p, *new; 2237aae6dd8STejun Heo int l, id, oid; 2245ba25331SAl Viro unsigned long bm; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds id = *starting_id; 2277aae6dd8STejun Heo restart: 2281da177e4SLinus Torvalds p = idp->top; 2291da177e4SLinus Torvalds l = idp->layers; 2301da177e4SLinus Torvalds pa[l--] = NULL; 2311da177e4SLinus Torvalds while (1) { 2321da177e4SLinus Torvalds /* 2331da177e4SLinus Torvalds * We run around this while until we reach the leaf node... 2341da177e4SLinus Torvalds */ 2351da177e4SLinus Torvalds n = (id >> (IDR_BITS*l)) & IDR_MASK; 2361da177e4SLinus Torvalds bm = ~p->bitmap; 2371da177e4SLinus Torvalds m = find_next_bit(&bm, IDR_SIZE, n); 2381da177e4SLinus Torvalds if (m == IDR_SIZE) { 2391da177e4SLinus Torvalds /* no space available go back to previous layer. */ 2401da177e4SLinus Torvalds l++; 2417aae6dd8STejun Heo oid = id; 2421da177e4SLinus Torvalds id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1; 2437aae6dd8STejun Heo 2447aae6dd8STejun Heo /* if already at the top layer, we need to grow */ 245d2e7276bSTejun Heo if (id >= 1 << (idp->layers * IDR_BITS)) { 2461da177e4SLinus Torvalds *starting_id = id; 24712d1b439STejun Heo return -EAGAIN; 2481da177e4SLinus Torvalds } 249d2e7276bSTejun Heo p = pa[l]; 250d2e7276bSTejun Heo BUG_ON(!p); 2517aae6dd8STejun Heo 2527aae6dd8STejun Heo /* If we need to go up one layer, continue the 2537aae6dd8STejun Heo * loop; otherwise, restart from the top. 2547aae6dd8STejun Heo */ 2557aae6dd8STejun Heo sh = IDR_BITS * (l + 1); 2567aae6dd8STejun Heo if (oid >> sh == id >> sh) 2571da177e4SLinus Torvalds continue; 2587aae6dd8STejun Heo else 2597aae6dd8STejun Heo goto restart; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds if (m != n) { 2621da177e4SLinus Torvalds sh = IDR_BITS*l; 2631da177e4SLinus Torvalds id = ((id >> sh) ^ n ^ m) << sh; 2641da177e4SLinus Torvalds } 265125c4c70SFengguang Wu if ((id >= MAX_IDR_BIT) || (id < 0)) 26612d1b439STejun Heo return -ENOSPC; 2671da177e4SLinus Torvalds if (l == 0) 2681da177e4SLinus Torvalds break; 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * Create the layer below if it is missing. 2711da177e4SLinus Torvalds */ 2721da177e4SLinus Torvalds if (!p->ary[m]) { 273d5c7409fSTejun Heo new = idr_layer_alloc(gfp_mask, layer_idr); 2744ae53789SNadia Derbey if (!new) 27512d1b439STejun Heo return -ENOMEM; 2766ff2d39bSManfred Spraul new->layer = l-1; 2773219b3b7SNadia Derbey rcu_assign_pointer(p->ary[m], new); 2781da177e4SLinus Torvalds p->count++; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds pa[l--] = p; 2811da177e4SLinus Torvalds p = p->ary[m]; 2821da177e4SLinus Torvalds } 283e33ac8bdSTejun Heo 284e33ac8bdSTejun Heo pa[l] = p; 285e33ac8bdSTejun Heo return id; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 288e33ac8bdSTejun Heo static int idr_get_empty_slot(struct idr *idp, int starting_id, 289d5c7409fSTejun Heo struct idr_layer **pa, gfp_t gfp_mask, 290d5c7409fSTejun Heo struct idr *layer_idr) 2911da177e4SLinus Torvalds { 2921da177e4SLinus Torvalds struct idr_layer *p, *new; 2931da177e4SLinus Torvalds int layers, v, id; 294c259cc28SRoland Dreier unsigned long flags; 2951da177e4SLinus Torvalds 2961da177e4SLinus Torvalds id = starting_id; 2971da177e4SLinus Torvalds build_up: 2981da177e4SLinus Torvalds p = idp->top; 2991da177e4SLinus Torvalds layers = idp->layers; 3001da177e4SLinus Torvalds if (unlikely(!p)) { 301d5c7409fSTejun Heo if (!(p = idr_layer_alloc(gfp_mask, layer_idr))) 30212d1b439STejun Heo return -ENOMEM; 3036ff2d39bSManfred Spraul p->layer = 0; 3041da177e4SLinus Torvalds layers = 1; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds /* 3071da177e4SLinus Torvalds * Add a new layer to the top of the tree if the requested 3081da177e4SLinus Torvalds * id is larger than the currently allocated space. 3091da177e4SLinus Torvalds */ 310326cf0f0STejun Heo while (id > idr_max(layers)) { 3111da177e4SLinus Torvalds layers++; 312711a49a0SManfred Spraul if (!p->count) { 313711a49a0SManfred Spraul /* special case: if the tree is currently empty, 314711a49a0SManfred Spraul * then we grow the tree by moving the top node 315711a49a0SManfred Spraul * upwards. 316711a49a0SManfred Spraul */ 317711a49a0SManfred Spraul p->layer++; 3181da177e4SLinus Torvalds continue; 319711a49a0SManfred Spraul } 320d5c7409fSTejun Heo if (!(new = idr_layer_alloc(gfp_mask, layer_idr))) { 3211da177e4SLinus Torvalds /* 3221da177e4SLinus Torvalds * The allocation failed. If we built part of 3231da177e4SLinus Torvalds * the structure tear it down. 3241da177e4SLinus Torvalds */ 325c259cc28SRoland Dreier spin_lock_irqsave(&idp->lock, flags); 3261da177e4SLinus Torvalds for (new = p; p && p != idp->top; new = p) { 3271da177e4SLinus Torvalds p = p->ary[0]; 3281da177e4SLinus Torvalds new->ary[0] = NULL; 3291da177e4SLinus Torvalds new->bitmap = new->count = 0; 3304ae53789SNadia Derbey __move_to_free_list(idp, new); 3311da177e4SLinus Torvalds } 332c259cc28SRoland Dreier spin_unlock_irqrestore(&idp->lock, flags); 33312d1b439STejun Heo return -ENOMEM; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds new->ary[0] = p; 3361da177e4SLinus Torvalds new->count = 1; 3376ff2d39bSManfred Spraul new->layer = layers-1; 3381da177e4SLinus Torvalds if (p->bitmap == IDR_FULL) 3391da177e4SLinus Torvalds __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 */ 3543594eb28STejun Heo static void idr_fill_slot(void *ptr, int id, struct idr_layer **pa) 3553594eb28STejun Heo { 3563594eb28STejun Heo rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr); 357e33ac8bdSTejun Heo pa[0]->count++; 358e33ac8bdSTejun Heo idr_mark_full(pa, id); 359e33ac8bdSTejun Heo } 360e33ac8bdSTejun Heo 3611da177e4SLinus Torvalds /** 3627c657f2fSJohn McCutchan * idr_get_new_above - allocate new idr entry above or equal to a start id 3631da177e4SLinus Torvalds * @idp: idr handle 36494e2bd68SThadeu Lima de Souza Cascardo * @ptr: pointer you want associated with the id 365ea24ea85SNaohiro Aota * @starting_id: id to start search at 3661da177e4SLinus Torvalds * @id: pointer to the allocated handle 3671da177e4SLinus Torvalds * 3681da177e4SLinus Torvalds * This is the allocate id function. It should be called with any 3691da177e4SLinus Torvalds * required locks. 3701da177e4SLinus Torvalds * 371066a9be6SNaohiro Aota * If allocation from IDR's private freelist fails, idr_get_new_above() will 37256083ab1SRandy Dunlap * return %-EAGAIN. The caller should retry the idr_pre_get() call to refill 373066a9be6SNaohiro Aota * IDR's preallocation and then retry the idr_get_new_above() call. 374066a9be6SNaohiro Aota * 37556083ab1SRandy Dunlap * If the idr is full idr_get_new_above() will return %-ENOSPC. 3761da177e4SLinus Torvalds * 37756083ab1SRandy Dunlap * @id returns a value in the range @starting_id ... %0x7fffffff 3781da177e4SLinus Torvalds */ 3791da177e4SLinus Torvalds int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id) 3801da177e4SLinus Torvalds { 381326cf0f0STejun Heo struct idr_layer *pa[MAX_IDR_LEVEL + 1]; 3821da177e4SLinus Torvalds int rv; 383e15ae2ddSJesper Juhl 384d5c7409fSTejun Heo rv = idr_get_empty_slot(idp, starting_id, pa, 0, idp); 385944ca05cSNadia Derbey if (rv < 0) 38612d1b439STejun Heo return rv == -ENOMEM ? -EAGAIN : rv; 3873594eb28STejun Heo 3883594eb28STejun Heo idr_fill_slot(ptr, rv, pa); 3891da177e4SLinus Torvalds *id = rv; 3901da177e4SLinus Torvalds return 0; 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds EXPORT_SYMBOL(idr_get_new_above); 3931da177e4SLinus Torvalds 394d5c7409fSTejun Heo /** 395d5c7409fSTejun Heo * idr_preload - preload for idr_alloc() 396d5c7409fSTejun Heo * @gfp_mask: allocation mask to use for preloading 397d5c7409fSTejun Heo * 398d5c7409fSTejun Heo * Preload per-cpu layer buffer for idr_alloc(). Can only be used from 399d5c7409fSTejun Heo * process context and each idr_preload() invocation should be matched with 400d5c7409fSTejun Heo * idr_preload_end(). Note that preemption is disabled while preloaded. 401d5c7409fSTejun Heo * 402d5c7409fSTejun Heo * The first idr_alloc() in the preloaded section can be treated as if it 403d5c7409fSTejun Heo * were invoked with @gfp_mask used for preloading. This allows using more 404d5c7409fSTejun Heo * permissive allocation masks for idrs protected by spinlocks. 405d5c7409fSTejun Heo * 406d5c7409fSTejun Heo * For example, if idr_alloc() below fails, the failure can be treated as 407d5c7409fSTejun Heo * if idr_alloc() were called with GFP_KERNEL rather than GFP_NOWAIT. 408d5c7409fSTejun Heo * 409d5c7409fSTejun Heo * idr_preload(GFP_KERNEL); 410d5c7409fSTejun Heo * spin_lock(lock); 411d5c7409fSTejun Heo * 412d5c7409fSTejun Heo * id = idr_alloc(idr, ptr, start, end, GFP_NOWAIT); 413d5c7409fSTejun Heo * 414d5c7409fSTejun Heo * spin_unlock(lock); 415d5c7409fSTejun Heo * idr_preload_end(); 416d5c7409fSTejun Heo * if (id < 0) 417d5c7409fSTejun Heo * error; 418d5c7409fSTejun Heo */ 419d5c7409fSTejun Heo void idr_preload(gfp_t gfp_mask) 420d5c7409fSTejun Heo { 421d5c7409fSTejun Heo /* 422d5c7409fSTejun Heo * Consuming preload buffer from non-process context breaks preload 423d5c7409fSTejun Heo * allocation guarantee. Disallow usage from those contexts. 424d5c7409fSTejun Heo */ 425d5c7409fSTejun Heo WARN_ON_ONCE(in_interrupt()); 426d5c7409fSTejun Heo might_sleep_if(gfp_mask & __GFP_WAIT); 427d5c7409fSTejun Heo 428d5c7409fSTejun Heo preempt_disable(); 429d5c7409fSTejun Heo 430d5c7409fSTejun Heo /* 431d5c7409fSTejun Heo * idr_alloc() is likely to succeed w/o full idr_layer buffer and 432d5c7409fSTejun Heo * return value from idr_alloc() needs to be checked for failure 433d5c7409fSTejun Heo * anyway. Silently give up if allocation fails. The caller can 434d5c7409fSTejun Heo * treat failures from idr_alloc() as if idr_alloc() were called 435d5c7409fSTejun Heo * with @gfp_mask which should be enough. 436d5c7409fSTejun Heo */ 437d5c7409fSTejun Heo while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) { 438d5c7409fSTejun Heo struct idr_layer *new; 439d5c7409fSTejun Heo 440d5c7409fSTejun Heo preempt_enable(); 441d5c7409fSTejun Heo new = kmem_cache_zalloc(idr_layer_cache, gfp_mask); 442d5c7409fSTejun Heo preempt_disable(); 443d5c7409fSTejun Heo if (!new) 444d5c7409fSTejun Heo break; 445d5c7409fSTejun Heo 446d5c7409fSTejun Heo /* link the new one to per-cpu preload list */ 447d5c7409fSTejun Heo new->ary[0] = __this_cpu_read(idr_preload_head); 448d5c7409fSTejun Heo __this_cpu_write(idr_preload_head, new); 449d5c7409fSTejun Heo __this_cpu_inc(idr_preload_cnt); 450d5c7409fSTejun Heo } 451d5c7409fSTejun Heo } 452d5c7409fSTejun Heo EXPORT_SYMBOL(idr_preload); 453d5c7409fSTejun Heo 454d5c7409fSTejun Heo /** 455d5c7409fSTejun Heo * idr_alloc - allocate new idr entry 456d5c7409fSTejun Heo * @idr: the (initialized) idr 457d5c7409fSTejun Heo * @ptr: pointer to be associated with the new id 458d5c7409fSTejun Heo * @start: the minimum id (inclusive) 459d5c7409fSTejun Heo * @end: the maximum id (exclusive, <= 0 for max) 460d5c7409fSTejun Heo * @gfp_mask: memory allocation flags 461d5c7409fSTejun Heo * 462d5c7409fSTejun Heo * Allocate an id in [start, end) and associate it with @ptr. If no ID is 463d5c7409fSTejun Heo * available in the specified range, returns -ENOSPC. On memory allocation 464d5c7409fSTejun Heo * failure, returns -ENOMEM. 465d5c7409fSTejun Heo * 466d5c7409fSTejun Heo * Note that @end is treated as max when <= 0. This is to always allow 467d5c7409fSTejun Heo * using @start + N as @end as long as N is inside integer range. 468d5c7409fSTejun Heo * 469d5c7409fSTejun Heo * The user is responsible for exclusively synchronizing all operations 470d5c7409fSTejun Heo * which may modify @idr. However, read-only accesses such as idr_find() 471d5c7409fSTejun Heo * or iteration can be performed under RCU read lock provided the user 472d5c7409fSTejun Heo * destroys @ptr in RCU-safe way after removal from idr. 473d5c7409fSTejun Heo */ 474d5c7409fSTejun Heo int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask) 475d5c7409fSTejun Heo { 476d5c7409fSTejun Heo int max = end > 0 ? end - 1 : INT_MAX; /* inclusive upper limit */ 477326cf0f0STejun Heo struct idr_layer *pa[MAX_IDR_LEVEL + 1]; 478d5c7409fSTejun Heo int id; 479d5c7409fSTejun Heo 480d5c7409fSTejun Heo might_sleep_if(gfp_mask & __GFP_WAIT); 481d5c7409fSTejun Heo 482d5c7409fSTejun Heo /* sanity checks */ 483d5c7409fSTejun Heo if (WARN_ON_ONCE(start < 0)) 484d5c7409fSTejun Heo return -EINVAL; 485d5c7409fSTejun Heo if (unlikely(max < start)) 486d5c7409fSTejun Heo return -ENOSPC; 487d5c7409fSTejun Heo 488d5c7409fSTejun Heo /* allocate id */ 489d5c7409fSTejun Heo id = idr_get_empty_slot(idr, start, pa, gfp_mask, NULL); 490d5c7409fSTejun Heo if (unlikely(id < 0)) 491d5c7409fSTejun Heo return id; 492d5c7409fSTejun Heo if (unlikely(id > max)) 493d5c7409fSTejun Heo return -ENOSPC; 494d5c7409fSTejun Heo 495d5c7409fSTejun Heo idr_fill_slot(ptr, id, pa); 496d5c7409fSTejun Heo return id; 497d5c7409fSTejun Heo } 498d5c7409fSTejun Heo EXPORT_SYMBOL_GPL(idr_alloc); 499d5c7409fSTejun Heo 5001da177e4SLinus Torvalds static void idr_remove_warning(int id) 5011da177e4SLinus Torvalds { 502f098ad65SNadia Derbey printk(KERN_WARNING 503f098ad65SNadia Derbey "idr_remove called for id=%d which is not allocated.\n", id); 5041da177e4SLinus Torvalds dump_stack(); 5051da177e4SLinus Torvalds } 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds static void sub_remove(struct idr *idp, int shift, int id) 5081da177e4SLinus Torvalds { 5091da177e4SLinus Torvalds struct idr_layer *p = idp->top; 510326cf0f0STejun Heo struct idr_layer **pa[MAX_IDR_LEVEL + 1]; 5111da177e4SLinus Torvalds struct idr_layer ***paa = &pa[0]; 512cf481c20SNadia Derbey struct idr_layer *to_free; 5131da177e4SLinus Torvalds int n; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds *paa = NULL; 5161da177e4SLinus Torvalds *++paa = &idp->top; 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds while ((shift > 0) && p) { 5191da177e4SLinus Torvalds n = (id >> shift) & IDR_MASK; 5201da177e4SLinus Torvalds __clear_bit(n, &p->bitmap); 5211da177e4SLinus Torvalds *++paa = &p->ary[n]; 5221da177e4SLinus Torvalds p = p->ary[n]; 5231da177e4SLinus Torvalds shift -= IDR_BITS; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds n = id & IDR_MASK; 5261da177e4SLinus Torvalds if (likely(p != NULL && test_bit(n, &p->bitmap))){ 5271da177e4SLinus Torvalds __clear_bit(n, &p->bitmap); 528cf481c20SNadia Derbey rcu_assign_pointer(p->ary[n], NULL); 529cf481c20SNadia Derbey to_free = NULL; 5301da177e4SLinus Torvalds while(*paa && ! --((**paa)->count)){ 531cf481c20SNadia Derbey if (to_free) 532cf481c20SNadia Derbey free_layer(to_free); 533cf481c20SNadia Derbey to_free = **paa; 5341da177e4SLinus Torvalds **paa-- = NULL; 5351da177e4SLinus Torvalds } 5361da177e4SLinus Torvalds if (!*paa) 5371da177e4SLinus Torvalds idp->layers = 0; 538cf481c20SNadia Derbey if (to_free) 539cf481c20SNadia Derbey free_layer(to_free); 540e15ae2ddSJesper Juhl } else 5411da177e4SLinus Torvalds idr_remove_warning(id); 5421da177e4SLinus Torvalds } 5431da177e4SLinus Torvalds 5441da177e4SLinus Torvalds /** 54556083ab1SRandy Dunlap * idr_remove - remove the given id and free its slot 54672fd4a35SRobert P. J. Day * @idp: idr handle 54772fd4a35SRobert P. J. Day * @id: unique key 5481da177e4SLinus Torvalds */ 5491da177e4SLinus Torvalds void idr_remove(struct idr *idp, int id) 5501da177e4SLinus Torvalds { 5511da177e4SLinus Torvalds struct idr_layer *p; 552cf481c20SNadia Derbey struct idr_layer *to_free; 5531da177e4SLinus Torvalds 554e8c8d1bcSTejun Heo if (WARN_ON_ONCE(id < 0)) 555e8c8d1bcSTejun Heo return; 5561da177e4SLinus Torvalds 5571da177e4SLinus Torvalds sub_remove(idp, (idp->layers - 1) * IDR_BITS, id); 558e15ae2ddSJesper Juhl if (idp->top && idp->top->count == 1 && (idp->layers > 1) && 559cf481c20SNadia Derbey idp->top->ary[0]) { 560cf481c20SNadia Derbey /* 561cf481c20SNadia Derbey * Single child at leftmost slot: we can shrink the tree. 562cf481c20SNadia Derbey * This level is not needed anymore since when layers are 563cf481c20SNadia Derbey * inserted, they are inserted at the top of the existing 564cf481c20SNadia Derbey * tree. 565cf481c20SNadia Derbey */ 566cf481c20SNadia Derbey to_free = idp->top; 5671da177e4SLinus Torvalds p = idp->top->ary[0]; 568cf481c20SNadia Derbey rcu_assign_pointer(idp->top, p); 5691da177e4SLinus Torvalds --idp->layers; 570cf481c20SNadia Derbey to_free->bitmap = to_free->count = 0; 571cf481c20SNadia Derbey free_layer(to_free); 5721da177e4SLinus Torvalds } 573125c4c70SFengguang Wu while (idp->id_free_cnt >= MAX_IDR_FREE) { 5744ae53789SNadia Derbey p = get_from_free_list(idp); 575cf481c20SNadia Derbey /* 576cf481c20SNadia Derbey * Note: we don't call the rcu callback here, since the only 577cf481c20SNadia Derbey * layers that fall into the freelist are those that have been 578cf481c20SNadia Derbey * preallocated. 579cf481c20SNadia Derbey */ 5801da177e4SLinus Torvalds kmem_cache_free(idr_layer_cache, p); 5811da177e4SLinus Torvalds } 582af8e2a4cSNadia Derbey return; 5831da177e4SLinus Torvalds } 5841da177e4SLinus Torvalds EXPORT_SYMBOL(idr_remove); 5851da177e4SLinus Torvalds 586fe6e24ecSTejun Heo void __idr_remove_all(struct idr *idp) 58723936cc0SKristian Hoegsberg { 5886ace06dcSOleg Nesterov int n, id, max; 5892dcb22b3SImre Deak int bt_mask; 59023936cc0SKristian Hoegsberg struct idr_layer *p; 591326cf0f0STejun Heo struct idr_layer *pa[MAX_IDR_LEVEL + 1]; 59223936cc0SKristian Hoegsberg struct idr_layer **paa = &pa[0]; 59323936cc0SKristian Hoegsberg 59423936cc0SKristian Hoegsberg n = idp->layers * IDR_BITS; 59523936cc0SKristian Hoegsberg p = idp->top; 5961b23336aSPaul E. McKenney rcu_assign_pointer(idp->top, NULL); 597326cf0f0STejun Heo max = idr_max(idp->layers); 59823936cc0SKristian Hoegsberg 59923936cc0SKristian Hoegsberg id = 0; 600326cf0f0STejun Heo while (id >= 0 && id <= max) { 60123936cc0SKristian Hoegsberg while (n > IDR_BITS && p) { 60223936cc0SKristian Hoegsberg n -= IDR_BITS; 60323936cc0SKristian Hoegsberg *paa++ = p; 60423936cc0SKristian Hoegsberg p = p->ary[(id >> n) & IDR_MASK]; 60523936cc0SKristian Hoegsberg } 60623936cc0SKristian Hoegsberg 6072dcb22b3SImre Deak bt_mask = id; 60823936cc0SKristian Hoegsberg id += 1 << n; 6092dcb22b3SImre Deak /* Get the highest bit that the above add changed from 0->1. */ 6102dcb22b3SImre Deak while (n < fls(id ^ bt_mask)) { 611cf481c20SNadia Derbey if (p) 612cf481c20SNadia Derbey free_layer(p); 61323936cc0SKristian Hoegsberg n += IDR_BITS; 61423936cc0SKristian Hoegsberg p = *--paa; 61523936cc0SKristian Hoegsberg } 61623936cc0SKristian Hoegsberg } 61723936cc0SKristian Hoegsberg idp->layers = 0; 61823936cc0SKristian Hoegsberg } 619fe6e24ecSTejun Heo EXPORT_SYMBOL(__idr_remove_all); 62023936cc0SKristian Hoegsberg 62123936cc0SKristian Hoegsberg /** 6228d3b3591SAndrew Morton * idr_destroy - release all cached layers within an idr tree 623ea24ea85SNaohiro Aota * @idp: idr handle 6249bb26bc1STejun Heo * 6259bb26bc1STejun Heo * Free all id mappings and all idp_layers. After this function, @idp is 6269bb26bc1STejun Heo * completely unused and can be freed / recycled. The caller is 6279bb26bc1STejun Heo * responsible for ensuring that no one else accesses @idp during or after 6289bb26bc1STejun Heo * idr_destroy(). 6299bb26bc1STejun Heo * 6309bb26bc1STejun Heo * A typical clean-up sequence for objects stored in an idr tree will use 6319bb26bc1STejun Heo * idr_for_each() to free all objects, if necessay, then idr_destroy() to 6329bb26bc1STejun Heo * free up the id mappings and cached idr_layers. 6338d3b3591SAndrew Morton */ 6348d3b3591SAndrew Morton void idr_destroy(struct idr *idp) 6358d3b3591SAndrew Morton { 636fe6e24ecSTejun Heo __idr_remove_all(idp); 6379bb26bc1STejun Heo 6388d3b3591SAndrew Morton while (idp->id_free_cnt) { 6394ae53789SNadia Derbey struct idr_layer *p = get_from_free_list(idp); 6408d3b3591SAndrew Morton kmem_cache_free(idr_layer_cache, p); 6418d3b3591SAndrew Morton } 6428d3b3591SAndrew Morton } 6438d3b3591SAndrew Morton EXPORT_SYMBOL(idr_destroy); 6448d3b3591SAndrew Morton 6458d3b3591SAndrew Morton /** 6461da177e4SLinus Torvalds * idr_find - return pointer for given id 6471da177e4SLinus Torvalds * @idp: idr handle 6481da177e4SLinus Torvalds * @id: lookup key 6491da177e4SLinus Torvalds * 6501da177e4SLinus Torvalds * Return the pointer given the id it has been registered with. A %NULL 6511da177e4SLinus Torvalds * return indicates that @id is not valid or you passed %NULL in 6521da177e4SLinus Torvalds * idr_get_new(). 6531da177e4SLinus Torvalds * 654f9c46d6eSNadia Derbey * This function can be called under rcu_read_lock(), given that the leaf 655f9c46d6eSNadia Derbey * pointers lifetimes are correctly managed. 6561da177e4SLinus Torvalds */ 6571da177e4SLinus Torvalds void *idr_find(struct idr *idp, int id) 6581da177e4SLinus Torvalds { 6591da177e4SLinus Torvalds int n; 6601da177e4SLinus Torvalds struct idr_layer *p; 6611da177e4SLinus Torvalds 662e8c8d1bcSTejun Heo if (WARN_ON_ONCE(id < 0)) 663e8c8d1bcSTejun Heo return NULL; 664e8c8d1bcSTejun Heo 66596be753aSPaul E. McKenney p = rcu_dereference_raw(idp->top); 6666ff2d39bSManfred Spraul if (!p) 6676ff2d39bSManfred Spraul return NULL; 6686ff2d39bSManfred Spraul n = (p->layer+1) * IDR_BITS; 6691da177e4SLinus Torvalds 670326cf0f0STejun Heo if (id > idr_max(p->layer + 1)) 6711da177e4SLinus Torvalds return NULL; 6726ff2d39bSManfred Spraul BUG_ON(n == 0); 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds while (n > 0 && p) { 6751da177e4SLinus Torvalds n -= IDR_BITS; 6766ff2d39bSManfred Spraul BUG_ON(n != p->layer*IDR_BITS); 67796be753aSPaul E. McKenney p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds return((void *)p); 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds EXPORT_SYMBOL(idr_find); 6821da177e4SLinus Torvalds 6835806f07cSJeff Mahoney /** 68496d7fa42SKristian Hoegsberg * idr_for_each - iterate through all stored pointers 68596d7fa42SKristian Hoegsberg * @idp: idr handle 68696d7fa42SKristian Hoegsberg * @fn: function to be called for each pointer 68796d7fa42SKristian Hoegsberg * @data: data passed back to callback function 68896d7fa42SKristian Hoegsberg * 68996d7fa42SKristian Hoegsberg * Iterate over the pointers registered with the given idr. The 69096d7fa42SKristian Hoegsberg * callback function will be called for each pointer currently 69196d7fa42SKristian Hoegsberg * registered, passing the id, the pointer and the data pointer passed 69296d7fa42SKristian Hoegsberg * to this function. It is not safe to modify the idr tree while in 69396d7fa42SKristian Hoegsberg * the callback, so functions such as idr_get_new and idr_remove are 69496d7fa42SKristian Hoegsberg * not allowed. 69596d7fa42SKristian Hoegsberg * 69696d7fa42SKristian Hoegsberg * We check the return of @fn each time. If it returns anything other 69756083ab1SRandy Dunlap * than %0, we break out and return that value. 69896d7fa42SKristian Hoegsberg * 69996d7fa42SKristian Hoegsberg * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove(). 70096d7fa42SKristian Hoegsberg */ 70196d7fa42SKristian Hoegsberg int idr_for_each(struct idr *idp, 70296d7fa42SKristian Hoegsberg int (*fn)(int id, void *p, void *data), void *data) 70396d7fa42SKristian Hoegsberg { 70496d7fa42SKristian Hoegsberg int n, id, max, error = 0; 70596d7fa42SKristian Hoegsberg struct idr_layer *p; 706326cf0f0STejun Heo struct idr_layer *pa[MAX_IDR_LEVEL + 1]; 70796d7fa42SKristian Hoegsberg struct idr_layer **paa = &pa[0]; 70896d7fa42SKristian Hoegsberg 70996d7fa42SKristian Hoegsberg n = idp->layers * IDR_BITS; 71096be753aSPaul E. McKenney p = rcu_dereference_raw(idp->top); 711326cf0f0STejun Heo max = idr_max(idp->layers); 71296d7fa42SKristian Hoegsberg 71396d7fa42SKristian Hoegsberg id = 0; 714326cf0f0STejun Heo while (id >= 0 && id <= max) { 71596d7fa42SKristian Hoegsberg while (n > 0 && p) { 71696d7fa42SKristian Hoegsberg n -= IDR_BITS; 71796d7fa42SKristian Hoegsberg *paa++ = p; 71896be753aSPaul E. McKenney p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]); 71996d7fa42SKristian Hoegsberg } 72096d7fa42SKristian Hoegsberg 72196d7fa42SKristian Hoegsberg if (p) { 72296d7fa42SKristian Hoegsberg error = fn(id, (void *)p, data); 72396d7fa42SKristian Hoegsberg if (error) 72496d7fa42SKristian Hoegsberg break; 72596d7fa42SKristian Hoegsberg } 72696d7fa42SKristian Hoegsberg 72796d7fa42SKristian Hoegsberg id += 1 << n; 72896d7fa42SKristian Hoegsberg while (n < fls(id)) { 72996d7fa42SKristian Hoegsberg n += IDR_BITS; 73096d7fa42SKristian Hoegsberg p = *--paa; 73196d7fa42SKristian Hoegsberg } 73296d7fa42SKristian Hoegsberg } 73396d7fa42SKristian Hoegsberg 73496d7fa42SKristian Hoegsberg return error; 73596d7fa42SKristian Hoegsberg } 73696d7fa42SKristian Hoegsberg EXPORT_SYMBOL(idr_for_each); 73796d7fa42SKristian Hoegsberg 73896d7fa42SKristian Hoegsberg /** 73938460b48SKAMEZAWA Hiroyuki * idr_get_next - lookup next object of id to given id. 74038460b48SKAMEZAWA Hiroyuki * @idp: idr handle 741ea24ea85SNaohiro Aota * @nextidp: pointer to lookup key 74238460b48SKAMEZAWA Hiroyuki * 74338460b48SKAMEZAWA Hiroyuki * Returns pointer to registered object with id, which is next number to 7441458ce16SNaohiro Aota * given id. After being looked up, *@nextidp will be updated for the next 7451458ce16SNaohiro Aota * iteration. 7469f7de827SHugh Dickins * 7479f7de827SHugh Dickins * This function can be called under rcu_read_lock(), given that the leaf 7489f7de827SHugh Dickins * pointers lifetimes are correctly managed. 74938460b48SKAMEZAWA Hiroyuki */ 75038460b48SKAMEZAWA Hiroyuki void *idr_get_next(struct idr *idp, int *nextidp) 75138460b48SKAMEZAWA Hiroyuki { 752326cf0f0STejun Heo struct idr_layer *p, *pa[MAX_IDR_LEVEL + 1]; 75338460b48SKAMEZAWA Hiroyuki struct idr_layer **paa = &pa[0]; 75438460b48SKAMEZAWA Hiroyuki int id = *nextidp; 75538460b48SKAMEZAWA Hiroyuki int n, max; 75638460b48SKAMEZAWA Hiroyuki 75738460b48SKAMEZAWA Hiroyuki /* find first ent */ 75894bfa3b6SPaul E. McKenney p = rcu_dereference_raw(idp->top); 75938460b48SKAMEZAWA Hiroyuki if (!p) 76038460b48SKAMEZAWA Hiroyuki return NULL; 7619f7de827SHugh Dickins n = (p->layer + 1) * IDR_BITS; 762326cf0f0STejun Heo max = idr_max(p->layer + 1); 76338460b48SKAMEZAWA Hiroyuki 764326cf0f0STejun Heo while (id >= 0 && id <= max) { 76538460b48SKAMEZAWA Hiroyuki while (n > 0 && p) { 76638460b48SKAMEZAWA Hiroyuki n -= IDR_BITS; 76738460b48SKAMEZAWA Hiroyuki *paa++ = p; 76894bfa3b6SPaul E. McKenney p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]); 76938460b48SKAMEZAWA Hiroyuki } 77038460b48SKAMEZAWA Hiroyuki 77138460b48SKAMEZAWA Hiroyuki if (p) { 77238460b48SKAMEZAWA Hiroyuki *nextidp = id; 77338460b48SKAMEZAWA Hiroyuki return p; 77438460b48SKAMEZAWA Hiroyuki } 77538460b48SKAMEZAWA Hiroyuki 7766cdae741STejun Heo /* 7776cdae741STejun Heo * Proceed to the next layer at the current level. Unlike 7786cdae741STejun Heo * idr_for_each(), @id isn't guaranteed to be aligned to 7796cdae741STejun Heo * layer boundary at this point and adding 1 << n may 7806cdae741STejun Heo * incorrectly skip IDs. Make sure we jump to the 7816cdae741STejun Heo * beginning of the next layer using round_up(). 7826cdae741STejun Heo */ 7836cdae741STejun Heo id = round_up(id + 1, 1 << n); 78438460b48SKAMEZAWA Hiroyuki while (n < fls(id)) { 78538460b48SKAMEZAWA Hiroyuki n += IDR_BITS; 78638460b48SKAMEZAWA Hiroyuki p = *--paa; 78738460b48SKAMEZAWA Hiroyuki } 78838460b48SKAMEZAWA Hiroyuki } 78938460b48SKAMEZAWA Hiroyuki return NULL; 79038460b48SKAMEZAWA Hiroyuki } 7914d1ee80fSBen Hutchings EXPORT_SYMBOL(idr_get_next); 79238460b48SKAMEZAWA Hiroyuki 79338460b48SKAMEZAWA Hiroyuki 79438460b48SKAMEZAWA Hiroyuki /** 7955806f07cSJeff Mahoney * idr_replace - replace pointer for given id 7965806f07cSJeff Mahoney * @idp: idr handle 7975806f07cSJeff Mahoney * @ptr: pointer you want associated with the id 7985806f07cSJeff Mahoney * @id: lookup key 7995806f07cSJeff Mahoney * 8005806f07cSJeff Mahoney * Replace the pointer registered with an id and return the old value. 80156083ab1SRandy Dunlap * A %-ENOENT return indicates that @id was not found. 80256083ab1SRandy Dunlap * A %-EINVAL return indicates that @id was not within valid constraints. 8035806f07cSJeff Mahoney * 804cf481c20SNadia Derbey * The caller must serialize with writers. 8055806f07cSJeff Mahoney */ 8065806f07cSJeff Mahoney void *idr_replace(struct idr *idp, void *ptr, int id) 8075806f07cSJeff Mahoney { 8085806f07cSJeff Mahoney int n; 8095806f07cSJeff Mahoney struct idr_layer *p, *old_p; 8105806f07cSJeff Mahoney 811e8c8d1bcSTejun Heo if (WARN_ON_ONCE(id < 0)) 812e8c8d1bcSTejun Heo return ERR_PTR(-EINVAL); 813e8c8d1bcSTejun Heo 8145806f07cSJeff Mahoney p = idp->top; 8156ff2d39bSManfred Spraul if (!p) 8166ff2d39bSManfred Spraul return ERR_PTR(-EINVAL); 8176ff2d39bSManfred Spraul 8186ff2d39bSManfred Spraul n = (p->layer+1) * IDR_BITS; 8195806f07cSJeff Mahoney 8205806f07cSJeff Mahoney if (id >= (1 << n)) 8215806f07cSJeff Mahoney return ERR_PTR(-EINVAL); 8225806f07cSJeff Mahoney 8235806f07cSJeff Mahoney n -= IDR_BITS; 8245806f07cSJeff Mahoney while ((n > 0) && p) { 8255806f07cSJeff Mahoney p = p->ary[(id >> n) & IDR_MASK]; 8265806f07cSJeff Mahoney n -= IDR_BITS; 8275806f07cSJeff Mahoney } 8285806f07cSJeff Mahoney 8295806f07cSJeff Mahoney n = id & IDR_MASK; 8305806f07cSJeff Mahoney if (unlikely(p == NULL || !test_bit(n, &p->bitmap))) 8315806f07cSJeff Mahoney return ERR_PTR(-ENOENT); 8325806f07cSJeff Mahoney 8335806f07cSJeff Mahoney old_p = p->ary[n]; 834cf481c20SNadia Derbey rcu_assign_pointer(p->ary[n], ptr); 8355806f07cSJeff Mahoney 8365806f07cSJeff Mahoney return old_p; 8375806f07cSJeff Mahoney } 8385806f07cSJeff Mahoney EXPORT_SYMBOL(idr_replace); 8395806f07cSJeff Mahoney 840199f0ca5SAkinobu Mita void __init idr_init_cache(void) 8411da177e4SLinus Torvalds { 8421da177e4SLinus Torvalds idr_layer_cache = kmem_cache_create("idr_layer_cache", 8435b019e99SAndrew Morton sizeof(struct idr_layer), 0, SLAB_PANIC, NULL); 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 8461da177e4SLinus Torvalds /** 8471da177e4SLinus Torvalds * idr_init - initialize idr handle 8481da177e4SLinus Torvalds * @idp: idr handle 8491da177e4SLinus Torvalds * 8501da177e4SLinus Torvalds * This function is use to set up the handle (@idp) that you will pass 8511da177e4SLinus Torvalds * to the rest of the functions. 8521da177e4SLinus Torvalds */ 8531da177e4SLinus Torvalds void idr_init(struct idr *idp) 8541da177e4SLinus Torvalds { 8551da177e4SLinus Torvalds memset(idp, 0, sizeof(struct idr)); 8561da177e4SLinus Torvalds spin_lock_init(&idp->lock); 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds EXPORT_SYMBOL(idr_init); 85972dba584STejun Heo 86072dba584STejun Heo 86156083ab1SRandy Dunlap /** 86256083ab1SRandy Dunlap * DOC: IDA description 86372dba584STejun Heo * IDA - IDR based ID allocator 86472dba584STejun Heo * 86556083ab1SRandy Dunlap * This is id allocator without id -> pointer translation. Memory 86672dba584STejun Heo * usage is much lower than full blown idr because each id only 86772dba584STejun Heo * occupies a bit. ida uses a custom leaf node which contains 86872dba584STejun Heo * IDA_BITMAP_BITS slots. 86972dba584STejun Heo * 87072dba584STejun Heo * 2007-04-25 written by Tejun Heo <htejun@gmail.com> 87172dba584STejun Heo */ 87272dba584STejun Heo 87372dba584STejun Heo static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap) 87472dba584STejun Heo { 87572dba584STejun Heo unsigned long flags; 87672dba584STejun Heo 87772dba584STejun Heo if (!ida->free_bitmap) { 87872dba584STejun Heo spin_lock_irqsave(&ida->idr.lock, flags); 87972dba584STejun Heo if (!ida->free_bitmap) { 88072dba584STejun Heo ida->free_bitmap = bitmap; 88172dba584STejun Heo bitmap = NULL; 88272dba584STejun Heo } 88372dba584STejun Heo spin_unlock_irqrestore(&ida->idr.lock, flags); 88472dba584STejun Heo } 88572dba584STejun Heo 88672dba584STejun Heo kfree(bitmap); 88772dba584STejun Heo } 88872dba584STejun Heo 88972dba584STejun Heo /** 89072dba584STejun Heo * ida_pre_get - reserve resources for ida allocation 89172dba584STejun Heo * @ida: ida handle 89272dba584STejun Heo * @gfp_mask: memory allocation flag 89372dba584STejun Heo * 89472dba584STejun Heo * This function should be called prior to locking and calling the 89572dba584STejun Heo * following function. It preallocates enough memory to satisfy the 89672dba584STejun Heo * worst possible allocation. 89772dba584STejun Heo * 89856083ab1SRandy Dunlap * If the system is REALLY out of memory this function returns %0, 89956083ab1SRandy Dunlap * otherwise %1. 90072dba584STejun Heo */ 90172dba584STejun Heo int ida_pre_get(struct ida *ida, gfp_t gfp_mask) 90272dba584STejun Heo { 90372dba584STejun Heo /* allocate idr_layers */ 90472dba584STejun Heo if (!idr_pre_get(&ida->idr, gfp_mask)) 90572dba584STejun Heo return 0; 90672dba584STejun Heo 90772dba584STejun Heo /* allocate free_bitmap */ 90872dba584STejun Heo if (!ida->free_bitmap) { 90972dba584STejun Heo struct ida_bitmap *bitmap; 91072dba584STejun Heo 91172dba584STejun Heo bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask); 91272dba584STejun Heo if (!bitmap) 91372dba584STejun Heo return 0; 91472dba584STejun Heo 91572dba584STejun Heo free_bitmap(ida, bitmap); 91672dba584STejun Heo } 91772dba584STejun Heo 91872dba584STejun Heo return 1; 91972dba584STejun Heo } 92072dba584STejun Heo EXPORT_SYMBOL(ida_pre_get); 92172dba584STejun Heo 92272dba584STejun Heo /** 92372dba584STejun Heo * ida_get_new_above - allocate new ID above or equal to a start id 92472dba584STejun Heo * @ida: ida handle 925ea24ea85SNaohiro Aota * @starting_id: id to start search at 92672dba584STejun Heo * @p_id: pointer to the allocated handle 92772dba584STejun Heo * 928e3816c54SWang Sheng-Hui * Allocate new ID above or equal to @starting_id. It should be called 929e3816c54SWang Sheng-Hui * with any required locks. 93072dba584STejun Heo * 93156083ab1SRandy Dunlap * If memory is required, it will return %-EAGAIN, you should unlock 93272dba584STejun Heo * and go back to the ida_pre_get() call. If the ida is full, it will 93356083ab1SRandy Dunlap * return %-ENOSPC. 93472dba584STejun Heo * 93556083ab1SRandy Dunlap * @p_id returns a value in the range @starting_id ... %0x7fffffff. 93672dba584STejun Heo */ 93772dba584STejun Heo int ida_get_new_above(struct ida *ida, int starting_id, int *p_id) 93872dba584STejun Heo { 939326cf0f0STejun Heo struct idr_layer *pa[MAX_IDR_LEVEL + 1]; 94072dba584STejun Heo struct ida_bitmap *bitmap; 94172dba584STejun Heo unsigned long flags; 94272dba584STejun Heo int idr_id = starting_id / IDA_BITMAP_BITS; 94372dba584STejun Heo int offset = starting_id % IDA_BITMAP_BITS; 94472dba584STejun Heo int t, id; 94572dba584STejun Heo 94672dba584STejun Heo restart: 94772dba584STejun Heo /* get vacant slot */ 948d5c7409fSTejun Heo t = idr_get_empty_slot(&ida->idr, idr_id, pa, 0, &ida->idr); 949944ca05cSNadia Derbey if (t < 0) 95012d1b439STejun Heo return t == -ENOMEM ? -EAGAIN : t; 95172dba584STejun Heo 952125c4c70SFengguang Wu if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT) 95372dba584STejun Heo return -ENOSPC; 95472dba584STejun Heo 95572dba584STejun Heo if (t != idr_id) 95672dba584STejun Heo offset = 0; 95772dba584STejun Heo idr_id = t; 95872dba584STejun Heo 95972dba584STejun Heo /* if bitmap isn't there, create a new one */ 96072dba584STejun Heo bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK]; 96172dba584STejun Heo if (!bitmap) { 96272dba584STejun Heo spin_lock_irqsave(&ida->idr.lock, flags); 96372dba584STejun Heo bitmap = ida->free_bitmap; 96472dba584STejun Heo ida->free_bitmap = NULL; 96572dba584STejun Heo spin_unlock_irqrestore(&ida->idr.lock, flags); 96672dba584STejun Heo 96772dba584STejun Heo if (!bitmap) 96872dba584STejun Heo return -EAGAIN; 96972dba584STejun Heo 97072dba584STejun Heo memset(bitmap, 0, sizeof(struct ida_bitmap)); 9713219b3b7SNadia Derbey rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK], 9723219b3b7SNadia Derbey (void *)bitmap); 97372dba584STejun Heo pa[0]->count++; 97472dba584STejun Heo } 97572dba584STejun Heo 97672dba584STejun Heo /* lookup for empty slot */ 97772dba584STejun Heo t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset); 97872dba584STejun Heo if (t == IDA_BITMAP_BITS) { 97972dba584STejun Heo /* no empty slot after offset, continue to the next chunk */ 98072dba584STejun Heo idr_id++; 98172dba584STejun Heo offset = 0; 98272dba584STejun Heo goto restart; 98372dba584STejun Heo } 98472dba584STejun Heo 98572dba584STejun Heo id = idr_id * IDA_BITMAP_BITS + t; 986125c4c70SFengguang Wu if (id >= MAX_IDR_BIT) 98772dba584STejun Heo return -ENOSPC; 98872dba584STejun Heo 98972dba584STejun Heo __set_bit(t, bitmap->bitmap); 99072dba584STejun Heo if (++bitmap->nr_busy == IDA_BITMAP_BITS) 99172dba584STejun Heo idr_mark_full(pa, idr_id); 99272dba584STejun Heo 99372dba584STejun Heo *p_id = id; 99472dba584STejun Heo 99572dba584STejun Heo /* Each leaf node can handle nearly a thousand slots and the 99672dba584STejun Heo * whole idea of ida is to have small memory foot print. 99772dba584STejun Heo * Throw away extra resources one by one after each successful 99872dba584STejun Heo * allocation. 99972dba584STejun Heo */ 100072dba584STejun Heo if (ida->idr.id_free_cnt || ida->free_bitmap) { 10014ae53789SNadia Derbey struct idr_layer *p = get_from_free_list(&ida->idr); 100272dba584STejun Heo if (p) 100372dba584STejun Heo kmem_cache_free(idr_layer_cache, p); 100472dba584STejun Heo } 100572dba584STejun Heo 100672dba584STejun Heo return 0; 100772dba584STejun Heo } 100872dba584STejun Heo EXPORT_SYMBOL(ida_get_new_above); 100972dba584STejun Heo 101072dba584STejun Heo /** 101172dba584STejun Heo * ida_remove - remove the given ID 101272dba584STejun Heo * @ida: ida handle 101372dba584STejun Heo * @id: ID to free 101472dba584STejun Heo */ 101572dba584STejun Heo void ida_remove(struct ida *ida, int id) 101672dba584STejun Heo { 101772dba584STejun Heo struct idr_layer *p = ida->idr.top; 101872dba584STejun Heo int shift = (ida->idr.layers - 1) * IDR_BITS; 101972dba584STejun Heo int idr_id = id / IDA_BITMAP_BITS; 102072dba584STejun Heo int offset = id % IDA_BITMAP_BITS; 102172dba584STejun Heo int n; 102272dba584STejun Heo struct ida_bitmap *bitmap; 102372dba584STejun Heo 102472dba584STejun Heo /* clear full bits while looking up the leaf idr_layer */ 102572dba584STejun Heo while ((shift > 0) && p) { 102672dba584STejun Heo n = (idr_id >> shift) & IDR_MASK; 102772dba584STejun Heo __clear_bit(n, &p->bitmap); 102872dba584STejun Heo p = p->ary[n]; 102972dba584STejun Heo shift -= IDR_BITS; 103072dba584STejun Heo } 103172dba584STejun Heo 103272dba584STejun Heo if (p == NULL) 103372dba584STejun Heo goto err; 103472dba584STejun Heo 103572dba584STejun Heo n = idr_id & IDR_MASK; 103672dba584STejun Heo __clear_bit(n, &p->bitmap); 103772dba584STejun Heo 103872dba584STejun Heo bitmap = (void *)p->ary[n]; 103972dba584STejun Heo if (!test_bit(offset, bitmap->bitmap)) 104072dba584STejun Heo goto err; 104172dba584STejun Heo 104272dba584STejun Heo /* update bitmap and remove it if empty */ 104372dba584STejun Heo __clear_bit(offset, bitmap->bitmap); 104472dba584STejun Heo if (--bitmap->nr_busy == 0) { 104572dba584STejun Heo __set_bit(n, &p->bitmap); /* to please idr_remove() */ 104672dba584STejun Heo idr_remove(&ida->idr, idr_id); 104772dba584STejun Heo free_bitmap(ida, bitmap); 104872dba584STejun Heo } 104972dba584STejun Heo 105072dba584STejun Heo return; 105172dba584STejun Heo 105272dba584STejun Heo err: 105372dba584STejun Heo printk(KERN_WARNING 105472dba584STejun Heo "ida_remove called for id=%d which is not allocated.\n", id); 105572dba584STejun Heo } 105672dba584STejun Heo EXPORT_SYMBOL(ida_remove); 105772dba584STejun Heo 105872dba584STejun Heo /** 105972dba584STejun Heo * ida_destroy - release all cached layers within an ida tree 1060ea24ea85SNaohiro Aota * @ida: ida handle 106172dba584STejun Heo */ 106272dba584STejun Heo void ida_destroy(struct ida *ida) 106372dba584STejun Heo { 106472dba584STejun Heo idr_destroy(&ida->idr); 106572dba584STejun Heo kfree(ida->free_bitmap); 106672dba584STejun Heo } 106772dba584STejun Heo EXPORT_SYMBOL(ida_destroy); 106872dba584STejun Heo 106972dba584STejun Heo /** 107088eca020SRusty Russell * ida_simple_get - get a new id. 107188eca020SRusty Russell * @ida: the (initialized) ida. 107288eca020SRusty Russell * @start: the minimum id (inclusive, < 0x8000000) 107388eca020SRusty Russell * @end: the maximum id (exclusive, < 0x8000000 or 0) 107488eca020SRusty Russell * @gfp_mask: memory allocation flags 107588eca020SRusty Russell * 107688eca020SRusty Russell * Allocates an id in the range start <= id < end, or returns -ENOSPC. 107788eca020SRusty Russell * On memory allocation failure, returns -ENOMEM. 107888eca020SRusty Russell * 107988eca020SRusty Russell * Use ida_simple_remove() to get rid of an id. 108088eca020SRusty Russell */ 108188eca020SRusty Russell int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end, 108288eca020SRusty Russell gfp_t gfp_mask) 108388eca020SRusty Russell { 108488eca020SRusty Russell int ret, id; 108588eca020SRusty Russell unsigned int max; 108646cbc1d3STejun Heo unsigned long flags; 108788eca020SRusty Russell 108888eca020SRusty Russell BUG_ON((int)start < 0); 108988eca020SRusty Russell BUG_ON((int)end < 0); 109088eca020SRusty Russell 109188eca020SRusty Russell if (end == 0) 109288eca020SRusty Russell max = 0x80000000; 109388eca020SRusty Russell else { 109488eca020SRusty Russell BUG_ON(end < start); 109588eca020SRusty Russell max = end - 1; 109688eca020SRusty Russell } 109788eca020SRusty Russell 109888eca020SRusty Russell again: 109988eca020SRusty Russell if (!ida_pre_get(ida, gfp_mask)) 110088eca020SRusty Russell return -ENOMEM; 110188eca020SRusty Russell 110246cbc1d3STejun Heo spin_lock_irqsave(&simple_ida_lock, flags); 110388eca020SRusty Russell ret = ida_get_new_above(ida, start, &id); 110488eca020SRusty Russell if (!ret) { 110588eca020SRusty Russell if (id > max) { 110688eca020SRusty Russell ida_remove(ida, id); 110788eca020SRusty Russell ret = -ENOSPC; 110888eca020SRusty Russell } else { 110988eca020SRusty Russell ret = id; 111088eca020SRusty Russell } 111188eca020SRusty Russell } 111246cbc1d3STejun Heo spin_unlock_irqrestore(&simple_ida_lock, flags); 111388eca020SRusty Russell 111488eca020SRusty Russell if (unlikely(ret == -EAGAIN)) 111588eca020SRusty Russell goto again; 111688eca020SRusty Russell 111788eca020SRusty Russell return ret; 111888eca020SRusty Russell } 111988eca020SRusty Russell EXPORT_SYMBOL(ida_simple_get); 112088eca020SRusty Russell 112188eca020SRusty Russell /** 112288eca020SRusty Russell * ida_simple_remove - remove an allocated id. 112388eca020SRusty Russell * @ida: the (initialized) ida. 112488eca020SRusty Russell * @id: the id returned by ida_simple_get. 112588eca020SRusty Russell */ 112688eca020SRusty Russell void ida_simple_remove(struct ida *ida, unsigned int id) 112788eca020SRusty Russell { 112846cbc1d3STejun Heo unsigned long flags; 112946cbc1d3STejun Heo 113088eca020SRusty Russell BUG_ON((int)id < 0); 113146cbc1d3STejun Heo spin_lock_irqsave(&simple_ida_lock, flags); 113288eca020SRusty Russell ida_remove(ida, id); 113346cbc1d3STejun Heo spin_unlock_irqrestore(&simple_ida_lock, flags); 113488eca020SRusty Russell } 113588eca020SRusty Russell EXPORT_SYMBOL(ida_simple_remove); 113688eca020SRusty Russell 113788eca020SRusty Russell /** 113872dba584STejun Heo * ida_init - initialize ida handle 113972dba584STejun Heo * @ida: ida handle 114072dba584STejun Heo * 114172dba584STejun Heo * This function is use to set up the handle (@ida) that you will pass 114272dba584STejun Heo * to the rest of the functions. 114372dba584STejun Heo */ 114472dba584STejun Heo void ida_init(struct ida *ida) 114572dba584STejun Heo { 114672dba584STejun Heo memset(ida, 0, sizeof(struct ida)); 114772dba584STejun Heo idr_init(&ida->idr); 114872dba584STejun Heo 114972dba584STejun Heo } 115072dba584STejun Heo EXPORT_SYMBOL(ida_init); 1151