Skip to contentSkip to conversation

virtio-blk: Don't free ida when disk is in use

PATCH Applied Alexander Graf 4 participants 14 messages Dec 19, 2012 – Jan 2, 2013

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-blk: Don't free ida when disk is in use

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
 drivers/block/virtio_blk.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +6 -1
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
 #ifdef CONFIG_PM
Signature · 1 line
1.7.12.4
#1 lore raw
Quoted 7 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Quoted 8 lines

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

I think deserves a CC:stable, no?

I've put it in my pending queue for *next* merge window for now...

Thanks,
Rusty.

#2 lore raw
Quoted 13 lines · Rusty Russell
Quoted 11 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Quoted 8 lines
Quoted 4 lines

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

I think deserves a CC:stable, no?

I've put it in my pending queue for *next* merge window for now...

Thanks for looking into this, Alexander!

I also noticed this problem. The problem is that, if you hot-unplug a mounted or opened disk, the disk is in opened state. Next time, when you hotplug the same disk. The kernel thought it was opened already. The driver will use the wrong gendisk data structure in bdev.

blkdev_open
blkdev_get

      __blkdev_get
         if (!bdev->bd_openers) {    <-- Here, bd_disk not got updated
                                         still points to old one
           bdev->bd_disk = disk;
           bdev->bd_queue = disk->queue;
           ...

I tried something like this:

@@ -854,6 +862,19 @@ static int __devinit virtblk_probe(struct virtio_device *vdev)

                blk_queue_io_opt(q, blk_size * opt_io_size);

        add_disk(vblk->disk);

+
+ for (i = 0; i < 1 << PART_BITS; i++) {
+ bdev = bdget_disk(vblk->disk, i);
+ if (bdev) {

+                       bdev->bd_disk = vblk->disk;
+                       bdev->bd_queue = q;
+                       bdput(bdev);

+ }
+ }

1) Before:
---> hot-plug

[   35.730183] virtio_blk: virtblk_probe: vblk=ffff880078b0e000,
disk=ffff880078d54c00, q=ffff88007f88b3d8
[   35.735352] virtio_blk:    virtblk_ioctl: vblk=ffff880078b0e000,

disk=ffff880078d54c00, bdev=ffff88007c45cc00, q=ffff88007f88b3d8 ---> hot-unplug

---> hot-plug

[   83.570480] virtio_blk: virtblk_probe: vblk=ffff880078b0e000,
disk=ffff880078d55800, q=ffff88007f88bb40
[   83.575614] virtio_blk:   virtblk_ioctl: vblk=ffff880078b0e000,

disk=ffff880078d54c00, bdev=ffff88007c45cc00, q=ffff88007f88b3d8

The disk points to old one ffff880078d54c00. The queue also points to old one ffff88007f88b3d8.

2) After:
---> hot-plug

[   68.035063] virtio_blk: virtblk_probe: vblk=ffff880079b20000,
disk=ffff88007f9ebc00, q=ffff8800784d8ed0
[   68.041140] virtio_blk:    virtblk_ioctl: vblk=ffff880079b20000,

disk=ffff88007f9ebc00, bdev=ffff88007ab2c000, q=ffff8800784d8ed0 ---> hot-unplug

---> hot-plug

[   86.317706] virtio_blk: virtblk_probe: vblk=ffff880079b20000,
disk=ffff88007f9eb000, q=ffff8800784d9638
[   86.322535] virtio_blk:   virtblk_ioctl: vblk=ffff880079b20000,

disk=ffff88007f9eb000, bdev=ffff88007ab2c000, q=ffff8800784d9638

The disk and queue are updated correctly. The attached disk works and still uses the old name.

Signature · 1 line
Asias
#3 lore raw
Quoted 14 lines · Asias He
Quoted 13 lines · Rusty Russell
Quoted 11 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

I think deserves a CC:stable, no?

I wanted to make sure people can comment on it first. But yes, it should go into stable when accepted.

Quoted 9 lines
Quoted 2 lines

I've put it in my pending queue for *next* merge window for now...

Thanks for looking into this, Alexander!

I also noticed this problem. The problem is that, if you hot-unplug a mounted or opened disk, the disk is in opened state. Next time, when you hotplug the same disk. The kernel thought it was opened already. The driver will use the wrong gendisk data structure in bdev.

Exactly :)

Quoted 14 lines

blkdev_open
blkdev_get

     __blkdev_get
        if (!bdev->bd_openers) {    <-- Here, bd_disk not got updated
                                        still points to old one
          bdev->bd_disk = disk;
          bdev->bd_queue = disk->queue;
          ...

I tried something like this:

(quoted diff) +0 -0
@@ -854,6 +862,19 @@ static int __devinit virtblk_probe(struct
 virtio_device *vdev)

Review static int __devinit virtblk_probe(struct · line 872

Quoted 10 lines
(quoted diff) +7 -0
               blk_queue_io_opt(q, blk_size * opt_io_size);
 
       add_disk(vblk->disk);
+
+       for (i = 0; i < 1 << PART_BITS; i++) {
+               bdev = bdget_disk(vblk->disk, i);
+               if (bdev) {
+                       bdev->bd_disk = vblk->disk;
+                       bdev->bd_queue = q;
+                       bdput(bdev);

Does this stack? What if the device was mounted twice? Or mounted and dd'ed at the same time? Then your refcount here is wrong.

Also this means that you're free'ing a bdev even though there's still a reference held to it. That means whoever holds it could later use that pointer and access bogus memory.

Alex

Quoted 40 lines
(quoted diff) +2 -0
+               }
+       }
 
 

1) Before:
---> hot-plug

[   35.730183] virtio_blk: virtblk_probe: vblk=ffff880078b0e000,
disk=ffff880078d54c00, q=ffff88007f88b3d8
[   35.735352] virtio_blk:    virtblk_ioctl: vblk=ffff880078b0e000,

disk=ffff880078d54c00, bdev=ffff88007c45cc00, q=ffff88007f88b3d8 ---> hot-unplug

---> hot-plug

[   83.570480] virtio_blk: virtblk_probe: vblk=ffff880078b0e000,
disk=ffff880078d55800, q=ffff88007f88bb40
[   83.575614] virtio_blk:   virtblk_ioctl: vblk=ffff880078b0e000,

disk=ffff880078d54c00, bdev=ffff88007c45cc00, q=ffff88007f88b3d8

The disk points to old one ffff880078d54c00. The queue also points to old one ffff88007f88b3d8.

2) After:
---> hot-plug

[   68.035063] virtio_blk: virtblk_probe: vblk=ffff880079b20000,
disk=ffff88007f9ebc00, q=ffff8800784d8ed0
[   68.041140] virtio_blk:    virtblk_ioctl: vblk=ffff880079b20000,

disk=ffff88007f9ebc00, bdev=ffff88007ab2c000, q=ffff8800784d8ed0 ---> hot-unplug

---> hot-plug

[   86.317706] virtio_blk: virtblk_probe: vblk=ffff880079b20000,
disk=ffff88007f9eb000, q=ffff8800784d9638
[   86.322535] virtio_blk:   virtblk_ioctl: vblk=ffff880079b20000,

disk=ffff88007f9eb000, bdev=ffff88007ab2c000, q=ffff8800784d9638

The disk and queue are updated correctly. The attached disk works and still uses the old name.

Signature · 1 line
Asias
#6 lore raw
In reply to Alexander Graf
Quoted 38 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
 drivers/block/virtio_blk.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +2 -0
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);

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

Quoted 10 lines
drivers/block/virtio_blk.c +4 -1
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

Quoted 2 lines
Signature · 1 line
1.7.12.4
#7 lore raw
Quoted 48 lines · Michael S. Tsirkin
Quoted 47 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
drivers/block/virtio_blk.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +6 -1
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
Quoted 4 lines

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

Quoted 1 line
drivers/block/virtio_blk.c +0 -0
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

I would rather think the 100% right approach would be a recursive unrolling of all users bottom to top. Force unmount. Force close all fd's. I'm not sure why that doesn't happen today, but it doesn't :).

Alex

#8 lore raw
In reply to Michael S. Tsirkin
Quoted 48 lines · Michael S. Tsirkin
Quoted 47 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
drivers/block/virtio_blk.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +6 -1
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
Quoted 4 lines

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

Quoted 1 line
drivers/block/virtio_blk.c +0 -0
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

I would rather think the 100% right approach would be a recursive unrolling of all users bottom to top. Force unmount. Force close all fd's. I'm not sure why that doesn't happen today, but it doesn't :).

Alex

#9 lore raw
Quoted 56 lines · Alexander Graf
Quoted 48 lines · Michael S. Tsirkin
Quoted 47 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
drivers/block/virtio_blk.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +6 -1
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
Quoted 4 lines

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

Quoted 1 line
drivers/block/virtio_blk.c +0 -0
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

Quoted 5 lines

I would rather think the 100% right approach would be a recursive unrolling of all users bottom to top. Force unmount. Force close all fd's. I'm not sure why that doesn't happen today, but it doesn't :).

Alex

Especially force close fd's won't be easy. A simpler alternative would be preventing unplug by taking some reference count.

Signature · 1 line
MST
#10 lore raw
Quoted 57 lines · Michael S. Tsirkin
Quoted 56 lines · Alexander Graf
Quoted 52 lines · Michael S. Tsirkin
Quoted 38 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
drivers/block/virtio_blk.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +2 -0
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);

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

Quoted 10 lines
drivers/block/virtio_blk.c +4 -1
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

Quoted 8 lines
Quoted 5 lines

I would rather think the 100% right approach would be a recursive unrolling of all users bottom to top. Force unmount. Force close all fd's. I'm not sure why that doesn't happen today, but it doesn't :).

Alex

Especially force close fd's won't be easy. A simpler alternative would be preventing unplug by taking some reference count.

That's what happens when you do

<add>
mount /mnt
ls /mnt
<remove>

-- here remove failed for me --

But when you do

<add>
mount /mnt
<remove>

then remove works and we get the above problem.

I have to admit that I'm really not a big fan of preventing unplug though. In some cases, preventing simply doesn't work, for example when you unplug a USB stick or when your iscsi remote just vanishes.

Alex

#4 lore raw
In reply to Alexander Graf
Quoted 55 lines · Alexander Graf
Quoted 14 lines · Asias He
Quoted 13 lines · Rusty Russell
Quoted 11 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

I think deserves a CC:stable, no?

I wanted to make sure people can comment on it first. But yes, it should go into stable when accepted.

Quoted 9 lines
Quoted 2 lines

I've put it in my pending queue for *next* merge window for now...

Thanks for looking into this, Alexander!

I also noticed this problem. The problem is that, if you hot-unplug a mounted or opened disk, the disk is in opened state. Next time, when you hotplug the same disk. The kernel thought it was opened already. The driver will use the wrong gendisk data structure in bdev.

Exactly :)

Quoted 16 lines

blkdev_open
blkdev_get

     __blkdev_get
        if (!bdev->bd_openers) {    <-- Here, bd_disk not got updated
                                        still points to old one
          bdev->bd_disk = disk;
          bdev->bd_queue = disk->queue;
          ...

I tried something like this:

(quoted diff) +0 -0
@@ -854,6 +862,19 @@ static int __devinit virtblk_probe(struct
 virtio_device *vdev)
               blk_queue_io_opt(q, blk_size * opt_io_size);
 
Quoted 10 lines

Review static int __devinit virtblk_probe(struct · line 872

Quoted 8 lines
(quoted diff) +7 -0
       add_disk(vblk->disk);
+
+       for (i = 0; i < 1 << PART_BITS; i++) {
+               bdev = bdget_disk(vblk->disk, i);
+               if (bdev) {
+                       bdev->bd_disk = vblk->disk;
+                       bdev->bd_queue = q;
+                       bdput(bdev);

Does this stack? What if the device was mounted twice? Or mounted and dd'ed at the same time? Then your refcount here is wrong.

This is in the probe path. And there is no extra refcount here. bdev->bd_openers stays what it was before the remove.

Quoted 1 line

Also this means that you're free'ing a bdev even though there's still a reference held to it. That means whoever holds it could later use that pointer and access bogus memory.

The bdev is not freed.

Quoted 42 lines

Alex

Quoted 40 lines
(quoted diff) +2 -0
+               }
+       }
 
 

1) Before:
---> hot-plug

[   35.730183] virtio_blk: virtblk_probe: vblk=ffff880078b0e000,
disk=ffff880078d54c00, q=ffff88007f88b3d8
[   35.735352] virtio_blk:    virtblk_ioctl: vblk=ffff880078b0e000,

disk=ffff880078d54c00, bdev=ffff88007c45cc00, q=ffff88007f88b3d8 ---> hot-unplug

---> hot-plug

[   83.570480] virtio_blk: virtblk_probe: vblk=ffff880078b0e000,
disk=ffff880078d55800, q=ffff88007f88bb40
[   83.575614] virtio_blk:   virtblk_ioctl: vblk=ffff880078b0e000,

disk=ffff880078d54c00, bdev=ffff88007c45cc00, q=ffff88007f88b3d8

The disk points to old one ffff880078d54c00. The queue also points to old one ffff88007f88b3d8.

2) After:
---> hot-plug

[   68.035063] virtio_blk: virtblk_probe: vblk=ffff880079b20000,
disk=ffff88007f9ebc00, q=ffff8800784d8ed0
[   68.041140] virtio_blk:    virtblk_ioctl: vblk=ffff880079b20000,

disk=ffff88007f9ebc00, bdev=ffff88007ab2c000, q=ffff8800784d8ed0 ---> hot-unplug

---> hot-plug

[   86.317706] virtio_blk: virtblk_probe: vblk=ffff880079b20000,
disk=ffff88007f9eb000, q=ffff8800784d9638
[   86.322535] virtio_blk:   virtblk_ioctl: vblk=ffff880079b20000,

