fs/fuse: fix potential memory leak from fuse_uring_cancel
Subsystems (3)
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.
--- 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
@@ -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
@@ -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;
Miklos Szeredi
maintainerQuoted 9 lines · Jian Huang Li
fuse: fix potential memory leak from
fuse_uring_cancelIf umount or fuse daemon quits at early stage, could happen all ring queues have already stopped and later some
FUSE_IO_URING_CMD_REGISTERcommands get canceled, that leaves ring entities inent_in_userspacelist and will not be freed byfuse_uring_destruct.
Move such ring entities toent_canceledlist and ensurefuse_uring_destructfrees 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 onfuse-server termination")
Signed-off-by Jian Huang Li <[email protected]>--- 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
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
Quoted 11 lines · Miklos Szeredi
Quoted 9 lines · Jian Huang Li
fuse: fix potential memory leak from
fuse_uring_cancelIf umount or fuse daemon quits at early stage, could happen all ring queues have already stopped and later some
FUSE_IO_URING_CMD_REGISTERcommands get canceled, that leaves ring entities inent_in_userspacelist and will not be freed byfuse_uring_destruct.
Move such ring entities toent_canceledlist and ensurefuse_uring_destructfrees 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 onfuse-server termination")
Signed-off-by Jian Huang Li <[email protected]>--- 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@@ -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_userspacelist?
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
Miklos Szeredi
maintainerQuoted 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_userspacelist?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