From nobody Sat Oct 4 14:33:04 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 0E0112D46BF; Fri, 15 Aug 2025 07:31:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243116; cv=none; b=nhg4Jb9la5xO0ZRFWny9f29hAlo6WZBVxXXq1NkPeIoXrQq8+PmqpUw5bwdExdSx6U5xDmS60PBFM39UlFlj7qCuDcVLwtLwL0Uf8bVviyBh8lvPcD5PCGP+oNgQjnOdTpjVJtLp5orOKpm1Q9f8qmgZyOWOtMmJNTOxyUYY1eQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243116; c=relaxed/simple; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PDqHwhziNr4pYhhNE/sIA1xQxiEzGXBW30rkVlqh/S+mqHE52BXrAzNXYpio/CmjmHPyMn1vs9xtURC5FFUFQ7diU2p+mBPuAcESu2LXvrRWCnLIkthQ/kdfHvmcWTPJHk08RqwDC9BbIege921i1hu8vsJP5lPGWBN4unPk4Bo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZanBq7oI; 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="ZanBq7oI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD357C4CEEB; Fri, 15 Aug 2025 07:31:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243114; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZanBq7oIOBy4VOHmzL1tqiOd+94t3PtGXHCEZVGx1Z1um3wGOTZtWWeaOVy+nDyaM U5334rPTHrS7oNpLdhWZ9GVvkWXyUt8KS812OslYKwjr2LNrlgPK7swKKG1NEbVII2 u+L5iNmCJGwWXIH+RRui8WiPKM0SBqd0mcdNFOZ6xk4/VVF0x+cy4yP9yt8DnBRyf+ eHHa0IvZD0HlnhGUHZr8I/71QIGzZ7mpfieULE0jof5K+TQN1636647kJc1gj+ebW7 Fdoe3U5duJ99Z6HN8LMqhRY7zIDD06/DMpSPhBQJWVq3iBC1a8ZZHgJwhNtGfpt3N6 vILDbUBawkDfw== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:36 +0200 Subject: [PATCH v5 01/18] rust: str: normalize imports in `str.rs` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-1-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=871; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI0A8GSO4ANhM15pKf4emp3CTOYA4EB+oVhK LkSIUKwMnyJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iNAAKCRDhuBo+eShj d4DGEACpixzUudYAiCbLXXZZqjc+7blcvYyKLLcB8WDAxlvDW0CdD8j/LQMdKa90REOa4YYhKYM NskUP+wcrAa1vRmzJQL/13Tr7/uQS7RinZP5yp+qMzlvUReFP5Y3WUpUGauaBMGaABT8853LS+K yUwOi3HdzmWQFm4mN7LaL0ko9qR29lwGm2M/GPGcvZG5I0H3hydS6VqJIJI2rqglFiQIrWqkxjj Zfn+Rfkuw4boanebtjhYi6KiRbX4d2gp1VkX+73EURWA/mibi23Fr1GRQYQ3nT+GzH7r9pgOR2u l36AUOO4CaysmyPdZ0og0lR+Lb0QAIjMaaCLDbz6SBDRyXfNXbGy7J7N7ots4hP/Hosu8yOVUSk H9QZHSBCLhE3wcnZJRV3xgMYSkmqjsZZOmAHnlriR1ABStY4D8yf4dHQ8jTqqGd0Qr1IskZhP2A GBR+mLFrw2boK8sOqYd56iqOAp6Cpwm7Aun3NB8toC4KwOIiGamkIwf89XlBAtVB3XGFdUz4lyl pQyfBsy9SAdDs6Cl9oN4zaRJ4D52qcFJxEg0SVSiwoqlwV2wj/Bzs4GTTaQrT3wHHo+X2djhu2j 1tXN+xFDhx28nSZhc45JrIjyMCSRP8CS5OeCLyK6m6omKsbRdAxdSuqZnx1dzzbKyv+uwO4AhMk BHNG0VdQ7npy5/A== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Clean up imports in `str.rs`. This makes future code manipulation more manageable. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 6c892550c0ba..082790b7a621 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -2,12 +2,13 @@ =20 //! String representations. =20 -use crate::alloc::{flags::*, AllocError, KVec}; -use crate::fmt::{self, Write}; +use crate::{ + alloc::{flags::*, AllocError, KVec}, + fmt::{self, Write}, + prelude::*, +}; use core::ops::{self, Deref, DerefMut, Index}; =20 -use crate::prelude::*; - /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] pub struct BStr([u8]); --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 BA5302D29C3; Fri, 15 Aug 2025 07:32:38 +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=1755243158; cv=none; b=A1AZGzPoWnbyC5HQIbEUB6N6wqXKY6tamt11UGU15s1ZA7J+GaWhOWS4wcEzS019WGkV5yGpKF7IfztTL8eBsOGEXHDvQJSfRTnLkM1w4K0OBfeZn93Pdx9NjI0xmPlOQEg6+8qp0WmAz22AFk/3+y3jx4SGqjd04ZOVf+gFPtI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243158; c=relaxed/simple; bh=FkVup2wcuL8c9tJJq2cNTZw4TsjvE38QLG8Ll4ELGPI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MbFmaJyjMKkoOIm5qjm63q35ZPV0jNiC4fPfNPOHHaZdkzLFGT/OJuWZCtQJfS4TWy1LvbKeUtSTdP1f2E1oScQvvpDOkWewNQy+5d7xREtPpAyJqj5Eou27tCe1ExOUmGqDpod08nn8Bw5i+wPp5Lj+cOa3kmTeyuWhw6l+SO0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bUq8VeVq; 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="bUq8VeVq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C4CAEC4CEF4; Fri, 15 Aug 2025 07:32:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243158; bh=FkVup2wcuL8c9tJJq2cNTZw4TsjvE38QLG8Ll4ELGPI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bUq8VeVq4G2Lbo/FB9Gd5FzGmagP1mDDFACpLKgw4kKbuFIY4JJ6qNhZkVAdH6sfB RG8e9XoNuDeiCCWxGPzbDKELueBaitFbeM98mt1f7qpIrzNk4AsgiZXiXLhoBP8qb2 iqrcQQvieA7gAkyX1qPwTrAI0bnK+H/kTipCSe6F5adw6fabyaQoRBNAbIhW5O97cl 6bYlSTVL4IdWTlEgdMiKI7iJkfWAor1Twf0z8GFMPhsonu5x3U4A4GHxBXzF3yfvKH 3CpwtcKpctLexJGdjn0pL3DYOFRaiAqpEODvOw8uUxtyDDqWHCzonsPii0yuXCCLQp IOFb7nkeBRysg== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:37 +0200 Subject: [PATCH v5 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-2-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2330; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=FkVup2wcuL8c9tJJq2cNTZw4TsjvE38QLG8Ll4ELGPI=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI1gCssunQzXbTBS8yYArxhqZnIwsIj8PGqE 7tpubIs5GuJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iNQAKCRDhuBo+eShj d6r7EACi7Lm5Kd2xpfgDDT66mnDNnEUmZ0ajjPempnCbfR9cvf9Ot3avkZBTwe2ZdTAVyCudpuI UCPisGWUp1PEBL9RoN6kTwF6B3ht25LLgK5jdhotyUoKU5ez+wHucYHABPgCXiH0t6pJQlibiEE RtAdi0iG51gOu/9EU/Dxd0UXhqKIJ584tWNqVE3Yfp4A24oMBit0qMYa5iiVT0NYkQdQ+b+WR7e VxJuhUuul46NkukxPcuFpr9lWPzEUXLuruAsTzSV8Erj+T7ZsgC/bIDaKLDSsOF5xzcf4myhcXO tqVJI2xrwK59Qcf1EQviKbJN+8MOs6napZ9krvosgC6KTnJEtTFee5+OKXJ0XyckIZlYWKSUHee qlPdXLw7oAQn6GcZDTXNuh4NSqeAzQQXeGN+rTdC04ukxENx85CeKGm2iOQgRWFIN5nronAnhUL DLmWdcUYdM10+TB4sQmHvjxCaLi5f06bbwA2QcVF+PX0uiiiXKDoIFFIykujJGtRwsSf9wMpLwF oqV5yiztugX14+y5tupFrhSrY9qB/yLHq3q5Wb5n6R3Jvv+0mN+/G1+rvwCzcbsVZ3J7fpH4S+T vDtbHOXDbH8FuZ4OOR73H8FiGnjobE0BzOK1oBSbpCmE/TKpYpSEOFdfyBDWXtfGY172JntVQ2+ +FqJp54b7t/QrhA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Improve `Formatter` so that it can write to an array or slice buffer. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 082790b7a621..76632da357a6 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -7,7 +7,10 @@ fmt::{self, Write}, prelude::*, }; -use core::ops::{self, Deref, DerefMut, Index}; +use core::{ + marker::PhantomData, + ops::{self, Deref, DerefMut, Index}, +}; =20 /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] @@ -825,9 +828,9 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// Fails if callers attempt to write more than will fit in the buffer. -pub(crate) struct Formatter(RawFormatter); +pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); =20 -impl Formatter { +impl Formatter<'_> { /// Creates a new instance of [`Formatter`] with the given buffer. /// /// # Safety @@ -836,11 +839,19 @@ impl Formatter { /// for the lifetime of the returned [`Formatter`]. pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self { // SAFETY: The safety requirements of this function satisfy those = of the callee. - Self(unsafe { RawFormatter::from_buffer(buf, len) }) + Self(unsafe { RawFormatter::from_buffer(buf, len) }, PhantomData) + } + + /// Create a new [`Self`] instance. + #[expect(dead_code)] + pub(crate) fn new(buffer: &mut [u8]) -> Self { + // SAFETY: `buffer` is valid for writes for the entire length for + // the lifetime of `Self`. + unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len())= } } } =20 -impl Deref for Formatter { +impl Deref for Formatter<'_> { type Target =3D RawFormatter; =20 fn deref(&self) -> &Self::Target { @@ -848,7 +859,7 @@ fn deref(&self) -> &Self::Target { } } =20 -impl fmt::Write for Formatter { +impl fmt::Write for Formatter<'_> { fn write_str(&mut self, s: &str) -> fmt::Result { self.0.write_str(s)?; =20 --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 0031A2E2853; Fri, 15 Aug 2025 07:32:14 +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=1755243135; cv=none; b=m5SFfHE18hKY0n0bE06uNRi6lPogvSrykQQROPPLuN0igDKiTPJbKPdAO1guUWVw+S+C1mxMRLbroPVnQ5SXyb4jbcXC250t8RLC0Wk2QzsivHOQXV/ecWynOc1apUTirz2f/Y7l5sbp39cWQ/toED7X9Gh3a+KBXiujoyGtZkk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243135; c=relaxed/simple; bh=0EwZ8Bosdo4P1PMrvkQ74N/d1HBPfa9SjJN+YgtIPJo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nnJnBT17EWIL70sPEfGsD9ukejK9+xU/m2qSlL9oXSgfM+fYhdXIKNFwgECaNmn0gKL2UD+UVcNW3SBdQJfpEvsOuQ+D7Voak4PWufZIgf8q7kIKdKV5x5Dv4Vurn2C+ebWnsr27qMnz/Q9JhpMx8g/fFgwNYDAEqDun2MKzvcg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gER1V5hN; 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="gER1V5hN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24AA5C4CEEB; Fri, 15 Aug 2025 07:32:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243134; bh=0EwZ8Bosdo4P1PMrvkQ74N/d1HBPfa9SjJN+YgtIPJo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gER1V5hNMG5oecJuxGCVxwQ64bJnJNVuc3KTfgFDNHqa3aif7xeaiGxSS48+Wevqc 1bWCdeTdq+Sl2pMu8tKjzmdZfyhej89Tzoru1YGOrtDqbstR7b6fVX9t9MK9t4ByD4 iYf+f/f/N5uHY+ZFjUIEW8vNhigVaYVTYxfHf3hlGR8mzoA6Je7Y5e27Nu52UQYioX onskeIkM9M6NxIRYB4EiXUTNCYiO3okMRuJXaSpi1lf+5NRTGM/Vx6brBBxxDvdg+v i1zjzSiyweNKfP5TwSKVaBew+2kFhK/vO0fqIS1Wi+7qT/jWQbjxew36xtuZVHYO7G 3NtV7/N9yMI7g== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:38 +0200 Subject: [PATCH v5 03/18] rust: str: expose `str::{Formatter, RawFormatter}` publicly. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-3-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2004; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=0EwZ8Bosdo4P1PMrvkQ74N/d1HBPfa9SjJN+YgtIPJo=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI2hSO5+cQftJrpInWHKQMFvFjF5OjwYj0py eua0QDIG5+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iNgAKCRDhuBo+eShj dwELD/9cTjghKDWUtkViDnmdNNe2B7cwgasLZEVzMGhRCUVjX0VNhPaU3YflbBRRqhHDv4HAC9Y gb0IhsmdtcBqBhgmp9Z9wQ8/fR0zECWaNXh/p+wpKjI1iVpJmZyVnYMa2YQkPvBtMFgJWBy7JI4 tfwNQhysauxk5ybhyaiQTBQb7xzxrxmfYI8OD03OssSTu6ahDk88cVE3zxemslExOY0QYqJ/tvG 538hAYJPqGUIMlmT5NgEI+D6zO/dXMIBDzOURjIv8YuIswg3btLQnwCb2fJLqWBs1379B1dT1L/ st7LBQVznKqOd8iKTx2A94HnDC36b4MB8f5cqY8s+jCF6xnNYtqVMeWxiguasFvAC0dxp7EiqMp 8gA+OcCabd9VlVB9ybB5+aSUVFE+pe/wjabtRuuR1+szRTrOknIEhfbKWxcKK+xisEUxn8iA56V WRZ1XuMdkDKDqetL2VjGvDt6HuGFCCbYKx1sxFg8nIK3iQRY8u0pbGmuorG00nUMhtjstvGkIGV CJacumUVlZyJwJzn7bk0sK3KhCv/1jYyt50l0zciMy5cTbJ4weU7TjuKyAQYN88Rre5hIHDI4rx yte2xcpI7LejcPExKvlloteuL96bo5yiFGSFpqJryviWf0DOU2vnY3UM6dQfnWx+ubbqp8rNPf4 Kajcpgvapr6pUhA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 rnull is going to make use of `str::Formatter` and `str::RawFormatter`, so expose them with public visibility. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 76632da357a6..46cdc85dad63 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -736,7 +736,7 @@ fn test_bstr_debug() -> Result { /// /// The memory region between `pos` (inclusive) and `end` (exclusive) is v= alid for writes if `pos` /// is less than `end`. -pub(crate) struct RawFormatter { +pub struct RawFormatter { // Use `usize` to use `saturating_*` functions. beg: usize, pos: usize, @@ -794,7 +794,7 @@ pub(crate) fn pos(&self) -> *mut u8 { } =20 /// Returns the number of bytes written to the formatter. - pub(crate) fn bytes_written(&self) -> usize { + pub fn bytes_written(&self) -> usize { self.pos - self.beg } } @@ -828,7 +828,7 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// Fails if callers attempt to write more than will fit in the buffer. -pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); +pub struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); =20 impl Formatter<'_> { /// Creates a new instance of [`Formatter`] with the given buffer. @@ -843,8 +843,7 @@ pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usi= ze) -> Self { } =20 /// Create a new [`Self`] instance. - #[expect(dead_code)] - pub(crate) fn new(buffer: &mut [u8]) -> Self { + pub fn new(buffer: &mut [u8]) -> Self { // SAFETY: `buffer` is valid for writes for the entire length for // the lifetime of `Self`. unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len())= } --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 5D1C931986D; Fri, 15 Aug 2025 07:31:58 +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=1755243118; cv=none; b=kjVDcB4J/m8BqUkzN7CbDozqLb0F9FsJ5Pf34+4zN+M1HPnZTwFFCdOC8DLAyk24hyWqqyGPI8rXroqfRzH9iGqAKjZsIqApZoINU0oG8tw+ZhI3yYb8LX4lI6Y7D7Z8OJ1a/9JSeC4y527oiAX0qZGOiJeEq4o2wL8FeTlVsEk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243118; c=relaxed/simple; bh=Hl2oRNYbBHso61BXXHLO+jiCInVPd7Fo+1gfwzFywQ0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mHY/dqoZZd9XiQv3RszrX5hqz74hOqcCKJRe8F98imJ0RnQ1Hm2ZMl/FmGz49LkSYg18kAukdt5wK+Zn4iz1KG0l/2F7qziY+a6ogenLFnbUjjQH/jeW8Rt4DwgahEywZX6ThulPtjMekUB+gZQrTYQcuDhQG8NN1rOk1pw05cA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LXA83exv; 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="LXA83exv" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14E50C4CEF4; Fri, 15 Aug 2025 07:31:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243117; bh=Hl2oRNYbBHso61BXXHLO+jiCInVPd7Fo+1gfwzFywQ0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LXA83exvk1yHaow9Vt5CPd16rsQ8vDDL5E1QZzqlLwsiIFhEd6w/Vs3Zl7r9e+mMd oCAWRjtqrND6Zqn7mAcrZErnThJM+JC1RIKMIj321PI+fpJd8X4lPGeUx7G6GZ9OpL ZmOlgjwNznKG72rRvUP8SgR3MVJKwbvK96Li+fvkb+8NiYrqtRz/u9198Q1/vHAJvd H/1OQsbjQSM3UKgSR/r/hSR4Haa7RvcSJQNE7qRpZrsgKVfjozAPXZRPKz9nj73WU0 r4sApKyQdByVsNIf375r7ByI+fJmWbpHX9Y2L1AnrXviEKirOAzHdl3QA+m5xkAZY3 rWURcAqvuM2Pw== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:39 +0200 Subject: [PATCH v5 04/18] rust: str: introduce `NullTerminatedFormatter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-4-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2576; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Hl2oRNYbBHso61BXXHLO+jiCInVPd7Fo+1gfwzFywQ0=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI3vUbKhBBQMW+RBgwIt+sBsnWaEjse0FiMQ l9nTK7DsBCJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iNwAKCRDhuBo+eShj dwrdD/wMWCOy6wPKZTmKVly7PWtSFZORMHM0qVPzJbU3zNNZuQLeWtlQ8wlvPoALTjihHBRIwWA oAJKgRVIJhXs3vDAATE1L40XwQpM451uHO9KOqhk5EL24nNVze5N8u5vqZtkN6tIg7ubqvooNKw MnXvkIrfPIgXb5GHAoiB2vnknCMvN0y9GeeJZDmW8+3QhpK6fnA/now4SjnXwf90c/vedjQtOfB ItTMp1H7x+3pwVQ6yB5F/g9IyETzv/SWuc2HrwmsJ4LesDVOSvWq/UC92QT67hcU7AX0yGUN+/g GZkg2yLmiZKcnJyNU1ZtiiYHd0sgEc17ojf1ng173Db9LavcvJedLDZdF6zW2H6R0ZfgDJSVQbn MezYgnjtXTZg1PZP9CuwueiXPa9H+Mqr8Ebwa2EpVLToZektMb/lcYN4wKNzELVMsUxj1+7syN9 INZ85qlsb3OfsHyMTWn45BiWYl1f5Tu37/N1HEcvtIJRVMfFfsUUzbEmLnqE+/GsEIFpiYIR1gl LjuVCLtIDYzWInBmV+XsXwsN7GdDoLT5eXUxhQrc2n46FiJviGdS0KkGNdCNwSulbgvarNHLLG0 IFTEeIZUgIQGK6Rtb6gtG/D8GYdTaZG9fTqHzttD04juwQ+OOrTkrCtCRoz5qvPddtPbzC1GrkP aiuMHSW3bnD4+3A== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add `NullTerminatedFormatter`, a formatter that writes a null terminated string to an array or slice buffer. Because this type needs to manage the trailing null marker, the existing formatters cannot be used to implement this type. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 46cdc85dad63..d8326f7bc9c1 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -871,6 +871,55 @@ fn write_str(&mut self, s: &str) -> fmt::Result { } } =20 +/// A mutable reference to a byte buffer where a string can be written int= o. +/// +/// The buffer will be automatically null terminated after the last writte= n character. +/// +/// # Invariants +/// +/// * The first byte of `buffer` is always zero. +/// * The length of `buffer` is at least 1. +pub(crate) struct NullTerminatedFormatter<'a> { + buffer: &'a mut [u8], +} + +impl<'a> NullTerminatedFormatter<'a> { + /// Create a new [`Self`] instance. + #[expect(dead_code)] + pub(crate) fn new(buffer: &'a mut [u8]) -> Option> { + *(buffer.first_mut()?) =3D 0; + + // INVARIANT: + // - We wrote zero to the first byte above. + // - If buffer was not at least length 1, `buffer.first_mut()` wo= uld return None. + Some(Self { buffer }) + } +} + +impl Write for NullTerminatedFormatter<'_> { + fn write_str(&mut self, s: &str) -> fmt::Result { + let bytes =3D s.as_bytes(); + let len =3D bytes.len(); + + // We want space for a zero. By type invariant, buffer length is a= lways at least 1, so no + // underflow. + if len > self.buffer.len() - 1 { + return Err(fmt::Error); + } + + let buffer =3D core::mem::take(&mut self.buffer); + // We break the zero start invariant for a short while. + buffer[..len].copy_from_slice(bytes); + // INVARIANT: We checked above that buffer will have size at least= 1 after this assignment. + self.buffer =3D &mut buffer[len..]; + + // INVARIANT: We write zero to the first byte of the buffer. + self.buffer[0] =3D 0; + + Ok(()) + } +} + /// An owned string that is guaranteed to have exactly one `NUL` byte, whi= ch is at the end. /// /// Used for interoperability with kernel APIs that take C strings. --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 165DF1862; Fri, 15 Aug 2025 07:32:08 +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=1755243128; cv=none; b=H0kVDH2lND5yygF+t0YFEW5DddKf6wGVU/qIWxpeI1w1btzXiCzFmPu5VPmUkoA/R/VjInrt8r8G28kZJneLBW27t3KJd7Dzmlfg/aGy2lj0dxF+W56v1OGXxF/FXq8cIsA5saF8yfC3L+OTI3aBxWbbI37e2hR8jZkg5WpLQe0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243128; c=relaxed/simple; bh=F7NQZ7aM/sjs4UN0cliuzm5bcnx/6FXfwMp5ca3gN68=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jDC5YfBH4NbsY45Uww6iETWR8x4ERrZziLJc0ae6DWvFO47JJtegDES27zz+S7bEoxGjwIjKIrKDRFCnV2Ml1mSp6+tzrx0wEedt4Z10aoEy+dBEs9/6Ewcd7n14WnzAaTuTx2f3Sxds9P+HuudnpBtOD507ih2J28AMlghG0Io= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uYMV7rmS; 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="uYMV7rmS" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 234E9C4CEF4; Fri, 15 Aug 2025 07:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243128; bh=F7NQZ7aM/sjs4UN0cliuzm5bcnx/6FXfwMp5ca3gN68=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=uYMV7rmSbFot9wzZKrNeruUNAUb3LXNE7Oh87g81u9uQMs1ahVLYMsJXyP75n5T2u sGlXU1xesYUwsCqtzzu8J2YUyI67W+KugiIfQQN9nbWVDDiE0dCqj1pRaHGFvHkUNO N5JtV7U2cTqicQ0OoNuWEWBbhr042rKFI41mopyYOoJ/mgj6EY9iRVxy84rTsWFdWz 6VrjgEq1+AX1/hfj6JiSKCVhtbCMxyMT20EWLvcxLdA24BnksKPVq3mS+sMuYY75y4 TkFUXTrbVzARYGQUVsBLyeM/IHWxOfKYDYr7e3KDMW09c4VLg1c7muSRG/MjHva4ib rsEOMFaFk4a1w== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:40 +0200 Subject: [PATCH v5 05/18] rust: str: introduce `kstrtobool` function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-5-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3198; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=F7NQZ7aM/sjs4UN0cliuzm5bcnx/6FXfwMp5ca3gN68=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI4uJJ0g/pyBl+VMaxi7s8/sdPJWjSHksM2z MarvgzP2CWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iOAAKCRDhuBo+eShj d4zcD/9oB4VDVb8WdtwzJDnbNqIIm3vbDWqynjDFM0hSP001ea3NPSZWir0Q118jOnTkLzMUhmY DGTbQePcd44bLHQjHuiQSCzQtpuIeXSOREyi747yCW7nHjmdWoQb7odNml3nyKO/xe7bXDrs5x3 wg/n/30r6fob4DivAoM/yv/pJyGSF2rWUYEVljZL4XuCbWg1tKSPVUiViubHgxQSQQKtQBYZwad RJKJ0WtfS0Rzrce67aXTq6f5XcvaMWWdta9hCmi9kwTjqIxeHMq/bn9ceRMpUQN2+MEy8Um/+5Y jHzDFrsZeiQ+DArCZxDRlK2hGmdbh9YeSpWnfRRIE/FsTy25CJ2wyKDhH3mRAEUDLpFFBjHLvBs AJizLS78HC3S+V424c3HLuVwEVXsclGou90WEpOiNB8GCb54luvm4UAYwwsGiPspEYkosiao9jz emHZhGaL5/pMLp8mODVSVylBCyE4r++A0T3DdrSvA+4lcfNIe8X1SMvOGgoZggH6bCtdwLGJz/n QN8eTDTcG+AmP2tc2hrSaH5hq6HqSxv8oaMkN2BvPTyBO1pI4KN5mP6zf4v4XEu8beQopcxrlii FY6pa77H1Xd0m5tGXFfKvoFMD2qxGWG9wiWl1xeIQ8mXzqHChW5P4SGzlNS+PzREdYH1+y+tuOz ZP4BsWdHYJmHwqw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a Rust wrapper for the kernel's `kstrtobool` function that converts common user inputs into boolean values. Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 58 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index d8326f7bc9c1..5611f7846dc0 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -4,6 +4,7 @@ =20 use crate::{ alloc::{flags::*, AllocError, KVec}, + error::Result, fmt::{self, Write}, prelude::*, }; @@ -920,6 +921,63 @@ fn write_str(&mut self, s: &str) -> fmt::Result { } } =20 +/// Convert common user inputs into boolean values using the kernel's `kst= rtobool` function. +/// +/// This routine returns `Ok(bool)` if the first character is one of 'YyTt= 1NnFf0', or +/// \[oO\]\[NnFf\] for "on" and "off". Otherwise it will return `Err(EINVA= L)`. +/// +/// # Examples +/// +/// ``` +/// # use kernel::{c_str, str::kstrtobool}; +/// +/// // Lowercase +/// assert_eq!(kstrtobool(c_str!("true")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("tr")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("t")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("twrong")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("false")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("f")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("yes")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("no")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("on")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("off")), Ok(false)); +/// +/// // Camel case +/// assert_eq!(kstrtobool(c_str!("True")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("False")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("Yes")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("No")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("On")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("Off")), Ok(false)); +/// +/// // All caps +/// assert_eq!(kstrtobool(c_str!("TRUE")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("FALSE")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("YES")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("NO")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("ON")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("OFF")), Ok(false)); +/// +/// // Numeric +/// assert_eq!(kstrtobool(c_str!("1")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("0")), Ok(false)); +/// +/// // Invalid input +/// assert_eq!(kstrtobool(c_str!("invalid")), Err(EINVAL)); +/// assert_eq!(kstrtobool(c_str!("2")), Err(EINVAL)); +/// ``` +pub fn kstrtobool(string: &CStr) -> Result { + let mut result: bool =3D false; + + // SAFETY: `string` is a valid null-terminated C string, and `result` = is a valid + // pointer to a bool that we own. + let ret =3D + unsafe { bindings::kstrtobool(string.as_char_ptr(), core::ptr::fro= m_mut(&mut result)) }; + + kernel::error::to_result(ret).map(|()| result) +} + /// An owned string that is guaranteed to have exactly one `NUL` byte, whi= ch is at the end. /// /// Used for interoperability with kernel APIs that take C strings. --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 B8623207DFE; Fri, 15 Aug 2025 07:32:18 +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=1755243141; cv=none; b=BRIOx75ERKhKx4RfDtr0kj3NxcR0heiPuWG3qbSUUrqZ73vB62z/L07ZGIUKgX929wCggV7ltEmv3tXMJgKfXFvT03AZ5rEr0o6wcT++oyazyjvZ0039NxsvSLUH/FLVaF8O+Ox4CEFH4gmnt8N61Tk2ex0NoKyVegduLo911CI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243141; c=relaxed/simple; bh=OtQddS6ZPwb9NsJVIHicx+JPypcp8+9DXNsP2q69rJk=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=prPbvGhYIzQgACbwtCmCwcpHLh0B4aH+tB2t6OzarLRsygyAvVQTn35AcBwLYBVPNzDtmdoYjSYgiSrfi4JN3iUDH2l1g+xjey10k8bbnc9GjIuGxqMLTa+WvIBoGUkBBH5p/TApWltZ17QzXmDF9zlbX9csv89FrUOWvlg4FTM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qZgO7ZcU; 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="qZgO7ZcU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 707BEC4CEEB; Fri, 15 Aug 2025 07:32:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243138; bh=OtQddS6ZPwb9NsJVIHicx+JPypcp8+9DXNsP2q69rJk=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qZgO7ZcUOShEL20wjOzsryQ3ygdF6qfuUgCBur6zonytmSQqSHSmZNJRpJE+x2o7N d1vf4dD0B0a7LSawyqY01xyvjUKWDwBViiYpmgEIHJRr1oFtrOMa6eWQcuAuJfxSIj t8mhcM7NgFyRp+vLnyTb1VPhqVOtcq+e9t6KFa/a0wpkQffo1cQrJIJ3ASim/j/flP nWXFYxcxXqf44VTYLr5rP6+ZqvVxBa83y5sbswBzK+y/TfOTpDK3lHa7zE0zGDbgaM fSvjZgJtiaSGYPY0nDgcSKiusHXy8ksE3KHgfsZXD6l90w1oF/Ck3/4VnWL4aaT69G PG5KbNoGygRsA== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:41 +0200 Subject: [PATCH v5 06/18] rust: str: add `bytes_to_bool` helper function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-6-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1366; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=OtQddS6ZPwb9NsJVIHicx+JPypcp8+9DXNsP2q69rJk=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI590lR6f8ZJYdslPPTOmFmX4I5TThmacgAR 9fFdCr4UpqJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iOQAKCRDhuBo+eShj d5ooD/98J4AUaj99036/IMhZ092yldDZQGM/wznNZmm64EQPuMLGwx9BYaMMckEb8qLzr7v2/WM zUSJpfKOpbOEDU78DHDNm2JZiEVUUB3WvlhhuYZlqauAae5MxSfxQgOXp2y78cFVoPeLCTG3txe c64LtWAoAFPY5R7TEvOiDA/iYAE7CK52XFpWbbSLbrbVWw4EBo5f/sOS8RoRlRFaviYvCyvN2Ee rptSENyTyRdOsUVl5f/81a7eqEfbMNbav+ionpwgetYQVvOtGHv6uoxFIQGT+bPdxaFyk2dp1M1 qJaoPphFU9AgiHYuadktVpUd9quYqSdHqv6pmoE/wm5+APT3f0QpCm6FtNxtPFVtIe/o/fG+q6i AvF0d459YC0YtZeMRxee0YFEc5xBAiotlrmHSAbd5xkLqZ3cll5BwYoj/SIkf2RSu91pOVcigta DohSVD5GznhqG49BHko43ShmJqrr1rAsBvqVlcrwqho0dEB6/giuuzOiqu+fx0LQRtQUaCPgEAU ofwqBzb853b2u8W2nVfpvpExzUR9ANYYD9lo4geKCvjTXphaO9jPV/mq/xTH1EBuQ8vp62UtV1t C8RciBdoFQY51jxRuqlrjDgSOiOaV0Ob1hAZ1wCc8AQS1jhl+O7RPCzkJ4Rj8kUAofAEf/lRZDA bV3QkPnGsV0DoCQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a convenience function to convert byte slices to boolean values by wrapping them in a null-terminated C string and delegating to the existing `kstrtobool` function. Only considers the first two bytes of the input slice, following the kernel's boolean parsing semantics. Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 5611f7846dc0..ced1cb639efc 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -978,6 +978,16 @@ pub fn kstrtobool(string: &CStr) -> Result { kernel::error::to_result(ret).map(|()| result) } =20 +/// Convert `&[u8]` to `bool` by deferring to [`kernel::str::kstrtobool`]. +/// +/// Only considers at most the first two bytes of `bytes`. +pub fn bytes_to_bool(bytes: &[u8]) -> Result { + // `ktostrbool` only considers the first two bytes of the input. + let nbuffer =3D [*bytes.first().unwrap_or(&0), *bytes.get(1).unwrap_or= (&0), 0]; + let c_str =3D CStr::from_bytes_with_nul(nbuffer.split_inclusive(|c| *c= =3D=3D 0).next().unwrap())?; + kstrtobool(c_str) +} + /// An owned string that is guaranteed to have exactly one `NUL` byte, whi= ch is at the end. /// /// Used for interoperability with kernel APIs that take C strings. --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 8DBEC31986D; Fri, 15 Aug 2025 07:32:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243121; cv=none; b=H0BE/D1rJ0wMCOoMOrO6p1UPLAF+nWMu30nWz4mQaPcaLfgrU4gKmIZpV7RMnVlXl4QuyCKup4cDECsT5DY7z9DteeheAMRMcupxvjI00qHkdT4qs/rzfx7qXphbtkB22d++1Jz9At/YC95SYoZbUzhs5tTjSumJ73prb1dytjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243121; c=relaxed/simple; bh=J0dcIO8q+R6ovaWgRSo4yUozRWOuH9a5eI1bG+1XmvE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hcH3DwGhVM1Rku2LGm5fePpN87tg3njvdK3IA2FRE1gaLx8959mFlPYAduDXr+o4OHDLztkYeiTQj1M4wDrbG8HOVGYLgLNe3OPtrqkRW9iSZ8dF/Fyll6Pfajj6QHSNYrN9qBXlmWEHj4gruniN7kDx8c94yT7G3yfLqMXL9x8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=G93voBOu; 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="G93voBOu" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7634FC4CEEB; Fri, 15 Aug 2025 07:31:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243121; bh=J0dcIO8q+R6ovaWgRSo4yUozRWOuH9a5eI1bG+1XmvE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=G93voBOu5yyqPy0wLvdJTEzBzl4FMgbOnsNVI82Bp4GG0Lym0JrKLK57QIbN9Ev0+ 2a9QvclbQ/Fb7rlrqhTNQgOi9s4tOuttd3WCYIoW2z77leQo3f5lAM71m6HD46X4D2 /jwmTpi4ZnPTSDgM7KLvjQeK5UYaaGT6XXgbzmb3XeyWi2QJzzyPa3fSLMbLZN97SL DLIo6CtkwTY7LdbcIR83u0GGtMPwQtBrLbKXi6D4HQi9Xe80ZAyiG8g/3FJa/jqIyj mAE5plPcOwjvPyMvx3F1oX/6/+KApPZQjkGX9KrLZXX6UoxEFQM2R0OXYpdwCkUrem YXfo4ODuX0wwg== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:42 +0200 Subject: [PATCH v5 07/18] rust: configfs: re-export `configfs_attrs` from `configfs` module Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-7-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1253; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=J0dcIO8q+R6ovaWgRSo4yUozRWOuH9a5eI1bG+1XmvE=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI6R4/sQo6t1ylP+Cdy7ShyHbySRhr5YP0/6 U8522t06GGJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iOgAKCRDhuBo+eShj d/YDD/sGRAPpZ7wgjqskbdog7R7HZgLmq5ybhci7fSz2TMcq28MfDjx5Er8eiKmrl6GxlS3kcCF ymK4ZDO8lcKj7OY7qjrr/mpskAxtVXIPrQNhPDzgs5P+I3EVCeIsRdx0X7GfwXiYKHHzT5sGblQ vLOQgNB58k/cbNHSG5PeFjPhV3yM1eD4vmGb2cZu5oadDRCN2MYz31ggHuN3mxGdeN9DK/JztPs aPv7SzJhPLnTYE8ftUC5lCgE/v+ZfYVbKpxfqBEdZfo54pEBkc018VXHrYLZIlrIKWk0GXMWwnV t+V979tio18dfXAPX2TdazPE2BybBFfQZnpMURFg4y9qr4dWqDNqScZvC6XYWjk8Edwu7H2sqE7 RrJAsSD2WDTWwZjDjA1J1Gn4E/x4A4i3r0D7LdrQGJDUw1UocFp27V/79ljFcqDwNftdc1nKbfT 48XRAfuKKQi/hhV0NZQejJ2u3cu7bbS0hsKf5s6GVmQaiJ2YRaz/llUV5IEBVizCjOaG1Hx4W5o 4yIPuSrKxuuHmna0OJiGoupG0pzZy/NYPPMBLBxzrto4/ZrJULL1QyCqEjGdc3FhgoVPYiXjMOK Yi8qShcOQDT99J1TApCEYyzgCHXzqDFeYJszB19ybRJAJ2m0GDADiIcuCgCTwpdYzHtSiSU0wpZ sDZu3pcxEkXqZSA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Re-export `configfs_attrs` from `configfs` module, so that users can import the macro from the `configfs` module rather than the root of the `kernel` crate. Also update users to import from the new path. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- rust/kernel/configfs.rs | 2 ++ samples/rust/rust_configfs.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rust/kernel/configfs.rs b/rust/kernel/configfs.rs index 2736b798cdc6..0ca35ca8acb8 100644 --- a/rust/kernel/configfs.rs +++ b/rust/kernel/configfs.rs @@ -121,6 +121,8 @@ use core::cell::UnsafeCell; use core::marker::PhantomData; =20 +pub use crate::configfs_attrs; + /// A configfs subsystem. /// /// This is the top level entrypoint for a configfs hierarchy. To register diff --git a/samples/rust/rust_configfs.rs b/samples/rust/rust_configfs.rs index af04bfa35cb2..ad364fb93e53 100644 --- a/samples/rust/rust_configfs.rs +++ b/samples/rust/rust_configfs.rs @@ -5,7 +5,7 @@ use kernel::alloc::flags; use kernel::c_str; use kernel::configfs; -use kernel::configfs_attrs; +use kernel::configfs::configfs_attrs; use kernel::new_mutex; use kernel::page::PAGE_SIZE; use kernel::prelude::*; --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 4E5592D0C76; Fri, 15 Aug 2025 07:31:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243108; cv=none; b=WR6SNZzmx2jVZYzCTf9Z/KTSfWn4ILU8ziQ4BYjmfsXu+dK4zAVTFZsDyz+EcM91i2PLE3EnkSGkem43+KI459fDQzntIsywhP06y1Y2KcAScfTwsPZVvDMdPOtSYpJcOFE1jJMgzlaon9XHOv1pnPcLdeOkxigQjIzNyIG+YcM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243108; c=relaxed/simple; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=WeDZ6ywzzdAYhhl5UgCaLxbASpDnbzGEqEKLJ8DVj51NC5DSr3F3e8GzTHeCfJM+T3F1vkD5M/sS68wdshWNdVbSHr0F8ykrN2sKr53YF7Ba9DSjAXDg35Gy3TnYNZGk/EqMfFqB9MmDBp3x6hGv5FL5k5e8HEPc1pfoz0qStWQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mTczE54H; 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="mTczE54H" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0040BC4CEEB; Fri, 15 Aug 2025 07:31:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243107; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mTczE54HZmbbZNwvOgdux1ShnM/tskNG8EVR0IcK30fWOVKmqLAVtN5ZotxfFDHRE DonrSW6M6oo/52meB6FhZzdG35k7aEZWYfzUXss/sMQkkEEOGs6cuomp2fBk0fstkY Sw43ugOfK/NMXzXirGCfgVQDUnB9H6+IdXfLLSPvzP4n0PsuKQc7EC+jooE1yMzQXg CcL7ojbSARx4LTyxEpNkS2r/cqc45jfLMJlisBiLfHwHO4hvtjcgVRoGeKzfO3AqEE BpNiebryK24kAo4JQVNncD6x10EOHE75iq5hLWVJgSPXZmAkp+gGwD2P3BzGX18xmA x4jnIbvAIln6A== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:43 +0200 Subject: [PATCH v5 08/18] rust: block: normalize imports for `gen_disk.rs` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-8-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1184; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI7fvb3LqftyA+u91hXHn434EGDlRuCUeMAM vLY3ldxArGJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iOwAKCRDhuBo+eShj d7M3EACUnDqc/rSdU8ExOD7kGKqbjFboBiKRTMhwuu0tG7ZIvEghvLe+AwIBoDX/OMxm2hHIIcc u/g13byv6CyAkTnaqBFgrMzVhvnGCBxzLUAxl9aVGTRGLm5+bzdjqfJtVuR6e8dqzlGGpwykRZn 95FEZvwVm53TYqSZSIkWNODfmUgQJUbPjAghUxBJCd1AUM6k+6o0Ldpif6mnYuQgzNaGXwloRp6 +iOk6xAFHTgEiXs2Ilo4n0PHT8nA86poRQgbi3Pk9Z0fXXBCTk+t+z0igEMGSe4wmDfr4cUtTbA zOCiM477pADPsdBI8t1kPlYaPu4aAZix8I7pPUGZDn//s6MbYl+kYQpFtwp52ghUl2mdEybvZvc Luja/Og5mARKtE1ko8zajW+4BREGlF4CozzWXRKQxfS4W8VmVnM9iP4nIgbQ5xYQSk5fQkSbtAp NatbL1TVC7XTjhjRwsek4x0ypVwVa9On9dn0Lb6UOv4Pp9F93BXSXTNVM2xyElOk7w1YQOaW910 Z0Uq3aC0YGSZn3VMPvHTEjMriP5pGQ1sLCGJkGcg8xPmTkJmkVsTCE210XnBySMoaQdA1h9sV26 AkmvSQZ0vNx5Om9wXj98QLXXwNbvGHQjlcHUyDv7vHX5rpRKoqoYVD+Qb0XPEcrU7AYp/0kVNjY BvuMp2wuwYPLzRg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Clean up the import statements in `gen_disk.rs` to make the code easier to maintain. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/gen_disk.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index cd54cd64ea88..679ee1bb2195 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -5,9 +5,13 @@ //! C header: [`include/linux/blkdev.h`](srctree/include/linux/blkdev.h) //! C header: [`include/linux/blk_mq.h`](srctree/include/linux/blk_mq.h) =20 -use crate::block::mq::{raw_writer::RawWriter, Operations, TagSet}; -use crate::{bindings, error::from_err_ptr, error::Result, sync::Arc}; -use crate::{error, static_lock_class}; +use crate::{ + bindings, + block::mq::{raw_writer::RawWriter, Operations, TagSet}, + error::{self, from_err_ptr, Result}, + static_lock_class, + sync::Arc, +}; use core::fmt::{self, Write}; =20 /// A builder for [`GenDisk`]. --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 14D4A2E5B22; Fri, 15 Aug 2025 07:32:25 +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=1755243145; cv=none; b=LOd0HW7GRz6wH/5bY1+5H+rJ9Ynjk6eBiO9NW68pDn+LZu8sRK9+xMsvcE6xuMph0hPOQNVvZoxKDJTnWmeUu/HIU6OUyV0suhqyAo1yjOqgsQC9uI4Ws0EVUKagl3QBr+/jXPqWpOUkjkx556bxjyIcP3yBgqYChhH5HYujK/M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243145; c=relaxed/simple; bh=9tB3J7dQh1l4F9h682AUS4KgJYolTIIS1I+Be6X1aiI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=r16uji2k5j4dAyOTxt6qqv1VCQxw2KqpxFTtS+eFF6jZEofJLHSLWP0sTUVkIthPSTO0Jv9Vqg+/0rtXAUXYpk0pjOhvOEUZpnrL8dgbYGb7nMZtukB0YHisXGRIlQGOvN9njzn7QThxoS/01N+5RQqn1flkiK1YgvdfUVhFPcc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=qgsr+D0M; 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="qgsr+D0M" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4AE96C4CEEB; Fri, 15 Aug 2025 07:32:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243145; bh=9tB3J7dQh1l4F9h682AUS4KgJYolTIIS1I+Be6X1aiI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=qgsr+D0MaqWugibKHWjkFj+jyGiYjwv/oPDwolUsyfl8N+IKngNZwLbArXpg+UTwU X3K0VFjGwHmOsSD6rxjr2klvoo8FjsqZApCJKKcih2MrGepGXAuqlh2xWCvHAXGwnp tOk2T582RanDt/IFXF62mzlp+LN+w63HqpnEZYtTdTTsNjKwpepIePczizA9ZThSAu ahp5JQQEMW303T7YslMkoUEMPtACf0mOlqeTfWgJ2N6Img4fRsVXAWtpl1Or4Nk9wI jKXjjEWCm08uff9DVPS4KDPIXTYre7Yu+5TwADlBOu3GVHMJDBFHBQJjZIf2DLmF89 k1pIhtpz68Gsw== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:44 +0200 Subject: [PATCH v5 09/18] rust: block: use `NullTerminatedFormatter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-9-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2901; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=9tB3J7dQh1l4F9h682AUS4KgJYolTIIS1I+Be6X1aiI=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI8C7IcaT3KXeuk4OwrC7yn2hhwBakMYVjnx QoIQB7uJEaJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iPAAKCRDhuBo+eShj d/VjD/wMq9kMtBAqFA2AgP+zUOtNrIHoueX9M68IaQ23GkiBAUsf9CUMyVukOx25/fmMzL8/4lO VzjBom+MaWDDs1fU2CKU/5nu0Ol4t7qhkJlB5zNOh5gz+N9KrRpGnByEtm6Ulm91LuacSTE74V4 AcBm78bgOqLd18Yln1+7VMQUanBPfpzl3ppQa+yhHhts6M5FzUnlfHO+RDd5TcfK/NW6Yp422Hm MmqEDON4wshS3nWrwr9Xof0bpg4i5Zg7dnD6la0zYoU+qt7XlomGHSweWvNBrC6jRT6iItxSS6f 62f9mTcBx0/WJvIli7UhqY3n0uCSNuo5v5Y1D9nd5Zo01p+TFxCbjfGwaACT/malUnGQieNKf6Z 2h1opvHsVo4cigPu0e2uJbWe78yntPTc0llngTM9nzOIpNNWREkcI5RClfZfWa4hFLxlCp5RxCP 2yiNnBhq0yGqJF9vRWpk3OoKqyN19u3oyHwjory4sltjuDnvm44Sbg0N6P2/Tdc9k04FGV89xMQ L5kKTotZDKyiXvnfIen1WIMCWvh8XSJYuGj729bi0VeI7YHOkatzMjKsxyEmrLmiSUvfrwXFZRg R9U3jhMX5/bKzMiYhacdcf16HzH1TuE2SO8uZYKA5bAkLOQEvFjhVgxbqf7gZcSZmmSzz62EYkC T7T/KIKQbwKNDpg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Use the new `NullTerminatedFormatter` to write the name of a `GenDisk` to the name buffer. This new formatter automatically adds a trailing null marker after the written characters, so we don't need to append that at the call site any longer. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/gen_disk.rs | 12 +++++++----- rust/kernel/block/mq/raw_writer.rs | 1 + rust/kernel/str.rs | 1 - 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 679ee1bb2195..20f1d46c774d 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -7,9 +7,11 @@ =20 use crate::{ bindings, - block::mq::{raw_writer::RawWriter, Operations, TagSet}, + block::mq::{Operations, TagSet}, error::{self, from_err_ptr, Result}, + prelude::*, static_lock_class, + str::NullTerminatedFormatter, sync::Arc, }; use core::fmt::{self, Write}; @@ -143,14 +145,14 @@ pub fn build( // SAFETY: `gendisk` is a valid pointer as we initialized it above unsafe { (*gendisk).fops =3D &TABLE }; =20 - let mut raw_writer =3D RawWriter::from_array( + let mut writer =3D NullTerminatedFormatter::new( // SAFETY: `gendisk` points to a valid and initialized instanc= e. We // have exclusive access, since the disk is not added to the V= FS // yet. unsafe { &mut (*gendisk).disk_name }, - )?; - raw_writer.write_fmt(name)?; - raw_writer.write_char('\0')?; + ) + .ok_or(EINVAL)?; + writer.write_fmt(name)?; =20 // SAFETY: `gendisk` points to a valid and initialized instance of // `struct gendisk`. `set_capacity` takes a lock to synchronize th= is diff --git a/rust/kernel/block/mq/raw_writer.rs b/rust/kernel/block/mq/raw_= writer.rs index 7e2159e4f6a6..0aef55703e71 100644 --- a/rust/kernel/block/mq/raw_writer.rs +++ b/rust/kernel/block/mq/raw_writer.rs @@ -24,6 +24,7 @@ fn new(buffer: &'a mut [u8]) -> Result> { Ok(Self { buffer, pos: 0 }) } =20 + #[expect(dead_code)] pub(crate) fn from_array( a: &'a mut [crate::ffi::c_char; N], ) -> Result> { diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index ced1cb639efc..2ea3ea1bdb4b 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -886,7 +886,6 @@ pub(crate) struct NullTerminatedFormatter<'a> { =20 impl<'a> NullTerminatedFormatter<'a> { /// Create a new [`Self`] instance. - #[expect(dead_code)] pub(crate) fn new(buffer: &'a mut [u8]) -> Option> { *(buffer.first_mut()?) =3D 0; =20 --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 66BD52D3725; Fri, 15 Aug 2025 07:31:51 +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=1755243111; cv=none; b=sSNDpjYEC3mPHaPsWv431KJw6Mf7foJMOq55Ml3o9BYrN7gZd4TcY016jJDu4iPrkCtWPmTGb54UzWrcx3UVLDzhMG1OcxBMsN8BQoXOQ1tjZqSYw6Hg5wj+Jc7f7YgpftLX9mvzST4OXR5YODN7RPQqjRuZFprE3cy5D5soG1s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243111; c=relaxed/simple; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BJmlHlVG0Z7eRjmEiVLMyqxJHxxACO1JICdap7xwse2m9cg/ucAiHUHm3Pu3qLQ6tTzp/jA9tWYzFkRVYVeqg9VSfoekyOMol3BDLRdz+Q5cwh2KlrQ/7+UlN9bsCcujDQsz3lTdvmEuG/SH23oJrIidOH19mC20ZGzlqij7reo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=VCi6hrM/; 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="VCi6hrM/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68790C4CEF5; Fri, 15 Aug 2025 07:31:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243111; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=VCi6hrM/NWYXHPrciG39yNdmLZPR5YDYNZlUFtZk8qAv8f1JmCMM/cyh0aPJo5ofV SWwQbtUQ/SJmHYMxn0FjdwC/EFhD3DWBStrD54lcccFVKQmnJBD8ToQB53x+iPhxtw S/bx4jXIqyFaZjcVtI+ug95RMi7hV2FGIEFdGSaOV4Yt9ApWDMIhtV8KXoaYLqUHl2 824bZMbt5ppBXQn7kbXLtGqp0YPAjFhYUN1ZQr1OalI/+H7ll+E+xCh4he4kVTIvrX /itC+RvEX1VZyjCyFElkzLqWv+uqN5/B7JzDHIy2qRGEFztLGwVz4TtiX11M4Xgb4n KJkvX7vOk9abg== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:45 +0200 Subject: [PATCH v5 10/18] rust: block: remove `RawWriter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-10-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2507; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI88IWZ7fNLhL0M+W00aAZDcjP7iujh+g0i6 ThQZ6OPhHaJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iPAAKCRDhuBo+eShj d6EhEADF619stZYj6i5xxO6CmLRojhkTXGrwOWBatsaqHH782uAvz6pkM2dB6MZD2JLf/RR8eYr fTONHUNnf8/2djGrAbNF/DtL9o4PQn0/7rMKOLQ06XpdKk/qSuC0g+XYc2ROzBt55WorpbC0B8M HQGRbRFsSHvJ2y63x4s/3Vyc3NEJAvBc4ygOKlvnVeh/fTrhmhZ5GvGGA8v9NzJi7GZcQgseMM1 3j342DuxIC2KRH8I1bYGsX6eVLoXbqhuMJoFvp7ywf3F4o/CPH1l4WMX9TuHmNKGOd82zchuzSc Yner+FSZ3xJ71iHuikIFS65HeEZHvC+7XzlBN8Fds1BNeKTa0JhNK/4/2P6OJSMUZR+56+i09UH IWQ2q4GALjiWSkIJWM9HmY5mmcXWvNvglucexsQMB3LmMI1ZeSq70K/t0RoJtX5KAb3ji3k4cTK ELVibfziNnAMirMHQXWqdsx3qOzqhJ1P9FNuV2k2/ep+JE3cjQFxNr2tpB4lneNxGIZp+NMejC9 GtvhCfPVxnufPW/FnP5INlEj5u4/hZKgX6EMfP2Pur3RY8qO//zV6F3WEtfQhKi7CChhLFN79Rb rFnitcP40afmfT+f/t4zjISZJqg05DCQi/Q91YYp+7sd252OlZQDYBGqjdMRDgfE7de3Elwrt0s 3s5huC8FqyJI65A== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 `RawWriter` is now dead code, so remove it. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq.rs | 1 - rust/kernel/block/mq/raw_writer.rs | 56 ----------------------------------= ---- 2 files changed, 57 deletions(-) diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 831445d37181..98fa0d6bc8f7 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -89,7 +89,6 @@ =20 pub mod gen_disk; mod operations; -mod raw_writer; mod request; mod tag_set; =20 diff --git a/rust/kernel/block/mq/raw_writer.rs b/rust/kernel/block/mq/raw_= writer.rs deleted file mode 100644 index 0aef55703e71..000000000000 --- a/rust/kernel/block/mq/raw_writer.rs +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -use core::fmt::{self, Write}; - -use crate::error::Result; -use crate::prelude::EINVAL; - -/// A mutable reference to a byte buffer where a string can be written int= o. -/// -/// # Invariants -/// -/// `buffer` is always null terminated. -pub(crate) struct RawWriter<'a> { - buffer: &'a mut [u8], - pos: usize, -} - -impl<'a> RawWriter<'a> { - /// Create a new `RawWriter` instance. - fn new(buffer: &'a mut [u8]) -> Result> { - *(buffer.last_mut().ok_or(EINVAL)?) =3D 0; - - // INVARIANT: We null terminated the buffer above. - Ok(Self { buffer, pos: 0 }) - } - - #[expect(dead_code)] - pub(crate) fn from_array( - a: &'a mut [crate::ffi::c_char; N], - ) -> Result> { - Self::new( - // SAFETY: the buffer of `a` is valid for read and write as `u= 8` for - // at least `N` bytes. - unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::= (), N) }, - ) - } -} - -impl Write for RawWriter<'_> { - fn write_str(&mut self, s: &str) -> fmt::Result { - let bytes =3D s.as_bytes(); - let len =3D bytes.len(); - - // We do not want to overwrite our null terminator - if self.pos + len > self.buffer.len() - 1 { - return Err(fmt::Error); - } - - // INVARIANT: We are not overwriting the last byte - self.buffer[self.pos..self.pos + len].copy_from_slice(bytes); - - self.pos +=3D len; - - Ok(()) - } -} --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 E23272E92D7; Fri, 15 Aug 2025 07:32:31 +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=1755243152; cv=none; b=oT5epJXS3FpO3RXUsRdL+/JAYvPZbZnoWzbnx4/16rRzBEGwAniw1Ryj+UXHkaFANp3ffXaN2LaDNB3LbDLYEPTBKku7114yEJP4Az02395typgeZN9WAljR3gtBGYr3pG3k1JS4U6wdCLz8BEfG1+WkJd0VnkH/PPrllKvLVcU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243152; c=relaxed/simple; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=CuBsVO6G0HXCyk5PmsU7lLq2Nh4uQZ36RXAxq0TSGxj8zJsrcO2S6U3WwcCANtzJbAj8GypHFgVkhY3kV0jIArz+dmr7ItjaZLfqQbipMqKMLsNvpSvz6Q/zFvjmi1tHK06jQb4LYNZ/JZC7KR6rXNiSWbizyGB64wkNQaOCNtg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gjENElwt; 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="gjENElwt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EC03AC4CEEB; Fri, 15 Aug 2025 07:32:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243151; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gjENElwtmJDtxbTFqY/YSgIyam/48XuoCV9bTeW36l4zDHW+EYUGFNKpNPHLbPeCw Cey4rIdvGy3+5BwzwTCIvsxcJOduJExGcCEtoZqHgORrpSoA7NWTNdh/1UkTlbqFJN mqAdDqBYjoLTKsMv4xDnJFmCGdQoPX8cDjc2WPLc/OBCqO4omOJsgjRn7+v1BKavhy FkaXNdIdBePWVI6BKOgCYGsUtHcFR/wW7UsaYP24Tku2VBsIPhuNsNCnWHb3BGCyBk sjSvDAg+tBUZJCWVMKYj/51M524pHaHyYjc1uqXmkHMOH0BMl4L3t55rEj7I1BFoYv sKzzY1VrwhjFg== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:46 +0200 Subject: [PATCH v5 11/18] rust: block: remove trait bound from `mq::Request` definition Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-11-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=963; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI9pjtmLRwmoX/9a7niCXIWqdqwqVeO2nSf1 hcJhyJmO5+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iPQAKCRDhuBo+eShj d/fFEACUwhkVewPjgePT9oEM5w6sLKE+pctmwFWP7VEg1Hmk8Qm7D1d1A0uf+6NlKq1aHayNcYr htpmuRZPasMpAgRXqGKEPoLSx6/R3ep2n0STDv4OmDpfpNn9fuf4Rtt11ZmG17ExsMIjErwqUJj omd9BPEm+tgMBm2Veq9R4fM3wtv9PHmOmJKIydmnVr6gvEakRdykSzdbt7taQSt8zYX1Pk1dpk5 frpDDZavn6gEtdzYC0GI23Zp4L5fWfutat1tXrMAjJ+9Sm7PQOH4TKfpVjrsFQBzdqGlXBLbQKg bOLjkFQcqj46b0HQW/TEYW98eFaZakYcUjMTOmL5wuaY8+36s9B/zjqpnupPTF3Y6q/VAxYgrNp UJ3v/lFk2cay5Ms2WhBudOP2511Aq/qnIGk/URK/kFPQS4b7KznVWbUp3qvsLOtDA1gatuZDCXp Tak1lrkAmjYN2uyMDIEneHg70P+fr7hF0PvpQGJubIDmQdIT0o9XWiXhpRW38fNcnyQziwHD2lj idaXRsOS25kWE+yV6JqI3dC8kheUsF/DfYXnbXIBVfNVmUYcLF1OYZR5+W9XJBDpy76J8BoRd4n s6abFa9Jxk1cECa6BL4WtRU/bBblg0pexk0rGO91Yl7RT18JrxNSSAh1stbNvZFL5FYWa3Cuy6b KO2YqJszJEhIv1w== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Remove the trait bound `T:Operations` from `mq::Request`. The bound is not required, so remove it to reduce complexity. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index fefd394f064a..f723d74091c1 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -53,7 +53,7 @@ /// [`struct request`]: srctree/include/linux/blk-mq.h /// #[repr(transparent)] -pub struct Request(Opaque, PhantomData); +pub struct Request(Opaque, PhantomData); =20 impl Request { /// Create an [`ARef`] from a [`struct request`] pointer. --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 9CB441862; Fri, 15 Aug 2025 07:31:41 +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=1755243101; cv=none; b=HZKaveQzXqvcXLi7ky/Rw+KagyfXYdMChG5HLvx/zFcyjOxTi11o+cR8XRKloy9ftInsQrucF1NbsqznslgziBe1IJmL1zVv/VZObnVWqxUWRXvw+JoNIj8qjEs5MXWArwc+fdqK5ulLSFvWQ3+KeBNnNblJxSc/hbKBHSHALVo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243101; c=relaxed/simple; bh=pzI+PzwBdiJXG7Rnf/MUlf5BoxNaAVR/4KH77t5lHuM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=A5E/lZCmE7lLcrgUNx4+OZ3SkDHCXMW9SMaETg0T0gTqs3CT+rIprJyXYB+0kq1KOlzafQDrj+n82Vu6gyu1rwK48EG4x5CvCk6FFMIVd4zBxPZy9fqRpAJHzgJ27rP91D5JTx/l3aoSsxH/gC8VtWOaAxduQl1M6ZPY4Jq3yWg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oQWJKGP6; 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="oQWJKGP6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EFDEC4CEEB; Fri, 15 Aug 2025 07:31:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243101; bh=pzI+PzwBdiJXG7Rnf/MUlf5BoxNaAVR/4KH77t5lHuM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oQWJKGP6gtCw1h1qH9RTOdClZX1YTDilo23ktLBDHUigtmARP4mX7tFmeptF0xHS6 unKZbkhn3Ey3Mfj9uSR+ssvoXd/7C5hvBLxhhXo04fliJ948lpaFjS1QJHZWocWeBo NxLdjQo7+NyVc934wLn2hJnGcBKSLSElwNk/QBe4c8CxPQgPSCsJl3IeIQB9uocwt6 yMGJCS+q7aGsmtvpsYvNIH9dq25oIDcs370kB95QNn+h15nxPC/SWtC512iFgTH+d2 Bd5JdcqWL2F9GRfvIW2mkT7986By3+PPD35F9+rTEYsC2765NINY/BAvI0U67xJSOe l3FD4eQbBEDAw== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:47 +0200 Subject: [PATCH v5 12/18] rust: block: add block related constants Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-12-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1050; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=pzI+PzwBdiJXG7Rnf/MUlf5BoxNaAVR/4KH77t5lHuM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI+NR1bm5f6cjdHO68QuIpWUgAQN2aK+HG51 YYTG3A+YBWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iPgAKCRDhuBo+eShj d51lEAC7gK4el8MDA3IF/dUMneYYCjOWN/qhhObe1yJ8CTdt16cTDagRa0ww86UJ6KXUvEUBG6J 9hvJopnLvcVT2uGNLJ8NH5Z15ntMirhKW+dflkmPZM+nRSDk79fvsrGMerrJoK8hcGybBnleenc A4jyFOvnq8MDqXAnRdxZ8c3vDplAALdhcBS0Vz0xcnkHxBOTXUyLR0K2jvXxKrqv9RQuKbtFc2l VEAe+NCN1+9RtsZ0cWwdGBpLokAUV1u0loXjQSZI+yRyiDnUHt1bg/lMV4p11uLGsdNRKbO+kwW usIGxdTCW8VtiNcHgFn3PkUqYWROGxE81iJCGD8IX+iV2zBmjDaOjtQ8U6VIMYaW5Bk/FqBVBzF cU1gyr4C49QGJkjmtfIwa+5DTwWR7LrifMIcUgJMBSc5ZUKWAwRevdjQZSemJ/29o2kOSwXkQXU NYCmKDMBsztu5EhK0VjtdS6RcovlBO1vJxmwaoCvrJYtcC+vyIh3naGzT39bHav6X9h8HpncIcR pBGmABThZD51BgwSNJdlYtTU8+jElIY7IFNXy/lfVOOBFV2i7mg95YdLxt7c6n5FELoSfWYo5C/ FVoKQWowg7yLS7fT0i36tYi6JsbolEjCrULiO/h3PuIqM7Gun+up5C6vEVoVBIuSjiia7Th5GL9 if1hvdqt/BzIA3A== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a few block subsystem constants to the rust `kernel::block` name space. This makes it easier to access the constants from rust code. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/block.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rust/kernel/block.rs b/rust/kernel/block.rs index 150f710efe5b..32c8d865afb6 100644 --- a/rust/kernel/block.rs +++ b/rust/kernel/block.rs @@ -3,3 +3,16 @@ //! Types for working with the block layer. =20 pub mod mq; + +/// Bit mask for masking out [`SECTOR_SIZE`]. +pub const SECTOR_MASK: u32 =3D bindings::SECTOR_MASK; + +/// Sectors are size `1 << SECTOR_SHIFT`. +pub const SECTOR_SHIFT: u32 =3D bindings::SECTOR_SHIFT; + +/// Size of a sector. +pub const SECTOR_SIZE: u32 =3D bindings::SECTOR_SIZE; + +/// The difference between the size of a page and the size of a sector, +/// expressed as a power of two. +pub const PAGE_SECTORS_SHIFT: u32 =3D bindings::PAGE_SECTORS_SHIFT; --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 AC6541862; Fri, 15 Aug 2025 07:32:11 +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=1755243131; cv=none; b=EX9zDYKHhOhpe84rRDEr226n+0H50BvWf1seuyOvJzSEgkq0SFrc60SF/OaW8sd6d1ZdGEj1Zz69zOlHvfxNLzJAPM4qJpC1ci3vpTw/JRmyraRpswadMOgaotm/1pwAA4mDdNb9W/4wPjA6XyjfSIPx+LlUmhWKUBCD72xARLQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243131; c=relaxed/simple; bh=tDTjZd+5UIJEcvpFCoWeOEwqbYXX//kOhz1DDrET25c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ieTWhbmny6IFR1bMRUbXXf7z3IuwAfQACA38YHK+NkwUpJ+S0Ks2ghbCE0OYOMboEmpPfowxSlbM/w9x1scRDVqHc+wp2yg4/h8q3VTEBxF+53wjm9LLHGaUqbdCOgpnAPzJ92bPp54RcupytAoSwcvHjk3ZVhd1mkdATmLJF1c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YDcbsEV1; 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="YDcbsEV1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8788AC4CEEB; Fri, 15 Aug 2025 07:32:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243131; bh=tDTjZd+5UIJEcvpFCoWeOEwqbYXX//kOhz1DDrET25c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=YDcbsEV17vKg5yNg3jhua6TpfMDCrYxRYSYApBzAJRjXbFvvTDgavmn+GwaOfQPl/ aOOB2YhoKPwGB89x/n+KPrCp8fS/b1Tr/+W/T0GgM1/loMRmZHRWbC3EsBrbwYCoNO hi7e8Al3LC4M22WZb1CArlLbvYJeJoj0aL3B18kANSLAmagwksniceeGe4hp44rKot DtSr2x2HJc3DX81Efuy1rmmeYnWzl4d2wYmmOvmwPRURwnkxVxyFzuZY17v+TaE/X5 x4KcDDy1nhk95JgUF5WNlkgKV0pLZ0KFy2wGAPv/tUaeLh23zHd555xueoxf+ga0rI bFGqPp+VggiUQ== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:48 +0200 Subject: [PATCH v5 13/18] rnull: move driver to separate directory Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-13-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3780; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=tDTjZd+5UIJEcvpFCoWeOEwqbYXX//kOhz1DDrET25c=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuI/zppQBSh4S5PYaROLGl1XUozcNfMmlZp+h UE23fYlUGWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iPwAKCRDhuBo+eShj dyjzD/sEfxmAJJGV82v1PYByIK6AiUOX96rXEi/OAnsFfL5LqQ+6kSE/69YAqRcHbUDbpOaBxPf g1uW/2blFH06dtp4FPI8tiQWGnl0erMVSzp0UqOsrSThL+iupGCYIJHBQfMKZml3eTYY48Jbdjb wilRQrUnGbweyE3983kMSM6cB9b/3E1+2wE80p9bmfKnkz6Gjghwttwhep4bT6ZbU5og/fAiI5d z+OqPsQ9wuBbieToQMLzpx15J8acg+tyqMPzbtCvkKMkquVIc9Yeyl8ZgDk5kp2kK4YBbytTNbU aG2YxsQom3Rn1La48g5pPXLOZdvy9TX0fE9CMxzSUw1e0hE1ET+Je6+srbMO2TrkICrD5klvSGX cXnqsNVmJE3Jnjaxj1BIkGwgVmZ+NS26SkPFfJZAUFG+WuQH5K79R/7eblZ98ow9cSL6wBXT9tG bNVqKce9t6x0uSGEkLHQ6mqzgx3MfuYSBCu2FOPWzDGK7BeS909JPFvp8niHJ6I0FR6wOIm4sKz sIEavpa5JBw28aTHUsEtu6rE/wn/fJJrlhgyGkMHxfHpHhOkbCB5Rju6SuiCc6shTJ6TIPTPdhH zOd0AqCeo8JRTT9FjlEWlrKES8gJsZoBN8ZQzwfYNG2fBwtTLao6BKIXaHrpNhvLcoLkOdNw2Zl rHLc1kJQmoofZfg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 The rust null block driver is about to gain some additional modules. Rather than pollute the current directory, move the driver to a subdirectory. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- MAINTAINERS | 2 +- drivers/block/Kconfig | 10 +--------- drivers/block/Makefile | 4 +--- drivers/block/rnull/Kconfig | 13 +++++++++++++ drivers/block/rnull/Makefile | 3 +++ drivers/block/{ =3D> rnull}/rnull.rs | 0 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index fe168477caa4..238ff10f537d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4343,7 +4343,7 @@ W: https://rust-for-linux.com B: https://github.com/Rust-for-Linux/linux/issues C: https://rust-for-linux.zulipchat.com/#narrow/stream/Block T: git https://github.com/Rust-for-Linux/linux.git rust-block-next -F: drivers/block/rnull.rs +F: drivers/block/rnull/ F: rust/kernel/block.rs F: rust/kernel/block/ =20 diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig index df38fb364904..77d694448990 100644 --- a/drivers/block/Kconfig +++ b/drivers/block/Kconfig @@ -17,6 +17,7 @@ menuconfig BLK_DEV if BLK_DEV =20 source "drivers/block/null_blk/Kconfig" +source "drivers/block/rnull/Kconfig" =20 config BLK_DEV_FD tristate "Normal floppy disk support" @@ -311,15 +312,6 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based VMMs (like KVM or Xen). Say Y or M. =20 -config BLK_DEV_RUST_NULL - tristate "Rust null block driver (Experimental)" - depends on RUST - help - This is the Rust implementation of the null block driver. For now it - is only a minimal stub. - - If unsure, say N. - config BLK_DEV_RBD tristate "Rados block device (RBD)" depends on INET && BLOCK diff --git a/drivers/block/Makefile b/drivers/block/Makefile index a695ce74ef22..2d8096eb8cdf 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -9,9 +9,6 @@ # needed for trace events ccflags-y +=3D -I$(src) =20 -obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull_mod.o -rnull_mod-y :=3D rnull.o - obj-$(CONFIG_MAC_FLOPPY) +=3D swim3.o obj-$(CONFIG_BLK_DEV_SWIM) +=3D swim_mod.o obj-$(CONFIG_BLK_DEV_FD) +=3D floppy.o @@ -38,6 +35,7 @@ obj-$(CONFIG_ZRAM) +=3D zram/ obj-$(CONFIG_BLK_DEV_RNBD) +=3D rnbd/ =20 obj-$(CONFIG_BLK_DEV_NULL_BLK) +=3D null_blk/ +obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull/ =20 obj-$(CONFIG_BLK_DEV_UBLK) +=3D ublk_drv.o obj-$(CONFIG_BLK_DEV_ZONED_LOOP) +=3D zloop.o diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig new file mode 100644 index 000000000000..6dc5aff96bf4 --- /dev/null +++ b/drivers/block/rnull/Kconfig @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Rust null block device driver configuration + +config BLK_DEV_RUST_NULL + tristate "Rust null block driver (Experimental)" + depends on RUST + help + This is the Rust implementation of the null block driver. Like + the C version, the driver allows the user to create virutal block + devices that can be configured via various configuration options. + + If unsure, say N. diff --git a/drivers/block/rnull/Makefile b/drivers/block/rnull/Makefile new file mode 100644 index 000000000000..11cfa5e615dc --- /dev/null +++ b/drivers/block/rnull/Makefile @@ -0,0 +1,3 @@ + +obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull_mod.o +rnull_mod-y :=3D rnull.o diff --git a/drivers/block/rnull.rs b/drivers/block/rnull/rnull.rs similarity index 100% rename from drivers/block/rnull.rs rename to drivers/block/rnull/rnull.rs --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 032B12D0C71; Fri, 15 Aug 2025 07:32:04 +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=1755243125; cv=none; b=AjVp7l7WsDZIYsEkXZ87LsjcwgW8pAlx7tsZAl+e2n+NkiE7kMYnQ2rDdaux92IvOwvM0bDe+T/t+H+DBq6KMv1yE3Rj9cLkg6sRsrCNZ7EwpjK6spWvzNHIsa4HoOrEe7XSnqOVWAzR1hIaBmgqzUzUg4JtdoJFk+umhz2BHFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243125; c=relaxed/simple; bh=OGLyatEXOCPSpOutMp50W+e7q9fc8iP/qLDZApYU/wQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ubrmKg4ENkx/ffOtw95mYvfFGWovHxGBrbMf7IacPVTku8awfrdkTIc5zxwpJHcpHVvn2Dlv1msvc2R3OI+0SK3vqQ3DcmB1U31BknwQFEkXTTpcByi6NRVptGy3cX0Apx7qwcseqqeO+eJ3PIgdWDiSeHyBxq/WYlOd/fcPYDQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pZT/Xr9l; 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="pZT/Xr9l" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B49D4C4CEEB; Fri, 15 Aug 2025 07:32:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243124; bh=OGLyatEXOCPSpOutMp50W+e7q9fc8iP/qLDZApYU/wQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=pZT/Xr9lcJS+kpOQclaEAhxmM2ImqA+V4ArodtmUa/v5y+kMsWWzfvqKRWXQq8i3f w5Jvc/5AkZN9lgD/9vOuiZAhiyphLaS3op87YP3SsHhdyoG4IoY+JiVUPxzlX02Coi TvCPdU162x69JFf/slKq4KekW0x6/zefNJU+/QsasHnNvJBmHNnPkPlktRaJUiyFk7 S8Xs9YFGj12HQeDIJz3XzsQLECG6sg4vjD2nbGet9Z/6Ph4AuAMA3zlYLCiHm9jtth Py1Dne3CaYckwK4SWAQrheCMKznUpuGf+N2nPSZh2b1SmUfaLs84en0Ownm8ZdjA1h KmdLMl5Gdx1SQ== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:49 +0200 Subject: [PATCH v5 14/18] rnull: enable configuration via `configfs` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-14-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=10499; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=OGLyatEXOCPSpOutMp50W+e7q9fc8iP/qLDZApYU/wQ=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuJAX11XWdNUyN5BPEJbFLt5McVOB75cxdKUk Vp67bFyrQ6JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iQAAKCRDhuBo+eShj d1zbEADBbi/U6j0KTrNt4jJdVzPUkpkwkak+GpR2QiFfCFdc8EcfivgavZPOKX63tO+nI0zVIU4 WVjH1w7iStl/71yWyPfgqu5pAJFHv1oaeWYHijikp4gsaPBKK8bxx6lzeBtoF+PF7dNChW/p+nx H7LALFDLXrQW0BjrMvA7QHmH6z5/Fcu+qlLgSLAkCKDtZSKX84yK/RtTwenNq/2X5FwNXXVxWXQ V4KppvOZ1jmshOQOprCToz8tSq4FuZYIDbqQfM950UT9knNlBemOd0mC7UqdnODvAy2keNU2QT7 CBZRKCeUSQDLwGLzQSWMxUlE7v4tQu2LNOGuVLOpGd00mr6KnsYo4ufjtQOmLz4YjNUbyjLx2/m /g4FREoZJYewi32d1suLBvsRiGjgoXiZLkbTpS1rt/rJyMq4sI9aDvjrjVrSAsYknNSKQ8uJMrx zLUz0i2e3UTFlulYCKqnIeXCQyPr4bZhpuAbB4lnlIcJCdFY3QDSC/R6SWmRRwhJW5u6WFzKlQz xXWffseRQZKnvE9j8nuoL8+0OkgO+jXt40e07J4XbDwcLwBWoMBwXxpFOqP7pZiUpFGxlRYNsLK srVt8VXylN0X65AELY1+rfXt6lqEv7lv2Io9kBY7pmOJFdeL6Vk0oauYSrDKAVi0ogJxzCVkU2p /nDQMhDoU5ZU77w== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow rust null block devices to be configured and instantiated via `configfs`. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- drivers/block/rnull/Kconfig | 2 +- drivers/block/rnull/configfs.rs | 207 +++++++++++++++++++++++++++++++++++= ++++ drivers/block/rnull/rnull.rs | 58 ++++++----- rust/kernel/block/mq/gen_disk.rs | 2 +- 4 files changed, 240 insertions(+), 29 deletions(-) diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig index 6dc5aff96bf4..7bc5b376c128 100644 --- a/drivers/block/rnull/Kconfig +++ b/drivers/block/rnull/Kconfig @@ -4,7 +4,7 @@ =20 config BLK_DEV_RUST_NULL tristate "Rust null block driver (Experimental)" - depends on RUST + depends on RUST && CONFIGFS_FS help This is the Rust implementation of the null block driver. Like the C version, the driver allows the user to create virutal block diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs= .rs new file mode 100644 index 000000000000..488fe27f83a2 --- /dev/null +++ b/drivers/block/rnull/configfs.rs @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::{NullBlkDevice, THIS_MODULE}; +use core::fmt::Write; +use kernel::{ + block::mq::gen_disk::{GenDisk, GenDiskBuilder}, + c_str, + configfs::{self, AttributeOperations}, + configfs_attrs, new_mutex, + page::PAGE_SIZE, + prelude::*, + str::{bytes_to_bool, CString}, + sync::Mutex, +}; +use pin_init::PinInit; + +pub(crate) fn subsystem() -> impl PinInit, Error> { + let item_type =3D configfs_attrs! { + container: configfs::Subsystem, + data: Config, + child: DeviceConfig, + attributes: [ + features: 0, + ], + }; + + kernel::configfs::Subsystem::new(c_str!("rnull"), item_type, try_pin_i= nit!(Config {})) +} + +#[pin_data] +pub(crate) struct Config {} + +#[vtable] +impl AttributeOperations<0> for Config { + type Data =3D Config; + + fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_str("blocksize,size,rotational\n")?; + Ok(writer.bytes_written()) + } +} + +#[vtable] +impl configfs::GroupOperations for Config { + type Child =3D DeviceConfig; + + fn make_group( + &self, + name: &CStr, + ) -> Result, Error>> { + let item_type =3D configfs_attrs! { + container: configfs::Group, + data: DeviceConfig, + attributes: [ + // Named for compatibility with C null_blk + power: 0, + blocksize: 1, + rotational: 2, + size: 3, + ], + }; + + Ok(configfs::Group::new( + name.try_into()?, + item_type, + // TODO: cannot coerce new_mutex!() to impl PinInit<_, Error>,= so put mutex inside + try_pin_init!( DeviceConfig { + data <- new_mutex!(DeviceConfigInner { + powered: false, + block_size: 4096, + rotational: false, + disk: None, + capacity_mib: 4096, + name: name.try_into()?, + }), + }), + )) + } +} + +#[pin_data] +pub(crate) struct DeviceConfig { + #[pin] + data: Mutex, +} + +#[pin_data] +struct DeviceConfigInner { + powered: bool, + name: CString, + block_size: u32, + rotational: bool, + capacity_mib: u64, + disk: Option>, +} + +#[vtable] +impl configfs::AttributeOperations<0> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + + if this.data.lock().powered { + writer.write_str("1\n")?; + } else { + writer.write_str("0\n")?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + let power_op =3D bytes_to_bool(page)?; + let mut guard =3D this.data.lock(); + + if !guard.powered && power_op { + guard.disk =3D Some(NullBlkDevice::new( + &guard.name, + guard.block_size, + guard.rotational, + guard.capacity_mib, + )?); + guard.powered =3D true; + } else if guard.powered && !power_op { + drop(guard.disk.take()); + guard.powered =3D false; + } + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<1> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().block_size))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + GenDiskBuilder::validate_block_size(value)?; + this.data.lock().block_size =3D value; + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<2> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + + if this.data.lock().rotational { + writer.write_str("1\n")?; + } else { + writer.write_str("0\n")?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + this.data.lock().rotational =3D bytes_to_bool(page)?; + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<3> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + this.data.lock().capacity_mib =3D value; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index d07e76ae2c13..d09bc77861e4 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -1,28 +1,26 @@ // SPDX-License-Identifier: GPL-2.0 =20 //! This is a Rust implementation of the C null block driver. -//! -//! Supported features: -//! -//! - blk-mq interface -//! - direct completion -//! - block size 4k -//! -//! The driver is not configurable. + +mod configfs; =20 use kernel::{ alloc::flags, - block::mq::{ + block::{ self, - gen_disk::{self, GenDisk}, - Operations, TagSet, + mq::{ + self, + gen_disk::{self, GenDisk}, + Operations, TagSet, + }, }, error::Result, - new_mutex, pr_info, + pr_info, prelude::*, - sync::{Arc, Mutex}, + sync::Arc, types::ARef, }; +use pin_init::PinInit; =20 module! { type: NullBlkModule, @@ -35,33 +33,39 @@ #[pin_data] struct NullBlkModule { #[pin] - _disk: Mutex>, + configfs_subsystem: kernel::configfs::Subsystem, } =20 impl kernel::InPlaceModule for NullBlkModule { fn init(_module: &'static ThisModule) -> impl PinInit { pr_info!("Rust null_blk loaded\n"); =20 - // Use a immediately-called closure as a stable `try` block - let disk =3D /* try */ (|| { - let tagset =3D Arc::pin_init(TagSet::new(1, 256, 1), flags::GF= P_KERNEL)?; - - gen_disk::GenDiskBuilder::new() - .capacity_sectors(4096 << 11) - .logical_block_size(4096)? - .physical_block_size(4096)? - .rotational(false) - .build(format_args!("rnullb{}", 0), tagset) - })(); - try_pin_init!(Self { - _disk <- new_mutex!(disk?, "nullb:disk"), + configfs_subsystem <- configfs::subsystem(), }) } } =20 struct NullBlkDevice; =20 +impl NullBlkDevice { + fn new( + name: &CStr, + block_size: u32, + rotational: bool, + capacity_mib: u64, + ) -> Result> { + let tagset =3D Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KE= RNEL)?; + + gen_disk::GenDiskBuilder::new() + .capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT)) + .logical_block_size(block_size)? + .physical_block_size(block_size)? + .rotational(rotational) + .build(fmt!("{}", name.to_str()?), tagset) + } +} + #[vtable] impl Operations for NullBlkDevice { #[inline(always)] diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 20f1d46c774d..6b1b846874db 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -51,7 +51,7 @@ pub fn rotational(mut self, rotational: bool) -> Self { =20 /// Validate block size by verifying that it is between 512 and `PAGE_= SIZE`, /// and that it is a power of two. - fn validate_block_size(size: u32) -> Result { + pub fn validate_block_size(size: u32) -> Result { if !(512..=3Dbindings::PAGE_SIZE as u32).contains(&size) || !size.= is_power_of_two() { Err(error::code::EINVAL) } else { --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 DE5DC2D0C81; Fri, 15 Aug 2025 07:31:44 +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=1755243107; cv=none; b=Kb89FRd7zhq34Qa/O45lsGboKsMlTDjvPPXl04E+VTEavZWlBjmeGGjZj5Z/h8JoHWeF+VOte4BetobJUEMZd52OxpMJAzagNXJ5fnPWEpwTndhAJCJ657u0Pcv5euFTI2U/doYKlPRE78u2oglWyZRLMGX+Nd7xzpIFImLeFw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243107; c=relaxed/simple; bh=iamvcpEdrA0t94Fax2Hfc94uf68XfdtGwPHLFZIDasU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=oYVVWwDtAUrSD6z8tgehvMwT3cshO2f+5JN1oiP1GVYYwNuRdsbDUKY+9a963Z4M8yifwfTPo58MkA/O/TAVLEqbidSPmGWBf/4vwetXtLxvu+4fH39W7jW8jJgGPSZ7WL59QPYZ0hv14/Et/4t44/zY06il3wV++0liLQmwUHA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e9yDbtqb; 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="e9yDbtqb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A3069C4CEF4; Fri, 15 Aug 2025 07:31:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243104; bh=iamvcpEdrA0t94Fax2Hfc94uf68XfdtGwPHLFZIDasU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=e9yDbtqbep4baLG42eBab5yyqvWNxTY3m51GB2djsUGN28ENjYTkWVXRTUJhBtXaj eSaRu7eZwsqfWXrLGTGTxfDocP/lJOknMHYEFVrvWyQ0Dt8UFQlgFImylBr2CTknaW oT/XccqEw0GmVZFPiHpMmOiKrx+LxVQ5F+JtVzD+7NSNcq6wSy7QSuRAaI+mB/pl6l HVtaIyFDZt6aNh52BTTJDI5/Z0mNFFiijXgtQtiXB9nbzBMNOQyU87YSL9wzWBNpZY GBJMl/XYjECA8uZKVgWJyOhOgDpMdlfOwgEbQMSpGZgGZuK+BCGYiY9hIzws8v5GT9 fAAuVUpJCzYyQ== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:50 +0200 Subject: [PATCH v5 15/18] rust: block: add `GenDisk` private data support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-15-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=10528; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=iamvcpEdrA0t94Fax2Hfc94uf68XfdtGwPHLFZIDasU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuJBmc3onlnFa71wVNzXzx82KMIaZZ449IcX8 WaPjXCquOiJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iQQAKCRDhuBo+eShj dzrjD/9U1EapnRQqOPlm8EoUQZDlgbvgN9HOIIq8Ma9h7xEBKd0aWEIpuDctQ+alKoRn5Z0FUDj HVuXxpbBl4jW+NXRQdW4Fdlq4quk7RXXq9kDGuCWnyCBvN+GHzQ2NNPVYonH89fCI2G0moMQn66 CD9CT9mm5rtvaAopqSauCpZhSvngKBxykY8CtxwGuWa9vjru7Gy2U8Z5LDMA7vpk65cZMVO4P4p r6QfOazl+IKZd91FB9xFTGmZYbhroHdVW+6bAeHPqcNhDEE+3jjFzq38lBtZKG2aCm24cFFJvFY 42h4OniUPHJyGd2dt3TIqNDFzzkAIsJxV3SxdBSU3HwRQWBKthqU+8TFdOiTOdfweaLNfgypDQ/ pSnZL5f5czenNDDlJeQuQyAsgje7CebfMv1h30tYCQS05qeX8NpI+V9XTuVkkX++pE66fM4wWxC ELcI4KZimO/dsc1PMZ/otV5YyKbneTMHvxOTNJxOb7P4/ZMQw92Lr7gLMOC0OUwGiDdT6ZZabOv Gp68tYcx4zS5V3di1R+FaCp7zqWg+8nzzDjYO7hKW4bFP195QN1NXnFrZzI4IIaJ1zCaA1HEAgZ n+ArpKDq98HH0YzoIgl/TdVr/wKX9GyxRtnlJWJ5iKOM0JjO9hK+EewKfmSpDDnHSfbpg1fj9we 56sMCbgCrv1Pamg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow users of the rust block device driver API to install private data in the `GenDisk` structure. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- drivers/block/rnull/rnull.rs | 8 ++++--- rust/kernel/block/mq.rs | 7 +++--- rust/kernel/block/mq/gen_disk.rs | 32 ++++++++++++++++++++++---- rust/kernel/block/mq/operations.rs | 46 ++++++++++++++++++++++++++++++----= ---- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index d09bc77861e4..a012c59ecb3c 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -62,14 +62,16 @@ fn new( .logical_block_size(block_size)? .physical_block_size(block_size)? .rotational(rotational) - .build(fmt!("{}", name.to_str()?), tagset) + .build(fmt!("{}", name.to_str()?), tagset, ()) } } =20 #[vtable] impl Operations for NullBlkDevice { + type QueueData =3D (); + #[inline(always)] - fn queue_rq(rq: ARef>, _is_last: bool) -> Result { + fn queue_rq(_queue_data: (), rq: ARef>, _is_last: bo= ol) -> Result { mq::Request::end_ok(rq) .map_err(|_e| kernel::error::code::EIO) // We take no refcounts on the request, so we expect to be abl= e to @@ -80,5 +82,5 @@ fn queue_rq(rq: ARef>, _is_last: bool) = -> Result { Ok(()) } =20 - fn commit_rqs() {} + fn commit_rqs(_queue_data: ()) {} } diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 98fa0d6bc8f7..6e546f4f3d1c 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -69,20 +69,21 @@ //! //! #[vtable] //! impl Operations for MyBlkDevice { +//! type QueueData =3D (); //! -//! fn queue_rq(rq: ARef>, _is_last: bool) -> Result { +//! fn queue_rq(_queue_data: (), rq: ARef>, _is_last: bo= ol) -> Result { //! Request::end_ok(rq); //! Ok(()) //! } //! -//! fn commit_rqs() {} +//! fn commit_rqs(_queue_data: ()) {} //! } //! //! let tagset: Arc> =3D //! Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?; //! let mut disk =3D gen_disk::GenDiskBuilder::new() //! .capacity_sectors(4096) -//! .build(format_args!("myblk"), tagset)?; +//! .build(format_args!("myblk"), tagset, ())?; //! //! # Ok::<(), kernel::error::Error>(()) //! ``` diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 6b1b846874db..ac7a33a00aaa 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -13,6 +13,7 @@ static_lock_class, str::NullTerminatedFormatter, sync::Arc, + types::{ForeignOwnable, ScopeGuard}, }; use core::fmt::{self, Write}; =20 @@ -98,7 +99,14 @@ pub fn build( self, name: fmt::Arguments<'_>, tagset: Arc>, + queue_data: T::QueueData, ) -> Result> { + let data =3D queue_data.into_foreign(); + let recover_data =3D ScopeGuard::new(|| { + // SAFETY: T::QueueData was created by the call to `into_forei= gn()` above + drop(unsafe { T::QueueData::from_foreign(data) }); + }); + // SAFETY: `bindings::queue_limits` contain only fields that are v= alid when zeroed. let mut lim: bindings::queue_limits =3D unsafe { core::mem::zeroed= () }; =20 @@ -113,7 +121,7 @@ pub fn build( bindings::__blk_mq_alloc_disk( tagset.raw_tag_set(), &mut lim, - core::ptr::null_mut(), + data.cast(), static_lock_class!().as_ptr(), ) })?; @@ -167,8 +175,12 @@ pub fn build( }, )?; =20 + recover_data.dismiss(); + // INVARIANT: `gendisk` was initialized above. // INVARIANT: `gendisk` was added to the VFS via `device_add_disk`= above. + // INVARIANT: `gendisk.queue.queue_data` is set to `data` in the c= all to + // `__blk_mq_alloc_disk` above. Ok(GenDisk { _tagset: tagset, gendisk, @@ -180,9 +192,10 @@ pub fn build( /// /// # Invariants /// -/// - `gendisk` must always point to an initialized and valid `struct gend= isk`. -/// - `gendisk` was added to the VFS through a call to -/// `bindings::device_add_disk`. +/// - `gendisk` must always point to an initialized and valid `struct gen= disk`. +/// - `gendisk` was added to the VFS through a call to +/// `bindings::device_add_disk`. +/// - `self.gendisk.queue.queuedata` is initialized by a call to `Foreign= Ownable::into_foreign`. pub struct GenDisk { _tagset: Arc>, gendisk: *mut bindings::gendisk, @@ -194,9 +207,20 @@ unsafe impl Send for GenDisk = {} =20 impl Drop for GenDisk { fn drop(&mut self) { + // SAFETY: By type invariant of `Self`, `self.gendisk` points to a= valid + // and initialized instance of `struct gendisk`, and, `queuedata` = was + // initialized with the result of a call to + // `ForeignOwnable::into_foreign`. + let queue_data =3D unsafe { (*(*self.gendisk).queue).queuedata }; + // SAFETY: By type invariant, `self.gendisk` points to a valid and // initialized instance of `struct gendisk`, and it was previously= added // to the VFS. unsafe { bindings::del_gendisk(self.gendisk) }; + + // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build= ` with + // a call to `ForeignOwnable::into_foreign` to create `queuedata`. + // `ForeignOwnable::from_foreign` is only called here. + drop(unsafe { T::QueueData::from_foreign(queue_data) }); } } diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index c2b98f507bcb..6fb256f55acc 100644 --- a/rust/kernel/block/mq/operations.rs +++ b/rust/kernel/block/mq/operations.rs @@ -6,14 +6,15 @@ =20 use crate::{ bindings, - block::mq::request::RequestDataWrapper, - block::mq::Request, + block::mq::{request::RequestDataWrapper, Request}, error::{from_result, Result}, prelude::*, - types::ARef, + types::{ARef, ForeignOwnable}, }; use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ord= ering}; =20 +type ForeignBorrowed<'a, T> =3D ::Borrowed<'a>; + /// Implement this trait to interface blk-mq as block devices. /// /// To implement a block device driver, implement this trait as described = in the @@ -26,12 +27,20 @@ /// [module level documentation]: kernel::block::mq #[macros::vtable] pub trait Operations: Sized { + /// Data associated with the `struct request_queue` that is allocated = for + /// the `GenDisk` associated with this `Operations` implementation. + type QueueData: ForeignOwnable; + /// Called by the kernel to queue a request with the driver. If `is_la= st` is /// `false`, the driver is allowed to defer committing the request. - fn queue_rq(rq: ARef>, is_last: bool) -> Result; + fn queue_rq( + queue_data: ForeignBorrowed<'_, Self::QueueData>, + rq: ARef>, + is_last: bool, + ) -> Result; =20 /// Called by the kernel to indicate that queued requests should be su= bmitted. - fn commit_rqs(); + fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); =20 /// Called by the kernel to poll the device for completed requests. On= ly /// used for poll queues. @@ -70,7 +79,7 @@ impl OperationsVTable { /// promise to not access the request until the driver calls /// `bindings::blk_mq_end_request` for the request. unsafe extern "C" fn queue_rq_callback( - _hctx: *mut bindings::blk_mq_hw_ctx, + hctx: *mut bindings::blk_mq_hw_ctx, bd: *const bindings::blk_mq_queue_data, ) -> bindings::blk_status_t { // SAFETY: `bd.rq` is valid as required by the safety requirement = for @@ -88,10 +97,20 @@ impl OperationsVTable { // reference counted by `ARef` until then. let rq =3D unsafe { Request::aref_from_raw((*bd).rq) }; =20 + // SAFETY: `hctx` is valid as required by this function. + let queue_data =3D unsafe { (*(*hctx).queue).queuedata }; + + // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` w= ith a + // call to `ForeignOwnable::into_pointer()` to create `queuedata`. + // `ForeignOwnable::from_foreign()` is only called when the tagset= is + // dropped, which happens after we are dropped. + let queue_data =3D unsafe { T::QueueData::borrow(queue_data) }; + // SAFETY: We have exclusive access and we just set the refcount a= bove. unsafe { Request::start_unchecked(&rq) }; =20 let ret =3D T::queue_rq( + queue_data, rq, // SAFETY: `bd` is valid as required by the safety requirement= for // this function. @@ -110,9 +129,18 @@ impl OperationsVTable { /// /// # Safety /// - /// This function may only be called by blk-mq C infrastructure. - unsafe extern "C" fn commit_rqs_callback(_hctx: *mut bindings::blk_mq_= hw_ctx) { - T::commit_rqs() + /// This function may only be called by blk-mq C infrastructure. The c= aller + /// must ensure that `hctx` is valid. + unsafe extern "C" fn commit_rqs_callback(hctx: *mut bindings::blk_mq_h= w_ctx) { + // SAFETY: `hctx` is valid as required by this function. + let queue_data =3D unsafe { (*(*hctx).queue).queuedata }; + + // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` w= ith a + // call to `ForeignOwnable::into_pointer()` to create `queuedata`. + // `ForeignOwnable::from_foreign()` is only called when the tagset= is + // dropped, which happens after we are dropped. + let queue_data =3D unsafe { T::QueueData::borrow(queue_data) }; + T::commit_rqs(queue_data) } =20 /// This function is called by the C kernel. It is not currently --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 BFF1F2D3225; Fri, 15 Aug 2025 07:32:35 +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=1755243158; cv=none; b=kMzuZCMvyMIJRw2cdHpn9Dmug7RZm5XeGROl6hAh15Z5+EJdljaqJeAmYNaaYUXN1/WWH5IPZg862ymnAuggm5gjIiqIgoBukKpL5NUWKi9wpuY5wwrw29vC0lsQw6hCkCbhhQ3v/SAPosCR0IMaCjMeFKhpDu1HPSSr8ZKLeK8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243158; c=relaxed/simple; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AR67Scki2vOSdPpC5IM0jBGXaKU3lfcTUllmdhKPNJ0qItTJ18uOm4cU+s6QyC4ROiY6qKlsk05Kqota4ll144eBoDKWa6oEvB7u1roRPunf2Tkt46KFWf7PH9/dM5r18fxAVXC+fdOfScI6Rm8AfYjgowaD2TVSuMpiiDRhTV4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e7ALk2rI; 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="e7ALk2rI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EABEC4CEEB; Fri, 15 Aug 2025 07:32:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243155; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=e7ALk2rIl0en5LLhKCdRB1UEYnrxlvodr13tF3QL0vApm9pmb+/Q6lmKAq5mpVxIk FwNG51Whdkh2Ex0EuHYO9eL+kV4fNzhwXaXhsKfI3CfsIxrdfp/jZHq71GUt3GYaF7 0AFkjrmZfENiBjOVCm+31kaSYr5O9M0Tv095rb3+dv6G45Ym2o5JF1WzlkdJ0odVnl nKRMl6OOzkXiIqJ0tDT2P13qiTwJu84dztasi3maiITLCCLZxh6yqShfNLEzKNblrP X+mH5q8pmZxV8eJv7mHj4EGeE9p2BVd260xzr8Q2NUZ/ckcn/maMld0kPZwT4fdh44 bnU1wdCftEkcQ== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:51 +0200 Subject: [PATCH v5 16/18] rust: block: mq: fix spelling in a safety comment Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-16-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1036; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuJCJxHe2wbjvtpom9FeQGv+WU7jKTJ1zJioN 19G94REFbGJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iQgAKCRDhuBo+eShj d8UDEAClgJzpbwWJkeqPd/Xgvho4AvLt55cbsiEN7FeT/q0Wh0rkkO+RfxcT3ymWlC84tduQv8n feeUkQZ3SUbs3svPb1muGnXG3YwRzi/rkk/2jeRn1foUFvqfkcwNhGOnhTJhmKEZcMP3QQkbQtR 75cKWvnImWkTx84GBMkoin15MYHSIY9/H4P51c7uyj8Tw5xpvea+2rJCfPlbceAWkZP1LcnITPr NO8D7o7DENBjzhE8BqDV/Dyg6eQXsItFrBn3o59nKS8WFN/hwmXpu4s5HhXKoGRfn1qZMRIkZy6 rDq8mgJPpmiw9FO6JBn9b+9GKotyLlADSysC6pBij2PVfXHzTm1HeooVQbtCrqeYJlJ+rPSxyXR X3Gdrd1EJxCUifxVNBvhTmXnRBQ+Cme13y36srZsXLrUBE0oRYo15IV+6NwLbIzsD30y17U3JXS GmLzaZp4rI0AuJx1oLqRq4hPObYs80LTW1r61A4i1uQ1pyk0TQW9wS8rq4QyQtD46xx0U5avHIW qEEoZ8BV+pVoS1aIGYXNm5UDqOWHCMebKcL6KT3mgAJALKoQ5SbK3crGIzN51lYyJpwugC6WA1n UBuIsUx/vuVrOSnyZ0IyXzH5TNn7wKiSQkcnGYZZA9SAGaPVAiuHsvLF1gZUfekCJy18BmjjASj 92Fot6+n/FXcsXw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add code block quotes to a safety comment. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index f723d74091c1..3848cfe63f77 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -148,7 +148,7 @@ pub(crate) unsafe fn wrapper_ptr(this: *mut Self) -> No= nNull // valid allocation. let wrapper_ptr =3D unsafe { bindings::blk_mq_rq_to_pdu(request_ptr).cast::() }; - // SAFETY: By C API contract, wrapper_ptr points to a valid alloca= tion + // SAFETY: By C API contract, `wrapper_ptr` points to a valid allo= cation // and is not null. unsafe { NonNull::new_unchecked(wrapper_ptr) } } --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 287B22E3717; Fri, 15 Aug 2025 07:32:21 +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=1755243142; cv=none; b=HeXtdDIzRqQy+sfYwyirZEUopXEycszb6UmDLN3AsJxw0rAkc6JtoDfMe0kXNeKR4lBMc5fjdXW3QvL2yP1H4qAsIm6tFpNUQ9t1c3rjEwI/XYPYuSX6xrc8y9JGDnsbRmfCfoRsTE+ycN5W07HyEfnkBF9xSfUVpRwVG8k1cRg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243142; c=relaxed/simple; bh=ptlSB6M90/rSSKcTITvpEJrxVHE1Cq2YggkTtF+7zuo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZD63QtOl4R7oVfkAKmuB4CYaZwZwqH9sVTQFPtlhLSaFW/NLBZooB5W9JlfgTpQ8ud+sJytGqCwqVqlltuOI9acjncIrEgtDAKHn7i7Eng+iz5CfblT1xR5B0fbYFJRbW6aO6LxtivsQ3MV9AfmVvAuEQGE615rB7/gUxeIUfA8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XoZljY9L; 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="XoZljY9L" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D62D3C4CEF4; Fri, 15 Aug 2025 07:32:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243141; bh=ptlSB6M90/rSSKcTITvpEJrxVHE1Cq2YggkTtF+7zuo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=XoZljY9Ldj/5cPq5m0vhHaIgNOEzPK8va33By5BX1vTY+rJc7R+8Kt+S6L61VIPw1 k+hhYiduVKs1zhiVUPitROCIhFuXPmJTaxtllbJ1k29fB0Ooab5gUOGqElsX2yMHgf 3xIuHt4MShJ0mm16uc832iBEJFgWr4PqSArSzAwxuxfK28jrZ4QLyGRSaEdUM8p1um 09OCoa7mJkocvNp3iaDOgNpPquPbFgwouTikJR5lRNgndBB2BaHtR/FCKbHW8TPZ4I zOC+EC5GJBg4huzzSU2hApsZag/Y/MxfYlH2x1zz7HCDJuCtHLssNCzrxqI/wIt5GQ pJjzHVHQKBkVg== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:52 +0200 Subject: [PATCH v5 17/18] rust: block: add remote completion to `Request` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-17-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=5096; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=ptlSB6M90/rSSKcTITvpEJrxVHE1Cq2YggkTtF+7zuo=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuJDRbh+iYUm3B/Rl8pnd7/DbU4talFphY1OV GNtK+xAov+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iQwAKCRDhuBo+eShj dxUOD/90P0qxled8tMgqTvZrwRFsQm2CK0mivxqCFoCrMruKHcUiMTXV/CxqiskJGYqYCkpkBCH etAlWuhoqjnhjXloLMEYN6ekPg7Wbr8xuO1fWfIC5tBq7AlGFYSIYqHzpgIpui9IxOSjpPug6SO 6YqbDCfc0TdmQiUwe03PMMzrVTm9v6kiKHQ/ZKJviSf16AGZlfMSbcfo97vqK/WRd9C8xV9wSp2 aqQtk/rmPyVYYKKm+Du1OetqntcnRi8flT4VkBqTytugIXk2midRXZo+5eu6ts07GQHHCxdgs2z Pedh+fJ13dqN3sJogGoH1GHZglTbrgpEEvGLP+OpC1mEAyRs7p74IpYUTM+WEKsKf1r0Sgcknxh 8Vk55GWdiF3HEHSgAaJWAg1wsJKN9TKINx6aI/ZEHWGLNH2iVnjiua3EmQ/AtNB5C14J5+R/bx/ dfBXSurWbO6YfneqXIRqQTutvTV5su03B2pApa/82fPw0Klph7aEQYdIEYe2k0/rDpcUO8OHsS4 tqXbGQbf7KgeG39yRehlOe3JqO78/hNBADBR3ne4VWYYCN1LoeGroim1jgrqxNEmFIZeJ5yhrgb pHySvaZnkCzl8StpnU4+8vGAJRSimdSjD9QKOkB6lySWBgIJ3wTqt8XIi5jnioRvviNlAQQyhf/ joinvrE3NcOQ18A== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow users of rust block device driver API to schedule completion of requests via `blk_mq_complete_request_remote`. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- drivers/block/rnull/rnull.rs | 9 +++++++++ rust/kernel/block/mq.rs | 6 ++++++ rust/kernel/block/mq/operations.rs | 19 +++++++++++++++---- rust/kernel/block/mq/request.rs | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index a012c59ecb3c..371786be7f47 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -83,4 +83,13 @@ fn queue_rq(_queue_data: (), rq: ARef>= , _is_last: bool) -> Res } =20 fn commit_rqs(_queue_data: ()) {} + + fn complete(rq: ARef>) { + mq::Request::end_ok(rq) + .map_err(|_e| kernel::error::code::EIO) + // We take no refcounts on the request, so we expect to be abl= e to + // end the request. The request reference must be unique at th= is + // point, and so `end_ok` cannot fail. + .expect("Fatal error - expected to be able to end request"); + } } diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 6e546f4f3d1c..c0ec06b84355 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -77,6 +77,12 @@ //! } //! //! fn commit_rqs(_queue_data: ()) {} +//! +//! fn complete(rq: ARef>) { +//! Request::end_ok(rq) +//! .map_err(|_e| kernel::error::code::EIO) +//! .expect("Fatal error - expected to be able to end request"= ); +//! } //! } //! //! let tagset: Arc> =3D diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index 6fb256f55acc..0fece577de7c 100644 --- a/rust/kernel/block/mq/operations.rs +++ b/rust/kernel/block/mq/operations.rs @@ -42,6 +42,9 @@ fn queue_rq( /// Called by the kernel to indicate that queued requests should be su= bmitted. fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); =20 + /// Called by the kernel when the request is completed. + fn complete(rq: ARef>); + /// Called by the kernel to poll the device for completed requests. On= ly /// used for poll queues. fn poll() -> bool { @@ -143,13 +146,21 @@ impl OperationsVTable { T::commit_rqs(queue_data) } =20 - /// This function is called by the C kernel. It is not currently - /// implemented, and there is no way to exercise this code path. + /// This function is called by the C kernel. A pointer to this functio= n is + /// installed in the `blk_mq_ops` vtable for the driver. /// /// # Safety /// - /// This function may only be called by blk-mq C infrastructure. - unsafe extern "C" fn complete_callback(_rq: *mut bindings::request) {} + /// This function may only be called by blk-mq C infrastructure. `rq` = must + /// point to a valid request that has been marked as completed. The po= intee + /// of `rq` must be valid for write for the duration of this function. + unsafe extern "C" fn complete_callback(rq: *mut bindings::request) { + // SAFETY: This function can only be dispatched through + // `Request::complete`. We leaked a refcount then which we pick ba= ck up + // now. + let aref =3D unsafe { Request::aref_from_raw(rq) }; + T::complete(aref); + } =20 /// This function is called by the C kernel. A pointer to this functio= n is /// installed in the `blk_mq_ops` vtable for the driver. diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index 3848cfe63f77..f7f757f7459f 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -135,6 +135,23 @@ pub fn end_ok(this: ARef) -> Result<(), ARef> { Ok(()) } =20 + /// Complete the request by scheduling `Operations::complete` for + /// execution. + /// + /// The function may be scheduled locally, via SoftIRQ or remotely via= IPMI. + /// See `blk_mq_complete_request_remote` in [`blk-mq.c`] for details. + /// + /// [`blk-mq.c`]: srctree/block/blk-mq.c + pub fn complete(this: ARef) { + let ptr =3D ARef::into_raw(this).cast::().as_pt= r(); + // SAFETY: By type invariant, `self.0` is a valid `struct request` + if !unsafe { bindings::blk_mq_complete_request_remote(ptr) } { + // SAFETY: We released a refcount above that we can reclaim he= re. + let this =3D unsafe { Request::aref_from_raw(ptr) }; + T::complete(this); + } + } + /// Return a pointer to the [`RequestDataWrapper`] stored in the priva= te area /// of the request structure. /// --=20 2.47.2 From nobody Sat Oct 4 14:33:04 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 7A0532D23B9; Fri, 15 Aug 2025 07:32:28 +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=1755243148; cv=none; b=PposFRSR9B6MQNhDvwofYhnlqQ9fmFyifHdcuvXGbsEQ9b2b3QT0m/P05PihUq2/qulFY12ZukVDgLGZp2iRjA7lGMox+r/9UeXrYEmroFA2EhBmboXR6FfyQ11zu12ytY3GCLqhnAEru2sqsKSRhLo6J0P6GMGYYl9J7SJDC5s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755243148; c=relaxed/simple; bh=W+DYHnlyo/3Sa+o2Sc3dmpZfrnUsGYHrKzmjAeLisGY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W/mF0ydxjdoKpzdr4LzA3jJv5lT5iY4hnEBlLeAkS6BnhuLmxE9ZOc+vWy2Ic+nGKkl7apiQ4ylTvMZ+jZreIpUlEgnEg2ZZeBxN++d/WaFA5aW2vQvJgs5tzcMSvpyhWgHsVHp/biOGQdWLtWgcAr3BPGDzA6H6/DUtSokipmQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=gfRtWcTn; 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="gfRtWcTn" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87E65C4CEEB; Fri, 15 Aug 2025 07:32:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755243148; bh=W+DYHnlyo/3Sa+o2Sc3dmpZfrnUsGYHrKzmjAeLisGY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=gfRtWcTn/QBPhQYeLScfmj4H9IcPxCbb6UHkfxkZzL76JRtaIFq3q1mBAVVKhfcqp oRwdwz/f5Lidx2eZU5fyM6e/zY0AUr/vORyTmcDv6VJ7RlH1y7gQ3jr325klLUpJhn 6Udbd35KeGqUmo7vfR9NsNW+or+BwXbRCEaCqXIH6FAz1aD8xp/ucFZ/9vv+LFMn6+ E4pnH2iHZQSprEJTntEOOXf5PfyLIe71c2u3TWtL3MmX8SacRxj9aXG/9NrOL2+t/B DtNwVh3/vdx7Clghe6s2Zzbh4gde4PA1eI/f1yiTy/BOYGFrAM1FVCjBiHCWRscE3o 4C34KymZzx6GQ== From: Andreas Hindborg Date: Fri, 15 Aug 2025 09:30:53 +0200 Subject: [PATCH v5 18/18] rnull: add soft-irq completion support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250815-rnull-up-v6-16-v5-18-581453124c15@kernel.org> References: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> In-Reply-To: <20250815-rnull-up-v6-16-v5-0-581453124c15@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=6342; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=W+DYHnlyo/3Sa+o2Sc3dmpZfrnUsGYHrKzmjAeLisGY=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBonuJEALxl5Ba/R/LoqX+1GBLRTMKyTcuBulT2l tlf/2tWyHCJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJ7iRAAKCRDhuBo+eShj d9e5EACODFBQuqVSnYJYQlsUAmfwrzv9+qNhzbBzxLnHSvyjDgKoDxB3uyqJuLciE+8sUw4oS5D lR0AvXm1x441hifAZMi6VR7hY0n0CQ68JyMUtQSCJfpqtY9WR1BK4Cuk80kTJ8srbobjivNtfSI xVIeQYg6FZSc36/B764Gs4y2+s/Z3f/EF3NjSXdWSg9I/j+A+8hGM7i31V4unJmjrbrwuLx4Fnt c39RVgcs67IlhemkwVed+WI83kZXFKD5lrreMPpdPsZs5Kaa9oikfEEGN3e/2IxH9ffMPcEc/hi obZ9DtUkWAqXTN8t+rntEazPv841wTcjugmuOheV6Rt8mI9x8pbk76BSfsRs5MMcYZaYnOZw54P pZoYPkkAh28C8sJ5YiIh5LI9PkuqlWBg4nUrYSnbeiNuei7sgZt/Ie1LLZlwytBhHkjdQyTH6XK PLbmjfe27vyuJFKjuR9LyEGMrpLiMkGSkyENfPUK8JeB/hUhHVFTPauyZFEg+X6aJvBweXpL7z/ eAhZii5+bAHY+giejiV1MnsWX7PAePA/2LML1GynmSvCZeqfXOFSow0b8+ik6Mz58w0LPE9S8rG O8XDLFFHJXGdasNbYvmjxi4woa3ZIj3992P61Jkh5y2rGKe6FLGk0LscXrOqP7UKjPlyibZ9tt8 ADEiS22QB+gRbKQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 rnull currently only supports direct completion. Add option for completing requests across CPU nodes via soft IRQ or IPI. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- drivers/block/rnull/configfs.rs | 59 +++++++++++++++++++++++++++++++++++++= ++-- drivers/block/rnull/rnull.rs | 32 ++++++++++++++-------- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs= .rs index 488fe27f83a2..adc5bf5d2f8c 100644 --- a/drivers/block/rnull/configfs.rs +++ b/drivers/block/rnull/configfs.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 =20 use super::{NullBlkDevice, THIS_MODULE}; -use core::fmt::Write; +use core::fmt::{Display, Write}; use kernel::{ block::mq::gen_disk::{GenDisk, GenDiskBuilder}, c_str, @@ -36,7 +36,7 @@ impl AttributeOperations<0> for Config { =20 fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result { let mut writer =3D kernel::str::Formatter::new(page); - writer.write_str("blocksize,size,rotational\n")?; + writer.write_str("blocksize,size,rotational,irqmode\n")?; Ok(writer.bytes_written()) } } @@ -58,6 +58,7 @@ fn make_group( blocksize: 1, rotational: 2, size: 3, + irqmode: 4, ], }; =20 @@ -72,6 +73,7 @@ fn make_group( rotational: false, disk: None, capacity_mib: 4096, + irq_mode: IRQMode::None, name: name.try_into()?, }), }), @@ -79,6 +81,34 @@ fn make_group( } } =20 +#[derive(Debug, Clone, Copy)] +pub(crate) enum IRQMode { + None, + Soft, +} + +impl TryFrom for IRQMode { + type Error =3D kernel::error::Error; + + fn try_from(value: u8) -> Result { + match value { + 0 =3D> Ok(Self::None), + 1 =3D> Ok(Self::Soft), + _ =3D> Err(EINVAL), + } + } +} + +impl Display for IRQMode { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + Self::None =3D> f.write_str("0")?, + Self::Soft =3D> f.write_str("1")?, + } + Ok(()) + } +} + #[pin_data] pub(crate) struct DeviceConfig { #[pin] @@ -92,6 +122,7 @@ struct DeviceConfigInner { block_size: u32, rotational: bool, capacity_mib: u64, + irq_mode: IRQMode, disk: Option>, } =20 @@ -121,6 +152,7 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { guard.block_size, guard.rotational, guard.capacity_mib, + guard.irq_mode, )?); guard.powered =3D true; } else if guard.powered && !power_op { @@ -205,3 +237,26 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { Ok(()) } } + +#[vtable] +impl configfs::AttributeOperations<4> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + this.data.lock().irq_mode =3D IRQMode::try_from(value)?; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 371786be7f47..85b1509a3106 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -4,6 +4,7 @@ =20 mod configfs; =20 +use configfs::IRQMode; use kernel::{ alloc::flags, block::{ @@ -54,35 +55,44 @@ fn new( block_size: u32, rotational: bool, capacity_mib: u64, + irq_mode: IRQMode, ) -> Result> { let tagset =3D Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KE= RNEL)?; =20 + let queue_data =3D Box::new(QueueData { irq_mode }, flags::GFP_KER= NEL)?; + gen_disk::GenDiskBuilder::new() .capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT)) .logical_block_size(block_size)? .physical_block_size(block_size)? .rotational(rotational) - .build(fmt!("{}", name.to_str()?), tagset, ()) + .build(fmt!("{}", name.to_str()?), tagset, queue_data) } } =20 +struct QueueData { + irq_mode: IRQMode, +} + #[vtable] impl Operations for NullBlkDevice { - type QueueData =3D (); + type QueueData =3D KBox; =20 #[inline(always)] - fn queue_rq(_queue_data: (), rq: ARef>, _is_last: bo= ol) -> Result { - mq::Request::end_ok(rq) - .map_err(|_e| kernel::error::code::EIO) - // We take no refcounts on the request, so we expect to be abl= e to - // end the request. The request reference must be unique at th= is - // point, and so `end_ok` cannot fail. - .expect("Fatal error - expected to be able to end request"); - + fn queue_rq(queue_data: &QueueData, rq: ARef>, _is_l= ast: bool) -> Result { + match queue_data.irq_mode { + IRQMode::None =3D> mq::Request::end_ok(rq) + .map_err(|_e| kernel::error::code::EIO) + // We take no refcounts on the request, so we expect to be= able to + // end the request. The request reference must be unique a= t this + // point, and so `end_ok` cannot fail. + .expect("Fatal error - expected to be able to end request"= ), + IRQMode::Soft =3D> mq::Request::complete(rq), + } Ok(()) } =20 - fn commit_rqs(_queue_data: ()) {} + fn commit_rqs(_queue_data: &QueueData) {} =20 fn complete(rq: ARef>) { mq::Request::end_ok(rq) --=20 2.47.2