Subject: Re: [PATCH RFC v2 net-next 04/28] net/sched: act_api: add init_ops to struct tc_action_op

From: Simon Horman <[email-masked]>
Date: Mon, 5 Jun 2023 11:51:14 +0200

CC: Dan Carpenter

On Wed, May 17, 2023 at 07:02:08AM -0400, Jamal Hadi Salim wrote:
> The initialisation of P4TC action instances require access to a struct p4tc_act
> (which appears in later patches) to help us to retrieve information like the
> dynamic action parameters etc. In order to retrieve struct p4tc_act we need the
> pipeline name or id and the action name or id. Also recall that P4TC
> action IDs are dynamic and  are net namespace specific. The init callback from
> tc_action_ops parameters had no way of supplying us that information. To solve
> this issue, we decided to create a new tc_action_ops callback (init_ops), that
> provides us with the tc_action_ops struct which then provides us with the
> pipeline and action name. In addition we add a new refcount to struct
> tc_action_ops called dyn_ref, which accounts for how many action instances we
> have of a specific dynamic action.
> 
> Co-developed-by: Victor Nogueira <[email-masked]>
> Signed-off-by: Victor Nogueira <[email-masked]>
> Co-developed-by: Pedro Tammela <[email-masked]>
> Signed-off-by: Pedro Tammela <[email-masked]>
> Signed-off-by: Jamal Hadi Salim <[email-masked]>

Hi Jamal, Victor and Pedro,

> index bc4e178873e4..0ba5a4b5db6f 100644
> --- a/net/sched/act_api.c
> +++ b/net/sched/act_api.c
> @@ -1006,7 +1006,7 @@ int tcf_register_action(struct tc_action_ops *act,
>  	struct tc_action_ops *a;
>  	int ret;
>  
> -	if (!act->act || !act->dump || !act->init)
> +	if (!act->act || !act->dump || (!act->init && !act->init_ops))
>  		return -EINVAL;
>  
>  	/* We have to register pernet ops before making the action ops visible,
> @@ -1494,8 +1494,13 @@ struct tc_action *tcf_action_init_1(struct net *net, struct tcf_proto *tp,
>  			}
>  		}
>  
> -		err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
> -				userflags.value | flags, extack);
> +		if (a_o->init)
> +			err = a_o->init(net, tb[TCA_ACT_OPTIONS], est, &a, tp,
> +					userflags.value | flags, extack);
> +		else if (a_o->init_ops)
> +			err = a_o->init_ops(net, tb[TCA_ACT_OPTIONS], est, &a,
> +					    tp, a_o, userflags.value | flags,
> +					    extack);

By my reading the initialisation of a occurs here.
Which is now conditional.

>  	} else {
>  		err = a_o->init(net, nla, est, &a, tp, userflags.value | flags,
>  				extack);

A bit further down, outside of the else clause above, the code looks like
this.

        if (!police && tb[TCA_ACT_COOKIE])
                tcf_set_action_cookie(&a->user_cookie, user_cookie);

        if (!police)
                a->hw_stats = hw_stats;

Which causes Smatch to complain that a may be used uninitialised.
Is this the case?