From: Jan Kara <jack@suse.cz>
To: Ming Lei <ming.lei@redhat.com>
Cc: Jens Axboe <axboe@kernel.dk>,
linux-block@vger.kernel.org, Jan Kara <jack@suse.cz>
Subject: Re: [PATCH] block: hold ->invalidate_lock in blkdev_fallocate
Date: Thu, 16 Sep 2021 16:16:55 +0200 [thread overview]
Message-ID: <20210916141655.GI10610@quack2.suse.cz> (raw)
In-Reply-To: <20210915123545.1000534-1-ming.lei@redhat.com>
On Wed 15-09-21 20:35:45, Ming Lei wrote:
> 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>
Interesting. Looking at block/009 testcase I'm somewhat struggling to see
how it could trigger a situation where stale data would be seen. Do you
have some independent read accesses to the block device while the testcase
is running? That would explain it and yes, this patch should fix that.
BTW, you'd need to rebase this against current Linus' tree - Christoph has
moved this code to block/...
Also with the invalidate_lock held, there's no need for the second
truncate_bdev_range() call anymore. No pages can be created in the
discarded area while you are holding the invalidate_lock.
Honza
> ---
> fs/block_dev.c | 18 +++++++++++-------
> 1 file changed, 11 insertions(+), 7 deletions(-)
>
> diff --git a/fs/block_dev.c b/fs/block_dev.c
> index 45df6cbccf12..f55e14ae89a0 100644
> --- a/fs/block_dev.c
> +++ b/fs/block_dev.c
> @@ -1516,7 +1516,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;
> @@ -1543,10 +1544,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:
> @@ -1563,17 +1566,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 = {
> --
> 2.31.1
>
--
Jan Kara <jack@suse.com>
SUSE Labs, CR
next prev parent reply other threads:[~2021-09-16 14:16 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-15 12:35 [PATCH] block: hold ->invalidate_lock in blkdev_fallocate Ming Lei
2021-09-15 14:41 ` kernel test robot
2021-09-16 14:16 ` Jan Kara [this message]
2021-09-23 1:51 ` Ming Lei
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=20210916141655.GI10610@quack2.suse.cz \
--to=jack@suse.cz \
--cc=axboe@kernel.dk \
--cc=linux-block@vger.kernel.org \
--cc=ming.lei@redhat.com \
/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