Re: [PATCH net v3] net: Refine key_len calculations in rhashtable_params
From: Jakub Kicinski
Date: Fri Dec 27 2024 - 14:15:03 EST
On Fri, 20 Dec 2024 16:24:36 +0800 Liang Jie wrote:
> 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 in certain scenarios of including
> unintended padding or extra data in the key, ensuring the rhashtable
> functions correctly.
IMHO the change is not worth the churn. Does any upstream code checker
/ tool prevent from new instances of this pattern occurring?
--
pw-bot: reject