Skip to contentSkip to conversation

rust: miscdevice: Provide sample driver using the new MiscDevice bindings

PATCH Superseded Lee Jones 3 participants 18 messages Dec 6, 2024

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Lee Jones

author
#0 lore raw

v5 cover rust: miscdevice: Provide sample driver using the new MiscDevice bindings

It has been suggested that the driver should use dev_info() instead of pr_info() however there is currently no scaffolding to successfully pull a 'struct device' out from driver data post register(). This is being worked on and we will convert this over in due course.

Lee Jones (4):
Documentation: ioctl-number: Carve out some identifiers for use by
sample drivers
samples: rust: Provide example using the new Rust MiscDevice
abstraction
sample: rust_misc_device: Demonstrate additional get/set value
functionality
MAINTAINERS: Add Rust Misc Sample to MISC entry

 .../userspace-api/ioctl/ioctl-number.rst      |   1 +
 MAINTAINERS                                   |   1 +
 samples/rust/Kconfig                          |  10 ++
 samples/rust/Makefile                         |   1 +
 samples/rust/rust_misc_device.rs              | 132 ++++++++++++++++++
 5 files changed, 145 insertions(+)
 create mode 100644 samples/rust/rust_misc_device.rs
Signature · 1 line
2.47.0.338.g60cca15819-goog
author
#1 lore raw

PATCH v5 1/4 Documentation: ioctl-number: Carve out some identifiers for use by sample drivers

32 IDs should be plenty (at yet, not too greedy) since a lot of sample drivers will use their own subsystem allocations.

Sample drivers predominately reside in <KERNEL_ROOT>/samples, but there should be no issue with in-place example drivers using them.

---
 Documentation/userspace-api/ioctl/ioctl-number.rst | 1 +
 1 file changed, 1 insertion(+)
Documentation/userspace-api/ioctl/ioctl-number.rst +1 -0
index 243f1f1b554a..dc4bc0cab69f 100644
@@ -311,6 +311,7 @@ Code  Seq#    Include File                                           Comments
                                                                      <mailto:[email protected]>
 'z'   10-4F  drivers/s390/crypto/zcrypt_api.h                        conflict!
 '|'   00-7F  linux/media.h
+'|'   80-9F  samples/                                                Any sample and example drivers
 0x80  00-1F  linux/fb.h
 0x81  00-1F  linux/vduse.h
 0x89  00-06  arch/x86/include/asm/sockios.h
Signature · 1 line
2.47.0.338.g60cca15819-goog
author
#2 lore raw

PATCH v5 2/4 samples: rust: Provide example using the new Rust MiscDevice abstraction

In reply to Lee Jones

This sample driver demonstrates the following basic operations:

* Register a Misc Device
* Create /dev/rust-misc-device
* Provide open call-back for the aforementioned character device
* Operate on the character device via a simple ioctl()
* Provide close call-back for the character device

---
 samples/rust/Kconfig             | 10 ++++
 samples/rust/Makefile            |  1 +
 samples/rust/rust_misc_device.rs | 79 ++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 samples/rust/rust_misc_device.rs
samples/rust/Kconfig +10 -0
index b0f74a81c8f9..df384e679901 100644
@@ -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
index c1a5c1655395..ad4b97a98580 100644
@@ -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 +79 -0

79 lines not on this page · raw message

Signature · 1 line
2.47.0.338.g60cca15819-goog
author
#3 lore raw

PATCH v5 3/4 sample: rust_misc_device: Demonstrate additional get/set value functionality

In reply to 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;
}

And here is the output (manually spliced together):

