Skip to content

Commit b0ba6be

Browse files
tamirdojeda
authored andcommitted
rust: retain pointer mut-ness in container_of!
Avoid casting the input pointer to `*const _`, allowing the output pointer to be `*mut` if the input is `*mut`. This allows a number of `*const` to `*mut` conversions to be removed at the cost of slightly worse ergonomics when the macro is used with a reference rather than a pointer; the only example of this was in the macro's own doctest. Reviewed-by: Benno Lossin <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent edc5e6e commit b0ba6be

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

rust/kernel/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
204204
/// }
205205
///
206206
/// let test = Test { a: 10, b: 20 };
207-
/// let b_ptr = &test.b;
207+
/// let b_ptr: *const _ = &test.b;
208208
/// // SAFETY: The pointer points at the `b` field of a `Test`, so the resulting pointer will be
209209
/// // in-bounds of the same allocation as `b_ptr`.
210210
/// let test_alias = unsafe { container_of!(b_ptr, Test, b) };
@@ -213,9 +213,8 @@ fn panic(info: &core::panic::PanicInfo<'_>) -> ! {
213213
#[macro_export]
214214
macro_rules! container_of {
215215
($ptr:expr, $type:ty, $($f:tt)*) => {{
216-
let ptr = $ptr as *const _ as *const u8;
217216
let offset: usize = ::core::mem::offset_of!($type, $($f)*);
218-
ptr.sub(offset) as *const $type
217+
$ptr.byte_sub(offset).cast::<$type>()
219218
}}
220219
}
221220

rust/kernel/rbtree.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ where
424424
while !node.is_null() {
425425
// SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self`
426426
// point to the links field of `Node<K, V>` objects.
427-
let this = unsafe { container_of!(node, Node<K, V>, links) }.cast_mut();
427+
let this = unsafe { container_of!(node, Node<K, V>, links) };
428428
// SAFETY: `this` is a non-null node so it is valid by the type invariants.
429429
let this_key = unsafe { &(*this).key };
430430
// SAFETY: `node` is a non-null node so it is valid by the type invariants.
@@ -496,7 +496,7 @@ impl<K, V> Drop for RBTree<K, V> {
496496
// but it is not observable. The loop invariant is still maintained.
497497

498498
// SAFETY: `this` is valid per the loop invariant.
499-
unsafe { drop(KBox::from_raw(this.cast_mut())) };
499+
unsafe { drop(KBox::from_raw(this)) };
500500
}
501501
}
502502
}
@@ -761,7 +761,7 @@ impl<'a, K, V> Cursor<'a, K, V> {
761761
let next = self.get_neighbor_raw(Direction::Next);
762762
// SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self`
763763
// point to the links field of `Node<K, V>` objects.
764-
let this = unsafe { container_of!(self.current.as_ptr(), Node<K, V>, links) }.cast_mut();
764+
let this = unsafe { container_of!(self.current.as_ptr(), Node<K, V>, links) };
765765
// SAFETY: `this` is valid by the type invariants as described above.
766766
let node = unsafe { KBox::from_raw(this) };
767767
let node = RBTreeNode { node };
@@ -806,7 +806,7 @@ impl<'a, K, V> Cursor<'a, K, V> {
806806
unsafe { bindings::rb_erase(neighbor, addr_of_mut!(self.tree.root)) };
807807
// SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self`
808808
// point to the links field of `Node<K, V>` objects.
809-
let this = unsafe { container_of!(neighbor, Node<K, V>, links) }.cast_mut();
809+
let this = unsafe { container_of!(neighbor, Node<K, V>, links) };
810810
// SAFETY: `this` is valid by the type invariants as described above.
811811
let node = unsafe { KBox::from_raw(this) };
812812
return Some(RBTreeNode { node });
@@ -912,7 +912,7 @@ impl<'a, K, V> Cursor<'a, K, V> {
912912
unsafe fn to_key_value_raw<'b>(node: NonNull<bindings::rb_node>) -> (&'b K, *mut V) {
913913
// SAFETY: By the type invariant of `Self`, all non-null `rb_node` pointers stored in `self`
914914
// point to the links field of `Node<K, V>` objects.
915-
let this = unsafe { container_of!(node.as_ptr(), Node<K, V>, links) }.cast_mut();
915+
let this = unsafe { container_of!(node.as_ptr(), Node<K, V>, links) };
916916
// SAFETY: The passed `node` is the current node or a non-null neighbor,
917917
// thus `this` is valid by the type invariants.
918918
let k = unsafe { &(*this).key };
@@ -1021,7 +1021,7 @@ impl<K, V> Iterator for IterRaw<K, V> {
10211021

10221022
// SAFETY: By the type invariant of `IterRaw`, `self.next` is a valid node in an `RBTree`,
10231023
// and by the type invariant of `RBTree`, all nodes point to the links field of `Node<K, V>` objects.
1024-
let cur = unsafe { container_of!(self.next, Node<K, V>, links) }.cast_mut();
1024+
let cur = unsafe { container_of!(self.next, Node<K, V>, links) };
10251025

10261026
// SAFETY: `self.next` is a valid tree node by the type invariants.
10271027
self.next = unsafe { bindings::rb_next(self.next) };
@@ -1216,7 +1216,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
12161216
// SAFETY:
12171217
// - `self.node_links` is a valid pointer to a node in the tree.
12181218
// - We have exclusive access to the underlying tree, and can thus give out a mutable reference.
1219-
unsafe { &mut (*(container_of!(self.node_links, Node<K, V>, links).cast_mut())).value }
1219+
unsafe { &mut (*(container_of!(self.node_links, Node<K, V>, links))).value }
12201220
}
12211221

12221222
/// Converts the entry into a mutable reference to its value.
@@ -1226,7 +1226,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
12261226
// SAFETY:
12271227
// - `self.node_links` is a valid pointer to a node in the tree.
12281228
// - This consumes the `&'a mut RBTree<K, V>`, therefore it can give out a mutable reference that lives for `'a`.
1229-
unsafe { &mut (*(container_of!(self.node_links, Node<K, V>, links).cast_mut())).value }
1229+
unsafe { &mut (*(container_of!(self.node_links, Node<K, V>, links))).value }
12301230
}
12311231

12321232
/// Remove this entry from the [`RBTree`].
@@ -1239,9 +1239,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
12391239
RBTreeNode {
12401240
// SAFETY: The node was a node in the tree, but we removed it, so we can convert it
12411241
// back into a box.
1242-
node: unsafe {
1243-
KBox::from_raw(container_of!(self.node_links, Node<K, V>, links).cast_mut())
1244-
},
1242+
node: unsafe { KBox::from_raw(container_of!(self.node_links, Node<K, V>, links)) },
12451243
}
12461244
}
12471245

@@ -1272,8 +1270,7 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> {
12721270
// SAFETY:
12731271
// - `self.node_ptr` produces a valid pointer to a node in the tree.
12741272
// - Now that we removed this entry from the tree, we can convert the node to a box.
1275-
let old_node =
1276-
unsafe { KBox::from_raw(container_of!(self.node_links, Node<K, V>, links).cast_mut()) };
1273+
let old_node = unsafe { KBox::from_raw(container_of!(self.node_links, Node<K, V>, links)) };
12771274

12781275
RBTreeNode { node: old_node }
12791276
}

0 commit comments

Comments
 (0)