brd: remove brd_devices_mutex mutex

If brd_alloc() from brd_probe() is called before brd_alloc() from
brd_init() is called, module loading will fail with -EEXIST error.
To close this race, call __register_blkdev() just before leaving
brd_init().

Then, we can remove brd_devices_mutex mutex, for brd_device list
will no longer be accessed concurrently.

Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Luis Chamberlain <mcgrof@kernel.org>
Link: https://lore.kernel.org/r/6b074af7-c165-4fab-b7da-8270a4f6f6cd@i-love.sakura.ne.jp
Signed-off-by: Jens Axboe <axboe@kernel.dk>
This commit is contained in:
Tetsuo Handa 2022-01-06 18:53:16 +09:00 коммит произвёл Jens Axboe
Родитель a6431e351c
Коммит 00358933f6
1 изменённых файлов: 30 добавлений и 43 удалений

Просмотреть файл

@ -362,7 +362,6 @@ __setup("ramdisk_size=", ramdisk_size);
* (should share code eventually). * (should share code eventually).
*/ */
static LIST_HEAD(brd_devices); static LIST_HEAD(brd_devices);
static DEFINE_MUTEX(brd_devices_mutex);
static struct dentry *brd_debugfs_dir; static struct dentry *brd_debugfs_dir;
static int brd_alloc(int i) static int brd_alloc(int i)
@ -372,21 +371,14 @@ static int brd_alloc(int i)
char buf[DISK_NAME_LEN]; char buf[DISK_NAME_LEN];
int err = -ENOMEM; int err = -ENOMEM;
mutex_lock(&brd_devices_mutex); list_for_each_entry(brd, &brd_devices, brd_list)
list_for_each_entry(brd, &brd_devices, brd_list) { if (brd->brd_number == i)
if (brd->brd_number == i) {
mutex_unlock(&brd_devices_mutex);
return -EEXIST; return -EEXIST;
}
}
brd = kzalloc(sizeof(*brd), GFP_KERNEL); brd = kzalloc(sizeof(*brd), GFP_KERNEL);
if (!brd) { if (!brd)
mutex_unlock(&brd_devices_mutex);
return -ENOMEM; return -ENOMEM;
}
brd->brd_number = i; brd->brd_number = i;
list_add_tail(&brd->brd_list, &brd_devices); list_add_tail(&brd->brd_list, &brd_devices);
mutex_unlock(&brd_devices_mutex);
spin_lock_init(&brd->brd_lock); spin_lock_init(&brd->brd_lock);
INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC); INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
@ -429,9 +421,7 @@ static int brd_alloc(int i)
out_cleanup_disk: out_cleanup_disk:
blk_cleanup_disk(disk); blk_cleanup_disk(disk);
out_free_dev: out_free_dev:
mutex_lock(&brd_devices_mutex);
list_del(&brd->brd_list); list_del(&brd->brd_list);
mutex_unlock(&brd_devices_mutex);
kfree(brd); kfree(brd);
return err; return err;
} }
@ -441,15 +431,19 @@ static void brd_probe(dev_t dev)
brd_alloc(MINOR(dev) / max_part); brd_alloc(MINOR(dev) / max_part);
} }
static void brd_del_one(struct brd_device *brd) static void brd_cleanup(void)
{ {
del_gendisk(brd->brd_disk); struct brd_device *brd, *next;
blk_cleanup_disk(brd->brd_disk);
brd_free_pages(brd); debugfs_remove_recursive(brd_debugfs_dir);
mutex_lock(&brd_devices_mutex);
list_del(&brd->brd_list); list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
mutex_unlock(&brd_devices_mutex); del_gendisk(brd->brd_disk);
kfree(brd); blk_cleanup_disk(brd->brd_disk);
brd_free_pages(brd);
list_del(&brd->brd_list);
kfree(brd);
}
} }
static inline void brd_check_and_reset_par(void) static inline void brd_check_and_reset_par(void)
@ -473,9 +467,18 @@ static inline void brd_check_and_reset_par(void)
static int __init brd_init(void) static int __init brd_init(void)
{ {
struct brd_device *brd, *next;
int err, i; int err, i;
brd_check_and_reset_par();
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
for (i = 0; i < rd_nr; i++) {
err = brd_alloc(i);
if (err)
goto out_free;
}
/* /*
* brd module now has a feature to instantiate underlying device * brd module now has a feature to instantiate underlying device
* structure on-demand, provided that there is an access dev node. * structure on-demand, provided that there is an access dev node.
@ -491,28 +494,16 @@ static int __init brd_init(void)
* dynamically. * dynamically.
*/ */
if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) {
return -EIO; err = -EIO;
goto out_free;
brd_check_and_reset_par();
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
for (i = 0; i < rd_nr; i++) {
err = brd_alloc(i);
if (err)
goto out_free;
} }
pr_info("brd: module loaded\n"); pr_info("brd: module loaded\n");
return 0; return 0;
out_free: out_free:
unregister_blkdev(RAMDISK_MAJOR, "ramdisk"); brd_cleanup();
debugfs_remove_recursive(brd_debugfs_dir);
list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
brd_del_one(brd);
pr_info("brd: module NOT loaded !!!\n"); pr_info("brd: module NOT loaded !!!\n");
return err; return err;
@ -520,13 +511,9 @@ out_free:
static void __exit brd_exit(void) static void __exit brd_exit(void)
{ {
struct brd_device *brd, *next;
unregister_blkdev(RAMDISK_MAJOR, "ramdisk"); unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
debugfs_remove_recursive(brd_debugfs_dir); brd_cleanup();
list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
brd_del_one(brd);
pr_info("brd: module unloaded\n"); pr_info("brd: module unloaded\n");
} }