mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

Toolchain and infrastructure: - Enable a set of Clippy lints: 'ptr_as_ptr', 'ptr_cast_constness', 'as_ptr_cast_mut', 'as_underscore', 'cast_lossless' and 'ref_as_ptr'. These are intended to avoid type casts with the 'as' operator, which are quite powerful, into restricted variants that are less powerful and thus should help to avoid mistakes. - Remove the 'author' key now that most instances were moved to the plural one in the previous cycle. 'kernel' crate: - New 'bug' module: add 'warn_on!' macro which reuses the existing 'BUG'/'WARN' infrastructure, i.e. it respects the usual sysctls and kernel parameters: warn_on!(value == 42); To avoid duplicating the assembly code, the same strategy is followed as for the static branch code in order to share the assembly between both C and Rust. This required a few rearrangements on C arch headers -- the existing C macros should still generate the same outputs, thus no functional change expected there. - 'workqueue' module: add delayed work items, including a 'DelayedWork' struct, a 'impl_has_delayed_work!' macro and an 'enqueue_delayed' method, e.g.: /// Enqueue the struct for execution on the system workqueue, /// where its value will be printed 42 jiffies later. fn print_later(value: Arc<MyStruct>) { let _ = workqueue::system().enqueue_delayed(value, 42); } - New 'bits' module: add support for 'bit' and 'genmask' functions, with runtime- and compile-time variants, e.g.: static_assert!(0b00010000 == bit_u8(4)); static_assert!(0b00011110 == genmask_u8(1..=4)); assert!(checked_bit_u32(u32::BITS).is_none()); - 'uaccess' module: add 'UserSliceReader::strcpy_into_buf', which reads NUL-terminated strings from userspace into a '&CStr'. Introduce 'UserPtr' newtype, similar in purpose to '__user' in C, to minimize mistakes handling userspace pointers, including mixing them up with integers and leaking them via the 'Debug' trait. Add it to the prelude, too. - Start preparations for the replacement of our custom 'CStr' type with the analogous type in the 'core' standard library. This will take place across several cycles to make it easier. For this one, it includes a new 'fmt' module, using upstream method names and some other cleanups. Replace 'fmt!' with a re-export, which helps Clippy lint properly, and clean up the found 'uninlined-format-args' instances. - 'dma' module: - Clarify wording and be consistent in 'coherent' nomenclature. - Convert the 'read!()' and 'write!()' macros to return a 'Result'. - Add 'as_slice()', 'write()' methods in 'CoherentAllocation'. - Expose 'count()' and 'size()' in 'CoherentAllocation' and add the corresponding type invariants. - Implement 'CoherentAllocation::dma_handle_with_offset()'. - 'time' module: - Make 'Instant' generic over clock source. This allows the compiler to assert that arithmetic expressions involving the 'Instant' use 'Instants' based on the same clock source. - Make 'HrTimer' generic over the timer mode. 'HrTimer' timers take a 'Duration' or an 'Instant' when setting the expiry time, depending on the timer mode. With this change, the compiler can check the type matches the timer mode. - Add an abstraction for 'fsleep'. 'fsleep' is a flexible sleep function that will select an appropriate sleep method depending on the requested sleep time. - Avoid 64-bit divisions on 32-bit hardware when calculating timestamps. - Seal the 'HrTimerMode' trait. This prevents users of the 'HrTimerMode' from implementing the trait on their own types. - Pass the correct timer mode ID to 'hrtimer_start_range_ns()'. - 'list' module: remove 'OFFSET' constants, allowing to remove pointer arithmetic; now 'impl_list_item!' invokes 'impl_has_list_links!' or 'impl_has_list_links_self_ptr!'. Other simplifications too. - 'types' module: remove 'ForeignOwnable::PointedTo' in favor of a constant, which avoids exposing the type of the opaque pointer, and require 'into_foreign' to return non-null. Remove the 'Either<L, R>' type as well. It is unused, and we want to encourage the use of custom enums for concrete use cases. - 'sync' module: implement 'Borrow' and 'BorrowMut' for 'Arc' types to allow them to be used in generic APIs. - 'alloc' module: implement 'Borrow' and 'BorrowMut' for 'Box<T, A>'; and 'Borrow', 'BorrowMut' and 'Default' for 'Vec<T, A>'. - 'Opaque' type: add 'cast_from' method to perform a restricted cast that cannot change the inner type and use it in callers of 'container_of!'. Rename 'raw_get' to 'cast_into' to match it. - 'rbtree' module: add 'is_empty' method. - 'sync' module: new 'aref' submodule to hold 'AlwaysRefCounted' and 'ARef', which are moved from the too general 'types' module which we want to reduce or eventually remove. Also fix a safety comment in 'static_lock_class'. 'pin-init' crate: - Add 'impl<T, E> [Pin]Init<T, E> for Result<T, E>', so results are now (pin-)initializers. - Add 'Zeroable::init_zeroed()' that delegates to 'init_zeroed()'. - New 'zeroed()', a safe version of 'mem::zeroed()' and also provide it via 'Zeroable::zeroed()'. - Implement 'Zeroable' for 'Option<&T>', 'Option<&mut T>' and for 'Option<[unsafe] [extern "abi"] fn(...args...) -> ret>' for '"Rust"' and '"C"' ABIs and up to 20 arguments. - Changed blanket impls of 'Init' and 'PinInit' from 'impl<T, E> [Pin]Init<T, E> for T' to 'impl<T> [Pin]Init<T> for T'. - Renamed 'zeroed()' to 'init_zeroed()'. - Upstream dev news: improve CI more to deny warnings, use '--all-targets'. Check the synchronization status of the two '-next' branches in upstream and the kernel. MAINTAINERS: - Add Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki and Lorenzo Stoakes as reviewers (thanks everyone). And a few other cleanups and improvements. -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEPjU5OPd5QIZ9jqqOGXyLc2htIW0FAmiOWREACgkQGXyLc2ht IW39Ig/9E0ExSiBgNKdkCOaULMq31wAxnu3iWoVVisFndlh/Inv+JlaLrmA57BCi xXgBwVZ1GoMsG8Fzt6gT+gyhGYi8waNd+5KXr/WJZVTaJ9v1KpdvxuCnSz0DjCbk GaKfAfxvJ5GAOEwiIIX8X0TFu6kx911DCJY387/VrqZQ7Msh1QSM3tcZeir/EV4w lPjUdlOh1FnLJLI9CGuW20d1IhQUP7K3pdoywgJPpCZV0I8QCyMlMqCEael8Tw2S r/PzRaQtiIzk5HTx06V8paK+nEn0K2vQXqW2kV56Y6TNm1Zcv6dES/8hCITsISs2 nwney3vXEwvoZX+YkQRffZddY4i6YenWMrtLgVxZzdshBL3bn6eHqBL04Nfix+p7 pQe3qMH3G8UBtX1lugBE7RrWGWcz9ARN8sK12ClmpAUnKJOwTpo97kpqXP7pDme8 Buh/oV3voAMsqwooSbVBzuUUWnbGaQ5Oj6CiiosSadfNh6AxJLYLKHtRLKJHZEw3 0Ob/1HhoWS6JSvYKVjMyD19qcH7O8ThZE+83CfMAkI4KphXJarWhpSmN4cHkFn/v 0clQ7Y5m+up9v1XWTaEq0Biqa6CaxLQwm/qW5WU0Y/TiovmvxAFdCwsQqDkRoJNx 9kNfMJRvNl78KQxrjEDz9gl7/ajgqX1KkqP8CQbGjv29cGzFlVE= =5Wt9 -----END PGP SIGNATURE----- Merge tag 'rust-6.17' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux Pull Rust updates from Miguel Ojeda: "Toolchain and infrastructure: - Enable a set of Clippy lints: 'ptr_as_ptr', 'ptr_cast_constness', 'as_ptr_cast_mut', 'as_underscore', 'cast_lossless' and 'ref_as_ptr' These are intended to avoid type casts with the 'as' operator, which are quite powerful, into restricted variants that are less powerful and thus should help to avoid mistakes - Remove the 'author' key now that most instances were moved to the plural one in the previous cycle 'kernel' crate: - New 'bug' module: add 'warn_on!' macro which reuses the existing 'BUG'/'WARN' infrastructure, i.e. it respects the usual sysctls and kernel parameters: warn_on!(value == 42); To avoid duplicating the assembly code, the same strategy is followed as for the static branch code in order to share the assembly between both C and Rust This required a few rearrangements on C arch headers -- the existing C macros should still generate the same outputs, thus no functional change expected there - 'workqueue' module: add delayed work items, including a 'DelayedWork' struct, a 'impl_has_delayed_work!' macro and an 'enqueue_delayed' method, e.g.: /// Enqueue the struct for execution on the system workqueue, /// where its value will be printed 42 jiffies later. fn print_later(value: Arc<MyStruct>) { let _ = workqueue::system().enqueue_delayed(value, 42); } - New 'bits' module: add support for 'bit' and 'genmask' functions, with runtime- and compile-time variants, e.g.: static_assert!(0b00010000 == bit_u8(4)); static_assert!(0b00011110 == genmask_u8(1..=4)); assert!(checked_bit_u32(u32::BITS).is_none()); - 'uaccess' module: add 'UserSliceReader::strcpy_into_buf', which reads NUL-terminated strings from userspace into a '&CStr' Introduce 'UserPtr' newtype, similar in purpose to '__user' in C, to minimize mistakes handling userspace pointers, including mixing them up with integers and leaking them via the 'Debug' trait. Add it to the prelude, too - Start preparations for the replacement of our custom 'CStr' type with the analogous type in the 'core' standard library. This will take place across several cycles to make it easier. For this one, it includes a new 'fmt' module, using upstream method names and some other cleanups Replace 'fmt!' with a re-export, which helps Clippy lint properly, and clean up the found 'uninlined-format-args' instances - 'dma' module: - Clarify wording and be consistent in 'coherent' nomenclature - Convert the 'read!()' and 'write!()' macros to return a 'Result' - Add 'as_slice()', 'write()' methods in 'CoherentAllocation' - Expose 'count()' and 'size()' in 'CoherentAllocation' and add the corresponding type invariants - Implement 'CoherentAllocation::dma_handle_with_offset()' - 'time' module: - Make 'Instant' generic over clock source. This allows the compiler to assert that arithmetic expressions involving the 'Instant' use 'Instants' based on the same clock source - Make 'HrTimer' generic over the timer mode. 'HrTimer' timers take a 'Duration' or an 'Instant' when setting the expiry time, depending on the timer mode. With this change, the compiler can check the type matches the timer mode - Add an abstraction for 'fsleep'. 'fsleep' is a flexible sleep function that will select an appropriate sleep method depending on the requested sleep time - Avoid 64-bit divisions on 32-bit hardware when calculating timestamps - Seal the 'HrTimerMode' trait. This prevents users of the 'HrTimerMode' from implementing the trait on their own types - Pass the correct timer mode ID to 'hrtimer_start_range_ns()' - 'list' module: remove 'OFFSET' constants, allowing to remove pointer arithmetic; now 'impl_list_item!' invokes 'impl_has_list_links!' or 'impl_has_list_links_self_ptr!'. Other simplifications too - 'types' module: remove 'ForeignOwnable::PointedTo' in favor of a constant, which avoids exposing the type of the opaque pointer, and require 'into_foreign' to return non-null Remove the 'Either<L, R>' type as well. It is unused, and we want to encourage the use of custom enums for concrete use cases - 'sync' module: implement 'Borrow' and 'BorrowMut' for 'Arc' types to allow them to be used in generic APIs - 'alloc' module: implement 'Borrow' and 'BorrowMut' for 'Box<T, A>'; and 'Borrow', 'BorrowMut' and 'Default' for 'Vec<T, A>' - 'Opaque' type: add 'cast_from' method to perform a restricted cast that cannot change the inner type and use it in callers of 'container_of!'. Rename 'raw_get' to 'cast_into' to match it - 'rbtree' module: add 'is_empty' method - 'sync' module: new 'aref' submodule to hold 'AlwaysRefCounted' and 'ARef', which are moved from the too general 'types' module which we want to reduce or eventually remove. Also fix a safety comment in 'static_lock_class' 'pin-init' crate: - Add 'impl<T, E> [Pin]Init<T, E> for Result<T, E>', so results are now (pin-)initializers - Add 'Zeroable::init_zeroed()' that delegates to 'init_zeroed()' - New 'zeroed()', a safe version of 'mem::zeroed()' and also provide it via 'Zeroable::zeroed()' - Implement 'Zeroable' for 'Option<&T>', 'Option<&mut T>' and for 'Option<[unsafe] [extern "abi"] fn(...args...) -> ret>' for '"Rust"' and '"C"' ABIs and up to 20 arguments - Changed blanket impls of 'Init' and 'PinInit' from 'impl<T, E> [Pin]Init<T, E> for T' to 'impl<T> [Pin]Init<T> for T' - Renamed 'zeroed()' to 'init_zeroed()' - Upstream dev news: improve CI more to deny warnings, use '--all-targets'. Check the synchronization status of the two '-next' branches in upstream and the kernel MAINTAINERS: - Add Vlastimil Babka, Liam R. Howlett, Uladzislau Rezki and Lorenzo Stoakes as reviewers (thanks everyone) And a few other cleanups and improvements" * tag 'rust-6.17' of git://git.kernel.org/pub/scm/linux/kernel/git/ojeda/linux: (76 commits) rust: Add warn_on macro arm64/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust riscv/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust x86/bug: Add ARCH_WARN_ASM macro for BUG/WARN asm code sharing with Rust rust: kernel: move ARef and AlwaysRefCounted to sync::aref rust: sync: fix safety comment for `static_lock_class` rust: types: remove `Either<L, R>` rust: kernel: use `core::ffi::CStr` method names rust: str: add `CStr` methods matching `core::ffi::CStr` rust: str: remove unnecessary qualification rust: use `kernel::{fmt,prelude::fmt!}` rust: kernel: add `fmt` module rust: kernel: remove `fmt!`, fix clippy::uninlined-format-args scripts: rust: emit path candidates in panic message scripts: rust: replace length checks with match rust: list: remove nonexistent generic parameter in link rust: bits: add support for bits/genmask macros rust: list: remove OFFSET constants rust: list: add `impl_list_item!` examples rust: list: use fully qualified path ...
369 lines
14 KiB
Rust
369 lines
14 KiB
Rust
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
// Copyright (C) 2024 Google LLC.
|
|
|
|
//! Miscdevice support.
|
|
//!
|
|
//! C headers: [`include/linux/miscdevice.h`](srctree/include/linux/miscdevice.h).
|
|
//!
|
|
//! Reference: <https://www.kernel.org/doc/html/latest/driver-api/misc_devices.html>
|
|
|
|
use crate::{
|
|
bindings,
|
|
device::Device,
|
|
error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR},
|
|
ffi::{c_int, c_long, c_uint, c_ulong},
|
|
fs::File,
|
|
mm::virt::VmaNew,
|
|
prelude::*,
|
|
seq_file::SeqFile,
|
|
types::{ForeignOwnable, Opaque},
|
|
};
|
|
use core::{marker::PhantomData, mem::MaybeUninit, pin::Pin};
|
|
|
|
/// Options for creating a misc device.
|
|
#[derive(Copy, Clone)]
|
|
pub struct MiscDeviceOptions {
|
|
/// The name of the miscdevice.
|
|
pub name: &'static CStr,
|
|
}
|
|
|
|
impl MiscDeviceOptions {
|
|
/// Create a raw `struct miscdev` ready for registration.
|
|
pub const fn into_raw<T: MiscDevice>(self) -> bindings::miscdevice {
|
|
// SAFETY: All zeros is valid for this C type.
|
|
let mut result: bindings::miscdevice = unsafe { MaybeUninit::zeroed().assume_init() };
|
|
result.minor = bindings::MISC_DYNAMIC_MINOR as ffi::c_int;
|
|
result.name = self.name.as_char_ptr();
|
|
result.fops = MiscdeviceVTable::<T>::build();
|
|
result
|
|
}
|
|
}
|
|
|
|
/// A registration of a miscdevice.
|
|
///
|
|
/// # Invariants
|
|
///
|
|
/// - `inner` contains a `struct miscdevice` that is registered using
|
|
/// `misc_register()`.
|
|
/// - This registration remains valid for the entire lifetime of the
|
|
/// [`MiscDeviceRegistration`] instance.
|
|
/// - Deregistration occurs exactly once in [`Drop`] via `misc_deregister()`.
|
|
/// - `inner` wraps a valid, pinned `miscdevice` created using
|
|
/// [`MiscDeviceOptions::into_raw`].
|
|
#[repr(transparent)]
|
|
#[pin_data(PinnedDrop)]
|
|
pub struct MiscDeviceRegistration<T> {
|
|
#[pin]
|
|
inner: Opaque<bindings::miscdevice>,
|
|
_t: PhantomData<T>,
|
|
}
|
|
|
|
// SAFETY: It is allowed to call `misc_deregister` on a different thread from where you called
|
|
// `misc_register`.
|
|
unsafe impl<T> Send for MiscDeviceRegistration<T> {}
|
|
// SAFETY: All `&self` methods on this type are written to ensure that it is safe to call them in
|
|
// parallel.
|
|
unsafe impl<T> Sync for MiscDeviceRegistration<T> {}
|
|
|
|
impl<T: MiscDevice> MiscDeviceRegistration<T> {
|
|
/// Register a misc device.
|
|
pub fn register(opts: MiscDeviceOptions) -> impl PinInit<Self, Error> {
|
|
try_pin_init!(Self {
|
|
inner <- Opaque::try_ffi_init(move |slot: *mut bindings::miscdevice| {
|
|
// SAFETY: The initializer can write to the provided `slot`.
|
|
unsafe { slot.write(opts.into_raw::<T>()) };
|
|
|
|
// SAFETY: We just wrote the misc device options to the slot. The miscdevice will
|
|
// get unregistered before `slot` is deallocated because the memory is pinned and
|
|
// the destructor of this type deallocates the memory.
|
|
// INVARIANT: If this returns `Ok(())`, then the `slot` will contain a registered
|
|
// misc device.
|
|
to_result(unsafe { bindings::misc_register(slot) })
|
|
}),
|
|
_t: PhantomData,
|
|
})
|
|
}
|
|
|
|
/// Returns a raw pointer to the misc device.
|
|
pub fn as_raw(&self) -> *mut bindings::miscdevice {
|
|
self.inner.get()
|
|
}
|
|
|
|
/// Access the `this_device` field.
|
|
pub fn device(&self) -> &Device {
|
|
// SAFETY: This can only be called after a successful register(), which always
|
|
// initialises `this_device` with a valid device. Furthermore, the signature of this
|
|
// function tells the borrow-checker that the `&Device` reference must not outlive the
|
|
// `&MiscDeviceRegistration<T>` used to obtain it, so the last use of the reference must be
|
|
// before the underlying `struct miscdevice` is destroyed.
|
|
unsafe { Device::from_raw((*self.as_raw()).this_device) }
|
|
}
|
|
}
|
|
|
|
#[pinned_drop]
|
|
impl<T> PinnedDrop for MiscDeviceRegistration<T> {
|
|
fn drop(self: Pin<&mut Self>) {
|
|
// SAFETY: We know that the device is registered by the type invariants.
|
|
unsafe { bindings::misc_deregister(self.inner.get()) };
|
|
}
|
|
}
|
|
|
|
/// Trait implemented by the private data of an open misc device.
|
|
#[vtable]
|
|
pub trait MiscDevice: Sized {
|
|
/// What kind of pointer should `Self` be wrapped in.
|
|
type Ptr: ForeignOwnable + Send + Sync;
|
|
|
|
/// Called when the misc device is opened.
|
|
///
|
|
/// The returned pointer will be stored as the private data for the file.
|
|
fn open(_file: &File, _misc: &MiscDeviceRegistration<Self>) -> Result<Self::Ptr>;
|
|
|
|
/// Called when the misc device is released.
|
|
fn release(device: Self::Ptr, _file: &File) {
|
|
drop(device);
|
|
}
|
|
|
|
/// Handle for mmap.
|
|
///
|
|
/// This function is invoked when a user space process invokes the `mmap` system call on
|
|
/// `file`. The function is a callback that is part of the VMA initializer. The kernel will do
|
|
/// initial setup of the VMA before calling this function. The function can then interact with
|
|
/// the VMA initialization by calling methods of `vma`. If the function does not return an
|
|
/// error, the kernel will complete initialization of the VMA according to the properties of
|
|
/// `vma`.
|
|
fn mmap(
|
|
_device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
|
|
_file: &File,
|
|
_vma: &VmaNew,
|
|
) -> Result {
|
|
build_error!(VTABLE_DEFAULT_ERROR)
|
|
}
|
|
|
|
/// Handler for ioctls.
|
|
///
|
|
/// The `cmd` argument is usually manipulated using the utilities in [`kernel::ioctl`].
|
|
///
|
|
/// [`kernel::ioctl`]: mod@crate::ioctl
|
|
fn ioctl(
|
|
_device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
|
|
_file: &File,
|
|
_cmd: u32,
|
|
_arg: usize,
|
|
) -> Result<isize> {
|
|
build_error!(VTABLE_DEFAULT_ERROR)
|
|
}
|
|
|
|
/// Handler for ioctls.
|
|
///
|
|
/// Used for 32-bit userspace on 64-bit platforms.
|
|
///
|
|
/// This method is optional and only needs to be provided if the ioctl relies on structures
|
|
/// that have different layout on 32-bit and 64-bit userspace. If no implementation is
|
|
/// provided, then `compat_ptr_ioctl` will be used instead.
|
|
#[cfg(CONFIG_COMPAT)]
|
|
fn compat_ioctl(
|
|
_device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
|
|
_file: &File,
|
|
_cmd: u32,
|
|
_arg: usize,
|
|
) -> Result<isize> {
|
|
build_error!(VTABLE_DEFAULT_ERROR)
|
|
}
|
|
|
|
/// Show info for this fd.
|
|
fn show_fdinfo(
|
|
_device: <Self::Ptr as ForeignOwnable>::Borrowed<'_>,
|
|
_m: &SeqFile,
|
|
_file: &File,
|
|
) {
|
|
build_error!(VTABLE_DEFAULT_ERROR)
|
|
}
|
|
}
|
|
|
|
/// A vtable for the file operations of a Rust miscdevice.
|
|
struct MiscdeviceVTable<T: MiscDevice>(PhantomData<T>);
|
|
|
|
impl<T: MiscDevice> MiscdeviceVTable<T> {
|
|
/// # Safety
|
|
///
|
|
/// `file` and `inode` must be the file and inode for a file that is undergoing initialization.
|
|
/// The file must be associated with a `MiscDeviceRegistration<T>`.
|
|
unsafe extern "C" fn open(inode: *mut bindings::inode, raw_file: *mut bindings::file) -> c_int {
|
|
// SAFETY: The pointers are valid and for a file being opened.
|
|
let ret = unsafe { bindings::generic_file_open(inode, raw_file) };
|
|
if ret != 0 {
|
|
return ret;
|
|
}
|
|
|
|
// SAFETY: The open call of a file can access the private data.
|
|
let misc_ptr = unsafe { (*raw_file).private_data };
|
|
|
|
// SAFETY: This is a miscdevice, so `misc_open()` set the private data to a pointer to the
|
|
// associated `struct miscdevice` before calling into this method. Furthermore,
|
|
// `misc_open()` ensures that the miscdevice can't be unregistered and freed during this
|
|
// call to `fops_open`.
|
|
let misc = unsafe { &*misc_ptr.cast::<MiscDeviceRegistration<T>>() };
|
|
|
|
// SAFETY:
|
|
// * This underlying file is valid for (much longer than) the duration of `T::open`.
|
|
// * There is no active fdget_pos region on the file on this thread.
|
|
let file = unsafe { File::from_raw_file(raw_file) };
|
|
|
|
let ptr = match T::open(file, misc) {
|
|
Ok(ptr) => ptr,
|
|
Err(err) => return err.to_errno(),
|
|
};
|
|
|
|
// This overwrites the private data with the value specified by the user, changing the type
|
|
// of this file's private data. All future accesses to the private data is performed by
|
|
// other fops_* methods in this file, which all correctly cast the private data to the new
|
|
// type.
|
|
//
|
|
// SAFETY: The open call of a file can access the private data.
|
|
unsafe { (*raw_file).private_data = ptr.into_foreign() };
|
|
|
|
0
|
|
}
|
|
|
|
/// # Safety
|
|
///
|
|
/// `file` and `inode` must be the file and inode for a file that is being released. The file
|
|
/// must be associated with a `MiscDeviceRegistration<T>`.
|
|
unsafe extern "C" fn release(_inode: *mut bindings::inode, file: *mut bindings::file) -> c_int {
|
|
// SAFETY: The release call of a file owns the private data.
|
|
let private = unsafe { (*file).private_data };
|
|
// SAFETY: The release call of a file owns the private data.
|
|
let ptr = unsafe { <T::Ptr as ForeignOwnable>::from_foreign(private) };
|
|
|
|
// SAFETY:
|
|
// * The file is valid for the duration of this call.
|
|
// * There is no active fdget_pos region on the file on this thread.
|
|
T::release(ptr, unsafe { File::from_raw_file(file) });
|
|
|
|
0
|
|
}
|
|
|
|
/// # Safety
|
|
///
|
|
/// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.
|
|
/// `vma` must be a vma that is currently being mmap'ed with this file.
|
|
unsafe extern "C" fn mmap(
|
|
file: *mut bindings::file,
|
|
vma: *mut bindings::vm_area_struct,
|
|
) -> c_int {
|
|
// SAFETY: The mmap call of a file can access the private data.
|
|
let private = unsafe { (*file).private_data };
|
|
// SAFETY: This is a Rust Miscdevice, so we call `into_foreign` in `open` and
|
|
// `from_foreign` in `release`, and `fops_mmap` is guaranteed to be called between those
|
|
// two operations.
|
|
let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private.cast()) };
|
|
// SAFETY: The caller provides a vma that is undergoing initial VMA setup.
|
|
let area = unsafe { VmaNew::from_raw(vma) };
|
|
// SAFETY:
|
|
// * The file is valid for the duration of this call.
|
|
// * There is no active fdget_pos region on the file on this thread.
|
|
let file = unsafe { File::from_raw_file(file) };
|
|
|
|
match T::mmap(device, file, area) {
|
|
Ok(()) => 0,
|
|
Err(err) => err.to_errno(),
|
|
}
|
|
}
|
|
|
|
/// # Safety
|
|
///
|
|
/// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.
|
|
unsafe extern "C" fn ioctl(file: *mut bindings::file, cmd: c_uint, arg: c_ulong) -> c_long {
|
|
// SAFETY: The ioctl call of a file can access the private data.
|
|
let private = unsafe { (*file).private_data };
|
|
// SAFETY: Ioctl calls can borrow the private data of the file.
|
|
let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private) };
|
|
|
|
// SAFETY:
|
|
// * The file is valid for the duration of this call.
|
|
// * There is no active fdget_pos region on the file on this thread.
|
|
let file = unsafe { File::from_raw_file(file) };
|
|
|
|
match T::ioctl(device, file, cmd, arg) {
|
|
Ok(ret) => ret as c_long,
|
|
Err(err) => err.to_errno() as c_long,
|
|
}
|
|
}
|
|
|
|
/// # Safety
|
|
///
|
|
/// `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.
|
|
#[cfg(CONFIG_COMPAT)]
|
|
unsafe extern "C" fn compat_ioctl(
|
|
file: *mut bindings::file,
|
|
cmd: c_uint,
|
|
arg: c_ulong,
|
|
) -> c_long {
|
|
// SAFETY: The compat ioctl call of a file can access the private data.
|
|
let private = unsafe { (*file).private_data };
|
|
// SAFETY: Ioctl calls can borrow the private data of the file.
|
|
let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private) };
|
|
|
|
// SAFETY:
|
|
// * The file is valid for the duration of this call.
|
|
// * There is no active fdget_pos region on the file on this thread.
|
|
let file = unsafe { File::from_raw_file(file) };
|
|
|
|
match T::compat_ioctl(device, file, cmd, arg) {
|
|
Ok(ret) => ret as c_long,
|
|
Err(err) => err.to_errno() as c_long,
|
|
}
|
|
}
|
|
|
|
/// # Safety
|
|
///
|
|
/// - `file` must be a valid file that is associated with a `MiscDeviceRegistration<T>`.
|
|
/// - `seq_file` must be a valid `struct seq_file` that we can write to.
|
|
unsafe extern "C" fn show_fdinfo(seq_file: *mut bindings::seq_file, file: *mut bindings::file) {
|
|
// SAFETY: The release call of a file owns the private data.
|
|
let private = unsafe { (*file).private_data };
|
|
// SAFETY: Ioctl calls can borrow the private data of the file.
|
|
let device = unsafe { <T::Ptr as ForeignOwnable>::borrow(private) };
|
|
// SAFETY:
|
|
// * The file is valid for the duration of this call.
|
|
// * There is no active fdget_pos region on the file on this thread.
|
|
let file = unsafe { File::from_raw_file(file) };
|
|
// SAFETY: The caller ensures that the pointer is valid and exclusive for the duration in
|
|
// which this method is called.
|
|
let m = unsafe { SeqFile::from_raw(seq_file) };
|
|
|
|
T::show_fdinfo(device, m, file);
|
|
}
|
|
|
|
const VTABLE: bindings::file_operations = bindings::file_operations {
|
|
open: Some(Self::open),
|
|
release: Some(Self::release),
|
|
mmap: if T::HAS_MMAP { Some(Self::mmap) } else { None },
|
|
unlocked_ioctl: if T::HAS_IOCTL {
|
|
Some(Self::ioctl)
|
|
} else {
|
|
None
|
|
},
|
|
#[cfg(CONFIG_COMPAT)]
|
|
compat_ioctl: if T::HAS_COMPAT_IOCTL {
|
|
Some(Self::compat_ioctl)
|
|
} else if T::HAS_IOCTL {
|
|
Some(bindings::compat_ptr_ioctl)
|
|
} else {
|
|
None
|
|
},
|
|
show_fdinfo: if T::HAS_SHOW_FDINFO {
|
|
Some(Self::show_fdinfo)
|
|
} else {
|
|
None
|
|
},
|
|
// SAFETY: All zeros is a valid value for `bindings::file_operations`.
|
|
..unsafe { MaybeUninit::zeroed().assume_init() }
|
|
};
|
|
|
|
const fn build() -> &'static bindings::file_operations {
|
|
&Self::VTABLE
|
|
}
|
|
}
|