Skip to contentSkip to conversation

virtio-blk: Use ida to allocate disk index

PATCH In review Mark Wu 6 participants 33 messages Jun 1, 2011 – Oct 25, 2011

Subsystems (6)
lore
Share & tools
lore

Mark Wu

author
#0 lore raw

PATCH 1/1 virtio-blk: Use ida to allocate disk index

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

---
 drivers/block/virtio_blk.c |   37 ++++++++++++++++++++++++++++++++-----
 1 files changed, 32 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +32 -5
index 079c088..ba734b3 100644
@@ -8,10 +8,14 @@
 #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_SPINLOCK(vd_index_lock);
+static DEFINE_IDA(vd_index_ida);
+
 struct workqueue_struct *virtblk_wq;
 
 struct virtio_blk
@@ -23,6 +27,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +348,26 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+
+		spin_lock(&vd_index_lock);
+		err = ida_get_new(&vd_index_ida, &index);
+		spin_unlock(&vd_index_lock);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +440,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 +535,10 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, index);
+	spin_unlock(&vd_index_lock);
 out:
 	return err;
 }
@@ -529,6 +552,10 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 	/* Nothing should be pending. */
 	BUG_ON(!list_empty(&vblk->reqs));
 
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, vblk->index);
+	spin_unlock(&vd_index_lock);
+
 	/* Stop all the virtqueues. */
 	vdev->config->reset(vdev);
 
Signature · 1 line
1.7.1
author
#1 lore raw

drivers/block/virtio_blk.c static int __devinit virtblk_probe(struct virtio_device *vdev) · line 358

Quoted 6 lines · Mark Wu
drivers/block/virtio_blk.c +4 -2
-	if (index_to_minor(index)>= 1<<  MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+

There's a problem in above code: err is not initialized before using, so change it to return -1;
+ do {
+ if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+ return -1;

author
#6 lore raw
In reply to Mark Wu

drivers/block/virtio_blk.c static int __devinit virtblk_probe(struct virtio_device *vdev) · line 358

Quoted 6 lines · Mark Wu
drivers/block/virtio_blk.c +4 -2
-	if (index_to_minor(index)>= 1<<  MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+

There's a problem in above code: err is not initialized before using, so change it to return -1;
+ do {
+ if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+ return -1;

#7 lore raw
In reply to Mark Wu
Quoted 8 lines · Mark Wu

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

Hi Mark,

I don't believe that we do disk probes in parallel, so the spinlock
is unnecessary. Otherwise, this looks good.

Thanks,
Rusty.

#30 lore raw
In reply to Mark Wu
Quoted 8 lines · Mark Wu

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

Hi Mark,

I don't believe that we do disk probes in parallel, so the spinlock
is unnecessary. Otherwise, this looks good.

Thanks,
Rusty.

#31 lore raw
In reply to Mark Wu
Quoted 91 lines · Mark Wu

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

---
 drivers/block/virtio_blk.c |   37 ++++++++++++++++++++++++++++++++-----
 1 files changed, 32 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +28 -5
index 079c088..ba734b3 100644
@@ -8,10 +8,14 @@
 #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_SPINLOCK(vd_index_lock);
+static DEFINE_IDA(vd_index_ida);
+
 struct workqueue_struct *virtblk_wq;
 
 struct virtio_blk
@@ -23,6 +27,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +348,26 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+
+		spin_lock(&vd_index_lock);
+		err = ida_get_new(&vd_index_ida, &index);
+		spin_unlock(&vd_index_lock);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +440,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 +535,10 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, index);
+	spin_unlock(&vd_index_lock);
 out:
 	return err;
 }

drivers/block/virtio_blk.c static void __devexit virtblk_remove(struct virtio_device *vdev) · line 560

Quoted 9 lines
drivers/block/virtio_blk.c +4 -0
@@ -529,6 +552,10 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 	/* Nothing should be pending. */
 	BUG_ON(!list_empty(&vblk->reqs));
 
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, vblk->index);
+	spin_unlock(&vd_index_lock);
+
 	/* Stop all the virtqueues. */
 	vdev->config->reset(vdev);

As we get index first thing in _probe, let's remove last thing in _remove.

I'm not sure violating the rule of cleanup
in the reverse order of initialization can lead
to problems here, but it's better to stick to this rule regardless, IMO.

Quoted 2 lines
Signature · 1 line
1.7.1
#32 lore raw
In reply to Mark Wu
Quoted 91 lines · Mark Wu

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

---
 drivers/block/virtio_blk.c |   37 ++++++++++++++++++++++++++++++++-----
 1 files changed, 32 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +28 -5
index 079c088..ba734b3 100644
@@ -8,10 +8,14 @@
 #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_SPINLOCK(vd_index_lock);
+static DEFINE_IDA(vd_index_ida);
+
 struct workqueue_struct *virtblk_wq;
 
 struct virtio_blk
@@ -23,6 +27,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +348,26 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+
+		spin_lock(&vd_index_lock);
+		err = ida_get_new(&vd_index_ida, &index);
+		spin_unlock(&vd_index_lock);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +440,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 +535,10 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, index);
+	spin_unlock(&vd_index_lock);
 out:
 	return err;
 }

