Linux PCI subsystem development
 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 v21 12/13] vfio/pci: Reset PCIe TPH state on device open and close
Date: Wed, 18 Nov 2026 12:20:21 +0000	[thread overview]
Message-ID: <sashiko-outbox-220903@kernel.org> (raw)
In-Reply-To: <20261118091238.45992-13-fengchengwen@huawei.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] vfio/pci: pcie_disable_tph() after reset in
  vfio_pci_core_enable()
- [Medium] vfio/pci: unsafe TPH teardown order and cpuhp race in
  vfio_pci_core_disable()
- [Medium] vfio/pci: stale PCIe TPH state across runtime resets and
  config writes
--

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

vfio/pci: Reset PCIe TPH state on device open and close

This commit calls pcie_disable_tph() in vfio_pci_core_enable() and
vfio_pci_core_disable() to reset PCIe TPH state when opening and closing
a VFIO PCI device.

> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 049392831b35f..8d3d532cd51ab 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -606,6 +606,8 @@ int vfio_pci_core_enable(struct vfio_pci_core_device *vdev)
>  		goto out_disable_device;
>  
>  	vdev->reset_works = !ret;
> +	/* Reset TPH state for new user session */
> +	pcie_disable_tph(vdev->pdev);
>  	pci_save_state(pdev);
>  	vdev->pci_saved_state = pci_store_saved_state(pdev);
>  	if (!vdev->pci_saved_state)

[Severity: Medium]
Should pcie_disable_tph() run before pci_try_reset_function() instead of
after it?

If pdev->tph_enabled is still set when entering vfio_pci_core_enable(),
pci_try_reset_function() calls pci_save_tph_state() before the reset and
pci_restore_tph_state() after it, writing the stale PCI_TPH_CTRL and
capability-located Steering Tag table entries back into hardware.
pcie_disable_tph() then clears only PCI_TPH_CTRL and sets
pdev->tph_enabled = 0, leaving the restored ST table entries in hardware.
Because pdev->tph_enabled is now 0, the following pci_save_state() ->
pci_save_tph_state() returns early without updating the saved buffer, so
vdev->pci_saved_state retains the stale pre-disable TPH state.

> @@ -762,6 +764,9 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
>  
>  	vfio_pci_zdev_close_device(vdev);
>  
> +	/* Reset TPH state on user session exit */
> +	pcie_disable_tph(vdev->pdev);
> +
>  	/*
>  	 * If we have saved state, restore it.  If we can reset the device,
>  	 * even better.  Resetting with current state seems better than

[Severity: Medium]
Is calling pcie_disable_tph() this late in vfio_pci_core_disable() safe
against MSI-X teardown and CPU hotplug?

vfio_pci_set_irqs_ioctl() disables MSI-X and vfio_pci_tph_dev_deinit()
runs before pcie_disable_tph() is reached here.  Meanwhile,
vfio_pci_tph_dev_deinit() frees vdev->tph_st_cpu without vdev->tph_lock
before removing the cpuhp instance:

CPU0                                  CPU1
vfio_pci_core_disable()               vfio_pci_tph_cpuhp_offline()
  vfio_pci_set_irqs_ioctl()
  ...
  vfio_pci_tph_dev_deinit()
                                        if (vdev->tph_st_cpu == NULL)
    kfree(vdev->tph_st_cpu);
    vdev->tph_st_cpu = NULL;
                                        mutex_lock(&vdev->tph_lock);
                                        vdev->tph_st_cpu[i]
                                          <-- use-after-free / NULL deref
    cpuhp_state_remove_instance_nocalls()

In addition, if a CPU goes offline after vfio_pci_set_irqs_ioctl()
disables MSI-X on a PCI_TPH_LOC_MSIX device while pdev->tph_enabled is
still 1, vfio_pci_tph_cpuhp_offline() -> pcie_tph_set_st_entry() ->
pci_msix_write_tph_tag() fails with -ENXIO and logs a pci_err().
vfio_pci_tph_dev_init() similarly registers vdev->tph_cpuhp_node before
allocating and initializing vdev->tph_st_cpu to -1, and
vfio_pci_tph_cpuhp_offline() calls pcie_tph_set_st_entry() without
vfio_pci_memory_lock_and_enable().

[Severity: Medium]
What happens to pdev->tph_enabled across runtime resets during an active
user session?

This patch disables TPH on open and close, but runtime reset paths
(vfio_pci_ioctl_reset(), vfio_pci_dev_set_hot_reset(), and PCIe/AF FLR in
vfio_exp_config_write() and vfio_af_config_write()) do not call
pcie_disable_tph() or reset vdev->tph_st_cpu.  When pdev->tph_enabled is
1 across pci_try_reset_function() or pci_reset_bus(), pci_save_tph_state()
and pci_restore_tph_state() restore PCI_TPH_CTRL after the reset, leaving
TPH enabled with wiped device-specific ST tables and causing subsequent
guest writes to PCI_TPH_CTRL in vfio_tph_config_write() to either drop
ST mode changes when req == org_req or fail in pcie_enable_tph_ext() with
-EBUSY.  If a reset clears hardware PCI_TPH_CTRL to 0 without restoring
it while pdev->tph_enabled remains 1, vfio_tph_config_write() reads
org_req == 0 from hardware, so enabling TPH fails with -EBUSY in
pcie_enable_tph_ext() while writing PCI_TPH_REQ_DISABLE exits early at
req == org_req without calling pcie_disable_tph().

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

  reply	other threads:[~2026-11-18 12:20 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  9:12 [PATCH v21 00/13] vfio/pci: Add PCIe TPH support Chengwen Feng
2026-11-18  9:12 ` [PATCH v21 01/13] PCI/TPH: Fix pcie_tph_get_st_table_loc() field extraction Chengwen Feng
2026-11-18  9:29   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 02/13] PCI/TPH: Fix tph_enabled concurrent update race by bitfield packing Chengwen Feng
2026-11-18  9:38   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 03/13] PCI/TPH: Cache negotiated max requester type at probe time Chengwen Feng
2026-11-18  9:45   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 04/13] PCI/TPH: Refactor pcie_enable_tph and add requester variant Chengwen Feng
2026-11-18  9:48   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 05/13] PCI/TPH: Refactor pcie_tph_get_cpu_st & " Chengwen Feng
2026-11-18  9:53   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 06/13] PCI/TPH: Add macros to check standard and extended TPH support Chengwen Feng
2026-11-18 10:06   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 07/13] PCI/TPH: Add pcie_tph_set_st_entries() batch ST entry programming helper Chengwen Feng
2026-11-18 10:15   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 08/13] PCI/TPH: Add pci_tph_dsm_supported() helper to detect device TPH ST _DSM Chengwen Feng
2026-11-18 10:29   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 09/13] vfio/pci: Add basic NO-ST TPH VFIO device feature support Chengwen Feng
2026-11-18 10:51   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 10/13] vfio/pci: Add IV-ST TPH policy and TPH_PROGRAM batch programming support Chengwen Feng
2026-11-18 11:21   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 11/13] vfio/pci: Add DS-ST TPH policy and TPH_RESOLVE capability Chengwen Feng
2026-11-18 11:43   ` sashiko-bot
2026-11-18  9:12 ` [PATCH v21 12/13] vfio/pci: Reset PCIe TPH state on device open and close Chengwen Feng
2026-11-18 12:20   ` sashiko-bot [this message]
2026-11-18  9:12 ` [PATCH v21 13/13] vfio/pci: Expose per-device TPH state via debugfs Chengwen Feng
2026-11-18 12:27   ` 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-220903@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox