Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

authored by

Andreas Hindborg and committed by
Jens Axboe
4ec05284 bde50e28

+47 -4
+9
drivers/block/rnull/rnull.rs
··· 82 82 } 83 83 84 84 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 + } 85 94 }
+6
rust/kernel/block/mq.rs
··· 77 77 //! } 78 78 //! 79 79 //! 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 + //! } 80 86 //! } 81 87 //! 82 88 //! let tagset: Arc<TagSet<MyBlkDevice>> =
+15 -4
rust/kernel/block/mq/operations.rs
··· 42 42 /// Called by the kernel to indicate that queued requests should be submitted. 43 43 fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); 44 44 45 + /// Called by the kernel when the request is completed. 46 + fn complete(rq: ARef<Request<Self>>); 47 + 45 48 /// Called by the kernel to poll the device for completed requests. Only 46 49 /// used for poll queues. 47 50 fn poll() -> bool { ··· 146 143 T::commit_rqs(queue_data) 147 144 } 148 145 149 - /// This function is called by the C kernel. It is not currently 150 - /// implemented, and there is no way to exercise this code path. 146 + /// This function is called by the C kernel. A pointer to this function is 147 + /// installed in the `blk_mq_ops` vtable for the driver. 151 148 /// 152 149 /// # Safety 153 150 /// 154 - /// This function may only be called by blk-mq C infrastructure. 155 - unsafe extern "C" fn complete_callback(_rq: *mut bindings::request) {} 151 + /// This function may only be called by blk-mq C infrastructure. `rq` must 152 + /// point to a valid request that has been marked as completed. The pointee 153 + /// of `rq` must be valid for write for the duration of this function. 154 + unsafe extern "C" fn complete_callback(rq: *mut bindings::request) { 155 + // SAFETY: This function can only be dispatched through 156 + // `Request::complete`. We leaked a refcount then which we pick back up 157 + // now. 158 + let aref = unsafe { Request::aref_from_raw(rq) }; 159 + T::complete(aref); 160 + } 156 161 157 162 /// This function is called by the C kernel. A pointer to this function is 158 163 /// installed in the `blk_mq_ops` vtable for the driver.
+17
rust/kernel/block/mq/request.rs
··· 135 135 Ok(()) 136 136 } 137 137 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 + 138 155 /// Return a pointer to the [`RequestDataWrapper`] stored in the private area 139 156 /// of the request structure. 140 157 ///