Skip to contentSkip to conversation

nbd: del_gendisk after we cleanup the queue

PATCH In review Josef Bacik 3 participants 9 messages Apr 13, 2018 – Apr 14, 2018

Subsystems (3)
lore
Share & tools
lore

Josef Bacik

author maintainer
#0 lore raw

PATCH 1/3 nbd: del_gendisk after we cleanup the queue

From: Josef Bacik <[email protected]>

This fixes a use after free bug, we need to do the del_gendisk after we cleanup the queue on the device.

Fixes c6a4759ea0c9 ("nbd: add device refcounting")
---
 drivers/block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/nbd.c +1 -1
index 86258b00a1d4..e33da3e6aa20 100644
@@ -174,8 +174,8 @@ static void nbd_dev_remove(struct nbd_device *nbd)
 {
 	struct gendisk *disk = nbd->disk;
 	if (disk) {
-		del_gendisk(disk);
 		blk_cleanup_queue(disk->queue);
+		del_gendisk(disk);
 		blk_mq_free_tag_set(&nbd->tag_set);
 		disk->private_data = NULL;
 		put_disk(disk);
Signature · 1 line
2.14.3
author maintainer
#1 lore raw

PATCH 2/3 nbd: update size when connected

From: Josef Bacik <[email protected]>

I messed up changing the size of an NBD device while it was connected by not actually updating the device or doing the uevent. Fix this by updating everything if we're connected and we change the size.

Fixes 639812a ("nbd: don't set the device size until we're connected")
---
 drivers/block/nbd.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/block/nbd.c +2 -0
index e33da3e6aa20..1520383b12f6 100644
@@ -243,6 +243,8 @@ static void nbd_size_set(struct nbd_device *nbd, loff_t blocksize,
 	struct nbd_config *config = nbd->config;
 	config->blksize = blocksize;
 	config->bytesize = blocksize * nr_blocks;
+	if (nbd->task_recv != NULL)
+		nbd_size_update(nbd);
 }
 
 static void nbd_complete_rq(struct request *req)
Signature · 1 line
2.14.3
Tested-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] nbd: update size when connected

Quoted 6 lines · Josef Bacik

From: Josef Bacik <[email protected]>

I messed up changing the size of an NBD device while it was connected by not actually updating the device or doing the uevent. Fix this by updating everything if we're connected and we change the size.

The three patches Josef submitted today fix the problem with the online rbd-nbd expansion completely.

Quoted 3 lines
Fixes 639812a ("nbd: don't set the device size until we're connected")
Quoted 7 lines

---

 drivers/block/nbd.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/block/nbd.c +0 -0

Review static void nbd_size_set(struct nbd_device *nbd,

Quoted 7 lines

index e33da3e6aa20..1520383b12f6 100644

--- a/drivers/block/nbd.c

+++ b/drivers/block/nbd.c

(quoted diff) +0 -0
@@ -243,6 +243,8 @@ static void nbd_size_set(struct nbd_device *nbd,

loff_t blocksize,

Review static void nbd_size_set(struct nbd_device *nbd,

Quoted 13 lines
(quoted diff) +2 -0
        struct nbd_config *config = nbd->config;
 
        config->blksize = blocksize;
 
        config->bytesize = blocksize * nr_blocks;
 
+       if (nbd->task_recv != NULL)
 
+               nbd_size_update(nbd);
 
 }

static void nbd_complete_rq(struct request *req)

Quoted 6 lines

--

2.14.3

author maintainer
#3 lore raw

PATCH 3/3 nbd: use bd_set_size when updating disk size

In reply to Josef Bacik

From: Josef Bacik <[email protected]>

When we stopped relying on the bdev everywhere I broke updating the block device size on the fly, which ceph relies on. We can't just do set_capacity, we also have to do bd_set_size so things like parted will notice the device size change.

Fixes 29eaadc ("nbd: stop using the bdev everywhere")
---
 drivers/block/nbd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/nbd.c +6 -1
index 1520383b12f6..61dd95aa47fa 100644
@@ -231,9 +231,15 @@ static void nbd_size_clear(struct nbd_device *nbd)
 static void nbd_size_update(struct nbd_device *nbd)
 {
 	struct nbd_config *config = nbd->config;
+	struct block_device *bdev = bdget_disk(nbd->disk, 0);
+
 	blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
 	blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
 	set_capacity(nbd->disk, config->bytesize >> 9);
+	if (bdev) {
+		bd_set_size(bdev, config->bytesize);
+		bdput(bdev);
+	}
 	kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
 }
 
@@ -1111,7 +1117,6 @@ static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *b
 	if (ret)
 		return ret;
 
-	bd_set_size(bdev, config->bytesize);
 	if (max_part)
 		bdev->bd_invalidated = 1;
 	mutex_unlock(&nbd->config_lock);
Signature · 1 line
2.14.3
Tested-by on v1 3/3
#4 lore raw

Re: [PATCH 3/3] nbd: use bd_set_size when updating disk size

Quoted 7 lines · Josef Bacik

From: Josef Bacik <[email protected]>

When we stopped relying on the bdev everywhere I broke updating the block device size on the fly, which ceph relies on. We can't just do set_capacity, we also have to do bd_set_size so things like parted will notice the device size change.

The three patches Josef submitted today fix the problem with the online rbd-nbd expansion completely.

Fixes 29eaadc ("nbd: stop using the bdev everywhere")
Quoted 1 line
Quoted 7 lines

---

 drivers/block/nbd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
drivers/block/nbd.c +0 -0
Quoted 27 lines

index 1520383b12f6..61dd95aa47fa 100644

--- a/drivers/block/nbd.c

+++ b/drivers/block/nbd.c

(quoted diff) +3 -0
@@ -231,9 +231,15 @@ static void nbd_size_clear(struct nbd_device *nbd)
 
 static void nbd_size_update(struct nbd_device *nbd)
 
 {
 
        struct nbd_config *config = nbd->config;
 
+       struct block_device *bdev = bdget_disk(nbd->disk, 0);
 
+
 
        blk_queue_logical_block_size(nbd->disk->queue, config->blksize);
 
        blk_queue_physical_block_size(nbd->disk->queue, config->blksize);
 
        set_capacity(nbd->disk, config->bytesize >> 9);
 
+       if (bdev) {

Review static void nbd_size_clear(struct nbd_device *nbd)

Quoted 10 lines
(quoted diff) +3 -0
 
+               bd_set_size(bdev, config->bytesize);
 
+               bdput(bdev);
 
+       }
 
        kobject_uevent(&nbd_to_dev(nbd)->kobj, KOBJ_CHANGE);
 
 }

@@ -1111,7 +1117,6 @@ static int nbd_start_device_ioctl(struct nbd_device *nbd, struct block_device *b

Quoted 5 lines
        if (ret)

                return ret;

- bd_set_size(bdev, config->bytesize);

Quoted 12 lines
        if (max_part)

                bdev->bd_invalidated = 1;

        mutex_unlock(&nbd->config_lock);

--

2.14.3

#5 lore raw
In reply to Josef Bacik

T24gRnJpLCAyMDE4LTA0LTEzIGF0IDEyOjAzIC0wNDAwLCBKb3NlZiBCYWNpayB3cm90ZToNCj4g VGhpcyBmaXhlcyBhIHVzZSBhZnRlciBmcmVlIGJ1Zywgd2UgbmVlZCB0byBkbyB0aGUgZGVsX2dl bmRpc2sgYWZ0ZXIgd2UNCj4gY2xlYW51cCB0aGUgcXVldWUgb24gdGhlIGRldmljZS4NCg0KSGVs bG8gSm9zZWYsDQoNCldoaWNoIHVzZS1hZnRlci1mcmVlIGJ1ZyBkb2VzIHRoaXMgcGF0Y2ggZml4 PyBBcmUgeW91IGF3YXJlIHRoYXQgbW9zdCBibG9jaw0KZHJpdmVycyBjYWxsIGJsa19jbGVhbnVw X3F1ZXVlKCkgYmVmb3JlIGNhbGxpbmcgZGVsX2dlbmRpc2soKT8gV2h5IGRvIHlvdQ0KdGhpbmsg dGhhdCB0aGUgbmJkIGRyaXZlciBzaG91bGQgdXNlIHRoZSBvcHBvc2l0ZSBvcmRlcj8NCg0KVGhh bmtzLA0KDQpCYXJ0Lg0KDQo=

author maintainer
#6 lore raw
Quoted 10 lines · Bart Van Assche
Quoted 2 lines · Josef Bacik

This fixes a use after free bug, we need to do the del_gendisk after we cleanup the queue on the device.

Hello Josef,

Which use-after-free bug does this patch fix? Are you aware that most block drivers call blk_cleanup_queue() before calling del_gendisk()? Why do you think that the nbd driver should use the opposite order?

I'm confused, that's what this patch does. Before I had del_gendisk() first and then the blk_cleanup_queue(), which was bugging out when I was testing stuff with a null pointer deref whenever I rmmod'ed the nbd. Swapping it to the way everybody else did it fixed the problem. Thanks,

Josef

#7 lore raw

T24gRnJpLCAyMDE4LTA0LTEzIGF0IDEyOjIxIC0wNDAwLCBKb3NlZiBCYWNpayB3cm90ZToNCj4g T24gRnJpLCBBcHIgMTMsIDIwMTggYXQgMDQ6M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m9zZWYsDQoNCk9vcHMsIEkgc3dhcHBlZCAiYmxrX2NsZWFudXBfcXVldWUoKSIg YW5kICJkZWxfZ2VuZGlzaygpIiBpbiBteSBlLW1haWwuDQoNCkNhbiB5b3Ugc2hhcmUgdGhlIGNh bGwgc3RhY2sgb2YgdGhlIE5VTEwgcG9pbnRlciBkZXJlZiBhbmQgYWxzbyB0aGUgdHJhbnNsYXRp b24NCm9mIHRoZSBjcmFzaCBhZGRyZXNzIGludG8gYSBzb3VyY2UgY29kZSBsaW5lPw0KDQpUaGFu a3MsDQoNCkJhcnQuDQoNCg0K

Tested-by on v1 1/3
#8 lore raw
In reply to Josef Bacik
Quoted 5 lines · Josef Bacik

From: Josef Bacik <[email protected]>

This fixes a use after free bug, we need to do the del_gendisk after we cleanup the queue on the device.

The three patches Josef submitted today fix the problem with the online rbd-nbd expansion completely.

Fixes c6a4759ea0c9 ("nbd: add device refcounting")
Quoted 3 lines
Quoted 7 lines

---

 drivers/block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/nbd.c +0 -0
Quoted 32 lines

index 86258b00a1d4..e33da3e6aa20 100644

--- a/drivers/block/nbd.c

+++ b/drivers/block/nbd.c

(quoted diff) +1 -2
@@ -174,8 +174,8 @@ static void nbd_dev_remove(struct nbd_device *nbd)
 
 {
 
        struct gendisk *disk = nbd->disk;
 
        if (disk) {
 
-               del_gendisk(disk);
 
                blk_cleanup_queue(disk->queue);
 
+               del_gendisk(disk);
 
                blk_mq_free_tag_set(&nbd->tag_set);
 
                disk->private_data = NULL;
 
                put_disk(disk);
 
--

2.14.3