* [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback @ 2026-04-10 16:47 ` Wenzhao Liao 2026-04-10 16:47 ` [RFC PATCH 1/2] rust: block: mq: safely abstract the " Wenzhao Liao ` (2 more replies) 0 siblings, 3 replies; 5+ messages in thread From: Wenzhao Liao @ 2026-04-10 16:47 UTC (permalink / raw) To: Andreas Hindborg, Jens Axboe, Miguel Ojeda, linux-block, rust-for-linux Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel This small RFC series fills a missing blk-mq callback in the Rust block layer. Today, Rust block drivers cannot participate in request timeout handling because the Rust blk-mq vtable hardcodes `timeout: None`. Patch 1 adds a typed `TimeoutReturn` wrapper for `enum blk_eh_timer_return` and extends `mq::Operations` with an optional `timeout(&Request<Self>) -> TimeoutReturn` callback. The key design choice is to model timeout as a shared borrow of `Request`, not as `ARef<Request<_>>`. In this tree, blk-mq invokes `timeout(struct request *)` synchronously while the block layer retains ownership of the request. The timeout path is therefore a notification over a request whose lifetime is guaranteed for the duration of the call, not an ownership handoff to the driver. Representing the callback as `&Request` keeps that contract explicit in the type system, avoids fabricating a fresh `ARef`, and avoids touching the request wrapper refcount from the timeout path entirely. It also prevents the Rust API from implying that drivers may persist or take ownership of the request from this callback. For compatibility, the vtable only installs the timeout callback when `#[vtable]` generates `HAS_TIMEOUT`; drivers that do not implement the method continue to expose `timeout: None`, preserving existing behavior. Patch 2 wires the new abstraction into `rnull` as a minimal reference user. `rnull` implements the callback and returns `TimeoutReturn::ResetTimer`, which keeps behavior close to the current default timeout handling while proving that the Rust abstraction wires cleanly into a real driver. This tree uses the `timeout(struct request *)` blk-mq ABI, so the RFC is intentionally scoped to that signature and does not model the newer `reserved` parameter variant. Build-tested with: make -C /home/lwz/rfl-dev/worktrees/rnull-upstream-contribution \ O=/tmp/c2saferust-rnull-upstream-build LLVM=-15 defconfig make -C /home/lwz/rfl-dev/worktrees/rnull-upstream-contribution \ O=/tmp/c2saferust-rnull-upstream-build LLVM=-15 rustavailable make -C /home/lwz/rfl-dev/worktrees/rnull-upstream-contribution \ O=/tmp/c2saferust-rnull-upstream-build LLVM=-15 vmlinux make -C /home/lwz/rfl-dev/worktrees/rnull-upstream-contribution \ O=/tmp/c2saferust-rnull-upstream-build LLVM=-15 \ drivers/block/rnull/rnull_mod.ko with `CONFIG_RUST=y`, `CONFIG_CONFIGFS_FS=y`, and `CONFIG_BLK_DEV_RUST_NULL=m` enabled in the out-of-tree build. Comments are especially welcome on whether `&Request<Self>` is the right long-term Rust surface for blk-mq timeout handling. Wenzhao Liao (2): rust: block: mq: safely abstract the timeout callback block: rnull: implement dummy timeout callback drivers/block/rnull/rnull.rs | 4 +++ rust/kernel/block/mq.rs | 2 +- rust/kernel/block/mq/operations.rs | 40 +++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 2 deletions(-) -- 2.34.1 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [RFC PATCH 1/2] rust: block: mq: safely abstract the timeout callback 2026-04-10 16:47 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback Wenzhao Liao @ 2026-04-10 16:47 ` Wenzhao Liao 2026-04-10 16:47 ` [RFC PATCH 2/2] block: rnull: implement dummy " Wenzhao Liao 2026-04-11 10:29 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq " Andreas Hindborg 2 siblings, 0 replies; 5+ messages in thread From: Wenzhao Liao @ 2026-04-10 16:47 UTC (permalink / raw) To: Andreas Hindborg, Jens Axboe, Miguel Ojeda, linux-block, rust-for-linux Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel Add a typed TimeoutReturn enum for blk-mq timeout handlers and extend the Operations trait with an optional timeout callback. The new callback borrows Request instead of taking an ARef because timeout is a synchronous notification from blk-mq and does not transfer request ownership to the driver. Wire the callback into OperationsVTable and keep drivers that do not implement timeout on the existing timeout: None path. Signed-off-by: Wenzhao Liao <wenzhaoliao@ruc.edu.cn> --- rust/kernel/block/mq.rs | 2 +- rust/kernel/block/mq/operations.rs | 40 +++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/rust/kernel/block/mq.rs b/rust/kernel/block/mq.rs index 1fd0d54dd549..cfcfcd99addc 100644 --- a/rust/kernel/block/mq.rs +++ b/rust/kernel/block/mq.rs @@ -98,6 +98,6 @@ mod request; mod tag_set; -pub use operations::Operations; +pub use operations::{Operations, TimeoutReturn}; pub use request::Request; pub use tag_set::TagSet; diff --git a/rust/kernel/block/mq/operations.rs b/rust/kernel/block/mq/operations.rs index 8ad46129a52c..a8ff5eb8dd31 100644 --- a/rust/kernel/block/mq/operations.rs +++ b/rust/kernel/block/mq/operations.rs @@ -16,6 +16,16 @@ type ForeignBorrowed<'a, T> = <T as ForeignOwnable>::Borrowed<'a>; +/// Return value for blk-mq timeout handlers. +#[repr(u32)] +pub enum TimeoutReturn { + /// The driver completed the request or will complete it later. + Done = bindings::blk_eh_timer_return_BLK_EH_DONE, + + /// Reset the request timer and keep waiting for completion. + ResetTimer = bindings::blk_eh_timer_return_BLK_EH_RESET_TIMER, +} + /// Implement this trait to interface blk-mq as block devices. /// /// To implement a block device driver, implement this trait as described in the @@ -46,6 +56,11 @@ fn queue_rq( /// Called by the kernel when the request is completed. fn complete(rq: ARef<Request<Self>>); + /// Called by the kernel when a request times out. + fn timeout(_rq: &Request<Self>) -> TimeoutReturn { + build_error!(crate::error::VTABLE_DEFAULT_ERROR) + } + /// Called by the kernel to poll the device for completed requests. Only /// used for poll queues. fn poll() -> bool { @@ -163,6 +178,25 @@ impl<T: Operations> OperationsVTable<T> { T::complete(aref); } + /// This function is called by the C kernel. A pointer to this function is + /// installed in the `blk_mq_ops` vtable for the driver. + /// + /// # Safety + /// + /// This function may only be called by blk-mq C infrastructure. `rq` must + /// point to a valid request that is still live for the duration of this + /// callback. + unsafe extern "C" fn timeout_callback( + rq: *mut bindings::request, + ) -> bindings::blk_eh_timer_return { + // SAFETY: `rq` is valid as required by the safety requirements for + // this function, and the private data is initialized while the request + // is live. + let rq = unsafe { &*rq.cast::<Request<T>>() }; + + T::timeout(rq) as bindings::blk_eh_timer_return + } + /// This function is called by the C kernel. A pointer to this function is /// installed in the `blk_mq_ops` vtable for the driver. /// @@ -262,7 +296,11 @@ impl<T: Operations> OperationsVTable<T> { put_budget: None, set_rq_budget_token: None, get_rq_budget_token: None, - timeout: None, + timeout: if T::HAS_TIMEOUT { + Some(Self::timeout_callback) + } else { + None + }, poll: if T::HAS_POLL { Some(Self::poll_callback) } else { -- 2.34.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* [RFC PATCH 2/2] block: rnull: implement dummy timeout callback 2026-04-10 16:47 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback Wenzhao Liao 2026-04-10 16:47 ` [RFC PATCH 1/2] rust: block: mq: safely abstract the " Wenzhao Liao @ 2026-04-10 16:47 ` Wenzhao Liao 2026-04-11 10:29 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq " Andreas Hindborg 2 siblings, 0 replies; 5+ messages in thread From: Wenzhao Liao @ 2026-04-10 16:47 UTC (permalink / raw) To: Andreas Hindborg, Jens Axboe, Miguel Ojeda, linux-block, rust-for-linux Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel Implement the new Operations::timeout callback for rnull and return TimeoutReturn::ResetTimer. Using ResetTimer keeps the behavior close to the existing blk-mq default timeout handling while proving that the Rust timeout abstraction wires cleanly into a driver. Signed-off-by: Wenzhao Liao <wenzhaoliao@ruc.edu.cn> --- drivers/block/rnull/rnull.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/drivers/block/rnull/rnull.rs b/drivers/block/rnull/rnull.rs index 0ca8715febe8..3833a5bec7a4 100644 --- a/drivers/block/rnull/rnull.rs +++ b/drivers/block/rnull/rnull.rs @@ -97,4 +97,8 @@ fn complete(rq: ARef<mq::Request<Self>>) { // point, and so `end_ok` cannot fail. .expect("Fatal error - expected to be able to end request"); } + + fn timeout(_rq: &mq::Request<Self>) -> mq::TimeoutReturn { + mq::TimeoutReturn::ResetTimer + } } -- 2.34.1 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback 2026-04-10 16:47 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback Wenzhao Liao 2026-04-10 16:47 ` [RFC PATCH 1/2] rust: block: mq: safely abstract the " Wenzhao Liao 2026-04-10 16:47 ` [RFC PATCH 2/2] block: rnull: implement dummy " Wenzhao Liao @ 2026-04-11 10:29 ` Andreas Hindborg 2026-04-11 13:37 ` wenzhaoliao 2 siblings, 1 reply; 5+ messages in thread From: Andreas Hindborg @ 2026-04-11 10:29 UTC (permalink / raw) To: Wenzhao Liao, Jens Axboe, Miguel Ojeda, linux-block, rust-for-linux Cc: Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel Hi Wenzhao, "Wenzhao Liao" <wenzhaoliao@ruc.edu.cn> writes: > This small RFC series fills a missing blk-mq callback in the Rust block > layer. Today, Rust block drivers cannot participate in request timeout > handling because the Rust blk-mq vtable hardcodes `timeout: None`. I already pointed you to [1] multiple times. Please make sure to read that patch series. The functionality you are submitting is already covered by that series. If you really want to use your tokens to improve the kernel, I would suggest adding boolean parameter support to the Rust module parameter parser code. Be sure to credit your LLM according to [2]. And please do instruct your agents to find related discussion on the mailing list, so we avoid solving the same problem over and over. Best regards, Andreas Hindborg [1] https://lore.kernel.org/rust-for-linux/20260216-rnull-v6-19-rc5-send-v1-0-de9a7af4b469@kernel.org/ [2] https://docs.kernel.org/process/coding-assistants.html ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re:Re: [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback 2026-04-11 10:29 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq " Andreas Hindborg @ 2026-04-11 13:37 ` wenzhaoliao 0 siblings, 0 replies; 5+ messages in thread From: wenzhaoliao @ 2026-04-11 13:37 UTC (permalink / raw) To: Andreas Hindborg Cc: Jens Axboe, Miguel Ojeda, linux-block, rust-for-linux, Boqun Feng, Gary Guo, Björn Roy Baron, Benno Lossin, Alice Ryhl, Trevor Gross, Danilo Krummrich, linux-kernel Hi Greg, Thank you so much for taking the time to review this patch and for your valuable guidance. To be completely honest, we do not have a specific driver use case for this feature at the moment. Our overarching goal is simply to learn the kernel development process and find ways to genuinely contribute to the Rust-for-Linux project. Recently, we submitted a patch that unfortunately duplicated existing work. In that thread, Andreas Hindborg kindly pointed us to a new task with this exact suggestion: "If you really want to use your tokens to improve the kernel, I would suggest adding boolean parameter support to the Rust module parameter parser code." (https://lore.kernel.org/rust-for-linux/20260216-rnull-v6-19-rc5-send-v1-0-de9a7af4b469@kernel.org/) We took this as a good "first issue" to familiarize ourselves with the Rust subsystem, and that is the sole reason we implemented it. Learning from you that module options are considered 1990's technology and do not fit the modern kernel model is a great lesson for us. If we misunderstood the maintainer's intention, or if pursuing this outdated mechanism goes against modern kernel design principles, we sincerely apologize for the noise and for any burden this has caused. We are perfectly happy to drop this patch if this functionality is no longer desired in the modern kernel. We really just want to help improve the kernel, and we will try our best to sync with the maintainers to pick up tasks that are more aligned with current kernel standards (like sysfs/configfs) next time. Best regards, Wenzhao 发件人:Andreas Hindborg <a.hindborg@kernel.org> 发送日期:2026-04-11 18:29:34 收件人:Wenzhao Liao <wenzhaoliao@ruc.edu.cn>,Jens Axboe <axboe@kernel.dk>,Miguel Ojeda <ojeda@kernel.org>,linux-block@vger.kernel.org,rust-for-linux@vger.kernel.org 抄送人:Boqun Feng <boqun@kernel.org>,Gary Guo <gary@garyguo.net>,"Björn Roy Baron" <bjorn3_gh@protonmail.com>,Benno Lossin <lossin@kernel.org>,Alice Ryhl <aliceryhl@google.com>,Trevor Gross <tmgross@umich.edu>,Danilo Krummrich <dakr@kernel.org>,linux-kernel@vger.kernel.org 主题:Re: [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback>Hi Wenzhao, > >"Wenzhao Liao" <wenzhaoliao@ruc.edu.cn> writes: > >> This small RFC series fills a missing blk-mq callback in the Rust block >> layer. Today, Rust block drivers cannot participate in request timeout >> handling because the Rust blk-mq vtable hardcodes `timeout: None`. > >I already pointed you to [1] multiple times. Please make sure to read >that patch series. The functionality you are submitting is already >covered by that series. > >If you really want to use your tokens to improve the kernel, I would >suggest adding boolean parameter support to the Rust module parameter >parser code. > >Be sure to credit your LLM according to [2]. And please do instruct your >agents to find related discussion on the mailing list, so we avoid >solving the same problem over and over. > >Best regards, >Andreas Hindborg > > >[1] https://lore.kernel.org/rust-for-linux/20260216-rnull-v6-19-rc5-send-v1-0-de9a7af4b469@kernel.org/ >[2] https://docs.kernel.org/process/coding-assistants.html > > ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-04-11 13:37 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <YpOxb7raN9ztoDEyfT5M8S4KZA74Ua0I9VLcImKPi_lJ6K5xWugbgbzKmQcl0rz2XdRw0cXOYrDpOYjz9q62Dw==@protonmail.internalid>
2026-04-10 16:47 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq timeout callback Wenzhao Liao
2026-04-10 16:47 ` [RFC PATCH 1/2] rust: block: mq: safely abstract the " Wenzhao Liao
2026-04-10 16:47 ` [RFC PATCH 2/2] block: rnull: implement dummy " Wenzhao Liao
2026-04-11 10:29 ` [RFC PATCH 0/2] rust: block: add a borrowed blk-mq " Andreas Hindborg
2026-04-11 13:37 ` wenzhaoliao
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox