idr.c 24 KB

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