All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chengwen Feng" <fengchengwen@huawei.com>
Cc: kvm@vger.kernel.org, linux-pci@vger.kernel.org
Subject: Re: [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support
Date: Mon, 23 Nov 2026 02:58:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-228576@kernel.org> (raw)
In-Reply-To: <20261123023628.7728-12-fengchengwen@huawei.com>

Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] vfio/pci: OOB read in vfio_pci_tph_resolve_st_tag() on
  cpu_online()
- [Medium] vfio/pci: ST table desync across TPH enable/mode and MSI-X
  transitions
- [Medium] vfio/pci: ST table and config accesses while device is
  suspended or in D3
- [Low] vfio/pci: unconditional memory_lock and per-entry ST writes on
  CPU offline
--

commit e8463ab09aa5bab8dbaf6df575e6c544d6e783aa
Author: Chengwen Feng <fengchengwen@huawei.com>

vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support

This commit adds the VFIO_PCI_TPH_POLICY_IV_ST policy, introduces the
VFIO_DEVICE_FEATURE_TPH_PROGRAM ioctl to batch program architected
steering tag entries, and registers a CPU hotplug offline callback to
clear entries bound to offlined CPUs.

> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index be7d1921f5f31..83b81d1a61227 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
[ ... ]
> @@ -1603,12 +1613,135 @@ static int vfio_pci_core_feature_token(struct vfio_pci_core_device *vdev,
>  	return 0;
>  }
>  
> -static u32 vfio_pci_tph_get_capability(struct vfio_pci_core_device *vdev)
> +static enum cpuhp_state vfio_tph_cpuhp_state;
> +
> +static int vfio_pci_tph_cpuhp_offline(unsigned int cpu, struct hlist_node *node)
>  {
> -	/* NO-ST only, no capability bits exposed now */
> +	struct vfio_pci_core_device *vdev =
> +		hlist_entry(node, struct vfio_pci_core_device, tph_cpuhp_node);
> +	u16 cmd;
> +	int i;
> +
> +	if (vdev->tph_st_cpu == NULL)
> +		return 0;
> +
> +	mutex_lock(&vdev->tph_lock);
> +	cmd = vfio_pci_memory_lock_and_enable(vdev);
> +	for (i = 0; i < vdev->tph_st_size; i++) {
> +		if (vdev->tph_st_cpu[i] != (s64)cpu)
> +			continue;
> +		pcie_tph_set_st_entry(vdev->pdev, i, 0);
> +		vdev->tph_st_cpu[i] = (s64)-1;
> +	}
> +	vfio_pci_memory_unlock_and_restore(vdev, cmd);
> +	mutex_unlock(&vdev->tph_lock);
> +
>  	return 0;
>  }

[Severity: Medium]
Can vfio_pci_tph_cpuhp_offline() and vfio_pci_core_feature_tph_program()
access config space and the MSI-X table BAR while the device is runtime
suspended or in PCI_D3hot?

Unlike VFIO file operations, vfio_pci_tph_cpuhp_offline() runs from a CPU
hotplug thread without calling pm_runtime_resume_and_get().  In both this
callback and vfio_pci_core_feature_tph_program(),
vfio_pci_memory_lock_and_enable() sets PCI_COMMAND_MEMORY without checking
or transitioning pdev->current_state out of guest-initiated PCI_D3hot, so
pcie_tph_set_st_entries() -> pci_msix_write_tph_tag() can issue MMIO
accesses to the MSI-X BAR while the device is in D3hot or D3cold.

Also, on devices with vdev->needs_pm_restore set,
vfio_pci_set_power_state() saves config space into vdev->pm_save on D3hot
entry and restores it via pci_restore_state() -> pci_restore_tph_state()
on return to PCI_D0, which overwrites any ST entries cleared on CPU
offline while in D3hot.

[Severity: Low]
Does vfio_pci_tph_cpuhp_offline() need to acquire vdev->memory_lock and
read/write PCI_COMMAND when no ST entry is bound to the offlined CPU?

Because vfio_pci_tph_dev_init() registers every open device with an
architected ST table, every CPU offline event takes vdev->memory_lock in
write mode and touches PCI_COMMAND even when no entries match cpu, and
calling pcie_tph_set_st_entry() per entry repeatedly toggles PCI_TPH_CTRL
and re-reads PCI_TPH_CAP for each matching entry.

[ ... ]

