Skip to content

Commit 273eeb7

Browse files
committed
docs: rust: fix formatting for kernel::block::mq::Request
Fix several issues with rustdoc formatting for the `kernel::block::mq::Request` module, in particular: - An ordered list not rendering correctly. - Code snippets formatted as regular text. - References to types missing intra-doc links. Additionally, to ensure that intra-doc links are rendered for private types, add the `--document-private-items` flag to all rustdoc invocations. Finally, update the Rust coding guidelines to mention the syntax for intra-doc links. Closes: #1108 Signed-off-by: Francesco Zardi <[email protected]> Suggested-by: Miguel Ojeda <[email protected]>
1 parent a335e95 commit 273eeb7

File tree

4 files changed

+44
-32
lines changed

4 files changed

+44
-32
lines changed

Documentation/rust/coding-guidelines.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ in the kernel:
162162
a section called ``# Examples``.
163163

164164
- Rust items (functions, types, constants...) must be linked appropriately
165-
(``rustdoc`` will create a link automatically).
165+
by wrapping them with square brackets (e.g., ``[`Foo::bar()`]``).
166166

167167
- Any ``unsafe`` block must be preceded by a ``// SAFETY:`` comment
168168
describing why the code inside is sound.

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ export rust_common_flags := --edition=2021 \
457457
-Wclippy::needless_continue \
458458
-Wclippy::no_mangle_with_rust_abi \
459459
-Wclippy::dbg_macro
460+
export rustdoc_common_flags := --document-private-items
460461

461462
KBUILD_HOSTCFLAGS := $(KBUILD_USERHOSTCFLAGS) $(HOST_LFS_CFLAGS) \
462463
$(HOSTCFLAGS) -I $(srctree)/scripts/include

rust/Makefile

