authorgravatar for joran@ronomon.comJoran Dirk Greef <joran@ronomon.com> 2020-10-04 12:49:48+02:00
committergravatar for joran@ronomon.comJoran Dirk Greef <joran@ronomon.com> 2020-10-04 12:49:48+02:00
loge32c7d06e5c51ff88856c8f48f6fb4fcdf564d17
tree9071ed0834339862facae6bbbebfe280dbc8bdee
parent61ec6cb6d375ff896d892102d8ee7b7d4536b3a5

Limit entries to u12, add errors for invalid entries, use mem.zeroInit


1 files changed, 11 insertions(+), 16 deletions(-)

lib/std/os/linux/io_uring.zig+11-16
...@@ -28,21 +28,11 @@ pub const IO_Uring = struct {...@@ -28,21 +28,11 @@ pub const IO_Uring = struct {
28 /// call on how many entries the submission and completion queues will ultimately have,28 /// call on how many entries the submission and completion queues will ultimately have,
29 /// see https://github.com/torvalds/linux/blob/v5.8/fs/io_uring.c#L8027-L8050.29 /// see https://github.com/torvalds/linux/blob/v5.8/fs/io_uring.c#L8027-L8050.
30 /// Matches the interface of io_uring_queue_init() in liburing.30 /// Matches the interface of io_uring_queue_init() in liburing.
31 pub fn init(entries: u32, flags: u32) !IO_Uring {31 pub fn init(entries: u12, flags: u32) !IO_Uring {
32 var params = io_uring_params {32 var params = mem.zeroInit(io_uring_params, .{
33 .sq_entries = 0,
34 .cq_entries = 0,
35 .flags = flags,33 .flags = flags,
36 .sq_thread_cpu = 0,34 .sq_thread_idle = 1000
37 .sq_thread_idle = 1000,35 });
38 .features = 0,
39 .wq_fd = 0,
40 .resv = [_]u32{0} ** 3,
41 .sq_off = undefined,
42 .cq_off = undefined,
43 };
44 // The kernel will zero the memory of the sq_off and cq_off structs in io_uring_create(),
45 // see https://github.com/torvalds/linux/blob/v5.8/fs/io_uring.c#L7986-L8002.
46 return try IO_Uring.init_params(entries, &params);36 return try IO_Uring.init_params(entries, &params);
47 }37 }
4838
...@@ -52,8 +42,10 @@ pub const IO_Uring = struct {...@@ -52,8 +42,10 @@ pub const IO_Uring = struct {
52 /// You may only set the `flags`, `sq_thread_cpu` and `sq_thread_idle` parameters.42 /// You may only set the `flags`, `sq_thread_cpu` and `sq_thread_idle` parameters.
53 /// Every other parameter belongs to the kernel and must be zeroed.43 /// Every other parameter belongs to the kernel and must be zeroed.
54 /// Matches the interface of io_uring_queue_init_params() in liburing.44 /// Matches the interface of io_uring_queue_init_params() in liburing.
55 pub fn init_params(entries: u32, p: *io_uring_params) !IO_Uring {45 pub fn init_params(entries: u12, p: *io_uring_params) !IO_Uring {
56 assert(entries >= 1 and entries <= 4096 and std.math.isPowerOfTwo(entries));46 if (entries == 0) return error.EntriesZero;
47 if (!std.math.isPowerOfTwo(entries)) return error.EntriesNotPowerOfTwo;
48
57 assert(p.sq_entries == 0);49 assert(p.sq_entries == 0);
58 assert(p.cq_entries == 0);50 assert(p.cq_entries == 0);
59 assert(p.features == 0);51 assert(p.features == 0);
...@@ -684,6 +676,9 @@ test "structs and offsets" {...@@ -684,6 +676,9 @@ test "structs and offsets" {
684 testing.expectEqual(0, linux.IORING_OFF_SQ_RING);676 testing.expectEqual(0, linux.IORING_OFF_SQ_RING);
685 testing.expectEqual(0x8000000, linux.IORING_OFF_CQ_RING);677 testing.expectEqual(0x8000000, linux.IORING_OFF_CQ_RING);
686 testing.expectEqual(0x10000000, linux.IORING_OFF_SQES);678 testing.expectEqual(0x10000000, linux.IORING_OFF_SQES);
679
680 testing.expectError(error.EntriesZero, IO_Uring.init(0, 0));
681 testing.expectError(error.EntriesNotPowerOfTwo, IO_Uring.init(3, 0));
687}682}
688683
689test "queue_nop" {684test "queue_nop" {