From nobody Thu Dec 18 00:14:22 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 EA76C222596; Thu, 5 Dec 2024 16:25:43 +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=1733415944; cv=none; b=dRTG3j73kR23xHeR6V7J4P2kkqChsVqS02SVfRJbB8xqTw+JSZ7NOlc2o2qjO+GKkQJkGQz+hquP37Z0y4akEWa62bHuGP1jAiGKlQRSJFF0VvLpQ1cB5nKcivdq+wIqVClz1hBI1y1lxDYJYWzoWapWXu1YlHO7aMjV2zYYizg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733415944; c=relaxed/simple; bh=RZlItOZezRowXOLbsBUld7wmGQAUobHGREmd6YYqaDI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=AVpq/wNJasMdWAoe25Rrwwl2+fak2P6l9Yx6M5kgi8CuW8//zjPXizdGbNmadnbAC9+AJp9gfNc0zz2clHEH4Y+izrSKGQ39BPsb8Z8SkxGJjhhrll2wy5c3u3sO0NZnbRpnuMBPwopxojjF7ixkVWUzyy1jj1erSIkGe4yqn/g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uX/o4hF0; 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="uX/o4hF0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B8A6EC4CED1; Thu, 5 Dec 2024 16:25:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733415943; bh=RZlItOZezRowXOLbsBUld7wmGQAUobHGREmd6YYqaDI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uX/o4hF0EYnzOMaBpy9lPRR3UJp46Uu6UF2GCVGgUQ9pu7p25jTk9ovNAJ/6g++pV 9J4VyQ5nauNcCpFSJ7ddFZ6+j0qm1IsOQTNSpNPot649yheLFIMKnG1TOG+m+larr7 MPycD2ZLx6TGxgorKizOQcn9wEvOFWfvvfnxH8pJl/Pp9n8FRw797JKaDMfzK3Jr2y P4xOLxI5U7FKwUO/bpe/BiuWvS1Z9bCOp6tiZ/BtJXYNxe989xvsRntxl9ZVqcojH0 U+impmyLCIsfx9Vfu55szyn9bbYmRCvraWHCqAE3EZ+eiqDyU7iSU4w6aGhb2f94dJ /rSn1eKYOzMqA== 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 1/5] rust: miscdevice: Provide accessor to pull out miscdevice::this_device Date: Thu, 5 Dec 2024 16:25:18 +0000 Message-ID: <20241205162531.1883859-2-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" There are situations where a pointer to a `struct device` will become necessary (e.g. for calling into dev_*() functions). This accessor allows callers to pull this out from the `struct miscdevice`. Signed-off-by: Lee Jones --- rust/kernel/miscdevice.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rust/kernel/miscdevice.rs b/rust/kernel/miscdevice.rs index 7e2a79b3ae26..55340f316006 100644 --- a/rust/kernel/miscdevice.rs +++ b/rust/kernel/miscdevice.rs @@ -10,11 +10,13 @@ =20 use crate::{ bindings, + device::Device, error::{to_result, Error, Result, VTABLE_DEFAULT_ERROR}, prelude::*, str::CStr, types::{ForeignOwnable, Opaque}, }; + use core::{ ffi::{c_int, c_long, c_uint, c_ulong}, marker::PhantomData, @@ -84,6 +86,13 @@ pub fn register(opts: MiscDeviceOptions) -> impl PinInit= { pub fn as_raw(&self) -> *mut bindings::miscdevice { self.inner.get() } + + /// Returns a pointer to the current Device + pub fn device(&self) -> &Device { + // SAFETY: This is only accessible after a successful register() w= hich always + // initialises this_device with a valid device. + unsafe { Device::as_ref((*self.as_raw()).this_device) } + } } =20 #[pinned_drop] --=20 2.47.0.338.g60cca15819-goog From nobody Thu Dec 18 00:14:22 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 3351A21D585; Thu, 5 Dec 2024 16:25: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=1733415947; cv=none; b=IHzOvAxaS5luTz38zuI7v1VV3ZTRmBQ7xR0A4DKIeDRdiatPjvKf86zTJJSBUC1DupLFc8Ah5y+HyzsHd+OloY3DxApdx4YjuM4rWryikeivwpxRkcMJMyMJ4fAchqH6dfLJ3R53slzyUUzvdoUYM14k+zzoorHaQpJcn3ev6Ww= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733415947; c=relaxed/simple; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CBsI8FLOw9hXdP8hnmsZr/7A/hRd9cZ7KmHeAmh4UxZVoCN6F2w5fs1fwtbKdCRNuIcZFpUaCLIaEvmhmOOYgAt8kWF2oGtfsqEjJUarFw5nCZ9X/YQi4zoycCt0uBkyjT+jKZPu0zqlcpi+M7qEWPgeuO9Bwo4klVJaSI8oysc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pXNVWB7F; 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="pXNVWB7F" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F1323C4CEDC; Thu, 5 Dec 2024 16:25:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733415946; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=pXNVWB7FljjxzxJGFWRyaESF3qeUWGZnSlD1aMqToGX03KiumzJH7WMrWLdgQunLe tbi5Sh4NLX92sFohPchh5JVrZONYjAqy7bpQrv3bXoOzy0YB+YWs14Ig8FsrT6/V3y UVoyXJF4HjSsnyxJ+8OtNBG7/VGgWPx3B0ZYhXGgrJJLKjAHlpsI2Wx4UrK4lHqLud /NsywIFdqr06OXGh30W4FdQ3BdFsxZnfyNFt6W0Nd7tGHj84ppKCKpkux82sahfUzi IH87V9tI9mA5JCCQRg51UmfodgckWheHN0mpX0s2MjvAN+y9GJhdnFw2Flt9J7CCOF TzIs1NglPD5YQ== 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 2/5] Documentation: ioctl-number: Carve out some identifiers for use by sample drivers Date: Thu, 5 Dec 2024 16:25:19 +0000 Message-ID: <20241205162531.1883859-3-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" 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 Thu Dec 18 00:14:22 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 63ABE221472; Thu, 5 Dec 2024 16:25:50 +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=1733415951; cv=none; b=jBKaUHZm9K0aO8/h3rpDiB2ONscV+nKMSs+vMkAlZiEmfF3OwZfrRoJV1EqztVwdexJU6d7r825hbOJhnqznnizKyZkpf2skcZ/wDiS1JGZcDoZ8nmq13qiv0d/DT8n0r0iwSXgeG4EQm6singH/XjxKkh2qRokoO2ExISz2v+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733415951; c=relaxed/simple; bh=3+8P2k2Anmh2tITJXydjYEGNUs7ATo3i9o4frL4TSpE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=D56uB3VLvEdT+C17IB0ChfubHRUzppcfcUsFNiUGuf/Rp7WLbBGvaNA2XfUPraKR1v3a5Pt9YM4De26UDxQYNSypPMLX+qL7VIRARid7uLIpvfvmFJsL/FM4Y/cnyfGt8Y9vETk1WbqzHBeGePXbUVZ3VrrWVXkua8BbbTu6Ato= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=TPmD4oi/; 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="TPmD4oi/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3875EC4CED1; Thu, 5 Dec 2024 16:25:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733415950; bh=3+8P2k2Anmh2tITJXydjYEGNUs7ATo3i9o4frL4TSpE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TPmD4oi/kGmSLyHwK5VgDYwcmbyBX1xxpZZ9/iH4CMzyRVLOxJLeePz45ZpjVv2gz XaHwKCztY0dN5wOufnGwxAx6wFeKQT/W6jyJegJ2Y1ZzS7TCJk4Mesf4wTfK/7ia81 7oM8nAVWqKIwJ+RTI7azXAGvsSQjYdDqwGxJXHwuAv7Pm1NitAW+h63R2i7Fclw36S uqJ3p0TYTDh3sf1NYtrbzuacoGxP/Bbp5maB4nq4gyCmtmicugFXUkwpho1CASHYcT yiHzlK+kpuVK3bRVYGOuApep20+uxzr+XkdEjB8DY6vAJdjg9+S3a9pCBSZ621ndRu lIJthvgllgPSA== 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 3/5] samples: rust: Provide example using the new Rust MiscDevice abstraction Date: Thu, 5 Dec 2024 16:25:20 +0000 Message-ID: <20241205162531.1883859-4-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" This sample driver demonstrates the following basic operations: * Register a Misc Device * Create /dev/rust-misc-device * Open the aforementioned character device * Operate on the character device via a simple ioctl() * Close the character device Signed-off-by: Lee Jones --- samples/rust/Kconfig | 10 ++++ samples/rust/Makefile | 1 + samples/rust/rust_misc_device.rs | 80 ++++++++++++++++++++++++++++++++ 3 files changed, 91 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..f50925713f1a --- /dev/null +++ b/samples/rust/rust_misc_device.rs @@ -0,0 +1,80 @@ +// 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('R' as u32, 9); + +module! { + type: RustMiscDeviceModule, + name: "rust_misc_device", + author: "Lee Jones", + description: "Rust misc device sample", + license: "GPL", +} + +struct RustMiscDeviceModule { + _miscdev: Pin>>, +} + +impl kernel::Module for RustMiscDeviceModule { + fn init(_module: &'static ThisModule) -> Result { + pr_info!("Initialising Rust Misc Device Sample\n"); + + let options =3D MiscDeviceOptions { + name: c_str!("rust-misc-device"), + }; + + Ok(Self { + _miscdev: KBox::pin_init( + MiscDeviceRegistration::::register(options= ), + GFP_KERNEL, + )?, + }) + } +} + +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(ENOIOCTLCMD); + } + } + + 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 Thu Dec 18 00:14:22 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 From nobody Thu Dec 18 00:14:22 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 DD6D0222572; Thu, 5 Dec 2024 16:25:56 +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=1733415957; cv=none; b=TgoBPUQ0F/vg7+0EZrmSYroliHdYAIKBT7UVlGJ58uJ9AXWvktQywXGf+rwBrscU5C9f/bF7gtmN3IRvcNwq9eWN/8vISLli7qbeKEStdGW0rCW8SWtcxBZ1Q3vyTi7GlYr8KTBE6FQxo0xvqtCKOuNUZVXg9uv5V4NhglTlQGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733415957; c=relaxed/simple; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=St+KMLeo4JaqogmEBHdc8ko5QQM16HJYxXG2cyky785FHfUgib/smQixCaFsbHWNOGg3V50+KicNsvrGh7KLlfU3qzADVF9caNfJtVmkHJaW5BTkx3mrKIRVoliJaJjDGqFF661HX+xsxpac/GLzlVbKgUuZyYbId8odRH11Fig= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rg3Md6Wq; 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="rg3Md6Wq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A76EEC4CED1; Thu, 5 Dec 2024 16:25:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733415956; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=rg3Md6Wqgrck2Aa0v0T/A2Y6HG5LMkNvckOQ1GlCU5vRr/0wQCL+MeNlpNfziLRPs F+bJWqXGS4EQ3/7hTrvtZnZ2Mqw/imoSoXjMnWHJVrl474+6B9cDasQSHRalfWNYbo Drghal833tzMVh01ByBtwp9ySKnAYNfUHmRNth29W+r58Wy79NuwUwZ5GuvuSqX0r+ etJNjHLCLLFSEuYooJk245TtZnU0NDePwH55I/4Z3QGVTOKPV7aTjM9ZNupJSYiOTA THhJ5Oye1srBaG+4qtNmjIYw9lIbhZBsZT8m5k2BySoDm9hqvSN9iuPH5/fy/Tx3t/ ZAFgP++DjVFAA== 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 5/5] MAINTAINERS: Add Rust Misc Sample to MISC entry Date: Thu, 5 Dec 2024 16:25:22 +0000 Message-ID: <20241205162531.1883859-6-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" 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