Skip to contentSkip to conversation

fuse: io-uring request completion and ordering fixes

PATCH Applied Joanne Koong 3 participants 15 messages Jun 5 – Jun 8

Subsystems (3)
lore
Share & tools
lore

Joanne Koong

author maintainer
#0 lore raw

cover fuse: io-uring request completion and ordering fixes

Forwarding three fuse io-uring bugs that Chris's AI program found and fixed. These are all marked for stable.

Chris Mason (3):
fuse: fix EFAULT clobber in fuse_uring_commit
fuse: fix data races on ring->ready
fuse: end fuse_req on io-uring cancel task work

 fs/fuse/dev_uring.c   | 14 ++++++--------
 fs/fuse/dev_uring_i.h |  4 +++-
 2 files changed, 9 insertions(+), 9 deletions(-)
Signature · 1 line
2.52.0
author maintainer
#1 lore raw

PATCH 1/3 fuse: fix EFAULT clobber in fuse_uring_commit

From: Chris Mason <[email protected]>

copy_from_user() returns the number of bytes not copied as an unsigned residual on failure (1..sizeof(struct fuse_out_header)). fuse_uring_commit stores that residual in ssize_t err, sets req->out.h.error to -EFAULT, then jumps to out: with err still holding the positive residual.

    err = copy_from_user(&req->out.h, &ent->headers->in_out,
                         sizeof(req->out.h));
    if (err) {
        req->out.h.error = -EFAULT;
        goto out;          /* err is the positive residual */
    }
    ...
    out:
        fuse_uring_req_end(ent, req, err);

fuse_uring_req_end() then runs

    if (error)
        req->out.h.error = error;

which overwrites the just-assigned -EFAULT with the positive residual. FUSE callers such as fuse_simple_request() test err < 0 to detect failure, so the positive value is interpreted as success and the caller proceeds with an uninitialised or partial req->out.args.

Fix by assigning err = -EFAULT in the failure branch before jumping to out, so fuse_uring_req_end() receives a negative errno and sets req->out.h.error to -EFAULT.

Fixes c090c8abae4b ("fuse: Add io-uring sqe commit and fetch support")
Assisted-by kres (claude-opus-4-7)
---
 fs/fuse/dev_uring.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
