Skip to content

Commit 0035893

Browse files
Tetsuo Handaaxboe
authored andcommitted
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 <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Luis Chamberlain <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent a6431e3 commit 0035893

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

drivers/block/brd.c

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,6 @@ __setup("ramdisk_size=", ramdisk_size);
362362
* (should share code eventually).
363363
*/
364364
static LIST_HEAD(brd_devices);
365-
static DEFINE_MUTEX(brd_devices_mutex);
366365
static struct dentry *brd_debugfs_dir;
367366

368367
static int brd_alloc(int i)
@@ -372,21 +371,14 @@ static int brd_alloc(int i)
372371
char buf[DISK_NAME_LEN];
373372
int err = -ENOMEM;
374373

375-
mutex_lock(&brd_devices_mutex);
376-
list_for_each_entry(brd, &brd_devices, brd_list) {
377-
if (brd->brd_number == i) {
378-
mutex_unlock(&brd_devices_mutex);
374+
list_for_each_entry(brd, &brd_devices, brd_list)
375+
if (brd->brd_number == i)
379376
return -EEXIST;
380-
}
381-
}
382377
brd = kzalloc(sizeof(*brd), GFP_KERNEL);
383-
if (!brd) {
384-
mutex_unlock(&brd_devices_mutex);
378+
if (!brd)
385379
return -ENOMEM;
386-
}
387380
brd->brd_number = i;
388381
list_add_tail(&brd->brd_list, &brd_devices);
389-
mutex_unlock(&brd_devices_mutex);
390382

391383
spin_lock_init(&brd->brd_lock);
392384
INIT_RADIX_TREE(&brd->brd_pages, GFP_ATOMIC);
@@ -429,9 +421,7 @@ static int brd_alloc(int i)
429421
out_cleanup_disk:
430422
blk_cleanup_disk(disk);
431423
out_free_dev:
432-
mutex_lock(&brd_devices_mutex);
433424
list_del(&brd->brd_list);
434-
mutex_unlock(&brd_devices_mutex);
435425
kfree(brd);
436426
return err;
437427
}
@@ -441,15 +431,19 @@ static void brd_probe(dev_t dev)
441431
brd_alloc(MINOR(dev) / max_part);
442432
}
443433

444-
static void brd_del_one(struct brd_device *brd)
434+
static void brd_cleanup(void)
445435
{
446-
del_gendisk(brd->brd_disk);
447-
blk_cleanup_disk(brd->brd_disk);
448-
brd_free_pages(brd);
449-
mutex_lock(&brd_devices_mutex);
450-
list_del(&brd->brd_list);
451-
mutex_unlock(&brd_devices_mutex);
452-
kfree(brd);
436+
struct brd_device *brd, *next;
437+
438+
debugfs_remove_recursive(brd_debugfs_dir);
439+
440+
list_for_each_entry_safe(brd, next, &brd_devices, brd_list) {
441+
del_gendisk(brd->brd_disk);
442+
blk_cleanup_disk(brd->brd_disk);
443+
brd_free_pages(brd);
444+
list_del(&brd->brd_list);
445+
kfree(brd);
446+
}
453447
}
454448

455449
static inline void brd_check_and_reset_par(void)
@@ -473,9 +467,18 @@ static inline void brd_check_and_reset_par(void)
473467

474468
static int __init brd_init(void)
475469
{
476-
struct brd_device *brd, *next;
477470
int err, i;
478471

472+
brd_check_and_reset_par();
473+
474+
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
475+
476+
for (i = 0; i < rd_nr; i++) {
477+
err = brd_alloc(i);
478+
if (err)
479+
goto out_free;
480+
}
481+
479482
/*
480483
* brd module now has a feature to instantiate underlying device
481484
* structure on-demand, provided that there is an access dev node.
@@ -491,42 +494,26 @@ static int __init brd_init(void)
491494
* dynamically.
492495
*/
493496

494-
if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe))
495-
return -EIO;
496-
497-
brd_check_and_reset_par();
498-
499-
brd_debugfs_dir = debugfs_create_dir("ramdisk_pages", NULL);
500-
501-
for (i = 0; i < rd_nr; i++) {
502-
err = brd_alloc(i);
503-
if (err)
504-
goto out_free;
497+
if (__register_blkdev(RAMDISK_MAJOR, "ramdisk", brd_probe)) {
498+
err = -EIO;
499+
goto out_free;
505500
}
506501

507502
pr_info("brd: module loaded\n");
508503
return 0;
509504

510505
out_free:
511-
unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
512-
debugfs_remove_recursive(brd_debugfs_dir);
513-
514-
list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
515-
brd_del_one(brd);
506+
brd_cleanup();
516507

517508
pr_info("brd: module NOT loaded !!!\n");
518509
return err;
519510
}
520511

521512
static void __exit brd_exit(void)
522513
{
523-
struct brd_device *brd, *next;
524514

525515
unregister_blkdev(RAMDISK_MAJOR, "ramdisk");
526-
debugfs_remove_recursive(brd_debugfs_dir);
527-
528-
list_for_each_entry_safe(brd, next, &brd_devices, brd_list)
529-
brd_del_one(brd);
516+
brd_cleanup();
530517

531518
pr_info("brd: module unloaded\n");
532519
}

0 commit comments

Comments
 (0)