Skip to content

Commit 663581f

Browse files
Andreas Hindborgkawasaki
authored andcommitted
rust: block: add GenDisk private data support
Allow users of the rust block device driver API to install private data in the `GenDisk` structure. Signed-off-by: Andreas Hindborg <[email protected]>
1 parent 8ac6eab commit 663581f

4 files changed

Lines changed: 74 additions & 19 deletions

File tree

drivers/block/rnull/rnull.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,16 @@ impl NullBlkDevice {
6262
.logical_block_size(block_size)?
6363
.physical_block_size(block_size)?
6464
.rotational(rotational)
65-
.build(fmt!("{}", name.to_str()?), tagset)
65+
.build(fmt!("{}", name.to_str()?), tagset, ())
6666
}
6767
}
6868

6969
#[vtable]
7070
impl Operations for NullBlkDevice {
71+
type QueueData = ();
72+
7173
#[inline(always)]
72-
fn queue_rq(rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result {
74+
fn queue_rq(_queue_data: (), rq: ARef<mq::Request<Self>>, _is_last: bool) -> Result {
7375
mq::Request::end_ok(rq)
7476
.map_err(|_e| kernel::error::code::EIO)
7577
// We take no refcounts on the request, so we expect to be able to
@@ -80,5 +82,5 @@ impl Operations for NullBlkDevice {
8082
Ok(())
8183
}
8284

83-
fn commit_rqs() {}
85+
fn commit_rqs(_queue_data: ()) {}
8486
}

rust/kernel/block/mq.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,20 +69,21 @@
6969
//!
7070
//! #[vtable]
7171
//! impl Operations for MyBlkDevice {
72+
//! type QueueData = ();
7273
//!
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 {
7475
//! Request::end_ok(rq);
7576
//! Ok(())
7677
//! }
7778
//!
78-
//! fn commit_rqs() {}
79+
//! fn commit_rqs(_queue_data: ()) {}
7980
//! }
8081
//!
8182
//! let tagset: Arc<TagSet<MyBlkDevice>> =
8283
//! Arc::pin_init(TagSet::new(1, 256, 1), flags::GFP_KERNEL)?;
8384
//! let mut disk = gen_disk::GenDiskBuilder::new()
8485
//! .capacity_sectors(4096)
85-
//! .build(format_args!("myblk"), tagset)?;
86+
//! .build(format_args!("myblk"), tagset, ())?;
8687
//!
8788
//! # Ok::<(), kernel::error::Error>(())
8889
//! ```

rust/kernel/block/mq/gen_disk.rs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::{
1212
static_lock_class,
1313
str::NullBorrowFormatter,
1414
sync::Arc,
15+
types::{ForeignOwnable, ScopeGuard},
1516
};
1617
use core::fmt::{self, Write};
1718

@@ -97,7 +98,14 @@ impl GenDiskBuilder {
9798
self,
9899
name: fmt::Arguments<'_>,
99100
tagset: Arc<TagSet<T>>,
101+
queue_data: T::QueueData,
100102
) -> 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+
unsafe { T::QueueData::from_foreign(data) };
107+
});
108+
101109
// SAFETY: `bindings::queue_limits` contain only fields that are valid when zeroed.
102110
let mut lim: bindings::queue_limits = unsafe { core::mem::zeroed() };
103111

@@ -112,7 +120,7 @@ impl GenDiskBuilder {
112120
bindings::__blk_mq_alloc_disk(
113121
tagset.raw_tag_set(),
114122
&mut lim,
115-
core::ptr::null_mut(),
123+
data.cast(),
116124
static_lock_class!().as_ptr(),
117125
)
118126
})?;
@@ -165,8 +173,12 @@ impl GenDiskBuilder {
165173
},
166174
)?;
167175

