From nobody Thu Dec 18 01:38:07 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60B8B204084; Fri, 6 Dec 2024 12:43:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733488981; cv=none; b=KJs5isygF9l6EunCOgwUHQkLDcteU6h8/He64Ss3Jv1XlER9Tx0JK1IdKDU6oDn/MJp1em4Q4Xuh5XWdmuD56r6NssNwdyoqMaEGpjzqkomWcOYwNAwYyYNjY6AzRmZzA2NxEVzY1xveZQ2Zb867xKtsodmXhU8QDU9OjXviFMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733488981; c=relaxed/simple; bh=Py0FD2j7lVfnB2hl12X2dgn6g97O4lfXlXqlXQBJIKw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ohJ9JfpjQPwfn8BvRXq4MBw5RG+B7LKbCIVI56XA1Qb0g+GKhUcmTzoafMsX6d4dyYCjyhWecH7T1EcfBx6NzmaOTDY+nJ3XRRsusDkUFoFWMT3MQ/MluUPwA7vo7NoKty9ZYlsX3io+vfjiYO4fxIv4SpETxsQrkrvKh09M+dE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nXN01lXR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="nXN01lXR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 742EDC4CED1; Fri, 6 Dec 2024 12:42:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733488981; bh=Py0FD2j7lVfnB2hl12X2dgn6g97O4lfXlXqlXQBJIKw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nXN01lXRCgfwOZDpqfUNZkEZLew1X4lj9qFzxf61lzNyoxXqEpfYFzVi5z+eIZ+Dv GZHwSvHaOe3wtSdjryLxKz+m/WD0R/Mp1+K0q8mvPAKwpjZ833tU3XXjIcv7OYiDGk 8/LJ6nadLgxjdE+74yJYB5mFNV0cNrwioRfsuM4NBRFb9etGuiGPcJq3OJg1EvKKef dXR4spcRmsst30g2omEVXVk7im9Lqr+W95yZYQvD4gqdnJ1DrNEhk3x9jfRcHXPtEB xr43wS3+7mI3LP1RyTkhkZY01U0BT/ThkGl9U/0+yuKKemyuNn7l+x6DhzUuUExUmW wH5U3zbuTe9tg== From: Lee Jones To: lee@kernel.org Cc: linux-kernel@vger.kernel.org, arnd@arndb.de, gregkh@linuxfoundation.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: [PATCH v5 3/4] sample: rust_misc_device: Demonstrate additional get/set value functionality Date: Fri, 6 Dec 2024 12:42:14 +0000 Message-ID: <20241206124218.165880-4-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206124218.165880-1-lee@kernel.org> References: <20241206124218.165880-1-lee@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 !=3D 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 =3D 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 ioct= l for device USERSPACE: Closing /dev/rust-misc-device KERNEL: rust_misc_device: Exiting the Rust Misc Device Sample USERSPACE: Success Signed-off-by: Lee Jones --- samples/rust/rust_misc_device.rs | 82 ++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 15 deletions(-) diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_devi= ce.rs index c94b56c454fb..8145cf072640 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -4,14 +4,21 @@ =20 //! Rust misc device sample. =20 +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}, }; =20 const RUST_MISC_DEV_HELLO: u32 =3D _IO('|' as u32, 0x80); +const RUST_MISC_DEV_GET_VALUE: u32 =3D _IOR::('|' as u32, 0x81); +const RUST_MISC_DEV_SET_VALUE: u32 =3D _IOW::('|' as u32, 0x82); =20 module! { type: RustMiscDeviceModule, @@ -41,39 +48,84 @@ fn init(_module: &'static ThisModule) -> impl PinInit { } } =20 -struct RustMiscDevice; +struct Inner { + value: i32, +} + +#[pin_data(PinnedDrop)] +struct RustMiscDevice { + #[pin] + inner: Mutex, +} =20 #[vtable] impl MiscDevice for RustMiscDevice { - type Ptr =3D KBox; + type Ptr =3D Pin>; =20 - fn open() -> Result> { + fn open() -> Result>> { pr_info!("Opening Rust Misc Device Sample\n"); =20 - Ok(KBox::new(RustMiscDevice, GFP_KERNEL)?) + KBox::try_pin_init( + try_pin_init! { + RustMiscDevice { inner <- new_mutex!( Inner{ value: 0_i32 = } )} + }, + GFP_KERNEL, + ) } =20 - fn ioctl( - _device: ::Borrowed<'_= >, - cmd: u32, - _arg: usize, - ) -> Result { + fn ioctl(device: Pin<&RustMiscDevice>, cmd: u32, arg: usize) -> Result= { pr_info!("IOCTLing Rust Misc Device Sample\n"); =20 + let size =3D _IOC_SIZE(cmd); + match cmd { - RUST_MISC_DEV_HELLO =3D> pr_info!("Hello from the Rust Misc De= vice\n"), + RUST_MISC_DEV_GET_VALUE =3D> device.get_value(UserSlice::new(a= rg, size).writer())?, + RUST_MISC_DEV_SET_VALUE =3D> device.set_value(UserSlice::new(a= rg, size).reader())?, + RUST_MISC_DEV_HELLO =3D> device.hello()?, _ =3D> { - pr_err!("IOCTL not recognised: {}\n", cmd); + pr_err!("-> IOCTL not recognised: {}\n", cmd); return Err(ENOTTY); } - } + }; =20 Ok(0) } } =20 -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 { + let new_value =3D reader.read::()?; + let mut guard =3D self.inner.lock(); + + pr_info!("-> Copying data from userspace (value: {})\n", new_value= ); + + guard.value =3D new_value; + Ok(0) + } + + fn get_value(&self, mut writer: UserSliceWriter) -> Result { + let guard =3D self.inner.lock(); + let value =3D guard.value; + + // Refrain from calling write() on a locked resource + drop(guard); + + pr_info!("-> Copying data to userspace (value: {})\n", &value); + + writer.write::(&value)?; + Ok(0) + } + + fn hello(&self) -> Result { + pr_info!("-> Hello from the Rust Misc Device\n"); + + Ok(0) + } +} --=20 2.47.0.338.g60cca15819-goog