Skip to content

Commit 55530e7

Browse files
author
Dan Robertson
committed
rust: kernel: add missing safety comments
- Add safety comment to usage of get_unused_fd_flags - Add documentation and safety comments to internal open_callback helper function - Add additional safety documentation to FileOpenAdapter::convert Signed-off-by: Dan Robertson <[email protected]>
1 parent cbd2a83 commit 55530e7

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

rust/kernel/file.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ pub struct FileDescriptorReservation {
9595
impl FileDescriptorReservation {
9696
/// Creates a new file descriptor reservation.
9797
pub fn new(flags: u32) -> Result<Self> {
98+
// SAFETY: FFI call, there are no safety requirements on `flags`.
9899
let fd = unsafe { bindings::get_unused_fd_flags(flags) };
99100
if fd < 0 {
100101
return Err(Error::from_kernel_errno(fd));

rust/kernel/file_operations.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,26 @@ pub enum SeekFrom {
7979
Current(i64),
8080
}
8181

82+
/// Called by the VFS when an inode should be opened.
83+
///
84+
/// Calls `T::open` on the returned value of `A::convert`.
85+
///
86+
/// # Safety
87+
///
88+
/// The returned value of `A::convert` must be a valid non-null pointer and
89+
/// `T:open` must return a valid non-null pointer on an `Ok` result.
8290
unsafe extern "C" fn open_callback<A: FileOpenAdapter, T: FileOpener<A::Arg>>(
8391
inode: *mut bindings::inode,
8492
file: *mut bindings::file,
8593
) -> c_types::c_int {
8694
from_kernel_result! {
95+
// SAFETY: `A::convert` must return a valid non-null pointer
8796
let arg = unsafe { A::convert(inode, file) };
97+
// SAFETY: `arg` was returned by `A::convert` and must be a valid
98+
// non-null pointer
8899
let ptr = T::open(unsafe { &*arg })?.into_pointer();
100+
// SAFETY: `file` was returned by `T::open` and must be a valid
101+
// non-null pointer.
89102
unsafe { (*file).private_data = ptr as *mut c_types::c_void };
90103
Ok(0)
91104
}
@@ -500,7 +513,8 @@ pub trait FileOpenAdapter {
500513
/// # Safety
501514
///
502515
/// This function must be called only when [`struct file_operations::open`] is being called for
503-
/// a file that was registered by the implementer.
516+
/// a file that was registered by the implementer. The returned pointer must be valid and
517+
/// not-null.
504518
unsafe fn convert(_inode: *mut bindings::inode, _file: *mut bindings::file)
505519
-> *const Self::Arg;
506520
}

0 commit comments

Comments
 (0)