Skip to content

Commit 2900f16

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 2900f16

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,29 @@ 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 that
96+
// should point to data in the inode or file that lives longer
97+
// than the following use of `T::open`.
8798
let arg = unsafe { A::convert(inode, file) };
99+
// SAFETY: `arg` was previously returned by `A::convert` and must
100+
// be a valid non-null pointer.
88101
let ptr = T::open(unsafe { &*arg })?.into_pointer();
102+
// SAFETY: `ptr` was previously returned by `T::open`. The returned
103+
// value should be a boxed value and should live the length of the
104+
// given file.
89105
unsafe { (*file).private_data = ptr as *mut c_types::c_void };
90106
Ok(0)
91107
}
@@ -500,7 +516,8 @@ pub trait FileOpenAdapter {
500516
/// # Safety
501517
///
502518
/// This function must be called only when [`struct file_operations::open`] is being called for
503-
/// a file that was registered by the implementer.
519+
/// a file that was registered by the implementer. The returned pointer must be valid and
520+
/// not-null.
504521
unsafe fn convert(_inode: *mut bindings::inode, _file: *mut bindings::file)
505522
-> *const Self::Arg;
506523
}

0 commit comments

Comments
 (0)