idr.c 21 KB

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