Skip to content

Commit 90d952f

Browse files
Andreas Hindborgaxboe
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. 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>
1 parent d969d50 commit 90d952f

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
@@ -61,14 +61,16 @@ impl NullBlkDevice {
6161
.logical_block_size(block_size)?
6262
.physical_block_size(block_size)?
6363
.rotational(rotational)
64-
.build(fmt!("{}", name.to_str()?), tagset)
64+
.build(fmt!("{}", name.to_str()?), tagset, ())
6565
}
6666
}
6767

6868
#[vtable]
6969
impl Operations for NullBlkDevice {
70+
type QueueData = ();
71+
7072
#[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 {
7274
mq::Request::end_ok(rq)
7375
.map_err(|_e| kernel::error::code::EIO)
7476
// We take no refcounts on the request, so we expect to be able to
@@ -79,5 +81,5 @@ impl Operations for NullBlkDevice {
7981
Ok(())
8082
}
8183

82-
fn commit_rqs() {}
84+
fn commit_rqs(_queue_data: ()) {}
8385
}

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
@@ -13,6 +13,7 @@ use crate::{
1313
static_lock_class,
1414
str::NullTerminatedFormatter,
1515
sync::Arc,
16+
types::{ForeignOwnable, ScopeGuard},
1617
};
1718
use core::fmt::{self, Write};
1819

@@ -98,7 +99,14 @@ impl GenDiskBuilder {
9899
self,
99100
name: fmt::Arguments<'_>,
100101
tagset: Arc<TagSet<T>>,
102+
queue_data: T::QueueData,
101103
) -> Result<GenDisk<T>> {
104+
let data = queue_data.into_foreign();
105+
let recover_data = ScopeGuard::new(|| {
106+
// SAFETY: T::QueueData was created by the call to `into_foreign()` above
107+
drop(unsafe { T::QueueData::from_foreign(data) });
108+
});
109+
102110
// SAFETY: `bindings::queue_limits` contain only fields that are valid when zeroed.
103111
let mut lim: bindings::queue_limits = unsafe { core::mem::zeroed() };
104112

@@ -113,7 +121,7 @@ impl GenDiskBuilder {
113121
bindings::__blk_mq_alloc_disk(
114122
tagset.raw_tag_set(),
115123
&mut lim,
116-
core::ptr::null_mut(),
124+
data,
117125
static_lock_class!().as_ptr(),
118126
)
119127
})?;
@@ -167,8 +175,12 @@ impl GenDiskBuilder {
167175
},
168176
)?;
169177

178+
recover_data.dismiss();
179+
170180
// INVARIANT: `gendisk` was initialized above.
171181
// INVARIANT: `gendisk` was added to the VFS via `device_add_disk` above.
182+
// INVARIANT: `gendisk.queue.queue_data` is set to `data` in the call to
183+
// `__blk_mq_alloc_disk` above.
172184
Ok(GenDisk {
173185
_tagset: tagset,
174186
gendisk,
@@ -180,9 +192,10 @@ impl GenDiskBuilder {
180192
///
181193
/// # Invariants
182194
///
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`.
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`.
198+
/// - `self.gendisk.queue.queuedata` is initialized by a call to `ForeignOwnable::into_foreign`.
186199
pub struct GenDisk<T: Operations> {
187200
_tagset: Arc<TagSet<T>>,
188201
gendisk: *mut bindings::gendisk,
@@ -194,9 +207,20 @@ unsafe impl<T: Operations + Send> Send for GenDisk<T> {}
194207

195208
impl<T: Operations> Drop for GenDisk<T> {
196209
fn drop(&mut self) {
210+
// SAFETY: By type invariant of `Self`, `self.gendisk` points to a valid
211+
// and initialized instance of `struct gendisk`, and, `queuedata` was
212+
// initialized with the result of a call to
213+
// `ForeignOwnable::into_foreign`.
214+
let queue_data = unsafe { (*(*self.gendisk).queue).queuedata };
215+
197216
// SAFETY: By type invariant, `self.gendisk` points to a valid and
198217
// initialized instance of `struct gendisk`, and it was previously added
199218
// to the VFS.
200219
unsafe { bindings::del_gendisk(self.gendisk) };
220+
221+
// SAFETY: `queue.queuedata` was created by `GenDiskBuilder::build` with
222+
// a call to `ForeignOwnable::into_foreign` to create `queuedata`.
223+
// `ForeignOwnable::from_foreign` is only called here.
224+
drop(unsafe { T::QueueData::from_foreign(queue_data) });
201225
}
202226
}

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 `GenDiskBuilder::build` with
104+
// a call to `ForeignOwnable::into_foreign` 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) };
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_foreign()` 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) };
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)