From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794425062; b=YhiQLB2J5kdyLZ/w2IFLTkO6qQ2tvjeccS72MURkCIYsLeXm2zg32WQo9UCZIbJy3FNp /sa0l5AdqbfiYVIA3RZWscGYxq5qpQIvBw31BUNaJvxT/nJiUY8lQoIE3HLS/6y2S1CM7 G23kl8XQK2+vvPLJsUTsh8XPDYiVzRVMT+vCVeo/g5XTmk64huz+i7Q1Aqozd4wbttqRx h2kIeyLKnDCxbr8NXxPhFkokRbhiw6fqBXbJ4sgkn+B+KENP1P3iiM7AxKF0gv5QwzTjT IJkHy/00kl178WNsR9dKrOS5KpEHMRtxIBWnnI1qcgdxLZuH1oHnmSgds39osOTrW9w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794425062; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=9sL5fqWbHi6g8R+z+W0YnlVdwW61K08vu/pGLyPlBpM=; b=2REijjV2lLHl+CrSMhsK7Q7glu255jc059P5KnSwOoWpf4XJa96kU33Jao3dLt6JGrMh hbNps2MM3NxXzxjMVPnVMkdXQTtA8yPjwQrRR8h+uRYcvLfQriwajf8Om2kKUElFNzwV4 rkjo/1dZCLFEJ3qlVWHmM3qWdzhsuiYcFkEhufVd87M7CFuGEsyafWX6bEFqPYBDc0ZWG tpIGRP9d6HeW0f6NLQIzgXOjsQgJGfoLmdCSYtCZMs/nSVUK8FTlafLZoRlavGRCfN3zC LlwuLa8V2Iu2PVu5tXim+6rwtZyjv47ISEPK2oJyM8eSSVHT02w5V3uEmAxJgvZC+XA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=X7bMVouG; arc=none smtp.remote-ip=209.85.160.69 Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E44DE60A263F for ; Wed, 11 Nov 2026 19:24:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E44DE60A263F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=X7bMVouG Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-45e83fd0663so3378528fac.1 for ; Wed, 11 Nov 2026 11:24:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794425061; x=1795029861; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=9sL5fqWbHi6g8R+z+W0YnlVdwW61K08vu/pGLyPlBpM=; b=X7bMVouGQSDR4ILhS01f8XJncA67ZnJnP5PwQqrPRuLRWu8CpHJg2A66lhHhJRGWt7 juxfLX3u/ya4/WT1MkZIqPcZXkIvK/wD3UHwQpwqKXpPpwkfYfz2pWdNX6x0OIMzZMFG UU5Y0btLBvaScL+pUtJEZ1M/zc8nK7XJP8Tt5uEp0mqUE53ROuo3rfptk/3SNrtS3Ojj l5GMuTw1aEIfHy0r7o5k425xLXRBBIj2//M1u30sHcQbZCE20HmwNoUWPHoawj0W8qn1 Tg5iH8+vQaKd6qabgyFiZCpQiTBEbkBEVwwyVy0hO2CB2MDDUXf5XfekWhnsTWD+V6Qk 125Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794425061; x=1795029861; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=9sL5fqWbHi6g8R+z+W0YnlVdwW61K08vu/pGLyPlBpM=; b=lhAPcy7wZN82mc5fTrvxHCmfui0YIsGL4nUdU2aMfUpgzP0R+jAAwI6fWBftaKCw7e kDKGuMABSPlkALhgJBjHBWh0hcGwf0nMt4Y7kMuHysSH0vlYr3Ha6qRyI6B8WuXOyePC 8N/B3+m+vbB2iY2R5gdyAek6etkKbX5cAdpH0FkbMy+Hbjv1+k6Mi/Nm9e7NAQNHGSHH br0wgG5tKF0jz2NEJj+uVxsLqjgcfDFI5w+em92MmNdEWaWO+tzQWDzvupdVL32mGG+w q40grPT99Q4NoWJfE2tlfnSuQmXEbBRsPt8h1/FLgoaqKVhtK6HekJlsuJwa7w+usDua 2ckA== X-Forwarded-Encrypted: i=1; AL+hErHwIyefIPib5F+YhqTGLCqq9QSJjmMbIPeYIOksvR4VvgwGco72ApEem61gM+xjHX9EQGpmPQ==@lists.linux.dev X-Gm-Message-State: APuU4Z+T6Z+nv0xNA5ynguiG0KPxy0dP664lNRKhjL7rxMhQ/vzu24Jz Dugcuqh+WO1tnZDMJv945g0BwZvrv1ObS3H3romdeqZ40X2UI/gHu1wxzqKd2nZlz3l3JGUpS+V E4OtuxrOKk9DZfA== X-Received: from oabus13.prod.google.com ([2002:a05:6870:df8d:b0:4ad:8f23:4d1b]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6870:1644:b0:49e:f7c:fa52 with SMTP id 586e51a60fabf-4ae46ba7423mr2997611fac.36.1794425060739; Wed, 11 Nov 2026 11:24:20 -0800 (PST) Date: Wed, 11 Nov 2026 19:24:08 +0000 In-Reply-To: <20261111192411.4133780-1-dmatlack@google.com> X-Mailing-List: iommu@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261111192411.4133780-1-dmatlack@google.com> X-Mailer: git-send-email 2.56.0.567.gdb80ae8cd5-goog Message-ID: <20261111192411.4133780-5-dmatlack@google.com> Subject: [RFC PATCH 4/7] vfio: selftests: Add vfio_pci_dmabuf_create() helper From: David Matlack To: dri-devel@lists.freedesktop.org, iommu@lists.linux.dev, kvm@vger.kernel.org, linaro-mm-sig@lists.linaro.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-media@vger.kernel.org, linux-pci@vger.kernel.org Cc: Alex Mastro , Alex Williamson , Bjorn Helgaas , "=?UTF-8?q?Christian=20K=C3=B6nig?=" , David Matlack , Jason Gunthorpe , Josh Hilke , Kevin Tian , Leon Romanovsky , Logan Gunthorpe , Matt Evans , Matt Evans , Mina Almasry , Samiullah Khawaja , Shuah Khan , Sumit Semwal , Vipin Sharma Content-Type: text/plain; charset="UTF-8" Add vfio_pci_dmabuf_create() and __vfio_pci_dmabuf_create() to export a VFIO PCI region range as a dma-buf fd via VFIO_DEVICE_FEATURE_DMA_BUF, and preserve -errno on failure in vfio_device_feature_ioctl(). Native iommufd maps device MMIO BARs for peer-to-peer DMA via dma-buf file descriptors rather than VM_PFNMAP virtual addresses. Signed-off-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 5 +++ .../selftests/vfio/lib/vfio_pci_device.c | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 19d458adba1a..fd130f680a2d 100644 --- a/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h +++ b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h @@ -172,6 +172,11 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device, bool vfio_pci_find_mem_bar(struct vfio_pci_device *device, u64 min_size, struct vfio_pci_mem_bar *mem_bar); +int __vfio_pci_dmabuf_create(struct vfio_pci_device *device, u32 region_index, + u64 offset, u64 length, u32 open_flags); +int vfio_pci_dmabuf_create(struct vfio_pci_device *device, u32 region_index, + u64 offset, u64 length, u32 open_flags); + const char *vfio_pci_get_cdev_path(const char *bdf); void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf); diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c index 9840463594ab..eca6eb13d7f0 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -153,11 +153,44 @@ static int vfio_device_feature_ioctl(int fd, u32 flags, void *data, feature->flags = flags; ret = ioctl(fd, VFIO_DEVICE_FEATURE, feature); + if (ret < 0) + ret = -errno; free(feature); return ret; } +int __vfio_pci_dmabuf_create(struct vfio_pci_device *device, u32 region_index, + u64 offset, u64 length, u32 open_flags) +{ + u8 buf[sizeof(struct vfio_device_feature_dma_buf) + + sizeof(struct vfio_region_dma_range)] = {}; + struct vfio_device_feature_dma_buf *dma_buf = (void *)buf; + + dma_buf->region_index = region_index; + dma_buf->open_flags = open_flags; + dma_buf->nr_ranges = 1; + dma_buf->dma_ranges[0].offset = offset; + dma_buf->dma_ranges[0].length = length; + + return vfio_device_feature_ioctl(device->fd, + VFIO_DEVICE_FEATURE_GET | + VFIO_DEVICE_FEATURE_DMA_BUF, + buf, sizeof(buf)); +} + +int vfio_pci_dmabuf_create(struct vfio_pci_device *device, u32 region_index, + u64 offset, u64 length, u32 open_flags) +{ + int fd; + + fd = __vfio_pci_dmabuf_create(device, region_index, offset, length, + open_flags); + VFIO_ASSERT_GE(fd, 0, "VFIO_DEVICE_FEATURE_DMA_BUF failed: %d\n", fd); + + return fd; +} + static void vfio_device_feature_set(int fd, u16 feature, void *data, size_t data_size) { u32 flags = VFIO_DEVICE_FEATURE_SET | feature; -- 2.56.0.567.gdb80ae8cd5-goog