From nobody Wed Dec 17 21:28:16 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 D0A3F202F7C; Fri, 6 Dec 2024 12:42:47 +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=1733488967; cv=none; b=EKJT2ySlKxVL61x2395We5Z2ZAI4E/0cBABx9BUg91E8p9OX+QVnHCSac/q/l/hc2ycQO/3QJ9fuKtBY9sO6x+HVxJPuF0IY632XTw3FYgDKHiFGgz7+Oc/5G+PQNNeLJSV1g78GnCF5WZX40PkjhhNhC8W02prsaJlzPzhdk5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733488967; c=relaxed/simple; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R/WjRk2+uX36FnkstmggbHN2J9VeX5n2/aSpsYqcqNjKqN8ZRs+nNv1NhkGU2LtY51ssrn6LjTYJhEFxnnaaETfne6dmFBmMkpiIHUF0hiazl3QMsOI34VXHdDblP1Xv45kxxymIhxoPqtuc+YVKyGcxkfJG+TPYo1+0oY2909A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=a+n0/+uG; 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="a+n0/+uG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44378C4AF09; Fri, 6 Dec 2024 12:42:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733488967; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=a+n0/+uGtvOKALSQgobwyvPSupUwFdAFldRrey79jA3zmTfz0f9LY/wT8nSWnjvGX qRF6ADfoum7VFiQzv++Qi7innBfZ3v62/d9WSRIJja27Erlw6P6qXS2FbkVmw8KVXt cl/nzbvY0jTB8LJAgqigY/Ff6ONACILEpzqBT3KiXR3jijnBU/Cx8x/n5ffgteVpeS K3an0NaISg3uVTKrpqwH7N6+wzKGv+zkHpQp/WXCoyn7r9IaFtup2tqxpaY+s4aTls mReDJ3BEFjINCjE0avmP2tD4QdjWsQL1UbeOToswptAM3B3SIjH6upL3AcwOMvnLyR Gd+v1/jaxS/ng== 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 1/4] Documentation: ioctl-number: Carve out some identifiers for use by sample drivers Date: Fri, 6 Dec 2024 12:42:12 +0000 Message-ID: <20241206124218.165880-2-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" 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 /samples, but there should be no issue with in-place example drivers using them. Signed-off-by: Lee Jones --- Documentation/userspace-api/ioctl/ioctl-number.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Documentation/userspace-api/ioctl/ioctl-number.rst b/Documenta= tion/userspace-api/ioctl/ioctl-number.rst index 243f1f1b554a..dc4bc0cab69f 100644 --- a/Documentation/userspace-api/ioctl/ioctl-number.rst +++ b/Documentation/userspace-api/ioctl/ioctl-number.rst @@ -311,6 +311,7 @@ Code Seq# Include File = Comments 'z' 10-4F drivers/s390/crypto/zcrypt_api.h confl= ict! '|' 00-7F linux/media.h +'|' 80-9F samples/ Any s= ample and example drivers 0x80 00-1F linux/fb.h 0x81 00-1F linux/vduse.h 0x89 00-06 arch/x86/include/asm/sockios.h --=20 2.47.0.338.g60cca15819-goog From nobody Wed Dec 17 21:28:16 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 66B2C2036F3; Fri, 6 Dec 2024 12:42:54 +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=1733488974; cv=none; b=MQ0z+OGpOgS1x0mHUiFG+Ni03+muOWS6+BGQ/qn0aNU+23fPubTeXtU7T/NmhUE4p7PNz9QUTe2z5loe99hRCFg67GPeKFBYJ1lpyHYXpLJ7aKzIbXCjBV2dha1T5HEXvEPKDdG1cGl3newf8r8TqkWB7N7uFQgpwylr+uMA0f0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733488974; c=relaxed/simple; bh=WFkpKiXyvQ59F/bCxcLhHbqQzdnF8Nx2WniZliuSybA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=r1HXOVb970uX0osP3/CB42yIR1d85vNdKbKED59R0c/b7dh+E46SKfviHwKVVZ/FrjKUgu3iqKn3BKI++/CVGtNrmnuuvaNwYR7Lg/K9sRYl37ACl7cANAADwOmFYIpIKaUW3QQT1VGRovUXwonB+pLXRcI+ilceiaQahl9hvAE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DE0a3bPG; 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="DE0a3bPG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1EA29C4CED1; Fri, 6 Dec 2024 12:42:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733488974; bh=WFkpKiXyvQ59F/bCxcLhHbqQzdnF8Nx2WniZliuSybA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DE0a3bPGqAObx3iJSMPQZ6MAwgC8vB5Y6m7QdcN6s3rnH+xu64+/8Y0LzfBjSSDe5 1pUkXu+E4hmzoqWKgx11Vksm31z1+wii+CZHhxO0U6oq4Fc+lqRXVO9ufi6rJgEmJ2 SPG6nOMW/DesdRZ4nPCdjbIS+YixUdCCVZjo7qL0P+G379H+ny6FViMdxPGgHkCBn0 7ZBtyf3XNwFaTXcNUX/bnm5TW0ef2ZlB0AR2ItIBqdt6G1UcnsBf4L+vSGQONAWbIq OUgKI/l4AloKTGEem/1AbILRUv6XqhYfzNqJTdaow2fNX3wym6pdh7N/JoMqGuM2vt rlWY3pj/Wzbrg== 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 2/4] samples: rust: Provide example using the new Rust MiscDevice abstraction Date: Fri, 6 Dec 2024 12:42:13 +0000 Message-ID: <20241206124218.165880-3-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" 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 Signed-off-by: Lee Jones --- 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 diff --git a/samples/rust/Kconfig b/samples/rust/Kconfig index b0f74a81c8f9..df384e679901 100644 --- a/samples/rust/Kconfig +++ b/samples/rust/Kconfig @@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL =20 If unsure, say N. =20 +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 diff --git a/samples/rust/Makefile b/samples/rust/Makefile index c1a5c1655395..ad4b97a98580 100644 --- a/samples/rust/Makefile +++ b/samples/rust/Makefile @@ -2,6 +2,7 @@ ccflags-y +=3D -I$(src) # needed for trace events =20 obj-$(CONFIG_SAMPLE_RUST_MINIMAL) +=3D rust_minimal.o +obj-$(CONFIG_SAMPLE_RUST_MISC_DEVICE) +=3D rust_misc_device.o obj-$(CONFIG_SAMPLE_RUST_PRINT) +=3D rust_print.o =20 rust_print-y :=3D rust_print_main.o rust_print_events.o diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_devi= ce.rs new file mode 100644 index 000000000000..c94b56c454fb --- /dev/null +++ b/samples/rust/rust_misc_device.rs @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Rust misc device sample. + +use kernel::{ + c_str, + ioctl::_IO, + miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, + prelude::*, +}; + +const RUST_MISC_DEV_HELLO: u32 =3D _IO('|' as u32, 0x80); + +module! { + type: RustMiscDeviceModule, + name: "rust_misc_device", + author: "Lee Jones", + description: "Rust misc device sample", + license: "GPL", +} + +#[pin_data] +struct RustMiscDeviceModule { + #[pin] + _miscdev: MiscDeviceRegistration, +} + +impl kernel::InPlaceModule for RustMiscDeviceModule { + fn init(_module: &'static ThisModule) -> impl PinInit { + pr_info!("Initialising Rust Misc Device Sample\n"); + + let options =3D MiscDeviceOptions { + name: c_str!("rust-misc-device"), + }; + + try_pin_init!(Self { + _miscdev <- MiscDeviceRegistration::register(options), + }) + } +} + +struct RustMiscDevice; + +#[vtable] +impl MiscDevice for RustMiscDevice { + type Ptr =3D KBox; + + fn open() -> Result> { + pr_info!("Opening Rust Misc Device Sample\n"); + + Ok(KBox::new(RustMiscDevice, GFP_KERNEL)?) + } + + fn ioctl( + _device: ::Borrowed<'_= >, + cmd: u32, + _arg: usize, + ) -> Result { + pr_info!("IOCTLing Rust Misc Device Sample\n"); + + match cmd { + RUST_MISC_DEV_HELLO =3D> pr_info!("Hello from the Rust Misc De= vice\n"), + _ =3D> { + pr_err!("IOCTL not recognised: {}\n", cmd); + return Err(ENOTTY); + } + } + + Ok(0) + } +} + +impl Drop for RustMiscDevice { + fn drop(&mut self) { + pr_info!("Exiting the Rust Misc Device Sample\n"); + } +} --=20 2.47.0.338.g60cca15819-goog From nobody Wed Dec 17 21:28:16 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 From nobody Wed Dec 17 21:28:16 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 209E1205AA4; Fri, 6 Dec 2024 12:43:10 +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=1733488991; cv=none; b=uzaD8uO8Knb/eWMCQfD+jMvNg8grIc1H01ihjMuJYhfEi9BZP/+kO6EMmzjy5i/0zGO4zBRB6+i1LUKsYNzqx8gl6eUv7LEUAnVaxHygCLviYLrj/Jw2NtYs+VLc69VnjMMB1D/sI2lBx3gGwME1oWVAUc7M3hwlX+xq4fHn4ZQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733488991; c=relaxed/simple; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hhYvcbVFAZ2nSXCGhQPbHMnEsxEe1nx1mib+xNKGug6pqeufukMvTBCkL/eWt9fcsw9pP1DsZ2De1dWA2NIOSU7DHWUKm3fcnbL0HaYE5So/3Rt4XNHl3x7wKgIvg93XSFQ1qARP7TMC+GNfrZPJNQZsHMbc86inNyd08Fsn1SE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uUWI8MlY; 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="uUWI8MlY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 60902C4CEDE; Fri, 6 Dec 2024 12:43:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733488990; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uUWI8MlYsFA0UXCwm8yUHYPTk3+YpBbhNl7Vh1wI/2pcqRpMPkgcJ/LSMno42pqiV iXPCzmbWIPH7YmtbsUSvB9reopFyllrssS+2wA2oQgIzZz4BAA+s7AuXP2h8+IhO9P nS0jNq06FYTejopL75f/4ehR5Cci6vTyvHG194Y/ug/sFwjvJW7181YyuX9TdmY35j ljSSJwBqF0IuAK/bfaumxJ8HKmtNzLB6rtuXihknjiBT2S8s2XkxoYs7T0mMCooy5k fZMCDbfh0EOMrHAOe50IXluSZRJ4+lXWkUogKo0Ux3FFpRUOMO/L0lipbOLVLcW/sI qo5Ao3cM7MmnQ== 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 4/4] MAINTAINERS: Add Rust Misc Sample to MISC entry Date: Fri, 6 Dec 2024 12:42:15 +0000 Message-ID: <20241206124218.165880-5-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" Signed-off-by: Lee Jones --- MAINTAINERS | 1 + 1 file changed, 1 insertion(+) diff --git a/MAINTAINERS b/MAINTAINERS index 21f855fe468b..ea5f7c628235 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -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/ --=20 2.47.0.338.g60cca15819-goog