From nobody Thu Dec 18 00:05: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 90B9C1DE8B7; Fri, 6 Dec 2024 09:05:34 +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=1733475935; cv=none; b=T9Nlp5TB+SJTL1TxYiesErK+2AmPfFUd80LKjQKcRmWt39sSYzbCv6snSslR8R3GitcdnyyxLPMydOE0VllMvSu828xbM8NyeQ13aYlUZzNY+pOUkIovzJKLVuJpINq3uw9mmzbQN2GF202NsAGYjy1w3M7100+eUfA7qDCTvXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475935; c=relaxed/simple; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RZWGRcOZ/6HkED7IWDQpmeNtuseOKo1D7PuCrkCS8HAZ7LOejQ5+Qn6zb8lEOyKscy9EIs+TORJu+Lo1/+tm8J/JsgC0vapTU3Zs93KbdhqSyZ+1DovNRjmqpWZy3h5Ktuo8tD4NIlptzl5CyTRQFv2YJv8IZ27fUnLMCaFVKB0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nxIAu4kP; 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="nxIAu4kP" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BE6EC4CEDD; Fri, 6 Dec 2024 09:05:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475934; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nxIAu4kPguSdYqB8VqbnjCCcCYv9b1Ig2tCGogUUuV+gj2S8b3IAUaAzFRbrSe+me 52TEHYUGp6wvTK3lQA9VXuwuUcy7BoDqCNpXm6G+nohza7x8eDqDVKKck4DFBgz3be 0IO3dDALWxg3dZMYXYx/cj5rZvdFmEVxRGs9IrpHs9U3MGSbOeATovXZItMAsacE3I gFL3A4VASnCW7dOrPWVGkU7pnCe5L1hLIT7260rVA89RLMDrgGLRjsA4NBXn2wc3Jq aHbk8uWO3nDd5oX9ZqbqCvAqDd+a/vsZM5ssyi0+iBj6U45069B5t+/jcaS3sJLY2w fYn8YtaGKRheA== 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 v4 1/4] Documentation: ioctl-number: Carve out some identifiers for use by sample drivers Date: Fri, 6 Dec 2024 09:05:03 +0000 Message-ID: <20241206090515.752267-2-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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:05: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 B17521EF097; Fri, 6 Dec 2024 09:05:37 +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=1733475937; cv=none; b=Qm3UYO0uw8QSbXGHpzZzPmfM1a5TsIhLx2+9UFGFoRpuxKllVazTT+7BrvVTpRjO+B2tkgtpGEC7qxwoOoFQ6qXPQVCPKKw8t5AcFttb4QSFvL1xPsu5DkiyunF+k376U4EBxw0qHh4Ha5sCgkZ9K/iYdfCNS90vHUyuw9HwECw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475937; c=relaxed/simple; bh=RZlItOZezRowXOLbsBUld7wmGQAUobHGREmd6YYqaDI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BMYBG1P74KfpmCMfRbsxKQrdf2VLUedErSiQ5WcfOoa0cMy4HbF0DOBX72A6o3K/rnevptBJZDUc+ovmoBw3tNTa8GbnzWfPb8ong51d0wCk+cnvl6Vx6PIhPm3sBFoI45LrOr8CcPeVNfyEWdvVmFn5TPruAJzsnX+NzAjr8t0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qeDAikPS; 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="qeDAikPS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 81986C4CED1; Fri, 6 Dec 2024 09:05:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475937; bh=RZlItOZezRowXOLbsBUld7wmGQAUobHGREmd6YYqaDI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qeDAikPSlCiDr6NEVNvzPn4isnpWs57s0DFGtEbum25hLbCv8nWTt5MhqZTPfM9qN hZx3GHCVdSCWVEU3AFbupBaYgfz8g/qpBC6+9ThJvsPvrcVXKL35/3YVnQez3p1NWk 56tSWJ8pFU1vVY9tu4jJqtV1LpOf9BVxeecJLHfvIZPk2fULZogBnn+k9eYM2aZMJ8 p6VyzrNElRiLh91Q0Mt8YPLuKUhc9XerQaIfibF90bsLBs5ewyhjolvRobt+RF9vU7 8Ow7g2Z0qbJ7zSEQx4i8+DC0xhO9jGJROeq5Lej7e6lvADkgOmgIK6FM2Wg9ffmYWl KKgpNP9vbJtzw== 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: Fri, 6 Dec 2024 09:05:04 +0000 Message-ID: <20241206090515.752267-3-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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:05: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 C82CB1F5403; Fri, 6 Dec 2024 09:05:40 +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=1733475940; cv=none; b=g6+/rE0lhEE2PfiDOxManDQ8l+XPQ/1aLsdM/o9SUDvEp7MaZ37eB+dC7D32mpvO/CNTFxbvY950Y+H/RSWWUd1irCUJPcjLjZ3j4GG+yj5piENjuzePYcWGe7Ti3XpitOS8z279U4R/wb00hiXbUsu+/upqGsvdTd4uCsEaA+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475940; c=relaxed/simple; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=htz6+3f+Igyc4JkRMO+KrqyEbmjacMPMhRm0c1ROLD9ZuIGPhqRvFcvJnRCsB1xCPltPc5rO70oGGUzjCIFM73Zl9zwNkz9vF8V5wl4b/4AjhN3FNmOXmXZCayM2shj5SXX/jOdFzOrq+AqHPHGr8vDhb0oWfRiFFqcH/vNlhXQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DT3H8cV3; 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="DT3H8cV3" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A40DDC4CEDF; Fri, 6 Dec 2024 09:05:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475940; bh=22NZyA5YVkMQ03pXUXXdocb9WXBGnORT9AlGZhxewK4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DT3H8cV3Moh7Exfxq4M978tHqv6qDn/2afPk84yQLdD7ypRQDppTKPcj7rOABjvjI mXYQAPUNe5lJWLZV8bsoZ1GlgJAD6cbDdVKHW46fwRlXqbgEjfEBwN0oxGYmlpIlOm UQMVCK/Of9JIMa2CNuoEqQxqGSbMeLGcUxLcJwOSQN3s07TSdMvr5LmENOpcFNTCcC W9peqv9HfqnHOTOLbRJXyTRIp2eZyDGqkCJxC5p7hj/GA4ZKlAcCmk4oQ88lAGweGq cipHGYhTPzg/SMhPCG+iKXukLYiowptznwKvsrpnSfi+6VG8u/NrOTlGWsOl2138Ub TC0AfyJA5oW6g== 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: Fri, 6 Dec 2024 09:05:05 +0000 Message-ID: <20241206090515.752267-4-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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:05: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 A13001F8AFD; Fri, 6 Dec 2024 09:05: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=1733475943; cv=none; b=KMVSS+nj129Y7OL+8nnTgUPEGZrFOBr2jaz/n3LzuyFs+MGyX81r+uvnaf0yHVtMlz4ouDzJ1uGOXzoxlYNDmutiskXyicA7U0s+myacE9rjns460C8negEQvE9PGG/2ct3BVMGWXbCAFy6Uc4ClOvbM53b/FCJfZ1N3OsoH8FA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475943; c=relaxed/simple; bh=/vGNwkuzJwrok7OwiHWU6wXKpr+MGQsYZDS7oxldDSE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vbw/+4Nrjl6PgxAfZBgejrNhvUr5wOSLrZo6jgZhjG0CNgTieFsWiEn0/MmL2waCA+qYvR+zPIm5yRbsxru2SsQn/34WT9E9UmFqIXXaKw1mfuD02HzNQTKyj0/UzENEqw4FBu/1yJyaZ/cs077k4JZ3z2aTkPq+gozdQ4evqH8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZNT1yTKh; 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="ZNT1yTKh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C40A7C4CEDD; Fri, 6 Dec 2024 09:05:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475943; bh=/vGNwkuzJwrok7OwiHWU6wXKpr+MGQsYZDS7oxldDSE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ZNT1yTKhmoK5Bln6Fk9kNGFSKx562INp9fUkT10gscUkP7ILGKB5OcizGbloWxSbZ km5ZkEzFdx/1RrTGBZ118hIdEYl4BgD44O97P6lYp6YGqgmGX0fDUPo0RrOJOE0E77 JjS1TEWt7ah3iN78hPfqYs29DHW8e8TSsUeUAqe/xZ7FrBxwTIhnO9YBAdgMkUcksE RRdjfZDcM69q7uBzbqyg7+b3lGEtWR2jt+qYZCHoScrRCRopENrq1HGZhHLTXDrAtT bWnzFKiV6Gi7+FbxP6krNlnjWsxT4SYeAoJ4KE5bom28ytcVmPvPqnC0L1AE1MisvE KEYG9gRfp36cA== 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 v4 2/4] samples: rust: Provide example using the new Rust MiscDevice abstraction Date: Fri, 6 Dec 2024 09:05:06 +0000 Message-ID: <20241206090515.752267-5-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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 | 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..3837532d259e --- /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('|' 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(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 Thu Dec 18 00:05: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 0255122315; Fri, 6 Dec 2024 09:05: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=1733475947; cv=none; b=eOzhu5nScWKWtz6zYdKxntpUgvJClAgJNBtt0abR7svuulxtZ8kI7wD+WPk4DeQxJhaF83MVfvuMFuv4yNiSRf2f00E1A9E7d8MFlxXG1Il5HXFUD0cfYlvl0hfle3AYH7Wb7+ugT3w9Rnh+WUC4UDLRLxRdxOaTWp64ZHJrE+g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475947; c=relaxed/simple; bh=z8ZHUz6kwnp2IDcmZZ1KEAfTtpu9BEoMiALS4JhpqeQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=agvdDYrc+I6G5/3NVZ28Yz43OFhSCXdf0PCjUNnIWN/8nPNXTp4xv1p15kJGnAwvBxTdj3XYCI1kW9cnWZR81uGCUjGNyUF77Ta4ajOYqRBUqGCDG/Rkmm6mTo7vKUIEOz7+z5MhiawIuMSAuHAep5PjUbPomTcqRRq/Np1gToM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E/SdWkeJ; 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="E/SdWkeJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9704C4CEDE; Fri, 6 Dec 2024 09:05:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475946; bh=z8ZHUz6kwnp2IDcmZZ1KEAfTtpu9BEoMiALS4JhpqeQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=E/SdWkeJsffV/GLow5WUql0MRSsP/p7rVqQJF3M1yu64delUACGbkgwSyhNDiE4mv NLE8xRHIMebHAxeqPqkOFrN7K4AjBc3MRvV7echU71z0azYo+ez5WmDteaKh9nrywN sx3FlDsLNxiDwI0x51GI/xH7ixNU4StN+y2CndR5ZUejx4TgY871/Bs48d6MBpWqHI U2LnMVtkIaKocIsAIYtlzKTRJl9urDzQsryYNsiZHHYiOvjGsltgYu0vgxE+p910gv xD911AaLWVCgTpCc0hjKR2znpcfv/9+u/PEkQxq4jvfqkjUKNlNP9TCtYVv3O7QKOA WGdUhhCJCCUrQ== 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 v4 3/4] sample: rust_misc_device: Demonstrate additional get/set value functionality Date: Fri, 6 Dec 2024 09:05:07 +0000 Message-ID: <20241206090515.752267-6-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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. #include #include #include #include #include #include #define RUST_MISC_DEV_FAIL _IO('|', 0) #define RUST_MISC_DEV_GET_VALUE _IOR('|', 7, int) #define RUST_MISC_DEV_SET_VALUE _IOW('|', 8, int) #define RUST_MISC_DEV_HELLO _IO('|', 9) 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 3837532d259e..02ef8780804e 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::('|' as u32, 7); +const RUST_MISC_DEV_SET_VALUE: u32 =3D _IOW::('|' as u32, 8); const RUST_MISC_DEV_HELLO: u32 =3D _IO('|' 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(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 Thu Dec 18 00:05: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 4D11B1E0E0A; Fri, 6 Dec 2024 09:05: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=1733475950; cv=none; b=T+SgL4jnbzhTD3VWXP+pCZXgKiEMq1WwgiSIcxpWktN0BFVqDclTwk38VfJcRnzWJZJCtJuC43UOYs/ggNI1ty29DaKg2PhIY9LNCukEB4Wp8vd/FFAbZtRmNdvXtXz2RC0+T7g7mOLSOb4wto1UnQED0r47hqsAghmrmZy/tXE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475950; c=relaxed/simple; bh=3+8P2k2Anmh2tITJXydjYEGNUs7ATo3i9o4frL4TSpE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=q1dm6q0OjkkWRG8Czul0XLArgqIipKFSHUTrvHBszOIF6Sk7u1WoAIfXp1s1/MVnxj0dWtonogzsYkckZOQTDqe50Jdlel8DPAby0m81Y36YmUlZZtlrviRx9AzxlTXXgJL1xgCMdXkG2nuKWbH8HYn8XNAeOX0pq/KqDTMYXoY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=X2kykV80; 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="X2kykV80" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 17CE7C4CED1; Fri, 6 Dec 2024 09:05:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475949; bh=3+8P2k2Anmh2tITJXydjYEGNUs7ATo3i9o4frL4TSpE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=X2kykV80FixJDIa9AVs9CioCuCIQdPs1hrsOzYxXVVTylkHFcSbv41/p2n8FKbaYA GPGxQ03zJu0b5cHX9dhNRGSNBHOdj5Jw9yTmfnXfstUIQRy8NyRL7mupBbc6+ndIyA tXO/1Vku21Me7zIuRD8/+7CCv2dBDYGh18vpCaAkwu0/pFVd2QnA4yG86jAYyMCUNt bKCi8MhM2YougfI0k/QJNjZUSGuRWfbcZxYw/R/etJksUpNoo3xcPRD+3oF5IXltPl KX00B1fwFCdT68q4FXQKccFWUrjmI82Rip/uZmpG1jTmyZlmiIUF9bulFe073sNEbC A8FR8x049lhIw== 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: Fri, 6 Dec 2024 09:05:08 +0000 Message-ID: <20241206090515.752267-7-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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:05: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 8131F1FAC43; Fri, 6 Dec 2024 09:05: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=1733475953; cv=none; b=SPpG6CCaTozli9/6pic7lhuvlj+YQk6b902ZhpieHZ2wVCxg+Eu7s6FKdwTlfNwg0oFY7mQJnCcunI6B7dF5RzjP2XY4o8WrNOsLcgSvWZtH7TPKedg6oXaCGrCb8PDwmqKNKSA3JR42pC97LYlsNIIX62xgv+NADPTywJ1wspI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475953; c=relaxed/simple; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JaHHIydpYAKqF92SUo1Z3EHk3LEBfVmXFZMilY9AmUyHkSbMZvrLUBsQOk8mxlPIibUzbCA5KUMiY3p/VY8LzZ2TEuZN7+x+p3WUKrMnXPp4dFNI4AY09u4E7vTMeBT69KT9L8tdxHBPF6Bw9T3zIgfETm2B8NPMOlH7f/3QIyo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A62aXE+B; 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="A62aXE+B" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E2D5C4CEDD; Fri, 6 Dec 2024 09:05:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475953; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A62aXE+B80Z0Viqf+w93a8HrV+vnBLw+y9iTBXifCB3CxbmNi/4/yzI6zAH+OONUU zW0H5uhlD3tBEopUB4fFWUaxgriM4DDtbAjffhK6OuK3EsIPPPnSLhBjHdNoboC8wp hCWKyYWB2DcJikwVPNQLGrO81DOiHR8U26Cs29ZKwEfQvxs+hUfjvtOQGN2kEK4Zh2 UqTqz+DMmFD2W/H9rE6RUk+JG+dhXj0/BIk6Td+fnVVqTxlRgFvL5niWdododbe7a1 0TZZI2qoGNHfy3lzKoMPrczC21ow5eK35YP6Z7KTeXpopuf2p0GduGZkNVmmoSLW1l 1v01Izkt3RLpg== 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 v4 4/4] MAINTAINERS: Add Rust Misc Sample to MISC entry Date: Fri, 6 Dec 2024 09:05:09 +0000 Message-ID: <20241206090515.752267-8-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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 From nobody Thu Dec 18 00:05: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 3F5B01FBC80; Fri, 6 Dec 2024 09:05: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=1733475956; cv=none; b=dlrQKFUJrjCXxVNo/+YF4/oG6VrrgriPkjU8zvjUzpTNKWaQ+68trIbBwwhil7Sx+FSbpL56wkmE4+HFJB+8rll0QskIZIZOrGcJRsCxKmwwsbKjXEWXqpatZgu+5OgzoM5EDURWNqZokmOVf7FiStIbJxkfyG8BPHrPtm3Cq5I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475956; c=relaxed/simple; bh=rNO/zBPeJJmilX3vGIpUePq0l+fx97aSIbCzfNre5kg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=U7M7ecIgB47Ma+5IZ7sX9gGf0BT8SEThdUxZnBil5KMKym66sEyv8WtxSgB/qZRoyI4dBk9i0BuoN5bTRoJFFswH60ksN2TNrFFL5fJHzo//26ZIunCPvh4rb9i1PXZVuMc+5/duCVYBjpnkbGQoxGSkUcFbtb03Ucnz6Ii4WQA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mREq4yEd; 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="mREq4yEd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69105C4CEDE; Fri, 6 Dec 2024 09:05:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475956; bh=rNO/zBPeJJmilX3vGIpUePq0l+fx97aSIbCzfNre5kg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mREq4yEdfHqEN6zKqeooEMYrUEcObYgI3HeCjjDf0sS+8RdDWnFDDYSNsRSXSstYX JtNVeGV2o6WBZmmo/3jWonGKsTE/r7p0BeCCXJpYiJYAwW/AWg7LNrhF4BKuGUTW+f n124SmQegzdKwj3VbCjcZWc+vz1a060BvUrmOr6n+T+OW++GZo5LOvJMkae+LUTRI5 5FW0zlJ5Edza2fgul3W3HpL6pBG3P+cfiHbWyhW+20S65c8BxO2CBAbyyvBI6ma57e 0LxxMklcUgjm8hd0b89VeP6YADoEWteo4NpnQb9ND9r5BYd5INEBzn1HuoMR1JgQCI r2u6tirzXNrqQ== 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: Fri, 6 Dec 2024 09:05:10 +0000 Message-ID: <20241206090515.752267-9-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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:05: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 B6B311F03DB; Fri, 6 Dec 2024 09:05:59 +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=1733475960; cv=none; b=hp51HO3XO/eHvFBz8Cd/zNd8IUdf1bU6+c2+vRsWkIztKqRed7om2IHi0XVoP/SiirVWG1T2DaVMnPar9BR7B0roTixys5+mvdt7mX0DqmXY6vsOcGsz5r8yGlztf0jbhgVDVCQptas75lj2DtHYkkRczSGaAxI5wN1Ri/dSzR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733475960; c=relaxed/simple; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PcHetvpt2zUmOelwIxw6WRg4ahU3Zgs6A/wRu/uX8+HaLetWtoSoANUvz3r0xPbJJQ87P5vTErFP47ldIYeFKiArQvOXWEzHlKwfwUv8XQeASfLU3km8R4s4bEU2MSdCvK3jRFUoEOH6cmArbBTszymIhQpkia45hyyENIQYHLw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=P74wnPHX; 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="P74wnPHX" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 89676C4CEE1; Fri, 6 Dec 2024 09:05:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1733475959; bh=23hCTKG960bDy1N3PIz21oRrK0JViO+CB1Bwjy/jA8g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=P74wnPHXOLowZ4cv4YPEOQ0OoM0njxtWxkenrmyY0eYHqqotiTvqxnQmchXw7ACOT +/iXxOeA1TcBlUKAyMw5skc0aRCYHS16PhMCjky2K+4h3i0vrOmX5KFG0BPg9W5pLT M4eOM95mTt/dCxw/Xl2zRn84C6rhtCToVg8Jlf8YtQ71NKaalEb4IYvMhbBC7iQ7+Q FdiHQMuyODirO9EhRi8nVRR4wZGY8TglOg1cjYDuTLr/l56LwUkwVvC+g433pKIsOB SDo2uM6iJevT/tpxaVLDZOd7JVqHKbVKWg9yavTufPa0n8N+s2mQRnXxm5KSK30dv6 /u9JTHJaEXXmQ== 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: Fri, 6 Dec 2024 09:05:11 +0000 Message-ID: <20241206090515.752267-10-lee@kernel.org> X-Mailer: git-send-email 2.47.0.338.g60cca15819-goog In-Reply-To: <20241206090515.752267-1-lee@kernel.org> References: <20241206090515.752267-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