kunit: use scoped_with_init_fs() in tests that resolve paths
Subsystems (2)
Christian Brauner
maintainerPATCH 1/1 kunit: use scoped_with_init_fs() in tests that resolve paths
KUnit runs each test in a kthread (kunit_generic_run_threadfn_adapter()). Since commit 32750c77e811 ("fs: start all kthreads in nullfs") every kthread starts out with its fs_struct root and pwd pointing at an empty, immutable nullfs and can no longer resolve paths.
Two test suites resolve paths from test context and now fail. The misc minor tests create a device node and open it:
# miscdev_test_static_basic: EXPECTATION FAILED at drivers/char/misc_minor_kunit.c:166 failed to create node
# miscdev_test_static_basic: EXPECTATION FAILED at drivers/char/misc_minor_kunit.c:170 failed to open misc device: -2
init_mknod("/dev/...") resolves relative to current->fs, which is now nullfs with no /dev, so node creation returns -ENOENT and the following filp_open() fails with -ENOENT (-2). The initramfs tests hit the same
wall: they call unpack_to_rootfs() (init_mkdir()/init_mknod() under the hood) and then verify with relative-name init_stat()/init_unlink()/ filp_open(), all of which resolve against the nullfs root and pwd.
Wrap the path-resolving regions of both suites in scoped_with_init_fs(), which borrows the userspace init fs_struct (set up by init_userspace_fs() before kunit_run_all_tests()) for the duration. This is the same opt-in the rest of the tree uses for kthread path resolution. Doing it per test rather than blanket-wrapping the KUnit runner keeps the nullfs isolation in place for the many tests that should never touch the filesystem.
--- drivers/char/misc_minor_kunit.c | 25 +++-- init/initramfs_test.c | 216 +++++++++++++++++++++++----------------- 2 files changed, 137 insertions(+), 104 deletions(-)
drivers/char/misc_minor_kunit.c +15 -10
@@ -5,6 +5,7 @@#include <linux/miscdevice.h> #include <linux/fs.h> #include <linux/file.h> +#include <linux/fs_struct.h> #include <linux/init_syscalls.h> /* static minor (LCD_MINOR) */
@@ -160,18 +161,22 @@ static void __init miscdev_test_can_open(struct kunit *test, struct miscdevice *char *devname; devname =kasprintf (GFP_KERNEL ,"/dev/%s" , misc->name); - ret =init_mknod (devname,S_IFCHR |0600 , -new_encode_dev (MKDEV (MISC_MAJOR , misc->minor))); -if (ret !=0 ) -KUNIT_FAIL (test,"failed to create node\n" ); - filp =filp_open (devname,O_RDONLY ,0 ); -if (IS_ERR (filp)) -KUNIT_FAIL (test,"failed to open misc device: %ld\n" ,PTR_ERR (filp)); -else -fput (filp); +/* Tests run in a nullfs kthread; borrow the init fs to resolve /dev. */ + scoped_with_init_fs() { + ret =init_mknod (devname,S_IFCHR |0600 , +new_encode_dev (MKDEV (MISC_MAJOR , misc->minor))); +if (ret !=0 ) +KUNIT_FAIL (test,"failed to create node\n" ); + + filp =filp_open (devname,O_RDONLY ,0 ); +if (IS_ERR (filp)) +KUNIT_FAIL (test,"failed to open misc device: %ld\n" ,PTR_ERR (filp)); +else +fput (filp); -init_unlink (devname); +init_unlink (devname); + }kfree (devname); }
init/initramfs_test.c +122 -94
293 lines not on this page · raw message
---
base-commit: 1b8a585da1b50b3db0e7bbd073ca274875539ea8
change-id: 20260701-work-kunit-nullfs-e47e4bf270e9
Quoted 1 line · Christian Brauner
kunit: use
scoped_with_init_fs()in tests that resolve paths
Applied to the vfs-7.3.kthread branch of the vfs/vfs.git tree. Patches in the vfs-7.3.kthread branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs-7.3.kthread
[1/1] kunit: use scoped_with_init_fs() in tests that resolve paths
https://git.kernel.org/vfs/vfs/c/804dd204728c
Hi Christian,
Sorry for the late response, I missed this...
Quoted 1 line · Christian Brauner
KUnit runs each test in a kthread (
kunit_generic_run_threadfn_adapter()).
...
Quoted 11 lines
The initramfs tests hit the same
wall: they callunpack_to_rootfs()(init_mkdir()/init_mknod() under the hood) and then verify with relative-name init_stat()/init_unlink()/filp_open(), all of which resolve against the nullfs root and pwd.Wrap the path-resolving regions of both suites in
scoped_with_init_fs(), which borrows the userspace initfs_struct(set up byinit_userspace_fs()beforekunit_run_all_tests()) for the duration. This is the same opt-in the rest of the tree uses for kthread path resolution. Doing it per test rather than blanket-wrapping the KUnit runner keeps the nullfs isolation in place for the many tests that should never touch the filesystem.
Doing it per-test certainly makes sense, but for initramfs I think it'd be a little cleaner to just use the existing per-test init() callback instead. I.e.:
init/initramfs_test.c +17 -2
@@ -3,6 +3,7 @@#include <linux/fcntl.h> #include <linux/file.h> #include <linux/fs.h> +#include <linux/fs_struct.h> #include <linux/init.h> #include <linux/init_syscalls.h> #include <linux/initrd.h>
@@ -562,7 +563,7 @@ static struct kunit_case __refdata initramfs_test_cases[] = { {}, }; -static int __init initramfs_test_init (struct kunit_suite *suite) +static int __init initramfs_suite_init (struct kunit_suite *suite) {/* * unpack_to_rootfs() uses module-static state (victim, byte_count,
@@ -574,9 +575,23 @@ static int __init initramfs_test_init(struct kunit_suite *suite)return 0 ; } +/* Tests run in a nullfs kthread; always use the init fs for path resolution. */ +static int __init initramfs_test_init (struct kunit *test) +{ + test->priv =__override_init_fs (); +return 0 ; +} + +static void __init initramfs_test_exit (struct kunit *test) +{ +__revert_init_fs (test->priv); +} +static struct kunit_suite __refdata initramfs_test_suite = { .name ="initramfs" , - .suite_init = initramfs_test_init, + .suite_init = initramfs_suite_init, + .init = initramfs_test_init, + .exit = initramfs_test_exit, .test_cases = initramfs_test_cases, };kunit_test_init_section_suites (&initramfs_test_suite);
What do you think?
Cheers, David