drivers/block/virtio_blk.c static void __devexit virtblk_remove(struct virtio_device *vdev) · line 560

Quoted 9 lines
drivers/block/virtio_blk.c +4 -0
@@ -529,6 +552,10 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 	/* Nothing should be pending. */
 	BUG_ON(!list_empty(&vblk->reqs));
 
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, vblk->index);
+	spin_unlock(&vd_index_lock);
+
 	/* Stop all the virtqueues. */
 	vdev->config->reset(vdev);

As we get index first thing in _probe, let's remove last thing in _remove.

I'm not sure violating the rule of cleanup
in the reverse order of initialization can lead
to problems here, but it's better to stick to this rule regardless, IMO.

Quoted 2 lines
Signature · 1 line
1.7.1
#2 lore raw
In reply to Mark Wu

drivers/block/virtio_blk.c static int __devinit virtblk_probe(struct virtio_device *vdev)

Quoted 12 lines · Mark Wu

drivers/block/virtio_blk.c static int __devinit virtblk_probe(struct virtio_device *vdev) · line 358

Quoted 6 lines · Mark Wu
drivers/block/virtio_blk.c +4 -2
-	if (index_to_minor(index)>= 1<<  MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+

There's a problem in above code: err is not initialized before using, so change it to return -1;

drivers/block/virtio_blk.c +3 -0
+       do {
+               if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+                       return -1;

Not -1. Pls return -ENOMEM.

#5 lore raw
In reply to Mark Wu

drivers/block/virtio_blk.c static int __devinit virtblk_probe(struct virtio_device *vdev)

Quoted 12 lines · Mark Wu

drivers/block/virtio_blk.c static int __devinit virtblk_probe(struct virtio_device *vdev) · line 358

Quoted 6 lines · Mark Wu
drivers/block/virtio_blk.c +4 -2
-	if (index_to_minor(index)>= 1<<  MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+

There's a problem in above code: err is not initialized before using, so change it to return -1;

drivers/block/virtio_blk.c +3 -0
+       do {
+               if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+                       return -1;

Not -1. Pls return -ENOMEM.

author
#8 lore raw
In reply to Rusty Russell
Quoted 8 lines · Rusty Russell
Quoted 7 lines · Mark Wu

Current index allocation in virtio-blk is based on a monotonically
increasing variable "index". It could cause some confusion about
disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

Quoted 8 lines
Quoted 1 line

Hi Mark,

I don't believe that we do disk probes in parallel, so the spinlock is unnecessary. Otherwise, this looks good.

Thanks, Rusty.

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

author
#29 lore raw
In reply to Rusty Russell
Quoted 8 lines · Rusty Russell
Quoted 7 lines · Mark Wu

Current index allocation in virtio-blk is based on a monotonically
increasing variable "index". It could cause some confusion about
disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

Quoted 8 lines
Quoted 1 line

Hi Mark,

I don't believe that we do disk probes in parallel, so the spinlock is unnecessary. Otherwise, this looks good.

Thanks, Rusty.

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

author
#3 lore raw
In reply to Michael S. Tsirkin
Quoted 14 lines · Michael S. Tsirkin

Review

Quoted 12 lines · Mark Wu

Review

Quoted 6 lines · Mark Wu
(quoted diff) +4 -2
-	if (index_to_minor(index)>= 1<<  MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+

There's a problem in above code: err is not initialized before using, so change it to return -1;

(quoted diff) +3 -0
+       do {
+               if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+                       return -1;
Quoted 6 lines

Not -1. Pls return -ENOMEM.

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Hi Michael,
Thanks for pointing out that. This is the revised patch.

Quoted 1 line

From ffe49efd20938952a09d5a87fe694a6f62937756 Mon Sep 17 00:00:00 2001

From: Mark Wu <[email protected]>
Date: Wed, 8 Jun 2011 08:25:53 -0400
Subject: [PATCH 1/1] [virt] virtio-blk: Use ida to allocate disk index

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

---
 drivers/block/virtio_blk.c |   37 ++++++++++++++++++++++++++++++++-----
 1 files changed, 32 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +32 -5
index 079c088..f13b758 100644
@@ -8,10 +8,14 @@
 #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_SPINLOCK(vd_index_lock);
+static DEFINE_IDA(vd_index_ida);
+
 struct workqueue_struct *virtblk_wq;
 
 struct virtio_blk
@@ -23,6 +27,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +348,26 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;
+
+		spin_lock(&vd_index_lock);
+		err = ida_get_new(&vd_index_ida, &index);
+		spin_unlock(&vd_index_lock);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +440,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 +535,10 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, index);
+	spin_unlock(&vd_index_lock);
 out:
 	return err;
 }
@@ -538,6 +561,10 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
+
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, vblk->index);
+	spin_unlock(&vd_index_lock);
 }
 
 static const struct virtio_device_id id_table[] = {
Signature · 1 line
1.7.1
author
#4 lore raw
In reply to Michael S. Tsirkin
Quoted 14 lines · Michael S. Tsirkin

Review

Quoted 12 lines · Mark Wu

Review

Quoted 6 lines · Mark Wu
(quoted diff) +4 -2
-	if (index_to_minor(index)>= 1<<  MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return err;
+

There's a problem in above code: err is not initialized before using, so change it to return -1;

(quoted diff) +3 -0
+       do {
+               if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+                       return -1;
Quoted 6 lines

Not -1. Pls return -ENOMEM.

--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Hi Michael,
Thanks for pointing out that. This is the revised patch.

From ffe49efd20938952a09d5a87fe694a6f62937756 Mon Sep 17 00:00:00 2001
From: Mark Wu <[email protected]>
Date: Wed, 8 Jun 2011 08:25:53 -0400
Subject: [PATCH 1/1] [virt] virtio-blk: Use ida to allocate disk index

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

---
 drivers/block/virtio_blk.c |   37 ++++++++++++++++++++++++++++++++-----
 1 files changed, 32 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +32 -5
index 079c088..f13b758 100644
@@ -8,10 +8,14 @@
 #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_SPINLOCK(vd_index_lock);
+static DEFINE_IDA(vd_index_ida);
+
 struct workqueue_struct *virtblk_wq;
 
 struct virtio_blk
@@ -23,6 +27,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +348,26 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;
+
+		spin_lock(&vd_index_lock);
+		err = ida_get_new(&vd_index_ida, &index);
+		spin_unlock(&vd_index_lock);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +440,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 +535,10 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, index);
+	spin_unlock(&vd_index_lock);
 out:
 	return err;
 }
@@ -538,6 +561,10 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
+
+	spin_lock(&vd_index_lock);
+	ida_remove(&vd_index_ida, vblk->index);
+	spin_unlock(&vd_index_lock);
 }
 
 static const struct virtio_device_id id_table[] = {
Signature · 1 line
1.7.1
#9 lore raw
In reply to Mark Wu
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Thanks,
Rusty.

#10 lore raw
In reply to Mark Wu
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Thanks,
Rusty.

#11 lore raw
Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

I know we've tried it in the past, at the PCI device level, and ran into some issues, but I don't remember if that code ever made it into the mainline kernel or not.

greg k-h

#12 lore raw
In reply to Rusty Russell
Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

I know we've tried it in the past, at the PCI device level, and ran into some issues, but I don't remember if that code ever made it into the mainline kernel or not.

greg k-h

#13 lore raw
In reply to Rusty Russell

Hello,

Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Unless async is explicitly used, probe happens sequentially. IOW, if there's no async_schedule() call, things won't happen in parallel. That said, I think it wouldn't be such a bad idea to protect ida with spinlock regardless unless the probe code explicitly requires serialization.

Thanks.

Signature · 1 line
tejun
#28 lore raw
In reply to Rusty Russell

Hello,

Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Unless async is explicitly used, probe happens sequentially. IOW, if there's no async_schedule() call, things won't happen in parallel. That said, I think it wouldn't be such a bad idea to protect ida with spinlock regardless unless the probe code explicitly requires serialization.

Thanks.

Signature · 1 line
tejun
author
#14 lore raw
In reply to Tejun Heo
Quoted 25 lines · Tejun Heo

Hello,

Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Quoted 8 lines

Unless async is explicitly used, probe happens sequentially. IOW, if there's no async_schedule() call, things won't happen in parallel. That said, I think it wouldn't be such a bad idea to protect ida with spinlock regardless unless the probe code explicitly requires serialization.

Thanks.

Since virtio blk driver doesn't use async probe, it needn't use spinlock to protect ida. So remove the lock from patch.

Quoted 1 line

From fbb396df9dbf8023f1b268be01b43529a3993d57 Mon Sep 17 00:00:00 2001

From: Mark Wu <[email protected]>
Date: Thu, 9 Jun 2011 06:34:07 -0400
Subject: [PATCH 1/1] [virt] virtio-blk: Use ida to allocate disk index

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

---
 drivers/block/virtio_blk.c |   28 +++++++++++++++++++++++-----
 1 files changed, 23 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +23 -5
index 079c088..bf81ab6 100644
@@ -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
@@ -23,6 +26,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +347,23 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;
+		err = ida_get_new(&vd_index_ida, &index);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +436,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 +531,8 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	ida_remove(&vd_index_ida, index);
 out:
 	return err;
 }
@@ -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_remove(&vd_index_ida, vblk->index);
 }
 
 static const struct virtio_device_id id_table[] = {
Signature · 1 line
1.7.1
author
#27 lore raw
In reply to Tejun Heo
Quoted 25 lines · Tejun Heo

Hello,

Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Quoted 8 lines

Unless async is explicitly used, probe happens sequentially. IOW, if there's no async_schedule() call, things won't happen in parallel. That said, I think it wouldn't be such a bad idea to protect ida with spinlock regardless unless the probe code explicitly requires serialization.

Thanks.

Since virtio blk driver doesn't use async probe, it needn't use spinlock to protect ida. So remove the lock from patch.

From fbb396df9dbf8023f1b268be01b43529a3993d57 Mon Sep 17 00:00:00 2001
From: Mark Wu <[email protected]>
Date: Thu, 9 Jun 2011 06:34:07 -0400
Subject: [PATCH 1/1] [virt] virtio-blk: Use ida to allocate disk index

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

---
 drivers/block/virtio_blk.c |   28 +++++++++++++++++++++++-----
 1 files changed, 23 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +23 -5
index 079c088..bf81ab6 100644
@@ -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
@@ -23,6 +26,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +347,23 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;
+		err = ida_get_new(&vd_index_ida, &index);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +436,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 +531,8 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	ida_remove(&vd_index_ida, index);
 out:
 	return err;
 }
@@ -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_remove(&vd_index_ida, vblk->index);
 }
 
 static const struct virtio_device_id id_table[] = {
Signature · 1 line
1.7.1
#15 lore raw
In reply to Mark Wu
Quoted 2 lines

Since virtio blk driver doesn't use async probe, it needn't use spinlock to protect ida. So remove the lock from patch.

OK, that's fine, but:

Quoted 5 lines
(quoted diff) +3 -2
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		err = ida_get_new(&vd_index_ida, &index);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}

Is this *really* how this is supposed to be used?

Tejun, this is your code. What do you think of something like this? (untested)

Subject: ida: Simplified functions for id allocation.

The current hyper-optimized functions are overkill if you simply want to allocate an id for a device. Create versions which use an internal lock.

include/linux/idr.h +3 -0
@@ -146,6 +146,9 @@ void ida_remove(struct ida *ida, int id)
 void ida_destroy(struct ida *ida);
 void ida_init(struct ida *ida);
 
+int ida_simple_get(struct ida *ida, int min_id, int max_id);
+void ida_simple_remove(struct ida *ida, int id);
+
 void __init idr_init_cache(void);
 
 #endif /* __IDR_H__ */
lib/idr.c +48 -0

62 lines not on this page · raw message

#16 lore raw
In reply to Mark Wu
Quoted 2 lines

Since virtio blk driver doesn't use async probe, it needn't use spinlock to protect ida. So remove the lock from patch.

OK, that's fine, but:

Quoted 5 lines
(quoted diff) +3 -2
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		err = ida_get_new(&vd_index_ida, &index);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}

Is this *really* how this is supposed to be used?

Tejun, this is your code. What do you think of something like this? (untested)

Subject: ida: Simplified functions for id allocation.

The current hyper-optimized functions are overkill if you simply want to allocate an id for a device. Create versions which use an internal lock.

