From nobody Thu Apr 2 06:31:34 2026 Received: from mail-oo1-f73.google.com (mail-oo1-f73.google.com [209.85.161.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 E85D5331220 for ; Tue, 3 Mar 2026 19:38:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566716; cv=none; b=LSvl2v27bT5K7ewELLpC0MGAMmjZbGZ1Oh7rUp+vyREEiUGe3iHRofXmsPtad5CHdVXpG1iiKRtmr9jW1TU8oZb0JlUD5xfeTEOf6curaAS1s0CSt06O3ZCLUOrjxq2xpz2LWlWaUP6kwvplpdAXhA/b0XsZOtV30L5A59Y/E3w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772566716; c=relaxed/simple; bh=gEp5dckqG5cP6r+qRr0XG/bWUuqISDwDcz3OMbSqQOA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=h2ZPsr2WFMZXxzQO+n8VMec/dDlG8yuZlkc7I8uHJ8cobFEJaB3Y00NGWGOpBKm6WjsZKeoEkvV3E8YCARQ3SXcEhrZJpgo4FoGpW+1Mk8d0yNQZv3+0tMjtZeiURmjEHBhpBD41OU67gxDR6V5LcL33C9IOeCI+nRJjX7wAIa4= 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=rpBT5V0Z; arc=none smtp.client-ip=209.85.161.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="rpBT5V0Z" Received: by mail-oo1-f73.google.com with SMTP id 006d021491bc7-67995e1ecacso110817997eaf.0 for ; Tue, 03 Mar 2026 11:38:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1772566712; x=1773171512; 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=0LK6yP7R5CEoceLoxZTwdESH49A+lkP7zsPWLGIA5lg=; b=rpBT5V0Z1z/OcdY0pbGbqBVmaueYLpV7VKZ7yrGYIbdzsbelIKqU1Nl4cVFq/1znXQ XpA1AaDCUUucVUQh8ZxPlAPehe2mE08pRth54L76H2GRLoDNm3tLKzI2sI7dUC5OfhiF teQXDaEHWIfS5PJb23BW2aHHCf3BRuvHCY4+A71ZbtFnSrqImQIy+GvghsM2D3jp5Yoo fybNMkgRN9vBCKF19OFksabTHi+I6W3FlX5s3OBmz+qEAYsRLigLUX1Y7l7yGKxJQVE8 THz6S/b9eg3HWk9X0zK6YYTkrATdMQcJJvMOqvcOkHX+HrBuTqRE8mQYb+TSr8oFrIGW GWOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772566712; x=1773171512; 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=0LK6yP7R5CEoceLoxZTwdESH49A+lkP7zsPWLGIA5lg=; b=FdzKnwQQj9xyXTW6IGlg4N3/cybgTzncgbPkZm6RdB1y7HuNjNSJlwUh8tZB9+ifuu hhDYzZaYCeqS40zL4GHq+6s0oCvO8zgEqsNR6kx44YZ7joX2DbRhXZ3VqFuzvGyyfjdW d0pIIerUyfh2deU3hVVKq4UHYQHb7HYFwpVKggFslvK3Gh2Dw69XP8BsZs/ezSPnqXCR 6HSVZQyPo6GHCebdPljpv/UQJLDDVU1INRmTeFH98gzuoqJEs7gxnNPoVxNVD4xLs88P iSrjMQMkOxGgVrzQX0/wuECH9aMo8WCvhHjaoiPa1L3kTXjUVReRgJxE8f78Pgi4ZoD4 LJAQ== X-Forwarded-Encrypted: i=1; AJvYcCUwuK1LIHx9afGPqVxnltjtYeTeZRVwntg8GV3k23rxlU2fuvhl4Qwm7IzX7uSAVdcPaeZkBxzdNThV8As=@vger.kernel.org X-Gm-Message-State: AOJu0YxjAZHrttS9tMkIkiC1nCHUZ/UJeOL0MDodgwMntReddN/4IyUa Df4tcEd3NPOKOeqBfYi3V4Z+cJTUPvVOUR4LTCJLov/eCULxbt4bzl1+gV6zOmVYiioA1fYJ1ye VIApgPnu+0A== X-Received: from jaad15-n1.prod.google.com ([2002:a05:6638:c78f:10b0:5d1:5fa2:71ef]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:2214:b0:67a:2256:e70b with SMTP id 006d021491bc7-67a2256ed3amr763619eaf.41.1772566711689; Tue, 03 Mar 2026 11:38:31 -0800 (PST) Date: Tue, 3 Mar 2026 19:38:18 +0000 In-Reply-To: <20260303193822.2526335-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: <20260303193822.2526335-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.473.g4a7958ca14-goog Message-ID: <20260303193822.2526335-5-rananta@google.com> Subject: [PATCH v6 4/8] vfio: selftests: Extend container/iommufd setup for passing vf_token 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A UUID is normally set as a vf_token to correspond the VFs with the PFs, if they are both bound by the vfio-pci driver. This is true for iommufd-based approach and container-based approach. The token can be set either during device creation (VFIO_GROUP_GET_DEVICE_FD) in container-based approach or during iommu bind (VFIO_DEVICE_BIND_IOMMUFD) in the iommu-fd case. Hence extend the functions, vfio_pci_iommufd_setup() and vfio_pci_container_setup(), to accept vf_token as an (optional) argument and handle the necessary setup. No functional changes are expected. Signed-off-by: Raghavendra Rao Ananta Reviewed-by: David Matlack --- tools/testing/selftests/vfio/Makefile | 2 + .../selftests/vfio/lib/vfio_pci_device.c | 46 +++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/tools/testing/selftests/vfio/Makefile b/tools/testing/selftest= s/vfio/Makefile index 6a9ac6dd32cb6..f27ed18070f14 100644 --- a/tools/testing/selftests/vfio/Makefile +++ b/tools/testing/selftests/vfio/Makefile @@ -28,6 +28,8 @@ CFLAGS +=3D $(EXTRA_CFLAGS) =20 LDFLAGS +=3D -pthread =20 +LDLIBS +=3D -luuid + $(TEST_GEN_PROGS): %: %.o $(LIBVFIO_O) $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $< $(LIBVFIO_O) $(LDLIBS) -o $@ =20 diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 82f255f0486dc..dc8b37df8d1f1 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -22,6 +22,8 @@ #include #include =20 +#include + #include "kselftest.h" #include =20 @@ -220,7 +222,27 @@ static void vfio_pci_group_setup(struct vfio_pci_devic= e *device, const char *bdf ioctl_assert(device->group_fd, VFIO_GROUP_SET_CONTAINER, &device->iommu->= container_fd); } =20 -static void vfio_pci_container_setup(struct vfio_pci_device *device, const= char *bdf) +static void vfio_pci_group_get_device_fd(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) +{ + char arg[64]; + + /* + * If a vf_token exists, argument to VFIO_GROUP_GET_DEVICE_FD + * will be in the form of the following example: + * "0000:04:10.0 vf_token=3Dbd8d9d2b-5a5f-4f5a-a211-f591514ba1f3" + */ + if (vf_token) + snprintf_assert(arg, ARRAY_SIZE(arg), "%s vf_token=3D%s", bdf, vf_token); + else + snprintf_assert(arg, ARRAY_SIZE(arg), "%s", bdf); + + device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, arg); + VFIO_ASSERT_GE(device->fd, 0); +} + +static void vfio_pci_container_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; @@ -238,8 +260,7 @@ static void vfio_pci_container_setup(struct vfio_pci_de= vice *device, const char */ (void)ioctl(iommu->container_fd, VFIO_SET_IOMMU, (void *)iommu_type); =20 - device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, bdf); - VFIO_ASSERT_GE(device->fd, 0); + vfio_pci_group_get_device_fd(device, bdf, vf_token); } =20 static void vfio_pci_device_setup(struct vfio_pci_device *device) @@ -300,12 +321,20 @@ 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) +static void vfio_device_bind_iommufd(int device_fd, int iommufd, + const char *vf_token) { struct vfio_device_bind_iommufd args =3D { .argsz =3D sizeof(args), .iommufd =3D iommufd, }; + uuid_t token_uuid; + + if (vf_token) { + VFIO_ASSERT_EQ(uuid_parse(vf_token, token_uuid), 0); + args.flags |=3D VFIO_DEVICE_BIND_FLAG_TOKEN; + args.token_uuid_ptr =3D (u64)token_uuid; + } =20 ioctl_assert(device_fd, VFIO_DEVICE_BIND_IOMMUFD, &args); } @@ -320,7 +349,8 @@ 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 c= har *bdf) +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, const char *vf_token) { const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); =20 @@ -328,7 +358,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, const char *b VFIO_ASSERT_GE(device->fd, 0); free((void *)cdev_path); =20 - vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); + vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 @@ -344,9 +374,9 @@ struct vfio_pci_device *vfio_pci_device_init(const char= *bdf, struct iommu *iomm device->bdf =3D bdf; =20 if (iommu->mode->container_path) - vfio_pci_container_setup(device, bdf); + vfio_pci_container_setup(device, bdf, NULL); else - vfio_pci_iommufd_setup(device, bdf); + vfio_pci_iommufd_setup(device, bdf, NULL); =20 vfio_pci_device_setup(device); vfio_pci_driver_probe(device); --=20 2.53.0.473.g4a7958ca14-goog