Re: [PATCH net-next] lwtunnel: Add lwtunnel_encap_type_check() helper

From: Simon Horman
Date: Wed Jun 25 2025 - 14:52:39 EST


On Wed, Jun 25, 2025 at 06:24:13PM +0800, Yue Haibing wrote:
> Consolidate encap_type check to dedicated helper for code clean.
>
> Signed-off-by: Yue Haibing <yuehaibing@xxxxxxxxxx>
> ---
> net/core/lwtunnel.c | 46 ++++++++++++++++++++++-----------------------
> 1 file changed, 22 insertions(+), 24 deletions(-)
>
> diff --git a/net/core/lwtunnel.c b/net/core/lwtunnel.c
> index f9d76d85d04f..f9453f278715 100644
> --- a/net/core/lwtunnel.c
> +++ b/net/core/lwtunnel.c
> @@ -79,10 +79,18 @@ EXPORT_SYMBOL_GPL(lwtunnel_state_alloc);
> static const struct lwtunnel_encap_ops __rcu *
> lwtun_encaps[LWTUNNEL_ENCAP_MAX + 1] __read_mostly;
>
> +static inline int lwtunnel_encap_type_check(unsigned int encap_type)

Please don't use the inline keyword in .c files unless there
is a demonstratable - usually performance - reason to do so.
Which I don't think that is hte case here.

Rather, let the compiler inine functions as it sees fit.

> +{
> + if (encap_type == LWTUNNEL_ENCAP_NONE ||
> + encap_type > LWTUNNEL_ENCAP_MAX)
> + return -EINVAL;
> + return 0;
> +}
> +

I'm not entirely sure if this change is worth the churn.
But if it is, perhaps a helper of the following form is simpler.

(Completely untested!)

static bool lwtunnel_encap_type_invalid(unsigned int encap_type)
{
return encap_type == LWTUNNEL_ENCAP_NONE ||
encap_type > LWTUNNEL_ENCAP_MAX;
}


> int lwtunnel_encap_add_ops(const struct lwtunnel_encap_ops *ops,
> unsigned int num)
> {
> - if (num > LWTUNNEL_ENCAP_MAX)
> + if (lwtunnel_encap_type_check(num) < 0)

Which can then be used like this:

if (lwtunnel_encap_type_invalid(num))

> return -ERANGE;
>
> return !cmpxchg((const struct lwtunnel_encap_ops **)

...

--
pw-bot: changes-requested