Skip to content

Commit 7607c44

Browse files
kawasakiaxboe
authored andcommitted
block: Hold invalidate_lock in BLKDISCARD ioctl
When BLKDISCARD 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 repeated hundreds of times. This patch can be applied back to the stable kernel version v5.15.y with slight patch edit. Rework is required for older stable kernels. Fixes: 351499a ("block: Invalidate cache on discard v2") 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-2-shinichiro.kawasaki@wdc.com Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent cb690f5 commit 7607c44

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
@@ -113,6 +113,7 @@ static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
113113
uint64_t range[2];
114114
uint64_t start, len;
115115
struct request_queue *q = bdev_get_queue(bdev);
116+
struct inode *inode = bdev->bd_inode;
116117
int err;
117118

118119
if (!(mode & FMODE_WRITE))
@@ -135,12 +136,17 @@ static int blk_ioctl_discard(struct block_device *bdev, fmode_t mode,
135136
if (start + len > bdev_nr_bytes(bdev))
136137
return -EINVAL;
137138

139+
filemap_invalidate_lock(inode->i_mapping);
138140
err = truncate_bdev_range(bdev, mode, start, start + len - 1);
139141
if (err)
140-
return err;
142+
goto fail;
143+
144+
err = blkdev_issue_discard(bdev, start >> 9, len >> 9,
145+
GFP_KERNEL, flags);
141146

142-
return blkdev_issue_discard(bdev, start >> 9, len >> 9,
143-
GFP_KERNEL, flags);
147+
fail:
148+
filemap_invalidate_unlock(inode->i_mapping);
149+
return err;
144150
}
145151

146152
static int blk_ioctl_zeroout(struct block_device *bdev, fmode_t mode,

0 commit comments

Comments
 (0)