Skip to content

Commit 6d1d540

Browse files
Tetsuo Handagregkh
authored andcommitted
ataflop: remove ataflop_probe_lock mutex
[ Upstream commit 4ddb85d ] Commit bf9c053 ("ataflop: use a separate gendisk for each media format") introduced ataflop_probe_lock mutex, but forgot to unlock the mutex when atari_floppy_init() (i.e. module loading) succeeded. This will result in double lock deadlock if ataflop_probe() is called. Also, unregister_blkdev() must not be called from atari_floppy_init() with ataflop_probe_lock held when atari_floppy_init() failed, for ataflop_probe() waits for ataflop_probe_lock with major_names_lock held (i.e. AB-BA deadlock). __register_blkdev() needs to be called last in order to avoid calling ataflop_probe() when atari_floppy_init() is about to fail, for memory for completing already-started ataflop_probe() safely will be released as soon as atari_floppy_init() released ataflop_probe_lock mutex. As with commit 8b52d8b ("loop: reorder loop_exit"), unregister_blkdev() needs to be called first in order to avoid calling ataflop_alloc_disk() from ataflop_probe() after del_gendisk() from atari_floppy_exit(). By relocating __register_blkdev() / unregister_blkdev() as explained above, we can remove ataflop_probe_lock mutex, for probe function and __exit function are serialized by major_names_lock mutex. Signed-off-by: Tetsuo Handa <[email protected]> Fixes: bf9c053 ("ataflop: use a separate gendisk for each media format") Reviewed-by: Luis Chamberlain <[email protected]> Tested-by: Michael Schmitz <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent e107071 commit 6d1d540

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

drivers/block/ataflop.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,8 +2008,6 @@ static int ataflop_alloc_disk(unsigned int drive, unsigned int type)
20082008
return 0;
20092009
}
20102010

2011-
static DEFINE_MUTEX(ataflop_probe_lock);
2012-
20132011
static void ataflop_probe(dev_t dev)
20142012
{
20152013
int drive = MINOR(dev) & 3;
@@ -2020,14 +2018,32 @@ static void ataflop_probe(dev_t dev)
20202018

20212019
if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS)
20222020
return;
2023-
mutex_lock(&ataflop_probe_lock);
20242021
if (!unit[drive].disk[type]) {
20252022
if (ataflop_alloc_disk(drive, type) == 0) {
20262023
add_disk(unit[drive].disk[type]);
20272024
unit[drive].registered[type] = true;
20282025
}
20292026
}
2030-
mutex_unlock(&ataflop_probe_lock);
2027+
}
2028+
2029+
static void atari_floppy_cleanup(void)
2030+
{
2031+
int i;
2032+
int type;
2033+
2034+
for (i = 0; i < FD_MAX_UNITS; i++) {
2035+
for (type = 0; type < NUM_DISK_MINORS; type++) {
2036+
if (!unit[i].disk[type])
2037+
continue;
2038+
del_gendisk(unit[i].disk[type]);
2039+
blk_cleanup_queue(unit[i].disk[type]->queue);
2040+
put_disk(unit[i].disk[type]);
2041+
}
2042+
blk_mq_free_tag_set(&unit[i].tag_set);
2043+
}
2044+
2045+
del_timer_sync(&fd_timer);
2046+
atari_stram_free(DMABuffer);
20312047
}
20322048

20332049
static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs)
@@ -2053,11 +2069,6 @@ static int __init atari_floppy_init (void)
20532069
/* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
20542070
return -ENODEV;
20552071

2056-
mutex_lock(&ataflop_probe_lock);
2057-
ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2058-
if (ret)
2059-
goto out_unlock;
2060-
20612072
for (i = 0; i < FD_MAX_UNITS; i++) {
20622073
memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
20632074
unit[i].tag_set.ops = &ataflop_mq_ops;
@@ -2111,15 +2122,17 @@ static int __init atari_floppy_init (void)
21112122
UseTrackbuffer ? "" : "no ");
21122123
config_types();
21132124

2114-
return 0;
2125+
ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2126+
if (ret) {
2127+
printk(KERN_ERR "atari_floppy_init: cannot register block device\n");
2128+
atari_floppy_cleanup();
2129+
}
2130+
return ret;
21152131

21162132
err:
21172133
while (--i >= 0)
21182134
atari_cleanup_floppy_disk(&unit[i]);
21192135

2120-
unregister_blkdev(FLOPPY_MAJOR, "fd");
2121-
out_unlock:
2122-
mutex_unlock(&ataflop_probe_lock);
21232136
return ret;
21242137
}
21252138

@@ -2164,14 +2177,8 @@ __setup("floppy=", atari_floppy_setup);
21642177

21652178
static void __exit atari_floppy_exit(void)
21662179
{
2167-
int i;
2168-
2169-
for (i = 0; i < FD_MAX_UNITS; i++)
2170-
atari_cleanup_floppy_disk(&unit[i]);
21712180
unregister_blkdev(FLOPPY_MAJOR, "fd");
2172-
2173-
del_timer_sync(&fd_timer);
2174-
atari_stram_free( DMABuffer );
2181+
atari_floppy_cleanup();
21752182
}
21762183

21772184
module_init(atari_floppy_init)

0 commit comments

Comments
 (0)