linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Mastro <amastro@fb.com>
To: Leon Romanovsky <leon@kernel.org>
Cc: "Bjorn Helgaas" <bhelgaas@google.com>,
	"Logan Gunthorpe" <logang@deltatee.com>,
	"Jens Axboe" <axboe@kernel.dk>,
	"Robin Murphy" <robin.murphy@arm.com>,
	"Joerg Roedel" <joro@8bytes.org>, "Will Deacon" <will@kernel.org>,
	"Marek Szyprowski" <m.szyprowski@samsung.com>,
	"Jason Gunthorpe" <jgg@ziepe.ca>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Jonathan Corbet" <corbet@lwn.net>,
	"Sumit Semwal" <sumit.semwal@linaro.org>,
	"Christian König" <christian.koenig@amd.com>,
	"Alex Williamson" <alex.williamson@redhat.com>,
	"Kees Cook" <kees@kernel.org>,
	"Gustavo A. R. Silva" <gustavoars@kernel.org>,
	"Ankit Agrawal" <ankita@nvidia.com>,
	"Yishai Hadas" <yishaih@nvidia.com>,
	"Shameer Kolothum" <skolothumtho@nvidia.com>,
	"Kevin Tian" <kevin.tian@intel.com>,
	"Krishnakant Jaju" <kjaju@nvidia.com>,
	"Matt Ochs" <mochs@nvidia.com>,
	linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-block@vger.kernel.org, iommu@lists.linux.dev,
	linux-mm@kvack.org, linux-doc@vger.kernel.org,
	linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org,
	linaro-mm-sig@lists.linaro.org, kvm@vger.kernel.org,
	linux-hardening@vger.kernel.org,
	"Vivek Kasireddy" <vivek.kasireddy@intel.com>
Subject: Re: [PATCH v6 00/11] vfio/pci: Allow MMIO regions to be exported through dma-buf
Date: Mon, 3 Nov 2025 12:07:12 -0800	[thread overview]
Message-ID: <aQkLcAxEn4qmF3c4@devgpu015.cco6.facebook.com> (raw)
In-Reply-To: <20251102-dmabuf-vfio-v6-0-d773cff0db9f@nvidia.com>

On Sun, Nov 02, 2025 at 10:00:48AM +0200, Leon Romanovsky wrote:
> Changelog:
> v6:
>  * Fixed wrong error check from pcim_p2pdma_init().
>  * Documented pcim_p2pdma_provider() function.
>  * Improved commit messages.
>  * Added VFIO DMA-BUF selftest.
>  * Added __counted_by(nr_ranges) annotation to struct vfio_device_feature_dma_buf.
>  * Fixed error unwind when dma_buf_fd() fails.
>  * Document latest changes to p2pmem.
>  * Removed EXPORT_SYMBOL_GPL from pci_p2pdma_map_type.
>  * Moved DMA mapping logic to DMA-BUF.
>  * Removed types patch to avoid dependencies between subsystems.
>  * Moved vfio_pci_dma_buf_move() in err_undo block.
>  * Added nvgrace patch.

Thanks Leon. Attaching a toy program which sanity tests the dma-buf export UAPI
by feeding the allocated dma-buf into an dma-buf importer (libibverbs + CX-7).
 
Tested-by: Alex Mastro <amastro@fb.com>

$ cc -Og -Wall -Wextra $(pkg-config --cflags --libs libibverbs) test_dmabuf.c -o test_dmabuf
$ ./test_dmabuf 0000:05:00.0 3 4 0 0x1000
opening 0000:05:00.0 via /dev/vfio/56
allocating dma_buf bar_idx=4, bar_offset=0x0, size=0x1000
allocated dma_buf fd=6
discovered 4 ibv devices: mlx5_0 mlx5_1 mlx5_2 mlx5_3
opened ibv device 3: mlx5_3
registered dma_buf
unregistered dma_buf
closed dma_buf fd

---
#include <fcntl.h>
#include <infiniband/verbs.h>
#include <libgen.h>
#include <linux/limits.h>
#include <linux/types.h>
#include <linux/vfio.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>

#define ensure(cond)                                                             \
	do {                                                                     \
		if (!(cond)) {                                                   \
			fprintf(stderr,                                          \
				"%s:%d Condition failed: '%s' (errno=%d: %s)\n", \
				__FILE__, __LINE__, #cond, errno,                \
				strerror(errno));                                \
			exit(EXIT_FAILURE);                                      \
		}                                                                \
	} while (0)

