Rev 3391 | Rev 4103 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3391 | Rev 4065 | ||
---|---|---|---|
Line 173... | Line 173... | ||
173 | id = id >> IDR_BITS; |
173 | id = id >> IDR_BITS; |
174 | __set_bit((id & IDR_MASK), p->bitmap); |
174 | __set_bit((id & IDR_MASK), p->bitmap); |
175 | } |
175 | } |
176 | } |
176 | } |
Line 177... | Line -... | ||
177 | - | ||
178 | /** |
- | |
179 | * idr_pre_get - reserve resources for idr allocation |
- | |
180 | * @idp: idr handle |
- | |
181 | * @gfp_mask: memory allocation flags |
- | |
182 | * |
- | |
183 | * This function should be called prior to calling the idr_get_new* functions. |
- | |
184 | * It preallocates enough memory to satisfy the worst possible allocation. The |
- | |
185 | * caller should pass in GFP_KERNEL if possible. This of course requires that |
- | |
186 | * no spinning locks be held. |
- | |
187 | * |
- | |
188 | * If the system is REALLY out of memory this function returns %0, |
- | |
189 | * otherwise %1. |
- | |
190 | */ |
177 | |
191 | int idr_pre_get(struct idr *idp, gfp_t gfp_mask) |
178 | int __idr_pre_get(struct idr *idp, gfp_t gfp_mask) |
192 | { |
179 | { |
193 | while (idp->id_free_cnt < MAX_IDR_FREE) { |
180 | while (idp->id_free_cnt < MAX_IDR_FREE) { |
194 | struct idr_layer *new; |
181 | struct idr_layer *new; |
195 | new = kzalloc(sizeof(struct idr_layer), gfp_mask); |
182 | new = kzalloc(sizeof(struct idr_layer), gfp_mask); |
196 | if (new == NULL) |
183 | if (new == NULL) |
197 | return (0); |
184 | return (0); |
198 | move_to_free_list(idp, new); |
185 | move_to_free_list(idp, new); |
199 | } |
186 | } |
200 | return 1; |
187 | return 1; |
201 | } |
188 | } |
Line 202... | Line 189... | ||
202 | EXPORT_SYMBOL(idr_pre_get); |
189 | EXPORT_SYMBOL(__idr_pre_get); |
203 | 190 | ||
204 | /** |
191 | /** |
205 | * sub_alloc - try to allocate an id without growing the tree depth |
192 | * sub_alloc - try to allocate an id without growing the tree depth |
206 | * @idp: idr handle |
- | |
207 | * @starting_id: id to start search at |
193 | * @idp: idr handle |
208 | * @id: pointer to the allocated handle |
194 | * @starting_id: id to start search at |
209 | * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer |
195 | * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer |
210 | * @gfp_mask: allocation mask for idr_layer_alloc() |
196 | * @gfp_mask: allocation mask for idr_layer_alloc() |
211 | * @layer_idr: optional idr passed to idr_layer_alloc() |
197 | * @layer_idr: optional idr passed to idr_layer_alloc() |
Line 365... | Line 351... | ||
365 | rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr); |
351 | rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr); |
366 | pa[0]->count++; |
352 | pa[0]->count++; |
367 | idr_mark_full(pa, id); |
353 | idr_mark_full(pa, id); |
368 | } |
354 | } |
Line 369... | Line -... | ||
369 | - | ||
370 | /** |
- | |
371 | * idr_get_new_above - allocate new idr entry above or equal to a start id |
- | |
372 | * @idp: idr handle |
- | |
373 | * @ptr: pointer you want associated with the id |
- | |
374 | * @starting_id: id to start search at |
- | |
375 | * @id: pointer to the allocated handle |
- | |
376 | * |
- | |
377 | * This is the allocate id function. It should be called with any |
- | |
378 | * required locks. |
- | |
379 | * |
- | |
380 | * If allocation from IDR's private freelist fails, idr_get_new_above() will |
- | |
381 | * return %-EAGAIN. The caller should retry the idr_pre_get() call to refill |
- | |
382 | * IDR's preallocation and then retry the idr_get_new_above() call. |
- | |
383 | * |
- | |
384 | * If the idr is full idr_get_new_above() will return %-ENOSPC. |
- | |
385 | * |
- | |
386 | * @id returns a value in the range @starting_id ... %0x7fffffff |
- | |
387 | */ |
355 | |
388 | int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id) |
356 | int __idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id) |
389 | { |
357 | { |
390 | struct idr_layer *pa[MAX_IDR_LEVEL + 1]; |
358 | struct idr_layer *pa[MAX_IDR_LEVEL + 1]; |
Line 391... | Line 359... | ||
391 | int rv; |
359 | int rv; |
Line 396... | Line 364... | ||
396 | 364 | ||
397 | idr_fill_slot(idp, ptr, rv, pa); |
365 | idr_fill_slot(idp, ptr, rv, pa); |
398 | *id = rv; |
366 | *id = rv; |
399 | return 0; |
367 | return 0; |
400 | } |
368 | } |
Line 401... | Line 369... | ||
401 | EXPORT_SYMBOL(idr_get_new_above); |
369 | EXPORT_SYMBOL(__idr_get_new_above); |
402 | 370 | ||
403 | /** |
371 | /** |
404 | * idr_preload - preload for idr_alloc() |
372 | * idr_preload - preload for idr_alloc() |
Line 494... | Line 462... | ||
494 | } |
462 | } |
495 | EXPORT_SYMBOL_GPL(idr_alloc); |
463 | EXPORT_SYMBOL_GPL(idr_alloc); |
Line 496... | Line 464... | ||
496 | 464 | ||
497 | static void idr_remove_warning(int id) |
465 | static void idr_remove_warning(int id) |
498 | { |
- | |
499 | printk(KERN_WARNING |
466 | { |
500 | "idr_remove called for id=%d which is not allocated.\n", id); |
- | |
501 | // dump_stack(); |
467 | WARN(1, "idr_remove called for id=%d which is not allocated.\n", id); |
Line 502... | Line 468... | ||
502 | } |
468 | } |
503 | 469 | ||
504 | static void sub_remove(struct idr *idp, int shift, int id) |
470 | static void sub_remove(struct idr *idp, int shift, int id) |
Line 546... | Line 512... | ||
546 | void idr_remove(struct idr *idp, int id) |
512 | void idr_remove(struct idr *idp, int id) |
547 | { |
513 | { |
548 | struct idr_layer *p; |
514 | struct idr_layer *p; |
549 | struct idr_layer *to_free; |
515 | struct idr_layer *to_free; |
Line 550... | Line -... | ||
550 | - | ||
551 | /* see comment in idr_find_slowpath() */ |
516 | |
552 | if (WARN_ON_ONCE(id < 0)) |
517 | if (id < 0) |
Line 553... | Line 518... | ||
553 | return; |
518 | return; |
554 | 519 | ||
555 | sub_remove(idp, (idp->layers - 1) * IDR_BITS, id); |
520 | sub_remove(idp, (idp->layers - 1) * IDR_BITS, id); |
Line 644... | Line 609... | ||
644 | void *idr_find_slowpath(struct idr *idp, int id) |
609 | void *idr_find_slowpath(struct idr *idp, int id) |
645 | { |
610 | { |
646 | int n; |
611 | int n; |
647 | struct idr_layer *p; |
612 | struct idr_layer *p; |
Line 648... | Line -... | ||
648 | - | ||
649 | /* |
- | |
650 | * If @id is negative, idr_find() used to ignore the sign bit and |
- | |
651 | * performed lookup with the rest of bits, which is weird and can |
- | |
652 | * lead to very obscure bugs. We're now returning NULL for all |
- | |
653 | * negative IDs but just in case somebody was depending on the sign |
- | |
654 | * bit being ignored, let's trigger WARN_ON_ONCE() so that they can |
- | |
655 | * be detected and fixed. WARN_ON_ONCE() can later be removed. |
- | |
656 | */ |
613 | |
657 | if (WARN_ON_ONCE(id < 0)) |
614 | if (id < 0) |
Line 658... | Line 615... | ||
658 | return NULL; |
615 | return NULL; |
659 | 616 | ||
660 | p = rcu_dereference_raw(idp->top); |
617 | p = rcu_dereference_raw(idp->top); |
Line 802... | Line 759... | ||
802 | void *idr_replace(struct idr *idp, void *ptr, int id) |
759 | void *idr_replace(struct idr *idp, void *ptr, int id) |
803 | { |
760 | { |
804 | int n; |
761 | int n; |
805 | struct idr_layer *p, *old_p; |
762 | struct idr_layer *p, *old_p; |
Line 806... | Line -... | ||
806 | - | ||
807 | /* see comment in idr_find_slowpath() */ |
763 | |
808 | if (WARN_ON_ONCE(id < 0)) |
764 | if (id < 0) |
Line 809... | Line 765... | ||
809 | return ERR_PTR(-EINVAL); |
765 | return ERR_PTR(-EINVAL); |
810 | 766 | ||
811 | p = idp->top; |
767 | p = idp->top; |
Line 836... | Line 792... | ||
836 | 792 | ||
Line 837... | Line 793... | ||
837 | 793 | ||
838 | #endif |
794 | #endif |
839 | 795 | ||
840 | 796 | ||
841 | void idr_init_cache(void) |
797 | void __init idr_init_cache(void) |
Line 856... | Line 812... | ||
856 | memset(idp, 0, sizeof(struct idr)); |
812 | memset(idp, 0, sizeof(struct idr)); |
857 | spin_lock_init(&idp->lock); |
813 | spin_lock_init(&idp->lock); |
858 | } |
814 | } |
859 | EXPORT_SYMBOL(idr_init); |
815 | EXPORT_SYMBOL(idr_init); |
Line 860... | Line -... | ||
860 | - | ||
Line 861... | Line 816... | ||
861 | #if 0 |
816 | |
862 | 817 | ||
863 | /** |
818 | /** |
864 | * DOC: IDA description |
819 | * DOC: IDA description |
Line 1000... | Line 955... | ||
1000 | * allocation. |
955 | * allocation. |
1001 | */ |
956 | */ |
1002 | if (ida->idr.id_free_cnt || ida->free_bitmap) { |
957 | if (ida->idr.id_free_cnt || ida->free_bitmap) { |
1003 | struct idr_layer *p = get_from_free_list(&ida->idr); |
958 | struct idr_layer *p = get_from_free_list(&ida->idr); |
1004 | if (p) |
959 | if (p) |
1005 | kmem_cache_free(idr_layer_cache, p); |
960 | kfree(p); |
1006 | } |
961 | } |
Line 1007... | Line 962... | ||
1007 | 962 | ||
1008 | return 0; |
963 | return 0; |
1009 | } |
964 | } |
Line 1050... | Line 1005... | ||
1050 | } |
1005 | } |
Line 1051... | Line 1006... | ||
1051 | 1006 | ||
Line 1052... | Line 1007... | ||
1052 | return; |
1007 | return; |
1053 | - | ||
1054 | err: |
1008 | |
1055 | printk(KERN_WARNING |
1009 | err: |
1056 | "ida_remove called for id=%d which is not allocated.\n", id); |
1010 | WARN(1, "ida_remove called for id=%d which is not allocated.\n", id); |
Line 1057... | Line 1011... | ||
1057 | } |
1011 | } |
1058 | EXPORT_SYMBOL(ida_remove); |
1012 | EXPORT_SYMBOL(ida_remove); |
Line 1082... | Line 1036... | ||
1082 | 1036 | ||
1083 | } |
1037 | } |
Line 1084... | Line -... | ||
1084 | EXPORT_SYMBOL(ida_init); |
- | |
1085 | - | ||
Line 1086... | Line 1038... | ||
1086 | 1038 | EXPORT_SYMBOL(ida_init); |
|
1087 | #endif |
1039 | |
1088 | 1040 | ||
1089 | 1041 |