From nobody Tue Dec 16 00:27:53 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 2CD731E0DB3; Fri, 13 Dec 2024 13:47:42 +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=1734097663; cv=none; b=Dq8QndV4MxLzQ9Xg1VzpNMImZTbifCstkveTXRFTMPpFESZxemraIuRzxV7EU7ZGZpO8VpEZTk9Tj43bHrxMmwdV0YrEH1bbFsUxlGdfrjrlbEwjLn95Ti/3/Iyg4mkOvJgCq+06yyty9tjMR7dL4LmUoWqr/5BLWtLGOGk6s4s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734097663; c=relaxed/simple; bh=lKocUKpsjHqNc6JQsO+YrfIuMd0JA6GMV0cGR4aKsRI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=XPM4NaYSilKHAhgH3DI90q2qQCqEpfQn2n3PdKvlm2l+D6KbUaH0MVq2A7Sx8RS0ayslO+qPMqizhzqiSgcWx5VlpQ6Sd497O6K67voMFgiyRLXhjnvDZcnYUMbV6gf+NVvOTCVmdfjtbM7xj3m5q1BQIw7t0RASn6ZOP0BygA4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KgalqpOT; 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="KgalqpOT" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03630C4CEDD; Fri, 13 Dec 2024 13:47:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734097662; bh=lKocUKpsjHqNc6JQsO+YrfIuMd0JA6GMV0cGR4aKsRI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KgalqpOTqHFi+Rt8yzztUOjfblqEJeTIsaRF5FcImV7KvPmuu6VOFqZQbJqsS+C/6 Cwpk+E26A0SL63/z6/GjWUmA5c0lHjmxWxdNNEXlQf+kHf2cCybGps3ZdBgNRuhSxM 9hBHTIf3R51XVFnHD/vgy+PgChzwk1rRSisTZn6g/ESuA67yyFdx3xO+HnNl6IaGhl Bf/raSkGlK5wsfg5yPWjWgh2vEXfcDxSzMzGbF4PFEMxUQlkTd3EXsdbOHD9oSFRjW fuC2DmKmG1l/CFs7y3XrCILgufCJzN5z9HQh2aAJvdUat7uepMZ5GJHlTsnAey2WKf 3e+6kTo2I/I/w== 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 v6 1/5] Documentation: ioctl-number: Carve out some identifiers for use by sample drivers Date: Fri, 13 Dec 2024 13:47:06 +0000 Message-ID: <20241213134715.601415-2-lee@kernel.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241213134715.601415-1-lee@kernel.org> References: <20241213134715.601415-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.1.613.gc27f4b7a9f-goog From nobody Tue Dec 16 00:27:53 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 6B51A1E0DB3; Fri, 13 Dec 2024 13:47:46 +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=1734097666; cv=none; b=dJkEJDpfW4H5XCAzm8g+iA3L3J/JSVCCY97/bLmohFHSYuMNomuVtzc08542768tekcpLaksc9qsg/mCXFR1/iGVwZWuvd6b6jInBXl7D2dE41ueEBvW4O2L+JqZKA/LdKYY557IMhEWLOgsQ/+a+3w3GweUvyYMgUWdgdP7s4I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734097666; c=relaxed/simple; bh=FUDuPCGiIzTe7+Z+u/MBEryhDck/6gCbdCtn7XdUV+o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kFrJJ2VVoR0w9ND4Yc0r5kmfI5pJ1oqzK4mhU6nOaTQ+cFOLm23swT7fN5MzohHHzTt0kLNqo7XVnKd28d8+MUYL4EFsmOkKUKZz6Y2rPDfA1tF9mKM9lBFlWfMvcpg8PbfUQUeXsaq8gMNZItVg7sKO6ir8jEoEUD9HI1K8V/A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=t5KUZx7b; 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="t5KUZx7b" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3B5B8C4CED0; Fri, 13 Dec 2024 13:47:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734097666; bh=FUDuPCGiIzTe7+Z+u/MBEryhDck/6gCbdCtn7XdUV+o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=t5KUZx7bDD+J0+axWwiOWGixA4kgDsoo0+oFdn/KH0Zy5OW6f8PAZzGPpOgbMvWKf r39Tb/4/P3VcEvgYXNYuE/61FLHN5MyMjmlZxRehlWvUSARi5lWMgB8NkbUPeDTjDA 3S4O6EfkbFBYiNcejxs0uXVOpy81u2fWpP07JPiwEv43kr/u29T9kG8WwVhGjsX2qy WFDYAPSjT1ouHA24ALekobuyxtWpabW2TiSqt8CYXYAmFfh076w/1nkQan46yuS04o BNi8ydaOIWK5P6+fAV/0kin1XrmgbHJWs+RrUMZwjGa2E5htFHx4gfNUnXQfzD7/NP uD/GWJnpnXt6Q== 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 v6 2/5] samples: rust: Provide example using the new Rust MiscDevice abstraction Date: Fri, 13 Dec 2024 13:47:07 +0000 Message-ID: <20241213134715.601415-3-lee@kernel.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241213134715.601415-1-lee@kernel.org> References: <20241213134715.601415-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 Reviewed-by: Danilo Krummrich --- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 87 ++++++++++++++++++++++++++++++++ 3 files changed, 98 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..324c3696ae3f --- /dev/null +++ b/samples/rust/rust_misc_device.rs @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0 + +// Copyright (C) 2024 Google LLC. + +//! Rust misc device sample. + +use kernel::{ + c_str, + device::Device, + fs::File, + ioctl::_IO, + miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, + prelude::*, + types::ARef, +}; + +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 { + dev: ARef, +} + +#[vtable] +impl MiscDevice for RustMiscDevice { + type Ptr =3D KBox; + + fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result> { + let dev =3D ARef::from(misc.device()); + + dev_info!(dev, "Opening Rust Misc Device Sample\n"); + + Ok(KBox::new(RustMiscDevice { dev }, GFP_KERNEL)?) + } + + fn ioctl( + me: ::Borrowed<'_>, + _file: &File, + cmd: u32, + _arg: usize, + ) -> Result { + dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n"); + + match cmd { + RUST_MISC_DEV_HELLO =3D> dev_info!(me.dev, "Hello from the Rus= t Misc Device\n"), + _ =3D> { + dev_err!(me.dev, "-> IOCTL not recognised: {}\n", cmd); + return Err(ENOTTY); + } + } + + Ok(0) + } +} + +impl Drop for RustMiscDevice { + fn drop(&mut self) { + dev_info!(self.dev, "Exiting the Rust Misc Device Sample\n"); + } +} --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Tue Dec 16 00:27:53 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 AF87B1E0DB3; Fri, 13 Dec 2024 13:47:49 +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=1734097669; cv=none; b=kpYHXzoKTD3l4GVzUeS+q0J7ci+Yq+cpPHizwrWCy8cGk/B4pNqa2urO2WRwIErobV4+ZG2jGBF3u/nu/G916WL2Izx0JdMCGwA4ocikPbExLbfWsyWf3HyAIN5J1O6aeFX0eOU/TLrKhFXtsrZnjJjXxdLAJliR//w3uDgzQIg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734097669; c=relaxed/simple; bh=gMScZr/PHM22TpsoMdi7V9GuENoPSrtgECRKdQYPiHE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eU8XeugdLovNUnidFiVfDEK0aHpt4jgfuPdTaYlEaBolp+2D8XJFY93PfkYg84GdCG58RGevjGBhvj8NBelCbIVJ35+76HgMqb57WH+SPQlXO4eyfQzXwx6JcD4o8734SBvVHduMy0uvfzWpKNQTUbpsOJULDFJ3aUJzNmwDtKw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=IRb47Kr9; 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="IRb47Kr9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 697AFC4CED2; Fri, 13 Dec 2024 13:47:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734097669; bh=gMScZr/PHM22TpsoMdi7V9GuENoPSrtgECRKdQYPiHE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IRb47Kr92U9I0DHpXrcp8QM8yHnfHguglLEQSHkoFsACwjW+Uype45Ux+n29GOW8U ixfO5Bu3g9gbrHPXCZ5260QGaVEblML03iHdoPkompmWBMJXkYA8tlPH+lQkhwLvJJ WUfxopokhP6hNSlH/zLufaaU5Ns5W0sNJDgA0j1cSCFvkof1qfq2hQLneihckxbWD7 7KlwTaSYQY65TNzQe/iJYZqYxeDoiYTopnFHCyBHxHtc/HJcHxumYeYgXLlHciTsxF Q0KZIdhBhgTv8Imo4WWvUWEoJpE0L1W5sjoYgaqjBqufzsx8kc61pH9tE5LgIEHCpF +5UtfnC4dAM0A== 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 v6 3/5] samples: rust_misc_device: Demonstrate additional get/set value functionality Date: Fri, 13 Dec 2024 13:47:08 +0000 Message-ID: <20241213134715.601415-4-lee@kernel.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241213134715.601415-1-lee@kernel.org> References: <20241213134715.601415-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. Signed-off-by: Lee Jones --- samples/rust/rust_misc_device.rs | 89 +++++++++++++++++++++++++++----- 1 file changed, 75 insertions(+), 14 deletions(-) diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_devi= ce.rs index 324c3696ae3f..ae1474a451f1 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -4,17 +4,24 @@ =20 //! Rust misc device sample. =20 +use core::pin::Pin; + use kernel::{ c_str, device::Device, fs::File, - ioctl::_IO, + ioctl::{_IO, _IOC_SIZE, _IOR, _IOW}, miscdevice::{MiscDevice, MiscDeviceOptions, MiscDeviceRegistration}, + new_mutex, prelude::*, + sync::Mutex, types::ARef, + 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, @@ -44,44 +51,98 @@ fn init(_module: &'static ThisModule) -> impl PinInit { } } =20 +struct Inner { + value: i32, +} + +#[pin_data(PinnedDrop)] struct RustMiscDevice { + #[pin] + inner: Mutex, dev: ARef, } =20 #[vtable] impl MiscDevice for RustMiscDevice { - type Ptr =3D KBox; + type Ptr =3D Pin>; =20 - fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result> { + fn open(_file: &File, misc: &MiscDeviceRegistration) -> Result>> { let dev =3D ARef::from(misc.device()); =20 dev_info!(dev, "Opening Rust Misc Device Sample\n"); =20 - Ok(KBox::new(RustMiscDevice { dev }, GFP_KERNEL)?) + KBox::try_pin_init( + try_pin_init! { + RustMiscDevice { + inner <- new_mutex!( Inner{ value: 0_i32 } ), + dev: dev, + } + }, + GFP_KERNEL, + ) } =20 - fn ioctl( - me: ::Borrowed<'_>, - _file: &File, - cmd: u32, - _arg: usize, - ) -> Result { + fn ioctl(me: Pin<&RustMiscDevice>, _file: &File, cmd: u32, arg: usize)= -> Result { dev_info!(me.dev, "IOCTLing Rust Misc Device Sample\n"); =20 + let size =3D _IOC_SIZE(cmd); + match cmd { - RUST_MISC_DEV_HELLO =3D> dev_info!(me.dev, "Hello from the Rus= t Misc Device\n"), + RUST_MISC_DEV_GET_VALUE =3D> me.get_value(UserSlice::new(arg, = size).writer())?, + RUST_MISC_DEV_SET_VALUE =3D> me.set_value(UserSlice::new(arg, = size).reader())?, + RUST_MISC_DEV_HELLO =3D> me.hello()?, _ =3D> { dev_err!(me.dev, "-> 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>) { dev_info!(self.dev, "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(); + + dev_info!( + self.dev, + "-> 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; + + // Free-up the lock and use our locally cached instance from here + drop(guard); + + dev_info!( + self.dev, + "-> Copying data to userspace (value: {})\n", + &value + ); + + writer.write::(&value)?; + Ok(0) + } + + fn hello(&self) -> Result { + dev_info!(self.dev, "-> Hello from the Rust Misc Device\n"); + + Ok(0) + } +} --=20 2.47.1.613.gc27f4b7a9f-goog From nobody Tue Dec 16 00:27:53 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 D9FC61E1C07; Fri, 13 Dec 2024 13:47:52 +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=1734097672; cv=none; b=IoYa8HN3/WdZEjdTisbY77Udvvc+denNtThsx2YIbJxctxsRqdtEsWEWTzO2J9Hnsa+rtlzl+ZMjL8AfnObsYoQSZWGHYSJfPNgUyNjY5UCpJzuC+WQbr6oQlEpxU/v+DvMcON+MJujMWDC6kWZ256rsjwO8yiwabiTB6km2/yI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734097672; c=relaxed/simple; bh=AkmQzt/HTDug1i0Rek37N4DXQKlYX4XxoSJ0WPd9308=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NCrpcYPMOgs42MF92yFsE6nCZtdpHAPCQe0JBOdR16mfZkNzSiSVuqn6YO7Le1bQ+2lgMqO+fwpQpMKqCEKddSfReWpUwL5JllLDLt50HNiewCQtMSaLYeGJQlb1756oNtdQn0x6YzF4Pky1lxNutagIha9erxjXXOdLh9zULb4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=owNrjFry; 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="owNrjFry" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9357AC4CEDF; Fri, 13 Dec 2024 13:47:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734097672; bh=AkmQzt/HTDug1i0Rek37N4DXQKlYX4XxoSJ0WPd9308=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=owNrjFry+lFTn+lM0m1lttmGKjXIS4O9yPAwrnT6DGLwl+5A642fqqdVxEPJDXhqV R/9/i+kZo6MrIk6aiifD+nKVAHM3pzwmcNZi8R9zXrWQ223eSW9OrzItbUOKxE6y9n qkKA+XQ6Ot09ub4VmUL4dc0HI1vRruCXkm4zkBmrUd0ne0a9cBsXt6xJk1EJyiiLHP gPFyKYHtTjmaKTgZXHrYLpkNoWxc4uEOB9P1yTY3nQCci3lQNGWpwkq+HR/gI8LCy/ J2p2qY+nP499nrqgcfBRMsgCP7P1kLwjyrB8Ub3c31m2/sBr/r3pTCQiezd32TyUNu IGNKuet3eHAxA== 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 v6 4/5] MAINTAINERS: Add Rust Misc Sample to MISC entry Date: Fri, 13 Dec 2024 13:47:09 +0000 Message-ID: <20241213134715.601415-5-lee@kernel.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241213134715.601415-1-lee@kernel.org> References: <20241213134715.601415-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 81348dbce8ca..d3e3b20e0376 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -5346,6 +5346,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.1.613.gc27f4b7a9f-goog From nobody Tue Dec 16 00:27:53 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 1320C1E1C07; Fri, 13 Dec 2024 13:47:55 +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=1734097676; cv=none; b=enAPHqps1q8AkekEeH0Fd0hBNTkXElmGRkpH+XQcouvCiHwL7H54Mb3pWijzoai2hzkDvJa4ToLUTtIb0LGt5lyeZqNdVcES8OX9hOMtBJR/RSp3/p3Q0zcj8voq64wXOtZlE3f8VxKVrPSDhJuJEIt3HfQTzCgKX9n//NvKW74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734097676; c=relaxed/simple; bh=2Cpd5nmMdpE1QJyo7HkmRlJyt5ZRsTgDLqFZwGiXn2M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=R4m0vGGvVdkNvyE7GpH1vSrC+1FVjB2xTlOFEwO2cwFe7phqNzaIEJnjejPvgeXj3k2n5WqEfKQ2NRFBYdZL6zkoLQlpJa1i+1EsWHGfSq9OP6NG8ZlzIrlhqXEE0kLAS57P4KU8tppXooBL2xuw30XneFg3jALe9a1cpoNMUtg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Z1yQALhO; 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="Z1yQALhO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CB6CBC4CEDE; Fri, 13 Dec 2024 13:47:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1734097675; bh=2Cpd5nmMdpE1QJyo7HkmRlJyt5ZRsTgDLqFZwGiXn2M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z1yQALhOOLQ8LMgGi1jrflJxFbg3+XxKnUOMVWKz3aWRZPrHeCFUR93YW4d1XFVLZ rX0X5SLIZDBt0K5r5HJVe+RNE0Jivaes42dRSW0wp7p4vDJc6j1o/BEVpkEpMCTPFs n/J3upUp8HWnq44WpFEWC3Go66Ztkcc9uAD+LiwkYiA8lWJVjBMv2C0Vwo64sM5MhK TKnwuwhy4n1wLnl/c1ScTORP94i3x4pKK5+RNohMNDlZpG3CjkYZgOpY6iCsDiuhHa TpC66GbNvWofEmQNdJitUdvV1wnbstkxqmkZroGTklH/KRfcopiN+z4Qtt71NVyVpS zzXcxLo3IaFTA== 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 v6 5/5] samples: rust_misc_device: Provide an example C program to exercise functionality Date: Fri, 13 Dec 2024 13:47:10 +0000 Message-ID: <20241213134715.601415-6-lee@kernel.org> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog In-Reply-To: <20241213134715.601415-1-lee@kernel.org> References: <20241213134715.601415-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" Here is the expected 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 | 90 ++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/samples/rust/rust_misc_device.rs b/samples/rust/rust_misc_devi= ce.rs index ae1474a451f1..40ad7266c225 100644 --- a/samples/rust/rust_misc_device.rs +++ b/samples/rust/rust_misc_device.rs @@ -4,6 +4,96 @@ =20 //! Rust misc device sample. =20 +/// Below is an example userspace C program that exercises this sample's f= unctionality. +/// +/// ```c +/// #include +/// #include +/// #include +/// #include +/// #include +/// #include +/// +/// #define RUST_MISC_DEV_FAIL _IO('|', 0) +/// #define RUST_MISC_DEV_HELLO _IO('|', 0x80) +/// #define RUST_MISC_DEV_GET_VALUE _IOR('|', 0x81, int) +/// #define RUST_MISC_DEV_SET_VALUE _IOW('|', 0x82, int) +/// +/// 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; +/// } +/// ``` use core::pin::Pin; =20 use kernel::{ --=20 2.47.1.613.gc27f4b7a9f-goog