From: Jan Kara <jack@suse.cz>
To: Yu Kuai <yukuai1@huaweicloud.com>
Cc: jack@suse.cz, hare@suse.de, hch@infradead.org, axboe@kernel.dk,
linux-block@vger.kernel.org, linux-kernel@vger.kernel.org,
yukuai3@huawei.com, yi.zhang@huawei.com, yangerkun@huawei.com
Subject: Re: [PATCH -next 2/2] block: fix scan partition for exclusively open device again
Date: Fri, 17 Feb 2023 12:05:17 +0100 [thread overview]
Message-ID: <20230217110517.hagxj47e2m4xbmkd@quack3> (raw)
In-Reply-To: <20230217022200.3092987-3-yukuai1@huaweicloud.com>
On Fri 17-02-23 10:22:00, Yu Kuai wrote:
> From: Yu Kuai <yukuai3@huawei.com>
>
> As explained in commit 36369f46e917 ("block: Do not reread partition table
> on exclusively open device"), reread partition on the device that is
> exclusively opened by someone else is problematic.
>
> This patch will make sure partition scan will only be proceed if current
> thread open the device exclusively, or the device is not opened
> exclusively, and in the later case, other scanners and exclusive openers
> will be blocked temporarily until partition scan is done.
>
> Fixes: 10c70d95c0f2 ("block: remove the bd_openers checks in blk_drop_partitions")
> Cc: <stable@vger.kernel.org>
> Suggested-by: Jan Kara <jack@suse.cz>
> Signed-off-by: Yu Kuai <yukuai3@huawei.com>
Looks good to me, just two minor comments below:
> diff --git a/block/genhd.c b/block/genhd.c
> index b30d5538710c..3ee5577e1586 100644
> --- a/block/genhd.c
> +++ b/block/genhd.c
> @@ -359,6 +359,7 @@ EXPORT_SYMBOL_GPL(disk_uevent);
> int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
> {
> struct block_device *bdev;
> + int ret = 0;
>
> if (disk->flags & (GENHD_FL_NO_PART | GENHD_FL_HIDDEN))
> return -EINVAL;
> @@ -368,11 +369,27 @@ int disk_scan_partitions(struct gendisk *disk, fmode_t mode)
> return -EBUSY;
>
> set_bit(GD_NEED_PART_SCAN, &disk->state);
I'd move the set_bit() after we are sure we have exclusive access to the
bdev. Otherwise we could set GD_NEED_PART_SCAN on a device exclusively open
by someone else and if we race with open in an unfortunate way, we could
trigger unexpected partition scan...
> - bdev = blkdev_get_by_dev(disk_devt(disk), mode, NULL);
> + /*
> + * If the device is opened exclusively by current thread already, it's
> + * safe to scan partitons, otherwise, use bd_prepare_to_claim() to
> + * synchronize with other exclusive openers and other partition
> + * scanners.
> + */
> + if (!(mode & FMODE_EXCL)) {
> + ret = bd_prepare_to_claim(disk->part0, disk_scan_partitions);
> + if (ret)
> + return ret;
> + }
> +
> + bdev = blkdev_get_by_dev(disk_devt(disk), mode & ~FMODE_EXCL, NULL);
> if (IS_ERR(bdev))
> - return PTR_ERR(bdev);
> - blkdev_put(bdev, mode);
> - return 0;
> + ret = PTR_ERR(bdev);
> + else
> + blkdev_put(bdev, mode);
> +
> + if (!(mode & FMODE_EXCL))
> + bd_abort_claiming(disk->part0, disk_scan_partitions);
> + return ret;
> }
>
> /**
> @@ -494,6 +511,11 @@ int __must_check device_add_disk(struct device *parent, struct gendisk *disk,
> if (ret)
> goto out_unregister_bdi;
>
> + /* Make sure the first partition scan will be proceed */
^^^^^^ "will happen"
probably makes more sense here.
Honza
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
next prev parent reply other threads:[~2023-02-17 11:06 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-17 2:21 [PATCH -next 0/2] block: fix scan partition for exclusively open device again Yu Kuai
2023-02-17 2:21 ` [PATCH -next 1/2] block: Revert "block: Do not reread partition table on exclusively open device" Yu Kuai
2023-02-17 11:01 ` Jan Kara
2023-02-17 2:22 ` [PATCH -next 2/2] block: fix scan partition for exclusively open device again Yu Kuai
2023-02-17 7:29 ` Christoph Hellwig
2023-02-17 11:05 ` Jan Kara [this message]
2023-02-17 13:16 ` [PATCH -next 0/2] " Jens Axboe
2023-03-21 11:43 ` Ming Lei
2023-03-22 1:26 ` Yu Kuai
2023-03-22 1:34 ` Ming Lei
2023-03-22 2:02 ` Yu Kuai
2023-03-22 2:15 ` Yu Kuai
2023-03-22 3:38 ` Ming Lei
2023-03-22 4:00 ` Yu Kuai
2023-03-22 3:59 ` [PATCH] block: don't set GD_NEED_PART_SCAN if scan partition failed Yu Kuai
2023-03-22 7:58 ` Ming Lei
2023-03-22 9:12 ` Yu Kuai
2023-03-22 9:47 ` Jan Kara
2023-03-22 11:34 ` Ming Lei
2023-03-22 13:07 ` Jan Kara
2023-03-22 16:08 ` Ming Lei
2023-03-23 10:51 ` Jan Kara
2023-03-23 12:03 ` Ming Lei
2023-03-22 9:52 ` Jan Kara
2023-03-23 23:59 ` Ming Lei
2023-04-06 3:42 ` Yu Kuai
2023-04-06 22:29 ` Jens Axboe
2023-04-07 2:01 ` Ming Lei
2023-04-07 2:42 ` Jens Axboe
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230217110517.hagxj47e2m4xbmkd@quack3 \
--to=jack@suse.cz \
--cc=axboe@kernel.dk \
--cc=hare@suse.de \
--cc=hch@infradead.org \
--cc=linux-block@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=yangerkun@huawei.com \
--cc=yi.zhang@huawei.com \
--cc=yukuai1@huaweicloud.com \
--cc=yukuai3@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox