Skip to contentSkip to conversation

Retrieve tph from dmabuf for PCIe P2P memory access

RFC In review Zhiping Zhang 5 participants 23 messages Mar 24 – Apr 16

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v2 cover Retrieve tph from dmabuf for PCIe P2P memory access

Currently, the steering tag can be used for a CPU on the motherboard; the ACPI check is in place to query and obtain the supported tph settings. Here we intend to use the tph info to improve RDMA NIC memory access on a vfio-based accelerator device via PCIe peer-to-peer. When an application registers a RDMA memory region with DMABUF for the RDMA NIC to access the device memory, the tph info of the memory region can be retrieved and used to set the steering tag / process hint (ph). Additional instructions or hints can be passed to the GPU or accelerator device for advanced memory operations, such as, read cache selection.

Note this RFC is for the discussion on the direction and is not intended to be a complete implementation. If no objection, we will convert it to a Patch set.

Changes v1 -> v2:
- Encode steering tag and ph in vfio_device_feature_dma_buf flags field instead of adding new uapi struct fields, to preserve ABI compatibility
- Fix subject prefixes: "Vfio:" -> "vfio:", "RMDA MLX5:" -> "RDMA/mlx5:"
- Fix raw steering tag vs st_index mismatch: get_tph() now returns a raw steering tag, and the mlx5 consumer converts it to an st_index via the new mlx5_st_alloc_index_by_tag() API
- Fix @tph doc typo to @steering_tag in dma-buf.h
- Remove unused variable, fix parameter alignment, fix trailing semicolon

Zhiping Zhang (2):
vfio: add callback to get tph info for dmabuf RDMA/mlx5: get tph for p2p access when registering dmabuf mr

 drivers/infiniband/hw/mlx5/mr.c               | 34 +++++++++++++++++++
 .../net/ethernet/mellanox/mlx5/core/lib/st.c  | 23 +++++++++----
 drivers/vfio/pci/vfio_pci_dmabuf.c            | 26 ++++++++++++--
 include/linux/dma-buf.h                       | 30 ++++++++++++++++
 include/linux/mlx5/driver.h                   |  7 ++++
 include/uapi/linux/vfio.h                     |  9 +++--
 6 files changed, 118 insertions(+), 11 deletions(-)

--
2.52.0

#1 lore raw

RFC v2 1/2 vfio: add callback to get tph info for dmabuf

This patch adds a callback to get the tph info on DMA buffer exporters. The tph info includes both the steering tag and the process hint (ph).

The steering tag and ph are encoded in the flags field of vfio_device_feature_dma_buf instead of adding new fields to the uapi struct, to preserve ABI compatibility.

---
 drivers/vfio/pci/vfio_pci_dmabuf.c | 26 ++++++++++++++++++++++++--
 include/linux/dma-buf.h            | 30 ++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h          |  9 +++++++--
 3 files changed, 61 insertions(+), 4 deletions(-)
drivers/vfio/pci/vfio_pci_dmabuf.c +24 -2
index 478beafc6ac3..c45cb3884b85 100644
@@ -17,6 +17,8 @@ struct vfio_pci_dma_buf {
 	struct phys_vec *phys_vec;
 	struct p2pdma_provider *provider;
 	u32 nr_ranges;
+	u16 steering_tag;
+	u8 ph;
 	u8 revoked : 1;
 };
 
@@ -60,6 +62,15 @@ vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment,
 				       priv->size, dir);
 }
 
+static int vfio_pci_dma_buf_get_tph(struct dma_buf *dmabuf, u16 *steering_tag,
+				    u8 *ph)
+{
+	struct vfio_pci_dma_buf *priv = dmabuf->priv;
+	*steering_tag = priv->steering_tag;
+	*ph = priv->ph;
+	return 0;
+}
+
 static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment,
 				   struct sg_table *sgt,
 				   enum dma_data_direction dir)
@@ -90,6 +101,7 @@ static const struct dma_buf_ops vfio_pci_dmabuf_ops = {
 	.unpin = vfio_pci_dma_buf_unpin,
 	.attach = vfio_pci_dma_buf_attach,
 	.map_dma_buf = vfio_pci_dma_buf_map,
+	.get_tph = vfio_pci_dma_buf_get_tph,
 	.unmap_dma_buf = vfio_pci_dma_buf_unmap,
 	.release = vfio_pci_dma_buf_release,
 };
@@ -228,7 +240,10 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
 	if (copy_from_user(&get_dma_buf, arg, sizeof(get_dma_buf)))
 		return -EFAULT;
 
-	if (!get_dma_buf.nr_ranges || get_dma_buf.flags)
+	if (!get_dma_buf.nr_ranges ||
+	    (get_dma_buf.flags & ~(VFIO_DMABUF_FL_TPH |
+				   VFIO_DMABUF_TPH_PH_MASK |
+				   VFIO_DMABUF_TPH_ST_MASK)))
 		return -EINVAL;
 
 	/*
@@ -285,7 +300,14 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
 		ret = PTR_ERR(priv->dmabuf);
 		goto err_dev_put;
 	}
-
+	if (get_dma_buf.flags & VFIO_DMABUF_FL_TPH) {
+		priv->steering_tag = (get_dma_buf.flags &
+				      VFIO_DMABUF_TPH_ST_MASK) >>
+				     VFIO_DMABUF_TPH_ST_SHIFT;
+		priv->ph = (get_dma_buf.flags &
+			    VFIO_DMABUF_TPH_PH_MASK) >>
+			   VFIO_DMABUF_TPH_PH_SHIFT;
+	}
 	/* dma_buf_put() now frees priv */
 	INIT_LIST_HEAD(&priv->dmabufs_elm);
 	down_write(&vdev->memory_lock);
