mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 10:07:28 +01:00
ublk: reclaim a stale device only when its server is proven dead; never unmap a running queue; submit every SQE
Review P1s: - reclaimStale reads the device's recorded server pid (GET_DEV_INFO) and deletes the device only if that process no longer exists (signal 0 -> ESRCH). Owner alive, liveness unknown, never started (no pid) or info unreadable: Start refuses and sends no STOP/DEL. A reused pid reads as alive: refused. - settleQueues waits until one shared deadline and releases only queues whose thread has exited; a queue still running keeps its mappings (leaked, fail closed) and Close reports it, and skips DEL_DEV, which would otherwise wait forever for the char device the mappings hold. - submitAndWait offers the ring's unconsumed SQEs again after a short io_uring_enter until the kernel has taken all of them, then waits for completions; a call that consumes nothing is an error. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
885fe6d116
commit
8640169e5c
4 files changed
+80
-24
No files matched your search
@@ -171,12 +171,18 @@ func (d *Device) teardown(added bool) error {
|
||||
for i, q := range d.queues {
|
||||
qs[i] = q
|
||||
}
|
||||
note(settleQueues(qs, queueStopTimeout))
|
||||
settleErr := settleQueues(qs, queueStopTimeout)
|
||||
note(settleErr)
|
||||
if d.charFD >= 0 {
|
||||
note(unix.Close(d.charFD))
|
||||
d.charFD = -1
|
||||
}
|
||||
if d.path != "" {
|
||||
if d.path != "" && settleErr != nil {
|
||||
// A queue's mappings are kept (it may still run), which holds the
|
||||
// char device open: DEL_DEV would wait for its release forever.
|
||||
// The device stays (it fails I/O); the error above reports it.
|
||||
note(fmt.Errorf("ublk: device %d left in place: a queue did not stop", d.devID))
|
||||
} else if d.path != "" {
|
||||
if _, err := d.ctrlCommand(uCmdDelDev, d.devID, nil, 0); err != nil {
|
||||
note(fmt.Errorf("ublk: delete device: %w", err))
|
||||
}
|
||||
|
||||
@@ -12,11 +12,29 @@ type controller interface {
|
||||
}
|
||||
|
||||
// reclaimStale handles a device already present at id before this server
|
||||
// creates its own. RED placeholder: removes it unconditionally.
|
||||
// creates its own. It is deleted only with proof that its server is dead:
|
||||
// the kernel's record of the server pid (set by START_DEV) names a process
|
||||
// that no longer exists. A device whose owner may be alive, never started
|
||||
// (no pid), or cannot be read is refused untouched; an operator removes it
|
||||
// once it is proven unused. A reused pid reads as alive: refused (safe).
|
||||
func reclaimStale(c controller, id uint32, exists bool, alive func(pid int32) (bool, error)) error {
|
||||
if !exists {
|
||||
return nil
|
||||
}
|
||||
var info devInfo
|
||||
if _, err := c.command(uCmdGetDevInfo, id, bytesOf(&info), 0); err != nil {
|
||||
return fmt.Errorf("ublk: device %d exists and its owner cannot be read (%v): not deleting it", id, err)
|
||||
}
|
||||
if info.SrvPID <= 0 {
|
||||
return fmt.Errorf("ublk: device %d exists with no recorded server (never started, or starting): not deleting it; remove it once proven unused", id)
|
||||
}
|
||||
live, err := alive(info.SrvPID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ublk: device %d: liveness of its server pid %d unknown (%v): not deleting it", id, info.SrvPID, err)
|
||||
}
|
||||
if live {
|
||||
return fmt.Errorf("ublk: device %d belongs to the live server pid %d: refusing", id, info.SrvPID)
|
||||
}
|
||||
_, _ = c.command(uCmdStopDev, id, nil, 0)
|
||||
if _, err := c.command(uCmdDelDev, id, nil, 0); err != nil {
|
||||
return fmt.Errorf("ublk: delete stale device %d: %w", id, err)
|
||||
@@ -32,19 +50,33 @@ type settler interface {
|
||||
name() string
|
||||
}
|
||||
|
||||
// settleQueues waits for the queues to stop. RED placeholder: releases
|
||||
// every queue even when it did not stop in time.
|
||||
// settleQueues waits, until one shared deadline, for the queues to stop
|
||||
// and releases each one that did. A queue that did not stop keeps its
|
||||
// mappings: its thread may still read descriptors or write buffers, so
|
||||
// they are leaked (fail closed) and the failure is returned.
|
||||
func settleQueues(qs []settler, timeout time.Duration) error {
|
||||
var firstErr error
|
||||
deadline := time.NewTimer(timeout)
|
||||
defer deadline.Stop()
|
||||
expired := false
|
||||
for _, q := range qs {
|
||||
if !expired {
|
||||
select {
|
||||
case <-q.doneCh():
|
||||
q.release()
|
||||
continue
|
||||
case <-deadline.C:
|
||||
expired = true
|
||||
}
|
||||
}
|
||||
select {
|
||||
case <-q.doneCh():
|
||||
case <-time.After(timeout):
|
||||
case <-q.doneCh(): // stopped by now: safe to release
|
||||
q.release()
|
||||
default:
|
||||
if firstErr == nil {
|
||||
firstErr = fmt.Errorf("ublk: %s did not stop", q.name())
|
||||
firstErr = fmt.Errorf("ublk: %s did not stop; its mappings are kept (leaked) while its thread may run", q.name())
|
||||
}
|
||||
}
|
||||
q.release()
|
||||
}
|
||||
return firstErr
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package ublk
|
||||
|
||||
import "unsafe"
|
||||
|
||||
// Kernel ABI of the ublk driver (include/uapi/linux/ublk_cmd.h). The
|
||||
// struct layouts and ioctl encodings are checked by uapi_test.go.
|
||||
|
||||
@@ -114,6 +116,12 @@ type params struct {
|
||||
Basic paramBasic
|
||||
}
|
||||
|
||||
// bytesOf views a fixed-size struct as bytes (an inline command or a
|
||||
// command buffer the kernel reads or fills).
|
||||
func bytesOf[T any](v *T) []byte {
|
||||
return unsafe.Slice((*byte)(unsafe.Pointer(v)), unsafe.Sizeof(*v))
|
||||
}
|
||||
|
||||
// iowr and ior encode a ublk command number like the kernel's _IOWR and
|
||||
// _IOR('u', nr, size).
|
||||
func iowr(nr, size uint32) uint32 { return 3<<30 | size<<16 | uint32('u')<<8 | nr }
|
||||
|
||||
@@ -142,26 +142,41 @@ func (r *uring) prepare(op uint8, fd int, cmdOp uint32, cmd []byte, userData uin
|
||||
return nil
|
||||
}
|
||||
|
||||
// submitAndWait publishes the prepared SQEs and waits for at least
|
||||
// minComplete completions.
|
||||
// submitAndWait publishes the prepared SQEs, makes sure the kernel has
|
||||
// consumed every one of them (a short io_uring_enter leaves the rest in
|
||||
// the ring: they are offered again), then waits for at least minComplete
|
||||
// completions. A call that consumes nothing is an error: the unsubmitted
|
||||
// SQEs would otherwise be reported as submitted.
|
||||
func (r *uring) submitAndWait(minComplete uint32) error {
|
||||
toSubmit := r.pending
|
||||
atomic.StoreUint32(r.sqTail, *r.sqTail+toSubmit)
|
||||
atomic.StoreUint32(r.sqTail, *r.sqTail+r.pending)
|
||||
r.pending = 0
|
||||
var flags uintptr
|
||||
if minComplete > 0 {
|
||||
flags = enterGetEvents
|
||||
for {
|
||||
outstanding := atomic.LoadUint32(r.sqTail) - atomic.LoadUint32(r.sqHead)
|
||||
if outstanding == 0 {
|
||||
break
|
||||
}
|
||||
n, errno := r.enter(r.fd, outstanding, 0, 0)
|
||||
switch {
|
||||
case errno == unix.EINTR:
|
||||
continue
|
||||
case errno != 0:
|
||||
return fmt.Errorf("ublk: io_uring_enter: %w", errno)
|
||||
case n == 0 && atomic.LoadUint32(r.sqTail)-atomic.LoadUint32(r.sqHead) == outstanding:
|
||||
return fmt.Errorf("ublk: io_uring_enter consumed none of %d SQEs", outstanding)
|
||||
}
|
||||
}
|
||||
if minComplete == 0 {
|
||||
return nil
|
||||
}
|
||||
for {
|
||||
_, errno := r.enter(r.fd, toSubmit, minComplete, flags)
|
||||
_, errno := r.enter(r.fd, 0, minComplete, enterGetEvents)
|
||||
switch errno {
|
||||
case 0:
|
||||
return nil
|
||||
case unix.EINTR:
|
||||
toSubmit = 0 // the kernel consumed the SQEs before the interrupt
|
||||
continue
|
||||
default:
|
||||
return fmt.Errorf("ublk: io_uring_enter: %w", errno)
|
||||
return fmt.Errorf("ublk: io_uring_enter wait: %w", errno)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -201,8 +216,3 @@ func (r *uring) close() error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// bytesOf views a fixed-size struct as bytes for an inline command.
|
||||
func bytesOf[T any](v *T) []byte {
|
||||
return unsafe.Slice((*byte)(unsafe.Pointer(v)), unsafe.Sizeof(*v))
|
||||
}
|
||||
Reference in new issue
Block a user