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 `GenDisk` private data support

Allow users of the rust block device driver API to install private data in
the `GenDisk` structure.

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-14-b5212cc89b98@kernel.org
Signed-off-by: Jens Axboe <axboe@kernel.dk>

authored by

Andreas Hindborg and committed by
Jens Axboe
90d952fa d969d504

+74 -19
+5 -3
drivers/block/rnull/rnull.rs
··· 61 61 .logical_block_size(block_size)? 62 62 .physical_block_size(block_size)? 63 63 .rotational(rotational) 64 - .build(fmt!("{}", name.to_str()?), tagset) 64 + .build(fmt!("{}", name.to_str()?), tagset, ()) 65 65 } 66 66 } 67 67 68 68 #[vtable] 69 69 impl Operations for NullBlkDevice { 70 + type QueueData = (); 71 + 70 72 #[inline(always)] 71 - fn queue_rq(rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result { 73 + fn queue_rq(_queue_data: (), rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result { 72 74 mq::Request::end_ok(rq) 73 75 .map_err(|_e| kernel::error::code::EIO) 74 76 // We take no refcounts on the request, so we expect to be able to ··· 81 79 Ok(()) 82 80 } 83 81 84 - fn commit_rqs() {} 82 + fn commit_rqs(_queue_data: ()) {} 85 83 }
+4 -3
rust/kernel/block/mq.rs
··· 69 69 //! 70 70 //! #[vtable] 71 71 //! impl Operations for MyBlkDevice { 72 + //! type QueueData = (); 72 73 //! 73 - //! fn queue_rq(rq: ARef<Request<Self>>, _is_last: bool) -> Result { 74 + //! fn queue_rq(_queue_data: (), rq: ARef<Request<Self>>, _is_last: bool) -> Result { 74 75 //! Request::end_ok(rq); 75 76 //! Ok(()) 76 77 //! } 77 78 //! 78 - //! fn commit_rqs() {} 79 + //! fn commit_rqs(_queue_data: ()) {} 79 80 //! } 80 81 //! 81 82 //! let tagset: Arc<TagSet<MyBlkDevice>> = 82 83 //! Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?; 83 84 //! let mut disk = gen_disk::GenDiskBuilder::new() 84 85 //! .capacity_sectors(4096) 85 - //! .build(format_args!("myblk"), tagset)?; 86 + //! .build(format_args!("myblk"), tagset, ())?; 86 87 //! 87 88 //! # Ok::<(), kernel::error::Error>(()) 88 89 //! ```
+28 -4
rust/kernel/block/mq/gen_disk.rs
··· 13 13 static_lock_class, 14 14 str::NullTerminatedFormatter, 15 15 sync::Arc, 16 + types::{ForeignOwnable, ScopeGuard}, 16 17 }; 17 18 use core::fmt::{self, Write}; 18 19 ··· 99 98 self, 100 99 name: fmt::Arguments<'_>, 101 100 tagset: Arc<TagSet<T>>, 101 + queue_data: T::QueueData, 102 102 ) -> Result<GenDisk<T>> { 103 + let data = queue_data.into_foreign(); 104 + let recover_data = ScopeGuard::new(|| { 105 + // SAFETY: T::QueueData was created by the call to `into_foreign()` above 106 + drop(unsafe { T::QueueData::from_foreign(data) }); 107 + }); 108 + 103 109 // SAFETY: `bindings::queue_limits` contain only fields that are valid when zeroed. 104 110 let mut lim: bindings::queue_limits = unsafe { core::mem::zeroed() }; 105 111 ··· 121 113 bindings::__blk_mq_alloc_disk( 122 114 tagset.raw_tag_set(), 123 115 &mut lim, 124 - core::ptr::null_mut(), 116 + data, 125 117 static_lock_class!().as_ptr(), 126 118 ) 127 119 })?; ··· 175 167 }, 176 168 )?; 177 169 170 + recover_data.dismiss(); 171 + 178 172 // INVARIANT: `gendisk` was initialized above. 179 173 // INVARIANT: `gendisk` was added to the VFS via `device_add_disk` above. 174 + // INVARIANT: `gendisk.queue.queue_data` is set to `data` in the call to 175 + // `__blk_mq_alloc_disk` above. 180 176 Ok(GenDisk { 181 177 _tagset: tagset, 182 178 gendisk, ··· 192 180 /// 193 181 /// # Invariants 194 182 /// 195 - /// - `gendisk` must always point to an initialized and valid `struct gendisk`. 196 - /// - `gendisk` was added to the VFS through a call to 197 - /// `bindings::device_add_disk`. 183 + /// - `gendisk` must always point to an initialized and valid `struct gendisk`. 184 + /// - `gendisk` was added to the VFS through a call to 185 + /// `bindings::device_add_disk`. 186 + /// - `self.gendisk.queue.queuedata` is initialized by a call to `ForeignOwnable::into_foreign`. 198 187 pub struct GenDisk<T: Operations> { 199 188 _tagset: Arc<TagSet<T>>, 200 189 gendisk: *mut bindings::gendisk, ··· 207 194 208 195 impl<T: Operations> Drop for GenDisk<T> { 209 196 fn drop(&mut self) { 197 + // SAFETY: By type invariant of `Self`, `self.gendisk` points to a valid 198 + // and initialized instance of `struct gendisk`, and, `queuedata` was 199 + // initialized with the result of a call to 200 + // `ForeignOwnable::into_foreign`. 201 + let queue_data = unsafe { (*(*self.gendisk).queue).queuedata }; 202 + 210 203 // SAFETY: By type invariant, `self.gendisk` points to a valid and 211 204 // initialized instance of `struct gendisk`, and it was previously added 212 205 // to the VFS. 213 206 unsafe { bindings::del_gendisk(self.gendisk) }; 207 + 208 + // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with 209 + // a call to `ForeignOwnable::into_foreign` to create `queuedata`. 210 + // `ForeignOwnable::from_foreign` is only called here. 211 + drop(unsafe { T::QueueData::from_foreign(queue_data) }); 214 212 } 215 213 }
+37 -9
rust/kernel/block/mq/operations.rs
··· 6 6 7 7 use crate::{ 8 8 bindings, 9 - block::mq::request::RequestDataWrapper, 10 - block::mq::Request, 9 + block::mq::{request::RequestDataWrapper, Request}, 11 10 error::{from_result, Result}, 12 11 prelude::*, 13 - types::ARef, 12 + types::{ARef, ForeignOwnable}, 14 13 }; 15 14 use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ordering}; 15 + 16 + type ForeignBorrowed<'a, T> = <T as ForeignOwnable>::Borrowed<'a>; 16 17 17 18 /// Implement this trait to interface blk-mq as block devices. 18 19 /// ··· 27 26 /// [module level documentation]: kernel::block::mq 28 27 #[macros::vtable] 29 28 pub trait Operations: Sized { 29 + /// Data associated with the `struct request_queue` that is allocated for 30 + /// the `GenDisk` associated with this `Operations` implementation. 31 + type QueueData: ForeignOwnable; 32 + 30 33 /// Called by the kernel to queue a request with the driver. If `is_last` is 31 34 /// `false`, the driver is allowed to defer committing the request. 32 - fn queue_rq(rq: ARef<Request<Self>>, is_last: bool) -> Result; 35 + fn queue_rq( 36 + queue_data: ForeignBorrowed<'_, Self::QueueData>, 37 + rq: ARef<Request<Self>>, 38 + is_last: bool, 39 + ) -> Result; 33 40 34 41 /// Called by the kernel to indicate that queued requests should be submitted. 35 - fn commit_rqs(); 42 + fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>); 36 43 37 44 /// Called by the kernel to poll the device for completed requests. Only 38 45 /// used for poll queues. ··· 79 70 /// promise to not access the request until the driver calls 80 71 /// `bindings::blk_mq_end_request` for the request. 81 72 unsafe extern "C" fn queue_rq_callback( 82 - _hctx: *mut bindings::blk_mq_hw_ctx, 73 + hctx: *mut bindings::blk_mq_hw_ctx, 83 74 bd: *const bindings::blk_mq_queue_data, 84 75 ) -> bindings::blk_status_t { 85 76 // SAFETY: `bd.rq` is valid as required by the safety requirement for ··· 97 88 // reference counted by `ARef` until then. 98 89 let rq = unsafe { Request::aref_from_raw((*bd).rq) }; 99 90 91 + // SAFETY: `hctx` is valid as required by this function. 92 + let queue_data = unsafe { (*(*hctx).queue).queuedata }; 93 + 94 + // SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with 95 + // a call to `ForeignOwnable::into_foreign` to create `queuedata`. 96 + // `ForeignOwnable::from_foreign` is only called when the tagset is 97 + // dropped, which happens after we are dropped. 98 + let queue_data = unsafe { T::QueueData::borrow(queue_data) }; 99 + 100 100 // SAFETY: We have exclusive access and we just set the refcount above. 101 101 unsafe { Request::start_unchecked(&rq) }; 102 102 103 103 let ret = T::queue_rq( 104 + queue_data, 104 105 rq, 105 106 // SAFETY: `bd` is valid as required by the safety requirement for 106 107 // this function. ··· 129 110 /// 130 111 /// # Safety 131 112 /// 132 - /// This function may only be called by blk-mq C infrastructure. 133 - unsafe extern "C" fn commit_rqs_callback(_hctx: *mut bindings::blk_mq_hw_ctx) { 134 - T::commit_rqs() 113 + /// This function may only be called by blk-mq C infrastructure. The caller 114 + /// must ensure that `hctx` is valid. 115 + unsafe extern "C" fn commit_rqs_callback(hctx: *mut bindings::blk_mq_hw_ctx) { 116 + // SAFETY: `hctx` is valid as required by this function. 117 + let queue_data = unsafe { (*(*hctx).queue).queuedata }; 118 + 119 + // SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` with a 120 + // call to `ForeignOwnable::into_foreign()` to create `queuedata`. 121 + // `ForeignOwnable::from_foreign()` is only called when the tagset is 122 + // dropped, which happens after we are dropped. 123 + let queue_data = unsafe { T::QueueData::borrow(queue_data) }; 124 + T::commit_rqs(queue_data) 135 125 } 136 126 137 127 /// This function is called by the C kernel. It is not currently