Skip to content

Commit 35e4c6c

Browse files
kawasakiaxboe
authored andcommitted
block: Hold invalidate_lock in BLKZEROOUT ioctl
When BLKZEROOUT ioctl and data read race, the data read leaves stale page cache. To avoid the stale page cache, hold invalidate_lock of the block device file mapping. The stale page cache is observed when blktests test case block/009 is modified to call "blkdiscard -z" command and repeated hundreds of times. This patch can be applied back to the stable kernel version v5.15.y. Rework is required for older stable kernels. Fixes: 22dd6d3 ("block: invalidate the page cache when issuing BLKZEROOUT") Signed-off-by: Shin'ichiro Kawasaki <shinichiro.kawasaki@wdc.com> Cc: stable@vger.kernel.org # v5.15 Reviewed-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20211109104723.835533-3-shinichiro.kawasaki@wdc.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent 7607c44 commit 35e4c6c

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

block/ioctl.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
154154
{
155155
uint64_t range[2];
156156
uint64_t start, end, len;
157+
struct inode *inode = bdev->bd_inode;
157158
int err;
158159

159160
if (!(mode & FMODE_WRITE))
@@ -176,12 +177,17 @@ static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,
176177
return -EINVAL;
177178

178179
/* Invalidate the page cache, including dirty pages */
180+
filemap_invalidate_lock(inode->i_mapping);
179181
err = truncate_bdev_range(bdev, mode, start, end);
180182
if (err)
181-
return err;
183+
goto fail;
184+
185+
err = blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
186+
BLKDEV_ZERO_NOUNMAP);
182187

183-
return blkdev_issue_zeroout(bdev, start >> 9, len >> 9, GFP_KERNEL,
184-
BLKDEV_ZERO_NOUNMAP);
188+
fail:
189+
filemap_invalidate_unlock(inode->i_mapping);
190+
return err;
185191
}
186192

187193
static int put_ushort(unsigned short __user *argp, unsigned short val)

0 commit comments

Comments
 (0)