From nobody Thu Apr 2 22:03:12 2026 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.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 304E13DB65E for ; Mon, 23 Mar 2026 23:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774310359; cv=none; b=BEJxM4SaJbl1sGjyiZ7wbbGNyB8B2kxftW9vrHc7GJcg6pn+x86EEyMnA40ZwJO/IGDvVwttV/3rhdBX+i9TYykS/QKgGej12NOMYJ90yEvpq3jKAiu/7yOakPjMq+uEMs0Ap2qbVrg90jpS2cCI+4FV3TQ4m+jwm8CPktdx3Wg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774310359; c=relaxed/simple; bh=lvaufN7TJ12UOehxU4H8lGXb0W9AqjyOq1Y2ZRN7GDg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jT4q4MJVTa2NZuUA/qOHOZ8G9WpYV3AN4RZEN16p66blRHzZRQhgc21AD6+LN51BXYO/QnfAJhLibodC9k8KV41WsH3o5+0xH64Zej2tJ7lrP4PDVKmuiXueQ7nzFg6GMVuEdpSmxYjCHLD3RI6sPHm1N/UDY4eFNFHT3gJNKZ0= 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=GblrU+QO; arc=none smtp.client-ip=209.85.216.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--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="GblrU+QO" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-35b9246248dso4896343a91.0 for ; Mon, 23 Mar 2026 16:59:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1774310356; x=1774915156; 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=a6ai3Q1Y7nW49bYKBphuq9xPRlXveX2MorY71NenjMU=; b=GblrU+QOeyYAi7Uxv/yN8eKWfVSEz0OGT4lyLInM1TUTJ/LI/pmkHNUXoXcSo7QaPm j41HcWgHt3g3ciNZVQX1sPFf/vgrCyOauzcRNmNTzuKjDSyqviDpmLaCN0ouzT09B8HK olJ9h6f3TeT0yLIC6wuW2iT4qBN3tvtgLFweGU5W3Itdw9v5Dw7pD9BFwDSWeBVWdmbh clKw8feAwtbtqwZRz8J4ipqL3kPjkDZuljBTisBZwOD0jZAHf7QLXEeGlxP7RaMXPp9p 35qUj9BvhZ3JZtzlEa0f0TswHXHVGdN5METHCzFKt2ird+/BxNAM5Lr7g2BB/L25QZrj eqjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774310356; x=1774915156; 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=a6ai3Q1Y7nW49bYKBphuq9xPRlXveX2MorY71NenjMU=; b=i9nglOVvW6MSY+rG5LSd9jOaBrr5sz9MIDmWGiqtBKLvJvK97Ks5HAgZu5gQgzTZCu v1tacl9EwkcqvG6jKAP+Ek9PAnxemkZJHZ3FQr2k0oY30/vrLKoc2g5YvCttpuflIFik oBDZRv3+JEquTkoENCrpI4ua42tCpVo0XxiSjb2wshDflKDOAj5qV0YQPIvSV9ktDDqD lTCc/p5Kvpo/aVHG8ZkiwtfNSFU2BPtff2R5FZyU/RKjXeuB1k/Y2PZwtoy/7oCXJYfS Bt0OyctuH5X0cISPD7x0MWiDT6IKzel+EqdZVHgdyIQNXLVaBQiUIEmy3nUi8J3BT/UY fdIg== X-Forwarded-Encrypted: i=1; AJvYcCUWa46ByZ0tTcKiuG57/hOWPdfnwgS5KDHmKQDkFJuRbOxdgcB22AzdHDixrjGikuZfEMTX4dKwKaw2lao=@vger.kernel.org X-Gm-Message-State: AOJu0YyuSs04h/KcZYZ3rJxwCCeQJqHyyFnFOFY5ATkQFXI66iCYKl/0 dlDOGYIdvKmMyQb4x9yzNkRELzTlcWmkBHt+EOrgrrNQA7A+ZeswLF2F05MywWoXNiy5B/GvxQV yTMWj3bRx9T/sqg== X-Received: from pjxu14.prod.google.com ([2002:a17:90a:db4e:b0:35b:a2a4:de5a]) (user=dmatlack job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:3891:b0:35b:e85a:865e with SMTP id 98e67ed59e1d1-35be85a8693mr5878913a91.33.1774310356298; Mon, 23 Mar 2026 16:59:16 -0700 (PDT) Date: Mon, 23 Mar 2026 23:58:14 +0000 In-Reply-To: <20260323235817.1960573-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: <20260323235817.1960573-1-dmatlack@google.com> X-Mailer: git-send-email 2.53.0.983.g0bb29b3bc5-goog Message-ID: <20260323235817.1960573-23-dmatlack@google.com> Subject: [PATCH v3 22/24] vfio: selftests: Expose low-level helper routines for setting up struct vfio_pci_device From: David Matlack To: Alex Williamson , Bjorn Helgaas Cc: Adithya Jayachandran , Alexander Graf , Alex Mastro , Andrew Morton , Ankit Agrawal , Arnd Bergmann , Askar Safin , "Borislav Petkov (AMD)" , Chris Li , Dapeng Mi , David Matlack , David Rientjes , Feng Tang , Jacob Pan , Jason Gunthorpe , Jason Gunthorpe , Jonathan Corbet , Josh Hilke , Kees Cook , Kevin Tian , kexec@lists.infradead.org, kvm@vger.kernel.org, Leon Romanovsky , Leon Romanovsky , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-pci@vger.kernel.org, Li RongQing , Lukas Wunner , Marco Elver , "=?UTF-8?q?Micha=C5=82=20Winiarski?=" , Mike Rapoport , Parav Pandit , Pasha Tatashin , "Paul E. McKenney" , Pawan Gupta , "Peter Zijlstra (Intel)" , Pranjal Shrivastava , Pratyush Yadav , Raghavendra Rao Ananta , Randy Dunlap , Rodrigo Vivi , Saeed Mahameed , Samiullah Khawaja , Shuah Khan , Vipin Sharma , Vivek Kasireddy , William Tu , Yi Liu , Zhu Yanjun Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expose a few low-level helper routings for setting up vfio_pci_device structs. These routines will be used in a subsequent commit to assert that VFIO_GROUP_GET_DEVICE_FD fails under certain conditions. Signed-off-by: David Matlack --- .../lib/include/libvfio/vfio_pci_device.h | 5 +++ .../selftests/vfio/lib/vfio_pci_device.c | 33 +++++++++++++------ 2 files changed, 28 insertions(+), 10 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 896dfde88118..2389c7698335 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 @@ -125,4 +125,9 @@ static inline bool vfio_pci_device_match(struct vfio_pc= i_device *device, =20 const char *vfio_pci_get_cdev_path(const char *bdf); =20 +/* Low-level routines for setting up a struct vfio_pci_device */ +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu); +void vfio_pci_group_setup(struct vfio_pci_device *device); +void vfio_pci_iommu_setup(struct vfio_pci_device *device); + #endif /* SELFTESTS_VFIO_LIB_INCLUDE_LIBVFIO_VFIO_PCI_DEVICE_H */ diff --git a/tools/testing/selftests/vfio/lib/vfio_pci_device.c b/tools/tes= ting/selftests/vfio/lib/vfio_pci_device.c index e9215c712cda..66ee268110e2 100644 --- a/tools/testing/selftests/vfio/lib/vfio_pci_device.c +++ b/tools/testing/selftests/vfio/lib/vfio_pci_device.c @@ -220,7 +220,7 @@ static unsigned int vfio_pci_get_group_from_dev(const c= har *bdf) return group; } =20 -static void vfio_pci_group_setup(struct vfio_pci_device *device, const cha= r *bdf) +void vfio_pci_group_setup(struct vfio_pci_device *device) { struct vfio_group_status group_status =3D { .argsz =3D sizeof(group_status), @@ -228,7 +228,7 @@ static void vfio_pci_group_setup(struct vfio_pci_device= *device, const char *bdf char group_path[32]; int group; =20 - group =3D vfio_pci_get_group_from_dev(bdf); + group =3D vfio_pci_get_group_from_dev(device->bdf); snprintf(group_path, sizeof(group_path), "/dev/vfio/%d", group); =20 device->group_fd =3D open(group_path, O_RDWR); @@ -240,14 +240,12 @@ static void vfio_pci_group_setup(struct vfio_pci_devi= ce *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) +void vfio_pci_iommu_setup(struct vfio_pci_device *device) { struct iommu *iommu =3D device->iommu; unsigned long iommu_type =3D iommu->mode->iommu_type; int ret; =20 - vfio_pci_group_setup(device, bdf); - ret =3D ioctl(iommu->container_fd, VFIO_CHECK_EXTENSION, iommu_type); VFIO_ASSERT_GT(ret, 0, "VFIO IOMMU type %lu not supported\n", iommu_type); =20 @@ -257,8 +255,14 @@ static void vfio_pci_container_setup(struct vfio_pci_d= evice *device, const char * because the IOMMU type is already set. */ (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); +static void vfio_pci_container_setup(struct vfio_pci_device *device) +{ + vfio_pci_group_setup(device); + vfio_pci_iommu_setup(device); + + device->fd =3D ioctl(device->group_fd, VFIO_GROUP_GET_DEVICE_FD, device->= bdf); VFIO_ASSERT_GE(device->fd, 0); } =20 @@ -358,9 +362,7 @@ static void vfio_pci_iommufd_setup(struct vfio_pci_devi= ce *device, 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, - int device_fd) +struct vfio_pci_device *vfio_pci_device_alloc(const char *bdf, struct iomm= u *iommu) { struct vfio_pci_device *device; =20 @@ -371,9 +373,20 @@ struct vfio_pci_device *__vfio_pci_device_init(const c= har *bdf, device->iommu =3D iommu; device->bdf =3D bdf; =20 + return device; +} + +struct vfio_pci_device *__vfio_pci_device_init(const char *bdf, + struct iommu *iommu, + int device_fd) +{ + struct vfio_pci_device *device; + + device =3D vfio_pci_device_alloc(bdf, iommu); + if (iommu->mode->container_path) { VFIO_ASSERT_EQ(device_fd, -1); - vfio_pci_container_setup(device, bdf); + vfio_pci_container_setup(device); } else { vfio_pci_iommufd_setup(device, bdf, device_fd); } --=20 2.53.0.983.g0bb29b3bc5-goog