Re: [PATCH v8 1/1] rust: introduce abstractions for fwctl

From: Alexandre Courbot

Date: Sun Aug 30 2026 - 21:42:55 EST


On Mon Aug 24, 2026 at 8:06 PM JST, Gary Guo wrote:
> On Mon Aug 24, 2026 at 3:59 AM BST, Alexandre Courbot wrote:
>> On Sat Aug 15, 2026 at 8:25 AM JST, Gary Guo wrote:
>>> On Thu Aug 13, 2026 at 4:23 PM BST, Zhi Wang wrote:
>>>> diff --git a/rust/kernel/fwctl.rs b/rust/kernel/fwctl.rs
>>>> new file mode 100644
>>>> index 000000000000..e6a8513a47d0
>>>> --- /dev/null
>>>> +++ b/rust/kernel/fwctl.rs
>>>> @@ -0,0 +1,593 @@
>>>> +// SPDX-License-Identifier: GPL-2.0-only
>>>> +
>>>> +//! Abstractions for the fwctl subsystem.
>>>> +//!
>>>> +//! C header: `include/linux/fwctl.h`
>>>> +
>>>> +use crate::{
>>>> + bindings,
>>>> + container_of,
>>>> + device,
>>>> + prelude::*,
>>>> + sync::aref::{
>>>> + ARef,
>>>> + AlwaysRefCounted, //
>>>> + },
>>>> + types::Opaque, //
>>>> +};
>>>> +use core::{
>>>> + alloc::Layout,
>>>> + cell::UnsafeCell,
>>>> + marker::PhantomData,
>>>> + ptr::NonNull,
>>>> + slice, //
>>>> +};
>>>> +
>>>> +/// Returns a kmalloc-compatible allocation size for `T`.
>>>> +const fn kmalloc_aligned_size<T>() -> usize {
>>>> + Layout::new::<T>().pad_to_align().size()
>>>
>>> What's this function doing? This is just identical to `size_of::<T>()`.
>>>
>>> The layout from a specific type is already padded to its alignment. You only
>>> need to call `pad_to_align()`, say, if you are computing a layout or increasing
>>> the alignment of layout.
>>
>> IIRC this was meant to be a const version of `Kmalloc::aligned_layout`
>> [1], which although it is equivalent carries the intent better than just
>> using `size_of`. But here it would maybe be better to just make
>> `Kmalloc::aligned_layout` const and call it instead.
>
> That'd certainly be better than having a duplicate impl.
>
> However, I do wonder if we should just codify the fact that kamlloc works for
> all padded layouts (which is true for all type layouts in Rust) and so we don't
> need to call that (and perhaps also just remove the method).

(+ Danilo, Alice, Vlastimil)

The guarantee indeed exists, it is even already codified in slab.h:

* bytes. For @size of power of two bytes, the alignment is also guaranteed
* to be at least to the size. For other sizes, the alignment is guaranteed to
* be at least the largest power-of-two divisor of @size.

I guess it could still be worth using `Kmalloc::aligned_layout` (which
references that guarantee) in order to track the intent, but that local
`kmalloc_aligned_size` does look superfluous.