Skip to content

fuse: alternate contested io-uring slots between foreground and bg - #282

Merged
hbirth merged 1 commit into
DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1from
hazhou-ddn:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1-fair-admit
Nov 9, 2026
Merged

hbirth merged 1 commit into
DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1from
hazhou-ddn:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1-fair-admit

Conversation

@hazhou-ddn

@hazhou-ddn hazhou-ddn commented Nov 8, 2026 •

Copy link
Copy Markdown

fuse: alternate contested io-uring slots between foreground and bg

With the writeback cache, foreground requests and non-critical background
requests share the non-critical entries of a ring queue, max_noncritical of
them, 8 at the default depth of 10. When a request ended,
fuse_uring_end_active() offered the freed slot first to the kind of request
that freed it. Once background requests held every slot, each one that ended
was replaced by the next one from the background list, and a foreground
request on that queue was admitted only at a moment when the background list
happened to be empty. The opposite holds too: a foreground load that keeps its
list full holds back async writes the same way.

Admit with one helper, fuse_uring_flush_queue(), wherever bg_lock is held: on
background arrival, when a slot is freed while background requests wait, when
an entry is registered, and when the queue stops. Critical background requests
still go first. A free non-critical slot then goes to the only kind that can
take it, or, when both kinds can, to the kind that did not get the previous
contested slot, tracked by a per-queue noncrit_bg_turn. While a request of
either kind can be admitted, it is thus admitted within two non-critical
completions on its queue. A background request that the connection-wide
max_background budget holds back cannot take a slot and does not contend.
Without contention nothing changes: an async-only or a metadata-only load gets
every slot as before.

A background request that ended used to count in fc->active_background until
fuse_request_end() ran, after its slot had been handed on. When the server
commits several requests in one io_uring_enter(), fuse_request_end() runs for
all of them later from task work, so their budget was still counted while
their slots were handed on, and foreground took those slots without
contending. When background requests wait on the queue,
fuse_uring_end_active() now gives back the budget itself, under bg_lock and
before it admits, and marks the request FR_URING_BG_RETURNED so that
fuse_request_end() does not give it back again, and admission no longer counts
requests that have already ended. Without waiting background requests the
budget is still given back in fuse_request_end(), and the completion does not
take bg_lock.

Foreground arrival still admits only foreground requests, without bg_lock: a
background request that waits while a slot is free is held by the background
limits, so it could not take the slot. A completion with no background request
waiting does the same, so it no longer takes the connection-wide bg_lock on
every non-critical completion.

@yongzech yongzech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main concern: the fairness guarantees in the commit message ("within two non-critical completions", "no slot stays free") don't fully hold. See the inline comments.

One finding is on a line outside the diff:

fs/fuse/dev_uring.c:401 fuse_uring_flush_bg() (stop path): it sets queue->stopped and calls only fuse_uring_flush_queue_bg(). On connection stop, max_background becomes UINT_MAX and every bg request floods past max_noncritical, while fg requests waiting on fuse_req_fg_queue stay there until abort. Calling fuse_uring_flush_queue() here would admit both.

Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
@hbirth

hbirth commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator

This is a nice workaround for a workaround, but I don't have a better idea at the moment ;-)

@hazhou-ddn

Copy link
Copy Markdown
Author

This is a nice workaround for a workaround, but I don't have a better idea at the moment ;-)

If upstream designed to support write-back cache for multiple client nodes, then this (fuse io-uring) was a day-1 design issue:-) As Bernd mentioned in the Slack, his initial design contained "such design" as we did in redfs kernel now, but dropped it to simplify it (I guess at that time the upstream didn't support the write-back cache, so no such design would not hit any problem) :)

@hazhou-ddn
hazhou-ddn force-pushed the redfs-ubuntu-hwe-6.17.0-16.16-24.04.1-fair-admit branch from 769d1f2 to d6c406a Compare November 8, 2026 08:27
@hazhou-ddn

Copy link
Copy Markdown
Author

fs/fuse/dev_uring.c:401 fuse_uring_flush_bg() (stop path): it sets queue->stopped and calls only fuse_uring_flush_queue_bg(). ...

@yongzech Done. The stop path now calls fuse_uring_flush_queue(). That alone wasn't enough, because fuse_uring_fg_admissible() still applied the non-critical limit while bg on a stopped queue skipped it. The bg flood pushed active_noncritical over the limit, and fg waited until it drained. A stopped queue now skips the limit for fg as well, so both kinds drain during fuse_flush_requests().

@yongzech yongzech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Follow-up review of the new head d6c406a375e8, after the force-push.

Earlier findings:

# Finding Status
1 end_active runs before fuse_request_end returns the bg budget Partly fixed: released handles a single completion; see inline at 312
2 bq_req used flush_queue_bg Fixed
3 fuse_request_end doesn't re-flush uring queues Still present; see below
4 Stop path didn't admit fg Fixed as written, but it doesn't drain; see inline at 210
5 Turn didn't track uncontested grants No longer applicable
6 register holds bg_lock longer Declined; agreed the cost is small
7 max_noncritical recomputed per iteration Declined; see inline at 179 for a way to fold it
8 Three admission helpers Fixed

On #3, which is outside the diff: fs/fuse/dev_uring.c:1979, fg arrival through fuse_uring_flush_queue_fg(). I agree nothing waits forever, because a held bg request implies an active bg request on that queue. But budget freed by fuse_request_end() on another queue (dev.c:496 flushes only the legacy list) does not re-flush this queue. Example: queue B has a free non-critical slot and a bg write held only by the global budget. A bg request on queue A ends. Every fg arrival on B then takes the free slot through flush_queue_fg, with no turn flip, until B's own bg request completes. That delays writeback and fsync on B. Returning the budget at end_active (inline at 123) fixes this too.

The PR description is also out of date after the force-push. It still says "no slot stays free while a request that could use it waits" and "The arrival paths keep admitting only their own kind". Neither holds for the new commit.

Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
Comment thread fs/fuse/dev_uring.c Resolved
With the writeback cache, foreground requests and non-critical background
requests share the non-critical entries of a ring queue, max_noncritical of
them, 8 at the default depth of 10. When a request ended,
fuse_uring_end_active() offered the freed slot first to the kind of request
that freed it. Once background requests held every slot, each one that ended
was replaced by the next one from the background list, and a foreground
request on that queue was admitted only at a moment when the background list
happened to be empty. The opposite holds too: a foreground load that keeps its
list full holds back async writes the same way.

Admit with one helper, fuse_uring_flush_queue(), wherever bg_lock is held: on
background arrival, when a slot is freed while background requests wait, when
an entry is registered, and when the queue stops. Critical background requests
still go first. A free non-critical slot then goes to the only kind that can
take it, or, when both kinds can, to the kind that did not get the previous
contested slot, tracked by a per-queue noncrit_bg_turn. While a request of
either kind can be admitted, it is thus admitted within two non-critical
completions on its queue. A background request that the connection-wide
max_background budget holds back cannot take a slot and does not contend.
Without contention nothing changes: an async-only or a metadata-only load gets
every slot as before.

A background request that ended used to count in fc->active_background until
fuse_request_end() ran, after its slot had been handed on. When the server
commits several requests in one io_uring_enter(), fuse_request_end() runs for
all of them later from task work, so their budget was still counted while
their slots were handed on, and foreground took those slots without
contending. When background requests wait on the queue,
fuse_uring_end_active() now gives back the budget itself, under bg_lock and
before it admits, and marks the request FR_URING_BG_RETURNED so that
fuse_request_end() does not give it back again, and admission no longer counts
requests that have already ended. Without waiting background requests the
budget is still given back in fuse_request_end(), and the completion does not
take bg_lock.

Foreground arrival still admits only foreground requests, without bg_lock: a
background request that waits while a slot is free is held by the background
limits, so it could not take the slot. A completion with no background request
waiting does the same, so it no longer takes the connection-wide bg_lock on
every non-critical completion.

Signed-off-by: Hai Zhong Zhou <hazhou@ddn.com>
@hazhou-ddn
hazhou-ddn force-pushed the redfs-ubuntu-hwe-6.17.0-16.16-24.04.1-fair-admit branch from d6c406a to 3444dea Compare November 9, 2026 04:58
@hazhou-ddn

Copy link
Copy Markdown
Author

@yongzech Thanks for the follow-up. Pushed 3444dea59fdc, and the PR description now matches the commit.

On #3 (cross-queue budget): returning the budget in end_active doesn't fix this. That return flushes only the queue where the request ended, so queue B still isn't re-flushed when budget is freed on queue A. As you agreed, a bg request held back by the budget on B means B has its own bg request in flight. That completion takes the slow path, because bg waits on B, and the turn then applies. So fg arrivals on B can take free slots ahead of that write only until B's next bg completion. Waking B from A would mean cross-queue wake-ups, which the existing design avoids on purpose (see the comment above fuse_uring_flush_queue_bg_crit()). I'd rather leave that out of this PR.

@hbirth

hbirth commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

@hazhou-ddn can we merge this after the two splice write optimixations, so that we have one commit for splice in redffs ... and then this ... and then later the mtime change?

@hazhou-ddn

Copy link
Copy Markdown
Author

@hazhou-ddn can we merge this after the two splice write optimixations, so that we have one commit for splice in redffs ... and then this ... and then later the mtime change?

Sure, we can do the merge as this order. So this implies the "two splice write optimizations" needs to be in 2.5.1?

@hbirth

hbirth commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

@hazhou-ddn I think so, since there is already communication going on about the splice write improvement. So I assumed ;-)

@yongzech yongzech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 3444dea. My findings from the review of d6c406a are resolved, and I found no new correctness bug.

  • Bg budget: fuse_uring_end_active() now gives back the budget under bg_lock before it hands on the freed slot, and FR_URING_BG_RETURNED keeps fuse_request_end() from giving it back twice. In the batched example, each of K commits in one io_uring_enter() returns its share before it admits, so fc->active_background stays exact. Every caller of end_active() ends the request with fuse_request_end() afterwards, directly or from task work: __fuse_uring_req_end, the commit-error path, and remove_pending_req (fg only). Teardown, abort and the fast path still give the budget back in fuse_request_end() as before.
  • Stopped queue: dropping the fg override and the commit-message claim is right, and with one copy of the override left there is nothing to drift.
  • Cross-queue re-flush (#3): agreed to leave it out. A bg request held back by the budget on queue B means B has a bg request in flight, and its completion re-flushes B.
  • Lock order is unchanged (queue->lock, then bg_lock). The fast-path test of the bg lists is stable under queue->lock.

Fine to merge after the splice changes in #281, as agreed with @hbirth.

Reviewed together with Claude Code.

@hbirth
hbirth merged commit 255d88e into DDNStorage:redfs-ubuntu-hwe-6.17.0-16.16-24.04.1 Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants