Subject: Re: [net-next PATCH v5 03/10] octeontx2-pf: Create representor netdev

From: Simon Horman <[email-masked]>
Date: Tue, 18 Jun 2024 09:25:28 +0100

On Tue, Jun 11, 2024 at 09:52:06PM +0530, Geetha sowjanya wrote:
> Adds initial devlink support to set/get the switchdev mode.
> Representor netdevs are created for each rvu devices when
> the switch mode is set to 'switchdev'. These netdevs are
> be used to control and configure VFs.
> 
> Signed-off-by: Geetha sowjanya <[email-masked]>

...

> +void rvu_rep_destroy(struct otx2_nic *priv)
> +{
> +	struct rep_dev *rep;
> +	int rep_id;
> +
> +	rvu_rep_free_cq_rsrc(priv);
> +	for (rep_id = 0; rep_id < priv->rep_cnt; rep_id++) {
> +		rep = priv->reps[rep_id];
> +		unregister_netdev(rep->netdev);
> +		free_netdev(rep->netdev);
> +	}
> +	kfree(priv->reps);
> +}
> +
> +int rvu_rep_create(struct otx2_nic *priv, struct netlink_ext_ack *extack)
> +{
> +	int rep_cnt = priv->rep_cnt;
> +	struct net_device *ndev;
> +	struct rep_dev *rep;
> +	int rep_id, err;
> +	u16 pcifunc;
> +
> +	priv->reps = kcalloc(rep_cnt, sizeof(struct rep_dev *), GFP_KERNEL);
> +	if (!priv->reps)
> +		return -ENOMEM;
> +
> +	for (rep_id = 0; rep_id < rep_cnt; rep_id++) {
> +		ndev = alloc_etherdev(sizeof(*rep));
> +		if (!ndev) {
> +			NL_SET_ERR_MSG_FMT_MOD(extack,
> +					       "PFVF representor:%d creation failed",
> +					       rep_id);
> +			err = -ENOMEM;
> +			goto exit;
> +		}
> +
> +		rep = netdev_priv(ndev);
> +		priv->reps[rep_id] = rep;
> +		rep->mdev = priv;
> +		rep->netdev = ndev;
> +		rep->rep_id = rep_id;
> +
> +		ndev->min_mtu = OTX2_MIN_MTU;
> +		ndev->max_mtu = priv->hw.max_mtu;
> +		pcifunc = priv->rep_pf_map[rep_id];
> +		rep->pcifunc = pcifunc;
> +
> +		snprintf(ndev->name, sizeof(ndev->name), "r%dp%d", rep_id,
> +			 rvu_get_pf(pcifunc));
> +
> +		eth_hw_addr_random(ndev);
> +		err = register_netdev(ndev);
> +		if (err) {
> +			NL_SET_ERR_MSG_MOD(extack,
> +					   "PFVF reprentator registration failed");

Hi Geetha,

(The most recently allocated) ndev appears to be leaked here.

I think that one way to address this could be to moving the contents of
this loop into a separate function that unwinds the most recent allocation
on error.

Highlighted by Smatch (although it seems a bit confused here).

 .../rep.c:184 rvu_rep_create() warn: 'ndev' from alloc_etherdev_mqs() not released on lines: 184.
 .../rep.c:184 rvu_rep_create() warn: 'ndev' from register_netdev() not released on lines: 184.

Sorry for not bringing this up earlier: it is at least the third time I
have looked over this, and for some reason I didn't notice this the other
times.

> +			goto exit;
> +		}
> +	}
> +	err = rvu_rep_napi_init(priv, extack);
> +	if (err)
> +		goto exit;
> +
> +	return 0;
> +exit:
> +	while (--rep_id >= 0) {
> +		rep = priv->reps[rep_id];
> +		unregister_netdev(rep->netdev);
> +		free_netdev(rep->netdev);
> +	}
> +	kfree(priv->reps);
> +	return err;
> +}
> +
>  static int rvu_rep_rsrc_free(struct otx2_nic *priv)
>  {
>  	struct otx2_qset *qset = &priv->qset;

...