idr.c 23 KB

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