diff mbox series

btrfs: Add assert or condition

Message ID 20240831002222.2275740-1-lizhi.xu@windriver.com (mailing list archive)
State New, archived
Headers show
Series btrfs: Add assert or condition | expand

Commit Message

Lizhi Xu Aug. 31, 2024, 12:22 a.m. UTC
When the value of fsync_skip_inode_lock is true, i_mmap_lock is used,
so add it or condition in the ASSERT. 

Reported-and-tested-by: syzbot+4704b3cc972bd76024f1@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=4704b3cc972bd76024f1
Signed-off-by: Lizhi Xu <lizhi.xu@windriver.com>
---
 fs/btrfs/ordered-data.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Filipe Manana Aug. 31, 2024, 10:55 a.m. UTC | #1
On Sat, Aug 31, 2024 at 1:36 AM Lizhi Xu <lizhi.xu@windriver.com> wrote:
>
> When the value of fsync_skip_inode_lock is true, i_mmap_lock is used,
> so add it or condition in the ASSERT.
>
> Reported-and-tested-by: syzbot+4704b3cc972bd76024f1@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=4704b3cc972bd76024f1
> Signed-off-by: Lizhi Xu <lizhi.xu@windriver.com>
> ---
>  fs/btrfs/ordered-data.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
> index 82a68394a89c..d0187e1fb941 100644
> --- a/fs/btrfs/ordered-data.c
> +++ b/fs/btrfs/ordered-data.c
> @@ -1015,7 +1015,8 @@ void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
>  {
>         struct rb_node *n;
>
> -       ASSERT(inode_is_locked(&inode->vfs_inode));
> +       ASSERT(inode_is_locked(&inode->vfs_inode) ||
> +              rwsem_is_locked(&inode->i_mmap_lock));

This definitely fixes the syzbot report, in the sense the assertion
won't fail anymore.
But it's wrong, very, very, very, very wrong.

The inode must be locked during the course of the fsync, that's why
the assertion is there.

Why do you think it's ok to not have the inode locked?
Have you done any analysis about that?

You mention "fsync_skip_inode_lock is true" in the changelog, but have
you checked where and why it's set to true?

Where we set it to true, at btrfs_direct_write(), there's a comment
which explains it's to avoid a deadlock on the inode lock at
btrfs_sync_file().

This is a perfect example of trying a patch not only without having
any idea how the code works but also being very lazy,
as there's a very explicit comment in the code about why the variable
is set to true, and even much more detailed in the
change log of the commit that introduced it.

And btw, there's already a patch to fix this issue:

https://lore.kernel.org/linux-btrfs/717029440fe379747b9548a9c91eb7801bc5a813.1724972507.git.fdmanana@suse.com/

>
>         spin_lock_irq(&inode->ordered_tree_lock);
>         for (n = rb_first(&inode->ordered_tree); n; n = rb_next(n)) {
> --
> 2.43.0
>
>
Lizhi Xu Aug. 31, 2024, 12:41 p.m. UTC | #2
On Sat, 31 Aug 2024 11:55:53 +0100, Filipe Manana wrote:
> > -       ASSERT(inode_is_locked(&inode->vfs_inode));
> > +       ASSERT(inode_is_locked(&inode->vfs_inode) ||
> > +              rwsem_is_locked(&inode->i_mmap_lock));
> 
> This definitely fixes the syzbot report, in the sense the assertion
> won't fail anymore.
> But it's wrong, very, very, very, very wrong.
> 
> The inode must be locked during the course of the fsync, that's why
> the assertion is there.
> 
> Why do you think it's ok to not have the inode locked?
> Have you done any analysis about that?
> 
> You mention "fsync_skip_inode_lock is true" in the changelog, but have
> you checked where and why it's set to true?
> 
> Where we set it to true, at btrfs_direct_write(), there's a comment
> which explains it's to avoid a deadlock on the inode lock at
> btrfs_sync_file().
> 
> This is a perfect example of trying a patch not only without having
> any idea how the code works but also being very lazy,
> as there's a very explicit comment in the code about why the variable
> is set to true, and even much more detailed in the
> change log of the commit that introduced it.
> 
> And btw, there's already a patch to fix this issue:
> 
> https://lore.kernel.org/linux-btrfs/717029440fe379747b9548a9c91eb7801bc5a813.1724972507.git.fdmanana@suse.com/
In your patch, I get what the mean of fsync_skip_inode_lock.

Thanks.
diff mbox series

Patch

diff --git a/fs/btrfs/ordered-data.c b/fs/btrfs/ordered-data.c
index 82a68394a89c..d0187e1fb941 100644
--- a/fs/btrfs/ordered-data.c
+++ b/fs/btrfs/ordered-data.c
@@ -1015,7 +1015,8 @@  void btrfs_get_ordered_extents_for_logging(struct btrfs_inode *inode,
 {
 	struct rb_node *n;
 
-	ASSERT(inode_is_locked(&inode->vfs_inode));
+	ASSERT(inode_is_locked(&inode->vfs_inode) ||
+	       rwsem_is_locked(&inode->i_mmap_lock));
 
 	spin_lock_irq(&inode->ordered_tree_lock);
 	for (n = rb_first(&inode->ordered_tree); n; n = rb_next(n)) {