linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Minchan Kim <minchan@kernel.org>
To: Christoph Hellwig <hch@lst.de>
Cc: "Jens Axboe" <axboe@kernel.dk>,
	"Justin Sanders" <justin@coraid.com>,
	"Josef Bacik" <josef@toxicpanda.com>,
	"Ilya Dryomov" <idryomov@gmail.com>,
	"Jack Wang" <jinpu.wang@cloud.ionos.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Konrad Rzeszutek Wilk" <konrad.wilk@oracle.com>,
	"Roger Pau Monné" <roger.pau@citrix.com>,
	"Mike Snitzer" <snitzer@redhat.com>, "Song Liu" <song@kernel.org>,
	"Martin K. Petersen" <martin.petersen@oracle.com>,
	dm-devel@redhat.com, linux-block@vger.kernel.org,
	drbd-dev@lists.linbit.com, nbd@other.debian.org,
	ceph-devel@vger.kernel.org, xen-devel@lists.xenproject.org,
	linux-raid@vger.kernel.org, linux-nvme@lists.infradead.org,
	linux-scsi@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	"Sergey Senozhatsky" <sergey.senozhatsky@gmail.com>
Subject: Re: [PATCH 60/78] zram: remove the claim mechanism
Date: Wed, 25 Nov 2020 17:11:07 -0800	[thread overview]
Message-ID: <20201126011107.GA57352@google.com> (raw)
In-Reply-To: <20201116145809.410558-61-hch@lst.de>

On Mon, Nov 16, 2020 at 03:57:51PM +0100, Christoph Hellwig wrote:
> The zram claim mechanism was added to ensure no new opens come in
> during teardown.  But the proper way to archive that is to call
> del_gendisk first, which takes care of all that.  Once del_gendisk
> is called in the right place, the reset side can also be simplified
> as no I/O can be outstanding on a block device that is not open.

It would be great if it makes the mess simple. Let me have a question
Please see below.

> 
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
>  drivers/block/zram/zram_drv.c | 76 ++++++++++-------------------------
>  1 file changed, 21 insertions(+), 55 deletions(-)
> 
> diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
> index 6d15d51cee2b7e..3641434a9b154d 100644
> --- a/drivers/block/zram/zram_drv.c
> +++ b/drivers/block/zram/zram_drv.c
> @@ -1756,64 +1756,33 @@ static ssize_t disksize_store(struct device *dev,
>  static ssize_t reset_store(struct device *dev,
>  		struct device_attribute *attr, const char *buf, size_t len)
>  {
> -	int ret;
> -	unsigned short do_reset;
> -	struct zram *zram;
> +	struct zram *zram = dev_to_zram(dev);
>  	struct block_device *bdev;
> +	unsigned short do_reset;
> +	int ret = 0;
>  
>  	ret = kstrtou16(buf, 10, &do_reset);
>  	if (ret)
>  		return ret;
> -
>  	if (!do_reset)
>  		return -EINVAL;
>  
> -	zram = dev_to_zram(dev);
>  	bdev = bdget_disk(zram->disk, 0);
>  	if (!bdev)
>  		return -ENOMEM;
>  
>  	mutex_lock(&bdev->bd_mutex);
> -	/* Do not reset an active device or claimed device */
> -	if (bdev->bd_openers || zram->claim) {
> -		mutex_unlock(&bdev->bd_mutex);
> -		bdput(bdev);
> -		return -EBUSY;
> -	}
> -
> -	/* From now on, anyone can't open /dev/zram[0-9] */
> -	zram->claim = true;
> +	if (bdev->bd_openers)
> +		ret = -EBUSY;
> +	else
> +		zram_reset_device(zram);
>  	mutex_unlock(&bdev->bd_mutex);
> -
> -	/* Make sure all the pending I/O are finished */
> -	fsync_bdev(bdev);
> -	zram_reset_device(zram);
>  	bdput(bdev);
>  
> -	mutex_lock(&bdev->bd_mutex);
> -	zram->claim = false;
> -	mutex_unlock(&bdev->bd_mutex);
> -
> -	return len;
> -}
> -
> -static int zram_open(struct block_device *bdev, fmode_t mode)
> -{
> -	int ret = 0;
> -	struct zram *zram;
> -
> -	WARN_ON(!mutex_is_locked(&bdev->bd_mutex));
> -
> -	zram = bdev->bd_disk->private_data;
> -	/* zram was claimed to reset so open request fails */
> -	if (zram->claim)
> -		ret = -EBUSY;
> -
> -	return ret;
> +	return ret ? ret : len;
>  }
>  
>  static const struct block_device_operations zram_devops = {
> -	.open = zram_open,
>  	.submit_bio = zram_submit_bio,
>  	.swap_slot_free_notify = zram_slot_free_notify,
>  	.rw_page = zram_rw_page,
> @@ -1821,7 +1790,6 @@ static const struct block_device_operations zram_devops = {
>  };
>  
>  static const struct block_device_operations zram_wb_devops = {
> -	.open = zram_open,
>  	.submit_bio = zram_submit_bio,
>  	.swap_slot_free_notify = zram_slot_free_notify,
>  	.owner = THIS_MODULE
> @@ -1972,34 +1940,32 @@ static int zram_add(void)
>  	return ret;
>  }
>  
> -static int zram_remove(struct zram *zram)
> +static bool zram_busy(struct zram *zram)
>  {
>  	struct block_device *bdev;
> +	bool busy = false;
>  
>  	bdev = bdget_disk(zram->disk, 0);
> -	if (!bdev)
> -		return -ENOMEM;
> -
> -	mutex_lock(&bdev->bd_mutex);
> -	if (bdev->bd_openers || zram->claim) {
> -		mutex_unlock(&bdev->bd_mutex);
> +	if (bdev) {
> +		if (bdev->bd_openers)
> +			busy = true;
>  		bdput(bdev);
> -		return -EBUSY;
>  	}
>  
> -	zram->claim = true;
> -	mutex_unlock(&bdev->bd_mutex);
> +	return busy;
> +}
>  
> -	zram_debugfs_unregister(zram);
> +static int zram_remove(struct zram *zram)
> +{
> +	if (zram_busy(zram))
> +		return -EBUSY;
>  
> -	/* Make sure all the pending I/O are finished */
> -	fsync_bdev(bdev);
> +	del_gendisk(zram->disk);
> +	zram_debugfs_unregister(zram);
>  	zram_reset_device(zram);
> -	bdput(bdev);
>  
>  	pr_info("Removed device: %s\n", zram->disk->disk_name);
>  
> -	del_gendisk(zram->disk);
>  	blk_cleanup_queue(zram->disk->queue);
>  	put_disk(zram->disk);
>  	kfree(zram);
> -- 
> 2.29.2
> 

With this patch, how deal with the race?

CPU 1                                     CPU 2

hot_remove_store
  zram_remove
    zram_busy
      return -EBUSY
                                         open /dev/zram0
    del_gendisk
    zram_reset and destroy


  parent reply	other threads:[~2020-11-26  1:11 UTC|newest]

Thread overview: 113+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-16 14:56 cleanup updating the size of block devices v3 Christoph Hellwig
2020-11-16 14:56 ` [PATCH 01/78] block: remove the call to __invalidate_device in check_disk_size_change Christoph Hellwig
2020-11-16 14:56 ` [PATCH 02/78] loop: let set_capacity_revalidate_and_notify update the bdev size Christoph Hellwig
2020-11-16 14:56 ` [PATCH 03/78] nvme: " Christoph Hellwig
2020-11-16 14:56 ` [PATCH 04/78] sd: update the bdev size in sd_revalidate_disk Christoph Hellwig
2020-11-16 14:56 ` [PATCH 05/78] block: remove the update_bdev parameter to set_capacity_revalidate_and_notify Christoph Hellwig
2020-11-16 14:56 ` [PATCH 06/78] nbd: remove the call to set_blocksize Christoph Hellwig
2020-11-16 14:56 ` [PATCH 07/78] nbd: move the task_recv check into nbd_size_update Christoph Hellwig
2020-11-16 14:56 ` [PATCH 08/78] nbd: refactor size updates Christoph Hellwig
2020-11-16 14:57 ` [PATCH 09/78] nbd: validate the block size in nbd_set_size Christoph Hellwig
2020-11-16 14:57 ` [PATCH 10/78] nbd: use set_capacity_and_notify Christoph Hellwig
2020-11-16 14:57 ` [PATCH 11/78] aoe: don't call set_capacity from irq context Christoph Hellwig
2020-11-16 14:57 ` [PATCH 12/78] dm: use set_capacity_and_notify Christoph Hellwig
2021-02-12 15:45   ` Mike Snitzer
2021-02-16 11:46     ` Christoph Hellwig
2020-11-16 14:57 ` [PATCH 13/78] pktcdvd: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 14/78] nvme: use set_capacity_and_notify in nvme_set_queue_dying Christoph Hellwig
2020-11-16 14:57 ` [PATCH 15/78] drbd: use set_capacity_and_notify Christoph Hellwig
2020-11-16 14:57 ` [PATCH 16/78] rbd: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 17/78] rnbd: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 18/78] zram: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 19/78] dm-raid: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 20/78] md: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 21/78] md: remove a spurious call to revalidate_disk_size in update_size Christoph Hellwig
2020-11-16 14:57 ` [PATCH 22/78] virtio-blk: remove a spurious call to revalidate_disk_size Christoph Hellwig
2020-11-16 14:57 ` [PATCH 23/78] block: unexport revalidate_disk_size Christoph Hellwig
2020-11-16 14:57 ` [PATCH 24/78] mtd_blkdevs: don't override BLKFLSBUF Christoph Hellwig
2020-11-16 14:57 ` [PATCH 25/78] block: don't call into the driver for BLKFLSBUF Christoph Hellwig
2020-11-16 14:57 ` [PATCH 26/78] block: add a new set_read_only method Christoph Hellwig
2020-11-16 14:57 ` [PATCH 27/78] rbd: implement ->set_read_only to hook into BLKROSET processing Christoph Hellwig
2020-11-16 14:57 ` [PATCH 28/78] md: " Christoph Hellwig
2020-11-16 17:37   ` Song Liu
2020-11-16 14:57 ` [PATCH 29/78] dasd: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 30/78] block: don't call into the driver for BLKROSET Christoph Hellwig
2020-11-20  7:19   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 31/78] loop: use set_disk_ro Christoph Hellwig
2020-11-20  7:20   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 32/78] block: remove set_device_ro Christoph Hellwig
2020-11-20  7:20   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 33/78] block: remove __blkdev_driver_ioctl Christoph Hellwig
2020-11-20  7:22   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 34/78] block: propagate BLKROSET to all partitions Christoph Hellwig
2020-11-20  7:23   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 35/78] block: cleanup del_gendisk a bit Christoph Hellwig
2020-11-16 14:57 ` [PATCH 36/78] block: open code kobj_map into in block/genhd.c Christoph Hellwig
2020-11-16 14:57 ` [PATCH 37/78] block: split block_class_lock Christoph Hellwig
2020-11-16 14:57 ` [PATCH 38/78] block: rework requesting modules for unclaimed devices Christoph Hellwig
2020-11-16 14:57 ` [PATCH 39/78] block: add an optional probe callback to major_names Christoph Hellwig
2020-11-16 14:57 ` [PATCH 40/78] ide: remove ide_{,un}register_region Christoph Hellwig
2020-11-16 14:57 ` [PATCH 41/78] swim: don't call blk_register_region Christoph Hellwig
2020-11-16 14:57 ` [PATCH 42/78] sd: use __register_blkdev to avoid a modprobe for an unregistered dev_t Christoph Hellwig
2020-11-16 14:57 ` [PATCH 43/78] brd: use __register_blkdev to allocate devices on demand Christoph Hellwig
2020-11-16 14:57 ` [PATCH 44/78] loop: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 45/78] md: " Christoph Hellwig
2020-11-16 14:57 ` [PATCH 46/78] ide: switch to __register_blkdev for command set probing Christoph Hellwig
2020-11-16 14:57 ` [PATCH 47/78] floppy: use a separate gendisk for each media format Christoph Hellwig
2020-11-16 14:57 ` [PATCH 48/78] amiflop: use separate gendisks for Amiga vs MS-DOS mode Christoph Hellwig
2020-11-16 14:57 ` [PATCH 49/78] ataflop: use a separate gendisk for each media format Christoph Hellwig
2020-11-16 14:57 ` [PATCH 50/78] z2ram: reindent Christoph Hellwig
2020-11-16 14:57 ` [PATCH 51/78] z2ram: use separate gendisk for the different modes Christoph Hellwig
2020-11-16 14:57 ` [PATCH 52/78] block: switch gendisk lookup to a simple xarray Christoph Hellwig
2020-11-16 14:57 ` [PATCH 53/78] blk-cgroup: fix a hd_struct leak in blkcg_fill_root_iostats Christoph Hellwig
2020-11-20  7:25   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 54/78] block: remove a duplicate __disk_get_part prototype Christoph Hellwig
2020-11-20  7:25   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 55/78] block: change the hash used for looking up block devices Christoph Hellwig
2020-11-20  7:26   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 56/78] init: refactor name_to_dev_t Christoph Hellwig
2020-11-20  7:31   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 57/78] init: refactor devt_from_partuuid Christoph Hellwig
2020-11-20  7:33   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 58/78] init: cleanup match_dev_by_uuid and match_dev_by_label Christoph Hellwig
2020-11-20  7:34   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 59/78] mtip32xx: remove the call to fsync_bdev on removal Christoph Hellwig
2020-11-20  7:35   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 60/78] zram: remove the claim mechanism Christoph Hellwig
2020-11-20  7:37   ` Hannes Reinecke
2020-11-26  1:11   ` Minchan Kim [this message]
2020-11-26  9:59     ` Christoph Hellwig
2020-11-16 14:57 ` [PATCH 61/78] zram: do not call set_blocksize Christoph Hellwig
2020-11-20  7:38   ` Hannes Reinecke
2020-11-26  1:16   ` Minchan Kim
2020-11-16 14:57 ` [PATCH 62/78] loop: " Christoph Hellwig
2020-11-20  7:38   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 63/78] bcache: remove a superflous lookup_bdev all Christoph Hellwig
2020-11-16 14:57 ` [PATCH 64/78] dm: simplify flush_bio initialization in __send_empty_flush Christoph Hellwig
2020-11-20  7:41   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 65/78] dm: remove the block_device reference in struct mapped_device Christoph Hellwig
2020-11-20  7:43   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 66/78] block: keep a block_device reference for each hd_struct Christoph Hellwig
2020-11-20  7:50   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 67/78] block: simplify the block device claiming interface Christoph Hellwig
2020-11-20  7:51   ` Hannes Reinecke
2020-11-16 14:57 ` [PATCH 68/78] block: remove ->bd_contains Christoph Hellwig
2020-11-20  7:52   ` Hannes Reinecke
2020-11-16 14:58 ` [PATCH 69/78] block: remove the nr_sects field in struct hd_struct Christoph Hellwig
2020-11-20  7:55   ` Hannes Reinecke
2020-11-16 14:58 ` [PATCH 70/78] block: replace bd_mutex with a per-gendisk mutex Christoph Hellwig
2020-11-20  7:58   ` Hannes Reinecke
2020-11-16 14:58 ` [PATCH 71/78] block: add a bdev_kobj helper Christoph Hellwig
2020-11-20  7:59   ` Hannes Reinecke
2020-11-16 14:58 ` [PATCH 72/78] block: use disk_part_iter_exit in disk_part_iter_next Christoph Hellwig
2020-11-20  7:59   ` Hannes Reinecke
2020-11-16 14:58 ` [PATCH 73/78] block: use put_device in put_disk Christoph Hellwig
2020-11-20  8:02   ` Hannes Reinecke
2020-11-16 14:58 ` [PATCH 74/78] block: merge struct block_device and struct hd_struct Christoph Hellwig
2020-11-20  8:58   ` Hannes Reinecke
2020-11-16 14:58 ` [PATCH 75/78] block: stop using bdget_disk for partition 0 Christoph Hellwig
2020-11-16 14:58 ` [PATCH 76/78] filemap: use ->f_mapping over ->i_mapping consistently Christoph Hellwig
2020-11-16 14:58 ` [PATCH 77/78] fs: simplify the get_super_thawed interface Christoph Hellwig
2020-11-16 14:58 ` [PATCH 78/78] block: remove i_bdev Christoph Hellwig
2020-11-16 15:05 ` cleanup updating the size of block devices v3 Christoph Hellwig
2020-11-16 15:40 ` 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=20201126011107.GA57352@google.com \
    --to=minchan@kernel.org \
    --cc=axboe@kernel.dk \
    --cc=ceph-devel@vger.kernel.org \
    --cc=dm-devel@redhat.com \
    --cc=drbd-dev@lists.linbit.com \
    --cc=hch@lst.de \
    --cc=idryomov@gmail.com \
    --cc=jasowang@redhat.com \
    --cc=jinpu.wang@cloud.ionos.com \
    --cc=josef@toxicpanda.com \
    --cc=justin@coraid.com \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-nvme@lists.infradead.org \
    --cc=linux-raid@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=mst@redhat.com \
    --cc=nbd@other.debian.org \
    --cc=pbonzini@redhat.com \
    --cc=roger.pau@citrix.com \
    --cc=sergey.senozhatsky@gmail.com \
    --cc=snitzer@redhat.com \
    --cc=song@kernel.org \
    --cc=stefanha@redhat.com \
    --cc=xen-devel@lists.xenproject.org \
    /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;
as well as URLs for NNTP newsgroup(s).