From nobody Fri Jun 12 20:19:56 2026 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.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 960FB355F54 for ; Tue, 12 May 2026 19:51:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778615498; cv=none; b=UTbuy7BB1CHZmhrKCej1VNPSY6vFV4eYF4Xe9BqrZgnbzmH/atJniJc38zVO/XKsVnU/rHW7Ta93J2Qco78S9zTCO/3x2cvW8h2xnjnMDUQDeA6teneGFpFrw4V4Vgq/i/jvNQF5Ak7EFw8Pus1o57A5ifwOrVfzCXtW/jIT2a0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778615498; c=relaxed/simple; bh=ZP06RG78JDKI245J2bZG+Ch9/EULhQE/vhcg5S/jhLk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Mxj1pCWxj2k21upWft9IgX08xvLpw7V9Zqt52++jWfrozWPxNestwDiMaYLk9yOuI3j/kvsFek0N/I4kcajDTvlOTSrxwqQvsaYRGyW44ad6MSvERf/PcZQPqTUfahuA5Ax7EQjWoIXqCUyH7UjenXinshraQvIM1m+MCk7iJGs= 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=gkKaUtYm; arc=none smtp.client-ip=209.85.214.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="gkKaUtYm" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2bc7f9b2213so27709335ad.0 for ; Tue, 12 May 2026 12:51:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1778615497; x=1779220297; 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=Ljg+98L2Wc2yizBAt6Gvu0fP3XnfuPS9FfSkO2m0vXM=; b=gkKaUtYmD7hDvLjbUlqueLSyv5TjHlxPGEoCO4lqnSKaEE0ShRBAoFgWG960o+03jw 5bgs/lSeu2kmxuv+CtcXhZTqBmDC1MognYGXbFwswV4QJEaj+c9LOmk9kT2jsh6ygVvy gjgV9GSN5AiPhcMXzjSeZj7uSMafVIxhy4bZbisnQvB1LTQuDWneKXM1FifBRLonSjKc 2auqzptcbRFjlIljhmpt2iOSooJT55vHmQw5lsB1M4dr1jADnO9OTpgN3I0HVWMtGXm/ dShZo5XqBYinQWQ30mCyHNcTFXtgzZeCHMtp1GnzOn9abKKHfKR8XubUWfUTuoCAI2// ypQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1778615497; x=1779220297; 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=Ljg+98L2Wc2yizBAt6Gvu0fP3XnfuPS9FfSkO2m0vXM=; b=Fz9rrZwmtoWxf2y/bDaaOVk1BSULsb5BxUhchAC8gi4mV/o7keOjYWgTrcEWV/MaR0 iLpLcJ9g3SfbgzDuYIl2ncKSSCxJLfKzVNsU4jcZQlkAd/lMtfHcg8VX4RtdZjUPHMIa L3Eqh8D5O+Lrn8HuSBeyOTH2p6uHtDpxDrATC8fY/++jvnAVLRNDADxf0Hatu16Y1w1I hd5+rLOj65RJyRlTyboLFJ+bKjxEmx5V9Kke078d2UYS6LnsIH8eSpa44v65pk6Zw7k4 oM2BGcpx8JWo44DOarUUIciLJOjaPfXd1V1ppH4cRf5ncn5ed9CBofs6RR9wupoVgfYD YWZw== X-Forwarded-Encrypted: i=1; AFNElJ8K3dmLFwtFqcNEX5z6OJNfjWF1YicAI2ZZy7oxcJXWkkXcyi0VU4nhzg/GtTH8Tuhv/s8EAKCES1l48/s=@vger.kernel.org X-Gm-Message-State: AOJu0YyZYmtyD97NKp1+A2TqyFX3Jt1sScN4DriD/uknb7dkcMkk1gIp ysMA/oCc/7PWNwF8cEkoQ6zGGxeUO+npgnuu4HfSionM4gjiiggM+V/Ki2hwsBuBNEuwOcqBNRg Wj/N98VkYGFfuXQ== X-Received: from play21.prod.google.com ([2002:a17:902:e195:b0:2bd:15bf:f9c7]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:1b45:b0:2b0:a957:304 with SMTP id d9443c01a7336-2bd270fb799mr4241965ad.6.1778615496629; Tue, 12 May 2026 12:51:36 -0700 (PDT) Date: Tue, 12 May 2026 19:51:33 +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-2-skhawaja@google.com> Subject: [PATCH 1/3] kho: Add kunit static stubs 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add kunit stubs in kho_restore_folio() and kho_restore_pages() so the users can mock these functions in kunit tests. Signed-off-by: Samiullah Khawaja --- kernel/liveupdate/kexec_handover.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_h= andover.c index d5718bef6d4d..1375291d9b07 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -11,6 +11,7 @@ #define pr_fmt(fmt) "KHO: " fmt =20 #include +#include #include #include #include @@ -437,7 +438,11 @@ static struct page *kho_restore_page(phys_addr_t phys,= bool is_folio) */ struct folio *kho_restore_folio(phys_addr_t phys) { - struct page *page =3D kho_restore_page(phys, true); + struct page *page; + + KUNIT_STATIC_STUB_REDIRECT(kho_restore_folio, phys); + + page =3D kho_restore_page(phys, true); =20 return page ? page_folio(page) : NULL; } @@ -459,6 +464,8 @@ struct page *kho_restore_pages(phys_addr_t phys, unsign= ed long nr_pages) const unsigned long end_pfn =3D start_pfn + nr_pages; unsigned long pfn =3D start_pfn; =20 + KUNIT_STATIC_STUB_REDIRECT(kho_restore_pages, phys, nr_pages); + while (pfn < end_pfn) { const unsigned int order =3D min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn)); --=20 2.54.0.563.g4f69b47b94-goog From nobody Fri Jun 12 20:19:56 2026 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 5EA24367288 for ; Tue, 12 May 2026 19:51:38 +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=1778615499; cv=none; b=YMLVMWuTRqzt2rb5TRpXB1Vxjkr1Tc/OQBnAI4VzGDK72hezkIrKL1V0CKvtlmJqnwfcpq1/C3G97uuOcueMPnk5YfohO0BMDHGyrVKfoVGLs+AGB2AmVEqv1vtr2fIhD/OHrmA/up8JOK6u8pBhjFUuMc0sjAUOCI98q6Rasrk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778615499; c=relaxed/simple; bh=1h3cl29oZUzLDsloTvTbA47c6D143IEuK2tWFTrh5k8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OtJTKcmyXufHhnl8G8rh/d/esjwiS8FQTaL1mzNr00qubK3kMNvHFXwyw9/4dJTr7XOz+Nj7CGEqGSSmGh3UTe9OT++65iMI8f6e1Mx3OMc31R3IOfge79wrXC/5w2nDVT1O7NiHAXGC8h+xq9Iu0YPvpgozWJQ4xEmSxX2WfbY= 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=j3+ZsfvK; 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="j3+ZsfvK" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-82f9f49e4beso2929566b3a.0 for ; Tue, 12 May 2026 12:51:38 -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=Hm3N3CUlxXRBXqf7e3XiIHtpRuBL112Zzp/Y6lHhCj8=; b=j3+ZsfvKySXfJxqkH5aceF8VZrNdkpsnBWA+IBG8lQYSDhBQEgjIczST09PrnRDpYp 8IOnxVahCyJ1xR3OLLP1MNNJXPV30rQMSiv5o6DaSJOWkIbQitrqCQqmacMsU+qbWbXR hoQYzs85j5zkU0VECYvwhOSiv9xczE4ScjdBCIhp6LpfEz1XRgg36bGn0jKKaaawXO3K KJ7MDTaWCp6JtAOtGlLwzoLSEvgxasqORoizIJxCCvPVwzIm0glrAvXOCre4rzElW9cY old3HKryMqi77vHuu7pYgkQLf8/63VqRRCPQiajjICPK/5JAX9aMcHi8je0N7AEYxSHY tcnA== 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=Hm3N3CUlxXRBXqf7e3XiIHtpRuBL112Zzp/Y6lHhCj8=; b=dtqSuRDkUEFAF95x5QI12K/gXMzj2LPpboFNkKLaYqrRONMDW0UE0DY/Kznov4DS3X Qe+C4ya67qTbbUiaS3Ugv4Ifn+ZMepanIts/zBa6LYpLuKOcCEWJaWW0ABu4mZdwBGDX yHUVKPkodPK65/8Skrmc/KEawnrBM+L1SE4YqgFhQM+wBMDiu74WBm15YRm+TdrlyhSV 55lWFqTeSfs6Uv+Jwy84i7sIumpuElYCbXynmEJIUgS48Zuklo8Ty6CvbdkP5Vl4DxN5 ifaeQzdUiQahgdOTgHrFmYVO/tcS1m1ocqWy8qcko/qCfeBiomBprIB8hy2Sw6IsAsJZ K3Jg== X-Forwarded-Encrypted: i=1; AFNElJ/b3QeEtvFyVLBZqNfYrrX0Po3tBVN9VUxIcOjTQt7TUXAATC2mSXeyOiF2C+phd8Kra1/JIrQqaV8UWxU=@vger.kernel.org X-Gm-Message-State: AOJu0YyFWMGElbLpo0rzY3ZijETuCOlx6h0+EY9IgmeZWEmursTBZw8U Rg7IGTdkwhPHnt5O3tnkDLbQHawEu/a+/DCby7vs729S4+8P30FB3ByXMKfx37DoW7vYCbQVbbZ Zqjt9cS8FNKkGLA== X-Received: from pfbdu16.prod.google.com ([2002:a05:6a00:2b50:b0:82f:9778:a429]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:a254:b0:82f:2226:5aeb with SMTP id d2e1a72fcca58-83f03fe5b91mr112295b3a.20.1778615497372; Tue, 12 May 2026 12:51:37 -0700 (PDT) Date: Tue, 12 May 2026 19:51:34 +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-3-skhawaja@google.com> Subject: [PATCH 2/3] kho: Add helper function to check if pages are preserved 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-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Kunit test needs to check whether the pages are preserved after preservation or after unpreserve. The helper function added is only enabled when kunit is enabled. Signed-off-by: Samiullah Khawaja --- include/linux/kexec_handover.h | 5 +++ kernel/liveupdate/kexec_handover.c | 52 ++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h index 8968c56d2d73..a3afc74cf41c 100644 --- a/include/linux/kexec_handover.h +++ b/include/linux/kexec_handover.h @@ -32,6 +32,11 @@ void kho_restore_free(void *mem); struct folio *kho_restore_folio(phys_addr_t phys); struct page *kho_restore_pages(phys_addr_t phys, unsigned long nr_pages); void *kho_restore_vmalloc(const struct kho_vmalloc *preservation); + +#if IS_ENABLED(CONFIG_KUNIT) +bool kho_test_pages_preserved(phys_addr_t phys, unsigned long nr_pages); +#endif + int kho_add_subtree(const char *name, void *blob, size_t size); void kho_remove_subtree(void *blob); int kho_retrieve_subtree(const char *name, phys_addr_t *phys, size_t *size= ); diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_h= andover.c index 1375291d9b07..f678372b3e81 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -12,6 +12,7 @@ =20 #include #include +#include #include #include #include @@ -1771,3 +1772,54 @@ int kho_locate_mem_hole(struct kexec_buf *kbuf, =20 return ret =3D=3D 1 ? 0 : -EADDRNOTAVAIL; } + +#if IS_ENABLED(CONFIG_KUNIT) +static bool _kho_is_pfn_preserved(struct kho_radix_tree *tree, + unsigned long pfn, unsigned int order) +{ + unsigned long key =3D kho_radix_encode_key(PFN_PHYS(pfn), order); + struct kho_radix_node *node =3D tree->root; + struct kho_radix_leaf *leaf; + unsigned int i, idx; + + if (!tree->root) + return false; + + guard(mutex)(&tree->lock); + + for (i =3D KHO_TREE_MAX_DEPTH - 1; i > 0; i--) { + idx =3D kho_radix_get_table_index(key, i); + + if (!node->table[idx]) + return false; + + node =3D phys_to_virt(node->table[idx]); + } + + idx =3D kho_radix_get_bitmap_index(key); + leaf =3D (struct kho_radix_leaf *)node; + + return test_bit(idx, leaf->bitmap); +} + +bool kho_test_pages_preserved(phys_addr_t phys, unsigned long nr_pages) +{ + struct kho_radix_tree *tree =3D &kho_out.radix_tree; + unsigned long pfn =3D PHYS_PFN(phys); + unsigned long end_pfn; + unsigned int order; + + end_pfn =3D pfn + nr_pages; + while (pfn < end_pfn) { + order =3D min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn)); + + if (!_kho_is_pfn_preserved(tree, pfn, order)) + return false; + + pfn +=3D 1 << order; + } + + return true; +} +EXPORT_SYMBOL_IF_KUNIT(kho_test_pages_preserved); +#endif --=20 2.54.0.563.g4f69b47b94-goog From nobody Fri Jun 12 20:19:56 2026 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-Transfer-Encoding: quoted-printable 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=3Doff'. =20 +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 :=3D \ obj-$(CONFIG_KEXEC_HANDOVER) +=3D kexec_handover.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUG) +=3D kexec_handover_debug.o obj-$(CONFIG_KEXEC_HANDOVER_DEBUGFS) +=3D kexec_handover_debugfs.o +obj-$(CONFIG_KEXEC_HANDOVER_KUNIT_TEST) +=3D kexec_handover_test.o =20 obj-$(CONFIG_LIVEUPDATE) +=3D luo.o diff --git a/kernel/liveupdate/kexec_handover_test.c b/kernel/liveupdate/ke= xec_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 =3D 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 =3D 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 =3D 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 =3D alloc_pages(GFP_KERNEL | __GFP_ZERO, 1); + KUNIT_ASSERT_NOT_NULL(test, page); + + /* Preserve and verify that pages are preserved */ + err =3D 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 =3D 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 =3D folio_alloc(GFP_KERNEL | __GFP_ZERO, 1); + KUNIT_ASSERT_NOT_NULL(test, folio); + + nr_pages =3D 1 << folio_order(folio); + + /* Preserve and verify that folio is preserved */ + err =3D 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 =3D 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(foli= o)), nr_pages)); + + folio_put(folio); +} + +static struct kunit_case kho_test_cases[] =3D { + 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 =3D { + .name =3D "kho_test", + .init =3D kho_test_init, + .test_cases =3D 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"); --=20 2.54.0.563.g4f69b47b94-goog