* [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update
@ 2026-11-26 22:34 Andrey Zakharov
2026-11-27 8:12 ` David Gow
2026-12-04 9:43 ` Andrey Zakharov
0 siblings, 2 replies; 5+ messages in thread
From: Andrey Zakharov @ 2026-11-26 22:34 UTC (permalink / raw)
To: tglx, mingo, bp, dave.hansen, x86
Cc: hpa, david, raemoar63, linux-kselftest, kunit-dev, linux-kernel,
Andrey Zakharov
There are no KUnit tests under arch/x86, and I could not find any other
tests of the E820 table code. e820__update_table() sorts a table, resolves
overlaps in favour of the higher type, merges neighbours of the same
type and stops when the table is full. e820__range_update_table()
changes the type of a range and may split an entry. Both take the table
as an argument, so they can be tested on a table of the test's own,
without touching the memory map of the running kernel.
Add 20 test cases. For e820__update_table(): too few entries and an
entry that wraps around (both return -1 and leave the table alone),
unsorted input, nested and partial overlaps, merging of equal types,
types that must not be merged (PRAM and SOFT_RESERVED), zero-size
entries, the sample map from the comment above the function, and a
full table. For e820__range_update_table(): a range covering an entry,
splitting an entry, head and tail, no match, a size that would wrap
around, and several entries at once.
Both functions are __init, so the suite is registered with
kunit_test_init_section_suite() and runs during boot, before init
memory is freed. It can only be built in: X86_E820_KUNIT_TEST depends
on KUNIT=y.
Tested: built with gcc 16.2.1 and run with kunit.py (CONFIG_KUNIT=y and
CONFIG_X86_E820_KUNIT_TEST=y) under QEMU (TCG, on top of
next-20261126): 20 of 20 pass, and 20 of 20 with --arch=i386. To
check that the tests can fail, I
changed e820.c temporarily in three ways, one at a time: always
starting a new entry at a change point (4 tests fail), setting the
wrong new start when cutting the head of an entry in
__e820__range_update() (1 fails), and swapping the order of range
start and end at equal addresses in cpcompare() (1 fails). None of
these changes is part of this patch.
Not tested: a full build with KUNIT_ALL_TESTS (I only checked that it
selects the option), other values of MAX_NUMNODES, real hardware.
Signed-off-by: Andrey Zakharov <dedemorow@gmail.com>
---
arch/x86/Kconfig.debug | 15 ++
arch/x86/kernel/Makefile | 1 +
arch/x86/kernel/e820_kunit.c | 368 +++++++++++++++++++++++++++++++++++
3 files changed, 384 insertions(+)
create mode 100644 arch/x86/kernel/e820_kunit.c
diff --git a/arch/x86/Kconfig.debug b/arch/x86/Kconfig.debug
index c95c3aaadf97..657e645a9bb0 100644
--- a/arch/x86/Kconfig.debug
+++ b/arch/x86/Kconfig.debug
@@ -110,6 +110,21 @@ config IOMMU_LEAK
config HAVE_MMIOTRACE_SUPPORT
def_bool y
+config X86_E820_KUNIT_TEST
+ bool "KUnit tests for the x86 E820 memory map code" if !KUNIT_ALL_TESTS
+ depends on KUNIT=y
+ default KUNIT_ALL_TESTS
+ help
+ Run KUnit tests for e820__update_table() and
+ e820__range_update_table() during boot. The tests work on
+ tables of their own and do not change the memory map of the
+ running kernel.
+
+ For more information on KUnit and unit tests in general please
+ refer to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+ If unsure, say N.
+
config X86_DECODER_SELFTEST
bool "x86 instruction decoder selftest"
depends on DEBUG_KERNEL && INSTRUCTION_DECODER
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 702536938433..6930495fdf32 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -82,6 +82,7 @@ obj-$(CONFIG_X86_64) += sys_x86_64.o
obj-$(CONFIG_X86_ESPFIX64) += espfix_64.o
obj-$(CONFIG_SYSFS) += ksysfs.o
obj-y += bootflag.o e820.o
+obj-$(CONFIG_X86_E820_KUNIT_TEST) += e820_kunit.o
obj-y += pci-dma.o quirks.o kdebugfs.o
obj-y += alternative.o i8253.o hw_breakpoint.o
obj-y += tsc.o tsc_msr.o io_delay.o rtc.o
diff --git a/arch/x86/kernel/e820_kunit.c b/arch/x86/kernel/e820_kunit.c
new file mode 100644
index 000000000000..efd0323bcfc8
--- /dev/null
+++ b/arch/x86/kernel/e820_kunit.c
@@ -0,0 +1,368 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for the E820 memory map code.
+ *
+ * e820__update_table() and e820__range_update_table() are __init functions
+ * that work on a table passed by the caller. The tests therefore run during
+ * boot, before the init sections are freed, and never touch the real map.
+ *
+ * The layouts are written in units of one 4K page.
+ */
+#include <kunit/test.h>
+#include <linux/init.h>
+#include <linux/limits.h>
+#include <linux/sizes.h>
+#include <linux/string.h>
+
+#include <asm/e820/api.h>
+#include <asm/e820/types.h>
+
+#define PG(n) ((u64)(n) * SZ_4K)
+#define E(a, s, t) { .addr = PG(a), .size = PG(s), .type = E820_TYPE_##t }
+#define CHECK_UPDATE(test, in, exp) \
+ check_update(test, in, ARRAY_SIZE(in), exp, ARRAY_SIZE(exp))
+
+static struct e820_table *__init new_table(struct kunit *test, const struct e820_entry *e, u32 n)
+{
+ struct e820_table *t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL);
+
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, t);
+ KUNIT_ASSERT_LE(test, n, (u32)E820_MAX_ENTRIES);
+ if (n)
+ memcpy(t->entries, e, n * sizeof(*e));
+ t->nr_entries = n;
+ return t;
+}
+
+static void __init expect_table(struct kunit *test, const struct e820_table *t,
+ const struct e820_entry *e, u32 n)
+{
+ u32 i;
+
+ KUNIT_ASSERT_EQ(test, t->nr_entries, n);
+ for (i = 0; i < n; i++) {
+ KUNIT_EXPECT_EQ_MSG(test, t->entries[i].addr, e[i].addr, "entry %u addr", i);
+ KUNIT_EXPECT_EQ_MSG(test, t->entries[i].size, e[i].size, "entry %u size", i);
+ KUNIT_EXPECT_EQ_MSG(test, (u32)t->entries[i].type, (u32)e[i].type,
+ "entry %u type", i);
+ }
+}
+
+static void __init check_update(struct kunit *test,
+ const struct e820_entry *in, u32 n_in,
+ const struct e820_entry *exp, u32 n_exp)
+{
+ struct e820_table *t = new_table(test, in, n_in);
+
+ KUNIT_EXPECT_EQ(test, e820__update_table(t), 0);
+ expect_table(test, t, exp, n_exp);
+}
+
+static void __init check_range_update(struct kunit *test,
+ const struct e820_entry *in, u32 n_in, u64 start, u64 size,
+ enum e820_type old_type, enum e820_type new_type, u64 ret,
+ const struct e820_entry *exp, u32 n_exp)
+{
+ struct e820_table *t = new_table(test, in, n_in);
+
+ KUNIT_EXPECT_EQ(test, e820__range_update_table(t, start, size, old_type, new_type), ret);
+ expect_table(test, t, exp, n_exp);
+}
+
+/* One entry, or none: nothing to sanitize. The table must stay as it was. */
+static void __init update_table_too_few(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 4, RAM) };
+ struct e820_table *t = new_table(test, in, ARRAY_SIZE(in));
+
+ KUNIT_EXPECT_EQ(test, e820__update_table(t), -1);
+ expect_table(test, t, in, ARRAY_SIZE(in));
+
+ t = new_table(test, NULL, 0);
+ KUNIT_EXPECT_EQ(test, e820__update_table(t), -1);
+ KUNIT_EXPECT_EQ(test, t->nr_entries, 0);
+}
+
+/* An entry that wraps around through zero makes the whole table invalid. */
+static void __init update_table_overflow(struct kunit *test)
+{
+ static const struct e820_entry in[] = {
+ E(0, 1, RAM),
+ { .addr = ULLONG_MAX - 0xfff, .size = SZ_8K, .type = E820_TYPE_RESERVED },
+ };
+ struct e820_table *t = new_table(test, in, ARRAY_SIZE(in));
+
+ KUNIT_EXPECT_EQ(test, e820__update_table(t), -1);
+ expect_table(test, t, in, ARRAY_SIZE(in));
+}
+
+static void __init update_table_unsorted_disjoint(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(4, 1, ACPI), E(0, 1, RESERVED), E(2, 1, RAM) };
+ static const struct e820_entry exp[] = { E(0, 1, RESERVED), E(2, 1, RAM), E(4, 1, ACPI) };
+
+ CHECK_UPDATE(test, in, exp);
+}
+
+/* A higher type inside a lower one splits it in three. */
+static void __init update_table_nested_higher_type(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 4, RAM), E(1, 1, RESERVED) };
+ static const struct e820_entry exp[] = { E(0, 1, RAM), E(1, 1, RESERVED), E(2, 2, RAM) };
+
+ CHECK_UPDATE(test, in, exp);
+}
+
+static void __init update_table_partial_overlap(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 2, RAM), E(1, 2, ACPI) };
+ static const struct e820_entry exp[] = { E(0, 1, RAM), E(1, 2, ACPI) };
+
+ CHECK_UPDATE(test, in, exp);
+}
+
+/* A lower type inside a higher one disappears. */
+static void __init update_table_lower_type_absorbed(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 4, ACPI), E(1, 1, RAM) };
+ static const struct e820_entry exp[] = { E(0, 4, ACPI) };
+
+ CHECK_UPDATE(test, in, exp);
+}
+
+static void __init update_table_merges_same_type(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 1, RAM), E(1, 1, RAM), E(2, 1, RAM) };
+ static const struct e820_entry exp[] = { E(0, 3, RAM) };
+
+ CHECK_UPDATE(test, in, exp);
+}
+
+static void __init update_table_duplicates(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 2, RAM), E(0, 2, RAM) };
+ static const struct e820_entry exp[] = { E(0, 2, RAM) };
+
+ CHECK_UPDATE(test, in, exp);
+}
+
+/* Adjacent entries of different types stay separate, in any input order. */
+static void __init update_table_adjacent_types(struct kunit *test)
+{
+ static const struct e820_entry a[] = { E(0, 1, RAM), E(1, 1, RESERVED) };
+ static const struct e820_entry b[] = { E(1, 1, RESERVED), E(0, 1, RAM) };
+ static const struct e820_entry c[] = { E(0, 1, RESERVED), E(1, 1, RAM) };
+
+ CHECK_UPDATE(test, a, a);
+ CHECK_UPDATE(test, b, a);
+ CHECK_UPDATE(test, c, c);
+}
+
+/* These types describe distinct ranges, so adjacent ones are not merged. */
+static void __init update_table_no_merge_pram_soft_reserved(struct kunit *test)
+{
+ static const struct e820_entry pram[] = { E(0, 1, PRAM), E(1, 1, PRAM) };
+ static const struct e820_entry soft[] = { E(0, 1, SOFT_RESERVED), E(1, 1, SOFT_RESERVED) };
+
+ CHECK_UPDATE(test, pram, pram);
+ CHECK_UPDATE(test, soft, soft);
+}
+
+static void __init update_table_drops_empty_entries(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 1, RAM), E(5, 0, RAM), E(2, 1, RESERVED) };
+ static const struct e820_entry exp[] = { E(0, 1, RAM), E(2, 1, RESERVED) };
+
+ CHECK_UPDATE(test, in, exp);
+}
+
+/* Converts rows like "__22__" to entries: column * 4K, run length * 4K, digit as type. */
+static void __init art_to_entries(const char * const *rows, u32 nrows, struct e820_entry *out)
+{
+ u32 i, start, end;
+
+ for (i = 0; i < nrows; i++) {
+ start = 0;
+ while (rows[i][start] == '_')
+ start++;
+ end = start;
+ while (rows[i][end] && rows[i][end] == rows[i][start])
+ end++;
+ out[i].addr = (u64)start * SZ_4K;
+ out[i].size = (u64)(end - start) * SZ_4K;
+ out[i].type = rows[i][start] - '0';
+ }
+}
+
+/* The example from the comment above e820__update_table(). */
+static void __init update_table_documented_example(struct kunit *test)
+{
+ static const char * const in_rows[] = {
+ "____22__________________",
+ "______________________4_",
+ "____1111________________",
+ "_44_____________________",
+ "11111111________________",
+ "____________________33__",
+ "___________44___________",
+ "__________33333_________",
+ "______________22________",
+ "___________________2222_",
+ "_________111111111______",
+ "_____________________11_",
+ "_________________4______",
+ };
+ static const char * const out_rows[] = {
+ "1_______________________",
+ "_44_____________________",
+ "___1____________________",
+ "____22__________________",
+ "______11________________",
+ "_________1______________",
+ "__________3_____________",
+ "___________44___________",
+ "_____________33_________",
+ "_______________2________",
+ "________________1_______",
+ "_________________4______",
+ "___________________2____",
+ "____________________33__",
+ "______________________4_",
+ };
+ struct e820_entry in[ARRAY_SIZE(in_rows)], exp[ARRAY_SIZE(out_rows)];
+
+ art_to_entries(in_rows, ARRAY_SIZE(in_rows), in);
+ art_to_entries(out_rows, ARRAY_SIZE(out_rows), exp);
+ CHECK_UPDATE(test, in, exp);
+}
+
+/*
+ * If the result does not fit, the table is cut at its capacity. This
+ * documents the current behaviour.
+ */
+static void __init update_table_full_table_truncates(struct kunit *test)
+{
+ struct e820_table *t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL);
+ u32 i;
+
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, t);
+
+ t->entries[0] = (struct e820_entry)E(0, 2 * E820_MAX_ENTRIES, RAM);
+ for (i = 1; i < E820_MAX_ENTRIES; i++) {
+ t->entries[i].addr = PG(2 * i - 1);
+ t->entries[i].size = PG(1);
+ t->entries[i].type = E820_TYPE_RESERVED;
+ }
+ t->nr_entries = E820_MAX_ENTRIES;
+
+ KUNIT_EXPECT_EQ(test, e820__update_table(t), 0);
+ KUNIT_ASSERT_EQ(test, t->nr_entries, (u32)E820_MAX_ENTRIES);
+ for (i = 0; i < E820_MAX_ENTRIES; i++) {
+ KUNIT_EXPECT_EQ_MSG(test, t->entries[i].addr, PG(i), "entry %u addr", i);
+ KUNIT_EXPECT_EQ_MSG(test, t->entries[i].size, PG(1), "entry %u size", i);
+ KUNIT_EXPECT_EQ_MSG(test, (u32)t->entries[i].type,
+ (u32)(i % 2 ? E820_TYPE_RESERVED : E820_TYPE_RAM),
+ "entry %u type", i);
+ }
+}
+
+static void __init range_update_covered(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(1, 4, RAM) };
+ static const struct e820_entry exp[] = { E(1, 4, RESERVED) };
+
+ check_range_update(test, in, ARRAY_SIZE(in), PG(0), PG(16),
+ E820_TYPE_RAM, E820_TYPE_RESERVED, PG(4), exp, ARRAY_SIZE(exp));
+}
+
+/* The new range is inside the entry: it is split, new pieces go to the end. */
+static void __init range_update_split(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(1, 4, RAM) };
+ static const struct e820_entry exp[] = { E(1, 1, RAM), E(2, 1, RESERVED), E(3, 2, RAM) };
+
+ check_range_update(test, in, ARRAY_SIZE(in), PG(2), PG(1),
+ E820_TYPE_RAM, E820_TYPE_RESERVED, PG(1), exp, ARRAY_SIZE(exp));
+}
+
+static void __init range_update_head(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(1, 4, RAM) };
+ static const struct e820_entry exp[] = { E(3, 2, RAM), E(1, 2, RESERVED) };
+
+ check_range_update(test, in, ARRAY_SIZE(in), PG(0), PG(3),
+ E820_TYPE_RAM, E820_TYPE_RESERVED, PG(2), exp, ARRAY_SIZE(exp));
+}
+
+static void __init range_update_tail(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(1, 4, RAM) };
+ static const struct e820_entry exp[] = { E(1, 3, RAM), E(4, 1, RESERVED) };
+
+ check_range_update(test, in, ARRAY_SIZE(in), PG(4), PG(4),
+ E820_TYPE_RAM, E820_TYPE_RESERVED, PG(1), exp, ARRAY_SIZE(exp));
+}
+
+/* No overlap, or a different old type: nothing changes. */
+static void __init range_update_no_match(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(1, 4, RAM) };
+
+ check_range_update(test, in, ARRAY_SIZE(in), PG(8), PG(1),
+ E820_TYPE_RAM, E820_TYPE_RESERVED, 0, in, ARRAY_SIZE(in));
+ check_range_update(test, in, ARRAY_SIZE(in), PG(0), PG(16),
+ E820_TYPE_ACPI, E820_TYPE_RESERVED, 0, in, ARRAY_SIZE(in));
+}
+
+/* A size that would wrap around is cut at the end of the address space. */
+static void __init range_update_clamps_size(struct kunit *test)
+{
+ static const struct e820_entry in[] = {
+ { .addr = ULLONG_MAX - 0xffff, .size = 0xffff, .type = E820_TYPE_RAM },
+ };
+ static const struct e820_entry exp[] = {
+ { .addr = ULLONG_MAX - 0xffff, .size = 0xffff, .type = E820_TYPE_RESERVED },
+ };
+
+ check_range_update(test, in, ARRAY_SIZE(in), ULLONG_MAX - 0xffff, ULLONG_MAX,
+ E820_TYPE_RAM, E820_TYPE_RESERVED, 0xffff, exp, ARRAY_SIZE(exp));
+}
+
+static void __init range_update_several_entries(struct kunit *test)
+{
+ static const struct e820_entry in[] = { E(0, 1, RAM), E(2, 1, RAM), E(4, 1, ACPI) };
+ static const struct e820_entry exp[] = { E(0, 1, NVS), E(2, 1, NVS), E(4, 1, ACPI) };
+
+ check_range_update(test, in, ARRAY_SIZE(in), PG(0), PG(8),
+ E820_TYPE_RAM, E820_TYPE_NVS, PG(2), exp, ARRAY_SIZE(exp));
+}
+
+static struct kunit_case __refdata e820_test_cases[] = {
+ KUNIT_CASE(update_table_too_few),
+ KUNIT_CASE(update_table_overflow),
+ KUNIT_CASE(update_table_unsorted_disjoint),
+ KUNIT_CASE(update_table_nested_higher_type),
+ KUNIT_CASE(update_table_partial_overlap),
+ KUNIT_CASE(update_table_lower_type_absorbed),
+ KUNIT_CASE(update_table_merges_same_type),
+ KUNIT_CASE(update_table_duplicates),
+ KUNIT_CASE(update_table_adjacent_types),
+ KUNIT_CASE(update_table_no_merge_pram_soft_reserved),
+ KUNIT_CASE(update_table_drops_empty_entries),
+ KUNIT_CASE(update_table_documented_example),
+ KUNIT_CASE(update_table_full_table_truncates),
+ KUNIT_CASE(range_update_covered),
+ KUNIT_CASE(range_update_split),
+ KUNIT_CASE(range_update_head),
+ KUNIT_CASE(range_update_tail),
+ KUNIT_CASE(range_update_no_match),
+ KUNIT_CASE(range_update_clamps_size),
+ KUNIT_CASE(range_update_several_entries),
+ {}
+};
+
+static struct kunit_suite e820_test_suite = {
+ .name = "e820",
+ .test_cases = e820_test_cases,
+};
+kunit_test_init_section_suite(e820_test_suite);
--
2.56.1
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update
2026-11-26 22:34 [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update Andrey Zakharov
@ 2026-11-27 8:12 ` David Gow
2026-11-28 11:48 ` Andrey Zakharov
2026-12-04 9:43 ` Andrey Zakharov
1 sibling, 1 reply; 5+ messages in thread
From: David Gow @ 2026-11-27 8:12 UTC (permalink / raw)
To: Andrey Zakharov
Cc: tglx, mingo, bp, dave.hansen, x86, hpa, david, raemoar63,
linux-kselftest, kunit-dev, linux-kernel
> There are no KUnit tests under arch/x86, and I could not find any other
> tests of the E820 table code. e820__update_table() sorts a table, resolves
> overlaps in favour of the higher type, merges neighbours of the same
> type and stops when the table is full. e820__range_update_table()
> changes the type of a range and may split an entry. Both take the table
> as an argument, so they can be tested on a table of the test's own,
> without touching the memory map of the running kernel.
>
> Add 20 test cases. For e820__update_table(): too few entries and an
> entry that wraps around (both return -1 and leave the table alone),
> unsorted input, nested and partial overlaps, merging of equal types,
> types that must not be merged (PRAM and SOFT_RESERVED), zero-size
> entries, the sample map from the comment above the function, and a
> full table. For e820__range_update_table(): a range covering an entry,
> splitting an entry, head and tail, no match, a size that would wrap
> around, and several entries at once.
>
> Both functions are __init, so the suite is registered with
> kunit_test_init_section_suite() and runs during boot, before init
> memory is freed. It can only be built in: X86_E820_KUNIT_TEST depends
> on KUNIT=y.
>
> Tested: built with gcc 16.2.1 and run with kunit.py (CONFIG_KUNIT=y and
> CONFIG_X86_E820_KUNIT_TEST=y) under QEMU (TCG, on top of
> next-20261126): 20 of 20 pass, and 20 of 20 with --arch=i386. To
> check that the tests can fail, I
> changed e820.c temporarily in three ways, one at a time: always
> starting a new entry at a change point (4 tests fail), setting the
> wrong new start when cutting the head of an entry in
> __e820__range_update() (1 fails), and swapping the order of range
> start and end at equal addresses in cpcompare() (1 fails). None of
> these changes is part of this patch.
>
> Not tested: a full build with KUNIT_ALL_TESTS (I only checked that it
> selects the option), other values of MAX_NUMNODES, real hardware.
>
> Signed-off-by: Andrey Zakharov <dedemorow@gmail.com>
>
This looks great for me from a KUnit point of view, and it passes here.
I do think there'd be some benefit going forward to extending this to cover
other parts of the e820 handling code: in particular the code for putting
the e820 memory map together from different BIOS memory maps. That's
the area which has had any issues I've hit.
This is definitely something for a separate follow-up patch, though, not a
reason to reject this as-is. I did do a quick hack for what it could look like
to use KUnit stubs and resources to intercept changes to the real e820 table.
If there's interest and I have time, I can polish it up:
--8<--
diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index 0967b485f38b..a74e342d911b 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -16,6 +16,9 @@
#include <linux/sort.h>
#include <linux/kvm_types.h>
+#include <kunit/static_stub.h>
+#include <kunit/visibility.h>
+
#include <asm/e820/api.h>
#include <asm/setup.h>
@@ -162,7 +165,7 @@ int e820__get_entry_type(u64 start, u64 end)
/*
* Add a memory region to the kernel E820 map.
*/
-__init static void __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type)
+__init VISIBLE_IF_KUNIT void __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type)
{
u32 idx = table->nr_entries;
struct e820_entry *entry_new;
@@ -181,6 +184,7 @@ __init static void __e820__range_add(struct e820_table *table, u64 start, u64 si
table->nr_entries++;
}
+EXPORT_SYMBOL_IF_KUNIT(__e820__range_add);
__init void e820__range_add(u64 start, u64 size, enum e820_type type)
{
@@ -446,7 +450,7 @@ __init int e820__update_table(struct e820_table *table)
*
* Sanity-check it while we're at it..
*/
-__init static int append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
+__init VISIBLE_IF_KUNIT int append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
{
struct boot_e820_entry *entry = entries;
@@ -471,6 +475,7 @@ __init static int append_e820_table(struct boot_e820_entry *entries, u32 nr_entr
}
return 0;
}
+EXPORT_SYMBOL_IF_KUNIT(append_e820_table);
__init static u64
__e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
diff --git a/arch/x86/kernel/e820_kunit.c b/arch/x86/kernel/e820_kunit.c
index efd0323bcfc8..be593fe6d0ec 100644
--- a/arch/x86/kernel/e820_kunit.c
+++ b/arch/x86/kernel/e820_kunit.c
@@ -9,6 +9,9 @@
* The layouts are written in units of one 4K page.
*/
#include <kunit/test.h>
+#include <kunit/test-bug.h>
+#include <kunit/static_stub.h>
+#include <kunit/visibility.h>
#include <linux/init.h>
#include <linux/limits.h>
#include <linux/sizes.h>
@@ -337,6 +340,34 @@ static void __init range_update_several_entries(struct kunit *test)
E820_TYPE_RAM, E820_TYPE_NVS, PG(2), exp, ARRAY_SIZE(exp));
}
+
+__init VISIBLE_IF_KUNIT void __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type);
+static void __init fake_e820_range_add(u64 start, u64 size, enum e820_type type)
+{
+ struct kunit *test = kunit_get_current_test();
+ struct kunit_resource *e820_resource = kunit_find_named_resource(test, "e820_table");
+ struct e820_table *table = (struct e820_table *)e820_resource->data;
+
+ __e820__range_add(table, start, size, type);
+
+ kunit_put_resource(e820_resource);
+}
+
+static struct kunit_resource e820_resource;
+__init VISIBLE_IF_KUNIT int append_e820_table(struct boot_e820_entry *entries, u32 nr_entries);
+static void __init append_e820_table_empty(struct kunit *test)
+{
+ struct e820_table *empty_table = new_table(test, NULL, 0);
+ KUNIT_ASSERT_EQ(test, kunit_add_named_resource(test, NULL, NULL, &e820_resource, "e820_table", empty_table), 0);
+ kunit_get_resource(&e820_resource);
+ kunit_activate_static_stub(test, e820__range_add, fake_e820_range_add);
+
+ /* Ensure an empty table doesn't get updated. */
+ KUNIT_EXPECT_EQ(test, append_e820_table(NULL, 0), -ENOENT);
+ KUNIT_EXPECT_EQ(test, empty_table->nr_entries, 0);
+
+}
+
static struct kunit_case __refdata e820_test_cases[] = {
KUNIT_CASE(update_table_too_few),
KUNIT_CASE(update_table_overflow),
@@ -358,6 +389,7 @@ static struct kunit_case __refdata e820_test_cases[] = {
KUNIT_CASE(range_update_no_match),
KUNIT_CASE(range_update_clamps_size),
KUNIT_CASE(range_update_several_entries),
+ KUNIT_CASE(append_e820_table_empty),
{}
};
-->8--
Regardless, this looks pretty good as-is to me.
Reviewed-by: David Gow <david@davidgow.net>
Cheers,
-- David
--
David Gow <david@davidgow.net>
^ permalink raw reply related [flat|nested] 5+ messages in thread* Re: [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update
2026-11-27 8:12 ` David Gow
@ 2026-11-28 11:48 ` Andrey Zakharov
2026-11-29 9:17 ` David Gow
0 siblings, 1 reply; 5+ messages in thread
From: Andrey Zakharov @ 2026-11-28 11:48 UTC (permalink / raw)
To: david
Cc: tglx, mingo, bp, dave.hansen, x86, hpa, raemoar63,
linux-kselftest, kunit-dev, linux-kernel, Andrey Zakharov
Hi David,
Thank you for the review and for running the tests.
> I do think there'd be some benefit going forward to extending this to
> cover other parts of the e820 handling code [...]
If you have not started polishing your sketch yet, I would be happy to
try turning it into a proper follow-up patch on top of this one. I would
mention you with a Suggested-by tag, or however you prefer, since the
idea and the stub approach are yours. Would that be OK with you?
Thanks again,
Andrey
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update
2026-11-28 11:48 ` Andrey Zakharov
@ 2026-11-29 9:17 ` David Gow
0 siblings, 0 replies; 5+ messages in thread
From: David Gow @ 2026-11-29 9:17 UTC (permalink / raw)
To: Andrey Zakharov
Cc: tglx, mingo, bp, dave.hansen, x86, hpa, raemoar63,
linux-kselftest, kunit-dev, linux-kernel
Le 28/11/2026 à 7:48 PM, Andrey Zakharov a écrit :
> Hi David,
>
> Thank you for the review and for running the tests.
>
>> I do think there'd be some benefit going forward to extending this to
>> cover other parts of the e820 handling code [...]
>
> If you have not started polishing your sketch yet, I would be happy to
> try turning it into a proper follow-up patch on top of this one. I would
> mention you with a Suggested-by tag, or however you prefer, since the
> idea and the stub approach are yours. Would that be OK with you?
Sounds good. I've not done any more than the sketch I've sent out (and
my computer has decided to die this week), so I'm definitely okay with
you taking it over. I think there's a lot of scope for doing further
tests which intercept the generation of the e820 table, not just the
example I had, though I'd probably wait until some more x86 folks are on
board before putting too much time into it.
Cheers,
-- David
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update
2026-11-26 22:34 [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update Andrey Zakharov
2026-11-27 8:12 ` David Gow
@ 2026-12-04 9:43 ` Andrey Zakharov
1 sibling, 0 replies; 5+ messages in thread
From: Andrey Zakharov @ 2026-12-04 9:43 UTC (permalink / raw)
To: tglx, mingo, bp, dave.hansen, x86
Cc: hpa, david, raemoar63, linux-kselftest, kunit-dev, linux-kernel,
Andrey Zakharov
Hi,
A gentle question about this patch, sent on 27 November: David has
given his Reviewed-by in this thread. Is there anything I should
change or add before it could be applied? I am happy to rebase on a
newer base or resend if that would help.
Thanks,
Andrey
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-12-04 9:43 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26 22:34 [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update Andrey Zakharov
2026-11-27 8:12 ` David Gow
2026-11-28 11:48 ` Andrey Zakharov
2026-11-29 9:17 ` David Gow
2026-12-04 9:43 ` Andrey Zakharov
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.