idr.c 21 KB

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