diff options
Diffstat (limited to 'lib/idr.c')
| -rw-r--r-- | lib/idr.c | 22 |
1 files changed, 19 insertions, 3 deletions
| @@ -185,6 +185,7 @@ static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa) | |||
| 185 | new = get_from_free_list(idp); | 185 | new = get_from_free_list(idp); |
| 186 | if (!new) | 186 | if (!new) |
| 187 | return -1; | 187 | return -1; |
| 188 | new->layer = l-1; | ||
| 188 | rcu_assign_pointer(p->ary[m], new); | 189 | rcu_assign_pointer(p->ary[m], new); |
| 189 | p->count++; | 190 | p->count++; |
| 190 | } | 191 | } |
| @@ -210,6 +211,7 @@ build_up: | |||
| 210 | if (unlikely(!p)) { | 211 | if (unlikely(!p)) { |
| 211 | if (!(p = get_from_free_list(idp))) | 212 | if (!(p = get_from_free_list(idp))) |
| 212 | return -1; | 213 | return -1; |
| 214 | p->layer = 0; | ||
| 213 | layers = 1; | 215 | layers = 1; |
| 214 | } | 216 | } |
| 215 | /* | 217 | /* |
| @@ -218,8 +220,14 @@ build_up: | |||
| 218 | */ | 220 | */ |
| 219 | while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) { | 221 | while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) { |
| 220 | layers++; | 222 | layers++; |
| 221 | if (!p->count) | 223 | if (!p->count) { |
| 224 | /* special case: if the tree is currently empty, | ||
| 225 | * then we grow the tree by moving the top node | ||
| 226 | * upwards. | ||
| 227 | */ | ||
| 228 | p->layer++; | ||
| 222 | continue; | 229 | continue; |
| 230 | } | ||
| 223 | if (!(new = get_from_free_list(idp))) { | 231 | if (!(new = get_from_free_list(idp))) { |
| 224 | /* | 232 | /* |
| 225 | * The allocation failed. If we built part of | 233 | * The allocation failed. If we built part of |
| @@ -237,6 +245,7 @@ build_up: | |||
| 237 | } | 245 | } |
| 238 | new->ary[0] = p; | 246 | new->ary[0] = p; |
| 239 | new->count = 1; | 247 | new->count = 1; |
| 248 | new->layer = layers-1; | ||
| 240 | if (p->bitmap == IDR_FULL) | 249 | if (p->bitmap == IDR_FULL) |
| 241 | __set_bit(0, &new->bitmap); | 250 | __set_bit(0, &new->bitmap); |
| 242 | p = new; | 251 | p = new; |
| @@ -493,17 +502,21 @@ void *idr_find(struct idr *idp, int id) | |||
| 493 | int n; | 502 | int n; |
| 494 | struct idr_layer *p; | 503 | struct idr_layer *p; |
| 495 | 504 | ||
| 496 | n = idp->layers * IDR_BITS; | ||
| 497 | p = rcu_dereference(idp->top); | 505 | p = rcu_dereference(idp->top); |
| 506 | if (!p) | ||
| 507 | return NULL; | ||
| 508 | n = (p->layer+1) * IDR_BITS; | ||
| 498 | 509 | ||
| 499 | /* Mask off upper bits we don't use for the search. */ | 510 | /* Mask off upper bits we don't use for the search. */ |
| 500 | id &= MAX_ID_MASK; | 511 | id &= MAX_ID_MASK; |
| 501 | 512 | ||
| 502 | if (id >= (1 << n)) | 513 | if (id >= (1 << n)) |
| 503 | return NULL; | 514 | return NULL; |
| 515 | BUG_ON(n == 0); | ||
| 504 | 516 | ||
| 505 | while (n > 0 && p) { | 517 | while (n > 0 && p) { |
| 506 | n -= IDR_BITS; | 518 | n -= IDR_BITS; |
| 519 | BUG_ON(n != p->layer*IDR_BITS); | ||
| 507 | p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]); | 520 | p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]); |
| 508 | } | 521 | } |
| 509 | return((void *)p); | 522 | return((void *)p); |
| @@ -582,8 +595,11 @@ void *idr_replace(struct idr *idp, void *ptr, int id) | |||
| 582 | int n; | 595 | int n; |
| 583 | struct idr_layer *p, *old_p; | 596 | struct idr_layer *p, *old_p; |
| 584 | 597 | ||
| 585 | n = idp->layers * IDR_BITS; | ||
| 586 | p = idp->top; | 598 | p = idp->top; |
| 599 | if (!p) | ||
| 600 | return ERR_PTR(-EINVAL); | ||
| 601 | |||
| 602 | n = (p->layer+1) * IDR_BITS; | ||
| 587 | 603 | ||
| 588 | id &= MAX_ID_MASK; | 604 | id &= MAX_ID_MASK; |
| 589 | 605 | ||