176+
recover_data.dismiss();
177+
168178
// INVARIANT: `gendisk` was initialized above.
169179
// INVARIANT: `gendisk` was added to the VFS via `device_add_disk` above.
180+
// INVARIANT: `gendisk.queue.queue_data` is set to `data` in the call to
181+
// `__blk_mq_alloc_disk` above.
170182
Ok(GenDisk {
171183
_tagset: tagset,
172184
gendisk,
@@ -178,9 +190,10 @@ impl GenDiskBuilder {
178190
///
179191
/// # Invariants
180192
///
181-
/// - `gendisk` must always point to an initialized and valid `struct gendisk`.
182-
/// - `gendisk` was added to the VFS through a call to
183-
/// `bindings::device_add_disk`.
193+
/// - `gendisk` must always point to an initialized and valid `struct gendisk`.
194+
/// - `gendisk` was added to the VFS through a call to
195+
/// `bindings::device_add_disk`.
196+
/// - `self.gendisk.queue.queuedata` is initialized by a call to `ForeignOwnable::into_foreign`.
184197
pub struct GenDisk<T: Operations> {
185198
_tagset: Arc<TagSet<T>>,
186199
gendisk: *mut bindings::gendisk,
@@ -192,9 +205,20 @@ unsafe impl<T: Operations + Send> Send for GenDisk<T> {}
192205

193206
impl<T: Operations> Drop for GenDisk<T> {
194207
fn drop(&mut self) {
208+
// SAFETY: By type invariant of `Self`, `self.gendisk` points to a valid
209+
// and initialized instance of `struct gendisk`, and, `queuedata` was
210+
// initialized with the result of a call to
211+
// `ForeignOwnable::into_foreign`.
212+
let queue_data = unsafe { (*(*self.gendisk).queue).queuedata };
213+
195214
// SAFETY: By type invariant, `self.gendisk` points to a valid and
196215
// initialized instance of `struct gendisk`, and it was previously added
197216
// to the VFS.
198217
unsafe { bindings::del_gendisk(self.gendisk) };
218+
219+
// SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with
220+
// a call to `ForeignOwnable::into_foreign` to create `queuedata`.
221+
// `ForeignOwnable::from_foreign` is only called here.
222+
let _queue_data = unsafe { T::QueueData::from_foreign(queue_data.cast()) };
199223
}
200224
}

rust/kernel/block/mq/operations.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
77
use crate::{
88
bindings,
9-
block::mq::request::RequestDataWrapper,
10-
block::mq::Request,
9+
block::mq::{request::RequestDataWrapper, Request},
1110
error::{from_result, Result},
1211
prelude::*,
13-
types::ARef,
12+
types::{ARef, ForeignOwnable},
1413
};
1514
use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ordering};
1615

16+
type ForeignBorrowed<'a, T> = <T as ForeignOwnable>::Borrowed<'a>;
17+
1718
/// Implement this trait to interface blk-mq as block devices.
1819
///
1920
/// To implement a block device driver, implement this trait as described in the
@@ -26,12 +27,20 @@ use core::{marker::PhantomData, sync::atomic::AtomicU64, sync::atomic::Ordering}
2627
/// [module level documentation]: kernel::block::mq
2728
#[macros::vtable]
2829
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+
2934
/// Called by the kernel to queue a request with the driver. If `is_last` is
3035
/// `false`, the driver is allowed to defer committing the request.
31-
fn queue_rq(rq: ARef<Request<Self>>, is_last: bool) -> Result;
36+
fn queue_rq(
37+
queue_data: ForeignBorrowed<'_, Self::QueueData>,
38+
rq: ARef<Request<Self>>,
39+
is_last: bool,
40+
) -> Result;
3241

3342
/// Called by the kernel to indicate that queued requests should be submitted.
34-
fn commit_rqs();
43+
fn commit_rqs(queue_data: ForeignBorrowed<'_, Self::QueueData>);
3544

3645
/// Called by the kernel to poll the device for completed requests. Only
3746
/// used for poll queues.
@@ -70,7 +79,7 @@ impl<T: Operations> OperationsVTable<T> {
7079
/// promise to not access the request until the driver calls
7180
/// `bindings::blk_mq_end_request` for the request.
7281
unsafe extern "C" fn queue_rq_callback(
73-
_hctx: *mut bindings::blk_mq_hw_ctx,
82+
hctx: *mut bindings::blk_mq_hw_ctx,
7483
bd: *const bindings::blk_mq_queue_data,
7584
) -> bindings::blk_status_t {
7685
// SAFETY: `bd.rq` is valid as required by the safety requirement for
@@ -88,10 +97,20 @@ impl<T: Operations> OperationsVTable<T> {
8897
// reference counted by `ARef` until then.
8998
let rq = unsafe { Request::aref_from_raw((*bd).rq) };
9099

100+
// SAFETY: `hctx` is valid as required by this function.
101+
let queue_data = unsafe { (*(*hctx).queue).queuedata };
102+
103+
// SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` with a
104+
// call to `ForeignOwnable::into_pointer()` to create `queuedata`.
105+
// `ForeignOwnable::from_foreign()` is only called when the tagset is
106+
// dropped, which happens after we are dropped.
107+
let queue_data = unsafe { T::QueueData::borrow(queue_data.cast()) };
108+
91109
// SAFETY: We have exclusive access and we just set the refcount above.
92110
unsafe { Request::start_unchecked(&rq) };
93111

94112
let ret = T::queue_rq(
113+
queue_data,
95114
rq,
96115
// SAFETY: `bd` is valid as required by the safety requirement for
97116
// this function.
@@ -110,9 +129,18 @@ impl<T: Operations> OperationsVTable<T> {
110129
///
111130
/// # Safety
112131
///
113-
/// This function may only be called by blk-mq C infrastructure.
114-
unsafe extern "C" fn commit_rqs_callback(_hctx: *mut bindings::blk_mq_hw_ctx) {
115-
T::commit_rqs()
132+
/// This function may only be called by blk-mq C infrastructure. The caller
133+
/// must ensure that `hctx` is valid.
134+
unsafe extern "C" fn commit_rqs_callback(hctx: *mut bindings::blk_mq_hw_ctx) {
135+
// SAFETY: `hctx` is valid as required by this function.
136+
let queue_data = unsafe { (*(*hctx).queue).queuedata };
137+
138+
// SAFETY: `queue.queuedata` was created by `GenDisk::try_new()` with a
139+
// call to `ForeignOwnable::into_pointer()` to create `queuedata`.
140+
// `ForeignOwnable::from_foreign()` is only called when the tagset is
141+
// dropped, which happens after we are dropped.
142+
let queue_data = unsafe { T::QueueData::borrow(queue_data.cast()) };
143+
T::commit_rqs(queue_data)
116144
}
117145

118146
/// This function is called by the C kernel. It is not currently

0 commit comments

Comments
 (0)