idr.c 20 KB

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