include/linux/idr.h +3 -0
@@ -146,6 +146,9 @@ void ida_remove(struct ida *ida, int id)
 void ida_destroy(struct ida *ida);
 void ida_init(struct ida *ida);
 
+int ida_simple_get(struct ida *ida, int min_id, int max_id);
+void ida_simple_remove(struct ida *ida, int id);
+
 void __init idr_init_cache(void);
 
 #endif /* __IDR_H__ */
lib/idr.c +48 -0

62 lines not on this page · raw message

#17 lore raw

Hello,

Quoted 9 lines · Rusty Russell

Review

Quoted 4 lines
(quoted diff) +4 -0
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}

Is this *really* how this is supposed to be used?

Tejun, this is your code. What do you think of something like this? (untested)

It's inherited from idr which was designed to have separate prepare/allocation stages so that allocation can happen inside an outer spinlock. It doesn't have too much to do with optimization. It's mostly to be able to use sleepable context for memory allocation while allowing atomic id[ra] allocation.

Quoted 4 lines

/**
+ * ida_simple_get - get a new id.
+ * @ida: the (initialized) ida.
+ * @min_id: the minimum id (inclusive)

Quoted 8 lines

+ * @max_id: the maximum id (inclusive)
+ *
+ * Allocates an id in the range min_id <= id <= max_id, or returns -ENOSPC.
+ * On allocation failure, returns -ENOMEM. This function can sleep.
+ *
+ * Use ida_simple_remove() to get rid of an id.
+ */
+int ida_simple_get(struct ida *ida, int min_id, int max_id)

