Subject: Re: [PATCH RFC v2 net-next 05/28] net/sched: act_api: introduce tc_lookup_action_byid()

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

On Wed, May 17, 2023 at 07:02:09AM -0400, Jamal Hadi Salim wrote:
> Introduce a lookup helper to retrieve the tc_action_ops
> instance given its action id.
> 
> 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]>
> ---
>  include/net/act_api.h |  1 +
>  net/sched/act_api.c   | 35 +++++++++++++++++++++++++++++++++++
>  2 files changed, 36 insertions(+)
> 
> diff --git a/include/net/act_api.h b/include/net/act_api.h
> index 363f7f8b5586..34b9a9ff05ee 100644
> --- a/include/net/act_api.h
> +++ b/include/net/act_api.h
> @@ -205,6 +205,7 @@ int tcf_idr_release(struct tc_action *a, bool bind);
>  
>  int tcf_register_action(struct tc_action_ops *a, struct pernet_operations *ops);
>  int tcf_register_dyn_action(struct net *net, struct tc_action_ops *act);
> +struct tc_action_ops *tc_lookup_action_byid(struct net *net, u32 act_id);
>  int tcf_unregister_action(struct tc_action_ops *a,
>  			  struct pernet_operations *ops);
>  int tcf_unregister_dyn_action(struct net *net, struct tc_action_ops *act);
> diff --git a/net/sched/act_api.c b/net/sched/act_api.c
> index 0ba5a4b5db6f..101c6debf356 100644

> --- a/net/sched/act_api.c
> +++ b/net/sched/act_api.c
> @@ -1084,6 +1084,41 @@ int tcf_unregister_dyn_action(struct net *net, struct tc_action_ops *act)
>  }
>  EXPORT_SYMBOL(tcf_unregister_dyn_action);
>  
> +/* lookup by ID */
> +struct tc_action_ops *tc_lookup_action_byid(struct net *net, u32 act_id)
> +{
> +	struct tcf_dyn_act_net *base_net;
> +	struct tc_action_ops *a, *res = NULL;

Hi Jamal, Victor and Pedro,

A minor nit from my side: as this is networking code, please use reverse
xmas tree - longest line to shortest - for local variable declarations.

> +
> +	if (!act_id)
> +		return NULL;
> +
> +	read_lock(&act_mod_lock);
> +
> +	list_for_each_entry(a, &act_base, head) {
> +		if (a->id == act_id) {
> +			if (try_module_get(a->owner)) {
> +				read_unlock(&act_mod_lock);
> +				return a;
> +			}
> +			break;
> +		}
> +	}
> +	read_unlock(&act_mod_lock);
> +
> +	read_lock(&base_net->act_mod_lock);

base_net does not appear to be initialised here.

> +
> +	base_net = net_generic(net, dyn_act_net_id);
> +	a = idr_find(&base_net->act_base, act_id);
> +	if (a && try_module_get(a->owner))
> +		res = a;
> +
> +	read_unlock(&base_net->act_mod_lock);
> +
> +	return res;
> +}
> +EXPORT_SYMBOL(tc_lookup_action_byid);
> +
>  /* lookup by name */
>  static struct tc_action_ops *tc_lookup_action_n(struct net *net, char *kind)
>  {
> -- 
> 2.25.1
>