1 // SPDX-License-Identifier: GPL-2.0 2 3 //! This module provides an interface for blk-mq drivers to implement. 4 //! 5 //! C header: [`include/linux/blk-mq.h`](srctree/include/linux/blk-mq.h) 6 7 use crate::{ 8 bindings, 9 block::mq::{request::RequestDataWrapper, Request}, 10 error::{from_result, Result}, 11 prelude::*, 12 types::{ARef, ForeignOwnable}, 13 }; 14 use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ordering}; 15 16 type ForeignBorrowed<'a, T> = <T as ForeignOwnable>::Borrowed<'a>; 17 18 /// Implement this trait to interface blk-mq as block devices. 19 /// 20 /// To implement a block device driver, implement this trait as described in the 21 /// [module level documentation]. The kernel will use the implementation of the 22 /// functions defined in this trait to interface a block device driver. Note: 23 /// There is no need for an exit_request() implementation, because the `drop` 24 /// implementation of the [`Request`] type will be invoked by automatically by 25 /// the C/Rust glue logic. 26 /// 27 /// [module level documentation]: kernel::block::mq 28 #[macros::vtable] 29 pub trait Operations: Sized { 30 /// Data associated with the `struct request_queue` that is allocated for 31 /// the `GenDisk` associated with this `Operations` implementation. 32 type QueueData: ForeignOwnable; 33 34 /// Called by the kernel to queue a request with the driver. If `is_last` is 35 /// `false`, the driver is allowed to defer committing the request. 36 fn queue_rq( 37 queue_data: ForeignBorrowed<'_, Self::QueueData>, 38 rq: ARef<Request<Self>>, 39 is_last: bool, 40 ) -> Result; 41 42 /// Called by the kernel to indicate that queued requests should be submitted. 43 fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); 44 45 /// Called by the kernel when the request is completed. 46 fn complete(rq: ARef<Request<Self>>); 47 48 /// Called by the kernel to poll the device for completed requests. Only 49 /// used for poll queues. 50 fn poll() -> bool { 51 build_error!(crate::error::VTABLE_DEFAULT_ERROR) 52 } 53 } 54 55 /// A vtable for blk-mq to interact with a block device driver. 56 /// 57 /// A `bindings::blk_mq_ops` vtable is constructed from pointers to the `extern 58 /// "C"` functions of this struct, exposed through the `OperationsVTable::VTABLE`. 59 /// 60 /// For general documentation of these methods, see the kernel source 61 /// documentation related to `struct blk_mq_operations` in 62 /// [`include/linux/blk-mq.h`]. 63 /// 64 /// [`include/linux/blk-mq.h`]: srctree/include/linux/blk-mq.h 65 pub(crate) struct OperationsVTable<T: Operations>(PhantomData<T>); 66 67 impl<T: Operations> OperationsVTable<T> { 68 /// This function is called by the C kernel. A pointer to this function is 69 /// installed in the `blk_mq_ops` vtable for the driver. 70 /// 71 /// # Safety 72 /// 73 /// - The caller of this function must ensure that the pointee of `bd` is 74 /// valid for reads for the duration of this function. 75 /// - This function must be called for an initialized and live `hctx`. That 76 /// is, `Self::init_hctx_callback` was called and 77 /// `Self::exit_hctx_callback()` was not yet called. 78 /// - `(*bd).rq` must point to an initialized and live `bindings:request`. 79 /// That is, `Self::init_request_callback` was called but 80 /// `Self::exit_request_callback` was not yet called for the request. 81 /// - `(*bd).rq` must be owned by the driver. That is, the block layer must 82 /// promise to not access the request until the driver calls 83 /// `bindings::blk_mq_end_request` for the request. 84 unsafe extern "C" fn queue_rq_callback( 85 hctx: *mut bindings::blk_mq_hw_ctx, 86 bd: *const bindings::blk_mq_queue_data, 87 ) -> bindings::blk_status_t { 88 // SAFETY: `bd.rq` is valid as required by the safety requirement for 89 // this function. 90 let request = unsafe { &*(*bd).rq.cast::<Request<T>>() }; 91 92 // One refcount for the ARef, one for being in flight 93 request.wrapper_ref().refcount().store(2, Ordering::Relaxed); 94 95 // SAFETY: 96 // - We own a refcount that we took above. We pass that to `ARef`. 97 // - By the safety requirements of this function, `request` is a valid 98 // `struct request` and the private data is properly initialized. 99 // - `rq` will be alive until `blk_mq_end_request` is called and is 100 // reference counted by `ARef` until then. 101 let rq = unsafe { Request::aref_from_raw((*bd).rq) }; 102 103 // SAFETY: `hctx` is valid as required by this function. 104 let queue_data = unsafe { (*(*hctx).queue).queuedata }; 105 106 // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with 107 // a call to `ForeignOwnable::into_foreign` to create `queuedata`. 108 // `ForeignOwnable::from_foreign` is only called when the tagset is 109 // dropped, which happens after we are dropped. 110 let queue_data = unsafe { T::QueueData::borrow(queue_data) }; 111 112 // SAFETY: We have exclusive access and we just set the refcount above. 113 unsafe { Request::start_unchecked(&rq) }; 114 115 let ret = T::queue_rq( 116 queue_data, 117 rq, 118 // SAFETY: `bd` is valid as required by the safety requirement for 119 // this function. 120 unsafe { (*bd).last }, 121 ); 122 123 if let Err(e) = ret { 124 e.to_blk_status() 125 } else { 126 bindings::BLK_STS_OK as bindings::blk_status_t 127 } 128 } 129 130 /// This function is called by the C kernel. A pointer to this function is 131 /// installed in the `blk_mq_ops` vtable for the driver. 132 /// 133 /// # Safety 134 /// 135 /// This function may only be called by blk-mq C infrastructure. The caller 136 /// must ensure that `hctx` is valid. 137 unsafe extern "C" fn commit_rqs_callback(hctx: *mut bindings::blk_mq_hw_ctx) { 138 // SAFETY: `hctx` is valid as required by this function. 139 let queue_data = unsafe { (*(*hctx).queue).queuedata }; 140 141 // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` with a 142 // call to `ForeignOwnable::into_foreign()` to create `queuedata`. 143 // `ForeignOwnable::from_foreign()` is only called when the tagset is 144 // dropped, which happens after we are dropped. 145 let queue_data = unsafe { T::QueueData::borrow(queue_data) }; 146 T::commit_rqs(queue_data) 147 } 148 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. 151 /// 152 /// # Safety 153 /// 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 } 164 165 /// This function is called by the C kernel. A pointer to this function is 166 /// installed in the `blk_mq_ops` vtable for the driver. 167 /// 168 /// # Safety 169 /// 170 /// This function may only be called by blk-mq C infrastructure. 171 unsafe extern "C" fn poll_callback( 172 _hctx: *mut bindings::blk_mq_hw_ctx, 173 _iob: *mut bindings::io_comp_batch, 174 ) -> crate::ffi::c_int { 175 T::poll().into() 176 } 177 178 /// This function is called by the C kernel. A pointer to this function is 179 /// installed in the `blk_mq_ops` vtable for the driver. 180 /// 181 /// # Safety 182 /// 183 /// This function may only be called by blk-mq C infrastructure. This 184 /// function may only be called once before `exit_hctx_callback` is called 185 /// for the same context. 186 unsafe extern "C" fn init_hctx_callback( 187 _hctx: *mut bindings::blk_mq_hw_ctx, 188 _tagset_data: *mut crate::ffi::c_void, 189 _hctx_idx: crate::ffi::c_uint, 190 ) -> crate::ffi::c_int { 191 from_result(|| Ok(0)) 192 } 193 194 /// This function is called by the C kernel. A pointer to this function is 195 /// installed in the `blk_mq_ops` vtable for the driver. 196 /// 197 /// # Safety 198 /// 199 /// This function may only be called by blk-mq C infrastructure. 200 unsafe extern "C" fn exit_hctx_callback( 201 _hctx: *mut bindings::blk_mq_hw_ctx, 202 _hctx_idx: crate::ffi::c_uint, 203 ) { 204 } 205 206 /// This function is called by the C kernel. A pointer to this function is 207 /// installed in the `blk_mq_ops` vtable for the driver. 208 /// 209 /// # Safety 210 /// 211 /// - This function may only be called by blk-mq C infrastructure. 212 /// - `_set` must point to an initialized `TagSet<T>`. 213 /// - `rq` must point to an initialized `bindings::request`. 214 /// - The allocation pointed to by `rq` must be at the size of `Request` 215 /// plus the size of `RequestDataWrapper`. 216 unsafe extern "C" fn init_request_callback( 217 _set: *mut bindings::blk_mq_tag_set, 218 rq: *mut bindings::request, 219 _hctx_idx: crate::ffi::c_uint, 220 _numa_node: crate::ffi::c_uint, 221 ) -> crate::ffi::c_int { 222 from_result(|| { 223 // SAFETY: By the safety requirements of this function, `rq` points 224 // to a valid allocation. 225 let pdu = unsafe { Request::wrapper_ptr(rq.cast::<Request<T>>()) }; 226 227 // SAFETY: The refcount field is allocated but not initialized, so 228 // it is valid for writes. 229 unsafe { RequestDataWrapper::refcount_ptr(pdu.as_ptr()).write(AtomicU64::new(0)) }; 230 231 Ok(0) 232 }) 233 } 234 235 /// This function is called by the C kernel. A pointer to this function is 236 /// installed in the `blk_mq_ops` vtable for the driver. 237 /// 238 /// # Safety 239 /// 240 /// - This function may only be called by blk-mq C infrastructure. 241 /// - `_set` must point to an initialized `TagSet<T>`. 242 /// - `rq` must point to an initialized and valid `Request`. 243 unsafe extern "C" fn exit_request_callback( 244 _set: *mut bindings::blk_mq_tag_set, 245 rq: *mut bindings::request, 246 _hctx_idx: crate::ffi::c_uint, 247 ) { 248 // SAFETY: The tagset invariants guarantee that all requests are allocated with extra memory 249 // for the request data. 250 let pdu = unsafe { bindings::blk_mq_rq_to_pdu(rq) }.cast::<RequestDataWrapper>(); 251 252 // SAFETY: `pdu` is valid for read and write and is properly initialised. 253 unsafe { core::ptr::drop_in_place(pdu) }; 254 } 255 256 const VTABLE: bindings::blk_mq_ops = bindings::blk_mq_ops { 257 queue_rq: Some(Self::queue_rq_callback), 258 queue_rqs: None, 259 commit_rqs: Some(Self::commit_rqs_callback), 260 get_budget: None, 261 put_budget: None, 262 set_rq_budget_token: None, 263 get_rq_budget_token: None, 264 timeout: None, 265 poll: if T::HAS_POLL { 266 Some(Self::poll_callback) 267 } else { 268 None 269 }, 270 complete: Some(Self::complete_callback), 271 init_hctx: Some(Self::init_hctx_callback), 272 exit_hctx: Some(Self::exit_hctx_callback), 273 init_request: Some(Self::init_request_callback), 274 exit_request: Some(Self::exit_request_callback), 275 cleanup_rq: None, 276 busy: None, 277 map_queues: None, 278 #[cfg(CONFIG_BLK_DEBUG_FS)] 279 show_rq: None, 280 }; 281 282 pub(crate) const fn build() -> &'static bindings::blk_mq_ops { 283 &Self::VTABLE 284 } 285 } 286