Skip to content

Commit b7421af

Browse files
mcgrofaxboe
authored andcommitted
nvdimm/blk: avoid calling del_gendisk() on early failures
If nd_integrity_init() fails we'd get del_gendisk() called, but that's not correct as we should only call that if we're done with device_add_disk(). Fix this by providing unwinding prior to the devm call being registered and moving the devm registration to the very end. This should fix calling del_gendisk() if nd_integrity_init() fails. I only spotted this issue through code inspection. It does not fix any real world bug. Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Luis Chamberlain <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 16be797 commit b7421af

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

drivers/nvdimm/blk.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
239239
resource_size_t available_disk_size;
240240
struct gendisk *disk;
241241
u64 internal_nlba;
242+
int rc;
242243

243244
internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
244245
available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
@@ -255,20 +256,26 @@ static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
255256
blk_queue_logical_block_size(disk->queue, nsblk_sector_size(nsblk));
256257
blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
257258

258-
if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
259-
return -ENOMEM;
260-
261259
if (nsblk_meta_size(nsblk)) {
262-
int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
260+
rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
263261

264262
if (rc)
265-
return rc;
263+
goto out_before_devm_err;
266264
}
267265

268266
set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
269267
device_add_disk(dev, disk, NULL);
268+
269+
/* nd_blk_release_disk() is called if this fails */
270+
if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
271+
return -ENOMEM;
272+
270273
nvdimm_check_and_set_ro(disk);
271274
return 0;
275+
276+
out_before_devm_err:
277+
blk_cleanup_disk(disk);
278+
return rc;
272279
}
273280

274281
static int nd_blk_probe(struct device *dev)

0 commit comments

Comments
 (0)