From nobody Sat Oct 4 22:33:05 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 F2B082E717B; Tue, 12 Aug 2025 08:46:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988395; cv=none; b=kGEk1JGzvKXqmcUEasnRm2HymPIxG2B+ID/ZVxGGyq18o91gzhJ0891/snmLgHVVrIUIU5J/BUBC1cmDmzOpG9JFO8tR7XwnADzDZGQaPzk05eTslgk2VOun5B35Byl5jdRRHUhwLMfWO8l9+J2/yGjApC6tA4JRCqNsr7UA3/E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988395; c=relaxed/simple; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XM/UpYGQThx5Mjky7e1xJ2V2TgCgelI4epvoKQGk5L0gv4ylzxJOm7dFFzxnTomqb+VWXXGmsGIufbFge9ZIYRG82uX9Iv6iHAWxUEcdiwzvEHIf25RCG/srhh2xIDfIcKNGOMJqJMoGbChy6MD5VQCzfaLKd/p3tbS+it/yzv8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DV0jDIDQ; 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="DV0jDIDQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B3F9C4CEF0; Tue, 12 Aug 2025 08:46:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988394; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DV0jDIDQWrHdaKqN1ICXporUHHalbuZre5EuRTNF9dY+pGlJ0qqL7+0k1AlhuTFHu /GHwk5riCa2ms7ymX7MpM/Ei6s3+Y+9j2nBMiTsB3F0Tp1Jzbj/0Y0UWVemNjRR/Gh crBWB1RGRYzCjV9SZXxZPvIMs4r+jNOPprYJgFTdj0rYKLzPSIM0nvMfU+FC2Ntrv6 uVUWMrqVEUctcqTzvgMhI+QnQtjplsd2ov3UK8KFREKN0RYdQjF7HG2LYXLLzy+Ok2 6cIPWXPCYVtWbccA/GXRQ29kMHNnu6QJM3HAEdDG67F4116GflisxBquAuxFzPYivU B6nPnPcaWRMsQ== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:19 +0200 Subject: [PATCH v4 01/15] 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: <20250812-rnull-up-v6-16-v4-1-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=871; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8czWZmbB3cBGXRwBeSNJDbgR/tNu0YBWMIP 0TvXbNe5kyJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/HAAKCRDhuBo+eShj d7DyD/0ZqKFk5Meb/0O9DOCFM2FffVYHIyetBoSlO6rjZDqjgseSlM0i0pVsy9HTd17zu8zW2er dRdFgZO/XOvOv/dqj8RVbcMQJBNtACKfPvAdDiwp0I3gG6hjCi1RtWBS/tkoNZMsYCL5uT2LghN Rh/Znkb86uxjVIV6W9+zCBOJZv9UhinvEkYQn1mNInBYchtaYw+fJbXFP7opGZC2a7MBnfrqs2v y+HtwY/e3+zzgTzeTODzb+GhcEkWVi9k18SeOFzC3+yrnuumZLT8qxsNmXNBFVoizsCT349dUfK 5dDFjRBK8ILhM6GUWMUqHPKWIsXnASKN6yuG5xcyup0YSyCUaiTDuFrIngpRiV3XYKSzRPUccwp GzLk7oeRYEqapMu3YfhfZtnLq1y15ue92Wd4/w9WnzU9JOiclq2e3b5DkszQKp3zHgHkh8jRVZb 0AhNyk6WFpkmrF4oX71yTa2XTiFaODzOIAh462LYoimK8EDO4CXkQx6u7lnTPv8d9fo7GBlOZ4L 1wDys7vFQc8/7gVe7AIG1qb/0kpV3uQc8lPdY0wvHqpTtRL+Gs8Rpy6+Ea+v6KoB7mqNpV4hRRP Q4sohRh6WEt/PYWm6MpjP64hWMC1WXZV6VsWUPoBAIQWLUlM4Yx+whJXJpRy4sLtlC/RVciXuDA qTQtcuE2I0+P6mA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Clean up imports in `str.rs`. This makes future code manipulation more manageable. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 6c892550c0ba..082790b7a621 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -2,12 +2,13 @@ =20 //! String representations. =20 -use crate::alloc::{flags::*, AllocError, KVec}; -use crate::fmt::{self, Write}; +use crate::{ + alloc::{flags::*, AllocError, KVec}, + fmt::{self, Write}, + prelude::*, +}; use core::ops::{self, Deref, DerefMut, Index}; =20 -use crate::prelude::*; - /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] pub struct BStr([u8]); --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 7315E2E62C8; Tue, 12 Aug 2025 08:46:26 +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=1754988386; cv=none; b=PnYRJfxMjEHr/U/Y09Dp4YdH0p7oULIYTwEZmfOYjnZIUndZL8BwJ1LPlvrpk7NOjgab8Mh4g4fOt1nUGn7lEoVmKBDN60xANsFxsVKg9NkfVkmgG4o+9Lo+BxUjrOUYPEi7C6CSc6K5xKos2zRiVWNUwzUzOCGx4ASMWTi4Ll0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988386; c=relaxed/simple; bh=LiaDRIukgxQ10jLoKnR7ZV1S7vIQx9wTW24ArpjaY5M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=RhoSsSgEglf51GuxH4RDgR2ATL+Z2fdPG7k8EuVTy4mMt/yLulO2OF1OYIl1gfgBmbT1hqD5f+4kzvTXVCg6SdLcsuudTXIiD6EkSA80wwS75bdNig0Sp0LnSq4zhF3FHlQa8jvKKrUDsLBgXA74dA347ijrbL2Ot6u/hl8wgwQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ft800Vv2; 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="ft800Vv2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2DF37C4CEF0; Tue, 12 Aug 2025 08:46:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988386; bh=LiaDRIukgxQ10jLoKnR7ZV1S7vIQx9wTW24ArpjaY5M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ft800Vv23gZxQJ+SZOvHct4qJJRRa0SWIqLK/KMimXn7YYReaEov4BM+h5G5dmVVi 1oUO2ZUXyv7cOqH+J1F5YMNMFsNoAmn45vfH49JjSUL+p/cK7GL7tbEvUmkvMNmLC3 txQmwJIXravQwYbP3Q0G78TuHDmn+lFGjxnYXgWBKuXGdmn4NtK5mxPoey91d5b2lp wOlkxDElAP5C+ilnX+jPeZ1HLycukFnQU//OCwPaNQED29LgbTmhwGVWOxSpQ+E0gv BPKdiNeKFsQXPUCdBqXdAxYyarGk/Gg+WpK5tmCPj5pz/UMakdmX/jquR2GqvbMKdv G/ZMkEqc+siuA== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:20 +0200 Subject: [PATCH v4 02/15] rust: str: allow `str::Formatter` to format into `&mut [u8]`. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250812-rnull-up-v6-16-v4-2-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=2282; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=LiaDRIukgxQ10jLoKnR7ZV1S7vIQx9wTW24ArpjaY5M=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8dOg+tWAebpOsOzOIbstCu7JSswO0iOdbYh ccMq+Xhv46JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/HQAKCRDhuBo+eShj d06iD/9CMSzzqlw4Eya2xtHn6xJ1Yxa7LWvHaxBhAFlzr/L4YxubXJ8y+RphqtBnm2V3nSTJDPp 7/WtlyAWJ5wtWlUWEcelv7qDbHlBuV4YbeXTCu6BRS+mvU5qGJDWDCRFPb/tQM5mbUnCDfnVN2d NJk1s0HOmu1B9ZzyUbB6vUirmFbg4LEBCsdTeTNCx8bf5fj9YFtZ4DVgWwlRcFQh7Ohw4Bn5uhF KfKxSwXeUg7TLKt3TWGdbaN3IvhEOjzb8p9nPSDEkg8WtvhQTK8VK64OSB7UT/6I3DO2GszmvKY SFK4qrnEY2bJypNoFGRmlj7MlJPlw8lFVOzhtvePH5FzlG/Ajv5bOGFnPNbdzBWKKBwHPca1qSe Gtw7cVLIiBfQ6K7qT8yisf3UYJQHab1sdoUwv/20ppNLo/+VW9axNFy00Nf3KBt4xrxcbMPW0Kn g8lF/dFhfvcjHFEMtv+pgStQWReK9+hekwz3u2yd3nDA/lCZ3szOKNP33TDU/RfAzR3GJPA5NZm L3xzdgL+b9cEHJqWhIzmN5M9dWjEozOvp/BAwOChpZ1uHv9Lt6rAqYjh2dthN9m8v3e51G2q77S fyW6UrTrUmIQi+rl5tQa0kFUfxMbEnjMSmjWw5iH4il29C4qXNdoJ4Bm++RMNGxhaBzThb0Zbf3 NIDhPJjG8xR2+lw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Improve `Formatter` so that it can write to an array or slice buffer. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- rust/kernel/str.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 082790b7a621..76632da357a6 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -7,7 +7,10 @@ fmt::{self, Write}, prelude::*, }; -use core::ops::{self, Deref, DerefMut, Index}; +use core::{ + marker::PhantomData, + ops::{self, Deref, DerefMut, Index}, +}; =20 /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] @@ -825,9 +828,9 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// Fails if callers attempt to write more than will fit in the buffer. -pub(crate) struct Formatter(RawFormatter); +pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); =20 -impl Formatter { +impl Formatter<'_> { /// Creates a new instance of [`Formatter`] with the given buffer. /// /// # Safety @@ -836,11 +839,19 @@ impl Formatter { /// for the lifetime of the returned [`Formatter`]. pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self { // SAFETY: The safety requirements of this function satisfy those = of the callee. - Self(unsafe { RawFormatter::from_buffer(buf, len) }) + Self(unsafe { RawFormatter::from_buffer(buf, len) }, PhantomData) + } + + /// Create a new [`Self`] instance. + #[expect(dead_code)] + pub(crate) fn new(buffer: &mut [u8]) -> Self { + // SAFETY: `buffer` is valid for writes for the entire length for + // the lifetime of `Self`. + unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len())= } } } =20 -impl Deref for Formatter { +impl Deref for Formatter<'_> { type Target =3D RawFormatter; =20 fn deref(&self) -> &Self::Target { @@ -848,7 +859,7 @@ fn deref(&self) -> &Self::Target { } } =20 -impl fmt::Write for Formatter { +impl fmt::Write for Formatter<'_> { fn write_str(&mut self, s: &str) -> fmt::Result { self.0.write_str(s)?; =20 --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 CA32D2E975A; Tue, 12 Aug 2025 08: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=1754988381; cv=none; b=f9lGFzI4S28XHfywwIgmzUxtRn+WQIUQiklhqO3wlXIJi4uVZDoofAqG69VqEE0QBW6e07H8tuUMeJxTbuiUB4kCPveRJteggpBRyENGY6RMrmLY25H9ot+3LlDVvZomaEv8k3BJWpSk+LSk5ozXQYVJJJNC/toJifQgFWTAqug= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988381; c=relaxed/simple; bh=BveodvWEzB6MGjcIRLP04oNvaHBriQqviB96NYNDa+M=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YNTtj0NnNrwwlruLfcoHksC9Ga8l0wUse6P6r8yKf6gmxNK+KHSJDhcq4nxbK3KscSXwYYSvpakql7cjyFKTi/MVLKYkc/rVGvcet8gCEmwyyzFKd4rY4njeif7z0A7TOZPb/t37HeCzv87KSjr4Zu3hDnCRKKzjWN6cqlD9YaE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f53wvq+h; 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="f53wvq+h" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95366C4CEF0; Tue, 12 Aug 2025 08:46:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988381; bh=BveodvWEzB6MGjcIRLP04oNvaHBriQqviB96NYNDa+M=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=f53wvq+hCa8Mw98rFvVkIOxx8V3faZJqmnNYw4Uv0S0A/1C5VGu7DOdu61uIRzrSF IrnsiR2xA+CsLP4NvLmG/0M0ENCjSBG8T5kdzbwrkzv3bmQem6WV9tgC/VAHEaRnEE WR5uss8GI5v/vQpHRPLYZxTedsPL/ztcg2GlhPa0crfXzMNJEDtchqQssUngS48iBh x/IdEwseLKpkkmW/oGYk2v5GGKhYrGdMtEcLkzLOnNLM0zE/nCdb2L5ha/E57qNF9q lZGok57JD4eWzkgMQh7VQ4NHPUEhJvIwrJAEpFosqlb/ANCAEVGfonXy5NpYcnuTdb vRcstGW5pksZA== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:21 +0200 Subject: [PATCH v4 03/15] rust: str: expose `str::{Formatter, RawFormatter}` publicly. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250812-rnull-up-v6-16-v4-3-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=1956; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=BveodvWEzB6MGjcIRLP04oNvaHBriQqviB96NYNDa+M=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8e1+pSFP4hOqDogQ+2AgdUf3lgAkjqSMCC1 SkWss+t5D+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/HgAKCRDhuBo+eShj dxtdD/9l0hy2XeewX2P0rBg1c3j+ESPyb6DIsPvAF92HbJHwpTB9pqwe2aMbzc485+uPm75WGuZ fuPMIbKvW6u19cqW+ICIh7hXV1KlQSySV6J8bBT7JB6Jj6vMbFgbQpbSjBRM7L0W6vR7Vvd+R5F 672Ih8d4zMre1zUvaNH5gr+rFRfsZU5Daif0BreOeaCfZNG3mUFrWxzmG/G1hc81Ve7gPZAxyVs PTcy0BGP+YkRG8J3aR8N52vswbw4KXxbpv0fwCyEV1i1H9hjlEHEdV8qHx7PrrIvMqcLMjUthKh txLP/Ha/FoKqbnxYTmDIm7uUh4JTCktBlW/lt4TvULlPen4K/2HplKVK+VnvvWIoamNlMt3jSgG 7UQ9BHwfGdxfQAjHxXFDGg8/sAbY4RLHapfN/8IdSC2CKStxEbz22jDV4IPkHDgdXcMP0SKsnV7 SoGHmw4Zk2TEa5MrdMuLeSDnIazg/t1LzB3y/euOazBvJJbn1zgXq2lTJAn5U8Pl6myQTxh3Lzc 3p3tQUffachPkOOTRXbJ3xYE38fMNalow0x1LqxBPow3WsPNLf64LLq2dvVgGGO3zhLVDycHbZ0 Ex7TejJObfvWL+jORHbm2hkOg862MA2BZ4WGK7JGh2m5p22Ru9377nFmMGKuHvDUoS4f1pey+1x eN6uknV5nhJN3dw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 rnull is going to make use of `str::Formatter` and `str::RawFormatter`, so expose them with public visibility. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- rust/kernel/str.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 76632da357a6..46cdc85dad63 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -736,7 +736,7 @@ fn test_bstr_debug() -> Result { /// /// The memory region between `pos` (inclusive) and `end` (exclusive) is v= alid for writes if `pos` /// is less than `end`. -pub(crate) struct RawFormatter { +pub struct RawFormatter { // Use `usize` to use `saturating_*` functions. beg: usize, pos: usize, @@ -794,7 +794,7 @@ pub(crate) fn pos(&self) -> *mut u8 { } =20 /// Returns the number of bytes written to the formatter. - pub(crate) fn bytes_written(&self) -> usize { + pub fn bytes_written(&self) -> usize { self.pos - self.beg } } @@ -828,7 +828,7 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// Fails if callers attempt to write more than will fit in the buffer. -pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); +pub struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); =20 impl Formatter<'_> { /// Creates a new instance of [`Formatter`] with the given buffer. @@ -843,8 +843,7 @@ pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usi= ze) -> Self { } =20 /// Create a new [`Self`] instance. - #[expect(dead_code)] - pub(crate) fn new(buffer: &mut [u8]) -> Self { + pub fn new(buffer: &mut [u8]) -> Self { // SAFETY: `buffer` is valid for writes for the entire length for // the lifetime of `Self`. unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len())= } --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 C36352E717B; Tue, 12 Aug 2025 08:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988399; cv=none; b=j2OeL6E+rYoEP5gAoayG+u/XCE/0HaEVKsZHkOXXh56Grt/UcpPb9CRUikrPoqpO5gZrn1yxShbVR8H9XUbZSdeiXOSyUXw2Ew/daLClDp7ZaOAH3tnnpmh1b2h5QxTKSA6N8HZvSoZltUV88D59KILBSJWW21gvasWHyNLtcGo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988399; c=relaxed/simple; bh=5vdh1riAR0ZL9xaLLQRBtVtO//nClfhfAq/IqeV6FBY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=mhvxgm1FO9fpiO7AKfG0X5TDKLe84/p/QiFYH6aI3p9uDHednN+dmoiA3tOPlP1yxnEVY++C7TPsZDEthQsDZsqlwA+Y4e5tUvmcvS8hurdB//zFuIynpApFYiFC4Xm90wOJpBdp34LEFqnc3pmPDbZDoUtwrQIilCynBq1xOF8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ZPD/D0Z4; 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="ZPD/D0Z4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 50E1BC4CEF0; Tue, 12 Aug 2025 08:46:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988398; bh=5vdh1riAR0ZL9xaLLQRBtVtO//nClfhfAq/IqeV6FBY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZPD/D0Z4UaD4iVP8yjxS4+6iYcJ392xZhUk6BdaEv35g1us/JnEDazC5fTkbogXrl tYKmB2FhB4fH1+n+zfwvHk9zB2WawE38DO9dCxbks3NfxMbEG+LB6+0kUJSxUNdOit aeopHDVSUNLSTOR03IOUAHT6f/xcOEIll3iOJ56Qj5Mz9r6WG0G0Mp0vZ/ZQVQnt5x EI6+kZmbkjg61xgf/onnEJyxRF22vTnzhMj7LuAfIFRbslv19xo9sty81J9sLVAmXx heXvN76DKe/KP48hKTHgNuem+L1+KcS+890S6x83pwblFH9tuVJ9GzKnp0LtXEjcKe Go3RvBr7k5sSg== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:22 +0200 Subject: [PATCH v4 04/15] rust: str: introduce `NullTerminatedFormatter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250812-rnull-up-v6-16-v4-4-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=2720; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=5vdh1riAR0ZL9xaLLQRBtVtO//nClfhfAq/IqeV6FBY=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8fKlpJ8jBjQG7Hiw5jhqLFg/iPJu/OAJjPr lI8UDjotlaJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/HwAKCRDhuBo+eShj dxdgD/kBvs3r8XCENbIp6EI7LHK7yDcd18oPIe4WjKcM17srqNA1W23fdXxzM6Ry0kpdjfD1R4v 3MPlNXPzD4ohFd2AM5ApFqhG8Ym28IH1UHRkJvdYYSnEN1jaV5yUzv+/AZC+BoV8GC9DyK9N+W2 CfBE/M33G4Rr8ZOQVwIu9fS5V7Aqq8M4L4DP4uappH90wGIvqAgQvmbdLW+LgsvWE4yxKuqwyxc E3iK+KFCo6lRUHcCKw64fG8yV3DwPPFk2mVI8WDKdky8sSkglJ8Nezk9ruTIXxi157oCuTwLt1N ejTAgTZIZz1dGp9Nxclb4JI3JFUKex/1BS/SWNEvAfLa1PnlIB4n+ukE8rL3WfcDm5DOt+2K+gi MCdhiMGzctEnC9p/LcIuz/9JaZyib8nIF0rH857tyMa9NZRPHtkAHCEmexTg3AY2WhnEv46s24Z sQiJ8hteUPu9xINr1BTe4MTEoDtcMiutlkGkiBxvv/wAxBTpQyintIIoLVz2NIQAa7QQEKPcCTO VqUz/kJGMlaqAhFA/MPerlK90N2UkGRGhFWEAu2MGhvr0Gf6vswAKDoKsLDkjrFncVpixFgFp0c p4qS5xFck4keRtbgMqV+VdGHtsjbvW6RMVt/F+LEvrfWZvlN8vMdjtq6udljN21VrDqsHg9qPnQ tBJqlRnf49ysphg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add `NullTerminatedFormatter`, a formatter that writes a null terminated string to an array or slice buffer. Because this type needs to manage the trailing null marker, the existing formatters cannot be used to implement this type. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- rust/kernel/str.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 55 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 46cdc85dad63..e84f62c01815 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -871,6 +871,61 @@ fn write_str(&mut self, s: &str) -> fmt::Result { } } =20 +/// A mutable reference to a byte buffer where a string can be written int= o. +/// +/// The buffer will be automatically null terminated after the last writte= n character. +/// +/// # Invariants +/// +/// * The first byte of `buffer` is always zero. +/// * The length of `buffer` is at least 1. +pub(crate) struct NullTerminatedFormatter<'a> { + buffer: &'a mut [u8], +} + +impl<'a> NullTerminatedFormatter<'a> { + /// Create a new [`Self`] instance. + pub(crate) fn new(buffer: &'a mut [u8]) -> Option> { + *(buffer.first_mut()?) =3D 0; + + // INVARIANT: + // - We wrote zero to the first byte above. + // - If buffer was not at least length 1, `buffer.first_mut()` wo= uld return None. + Some(Self { buffer }) + } + + #[expect(dead_code)] + pub(crate) fn from_array( + buffer: &'a mut [crate::ffi::c_char; N], + ) -> Option> { + Self::new(buffer) + } +} + +impl Write for NullTerminatedFormatter<'_> { + fn write_str(&mut self, s: &str) -> fmt::Result { + let bytes =3D s.as_bytes(); + let len =3D bytes.len(); + + // We want space for a zero. By type invariant, buffer length is a= lways at least 1, so no + // underflow. + if len > self.buffer.len() - 1 { + return Err(fmt::Error); + } + + let buffer =3D core::mem::take(&mut self.buffer); + // We break the zero start invariant for a short while. + buffer[..len].copy_from_slice(bytes); + // INVARIANT: We checked above that buffer will have size at least= 1 after this assignment. + self.buffer =3D &mut buffer[len..]; + + // INVARIANT: We write zero to the first byte of the buffer. + self.buffer[0] =3D 0; + + Ok(()) + } +} + /// An owned string that is guaranteed to have exactly one `NUL` byte, whi= ch is at the end. /// /// Used for interoperability with kernel APIs that take C strings. --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 654CF2E765B; Tue, 12 Aug 2025 08:46:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988364; cv=none; b=lA6Xtxk18QTBn771IGsVAO2SVbIycUXwUOg511w/Z9mK6P/gtumqWg9YdyxxP/yA9GilwK8fy380G9GGfoCH7rYTRtA6r7svF2xHIYy4NZ678OUkwzipTZPUAQ7Vmm944sIUDEd3FMQFfaP7ekgNyUGFX+WFPz1VryvWDpezMRE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988364; c=relaxed/simple; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=OzFlD2ThOFk9koW4TlNQjjwXOLpZmNv7Mix4kC3kCh1vF8p2wdZavGm9g5OLg+YZXetdhc9g7TbYK6sJJDmrS+frTmvGXUzpdSPjkMYTtFUNYdEiphNyE1MkDP9cqL90RSeG6sf7ykHOOgbZz802uwQ4kz75bBNxBk1JkaMKoNM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SuGlXe6s; 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="SuGlXe6s" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 43372C4CEF0; Tue, 12 Aug 2025 08:46:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988364; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SuGlXe6sCLIu3okn7PYaN4bDyJUnQZw0gpk0HR+3CITWXJeZ/Q4pDsi1XHUIEaZab x7zVtLrOH5e38qgUqMWXdibYT6QvbtxbT9YyAyMbORD1uteRB2pKNbyGUEnuQ5Qfzl EeXIpeG0PXpxNOAe596Rk/hldYcQ8bCWw7pN3NyimIKkGPtT7IIvujBW/Sr5htP3nl b09qpJGiYIClfoyOjJW0zEVBCyh5ZT9yBV7sEPLEvnMIF0JLTDXGuxpOuN0h3oi5fI cT6NK/MXWbZd31uzdLbZP+g54580jJPQXp2DV48sZz38u/Nl+4BI2/crslsLhrlm3C lCuD8wxG8OCTA== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:23 +0200 Subject: [PATCH v4 05/15] 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: <20250812-rnull-up-v6-16-v4-5-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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 , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1184; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8gbDFSIyS+dJdKjQepL2c0T78gls0br2zuD 5W+XncDEyeJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/IAAKCRDhuBo+eShj d4UPEACt4fVFdPJtSaZGxLfUpgJvz6zQ903Fb5lTRp7ufbkONXm+N4qMG8JIbAc2Veo2PgmOpWC xTtu7+mzlp05XnmP3jeM/eMf9G0GFGPlZHYhzMFM6t08YWjGrqXanfRHdtdYLbpBhP4MIzleQG5 A5Z1061hMu3GydadqHG4g8o2H/lMRenVPYMfT7kfWrkZ7t5ZaSXLGuUdkzRYkLZC8tzNz1SsD5T IRe7eQ1yd6z/9wLa/sMXJoV7fymjfCmTExNGPWcQxJ6vYOHwfrOzgwn8ee5uO4M0rH/S9kNzOJI 2Le4oRgk7HAz/I8Eu8NOl+7uov44QSH9U7qoGzfIYjHPTeZ2qrnWkwJspr9JTcH1yEHZBpRbVe2 XQ9OXIrHk44fkVhqdM1DivdAbTv7/2pybXCGMnXUD3OWVFazXd4NjY/X+SVtXTh9rjT9gNFc8PU fv+CgdJiECC9p3C4SntbfwXtClPoo1fnwV3Ww184UHe+zFsvdmG7/VxP/+Cf41c959nPDJ7bQFn Ze8b9+7k14fhY09gh4Cyr9GZC0LcEvSCZ0ogCXuENWM0SVc9PokoivuKEG+kFzwqXRjlCfSZe+Z yNPw9583CrLpVdjqKBfGQeSG44i4IuKNFXW8Ggm27Yy3nyrkoPgtV5eJAMzN1Kl7kaF4wLWE8js KjfrAUKobr1uScA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Clean up the import statements in `gen_disk.rs` to make the code easier to maintain. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/gen_disk.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index cd54cd64ea88..679ee1bb2195 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -5,9 +5,13 @@ //! C header: [`include/linux/blkdev.h`](srctree/include/linux/blkdev.h) //! C header: [`include/linux/blk_mq.h`](srctree/include/linux/blk_mq.h) =20 -use crate::block::mq::{raw_writer::RawWriter, Operations, TagSet}; -use crate::{bindings, error::from_err_ptr, error::Result, sync::Arc}; -use crate::{error, static_lock_class}; +use crate::{ + bindings, + block::mq::{raw_writer::RawWriter, Operations, TagSet}, + error::{self, from_err_ptr, Result}, + static_lock_class, + sync::Arc, +}; use core::fmt::{self, Write}; =20 /// A builder for [`GenDisk`]. --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 86E712E6125; Tue, 12 Aug 2025 08:46:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988371; cv=none; b=ANN5+Kt5d+597BhxG2M8AWU/SCj2aezmY43+n3PRde2ez8LooOrPZR+QOJ22+JJdc7JbisMhq3nJeI3eiUQBk/UAUPLYeVTaqwYTqiwZNFO6kefWPK3Y7CEJuyW52e9vNTkwIFOHJMhvOCKgvlSbMuEqaKDONqXDpOffexwZPJ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988371; c=relaxed/simple; bh=Q1K4pcq9Ex/JoCw6ncPnJkkByzSKTsmoTNB/gxpnoZ4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=e+BP1I0DBzlewFF+55cJGVyoMxTdGjXXvrLTH5elMIzGumdOmUbHa64Nyp1A+Q6n2LbK95S0uSbB9m1OJRZEdjxTNZ+7j/Q+97Oo5TJgxp5u5Li407gGgy+13atmPa7qJpMYp2b7JeMgV7wcz21zHLRebmMgLGvlnNPwhl2uDkM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OC8J10j/; 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="OC8J10j/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D8FCC4CEF0; Tue, 12 Aug 2025 08:46:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988371; bh=Q1K4pcq9Ex/JoCw6ncPnJkkByzSKTsmoTNB/gxpnoZ4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=OC8J10j/7DzNIU1pYSQ0U9/QEzWXAArkR6UciZTIyslySk9whyoL9QPQ7LRmJqE3Z hotu+0NdB/I1ecBY9X1SZU9WveDWxQrC9+BDYBEGtWToZ1hvitHVz+bNgG9iNAKz8g U/IJhpGzx1ywrEQh6JEUvzbJj/sClFdtFNTnA7PIEdKS31yjdAd1BTxpYONXR036Ba 2810VwEFRiL9PmpfJpvzHgzQbbqfzzx5rL8+vA/uXaGz387BgjGYzJAxY1xh3Y7cNd 3nfHty/UbQfaKEoJnQdG5OF6u27dKUrxzlpe2HeXeKU4vDyPr6zG/WsM7iVhuOy1xG jXqI+Pp7R9alg== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:24 +0200 Subject: [PATCH v4 06/15] rust: block: use `NullTerminatedFormatter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250812-rnull-up-v6-16-v4-6-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=3054; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Q1K4pcq9Ex/JoCw6ncPnJkkByzSKTsmoTNB/gxpnoZ4=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8hmvXK3//0xXc0H/bbvDx/v5E2gnXFrpfMq aJZVvGgOzGJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/IQAKCRDhuBo+eShj d6JfEAC6gBRwonGCHhfmqzFwU4IYTGIJgwnC9Z83VSxrfZrezMqI1In+X21PgqcULrt/00sEfbr kSH/QFNzfOtByQRBXWRgwxL9FYMAn7wt9XVZ15fpR37otIhtHrpbXEjvbuhFILM1Z1WufrN0yyA 0LkOzd343AoMFgdL+IgiSRbn3TJHJG9euivQ/ULsPxn820P5g8MxNWcZ6MT/nx8JoBTLt4Wc89Y dRIt1GjG5Mj5qUvRWEKw+KKwBAlsxiSMohPYXiw7Z1CkOp1jYWhDwlASjgjnfQrBPHDeIye+RIk w/FfMnnwmcjSSv3ygMxzRTMILGPyobbIIaZNI2jBIviA53AxlQ3CPXdz2zOYMzBLzMH0OiiPy64 CJm5osMlko+/3YzbaAEWqwvfZ+N2Ty34B4eWIbjsUbxe7gNyouCbHKPvENkIzQwN/z0mpnMyX9q yIoXYEOjkJ9wnHu2FAEkNRzP9C8lmYoLeolBA5jT//07q9uH8sOla6ryOBtyIsb914AtXtHcZKO JUETQg3Yji3jhr0Heab5/GHN3jWo7/8Cwp98PdzbQI0ZgXCpttxG+XC7vchlkphh2912GvMAiCy NMPQ9gBaaOUigNtNeAwe+O4+B5wDm7jSaL5uI9VVO1s/CGVkYyqAnT4geqasj0C/JcRNccaMPLr 0A2zBDIaEjhiDBQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Use the new `NullTerminatedFormatter` to write the name of a `GenDisk` to the name buffer. This new formatter automatically adds a trailing null marker after the written characters, so we don't need to append that at the call site any longer. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- rust/kernel/block/mq/gen_disk.rs | 12 +++++++----- rust/kernel/block/mq/raw_writer.rs | 1 + rust/kernel/str.rs | 7 ------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 679ee1bb2195..20f1d46c774d 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -7,9 +7,11 @@ =20 use crate::{ bindings, - block::mq::{raw_writer::RawWriter, Operations, TagSet}, + block::mq::{Operations, TagSet}, error::{self, from_err_ptr, Result}, + prelude::*, static_lock_class, + str::NullTerminatedFormatter, sync::Arc, }; use core::fmt::{self, Write}; @@ -143,14 +145,14 @@ pub fn build( // SAFETY: `gendisk` is a valid pointer as we initialized it above unsafe { (*gendisk).fops =3D &TABLE }; =20 - let mut raw_writer =3D RawWriter::from_array( + let mut writer =3D NullTerminatedFormatter::new( // SAFETY: `gendisk` points to a valid and initialized instanc= e. We // have exclusive access, since the disk is not added to the V= FS // yet. unsafe { &mut (*gendisk).disk_name }, - )?; - raw_writer.write_fmt(name)?; - raw_writer.write_char('\0')?; + ) + .ok_or(EINVAL)?; + writer.write_fmt(name)?; =20 // SAFETY: `gendisk` points to a valid and initialized instance of // `struct gendisk`. `set_capacity` takes a lock to synchronize th= is diff --git a/rust/kernel/block/mq/raw_writer.rs b/rust/kernel/block/mq/raw_= writer.rs index 7e2159e4f6a6..0aef55703e71 100644 --- a/rust/kernel/block/mq/raw_writer.rs +++ b/rust/kernel/block/mq/raw_writer.rs @@ -24,6 +24,7 @@ fn new(buffer: &'a mut [u8]) -> Result> { Ok(Self { buffer, pos: 0 }) } =20 + #[expect(dead_code)] pub(crate) fn from_array( a: &'a mut [crate::ffi::c_char; N], ) -> Result> { diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index e84f62c01815..b8eb50afdb24 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -893,13 +893,6 @@ pub(crate) fn new(buffer: &'a mut [u8]) -> Option> { // - If buffer was not at least length 1, `buffer.first_mut()` wo= uld return None. Some(Self { buffer }) } - - #[expect(dead_code)] - pub(crate) fn from_array( - buffer: &'a mut [crate::ffi::c_char; N], - ) -> Option> { - Self::new(buffer) - } } =20 impl Write for NullTerminatedFormatter<'_> { --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 69C711F37D3; Tue, 12 Aug 2025 08:45:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988343; cv=none; b=mbFH5xCS8Q3mH+J4DthA9ZCPMCRSpikDJzvQT7jx7JGGOjH+rk022URoqKt+4R7QBQXCYE6CGerGKXZSzrgHUGMB3nkxwR4ByQLfplnU5PTdAn/R3MEtc4UDXPCJIpci0+aFD0Oa7yBzktwIZ7/mhYz7vigk05abMWfEK4VnofA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988343; c=relaxed/simple; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fptkrUorGs6nxICQewD1niW/OULKv02vDjT8pnTxlE+DTZh42xUvD0/Z08bbcGkw3MeJSvwXhhLRtZgMEwrCbTW7jz38DGE1yt1ypixKE4P0OZ16peqaWGS8lbuzjy21Hu6n3X8fOD4p0Txm1tVb5KOqzYfY8jrTr4czQoFAqe0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oKFo4FVZ; 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="oKFo4FVZ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0272CC4CEF0; Tue, 12 Aug 2025 08:45:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988342; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oKFo4FVZm43yYmuz3eVUVd/hsTA/yidp1enNvBSPO3fpYKBEUlWrEbdUvFVeuAnwd 7a2sRAJzEF7Gt+SRpuRbAKDE7vaTW/aQPP7Qv03XKOnjXnI9G5q4OlclWjQDV/Z+KR Be4SxiiJYYhiReKNs08d8Ymaap6YpnlZGr6aItA8+/MOpL+zFJvIAi2D6oH/q6jizx WPlqRIVeUFaL7KVp7S0KHXAIcI1g3aHJvRXRL46Boi7Wrk8hYZuDRi6vfG3MIEL/YA H3s44Hs5Vmhs8NnA8y+F+JaOzkON64yKeZNQ1XUs0myGYXNpt4OCDck8g04I66Hihy /FX1CXJd7Kscw== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:25 +0200 Subject: [PATCH v4 07/15] 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: <20250812-rnull-up-v6-16-v4-7-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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 , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2507; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8iIkoNkqQUOnDywI9o6nQQFQELgIpckIOPV FPvKRB0To+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/IgAKCRDhuBo+eShj d66UD/9FI7DxPt6eeUbAybLCy13Y/KUouBapa3d2tfUklAlEDtK4XIum0BrtyGWbUvTwpzcofpi tcwaBvFH3+K26D1sTDgykvMdHdHUGIXBxWK3va381Wdlx9QAKJ2frWCYQxV6QXBg5dkT2ORgEZ/ J+oMiq53O82eIBmxNHMxs4ioHFRcU7kdSl1GIPXtbmtAlkS2+r8sxutQCbVpQWlZHaFQ9n8boen HO6Ibs2LBIKmNgXWG8jGWa09mVaS474zOHpuXei0TRDUoSKKL0OUQXNjujx2D5577W1JEQ91rko N+MLnkgMKB69HbQC2InW96KRDGKhRZizU35bPmbsVRMoU4TqZoSA3xM8qYAjYCuIPRoKZ+cgfJH dW+Aal+djZxYReAIzig1APbxKfmuc2gRHZnu4ZTFcuiHMucbRq75ZM3GmpA8G7cFbiCTbb4FrpD BwIYOY+1mnIxRcsiq4KcfkmqJVWQ7Zdz0qTzI2urCVhm1L62C6t+zKXe1kQTmEaa1licilECFJa NBTvq2fjinwrbY/81wgxiHnPD8HgDZ+EzXV4dZhgt3K+O/kWMqPAhM1TVjGVdbIF3VQcWCRFNP1 /tNDkIVyHryZrYt/8ievZ2AAdBPx5DrurwnaLVId58xGjcFDahfKlt26b0iz2QCu7ZhJN79dLVD QHmBExR+eepfpwg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 `RawWriter` is now dead code, so remove it. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq.rs | 1 - rust/kernel/block/mq/raw_writer.rs | 56 ----------------------------------= ---- 2 files changed, 57 deletions(-) diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 831445d37181..98fa0d6bc8f7 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -89,7 +89,6 @@ =20 pub mod gen_disk; mod operations; -mod raw_writer; mod request; mod tag_set; =20 diff --git a/rust/kernel/block/mq/raw_writer.rs b/rust/kernel/block/mq/raw_= writer.rs deleted file mode 100644 index 0aef55703e71..000000000000 --- a/rust/kernel/block/mq/raw_writer.rs +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -use core::fmt::{self, Write}; - -use crate::error::Result; -use crate::prelude::EINVAL; - -/// A mutable reference to a byte buffer where a string can be written int= o. -/// -/// # Invariants -/// -/// `buffer` is always null terminated. -pub(crate) struct RawWriter<'a> { - buffer: &'a mut [u8], - pos: usize, -} - -impl<'a> RawWriter<'a> { - /// Create a new `RawWriter` instance. - fn new(buffer: &'a mut [u8]) -> Result> { - *(buffer.last_mut().ok_or(EINVAL)?) =3D 0; - - // INVARIANT: We null terminated the buffer above. - Ok(Self { buffer, pos: 0 }) - } - - #[expect(dead_code)] - pub(crate) fn from_array( - a: &'a mut [crate::ffi::c_char; N], - ) -> Result> { - Self::new( - // SAFETY: the buffer of `a` is valid for read and write as `u= 8` for - // at least `N` bytes. - unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::= (), N) }, - ) - } -} - -impl Write for RawWriter<'_> { - fn write_str(&mut self, s: &str) -> fmt::Result { - let bytes =3D s.as_bytes(); - let len =3D bytes.len(); - - // We do not want to overwrite our null terminator - if self.pos + len > self.buffer.len() - 1 { - return Err(fmt::Error); - } - - // INVARIANT: We are not overwriting the last byte - self.buffer[self.pos..self.pos + len].copy_from_slice(bytes); - - self.pos +=3D len; - - Ok(()) - } -} --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 42AAE2E2F00; Tue, 12 Aug 2025 08:45:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988354; cv=none; b=godBE3j/cnI1gDqZtTsUqE0WXb96vJb5ul/+kwLWT/oEFQECc0qN8xcvRbKppFfAOGNAtZdlDi+di5fGAXpl8JXW3pCq97A8Yr1OniV6UdxK6KOMEKWmslz9GMYHOV3XtLAptwlcPhVkAcsKEJqCPHMAOSrFI8Ih0Dvkksm78Y0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988354; c=relaxed/simple; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Xk0D/oiQ/AWuckeOhkzznrb7rysisnKfE/tLUwN6oI+xJPhHG78R4Ox1X7e5idgI/krjSSOBuSBn+TT/s8zcmR7mouRSzX4GuhiC4Lbi03xEZaSmg5zczczEYplTH+IDxXBiBXZEDtQMvRJzek/t1ryj/GYsHQwnDpxhe9Awpgg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hziF534x; 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="hziF534x" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BB5F3C4CEF0; Tue, 12 Aug 2025 08:45:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988353; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hziF534xKIukfDVBUdhLEz8L1eWhWqEWvxSk/gqPR5farO2zONUtbp0j18yPz6EcR Co3lp85+TiMsc+/RookxNKrmTCeC5pxd3oY+2nvuRDcj85xGH9s9FfKkM3EFkv3xPS 9yvlwr8rrKAVWCptnCN7C7oVcVpBQNRRc8qYeAAsMKDAXcp4x0+qF1mL6YidYztYFP jPukN2DJK5g9gmpVOUO7NIIUl8JrEPOG+Da2tnj61SLB4IN+/7HcmVt7a9aX6DSkal +Rstqk4tf6eTDqQ6r6DsKe1n4OyOdk97seL4oksD54venthYpl7dq4sw9h1R5btCVU G/QgrvixhmlHA== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:26 +0200 Subject: [PATCH v4 08/15] 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: <20250812-rnull-up-v6-16-v4-8-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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 , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=963; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8jaZeO3dGdonKT2i5C2dorwO/9Ab5rVdZhZ GBmcUjGHDuJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/IwAKCRDhuBo+eShj dxY1D/9B3vYO2SWWAL7+K33cg7E0I6P3WY9aXa/oJp9smSuJnjclyPLyLb3Bxv398m1C4irON19 YYnAvHETMmI1RTlLbczokubEIlFfnxz4DLBnLbCVhemXgfkdmqOwQZXW0REr/J9iJkouRcWDLVu fAguXT23HDNeJs0bM7uLkzMrLVniLYH2PjqMZrQi3+IV3U9ImwoBxuwEm81VY0Ly8S4qONZXdtl gnfYDc0TbpcNH9xBxvp0tDVe7PJWdDApcmC8/A3UssthuryCKDkrr/fZ649hPerRubZ3bx84JT+ uqxwaUUL/B+7iqpq8zqP2iDFg+H4KJFO61LzJ18icMmsIi4ozUbCFiJWnZxWGERRSDzGM+AtMUT D2vehgPtBuyU4bemn4v5EmmwYeahklonivMd/3FRJFuLGKbeEUkrO+psMC1QDvrM5eHfA2AfLYr uzUYDAhSy2Viw3B86DMt+6g5nAPxi6fOZ/O3hrvqkg5pzLH6ILC0Z8jUGRVKKx/FZ9Yki2JkPBE zGSwydIXDQPNdEQ2BBcxeKNPzUAHpAoH5/u1kZ7oMLx76iPToviLLvLWNnuBiJyWnrHzvEXCR0u wl3GQY8G03x0FLw/QnDaOwxeyiurHaD1Kf3S4VygL74nuPQCS397vcyFALGRp/ia0Pi/w9RMHT8 pjQ5hL5tgtob8ZQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Remove the trait bound `T:Operations` from `mq::Request`. The bound is not required, so remove it to reduce complexity. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index fefd394f064a..f723d74091c1 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -53,7 +53,7 @@ /// [`struct request`]: srctree/include/linux/blk-mq.h /// #[repr(transparent)] -pub struct Request(Opaque, PhantomData); +pub struct Request(Opaque, PhantomData); =20 impl Request { /// Create an [`ARef`] from a [`struct request`] pointer. --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 CFA272EA159; Tue, 12 Aug 2025 08:46:30 +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=1754988390; cv=none; b=qmNBgJl08t2SsRgUUoIeCbv8Ned56it29HH5AUA+merRp7tFLjBEESpmNZQWetrHhSaz2zm/1KsPdL+5fzq4nswDKqddpQ/5dWuYUsKmF+7iQxoAn58VAujNb23ICAnqEY9TMmFYFHHT88hfaqxS2JFppdROMXON0/WB1ckw3QA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988390; c=relaxed/simple; bh=dNkL1AXVwuljdfihZmuwo7IT4fyYZ7hZ0xzOqsCodNw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=h44NeslVWFuEYGG1+z4z5c4tJIEj3gHuPGXfJtOlzf9/gzwsQzQB5VINoWRbvpd/cJdR6jMEfPg6i9Ly7QQK0KX99SoLzIT+0OIZQMfLBJUZi0JjAhVEP4nosBfnbG76r1ke5s41iXOxQLYY8uciarYSX4St4LMJslroBbqKneM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=ej1iRFju; 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="ej1iRFju" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4D039C4CEF0; Tue, 12 Aug 2025 08:46:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988390; bh=dNkL1AXVwuljdfihZmuwo7IT4fyYZ7hZ0xzOqsCodNw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ej1iRFjuusjXG1jfGwSNW/I+toJIZc53wAFDC2Xx1g+YKF4wObQOsPkO9OXH7My+u CaXpNpxy8+DVLbDtK/+8Xp6dTx7BJ6Dvtb8lZFvb7L2tRRUdx002RtCaGU+EA0mMoB GJ8w/SE3ln49+MlbDJq5rz33ldpKbqrKQDnnq8g+NcnfvlKv2VqnqVdYg6nesdv0IR E6MEmOGW7kCHlXyrAyWF5JhXCbc6lEiN5I0cB3ckeC0Z6DQBBO8EdSl54INCBmmrOD 7kVBLI4HNXEq6CfbGi32M9W/gXEJxKih9oR5u/Q88I8I/3KzcuIa/bEMU3nFSh6vSb Q9H42K6GRijQw== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:27 +0200 Subject: [PATCH v4 09/15] 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: <20250812-rnull-up-v6-16-v4-9-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=1002; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=dNkL1AXVwuljdfihZmuwo7IT4fyYZ7hZ0xzOqsCodNw=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8jjBkVYLCEE3H1oiwNjZEP6Wcm1NqusNCgt mt451E3+aWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/IwAKCRDhuBo+eShj d22LEADHinWAGFXhRHNPUgtQHg5EnNSr5zZQd/Of85wEJ021ljCE3ldcEFIx0JtLKFrY8qXmDrE JJkpBWTQdg1tujK8HK35fVqBFJIK0JU0h14r1t9T9b3kNBTNFMOYtsoWNV6HuJcghog8OjGNv2/ 6VgWSzv1wGHSrH/GwJ0HmgFc/xNQnUBJGSzFwOUk5Xh4EcMUElFG/EC/0ycFiJg0BO7wpag/GJW XgcPIC7Qy9QnWe0CMVgtVBSkL7slMl+XkD1MdoWomKQW/uga4NPpuNmLcL2L58Ct1b/i2ItPhIl 4DGcE0AU1o1cxsTiU71cheDrAVw/hcCiMYcH0MsoQoBzBdGmXxqdpPd3kmpLcX2qRRLR9OveMeU gKkiMRCbDJ34LgjfHO7+niTh032f1O+E8I+nZllaWmXJmWhnKDlLUNZ5NDrHLgnI+lkMJJtf8mN JbB7Vakg36EV47/z9T0ZmmdLny70e0UDiErxsD3IUdIQhaDrQbdAXeXl4VX1fT+sGr6nmyQWeci DpryroIHBeojSaDimO6dgv0CAQXvSViXqovQ5bm7Pi10ZpvvK2nP5cGzyEF/yoZeHZuSVlwCNos wsQ8SHwOGoA3FhctkgqXwwjkGaTrxKI2EJpYYQguAFa2M0mRDsvmVPPr5JCITdFHyQOfXbDGj00 xYb6bwYhn8i3Zqw== 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 Reviewed-by: Alice Ryhl --- rust/kernel/block.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rust/kernel/block.rs b/rust/kernel/block.rs index 150f710efe5b..32c8d865afb6 100644 --- a/rust/kernel/block.rs +++ b/rust/kernel/block.rs @@ -3,3 +3,16 @@ //! Types for working with the block layer. =20 pub mod mq; + +/// Bit mask for masking out [`SECTOR_SIZE`]. +pub const SECTOR_MASK: u32 =3D bindings::SECTOR_MASK; + +/// Sectors are size `1 << SECTOR_SHIFT`. +pub const SECTOR_SHIFT: u32 =3D bindings::SECTOR_SHIFT; + +/// Size of a sector. +pub const SECTOR_SIZE: u32 =3D bindings::SECTOR_SIZE; + +/// The difference between the size of a page and the size of a sector, +/// expressed as a power of two. +pub const PAGE_SECTORS_SHIFT: u32 =3D bindings::PAGE_SECTORS_SHIFT; --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 33DAA2E6107; Tue, 12 Aug 2025 08:46:07 +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=1754988368; cv=none; b=akfsq6NreJQDakHK/XpFuqs4agymFCvQrpsqGrGQiO2mZCDmn4cvQ6J89rFYRSRc3fwnmyvTA8CGWOVqloqWFsYFgwDBsPOy2/CljRGBp7p8dnD2sohqbyF6N4UHLpMFWU7xB/GCp9avlY/kCLMCrMUqT3AJ1qKps96qWRCWNuE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988368; c=relaxed/simple; bh=tDTjZd+5UIJEcvpFCoWeOEwqbYXX//kOhz1DDrET25c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=a6WmUEZKzHVdiWcElUfun5D+bVjFEyfsHB6JwuU4Ng1FQifkHpW3y8bscy+V1/o5/8G5P8sxKIkyjNwc/AWN7T3kqWEAtyKyqsOARVAE7JZeCs1S7ZTp5an/Ks9+jpguTVAlkjWTZwJ/MKxbDGNgp/5uFkv0sc88YWx/ujel1+E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E2/ZV494; 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="E2/ZV494" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DE835C4CEF0; Tue, 12 Aug 2025 08:46:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988367; bh=tDTjZd+5UIJEcvpFCoWeOEwqbYXX//kOhz1DDrET25c=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=E2/ZV494mudYHlpgVWjNxKISJG5UKWz2Awf8/cYPMJ6qjumpq4TmPuAppcOGdAjZ1 1zTQJEg3pfpZb+xmfBk7/9jT67HAvCk+iWF2uXnm4oQFpN57Scn9uEtB7HdR1xcfKH RsTEEILMY5QQMzuTFzhEjqo9wCN5EE771VLkmFzMCK9YS94a/2P1w5AOM5fVfPt3nn a5M8MpCwaJhUkM5ISAAosMTXQfwREPzymhcOwSo87gFquUZ8FAbJG9kl6vP1Q2aUdc VOOXaQxgg/8TQp0gt63t7DM5edl0LjbVAOMSfEJZ3dlktwKyf/Mh4FK5uTbIofcBol KxhwQenE02C7Q== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:28 +0200 Subject: [PATCH v4 10/15] 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: <20250812-rnull-up-v6-16-v4-10-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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 , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3780; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=tDTjZd+5UIJEcvpFCoWeOEwqbYXX//kOhz1DDrET25c=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8kZJmuBPEmYZwNzs7rTnhXXedH/LLv8DpHm eoutnNRhUOJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/JAAKCRDhuBo+eShj d976EADFMv1LwdnrO6N+MsaGRv7g04OnsuBx3fr7YvQlmiZXMJGqcaaLlmwUFgQPniZoMxdZS3X RF6Dws9/nQLErDnWiL4CJ//u9xQbhLwyCvvudPDVBqN1VN7lZVZd8Ga0OQVxVHF0Mfxi5VPAI3g 6mPfRsmjyIdw89URV13hAeqSsV/OaZEgdqwaQWqGYE86n4eQ/4P+as3Y0nb0oYPzizbLmZRg6n2 NnCRjJHtHFPpO/XqDC9gVzP1OfN/hI9sn/kFkxhxhVmw300qt4aPUAnz4wM3nOYpzEGLQoiuvij WpQGt7BE83wcrrrquXjmsFBzetbreIrhI2IK3Q0SJBR4ycHDV46UIDqJXgTxuY/9kgBOO0c7AAd vTRefLLgNHf93tIoSFAkxeNd2y9RoPTw7jmFi96i1n3nHISk/dcY549cT47ip7BRfyzy3AUZ9OI qQE5Wks4WDDb8wV8iEI3GIZxtaV61Qgi31uayuV4/40t0CRTr8Q59z0OCe1iX2EwWfY7u0Fm7Lt x0VpVTlLsN5vjAhW3ZWaJstL4QJyc0CX9EFxdtkiCh14tpcJs4jYyklEqWy60n1xGhGMeObDBMW zaIWVV+oTbGjNiu1Vio3oLxaQT26CufZl5Hd6zxMuRptKtM5usmaIl8BL3WexA68NUte/Th+qSO m3nDW6BgFoZg+7w== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 The rust null block driver is about to gain some additional modules. Rather than pollute the current directory, move the driver to a subdirectory. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- MAINTAINERS | 2 +- drivers/block/Kconfig | 10 +--------- drivers/block/Makefile | 4 +--- drivers/block/rnull/Kconfig | 13 +++++++++++++ drivers/block/rnull/Makefile | 3 +++ drivers/block/{ =3D> rnull}/rnull.rs | 0 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index fe168477caa4..238ff10f537d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4343,7 +4343,7 @@ W: https://rust-for-linux.com B: https://github.com/Rust-for-Linux/linux/issues C: https://rust-for-linux.zulipchat.com/#narrow/stream/Block T: git https://github.com/Rust-for-Linux/linux.git rust-block-next -F: drivers/block/rnull.rs +F: drivers/block/rnull/ F: rust/kernel/block.rs F: rust/kernel/block/ =20 diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig index df38fb364904..77d694448990 100644 --- a/drivers/block/Kconfig +++ b/drivers/block/Kconfig @@ -17,6 +17,7 @@ menuconfig BLK_DEV if BLK_DEV =20 source "drivers/block/null_blk/Kconfig" +source "drivers/block/rnull/Kconfig" =20 config BLK_DEV_FD tristate "Normal floppy disk support" @@ -311,15 +312,6 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based VMMs (like KVM or Xen). Say Y or M. =20 -config BLK_DEV_RUST_NULL - tristate "Rust null block driver (Experimental)" - depends on RUST - help - This is the Rust implementation of the null block driver. For now it - is only a minimal stub. - - If unsure, say N. - config BLK_DEV_RBD tristate "Rados block device (RBD)" depends on INET && BLOCK diff --git a/drivers/block/Makefile b/drivers/block/Makefile index a695ce74ef22..2d8096eb8cdf 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -9,9 +9,6 @@ # needed for trace events ccflags-y +=3D -I$(src) =20 -obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull_mod.o -rnull_mod-y :=3D rnull.o - obj-$(CONFIG_MAC_FLOPPY) +=3D swim3.o obj-$(CONFIG_BLK_DEV_SWIM) +=3D swim_mod.o obj-$(CONFIG_BLK_DEV_FD) +=3D floppy.o @@ -38,6 +35,7 @@ obj-$(CONFIG_ZRAM) +=3D zram/ obj-$(CONFIG_BLK_DEV_RNBD) +=3D rnbd/ =20 obj-$(CONFIG_BLK_DEV_NULL_BLK) +=3D null_blk/ +obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull/ =20 obj-$(CONFIG_BLK_DEV_UBLK) +=3D ublk_drv.o obj-$(CONFIG_BLK_DEV_ZONED_LOOP) +=3D zloop.o diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig new file mode 100644 index 000000000000..6dc5aff96bf4 --- /dev/null +++ b/drivers/block/rnull/Kconfig @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Rust null block device driver configuration + +config BLK_DEV_RUST_NULL + tristate "Rust null block driver (Experimental)" + depends on RUST + help + This is the Rust implementation of the null block driver. Like + the C version, the driver allows the user to create virutal block + devices that can be configured via various configuration options. + + If unsure, say N. diff --git a/drivers/block/rnull/Makefile b/drivers/block/rnull/Makefile new file mode 100644 index 000000000000..11cfa5e615dc --- /dev/null +++ b/drivers/block/rnull/Makefile @@ -0,0 +1,3 @@ + +obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull_mod.o +rnull_mod-y :=3D rnull.o diff --git a/drivers/block/rnull.rs b/drivers/block/rnull/rnull.rs similarity index 100% rename from drivers/block/rnull.rs rename to drivers/block/rnull/rnull.rs --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 1BC7E2E972D; Tue, 12 Aug 2025 08:46:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988378; cv=none; b=RPXqpLbzYuoKUhYmyaUUiDqocErsPs1mtOsTVL2ZBBS5jyhd08SD+5cEEkD7B0Q0YJfGVtZqGKYj6QeEXP74U2TnZfv0HNGhHNQQ5b/dDbzyLecVBqZfFV8mrhUNiZkBKLsH4qcYakeKlltY2d3oW1AmPNL+yFP4MBDSz0Ve258= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988378; c=relaxed/simple; bh=flRlDhE3XAzugM1vKE+JBErvKHVu87t30+xem0Jh3xo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fIDCGjU9fwfgQ2hbJl9Bf7v4yErYzVsSNpz+MrWsLrw1RgxrU7ARySn9fax5hiS35TNOWnUyBnoTPeKA07UYKA3qRC08mbKF/ZempsBDhzU3zkBGS3VhIk4hd9RKBh3APZ62jA0icqgEW+JBXLPsRN3flR+byt4Fiw6yHASZFMg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CrN8LnBH; 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="CrN8LnBH" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 434B9C4CEF0; Tue, 12 Aug 2025 08:46:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988378; bh=flRlDhE3XAzugM1vKE+JBErvKHVu87t30+xem0Jh3xo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CrN8LnBHlkSzLow1hSYg4P4efNQY3Z960sEkHza+q+7o3ewJGxhwzbU2Syd7w+8R9 sK4fQMhDs+Bi9FH0ajzynMGuvm/OfoHDjWt9Umjp7GHNeJQtaWB/bKo0qE8WGdDsN2 sP9C1mi856yJiE3uTvV2FQyZip6WFk6n3umM/AXsUYgLwRsJnVT9ee1VHSZnnNVHWk w7AZVMoixkKSINr+ofiXHoHhwDgobuerbfOoLLiPm2DJbx9xe/2L3kkGAIlGTlJHG8 J2kRowcmOipUE8p7CkiFH0q2NUJUjRj0QJKJvmQkCJlommCgOIsZGHeGfOE35TZtnA 3iB8bq/ym53sw== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:29 +0200 Subject: [PATCH v4 11/15] 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: <20250812-rnull-up-v6-16-v4-11-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=10790; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=flRlDhE3XAzugM1vKE+JBErvKHVu87t30+xem0Jh3xo=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8lR83zjnMXRogKV0DZfL2Jl2g7lRYWIR1kx 5pethdelRSJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/JQAKCRDhuBo+eShj dwpLD/9IESeTlwvxp9yP+aWuY+2q1SqVQCHpo3ZIYXVLbcmWD8z1MDAxX4h8T6ij/NOr0VqtN9H piLPCiqW18WRkclQzpWRe/dSewFmqx7u5xishvA7om+kwVDj+EJH9eg7Ev6HSXaKZ/nK1LpHEMg 4l5mAYxYDuJQNynVAdGYgYhRLf5LzIRvg07szewQAQepX+F6cVLXban2vkZz3MDAxCnNw0iw6PY AQK2QmkKgWlMPj8phQCzSrisTMpm8LwU0ZtgTRSXcxsgd0i1UBHqf6cDwIDDRHQsbSguDvVI/vM yslU/IYJUqlZtd1HBub5a0RhvU/Jfh0V+pbJW4aHJJdiynIpFmOxaTZdWsk+zl9NS2BJRBE30B2 ARTUgB+3swNyeZxhbjkCHK++/lKh+R8F0Wl5JoKZYGsx+WQzVfkChB8i+ukNdfrod8SU94KJ9rs fVVkuHlZ+cPXRFDbazssZsbLEY9Uh+cOjHC/ajClegfj6ZCWLOKmgK0U3lrMUIhnSD4CZocx8gY Fqb99RhU4IGw4jiIM6Y8cDhmB80+9vESwSjYlQh1q90RrIb7fkFu8Vf/DIoenoBEA35O9Qgk21d 6FzGlmobt1rWnLjO/Q3MJpGyuzq9jhzpS96mesgK2CQoz7g/JD2Ej1rDp6tEEyae9MdD0QNCk+E e7ka7nm+uPqm8KA== 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 | 218 +++++++++++++++++++++++++++++++++++= ++++ drivers/block/rnull/rnull.rs | 58 ++++++----- rust/kernel/block/mq/gen_disk.rs | 2 +- 4 files changed, 251 insertions(+), 29 deletions(-) diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig index 6dc5aff96bf4..7bc5b376c128 100644 --- a/drivers/block/rnull/Kconfig +++ b/drivers/block/rnull/Kconfig @@ -4,7 +4,7 @@ =20 config BLK_DEV_RUST_NULL tristate "Rust null block driver (Experimental)" - depends on RUST + depends on RUST && CONFIGFS_FS help This is the Rust implementation of the null block driver. Like the C version, the driver allows the user to create virutal block diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs= .rs new file mode 100644 index 000000000000..8d469c046a39 --- /dev/null +++ b/drivers/block/rnull/configfs.rs @@ -0,0 +1,218 @@ +// 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::Formatter::new(page); + writer.write_str("blocksize,size,rotational\n")?; + Ok(writer.bytes_written()) + } +} + +#[vtable] +impl configfs::GroupOperations for Config { + type Child =3D DeviceConfig; + + fn make_group( + &self, + name: &CStr, + ) -> Result, Error>> { + let item_type =3D configfs_attrs! { + container: configfs::Group, + data: DeviceConfig, + attributes: [ + // Named for compatibility with C null_blk + power: 0, + blocksize: 1, + rotational: 2, + size: 3, + ], + }; + + Ok(configfs::Group::new( + name.try_into()?, + item_type, + // TODO: cannot coerce new_mutex!() to impl PinInit<_, Error>,= so put mutex inside + try_pin_init!( DeviceConfig { + data <- new_mutex!( DeviceConfigInner { + powered: false, + block_size: 4096, + rotational: false, + disk: None, + capacity_mib: 4096, + name: name.try_into()?, + }), + }), + )) + } +} + +#[pin_data] +pub(crate) struct DeviceConfig { + #[pin] + data: Mutex, +} + +#[pin_data] +struct DeviceConfigInner { + powered: bool, + name: CString, + block_size: u32, + rotational: bool, + capacity_mib: u64, + disk: Option>, +} + +#[vtable] +impl configfs::AttributeOperations<0> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + + if this.data.lock().powered { + writer.write_str("1\n")?; + } else { + writer.write_str("0\n")?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + let power_op_str =3D core::str::from_utf8(page)?.trim(); + + let power_op =3D match power_op_str { + "0" =3D> Ok(false), + "1" =3D> Ok(true), + _ =3D> Err(EINVAL), + }?; + + let mut guard =3D this.data.lock(); + + if !guard.powered && power_op { + guard.disk =3D Some(NullBlkDevice::new( + &guard.name, + guard.block_size, + guard.rotational, + guard.capacity_mib, + )?); + guard.powered =3D true; + } else if guard.powered && !power_op { + drop(guard.disk.take()); + guard.powered =3D false; + } + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<1> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().block_size))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + GenDiskBuilder::validate_block_size(value)?; + this.data.lock().block_size =3D value; + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<2> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + + if this.data.lock().rotational { + writer.write_str("1\n")?; + } else { + writer.write_str("0\n")?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + this.data.lock().rotational =3D match core::str::from_utf8(page)?.= trim() { + "0" =3D> false, + "1" =3D> true, + _ =3D> return Err(EINVAL), + }; + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<3> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + this.data.lock().capacity_mib =3D value; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index d07e76ae2c13..d09bc77861e4 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -1,28 +1,26 @@ // SPDX-License-Identifier: GPL-2.0 =20 //! This is a Rust implementation of the C null block driver. -//! -//! Supported features: -//! -//! - blk-mq interface -//! - direct completion -//! - block size 4k -//! -//! The driver is not configurable. + +mod configfs; =20 use kernel::{ alloc::flags, - block::mq::{ + block::{ self, - gen_disk::{self, GenDisk}, - Operations, TagSet, + mq::{ + self, + gen_disk::{self, GenDisk}, + Operations, TagSet, + }, }, error::Result, - new_mutex, pr_info, + pr_info, prelude::*, - sync::{Arc, Mutex}, + sync::Arc, types::ARef, }; +use pin_init::PinInit; =20 module! { type: NullBlkModule, @@ -35,33 +33,39 @@ #[pin_data] struct NullBlkModule { #[pin] - _disk: Mutex>, + configfs_subsystem: kernel::configfs::Subsystem, } =20 impl kernel::InPlaceModule for NullBlkModule { fn init(_module: &'static ThisModule) -> impl PinInit { pr_info!("Rust null_blk loaded\n"); =20 - // Use a immediately-called closure as a stable `try` block - let disk =3D /* try */ (|| { - let tagset =3D Arc::pin_init(TagSet::new(1, 256, 1), flags::GF= P_KERNEL)?; - - gen_disk::GenDiskBuilder::new() - .capacity_sectors(4096 << 11) - .logical_block_size(4096)? - .physical_block_size(4096)? - .rotational(false) - .build(format_args!("rnullb{}", 0), tagset) - })(); - try_pin_init!(Self { - _disk <- new_mutex!(disk?, "nullb:disk"), + configfs_subsystem <- configfs::subsystem(), }) } } =20 struct NullBlkDevice; =20 +impl NullBlkDevice { + fn new( + name: &CStr, + block_size: u32, + rotational: bool, + capacity_mib: u64, + ) -> Result> { + let tagset =3D Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KE= RNEL)?; + + gen_disk::GenDiskBuilder::new() + .capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT)) + .logical_block_size(block_size)? + .physical_block_size(block_size)? + .rotational(rotational) + .build(fmt!("{}", name.to_str()?), tagset) + } +} + #[vtable] impl Operations for NullBlkDevice { #[inline(always)] diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 20f1d46c774d..6b1b846874db 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -51,7 +51,7 @@ pub fn rotational(mut self, rotational: bool) -> Self { =20 /// Validate block size by verifying that it is between 512 and `PAGE_= SIZE`, /// and that it is a power of two. - fn validate_block_size(size: u32) -> Result { + pub fn validate_block_size(size: u32) -> Result { if !(512..=3Dbindings::PAGE_SIZE as u32).contains(&size) || !size.= is_power_of_two() { Err(error::code::EINVAL) } else { --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 C11572E5B0F; Tue, 12 Aug 2025 08:46:00 +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=1754988360; cv=none; b=iLPF7I8J6dzxh3RdeAPXh3g6wyx/pHn7Q7NUGQa/SmYj7lLbh87sSxxB8mYn+XKpZDrXRa+oX5e7ENOR8JwdBKokLzagn+r62ipJ3tJnoo/v2n/hKbbbTgCqEhBB8lhEXV3DEZrvZFTHVHfGoOpcHq//mHt5WMh5OCGW8xGBPMs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988360; c=relaxed/simple; bh=rUxVuA/H4D+xg3wZltRNuyZCQgvqXzcwSu3zTZMdS/A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=XE3eZjElzRwQsz/ROL4rX7wgWBFFRze+H2S8DaCTD+wdxcuvR+1nyI71Cetri6OSIzPJ2K8WaU8BQKPhSEtUX4cxPKJ8k6bBQgpde721ah5P1VFeRS8D3I+7I1zc1RtnaAuyeUuKTmrdjdH5qySdLKff/Ep+em6cEMdGaV72Nkw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=um6n+yZ2; 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="um6n+yZ2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id C705EC4CEF0; Tue, 12 Aug 2025 08:45:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988360; bh=rUxVuA/H4D+xg3wZltRNuyZCQgvqXzcwSu3zTZMdS/A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=um6n+yZ2l133+I/VacvlhO3Clzw8UOsTP8VJ7YAHjHHZv3e4zjpi3TW8n5t8l1baB i4oPfb6p/DGReL4A99+i1gsfJ4KnIND1gMwIIMDvs4LSACYYjnQr5YpjhE7Jjk0c7s 3h5GKDv1N8sZiUfTanuma21YNkqVFaFSJ57R9wcvTZYnvsZgEvLVI/ghEbaPxuQ6MB ddcFGAMK1e3EVbQs+X1u2TakH3SmdTG/qANuGwbQCfOTkylT1b5+rLsBSJt+eIdzUX KkQpCmtLgWdFlnFrZmv6aaGtuX9Pm/KQWaq75kncQ5Ry3653jb0aNDr33E8eANz36w G6h05cT7w48Fg== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:30 +0200 Subject: [PATCH v4 12/15] 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: <20250812-rnull-up-v6-16-v4-12-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=10623; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=rUxVuA/H4D+xg3wZltRNuyZCQgvqXzcwSu3zTZMdS/A=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8mTL2Z1Xmo5CyaEzB8B27B2jp4Sc9PBqJ02 /MlHUJMgLWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/JgAKCRDhuBo+eShj dwL0D/9kBcrCq1FtdE8QoA2w/ceXCbx5dMYx2DNwtxSqPM1YG5Mj0PMG2pJcYejSwmdeuw79zg/ HiKMMVu4PybEl8hNWGpXQ0i4zv26sGhUBUXFQqHN9NB/RAwMXZm2/aiySxDp5ifS2SsIX6lg2bh kUnVecK523DCcoe/W3tYMpknmmw5fqzrQ2aYZOWjrGU1cVqMgQaN3HlYq0V7ajUSrbvQQQdKVxy 083npc1AIda/l3tiVHbqxE2GQlrhE5/zsGdxmttyFVVeZh2sYx+wqfb1+qTDq7Z8Bmqib5O8z5O Hkms1tDIW/u4DF94hdtXYSoNkni7h4cXYSDohBBrDbL2vcrd0WRibAlZbdSQFfmcWXCpWN2qYRm Sn+xdYrJ0+97Taner9tLDjdvmB645Z6L5XXr0jD7w2Oi2rK5hGYbLuwNBsgHh7pM+umdZfsrajA gcQBrxrDccyODEiQC+LwaGsm3twOBgHfgGeg2uclIZF6U07FxwZrO+uU/pqTKzF78AN1Ew6QBm5 I9qlBF5vHV1/rBp3DN3O4tD6WFVq+lwDwCROIC2lKEXHFCDhEqLIhXWutLlt2hZ4Z15mpuy7L4p 6FlJZRr5SxoMmYjD/Mc9c+Ea/+JuRihBRCpenykwGC7d4El7lq97gmXt5JnPCZSBltxtmwQjvnY sNGQTk1WvdsoOeA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow users of the rust block device driver API to install private data in the `GenDisk` structure. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- drivers/block/rnull/rnull.rs | 8 ++++--- rust/kernel/block/mq.rs | 7 +++--- rust/kernel/block/mq/gen_disk.rs | 36 +++++++++++++++++++++++++---- rust/kernel/block/mq/operations.rs | 46 ++++++++++++++++++++++++++++++----= ---- 4 files changed, 78 insertions(+), 19 deletions(-) diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index d09bc77861e4..a012c59ecb3c 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -62,14 +62,16 @@ fn new( .logical_block_size(block_size)? .physical_block_size(block_size)? .rotational(rotational) - .build(fmt!("{}", name.to_str()?), tagset) + .build(fmt!("{}", name.to_str()?), tagset, ()) } } =20 #[vtable] impl Operations for NullBlkDevice { + type QueueData =3D (); + #[inline(always)] - fn queue_rq(rq: ARef>, _is_last: bool) -> Result { + fn queue_rq(_queue_data: (), rq: ARef>, _is_last: bo= ol) -> Result { mq::Request::end_ok(rq) .map_err(|_e| kernel::error::code::EIO) // We take no refcounts on the request, so we expect to be abl= e to @@ -80,5 +82,5 @@ fn queue_rq(rq: ARef>, _is_last: bool) = -> Result { Ok(()) } =20 - fn commit_rqs() {} + fn commit_rqs(_queue_data: ()) {} } diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 98fa0d6bc8f7..6e546f4f3d1c 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -69,20 +69,21 @@ //! //! #[vtable] //! impl Operations for MyBlkDevice { +//! type QueueData =3D (); //! -//! fn queue_rq(rq: ARef>, _is_last: bool) -> Result { +//! fn queue_rq(_queue_data: (), rq: ARef>, _is_last: bo= ol) -> Result { //! Request::end_ok(rq); //! Ok(()) //! } //! -//! fn commit_rqs() {} +//! fn commit_rqs(_queue_data: ()) {} //! } //! //! let tagset: Arc> =3D //! Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?; //! let mut disk =3D gen_disk::GenDiskBuilder::new() //! .capacity_sectors(4096) -//! .build(format_args!("myblk"), tagset)?; +//! .build(format_args!("myblk"), tagset, ())?; //! //! # Ok::<(), kernel::error::Error>(()) //! ``` diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 6b1b846874db..988e0ba63693 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -13,6 +13,7 @@ static_lock_class, str::NullTerminatedFormatter, sync::Arc, + types::{ForeignOwnable, ScopeGuard}, }; use core::fmt::{self, Write}; =20 @@ -98,7 +99,16 @@ 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(|| { + drop( + // SAFETY: T::QueueData was created by the call to `into_f= oreign()` 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 @@ -113,7 +123,7 @@ pub fn build( bindings::__blk_mq_alloc_disk( tagset.raw_tag_set(), &mut lim, - core::ptr::null_mut(), + data.cast(), static_lock_class!().as_ptr(), ) })?; @@ -167,8 +177,12 @@ pub fn build( }, )?; =20 + recover_data.dismiss(); + // INVARIANT: `gendisk` was initialized above. // INVARIANT: `gendisk` was added to the VFS via `device_add_disk`= above. + // INVARIANT: `gendisk.queue.queue_data` is set to `data` in the c= all to + // `__blk_mq_alloc_disk` above. Ok(GenDisk { _tagset: tagset, gendisk, @@ -180,9 +194,10 @@ pub fn build( /// /// # Invariants /// -/// - `gendisk` must always point to an initialized and valid `struct gend= isk`. -/// - `gendisk` was added to the VFS through a call to -/// `bindings::device_add_disk`. +/// - `gendisk` must always point to an initialized and valid `struct gen= disk`. +/// - `gendisk` was added to the VFS through a call to +/// `bindings::device_add_disk`. +/// - `self.gendisk.queue.queuedata` is initialized by a call to `Foreign= Ownable::into_foreign`. pub struct GenDisk { _tagset: Arc>, gendisk: *mut bindings::gendisk, @@ -194,9 +209,22 @@ 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) }; + + drop( + // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::b= uild` with + // a call to `ForeignOwnable::into_foreign` to create `queueda= ta`. + // `ForeignOwnable::from_foreign` is only called here. + unsafe { T::QueueData::from_foreign(queue_data) }, + ); } } diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index c2b98f507bcb..a6c1ee2190a1 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 Sat Oct 4 22:33:05 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 8190D2E2F00; Tue, 12 Aug 2025 08: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=1754988350; cv=none; b=Ne0+YDakcl1UUvDXvU55Uhl96fQIVVEIBUiDCpjxVHWDLi6bNiNfbi9NC4C9goLwyYJr4jZ0iuahC8KT8O34tfoerYb0wjtHaWK5vB4/ncp3HvdDgo5Ha2cbBjdM+nU79bPfSuDyKjSkif/YBafp9xa61z9Xto8c/I48JPZYba0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988350; c=relaxed/simple; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=H/ipXPidpcY9JJsH38Pv9yFGYFo+TVrVK8A/Kcr/jc8N1UvYaHmxUvzlHPtGJ1/SGXGKcoYzwT+lqJPMS52v68b17vWcOf0nmZVcObaEgP+DeYwpPiRP63ERDx6L9zm5snEIkGrQmkfKSgb+FbR1DPes9sYBRAzancm8Zo+4eAU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OnDqN1w+; 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="OnDqN1w+" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 24F6AC4CEF0; Tue, 12 Aug 2025 08:45:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988350; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=OnDqN1w+kxgPxjqRAoMGPGkSBnHIZ2OgTuodUVoMErKT7/ZonG9AapY1wOiEtKx4B 0KrcbayDHBijDL1XUXbezRqp29RPFi3w+dsD6f1pnz2Pp/219Gy3F4SMlodgjxxmuI +1MusBoCih8p7rDjQQvlE+NotTlGBRGDV0lsv5u8eHTc58TDkT/td0twI+kXxMYvAV /XFS+FTLDZaKklAKcg3jf0pftNA5u/pLZe8G+MFK264tiNcJ8ClQfwh/wGlLju/paA 8ykKx4NcnE4U1w9bcXEJbS7L9INGyR8s8XN/uDw1k3hZ7rV+/dQnZ4VyFw4r4PM66F QlhRNDBLRsLGQ== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:31 +0200 Subject: [PATCH v4 13/15] 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: <20250812-rnull-up-v6-16-v4-13-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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 , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1036; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8nR6f5YHNi4lcwRMm3sGBZYHg2ejkbg6QKT Prm3hxxSD2JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/JwAKCRDhuBo+eShj d+YtD/982aKjbpSoMiK83wsCuYk2VQaDUMyorncCr6PdMK/TwEBwD3wK56jhHzdMbzvvm9m5JEo IzhWFFPB2w/yBxlioQNkU2CJ+IcOgubz0S/eyeaXqdRSP+/Hr2Z+98WaEjHTfchuNDCa0X4/Qmb 4gJJQtbRDhb2XmquTWWdDVcoUZ39VS93HJXdrdcSz/Nf+ngdnkni23ek1LLGaNyQkUpb3F6QgJJ ochCpD5WT9+xpZDNnTpgOb/aKdSvMM0NApHeb3wiUnsdMunz/34jLAtGEMDQTSq6TI+pXCzdez9 aicXXTzZar9hl+/XsdCD2X8tV5z/gpmXtAFT5bUBhgDg44vVZhWaggO/v0YKC8lIusoF+7yhnFm h2X2nwlhAAnz+7XMX2ZqvGnsp7NMmihGFjS5WiQX2D81QVFEJyCrwTN0p6O+d5pT3SPvoO6zIXk lQQClEiJmT40f80EdS1QG9djnRyr9HvJyDL5dt7AxTIKXF+GQcBgWNQ3uEjZ3out/OLx5vpA3mE 0RME6VdfbQrPNze6FJfgu6LXX8l0onlkkTxTMo2qHWJLtyu/3x1zbE9SWFRQHzqCbsf8D9SZtKC kZDlnRtIYln7Cd2kNbw5Leuhc87SDeuRH2bRTZHGewhOHblVLo6rkNUEapS8KJBkxilhnSGBtCV zX/bebxdXI7/4oA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add code block quotes to a safety comment. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index f723d74091c1..3848cfe63f77 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -148,7 +148,7 @@ pub(crate) unsafe fn wrapper_ptr(this: *mut Self) -> No= nNull // valid allocation. let wrapper_ptr =3D unsafe { bindings::blk_mq_rq_to_pdu(request_ptr).cast::() }; - // SAFETY: By C API contract, wrapper_ptr points to a valid alloca= tion + // SAFETY: By C API contract, `wrapper_ptr` points to a valid allo= cation // and is not null. unsafe { NonNull::new_unchecked(wrapper_ptr) } } --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 28FFC2E172C; Tue, 12 Aug 2025 08:46:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988375; cv=none; b=AfmKKilIaPLl5mOTMPLt9HSbjiI5x70Y/uy8L7D6g1OEiUgZgL+UCezw3C5Ub1p3cUD7sW55aos/UIwPDoqvfUUAVo4upsBQKU5ofumKDVpdOwTBdMZUqLOzoDLwG0rVbFrMtWuEV+hO36S9BLjpvVzb1fYqO4xyWgKEN576seA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988375; c=relaxed/simple; bh=SarPWE2WmtPmfcOjFzORYVCDQimCZg5WOJ+Eedavrtg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jFQa45kOq/gSKbDpcPawlIWnjhswLwMqgdL89HuffdVG8tVSzillzZgqhXecchQuv2BHxDXamnLmO8xJQG8JwCD9vSf36/X4effnMkS8Fmu9kstGxGjBD5JWgTviiQrEOgv2NZ0irPsK8PVOJfQYMadZ3iM8uiu2xNetYp9LBZY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oaHEl/je; 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="oaHEl/je" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2F34C4CEF7; Tue, 12 Aug 2025 08:46:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988374; bh=SarPWE2WmtPmfcOjFzORYVCDQimCZg5WOJ+Eedavrtg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oaHEl/jeeFKaivAe/gvmDG8jl2yfl5CUpcD4N8Jseo6SKIlDHwDYxtJWVS1MD5Nek nQqFulVbOqCBv5P+Sh17OCVxahO2RIasmWvwLRMHnOqcbTjPF6g24oa9uLdaU0sZAa Y99o2ssVqsYeB0zPYHD8cttc63RrlNdbO6TLSTBFrJ4QhQXkVg+gzKNgzpK8HQdm38 erdFq/GHzTnnNz86eJQv3G4eEDgrBogOx4+n9vDvf5fhzFLCtyWHfJVMzkDZRkSxn7 ZY0apkLviitsMQz1pO4eXRKUBm8cmDkqvp2LvfnmfE7CJRssE4XBrn9yO4utxbUi4Y OVv5KPslGUOPw== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:32 +0200 Subject: [PATCH v4 14/15] 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: <20250812-rnull-up-v6-16-v4-14-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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 , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=5096; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=SarPWE2WmtPmfcOjFzORYVCDQimCZg5WOJ+Eedavrtg=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8o2QHChnXou8Lxfh1/0rj32z9j9ZoyXu9Yb jQU0+oeTeaJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/KAAKCRDhuBo+eShj dxHCD/9bmVWFiWfqD/26kXmM6NbrM25WuYly7maffAO/JqUGVjgJ58hRv/R9wIrQgtFbmuyTd/7 GHJZIql+aPLwcQMpj30xsqa5QvI99KhKRWK6ew2JUzw0k1fGBHWfMvyWbHRl8WiaZIo3Xjccpov 4MWO6nfANLhRQ9U4hbm1JjrEJvWkvAdDeEY7p52TYArTrX7Wd/fwt2kM/ZLf4n6UrpujpDI5NBn gxgE9UiWW5PDqqwBoeflDpT0f0+P+72PT1Chdoiy3l9vRVGQA6NrAx4FFq46zCZezYsefL9mBC0 cxMvcDVfrVzaRBVM8qZYcmHWL2MO61s2doOw+11l0CUgH+DrgJi7ONQJX4AadvCtDyIULHM+DgJ dTi+BG4RrzZ1RHdabtHkQHv23b9SHYKj7PmpT2ci03yOgyaKwEkN2+BP4EbB8gzlgubO13Naz1w /KU4Ec8VE4yOa2Nn5O6DSZGV5NgT8BMwf5DpOyhn0THQTszPlxDGBD9DCPXblxREZFUD64phPXz +0vU3gr7DwGreqsOFk/Cz7PObgFFDqzsTIs65QH30gD1oYRn1PjoxJ5rQ9Y5zxzZCMQZWnHMZS+ k7UAM2bqSlmX9qgdE4cQbC9dJZYHgnTHGsu/7f8vKtDS7SMFHvL3nO4NbTProrhI+98/NJlO6hL EK7inB/NmD+SHkA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow users of rust block device driver API to schedule completion of requests via `blk_mq_complete_request_remote`. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- drivers/block/rnull/rnull.rs | 9 +++++++++ rust/kernel/block/mq.rs | 6 ++++++ rust/kernel/block/mq/operations.rs | 19 +++++++++++++++---- rust/kernel/block/mq/request.rs | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index a012c59ecb3c..371786be7f47 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -83,4 +83,13 @@ fn queue_rq(_queue_data: (), rq: ARef>= , _is_last: bool) -> Res } =20 fn commit_rqs(_queue_data: ()) {} + + fn complete(rq: ARef>) { + mq::Request::end_ok(rq) + .map_err(|_e| kernel::error::code::EIO) + // We take no refcounts on the request, so we expect to be abl= e to + // end the request. The request reference must be unique at th= is + // point, and so `end_ok` cannot fail. + .expect("Fatal error - expected to be able to end request"); + } } diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 6e546f4f3d1c..c0ec06b84355 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -77,6 +77,12 @@ //! } //! //! fn commit_rqs(_queue_data: ()) {} +//! +//! fn complete(rq: ARef>) { +//! Request::end_ok(rq) +//! .map_err(|_e| kernel::error::code::EIO) +//! .expect("Fatal error - expected to be able to end request"= ); +//! } //! } //! //! let tagset: Arc> =3D diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index a6c1ee2190a1..fe516ca52ef9 100644 --- a/rust/kernel/block/mq/operations.rs +++ b/rust/kernel/block/mq/operations.rs @@ -42,6 +42,9 @@ fn queue_rq( /// Called by the kernel to indicate that queued requests should be su= bmitted. fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); =20 + /// Called by the kernel when the request is completed. + fn complete(rq: ARef>); + /// Called by the kernel to poll the device for completed requests. On= ly /// used for poll queues. fn poll() -> bool { @@ -143,13 +146,21 @@ impl OperationsVTable { T::commit_rqs(queue_data) } =20 - /// This function is called by the C kernel. It is not currently - /// implemented, and there is no way to exercise this code path. + /// This function is called by the C kernel. A pointer to this functio= n is + /// installed in the `blk_mq_ops` vtable for the driver. /// /// # Safety /// - /// This function may only be called by blk-mq C infrastructure. - unsafe extern "C" fn complete_callback(_rq: *mut bindings::request) {} + /// This function may only be called by blk-mq C infrastructure. `rq` = must + /// point to a valid request that has been marked as completed. The po= intee + /// of `rq` must be valid for write for the duration of this function. + unsafe extern "C" fn complete_callback(rq: *mut bindings::request) { + // SAFETY: This function can only be dispatched through + // `Request::complete`. We leaked a refcount then which we pick ba= ck up + // now. + let aref =3D unsafe { Request::aref_from_raw(rq) }; + T::complete(aref); + } =20 /// This function is called by the C kernel. A pointer to this functio= n is /// installed in the `blk_mq_ops` vtable for the driver. diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index 3848cfe63f77..f7f757f7459f 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -135,6 +135,23 @@ pub fn end_ok(this: ARef) -> Result<(), ARef> { Ok(()) } =20 + /// Complete the request by scheduling `Operations::complete` for + /// execution. + /// + /// The function may be scheduled locally, via SoftIRQ or remotely via= IPMI. + /// See `blk_mq_complete_request_remote` in [`blk-mq.c`] for details. + /// + /// [`blk-mq.c`]: srctree/block/blk-mq.c + pub fn complete(this: ARef) { + let ptr =3D ARef::into_raw(this).cast::().as_pt= r(); + // SAFETY: By type invariant, `self.0` is a valid `struct request` + if !unsafe { bindings::blk_mq_complete_request_remote(ptr) } { + // SAFETY: We released a refcount above that we can reclaim he= re. + let this =3D unsafe { Request::aref_from_raw(ptr) }; + T::complete(this); + } + } + /// Return a pointer to the [`RequestDataWrapper`] stored in the priva= te area /// of the request structure. /// --=20 2.47.2 From nobody Sat Oct 4 22:33:05 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 AE6272E2F00; Tue, 12 Aug 2025 08:45:57 +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=1754988357; cv=none; b=gn2SJF1tB+RyDrklSGT3z4RgCYsO8tXVq4vesXRm8Nn94O1xwM+v9jmIpsiPW6YMMvpfaBVQGRkNqBgT8AyZCVl9hOQhsjLLBgeqWQywPFnrAXvVs1e4FnnOEABRsKYG/EfLkapyr3gzk5zHEIftTeXtqpoMX1idJA4n7ZLgTSo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754988357; c=relaxed/simple; bh=Y3vina1qhyMgk8nl1C5xu+5Ei5OeBSOUuHwJTqyPFQU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=kE4voUMfFzfE/gPuBbfUtHcu3pPsa8lkDUkYud5bziVuL4zhfekjitQKoehoMvXH/NKQ8ohr5B4Xe4wKUibw3o2s33N0KqQqNNZ46kk0TE+rv/NcfgEIK8v6fnvjtrYztvIbKarQNLb0j/REIenAQheL0p5K+IqwXyNqT28xrKo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=k4B5LTe/; 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="k4B5LTe/" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6163FC4CEF4; Tue, 12 Aug 2025 08:45:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754988357; bh=Y3vina1qhyMgk8nl1C5xu+5Ei5OeBSOUuHwJTqyPFQU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=k4B5LTe/KYuFbmJ+Us8rQokdIyZpgNGr9iHlzUnK7yEGMjm8hLjhBlRpEjvH/IHh0 32030t7iLU12tpgLDWFnYP+2XuXgmErZbJbVxkRCLS5cSTFDMA1/E9XxZevFmVoLQZ RiYrr0y10csIlemn5KSb7MNIHekcluZ2ArU1QLE7Bb/FlDjPhTtHOPTtuQyHDeT2po xiS0Qqjd4NHqtABygJh5psuO7DCECdt4ZqnwOk+7sfy9oEFfNm2aMWocoEouoX6eyI vgi2mB7hSJFryXT6C2Pqv61bwhNRG+nbqjiMCTjZqqmkf+tkrOUZQ1wfWC7a/GT3PT Q0BYazNZq7xnQ== From: Andreas Hindborg Date: Tue, 12 Aug 2025 10:44:33 +0200 Subject: [PATCH v4 15/15] 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: <20250812-rnull-up-v6-16-v4-15-ed801dd3ba5c@kernel.org> References: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@kernel.org> In-Reply-To: <20250812-rnull-up-v6-16-v4-0-ed801dd3ba5c@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=6342; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Y3vina1qhyMgk8nl1C5xu+5Ei5OeBSOUuHwJTqyPFQU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBomv8p3KOzLO9xTSZ1fel1sUh05sdwYtsqovB60 dQt9ItidxOJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaJr/KQAKCRDhuBo+eShj d3AED/9vxUI0kMQwOYpfQIRuxeIoyYi+yoNMALeEuFXYdOixW7mHdvNfGqnN19QgnDuprb2IioA UgZqQvMt+6mLsakQjUZvL/rGDO4D7KSmis07n5FBvQd2s965L0K9BxQXZ97yyIDgbH7Mz5H4fI4 dcugL/uHdarTp4VkehMoD1luZMzpPIons1lEBbm69mtIoPLZTCUxU2d4CvaI/CHqQ+jTvRLCJSI C/cxNGHsJEdDcJ25baHJoBxwQtC6Stv9DCMUBxRfEWsMgPr0j01itPpdGK9Z2h/DVHT9h3g0kuE HHFLwT43uT2MWrlH/UIkyqDrFGNSu91kUGYyYDigAc2qosIR/NnisVY2ENmA2LAe/zPbItSfEYp 4ASpbwO+cow0JGc4+mdVYXmW0IVXPEg4gMdV+d+w23Z+kJIk/2sC/yCGXbfAfIp0zonYoa9Gike WxRFA4NSWD9kDFARYtoczSfmSKalp7j4SPJ6I0S8WeR03Uf2XMeJRnCb0tL/sZaHHfb8uWmCW3K GmdZy8OsVCVmYkYmZcctfc+oeg4QG+nGpdnX1iG028mKSNaJUXcNSi4ncGI+g6fpFYzfmf2fvIS 0DDmU2DG5qRL4fGCcSGdhF0h27rVpMitQFqk/oMSFW7VQ0AFAO4n2NIjKhGisRPh1SC9Xpi4CFK twyiBQ3+3ijN9VQ== 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 | 59 +++++++++++++++++++++++++++++++++++++= ++-- drivers/block/rnull/rnull.rs | 32 ++++++++++++++-------- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs= .rs index 8d469c046a39..c4f6131eb9da 100644 --- a/drivers/block/rnull/configfs.rs +++ b/drivers/block/rnull/configfs.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 =20 use super::{NullBlkDevice, THIS_MODULE}; -use core::fmt::Write; +use core::fmt::{Display, Write}; use kernel::{ block::mq::gen_disk::{GenDisk, GenDiskBuilder}, c_str, @@ -36,7 +36,7 @@ impl AttributeOperations<0> for Config { =20 fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result { let mut writer =3D kernel::str::Formatter::new(page); - writer.write_str("blocksize,size,rotational\n")?; + writer.write_str("blocksize,size,rotational,irqmode\n")?; Ok(writer.bytes_written()) } } @@ -58,6 +58,7 @@ fn make_group( blocksize: 1, rotational: 2, size: 3, + irqmode: 4, ], }; =20 @@ -72,6 +73,7 @@ fn make_group( rotational: false, disk: None, capacity_mib: 4096, + irq_mode: IRQMode::None, name: name.try_into()?, }), }), @@ -79,6 +81,34 @@ fn make_group( } } =20 +#[derive(Debug, Clone, Copy)] +pub(crate) enum IRQMode { + None, + Soft, +} + +impl TryFrom for IRQMode { + type Error =3D kernel::error::Error; + + fn try_from(value: u8) -> Result { + match value { + 0 =3D> Ok(Self::None), + 1 =3D> Ok(Self::Soft), + _ =3D> Err(EINVAL), + } + } +} + +impl Display for IRQMode { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + Self::None =3D> f.write_str("0")?, + Self::Soft =3D> f.write_str("1")?, + } + Ok(()) + } +} + #[pin_data] pub(crate) struct DeviceConfig { #[pin] @@ -92,6 +122,7 @@ struct DeviceConfigInner { block_size: u32, rotational: bool, capacity_mib: u64, + irq_mode: IRQMode, disk: Option>, } =20 @@ -128,6 +159,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 { @@ -216,3 +248,26 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { Ok(()) } } + +#[vtable] +impl configfs::AttributeOperations<4> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + this.data.lock().irq_mode =3D IRQMode::try_from(value)?; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 371786be7f47..85b1509a3106 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -4,6 +4,7 @@ =20 mod configfs; =20 +use configfs::IRQMode; use kernel::{ alloc::flags, block::{ @@ -54,35 +55,44 @@ fn new( block_size: u32, rotational: bool, capacity_mib: u64, + irq_mode: IRQMode, ) -> Result> { let tagset =3D Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KE= RNEL)?; =20 + let queue_data =3D Box::new(QueueData { irq_mode }, flags::GFP_KER= NEL)?; + gen_disk::GenDiskBuilder::new() .capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT)) .logical_block_size(block_size)? .physical_block_size(block_size)? .rotational(rotational) - .build(fmt!("{}", name.to_str()?), tagset, ()) + .build(fmt!("{}", name.to_str()?), tagset, queue_data) } } =20 +struct QueueData { + irq_mode: IRQMode, +} + #[vtable] impl Operations for NullBlkDevice { - type QueueData =3D (); + type QueueData =3D KBox; =20 #[inline(always)] - fn queue_rq(_queue_data: (), rq: ARef>, _is_last: bo= ol) -> Result { - mq::Request::end_ok(rq) - .map_err(|_e| kernel::error::code::EIO) - // We take no refcounts on the request, so we expect to be abl= e to - // end the request. The request reference must be unique at th= is - // point, and so `end_ok` cannot fail. - .expect("Fatal error - expected to be able to end request"); - + fn queue_rq(queue_data: &QueueData, rq: ARef>, _is_l= ast: bool) -> Result { + match queue_data.irq_mode { + IRQMode::None =3D> mq::Request::end_ok(rq) + .map_err(|_e| kernel::error::code::EIO) + // We take no refcounts on the request, so we expect to be= able to + // end the request. The request reference must be unique a= t this + // point, and so `end_ok` cannot fail. + .expect("Fatal error - expected to be able to end request"= ), + IRQMode::Soft =3D> mq::Request::complete(rq), + } Ok(()) } =20 - fn commit_rqs(_queue_data: ()) {} + fn commit_rqs(_queue_data: &QueueData) {} =20 fn complete(rq: ARef>) { mq::Request::end_ok(rq) --=20 2.47.2