[PATCH] rust: list: add SAFETY comment for container_of in ListItem::prepare_to_insert

Alexander Smoliyaninov posted 1 patch 1 month, 1 week ago
rust/kernel/list/impl_list_item_mod.rs | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] rust: list: add SAFETY comment for container_of in ListItem::prepare_to_insert
Posted by Alexander Smoliyaninov 1 month, 1 week ago
From 8d8c1a4d0dbaa2653e667a0bd0b3b24df4d5ebf0 Mon Sep 17 00:00:00 2001
From: Alex Smolya <alexsmolya@gmail.com>
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 <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 | 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