mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 18:47:20 +01:00
phase168: drain accepted io_uring requests safely
This commit is contained in:
1 parent
37ed88b6b2
commit
8b979ae02a
4 files changed
+170
-36
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
@@ -123,6 +124,7 @@ type ioUring struct {
|
||||
cqes []ioUringCQE
|
||||
|
||||
submitSyscalls int
|
||||
enterCall func(toSubmit, minComplete, flags uint32) (int, error)
|
||||
}
|
||||
|
||||
func runProbe(requestedDepth uint32) (probeReport, error) {
|
||||
@@ -412,28 +414,40 @@ func (ring *ioUring) submitAndWait(operations []ringOperation) ([]ioUringCQE, in
|
||||
}
|
||||
}
|
||||
|
||||
submissionHead := atomic.LoadUint32(ring.sqHead)
|
||||
submitted := 0
|
||||
for submitted < len(operations) {
|
||||
count, err := ring.enter(uint32(len(operations)-submitted), 0, 0)
|
||||
if err != nil {
|
||||
return nil, submitted, err
|
||||
before := submitted
|
||||
_, err := ring.enter(uint32(len(operations)-submitted), 0, 0)
|
||||
submitted = int(atomic.LoadUint32(ring.sqHead) - submissionHead)
|
||||
if submitted > len(operations) {
|
||||
return nil, 0, fmt.Errorf(
|
||||
"submission head advanced by %d want at most %d",
|
||||
submitted,
|
||||
len(operations),
|
||||
)
|
||||
}
|
||||
if count == 0 {
|
||||
return nil, submitted, errors.New("io_uring_enter submitted zero operations")
|
||||
if err != nil {
|
||||
if errors.Is(err, unix.EINTR) {
|
||||
continue
|
||||
}
|
||||
if submitted == 0 {
|
||||
return nil, 0, err
|
||||
}
|
||||
completions := ring.waitForAccepted(submitted)
|
||||
return completions, submitted, err
|
||||
}
|
||||
if submitted == before {
|
||||
if submitted == 0 {
|
||||
return nil, 0, errors.New("io_uring_enter submitted zero operations")
|
||||
}
|
||||
completions := ring.waitForAccepted(submitted)
|
||||
return completions, submitted, errors.New("io_uring_enter submitted zero operations")
|
||||
}
|
||||
submitted += count
|
||||
}
|
||||
|
||||
completions := make([]ioUringCQE, 0, len(operations))
|
||||
for len(completions) < len(operations) {
|
||||
completions = append(completions, ring.drainCompletions()...)
|
||||
if len(completions) >= len(operations) {
|
||||
break
|
||||
}
|
||||
if _, err := ring.enter(0, uint32(len(operations)-len(completions)), ioUringEnterGetEvents); err != nil {
|
||||
return completions, submitted, err
|
||||
}
|
||||
}
|
||||
completions := ring.waitForAccepted(len(operations))
|
||||
if len(completions) != len(operations) {
|
||||
return completions, submitted, fmt.Errorf(
|
||||
"completion count=%d want=%d",
|
||||
@@ -444,6 +458,24 @@ func (ring *ioUring) submitAndWait(operations []ringOperation) ([]ioUringCQE, in
|
||||
return completions, submitted, nil
|
||||
}
|
||||
|
||||
// waitForAccepted does not return until every accepted SQE has a terminal CQE.
|
||||
// Returning earlier would let the kernel retain a pointer to a Go-owned buffer
|
||||
// after its final strong reference has disappeared. The gate process supplies
|
||||
// the outer timeout for a kernel that can no longer make progress.
|
||||
func (ring *ioUring) waitForAccepted(expected int) []ioUringCQE {
|
||||
completions := make([]ioUringCQE, 0, expected)
|
||||
for len(completions) < expected {
|
||||
completions = append(completions, ring.drainCompletions()...)
|
||||
if len(completions) >= expected {
|
||||
break
|
||||
}
|
||||
if _, err := ring.enter(0, uint32(expected-len(completions)), ioUringEnterGetEvents); err != nil {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
}
|
||||
return completions
|
||||
}
|
||||
|
||||
func (ring *ioUring) enqueue(operation ringOperation) error {
|
||||
head := atomic.LoadUint32(ring.sqHead)
|
||||
tail := atomic.LoadUint32(ring.sqTail)
|
||||
@@ -469,20 +501,32 @@ func (ring *ioUring) enqueue(operation ringOperation) error {
|
||||
}
|
||||
|
||||
func (ring *ioUring) enter(toSubmit, minComplete, flags uint32) (int, error) {
|
||||
ring.submitSyscalls++
|
||||
count, _, errno := unix.Syscall6(
|
||||
unix.SYS_IO_URING_ENTER,
|
||||
uintptr(ring.fd),
|
||||
uintptr(toSubmit),
|
||||
uintptr(minComplete),
|
||||
uintptr(flags),
|
||||
0,
|
||||
0,
|
||||
)
|
||||
if errno != 0 {
|
||||
return int(count), errno
|
||||
for {
|
||||
ring.submitSyscalls++
|
||||
if ring.enterCall != nil {
|
||||
count, err := ring.enterCall(toSubmit, minComplete, flags)
|
||||
if toSubmit == 0 && errors.Is(err, unix.EINTR) {
|
||||
continue
|
||||
}
|
||||
return count, err
|
||||
}
|
||||
count, _, errno := unix.Syscall6(
|
||||
unix.SYS_IO_URING_ENTER,
|
||||
uintptr(ring.fd),
|
||||
uintptr(toSubmit),
|
||||
uintptr(minComplete),
|
||||
uintptr(flags),
|
||||
0,
|
||||
0,
|
||||
)
|
||||
if toSubmit == 0 && errno == unix.EINTR {
|
||||
continue
|
||||
}
|
||||
if errno != 0 {
|
||||
return int(count), errno
|
||||
}
|
||||
return int(count), nil
|
||||
}
|
||||
return int(count), nil
|
||||
}
|
||||
|
||||
func (ring *ioUring) drainCompletions() []ioUringCQE {
|
||||
|
||||
@@ -3,8 +3,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestLinuxUAPIStructSizes(t *testing.T) {
|
||||
@@ -40,3 +44,72 @@ func TestRunProbeCompletesWritesFsyncAndReopen(t *testing.T) {
|
||||
t.Fatalf("verified bytes=%d want=%d", report.VerifiedBytes, 3*probeBlockSize)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnterRetriesInterruptedWait(t *testing.T) {
|
||||
calls := 0
|
||||
ring := &ioUring{
|
||||
fd: -1,
|
||||
enterCall: func(toSubmit, minComplete, flags uint32) (int, error) {
|
||||
calls++
|
||||
if calls == 1 {
|
||||
return 0, unix.EINTR
|
||||
}
|
||||
if toSubmit != 0 || minComplete != 3 || flags != ioUringEnterGetEvents {
|
||||
t.Fatalf(
|
||||
"retry arguments=(%d,%d,%d) want=(0,3,%d)",
|
||||
toSubmit,
|
||||
minComplete,
|
||||
flags,
|
||||
ioUringEnterGetEvents,
|
||||
)
|
||||
}
|
||||
return 0, nil
|
||||
},
|
||||
}
|
||||
if _, err := ring.enter(0, 3, ioUringEnterGetEvents); err != nil {
|
||||
t.Fatalf("enter returned interrupted wait: %v", err)
|
||||
}
|
||||
if calls != 2 || ring.submitSyscalls != 2 {
|
||||
t.Fatalf("calls=%d submitSyscalls=%d want=2/2", calls, ring.submitSyscalls)
|
||||
}
|
||||
|
||||
ring.enterCall = func(uint32, uint32, uint32) (int, error) {
|
||||
return 0, errors.New("terminal test error")
|
||||
}
|
||||
if _, err := ring.enter(0, 1, ioUringEnterGetEvents); err == nil {
|
||||
t.Fatal("non-EINTR error was unexpectedly retried as success")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAcceptedWaitDoesNotReturnOnTransientError(t *testing.T) {
|
||||
var head uint32
|
||||
var tail uint32
|
||||
var mask uint32
|
||||
calls := 0
|
||||
ring := &ioUring{
|
||||
fd: -1,
|
||||
cqHead: &head,
|
||||
cqTail: &tail,
|
||||
cqMask: &mask,
|
||||
cqes: []ioUringCQE{{
|
||||
UserData: 17,
|
||||
Result: probeBlockSize,
|
||||
}},
|
||||
enterCall: func(uint32, uint32, uint32) (int, error) {
|
||||
calls++
|
||||
if calls == 1 {
|
||||
return 0, errors.New("transient wait failure")
|
||||
}
|
||||
atomic.StoreUint32(&tail, 1)
|
||||
return 0, nil
|
||||
},
|
||||
}
|
||||
|
||||
completions := ring.waitForAccepted(1)
|
||||
if calls != 2 {
|
||||
t.Fatalf("wait calls=%d want=2", calls)
|
||||
}
|
||||
if len(completions) != 1 || completions[0].UserData != 17 {
|
||||
t.Fatalf("completions=%+v want user_data=17", completions)
|
||||
}
|
||||
}
|
||||
+5
-3
@@ -1,6 +1,7 @@
|
||||
# Phase 168 D1 io_uring Capability Gate
|
||||
|
||||
Status: developer gate PASS at `aa538bf`; independent review and QA pending.
|
||||
Status: initial developer/QA happy-path PASS at `e919720`; adversarial review
|
||||
found a submitted-buffer lifetime blocker. Hardened re-validation pending.
|
||||
|
||||
## Scope
|
||||
|
||||
@@ -36,7 +37,7 @@ probe package a public storage abstraction.
|
||||
|
||||
## Developer Evidence
|
||||
|
||||
Exact source commit: `aa538bf`
|
||||
Initial source commit: `e919720`
|
||||
|
||||
Environment:
|
||||
|
||||
@@ -88,7 +89,8 @@ The three writes use offsets `0`, `12288`, and `4096`, so they are deliberately
|
||||
non-contiguous in submission order. The barrier is an `IORING_OP_FSYNC`, not an
|
||||
`os.File.Sync` hidden outside the ring. Every CQE is matched by `user_data`,
|
||||
write lengths are checked, and all payloads are verified after reopening the
|
||||
file.
|
||||
file. The hardened follow-up must additionally retry interrupted waits and keep
|
||||
every accepted request buffer alive through a terminal CQE.
|
||||
|
||||
## D1 Verdict
|
||||
|
||||
|
||||
@@ -32,16 +32,22 @@ if [[ "$(go env GOOS)/$(go env GOARCH)" != "linux/amd64" ]]; then
|
||||
exit 2
|
||||
fi
|
||||
|
||||
go test ./cmd/io-uring-probe -count=1 -v \
|
||||
>"${ARTIFACT_DIR}/probe-tests.log" 2>&1
|
||||
write_summary "linux_probe_test=pass"
|
||||
set +e
|
||||
timeout 30s go run ./cmd/io-uring-probe >"${PROBE_LOG}" 2>&1
|
||||
probe_exit=$?
|
||||
set -e
|
||||
if (( probe_exit != 0 )); then
|
||||
write_summary "phase168_io_uring_capability_status=unsupported"
|
||||
write_summary "probe_exit=${probe_exit}"
|
||||
refusal_reason="$(sed -n 's/^refusal_reason=//p' "${PROBE_LOG}" | head -1)"
|
||||
write_summary "refusal_reason=${refusal_reason:-probe_did_not_report_reason}"
|
||||
exit "${probe_exit}"
|
||||
fi
|
||||
|
||||
go run ./cmd/io-uring-probe | tee "${PROBE_LOG}" >/dev/null
|
||||
require_probe_line '^io_uring_probe_status=ok$'
|
||||
require_probe_line '^platform=linux/amd64$'
|
||||
require_probe_line '^io_uring_supported=true$'
|
||||
require_probe_line '^refusal_reason=-$'
|
||||
require_probe_line '^queue_depth=[4-9][0-9]*$'
|
||||
require_probe_line '^write_opcode_supported=true$'
|
||||
require_probe_line '^fsync_opcode_supported=true$'
|
||||
require_probe_line '^submitted_ops=4$'
|
||||
@@ -52,11 +58,20 @@ require_probe_line '^verified_bytes=12288$'
|
||||
require_probe_line '^implementation=raw_linux_uapi$'
|
||||
require_probe_line '^dependency=golang.org/x/sys/unix$'
|
||||
require_probe_line '^cgo_required=false$'
|
||||
queue_depth="$(sed -n 's/^queue_depth=//p' "${PROBE_LOG}" | head -1)"
|
||||
if [[ ! "${queue_depth}" =~ ^[0-9]+$ ]] || (( queue_depth < 4 )); then
|
||||
echo "invalid queue depth: ${queue_depth:-missing}" >&2
|
||||
exit 1
|
||||
fi
|
||||
write_summary "linux_write_fsync_reopen=pass"
|
||||
write_summary "required_opcodes=write,fsync"
|
||||
write_summary "dependency_added=false"
|
||||
write_summary "cgo_required=false"
|
||||
|
||||
timeout 60s go test ./cmd/io-uring-probe -count=1 -v \
|
||||
>"${ARTIFACT_DIR}/probe-tests.log" 2>&1
|
||||
write_summary "linux_probe_test=pass"
|
||||
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 \
|
||||
go build -o "${ARTIFACT_DIR}/io-uring-probe-windows-amd64.exe" ./cmd/io-uring-probe
|
||||
write_summary "windows_cross_compile=pass"
|
||||
|
||||
Reference in new issue
Block a user