fs/fuse/dev_uring.c +3 -6
index e467b23e6895..e33847436693 100644
@@ -811,14 +811,11 @@ static void fuse_uring_commit(struct fuse_ring_ent *ent, struct fuse_req *req,
 			      unsigned int issue_flags)
 {
 	struct fuse_ring *ring = ent->queue->ring;
-	ssize_t err = 0;
+	ssize_t err = -EFAULT;
 
-	err = copy_from_user(&req->out.h, &ent->headers->in_out,
-			     sizeof(req->out.h));
-	if (err) {
-		req->out.h.error = -EFAULT;
+	if (copy_from_user(&req->out.h, &ent->headers->in_out,
+			   sizeof(req->out.h)))
 		goto out;
-	}
 
 	err = fuse_uring_out_header_has_err(&req->out.h, req);
 	if (err) {
Signature · 1 line
2.52.0
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] fuse: fix EFAULT clobber in fuse_uring_commit

Quoted 52 lines · Joanne Koong

From: Chris Mason <[email protected]>

copy_from_user() returns the number of bytes not copied as an unsigned residual on failure (1..sizeof(struct fuse_out_header)). fuse_uring_commit stores that residual in ssize_t err, sets req->out.h.error to -EFAULT, then jumps to out: with err still holding the positive residual.

    err = copy_from_user(&req->out.h, &ent->headers->in_out,
                         sizeof(req->out.h));
    if (err) {
        req->out.h.error = -EFAULT;
        goto out;          /* err is the positive residual */
    }
    ...
    out:
        fuse_uring_req_end(ent, req, err);

fuse_uring_req_end() then runs

    if (error)
        req->out.h.error = error;

which overwrites the just-assigned -EFAULT with the positive residual. FUSE callers such as fuse_simple_request() test err < 0 to detect failure, so the positive value is interpreted as success and the caller proceeds with an uninitialised or partial req->out.args.

Fix by assigning err = -EFAULT in the failure branch before jumping to out, so fuse_uring_req_end() receives a negative errno and sets req->out.h.error to -EFAULT.

Fixes c090c8abae4b ("fuse: Add io-uring sqe commit and fetch support")
Assisted-by kres (claude-opus-4-7)
---
 fs/fuse/dev_uring.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
fs/fuse/dev_uring.c +1 -2
index e467b23e6895..e33847436693 100644
@@ -811,14 +811,11 @@ static void fuse_uring_commit(struct fuse_ring_ent *ent, struct fuse_req *req,
 			      unsigned int issue_flags)
 {
 	struct fuse_ring *ring = ent->queue->ring;
-	ssize_t err = 0;
+	ssize_t err = -EFAULT;
 
-	err = copy_from_user(&req->out.h, &ent->headers->in_out,

fs/fuse/dev_uring.c static void fuse_uring_commit(struct fuse_ring_ent *ent, struct fuse_req *req, · line 821

Quoted 10 lines
fs/fuse/dev_uring.c +2 -4
-			     sizeof(req->out.h));
-	if (err) {
-		req->out.h.error = -EFAULT;
+	if (copy_from_user(&req->out.h, &ent->headers->in_out,
+			   sizeof(req->out.h)))
 		goto out;
-	}
 
 	err = fuse_uring_out_header_has_err(&req->out.h, req);
 	if (err) {

Oh right, I run every time into the same trap, need to set up some kind of static check for {copy_from,to}_user.

maintainer Applied
#3 lore raw

Re: [PATCH 1/3] fuse: fix EFAULT clobber in fuse_uring_commit

In reply to Joanne Koong
Quoted 32 lines · Joanne Koong

From: Chris Mason <[email protected]>

copy_from_user() returns the number of bytes not copied as an unsigned residual on failure (1..sizeof(struct fuse_out_header)). fuse_uring_commit stores that residual in ssize_t err, sets req->out.h.error to -EFAULT, then jumps to out: with err still holding the positive residual.

    err = copy_from_user(&req->out.h, &ent->headers->in_out,
                         sizeof(req->out.h));
    if (err) {
        req->out.h.error = -EFAULT;
        goto out;          /* err is the positive residual */
    }
    ...
    out:
        fuse_uring_req_end(ent, req, err);

fuse_uring_req_end() then runs

    if (error)
        req->out.h.error = error;

which overwrites the just-assigned -EFAULT with the positive residual. FUSE callers such as fuse_simple_request() test err < 0 to detect failure, so the positive value is interpreted as success and the caller proceeds with an uninitialised or partial req->out.args.

Fix by assigning err = -EFAULT in the failure branch before jumping to out, so fuse_uring_req_end() receives a negative errno and sets req->out.h.error to -EFAULT.

Quoted 5 lines
Fixes c090c8abae4b ("fuse: Add io-uring sqe commit and fetch support")
Assisted-by kres (claude-opus-4-7)

Applied, thanks.

Miklos

author maintainer
#4 lore raw

PATCH 2/3 fuse: fix data races on ring->ready

In reply to Joanne Koong

From: Chris Mason <[email protected]>

On weakly-ordered architectures, the store to fiq->ops can be reordered past the store to ring->ready, allowing a CPU that sees ring->ready == true via fuse_uring_ready() to dispatch requests through a stale fiq->ops pointer. Upgrade the store to smp_store_release() and the load in fuse_uring_ready() to smp_load_acquire() so that the preceding WRITE_ONCE(fiq->ops, ...) is visible to any CPU that observes ring->ready == true.

Additionally, fuse_uring_do_register() publishes ring->ready with WRITE_ONCE() but the fast-path check reads it with a plain load. This is a marked-vs-unmarked access that KCSAN will flag. Wrap it in READ_ONCE() to mark it without adding unnecessary ordering.

Also wrap the fc->ring load in fuse_uring_ready() in READ_ONCE() to prevent the compiler from reloading it between the NULL check and the dereference.

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres (claude-opus-4-7)
---
 fs/fuse/dev_uring.c   | 4 ++--
 fs/fuse/dev_uring_i.h | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)
fs/fuse/dev_uring.c +2 -2
index e33847436693..7cd50990b097 100644
@@ -986,12 +986,12 @@ static void fuse_uring_do_register(struct fuse_ring_ent *ent,
 	fuse_uring_ent_avail(ent, queue);
 	spin_unlock(&queue->lock);
 
-	if (!ring->ready) {
+	if (!READ_ONCE(ring->ready)) {
 		bool ready = is_ring_ready(ring, queue->qid);
 
 		if (ready) {
 			WRITE_ONCE(fiq->ops, &fuse_io_uring_ops);
-			WRITE_ONCE(ring->ready, true);
+			smp_store_release(&ring->ready, true);
 			wake_up_all(&fch->blocked_waitq);
 		}
 	}
fs/fuse/dev_uring_i.h +3 -1
index 368f4d0790eb..6af604e17b2d 100644
@@ -167,7 +167,9 @@ static inline void fuse_uring_wait_stopped_queues(struct fuse_chan *fch)
 
 static inline bool fuse_uring_ready(struct fuse_chan *fch)
 {
-	return fch->ring && fch->ring->ready;
+	struct fuse_ring *ring = READ_ONCE(fch->ring);
+
+	return ring && smp_load_acquire(&ring->ready);
 }
 
 #else /* CONFIG_FUSE_IO_URING */
Signature · 1 line
2.52.0
maintainer Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] fuse: fix data races on ring->ready

Quoted 53 lines · Joanne Koong

From: Chris Mason <[email protected]>

On weakly-ordered architectures, the store to fiq->ops can be reordered past the store to ring->ready, allowing a CPU that sees ring->ready == true via fuse_uring_ready() to dispatch requests through a stale fiq->ops pointer. Upgrade the store to smp_store_release() and the load in fuse_uring_ready() to smp_load_acquire() so that the preceding WRITE_ONCE(fiq->ops, ...) is visible to any CPU that observes ring->ready == true.

Additionally, fuse_uring_do_register() publishes ring->ready with WRITE_ONCE() but the fast-path check reads it with a plain load. This is a marked-vs-unmarked access that KCSAN will flag. Wrap it in READ_ONCE() to mark it without adding unnecessary ordering.

Also wrap the fc->ring load in fuse_uring_ready() in READ_ONCE() to prevent the compiler from reloading it between the NULL check and the dereference.

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres (claude-opus-4-7)
---
 fs/fuse/dev_uring.c   | 4 ++--
 fs/fuse/dev_uring_i.h | 4 +++-
 2 files changed, 5 insertions(+), 3 deletions(-)
fs/fuse/dev_uring.c +2 -2
index e33847436693..7cd50990b097 100644
@@ -986,12 +986,12 @@ static void fuse_uring_do_register(struct fuse_ring_ent *ent,
 	fuse_uring_ent_avail(ent, queue);
 	spin_unlock(&queue->lock);
 
-	if (!ring->ready) {
+	if (!READ_ONCE(ring->ready)) {
 		bool ready = is_ring_ready(ring, queue->qid);
 
 		if (ready) {
 			WRITE_ONCE(fiq->ops, &fuse_io_uring_ops);
-			WRITE_ONCE(ring->ready, true);
+			smp_store_release(&ring->ready, true);
 			wake_up_all(&fch->blocked_waitq);
 		}
 	}
fs/fuse/dev_uring_i.h +0 -0
index 368f4d0790eb..6af604e17b2d 100644

fs/fuse/dev_uring_i.h static inline void fuse_uring_wait_stopped_queues(struct fuse_chan *fch) · line 175

Quoted 10 lines
fs/fuse/dev_uring_i.h +3 -1
@@ -167,7 +167,9 @@ static inline void fuse_uring_wait_stopped_queues(struct fuse_chan *fch)
 
 static inline bool fuse_uring_ready(struct fuse_chan *fch)
 {
-	return fch->ring && fch->ring->ready;
+	struct fuse_ring *ring = READ_ONCE(fch->ring);
+
+	return ring && smp_load_acquire(&ring->ready);
 }
 
 #else /* CONFIG_FUSE_IO_URING */
maintainer Applied
#6 lore raw

Re: [PATCH 2/3] fuse: fix data races on ring->ready

In reply to Joanne Koong
Quoted 20 lines · Joanne Koong

From: Chris Mason <[email protected]>

On weakly-ordered architectures, the store to fiq->ops can be reordered past the store to ring->ready, allowing a CPU that sees ring->ready == true via fuse_uring_ready() to dispatch requests through a stale fiq->ops pointer. Upgrade the store to smp_store_release() and the load in fuse_uring_ready() to smp_load_acquire() so that the preceding WRITE_ONCE(fiq->ops, ...) is visible to any CPU that observes ring->ready == true.

Additionally, fuse_uring_do_register() publishes ring->ready with WRITE_ONCE() but the fast-path check reads it with a plain load. This is a marked-vs-unmarked access that KCSAN will flag. Wrap it in READ_ONCE() to mark it without adding unnecessary ordering.

Also wrap the fc->ring load in fuse_uring_ready() in READ_ONCE() to prevent the compiler from reloading it between the NULL check and the dereference.

Quoted 5 lines
Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres (claude-opus-4-7)

Applied, thanks.

Miklos

author maintainer
#7 lore raw

PATCH 3/3 fuse: end fuse_req on io-uring cancel task work

In reply to Joanne Koong

From: Chris Mason <[email protected]>

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at dispatch time.

    fuse_uring_send_in_task()
      tw.cancel == true
        err = -ECANCELED
      fuse_uring_send(ent, cmd, err, issue_flags)
        ent->state = FRRS_USERSPACE
        list_move(&ent->list, &queue->ent_in_userspace)
        ent->cmd = NULL
        io_uring_cmd_done(-ECANCELED)
        /* ent->fuse_req still set, req still hashed */

The fuse_req stays linked on fpq->processing[hash] and fuse_request_end() is never invoked. The originating syscall thread blocks in D-state in request_wait_answer() until fuse_abort_conn() runs, which can be the entire connection lifetime. For FR_BACKGROUND requests fc->num_background is never decremented either, so repeated cancels inflate the counter until max_background is hit and all later background ops stall.

The non-cancel error branch already handles this correctly: when fuse_uring_prepare_send() fails it calls fuse_uring_req_end() before fuse_uring_send(). The cancel branch must do the same.

Fix by calling fuse_uring_req_end(ent, req, err) in the cancel branch before falling through to fuse_uring_send().

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres:claude-opus-4-7
---
 fs/fuse/dev_uring.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dev_uring.c +1 -0
index 7cd50990b097..b5cc700575ca 100644
@@ -1222,6 +1222,7 @@ static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw)
 		}
 	} else {
 		err = -ECANCELED;
+		fuse_uring_req_end(ent, ent->fuse_req, err);
 	}
 
 	fuse_uring_send(ent, cmd, err, issue_flags);
Signature · 1 line
2.52.0
maintainer
#8 lore raw

Re: [PATCH 3/3] fuse: end fuse_req on io-uring cancel task work

Quoted 50 lines · Joanne Koong

From: Chris Mason <[email protected]>

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at dispatch time.

    fuse_uring_send_in_task()
      tw.cancel == true
        err = -ECANCELED
      fuse_uring_send(ent, cmd, err, issue_flags)
        ent->state = FRRS_USERSPACE
        list_move(&ent->list, &queue->ent_in_userspace)
        ent->cmd = NULL
        io_uring_cmd_done(-ECANCELED)
        /* ent->fuse_req still set, req still hashed */

The fuse_req stays linked on fpq->processing[hash] and fuse_request_end() is never invoked. The originating syscall thread blocks in D-state in request_wait_answer() until fuse_abort_conn() runs, which can be the entire connection lifetime. For FR_BACKGROUND requests fc->num_background is never decremented either, so repeated cancels inflate the counter until max_background is hit and all later background ops stall.

The non-cancel error branch already handles this correctly: when fuse_uring_prepare_send() fails it calls fuse_uring_req_end() before fuse_uring_send(). The cancel branch must do the same.

Fix by calling fuse_uring_req_end(ent, req, err) in the cancel branch before falling through to fuse_uring_send().

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres:claude-opus-4-7
---
 fs/fuse/dev_uring.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dev_uring.c +0 -0
index 7cd50990b097..b5cc700575ca 100644

fs/fuse/dev_uring.c static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw) · line 1228

Quoted 7 lines
fs/fuse/dev_uring.c +1 -0
@@ -1222,6 +1222,7 @@ static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw)
 		}
 	} else {
 		err = -ECANCELED;
+		fuse_uring_req_end(ent, ent->fuse_req, err);
 	}
 
 	fuse_uring_send(ent, cmd, err, issue_flags);

