6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ryusuke Konishi [ Upstream commit 66f4ad3ce158902e5f98afea93189972ed8750c2 ] Syzbot reported a kernel BUG triggered within nilfs_copy_dirty_pages(), which copies dirty DAT file folios/pages to its shadow page cache. The BUG occurs when a retrieved dirty folio/page unexpectedly loses its 'dirty' status. This issue arises because, since the commit referenced below, the 'dirty' flag of a folio/page can be cleared asynchronously after the filesystem detects metadata corruption and transitions to read-only mode. Resolve the issue by returning an -EROFS error if the filesystem has transitioned to read-only mode. Also change the behavior to issue a kernel warning only once instead of triggering a kernel BUG when this unexpected 'dirty' state is detected while the filesystem is not in read-only mode. Reported-by: syzbot+8baf9a79a3ffc6271cb6@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=8baf9a79a3ffc6271cb6 Fixes: 8c26c4e2694a ("nilfs2: fix issue with flush kernel thread after remount in RO mode because of driver's internal error or metadata corruption") Signed-off-by: Ryusuke Konishi Signed-off-by: Viacheslav Dubeyko Signed-off-by: Sasha Levin --- fs/nilfs2/page.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/fs/nilfs2/page.c b/fs/nilfs2/page.c index bf090240f6ede..89cd1510bf715 100644 --- a/fs/nilfs2/page.c +++ b/fs/nilfs2/page.c @@ -241,6 +241,7 @@ static void nilfs_copy_page(struct page *dst, struct page *src, int copy_dirty) int nilfs_copy_dirty_pages(struct address_space *dmap, struct address_space *smap) { + struct inode *smap_inode = smap->host; struct pagevec pvec; unsigned int i; pgoff_t index = 0; @@ -255,8 +256,19 @@ int nilfs_copy_dirty_pages(struct address_space *dmap, struct page *page = pvec.pages[i], *dpage; lock_page(page); - if (unlikely(!PageDirty(page))) - NILFS_PAGE_BUG(page, "inconsistent dirty state"); + if (unlikely(!PageDirty(page))) { + if (WARN_ONCE(!sb_rdonly(smap_inode->i_sb), + "inconsistent dirty state\n")) + goto unlock_src_page; + + /* + * If the filesystem has been forced to read-only + * due to metadata corruption. + */ + unlock_page(page); + err = -EROFS; + break; + } dpage = grab_cache_page(dmap, page->index); if (unlikely(!dpage)) { @@ -274,6 +286,7 @@ int nilfs_copy_dirty_pages(struct address_space *dmap, unlock_page(dpage); put_page(dpage); +unlock_src_page: unlock_page(page); } pagevec_release(&pvec); -- 2.53.0