idr.c 20 KB

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