From nobody Sun Feb 8 15:58:04 2026 Received: from mail-oa1-f73.google.com (mail-oa1-f73.google.com [209.85.160.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 9F07425A2CF for ; Wed, 4 Feb 2026 01:01:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166870; cv=none; b=SQOdm/WRmZOb7Njzlyd1KkI9R0jXN6BZLfkikuf9FPatymfkeq7lj7pZ1CX/gimSZ8KVUQZ3Tf3QefRukmZsH0F2HBYU0P7kFihPMzcowj1Bzhv+BSklrjnd0TuzwaFqHi5oit+CGNV5JJR/miJw5y5/qYVFeFuajVcDDvWnI2s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770166870; c=relaxed/simple; bh=+vdQEGyCwSDq42HprLYyZAxVP9gA4Sg0HO4ld5kKHfs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PC297I2Fvv2LIy2Px6uB2v9Vn5XVGRgoOTdTmfwUQA/0TIGko/tZskMDnnwPNLuozuBTsY7BIKN8hppewthD+JwtbJnqCBR0SUidRxiBfLWkzIJoLCgVuyiI4Z8UOj5+lQOJlCp3KmDQPnQv6DZRHgni9G4NiwP+Uhrlw2ET6KU= 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=2CSZt8Ch; arc=none smtp.client-ip=209.85.160.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="2CSZt8Ch" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-40418f60513so26869797fac.2 for ; Tue, 03 Feb 2026 17:01:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1770166867; x=1770771667; 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=0udRq4j88lomrvASezCNuuV2zCz1GvvKR3IJOID3phM=; b=2CSZt8ChnE/IHXY0bUrS8EYfOV6q4VE7+DjNT4CB889byXiEud5g4MjRrqPS1leYaL P3p/tIgeUYPI945pm55E6YRHKI2K19wKp4T5cHJaIt4RcBHH69rG9w/RdnYb+x4FyeVq e6deu44qT1l9vBMoDT/MoEyjFVpZAOwtdp+vkRLTbWF4SKdveTPqD098ceLwu1XNQ8Ul qgV0/C4DG88mgFCra4G4L9C6HtqjI0jX2rAk7UA/VXA1MMwyFJpb9cf1C3kUcBzAfeDZ C9Uty/xKkHiSF54uo8H/BwPR1MlkpvxG4CM29vqlOhOzSG5+qKpriGNp/cIHrkeJroCw IR6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770166867; x=1770771667; 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=0udRq4j88lomrvASezCNuuV2zCz1GvvKR3IJOID3phM=; b=nY5/EfMZmV6llQ9aF+p5s1VH7db+rcwVuNBZ9QnETadVX9lZTeadhv/6Y1NSNca56L K+CtntrmsdKIA7Ox3gRx4HP752gCXWVeIR8B9sJw5ZkmEJ3ckyK3+alw31FdUjIaF3PL 5rHsUSvrL4plNq4ERinPRyQnbzHE5sWOuUZ7MlKpme5DK12A7aAjxS9DQEe5kUyAwGx/ 0p/oMAxjOIolTrWaNiRgD/pmy5M1FFIgmBvjJgBNr0tnUbYxF2/GF7qFYdI46YaStHJy 6/wRxtY2sxGAsNWapK+OlzyF7UpDyu5WNtPs6dWwFhBzRz5r2QBTQoyYBayKT9ZNE6FP kegw== X-Forwarded-Encrypted: i=1; AJvYcCW2phbBKPFq+k8kkG6aI2gqBfmQsdcRWXPWs9vVj7E628Des32tO8krHwHSF72WimZBty6IGQegUMI//EQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yz2X2Ve/xVjT/hC/qq0gSLBcJuKCknrrCLXV9+Z5UXUD7/4lbqp 1gme3Dq2XT+o1YmLdGRQqAbJEXnmFRnC+1B6d5EdmjIlOQ/YwR/b6OsS7ITssiIyuO2H120hUJc eXGlgz5N5yg== X-Received: from iock2.prod.google.com ([2002:a6b:6f02:0:b0:957:7451:a858]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:602:b0:659:9a49:8fd9 with SMTP id 006d021491bc7-66a23df34aemr676968eaf.82.1770166867191; Tue, 03 Feb 2026 17:01:07 -0800 (PST) Date: Wed, 4 Feb 2026 01:00:54 +0000 In-Reply-To: <20260204010057.1079647-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: <20260204010057.1079647-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.rc2.204.g2597b5adb4-goog Message-ID: <20260204010057.1079647-6-rananta@google.com> Subject: [PATCH v3 5/8] vfio: selftests: Expose more vfio_pci_device functions From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: Vipin Sharma , Josh Hilke , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Raghavendra Rao Ananta Content-Transfer-Encoding: quoted-printable 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_devi= ce.h b/tools/testing/selftests/vfio/lib/include/libvfio/vfio_pci_device.h index 2858885a89bb..898de032fed5 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_p= ci_device *device, =20 const char *vfio_pci_get_cdev_path(const char *bdf); =20 +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/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 4c79557cc4e0..142b13a77ab8 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 *devi= ce) ioctl_assert(device->fd, VFIO_DEVICE_RESET, NULL); } =20 -static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) +void vfio_pci_group_setup(struct vfio_pci_device *device, const char *bdf) { struct vfio_group_status group_status =3D { .argsz =3D 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); } =20 -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]; =20 @@ -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); =20 device->fd =3D 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); } =20 -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 =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; int ret; =20 - vfio_pci_group_setup(device, bdf); - ret =3D ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); =20 @@ -237,7 +240,13 @@ static void vfio_pci_container_setup(struct vfio_pci_d= evice *device, * because the IOMMU type is already set. */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); +} =20 +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); } =20 @@ -299,8 +308,7 @@ const char *vfio_pci_get_cdev_path(const char *bdf) return cdev_path; } =20 -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 =3D { .argsz =3D sizeof(args), @@ -314,7 +322,18 @@ static void vfio_device_bind_iommufd(int device_fd, in= t iommufd, args.token_uuid_ptr =3D (u64)token_uuid; } =20 - 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 =3D __vfio_device_bind_iommufd(device_fd, iommufd, vf_token); + + VFIO_ASSERT_EQ(ret, 0, "Failed VFIO_DEVICE_BIND_IOMMUFD ioctl\n"); } =20 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); } =20 -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 =3D vfio_pci_get_cdev_path(bdf); =20 device->fd =3D open(cdev_path, O_RDWR); VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); +} =20 +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); } --=20 2.53.0.rc2.204.g2597b5adb4-goog