Hmmm... new interface different from existing id[ra] style, but yeah something like the above would have made more sense from the beginning. The only thing is that isn't (begin <= range < end) more conventional form to express ranges?

Thanks.

Signature · 1 line
tejun
#22 lore raw
In reply to Rusty Russell

Hello,

Quoted 9 lines · Rusty Russell

Review

Quoted 4 lines
(quoted diff) +4 -0
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}

Is this *really* how this is supposed to be used?

Tejun, this is your code. What do you think of something like this? (untested)

It's inherited from idr which was designed to have separate prepare/allocation stages so that allocation can happen inside an outer spinlock. It doesn't have too much to do with optimization. It's mostly to be able to use sleepable context for memory allocation while allowing atomic id[ra] allocation.

Quoted 4 lines

/**
+ * ida_simple_get - get a new id.
+ * @ida: the (initialized) ida.
+ * @min_id: the minimum id (inclusive)

Quoted 8 lines

+ * @max_id: the maximum id (inclusive)
+ *
+ * Allocates an id in the range min_id <= id <= max_id, or returns -ENOSPC.
+ * On allocation failure, returns -ENOMEM. This function can sleep.
+ *
+ * Use ida_simple_remove() to get rid of an id.
+ */
+int ida_simple_get(struct ida *ida, int min_id, int max_id)

Hmmm... new interface different from existing id[ra] style, but yeah something like the above would have made more sense from the beginning. The only thing is that isn't (begin <= range < end) more conventional form to express ranges?

Thanks.

Signature · 1 line
tejun
#18 lore raw
In reply to Tejun Heo
Quoted 5 lines · Tejun Heo

It's inherited from idr which was designed to have separate prepare/allocation stages so that allocation can happen inside an outer spinlock. It doesn't have too much to do with optimization. It's mostly to be able to use sleepable context for memory allocation while allowing atomic id[ra] allocation.

It might have made sense for a few callers, but as a general mechanism it stinks. It's a lot of dancing to avoid GFP_ATOMIC allocations; we'd be better making idr_get_new() take a gfp_t, and have an idr_pre_alloc() for those who care.

*Sure* there's a chance of racing and we will need to do an atomic allocation. But can anyone justify the current complexity for all callers?

Quoted 12 lines
Quoted 11 lines

+ * ida_simple_get - get a new id.
+ * @ida: the (initialized) ida.
+ * @min_id: the minimum id (inclusive)
+ * @max_id: the maximum id (inclusive)
+ *
+ * Allocates an id in the range min_id <= id <= max_id, or returns -ENOSPC.
+ * On allocation failure, returns -ENOMEM. This function can sleep.
+ *
+ * Use ida_simple_remove() to get rid of an id.
+ */
+int ida_simple_get(struct ida *ida, int min_id, int max_id)

Quoted 4 lines

Hmmm... new interface different from existing id[ra] style, but yeah something like the above would have made more sense from the beginning. The only thing is that isn't (begin <= range < end) more conventional form to express ranges?

Yes, but how to express an unlimited range then? I could used unsigned and 0x80000000, but that seemed crude.

Cheers,
Rusty.

#19 lore raw
In reply to Tejun Heo
Quoted 5 lines · Tejun Heo

It's inherited from idr which was designed to have separate prepare/allocation stages so that allocation can happen inside an outer spinlock. It doesn't have too much to do with optimization. It's mostly to be able to use sleepable context for memory allocation while allowing atomic id[ra] allocation.

It might have made sense for a few callers, but as a general mechanism it stinks. It's a lot of dancing to avoid GFP_ATOMIC allocations; we'd be better making idr_get_new() take a gfp_t, and have an idr_pre_alloc() for those who care.

*Sure* there's a chance of racing and we will need to do an atomic allocation. But can anyone justify the current complexity for all callers?

Quoted 12 lines
Quoted 11 lines

+ * ida_simple_get - get a new id.
+ * @ida: the (initialized) ida.
+ * @min_id: the minimum id (inclusive)
+ * @max_id: the maximum id (inclusive)
+ *
+ * Allocates an id in the range min_id <= id <= max_id, or returns -ENOSPC.
+ * On allocation failure, returns -ENOMEM. This function can sleep.
+ *
+ * Use ida_simple_remove() to get rid of an id.
+ */
+int ida_simple_get(struct ida *ida, int min_id, int max_id)

Quoted 4 lines

Hmmm... new interface different from existing id[ra] style, but yeah something like the above would have made more sense from the beginning. The only thing is that isn't (begin <= range < end) more conventional form to express ranges?

Yes, but how to express an unlimited range then? I could used unsigned and 0x80000000, but that seemed crude.

Cheers,
Rusty.

#20 lore raw

Hello,

Quoted 7 lines · Rusty Russell
Quoted 5 lines · Tejun Heo

It's inherited from idr which was designed to have separate prepare/allocation stages so that allocation can happen inside an outer spinlock. It doesn't have too much to do with optimization. It's mostly to be able to use sleepable context for memory allocation while allowing atomic id[ra] allocation.

Quoted 8 lines

It might have made sense for a few callers, but as a general mechanism it stinks. It's a lot of dancing to avoid GFP_ATOMIC allocations; we'd be better making idr_get_new() take a gfp_t, and have an idr_pre_alloc() for those who care.

*Sure* there's a chance of racing and we will need to do an atomic allocation. But can anyone justify the current complexity for all callers?

Sure, I'm not arguing for the current interface.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

+ * ida_simple_get - get a new id.
+ * @ida: the (initialized) ida.
+ * @min_id: the minimum id (inclusive)
+ * @max_id: the maximum id (inclusive)
+ *
+ * Allocates an id in the range min_id <= id <= max_id, or returns -ENOSPC.
+ * On allocation failure, returns -ENOMEM. This function can sleep.
+ *
+ * Use ida_simple_remove() to get rid of an id.
+ */
+int ida_simple_get(struct ida *ida, int min_id, int max_id)

Quoted 7 lines
Quoted 4 lines

Hmmm... new interface different from existing id[ra] style, but yeah something like the above would have made more sense from the beginning. The only thing is that isn't (begin <= range < end) more conventional form to express ranges?

Yes, but how to express an unlimited range then? I could used unsigned and 0x80000000, but that seemed crude.

So, why not do this properly then? ie. ida_get(ida, begin, end, gfp).

As for the end of range, shouldn't 0 mean the default max range? Only some users care about the upper limit anyway. We can also make it unsigned just in case and cap the value.

Thanks.

Signature · 1 line
tejun
#21 lore raw
In reply to Rusty Russell

Hello,

Quoted 7 lines · Rusty Russell
Quoted 5 lines · Tejun Heo

It's inherited from idr which was designed to have separate prepare/allocation stages so that allocation can happen inside an outer spinlock. It doesn't have too much to do with optimization. It's mostly to be able to use sleepable context for memory allocation while allowing atomic id[ra] allocation.

Quoted 8 lines

It might have made sense for a few callers, but as a general mechanism it stinks. It's a lot of dancing to avoid GFP_ATOMIC allocations; we'd be better making idr_get_new() take a gfp_t, and have an idr_pre_alloc() for those who care.

*Sure* there's a chance of racing and we will need to do an atomic allocation. But can anyone justify the current complexity for all callers?

Sure, I'm not arguing for the current interface.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines

