Skip to content

Commit cb690f5

Browse files
committed
Merge tag 'for-5.16/drivers-2021-11-09' of git://git.kernel.dk/linux-block
Pull more block driver updates from Jens Axboe: - Last series adding error handling support for add_disk() in drivers. After this one, and once the SCSI side has been merged, we can finally annotate add_disk() as must_check. (Luis) - bcache fixes (Coly) - zram fixes (Ming) - ataflop locking fix (Tetsuo) - nbd fixes (Ye, Yu) - MD merge via Song - Cleanup (Yang) - sysfs fix (Guoqing) - Misc fixes (Geert, Wu, luo) * tag 'for-5.16/drivers-2021-11-09' of git://git.kernel.dk/linux-block: (34 commits) bcache: Revert "bcache: use bvec_virt" ataflop: Add missing semicolon to return statement floppy: address add_disk() error handling on probe ataflop: address add_disk() error handling on probe block: update __register_blkdev() probe documentation ataflop: remove ataflop_probe_lock mutex mtd/ubi/block: add error handling support for add_disk() block/sunvdc: add error handling support for add_disk() z2ram: add error handling support for add_disk() nvdimm/pmem: use add_disk() error handling nvdimm/pmem: cleanup the disk if pmem_release_disk() is yet assigned nvdimm/blk: add error handling support for add_disk() nvdimm/blk: avoid calling del_gendisk() on early failures nvdimm/btt: add error handling support for add_disk() nvdimm/btt: use goto error labels on btt_blk_init() loop: Remove duplicate assignments drbd: Fix double free problem in drbd_create_device nvdimm/btt: do not call del_gendisk() if not needed bcache: fix use-after-free problem in bcache_device_free() zram: replace fsync_bdev with sync_blockdev ...
2 parents 3e28850 + 2878fea commit cb690f5

File tree

20 files changed

+221
-101
lines changed

20 files changed

+221
-101
lines changed

block/genhd.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,10 @@ void blkdev_show(struct seq_file *seqf, off_t offset)
213213
* @major: the requested major device number [1..BLKDEV_MAJOR_MAX-1]. If
214214
* @major = 0, try to allocate any unused major number.
215215
* @name: the name of the new block device as a zero terminated string
216-
* @probe: allback that is called on access to any minor number of @major
216+
* @probe: pre-devtmpfs / pre-udev callback used to create disks when their
217+
* pre-created device node is accessed. When a probe call uses
218+
* add_disk() and it fails the driver must cleanup resources. This
219+
* interface may soon be removed.
217220
*
218221
* The @name must be unique within the system.
219222
*

drivers/block/ataflop.c

