Skip to contentSkip to conversation

RFC: sort out get_gendisk abuses

PATCH In review Christoph Hellwig 4 participants 9 messages Sep 25, 2020 – Oct 2, 2020

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

Hi all,

this series tries to remove two abuses of the get_gendisk API. The first one is fairly straigt forward and switched the blk-cgroup configuration API to properly open the block device, but I'd love to see it reviewed and tested by the cgroup maintainers, as I don't really know how this code is actually used.

The other one in the hibernation code really puzzles me - it busy loops on the gendisk lookup, just to leak a reference when it finally succeeds and then doesn't actually do anything with the result. My proposal for now is to simply revert the offending change, but better proposals are welcome.

#1 lore raw

PATCH 1/2 blk-cgroup: stop abusing get_gendisk

Properly open the device instead of relying on deep internals by using get_gendisk.

---
 block/blk-cgroup.c         | 42 +++++++++++++++++++-------------------
 block/blk-iocost.c         | 36 ++++++++++++++++----------------
 include/linux/blk-cgroup.h |  4 ++--
 3 files changed, 41 insertions(+), 41 deletions(-)
block/blk-cgroup.c +21 -21
index f9b55614d67d45..59b3983c84460a 100644
@@ -556,22 +556,22 @@ static struct blkcg_gq *blkg_lookup_check(struct blkcg *blkcg,
 }
 
 /**
- * blkg_conf_prep - parse and prepare for per-blkg config update
+ * blkcg_conf_open_bdev - parse and open bdev for per-blkg config update
  * @inputp: input string pointer
  *
  * Parse the device node prefix part, MAJ:MIN, of per-blkg config update
- * from @input and get and return the matching gendisk.  *@inputp is
+ * from @input and get and return the matching bdev.  *@inputp is
  * updated to point past the device node prefix.  Returns an ERR_PTR()
  * value on error.
  *
  * Use this function iff blkg_conf_prep() can't be used for some reason.
  */
-struct gendisk *blkcg_conf_get_disk(char **inputp)
+struct block_device *blkcg_conf_open_bdev(char **inputp)
 {
 	char *input = *inputp;
 	unsigned int major, minor;
-	struct gendisk *disk;
-	int key_len, part;
+	struct block_device *bdev;
+	int key_len;
 
 	if (sscanf(input, "%u:%u%n", &major, &minor, &key_len) != 2)
 		return ERR_PTR(-EINVAL);
@@ -581,16 +581,16 @@ struct gendisk *blkcg_conf_get_disk(char **inputp)
 		return ERR_PTR(-EINVAL);
 	input = skip_spaces(input);
 
-	disk = get_gendisk(MKDEV(major, minor), &part);
-	if (!disk)
+	bdev = blkdev_get_by_dev(MKDEV(major, minor), FMODE_READ, NULL);
+	if (!bdev)
 		return ERR_PTR(-ENODEV);
-	if (part) {
-		put_disk_and_module(disk);
+	if (bdev_is_partition(bdev)) {
+		blkdev_put(bdev, FMODE_READ);
 		return ERR_PTR(-ENODEV);
 	}
 
 	*inputp = input;
-	return disk;
+	return bdev;
 }
 
 /**
@@ -607,18 +607,18 @@ struct gendisk *blkcg_conf_get_disk(char **inputp)
  */
 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 		   char *input, struct blkg_conf_ctx *ctx)
-	__acquires(rcu) __acquires(&disk->queue->queue_lock)
+	__acquires(rcu) __acquires(&bdev->bd_disk->queue->queue_lock)
 {
-	struct gendisk *disk;
+	struct block_device *bdev;
 	struct request_queue *q;
 	struct blkcg_gq *blkg;
 	int ret;
 
-	disk = blkcg_conf_get_disk(&input);
-	if (IS_ERR(disk))
-		return PTR_ERR(disk);
+	bdev = blkcg_conf_open_bdev(&input);
+	if (IS_ERR(bdev))
+		return PTR_ERR(bdev);
 
-	q = disk->queue;
+	q = bdev->bd_disk->queue;
 
 	rcu_read_lock();
 	spin_lock_irq(&q->queue_lock);
@@ -680,7 +680,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 			goto success;
 	}
 success:
