Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

vfio: selftests: Move helper to get cdev path to libvfio

Move the helper function to get the VFIO cdev path to libvfio so that it
can be used in libvfio in a subsequent commit.

No functional change intended.

Acked-by: Shuah Khan <skhan@linuxfoundation.org>
Signed-off-by: David Matlack <dmatlack@google.com>
Link: https://lore.kernel.org/r/20250822212518.4156428-24-dmatlack@google.com
Signed-off-by: Alex Williamson <alex.williamson@redhat.com>

authored by

David Matlack and committed by
Alex Williamson
118e073e 35b05bd9

+34 -32
+1
tools/testing/selftests/vfio/lib/include/vfio_util.h
··· 175 175 * If BDF cannot be determined then the test will exit with KSFT_SKIP. 176 176 */ 177 177 const char *vfio_selftests_get_bdf(int *argc, char *argv[]); 178 + const char *vfio_pci_get_cdev_path(const char *bdf); 178 179 179 180 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type); 180 181 void vfio_pci_device_cleanup(struct vfio_pci_device *device);
+31
tools/testing/selftests/vfio/lib/vfio_pci_device.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0-only 2 + #include <dirent.h> 2 3 #include <fcntl.h> 3 4 #include <libgen.h> 4 5 #include <stdlib.h> ··· 331 330 332 331 for (i = 0; i < ARRAY_SIZE(device->msi_eventfds); i++) 333 332 device->msi_eventfds[i] = -1; 333 + } 334 + 335 + const char *vfio_pci_get_cdev_path(const char *bdf) 336 + { 337 + char dir_path[PATH_MAX]; 338 + struct dirent *entry; 339 + char *cdev_path; 340 + DIR *dir; 341 + 342 + cdev_path = calloc(PATH_MAX, 1); 343 + VFIO_ASSERT_NOT_NULL(cdev_path); 344 + 345 + snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf); 346 + 347 + dir = opendir(dir_path); 348 + VFIO_ASSERT_NOT_NULL(dir, "Failed to open directory %s\n", dir_path); 349 + 350 + while ((entry = readdir(dir)) != NULL) { 351 + /* Find the file that starts with "vfio" */ 352 + if (strncmp("vfio", entry->d_name, 4)) 353 + continue; 354 + 355 + snprintf(cdev_path, PATH_MAX, "/dev/vfio/devices/%s", entry->d_name); 356 + break; 357 + } 358 + 359 + VFIO_ASSERT_NE(cdev_path[0], 0, "Failed to find vfio cdev file.\n"); 360 + VFIO_ASSERT_EQ(closedir(dir), 0); 361 + 362 + return cdev_path; 334 363 } 335 364 336 365 struct vfio_pci_device *vfio_pci_device_init(const char *bdf, int iommu_type)
+2 -32
tools/testing/selftests/vfio/vfio_iommufd_setup_test.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 - #include <assert.h> 3 - #include <dirent.h> 4 - #include <fcntl.h> 5 - 6 2 #include <uapi/linux/types.h> 7 3 #include <linux/limits.h> 8 4 #include <linux/sizes.h> ··· 7 11 8 12 #include <stdint.h> 9 13 #include <stdio.h> 10 - #include <string.h> 11 14 #include <sys/ioctl.h> 12 15 #include <unistd.h> 13 16 ··· 14 19 #include "../kselftest_harness.h" 15 20 16 21 static const char iommu_dev_path[] = "/dev/iommu"; 17 - static char cdev_path[PATH_MAX] = { '\0' }; 18 - 19 - static void set_cdev_path(const char *bdf) 20 - { 21 - char dir_path[PATH_MAX]; 22 - DIR *dir; 23 - struct dirent *entry; 24 - 25 - snprintf(dir_path, sizeof(dir_path), "/sys/bus/pci/devices/%s/vfio-dev/", bdf); 26 - 27 - dir = opendir(dir_path); 28 - assert(dir); 29 - 30 - /* Find the file named "vfio<number>" */ 31 - while ((entry = readdir(dir)) != NULL) { 32 - if (!strncmp("vfio", entry->d_name, 4)) { 33 - snprintf(cdev_path, sizeof(cdev_path), "/dev/vfio/devices/%s", 34 - entry->d_name); 35 - break; 36 - } 37 - } 38 - 39 - assert(strlen(cdev_path) > 0); 40 - 41 - closedir(dir); 42 - } 22 + static const char *cdev_path; 43 23 44 24 static int vfio_device_bind_iommufd_ioctl(int cdev_fd, int iommufd) 45 25 { ··· 120 150 { 121 151 const char *device_bdf = vfio_selftests_get_bdf(&argc, argv); 122 152 123 - set_cdev_path(device_bdf); 153 + cdev_path = vfio_pci_get_cdev_path(device_bdf); 124 154 printf("Using cdev device %s\n", cdev_path); 125 155 126 156 return test_harness_run(argc, argv);