Commit 24b45e6c authored by Christoph Hellwig's avatar Christoph Hellwig Committed by Jens Axboe

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: default avatarChristoph Hellwig <hch@lst.de>
Reviewed-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
Acked-by: default avatarMichael S. Tsirkin <mst@redhat.com>
Link: https://lore.kernel.org/r/20220215094514.3828912-6-hch@lst.deSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 185ed423
...@@ -69,13 +69,6 @@ struct virtio_blk { ...@@ -69,13 +69,6 @@ struct virtio_blk {
/* Process context for config space updates */ /* Process context for config space updates */
struct work_struct config_work; struct work_struct config_work;
/*
* Tracks references from block_device_operations open/release and
* virtio_driver probe/remove so this object can be freed once no
* longer in use.
*/
refcount_t refs;
/* What host tells us, plus 2 for header & tailer. */ /* What host tells us, plus 2 for header & tailer. */
unsigned int sg_elems; unsigned int sg_elems;
...@@ -391,43 +384,6 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str) ...@@ -391,43 +384,6 @@ static int virtblk_get_id(struct gendisk *disk, char *id_str)
return err; return err;
} }
static void virtblk_get(struct virtio_blk *vblk)
{
refcount_inc(&vblk->refs);
}
static void virtblk_put(struct virtio_blk *vblk)
{
if (refcount_dec_and_test(&vblk->refs)) {
ida_simple_remove(&vd_index_ida, vblk->index);
mutex_destroy(&vblk->vdev_mutex);
kfree(vblk);
}
}
static int virtblk_open(struct block_device *bd, fmode_t mode)
{
struct virtio_blk *vblk = bd->bd_disk->private_data;
int ret = 0;
mutex_lock(&vblk->vdev_mutex);
if (vblk->vdev)
virtblk_get(vblk);
else
ret = -ENXIO;
mutex_unlock(&vblk->vdev_mutex);
return ret;
}
static void virtblk_release(struct gendisk *disk, fmode_t mode)
{
struct virtio_blk *vblk = disk->private_data;
virtblk_put(vblk);
}
/* We provide getgeo only to please some old bootloader/partitioning tools */ /* We provide getgeo only to please some old bootloader/partitioning tools */
static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
{ {
...@@ -460,11 +416,19 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo) ...@@ -460,11 +416,19 @@ static int virtblk_getgeo(struct block_device *bd, struct hd_geometry *geo)
return ret; return ret;
} }
static void virtblk_free_disk(struct gendisk *disk)
{
struct virtio_blk *vblk = disk->private_data;
ida_simple_remove(&vd_index_ida, vblk->index);
mutex_destroy(&vblk->vdev_mutex);
kfree(vblk);
}
static const struct block_device_operations virtblk_fops = { static const struct block_device_operations virtblk_fops = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.open = virtblk_open,
.release = virtblk_release,
.getgeo = virtblk_getgeo, .getgeo = virtblk_getgeo,
.free_disk = virtblk_free_disk,
}; };
static int index_to_minor(int index) static int index_to_minor(int index)
...@@ -791,8 +755,6 @@ static int virtblk_probe(struct virtio_device *vdev) ...@@ -791,8 +755,6 @@ static int virtblk_probe(struct virtio_device *vdev)
goto out_free_index; goto out_free_index;
} }
/* This reference is dropped in virtblk_remove(). */
refcount_set(&vblk->refs, 1);
mutex_init(&vblk->vdev_mutex); mutex_init(&vblk->vdev_mutex);
vblk->vdev = vdev; vblk->vdev = vdev;
...@@ -970,7 +932,7 @@ static void virtblk_remove(struct virtio_device *vdev) ...@@ -970,7 +932,7 @@ static void virtblk_remove(struct virtio_device *vdev)
flush_work(&vblk->config_work); flush_work(&vblk->config_work);
del_gendisk(vblk->disk); del_gendisk(vblk->disk);
blk_cleanup_disk(vblk->disk); blk_cleanup_queue(vblk->disk->queue);
blk_mq_free_tag_set(&vblk->tag_set); blk_mq_free_tag_set(&vblk->tag_set);
mutex_lock(&vblk->vdev_mutex); mutex_lock(&vblk->vdev_mutex);
...@@ -986,7 +948,7 @@ static void virtblk_remove(struct virtio_device *vdev) ...@@ -986,7 +948,7 @@ static void virtblk_remove(struct virtio_device *vdev)
mutex_unlock(&vblk->vdev_mutex); mutex_unlock(&vblk->vdev_mutex);
virtblk_put(vblk); put_disk(vblk->disk);
} }
#ifdef CONFIG_PM_SLEEP #ifdef CONFIG_PM_SLEEP
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment