blob: bf70b3ef0a073bbcb301ddf0141d90e8ac6fa018 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * include/linux/idr.h
3 *
4 * 2002-10-18 written by Jim Houston jim.houston@ccur.com
5 * Copyright (C) 2002 by Concurrent Computer Corporation
6 * Distributed under the GNU GPL license version 2.
7 *
8 * Small id to pointer translation service avoiding fixed sized
9 * tables.
10 */
Luben Tuikovf668ab12005-11-08 17:14:08 +010011
12#ifndef __IDR_H__
13#define __IDR_H__
14
Matthew Wilcox0a835c42016-12-20 10:27:56 -050015#include <linux/radix-tree.h>
16#include <linux/gfp.h>
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -050017#include <linux/percpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19struct idr {
Matthew Wilcox0a835c42016-12-20 10:27:56 -050020 struct radix_tree_root idr_rt;
21 unsigned int idr_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022};
23
Matthew Wilcox0a835c42016-12-20 10:27:56 -050024/*
25 * The IDR API does not expose the tagging functionality of the radix tree
26 * to users. Use tag 0 to track whether a node has free space below it.
27 */
28#define IDR_FREE 0
29
30/* Set the IDR flag and the IDR_FREE tag */
31#define IDR_RT_MARKER ((__force gfp_t)(3 << __GFP_BITS_SHIFT))
32
33#define IDR_INIT \
Tejun Heo4106eca2013-02-27 17:03:51 -080034{ \
Matthew Wilcox0a835c42016-12-20 10:27:56 -050035 .idr_rt = RADIX_TREE_INIT(IDR_RT_MARKER) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
Matthew Wilcox0a835c42016-12-20 10:27:56 -050037#define DEFINE_IDR(name) struct idr name = IDR_INIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Nadia Derbeyf9c46d62008-07-25 01:48:01 -070039/**
Matthew Wilcox44430612016-12-14 15:09:19 -080040 * idr_get_cursor - Return the current position of the cyclic allocator
41 * @idr: idr handle
42 *
43 * The value returned is the value that will be next returned from
44 * idr_alloc_cyclic() if it is free (otherwise the search will start from
45 * this position).
46 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050047static inline unsigned int idr_get_cursor(const struct idr *idr)
Matthew Wilcox44430612016-12-14 15:09:19 -080048{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050049 return READ_ONCE(idr->idr_next);
Matthew Wilcox44430612016-12-14 15:09:19 -080050}
51
52/**
53 * idr_set_cursor - Set the current position of the cyclic allocator
54 * @idr: idr handle
55 * @val: new position
56 *
57 * The next call to idr_alloc_cyclic() will return @val if it is free
58 * (otherwise the search will start from this position).
59 */
60static inline void idr_set_cursor(struct idr *idr, unsigned int val)
61{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050062 WRITE_ONCE(idr->idr_next, val);
Matthew Wilcox44430612016-12-14 15:09:19 -080063}
64
65/**
Randy Dunlap56083ab2010-10-26 14:19:08 -070066 * DOC: idr sync
Nadia Derbeyf9c46d62008-07-25 01:48:01 -070067 * idr synchronization (stolen from radix-tree.h)
68 *
69 * idr_find() is able to be called locklessly, using RCU. The caller must
70 * ensure calls to this function are made within rcu_read_lock() regions.
71 * Other readers (lock-free or otherwise) and modifications may be running
72 * concurrently.
73 *
74 * It is still required that the caller manage the synchronization and
75 * lifetimes of the items. So if RCU lock-free lookups are used, typically
76 * this would mean that the items have their own locks, or are amenable to
77 * lock-free access; and that the items are freed by RCU (or only freed after
78 * having been deleted from the idr tree *and* a synchronize_rcu() grace
79 * period).
80 */
81
Tejun Heod5c74092013-02-27 17:03:55 -080082void idr_preload(gfp_t gfp_mask);
Matthew Wilcox0a835c42016-12-20 10:27:56 -050083int idr_alloc(struct idr *, void *entry, int start, int end, gfp_t);
84int idr_alloc_cyclic(struct idr *, void *entry, int start, int end, gfp_t);
85int idr_for_each(const struct idr *,
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -070086 int (*fn)(int id, void *p, void *data), void *data);
Matthew Wilcox0a835c42016-12-20 10:27:56 -050087void *idr_get_next(struct idr *, int *nextid);
88void *idr_replace(struct idr *, void *, int id);
89void idr_destroy(struct idr *);
90
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -050091static inline void *idr_remove(struct idr *idr, int id)
Matthew Wilcox0a835c42016-12-20 10:27:56 -050092{
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -050093 return radix_tree_delete_item(&idr->idr_rt, id, NULL);
Matthew Wilcox0a835c42016-12-20 10:27:56 -050094}
95
96static inline void idr_init(struct idr *idr)
97{
98 INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
99 idr->idr_next = 0;
100}
101
102static inline bool idr_is_empty(const struct idr *idr)
103{
104 return radix_tree_empty(&idr->idr_rt) &&
105 radix_tree_tagged(&idr->idr_rt, IDR_FREE);
106}
Luben Tuikovf668ab12005-11-08 17:14:08 +0100107
Tejun Heo49038ef2013-02-27 17:03:52 -0800108/**
Tejun Heod5c74092013-02-27 17:03:55 -0800109 * idr_preload_end - end preload section started with idr_preload()
110 *
111 * Each idr_preload() should be matched with an invocation of this
112 * function. See idr_preload() for details.
113 */
114static inline void idr_preload_end(void)
115{
116 preempt_enable();
117}
118
119/**
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800120 * idr_find - return pointer for given id
Randy Dunlap5857f702013-03-04 14:32:54 -0800121 * @idr: idr handle
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800122 * @id: lookup key
123 *
124 * Return the pointer given the id it has been registered with. A %NULL
125 * return indicates that @id is not valid or you passed %NULL in
126 * idr_get_new().
127 *
128 * This function can be called under rcu_read_lock(), given that the leaf
129 * pointers lifetimes are correctly managed.
130 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500131static inline void *idr_find(const struct idr *idr, int id)
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800132{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500133 return radix_tree_lookup(&idr->idr_rt, id);
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800134}
135
136/**
Tejun Heo49038ef2013-02-27 17:03:52 -0800137 * idr_for_each_entry - iterate over an idr's elements of a given type
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500138 * @idr: idr handle
Tejun Heo49038ef2013-02-27 17:03:52 -0800139 * @entry: the type * to use as cursor
140 * @id: id entry's key
George Spelvinb949be52013-03-27 14:08:33 +0100141 *
142 * @entry and @id do not need to be initialized before the loop, and
143 * after normal terminatinon @entry is left with the value NULL. This
144 * is convenient for a "not found" value.
Tejun Heo49038ef2013-02-27 17:03:52 -0800145 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500146#define idr_for_each_entry(idr, entry, id) \
147 for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
Tejun Heo49038ef2013-02-27 17:03:52 -0800148
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200149/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500150 * idr_for_each_entry_continue - continue iteration over an idr's elements of a given type
151 * @idr: idr handle
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200152 * @entry: the type * to use as cursor
153 * @id: id entry's key
154 *
155 * Continue to iterate over list of given type, continuing after
156 * the current position.
157 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500158#define idr_for_each_entry_continue(idr, entry, id) \
159 for ((entry) = idr_get_next((idr), &(id)); \
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200160 entry; \
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500161 ++id, (entry) = idr_get_next((idr), &(id)))
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200162
Tejun Heoc8615d32013-03-13 14:59:42 -0700163/*
Tejun Heo72dba582007-06-14 03:45:13 +0900164 * IDA - IDR based id allocator, use when translation from id to
165 * pointer isn't necessary.
166 */
167#define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500168#define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
Namhyung Kimed9f5242010-09-16 01:30:19 +0900169#define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
Tejun Heo72dba582007-06-14 03:45:13 +0900170
171struct ida_bitmap {
Tejun Heo72dba582007-06-14 03:45:13 +0900172 unsigned long bitmap[IDA_BITMAP_LONGS];
173};
174
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500175DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
176
Tejun Heo72dba582007-06-14 03:45:13 +0900177struct ida {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500178 struct radix_tree_root ida_rt;
Tejun Heo72dba582007-06-14 03:45:13 +0900179};
180
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500181#define IDA_INIT { \
182 .ida_rt = RADIX_TREE_INIT(IDR_RT_MARKER | GFP_NOWAIT), \
183}
184#define DEFINE_IDA(name) struct ida name = IDA_INIT
Tejun Heo72dba582007-06-14 03:45:13 +0900185
186int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
187int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
Tejun Heo72dba582007-06-14 03:45:13 +0900188void ida_remove(struct ida *ida, int id);
189void ida_destroy(struct ida *ida);
Tejun Heo72dba582007-06-14 03:45:13 +0900190
Rusty Russell88eca022011-08-03 16:21:06 -0700191int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
192 gfp_t gfp_mask);
193void ida_simple_remove(struct ida *ida, unsigned int id);
194
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500195static inline void ida_init(struct ida *ida)
196{
197 INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500198}
199
Philipp Reisner9749f302011-07-20 14:59:37 +0200200/**
Tejun Heo49038ef2013-02-27 17:03:52 -0800201 * ida_get_new - allocate new ID
202 * @ida: idr handle
203 * @p_id: pointer to the allocated handle
204 *
205 * Simple wrapper around ida_get_new_above() w/ @starting_id of zero.
Philipp Reisner9749f302011-07-20 14:59:37 +0200206 */
Tejun Heo49038ef2013-02-27 17:03:52 -0800207static inline int ida_get_new(struct ida *ida, int *p_id)
208{
209 return ida_get_new_above(ida, 0, p_id);
210}
211
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500212static inline bool ida_is_empty(const struct ida *ida)
Matthew Wilcox99c49402016-12-14 15:09:13 -0800213{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500214 return radix_tree_empty(&ida->ida_rt);
Matthew Wilcox99c49402016-12-14 15:09:13 -0800215}
Luben Tuikovf668ab12005-11-08 17:14:08 +0100216#endif /* __IDR_H__ */