[PATCH] rust: list: add SAFETY comment for container_of in ListItem::prepare_to_insert
Alexander Smoliyaninov <[email protected]>
| Newsgroups | org.kernel.vger.rust-for-linux,org.kernel.vger.linux-kernel |
|---|---|
| Message-ID | <CAD4eaOwGZR-gBM-R07+j0zQZ_b0NfCjYKgysT1i0oCE70gWq_Q@mail.gmail.com> |
From 8d8c1a4d0dbaa2653e667a0bd0b3b24df4d5ebf0 Mon Sep 17 00:00:00 2001 From: Alex Smolya <[email protected]> Date: Sun, 16 Aug 2026 09:27:53 +0200 Subject: [PATCH] rust: list: add SAFETY comment for container_of in ListItem::prepare_to_insert Document the safety rationale for `container_of!` in `ListItem::prepare_to_insert` when using `ListLinksSelfPtr`. In the `ListLinksSelfPtr` implementation of `ListItem`, `view_links(me)` delegates to `raw_get_list_links`, which yields a pointer to the `inner` field of `ListLinksSelfPtr<Self, $num>`. Therefore, `links_field` is guaranteed to point to that `inner` field, making the `container_of!` calculation sound and in-bounds of the allocation. Suggested-by: Miguel Ojeda <[email protected]> Link: https://github.com/Rust-for-Linux/linux/issues/351 Signed-off-by: Alex Smolya <[email protected]> --- rust/kernel/list/impl_list_item_mod.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rust/kernel/list/impl_list_item_mod.rs b/rust/kernel/list/impl_list_item_mod.rs index d84aedfdc..c2a1f77fb 100644 --- a/rust/kernel/list/impl_list_item_mod.rs +++ b/rust/kernel/list/impl_list_item_mod.rs @@ -276,7 +276,8 @@ unsafe fn prepare_to_insert(me: *const Self) -> *mut $crate::list::ListLinks<$nu // SAFETY: The caller promises that `me` points at a valid value of type `Self`. let links_field = unsafe { <Self as $crate::list::ListItem<$num>>::view_links(me) }; - // SAFETY: TODO. + // SAFETY: `links_field` originates from `view_links(me)`, which returns a pointer + // to the `inner` field of a `ListLinksSelfPtr<Self, $num>` inside `Self`. let container = unsafe { $crate::container_of!( links_field, $crate::list::ListLinksSelfPtr<Self, $num>, inner -- 2.55.0