Linux virtualization list
 help / color / mirror / Atom feed
* [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount
@ 2026-11-30 14:37 Miklos Szeredi
  2026-11-30 14:37 ` [PATCH v2 2/4] virtiofs: don't abort DAX removemapping on allocation failure Miklos Szeredi
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Miklos Szeredi @ 2026-11-30 14:37 UTC (permalink / raw)
  To: fuse-devel, virtualization; +Cc: Li Wang, German Maglione, Stefan Hajnoczi

generic_shutdown_super() clears SB_ACTIVE before calling evict_inodes(),
so at unmount time fuse_evict_inode() skipped fuse_vdax_inode_cleanup()
for every remaining inode.  The DAX ranges owned by those inodes stay on
fc->vdax->busy_ranges instead of being returned to the free pool, and
fuse_vdax_conn_free() only frees free_ranges before kfree()ing the
connection, so the busy fuse_dax_mapping objects are leaked.  Nothing
warns about it: fuse_free_inode() kfree()s fi->vdax without checking
that the interval tree is empty.

Call fuse_vdax_inode_cleanup() regardless of SB_ACTIVE.  That is what
virtio_fs_conn_destroy() has always assumed ("Soon evict_inodes() will
be called which will free all memory ranges belonging to all inodes").
Unlike the FORGET in the same block, which is pointless once DESTROY has
been sent, the DAX cleanup is the only thing there that owns memory.

On a full unmount virtio_fs_conn_destroy() has already run by the time
evict_inodes() is called, so the FUSE_REMOVEMAPPING requests sent by
fuse_vdax_inode_cleanup() fail with -ENOTCONN, which
inode_reclaim_dmap_range() ignores.  On a submount unmount the
connection is still alive and those requests are both valid and needed.

Fixes: c2d0ad00d948 ("virtiofs: implement dax read/write operations")
Cc: <stable@vger.kernel.org>
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-3b38f8c0-5e4a-4c21-87a6-4f96cc3cf147
Reviewed-by: Li Wang <liwang@kylinos.cn>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 fs/fuse/inode.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
index 20fbc99f3590..344fcb55a64b 100644
--- a/fs/fuse/inode.c
+++ b/fs/fuse/inode.c
@@ -150,11 +150,16 @@ static void fuse_evict_inode(struct inode *inode)
 
 	truncate_inode_pages_final(&inode->i_data);
 	clear_inode(inode);
+	/*
+	 * Must be done unconditionally: the DAX ranges owned by this inode
+	 * are freed nowhere else, so skipping this on an inactive
+	 * superblock would leak them.
+	 */
+	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);
 
-		if (FUSE_IS_VDAX(inode))
-			fuse_vdax_inode_cleanup(inode);
 		if (fi->nlookup) {
 			fuse_chan_queue_forget(fc->chan, fi->forget, fi->nodeid,
 					       fi->nlookup);
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 2/4] virtiofs: don't abort DAX removemapping on allocation failure
  2026-11-30 14:37 [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount Miklos Szeredi
@ 2026-11-30 14:37 ` Miklos Szeredi
  2026-11-30 14:37 ` [PATCH v2 3/4] virtiofs: retry request submission from the worker on -ENOMEM Miklos Szeredi
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Miklos Szeredi @ 2026-11-30 14:37 UTC (permalink / raw)
  To: fuse-devel, virtualization; +Cc: Li Wang, German Maglione, Stefan Hajnoczi

dmap_removemapping_list() allocates an array of fuse_removemapping_one
entries to batch the FUSE_REMOVEMAPPING request, and returns -ENOMEM if
that fails.

The batch array is only a convenience: dmap_removemapping_one() already
sends a single entry entirely from the stack.  Move it above
dmap_removemapping_list() and use it to remove the ranges one at a time
when the array cannot be allocated.  That is slower, but it cannot fail
for lack of memory, and it only happens under memory pressure.

While at it, initialize ret, which was left uninitialized if the list
passed in was empty.  The current caller checks for that beforehand, so
this is not a bug today.

Fixes: c2d0ad00d948 ("virtiofs: implement dax read/write operations")
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 fs/fuse/dax.c | 49 +++++++++++++++++++++++++++++++------------------
 1 file changed, 31 insertions(+), 18 deletions(-)

diff --git a/fs/fuse/dax.c b/fs/fuse/dax.c
index e20a5ae6fecc..c5bbb5d91026 100644
--- a/fs/fuse/dax.c
+++ b/fs/fuse/dax.c
@@ -248,18 +248,46 @@ static int fuse_send_removemapping(struct inode *inode,
 	return fuse_simple_request(fm, &args);
 }
 
+static int dmap_removemapping_one(struct inode *inode,
+				  struct fuse_dax_mapping *dmap)
+{
+	struct fuse_removemapping_one forget_one;
+	struct fuse_removemapping_in inarg;
+
+	memset(&inarg, 0, sizeof(inarg));
+	inarg.count = 1;
+	memset(&forget_one, 0, sizeof(forget_one));
+	forget_one.moffset = dmap->window_offset;
+	forget_one.len = dmap->length;
+
+	return fuse_send_removemapping(inode, &inarg, &forget_one);
+}
+
 static int dmap_removemapping_list(struct inode *inode, unsigned int num,
 				   struct list_head *to_remove)
 {
 	struct fuse_removemapping_one *remove_one, *ptr;
 	struct fuse_removemapping_in inarg;
 	struct fuse_dax_mapping *dmap;
-	int ret, i = 0, nr_alloc;
+	int ret = 0, i = 0, nr_alloc;
 
 	nr_alloc = min_t(unsigned int, num, FUSE_REMOVEMAPPING_MAX_ENTRY);
 	remove_one = kmalloc_objs(*remove_one, nr_alloc, GFP_NOFS);
-	if (!remove_one)
-		return -ENOMEM;
+	if (!remove_one) {
+		/*
+		 * The ranges are not reusable until the server has dropped
+		 * the mappings, and the request is never retried, so failing
+		 * here would take them out of use for good.  Fall back to
+		 * removing them one at a time, which needs no allocation;
+		 * slow, but this only happens under memory pressure.
+		 */
+		list_for_each_entry(dmap, to_remove, list) {
+			ret = dmap_removemapping_one(inode, dmap);
+			if (ret)
+				break;
+		}
+		return ret;
+	}
 
 	ptr = remove_one;
 	list_for_each_entry(dmap, to_remove, list) {
@@ -350,21 +378,6 @@ static void inode_reclaim_dmap_range(struct fuse_conn_vdax *fcd,
 	spin_unlock(&fcd->lock);
 }
 
-static int dmap_removemapping_one(struct inode *inode,
-				  struct fuse_dax_mapping *dmap)
-{
-	struct fuse_removemapping_one forget_one;
-	struct fuse_removemapping_in inarg;
-
-	memset(&inarg, 0, sizeof(inarg));
-	inarg.count = 1;
-	memset(&forget_one, 0, sizeof(forget_one));
-	forget_one.moffset = dmap->window_offset;
-	forget_one.len = dmap->length;
-
-	return fuse_send_removemapping(inode, &inarg, &forget_one);
-}
-
 /*
  * It is called from evict_inode() and by that time inode is going away. So
  * this function does not take any locks like fi->vdax->sem for traversing
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 3/4] virtiofs: retry request submission from the worker on -ENOMEM
  2026-11-30 14:37 [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount Miklos Szeredi
  2026-11-30 14:37 ` [PATCH v2 2/4] virtiofs: don't abort DAX removemapping on allocation failure Miklos Szeredi
@ 2026-11-30 14:37 ` Miklos Szeredi
  2026-11-30 14:37 ` [PATCH v2 4/4] virtiofs: fix forget leak on submount unmount Miklos Szeredi
  2026-11-30 14:54 ` [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount sashiko-bot
  3 siblings, 0 replies; 6+ messages in thread
From: Miklos Szeredi @ 2026-11-30 14:37 UTC (permalink / raw)
  To: fuse-devel, virtualization; +Cc: Li Wang, German Maglione, Stefan Hajnoczi

virtio_fs_send_req() enqueues with GFP_ATOMIC, because it may be called
with fc->chan->bg_lock held.  The sg arrays and, via
copy_args_to_argbuf(), the bounce buffer are allocated with those flags,
so under memory pressure a request can be failed with -ENOMEM even
though nothing is wrong with it.  The buffer is as large as the request
arguments, so this is not limited to tiny allocations:
FUSE_REMOVEMAPPING, for example, can ask for just over 4 KiB, an order-1
allocation out of the atomic reserves with no reclaim.

A virtqueue that is merely full is already handled gracefully: -ENOSPC
puts the request on fsvq->queued_reqs and virtio_fs_request_dispatch_work()
retries it with GFP_KERNEL under memalloc_nofs_save().  A failed atomic
allocation is just as transient, so treat it the same way instead of
ending the request.  Unlike -ENOSPC there is no completion to kick the
worker afterwards, so schedule it here.

virtqueue_add_sgs() can also report -ENOMEM, but only for a DMA mapping
failure, which the retry will not fix (failure to allocate the indirect
descriptor table falls back to direct descriptors and does not return
-ENOMEM).  Such a request is retried once and then ended with -ENOMEM by
the worker, so the two cases are not worth telling apart.

virtio_fs_enqueue_req() queues on -ENOSPC only after checking
fsvq->connected under fsvq->lock; do the same here and fail the request
with -ENOTCONN if the queue is gone.  req->argbuf has already been freed
and cleared on the error path, so the retry reallocates it.

Fixes: a62a8ef9d97d ("virtio-fs: add virtiofs filesystem")
Reviewed-by: Li Wang <liwang@kylinos.cn>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 fs/fuse/virtio_fs.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index 8bd63c6165d2..6ddb2f181a16 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -1586,6 +1586,23 @@ static void virtio_fs_send_req(struct fuse_iqueue *fiq, struct fuse_req *req)
 			 */
 			return;
 		}
+		if (ret == -ENOMEM) {
+			/*
+			 * Only the GFP_ATOMIC allocation failed.  Hand the
+			 * request to the dispatch worker, which retries with
+			 * GFP_KERNEL, rather than failing it outright.
+			 */
+			spin_lock(&fsvq->lock);
+			if (fsvq->connected) {
+				list_add_tail(&req->list, &fsvq->queued_reqs);
+				inc_in_flight_req(fsvq);
+				schedule_work(&fsvq->dispatch_work);
+				spin_unlock(&fsvq->lock);
+				return;
+			}
+			spin_unlock(&fsvq->lock);
+			ret = -ENOTCONN;
+		}
 		req->out.h.error = ret;
 		pr_err("virtio-fs: virtio_fs_enqueue_req() failed %d\n", ret);
 
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v2 4/4] virtiofs: fix forget leak on submount unmount
  2026-11-30 14:37 [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount Miklos Szeredi
  2026-11-30 14:37 ` [PATCH v2 2/4] virtiofs: don't abort DAX removemapping on allocation failure Miklos Szeredi
  2026-11-30 14:37 ` [PATCH v2 3/4] virtiofs: retry request submission from the worker on -ENOMEM Miklos Szeredi
@ 2026-11-30 14:37 ` Miklos Szeredi
  2026-11-30 14:54   ` sashiko-bot
  2026-11-30 14:54 ` [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount sashiko-bot
  3 siblings, 1 reply; 6+ messages in thread
From: Miklos Szeredi @ 2026-11-30 14:37 UTC (permalink / raw)
  To: fuse-devel, virtualization; +Cc: Li Wang, German Maglione, Stefan Hajnoczi

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: <stable@vger.kernel.org>
Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 4/4] virtiofs: fix forget leak on submount unmount
  2026-11-30 14:37 ` [PATCH v2 4/4] virtiofs: fix forget leak on submount unmount Miklos Szeredi
@ 2026-11-30 14:54   ` sashiko-bot
  0 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-30 14:54 UTC (permalink / raw)
  To: Miklos Szeredi, virtualization; +Cc: Michael S. Tsirkin, Eugenio Perez

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] fuse: forget leak and premature FORGET in fuse_evict_inode()
  on failed mount
