All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Lukasz Laguna" <lukasz.laguna@intel.com>, linux-pci@vger.kernel.org
Subject: Re: [PATCH v2 02/13] PCI/IOV: Introduce VF iterators
Date: Tue, 27 Oct 2026 16:21:20 +0000	[thread overview]
Message-ID: <sashiko-outbox-186901@kernel.org> (raw)
In-Reply-To: <20261027160446.858300-3-lukasz.laguna@intel.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] PCI/IOV: divide by zero in pci_get_vf() when PF VF stride is zero
--

commit a822ae2dea34f9ae5ccc5c50cffaa927173aa6ac
Author: Lukasz Laguna <lukasz.laguna@intel.com>

PCI/IOV: Introduce VF iterators

This commit introduces pci_get_vf() and pci_get_vf_reverse() along with
for_each_pci_vf(), for_each_pci_vf_reverse(), and
for_each_pci_vf_continue_reverse() macros to iterate over the enabled VFs
of a PF.

> diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
> index f6e74847df72c..da895e7f2683e 100644
> --- a/drivers/pci/iov.c
> +++ b/drivers/pci/iov.c
> @@ -70,6 +70,69 @@ struct pci_dev *pci_iov_get_virtfn(struct pci_dev *dev, int id)
>  }
>  EXPORT_SYMBOL_GPL(pci_iov_get_virtfn);
>  
> +/**
> + * pci_get_vf() - Return the first or next existing VF of a PF
> + * @dev: PCI PF whose VFs are being iterated
> + * @from: Previous VF returned by this function, or %NULL to return the first VF
> + *
> + * Iterate over existing VFs of @dev in ascending VF ID order, skipping VF IDs
> + * with no device present. If a VF is found, its reference count is incremented.
> + * A new iteration is started by passing %NULL as the @from argument. Otherwise,
> + * iteration continues after @from, and the reference count on @from is
> + * decremented.
> + *
> + * Return: a pointer to the first or next VF, or %NULL if no further VF is found.
> + */
> +struct pci_dev *pci_get_vf(struct pci_dev *dev, struct pci_dev *from)
> +{
> +	int id = 0;
> +
> +	if (from) {
> +		id = pci_iov_vf_id(from) + 1;
> +		pci_dev_put(from);
> +	}
> +
> +	for (; id < pci_num_vf(dev); id++) {
> +		from = pci_iov_get_virtfn(dev, id);
> +		if (from)
> +			return from;
> +	}
> +
> +	return NULL;
> +}
> +EXPORT_SYMBOL_GPL(pci_get_vf);
> +
> +/**
> + * pci_get_vf_reverse() - Return the last or previous existing VF of a PF
> + * @dev: PCI PF whose VFs are being iterated
> + * @from: Previous VF returned by this function, or %NULL to return the last VF
> + *
> + * Same semantics as pci_get_vf(), except iteration proceeds in descending VF
> + * ID order.
> + *
> + * Return: a pointer to the last or previous VF, or %NULL if no further VF is
> + * found.
> + */
> +struct pci_dev *pci_get_vf_reverse(struct pci_dev *dev, struct pci_dev *from)
> +{
> +	int id = pci_num_vf(dev);
> +
> +	if (from) {
> +		id = pci_iov_vf_id(from);
> +		pci_dev_put(from);
> +	}

[Severity: High]
Can pci_get_vf() and pci_get_vf_reverse() trigger a divide-by-zero when
iterating over a PF whose SR-IOV VF stride (pf->sriov->stride) is 0?

In drivers/pci/iov.c, compute_max_vf_buses() validates offset and stride,
explicitly permitting iov->stride == 0 when nr_virtfn == 1 (per commit
ea9a8854161d, "allow numVF==1 && stride==0"):

drivers/pci/iov.c:compute_max_vf_buses() {
    ...
	for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) {
		pci_iov_set_numvfs(dev, nr_virtfn);
		if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) {
			rc = -EIO;
			goto out;
		}
    ...
}

