linux-support 0.0.25

Comprehensive Linux support for namespaces, cgroups, processes, scheduling, parsing /proc, parsing /sys, signals, hyper threads, CPUS, NUMA nodes, unusual file descriptors, PCI devices and much, much more
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// This file is part of linux-support. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT. No part of linux-support, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
// Copyright © 2020 The developers of linux-support. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/linux-support/master/COPYRIGHT.


/// An io_uring file descriptor.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct IoUringFileDescriptor
{
	raw_file_descriptor: RawFd,
	using_kernel_submission_queue_poll: bool,
	using_io_poll: bool,
}

impl Drop for IoUringFileDescriptor
{
	#[inline(always)]
	fn drop(&mut self)
	{
		self.raw_file_descriptor.close()
	}
}

impl FromRawFd for IoUringFileDescriptor
{
	#[inline(always)]
	unsafe fn from_raw_fd(_raw_file_descriptor: RawFd) -> Self
	{
		unimplemented!("Can not be implemented")
	}
}

impl IntoRawFd for IoUringFileDescriptor
{
	#[inline(always)]
	fn into_raw_fd(self) -> RawFd
	{
		self.raw_file_descriptor
	}
}

impl AsRawFd for IoUringFileDescriptor
{
	#[inline(always)]
	fn as_raw_fd(&self) -> RawFd
	{
		self.raw_file_descriptor
	}
}

impl FileDescriptor for IoUringFileDescriptor
{
}

impl MemoryMappableFileDescriptor for IoUringFileDescriptor
{
}

impl IoUringFileDescriptor
{
	/// Using a kernel thread requires the `CAP_SYS_ADMIN` capability.
	/// `number_of_submission_queue_entries` can be thought of as the submission queue depth; it is clamped to a maximum of 32,768.
	/// `number_of_completion_queue_entries` can be thought of as the completion queue depth; if unspecified, it defaults to double the `number_of_submission_queue_entries` up to a maximum of 65,536.
	fn new(number_of_submission_queue_entries: NonZeroU16, number_of_completion_queue_entries: Option<NonZeroU32>, kernel_submission_queue_thread_configuration: Option<&LinuxKernelSubmissionQueuePollingThreadConfiguration>, shared_work_queue: Option<&Self>) -> io::Result<(Self, io_uring_params)>
	{
		const IORING_MAX_ENTRIES: u32 = 32768;
		const IORING_MAX_CQ_ENTRIES: u32 = 2 * IORING_MAX_ENTRIES;

		let number_of_submission_queue_entries = number_of_submission_queue_entries.get() as u32;
		debug_assert!(number_of_submission_queue_entries < IORING_MAX_ENTRIES);
		
		let (sq_thread_cpu, sq_thread_idle, mut flags) = LinuxKernelSubmissionQueuePollingThreadConfiguration::configure(kernel_submission_queue_thread_configuration, SetupFlags::Clamp);

		let mut parameters = io_uring_params
		{
			// Specified if `flags` contains `SetupFlags::CompletionQueueSize`.
			cq_entries: match number_of_completion_queue_entries
			{
				None => unsafe_uninitialized(),
				Some(number_of_completion_queue_entries) =>
				{
					flags |= SetupFlags::CompletionQueueSize;
					let number_of_completion_queue_entries = number_of_completion_queue_entries.get();
					debug_assert!(number_of_completion_queue_entries <= IORING_MAX_CQ_ENTRIES, "number_of_completion_queue_entries {} exceeds IORING_MAX_CQ_ENTRIES {}", number_of_completion_queue_entries, IORING_MAX_CQ_ENTRIES);
					number_of_completion_queue_entries
				}
			},

			sq_thread_cpu,

			sq_thread_idle,
			
			wq_fd: if let Some(shared_work_queue) = shared_work_queue
			{
				flags |= SetupFlags::AttachWorkQueue;
				shared_work_queue.as_raw_fd()
			}
			else
			{
				unsafe_zeroed()
			},

			flags,

			features: unsafe_zeroed(),
			resv: unsafe_zeroed(),
			sq_entries: unsafe_uninitialized(),
			sq_off: unsafe_uninitialized(),
			cq_off: unsafe_uninitialized(),
		};

		let result = io_uring_setup(number_of_submission_queue_entries, &mut parameters);
		if likely!(result >= 0)
		{
			let features = parameters.features;

			if unlikely!(!features.contains(ParametersFeatureFlags::AllAsOfLinux57))
			{
				panic!("Essential features coded for not supported (instead {:?} is supported", features)
			}

			let this = Self
			{
				raw_file_descriptor: result,
				using_kernel_submission_queue_poll: flags.contains(SetupFlags::SubmissionQueuePoll),
				using_io_poll: flags.contains(SetupFlags::IoPoll),
			};
			
			Ok((this, parameters))
		}
		else if likely!(result == -1)
		{
			Err(io::Error::last_os_error())
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_setup()", result))
		}
	}

