Message ID | 20241105013056.3711427-1-mcgrof@kernel.org (mailing list archive) |
---|---|
State | New |
Headers | show |
Series | selftests/firmware/fw_namespace.c: sanity check on initialization | expand |
On Mon, Nov 04, 2024 at 05:30:56PM -0800, Luis R. Rodriguez wrote: > From: Luis Chamberlain <mcgrof@kernel.org> > > 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") > Signed-off-by: Luis Chamberlain <mcgrof@kernel.org> Reviewed-by: Russ Weight <russ.weight@linux.dev> > --- > .../testing/selftests/firmware/fw_namespace.c | 37 +++++++++++++++++++ > 1 file changed, 37 insertions(+) > > diff --git a/tools/testing/selftests/firmware/fw_namespace.c b/tools/testing/selftests/firmware/fw_namespace.c > index 04757dc7e546..9f4199a54a38 100644 > --- a/tools/testing/selftests/firmware/fw_namespace.c > +++ b/tools/testing/selftests/firmware/fw_namespace.c > @@ -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) > -- > 2.43.0 >
diff --git a/tools/testing/selftests/firmware/fw_namespace.c b/tools/testing/selftests/firmware/fw_namespace.c index 04757dc7e546..9f4199a54a38 100644 --- a/tools/testing/selftests/firmware/fw_namespace.c +++ b/tools/testing/selftests/firmware/fw_namespace.c @@ -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)