All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alex Williamson <alex@shazbot.org>
To: <mhonap@nvidia.com>
Cc: <djbw@kernel.org>, <jgg@ziepe.ca>, <jic23@kernel.org>,
	<dave.jiang@intel.com>, <ankita@nvidia.com>,
	<alejandro.lucero-palau@amd.com>, <alison.schofield@intel.com>,
	<dave@stgolabs.net>, <dmatlack@google.com>, <gourry@gourry.net>,
	<ira.weiny@intel.com>, <cjia@nvidia.com>, <kjaju@nvidia.com>,
	<vsethi@nvidia.com>, <zhiw@nvidia.com>, <kvm@vger.kernel.org>,
	<linux-cxl@vger.kernel.org>, <linux-doc@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>, <linux-kselftest@vger.kernel.org>,
	alex@shazbot.org
Subject: Re: [PATCH v3 08/11] vfio/pci/cxl: Add HDM + COMP_REGS regions and DVSEC clipping shim
Date: Fri, 10 Jul 2026 16:23:09 -0600	[thread overview]
Message-ID: <20260710162309.2c257883@shazbot.org> (raw)
In-Reply-To: <20260625165407.1769572-9-mhonap@nvidia.com>

On Thu, 25 Jun 2026 22:24:04 +0530
<mhonap@nvidia.com> wrote:
> diff --git a/drivers/vfio/pci/vfio_pci_config.c b/drivers/vfio/pci/vfio_pci_config.c
> index a10ed733f0e3..b9f30a33515a 100644
> --- a/drivers/vfio/pci/vfio_pci_config.c
> +++ b/drivers/vfio/pci/vfio_pci_config.c
> @@ -1898,8 +1898,15 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
>  	/*
>  	 * Chop accesses into aligned chunks containing no more than a
>  	 * single capability.  Caller increments to the next chunk.
> +	 *
> +	 * For CXL Type-2 devices also clip at the CXL Device DVSEC body
> +	 * boundary so the generic perm-bits path handles the DVSEC
> +	 * header bytes and the CXL hook handles the body bytes; without
> +	 * this clip a 32-bit access at dvsec + 0x08 would span the
> +	 * generic Header2 word and the CXL CAPABILITY word.
>  	 */
>  	count = min(count, vfio_pci_cap_remaining_dword(vdev, *ppos));
> +	count = min(count, vfio_pci_cxl_config_boundary(vdev, *ppos));
>  	if (count >= 4 && !(*ppos % 4))
>  		count = 4;
>  	else if (count >= 2 && !(*ppos % 2))
> @@ -1909,6 +1916,30 @@ ssize_t vfio_pci_config_rw_single(struct vfio_pci_core_device *vdev,
>  
>  	ret = count;
>  
> +	/*
> +	 * Give the CXL Type-2 hook first claim on this access: if the
> +	 * range lies inside the CXL Device DVSEC body, forward it to
> +	 * cxl-core's register-virtualization helpers instead of the
> +	 * standard perm-bits path.  -ENOENT means "not for me; use the
> +	 * default path"; any other negative value is a hard error.
> +	 */
> +	if (vdev->cxl) {
> +		__le32 le_val = 0;
> +		ssize_t cxl_ret;
> +
> +		if (iswrite && copy_from_user(&le_val, buf, count))
> +			return -EFAULT;
> +		cxl_ret = vfio_pci_cxl_config_rw(vdev, *ppos, count, &le_val,
> +						 iswrite);
> +		if (cxl_ret >= 0) {
> +			if (!iswrite && copy_to_user(buf, &le_val, count))
> +				return -EFAULT;
> +			return cxl_ret;
> +		}
> +		if (cxl_ret != -ENOENT)
> +			return cxl_ret;
> +	}
> +

I think the solution here is just to set the .readfn and .writefn for
PCI_EXT_CAP_ID_DVSEC to dvsec specific handlers, rather than the raw
write and direct read handlers.  The new handlers would detect whether
the reference is to the CXL DVSEC body, possibly via ranges stored in
vdev->cxl, and either call through to CXL handlers via cxl_ops
(previously suggested), or fall through to the raw/direct handlers.

>  	cap_id = vdev->pci_config_map[*ppos];
>  
>  	if (cap_id == PCI_CAP_ID_INVALID) {
> diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c
> index 05ab4ae59157..2d2dae278d1e 100644
> --- a/drivers/vfio/pci/vfio_pci_core.c
> +++ b/drivers/vfio/pci/vfio_pci_core.c
> @@ -501,6 +501,23 @@ static void vfio_pci_core_map_bars(struct vfio_pci_core_device *vdev)
>  		if (!pci_resource_len(pdev, i))
>  			continue;
>  
> +		/*
> +		 * cxl-core already holds request_mem_region() on the CXL
> +		 * component register sub-range of this BAR.  Skip the
> +		 * full-BAR request so we do not collide with that
> +		 * sub-region; vfio still owns the BAR via the driver
> +		 * binding and the iomap below succeeds without a region
> +		 * claim.
> +		 */
> +		if (vdev->cxl && bar == vfio_pci_cxl_get_component_reg_bar(vdev)) {
> +			vdev->barmap[bar] = pci_iomap(pdev, bar, 0);
> +			if (!vdev->barmap[bar]) {
> +				pci_dbg(pdev, "Failed to iomap region %d\n", bar);
> +				vdev->barmap[bar] = IOMEM_ERR_PTR(-ENOMEM);
> +			}
> +			continue;
> +		}
> +
>  		if (pci_request_selected_regions(pdev, 1 << bar, "vfio")) {
>  			pci_dbg(pdev, "Failed to reserve region %d\n", bar);
>  			vdev->barmap[bar] = IOMEM_ERR_PTR(-EBUSY);
> @@ -701,7 +718,10 @@ void vfio_pci_core_disable(struct vfio_pci_core_device *vdev)
>  		if (IS_ERR_OR_NULL(vdev->barmap[bar]))
>  			continue;
>  		pci_iounmap(pdev, vdev->barmap[bar]);
> -		pci_release_selected_regions(pdev, 1 << bar);
> +		/* Mirror the asymmetric setup-time skip in map_bars(). */
> +		if (!(vdev->cxl &&
> +		      i == vfio_pci_cxl_get_component_reg_bar(vdev)))
> +			pci_release_selected_regions(pdev, 1 << bar);

It would be much less ugly to create
vfio_pci_{request,release}_selected_region() wrappers that mask whether
the region is actually requested or released than to disrupt the code
flow like this.  Likewise below, think about creating wrappers that do
the right thing for cxl and are no-ops otherwise.  For example, embed
the vdev->cxl test into the function to cleanup the callers.  Thanks,

Alex

>  		vdev->barmap[bar] = NULL;
>  	}
>  
> @@ -1051,6 +1071,16 @@ static int vfio_pci_ioctl_get_info(struct vfio_pci_core_device *vdev,
>  	info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
>  	info.num_irqs = VFIO_PCI_NUM_IRQS;
>  
> +	if (vdev->cxl) {
> +		ret = vfio_pci_cxl_get_info(vdev, &caps);
> +		if (ret) {
> +			pci_warn(vdev->pdev,
> +				 "Failed to add CXL info capability\n");
> +			return ret;
> +		}
> +		info.flags |= VFIO_DEVICE_FLAGS_CXL;
> +	}
> +
>  	ret = vfio_pci_info_zdev_add_caps(vdev, &caps);
>  	if (ret && ret != -ENODEV) {
>  		pci_warn(vdev->pdev,
> @@ -1093,6 +1123,12 @@ int vfio_pci_ioctl_get_region_info(struct vfio_device *core_vdev,
>  	struct pci_dev *pdev = vdev->pdev;
>  	int i, ret;
>  
> +	if (vdev->cxl) {
> +		ret = vfio_pci_cxl_get_region_info(vdev, info, caps);
> +		if (ret != -ENOTTY)
> +			return ret;
> +	}
> +
>  	switch (info->index) {
>  	case VFIO_PCI_CONFIG_REGION_INDEX:
>  		info->offset = VFIO_PCI_INDEX_TO_OFFSET(info->index);

  parent reply	other threads:[~2026-07-10 22:23 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-25 16:53 [PATCH v3 00/11] vfio/pci: Add CXL Type-2 device passthrough support mhonap
2026-06-25 16:53 ` [PATCH v3 01/11] cxl: Add cxl_get_hdm_info() helper for HDM decoder metadata mhonap
2026-07-10  1:00   ` Dan Williams (nvidia)
2026-06-25 16:53 ` [PATCH v3 02/11] cxl: Split cxl_await_range_active() from media-ready wait mhonap
2026-06-25 16:53 ` [PATCH v3 03/11] cxl: Record BIR and BAR offset in cxl_register_map mhonap
2026-06-25 16:54 ` [PATCH v3 04/11] cxl: Move component/HDM register defines to uapi/cxl/cxl_regs.h mhonap
2026-06-25 16:54 ` [PATCH v3 05/11] vfio: UAPI for CXL Type-2 device passthrough mhonap
2026-07-10 22:23   ` Alex Williamson
2026-06-25 16:54 ` [PATCH v3 06/11] cxl: Add register-virtualization helpers for vfio Type-2 passthrough mhonap
2026-07-10 21:56   ` Dan Williams (nvidia)
2026-06-25 16:54 ` [PATCH v3 07/11] vfio/pci: Add CONFIG_VFIO_PCI_CXL with bind-time CXL Type-2 acquisition mhonap
2026-07-10 22:23   ` Alex Williamson
2026-06-25 16:54 ` [PATCH v3 08/11] vfio/pci/cxl: Add HDM + COMP_REGS regions and DVSEC clipping shim mhonap
2026-07-10 22:09   ` Dan Williams (nvidia)
2026-07-10 22:23   ` Alex Williamson [this message]
2026-06-25 16:54 ` [PATCH v3 09/11] selftests/vfio: Add CXL Type-2 device passthrough smoke test mhonap
2026-06-25 16:54 ` [PATCH v3 10/11] docs: vfio-pci: Document CXL Type-2 device passthrough mhonap
2026-06-25 16:54 ` [PATCH v3 11/11] vfio/pci: Provide opt-out for CXL Type-2 extensions mhonap
2026-06-26  9:16 ` [PATCH v3 00/11] vfio/pci: Add CXL Type-2 device passthrough support Richard Cheng
2026-07-10 16:26 ` Dave Jiang

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=20260710162309.2c257883@shazbot.org \
    --to=alex@shazbot.org \
    --cc=alejandro.lucero-palau@amd.com \
    --cc=alison.schofield@intel.com \
    --cc=ankita@nvidia.com \
    --cc=cjia@nvidia.com \
    --cc=dave.jiang@intel.com \
    --cc=dave@stgolabs.net \
    --cc=djbw@kernel.org \
    --cc=dmatlack@google.com \
    --cc=gourry@gourry.net \
    --cc=ira.weiny@intel.com \
    --cc=jgg@ziepe.ca \
    --cc=jic23@kernel.org \
    --cc=kjaju@nvidia.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-cxl@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mhonap@nvidia.com \
    --cc=vsethi@nvidia.com \
    --cc=zhiw@nvidia.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 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.