-	ctx->disk = disk;
+	ctx->bdev = bdev;
 	ctx->blkg = blkg;
 	ctx->body = input;
 	return 0;
@@ -689,7 +689,7 @@ int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 	spin_unlock_irq(&q->queue_lock);
 	rcu_read_unlock();
 fail:
-	put_disk_and_module(disk);
+	blkdev_put(bdev, FMODE_READ);
 	/*
 	 * If queue was bypassing, we should retry.  Do so after a
 	 * short msleep().  It isn't strictly necessary but queue
@@ -712,11 +712,11 @@ EXPORT_SYMBOL_GPL(blkg_conf_prep);
  * with blkg_conf_prep().
  */
 void blkg_conf_finish(struct blkg_conf_ctx *ctx)
-	__releases(&ctx->disk->queue->queue_lock) __releases(rcu)
+	__releases(&ctx->bdev->bd_disk->queue->queue_lock) __releases(rcu)
 {
-	spin_unlock_irq(&ctx->disk->queue->queue_lock);
+	spin_unlock_irq(&ctx->bdev->bd_disk->queue->queue_lock);
 	rcu_read_unlock();
-	put_disk_and_module(ctx->disk);
+	blkdev_put(ctx->bdev, FMODE_READ);
 }
 EXPORT_SYMBOL_GPL(blkg_conf_finish);
 
