netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: Tony Nguyen <anthony.l.nguyen@intel.com>
Cc: davem@davemloft.net, pabeni@redhat.com, edumazet@google.com,
	Benjamin Mikailenko <benjamin.mikailenko@intel.com>,
	netdev@vger.kernel.org, Gurucharan G <gurucharanx.g@intel.com>
Subject: Re: [PATCH net-next 5/7] ice: Accumulate ring statistics over reset
Date: Tue, 15 Nov 2022 21:02:39 -0800	[thread overview]
Message-ID: <20221115210239.3a1c05ba@kernel.org> (raw)
In-Reply-To: <20221114234250.3039889-6-anthony.l.nguyen@intel.com>

On Mon, 14 Nov 2022 15:42:48 -0800 Tony Nguyen wrote:
> +static int ice_vsi_alloc_stat_arrays(struct ice_vsi *vsi)
> +{
> +	struct ice_vsi_stats *vsi_stat;
> +	struct ice_pf *pf = vsi->back;
> +	struct device *dev;
> +
> +	dev = ice_pf_to_dev(pf);
> +
> +	if (vsi->type == ICE_VSI_CHNL)
> +		return 0;
> +	if (!pf->vsi_stats)
> +		return -ENOENT;
> +
> +	vsi_stat = devm_kzalloc(dev, sizeof(*vsi_stat), GFP_KERNEL);

Don't use managed allocations if the structure's lifetime does not
match that of the driver instance. It's not generic garbage collection.

> +	if (!vsi_stat)
> +		return -ENOMEM;
> +
> +	vsi_stat->tx_ring_stats =
> +		devm_kcalloc(dev, vsi->alloc_txq,
> +			     sizeof(*vsi_stat->tx_ring_stats), GFP_KERNEL);
> +
> +	vsi_stat->rx_ring_stats =
> +		devm_kcalloc(dev, vsi->alloc_rxq,
> +			     sizeof(*vsi_stat->rx_ring_stats), GFP_KERNEL);
> +
> +	if (!vsi_stat->tx_ring_stats || !vsi_stat->rx_ring_stats)
> +		goto err_alloc;
> +
> +	pf->vsi_stats[vsi->idx] = vsi_stat;
> +
> +	return 0;
> +
> +err_alloc:

Don't do combined error checking, add appropriate labels for each case.

> +	devm_kfree(dev, vsi_stat->tx_ring_stats);
> +	vsi_stat->tx_ring_stats = NULL;

No need to clear the pointers, vsi_stats is freed few lines down.

> +	devm_kfree(dev, vsi_stat->rx_ring_stats);
> +	vsi_stat->rx_ring_stats = NULL;
> +	devm_kfree(dev, vsi_stat);
> +	pf->vsi_stats[vsi->idx] = NULL;
> +	return -ENOMEM;
> +}
> +
>  /**
>   * ice_vsi_alloc - Allocates the next available struct VSI in the PF
>   * @pf: board private structure
> @@ -560,6 +606,11 @@ ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type vsi_type,
>  
>  	if (vsi->type == ICE_VSI_CTRL && vf)
>  		vf->ctrl_vsi_idx = vsi->idx;
> +
> +	/* allocate memory for Tx/Rx ring stat pointers */
> +	if (ice_vsi_alloc_stat_arrays(vsi))
> +		goto err_rings;
> +
>  	goto unlock_pf;
>  
>  err_rings:
> @@ -1535,6 +1586,122 @@ static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
>  	return -ENOMEM;
>  }
>  
> +/**
> + * ice_vsi_free_stats - Free the ring statistics structures
> + * @vsi: VSI pointer
> + */
> +static void ice_vsi_free_stats(struct ice_vsi *vsi)
> +{
> +	struct ice_vsi_stats *vsi_stat;
> +	struct ice_pf *pf = vsi->back;
> +	struct device *dev;
> +	int i;
> +
> +	dev = ice_pf_to_dev(pf);
> +
> +	if (vsi->type == ICE_VSI_CHNL)
> +		return;
> +	if (!pf->vsi_stats)
> +		return;
> +
> +	vsi_stat = pf->vsi_stats[vsi->idx];
> +	if (!vsi_stat)
> +		return;
> +
> +	ice_for_each_alloc_txq(vsi, i) {
> +		if (vsi_stat->tx_ring_stats[i]) {
> +			kfree_rcu(vsi_stat->tx_ring_stats[i], rcu);
> +			WRITE_ONCE(vsi_stat->tx_ring_stats[i], NULL);
> +		}
> +	}
> +
> +	ice_for_each_alloc_rxq(vsi, i) {
> +		if (vsi_stat->rx_ring_stats[i]) {
> +			kfree_rcu(vsi_stat->rx_ring_stats[i], rcu);
> +			WRITE_ONCE(vsi_stat->rx_ring_stats[i], NULL);
> +		}
> +	}
> +
> +	devm_kfree(dev, vsi_stat->tx_ring_stats);
> +	vsi_stat->tx_ring_stats = NULL;
> +	devm_kfree(dev, vsi_stat->rx_ring_stats);
> +	vsi_stat->rx_ring_stats = NULL;
> +	devm_kfree(dev, vsi_stat);
> +	pf->vsi_stats[vsi->idx] = NULL;
> +}
> +
> +/**
> + * ice_vsi_alloc_ring_stats - Allocates Tx and Rx ring stats for the VSI
> + * @vsi: VSI which is having stats allocated
> + */
> +static int ice_vsi_alloc_ring_stats(struct ice_vsi *vsi)
> +{
> +	struct ice_ring_stats **tx_ring_stats;
> +	struct ice_ring_stats **rx_ring_stats;
> +	struct ice_vsi_stats *vsi_stats;
> +	struct ice_pf *pf = vsi->back;
> +	u16 i;
> +
> +	if (!pf->vsi_stats)
> +		return -ENOENT;
> +
> +	vsi_stats = pf->vsi_stats[vsi->idx];
> +	if (!vsi_stats)
> +		return -ENOENT;
> +
> +	tx_ring_stats = vsi_stats->tx_ring_stats;
> +	if (!tx_ring_stats)
> +		return -ENOENT;
> +
> +	rx_ring_stats = vsi_stats->rx_ring_stats;
> +	if (!rx_ring_stats)
> +		return -ENOENT;
> +

Why all this NULL-checking? The init should have failed if any of these
are NULL. Please avoid defensive programming.

  reply	other threads:[~2022-11-16  5:02 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-14 23:42 [PATCH net-next 0/7][pull request] 100GbE Intel Wired LAN Driver Updates 2022-11-14 (ice) Tony Nguyen
2022-11-14 23:42 ` [PATCH net-next 1/7] ice: Check for PTP HW lock more frequently Tony Nguyen
2022-11-14 23:42 ` [PATCH net-next 2/7] ice: Remove gettime HW semaphore Tony Nguyen
2022-11-14 23:42 ` [PATCH net-next 3/7] ice: Remove and replace ice speed defines with ethtool.h versions Tony Nguyen
2022-11-14 23:42 ` [PATCH net-next 4/7] ice: Accumulate HW and Netdev statistics over reset Tony Nguyen
2022-11-14 23:42 ` [PATCH net-next 5/7] ice: Accumulate ring " Tony Nguyen
2022-11-16  5:02   ` Jakub Kicinski [this message]
2022-11-16 23:34     ` Benjamin Mikailenko
2022-11-14 23:42 ` [PATCH net-next 6/7] ice: Fix configuring VIRTCHNL_OP_CONFIG_VSI_QUEUES with unbalanced queues Tony Nguyen
2022-11-14 23:42 ` [PATCH net-next 7/7] ice: Use ICE_RLAN_BASE_S instead of magic number Tony Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221115210239.3a1c05ba@kernel.org \
    --to=kuba@kernel.org \
    --cc=anthony.l.nguyen@intel.com \
    --cc=benjamin.mikailenko@intel.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=gurucharanx.g@intel.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).