blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, nullb->q);
 
        mutex_lock(&lock);
-       nullb->index = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
-       dev->index = nullb->index;
+       rv = ida_simple_get(&nullb_indexes, 0, 0, GFP_KERNEL);
+       if (rv < 0) {
+               mutex_unlock(&lock);
+               goto out_cleanup_zone;
+       }
+       nullb->index = rv;
+       dev->index = rv;
        mutex_unlock(&lock);
 
        blk_queue_logical_block_size(nullb->q, dev->blocksize);
 
        rv = null_gendisk_register(nullb);
        if (rv)
-               goto out_cleanup_zone;
+               goto out_ida_free;
 
        mutex_lock(&lock);
        list_add_tail(&nullb->list, &nullb_list);
        mutex_unlock(&lock);
 
        return 0;
+
+out_ida_free:
+       ida_free(&nullb_indexes, nullb->index);
 out_cleanup_zone:
        null_free_zoned_dev(dev);
 out_cleanup_disk: