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 {
2828 /// call on how many entries the submission and completion queues will ultimately have,
2929 /// see https://github.com/torvalds/linux/blob/v5.8/fs/io_uring.c#L8027-L8050.
3030 /// Matches the interface of io_uring_queue_init() in liburing.
31 pub fn init(entries: u32, flags: u32) !IO_Uring {
32 var params = io_uring_params {
33 .sq_entries = 0,
34 .cq_entries = 0,
31 pub fn init(entries: u12, flags: u32) !IO_Uring {
32 var params = mem.zeroInit(io_uring_params, .{
3533 .flags = flags,
36 .sq_thread_cpu = 0,
37 .sq_thread_idle = 1000,
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.
34 .sq_thread_idle = 1000
35 });
4636 return try IO_Uring.init_params(entries, &params);
4737 }
4838
......@@ -52,8 +42,10 @@ pub const IO_Uring = struct {
5242 /// You may only set the `flags`, `sq_thread_cpu` and `sq_thread_idle` parameters.
5343 /// Every other parameter belongs to the kernel and must be zeroed.
5444 /// Matches the interface of io_uring_queue_init_params() in liburing.
55 pub fn init_params(entries: u32, p: *io_uring_params) !IO_Uring {
56 assert(entries >= 1 and entries <= 4096 and std.math.isPowerOfTwo(entries));
45 pub fn init_params(entries: u12, p: *io_uring_params) !IO_Uring {
46 if (entries == 0) return error.EntriesZero;
47 if (!std.math.isPowerOfTwo(entries)) return error.EntriesNotPowerOfTwo;
48
5749 assert(p.sq_entries == 0);
5850 assert(p.cq_entries == 0);
5951 assert(p.features == 0);
......@@ -684,6 +676,9 @@ test "structs and offsets" {
684676 testing.expectEqual(0, linux.IORING_OFF_SQ_RING);
685677 testing.expectEqual(0x8000000, linux.IORING_OFF_CQ_RING);
686678 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));
687682}
688683
689684test "queue_nop" {