+ * ida_simple_get - get a new id.
+ * @ida: the (initialized) ida.
+ * @min_id: the minimum id (inclusive)
+ * @max_id: the maximum id (inclusive)
+ *
+ * Allocates an id in the range min_id <= id <= max_id, or returns -ENOSPC.
+ * On allocation failure, returns -ENOMEM. This function can sleep.
+ *
+ * Use ida_simple_remove() to get rid of an id.
+ */
+int ida_simple_get(struct ida *ida, int min_id, int max_id)

Quoted 7 lines
Quoted 4 lines

Hmmm... new interface different from existing id[ra] style, but yeah something like the above would have made more sense from the beginning. The only thing is that isn't (begin <= range < end) more conventional form to express ranges?

Yes, but how to express an unlimited range then? I could used unsigned and 0x80000000, but that seemed crude.

So, why not do this properly then? ie. ida_get(ida, begin, end, gfp).

As for the end of range, shouldn't 0 mean the default max range? Only some users care about the upper limit anyway. We can also make it unsigned just in case and cap the value.

Thanks.

Signature · 1 line
tejun
maintainer Acked-by
#23 lore raw
In reply to Mark Wu
Quoted 42 lines · Mark Wu
Quoted 25 lines · Tejun Heo

Hello,

Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Quoted 8 lines

Unless async is explicitly used, probe happens sequentially. IOW, if there's no async_schedule() call, things won't happen in parallel. That said, I think it wouldn't be such a bad idea to protect ida with spinlock regardless unless the probe code explicitly requires serialization.

Thanks.

Since virtio blk driver doesn't use async probe, it needn't use spinlock to protect ida. So remove the lock from patch.

Quoted 1 line

From fbb396df9dbf8023f1b268be01b43529a3993d57 Mon Sep 17 00:00:00 2001

From: Mark Wu <[email protected]>
Date: Thu, 9 Jun 2011 06:34:07 -0400
Subject: [PATCH 1/1] [virt] virtio-blk: Use ida to allocate disk index

Quoted 8 lines

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

This got lost in the noise and missed 3.1 which is unfortunate. How about we apply this as is and look at cleanups as a next step?

Quoted 91 lines
---
 drivers/block/virtio_blk.c |   28 +++++++++++++++++++++++-----
 1 files changed, 23 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +23 -5
index 079c088..bf81ab6 100644
@@ -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
@@ -23,6 +26,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +347,23 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;
+		err = ida_get_new(&vd_index_ida, &index);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +436,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 +531,8 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	ida_remove(&vd_index_ida, index);
 out:
 	return err;
 }
@@ -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_remove(&vd_index_ida, vblk->index);
 }
 
 static const struct virtio_device_id id_table[] = {
Signature · 6 lines
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#24 lore raw
Quoted 50 lines · Michael S. Tsirkin
Quoted 49 lines · Mark Wu
Quoted 25 lines · Tejun Heo

Hello,

Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Quoted 8 lines

Unless async is explicitly used, probe happens sequentially. IOW, if there's no async_schedule() call, things won't happen in parallel. That said, I think it wouldn't be such a bad idea to protect ida with spinlock regardless unless the probe code explicitly requires serialization.

Thanks.

Since virtio blk driver doesn't use async probe, it needn't use spinlock to protect ida. So remove the lock from patch.

Quoted 1 line

From fbb396df9dbf8023f1b268be01b43529a3993d57 Mon Sep 17 00:00:00 2001

From: Mark Wu <[email protected]>
Date: Thu, 9 Jun 2011 06:34:07 -0400
Subject: [PATCH 1/1] [virt] virtio-blk: Use ida to allocate disk index

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

Quoted 6 lines
Quoted 1 line

This got lost in the noise and missed 3.1 which is unfortunate. How about we apply this as is and look at cleanups as a next step?

Rusty, any opinion on merging this for 3.2?
I expect merge window will open right after the summit, so need to decide soon ...

Quoted 91 lines
Quoted 91 lines
---
 drivers/block/virtio_blk.c |   28 +++++++++++++++++++++++-----
 1 files changed, 23 insertions(+), 5 deletions(-)
drivers/block/virtio_blk.c +23 -5
index 079c088..bf81ab6 100644
@@ -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
@@ -23,6 +26,7 @@ struct virtio_blk
 
 	/* The disk structure for the kernel. */
 	struct gendisk *disk;
+	u32 index;
 
 	/* Request tracking. */
 	struct list_head reqs;
@@ -343,12 +347,23 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)
 	struct request_queue *q;
 	int err;
 	u64 cap;
