Skip to content

Commit 24b45e6

Browse files
Christoph Hellwigaxboe
authored andcommitted
virtio_blk: simplify refcounting
Implement the ->free_disk method to free the virtio_blk structure only once the last gendisk reference goes away instead of keeping a local refcount. Signed-off-by: Christoph Hellwig <hch@lst.de> Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Link: https://lore.kernel.org/r/20220215094514.3828912-6-hch@lst.de Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 185ed42 commit 24b45e6

1 file changed

Lines changed: 14 additions & 52 deletions

File tree

drivers/block/virtio_blk.c

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,6 @@ struct virtio_blk {
6969
/* Process context for config space updates */
7070
struct work_struct config_work;
7171

72-
/*
73-
* Tracks references from block_device_operations open/release and
74-
* virtio_driver probe/remove so this object can be freed once no
75-
* longer in use.
76-
*/
77-
refcount_t refs;
78-
7972
/* What host tells us, plus 2 for header & tailer. */
8073
unsigned int sg_elems;
8174

@@ -391,43 +384,6 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)
391384
return err;
392385
}
393386

394-
static void virtblk_get(struct virtio_blk *vblk)
395-
{
396-
refcount_inc(&vblk->refs);
397-
}
398-
399-
static void virtblk_put(struct virtio_blk *vblk)
400-
{
401-
if (refcount_dec_and_test(&vblk->refs)) {
402-
ida_simple_remove(&vd_index_ida, vblk->index);
403-
mutex_destroy(&vblk->vdev_mutex);
404-
kfree(vblk);
405-
}
406-
}
407-
408-
static int virtblk_open(struct block_device *bd, fmode_t mode)
409-
{
410-
struct virtio_blk *vblk = bd->bd_disk->private_data;
411-
int ret = 0;
412-
413-
mutex_lock(&vblk->vdev_mutex);
414-
415-
if (vblk->vdev)
416-
virtblk_get(vblk);
417-
else
418-
ret = -ENXIO;
419-
420-
mutex_unlock(&vblk->vdev_mutex);
421-
return ret;
422-
}
423-
424-
static void virtblk_release(struct gendisk *disk, fmode_t mode)
425-
{
426-
struct virtio_blk *vblk = disk->private_data;
427-
428-
virtblk_put(vblk);
429-
}
430-
431387
/* We provide getgeo only to please some old bootloader/partitioning tools */
432388
static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
433389
{
@@ -460,11 +416,19 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
460416
return ret;
461417
}
462418

419+
static void virtblk_free_disk(struct gendisk *disk)
420+
{
421+
struct virtio_blk *vblk = disk->private_data;
422+
423+
ida_simple_remove(&vd_index_ida, vblk->index);
424+
mutex_destroy(&vblk->vdev_mutex);
425+
kfree(vblk);
426+
}
427+
463428
static const struct block_device_operations virtblk_fops = {
464-
.owner = THIS_MODULE,
465-
.open = virtblk_open,
466-
.release = virtblk_release,
467-
.getgeo = virtblk_getgeo,
429+
.owner = THIS_MODULE,
430+
.getgeo = virtblk_getgeo,
431+
.free_disk = virtblk_free_disk,
468432
};
469433

470434
static int index_to_minor(int index)
@@ -791,8 +755,6 @@ static int virtblk_probe(struct virtio_device *vdev)
791755
goto out_free_index;
792756
}
793757

794-
/* This reference is dropped in virtblk_remove(). */
795-
refcount_set(&vblk->refs, 1);
796758
mutex_init(&vblk->vdev_mutex);
797759

798760
vblk->vdev = vdev;
@@ -970,7 +932,7 @@ static void virtblk_remove(struct virtio_device *vdev)
970932
flush_work(&vblk->config_work);
971933

972934
del_gendisk(vblk->disk);
973-
blk_cleanup_disk(vblk->disk);
935+
blk_cleanup_queue(vblk->disk->queue);
974936
blk_mq_free_tag_set(&vblk->tag_set);
975937

976938
mutex_lock(&vblk->vdev_mutex);
@@ -986,7 +948,7 @@ static void virtblk_remove(struct virtio_device *vdev)
986948

987949
mutex_unlock(&vblk->vdev_mutex);
988950

989-
virtblk_put(vblk);
951+
put_disk(vblk->disk);
990952
}
991953

992954
#ifdef CONFIG_PM_SLEEP

0 commit comments

Comments
 (0)