include/linux/dma-buf.h +30 -0
index 133b9e637b55..26705c83ad80 100644
@@ -113,6 +113,36 @@ struct dma_buf_ops {
 	 */
 	void (*unpin)(struct dma_buf_attachment *attach);
 
+	/**
+	 * @get_tph:
+	 *
+	 * Get the TPH (TLP Processing Hints) for this DMA buffer.
+	 *
+	 * This callback allows DMA buffer exporters to provide TPH including
+	 * both the steering tag and the process hints (ph), which can be used
+	 * to optimize peer-to-peer (P2P) memory access. The TPH info is typically
+	 * used in scenarios where:
+	 * - A PCIe device (e.g., RDMA NIC) needs to access memory on another
+	 *   PCIe device (e.g., GPU),
+	 * - The system supports TPH and can use steering tags / ph to optimize
+	 *   cache placement and memory access patterns,
+	 * - The memory is exported via DMABUF for cross-device sharing.
+	 *
+	 * @dmabuf: [in] The DMA buffer for which to retrieve TPH
+	 * @steering_tag: [out] Pointer to store the 16-bit TPH steering tag value
+	 * @ph: [out] Pointer to store the 8-bit TPH processing-hint value
+	 *
+	 * Returns:
+	 * * 0 - Success, steering tag stored in @steering_tag
+	 * * -EOPNOTSUPP - TPH steering tags not supported for this buffer
+	 * * -EINVAL - Invalid parameters
+	 *
+	 * This callback is optional. If not implemented, the buffer does not
+	 * support TPH.
+	 *
+	 */
+	int (*get_tph)(struct dma_buf *dmabuf, u16 *steering_tag, u8 *ph);
+
 	/**
 	 * @map_dma_buf:
 	 *
include/uapi/linux/vfio.h +7 -2
index bb7b89330d35..e2a8962641d2 100644
@@ -1505,8 +1505,13 @@ struct vfio_region_dma_range {
 struct vfio_device_feature_dma_buf {
 	__u32	region_index;
 	__u32	open_flags;
-	__u32   flags;
-	__u32   nr_ranges;
+	__u32	flags;
+#define VFIO_DMABUF_FL_TPH		(1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT	1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK	0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT	16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK		0xffff0000U
+	__u32	nr_ranges;
 	struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
 };
 

--
2.52.0

#2 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 13 lines · Zhiping Zhang

This patch adds a callback to get the tph info on DMA buffer exporters. The tph info includes both the steering tag and the process hint (ph).

The steering tag and ph are encoded in the flags field of vfio_device_feature_dma_buf instead of adding new fields to the uapi struct, to preserve ABI compatibility.

---
 drivers/vfio/pci/vfio_pci_dmabuf.c | 26 ++++++++++++++++++++++++--
 include/linux/dma-buf.h            | 30 ++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h          |  9 +++++++--
 3 files changed, 61 insertions(+), 4 deletions(-)

<...>

Quoted 6 lines
include/uapi/linux/vfio.h +0 -0
index bb7b89330d35..e2a8962641d2 100644
@@ -1505,8 +1505,13 @@ struct vfio_region_dma_range {
 struct vfio_device_feature_dma_buf {

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1513

Quoted 10 lines
include/uapi/linux/vfio.h +6 -2
 	__u32	region_index;
 	__u32	open_flags;
-	__u32   flags;
-	__u32   nr_ranges;
+	__u32	flags;
+#define VFIO_DMABUF_FL_TPH		(1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT	1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK	0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT	16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK		0xffff0000U

This extension of flags is basically kills future extension of this struct for anything that includes TPH.

Add new

enum vfio_device_feature_dma_buf_flags {
    VFIO_DMABUF_FL_TPH  = 1 << 0
}

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1514

Quoted 1 line
include/uapi/linux/vfio.h +1 -0
+	__u32	nr_ranges;

add your "__u16 steering_tag" and "__u8 ph" fields here.

Quoted 7 lines
include/uapi/linux/vfio.h +0 -1
 	struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
 };
 
--

2.52.0

#3 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 16 lines · Leon Romanovsky

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1513

Quoted 11 lines · Zhiping Zhang

struct vfio_device_feature_dma_buf {

include/uapi/linux/vfio.h +6 -2
 	__u32	region_index;
 	__u32	open_flags;
-	__u32   flags;
-	__u32   nr_ranges;
+	__u32	flags;
+#define VFIO_DMABUF_FL_TPH		(1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT	1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK	0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT	16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK		0xffff0000U

This extension of flags is basically kills future extension of this struct for anything that includes TPH.

Quoted 8 lines

Add new

enum vfio_device_feature_dma_buf_flags {
    VFIO_DMABUF_FL_TPH  = 1 << 0
}

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1514

Quoted 1 line
include/uapi/linux/vfio.h +1 -0
+	__u32	nr_ranges;

add your "__u16 steering_tag" and "__u8 ph" fields here.

You're suggesting that Ziping append the new fields to the end of this struct? I don't think we can modify the layout of a uapi.

If we can't carve the space for this out of the existing unused flags field, I think we'd have to introduce a new vfio device feature that basically copies VFIO_DEVICE_FEATURE_DMA_BUF with the extra hints fields.

Quoted 2 lines
Quoted 2 lines

struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
};

#4 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 19 lines · Keith Busch
Quoted 2 lines
Quoted 16 lines · Leon Romanovsky

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1513

Quoted 11 lines · Zhiping Zhang
 struct vfio_device_feature_dma_buf {
    __u32   region_index;
    __u32   open_flags;
include/uapi/linux/vfio.h +6 -2
-   __u32   flags;
-   __u32   nr_ranges;
+   __u32   flags;
+#define VFIO_DMABUF_FL_TPH         (1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT   1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK    0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT   16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK            0xffff0000U

This extension of flags is basically kills future extension of this struct for anything that includes TPH.

Quoted 4 lines
Quoted 4 lines

Add new

enum vfio_device_feature_dma_buf_flags {
    VFIO_DMABUF_FL_TPH  = 1 << 0
}

yes we can do that.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

+ __u32 nr_ranges;

add your "__u16 steering_tag" and "__u8 ph" fields here.

That is what I did in V1, Leon.

Quoted 8 lines

You're suggesting that Ziping append the new fields to the end of this struct? I don't think we can modify the layout of a uapi.

If we can't carve the space for this out of the existing unused flags field, I think we'd have to introduce a new vfio device feature that basically copies VFIO_DEVICE_FEATURE_DMA_BUF with the extra hints fields.

if not using the fields in the flag, then we probably have to introduce a new vfio
device feature.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
    struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);

};

#5 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 25 lines · Zhiping Zhang
Quoted 19 lines · Keith Busch
Quoted 2 lines
Quoted 16 lines · Leon Romanovsky

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1513

Quoted 11 lines · Zhiping Zhang
 struct vfio_device_feature_dma_buf {
    __u32   region_index;
    __u32   open_flags;
include/uapi/linux/vfio.h +6 -2
-   __u32   flags;
-   __u32   nr_ranges;
+   __u32   flags;
+#define VFIO_DMABUF_FL_TPH         (1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT   1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK    0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT   16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK            0xffff0000U

This extension of flags is basically kills future extension of this struct for anything that includes TPH.

Quoted 4 lines
Quoted 4 lines

Add new

enum vfio_device_feature_dma_buf_flags {
    VFIO_DMABUF_FL_TPH  = 1 << 0
}
Quoted 8 lines

yes we can do that.

Quoted 5 lines
Quoted 4 lines
Quoted 2 lines

+ __u32 nr_ranges;

add your "__u16 steering_tag" and "__u8 ph" fields here.

That is what I did in V1, Leon.

Not really, you did only half of the work. You didn't introduce new flag and didn't calculate "old dma range" position.

Thanks

#6 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

In reply to Keith Busch
Quoted 22 lines · Keith Busch
Quoted 21 lines · Leon Romanovsky

Review

Quoted 11 lines · Zhiping Zhang

struct vfio_device_feature_dma_buf {

(quoted diff) +6 -2
 	__u32	region_index;
 	__u32	open_flags;
-	__u32   flags;
-	__u32   nr_ranges;
+	__u32	flags;
+#define VFIO_DMABUF_FL_TPH		(1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT	1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK	0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT	16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK		0xffff0000U

This extension of flags is basically kills future extension of this struct for anything that includes TPH.

Add new

enum vfio_device_feature_dma_buf_flags {
    VFIO_DMABUF_FL_TPH  = 1 << 0
}
Quoted 6 lines
Quoted 3 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+	__u32	nr_ranges;

add your "__u16 steering_tag" and "__u8 ph" fields here.

You're suggesting that Ziping append the new fields to the end of this struct? I don't think we can modify the layout of a uapi.

He needs to add before flex array. This struct is submitted by the user and kernel can easily calculate the position of that array.

Something like this:

drivers/vfio/pci/vfio_pci_dmabuf.c +5 -1
index b1d658b8f7b51..d78d915992232 100644
@@ -237,7 +237,11 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
        if (get_dma_buf.region_index >= VFIO_PCI_ROM_REGION_INDEX)
                return -ENODEV;
 
-       dma_ranges = memdup_array_user(&arg->dma_ranges, get_dma_buf.nr_ranges,
+       if (!tph_supplied)
+               dma_ranges = memdup_array_user(old_dma_ranges_pos, get_dma_buf.nr_ranges,
+                                      sizeof(*dma_ranges));
+       else
+               dma_ranges = memdup_array_user(&arg->dma_ranges, get_dma_buf.nr_ranges,
                                       sizeof(*dma_ranges));
        if (IS_ERR(dma_ranges))
                return PTR_ERR(dma_ranges);

~

Thanks

Quoted 8 lines

If we can't carve the space for this out of the existing unused flags field, I think we'd have to introduce a new vfio device feature that basically copies VFIO_DEVICE_FEATURE_DMA_BUF with the extra hints fields.

Quoted 2 lines
Quoted 2 lines

struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
};

#7 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 7 lines · Leon Romanovsky
Quoted 3 lines · Keith Busch

You're suggesting that Ziping append the new fields to the end of this struct? I don't think we can modify the layout of a uapi.

He needs to add before flex array. This struct is submitted by the user and kernel can easily calculate the position of that array.

No, you can't just do that. Existing applications would break when they compile against the updated kernel header. They don't know about this new "tph" supplied flag, but they'll all accidently use the new dma_ranges offset.

Quoted 19 lines

Something like this:

drivers/vfio/pci/vfio_pci_dmabuf.c +5 -1
index b1d658b8f7b51..d78d915992232 100644
@@ -237,7 +237,11 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
        if (get_dma_buf.region_index >= VFIO_PCI_ROM_REGION_INDEX)
                return -ENODEV;
 
-       dma_ranges = memdup_array_user(&arg->dma_ranges, get_dma_buf.nr_ranges,
+       if (!tph_supplied)
+               dma_ranges = memdup_array_user(old_dma_ranges_pos, get_dma_buf.nr_ranges,
+                                      sizeof(*dma_ranges));
+       else
+               dma_ranges = memdup_array_user(&arg->dma_ranges, get_dma_buf.nr_ranges,
                                       sizeof(*dma_ranges));
        if (IS_ERR(dma_ranges))
                return PTR_ERR(dma_ranges);

~

#8 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 6 lines · Keith Busch
Quoted 5 lines · Leon Romanovsky
Quoted 3 lines · Keith Busch

You're suggesting that Ziping append the new fields to the end of this struct? I don't think we can modify the layout of a uapi.

Quoted 7 lines
Quoted 2 lines

He needs to add before flex array. This struct is submitted by the user and kernel can easily calculate the position of that array.

No, you can't just do that. Existing applications would break when they compile against the updated kernel header. They don't know about this new "tph" supplied flag, but they'll all accidently use the new dma_ranges offset.

So we need to always pass TPH flag and treat 0 as do-nothing-field.

Thanks

#9 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 10 lines · Leon Romanovsky
Quoted 9 lines · Keith Busch
Quoted 7 lines · Leon Romanovsky
Quoted 3 lines · Keith Busch

You're suggesting that Ziping append the new fields to the end of this struct? I don't think we can modify the layout of a uapi.

He needs to add before flex array. This struct is submitted by the user and kernel can easily calculate the position of that array.

Quoted 6 lines
Quoted 4 lines

No, you can't just do that. Existing applications would break when they compile against the updated kernel header. They don't know about this new "tph" supplied flag, but they'll all accidently use the new dma_ranges offset.

So we need to always pass TPH flag and treat 0 as do-nothing-field.

I don't think you're understanding the implications. If Zhiping appends new fields in front of the flex array dma_ranges, then existing applications will implicitly use the new offset if they are recompiled against the new kernel header. But if the binary was compiled against the older kernel header, then that application would use the previous offset. Both applications have the TPH flag cleared to 0. How is the kernel supposed to know which offset the application used?

#10 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 18 lines · Keith Busch
Quoted 10 lines · Leon Romanovsky
Quoted 9 lines · Keith Busch
Quoted 7 lines · Leon Romanovsky
Quoted 3 lines · Keith Busch

You're suggesting that Ziping append the new fields to the end of this struct? I don't think we can modify the layout of a uapi.

He needs to add before flex array. This struct is submitted by the user and kernel can easily calculate the position of that array.

Quoted 6 lines
Quoted 4 lines

No, you can't just do that. Existing applications would break when they compile against the updated kernel header. They don't know about this new "tph" supplied flag, but they'll all accidently use the new dma_ranges offset.

So we need to always pass TPH flag and treat 0 as do-nothing-field.

Quoted 7 lines

I don't think you're understanding the implications. If Zhiping appends new fields in front of the flex array dma_ranges, then existing applications will implicitly use the new offset if they are recompiled against the new kernel header. But if the binary was compiled against the older kernel header, then that application would use the previous offset. Both applications have the TPH flag cleared to 0. How is the kernel supposed to know which offset the application used?

I understand, my proposal is always set TPH flag when new struct is used. Everything will be much easier if we can add fields after flex array.

Thanks

#11 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 2 lines · Leon Romanovsky

I understand, my proposal is always set TPH flag when new struct is used.

An existing application recompiled against the new kernel api implicitly uses the new struct layout without setting the TPH flag, so kernel and application are out of sync on where dma_ranges exists with your proposal.

#12 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 8 lines · Keith Busch
Quoted 2 lines · Leon Romanovsky

I understand, my proposal is always set TPH flag when new struct is used.

An existing application recompiled against the new kernel api implicitly uses the new struct layout without setting the TPH flag, so kernel and application are out of sync on where dma_ranges exists with your proposal.

Right, what about adding TPH fields to struct vfio_region_dma_range instead of struct vfio_device_feature_dma_buf?

Thanks

#13 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 3 lines · Leon Romanovsky

Right, what about adding TPH fields to struct vfio_region_dma_range instead of struct vfio_device_feature_dma_buf?

You might have to show me with code what you're talking about because I can't see any way we can add fields to any struct here without breaking backward compatibility.

If we can't claim bits out of the unused "flags" field for this feature, then my initial reply is the only sane approach: we can introduce a new feature and struct for it that closely mirrors the existing one, but with the extra hint fields.

#14 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 5 lines · Keith Busch
Quoted 3 lines · Leon Romanovsky

Right, what about adding TPH fields to struct vfio_region_dma_range instead of struct vfio_device_feature_dma_buf?

Quoted 8 lines

You might have to show me with code what you're talking about because I can't see any way we can add fields to any struct here without breaking backward compatibility.

If we can't claim bits out of the unused "flags" field for this feature, then my initial reply is the only sane approach: we can introduce a new feature and struct for it that closely mirrors the existing one, but with the extra hint fields.

Something like that, on top of this proposal:

drivers/vfio/pci/vfio_pci_dmabuf.c +5 -10
index 3961afa640391..70d5ee1e3ef7b 100644
@@ -241,9 +241,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
 		return -EFAULT;
 
 	if (!get_dma_buf.nr_ranges ||
-	    (get_dma_buf.flags & ~(VFIO_DMABUF_FL_TPH |
-				   VFIO_DMABUF_TPH_PH_MASK |
-				   VFIO_DMABUF_TPH_ST_MASK)))
+	    (get_dma_buf.flags & ~VFIO_DMABUF_FLAG_TPH))
 		return -EINVAL;
 
 	/*
@@ -300,13 +298,10 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
 		ret = PTR_ERR(priv->dmabuf);
 		goto err_dev_put;
 	}
-	if (get_dma_buf.flags & VFIO_DMABUF_FL_TPH) {
-		priv->steering_tag = (get_dma_buf.flags &
-				      VFIO_DMABUF_TPH_ST_MASK) >>
-				     VFIO_DMABUF_TPH_ST_SHIFT;
-		priv->ph = (get_dma_buf.flags &
-			    VFIO_DMABUF_TPH_PH_MASK) >>
-			   VFIO_DMABUF_TPH_PH_SHIFT;
+	if (get_dma_buf.flags & VFIO_DMABUF_FLAG_TPH) {
+		priv->steering_tag =
+			dma_ranges[get_dma_buf.nr_ranges + 1].tph.tag;
+		priv->ph = dma_ranges[get_dma_buf.nr_ranges + 1].tph.ph;
 	}
 	/* dma_buf_put() now frees priv */
 	INIT_LIST_HEAD(&priv->dmabufs_elm);
include/uapi/linux/vfio.h +17 -7
index e2a8962641d2c..a8b8d8b1a3278 100644
@@ -1497,20 +1497,30 @@ struct vfio_device_feature_bus_master {
  */
 #define VFIO_DEVICE_FEATURE_DMA_BUF 11
 
+struct vfio_region_dma_tph {
+	u16 tag;
+	u8 ph;
+};
+
 struct vfio_region_dma_range {
-	__u64 offset;
-	__u64 length;
+	union {
+		__u64 offset;
+		struct vfio_region_dma_tph tph;
+	};
+	union {
+		__u64 length;
+		__u64 reserved;
+	};
+};
+
+enum {
+	VFIO_DMABUF_FLAG_TPH = 1 << 0,
 };
 
 struct vfio_device_feature_dma_buf {
 	__u32	region_index;
 	__u32	open_flags;
 	__u32	flags;
-#define VFIO_DMABUF_FL_TPH		(1U << 0) /* TPH info is present */
-#define VFIO_DMABUF_TPH_PH_SHIFT	1         /* bits 1-2: PH (2-bit) */
-#define VFIO_DMABUF_TPH_PH_MASK	0x6U
-#define VFIO_DMABUF_TPH_ST_SHIFT	16        /* bits 16-31: steering tag */
-#define VFIO_DMABUF_TPH_ST_MASK		0xffff0000U
 	__u32	nr_ranges;
 	struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
 };
#15 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 84 lines · Leon Romanovsky
Quoted 2 lines
Quoted 5 lines · Keith Busch
Quoted 3 lines · Leon Romanovsky

Right, what about adding TPH fields to struct vfio_region_dma_range instead of struct vfio_device_feature_dma_buf?

Quoted 8 lines

You might have to show me with code what you're talking about because I can't see any way we can add fields to any struct here without breaking backward compatibility.

If we can't claim bits out of the unused "flags" field for this feature, then my initial reply is the only sane approach: we can introduce a new feature and struct for it that closely mirrors the existing one, but with the extra hint fields.

Something like that, on top of this proposal:

drivers/vfio/pci/vfio_pci_dmabuf.c +5 -10
index 3961afa640391..70d5ee1e3ef7b 100644
@@ -241,9 +241,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
                return -EFAULT;
 
        if (!get_dma_buf.nr_ranges ||
-           (get_dma_buf.flags & ~(VFIO_DMABUF_FL_TPH |
-                                  VFIO_DMABUF_TPH_PH_MASK |
-                                  VFIO_DMABUF_TPH_ST_MASK)))
+           (get_dma_buf.flags & ~VFIO_DMABUF_FLAG_TPH))
                return -EINVAL;
 
        /*
@@ -300,13 +298,10 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
                ret = PTR_ERR(priv->dmabuf);
                goto err_dev_put;
        }
-       if (get_dma_buf.flags & VFIO_DMABUF_FL_TPH) {
-               priv->steering_tag = (get_dma_buf.flags &
-                                     VFIO_DMABUF_TPH_ST_MASK) >>
-                                    VFIO_DMABUF_TPH_ST_SHIFT;
-               priv->ph = (get_dma_buf.flags &
-                           VFIO_DMABUF_TPH_PH_MASK) >>
-                          VFIO_DMABUF_TPH_PH_SHIFT;
+       if (get_dma_buf.flags & VFIO_DMABUF_FLAG_TPH) {
+               priv->steering_tag =
+                       dma_ranges[get_dma_buf.nr_ranges + 1].tph.tag;
+               priv->ph = dma_ranges[get_dma_buf.nr_ranges + 1].tph.ph;
        }
        /* dma_buf_put() now frees priv */
        INIT_LIST_HEAD(&priv->dmabufs_elm);
include/uapi/linux/vfio.h +17 -2
index e2a8962641d2c..a8b8d8b1a3278 100644
@@ -1497,20 +1497,30 @@ struct vfio_device_feature_bus_master {
  */
 #define VFIO_DEVICE_FEATURE_DMA_BUF 11
 
+struct vfio_region_dma_tph {
+       u16 tag;
+       u8 ph;
+};
+
 struct vfio_region_dma_range {
-       __u64 offset;
-       __u64 length;
+       union {
+               __u64 offset;
+               struct vfio_region_dma_tph tph;
+       };
+       union {
+               __u64 length;
+               __u64 reserved;
+       };
+};
+
+enum {
+       VFIO_DMABUF_FLAG_TPH = 1 << 0,
 };
 
 struct vfio_device_feature_dma_buf {
        __u32   region_index;

include/uapi/linux/vfio.h struct vfio_device_feature_bus_master { · line 1526

Quoted 10 lines
include/uapi/linux/vfio.h +0 -5
        __u32   open_flags;
        __u32   flags;
-#define VFIO_DMABUF_FL_TPH             (1U << 0) /* TPH info is present */
-#define VFIO_DMABUF_TPH_PH_SHIFT       1         /* bits 1-2: PH (2-bit) */
-#define VFIO_DMABUF_TPH_PH_MASK        0x6U
-#define VFIO_DMABUF_TPH_ST_SHIFT       16        /* bits 16-31: steering tag */
-#define VFIO_DMABUF_TPH_ST_MASK                0xffff0000U
        __u32   nr_ranges;
        struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
 };

Sounds good, thanks! We will follow up and move this RFC to a formal patch.

Zhiping

#16 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 83 lines · Zhiping Zhang
Quoted 82 lines · Leon Romanovsky
Quoted 2 lines
Quoted 5 lines · Keith Busch
Quoted 3 lines · Leon Romanovsky

Right, what about adding TPH fields to struct vfio_region_dma_range instead of struct vfio_device_feature_dma_buf?

Quoted 8 lines

You might have to show me with code what you're talking about because I can't see any way we can add fields to any struct here without breaking backward compatibility.

If we can't claim bits out of the unused "flags" field for this feature, then my initial reply is the only sane approach: we can introduce a new feature and struct for it that closely mirrors the existing one, but with the extra hint fields.

Something like that, on top of this proposal:

drivers/vfio/pci/vfio_pci_dmabuf.c +5 -10
index 3961afa640391..70d5ee1e3ef7b 100644
@@ -241,9 +241,7 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
                return -EFAULT;
 
        if (!get_dma_buf.nr_ranges ||
-           (get_dma_buf.flags & ~(VFIO_DMABUF_FL_TPH |
-                                  VFIO_DMABUF_TPH_PH_MASK |
-                                  VFIO_DMABUF_TPH_ST_MASK)))
+           (get_dma_buf.flags & ~VFIO_DMABUF_FLAG_TPH))
                return -EINVAL;
 
        /*
@@ -300,13 +298,10 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
                ret = PTR_ERR(priv->dmabuf);
                goto err_dev_put;
        }
-       if (get_dma_buf.flags & VFIO_DMABUF_FL_TPH) {
-               priv->steering_tag = (get_dma_buf.flags &
-                                     VFIO_DMABUF_TPH_ST_MASK) >>
-                                    VFIO_DMABUF_TPH_ST_SHIFT;
-               priv->ph = (get_dma_buf.flags &
-                           VFIO_DMABUF_TPH_PH_MASK) >>
-                          VFIO_DMABUF_TPH_PH_SHIFT;
+       if (get_dma_buf.flags & VFIO_DMABUF_FLAG_TPH) {
+               priv->steering_tag =
+                       dma_ranges[get_dma_buf.nr_ranges + 1].tph.tag;
+               priv->ph = dma_ranges[get_dma_buf.nr_ranges + 1].tph.ph;
        }
        /* dma_buf_put() now frees priv */
        INIT_LIST_HEAD(&priv->dmabufs_elm);
include/uapi/linux/vfio.h +17 -2
index e2a8962641d2c..a8b8d8b1a3278 100644
@@ -1497,20 +1497,30 @@ struct vfio_device_feature_bus_master {
  */
 #define VFIO_DEVICE_FEATURE_DMA_BUF 11
 
+struct vfio_region_dma_tph {
+       u16 tag;
+       u8 ph;
+};
+
 struct vfio_region_dma_range {
-       __u64 offset;
-       __u64 length;
+       union {
+               __u64 offset;
+               struct vfio_region_dma_tph tph;
+       };
+       union {
+               __u64 length;
+               __u64 reserved;
+       };
+};
+
+enum {
+       VFIO_DMABUF_FLAG_TPH = 1 << 0,
 };
 
Quoted 14 lines

include/uapi/linux/vfio.h struct vfio_device_feature_bus_master { · line 1526

Quoted 12 lines
include/uapi/linux/vfio.h +0 -5
 struct vfio_device_feature_dma_buf {
        __u32   region_index;
        __u32   open_flags;
        __u32   flags;
-#define VFIO_DMABUF_FL_TPH             (1U << 0) /* TPH info is present */
-#define VFIO_DMABUF_TPH_PH_SHIFT       1         /* bits 1-2: PH (2-bit) */
-#define VFIO_DMABUF_TPH_PH_MASK        0x6U
-#define VFIO_DMABUF_TPH_ST_SHIFT       16        /* bits 16-31: steering tag */
-#define VFIO_DMABUF_TPH_ST_MASK                0xffff0000U
        __u32   nr_ranges;
        struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
 };

Sounds good, thanks! We will follow up and move this RFC to a formal patch.

Great. Also, please rename "struct vfio_region_dma_range dma_ranges" to something that makes it clear this is a storage object, not something limited to a DMA range.

Thanks

Quoted 3 lines

Zhiping

#17 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

In reply to Leon Romanovsky
Quoted 1 line · Leon Romanovsky

Something like that, on top of this proposal:

...

Quoted 11 lines
include/uapi/linux/vfio.h +8 -2
+struct vfio_region_dma_tph {
+	u16 tag;
+	u8 ph;
+};
+
 struct vfio_region_dma_range {
-	__u64 offset;
-	__u64 length;
+	union {
+		__u64 offset;
+		struct vfio_region_dma_tph tph;

include/uapi/linux/vfio.h struct vfio_device_feature_bus_master { · line 1518

Quoted 10 lines
include/uapi/linux/vfio.h +9 -0
+	};
+	union {
+		__u64 length;
+		__u64 reserved;
+	};
+};
+
+enum {
+	VFIO_DMABUF_FLAG_TPH = 1 << 0,
 };

Okay, so you have the hints as a separate action from the dmabuf creation. I was trying to set it up in one shot, but this proposal may be fine. We'll try this idea out internally.

#18 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 21 lines · Keith Busch
Quoted 1 line · Leon Romanovsky

Something like that, on top of this proposal:

...

Quoted 16 lines
include/uapi/linux/vfio.h +13 -2
+struct vfio_region_dma_tph {
+	u16 tag;
+	u8 ph;
+};
+
 struct vfio_region_dma_range {
-	__u64 offset;
-	__u64 length;
+	union {
+		__u64 offset;
+		struct vfio_region_dma_tph tph;
+	};
+	union {
+		__u64 length;
+		__u64 reserved;
+	};
Quoted 8 lines

include/uapi/linux/vfio.h struct vfio_device_feature_bus_master { · line 1518

Quoted 5 lines
include/uapi/linux/vfio.h +4 -0
+};
+
+enum {
+	VFIO_DMABUF_FLAG_TPH = 1 << 0,
 };

Okay, so you have the hints as a separate action from the dmabuf creation.

I'm not sure I fully understood your point, but in my proposal the entire operation is done in one pass, with the hints provided as the final entry in the dma_range array.

Thanks

#19 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

In reply to Zhiping Zhang

Hi Zhiping,

Quoted 28 lines · Zhiping Zhang

This patch adds a callback to get the tph info on DMA buffer exporters. The tph info includes both the steering tag and the process hint (ph).

The steering tag and ph are encoded in the flags field of vfio_device_feature_dma_buf instead of adding new fields to the uapi struct, to preserve ABI compatibility.

---
 drivers/vfio/pci/vfio_pci_dmabuf.c | 26 ++++++++++++++++++++++++--
 include/linux/dma-buf.h            | 30 ++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h          |  9 +++++++--
 3 files changed, 61 insertions(+), 4 deletions(-)
drivers/vfio/pci/vfio_pci_dmabuf.c +2 -0
index 478beafc6ac3..c45cb3884b85 100644
@@ -17,6 +17,8 @@ struct vfio_pci_dma_buf {
 	struct phys_vec *phys_vec;
 	struct p2pdma_provider *provider;
 	u32 nr_ranges;
+	u16 steering_tag;
+	u8 ph;
 	u8 revoked : 1;
 };
 

drivers/vfio/pci/vfio_pci_dmabuf.c vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment, · line 70

Quoted 9 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +6 -0
@@ -60,6 +62,15 @@ vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment,
 				       priv->size, dir);
 }
 
+static int vfio_pci_dma_buf_get_tph(struct dma_buf *dmabuf, u16 *steering_tag,
+				    u8 *ph)
+{
+	struct vfio_pci_dma_buf *priv = dmabuf->priv;
+	*steering_tag = priv->steering_tag;
+	*ph = priv->ph;

If the dmabuf exporter don't provide st&ph, this ops should return error

Quoted 66 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +16 -2
+	return 0;
+}
+
 static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment,
 				   struct sg_table *sgt,
 				   enum dma_data_direction dir)
@@ -90,6 +101,7 @@ static const struct dma_buf_ops vfio_pci_dmabuf_ops = {
 	.unpin = vfio_pci_dma_buf_unpin,
 	.attach = vfio_pci_dma_buf_attach,
 	.map_dma_buf = vfio_pci_dma_buf_map,
+	.get_tph = vfio_pci_dma_buf_get_tph,
 	.unmap_dma_buf = vfio_pci_dma_buf_unmap,
 	.release = vfio_pci_dma_buf_release,
 };
@@ -228,7 +240,10 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
 	if (copy_from_user(&get_dma_buf, arg, sizeof(get_dma_buf)))
 		return -EFAULT;
 
-	if (!get_dma_buf.nr_ranges || get_dma_buf.flags)
+	if (!get_dma_buf.nr_ranges ||
+	    (get_dma_buf.flags & ~(VFIO_DMABUF_FL_TPH |
+				   VFIO_DMABUF_TPH_PH_MASK |
+				   VFIO_DMABUF_TPH_ST_MASK)))
 		return -EINVAL;
 
 	/*
@@ -285,7 +300,14 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
 		ret = PTR_ERR(priv->dmabuf);
 		goto err_dev_put;
 	}
-
+	if (get_dma_buf.flags & VFIO_DMABUF_FL_TPH) {
+		priv->steering_tag = (get_dma_buf.flags &
+				      VFIO_DMABUF_TPH_ST_MASK) >>
+				     VFIO_DMABUF_TPH_ST_SHIFT;
+		priv->ph = (get_dma_buf.flags &
+			    VFIO_DMABUF_TPH_PH_MASK) >>
+			   VFIO_DMABUF_TPH_PH_SHIFT;
+	}
 	/* dma_buf_put() now frees priv */
 	INIT_LIST_HEAD(&priv->dmabufs_elm);
 	down_write(&vdev->memory_lock);
include/linux/dma-buf.h +16 -0
index 133b9e637b55..26705c83ad80 100644
@@ -113,6 +113,36 @@ struct dma_buf_ops {
 	 */
 	void (*unpin)(struct dma_buf_attachment *attach);
 
+	/**
+	 * @get_tph:
+	 *
+	 * Get the TPH (TLP Processing Hints) for this DMA buffer.
+	 *
+	 * This callback allows DMA buffer exporters to provide TPH including
+	 * both the steering tag and the process hints (ph), which can be used
+	 * to optimize peer-to-peer (P2P) memory access. The TPH info is typically
+	 * used in scenarios where:
+	 * - A PCIe device (e.g., RDMA NIC) needs to access memory on another
+	 *   PCIe device (e.g., GPU),
+	 * - The system supports TPH and can use steering tags / ph to optimize
+	 *   cache placement and memory access patterns,
+	 * - The memory is exported via DMABUF for cross-device sharing.
+	 *
+	 * @dmabuf: [in] The DMA buffer for which to retrieve TPH

include/linux/dma-buf.h struct dma_buf_ops { · line 141

Quoted 10 lines
include/linux/dma-buf.h +10 -0
+	 * @steering_tag: [out] Pointer to store the 16-bit TPH steering tag value
+	 * @ph: [out] Pointer to store the 8-bit TPH processing-hint value
+	 *
+	 * Returns:
+	 * * 0 - Success, steering tag stored in @steering_tag
+	 * * -EOPNOTSUPP - TPH steering tags not supported for this buffer
+	 * * -EINVAL - Invalid parameters
+	 *
+	 * This callback is optional. If not implemented, the buffer does not
+	 * support TPH.

It seemed already impl...

Quoted 16 lines
include/linux/dma-buf.h +4 -0
+	 *
+	 */
+	int (*get_tph)(struct dma_buf *dmabuf, u16 *steering_tag, u8 *ph);
+
 	/**
 	 * @map_dma_buf:
 	 *
include/uapi/linux/vfio.h +0 -1
index bb7b89330d35..e2a8962641d2 100644
@@ -1505,8 +1505,13 @@ struct vfio_region_dma_range {
 struct vfio_device_feature_dma_buf {
 	__u32	region_index;
 	__u32	open_flags;
-	__u32   flags;

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1516

Quoted 10 lines
include/uapi/linux/vfio.h +7 -1
-	__u32   nr_ranges;
+	__u32	flags;
+#define VFIO_DMABUF_FL_TPH		(1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT	1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK	0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT	16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK		0xffff0000U
+	__u32	nr_ranges;
 	struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
 };

Another question:
1\ PCIE protocol define 8bit and 16bit ST
2\ In host-device ST impl, the ACPI will provide 8bit and 16bit ST, the choice of which
one to use depends on the minimum supported range of the device and the RP.
3\ So in this P2P scene, although exporter (e.g. GPU) support 16bit ST, but the consumer
(e.g. RDMA NIC) only support 8bit this may lead to mis-match

Quoted 6 lines
include/uapi/linux/vfio.h +0 -1
--

2.52.0

#20 lore raw

Re: [RFC v2 1/2] vfio: add callback to get tph info for dmabuf

Quoted 36 lines · fengchengwen
Quoted 2 lines

Hi Zhiping,

Quoted 31 lines · Zhiping Zhang

This patch adds a callback to get the tph info on DMA buffer exporters. The tph info includes both the steering tag and the process hint (ph).

The steering tag and ph are encoded in the flags field of vfio_device_feature_dma_buf instead of adding new fields to the uapi struct, to preserve ABI compatibility.

---
 drivers/vfio/pci/vfio_pci_dmabuf.c | 26 ++++++++++++++++++++++++--
 include/linux/dma-buf.h            | 30 ++++++++++++++++++++++++++++++
 include/uapi/linux/vfio.h          |  9 +++++++--
 3 files changed, 61 insertions(+), 4 deletions(-)
drivers/vfio/pci/vfio_pci_dmabuf.c +2 -0
index 478beafc6ac3..c45cb3884b85 100644
@@ -17,6 +17,8 @@ struct vfio_pci_dma_buf {
      struct phys_vec *phys_vec;
      struct p2pdma_provider *provider;
      u32 nr_ranges;
+     u16 steering_tag;
+     u8 ph;
      u8 revoked : 1;
 };
 
@@ -60,6 +62,15 @@ vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment,
                                     priv->size, dir);
 }
 
Quoted 8 lines

drivers/vfio/pci/vfio_pci_dmabuf.c vfio_pci_dma_buf_map(struct dma_buf_attachment *attachment, · line 70

Quoted 6 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +6 -0
+static int vfio_pci_dma_buf_get_tph(struct dma_buf *dmabuf, u16 *steering_tag,
+                                 u8 *ph)
+{
+     struct vfio_pci_dma_buf *priv = dmabuf->priv;
+     *steering_tag = priv->steering_tag;
+     *ph = priv->ph;

If the dmabuf exporter don't provide st&ph, this ops should return error

That is a good call, let me address that in the new revision.

Quoted 71 lines
Quoted 71 lines
drivers/vfio/pci/vfio_pci_dmabuf.c +16 -2
+     return 0;
+}
+
 static void vfio_pci_dma_buf_unmap(struct dma_buf_attachment *attachment,
                                 struct sg_table *sgt,
                                 enum dma_data_direction dir)
@@ -90,6 +101,7 @@ static const struct dma_buf_ops vfio_pci_dmabuf_ops = {
      .unpin = vfio_pci_dma_buf_unpin,
      .attach = vfio_pci_dma_buf_attach,
      .map_dma_buf = vfio_pci_dma_buf_map,
+     .get_tph = vfio_pci_dma_buf_get_tph,
      .unmap_dma_buf = vfio_pci_dma_buf_unmap,
      .release = vfio_pci_dma_buf_release,
 };
@@ -228,7 +240,10 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
      if (copy_from_user(&get_dma_buf, arg, sizeof(get_dma_buf)))
              return -EFAULT;
 
-     if (!get_dma_buf.nr_ranges || get_dma_buf.flags)
+     if (!get_dma_buf.nr_ranges ||
+         (get_dma_buf.flags & ~(VFIO_DMABUF_FL_TPH |
+                                VFIO_DMABUF_TPH_PH_MASK |
+                                VFIO_DMABUF_TPH_ST_MASK)))
              return -EINVAL;
 
      /*
@@ -285,7 +300,14 @@ int vfio_pci_core_feature_dma_buf(struct vfio_pci_core_device *vdev, u32 flags,
              ret = PTR_ERR(priv->dmabuf);
              goto err_dev_put;
      }
-
+     if (get_dma_buf.flags & VFIO_DMABUF_FL_TPH) {
+             priv->steering_tag = (get_dma_buf.flags &
+                                   VFIO_DMABUF_TPH_ST_MASK) >>
+                                  VFIO_DMABUF_TPH_ST_SHIFT;
+             priv->ph = (get_dma_buf.flags &
+                         VFIO_DMABUF_TPH_PH_MASK) >>
+                        VFIO_DMABUF_TPH_PH_SHIFT;
+     }
      /* dma_buf_put() now frees priv */
      INIT_LIST_HEAD(&priv->dmabufs_elm);
      down_write(&vdev->memory_lock);
include/linux/dma-buf.h +20 -0
index 133b9e637b55..26705c83ad80 100644
@@ -113,6 +113,36 @@ struct dma_buf_ops {
       */
      void (*unpin)(struct dma_buf_attachment *attach);
 
+     /**
+      * @get_tph:
+      *
+      * Get the TPH (TLP Processing Hints) for this DMA buffer.
+      *
+      * This callback allows DMA buffer exporters to provide TPH including
+      * both the steering tag and the process hints (ph), which can be used
+      * to optimize peer-to-peer (P2P) memory access. The TPH info is typically
+      * used in scenarios where:
+      * - A PCIe device (e.g., RDMA NIC) needs to access memory on another
+      *   PCIe device (e.g., GPU),
+      * - The system supports TPH and can use steering tags / ph to optimize
+      *   cache placement and memory access patterns,
+      * - The memory is exported via DMABUF for cross-device sharing.
+      *
+      * @dmabuf: [in] The DMA buffer for which to retrieve TPH
+      * @steering_tag: [out] Pointer to store the 16-bit TPH steering tag value
+      * @ph: [out] Pointer to store the 8-bit TPH processing-hint value
+      *
+      * Returns:
Quoted 8 lines

include/linux/dma-buf.h struct dma_buf_ops { · line 141

Quoted 6 lines
include/linux/dma-buf.h +6 -0
+      * * 0 - Success, steering tag stored in @steering_tag
+      * * -EOPNOTSUPP - TPH steering tags not supported for this buffer
+      * * -EINVAL - Invalid parameters
+      *
+      * This callback is optional. If not implemented, the buffer does not
+      * support TPH.

It seemed already impl...

Yup, it's supposed to be implemented.

Quoted 26 lines
Quoted 26 lines
include/linux/dma-buf.h +4 -0
+      *
+      */
+     int (*get_tph)(struct dma_buf *dmabuf, u16 *steering_tag, u8 *ph);
+
      /**
       * @map_dma_buf:
       *
include/uapi/linux/vfio.h +7 -2
index bb7b89330d35..e2a8962641d2 100644
@@ -1505,8 +1505,13 @@ struct vfio_region_dma_range {
 struct vfio_device_feature_dma_buf {
      __u32   region_index;
      __u32   open_flags;
-     __u32   flags;
-     __u32   nr_ranges;
+     __u32   flags;
+#define VFIO_DMABUF_FL_TPH           (1U << 0) /* TPH info is present */
+#define VFIO_DMABUF_TPH_PH_SHIFT     1         /* bits 1-2: PH (2-bit) */
+#define VFIO_DMABUF_TPH_PH_MASK      0x6U
+#define VFIO_DMABUF_TPH_ST_SHIFT     16        /* bits 16-31: steering tag */
+#define VFIO_DMABUF_TPH_ST_MASK              0xffff0000U
+     __u32   nr_ranges;
      struct vfio_region_dma_range dma_ranges[] __counted_by(nr_ranges);
Quoted 9 lines

include/uapi/linux/vfio.h struct vfio_region_dma_range { · line 1516

Quoted 1 line
include/uapi/linux/vfio.h +0 -0
 };

Another question:
1\ PCIE protocol define 8bit and 16bit ST
2\ In host-device ST impl, the ACPI will provide 8bit and 16bit ST, the choice of which
one to use depends on the minimum supported range of the device and the RP.
3\ So in this P2P scene, although exporter (e.g. GPU) support 16bit ST, but the consumer
(e.g. RDMA NIC) only support 8bit this may lead to mis-match

Hmm, let me check how we can address this mis-match issue. One option is to add an
additional parameter and fail the get_tph call when a mis-match is found.

Quoted 7 lines
Quoted 6 lines
include/uapi/linux/vfio.h +0 -1
--

2.52.0

#21 lore raw

RFC v2 2/2 RDMA/mlx5: get tph for p2p access when registering dmabuf mr

In reply to Zhiping Zhang

This patch adds support to retrieve tph info from dmabuf during mr registration for P2P memory access. A new helper get_tph_mr_dmabuf() queries the dmabuf exporter for tph (steering tag and processing hint) and converts the raw steering tag to an st_index via mlx5_st_alloc_index_by_tag(). The DMAH workflow for CPU still takes precedence in the process.

The new mlx5_st_alloc_index_by_tag() API is extracted from mlx5_st_alloc_index() to allow callers that already have a raw steering tag value (e.g., from a dmabuf) to allocate an st_index directly, without requiring a CPU ID and memory type lookup.

---
 drivers/infiniband/hw/mlx5/mr.c               | 34 +++++++++++++++++++
 .../net/ethernet/mellanox/mlx5/core/lib/st.c  | 23 +++++++++----
 include/linux/mlx5/driver.h                   |  7 ++++
 3 files changed, 57 insertions(+), 7 deletions(-)
drivers/infiniband/hw/mlx5/mr.c +34 -0
index 665323b90b64..041922ba3bff 100644
@@ -46,6 +46,8 @@
 #include "data_direct.h"
 #include "dmah.h"
 
+MODULE_IMPORT_NS("DMA_BUF");
+
 enum {
 	MAX_PENDING_REG_MR = 8,
 };
@@ -1622,6 +1624,36 @@ static struct dma_buf_attach_ops mlx5_ib_dmabuf_attach_ops = {
 	.move_notify = mlx5_ib_dmabuf_invalidate_cb,
 };
 
+static void get_tph_mr_dmabuf(struct mlx5_ib_dev *dev, int fd, u16 *st_index,
+			      u8 *ph)
+{
+	struct dma_buf *dmabuf;
+	u16 steering_tag;
+	int ret;
+
+	dmabuf = dma_buf_get(fd);
+	if (IS_ERR(dmabuf))
+		return;
+
+	if (!dmabuf->ops->get_tph)
+		goto end_dbuf_put;
+
+	ret = dmabuf->ops->get_tph(dmabuf, &steering_tag, ph);
+	if (ret) {
+		mlx5_ib_dbg(dev, "get_tph failed (%d)\n", ret);
+		goto end_dbuf_put;
+	}
+
+	ret = mlx5_st_alloc_index_by_tag(dev->mdev, steering_tag, st_index);
+	if (ret) {
+		*ph = MLX5_IB_NO_PH;
+		mlx5_ib_dbg(dev, "st_alloc_index_by_tag failed (%d)\n", ret);
+	}
+
+end_dbuf_put:
+	dma_buf_put(dmabuf);
+}
+
 static struct ib_mr *
 reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
 		   u64 offset, u64 length, u64 virt_addr,
@@ -1664,6 +1696,8 @@ reg_user_mr_dmabuf(struct ib_pd *pd, struct device *dma_device,
 		ph = dmah->ph;
 		if (dmah->valid_fields & BIT(IB_DMAH_CPU_ID_EXISTS))
 			st_index = mdmah->st_index;
+	} else {
+		get_tph_mr_dmabuf(dev, fd, &st_index, &ph);
 	}
 
 	mr = alloc_cacheable_mr(pd, &umem_dmabuf->umem, virt_addr,
drivers/net/ethernet/mellanox/mlx5/core/lib/st.c +16 -7
index 997be91f0a13..112c55ede731 100644
@@ -92,23 +92,18 @@ void mlx5_st_destroy(struct mlx5_core_dev *dev)
 	kfree(st);
 }
 
-int mlx5_st_alloc_index(struct mlx5_core_dev *dev, enum tph_mem_type mem_type,
-			unsigned int cpu_uid, u16 *st_index)
+int mlx5_st_alloc_index_by_tag(struct mlx5_core_dev *dev, u16 tag,
+			       u16 *st_index)
 {
 	struct mlx5_st_idx_data *idx_data;
 	struct mlx5_st *st = dev->st;
 	unsigned long index;
 	u32 xa_id;
-	u16 tag;
 	int ret;
 
 	if (!st)
 		return -EOPNOTSUPP;
 
-	ret = pcie_tph_get_cpu_st(dev->pdev, mem_type, cpu_uid, &tag);
-	if (ret)
-		return ret;
-
 	if (st->direct_mode) {
 		*st_index = tag;
 		return 0;
@@ -152,6 +147,20 @@ int mlx5_st_alloc_index(struct mlx5_core_dev *dev, enum tph_mem_type mem_type,
 	mutex_unlock(&st->lock);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(mlx5_st_alloc_index_by_tag);
+
+int mlx5_st_alloc_index(struct mlx5_core_dev *dev, enum tph_mem_type mem_type,
+			unsigned int cpu_uid, u16 *st_index)
+{
+	u16 tag;
+	int ret;
+
+	ret = pcie_tph_get_cpu_st(dev->pdev, mem_type, cpu_uid, &tag);
+	if (ret)
+		return ret;
+
+	return mlx5_st_alloc_index_by_tag(dev, tag, st_index);
+}
 EXPORT_SYMBOL_GPL(mlx5_st_alloc_index);
 
 int mlx5_st_dealloc_index(struct mlx5_core_dev *dev, u16 st_index)
include/linux/mlx5/driver.h +7 -0
index 04dcd09f7517..c1d2d603bd96 100644
@@ -1177,10 +1177,17 @@ int mlx5_dm_sw_icm_dealloc(struct mlx5_core_dev *dev, enum mlx5_sw_icm_type type
 			   u64 length, u16 uid, phys_addr_t addr, u32 obj_id);
 
 #ifdef CONFIG_PCIE_TPH
+int mlx5_st_alloc_index_by_tag(struct mlx5_core_dev *dev, u16 tag,
+			       u16 *st_index);
 int mlx5_st_alloc_index(struct mlx5_core_dev *dev, enum tph_mem_type mem_type,
 			unsigned int cpu_uid, u16 *st_index);
 int mlx5_st_dealloc_index(struct mlx5_core_dev *dev, u16 st_index);
 #else
+static inline int mlx5_st_alloc_index_by_tag(struct mlx5_core_dev *dev,
+					     u16 tag, u16 *st_index)
+{
+	return -EOPNOTSUPP;
+}
 static inline int mlx5_st_alloc_index(struct mlx5_core_dev *dev,
 				      enum tph_mem_type mem_type,
 				      unsigned int cpu_uid, u16 *st_index)

--
2.52.0

#22 lore raw

Re: [RFC v2 2/2] RDMA/mlx5: get tph for p2p access when registering dmabuf mr

kernel test robot noticed the following build warnings:

Hi Zhiping,

[This is a private test report for your RFC patch.] kernel test robot noticed the following build warnings:

[auto build test WARNING on awilliam-vfio/next]
[also build test WARNING on awilliam-vfio/for-linus linus/master v7.0-rc5] [cannot apply to rdma/for-next drm-misc/drm-misc-next next-20260327] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Zhiping-Zhang/vfio-add-callback-to-get-tph-info-for-dmabuf/20260327-214419
base:   https://github.com/awilliam/linux-vfio.git next
patch link:    https://lore.kernel.org/r/20260324234615.3731237-3-zhipingz%40meta.com
patch subject: [RFC v2 2/2] RDMA/mlx5: get tph for p2p access when registering dmabuf mr
config: sparc64-allmodconfig (https://download.01.org/0day-ci/archive/20260330/[email protected]/config)
compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project 054e11d1a17e5ba88bb1a8ef32fad3346e80b186)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260330/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/net/ethernet/mellanox/mlx5/core/lib/st.c:115:7: warning: variable 'ret' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
     115 |                 if (tag == idx_data->tag) {
         |                     ^~~~~~~~~~~~~~~~~~~~
   drivers/net/ethernet/mellanox/mlx5/core/lib/st.c:148:9: note: uninitialized use occurs here
     148 |         return ret;
         |                ^~~
   drivers/net/ethernet/mellanox/mlx5/core/lib/st.c:115:3: note: remove the 'if' if its condition is always false
     115 |                 if (tag == idx_data->tag) {
         |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~
     116 |                         refcount_inc(&idx_data->usecount);
         |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     117 |                         *st_index = index;
         |                         ~~~~~~~~~~~~~~~~~~
     118 |                         goto end;
         |                         ~~~~~~~~~
     119 |                 }
         |                 ~
   drivers/net/ethernet/mellanox/mlx5/core/lib/st.c:102:9: note: initialize the variable 'ret' to silence this warning
     102 |         int ret;
         |                ^
         |                 = 0

1 warning generated.

vim +115 drivers/net/ethernet/mellanox/mlx5/core/lib/st.c

888a7776f4fb04 Yishai Hadas   2025-07-17   94  
0af7a73615dcee Zhiping Zhang  2026-03-24   95  int mlx5_st_alloc_index_by_tag(struct mlx5_core_dev *dev, u16 tag,
0af7a73615dcee Zhiping Zhang  2026-03-24   96  			       u16 *st_index)
888a7776f4fb04 Yishai Hadas   2025-07-17   97  {
888a7776f4fb04 Yishai Hadas   2025-07-17   98  	struct mlx5_st_idx_data *idx_data;
888a7776f4fb04 Yishai Hadas   2025-07-17   99  	struct mlx5_st *st = dev->st;
888a7776f4fb04 Yishai Hadas   2025-07-17  100  	unsigned long index;
888a7776f4fb04 Yishai Hadas   2025-07-17  101  	u32 xa_id;
888a7776f4fb04 Yishai Hadas   2025-07-17  102  	int ret;
888a7776f4fb04 Yishai Hadas   2025-07-17  103  
888a7776f4fb04 Yishai Hadas   2025-07-17  104  	if (!st)
888a7776f4fb04 Yishai Hadas   2025-07-17  105  		return -EOPNOTSUPP;
888a7776f4fb04 Yishai Hadas   2025-07-17  106  
2d838c11e10e91 Yishai Hadas   2025-10-27  107  	if (st->direct_mode) {
2d838c11e10e91 Yishai Hadas   2025-10-27  108  		*st_index = tag;
2d838c11e10e91 Yishai Hadas   2025-10-27  109  		return 0;
2d838c11e10e91 Yishai Hadas   2025-10-27  110  	}
2d838c11e10e91 Yishai Hadas   2025-10-27  111  
888a7776f4fb04 Yishai Hadas   2025-07-17  112  	mutex_lock(&st->lock);
888a7776f4fb04 Yishai Hadas   2025-07-17  113  
888a7776f4fb04 Yishai Hadas   2025-07-17  114  	xa_for_each(&st->idx_xa, index, idx_data) {
888a7776f4fb04 Yishai Hadas   2025-07-17 @115  		if (tag == idx_data->tag) {
888a7776f4fb04 Yishai Hadas   2025-07-17  116  			refcount_inc(&idx_data->usecount);
888a7776f4fb04 Yishai Hadas   2025-07-17  117  			*st_index = index;
888a7776f4fb04 Yishai Hadas   2025-07-17  118  			goto end;
888a7776f4fb04 Yishai Hadas   2025-07-17  119  		}
888a7776f4fb04 Yishai Hadas   2025-07-17  120  	}
888a7776f4fb04 Yishai Hadas   2025-07-17  121  
bf4afc53b77aea Linus Torvalds 2026-02-21  122  	idx_data = kzalloc_obj(*idx_data);
888a7776f4fb04 Yishai Hadas   2025-07-17  123  	if (!idx_data) {
888a7776f4fb04 Yishai Hadas   2025-07-17  124  		ret = -ENOMEM;
888a7776f4fb04 Yishai Hadas   2025-07-17  125  		goto end;
888a7776f4fb04 Yishai Hadas   2025-07-17  126  	}
888a7776f4fb04 Yishai Hadas   2025-07-17  127  
888a7776f4fb04 Yishai Hadas   2025-07-17  128  	refcount_set(&idx_data->usecount, 1);
888a7776f4fb04 Yishai Hadas   2025-07-17  129  	idx_data->tag = tag;
888a7776f4fb04 Yishai Hadas   2025-07-17  130  
888a7776f4fb04 Yishai Hadas   2025-07-17  131  	ret = xa_alloc(&st->idx_xa, &xa_id, idx_data, st->index_limit, GFP_KERNEL);
888a7776f4fb04 Yishai Hadas   2025-07-17  132  	if (ret)
888a7776f4fb04 Yishai Hadas   2025-07-17  133  		goto clean_idx_data;
888a7776f4fb04 Yishai Hadas   2025-07-17  134  
888a7776f4fb04 Yishai Hadas   2025-07-17  135  	ret = pcie_tph_set_st_entry(dev->pdev, xa_id, tag);
888a7776f4fb04 Yishai Hadas   2025-07-17  136  	if (ret)
888a7776f4fb04 Yishai Hadas   2025-07-17  137  		goto clean_idx_xa;
888a7776f4fb04 Yishai Hadas   2025-07-17  138  
888a7776f4fb04 Yishai Hadas   2025-07-17  139  	*st_index = xa_id;
888a7776f4fb04 Yishai Hadas   2025-07-17  140  	goto end;
888a7776f4fb04 Yishai Hadas   2025-07-17  141  
888a7776f4fb04 Yishai Hadas   2025-07-17  142  clean_idx_xa:
888a7776f4fb04 Yishai Hadas   2025-07-17  143  	xa_erase(&st->idx_xa, xa_id);
888a7776f4fb04 Yishai Hadas   2025-07-17  144  clean_idx_data:
888a7776f4fb04 Yishai Hadas   2025-07-17  145  	kfree(idx_data);
888a7776f4fb04 Yishai Hadas   2025-07-17  146  end:
888a7776f4fb04 Yishai Hadas   2025-07-17  147  	mutex_unlock(&st->lock);
888a7776f4fb04 Yishai Hadas   2025-07-17  148  	return ret;
888a7776f4fb04 Yishai Hadas   2025-07-17  149  }
0af7a73615dcee Zhiping Zhang  2026-03-24  150  EXPORT_SYMBOL_GPL(mlx5_st_alloc_index_by_tag);
0af7a73615dcee Zhiping Zhang  2026-03-24  151  
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki