Re: [PATCH] x86/e820: Add KUnit tests for table sanitizing and range update

From: David Gow

Date: Fri Nov 27 2026 - 03:12:53 EST


> 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@xxxxxxxxx>
>

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@xxxxxxxxxxxx>

Cheers,
-- David

--
David Gow <david@xxxxxxxxxxxx>