Commit Graph

3 Commits

Author SHA1 Message Date
Alice Ryhl
bd5ee6bcc5 rust: miscdevice: add missing safety comments
This fixes the following four warnings:

	warning: unsafe block missing a safety comment
	   --> /home/aliceryhl/rust-for-linux/rust/kernel/miscdevice.rs:168:15
	    |
	168 |             ..unsafe { MaybeUninit::zeroed().assume_init() }
	    |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	    |
	    = help: consider adding a safety comment on the preceding line
	    = help: for further information visit
	      https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks
	    = note: requested on the command line with `-W clippy::undocumented-unsafe-blocks`

	warning: unsafe function's docs are missing a `# Safety` section
	   --> /home/aliceryhl/rust-for-linux/rust/kernel/miscdevice.rs:175:1
	    |
	175 | / unsafe extern "C" fn fops_open<T: MiscDevice>(
	176 | |     inode: *mut bindings::inode,
	177 | |     file: *mut bindings::file,
	178 | | ) -> c_int {
	    | |__________^
	    |
	    = help: for further information visit
	      https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc
	    = note: `-W clippy::missing-safety-doc` implied by `-W clippy::all`
	    = help: to override `-W clippy::all` add `#[allow(clippy::missing_safety_doc)]`

	warning: unsafe function's docs are missing a `# Safety` section
	   --> /home/aliceryhl/rust-for-linux/rust/kernel/miscdevice.rs:196:1
	    |
	196 | / unsafe extern "C" fn fops_release<T: MiscDevice>(
	197 | |     _inode: *mut bindings::inode,
	198 | |     file: *mut bindings::file,
	199 | | ) -> c_int {
	    | |__________^
	    |
	    = help: for further information visit
	      https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc

	warning: unsafe function's docs are missing a `# Safety` section
	   --> /home/aliceryhl/rust-for-linux/rust/kernel/miscdevice.rs:210:1
	    |
	210 | / unsafe extern "C" fn fops_ioctl<T: MiscDevice>(
	211 | |     file: *mut bindings::file,
	212 | |     cmd: c_uint,
	213 | |     arg: c_ulong,
	214 | | ) -> c_long {
	    | |___________^
	    |
	    = help: for further information visit
	      https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc

Note that these warnings are currently not enabled in the build, but
rust-next contains a commit that will enable them, so we should fix
them.

Reported-by: Miguel Ojeda <ojeda@kernel.org>
Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Closes: https://lore.kernel.org/rust-for-linux/CANiq72kOs6vPDUzZttQNqePFHphCQ30iVmZ5MO7eCJfPG==Vzg@mail.gmail.com/
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://lore.kernel.org/r/20241022-miscdevice-unsafe-warn-fix-v1-1-a78fde1740d6@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-10-22 15:53:15 +02:00
Alice Ryhl
ccb22ca280 rust: miscdevice: fix warning on c_uint to u32 cast
When building miscdevice with clippy warnings, the following warning is
emitted:

	warning: casting to the same type is unnecessary (`u32` -> `u32`)
	   --> /home/aliceryhl/rust-for-linux/rust/kernel/miscdevice.rs:220:28
	    |
	220 |     match T::ioctl(device, cmd as u32, arg as usize) {
	    |                            ^^^^^^^^^^ help: try: `cmd`
	    |
	    = help: for further information visit
	      https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
	    = note: `-W clippy::unnecessary-cast` implied by `-W clippy::all`
	    = help: to override `-W clippy::all` add `#[allow(clippy::unnecessary_cast)]`

Thus, fix it.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/r/20241015-miscdevice-cint-cast-v1-1-fcf4b75700ac@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-10-15 19:11:39 +02:00
Alice Ryhl
f893691e74 rust: miscdevice: add base miscdevice abstraction
Provide a `MiscDevice` trait that lets you specify the file operations
that you wish to provide for your misc device. For now, only three file
operations are provided: open, close, ioctl.

These abstractions only support MISC_DYNAMIC_MINOR. This enforces that
new miscdevices should not hard-code a minor number.

When implementing ioctl, the Result type is used. This means that you
can choose to return either of:
* An integer of type isize.
* An errno using the kernel::error::Error type.
When returning an isize, the integer is returned verbatim. It's mainly
intended for returning positive integers to userspace. However, it is
technically possible to return errors via the isize return value too.

To avoid having a dependency on files, this patch does not provide the
file operations callbacks a pointer to the file. This means that they
cannot check file properties such as O_NONBLOCK (which Binder needs).
Support for that can be added as a follow-up.

To avoid having a dependency on vma, this patch does not provide any way
to implement mmap (which Binder needs). Support for that can be added as
a follow-up.

Rust Binder will use these abstractions to create the /dev/binder file
when binderfs is disabled.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Link: https://lore.kernel.org/rust-for-linux/20240328195457.225001-1-wedsonaf@gmail.com/
Link: https://lore.kernel.org/r/20241001-b4-miscdevice-v2-2-330d760041fa@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2024-10-09 12:18:30 +02:00