From nobody Sat Jul 25 20:48:39 2026 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.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 5DC9935E1AA for ; Mon, 13 Jul 2026 20:49:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783975782; cv=none; b=Ypk+UbeLjhNSGpxpJXrk8GPuqKBGL2uQcyUHDLX9rwvICv14LZ8cX7RhzwNt4nb1Eu6cy2s59Voc7mamWohIItZM+tPiHEfav4wPdvpXiL0Y26FAsSZ9fG+s6U+IrZHWeWH1C8JT/M6rLOqSnziE9gaknGX8dMXTB6oOxrcLORs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783975782; c=relaxed/simple; bh=KXWsv30gE2s0GMl2XfNulLmfi185CojKSIxyONXqRFw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b7LuQk61tVw+YPnDqawEllQ8iemu94P2LVCGQGEnZsXPh4vBgXSN8f2aFkEGM+xegKEAFzFm3in9Lc8fLRCdrYWvv/r5znPTmS6ILTUe4uQBgmutUygZk53wmpTzT5mqHIGjKVMAnfbl3EGzaRqsWZk4JaDFFj0bz+RS3ewyLdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--praan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=p5i1qy3W; arc=none smtp.client-ip=209.85.210.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--praan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p5i1qy3W" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-84a251c2e3eso357263b3a.1 for ; Mon, 13 Jul 2026 13:49:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1783975781; x=1784580581; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=hg67XY3VqVr1xT4YbJfB+2ZOPF2cG732AuNnOCY2r7I=; b=p5i1qy3WK3tEfli+Y0tHqY63oMH9SuqZxbreHTIUV2lAsT2IjV745xcQm9a86HHNrk 9fZ3LWjFXAxHOAVF7FO7u/hJJQwkcrXMOiRp41s31qdidGF3kIJMtqY87JqPIeKQokOJ o6a5SA7sgXOO4RdCrUX7FaLA+6CeIdjKZ2guwujEyNSEYrlTinvG30mHRco+sw8Zm/3C 6B8BQGHj3svrDxpiCjzk04Oyco1TLBqqmfwBbRLF5S76vmoOvIXi767LiyJwWGdrrKuT UpqIFC9ZMU9asYiHZmND4mtdsY1BBgROSseUOTVMhry6XZ6/TWfRPP7xwj8yahfVE58n Phgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783975781; x=1784580581; h=content-type: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:content-type; bh=hg67XY3VqVr1xT4YbJfB+2ZOPF2cG732AuNnOCY2r7I=; b=WcbmvxbprnMkABrZ5DrqDaYoVDliSeiMh/jNzd35lWinqgcbNL/r07YKk6sL7+XDUQ HDhZtg3qtqJ2Mq/ET3mrk3EPLI6bNtmIiiBAWuFfZQI0x6AdjL3eOQzrmaEnbZqr0SYt rTZKy64sdx06J3bc8HPRbLO3/kTi0S5UVKmj3pg88b20hgt6X6IhkFcGSGtetxIB5beB oLDlRsnT8DnMrXS45i1+ZKrJBtA7ogFKfFRZ4vZAMVwKXhyB1/V26qiY4Mux1pYnhg0V g4KaIrgxp2x9zWHt1ZDNMen2UTRalYn5BqkStcAuTCJv9fjChsb1sx+xxP/DynfMzUGS Sprw== X-Forwarded-Encrypted: i=1; AHgh+RoM6Pviq6qPkx+Wnsl0LcV1WluyXOsdC6XTSuKx97HasIG8g2b1gN5zAkD9RW67bsWarej3NF7Htp+KylE=@vger.kernel.org X-Gm-Message-State: AOJu0YwaReQ4CF5HIU/LIHBR2tBndltvg6vqf9L7h1m85CpiPXvECONi gKvR0K4XS84xdPQPRMjH7QgtFplwgaywV3l6gtbaxpJQb0kxM9FbJYji7nd7s90jS0qdHECYzqG LAw== X-Received: from pgl36.prod.google.com ([2002:a63:b24:0:b0:c89:2504:2df5]) (user=praan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:6f01:b0:3b3:ccd0:cf62 with SMTP id adf61e73a8af0-3c0f0c11140mr14952998637.36.1783975780428; Mon, 13 Jul 2026 13:49:40 -0700 (PDT) Date: Mon, 13 Jul 2026 20:49:34 +0000 In-Reply-To: <20260713204935.3069000-1-praan@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260713204935.3069000-1-praan@google.com> X-Mailer: git-send-email 2.55.0.141.g00534a21ce-goog Message-ID: <20260713204935.3069000-2-praan@google.com> Subject: [RFC PATCH v2 1/2] kho: Introduce a helper to init unsplit pages From: Pranjal Shrivastava To: Mike Rapoport , Pasha Tatashin , Pratyush Yadav Cc: Alexander Graf , Samiullah Khawaja , David Matlack , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Pranjal Shrivastava Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The current KHO restoration logic assumes all multi-page blocks are split into independent 4KB pages. Break out a helper to prepare for supporting high-order non-compound pages. Extract kho_init_unsplit_pages() to handle the refcount pattern where only the head page is refcounted. Use the helper for folio restoration that requires a similar refcount logic. Signed-off-by: Pranjal Shrivastava Reviewed-by: Samiullah Khawaja --- kernel/liveupdate/kexec_handover.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_h= andover.c index 4834a809985a..07f6f453cd69 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -357,6 +357,24 @@ int kho_radix_walk_tree(struct kho_radix_tree *tree, } EXPORT_SYMBOL_GPL(kho_radix_walk_tree); =20 +/* For physically contiguous pages. */ +static void kho_init_unsplit_pages(struct page *page, unsigned int order) +{ + unsigned long nr_pages =3D (1UL << order); + + /* Head page gets refcount of 1. */ + set_page_count(page, 1); + /* Clear head page's codetag to avoid accounting mismatch. */ + clear_page_tag_ref(page); + + /* For high-order blocks, tail pages get a page count of zero. */ + for (unsigned long i =3D 1; i < nr_pages; i++) { + set_page_count(page + i, 0); + /* Clear each page's codetag to avoid accounting mismatch. */ + clear_page_tag_ref(page + i); + } +} + /* For physically contiguous 0-order pages. */ static void kho_init_pages(struct page *page, unsigned long nr_pages) { @@ -369,16 +387,7 @@ static void kho_init_pages(struct page *page, unsigned= long nr_pages) =20 static void kho_init_folio(struct page *page, unsigned int order) { - unsigned long nr_pages =3D (1 << order); - - /* Head page gets refcount of 1. */ - set_page_count(page, 1); - /* Clear head page's codetag to avoid accounting mismatch. */ - clear_page_tag_ref(page); - - /* For higher order folios, tail pages get a page count of zero. */ - for (unsigned long i =3D 1; i < nr_pages; i++) - set_page_count(page + i, 0); + kho_init_unsplit_pages(page, order); =20 if (order > 0) prep_compound_page(page, order); --=20 2.55.0.141.g00534a21ce-goog From nobody Sat Jul 25 20:48:39 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 88DE735AC01 for ; Mon, 13 Jul 2026 20:49:43 +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=1783975785; cv=none; b=W1t4w5QM2hNLsQERBMd6mP2v9JFqMn8Yen482nHHPWIkPPk7yQHRCn8LSdLxpmQdabQQ/FDdIT8LZ90N0qURm2JQexfvzBCUXRsKxZiExE7uue+vuloLFPHokGPUKhYOTb/Zwu6UZdWct5f1A7FKkKLEeYwrwELAkc2stgu/NTk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783975785; c=relaxed/simple; bh=XnyLaMQxfisBlvRwYjTD0FnbUPRK6nR5CMCcLrbeirA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cu7NIkfBOot5eZoGhcqA2Yn4N18ifzgM/MyDcpJA9IeC82z0mV6iTJuFdiXKYor/TAxQdRqOX9k1EdZ/MLejskXEn4Lp/OgGLHZHJf4Ra38IOOBsvb+FC7YpXEYPYXaQ6UHZiokE/IIgO1EPglPhRNIpsoseoDr+CploM+Oqd+M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--praan.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=U4LyshFK; 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--praan.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="U4LyshFK" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-c889d1eedcdso5878188a12.1 for ; Mon, 13 Jul 2026 13:49:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1783975783; x=1784580583; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=xzJzBgZ7BaDqZpOvHA29oVPMSyBnofRxze7m51B0LYQ=; b=U4LyshFKIdE4hibRbAp+t09OxeLg0yfhObOwodUEKF10m59H3BlgSMxCfr/9YvyOwY Mz+vPuBovEjo+RmE9ltYnUlSTirgBi7udQkYp0uStyAlOQitfvndRCMmTK5NUbNJ8i1P MeZIJgzqU4VaoLnApqA/xCP/Bzz8jQO8H5EUiPrHfdtkUxMq+jmop2TWHXnVNlsTnxTD odCTAmc6Hg6Mv6gxmMw6ydwBBXWsazq+VZlnLzD+Plerku/c2nf/8rMcRF0qLaHBAZle InM5vfu1yHJqhzHzvv3NFKQtn/xiWKDtkYSIgmQDo1pi0yfQJ3KFO1jE3bdod/99Q5S9 e8bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783975783; x=1784580583; h=content-type: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:content-type; bh=xzJzBgZ7BaDqZpOvHA29oVPMSyBnofRxze7m51B0LYQ=; b=oVZnTu0QuTyv/SaWditPfIWeE4BQAwyBaGvZ1d5OcZ7/oZpLUhZKs4YUEAfiB7+yPV zfN+9/7khqXnAjDOJJPNQCJuJrM3zS2yhcXRKLHcYsN0zlmQHdfdhuZwCR8XCmoZiwBs KoEEEcyGX6b/k6QIXJv47SWcyA8dVLdxFXZLSNyViH1TpkIrpblaWoudf6URVORzwu9m ZN5x3tKnNCf1z3GyEPc1W+ssCH8Z7DOff6ymyyuNpu8huy1/nZOpQ6Qaei64B3sLqveq HjSjzei1kpGvIFazloKPYJlYOGn4qV66SF6kwGNKBX2wkzn7XXWNHQ60dZ/G8jrXepFR /cng== X-Forwarded-Encrypted: i=1; AHgh+Rq39Bpr7RWyWDXXJHSl6DyDVlpi7RukG/wluAJLBymV9VYmb7l+/OUzoWupCYSF6BDmO988jaxXI1A7iGU=@vger.kernel.org X-Gm-Message-State: AOJu0Yz6c6ulzfcXZba14/gyyjlBCxE+LyFprNL+sOn95Xvgr4qKhxvH HvHIX+v1eQ3tHP1V0g/T7dLeltVNk8/LleD889X1XVXYezNFFAKVmNQoZVUn6qk3gynqrabPyXV 54A== X-Received: from pgh2.prod.google.com ([2002:a05:6a02:4e02:b0:c86:2164:9671]) (user=praan job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c6cb:b0:3aa:c964:3c27 with SMTP id adf61e73a8af0-3c34d719d0bmr820808637.46.1783975782789; Mon, 13 Jul 2026 13:49:42 -0700 (PDT) Date: Mon, 13 Jul 2026 20:49:35 +0000 In-Reply-To: <20260713204935.3069000-1-praan@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260713204935.3069000-1-praan@google.com> X-Mailer: git-send-email 2.55.0.141.g00534a21ce-goog Message-ID: <20260713204935.3069000-3-praan@google.com> Subject: [RFC PATCH v2 2/2] kho: Introduce preserve/restore APIs for unsplit pages From: Pranjal Shrivastava To: Mike Rapoport , Pasha Tatashin , Pratyush Yadav Cc: Alexander Graf , Samiullah Khawaja , David Matlack , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Pranjal Shrivastava Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The current KHO page preservation APIs (e.g. kho_preserve_pages) assume that multi-page blocks are split into independent 4KB pages during restoration. This is incompatible with high-order non-compound pages, such as DMA buffers, which must be restored with tail pages having a zero reference count. Introduce explicit preserve and restore APIs for unsplit pages, which preserve and restore a high-order page block as a single unit, applying a refcount of 1 to the head page while leaving tail pages at 0. Rename the existing internal helper to __kho_restore_page(). Signed-off-by: Pranjal Shrivastava --- include/linux/kexec_handover.h | 10 ++++ kernel/liveupdate/kexec_handover.c | 86 ++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 4 deletions(-) diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h index 8968c56d2d73..6e2c75b16209 100644 --- a/include/linux/kexec_handover.h +++ b/include/linux/kexec_handover.h @@ -22,6 +22,8 @@ bool is_kho_boot(void); =20 int kho_preserve_folio(struct folio *folio); void kho_unpreserve_folio(struct folio *folio); +int kho_preserve_page(struct page *page, unsigned int order); +void kho_unpreserve_page(struct page *page, unsigned int order); int kho_preserve_pages(struct page *page, unsigned long nr_pages); void kho_unpreserve_pages(struct page *page, unsigned long nr_pages); int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation); @@ -30,6 +32,7 @@ void *kho_alloc_preserve(size_t size); void kho_unpreserve_free(void *mem); void kho_restore_free(void *mem); struct folio *kho_restore_folio(phys_addr_t phys); +struct page *kho_restore_page(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); int kho_add_subtree(const char *name, void *blob, size_t size); @@ -65,6 +68,13 @@ static inline int kho_preserve_pages(struct page *page, = unsigned int nr_pages) =20 static inline void kho_unpreserve_pages(struct page *page, unsigned int nr= _pages) { } =20 +static inline int kho_preserve_page(struct page *page, unsigned int order) +{ + return -EOPNOTSUPP; +} + +static inline void kho_unpreserve_page(struct page *page, unsigned int ord= er) { } + static inline int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation) { diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_h= andover.c index 07f6f453cd69..43ae3e2de5da 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -393,7 +393,7 @@ static void kho_init_folio(struct page *page, unsigned = int order) prep_compound_page(page, order); } =20 -static struct page *kho_restore_page(phys_addr_t phys, bool is_folio) +static struct page *__kho_restore_page(phys_addr_t phys, bool is_folio) { struct page *page =3D pfn_to_online_page(PHYS_PFN(phys)); unsigned long nr_pages; @@ -432,12 +432,44 @@ 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 =3D __kho_restore_page(phys, true); =20 return page ? page_folio(page) : NULL; } EXPORT_SYMBOL_GPL(kho_restore_folio); =20 +/** + * kho_restore_page - restore a higher-order unsplit page block. + * @phys: physical address of the first page. + * + * Restore a higher-order unsplit page block that was preserved with + * kho_preserve_page(). + * + * Return: the head page on success, NULL on failure. + */ +struct page *kho_restore_page(phys_addr_t phys) +{ + struct page *page =3D pfn_to_online_page(PHYS_PFN(phys)); + unsigned long nr_pages; + union kho_page_info info; + + if (!page) + return NULL; + + info.page_private =3D page->private; + if (WARN_ON_ONCE(info.magic !=3D KHO_PAGE_MAGIC)) + return NULL; + + nr_pages =3D (1UL << info.order); + page->private =3D 0; + + kho_init_unsplit_pages(page, info.order); + adjust_managed_page_count(page, nr_pages); + + return page; +} +EXPORT_SYMBOL_GPL(kho_restore_page); + /** * kho_restore_pages - restore list of contiguous order 0 pages. * @phys: physical address of the first page. @@ -457,7 +489,7 @@ struct page *kho_restore_pages(phys_addr_t phys, unsign= ed long nr_pages) while (pfn < end_pfn) { const unsigned int order =3D min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn)); - struct page *page =3D kho_restore_page(PFN_PHYS(pfn), false); + struct page *page =3D __kho_restore_page(PFN_PHYS(pfn), false); =20 if (!page) return NULL; @@ -890,6 +922,51 @@ void kho_unpreserve_folio(struct folio *folio) } EXPORT_SYMBOL_GPL(kho_unpreserve_folio); =20 +/** + * kho_preserve_page - preserve a higher-order "unsplit" page block. + * @page: head page of the block. + * @order: order of the allocation. + * + * Instructs KHO to preserve a higher-order contiguous allocation (like a = DMA + * buffer) as a single unit. It must be restored using kho_restore_page() = to + * ensure the tail pages are correctly initialized with a zero refcount. + * + * If a driver needs to split a block that has been preserved with this + * function, it must first unpreserve the block using kho_unpreserve_page(= ), + * perform the split, and then re-preserve the individual pages using + * kho_preserve_pages(). + * + * Return: 0 on success, error code on failure + */ +int kho_preserve_page(struct page *page, unsigned int order) +{ + struct kho_radix_tree *tree =3D &kho_out.radix_tree; + const unsigned long pfn =3D page_to_pfn(page); + + if (WARN_ON(kho_scratch_overlap(pfn << PAGE_SHIFT, PAGE_SIZE << order))) + return -EINVAL; + + return kho_radix_add_page(tree, pfn, order); +} +EXPORT_SYMBOL_GPL(kho_preserve_page); + +/** + * kho_unpreserve_page - unpreserve a higher-order "unsplit" page block. + * @page: head page of the block. + * @order: order of the allocation. + * + * Instructs KHO to unpreserve a high-order block that was preserved by + * kho_preserve_page() before. + */ +void kho_unpreserve_page(struct page *page, unsigned int order) +{ + struct kho_radix_tree *tree =3D &kho_out.radix_tree; + const unsigned long pfn =3D page_to_pfn(page); + + kho_radix_del_page(tree, pfn, order); +} +EXPORT_SYMBOL_GPL(kho_unpreserve_page); + static unsigned int __kho_preserve_pages_order(unsigned long start_pfn, unsigned long end_pfn) { @@ -927,7 +1004,8 @@ static void __kho_unpreserve(struct kho_radix_tree *tr= ee, * @nr_pages: number of pages. * * Preserve a contiguous list of order 0 pages. Must be restored using - * kho_restore_pages() to ensure the pages are restored properly as order = 0. + * kho_restore_pages() to ensure the pages are restored properly as order 0 + * with each page having a reference count of 1 (split). * * Return: 0 on success, error code on failure */ --=20 2.55.0.141.g00534a21ce-goog