virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCHv4] virtio-blk: use ida to allocate disk index
@ 2011-10-30 19:29 Michael S. Tsirkin
  2011-10-31  7:05 ` Jens Axboe
       [not found] ` <4EAE48D3.4090504@kernel.dk>
  0 siblings, 2 replies; 3+ messages in thread
From: Michael S. Tsirkin @ 2011-10-30 19:29 UTC (permalink / raw)
  To: Jens Axboe
  Cc: kvm, Greg Kroah-Hartman, linux-kernel, virtualization, Tejun Heo

Based on a patch by Mark Wu <dwu@redhat.com>

Current index allocation in virtio-blk is based on a monotonically
increasing variable "index". This means we'll run out of numbers
after a while.  It also could cause confusion about the disk
name in the case of hot-plugging disks.
Change virtio-blk to use ida to allocate index, instead.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---

Changes from Mark's versions:
 use the new ida_simple_get
 error handling cleanup
 fix user after free

Works fine for me.

Jens, could you merge this for 3.2?
That is, unless Rusty complains shortly ...

 drivers/block/virtio_blk.c |   30 ++++++++++++++++++++++++------
 1 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 079c088..e7a5750 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -8,10 +8,13 @@
 #include <linux/scatterlist.h>
 #include <linux/string_helpers.h>
 #include <scsi/scsi_cmnd.h>
+#include <linux/idr.h>
 
 #define PART_BITS 4
 
-static int major, index;
+static int major;
+static DEFINE_IDA(vd_index_ida);
+
 struct workqueue_struct *virtblk_wq;
 
 struct virtio_blk
@@ -35,6 +38,9 @@ struct virtio_blk
 	/* What host tells us, plus 2 for header & tailer. */
 	unsigned int sg_elems;
 
+	/* Ida index - used to track minor number allocations. */
+	int index;
+
 	/* Scatterlist: can be too big for stack. */
 	struct scatterlist sg[/*sg_elems*/];
 };
@@ -276,6 +282,11 @@ static int index_to_minor(int index)
 	return index << PART_BITS;
 }
 
+static int minor_to_index(int minor)
+{
+	return minor >> PART_BITS;
+}
+
 static ssize_t virtblk_serial_show(struct device *dev,
 				struct device_attribute *attr, char *buf)
 {
@@ -341,14 +352,17 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk;
 	struct request_queue *q;
-	int err;
+	int err, index;
 	u64 cap;
 	u32 v, blk_size, sg_elems, opt_io_size;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	err = ida_simple_get(&vd_index_ida, 0, minor_to_index(1 << MINORBITS),
+			     GFP_KERNEL);
+	if (err < 0)
+		goto out;
+	index = err;
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -365,7 +379,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 				    sizeof(vblk->sg[0]) * sg_elems, GFP_KERNEL);
 	if (!vblk) {
 		err = -ENOMEM;
-		goto out;
+		goto out_free_index;
 	}
 
 	INIT_LIST_HEAD(&vblk->reqs);
@@ -421,7 +435,7 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	vblk->disk->private_data = vblk;
 	vblk->disk->fops = &virtblk_fops;
 	vblk->disk->driverfs_dev = &vdev->dev;
-	index++;
+	vblk->index = index;
 
 	/* configure queue flush support */
 	if (virtio_has_feature(vdev, VIRTIO_BLK_F_FLUSH))
@@ -516,6 +530,8 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	ida_simple_remove(&vd_index_ida, index);
 out:
 	return err;
 }
@@ -523,6 +539,7 @@ out:
 static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
+	int index = vblk->index;
 
 	flush_work(&vblk->config_work);
 
@@ -538,6 +555,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
+	ida_simple_remove(&vd_index_ida, index);
 }
 
 static const struct virtio_device_id id_table[] = {
-- 
1.7.5.53.gc233e

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCHv4] virtio-blk: use ida to allocate disk index
  2011-10-30 19:29 [PATCHv4] virtio-blk: use ida to allocate disk index Michael S. Tsirkin
@ 2011-10-31  7:05 ` Jens Axboe
       [not found] ` <4EAE48D3.4090504@kernel.dk>
  1 sibling, 0 replies; 3+ messages in thread
From: Jens Axboe @ 2011-10-31  7:05 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: kvm, Greg Kroah-Hartman, linux-kernel, virtualization, Tejun Heo

On 2011-10-30 20:29, Michael S. Tsirkin wrote:
> Based on a patch by Mark Wu <dwu@redhat.com>
> 
> Current index allocation in virtio-blk is based on a monotonically
> increasing variable "index". This means we'll run out of numbers
> after a while.  It also could cause confusion about the disk
> name in the case of hot-plugging disks.
> Change virtio-blk to use ida to allocate index, instead.
> 
> Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> ---
> 
> Changes from Mark's versions:
>  use the new ida_simple_get
>  error handling cleanup
>  fix user after free
> 
> Works fine for me.
> 
> Jens, could you merge this for 3.2?
> That is, unless Rusty complains shortly ...

Yep, tentatively added.

-- 
Jens Axboe

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCHv4] virtio-blk: use ida to allocate disk index
       [not found] ` <4EAE48D3.4090504@kernel.dk>
@ 2011-10-31 23:55   ` Rusty Russell
  0 siblings, 0 replies; 3+ messages in thread
From: Rusty Russell @ 2011-10-31 23:55 UTC (permalink / raw)
  To: Jens Axboe, Michael S. Tsirkin
  Cc: Tejun Heo, Greg Kroah-Hartman, linux-kernel, kvm, virtualization

On Mon, 31 Oct 2011 08:05:55 +0100, Jens Axboe <axboe@kernel.dk> wrote:
> On 2011-10-30 20:29, Michael S. Tsirkin wrote:
> > Based on a patch by Mark Wu <dwu@redhat.com>
> > 
> > Current index allocation in virtio-blk is based on a monotonically
> > increasing variable "index". This means we'll run out of numbers
> > after a while.  It also could cause confusion about the disk
> > name in the case of hot-plugging disks.
> > Change virtio-blk to use ida to allocate index, instead.
> > 
> > Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
> > ---
> > 
> > Changes from Mark's versions:
> >  use the new ida_simple_get
> >  error handling cleanup
> >  fix user after free
> > 
> > Works fine for me.
> > 
> > Jens, could you merge this for 3.2?
> > That is, unless Rusty complains shortly ...
> 
> Yep, tentatively added.

Thanks!

Acked-by: Rusty Russell <rusty@rustcorp.com.au>

Cheers,
Rusty.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-10-31 23:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-30 19:29 [PATCHv4] virtio-blk: use ida to allocate disk index Michael S. Tsirkin
2011-10-31  7:05 ` Jens Axboe
     [not found] ` <4EAE48D3.4090504@kernel.dk>
2011-10-31 23:55   ` Rusty Russell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).