FILESYSTEM IN USERSPACE (FUSE) development
 help / color / mirror / Atom feed
From: Miklos Szeredi <mszeredi@redhat.com>
To: fuse-devel@lists.linux.dev, linux-fsdevel@vger.kernel.org
Subject: [PATCH 31/32] fuse: alloc pqueue before installing fch in fuse_dev
Date: Thu, 16 Apr 2026 11:16:55 +0200	[thread overview]
Message-ID: <20260416091658.462783-32-mszeredi@redhat.com> (raw)
In-Reply-To: <20260416091658.462783-1-mszeredi@redhat.com>

Prior to this patchset, fuse_dev (containing fuse_pqueue) was allocated on
mount.  But now fuse_dev is allocated when opening /dev/fuse, even though
the queues are not needed at that time.

Delay allocation of the pqueue (4k worth of list_head) just before mounting
or cloning a device.

Various distributions (e.g. Debian/Fedora) configure /dev/fuse as world
writable, so the pqueue allocation should be deferred to a privileged
operation (mount) to prevent unprivileged userspace from consuming pinned
kernel memory.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 fs/fuse/dev.c        | 75 +++++++++++++++++++++++++++++++++-----------
 fs/fuse/fuse_dev_i.h |  3 ++
 2 files changed, 59 insertions(+), 19 deletions(-)

diff --git a/fs/fuse/dev.c b/fs/fuse/dev.c
index d6cd066a3fff..6fbe6084c5ae 100644
--- a/fs/fuse/dev.c
+++ b/fs/fuse/dev.c
@@ -328,6 +328,7 @@ void fuse_chan_release(struct fuse_chan *fch)
 void fuse_chan_free(struct fuse_chan *fch)
 {
 	WARN_ON(!list_empty(&fch->devices));
+	kfree(fch->pq_prealloc);
 	kfree(fch);
 }
 EXPORT_SYMBOL_GPL(fuse_chan_free);
@@ -354,15 +355,30 @@ struct fuse_chan *fuse_chan_new(void)
 }
 EXPORT_SYMBOL_GPL(fuse_chan_new);
 
+static struct list_head *fuse_pqueue_alloc(void)
+{
+	struct list_head *pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE);
+
+	if (pq) {
+		for (int i = 0; i < FUSE_PQ_HASH_SIZE; i++)
+			INIT_LIST_HEAD(&pq[i]);
+	}
+	return pq;
+}
+
 struct fuse_chan *fuse_dev_chan_new(void)
 {
-	struct fuse_chan *fch = fuse_chan_new();
+	struct fuse_chan *fch __free(kfree) = fuse_chan_new();
 	if (!fch)
 		return NULL;
 
+	fch->pq_prealloc = fuse_pqueue_alloc();
+	if (!fch->pq_prealloc)
+		return NULL;
+
 	fuse_iqueue_init(&fch->iq, &fuse_dev_fiq_ops, NULL);
 
-	return fch;
+	return no_free_ptr(fch);
 }
 EXPORT_SYMBOL_GPL(fuse_dev_chan_new);
 
@@ -403,39 +419,42 @@ void fuse_chan_io_uring_enable(struct fuse_chan *fch)
 
 void fuse_pqueue_init(struct fuse_pqueue *fpq)
 {
-	unsigned int i;
-
 	spin_lock_init(&fpq->lock);
-	for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
-		INIT_LIST_HEAD(&fpq->processing[i]);
 	INIT_LIST_HEAD(&fpq->io);
 	fpq->connected = 1;
+	fpq->processing = NULL;
 }
 
-struct fuse_dev *fuse_dev_alloc(void)
+static struct fuse_dev *fuse_dev_alloc_no_pq(void)
 {
 	struct fuse_dev *fud;
-	struct list_head *pq;
 
 	fud = kzalloc_obj(struct fuse_dev);
 	if (!fud)
 		return NULL;
 
 	refcount_set(&fud->ref, 1);
-	pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE);
-	if (!pq) {
-		kfree(fud);
-		return NULL;
-	}
-
-	fud->pq.processing = pq;
 	fuse_pqueue_init(&fud->pq);
 
 	return fud;
 }
