static DEFINE_PER_CPU(int, idr_preload_cnt);
 static DEFINE_SPINLOCK(simple_ida_lock);
 
+/* the maximum ID which can be allocated given idr->layers */
+static int idr_max(int layers)
+{
+       int bits = min_t(int, layers * IDR_BITS, MAX_IDR_SHIFT);
+
+       return (1 << bits) - 1;
+}
+
 static struct idr_layer *get_from_free_list(struct idr *idp)
 {
        struct idr_layer *p;
         * Add a new layer to the top of the tree if the requested
         * id is larger than the currently allocated space.
         */
-       while ((layers < (MAX_IDR_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
+       while (id > idr_max(layers)) {
                layers++;
                if (!p->count) {
                        /* special case: if the tree is currently empty,
  */
 int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
 {
-       struct idr_layer *pa[MAX_IDR_LEVEL];
+       struct idr_layer *pa[MAX_IDR_LEVEL + 1];
        int rv;
 
        rv = idr_get_empty_slot(idp, starting_id, pa, 0, idp);
 int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
 {
        int max = end > 0 ? end - 1 : INT_MAX;  /* inclusive upper limit */
-       struct idr_layer *pa[MAX_IDR_LEVEL];
+       struct idr_layer *pa[MAX_IDR_LEVEL + 1];
        int id;
 
        might_sleep_if(gfp_mask & __GFP_WAIT);
 static void sub_remove(struct idr *idp, int shift, int id)
 {
        struct idr_layer *p = idp->top;
-       struct idr_layer **pa[MAX_IDR_LEVEL];
+       struct idr_layer **pa[MAX_IDR_LEVEL + 1];
        struct idr_layer ***paa = &pa[0];
        struct idr_layer *to_free;
        int n;
        int n, id, max;
        int bt_mask;
        struct idr_layer *p;
-       struct idr_layer *pa[MAX_IDR_LEVEL];
+       struct idr_layer *pa[MAX_IDR_LEVEL + 1];
        struct idr_layer **paa = &pa[0];
 
        n = idp->layers * IDR_BITS;
        p = idp->top;
        rcu_assign_pointer(idp->top, NULL);
-       max = 1 << n;
+       max = idr_max(idp->layers);
 
        id = 0;
-       while (id < max) {
+       while (id >= 0 && id <= max) {
                while (n > IDR_BITS && p) {
                        n -= IDR_BITS;
                        *paa++ = p;
        /* Mask off upper bits we don't use for the search. */
        id &= MAX_IDR_MASK;
 
-       if (id >= (1 << n))
+       if (id > idr_max(p->layer + 1))
                return NULL;
        BUG_ON(n == 0);
 
 {
        int n, id, max, error = 0;
        struct idr_layer *p;
-       struct idr_layer *pa[MAX_IDR_LEVEL];
+       struct idr_layer *pa[MAX_IDR_LEVEL + 1];
        struct idr_layer **paa = &pa[0];
 
        n = idp->layers * IDR_BITS;
        p = rcu_dereference_raw(idp->top);
-       max = 1 << n;
+       max = idr_max(idp->layers);
 
        id = 0;
-       while (id < max) {
+       while (id >= 0 && id <= max) {
                while (n > 0 && p) {
                        n -= IDR_BITS;
                        *paa++ = p;
  */
 void *idr_get_next(struct idr *idp, int *nextidp)
 {
-       struct idr_layer *p, *pa[MAX_IDR_LEVEL];
+       struct idr_layer *p, *pa[MAX_IDR_LEVEL + 1];
        struct idr_layer **paa = &pa[0];
        int id = *nextidp;
        int n, max;
        if (!p)
                return NULL;
        n = (p->layer + 1) * IDR_BITS;
-       max = 1 << n;
+       max = idr_max(p->layer + 1);
 
-       while (id < max) {
+       while (id >= 0 && id <= max) {
                while (n > 0 && p) {
                        n -= IDR_BITS;
                        *paa++ = p;
  */
 int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
 {
-       struct idr_layer *pa[MAX_IDR_LEVEL];
+       struct idr_layer *pa[MAX_IDR_LEVEL + 1];
        struct ida_bitmap *bitmap;
        unsigned long flags;
        int idr_id = starting_id / IDA_BITMAP_BITS;