Re: [PATCH] net: Refine key_len calculations in rhashtable_params
From: ericnetdev dumazet
Date: Tue Dec 17 2024 - 02:34:50 EST
Le mar. 17 déc. 2024 à 08:16, Liang Jie <buaajxlj@xxxxxxx> a écrit :
>
> From: Liang Jie <liangjie@xxxxxxxxxxx>
>
> This patch improves the calculation of key_len in the rhashtable_params
> structures across the net driver modules by replacing hardcoded sizes
> and previous calculations with appropriate macros like sizeof_field()
> and offsetofend().
>
> Previously, key_len was set using hardcoded sizes like sizeof(u32) or
> sizeof(unsigned long), or using offsetof() calculations. This patch
> replaces these with sizeof_field() and correct use of offsetofend(),
> making the code more robust, maintainable, and improving readability.
>
> Using sizeof_field() and offsetofend() provides several advantages:
> - They explicitly specify the size of the field or the end offset of a
> member being used as a key.
> - They ensure that the key_len is accurate even if the structs change in
> the future.
> - They improve code readability by clearly indicating which fields are used
> and how their sizes are determined, making the code easier to understand
> and maintain.
>
> For example, instead of:
> .key_len = sizeof(u32),
> we now use:
> .key_len = sizeof_field(struct mae_mport_desc, mport_id),
>
> And instead of:
> .key_len = offsetof(struct efx_tc_encap_match, linkage),
> we now use:
> .key_len = offsetofend(struct efx_tc_encap_match, ip_tos_mask),
>
> These changes eliminate the risk of including unintended padding or extra
> data in the key, ensuring the rhashtable functions correctly.
I do not see how this patch can eliminate padding.
If keys include holes or padding, something still needs to clear the
holes/padding in objects and lookup keys.
struct key {
u8 first_component;
u32 second_component;
};