I think that can race with fuse_uring_stop_queues(), which leaves us two choices

a) Same logic as fuse_uring_cancel()
https://lore.kernel.org/r/[email protected]

(also introduces slight code dup)

b) avoid the code dup and send it through fuse_uring_entry_teardown() with a bit refactoring. I thought I had send an updated patch version for that, but don't find it myself anymore. Explains why I never got reply. The old patch from October had bug, but I'm rather that I posted an mail with updated inline patch, not as separate series, though.

Thanks,
Bernd

author maintainer
#9 lore raw

Re: [PATCH 3/3] fuse: end fuse_req on io-uring cancel task work

Quoted 60 lines · Bernd Schubert
Quoted 56 lines · Joanne Koong

From: Chris Mason <[email protected]>

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at dispatch time.

    fuse_uring_send_in_task()
      tw.cancel == true
        err = -ECANCELED
      fuse_uring_send(ent, cmd, err, issue_flags)
        ent->state = FRRS_USERSPACE
        list_move(&ent->list, &queue->ent_in_userspace)
        ent->cmd = NULL
        io_uring_cmd_done(-ECANCELED)
        /* ent->fuse_req still set, req still hashed */

The fuse_req stays linked on fpq->processing[hash] and fuse_request_end() is never invoked. The originating syscall thread blocks in D-state in request_wait_answer() until fuse_abort_conn() runs, which can be the entire connection lifetime. For FR_BACKGROUND requests fc->num_background is never decremented either, so repeated cancels inflate the counter until max_background is hit and all later background ops stall.

