[PATCH] rust: list: add SAFETY comment for HasListLinks in impl_has_list_links_self_ptr!

Alexander Smoliyaninov posted 1 patch 1 month, 1 week ago
rust/kernel/list/impl_list_item_mod.rs | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
[PATCH] rust: list: add SAFETY comment for HasListLinks in impl_has_list_links_self_ptr!
Posted by Alexander Smoliyaninov 1 month, 1 week ago
Document the safety rationale for the `unsafe impl HasListLinks`
generated by `impl_has_list_links_self_ptr!`.

The implementation of `raw_get_list_links` computes a pointer to the
`ListLinksSelfPtr` field and casts it to a `ListLinks` pointer. This is
sound because `ListLinksSelfPtr` is `#[repr(C)]` with `inner: ListLinks`
as its first field, and the method only compiles if the field has the
expected `ListLinksSelfPtr` type.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://github.com/Rust-for-Linux/linux/issues/351
Signed-off-by: Alex Smolya <alexsmolya@gmail.com>
---
 rust/kernel/list/impl_list_item_mod.rs | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/rust/kernel/list/impl_list_item_mod.rs
b/rust/kernel/list/impl_list_item_mod.rs
index 5a3eac9f3..d84aedfdc 100644
--- a/rust/kernel/list/impl_list_item_mod.rs
+++ b/rust/kernel/list/impl_list_item_mod.rs
@@ -86,7 +86,9 @@ macro_rules! impl_has_list_links_self_ptr {
         // right type.
         unsafe impl$(<$($generics)*>)?
$crate::list::HasSelfPtr<$item_type $(, $id)?> for $self {}

-        // SAFETY: TODO.
+        // SAFETY: `raw_get_list_links` only compiles if the field has type
+        // `ListLinksSelfPtr<$item_type, $id>`, which is `repr(C)`
and has `ListLinks` as its first
+        // field.
         unsafe impl$(<$($generics)*>)?
$crate::list::HasListLinks$(<$id>)? for $self {
             #[inline]
             unsafe fn raw_get_list_links(ptr: *mut Self) -> *mut
$crate::list::ListLinks$(<$id>)? {
-- 
2.55.0