disk=ffff88007f9eb000, bdev=ffff88007ab2c000, q=ffff8800784d9638

The disk and queue are updated correctly. The attached disk works and still uses the old name.

Signature · 1 line
Asias
Signature · 1 line
Asias
#13 lore raw
In reply to Michael S. Tsirkin
Quoted 48 lines · Michael S. Tsirkin
Quoted 47 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
 drivers/block/virtio_blk.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +6 -1
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
Quoted 4 lines

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

Quoted 1 line
drivers/block/virtio_blk.c +0 -0
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

virtnet_remove()? I am not seeing the code there.

We are too late in the remove path in virtblk_remove(). No easy way to come back to the state before the remove. So, giving user warning (tell him to close the device) and retrying might be a choice.

Quoted 7 lines
Quoted 2 lines
Signature · 1 line
1.7.12.4

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Signature · 1 line
Asias
#12 lore raw
In reply to Alexander Graf
Quoted 51 lines · Alexander Graf
Quoted 48 lines · Michael S. Tsirkin
Quoted 47 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
drivers/block/virtio_blk.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +6 -1
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
Quoted 6 lines
Quoted 4 lines

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

Quoted 1 line
drivers/block/virtio_blk.c +0 -0
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

I would rather think the 100% right approach would be a recursive unrolling of all users bottom to top. Force unmount. Force close all fd's. I'm not sure why that doesn't happen today, but it doesn't :).

Yes ;-)

Quoted 8 lines

Alex

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Signature · 1 line
Asias
#11 lore raw
In reply to Alexander Graf
Quoted 69 lines · Alexander Graf
Quoted 57 lines · Michael S. Tsirkin
Quoted 56 lines · Alexander Graf
Quoted 52 lines · Michael S. Tsirkin
Quoted 38 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

---
drivers/block/virtio_blk.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/virtio_blk.c +2 -0
index 0bdde8f..07a18e2 100644
@@ -889,6 +889,7 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 {
 	struct virtio_blk *vblk = vdev->priv;
 	int index = vblk->index;
+	int refc;
 
 	/* Prevent config work handler from accessing the device. */
 	mutex_lock(&vblk->config_lock);
@@ -903,11 +904,15 @@ static void __devexit virtblk_remove(struct virtio_device *vdev)
 
 	flush_work(&vblk->config_work);
 
+	refc = atomic_read(&disk_to_dev(vblk->disk)->kobj.kref.refcount);
 	put_disk(vblk->disk);
 	mempool_destroy(vblk->pool);

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

Quoted 10 lines
drivers/block/virtio_blk.c +4 -1
 	vdev->config->del_vqs(vdev);
 	kfree(vblk);
-	ida_simple_remove(&vd_index_ida, index);
+
+	/* Only free device id if we don't have any users */
+	if (refc == 1)
+		ida_simple_remove(&vd_index_ida, index);
 }
 
 #ifdef CONFIG_PM

Network devices take the approach of retrying every second. Donnu if it makes sense here.

Quoted 8 lines
Quoted 5 lines

I would rather think the 100% right approach would be a recursive unrolling of all users bottom to top. Force unmount. Force close all fd's. I'm not sure why that doesn't happen today, but it doesn't :).

Alex

Especially force close fd's won't be easy. A simpler alternative would be preventing unplug by taking some reference count.

Quoted 8 lines

That's what happens when you do

<add>
mount /mnt
ls /mnt
<remove>

-- here remove failed for me --

What kernel version are you using? Any debug messages available.

Quoted 17 lines

But when you do

<add>
mount /mnt
<remove>

then remove works and we get the above problem.

I have to admit that I'm really not a big fan of preventing unplug though. In some cases, preventing simply doesn't work, for example when you unplug a USB stick or when your iscsi remote just vanishes. Alex

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

Signature · 1 line
Asias
#5 lore raw
In reply to Alexander Graf
Quoted 16 lines · Alexander Graf
Quoted 14 lines · Asias He
Quoted 13 lines · Rusty Russell
Quoted 11 lines · Alexander Graf

When a file system is mounted on a virtio-blk disk, we then remove it and then reattach it, the reattached disk gets the same disk name and ids as the hot removed one.

This leads to very nasty effects - mostly rendering the newly attached device completely unusable.

Trying what happens when I do the same thing with a USB device, I saw that the sd node simply doesn't get free'd when a device gets forcefully removed.

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines

Imitate the same behavior for vd devices. This way broken vd devices simply are never free'd and newly attached ones keep working just fine.

I think deserves a CC:stable, no?

I wanted to make sure people can comment on it first. But yes, it should go into stable when accepted.

Queued in my fixes branch, will go to Linus soon.

Thanks,
Rusty.