idr.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. 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. * This function can be called under rcu_read_lock(), given that the leaf
  548. * pointers lifetimes are correctly managed.
  549. */
  550. void *idr_get_next(struct idr *idp, int *nextidp)
  551. {
  552. struct idr_layer *p, *pa[MAX_LEVEL];
  553. struct idr_layer **paa = &pa[0];
  554. int id = *nextidp;
  555. int n, max;
  556. /* find first ent */
  557. p = rcu_dereference_raw(idp->top);
  558. if (!p)
  559. return NULL;
  560. n = (p->layer + 1) * IDR_BITS;
  561. max = 1 << n;
  562. while (id < max) {
  563. while (n > 0 && p) {
  564. n -= IDR_BITS;
  565. *paa++ = p;
  566. p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
  567. }
  568. if (p) {
  569. *nextidp = id;
  570. return p;
  571. }
  572. id += 1 << n;
  573. while (n < fls(id)) {
  574. n += IDR_BITS;
  575. p = *--paa;
  576. }
  577. }
  578. return NULL;
  579. }
  580. EXPORT_SYMBOL(idr_get_next);
  581. /**
  582. * idr_replace - replace pointer for given id
  583. * @idp: idr handle
  584. * @ptr: pointer you want associated with the id
  585. * @id: lookup key
  586. *
  587. * Replace the pointer registered with an id and return the old value.
  588. * A %-ENOENT return indicates that @id was not found.
  589. * A %-EINVAL return indicates that @id was not within valid constraints.
  590. *
  591. * The caller must serialize with writers.
  592. */
  593. void *idr_replace(struct idr *idp, void *ptr, int id)
  594. {
  595. int n;
  596. struct idr_layer *p, *old_p;
  597. p = idp->top;
  598. if (!p)
  599. return ERR_PTR(-EINVAL);
  600. n = (p->layer+1) * IDR_BITS;
  601. id &= MAX_ID_MASK;
  602. if (id >= (1 << n))
  603. return ERR_PTR(-EINVAL);
  604. n -= IDR_BITS;
  605. while ((n > 0) && p) {
  606. p = p->ary[(id >> n) & IDR_MASK];
  607. n -= IDR_BITS;
  608. }
  609. n = id & IDR_MASK;
  610. if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
  611. return ERR_PTR(-ENOENT);
  612. old_p = p->ary[n];
  613. rcu_assign_pointer(p->ary[n], ptr);
  614. return old_p;
  615. }
  616. EXPORT_SYMBOL(idr_replace);
  617. void __init idr_init_cache(void)
  618. {
  619. idr_layer_cache = kmem_cache_create("idr_layer_cache",
  620. sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
  621. }
  622. /**
  623. * idr_init - initialize idr handle
  624. * @idp: idr handle
  625. *
  626. * This function is use to set up the handle (@idp) that you will pass
  627. * to the rest of the functions.
  628. */
  629. void idr_init(struct idr *idp)
  630. {
  631. memset(idp, 0, sizeof(struct idr));
  632. spin_lock_init(&idp->lock);
  633. }
  634. EXPORT_SYMBOL(idr_init);
  635. /**
  636. * DOC: IDA description
  637. * IDA - IDR based ID allocator
  638. *
  639. * This is id allocator without id -> pointer translation. Memory
  640. * usage is much lower than full blown idr because each id only
  641. * occupies a bit. ida uses a custom leaf node which contains
  642. * IDA_BITMAP_BITS slots.
  643. *
  644. * 2007-04-25 written by Tejun Heo <htejun@gmail.com>
  645. */
  646. static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
  647. {
  648. unsigned long flags;
  649. if (!ida->free_bitmap) {
  650. spin_lock_irqsave(&ida->idr.lock, flags);
  651. if (!ida->free_bitmap) {
  652. ida->free_bitmap = bitmap;
  653. bitmap = NULL;
  654. }
  655. spin_unlock_irqrestore(&ida->idr.lock, flags);
  656. }
  657. kfree(bitmap);
  658. }
  659. /**
  660. * ida_pre_get - reserve resources for ida allocation
  661. * @ida: ida handle
  662. * @gfp_mask: memory allocation flag
  663. *
  664. * This function should be called prior to locking and calling the
  665. * following function. It preallocates enough memory to satisfy the
  666. * worst possible allocation.
  667. *
  668. * If the system is REALLY out of memory this function returns %0,
  669. * otherwise %1.
  670. */
  671. int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
  672. {
  673. /* allocate idr_layers */
  674. if (!idr_pre_get(&ida->idr, gfp_mask))
  675. return 0;
  676. /* allocate free_bitmap */
  677. if (!ida->free_bitmap) {
  678. struct ida_bitmap *bitmap;
  679. bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
  680. if (!bitmap)
  681. return 0;
  682. free_bitmap(ida, bitmap);
  683. }
  684. return 1;
  685. }
  686. EXPORT_SYMBOL(ida_pre_get);
  687. /**
  688. * ida_get_new_above - allocate new ID above or equal to a start id
  689. * @ida: ida handle
  690. * @starting_id: id to start search at
  691. * @p_id: pointer to the allocated handle
  692. *
  693. * Allocate new ID above or equal to @starting_id. It should be called
  694. * with any required locks.
  695. *
  696. * If memory is required, it will return %-EAGAIN, you should unlock
  697. * and go back to the ida_pre_get() call. If the ida is full, it will
  698. * return %-ENOSPC.
  699. *
  700. * @p_id returns a value in the range @starting_id ... %0x7fffffff.
  701. */
  702. int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
  703. {
  704. struct idr_layer *pa[MAX_LEVEL];
  705. struct ida_bitmap *bitmap;
  706. unsigned long flags;
  707. int idr_id = starting_id / IDA_BITMAP_BITS;
  708. int offset = starting_id % IDA_BITMAP_BITS;
  709. int t, id;
  710. restart:
  711. /* get vacant slot */
  712. t = idr_get_empty_slot(&ida->idr, idr_id, pa);
  713. if (t < 0)
  714. return _idr_rc_to_errno(t);
  715. if (t * IDA_BITMAP_BITS >= MAX_ID_BIT)
  716. return -ENOSPC;
  717. if (t != idr_id)
  718. offset = 0;
  719. idr_id = t;
  720. /* if bitmap isn't there, create a new one */
  721. bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
  722. if (!bitmap) {
  723. spin_lock_irqsave(&ida->idr.lock, flags);
  724. bitmap = ida->free_bitmap;
  725. ida->free_bitmap = NULL;
  726. spin_unlock_irqrestore(&ida->idr.lock, flags);
  727. if (!bitmap)
  728. return -EAGAIN;
  729. memset(bitmap, 0, sizeof(struct ida_bitmap));
  730. rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
  731. (void *)bitmap);
  732. pa[0]->count++;
  733. }
  734. /* lookup for empty slot */
  735. t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
  736. if (t == IDA_BITMAP_BITS) {
  737. /* no empty slot after offset, continue to the next chunk */
  738. idr_id++;
  739. offset = 0;
  740. goto restart;
  741. }
  742. id = idr_id * IDA_BITMAP_BITS + t;
  743. if (id >= MAX_ID_BIT)
  744. return -ENOSPC;
  745. __set_bit(t, bitmap->bitmap);
  746. if (++bitmap->nr_busy == IDA_BITMAP_BITS)
  747. idr_mark_full(pa, idr_id);
  748. *p_id = id;
  749. /* Each leaf node can handle nearly a thousand slots and the
  750. * whole idea of ida is to have small memory foot print.
  751. * Throw away extra resources one by one after each successful
  752. * allocation.
  753. */
  754. if (ida->idr.id_free_cnt || ida->free_bitmap) {
  755. struct idr_layer *p = get_from_free_list(&ida->idr);
  756. if (p)
  757. kmem_cache_free(idr_layer_cache, p);
  758. }
  759. return 0;
  760. }
  761. EXPORT_SYMBOL(ida_get_new_above);
  762. /**
  763. * ida_get_new - allocate new ID
  764. * @ida: idr handle
  765. * @p_id: pointer to the allocated handle
  766. *
  767. * Allocate new ID. It should be called with any required locks.
  768. *
  769. * If memory is required, it will return %-EAGAIN, you should unlock
  770. * and go back to the idr_pre_get() call. If the idr is full, it will
  771. * return %-ENOSPC.
  772. *
  773. * @p_id returns a value in the range %0 ... %0x7fffffff.
  774. */
  775. int ida_get_new(struct ida *ida, int *p_id)
  776. {
  777. return ida_get_new_above(ida, 0, p_id);
  778. }
  779. EXPORT_SYMBOL(ida_get_new);
  780. /**
  781. * ida_remove - remove the given ID
  782. * @ida: ida handle
  783. * @id: ID to free
  784. */
  785. void ida_remove(struct ida *ida, int id)
  786. {
  787. struct idr_layer *p = ida->idr.top;
  788. int shift = (ida->idr.layers - 1) * IDR_BITS;
  789. int idr_id = id / IDA_BITMAP_BITS;
  790. int offset = id % IDA_BITMAP_BITS;
  791. int n;
  792. struct ida_bitmap *bitmap;
  793. /* clear full bits while looking up the leaf idr_layer */
  794. while ((shift > 0) && p) {
  795. n = (idr_id >> shift) & IDR_MASK;
  796. __clear_bit(n, &p->bitmap);
  797. p = p->ary[n];
  798. shift -= IDR_BITS;
  799. }
  800. if (p == NULL)
  801. goto err;
  802. n = idr_id & IDR_MASK;
  803. __clear_bit(n, &p->bitmap);
  804. bitmap = (void *)p->ary[n];
  805. if (!test_bit(offset, bitmap->bitmap))
  806. goto err;
  807. /* update bitmap and remove it if empty */
  808. __clear_bit(offset, bitmap->bitmap);
  809. if (--bitmap->nr_busy == 0) {
  810. __set_bit(n, &p->bitmap); /* to please idr_remove() */
  811. idr_remove(&ida->idr, idr_id);
  812. free_bitmap(ida, bitmap);
  813. }
  814. return;
  815. err:
  816. printk(KERN_WARNING
  817. "ida_remove called for id=%d which is not allocated.\n", id);
  818. }
  819. EXPORT_SYMBOL(ida_remove);
  820. /**
  821. * ida_destroy - release all cached layers within an ida tree
  822. * @ida: ida handle
  823. */
  824. void ida_destroy(struct ida *ida)
  825. {
  826. idr_destroy(&ida->idr);
  827. kfree(ida->free_bitmap);
  828. }
  829. EXPORT_SYMBOL(ida_destroy);
  830. /**
  831. * ida_simple_get - get a new id.
  832. * @ida: the (initialized) ida.
  833. * @start: the minimum id (inclusive, < 0x8000000)
  834. * @end: the maximum id (exclusive, < 0x8000000 or 0)
  835. * @gfp_mask: memory allocation flags
  836. *
  837. * Allocates an id in the range start <= id < end, or returns -ENOSPC.
  838. * On memory allocation failure, returns -ENOMEM.
  839. *
  840. * Use ida_simple_remove() to get rid of an id.
  841. */
  842. int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
  843. gfp_t gfp_mask)
  844. {
  845. int ret, id;
  846. unsigned int max;
  847. unsigned long flags;
  848. BUG_ON((int)start < 0);
  849. BUG_ON((int)end < 0);
  850. if (end == 0)
  851. max = 0x80000000;
  852. else {
  853. BUG_ON(end < start);
  854. max = end - 1;
  855. }
  856. again:
  857. if (!ida_pre_get(ida, gfp_mask))
  858. return -ENOMEM;
  859. spin_lock_irqsave(&simple_ida_lock, flags);
  860. ret = ida_get_new_above(ida, start, &id);
  861. if (!ret) {
  862. if (id > max) {
  863. ida_remove(ida, id);
  864. ret = -ENOSPC;
  865. } else {
  866. ret = id;
  867. }
  868. }
  869. spin_unlock_irqrestore(&simple_ida_lock, flags);
  870. if (unlikely(ret == -EAGAIN))
  871. goto again;
  872. return ret;
  873. }
  874. EXPORT_SYMBOL(ida_simple_get);
  875. /**
  876. * ida_simple_remove - remove an allocated id.
  877. * @ida: the (initialized) ida.
  878. * @id: the id returned by ida_simple_get.
  879. */
  880. void ida_simple_remove(struct ida *ida, unsigned int id)
  881. {
  882. unsigned long flags;
  883. BUG_ON((int)id < 0);
  884. spin_lock_irqsave(&simple_ida_lock, flags);
  885. ida_remove(ida, id);
  886. spin_unlock_irqrestore(&simple_ida_lock, flags);
  887. }
  888. EXPORT_SYMBOL(ida_simple_remove);
  889. /**
  890. * ida_init - initialize ida handle
  891. * @ida: ida handle
  892. *
  893. * This function is use to set up the handle (@ida) that you will pass
  894. * to the rest of the functions.
  895. */
  896. void ida_init(struct ida *ida)
  897. {
  898. memset(ida, 0, sizeof(struct ida));
  899. idr_init(&ida->idr);
  900. }
  901. EXPORT_SYMBOL(ida_init);