Skip to contentSkip to conversation

fuse: fix io-uring background queue stall on request completion

PATCH Superseded Joanne Koong 2 participants 5 messages Apr 1 – Apr 2

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Joanne Koong

author maintainer
#0 lore raw

PATCH 1/1 fuse: fix io-uring background queue stall on request completion

When a background request completes via the io_uring path, the background queue gets flushed to dispatch pending background requests, but this is done before the connection-level background counters (fc->num_background, fc->active_background) are properly accounted, which can leave pending background requests stuck in the per-queue background queue.

The connection-level counters are decremented in fuse_request_end(), but flush_bg_queue() flushes the /dev/fuse path queue (fc->bg_queue), not the io_uring per-queue bg one, which means pending uring background requests on the queue are never dispatched.

Fix this by accounting the connection-level background counters first before flushing the queue's background queue. Since fuse_request_bg_finish() clears FR_BACKGROUND, fuse_request_end() will skip the background cleanup branch entirely, which avoids any double-decrements; it will call the wake_up(&req->waitq) branch but this is effectively a no-op as background requests have no waiters on req->waitq.

Fixes 857b0263f30e ("fuse: Allow to queue bg requests through io-uring")
---
 fs/fuse/dev.c        | 41 ++++++++++++++++++++++++-----------------
 fs/fuse/dev_uring.c  |  1 +
 fs/fuse/fuse_dev_i.h |  1 +
 3 files changed, 26 insertions(+), 17 deletions(-)
fs/fuse/dev.c +24 -17
index b212565a78cf..35cdfc162ba5 100644
@@ -447,6 +447,29 @@ static void flush_bg_queue(struct fuse_conn *fc)
 	}
 }
 
+void fuse_request_bg_finish(struct fuse_conn *fc, struct fuse_req *req)
+{
+	lockdep_assert_held(&fc->bg_lock);
+
+	clear_bit(FR_BACKGROUND, &req->flags);
+	if (fc->num_background == fc->max_background) {
+		fc->blocked = 0;
+		wake_up(&fc->blocked_waitq);
+	} else if (!fc->blocked) {
+		/*
+		 * Wake up next waiter, if any.  It's okay to use
+		 * waitqueue_active(), as we've already synced up
+		 * fc->blocked with waiters with the wake_up() call
+		 * above.
+		 */
+		if (waitqueue_active(&fc->blocked_waitq))
+			wake_up(&fc->blocked_waitq);
+	}
+
+	fc->num_background--;
+	fc->active_background--;
+}
+
 /*
  * This function is called when a request is finished.  Either a reply
  * has arrived or it was aborted (and not yet sent) or some error
@@ -479,23 +502,7 @@ void fuse_request_end(struct fuse_req *req)
 	WARN_ON(test_bit(FR_SENT, &req->flags));
 	if (test_bit(FR_BACKGROUND, &req->flags)) {
 		spin_lock(&fc->bg_lock);
-		clear_bit(FR_BACKGROUND, &req->flags);
-		if (fc->num_background == fc->max_background) {
-			fc->blocked = 0;
-			wake_up(&fc->blocked_waitq);
-		} else if (!fc->blocked) {
-			/*
-			 * Wake up next waiter, if any.  It's okay to use
-			 * waitqueue_active(), as we've already synced up
-			 * fc->blocked with waiters with the wake_up() call
-			 * above.
-			 */
-			if (waitqueue_active(&fc->blocked_waitq))
-				wake_up(&fc->blocked_waitq);
-		}
-
-		fc->num_background--;
-		fc->active_background--;
+		fuse_request_bg_finish(fc, req);
 		flush_bg_queue(fc);
 		spin_unlock(&fc->bg_lock);
 	} else {
fs/fuse/dev_uring.c +1 -0
index 7b9822e8837b..ae916733f18a 100644
@@ -90,6 +90,7 @@ static void fuse_uring_req_end(struct fuse_ring_ent *ent, struct fuse_req *req,
 	if (test_bit(FR_BACKGROUND, &req->flags)) {
 		queue->active_background--;
 		spin_lock(&fc->bg_lock);
+		fuse_request_bg_finish(fc, req);
 		fuse_uring_flush_bg(queue);
 		spin_unlock(&fc->bg_lock);
 	}
fs/fuse/fuse_dev_i.h +1 -0
index 134bf44aff0d..7da505af6d35 100644
@@ -59,6 +59,7 @@ unsigned int fuse_req_hash(u64 unique);
 struct fuse_req *fuse_request_find(struct fuse_pqueue *fpq, u64 unique);
 
 void fuse_dev_end_requests(struct list_head *head);
+void fuse_request_bg_finish(struct fuse_conn *fc, struct fuse_req *req);
 
 void fuse_copy_init(struct fuse_copy_state *cs, bool write,
 			   struct iov_iter *iter);
Signature · 1 line
2.52.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Joanne Koong

When a background request completes via the io_uring path, the background queue gets flushed to dispatch pending background requests, but this is done before the connection-level background counters (fc->num_background, fc->active_background) are properly accounted, which can leave pending background requests stuck in the per-queue background queue.

I don't think it ever gets stuck. In fuse_uring_flush_bg()

        while ((fc->active_background < fc->max_background ||
                !queue->active_background) &&

And queue->active_background gets decreased in the caller, fuse_uring_req_end. Idea is to always let through at least one background request per queue. Reson is that the global fc->num_background might be at the limit already, a queue then might get a request, it would get added to queue->fuse_req_bg_queue, but once fc->active_background goes down, there wouldn't be anything to wake up these requests.
Issue: Only one request allowed per queue when this comes up.

Quoted 87 lines

The connection-level counters are decremented in fuse_request_end(), but flush_bg_queue() flushes the /dev/fuse path queue (fc->bg_queue), not the io_uring per-queue bg one, which means pending uring background requests on the queue are never dispatched.

Fix this by accounting the connection-level background counters first before flushing the queue's background queue. Since fuse_request_bg_finish() clears FR_BACKGROUND, fuse_request_end() will skip the background cleanup branch entirely, which avoids any double-decrements; it will call the wake_up(&req->waitq) branch but this is effectively a no-op as background requests have no waiters on req->waitq.

Fixes 857b0263f30e ("fuse: Allow to queue bg requests through io-uring")
---
 fs/fuse/dev.c        | 41 ++++++++++++++++++++++++-----------------
 fs/fuse/dev_uring.c  |  1 +
 fs/fuse/fuse_dev_i.h |  1 +
 3 files changed, 26 insertions(+), 17 deletions(-)
fs/fuse/dev.c +24 -17
index b212565a78cf..35cdfc162ba5 100644
@@ -447,6 +447,29 @@ static void flush_bg_queue(struct fuse_conn *fc)
 	}
 }
 
+void fuse_request_bg_finish(struct fuse_conn *fc, struct fuse_req *req)
+{
+	lockdep_assert_held(&fc->bg_lock);
+
+	clear_bit(FR_BACKGROUND, &req->flags);
+	if (fc->num_background == fc->max_background) {
+		fc->blocked = 0;
+		wake_up(&fc->blocked_waitq);
+	} else if (!fc->blocked) {
+		/*
+		 * Wake up next waiter, if any.  It's okay to use
+		 * waitqueue_active(), as we've already synced up
+		 * fc->blocked with waiters with the wake_up() call
+		 * above.
+		 */
+		if (waitqueue_active(&fc->blocked_waitq))
+			wake_up(&fc->blocked_waitq);
+	}
+
+	fc->num_background--;
+	fc->active_background--;
+}
+
 /*
  * This function is called when a request is finished.  Either a reply
  * has arrived or it was aborted (and not yet sent) or some error
@@ -479,23 +502,7 @@ void fuse_request_end(struct fuse_req *req)
 	WARN_ON(test_bit(FR_SENT, &req->flags));
 	if (test_bit(FR_BACKGROUND, &req->flags)) {
 		spin_lock(&fc->bg_lock);
-		clear_bit(FR_BACKGROUND, &req->flags);
-		if (fc->num_background == fc->max_background) {
-			fc->blocked = 0;
-			wake_up(&fc->blocked_waitq);
-		} else if (!fc->blocked) {
-			/*
-			 * Wake up next waiter, if any.  It's okay to use
-			 * waitqueue_active(), as we've already synced up
-			 * fc->blocked with waiters with the wake_up() call
-			 * above.
-			 */
-			if (waitqueue_active(&fc->blocked_waitq))
-				wake_up(&fc->blocked_waitq);
-		}
-
-		fc->num_background--;
-		fc->active_background--;
+		fuse_request_bg_finish(fc, req);
 		flush_bg_queue(fc);
 		spin_unlock(&fc->bg_lock);
 	} else {
fs/fuse/dev_uring.c +0 -0
index 7b9822e8837b..ae916733f18a 100644

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

Quoted 4 lines
fs/fuse/dev_uring.c +1 -0
@@ -90,6 +90,7 @@ static void fuse_uring_req_end(struct fuse_ring_ent *ent, struct fuse_req *req,
 	if (test_bit(FR_BACKGROUND, &req->flags)) {
 		queue->active_background--;
 		spin_lock(&fc->bg_lock);
+		fuse_request_bg_finish(fc, req);

This basically solves the issue that situations could come up where only one requests runs at a time.

Quoted 8 lines
fs/fuse/dev_uring.c +0 -0
 		fuse_uring_flush_bg(queue);
 		spin_unlock(&fc->bg_lock);
 	}
fs/fuse/fuse_dev_i.h +0 -0
index 134bf44aff0d..7da505af6d35 100644

fs/fuse/fuse_dev_i.h unsigned int fuse_req_hash(u64 unique); · line 65

Quoted 7 lines
fs/fuse/fuse_dev_i.h +1 -0
@@ -59,6 +59,7 @@ unsigned int fuse_req_hash(u64 unique);
 struct fuse_req *fuse_request_find(struct fuse_pqueue *fpq, u64 unique);
 
 void fuse_dev_end_requests(struct list_head *head);
+void fuse_request_bg_finish(struct fuse_conn *fc, struct fuse_req *req);
 
 void fuse_copy_init(struct fuse_copy_state *cs, bool write,
 			   struct iov_iter *iter);

PS: If you should be chasing a stuck bg queue issue at tear down, Horst is chasing a teardown issue with bg queue.
We are currently testing this patch

fs/fuse/dev_uring.c +1 -2
index 3de97ed2280f..451bf8981e19 100644
@@ -136,11 +136,10 @@ void fuse_uring_abort_end_requests(struct fuse_ring *ring)
                if (!queue)
                        continue;
 
-               queue->stopped = true;
-
                WARN_ON_ONCE(ring->fc->max_background != UINT_MAX);
                spin_lock(&queue->lock);
                spin_lock(&fc->bg_lock);
+               queue->stopped = true;
                fuse_uring_flush_bg(queue);
                spin_unlock(&fc->bg_lock);
                spin_unlock(&queue->lock);

Thanks,
Bernd

author maintainer
#2 lore raw
Quoted 1 line · Bernd Schubert

Hi Bernd,

Thanks for taking a look at this.

Quoted 9 lines · Joanne Koong
Quoted 6 lines

When a background request completes via the io_uring path, the background queue gets flushed to dispatch pending background requests, but this is done before the connection-level background counters (fc->num_background, fc->active_background) are properly accounted, which can leave pending background requests stuck in the per-queue background queue.

Quoted 5 lines

I don't think it ever gets stuck. In fuse_uring_flush_bg()

        while ((fc->active_background < fc->max_background ||
                !queue->active_background) &&

If the queue already has other background requests in-flight, then this check never passes due to the stale fc->active_background value and all pending background requests on the queue are stuck until that in-flight background request completes, no? I'm rereading my commit message, maybe the wording is unclear - do you prefer it to be reworded to "which can leave pending background requests in the per-queue background queue stalled"?

Quoted 126 lines

And queue->active_background gets decreased in the caller, fuse_uring_req_end. Idea is to always let through at least one background request per queue. Reson is that the global fc->num_background might be at the limit already, a queue then might get a request, it would get added to queue->fuse_req_bg_queue, but once fc->active_background goes down, there wouldn't be anything to wake up these requests.
Issue: Only one request allowed per queue when this comes up.

Quoted 87 lines

The connection-level counters are decremented in fuse_request_end(), but flush_bg_queue() flushes the /dev/fuse path queue (fc->bg_queue), not the io_uring per-queue bg one, which means pending uring background requests on the queue are never dispatched.

Fix this by accounting the connection-level background counters first before flushing the queue's background queue. Since fuse_request_bg_finish() clears FR_BACKGROUND, fuse_request_end() will skip the background cleanup branch entirely, which avoids any double-decrements; it will call the wake_up(&req->waitq) branch but this is effectively a no-op as background requests have no waiters on req->waitq.

Fixes 857b0263f30e ("fuse: Allow to queue bg requests through io-uring")
---
 fs/fuse/dev.c        | 41 ++++++++++++++++++++++++-----------------
 fs/fuse/dev_uring.c  |  1 +
 fs/fuse/fuse_dev_i.h |  1 +
 3 files changed, 26 insertions(+), 17 deletions(-)
fs/fuse/dev.c +24 -17
index b212565a78cf..35cdfc162ba5 100644
@@ -447,6 +447,29 @@ static void flush_bg_queue(struct fuse_conn *fc)
      }
 }
 
+void fuse_request_bg_finish(struct fuse_conn *fc, struct fuse_req *req)
+{
+     lockdep_assert_held(&fc->bg_lock);
+
+     clear_bit(FR_BACKGROUND, &req->flags);
+     if (fc->num_background == fc->max_background) {
+             fc->blocked = 0;
+             wake_up(&fc->blocked_waitq);
+     } else if (!fc->blocked) {
+             /*
+              * Wake up next waiter, if any.  It's okay to use
+              * waitqueue_active(), as we've already synced up
+              * fc->blocked with waiters with the wake_up() call
+              * above.
+              */
+             if (waitqueue_active(&fc->blocked_waitq))
+                     wake_up(&fc->blocked_waitq);
+     }
+
+     fc->num_background--;
+     fc->active_background--;
+}
+
 /*
  * This function is called when a request is finished.  Either a reply
  * has arrived or it was aborted (and not yet sent) or some error
@@ -479,23 +502,7 @@ void fuse_request_end(struct fuse_req *req)
      WARN_ON(test_bit(FR_SENT, &req->flags));
      if (test_bit(FR_BACKGROUND, &req->flags)) {
              spin_lock(&fc->bg_lock);
-             clear_bit(FR_BACKGROUND, &req->flags);
-             if (fc->num_background == fc->max_background) {
-                     fc->blocked = 0;
-                     wake_up(&fc->blocked_waitq);
-             } else if (!fc->blocked) {
-                     /*
-                      * Wake up next waiter, if any.  It's okay to use
-                      * waitqueue_active(), as we've already synced up
-                      * fc->blocked with waiters with the wake_up() call
-                      * above.
-                      */
-                     if (waitqueue_active(&fc->blocked_waitq))
-                             wake_up(&fc->blocked_waitq);
-             }
-
-             fc->num_background--;
-             fc->active_background--;
+             fuse_request_bg_finish(fc, req);
              flush_bg_queue(fc);
              spin_unlock(&fc->bg_lock);
      } else {
fs/fuse/dev_uring.c +0 -0
index 7b9822e8837b..ae916733f18a 100644

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

Quoted 4 lines
fs/fuse/dev_uring.c +1 -0
@@ -90,6 +90,7 @@ static void fuse_uring_req_end(struct fuse_ring_ent *ent, struct fuse_req *req,
      if (test_bit(FR_BACKGROUND, &req->flags)) {
              queue->active_background--;
              spin_lock(&fc->bg_lock);
+             fuse_request_bg_finish(fc, req);

This basically solves the issue that situations could come up where only one requests runs at a time.

Quoted 8 lines
fs/fuse/dev_uring.c +0 -0
              fuse_uring_flush_bg(queue);
              spin_unlock(&fc->bg_lock);
      }
fs/fuse/fuse_dev_i.h +0 -0
index 134bf44aff0d..7da505af6d35 100644

fs/fuse/fuse_dev_i.h unsigned int fuse_req_hash(u64 unique); · line 65

Quoted 7 lines
fs/fuse/fuse_dev_i.h +1 -0
@@ -59,6 +59,7 @@ unsigned int fuse_req_hash(u64 unique);
 struct fuse_req *fuse_request_find(struct fuse_pqueue *fpq, u64 unique);
 
 void fuse_dev_end_requests(struct list_head *head);
+void fuse_request_bg_finish(struct fuse_conn *fc, struct fuse_req *req);
 
 void fuse_copy_init(struct fuse_copy_state *cs, bool write,
                         struct iov_iter *iter);
Quoted 2 lines

PS: If you should be chasing a stuck bg queue issue at tear down, Horst is chasing a teardown issue with bg queue.

Awesome, looking forward to seeing Horst's investigation.

Thanks,
Joanne

Quoted 23 lines

We are currently testing this patch

fs/fuse/dev_uring.c +1 -2
index 3de97ed2280f..451bf8981e19 100644
@@ -136,11 +136,10 @@ void fuse_uring_abort_end_requests(struct fuse_ring *ring)
                if (!queue)
                        continue;
 
-               queue->stopped = true;
-
                WARN_ON_ONCE(ring->fc->max_background != UINT_MAX);
                spin_lock(&queue->lock);
                spin_lock(&fc->bg_lock);
+               queue->stopped = true;
                fuse_uring_flush_bg(queue);
                spin_unlock(&fc->bg_lock);
                spin_unlock(&queue->lock);

Thanks,
Bernd

maintainer
#3 lore raw
Quoted 20 lines · Joanne Koong
Quoted 1 line · Bernd Schubert

Hi Bernd,

Thanks for taking a look at this.

Quoted 9 lines · Joanne Koong
Quoted 6 lines

When a background request completes via the io_uring path, the background queue gets flushed to dispatch pending background requests, but this is done before the connection-level background counters (fc->num_background, fc->active_background) are properly accounted, which can leave pending background requests stuck in the per-queue background queue.

Quoted 5 lines

I don't think it ever gets stuck. In fuse_uring_flush_bg()

        while ((fc->active_background < fc->max_background ||
                !queue->active_background) &&
Quoted 7 lines

If the queue already has other background requests in-flight, then this check never passes due to the stale fc->active_background value and all pending background requests on the queue are stuck until that in-flight background request completes, no? I'm rereading my commit message, maybe the wording is unclear - do you prefer it to be reworded to "which can leave pending background requests in the per-queue background queue stalled"?

From my point of view "which might reduce effective queue depth to 1". I certainly agree with the fix, just not on the word "stalled".

Cheers,
Bernd

author maintainer
#4 lore raw
Quoted 32 lines · Bernd Schubert
Quoted 20 lines · Joanne Koong
Quoted 1 line · Bernd Schubert

Hi Bernd,

Thanks for taking a look at this.

Quoted 9 lines · Joanne Koong
Quoted 6 lines

When a background request completes via the io_uring path, the background queue gets flushed to dispatch pending background requests, but this is done before the connection-level background counters (fc->num_background, fc->active_background) are properly accounted, which can leave pending background requests stuck in the per-queue background queue.

Quoted 5 lines

I don't think it ever gets stuck. In fuse_uring_flush_bg()

        while ((fc->active_background < fc->max_background ||
                !queue->active_background) &&
Quoted 7 lines

If the queue already has other background requests in-flight, then this check never passes due to the stale fc->active_background value and all pending background requests on the queue are stuck until that in-flight background request completes, no? I'm rereading my commit message, maybe the wording is unclear - do you prefer it to be reworded to "which can leave pending background requests in the per-queue background queue stalled"?

Quoted 3 lines

From my point of view "which might reduce effective queue depth to 1". I certainly agree with the fix, just not on the word "stalled".

Sounds good,m I'll send out a v2 that uses your wording.

Thanks,
Joanne

Quoted 3 lines

Cheers,
Bernd