	/// Call this if we either want new events or we need to wake up the kernel submission queue poll thread.
	///
	/// `to_submit` specifies the number of I/Os to submit from the submission queue.
	///
	/// A single call can both submit new I/O and wait for completions of I/O initiated by this call or previous calls to io_uring_enter().
	///
	/// If the io_uring instance was configured for interrupt driven I/O (where IORING_SETUP_IOPOLL was not specified in the call to io_uring_setup(2)), an application may check the completion queue for event completions without entering the kernel at all.
	/// `minimum_wanted_to_complete` is a request, but not order, to wait until at least that many I/O event completions have occurred.
	///
	/// If the io_uring instance was configured for polling, by specifying IORING_SETUP_IOPOLL in the call to io_uring_setup(2), then min_complete has a slightly different meaning.
	/// Passing a value of 0 instructs the kernel to return any events which are already complete, without blocking.
	/// If min_complete is a non-zero value, the kernel will still return immediately if any completion events are available.
	/// If no event completions are available, then the call will poll either until one or more completions become available, or until the process has exceeded its scheduler time slice.
	///
	/// When the system call returns that a certain amount of SQEs have been consumed and submitted, it's safe to reuse SQE entries in the ring.
	///
	/// Returns the number of I/Os successfully consumed.
	#[inline(always)]
	fn enter(&self, minimum_wanted_to_complete: Option<NonZeroU32>, submission_queue_ring: &SubmissionQueueRing, temporary_thread_signal_mask: Option<NonNull<sigset_t>>) -> Result<u32, SubmitError>
	{
		let to_submit = submission_queue_ring.length();

		let (mut flags, minimum_wanted_to_complete) = match minimum_wanted_to_complete
		{
			None => (EnterFlags::empty(), 0),
			Some(minimum_wanted_to_complete) => (EnterFlags::GetEvents, minimum_wanted_to_complete.get()),
		};

		if self.using_kernel_submission_queue_poll
		{
			if submission_queue_ring.needs_to_wake_up_kernel_submission_queue_poll_thread()
			{
				flags |= EnterFlags::SubmissionQueueWakeUp
			}
			// Fast Poll feature.
			else if minimum_wanted_to_complete == 0
			{
				return Ok(to_submit)
			}
		}

		let result = io_uring_enter(self.raw_file_descriptor, to_submit, minimum_wanted_to_complete, flags, unsafe { transmute(temporary_thread_signal_mask) });
		if likely!(result >= 0)
		{
			Ok(result as u32)
		}
		else if likely!(result == -1)
		{
			use self::SubmitError::*;

			match errno().0
			{
				EAGAIN => Err(TryAgain),
				EBUSY => Err(Busy),

				EBADF => panic!("The fd field in the submission queue entry is invalid, or the IOSQE_FIXED_FILE flag was set in the submission queue entry, but no files were registered with the io_uring instance"),

				EFAULT => panic!("IORING_OP_READ_FIXED or IORING_OP_WRITE_FIXED was specified in the opcode field of the submission queue entry, but either buffers were not registered for this io_uring instance, or the address range described by addr and len does not fit within the buffer registered at buf_index. Or, buffer is outside of the process' accessible address space."),

				EINVAL => panic!("The index member of the submission queue entry is invalid. Or, The flags field or opcode in a submission queue entry is invalid. Or, IORING_OP_NOP was specified in the submission queue entry, but the io_uring context was setup for polling (IORING_SETUP_IOPOLL was specified in the call to io_uring_setup). Or, IORING_OP_READV or IORING_OP_WRITEV was specified in the submission queue entry, but the io_uring instance has fixed buffers registered. Or, IORING_OP_READ_FIXED or IORING_OP_WRITE_FIXED was specified in the submission queue entry, and the buf_index is invalid. Or, IORING_OP_READV, IORING_OP_WRITEV, IORING_OP_READ_FIXED, IORING_OP_WRITE_FIXED or IORING_OP_FSYNC was specified in the submission queue entry, but the io_uring instance was configured for IOPOLLing, or any of addr, ioprio, off, len, or buf_index was set in the submission queue entry. Or, IORING_OP_POLL_ADD or IORING_OP_POLL_REMOVE was specified in the opcode field of the submission queue entry, but the io_uring instance was configured for busy-wait polling (IORING_SETUP_IOPOLL), or any of ioprio, off, len, or buf_index was non-zero in the submission queue entry. Or, IORING_OP_POLL_ADD was specified in the opcode field of the submission queue entry, and the addr field was non-zero."),

				ENXIO => panic!("The io_uring instance is in the process of being torn down"),
				EOPNOTSUPP => panic!("fd does not refer to an io_uring instance. Or, opcode is valid, but not supported by this kernel."),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_enter()", unexpected),
			}
		}
		else
		{
			unreachable_code(format_args!("io_uring_enter() returned unexpected result {}", result))
		}
	}
	
	#[inline(always)]
	fn register_buffers(&self, buffers: &[&mut [u8]]) -> io::Result<()>
	{
		let result = self.register_array(RegisterOperation::RegisterBuffers, buffers);

		if likely!(result == 0)
		{
			Ok(())
		}
		else if likely!(result == -1)
		{
			Err(io::Error::last_os_error())
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn unregister_all_buffers(&self) -> io::Result<()>
	{
		let result = self.unregister(RegisterOperation::UnregisterBuffers);

		if likely!(result == 0)
		{
			Ok(())
		}
		else if likely!(result == -1)
		{
			Err(io::Error::last_os_error())
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn register_file_descriptors(&self, file_descriptors: &[SupportedFileDescriptor]) -> io::Result<()>
	{
		let result = self.register_array(RegisterOperation::RegisterFiles, file_descriptors);

		if likely!(result == 0)
		{
			Ok(())
		}
		else if likely!(result == -1)
		{
			Err(io::Error::last_os_error())
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn unregister_all_file_descriptors(&self) -> io::Result<()>
	{
		let result = self.unregister(RegisterOperation::UnregisterFiles);

		if likely!(result == 0)
		{
			Ok(())
		}
		else if likely!(result == -1)
		{
			Err(io::Error::last_os_error())
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	/// `replace_with_files_descriptors.len()` must be less than or equal to `i32::MAX as usize`.
	///
	/// Returns the number of file descriptors replaced.
	#[deprecated]
	#[inline(always)]
	fn replace_some_registered_file_descriptors(&self, replace_with_files_descriptors: &[SupportedFileDescriptor], starting_from_index_inclusive: RegisteredFileDescriptorIndex) -> Result<u32, ()>
	{
		let length = replace_with_files_descriptors.len();
		debug_assert!(length <= i32::MAX as usize);

		let mut argument = io_uring_files_update
		{
			offset: starting_from_index_inclusive.0,
			resv: 0,
			fds: replace_with_files_descriptors.as_ptr() as *const RawFd,
		};

		let result = self.register(RegisterOperation::RegisterFilesUpdate, &mut argument, length as u32);

		if likely!(result >= 0)
		{
			debug_assert!(result <= length as i32);
			Ok(result as u32)
		}
		else if likely!(result == -1)
		{
			match errno().0
			{
				ENOMEM => Err(()),
				ENXIO => panic!("io_uring is being torn down"),
				EINVAL => panic!("bad arguments"),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_register()", unexpected)
			}
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn register_event_file_descriptor_for_all_notifications(&self, event_file_descriptor: &EventFileDescriptor) -> Result<(), bool>
	{
		let result = self.register_array(RegisterOperation::RegisterEventFileDescriptor, &[event_file_descriptor.as_raw_fd()]);

		if likely!(result == 0)
		{
			Ok(())
		}
		else if likely!(result == -1)
		{
			match errno().0
			{
				ENOMEM => Err(false),
				EINTR => Err(true),
				ENXIO => panic!("io_uring is being torn down"),
				EINVAL => panic!("bad arguments"),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_register()", unexpected)
			}
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn register_event_file_descriptor_for_only_asynchronous_notifications(&self, event_file_descriptor: &EventFileDescriptor) -> Result<(), bool>
	{
		let result = self.register_array(RegisterOperation::RegisterEventFileDescriptorAsynchronous, &[event_file_descriptor.as_raw_fd()]);

		if likely!(result == 0)
		{
			Ok(())
		}
		else if likely!(result == -1)
		{
			match errno().0
			{
				ENOMEM => Err(false),
				EINTR => Err(true),
				ENXIO => panic!("io_uring is being torn down"),
				EINVAL => panic!("bad arguments"),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_register()", unexpected)
			}
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn unregister_event_file_descriptor_for_notifications(&self) -> Result<(), bool>
	{
		let result = self.unregister(RegisterOperation::UnregisterEventFileDescriptor);

		if likely!(result == 0)
		{
			Ok(())
		}
		else if likely!(result == -1)
		{
			match errno().0
			{
				ENOMEM => Err(false),
				EINTR => Err(true),
				ENXIO => panic!("io_uring is being torn down"),
				EINVAL => panic!("bad arguments"),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_register()", unexpected)
			}
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn probe_for_supported_operations(&self) -> Result<BTreeSet<IORING_OP>, ()>
	{
		let mut arguments = io_uring_probe::new();
		let result = self.register(RegisterOperation::RegisterProbe, &mut arguments, arguments.ops_len as u32);

		if likely!(result == 0)
		{
			let mut supported_operations = BTreeSet::new();
			for op in arguments.ops()
			{
				if likely!(op.flags.contains(ProbeFlags::OperationSupported) && op.op < IORING_OP::IORING_OP_LAST as u8)
				{
					supported_operations.insert(unsafe { transmute(op.op) });
				}
			}
			Ok(supported_operations)
		}
		else if likely!(result == -1)
		{
			match errno().0
			{
				ENOMEM => Err(()),

				ENXIO => panic!("io_uring is being torn down"),
				EOVERFLOW => panic!("arguments ops array field was too large"),
				EFAULT => panic!("Bad pointers to user space data"),
				EINVAL => panic!("arguments ops array field was empty"),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_register()", unexpected)
			}
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn register_personality(&self) -> Result<PersonalityCredentialsIdentifier, ()>
	{
		let result = self.register::<()>(RegisterOperation::RegisterPersonality, null_mut(), 0);
		if likely!(result > 0 && result <= (u16::MAX as i32))
		{
			Ok(PersonalityCredentialsIdentifier(new_non_zero_u16(result as u16)))
		}
		else if likely!(result == -1)
		{
			match errno().0
			{
				ENOMEM => Err(()),

				ENXIO => panic!("io_uring is being torn down"),
				EINVAL => panic!("Bad arg or nr_arg"),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_register()", unexpected)
			}
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}
	
	#[inline(always)]
	fn unregister_personality(&self, personality_credentials_identifier: PersonalityCredentialsIdentifier)
	{
		let result = self.register::<()>(RegisterOperation::UnregisterPersonality, null_mut(), personality_credentials_identifier.0.get() as u32);
		if likely!(result == 0)
		{
		}
		else if likely!(result == -1)
		{
			match errno().0
			{
				ENXIO => panic!("io_uring is being torn down"),
				EINVAL => panic!("Invalid personality credentials identifier"),

				unexpected @ _ => panic!("Unexpected error {} from io_uring_register()", unexpected)
			}
		}
		else
		{
			unreachable_code(format_args!("Unexpected result {} from io_uring_register()", result))
		}
	}

	#[inline(always)]
	fn unregister(&self, unregister_operation: RegisterOperation) -> i32
	{
		self.register::<()>(unregister_operation, null_mut(), 0)
	}

	#[inline(always)]
	fn register_array<Argument>(&self, register_operation: RegisterOperation, arguments: &[Argument]) -> i32
	{
		let length = arguments.len();
		debug_assert!(length <= u32::MAX as usize);
		self.register(register_operation, arguments.as_ptr() as *mut Argument, length as u32)
	}

	#[inline(always)]
	fn register<Argument>(&self, register_operation: RegisterOperation, arguments: *mut Argument, length: u32) -> i32
	{
		io_uring_register(self.raw_file_descriptor, register_operation, arguments as *mut c_void, length)
	}
}