From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4D67136F8F2 for ; Tue, 12 May 2026 19:51:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778615500; cv=none; b=di77zqUDK30LD+9442uegHDguoYygd1U3EdVAid6xz6HAyRt3mpHLgAxIA3slRRVaAd5lZU9KtplAUGD9j5s+kYdF5bK/sOQrx6nZrnwK4kHP1vhd31ZUhn5qmVrrZxXJSoazzPya+PjDtEvzINOtqjRuK5gr6qT+gMBV4OrLOU= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778615500; c=relaxed/simple; bh=s5jA43xBR0yBAGlqGzPOOLWtqHBz7cNRz/eRUc7cA84=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=M57AfyzJUPRLXKAOP32MADlir9idytpaQNHbPe/uLjmJIF+2mZWWI8y5+VYuI9yo+siPLbuL9PORKad70Cp6mh0dUkKMirBMYX//zqNKeUfxIv6F/FaQuTxdD+K4gSK9LpiVxlUVW7F2Xj1Lt7ClASxgGiBhYp1G8vdBHLqB+8o= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=i0m7kqXS; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--skhawaja.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="i0m7kqXS" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c828ab3b033so3128051a12.3 for ; Tue, 12 May 2026 12:51:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778615498; x=1779220298; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=v7TVTYRDvcEKHxiAe879U7fECAlukFuq2rcibnBNqK4=; b=i0m7kqXSJ9S5FI+LmhyJdXZ5kpxfiy/YbJu6oVfEDx5ax2L+4hQEtUvRFbX8d31FRF LecQGSaXFQhaXShV0uhwGl+Djhkoi8CGk6VFRscrCaQSWQy9JdJ4JoUk0KKB8G4sjfdZ O6gQfSwJt3SAe+799Nei1PtRVoLAg62JU1puKG9soCXSu9R7rN7fk340fsI1co1mnt/y D7LPbnL0rowQFlz5EH5DepMktZEmX3tBaebyS2I7E6j96mPJMghTEyU4utjjk84EKRBv Ls9dEslsXoFCdGbKIlQvoNjFAMhhhlc+tJnf1tmNbrhA08YyGpPEM1p/u3dsVYZa/uh8 F4JQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778615498; x=1779220298; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=v7TVTYRDvcEKHxiAe879U7fECAlukFuq2rcibnBNqK4=; b=JGUoNT8zogVrqYnW5veCKsry6YpOumYyW8bvRI5xE6nRxRImYpv/s7K2NpDhJXaxb8 +jQvIyj4cxPy46Y2tkj+ekbrO2x5vXsTRXie88zueLS89MCmfPDMGhXPSsQVHzUbihCJ 82KLoHmC3gPv/mogNtnXE+v6uAxiN2z8r/IG7AvcTyG+hTPgP4x7RfCsXJimmXmaF/XA txwkXFhAV0aYb3gxP0Kqs3B4URHUFh7dIZ4iezOly9smcEcBhCm/S4nJa+QaYWhN+c3K FMiPr1V4BHhBZ4axHoGMiswndlhpaDJyvb+UlDO+/bQwmuYS34Yk7RXW5nelPl9O+5RN rVtw== X-Forwarded-Encrypted: i=1; AFNElJ8ywI41iRBp3Tso1AgYvbjnnotZ/qk1A/UPaof6UxHJmAVhHP3WpbIiR5btFW4Z5lHb2XisxVDkFlzNZrM=@vger.kernel.org X-Gm-Message-State: AOJu0YwAMoxDa5c41r2pimde6g4U+yN/TRcmnAbCjtJN8PUDkvC5eUSM gV+GEN4MlBAKA7/UfZIst2z2PW8apjpl8zdUi9nFf56i8h8ytyLYsMSf70N/Nhhvg5dzsYx3gEu CMba5ULHczsNpHg== X-Received: from pfbcg6.prod.google.com ([2002:a05:6a00:2906:b0:836:ed4a:203f]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:298f:b0:82f:390a:69c7 with SMTP id d2e1a72fcca58-83f0429e3c3mr90865b3a.33.1778615498220; Tue, 12 May 2026 12:51:38 -0700 (PDT) Date: Tue, 12 May 2026 19:51:35 +0000 In-Reply-To: <20260512195135.804833-1-skhawaja@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260512195135.804833-1-skhawaja@google.com> X-Mailer: git-send-email 2.54.0.563.g4f69b47b94-goog Message-ID: <20260512195135.804833-4-skhawaja@google.com> Subject: [PATCH 3/3] kho: Add kunit test to verify preserve/restore pages and folio From: Samiullah Khawaja To: Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Alexander Graf Cc: Samiullah Khawaja , David Matlack , open list , "open list:KEXEC HANDOVER (KHO)" , "open list:KEXEC HANDOVER (KHO)" Content-Type: text/plain; charset="UTF-8" Add a kunit test to verify the preserve/unpreserve and restore of pages and folios by mocking the kho kunit restore stubs. Signed-off-by: Samiullah Khawaja --- kernel/liveupdate/Kconfig | 10 ++ kernel/liveupdate/Makefile | 1 + kernel/liveupdate/kexec_handover_test.c | 130 ++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 kernel/liveupdate/kexec_handover_test.c diff --git a/kernel/liveupdate/Kconfig b/kernel/liveupdate/Kconfig index c13af38ba23a..ffdfdc4080ef 100644 --- a/kernel/liveupdate/Kconfig +++ b/kernel/liveupdate/Kconfig @@ -49,6 +49,16 @@ config KEXEC_HANDOVER_ENABLE_DEFAULT The default behavior can still be overridden at boot time by passing 'kho=off'. +config KEXEC_HANDOVER_KUNIT_TEST + tristate "KUnit test for Kexec Handover (KHO)" + depends on KEXEC_HANDOVER && KUNIT + default KUNIT_ALL_TESTS + help + Enable Kunit tests for Kexec Handover to verify preservation and + unpreservation of memory using KHO API without triggering kexec. + + If unsure, say N. + config LIVEUPDATE bool "Live Update Orchestrator" depends on KEXEC_HANDOVER diff --git a/kernel/liveupdate/Makefile b/kernel/liveupdate/Makefile index d2f779cbe279..ed798b2401a8 100644 --- a/kernel/liveupdate/Makefile +++ b/kernel/liveupdate/Makefile @@ -9,5 +9,6 @@ luo-y := \ obj-$(CONFIG_KEXEC_HANDOVER) += kexec_handover.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUG) += kexec_handover_debug.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUGFS) += kexec_handover_debugfs.o +obj-$(CONFIG_KEXEC_HANDOVER_KUNIT_TEST) += kexec_handover_test.o obj-$(CONFIG_LIVEUPDATE) += luo.o diff --git a/kernel/liveupdate/kexec_handover_test.c b/kernel/liveupdate/kexec_handover_test.c new file mode 100644 index 000000000000..1113007f9ac5 --- /dev/null +++ b/kernel/liveupdate/kexec_handover_test.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2026, Google LLC + * Author: Samiullah Khawaja + */ + +#include +#include +#include +#include +#include + +static struct page *kho_test_restore_pages_mock(phys_addr_t phys, + unsigned long nr_pages) +{ + struct page *page = phys_to_page(phys); + + if (!kho_test_pages_preserved(phys, nr_pages)) + return NULL; + + kho_unpreserve_pages(page, nr_pages); + return page; +} + +static struct folio *kho_test_restore_folio_mock(phys_addr_t phys) +{ + struct folio *folio = page_folio(phys_to_page(phys)); + + if (!kho_test_pages_preserved(phys, (1 << folio_order(folio)))) + return NULL; + + kho_unpreserve_folio(folio); + return folio; +} + +static int kho_test_init(struct kunit *test) +{ + kunit_activate_static_stub(test, kho_restore_pages, + kho_test_restore_pages_mock); + kunit_activate_static_stub(test, kho_restore_folio, + kho_test_restore_folio_mock); + return 0; +} + +static void kho_test_alloc_preserve(struct kunit *test) +{ + void *mem; + + mem = kho_alloc_preserve(PAGE_SIZE); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, mem); + + /* Verify if memory is preserved */ + KUNIT_EXPECT_TRUE(test, kho_test_pages_preserved(__pa(mem), 1)); + + kho_restore_free(mem); + + /* Memory should be unpreserved after restore_free */ + KUNIT_EXPECT_FALSE(test, kho_test_pages_preserved(__pa(mem), 1)); +} + +static void kho_test_preserve_pages(struct kunit *test) +{ + struct page *restored; + struct page *page; + int err; + + page = alloc_pages(GFP_KERNEL | __GFP_ZERO, 1); + KUNIT_ASSERT_NOT_NULL(test, page); + + /* Preserve and verify that pages are preserved */ + err = kho_preserve_pages(page, 2); + KUNIT_EXPECT_EQ(test, err, 0); + KUNIT_EXPECT_TRUE(test, + kho_test_pages_preserved(page_to_phys(page), 2)); + + restored = kho_restore_pages(page_to_phys(page), 2); + KUNIT_EXPECT_NOT_NULL(test, restored); + KUNIT_EXPECT_PTR_EQ(test, restored, page); + + /* Verify that the pages are not preserved */ + KUNIT_EXPECT_FALSE(test, kho_test_pages_preserved(page_to_phys(page), 2)); + + __free_pages(page, 1); +} + +static void kho_test_preserve_folio(struct kunit *test) +{ + struct folio *restored; + unsigned long nr_pages; + struct folio *folio; + int err; + + folio = folio_alloc(GFP_KERNEL | __GFP_ZERO, 1); + KUNIT_ASSERT_NOT_NULL(test, folio); + + nr_pages = 1 << folio_order(folio); + + /* Preserve and verify that folio is preserved */ + err = kho_preserve_folio(folio); + KUNIT_EXPECT_EQ(test, err, 0); + KUNIT_EXPECT_TRUE(test, kho_test_pages_preserved(PFN_PHYS(folio_pfn(folio)), nr_pages)); + + restored = kho_restore_folio(PFN_PHYS(folio_pfn(folio))); + KUNIT_EXPECT_NOT_NULL(test, restored); + KUNIT_EXPECT_PTR_EQ(test, restored, folio); + + /* Verify that the folio is not preserved */ + KUNIT_EXPECT_FALSE(test, kho_test_pages_preserved(PFN_PHYS(folio_pfn(folio)), nr_pages)); + + folio_put(folio); +} + +static struct kunit_case kho_test_cases[] = { + KUNIT_CASE(kho_test_alloc_preserve), + KUNIT_CASE(kho_test_preserve_pages), + KUNIT_CASE(kho_test_preserve_folio), + {} +}; + +static struct kunit_suite kho_test_suite = { + .name = "kho_test", + .init = kho_test_init, + .test_cases = kho_test_cases, +}; + +kunit_test_suite(kho_test_suite); + +MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING"); +MODULE_DESCRIPTION("KUnit tests for Kexec Handover"); +MODULE_LICENSE("GPL"); -- 2.54.0.563.g4f69b47b94-goog