blob: e856f4e0ab359d085d33266ca4505ea941df01af [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;
Matthew Wilcox6ce711f2017-11-30 13:45:11 -050021 unsigned int idr_base;
Matthew Wilcox0a835c42016-12-20 10:27:56 -050022 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 */
Matthew Wilcoxfa290cd2018-04-10 16:36:28 -070032#define IDR_RT_MARKER (ROOT_IS_IDR | (__force gfp_t) \
33 (1 << (ROOT_TAG_SHIFT + IDR_FREE)))
Matthew Wilcox0a835c42016-12-20 10:27:56 -050034
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070035#define IDR_INIT_BASE(name, base) { \
36 .idr_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER), \
Matthew Wilcox6ce711f2017-11-30 13:45:11 -050037 .idr_base = (base), \
38 .idr_next = 0, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Nadia Derbeyf9c46d62008-07-25 01:48:01 -070041/**
Matthew Wilcox6ce711f2017-11-30 13:45:11 -050042 * IDR_INIT() - Initialise an IDR.
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070043 * @name: Name of IDR.
Matthew Wilcox6ce711f2017-11-30 13:45:11 -050044 *
45 * A freshly-initialised IDR contains no IDs.
46 */
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070047#define IDR_INIT(name) IDR_INIT_BASE(name, 0)
Matthew Wilcox6ce711f2017-11-30 13:45:11 -050048
49/**
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070050 * DEFINE_IDR() - Define a statically-allocated IDR.
51 * @name: Name of IDR.
Matthew Wilcoxac665d92018-02-06 15:05:49 -050052 *
53 * An IDR defined using this macro is ready for use with no additional
54 * initialisation required. It contains no IDs.
55 */
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -070056#define DEFINE_IDR(name) struct idr name = IDR_INIT(name)
Matthew Wilcoxac665d92018-02-06 15:05:49 -050057
58/**
Matthew Wilcox44430612016-12-14 15:09:19 -080059 * idr_get_cursor - Return the current position of the cyclic allocator
60 * @idr: idr handle
61 *
62 * The value returned is the value that will be next returned from
63 * idr_alloc_cyclic() if it is free (otherwise the search will start from
64 * this position).
65 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -050066static inline unsigned int idr_get_cursor(const struct idr *idr)
Matthew Wilcox44430612016-12-14 15:09:19 -080067{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050068 return READ_ONCE(idr->idr_next);
Matthew Wilcox44430612016-12-14 15:09:19 -080069}
70
71/**
72 * idr_set_cursor - Set the current position of the cyclic allocator
73 * @idr: idr handle
74 * @val: new position
75 *
76 * The next call to idr_alloc_cyclic() will return @val if it is free
77 * (otherwise the search will start from this position).
78 */
79static inline void idr_set_cursor(struct idr *idr, unsigned int val)
80{
Matthew Wilcox0a835c42016-12-20 10:27:56 -050081 WRITE_ONCE(idr->idr_next, val);
Matthew Wilcox44430612016-12-14 15:09:19 -080082}
83
84/**
Randy Dunlap56083ab2010-10-26 14:19:08 -070085 * DOC: idr sync
Nadia Derbeyf9c46d62008-07-25 01:48:01 -070086 * idr synchronization (stolen from radix-tree.h)
87 *
88 * idr_find() is able to be called locklessly, using RCU. The caller must
89 * ensure calls to this function are made within rcu_read_lock() regions.
90 * Other readers (lock-free or otherwise) and modifications may be running
91 * concurrently.
92 *
93 * It is still required that the caller manage the synchronization and
94 * lifetimes of the items. So if RCU lock-free lookups are used, typically
95 * this would mean that the items have their own locks, or are amenable to
96 * lock-free access; and that the items are freed by RCU (or only freed after
97 * having been deleted from the idr tree *and* a synchronize_rcu() grace
98 * period).
99 */
100
Tejun Heod5c74092013-02-27 17:03:55 -0800101void idr_preload(gfp_t gfp_mask);
Chris Mi388f79f2017-08-30 02:31:57 -0400102
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500103int idr_alloc(struct idr *, void *ptr, int start, int end, gfp_t);
104int __must_check idr_alloc_u32(struct idr *, void *ptr, u32 *id,
Matthew Wilcoxe096f6a2017-11-28 10:14:27 -0500105 unsigned long max, gfp_t);
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500106int idr_alloc_cyclic(struct idr *, void *ptr, int start, int end, gfp_t);
107void *idr_remove(struct idr *, unsigned long id);
108void *idr_find(const struct idr *, unsigned long id);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500109int idr_for_each(const struct idr *,
Kristian Hoegsberg96d7fa42007-07-15 23:37:24 -0700110 int (*fn)(int id, void *p, void *data), void *data);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500111void *idr_get_next(struct idr *, int *nextid);
Matthew Wilcox7a457572017-11-28 15:39:51 -0500112void *idr_get_next_ul(struct idr *, unsigned long *nextid);
Matthew Wilcox234a4622017-11-28 09:56:36 -0500113void *idr_replace(struct idr *, void *, unsigned long id);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500114void idr_destroy(struct idr *);
115
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500116/**
117 * idr_init_base() - Initialise an IDR.
118 * @idr: IDR handle.
119 * @base: The base value for the IDR.
120 *
121 * This variation of idr_init() creates an IDR which will allocate IDs
122 * starting at %base.
123 */
124static inline void idr_init_base(struct idr *idr, int base)
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500125{
126 INIT_RADIX_TREE(&idr->idr_rt, IDR_RT_MARKER);
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500127 idr->idr_base = base;
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500128 idr->idr_next = 0;
129}
130
Matthew Wilcox6ce711f2017-11-30 13:45:11 -0500131/**
132 * idr_init() - Initialise an IDR.
133 * @idr: IDR handle.
134 *
135 * Initialise a dynamically allocated IDR. To initialise a
136 * statically allocated IDR, use DEFINE_IDR().
137 */
138static inline void idr_init(struct idr *idr)
139{
140 idr_init_base(idr, 0);
141}
142
Matthew Wilcoxac665d92018-02-06 15:05:49 -0500143/**
144 * idr_is_empty() - Are there any IDs allocated?
145 * @idr: IDR handle.
146 *
147 * Return: %true if any IDs have been allocated from this IDR.
148 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500149static inline bool idr_is_empty(const struct idr *idr)
150{
151 return radix_tree_empty(&idr->idr_rt) &&
152 radix_tree_tagged(&idr->idr_rt, IDR_FREE);
153}
Luben Tuikovf668ab12005-11-08 17:14:08 +0100154
Tejun Heo49038ef2013-02-27 17:03:52 -0800155/**
Tejun Heod5c74092013-02-27 17:03:55 -0800156 * idr_preload_end - end preload section started with idr_preload()
157 *
158 * Each idr_preload() should be matched with an invocation of this
159 * function. See idr_preload() for details.
160 */
161static inline void idr_preload_end(void)
162{
163 preempt_enable();
164}
165
166/**
Matthew Wilcox7a457572017-11-28 15:39:51 -0500167 * idr_for_each_entry() - Iterate over an IDR's elements of a given type.
168 * @idr: IDR handle.
169 * @entry: The type * to use as cursor
170 * @id: Entry ID.
George Spelvinb949be52013-03-27 14:08:33 +0100171 *
172 * @entry and @id do not need to be initialized before the loop, and
Matthew Wilcox7a457572017-11-28 15:39:51 -0500173 * after normal termination @entry is left with the value NULL. This
George Spelvinb949be52013-03-27 14:08:33 +0100174 * is convenient for a "not found" value.
Tejun Heo49038ef2013-02-27 17:03:52 -0800175 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500176#define idr_for_each_entry(idr, entry, id) \
177 for (id = 0; ((entry) = idr_get_next(idr, &(id))) != NULL; ++id)
Tejun Heo49038ef2013-02-27 17:03:52 -0800178
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200179/**
Matthew Wilcox7a457572017-11-28 15:39:51 -0500180 * idr_for_each_entry_ul() - Iterate over an IDR's elements of a given type.
181 * @idr: IDR handle.
182 * @entry: The type * to use as cursor.
183 * @id: Entry ID.
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200184 *
Matthew Wilcox7a457572017-11-28 15:39:51 -0500185 * @entry and @id do not need to be initialized before the loop, and
186 * after normal termination @entry is left with the value NULL. This
187 * is convenient for a "not found" value.
188 */
189#define idr_for_each_entry_ul(idr, entry, id) \
190 for (id = 0; ((entry) = idr_get_next_ul(idr, &(id))) != NULL; ++id)
191
192/**
193 * idr_for_each_entry_continue() - Continue iteration over an IDR's elements of a given type
194 * @idr: IDR handle.
195 * @entry: The type * to use as a cursor.
196 * @id: Entry ID.
197 *
198 * Continue to iterate over entries, continuing after the current position.
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200199 */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500200#define idr_for_each_entry_continue(idr, entry, id) \
201 for ((entry) = idr_get_next((idr), &(id)); \
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200202 entry; \
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500203 ++id, (entry) = idr_get_next((idr), &(id)))
Andreas Gruenbachera55bbd32014-08-28 13:31:14 +0200204
Tejun Heoc8615d32013-03-13 14:59:42 -0700205/*
Tejun Heo72dba582007-06-14 03:45:13 +0900206 * IDA - IDR based id allocator, use when translation from id to
207 * pointer isn't necessary.
208 */
209#define IDA_CHUNK_SIZE 128 /* 128 bytes per chunk */
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500210#define IDA_BITMAP_LONGS (IDA_CHUNK_SIZE / sizeof(long))
Namhyung Kimed9f5242010-09-16 01:30:19 +0900211#define IDA_BITMAP_BITS (IDA_BITMAP_LONGS * sizeof(long) * 8)
Tejun Heo72dba582007-06-14 03:45:13 +0900212
213struct ida_bitmap {
Tejun Heo72dba582007-06-14 03:45:13 +0900214 unsigned long bitmap[IDA_BITMAP_LONGS];
215};
216
Matthew Wilcox7ad3d4d2016-12-16 11:55:56 -0500217DECLARE_PER_CPU(struct ida_bitmap *, ida_bitmap);
218
Tejun Heo72dba582007-06-14 03:45:13 +0900219struct ida {
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500220 struct radix_tree_root ida_rt;
Tejun Heo72dba582007-06-14 03:45:13 +0900221};
222
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700223#define IDA_INIT(name) { \
224 .ida_rt = RADIX_TREE_INIT(name, IDR_RT_MARKER | GFP_NOWAIT), \
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500225}
Matthew Wilcoxf6bb2a22018-04-10 16:36:52 -0700226#define DEFINE_IDA(name) struct ida name = IDA_INIT(name)
Tejun Heo72dba582007-06-14 03:45:13 +0900227
228int ida_pre_get(struct ida *ida, gfp_t gfp_mask);
229int ida_get_new_above(struct ida *ida, int starting_id, int *p_id);
Tejun Heo72dba582007-06-14 03:45:13 +0900230void ida_remove(struct ida *ida, int id);
231void ida_destroy(struct ida *ida);
Tejun Heo72dba582007-06-14 03:45:13 +0900232
Rusty Russell88eca022011-08-03 16:21:06 -0700233int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
234 gfp_t gfp_mask);
235void ida_simple_remove(struct ida *ida, unsigned int id);
236
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500237static inline void ida_init(struct ida *ida)
238{
239 INIT_RADIX_TREE(&ida->ida_rt, IDR_RT_MARKER | GFP_NOWAIT);
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500240}
241
Philipp Reisner9749f302011-07-20 14:59:37 +0200242/**
Tejun Heo49038ef2013-02-27 17:03:52 -0800243 * ida_get_new - allocate new ID
244 * @ida: idr handle
245 * @p_id: pointer to the allocated handle
246 *
247 * Simple wrapper around ida_get_new_above() w/ @starting_id of zero.
Philipp Reisner9749f302011-07-20 14:59:37 +0200248 */
Tejun Heo49038ef2013-02-27 17:03:52 -0800249static inline int ida_get_new(struct ida *ida, int *p_id)
250{
251 return ida_get_new_above(ida, 0, p_id);
252}
253
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500254static inline bool ida_is_empty(const struct ida *ida)
Matthew Wilcox99c49402016-12-14 15:09:13 -0800255{
Matthew Wilcox0a835c42016-12-20 10:27:56 -0500256 return radix_tree_empty(&ida->ida_rt);
Matthew Wilcox99c49402016-12-14 15:09:13 -0800257}
Luben Tuikovf668ab12005-11-08 17:14:08 +0100258#endif /* __IDR_H__ */