From nobody Wed Apr 8 23:38:27 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 4E0263F0AA2 for ; Thu, 2 Apr 2026 17:31: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=1775151070; cv=none; b=gTzo/oDYatAgTwmWis9b1DIj7qEMcDq0K3Me+0T39nAgpqPZqtWYq2ICZWY2wkCRcq0Esz8c6kH2e8C8Qioh+M5NOB09txg31MN5rhz5sPph3uR9nYlQpDsuM2osJ1SwS5fYU1ccLgkBl7qSuFeENgHDpcOER/irnOTJppmOQn4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775151070; c=relaxed/simple; bh=aioNoLlDzw5Q8eJ+PJale07yZdVO/sraFRI5eymlHV0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XIz9tTZp3LUjNN1t7QZGNp4l5KtJFyyzzfhprLzYfTwRigimsXEWFOpD89znAb/PjGeMiRfJfQNhVgJ9ll/C6mem1Ux4/hol4zG+2638fDa5OG+Bgexn+SfUCzhUQ2u0DEauK9gIPHUDMVvzU8AZgsAfhy52ZFlH966KdZocV/Y= 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=pFJh5Hjs; 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="pFJh5Hjs" Received: by mail-oa1-f73.google.com with SMTP id 586e51a60fabf-40450320b4fso1828164fac.0 for ; Thu, 02 Apr 2026 10:31:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1775151067; x=1775755867; 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=LjG0Gd3MypaxsBilbokFGxAV+Zt5lu/q3yDnocf8EBM=; b=pFJh5HjsTimr1bWreO1wTldCTRrcdYbOd9pD1xzhINYvkcXcaVmMLD9EMUP1iOdsy2 UuzAKV1bBnP1QEjxwKN1SSkDncjg4iinBPPm5I/5YBqvG2gvDjkytjPtPmBmzHXwMXCJ hFVdTKdMHTqhf2DaFdk34aPmdIViK0ywshOx7nf5A9fvSOt5GY5FZVfbV+j2JeyIgUoi k5QbZ1X5/2rar7vpNTe6puNGmy5VdZiIvUKcZl0SgjxhvsSEb/WH8rIi+RSYiHLDkuxW +m5LbJhUTADuE/Ec3ej5woRsFMBMzoOJp29J5WuF/LkbCiPCl7VH7afmS509gONp7++5 7+kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1775151067; x=1775755867; 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=LjG0Gd3MypaxsBilbokFGxAV+Zt5lu/q3yDnocf8EBM=; b=Cl4NPtyIXcZYZxg63wBhXWMzFfJE2OE6STdBAame4aoOUD+92gSOBdl5XuhBh6LDYY i77t4oeQm4u2JK/0+mMv1gEDAew2rCpvRuclq50b/aTG6r8OY+PYzGDQJjN7OBjWJbJg Du0yc8f7NLP+Q62yYMKl2ldZAK9or8Va1eVHo2k1fHj7LYmKbbMeFtU/vaOZrQ6AQcGq Re4SLchhQdoN66DdApuA5UsR6cjJpCkKBtpKfNutyHBZDTDTU/69RadKuYGPdEyfNF7N iQEzH1DZBJQ3DQaqYYphQyxUacJdjTSyMU/o6aYNoQbQm2UQh4y9P195Xmvqh3x1ZoJg 3Tzw== X-Forwarded-Encrypted: i=1; AJvYcCUlPuoVaqZEKKoYlDxQAY62vDn1EtISg9p/7eIScqOR39McqHewKPGm1gJ7GwhUK51FVM7o5RdctXxFdpQ=@vger.kernel.org X-Gm-Message-State: AOJu0YxoaEbHcsbU4Sb/4UzfgMM4WGZDAbD1ELfZEfYgLCfVjBLcXc13 TgDkb17uQ02LFJ5eW0AVAp8ItHCqhoPVMv2VRj/lgxUZJCf4wCJQB00sYlONY2kZfazPUj7CpWQ f/3HWVvNiVg== X-Received: from jakz8.prod.google.com ([2002:a05:6638:3188:b0:5d6:5cd9:76c7]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1614:b0:67e:2c8:c0c0 with SMTP id 006d021491bc7-680a8548985mr2283825eaf.58.1775151067129; Thu, 02 Apr 2026 10:31:07 -0700 (PDT) Date: Thu, 2 Apr 2026 17:30:55 +0000 In-Reply-To: <20260402173059.1018805-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: <20260402173059.1018805-1-rananta@google.com> X-Mailer: git-send-email 2.53.0.1213.gd9a14994de-goog Message-ID: <20260402173059.1018805-5-rananta@google.com> Subject: [PATCH v7 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.1213.gd9a14994de-goog