Skip to content

Commit 6a0100f

Browse files
authored
Merge pull request #619 from wedsonaf/file-operations-sync
rust: require `Sync` and `Send` on file operations context data
2 parents d2a7383 + 199c49e commit 6a0100f

5 files changed

Lines changed: 14 additions & 16 deletions

File tree

drivers/char/hw_random/bcm2835_rng_rust.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ struct RngDevice;
2323
impl FileOperations for RngDevice {
2424
kernel::declare_file_operations!(read);
2525

26-
fn open(_open_data: &(), _file: &File) -> Result<Self::Wrapper> {
27-
Ok(Box::try_new(RngDevice)?)
26+
fn open(_open_data: &(), _file: &File) -> Result {
27+
Ok(())
2828
}
2929

30-
fn read(_: &Self, _: &File, data: &mut impl IoBufferWriter, offset: u64) -> Result<usize> {
30+
fn read(_: (), _: &File, data: &mut impl IoBufferWriter, offset: u64) -> Result<usize> {
3131
// Succeed if the caller doesn't provide a buffer or if not at the start.
3232
if data.is_empty() || offset != 0 {
3333
return Ok(0);

rust/kernel/file_operations.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
//!
55
//! C header: [`include/linux/fs.h`](../../../../include/linux/fs.h)
66
7-
use core::convert::{TryFrom, TryInto};
8-
use core::{marker, mem, ptr};
9-
10-
use alloc::boxed::Box;
11-
127
use crate::{
138
bindings, c_types,
149
error::{from_kernel_result, Error, Result},
@@ -19,6 +14,8 @@ use crate::{
1914
types::PointerWrapper,
2015
user_ptr::{UserSlicePtr, UserSlicePtrReader, UserSlicePtrWriter},
2116
};
17+
use core::convert::{TryFrom, TryInto};
18+
use core::{marker, mem, ptr};
2219

2320
/// Wraps the kernel's `struct poll_table_struct`.
2421
///
@@ -610,12 +607,12 @@ pub trait FileOpenAdapter<T: Sync> {
610607
/// File descriptors may be used from multiple threads/processes concurrently, so your type must be
611608
/// [`Sync`]. It must also be [`Send`] because [`FileOperations::release`] will be called from the
612609
/// thread that decrements that associated file's refcount to zero.
613-
pub trait FileOperations: Send + Sync + Sized + 'static {
610+
pub trait FileOperations {
614611
/// The methods to use to populate [`struct file_operations`].
615612
const TO_USE: ToUse;
616613

617614
/// The pointer type that will be used to hold ourselves.
618-
type Wrapper: PointerWrapper = Box<Self>;
615+
type Wrapper: PointerWrapper + Send + Sync = ();
619616

620617
/// The type of the context data passed to [`FileOperations::open`].
621618
type OpenData: Sync = ();

samples/rust/rust_chrdev.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ struct RustFile;
2121
impl FileOperations for RustFile {
2222
kernel::declare_file_operations!();
2323

24-
fn open(_shared: &(), _file: &file::File) -> Result<Self::Wrapper> {
25-
Ok(Box::try_new(RustFile)?)
24+
fn open(_shared: &(), _file: &file::File) -> Result {
25+
Ok(())
2626
}
2727
}
2828

samples/rust/rust_random.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ struct RandomFile;
2020
impl FileOperations for RandomFile {
2121
kernel::declare_file_operations!(read, write, read_iter, write_iter);
2222

23-
fn open(_open_data: &(), _file: &File) -> Result<Self::Wrapper> {
24-
Ok(Box::try_new(RandomFile)?)
23+
fn open(_data: &(), _file: &File) -> Result {
24+
Ok(())
2525
}
2626

27-
fn read(_this: &Self, file: &File, buf: &mut impl IoBufferWriter, _: u64) -> Result<usize> {
27+
fn read(_this: (), file: &File, buf: &mut impl IoBufferWriter, _: u64) -> Result<usize> {
2828
let total_len = buf.len();
2929
let mut chunkbuf = [0; 256];
3030

@@ -42,7 +42,7 @@ impl FileOperations for RandomFile {
4242
Ok(total_len)
4343
}
4444

45-
fn write(_this: &Self, _file: &File, buf: &mut impl IoBufferReader, _: u64) -> Result<usize> {
45+
fn write(_this: (), _file: &File, buf: &mut impl IoBufferReader, _: u64) -> Result<usize> {
4646
let total_len = buf.len();
4747
let mut chunkbuf = [0; 256];
4848
while !buf.is_empty() {

samples/rust/rust_semaphore.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ impl FileState {
6666
}
6767

6868
impl FileOperations for FileState {
69+
type Wrapper = Box<Self>;
6970
type OpenData = Ref<Semaphore>;
7071

7172
declare_file_operations!(read, write, ioctl);

0 commit comments

Comments
 (0)