From mboxrd@z Thu Jan 1 00:00:00 1970 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 D9FCC2D97AA for ; Tue, 14 Jul 2026 15:15:25 +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=1784042127; cv=none; b=AgpB6HQLqgd+dFe9CBSEdIKKB1Vno29Em6E5sU1gyObGJ64VVlrSjY5a8wge7tL/I4mlUrJiIPepO3lcz9hkipVermVzTihQIqydYd0QgmKu4ZnXIr0AM0j3RnLU0WjOYoc4DmtWPqCKg33dd6hxkI3VGFDoxPJYi+A6g9EboO4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784042127; c=relaxed/simple; bh=18is/Qt0S+jmUTzhdeluE3sxLqScHvGF040iAxoJfOk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RICAunaGbIy8NHBotpTjyV4cLPc45DihMia7+FzTNBRBn9q2m8Ldy2crKJ/dtSITqti2mTzGvFb9+HwNxkXAsUODfZ+A+QcG8BW4tQz1ZBw1cpwFgHE3s58TfXyFAsdQp6hOetrjvl5+pb9AVyG4jSghH7wrHArBA7LJK3tRY3A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--vipinsh.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hTpjetri; 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--vipinsh.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hTpjetri" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-84877b362f6so7350065b3a.2 for ; Tue, 14 Jul 2026 08:15:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1784042125; x=1784646925; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=JgKrbgFKZxbxCBaliRqkNgo/i0ql++Rr7PTW+pok5fM=; b=hTpjetrirdlqoEFS/f56wAzOxHd4w/QamnydPc19Y/LJtawKyAhOx4W4TPKTm73c6g Eua067ADISKd0fsw9arYvGfi9pU9Ms/x+lq1pMiAqo/Z9YAjcekHMbY0gmg5y1osaHuE IbO9LCx7dFdg6+HVGFeaYWeEj+4UyQZqBcdauktw+pWJVmBvpdJMGA9lH6amUfhmmJVv 9VGDpPsqqHThH/1ymD/XkugLKbs0E8SjGMnO6cOPrta2jHCtNKunARRdfV15rBCs6TI8 Wo842BcgAiRv8Ch0uxDISIRhQ1TmoakAVDXJlOMQwXxIYCzaRw9IFomg/kd7JNjR8ozX E5yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1784042125; x=1784646925; h=content-type: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:content-type; bh=JgKrbgFKZxbxCBaliRqkNgo/i0ql++Rr7PTW+pok5fM=; b=slPTFJrGO3CGuuK2505ow0CG+9lSN0e9SYUGwymqI4ddkz8YRhfjUtxE5FcdfWkCCX KOPxt1u+vVHXMnH/5u54TZfK+T+F8RGdhWAHihBGO2LK20/1AQWseL/yjJTj2m6pKC6T JXoRZW8OrMN8pIyg1CTPTPmS8OwRt95EleOW1pIfo0BezNWIi6C+We6LYIRavRQ8edGd QVn3RbQPh63fEdL158xxJI/0gvbTYsAQCELVNhkTklZJK2Z0RCCFfzgAa/CAPMyvOEVS 2K1IF38OQnsf8rhSvp28Hdox+iXPuLJA1mtCSEBFtMWAoN/flLsvn8Mt55y4AL7FrmaG uBWg== X-Forwarded-Encrypted: i=1; AHgh+RqhrisvnzMOqQwBJKHbBJpYCpfIpjcVYB/2gCP9KaKNe8pzVcTFy7Dq/OpHjIY/lSh+r8tPAtLIkNuE5m0=@vger.kernel.org X-Gm-Message-State: AOJu0YwC9vydIoAv8oT40T1J38HrZM0oLlzltZi7An701cdfES8y2x0L RrXjFDehK3rWP6Jbq1DVRtnTNuICjDdW+SjCtX9TLXO9ZvQZHiSmMSjPTgkEuUiNOuxniA/Pdcl uTUwOVdqHqw== X-Received: from pfbll1.prod.google.com ([2002:a05:6a00:7281:b0:845:e386:e036]) (user=vipinsh job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1f14:b0:845:e9e5:cdec with SMTP id d2e1a72fcca58-84a559bb7dfmr2554072b3a.62.1784042125277; Tue, 14 Jul 2026 08:15:25 -0700 (PDT) Date: Tue, 14 Jul 2026 08:15:00 -0700 In-Reply-To: <20260714151505.3466855-1-vipinsh@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260714151505.3466855-1-vipinsh@google.com> X-Mailer: git-send-email 2.55.0.795.g602f6c329a-goog Message-ID: <20260714151505.3466855-16-vipinsh@google.com> Subject: [PATCH v5 15/20] vfio: selftests: Initialize vfio_pci_device using a VFIO cdev FD From: Vipin Sharma To: kexec@lists.infradead.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org Cc: ajayachandra@nvidia.com, alex@shazbot.org, amastro@fb.com, ankita@nvidia.com, apopple@nvidia.com, bhelgaas@google.com, chrisl@kernel.org, christian.koenig@amd.com, corbet@lwn.net, dmatlack@google.com, graf@amazon.com, jacob.pan@linux.microsoft.com, jgg@nvidia.com, jgg@ziepe.ca, jrhilke@google.com, julianr@linux.ibm.com, kees@kernel.org, kevin.tian@intel.com, leon@kernel.org, leonro@nvidia.com, lukas@wunner.de, mattev@meta.com, michal.winiarski@intel.com, parav@nvidia.com, pasha.tatashin@soleen.com, praan@google.com, pratyush@kernel.org, rananta@google.com, rientjes@google.com, rodrigo.vivi@intel.com, rppt@kernel.org, saeedm@nvidia.com, schnelle@linux.ibm.com, skhan@linuxfoundation.org, skhawaja@google.com, vipinsh@google.com, vivek.kasireddy@intel.com, witu@nvidia.com, yanjun.zhu@linux.dev, yi.l.liu@intel.com Content-Type: text/plain; charset="UTF-8" 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 writing selftests where VFIO device FD is preserved using liveupdate and retrieved later using liveupdate ioctl after kexec. Co-developed-by: David Matlack Signed-off-by: David Matlack Signed-off-by: Vipin Sharma --- .../lib/include/libvfio/vfio_pci_device.h | 3 +++ .../selftests/vfio/lib/vfio_pci_device.c | 25 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 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 3eabead717bb..66fc29662daa 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 @@ -40,6 +40,9 @@ struct vfio_pci_device { struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iommu *iommu); void vfio_pci_device_free(struct vfio_pci_device *device); +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 94dc5fcecbeb..a6e0e7c6e18e 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -414,9 +414,13 @@ void vfio_pci_cdev_open(struct vfio_pci_device *device, const char *bdf) } static void vfio_pci_iommufd_setup(struct vfio_pci_device *device, - const char *bdf, const char *vf_token) + const char *bdf, int device_fd, + const char *vf_token) { - vfio_pci_cdev_open(device, bdf); + if (device_fd >= 0) + device->fd = device_fd; + else + vfio_pci_cdev_open(device, bdf); vfio_device_bind_iommufd(device->fd, device->iommu->iommufd, vf_token); vfio_device_attach_iommufd_pt(device->fd, device->iommu->ioas_id); } @@ -440,16 +444,20 @@ void vfio_pci_device_free(struct vfio_pci_device *device) free(device); } -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; device = vfio_pci_device_alloc(bdf, iommu); - if (iommu->mode->container_path) + if (iommu->mode->container_path) { + VFIO_ASSERT_EQ(device_fd, -1); vfio_pci_container_setup(device, bdf, NULL); - else - vfio_pci_iommufd_setup(device, bdf, NULL); + } else { + vfio_pci_iommufd_setup(device, bdf, device_fd, NULL); + } vfio_pci_device_setup(device); vfio_pci_driver_probe(device); @@ -457,6 +465,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.55.0.795.g602f6c329a-goog