generic_shutdown_super() clears SB_ACTIVE before calling evict_inodes(),
so fuse_evict_inode() skipped both the FUSE_FORGET for fi->nlookup and
fuse_cleanup_submount_lookup() for every remaining inode.

For a full unmount that is mostly fine: FUSE_DESTROY has already told the
server to drop everything.  But a submount unmount takes the same path
with the connection still alive: fuse_mount_remove() returns false, so
fuse_conn_destroy() is not called and no FUSE_DESTROY is sent.  The
server then keeps the lookup count of every inode of the submount for the
lifetime of the connection, which can be arbitrarily long if the parent
mount stays around and submounts come and go.

fuse_submount_lookup is worse, because it is freed nowhere else: the
object and its embedded forget are leaked on any unmount, including a
full one, for every inode that has one.

Queue both regardless of SB_ACTIVE.  This is safe on the paths where the
connection is already gone: both fuse_dev_queue_forget() and
virtio_fs_send_forget() just free the link when the queue is
disconnected, and fc->chan stays valid until fuse_free_conn(), well after
evict_inodes().

Fixes: c4d361f66ac9 ("fuse: share lookup state between submount and its parent")
Cc: <[email protected]>
Signed-off-by: Miklos Szeredi <[email protected]>
---
 fs/fuse/inode.c | 31 ++++++++++++++++++++-----------
 1 file changed, 20 insertions(+), 11 deletions(-)

diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index 344fcb55a64b..81ee6c96203c 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -141,6 +141,7 @@ static void fuse_cleanup_submount_lookup(struct fuse_conn 
*fc,
 static void fuse_evict_inode(struct inode *inode)
 {
        struct fuse_inode *fi = get_fuse_inode(inode);
+       struct fuse_conn *fc = get_fuse_conn(inode);
 
        /* Will write inode on close/munmap and in all other dirtiers */
        WARN_ON(inode_state_read_once(inode) & I_DIRTY_INODE);
@@ -157,19 +158,27 @@ static void fuse_evict_inode(struct inode *inode)
         */
        if (FUSE_IS_VDAX(inode))
                fuse_vdax_inode_cleanup(inode);
-       if (inode->i_sb->s_flags & SB_ACTIVE) {
-               struct fuse_conn *fc = get_fuse_conn(inode);
+       /*
+        * Must be done unconditionally as well: on a submount unmount the
+        * connection stays alive and no FUSE_DESTROY is sent, so without
+        * this the server keeps the lookup counts of every inode in the
+        * submount for the lifetime of the connection.  The
+        * fuse_submount_lookup object is freed nowhere else, so skipping it
+        * leaks that too.  If the connection is already gone (full unmount,
+        * failed mount) fuse_chan_queue_forget() just frees the forget.
+        */
+       if (fi->nlookup) {
+               fuse_chan_queue_forget(fc->chan, fi->forget, fi->nodeid,
+                                      fi->nlookup);
+               fi->forget = NULL;
+       }
 
-               if (fi->nlookup) {
-                       fuse_chan_queue_forget(fc->chan, fi->forget, fi->nodeid,
-                                              fi->nlookup);
-                       fi->forget = NULL;
-               }
+       if (fi->submount_lookup) {
+               fuse_cleanup_submount_lookup(fc, fi->submount_lookup);
+               fi->submount_lookup = NULL;
+       }
 
-               if (fi->submount_lookup) {
-                       fuse_cleanup_submount_lookup(fc, fi->submount_lookup);
-                       fi->submount_lookup = NULL;
-               }
+       if (inode->i_sb->s_flags & SB_ACTIVE) {
                /*
                 * Evict of non-deleted inode may race with outstanding
                 * LOOKUP/READDIRPLUS requests and result in inconsistency when
-- 
2.56.1


Reply via email to