-	u32 v, blk_size, sg_elems, opt_io_size;
+	u32 v, blk_size, sg_elems, opt_io_size, index;
 	u16 min_io_size;
 	u8 physical_block_exp, alignment_offset;
 
-	if (index_to_minor(index) >= 1 << MINORBITS)
-		return -ENOSPC;
+	do {
+		if (!ida_pre_get(&vd_index_ida, GFP_KERNEL))
+			return -ENOMEM;
+		err = ida_get_new(&vd_index_ida, &index);
+	} while (err == -EAGAIN);
+
+	if (err)
+		return err;
+
+	if (index_to_minor(index) >= 1 << MINORBITS) {
+		err =  -ENOSPC;
+		goto out_free_index;
+	}
 
 	/* We need to know how many segments before we allocate. */
 	err = virtio_config_val(vdev, VIRTIO_BLK_F_SEG_MAX,
@@ -421,7 +436,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 +531,8 @@ out_free_vq:
 	vdev->config->del_vqs(vdev);
 out_free_vblk:
 	kfree(vblk);
+out_free_index:
+	ida_remove(&vd_index_ida, index);
 out:
 	return err;
 }
@@ -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_remove(&vd_index_ida, vblk->index);
 }
 
 static const struct virtio_device_id id_table[] = {
Signature · 6 lines
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
maintainer
#25 lore raw
Quoted 53 lines · Michael S. Tsirkin
Quoted 52 lines · Michael S. Tsirkin
Quoted 50 lines · Mark Wu
Quoted 25 lines · Tejun Heo

Hello,

Quoted 18 lines · Rusty Russell
Quoted 8 lines · Mark Wu

Hi Rusty,
Yes, I can't figure out an instance of disk probing in parallel either, but as per the following commit, I think we still need use lock for safety. What's your opinion?

commit 4034cc68157bfa0b6622efe368488d3d3e20f4e6

Author: Tejun Heo <[email protected]>
Date:   Sat Feb 21 11:04:45 2009 +0900
Quoted 8 lines

[SCSI] sd: revive sd_index_lock

Commit f27bac2761cab5a2e212dea602d22457a9aa6943 which converted sd to use ida instead of idr incorrectly removed sd_index_lock around id allocation and free. idr/ida do have internal locks but they protect their free object lists not the allocation itself. The caller is responsible for that. This missing synchronization led to the same id being assigned to multiple devices leading to oops.

Quoted 3 lines

I'm confused. Tejun, Greg, anyone can probes happen in parallel?

If so, I'll have to review all my drivers.

Quoted 8 lines

Unless async is explicitly used, probe happens sequentially. IOW, if there's no async_schedule() call, things won't happen in parallel. That said, I think it wouldn't be such a bad idea to protect ida with spinlock regardless unless the probe code explicitly requires serialization.

Thanks.

Since virtio blk driver doesn't use async probe, it needn't use spinlock to protect ida. So remove the lock from patch.

Quoted 1 line

From fbb396df9dbf8023f1b268be01b43529a3993d57 Mon Sep 17 00:00:00 2001

From: Mark Wu <[email protected]>
Date: Thu, 9 Jun 2011 06:34:07 -0400
Subject: [PATCH 1/1] [virt] virtio-blk: Use ida to allocate disk index

Current index allocation in virtio-blk is based on a monotonically increasing variable "index". It could cause some confusion about disk name in the case of hot-plugging disks. And it's impossible to find the lowest available index by just maintaining a simple index. So it's changed to use ida to allocate index via referring to the index allocation in scsi disk.

Quoted 7 lines
Quoted 4 lines

This got lost in the noise and missed 3.1 which is unfortunate. How about we apply this as is and look at cleanups as a next step?

Rusty, any opinion on merging this for 3.2?
I expect merge window will open right after the summit,

I can toss it into for-3.2/drivers, if there's consensus to do that now.

Signature · 1 line
Jens Axboe
#26 lore raw
Quoted 6 lines · Jens Axboe
Quoted 3 lines · Michael S. Tsirkin

On Wed, Oct 19, 2011 at 12:12:20PM +0200, Michael S. Tsirkin wrote:
Rusty, any opinion on merging this for 3.2?
I expect merge window will open right after the summit,

I can toss it into for-3.2/drivers, if there's consensus to do that now.

I'd like to see the final patch... we got the new simplified ida stuff in, so I assume it uses that?

But assume silence from me means consent: it's obviously the Right Thing.

Thanks,
Rusty.