]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
nilfs2: fix WARNING in mark_buffer_dirty due to discarded buffer reuse
authorRyusuke Konishi <konishi.ryusuke@gmail.com>
Fri, 18 Aug 2023 13:18:04 +0000 (22:18 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 23 Sep 2023 08:46:52 +0000 (10:46 +0200)
commit cdaac8e7e5a059f9b5e816cda257f08d0abffacd upstream.

A syzbot stress test using a corrupted disk image reported that
mark_buffer_dirty() called from __nilfs_mark_inode_dirty() or
nilfs_palloc_commit_alloc_entry() may output a kernel warning, and can
panic if the kernel is booted with panic_on_warn.

This is because nilfs2 keeps buffer pointers in local structures for some
metadata and reuses them, but such buffers may be forcibly discarded by
nilfs_clear_dirty_page() in some critical situations.

This issue is reported to appear after commit 28a65b49eb53 ("nilfs2: do
not write dirty data after degenerating to read-only"), but the issue has
potentially existed before.

Fix this issue by checking the uptodate flag when attempting to reuse an
internally held buffer, and reloading the metadata instead of reusing the
buffer if the flag was lost.

Link: https://lkml.kernel.org/r/20230818131804.7758-1-konishi.ryusuke@gmail.com
Signed-off-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Reported-by: syzbot+cdfcae656bac88ba0e2d@syzkaller.appspotmail.com
Closes: https://lkml.kernel.org/r/0000000000003da75f05fdeffd12@google.com
Fixes: 8c26c4e2694a ("nilfs2: fix issue with flush kernel thread after remount in RO mode because of driver's internal error or metadata corruption")
Tested-by: Ryusuke Konishi <konishi.ryusuke@gmail.com>
Cc: <stable@vger.kernel.org> # 3.10+
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/nilfs2/alloc.c
fs/nilfs2/inode.c

index 03b8ba933eb2a3dcc781eaf9e57594da5a40f48b..095cf8b5049a0741537fdca32df0930158382a89 100644 (file)
@@ -214,7 +214,8 @@ static int nilfs_palloc_get_block(struct inode *inode, unsigned long blkoff,
        int ret;
 
        spin_lock(lock);
-       if (prev->bh && blkoff == prev->blkoff) {
+       if (prev->bh && blkoff == prev->blkoff &&
+           likely(buffer_uptodate(prev->bh))) {
                get_bh(prev->bh);
                *bhp = prev->bh;
                spin_unlock(lock);
index bc5f447d93bd0e7598c8356c0e9a7f2be2aa323f..7fe5b55b647653849db808a2cc67df16d1b3e47a 100644 (file)
@@ -1045,7 +1045,7 @@ int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
        int err;
 
        spin_lock(&nilfs->ns_inode_lock);
-       if (ii->i_bh == NULL) {
+       if (ii->i_bh == NULL || unlikely(!buffer_uptodate(ii->i_bh))) {
                spin_unlock(&nilfs->ns_inode_lock);
                err = nilfs_ifile_get_inode_block(ii->i_root->ifile,
                                                  inode->i_ino, pbh);
@@ -1054,7 +1054,10 @@ int nilfs_load_inode_block(struct inode *inode, struct buffer_head **pbh)
                spin_lock(&nilfs->ns_inode_lock);
                if (ii->i_bh == NULL)
                        ii->i_bh = *pbh;
-               else {
+               else if (unlikely(!buffer_uptodate(ii->i_bh))) {
+                       __brelse(ii->i_bh);
+                       ii->i_bh = *pbh;
+               } else {
                        brelse(*pbh);
                        *pbh = ii->i_bh;
                }