From nobody Tue Dec 16 20:46:00 2025 Received: from mail-ot1-f74.google.com (mail-ot1-f74.google.com [209.85.210.74]) (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 A792B328607 for ; Wed, 10 Dec 2025 18:14:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390466; cv=none; b=FmINA1dn7XUh01XV1Z6cU7kHn4h6JOq8r6QzoSihCRxF3oColtOvbAMDWgi+uEkeEfKVayjEdl3GJgqkrZdprYBNLcS3lqzKs34hCTtLrEOXhpUt6VqAamw7uK5QQ/VamPu0FHRsz9VgojvxrACsl5EWavUIE7JPZI0Kntx6M74= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765390466; c=relaxed/simple; bh=l+cCwjio5EPRmT49D2SJmoZp0TKezhIl5PRxRDOzXLU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=gZncHGhY3Aop7pwoEoEjj72goic5UAMJKkjB51u1gf6k7CYVzgNFKo6LEse5mbBtMmfpTncj6H3++qVEo/QYjlHLMmn0q9xD/FeHgnDFt6DUw3/zQ59fShGuh4jxNaL5dy1H9V1KkSPPy2cUWuxPGq9qmwtgIOCHpcUPO/eq9bY= 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=IEh41gQ3; arc=none smtp.client-ip=209.85.210.74 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="IEh41gQ3" Received: by mail-ot1-f74.google.com with SMTP id 46e09a7af769-7c702347c6eso222587a34.1 for ; Wed, 10 Dec 2025 10:14:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1765390464; x=1765995264; 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=cc3yaQBIDdGsG6IwnoL+tF4JZwHQKMPcuou0OF8CLFo=; b=IEh41gQ3wPZ35EPFuJ8/LUzLftTj1XpZ3uH/fb/qAL6b9IkEb+PJqHyLVr82U4SUoE 2iBVat6pL13bT3keVlXnV0SVw2YQl1nrI19DvplEjjZxvw/pvrs4PK8xC561v+SFynzx EZ6ZUwJ3e/Ljg/R2EQrxjdFMp/uZWhgK6+0kjrzhgsgenV2yykSLr2HkhG9sCEQJT3oE Xd/2dSPh0VhOgzF3AjNd/X65VY8AfIz4BcKswSvrLqRPMecoUdvMDSvyouJgS9GiY2fa 6xogG7QaW04oMxOAoPGwQpLpsjmvPyBqRv8Aq8X/or+iCUcF3oSfK1bHWYTdZPMKrY+m Kr7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765390464; x=1765995264; 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=cc3yaQBIDdGsG6IwnoL+tF4JZwHQKMPcuou0OF8CLFo=; b=IbGFwlE7fhJ+aYiPguoQZrozz/V9Up4K54e+OOIsVYPpmZdEP5qzjMEeW48nnMoaPl L8sUKLDzx51n0MiZhSC+EQ3i93s3EzVCSM670N7zGXZkv3B+u7TueIAk3an9Qk3h74i8 c3FcSiP4ABOtA+br92drLLJscdbs1h/C2eSHSvixnHzV35ZHG/UIlBmGABP0nF/lLHHE nAK5GQJU7uPfbNBAtRHBpH0C5JPtQclK21cwz1dkoTZySqjszRmIn6GlDiuvKD0yvgVx QoRVpxwP40Khv0OqKfmIGlWBLNWvRXgx68TpkyUide/K8BeG5+6mTH6DM5HNFxVBba+E vk9w== X-Forwarded-Encrypted: i=1; AJvYcCV5XgHGsN8aet6Pt/NsTccHzZyHWR6cgv93c6v09Uh2mefyTDg4agpHV2YVn8gT8f+nsiX0j67pWo9TKvQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx9veoHwpisCrjJRjFfg9NOLRDjXQcQYA8tqpIf/9GnNbKIaFos qLoRd/PoKkb91cJzPfjpxhpWg/1KbgU5G9swqJbhlarH+7vmy38agzRdaCd4cPY7m24DHritZ2/ yGXgW4pwa0g== X-Google-Smtp-Source: AGHT+IH7asKz8/hYJaH0fCIkSFlYh1Npe7OXIQ50oexIrSsl3V8oTPQzBwYi5e+FIdrmm5DKQ0bUjKgBpNRR X-Received: from ilbcb14.prod.google.com ([2002:a05:6e02:318e:b0:433:78dd:2882]) (user=rananta job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:1849:b0:659:9a49:8e4b with SMTP id 006d021491bc7-65b2ac11e38mr2143163eaf.27.1765390463721; Wed, 10 Dec 2025 10:14:23 -0800 (PST) Date: Wed, 10 Dec 2025 18:14:14 +0000 In-Reply-To: <20251210181417.3677674-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: <20251210181417.3677674-1-rananta@google.com> X-Mailer: git-send-email 2.52.0.239.gd5f0c6e74e-goog Message-ID: <20251210181417.3677674-4-rananta@google.com> Subject: [PATCH v2 3/6] vfio: selftests: Extend container/iommufd setup for passing vf_token From: Raghavendra Rao Ananta To: David Matlack , Alex Williamson , Alex Williamson Cc: 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 --- tools/testing/selftests/vfio/lib/libvfio.mk | 4 +- .../selftests/vfio/lib/vfio_pci_device.c | 45 +++++++++++++++---- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/tools/testing/selftests/vfio/lib/libvfio.mk b/tools/testing/se= lftests/vfio/lib/libvfio.mk index b7857319c3f1f..459b14c6885a8 100644 --- a/tools/testing/selftests/vfio/lib/libvfio.mk +++ b/tools/testing/selftests/vfio/lib/libvfio.mk @@ -15,6 +15,8 @@ LIBVFIO_C +=3D drivers/ioat/ioat.c LIBVFIO_C +=3D drivers/dsa/dsa.c endif =20 +LDLIBS +=3D -luuid + LIBVFIO_OUTPUT :=3D $(OUTPUT)/libvfio =20 LIBVFIO_O :=3D $(patsubst %.c, $(LIBVFIO_OUTPUT)/%.o, $(LIBVFIO_C)) @@ -25,6 +27,6 @@ $(shell mkdir -p $(LIBVFIO_O_DIRS)) CFLAGS +=3D -I$(LIBVFIO_SRCDIR)/include =20 $(LIBVFIO_O): $(LIBVFIO_OUTPUT)/%.o : $(LIBVFIO_SRCDIR)/%.c - $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< $(LDLIBS) -o $@ =20 EXTRA_CLEAN +=3D $(LIBVFIO_OUTPUT) diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index 9b2a123cee5fc..ac9a5244ddc46 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -12,6 +12,7 @@ #include =20 #include +#include #include #include #include @@ -199,7 +200,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] =3D {0}; + + /* + * 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(arg, ARRAY_SIZE(arg), "%s vf_token=3D%s", bdf, vf_token); + else + snprintf(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; @@ -217,8 +238,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) @@ -279,12 +299,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 =3D {0}; + + 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); } @@ -299,7 +327,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 @@ -307,7 +336,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 @@ -323,9 +352,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.52.0.239.gd5f0c6e74e-goog