Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 C5EEF34B18E for ; Wed, 26 Nov 2025 19:36:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764185799; cv=none; b=J0ugin9+rk1gfAnPOLoQbkeG4tqT2vTVBwKsa92Vh2JrVizgJpwbx5kmaJ421Gb541sLT8+I0rANxxzF9dzZN/qwnIAznpFW5VzL05vFtdHrOe/aNiEvNCXFzi8QtCdAJ3IJ2SDYyYCWbQSyOlpNEoAI9k7gwbI1D2TiAfkJ3QU= 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.201 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-f201.google.com with SMTP id d2e1a72fcca58-7b89ee2c1a4so76919b3a.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=P6F7MGfiNqml2UkesGNtbk3eBbcHl0yWFfDgTwshCradonSk8D/h4dkpj/iNth+JTh 18gvz/h8ytmNjqRgSdMeJ2u++Kgdsknufhxx41kjdRNgcJ279uo0OWHqW+5Hxn6sNI0T f2799XoEA9pgYI2hX73qG6ERHt2YwGlP0PK6y50D4Km6qVf1oW1PjklQ96MBVDCDzn0Y VOXi80H/FhkSwwliV1AVLJLnQ8pGm5RJ+lfKT3/Xb0uzW1LERRLtmLrHwIi5MCAnDFPg WbQHN0m5sBf/03PBx2U1vB3g28bFRQGagMEp1N0BGSjvjRacTz9UVIeF1w/jwkzZ9gNK EBZA== X-Forwarded-Encrypted: i=1; AJvYcCXhgKVXv4ebUZn3vJ3mOH9g7cFWz4dcVkBWAke7Pi7cmD5vKTcRoOlJaqyGi+NvVpQ2WFg=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/LJygr8rtXxhYkObOBKOrjWbzpsIPSLZxLT7if75HUYXIVBQv LVpa/6UCNTyIB+5V1YHEkoxycWhSpXACW12FQqDfKAB4LH43iOZequdRxV06Y6T0BbvXVnxnWHS +Rwc7dsGBMveozg== 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: kvm@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-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_device.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__) +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); diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/testing/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); } -static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, const char *bdf) +static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, + const char *bdf, int device_fd) { - const char *cdev_path = vfio_pci_get_cdev_path(bdf); + const char *cdev_path; - device->fd = open(cdev_path, O_RDWR); - VFIO_ASSERT_GE(device->fd, 0); - free((void *)cdev_path); + if (device_fd >= 0) { + device->fd = device_fd; + } else { + cdev_path = vfio_pci_get_cdev_path(bdf); + device->fd = open(cdev_path, O_RDWR); + VFIO_ASSERT_GE(device->fd, 0); + free((void *)cdev_path); + } vfio_device_bind_iommufd(device->fd, device->iommu->iommufd); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } -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; @@ -341,10 +349,12 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iomm device->bdf = bdf; device->iommu = iommu; - 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); + } vfio_pci_device_setup(device); vfio_pci_driver_probe(device); @@ -352,6 +362,11 @@ struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iomm return device; } +struct vfio_pci_device *vfio_pci_device_init(const char *bdf, struct iommu *iommu) +{ + return __vfio_pci_device_init(bdf, iommu, /*device_fd=*/-1); +} + void vfio_pci_device_cleanup(struct vfio_pci_device *device) { int i; -- 2.52.0.487.g5c8c507ade-goog