Skip to content

Commit 4c36656

Browse files
Andreas Hindborgkawasaki
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`. Signed-off-by: Andreas Hindborg <[email protected]>
1 parent bb290b2 commit 4c36656

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
@@ -83,4 +83,13 @@ impl Operations for NullBlkDevice {
8383
}
8484

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

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
@@ -130,6 +130,23 @@ impl<T: Operations> Request<T> {
130130
Ok(())
131131
}
132132

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

0 commit comments

Comments
 (0)