Lines changed: 37 additions & 24 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,38 @@ 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);
2024-
if (!unit[drive].disk[type]) {
2025-
if (ataflop_alloc_disk(drive, type) == 0) {
2026-
add_disk(unit[drive].disk[type]);
2027-
unit[drive].registered[type] = true;
2021+
if (unit[drive].disk[type])
2022+
return;
2023+
if (ataflop_alloc_disk(drive, type))
2024+
return;
2025+
if (add_disk(unit[drive].disk[type]))
2026+
goto cleanup_disk;
2027+
unit[drive].registered[type] = true;
2028+
return;
2029+
2030+
cleanup_disk:
2031+
blk_cleanup_disk(unit[drive].disk[type]);
2032+
unit[drive].disk[type] = NULL;
2033+
}
2034+
2035+
static void atari_floppy_cleanup(void)
2036+
{
2037+
int i;
2038+
int type;
2039+
2040+
for (i = 0; i < FD_MAX_UNITS; i++) {
2041+
for (type = 0; type < NUM_DISK_MINORS; type++) {
2042+
if (!unit[i].disk[type])
2043+
continue;
2044+
del_gendisk(unit[i].disk[type]);
2045+
blk_cleanup_queue(unit[i].disk[type]->queue);
2046+
put_disk(unit[i].disk[type]);
20282047
}
2048+
blk_mq_free_tag_set(&unit[i].tag_set);
20292049
}
2030-
mutex_unlock(&ataflop_probe_lock);
2050+
2051+
del_timer_sync(&fd_timer);
2052+
atari_stram_free(DMABuffer);
20312053
}
20322054

20332055
static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs)
@@ -2053,11 +2075,6 @@ static int __init atari_floppy_init (void)
20532075
/* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
20542076
return -ENODEV;
20552077

2056-
mutex_lock(&ataflop_probe_lock);
2057-
ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2058-
if (ret)
2059-
goto out_unlock;
2060-
20612078
for (i = 0; i < FD_MAX_UNITS; i++) {
20622079
memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set));
20632080
unit[i].tag_set.ops = &ataflop_mq_ops;
@@ -2113,17 +2130,19 @@ static int __init atari_floppy_init (void)
21132130
UseTrackbuffer ? "" : "no ");
21142131
config_types();
21152132

2116-
return 0;
2133+
ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe);
2134+
if (ret) {
2135+
printk(KERN_ERR "atari_floppy_init: cannot register block device\n");
2136+
atari_floppy_cleanup();
2137+
}
2138+
return ret;
21172139

21182140
err_out_dma:
21192141
atari_stram_free(DMABuffer);
21202142
err:
21212143
while (--i >= 0)
21222144
atari_cleanup_floppy_disk(&unit[i]);
21232145

2124-
unregister_blkdev(FLOPPY_MAJOR, "fd");
2125-
out_unlock:
2126-
mutex_unlock(&ataflop_probe_lock);
21272146
return ret;
21282147
}
21292148

@@ -2168,14 +2187,8 @@ __setup("floppy=", atari_floppy_setup);
21682187

21692188
static void __exit atari_floppy_exit(void)
21702189
{
2171-
int i;
2172-
2173-
for (i = 0; i < FD_MAX_UNITS; i++)
2174-
atari_cleanup_floppy_disk(&unit[i]);
21752190
unregister_blkdev(FLOPPY_MAJOR, "fd");
2176-
2177-
del_timer_sync(&fd_timer);
2178-
atari_stram_free( DMABuffer );
2191+
atari_floppy_cleanup();
21792192
}
21802193

21812194
module_init(atari_floppy_init)

drivers/block/brd.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ static int brd_alloc(int i)
370370
struct brd_device *brd;
371371
struct gendisk *disk;
372372
char buf[DISK_NAME_LEN];
373+
int err = -ENOMEM;
373374

374375
mutex_lock(&brd_devices_mutex);
375376
list_for_each_entry(brd, &brd_devices, brd_list) {
@@ -420,16 +421,20 @@ static int brd_alloc(int i)
420421
/* Tell the block layer that this is not a rotational device */
421422
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
422423
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, disk->queue);
423-
add_disk(disk);
424+
err = add_disk(disk);
425+
if (err)
426+
goto out_cleanup_disk;
424427

425428
return 0;
426429

430+
out_cleanup_disk:
431+
blk_cleanup_disk(disk);
427432
out_free_dev:
428433
mutex_lock(&brd_devices_mutex);
429434
list_del(&brd->brd_list);
430435
mutex_unlock(&brd_devices_mutex);
431436
kfree(brd);
432-
return -ENOMEM;
437+
return err;
433438
}
434439

435440
static void brd_probe(dev_t dev)

drivers/block/drbd/drbd_main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2796,7 +2796,7 @@ enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsig
27962796

27972797
err = add_disk(disk);
27982798
if (err)
2799-
goto out_cleanup_disk;
2799+
goto out_idr_remove_vol;
28002800

28012801
/* inherit the connection state */
28022802
device->state.conn = first_connection(resource)->cstate;
@@ -2810,8 +2810,6 @@ enum drbd_ret_code drbd_create_device(struct drbd_config_context *adm_ctx, unsig
28102810
drbd_debugfs_device_add(device);
28112811
return NO_ERROR;
28122812

2813-
out_cleanup_disk:
2814-
blk_cleanup_disk(disk);
28152813
out_idr_remove_vol:
28162814
idr_remove(&connection->peer_devices, vnr);
28172815
out_idr_remove_from_resource:

drivers/block/floppy.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4528,10 +4528,19 @@ static void floppy_probe(dev_t dev)
45284528
return;
45294529

45304530
mutex_lock(&floppy_probe_lock);
4531-
if (!disks[drive][type]) {
4532-
if (floppy_alloc_disk(drive, type) == 0)
4533-
add_disk(disks[drive][type]);
4534-
}
4531+
if (disks[drive][type])
4532+
goto out;
4533+
if (floppy_alloc_disk(drive, type))
4534+
goto out;
4535+
if (add_disk(disks[drive][type]))
4536+
goto cleanup_disk;
4537+
out:
4538+
mutex_unlock(&floppy_probe_lock);
4539+
return;
4540+
4541+
cleanup_disk:
4542+
blk_cleanup_disk(disks[drive][type]);
4543+
disks[drive][type] = NULL;
45354544
mutex_unlock(&floppy_probe_lock);
45364545
}
45374546

