idr.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 IDR_FREE_MAX) 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/module.h>
  31. #endif
  32. #include <linux/err.h>
  33. #include <linux/string.h>
  34. #include <linux/idr.h>
  35. static struct kmem_cache *idr_layer_cache;
  36. static struct idr_layer *get_from_free_list(struct idr *idp)
  37. {
  38. struct idr_layer *p;
  39. unsigned long flags;
  40. spin_lock_irqsave(&idp->lock, flags);
  41. if ((p = idp->id_free)) {
  42. idp->id_free = p->ary[0];
  43. idp->id_free_cnt--;
  44. p->ary[0] = NULL;
  45. }
  46. spin_unlock_irqrestore(&idp->lock, flags);
  47. return(p);
  48. }
  49. /* only called when idp->lock is held */
  50. static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
  51. {
  52. p->ary[0] = idp->id_free;
  53. idp->id_free = p;
  54. idp->id_free_cnt++;
  55. }
  56. static void move_to_free_list(struct idr *idp, struct idr_layer *p)
  57. {
  58. unsigned long flags;
  59. /*
  60. * Depends on the return element being zeroed.
  61. */
  62. spin_lock_irqsave(&idp->lock, flags);
  63. __move_to_free_list(idp, p);
  64. spin_unlock_irqrestore(&idp->lock, flags);
  65. }
  66. static void idr_mark_full(struct idr_layer **pa, int id)
  67. {
  68. struct idr_layer *p = pa[0];
  69. int l = 0;
  70. __set_bit(id & IDR_MASK, &p->bitmap);
  71. /*
  72. * If this layer is full mark the bit in the layer above to
  73. * show that this part of the radix tree is full. This may
  74. * complete the layer above and require walking up the radix
  75. * tree.
  76. */
  77. while (p->bitmap == IDR_FULL) {
  78. if (!(p = pa[++l]))
  79. break;
  80. id = id >> IDR_BITS;
  81. __set_bit((id & IDR_MASK), &p->bitmap);
  82. }
  83. }
  84. /**
  85. * idr_pre_get - reserver resources for idr allocation
  86. * @idp: idr handle
  87. * @gfp_mask: memory allocation flags
  88. *
  89. * This function should be called prior to locking and calling the
  90. * idr_get_new* functions. It preallocates enough memory to satisfy
  91. * the worst possible allocation.
  92. *
  93. * If the system is REALLY out of memory this function returns 0,
  94. * otherwise 1.
  95. */
  96. int idr_pre_get(struct idr *idp, gfp_t gfp_mask)
  97. {
  98. while (idp->id_free_cnt < IDR_FREE_MAX) {
  99. struct idr_layer *new;
  100. new = kmem_cache_alloc(idr_layer_cache, gfp_mask);
  101. if (new == NULL)
  102. return (0);
  103. move_to_free_list(idp, new);
  104. }
  105. return 1;
  106. }
  107. EXPORT_SYMBOL(idr_pre_get);
  108. static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa)
  109. {
  110. int n, m, sh;
  111. struct idr_layer *p, *new;
  112. int l, id, oid;
  113. unsigned long bm;
  114. id = *starting_id;
  115. restart:
  116. p = idp->top;
  117. l = idp->layers;
  118. pa[l--] = NULL;
  119. while (1) {
  120. /*
  121. * We run around this while until we reach the leaf node...
  122. */
  123. n = (id >> (IDR_BITS*l)) & IDR_MASK;
  124. bm = ~p->bitmap;
  125. m = find_next_bit(&bm, IDR_SIZE, n);
  126. if (m == IDR_SIZE) {
  127. /* no space available go back to previous layer. */
  128. l++;
  129. oid = id;
  130. id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
  131. /* if already at the top layer, we need to grow */
  132. if (!(p = pa[l])) {
  133. *starting_id = id;
  134. return IDR_NEED_TO_GROW;
  135. }
  136. /* If we need to go up one layer, continue the
  137. * loop; otherwise, restart from the top.
  138. */
  139. sh = IDR_BITS * (l + 1);
  140. if (oid >> sh == id >> sh)
  141. continue;
  142. else
  143. goto restart;
  144. }
  145. if (m != n) {
  146. sh = IDR_BITS*l;
  147. id = ((id >> sh) ^ n ^ m) << sh;
  148. }
  149. if ((id >= MAX_ID_BIT) || (id < 0))
  150. return IDR_NOMORE_SPACE;
  151. if (l == 0)
  152. break;
  153. /*
  154. * Create the layer below if it is missing.
  155. */
  156. if (!p->ary[m]) {
  157. new = get_from_free_list(idp);
  158. if (!new)
  159. return -1;
  160. rcu_assign_pointer(p->ary[m], new);
  161. p->count++;
  162. }
  163. pa[l--] = p;
  164. p = p->ary[m];
  165. }
  166. pa[l] = p;
  167. return id;
  168. }
  169. static int idr_get_empty_slot(struct idr *idp, int starting_id,
  170. struct idr_layer **pa)
  171. {
  172. struct idr_layer *p, *new;
  173. int layers, v, id;
  174. unsigned long flags;
  175. id = starting_id;
  176. build_up:
  177. p = idp->top;
  178. layers = idp->layers;
  179. if (unlikely(!p)) {
  180. if (!(p = get_from_free_list(idp)))
  181. return -1;
  182. layers = 1;
  183. }
  184. /*
  185. * Add a new layer to the top of the tree if the requested
  186. * id is larger than the currently allocated space.
  187. */
  188. while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
  189. layers++;
  190. if (!p->count)
  191. continue;
  192. if (!(new = get_from_free_list(idp))) {
  193. /*
  194. * The allocation failed. If we built part of
  195. * the structure tear it down.
  196. */
  197. spin_lock_irqsave(&idp->lock, flags);
  198. for (new = p; p && p != idp->top; new = p) {
  199. p = p->ary[0];
  200. new->ary[0] = NULL;
  201. new->bitmap = new->count = 0;
  202. __move_to_free_list(idp, new);
  203. }
  204. spin_unlock_irqrestore(&idp->lock, flags);
  205. return -1;
  206. }
  207. new->ary[0] = p;
  208. new->count = 1;
  209. if (p->bitmap == IDR_FULL)
  210. __set_bit(0, &new->bitmap);
  211. p = new;
  212. }
  213. rcu_assign_pointer(idp->top, p);
  214. idp->layers = layers;
  215. v = sub_alloc(idp, &id, pa);
  216. if (v == IDR_NEED_TO_GROW)
  217. goto build_up;
  218. return(v);
  219. }
  220. static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id)
  221. {
  222. struct idr_layer *pa[MAX_LEVEL];
  223. int id;
  224. id = idr_get_empty_slot(idp, starting_id, pa);
  225. if (id >= 0) {
  226. /*
  227. * Successfully found an empty slot. Install the user
  228. * pointer and mark the slot full.
  229. */
  230. rcu_assign_pointer(pa[0]->ary[id & IDR_MASK],
  231. (struct idr_layer *)ptr);
  232. pa[0]->count++;
  233. idr_mark_full(pa, id);
  234. }
  235. return id;
  236. }
  237. /**
  238. * idr_get_new_above - allocate new idr entry above or equal to a start id
  239. * @idp: idr handle
  240. * @ptr: pointer you want associated with the ide
  241. * @start_id: id to start search at
  242. * @id: pointer to the allocated handle
  243. *
  244. * This is the allocate id function. It should be called with any
  245. * required locks.
  246. *
  247. * If memory is required, it will return -EAGAIN, you should unlock
  248. * and go back to the idr_pre_get() call. If the idr is full, it will
  249. * return -ENOSPC.
  250. *
  251. * @id returns a value in the range 0 ... 0x7fffffff
  252. */
  253. int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
  254. {
  255. int rv;
  256. rv = idr_get_new_above_int(idp, ptr, starting_id);
  257. /*
  258. * This is a cheap hack until the IDR code can be fixed to
  259. * return proper error values.
  260. */
  261. if (rv < 0)
  262. return _idr_rc_to_errno(rv);
  263. *id = rv;
  264. return 0;
  265. }
  266. EXPORT_SYMBOL(idr_get_new_above);
  267. /**
  268. * idr_get_new - allocate new idr entry
  269. * @idp: idr handle
  270. * @ptr: pointer you want associated with the ide
  271. * @id: pointer to the allocated handle
  272. *
  273. * This is the allocate id function. It should be called with any
  274. * required locks.
  275. *
  276. * If memory is required, it will return -EAGAIN, you should unlock
  277. * and go back to the idr_pre_get() call. If the idr is full, it will
  278. * return -ENOSPC.
  279. *
  280. * @id returns a value in the range 0 ... 0x7fffffff
  281. */
  282. int idr_get_new(struct idr *idp, void *ptr, int *id)
  283. {
  284. int rv;
  285. rv = idr_get_new_above_int(idp, ptr, 0);
  286. /*
  287. * This is a cheap hack until the IDR code can be fixed to
  288. * return proper error values.
  289. */
  290. if (rv < 0)
  291. return _idr_rc_to_errno(rv);
  292. *id = rv;
  293. return 0;
  294. }
  295. EXPORT_SYMBOL(idr_get_new);
  296. static void idr_remove_warning(int id)
  297. {
  298. printk(KERN_WARNING
  299. "idr_remove called for id=%d which is not allocated.\n", id);
  300. dump_stack();
  301. }
  302. static void sub_remove(struct idr *idp, int shift, int id)
  303. {
  304. struct idr_layer *p = idp->top;
  305. struct idr_layer **pa[MAX_LEVEL];
  306. struct idr_layer ***paa = &pa[0];
  307. int n;
  308. *paa = NULL;
  309. *++paa = &idp->top;
  310. while ((shift > 0) && p) {
  311. n = (id >> shift) & IDR_MASK;
  312. __clear_bit(n, &p->bitmap);
  313. *++paa = &p->ary[n];
  314. p = p->ary[n];
  315. shift -= IDR_BITS;
  316. }
  317. n = id & IDR_MASK;
  318. if (likely(p != NULL && test_bit(n, &p->bitmap))){
  319. __clear_bit(n, &p->bitmap);
  320. p->ary[n] = NULL;
  321. while(*paa && ! --((**paa)->count)){
  322. move_to_free_list(idp, **paa);
  323. **paa-- = NULL;
  324. }
  325. if (!*paa)
  326. idp->layers = 0;
  327. } else
  328. idr_remove_warning(id);
  329. }
  330. /**
  331. * idr_remove - remove the given id and free it's slot
  332. * @idp: idr handle
  333. * @id: unique key
  334. */
  335. void idr_remove(struct idr *idp, int id)
  336. {
  337. struct idr_layer *p;
  338. /* Mask off upper bits we don't use for the search. */
  339. id &= MAX_ID_MASK;
  340. sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
  341. if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
  342. idp->top->ary[0]) { // We can drop a layer
  343. p = idp->top->ary[0];
  344. idp->top->bitmap = idp->top->count = 0;
  345. move_to_free_list(idp, idp->top);
  346. idp->top = p;
  347. --idp->layers;
  348. }
  349. while (idp->id_free_cnt >= IDR_FREE_MAX) {
  350. p = get_from_free_list(idp);
  351. kmem_cache_free(idr_layer_cache, p);
  352. }
  353. return;
  354. }
  355. EXPORT_SYMBOL(idr_remove);
  356. /**
  357. * idr_remove_all - remove all ids from the given idr tree
  358. * @idp: idr handle
  359. *
  360. * idr_destroy() only frees up unused, cached idp_layers, but this
  361. * function will remove all id mappings and leave all idp_layers
  362. * unused.
  363. *
  364. * A typical clean-up sequence for objects stored in an idr tree, will
  365. * use idr_for_each() to free all objects, if necessay, then
  366. * idr_remove_all() to remove all ids, and idr_destroy() to free
  367. * up the cached idr_layers.
  368. */
  369. void idr_remove_all(struct idr *idp)
  370. {
  371. int n, id, max;
  372. struct idr_layer *p;
  373. struct idr_layer *pa[MAX_LEVEL];
  374. struct idr_layer **paa = &pa[0];
  375. n = idp->layers * IDR_BITS;
  376. p = idp->top;
  377. max = 1 << n;
  378. id = 0;
  379. while (id < max) {
  380. while (n > IDR_BITS && p) {
  381. n -= IDR_BITS;
  382. *paa++ = p;
  383. p = p->ary[(id >> n) & IDR_MASK];
  384. }
  385. id += 1 << n;
  386. while (n < fls(id)) {
  387. if (p) {
  388. memset(p, 0, sizeof *p);
  389. move_to_free_list(idp, p);
  390. }
  391. n += IDR_BITS;
  392. p = *--paa;
  393. }
  394. }
  395. idp->top = NULL;
  396. idp->layers = 0;
  397. }
  398. EXPORT_SYMBOL(idr_remove_all);
  399. /**
  400. * idr_destroy - release all cached layers within an idr tree
  401. * idp: idr handle
  402. */
  403. void idr_destroy(struct idr *idp)
  404. {
  405. while (idp->id_free_cnt) {
  406. struct idr_layer *p = get_from_free_list(idp);
  407. kmem_cache_free(idr_layer_cache, p);
  408. }
  409. }
  410. EXPORT_SYMBOL(idr_destroy);
  411. /**
  412. * idr_find - return pointer for given id
  413. * @idp: idr handle
  414. * @id: lookup key
  415. *
  416. * Return the pointer given the id it has been registered with. A %NULL
  417. * return indicates that @id is not valid or you passed %NULL in
  418. * idr_get_new().
  419. *
  420. * This function can be called under rcu_read_lock(), given that the leaf
  421. * pointers lifetimes are correctly managed.
  422. */
  423. void *idr_find(struct idr *idp, int id)
  424. {
  425. int n;
  426. struct idr_layer *p;
  427. n = idp->layers * IDR_BITS;
  428. p = rcu_dereference(idp->top);
  429. /* Mask off upper bits we don't use for the search. */
  430. id &= MAX_ID_MASK;
  431. if (id >= (1 << n))
  432. return NULL;
  433. while (n > 0 && p) {
  434. n -= IDR_BITS;
  435. p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]);
  436. }
  437. return((void *)p);
  438. }
  439. EXPORT_SYMBOL(idr_find);
  440. /**
  441. * idr_for_each - iterate through all stored pointers
  442. * @idp: idr handle
  443. * @fn: function to be called for each pointer
  444. * @data: data passed back to callback function
  445. *
  446. * Iterate over the pointers registered with the given idr. The
  447. * callback function will be called for each pointer currently
  448. * registered, passing the id, the pointer and the data pointer passed
  449. * to this function. It is not safe to modify the idr tree while in
  450. * the callback, so functions such as idr_get_new and idr_remove are
  451. * not allowed.
  452. *
  453. * We check the return of @fn each time. If it returns anything other
  454. * than 0, we break out and return that value.
  455. *
  456. * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
  457. */
  458. int idr_for_each(struct idr *idp,
  459. int (*fn)(int id, void *p, void *data), void *data)
  460. {
  461. int n, id, max, error = 0;
  462. struct idr_layer *p;
  463. struct idr_layer *pa[MAX_LEVEL];
  464. struct idr_layer **paa = &pa[0];
  465. n = idp->layers * IDR_BITS;
  466. p = rcu_dereference(idp->top);
  467. max = 1 << n;
  468. id = 0;
  469. while (id < max) {
  470. while (n > 0 && p) {
  471. n -= IDR_BITS;
  472. *paa++ = p;
  473. p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]);
  474. }
  475. if (p) {
  476. error = fn(id, (void *)p, data);
  477. if (error)
  478. break;
  479. }
  480. id += 1 << n;
  481. while (n < fls(id)) {
  482. n += IDR_BITS;
  483. p = *--paa;
  484. }
  485. }
  486. return error;
  487. }
  488. EXPORT_SYMBOL(idr_for_each);
  489. /**
  490. * idr_replace - replace pointer for given id
  491. * @idp: idr handle
  492. * @ptr: pointer you want associated with the id
  493. * @id: lookup key
  494. *
  495. * Replace the pointer registered with an id and return the old value.
  496. * A -ENOENT return indicates that @id was not found.
  497. * A -EINVAL return indicates that @id was not within valid constraints.
  498. *
  499. * The caller must serialize vs idr_find(), idr_get_new(), and idr_remove().
  500. */
  501. void *idr_replace(struct idr *idp, void *ptr, int id)
  502. {
  503. int n;
  504. struct idr_layer *p, *old_p;
  505. n = idp->layers * IDR_BITS;
  506. p = idp->top;
  507. id &= MAX_ID_MASK;
  508. if (id >= (1 << n))
  509. return ERR_PTR(-EINVAL);
  510. n -= IDR_BITS;
  511. while ((n > 0) && p) {
  512. p = p->ary[(id >> n) & IDR_MASK];
  513. n -= IDR_BITS;
  514. }
  515. n = id & IDR_MASK;
  516. if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
  517. return ERR_PTR(-ENOENT);
  518. old_p = p->ary[n];
  519. p->ary[n] = ptr;
  520. return old_p;
  521. }
  522. EXPORT_SYMBOL(idr_replace);
  523. static void idr_cache_ctor(struct kmem_cache *idr_layer_cache, void *idr_layer)
  524. {
  525. memset(idr_layer, 0, sizeof(struct idr_layer));
  526. }
  527. void __init idr_init_cache(void)
  528. {
  529. idr_layer_cache = kmem_cache_create("idr_layer_cache",
  530. sizeof(struct idr_layer), 0, SLAB_PANIC,
  531. idr_cache_ctor);
  532. }
  533. /**
  534. * idr_init - initialize idr handle
  535. * @idp: idr handle
  536. *
  537. * This function is use to set up the handle (@idp) that you will pass
  538. * to the rest of the functions.
  539. */
  540. void idr_init(struct idr *idp)
  541. {
  542. memset(idp, 0, sizeof(struct idr));
  543. spin_lock_init(&idp->lock);
  544. }
  545. EXPORT_SYMBOL(idr_init);
  546. /*
  547. * IDA - IDR based ID allocator
  548. *
  549. * this is id allocator without id -> pointer translation. Memory
  550. * usage is much lower than full blown idr because each id only
  551. * occupies a bit. ida uses a custom leaf node which contains
  552. * IDA_BITMAP_BITS slots.
  553. *
  554. * 2007-04-25 written by Tejun Heo <htejun@gmail.com>
  555. */
  556. static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
  557. {
  558. unsigned long flags;
  559. if (!ida->free_bitmap) {
  560. spin_lock_irqsave(&ida->idr.lock, flags);
  561. if (!ida->free_bitmap) {
  562. ida->free_bitmap = bitmap;
  563. bitmap = NULL;
  564. }
  565. spin_unlock_irqrestore(&ida->idr.lock, flags);
  566. }
  567. kfree(bitmap);
  568. }
  569. /**
  570. * ida_pre_get - reserve resources for ida allocation
  571. * @ida: ida handle
  572. * @gfp_mask: memory allocation flag
  573. *
  574. * This function should be called prior to locking and calling the
  575. * following function. It preallocates enough memory to satisfy the
  576. * worst possible allocation.
  577. *
  578. * If the system is REALLY out of memory this function returns 0,
  579. * otherwise 1.
  580. */
  581. int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
  582. {
  583. /* allocate idr_layers */
  584. if (!idr_pre_get(&ida->idr, gfp_mask))
  585. return 0;
  586. /* allocate free_bitmap */
  587. if (!ida->free_bitmap) {
  588. struct ida_bitmap *bitmap;
  589. bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
  590. if (!bitmap)
  591. return 0;
  592. free_bitmap(ida, bitmap);
  593. }
  594. return 1;
  595. }
  596. EXPORT_SYMBOL(ida_pre_get);
  597. /**
  598. * ida_get_new_above - allocate new ID above or equal to a start id
  599. * @ida: ida handle
  600. * @staring_id: id to start search at
  601. * @p_id: pointer to the allocated handle
  602. *
  603. * Allocate new ID above or equal to @ida. It should be called with
  604. * any required locks.
  605. *
  606. * If memory is required, it will return -EAGAIN, you should unlock
  607. * and go back to the ida_pre_get() call. If the ida is full, it will
  608. * return -ENOSPC.
  609. *
  610. * @p_id returns a value in the range 0 ... 0x7fffffff.
  611. */
  612. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
  613. {
  614. struct idr_layer *pa[MAX_LEVEL];
  615. struct ida_bitmap *bitmap;
  616. unsigned long flags;
  617. int idr_id = starting_id / IDA_BITMAP_BITS;
  618. int offset = starting_id % IDA_BITMAP_BITS;
  619. int t, id;
  620. restart:
  621. /* get vacant slot */
  622. t = idr_get_empty_slot(&ida->idr, idr_id, pa);
  623. if (t < 0)
  624. return _idr_rc_to_errno(t);
  625. if (t * IDA_BITMAP_BITS >= MAX_ID_BIT)
  626. return -ENOSPC;
  627. if (t != idr_id)
  628. offset = 0;
  629. idr_id = t;
  630. /* if bitmap isn't there, create a new one */
  631. bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
  632. if (!bitmap) {
  633. spin_lock_irqsave(&ida->idr.lock, flags);
  634. bitmap = ida->free_bitmap;
  635. ida->free_bitmap = NULL;
  636. spin_unlock_irqrestore(&ida->idr.lock, flags);
  637. if (!bitmap)
  638. return -EAGAIN;
  639. memset(bitmap, 0, sizeof(struct ida_bitmap));
  640. rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
  641. (void *)bitmap);
  642. pa[0]->count++;
  643. }
  644. /* lookup for empty slot */
  645. t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
  646. if (t == IDA_BITMAP_BITS) {
  647. /* no empty slot after offset, continue to the next chunk */
  648. idr_id++;
  649. offset = 0;
  650. goto restart;
  651. }
  652. id = idr_id * IDA_BITMAP_BITS + t;
  653. if (id >= MAX_ID_BIT)
  654. return -ENOSPC;
  655. __set_bit(t, bitmap->bitmap);
  656. if (++bitmap->nr_busy == IDA_BITMAP_BITS)
  657. idr_mark_full(pa, idr_id);
  658. *p_id = id;
  659. /* Each leaf node can handle nearly a thousand slots and the
  660. * whole idea of ida is to have small memory foot print.
  661. * Throw away extra resources one by one after each successful
  662. * allocation.
  663. */
  664. if (ida->idr.id_free_cnt || ida->free_bitmap) {
  665. struct idr_layer *p = get_from_free_list(&ida->idr);
  666. if (p)
  667. kmem_cache_free(idr_layer_cache, p);
  668. }
  669. return 0;
  670. }
  671. EXPORT_SYMBOL(ida_get_new_above);
  672. /**
  673. * ida_get_new - allocate new ID
  674. * @ida: idr handle
  675. * @p_id: pointer to the allocated handle
  676. *
  677. * Allocate new ID. It should be called with any required locks.
  678. *
  679. * If memory is required, it will return -EAGAIN, you should unlock
  680. * and go back to the idr_pre_get() call. If the idr is full, it will
  681. * return -ENOSPC.
  682. *
  683. * @id returns a value in the range 0 ... 0x7fffffff.
  684. */
  685. int ida_get_new(struct ida *ida, int *p_id)
  686. {
  687. return ida_get_new_above(ida, 0, p_id);
  688. }
  689. EXPORT_SYMBOL(ida_get_new);
  690. /**
  691. * ida_remove - remove the given ID
  692. * @ida: ida handle
  693. * @id: ID to free
  694. */
  695. void ida_remove(struct ida *ida, int id)
  696. {
  697. struct idr_layer *p = ida->idr.top;
  698. int shift = (ida->idr.layers - 1) * IDR_BITS;
  699. int idr_id = id / IDA_BITMAP_BITS;
  700. int offset = id % IDA_BITMAP_BITS;
  701. int n;
  702. struct ida_bitmap *bitmap;
  703. /* clear full bits while looking up the leaf idr_layer */
  704. while ((shift > 0) && p) {
  705. n = (idr_id >> shift) & IDR_MASK;
  706. __clear_bit(n, &p->bitmap);
  707. p = p->ary[n];
  708. shift -= IDR_BITS;
  709. }
  710. if (p == NULL)
  711. goto err;
  712. n = idr_id & IDR_MASK;
  713. __clear_bit(n, &p->bitmap);
  714. bitmap = (void *)p->ary[n];
  715. if (!test_bit(offset, bitmap->bitmap))
  716. goto err;
  717. /* update bitmap and remove it if empty */
  718. __clear_bit(offset, bitmap->bitmap);
  719. if (--bitmap->nr_busy == 0) {
  720. __set_bit(n, &p->bitmap); /* to please idr_remove() */
  721. idr_remove(&ida->idr, idr_id);
  722. free_bitmap(ida, bitmap);
  723. }
  724. return;
  725. err:
  726. printk(KERN_WARNING
  727. "ida_remove called for id=%d which is not allocated.\n", id);
  728. }
  729. EXPORT_SYMBOL(ida_remove);
  730. /**
  731. * ida_destroy - release all cached layers within an ida tree
  732. * ida: ida handle
  733. */
  734. void ida_destroy(struct ida *ida)
  735. {
  736. idr_destroy(&ida->idr);
  737. kfree(ida->free_bitmap);
  738. }
  739. EXPORT_SYMBOL(ida_destroy);
  740. /**
  741. * ida_init - initialize ida handle
  742. * @ida: ida handle
  743. *
  744. * This function is use to set up the handle (@ida) that you will pass
  745. * to the rest of the functions.
  746. */
  747. void ida_init(struct ida *ida)
  748. {
  749. memset(ida, 0, sizeof(struct ida));
  750. idr_init(&ida->idr);
  751. }
  752. EXPORT_SYMBOL(ida_init);