From 4b70ffe375f6a8891e81081b97ff055b754a610b Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 9 Jul 2025 20:53:23 +0000 Subject: [PATCH] Fix short submits and also fix the registration ops enum, which somehow stopped compiling overnight --- Sources/CSystem/include/io_uring.h | 18 ---------------- Sources/System/IORing.swift | 33 +++++++++++++++++++++++------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/Sources/CSystem/include/io_uring.h b/Sources/CSystem/include/io_uring.h index 2d081af..0068f50 100644 --- a/Sources/CSystem/include/io_uring.h +++ b/Sources/CSystem/include/io_uring.h @@ -33,24 +33,6 @@ struct swift_io_uring_getevents_arg { __u64 ts; }; -//This was #defines in older headers, so we redeclare it to get a consistent import -typedef enum : __u32 { - SWIFT_IORING_REGISTER_BUFFERS = 0, - SWIFT_IORING_UNREGISTER_BUFFERS = 1, - SWIFT_IORING_REGISTER_FILES = 2, - SWIFT_IORING_UNREGISTER_FILES = 3, - SWIFT_IORING_REGISTER_EVENTFD = 4, - SWIFT_IORING_UNREGISTER_EVENTFD = 5, - SWIFT_IORING_REGISTER_FILES_UPDATE = 6, - SWIFT_IORING_REGISTER_EVENTFD_ASYNC = 7, - SWIFT_IORING_REGISTER_PROBE = 8, - SWIFT_IORING_REGISTER_PERSONALITY = 9, - SWIFT_IORING_UNREGISTER_PERSONALITY = 10, - - /* this goes last */ - SWIFT_IORING_REGISTER_LAST -} SWIFT_IORING_REGISTER_OPS; - static inline int io_uring_register(int fd, unsigned int opcode, void *arg, unsigned int nr_args) { diff --git a/Sources/System/IORing.swift b/Sources/System/IORing.swift index 4a28f7c..5546540 100644 --- a/Sources/System/IORing.swift +++ b/Sources/System/IORing.swift @@ -8,6 +8,21 @@ import Musl #endif import Synchronization +//This was #defines in older headers, so we redeclare it to get a consistent import +internal enum RegistrationOps: UInt32 { + case registerBuffers = 0 + case unregisterBuffers = 1 + case registerFiles = 2 + case unregisterFiles = 3 + case registerEventFD = 4 + case unregisterEventFD = 5 + case registerFilesUpdate = 6 + case registerEventFDAsync = 7 + case registerProbe = 8 + case registerPersonality = 9 + case unregisterPersonality = 10 +} + extension UnsafeMutableRawPointer { func advanced(by offset: UInt32) -> UnsafeMutableRawPointer { return advanced(by: Int(offset)) @@ -71,6 +86,7 @@ internal func _tryWriteRequest( //Tell the kernel that we've submitted requests and/or are waiting for completions @inlinable internal func _enter( + ring: borrowing SQRing, ringDescriptor: Int32, numEvents: UInt32, minCompletions: UInt32, @@ -91,6 +107,9 @@ internal func _enter( continue } else if ret < 0 { throw(Errno(rawValue: -ret)) + } else if _getSubmissionQueueCount(ring: ring) > 0 { + // See https://github.com/axboe/liburing/issues/309, in some cases not all pending requests are submitted + continue } else { return ret } @@ -101,7 +120,7 @@ internal func _enter( internal func _submitRequests(ring: borrowing SQRing, ringDescriptor: Int32) throws(Errno) { let flushedEvents = _flushQueue(ring: ring) _ = try _enter( - ringDescriptor: ringDescriptor, numEvents: flushedEvents, minCompletions: 0, flags: 0) + ring: ring, ringDescriptor: ringDescriptor, numEvents: flushedEvents, minCompletions: 0, flags: 0) } @inlinable @@ -536,7 +555,7 @@ public struct IORing: ~Copyable { let result = withUnsafePointer(to: &rawfd) { fdptr in let result = io_uring_register( ringDescriptor, - SWIFT_IORING_REGISTER_EVENTFD.rawValue, + RegistrationOps.registerEventFD.rawValue, UnsafeMutableRawPointer(mutating: fdptr), 1 ) @@ -551,7 +570,7 @@ public struct IORing: ~Copyable { public mutating func unregisterEventFD() throws(Errno) { let result = io_uring_register( ringDescriptor, - IORING_UNREGISTER_EVENTFD.rawValue, + RegistrationOps.unregisterEventFD.rawValue, nil, 0 ) @@ -569,7 +588,7 @@ public struct IORing: ~Copyable { let regResult = files.withUnsafeBufferPointer { bPtr in let result = io_uring_register( self.ringDescriptor, - IORING_REGISTER_FILES.rawValue, + RegistrationOps.registerFiles.rawValue, UnsafeMutableRawPointer(mutating: bPtr.baseAddress!), UInt32(truncatingIfNeeded: count) ) @@ -588,7 +607,7 @@ public struct IORing: ~Copyable { public func unregisterFiles() throws { let result = io_uring_register( ringDescriptor, - IORING_UNREGISTER_FILES.rawValue, + RegistrationOps.unregisterFiles.rawValue, nil, 0 ) @@ -613,7 +632,7 @@ public struct IORing: ~Copyable { let regResult = iovecs.withUnsafeBufferPointer { bPtr in let result = io_uring_register( self.ringDescriptor, - IORING_REGISTER_BUFFERS.rawValue, + RegistrationOps.registerBuffers.rawValue, UnsafeMutableRawPointer(mutating: bPtr.baseAddress!), UInt32(truncatingIfNeeded: buffers.count) ) @@ -662,7 +681,7 @@ public struct IORing: ~Copyable { public func unregisterBuffers() throws { let result = io_uring_register( self.ringDescriptor, - IORING_UNREGISTER_BUFFERS.rawValue, + RegistrationOps.unregisterBuffers.rawValue, nil, 0 )