From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f73.google.com (mail-ot1-f73.google.com [209.85.210.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 29F563AA1B2 for ; Tue, 24 Feb 2026 18:25:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957545; cv=none; b=PpS/9I37v57TeAcz8vJ5l35psUv/J7goMPQ666F3AQzHPDF0vL44RU42RENuvgAABezNrnrvLxlkbDxiGM6VcGHnT6EOlHJ7s+J/cn/qcqsaGBH8hQhVPATTJHSf8cJREe2/ZgJHqI7FNxVk86QQWIIdZJUTFOpm+iXOxe9WoSM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771957545; c=relaxed/simple; bh=vGdV3ktht32WblZs3LsiIfE3xzMkY/XL4LUK0CaWYe4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=kw3/3BEBDDU3DaE7AWRY8EonWaWLj81tbrjRHxz7BZRLP1FBahjWms5zBRFmM4aGFP3JYjXS6NVzw//X50r7DJCgvsPhNrNzfLeIQJZqFEzuGTAp+xZWotgP4l4QNEoAibemVPJY1aDr4B9PvNtY2fNFX0iKSV3mO5JL8cIl9T8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--rananta.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=h5GhGTp9; arc=none smtp.client-ip=209.85.210.73 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--rananta.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="h5GhGTp9" Received: by mail-ot1-f73.google.com with SMTP id 46e09a7af769-7d4d4db1523so25879182a34.1 for ; Tue, 24 Feb 2026 10:25:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771957542; x=1772562342; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=W5wQFF+d0vZbX1lJAkgEnS9lDKccfjOQiCEdtZxbCbg=; b=h5GhGTp9g3+N4nqB9Xm1HO41HlAK+7iEhZSUF8FwcGZbbpF9/sbmECw3W70xvHLS76 o+Ux60Lu0vq809XSQBQQsm9uRhN/hhHX3DUEgtw2aKd8FAg5ukJNxVxNfSYByNP1xvRh Ut0yl3FV68pn0A9VfaVewGJnzwtLlYQABnLXeVB5EICGdJ6KOo7zX8qpOrfkI/kdyEu/ 2jk39nK+PLi0c4KQ5letBI4XTNv8R1FHJ4FashDaZgiXYa0LA6elnE9kUD3oF7R82CoP e9u4gw34YvqvkfRlahMj8m56Ztehdr71qkxAXg/tR1nd0L22KVWIu1J4rJpY71jYu5sR L/oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771957542; x=1772562342; h=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; bh=W5wQFF+d0vZbX1lJAkgEnS9lDKccfjOQiCEdtZxbCbg=; b=SVteZx/izaGwOoo/E5UFKpqg38mKZG/N4ec3V1y3BcDpGlqW5DnD/2a8n9U4DjVzko scISyP8L1cWV7KWg36KX9KCsxd9Gk6eDbKsS04SmPnD0JxhUcmnP8ah56cf9YyW9yANn XM8WoRUCltFw6G8ydGioRFEVLAb6gjyhpeUKs4Ufe5ctDA1aSUNVjq8lrLnpBDo1xCNJ 1Rw4sDI+rsV9gfgp4qIiS7XCvz6rjyXj9UY/OBEcM1rmauMuF38uwnmXONB9hMhKvatS EPf8A1PsnEyVhr8MnL8hXzrBY4J4qbtMe6F9SmaxZyWWO0QRn1keXswLIDyXzwjUTnou Zmvw== X-Forwarded-Encrypted: i=1; AJvYcCXLv80YEpVBNX1UZ+rXGGiM5RXwjTBeHUq9NBuwz8D81M7LKY1lnJW9HvOqiTCiuJn01J49CPLDptzzDlY=@vger.kernel.org X-Gm-Message-State: AOJu0YyONvQbcyuBwmgkhbA6U9prWYp0Dd1lZy8XkTAYZojY9t0yKKKI i78Llz2Fo/vY+u+NZGgYVUDijuoqxr24J8yY4cNbI5D7Y6Uuk1xdA9eKbdcTPPt0LXTaAP/BOz9 aXkkkbzqojQ== X-Received: from ilst5.prod.google.com ([2002:a05:6e02:605:b0:4b4:27a0:c01e]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:d0e:b0:66a:9d34:6cc0 with SMTP id 006d021491bc7-679c4280f47mr7318572eaf.27.1771957541683; Tue, 24 Feb 2026 10:25:41 -0800 (PST) Date: Tue, 24 Feb 2026 18:25:29 +0000 In-Reply-To: <20260224182532.3914470-1-rananta@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260224182532.3914470-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.414.gf7e9f6c205-goog Message-ID: <20260224182532.3914470-6-rananta@google.com> Subject: [PATCH v4 5/8] vfio: selftests: Expose more vfio_pci_device functions From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Type: text/plain; charset="UTF-8" Refactor and make the functions called under device initialization public. A later patch adds a test that calls these functions to validate the UAPI of SR-IOV devices. Opportunistically, to test the success and failure cases of the UAPI, split the functions dealing with VFIO_GROUP_GET_DEVICE_FD and VFIO_DEVICE_BIND_IOMMUFD into a core function and another one that asserts the ioctl. The former will be used for testing the SR-IOV UAPI, hence only export these. Signed-off-by: Raghavendra Rao Ananta --- .../lib/include/libvfio/vfio_pci_device.h | 7 +++ .../selftests/vfio/lib/vfio_pci_device.c | 47 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) 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 2858885a89bbb..898de032fed5a 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 @@ -122,4 +122,11 @@ static inline bool vfio_pci_device_match(struct vfio_pci_device *device, const char *vfio_pci_get_cdev_path(const char *bdf); +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf); +void __vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token); +void vfio_container_set_iommu(struct vfio_pci_device *device); +void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf); +int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/selftests/vfio/lib/vfio_pci_device.c index 4c79557cc4e09..142b13a77ab83 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -180,7 +180,7 @@ void vfio_pci_device_reset(struct vfio_pci_device *device) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } -static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) { struct vfio_group_status group_status = { .argsz = sizeof(group_status), @@ -200,8 +200,8 @@ static void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->container_fd); } -static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void __vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { char arg[64]; @@ -216,18 +216,21 @@ static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, snprintf_assert(arg, ARRAY_SIZE(arg), "%s", bdf); device->fd = ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); +} + +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + __vfio_pci_group_get_device_fd(device, bdf, vf_token); VFIO_ASSERT_GE(device->fd, 0); } -static void vfio_pci_container_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void vfio_container_set_iommu(struct vfio_pci_device *device) { struct iommu *iommu = device->iommu; unsigned long iommu_type = iommu->mode->iommu_type; int ret; - vfio_pci_group_setup(device, bdf); - ret = ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); @@ -237,7 +240,13 @@ static void vfio_pci_container_setup(struct vfio_pci_device *device, * because the IOMMU type is already set. */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); +} +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_group_setup(device, bdf); + vfio_container_set_iommu(device); vfio_pci_group_get_device_fd(device, bdf, vf_token); } @@ -299,8 +308,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) return cdev_path; } -static void vfio_device_bind_iommufd(int device_fd, int iommufd, - const char *vf_token) +int __vfio_device_bind_iommufd(int device_fd, int iommufd, const char *vf_token) { struct vfio_device_bind_iommufd args = { .argsz = sizeof(args), @@ -314,7 +322,18 @@ static void vfio_device_bind_iommufd(int device_fd, int iommufd, args.token_uuid_ptr = (u64)token_uuid; } - ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); + if (ioctl(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args)) + return -errno; + + return 0; +} + +static void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) +{ + int ret = __vfio_device_bind_iommufd(device_fd, iommufd, vf_token); + + VFIO_ASSERT_EQ(ret, 0, "Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n"); } static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id) @@ -327,15 +346,19 @@ static void vfio_device_attach_iommufd_pt(int device_fd, u32 pt_id) ioctl_assert(device_fd, VFIO_DEVICE_ATTACH_IOMMUFD_PT, &args); } -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) +void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf) { const char *cdev_path = vfio_pci_get_cdev_path(bdf); device->fd = open(cdev_path, O_RDWR); VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); +} +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + vfio_pci_cdev_open(device, bdf); vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } -- 2.53.0.414.gf7e9f6c205-goog