linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3] block: Discard page cache of zone reset target range
@ 2021-03-11  7:25 Shin'ichiro Kawasaki
  2021-03-11  8:20 ` Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Shin'ichiro Kawasaki @ 2021-03-11  7:25 UTC (permalink / raw)
  To: linux-block, Jens Axboe
  Cc: Damien Le Moal, Keith Busch, Jan Kara, Kanchan Joshi,
	Christoph Hellwig, Shin'ichiro Kawasaki

When zone reset ioctl and data read race for a same zone on zoned block
devices, the data read leaves stale page cache even though the zone
reset ioctl zero clears all the zone data on the device. To avoid
non-zero data read from the stale page cache after zone reset, discard
page cache of reset target zones in blkdev_zone_mgmt_ioctl(). Introduce
the helper function blkdev_truncate_zone_range() to discard the page
cache. Ensure the page cache discarded by calling the helper function
before and after zone reset in same manner as fallocate does.

This patch can be applied back to the stable kernel version v5.10.y.
Rework is needed for older stable kernels.

Signed-off-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com>
Fixes: 3ed05a987e0f ("blk-zoned: implement ioctls")
Cc: <stable@vger.kernel.org> # 5.10+
---
Changes from v2:
* Factored out blkdev_truncate_zone_range()

Changes from v1:
* Addressed comments on the list

 block/blk-zoned.c | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/block/blk-zoned.c b/block/blk-zoned.c
index 833978c02e60..03c3b8df2c0d 100644
--- a/block/blk-zoned.c
+++ b/block/blk-zoned.c
@@ -318,6 +318,22 @@ int blkdev_report_zones_ioctl(struct block_device *bdev, fmode_t mode,
 	return 0;
 }
 
+static int blkdev_truncate_zone_range(struct block_device *bdev, fmode_t mode,
+				      const struct blk_zone_range *zrange)
+{
+	loff_t start, end;
+
+	if (zrange->sector + zrange->nr_sectors <= zrange->sector ||
+	    zrange->sector + zrange->nr_sectors > get_capacity(bdev->bd_disk))
+		/* Out of range */
+		return -EINVAL;
+
+	start = zrange->sector << SECTOR_SHIFT;
+	end = ((zrange->sector + zrange->nr_sectors) << SECTOR_SHIFT) - 1;
+
+	return truncate_bdev_range(bdev, mode, start, end);
+}
+
 /*
  * BLKRESETZONE, BLKOPENZONE, BLKCLOSEZONE and BLKFINISHZONE ioctl processing.
  * Called from blkdev_ioctl.
@@ -329,6 +345,7 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
 	struct request_queue *q;
 	struct blk_zone_range zrange;
 	enum req_opf op;
+	int ret;
 
 	if (!argp)
 		return -EINVAL;
@@ -352,6 +369,11 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
 	switch (cmd) {
 	case BLKRESETZONE:
 		op = REQ_OP_ZONE_RESET;
+
+		/* Invalidate the page cache, including dirty pages. */
+		ret = blkdev_truncate_zone_range(bdev, mode, &zrange);
+		if (ret)
+			return ret;
 		break;
 	case BLKOPENZONE:
 		op = REQ_OP_ZONE_OPEN;
@@ -366,8 +388,20 @@ int blkdev_zone_mgmt_ioctl(struct block_device *bdev, fmode_t mode,
 		return -ENOTTY;
 	}
 
-	return blkdev_zone_mgmt(bdev, op, zrange.sector, zrange.nr_sectors,
-				GFP_KERNEL);
+	ret = blkdev_zone_mgmt(bdev, op, zrange.sector, zrange.nr_sectors,
+			       GFP_KERNEL);
+
+	/*
+	 * Invalidate the page cache again for zone reset: writes can only be
+	 * direct for zoned devices so concurrent writes would not add any page
+	 * to the page cache after/during reset. The page cache may be filled
+	 * again due to concurrent reads though and dropping the pages for
+	 * these is fine.
+	 */
+	if (!ret && cmd == BLKRESETZONE)
+		ret = blkdev_truncate_zone_range(bdev, mode, &zrange);
+
+	return ret;
 }
 
 static inline unsigned long *blk_alloc_zone_bitmap(int node,
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] block: Discard page cache of zone reset target range
  2021-03-11  7:25 [PATCH v3] block: Discard page cache of zone reset target range Shin'ichiro Kawasaki
@ 2021-03-11  8:20 ` Christoph Hellwig
  2021-03-11  8:26 ` Johannes Thumshirn
  2021-03-11 18:49 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Christoph Hellwig @ 2021-03-11  8:20 UTC (permalink / raw)
  To: Shin'ichiro Kawasaki
  Cc: linux-block, Jens Axboe, Damien Le Moal, Keith Busch, Jan Kara,
	Kanchan Joshi, Christoph Hellwig

Looks good,

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] block: Discard page cache of zone reset target range
  2021-03-11  7:25 [PATCH v3] block: Discard page cache of zone reset target range Shin'ichiro Kawasaki
  2021-03-11  8:20 ` Christoph Hellwig
@ 2021-03-11  8:26 ` Johannes Thumshirn
  2021-03-11 18:49 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Johannes Thumshirn @ 2021-03-11  8:26 UTC (permalink / raw)
  To: Shinichiro Kawasaki, linux-block, Jens Axboe
  Cc: Damien Le Moal, Keith Busch, Jan Kara, Kanchan Joshi, hch

Looks good,
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] block: Discard page cache of zone reset target range
  2021-03-11  7:25 [PATCH v3] block: Discard page cache of zone reset target range Shin'ichiro Kawasaki
  2021-03-11  8:20 ` Christoph Hellwig
  2021-03-11  8:26 ` Johannes Thumshirn
@ 2021-03-11 18:49 ` Jens Axboe
  2 siblings, 0 replies; 4+ messages in thread
From: Jens Axboe @ 2021-03-11 18:49 UTC (permalink / raw)
  To: Shin'ichiro Kawasaki, linux-block
  Cc: Damien Le Moal, Keith Busch, Jan Kara, Kanchan Joshi, Christoph Hellwig

On 3/11/21 12:25 AM, Shin'ichiro Kawasaki wrote:
> When zone reset ioctl and data read race for a same zone on zoned block
> devices, the data read leaves stale page cache even though the zone
> reset ioctl zero clears all the zone data on the device. To avoid
> non-zero data read from the stale page cache after zone reset, discard
> page cache of reset target zones in blkdev_zone_mgmt_ioctl(). Introduce
> the helper function blkdev_truncate_zone_range() to discard the page
> cache. Ensure the page cache discarded by calling the helper function
> before and after zone reset in same manner as fallocate does.
> 
> This patch can be applied back to the stable kernel version v5.10.y.
> Rework is needed for older stable kernels.

Applied, thanks.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-03-11 18:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-11  7:25 [PATCH v3] block: Discard page cache of zone reset target range Shin'ichiro Kawasaki
2021-03-11  8:20 ` Christoph Hellwig
2021-03-11  8:26 ` Johannes Thumshirn
2021-03-11 18:49 ` Jens Axboe

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).