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 9045F39A047 for ; Thu, 21 May 2026 19:32:05 +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=1779391926; cv=none; b=oSbqrSgwyrxsqRYoF/Yul5/rfUZOAMvRgHRTT7bMStKwBFT/jbR6R0/5l/xS6OjU0llTG6CVhxeFocyC8ABnzA2Aks41zehOxAx43tKPS4PsI8LjcQb7D0EuYz5Zq6PGHBdlW1IOsgGxS0UFrLTiB/dkPRguHdiBPmCWr7Dd1Es= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779391926; c=relaxed/simple; bh=WhD+jJXvDqJJs6pNZlYlwoBY2bmSJVd0oXI2uRY+f+Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PkCLrRjMlDCQXtBYxDYfQxXHjJW0cdWVaXc2GZWJDHlMRQDrKIV+S4RWqypdnEWt5fYVjgMlR3Qds0uZgvn6aRH7eWgVdCDmJ11k2n52TfdU6MlfGqrQ+YYC1AHRr3tctKxmf2lQStWqZOWhQts3G9P4kccLRgBmsG5AR7pkuEI= 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=MU2Yy1lP; 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="MU2Yy1lP" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-837c4eb3bdfso3994403b3a.2 for ; Thu, 21 May 2026 12:32:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779391925; x=1779996725; 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=T5K2FdXzRnABLtF63rsnxGYJOAKntAeJNMkiCQxBi/I=; b=MU2Yy1lPAzupeHmXX28ixvXv+FXVbyYeW2rwNsTZhGk5SPAfmNskNa6IUr0jwi8kh8 lVyOYcCK4gIfoBr38532e08bQe5MmAV2KhER6Ly5529xcXU0JwbI0odoD7IcoReAPphW yhg5d+N7NZTjCwk9gA3ROlRGq7ZP21/IBA01nis5LGnw93/N6dEtzpG3G0vR1uhgx9eK DTUw+Tos1qBQmj0b5JrmECfJWTtD17V9ZdpURSneMbdb1XyClgvd/eele5TPbUqoGVIH kChuBokzT4eCejvuSyFaaxXggmpMqn7lWd1tErFFS0PfiB8qhLSZvUNRBqB0J/nvWW6u SBtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779391925; x=1779996725; 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=T5K2FdXzRnABLtF63rsnxGYJOAKntAeJNMkiCQxBi/I=; b=l72LlbmKG8jYoR5JcvyLb4TesMrprjYSK5y1zZDtsvtFQsvq1Dy4lCpZJy3w5rW2Su c4cMXI6Tl5msXyQiZC7Y743cuLoazAh/H0zr6aQRdV6zBUGWyKKEP8UqsnofOaGhlM7D v1A/mgwysaWghSviUcMEeyBE347KzLz6akVtoY88zl30fgA6khwRCMKednSN3iRAjUGz +U1bxOc754fYW6MxwbzZ+LrixFw3kEDiEkn7fbrz1XpEDPahapy6DQtad8uvKJWMAaLC dNiI/z6/dBnu3riWkYbLV2vPiYZiwG+2SxTEtlAezJG4WPv/FHSWYVx5bnY3hDPyx87r 3N+g== X-Forwarded-Encrypted: i=1; AFNElJ8GWlOv3UR7B2EIVLkqBpTO8uL9r/QR6J6I3LJebSjGad1507OM4uMesPQP3kx1xAlRLY3TT8gzEiRVFgg=@vger.kernel.org X-Gm-Message-State: AOJu0YyV2bQm/k4JDd/MxxP73muinPx/42b3JNnPdPirTjTmHp5DeZjn fCrnL6/MIXfxSXs8YHoOb5byu0i3IGGhf/xBecdRHABoYyw0jEgCwW+ZNGSgK0a2/CpTFZa6UjU SVPO4Xdumktlm+A== X-Received: from pfwp44.prod.google.com ([2002:a05:6a00:26ec:b0:83f:75b6:75ac]) (user=skhawaja job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1d81:b0:837:42a6:589d with SMTP id d2e1a72fcca58-8415f129f9amr521581b3a.11.1779391924486; Thu, 21 May 2026 12:32:04 -0700 (PDT) Date: Thu, 21 May 2026 19:32:01 +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-3-skhawaja@google.com> Subject: [PATCH v2 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-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 | 53 ++++++++++++++++++++++++++++++ 2 files changed, 58 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_handover.c index 902019f36c24..abe201825e83 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -11,6 +11,7 @@ #define pr_fmt(fmt) "KHO: " fmt #include +#include #include #include @@ -1775,3 +1776,55 @@ int kho_locate_mem_hole(struct kexec_buf *kbuf, return ret == 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 = kho_radix_encode_key(PFN_PHYS(pfn), order); + struct kho_radix_node *node; + struct kho_radix_leaf *leaf; + unsigned int i, idx; + + if (!tree->root) + return false; + + guard(mutex)(&tree->lock); + + node = tree->root; + for (i = KHO_TREE_MAX_DEPTH - 1; i > 0; i--) { + idx = kho_radix_get_table_index(key, i); + + if (!node->table[idx]) + return false; + + node = phys_to_virt(node->table[idx]); + } + + idx = kho_radix_get_bitmap_index(key); + leaf = (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 = &kho_out.radix_tree; + unsigned long pfn = PHYS_PFN(phys); + unsigned long end_pfn; + unsigned int order; + + end_pfn = pfn + nr_pages; + while (pfn < end_pfn) { + order = __kho_preserve_pages_order(pfn, end_pfn); + + if (!_kho_is_pfn_preserved(tree, pfn, order)) + return false; + + pfn += 1 << order; + } + + return true; +} +EXPORT_SYMBOL_IF_KUNIT(kho_test_pages_preserved); +#endif -- 2.54.0.746.g67dd491aae-goog