From nobody Fri Oct 3 23:02:53 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 D449E2FD7A6; Fri, 22 Aug 2025 12:15:32 +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=1755864933; cv=none; b=DikjWHK4MlRhIhN3pQVY3Z9jPbf8Ba1EZFAJZ1lspi8HvyI48nMC5qnmciqgVh3SUPitjExz5yAOHItBxBt1reO0Ebp7ghgSh2WN0c56LzBq43ye2wZ2ZtON3n7NP9t2ad9PuRa+qhYX2YV5ByhXd6b0hOjD2nqAVOBokQ2iAh0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864933; c=relaxed/simple; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ES1ykIrMLpVV5UKUxGbKbNKCM3WIQwbuXwWr61cQ5bv5u1QtaaHoodbSapKGPaPeE120nBAxdDTzHQuwLTUQwh8uxXmjEMmvX0HWD6z53mYiK+a18Odrgkoj0/BZ2f+MqngPkgW4SPfNL7Q6kwLHbAhTTmgjUiAQTk8Cj2NJ3OQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YBWTYKQo; 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="YBWTYKQo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 30B2EC4AF09; Fri, 22 Aug 2025 12:15:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864931; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=YBWTYKQoenj0/Ay3VOigpzS9rvs1/HiznpaF0fi/5X/prxjG5Tb7ef4S+oxBWtc9r 5NTLaydzcZOf8QSBMhNoIp885rD5UWmxt6nfG9C2/Y5uV015kr4jqeSoYfA7TPR3JH IlcOb1Y0HRkuCrWRLu05hz4nGIp68aig45DPLRzS92tQ7Y6CIyjUBr8DDjgHnkNfnk RV8rdTLqL0EoLfegEd8t5iMnLZHEPl1Wh07CEEJFhsmaChcnjBi2FHi+45QSA144oh oyRu6WRVoAQuXq35DqMGw3+klYEA5dx30ZZy6pYxRyYX/JQ7H/5PU2iSkhvL0S0Ygy RGPhb5q9DGahQ== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:37 +0200 Subject: [PATCH v6 01/18] rust: str: normalize imports in `str.rs` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-1-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=871; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=WE0oYMKVocpeSumFth9LF4yM1uvggXOmT17lbIpdQMg=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF8yQBXW+JFCudmtBmoZUtf2BkqMp1/Om1gIZ E47ujvZOVqJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfMgAKCRDhuBo+eShj d1tmD/4j5/ZDzjuBD6KFdFM2l9aVRVQysnhE30axIgf4tlhXuvirDhm5DoS5rA0SobhehVSFv23 GgkK+fTI7ZCyP2Uj+91lnRq3bbezPZIJANTB3aiXY1zfiRuRViNVdwRSVKQWAQcZ1nPYJsL//KX uJ9o7xD2BU7D0lnWytAIrT6AKm+UNTlZoGQ4h2gHCNDudtBWIDXo8GpyUn83yZPedKSQyyErOsb ZzLo0Iv97f5ZCcF5SlPO9Z9uHUaN4zimLF/+5685bIoPcGtkxRp9gXy7Ix7cMhNmgboYwKFGjSO yN9vUYy5Lj0w09E+T2slkLeBKAwOIGuSY2LTrmHjoFoMvdZfU/nOuWx1rBEIHT8NMjBIOd2olvI h9EKGh2vwcKwXQqbSQW1IHIwik2J1+0r1db16oxIadxzmEopm0RwTyrR4svQ2cOMJg1sbcUShOV iKEuamFDJH7wI6JnoT5GabhiNG87m5pprFGgVLKi18yMToHwwYrfl+b+G6JwPjNCN421FVQ7EcH M9G/gbE2NuMqQ58YsjFJK1djpFWqZ7ooNqR8iZ39LtOXmxiUX7fZ8UEYgmeI6jYExy/Hq5dAyOt QXH4YkuFO3/I4lXwOyOUi8r1noECuZNxTf+mKf6rW8+KXXl754E0UWLa42E+gKtcBMD3WI5hQfS Pw03ZI2x7hH5B6w== 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 | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 6c892550c0ba..082790b7a621 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -2,12 +2,13 @@ =20 //! String representations. =20 -use crate::alloc::{flags::*, AllocError, KVec}; -use crate::fmt::{self, Write}; +use crate::{ + alloc::{flags::*, AllocError, KVec}, + fmt::{self, Write}, + prelude::*, +}; use core::ops::{self, Deref, DerefMut, Index}; =20 -use crate::prelude::*; - /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] pub struct BStr([u8]); --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 B1DA42FC03C; Fri, 22 Aug 2025 12:15:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864918; cv=none; b=SEg8WoIiK5WokLf/hAcIYIEo2JJZ85x6Mno34sBqkWUxuZ93ZQIAhJDr7KQZSSfKTXzPeZbhfPjoDCjjrpm1+ocO5BySaCKK8WRAT/RdA6mdmbZ+if7N93VyX3YMezP2Q2VxvTk2in7PxDB2gwfis4RQx9RUmkw0MTi/b1oLMf4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864918; c=relaxed/simple; bh=FkVup2wcuL8c9tJJq2cNTZw4TsjvE38QLG8Ll4ELGPI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=dO5aNsSxr3eWHd3A2BDVGWR+PtjRsSe01CVGbKhLZSyfToveyR3KhgcwCo/60Gao/52jwypbZyrv9VbqV75GcjMmRReE4gtcOZxk+6eveDmLpUbEbyQ9WinE+3sjOCP2gEX5aHVv6B06uSQizrXNja2/eFY+T/ZhyHqzRAktOg0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YdX5lPCb; 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="YdX5lPCb" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 69583C4CEED; Fri, 22 Aug 2025 12:15:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864918; bh=FkVup2wcuL8c9tJJq2cNTZw4TsjvE38QLG8Ll4ELGPI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=YdX5lPCb/yI3Y1wC+QWIW6K7Lx/o0oQ2TJZtr/21ijWoMz26dP/IKX6iwLLjVqjH3 esFsQjyfUYTfTmh+WEvvyjlHMn94bzI8vDph7BBszSASy9r7fIh5XoDFWSvWnwIm2M fsuS2Qhk6BpSgW9zqckcvCSw1jBwiwDXnrSbdHC7rMSqrziPugTP8qkpGbSvm/PW7I AC4ase2hxTuiGu+pxMn+K1aH1Pu7CeEEqatiULmRfDPZbMJJPADVBgflhaSnGI+bP9 aa9ouP7fMoPM514IBpZ0rPHEpH7rZhecTgBujNXMxxjXDWFEey9jjEiubOz1yQLW8+ zKlPt0DqS7Epw== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:38 +0200 Subject: [PATCH v6 02/18] rust: str: allow `str::Formatter` to format into `&mut [u8]`. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-2-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2330; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=FkVup2wcuL8c9tJJq2cNTZw4TsjvE38QLG8Ll4ELGPI=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF8zhIn+GVKRFOTsaKYZiWBbPIfzc7f0NsPbt s7TiC8R71uJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfMwAKCRDhuBo+eShj dzerD/wNMRoN7oTltvty/HcxYgoiej+j/Yo8g6zwoAFfnPaxTMhVZW6zIpLpKSKUeoWVDQrCsT7 k/LruEV7vzKUykXHsXICE8ZIa5uYGfqizepqwYB/tT0GQb4xJ+X5O/2M3ODDzWXsu5+8kJ5KE4g 5JKuhnaMovBR5Kahbsj84AIdnA/WVfLzYkBu6mZoWTsFV0FThVYH03irwRTCtSckVOffQD62ygs 3Ba1yvEY+2C9I2RtpgSEbupcZ0GlqJ0l1ghaXHBvslE1yFPHvYxuA6GK/wYyXPzSlBJTNASmhgT gbdbKAwAmDXRfxbqomDl+lqFHlWw2QFxRD7ZiRWSo4AJlI1s1Kk2N9KZAsDhMWlM07RSL+4L9cB TNqKqNkLWjnYif2U8/csmxQUm+8K67BeTx0US6HigxT3aOmWjvGAWWfJqrFsKgR3CON8RBNiqze czxCpQmEGV1CoS/zY/pQvPHbYKnxk2Q0MP38ll5bxNCaKgfBXWaIF3pCIjYaAg/aOuct2FibFFq R3xyxUC8PS0pkTNClLTE6mF6uoKCOwLu43J22UXOLnmlC/j6xOiZJjGaCjq8CUU+eapJGNmVAGW wPErnSBddDw9uEucsd+mTGTS+nmk9e90NlUwC8tG6lViw+Os6dd7y7JezF/vnusCC2IRAFO9vXz i3sNZRHy3KKCn/g== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Improve `Formatter` so that it can write to an array or slice buffer. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- rust/kernel/str.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 082790b7a621..76632da357a6 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -7,7 +7,10 @@ fmt::{self, Write}, prelude::*, }; -use core::ops::{self, Deref, DerefMut, Index}; +use core::{ + marker::PhantomData, + ops::{self, Deref, DerefMut, Index}, +}; =20 /// Byte string without UTF-8 validity guarantee. #[repr(transparent)] @@ -825,9 +828,9 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// Fails if callers attempt to write more than will fit in the buffer. -pub(crate) struct Formatter(RawFormatter); +pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); =20 -impl Formatter { +impl Formatter<'_> { /// Creates a new instance of [`Formatter`] with the given buffer. /// /// # Safety @@ -836,11 +839,19 @@ impl Formatter { /// for the lifetime of the returned [`Formatter`]. pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usize) -> Self { // SAFETY: The safety requirements of this function satisfy those = of the callee. - Self(unsafe { RawFormatter::from_buffer(buf, len) }) + Self(unsafe { RawFormatter::from_buffer(buf, len) }, PhantomData) + } + + /// Create a new [`Self`] instance. + #[expect(dead_code)] + pub(crate) fn new(buffer: &mut [u8]) -> Self { + // SAFETY: `buffer` is valid for writes for the entire length for + // the lifetime of `Self`. + unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len())= } } } =20 -impl Deref for Formatter { +impl Deref for Formatter<'_> { type Target =3D RawFormatter; =20 fn deref(&self) -> &Self::Target { @@ -848,7 +859,7 @@ fn deref(&self) -> &Self::Target { } } =20 -impl fmt::Write for Formatter { +impl fmt::Write for Formatter<'_> { fn write_str(&mut self, s: &str) -> fmt::Result { self.0.write_str(s)?; =20 --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 3C36E2FE58B; Fri, 22 Aug 2025 12:16: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=1755864969; cv=none; b=gYCc6ZRNQvj1BaANkWSYsxFB7/+XKWfxogoUJP3vCt0fUOAp5Bi3kCpKJ/NPHFdhy2OKylSumqjIk6U+ZufZw34fxgCdVE+1K9X8DYZg1kH6sVhrNWUOxHX59iaLUYmRBVW2VLqVh7eDwo+GqcEc26iaOHrwjd5UOjlVcU6XTgo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864969; c=relaxed/simple; bh=0EwZ8Bosdo4P1PMrvkQ74N/d1HBPfa9SjJN+YgtIPJo=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=UwQLdmSulWV23xmMjsygvaxDNCKoOd73z9emspJ3TiemlfJWPlVtgfnsKZz+EI68J4zuu8Etrcq3nk5ejFNEEu+zKrK/6vJT+TFWfjVk5saGEp4jgAKDW8yfspZcoP8HS+ExC2Jb6ePVE/XYQ5fsrD5jSpJjvgy6fVmdCSYgCLc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=CteaHG5u; 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="CteaHG5u" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 80E3AC4CEF1; Fri, 22 Aug 2025 12:16:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864968; bh=0EwZ8Bosdo4P1PMrvkQ74N/d1HBPfa9SjJN+YgtIPJo=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=CteaHG5uYu+fgU0ydpsQctoO+NcRp99GavfSh0gZ5Neygi8ZsqBkW9AUBuxranIFn rbioL8nUK4Hhqi1+tqYeIb99EazrzYRS8hxspN/PnuD/uaaLgq8RF2NoZcrIWb4OZL WOkRFSR/IUk3hn0BuKjSPpgk4cn89mWouD89h9OiTDz2a3TSMJdP+ghSHu2uC0rxyw oj4ZTKSyUmqRGomL9CDsA9CMZcHmlBpx9stSUKrjNikXxl+R7xGILPGXQs3g6kxxMC 625e0YKD1/5VCRGhbQ1mxeuNelgz3D9hZFvwrTFBDghkP6eK8DX1vvcJ9JTUY/SLoS ybtXg1rw+1PPw== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:39 +0200 Subject: [PATCH v6 03/18] rust: str: expose `str::{Formatter, RawFormatter}` publicly. Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-3-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2004; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=0EwZ8Bosdo4P1PMrvkQ74N/d1HBPfa9SjJN+YgtIPJo=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF80+rrHS7+GZ81ClKoMp1B5FnJIH4cZzbR+9 k4O04baqHKJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfNAAKCRDhuBo+eShj d2ldD/9QpdZrIQFQXXsHosaGs8sNuzaafpIEffo9hkUXORjV1Rp3i23cpbWzjtks/RHCyW6Jjy4 +K7ZNNnngVY391rjjkrHlC8syOouJ3mnX8il3XHbCNIJ+n01lGo2SEGLm4RjQCzRWjn2N4ZvGJW H4BkX0y/BWTERKMdul0uDDP1U/FsFU2IgvWBG5W5zo4yrvmZD59aH5un5o/PbvzEXyDA7Psgm8+ FAp0G5Gy/J8IfP5AGi+yzfcaFS+P4wMNmcwd11Wm1zu5FE40M7rgkMzRX978p5ssLt9cbwyfbfc Bj7EA7yKKMF+cm0PNxEoY+UdlRG2IyaECRExjmINEJfUAJxYWmrqqrBGgGfysKdH9a3zyi72Tti mVDe9w3lC7k6EM4YOMFLkILPe2JrN/q9wshtTbG9yTAZYBTTD+y+wP5fPKkqpgIux/KBIPa3doy tuBJHbl1RuFVniYBy9fc2M+6+FBHfyo27/r2llTI1/aC/RPJtObVXMAuHgAZ0ikkjQT3IwoNMez b/HbXvf8JR26GWQLuG0MywcNDTjXT/QLeK7U8pQKZkArXvdK4DpVcyi64l+Lb4pdir66N4XN4A2 hMn+/985BOYL+TEPqLL/UjNMwaQVvspPfMAqZLGxjjt99lNVfL5uEJCQ2bt8DTIX4mGFpj/qp7n dV3odvICku5OSuQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 rnull is going to make use of `str::Formatter` and `str::RawFormatter`, so expose them with public visibility. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 76632da357a6..46cdc85dad63 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -736,7 +736,7 @@ fn test_bstr_debug() -> Result { /// /// The memory region between `pos` (inclusive) and `end` (exclusive) is v= alid for writes if `pos` /// is less than `end`. -pub(crate) struct RawFormatter { +pub struct RawFormatter { // Use `usize` to use `saturating_*` functions. beg: usize, pos: usize, @@ -794,7 +794,7 @@ pub(crate) fn pos(&self) -> *mut u8 { } =20 /// Returns the number of bytes written to the formatter. - pub(crate) fn bytes_written(&self) -> usize { + pub fn bytes_written(&self) -> usize { self.pos - self.beg } } @@ -828,7 +828,7 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// Allows formatting of [`fmt::Arguments`] into a raw buffer. /// /// Fails if callers attempt to write more than will fit in the buffer. -pub(crate) struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); +pub struct Formatter<'a>(RawFormatter, PhantomData<&'a mut ()>); =20 impl Formatter<'_> { /// Creates a new instance of [`Formatter`] with the given buffer. @@ -843,8 +843,7 @@ pub(crate) unsafe fn from_buffer(buf: *mut u8, len: usi= ze) -> Self { } =20 /// Create a new [`Self`] instance. - #[expect(dead_code)] - pub(crate) fn new(buffer: &mut [u8]) -> Self { + pub fn new(buffer: &mut [u8]) -> Self { // SAFETY: `buffer` is valid for writes for the entire length for // the lifetime of `Self`. unsafe { Formatter::from_buffer(buffer.as_mut_ptr(), buffer.len())= } --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 8A8C62FDC3D; Fri, 22 Aug 2025 12:15:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864947; cv=none; b=h5B8/6TsfVsLOuGTIbJlgCckHD+ukviTNqjULAqvPQjVbCxJ5FgdpzRK/Y0oqZZXNGB2ErdeNy8i+ZI4aCCadclUFWChW7xfIRP0B5oRm1BykB5DZTp/0Xv1u8RTYq10rM9b/zPfgSRORVRMBnyDnrexoSDPztD7rEHHzHZ7xB0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864947; c=relaxed/simple; bh=Hl2oRNYbBHso61BXXHLO+jiCInVPd7Fo+1gfwzFywQ0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=MJWP5zE0ve8pRgjdi9KIf75WqqY2PxbweU+p3/ead4/8JQcDggv+HXw++MmhENEdo7v0RNOUGCEj9VXj+lhIcnJu+7l57vO9wRC4WymY/si9yl/s3M7cXp83wo0ThU9uJTIm6hNstLjXQCP4hOCBsAkt58NJbuITyz9AEOIUMLE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rbJZv7wI; 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="rbJZv7wI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 010BFC113D0; Fri, 22 Aug 2025 12:15:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864946; bh=Hl2oRNYbBHso61BXXHLO+jiCInVPd7Fo+1gfwzFywQ0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rbJZv7wIu9O3kILewVIi76qiZPtowYk4zdxAP/7L1VO240NlxgeRuUSkLr0CWmHJR V+JklussjI9UlyR6a7Zj5eWCQ/rxuZpAbfBbg+wS9UO/0nYamGBM39kHdS5rRBLLve DZlKKKNj+WgVcQOFzuMeMZE5gLcdvyRTakutosYFM3txt/K4gEcmXso+O2iNCOEkki WoFJZ5Iybq131jBu667TqdwvMQBVc+pLOPdUu9ohNuCd04euuZmyqHskSYPBCmTTej gACGAC+XtyB/UxsklpAPMLrTZZTz1lNVzwBE9zzq7nlbNNwWBcvPJn+82G7gldzTnD xV6moeKfPZCIQ== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:40 +0200 Subject: [PATCH v6 04/18] rust: str: introduce `NullTerminatedFormatter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-4-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2576; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Hl2oRNYbBHso61BXXHLO+jiCInVPd7Fo+1gfwzFywQ0=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF81D6SbPuy2OHW/7vXXiRBHmhys31IydQyJA 8XCju6/YxOJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfNQAKCRDhuBo+eShj dxxSD/wIE9APfwQmyjVRYGzUfekfLRx6TbbRm+Vq7rflfHMcgXlME1VaBXSSPajHOmHkyC9GdJ4 onz2jGzVO7NWiuXgNrJ10A5+1HKPTgpcKVMYQQsuc99mfrI7CHvDtEDd407kAku/RgeMEsc01E1 Bq7VAehUj2HOFrSbrfCCR05gDYjRIWQb8XigXAp0J+BtDAsKuUU1yIXTN/SEwwblue4uIgisDsS VX7lwWe+rt9VEHrVfnNcKJc/kDimBQNQ/kvR/fWDRF1amAi4l9eCmP2rAmZDaVIybmyFG2d5prS BtlIn4G8mgBuVOmi81btNl9uloCKLtqSAGcD5zwAHGQz0c8VUf0rPMQ6fFJ4ZkY3+6fIBNK70j5 0GGB1UAiasWvxQiAABvCS/3ZrmJeOzwDlNyUZ7IwnpNaI2XPSlY1WCwrRqlYwCB3jtRK3K9IRR4 /5FoQCOwz6eZchfmGgpikWBS2vQnuxWXvM3mxSgYkTwFjJWiKNgLe31fwYbwztAJdk8ki3Z4inK uCRkPGR3IjfO6jLPQcGIEsF5X22aO+ercCfV5/efu39Dvc93bYLiZ1SUdo0kqOfXQdIzOuLGVNh xQBf93dAoKPwVNV7HeOntCL3Eu2gdoTXg6tzBuIHbBocS5o5mjm4401jKYacu2wH7PoGkeqbJTP mZglkuwkDKAG5iQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add `NullTerminatedFormatter`, a formatter that writes a null terminated string to an array or slice buffer. Because this type needs to manage the trailing null marker, the existing formatters cannot be used to implement this type. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index 46cdc85dad63..d8326f7bc9c1 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -871,6 +871,55 @@ fn write_str(&mut self, s: &str) -> fmt::Result { } } =20 +/// A mutable reference to a byte buffer where a string can be written int= o. +/// +/// The buffer will be automatically null terminated after the last writte= n character. +/// +/// # Invariants +/// +/// * The first byte of `buffer` is always zero. +/// * The length of `buffer` is at least 1. +pub(crate) struct NullTerminatedFormatter<'a> { + buffer: &'a mut [u8], +} + +impl<'a> NullTerminatedFormatter<'a> { + /// Create a new [`Self`] instance. + #[expect(dead_code)] + pub(crate) fn new(buffer: &'a mut [u8]) -> Option> { + *(buffer.first_mut()?) =3D 0; + + // INVARIANT: + // - We wrote zero to the first byte above. + // - If buffer was not at least length 1, `buffer.first_mut()` wo= uld return None. + Some(Self { buffer }) + } +} + +impl Write for NullTerminatedFormatter<'_> { + fn write_str(&mut self, s: &str) -> fmt::Result { + let bytes =3D s.as_bytes(); + let len =3D bytes.len(); + + // We want space for a zero. By type invariant, buffer length is a= lways at least 1, so no + // underflow. + if len > self.buffer.len() - 1 { + return Err(fmt::Error); + } + + let buffer =3D core::mem::take(&mut self.buffer); + // We break the zero start invariant for a short while. + buffer[..len].copy_from_slice(bytes); + // INVARIANT: We checked above that buffer will have size at least= 1 after this assignment. + self.buffer =3D &mut buffer[len..]; + + // INVARIANT: We write zero to the first byte of the buffer. + self.buffer[0] =3D 0; + + Ok(()) + } +} + /// An owned string that is guaranteed to have exactly one `NUL` byte, whi= ch is at the end. /// /// Used for interoperability with kernel APIs that take C strings. --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 BFCBA2FB617; Fri, 22 Aug 2025 12:15:27 +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=1755864927; cv=none; b=ADU7wIy9asDJMkbhB5XA5kZnRB/FD8ej6zmf6p8kkNW+fec096VREli6R2kqPKTxWUjq388K/AieH5qltjZ4pF1FQNcYoXoE96ch++kiSkuRqLUZ7nparf6afsah/9ARrnmJN2GdOxWeh9ADB+2M9VurGB6HuVBF/XOHl0OgjXk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864927; c=relaxed/simple; bh=bIxxtoDqzYFDJ1IXIjGENBqJclBo9NxY+i1qnM7Qz0o=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=gLmsxGWM2ZfhucNNQRaG/gsR5vl/x7H0br+ZPOpuiUnSd53Qz5yBf+MZ+G+EVJc00aC94FxMd6LVikVu3H4SRXxJVWqzCcQ0TKgKEXvJwZ/w2B/f78GTmPjr0Gbu4HCPajldGPGDw5KqyEwgPlmpvAxSgAYiZTJAhwQXnbrwPTg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=oJ1E3SEj; 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="oJ1E3SEj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 030F0C4CEED; Fri, 22 Aug 2025 12:15:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864927; bh=bIxxtoDqzYFDJ1IXIjGENBqJclBo9NxY+i1qnM7Qz0o=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=oJ1E3SEjEkH0GF8GsmcGJwDYv6SFby6YllSCraLikrMrbJW421ogFP6C+l7WWydsx MwkOuzA1J67aU0aYHSjgyifSA4J9QxgLwM7cBWBLGaifRrpAB3DbN9rflsDKpKOFNh yKznC6Q0FS3rnjiLavv8av/x8hG/Vq63INCzYUVZk/0QVoNrbmvF9+yJAyHU90+9fv HJ5i72lqASkFkYF1BY+VC201hCgTK4hOhQsK7PCUf0tBS1LI+1bYFb7iEVqrRBX3GY OAGZy9LseLzlkqFRxHOrYAKB2hptC+780arAEBiEVzMOcAXOGB2OuGTVs65bteyXyl BpQqfUHq4knyw== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:41 +0200 Subject: [PATCH v6 05/18] rust: str: introduce `kstrtobool` function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-5-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3167; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=bIxxtoDqzYFDJ1IXIjGENBqJclBo9NxY+i1qnM7Qz0o=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF82flEFsnvimB9symy7Ed4SF+IaSLnHrFYhA A7cQmKaMM6JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfNgAKCRDhuBo+eShj dzg7EACPTsE9Guwck+I8kM8n6or3Jz24zvlyySn2Rjxw1riOjRg4gBRhSib7JbWycYglCJc7FvQ qPPOjm83UUJx+v+g97q4fJhmPR2PPUyr/BhDb2sZL14ZoP4zuaPaO1BQVUDv9gb7qcbQjnf4rjK iaFkANSvlUchHD/NA8WZ4VlcLdLWg1spQdhxmrxNbjFXTfEFavI1y6qKKCMXKEUnobCZ+aAj46z ku+bwIOPJmWo1Qnfb10dV+/T+0mZWkbKhJwFk0ONNDh22Sj/wYcNnKzaWrmU95rIdhBlDYlPxtp MytOeYq+6jsiZ9onfn3qBzIIEShC7JxKuKL2GBxRX5ZoGOrk51ZrzB2+wpWkzEtzCUpxJBPqwY/ f2Mr3KuWyh+ywypVQwKGPDQMmeSjojXnG30dgqk7kVNwZYJXy7VwXE7w/G7IPYOWEPkZXLWE1tE yKgQvRgk4HHd7XkuPyFz+V5E7e+4fDKcGfQcqOLrMTiE++6vjNTpSmx/SveBdW+COHcJuB4SbL6 rgojzDI9YBpbFgHo+X20hm0lWzSnQy6yzWmRcpP5HPj0KUGeu5ovUAlM1V9wo78a+2OK4vQU8+B 6LLPBEWBScom2xemjaM9KuqmejRbl8mLwyxhVvcPLvjAnxaOLFGSRfvA1pEoKMpcCkJG75S4Oo+ LscmfAWWT+X1YlQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a Rust wrapper for the kernel's `kstrtobool` function that converts common user inputs into boolean values. Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 57 insertions(+) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index d8326f7bc9c1..d070c0bd86c3 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -4,6 +4,7 @@ =20 use crate::{ alloc::{flags::*, AllocError, KVec}, + error::Result, fmt::{self, Write}, prelude::*, }; @@ -920,6 +921,62 @@ fn write_str(&mut self, s: &str) -> fmt::Result { } } =20 +/// Convert common user inputs into boolean values using the kernel's `kst= rtobool` function. +/// +/// This routine returns `Ok(bool)` if the first character is one of 'YyTt= 1NnFf0', or +/// \[oO\]\[NnFf\] for "on" and "off". Otherwise it will return `Err(EINVA= L)`. +/// +/// # Examples +/// +/// ``` +/// # use kernel::{c_str, str::kstrtobool}; +/// +/// // Lowercase +/// assert_eq!(kstrtobool(c_str!("true")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("tr")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("t")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("twrong")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("false")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("f")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("yes")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("no")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("on")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("off")), Ok(false)); +/// +/// // Camel case +/// assert_eq!(kstrtobool(c_str!("True")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("False")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("Yes")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("No")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("On")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("Off")), Ok(false)); +/// +/// // All caps +/// assert_eq!(kstrtobool(c_str!("TRUE")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("FALSE")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("YES")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("NO")), Ok(false)); +/// assert_eq!(kstrtobool(c_str!("ON")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("OFF")), Ok(false)); +/// +/// // Numeric +/// assert_eq!(kstrtobool(c_str!("1")), Ok(true)); +/// assert_eq!(kstrtobool(c_str!("0")), Ok(false)); +/// +/// // Invalid input +/// assert_eq!(kstrtobool(c_str!("invalid")), Err(EINVAL)); +/// assert_eq!(kstrtobool(c_str!("2")), Err(EINVAL)); +/// ``` +pub fn kstrtobool(string: &CStr) -> Result { + let mut result: bool =3D false; + + // SAFETY: `string` is a valid null-terminated C string, and `result` = is a valid + // pointer to a bool that we own. + let ret =3D unsafe { bindings::kstrtobool(string.as_char_ptr(), &mut r= esult) }; + + kernel::error::to_result(ret).map(|()| result) +} + /// An owned string that is guaranteed to have exactly one `NUL` byte, whi= ch is at the end. /// /// Used for interoperability with kernel APIs that take C strings. --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 08D622FB602; Fri, 22 Aug 2025 12:15:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864958; cv=none; b=MISSI1EmhjCJTcxyyMtAnPwGyqjUVmrVMhN7jHKPkyunAfRa0Vy1trLHtHJZp0oWZZcWG/IDtVD94i9CTfOngZVtene56j53jOR9ZbcgAYN64hshj4pJQRGNkGBKrENSa+G4YdKvkohOvT5Wf6KkahM6Z+S+PcVyWzIEL9mV/P4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864958; c=relaxed/simple; bh=pnrl4BHc/0QCGXcnlvXhVfPL/hCVQSnNuvvFpYApzks=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cI0unaqys1kKOCTRR1gH0X3FAi22O/gwlseBZia9TMFlQ35LBqyVnEbokDN05Xlsybn5KLKyxl00pAVco8dSz6xWIZz62uJbTajY4XQMiZ4YemiRK1CZA/ne92ryRq+dbX1q5B8WtvVzb0GElsX5wdqb8svgSPJN5ftTo8U3y+I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Y/FLA875; 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="Y/FLA875" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4161C4CEF1; Fri, 22 Aug 2025 12:15:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864957; bh=pnrl4BHc/0QCGXcnlvXhVfPL/hCVQSnNuvvFpYApzks=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Y/FLA875JXp7wJkqK81JuURrI8rDS18UCuzxgdG9ySClggRM0mvBppLbrEPqMVoGw RPXYZfptCAkvG3jtNnRH2GPgxOeLHv2RvmZb1VOwkcoY2YB5MflQjVc9Y9I/pZ6WZ1 8COUTKr+chcMJ77uOarmjjvFGa2beAdsDkgAc6VYEBpRHSMWHDALzIF1V9gJ2nAHwr e9DwEV1MFbIRNm5uh/nDwZN8AcKBharOUZjo3qetqsd7cftWf9d2qiUYeqFmdKKVe/ ZUXDbFDAMHWrBS+JoKIHEHAxa5tl1DrbHMp+ftRa6L5cAV+6wmPVZ+cNQTXS1LIP8M YydsuVI/UmmhQ== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:42 +0200 Subject: [PATCH v6 06/18] rust: str: add `bytes_to_bool` helper function Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-6-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2761; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=pnrl4BHc/0QCGXcnlvXhVfPL/hCVQSnNuvvFpYApzks=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF83joD+qfDFd5COVCdtfn2Uj8Qes74wKaVFg LfX599ms3+JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfNwAKCRDhuBo+eShj d24nD/9YPa7FLU1snrfCuoQK6W+EWxTuFfetVokgP3TXmRcM3lq8ycM1MWGV2t5DUL4yhxLYzcu i5sh49axa3ZwtZTlqOmt1E+D3U+mpXrWI1WkR+J3mhT7feLXxD2cj7VfKLa5gY5OC4mXWq7xmMe mUyhk/MFHhxPArMQuaF5FK8r2kYqTuiLC7lUmT+IWQsZrMUMKP87ms9+4t6HZ7CuRL5UBIDUDY+ TbS2v5K4BYwhSA3iUUwBHWoj9q187d89yKh4x/4Pu0OI7pcFLfZwP6QmRjbrgpr/nb0fmkP4JFd 2z0lpQm5VqcwwfzncnFFtQ3ZYWJ5aI6Flur5op+G6dC4MZMv9PzXE1kmlGrAKSbGhG/GG8SpmG9 4Lf9mISNr8TrwEWhVIEHNyvv4rmegtWchMu93W2dIjAHmmdyn/hACsVstiKx6gp1CgRxfIQ3s9v WAWuK8dhnw/xqEUHbP/0Ns1zY/4wA/kyA56x32eleAk9lcAqr7j+SbA2bYplmcDw9G6UeWyuzGf khbgYT2n+qAcwyypgkC5fHcmigRUR+xhbmSCFk3CjJZfpPkNQXa5K2xV9TCCBfZAOQe5GmUM363 5YYyZak4Rl+MQBRHQL/bSI2UE1PyF/yXyxc/DCKLWNY0RP/eqZz1ItU72AMOK1SwsZrmBwS2mfX v+HpO2IytWO5Jhg== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a convenience function to convert byte slices to boolean values by wrapping them in a null-terminated C string and delegating to the existing `kstrtobool` function. Only considers the first two bytes of the input slice, following the kernel's boolean parsing semantics. Signed-off-by: Andreas Hindborg Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida --- rust/kernel/str.rs | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index d070c0bd86c3..b185262b4851 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -921,6 +921,20 @@ fn write_str(&mut self, s: &str) -> fmt::Result { } } =20 +/// # Safety +/// +/// - `string` must point to a null terminated string that is valid for re= ad. +unsafe fn kstrtobool_raw(string: *const u8) -> Result { + let mut result: bool =3D false; + + // SAFETY: + // - By function safety requirement, `string` is a valid null-terminat= ed string. + // - `result` is a valid `bool` that we own. + let ret =3D unsafe { bindings::kstrtobool(string, &mut result) }; + + kernel::error::to_result(ret).map(|()| result) +} + /// Convert common user inputs into boolean values using the kernel's `kst= rtobool` function. /// /// This routine returns `Ok(bool)` if the first character is one of 'YyTt= 1NnFf0', or @@ -968,13 +982,22 @@ fn write_str(&mut self, s: &str) -> fmt::Result { /// assert_eq!(kstrtobool(c_str!("2")), Err(EINVAL)); /// ``` pub fn kstrtobool(string: &CStr) -> Result { - let mut result: bool =3D false; - - // SAFETY: `string` is a valid null-terminated C string, and `result` = is a valid - // pointer to a bool that we own. - let ret =3D unsafe { bindings::kstrtobool(string.as_char_ptr(), &mut r= esult) }; + // SAFETY: + // - The pointer returned by `CStr::as_char_ptr` is guaranteed to be + // null terminated. + // - `string` is live and thus the string is valid for read. + unsafe { kstrtobool_raw(string.as_char_ptr()) } +} =20 - kernel::error::to_result(ret).map(|()| result) +/// Convert `&[u8]` to `bool` by deferring to [`kernel::str::kstrtobool`]. +/// +/// Only considers at most the first two bytes of `bytes`. +pub fn kstrtobool_bytes(bytes: &[u8]) -> Result { + // `ktostrbool` only considers the first two bytes of the input. + let stack_string =3D [*bytes.first().unwrap_or(&0), *bytes.get(1).unwr= ap_or(&0), 0]; + // SAFETY: `stack_string` is null terminated and it is live on the sta= ck so + // it is valid for read. + unsafe { kstrtobool_raw(stack_string.as_ptr()) } } =20 /// An owned string that is guaranteed to have exactly one `NUL` byte, whi= ch is at the end. --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 E5E152FDC3D; Fri, 22 Aug 2025 12:15:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864944; cv=none; b=VnG09z3aI7lxarSTb9qiFt3RCGx99fl9yZRQXHOOU1UfdDl3yt9VaGnQhen7B4c88Qg/m74jxBuI2DeeNl4u1QCkFjqoey12SfklIovz98asCX4ASuLTSRXQmYNqd3s1kbaXhGmiZ91/0tEnar1iU/JotJXaWfN2AAWPbzIkWb4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864944; c=relaxed/simple; bh=ATd5+u0whvcapgNxLkUWKYG0itmO7zjTecaarUvu5lI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nNJEcunB3dAWsPxBrecswY4TXn4RH1qBzhQKAc/5kBpUnreZIsW1fVrhKeRaY9hfI8EDetSofNuNjGFbLdLrurS/xClAh+0vvQvVyAVJG6qGlleT25JJeoiWAXoW2AzGbmCboa+ta5VT+Z2auwPFoxohTxVBs4XDGmrKU/WWvaY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DYBxR+IB; 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="DYBxR+IB" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 722A8C116C6; Fri, 22 Aug 2025 12:15:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864943; bh=ATd5+u0whvcapgNxLkUWKYG0itmO7zjTecaarUvu5lI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=DYBxR+IBPUkqD9JvCaIrUrnxARfa8PdbKEvhc8fFvoYFBsqFNveyEOJQxHtpWpRLn pa1hdyq1R4Zty1e0KewQBhWIPJUVeV90btoZB6eMcmf0Bi0v4J8zK6PnS8EruV79xp 7zJcWUAOmzucC7xnObrXV3f1QhhYQq9ixwrWwJRe7gO5IdOypAB2I3fLqcDy2Zr8Hd IvNBQ4JyGOp46fMPdwO5CJIRpWNfEvUYr5qaQp/aRiZHzwQa7ajFWcQJRLIpRJPUDs wCkuacgK1TLANRIcbfR9kT7wgB7mPj7RWE3HOd02Pz5dyssVrF5YJSi5RK40W2dHgL BjKr+NqdK/ZeQ== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:43 +0200 Subject: [PATCH v6 07/18] rust: configfs: re-export `configfs_attrs` from `configfs` module Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-7-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1172; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=ATd5+u0whvcapgNxLkUWKYG0itmO7zjTecaarUvu5lI=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF83Ky5IQVSq++INe0nzExH+utK1YiwzqUJP2 B46T5iL9oGJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfNwAKCRDhuBo+eShj d3m8D/9uwUXdXA4j6OM0nJBLpMcuKCeT5uL4rSeQXBorLWs9MhuzL2CdiMU28UhjHrJ0ODQPmOo QuHmoaW/a2e0RApYeKaIYlzAm554uL38s5Kr4FmrMeBPKuaZdDbRVP/EptSBMTenoivzQneTuwD pbNUrMzu7lg1gacYAGx0JO4LVtLu7x1KkF/X9mf37N29MpxQFdDFooajTAnugqlbXKUpIDTZpvQ 1WYI1ABbCzX0f5XMY/Ao0ZvQISsT7owoxv84liZDhKob6j07ci5RUUm05dFmBXRes+MZ2LG83h4 5o2tokuJCGDa0g5x/MFYkiiaggONgz0/brR3ru18hiqEsvAnVZGT/CaDNxWHdiO8czn1ZyITonv NzdYBKgJPNWhHYJ0MClKWSulNK+eJUghBdy3qks7ucJI3uhMeSOpaKO84EFkoQIV7hi4FJWR8Vw m7KGddpWzmNn/jPdV91kqXkWilTpB9C6Baw/P6B/5ILyl8++NK/w+HeXyz4nTuULdsh4jF2rY51 PugppUB6jLdJHjVkOmOJnizgrYuhi13UpGepZqOeg5do9Ey2/KpaMgmg1pIqZIRpoAvDXvpuBsp 5bNHae3+MZEXvSS0QbYCwDkTO4M0/mJ2LM6vfp0LguF018yOoMH1CHvlWn0uXLaSRdiFPpHYEUk B2YwsZuLXThZoTA== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Re-export `configfs_attrs` from `configfs` module, so that users can import the macro from the `configfs` module rather than the root of the `kernel` crate. Also update users to import from the new path. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/configfs.rs | 2 ++ samples/rust/rust_configfs.rs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/rust/kernel/configfs.rs b/rust/kernel/configfs.rs index 2736b798cdc6..318a2f073d1c 100644 --- a/rust/kernel/configfs.rs +++ b/rust/kernel/configfs.rs @@ -1039,3 +1039,5 @@ macro_rules! configfs_attrs { }; =20 } + +pub use crate::configfs_attrs; diff --git a/samples/rust/rust_configfs.rs b/samples/rust/rust_configfs.rs index af04bfa35cb2..ad364fb93e53 100644 --- a/samples/rust/rust_configfs.rs +++ b/samples/rust/rust_configfs.rs @@ -5,7 +5,7 @@ use kernel::alloc::flags; use kernel::c_str; use kernel::configfs; -use kernel::configfs_attrs; +use kernel::configfs::configfs_attrs; use kernel::new_mutex; use kernel::page::PAGE_SIZE; use kernel::prelude::*; --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 501D92FD1CE; Fri, 22 Aug 2025 12:15:22 +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=1755864922; cv=none; b=thAHMqCunaXNE2b2KkuCMg7h4ibZfVNZzJg5gzhNPXqqSDX06hpDZRUNl25kW5GTdDu4gqAiLxIdEH6oGUPbVW77TxIzzrq8AUabCgoRN8d06N4FN4WQn+IKIioS7J3MJRzzKSSlGLfmb8OeFkJsXX6HD829nFzGOAKpMbnCn58= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864922; c=relaxed/simple; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=cYj3SzF/XCmk1G4YvT6Q1vbzEhVGfJGsS88BrmVZJr3JIQVyFb1f5sAMuNrzSrZtLeLmEEVauEphz+s7ufp0mzXuJCl7Tys2DYB29XVpcKhV9xqzD6zldV7ugFoOkYNMKo8B8jJzESfxZtj6CVwBhGdZktFM2j3GLQhDQMq7EnA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=UTkOEpWr; 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="UTkOEpWr" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB522C113D0; Fri, 22 Aug 2025 12:15:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864922; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=UTkOEpWrLXJcs42jUIvOnetUEv2udipxzsscUOceES+Qauxe1dY7I2xZcD2w8m4hP TwBQM7MuWlYlad0UGtrWDrraur54OSJO2OSC0Niqe/wuOD/6aqeshyR7KR1cTykOMu 9ONCK3v8qoIUF+gcAC7PLFUWAGWjLpI61NaBpb4sdMv4gkDD3qK39yTowe/qwfXDAg SfbZXPWBtr92VQS29sFuUDlHQ5buy2U5skIixnZ1G9rKn4JdAPDlTtrxUmNqhobCy5 dSVyKPABePoQN2GF+DEahsAycGaZa5xh2fNiTgZlIocyREnYvgPJhlkOSpxLCbO+/e 5K4VPJ1uX38zw== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:44 +0200 Subject: [PATCH v6 08/18] rust: block: normalize imports for `gen_disk.rs` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-8-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1184; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=IorSNq8bDFCuUxmsqyPpldQ/kj+0H+QjukXF6SExSLU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF84IZsT/PnZCkY0C5tGvDURlEdBW+zo4aeAe sbUkXcKYB6JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfOAAKCRDhuBo+eShj dzQTEACH3/BznknYgy92P4Z7MDtqAQS7KjEC+y+w2I35vtnXwaCX+/puZnoBQamnsS3sOJQGaib nOjyFL+LYLA1yad9+Ugpn21KRaGbKzM7GaJYdzuUkUJ+d592isdYxbYRHiWVD+OaYCUjTemIDBF HZ5kgHoX3JPWqJuUlV/5SPOZLF8vyMPLe17D/e/EpFPJSNdEe8e2dnZXtFULb9SRexpVATzPMJp yMViMtWhr7EkoTbZnv615gveVGIanp6/gp4JvzC+SuBZoVw/hFtqvESr2pqjEs6rutM2+PxhJ5Y vSgLrwAMDRv5WmvTE7Ref66UbvQccJa9d6Xb6/eqbQATdyWi8GXSaP2iIs4q4FUCW0i7tcatDBk bu7dFx4GklCl1akhxDPmsuuPjPNovMtNDpMYuRWOV01NnJVIaU2zV4KkFIHNCUqH53Heh6JWfZf 6Y8nArdvUV3K2J51HCkADhXkroKXAavfoMzv4ic2uQ+gQRSjstB9s/NQPxyLw9vNRZPQL89pNdr uh9qHt6EH+rSdsWGGKXRnO/8aLVGg2rmZNYrCCXKQd50TyO5KQ+5EuUbaS7F9GKLk4mSxwT5Ao5 X2gQxcCrjVxYaBvY4Z/EFFE9kbYzYtv+GOAIQ5xENjeehuC5b5sngMgjWek/PPGSTfThy7eJTL3 Fo+R41q372PA2kw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Clean up the import statements in `gen_disk.rs` to make the code easier to maintain. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/gen_disk.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index cd54cd64ea88..679ee1bb2195 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -5,9 +5,13 @@ //! C header: [`include/linux/blkdev.h`](srctree/include/linux/blkdev.h) //! C header: [`include/linux/blk_mq.h`](srctree/include/linux/blk_mq.h) =20 -use crate::block::mq::{raw_writer::RawWriter, Operations, TagSet}; -use crate::{bindings, error::from_err_ptr, error::Result, sync::Arc}; -use crate::{error, static_lock_class}; +use crate::{ + bindings, + block::mq::{raw_writer::RawWriter, Operations, TagSet}, + error::{self, from_err_ptr, Result}, + static_lock_class, + sync::Arc, +}; use core::fmt::{self, Write}; =20 /// A builder for [`GenDisk`]. --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 745783043A9; Fri, 22 Aug 2025 12:15:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864950; cv=none; b=mjVVoB6izd/tIMTgQ1PeXfr42QgSiuh+3gy3hhCqsJdVJMvr0JGagJz8sMmLDF+zG/ILloeA2feX/WPXSTedWYZCpPvUwIkOuJoJ1DQw+pRg0BtmoXqmN6tiwTmfTCuMElipTCu5yFpKAhfUC3lxiHA6VaS61ZoE5oo4hjuyB7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864950; c=relaxed/simple; bh=/9IChsMSyP1ostiMTrAMl16dR46Wiyp00l1jbhMBpuE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=W/0NGSLTHx4PAQQe0L8blw984vUfR00TejmXbRKW7vJSOaMKa8HVgcU633XHsxnznT57rQHS7YwmtFDe8T7Tlv+nya8uBiksDdMc97oqEJ5ENiRaECPkVCxvZERG2hiEL3lwR2pbNOcd9vKikbHvrfbbel/GGvuotd5a2E4ONJA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=F5gsG/dN; 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="F5gsG/dN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 8AA73C16AAE; Fri, 22 Aug 2025 12:15:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864950; bh=/9IChsMSyP1ostiMTrAMl16dR46Wiyp00l1jbhMBpuE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=F5gsG/dN+EpqIioF+5Crx9jqBdMVWahUbwtn3E/GmU0zgdy/I53u6GHyVyOaNXEgd P15Ff86rSnlHaAnx6zCQ6fXh4tApHRG2j9G2cNckEjWNLloo2TV++osT6euxf3f4ud TeOwxjy2RK21bxvq8tnNFMhNABK6eHDQaMYV/Y8mSQ1Co/W/S9GeaoiGxgkvc/S0v+ +yZoh1pQhr2bUQ58+g/pU9lB1TA8Avsx22ctyW+nA6jYqnXJkTgW05uz6/66BIaKsa Nn/0fMuBhi30btKDaAlf5RvMTrc373k+3N5SPL3r3ITwFri7VpcN29gQCJs9Fxf4Oz tpUpHCOM2huqg== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:45 +0200 Subject: [PATCH v6 09/18] rust: block: use `NullTerminatedFormatter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-9-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2901; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=/9IChsMSyP1ostiMTrAMl16dR46Wiyp00l1jbhMBpuE=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF85tuIe+jHw5Yrp/W7w54gULQ9gjtqD8kzcK 8rlqP8N3+6JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfOQAKCRDhuBo+eShj d0JAEADE6g7CESKxxVlGqo8GKfTbnQxwLmN2BTD+du02ktR+sVgySmlLbCgX9t0rdWGG01JKIYU uTUzXs+baA9YiL8YHxmFO3/pWC2VE0PAnzaJU8gyvJrTblU010ei6Ze8johw+45ENaDUhWlhI2k YuwXphez1+p9yNbJaBh1KuNK/aC4QJgNGigaSvjjCrYT4NEc9375F5D6sTuKll0GPcZnq/pr3zj sMhVYbl9GqOs3gKpCUDYInqZ+cEcGzl9AGdqqyjkz93FDBg5zOQIAmwt3VTWTwlQkgy4sCbmZbS z86O0pRhZi4RjrkBXt+424H06+BFaE8y0WCRbfMONpNrMUyZfFK0sJdNVlDWEDz0v5Hqg3gPWas 5K0WVD/B8OjlDxje7EVsTzKSEJFyQZMrdPRunsCO91SkuimCDBcBCVBMv0M2hxEK6xkiCFv4m/M 3JzGh5x7mwGfGHz9qT7fn0v7jtyGMjSrDPyk5RrD7CPX/zX8mebb3aMVF82LEjKxu3/WNa8bDx9 vksc5GLOAxDwDcE6iIEHvSM4Tyk/z2QtLeVmyBx40CoMBVJ6LoC3bFm7Jik8xHjlpQkY2Ig6TKV 7rfsBJg4+VlIIyRsprZwRyqDAvZ3CtXzL08LR5j2ySn9diMdLwMpiAG/ArXlLbi/oSAtAeqbW02 C1ea3nuysqe2fZw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Use the new `NullTerminatedFormatter` to write the name of a `GenDisk` to the name buffer. This new formatter automatically adds a trailing null marker after the written characters, so we don't need to append that at the call site any longer. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/block/mq/gen_disk.rs | 12 +++++++----- rust/kernel/block/mq/raw_writer.rs | 1 + rust/kernel/str.rs | 1 - 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 679ee1bb2195..20f1d46c774d 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -7,9 +7,11 @@ =20 use crate::{ bindings, - block::mq::{raw_writer::RawWriter, Operations, TagSet}, + block::mq::{Operations, TagSet}, error::{self, from_err_ptr, Result}, + prelude::*, static_lock_class, + str::NullTerminatedFormatter, sync::Arc, }; use core::fmt::{self, Write}; @@ -143,14 +145,14 @@ pub fn build( // SAFETY: `gendisk` is a valid pointer as we initialized it above unsafe { (*gendisk).fops =3D &TABLE }; =20 - let mut raw_writer =3D RawWriter::from_array( + let mut writer =3D NullTerminatedFormatter::new( // SAFETY: `gendisk` points to a valid and initialized instanc= e. We // have exclusive access, since the disk is not added to the V= FS // yet. unsafe { &mut (*gendisk).disk_name }, - )?; - raw_writer.write_fmt(name)?; - raw_writer.write_char('\0')?; + ) + .ok_or(EINVAL)?; + writer.write_fmt(name)?; =20 // SAFETY: `gendisk` points to a valid and initialized instance of // `struct gendisk`. `set_capacity` takes a lock to synchronize th= is diff --git a/rust/kernel/block/mq/raw_writer.rs b/rust/kernel/block/mq/raw_= writer.rs index 7e2159e4f6a6..0aef55703e71 100644 --- a/rust/kernel/block/mq/raw_writer.rs +++ b/rust/kernel/block/mq/raw_writer.rs @@ -24,6 +24,7 @@ fn new(buffer: &'a mut [u8]) -> Result> { Ok(Self { buffer, pos: 0 }) } =20 + #[expect(dead_code)] pub(crate) fn from_array( a: &'a mut [crate::ffi::c_char; N], ) -> Result> { diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs index b185262b4851..a3e34f566034 100644 --- a/rust/kernel/str.rs +++ b/rust/kernel/str.rs @@ -886,7 +886,6 @@ pub(crate) struct NullTerminatedFormatter<'a> { =20 impl<'a> NullTerminatedFormatter<'a> { /// Create a new [`Self`] instance. - #[expect(dead_code)] pub(crate) fn new(buffer: &'a mut [u8]) -> Option> { *(buffer.first_mut()?) =3D 0; =20 --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 992042FE560; Fri, 22 Aug 2025 12:15:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864955; cv=none; b=Msjr6jgRkOJU4oN5bDKcjXFP7nWqpuu+3VFcYSbHnGEHmLsNxFPkanA4goSQBN34t/OLHuNaCxIIELWqDmBa9yD+DrIgF6SP/9LgR+L2VAYrRPWWWBs8402pIBWSRhkjFhcqkGnxIAi0xVmi/lFd+b7sFCuhnMcOj8dZkm2ED0M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864955; c=relaxed/simple; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Y/0Q5n4VFA4hqIfj8rSDcVY6bXiYk1IwfJ5ttBerwYwKs+ZMAXbK+wyyBbEONB9XqOXSYfQrUebdm3njgXUQvJ7ZbnqVwslTPz+9YcZ+vCOGj4JZB0pl+TdEpIpcXLg5/pD+tzAQb0TAvfajAPG3GvG5t+JhRaxQoX1QjOz3EPs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BNvlq2Qo; 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="BNvlq2Qo" Received: by smtp.kernel.org (Postfix) with ESMTPSA id EB535C4CEED; Fri, 22 Aug 2025 12:15:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864954; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=BNvlq2QoAlt+R95WvgpaQn4IfjlpSeKsqIg0mFAUsxHMbnpY2W4zfOZxbuQHaXEj5 VhLJlcWsfiqt6WtEvOeMvMm/IPRwV6VKSSTZgRRJpbG6X7MOFtTmneiKuRZ15UChfQ bK+U80uFAC7P90XlmJy+ObMymqjVYfVwfHWqPCAQIeYmRAxpLGVZt0dWiN+Hxq5Sqf WgGDv1ykYmgM+4BbDIaS7CRqElhWrexKiz8ehv4tbDrVRJOCHaYxzV21u60eoVRpmY 3PHcK8eudRkbzvP6MPM2VvP0jVH2w/CEZvS9MbGwbXozXHnaWu1SVTnzMk63DcQe/O SzuCtFuLSp/NQ== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:46 +0200 Subject: [PATCH v6 10/18] rust: block: remove `RawWriter` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-10-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=2507; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=Ccx5zgSLlbSIiXofZb52HFU6BY4bx1BMin6k8F+fBLU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF86FoAaKosNvDG8ngMAfCKCWd9BadJVJCEft CEZlJp1unyJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfOgAKCRDhuBo+eShj d/1OD/4j5JC5VxiHDtaORfNSyAmNclBnbRVJHZj01dhorrCKQ1UDk7qO+OKTJi0CAzro9pGiskp Vjl520At8JnixeVE691zQ7GHTCRy53zwRSVwK9tpOGE6cVShJFLyDZUZV/IiqAic+NTVfQ/DsDu 8ZhQSRJnuNRsmMfzSu66v5riLv+tOZN2eM52QvogZkrPC5K1fsgq7gSPPw7gQpTCfzqEj2Ad+aH /jnaK/X/Ximf6EoF7SOExCG2tnhOYcj6I1oQNm6ex+BiU4yytalNlklvKaBp56kIjUv9SBFC9SE eiruFnUqhlBlBtBmIZhkCVF59sH/pWv2gWg9s49xlJUZQY7v7cS48MsnBCK6lv3KrQ8+yA3Exa2 KJD7ANv4nRLZm4M/DC0yfIKYpLRQujcXPwosIptCOzIlHfO6xEVDG1tVOaTzJoHgbyM33dFYvGa Ii+yhAbYtG6JpKQzwiwVvP/ICq2OwNSVSk5CD33Aqn6drLKIozqnwvGOwXTf1hoqnyhQOc6QGg2 6am6OVNmy3xdzQwIo2D2b7hVEBkmQ0UfiM43D65mXsVq0IR+DJxMHyha+4P18dOaz/4dDVliBTK tATH3e7/HfQqgSRLZDfZVurxOfxVxTaVMB7mLOZc+Nzw5l5MHAdS1AX6cZhLJgTy+6cZaxxbBlf jcq035eUUeubmfQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 `RawWriter` is now dead code, so remove it. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq.rs | 1 - rust/kernel/block/mq/raw_writer.rs | 56 ----------------------------------= ---- 2 files changed, 57 deletions(-) diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 831445d37181..98fa0d6bc8f7 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -89,7 +89,6 @@ =20 pub mod gen_disk; mod operations; -mod raw_writer; mod request; mod tag_set; =20 diff --git a/rust/kernel/block/mq/raw_writer.rs b/rust/kernel/block/mq/raw_= writer.rs deleted file mode 100644 index 0aef55703e71..000000000000 --- a/rust/kernel/block/mq/raw_writer.rs +++ /dev/null @@ -1,56 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0 - -use core::fmt::{self, Write}; - -use crate::error::Result; -use crate::prelude::EINVAL; - -/// A mutable reference to a byte buffer where a string can be written int= o. -/// -/// # Invariants -/// -/// `buffer` is always null terminated. -pub(crate) struct RawWriter<'a> { - buffer: &'a mut [u8], - pos: usize, -} - -impl<'a> RawWriter<'a> { - /// Create a new `RawWriter` instance. - fn new(buffer: &'a mut [u8]) -> Result> { - *(buffer.last_mut().ok_or(EINVAL)?) =3D 0; - - // INVARIANT: We null terminated the buffer above. - Ok(Self { buffer, pos: 0 }) - } - - #[expect(dead_code)] - pub(crate) fn from_array( - a: &'a mut [crate::ffi::c_char; N], - ) -> Result> { - Self::new( - // SAFETY: the buffer of `a` is valid for read and write as `u= 8` for - // at least `N` bytes. - unsafe { core::slice::from_raw_parts_mut(a.as_mut_ptr().cast::= (), N) }, - ) - } -} - -impl Write for RawWriter<'_> { - fn write_str(&mut self, s: &str) -> fmt::Result { - let bytes =3D s.as_bytes(); - let len =3D bytes.len(); - - // We do not want to overwrite our null terminator - if self.pos + len > self.buffer.len() - 1 { - return Err(fmt::Error); - } - - // INVARIANT: We are not overwriting the last byte - self.buffer[self.pos..self.pos + len].copy_from_slice(bytes); - - self.pos +=3D len; - - Ok(()) - } -} --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 9A4E927B342; Fri, 22 Aug 2025 12:15:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864937; cv=none; b=HE3S6UHx0PgejcUn+vvz1CqcK+CY3pF2cbj+dstV/esZeenU7LkTBX+Wk+kwAt6sRZ7gm9h9D5BScvNY6pst7ZS1AstNfaq4cMw9V0LORAjH9iLk7nVX/qqm5UCW9uMlp87HvNMP9iXAecwo+FWGRLpZUIJEfqdu7NfEGz/PkVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864937; c=relaxed/simple; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bIkOuXusU/aTtBCuHru9Ue8MIhMcCakJKK6peO9PDSlmOxKHUKNKAjcfj7YuuOP7l0eZinIhprqkodHRd222PSeL4fHwAG4dFgtoCUQ2PDyq01jczva7iVOCpEY+iStcZNk3aWuIEs6p/I98DmztSWdSPS/VqdyisxOYhNtfF00= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mYCC//FR; 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="mYCC//FR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 770F6C4CEED; Fri, 22 Aug 2025 12:15:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864935; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=mYCC//FR8V8cZUti5WHy2kpH5EUTxhD+UlAfCBG8k3Rx+sFsOvDfTcexFXVqgrESZ MEp/kFzVNtlza/TyOaCXUPFoUm4sbLXF8avrGeJDT3bHMnw7tm4rdAP+lAdqVxliIJ c6EJLL00FaJ+vjU1AEiIWf7GotMYi140WBI+A4z87Xy+BNXiqt3UmW2s+Gqb4m2Xk6 aqEI2qxcSMLVu+O/ZTu1bFiQrhXAoLhQlDr4JKWHSbTxvCETgV1P6vFPxJb6x48O41 k+sZy2w2hGc9shHW1YydUJvwXa//D1IYCbdOF8XDlCi1czGFz0zWdfk8S9zY723Z3L KNU6heLoKrZ/w== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:47 +0200 Subject: [PATCH v6 11/18] rust: block: remove trait bound from `mq::Request` definition Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-11-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=963; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=OfpvNSrrUeZHTWGA2qHRRXa1nwQ1xGGBIsoZwm6M11A=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF87ZTCo8Y+0upwu+bueiC9OFcHfiqh3v1P29 IbqTUVgBy6JAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfOwAKCRDhuBo+eShj dwS+D/0eiPktBwS1xwnUslpGJQUXrEkaYlXdV2dxbIahQ45Fu3stw92O101FyaS5abHHqgXyfQ+ d7s72X0Zz8wchnQlB+0MnFDgwQ+FE5pFoVCnM2J5I0YHetEQGZ//BHFUioaRV7QpjdmabJblx+p E+DUmUVbI/S60PahhZSR/+JpfyAQDX3a2EyzLO3oh+fQ9kIrC3XXuHvCtlg0eWkABETNWV0H12a mOrer3SwhSqvBpXj35MxznHNnZSVIPo3fj2nSka6WFCHxhUJqJDEvgODbzgOFIMc+uFw60qN9Hp uBzjFmHeeXiqrUuX5M9HBsJNiANxDaDdhGylaMgFpYVVggMMJ9JKY5Ji+QTr0qG3u5mcg1IRK+A kaW2J16t+VqZnYAeqFb3K7GRtDunN94cgFVDTfPfYHQizKZvik40Oqfik2Pt4lDzP5VN7GEOglX yLZvwkTzQsXfpQFY9ByUA0rOVx+0FzfJ7kgA0jDJ3PhF71c3gS4tniarMiXlhqEswWaGHf/SwKi 5JWxK+utV/XoQEoyGpfOHlPR/ldcnTbqzPogdSbwKpJIyhGDxLDsol+UK9fiDAU7KqyPGpaa0Vl VIoLX3BB3crUAER1nktwrxg0ITeEiKpnLV+p9C5Jxph48ufUkorMGtet/WMPo6UsMP37/T00KN+ A2jVFhqa8vDpJGw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Remove the trait bound `T:Operations` from `mq::Request`. The bound is not required, so remove it to reduce complexity. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index fefd394f064a..f723d74091c1 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -53,7 +53,7 @@ /// [`struct request`]: srctree/include/linux/blk-mq.h /// #[repr(transparent)] -pub struct Request(Opaque, PhantomData); +pub struct Request(Opaque, PhantomData); =20 impl Request { /// Create an [`ARef`] from a [`struct request`] pointer. --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 88C332FB602; Fri, 22 Aug 2025 12:16:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864961; cv=none; b=KweuvthBxUnOepmYyouTqrUtkQARnTqC6K9JL4VbAUCrLJTgPlbyGiSqUAl6AI7R9pX9AjOtS4H4Ldl6ovC/g2Wl6CeBigj4/BcBWZ6Fo0HLW6HslgP9aiq9Ngs5bZ5tqLeLArUvgPRukickaVUlDUxn2exSzDhcMEF8G07In3s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864961; c=relaxed/simple; bh=pzI+PzwBdiJXG7Rnf/MUlf5BoxNaAVR/4KH77t5lHuM=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=F2K2tOq0JKl7BiqHkIeiw51r1T8Ajkh70YJD/H1vJHvau/jKazLAQkeBqU3kczVeXEU76ay2HBwZ0MsMzjZhkBP03B26vkfkWCv5a4LpVLo4N+aSXj4UJKXyaneSbMlN5O5n66gSHAX0k9Iigqmb9Ffohr37IkE2jaJXGtPZz5w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eugPQauq; 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="eugPQauq" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35A04C4CEED; Fri, 22 Aug 2025 12:15:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864961; bh=pzI+PzwBdiJXG7Rnf/MUlf5BoxNaAVR/4KH77t5lHuM=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=eugPQauqg9s+DD4CckkQbzlLP6GQoWAYapYakDQwI813GtNJNLD6+HbFC8OvY6G4k whaZnS3bhuC0qC4OBeEHezW6Z1vsvseL3yf21h/GvkukmlcJhZi5i+r33XElg8R6w5 STChSaAiywon/8S883fKnxUSUB+knJwSZlRgCfRnfulOWsF9ekZ4Mii/vMUNYtTrcw cM+N+DbuqxOBrJc0nWsmKhNK+GhXPGZQbBffe5YA78vkKmT2w4TfF8uEpwrJtiDcF+ 3FFBbkWSZGrb/2NmL9xeAXJ+4Z1twB2fSyMslO3BdStXmqK0ARFsi6vQUN0VzhEtm0 Tmv8W9i3KTIbA== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:48 +0200 Subject: [PATCH v6 12/18] rust: block: add block related constants Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-12-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1050; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=pzI+PzwBdiJXG7Rnf/MUlf5BoxNaAVR/4KH77t5lHuM=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF88vYZZFUYtq6sIQRW6685mmouPx6UDAXTUz URQ6DhtdBaJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfPAAKCRDhuBo+eShj d7M+EADFxD2piNMvveM7zDctM76JNI59SOxhxT6ZlWjPWmPWx5CCZTUK/rCIQ0kAm2TgGGM2rma cDpyV9InTXTn3TTIltEvKKTDk6Y3JeRARm1hfw961YFsZSFlqBJwoMbk/MFvkpFk+jvs3Yg+TBa MdoFL/4dh/7h6R1qRYD2Cfqn4v7CozNWUeQyhwUSg/hWJ/ioPnAn3VPonhFiVySWOt1zeYSi0/m wIf/fBzfqQll9OXmgVaiz/W+VGYKW+cYvt+OM6p7UWsfaCDZ4nkxygRvQSnBaySkfx2ZRIxR85c dw4fKYHjojxzYii4CP8soLiZrT5kpzntUn8ZMYE1yLCl3Sn3IvQOstQwTu9H9uKwj+7VjCwEcz+ ead+sErcQMb7r9PpD1ZPZ0lYb6zXfDOCevOnBYMJ3TtolipM4DaqvgLyKpd/vsWhgLsEXIqBoeP 89hHIG3j+Oez4ykDwcl2AchPA2SDbhE8YWv47adWvZtGgQrm4thwAjJRqhpG4nv0zqm+K0ef9r/ nJIf3+rf9WChUCsIXT4/FNO/P6SvT+DxhxnYtrCyQJTZU/2aKq1ZrkbWoU8/cP7jlYfJGcgQL7H QzeNx8rOEJGzh6Yrwyy0QqtO/oxTRXJuFh1TI4RACJPoVe08wPuliPxBEmInMOpOm9oKNkCcz+H f8QKbiuohVn2LyQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add a few block subsystem constants to the rust `kernel::block` name space. This makes it easier to access the constants from rust code. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- rust/kernel/block.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rust/kernel/block.rs b/rust/kernel/block.rs index 150f710efe5b..32c8d865afb6 100644 --- a/rust/kernel/block.rs +++ b/rust/kernel/block.rs @@ -3,3 +3,16 @@ //! Types for working with the block layer. =20 pub mod mq; + +/// Bit mask for masking out [`SECTOR_SIZE`]. +pub const SECTOR_MASK: u32 =3D bindings::SECTOR_MASK; + +/// Sectors are size `1 << SECTOR_SHIFT`. +pub const SECTOR_SHIFT: u32 =3D bindings::SECTOR_SHIFT; + +/// Size of a sector. +pub const SECTOR_SIZE: u32 =3D bindings::SECTOR_SIZE; + +/// The difference between the size of a page and the size of a sector, +/// expressed as a power of two. +pub const PAGE_SECTORS_SHIFT: u32 =3D bindings::PAGE_SECTORS_SHIFT; --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 4A6183009DA; Fri, 22 Aug 2025 12:15:39 +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=1755864940; cv=none; b=VFbCdiUJJeunltveCIqcIB30B/pifVQ+6Rdi8AkBFSwOsHWHUoTJFyBPDyXpYtozsxSjvSRcWfSs5DI1BonnNCsH+e9vCiWnPBlFYkM5DbiZ+ok2FKNc/JsaHnzVp0k9jJq1U9cxUtGkTxHyXZvDlWaus1ntOc7I7lYoUYR5DKY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864940; c=relaxed/simple; bh=pIuBZvrIaCfOxk5R8QT6fU57Jtv4V7ScNiWvZ/2peFE=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=bp17TVpEDQxfFDOxfeSBs+EkQ++RNnvH+QVFGQQh1OUgt9jLb1YZArUqmL0siaR7JHmeo1mkdrDVcPnIYqHtQmtfyV+o3o08Tb5X27B2zl7i9B+7n/0/ZgteSzRbp+wID9yQI1li8UO33eabOaxTQ5zzUguhLjcCW5uvmBCWN0s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=rSd+jrfY; 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="rSd+jrfY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 86B2CC113D0; Fri, 22 Aug 2025 12:15:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864939; bh=pIuBZvrIaCfOxk5R8QT6fU57Jtv4V7ScNiWvZ/2peFE=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=rSd+jrfYFV5sW5ZHN15MdlosaaP5RKDfXXjcYs35eYY+tzxYF6zKhp7OsBQKLDdaz eZwtaJh4IR9JAD1SNQukCbLFZgSm1Ao5IuEgkJA66YfQRLRTr3RLRuhxIFCkX/1VR9 fS0b2sUDoEw5pQ3+0NrsSCDmpFjs/Whu0QfZeGCIyfsVmVWUsV+bYhZ8zDS5GnY6oP vT7NZmb84gIyOrlr6hqIfKgTPP6duwTpVea/ubfEXZHmdq1mOXN8quQ0vw5agYZkdr UbQtHFJqEm3dBUEHIvXxDdAUVW0y6PYwgr2DXDLZxA/KdL0Ci3b3dunk8Kg6qKTRM7 IPevpiYDSuJ2g== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:49 +0200 Subject: [PATCH v6 13/18] rnull: move driver to separate directory Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-13-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3780; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=pIuBZvrIaCfOxk5R8QT6fU57Jtv4V7ScNiWvZ/2peFE=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF89GOSg2qFj2udURlTwvSVv2liyoM3WDOLhl v8N4YeKOXWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfPQAKCRDhuBo+eShj d0bED/9uBA/z+Yy0POTVykqZ537WGT9ztvQilq8u2+Ai3d8yzTH5Krr02S4mdyFwsf4wUguhMmL fLv76qn0kC5TvT5VfRajnrQdBLZRY1Jw6j06n4LG/Bw4g5wtAaci9iXmtVlhWuS/ydHqKEMskLI ARZgXKz2+ot+ucaE3X9Yn2B7qyDkp/WBqPXQRC79XDdyYf922d2UcMVkhP6vkpAjhtDJd0jb96y fPHFXOZ7IK8hdmZLI9G8G21jt9psa/MneXoCt/8vvjrEG0Rtfm4t/CSSOzY4yb3LfN29Vp72uDG 6DZ1ANddIM9VooXMiPSyBbz2RoJTPU6Un6u5dvDHWE5AmSejAQTECnKTe8FeuYVm8A3Aa/2IUtg YzOCr1N7Z3HNVuL/4ekdVsah4WCTByqofL4GoDkEuvf6tPhGrHRH3O6zgtK9UH5ibOEbl5TSdAh kfumZcxXFrpqFmDSZeVdP9RSAv5/0JmyuuKIsrkZ/FuDlupCE4tU5+nGOpLey64lAqrmjhQ8sLW dHjgvpUNX7KuZw/Na3sGmELVE6xtMCwP7EUYNzbeU5ujTqnDsrGZTzqcX5lRByIvl+wbklymWta 4OCV2Ad9cKa2JQFVeWfxOjhu1ToRVoUAZifc9Vd72KYPkSwAdobXxhfbgDXiBEKnRHt3TcVpUH2 6QVem7vFQOUnGlQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 The rust null block driver is about to gain some additional modules. Rather than pollute the current directory, move the driver to a subdirectory. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- MAINTAINERS | 2 +- drivers/block/Kconfig | 10 +--------- drivers/block/Makefile | 4 +--- drivers/block/rnull/Kconfig | 13 +++++++++++++ drivers/block/rnull/Makefile | 3 +++ drivers/block/{ =3D> rnull}/rnull.rs | 0 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/MAINTAINERS b/MAINTAINERS index daf520a13bdf..d5addcc4b132 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -4343,7 +4343,7 @@ W: https://rust-for-linux.com B: https://github.com/Rust-for-Linux/linux/issues C: https://rust-for-linux.zulipchat.com/#narrow/stream/Block T: git https://github.com/Rust-for-Linux/linux.git rust-block-next -F: drivers/block/rnull.rs +F: drivers/block/rnull/ F: rust/kernel/block.rs F: rust/kernel/block/ =20 diff --git a/drivers/block/Kconfig b/drivers/block/Kconfig index df38fb364904..77d694448990 100644 --- a/drivers/block/Kconfig +++ b/drivers/block/Kconfig @@ -17,6 +17,7 @@ menuconfig BLK_DEV if BLK_DEV =20 source "drivers/block/null_blk/Kconfig" +source "drivers/block/rnull/Kconfig" =20 config BLK_DEV_FD tristate "Normal floppy disk support" @@ -311,15 +312,6 @@ config VIRTIO_BLK This is the virtual block driver for virtio. It can be used with QEMU based VMMs (like KVM or Xen). Say Y or M. =20 -config BLK_DEV_RUST_NULL - tristate "Rust null block driver (Experimental)" - depends on RUST - help - This is the Rust implementation of the null block driver. For now it - is only a minimal stub. - - If unsure, say N. - config BLK_DEV_RBD tristate "Rados block device (RBD)" depends on INET && BLOCK diff --git a/drivers/block/Makefile b/drivers/block/Makefile index a695ce74ef22..2d8096eb8cdf 100644 --- a/drivers/block/Makefile +++ b/drivers/block/Makefile @@ -9,9 +9,6 @@ # needed for trace events ccflags-y +=3D -I$(src) =20 -obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull_mod.o -rnull_mod-y :=3D rnull.o - obj-$(CONFIG_MAC_FLOPPY) +=3D swim3.o obj-$(CONFIG_BLK_DEV_SWIM) +=3D swim_mod.o obj-$(CONFIG_BLK_DEV_FD) +=3D floppy.o @@ -38,6 +35,7 @@ obj-$(CONFIG_ZRAM) +=3D zram/ obj-$(CONFIG_BLK_DEV_RNBD) +=3D rnbd/ =20 obj-$(CONFIG_BLK_DEV_NULL_BLK) +=3D null_blk/ +obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull/ =20 obj-$(CONFIG_BLK_DEV_UBLK) +=3D ublk_drv.o obj-$(CONFIG_BLK_DEV_ZONED_LOOP) +=3D zloop.o diff --git a/drivers/block/rnull/Kconfig b/drivers/block/rnull/Kconfig new file mode 100644 index 000000000000..6dc5aff96bf4 --- /dev/null +++ b/drivers/block/rnull/Kconfig @@ -0,0 +1,13 @@ +# SPDX-License-Identifier: GPL-2.0 +# +# Rust null block device driver configuration + +config BLK_DEV_RUST_NULL + tristate "Rust null block driver (Experimental)" + depends on RUST + help + This is the Rust implementation of the null block driver. Like + the C version, the driver allows the user to create virutal block + devices that can be configured via various configuration options. + + If unsure, say N. diff --git a/drivers/block/rnull/Makefile b/drivers/block/rnull/Makefile new file mode 100644 index 000000000000..11cfa5e615dc --- /dev/null +++ b/drivers/block/rnull/Makefile @@ -0,0 +1,3 @@ + +obj-$(CONFIG_BLK_DEV_RUST_NULL) +=3D rnull_mod.o +rnull_mod-y :=3D rnull.o diff --git a/drivers/block/rnull.rs b/drivers/block/rnull/rnull.rs similarity index 100% rename from drivers/block/rnull.rs rename to drivers/block/rnull/rnull.rs --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 9F39430E0E3; Fri, 22 Aug 2025 12:16:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864986; cv=none; b=MnEDcqdXFjmUgI09p9QsAYhm/TqFB2l27p0q1A51j70uydaEOkO7tGrCJ6hvM4Tvv7V3nbg3jDRD3EoFgff85/Ab/m3CaQGrOdpPmTg7aUdn1TC8mr503AgQJdvZQgoQNXAxdowF2ujM/kFgaN5QDzjydz7WCNCgFCgW9cfe48U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864986; c=relaxed/simple; bh=iW4yDCdWglsE+9DJN1QwjcjeXTNvaRhTe3qyZwnbuV4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fBaqpQiGtbk79AOTkIMAxKfDK7HVdzFHVVyzCZX92aHloNKBv6AGWjCPv9KvQTco+/RFAIfQgBdIW6RWnAHDZD3diqrurgAvUQs0Vlv0eIhlJIU5shjEnWRMBxtBla6ybxeTg6udKJAL85FmTeBqzZRVsFpgcEObcDW88/tmdBE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=F5kSOHAj; 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="F5kSOHAj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 59C9DC4CEED; Fri, 22 Aug 2025 12:16:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864986; bh=iW4yDCdWglsE+9DJN1QwjcjeXTNvaRhTe3qyZwnbuV4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=F5kSOHAjTL69SW+2oyROThVneKvWw7if+tPNMyZXHwQ6Z5VD9V+5UaKyBBhjIReTR 9zAp4ytX7gJxRmH5XYMuZ6nvHqZchXw/kVFtRJ9AfSVGESxCnBV7TNMsx4eDH2GKWY PzGjGv0J3fz3LGWfvtE7DvfAD0o3eBVIxArdGxiL0hzpStSztVRo7/+Z5jLy3/Jxh3 ldKDSCN0CXKcBIoWgUzAL1rOqKFrc0Iu15rGUZgrp5ZCkhQfLCIWyDWkssEMn78TGo Ar7c/uMmyGGzFUGbr26ofr6CADJaIxH+COk80f046Np5bwBi/sXAso7dKkaglGdKt4 n1c0MvPiuNAMA== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:50 +0200 Subject: [PATCH v6 14/18] rnull: enable configuration via `configfs` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-14-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=10549; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=iW4yDCdWglsE+9DJN1QwjcjeXTNvaRhTe3qyZwnbuV4=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF8+c7BLemjjK/8SdYg0VzN5/42FjSr7yUTRF ZkmkF3eeYmJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfPgAKCRDhuBo+eShj dzTiD/0bjy0NPt6q3dRTSYNJfq2FpMzH1mamhEZY51Me13GfCRS/zuUaf9C1Y9SQ8FAhkNqAKp9 I/Lw8iG2u0NlfW/om/LXRyrfD5jECPNQlcKV1dYQFAEEM63fAcUvkc9MZoGs19J8nofTQTKFBhV DAkaIgRlfPMqEpW4pvn0Y56zY2t0tt2I3DwbqqPiRhXrwO1WW2XlfZpc1NfH1Tnnq8PXC/bmv/N LR1+Y1/3poZHA7NzKevcwqyRZDYnWUQU9mFwe0dHNLvrwex2cY+gjMm13H9uZxpxUQQs4eCLgAN aNwrW7DKxgfCfxxoJVQ/LClwI4YPc3pu1S1d6mdl4dy1ER4NOQExMzs4s+oFiXvhgs3FiO24hpr 6sJxE8LTznVSd+798V26w3+J2UamOvwi8rvvJVC4GC4OGMG8gv819/L5uW4pwElAJ+J+44XJSQ5 dsO6ckx/ZdyMkZxDKp7okXmtvD6gvZfSeYZregLx+Hzxv1P1/fDEm9TqbX/MUUEGnWlwHEi2p4Z 2nBr6+7+m/ieSS0tKQzPq22YrKCNvP9uszrKGTQogLEaqANG6nW60cjMkUXpHIPEEYAijy36APy LBi/rYhDA04iBsG+om5/DcXe2IAxxhabufPlfPoWu01zulxiOQgp29VXl+HBMrGDdi2sYPQRdJA qhcqYTuABrK8xFQ== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow rust null block devices to be configured and instantiated via `configfs`. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- drivers/block/rnull/Kconfig | 2 +- drivers/block/rnull/configfs.rs | 207 +++++++++++++++++++++++++++++++++++= ++++ drivers/block/rnull/rnull.rs | 59 +++++------ rust/kernel/block/mq/gen_disk.rs | 2 +- 4 files changed, 240 insertions(+), 30 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..46710a1e1af4 --- /dev/null +++ b/drivers/block/rnull/configfs.rs @@ -0,0 +1,207 @@ +// SPDX-License-Identifier: GPL-2.0 + +use super::{NullBlkDevice, THIS_MODULE}; +use core::fmt::Write; +use kernel::{ + block::mq::gen_disk::{GenDisk, GenDiskBuilder}, + c_str, + configfs::{self, AttributeOperations}, + configfs_attrs, new_mutex, + page::PAGE_SIZE, + prelude::*, + str::{kstrtobool_bytes, CString}, + sync::Mutex, +}; +use pin_init::PinInit; + +pub(crate) fn subsystem() -> impl PinInit, Error> { + let item_type =3D configfs_attrs! { + container: configfs::Subsystem, + data: Config, + child: DeviceConfig, + attributes: [ + features: 0, + ], + }; + + kernel::configfs::Subsystem::new(c_str!("rnull"), item_type, try_pin_i= nit!(Config {})) +} + +#[pin_data] +pub(crate) struct Config {} + +#[vtable] +impl AttributeOperations<0> for Config { + type Data =3D Config; + + fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_str("blocksize,size,rotational\n")?; + Ok(writer.bytes_written()) + } +} + +#[vtable] +impl configfs::GroupOperations for Config { + type Child =3D DeviceConfig; + + fn make_group( + &self, + name: &CStr, + ) -> Result, Error>> { + let item_type =3D configfs_attrs! { + container: configfs::Group, + data: DeviceConfig, + attributes: [ + // Named for compatibility with C null_blk + power: 0, + blocksize: 1, + rotational: 2, + size: 3, + ], + }; + + Ok(configfs::Group::new( + name.try_into()?, + item_type, + // TODO: cannot coerce new_mutex!() to impl PinInit<_, Error>,= so put mutex inside + try_pin_init!( DeviceConfig { + data <- new_mutex!(DeviceConfigInner { + powered: false, + block_size: 4096, + rotational: false, + disk: None, + capacity_mib: 4096, + name: name.try_into()?, + }), + }), + )) + } +} + +#[pin_data] +pub(crate) struct DeviceConfig { + #[pin] + data: Mutex, +} + +#[pin_data] +struct DeviceConfigInner { + powered: bool, + name: CString, + block_size: u32, + rotational: bool, + capacity_mib: u64, + disk: Option>, +} + +#[vtable] +impl configfs::AttributeOperations<0> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + + if this.data.lock().powered { + writer.write_str("1\n")?; + } else { + writer.write_str("0\n")?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + let power_op =3D kstrtobool_bytes(page)?; + let mut guard =3D this.data.lock(); + + if !guard.powered && power_op { + guard.disk =3D Some(NullBlkDevice::new( + &guard.name, + guard.block_size, + guard.rotational, + guard.capacity_mib, + )?); + guard.powered =3D true; + } else if guard.powered && !power_op { + drop(guard.disk.take()); + guard.powered =3D false; + } + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<1> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().block_size))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + GenDiskBuilder::validate_block_size(value)?; + this.data.lock().block_size =3D value; + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<2> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + + if this.data.lock().rotational { + writer.write_str("1\n")?; + } else { + writer.write_str("0\n")?; + } + + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + this.data.lock().rotational =3D kstrtobool_bytes(page)?; + + Ok(()) + } +} + +#[vtable] +impl configfs::AttributeOperations<3> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().capacity_mib))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + this.data.lock().capacity_mib =3D value; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index d07e76ae2c13..8690ff5f974f 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -1,28 +1,25 @@ // 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 +32,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), GFP_KERNEL)?; + + gen_disk::GenDiskBuilder::new() + .capacity_sectors(capacity_mib << (20 - block::SECTOR_SHIFT)) + .logical_block_size(block_size)? + .physical_block_size(block_size)? + .rotational(rotational) + .build(fmt!("{}", name.to_str()?), tagset) + } +} + #[vtable] impl Operations for NullBlkDevice { #[inline(always)] diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 20f1d46c774d..6b1b846874db 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -51,7 +51,7 @@ pub fn rotational(mut self, rotational: bool) -> Self { =20 /// Validate block size by verifying that it is between 512 and `PAGE_= SIZE`, /// and that it is a power of two. - fn validate_block_size(size: u32) -> Result { + pub fn validate_block_size(size: u32) -> Result { if !(512..=3Dbindings::PAGE_SIZE as u32).contains(&size) || !size.= is_power_of_two() { Err(error::code::EINVAL) } else { --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 891BF307AF6; Fri, 22 Aug 2025 12:16: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=1755864974; cv=none; b=nltOQIPqNKhdzExN5DELj9df6F8t302dzqIukX2Yz2CTaaiBeNvatMrjC6wDh2CU4Jd0b8xR28prpVYii+qGqqhXypNY3AUHJciZWbER4HSmu/7lTruy9kS3/iW7pgBNYWn6+KHVMyfYLHdBlKlgWJrWtx6S7Tc9VNu3CgfPAN4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864974; c=relaxed/simple; bh=oVrJO2lhm9L0Sbi6/GPoc9gGsUrYJTTM/W2HQClLWbw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Th20tyGhVQCxAEkKqbE+v67iaOJKqKbLf6TIGzojMOXvEyEe9PDA51kp93IsSVxhmgs/bv9PGS9EsnQcRCmC3yzP/0EQZTQlB2kaB89Gk2e8XosZvNCzM15uz1WPCwoVk4HUQgxc0mPb10VHR0uvSl12qXNY9PQUdug7uWZx11I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Mvo/RLS4; 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="Mvo/RLS4" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7AAEC4CEED; Fri, 22 Aug 2025 12:16:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864974; bh=oVrJO2lhm9L0Sbi6/GPoc9gGsUrYJTTM/W2HQClLWbw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Mvo/RLS4qEBmQoE8Af+sWOriQ8og3j4MpU5scFyt76RNhnHXnL8agMHCi2nyJdb0D HLpMJGcpvivQWmGBylOhGssY7168JRD9Ku7mfijDM156ymBmxRL1WAOoQtvO3qwnzu VhF2iOvVj6++Fbo3dmvdHNKy0/DBv3kkqy/Ghktvu0e5mEFswI3aTsaBRW0OWi5Pvm WRYVsbankp7v36Rx7gmv7qcZZB27uJrkZV0mmYM1SuaV2FAnbzOokjihigrQxXZMCJ B7knQKMAmVGFFYaNwO+1PiFpQopt/HzCNj595S/COAt+iQ0Rr+k/Z1bhzMtNTFwttq u452Bb6+9PcDA== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:51 +0200 Subject: [PATCH v6 15/18] rust: block: add `GenDisk` private data support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-15-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=10569; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=oVrJO2lhm9L0Sbi6/GPoc9gGsUrYJTTM/W2HQClLWbw=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF8/gB1uemMPaDCRHdOfwELmbGlj5G25qXWBe q/sEMS147GJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfPwAKCRDhuBo+eShj dye5D/9eXJ5djqzvqkRSHjpPO9xJO6wULCPyxeexf9/6ZMxoNYNmXwzupakFKEnjZZ+zmDd+51Q iSkklEZUmzsHGslfGxDmRi3HkyWUct0NA6sA++osT4tIO7NMn4f9K74ym/Iz1BNK27PLywXb29t CdvGiFs7s9O6bLfhKJp9PtncNafkatZ3y3V3mWqYtr7IY6tjf8tpVk8hNcbgYm9mNeWfoyy3nxz 2RdTbuPYPVTxVzkvpSX+P3PYdduOQnkR9qTaYnPAsc42s1ttHMQJK9Q2hWtsNEdS7hlBuf9Ka7m VHmhmTByS8+ZMle0XQ0B+ZiNZ/guVQ4NYQYQFSCExwSBpugwwtHdRhazp52CcCk4pXgCDbpNDV1 Rw8oggAHRv4lscBnZs5mf2ycnDQ/z7U6yE84Pu5rEwdYDeURXODc8FmOX6CBczdeFKCwZGmMs/B u7hscpDKADFz8kp0bvk6Hf5S8ZdY+guSOnkDTVBLrcW1hLYOLyrJho5x/qUlIBdZkSyVys6VrA2 dsZ0CuvPmxnfXRCMUt0ypwbpiqlIvVGLA+leLtKHMCAa071kDgFMkw0DhTAE1+3bpqeayYhfjFT gcY7a7lr3MUF/FcZ08O68dQY1WibEoBWkHS2rcYUlBKn4MbDNi7eXX4JG+QJ+SMSgpKXdeFsjdN +5Y9/5FnlQVM/Mg== 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. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg Reviewed-by: Daniel Almeida --- 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 8690ff5f974f..8255236bc550 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -61,14 +61,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 @@ -79,5 +81,5 @@ fn queue_rq(rq: ARef>, _is_last: bool) = -> Result { Ok(()) } =20 - fn commit_rqs() {} + fn commit_rqs(_queue_data: ()) {} } diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 98fa0d6bc8f7..6e546f4f3d1c 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -69,20 +69,21 @@ //! //! #[vtable] //! impl Operations for MyBlkDevice { +//! type QueueData =3D (); //! -//! fn queue_rq(rq: ARef>, _is_last: bool) -> Result { +//! fn queue_rq(_queue_data: (), rq: ARef>, _is_last: bo= ol) -> Result { //! Request::end_ok(rq); //! Ok(()) //! } //! -//! fn commit_rqs() {} +//! fn commit_rqs(_queue_data: ()) {} //! } //! //! let tagset: Arc> =3D //! Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?; //! let mut disk =3D gen_disk::GenDiskBuilder::new() //! .capacity_sectors(4096) -//! .build(format_args!("myblk"), tagset)?; +//! .build(format_args!("myblk"), tagset, ())?; //! //! # Ok::<(), kernel::error::Error>(()) //! ``` diff --git a/rust/kernel/block/mq/gen_disk.rs b/rust/kernel/block/mq/gen_di= sk.rs index 6b1b846874db..46ec80269970 100644 --- a/rust/kernel/block/mq/gen_disk.rs +++ b/rust/kernel/block/mq/gen_disk.rs @@ -13,6 +13,7 @@ static_lock_class, str::NullTerminatedFormatter, sync::Arc, + types::{ForeignOwnable, ScopeGuard}, }; use core::fmt::{self, Write}; =20 @@ -98,7 +99,14 @@ pub fn build( self, name: fmt::Arguments<'_>, tagset: Arc>, + queue_data: T::QueueData, ) -> Result> { + let data =3D queue_data.into_foreign(); + let recover_data =3D ScopeGuard::new(|| { + // SAFETY: T::QueueData was created by the call to `into_forei= gn()` above + drop(unsafe { T::QueueData::from_foreign(data) }); + }); + // SAFETY: `bindings::queue_limits` contain only fields that are v= alid when zeroed. let mut lim: bindings::queue_limits =3D unsafe { core::mem::zeroed= () }; =20 @@ -113,7 +121,7 @@ pub fn build( bindings::__blk_mq_alloc_disk( tagset.raw_tag_set(), &mut lim, - core::ptr::null_mut(), + data, static_lock_class!().as_ptr(), ) })?; @@ -167,8 +175,12 @@ pub fn build( }, )?; =20 + recover_data.dismiss(); + // INVARIANT: `gendisk` was initialized above. // INVARIANT: `gendisk` was added to the VFS via `device_add_disk`= above. + // INVARIANT: `gendisk.queue.queue_data` is set to `data` in the c= all to + // `__blk_mq_alloc_disk` above. Ok(GenDisk { _tagset: tagset, gendisk, @@ -180,9 +192,10 @@ pub fn build( /// /// # Invariants /// -/// - `gendisk` must always point to an initialized and valid `struct gend= isk`. -/// - `gendisk` was added to the VFS through a call to -/// `bindings::device_add_disk`. +/// - `gendisk` must always point to an initialized and valid `struct gen= disk`. +/// - `gendisk` was added to the VFS through a call to +/// `bindings::device_add_disk`. +/// - `self.gendisk.queue.queuedata` is initialized by a call to `Foreign= Ownable::into_foreign`. pub struct GenDisk { _tagset: Arc>, gendisk: *mut bindings::gendisk, @@ -194,9 +207,20 @@ unsafe impl Send for GenDisk = {} =20 impl Drop for GenDisk { fn drop(&mut self) { + // SAFETY: By type invariant of `Self`, `self.gendisk` points to a= valid + // and initialized instance of `struct gendisk`, and, `queuedata` = was + // initialized with the result of a call to + // `ForeignOwnable::into_foreign`. + let queue_data =3D unsafe { (*(*self.gendisk).queue).queuedata }; + // SAFETY: By type invariant, `self.gendisk` points to a valid and // initialized instance of `struct gendisk`, and it was previously= added // to the VFS. unsafe { bindings::del_gendisk(self.gendisk) }; + + // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build= ` with + // a call to `ForeignOwnable::into_foreign` to create `queuedata`. + // `ForeignOwnable::from_foreign` is only called here. + drop(unsafe { T::QueueData::from_foreign(queue_data) }); } } diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index c2b98f507bcb..6fb256f55acc 100644 --- a/rust/kernel/block/mq/operations.rs +++ b/rust/kernel/block/mq/operations.rs @@ -6,14 +6,15 @@ =20 use crate::{ bindings, - block::mq::request::RequestDataWrapper, - block::mq::Request, + block::mq::{request::RequestDataWrapper, Request}, error::{from_result, Result}, prelude::*, - types::ARef, + types::{ARef, ForeignOwnable}, }; use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ord= ering}; =20 +type ForeignBorrowed<'a, T> =3D ::Borrowed<'a>; + /// Implement this trait to interface blk-mq as block devices. /// /// To implement a block device driver, implement this trait as described = in the @@ -26,12 +27,20 @@ /// [module level documentation]: kernel::block::mq #[macros::vtable] pub trait Operations: Sized { + /// Data associated with the `struct request_queue` that is allocated = for + /// the `GenDisk` associated with this `Operations` implementation. + type QueueData: ForeignOwnable; + /// Called by the kernel to queue a request with the driver. If `is_la= st` is /// `false`, the driver is allowed to defer committing the request. - fn queue_rq(rq: ARef>, is_last: bool) -> Result; + fn queue_rq( + queue_data: ForeignBorrowed<'_, Self::QueueData>, + rq: ARef>, + is_last: bool, + ) -> Result; =20 /// Called by the kernel to indicate that queued requests should be su= bmitted. - fn commit_rqs(); + fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); =20 /// Called by the kernel to poll the device for completed requests. On= ly /// used for poll queues. @@ -70,7 +79,7 @@ impl OperationsVTable { /// promise to not access the request until the driver calls /// `bindings::blk_mq_end_request` for the request. unsafe extern "C" fn queue_rq_callback( - _hctx: *mut bindings::blk_mq_hw_ctx, + hctx: *mut bindings::blk_mq_hw_ctx, bd: *const bindings::blk_mq_queue_data, ) -> bindings::blk_status_t { // SAFETY: `bd.rq` is valid as required by the safety requirement = for @@ -88,10 +97,20 @@ impl OperationsVTable { // reference counted by `ARef` until then. let rq =3D unsafe { Request::aref_from_raw((*bd).rq) }; =20 + // SAFETY: `hctx` is valid as required by this function. + let queue_data =3D unsafe { (*(*hctx).queue).queuedata }; + + // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` w= ith a + // call to `ForeignOwnable::into_pointer()` to create `queuedata`. + // `ForeignOwnable::from_foreign()` is only called when the tagset= is + // dropped, which happens after we are dropped. + let queue_data =3D unsafe { T::QueueData::borrow(queue_data) }; + // SAFETY: We have exclusive access and we just set the refcount a= bove. unsafe { Request::start_unchecked(&rq) }; =20 let ret =3D T::queue_rq( + queue_data, rq, // SAFETY: `bd` is valid as required by the safety requirement= for // this function. @@ -110,9 +129,18 @@ impl OperationsVTable { /// /// # Safety /// - /// This function may only be called by blk-mq C infrastructure. - unsafe extern "C" fn commit_rqs_callback(_hctx: *mut bindings::blk_mq_= hw_ctx) { - T::commit_rqs() + /// This function may only be called by blk-mq C infrastructure. The c= aller + /// must ensure that `hctx` is valid. + unsafe extern "C" fn commit_rqs_callback(hctx: *mut bindings::blk_mq_h= w_ctx) { + // SAFETY: `hctx` is valid as required by this function. + let queue_data =3D unsafe { (*(*hctx).queue).queuedata }; + + // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` w= ith a + // call to `ForeignOwnable::into_pointer()` to create `queuedata`. + // `ForeignOwnable::from_foreign()` is only called when the tagset= is + // dropped, which happens after we are dropped. + let queue_data =3D unsafe { T::QueueData::borrow(queue_data) }; + T::commit_rqs(queue_data) } =20 /// This function is called by the C kernel. It is not currently --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 55BD22FB99D; Fri, 22 Aug 2025 12:16:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864965; cv=none; b=Ll1Zkyco5kGx+UA6gjeIcDexkkOOzsK2AlR7Cg389VYi+JoMWt0VECdmEWy/kACPFPKZMrzAAX67TuVrHiiYwrH/7bXCEfi9/2H9hO5dJhwJ/iQFFBcVfSkn/AylVt9G7uoydHKpyhWfG8aLJ9FRzT8uFU/2I6ANx80DyTY8Z38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864965; c=relaxed/simple; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=QUqqDyawgZlCyYnxRxZShHtDFczNR7W/5iQ/IaCY/xk4idlq2890vIYehuGT4qePDAtkysJiOApoNwWQnu4mu9IRj4Gmp7tanXtV6JVnHgw1owH20RZjaRMpJcUhNIwBtRhOYwNSDUsvLgh2sSidXf37mY8EuHt+HH4ZMciIvoE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=T794kpr2; 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="T794kpr2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id BC761C4CEED; Fri, 22 Aug 2025 12:16:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864964; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=T794kpr2h+gU9Uv15PtsimOUEZ5w3zHQF6j27zlDiYik6NM/38VGrNl/8moL9gFpW C38cPntho+JTr9GbjdCRUj/UYURlvdHIQGKIgaHtj4SYQoRuzQSx26yWVKt6pJEf2J u6MgGpAaNbZH1uqGBHq8ZhP2kHDEo+2QfloVhzcw/KIy3st5mbDcz2sYex3BKgL4KF XDzgJxMYNCeeGn4HLBoqBSPnhnFYD0Dn8+biXmsYMVFMiXCwbLIUDmWDvg+gNEOFCF q4iut4rH8hKNN18nWE+okOg31oy01ldqHYR+3DaTRH1lgYTzL6dbsT/3BG38nbu52a wPr2LrrFcz7Lw== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:52 +0200 Subject: [PATCH v6 16/18] rust: block: mq: fix spelling in a safety comment Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-16-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1036; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=hixYhOSo78Opw3JCXq3T7Ep1Lb7kHadQhoAfotnnjUc=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF9ANI4YO+smi7cQq/qeFb+af2TjjuNSdBh8n 4I/oe2FvVWJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfQAAKCRDhuBo+eShj dzhREAC+30PPeI2mX66s3bRMr5GopkanEZobESOXLAncwXZb8RxrE1NtTDv9UgiAqZQk8+m4emD MDj6gNfn0DZaSxub9ZcvzTaFpSdmIfofAphzsxnGyRto1pW8qJeR6WyoGj1AEqxvkQt6DuhNoNl peGcAYm5tR3grzsCS1Ry0hlkcsuRtqC7hUmEk+g/bY4S98AtcqyXG5wYA9aDC77dh7mln7av0v1 SSV7BpZlxI1fZyOP3/TF4pxJEldAXa9GRY9xj9fsY3Endt1EmM61eXvobZ2Sx7uxG2xwMLKFwwL 8yncrOJ3G60EgbyBmSRpqWopt+B3i4OX7osO8XLE6ZfoP/tbV6+RaHRCeWRZTBgiJFeX+DeemHc qm2U4D8Mx+v/WFdSCP+zCyAaBv4Xk5TFn0xC+WpuXP3y8p1uUjcwf3ekdzWfS9wmSUE9zT8b+KW qSYlbhjuFfbTQzaScveRtVij/DfoVBVGBxAi/E9NutOW5umb+09qXWFlIdoIgHCkYUbyloEKdhI 4VdMQEXO2Vz/w1CiDHPvCHgNdg2XpE5sWpWj03ZHvs+lzEQqGCN/OWLpVa6s9B7h3hgbRx4s5SE 4eApfSLrD1J+OjJwk8Bkpgw17jw7PEL7fjt8kYzWCre2Fm7pMFyrWrZbXLQ5CtrS+rcRzAXtE7U ow6f+/9flP5Na7Q== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Add code block quotes to a safety comment. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- rust/kernel/block/mq/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index f723d74091c1..3848cfe63f77 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -148,7 +148,7 @@ pub(crate) unsafe fn wrapper_ptr(this: *mut Self) -> No= nNull // valid allocation. let wrapper_ptr =3D unsafe { bindings::blk_mq_rq_to_pdu(request_ptr).cast::() }; - // SAFETY: By C API contract, wrapper_ptr points to a valid alloca= tion + // SAFETY: By C API contract, `wrapper_ptr` points to a valid allo= cation // and is not null. unsafe { NonNull::new_unchecked(wrapper_ptr) } } --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 2D8F53093A6; Fri, 22 Aug 2025 12:16:22 +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=1755864983; cv=none; b=Z638fDK+xfOR7LLB26qjXHNmkGPtTVmQU1A/ZuqJq5KYQ0gp44mQlRlrCXhgnDFbMP0c1d1/4dvWMEFyLjyTogObR954d/OnODMoWS9QgHk0lVuTbsfv5mbDn49hbX67nlQHkuILgvDnzc1JP4wk6TxB3jfnS0G078qiyljZNhA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864983; c=relaxed/simple; bh=/q3BHJ4OBUGrEvfMJAnS4b16x6NbLDJGWQVOq+Cb5uU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=LdUgJ8zCY5qamZySCrJ2IjftmMf9OIdmhn5X4N4DJLzEsJAntEChGSWKzXNOTlZKl1pVVRp7xb5EboBNRtZ3Ioa0QfTTDtSIjh6bJCZdJM+6vQ53DO48bJUmlc8ILk5OsWmci3EizgfEL6ZtZzJwXEJwuk4B1M0hd/GZv4w2hFU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RtMtEpoY; 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="RtMtEpoY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31164C116C6; Fri, 22 Aug 2025 12:16:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864982; bh=/q3BHJ4OBUGrEvfMJAnS4b16x6NbLDJGWQVOq+Cb5uU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=RtMtEpoYkzibvcasSDr1cZBGonwA9xeXLrKd6nlII8gUQVl/srmYCpDbcv3tFh+Fb 8SbhI+lfZZcfys2PHj1lZWphhfmtGGhQ8ziEGb+8xq02eNCl2NlrFtSD9XgEoqFl3h JmbnT2jcbrwaj5vNpjoamEZTfSGABYeSK4YumRL5vh75Vrzgl6PNTWk9ZsG5t7nJ1x vtZ/af/Hu+tRrFQybTd4N3yGhBuF1MGyYvZPg26Ca1OD5b/n5eaZUu//hcBSu9ldoj xQO9jL9NcYmfT3S93I6gQMwtIOKDnuRe0f/i04kk32XbMlIiTtygxvDjLeo//3YLw5 MUB1YCDswMI2A== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:53 +0200 Subject: [PATCH v6 17/18] rust: block: add remote completion to `Request` Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-17-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg , Daniel Almeida X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=5096; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=/q3BHJ4OBUGrEvfMJAnS4b16x6NbLDJGWQVOq+Cb5uU=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF9B0QbpmE+wvq7lNs2hawy9BgyH6y+HGY1UR /cZqI1GnLqJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfQQAKCRDhuBo+eShj d+yXEAClAdZfSjB1v1hZEBspjqmjPu3dBNcFRtAtw4vgeFTOO80iQLswPbq3WcZnw1Hcgj3FcrS MjqfMd6z6FToOwvxqKzKNgk8hItYfmvLH+80bnMSCUo7O/im7n+S3V0712mTC9Ov8o0GeBfGlpX pRClqhJlfqRFRGgoMWtRsC8ZTRAaEYzUwck9oRjeeQghp8p23XReudZcMsLeH6tgBnwmEHaFzrF FPHHxdOqvZM6tDQbBQ/6zk4LFoE8P6tFjeZW7KBpqVZRDZhmJchH8ZFDp4J9OmTJFGIFgAM2e0K +ZbAdbK/VIeVg+jp85clymPCMcpS5jEP0co6IHjU6mbZduDeKb7NlvXGQMH7G1Fvo1w3zdvvBpk ZMepeUa+SLCcCPXOFAKbUolS8lIk1UWrVixZFU8Dx3F2iUFzYf4FyJEDY6fhRvT1K86bX1dDuaS UK5H+cq+aM+UuWa3O5pgeRSAPLX6BFBxrDXtLH/m+evMRbd3HMwIKgccXSBQcFyijPDw9lksQN5 GsVowLd3qQr/9UaeuCEowUBe0dDR6smQUwTlWwEk5gYoIc0uTeHpYipEwMVQHJvaPHk8kQRLb34 rmlBPCWcgv/uh7Y9dpn20qlhDMYhlCxpA9h1CHhLl2YNZURVFVQQn/LEycI7gd9f4Tp/0ilgSgU NcFthhlq3DfMNnw== X-Developer-Key: i=a.hindborg@kernel.org; a=openpgp; fpr=3108C10F46872E248D1FB221376EB100563EF7A7 Allow users of rust block device driver API to schedule completion of requests via `blk_mq_complete_request_remote`. Reviewed-by: Alice Ryhl Reviewed-by: Daniel Almeida Signed-off-by: Andreas Hindborg --- drivers/block/rnull/rnull.rs | 9 +++++++++ rust/kernel/block/mq.rs | 6 ++++++ rust/kernel/block/mq/operations.rs | 19 +++++++++++++++---- rust/kernel/block/mq/request.rs | 17 +++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 8255236bc550..a19c55717c4f 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -82,4 +82,13 @@ fn queue_rq(_queue_data: (), rq: ARef>= , _is_last: bool) -> Res } =20 fn commit_rqs(_queue_data: ()) {} + + fn complete(rq: ARef>) { + mq::Request::end_ok(rq) + .map_err(|_e| kernel::error::code::EIO) + // We take no refcounts on the request, so we expect to be abl= e to + // end the request. The request reference must be unique at th= is + // point, and so `end_ok` cannot fail. + .expect("Fatal error - expected to be able to end request"); + } } diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 6e546f4f3d1c..c0ec06b84355 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -77,6 +77,12 @@ //! } //! //! fn commit_rqs(_queue_data: ()) {} +//! +//! fn complete(rq: ARef>) { +//! Request::end_ok(rq) +//! .map_err(|_e| kernel::error::code::EIO) +//! .expect("Fatal error - expected to be able to end request"= ); +//! } //! } //! //! let tagset: Arc> =3D diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/oper= ations.rs index 6fb256f55acc..0fece577de7c 100644 --- a/rust/kernel/block/mq/operations.rs +++ b/rust/kernel/block/mq/operations.rs @@ -42,6 +42,9 @@ fn queue_rq( /// Called by the kernel to indicate that queued requests should be su= bmitted. fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); =20 + /// Called by the kernel when the request is completed. + fn complete(rq: ARef>); + /// Called by the kernel to poll the device for completed requests. On= ly /// used for poll queues. fn poll() -> bool { @@ -143,13 +146,21 @@ impl OperationsVTable { T::commit_rqs(queue_data) } =20 - /// This function is called by the C kernel. It is not currently - /// implemented, and there is no way to exercise this code path. + /// This function is called by the C kernel. A pointer to this functio= n is + /// installed in the `blk_mq_ops` vtable for the driver. /// /// # Safety /// - /// This function may only be called by blk-mq C infrastructure. - unsafe extern "C" fn complete_callback(_rq: *mut bindings::request) {} + /// This function may only be called by blk-mq C infrastructure. `rq` = must + /// point to a valid request that has been marked as completed. The po= intee + /// of `rq` must be valid for write for the duration of this function. + unsafe extern "C" fn complete_callback(rq: *mut bindings::request) { + // SAFETY: This function can only be dispatched through + // `Request::complete`. We leaked a refcount then which we pick ba= ck up + // now. + let aref =3D unsafe { Request::aref_from_raw(rq) }; + T::complete(aref); + } =20 /// This function is called by the C kernel. A pointer to this functio= n is /// installed in the `blk_mq_ops` vtable for the driver. diff --git a/rust/kernel/block/mq/request.rs b/rust/kernel/block/mq/request= .rs index 3848cfe63f77..f7f757f7459f 100644 --- a/rust/kernel/block/mq/request.rs +++ b/rust/kernel/block/mq/request.rs @@ -135,6 +135,23 @@ pub fn end_ok(this: ARef) -> Result<(), ARef> { Ok(()) } =20 + /// Complete the request by scheduling `Operations::complete` for + /// execution. + /// + /// The function may be scheduled locally, via SoftIRQ or remotely via= IPMI. + /// See `blk_mq_complete_request_remote` in [`blk-mq.c`] for details. + /// + /// [`blk-mq.c`]: srctree/block/blk-mq.c + pub fn complete(this: ARef) { + let ptr =3D ARef::into_raw(this).cast::().as_pt= r(); + // SAFETY: By type invariant, `self.0` is a valid `struct request` + if !unsafe { bindings::blk_mq_complete_request_remote(ptr) } { + // SAFETY: We released a refcount above that we can reclaim he= re. + let this =3D unsafe { Request::aref_from_raw(ptr) }; + T::complete(this); + } + } + /// Return a pointer to the [`RequestDataWrapper`] stored in the priva= te area /// of the request structure. /// --=20 2.47.2 From nobody Fri Oct 3 23:02:53 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 E3DF93093A6; Fri, 22 Aug 2025 12:16:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864979; cv=none; b=NBQ4EQq7nRHI0dwLSZJuiJwp4MtCQteHW3hTlM56vf4sIl/f2PJqAAGqExQRzvtMoZyWO+/TJloc5x5dennQZhE4yvtqPgJv71QPAutjtLozF9hxYh2HvQS815M8OV2GGIP1BSy8a/eA6JBYhK9ysdhNGnFy5tRxCl3gkNRkmZs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755864979; c=relaxed/simple; bh=OzYLZcoR2X2ar16/GYPn46bn5FFs0TT9hSw7MAwyKy0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=lcn1kFuXOIZcIj4ked8+P/Nj3G57MhA2WkaD4bYCZ7up8hHCcAxNJpWzfD9HFsMjlO0O5f8lAquD+aiYEVwmWwI/9KzQk/B3+ZtynLc0HaNnOk+6NCTPok9l1Inwp1LyinSIkas/aWPS8ZAFWPQG6cm+zhGIQ2JMyQtR7FaxMqo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Po+xr1kJ; 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="Po+xr1kJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 12C51C4CEED; Fri, 22 Aug 2025 12:16:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1755864978; bh=OzYLZcoR2X2ar16/GYPn46bn5FFs0TT9hSw7MAwyKy0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=Po+xr1kJaQN1T2m1C8jqLNFgZsBbLa+lSQdi84iOJxr7Mr1W/8kPLLGK9OqwUoX4v UeQCeTxa+29nq6ApUVS6iCrRGxH0W4OjHIjpqx1iIjhOuzyxx8ZiFvm8uQaLHPq7nA FL+o9pJH6xMZuj4Vg18PXH7YYVt+S6W6l/3tEv6DLVYcmMVdLhaWlxMTPZRcut/h8w qXKxHmAIMArPKuEQpFfKr2Q8Lh/99Rwweoe/Ut7KmT/pJsJvhtWmOFd+4VTCAx3pjy YSl3vW2+KA5pdvY9ys7gXWj3piyg7rqSGsie2I4soxkUC0xD/AfVqj/YZF3BYZDeP4 2zZObObkvDKKw== From: Andreas Hindborg Date: Fri, 22 Aug 2025 14:14:54 +0200 Subject: [PATCH v6 18/18] rnull: add soft-irq completion support Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250822-rnull-up-v6-16-v6-18-ec65006e2f07@kernel.org> References: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> In-Reply-To: <20250822-rnull-up-v6-16-v6-0-ec65006e2f07@kernel.org> To: Boqun Feng , Miguel Ojeda , Alex Gaynor , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Alice Ryhl , Trevor Gross , Danilo Krummrich , Jens Axboe , Breno Leitao Cc: linux-block@vger.kernel.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Andreas Hindborg X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=6372; i=a.hindborg@kernel.org; h=from:subject:message-id; bh=OzYLZcoR2X2ar16/GYPn46bn5FFs0TT9hSw7MAwyKy0=; b=owEBbQKS/ZANAwAIAeG4Gj55KGN3AcsmYgBoqF9CEw3x/wP6LFyggIIgSviVsPb85GCgiEaau Mt5L6d3SSSJAjMEAAEIAB0WIQQSwflHVr98KhXWwBLhuBo+eShjdwUCaKhfQgAKCRDhuBo+eShj d/aEEACrZFNVeRnnwzbNU9bsVXC6SOQ9PeQzgDNcR5APm1K90vavLZtiVvFIYQMb1sIyREVkhDL nz0Cadgcb6ipuj1T+SF7BoXy6vAr8LKEjtqBgqguFErS2CmlqMg4/tirtPDjd7HLLS2CagQkyVv YAum7ai/cb2FRO+eN+Ps75/Yx7W15kLpUSmK1d7QCdxcrtmSkqCHYHN00JTVg5GV7IqYET8pGLM AmETYYUVuYb8d77ywUd7m4kx8bgO1WapFUfQ7UK3Iw7ioUNYP45+CiuQkol1f0QEqXWKePknGsa PhmTIvYm2nDYbJmC8ECNW+Fjn45WwWHj1FbIvuR+qIpnumvnwjZTmOmmrfY1lVj8uahi/57oJci uGscKuVGXLSJvT+neKvDf34vaiu9dB6Gqidgx14LCZsyfzhwEmY538/LpyeQek+7tmToC6Lq75J 8cONxUfT0mlMuJqxLuHzOl4JWg2qgWwZUSqMu6gwKLO5fAjiyiMbTvoF3BURdA27FwSea7LPqu0 6FKKC4ZRnuLEy4b0JAWDNbAhRA5/bSBzbZlZpTKK8PPhPG7xvC1a0F3gXlxGti2Zl8SWd3O+HNt bxykHH8so8hRAiV2J8gx5lcA9ZPQdXoV3SjoR0/nWzw5mFqS7CxrMS2RZvZ8BRszFy/8ekcW+V4 cvuKxOCotTtQ/pQ== 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. Reviewed-by: Alice Ryhl Signed-off-by: Andreas Hindborg --- drivers/block/rnull/configfs.rs | 59 +++++++++++++++++++++++++++++++++++++= ++-- drivers/block/rnull/rnull.rs | 32 ++++++++++++++-------- 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/drivers/block/rnull/configfs.rs b/drivers/block/rnull/configfs= .rs index 46710a1e1af4..8498e9bae6fd 100644 --- a/drivers/block/rnull/configfs.rs +++ b/drivers/block/rnull/configfs.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 =20 use super::{NullBlkDevice, THIS_MODULE}; -use core::fmt::Write; +use core::fmt::{Display, Write}; use kernel::{ block::mq::gen_disk::{GenDisk, GenDiskBuilder}, c_str, @@ -36,7 +36,7 @@ impl AttributeOperations<0> for Config { =20 fn show(_this: &Config, page: &mut [u8; PAGE_SIZE]) -> Result { let mut writer =3D kernel::str::Formatter::new(page); - writer.write_str("blocksize,size,rotational\n")?; + writer.write_str("blocksize,size,rotational,irqmode\n")?; Ok(writer.bytes_written()) } } @@ -58,6 +58,7 @@ fn make_group( blocksize: 1, rotational: 2, size: 3, + irqmode: 4, ], }; =20 @@ -72,6 +73,7 @@ fn make_group( rotational: false, disk: None, capacity_mib: 4096, + irq_mode: IRQMode::None, name: name.try_into()?, }), }), @@ -79,6 +81,34 @@ fn make_group( } } =20 +#[derive(Debug, Clone, Copy)] +pub(crate) enum IRQMode { + None, + Soft, +} + +impl TryFrom for IRQMode { + type Error =3D kernel::error::Error; + + fn try_from(value: u8) -> Result { + match value { + 0 =3D> Ok(Self::None), + 1 =3D> Ok(Self::Soft), + _ =3D> Err(EINVAL), + } + } +} + +impl Display for IRQMode { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + match self { + Self::None =3D> f.write_str("0")?, + Self::Soft =3D> f.write_str("1")?, + } + Ok(()) + } +} + #[pin_data] pub(crate) struct DeviceConfig { #[pin] @@ -92,6 +122,7 @@ struct DeviceConfigInner { block_size: u32, rotational: bool, capacity_mib: u64, + irq_mode: IRQMode, disk: Option>, } =20 @@ -121,6 +152,7 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { guard.block_size, guard.rotational, guard.capacity_mib, + guard.irq_mode, )?); guard.powered =3D true; } else if guard.powered && !power_op { @@ -205,3 +237,26 @@ fn store(this: &DeviceConfig, page: &[u8]) -> Result { Ok(()) } } + +#[vtable] +impl configfs::AttributeOperations<4> for DeviceConfig { + type Data =3D DeviceConfig; + + fn show(this: &DeviceConfig, page: &mut [u8; PAGE_SIZE]) -> Result { + let mut writer =3D kernel::str::Formatter::new(page); + writer.write_fmt(fmt!("{}\n", this.data.lock().irq_mode))?; + Ok(writer.bytes_written()) + } + + fn store(this: &DeviceConfig, page: &[u8]) -> Result { + if this.data.lock().powered { + return Err(EBUSY); + } + + let text =3D core::str::from_utf8(page)?.trim(); + let value =3D text.parse::().map_err(|_| EINVAL)?; + + this.data.lock().irq_mode =3D IRQMode::try_from(value)?; + Ok(()) + } +} diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index a19c55717c4f..1ec694d7f1a6 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::{ block::{ self, @@ -53,35 +54,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), GFP_KERNEL)?; =20 + let queue_data =3D Box::new(QueueData { irq_mode }, GFP_KERNEL)?; + 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