When SR-IOV is enabled with nr_virtfn == 1 on a device reporting stride == 0,
sriov_enable() -> pci_iov_add_virtfn(dev, 0) succeeds because
pci_iov_virtfn_bus(dev, 0) and pci_iov_virtfn_devfn(dev, 0) multiply
dev->sriov->stride by 0, creating VF 0 with pf->sriov->stride == 0.

When iterating via for_each_pci_vf() or for_each_pci_vf_reverse(), the first
call with from == NULL looks up VF 0 via pci_iov_get_virtfn(dev, 0) and
returns it. On the subsequent step to advance past VF 0 and terminate the
loop, pci_get_vf() and pci_get_vf_reverse() call pci_iov_vf_id(from), which
divides unconditionally by pf->sriov->stride:

drivers/pci/iov.c:pci_iov_vf_id() {
    ...
	pf = pci_physfn(dev);
	return (pci_dev_id(dev) - (pci_dev_id(pf) + pf->sriov->offset)) /
	       pf->sriov->stride;
}

A similar division by zero can also occur if NumVFs was reset to 0 via
pci_iov_set_numvfs(dev, 0), which re-reads PCI_SRIOV_VF_STRIDE while
NumVFs == 0.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261027160446.858300-1-lukasz.laguna@intel.com?part=2

  reply	other threads:[~2026-10-27 16:21 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-27 16:04 [PATCH v2 00/13] PCI/IOV: Improve reset handling with VFs enabled Lukasz Laguna
2026-10-27 16:04 ` [PATCH v2 01/13] PCI/IOV: Introduce pci_iov_get_virtfn() Lukasz Laguna
2026-10-27 16:20   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 02/13] PCI/IOV: Introduce VF iterators Lukasz Laguna
2026-10-27 16:21   ` sashiko-bot [this message]
2026-10-27 21:35   ` Bjorn Helgaas
2026-10-28  7:29     ` Laguna, Lukasz
2026-10-27 16:04 ` [PATCH v2 03/13] PCI/IOV: Introduce VF locking helpers Lukasz Laguna
2026-10-27 16:26   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 04/13] PCI/IOV: Allow VF drivers to handle resets Lukasz Laguna
2026-10-27 16:34   ` sashiko-bot
2026-10-27 16:51     ` Laguna, Lukasz
2026-10-28  8:52       ` Weili Qian
2026-10-28 14:05         ` Laguna, Lukasz
2026-10-27 20:00   ` Bjorn Helgaas
2026-10-28  6:39     ` Laguna, Lukasz
2026-10-27 16:04 ` [PATCH v2 05/13] ionic: Lock VFs around pci_reset_function_locked() Lukasz Laguna
2026-10-27 16:14   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 06/13] pds_core: " Lukasz Laguna
2026-10-27 16:12   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 07/13] i40e: Drop VF MSI state restore from PF reset_done Lukasz Laguna
2026-10-27 16:13   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 08/13] PCI/IOV: Allow bus reset_method for PF with VFs enabled Lukasz Laguna
2026-10-27 16:20   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 09/13] PCI/TSM: Use generic VF iterators Lukasz Laguna
2026-10-27 16:15   ` sashiko-bot
2026-10-27 20:08   ` Bjorn Helgaas
2026-10-28  7:25     ` Laguna, Lukasz
2026-10-27 16:04 ` [PATCH v2 10/13] ixgbe: Use generic VF iterator Lukasz Laguna
2026-10-27 16:17   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 11/13] ice: Use pci_iov_get_virtfn() Lukasz Laguna
2026-10-27 16:20   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 12/13] liquidio: " Lukasz Laguna
2026-10-27 16:19   ` sashiko-bot
2026-10-27 16:04 ` [PATCH v2 13/13] drm/xe/pf: " Lukasz Laguna
2026-10-27 16:21   ` sashiko-bot
2026-10-27 19:49 ` [PATCH v2 00/13] PCI/IOV: Improve reset handling with VFs enabled Bjorn Helgaas
2026-10-28  7:03   ` Laguna, Lukasz

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=sashiko-outbox-186901@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=lukasz.laguna@intel.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.