Skip to content
Permalink
Browse files
block: hold ->invalidate_lock in blkdev_fallocate
When running ->fallocate(), blkdev_fallocate() should hold
mapping->invalidate_lock to prevent page cache from being
accessed, otherwise stale data may be read in page cache.

Without this patch, blktests block/009 fails sometimes. With
this patch, block/009 can pass always.

Cc: Jan Kara <jack@suse.cz>
Signed-off-by: Ming Lei <ming.lei@redhat.com>
  • Loading branch information
Ming Lei authored and intel-lab-lkp committed Sep 15, 2021
1 parent e22ce8e commit 802c451a54927267d87a707d2a5fe3e47bc4ca1c
Showing 1 changed file with 11 additions and 7 deletions.
@@ -1699,7 +1699,8 @@ static const struct address_space_operations def_blk_aops = {
static long blkdev_fallocate(struct file *file, int mode, loff_t start,
loff_t len)
{
struct block_device *bdev = I_BDEV(bdev_file_inode(file));
struct inode *inode = bdev_file_inode(file);
struct block_device *bdev = I_BDEV(inode);
loff_t end = start + len - 1;
loff_t isize;
int error;
@@ -1726,10 +1727,12 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
if ((start | len) & (bdev_logical_block_size(bdev) - 1))
return -EINVAL;

filemap_invalidate_lock(inode->i_mapping);

/* Invalidate the page cache, including dirty pages. */
error = truncate_bdev_range(bdev, file->f_mode, start, end);
if (error)
return error;
goto fail;

switch (mode) {
case FALLOC_FL_ZERO_RANGE:
@@ -1746,17 +1749,18 @@ static long blkdev_fallocate(struct file *file, int mode, loff_t start,
GFP_KERNEL, 0);
break;
default:
return -EOPNOTSUPP;
error = -EOPNOTSUPP;
}
if (error)
return error;

/*
* Invalidate the page cache again; if someone wandered in and dirtied
* a page, we just discard it - userspace has no way of knowing whether
* the write happened before or after discard completing...
*/
return truncate_bdev_range(bdev, file->f_mode, start, end);
if (!error)
error = truncate_bdev_range(bdev, file->f_mode, start, end);
fail:
filemap_invalidate_unlock(inode->i_mapping);
return error;
}

const struct file_operations def_blk_fops = {

0 comments on commit 802c451

Please sign in to comment.