Skip to content

Commit

Permalink
idr: remove length restriction from idr_layer->bitmap
Browse files Browse the repository at this point in the history
Currently, idr->bitmap is declared as an unsigned long which restricts
the number of bits an idr_layer can contain.  All bitops can handle
arbitrary positive integer bit number and there's no reason for this
restriction.

Declare idr_layer->bitmap using DECLARE_BITMAP() instead of a single
unsigned long.

* idr_layer->bitmap is now an array.  '&' dropped from params to
  bitops.

* Replaced "== IDR_FULL" tests with bitmap_full() and removed
  IDR_FULL.

* Replaced find_next_bit() on ~bitmap with find_next_zero_bit().

* Replaced "bitmap = 0" with bitmap_clear().

This patch doesn't (or at least shouldn't) introduce any behavior
changes.

[[email protected]: checkpatch fixes]
Signed-off-by: Tejun Heo <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
htejun authored and torvalds committed Feb 28, 2013
1 parent e8c8d1b commit 1d9b2e1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
12 changes: 1 addition & 11 deletions include/linux/idr.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,8 @@

#if BITS_PER_LONG == 32
# define IDR_BITS 5
# define IDR_FULL 0xfffffffful
/* We can only use two of the bits in the top level because there is
only one possible bit in the top level (5 bits * 7 levels = 35
bits, but you only use 31 bits in the id). */
# define TOP_LEVEL_FULL (IDR_FULL >> 30)
#elif BITS_PER_LONG == 64
# define IDR_BITS 6
# define IDR_FULL 0xfffffffffffffffful
/* We can only use two of the bits in the top level because there is
only one possible bit in the top level (6 bits * 6 levels = 36
bits, but you only use 31 bits in the id). */
# define TOP_LEVEL_FULL (IDR_FULL >> 62)
#else
# error "BITS_PER_LONG is not 32 or 64"
#endif
Expand All @@ -39,7 +29,7 @@
#define IDR_MASK ((1 << IDR_BITS)-1)

struct idr_layer {
unsigned long bitmap; /* A zero bit means "space here" */
DECLARE_BITMAP(bitmap, IDR_SIZE); /* A zero bit means "space here" */
struct idr_layer __rcu *ary[1<<IDR_BITS];
int count; /* When zero, we can release it */
int layer; /* distance from leaf */
Expand Down
34 changes: 17 additions & 17 deletions lib/idr.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,18 +157,18 @@ static void idr_mark_full(struct idr_layer **pa, int id)
struct idr_layer *p = pa[0];
int l = 0;

__set_bit(id & IDR_MASK, &p->bitmap);
__set_bit(id & IDR_MASK, p->bitmap);
/*
* If this layer is full mark the bit in the layer above to
* show that this part of the radix tree is full. This may
* complete the layer above and require walking up the radix
* tree.
*/
while (p->bitmap == IDR_FULL) {
while (bitmap_full(p->bitmap, IDR_SIZE)) {
if (!(p = pa[++l]))
break;
id = id >> IDR_BITS;
__set_bit((id & IDR_MASK), &p->bitmap);
__set_bit((id & IDR_MASK), p->bitmap);
}
}

Expand Down Expand Up @@ -221,7 +221,6 @@ static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
int n, m, sh;
struct idr_layer *p, *new;
int l, id, oid;
unsigned long bm;

id = *starting_id;
restart:
Expand All @@ -233,8 +232,7 @@ static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
* We run around this while until we reach the leaf node...
*/
n = (id >> (IDR_BITS*l)) & IDR_MASK;
bm = ~p->bitmap;
m = find_next_bit(&bm, IDR_SIZE, n);
m = find_next_zero_bit(p->bitmap, IDR_SIZE, n);
if (m == IDR_SIZE) {
/* no space available go back to previous layer. */
l++;
Expand Down Expand Up @@ -326,7 +324,8 @@ static int idr_get_empty_slot(struct idr *idp, int starting_id,
for (new = p; p && p != idp->top; new = p) {
p = p->ary[0];
new->ary[0] = NULL;
new->bitmap = new->count = 0;
new->count = 0;
bitmap_clear(new->bitmap, 0, IDR_SIZE);
__move_to_free_list(idp, new);
}
spin_unlock_irqrestore(&idp->lock, flags);
Expand All @@ -335,8 +334,8 @@ static int idr_get_empty_slot(struct idr *idp, int starting_id,
new->ary[0] = p;
new->count = 1;
new->layer = layers-1;
if (p->bitmap == IDR_FULL)
__set_bit(0, &new->bitmap);
if (bitmap_full(p->bitmap, IDR_SIZE))
__set_bit(0, new->bitmap);
p = new;
}
rcu_assign_pointer(idp->top, p);
Expand Down Expand Up @@ -517,14 +516,14 @@ static void sub_remove(struct idr *idp, int shift, int id)

while ((shift > 0) && p) {
n = (id >> shift) & IDR_MASK;
__clear_bit(n, &p->bitmap);
__clear_bit(n, p->bitmap);
*++paa = &p->ary[n];
p = p->ary[n];
shift -= IDR_BITS;
}
n = id & IDR_MASK;
if (likely(p != NULL && test_bit(n, &p->bitmap))){
__clear_bit(n, &p->bitmap);
if (likely(p != NULL && test_bit(n, p->bitmap))) {
__clear_bit(n, p->bitmap);
rcu_assign_pointer(p->ary[n], NULL);
to_free = NULL;
while(*paa && ! --((**paa)->count)){
Expand Down Expand Up @@ -567,7 +566,8 @@ void idr_remove(struct idr *idp, int id)
p = idp->top->ary[0];
rcu_assign_pointer(idp->top, p);
--idp->layers;
to_free->bitmap = to_free->count = 0;
to_free->count = 0;
bitmap_clear(to_free->bitmap, 0, IDR_SIZE);
free_layer(to_free);
}
while (idp->id_free_cnt >= MAX_IDR_FREE) {
Expand Down Expand Up @@ -827,7 +827,7 @@ void *idr_replace(struct idr *idp, void *ptr, int id)
}

n = id & IDR_MASK;
if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
if (unlikely(p == NULL || !test_bit(n, p->bitmap)))
return ERR_PTR(-ENOENT);

old_p = p->ary[n];
Expand Down Expand Up @@ -1024,7 +1024,7 @@ void ida_remove(struct ida *ida, int id)
/* clear full bits while looking up the leaf idr_layer */
while ((shift > 0) && p) {
n = (idr_id >> shift) & IDR_MASK;
__clear_bit(n, &p->bitmap);
__clear_bit(n, p->bitmap);
p = p->ary[n];
shift -= IDR_BITS;
}
Expand All @@ -1033,7 +1033,7 @@ void ida_remove(struct ida *ida, int id)
goto err;

n = idr_id & IDR_MASK;
__clear_bit(n, &p->bitmap);
__clear_bit(n, p->bitmap);

bitmap = (void *)p->ary[n];
if (!test_bit(offset, bitmap->bitmap))
Expand All @@ -1042,7 +1042,7 @@ void ida_remove(struct ida *ida, int id)
/* update bitmap and remove it if empty */
__clear_bit(offset, bitmap->bitmap);
if (--bitmap->nr_busy == 0) {
__set_bit(n, &p->bitmap); /* to please idr_remove() */
__set_bit(n, p->bitmap); /* to please idr_remove() */
idr_remove(&ida->idr, idr_id);
free_bitmap(ida, bitmap);
}
Expand Down

0 comments on commit 1d9b2e1

Please sign in to comment.