+4-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ alloc-cfgs = \
6161
quiet_cmd_rustdoc = RUSTDOC $(if $(rustdoc_host),H, ) $<
6262
cmd_rustdoc = \
6363
OBJTREE=$(abspath $(objtree)) \
64-
$(RUSTDOC) $(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \
64+
$(RUSTDOC) $(rustdoc_common_flags) \
65+
$(if $(rustdoc_host),$(rust_common_flags),$(rust_flags)) \
6566
$(rustc_target_flags) -L$(objtree)/$(obj) \
6667
-Zunstable-options --generate-link-to-definition \
6768
--output $(rustdoc_output) \
@@ -149,7 +150,7 @@ rusttestlib-uapi: $(src)/uapi/lib.rs FORCE
149150
quiet_cmd_rustdoc_test = RUSTDOC T $<
150151
cmd_rustdoc_test = \
151152
OBJTREE=$(abspath $(objtree)) \
152-
$(RUSTDOC) --test $(rust_common_flags) \
153+
$(RUSTDOC) $(rustdoc_common_flags) --test $(rust_common_flags) \
153154
@$(objtree)/include/generated/rustc_cfg \
154155
$(rustc_target_flags) $(rustdoc_test_target_flags) \
155156
$(rustdoc_test_quiet) \
@@ -161,7 +162,7 @@ quiet_cmd_rustdoc_test_kernel = RUSTDOC TK $<
161162
rm -rf $(objtree)/$(obj)/test/doctests/kernel; \
162163
mkdir -p $(objtree)/$(obj)/test/doctests/kernel; \
163164
OBJTREE=$(abspath $(objtree)) \
164-
$(RUSTDOC) --test $(rust_flags) \
165+
$(RUSTDOC) $(rustdoc_common_flags) --test $(rust_flags) \
165166
-L$(objtree)/$(obj) --extern alloc --extern kernel \
166167
--extern build_error --extern macros \
167168
--extern bindings --extern uapi \

rust/kernel/block/mq/request.rs

+38-28
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,56 @@ use core::{
1616
sync::atomic::{AtomicU64, Ordering},
1717
};
1818

19-
/// A wrapper around a blk-mq `struct request`. This represents an IO request.
19+
#[allow(rustdoc::private_intra_doc_links)]
20+
/// A wrapper around a blk-mq [`struct request`]. This represents an IO request.
2021
///
2122
/// # Implementation details
2223
///
2324
/// There are four states for a request that the Rust bindings care about:
2425
///
25-
/// A) Request is owned by block layer (refcount 0)
26-
/// B) Request is owned by driver but with zero `ARef`s in existence
26+
/// 1. Request is owned by block layer (refcount 0)
27+
/// 2. Request is owned by driver but with zero [`ARef`]s in existence
2728
/// (refcount 1)
28-
/// C) Request is owned by driver with exactly one `ARef` in existence
29+
/// 3. Request is owned by driver with exactly one [`ARef`] in existence
2930
/// (refcount 2)
30-
/// D) Request is owned by driver with more than one `ARef` in existence
31+
/// 4. Request is owned by driver with more than one [`ARef`] in existence
3132
/// (refcount > 2)
3233
///
3334
///
34-
/// We need to track A and B to ensure we fail tag to request conversions for
35+
/// We need to track 1 and 2 to ensure we fail tag to request conversions for
3536
/// requests that are not owned by the driver.
3637
///
37-
/// We need to track C and D to ensure that it is safe to end the request and hand
38+
/// We need to track 3 and 4 to ensure that it is safe to end the request and hand
3839
/// back ownership to the block layer.
3940
///
4041
/// The states are tracked through the private `refcount` field of
41-
/// `RequestDataWrapper`. This structure lives in the private data area of the C
42-
/// `struct request`.
42+
/// [`RequestDataWrapper`]. This structure lives in the private data area of the C
43+
/// [`struct request`].
4344
///
4445
/// # Invariants
4546
///
46-
/// * `self.0` is a valid `struct request` created by the C portion of the kernel.
47+
/// * `self.0` is a valid [`struct request`] created by the C portion of the
48+
/// kernel.
4749
/// * The private data area associated with this request must be an initialized
48-
/// and valid `RequestDataWrapper<T>`.
50+
/// and valid [`RequestDataWrapper<T>`].
4951
/// * `self` is reference counted by atomic modification of
50-
/// self.wrapper_ref().refcount().
52+
/// `self.wrapper_ref().refcount()`.
53+
///
54+
/// [`struct request`]: srctree/include/linux/blk-mq.h
5155
///
5256
#[repr(transparent)]
5357
pub struct Request<T: Operations>(Opaque<bindings::request>, PhantomData<T>);
5458

5559
impl<T: Operations> Request<T> {
56-
/// Create an `ARef<Request>` from a `struct request` pointer.
60+
/// Create an [`ARef<Request>`] from a [`struct request`] pointer.
5761
///
5862
/// # Safety
5963
///
6064
/// * The caller must own a refcount on `ptr` that is transferred to the
61-
/// returned `ARef`.
62-
/// * The type invariants for `Request` must hold for the pointee of `ptr`.
65+
/// returned [`ARef`].
66+
/// * The type invariants for [`Request`] must hold for the pointee of `ptr`.
67+
///
68+
/// [`struct request`]: srctree/include/linux/blk-mq.h
6369
pub(crate) unsafe fn aref_from_raw(ptr: *mut bindings::request) -> ARef<Self> {
6470
// INVARIANT: By the safety requirements of this function, invariants are upheld.
6571
// SAFETY: By the safety requirement of this function, we own a
@@ -84,12 +90,14 @@ impl<T: Operations> Request<T> {
8490
}
8591

8692
/// Try to take exclusive ownership of `this` by dropping the refcount to 0.
87-
/// This fails if `this` is not the only `ARef` pointing to the underlying
88-
/// `Request`.
93+
/// This fails if `this` is not the only [`ARef`] pointing to the underlying
94+
/// [`Request`].
8995
///
90-
/// If the operation is successful, `Ok` is returned with a pointer to the
91-
/// C `struct request`. If the operation fails, `this` is returned in the
92-
/// `Err` variant.
96+
/// If the operation is successful, [`Ok`] is returned with a pointer to the
97+
/// C [`struct request`]. If the operation fails, `this` is returned in the
98+
/// [`Err`] variant.
99+
///
100+
/// [`struct request`]: srctree/include/linux/blk-mq.h
93101
fn try_set_end(this: ARef<Self>) -> Result<*mut bindings::request, ARef<Self>> {
94102
// We can race with `TagSet::tag_to_rq`
95103
if let Err(_old) = this.wrapper_ref().refcount().compare_exchange(
@@ -109,7 +117,7 @@ impl<T: Operations> Request<T> {
109117

110118
/// Notify the block layer that the request has been completed without errors.
111119
///
112-
/// This function will return `Err` if `this` is not the only `ARef`
120+
/// This function will return [`Err`] if `this` is not the only [`ARef`]
113121
/// referencing the request.
114122
pub fn end_ok(this: ARef<Self>) -> Result<(), ARef<Self>> {
115123
let request_ptr = Self::try_set_end(this)?;
@@ -123,13 +131,13 @@ impl<T: Operations> Request<T> {
123131
Ok(())
124132
}
125133

126-
/// Return a pointer to the `RequestDataWrapper` stored in the private area
134+
/// Return a pointer to the [`RequestDataWrapper`] stored in the private area
127135
/// of the request structure.
128136
///
129137
/// # Safety
130138
///
131139
/// - `this` must point to a valid allocation of size at least size of
132-
/// `Self` plus size of `RequestDataWrapper`.
140+
/// [`Self`] plus size of [`RequestDataWrapper`].
133141
pub(crate) unsafe fn wrapper_ptr(this: *mut Self) -> NonNull<RequestDataWrapper> {
134142
let request_ptr = this.cast::<bindings::request>();
135143
// SAFETY: By safety requirements for this function, `this` is a
@@ -141,7 +149,7 @@ impl<T: Operations> Request<T> {
141149
unsafe { NonNull::new_unchecked(wrapper_ptr) }
142150
}
143151

144-
/// Return a reference to the `RequestDataWrapper` stored in the private
152+
/// Return a reference to the [`RequestDataWrapper`] stored in the private
145153
/// area of the request structure.
146154
pub(crate) fn wrapper_ref(&self) -> &RequestDataWrapper {
147155
// SAFETY: By type invariant, `self.0` is a valid allocation. Further,
@@ -152,13 +160,15 @@ impl<T: Operations> Request<T> {
152160
}
153161
}
154162

155-
/// A wrapper around data stored in the private area of the C `struct request`.
163+
/// A wrapper around data stored in the private area of the C [`struct request`].
164+
///
165+
/// [`struct request`]: srctree/include/linux/blk-mq.h
156166
pub(crate) struct RequestDataWrapper {
157167
/// The Rust request refcount has the following states:
158168
///
159169
/// - 0: The request is owned by C block layer.
160-
/// - 1: The request is owned by Rust abstractions but there are no ARef references to it.
161-
/// - 2+: There are `ARef` references to the request.
170+
/// - 1: The request is owned by Rust abstractions but there are no [ARef] references to it.
171+
/// - 2+: There are [`ARef`] references to the request.
162172
refcount: AtomicU64,
163173
}
164174

@@ -204,7 +214,7 @@ fn atomic_relaxed_op_return(target: &AtomicU64, op: impl Fn(u64) -> u64) -> u64
204214
}
205215

206216
/// Store the result of `op(target.load)` in `target` if `target.load() !=
207-
/// pred`, returning true if the target was updated.
217+
/// pred`, returning `true` if the target was updated.
208218
fn atomic_relaxed_op_unless(target: &AtomicU64, op: impl Fn(u64) -> u64, pred: u64) -> bool {
209219
target
210220
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |x| {

0 commit comments

Comments
 (0)