idr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * 2002-10-18 written by Jim Houston jim.houston@ccur.com
  3. * Copyright (C) 2002 by Concurrent Computer Corporation
  4. * Distributed under the GNU GPL license version 2.
  5. *
  6. * Modified by George Anzinger to reuse immediately and to use
  7. * find bit instructions. Also removed _irq on spinlocks.
  8. *
  9. * Modified by Nadia Derbey to make it RCU safe.
  10. *
  11. * Small id to pointer translation service.
  12. *
  13. * It uses a radix tree like structure as a sparse array indexed
  14. * by the id to obtain the pointer. The bitmap makes allocating
  15. * a new id quick.
  16. *
  17. * You call it to allocate an id (an int) an associate with that id a
  18. * pointer or what ever, we treat it as a (void *). You can pass this
  19. * id to a user for him to pass back at a later time. You then pass
  20. * that id to this code and it returns your pointer.
  21. * You can release ids at any time. When all ids are released, most of
  22. * the memory is returned (we keep MAX_IDR_FREE) in a local pool so we
  23. * don't need to go to the memory "store" during an id allocate, just
  24. * so you don't need to be too concerned about locking and conflicts
  25. * with the slab allocator.
  26. */
  27. #ifndef TEST // to test in user space...
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/export.h>
  31. #endif
  32. #include <linux/err.h>
  33. #include <linux/string.h>
  34. #include <linux/idr.h>
  35. #include <linux/spinlock.h>
  36. static struct kmem_cache *idr_layer_cache;
  37. static DEFINE_SPINLOCK(simple_ida_lock);
  38. static struct idr_layer *get_from_free_list(struct idr *idp)
  39. {
  40. struct idr_layer *p;
  41. unsigned long flags;
  42. spin_lock_irqsave(&idp->lock, flags);
  43. if ((p = idp->id_free)) {
  44. idp->id_free = p->ary[0];
  45. idp->id_free_cnt--;
  46. p->ary[0] = NULL;
  47. }
  48. spin_unlock_irqrestore(&idp->lock, flags);
  49. return(p);
  50. }
  51. static void idr_layer_rcu_free(struct rcu_head *head)
  52. {
  53. struct idr_layer *layer;
  54. layer = container_of(head, struct idr_layer, rcu_head);
  55. kmem_cache_free(idr_layer_cache, layer);
  56. }
  57. static inline void free_layer(struct idr_layer *p)
  58. {
  59. call_rcu(&p->rcu_head, idr_layer_rcu_free);
  60. }
  61. /* only called when idp->lock is held */
  62. static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
  63. {
  64. p->ary[0] = idp->id_free;
  65. idp->id_free = p;
  66. idp->id_free_cnt++;
  67. }
  68. static void move_to_free_list(struct idr *idp, struct idr_layer *p)
  69. {
  70. unsigned long flags;
  71. /*
  72. * Depends on the return element being zeroed.
  73. */
  74. spin_lock_irqsave(&idp->lock, flags);
  75. __move_to_free_list(idp, p);
  76. spin_unlock_irqrestore(&idp->lock, flags);
  77. }
  78. static void idr_mark_full(struct idr_layer **pa, int id)
  79. {
  80. struct idr_layer *p = pa[0];
  81. int l = 0;
  82. __set_bit(id & IDR_MASK, &p->bitmap);
  83. /*
  84. * If this layer is full mark the bit in the layer above to
  85. * show that this part of the radix tree is full. This may
  86. * complete the layer above and require walking up the radix
  87. * tree.
  88. */
  89. while (p->bitmap == IDR_FULL) {
  90. if (!(p = pa[++l]))
  91. break;
  92. id = id >> IDR_BITS;
  93. __set_bit((id & IDR_MASK), &p->bitmap);
  94. }
  95. }
  96. /**
  97. * idr_pre_get - reserve resources for idr allocation
  98. * @idp: idr handle
  99. * @gfp_mask: memory allocation flags
  100. *
  101. * This function should be called prior to calling the idr_get_new* functions.
  102. * It preallocates enough memory to satisfy the worst possible allocation. The
  103. * caller should pass in GFP_KERNEL if possible. This of course requires that
  104. * no spinning locks be held.
  105. *
  106. * If the system is REALLY out of memory this function returns %0,
  107. * otherwise %1.
  108. */
  109. int idr_pre_get(struct idr *idp, gfp_t gfp_mask)
  110. {
  111. while (idp->id_free_cnt < MAX_IDR_FREE) {
  112. struct idr_layer *new;
  113. new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
  114. if (new == NULL)
  115. return (0);
  116. move_to_free_list(idp, new);
  117. }
  118. return 1;
  119. }
  120. EXPORT_SYMBOL(idr_pre_get);
  121. /**
  122. * sub_alloc - try to allocate an id without growing the tree depth
  123. * @idp: idr handle
  124. * @starting_id: id to start search at
  125. * @id: pointer to the allocated handle
  126. * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer
  127. *
  128. * Allocate an id in range [@starting_id, INT_MAX] from @idp without
  129. * growing its depth. Returns
  130. *
  131. * the allocated id >= 0 if successful,
  132. * -EAGAIN if the tree needs to grow for allocation to succeed,
  133. * -ENOSPC if the id space is exhausted,
  134. * -ENOMEM if more idr_layers need to be allocated.
  135. */
  136. static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa)
  137. {
  138. int n, m, sh;
  139. struct idr_layer *p, *new;
  140. int l, id, oid;
  141. unsigned long bm;
  142. id = *starting_id;
  143. restart:
  144. p = idp->top;
  145. l = idp->layers;
  146. pa[l--] = NULL;
  147. while (1) {
  148. /*
  149. * We run around this while until we reach the leaf node...
  150. */
  151. n = (id >> (IDR_BITS*l)) & IDR_MASK;
  152. bm = ~p->bitmap;
  153. m = find_next_bit(&bm, IDR_SIZE, n);
  154. if (m == IDR_SIZE) {
  155. /* no space available go back to previous layer. */
  156. l++;
  157. oid = id;
  158. id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
  159. /* if already at the top layer, we need to grow */
  160. if (id >= 1 << (idp->layers * IDR_BITS)) {
  161. *starting_id = id;
  162. return -EAGAIN;
  163. }
  164. p = pa[l];
  165. BUG_ON(!p);
  166. /* If we need to go up one layer, continue the
  167. * loop; otherwise, restart from the top.
  168. */
  169. sh = IDR_BITS * (l + 1);
  170. if (oid >> sh == id >> sh)
  171. continue;
  172. else
  173. goto restart;
  174. }
  175. if (m != n) {
  176. sh = IDR_BITS*l;
  177. id = ((id >> sh) ^ n ^ m) << sh;
  178. }
  179. if ((id >= MAX_IDR_BIT) || (id < 0))
  180. return -ENOSPC;
  181. if (l == 0)
  182. break;
  183. /*
  184. * Create the layer below if it is missing.
  185. */
  186. if (!p->ary[m]) {
  187. new = get_from_free_list(idp);
  188. if (!new)
  189. return -ENOMEM;
  190. new->layer = l-1;
  191. rcu_assign_pointer(p->ary[m], new);
  192. p->count++;
  193. }
  194. pa[l--] = p;
  195. p = p->ary[m];
  196. }
  197. pa[l] = p;
  198. return id;
  199. }
  200. static int idr_get_empty_slot(struct idr *idp, int starting_id,
  201. struct idr_layer **pa)
  202. {
  203. struct idr_layer *p, *new;
  204. int layers, v, id;
  205. unsigned long flags;
  206. id = starting_id;
  207. build_up:
  208. p = idp->top;
  209. layers = idp->layers;
  210. if (unlikely(!p)) {
  211. if (!(p = get_from_free_list(idp)))
  212. return -ENOMEM;
  213. p->layer = 0;
  214. layers = 1;
  215. }
  216. /*
  217. * Add a new layer to the top of the tree if the requested
  218. * id is larger than the currently allocated space.
  219. */
  220. while ((layers < (MAX_IDR_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
  221. layers++;
  222. if (!p->count) {
  223. /* special case: if the tree is currently empty,
  224. * then we grow the tree by moving the top node
  225. * upwards.
  226. */
  227. p->layer++;
  228. continue;
  229. }
  230. if (!(new = get_from_free_list(idp))) {
  231. /*
  232. * The allocation failed. If we built part of
  233. * the structure tear it down.
  234. */
  235. spin_lock_irqsave(&idp->lock, flags);
  236. for (new = p; p && p != idp->top; new = p) {
  237. p = p->ary[0];
  238. new->ary[0] = NULL;
  239. new->bitmap = new->count = 0;
  240. __move_to_free_list(idp, new);
  241. }
  242. spin_unlock_irqrestore(&idp->lock, flags);
  243. return -ENOMEM;
  244. }
  245. new->ary[0] = p;
  246. new->count = 1;
  247. new->layer = layers-1;
  248. if (p->bitmap == IDR_FULL)
  249. __set_bit(0, &new->bitmap);
  250. p = new;
  251. }
  252. rcu_assign_pointer(idp->top, p);
  253. idp->layers = layers;
  254. v = sub_alloc(idp, &id, pa);
  255. if (v == -EAGAIN)
  256. goto build_up;
  257. return(v);
  258. }
  259. /*
  260. * @id and @pa are from a successful allocation from idr_get_empty_slot().
  261. * Install the user pointer @ptr and mark the slot full.
  262. */
  263. static void idr_fill_slot(void *ptr, int id, struct idr_layer **pa)
  264. {
  265. rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
  266. pa[0]->count++;
  267. idr_mark_full(pa, id);
  268. }
  269. /**
  270. * idr_get_new_above - allocate new idr entry above or equal to a start id
  271. * @idp: idr handle
  272. * @ptr: pointer you want associated with the id
  273. * @starting_id: id to start search at
  274. * @id: pointer to the allocated handle
  275. *
  276. * This is the allocate id function. It should be called with any
  277. * required locks.
  278. *
  279. * If allocation from IDR's private freelist fails, idr_get_new_above() will
  280. * return %-EAGAIN. The caller should retry the idr_pre_get() call to refill
  281. * IDR's preallocation and then retry the idr_get_new_above() call.
  282. *
  283. * If the idr is full idr_get_new_above() will return %-ENOSPC.
  284. *
  285. * @id returns a value in the range @starting_id ... %0x7fffffff
  286. */
  287. int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
  288. {
  289. struct idr_layer *pa[MAX_IDR_LEVEL];
  290. int rv;
  291. rv = idr_get_empty_slot(idp, starting_id, pa);
  292. if (rv < 0)
  293. return rv == -ENOMEM ? -EAGAIN : rv;
  294. idr_fill_slot(ptr, rv, pa);
  295. *id = rv;
  296. return 0;
  297. }
  298. EXPORT_SYMBOL(idr_get_new_above);
  299. static void idr_remove_warning(int id)
  300. {
  301. printk(KERN_WARNING
  302. "idr_remove called for id=%d which is not allocated.\n", id);
  303. dump_stack();
  304. }
  305. static void sub_remove(struct idr *idp, int shift, int id)
  306. {
  307. struct idr_layer *p = idp->top;
  308. struct idr_layer **pa[MAX_IDR_LEVEL];
  309. struct idr_layer ***paa = &pa[0];
  310. struct idr_layer *to_free;
  311. int n;
  312. *paa = NULL;
  313. *++paa = &idp->top;
  314. while ((shift > 0) && p) {
  315. n = (id >> shift) & IDR_MASK;
  316. __clear_bit(n, &p->bitmap);
  317. *++paa = &p->ary[n];
  318. p = p->ary[n];
  319. shift -= IDR_BITS;
  320. }
  321. n = id & IDR_MASK;
  322. if (likely(p != NULL && test_bit(n, &p->bitmap))){
  323. __clear_bit(n, &p->bitmap);
  324. rcu_assign_pointer(p->ary[n], NULL);
  325. to_free = NULL;
  326. while(*paa && ! --((**paa)->count)){
  327. if (to_free)
  328. free_layer(to_free);
  329. to_free = **paa;
  330. **paa-- = NULL;
  331. }
  332. if (!*paa)
  333. idp->layers = 0;
  334. if (to_free)
  335. free_layer(to_free);
  336. } else
  337. idr_remove_warning(id);
  338. }
  339. /**
  340. * idr_remove - remove the given id and free its slot
  341. * @idp: idr handle
  342. * @id: unique key
  343. */
  344. void idr_remove(struct idr *idp, int id)
  345. {
  346. struct idr_layer *p;
  347. struct idr_layer *to_free;
  348. /* Mask off upper bits we don't use for the search. */
  349. id &= MAX_IDR_MASK;
  350. sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
  351. if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
  352. idp->top->ary[0]) {
  353. /*
  354. * Single child at leftmost slot: we can shrink the tree.
  355. * This level is not needed anymore since when layers are
  356. * inserted, they are inserted at the top of the existing
  357. * tree.
  358. */
  359. to_free = idp->top;
  360. p = idp->top->ary[0];
  361. rcu_assign_pointer(idp->top, p);
  362. --idp->layers;
  363. to_free->bitmap = to_free->count = 0;
  364. free_layer(to_free);
  365. }
  366. while (idp->id_free_cnt >= MAX_IDR_FREE) {
  367. p = get_from_free_list(idp);
  368. /*
  369. * Note: we don't call the rcu callback here, since the only
  370. * layers that fall into the freelist are those that have been
  371. * preallocated.
  372. */
  373. kmem_cache_free(idr_layer_cache, p);
  374. }
  375. return;
  376. }
  377. EXPORT_SYMBOL(idr_remove);
  378. void __idr_remove_all(struct idr *idp)
  379. {
  380. int n, id, max;
  381. int bt_mask;
  382. struct idr_layer *p;
  383. struct idr_layer *pa[MAX_IDR_LEVEL];
  384. struct idr_layer **paa = &pa[0];
  385. n = idp->layers * IDR_BITS;
  386. p = idp->top;
  387. rcu_assign_pointer(idp->top, NULL);
  388. max = 1 << n;
  389. id = 0;
  390. while (id < max) {
  391. while (n > IDR_BITS && p) {
  392. n -= IDR_BITS;
  393. *paa++ = p;
  394. p = p->ary[(id >> n) & IDR_MASK];
  395. }
  396. bt_mask = id;
  397. id += 1 << n;
  398. /* Get the highest bit that the above add changed from 0->1. */
  399. while (n < fls(id ^ bt_mask)) {
  400. if (p)
  401. free_layer(p);
  402. n += IDR_BITS;
  403. p = *--paa;
  404. }
  405. }
  406. idp->layers = 0;
  407. }
  408. EXPORT_SYMBOL(__idr_remove_all);
  409. /**
  410. * idr_destroy - release all cached layers within an idr tree
  411. * @idp: idr handle
  412. *
  413. * Free all id mappings and all idp_layers. After this function, @idp is
  414. * completely unused and can be freed / recycled. The caller is
  415. * responsible for ensuring that no one else accesses @idp during or after
  416. * idr_destroy().
  417. *
  418. * A typical clean-up sequence for objects stored in an idr tree will use
  419. * idr_for_each() to free all objects, if necessay, then idr_destroy() to
  420. * free up the id mappings and cached idr_layers.
  421. */
  422. void idr_destroy(struct idr *idp)
  423. {
  424. __idr_remove_all(idp);
  425. while (idp->id_free_cnt) {
  426. struct idr_layer *p = get_from_free_list(idp);
  427. kmem_cache_free(idr_layer_cache, p);
  428. }
  429. }
  430. EXPORT_SYMBOL(idr_destroy);
  431. /**
  432. * idr_find - return pointer for given id
  433. * @idp: idr handle
  434. * @id: lookup key
  435. *
  436. * Return the pointer given the id it has been registered with. A %NULL
  437. * return indicates that @id is not valid or you passed %NULL in
  438. * idr_get_new().
  439. *
  440. * This function can be called under rcu_read_lock(), given that the leaf
  441. * pointers lifetimes are correctly managed.
  442. */
  443. void *idr_find(struct idr *idp, int id)
  444. {
  445. int n;
  446. struct idr_layer *p;
  447. p = rcu_dereference_raw(idp->top);
  448. if (!p)
  449. return NULL;
  450. n = (p->layer+1) * IDR_BITS;
  451. /* Mask off upper bits we don't use for the search. */
  452. id &= MAX_IDR_MASK;
  453. if (id >= (1 << n))
  454. return NULL;
  455. BUG_ON(n == 0);
  456. while (n > 0 && p) {
  457. n -= IDR_BITS;
  458. BUG_ON(n != p->layer*IDR_BITS);
  459. p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
  460. }
  461. return((void *)p);
  462. }
  463. EXPORT_SYMBOL(idr_find);
  464. /**
  465. * idr_for_each - iterate through all stored pointers
  466. * @idp: idr handle
  467. * @fn: function to be called for each pointer
  468. * @data: data passed back to callback function
  469. *
  470. * Iterate over the pointers registered with the given idr. The
  471. * callback function will be called for each pointer currently
  472. * registered, passing the id, the pointer and the data pointer passed
  473. * to this function. It is not safe to modify the idr tree while in
  474. * the callback, so functions such as idr_get_new and idr_remove are
  475. * not allowed.
  476. *
  477. * We check the return of @fn each time. If it returns anything other
  478. * than %0, we break out and return that value.
  479. *
  480. * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
  481. */
  482. int idr_for_each(struct idr *idp,
  483. int (*fn)(int id, void *p, void *data), void *data)
  484. {
  485. int n, id, max, error = 0;
  486. struct idr_layer *p;
  487. struct idr_layer *pa[MAX_IDR_LEVEL];
  488. struct idr_layer **paa = &pa[0];
  489. n = idp->layers * IDR_BITS;
  490. p = rcu_dereference_raw(idp->top);
  491. max = 1 << n;
  492. id = 0;
  493. while (id < max) {
  494. while (n > 0 && p) {
  495. n -= IDR_BITS;
  496. *paa++ = p;
  497. p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
  498. }
  499. if (p) {
  500. error = fn(id, (void *)p, data);
  501. if (error)
  502. break;
  503. }
  504. id += 1 << n;
  505. while (n < fls(id)) {
  506. n += IDR_BITS;
  507. p = *--paa;
  508. }
  509. }
  510. return error;
  511. }
  512. EXPORT_SYMBOL(idr_for_each);
  513. /**
  514. * idr_get_next - lookup next object of id to given id.
  515. * @idp: idr handle
  516. * @nextidp: pointer to lookup key
  517. *
  518. * Returns pointer to registered object with id, which is next number to
  519. * given id. After being looked up, *@nextidp will be updated for the next
  520. * iteration.
  521. *
  522. * This function can be called under rcu_read_lock(), given that the leaf
  523. * pointers lifetimes are correctly managed.
  524. */
  525. void *idr_get_next(struct idr *idp, int *nextidp)
  526. {
  527. struct idr_layer *p, *pa[MAX_IDR_LEVEL];
  528. struct idr_layer **paa = &pa[0];
  529. int id = *nextidp;
  530. int n, max;
  531. /* find first ent */
  532. p = rcu_dereference_raw(idp->top);
  533. if (!p)
  534. return NULL;
  535. n = (p->layer + 1) * IDR_BITS;
  536. max = 1 << n;
  537. while (id < max) {
  538. while (n > 0 && p) {
  539. n -= IDR_BITS;
  540. *paa++ = p;
  541. p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
  542. }
  543. if (p) {
  544. *nextidp = id;
  545. return p;
  546. }
  547. /*
  548. * Proceed to the next layer at the current level. Unlike
  549. * idr_for_each(), @id isn't guaranteed to be aligned to
  550. * layer boundary at this point and adding 1 << n may
  551. * incorrectly skip IDs. Make sure we jump to the
  552. * beginning of the next layer using round_up().
  553. */
  554. id = round_up(id + 1, 1 << n);
  555. while (n < fls(id)) {
  556. n += IDR_BITS;
  557. p = *--paa;
  558. }
  559. }
  560. return NULL;
  561. }
  562. EXPORT_SYMBOL(idr_get_next);
  563. /**
  564. * idr_replace - replace pointer for given id
  565. * @idp: idr handle
  566. * @ptr: pointer you want associated with the id
  567. * @id: lookup key
  568. *
  569. * Replace the pointer registered with an id and return the old value.
  570. * A %-ENOENT return indicates that @id was not found.
  571. * A %-EINVAL return indicates that @id was not within valid constraints.
  572. *
  573. * The caller must serialize with writers.
  574. */
  575. void *idr_replace(struct idr *idp, void *ptr, int id)
  576. {
  577. int n;
  578. struct idr_layer *p, *old_p;
  579. p = idp->top;
  580. if (!p)
  581. return ERR_PTR(-EINVAL);
  582. n = (p->layer+1) * IDR_BITS;
  583. id &= MAX_IDR_MASK;
  584. if (id >= (1 << n))
  585. return ERR_PTR(-EINVAL);
  586. n -= IDR_BITS;
  587. while ((n > 0) && p) {
  588. p = p->ary[(id >> n) & IDR_MASK];
  589. n -= IDR_BITS;
  590. }
  591. n = id & IDR_MASK;
  592. if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
  593. return ERR_PTR(-ENOENT);
  594. old_p = p->ary[n];
  595. rcu_assign_pointer(p->ary[n], ptr);
  596. return old_p;
  597. }
  598. EXPORT_SYMBOL(idr_replace);
  599. void __init idr_init_cache(void)
  600. {
  601. idr_layer_cache = kmem_cache_create("idr_layer_cache",
  602. sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
  603. }
  604. /**
  605. * idr_init - initialize idr handle
  606. * @idp: idr handle
  607. *
  608. * This function is use to set up the handle (@idp) that you will pass
  609. * to the rest of the functions.
  610. */
  611. void idr_init(struct idr *idp)
  612. {
  613. memset(idp, 0, sizeof(struct idr));
  614. spin_lock_init(&idp->lock);
  615. }
  616. EXPORT_SYMBOL(idr_init);
  617. /**
  618. * DOC: IDA description
  619. * IDA - IDR based ID allocator
  620. *
  621. * This is id allocator without id -> pointer translation. Memory
  622. * usage is much lower than full blown idr because each id only
  623. * occupies a bit. ida uses a custom leaf node which contains
  624. * IDA_BITMAP_BITS slots.
  625. *
  626. * 2007-04-25 written by Tejun Heo <htejun@gmail.com>
  627. */
  628. static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
  629. {
  630. unsigned long flags;
  631. if (!ida->free_bitmap) {
  632. spin_lock_irqsave(&ida->idr.lock, flags);
  633. if (!ida->free_bitmap) {
  634. ida->free_bitmap = bitmap;
  635. bitmap = NULL;
  636. }
  637. spin_unlock_irqrestore(&ida->idr.lock, flags);
  638. }
  639. kfree(bitmap);
  640. }
  641. /**
  642. * ida_pre_get - reserve resources for ida allocation
  643. * @ida: ida handle
  644. * @gfp_mask: memory allocation flag
  645. *
  646. * This function should be called prior to locking and calling the
  647. * following function. It preallocates enough memory to satisfy the
  648. * worst possible allocation.
  649. *
  650. * If the system is REALLY out of memory this function returns %0,
  651. * otherwise %1.
  652. */
  653. int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
  654. {
  655. /* allocate idr_layers */
  656. if (!idr_pre_get(&ida->idr, gfp_mask))
  657. return 0;
  658. /* allocate free_bitmap */
  659. if (!ida->free_bitmap) {
  660. struct ida_bitmap *bitmap;
  661. bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
  662. if (!bitmap)
  663. return 0;
  664. free_bitmap(ida, bitmap);
  665. }
  666. return 1;
  667. }
  668. EXPORT_SYMBOL(ida_pre_get);
  669. /**
  670. * ida_get_new_above - allocate new ID above or equal to a start id
  671. * @ida: ida handle
  672. * @starting_id: id to start search at
  673. * @p_id: pointer to the allocated handle
  674. *
  675. * Allocate new ID above or equal to @starting_id. It should be called
  676. * with any required locks.
  677. *
  678. * If memory is required, it will return %-EAGAIN, you should unlock
  679. * and go back to the ida_pre_get() call. If the ida is full, it will
  680. * return %-ENOSPC.
  681. *
  682. * @p_id returns a value in the range @starting_id ... %0x7fffffff.
  683. */
  684. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
  685. {
  686. struct idr_layer *pa[MAX_IDR_LEVEL];
  687. struct ida_bitmap *bitmap;
  688. unsigned long flags;
  689. int idr_id = starting_id / IDA_BITMAP_BITS;
  690. int offset = starting_id % IDA_BITMAP_BITS;
  691. int t, id;
  692. restart:
  693. /* get vacant slot */
  694. t = idr_get_empty_slot(&ida->idr, idr_id, pa);
  695. if (t < 0)
  696. return t == -ENOMEM ? -EAGAIN : t;
  697. if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT)
  698. return -ENOSPC;
  699. if (t != idr_id)
  700. offset = 0;
  701. idr_id = t;
  702. /* if bitmap isn't there, create a new one */
  703. bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
  704. if (!bitmap) {
  705. spin_lock_irqsave(&ida->idr.lock, flags);
  706. bitmap = ida->free_bitmap;
  707. ida->free_bitmap = NULL;
  708. spin_unlock_irqrestore(&ida->idr.lock, flags);
  709. if (!bitmap)
  710. return -EAGAIN;
  711. memset(bitmap, 0, sizeof(struct ida_bitmap));
  712. rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
  713. (void *)bitmap);
  714. pa[0]->count++;
  715. }
  716. /* lookup for empty slot */
  717. t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
  718. if (t == IDA_BITMAP_BITS) {
  719. /* no empty slot after offset, continue to the next chunk */
  720. idr_id++;
  721. offset = 0;
  722. goto restart;
  723. }
  724. id = idr_id * IDA_BITMAP_BITS + t;
  725. if (id >= MAX_IDR_BIT)
  726. return -ENOSPC;
  727. __set_bit(t, bitmap->bitmap);
  728. if (++bitmap->nr_busy == IDA_BITMAP_BITS)
  729. idr_mark_full(pa, idr_id);
  730. *p_id = id;
  731. /* Each leaf node can handle nearly a thousand slots and the
  732. * whole idea of ida is to have small memory foot print.
  733. * Throw away extra resources one by one after each successful
  734. * allocation.
  735. */
  736. if (ida->idr.id_free_cnt || ida->free_bitmap) {
  737. struct idr_layer *p = get_from_free_list(&ida->idr);
  738. if (p)
  739. kmem_cache_free(idr_layer_cache, p);
  740. }
  741. return 0;
  742. }
  743. EXPORT_SYMBOL(ida_get_new_above);
  744. /**
  745. * ida_remove - remove the given ID
  746. * @ida: ida handle
  747. * @id: ID to free
  748. */
  749. void ida_remove(struct ida *ida, int id)
  750. {
  751. struct idr_layer *p = ida->idr.top;
  752. int shift = (ida->idr.layers - 1) * IDR_BITS;
  753. int idr_id = id / IDA_BITMAP_BITS;
  754. int offset = id % IDA_BITMAP_BITS;
  755. int n;
  756. struct ida_bitmap *bitmap;
  757. /* clear full bits while looking up the leaf idr_layer */
  758. while ((shift > 0) && p) {
  759. n = (idr_id >> shift) & IDR_MASK;
  760. __clear_bit(n, &p->bitmap);
  761. p = p->ary[n];
  762. shift -= IDR_BITS;
  763. }
  764. if (p == NULL)
  765. goto err;
  766. n = idr_id & IDR_MASK;
  767. __clear_bit(n, &p->bitmap);
  768. bitmap = (void *)p->ary[n];
  769. if (!test_bit(offset, bitmap->bitmap))
  770. goto err;
  771. /* update bitmap and remove it if empty */
  772. __clear_bit(offset, bitmap->bitmap);
  773. if (--bitmap->nr_busy == 0) {
  774. __set_bit(n, &p->bitmap); /* to please idr_remove() */
  775. idr_remove(&ida->idr, idr_id);
  776. free_bitmap(ida, bitmap);
  777. }
  778. return;
  779. err:
  780. printk(KERN_WARNING
  781. "ida_remove called for id=%d which is not allocated.\n", id);
  782. }
  783. EXPORT_SYMBOL(ida_remove);
  784. /**
  785. * ida_destroy - release all cached layers within an ida tree
  786. * @ida: ida handle
  787. */
  788. void ida_destroy(struct ida *ida)
  789. {
  790. idr_destroy(&ida->idr);
  791. kfree(ida->free_bitmap);
  792. }
  793. EXPORT_SYMBOL(ida_destroy);
  794. /**
  795. * ida_simple_get - get a new id.
  796. * @ida: the (initialized) ida.
  797. * @start: the minimum id (inclusive, < 0x8000000)
  798. * @end: the maximum id (exclusive, < 0x8000000 or 0)
  799. * @gfp_mask: memory allocation flags
  800. *
  801. * Allocates an id in the range start <= id < end, or returns -ENOSPC.
  802. * On memory allocation failure, returns -ENOMEM.
  803. *
  804. * Use ida_simple_remove() to get rid of an id.
  805. */
  806. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  807. gfp_t gfp_mask)
  808. {
  809. int ret, id;
  810. unsigned int max;
  811. unsigned long flags;
  812. BUG_ON((int)start < 0);
  813. BUG_ON((int)end < 0);
  814. if (end == 0)
  815. max = 0x80000000;
  816. else {
  817. BUG_ON(end < start);
  818. max = end - 1;
  819. }
  820. again:
  821. if (!ida_pre_get(ida, gfp_mask))
  822. return -ENOMEM;
  823. spin_lock_irqsave(&simple_ida_lock, flags);
  824. ret = ida_get_new_above(ida, start, &id);
  825. if (!ret) {
  826. if (id > max) {
  827. ida_remove(ida, id);
  828. ret = -ENOSPC;
  829. } else {
  830. ret = id;
  831. }
  832. }
  833. spin_unlock_irqrestore(&simple_ida_lock, flags);
  834. if (unlikely(ret == -EAGAIN))
  835. goto again;
  836. return ret;
  837. }
  838. EXPORT_SYMBOL(ida_simple_get);
  839. /**
  840. * ida_simple_remove - remove an allocated id.
  841. * @ida: the (initialized) ida.
  842. * @id: the id returned by ida_simple_get.
  843. */
  844. void ida_simple_remove(struct ida *ida, unsigned int id)
  845. {
  846. unsigned long flags;
  847. BUG_ON((int)id < 0);
  848. spin_lock_irqsave(&simple_ida_lock, flags);
  849. ida_remove(ida, id);
  850. spin_unlock_irqrestore(&simple_ida_lock, flags);
  851. }
  852. EXPORT_SYMBOL(ida_simple_remove);
  853. /**
  854. * ida_init - initialize ida handle
  855. * @ida: ida handle
  856. *
  857. * This function is use to set up the handle (@ida) that you will pass
  858. * to the rest of the functions.
  859. */
  860. void ida_init(struct ida *ida)
  861. {
  862. memset(ida, 0, sizeof(struct ida));
  863. idr_init(&ida->idr);
  864. }
  865. EXPORT_SYMBOL(ida_init);