Skip to contentSkip to conversation

selftests/firmware/fw_namespace.c: sanity check on initialization

PATCH In review Luis R. Rodriguez 2 participants 2 messages Nov 5, 2024 – Nov 7, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/firmware/fw_namespace.c: sanity check on initialization

From: Luis Chamberlain <[email protected]>

The fw_namespace.c test runs in a pretty self contained environment. It can easily fail with false positive if the DUT does not have the /lib/firmware directory created though, and CI tests will use minimal guests which may not have the directory created. Although this can be fixed by the test runners, it is also easy to just ensure the directory is created by the test itself.

While at it, clarify that the test is expected to run in the same namespace as the first process, this will save folks trying to debug this test some time in terms of context. The mounted tmpfs later will use the same init namespace for some temporary testing for this test.

Fixes 901cff7cb9614 ("firmware_loader: load files from the mount namespace of init")
---
 .../testing/selftests/firmware/fw_namespace.c | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
tools/testing/selftests/firmware/fw_namespace.c +37 -0
index 04757dc7e546..9f4199a54a38 100644
@@ -112,6 +112,40 @@ static bool test_fw_in_ns(const char *fw_name, const char *sys_path, bool block_
 	exit(EXIT_SUCCESS);
 }
 
+static void verify_init_ns(void)
+{
+    struct stat init_ns, self_ns;
+
+    if (stat("/proc/1/ns/mnt", &init_ns) != 0)
+        die("Failed to stat init mount namespace: %s\n",
+            strerror(errno));
+
+    if (stat("/proc/self/ns/mnt", &self_ns) != 0)
+        die("Failed to stat self mount namespace: %s\n",
+            strerror(errno));
+
+    if (init_ns.st_ino != self_ns.st_ino)
+        die("Test must run in init mount namespace\n");
+}
+
+static void ensure_firmware_dir(void)
+{
+    struct stat st;
+
+    if (stat("/lib/firmware", &st) == 0) {
+        if (!S_ISDIR(st.st_mode))
+            die("/lib/firmware exists but is not a directory\n");
+        return;
+    }
+
+    if (errno != ENOENT)
+        die("Failed to stat /lib/firmware: %s\n", strerror(errno));
+
+    if (mkdir("/lib/firmware", 0755) != 0)
+        die("Failed to create /lib/firmware directory: %s\n",
+            strerror(errno));
+}
+
 int main(int argc, char **argv)
 {
 	const char *fw_name = "test-firmware.bin";
@@ -119,6 +153,9 @@ int main(int argc, char **argv)
 	if (argc != 2)
 		die("usage: %s sys_path\n", argv[0]);
 
+	verify_init_ns();
+	ensure_firmware_dir();
+
 	/* Mount tmpfs to /lib/firmware so we don't have to assume
 	   that it is writable for us.*/
 	if (mount("test", "/lib/firmware", "tmpfs", 0, NULL) == -1)
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 9 lines · Luis R. Rodriguez

From: Luis Chamberlain <[email protected]>

The fw_namespace.c test runs in a pretty self contained environment. It can easily fail with false positive if the DUT does not have the /lib/firmware directory created though, and CI tests will use minimal guests which may not have the directory created. Although this can be fixed by the test runners, it is also easy to just ensure the directory is created by the test itself.

Quoted 7 lines

While at it, clarify that the test is expected to run in the same namespace as the first process, this will save folks trying to debug this test some time in terms of context. The mounted tmpfs later will use the same init namespace for some temporary testing for this test.

Fixes 901cff7cb9614 ("firmware_loader: load files from the mount namespace of init")
Quoted 62 lines
---
 .../testing/selftests/firmware/fw_namespace.c | 37 +++++++++++++++++++
 1 file changed, 37 insertions(+)
tools/testing/selftests/firmware/fw_namespace.c +37 -0
index 04757dc7e546..9f4199a54a38 100644
@@ -112,6 +112,40 @@ static bool test_fw_in_ns(const char *fw_name, const char *sys_path, bool block_
 	exit(EXIT_SUCCESS);
 }
 
+static void verify_init_ns(void)
+{
+    struct stat init_ns, self_ns;
+
+    if (stat("/proc/1/ns/mnt", &init_ns) != 0)
+        die("Failed to stat init mount namespace: %s\n",
+            strerror(errno));
+
+    if (stat("/proc/self/ns/mnt", &self_ns) != 0)
+        die("Failed to stat self mount namespace: %s\n",
+            strerror(errno));
+
+    if (init_ns.st_ino != self_ns.st_ino)
+        die("Test must run in init mount namespace\n");
+}
+
+static void ensure_firmware_dir(void)
+{
+    struct stat st;
+
+    if (stat("/lib/firmware", &st) == 0) {
+        if (!S_ISDIR(st.st_mode))
+            die("/lib/firmware exists but is not a directory\n");
+        return;
+    }
+
+    if (errno != ENOENT)
+        die("Failed to stat /lib/firmware: %s\n", strerror(errno));
+
+    if (mkdir("/lib/firmware", 0755) != 0)
+        die("Failed to create /lib/firmware directory: %s\n",
+            strerror(errno));
+}
+
 int main(int argc, char **argv)
 {
 	const char *fw_name = "test-firmware.bin";
@@ -119,6 +153,9 @@ int main(int argc, char **argv)
 	if (argc != 2)
 		die("usage: %s sys_path\n", argv[0]);
 
+	verify_init_ns();
+	ensure_firmware_dir();
+
 	/* Mount tmpfs to /lib/firmware so we don't have to assume
 	   that it is writable for us.*/
 	if (mount("test", "/lib/firmware", "tmpfs", 0, NULL) == -1)
Signature · 1 line
2.43.0