From nobody Thu Dec 18 08:16:32 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 B381B222565; Thu, 5 Dec 2024 16:25:53 +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=1733415953; cv=none; b=AWtVv47ms0tJgXGXd4SVDaVLmBcN1Ue4BAquf6lRd4VfWaQ6ghouc4syHR0pNtMzK7CAbSzNPeLkclGG5hDdnuyEzQ3atnMhBMXCK6a30PR5X6p9SWrD1DdDi4Pnhk+pl5vUI/kekTGldFytbdruPpuLyW6ym4N536KUVYCjqmA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733415953; c=relaxed/simple; bh=rNO/zBPeJJmilX3vGIpUePq0l+fx97aSIbCzfNre5kg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IX8JRifJajE9hqKq6eF+y+aGnWAX7PBEXsBnevK8Frea0Z+1s9xbgHg8GSQet+/8U1J0caCRzuK2pTs59spOZsEtCi/OA96pZBP/JLzUxP9sFq6t8knSrQQpE6xCTPOFt9TfY4JxPI7HfDVvn1GrUaVCsoWQH176ZaCvJc7j76g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VwUwmZAM; 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="VwUwmZAM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E34CC4CEDC; Thu, 5 Dec 2024 16:25:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733415953; bh=rNO/zBPeJJmilX3vGIpUePq0l+fx97aSIbCzfNre5kg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VwUwmZAMBPhd9RcqL/D9jc9gNrq+pfLOI8LZp38rt6cxwGVHIpFua2El4uFTiYmch Izlz3FjStKlcsvsX8XfGZR9Cpl7A9vHGzIe9tCq9mVL7hpklvpL8AUmxQicu+Mdfm5 ATTdyn8gp3uxt+0L30hcAhMhn246SHq3ZXUs4z0KWK4vCVEb+5DI+ofpJxSHgGjtFl 2g4nE8JOSOrOIl0Lj/SWcEH5NkYPewvwggqzlTIL5BfztxVbeHcFzkEjfD7MNcWb0f Wv8f+XG9pfs0Zx07pGxna9QszWvghKvDZertA8uddhPGPDwBhplMQBga/nTp4iBE3r nV2RXjLeWNKeg== 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 v3 4/5] sample: rust_misc_device: Demonstrate additional get/set value functionality Date: Thu, 5 Dec 2024 16:25:21 +0000 Message-ID: <20241205162531.1883859-5-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241205162531.1883859-1-lee@kernel.org> References: <20241205162531.1883859-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 f50925713f1a..2d40e2bb7a59 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -4,13 +4,20 @@ =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_GET_VALUE: u32 =3D _IOR::('R' as u32, 7); +const RUST_MISC_DEV_SET_VALUE: u32 =3D _IOW::('R' as u32, 8); const RUST_MISC_DEV_HELLO: u32 =3D _IO('R' as u32, 9); =20 module! { @@ -42,39 +49,84 @@ fn init(_module: &'static ThisModule) -> Result { } } =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(ENOIOCTLCMD); } - } + }; =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