[PATCH v2 3/8] vfio: selftests: Add __sysfs_sriov_numvfs_set()
From: David Matlack
Date: Wed Nov 18 2026 - 19:02:17 EST
Add __sysfs_sriov_numvfs_set(), which returns the error from writing to
a PF's sriov_numvfs file instead of asserting that the write succeeds,
and implement sysfs_sriov_numvfs_set() on top of it.
This will be used in subsequent commits to skip SR-IOV tests when the
kernel returns -ENOENT, which means SR-IOV cannot be configured on the
PF, e.g. because vfio-pci's enable_sriov module parameter is not set.
Signed-off-by: David Matlack <dmatlack@xxxxxxxxxx>
---
.../vfio/lib/include/libvfio/sysfs.h | 1 +
tools/testing/selftests/vfio/lib/sysfs.c | 38 +++++++++++++++----
2 files changed, 31 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h b/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h
index c9ab1ea8f5a9..ede114181cf8 100644
--- a/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h
+++ b/tools/testing/selftests/vfio/lib/include/libvfio/sysfs.h
@@ -4,6 +4,7 @@
int sysfs_sriov_totalvfs_get(const char *bdf);
int sysfs_sriov_numvfs_get(const char *bdf);
+int __sysfs_sriov_numvfs_set(const char *bdf, int numvfs);
void sysfs_sriov_numvfs_set(const char *bdf, int numvfs);
char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i);
int sysfs_iommu_group_get(const char *bdf);
diff --git a/tools/testing/selftests/vfio/lib/sysfs.c b/tools/testing/selftests/vfio/lib/sysfs.c
index d88e348a1e0d..308da0526543 100644
--- a/tools/testing/selftests/vfio/lib/sysfs.c
+++ b/tools/testing/selftests/vfio/lib/sysfs.c
@@ -56,17 +56,24 @@ static int sysfs_val_get_int(const char *component, const char *name,
return ret;
}
-static void sysfs_val_set(const char *component, const char *name,
- const char *file, const char *val)
+static int sysfs_val_set(const char *component, const char *name,
+ const char *file, const char *val)
{
char path[PATH_MAX];
+ ssize_t ret;
int fd;
snprintf_assert(path, PATH_MAX, "/sys/bus/pci/%s/%s/%s", component, name, file);
VFIO_ASSERT_GT(fd = open(path, O_WRONLY), 0);
- VFIO_ASSERT_EQ(write(fd, val, strlen(val)), strlen(val));
+ ret = write(fd, val, strlen(val));
+ if (ret < 0)
+ ret = -errno;
+ else
+ VFIO_ASSERT_EQ(ret, strlen(val));
+
VFIO_ASSERT_EQ(close(fd), 0);
+ return ret < 0 ? ret : 0;
}
static int sysfs_device_val_get(const char *bdf, const char *file)
@@ -74,17 +81,17 @@ static int sysfs_device_val_get(const char *bdf, const char *file)
return sysfs_val_get_int("devices", bdf, file);
}
-static void sysfs_device_val_set(const char *bdf, const char *file, const char *val)
+static int sysfs_device_val_set(const char *bdf, const char *file, const char *val)
{
- sysfs_val_set("devices", bdf, file, val);
+ return sysfs_val_set("devices", bdf, file, val);
}
-static void sysfs_device_val_set_int(const char *bdf, const char *file, int val)
+static int sysfs_device_val_set_int(const char *bdf, const char *file, int val)
{
char val_str[32];
snprintf_assert(val_str, sizeof(val_str), "%d", val);
- sysfs_device_val_set(bdf, file, val_str);
+ return sysfs_device_val_set(bdf, file, val_str);
}
int sysfs_sriov_totalvfs_get(const char *bdf)
@@ -97,9 +104,24 @@ int sysfs_sriov_numvfs_get(const char *bdf)
return sysfs_device_val_get(bdf, "sriov_numvfs");
}
+/**
+ * __sysfs_sriov_numvfs_set() - Set the number of VFs enabled on a PF
+ * @bdf: BDF of the PF.
+ * @numvfs: Number of VFs to enable, or 0 to disable SR-IOV.
+ *
+ * Return: 0 on success, or a negative errno on failure. The kernel returns
+ * -ENOENT if SR-IOV cannot be configured on the PF, e.g. because its driver
+ * does not support configuring SR-IOV or has it disabled (vfio-pci unless its
+ * enable_sriov module parameter is set).
+ */
+int __sysfs_sriov_numvfs_set(const char *bdf, int numvfs)
+{
+ return sysfs_device_val_set_int(bdf, "sriov_numvfs", numvfs);
+}
+
void sysfs_sriov_numvfs_set(const char *bdf, int numvfs)
{
- sysfs_device_val_set_int(bdf, "sriov_numvfs", numvfs);
+ VFIO_ASSERT_EQ(__sysfs_sriov_numvfs_set(bdf, numvfs), 0);
}
char *sysfs_sriov_vf_bdf_get(const char *pf_bdf, int i)
--
2.56.1.708.g77570a6a1d-goog