Skip to contentSkip to conversation

fs/fuse: fix potential memory leak from fuse_uring_cancel

PATCH Superseded Jian Huang Li 3 participants 4 messages Sep 9, 2025

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fs/fuse: fix potential memory leak from fuse_uring_cancel

fuse: fix potential memory leak from fuse_uring_cancel

If umount or fuse daemon quits at early stage, could happen all ring queues have already stopped and later some FUSE_IO_URING_CMD_REGISTER commands get canceled, that leaves ring entities in ent_in_userspace list and will not be freed by fuse_uring_destruct.
Move such ring entities to ent_canceled list and ensure fuse_uring_destruct frees these ring entities.

Fixes b6236c8407cb ("fuse: {io-uring} Prevent mount point hang on fuse-server termination")
---
 fs/fuse/dev_uring.c   | 13 +++++++++++--
 fs/fuse/dev_uring_i.h |  6 ++++++
 2 files changed, 17 insertions(+), 2 deletions(-)
fs/fuse/dev_uring.c +11 -2
index 249b210becb1..db35797853c1 100644
@@ -203,6 +203,12 @@ void fuse_uring_destruct(struct fuse_conn *fc)
 		WARN_ON(!list_empty(&queue->ent_commit_queue));
 		WARN_ON(!list_empty(&queue->ent_in_userspace));
 