The non-cancel error branch already handles this correctly: when fuse_uring_prepare_send() fails it calls fuse_uring_req_end() before fuse_uring_send(). The cancel branch must do the same.

Fix by calling fuse_uring_req_end(ent, req, err) in the cancel branch before falling through to fuse_uring_send().

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres:claude-opus-4-7
---
 fs/fuse/dev_uring.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dev_uring.c +1 -0
index 7cd50990b097..b5cc700575ca 100644
@@ -1222,6 +1222,7 @@ static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw)
              }
      } else {
              err = -ECANCELED;
+             fuse_uring_req_end(ent, ent->fuse_req, err);
      }
 
Quoted 3 lines

fs/fuse/dev_uring.c static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw) · line 1228

Quoted 1 line
fs/fuse/dev_uring.c +0 -0
      fuse_uring_send(ent, cmd, err, issue_flags);

I think that can race with fuse_uring_stop_queues(), which leaves us two

Hmm, I don't think this races with fuse_uring_stop_queues() as ent->state here is still FRRS_FUSE_REQ and fuse_uring_send_in_task() can only be called for a registered fuse ent, which means the ent has already grabbed the queue refcount which will trigger the async teardown worker to run in the background during abort until the ent is reclaimed. I think this adds a race though with the request expiration checking logic which (a) fixed, so I think you're right that we'll probably need the same cleanup here. I'll look at this early next week and send a v2.

Thanks for taking a look at this series.

Thanks,
Joanne

Quoted 20 lines

choices

a) Same logic as fuse_uring_cancel()
https://lore.kernel.org/r/[email protected]

(also introduces slight code dup)

b) avoid the code dup and send it through fuse_uring_entry_teardown() with a bit refactoring. I thought I had send an updated patch version for that, but don't find it myself anymore. Explains why I never got reply. The old patch from October had bug, but I'm rather that I posted an mail with updated inline patch, not as separate series, though.

Thanks,
Bernd

maintainer
#10 lore raw

Re: [PATCH 3/3] fuse: end fuse_req on io-uring cancel task work

Quoted 65 lines · Joanne Koong
Quoted 60 lines · Bernd Schubert
Quoted 56 lines · Joanne Koong

From: Chris Mason <[email protected]>

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at dispatch time.

    fuse_uring_send_in_task()
      tw.cancel == true
        err = -ECANCELED
      fuse_uring_send(ent, cmd, err, issue_flags)
        ent->state = FRRS_USERSPACE
        list_move(&ent->list, &queue->ent_in_userspace)
        ent->cmd = NULL
        io_uring_cmd_done(-ECANCELED)
        /* ent->fuse_req still set, req still hashed */

The fuse_req stays linked on fpq->processing[hash] and fuse_request_end() is never invoked. The originating syscall thread blocks in D-state in request_wait_answer() until fuse_abort_conn() runs, which can be the entire connection lifetime. For FR_BACKGROUND requests fc->num_background is never decremented either, so repeated cancels inflate the counter until max_background is hit and all later background ops stall.

The non-cancel error branch already handles this correctly: when fuse_uring_prepare_send() fails it calls fuse_uring_req_end() before fuse_uring_send(). The cancel branch must do the same.

Fix by calling fuse_uring_req_end(ent, req, err) in the cancel branch before falling through to fuse_uring_send().

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres:claude-opus-4-7
---
 fs/fuse/dev_uring.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dev_uring.c +1 -0
index 7cd50990b097..b5cc700575ca 100644
@@ -1222,6 +1222,7 @@ static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw)
              }
      } else {
              err = -ECANCELED;
+             fuse_uring_req_end(ent, ent->fuse_req, err);
      }
 
Quoted 3 lines

fs/fuse/dev_uring.c static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw) · line 1228

Quoted 1 line
fs/fuse/dev_uring.c +0 -0
      fuse_uring_send(ent, cmd, err, issue_flags);

I think that can race with fuse_uring_stop_queues(), which leaves us two

Quoted 10 lines

Hmm, I don't think this races with fuse_uring_stop_queues() as ent->state here is still FRRS_FUSE_REQ and fuse_uring_send_in_task() can only be called for a registered fuse ent, which means the ent has already grabbed the queue refcount which will trigger the async teardown worker to run in the background during abort until the ent is reclaimed. I think this adds a race though with the request expiration checking logic which (a) fixed, so I think you're right that we'll probably need the same cleanup here. I'll look at this early next week and send a v2.

Right, actually no race at all, just a plain use-after-free, because the entry is set to FRRS_USERSPACE and then cleaned up during connection abort and then released again through fuse_uring_queues or fuse_uring_async_stop_queues.

I actually do not get this part of the commit message

Quoted 7 lines

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at

A mean exit does not trigger fuse_dev_release() with a fuse_abort_conn() from that function?

I do not think we need 3/3 at all.

Thanks,
Bernd

author maintainer
#11 lore raw

Re: [PATCH 3/3] fuse: end fuse_req on io-uring cancel task work

Quoted 94 lines · Bernd Schubert
Quoted 65 lines · Joanne Koong
Quoted 60 lines · Bernd Schubert
Quoted 56 lines · Joanne Koong

From: Chris Mason <[email protected]>

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at dispatch time.

    fuse_uring_send_in_task()
      tw.cancel == true
        err = -ECANCELED
      fuse_uring_send(ent, cmd, err, issue_flags)
        ent->state = FRRS_USERSPACE
        list_move(&ent->list, &queue->ent_in_userspace)
        ent->cmd = NULL
        io_uring_cmd_done(-ECANCELED)
        /* ent->fuse_req still set, req still hashed */

The fuse_req stays linked on fpq->processing[hash] and fuse_request_end() is never invoked. The originating syscall thread blocks in D-state in request_wait_answer() until fuse_abort_conn() runs, which can be the entire connection lifetime. For FR_BACKGROUND requests fc->num_background is never decremented either, so repeated cancels inflate the counter until max_background is hit and all later background ops stall.

The non-cancel error branch already handles this correctly: when fuse_uring_prepare_send() fails it calls fuse_uring_req_end() before fuse_uring_send(). The cancel branch must do the same.

Fix by calling fuse_uring_req_end(ent, req, err) in the cancel branch before falling through to fuse_uring_send().

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres:claude-opus-4-7
---
 fs/fuse/dev_uring.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dev_uring.c +1 -0
index 7cd50990b097..b5cc700575ca 100644
@@ -1222,6 +1222,7 @@ static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw)
              }
      } else {
              err = -ECANCELED;
+             fuse_uring_req_end(ent, ent->fuse_req, err);
      }
 
Quoted 3 lines

fs/fuse/dev_uring.c static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw) · line 1228

Quoted 1 line
fs/fuse/dev_uring.c +0 -0
      fuse_uring_send(ent, cmd, err, issue_flags);

I think that can race with fuse_uring_stop_queues(), which leaves us two

Quoted 10 lines

Hmm, I don't think this races with fuse_uring_stop_queues() as ent->state here is still FRRS_FUSE_REQ and fuse_uring_send_in_task() can only be called for a registered fuse ent, which means the ent has already grabbed the queue refcount which will trigger the async teardown worker to run in the background during abort until the ent is reclaimed. I think this adds a race though with the request expiration checking logic which (a) fixed, so I think you're right that we'll probably need the same cleanup here. I'll look at this early next week and send a v2.

Right, actually no race at all, just a plain use-after-free, because the entry is set to FRRS_USERSPACE and then cleaned up during connection abort and then released again through fuse_uring_queues or fuse_uring_async_stop_queues.

I actually do not get this part of the commit message

Quoted 7 lines

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at

Quoted 2 lines

A mean exit does not trigger fuse_dev_release() with a fuse_abort_conn() from that function?

I don't think tw.cancel implies a connection abort. tw.cancel gets set on io_uring task death or ring death which is different from fuse connection death (eg a single worker thread of a multithreaded daemon exiting doesn't drop any ref on /dev/fuse fd, but that thread's in-flight task work still drains with tw.cancel)

Quoted 2 lines

I do not think we need 3/3 at all.

I think this is needed for the cases where tw.cancel occurs without a subsequent fuse abort, else the application syscall thread is stuck uninterruptibly in D-state in request_wait_answer() for the connection's lifetime. tw.cancel with a fuse abort is the common case, but I think unfortunately we also need to handle the case where this doesn't occur.

Thanks,
Joanne

Quoted 4 lines

Thanks,
Bernd

maintainer
#12 lore raw

Re: [PATCH 3/3] fuse: end fuse_req on io-uring cancel task work

Quoted 105 lines · Joanne Koong
Quoted 94 lines · Bernd Schubert
Quoted 65 lines · Joanne Koong
Quoted 60 lines · Bernd Schubert
Quoted 56 lines · Joanne Koong

From: Chris Mason <[email protected]>

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at dispatch time.

    fuse_uring_send_in_task()
      tw.cancel == true
        err = -ECANCELED
      fuse_uring_send(ent, cmd, err, issue_flags)
        ent->state = FRRS_USERSPACE
        list_move(&ent->list, &queue->ent_in_userspace)
        ent->cmd = NULL
        io_uring_cmd_done(-ECANCELED)
        /* ent->fuse_req still set, req still hashed */

The fuse_req stays linked on fpq->processing[hash] and fuse_request_end() is never invoked. The originating syscall thread blocks in D-state in request_wait_answer() until fuse_abort_conn() runs, which can be the entire connection lifetime. For FR_BACKGROUND requests fc->num_background is never decremented either, so repeated cancels inflate the counter until max_background is hit and all later background ops stall.

The non-cancel error branch already handles this correctly: when fuse_uring_prepare_send() fails it calls fuse_uring_req_end() before fuse_uring_send(). The cancel branch must do the same.

Fix by calling fuse_uring_req_end(ent, req, err) in the cancel branch before falling through to fuse_uring_send().

Fixes c2c9af9a0b13 ("fuse: Allow to queue fg requests through io-uring")
Assisted-by kres:claude-opus-4-7
---
 fs/fuse/dev_uring.c | 1 +
 1 file changed, 1 insertion(+)
fs/fuse/dev_uring.c +1 -0
index 7cd50990b097..b5cc700575ca 100644
@@ -1222,6 +1222,7 @@ static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw)
              }
      } else {
              err = -ECANCELED;
+             fuse_uring_req_end(ent, ent->fuse_req, err);
      }
 
Quoted 3 lines

fs/fuse/dev_uring.c static void fuse_uring_send_in_task(struct io_tw_req tw_req, io_tw_token_t tw) · line 1228

Quoted 1 line
fs/fuse/dev_uring.c +0 -0
      fuse_uring_send(ent, cmd, err, issue_flags);

I think that can race with fuse_uring_stop_queues(), which leaves us two

Quoted 10 lines

Hmm, I don't think this races with fuse_uring_stop_queues() as ent->state here is still FRRS_FUSE_REQ and fuse_uring_send_in_task() can only be called for a registered fuse ent, which means the ent has already grabbed the queue refcount which will trigger the async teardown worker to run in the background during abort until the ent is reclaimed. I think this adds a race though with the request expiration checking logic which (a) fixed, so I think you're right that we'll probably need the same cleanup here. I'll look at this early next week and send a v2.

Right, actually no race at all, just a plain use-after-free, because the entry is set to FRRS_USERSPACE and then cleaned up during connection abort and then released again through fuse_uring_queues or fuse_uring_async_stop_queues.

I actually do not get this part of the commit message

Quoted 7 lines

When io_uring delivers task work with tw.cancel set (PF_EXITING, PF_KTHREAD fallback, or percpu_ref_is_dying on the ring context), fuse_uring_send_in_task() takes the cancel branch, assigns -ECANCELED, and falls through to fuse_uring_send(). That path only flips the entry to FRRS_USERSPACE and completes the io_uring cmd; it never discharges the ring entry's owning reference to the fuse_req that fuse_uring_add_req_to_ring_ent() handed it at

Quoted 2 lines

A mean exit does not trigger fuse_dev_release() with a fuse_abort_conn() from that function?

I don't think tw.cancel implies a connection abort. tw.cancel gets set on io_uring task death or ring death which is different from fuse connection death (eg a single worker thread of a multithreaded daemon exiting doesn't drop any ref on /dev/fuse fd, but that thread's in-flight task work still drains with tw.cancel)

Quoted 1 line
Quoted 8 lines
Quoted 1 line

I do not think we need 3/3 at all.

I think this is needed for the cases where tw.cancel occurs without a subsequent fuse abort, else the application syscall thread is stuck uninterruptibly in D-state in request_wait_answer() for the connection's lifetime. tw.cancel with a fuse abort is the common case, but I think unfortunately we also need to handle the case where this doesn't occur.

I see, the initial code was using IO_URING_F_TASK_DEAD and I had wrongly assumed that is related to PF_EXITING.
Well, I think the fix is clear, although I personally do not like the exit code dup (or better triple)
https://lore.kernel.org/r/[email protected]

In my option fuse_uring_cancel() and canceled fuse_uring_send_in_task() should go through fuse_uring_entry_teardown().

Thanks,
Bernd

author maintainer
#13 lore raw

Re: [PATCH 3/3] fuse: end fuse_req on io-uring cancel task work

Quoted 16 lines · Bernd Schubert
Quoted 11 lines · Joanne Koong
Quoted 3 lines · Bernd Schubert

I do not think we need 3/3 at all.

I think this is needed for the cases where tw.cancel occurs without a subsequent fuse abort, else the application syscall thread is stuck uninterruptibly in D-state in request_wait_answer() for the connection's lifetime. tw.cancel with a fuse abort is the common case, but I think unfortunately we also need to handle the case where this doesn't occur.

Quoted 9 lines

I see, the initial code was using IO_URING_F_TASK_DEAD and I had wrongly assumed that is related to PF_EXITING.
Well, I think the fix is clear, although I personally do not like the exit code dup (or better triple)
https://lore.kernel.org/r/[email protected]

In my option fuse_uring_cancel() and canceled fuse_uring_send_in_task() should go through fuse_uring_entry_teardown().

I don't feel strongly about this. On cancel, the ent becomes useless since cancel consumes the cmd, so imo it makes sense to just free and clean up the ent immediately instead of keeping it around until the fuse connection gets eventually aborted, but I don't think it really matters. Happy to go with what you prefer.

Thanks,
Joanne

Quoted 3 lines

Thanks,
Bernd

author maintainer
#14 lore raw

Re: [PATCH 3/3] fuse: end fuse_req on io-uring cancel task work

Quoted 28 lines · Joanne Koong
Quoted 16 lines · Bernd Schubert
Quoted 11 lines · Joanne Koong
Quoted 3 lines · Bernd Schubert

I do not think we need 3/3 at all.

I think this is needed for the cases where tw.cancel occurs without a subsequent fuse abort, else the application syscall thread is stuck uninterruptibly in D-state in request_wait_answer() for the connection's lifetime. tw.cancel with a fuse abort is the common case, but I think unfortunately we also need to handle the case where this doesn't occur.

Quoted 9 lines

I see, the initial code was using IO_URING_F_TASK_DEAD and I had wrongly assumed that is related to PF_EXITING.
Well, I think the fix is clear, although I personally do not like the exit code dup (or better triple)
https://lore.kernel.org/r/[email protected]

In my option fuse_uring_cancel() and canceled fuse_uring_send_in_task() should go through fuse_uring_entry_teardown().

Quoted 5 lines

I don't feel strongly about this. On cancel, the ent becomes useless since cancel consumes the cmd, so imo it makes sense to just free and clean up the ent immediately instead of keeping it around until the fuse connection gets eventually aborted, but I don't think it really matters. Happy to go with what you prefer.

This fix (as well as [1]) needs to be backported to stable so I think it'd be better to keep the fix as minimal as possible and do any entry_teardown deduping as a follow-up cleanup after that. I'll send out a v2 that fixes it minimally.

Thanks,
Joanne

[1] https://lore.kernel.org/fuse-devel/[email protected]/

Quoted 7 lines

Thanks,
Joanne

Quoted 3 lines

Thanks,
Bernd