#ifndef VFIO_DEVICE_FEATURE_DMA_BUF
#define VFIO_DEVICE_FEATURE_DMA_BUF 11

struct vfio_region_dma_range {
	__u64 offset;
	__u64 length;
};

struct vfio_device_feature_dma_buf {
	__u32 region_index;
	__u32 open_flags;
	__u32 flags;
	__u32 nr_ranges;
	struct vfio_region_dma_range dma_ranges[];
};
#endif

static uint32_t group_for_bdf(const char *bdf)
{
	char path[PATH_MAX];
	char link[PATH_MAX];
	int ret;

	snprintf(path, sizeof(path), "/sys/bus/pci/devices/%s/iommu_group",
		 bdf);
	ret = readlink(path, link, sizeof(link));
	ensure(ret > 0);

	const char *filename = basename(link);
	ensure(filename);

	return strtoul(filename, NULL, 0);
}

int main(int argc, char **argv)
{
	int ret;

	if (argc != 6) {
		printf("usage: %s <vfio_bdf> <ibv_device_idx> <bar_idx> <bar_offset> <size>\n",
		       argv[0]);
		printf("example: %s 0000:05:00.0 3 2 0x20000 0x1000\n",
		       argv[0]);
		return 1;
	}

	const char *bdf = argv[1];
	uint32_t ibv_idx = strtoul(argv[2], NULL, 0);
	uint32_t bar_idx = strtoul(argv[3], NULL, 0);
	uint64_t bar_offs = strtoull(argv[4], NULL, 0);
	uint64_t dmabuf_len = strtoull(argv[5], NULL, 0);

	uint32_t group_num = group_for_bdf(bdf);
	char group_path[PATH_MAX];
	snprintf(group_path, sizeof(group_path), "/dev/vfio/%u", group_num);

	int container_fd = open("/dev/vfio/vfio", O_RDWR);
	ensure(container_fd >= 0);

	printf("opening %s via %s\n", bdf, group_path);
	int group_fd = open(group_path, O_RDWR);
	ensure(group_fd >= 0);

	ret = ioctl(group_fd, VFIO_GROUP_SET_CONTAINER, &container_fd);
	ensure(!ret);

	ret = ioctl(container_fd, VFIO_SET_IOMMU, VFIO_TYPE1v2_IOMMU);
	ensure(!ret);

	int device_fd = ioctl(group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf);
	ensure(device_fd >= 0);

	uint8_t buf[sizeof(struct vfio_device_feature) +
		    sizeof(struct vfio_device_feature_dma_buf) +
		    sizeof(struct vfio_region_dma_range)]
		__attribute__((aligned(32)));

	struct vfio_device_feature *ft = (struct vfio_device_feature *)buf;
	*ft = (struct vfio_device_feature){
		.argsz = sizeof(buf),
		.flags = VFIO_DEVICE_FEATURE_GET | VFIO_DEVICE_FEATURE_DMA_BUF,
	};

	struct vfio_device_feature_dma_buf *ft_dma_buf =
		(struct vfio_device_feature_dma_buf *)ft->data;
	*ft_dma_buf = (struct vfio_device_feature_dma_buf){
		.region_index = bar_idx,
		.open_flags = O_RDWR,
		.nr_ranges = 1,
	};

	ft_dma_buf->dma_ranges[0] = (struct vfio_region_dma_range){
		.length = dmabuf_len,
		.offset = bar_offs,
	};

	printf("allocating dma_buf bar_idx=%u, bar_offset=0x%lx, size=0x%lx\n",
	       bar_idx, bar_offs, dmabuf_len);
	int dmabuf_fd = ioctl(device_fd, VFIO_DEVICE_FEATURE, buf);
	ensure(dmabuf_fd >= 0);
	printf("allocated dma_buf fd=%d\n", dmabuf_fd);

	int num;
	struct ibv_device **devs = ibv_get_device_list(&num);
	ensure(devs && num > 0);
	printf("discovered %d ibv devices:", num);
	for (int i = 0; i < num; i++) {
		printf(" %s", ibv_get_device_name(devs[i]));
	}
	printf("\n");
	ensure(ibv_idx < (uint32_t)num);

	struct ibv_context *ctx = ibv_open_device(devs[ibv_idx]);
	ensure(ctx);
	printf("opened ibv device %d: %s\n", ibv_idx,
	       ibv_get_device_name(devs[ibv_idx]));

	struct ibv_pd *pd = ibv_alloc_pd(ctx);
	ensure(pd);

	uint64_t offset = 0;
	uint64_t iova = 0;
	int access = IBV_ACCESS_LOCAL_WRITE | IBV_ACCESS_REMOTE_READ |
		     IBV_ACCESS_REMOTE_WRITE;

	struct ibv_mr *mr = ibv_reg_dmabuf_mr(pd, offset, dmabuf_len, iova,
					      dmabuf_fd, access);
	ensure(mr);
	printf("registered dma_buf\n");

	ret = ibv_dereg_mr(mr);
	ensure(!ret);
	printf("unregistered dma_buf\n");

	ret = close(dmabuf_fd);
	ensure(!ret);
	printf("closed dma_buf fd\n");

	return 0;
}
---

  parent reply	other threads:[~2025-11-03 20:08 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-02  8:00 [PATCH v6 00/11] vfio/pci: Allow MMIO regions to be exported through dma-buf Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 01/11] PCI/P2PDMA: Separate the mmap() support from the core logic Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 02/11] PCI/P2PDMA: Simplify bus address mapping API Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 03/11] PCI/P2PDMA: Refactor to separate core P2P functionality from memory allocation Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 04/11] PCI/P2PDMA: Provide an access to pci_p2pdma_map_type() function Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 05/11] PCI/P2PDMA: Document DMABUF model Leon Romanovsky
