Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 71726401A3A for ; Thu, 21 May 2026 19:32:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779391927; cv=none; b=cvXDCBecDeIdWkvfK6h1xmb8pEmZkZnGFND12XEjThFvCWdD4rT3e+7HLNhG2j0yU+DRpdHrRyFGHmW+/XOD4+FKsyXoizUoSqK6Nhzgla50J30q/+f0FVxAJLlEgA6NDPgudi6+kjE+FPKFmJoytxCadW5nYRzQr9e+eQug6F4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779391927; c=relaxed/simple; bh=fzTmEXtobJ8gPQ1Uy+bZ7A/yz+rx1mjTIG84+U0AIjw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cuH5bsw4uLIjXPZEPXrzmqoU2PXpSS87IF85cq0iA6YFcIsN8pzBXONheRPRf2kbGL1FPpYQx1TnLz4TlINY7oZvtotMhWykZTnxkDnPt12bufFDJo46UM+n9vlt2OlGmXvoh+Qn6bSPFJmSrSac34Z2WlgyqMRKUesLuXeW034= 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=tezuphww; arc=none smtp.client-ip=209.85.210.202 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="tezuphww" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-83565161a6eso3324999b3a.1 for ; Thu, 21 May 2026 12:32:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779391926; x=1779996726; 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=ncOCeCfKsOq2argSmTVmNq9obkxAz4X3VwJeLEeWDgg=; b=tezuphwwbmVs09w+TByMKYUpct82x8oCW+SRNfg8/He32yblOdBywdm4QcZ/MnWseP 0YSanMQ5vAofKdCEeL+XwzlyPhY8bYE98pDGFuArV/X4JETSHaBeQTjds9BEMw7eQyLs De+zKY29IFd6hiM2Db5n/8t2L2TF4JPPEn48Qy24K/omSPTqRI3N/OekNK7tFXd0N5zS XjSsad+qdeueZLS3/mfUCOWJAB1dLr8/pR1KHe+1pYL1onUafywvwDQY6urfHOL7zUtc qrfiCu57n/AtXKplAV8c6qxjDW+J9sxU+Hw+/tP8/IwHiKwKlVy38TAaE39avpGUTkkP cMLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779391926; x=1779996726; 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=ncOCeCfKsOq2argSmTVmNq9obkxAz4X3VwJeLEeWDgg=; b=DvKhMxQ4mKXwz9lTXdljewhTPo3io3D1z0HUen8T7A6our9//IILblTkBSpLfBlgwI t8KBsd1803xe/Du1gbRUOtL/XjEaYPm3xziJ52KbWRs3BbvLT2+cXPsgrYiLyuoxSvnZ GWnFaCukHSiECcesmlFDhsaHDf23IxWHLBn30igU+V8/WBzYRut/xw10h/rjS0Q+N+cK rLKtIJcKjp6pBcnMGD5EouLeMtBKB2zpogPfVuBRPZY58Gp9WoeULc/OQGK0M1KNMAmz cunQ+St0fMJ/70ftC5lA6wODwiikf+yi05o2Y920eLI4721L0nr+qmp4wb1XGXKwCCEB HxbA== X-Forwarded-Encrypted: i=1; AFNElJ+QHdzhEOaYnwOcLpsMXHrV1xnPgTlMujO7b1a0WEkTuHr2yHkTztXlx025mb8+CWn8zzh2Kiiipg+ZOBY=@vger.kernel.org X-Gm-Message-State: AOJu0YyYtq8r7/0Fg7Fzzobh/3MY9N0LaLa5RxtVsrnpQ5VgFxf8EiG8 M29eNc85Z2FF4a0zb8NG0E9mimaS53+/JLIAXAtO/TKbjMaGspsZM1KM7NXx4wy7DIBEw+ioJIo s+rSIe90QRETmHA== X-Received: from pfll21.prod.google.com ([2002:a05:6a00:1595:b0:82f:93e5:c64c]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:847:b0:82c:7420:82bf with SMTP id d2e1a72fcca58-84161176bddmr117690b3a.19.1779391925396; Thu, 21 May 2026 12:32:05 -0700 (PDT) Date: Thu, 21 May 2026 19:32:02 +0000 In-Reply-To: <20260521193202.746810-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: <20260521193202.746810-1-skhawaja@google.com> X-Mailer: git-send-email 2.54.0.746.g67dd491aae-goog Message-ID: <20260521193202.746810-4-skhawaja@google.com> Subject: [PATCH v2 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 | 131 ++++++++++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 kernel/liveupdate/kexec_handover_test.c diff --git a/kernel/liveupdate/Kconfig b/kernel/liveupdate/Kconfig index 1a8513f16ef7..d2623f60c0dd 100644 --- a/kernel/liveupdate/Kconfig +++ b/kernel/liveupdate/Kconfig @@ -51,6 +51,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..9e6dca45c3f3 --- /dev/null +++ b/kernel/liveupdate/kexec_handover_test.c @@ -0,0 +1,131 @@ +// 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.746.g67dd491aae-goog