[PATCH v1 2/3] list: add kunit test for private list primitives

Pasha Tatashin posted 3 patches 5 days, 4 hours ago
[PATCH v1 2/3] list: add kunit test for private list primitives
Posted by Pasha Tatashin 5 days, 4 hours ago
Add a KUnit test suite for the new private list primitives.

The test defines a struct with a __private list_head and exercises every
macro defined in <linux/list_private.h>.

This ensures that the macros correctly handle the ACCESS_PRIVATE()
abstraction and compile without warnings when acting on private members,
verifying that qualifiers are stripped and offsets are calculated
correctly.

Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
---
 lib/Kconfig.debug             | 14 +++++++
 lib/tests/Makefile            |  1 +
 lib/tests/list-private-test.c | 76 +++++++++++++++++++++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 lib/tests/list-private-test.c

diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 42143686151f..fa9388726182 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -2788,6 +2788,20 @@ config LIST_KUNIT_TEST
 
 	  If unsure, say N.
 
+config LIST_PRIVATE_KUNIT_TEST
+	tristate "KUnit Test for Kernel Private Linked-list structures" if !KUNIT_ALL_TESTS
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This builds the KUnit test for the private linked-list primitives
+	  defined in include/linux/list_private.h.
+
+	  These primitives allow manipulation of list_head members that are
+	  marked as private and require special accessors (ACCESS_PRIVATE)
+	  to strip qualifiers or handle encapsulation.
+
+	  If unsure, say N.
+
 config HASHTABLE_KUNIT_TEST
 	tristate "KUnit Test for Kernel Hashtable structures" if !KUNIT_ALL_TESTS
 	depends on KUNIT
diff --git a/lib/tests/Makefile b/lib/tests/Makefile
index 601dba4b7d96..62a7f115f622 100644
--- a/lib/tests/Makefile
+++ b/lib/tests/Makefile
@@ -25,6 +25,7 @@ obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o
 obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o
 obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
 obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
+obj-$(CONFIG_LIST_PRIVATE_KUNIT_TEST) += list-private-test.o
 obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o
 obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
 obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
diff --git a/lib/tests/list-private-test.c b/lib/tests/list-private-test.c
new file mode 100644
index 000000000000..3bd62939ae67
--- /dev/null
+++ b/lib/tests/list-private-test.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit compilation/smoke test for Private list primitives.
+ *
+ * Copyright (c) 2025, Google LLC.
+ * Pasha Tatashin <pasha.tatashin@soleen.com>
+ */
+#include <linux/list_private.h>
+#include <kunit/test.h>
+
+/*
+ * This forces compiler to warn if you access it directly, because list
+ * primitives expect (struct list_head *), not (volatile struct list_head *).
+ */
+#undef __private
+#define __private volatile
+
+/* Redefine ACCESS_PRIVATE for this test. */
+#undef ACCESS_PRIVATE
+#define ACCESS_PRIVATE(p, member) \
+	(*((struct list_head *)((unsigned long)&((p)->member))))
+
+struct list_test_struct {
+	int data;
+	struct list_head __private list;
+};
+
+static void list_private_compile_test(struct kunit *test)
+{
+	struct list_test_struct entry;
+	struct list_test_struct *pos, *n;
+	LIST_HEAD(head);
+
+	INIT_LIST_HEAD(&ACCESS_PRIVATE(&entry, list));
+	list_add(&ACCESS_PRIVATE(&entry, list), &head);
+	pos = &entry;
+
+	pos = list_private_entry(&ACCESS_PRIVATE(&entry, list), struct list_test_struct, list);
+	pos = list_private_first_entry(&head, struct list_test_struct, list);
+	pos = list_private_last_entry(&head, struct list_test_struct, list);
+	pos = list_private_next_entry(pos, list);
+	pos = list_private_prev_entry(pos, list);
+	pos = list_private_next_entry_circular(pos, &head, list);
+	pos = list_private_prev_entry_circular(pos, &head, list);
+
+	if (list_private_entry_is_head(pos, &head, list))
+		return;
+
+	list_private_for_each_entry(pos, &head, list) { }
+	list_private_for_each_entry_reverse(pos, &head, list) { }
+	list_private_for_each_entry_continue(pos, &head, list) { }
+	list_private_for_each_entry_continue_reverse(pos, &head, list) { }
+	list_private_for_each_entry_from(pos, &head, list) { }
+	list_private_for_each_entry_from_reverse(pos, &head, list) { }
+
+	list_private_for_each_entry_safe(pos, n, &head, list)
+		list_private_safe_reset_next(pos, n, list);
+	list_private_for_each_entry_safe_continue(pos, n, &head, list) { }
+	list_private_for_each_entry_safe_from(pos, n, &head, list) { }
+	list_private_for_each_entry_safe_reverse(pos, n, &head, list) { }
+}
+
+static struct kunit_case list_private_test_cases[] = {
+	KUNIT_CASE(list_private_compile_test),
+	{},
+};
+
+static struct kunit_suite list_private_test_module = {
+	.name = "list-private-kunit-test",
+	.test_cases = list_private_test_cases,
+};
+
+kunit_test_suite(list_private_test_module);
+
+MODULE_DESCRIPTION("KUnit compilation test for private list primitives");
+MODULE_LICENSE("GPL");
-- 
2.52.0.487.g5c8c507ade-goog
Re: [PATCH v1 2/3] list: add kunit test for private list primitives
Posted by David Gow 2 days, 15 hours ago
On Thu, 27 Nov 2025 at 02:57, Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
>
> Add a KUnit test suite for the new private list primitives.
>
> The test defines a struct with a __private list_head and exercises every
> macro defined in <linux/list_private.h>.
>
> This ensures that the macros correctly handle the ACCESS_PRIVATE()
> abstraction and compile without warnings when acting on private members,
> verifying that qualifiers are stripped and offsets are calculated
> correctly.
>
> Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> ---