USERSPACE: Opening /dev/rust-misc-device for reading and writing
KERNEL: rust_misc_device: Opening Rust Misc Device Sample
USERSPACE: Calling Hello
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Hello from the Rust Misc Device
USERSPACE: Fetching initial value
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Copying data to userspace (value: 0)
USERSPACE: Submitting new value (1)
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Copying data from userspace (value: 1)
USERSPACE: Fetching new value
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> Copying data to userspace (value: 1)
USERSPACE: Attempting to call in to an non-existent IOCTL
KERNEL: rust_misc_device: IOCTLing Rust Misc Device Sample
KERNEL: rust_misc_device: -> IOCTL not recognised: 20992
USERSPACE: ioctl: Succeeded to fail - this was expected: Inappropriate ioctl for device
USERSPACE: Closing /dev/rust-misc-device
KERNEL: rust_misc_device: Exiting the Rust Misc Device Sample
USERSPACE: Success

---
 samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++++------
 1 file changed, 67 insertions(+), 15 deletions(-)
samples/rust/rust_misc_device.rs +67 -15
index c94b56c454fb..8145cf072640 100644
@@ -4,14 +4,21 @@
 
 //! 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_HELLO: u32 = _IO('|' as u32, 0x80);
+const RUST_MISC_DEV_GET_VALUE: u32 = _IOR::<i32>('|' as u32, 0x81);
+const RUST_MISC_DEV_SET_VALUE: u32 = _IOW::<i32>('|' as u32, 0x82);
 
 module! {
     type: RustMiscDeviceModule,
@@ -41,39 +48,84 @@ fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> {
     }
 }
 
-struct RustMiscDevice;
+struct Inner {
+    value: i32,
+}
+
+#[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, 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");
 
+        let size = _IOC_SIZE(cmd);
+
         match cmd {
-            RUST_MISC_DEV_HELLO => pr_info!("Hello from the Rust Misc Device\n"),
+            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(ENOTTY);
             }
-        }
+        };
 
         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();
+        let value = guard.value;
+
+        // Refrain from calling write() on a locked resource
+        drop(guard);
+
+        pr_info!("-> Copying data to userspace (value: {})\n", &value);
+
+        writer.write::<i32>(&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
maintainer
#4 lore raw

Re: [PATCH v5 3/4] sample: rust_misc_device: Demonstrate additional get/set value functionality

Quoted 5 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.

nit, subject line should have "samples" not "sample" :)

samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> { · line 124

Quoted 12 lines
samples/rust/rust_misc_device.rs +12 -0
+    fn get_value(&self, mut writer: UserSliceWriter) -> Result<isize> {
+        let guard = self.inner.lock();
+        let value = guard.value;
+
+        // Refrain from calling write() on a locked resource
+        drop(guard);
+
+        pr_info!("-> Copying data to userspace (value: {})\n", &value);
+
+        writer.write::<i32>(&value)?;
+        Ok(0)
+    }

I don't understand why you have to drop the mutex before calling pr_info() and write (i.e. copy_to_user())? It's a mutex, not a spinlock, so you can hold it over that potentially-sleeping call, right? Or is there some other reason why here?

thanks,

greg k-h

author
#5 lore raw

Re: [PATCH v5 3/4] sample: rust_misc_device: Demonstrate additional get/set value functionality

Quoted 8 lines · Greg KH
Quoted 5 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.

nit, subject line should have "samples" not "sample" :)

Ack.

Quoted 9 lines
Quoted 9 lines
samples/rust/rust_misc_device.rs +9 -0
+    fn get_value(&self, mut writer: UserSliceWriter) -> Result<isize> {
+        let guard = self.inner.lock();
+        let value = guard.value;
+
+        // Refrain from calling write() on a locked resource
+        drop(guard);
+
+        pr_info!("-> Copying data to userspace (value: {})\n", &value);
+
Quoted 8 lines

samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> { · line 124

Quoted 3 lines
samples/rust/rust_misc_device.rs +3 -0
+        writer.write::<i32>(&value)?;
+        Ok(0)
+    }

I don't understand why you have to drop the mutex before calling pr_info() and write (i.e. copy_to_user())? It's a mutex, not a spinlock, so you can hold it over that potentially-sleeping call, right? Or is there some other reason why here?

This was a request from Alice to demonstrate how to unlock a mutex.

Signature · 1 line
Lee Jones [李琼斯]
author
#6 lore raw

Re: [PATCH v5 3/4] sample: rust_misc_device: Demonstrate additional get/set value functionality

Quoted 31 lines · Lee Jones
Quoted 8 lines · Greg KH
Quoted 5 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.

nit, subject line should have "samples" not "sample" :)

