blob: fa14f834e4ede3a81e7e6182c889fec338b2495f [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>
Wei Wangc47d7f52017-12-14 15:32:24 -080018#include <linux/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20struct idr {
Matthew Wilcox0a835c42016-12-20 10:27:56 -050021 struct radix_tree_root idr_rt;
22 unsigned int idr_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023};
24
Matthew Wilcox0a835c42016-12-20 10:27:56 -050025/*
26 * The IDR API does not expose the tagging functionality of the radix tree
27 * to users. Use tag 0 to track whether a node has free space below it.
28 */
29#define IDR_FREE 0
30
31/* Set the IDR flag and the IDR_FREE tag */
32#define IDR_RT_MARKER ((__force gfp_t)(3 << __GFP_BITS_SHIFT))
33
34#define IDR_INIT \
Tejun Heo4106eca2013-02-27 17:03:51 -080035{ \
Matthew Wilcox0a835c42016-12-20 10:27:56 -050036 .idr_rt = RADIX_TREE_INIT(IDR_RT_MARKER) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
Matthew Wilcox0a835c42016-12-20 10:27:56 -050038#define DEFINE_IDR(name) struct idr name = IDR_INIT
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Nadia Derbeyf9c46d62008-07-25 01:48:01 -070040/**
Matthew Wilcox44430612016-12-14 15:09:19 -080041 * idr_get_cursor - Return the current position of the cyclic allocator
42 * @idr: idr handle
43 *
44 * The value returned is the value that will be next returned from
45 * idr_alloc_cyclic() if it is free (otherwise the search will start from
46 * this position).
47 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050048static inline unsigned int idr_get_cursor(const struct idr *idr)
Matthew Wilcox44430612016-12-14 15:09:19 -080049{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050050 return READ_ONCE(idr->idr_next);
Matthew Wilcox44430612016-12-14 15:09:19 -080051}
52
53/**
54 * idr_set_cursor - Set the current position of the cyclic allocator
55 * @idr: idr handle
56 * @val: new position
57 *
58 * The next call to idr_alloc_cyclic() will return @val if it is free
59 * (otherwise the search will start from this position).
60 */
61static inline void idr_set_cursor(struct idr *idr, unsigned int val)
62{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050063 WRITE_ONCE(idr->idr_next, val);
Matthew Wilcox44430612016-12-14 15:09:19 -080064}
65
66/**
Randy Dunlap56083ab2010-10-26 14:19:08 -070067 * DOC: idr sync
Nadia Derbeyf9c46d62008-07-25 01:48:01 -070068 * idr synchronization (stolen from radix-tree.h)
69 *
70 * idr_find() is able to be called locklessly, using RCU. The caller must
71 * ensure calls to this function are made within rcu_read_lock() regions.
72 * Other readers (lock-free or otherwise) and modifications may be running
73 * concurrently.
74 *
75 * It is still required that the caller manage the synchronization and
76 * lifetimes of the items. So if RCU lock-free lookups are used, typically
77 * this would mean that the items have their own locks, or are amenable to
78 * lock-free access; and that the items are freed by RCU (or only freed after
79 * having been deleted from the idr tree *and* a synchronize_rcu() grace
80 * period).
81 */
82
Tejun Heod5c74092013-02-27 17:03:55 -080083void idr_preload(gfp_t gfp_mask);
Chris Mi388f79f2017-08-30 02:31:57 -040084
85int idr_alloc_cmn(struct idr *idr, void *ptr, unsigned long *index,
86 unsigned long start, unsigned long end, gfp_t gfp,
87 bool ext);
88
89/**
90 * idr_alloc - allocate an id
91 * @idr: idr handle
92 * @ptr: pointer to be associated with the new id
93 * @start: the minimum id (inclusive)
94 * @end: the maximum id (exclusive)
95 * @gfp: memory allocation flags
96 *
97 * Allocates an unused ID in the range [start, end). Returns -ENOSPC
98 * if there are no unused IDs in that range.
99 *
100 * Note that @end is treated as max when <= 0. This is to always allow
101 * using @start + N as @end as long as N is inside integer range.
102 *
103 * Simultaneous modifications to the @idr are not allowed and should be
104 * prevented by the user, usually with a lock. idr_alloc() may be called
105 * concurrently with read-only accesses to the @idr, such as idr_find() and
106 * idr_for_each_entry().
107 */
108static inline int idr_alloc(struct idr *idr, void *ptr,
109 int start, int end, gfp_t gfp)
110{
111 unsigned long id;
112 int ret;
113
114 if (WARN_ON_ONCE(start < 0))
115 return -EINVAL;
116
117 ret = idr_alloc_cmn(idr, ptr, &id, start, end, gfp, false);
118
119 if (ret)
120 return ret;
121
122 return id;
123}
124
125static inline int idr_alloc_ext(struct idr *idr, void *ptr,
126 unsigned long *index,
127 unsigned long start,
128 unsigned long end,
129 gfp_t gfp)
130{
131 return idr_alloc_cmn(idr, ptr, index, start, end, gfp, true);
132}
133
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500134int idr_alloc_cyclic(struct idr *, void *entry, int start, int end, gfp_t);
135int idr_for_each(const struct idr *,
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700136 int (*fn)(int id, void *p, void *data), void *data);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500137void *idr_get_next(struct idr *, int *nextid);
Chris Mi388f79f2017-08-30 02:31:57 -0400138void *idr_get_next_ext(struct idr *idr, unsigned long *nextid);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500139void *idr_replace(struct idr *, void *, int id);
Chris Mi388f79f2017-08-30 02:31:57 -0400140void *idr_replace_ext(struct idr *idr, void *ptr, unsigned long id);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500141void idr_destroy(struct idr *);
142
Chris Mi388f79f2017-08-30 02:31:57 -0400143static inline void *idr_remove_ext(struct idr *idr, unsigned long id)
144{
145 return radix_tree_delete_item(&idr->idr_rt, id, NULL);
146}
147
Matthew Wilcoxd3e709e2016-12-22 13:30:22 -0500148static inline void *idr_remove(struct idr *idr, int id)
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500149{
Chris Mi388f79f2017-08-30 02:31:57 -0400150 return idr_remove_ext(idr, id);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500151}
152
153static inline void idr_init(struct idr *idr)
154{
155 INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
156 idr->idr_next = 0;
157}
158
159static inline bool idr_is_empty(const struct idr *idr)
160{
161 return radix_tree_empty(&idr->idr_rt) &&
162 radix_tree_tagged(&idr->idr_rt, IDR_FREE);
163}
Luben Tuikovf668ab12005-11-08 17:14:08 +0100164
Tejun Heo49038ef2013-02-27 17:03:52 -0800165/**
Tejun Heod5c74092013-02-27 17:03:55 -0800166 * idr_preload_end - end preload section started with idr_preload()
167 *
168 * Each idr_preload() should be matched with an invocation of this
169 * function. See idr_preload() for details.
170 */
171static inline void idr_preload_end(void)
172{
173 preempt_enable();
174}
175
176/**
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800177 * idr_find - return pointer for given id
Randy Dunlap5857f702013-03-04 14:32:54 -0800178 * @idr: idr handle
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800179 * @id: lookup key
180 *
181 * Return the pointer given the id it has been registered with. A %NULL
182 * return indicates that @id is not valid or you passed %NULL in
183 * idr_get_new().
184 *
185 * This function can be called under rcu_read_lock(), given that the leaf
186 * pointers lifetimes are correctly managed.
187 */
Chris Mi388f79f2017-08-30 02:31:57 -0400188static inline void *idr_find_ext(const struct idr *idr, unsigned long id)
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800189{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500190 return radix_tree_lookup(&idr->idr_rt, id);
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800191}
192
Chris Mi388f79f2017-08-30 02:31:57 -0400193static inline void *idr_find(const struct idr *idr, int id)
194{
195 return idr_find_ext(idr, id);
196}
197
Tejun Heo0ffc2a92013-02-27 17:05:08 -0800198/**
Tejun Heo49038ef2013-02-27 17:03:52 -0800199 * idr_for_each_entry - iterate over an idr's elements of a given type
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500200 * @idr: idr handle
Tejun Heo49038ef2013-02-27 17:03:52 -0800201 * @entry: the type * to use as cursor
202 * @id: id entry's key
George Spelvinb949be52013-03-27 14:08:33 +0100203 *
204 * @entry and @id do not need to be initialized before the loop, and
205 * after normal terminatinon @entry is left with the value NULL. This
206 * is convenient for a "not found" value.
Tejun Heo49038ef2013-02-27 17:03:52 -0800207 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500208#define idr_for_each_entry(idr, entry, id) \
209 for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
Chris Mi388f79f2017-08-30 02:31:57 -0400210#define idr_for_each_entry_ext(idr, entry, id) \
211 for (id = 0; ((entry) = idr_get_next_ext(idr, &(id))) != NULL; ++id)
Tejun Heo49038ef2013-02-27 17:03:52 -0800212
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200213/**
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500214 * idr_for_each_entry_continue - continue iteration over an idr's elements of a given type
215 * @idr: idr handle
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200216 * @entry: the type * to use as cursor
217 * @id: id entry's key
218 *
219 * Continue to iterate over list of given type, continuing after
220 * the current position.
221 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500222#define idr_for_each_entry_continue(idr, entry, id) \
223 for ((entry) = idr_get_next((idr), &(id)); \
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200224 entry; \
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500225 ++id, (entry) = idr_get_next((idr), &(id)))
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200226
Tejun Heoc8615d32013-03-13 14:59:42 -0700227/*
Tejun Heo72dba582007-06-14 03:45:13 +0900228 * IDA - IDR based id allocator, use when translation from id to
229 * pointer isn't necessary.
230 */
231#define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500232#define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
Namhyung Kimed9f5242010-09-16 01:30:19 +0900233#define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
Tejun Heo72dba582007-06-14 03:45:13 +0900234
235struct ida_bitmap {
Tejun Heo72dba582007-06-14 03:45:13 +0900236 unsigned long bitmap[IDA_BITMAP_LONGS];
237};
238
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500239DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
240
Tejun Heo72dba582007-06-14 03:45:13 +0900241struct ida {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500242 struct radix_tree_root ida_rt;
Tejun Heo72dba582007-06-14 03:45:13 +0900243};
244
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500245#define IDA_INIT { \
246 .ida_rt = RADIX_TREE_INIT(IDR_RT_MARKER | GFP_NOWAIT), \
247}
248#define DEFINE_IDA(name) struct ida name = IDA_INIT
Tejun Heo72dba582007-06-14 03:45:13 +0900249
250int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
251int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
Tejun Heo72dba582007-06-14 03:45:13 +0900252void ida_remove(struct ida *ida, int id);
253void ida_destroy(struct ida *ida);
Tejun Heo72dba582007-06-14 03:45:13 +0900254
Rusty Russell88eca022011-08-03 16:21:06 -0700255int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
256 gfp_t gfp_mask);
257void ida_simple_remove(struct ida *ida, unsigned int id);
258
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500259static inline void ida_init(struct ida *ida)
260{
261 INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500262}
263
Philipp Reisner9749f302011-07-20 14:59:37 +0200264/**
Tejun Heo49038ef2013-02-27 17:03:52 -0800265 * ida_get_new - allocate new ID
266 * @ida: idr handle
267 * @p_id: pointer to the allocated handle
268 *
269 * Simple wrapper around ida_get_new_above() w/ @starting_id of zero.
Philipp Reisner9749f302011-07-20 14:59:37 +0200270 */
Tejun Heo49038ef2013-02-27 17:03:52 -0800271static inline int ida_get_new(struct ida *ida, int *p_id)
272{
273 return ida_get_new_above(ida, 0, p_id);
274}
275
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500276static inline bool ida_is_empty(const struct ida *ida)
Matthew Wilcox99c49402016-12-14 15:09:13 -0800277{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500278 return radix_tree_empty(&ida->ida_rt);
Matthew Wilcox99c49402016-12-14 15:09:13 -0800279}
Luben Tuikovf668ab12005-11-08 17:14:08 +0100280#endif /* __IDR_H__ */