Thanks -- I'm glad to see tests added with this!

I'd be okay with this living in the same config option as the other
list tests (LIST_KUNIT_TEST) if you'd prefer, but don't particularly
mind either way.

Reviewed-by: David Gow <davidgow@google.com>

Cheers,
-- David

>  lib/Kconfig.debug             | 14 +++++++
>  lib/tests/Makefile            |  1 +
>  lib/tests/list-private-test.c | 76 +++++++++++++++++++++++++++++++++++
>  3 files changed, 91 insertions(+)
>  create mode 100644 lib/tests/list-private-test.c
>
> diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
> index 42143686151f..fa9388726182 100644
> --- a/lib/Kconfig.debug
> +++ b/lib/Kconfig.debug
> @@ -2788,6 +2788,20 @@ config LIST_KUNIT_TEST
>
>           If unsure, say N.
>
> +config LIST_PRIVATE_KUNIT_TEST
> +       tristate "KUnit Test for Kernel Private Linked-list structures" if !KUNIT_ALL_TESTS
> +       depends on KUNIT
> +       default KUNIT_ALL_TESTS
> +       help
> +         This builds the KUnit test for the private linked-list primitives
> +         defined in include/linux/list_private.h.
> +
> +         These primitives allow manipulation of list_head members that are
> +         marked as private and require special accessors (ACCESS_PRIVATE)
> +         to strip qualifiers or handle encapsulation.
> +
> +         If unsure, say N.
> +
>  config HASHTABLE_KUNIT_TEST
>         tristate "KUnit Test for Kernel Hashtable structures" if !KUNIT_ALL_TESTS
>         depends on KUNIT
> diff --git a/lib/tests/Makefile b/lib/tests/Makefile
> index 601dba4b7d96..62a7f115f622 100644
> --- a/lib/tests/Makefile
> +++ b/lib/tests/Makefile
> @@ -25,6 +25,7 @@ obj-$(CONFIG_TEST_IOV_ITER) += kunit_iov_iter.o
>  obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o
>  obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
>  obj-$(CONFIG_LIST_KUNIT_TEST) += list-test.o
> +obj-$(CONFIG_LIST_PRIVATE_KUNIT_TEST) += list-private-test.o
>  obj-$(CONFIG_KFIFO_KUNIT_TEST) += kfifo_kunit.o
>  obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
>  obj-$(CONFIG_LINEAR_RANGES_TEST) += test_linear_ranges.o
> diff --git a/lib/tests/list-private-test.c b/lib/tests/list-private-test.c
> new file mode 100644
> index 000000000000..3bd62939ae67
> --- /dev/null
> +++ b/lib/tests/list-private-test.c
> @@ -0,0 +1,76 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * KUnit compilation/smoke test for Private list primitives.
> + *
> + * Copyright (c) 2025, Google LLC.
> + * Pasha Tatashin <pasha.tatashin@soleen.com>
> + */
> +#include <linux/list_private.h>
> +#include <kunit/test.h>
> +
> +/*
> + * This forces compiler to warn if you access it directly, because list
> + * primitives expect (struct list_head *), not (volatile struct list_head *).
> + */
> +#undef __private
> +#define __private volatile
> +
> +/* Redefine ACCESS_PRIVATE for this test. */
> +#undef ACCESS_PRIVATE
> +#define ACCESS_PRIVATE(p, member) \
> +       (*((struct list_head *)((unsigned long)&((p)->member))))
> +
> +struct list_test_struct {
> +       int data;
> +       struct list_head __private list;
> +};
> +
> +static void list_private_compile_test(struct kunit *test)
> +{
> +       struct list_test_struct entry;
> +       struct list_test_struct *pos, *n;
> +       LIST_HEAD(head);
> +
> +       INIT_LIST_HEAD(&ACCESS_PRIVATE(&entry, list));
> +       list_add(&ACCESS_PRIVATE(&entry, list), &head);
> +       pos = &entry;
> +
> +       pos = list_private_entry(&ACCESS_PRIVATE(&entry, list), struct list_test_struct, list);
> +       pos = list_private_first_entry(&head, struct list_test_struct, list);
> +       pos = list_private_last_entry(&head, struct list_test_struct, list);
> +       pos = list_private_next_entry(pos, list);
> +       pos = list_private_prev_entry(pos, list);
> +       pos = list_private_next_entry_circular(pos, &head, list);
> +       pos = list_private_prev_entry_circular(pos, &head, list);
> +
> +       if (list_private_entry_is_head(pos, &head, list))
> +               return;
> +
> +       list_private_for_each_entry(pos, &head, list) { }
> +       list_private_for_each_entry_reverse(pos, &head, list) { }
> +       list_private_for_each_entry_continue(pos, &head, list) { }
> +       list_private_for_each_entry_continue_reverse(pos, &head, list) { }
> +       list_private_for_each_entry_from(pos, &head, list) { }
> +       list_private_for_each_entry_from_reverse(pos, &head, list) { }
> +
> +       list_private_for_each_entry_safe(pos, n, &head, list)
> +               list_private_safe_reset_next(pos, n, list);
> +       list_private_for_each_entry_safe_continue(pos, n, &head, list) { }
> +       list_private_for_each_entry_safe_from(pos, n, &head, list) { }
> +       list_private_for_each_entry_safe_reverse(pos, n, &head, list) { }
> +}
> +
> +static struct kunit_case list_private_test_cases[] = {
> +       KUNIT_CASE(list_private_compile_test),
> +       {},
> +};
> +
> +static struct kunit_suite list_private_test_module = {
> +       .name = "list-private-kunit-test",
> +       .test_cases = list_private_test_cases,
> +};
> +
> +kunit_test_suite(list_private_test_module);
> +
> +MODULE_DESCRIPTION("KUnit compilation test for private list primitives");
> +MODULE_LICENSE("GPL");
> --
> 2.52.0.487.g5c8c507ade-goog
>
Re: [PATCH v1 2/3] list: add kunit test for private list primitives
Posted by Pasha Tatashin 2 days, 3 hours ago
On Sat, Nov 29, 2025 at 2:47 AM David Gow <davidgow@google.com> wrote:
>
> On Thu, 27 Nov 2025 at 02:57, Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
> >
> > Add a KUnit test suite for the new private list primitives.
> >
> > The test defines a struct with a __private list_head and exercises every
> > macro defined in <linux/list_private.h>.
> >
> > This ensures that the macros correctly handle the ACCESS_PRIVATE()
> > abstraction and compile without warnings when acting on private members,
> > verifying that qualifiers are stripped and offsets are calculated
> > correctly.
> >
> > Signed-off-by: Pasha Tatashin <pasha.tatashin@soleen.com>
> > ---
>
> Thanks -- I'm glad to see tests added with this!
>
> I'd be okay with this living in the same config option as the other
> list tests (LIST_KUNIT_TEST) if you'd prefer, but don't particularly
> mind either way.

I prefer to keep them separate just because they work differently. The
other list tests emphasis on runtime correctness, and here emphasizes
on verifying ACCESS_PRIVATE() does not generate compiler warnings.

> Reviewed-by: David Gow <davidgow@google.com>

Thanks!

Pasha