From nobody Tue Oct 7 14:48:38 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 464032512D7; Tue, 8 Jul 2025 19:46:05 +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=1752003965; cv=none; b=l/xY2BOTisiv0mRSpfKNev29uivDBbcBl2npELMDK5hSJW4rkw/W+ooNC7Km6EiMx8kQZ6QtVixPWN4dUzzQhIcNCi/j/5QTcRLsYYwZgaGPdizn7KsxlP0irvX+Gid9nmImaKSOczDQ1aOqy+cial4x8hAhADTPgH+cGAbdeeo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003965; c=relaxed/simple; bh=KtHw1QmkuL/sm+PVBCjjLX5l+yiMnLEt2qOzqap7pmU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=HZwU1wzptQuMNOL3cI3k2WJtS+pfqydp0hsYmNUcyoHfWKn/1SmQjPqEqpzYL9rv59c0JLFwP+PtF80kLglT57y+e73fzBwn0q0ABIsyzxjiwgNVojWJocV1dXsVhIuo3EovxCUSjAuUwvAiY/lG++yI1PUigyTIEmnJuj4H6UQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SE0ws8Cs; 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="SE0ws8Cs" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 41137C4CEF0; Tue, 8 Jul 2025 19:46:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003965; bh=KtHw1QmkuL/sm+PVBCjjLX5l+yiMnLEt2qOzqap7pmU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SE0ws8CsgBA9mG/XWsHoNwyX+xEidTQ9JhrD5Ti0BqqobZmowadq1wVGrgrQCWWz0 7MtbyBsSMK3p8v7n5dX3dB5uhhHBlmaNRBWyDVRfbi4T1eGaolertrJltv2oStl4Hk 734BQ6+TsL2xKqBsPFs9FeoR1UPpYaIqwXzb336AHHRcgXaQgPb6oDvMaiF44Ne55G 1pYSyFmJ4j9v5c4zNqqzfv1/B3teZaQgxjyhEp8Q5hEnt/oYgemU3o5ArtgBd1WIMR sB7kEGwvwR/3qlD9suXnqQMZqkYVshUdSd9wR41mWYBY5tUp6efl5oTQXqMriYRn58 F3PRtz8lPw6ng== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:44:56 +0200 Subject: [PATCH v2 01/14] 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: <20250708-rnull-up-v6-16-v2-1-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=775; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=KtHw1QmkuL/sm+PVBCjjLX5l+yiMnLEt2qOzqap7pmU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVBOv4u+LhBKYblj3Sy/TSNHHB9B2MPUVN7k Ri70aUpKcuJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11QQAKCRDhuBo+eShj d6NxD/0bOh9Pbw5gaGMfYn3GdzY6iFiaSXPUGHnM4MQQ+m7+Kf9umk5i1mimS3403cA1j5uRm04 ONhjQLZ92B3nVnoGUHpYXRALurFjQY9hE5WPiMD4CX6YF5LBHT1HUQPyBMCqAJiQAcLzyn7lGmI mc7HgibggI1otko7n0Vr+yrK0Ti2BQYqWMYIgb0p3druoflBgCVRNoPjEShxmrG5usbO3oPawm4 NZkrNe1QqYRQuDSlkqNEt8YD/ddPxDgpbrMEC7xxQQksFf3nqwm/hpZDcUJzzYwh8Qj1ZibvOvp +huESt9lOXQvVFTPxmHFPS4PLgMT73/HLcW4b/hr96TUBtRWENJgPAuqva3Nts8gqk8XKcvlHrQ qAX9SvdOr4lLtyyXgrXdQOXs212N9O1FW9U87RVjLLurmsWDTr89dyM3Jlh4RXCUYUGdydXskqf kgMZjEPQqIGPlzuZ/3e8e2wOWhyNTEMb8KbFHG8ptC+shtrTWz2/jKvOnDq6NJaqqeGNvDD6Cy8 NqMB+Mb5WvnwF9vy36qDVAcHA8ZHq2gMZtMpY0Awoh+B2Se347JBOPv+L5uwP18RBax9F8zbmAs GR8r1n7xqfV4eXm8vYvj30lY+TLDLIKUTD+4zZpyhoXhQytN4DkrAA8nrGvoFq78R6lTTH29v8o WIucYeMjSlxRkTQ== 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. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- rust/kernel/str.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index a927db8e079c3..488b0e97004ee 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -3,10 +3,11 @@ //! String representations. =20 use crate::alloc::{flags::*, AllocError, KVec}; -use core::fmt::{self, Write}; -use core::ops::{self, Deref, DerefMut, Index}; - use crate::prelude::*; +use core::{ + fmt::{self, Write}, + ops::{self, Deref, DerefMut, Index}, +}; =20 /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] --=20 2.47.2 From nobody Tue Oct 7 14:48:38 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 A69042594BE; Tue, 8 Jul 2025 19:46: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=1752003985; cv=none; b=ryNpECfamIr5xtJwzGMIESZ1QTr4k6/nOpZGkSUYDUViYaoWcbQHY31TiZbjaXvyMdsdr/1MRBrd0+EurFE7OO5zQ5Nc7M3JXArhgyui7sqeqDoJ+SJZQL8Eh/39FMH20Jfkm27ZqEr+e522gzd5sA3KH7ZZjCdxAna4apqvGBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003985; c=relaxed/simple; bh=7i7e7fiEtIGYD9fchcq2LcqXTHm92DVFhpGmcv+f2KM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=h2ce7nTUASTarwVNkCVfeOl6fgW2i9Fs+46nQhos/iNKkyS9oGkr2n4yDlNEKNrDuAvUNUroobxBiCiOMHn+KRyfCfLgPQKSxRjUYdiIPqKDse16LRi2UaYBgJ7B2X72y0YY9CnnWejOythumNl8LA1S5ZiBreemZUgBeSLXQaw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fI+UqtDC; 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="fI+UqtDC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5A67CC4CEED; Tue, 8 Jul 2025 19:46:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003985; bh=7i7e7fiEtIGYD9fchcq2LcqXTHm92DVFhpGmcv+f2KM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fI+UqtDC3BgSBTqMyT2dxnS2KQ8KJYge5ZtQmNkDiBT+D7OQhwyeZryHiOn2NL+ew ACW4mdx94KQQcYFjOp+KpqNkPxa1Smdst86sGxmqfJmlbOQqkt5vrVVocbEq2PhLV+ TyNrCXZBCx9qVy6C5OiH5ZTJYDlJfgzZ6f4hv4aiOP/ixVNnAMyCkpWfbb3NBF6I+B R3hN6+/MSntUiLSAZQ0KgUG0pxMELq/FmGc9hO6c3dzJk3VZy4s3jHciAgkOuiVMP6 9NG7bbzt+tVUGLzDsIyE1ff3lhXlNf6ODAn4y/P4U+OD8LGdbT2L62hNgMigo2Pdky gUhTBZz7fFTwA== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:44:57 +0200 Subject: [PATCH v2 02/14] rust: str: introduce `BorrowFormatter` 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: <20250708-rnull-up-v6-16-v2-2-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=2757; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=7i7e7fiEtIGYD9fchcq2LcqXTHm92DVFhpGmcv+f2KM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVCEabjxTjPw05XJUqWVzIF5Z7Tmo2TrmLgZ cNeVFQgUdOJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11QgAKCRDhuBo+eShj d4q9D/4yv1KgPy/VvY1lc20i5dN5twWfhYTDUwVs/vjT1Br2tP5ElMgaJ1f4G1/2l5DkHFO52on 4cgVJAsIcdqmS3tRMnLs6XrAShN+b+q67HIMzSX8YRNmaHHHDaiC8brKNvmUlrz9kJ/3rDpwh1m eP30pfxNqUo5oMv7gVyL9mpSE4IQI/3cr2bqsBO1WJRF0UvlubEADWpFYj1M2JEBctHayEwmf6V iWNjIOPAb3KRQfcYno8mf808MdATsP/IvYzCQMFL06QAgc3qrGZeNw5taC2SxFsjJcfsy7Mb++6 IiD/jGiQmsUDtI2UsLt6miJEdbp660p9V1pRxoMRDorQGX/2MF2tMmEZBjSmSou6e0b66zVWFs1 VB2LzTFkWwWjGzBKRQLeIA8zcQgX3VEZZ4Ex9Y+Vpyybp+mHeB+A4w2fPEPa6Tah62uEP00UlBm CfPu+A1De9wyUvXUAIPGVNRZFEPx0b7zsI9w3NyH8XYm/9XanncaJ4igjvzBQg0+aI9t2LhPDvt 8MPgIdtmJdpUvCPdQu0SWFoXITynRB6amVwbB5hlO9zDxEjKLUaZTj/6pO8bZs0YVnv+nzF5x4B uLp32pZVBFpT3WD0usGOhoFrDy1jFoKqRusfx/QzWoA2+LnnIBJAE+l/b7mKoquh6N3fqmat55Y yu5YZgcoEztWTWA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add `BorrowFormatter`, a formatter that writes to an array or slice buffer. This formatter is backed by the existing `Formatter`. Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 488b0e97004ee..78b2f95eb3171 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -6,6 +6,7 @@ use crate::prelude::*; use core::{ fmt::{self, Write}, + marker::PhantomData, ops::{self, Deref, DerefMut, Index}, }; =20 @@ -702,7 +703,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, @@ -760,7 +761,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 } } @@ -794,7 +795,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(RawFormatter); +pub struct Formatter(RawFormatter); =20 impl Formatter { /// Creates a new instance of [`Formatter`] with the given buffer. @@ -830,6 +831,35 @@ 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. +pub struct BorrowFormatter<'a>(Formatter, PhantomData<&'a mut ()>); + +impl<'a> BorrowFormatter<'a> { + /// Create a new [`Self`] instance. + pub fn new(buffer: &'a mut [u8]) -> Result> { + Ok(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.le= n()) }, + PhantomData, + )) + } +} + +impl Deref for BorrowFormatter<'_> { + type Target =3D Formatter; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for BorrowFormatter<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + /// 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 Tue Oct 7 14:48:38 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 247E7248871; Tue, 8 Jul 2025 19:45: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=1752003958; cv=none; b=QxUXx0hMdUkMZV5eP9ZfX1OJcF1o3R/o6oX50A089w4GrOXCcqW0/j8VFENAgvg8DafgkJOESTH6quuvyOGVN0JbfvtpdiehvvaChUXg0wI9dos5vdUvn9rv523r7GS1IpL2jX6jJlfcefQZvhHQRkbw6ot3iZRdKPHQlinl7Pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003958; c=relaxed/simple; bh=Tc69NpZsnd7U7OJBUvDg6vLnyx/YuYb6KbZ5n87FHyw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=sDwaeq/Ac/hKJOLy72JTv0N/RNUdwHKRU6y1x6G38YkJ70XONwI6wCVFQhJ9jwmZ5yiMLp3nZd4WoT1kyT80S4ga7V2cL40M6RvyC47O5X0V1rE64/luYOBP61QyiHBLQaZ+YSx7Y+k61LAN1jgwsbr9ROAHyJSVgi5ZsrxiVOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=tdntC6Uk; 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="tdntC6Uk" Received: by smtp.kernel.org (Postfix) with ESMTPSA id E0203C4CEED; Tue, 8 Jul 2025 19:45:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003958; bh=Tc69NpZsnd7U7OJBUvDg6vLnyx/YuYb6KbZ5n87FHyw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=tdntC6UkSZ7F7DdjJkJWDuW15dnotGqWbLgVC1VAFsr6YuklEq09j9XdBYCVWMsci RGKlu5KezaXkXriRM7kY0i+MhBN8aEoZ3ImcKRy4IGwJH9D63or6Hel2MxAplsGP+f mG8aXZSaQa0oXEOF5iTA4Gp7U6sm3iKq7giTkVjbHnna2mbVbRTD4sLGaW5Bz5pHdN tzDA9ywNbNcp8v6QDjB5DNHq8jgbwzgFNXi1GjCWTSwjVWgTUJZgqhCDgjh+oI8/8K OjVcFdOdFOr2ZJLJZm/N5r3gUUMTpvaXk24rE6jQmQOHIb1LeaNQzQ5BpePiXQt/kS FR2uBXeEkM4Rg== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:44:58 +0200 Subject: [PATCH v2 03/14] rust: str: introduce `NullBorrowFormatter` 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: <20250708-rnull-up-v6-16-v2-3-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=2660; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Tc69NpZsnd7U7OJBUvDg6vLnyx/YuYb6KbZ5n87FHyw=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVDxefYaaw1FyQfRThv9Lp6u74GZ80xXa9hq cLhVemmuAKJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11QwAKCRDhuBo+eShj d7yBEACOHt07/pNgojf/PNidcBKq3qj6UG5iv6qDOtFuv8W9FNsqcAHX80ECtElLNo228irDdRr yze/AkN6NmpOorqMAvQArEXc3BEJktLlcz311J2/5Uhy/QeDdj4TVEmKd0LgmMWAOTQjNXvn7CM /8gIwtBT4YEti/wodwpcPXvlH0Hv5se0tKrI6iWlbsiKSVpsGKjeZ8DBOld8hLr1R70spQIzjHr nlHrk2AJQIbyAkSB5lPwZvOUinecnYVF7H5Eqp0rNRyWyEne8f30Pwr6jAab7EtT4Djsw8FT4+K 7/GcIkiThHIL2oMO4E6ePHv0vIT8zuZLpUogsjs27rm8Q/R3GLTPwn6wyfQ4Bv9inpVhnyVxuyO W5uBis0EoGwCB2Rmzo4ez5pwwPUOlnddK0dAfqwKw436NfTbow7uKybgcSTAmvzrPg6sikI0m2v qcRePKupl0cKhYhn//VF7wrC6F2HP1UIaXgXRbYEBpHPb0fuQFx3qR+UgxKC1UQlgDQXFwwhIw1 w2gP3uF7vpIJ4itxVkNNIkWTP8lq3OT5xQdS1SOmehBVJQXv2taq+7yHrgvemW73C5+QJ+BImgY R9iGBu253bPoRQRVUFTXWER5h8SmtADRwj/u1AEwLhi4wIHZLwhFTBVtjcPfND+k/IUY9wXOfiq kpIW6gLj5CEAC4Q== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add `NullBorrowFormatter`, 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. Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 59 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 78b2f95eb3171..05d79cf40c201 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -860,6 +860,65 @@ fn deref_mut(&mut self) -> &mut Self::Target { } } =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 +/// +/// `buffer` is always null terminated. +pub(crate) struct NullBorrowFormatter<'a> { + buffer: &'a mut [u8], + pos: usize, +} + +impl<'a> NullBorrowFormatter<'a> { + /// Create a new [`Self`] instance. + pub(crate) fn new(buffer: &'a mut [u8]) -> Result> { + *(buffer.first_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) }, + ) + } + + /// Return the position of the write pointer in the underlying buffer. + #[expect(dead_code)] + pub(crate) fn pos(&self) -> usize { + self.pos + } +} + +impl Write for NullBorrowFormatter<'_> { + 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 null terminator + if self.pos + len > self.buffer.len() - 1 { + return Err(fmt::Error); + } + + self.buffer[self.pos..self.pos + len].copy_from_slice(bytes); + self.pos +=3D len; + + // INVARIANT: The buffer is null terminated. + self.buffer[self.pos] =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 Tue Oct 7 14:48:38 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 CDC87246768; Tue, 8 Jul 2025 19:45:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003950; cv=none; b=nh7s+oz3VjtKiPvDMCRp3Af4S100v7qzwBCSY5C/u9zSOcX5rN2eMvtLyn4Vh6S+yDR4dcQPKLzqZG+TjOLAc8FZFSECx2cyyjXpOOEPc0qU5XBgmJMX2Km8bs/ZwavEMfKH/IhMuhuj605i42mk/eir0wUSS4Ifx/L2U1NgB+I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003950; c=relaxed/simple; bh=ZZpCWZQcgpSLHSvgu+5Wu9OlonmFvYaDz/ku4DcnLIs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZfBMtTbMiWWZMmX2kHgrzRad4k8zccQy60JKVoAs1uybfziwvWQplJaSy2nmtjyNTTp8bBpxEAO3zj90VxHXX/MsB8Aj8hDJa8yap28dmaE3/So5deVdzm5/8JMVbV+2XGpGVPv2xrQi7Dx9HD/hO/bOPkxd/Olcfxw7nllqN3E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hT3ajMZw; 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="hT3ajMZw" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EACF9C4AF09; Tue, 8 Jul 2025 19:45:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003950; bh=ZZpCWZQcgpSLHSvgu+5Wu9OlonmFvYaDz/ku4DcnLIs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hT3ajMZwAownDJoxQ1lGe2O9YsyPK05j/08PDdhAtYoJ2mGZZhWW+dZQzDhRIlddM jKBu/iXsMmYHLOWtDKo5AUfWK+ANTII8nyd9SMi61+drs0HJv+1lO2ZwEm1iriTRdP ZwmCvquFzIBpU5P69TK5C/uPvhIhbtsGHdi6gctkj3hqpuS9hiwOViHnSRP7dpk7CR FFnqHzn0N5BARPTpCjsXYyuCkfeK6ldn8xatx9LwTE0rr6hmDrIpGsgEZ1gii33CWh N1i0z4PMt7Qwo9aZeJ3E87wNl1VWhzM8pf15xTLt72oj24AE99xej+zglmL/9zMw5C BzuCAbK9pykIg== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:44:59 +0200 Subject: [PATCH v2 04/14] 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: <20250708-rnull-up-v6-16-v2-4-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=1078; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=ZZpCWZQcgpSLHSvgu+5Wu9OlonmFvYaDz/ku4DcnLIs=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVDJULtl9kfhBJCutDHkF65seZyQbiU5sTID TiALKWxtTmJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11QwAKCRDhuBo+eShj d5xyD/940AMT2NjuhS51jei3L0Ac4BF7H1bujtnPBTt/f0WKJRVa8EjY9dN6tq8X8j+0lwBbyTy UY2Y2d7AZrKyfq2dyrUKlkdPJ1e0bIbEhTjvobY2EpFkF3d/9o9CfWtz5W4cmSRp21G1jnb8cRb aaoR3tUD6lkQRjG1rhDiTDa2WCU7veA62rZdjCDZqU0JzKrwWc0mSyHG0S4j5Fgfk99cxVoMtj/ +G0sbRufyjfCNl0pYZeiYZTpeGdGXBUvkdSy6+m8JjABIf5mA/nzwnhsOgpdbp479q+akkBfLpG A5yrq3l835sV5TYkbHyL8VxeSw9acjJLJ/hBKKFc2E8g/d2pb4bZDolEvXdyam9S9F+dRBEX5hF 6A5AkiSNcDsB0RA17AGLT3AJWFyEyBUpiOlvlVIxN8mi1A5Lb42vim6zMGhLqls8YHXlQmYR7wX pBW1TjLkZhJAf4wrqN4l4VbFPKxNPizn36nJVQZgwNNNT8GV9FZABgA1W89fBuXX2tia59ZvoGq pKnBqAIYZ7zY3Z9VNM58mGMFwymhJ2P4k4u10DXqSRwJlJM95xGjCi2WLl11wq0IA0QZGqxPw9f 8QUVOB6xdqYI8fPjxEGCIR0EkGtd2B6IQZ1/mlsIJPFMWrRxCcJUn0Y3Fy8cOjm4vCtoAHsLhe+ em3bvXdBG0eTuFQ== 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. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- 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 cd54cd64ea887..679ee1bb21950 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 Tue Oct 7 14:48:38 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 E98902405E8; Tue, 8 Jul 2025 19:46: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=1752003982; cv=none; b=JKO3qILIjWV5XM9lsxmswuO/gabG8vl0+5/HJRY891kMCur66dqiybf6N9x0n9IwNGnvw6xQ36yB2UwlTjrU6dIzg+f7y4A7I99XupYZ8vW8EX6Gn9mnuIQg9rdm2emGo0Anu+Dy4jjYU5If6GBx/GrT5wKLY3XVBegbknJu3tE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003982; c=relaxed/simple; bh=rI3PMBuRoxQk4V0KGavukqegyFDWNHFNecQhnnrOVMI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=q4eq3EF0Pln2zWlusckkSxsujR6dDGQAx+mKvttVSYvwkyY+J9KmNAAXfyHUiFy4Ef1p4LAg94NXWv4np8/Y9w1QK/q5TwWoROPQ67u33LCnlmXJXCycf1uWqUBYU7PA9rFNkHF4QxZEjt5cnUAd/6EwhzbJunuMEDpZjRKiigk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=PFiH8R4n; 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="PFiH8R4n" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 94773C4CEED; Tue, 8 Jul 2025 19:46:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003981; bh=rI3PMBuRoxQk4V0KGavukqegyFDWNHFNecQhnnrOVMI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=PFiH8R4nnhGB68C/FqbYxA5++Esk9oIVZpkEpqIdFEMYMgw4nyFCheJmXXaKk1ILM Hpe4rLts9To3oH8Gd0TK8H4oqg4WZ3mNtSEzJvcpGrsetsRZHSe+kollITl5nWMINF bUaUDtApZlfP0CjWRf4PRGBa5Z1s12ljtasFUoK40u9dGwZpkYe6LS22PKK1kWGr9n 42DOF24rX5KngonVpvwBaWkUtdHCl9hLbbTqYuL6SWE0FVKQiWznGgjeYP4ricsHDg m0ykS1/SFl2uvs4NC2zxKFymJLheIf0W2oK+rK5e/SewcpXC+5hn1Fnxx2I7FhINzM 5LozSIsssxHEg== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:00 +0200 Subject: [PATCH v2 05/14] rust: block: use `NullBorrowFormatter` 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: <20250708-rnull-up-v6-16-v2-5-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=3200; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=rI3PMBuRoxQk4V0KGavukqegyFDWNHFNecQhnnrOVMI=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVEwXx/A97m/Qc65sLZ0qY5/J1wRThTKRGFo kVCyU9sm5KJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11RAAKCRDhuBo+eShj dxvQEAC+gnO0EHTbKeHnj9f2L9pEzdhwkcR5/OUeySZcSzi3O+xyQzUBte0OUCZqqT+GZI8bUxN 16klAl9nvGweu4J1pK9lSOnB88ArwbsU6VlyBFXTu4MIhiVrNzrRXHcgSk1hmyIiXxiONR6pYh+ +2Xpw/m3i31EngidyC0+Bq+4DvPh0KZQpJgn2qZqRXlHZ7LnTHKrZpqbLuOBfuJ2dia/Oo1d7A/ 9+18ugpOko+IpX0p1nRQWHtIs34IX0vIugMzV51SzbHZmelh5WFxUbeUcY5yaXbPtV46o2BpDos G1Iv9UoebD4N9QiuEoQKfXlrRxNFiWzrGw10OAKKXAdbnU4NpIin33kxeTGh9KFsu9bEVDppoW1 +abDoW7e0Rqzoa5XSZNYC4hx5wPTylC7jiqC+zEL8Gjj6+fOK5/J57L5efzsgWNiBr72jzYcz/b +YByiC9uR281XeEOJrPLPwjBt2B52/7oG6UQn2Hdh1RCFjFfQ/Omyn51obUyT4aKCDZI8hrEQVZ ieS7iX6Eq93nwN05LBkC5QcDPqH/wfU8ePVIYN2yEetZn8D2GaY7jkCCw90SL0DsmIYMk1ihynD GeFIYV1+zFpH3nmH/kRUEnwSX7MX7QbsU3UF+L3lbTxfnRDsB6sQZpDb0qiW478BYDsto6zKsYf Up7ROsVQTYG80Fg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Use the new `NullBorrowFormatter` 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. Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/gen_disk.rs | 8 ++++---- rust/kernel/block/mq/raw_writer.rs | 1 + rust/kernel/str.rs | 7 ------- 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 679ee1bb21950..e0e42f7028276 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -7,9 +7,10 @@ =20 use crate::{ bindings, - block::mq::{raw_writer::RawWriter, Operations, TagSet}, + block::mq::{Operations, TagSet}, error::{self, from_err_ptr, Result}, static_lock_class, + str::NullBorrowFormatter, sync::Arc, }; use core::fmt::{self, Write}; @@ -143,14 +144,13 @@ 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 NullBorrowFormatter::from_array( // 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')?; + 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 7e2159e4f6a6f..0aef55703e71d 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 05d79cf40c201..4140b4af64e50 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -881,7 +881,6 @@ pub(crate) 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> { @@ -891,12 +890,6 @@ pub(crate) fn from_array( unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::= (), N) }, ) } - - /// Return the position of the write pointer in the underlying buffer. - #[expect(dead_code)] - pub(crate) fn pos(&self) -> usize { - self.pos - } } =20 impl Write for NullBorrowFormatter<'_> { --=20 2.47.2 From nobody Tue Oct 7 14:48:38 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 975C525487A; Tue, 8 Jul 2025 19:46:12 +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=1752003972; cv=none; b=fyfIW50cj1D9ik7jFPZVfsVo5lr+YGHN3phW5ujmbV15pyfiEefpEJ5Kx4Hv/Ydoi8tRO/9WdPHg3ZhlBgVS0dY1Zq/Zzhd6SuY+ileYL6UV6U3gbd8FQwKLfD/++qu9raK99FacETNKbAT/i6IUZvu6hPpRxsSEM21GLACShYc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003972; c=relaxed/simple; bh=r1Ti8mK0XX0RvsscDl8Fnpgr342uxk/7gLrReFuGAvc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=J1UZY4WvxyTKEco1aB8PVgu9Eqjili/+4aAU7itiWGCKpRad5OQZsB6D94YRNFloHZcrj8Qc36B7iiKWo7poSVTPHKezGHsfMzfQsGAz6v7s3daBYGHStYTjSHqgESq3Qs6lqsttD2/fpNTl3cn3gFR4M6HOzae0LGCzKmIDL/Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=lVfc+GMe; 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="lVfc+GMe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 375BDC4CEF5; Tue, 8 Jul 2025 19:46:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003972; bh=r1Ti8mK0XX0RvsscDl8Fnpgr342uxk/7gLrReFuGAvc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=lVfc+GMeU395eajoAEEsb4ecLeUfFEVA25gYAA/Xpnz2T+jfFxiuBuvD8qwZW2ojJ xwbXPT7SE95I05a1mRIbdnmltQ1AYNqQdujnjX6TRXcn2UInWnhiPajberJk2fNRx1 2q91Nb+aRDXhmr3WE8u94EfRLw6BtQRDbD4lkalUAE2bARgu3X2l4kjHFrhF5Ac/Us rz9B7UstQtqdJnnr9noCdKfsugigJmUPMU9a64e+vk5CiZXc6Dy2KQYH4RjnqA/gSt tyFm+b0qWr/79zCqtqZscUjTfEIiPoT4gA6tKX5KOqJcwk7j98G8UbPIlKY6XGFb37 JfpSjoX+WY6qg== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:01 +0200 Subject: [PATCH v2 06/14] 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: <20250708-rnull-up-v6-16-v2-6-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=2403; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=r1Ti8mK0XX0RvsscDl8Fnpgr342uxk/7gLrReFuGAvc=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVFvPXh1hTdix/inMHLBf5ERvQEGu9KTYE+B sjXvx8UyGqJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11RQAKCRDhuBo+eShj dzJXD/4iZ7SCIVD40OfjzjhpkI0XHCktn3/n7ywjfj5FjaDtwrW78Vm7pBewylmaL6XetfVQGGJ uFaALh1a+aoSwm2HpTO2LW7zHzyN8Iyh1FCeVPbo9xfQ/xiySA8iHp0cLvVzv5cruQEq50sp7SH 0er99rW3avyDF0JEGJ8qrS+chkCOtckPO1SmLz92LSVtmHfn8PUIl2Pu5vT576UII86dARdrOaY QhQiN2ZNI6KPG1OH4cXBu+fcb9H+F4T2KMPiYpnY3k54TncZ9FHe7O30989Fci2EHv0KiJEu7+2 yUfMm7Gg9G2uzmiP5+hSWS9DAlzxVvKFX27Nk9ySwuqTzdaGB51saosy1ISgNmJb2vSute5w7rX dWDFPe3mg8r+g2paTi2+o9SdAnmZlycSA0MXv6Hzx24QgK+LO21A4bAHtsEJvSkZG9znI2kwoQG KBfMS/HKD0p+mpvVNX2+6omwjkE9vT+C/SPME/tsjbgQq249wWgCBPEgvE+cpDSBbd1oUpo3qFg zMXBXVhXpYX5uPuR5E1ADkL9GgNJlI9SSOqgE/sEXGk6/9RnYApBUZY/UwRWxVNUVDUqsqkMiXJ xabFNEQQA5u900asreNL+ynsfehvtKJ84ODu1zXMN37a4UJInT2j22PxP9F+O3/VzmK5VIKqxzb kxI3Dcvzq7m0DOA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 `RawWriter` is now dead code, so remove it. 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 fb0f393c1cea6..faa3ccb5a49a8 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 0aef55703e71d..0000000000000 --- 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 Tue Oct 7 14:48:38 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 8B79B21CC68; Tue, 8 Jul 2025 19:46:29 +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=1752003989; cv=none; b=KoXUh0/JLrC3kVVF4vKA9WtVQ+6Iys6rG8fcdomgjy1CRT63ZpLsPrX+54KAU3C9KyzRaxYpXMMnKGx6YGvQgWjgPS9xTrQAw172vcF/JGsy4XutrtUzbkZkDIJwOxkvs26PZYkQ+peo4xJD1hQkwi0f6hc5MPC30pC3LNQcfI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003989; c=relaxed/simple; bh=Xmgym1IZWOog8WJ4qolzgPA+pX4lELKlYs3ixE38TJM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gz+m+zZIJ1tdsPNYk93SSYqHqh9WHPpTxltQg9o1EYH1z+wdzImeTSl/RIh/JvJtfHgWgcSJXiXItHOUQWiyWIJp84B+pQRXAk8kH3PnoFhZSAVyt2XtmeTJRX6LobOxgik1DdRwqPQktoDy5EFp1UOfxwhic9m4ZObxtpbGF/o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bg/vqnTd; 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="bg/vqnTd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3342AC4CEED; Tue, 8 Jul 2025 19:46:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003989; bh=Xmgym1IZWOog8WJ4qolzgPA+pX4lELKlYs3ixE38TJM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=bg/vqnTdy7Y+LvC6NrfVYROnqPRhRir35pgC2D3IjF81lAISsldNepcmMm55E2oLl PK8MJxXCK/baABMkK+Pd5vzgtuGNKIzEknmgpN8uGIP8Yi6ABc/o6KresNd+rgSMb3 ykIS/9u8/1wQvxZgUnIqFph8dpxpSgmeqQtXzP1diRhUKtLUrO1WETsQRSKD6Y6sd8 cqoWmgIU/WAGCh0OarrI4hedEbWDRhAh4xixZn8puzGbPF1B4bu7I31h+3/mGNgtyZ KvbIg7p2qNquUjbIdkxKsJd17w1Zioaan0wqEjyxNhEXjgk2sxuZpsUCLyfstwDgZA 5h2C2I7ozjiNg== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:02 +0200 Subject: [PATCH v2 07/14] 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: <20250708-rnull-up-v6-16-v2-7-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=857; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Xmgym1IZWOog8WJ4qolzgPA+pX4lELKlYs3ixE38TJM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVGIkHZZw1DgQOridEB2hltFA/XOcDJt/ghj jdW29w0eUCJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11RgAKCRDhuBo+eShj d36iEACMzi+0C6NInqA+7u2yu194iA5hbVsPygIWn09xlGjVOihjFKdfaA7ugxVNP+BWyRgn9xV uEtKUg8fAmHyQYHgnK0t4dJVv27NipgOplLQfzeb397VhLnWSTbJDReFjXs+rRE/uml34DWEry8 QJOLPJiK/bIpJRZkFKWdezb96UDx+LAoMo8hpydlS4aKavl1C2vinpEZDIycTKG4qDsv1Ia+zu6 67iXBce8oeGualOMgGgHhj+McojSwc1JpJ/lFfgmQBHAbJIsxtQocjifnDamN8mFEUizpfUVZqd b92CpbBWCVYoH9vK9/iUajpw8r3eKDND4Pk0RhzVTNqalxxULJiko850yuc6T3SzZCFJnSrAw4+ H4Pv6RtPZaZQjCVAqaV2UlheqvBWmZb/uvjg2XWg12ueP9eZzwDrejLbu8LYhtODuOZOVzKy1ly uBm/U/gqmyxf7zw96FUGBHpkKV2B8oy/I6LFUC9e8NfTdQtNGjJowJp0eX2BERvWuF4t1ZtEWMx B/OCCnHV5+kTUOn5IIp0VQTt5GL8JpgIap9WsZOYfI249nfJ/oXcVrAw3nPlcPFT3PPXQEPAdNY qquCs7fdI+jeZyWbfe8GBblH0wseXpRDI60QFXed+N36Eh0yZLVM954OUpIdzDjllbATYhv+ztI DuVpeYX1/ZZY/Vw== 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. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- 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 4a5b7ec914efa..2d14a6261a313 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 Tue Oct 7 14:48:38 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 673D9248871; Tue, 8 Jul 2025 19:45: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=1752003954; cv=none; b=oTpb7Mh5Sq24Oh5bCIHYhcPHICV9NJIoqhzxGAT8CU4HcIO+h3UUcKTgTqvxmHj/KFmrkU2FackjTKkvno898oHJTmqOgA3cMda8Q17mmRoOJgWGj+ECEiQyPmIjNO4DDFOUeGDThksDph8ChL35JcWA3/XAiewRewbOIHONAOE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003954; c=relaxed/simple; bh=RO+tyqWrHCbnzP2crbU2WSIANTt1IoquzI2eUdQ4Jgs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bHU1fvGPbvTgm2MNSR3oqo/44iQgBqip/r5ZLLjEbnrU9EXJw4fl1m0P7te5s2QqaurwQbb2yscopjAz76r1TjzCKJvL6DqSrPukEIxhYDtl2JQz+LSjJXvJsG0KrljJFfSN5GfDZrUe9UXTFvm/fPqxF9uBgdeGTKN5f7SsbKc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DTmIp99g; 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="DTmIp99g" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56682C4CEF0; Tue, 8 Jul 2025 19:45:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003954; bh=RO+tyqWrHCbnzP2crbU2WSIANTt1IoquzI2eUdQ4Jgs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DTmIp99gTpVr5Jmw3A0m1aU4I6v3Pdo7AOA1XhgzGwkd+3fmMH6Buf70sun8PovGd 7Vay58Nra6QDFkzv7nuLj0WcO8okpLBYP7BuG5pnGXu2QRupWpc7NvX5J6rxwea0oW f15C1+2bA9S1fYf1026410nNYAZaLXrMfshxDSb5A+hpaer6PHVOF/i27jOXT+zy2y y2h4XIHOcoNgVnR2KpazGnEhbIEEz1rrmLCPBkzrDfoZ9TcNbu6UVROAwRF+SUgPzZ 6mDr2qZRBe0C5Fih2zBrKvZ3COXo9rPC5WnyM0qR1wABWTBiERH1B0kV+o4F679Vd2 qNeK0GRSJbF4w== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:03 +0200 Subject: [PATCH v2 08/14] 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: <20250708-rnull-up-v6-16-v2-8-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=963; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=RO+tyqWrHCbnzP2crbU2WSIANTt1IoquzI2eUdQ4Jgs=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVH+GZmiXeE7Y47innOMVFwgcZO1TcQ4uQ6Y NkEoJ3KD2aJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11RwAKCRDhuBo+eShj d4eGEACunnqesWrxZ0yRDuKSHCfSG7jn9oz45l2C2TjKpg+R9osmock0kBfUwAXlmFRu9eg/tF1 lZHv95o6PEouK/Yp8FaohtxnrM1r4uNsHcLbBo3tQ87ItTCT/vPmFbMwiK11Yr1Gw6YYsKLEYe3 YKPilfB7jHN4gRfVqphThGPVJU+TQnnQQA0zHKlxFyXdlWDnVhqdW9e7rY7rFhYU7mmLuUPJ7xJ FhJuKXhAyMTqVW3NjbUeXeqgqwQH29AHAjQvfFTSbIut7hiR81J6cVGZR7xmUCmV3WKoExSKBoI lKsHog4QNUesnMe/PNaek/wnsurTEdfVWu+1DRys6mT0dBPBfPXDUiO6nre0nFIj3tMhz2yprTK LB3Pq6kk/+zb7RtOTufb9bW6p0yrtdbYREsJWXFSyQDH9+pNM8BpZMKoEI/znQTd+6rvKzoIikR O3LvpElBlnSNCPbGMLNEY5vGKpAhXZOexgWe1oNB7iNsKi4gHs51LSq3tKnlpuIFoiDfUCWSWvw uOABhPHgLYcTNm9liUbQP1nTpqBEoXDfKFGAhZtTVVNnF9d8MyGV734hXmqTIravYAgm8S8b5Ij 9J2mfSKiLS5UmMBF1CJLQFTPYxcUViEKelabjdQhI2xQuPd9SvtoIw6W+dS+0XVTbeLSskdi+Nf JZv7rwkCJjYQBWA== 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. Signed-off-by: Andreas Hindborg --- rust/kernel/block.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rust/kernel/block.rs b/rust/kernel/block.rs index 150f710efe5b4..7461adf4d7e06 100644 --- a/rust/kernel/block.rs +++ b/rust/kernel/block.rs @@ -3,3 +3,15 @@ //! 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; + +/// Power of two difference in size of a page and size of a sector. +pub const PAGE_SECTORS_SHIFT: u32 =3D bindings::PAGE_SECTORS_SHIFT; --=20 2.47.2 From nobody Tue Oct 7 14:48:38 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 C4BCC2192F9; Tue, 8 Jul 2025 19:45:36 +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=1752003939; cv=none; b=VIwoFOUsLYBUiTX+pn4OlVjkgpva1lEItjI9D1IrmI4IeVolTEWgVCpLmuvOvqxN0xIEx26W9KzXw7wZ2tyl9EYCXimD8aPUuu/TRWxHPgergQwbZxHXk3WNxCqbyya22p0yy/gEcs2U7TEAY86H0641YSFmjIEzAc3BoLZxQ2o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003939; c=relaxed/simple; bh=CTNIjJksV39rh1vFRofP0gEi36ZUmiBHOsMajM2KEl0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=De4lmhlKLvdPFPJaaoQ6CcQeGg/wWUvaKrRjiN50mdFIvEUXCV3kVANfGQp4yAbyA0FfYRA9KQrD4Lh9ef7WKnsq/mQN/fqEp0C8MW8Ioe+MuMTsJneb4w/fWYOnzzUWZ8zTlCIk/mAq9MoW9moyHvYJgWo9G0n5hNWSayv+bJc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uR0kh2cr; 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="uR0kh2cr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24D22C4CEED; Tue, 8 Jul 2025 19:45:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003936; bh=CTNIjJksV39rh1vFRofP0gEi36ZUmiBHOsMajM2KEl0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=uR0kh2crNrboAt87xB96IzHU+EicAu2vZIpdaro6u/wXy56susc+EOCfLvgOKAP3y tE4df7XUEpvmSGjZeWN3ubj6kPL1zW/vHtPTec0faxAcvC4WBMaB63ZBPORuvvRSTN 38ctoc/ICnR0GVkXErQ3RGhO6Z5qZypnklztmaGawOOa33dRzLW7E72Wd5VNzwREtq /LkE9ZZpFNUqRpLFeApAm6492nmRPEQwZB0k/zkC6Sz8RNzABiXLdiupmlJTx3wx0e Jkx7nSADRJhW4hTJR/tUbhYWGhpRLLoZ6FC1go1kIQxjbb1aiG6YUJ9y8Dru619AFz aot+C7hxcV51A== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:04 +0200 Subject: [PATCH v2 09/14] 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: <20250708-rnull-up-v6-16-v2-9-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=3682; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=CTNIjJksV39rh1vFRofP0gEi36ZUmiBHOsMajM2KEl0=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVIHFJxLawG6BbjGd2qf1DI9BWdswMqTl91c VwaqS1iL12JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11SAAKCRDhuBo+eShj dxDtD/wKS34qv+11LdMaa9Lkxb2/fdmDkQcNx7CBaTf0JLBxSwmYuom1Vz6ozCbTioAgh8t03mc SKAin0ySXlz/dRuG46NzFumnQEnwpkPr7fwAwp5VrqAEz0bBfUHQd9tE8L0pDeOhuh5+d1oeOWn CVOLacjT6Qds2vSbIngsgHFKr16M9Tx2Wy0DRwcBm/1Uy5HuCV6aUoUARisIPZr+rk6rXtZ3kS9 GpadYe8bpRAJSofdh5DKsTsz+6y95y2H95y0C37j0YMska+7w5Fb/kZ7d0hnyJD2FGTJ7BjWl+q I5C5vfDq/rcKAHW70W+cAbb7PMommxqToEwbsyf9P3I8/d5LOepTPWfszwWqtgFT+JV3bfvuTGe dqEYJNgHFeXWHBWYrKHxbbccxlOYPxqGpFJknHJQ+k67NE4lzKiJ/7FXi8W8C29EnJHWzNWwdI4 8ZVeP218YHwcSIDSUsJ8ldCqHJeF79ERoK8Hn+HFzt7TIT5yw509FH77zULcyaKK+RG0HkHYxY8 GqC3oK8vvgRRXiyaakMIukJZDqLLS3SnmXSelojyhhPFxszoQ6HltQFRAzWXBXEtcHIxvUz0opk OWu8EyfLtvc8jkQVbqDWdNV6AkOZwkRsQNx6rc2yHwfaY8vcWwJZeZdMJl7O7+l7oF00kqMdRo2 Mc+o1v+5Q243dWg== 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. 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 0c1d245bf7b84..29b14aec3559f 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4246,7 +4246,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 0f70e2374e7fa..6b50dbc0495ba 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" @@ -354,15 +355,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 097707aca7255..aba3e93d5014b 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 @@ -39,6 +36,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 0000000000000..6dc5aff96bf4e --- /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 0000000000000..11cfa5e615dcf --- /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 Tue Oct 7 14:48:38 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 1DAA323D2AD; Tue, 8 Jul 2025 19:46: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=1752003969; cv=none; b=YNqfzrE+FEj93c391PUtA+2t3upebmKkUo8gyXHFC36tn0XbAL6A2W6ZReLLP/PA6HKxsXmTscm2O9sQt6araEkc7lp6SC5d2X8YxjWmVk/gQQXZoQvEyXey8yTwp0C+aMLUhqsYuuOSTIbQw/dZGmkO4BW//GDgQlnQ7ROgnfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003969; c=relaxed/simple; bh=EfT5ee6ZDKLcdtWm0kzm2QQNu8YrPCPhmrWRg9QQ6MM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=i+O4ZjBaV6XN36rA0slFWI04jqr8dV0U6s5+i8oM0VhG5Ggp57XBfWsBz9gtWJme9QS4fA6pkw6nJZgCTeUVpf7X1AKAgcp63oUizvsiaSweRUyIsJNWfw9kxVb8uL3zUo8VNads9aJwConRF0CF2GF3W0PHJuOGd7I9go2itgc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=d645AdX2; 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="d645AdX2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BEFFBC4CEED; Tue, 8 Jul 2025 19:46:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003968; bh=EfT5ee6ZDKLcdtWm0kzm2QQNu8YrPCPhmrWRg9QQ6MM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=d645AdX2szkm9zIhuE0bIY1GB8Gnb1vJA63AVZoDtbDoK24VJO8rgVXymOrXnrt3g aJggyLnbzGb4qZB9KWpAA6UCVlaJJnrj+3onnoMpheMfuBK7/06d8LCWtWOXwr01QX /8Y3r7FPTrRB7l+6+Eq+ka/0RhDkrdua1W2IIlhhUKHTurAkFdYSoG82YHTjyYRQuh LULWIGP/orOw/tjA9VY6/u1RbNq+JEIvsVaNTBChlgMqzskT2pVlXhvGfBkBbSjjms sOlp4KeDMsPxgX3Jn1jAIHD/B7W+uTtnnFeN34r043/efwOHJ4B71RpVtjXUv2CxAB hQRxOX9xubTmA== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:05 +0200 Subject: [PATCH v2 10/14] 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: <20250708-rnull-up-v6-16-v2-10-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=10929; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=EfT5ee6ZDKLcdtWm0kzm2QQNu8YrPCPhmrWRg9QQ6MM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVJu5SzzA11kvRG+tlEpGDFFWCfaGOVS2D8Y NX5MSVPNc+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11SQAKCRDhuBo+eShj d1gEEACkqtbzGrUUd5y4vgoaQCMxGvGe5Wv1x04omOzhFzmdinPzWwQIBCAkwPkD95uBmgnfVgA xJjXFGRTHOucLhRla97fTixdTy3HEDIZKyOdwJi+dFXWArEvEuO0i4HZlIdDDhTpNkriBS9Az1V wZYRig8tw364RIaMBnyQ0iC3UTFp2MGxXZntVdMUcoYmsEyP14R8Zrr3UFOOerR1ZW1Kzj4bHB8 pmmScnIe0Atdg4XMkuYngiV7hb4n7i3Bb/gm4U8SuC/A3qbcGO9fIlOgQRmxtcV5xoB+2GMOYSN w4BmhkUnWLzk2VJVZxaBQQkZdrLKeZrhBzkaK0h/CwUpl1DgI1QYRjs6eTnzyMzkcRfJA0FK9Ls zTXcE5f6iCjwxCt9Bhgf/KQ3MIhsqEG1p3vDw8CZ3b/qEHKKSTFP7P7MVp4xF+rhi+zFsMnCwAR z5stGqgSzhhWvxgiIwFfZYKVXZUY46V/7/J6K5d94LvlcgdReQhytFcIsX/BXSaJY8FI2/hDjsG mz7mZM1D20nW6olplZT2WaidO8eJngj84OaZX7HOAA+czGpyFPmH5fYn0YonagIpOtmVy2itUOG McuvfHlkuT0NYrDv0DTfDxdgJUMBZAflMMpzhKDATvoi/NNFjNwIYwVXTbe0ruJI3TkGaZ5IINQ Ehhv6IG7yvjwCyQ== 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 --- drivers/block/rnull/Kconfig | 2 +- drivers/block/rnull/configfs.rs | 220 +++++++++++++++++++++++++++++++++++= ++++ drivers/block/rnull/rnull.rs | 58 ++++++----- rust/kernel/block/mq/gen_disk.rs | 2 +- 4 files changed, 253 insertions(+), 29 deletions(-) diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig index 6dc5aff96bf4e..7bc5b376c128b 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 0000000000000..e7bd2b8f1e905 --- /dev/null +++ b/drivers/block/rnull/configfs.rs @@ -0,0 +1,220 @@ +// 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::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::BorrowFormatter::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::BorrowFormatter::new(page)?; + + if this.data.lock().powered { + writer.write_fmt(fmt!("1\n"))?; + } else { + writer.write_fmt(fmt!("0\n"))?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + let power_op: bool =3D core::str::from_utf8(page)? + .trim() + .parse::() + .map_err(|_| kernel::error::code::EINVAL)? + !=3D 0; + + 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::BorrowFormatter::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(|_| kernel::error::code::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::BorrowFormatter::new(page)?; + + if this.data.lock().rotational { + writer.write_fmt(fmt!("1\n"))?; + } else { + writer.write_fmt(fmt!("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 core::str::from_utf8(page)? + .trim() + .parse::() + .map_err(|_| kernel::error::code::EINVAL)? + !=3D 0; + + 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::BorrowFormatter::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(|_| kernel::error::code::EINVAL)?; + + this.data.lock().capacity_mib =3D value; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index d07e76ae2c13f..d09bc77861e4e 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 e0e42f7028276..6cf0f3d411b4f 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -50,7 +50,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 Tue Oct 7 14:48:38 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 24B7323D28F; Tue, 8 Jul 2025 19:46: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=1752003962; cv=none; b=GYaETqMscqQt9Fe8Cj1h0n3SNQuElE3sZ1rHl/AdKLO1kXMhodPc87aMoimDX4OfEjSN2xST+BzOu6PUMXkO83F3dZ9B8ApTh/NYfGeOX4156UTzWaXM7IvTMf40+JarqkNj6cEIUkNHR+Ux9zeOK9SC7SIG98QIAkMSn6/kLrc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003962; c=relaxed/simple; bh=GcQ1UJrPDOA7bSA+9kYVY3/YDxzXw1Xb34x01G6QVg8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JpscrWcbNiJmQshR5y0gBxltZpnkCIzdg9J78nSJyHJQ1cTbGBgATNUqZ99//pihgcJh+6aH5l3BIzou2dr1wfGjP2G6mBgD3j3Szocy5bkLcjtB4pI8Kg9bqqLpxqMSiYl3eS65esCAmD+7rE3u8ittog6T6FGkdp43VJjKpig= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nrO1icVF; 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="nrO1icVF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9FB8FC4CEED; Tue, 8 Jul 2025 19:45:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003961; bh=GcQ1UJrPDOA7bSA+9kYVY3/YDxzXw1Xb34x01G6QVg8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=nrO1icVFEdUYQIi0o9pQJnFYeXmBnf7b4LS8oEuKCA2rCXWr7p0+1jUXzMbdnseXk WNpzW74Pfownuj/7ylbueznro03swhpvh3UkK4GBUtKg3yjWSxg+7eqxN5M+W4NVGs i4DAbQ9yz/fCabFayHAwODX8obqZfRvRxfTq4imSFTdtwaBJP1E4mL2i9NpH4aHbms pm9ZiAneyHM7IbiVKrbBrxML6G4c8n3oWvlZExfOZn/JgCxhen71c6iBwLj5zyFLrI pSNE3lgSBPOYnAPj5kzEWi8NqFuwSesJVsB96bzxEXn65/aQN6Q7NLseKNGk9eEOQe /caVJDv+Wv00g== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:06 +0200 Subject: [PATCH v2 11/14] 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: <20250708-rnull-up-v6-16-v2-11-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=10559; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=GcQ1UJrPDOA7bSA+9kYVY3/YDxzXw1Xb34x01G6QVg8=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVKEY2BN+1iBPkcg+RH9zwpRCzjcmM7SHQLL s1ERIXURpWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11SgAKCRDhuBo+eShj d1+YEAC8f46HtQZdmV2Od6JCohRX5mbBJ40ZX+LUi2tTMIy+Y2xU2eRi5+zMU8wyVb2Reh9RNfY eArJblQVX0yf528uCgYpri2ds9TJvFNyOJJ1VW+JY04CO1WobNOrvQULr8yy6X4G6+F0YKYjpak 438wOVGyi8B+3Qa9+OyzJYr1Gt6jW4KKR2u+4JXpKLH5xkT5bpuwL8+gQz8VsHCNbU5hPYjfdiD r3MuggtMDpsRN9i21sp6ctHg7G4bFvRxK591U9mbMnGMnDYZmVFUWJvXcGnPjqwEYuzN3ikvqQh myGAGnwvN9baq8vA+UiETMAhSFLRSznriToJPyzCDSIjmWuIH4bni+8PJ6ExvUUfwG85dtHkdY0 t++TV4fo25S+0R/vqYltNylhoS58cp9WMcddpSKJGSQTq+uQFMPnbP4ZrvRnUoUes1n+B6RRQro 9iowx+WdQMPgA3fT6EH+gYImy6rABLcFSjoYhw1Sk4Ianpp83GHcPygbfpOo0+QtV85VJoaVfzv 1wSHFr4Mviu2ufC+NSUY5cB/phXyJl+XzDWQRTqU/nypmHZWJDBLNUfm+YlaKa+Y6NwHvLcoUUM yCIpfNotfPtuZRzSoYZrW9ikvZEk5TrkhwKGwcfOx5tUmTFnPb459KfaOOvtA3KpqVo/CzWMGm+ /APH4EW9WGl+umw== 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 --- 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 d09bc77861e4e..a012c59ecb3c9 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 faa3ccb5a49a8..34b7425fa94d8 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 6cf0f3d411b4f..4ab658cf811eb 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -12,6 +12,7 @@ static_lock_class, str::NullBorrowFormatter, sync::Arc, + types::{ForeignOwnable, ScopeGuard}, }; use core::fmt::{self, Write}; =20 @@ -97,7 +98,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 + 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 @@ -112,7 +120,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(), ) })?; @@ -165,8 +173,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, @@ -178,9 +190,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, @@ -192,9 +205,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. + let _queue_data =3D unsafe { T::QueueData::from_foreign(queue_data= .cast()) }; } } diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index 864ff379dc918..c50959d5517bc 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.cast()= ) }; + // 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.cast()= ) }; + T::commit_rqs(queue_data) } =20 /// This function is called by the C kernel. It is not currently --=20 2.47.2 From nobody Tue Oct 7 14:48:38 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 DAC1423C507; Tue, 8 Jul 2025 19:45:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003941; cv=none; b=l4sBJsmqQImLS6rX2sjmQFdK06yilggkrMIVNeZXVyWZrAHsRxBzUdyTkb41y1Sc2AWi2rKwKhgqky0xbMcJch0rP/DO5fydCfV7O4m3g+Lp/Asqa1mky3oOQO++9piLzzCKjmi9uaCZjHgpk7iIovvoQ2dn1bEgtHVAVbJVYrQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003941; c=relaxed/simple; bh=z0+jOSZzCCJUSyUkPI8s+JDMHgPTdqPWG6ejwrYP3mo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W5my0Bola2vtdCGoHtK2gnCPG2WY2VZfwSgpcrczB080xn8rPrsstRgTd5iyVuwIaZAePcrpjwt922jgknl+VCZ3aDMQRSAx4YFTBTqOkO3oz8tlbbbFHiVI2bZ23OWF+yZpfEKF70x0Rxg5RA6wUymJOaXXV8B21/7Y52aTKBM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=KW4fjsve; 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="KW4fjsve" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07650C4CEF0; Tue, 8 Jul 2025 19:45:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003940; bh=z0+jOSZzCCJUSyUkPI8s+JDMHgPTdqPWG6ejwrYP3mo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=KW4fjsverz1sLkNuZuhm+M1UyyaFr38t6Tn/wRUdn5D6gEHeN0TZ2oMneVlaKRpv8 CHcqBUHvrsxRE6D9RntP5yCJ0fAfv9Y16DLd60WzRFl0qnes35VRzVKMp/o/+YPyc6 lxDlBpOU3R1yj6qTy8pU0iUXD4xp+dNPNpV0zpA0msQ6FJJIiGSJRJ8tq2M2YVOoQr pFFuydt2uLsW27Ft2GpP7exsQ+oGlRAKlF2/kkuTOPkiHOvZShJjVBCm/vx/ZF4E+T tMVB1BVTd8jJC/nkn3wnhytpOLxanpJ1pQGklSMxR+5ndVbcalU6Al24aRH9p/3lPF BlM5/jd7S0Vnw== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:07 +0200 Subject: [PATCH v2 12/14] 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: <20250708-rnull-up-v6-16-v2-12-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=930; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=z0+jOSZzCCJUSyUkPI8s+JDMHgPTdqPWG6ejwrYP3mo=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVL2BNjzLJTazu9p0aBbWi5VxY6sIudKezm9 U9lZkXR/JCJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11SwAKCRDhuBo+eShj d4jzD/0Vl+QhDQ9TzC2wDCbB2E0Zswy8wbWlcX9YsLptw6xfv2xcV4TNB5D8CCXf5wEOVmMspiV dqeaUplFJcZftgDCKTWsST5Pnag6PhRoBI/DMTprQle1kH2TwnIp1BXZ/GWkgV9UXi6/3DVp1XX 29fYB/pkmBQcmA80JBfVyR8EV+JCzEmqHJqHNj2nWXAU2Fy8/FK28ZXnmMJzvathTR9UKqJvSno cn2h/Ctn+MPGwTf+Syo4gIJQxauZ9adFI1Ll7fHkRaXgRcRJteJV9USL1oT/EnF9QAT2XFkCUs0 rN3BkrKBmxYTNK6Z85+cAL6nmmwymeelCuBdKkHNQMwA/2zkcFJOAkQsf8dYbWNMa/ERNjlnqNS hDDmNHLPqge0bHF0p/h4RpeObCDiBB3c1REtWhPmqA14hYjxa/x8uEv87DVGLJGR+tmhpXVJ1jn joc14CFRj65S7jPyL8uQrfKRnBG0ZY4DcFL87vj9399MhAhsg8It2vdIy62LaLHcm6LFPdPL6dV oojFpnlh7052HED5KKB/qL51rv+1vB88A79ksd8uUDhB71Om9MPFtOr+3kzB3+He6H01EWU+RmL bop21V7zo2MynDrykz2s6rrFakFZrLdRVljfziehp721rP7sKysvWBL9lF0ZhPvafSZm9YLr6mz Aaz9wZXLFPJc1hQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add code block quotes to a safety comment. 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 2d14a6261a313..873d00db74dd5 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -143,7 +143,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 Tue Oct 7 14:48:38 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 565A124110F; Tue, 8 Jul 2025 19:45: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=1752003944; cv=none; b=DyaQT58gxKujNPlxc8KCvWcPNrOlaeOU0mqqDJLPe7uW4CY+v3euYzGz8FoUkzC6mhp9O0NAyp/eFLejrK/tYDX8zaVECIGWz0sWnnJ1QC9bHKiLjsPkZH2Bwzpap19WGRlu5ZUCBkwT4ECIgQ61T4q80rsgbhZ6qAEFAIlqTws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003944; c=relaxed/simple; bh=S1YvVw9btOE1k1+ZSLyFicKOHlIpthgkLxSgRfxxtWw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TdDjL+wg3yUnSbXNdQufkiZYgx4A2D8gwmh3lULWLPjeXI5V6rDpHKQVrcLqcNIbLl1mYaTy3YoF/QSu+OU7SL2MyM5PWwh+RXyYFw0coENrsjXlH0VAuias3Myge54o+g/MYyVd3w52uRjGiTGlwahax1tLEmczYMm0C9d5oVM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=K2sPBeK8; 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="K2sPBeK8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 07678C4CEF5; Tue, 8 Jul 2025 19:45:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003943; bh=S1YvVw9btOE1k1+ZSLyFicKOHlIpthgkLxSgRfxxtWw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=K2sPBeK8Ob1xX3FzWQFmqJWh/ju9lb8M/ExBDTg4oRsESQvtMGRibzpYIOMf9Wxnz D+/LMdS+gt9fMN0fXa+SixlYBQXS67FBGVT8ZwjzS77pW9kYFUB1ZOzTVm6tV+xo1J 9wVc4wV+u+lAtkfrx1RCsjpPRHaD0muG18ptPCdZDBb5iKuPYipfadNs8bmc5+NJJt preJdrRmaqsHEXgXVdMi51gVJyDyVgrMKNaOnw7H00oP+RvKmC0ag9Ev7/qRih/mWJ 1zv254KjrVixShrWRB/CjTKJse32gQUh2tIJYaMUnqFZ9BIfWEHGn9jtluTbET+uzw jCYkbi0VkVDlQ== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:08 +0200 Subject: [PATCH v2 13/14] 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: <20250708-rnull-up-v6-16-v2-13-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=4996; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=S1YvVw9btOE1k1+ZSLyFicKOHlIpthgkLxSgRfxxtWw=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVMj4/Hj1QhktbhgAd/zLaImmyq/E+z3nyBy OIAr/soTqiJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11TAAKCRDhuBo+eShj dyqwD/9ELvYnnB689pWIYcj2I1pJyU0ZJHkYN17INvKyj2NTOT8D4EPzgSu0cYyXKSABh0mqIiW eAZQDsghBRLk30WlLRAS9fusg4TsBP4arKKqWGpQo2Qsx9+Y8RGbEZ6FHP2f+ln8nQ69ZrOVduo KUQCtJww9M0mYRMCO1ZiuqPnAKx7Tegljcsf+DFjQlsgMPgk0VUCb0cOf1SdAlNPLyVqZdK5Cih XPYx2yhTFeIcL5S+Sa2GwwVS9K1QzGIHs8V1RcMbushb0TAlOvNeCFuxj4+3fx0HHFCTh8SI4eb kjTrC4TE4sWdvZAuPD9SVIHuC7CeQGvgrQ0Z2LVfn8LtZ96IMyRHRM6fa5Z3zYzeA6zgzuQfY3n tYtM5gZYczBfIk/KPzEv/x1/1jhJwoofswzAcpu8vvvfVZ1OceNiTFpFCXlN39lVHFNJvjEy/Hz t6p9aHoMpwlrM45Ad8DaxpcuTGBFZo6/4VEuUPufm3QemOTTvzFCwyShoyvlUfe0i98rAH0xkjn hB2dKDfPphTwuji59H/aQowzO/vBsLWQ4AT9wj/wS+wfT35hIul+mS3ixf7tuOWon+swJ1xN51z vAe0QBDLBEPWGJbL6Y5O0xHZb2gpXrpMiDXjADHlj5mQeEjB+IWpiKL2xq+IofQPgTAtoI1muTA 5XCeus1Gf9ydb3w== 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`. 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 a012c59ecb3c9..371786be7f476 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 34b7425fa94d8..551ef38efea25 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 c50959d5517bc..b6b26cebd4f55 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 873d00db74dd5..244578a802ceb 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -130,6 +130,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 Tue Oct 7 14:48:38 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 6F6D524395C; Tue, 8 Jul 2025 19:45: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=1752003947; cv=none; b=Jmw5rFhblU2TPs9mw/8KC1DGc5Z9bGUElQBZz6zdGP3MF9KZC5VO/6azJTR5+hjXyZ3ibMnlmhfIlv6E6O8E3w9NBdAGAS65Te8A9zdmsUBjL+LNRawSjk1V0WprjzN8jHiCnQpecX+S1bSbw8SRYhCfBwvIAJv7h7ZbQn8AnJc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752003947; c=relaxed/simple; bh=KyYAdTjq7vXAKf52Y9J5Q0lQpvupgzRX8cJI0RjW8t8=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FXYxH9bMk2RWKAC2AFLmJpzgWhs1+YiyeGpDiO2hQgtcpAlP5/nfpJXISna+ikcwckprYVcss0Q4qwQG/Euf0LYE/MSDvAEkPeWegTUpgfGi48mvopg2GyK+YQGB7nh74ibyNJT027gQqFRvxjAWvXmjJua8HKad55/LuqNDFKI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=aTcQb7zJ; 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="aTcQb7zJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 885D9C4CEED; Tue, 8 Jul 2025 19:45:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752003947; bh=KyYAdTjq7vXAKf52Y9J5Q0lQpvupgzRX8cJI0RjW8t8=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=aTcQb7zJQq6bVWrJiUhU/TPXZIgXc3KkoDJHJk/SjbRdgVbvl5ag4M6hc2fQE863d VBii1N38OysurjMNk6yzrf1ohPcXk08Y0I3N6RGcAAFRfVrxvMZH8GqmAgeyUW1U0k A9Ewkmuvfwu5q5rvtcDba3tfKmOAUtpWKolItDSQSIED3TlDL8t1jl4aTOXY9aZRbF GgNsgG17hNHDUcgHkzzfhhOaItTGuvpZ1roamt4W9ePegCthSmNJC4aNGLhc6uDmAR qcJUVsOiGcFA+xyb+OLDWQMcoaPA9mVqzYre3yyWOFmIPJ+xrHz7BGeYK3qxR29x1N MH4jGUKw6TRig== From: Andreas Hindborg Date: Tue, 08 Jul 2025 21:45:09 +0200 Subject: [PATCH v2 14/14] 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: <20250708-rnull-up-v6-16-v2-14-ab93c0ff429b@kernel.org> References: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@kernel.org> In-Reply-To: <20250708-rnull-up-v6-16-v2-0-ab93c0ff429b@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 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=6431; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=KyYAdTjq7vXAKf52Y9J5Q0lQpvupgzRX8cJI0RjW8t8=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBobXVNYdc2WyE6oKp1TOjtf2Lt4LycJsFSVCzgr /MTspRzJumJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaG11TQAKCRDhuBo+eShj dwJdD/9vJBxCqEBSG2raQocTX4RuWNggHvlY/VLjAP77sFfWpQaXwb/WRpqbEV7P0R6UE6MOAbs q8/cwespWiSMdku9dPFIHFoFIfVNcai66yIHI7e0HafOm1ytkfwbVmesLHNtnnwAVI+3GTt6Dj+ CQNTug0GxMBEhw5FzxUk6y6+Yj8vOp8sIVr7PZfAVQ/7WOGij1ZxNxme8Kb+auX7DT2nWkLfGhg prBjpErKK6aNW3Q+KD5bRx/xHsJEGtPaTwxsm2xp91mZ6jmUj1CC+hgQGcl/wXWWf7crJgvt8xA g7Jds833m4HNRmey3ByTXsIO65311OS0BQ9EaK/hwGlBc8g8/28kyEGHyUA3RVWp8VY1PU2HcOD OowVP+Gx3uSqmm7nitlhPnrGNGzThnaH2JUMojC7yX1PEQgOsymmGe8X+2V2tgHndwefSGuvzv3 wE9urzlb6ZdE44Wz0+6eJ2qm+dEZzPNFRfePfm1jeR36HbrqP/t1Eo6qcbRjQ8F2ZBFk59oHiKt xS7hcc0ugkOlVcPt1FLZ9T+QhZ9QR5qOdgp6k3vp0E+E2I494DrixWFPgHCiXwwqMzF3zmmItz5 UrQKTNkX00TYiwb9XK5kNa+X145SvH1SW+oh7CfzBO8MvObkhE4vlRidCDz0l+acmMrLd/VR5BE pl42itOTm0YJiKw== 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 --- drivers/block/rnull/configfs.rs | 61 +++++++++++++++++++++++++++++++++++++= ++-- drivers/block/rnull/rnull.rs | 32 +++++++++++++-------- 2 files changed, 80 insertions(+), 13 deletions(-) diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs= .rs index e7bd2b8f1e905..19bc7278546ca 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::BorrowFormatter::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(kernel::error::code::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 @@ -126,6 +157,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 { @@ -218,3 +250,28 @@ 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::BorrowFormatter::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(|_| kernel::error::code::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 371786be7f476..85b1509a31065 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