+
+struct fuse_dev *fuse_dev_alloc(void)
+{
+	struct fuse_dev *fud __free(kfree) = fuse_dev_alloc_no_pq();
+	if (!fud)
+		return NULL;
+
+	fud->pq.processing = fuse_pqueue_alloc();
+	if (!fud->pq.processing)
+		return NULL;
+
+	return no_free_ptr(fud);
+}
 EXPORT_SYMBOL_GPL(fuse_dev_alloc);
 
-void fuse_dev_install(struct fuse_dev *fud, struct fuse_chan *fch)
+static void fuse_dev_install_with_pq(struct fuse_dev *fud, struct fuse_chan *fch,
+				     struct list_head *pq)
 {
 	struct fuse_chan *old_fch;
 
@@ -453,20 +472,33 @@ void fuse_dev_install(struct fuse_dev *fud, struct fuse_chan *fch)
 		 *  - it was set to disconneted
 		 */
 		fch->connected = 0;
+		kfree(pq);
 	} else {
+		if (pq) {
+			WARN_ON(fud->pq.processing);
+			fud->pq.processing = pq;
+		}
 		list_add_tail(&fud->entry, &fch->devices);
 		fuse_conn_get(fch->conn);
 		wake_up_all(&fuse_dev_waitq);
 	}
 	spin_unlock(&fch->lock);
 }
