samples: rust: Provide example using the new Rust MiscDevice abstraction
Subsystems (2)
PATCH 1/2 samples: rust: Provide example using the new Rust MiscDevice abstraction
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create /dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simple ioctl()
* Close the character device
--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rs
samples/rust/Kconfig +10 -0
@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" help
samples/rust/Makefile +1 -0
@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print-y := rust_print_main.o rust_print_events.o
samples/rust/rust_misc_device.rs new file +84 -0
84 lines not on this page · raw message
Signature · 1 line
2.47.0.338.g60cca15819-goog
PATCH 2/2 sample: rust_misc_device: Demonstrate additional get/set value functionality
Expand the complexity of the sample driver by providing the ability to get and set an integer. The value is protected by a mutex.
Here is a simple userspace program that fully exercises the sample driver's capabilities.
int main() {
int value, new_value;
int fd, ret;
// Open the device file
printf("Opening /dev/rust-misc-device for reading and writing\n");
fd = open("/dev/rust-misc-device", O_RDWR);
if (fd < 0) {
perror("open");
return errno;
}
// Make call into driver to say "hello"
printf("Calling Hello\n");
ret = ioctl(fd, RUST_MISC_DEV_HELLO, NULL);
if (ret < 0) {
perror("ioctl: Failed to call into Hello");
close(fd);
return errno;
}
// Get initial value
printf("Fetching initial value\n");
ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &value);
if (ret < 0) {
perror("ioctl: Failed to fetch the initial value");
close(fd);
return errno;
}value++;
// Set value to something different
printf("Submitting new value (%d)\n", value);
ret = ioctl(fd, RUST_MISC_DEV_SET_VALUE, &value);
if (ret < 0) {
perror("ioctl: Failed to submit new value");
close(fd);
return errno;
}
// Ensure new value was applied
printf("Fetching new value\n");
ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value);
if (ret < 0) {
perror("ioctl: Failed to fetch the new value");
close(fd);
return errno;
}
if (value != new_value) {
printf("Failed: Committed and retrieved values are different (%d - %d)\n", value, new_value);
close(fd);
return -1;
}
// Call the unsuccessful ioctl
printf("Attempting to call in to an non-existent IOCTL\n");
ret = ioctl(fd, RUST_MISC_DEV_FAIL, NULL);
if (ret < 0) {
perror("ioctl: Succeeded to fail - this was expected");
} else {
printf("ioctl: Failed to fail\n");
close(fd);
return -1;
}
// Close the device file
printf("Closing /dev/rust-misc-device\n");
close(fd); printf("Success\n");
return 0;
}
--- samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 20 deletions(-)
samples/rust/rust_misc_device.rs +62 -20
@@ -2,13 +2,20 @@//! Rust misc device sample. +use core::pin::Pin ; +use kernel::{ c_str, - ioctl::_IO, + ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + new_mutex, prelude::*, + sync::Mutex , + uaccess::{UserSlice ,UserSliceReader ,UserSliceWriter }, }; +const RUST_MISC_DEV_GET_VALUE :u32 =_IOR ::<i32 >('R' as u32 ,7 ); +const RUST_MISC_DEV_SET_VALUE :u32 =_IOW ::<i32 >('R' as u32 ,8 );const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 );module! {
@@ -40,45 +47,80 @@ fn init(_module: &'static ThisModule) -> Result<Self> { } } -struct RustMiscDevice ; +struct Inner { + value:i32 , +} -impl RustMiscDevice { -fn new () ->Self { -Self - } +#[pin_data( PinnedDrop )] +struct RustMiscDevice { +#[pin] + inner:Mutex <Inner >, }#[vtable] impl MiscDevice for RustMiscDevice { -type Ptr =KBox <Self >; +type Ptr =Pin <KBox <Self >>; -fn open () ->Result <KBox <Self >> { +fn open () ->Result <Pin <KBox <Self >>> {pr_info! ("Opening Rust Misc Device Sample\n" ); -Ok (KBox ::new (RustMiscDevice ::new (),GFP_KERNEL )?) +KBox ::try_pin_init ( +try_pin_init! { +RustMiscDevice { inner <-new_mutex! (Inner { value:0_i32 } )} + }, +GFP_KERNEL , + ) } -fn ioctl ( - _device: <Self ::Ptr as kernel::types::ForeignOwnable >::Borrowed <'_>, - cmd:u32 , - _arg:usize , - ) ->Result <isize > { +fn ioctl (device:Pin <&RustMiscDevice >, cmd:u32 , arg:usize ) ->Result <isize > {pr_info! ("IOCTLing Rust Misc Device Sample\n" ); -match cmd { -RUST_MISC_DEV_HELLO =>pr_info! ("Hello from the Rust Misc Device\n" ), +let size =_IOC_SIZE (cmd); + +let _ =match cmd { +RUST_MISC_DEV_GET_VALUE => device.get_value (UserSlice ::new (arg, size).writer ())?, +RUST_MISC_DEV_SET_VALUE => device.set_value (UserSlice ::new (arg, size).reader ())?, +RUST_MISC_DEV_HELLO => device.hello ()?, _ => { -pr_err! ("IOCTL not recognised: {}\n" , cmd); +pr_err! ("-> IOCTL not recognised: {}\n" , cmd);return Err (EINVAL ); } - } + };Ok (0 ) } } -impl Drop for RustMiscDevice { -fn drop (&mut self) { +#[pinned_drop] +impl PinnedDrop for RustMiscDevice { +fn drop (self:Pin <&mut Self >) {pr_info! ("Exiting the Rust Misc Device Sample\n" ); } } + +impl RustMiscDevice { +fn set_value (&self,mut reader:UserSliceReader ) ->Result <isize > { +let new_value = reader.read ::<i32 >()?; +let mut guard = self.inner.lock (); + +pr_info! ("-> Copying data from userspace (value: {})\n" , new_value); + + guard.value = new_value; +Ok (0 ) + } + +fn get_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { +let guard = self.inner.lock (); + +pr_info! ("-> Copying data to userspace (value: {})\n" , &guard.value); + + writer.write ::<i32 >(&guard.value)?; +Ok (0 ) + } + +fn hello (&self) ->Result <isize > { +pr_info! ("-> Hello from the Rust Misc Device\n" ); + +Ok (0 ) + } +}
Signature · 1 line
2.47.0.338.g60cca15819-goog
Greg KH
maintainerRe: [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality
Quoted 99 lines · Lee Jones
Expand the complexity of the sample driver by providing the ability to get and set an integer. The value is protected by a mutex.
Here is a simple userspace program that fully exercises the sample driver's capabilities.
int
main(){
int value,new_value;
int fd, ret;// Open the device file printf("Opening /dev/rust-misc-device for reading and writing\n"); fd = open("/dev/rust-misc-device", O_RDWR); if (fd < 0) { perror("open"); return errno; } // Make call into driver to say "hello" printf("Calling Hello\n"); ret = ioctl(fd, RUST_MISC_DEV_HELLO, NULL); if (ret < 0) { perror("ioctl: Failed to call into Hello"); close(fd); return errno; } // Get initial value printf("Fetching initial value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to fetch the initial value"); close(fd); return errno; }value++;
// Set value to something different printf("Submitting new value (%d)\n", value); ret = ioctl(fd, RUST_MISC_DEV_SET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to submit new value"); close(fd); return errno; } // Ensure new value was applied printf("Fetching new value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value); if (ret < 0) { perror("ioctl: Failed to fetch the new value"); close(fd); return errno; } if (value != new_value) { printf("Failed: Committed and retrieved values are different (%d - %d)\n", value, new_value); close(fd); return -1; } // Call the unsuccessful ioctl printf("Attempting to call in to an non-existent IOCTL\n"); ret = ioctl(fd, RUST_MISC_DEV_FAIL, NULL); if (ret < 0) { perror("ioctl: Succeeded to fail - this was expected"); } else { printf("ioctl: Failed to fail\n"); close(fd); return -1; } // Close the device file printf("Closing /dev/rust-misc-device\n"); close(fd);
printf("Success\n");
return 0;
}Signed-off-by Lee Jones <[email protected]>--- samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 20 deletions(-)samples/rust/rust_misc_device.rs +2
-1@@ -2,13 +2,20 @@//! Rust misc device sample. +use core::pin::Pin ; +use kernel::{ c_str, - ioctl::_IO,
samples/rust/rust_misc_device.rs line 18
Quoted 10 lines
samples/rust/rust_misc_device.rs +6
-0+ ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, + new_mutex, prelude::*, + sync::Mutex, + uaccess::{UserSlice, UserSliceReader, UserSliceWriter}, }; +const RUST_MISC_DEV_GET_VALUE :u32 =_IOR ::<i32 >('R' as u32 ,7 ); +const RUST_MISC_DEV_SET_VALUE :u32 =_IOW ::<i32 >('R' as u32 ,8 );
Shouldn't this be 'W'?
Quoted 50 lines
samples/rust/rust_misc_device.rs +16
-15const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 );module! {@@ -40,45 +47,80 @@ fn init(_module: &'static ThisModule) -> Result<Self> { } } -struct RustMiscDevice ; +struct Inner { + value:i32 , +} -impl RustMiscDevice { -fn new () ->Self { -Self - } +#[pin_data( PinnedDrop )] +struct RustMiscDevice { +#[pin] + inner:Mutex <Inner >, }#[vtable] impl MiscDevice for RustMiscDevice { -type Ptr =KBox <Self >; +type Ptr =Pin <KBox <Self >>; -fn open () ->Result <KBox <Self >> { +fn open () ->Result <Pin <KBox <Self >>> {pr_info! ("Opening Rust Misc Device Sample\n" ); -Ok (KBox ::new (RustMiscDevice ::new (),GFP_KERNEL )?) +KBox ::try_pin_init ( +try_pin_init! { +RustMiscDevice { inner <-new_mutex! (Inner { value:0_i32 } )} + }, +GFP_KERNEL , + ) } -fn ioctl ( - _device: <Self ::Ptr as kernel::types::ForeignOwnable >::Borrowed <'_>, - cmd:u32 , - _arg:usize , - ) ->Result <isize > { +fn ioctl (device:Pin <&RustMiscDevice >, cmd:u32 , arg:usize ) ->Result <isize > {pr_info! ("IOCTLing Rust Misc Device Sample\n" ); -match cmd { -RUST_MISC_DEV_HELLO =>pr_info! ("Hello from the Rust Misc Device\n" ),
samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> Result<Self> { · line 86
Quoted 10 lines
samples/rust/rust_misc_device.rs +7
-1+let size =_IOC_SIZE (cmd); + +let _ =match cmd { +RUST_MISC_DEV_GET_VALUE => device.get_value (UserSlice ::new (arg, size).writer ())?, +RUST_MISC_DEV_SET_VALUE => device.set_value (UserSlice ::new (arg, size).reader ())?, +RUST_MISC_DEV_HELLO => device.hello ()?, _ => { -pr_err! ("IOCTL not recognised: {}\n" , cmd); +pr_err! ("-> IOCTL not recognised: {}\n" , cmd);return Err (EINVAL );
Nit, wrong return value for an invalid ioctl, I missed that in patch 1, sorry about that.
Quoted 24 lines
samples/rust/rust_misc_device.rs +12
-3} - } + };Ok (0 ) } } -impl Drop for RustMiscDevice { -fn drop (&mut self) { +#[pinned_drop] +impl PinnedDrop for RustMiscDevice { +fn drop (self:Pin <&mut Self >) {pr_info! ("Exiting the Rust Misc Device Sample\n" ); } } + +impl RustMiscDevice { +fn set_value (&self,mut reader:UserSliceReader ) ->Result <isize > { +let new_value = reader.read ::<i32 >()?; +let mut guard = self.inner.lock (); + +pr_info! ("-> Copying data from userspace (value: {})\n" , new_value); +
samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> Result<Self> { · line 117
Quoted 10 lines
samples/rust/rust_misc_device.rs +10
-0+ guard.value = new_value; +Ok (0 ) + } + +fn get_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { +let guard = self.inner.lock (); + +pr_info! ("-> Copying data to userspace (value: {})\n" , &guard.value); + + writer.write ::<i32 >(&guard.value)?;
What happens if it fails, shouldn't your pr_info() happen after this?
thanks,
greg k-h
Quoted 12 lines
samples/rust/rust_misc_device.rs +9
-0+Ok (0 ) + } + +fn hello (&self) ->Result <isize > { +pr_info! ("-> Hello from the Rust Misc Device\n" ); + +Ok (0 ) + } +}Signature · 1 line
2.47.0.338.g60cca15819-goog
Re: [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality
Quoted 108 lines · Greg KH
Quoted 107 lines · Lee Jones
Expand the complexity of the sample driver by providing the ability to get and set an integer. The value is protected by a mutex.
Here is a simple userspace program that fully exercises the sample driver's capabilities.
int
main(){
int value,new_value;
int fd, ret;// Open the device file printf("Opening /dev/rust-misc-device for reading and writing\n"); fd = open("/dev/rust-misc-device", O_RDWR); if (fd < 0) { perror("open"); return errno; } // Make call into driver to say "hello" printf("Calling Hello\n"); ret = ioctl(fd, RUST_MISC_DEV_HELLO, NULL); if (ret < 0) { perror("ioctl: Failed to call into Hello"); close(fd); return errno; } // Get initial value printf("Fetching initial value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to fetch the initial value"); close(fd); return errno; }value++;
// Set value to something different printf("Submitting new value (%d)\n", value); ret = ioctl(fd, RUST_MISC_DEV_SET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to submit new value"); close(fd); return errno; } // Ensure new value was applied printf("Fetching new value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value); if (ret < 0) { perror("ioctl: Failed to fetch the new value"); close(fd); return errno; } if (value != new_value) { printf("Failed: Committed and retrieved values are different (%d - %d)\n", value, new_value); close(fd); return -1; } // Call the unsuccessful ioctl printf("Attempting to call in to an non-existent IOCTL\n"); ret = ioctl(fd, RUST_MISC_DEV_FAIL, NULL); if (ret < 0) { perror("ioctl: Succeeded to fail - this was expected"); } else { printf("ioctl: Failed to fail\n"); close(fd); return -1; } // Close the device file printf("Closing /dev/rust-misc-device\n"); close(fd);
printf("Success\n");
return 0;
}Signed-off-by Lee Jones <[email protected]>--- samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 20 deletions(-)samples/rust/rust_misc_device.rs +6
-1@@ -2,13 +2,20 @@//! Rust misc device sample. +use core::pin::Pin ; +use kernel::{ c_str, - ioctl::_IO, + ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + new_mutex, prelude::*, + sync::Mutex , + uaccess::{UserSlice ,UserSliceReader ,UserSliceWriter }, };
Quoted 4 lines
samples/rust/rust_misc_device.rs line 18
Quoted 2 lines
samples/rust/rust_misc_device.rs +2
-0+const RUST_MISC_DEV_GET_VALUE :u32 =_IOR ::<i32 >('R' as u32 ,7 ); +const RUST_MISC_DEV_SET_VALUE :u32 =_IOW ::<i32 >('R' as u32 ,8 );Shouldn't this be 'W'?
No, I don't think so.
'W' doesn't mean 'write'. It's supposed to be a unique identifier:
'W' 00-1F linux/watchdog.h conflict! 'W' 00-1F linux/wanrouter.h conflict! (pre 3.9) 'W' 00-3F sound/asound.h conflict! 'W' 40-5F drivers/pci/switch/switchtec.c 'W' 60-61 linux/watch_queue.h
'R' isn't registered for this either:
'R' 00-1F linux/random.h conflict! 'R' 01 linux/rfkill.h conflict! 'R' 20-2F linux/trace_mmap.h 'R' C0-DF net/bluetooth/rfcomm.h 'R' E0 uapi/linux/fsl_mc.h
... but since this is just example code with no real purpose, I'm going to hold short of registering a unique identifier for it.
Quoted 48 lines
Quoted 48 lines
samples/rust/rust_misc_device.rs +16
-13const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 );module! {@@ -40,45 +47,80 @@ fn init(_module: &'static ThisModule) -> Result<Self> { } } -struct RustMiscDevice ; +struct Inner { + value:i32 , +} -impl RustMiscDevice { -fn new () ->Self { -Self - } +#[pin_data( PinnedDrop )] +struct RustMiscDevice { +#[pin] + inner:Mutex <Inner >, }#[vtable] impl MiscDevice for RustMiscDevice { -type Ptr =KBox <Self >; +type Ptr =Pin <KBox <Self >>; -fn open () ->Result <KBox <Self >> { +fn open () ->Result <Pin <KBox <Self >>> {pr_info! ("Opening Rust Misc Device Sample\n" ); -Ok (KBox ::new (RustMiscDevice ::new (),GFP_KERNEL )?) +KBox ::try_pin_init ( +try_pin_init! { +RustMiscDevice { inner <-new_mutex! (Inner { value:0_i32 } )} + }, +GFP_KERNEL , + ) } -fn ioctl ( - _device: <Self ::Ptr as kernel::types::ForeignOwnable >::Borrowed <'_>, - cmd:u32 , - _arg:usize , - ) ->Result <isize > { +fn ioctl (device:Pin <&RustMiscDevice >, cmd:u32 , arg:usize ) ->Result <isize > {pr_info! ("IOCTLing Rust Misc Device Sample\n" );
Quoted 15 lines
samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> Result<Self> { · line 86
Quoted 12 lines
samples/rust/rust_misc_device.rs +7
-3-match cmd { -RUST_MISC_DEV_HELLO =>pr_info! ("Hello from the Rust Misc Device\n" ), +let size =_IOC_SIZE (cmd); + +let _ =match cmd { +RUST_MISC_DEV_GET_VALUE => device.get_value (UserSlice ::new (arg, size).writer ())?, +RUST_MISC_DEV_SET_VALUE => device.set_value (UserSlice ::new (arg, size).reader ())?, +RUST_MISC_DEV_HELLO => device.hello ()?, _ => { -pr_err! ("IOCTL not recognised: {}\n" , cmd); +pr_err! ("-> IOCTL not recognised: {}\n" , cmd);return Err (EINVAL );Nit, wrong return value for an invalid ioctl, I missed that in patch 1, sorry about that.
Good shout, thanks.
Quoted 28 lines
Quoted 28 lines
samples/rust/rust_misc_device.rs +16
-3} - } + };Ok (0 ) } } -impl Drop for RustMiscDevice { -fn drop (&mut self) { +#[pinned_drop] +impl PinnedDrop for RustMiscDevice { +fn drop (self:Pin <&mut Self >) {pr_info! ("Exiting the Rust Misc Device Sample\n" ); } } + +impl RustMiscDevice { +fn set_value (&self,mut reader:UserSliceReader ) ->Result <isize > { +let new_value = reader.read ::<i32 >()?; +let mut guard = self.inner.lock (); + +pr_info! ("-> Copying data from userspace (value: {})\n" , new_value); + + guard.value = new_value; +Ok (0 ) + } +
Quoted 8 lines
samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> Result<Self> { · line 117
Quoted 6 lines
samples/rust/rust_misc_device.rs +6
-0+fn get_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { +let guard = self.inner.lock (); + +pr_info! ("-> Copying data to userspace (value: {})\n" , &guard.value); + + writer.write ::<i32 >(&guard.value)?;What happens if it fails, shouldn't your
pr_info()happen after this?
If this fails, I need the line in the log to show where it failed.
It says "copying" as in "attempting to copy", rather than "copied".
Signature · 1 line
Lee Jones [李琼斯]
Greg KH
maintainerRe: [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality
Quoted 127 lines · Lee Jones
Quoted 108 lines · Greg KH
Quoted 107 lines · Lee Jones
Expand the complexity of the sample driver by providing the ability to get and set an integer. The value is protected by a mutex.
Here is a simple userspace program that fully exercises the sample driver's capabilities.
int
main(){
int value,new_value;
int fd, ret;// Open the device file printf("Opening /dev/rust-misc-device for reading and writing\n"); fd = open("/dev/rust-misc-device", O_RDWR); if (fd < 0) { perror("open"); return errno; } // Make call into driver to say "hello" printf("Calling Hello\n"); ret = ioctl(fd, RUST_MISC_DEV_HELLO, NULL); if (ret < 0) { perror("ioctl: Failed to call into Hello"); close(fd); return errno; } // Get initial value printf("Fetching initial value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to fetch the initial value"); close(fd); return errno; }value++;
// Set value to something different printf("Submitting new value (%d)\n", value); ret = ioctl(fd, RUST_MISC_DEV_SET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to submit new value"); close(fd); return errno; } // Ensure new value was applied printf("Fetching new value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value); if (ret < 0) { perror("ioctl: Failed to fetch the new value"); close(fd); return errno; } if (value != new_value) { printf("Failed: Committed and retrieved values are different (%d - %d)\n", value, new_value); close(fd); return -1; } // Call the unsuccessful ioctl printf("Attempting to call in to an non-existent IOCTL\n"); ret = ioctl(fd, RUST_MISC_DEV_FAIL, NULL); if (ret < 0) { perror("ioctl: Succeeded to fail - this was expected"); } else { printf("ioctl: Failed to fail\n"); close(fd); return -1; } // Close the device file printf("Closing /dev/rust-misc-device\n"); close(fd);
printf("Success\n");
return 0;
}Signed-off-by Lee Jones <[email protected]>--- samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 20 deletions(-)samples/rust/rust_misc_device.rs +6
-1@@ -2,13 +2,20 @@//! Rust misc device sample. +use core::pin::Pin ; +use kernel::{ c_str, - ioctl::_IO, + ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + new_mutex, prelude::*, + sync::Mutex , + uaccess::{UserSlice ,UserSliceReader ,UserSliceWriter }, };Quoted 4 lines
samples/rust/rust_misc_device.rs line 18
Quoted 2 lines
samples/rust/rust_misc_device.rs +2
-0+const RUST_MISC_DEV_GET_VALUE :u32 =_IOR ::<i32 >('R' as u32 ,7 ); +const RUST_MISC_DEV_SET_VALUE :u32 =_IOW ::<i32 >('R' as u32 ,8 );Shouldn't this be 'W'?
No, I don't think so.
'W' doesn't mean 'write'. It's supposed to be a unique identifier:
'W' 00-1F linux/watchdog.h conflict! 'W' 00-1F linux/wanrouter.h conflict! (pre 3.9) 'W' 00-3F sound/asound.h conflict! 'W' 40-5F drivers/pci/switch/switchtec.c 'W' 60-61 linux/watch_queue.h'R' isn't registered for this either:
Quoted 8 lines
'R' 00-1F linux/random.h conflict! 'R' 01 linux/rfkill.h conflict! 'R' 20-2F linux/trace_mmap.h 'R' C0-DF net/bluetooth/rfcomm.h 'R' E0 uapi/linux/fsl_mc.h... but since this is just example code with no real purpose, I'm going to hold short of registering a unique identifier for it.
Ah, sorry, I missed that this is the ioctl "name". As the ptrace people will complain, why not use a new one? Ick, ioctl-number.rst is way out of date, but I guess we should carve out one for "sample drivers, do not use in anything real" use cases like here.
Quoted 10 lines
Quoted 8 lines
samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> Result<Self> { · line 117
Quoted 6 lines
samples/rust/rust_misc_device.rs +6
-0+fn get_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { +let guard = self.inner.lock (); + +pr_info! ("-> Copying data to userspace (value: {})\n" , &guard.value); + + writer.write ::<i32 >(&guard.value)?;What happens if it fails, shouldn't your
pr_info()happen after this?If this fails, I need the line in the log to show where it failed.
pr_info() doesn't show file lines from what I remember has that changed?
But wait, this is a misc device, you should be using dev_info() and friends here, no pr_*() stuff please.
Quoted 1 line
It says "copying" as in "attempting to copy", rather than "copied".
Fair enough, but if the copy fails, nothing gets printed out, right?
thanks,
greg k-h
Re: [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality
Quoted 134 lines · Greg KH
Quoted 133 lines · Lee Jones
Quoted 108 lines · Greg KH
Quoted 107 lines · Lee Jones
Expand the complexity of the sample driver by providing the ability to get and set an integer. The value is protected by a mutex.
Here is a simple userspace program that fully exercises the sample driver's capabilities.
int
main(){
int value,new_value;
int fd, ret;// Open the device file printf("Opening /dev/rust-misc-device for reading and writing\n"); fd = open("/dev/rust-misc-device", O_RDWR); if (fd < 0) { perror("open"); return errno; } // Make call into driver to say "hello" printf("Calling Hello\n"); ret = ioctl(fd, RUST_MISC_DEV_HELLO, NULL); if (ret < 0) { perror("ioctl: Failed to call into Hello"); close(fd); return errno; } // Get initial value printf("Fetching initial value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to fetch the initial value"); close(fd); return errno; }value++;
// Set value to something different printf("Submitting new value (%d)\n", value); ret = ioctl(fd, RUST_MISC_DEV_SET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to submit new value"); close(fd); return errno; } // Ensure new value was applied printf("Fetching new value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value); if (ret < 0) { perror("ioctl: Failed to fetch the new value"); close(fd); return errno; } if (value != new_value) { printf("Failed: Committed and retrieved values are different (%d - %d)\n", value, new_value); close(fd); return -1; } // Call the unsuccessful ioctl printf("Attempting to call in to an non-existent IOCTL\n"); ret = ioctl(fd, RUST_MISC_DEV_FAIL, NULL); if (ret < 0) { perror("ioctl: Succeeded to fail - this was expected"); } else { printf("ioctl: Failed to fail\n"); close(fd); return -1; } // Close the device file printf("Closing /dev/rust-misc-device\n"); close(fd);
printf("Success\n");
return 0;
}Signed-off-by Lee Jones <[email protected]>--- samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 20 deletions(-)samples/rust/rust_misc_device.rs +6
-1@@ -2,13 +2,20 @@//! Rust misc device sample. +use core::pin::Pin ; +use kernel::{ c_str, - ioctl::_IO, + ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + new_mutex, prelude::*, + sync::Mutex , + uaccess::{UserSlice ,UserSliceReader ,UserSliceWriter }, };Quoted 4 lines
samples/rust/rust_misc_device.rs line 18
Quoted 2 lines
samples/rust/rust_misc_device.rs +2
-0+const RUST_MISC_DEV_GET_VALUE :u32 =_IOR ::<i32 >('R' as u32 ,7 ); +const RUST_MISC_DEV_SET_VALUE :u32 =_IOW ::<i32 >('R' as u32 ,8 );Shouldn't this be 'W'?
No, I don't think so.
'W' doesn't mean 'write'. It's supposed to be a unique identifier:
'W' 00-1F linux/watchdog.h conflict! 'W' 00-1F linux/wanrouter.h conflict! (pre 3.9) 'W' 00-3F sound/asound.h conflict! 'W' 40-5F drivers/pci/switch/switchtec.c 'W' 60-61 linux/watch_queue.h'R' isn't registered for this either:
'R' 00-1F linux/random.h conflict! 'R' 01 linux/rfkill.h conflict! 'R' 20-2F linux/trace_mmap.h 'R' C0-DF net/bluetooth/rfcomm.h 'R' E0 uapi/linux/fsl_mc.h
Quoted 7 lines
Quoted 2 lines
... but since this is just example code with no real purpose, I'm going to hold short of registering a unique identifier for it.
Ah, sorry, I missed that this is the ioctl "name". As the ptrace people will complain, why not use a new one? Ick,
ioctl-number.rstis way out of date, but I guess we should carve out one for "sample drivers, do not use in anything real" use cases like here.
I can carve one out for Samples.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> Result<Self> { · line 117
Quoted 6 lines
samples/rust/rust_misc_device.rs +6
-0+fn get_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { +let guard = self.inner.lock (); + +pr_info! ("-> Copying data to userspace (value: {})\n" , &guard.value); + + writer.write ::<i32 >(&guard.value)?;What happens if it fails, shouldn't your
pr_info()happen after this?
Quoted 6 lines
Quoted 1 line
If this fails, I need the line in the log to show where it failed.
pr_info()doesn't show file lines from what I remember has that changed?But wait, this is a misc device, you should be using
dev_info()and friends here, no pr_*() stuff please.
Looks like no other Rust driver does this, so I would be the first.
I need to figure out how to obtain Device data via these APIs.
Can I put it on the subsequent work priority list _before_ read/write?
Quoted 3 lines
Quoted 1 line
It says "copying" as in "attempting to copy", rather than "copied".
Fair enough, but if the copy fails, nothing gets printed out, right?
Not from the kernel, but userspace catches it without issue:
ioctl: Failed to submit new value: Bad address
This does not appear to be common practice in kernel Rust.
The commonly used ? operator seems to just propagate.
Signature · 1 line
Lee Jones [李琼斯]
Greg KH
maintainerRe: [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality
Quoted 155 lines · Lee Jones
Quoted 134 lines · Greg KH
Quoted 133 lines · Lee Jones
Quoted 108 lines · Greg KH
Quoted 107 lines · Lee Jones
Expand the complexity of the sample driver by providing the ability to get and set an integer. The value is protected by a mutex.
Here is a simple userspace program that fully exercises the sample driver's capabilities.
int
main(){
int value,new_value;
int fd, ret;// Open the device file printf("Opening /dev/rust-misc-device for reading and writing\n"); fd = open("/dev/rust-misc-device", O_RDWR); if (fd < 0) { perror("open"); return errno; } // Make call into driver to say "hello" printf("Calling Hello\n"); ret = ioctl(fd, RUST_MISC_DEV_HELLO, NULL); if (ret < 0) { perror("ioctl: Failed to call into Hello"); close(fd); return errno; } // Get initial value printf("Fetching initial value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to fetch the initial value"); close(fd); return errno; }value++;
// Set value to something different printf("Submitting new value (%d)\n", value); ret = ioctl(fd, RUST_MISC_DEV_SET_VALUE, &value); if (ret < 0) { perror("ioctl: Failed to submit new value"); close(fd); return errno; } // Ensure new value was applied printf("Fetching new value\n"); ret = ioctl(fd, RUST_MISC_DEV_GET_VALUE, &new_value); if (ret < 0) { perror("ioctl: Failed to fetch the new value"); close(fd); return errno; } if (value != new_value) { printf("Failed: Committed and retrieved values are different (%d - %d)\n", value, new_value); close(fd); return -1; } // Call the unsuccessful ioctl printf("Attempting to call in to an non-existent IOCTL\n"); ret = ioctl(fd, RUST_MISC_DEV_FAIL, NULL); if (ret < 0) { perror("ioctl: Succeeded to fail - this was expected"); } else { printf("ioctl: Failed to fail\n"); close(fd); return -1; } // Close the device file printf("Closing /dev/rust-misc-device\n"); close(fd);
printf("Success\n");
return 0;
}Signed-off-by Lee Jones <[email protected]>--- samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 20 deletions(-)samples/rust/rust_misc_device.rs +6
-1@@ -2,13 +2,20 @@//! Rust misc device sample. +use core::pin::Pin ; +use kernel::{ c_str, - ioctl::_IO, + ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + new_mutex, prelude::*, + sync::Mutex , + uaccess::{UserSlice ,UserSliceReader ,UserSliceWriter }, };Quoted 4 lines
samples/rust/rust_misc_device.rs line 18
Quoted 2 lines
samples/rust/rust_misc_device.rs +2
-0+const RUST_MISC_DEV_GET_VALUE :u32 =_IOR ::<i32 >('R' as u32 ,7 ); +const RUST_MISC_DEV_SET_VALUE :u32 =_IOW ::<i32 >('R' as u32 ,8 );Shouldn't this be 'W'?
No, I don't think so.
'W' doesn't mean 'write'. It's supposed to be a unique identifier:
'W' 00-1F linux/watchdog.h conflict! 'W' 00-1F linux/wanrouter.h conflict! (pre 3.9) 'W' 00-3F sound/asound.h conflict! 'W' 40-5F drivers/pci/switch/switchtec.c 'W' 60-61 linux/watch_queue.h'R' isn't registered for this either:
'R' 00-1F linux/random.h conflict! 'R' 01 linux/rfkill.h conflict! 'R' 20-2F linux/trace_mmap.h 'R' C0-DF net/bluetooth/rfcomm.h 'R' E0 uapi/linux/fsl_mc.hQuoted 7 lines
Quoted 2 lines
... but since this is just example code with no real purpose, I'm going to hold short of registering a unique identifier for it.
Ah, sorry, I missed that this is the ioctl "name". As the ptrace people will complain, why not use a new one? Ick,
ioctl-number.rstis way out of date, but I guess we should carve out one for "sample drivers, do not use in anything real" use cases like here.I can carve one out for Samples.
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> Result<Self> { · line 117
Quoted 6 lines
samples/rust/rust_misc_device.rs +6
-0+fn get_value (&self,mut writer:UserSliceWriter ) ->Result <isize > { +let guard = self.inner.lock (); + +pr_info! ("-> Copying data to userspace (value: {})\n" , &guard.value); + + writer.write ::<i32 >(&guard.value)?;What happens if it fails, shouldn't your
pr_info()happen after this?
Quoted 8 lines
Quoted 6 lines
Quoted 1 line
If this fails, I need the line in the log to show where it failed.
pr_info()doesn't show file lines from what I remember has that changed?But wait, this is a misc device, you should be using
dev_info()and friends here, no pr_*() stuff please.Looks like no other Rust driver does this, so I would be the first.
Yes, because those macros just got added this release cycle :)
Also, moving the existing in-tree drivers to use these macros would be a good idea.
Quoted 1 line
I need to figure out how to obtain Device data via these APIs.
You should have access to it from the misc device structure.
Quoted 1 line
Can I put it on the subsequent work priority list _before_ read/write?
It should be in patch 1 :)
Quoted 11 lines
Quoted 3 lines
Quoted 1 line
It says "copying" as in "attempting to copy", rather than "copied".
Fair enough, but if the copy fails, nothing gets printed out, right?
Not from the kernel, but userspace catches it without issue:
ioctl: Failed to submit new value: Bad address
This does not appear to be common practice in kernel Rust.
The commonly used ? operator seems to just propagate.
Ok, nice!
automatic error handling is going to be one of the killer features here.
greg k-h
Re: [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality
In reply to Lee JonesQuoted 3 lines · Lee Jones
Here is a simple userspace program that fully exercises the sample driver's capabilities.
Before I forget: one idea would be nice to get this program into an actual file so that people can easily give it a try. Or maybe as a ```c ...``` code block in the docs.
Thanks!
Cheers,
Miguel
Greg KH
maintainerQuoted 55 lines · Lee Jones
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +0
-0
samples/rust/rust_misc_device.rs line 1
Quoted 1 line
samples/rust/rust_misc_device.rs new file +1
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0
Nit, you forgot a copyright line here :)
Quoted 72 lines
samples/rust/rust_misc_device.rs new file +72
-0+ +//! Rust misc device sample. + +use kernel::{ + c_str, + ioctl::_IO, + miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + prelude::*, +}; + +const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 ); + +module! { +type :RustMiscDeviceModule , + name:"rust_misc_device" , + author:"Lee Jones" , + description:"Rust misc device sample" , + license:"GPL" , +} + +struct RustMiscDeviceModule { + _miscdev:Pin <KBox <MiscDeviceRegistration <RustMiscDevice >>>, +} + +impl kernel::Module for RustMiscDeviceModule { +fn init (_module: &'staticThisModule ) ->Result <Self > { +pr_info! ("Initialising Rust Misc Device Sample\n" ); + +let options =MiscDeviceOptions { + name:c_str! ("rust-misc-device" ), + }; + +Ok (Self { + _miscdev:KBox ::pin_init ( +MiscDeviceRegistration ::<RustMiscDevice >::register (options), +GFP_KERNEL , + )?, + }) + } +} + +struct RustMiscDevice ; + +impl RustMiscDevice { +fn new () ->Self { +Self + } +} + +#[vtable] +impl MiscDevice for RustMiscDevice { +type Ptr =KBox <Self >; + +fn open () ->Result <KBox <Self >> { +pr_info! ("Opening Rust Misc Device Sample\n" ); + +Ok (KBox ::new (RustMiscDevice ::new (),GFP_KERNEL )?) + } + +fn ioctl ( + _device: <Self ::Ptr as kernel::types::ForeignOwnable >::Borrowed <'_>, + cmd:u32 , + _arg:usize , + ) ->Result <isize > { +pr_info! ("IOCTLing Rust Misc Device Sample\n" ); + +match cmd { +RUST_MISC_DEV_HELLO =>pr_info! ("Hello from the Rust Misc Device\n" ), + _ => { +pr_err! ("IOCTL not recognised: {}\n" , cmd); +return Err (EINVAL ); + }
samples/rust/rust_misc_device.rs line 83
Quoted 10 lines
samples/rust/rust_misc_device.rs new file +10
-0+ } + +Ok (0 ) + } +} + +impl Drop for RustMiscDevice { +fn drop (&mut self) { +pr_info! ("Exiting the Rust Misc Device Sample\n" ); + }
Cool! But no read/write functionality? :)
Anyway, other than the copyright, this looks good to me.
Although we should get the "validate the data" rust patch set in here soon, so we don't have to go and fix up all users of the miscdev rust api at once. Maybe I'll dig that series up over the holiday break if someone doesn't beat me to it.
thanks,
greg k-h
Quoted 49 lines · Greg KH
Quoted 48 lines · Lee Jones
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
Quoted 10 lines
samples/rust/rust_misc_device.rs line 1
Quoted 8 lines
samples/rust/Makefile +0
-0rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +1
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 Nit, you forgot a copyright line here :)
I can add one, but none of the other drivers in this directory has one.
Quoted 76 lines
Quoted 76 lines
Quoted 1 line
+
samples/rust/rust_misc_device.rs new file +75
-0+//! Rust misc device sample. + +use kernel::{ + c_str, + ioctl::_IO, + miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + prelude::*, +}; + +const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 ); + +module! { +type :RustMiscDeviceModule , + name:"rust_misc_device" , + author:"Lee Jones" , + description:"Rust misc device sample" , + license:"GPL" , +} + +struct RustMiscDeviceModule { + _miscdev:Pin <KBox <MiscDeviceRegistration <RustMiscDevice >>>, +} + +impl kernel::Module for RustMiscDeviceModule { +fn init (_module: &'staticThisModule ) ->Result <Self > { +pr_info! ("Initialising Rust Misc Device Sample\n" ); + +let options =MiscDeviceOptions { + name:c_str! ("rust-misc-device" ), + }; + +Ok (Self { + _miscdev:KBox ::pin_init ( +MiscDeviceRegistration ::<RustMiscDevice >::register (options), +GFP_KERNEL , + )?, + }) + } +} + +struct RustMiscDevice ; + +impl RustMiscDevice { +fn new () ->Self { +Self + } +} + +#[vtable] +impl MiscDevice for RustMiscDevice { +type Ptr =KBox <Self >; + +fn open () ->Result <KBox <Self >> { +pr_info! ("Opening Rust Misc Device Sample\n" ); + +Ok (KBox ::new (RustMiscDevice ::new (),GFP_KERNEL )?) + } + +fn ioctl ( + _device: <Self ::Ptr as kernel::types::ForeignOwnable >::Borrowed <'_>, + cmd:u32 , + _arg:usize , + ) ->Result <isize > { +pr_info! ("IOCTLing Rust Misc Device Sample\n" ); + +match cmd { +RUST_MISC_DEV_HELLO =>pr_info! ("Hello from the Rust Misc Device\n" ), + _ => { +pr_err! ("IOCTL not recognised: {}\n" , cmd); +return Err (EINVAL ); + } + } + +Ok (0 ) + }
Quoted 8 lines
samples/rust/rust_misc_device.rs line 83
Quoted 6 lines
samples/rust/rust_misc_device.rs new file +6
-0+} + +impl Drop for RustMiscDevice { +fn drop (&mut self) { +pr_info! ("Exiting the Rust Misc Device Sample\n" ); + }Cool! But no read/write functionality? :)
Happy to add it subsequently.
Quoted 6 lines
Anyway, other than the copyright, this looks good to me.
Although we should get the "validate the data" rust patch set in here soon, so we don't have to go and fix up all users of the miscdev rust api at once. Maybe I'll dig that series up over the holiday break if someone doesn't beat me to it.
What needs doing? Do you have a link?
Signature · 1 line
Lee Jones [李琼斯]
Greg KH
maintainerQuoted 62 lines · Lee Jones
Quoted 49 lines · Greg KH
Quoted 48 lines · Lee Jones
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.oQuoted 10 lines
samples/rust/rust_misc_device.rs line 1
Quoted 8 lines
samples/rust/Makefile +0
-0rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +1
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 Nit, you forgot a copyright line here :)
Quoted 1 line
I can add one, but none of the other drivers in this directory has one.
I think the copyright owner of this file will appreciate that. In fact, I think it might be required by them :)
Quoted 8 lines
Quoted 6 lines
Anyway, other than the copyright, this looks good to me.
Although we should get the "validate the data" rust patch set in here soon, so we don't have to go and fix up all users of the miscdev rust api at once. Maybe I'll dig that series up over the holiday break if someone doesn't beat me to it.
What needs doing? Do you have a link?
https://lore.kernel.org/r/[email protected]
But in thinking about it more, this isn't going to work well with misc devices as the data is coming from userspace, which already goes through the user slice code. Unless userslice should be marking the data as untrusted? I think that needs to happen as well.
thanks,
greg k-h
Quoted 63 lines · Greg KH
Quoted 62 lines · Lee Jones
Quoted 59 lines · Greg KH
Quoted 55 lines · Lee Jones
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +0
-0samples/rust/rust_misc_device.rs line 1
Quoted 1 line
samples/rust/rust_misc_device.rs new file +1
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 Nit, you forgot a copyright line here :)
Quoted 4 lines
Quoted 1 line
I can add one, but none of the other drivers in this directory has one.
I think the copyright owner of this file will appreciate that. In fact, I think it might be required by them :)
I think you're right. Probably just an oversight from the original.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Anyway, other than the copyright, this looks good to me.
Although we should get the "validate the data" rust patch set in here soon, so we don't have to go and fix up all users of the miscdev rust api at once. Maybe I'll dig that series up over the holiday break if someone doesn't beat me to it.
Quoted 8 lines
Quoted 1 line
What needs doing? Do you have a link?
https://lore.kernel.org/r/[email protected]
But in thinking about it more, this isn't going to work well with misc devices as the data is coming from userspace, which already goes through the user slice code. Unless userslice should be marking the data as untrusted? I think that needs to happen as well.
Not too relevant here I think. The data coming in is just a single int that is used as-is rather than any kind of index into memory.
Signature · 1 line
Lee Jones [李琼斯]
Greg KH
maintainerQuoted 83 lines · Lee Jones
Quoted 63 lines · Greg KH
Quoted 62 lines · Lee Jones
Quoted 59 lines · Greg KH
Quoted 55 lines · Lee Jones
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +0
-0samples/rust/rust_misc_device.rs line 1
Quoted 1 line
samples/rust/rust_misc_device.rs new file +1
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 Nit, you forgot a copyright line here :)
Quoted 4 lines
Quoted 1 line
I can add one, but none of the other drivers in this directory has one.
I think the copyright owner of this file will appreciate that. In fact, I think it might be required by them :)
I think you're right. Probably just an oversight from the original.
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines
Anyway, other than the copyright, this looks good to me.
Although we should get the "validate the data" rust patch set in here soon, so we don't have to go and fix up all users of the miscdev rust api at once. Maybe I'll dig that series up over the holiday break if someone doesn't beat me to it.
What needs doing? Do you have a link?
Quoted 7 lines
Quoted 4 lines
But in thinking about it more, this isn't going to work well with misc devices as the data is coming from userspace, which already goes through the user slice code. Unless userslice should be marking the data as untrusted? I think that needs to happen as well.
Not too relevant here I think. The data coming in is just a single int that is used as-is rather than any kind of index into memory.
For now, yes. But in reality, the data is "untrusted" as userspace provided it, and for a sample, we should at least document that otherwise people will forget.
thanks,
greg k-h
Quoted 71 lines · On Wed, 4 Dec 2024 17:46:24 +0000 Lee Jones <[email protected]> wrote:
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +16
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust misc device sample. + +use kernel::{ + c_str, + ioctl::_IO, + miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + prelude::*, +}; + +const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 ); + +module! { +type :RustMiscDeviceModule , + name:"rust_misc_device" ,
samples/rust/rust_misc_device.rs line 26
Quoted 10 lines
samples/rust/rust_misc_device.rs new file +10
-0+ author: "Lee Jones", + description: "Rust misc device sample", + license: "GPL", +} + +struct RustMiscDeviceModule { + _miscdev:Pin <KBox <MiscDeviceRegistration <RustMiscDevice >>>, +} + +impl kernel::Module for RustMiscDeviceModule {
This could probably use kernel::InPlaceModule and avoid allocating the registration.
Quoted 13 lines
samples/rust/rust_misc_device.rs new file +13
-0+fn init (_module: &'staticThisModule ) ->Result <Self > { +pr_info! ("Initialising Rust Misc Device Sample\n" ); + +let options =MiscDeviceOptions { + name:c_str! ("rust-misc-device" ), + }; + +Ok (Self { + _miscdev:KBox ::pin_init ( +MiscDeviceRegistration ::<RustMiscDevice >::register (options), +GFP_KERNEL , + )?, + })
samples/rust/rust_misc_device.rs line 49
Quoted 10 lines
samples/rust/rust_misc_device.rs new file +10
-0+ } +} + +struct RustMiscDevice ; + +impl RustMiscDevice { +fn new () ->Self { +Self + } +}
Given that this function is nearly empty and removed in the next commit, it could probably not exist in the first place.
Quoted 36 lines
samples/rust/rust_misc_device.rs new file +35
-1+ +#[vtable] +impl MiscDevice for RustMiscDevice { +type Ptr =KBox <Self >; + +fn open () ->Result <KBox <Self >> { +pr_info! ("Opening Rust Misc Device Sample\n" ); + +Ok (KBox ::new (RustMiscDevice ::new (),GFP_KERNEL )?) + } + +fn ioctl ( + _device: <Self ::Ptr as kernel::types::ForeignOwnable >::Borrowed <'_>, + cmd:u32 , + _arg:usize , + ) ->Result <isize > { +pr_info! ("IOCTLing Rust Misc Device Sample\n" ); + +match cmd { +RUST_MISC_DEV_HELLO =>pr_info! ("Hello from the Rust Misc Device\n" ), + _ => { +pr_err! ("IOCTL not recognised: {}\n" , cmd); +return Err (EINVAL ); + } + } + +Ok (0 ) + } +} + +impl Drop for RustMiscDevice { +fn drop (&mut self) { +pr_info! ("Exiting the Rust Misc Device Sample\n" ); + } +} --
Quoted 1 line
2.47.0.338.g60cca15819-goog
Benoît du Garreau
Quoted 78 lines · Benoît du Garreau
Quoted 76 lines · On Wed, 4 Dec 2024 17:46:24 +0000 Lee Jones <[email protected]> wrote:
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +21
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust misc device sample. + +use kernel::{ + c_str, + ioctl::_IO, + miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + prelude::*, +}; + +const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 ); + +module! { +type :RustMiscDeviceModule , + name:"rust_misc_device" , + author:"Lee Jones" , + description:"Rust misc device sample" , + license:"GPL" , +} +
Quoted 8 lines
samples/rust/rust_misc_device.rs line 26
Quoted 5 lines
samples/rust/rust_misc_device.rs new file +5
-0+struct RustMiscDeviceModule { + _miscdev:Pin <KBox <MiscDeviceRegistration <RustMiscDevice >>>, +} + +impl kernel::Module for RustMiscDeviceModule {This could probably use
kernel::InPlaceModuleand avoid allocating the registration.
Is there a way to do this and keep the print?
Quoted 18 lines
Quoted 18 lines
samples/rust/rust_misc_device.rs new file +18
-0+fn init (_module: &'staticThisModule ) ->Result <Self > { +pr_info! ("Initialising Rust Misc Device Sample\n" ); + +let options =MiscDeviceOptions { + name:c_str! ("rust-misc-device" ), + }; + +Ok (Self { + _miscdev:KBox ::pin_init ( +MiscDeviceRegistration ::<RustMiscDevice >::register (options), +GFP_KERNEL , + )?, + }) + } +} + +struct RustMiscDevice ; +
Quoted 8 lines
samples/rust/rust_misc_device.rs line 49
Quoted 5 lines
samples/rust/rust_misc_device.rs new file +5
-0+impl RustMiscDevice { +fn new () ->Self { +Self + } +}Given that this function is nearly empty and removed in the next commit, it could probably not exist in the first place.
This is showing my Rust immaturity here.
How does one create a new Self without new() in this context?
Quoted 40 lines
Quoted 36 lines
samples/rust/rust_misc_device.rs new file +35
-1+ +#[vtable] +impl MiscDevice for RustMiscDevice { +type Ptr =KBox <Self >; + +fn open () ->Result <KBox <Self >> { +pr_info! ("Opening Rust Misc Device Sample\n" ); + +Ok (KBox ::new (RustMiscDevice ::new (),GFP_KERNEL )?) + } + +fn ioctl ( + _device: <Self ::Ptr as kernel::types::ForeignOwnable >::Borrowed <'_>, + cmd:u32 , + _arg:usize , + ) ->Result <isize > { +pr_info! ("IOCTLing Rust Misc Device Sample\n" ); + +match cmd { +RUST_MISC_DEV_HELLO =>pr_info! ("Hello from the Rust Misc Device\n" ), + _ => { +pr_err! ("IOCTL not recognised: {}\n" , cmd); +return Err (EINVAL ); + } + } + +Ok (0 ) + } +} + +impl Drop for RustMiscDevice { +fn drop (&mut self) { +pr_info! ("Exiting the Rust Misc Device Sample\n" ); + } +} --Quoted 1 line
2.47.0.338.g60cca15819-goog
Benoît du Garreau
Signature · 1 line
Lee Jones [李琼斯]
Quoted 89 lines · Lee Jones
Quoted 78 lines · Benoît du Garreau
Quoted 76 lines · On Wed, 4 Dec 2024 17:46:24 +0000 Lee Jones <[email protected]> wrote:
This sample driver demonstrates the following basic operations:
* Register a Misc Device
* Create/dev/rust-misc-device
* Open the aforementioned character device
* Operate on the character device via a simpleioctl()
* Close the character deviceSigned-off-by Lee Jones <[email protected]>--- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 84 ++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 samples/rust/rust_misc_device.rssamples/rust/Kconfig +10
-0@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL If unsure, say N. +config SAMPLE_RUST_MISC_DEVICE + tristate "Misc device" + help + This option builds the Rust misc device. + + To compile this as a module, choose M here: + the module will be called rust_misc_device. + + If unsure, say N. + config SAMPLE_RUST_PRINT tristate "Printing macros" helpsamples/rust/Makefile +1
-0@@ -2,6 +2,7 @@ ccflags-y += -I$(src) # needed for trace events obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) += rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o rust_print-y := rust_print_main.o rust_print_events.osamples/rust/rust_misc_device.rs new file +21
-0@@ -0,0 +1,84 @@ +// SPDX-License-Identifier: GPL-2.0 + +//! Rust misc device sample. + +use kernel::{ + c_str, + ioctl::_IO, + miscdevice::{MiscDevice ,MiscDeviceOptions ,MiscDeviceRegistration }, + prelude::*, +}; + +const RUST_MISC_DEV_HELLO :u32 =_IO ('R' as u32 ,9 ); + +module! { +type :RustMiscDeviceModule , + name:"rust_misc_device" , + author:"Lee Jones" , + description:"Rust misc device sample" , + license:"GPL" , +} +Quoted 8 lines
samples/rust/rust_misc_device.rs line 26
Quoted 5 lines
samples/rust/rust_misc_device.rs new file +5
-0+struct RustMiscDeviceModule { + _miscdev:Pin <KBox <MiscDeviceRegistration <RustMiscDevice >>>, +} + +impl kernel::Module for RustMiscDeviceModule {This could probably use
kernel::InPlaceModuleand avoid allocating the registration.
Quoted 1 line
Is there a way to do this and keep the print?
Seeing as this is the concept/API demonstrator, I think what I'd sooner do, is demonstrate some actual initialisation. Maybe with a head-nod to kernel::InPlaceModule in a comment.
Signature · 1 line
Lee Jones [李琼斯]