+		list_for_each_entry_safe(ent, next, &queue->ent_canceled,
+					 list) {
+			list_del_init(&ent->list);
+			kfree(ent);
+		}
+
 		list_for_each_entry_safe(ent, next, &queue->ent_released,
 					 list) {
 			list_del_init(&ent->list);
@@ -291,6 +297,7 @@ static struct fuse_ring_queue *fuse_uring_create_queue(struct fuse_ring *ring,
 	INIT_LIST_HEAD(&queue->ent_in_userspace);
 	INIT_LIST_HEAD(&queue->fuse_req_queue);
 	INIT_LIST_HEAD(&queue->fuse_req_bg_queue);
+	INIT_LIST_HEAD(&queue->ent_canceled);
 	INIT_LIST_HEAD(&queue->ent_released);
 
 	queue->fpq.processing = pq;
@@ -391,6 +398,8 @@ static void fuse_uring_teardown_entries(struct fuse_ring_queue *queue)
 {
 	fuse_uring_stop_list_entries(&queue->ent_in_userspace, queue,
 				     FRRS_USERSPACE);
+	fuse_uring_stop_list_entries(&queue->ent_canceled, queue,
+				     FRRS_CANCELED);
 	fuse_uring_stop_list_entries(&queue->ent_avail_queue, queue,
 				     FRRS_AVAILABLE);
 }
@@ -509,8 +518,8 @@ static void fuse_uring_cancel(struct io_uring_cmd *cmd,
 	queue = ent->queue;
 	spin_lock(&queue->lock);
 	if (ent->state == FRRS_AVAILABLE) {
-		ent->state = FRRS_USERSPACE;
-		list_move_tail(&ent->list, &queue->ent_in_userspace);
+		ent->state = FRRS_CANCELED;
+		list_move_tail(&ent->list, &queue->ent_canceled);
 		need_cmd_done = true;
 		ent->cmd = NULL;
 	}
fs/fuse/dev_uring_i.h +6 -0
index 51a563922ce1..e62bd705e4f5 100644
@@ -32,6 +32,9 @@ enum fuse_ring_req_state {
 	/* The ring entry is in teardown */
 	FRRS_TEARDOWN,
 
+	/* The ring entry is canceled */
+	FRRS_CANCELED,
+
 	/* The ring entry is released, but not freed yet */
 	FRRS_RELEASED,
 };
@@ -85,6 +88,9 @@ struct fuse_ring_queue {
 	/* entries in userspace */
 	struct list_head ent_in_userspace;
 
+	/* entries that are canceled */
+	struct list_head ent_canceled;
+
 	/* entries that are released */
 	struct list_head ent_released;
 
maintainer
#1 lore raw
Quoted 9 lines · Jian Huang Li

fuse: fix potential memory leak from fuse_uring_cancel

If umount or fuse daemon quits at early stage, could happen all ring queues have already stopped and later some FUSE_IO_URING_CMD_REGISTER commands get canceled, that leaves ring entities in ent_in_userspace list and will not be freed by fuse_uring_destruct.
Move such ring entities to ent_canceled list and ensure fuse_uring_destruct frees these ring entities.

Thank you for the report.

Do you have a reproducer?

Quoted 13 lines
Fixes b6236c8407cb ("fuse: {io-uring} Prevent mount point hang on

fuse-server termination")

---
  fs/fuse/dev_uring.c   | 13 +++++++++++--
  fs/fuse/dev_uring_i.h |  6 ++++++
  2 files changed, 17 insertions(+), 2 deletions(-)
fs/fuse/dev_uring.c +0 -0
index 249b210becb1..db35797853c1 100644

fs/fuse/dev_uring.c void fuse_uring_destruct(struct fuse_conn *fc) · line 210

Quoted 8 lines
fs/fuse/dev_uring.c +5 -0
@@ -203,6 +203,12 @@ void fuse_uring_destruct(struct fuse_conn *fc)
                WARN_ON(!list_empty(&queue->ent_commit_queue));
                WARN_ON(!list_empty(&queue->ent_in_userspace));
 
+               list_for_each_entry_safe(ent, next, &queue->ent_canceled,
+                                        list) {
+                       list_del_init(&ent->list);
+                       kfree(ent);
+               }

Instead of introducing yet another list, we could do the same iterate/free on the ent_in_userspace list?

Thanks,
Miklos

#2 lore raw
Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Jian Huang Li

fuse: fix potential memory leak from fuse_uring_cancel

If umount or fuse daemon quits at early stage, could happen all ring queues have already stopped and later some FUSE_IO_URING_CMD_REGISTER commands get canceled, that leaves ring entities in ent_in_userspace list and will not be freed by fuse_uring_destruct.
Move such ring entities to ent_canceled list and ensure fuse_uring_destruct frees these ring entities.

Quoted 3 lines

Thank you for the report.

Do you have a reproducer?

We run with 2 more patches to pin the memory. That pin slows down startup a bit and then it becomes visible with xfstests generic/001. I guess it could be artificially reproduced with the current code by adding some delays into libfuse.

Quoted 17 lines
Quoted 17 lines
Fixes b6236c8407cb ("fuse: {io-uring} Prevent mount point hang on

fuse-server termination")

---
  fs/fuse/dev_uring.c   | 13 +++++++++++--
  fs/fuse/dev_uring_i.h |  6 ++++++
  2 files changed, 17 insertions(+), 2 deletions(-)
fs/fuse/dev_uring.c +0 -0
index 249b210becb1..db35797853c1 100644
@@ -203,6 +203,12 @@ void fuse_uring_destruct(struct fuse_conn *fc)
                WARN_ON(!list_empty(&queue->ent_commit_queue));
                WARN_ON(!list_empty(&queue->ent_in_userspace));
 
Quoted 8 lines

fs/fuse/dev_uring.c void fuse_uring_destruct(struct fuse_conn *fc) · line 210

Quoted 5 lines
fs/fuse/dev_uring.c +5 -0
+               list_for_each_entry_safe(ent, next, &queue->ent_canceled,
+                                        list) {
+                       list_del_init(&ent->list);
+                       kfree(ent);
+               }

Instead of introducing yet another list, we could do the same iterate/free on the ent_in_userspace list?

Yeah, would be possible, but probably hard to understand for someone reading the code? At a minimum it would need a good comment.

Thanks,
Bernd

maintainer
#3 lore raw
Quoted 7 lines · Bernd Schubert
Quoted 2 lines · Miklos Szeredi

Instead of introducing yet another list, we could do the same iterate/free on the ent_in_userspace list?

Yeah, would be possible, but probably hard to understand for someone reading the code? At a minimum it would need a good comment.

I'd prefer a good comment to additional complexity.

Thanks,
Miklos