+
+void fuse_dev_install(struct fuse_dev *fud, struct fuse_chan *fch)
+{
+	struct list_head *pq = fch->pq_prealloc;
+
+	fch->pq_prealloc = NULL;
+	fuse_dev_install_with_pq(fud, fch, pq);
+}
 EXPORT_SYMBOL_GPL(fuse_dev_install);
 
 struct fuse_dev *fuse_dev_alloc_install(struct fuse_chan *fch)
 {
 	struct fuse_dev *fud;
 
-	fud = fuse_dev_alloc();
+	fud = fuse_dev_alloc_no_pq();
 	if (!fud)
 		return NULL;
 
@@ -1632,7 +1664,7 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
 
 static int fuse_dev_open(struct inode *inode, struct file *file)
 {
-	struct fuse_dev *fud = fuse_dev_alloc();
+	struct fuse_dev *fud = fuse_dev_alloc_no_pq();
 
 	if (!fud)
 		return -ENOMEM;
@@ -2231,6 +2263,7 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
 {
 	int oldfd;
 	struct fuse_dev *fud, *new_fud;
+	struct list_head *pq;
 
 	if (get_user(oldfd, argp))
 		return -EFAULT;
@@ -2254,7 +2287,11 @@ static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
 	if (fuse_dev_chan_get(new_fud))
 		return -EINVAL;
 
-	fuse_dev_install(new_fud, fud->chan);
+	pq = fuse_pqueue_alloc();
+	if (!pq)
+		return -ENOMEM;
+
+	fuse_dev_install_with_pq(new_fud, fud->chan, pq);
 
 	return 0;
 }
diff --git a/fs/fuse/fuse_dev_i.h b/fs/fuse/fuse_dev_i.h
index 3d945b20f8d5..f9364d4103d4 100644
--- a/fs/fuse/fuse_dev_i.h
+++ b/fs/fuse/fuse_dev_i.h
@@ -248,6 +248,9 @@ struct fuse_chan {
 	/* Maximum number of pages that can be used in a single request */
 	unsigned int max_pages;
 
+	/* Before being installed into fud, contains the preallocated pq array*/
+	struct list_head *pq_prealloc;
+
 	/** Connection aborted via sysfs, respond with ECONNABORTED on device I/O */
 	bool abort_with_err;
 
-- 
2.53.0


  parent reply	other threads:[~2026-04-16  9:17 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-16  9:16 [PATCH 00/32] fuse: improve transport and fs layer separation Miklos Szeredi
2026-04-16  9:16 ` [PATCH 01/32] fuse: move request timeout code to a new source file Miklos Szeredi
2026-04-16  9:16 ` [PATCH 02/32] fuse: add struct fuse_chan Miklos Szeredi
2026-04-16  9:16 ` [PATCH 03/32] fuse: move fuse_iqueue to fuse_chan Miklos Szeredi
2026-04-16  9:16 ` [PATCH 04/32] fuse: move fuse_dev and fuse_pqueue to dev.c Miklos Szeredi
2026-04-16  9:16 ` [PATCH 05/32] fuse: move 'devices' member from fuse_conn to fuse_chan Miklos Szeredi
2026-04-16  9:16 ` [PATCH 06/32] fuse: move background queuing related members " Miklos Szeredi
2026-04-22 17:53   ` Joanne Koong
2026-04-23  7:20     ` Miklos Szeredi
2026-04-16  9:16 ` [PATCH 07/32] fuse: move request blocking " Miklos Szeredi
2026-04-16  9:16 ` [PATCH 08/32] fuse: move io_uring " Miklos Szeredi
2026-04-16  9:16 ` [PATCH 09/32] fuse: move interrupt " Miklos Szeredi
2026-04-16  9:16 ` [PATCH 10/32] fuse: split off fch->lock from fc->lock Miklos Szeredi
2026-04-16  9:16 ` [PATCH 11/32] fuse: add back pointer from fuse_chan to fuse_conn Miklos Szeredi
2026-04-16  9:16 ` [PATCH 12/32] fuse: move request timeout to fuse_chan Miklos Szeredi
2026-04-16  9:16 ` [PATCH 13/32] fuse: move struct fuse_req and related to fuse_dev_i.h Miklos Szeredi
2026-04-16  9:16 ` [PATCH 14/32] fuse: don't access transport layer structs directly from the fs layer Miklos Szeredi
2026-04-16  9:16 ` [PATCH 15/32] fuse: move forget related struct and helpers Miklos Szeredi
2026-04-16  9:16 ` [PATCH 16/32] fuse: move fuse_dev_waitq to dev.c Miklos Szeredi
2026-04-16  9:16 ` [PATCH 17/32] fuse: remove #include "fuse_i.h" from "dev_uring_i.h" Miklos Szeredi
2026-04-16  9:16 ` [PATCH 18/32] fuse: remove #include "fuse_i.h" from "req_timeout.c" Miklos Szeredi
2026-04-16  9:16 ` [PATCH 19/32] fuse: abort related layering cleanup Miklos Szeredi
2026-04-16  9:16 ` [PATCH 20/32] fuse: split off fuse_args and related definitions into a separate header Miklos Szeredi
2026-04-17 21:52   ` Joanne Koong
2026-04-20 10:14     ` Miklos Szeredi
2026-04-16  9:16 ` [PATCH 21/32] fuse: remove fm arg of args->end callback Miklos Szeredi
2026-04-16  9:16 ` [PATCH 22/32] fuse: change req->fm to req->chan Miklos Szeredi
2026-04-16  9:16 ` [PATCH 23/32] fuse: split out filesystem part of request sending Miklos Szeredi
2026-04-16  9:16 ` [PATCH 24/32] fuse: change fud->fc to fud->chan Miklos Szeredi
2026-04-16  9:16 ` [PATCH 25/32] fuse: create poll.c Miklos Szeredi
2026-04-16  9:16 ` [PATCH 26/32] fuse: create notify.c Miklos Szeredi
2026-04-16  9:16 ` [PATCH 27/32] fuse: set params in fuse_chan_set_initialized() Miklos Szeredi
2026-04-22 17:41   ` Joanne Koong
2026-04-23  7:19     ` Miklos Szeredi
2026-04-16  9:16 ` [PATCH 28/32] fuse: remove fuse_mutex protection from fuse_dev_ioctl_sync_init() Miklos Szeredi
2026-04-16  9:16 ` [PATCH 29/32] fuse: change ring->fc to ring->chan Miklos Szeredi
2026-04-16  9:16 ` [PATCH 30/32] fuse: remove #include "fuse_i.h" from dev.c and dev_uring.c Miklos Szeredi
2026-04-16  9:16 ` Miklos Szeredi [this message]
2026-04-22 19:30   ` [PATCH 31/32] fuse: alloc pqueue before installing fch in fuse_dev Joanne Koong
2026-04-16  9:16 ` [PATCH 32/32] fuse: simplify fuse_dev_ioctl_clone() Miklos Szeredi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260416091658.462783-32-mszeredi@redhat.com \
    --to=mszeredi@redhat.com \
    --cc=fuse-devel@lists.linux.dev \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox