[igt-dev] [PATCH i-g-t] i915/sysfs: Verify .defaults are read-only and match their namesakes

Chris Wilson chris at chris-wilson.co.uk
Thu Oct 15 08:36:32 UTC 2020


Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
---
 tests/Makefile.sources      |   3 +
 tests/i915/sysfs_defaults.c | 110 ++++++++++++++++++++++++++++++++++++
 tests/meson.build           |   1 +
 3 files changed, 114 insertions(+)
 create mode 100644 tests/i915/sysfs_defaults.c

diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index ec535ba85..2581c0429 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -111,6 +111,9 @@ TESTS_progs = \
 TESTS_progs += api_intel_bb
 api_intel_bb_SOURCES = i915/api_intel_bb.c
 
+TESTS_progs += sysfs_defaults
+sysfs_defaults_SOURCES = i915/sysfs_defaults.c
+
 TESTS_progs += sysfs_heartbeat_interval
 sysfs_heartbeat_interval_SOURCES = i915/sysfs_heartbeat_interval.c
 
diff --git a/tests/i915/sysfs_defaults.c b/tests/i915/sysfs_defaults.c
new file mode 100644
index 000000000..64c5eb373
--- /dev/null
+++ b/tests/i915/sysfs_defaults.c
@@ -0,0 +1,110 @@
+/*
+ * Copyright © 2019 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include "drmtest.h"
+#include "i915/gem.h"
+#include "i915/gem_engine_topology.h"
+#include "igt_sysfs.h"
+
+static bool may_write(int dir, const char *file)
+{
+	struct stat st;
+
+	igt_assert(fstatat(dir, file, &st, 0) == 0);
+	return st.st_mode & 0222;
+}
+
+static void test_writable(int i915, int engine)
+{
+	struct dirent *de;
+	int defaults;
+	DIR *dir;
+
+	defaults = openat(engine, ".defaults", O_DIRECTORY);
+	igt_require(defaults != -1);
+
+	dir = fdopendir(engine);
+	while ((de = readdir(dir))) {
+		if (!(de->d_type & DT_REG))
+			continue;
+
+		if (!may_write(engine, de->d_name)) {
+			igt_debug("Skipping constant attr '%s'\n", de->d_name);
+			continue;
+		}
+
+		igt_debug("Checking attr '%s'\n", de->d_name);
+
+		/* Every attribute should have a default value */
+		igt_assert_f(faccessat(defaults, de->d_name, F_OK, 0) == 0,
+			     "default value for %s not accessible\n",
+			     de->d_name);
+
+		/* But no one is allowed to change it */
+		igt_assert_f(!may_write(defaults, de->d_name),
+			     "default value for %s writable!\n",
+			     de->d_name);
+
+		igt_assert_f(!igt_sysfs_set(defaults, de->d_name, "garbage"),
+			     "write into default value of %s succeeded!\n",
+			     de->d_name);
+	}
+	closedir(dir);
+}
+
+igt_main
+{
+	int i915 = -1, engines = -1;
+
+	igt_fixture {
+		int sys;
+
+		i915 = drm_open_driver(DRIVER_INTEL);
+		igt_require_gem(i915);
+		igt_allow_hang(i915, 0, 0);
+
+		sys = igt_sysfs_open(i915);
+		igt_require(sys != -1);
+
+		engines = openat(sys, "engine", O_RDONLY);
+		igt_require(engines != -1);
+
+		close(sys);
+	}
+
+	igt_subtest_with_dynamic("writable")
+		dyn_sysfs_engines(i915, engines, NULL, test_writable);
+
+	igt_fixture {
+		close(engines);
+		close(i915);
+	}
+}
diff --git a/tests/meson.build b/tests/meson.build
index 08200f26c..4f765da30 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -235,6 +235,7 @@ i915_progs = [
 	'i915_query',
 	'i915_selftest',
 	'i915_suspend',
+	'sysfs_defaults',
 	'sysfs_heartbeat_interval',
 	'sysfs_preempt_timeout',
 	'sysfs_timeslice_duration',
-- 
2.28.0



More information about the igt-dev mailing list