[PATCH] x86/e820: Add KUnit tests for append_e820_table()
From: Andrey Zakharov
Date: Fri Dec 04 2026 - 05:07:22 EST
The existing e820 KUnit tests cover the table sanitizing and range
update helpers only. Add tests for append_e820_table(), which copies
the entries of the BIOS-provided map into the e820 table. It returns
-ENOENT for an empty input and -EINVAL for an entry that wraps around
the address space.
To see what append_e820_table() hands over without touching the real
table, e820__range_add() is redirected with a KUnit static stub and the
test records the arguments of each call. append_e820_table() is made
visible to the tests with VISIBLE_IF_KUNIT, and its prototype is
declared under CONFIG_KUNIT in asm/e820/api.h.
The new tests cover an empty input, entries keeping their order and
types, an entry wrapping around the address space, entries of zero
size and an entry ending at the end of the address space.
The approach and the first test follow a sketch by David Gow.
Tested with kunit.py on x86_64 and i386: 25 of 25 tests pass (the 20
existing ones and the 5 new ones). Breaking append_e820_table() in five
different ways makes the new tests fail. One of those runs printed no
KTAP output and worked when repeated without changes; I did not find
out why. Not tested: real hardware.
Suggested-by: David Gow <david@xxxxxxxxxxxx>
Signed-off-by: Andrey Zakharov <dedemorow@xxxxxxxxx>
---
Hi,
This applies on top of "x86/e820: Add KUnit tests for table sanitizing
and range update", which is still waiting for review:
https://lore.kernel.org/all/20261126223400.1549478-1-dedemorow@xxxxxxxxx/
David Gow suggested this follow-up in that thread. Would you rather
receive it only after the first patch is applied? I am happy to hold it
back and resend it then.
Based on next-20261126 plus the patch above.
arch/x86/include/asm/e820/api.h | 4 +
arch/x86/kernel/e820.c | 6 +-
arch/x86/kernel/e820_kunit.c | 140 ++++++++++++++++++++++++++++++++
3 files changed, 149 insertions(+), 1 deletion(-)
diff --git a/arch/x86/include/asm/e820/api.h b/arch/x86/include/asm/e820/api.h
index bbe0c8de976c..a3bbecf54df0 100644
--- a/arch/x86/include/asm/e820/api.h
+++ b/arch/x86/include/asm/e820/api.h
@@ -20,6 +20,10 @@ extern void e820__range_remove(u64 start, u64 size, enum e820_type filter_type);
extern u64 e820__range_update_table(struct e820_table *t, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type);
extern int e820__update_table(struct e820_table *table);
+
+#if IS_ENABLED(CONFIG_KUNIT)
+int append_e820_table(struct boot_e820_entry *entries, u32 nr_entries);
+#endif
extern void e820__update_table_print(void);
extern unsigned long e820__end_of_ram_pfn(void);
diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index 0967b485f38b..105ae9858f12 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>
@@ -184,6 +187,7 @@ __init static void __e820__range_add(struct e820_table *table, u64 start, u64 si
__init void e820__range_add(u64 start, u64 size, enum e820_type type)
{
+ KUNIT_STATIC_STUB_REDIRECT(e820__range_add, start, size, type);
__e820__range_add(e820_table, start, size, 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;
diff --git a/arch/x86/kernel/e820_kunit.c b/arch/x86/kernel/e820_kunit.c
index efd0323bcfc8..2a7f05da0912 100644
--- a/arch/x86/kernel/e820_kunit.c
+++ b/arch/x86/kernel/e820_kunit.c
@@ -8,7 +8,10 @@
*
* The layouts are written in units of one 4K page.
*/
+#include <kunit/static_stub.h>
+#include <kunit/test-bug.h>
#include <kunit/test.h>
+#include <linux/errno.h>
#include <linux/init.h>
#include <linux/limits.h>
#include <linux/sizes.h>
@@ -337,6 +340,138 @@ static void __init range_update_several_entries(struct kunit *test)
E820_TYPE_RAM, E820_TYPE_NVS, PG(2), exp, ARRAY_SIZE(exp));
}
+/*
+ * append_e820_table() adds every firmware entry to the kernel table with
+ * e820__range_add(). The tests replace that function with a stub which only
+ * records its arguments, so the real E820 table is never touched.
+ */
+struct range_add_log {
+ u32 nr;
+ struct e820_entry calls[8];
+};
+
+#define BOOT_E(a, s, t) { .addr = PG(a), .size = PG(s), .type = (t) }
+
+static void __init fake_e820_range_add(u64 start, u64 size, enum e820_type type)
+{
+ struct kunit *test = kunit_get_current_test();
+ struct range_add_log *log = test->priv;
+
+ if (log->nr >= ARRAY_SIZE(log->calls)) {
+ KUNIT_FAIL(test, "too many e820__range_add() calls");
+ return;
+ }
+ log->calls[log->nr].addr = start;
+ log->calls[log->nr].size = size;
+ log->calls[log->nr].type = type;
+ log->nr++;
+}
+
+static struct range_add_log *__init stub_range_add(struct kunit *test)
+{
+ struct range_add_log *log = kunit_kzalloc(test, sizeof(*log), GFP_KERNEL);
+
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, log);
+ test->priv = log;
+ kunit_activate_static_stub(test, e820__range_add, fake_e820_range_add);
+ return log;
+}
+
+static void __init expect_range_add_log(struct kunit *test,
+ const struct range_add_log *log,
+ const struct e820_entry *exp, u32 n)
+{
+ u32 i;
+
+ KUNIT_ASSERT_EQ(test, log->nr, n);
+ for (i = 0; i < n; i++) {
+ KUNIT_EXPECT_EQ_MSG(test, log->calls[i].addr, exp[i].addr, "call %u addr", i);
+ KUNIT_EXPECT_EQ_MSG(test, log->calls[i].size, exp[i].size, "call %u size", i);
+ KUNIT_EXPECT_EQ_MSG(test, (u32)log->calls[i].type, (u32)exp[i].type,
+ "call %u type", i);
+ }
+}
+
+static void __init append_empty(struct kunit *test)
+{
+ struct boot_e820_entry unused = {};
+ struct range_add_log *log = stub_range_add(test);
+
+ KUNIT_EXPECT_EQ(test, append_e820_table(NULL, 0), -ENOENT);
+ KUNIT_EXPECT_EQ(test, append_e820_table(&unused, 0), -ENOENT);
+ KUNIT_EXPECT_EQ(test, log->nr, 0);
+}
+
+/* Entries are added one by one in firmware order, and the types are passed on as they are. */
+static void __init append_keeps_order_and_types(struct kunit *test)
+{
+ struct boot_e820_entry in[] = {
+ BOOT_E(8, 1, E820_TYPE_ACPI),
+ BOOT_E(0, 2, E820_TYPE_RAM),
+ BOOT_E(4, 1, E820_TYPE_SOFT_RESERVED),
+ BOOT_E(6, 1, 99),
+ };
+ static const struct e820_entry exp[] = {
+ E(8, 1, ACPI), E(0, 2, RAM), E(4, 1, SOFT_RESERVED),
+ { .addr = PG(6), .size = PG(1), .type = 99 },
+ };
+ struct range_add_log *log = stub_range_add(test);
+
+ KUNIT_EXPECT_EQ(test, append_e820_table(in, ARRAY_SIZE(in)), 0);
+ expect_range_add_log(test, log, exp, ARRAY_SIZE(exp));
+}
+
+/* The first entry that wraps around ends the scan, the entries after it are ignored. */
+static void __init append_wrapping_entry_stops(struct kunit *test)
+{
+ struct boot_e820_entry in[] = {
+ BOOT_E(0, 1, E820_TYPE_RAM),
+ { .addr = ULLONG_MAX - 0xfff, .size = SZ_8K, .type = E820_TYPE_RESERVED },
+ BOOT_E(4, 1, E820_TYPE_RAM),
+ };
+ static const struct e820_entry exp[] = { E(0, 1, RAM) };
+ struct range_add_log *log = stub_range_add(test);
+
+ KUNIT_EXPECT_EQ(test, append_e820_table(in, ARRAY_SIZE(in)), -EINVAL);
+ expect_range_add_log(test, log, exp, ARRAY_SIZE(exp));
+}
+
+/* An empty entry is not an error, even though start - 1 wraps around for start 0. */
+static void __init append_zero_size_entries(struct kunit *test)
+{
+ struct boot_e820_entry in[] = {
+ BOOT_E(5, 0, E820_TYPE_RAM),
+ BOOT_E(0, 0, E820_TYPE_RESERVED),
+ };
+ static const struct e820_entry exp[] = { E(5, 0, RAM), E(0, 0, RESERVED) };
+ struct range_add_log *log = stub_range_add(test);
+
+ KUNIT_EXPECT_EQ(test, append_e820_table(in, ARRAY_SIZE(in)), 0);
+ expect_range_add_log(test, log, exp, ARRAY_SIZE(exp));
+}
+
+/* The last page of the address space is valid, one byte more is not. */
+static void __init append_end_of_address_space(struct kunit *test)
+{
+ struct boot_e820_entry last[] = {
+ { .addr = ULLONG_MAX - 0xfff, .size = SZ_4K, .type = E820_TYPE_RAM },
+ };
+ struct boot_e820_entry over[] = {
+ { .addr = ULLONG_MAX - 0xfff, .size = SZ_4K + 1, .type = E820_TYPE_RAM },
+ };
+ static const struct e820_entry exp[] = {
+ { .addr = ULLONG_MAX - 0xfff, .size = SZ_4K, .type = E820_TYPE_RAM },
+ };
+ struct range_add_log *log = stub_range_add(test);
+
+ KUNIT_EXPECT_EQ(test, append_e820_table(last, ARRAY_SIZE(last)), 0);
+ expect_range_add_log(test, log, exp, ARRAY_SIZE(exp));
+
+ log->nr = 0;
+ KUNIT_EXPECT_EQ(test, append_e820_table(over, ARRAY_SIZE(over)), -EINVAL);
+ KUNIT_EXPECT_EQ(test, log->nr, 0);
+}
+
static struct kunit_case __refdata e820_test_cases[] = {
KUNIT_CASE(update_table_too_few),
KUNIT_CASE(update_table_overflow),
@@ -358,6 +493,11 @@ 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_empty),
+ KUNIT_CASE(append_keeps_order_and_types),
+ KUNIT_CASE(append_wrapping_entry_stops),
+ KUNIT_CASE(append_zero_size_entries),
+ KUNIT_CASE(append_end_of_address_space),
{}
};
--
2.56.1