block/blk-iocost.c +18 -18
index b82649c1440bb3..731a09e7e0f4f5 100644
@@ -3120,23 +3120,23 @@ static const match_table_t qos_tokens = {
 static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
 			     size_t nbytes, loff_t off)
 {
-	struct gendisk *disk;
+	struct block_device *bdev;
 	struct ioc *ioc;
 	u32 qos[NR_QOS_PARAMS];
 	bool enable, user;
 	char *p;
 	int ret;
 
-	disk = blkcg_conf_get_disk(&input);
-	if (IS_ERR(disk))
-		return PTR_ERR(disk);
+	bdev = blkcg_conf_open_bdev(&input);
+	if (IS_ERR(bdev))
+		return PTR_ERR(bdev);
 
-	ioc = q_to_ioc(disk->queue);
+	ioc = q_to_ioc(bdev->bd_disk->queue);
 	if (!ioc) {
-		ret = blk_iocost_init(disk->queue);
+		ret = blk_iocost_init(bdev->bd_disk->queue);
 		if (ret)
 			goto err;
-		ioc = q_to_ioc(disk->queue);
+		ioc = q_to_ioc(bdev->bd_disk->queue);
 	}
 
 	spin_lock_irq(&ioc->lock);
@@ -3231,12 +3231,12 @@ static ssize_t ioc_qos_write(struct kernfs_open_file *of, char *input,
 	ioc_refresh_params(ioc, true);
 	spin_unlock_irq(&ioc->lock);
 
-	put_disk_and_module(disk);
+	blkdev_put(bdev, FMODE_READ);
 	return nbytes;
 einval:
 	ret = -EINVAL;
 err:
-	put_disk_and_module(disk);
+	blkdev_put(bdev, FMODE_READ);
 	return ret;
 }
 
@@ -3287,23 +3287,23 @@ static const match_table_t i_lcoef_tokens = {
 static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
 				    size_t nbytes, loff_t off)
 {
-	struct gendisk *disk;
+	struct block_device *bdev;
 	struct ioc *ioc;
 	u64 u[NR_I_LCOEFS];
 	bool user;
 	char *p;
 	int ret;
 
-	disk = blkcg_conf_get_disk(&input);
-	if (IS_ERR(disk))
-		return PTR_ERR(disk);
+	bdev = blkcg_conf_open_bdev(&input);
+	if (IS_ERR(bdev))
+		return PTR_ERR(bdev);
 
-	ioc = q_to_ioc(disk->queue);
+	ioc = q_to_ioc(bdev->bd_disk->queue);
 	if (!ioc) {
-		ret = blk_iocost_init(disk->queue);
+		ret = blk_iocost_init(bdev->bd_disk->queue);
 		if (ret)
 			goto err;
-		ioc = q_to_ioc(disk->queue);
+		ioc = q_to_ioc(bdev->bd_disk->queue);
 	}
 
 	spin_lock_irq(&ioc->lock);
@@ -3356,13 +3356,13 @@ static ssize_t ioc_cost_model_write(struct kernfs_open_file *of, char *input,
 	ioc_refresh_params(ioc, true);
 	spin_unlock_irq(&ioc->lock);
 
-	put_disk_and_module(disk);
+	blkdev_put(bdev, FMODE_READ);
 	return nbytes;
 
 einval:
 	ret = -EINVAL;
 err:
-	put_disk_and_module(disk);
+	blkdev_put(bdev, FMODE_READ);
 	return ret;
 }
 
include/linux/blk-cgroup.h +2 -2
index c8fc9792ac776d..b9f3c246c3c908 100644
@@ -197,12 +197,12 @@ void blkcg_print_blkgs(struct seq_file *sf, struct blkcg *blkcg,
 u64 __blkg_prfill_u64(struct seq_file *sf, struct blkg_policy_data *pd, u64 v);
 
 struct blkg_conf_ctx {
-	struct gendisk			*disk;
+	struct block_device		*bdev;
 	struct blkcg_gq			*blkg;
 	char				*body;
 };
 
-struct gendisk *blkcg_conf_get_disk(char **inputp);
+struct block_device *blkcg_conf_open_bdev(char **inputp);
 int blkg_conf_prep(struct blkcg *blkcg, const struct blkcg_policy *pol,
 		   char *input, struct blkg_conf_ctx *ctx);
 void blkg_conf_finish(struct blkg_conf_ctx *ctx);
Signature · 1 line
2.28.0
#2 lore raw

PATCH 2/2 PM/hibernate: remove the bogus call to get_gendisk in software_resume

In reply to Christoph Hellwig

get_gendisk grabs a reference on the disk and file operation, so this code will leak both of them while having absolutely no use for the gendisk itself.

This effectively reverts commit 2df83fa4bce421f
("PM / Hibernate: Use get_gendisk to verify partition if resume_file is integer format")

---
 kernel/power/hibernate.c | 11 -----------
 1 file changed, 11 deletions(-)
kernel/power/hibernate.c +0 -11
index e7aa57fb2fdc33..7d0b99d2e69631 100644
@@ -948,17 +948,6 @@ static int software_resume(void)
 
 	/* Check if the device is there */
 	swsusp_resume_device = name_to_dev_t(resume_file);
-
-	/*
-	 * name_to_dev_t is ineffective to verify parition if resume_file is in
-	 * integer format. (e.g. major:minor)
-	 */
-	if (isdigit(resume_file[0]) && resume_wait) {
-		int partno;
-		while (!get_gendisk(swsusp_resume_device, &partno))
-			msleep(10);
-	}
-
 	if (!swsusp_resume_device) {
 		/*
 		 * Some device discovery might still be in progress; we need
Signature · 1 line
2.28.0
#3 lore raw

Re: [PATCH 2/2] PM/hibernate: remove the bogus call to get_gendisk in software_resume

Hi!

Quoted 20 lines

get_gendisk grabs a reference on the disk and file operation, so this code will leak both of them while having absolutely no use for the gendisk itself.

This effectively reverts commit 2df83fa4bce421f
("PM / Hibernate: Use get_gendisk to verify partition if resume_file is integer format")

---
 kernel/power/hibernate.c | 11 -----------
 1 file changed, 11 deletions(-)
kernel/power/hibernate.c +0 -0
index e7aa57fb2fdc33..7d0b99d2e69631 100644
@@ -948,17 +948,6 @@ static int software_resume(void)
 
 	/* Check if the device is there */
 	swsusp_resume_device = name_to_dev_t(resume_file);

kernel/power/hibernate.c static int software_resume(void) · line 960

Quoted 10 lines
kernel/power/hibernate.c +0 -10
-
-	/*
-	 * name_to_dev_t is ineffective to verify parition if resume_file is in
-	 * integer format. (e.g. major:minor)
-	 */
-	if (isdigit(resume_file[0]) && resume_wait) {
-		int partno;
-		while (!get_gendisk(swsusp_resume_device, &partno))
-			msleep(10);
-	}

I believe point of this code was to wait for resume device to appear -- see the resume_wait condition. It should not be simply removed.

Best regards,
Pavel

Signature · 2 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
#4 lore raw

Re: [PATCH 2/2] PM/hibernate: remove the bogus call to get_gendisk in software_resume

Quoted 11 lines · Pavel Machek

kernel/power/hibernate.c static int software_resume(void) · line 960

Quoted 8 lines
kernel/power/hibernate.c +0 -8
-	 * name_to_dev_t is ineffective to verify parition if resume_file is in
-	 * integer format. (e.g. major:minor)
-	 */
-	if (isdigit(resume_file[0]) && resume_wait) {
-		int partno;
-		while (!get_gendisk(swsusp_resume_device, &partno))
-			msleep(10);
-	}

I believe point of this code was to wait for resume device to appear -- see the resume_wait condition. It should not be simply removed.

But get_gendisk has absolutely no relation to a device appearing. So whatever this code tried to do doesn't make any sense.

maintainer Acked-by on v1 2/2
#5 lore raw

Re: [PATCH 2/2] PM/hibernate: remove the bogus call to get_gendisk in software_resume

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

get_gendisk grabs a reference on the disk and file operation, so this code will leak both of them while having absolutely no use for the gendisk itself.

This effectively reverts commit 2df83fa4bce421f
("PM / Hibernate: Use get_gendisk to verify partition if resume_file is integer format")

Quoted 29 lines
---
 kernel/power/hibernate.c | 11 -----------
 1 file changed, 11 deletions(-)
kernel/power/hibernate.c +0 -12
index e7aa57fb2fdc33..7d0b99d2e69631 100644
@@ -948,17 +948,6 @@ static int software_resume(void)
 
        /* Check if the device is there */
        swsusp_resume_device = name_to_dev_t(resume_file);
-
-       /*
-        * name_to_dev_t is ineffective to verify parition if resume_file is in
-        * integer format. (e.g. major:minor)
-        */
-       if (isdigit(resume_file[0]) && resume_wait) {
-               int partno;
-               while (!get_gendisk(swsusp_resume_device, &partno))
-                       msleep(10);
-       }
-
        if (!swsusp_resume_device) {
                /*
                 * Some device discovery might still be in progress; we need
--

2.28.0

#6 lore raw

Re: [PATCH 2/2] PM/hibernate: remove the bogus call to get_gendisk in software_resume

Quoted 6 lines · Rafael J. Wysocki
Quoted 5 lines · Christoph Hellwig

get_gendisk grabs a reference on the disk and file operation, so this code will leak both of them while having absolutely no use for the gendisk itself.

Quoted 6 lines
Quoted 4 lines

This effectively reverts commit 2df83fa4bce421f
("PM / Hibernate: Use get_gendisk to verify partition if resume_file is integer format")

Can you pick it up through the PM tree? The big rework in this area I have planned won't land before 5.11 anyway.

#7 lore raw

Re: [PATCH 2/2] PM/hibernate: remove the bogus call to get_gendisk in software_resume

Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Rafael J. Wysocki
Quoted 8 lines · Christoph Hellwig

get_gendisk grabs a reference on the disk and file operation, so this code will leak both of them while having absolutely no use for the gendisk itself.

This effectively reverts commit 2df83fa4bce421f
("PM / Hibernate: Use get_gendisk to verify partition if resume_file is integer format")

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Can you pick it up through the PM tree? The big rework in this area I have planned won't land before 5.11 anyway.

Will do, thanks!

maintainer
#8 lore raw
In reply to Christoph Hellwig

Hello, Christoph.

Quoted 5 lines · Christoph Hellwig

this series tries to remove two abuses of the get_gendisk API. The first one is fairly straigt forward and switched the blk-cgroup configuration API to properly open the block device, but I'd love to see it reviewed and tested by the cgroup maintainers, as I don't really know how this code is actually used.

I'm a bit worried that requiring fully opening the device for configuration can lead to surprising behaviors. A now-unlikely but still possible case would be trying to configure IO parameters for a device w/ removeable media. All that the user is trying to do is configuring a bunch of parameters but the kernel would try to spin up the media and fail configuration and so on.

The use case of needing to access the associated data structures without fully activating the IO device seems valid to me. Whether that interface is blkdev_get() or something better abstracted, I don't really care.

Thanks.

Signature · 1 line
tejun