2025-11-02 18:16   ` Randy Dunlap
2025-11-03  8:05     ` Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 06/11] dma-buf: provide phys_vec to scatter-gather mapping routine Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 07/11] vfio: Export vfio device get and put registration helpers Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 08/11] vfio/pci: Share the core device pointer while invoking feature functions Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 09/11] vfio/pci: Enable peer-to-peer DMA transactions by default Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 10/11] vfio/pci: Add dma-buf export support for MMIO regions Leon Romanovsky
2025-11-02 15:01   ` Alex Williamson
2025-11-02 15:12     ` Leon Romanovsky
2025-11-02 17:11       ` Alex Williamson
2025-11-02 17:16         ` Leon Romanovsky
2025-11-02  8:00 ` [PATCH v6 11/11] vfio/nvgrace: Support get_dmabuf_phys Leon Romanovsky
2025-11-03 20:07 ` Alex Mastro [this message]
2025-11-04  0:06   ` [PATCH v6 00/11] vfio/pci: Allow MMIO regions to be exported through dma-buf Jason Gunthorpe
2025-11-05  8:16   ` Leon Romanovsky
2025-11-04  7:00 ` Leon Romanovsky
2025-11-04 19:19 ` Nicolin Chen
2025-11-04 19:26   ` Jason Gunthorpe
2025-11-05  8:17   ` Leon Romanovsky
2025-12-17 17:01   ` Cédric Le Goater
2025-12-17 17:15     ` Shameer Kolothum

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=aQkLcAxEn4qmF3c4@devgpu015.cco6.facebook.com \
    --to=amastro@fb.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.williamson@redhat.com \
    --cc=ankita@nvidia.com \
    --cc=axboe@kernel.dk \
    --cc=bhelgaas@google.com \
    --cc=christian.koenig@amd.com \
    --cc=corbet@lwn.net \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=gustavoars@kernel.org \
    --cc=iommu@lists.linux.dev \
    --cc=jgg@ziepe.ca \
    --cc=joro@8bytes.org \
    --cc=kees@kernel.org \
    --cc=kevin.tian@intel.com \
    --cc=kjaju@nvidia.com \
    --cc=kvm@vger.kernel.org \
    --cc=leon@kernel.org \
    --cc=linaro-mm-sig@lists.linaro.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-media@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=logang@deltatee.com \
    --cc=m.szyprowski@samsung.com \
    --cc=mochs@nvidia.com \
    --cc=robin.murphy@arm.com \
    --cc=skolothumtho@nvidia.com \
    --cc=sumit.semwal@linaro.org \
    --cc=vivek.kasireddy@intel.com \
    --cc=will@kernel.org \
    --cc=yishaih@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).