summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBill Wendling <morbo@google.com>2026-08-27 04:17:30 +0000
committerChristian Brauner <brauner@kernel.org>2026-09-10 09:32:48 +0200
commit501cbbd276b77d34af20ea3dd4f897c93630bac8 (patch)
tree512946dc57a2fc414542b1e6534631680b155f64
parentb4db97b9ef46909d6a8323d8cd04d81ef042e126 (diff)
downloadlinux-next-501cbbd276b77d34af20ea3dd4f897c93630bac8.tar.gz
linux-next-501cbbd276b77d34af20ea3dd4f897c93630bac8.zip
vfs: Add KUnit tests for fdtable
This adds a KUnit test suite for fdtable to verify correct allocation, max_fds initialization, and dynamic object size of the fd array under __counted_by_ptr when CONFIG_CC_HAS_COUNTED_BY_PTR is enabled. Signed-off-by: Bill Wendling <morbo@google.com> Link: https://patch.msgid.link/20260827041732.188707-2-morbo@google.com Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
-rw-r--r--fs/Kconfig8
-rw-r--r--fs/file.c4
-rw-r--r--fs/tests/.kunitconfig2
-rw-r--r--fs/tests/fdtable_kunit.c72
4 files changed, 86 insertions, 0 deletions
diff --git a/fs/Kconfig b/fs/Kconfig
index cf6ae64776e6..f4b9235ab883 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -423,4 +423,12 @@ source "fs/unicode/Kconfig"
config IO_WQ
bool
+config FDTABLE_KUNIT_TEST
+ bool "KUnit test for fdtable" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y
+ default KUNIT_ALL_TESTS
+ help
+ This builds the fdtable KUnit tests, which tests various aspects
+ of the fdtable structure and allocation.
+
endmenu
diff --git a/fs/file.c b/fs/file.c
index 628ca07dc4b1..9c7001b901cf 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -1529,3 +1529,7 @@ int iterate_fd(struct files_struct *files, unsigned n,
return res;
}
EXPORT_SYMBOL(iterate_fd);
+
+#ifdef CONFIG_FDTABLE_KUNIT_TEST
+#include "tests/fdtable_kunit.c"
+#endif
diff --git a/fs/tests/.kunitconfig b/fs/tests/.kunitconfig
new file mode 100644
index 000000000000..de67125a9421
--- /dev/null
+++ b/fs/tests/.kunitconfig
@@ -0,0 +1,2 @@
+CONFIG_KUNIT=y
+CONFIG_FDTABLE_KUNIT_TEST=y
diff --git a/fs/tests/fdtable_kunit.c b/fs/tests/fdtable_kunit.c
new file mode 100644
index 000000000000..6abd2a8d8f5d
--- /dev/null
+++ b/fs/tests/fdtable_kunit.c
@@ -0,0 +1,72 @@
+// SPDX-License-Identifier: GPL-2.0-only
+#include <kunit/test.h>
+#include <linux/fdtable.h>
+#include <linux/file.h>
+
+static void test_alloc_fdtable(struct kunit *test)
+{
+ struct fdtable *fdt;
+ unsigned int slots = 64;
+
+ fdt = alloc_fdtable(slots);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fdt);
+
+ /* Check that max_fds is set correctly and is >= slots */
+ KUNIT_EXPECT_GE(test, fdt->max_fds, slots);
+
+ /* Check that fd is allocated */
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fdt->fd);
+
+ /*
+ * Check dynamic object size of fdt->fd if compiler supports
+ * __counted_by_ptr.
+ */
+#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR
+ KUNIT_EXPECT_EQ(test, __builtin_dynamic_object_size(fdt->fd, 0),
+ fdt->max_fds * sizeof(struct file *));
+#endif
+
+ __free_fdtable(fdt);
+}
+
+static void test_dup_fd(struct kunit *test)
+{
+ struct files_struct *newf;
+ struct fdtable *fdt;
+
+ newf = dup_fd(&init_files, NULL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, newf);
+
+ fdt = rcu_dereference_raw(newf->fdt);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fdt);
+
+ /* Check that max_fds is set correctly and is >= NR_OPEN_DEFAULT */
+ KUNIT_EXPECT_GE(test, fdt->max_fds, NR_OPEN_DEFAULT);
+
+ /* Check that fd is allocated */
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fdt->fd);
+
+ /*
+ * Check dynamic object size of fdt->fd if compiler supports
+ * __counted_by_ptr.
+ */
+#ifdef CONFIG_CC_HAS_COUNTED_BY_PTR
+ KUNIT_EXPECT_EQ(test, __builtin_dynamic_object_size(fdt->fd, 0),
+ fdt->max_fds * sizeof(struct file *));
+#endif
+
+ put_files_struct(newf);
+}
+
+static struct kunit_case fdtable_test_cases[] = {
+ KUNIT_CASE(test_alloc_fdtable),
+ KUNIT_CASE(test_dup_fd),
+ {}
+};
+
+static struct kunit_suite fdtable_test_suite = {
+ .name = "fdtable",
+ .test_cases = fdtable_test_cases,
+};
+
+kunit_test_suite(fdtable_test_suite);