idr.c 24 KB

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