idr.c 22 KB

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