operations.rs 9.4 KB

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