idr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. #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 < IDR_FREE_MAX) {
  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_ID_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_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_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. /**
  294. * idr_get_new - allocate new idr entry
  295. * @idp: idr handle
  296. * @ptr: pointer you want associated with the id
  297. * @id: pointer to the allocated handle
  298. *
  299. * If allocation from IDR's private freelist fails, idr_get_new_above() will
  300. * return %-EAGAIN. The caller should retry the idr_pre_get() call to refill
  301. * IDR's preallocation and then retry the idr_get_new_above() call.
  302. *
  303. * If the idr is full idr_get_new_above() will return %-ENOSPC.
  304. *
  305. * @id returns a value in the range %0 ... %0x7fffffff
  306. */
  307. int idr_get_new(struct idr *idp, void *ptr, int *id)
  308. {
  309. int rv;
  310. rv = idr_get_new_above_int(idp, ptr, 0);
  311. /*
  312. * This is a cheap hack until the IDR code can be fixed to
  313. * return proper error values.
  314. */
  315. if (rv < 0)
  316. return _idr_rc_to_errno(rv);
  317. *id = rv;
  318. return 0;
  319. }
  320. EXPORT_SYMBOL(idr_get_new);
  321. static void idr_remove_warning(int id)
  322. {
  323. printk(KERN_WARNING
  324. "idr_remove called for id=%d which is not allocated.\n", id);
  325. dump_stack();
  326. }
  327. static void sub_remove(struct idr *idp, int shift, int id)
  328. {
  329. struct idr_layer *p = idp->top;
  330. struct idr_layer **pa[MAX_LEVEL];
  331. struct idr_layer ***paa = &pa[0];
  332. struct idr_layer *to_free;
  333. int n;
  334. *paa = NULL;
  335. *++paa = &idp->top;
  336. while ((shift > 0) && p) {
  337. n = (id >> shift) & IDR_MASK;
  338. __clear_bit(n, &p->bitmap);
  339. *++paa = &p->ary[n];
  340. p = p->ary[n];
  341. shift -= IDR_BITS;
  342. }
  343. n = id & IDR_MASK;
  344. if (likely(p != NULL && test_bit(n, &p->bitmap))){
  345. __clear_bit(n, &p->bitmap);
  346. rcu_assign_pointer(p->ary[n], NULL);
  347. to_free = NULL;
  348. while(*paa && ! --((**paa)->count)){
  349. if (to_free)
  350. free_layer(to_free);
  351. to_free = **paa;
  352. **paa-- = NULL;
  353. }
  354. if (!*paa)
  355. idp->layers = 0;
  356. if (to_free)
  357. free_layer(to_free);
  358. } else
  359. idr_remove_warning(id);
  360. }
  361. /**
  362. * idr_remove - remove the given id and free its slot
  363. * @idp: idr handle
  364. * @id: unique key
  365. */
  366. void idr_remove(struct idr *idp, int id)
  367. {
  368. struct idr_layer *p;
  369. struct idr_layer *to_free;
  370. /* Mask off upper bits we don't use for the search. */
  371. id &= MAX_ID_MASK;
  372. sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
  373. if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
  374. idp->top->ary[0]) {
  375. /*
  376. * Single child at leftmost slot: we can shrink the tree.
  377. * This level is not needed anymore since when layers are
  378. * inserted, they are inserted at the top of the existing
  379. * tree.
  380. */
  381. to_free = idp->top;
  382. p = idp->top->ary[0];
  383. rcu_assign_pointer(idp->top, p);
  384. --idp->layers;
  385. to_free->bitmap = to_free->count = 0;
  386. free_layer(to_free);
  387. }
  388. while (idp->id_free_cnt >= IDR_FREE_MAX) {
  389. p = get_from_free_list(idp);
  390. /*
  391. * Note: we don't call the rcu callback here, since the only
  392. * layers that fall into the freelist are those that have been
  393. * preallocated.
  394. */
  395. kmem_cache_free(idr_layer_cache, p);
  396. }
  397. return;
  398. }
  399. EXPORT_SYMBOL(idr_remove);
  400. /**
  401. * idr_remove_all - remove all ids from the given idr tree
  402. * @idp: idr handle
  403. *
  404. * idr_destroy() only frees up unused, cached idp_layers, but this
  405. * function will remove all id mappings and leave all idp_layers
  406. * unused.
  407. *
  408. * A typical clean-up sequence for objects stored in an idr tree will
  409. * use idr_for_each() to free all objects, if necessay, then
  410. * idr_remove_all() to remove all ids, and idr_destroy() to free
  411. * up the cached idr_layers.
  412. */
  413. void idr_remove_all(struct idr *idp)
  414. {
  415. int n, id, max;
  416. int bt_mask;
  417. struct idr_layer *p;
  418. struct idr_layer *pa[MAX_LEVEL];
  419. struct idr_layer **paa = &pa[0];
  420. n = idp->layers * IDR_BITS;
  421. p = idp->top;
  422. rcu_assign_pointer(idp->top, NULL);
  423. max = 1 << n;
  424. id = 0;
  425. while (id < max) {
  426. while (n > IDR_BITS && p) {
  427. n -= IDR_BITS;
  428. *paa++ = p;
  429. p = p->ary[(id >> n) & IDR_MASK];
  430. }
  431. bt_mask = id;
  432. id += 1 << n;
  433. /* Get the highest bit that the above add changed from 0->1. */
  434. while (n < fls(id ^ bt_mask)) {
  435. if (p)
  436. free_layer(p);
  437. n += IDR_BITS;
  438. p = *--paa;
  439. }
  440. }
  441. idp->layers = 0;
  442. }
  443. EXPORT_SYMBOL(idr_remove_all);
  444. /**
  445. * idr_destroy - release all cached layers within an idr tree
  446. * @idp: idr handle
  447. */
  448. void idr_destroy(struct idr *idp)
  449. {
  450. while (idp->id_free_cnt) {
  451. struct idr_layer *p = get_from_free_list(idp);
  452. kmem_cache_free(idr_layer_cache, p);
  453. }
  454. }
  455. EXPORT_SYMBOL(idr_destroy);
  456. /**
  457. * idr_find - return pointer for given id
  458. * @idp: idr handle
  459. * @id: lookup key
  460. *
  461. * Return the pointer given the id it has been registered with. A %NULL
  462. * return indicates that @id is not valid or you passed %NULL in
  463. * idr_get_new().
  464. *
  465. * This function can be called under rcu_read_lock(), given that the leaf
  466. * pointers lifetimes are correctly managed.
  467. */
  468. void *idr_find(struct idr *idp, int id)
  469. {
  470. int n;
  471. struct idr_layer *p;
  472. p = rcu_dereference_raw(idp->top);
  473. if (!p)
  474. return NULL;
  475. n = (p->layer+1) * IDR_BITS;
  476. /* Mask off upper bits we don't use for the search. */
  477. id &= MAX_ID_MASK;
  478. if (id >= (1 << n))
  479. return NULL;
  480. BUG_ON(n == 0);
  481. while (n > 0 && p) {
  482. n -= IDR_BITS;
  483. BUG_ON(n != p->layer*IDR_BITS);
  484. p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
  485. }
  486. return((void *)p);
  487. }
  488. EXPORT_SYMBOL(idr_find);
  489. /**
  490. * idr_for_each - iterate through all stored pointers
  491. * @idp: idr handle
  492. * @fn: function to be called for each pointer
  493. * @data: data passed back to callback function
  494. *
  495. * Iterate over the pointers registered with the given idr. The
  496. * callback function will be called for each pointer currently
  497. * registered, passing the id, the pointer and the data pointer passed
  498. * to this function. It is not safe to modify the idr tree while in
  499. * the callback, so functions such as idr_get_new and idr_remove are
  500. * not allowed.
  501. *
  502. * We check the return of @fn each time. If it returns anything other
  503. * than %0, we break out and return that value.
  504. *
  505. * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
  506. */
  507. int idr_for_each(struct idr *idp,
  508. int (*fn)(int id, void *p, void *data), void *data)
  509. {
  510. int n, id, max, error = 0;
  511. struct idr_layer *p;
  512. struct idr_layer *pa[MAX_LEVEL];
  513. struct idr_layer **paa = &pa[0];
  514. n = idp->layers * IDR_BITS;
  515. p = rcu_dereference_raw(idp->top);
  516. max = 1 << n;
  517. id = 0;
  518. while (id < max) {
  519. while (n > 0 && p) {
  520. n -= IDR_BITS;
  521. *paa++ = p;
  522. p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
  523. }
  524. if (p) {
  525. error = fn(id, (void *)p, data);
  526. if (error)
  527. break;
  528. }
  529. id += 1 << n;
  530. while (n < fls(id)) {
  531. n += IDR_BITS;
  532. p = *--paa;
  533. }
  534. }
  535. return error;
  536. }
  537. EXPORT_SYMBOL(idr_for_each);
  538. /**
  539. * idr_get_next - lookup next object of id to given id.
  540. * @idp: idr handle
  541. * @nextidp: pointer to lookup key
  542. *
  543. * Returns pointer to registered object with id, which is next number to
  544. * given id. After being looked up, *@nextidp will be updated for the next
  545. * iteration.
  546. */
  547. void *idr_get_next(struct idr *idp, int *nextidp)
  548. {
  549. struct idr_layer *p, *pa[MAX_LEVEL];
  550. struct idr_layer **paa = &pa[0];
  551. int id = *nextidp;
  552. int n, max;
  553. /* find first ent */
  554. n = idp->layers * IDR_BITS;
  555. max = 1 << n;
  556. p = rcu_dereference_raw(idp->top);
  557. if (!p)
  558. return NULL;
  559. while (id < max) {
  560. while (n > 0 && p) {
  561. n -= IDR_BITS;
  562. *paa++ = p;
  563. p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
  564. }
  565. if (p) {
  566. *nextidp = id;
  567. return p;
  568. }
  569. id += 1 << n;
  570. while (n < fls(id)) {
  571. n += IDR_BITS;
  572. p = *--paa;
  573. }
  574. }
  575. return NULL;
  576. }
  577. EXPORT_SYMBOL(idr_get_next);
  578. /**
  579. * idr_replace - replace pointer for given id
  580. * @idp: idr handle
  581. * @ptr: pointer you want associated with the id
  582. * @id: lookup key
  583. *
  584. * Replace the pointer registered with an id and return the old value.
  585. * A %-ENOENT return indicates that @id was not found.
  586. * A %-EINVAL return indicates that @id was not within valid constraints.
  587. *
  588. * The caller must serialize with writers.
  589. */
  590. void *idr_replace(struct idr *idp, void *ptr, int id)
  591. {
  592. int n;
  593. struct idr_layer *p, *old_p;
  594. p = idp->top;
  595. if (!p)
  596. return ERR_PTR(-EINVAL);
  597. n = (p->layer+1) * IDR_BITS;
  598. id &= MAX_ID_MASK;
  599. if (id >= (1 << n))
  600. return ERR_PTR(-EINVAL);
  601. n -= IDR_BITS;
  602. while ((n > 0) && p) {
  603. p = p->ary[(id >> n) & IDR_MASK];
  604. n -= IDR_BITS;
  605. }
  606. n = id & IDR_MASK;
  607. if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
  608. return ERR_PTR(-ENOENT);
  609. old_p = p->ary[n];
  610. rcu_assign_pointer(p->ary[n], ptr);
  611. return old_p;
  612. }
  613. EXPORT_SYMBOL(idr_replace);
  614. void __init idr_init_cache(void)
  615. {
  616. idr_layer_cache = kmem_cache_create("idr_layer_cache",
  617. sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
  618. }
  619. /**
  620. * idr_init - initialize idr handle
  621. * @idp: idr handle
  622. *
  623. * This function is use to set up the handle (@idp) that you will pass
  624. * to the rest of the functions.
  625. */
  626. void idr_init(struct idr *idp)
  627. {
  628. memset(idp, 0, sizeof(struct idr));
  629. spin_lock_init(&idp->lock);
  630. }
  631. EXPORT_SYMBOL(idr_init);
  632. /**
  633. * DOC: IDA description
  634. * IDA - IDR based ID allocator
  635. *
  636. * This is id allocator without id -> pointer translation. Memory
  637. * usage is much lower than full blown idr because each id only
  638. * occupies a bit. ida uses a custom leaf node which contains
  639. * IDA_BITMAP_BITS slots.
  640. *
  641. * 2007-04-25 written by Tejun Heo <htejun@gmail.com>
  642. */
  643. static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
  644. {
  645. unsigned long flags;
  646. if (!ida->free_bitmap) {
  647. spin_lock_irqsave(&ida->idr.lock, flags);
  648. if (!ida->free_bitmap) {
  649. ida->free_bitmap = bitmap;
  650. bitmap = NULL;
  651. }
  652. spin_unlock_irqrestore(&ida->idr.lock, flags);
  653. }
  654. kfree(bitmap);
  655. }
  656. /**
  657. * ida_pre_get - reserve resources for ida allocation
  658. * @ida: ida handle
  659. * @gfp_mask: memory allocation flag
  660. *
  661. * This function should be called prior to locking and calling the
  662. * following function. It preallocates enough memory to satisfy the
  663. * worst possible allocation.
  664. *
  665. * If the system is REALLY out of memory this function returns %0,
  666. * otherwise %1.
  667. */
  668. int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
  669. {
  670. /* allocate idr_layers */
  671. if (!idr_pre_get(&ida->idr, gfp_mask))
  672. return 0;
  673. /* allocate free_bitmap */
  674. if (!ida->free_bitmap) {
  675. struct ida_bitmap *bitmap;
  676. bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
  677. if (!bitmap)
  678. return 0;
  679. free_bitmap(ida, bitmap);
  680. }
  681. return 1;
  682. }
  683. EXPORT_SYMBOL(ida_pre_get);
  684. /**
  685. * ida_get_new_above - allocate new ID above or equal to a start id
  686. * @ida: ida handle
  687. * @starting_id: id to start search at
  688. * @p_id: pointer to the allocated handle
  689. *
  690. * Allocate new ID above or equal to @ida. It should be called with
  691. * any required locks.
  692. *
  693. * If memory is required, it will return %-EAGAIN, you should unlock
  694. * and go back to the ida_pre_get() call. If the ida is full, it will
  695. * return %-ENOSPC.
  696. *
  697. * @p_id returns a value in the range @starting_id ... %0x7fffffff.
  698. */
  699. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
  700. {
  701. struct idr_layer *pa[MAX_LEVEL];
  702. struct ida_bitmap *bitmap;
  703. unsigned long flags;
  704. int idr_id = starting_id / IDA_BITMAP_BITS;
  705. int offset = starting_id % IDA_BITMAP_BITS;
  706. int t, id;
  707. restart:
  708. /* get vacant slot */
  709. t = idr_get_empty_slot(&ida->idr, idr_id, pa);
  710. if (t < 0)
  711. return _idr_rc_to_errno(t);
  712. if (t * IDA_BITMAP_BITS >= MAX_ID_BIT)
  713. return -ENOSPC;
  714. if (t != idr_id)
  715. offset = 0;
  716. idr_id = t;
  717. /* if bitmap isn't there, create a new one */
  718. bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
  719. if (!bitmap) {
  720. spin_lock_irqsave(&ida->idr.lock, flags);
  721. bitmap = ida->free_bitmap;
  722. ida->free_bitmap = NULL;
  723. spin_unlock_irqrestore(&ida->idr.lock, flags);
  724. if (!bitmap)
  725. return -EAGAIN;
  726. memset(bitmap, 0, sizeof(struct ida_bitmap));
  727. rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
  728. (void *)bitmap);
  729. pa[0]->count++;
  730. }
  731. /* lookup for empty slot */
  732. t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
  733. if (t == IDA_BITMAP_BITS) {
  734. /* no empty slot after offset, continue to the next chunk */
  735. idr_id++;
  736. offset = 0;
  737. goto restart;
  738. }
  739. id = idr_id * IDA_BITMAP_BITS + t;
  740. if (id >= MAX_ID_BIT)
  741. return -ENOSPC;
  742. __set_bit(t, bitmap->bitmap);
  743. if (++bitmap->nr_busy == IDA_BITMAP_BITS)
  744. idr_mark_full(pa, idr_id);
  745. *p_id = id;
  746. /* Each leaf node can handle nearly a thousand slots and the
  747. * whole idea of ida is to have small memory foot print.
  748. * Throw away extra resources one by one after each successful
  749. * allocation.
  750. */
  751. if (ida->idr.id_free_cnt || ida->free_bitmap) {
  752. struct idr_layer *p = get_from_free_list(&ida->idr);
  753. if (p)
  754. kmem_cache_free(idr_layer_cache, p);
  755. }
  756. return 0;
  757. }
  758. EXPORT_SYMBOL(ida_get_new_above);
  759. /**
  760. * ida_get_new - allocate new ID
  761. * @ida: idr handle
  762. * @p_id: pointer to the allocated handle
  763. *
  764. * Allocate new ID. It should be called with any required locks.
  765. *
  766. * If memory is required, it will return %-EAGAIN, you should unlock
  767. * and go back to the idr_pre_get() call. If the idr is full, it will
  768. * return %-ENOSPC.
  769. *
  770. * @id returns a value in the range %0 ... %0x7fffffff.
  771. */
  772. int ida_get_new(struct ida *ida, int *p_id)
  773. {
  774. return ida_get_new_above(ida, 0, p_id);
  775. }
  776. EXPORT_SYMBOL(ida_get_new);
  777. /**
  778. * ida_remove - remove the given ID
  779. * @ida: ida handle
  780. * @id: ID to free
  781. */
  782. void ida_remove(struct ida *ida, int id)
  783. {
  784. struct idr_layer *p = ida->idr.top;
  785. int shift = (ida->idr.layers - 1) * IDR_BITS;
  786. int idr_id = id / IDA_BITMAP_BITS;
  787. int offset = id % IDA_BITMAP_BITS;
  788. int n;
  789. struct ida_bitmap *bitmap;
  790. /* clear full bits while looking up the leaf idr_layer */
  791. while ((shift > 0) && p) {
  792. n = (idr_id >> shift) & IDR_MASK;
  793. __clear_bit(n, &p->bitmap);
  794. p = p->ary[n];
  795. shift -= IDR_BITS;
  796. }
  797. if (p == NULL)
  798. goto err;
  799. n = idr_id & IDR_MASK;
  800. __clear_bit(n, &p->bitmap);
  801. bitmap = (void *)p->ary[n];
  802. if (!test_bit(offset, bitmap->bitmap))
  803. goto err;
  804. /* update bitmap and remove it if empty */
  805. __clear_bit(offset, bitmap->bitmap);
  806. if (--bitmap->nr_busy == 0) {
  807. __set_bit(n, &p->bitmap); /* to please idr_remove() */
  808. idr_remove(&ida->idr, idr_id);
  809. free_bitmap(ida, bitmap);
  810. }
  811. return;
  812. err:
  813. printk(KERN_WARNING
  814. "ida_remove called for id=%d which is not allocated.\n", id);
  815. }
  816. EXPORT_SYMBOL(ida_remove);
  817. /**
  818. * ida_destroy - release all cached layers within an ida tree
  819. * @ida: ida handle
  820. */
  821. void ida_destroy(struct ida *ida)
  822. {
  823. idr_destroy(&ida->idr);
  824. kfree(ida->free_bitmap);
  825. }
  826. EXPORT_SYMBOL(ida_destroy);
  827. /**
  828. * ida_simple_get - get a new id.
  829. * @ida: the (initialized) ida.
  830. * @start: the minimum id (inclusive, < 0x8000000)
  831. * @end: the maximum id (exclusive, < 0x8000000 or 0)
  832. * @gfp_mask: memory allocation flags
  833. *
  834. * Allocates an id in the range start <= id < end, or returns -ENOSPC.
  835. * On memory allocation failure, returns -ENOMEM.
  836. *
  837. * Use ida_simple_remove() to get rid of an id.
  838. */
  839. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  840. gfp_t gfp_mask)
  841. {
  842. int ret, id;
  843. unsigned int max;
  844. BUG_ON((int)start < 0);
  845. BUG_ON((int)end < 0);
  846. if (end == 0)
  847. max = 0x80000000;
  848. else {
  849. BUG_ON(end < start);
  850. max = end - 1;
  851. }
  852. again:
  853. if (!ida_pre_get(ida, gfp_mask))
  854. return -ENOMEM;
  855. spin_lock(&simple_ida_lock);
  856. ret = ida_get_new_above(ida, start, &id);
  857. if (!ret) {
  858. if (id > max) {
  859. ida_remove(ida, id);
  860. ret = -ENOSPC;
  861. } else {
  862. ret = id;
  863. }
  864. }
  865. spin_unlock(&simple_ida_lock);
  866. if (unlikely(ret == -EAGAIN))
  867. goto again;
  868. return ret;
  869. }
  870. EXPORT_SYMBOL(ida_simple_get);
  871. /**
  872. * ida_simple_remove - remove an allocated id.
  873. * @ida: the (initialized) ida.
  874. * @id: the id returned by ida_simple_get.
  875. */
  876. void ida_simple_remove(struct ida *ida, unsigned int id)
  877. {
  878. BUG_ON((int)id < 0);
  879. spin_lock(&simple_ida_lock);
  880. ida_remove(ida, id);
  881. spin_unlock(&simple_ida_lock);
  882. }
  883. EXPORT_SYMBOL(ida_simple_remove);
  884. /**
  885. * ida_init - initialize ida handle
  886. * @ida: ida handle
  887. *
  888. * This function is use to set up the handle (@ida) that you will pass
  889. * to the rest of the functions.
  890. */
  891. void ida_init(struct ida *ida)
  892. {
  893. memset(ida, 0, sizeof(struct ida));
  894. idr_init(&ida->idr);
  895. }
  896. EXPORT_SYMBOL(ida_init);