From nobody Tue Oct 7 08:17:29 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 3F39F291142; Fri, 11 Jul 2025 11:44: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=1752234251; cv=none; b=D2zGYUlGmkHwPMMepEC1ioIqeaqoSF/5CrRhjckKVLu/TIljETAkUYrl7R3rxyQ+0qf0LcpGwB2K/pmwPPdqhEucsKJCHT3j55/bPE2SNP5q+8Z3lKihxcEOZxiWTYdlss4KS5678JFh6LYchx8NSVth654o5PLeXLfynHB2ZFg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234251; c=relaxed/simple; bh=jXiS1Zr4AgONsdr3cNnll+LNNcD6UkVhZL6a7Sj9en4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=m50lT+aqDx6GjARXG64ceZ98ZkVnyTWBKk38rLVF47q/RfGjsSkPlVMFxr/oz6vzQDL/DytCMtRq9/9s2URgH16zM8BMUAf4BrfG7oV//akgV30JAMf/0InuEa98ylrwAH9Hhr+tJZ3UwOh7WJc2BuaLtnA39gW8TkiDG7sVSzQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Xl2U/s8S; 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="Xl2U/s8S" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7A4F0C4CEED; Fri, 11 Jul 2025 11:44:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234251; bh=jXiS1Zr4AgONsdr3cNnll+LNNcD6UkVhZL6a7Sj9en4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Xl2U/s8SJOSM60Sf7KdgVwhMH19SB7gebIHAOdUNfko73//2lMNKpq2OjtmQ6CZ3m uw+BSIIW2y5TRgriqqcwbqX+MXhs40+Jk62PZIO96bwIsXR9ScjlmszBjUiGXEp9yP 371716yFKAHtWPp6RyQugbLeBzYh0O9p3d1jW/f0yRMZCVsXART9nN8y2R4E6WMl4J UbSmSaguEFjx5xuEXlUY85oiBWcTJRVubbG/6CAt7FFC50k/GKVq4yapuAYzdQjayH +leNylcVLfaPbVhaLHWr/c4SbctlaEb9Cv/+iWh/MBQPbmUe8UEog3czN9hdkk2rLu ANb3ZOOZYKlsw== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:02 +0200 Subject: [PATCH v3 01/16] 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: <20250711-rnull-up-v6-16-v3-1-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=821; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=jXiS1Zr4AgONsdr3cNnll+LNNcD6UkVhZL6a7Sj9en4=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjKl+Q2WYQl0UeRgBLWSU60DfU6PgRy5/yzh jANcaat0zeJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD4ygAKCRDhuBo+eShj d6dlEACa1IbHaekBERBUBnnKtwCaP4ThvpT0/KN3qi/7VdhuR3DOMWeb7YG6dyzrOfgK6F0ulZs cPoVC6Ggw5Tl78v2HCQwQZkl6/phSOCjBxOGYktfHXEaFpGCS6dwbsiKL7jtQ+72lXfLB5S42fJ G/DyXsLibet+SExoFK334tWPspLTG53C2J7xW+VLwngxu7ogPbscIMZdwPkMP8OXt1izE0QnJzv qLeD37ByDJDv5wD8jYByxaT5oteCrreGtk0D7ReUqwyb1yg0QGmVUlHOwegX6ld+1XfnnLMo0S5 ijFMugENhVAnxddhTkg2RaOu1ExoqG+hl8+96/vfh3zOnO/0h9rckweUKCqTHaSkmOWoMmOQe1i /tleXSLxDv1owkwQHzYGEsIwYWvRQAO9kTfjjFA4NGU0WBNuIF0rP9076SUq11aQ078LR+frCRg OIavP4cVs645znyNBK41+YHry9CoAon4rBU1JanbCHgSpa9vH25Q5E32FA5LBIPGxn0pJIzeUhV GddGtQlYy9dkSBTDeSgHM/CpkLgzKZqXYi4wy7xNdjq+8+XKgT283Zid+P57bapW6sGbrC8nQpT qkPqLX1iRIHQlVquYVpkVnwslb1OcdivrYLfGohuSoFjBkNF8fa01CWRTW1jVANISz2/Eshghaa WKqOfoXRszLB4Fg== 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 Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index a927db8e079c..488b0e97004e 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -3,10 +3,11 @@ //! String representations. =20 use crate::alloc::{flags::*, AllocError, KVec}; -use core::fmt::{self, Write}; -use core::ops::{self, Deref, DerefMut, Index}; - use crate::prelude::*; +use core::{ + fmt::{self, Write}, + ops::{self, Deref, DerefMut, Index}, +}; =20 /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] --=20 2.47.2 From nobody Tue Oct 7 08:17:29 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 10F022EAB62; Fri, 11 Jul 2025 11:44:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234268; cv=none; b=G2GVvs3/zU5t/f7Xs5wAhRbym4ry1qxRCP8rqLB0nil7oAXFNQC+gEdPMKzem9NuIHtJAl3Sfu2kV43BJFb52V/mYVf8sBJYsOU8AfJ+jPEUCnJTqTJnkz84y4igpw83arp8pI2aEH5iXEF0ti+dOiDqY6ZmS6HSwdCdwPLEsiE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234268; c=relaxed/simple; bh=YR3+KxDyGdcP+mNlRtl4taUFDwd89vDjI3tc8bE0Zrc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=BT6nfjyXURWUD1/thRSjgZ5BTYR4uSkhJfsXi5PIChACvSrGu+d4a7BO1N3ycnZdNmGW4xtMb6fE1xfKFnED8RUmZs+de3XGimxmB9P682G83T8wYCV+Gr3t1hHVImOJ6ZtCuNgzQj+RWPxUCg6wZ5SF9Chaw9l1mTkZgKgaymM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=l/EfB5VF; 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="l/EfB5VF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1CB3FC4CEED; Fri, 11 Jul 2025 11:44:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234267; bh=YR3+KxDyGdcP+mNlRtl4taUFDwd89vDjI3tc8bE0Zrc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=l/EfB5VFaOH9n3pwwxaB+J2zSQYRdKTthzcmHAgR9j2ccJIGcL4R3GzmB2EyUsQYt a1pRhH9ZccX28O2puUaUWFxduLJLlYIYO/vHC4/2eg3D8VhdI/FfbMgtMY9ThhY2qr ZYTUEO22HFSsY3qocRrc4JLgERW5uwc2n8cF0b37487IF2lgho/xBPZmBfmhPNh+fp Cl0H3afjPidm+5h0lIKwIepvuQ86k8P7E8Ty3AMQ4BHEYOJZmjHQ0ga4nKUZjkWrKm VyPTwAyx0sSHVL1SFVNb8CLtFxXgldIAhBJDKg9EuClDNybAWvgWDP9xP+cphC8Ygj v2OWJQIQnaOfQ== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:03 +0200 Subject: [PATCH v3 02/16] 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: <20250711-rnull-up-v6-16-v3-2-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=2171; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=YR3+KxDyGdcP+mNlRtl4taUFDwd89vDjI3tc8bE0Zrc=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjLa/RPa7IihwUNcpudTtRNnpMt3yl/RrYb9 W1u1CEqjECJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD4ywAKCRDhuBo+eShj d0OrD/4yBwrg+x8SfhqC1StjqxYxDerfuLQC5zH6ntePB3nydWy1awF/QWcW6s+6eZaNSvRwtFg OKWGzlmOWMgxdfSpiX752ELAsTmF9ge74MkbPnRLjTyrzvxKQtzrY9jTYAmk1XcSUZUN/NPUDQe 7bTeHvM4lNOZ4DE2Y1hoQ9uPhNbGb8mUH2ukexbCTbpTPVuf/smH8+N5PVFUwbc0mWvPHaLAFq3 0L8J1/jduPzdpohhZeRYDrPZjAH+6515zvaKCzdE42boj6fF1d68Vv5SsgJ9GJSzwF0l6DJSrLR uCzAWjcxwzNxVfWZT+l4YRWpXu7OqBHjyo2IZs30jRzbsPKkvsh3CsRC/lwnGy1ItcCXhkWF2Ii YOljPM2MX2/Fxhuwmckm0tStPDpRdaeq8jdetjvvZn3RROwHl/PwZcTccyDO7Grq1l93e5BVo3K nUpaahKPxa4peV/tADjc4tKg14gEGoZHFMBXqNpbare7RmFA0z8md0IYNN2nI1MhpeXIavyDaPM hmfY2NaJkczQJhTqkGSlpYbSimOjC83xshHd8WF4K2FiP1Aj+hfT9NgCpw2ciSgso0leaN7YJKw auYVwyQ1sqeVpN8fz75ecbOdmkcFDU1omC2NT1vmRhjV7JMgkUbBbf8+d6YUNJEcymxUrgdQ17p OgAoECwIGfGUBqw== 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 Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 488b0e97004e..41af456a46c8 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -6,6 +6,7 @@ use crate::prelude::*; use core::{ fmt::{self, Write}, + marker::PhantomData, ops::{self, Deref, DerefMut, Index}, }; =20 @@ -794,9 +795,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 @@ -805,11 +806,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<'a>(buffer: &'a mut [u8]) -> Formatter<'a> { + // 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 { @@ -817,7 +826,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 Tue Oct 7 08:17:29 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 AEF812BD016; Fri, 11 Jul 2025 11:43:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234231; cv=none; b=r28wFbyhK+L+qdoRigJz6NC/Nqw7ouD8DjqF+IHSvT8IleYA7aVQGtXDdWwaPgH5r/5tf4JT6eI/2yzU52uddRK9f6qLW95cGR9pT5IMHVYsWnPKu+mS6R8j5a/AgSO+GpJeNNuZyA4xdYEBFEQiu9F38AqpnI1H4NjHExk1WHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234231; c=relaxed/simple; bh=MFRvhK8iPDT6FMh7J1nCDqXSyE+ZNVaoKHHwymSCd0o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=D0KpcWbpEgzmyljB4P991Jx3cX4yRgAFIwFt8pD/dEslprlB3U7XAfm7g7aHll6TzhjxbRdIuhRQoUE1yb1ZGtWVVa26Oswe5AEKOmfY1LADS47zIEv+bc4q4mmgIPfNyma4fOJalN7bUVcrhcITG5ZYdH5amKBWcbnc471NUrc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rSddd0uh; 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="rSddd0uh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 831FEC4CEED; Fri, 11 Jul 2025 11:43:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234231; bh=MFRvhK8iPDT6FMh7J1nCDqXSyE+ZNVaoKHHwymSCd0o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rSddd0uhFozLcQMSccVl6VLJ0v47c+rk+t3WSZmYw40Dnuz4jNeZTiPRGuZrSu81X cUMX+konBdDDLe18Jt5X94wXz+nvMS/30gxXmen42ZkJ2CBJUg4fpu2aFhCxQHbA32 yt1JEhgrNqb/K2PxnaPPt6LtYOVSDfErUHzo69FGtkNqy8l2aHQlbEcBFmudP3pe/5 F6Kn98ZW+EWbsoFTGg+k+nqhnRPjvgrJGhiy9o+zPtjfwD03giyO21DFd7M7e39SmB J0O3jjKSfT2UileQ6a8WPY9Y4DNlgXG8/67DoIuCgJ1HvX1/lqbc/LkZfydSSujxcZ UKawDEzXYUVSw== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:04 +0200 Subject: [PATCH v3 03/16] rust: str: expose `str::Formatter::new` 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: <20250711-rnull-up-v6-16-v3-3-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=1687; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=MFRvhK8iPDT6FMh7J1nCDqXSyE+ZNVaoKHHwymSCd0o=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjMNEVGX2NFrpauvhDJbDfkW+/YUeSQRb1U1 ZAKq6h7gtmJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD4zAAKCRDhuBo+eShj d9ypD/4pjC5iRSq9F2EevIp+MEPWVyVynL5WashYO2zdZbOfpmyz0ViYVUz2SCGonKwCMasAU2b kn1n0/2C263SP56sUGgEnwswG8sBTmlrs5xsKL4FFWO7e+I+ft49cNginJajEGnfSAejUdEMl+Y z45RS5S91O0UUfNjs1fQzJ6rODtMivxdDIbRBWTGw6eyNOVvJ872Xt2JBcTWJuVUhJ7i3r1dwck kOK7vMWvuBOuMWE1S5GQT3+VBnl/gvDOA3btcVbhnTb0jpTPy8wWjJeuH8yztqOwgu0gNu/Cvnv whWMdzt2LKr3ZyM5ioiq9gdXO/lUG4Ti1WHx1BDe7WOTSCu/IlccVAyfsj6Fh5RYHzcEVQhAJrx K1kw7Olrdp27Fs22O2Kk7R428SAuL8E+eqXtObaItAVGtUgYZiyMXm5tV7TW+44YfPZY4jSpzc9 jWIbUC7JTuSOB3j2X5ATbi5TMpfDHnXWyfX7tJjvESVJrYNWHRIymqehYwlpmKGllxZBSaLZ/1P UnKizTajESklGykT5YuBAFzm33doKKMgrmkHLmmIAgMusT+sizCHYvbruPltebllqTuGjc6q0wU f9dBEGoW1y95iqPJD+asFongyQKuKe04JepWOtJAvMQfcvj8GiyONsu1Eto06PhNamdPcelz5ri 8kUWrQI5oNBrjHQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 rnull is going to make use of `str::Formatter`, so expose it with public visibility. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 41af456a46c8..28a6179385fc 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -703,7 +703,7 @@ fn test_bstr_debug() -> Result { /// /// The memory region between `pos` (inclusive) and `end` (exclusive) is v= alid for writes if `pos` /// is less than `end`. -pub(crate) struct RawFormatter { +pub struct RawFormatter { // Use `usize` to use `saturating_*` functions. beg: usize, pos: usize, @@ -795,7 +795,7 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// Fails if callers attempt to write more than will fit in the buffer. -pub(crate) struct Formatter<'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. @@ -810,8 +810,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<'a>(buffer: &'a mut [u8]) -> Formatter<'a> { + pub fn new<'a>(buffer: &'a mut [u8]) -> Formatter<'a> { // 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 Tue Oct 7 08:17:29 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 928752E03EA; Fri, 11 Jul 2025 11:43:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234224; cv=none; b=ILPy53hzjxiTiAd2QnQKZMjqlZHsj02qv+zYtK1z/1XaUGEFg2XaxCOGt6NmFS28xMfHEo7mvMUFz1NEwYeCOqr8F4z/ARdAP8qpT95hMtV5B6nK/3dP9nd3g9Hgagmy0l7B2Pc4oBSlSg0hLVpEnJq3VTkQVCTmK1rf0gx0Tr8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234224; c=relaxed/simple; bh=vgGFsiKABw+m9kDtLSj7y9JLgW+Gk/xJ51MxKxfWHeE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZpKgau/MOmj875KcHLglq0Xwi5BG/8DOA2DKDdS6UzYOegkkdqyKqvJ1LzTHi8S655IFQtsbn75U3lPymgrfK3aJYYDeLkaQhYwO2vlBuc4NaUWvjkcHarGHqo1YE1U6HE64Prwuqloe5xeALhs2NWfbI6Evnr3P+HPqYPwuIrg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=szUMbKzE; 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="szUMbKzE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9B8A8C4CEED; Fri, 11 Jul 2025 11:43:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234224; bh=vgGFsiKABw+m9kDtLSj7y9JLgW+Gk/xJ51MxKxfWHeE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=szUMbKzEg7cw4GH87BzqC0rSy4jsrc/UMIqvLUDkbVYZ5T9uvuPOlhBjByLM5e4qy kOIr9rEcI8QGWu0F0FYLtLbhfNxUM8x4K0AafjvxCg4nh3mh4EPT80m8nSLrhwRIRe AmogqJh/KjJlA8f/p6rKFhYvltX8dFMeBNfWFQffbvlM6psVsiGuyWosFuQzhcJ7+0 wyFgcChOpn85Hw0e4mvIhFEwsFvtYkjtZTtVBtoVdsYaC2DColk8JVufpd9eGmk5j6 uOa/r0Jz5DhMootY4lUAhkLjufc6G+YSvjMOQWGIUHNKNsvvrB0JWRguR2Pz2EazrP rnu5HuJ18YW5A== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:05 +0200 Subject: [PATCH v3 04/16] rust: str: make `RawFormatter::bytes_written` public. 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: <20250711-rnull-up-v6-16-v3-4-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=684; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=vgGFsiKABw+m9kDtLSj7y9JLgW+Gk/xJ51MxKxfWHeE=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjNxcEYjeAkY0MGRQ0Mh/HGA6ILX2qDqZ1Nl YDx3bkCS3CJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD4zQAKCRDhuBo+eShj d782D/9XKgb7ERKPIoL156wLxet8cxy5LHF+uVxpm4iJa8Wc+Cn7HJTn0o7bpieyBuER4I5Ft6H Pkqfw5eqHNU0jHOT1cosXSuigCRYZ30rEOjfEsPdyBTMfCAlFxPMCxY4Jxf7I0yXdpGtinppf69 wj9S5X3JgSse4g0CVI8qtQ5Eqs4XsAkoxOv4LLsdmeZDeqKpI0CoLimdzrW9omQIcuxLA6RfnZ5 vHe4rlhhpiXkpJSoYuTU5p7LOwX9G2Od7cYb4NRUiRCmUekV5BQ6L2eetawF2/16cZKzKiqr3Ag 5qllCPBor7G+pnY5dgVAIQIbfSsQeKqlVdwetqLRANiQAVzEUogCC9b7jOYuY49OZ0q94oJt/ri yaF3AQUjmKQNHQJzGOATB9OhKRmo/723Ej0bF1dlwYEtoMtmBCRbPezgB8t8IwZjuQfjfzghCgI 1LL1dL7aVcb6wAqq4lys6eIqxXR+S0ZS/OCRUpBgtlrG54wajvzp2w/Cu3RaMVRYT9B9BXVv3jr Hlx5Nu99HfmO42FxwUYTSsOoVSYnaeyphS5GOVTYf3kpk5iB18CcxGBgRmYhIJWb3Hapv80a/3S 0LPrFY4W0lNm+kCUJmib5ymsSRDghLnUwJ+d6yh6mOZC6k8zuX/lIu8gX0DO7cce9Tifqe5coXc es3+yC5+HrYFijw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 rnull is going to make use of `kernel::str::RawFormatter::bytes_written`, so make the visibility public. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 28a6179385fc..b1bc584803b0 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -761,7 +761,7 @@ pub(crate) fn pos(&self) -> *mut u8 { } =20 /// Returns the number of bytes written to the formatter. - pub(crate) fn bytes_written(&self) -> usize { + pub fn bytes_written(&self) -> usize { self.pos - self.beg } } --=20 2.47.2 From nobody Tue Oct 7 08:17:29 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 B48A5299A9A; Fri, 11 Jul 2025 11:43: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=1752234215; cv=none; b=mKjAdGAlFN67iAHbixlvxzsIVlxYxXwG+yWpgUpiOdJ/qwVS1WHEqcSksSSyBYevLUokLoEnp3OYW1ivzBio4Ezm6k6iMqZGBabc8H/lau3178EVNKOLaSQ2ffTnSEfQuQskYq1rnsreFrrdJW+ROVD14NyZHe5xBrXHhoHYiws= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234215; c=relaxed/simple; bh=EhHdiid3UqGuUgMiYwFJtu8dUDXo0aTr/QDEzmI5oUc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=a6vxbmK9Y0vfxEi+kxeRyV/K/YRorXEOTeOQhE5zQ/xOd+qjQBnPPWXDuQA8endjL1Au/bi2CnmtsZBsJBASHSRg8HqYXTWHIt1Ebge3GVgd0oFPQnUgPbv6FXx54bNS6VoYUPH4MkXEjJ5aL+0BaGr+0M9dhc34l8id22VIGbE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fuz9EZwK; 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="fuz9EZwK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A06C7C4CEED; Fri, 11 Jul 2025 11:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234214; bh=EhHdiid3UqGuUgMiYwFJtu8dUDXo0aTr/QDEzmI5oUc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=fuz9EZwKBmjSvH5fGej6no7QVdsInQo2sKOPB0s+58nAswK4J0l+dopviO78U9ObG vY0STPEUPVQmM6CONsK5w01wY6XAndqad+ztNC1r3KNIBfwY4Jwek0aBjvog5aZRYB +uLLiQ++mbkZgjB0Lj/ZOWBRLiyNUZK+lNbb+/sMNuXRMqQ4t7LeqVinAG3c6XqDuU 347IwJ0BhqakI3tOruBFE4djUrALeMbXtTSYvbx57BbuX/MI5plX9d/4eMPdLipP/g wyUhV4TL4ROq3RLliNItIy9VjRDKLRjB7b0Sc3TXT9claCqVVhuUSeofxClFta+Vyw 59u4GgzKSaQrw== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:06 +0200 Subject: [PATCH v3 05/16] 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: <20250711-rnull-up-v6-16-v3-5-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=2415; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=EhHdiid3UqGuUgMiYwFJtu8dUDXo0aTr/QDEzmI5oUc=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjO0ay/x8XurYLiBY9uUrinQhxpWdlIN08tx O/PlQFgf7WJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD4zgAKCRDhuBo+eShj dy7DD/90N/KMj2ZMuzp6xbysj2qmyIXagWj9lcuKrdAK6j653C7Y5lwSkKGoBWO/psiT0k8w2tF p5zcOd3NhTsmdriIfZJN29y4/UWpawpVeiB/5ilz1o0jP7b84JruSQjKqB+BKqHt4Sg8s8MeZLt guedU0rjY3vos4Vlj+XVVdhuf3BLztLpEyNfQ1nmjeeo/MkqhHFmJBfw7bPcpYK5pLRu8PBPpMw py7P7JQPRIOcK8WgKbyStSDzS5/onth15SON6rHyMXQ7tl+Tz8UkOO2+Nqqvfm+fPDXN1UcgzLn a2p3e8/xUaYDOwelkAWxmgtA6HbiUHV0Z0EbCBZ/2SJkiiC9khe9iUq2Mj2qv/hpyOOhU3pfaKg FwnsRp0G7FWSQGbQRsr3KCqf5A89eUkCXcS8g6/Y9GRwavmnkk1zamGewaQMZlo+3hmf+bhmAa3 BK6FD0CB+wGKaOCeClBg8uqUlaln7SJic0TfjE3nB38eVIKNHSBXJbOkDwaP/I+D2d5F7H0ZrXf v3BG3WCSowYbkv/M+vnhQArlDuU6NVmR75rxa3bNXu4lVY78Whqfbyi8dm1eoCs+ejeOz8SxWv4 6ENi7wlC36jVdOF4uU4TXL5UMX1GMSmziLBtvWzyXZqe6oLsMw9yCXLopB+3TO/XrW2UEkyXlwX u4CmFp48PstXWxA== 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 --- rust/kernel/str.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index b1bc584803b0..c58925438c6e 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -838,6 +838,56 @@ 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 +/// +/// `buffer` is always null terminated. +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 null terminated the buffer above. + 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 null terminator. Buffer length is always at= least 1, so no overflow. + if len > self.buffer.len() - 1 { + return Err(fmt::Error); + } + + let buffer =3D core::mem::take(&mut self.buffer); + // We break the null termination invariant for a short while. + buffer[..len].copy_from_slice(bytes); + self.buffer =3D &mut buffer[len..]; + + // INVARIANT: We null terminate 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 Tue Oct 7 08:17:29 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 0C3472E498D; Fri, 11 Jul 2025 11:43:48 +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=1752234228; cv=none; b=cSkF+u641doH3SAE0+0nYweFUzrdps6NKgEPmBoa96ppVpJCl1UpMOAKnrARTPWW5pcq7+uT0vQ8sV7aJ2bgabflcEJw8iNcN/+FwVF8rraDwCBqS2HR4mzM36DPzAkCibXtlsT5KLfcwUCuqVs5Q41cJlbZfT/GW5gwzAS64Qs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234228; c=relaxed/simple; bh=xZxbhgmVN01wUF60DeMwDRhnPjojUh1KgzCRHhMmUmA=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=PNQhePvMW6kyl/wc/JBIprr5AeuUsEAuq0j0rmFk36Skk+Yl0J06+Zwt/3+2YOxbSfy173fHqnk7FlW5ZnbXMv8RWpH6DEwF9okW/cavWZ1v0jkCfuXta4m/ZmgVA0QojWiSMDauwrlXFMKLcnD5mE7nYOZW/xiS/6zRXYeTaPI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=hSO+xglU; 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="hSO+xglU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 14D78C4CEED; Fri, 11 Jul 2025 11:43:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234227; bh=xZxbhgmVN01wUF60DeMwDRhnPjojUh1KgzCRHhMmUmA=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=hSO+xglU/FqjynG7/j8fwnroX0gNti24HDSlnUdwWaG2KUqVDlwtdG2gZpwsCsDa2 /ZwfzbFxZlniX1/mp2elzZuHMaW3Ku8yGEUgoEp8sScpcFdBEAb5jb06t8rtIbO6jk rtG3JbMGW05maE/XMBTPzf9q+lCgqTRbYrXz11OWWl3peGLrDWNko+7ZDUC7RCmPBt k4Ge+gD4GIPeMlaZuDR3VVGW3KTAalLkkbEEfHZLVljqCQwdVaTNU8bNSH8hUTchhU TQQ4Y1y+gDaH6r/NLBmdwlvpGLfHZkg15YvbnNKyLN4PbpeBDP++Qe5cW4g6Rl9H2J oRRfI1aAm+NJw== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:07 +0200 Subject: [PATCH v3 06/16] 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: <20250711-rnull-up-v6-16-v3-6-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=1124; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=xZxbhgmVN01wUF60DeMwDRhnPjojUh1KgzCRHhMmUmA=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjPTftHNyvPJOZBiXgWJKvvRUufBbmAXg+Hm nu8v+K5DaCJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD4zwAKCRDhuBo+eShj d9TrEACYuzwo6y8OnstIRUqvJK5xiumnApmqBDRQqE2BaCPQ8hQDVC72NEmfxRgMQf8leI7QjgV Ami7PcQwyBGp9xkK1dMeAvrLfHvgW1VQ9q9Rnm37hwndOT8xV6gKFOETe1mvi9aH0HhS4GGTRgB RSDMTwsygG6iFz9mTeB/BwTVwPsa/wb63iCfGNl65O3e2My/PazDyUB5cu4LuOKaT4CIHmG693q OyLwizIw6gCagMUcI2pLW3LmxLFB+j3K/2HgaA/Hsx9EcOW2du0J2/TFTDAVDGFtnQT+MQXWmmY CvEme6mP+i0FsJGR4lvwjus0SPU6mWkzVmJXAwXlVQMclpoWEAOUXoFsMM/62uhiUE5sfkGX8ZQ XoEPhFSdRuL53IVO/8BmUcb1WZRyXv/RhEzddj3HEVbwSRcNcliJhC08C+ZY4MdHw2ZysAg8SGT Ak2uu7AK2AKjESax8OJmA34hRfyb5kZ/ThDQJK7Ky42ojVSb1VRGDmYBs7bBRlLCdXkVZS2/hQy TtxgNG0yvXc5f/YMXYGlZidw/aHCV1TU9hzt4AGaboUXGtqEdW9r87KL4qEyyjNQW7v+CpClC7C oGpIfqgw8vjtDPyk3J5UF5JOOvBZ4VNL22eCSFXl+8y12sAdxFTuFkVFTxLVEz6OwWz4v8pQAbJ 9OqpPGXdtixG5zg== 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 Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- 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 Tue Oct 7 08:17:29 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 32858218EBF; Fri, 11 Jul 2025 11:44: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=1752234242; cv=none; b=XG/fndMeKq6LWan8lzuUpJYk8LspZpQ4nBORxM9ocHzxrEf9P6mSb0FsG+qloEH2ylU+mUbkPduwyLXJmyY5CC72tll0mxkNKhw2MX2sxSsynCEZgbBmxjE8tp1l3LUKQgUL9mkZq4SWQUQBkR2v/bDiLStkTi/rdIYy7G9cEXw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234242; c=relaxed/simple; bh=c8ef8NCxcJj3uhP3telW9Gs7hYZ+vnC1spPz72jpOXw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=tYs/5Z0GlELmi6s6tKnZ9U0OuoBx5UKItqkNlFNd/3P+ecNm4eGkh1B/5vlskZm9CsEnTUV0GUAF6iIyLzqMZ5lZqxhzvFsNoojjQ8258iwpCgGL+UKI9nx7UQgUGEyIYoPWQdIaNZLlxN4r5WLemHcRlEH6Ymy9XHsWRSoIOsg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FPhYgKfd; 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="FPhYgKfd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 640BBC4CEED; Fri, 11 Jul 2025 11:43:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234240; bh=c8ef8NCxcJj3uhP3telW9Gs7hYZ+vnC1spPz72jpOXw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=FPhYgKfdqbO7r+g+fItzkqokF7x9IzuEZPwUh5nmz2Mzp49/GuJW2VbOXnDJMGvH9 MTtNxsNpcSHU66wQmg53ijo+cuuQt3DYAulm/f+n+M5h7ynolr9GA3XlJRUFVoOdoJ ZfulZPbL7V0bV0ivMhSK12y673c30FkQc0HDhVxeXKegmg00wgL30IPdcZLcFVGtuH +m5jVX1Ap6o7dkIeLK2I8Qwy3+1tl3x6zh5lqnx+/vSKBn3ZR6VWaAGdQTK2hVgaL+ M+c5EiRyr/LkTnpjvpZIkBevkvaNGuxFoi3ncKNfsoHS7OhflHiNCao1p5ZhT8NwKn JC/sgV8NGXqbw== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:08 +0200 Subject: [PATCH v3 07/16] 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: <20250711-rnull-up-v6-16-v3-7-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=2864; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=c8ef8NCxcJj3uhP3telW9Gs7hYZ+vnC1spPz72jpOXw=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjQfUjXOcIs00NfAyS8HbQU/kkUa/+DvN9RD Y+RJQ+1useJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD40AAKCRDhuBo+eShj d1BtEACaOsfocomU65bbbd0tTJUQH9S81vtE5L2lCPboJoYhEcb3KQLUIb2tIGs1vaSEBKtqsuF 7CZ3BMzrBGP9Co05GFO/wP0sDzmnijluy8Lwj40tcbmPcP11WIJftbDckGuwTSQ/NlKNMxCyUmy cPE/4Jv7yJq0mCwZhEM2l4hlWiVJ9kpt2K4KTjMDXbvUiPdsIjPMJ8MvF8A+Eua7vHsYAc1KKa8 5nY3vwv89+iTOYelPOIHGzkdGvInB+X5cpLEml0hzItEqXQhzXVqNio192LqbhV14s9jiBtlE/7 0ypFN/aG5Dp8lqv7AHXY370BpWYr8s0vEH7hx6TAM+T+YnSx1Q4aj4aCkfaPnkfPi+sBb1E+4YU 6/qLts5wFv0O6+W4k/UP3NIQSDx8u0Eg+/tgrUqJ4ouHKyMohdeq2qhrxSALWTLv51aNkRwoSKN /GEKEpvrwVHaO2FPe5v3EEx2VV4XraD7XP/s5a4xqFvh1GTHRdY6z61pHFak2KXhlp4htx+Xqax 33SyCxuNcM9u5onMSx53zyKBAtWCYrtYtPVpPa+2cH1QKP2eBhiqAON/QhJsMIB1qqaD9Sl86WY cGzMYmAVHEsKHeDTfsaWYX7+KvEaasmOZ8XZL0CLjq5zO0Fw+0oVJdh+CFJ+OPHjRQE7w1mx5eJ cidIwNrWGSIgnNA== 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 Reviewed-by: Daniel Almeida --- rust/kernel/block/mq/gen_disk.rs | 11 ++++++----- rust/kernel/block/mq/raw_writer.rs | 1 + rust/kernel/str.rs | 1 - 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 679ee1bb2195..39be2a31337f 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -7,9 +7,10 @@ =20 use crate::{ bindings, - block::mq::{raw_writer::RawWriter, Operations, TagSet}, + block::mq::{Operations, TagSet}, error::{self, from_err_ptr, Result}, static_lock_class, + str::NullTerminatedFormatter, sync::Arc, }; use core::fmt::{self, Write}; @@ -143,14 +144,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::from_array( // SAFETY: `gendisk` points to a valid and initialized instanc= e. We // have exclusive access, since the disk is not added to the V= FS // yet. unsafe { &mut (*gendisk).disk_name }, - )?; - raw_writer.write_fmt(name)?; - raw_writer.write_char('\0')?; + ) + .ok_or(error::code::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 c58925438c6e..7396c49174cd 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -858,7 +858,6 @@ pub(crate) fn new(buffer: &'a mut [u8]) -> Option> { Some(Self { buffer }) } =20 - #[expect(dead_code)] pub(crate) fn from_array( buffer: &'a mut [crate::ffi::c_char; N], ) -> Option> { --=20 2.47.2 From nobody Tue Oct 7 08:17:29 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 E97E5291142; Fri, 11 Jul 2025 11:44: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=1752234255; cv=none; b=EJ/4uLZY0lyjUUEjDy4E29nGpUgyE7mpYsaXSZL+BboKR6Fv/eymjbmTgQB9ByqrPm79mSPjE5WU4MIOCjp66FCVuA1k797UT33bEhchB9Cq5LGOH+FHwx+XPkZPgCkSCRWzIZbl/6Nwh/z00IY4J01vytAizc2CRGKl/WEFx7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234255; c=relaxed/simple; bh=1AU4xJI0E5Q+J1Ud3T+hTZQungA0edH5V6tp+Lkf0Fs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=FYj35rkJnsG6x48uyJemIFjoKmbLuuvCGum6gXBAMHu7d0wHbAg3CqRh0S45Qv2p0wng+cEhRS8arYYvTGVFAx5ILjCeTr666gdYJBPcB1egOeOZq9lObkJUy32jHOn97KP3kkK7J/yC8dLsi84arQirrnUn0LXUJDkdSnLvSRs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sccE1Bt2; 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="sccE1Bt2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7E2FC4CEED; Fri, 11 Jul 2025 11:44:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234254; bh=1AU4xJI0E5Q+J1Ud3T+hTZQungA0edH5V6tp+Lkf0Fs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=sccE1Bt2EMVQb0TBPpUYIMCtxCz4Tikua/en4ARh3pSK0mqIDbiXp9pqdxgncVGZE SEiHiBd113HukeacYBUj67yz2RQgsvDeV/VH8Zc/329m4PBhd/cdHPqbv79H09zXbF g6P8NMAkUZB/O0JNI1Td34PEkNYM2NYV1RazGHvnc7OX5rH1vUbD5xKbiUZISEbaP2 Cf5q/lgXLrRxh/0vhoLedC1VgoJy2kRipocxAijJ//P62TE4LCIBoBMk7oP4TDUsgP A+2UC7t2xZ1V7r4ul3g2WbBrY2Au5jUQb1I9daKYHrDRVaf+VJ+Fm2As8QbbbJernV ob4GpVK6RMuFA== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:09 +0200 Subject: [PATCH v3 08/16] 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: <20250711-rnull-up-v6-16-v3-8-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=2399; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=1AU4xJI0E5Q+J1Ud3T+hTZQungA0edH5V6tp+Lkf0Fs=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjRreHfGsCeoO20oQo5iIL6Q25Uy0r7oK7PS v/GWIRDww6JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD40QAKCRDhuBo+eShj d9BxEADGYPRAd2cccXOYG4GjLum++ufegiZ9XCBIEdyYdDdNlbx6LKC1cR17scivwnsgK4+cfqW rprNg3WwCJz6YaVmuClCnc9gdYL+EC9/bmhmer4m4FbV28Q7v+r1W1UPI0g/U0YXOhE49bZN+YW 2hguD9fnyW0WefmVMXKjQDfO0EppjV+IHcWTtbMisiAqJpm3xDs5i7qwI4r4UyU7eSgc/jdgOoR 7OeA+jZ9amrJLqdpoLqUYG28gbfGSEJZ3QLlStwm/htqvW1MacjDg/lnbxdad+V9HCRo6PIyX5Z bBR50ZP11AHAt9SlXAoa9S1xxl07m1ry8b45AyoC7ZnWnZfANPlpsoF5i8y0k8q8s3pRfkcqgdo lbDIIRUeLggHhIaeneS0izbesVoOcWmN1o39j/UGOJOOCc1aFobQj1HdyyD2eECPzhuk3V2EO8G 8o2gvGSR6fJgA9MQy36G1JmjWJjpV8COenrI04kc0upJrS8Ocy2Rx5iLwYsDd68HazaEITspufR 9Y5zGnQCo1mA6uDRihyL528gtIjivSewICLG0YEAiOsgbm6um49hr6xZMNwAF+mg/HUBUFAnk/6 Areih00Vb+N9GAZ9/IiWNnaqYOvnBWE4zIrw9orDGj9HnlV0ZHI+ulQyup/ORGe6sDhrvnabQYP ipCPe9s9mMDXzxQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 `RawWriter` is now dead code, so remove it. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida --- 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 fb0f393c1cea..faa3ccb5a49a 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 Tue Oct 7 08:17:29 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 04F4328C035; Fri, 11 Jul 2025 11:43:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234237; cv=none; b=DGyqijRAuYc1fICBBXLaN1tqrZJZsZlIUU4BNcTsF+76OVts96kuwLBTHmO1QO4OxczWUMc6d1NJqi+rEdFeLNBf4UaeuvoVGZA24LgAKcxuhioDGVOKBTPbMvETTShB0tJo2/0ZvKqaQ7/v2Pwnmz0QN5Byowt2lWb09DAAOQ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234237; c=relaxed/simple; bh=0EQMqwW4o3TiIuFA9NjfmF2wt3+kw0d4Rv4MVtzTMgM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Kvau66TlCfOwYDQQUoBKM5gGpnAm36Y+JMMvyeZ8fBC4anpVlDMMaz3n/qFq3Ezo6VR/cdjeJCgPwBuw/DpKaMadF0hE7hmlbuLNPtHlMa7s/Xl0fMwc9o7p6WNzJZ0uejTzwwdn35HDVrkASM9MeUw9a/rUkZksEvra+VMySpE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=uPx1VSrL; 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="uPx1VSrL" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 42A94C4CEED; Fri, 11 Jul 2025 11:43:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234236; bh=0EQMqwW4o3TiIuFA9NjfmF2wt3+kw0d4Rv4MVtzTMgM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=uPx1VSrL1qUSMH2yx4i7iPhRQwN1dNmOKWFBkiOBiZWerIakMcdnhE+SI51tj5V4W vcyq6W0jjrqC9/zDjhGmm2Uwxe2SQacFzPkPMA798y4jsbbdvj0nDB+WpfEk9m0b9M MRtGpfcVHh1W+VxnhJd8RkOeAqgF8CFLs2/q0QzdYW+sjpZvtZgEbg//NOr2T9NWND k3NP2ksNqk5tRGLsnOCjhG6Q542JRbj0WHCS5w3Be1wLa7RgXbSFdVIUxyP5Ww9bH1 dB/NgMbtwIiEYhfA9ea2mV75pVbYxADbOFynH9Mdp1RbXaFoPzsZEqlGytT3G/o7CS JsOPd9dwdbUDQ== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:10 +0200 Subject: [PATCH v3 09/16] 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: <20250711-rnull-up-v6-16-v3-9-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=903; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=0EQMqwW4o3TiIuFA9NjfmF2wt3+kw0d4Rv4MVtzTMgM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjSkIuSjcnlZY9GDxYXn2lSIO/Xb/XtfY6ig WyxQC2KJ22JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD40gAKCRDhuBo+eShj dyxRD/0Rjacli0N+bgNxxLgMjy2/7QOV6TIx+3QHQJ8hBtihGeZDtGxUmoo5Lad8rK04RPTE2Pb DlQru3haN/a4zWB7VAbr8xtr2e/vdEtcUPqqmziul1AWUF7eS4+DgUNttzdwi/sZoq4Sd+A3geM HhquptK+5o4x5AK8ImD8Ins8PKbjfneKLIDX4SU8ypdByk8D5bcK6IJx0Xef9pPUBwnHGJOYSs/ rSl/qshZlJIxz9RWfYN9xaIKQK+PMJEKUUyrvSTQXCOXEGvUO04x2/WPobSbCD6sRjcSn9cPQLp 4Uk+KT36C0lewZ/JWKJ1kN/YqE5TT2b+yE7pxlFyyck0q4gH+lLmzpqBkZmSYdH/BK8gW7XdxTN rTL/sB+CpE9tv21xRHUruQymO31JJia01UjyJjwOq1SpDsYfCLurHkLPng/SBzImX1QoeNkcTD8 Vso/y/OiHNCReQ+j9yKx54hO7u0yeFw7C18QogQQDMZPnq0nEt57sTS7svJoTWkRaLw2TQ3o3HI 8dhFtQwJKCtjdcUluF+DNEZZzI5UIOOmLhzmzRuYDXLWDOpP9rrzkbnPwz7aysfwaCL6AuJFVoj fK/+ZLZau2lRphmTkqizq7VHpBsSXUO3gIKKZ4YNr2YxlxWDZcCLrvFM/EmJdxMLFwDHMfa/M6A X4Sxy0IZpIr1bwQ== 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 Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- 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 4a5b7ec914ef..2d14a6261a31 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -53,7 +53,7 @@ /// [`struct request`]: srctree/include/linux/blk-mq.h /// #[repr(transparent)] -pub struct Request(Opaque, PhantomData); +pub struct Request(Opaque, PhantomData); =20 impl Request { /// Create an [`ARef`] from a [`struct request`] pointer. --=20 2.47.2 From nobody Tue Oct 7 08:17:29 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 8D6D82E7BA2; Fri, 11 Jul 2025 11:44: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=1752234244; cv=none; b=uPbcKSB4g5Q2vtjfAPeKOgWaG2u0lEk9F1F3SvcgzOkbGTdKizozP0qNrHqWSeriv8t9w31qsnflq1GmXSjGCbSdEgty7NUGzt0rZk0pkdTiJLDL89H5xjgA5Rrp7SXHXnrbCJUTmngkVkT30cKs4AEg4myb+JRnLZ0jroDh9b0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234244; c=relaxed/simple; bh=UIGqb28OhjGi0ShhnGEPp1GLyZDUYd5N5Zi5nYnQrJQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Vv3f6zKVGAjiITjbXRJmEUhAHOlZqDKSNeG+cbyGMBx/xH5XG9owXZ+bvHZgk40Rqwu45YKS9BukYZijeMGFoXWzL+B9WIjPCmFoKODF97u7Wl59KYY/G8J8ueILoGtijZDVnrsU7nxj8F0bo2pYC/2Ln7bqkUHyHQGipnhlQGM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XsDD55Rt; 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="XsDD55Rt" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6BAF9C4CEF4; Fri, 11 Jul 2025 11:44:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234244; bh=UIGqb28OhjGi0ShhnGEPp1GLyZDUYd5N5Zi5nYnQrJQ=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=XsDD55RtcGpniNH/jdop4fKpcW+Of3DRyrCx3O+1eBU2BGFytR2gT21HVhd0QsCOz RRP7JIVwyixYMx+LJ3RhKj6cbvZsd/GRC0yndK0eQAkZ1uX4wxGCYxqLpIv8WddL/+ e6wgYqezXr41Vx4v/vQ1CNdW7du/GOvrHTOgsizAJDQs4dnpivVQoBe8EBCcY6H8jq eKsZo+H7DcDqXKDMt8xEVsGSxh8X35+Ymp1Y9N/sYs8qGNLf6yP2pV741NKux/XRNg 2ywNNnHPIznvxLwu/RhDsxxfqBP0HTVjdY3aIlQtwaAOdjlcrbGEt0e9xVNNr8tQHi 4cQ1h2xK3dxsA== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:11 +0200 Subject: [PATCH v3 10/16] 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: <20250711-rnull-up-v6-16-v3-10-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=961; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=UIGqb28OhjGi0ShhnGEPp1GLyZDUYd5N5Zi5nYnQrJQ=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjTeBeWTnRhlbZskQwG+CAK8P3M4uHTzUQA6 V21L1v/JieJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD40wAKCRDhuBo+eShj dy1pD/4jn5DzyirTPZjA6UFtCeAC3oEyjUriqitwsoile9/Eez6NzTCbWWAq4KrX1zhbyKNFixh K4W679rqOpFLitXptkNZwQfVl45qnN7aBUwQk/rpx/8NEQ74HDAm2tY3Xg8GvjM4G2bxv1/Sozy YWw0t8L/SK8TLuPoWNTjSc+J1zGsSL3N0B4oACiMuZF2hgpNyHzvCqi2RbWyq/fOm7LJ+IwZYSO vurzdjPAP1jsN3o/2z487KMNM8gflvhxYueMQtvgtq7EyJps5OeuzlbUo6yxa/uqq/Z+HIVfFdo J/DOnKcXxzlbHVpkT8h+e7DzarVUcfFCCwv2F5YeOQ8AEEn9NQ8mFIqN99E3IzEPTxaNtokCOJB EM99d9FcqfavrC3c7fxgHL8C0e3ZKUa8mFS7jzluXjHsy5X62UXwFIcn3eSeSBRr3zErZSqCHtC A2c1ZgXw26BpjP2Efo6m9TnH+LaJyduTylecyHV0I3WghcX/jfhhEj2GxIAT+T+HRx6styFdPW4 98wiXaP+aGn/tOUbl/IwgxNJh2NY6gCoQzhEMRJSC+71T+lnX6UhSK4k1DIU1wLvKPKb4pDpsCn 2zz/ypS6R1aScsgaFVuaNsvyZX/RPqtHLGelkGH3lkr/40nEfg4MO2yEl00vNSlqPs0COPct6QH 7lsju5clVlzd4Tw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a few block subsystem constants to the rust `kernel::block` name space. This makes it easier to access the constants from rust code. Signed-off-by: Andreas Hindborg --- rust/kernel/block.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/rust/kernel/block.rs b/rust/kernel/block.rs index 150f710efe5b..7461adf4d7e0 100644 --- a/rust/kernel/block.rs +++ b/rust/kernel/block.rs @@ -3,3 +3,15 @@ //! Types for working with the block layer. =20 pub mod mq; + +/// Bit mask for masking out [`SECTOR_SIZE`] +pub const SECTOR_MASK: u32 =3D bindings::SECTOR_MASK; + +/// Sectors are size `1 << SECTOR_SHIFT`. +pub const SECTOR_SHIFT: u32 =3D bindings::SECTOR_SHIFT; + +/// Size of a sector. +pub const SECTOR_SIZE: u32 =3D bindings::SECTOR_SIZE; + +/// Power of two difference in size of a page and size of a sector. +pub const PAGE_SECTORS_SHIFT: u32 =3D bindings::PAGE_SECTORS_SHIFT; --=20 2.47.2 From nobody Tue Oct 7 08:17:30 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 2A22D27A44C; Fri, 11 Jul 2025 11:43:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234211; cv=none; b=rHyVQ3a9ZNKsJeUult8o4MDuEqKXqeX+jngez7CRxAsztPJZIrM0Ce/n/UWEnu0uUW358vl14eztAJ7/jwbWq/ci/S9dk5nwpufzaCudT9yxTCP7M8syLw+pXmoIUbEEeFYxgkknwt/8IYMM6BnMWgXiCVupRInblX7o3DMxppk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234211; c=relaxed/simple; bh=7kKEIeuBmyNvAHyL84OBtlsy59K5vqkobCWeseIJrWM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=stBnaOMcwYtKATLRKmYXCLfxICaByu776MPkaAgppPmaLBzfru0t2fdVXweSy0IO8j4c8H7cwRmRIbfZsyKpkopxbwnxbyF7I9oUqHG08sNtRLVqTlWUnkQpXflKvoTb0vM6R+WShgf0PrOVbXBzW+2+9PLtKtonOM/UIEQSHoQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n+EeDWUc; 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="n+EeDWUc" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5E4BCC4CEED; Fri, 11 Jul 2025 11:43:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234211; bh=7kKEIeuBmyNvAHyL84OBtlsy59K5vqkobCWeseIJrWM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=n+EeDWUciQZQU4P8LvXIE4ClDK3TJCLpfCMYgO/FB7f4fG96iAfCxiRN5iBWLfq0o podjPxZ2fGyfNN7o3eLL0AeiII9YkuImlq9F8IP0CKtfn36mDj0zrqLgNDgT/AGDyx t8fZeXyUIEb3YYFWquXgQV0GaX5D5mfvkLgBUct9GYBKSTovSyxM+CwbtKKQaXwH2X EqMeIOdK/OOU39iiO4EOX7Rh3swIMwELmL80AOKlz1PTDkakuYYQ6VRf6H/mRUNCK3 mIW33jG3+RbwsK2rOqxfSo3LqXtaCp+yUOQgSVEILLSyaHQdxqJPfj7342JL4FUjJv BydZ4QVHApDcA== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:12 +0200 Subject: [PATCH v3 11/16] 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: <20250711-rnull-up-v6-16-v3-11-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=3672; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=7kKEIeuBmyNvAHyL84OBtlsy59K5vqkobCWeseIJrWM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjUca7fbC/aea8BEVK/3llQKLK36fA9k7Xnj aNuufcwO4SJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD41AAKCRDhuBo+eShj d4D4EACzGLHqO5APIkvsBpjpjj+/xKLPQhp6RGUGwTmO6j1EXo4vIaeHJHacQe2mOcRBMeUCN+b CRsDrev79mNrw7rMAToKZLHkMuw7UBLfMBN1QllLFlxB/2dzGjkxsh8FYcSjhW1ZQ9I94TGct/R iearpkNrm3UejFWjPYlHopUnE4Z3yWuUgZ5ZDU85mAXBURcCVKkv3hHN48LAfBxPifSZu8e8Huo eX6spESu9r6BFKOKXu1pVckg5SiXDUXgmePK+K6kZ14yA+IXCxk8CIdEr5zZfUHXi4F1LKbFej1 W6K76l2C7UM3/mwCxQFV9brl+ItECvnYbRW59n6f1ZXROk2FxL4L5OPuHnUPWboP/zN7ij/bSyq CpP/6NTZoavz8eEPBE8MEGSujT8Nb2RjK40AM9HhlZgzKKf+At8A4vEuSSEtKVfnOMnjW0DkjLs k0hLzXt8N0wx7vupklyTm1aobt8zs9ek1FG1nk7wZJI4hDBOt3IzYoyV3gvchgxFrG4Q4vIXzeS I6a1+1g4stsiIuFPzeorl3agaYAkAwhOyHbM1AiTwScN+eGmYpKHp5owXtdUthWypiPgyldGWCb Py3RzeX+SEIWfOhmlL082pwJt5Iufy+dNTmU3Viq1N8YislTKvJ/upxlq6/Yb4ta5j405s70N9x thqyBlspsdROJOA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 The rust null block driver is about to gain some additional modules. Rather than pollute the current directory, move the driver to a subdirectory. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida --- 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 0c1d245bf7b8..29b14aec3559 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4246,7 +4246,7 @@ W: https://rust-for-linux.com B: https://github.com/Rust-for-Linux/linux/issues C: https://rust-for-linux.zulipchat.com/#narrow/stream/Block T: git https://github.com/Rust-for-Linux/linux.git rust-block-next -F: drivers/block/rnull.rs +F: drivers/block/rnull/ F: rust/kernel/block.rs F: rust/kernel/block/ =20 diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig index 0f70e2374e7f..6b50dbc0495b 100644 --- a/drivers/block/Kconfig +++ b/drivers/block/Kconfig @@ -17,6 +17,7 @@ menuconfig BLK_DEV if BLK_DEV =20 source "drivers/block/null_blk/Kconfig" +source "drivers/block/rnull/Kconfig" =20 config BLK_DEV_FD tristate "Normal floppy disk support" @@ -354,15 +355,6 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based VMMs (like KVM or Xen). Say Y or M. =20 -config BLK_DEV_RUST_NULL - tristate "Rust null block driver (Experimental)" - depends on RUST - help - This is the Rust implementation of the null block driver. For now it - is only a minimal stub. - - If unsure, say N. - config BLK_DEV_RBD tristate "Rados block device (RBD)" depends on INET && BLOCK diff --git a/drivers/block/Makefile b/drivers/block/Makefile index 097707aca725..aba3e93d5014 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -9,9 +9,6 @@ # needed for trace events ccflags-y +=3D -I$(src) =20 -obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull_mod.o -rnull_mod-y :=3D rnull.o - obj-$(CONFIG_MAC_FLOPPY) +=3D swim3.o obj-$(CONFIG_BLK_DEV_SWIM) +=3D swim_mod.o obj-$(CONFIG_BLK_DEV_FD) +=3D floppy.o @@ -39,6 +36,7 @@ obj-$(CONFIG_ZRAM) +=3D zram/ obj-$(CONFIG_BLK_DEV_RNBD) +=3D rnbd/ =20 obj-$(CONFIG_BLK_DEV_NULL_BLK) +=3D null_blk/ +obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull/ =20 obj-$(CONFIG_BLK_DEV_UBLK) +=3D ublk_drv.o obj-$(CONFIG_BLK_DEV_ZONED_LOOP) +=3D zloop.o diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig new file mode 100644 index 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 Tue Oct 7 08:17:30 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 E329B2EA499; Fri, 11 Jul 2025 11:44:24 +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=1752234265; cv=none; b=nSX9LslbNhOvf23JEnDEhvoMEkM5QTOCjU4IM6ykH+/I552FIKL3S5my0hORSFBdmQjzwaHAlLEP3hKn19TVx2tlgPC8mPHrZd7mLOMX0O4/XLgE4MYxlsFAK7nbBRr3nwupB9W6s2Emvk1y11fdj9is2V1h58C8oinK3cgVCrM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234265; c=relaxed/simple; bh=kNIg3hkkOzE1843fVUQVqCA9SmYe5SbYcBp0IcJb8eE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bnOEuJr73JWqj/RcMHjEw6W62aGgClDcug038/R9z+wbKk0+oEpYNElnY215EDJre352XW4ahROFHYiKsSWFfLEzja4KjAh0GTOKTay2zW9Qerth5U1RsLGRz3m7vY5DaiTO7ggtH5CPQAq4ONKxh6wpnQEzjD21Bv3fFSWEius= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=SrhJM/yE; 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="SrhJM/yE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AE335C4CEF4; Fri, 11 Jul 2025 11:44:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234264; bh=kNIg3hkkOzE1843fVUQVqCA9SmYe5SbYcBp0IcJb8eE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=SrhJM/yEE4ktjpHGsABvW/G91MYd6vxV7snrc7i9+kCqM+TnhZ9zz667iAl5JNNZi P5mC9VNWX/Dk97IceOSZY7Rs4t1+T4oeTGCiWV7bndbgYaHwVh4Ad9hmrH1it1tmqe vP4K+3qcrSSDD7lqFB/RIpaahj1LXmj5LzE6/d43l61zTz4gL1t8pRc1ToFswkqHZM MLjPTlU5hFwiJ15kgBOQOW6i8FT95dITsj+7mt22EguRq2Hd5rmakzNGsK2iTcP4p8 BLTwDbBJddX/TguSojmjYAj6N1xy3uITiNbgZmaEkDAq59dq3H+0kpQBC+diUA05T3 oP8xO5QJ5dCUw== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:13 +0200 Subject: [PATCH v3 12/16] 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: <20250711-rnull-up-v6-16-v3-12-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=10886; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=kNIg3hkkOzE1843fVUQVqCA9SmYe5SbYcBp0IcJb8eE=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjUK0FtDzMzsNcedDP78IaboOs5lpCvhB7ol 9aI1ffsl2iJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD41AAKCRDhuBo+eShj dxg9EACtZN3kH8xV1R4zdsh5hc1Im/NifutQhHvxiIWpXLi6e6P8s3RevmKZ2gmE8U+6LQIFwC3 kGq1vjsLs39qSr9za+R4z8SSGST8iqf4rBQ956hXVybJ24Stqe0Cck5W9td0mMtIna4UglCrhcn s1yMgjZVS4nUDHv4iYuXrzvD48X//mpeyJMY8ip3Oleb/w8UFQPN18aZpz/x6H1twAamK6THwOF +laaxnoi9B90cemvVyxbWrxZxnstIav7w9dbBvLK8J91VJKDR3WlRU9obCiE1Cq1ZMuDZC2kcEv wpsUL1Xis0oymntt1UBk95y6NpJaBbUnNB+eeDINIXtTjQDmDDKzpHIRyvLOqO5yW9m8qcws9Wb /gaXc05KTNqKUznccwMCKhAZCqQDdMI7t3ALdo4lWMwNWFSbksirYo4VxBXF+bLBVCIRxjss9L+ nv+XZ9iCsb4T3r9cxJnS6rnGEUrd1nxzoYtvKtO71wsb3Xr+TGM0u8pGwr9kGNBwKSnGROHMhDn xdblVkrVxEW+3bhfbhyETQ+CdYroX0FS6+k7M0kY2B+T0b08kABD4w9Bd8Iz5meQNcK1/uvL3Bz EZz2GFY5ntCK5ikaCS3laogMURlN+2TDm5XhK/JZCWktp8LgM/9mfWRIMOKNuP+CnW5cUS/sy7A FFHTiWQaFqfwFkQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow rust null block devices to be configured and instantiated via `configfs`. Signed-off-by: Andreas Hindborg --- drivers/block/rnull/Kconfig | 2 +- drivers/block/rnull/configfs.rs | 220 +++++++++++++++++++++++++++++++++++= ++++ drivers/block/rnull/rnull.rs | 58 ++++++----- rust/kernel/block/mq/gen_disk.rs | 2 +- 4 files changed, 253 insertions(+), 29 deletions(-) diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig index 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..6c0e3bbb36ec --- /dev/null +++ b/drivers/block/rnull/configfs.rs @@ -0,0 +1,220 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::{NullBlkDevice, THIS_MODULE}; +use core::fmt::Write; +use kernel::{ + block::mq::gen_disk::{GenDisk, GenDiskBuilder}, + c_str, + configfs::{self, AttributeOperations}, + configfs_attrs, new_mutex, + page::PAGE_SIZE, + prelude::*, + str::CString, + sync::Mutex, +}; +use pin_init::PinInit; + +pub(crate) fn subsystem() -> impl PinInit, Error> { + let item_type =3D configfs_attrs! { + container: configfs::Subsystem, + data: Config, + child: DeviceConfig, + attributes: [ + features: 0, + ], + }; + + kernel::configfs::Subsystem::new(c_str!("rnull"), item_type, try_pin_i= nit!(Config {})) +} + +#[pin_data] +pub(crate) struct Config {} + +#[vtable] +impl AttributeOperations<0> for Config { + type Data =3D Config; + + fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::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_fmt(fmt!("1\n"))?; + } else { + writer.write_fmt(fmt!("0\n"))?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + let power_op: bool =3D core::str::from_utf8(page)? + .trim() + .parse::() + .map_err(|_| kernel::error::code::EINVAL)? + !=3D 0; + + let mut guard =3D this.data.lock(); + + if !guard.powered && power_op { + guard.disk =3D Some(NullBlkDevice::new( + &guard.name, + guard.block_size, + guard.rotational, + guard.capacity_mib, + )?); + guard.powered =3D true; + } else if guard.powered && !power_op { + drop(guard.disk.take()); + guard.powered =3D false; + } + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<1> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::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(|_| kernel::error::code::EINVAL)?; + + GenDiskBuilder::validate_block_size(value)?; + this.data.lock().block_size =3D value; + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<2> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + + if this.data.lock().rotational { + writer.write_fmt(fmt!("1\n"))?; + } else { + writer.write_fmt(fmt!("0\n"))?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + this.data.lock().rotational =3D core::str::from_utf8(page)? + .trim() + .parse::() + .map_err(|_| kernel::error::code::EINVAL)? + !=3D 0; + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<3> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::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(|_| kernel::error::code::EINVAL)?; + + this.data.lock().capacity_mib =3D value; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 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 39be2a31337f..7ab049ec591b 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -50,7 +50,7 @@ pub fn rotational(mut self, rotational: bool) -> Self { =20 /// Validate block size by verifying that it is between 512 and `PAGE_= SIZE`, /// and that it is a power of two. - fn validate_block_size(size: u32) -> Result { + pub fn validate_block_size(size: u32) -> Result { if !(512..=3Dbindings::PAGE_SIZE as u32).contains(&size) || !size.= is_power_of_two() { Err(error::code::EINVAL) } else { --=20 2.47.2 From nobody Tue Oct 7 08:17:30 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 3F9022EA463; Fri, 11 Jul 2025 11:44: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=1752234261; cv=none; b=X3MADSiFNM3+yppTpV0Bkihf8N4ek6tXsxdJMo+C2liPnfCCrss8wE6tgZktiZJy/PDKuNKbwEB0Pq3xYjMoLmLPWLWa7GIaVHVtj8hU8wiGQFXECy/meNNTZP6205jzixWJBoOInK9CXXKnsNnp8sJeVuBghXVLNzPwmC5184g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234261; c=relaxed/simple; bh=8EIFORhiI91k51v/6Z1Q3xAZrwAWnbDgqdU03+cKtQI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=iVM5WpG2oFz48C8jZgCVzgYCbb/ymjSAz57w1RbH7IHn7C2+8jUfKTA/DN9kIrWLGt8F6dLVOwrhbJVbNj4YzSDtDOq7Y42gxQzMowY7CqexJ18hczxRVv1rlD+WF1DhRJhiuGIA9M0mniJVg//R+Hi4fYneeiiejgttuvPZgFs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GRTR66LG; 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="GRTR66LG" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 66C3BC4CEF4; Fri, 11 Jul 2025 11:44:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234261; bh=8EIFORhiI91k51v/6Z1Q3xAZrwAWnbDgqdU03+cKtQI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=GRTR66LGeIw1pQOogW+oWfnhyELRcFY+yjk7HUDxGdg/MKeDxTZvdgYD7IubnwCgI VWzL164fr3lI2W5+q4n3F+lo7MRFCpuV/XGkEgf9LnsTSlw4JPfySv+J9Wi6ILIIVY pfwGef1SbSM2oq7N4qKyjE+koawP6zbkOimRXzRz2NX9rU5DCFWe5BKDY2VDydsWNi OkjBH7XHDpKFyILZRoTmwLFKRL03edsX7236U0JXJZ8L1E1jFWwhBriLlnQ2tPTAoI g3R3n5rPIDGEhXGdTlhsCVm+nQMkbxrtvH2/ykAPhGTkUr36PMnbCCSzzt39lTF6uE HvDKBrZbyHrQg== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:14 +0200 Subject: [PATCH v3 13/16] 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: <20250711-rnull-up-v6-16-v3-13-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=10555; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=8EIFORhiI91k51v/6Z1Q3xAZrwAWnbDgqdU03+cKtQI=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjVt9ul3aKi8XBriFTHyx6qC0mFonSLdusJW IMtNoUaikKJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD41QAKCRDhuBo+eShj d5tBD/0UoubIR3NJ4xd2RrvxL2tZIKTKSQpq/X6fbRiYTOLR6zLJrM51HQXBbnxVTkwOBy1rS4m HJ89jeYaq/2r5PYPerMPwPumPQnVqfVrt4TkgZNq+AJhOK44C9eui9h45b4PNR0bJKn9hfAtSDM rf5kECy9w9yS+T1kDjSw0tPSq1BdfKegvaJmxsAv2ckODxy2EKibCg/qMiLO1MjpNkfHym1ueq6 j9N0lDbnm7nyAzNeonphKLyolbUTBwpR3/5UgywOOKWD2KuYoASsjZwpeFjAUErnx2mR+f2ryMh itkcHoQUFSCujnMu2jIPYfFq8IhHN47Lvn0oLUjUc8oscrx0FAdGFr0CNlxSGAWiWHZPaCjkoy0 a7FnZyXKQoK0SnXCPARdShKpY0YTOsFn+oU19eRpxsplfxFUiBTzzITSqQw2F62+kmkI6DzLIs/ ivQVtQ8YDRyV1lBHKE2URaQ9wjSrFODyjEUQCUTpfMkJMdQcBRKQa68IMkTAMaGDJ1CQDXVOe83 GrfH4EVqdy4wLUt67E0tdisBM55s77SLxmhpAljpvmfSxcWIpTNAkGgLlv0Dzg/aJKQJG+KF4Uh qShRCdQlHDbXDd1QK1YB37PO8nrPB1vOb7XT/0HbfFaEFSIRAqu/FHW4vGQS6LBEojdF7T/HIjw NPPzNcCGr3QRKlg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow users of the rust block device driver API to install private data in the `GenDisk` structure. Signed-off-by: Andreas Hindborg --- drivers/block/rnull/rnull.rs | 8 ++++--- rust/kernel/block/mq.rs | 7 +++--- rust/kernel/block/mq/gen_disk.rs | 32 ++++++++++++++++++++++---- rust/kernel/block/mq/operations.rs | 46 ++++++++++++++++++++++++++++++----= ---- 4 files changed, 74 insertions(+), 19 deletions(-) diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 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 faa3ccb5a49a..34b7425fa94d 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 7ab049ec591b..954bc64cfa59 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -12,6 +12,7 @@ static_lock_class, str::NullTerminatedFormatter, sync::Arc, + types::{ForeignOwnable, ScopeGuard}, }; use core::fmt::{self, Write}; =20 @@ -97,7 +98,14 @@ pub fn build( self, name: fmt::Arguments<'_>, tagset: Arc>, + queue_data: T::QueueData, ) -> Result> { + let data =3D queue_data.into_foreign(); + let recover_data =3D ScopeGuard::new(|| { + // SAFETY: T::QueueData was created by the call to `into_forei= gn()` above + unsafe { T::QueueData::from_foreign(data) }; + }); + // SAFETY: `bindings::queue_limits` contain only fields that are v= alid when zeroed. let mut lim: bindings::queue_limits =3D unsafe { core::mem::zeroed= () }; =20 @@ -112,7 +120,7 @@ pub fn build( bindings::__blk_mq_alloc_disk( tagset.raw_tag_set(), &mut lim, - core::ptr::null_mut(), + data.cast(), static_lock_class!().as_ptr(), ) })?; @@ -166,8 +174,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, @@ -179,9 +191,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, @@ -193,9 +206,20 @@ unsafe impl Send for GenDisk = {} =20 impl Drop for GenDisk { fn drop(&mut self) { + // SAFETY: By type invariant of `Self`, `self.gendisk` points to a= valid + // and initialized instance of `struct gendisk`, and, `queuedata` = was + // initialized with the result of a call to + // `ForeignOwnable::into_foreign`. + let queue_data =3D unsafe { (*(*self.gendisk).queue).queuedata }; + // SAFETY: By type invariant, `self.gendisk` points to a valid and // initialized instance of `struct gendisk`, and it was previously= added // to the VFS. unsafe { bindings::del_gendisk(self.gendisk) }; + + // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build= ` with + // a call to `ForeignOwnable::into_foreign` to create `queuedata`. + // `ForeignOwnable::from_foreign` is only called here. + let _queue_data =3D unsafe { T::QueueData::from_foreign(queue_data= .cast()) }; } } diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index 864ff379dc91..c50959d5517b 100644 --- a/rust/kernel/block/mq/operations.rs +++ b/rust/kernel/block/mq/operations.rs @@ -6,14 +6,15 @@ =20 use crate::{ bindings, - block::mq::request::RequestDataWrapper, - block::mq::Request, + block::mq::{request::RequestDataWrapper, Request}, error::{from_result, Result}, prelude::*, - types::ARef, + types::{ARef, ForeignOwnable}, }; use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ord= ering}; =20 +type ForeignBorrowed<'a, T> =3D ::Borrowed<'a>; + /// Implement this trait to interface blk-mq as block devices. /// /// To implement a block device driver, implement this trait as described = in the @@ -26,12 +27,20 @@ /// [module level documentation]: kernel::block::mq #[macros::vtable] pub trait Operations: Sized { + /// Data associated with the `struct request_queue` that is allocated = for + /// the `GenDisk` associated with this `Operations` implementation. + type QueueData: ForeignOwnable; + /// Called by the kernel to queue a request with the driver. If `is_la= st` is /// `false`, the driver is allowed to defer committing the request. - fn queue_rq(rq: ARef>, is_last: bool) -> Result; + fn queue_rq( + queue_data: ForeignBorrowed<'_, Self::QueueData>, + rq: ARef>, + is_last: bool, + ) -> Result; =20 /// Called by the kernel to indicate that queued requests should be su= bmitted. - fn commit_rqs(); + fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); =20 /// Called by the kernel to poll the device for completed requests. On= ly /// used for poll queues. @@ -70,7 +79,7 @@ impl OperationsVTable { /// promise to not access the request until the driver calls /// `bindings::blk_mq_end_request` for the request. unsafe extern "C" fn queue_rq_callback( - _hctx: *mut bindings::blk_mq_hw_ctx, + hctx: *mut bindings::blk_mq_hw_ctx, bd: *const bindings::blk_mq_queue_data, ) -> bindings::blk_status_t { // SAFETY: `bd.rq` is valid as required by the safety requirement = for @@ -88,10 +97,20 @@ impl OperationsVTable { // reference counted by `ARef` until then. let rq =3D unsafe { Request::aref_from_raw((*bd).rq) }; =20 + // SAFETY: `hctx` is valid as required by this function. + let queue_data =3D unsafe { (*(*hctx).queue).queuedata }; + + // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` w= ith a + // call to `ForeignOwnable::into_pointer()` to create `queuedata`. + // `ForeignOwnable::from_foreign()` is only called when the tagset= is + // dropped, which happens after we are dropped. + let queue_data =3D unsafe { T::QueueData::borrow(queue_data.cast()= ) }; + // SAFETY: We have exclusive access and we just set the refcount a= bove. unsafe { Request::start_unchecked(&rq) }; =20 let ret =3D T::queue_rq( + queue_data, rq, // SAFETY: `bd` is valid as required by the safety requirement= for // this function. @@ -110,9 +129,18 @@ impl OperationsVTable { /// /// # Safety /// - /// This function may only be called by blk-mq C infrastructure. - unsafe extern "C" fn commit_rqs_callback(_hctx: *mut bindings::blk_mq_= hw_ctx) { - T::commit_rqs() + /// This function may only be called by blk-mq C infrastructure. The c= aller + /// must ensure that `hctx` is valid. + unsafe extern "C" fn commit_rqs_callback(hctx: *mut bindings::blk_mq_h= w_ctx) { + // SAFETY: `hctx` is valid as required by this function. + let queue_data =3D unsafe { (*(*hctx).queue).queuedata }; + + // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` w= ith a + // call to `ForeignOwnable::into_pointer()` to create `queuedata`. + // `ForeignOwnable::from_foreign()` is only called when the tagset= is + // dropped, which happens after we are dropped. + let queue_data =3D unsafe { T::QueueData::borrow(queue_data.cast()= ) }; + T::commit_rqs(queue_data) } =20 /// This function is called by the C kernel. It is not currently --=20 2.47.2 From nobody Tue Oct 7 08:17:30 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 039DC2E7BC9; Fri, 11 Jul 2025 11:44:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234248; cv=none; b=osqE5VN6EvQjYO7AQSgA4H74BSRyk9HjG2NH3WwL0uC0Mn5FW124pBKcLBeC+y19njT+lrB3YNTJ72Kum8yF4TlxlaUmE/MLlCSFA5sNghCOlw5+YOJ1qvaOwsXbJBR+HhyhBwWq2rsa35F9drCx7ouVFYPcVA+NtaoQ9ITwJ9o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234248; c=relaxed/simple; bh=bwWpprsdtyVDZaqvZNUcUvzzn3+xorlxFgD0pWYxMCs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hXtDurlbxOzqLGVKHKB7SB7qsCx0GFN1+ZaHC6VTQ+AJo7/TSvk1KTUXgI2LPCy3Rj4Su41ajq7FUn/1H5hAmsuqRZl97kFM4JCMIPQD8XJQmEQg6u5G0CdVHRV8iLAe0cQd/9GzZ0sSXtHcDd9zTTfWJ2DOmNqazFkFhBRNac8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BtIRTN5W; 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="BtIRTN5W" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 15DC8C4CEED; Fri, 11 Jul 2025 11:44:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234247; bh=bwWpprsdtyVDZaqvZNUcUvzzn3+xorlxFgD0pWYxMCs=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BtIRTN5WRRlkK4GdCX+LH6geTHzmIpPeDEwLD5X9VQhZMMoPzG3N4D7cpggtt7MtK yFW2QG8Ov0zFg1jrXuDIacY4IkRpK0DHYPkEsmSmUl+WwPDzS2WiFCWznP5rbyDB9p JhUBPbum51dwk37gAGihntlbDtU3Yvby/6D+ypj1hpKtTjHs14OmARc7afKZMx4hEJ i55/03brw07Kr+gftmsDKd/MwnDQR0xoj/xkKlxKD/B8JAGnqWDX0Oae/aOoSEGnSX F0axrhS4VwAOpvOQ8cVPquSzIzB1C3akAyopikIu3JgGu1cEXRm4Af6w9nxtOGgr3y c3vDRrCZdb1TQ== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:15 +0200 Subject: [PATCH v3 14/16] 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: <20250711-rnull-up-v6-16-v3-14-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=928; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=bwWpprsdtyVDZaqvZNUcUvzzn3+xorlxFgD0pWYxMCs=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjWCKRMbAOxHydWnAXkvhwvFbZyeFFvgYNEL 4bmKes5mWiJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD41gAKCRDhuBo+eShj d4GKEAC0Ryfui49XYVyNu2nBXF0JD3WMkvbaj+vgsxuk5Uqf4zERuN76BaLLX/qdPJyF11K+DS3 HDXfl9B7X9PwBbE+VfJcUuSJhSBmEG1Ln80/+j009quKHDX05//qzVzItMJIqYF7Ju4hyBBmHGn qxDdWVeDHp5vDXnwT8lsSyP9kvNacZZKOed8hiJIZd8bTCZ8+Jh5YSUd6rI8Vf2rVM4uN5JO4+W MC43+2ZxubjFbTaU3MMeoXx9pFJ4f4n6waI8Z+9es3gFW3mXgsFvOmE9DHngxsCiLs2LJ4h8kSL ep3Ak00Lw/0wDiXMRyVhvBKW+ana1C6ar5PbJeJ6dhe+jxgsAZ6hqBCrJ4UY10FsUY44hNsAYRu DPDk9FEPZrY938GN0HkW/2tswymvgQXyxPGC3DWnnphG6Qfo/YFz1cD7avFn6WmTo7Ovr1Cez3m 83f5U8n8ZFtuF3RkzAzmpYv/y5Hc/y/4mrzBlqUTJybp6T44DoRUwe8C0+LMmTfOpMKd/GEmbZk kvJktevrzCoTnBYUsWF85P0R19/43oVxHNK0Hnnk2ppsfUfPBHmK7ow/6jaTzsL3w2gqmg9U9b0 kogFdsaSdXVsKhxzwIYwjw/yLvyyTSoaMuYPlM+fPQ+OAlT9ldoZilhMG9Fw4mgza4dnmnmgI6X DoQE8cWsEjnz9lA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add code block quotes to a safety comment. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- rust/kernel/block/mq/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index 2d14a6261a31..873d00db74dd 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -143,7 +143,7 @@ pub(crate) unsafe fn wrapper_ptr(this: *mut Self) -> No= nNull // valid allocation. let wrapper_ptr =3D unsafe { bindings::blk_mq_rq_to_pdu(request_ptr).cast::() }; - // SAFETY: By C API contract, wrapper_ptr points to a valid alloca= tion + // SAFETY: By C API contract, `wrapper_ptr` points to a valid allo= cation // and is not null. unsafe { NonNull::new_unchecked(wrapper_ptr) } } --=20 2.47.2 From nobody Tue Oct 7 08:17:30 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 249172DE6E6; Fri, 11 Jul 2025 11:43:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234221; cv=none; b=O1MiwIW+m7O91USJFPxpaOg62A2c+tBJ5qwDF4rzFfTF1weed/+hfzOeDV8qa3sa2VAgp15EOncjGQeTr1akAd214jNCX3u4tpU2EC3x6mkm69HBSNuBPqIpsQaeA8ONaJxLtKnMar+d3Fz/B6cBsQnoFLpWavsoTFwOnGgEo7s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234221; c=relaxed/simple; bh=LSx1ItJzMBA57/EBhXrxJqKwSb/byhu1bF1rbuvWUgY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=n0awLvZa2teugz/MR/zRKM9QQeKFoK/UqvZLz9vHdplqlDfJaza3uGSyViF8hSrhMsG9Wj96N+g2rhFC3YEl1MVGl0M6oUdZLAh15x0vw3u3h4kcr1VPCacgA0ODIbyO1J/I/YaM1DWD/EElDjZAkghLWXNNYmOFztqdQnepLAM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LrR7ni1Y; 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="LrR7ni1Y" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4F5EEC4CEED; Fri, 11 Jul 2025 11:43:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234221; bh=LSx1ItJzMBA57/EBhXrxJqKwSb/byhu1bF1rbuvWUgY=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=LrR7ni1YPSs0r3RkVSo17/sicYi83TfoU7GHxB4EWODIP0rV+EbT+/uAuefgSKZUK UaAruYDXt7t1q2uIKxmDQ12UumUX2eL5DcB+dcb3iugJuuYx0AJOACjmzqTJVA9vXb o8kZ7rpWK4gNheUwc9YCsjNYoLPSfgzSYCW2rNqGnX7uxdOTEAALcE33raTSaStq9d fheV6pI3040MyLD6seCVNQTKPQZ+b/2GjE+Yw3/s1/ZGA2DXDxm6pP1eQBmgiwfw6S 7i2yRKH2Hkw4v+gQqqUgvQTLfbRWPLDvYgJzVMDbpOrEvVTN6zFxJT66SrFBkW5obw +OW/GF1ZIF4gg== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:16 +0200 Subject: [PATCH v3 15/16] 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: <20250711-rnull-up-v6-16-v3-15-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=4988; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=LSx1ItJzMBA57/EBhXrxJqKwSb/byhu1bF1rbuvWUgY=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjXjJ5h+X/52bpiq+g1p0EkDmulUlDzWVwK+ Hqe0mke+Z+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD41wAKCRDhuBo+eShj d/N6D/9n2oK3tRZ+5im8UulYawUc/j2qGQgm5MhJ1gGksKmQfI5DmBJxd3rryoC2C4xXqExELO4 6KXrk5yOIrLBjS/0QTO8SNt9UlxmP97I+IuKcASEoLnf/a7NmXq2mFrpU/fiQVQQHn0W7vOTle6 CsrcjzK7Gj4ZsjWrcjBpAeFpiAWAP3n1i/jr9CwkF6hV3p+7QlzeGU32CgbbfPqrrIHzd695Jwj rEJiJKG91udigrzconxABqna/n2kdXCfD4ZIkOOwIiUOjafhh/V5aGauHXq0JO1AZxZLhYI6oTw 9rguHAyFU8rIG5aPrjiUcmLS6cFbG1Vq8V/su+3PMdH8ZCjdKhmLPBpEHcu5bVo610TwsrQlKxZ 08wlnKeAAwfMikag0T6KE1LMc8UL7GX/xWEEr4sBe3zvc/HAVduD2iHT6Pc3zpztQ4IBrSiIkFF Rs18tjkmmFHN3HWqiEGKpv8FckqX5WWNUO/rhP6GyIqdAPRCbZGxLugf+qUHR5pK28XXSSxtPO9 cTdHfaVQ1wlqREECASoZD+6+VudZTtoghp0fKHlSOOFbDsFhk0faIja+aXVfnudg9G2K47YaOYB 0nPTotxoA97GR0j4yJIfCLDGef0NEaFgLTmRCa4bZnv8Dl8ChUtpkVFKhUKTp4ssUW3M0cDnhFi 1SonFjAD6S4ryoA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow users of rust block device driver API to schedule completion of requests via `blk_mq_complete_request_remote`. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- 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 34b7425fa94d..551ef38efea2 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 c50959d5517b..b6b26cebd4f5 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 873d00db74dd..244578a802ce 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -130,6 +130,23 @@ pub fn end_ok(this: ARef) -> Result<(), ARef> { Ok(()) } =20 + /// Complete the request by scheduling `Operations::complete` for + /// execution. + /// + /// The function may be scheduled locally, via SoftIRQ or remotely via= IPMI. + /// See `blk_mq_complete_request_remote` in [`blk-mq.c`] for details. + /// + /// [`blk-mq.c`]: srctree/block/blk-mq.c + pub fn complete(this: ARef) { + let ptr =3D ARef::into_raw(this).cast::().as_pt= r(); + // SAFETY: By type invariant, `self.0` is a valid `struct request` + if !unsafe { bindings::blk_mq_complete_request_remote(ptr) } { + // SAFETY: We released a refcount above that we can reclaim he= re. + let this =3D unsafe { Request::aref_from_raw(ptr) }; + T::complete(this); + } + } + /// Return a pointer to the [`RequestDataWrapper`] stored in the priva= te area /// of the request structure. /// --=20 2.47.2 From nobody Tue Oct 7 08:17:30 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 E4C642E9EBF; Fri, 11 Jul 2025 11:44:17 +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=1752234258; cv=none; b=uFdXPqJxTPmp6KMSgembinvBOUkXS9iYT+Qp7e+H9If4E3YEnRHvklvc4RF2QZAHna8hGsA1paLawa/uzq/7mgJ2ayzppAk+MpPDLdPZNMCyI08WcyMqyTxSkKRqpT4KwWW2eXHEgek0T0V2/YUz+HSxjkzvcE+QP1wdXWhmCyY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1752234258; c=relaxed/simple; bh=dCspNMLPwGgCi5Y2yRvx3utl23R9AkFozzldxElpP+4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QIatbUUjX0z+hABWt/5CspjnlpoQdvGDzJnlIIxqBvAwqYGeUDtRllJe7f9xaspYWeIaOAT62DLi9sy5jsncVM2wCF4x+c0h+6i+iaJKcPBlRsmMZH2bqG7aZduIGeHRgrihZY5VF5BNgMoLwXRoOOOLqkYGM46DFMLqr5uSOxI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FtU4tTZQ; 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="FtU4tTZQ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 255E1C4CEED; Fri, 11 Jul 2025 11:44:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752234257; bh=dCspNMLPwGgCi5Y2yRvx3utl23R9AkFozzldxElpP+4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=FtU4tTZQZ49iRTOJw6oEdBBQjILjkMdmt3cm1gTsaTI8rzOE4mbp2UsuoMc4kxpDW jYAn4Ftvja/Za+yf2a/Pq3nrAHnaFkqi94N/gOkR0lIj3MDZAyjA8PSmFD/omKO+vx jhnTAdRymK+Mw05X0vEVw/3WLVKWsYhsPSjZS2el+0ndus/4fqtfDcGZQzBj/5iT4h O7CAZCIhdr7PKJEXXQo/Eskj2qSynAXISx0Xgzj0DyhKrdvEnyzxBsexsonMBD5eRq 5HjlWA24vsvbuaZAJLMIWbQ/TfpCywbCvHb6f1W7gI6JZKxhBAGafNd2vtFxHLYIzl DMT4Yse40RgTg== From: Andreas Hindborg Date: Fri, 11 Jul 2025 13:43:17 +0200 Subject: [PATCH v3 16/16] 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: <20250711-rnull-up-v6-16-v3-16-3a262b4e2921@kernel.org> References: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@kernel.org> In-Reply-To: <20250711-rnull-up-v6-16-v3-0-3a262b4e2921@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=6413; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=dCspNMLPwGgCi5Y2yRvx3utl23R9AkFozzldxElpP+4=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBocPjYZ8B6U4335YMpCCgIcK016VDnpRFmdpT3X uX49mJpD/iJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaHD42AAKCRDhuBo+eShj d1LvEACY7AU7V2ksm5sArjmFRpzTEsq9CrgiYgZRYVZJI7a4CiDypBQhpZxhKMWb1uvpjeDNB74 iYXqHWhRbu5pfPjhrzxGPWCBAEpec8+LTtqHZ0XgnhztvsSLcD9iSXL6tvGEQDWnwhK2svhG0Wy zr1LDbtZ2nbShMUeKrTzgx4bTXdalkXzeb171XN/3WewZf1KiIUIm/iH9hDWUtoqgDSz/uoX7Vp 9hytxfXq/+/kfKsvZqzlOyTALo+0jEwOej5uUdHC44CskZEtuj5JvdEx3/kGva5B/s25u9kQNIL oOfNAevwuIlUjweGeR4EfQw8pcqzfcyE2PWmeXmzr4OdQMdUgeZDGA1nfmTwXfseuFkwkTCKVNA 7FJRLWqxgNTxc0zvi4XxZiwO2lDSNZJtAsIXA0d8h4XFzRp2qNzBLI2TrG6A4F8e+tUzpu6nor0 qUJvfAA9a5gwPZNCkR1PfnglbmD6aqQxO7ylR3cBtC+cWieejHktZHPv5na5UqEFYSk81Wc782U zRl+TV52tKgkf+QLpEBe351HM3s7bRR8hZQA1kASbxuOou6zriGP9aNTv5ln5+1Udz5AvS8CT/I 1kkpAC94LaGn8J6wqjPNByIzXZjqKQ5RuSnkRB1/ck8YBEa/UWsubIgkJdwdmkeVEB2Sw72X7Q6 lvBEKpBUfojYAEw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 rnull currently only supports direct completion. Add option for completing requests across CPU nodes via soft IRQ or IPI. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- drivers/block/rnull/configfs.rs | 61 +++++++++++++++++++++++++++++++++++++= ++-- drivers/block/rnull/rnull.rs | 32 +++++++++++++-------- 2 files changed, 80 insertions(+), 13 deletions(-) diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs= .rs index 6c0e3bbb36ec..3ae84dfc8d62 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(kernel::error::code::EINVAL), + } + } +} + +impl Display for IRQMode { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + Self::None =3D> f.write_str("0")?, + Self::Soft =3D> f.write_str("1")?, + } + Ok(()) + } +} + #[pin_data] pub(crate) struct DeviceConfig { #[pin] @@ -92,6 +122,7 @@ struct DeviceConfigInner { block_size: u32, rotational: bool, capacity_mib: u64, + irq_mode: IRQMode, disk: Option>, } =20 @@ -126,6 +157,7 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { guard.block_size, guard.rotational, guard.capacity_mib, + guard.irq_mode, )?); guard.powered =3D true; } else if guard.powered && !power_op { @@ -218,3 +250,28 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { Ok(()) } } + +#[vtable] +impl configfs::AttributeOperations<4> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::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(|_| kernel::error::code::EINVAL)?; + + this.data.lock().irq_mode =3D IRQMode::try_from(value)?; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 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