From nobody Mon Dec 1 23:06:44 2025 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 13F39341642 for ; Wed, 26 Nov 2025 19:36:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764185799; cv=none; b=OMa5IsEjZsYPgHzB06IaYC57VjjMKnmW63v162W78pSMShr/wRhuRem0R3pf18D5VMgYFMzOPd8oxXQVsnTxuAHZsnGLMl3+1YtN8f87ny79S2fbYRGOo2bmXCEZi7bYaqKAzWkRAxSKyNHSvd5i44/sJf43g+tdV+2napYCXcs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764185799; c=relaxed/simple; bh=oOTZ22wFJZqnFzsFStI6K/6j0+r7OjYCkSOC13WPK5c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=d+2mz8GD0e4ErzbQ8QeAGl7pkJ8/AWjJebeEF+Z9FYwbF6JhsoAY2OlkCOvxw02iSF/HcpblgU+a0NCd1rsCTmtE6OPy0UJNC2niAE7mF1QmBrC5+vy7RUordeuh1zeEC4N04kKJ+gibtk0NjOg2GQYLMJhsjURQRrMKlQjfQGE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--dmatlack.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DTHOE9Hi; arc=none smtp.client-ip=209.85.210.202 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--dmatlack.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DTHOE9Hi" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-7b89ee2c1a4so76918b3a.2 for ; Wed, 26 Nov 2025 11:36:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1764185796; x=1764790596; 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=JNFE9+5HOL1VTfFvE91RbwOqejjTuBWxPiJYobw2Kcg=; b=DTHOE9HimPqnsmGO6KjbvxuhCOECIH5aP141Aqw88qkgl6OjQ/aonnhvd3T1slhAQ7 zBUK7gmd6ghG/GH2jPBWTHoUDeSuChKWwmm2+AzYrL/XB6eFCwQRVy1I/+VA+NZYZMRK HKRnSVQga9HjIrnis7Uycvjamsaea+CHWIyRI5M97xgqVWAwxARlG0GlRzxQrR0iVdyA /bROYq3pk8j8XhOnWoMwpZY9W1UzpnC3Py2OcW0iNfEd78pk1+vp8IjPipFXenTadPdj 9zAWfR+KXH9iCYV5yHBISKm6QDihYJqf32tPuugvozilzqVpr/g1kbIF3I11epmvepsW v0GA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764185796; x=1764790596; 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=JNFE9+5HOL1VTfFvE91RbwOqejjTuBWxPiJYobw2Kcg=; b=rFSE+Oi5qd0DeAB0qHSnD8Y6ar0YoWl77MHgw6Lpwphgz0nQN1B86a7ql3OKKd+zo7 qBgzXFwYdOIidN5Fhgm62n0uOZTGSb1f0itQe0QtbUn1jGCli2pkY5hpLJM7GHS9Lpx2 Pa7g5D09/NugaXmvkVOQgrb98d94cnR3HIFDDOo9VQ/uDZUob1c1kfGt1rfg0b43GauX 5RJm0Ijp0OnDSSsl42YWHlfy7YCevvszHcLmoXQuutEvAPPqXYDCXxdW6RAS24ICsOxc GEdqUYxdskoKGhpa8N/V9jTOxGd5iYQQC3WBXc4VQwABbfK5paaXSMrokeLvcbWoOp3t 6t+g== X-Forwarded-Encrypted: i=1; AJvYcCVePwk0Sa7bEwS6b3uyKrDdMsRS3emFIXM9aGPw+gNQ1rmWKVETF30202PD2s8x2RYM6/XyIfe42a4BIw8=@vger.kernel.org X-Gm-Message-State: AOJu0Yyjhl+U+WgKFx3QdcKEantEj9FYUTjZl4UvHOMI0r1gR/Us/iSV ENZxsMDfvi/osWx9hHzkVOJMbVPAihqiRzhCEwaiMmRgJxSHMRIieaO3NwsCpplUHzbru1P3r63 FWPqIy/Fe8vDWLA== X-Google-Smtp-Source: AGHT+IFQhjKrX/N6hJ5PO1HA/1hqWlujUuZce1Edh5MBtvzNhWFpXfCucZ7C6spTR8QDg6JjwdBie16yKE0H0A== X-Received: from pfbmy26-n2.prod.google.com ([2002:a05:6a00:6d5a:20b0:7ba:3994:c73f]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:2d88:b0:7b9:ef46:ec70 with SMTP id d2e1a72fcca58-7c58e50c16emr18773625b3a.23.1764185795950; Wed, 26 Nov 2025 11:36:35 -0800 (PST) Date: Wed, 26 Nov 2025 19:36:03 +0000 In-Reply-To: <20251126193608.2678510-1-dmatlack@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20251126193608.2678510-1-dmatlack@google.com> X-Mailer: git-send-email 2.52.0.487.g5c8c507ade-goog Message-ID: <20251126193608.2678510-17-dmatlack@google.com> Subject: [PATCH 16/21] vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD From: David Matlack To: Alex Williamson Cc: Adithya Jayachandran , Alex Mastro , Alistair Popple , Andrew Morton , Bjorn Helgaas , Chris Li , David Matlack , David Rientjes , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Josh Hilke , Kevin Tian , kvm@vger.kernel.org, Leon Romanovsky , linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-pci@vger.kernel.org, Lukas Wunner , Mike Rapoport , Parav Pandit , Pasha Tatashin , Philipp Stanner , Pratyush Yadav , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , Tomita Moeko , Vipin Sharma , William Tu , Yi Liu , Yunxiang Li , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Vipin Sharma Use the given VFIO cdev FD to initialize vfio_pci_device in VFIO selftests. Add the assertion to make sure that passed cdev FD is not used with legacy VFIO APIs. If VFIO cdev FD is provided then do not open the device instead use the FD for any interaction with the device. This API will allow to write selftests where VFIO device FD is preserved using liveupdate and retrieved later using liveupdate ioctl after kexec. Signed-off-by: Vipin Sharma Co-Developed-by: David Matlack Signed-off-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 3 ++ .../selftests/vfio/lib/vfio_pci_device.c | 33 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 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..896dfde88118 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 @@ -38,6 +38,9 @@ struct vfio_pci_device { #define dev_info(_dev, _fmt, ...) printf("%s: " _fmt, (_dev)->bdf, ##__VA_= ARGS__) #define dev_err(_dev, _fmt, ...) fprintf(stderr, "%s: " _fmt, (_dev)->bdf,= ##__VA_ARGS__) =20 +struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, + struct iommu *iommu, + int device_fd); struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu); void vfio_pci_device_cleanup(struct vfio_pci_device *device); =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 468ee1c61b0c..e9423dc3864a 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -319,19 +319,27 @@ 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, int device_fd) { - const char *cdev_path =3D vfio_pci_get_cdev_path(bdf); + const char *cdev_path; =20 - device->fd =3D open(cdev_path, O_RDWR); - VFIO_ASSERT_GE(device->fd, 0); - free((void *)cdev_path); + if (device_fd >=3D 0) { + device->fd =3D device_fd; + } else { + cdev_path =3D vfio_pci_get_cdev_path(bdf); + device->fd =3D open(cdev_path, O_RDWR); + VFIO_ASSERT_GE(device->fd, 0); + free((void *)cdev_path); + } =20 vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } =20 -struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, + struct iommu *iommu, + int device_fd) { struct vfio_pci_device *device; =20 @@ -341,10 +349,12 @@ struct vfio_pci_device *vfio_pci_device_init(const ch= ar *bdf, struct iommu *iomm device->bdf =3D bdf; device->iommu =3D iommu; =20 - if (device->iommu->mode->container_path) + if (device->iommu->mode->container_path) { + VFIO_ASSERT_EQ(device_fd, -1); vfio_pci_container_setup(device, bdf); - else - vfio_pci_iommufd_setup(device, bdf); + } else { + vfio_pci_iommufd_setup(device, bdf, device_fd); + } =20 vfio_pci_device_setup(device); vfio_pci_driver_probe(device); @@ -352,6 +362,11 @@ struct vfio_pci_device *vfio_pci_device_init(const cha= r *bdf, struct iommu *iomm return device; } =20 +struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu= *iommu) +{ + return __vfio_pci_device_init(bdf, iommu, /*device_fd=3D*/-1); +} + void vfio_pci_device_cleanup(struct vfio_pci_device *device) { int i; --=20 2.52.0.487.g5c8c507ade-goog