From mboxrd@z Thu Jan 1 00:00:00 1970 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-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_handover.c index 1375291d9b07..f678372b3e81 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -12,6 +12,7 @@ #include #include +#include #include #include #include @@ -1771,3 +1772,54 @@ 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 = tree->root; + struct kho_radix_leaf *leaf; + unsigned int i, idx; + + if (!tree->root) + return false; + + guard(mutex)(&tree->lock); + + 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 = min(count_trailing_zeros(pfn), ilog2(end_pfn - 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.563.g4f69b47b94-goog