Ack.

Quoted 9 lines
Quoted 9 lines
samples/rust/rust_misc_device.rs +9 -0
+    fn get_value(&self, mut writer: UserSliceWriter) -> Result<isize> {
+        let guard = self.inner.lock();
+        let value = guard.value;
+
+        // Refrain from calling write() on a locked resource
+        drop(guard);
+
+        pr_info!("-> Copying data to userspace (value: {})\n", &value);
+
Quoted 8 lines

samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> { · line 124

Quoted 3 lines
samples/rust/rust_misc_device.rs +3 -0
+        writer.write::<i32>(&value)?;
+        Ok(0)
+    }

I don't understand why you have to drop the mutex before calling pr_info() and write (i.e. copy_to_user())? It's a mutex, not a spinlock, so you can hold it over that potentially-sleeping call, right? Or is there some other reason why here?

Quoted 1 line

This was a request from Alice to demonstrate how to unlock a mutex.

It's common practice to apply guards only around the protected value.

Why would this be different?

Signature · 1 line
Lee Jones [李琼斯]
maintainer
#7 lore raw

Re: [PATCH v5 3/4] sample: rust_misc_device: Demonstrate additional get/set value functionality

Quoted 21 lines · Lee Jones
Quoted 20 lines · Lee Jones
Quoted 18 lines · Greg KH

samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> { · line 124

Quoted 12 lines · Lee Jones
samples/rust/rust_misc_device.rs +12 -0
+    fn get_value(&self, mut writer: UserSliceWriter) -> Result<isize> {
+        let guard = self.inner.lock();
+        let value = guard.value;
+
+        // Refrain from calling write() on a locked resource
+        drop(guard);
+
+        pr_info!("-> Copying data to userspace (value: {})\n", &value);
+
+        writer.write::<i32>(&value)?;
+        Ok(0)
+    }

I don't understand why you have to drop the mutex before calling pr_info() and write (i.e. copy_to_user())? It's a mutex, not a spinlock, so you can hold it over that potentially-sleeping call, right? Or is there some other reason why here?

Quoted 5 lines
Quoted 1 line

This was a request from Alice to demonstrate how to unlock a mutex.

It's common practice to apply guards only around the protected value.

Why would this be different?

It isn't, it's just that you are implying that the guard has to be dropped because of the call to write(), which is confusing. It's only "needed" because you want to guard a single cpu instruction that is guaranteed atomic by the processor :)

As this is an example driver, documentation is essential, so maybe the comment should be:

	// Drop the mutex as we can now use our local copy

or something like that.

thanks,

greg k-h

author
#8 lore raw

Re: [PATCH v5 3/4] sample: rust_misc_device: Demonstrate additional get/set value functionality

Quoted 33 lines · Greg KH
Quoted 21 lines · Lee Jones
Quoted 20 lines · Lee Jones
Quoted 18 lines · Greg KH

samples/rust/rust_misc_device.rs fn init(_module: &'static ThisModule) -> impl PinInit<Self, Error> { · line 124

Quoted 12 lines · Lee Jones
samples/rust/rust_misc_device.rs +12 -0
+    fn get_value(&self, mut writer: UserSliceWriter) -> Result<isize> {
+        let guard = self.inner.lock();
+        let value = guard.value;
+
+        // Refrain from calling write() on a locked resource
+        drop(guard);
+
+        pr_info!("-> Copying data to userspace (value: {})\n", &value);
+
+        writer.write::<i32>(&value)?;
+        Ok(0)
+    }

I don't understand why you have to drop the mutex before calling pr_info() and write (i.e. copy_to_user())? It's a mutex, not a spinlock, so you can hold it over that potentially-sleeping call, right? Or is there some other reason why here?

Quoted 5 lines
Quoted 1 line

This was a request from Alice to demonstrate how to unlock a mutex.

It's common practice to apply guards only around the protected value.

Why would this be different?

It isn't, it's just that you are implying that the guard has to be dropped because of the call to write(), which is confusing. It's only "needed" because you want to guard a single cpu instruction that is guaranteed atomic by the processor :)

Quoted 4 lines

As this is an example driver, documentation is essential, so maybe the comment should be:

	// Drop the mutex as we can now use our local copy

or something like that.

Sounds reasonable.

I've ran out of time this week. I'll take another peek next week.

Signature · 1 line
Lee Jones [李琼斯]
author
#9 lore raw

PATCH v5 4/4 MAINTAINERS: Add Rust Misc Sample to MISC entry

In reply to Lee Jones
---
 MAINTAINERS | 1 +
 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
index 21f855fe468b..ea5f7c628235 100644
@@ -5328,6 +5328,7 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/char-misc.git
 F:	drivers/char/
 F:	drivers/misc/
 F:	include/linux/miscdevice.h
+F:	samples/rust/rust_misc_device.rs
 X:	drivers/char/agp/
 X:	drivers/char/hw_random/
 X:	drivers/char/ipmi/
Signature · 1 line
2.47.0.338.g60cca15819-goog
#10 lore raw
In reply to Lee Jones
Quoted 4 lines · Lee Jones

It has been suggested that the driver should use dev_info() instead of pr_info() however there is currently no scaffolding to successfully pull a 'struct device' out from driver data post register(). This is being worked on and we will convert this over in due course.

I think you're going too fast with this series.

Please address the comments you receive before sending out new versions.

Also, please document the changes you have made from one version to the next, otherwise it's gonna be very hard to review this.

Thanks,
Danilo

Quoted 22 lines

Lee Jones (4):
Documentation: ioctl-number: Carve out some identifiers for use by
sample drivers
samples: rust: Provide example using the new Rust MiscDevice
abstraction
sample: rust_misc_device: Demonstrate additional get/set value
functionality
MAINTAINERS: Add Rust Misc Sample to MISC entry

 .../userspace-api/ioctl/ioctl-number.rst      |   1 +
 MAINTAINERS                                   |   1 +
 samples/rust/Kconfig                          |  10 ++
 samples/rust/Makefile                         |   1 +
 samples/rust/rust_misc_device.rs              | 132 ++++++++++++++++++
 5 files changed, 145 insertions(+)
 create mode 100644 samples/rust/rust_misc_device.rs
Signature · 1 line
2.47.0.338.g60cca15819-goog
author
#11 lore raw
Quoted 6 lines · Danilo Krummrich
Quoted 4 lines · Lee Jones

It has been suggested that the driver should use dev_info() instead of pr_info() however there is currently no scaffolding to successfully pull a 'struct device' out from driver data post register(). This is being worked on and we will convert this over in due course.

Quoted 6 lines

I think you're going too fast with this series.

Please address the comments you receive before sending out new versions.

Also, please document the changes you have made from one version to the next, otherwise it's gonna be very hard to review this.

I can add a change log.

What comments were missed?

Signature · 1 line
Lee Jones [李琼斯]
#12 lore raw
Quoted 10 lines · Lee Jones
Quoted 8 lines · Danilo Krummrich
Quoted 4 lines · Lee Jones

It has been suggested that the driver should use dev_info() instead of pr_info() however there is currently no scaffolding to successfully pull a 'struct device' out from driver data post register(). This is being worked on and we will convert this over in due course.

I think you're going too fast with this series.

Quoted 8 lines
Quoted 4 lines

Please address the comments you receive before sending out new versions.

Also, please document the changes you have made from one version to the next, otherwise it's gonna be very hard to review this.

I can add a change log.

What comments were missed?

I think MiscDevice should ideally use the generic Registration type from [1].

I see that you use InPlaceModule now, which is fine. But since this is just a sample, we could probably afford to wait until the generic type lands.

Also, there was a comment about how we can make use of the dev_* macros.

I really think we should fix those before we land a sample driver. It's gonna be hard to explain people later on that they shouldn't do what the example does...

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

Quoted 3 lines
Signature · 2 lines
-- 
Lee Jones [李琼斯]
author
#13 lore raw
Quoted 13 lines · Danilo Krummrich
Quoted 12 lines · Lee Jones
Quoted 10 lines · Danilo Krummrich
Quoted 4 lines · Lee Jones

It has been suggested that the driver should use dev_info() instead of pr_info() however there is currently no scaffolding to successfully pull a 'struct device' out from driver data post register(). This is being worked on and we will convert this over in due course.

I think you're going too fast with this series.

Please address the comments you receive before sending out new versions.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Also, please document the changes you have made from one version to the next, otherwise it's gonna be very hard to review this.

I can add a change log.

What comments were missed?

I think MiscDevice should ideally use the generic Registration type from [1].

How can an in-tree driver use out-of-tree functionality?

Quoted 8 lines

I see that you use InPlaceModule now, which is fine. But since this is just a sample, we could probably afford to wait until the generic type lands.

Also, there was a comment about how we can make use of the dev_* macros.

I really think we should fix those before we land a sample driver. It's gonna be hard to explain people later on that they shouldn't do what the example does...

We're authoring the sample based on what is available at the moment.

There will always be something better / more convenient coming down the pipe. We don't usually put off contributors pending acceptance of out-of-tree functionality, sample or otherwise.

As I've already mentioned, I'd be _more than_ happy to keep improving this over time as new and improved helpers / infra. arrives.

Quoted 1 line

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

Signature · 1 line
Lee Jones [李琼斯]
#14 lore raw
Quoted 18 lines · Lee Jones
Quoted 16 lines · Danilo Krummrich
Quoted 15 lines · Lee Jones
Quoted 12 lines · Danilo Krummrich
Quoted 4 lines · Lee Jones

It has been suggested that the driver should use dev_info() instead of pr_info() however there is currently no scaffolding to successfully pull a 'struct device' out from driver data post register(). This is being worked on and we will convert this over in due course.

I think you're going too fast with this series.

Please address the comments you receive before sending out new versions.

Also, please document the changes you have made from one version to the next, otherwise it's gonna be very hard to review this.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I can add a change log.

What comments were missed?

I think MiscDevice should ideally use the generic Registration type from [1].

How can an in-tree driver use out-of-tree functionality?

AFAICT, this sample module is in the exact same stage as the generic device / driver infrastructure.

Both are on the mailing list in discussion for inclusion into the kernel. Labeling one as in-tree and the other one as out-of-tree is clearly misleading.

I'm just saying it would be good to align this. If the sample driver is time critical, I have no problem if you go ahead with the current MiscDeviceRegistration and InPlaceModule, but again, why not align it from the get-go?

Quoted 11 lines
Quoted 9 lines

I see that you use InPlaceModule now, which is fine. But since this is just a sample, we could probably afford to wait until the generic type lands.

Also, there was a comment about how we can make use of the dev_* macros.

I really think we should fix those before we land a sample driver. It's gonna be hard to explain people later on that they shouldn't do what the example does...

We're authoring the sample based on what is available at the moment.

Well, for this I have to disagree, not being able to use the dev_* macros is simply meaning that the abstraction is incomplete (in this aspect).

I don't see the need to land a sample driver that tells the user to do the wrong thing, i.e. use the pr_* macros.

As Alice mentioned, you can get the miscdevice pointer from the file private data in open() and then make it accessible in the other fops hooks. If we go for this solution it will change the callbacks of MiscDevice and maybe even some other architectural aspects.

This needs to be addressed first.

Quoted 4 lines

There will always be something better / more convenient coming down the pipe. We don't usually put off contributors pending acceptance of out-of-tree functionality, sample or otherwise.

No one asks for this here. But if the example reveals shortcomings, we shouldn't promote them as example.

Quoted 8 lines

As I've already mentioned, I'd be _more than_ happy to keep improving this over time as new and improved helpers / infra. arrives.

Quoted 1 line

[1] https://lore.kernel.org/rust-for-linux/[email protected]/

Signature · 1 line
Lee Jones [李琼斯]
author
#15 lore raw
Quoted 25 lines · Danilo Krummrich
Quoted 24 lines · Lee Jones
Quoted 21 lines · Danilo Krummrich
Quoted 10 lines · Lee Jones
Quoted 8 lines · Danilo Krummrich
Quoted 4 lines · Lee Jones

It has been suggested that the driver should use dev_info() instead of pr_info() however there is currently no scaffolding to successfully pull a 'struct device' out from driver data post register(). This is being worked on and we will convert this over in due course.

I think you're going too fast with this series.

Quoted 8 lines
Quoted 4 lines

Please address the comments you receive before sending out new versions.

Also, please document the changes you have made from one version to the next, otherwise it's gonna be very hard to review this.

I can add a change log.

What comments were missed?

I think MiscDevice should ideally use the generic Registration type from [1].

Quoted 7 lines
Quoted 1 line

How can an in-tree driver use out-of-tree functionality?

AFAICT, this sample module is in the exact same stage as the generic device / driver infrastructure.

Both are on the mailing list in discussion for inclusion into the kernel. Labeling one as in-tree and the other one as out-of-tree is clearly misleading.

If I was saying that, I'd agree with you.

I was asking how MiscDevice (in-tree) could use Registration (out-of-free).

Quoted 4 lines

I'm just saying it would be good to align this. If the sample driver is time critical, I have no problem if you go ahead with the current MiscDeviceRegistration and InPlaceModule, but again, why not align it from the get-go?

Because it's not available yet. :)

Quoted 17 lines
Quoted 10 lines
Quoted 8 lines

I see that you use InPlaceModule now, which is fine. But since this is just a sample, we could probably afford to wait until the generic type lands.

Also, there was a comment about how we can make use of the dev_* macros.

I really think we should fix those before we land a sample driver. It's gonna be hard to explain people later on that they shouldn't do what the example does...

We're authoring the sample based on what is available at the moment.

Well, for this I have to disagree, not being able to use the dev_* macros is simply meaning that the abstraction is incomplete (in this aspect).

I don't see the need to land a sample driver that tells the user to do the wrong thing, i.e. use the pr_* macros.

Quoted 6 lines

As Alice mentioned, you can get the miscdevice pointer from the file private data in open() and then make it accessible in the other fops hooks. If we go for this solution it will change the callbacks of MiscDevice and maybe even some other architectural aspects.

This needs to be addressed first.

The issue about ever growing dependencies _can_ be that authors have other priorities and are slow to turn things around, which may end up with nothing being accepted and contributors getting frustrated.

However, taking into consideration how swift Alice is with these things, I'd be happy to wait for this part if people are insistent.

Quoted 6 lines
Quoted 3 lines

There will always be something better / more convenient coming down the pipe. We don't usually put off contributors pending acceptance of out-of-tree functionality, sample or otherwise.

No one asks for this here. But if the example reveals shortcomings, we shouldn't promote them as example.

IMHO it's reasonable for the sample to represent the current status of the frameworks in use. As advancements / adaptions are introduced we can use them to continually improve the example.

Signature · 1 line
Lee Jones [李琼斯]
#16 lore raw
Quoted 20 lines · Lee Jones
Quoted 19 lines · Danilo Krummrich
Quoted 7 lines
Quoted 5 lines

Also, there was a comment about how we can make use of the dev_* macros.

I really think we should fix those before we land a sample driver. It's gonna be hard to explain people later on that they shouldn't do what the example does...

We're authoring the sample based on what is available at the moment.

Well, for this I have to disagree, not being able to use the dev_* macros is simply meaning that the abstraction is incomplete (in this aspect).

I don't see the need to land a sample driver that tells the user to do the wrong thing, i.e. use the pr_* macros.

As Alice mentioned, you can get the miscdevice pointer from the file private data in open() and then make it accessible in the other fops hooks. If we go for this solution it will change the callbacks of MiscDevice and maybe even some other architectural aspects.

Quoted 5 lines
Quoted 1 line

This needs to be addressed first.

The issue about ever growing dependencies _can_ be that authors have other priorities and are slow to turn things around, which may end up with nothing being accepted and contributors getting frustrated.

I would share your argumentation if

1) we'd talk about a real driver, where people are actually waiting for,
2) it'd be about a new feature, performance improvement, etc.

What we have here is different:

You wrote a sample implementation for a new and just landed abstraction that reveals a shortcoming. (Which is great, because it means the sample already served an important purpose.)

IMHO, the consequence should not be to merge the sample as is anyways, because another purpose of the sample implementation is to tell people "look, this is exactly how it should look like, please do it the same way".

Instead, we should fix the shortcoming, adjust the sample implementation and merge it then.

Just to make it clear, for a real driver I think it would be reasonable to just go ahead, but for a sample that should educate, we should fix things first.

Quoted 17 lines

However, taking into consideration how swift Alice is with these things, I'd be happy to wait for this part if people are insistent.

Quoted 6 lines
Quoted 3 lines

There will always be something better / more convenient coming down the pipe. We don't usually put off contributors pending acceptance of out-of-tree functionality, sample or otherwise.

No one asks for this here. But if the example reveals shortcomings, we shouldn't promote them as example.

IMHO it's reasonable for the sample to represent the current status of the frameworks in use. As advancements / adaptions are introduced we can use them to continually improve the example.

Signature · 1 line
Lee Jones [李琼斯]
author
#17 lore raw
Quoted 42 lines · Danilo Krummrich
Quoted 20 lines · Lee Jones
Quoted 19 lines · Danilo Krummrich
Quoted 7 lines
Quoted 5 lines

Also, there was a comment about how we can make use of the dev_* macros.

I really think we should fix those before we land a sample driver. It's gonna be hard to explain people later on that they shouldn't do what the example does...

We're authoring the sample based on what is available at the moment.

Well, for this I have to disagree, not being able to use the dev_* macros is simply meaning that the abstraction is incomplete (in this aspect).

I don't see the need to land a sample driver that tells the user to do the wrong thing, i.e. use the pr_* macros.

As Alice mentioned, you can get the miscdevice pointer from the file private data in open() and then make it accessible in the other fops hooks. If we go for this solution it will change the callbacks of MiscDevice and maybe even some other architectural aspects.

Quoted 5 lines
Quoted 1 line

This needs to be addressed first.

The issue about ever growing dependencies _can_ be that authors have other priorities and are slow to turn things around, which may end up with nothing being accepted and contributors getting frustrated.

I would share your argumentation if

1) we'd talk about a real driver, where people are actually waiting for,
2) it'd be about a new feature, performance improvement, etc.

What we have here is different:

You wrote a sample implementation for a new and just landed abstraction that reveals a shortcoming. (Which is great, because it means the sample already served an important purpose.)

IMHO, the consequence should not be to merge the sample as is anyways, because another purpose of the sample implementation is to tell people "look, this is exactly how it should look like, please do it the same way".

Quoted 5 lines

Instead, we should fix the shortcoming, adjust the sample implementation and merge it then.

Just to make it clear, for a real driver I think it would be reasonable to just go ahead, but for a sample that should educate, we should fix things first.

Provided that we stay within certain tolerances, I don't see any of what you've said as particularly unreasonable. I'll have an out-of-band chat with Alice on Monday with a view to conjuring up a game plan.

Signature · 1 line
Lee Jones [李琼斯]