The early device registration made possible a race leading to allocations
of disks with wrong minors.
This patch moves the device registration further down the loop_init
function to make the race infeasible.
Found by Linux Driver Verification project (linuxtesting.org).
Signed-off-by: Anton Volkov <avolkov@ispras.ru>
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
        struct loop_device *lo;
        int err;
 
-       err = misc_register(&loop_misc);
-       if (err < 0)
-               return err;
-
        part_shift = 0;
        if (max_part > 0) {
                part_shift = fls(max_part);
 
        if ((1UL << part_shift) > DISK_MAX_PARTS) {
                err = -EINVAL;
-               goto misc_out;
+               goto err_out;
        }
 
        if (max_loop > 1UL << (MINORBITS - part_shift)) {
                err = -EINVAL;
-               goto misc_out;
+               goto err_out;
        }
 
        /*
                range = 1UL << MINORBITS;
        }
 
+       err = misc_register(&loop_misc);
+       if (err < 0)
+               goto err_out;
+
+
        if (register_blkdev(LOOP_MAJOR, "loop")) {
                err = -EIO;
                goto misc_out;
 
 misc_out:
        misc_deregister(&loop_misc);
+err_out:
        return err;
 }