All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg KH <gregkh@linuxfoundation.org>
To: Lee Jones <lee@kernel.org>
Cc: linux-kernel@vger.kernel.org, ojeda@kernel.org,
	alex.gaynor@gmail.com, boqun.feng@gmail.com, gary@garyguo.net,
	bjorn3_gh@protonmail.com, benno.lossin@proton.me,
	a.hindborg@kernel.org, aliceryhl@google.com, tmgross@umich.edu,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality
Date: Thu, 5 Dec 2024 10:01:38 +0100	[thread overview]
Message-ID: <2024120517-bright-expire-955e@gregkh> (raw)
In-Reply-To: <20241205083848.GD7451@google.com>

On Thu, Dec 05, 2024 at 08:38:48AM +0000, Lee Jones wrote:
> On Wed, 04 Dec 2024, Greg KH wrote:
> 
> > On Wed, Dec 04, 2024 at 05:46:25PM +0000, Lee Jones wrote:
> > > 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 <lee@kernel.org>
> > > ---
> > >  samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++--------
> > >  1 file changed, 62 insertions(+), 20 deletions(-)
> > > 
> > > diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_device.rs
> > > index 5f1b69569ef7..9c041497d881 100644
> > > --- a/samples/rust/rust_misc_device.rs
> > > +++ b/samples/rust/rust_misc_device.rs
> > > @@ -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);
> > 
> > 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.

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.

> > > +    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.

> 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

  reply	other threads:[~2024-12-05  9:01 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-04 17:46 [PATCH 1/2] samples: rust: Provide example using the new Rust MiscDevice abstraction Lee Jones
2024-12-04 17:46 ` [PATCH 2/2] sample: rust_misc_device: Demonstrate additional get/set value functionality Lee Jones
2024-12-04 18:25   ` Greg KH
2024-12-05  8:38     ` Lee Jones
2024-12-05  9:01       ` Greg KH [this message]
2024-12-05  9:27         ` Lee Jones
2024-12-05  9:47           ` Greg KH
2024-12-09 21:34   ` Miguel Ojeda
2024-12-04 18:20 ` [PATCH 1/2] samples: rust: Provide example using the new Rust MiscDevice abstraction Greg KH
2024-12-05  8:41   ` Lee Jones
2024-12-05  9:03     ` Greg KH
2024-12-05  9:17       ` Lee Jones
2024-12-05  9:26         ` Greg KH
2024-12-05  9:21 ` Benoît du Garreau
2024-12-05  9:46   ` Lee Jones
2024-12-05 10:03     ` Lee Jones

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2024120517-bright-expire-955e@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=lee@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.