From: Tristan Madani jffs2_del_ino_cache() triggers BUG_ON(old->xref) when an inode cache entry still has xattr references. This can happen during unmount: generic_shutdown_super() calls evict_inodes() before put_super(), but jffs2_evict_inode -> jffs2_do_clear_inode -> jffs2_xattr_delete_inode only clears xrefs when pino_nlink == 0. For inodes with nlink > 0 at unmount time, xrefs survive past eviction, and the subsequent jffs2_del_ino_cache() hits the BUG_ON. Rather than freeing the xattr ref slabs at this point (which would leave dangling raw_node_refs reachable by GC), skip the inode cache deletion entirely when xrefs are still attached. The inode cache and its xrefs are then torn down together by jffs2_free_ino_caches() during jffs2_put_super(). Add a jffs2_ic_has_xrefs() helper that checks ic->xref when CONFIG_JFFS2_FS_XATTR is enabled, so the check compiles cleanly in both xattr and non-xattr configurations. Reported-by: syzbot+44664704c1494ad5f7a0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=44664704c1494ad5f7a0 Suggested-by: Richard Weinberger Fixes: aa98d7cf59b5 ("[JFFS2][XATTR] XATTR support on JFFS2 (version. 5)") Cc: stable@vger.kernel.org Signed-off-by: Tristan Madani --- Changes in v5: - Instead of freeing xattr ref slabs (which leaves dangling raw_node_refs for GC), skip the inode cache deletion when xrefs are still attached and let jffs2_free_ino_caches() tear down both at unmount. This avoids both the stale raw_node_ref issue and the sleeping-after-state-change race identified in v4 review (Richard Weinberger). Changes in v4: - Move jffs2_xattr_free_inode() call from jffs2_del_ino_cache() (which can be called under spinlock) to jffs2_do_clear_inode() (which holds only the inode mutex), avoiding sleeping-in-atomic (Richard Weinberger). - Use Haris Iqbal approach of placing the call just before jffs2_del_ino_cache() inside the nodes-exhausted check (Haris Iqbal). - Dropped patches 1/3 and 3/3 from the original series; this v4 is a standalone single patch replacing the entire series. Changes in v3: - Replace BUG_ON with jffs2_xattr_free_inode() call (v2 downgrade to WARN still triggered syzbot). Changes in v2: - Downgrade BUG_ON to WARN_ON_ONCE (dropped in v3). fs/jffs2/readinode.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/fs/jffs2/readinode.c b/fs/jffs2/readinode.c index 1caabff9dc910..5516779270255 100644 --- a/fs/jffs2/readinode.c +++ b/fs/jffs2/readinode.c @@ -1410,6 +1410,20 @@ int jffs2_do_crccheck_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *i return ret; } +/* + * xrefs point to their inode cache and GC reaches them via the raw node + * refs, so an inode cache with xrefs has to stay until + * jffs2_free_ino_caches() tears down both. + */ +static bool jffs2_ic_has_xrefs(struct jffs2_inode_cache *ic) +{ +#ifdef CONFIG_JFFS2_FS_XATTR + return ic->xref; +#else + return false; +#endif +} + void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f) { struct jffs2_full_dirent *fd, *fds; @@ -1439,7 +1453,8 @@ void jffs2_do_clear_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f) if (f->inocache && f->inocache->state != INO_STATE_CHECKING) { jffs2_set_inocache_state(c, f->inocache, INO_STATE_CHECKEDABSENT); - if (f->inocache->nodes == (void *)f->inocache) + if (f->inocache->nodes == (void *)f->inocache && + !jffs2_ic_has_xrefs(f->inocache)) jffs2_del_ino_cache(c, f->inocache); } -- 2.47.3