> @@ -1644,6 +1777,144 @@ static int vfio_pci_core_feature_tph(struct vfio_pci_core_device *vdev,
>  	return copy_to_user(arg, &tph, sizeof(tph)) ? -EFAULT : 0;
>  }
>  
> +static u32 vfio_pci_tph_get_src_mask(struct vfio_pci_core_device *vdev)
> +{
> +	u32 flags = vfio_pci_tph_get_capability(vdev);
> +	u32 src_mask = 0;
> +
> +	if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_NONE)
> +		src_mask |= VFIO_DEVICE_TPH_SRC_NONE;
> +	if (flags & VFIO_DEVICE_TPH_CAP_PROG_ST_CPU)
> +		src_mask |= VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
> +			    VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
> +
> +	return src_mask;
> +}
> +
> +static int vfio_pci_tph_resolve_st_tag(struct pci_dev *pdev, u32 src_bits,
> +				       u32 src_handle, bool extended, u16 *st)
> +{
> +	u8 req_type = extended ? PCI_TPH_REQ_EXT_TPH : PCI_TPH_REQ_TPH_ONLY;
> +	u32 cpu_srcs = VFIO_DEVICE_TPH_SRC_CPU_VOLATILE |
> +		       VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT;
> +	int ret = 0;
> +
> +	if ((src_bits & cpu_srcs) && !cpu_online(src_handle)) {
> +		*st = 0;
> +		return -EINVAL;
> +	}

[Severity: High]
Can a user-supplied src_handle >= nr_cpu_ids trigger an out-of-bounds read
in cpu_online(src_handle)?

vfio_pci_core_feature_tph_program() copies arbitrary u32 values from
program.srcs into src_handle[] and passes them here without bounds
checking.  On SMP kernels, cpu_online() calls cpumask_test_cpu(), which
expects cpu < nr_cpu_ids and performs an unchecked test_bit() read relative
to __cpu_online_mask.  Checking src_handle >= nr_cpu_ids before calling
cpu_online() avoids the out-of-bounds access.

> +
> +	if (src_bits & VFIO_DEVICE_TPH_SRC_NONE)
> +		*st = 0;
> +	else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_VOLATILE)
> +		ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_VM,
> +					      req_type, src_handle, st);
> +	else if (src_bits & VFIO_DEVICE_TPH_SRC_CPU_PERSISTENT)
> +		ret = pcie_tph_get_cpu_st_ext(pdev, TPH_MEM_TYPE_PM,
> +					      req_type, src_handle, st);
> +	else
> +		ret = -EINVAL;
> +
> +	if (ret != 0)
> +		*st = 0;
> +
> +	return ret;
> +}
[ ... ]
> +	if (i > 0) {
> +		mutex_lock(&vdev->tph_lock);
> +
> +		cmd = vfio_pci_memory_lock_and_enable(vdev);
> +		ret = written = pcie_tph_set_st_entries(pdev, program.start, i,
> +							tags);
> +		vfio_pci_memory_unlock_and_restore(vdev, cmd);
> +
> +		if (written > 0) {
> +			for (k = 0; k < written; k++)
> +				vdev->tph_st_cpu[program.start + k] =
> +					(tags[k] == 0) ?
> +					(s64)-1 : (s64)src_handle[k];
> +		}
> +
> +		mutex_unlock(&vdev->tph_lock);
> +	}

[Severity: Medium]
Can vdev->tph_st_cpu[] become desynchronized from the hardware ST table
across PCI_TPH_CTRL mode/enable and MSI-X transitions?

In drivers/pci/tph.c, pcie_tph_set_st_entries() returns -EINVAL when
!pdev->tph_enabled and returns count without writing to hardware when
pdev->tph_mode == PCI_TPH_ST_NS_MODE (and pci_msix_write_tph_tag() returns
-ENXIO when !pdev->msix_enabled):

drivers/pci/tph.c:pcie_tph_set_st_entries() {
    ...
	if (!pdev->tph_enabled)
		return -EINVAL;

	if (!count)
		return -EINVAL;

	/* No need to write tag if device is in "No ST Mode" */
	if (pdev->tph_mode == PCI_TPH_ST_NS_MODE)
		return count;
    ...
}

If userspace invokes VFIO_DEVICE_FEATURE_TPH_PROGRAM while TPH is enabled
in PCI_TPH_ST_NS_MODE, pcie_tph_set_st_entries() skips the hardware writes
and returns i, so vdev->tph_st_cpu[] and the return value report that the
entries were programmed even though the hardware ST table was not updated.

Conversely, if a bound CPU goes offline while userspace has temporarily
disabled TPH, switched to PCI_TPH_ST_NS_MODE, or disabled MSI-X,
pcie_tph_set_st_entry(vdev->pdev, i, 0) in vfio_pci_tph_cpuhp_offline()
does not clear the hardware entry, yet vdev->tph_st_cpu[i] is reset to -1,
leaving the offlined CPU's stale Steering Tag in hardware when IV-ST mode
or MSI-X is later re-enabled.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123023628.7728-1-fengchengwen@huawei.com?part=11

  reply	other threads:[~2026-11-23  2:58 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23  2:36 [PATCH v22 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 06/13] PCI/TPH: Add helpers to check standard and extended TPH support Chengwen Feng
2026-11-23  2:47   ` sashiko-bot
2026-11-23  2:36 ` [PATCH v22 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
2026-11-23  2:48   ` sashiko-bot
2026-11-23  2:36 ` [PATCH v22 08/13] PCI/TPH: Add pcie_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 09/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 10/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
2026-11-23  2:36 ` [PATCH v22 11/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
2026-11-23  2:58   ` sashiko-bot [this message]
2026-11-23  2:36 ` [PATCH v22 12/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
2026-11-23  2:50   ` sashiko-bot
2026-11-23  2:36 ` [PATCH v22 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
2026-11-23  2:52 ` [PATCH v22 00/13] vfio/pci: Add PCIe TPH support fengchengwen
2026-11-23  2:58 ` sashiko-bot

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-228576@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=fengchengwen@huawei.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --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.