X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fidr.c;h=16d2143fea4847e5c367e56d87db654599109713;hb=44f96dd971be982b16be51945bf21e9e1ea4b467;hp=cd4d1389fde0725fa0254134a97abc726e3584fb;hpb=e686509282835a634ee3285c39a1b5b04ee2a88f;p=linux-2.6.git diff --git a/lib/idr.c b/lib/idr.c index cd4d1389f..16d2143fe 100644 --- a/lib/idr.c +++ b/lib/idr.c @@ -1,148 +1,93 @@ /* - * linux/kernel/id.c - * * 2002-10-18 written by Jim Houston jim.houston@ccur.com * Copyright (C) 2002 by Concurrent Computer Corporation * Distributed under the GNU GPL license version 2. * - * Small id to pointer translation service. - * - * It uses a radix tree like structure as a sparse array indexed - * by the id to obtain the pointer. The bitmap makes allocating - * a new id quick. - * Modified by George Anzinger to reuse immediately and to use * find bit instructions. Also removed _irq on spinlocks. - - * So here is what this bit of code does: - + * + * Small id to pointer translation service. + * + * It uses a radix tree like structure as a sparse array indexed + * by the id to obtain the pointer. The bitmap makes allocating + * a new id quick. + * * You call it to allocate an id (an int) an associate with that id a * pointer or what ever, we treat it as a (void *). You can pass this * id to a user for him to pass back at a later time. You then pass * that id to this code and it returns your pointer. - * You can release ids at any time. When all ids are released, most of + * You can release ids at any time. When all ids are released, most of * the memory is returned (we keep IDR_FREE_MAX) in a local pool so we - * don't need to go to the memory "store" during an id allocate, just + * don't need to go to the memory "store" during an id allocate, just * so you don't need to be too concerned about locking and conflicts * with the slab allocator. - - * What you need to do is, since we don't keep the counter as part of - * id / ptr pair, to keep a copy of it in the pointed to structure - * (or else where) so that when you ask for a ptr you can varify that - * the returned ptr is correct by comparing the id it contains with the one - * you asked for. In other words, we only did half the reuse protection. - * Since the code depends on your code doing this check, we ignore high - * order bits in the id, not just the count, but bits that would, if used, - * index outside of the allocated ids. In other words, if the largest id - * currently allocated is 32 a look up will only look at the low 5 bits of - * the id. Since you will want to keep this id in the structure anyway - * (if for no other reason than to be able to eliminate the id when the - * structure is found in some other way) this seems reasonable. If you - * really think otherwise, the code to check these bits here, it is just - * disabled with a #if 0. - - - * So here are the complete details: - - * include - - * void idr_init(struct idr *idp) - - * This function is use to set up the handle (idp) that you will pass - * to the rest of the functions. The structure is defined in the - * header. - - * int idr_pre_get(struct idr *idp, unsigned gfp_mask) - - * This function should be called prior to locking and calling the - * following function. It pre allocates enough memory to satisfy the - * worst possible allocation. Unless gfp_mask is GFP_ATOMIC, it can - * sleep, so must not be called with any spinlocks held. If the system is - * REALLY out of memory this function returns 0, other wise 1. - - * int idr_get_new(struct idr *idp, void *ptr, int *id); - - * This is the allocate id function. It should be called with any - * required locks. In fact, in the SMP case, you MUST lock prior to - * calling this function to avoid possible out of memory problems. - * If memory is required, it will return -EAGAIN, you should unlock - * and go back to the idr_pre_get() call. If the idr is full, it - * will return a -ENOSPC. ptr is the pointer you want associated - * with the id. The value is returned in the "id" field. idr_get_new() - * returns a value in the range 0 ... 0x7fffffff - - * int idr_get_new_above(struct idr *idp, void *ptr, int start_id, int *id); - - * Like idr_get_new(), but the returned id is guaranteed to be at or - * above start_id. - - * void *idr_find(struct idr *idp, int id); - - * returns the "ptr", given the id. A NULL return indicates that the - * id is not valid (or you passed NULL in the idr_get_new(), shame on - * you). This function must be called with a spinlock that prevents - * calling either idr_get_new() or idr_remove() or idr_find() while it - * is working. - - * void idr_remove(struct idr *idp, int id); - - * removes the given id, freeing that slot and any memory that may - * now be unused. See idr_find() for locking restrictions. - - * int idr_full(struct idr *idp); - - * Returns true if the idr is full and false if not. - */ - - #ifndef TEST // to test in user space... #include #include #include #endif +#include #include #include - static kmem_cache_t *idr_layer_cache; - - static struct idr_layer *alloc_layer(struct idr *idp) { struct idr_layer *p; + unsigned long flags; - spin_lock(&idp->lock); - if (!(p = idp->id_free)) - return NULL; - idp->id_free = p->ary[0]; - idp->id_free_cnt--; - p->ary[0] = 0; - spin_unlock(&idp->lock); + spin_lock_irqsave(&idp->lock, flags); + if ((p = idp->id_free)) { + idp->id_free = p->ary[0]; + idp->id_free_cnt--; + p->ary[0] = NULL; + } + spin_unlock_irqrestore(&idp->lock, flags); return(p); } +/* only called when idp->lock is held */ +static void __free_layer(struct idr *idp, struct idr_layer *p) +{ + p->ary[0] = idp->id_free; + idp->id_free = p; + idp->id_free_cnt++; +} + static void free_layer(struct idr *idp, struct idr_layer *p) { + unsigned long flags; + /* * Depends on the return element being zeroed. */ - spin_lock(&idp->lock); - p->ary[0] = idp->id_free; - idp->id_free = p; - idp->id_free_cnt++; - spin_unlock(&idp->lock); + spin_lock_irqsave(&idp->lock, flags); + __free_layer(idp, p); + spin_unlock_irqrestore(&idp->lock, flags); } -int idr_pre_get(struct idr *idp, unsigned gfp_mask) +/** + * idr_pre_get - reserver resources for idr allocation + * @idp: idr handle + * @gfp_mask: memory allocation flags + * + * This function should be called prior to locking and calling the + * following function. It preallocates enough memory to satisfy + * the worst possible allocation. + * + * If the system is REALLY out of memory this function returns 0, + * otherwise 1. + */ +int idr_pre_get(struct idr *idp, gfp_t gfp_mask) { while (idp->id_free_cnt < IDR_FREE_MAX) { struct idr_layer *new; new = kmem_cache_alloc(idr_layer_cache, gfp_mask); - if(new == NULL) + if (new == NULL) return (0); free_layer(idp, new); } @@ -172,7 +117,7 @@ static int sub_alloc(struct idr *idp, void *ptr, int *starting_id) if (m == IDR_SIZE) { /* no space available go back to previous layer. */ l++; - id = (id | ((1 << (IDR_BITS*l))-1)) + 1; + id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1; if (!(p = pa[l])) { *starting_id = id; return -2; @@ -226,7 +171,8 @@ static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id) { struct idr_layer *p, *new; int layers, v, id; - + unsigned long flags; + id = starting_id; build_up: p = idp->top; @@ -240,7 +186,7 @@ build_up: * Add a new layer to the top of the tree if the requested * id is larger than the currently allocated space. */ - while ((layers < MAX_LEVEL) && (id >= (1 << (layers*IDR_BITS)))) { + while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) { layers++; if (!p->count) continue; @@ -249,12 +195,14 @@ build_up: * The allocation failed. If we built part of * the structure tear it down. */ + spin_lock_irqsave(&idp->lock, flags); for (new = p; p && p != idp->top; new = p) { p = p->ary[0]; - new->ary[0] = 0; + new->ary[0] = NULL; new->bitmap = new->count = 0; - free_layer(idp, new); + __free_layer(idp, new); } + spin_unlock_irqrestore(&idp->lock, flags); return -1; } new->ary[0] = p; @@ -271,9 +219,26 @@ build_up: return(v); } +/** + * idr_get_new_above - allocate new idr entry above or equal to a start id + * @idp: idr handle + * @ptr: pointer you want associated with the ide + * @start_id: id to start search at + * @id: pointer to the allocated handle + * + * This is the allocate id function. It should be called with any + * required locks. + * + * If memory is required, it will return -EAGAIN, you should unlock + * and go back to the idr_pre_get() call. If the idr is full, it will + * return -ENOSPC. + * + * @id returns a value in the range 0 ... 0x7fffffff + */ int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id) { int rv; + rv = idr_get_new_above_int(idp, ptr, starting_id); /* * This is a cheap hack until the IDR code can be fixed to @@ -290,9 +255,25 @@ int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id) } EXPORT_SYMBOL(idr_get_new_above); +/** + * idr_get_new - allocate new idr entry + * @idp: idr handle + * @ptr: pointer you want associated with the ide + * @id: pointer to the allocated handle + * + * This is the allocate id function. It should be called with any + * required locks. + * + * If memory is required, it will return -EAGAIN, you should unlock + * and go back to the idr_pre_get() call. If the idr is full, it will + * return -ENOSPC. + * + * @id returns a value in the range 0 ... 0x7fffffff + */ int idr_get_new(struct idr *idp, void *ptr, int *id) { int rv; + rv = idr_get_new_above_int(idp, ptr, 0); /* * This is a cheap hack until the IDR code can be fixed to @@ -309,35 +290,48 @@ int idr_get_new(struct idr *idp, void *ptr, int *id) } EXPORT_SYMBOL(idr_get_new); +static void idr_remove_warning(int id) +{ + printk("idr_remove called for id=%d which is not allocated.\n", id); + dump_stack(); +} + static void sub_remove(struct idr *idp, int shift, int id) { struct idr_layer *p = idp->top; struct idr_layer **pa[MAX_LEVEL]; struct idr_layer ***paa = &pa[0]; + int n; *paa = NULL; *++paa = &idp->top; while ((shift > 0) && p) { - int n = (id >> shift) & IDR_MASK; + n = (id >> shift) & IDR_MASK; __clear_bit(n, &p->bitmap); *++paa = &p->ary[n]; p = p->ary[n]; shift -= IDR_BITS; } - if (likely(p != NULL)){ - int n = id & IDR_MASK; + n = id & IDR_MASK; + if (likely(p != NULL && test_bit(n, &p->bitmap))){ __clear_bit(n, &p->bitmap); p->ary[n] = NULL; while(*paa && ! --((**paa)->count)){ free_layer(idp, **paa); **paa-- = NULL; } - if ( ! *paa ) + if (!*paa) idp->layers = 0; - } + } else + idr_remove_warning(id); } +/** + * idr_remove - remove the given id and free it's slot + * idp: idr handle + * id: uniqueue key + */ void idr_remove(struct idr *idp, int id) { struct idr_layer *p; @@ -346,9 +340,8 @@ void idr_remove(struct idr *idp, int id) id &= MAX_ID_MASK; sub_remove(idp, (idp->layers - 1) * IDR_BITS, id); - if ( idp->top && idp->top->count == 1 && - (idp->layers > 1) && - idp->top->ary[0]){ // We can drop a layer + if (idp->top && idp->top->count == 1 && (idp->layers > 1) && + idp->top->ary[0]) { // We can drop a layer p = idp->top->ary[0]; idp->top->bitmap = idp->top->count = 0; @@ -357,7 +350,6 @@ void idr_remove(struct idr *idp, int id) --idp->layers; } while (idp->id_free_cnt >= IDR_FREE_MAX) { - p = alloc_layer(idp); kmem_cache_free(idr_layer_cache, p); return; @@ -365,6 +357,30 @@ void idr_remove(struct idr *idp, int id) } EXPORT_SYMBOL(idr_remove); +/** + * idr_destroy - release all cached layers within an idr tree + * idp: idr handle + */ +void idr_destroy(struct idr *idp) +{ + while (idp->id_free_cnt) { + struct idr_layer *p = alloc_layer(idp); + kmem_cache_free(idr_layer_cache, p); + } +} +EXPORT_SYMBOL(idr_destroy); + +/** + * idr_find - return pointer for given id + * @idp: idr handle + * @id: lookup key + * + * Return the pointer given the id it has been registered with. A %NULL + * return indicates that @id is not valid or you passed %NULL in + * idr_get_new(). + * + * The caller must serialize idr_find() vs idr_get_new() and idr_remove(). + */ void *idr_find(struct idr *idp, int id) { int n; @@ -372,17 +388,13 @@ void *idr_find(struct idr *idp, int id) n = idp->layers * IDR_BITS; p = idp->top; -#if 0 - /* - * This tests to see if bits outside the current tree are - * present. If so, tain't one of ours! - */ - if ( unlikely( (id & ~(~0 << MAX_ID_SHIFT)) >> (n + IDR_BITS))) - return NULL; -#endif + /* Mask off upper bits we don't use for the search. */ id &= MAX_ID_MASK; + if (id >= (1 << n)) + return NULL; + while (n > 0 && p) { n -= IDR_BITS; p = p->ary[(id >> n) & IDR_MASK]; @@ -391,8 +403,50 @@ void *idr_find(struct idr *idp, int id) } EXPORT_SYMBOL(idr_find); -static void idr_cache_ctor(void * idr_layer, - kmem_cache_t *idr_layer_cache, unsigned long flags) +/** + * idr_replace - replace pointer for given id + * @idp: idr handle + * @ptr: pointer you want associated with the id + * @id: lookup key + * + * Replace the pointer registered with an id and return the old value. + * A -ENOENT return indicates that @id was not found. + * A -EINVAL return indicates that @id was not within valid constraints. + * + * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove(). + */ +void *idr_replace(struct idr *idp, void *ptr, int id) +{ + int n; + struct idr_layer *p, *old_p; + + n = idp->layers * IDR_BITS; + p = idp->top; + + id &= MAX_ID_MASK; + + if (id >= (1 << n)) + return ERR_PTR(-EINVAL); + + n -= IDR_BITS; + while ((n > 0) && p) { + p = p->ary[(id >> n) & IDR_MASK]; + n -= IDR_BITS; + } + + n = id & IDR_MASK; + if (unlikely(p == NULL || !test_bit(n, &p->bitmap))) + return ERR_PTR(-ENOENT); + + old_p = p->ary[n]; + p->ary[n] = ptr; + + return old_p; +} +EXPORT_SYMBOL(idr_replace); + +static void idr_cache_ctor(void * idr_layer, kmem_cache_t *idr_layer_cache, + unsigned long flags) { memset(idr_layer, 0, sizeof(struct idr_layer)); } @@ -400,11 +454,18 @@ static void idr_cache_ctor(void * idr_layer, static int init_id_cache(void) { if (!idr_layer_cache) - idr_layer_cache = kmem_cache_create("idr_layer_cache", - sizeof(struct idr_layer), 0, 0, idr_cache_ctor, 0); + idr_layer_cache = kmem_cache_create("idr_layer_cache", + sizeof(struct idr_layer), 0, 0, idr_cache_ctor, NULL); return 0; } +/** + * idr_init - initialize idr handle + * @idp: idr handle + * + * This function is use to set up the handle (@idp) that you will pass + * to the rest of the functions. + */ void idr_init(struct idr *idp) { init_id_cache(); @@ -412,4 +473,3 @@ void idr_init(struct idr *idp) spin_lock_init(&idp->lock); } EXPORT_SYMBOL(idr_init); -