[PATCH 07/10] This patch introduces the ridr_find() routine. Signed-off-by: Nadia Derbey --- include/linux/ridr.h | 17 +++++++++++++++++ lib/ridr.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) Index: linux-2.6.25-mm1/include/linux/ridr.h =================================================================== --- linux-2.6.25-mm1.orig/include/linux/ridr.h 2008-04-29 14:16:09.000000000 +0200 +++ linux-2.6.25-mm1/include/linux/ridr.h 2008-04-29 14:31:43.000000000 +0200 @@ -40,9 +40,26 @@ struct ridr { #define idr_to_ridr(p) container_of((p), struct ridr_layer, idr) #define ridr_to_idr(p) (&((p)->idr)) +/** + * Ridr synchronization (see radix-tree.h) + * + * ridr_find() is able to be called locklessly, using RCU. The caller must + * ensure calls to this function are made within rcu_read_lock() regions. + * Other readers (lock-free or otherwise) and modifications may be running + * concurrently. + * + * It is still required that the caller manage the synchronization and + * lifetimes of the items. So if RCU lock-free lookups are used, typically + * this would mean that the items have their own locks, or are amenable to + * lock-free access; and that the items are freed by RCU (or only freed after + * having been deleted from the ridr tree *and* a synchronize_rcu() grace + * period). + */ + /* * This is what we export. */ +void *ridr_find(struct ridr *, int); int ridr_pre_get(struct ridr *, gfp_t); int ridr_get_new(struct ridr *, void *, int *); int ridr_get_new_above(struct ridr *, void *, int, int *); Index: linux-2.6.25-mm1/lib/ridr.c =================================================================== --- linux-2.6.25-mm1.orig/lib/ridr.c 2008-04-29 14:17:05.000000000 +0200 +++ linux-2.6.25-mm1/lib/ridr.c 2008-04-29 14:32:26.000000000 +0200 @@ -259,6 +259,40 @@ int ridr_get_new(struct ridr *idp, void } EXPORT_SYMBOL(ridr_get_new); +/** + * ridr_find - return pointer for given id + * @idp: ridr 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 + * ridr_get_new(). + * + * This function can be called under rcu_read_lock(), given that the leaf + * pointers lifetimes are correctly managed. + */ +void *ridr_find(struct ridr *idp, int id) +{ + int n; + struct ridr_layer *p; + + n = idp->layers * IDR_BITS; + p = rcu_dereference(idp->top); + + /* 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 = rcu_dereference(p->idr.ary[(id >> n) & IDR_MASK]); + } + return((void *)p); +} +EXPORT_SYMBOL(ridr_find); + static void ridr_cache_ctor(struct kmem_cache *ridr_layer_cache, void *ridr_layer) { -- -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/