drivers/block/loop.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1983,7 +1983,6 @@ static int loop_add(int i)
19831983
goto out_free_dev;
19841984
i = err;
19851985

1986-
err = -ENOMEM;
19871986
lo->tag_set.ops = &loop_mq_ops;
19881987
lo->tag_set.nr_hw_queues = 1;
19891988
lo->tag_set.queue_depth = 128;

drivers/block/nbd.c

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ static void nbd_dev_remove(struct nbd_device *nbd)
260260
mutex_lock(&nbd_index_mutex);
261261
idr_remove(&nbd_index_idr, nbd->index);
262262
mutex_unlock(&nbd_index_mutex);
263-
263+
destroy_workqueue(nbd->recv_workq);
264264
kfree(nbd);
265265
}
266266

@@ -755,6 +755,8 @@ static struct nbd_cmd *nbd_handle_reply(struct nbd_device *nbd, int index,
755755
if (cmd->index != index) {
756756
dev_err(disk_to_dev(nbd->disk), "Unexpected reply %d from different sock %d (expected %d)",
757757
tag, index, cmd->index);
758+
ret = -ENOENT;
759+
goto out;
758760
}
759761
if (cmd->cmd_cookie != nbd_handle_to_cookie(handle)) {
760762
dev_err(disk_to_dev(nbd->disk), "Double reply on req %p, cmd_cookie %u, handle cookie %u\n",
@@ -1314,10 +1316,6 @@ static void nbd_config_put(struct nbd_device *nbd)
13141316
kfree(nbd->config);
13151317
nbd->config = NULL;
13161318

1317-
if (nbd->recv_workq)
1318-
destroy_workqueue(nbd->recv_workq);
1319-
nbd->recv_workq = NULL;
1320-
13211319
nbd->tag_set.timeout = 0;
13221320
nbd->disk->queue->limits.discard_granularity = 0;
13231321
nbd->disk->queue->limits.discard_alignment = 0;
@@ -1346,14 +1344,6 @@ static int nbd_start_device(struct nbd_device *nbd)
13461344
return -EINVAL;
13471345
}
13481346

1349-
nbd->recv_workq = alloc_workqueue("knbd%d-recv",
1350-
WQ_MEM_RECLAIM | WQ_HIGHPRI |
1351-
WQ_UNBOUND, 0, nbd->index);
1352-
if (!nbd->recv_workq) {
1353-
dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1354-
return -ENOMEM;
1355-
}
1356-
13571347
blk_mq_update_nr_hw_queues(&nbd->tag_set, config->num_connections);
13581348
nbd->pid = task_pid_nr(current);
13591349

@@ -1779,6 +1769,15 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
17791769
}
17801770
nbd->disk = disk;
17811771

1772+
nbd->recv_workq = alloc_workqueue("nbd%d-recv",
1773+
WQ_MEM_RECLAIM | WQ_HIGHPRI |
1774+
WQ_UNBOUND, 0, nbd->index);
1775+
if (!nbd->recv_workq) {
1776+
dev_err(disk_to_dev(nbd->disk), "Could not allocate knbd recv work queue.\n");
1777+
err = -ENOMEM;
1778+
goto out_err_disk;
1779+
}
1780+
17821781
/*
17831782
* Tell the block layer that we are not a rotational device
17841783
*/
@@ -1803,13 +1802,13 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
18031802
disk->major = NBD_MAJOR;
18041803

18051804
/* Too big first_minor can cause duplicate creation of
1806-
* sysfs files/links, since first_minor will be truncated to
1807-
* byte in __device_add_disk().
1805+
* sysfs files/links, since index << part_shift might overflow, or
1806+
* MKDEV() expect that the max bits of first_minor is 20.
18081807
*/
18091808
disk->first_minor = index << part_shift;
1810-
if (disk->first_minor > 0xff) {
1809+
if (disk->first_minor < index || disk->first_minor > MINORMASK) {
18111810
err = -EINVAL;
1812-
goto out_free_idr;
1811+
goto out_free_work;
18131812
}
18141813

18151814
disk->minors = 1 << part_shift;
@@ -1818,7 +1817,7 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
18181817
sprintf(disk->disk_name, "nbd%d", index);
18191818
err = add_disk(disk);
18201819
if (err)
1821-
goto out_err_disk;
1820+
goto out_free_work;
18221821

18231822
/*
18241823
* Now publish the device.
@@ -1827,6 +1826,8 @@ static struct nbd_device *nbd_dev_add(int index, unsigned int refs)
18271826
nbd_total_devices++;
18281827
return nbd;
18291828

1829+
out_free_work:
1830+
destroy_workqueue(nbd->recv_workq);
18301831
out_err_disk:
18311832
blk_cleanup_disk(disk);
18321833
out_free_idr:
@@ -2082,13 +2083,10 @@ static void nbd_disconnect_and_put(struct nbd_device *nbd)
20822083
nbd_disconnect(nbd);
20832084
sock_shutdown(nbd);
20842085
/*
2085-
* Make sure recv thread has finished, so it does not drop the last
2086-
* config ref and try to destroy the workqueue from inside the work
2087-
* queue. And this also ensure that we can safely call nbd_clear_que()
2086+
* Make sure recv thread has finished, we can safely call nbd_clear_que()
20882087
* to cancel the inflight I/Os.
20892088
*/
2090-
if (nbd->recv_workq)
2091-
flush_workqueue(nbd->recv_workq);
2089+
flush_workqueue(nbd->recv_workq);
20922090
nbd_clear_que(nbd);
20932091
nbd->task_setup = NULL;
20942092
mutex_unlock(&nbd->config_lock);

drivers/block/ps3disk.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,13 @@ static int ps3disk_probe(struct ps3_system_bus_device *_dev)
467467
gendisk->disk_name, priv->model, priv->raw_capacity >> 11,
468468
get_capacity(gendisk) >> 11);
469469

470-
device_add_disk(&dev->sbd.core, gendisk, NULL);
471-
return 0;
470+
error = device_add_disk(&dev->sbd.core, gendisk, NULL);
471+
if (error)
472+
goto fail_cleanup_disk;
472473

474+
return 0;
475+
fail_cleanup_disk:
476+
blk_cleanup_disk(gendisk);
473477
fail_free_tag_set:
474478
blk_mq_free_tag_set(&priv->tag_set);
475479
fail_teardown:

drivers/block/ps3vram.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,9 +753,14 @@ static int ps3vram_probe(struct ps3_system_bus_device *dev)
753753
dev_info(&dev->core, "%s: Using %llu MiB of GPU memory\n",
754754
gendisk->disk_name, get_capacity(gendisk) >> 11);
755755

756-
device_add_disk(&dev->core, gendisk, NULL);
756+
error = device_add_disk(&dev->core, gendisk, NULL);
757+
if (error)
758+
goto out_cleanup_disk;
759+
757760
return 0;
758761

762+
out_cleanup_disk:
763+
blk_cleanup_disk(gendisk);
759764
out_cache_cleanup:
760765
remove_proc_entry(DEVICE_NAME, NULL);
761766
ps3vram_cache_cleanup(dev);

drivers/block/sunvdc.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,8 @@ static int probe_disk(struct vdc_port *port)
826826
if (IS_ERR(g)) {
827827
printk(KERN_ERR PFX "%s: Could not allocate gendisk.\n",
828828
port->vio.name);
829-
blk_mq_free_tag_set(&port->tag_set);
830-
return PTR_ERR(g);
829+
err = PTR_ERR(g);
830+
goto out_free_tag;
831831
}
832832

833833
port->disk = g;
@@ -879,9 +879,17 @@ static int probe_disk(struct vdc_port *port)
879879
port->vdisk_size, (port->vdisk_size >> (20 - 9)),
880880
port->vio.ver.major, port->vio.ver.minor);
881881

882-
device_add_disk(&port->vio.vdev->dev, g, NULL);
882+
err = device_add_disk(&port->vio.vdev->dev, g, NULL);
883+
if (err)
884+
goto out_cleanup_disk;
883885

884886
return 0;
887+
888+
out_cleanup_disk:
889+
blk_cleanup_disk(g);
890+
out_free_tag:
891+
blk_mq_free_tag_set(&port->tag_set);
892+
return err;
885893
}
886894

887895
static struct ldc_channel_config vdc_ldc_cfg = {

0 commit comments

Comments
 (0)