[PATCH i-g-t v3 03/41] lib/vkms: Allow to enable/disable VKMS devices

Louis Chauvet louis.chauvet at bootlin.com
Wed Jul 16 09:22:51 UTC 2025



Le 15/07/2025 à 12:24, José Expósito a écrit :
> Add helper to enable, disable and check the enabled status of a VKMS
> device and add a test to ensure that the default value is correctly set.
> 
> Signed-off-by: José Expósito <jose.exposito89 at gmail.com>

Reviewed-by: Louis Chauvet <louis.chauvet at bootlin.com>

> ---
>   lib/igt_vkms.c             | 98 ++++++++++++++++++++++++++++++++++++++
>   lib/igt_vkms.h             |  7 +++
>   tests/vkms/vkms_configfs.c | 18 +++++++
>   3 files changed, 123 insertions(+)
> 
> diff --git a/lib/igt_vkms.c b/lib/igt_vkms.c
> index fa41f741e..1fd00afd7 100644
> --- a/lib/igt_vkms.c
> +++ b/lib/igt_vkms.c
> @@ -7,7 +7,12 @@
>   
>   #include <dirent.h>
>   #include <errno.h>
> +#include <fcntl.h>
> +#include <stddef.h>
> +#include <stdio.h>
> +#include <stdlib.h>
>   #include <string.h>
> +#include <unistd.h>
>   
>   #include <ftw.h>
>   #include <linux/limits.h>
> @@ -17,6 +22,7 @@
>   #include "igt_vkms.h"
>   
>   #define VKMS_ROOT_DIR_NAME		"vkms"
> +#define VKMS_FILE_ENABLED		"enabled"
>   
>   /**
>    * SECTION:igt_vkms
> @@ -47,6 +53,50 @@ static const char *mount_vkms_configfs(void)
>   	return vkms_root_path;
>   }
>   
> +static int read_int(const char *path)
> +{
> +	FILE *file;
> +	int value;
> +	int ret;
> +
> +	file = fopen(path, "r");
> +	igt_assert_f(file, "Error opening '%s'\n", path);
> +
> +	ret = fscanf(file, "%d", &value);
> +	fclose(file);
> +	igt_assert_f(ret == 1, "Error reading integer from '%s'\n", path);
> +
> +	return value;
> +}
> +
> +static bool read_bool(const char *path)
> +{
> +	int ret;
> +
> +	ret = read_int(path);
> +
> +	return !!ret;
> +}
> +
> +static void write_int(const char *path, int value)
> +{
> +	FILE *file;
> +	int ret;
> +
> +	file = fopen(path, "w");
> +	igt_assert_f(file, "Error opening '%s'\n", path);
> +
> +	ret = fprintf(file, "%d", value);
> +	igt_assert_f(ret >= 0, "Error writing to '%s'\n", path);
> +
> +	fclose(file);
> +}
> +
> +static void write_bool(const char *path, bool value)
> +{
> +	write_int(path, value ? 1 : 0);
> +}
> +
>   /**
>    * igt_require_vkms_configfs:
>    *
> @@ -65,6 +115,22 @@ void igt_require_vkms_configfs(void)
>   		closedir(dir);
>   }
>   
> +/**
> + * igt_vkms_get_device_enabled_path:
> + * @dev: Device to get the enabled path from
> + * @path: Output path
> + * @len: Maximum @path length
> + *
> + * Returns the device "enabled" file path.
> + */
> +void igt_vkms_get_device_enabled_path(igt_vkms_t *dev, char *path, size_t len)
> +{
> +	int ret;
> +
> +	ret = snprintf(path, len, "%s/%s", dev->path, VKMS_FILE_ENABLED);
> +	igt_assert(ret >= 0 && ret < len);
> +}
> +
>   /**
>    * igt_vkms_device_create:
>    * @name: VKMS device name
> @@ -167,6 +233,8 @@ void igt_vkms_device_destroy(igt_vkms_t *dev)
>   
>   	igt_assert(dev);
>   
> +	igt_vkms_device_set_enabled(dev, false);
> +
>   	ret = remove_device_dir(dev);
>   	igt_assert_f(ret == 0,
>   		     "Unable to rmdir device directory '%s'. Got errno=%d (%s)\n",
> @@ -205,3 +273,33 @@ void igt_vkms_destroy_all_devices(void)
>   
>   	closedir(dir);
>   }
> +
> +/**
> + * igt_vkms_device_is_enabled:
> + * @dev: The device to check
> + *
> + * Indicate whether a VKMS device is enabled or not.
> + */
> +bool igt_vkms_device_is_enabled(igt_vkms_t *dev)
> +{
> +	char path[PATH_MAX];
> +
> +	igt_vkms_get_device_enabled_path(dev, path, sizeof(path));
> +
> +	return read_bool(path);
> +}
> +
> +/**
> + * igt_vkms_device_set_enabled:
> + * @dev: Device to enable or disable
> + *
> + * Enable or disable a VKMS device.
> + */
> +void igt_vkms_device_set_enabled(igt_vkms_t *dev, bool enabled)
> +{
> +	char path[PATH_MAX];
> +
> +	igt_vkms_get_device_enabled_path(dev, path, sizeof(path));
> +
> +	write_bool(path, enabled);
> +}
> diff --git a/lib/igt_vkms.h b/lib/igt_vkms.h
> index 48c48f296..d3741b6ee 100644
> --- a/lib/igt_vkms.h
> +++ b/lib/igt_vkms.h
> @@ -8,6 +8,8 @@
>   #ifndef __IGT_VKMS_H__
>   #define __IGT_VKMS_H__
>   
> +#include <stdbool.h>
> +
>   /**
>    * igt_vkms_t:
>    * @path: VKMS root directory inside configfs mounted directory
> @@ -20,8 +22,13 @@ typedef struct igt_vkms {
>   
>   void igt_require_vkms_configfs(void);
>   
> +void igt_vkms_get_device_enabled_path(igt_vkms_t *dev, char *path, size_t len);
> +
>   igt_vkms_t *igt_vkms_device_create(const char *name);
>   void igt_vkms_device_destroy(igt_vkms_t *dev);
>   void igt_vkms_destroy_all_devices(void);
>   
> +bool igt_vkms_device_is_enabled(igt_vkms_t *dev);
> +void igt_vkms_device_set_enabled(igt_vkms_t *dev, bool enabled);
> +
>   #endif /* __IGT_VKMS_H__ */
> diff --git a/tests/vkms/vkms_configfs.c b/tests/vkms/vkms_configfs.c
> index b942cec64..aa7cd9e4e 100644
> --- a/tests/vkms/vkms_configfs.c
> +++ b/tests/vkms/vkms_configfs.c
> @@ -103,6 +103,23 @@ static void test_device_default_files(void)
>   	igt_vkms_device_destroy(dev);
>   }
>   
> +/**
> + * SUBTEST: device-default-values
> + * Description: Check that the default values for the device are correct.
> + */
> +
> +static void test_device_default_values(void)
> +{
> +	igt_vkms_t *dev;
> +
> +	dev = igt_vkms_device_create(__func__);
> +	igt_assert(dev);
> +
> +	igt_assert(!igt_vkms_device_is_enabled(dev));
> +
> +	igt_vkms_device_destroy(dev);
> +}
> +
>   igt_main
>   {
>   	struct {
> @@ -110,6 +127,7 @@ igt_main
>   		void (*fn)(void);
>   	} tests[] = {
>   		{ "device-default-files", test_device_default_files },
> +		{ "device-default-values", test_device_default_values },
>   	};
>   
>   	igt_fixture {

-- 
Louis Chauvet, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com




More information about the igt-dev mailing list