Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Alex Williamson <alex@shazbot.org>
To: <mhonap@nvidia.com>
Cc: <ankita@nvidia.com>, <satyanarayana.k.v.p@intel.com>,
	<kjaju@nvidia.com>, <kvm@vger.kernel.org>,
	<linux-cxl@vger.kernel.org>,
	alex@shazbot.org
Subject: Re: [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only
Date: Thu, 3 Dec 2026 13:55:36 -0700	[thread overview]
Message-ID: <20261203135536.7bd80320@shazbot.org> (raw)
In-Reply-To: <20261202091105.1674275-3-mhonap@nvidia.com>

On Wed, 2 Dec 2026 14:41:05 +0530
<mhonap@nvidia.com> wrote:

> From: Manish Honap <mhonap@nvidia.com>
> 
> Since commit 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config
> Capability to user visible list."), vfio-pci passes user writes to
> every DVSEC through to the device. Before that commit DVSECs were
> hidden.
> 
> For a function that is CXL.cache and CXL.mem capable, the user can then:
> - clear Cache_Enable or Mem_Enable in DVSEC Control,
> - set Initiate CXL Reset or Initiate Cache Writeback and Invalidation
>   in DVSEC Control2, which the host does not see or handle,
> - write the DVSEC Range registers,
> - set Config Lock, after which the host cannot change the locked
>   registers.
> 
> This change keeps the CXL Device DVSEC visible and makes it read-only:
> reads return the device values and writes are dropped. The capability is
> matched by its start, so a CXL DVSEC that directly follows another DVSEC
> relies on the earlier patch to correctly calculate start of abutting
> capabilities.

"earlier patch" is only relevant to the series on the mailing list, not
to the commit once merged.
 
> Fixes: 6467d0740a2b ("vfio/pci: Add DVSEC PCI Extended Config Capability to user visible list.")
> Signed-off-by: Manish Honap <mhonap@nvidia.com>
> ---
>  drivers/vfio/pci/vfio_pci_config.c | 23 +++++++++++++++++++++++
>  include/linux/vfio_pci_core.h      |  1 +
>  2 files changed, 24 insertions(+)
> 
> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
> index dd0b899fbdfd..2bc0c7cbc4b7 100644
> --- a/drivers/vfio/pci/vfio_pci_config.c
> +++ b/drivers/vfio/pci/vfio_pci_config.c
> @@ -1746,6 +1746,23 @@ static int vfio_ecap_init(struct vfio_pci_core_device *vdev)
>  	return 0;
>  }
>  
> +static u16 vfio_pci_cxl_dvsec(struct pci_dev *pdev)
> +{
> +	u16 dvsec, cap;
> +
> +	dvsec = pci_find_dvsec_capability(pdev, PCI_VENDOR_ID_CXL,
> +					  PCI_DVSEC_CXL_DEVICE);
> +	if (!dvsec ||
> +	    pci_read_config_word(pdev, dvsec + PCI_DVSEC_CXL_CAP, &cap))
> +		return 0;
> +
> +	if (!(cap & PCI_DVSEC_CXL_CACHE_CAPABLE) ||
> +	    !(cap & PCI_DVSEC_CXL_MEM_CAPABLE))
> +		return 0;

Have we actually checked whether type 1 or 3 expose unsafe accesses
through the CXL DVSEC or are we only focused on type 2?

> +
> +	return dvsec;
> +}
> +
>  /*
>   * Nag about hardware bugs, hopefully to have vendors fix them, but at least
>   * to collect a list of dependencies for the VF INTx pin quirk below.
> @@ -1863,6 +1880,8 @@ int vfio_config_init(struct vfio_pci_core_device *vdev)
>  	if (ret)
>  		goto out;
>  
> +	vdev->cxl_dvsec = vfio_pci_cxl_dvsec(vdev->pdev);
> +

This belongs in vfio_ecap_init(), probably after we've checked for
collision in the pci_config_map:

	if (ecap == PCI_EXT_CAP_ID_DVSEC) {
		u16 cxl_dvsec = vfio_pci_probe_cxl_dvsec(pdev, epos);

		if (cxl_dvsec) {
			if (vdev->cxl_dvsec)
				pci_warn(pdev, "Device contains multiple CXL DVSEC capabilities\n");

			vdev->cxl_dvsec = cxl_dvsec;
		}
	}

The suggested probe would unfortunately duplicate the read and check of
the DVSEC headers, but this seems like the logical place to test and
add since we've already got a DVSEC in hand.

>  	return 0;
>  
>  out:
> @@ -1953,6 +1972,10 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
>  				perm = &ecap_perms[cap_id];
>  
>  			cap_start = vfio_find_cap_start(vdev, *ppos);
> +
> +			if (cap_id == PCI_EXT_CAP_ID_DVSEC &&
> +			    cap_start == vdev->cxl_dvsec)
> +				perm = &direct_ro_perms;

This is basically just open coding in the rw dispatch handler what
should be done in a perm writefn.  Define a writefn for this:

static int vfio_dvsec_config_write(struct vfio_pci_core_device *vdev, int pos,
				   int count, struct perm_bits *perm,
				   int offset, __le32 val)
{
	if (pos - offset == vdev->cxl_dvsec)
		return count;

	return vfio_raw_config_write(vdev, pos, count, perm, offset, val);
}

Set the PCI_EXT_CAP_ID_DVSEC writefn to this new function.

>  		} else {
>  			WARN_ON(cap_id > PCI_CAP_ID_MAX);
>  
> diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h
> index 2f3fa2787737..dc25f737fe93 100644
> --- a/include/linux/vfio_pci_core.h
> +++ b/include/linux/vfio_pci_core.h
> @@ -109,6 +109,7 @@ struct vfio_pci_core_device {
>  	u8			*pci_config_map;
>  	u8			*vconfig;
>  	struct perm_bits	*msi_perm;
> +	u16			cxl_dvsec;
>  	spinlock_t		irqlock;
>  	struct mutex		igate;
>  	struct xarray		ctx;

This adds a hole in the structure.  There's an existing gap after
ioeventfds_nr.  Thanks,

Alex

      parent reply	other threads:[~2026-12-03 20:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  9:11 [PATCH 0/2] vfio/pci: CXL DVSEC config space fixes mhonap
2026-12-02  9:11 ` [PATCH 1/2] vfio/pci: Find the start of an abutting extended capability mhonap
2026-12-02  9:21   ` sashiko-bot
2026-12-03 20:55   ` Alex Williamson
2026-12-02  9:11 ` [PATCH 2/2] vfio/pci: Make the CXL DVSEC read-only mhonap
2026-12-02  9:22   ` sashiko-bot
2026-12-03 20:55   ` Alex Williamson [this message]

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=20261203135536.7bd80320@shazbot.org \
    --to=alex@shazbot.org \
    --cc=ankita@nvidia.com \
    --cc=kjaju@nvidia.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=mhonap@nvidia.com \
    --cc=satyanarayana.k.v.p@intel.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