Skip to contentSkip to conversation

two fixup for the block_device / hd_struct merge

PATCH In review Christoph Hellwig 3 participants 9 messages Jul 1, 2021 Block layer

lore
Share & tools
lore
#0 lore raw

Hi Jens,

find two fixes for the block_device / hd_struct merge series attached.

#1 lore raw

PATCH 1/2 block: grab a device refcount in disk_uevent

Sending uevents requires the struct device to be alive. To ensure that grab the device refcount instead of just an inode reference.

Fixes bc359d03c7ec ("block: add a disk_uevent helper")
---
 block/genhd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/genhd.c +2 -2
index 79aa40b4c39c..af4d2ab4a633 100644
@@ -365,12 +365,12 @@ void disk_uevent(struct gendisk *disk, enum kobject_action action)
 	xa_for_each(&disk->part_tbl, idx, part) {
 		if (bdev_is_partition(part) && !bdev_nr_sectors(part))
 			continue;
-		if (!bdgrab(part))
+		if (!kobject_get_unless_zero(&part->bd_device.kobj))
 			continue;
 
 		rcu_read_unlock();
 		kobject_uevent(bdev_kobj(part), action);
-		bdput(part);
+		put_device(&part->bd_device);
 		rcu_read_lock();
 	}
 	rcu_read_unlock();
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH 1/2] block: grab a device refcount in disk_uevent

Quoted 15 lines · Christoph Hellwig

Sending uevents requires the struct device to be alive. To ensure that grab the device refcount instead of just an inode reference.

Fixes bc359d03c7ec ("block: add a disk_uevent helper")
---
 block/genhd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/genhd.c +0 -0
index 79aa40b4c39c..af4d2ab4a633 100644

block/genhd.c void disk_uevent(struct gendisk *disk, enum kobject_action action) · line 369

Quoted 6 lines
block/genhd.c +1 -1
@@ -365,12 +365,12 @@ void disk_uevent(struct gendisk *disk, enum kobject_action action)
 	xa_for_each(&disk->part_tbl, idx, part) {
 		if (bdev_is_partition(part) && !bdev_nr_sectors(part))
 			continue;
-		if (!bdgrab(part))
+		if (!kobject_get_unless_zero(&part->bd_device.kobj))
 			continue;

->bd_device is embedded in the block device, and it has same lifetime with the block device, even part_release() calls bdput() to release this device, so why doesn't work by holding a inode reference?

Thanks,
Ming

#3 lore raw

Re: [PATCH 1/2] block: grab a device refcount in disk_uevent

Quoted 11 lines · Ming Lei
Quoted 10 lines · Christoph Hellwig

Sending uevents requires the struct device to be alive. To ensure that grab the device refcount instead of just an inode reference.

Fixes bc359d03c7ec ("block: add a disk_uevent helper")
---
 block/genhd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 15 lines

block/genhd.c void disk_uevent(struct gendisk *disk, enum kobject_action action) · line 369

Quoted 11 lines
block/genhd.c +1 -1
index 79aa40b4c39c..af4d2ab4a633 100644
@@ -365,12 +365,12 @@ void disk_uevent(struct gendisk *disk, enum kobject_action action)
 	xa_for_each(&disk->part_tbl, idx, part) {
 		if (bdev_is_partition(part) && !bdev_nr_sectors(part))
 			continue;
-		if (!bdgrab(part))
+		if (!kobject_get_unless_zero(&part->bd_device.kobj))
 			continue;

->bd_device is embedded in the block device, and it has same lifetime with the block device, even part_release() calls bdput() to release this device, so why doesn't work by holding a inode reference?

Because sending a uevent on a device that has device_del called on it is going to blow up.

#4 lore raw

Re: [PATCH 1/2] block: grab a device refcount in disk_uevent

Quoted 28 lines · Christoph Hellwig
Quoted 11 lines · Ming Lei
Quoted 10 lines · Christoph Hellwig

Sending uevents requires the struct device to be alive. To ensure that grab the device refcount instead of just an inode reference.

Fixes bc359d03c7ec ("block: add a disk_uevent helper")
---
 block/genhd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 15 lines

block/genhd.c void disk_uevent(struct gendisk *disk, enum kobject_action action) · line 369

Quoted 11 lines
block/genhd.c +1 -1
index 79aa40b4c39c..af4d2ab4a633 100644
@@ -365,12 +365,12 @@ void disk_uevent(struct gendisk *disk, enum kobject_action action)
 	xa_for_each(&disk->part_tbl, idx, part) {
 		if (bdev_is_partition(part) && !bdev_nr_sectors(part))
 			continue;
-		if (!bdgrab(part))
+		if (!kobject_get_unless_zero(&part->bd_device.kobj))
 			continue;

->bd_device is embedded in the block device, and it has same lifetime with the block device, even part_release() calls bdput() to release this device, so why doesn't work by holding a inode reference?

Quoted 2 lines

Because sending a uevent on a device that has device_del called on it is going to blow up.

But grabbing one reference can't prevent device_del() from being called.

IMO, if driver core doesn't allow to sending uevent on one deleted device, it should return a failure instead of kernel panic.

Thanks,
Ming

#5 lore raw

Re: [PATCH 1/2] block: grab a device refcount in disk_uevent

Quoted 29 lines · Ming Lei
Quoted 28 lines · Christoph Hellwig
Quoted 26 lines · Ming Lei
Quoted 15 lines · Christoph Hellwig

Sending uevents requires the struct device to be alive. To ensure that grab the device refcount instead of just an inode reference.

Fixes bc359d03c7ec ("block: add a disk_uevent helper")
---
 block/genhd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/genhd.c +0 -0
index 79aa40b4c39c..af4d2ab4a633 100644

block/genhd.c void disk_uevent(struct gendisk *disk, enum kobject_action action) · line 369

Quoted 6 lines
block/genhd.c +1 -1
@@ -365,12 +365,12 @@ void disk_uevent(struct gendisk *disk, enum kobject_action action)
 	xa_for_each(&disk->part_tbl, idx, part) {
 		if (bdev_is_partition(part) && !bdev_nr_sectors(part))
 			continue;
-		if (!bdgrab(part))
+		if (!kobject_get_unless_zero(&part->bd_device.kobj))
 			continue;

->bd_device is embedded in the block device, and it has same lifetime with the block device, even part_release() calls bdput() to release this device, so why doesn't work by holding a inode reference?

Quoted 7 lines
Quoted 2 lines

Because sending a uevent on a device that has device_del called on it is going to blow up.

But grabbing one reference can't prevent device_del() from being called.

IMO, if driver core doesn't allow to sending uevent on one deleted device, it should return a failure instead of kernel panic.

Maybe the reason is that sending uevent needs the device/kobject not 'released' in viewpoint of driver core world, other device resource(such as device name) may be referred but have been freed.

If that is the reason, this patch looks fine, and kobject_get_unless_zero() may be replaced with get_device() since the referred memory isn't freed yet.

Thanks,
Ming

#6 lore raw

Re: [PATCH 1/2] block: grab a device refcount in disk_uevent

Quoted 8 lines · Ming Lei

Maybe the reason is that sending uevent needs the device/kobject not 'released' in viewpoint of driver core world, other device resource(such as device name) may be referred but have been freed.

If that is the reason, this patch looks fine, and kobject_get_unless_zero() may be replaced with get_device() since the referred memory isn't freed yet.

Yes, sorry. Braino while looking up the cause. But there isn't much of a point in sending a uevent to a partition already deleted, so I think the unless_zero here still makes sense.

#7 lore raw

PATCH 2/2 block: remove the bdgrab in blk_drop_partitions

In reply to Christoph Hellwig

There is no need to hold a bdev reference when removing the partition.

---
 block/partitions/core.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
block/partitions/core.c +1 -5
index 347c56a51d87..3907cebd2be9 100644
@@ -537,12 +537,8 @@ void blk_drop_partitions(struct gendisk *disk)
 
 	lockdep_assert_held(&disk->open_mutex);
 
-	xa_for_each_start(&disk->part_tbl, idx, part, 1) {
-		if (!bdgrab(part))
-			continue;
+	xa_for_each_start(&disk->part_tbl, idx, part, 1)
 		delete_partition(part);
-		bdput(part);
-	}
 }
 
 static bool blk_add_partition(struct gendisk *disk,
Signature · 1 line
2.30.2
maintainer Applied
#8 lore raw
In reply to Christoph Hellwig
Quoted 3 lines · Christoph Hellwig

Hi Jens,

find two fixes for the block_device / hd_struct merge series attached.

Applied, thanks.

Signature · 1 line
Jens Axboe