Re: [PATCH v6 09/26] rust: alloc: implement kernel `Box`
From: Alice Ryhl
Date: Tue Aug 20 2024 - 05:48:12 EST
On Fri, Aug 16, 2024 at 2:13 AM Danilo Krummrich <dakr@xxxxxxxxxx> wrote:
>
> `Box` provides the simplest way to allocate memory for a generic type
> with one of the kernel's allocators, e.g. `Kmalloc`, `Vmalloc` or
> `KVmalloc`.
>
> In contrast to Rust's `Box` type, the kernel `Box` type considers the
> kernel's GFP flags for all appropriate functions, always reports
> allocation failures through `Result<_, AllocError>` and remains
> independent from unstable features.
>
> Signed-off-by: Danilo Krummrich <dakr@xxxxxxxxxx>
Overall looks good to me, but I have a question:
> +impl<T: 'static, A> ForeignOwnable for Box<T, A>
> +where
> + A: Allocator,
> +{
> + type Borrowed<'a> = &'a T;
> + type BorrowedMut<'a> = &'a mut T;
> [..]
> + unsafe fn borrow_mut<'a>(ptr: *const core::ffi::c_void) -> &'a mut T {
> + // SAFETY: The safety requirements of this method ensure that the pointer is valid and that
> + // nothing else will access the value for the duration of 'a.
> + unsafe { &mut *ptr.cast_mut().cast() }
> + }
Where does this come from? It looks like you've based the series on
top of [1], but I dropped that patch a long time ago, and I don't see
it in rust-dev anymore.
Alice
[1]: https://lore.kernel.org/all/20230710074642.683831-1-aliceryhl@xxxxxxxxxx/