Skip to content

Commit 4ec0528

Browse files
Andreas Hindborgaxboe
authored andcommitted
rust: block: add remote completion to Request
Allow users of rust block device driver API to schedule completion of requests via `blk_mq_complete_request_remote`. Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Daniel Almeida <daniel.almeida@collabora.com> Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org> Link: https://lore.kernel.org/r/20250902-rnull-up-v6-16-v7-16-b5212cc89b98@kernel.org Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent bde50e2 commit 4ec0528

4 files changed

Lines changed: 47 additions & 4 deletions

File tree

drivers/block/rnull/rnull.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,13 @@ impl Operations for NullBlkDevice {
8282
}
8383

8484
fn commit_rqs(_queue_data: ()) {}
85+
86+
fn complete(rq: ARef<mq::Request<Self>>) {
87+
mq::Request::end_ok(rq)
88+
.map_err(|_e| kernel::error::code::EIO)
89+
// We take no refcounts on the request, so we expect to be able to
90+
// end the request. The request reference must be unique at this
91+
// point, and so `end_ok` cannot fail.
92+
.expect("Fatal error - expected to be able to end request");
93+
}
8594
}

rust/kernel/block/mq.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
//! }
7878
//!
7979
//! fn commit_rqs(_queue_data: ()) {}
80+
//!
81+
//! fn complete(rq: ARef<Request<Self>>) {
82+
//! Request::end_ok(rq)
83+
//! .map_err(|_e| kernel::error::code::EIO)
84+
//! .expect("Fatal error - expected to be able to end request");
85+
//! }
8086
//! }
8187
//!
8288
//! let tagset: Arc<TagSet<MyBlkDevice>> =

rust/kernel/block/mq/operations.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ pub trait Operations: Sized {
4242
/// Called by the kernel to indicate that queued requests should be submitted.
4343
fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>);
4444

45+
/// Called by the kernel when the request is completed.
46+
fn complete(rq: ARef<Request<Self>>);
47+
4548
/// Called by the kernel to poll the device for completed requests. Only
4649
/// used for poll queues.
4750
fn poll() -> bool {
@@ -143,13 +146,21 @@ impl<T: Operations> OperationsVTable<T> {
143146
T::commit_rqs(queue_data)
144147
}
145148

146-
/// This function is called by the C kernel. It is not currently
147-
/// implemented, and there is no way to exercise this code path.
149+
/// This function is called by the C kernel. A pointer to this function is
150+
/// installed in the `blk_mq_ops` vtable for the driver.
148151
///
149152
/// # Safety
150153
///
151-
/// This function may only be called by blk-mq C infrastructure.
152-
unsafe extern "C" fn complete_callback(_rq: *mut bindings::request) {}
154+
/// This function may only be called by blk-mq C infrastructure. `rq` must
155+
/// point to a valid request that has been marked as completed. The pointee
156+
/// of `rq` must be valid for write for the duration of this function.
157+
unsafe extern "C" fn complete_callback(rq: *mut bindings::request) {
158+
// SAFETY: This function can only be dispatched through
159+
// `Request::complete`. We leaked a refcount then which we pick back up
160+
// now.
161+
let aref = unsafe { Request::aref_from_raw(rq) };
162+
T::complete(aref);
163+
}
153164

154165
/// This function is called by the C kernel. A pointer to this function is
155166
/// installed in the `blk_mq_ops` vtable for the driver.

rust/kernel/block/mq/request.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,23 @@ impl<T: Operations> Request<T> {
135135
Ok(())
136136
}
137137

138+
/// Complete the request by scheduling `Operations::complete` for
139+
/// execution.
140+
///
141+
/// The function may be scheduled locally, via SoftIRQ or remotely via IPMI.
142+
/// See `blk_mq_complete_request_remote` in [`blk-mq.c`] for details.
143+
///
144+
/// [`blk-mq.c`]: srctree/block/blk-mq.c
145+
pub fn complete(this: ARef<Self>) {
146+
let ptr = ARef::into_raw(this).cast::<bindings::request>().as_ptr();
147+
// SAFETY: By type invariant, `self.0` is a valid `struct request`
148+
if !unsafe { bindings::blk_mq_complete_request_remote(ptr) } {
149+
// SAFETY: We released a refcount above that we can reclaim here.
150+
let this = unsafe { Request::aref_from_raw(ptr) };
151+
T::complete(this);
152+
}
153+
}
154+
138155
/// Return a pointer to the [`RequestDataWrapper`] stored in the private area
139156
/// of the request structure.
140157
///

0 commit comments

Comments
 (0)