From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.44 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796378836; b=zwISnbDmyw7XvIrdGlRBiE2UBCK2mUJOs9SStyMFgcLa6zuZFKOO7kdaklR4jxksUrfC 4Nyc6C9yXv03P1XwvQWxF25CWwKht5O7lZh35MVPurRvmOhr1T3HXRozIm6Al5SKDqqGi x058mknZq5O28hy1ywgc1dxNGsr/ML3r3xbXZQljU3vtHgVjF96ttxC8csCegBxi4XB68 Dqbf+cdgB/NiyEF+4qIOXFywk2taALMVixQrRAwyPRN3G2sljI5N+gEl1nJuSds+fq8PU Qo+T2QWRvOrbHX9jjbr/JbvOiHPILEt2xKsJ+i8bbzOOf75/kx2G8bkQJhMk91N8wuQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796378836; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=I3Up84nsBjzexlRVJWIFxnTokBcEJ0ExKHTre12EQdU=; b=UQcBhry18LtScs0fQQrO3YIqDnKEq1ZV78jN5+3SM91MUuZntjP2lEDIaPgW/LsHKVzR nDId+lPIE/r/+SnOWMCY09WP11uIzjY7KWxzGDrUxyG0Ssg7l/U3iQoxrwx5f03xw6Way hM3HtreDvmCP0aS3ayqgIWSLlS0KFsolF83akmUf7zl8mvItXeVDGFlg16rlCG4YzIpiq Cof7CD16ooaJttpQroa9t0HEVz1s63LzcyLQZQ7nOopHiPgcn2JLtzXKohGeq+pE+oogY dtEiCof6lU9OpEJUR34rfsI1gkrBVHF1T4pjDDqOmapEl9dIGb+QqLUW6NqfyQ1k3vg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=pUxFMRdj; arc=none smtp.remote-ip=209.85.221.44 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F3835606A322 for ; Fri, 4 Dec 2026 10:07:15 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F3835606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=pUxFMRdj Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-4a7b8abe209so502265f8f.0 for ; Fri, 04 Dec 2026 02:07:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796378834; x=1796983634; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=I3Up84nsBjzexlRVJWIFxnTokBcEJ0ExKHTre12EQdU=; b=pUxFMRdjeLPeIlRP8KTqKQ6Y4Zcm24NLtADyWesWJESfR5MhBdHyHHJHB9r8lu6JSF y3AqfHZ01yTEdfSZtN66gEpJ3xUvdW/k2ohSJdj0B475w4LVzDYoeqUGiJIVtkW++CIX Qnfzn+yxi2rggv5xPAb52NlIhI5r962hfF51WSpOgnwHvUW08wwyHwR2pbKMro/KQuN5 28llcMhL1+omcX+yxWr0o4KgNtC5dxWc58s9Zkv9IAY2EltHBswUrQuNLjienOpSB9yA Jr6HNK5eP2/9YEa9AvusXPXs0pN/Vja4munJadlnKTvUpOBI4KZbY49Ansfnp00j/9Qb o85w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796378834; x=1796983634; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=I3Up84nsBjzexlRVJWIFxnTokBcEJ0ExKHTre12EQdU=; b=nEkE1BohYrrLzd4cfg5eE2MRsoX7f3ML7aw+ivOn3ParRsYdUV6IfVwz/gsedZ4mib vwhg/Gz+g3o8r6VTzVi7Zyq4MGum4u/Esvqe/EhhsCASm4IBaNOwwbXNRSrEepZmgJWc hFNOQ8ciTHt8rNZPbvuu4qoPUcxJ+aIWJJ8wjfsMXlrTPi0DA/9BpwdfHfkyWBK8aqol YEryr1UIxdqP1x66Nj5fwdfxVU8zGxMLn2j46nDF1L3SBODBCr7JtjqR8eGvgdKiLwR+ WOE9T97MS8ZfEEty1ij0fO0MmWLyHgNbNcrA8VFUO87SOr+KFyTCMUxSRF2hGBNKjtiK dcWA== X-Forwarded-Encrypted: i=1; AL+hErEMWkMXPkobmVQd2fN7CDgozPVqsu9mZT9lo0ZFIpIhEGtEvOVT4uaKCtNHnz/3Ew8aT0PJq8thelvL41Y=@vger.kernel.org X-Gm-Message-State: APuU4Z9GirNC+fSJ2r5nFo7LKbJdWHHBtUxUb9T1aqUWZFMuYET/oJKE gwdf+OWqoXe9IEVgKAUV0ljsZAAE8yZT9mp4VRlmVCjZ2EB/0meFggQ4 X-Gm-Gg: AZFdTZfqZvxwQmZVDALGI07v1uJsOzgI9ls04LrzEv5OaiVMhqDoKWAghql66Qtc1Xj Z6bQYPNKSRMZPEP2lFKcspTQBUFMtjZUG3i4gS9/bqZnRCWUdmV2UFX1c+F/qWFa94gVTEN3zUK xQtUSwJJMNYgDKI/YbUJoWJ7XELNzrtnq0jrQGESjp97g+u5sCnTxKx2Van8j4u6Ymb6veOWk6d i2mO1w/Tq4fH/FXlu7p9qoVd0sSxxIwz/yoGfgUAGZVLsyJNcus8pFeUSTFLIAWuBhkb7xD3wvd uFYTalDuBgvRBjTB4DCc6cM3yv75Aiq84H4eFxNfasS8GnchDmamjs8YGzYKE73JjirjQVj/Pr8 02zuysl+61xWemNole1LoRi0fZilI2n43KuIVM8NAW9FCMmoryO3Bdxek8GjLwxSGKC7fqEsJ7W LSVjgFCyFaZD4BIBkSxeddCS+fWs+fXkPskheYWZVlv0Z0OtbXYkJ6T+Xo0Arqk1F6TGXHEjlUA YSetKPVSs0LYedl/8VWAJHXjt7JAT8+8mblBqRiDcBM9oH3hso1c5uP9r77KaQNRzr0vnh2dpeX GpNn3lDJIFKxAWzqonSACQN1W5+oY/PIf60wkO52cr2A6qN0CjI= X-Received: by 2002:a05:600c:1d05:b0:4bb:53e4:a7cc with SMTP id 5b1f17b1804b1-4bbeb8d1ffbmr24095195e9.30.1796378834233; Fri, 04 Dec 2026 02:07:14 -0800 (PST) Received: from thinkpad ([2a00:1370:81ac:4181:10b1:754b:3cfe:5139]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4d174fe6fbcsm4342699f8f.58.2026.12.04.02.07.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Dec 2026 02:07:13 -0800 (PST) From: Andrey Zakharov To: tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org Cc: hpa@zytor.com, david@davidgow.net, raemoar63@gmail.com, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com, linux-kernel@vger.kernel.org, Andrey Zakharov Subject: [PATCH] x86/e820: Add KUnit tests for append_e820_table() Date: Fri, 4 Dec 2026 13:07:01 +0300 Message-ID: <20261204100701.114566-1-dedemorow@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Andrey Zakharov --- 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@gmail.com/ 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 #include +#include +#include + #include #include @@ -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 +#include #include +#include #include #include #include @@ -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