--

commit 6888c05c6b67c7c443bc776bdec5c1257aae460e
Author: Miklos Szeredi <mszeredi@redhat.com>

virtiofs: fix forget leak on submount unmount

This commit moves the queuing of fi->forget and cleanup of
fi->submount_lookup in fuse_evict_inode() outside the SB_ACTIVE check so
that submount unmounts do not skip them.

> diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c
> index eff1c05681e6b..613c203dcd506 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;
> +	}

[Severity: Medium]
Does this leak fi->forget when fuse_fill_super_common() fails after
allocating the root inode?

In fuse_fill_super_common(), fuse_get_root_inode() creates the root inode
with fi->nlookup = 1 while sb->s_root is still NULL and
fc->chan->iq.connected is already 1.  If d_make_root(),
fuse_dev_is_installed(), or fuse_ctl_add_conn() then fails, evicting the
root inode queues fi->forget onto fc->chan->iq.forget_list_tail and clears
fi->forget = NULL instead of letting fuse_free_inode() free it.  During
superblock teardown, fuse_sb_destroy() skips fuse_conn_destroy() and
fuse_chan_abort() because sb->s_root is NULL:

fs/fuse/inode.c:fuse_sb_destroy() {
	...
	if (sb->s_root) {
		last = fuse_mount_remove(fm);
		if (last)
			fuse_conn_destroy(fm);
	}
}

As a result, fi->forget is never drained from forget_list_head before
fc->chan is freed.  On virtiofs, where the virtqueues are already
connected at probe time, the same failure path sends a FUSE_FORGET for
FUSE_ROOT_ID to the server before FUSE_INIT.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130143728.326871-1-mszeredi@redhat.com?part=4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount
  2026-11-30 14:37 [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount Miklos Szeredi
                   ` (2 preceding siblings ...)
  2026-11-30 14:37 ` [PATCH v2 4/4] virtiofs: fix forget leak on submount unmount Miklos Szeredi
@ 2026-11-30 14:54 ` sashiko-bot
  3 siblings, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-30 14:54 UTC (permalink / raw)
  To: Miklos Szeredi, virtualization; +Cc: Michael S. Tsirkin, Eugenio Perez

Sashiko has finished reviewing this 4-patch series:

- Patches 1-3: no issues found.
- Patch 4 ("virtiofs: fix forget leak on submount unmount"): 1 potential
  issue (1 Medium)

A detailed review has been sent as a separate reply to patch 4.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130143728.326871-1-mszeredi@redhat.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-30 14:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 14:37 [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount Miklos Szeredi
2026-11-30 14:37 ` [PATCH v2 2/4] virtiofs: don't abort DAX removemapping on allocation failure Miklos Szeredi
2026-11-30 14:37 ` [PATCH v2 3/4] virtiofs: retry request submission from the worker on -ENOMEM Miklos Szeredi
2026-11-30 14:37 ` [PATCH v2 4/4] virtiofs: fix forget leak on submount unmount Miklos Szeredi
2026-11-30 14:54   ` sashiko-bot
2026-11-30 14:54 ` [PATCH v2 1/4] virtiofs: fix DAX range leak on unmount sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox