From nobody Thu Oct 2 10:49:39 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3DC827E040 for ; Wed, 17 Sep 2025 17:40:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130844; cv=none; b=WICbIulVYK+EuqPfMq3MfMGUE43Xfz6fyisQunZd+3Kfy72fbQQybZaiNjFYi1DZ3di2kbtLwwZfHDTKjoOgLywgwtOeJabPKMlMSyk/VJ6khXJILSYTNv0QUuXbbGIFHd/LqB5Wei8OI+/CRGkEvohTPJAcmywRrm69qm8zIq4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130844; c=relaxed/simple; bh=8S8e7n6CmQnQKabzKFlVH8Uaq/Xv4oQU9mDtf6delh4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=e+EPDHBzdoMCxr8ULkpMZMAGgP6iHuhu+kJLtr9K8epDnUosap5dkChKlBA3teSJ3SUKQchsO9fi+EcbmQA1plTdBgnMH2WrzqkdysyDLWu6Tbn23rCVSN/GTVwzHPzR/cGKik6uKM7TZ2mfnUpW+L/28pE/vb5mmx/IZ5CNttw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=imKc2B/G; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="imKc2B/G" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 56019C4CEE7; Wed, 17 Sep 2025 17:40:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758130844; bh=8S8e7n6CmQnQKabzKFlVH8Uaq/Xv4oQU9mDtf6delh4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=imKc2B/Go7tqGnVzKJxq26RWe2jdsBf9Ed5PIKotck8RwFHLfGS0BBPjt8DjrQzwQ PoZq1MNENH7vxNPLowth8cqCBY6VFfgrh/wMPvNHtXoc1QdyvzGZ8KBajL+RtHpAKH BIqzDAu8/OgWWkBn++4a8QixTRL1Z/m4JEbIaMfM7i121nzr00gzHlLgQwfikkq8ZO /7V0AUJnUyLyc32HBkNk+A9WDN+0OI8eYdqYYVDkT5cugq5KiRquwEcou1kuW/iU7K 1NOiI35NaZo02txKyCoqSbdnRtMfhvXTf4yEpXQS4PPwgGf9N6kV+jGCJhpSJ0ABX5 huHkkfAT7XbvA== From: Mike Rapoport To: Andrew Morton Cc: Alexander Graf , Baoquan He , Changyuan Lyu , Chris Li , Jason Gunthorpe , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 1/4] kho: check if kho is finalized in __kho_preserve_order() Date: Wed, 17 Sep 2025 20:40:30 +0300 Message-ID: <20250917174033.3810435-2-rppt@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250917174033.3810435-1-rppt@kernel.org> References: <20250917174033.3810435-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" Instead of checking if kho is finalized in each caller of __kho_preserve_order(), do it in the core function itself. Signed-off-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- kernel/kexec_handover.c | 55 +++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/kernel/kexec_handover.c b/kernel/kexec_handover.c index 8079fc4b9189..f421acc58c1f 100644 --- a/kernel/kexec_handover.c +++ b/kernel/kexec_handover.c @@ -91,6 +91,29 @@ struct kho_serialization { struct khoser_mem_chunk *preserved_mem_map; }; =20 +struct kho_out { + struct blocking_notifier_head chain_head; + + struct dentry *dir; + + struct mutex lock; /* protects KHO FDT finalization */ + + struct kho_serialization ser; + bool finalized; +}; + +static struct kho_out kho_out =3D { + .chain_head =3D BLOCKING_NOTIFIER_INIT(kho_out.chain_head), + .lock =3D __MUTEX_INITIALIZER(kho_out.lock), + .ser =3D { + .fdt_list =3D LIST_HEAD_INIT(kho_out.ser.fdt_list), + .track =3D { + .orders =3D XARRAY_INIT(kho_out.ser.track.orders, 0), + }, + }, + .finalized =3D false, +}; + static void *xa_load_or_alloc(struct xarray *xa, unsigned long index, size= _t sz) { void *elm, *res; @@ -149,6 +172,9 @@ static int __kho_preserve_order(struct kho_mem_track *t= rack, unsigned long pfn, =20 might_sleep(); =20 + if (kho_out.finalized) + return -EBUSY; + physxa =3D xa_load(&track->orders, order); if (!physxa) { int err; @@ -640,29 +666,6 @@ int kho_add_subtree(struct kho_serialization *ser, con= st char *name, void *fdt) } EXPORT_SYMBOL_GPL(kho_add_subtree); =20 -struct kho_out { - struct blocking_notifier_head chain_head; - - struct dentry *dir; - - struct mutex lock; /* protects KHO FDT finalization */ - - struct kho_serialization ser; - bool finalized; -}; - -static struct kho_out kho_out =3D { - .chain_head =3D BLOCKING_NOTIFIER_INIT(kho_out.chain_head), - .lock =3D __MUTEX_INITIALIZER(kho_out.lock), - .ser =3D { - .fdt_list =3D LIST_HEAD_INIT(kho_out.ser.fdt_list), - .track =3D { - .orders =3D XARRAY_INIT(kho_out.ser.track.orders, 0), - }, - }, - .finalized =3D false, -}; - int register_kho_notifier(struct notifier_block *nb) { return blocking_notifier_chain_register(&kho_out.chain_head, nb); @@ -690,9 +693,6 @@ int kho_preserve_folio(struct folio *folio) const unsigned int order =3D folio_order(folio); struct kho_mem_track *track =3D &kho_out.ser.track; =20 - if (kho_out.finalized) - return -EBUSY; - return __kho_preserve_order(track, pfn, order); } EXPORT_SYMBOL_GPL(kho_preserve_folio); @@ -716,9 +716,6 @@ int kho_preserve_phys(phys_addr_t phys, size_t size) int err =3D 0; struct kho_mem_track *track =3D &kho_out.ser.track; =20 - if (kho_out.finalized) - return -EBUSY; - if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size)) return -EINVAL; =20 --=20 2.50.1 From nobody Thu Oct 2 10:49:39 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AA11727E040 for ; Wed, 17 Sep 2025 17:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130848; cv=none; b=oDoLUKw5ycaW+ctbfGylNKqJE+Uba4RcA1OBCET6/gP1BsfAzSKEPAl5JWTxdDsdb7J3nlKa2x0TVC8ef7xSt0ZmoyRi0UNBog4qWrD02qNpgfbPHNLlixuy33WMT7aHYzelF0WbzR480fgwRQgU35jXsJABN5XBoQW83U2zLjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130848; c=relaxed/simple; bh=q/t3uKKPwUDe/VS493c2y7ty/sgLNnbkDT1wsgUidLE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=SuvaOagGvhioqSUZBxfWAvxjbRQ1zmChUrabOC4BW1y8SowvTG/tRmzTv5Y7hob5RU5RXwtpT3+AJwqq+8I/uzRp510X9MtyD55j80pUTMuMW+DM6PNIy5EW+f03/Xgncz3DB8ICzIJT2LdvPzDF2frUvsEHi9q8ZGCFlEv0djU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=YWNVC2+N; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="YWNVC2+N" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0CD38C4CEE7; Wed, 17 Sep 2025 17:40:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758130848; bh=q/t3uKKPwUDe/VS493c2y7ty/sgLNnbkDT1wsgUidLE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YWNVC2+NhhjJf+hD1kotVftCFqdoDZkFLt5It7TUxiKYJRf3UOOfk0X3WW8ki+GiX KkU1fJdKFjt9BzSmWLeuCYwjt+e5EmLTt0jbv4eGIc44Ha62Nipr9/l+cYYwTIZ7pX tItwXA4/CVyvwh6t9CLCgV2Kao+l46eSUIVQisWIuqc2ovOv6znBhwjiRvCvTE7BGa W/bqshWp76kkI/KnmtBOrXYSUndLrW+96geZMRXLsVFn198LIzSNc1qw+8aBnnS/oX xEVuk4XZ9Jrn29sG+OgcUNu9/KuS4NoQpBVAWeqqZibjpWtG7MXZEQWmZkcdmZbwgC C0mR3JWJch7gQ== From: Mike Rapoport To: Andrew Morton Cc: Alexander Graf , Baoquan He , Changyuan Lyu , Chris Li , Jason Gunthorpe , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 2/4] kho: replace kho_preserve_phys() with kho_preserve_pages() Date: Wed, 17 Sep 2025 20:40:31 +0300 Message-ID: <20250917174033.3810435-3-rppt@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250917174033.3810435-1-rppt@kernel.org> References: <20250917174033.3810435-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" to make it clear that KHO operates on pages rather than on a random physical address. The kho_preserve_pages() will be also used in upcoming support for vmalloc preservation. Signed-off-by: Mike Rapoport (Microsoft) --- include/linux/kexec_handover.h | 5 +++-- kernel/kexec_handover.c | 25 +++++++++++-------------- mm/memblock.c | 4 +++- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h index 348844cffb13..cc5c49b0612b 100644 --- a/include/linux/kexec_handover.h +++ b/include/linux/kexec_handover.h @@ -18,6 +18,7 @@ enum kho_event { =20 struct folio; struct notifier_block; +struct page; =20 #define DECLARE_KHOSER_PTR(name, type) \ union { \ @@ -42,7 +43,7 @@ struct kho_serialization; bool kho_is_enabled(void); =20 int kho_preserve_folio(struct folio *folio); -int kho_preserve_phys(phys_addr_t phys, size_t size); +int kho_preserve_pages(struct page *page, unsigned int nr_pages); struct folio *kho_restore_folio(phys_addr_t phys); int kho_add_subtree(struct kho_serialization *ser, const char *name, void = *fdt); int kho_retrieve_subtree(const char *name, phys_addr_t *phys); @@ -65,7 +66,7 @@ static inline int kho_preserve_folio(struct folio *folio) return -EOPNOTSUPP; } =20 -static inline int kho_preserve_phys(phys_addr_t phys, size_t size) +static inline int kho_preserve_pages(struct page *page, unsigned int nr_pa= ges) { return -EOPNOTSUPP; } diff --git a/kernel/kexec_handover.c b/kernel/kexec_handover.c index f421acc58c1f..3ad59c5f9eaa 100644 --- a/kernel/kexec_handover.c +++ b/kernel/kexec_handover.c @@ -698,26 +698,23 @@ int kho_preserve_folio(struct folio *folio) EXPORT_SYMBOL_GPL(kho_preserve_folio); =20 /** - * kho_preserve_phys - preserve a physically contiguous range across kexec. - * @phys: physical address of the range. - * @size: size of the range. + * kho_preserve_pages - preserve contiguous pages across kexec + * @page: first page in the list. + * @nr_pages: number of pages. * - * Instructs KHO to preserve the memory range from @phys to @phys + @size - * across kexec. + * Preserve a contiguous list of order 0 pages. Must be restored using + * kho_restore_page() on each order 0 page. * * Return: 0 on success, error code on failure */ -int kho_preserve_phys(phys_addr_t phys, size_t size) +int kho_preserve_pages(struct page *page, unsigned int nr_pages) { - unsigned long pfn =3D PHYS_PFN(phys); + struct kho_mem_track *track =3D &kho_out.ser.track; + const unsigned long start_pfn =3D page_to_pfn(page); + const unsigned long end_pfn =3D start_pfn + nr_pages; + unsigned long pfn =3D start_pfn; unsigned long failed_pfn =3D 0; - const unsigned long start_pfn =3D pfn; - const unsigned long end_pfn =3D PHYS_PFN(phys + size); int err =3D 0; - struct kho_mem_track *track =3D &kho_out.ser.track; - - if (!PAGE_ALIGNED(phys) || !PAGE_ALIGNED(size)) - return -EINVAL; =20 while (pfn < end_pfn) { const unsigned int order =3D @@ -737,7 +734,7 @@ int kho_preserve_phys(phys_addr_t phys, size_t size) =20 return err; } -EXPORT_SYMBOL_GPL(kho_preserve_phys); +EXPORT_SYMBOL_GPL(kho_preserve_pages); =20 /* Handling for debug/kho/out */ =20 diff --git a/mm/memblock.c b/mm/memblock.c index 117d963e677c..6ec3eaa4e8d1 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -2516,8 +2516,10 @@ static int reserve_mem_kho_finalize(struct kho_seria= lization *ser) =20 for (i =3D 0; i < reserved_mem_count; i++) { struct reserve_mem_table *map =3D &reserved_mem_table[i]; + struct page *page =3D phys_to_page(map->start); + unsigned int nr_pages =3D map->size >> PAGE_SHIFT; =20 - err |=3D kho_preserve_phys(map->start, map->size); + err |=3D kho_preserve_pages(page, nr_pages); } =20 err |=3D kho_preserve_folio(page_folio(kho_fdt)); --=20 2.50.1 From nobody Thu Oct 2 10:49:39 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 446A627E040 for ; Wed, 17 Sep 2025 17:40:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130852; cv=none; b=n0ceGlaN4eW4uAFyjYOe77le4ACpjL7Jw5vPV/kWC/1RDAubqd2ExYxzjs/3HhbEwp1ST/9SosSEHOENaDzl9tceH8UhfeJ6mrXm8Dta4AQjNN5O8WLEsw7LkTxJqzEw8FdmJ9O0xX5hG6Kg0aY+WbCrpR5vfecpV/zGslvlTxM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130852; c=relaxed/simple; bh=g9UTSU7mKXHMD706ZTpvo0p32ufAy0oIGmLbfXKXhCc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OrEjvnQj37c6i9IWH4wjQ7CRwsjeNCYf3GMUeMWH6UPyv0kbJ+wWQs/bcjZVv31bjQUvf0qXXzdrQk6YeL5bIKGdwjpjzuFo5+xh9IJNKT2gw8l3ZxfYV2J+hEgbrlEQvcfjVAkNVaKP3vS+94w9IXWHlBV+NxraqgADRTNAIto= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=FAXZMEr8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="FAXZMEr8" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B7778C4CEFA; Wed, 17 Sep 2025 17:40:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758130851; bh=g9UTSU7mKXHMD706ZTpvo0p32ufAy0oIGmLbfXKXhCc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FAXZMEr8KZZlgSJlbISRNQ33VJ5g0BYw/qXtmKT3Bikjm7rG0i2jlf5Gurdno4AxR 1r7kxPBsntpO+BCU/Afz6T7L1GuWNbW2ASSlVJPGeoEEVdwH6PWvanekV7UeDfr1MC s+HFoBm3q2nnY5yzvnWNK3AMVV58pLJXAG9CZC0zqI0YQJJKqsBstjKijtb03VWxAt Ulqw2jU+BDrUiqkrOlSVKXRt9iBQOTgapRnvGICFjPgDdlL54+mwEl07bcKzV4T9K4 aMxkUacPkUj4Pl31p3MHYqNOrU9MFGOa5mEiXdv38InkEV95ZCH20w7oSNVHlrIMtP EzA1Q7VoDi21Q== From: Mike Rapoport To: Andrew Morton Cc: Alexander Graf , Baoquan He , Changyuan Lyu , Chris Li , Jason Gunthorpe , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 3/4] kho: add support for preserving vmalloc allocations Date: Wed, 17 Sep 2025 20:40:32 +0300 Message-ID: <20250917174033.3810435-4-rppt@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250917174033.3810435-1-rppt@kernel.org> References: <20250917174033.3810435-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" A vmalloc allocation is preserved using binary structure similar to global KHO memory tracker. It's a linked list of pages where each page is an array of physical address of pages in vmalloc area. kho_preserve_vmalloc() hands out the physical address of the head page to the caller. This address is used as the argument to kho_vmalloc_restore() to restore the mapping in the vmalloc address space and populate it with the preserved pages. Signed-off-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- include/linux/kexec_handover.h | 21 +++ kernel/kexec_handover.c | 244 +++++++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+) diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h index cc5c49b0612b..1cb515d8257a 100644 --- a/include/linux/kexec_handover.h +++ b/include/linux/kexec_handover.h @@ -39,12 +39,22 @@ struct page; =20 struct kho_serialization; =20 +struct kho_vmalloc_chunk; +struct kho_vmalloc { + DECLARE_KHOSER_PTR(first, struct kho_vmalloc_chunk *); + unsigned int total_pages; + unsigned short flags; + unsigned short order; +}; + #ifdef CONFIG_KEXEC_HANDOVER bool kho_is_enabled(void); =20 int kho_preserve_folio(struct folio *folio); int kho_preserve_pages(struct page *page, unsigned int nr_pages); +int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation); struct folio *kho_restore_folio(phys_addr_t phys); +void *kho_restore_vmalloc(const struct kho_vmalloc *preservation); int kho_add_subtree(struct kho_serialization *ser, const char *name, void = *fdt); int kho_retrieve_subtree(const char *name, phys_addr_t *phys); =20 @@ -71,11 +81,22 @@ static inline int kho_preserve_pages(struct page *page,= unsigned int nr_pages) return -EOPNOTSUPP; } =20 +static inline int kho_preserve_vmalloc(void *ptr, + struct kho_vmalloc *preservation) +{ + return -EOPNOTSUPP; +} + static inline struct folio *kho_restore_folio(phys_addr_t phys) { return NULL; } =20 +static inline void *kho_restore_vmalloc(const struct kho_vmalloc *preserva= tion) +{ + return NULL; +} + static inline int kho_add_subtree(struct kho_serialization *ser, const char *name, void *fdt) { diff --git a/kernel/kexec_handover.c b/kernel/kexec_handover.c index 3ad59c5f9eaa..d670caf6d07f 100644 --- a/kernel/kexec_handover.c +++ b/kernel/kexec_handover.c @@ -18,6 +18,7 @@ #include #include #include +#include =20 #include =20 @@ -736,6 +737,249 @@ int kho_preserve_pages(struct page *page, unsigned in= t nr_pages) } EXPORT_SYMBOL_GPL(kho_preserve_pages); =20 +struct kho_vmalloc_hdr { + DECLARE_KHOSER_PTR(next, struct kho_vmalloc_chunk *); +}; + +#define KHO_VMALLOC_SIZE \ + ((PAGE_SIZE - sizeof(struct kho_vmalloc_hdr)) / \ + sizeof(phys_addr_t)) + +struct kho_vmalloc_chunk { + struct kho_vmalloc_hdr hdr; + phys_addr_t phys[KHO_VMALLOC_SIZE]; +}; + +static_assert(sizeof(struct kho_vmalloc_chunk) =3D=3D PAGE_SIZE); + +/* vmalloc flags KHO supports */ +#define KHO_VMALLOC_SUPPORTED_FLAGS (VM_ALLOC | VM_ALLOW_HUGE_VMAP) + +/* KHO internal flags for vmalloc preservations */ +#define KHO_VMALLOC_ALLOC 0x0001 +#define KHO_VMALLOC_HUGE_VMAP 0x0002 + +static unsigned short vmalloc_flags_to_kho(unsigned int vm_flags) +{ + unsigned short kho_flags =3D 0; + + if (vm_flags & VM_ALLOC) + kho_flags |=3D KHO_VMALLOC_ALLOC; + if (vm_flags & VM_ALLOW_HUGE_VMAP) + kho_flags |=3D KHO_VMALLOC_HUGE_VMAP; + + return kho_flags; +} + +static unsigned int kho_flags_to_vmalloc(unsigned short kho_flags) +{ + unsigned int vm_flags =3D 0; + + if (kho_flags & KHO_VMALLOC_ALLOC) + vm_flags |=3D VM_ALLOC; + if (kho_flags & KHO_VMALLOC_HUGE_VMAP) + vm_flags |=3D VM_ALLOW_HUGE_VMAP; + + return vm_flags; +} + +static struct kho_vmalloc_chunk *new_vmalloc_chunk(struct kho_vmalloc_chun= k *cur) +{ + struct kho_vmalloc_chunk *chunk; + int err; + + chunk =3D (struct kho_vmalloc_chunk *)get_zeroed_page(GFP_KERNEL); + if (!chunk) + return NULL; + + err =3D kho_preserve_pages(virt_to_page(chunk), 1); + if (err) + goto err_free; + if (cur) + KHOSER_STORE_PTR(cur->hdr.next, chunk); + return chunk; + +err_free: + free_page((unsigned long)chunk); + return NULL; +} + +static void kho_vmalloc_unpreserve_chunk(struct kho_vmalloc_chunk *chunk) +{ + struct kho_mem_track *track =3D &kho_out.ser.track; + unsigned long pfn =3D PHYS_PFN(virt_to_phys(chunk)); + + __kho_unpreserve(track, pfn, pfn + 1); + + for (int i =3D 0; chunk->phys[i]; i++) { + pfn =3D PHYS_PFN(chunk->phys[i]); + __kho_unpreserve(track, pfn, pfn + 1); + } +} + +static void kho_vmalloc_free_chunks(struct kho_vmalloc *kho_vmalloc) +{ + struct kho_vmalloc_chunk *chunk =3D KHOSER_LOAD_PTR(kho_vmalloc->first); + + while (chunk) { + struct kho_vmalloc_chunk *tmp =3D chunk; + + kho_vmalloc_unpreserve_chunk(chunk); + + chunk =3D KHOSER_LOAD_PTR(chunk->hdr.next); + kfree(tmp); + } +} + +/** + * kho_preserve_vmalloc - preserve memory allocated with vmalloc() across = kexec + * @ptr: pointer to the area in vmalloc address space + * @preservation: placeholder for preservation metadata + * + * Instructs KHO to preserve the area in vmalloc address space at @ptr. The + * physical pages mapped at @ptr will be preserved and on successful return + * @preservation will hold the physical address of a structure that descri= bes + * the preservation. + * + * NOTE: The memory allocated with vmalloc_node() variants cannot be relia= bly + * restored on the same node + * + * Return: 0 on success, error code on failure + */ +int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation) +{ + struct kho_vmalloc_chunk *chunk; + struct vm_struct *vm =3D find_vm_area(ptr); + unsigned int order, flags, nr_contig_pages; + unsigned int idx =3D 0; + int err; + + if (!vm) + return -EINVAL; + + if (vm->flags & ~KHO_VMALLOC_SUPPORTED_FLAGS) + return -EOPNOTSUPP; + + flags =3D vmalloc_flags_to_kho(vm->flags); + order =3D get_vm_area_page_order(vm); + + chunk =3D new_vmalloc_chunk(NULL); + if (!chunk) + return -ENOMEM; + KHOSER_STORE_PTR(preservation->first, chunk); + + nr_contig_pages =3D (1 << order); + for (int i =3D 0; i < vm->nr_pages; i +=3D nr_contig_pages) { + phys_addr_t phys =3D page_to_phys(vm->pages[i]); + + err =3D kho_preserve_pages(vm->pages[i], nr_contig_pages); + if (err) + goto err_free; + + chunk->phys[idx++] =3D phys; + if (idx =3D=3D ARRAY_SIZE(chunk->phys)) { + chunk =3D new_vmalloc_chunk(chunk); + if (!chunk) + goto err_free; + idx =3D 0; + } + } + + preservation->total_pages =3D vm->nr_pages; + preservation->flags =3D flags; + preservation->order =3D order; + + return 0; + +err_free: + kho_vmalloc_free_chunks(preservation); + return err; +} +EXPORT_SYMBOL_GPL(kho_preserve_vmalloc); + +/** + * kho_restore_vmalloc - recreates and populates an area in vmalloc address + * space from the preserved memory. + * @preservation: preservation metadata. + * + * Recreates an area in vmalloc address space and populates it with memory= that + * was preserved using kho_preserve_vmalloc(). + * + * Return: pointer to the area in the vmalloc address space, NULL on failu= re. + */ +void *kho_restore_vmalloc(const struct kho_vmalloc *preservation) +{ + struct kho_vmalloc_chunk *chunk =3D KHOSER_LOAD_PTR(preservation->first); + unsigned int align, order, shift, vm_flags; + unsigned int idx =3D 0, nr; + unsigned long addr, size; + struct vm_struct *area; + struct page **pages; + int err; + + vm_flags =3D kho_flags_to_vmalloc(preservation->flags); + if (vm_flags & ~KHO_VMALLOC_SUPPORTED_FLAGS) + return NULL; + + nr =3D preservation->total_pages; + pages =3D kvmalloc_array(nr, sizeof(*pages), GFP_KERNEL); + if (!pages) + return NULL; + order =3D preservation->order; + shift =3D PAGE_SHIFT + order; + align =3D 1 << shift; + + while (chunk) { + struct page *page; + + for (int i =3D 0; chunk->phys[i]; i++) { + phys_addr_t phys =3D chunk->phys[i]; + + if (idx + (1 << order) > nr) + goto err_free_pages_array; + + for (int j =3D 0; j < (1 << order); j++) { + page =3D phys_to_page(phys); + kho_restore_page(page, 0); + pages[idx++] =3D page; + phys +=3D PAGE_SIZE; + } + } + + page =3D virt_to_page(chunk); + chunk =3D KHOSER_LOAD_PTR(chunk->hdr.next); + kho_restore_page(page, 0); + __free_page(page); + } + + if (idx !=3D nr) + goto err_free_pages_array; + + area =3D __get_vm_area_node(nr * PAGE_SIZE, align, shift, vm_flags, + VMALLOC_START, VMALLOC_END, NUMA_NO_NODE, + GFP_KERNEL, __builtin_return_address(0)); + if (!area) + goto err_free_pages_array; + + addr =3D (unsigned long)area->addr; + size =3D get_vm_area_size(area); + err =3D vmap_pages_range(addr, addr + size, PAGE_KERNEL, pages, shift); + if (err) + goto err_free_vm_area; + + area->pages =3D pages; + area->nr_pages =3D nr; + + return area->addr; + +err_free_vm_area: + free_vm_area(area); +err_free_pages_array: + kvfree(pages); + return NULL; +} +EXPORT_SYMBOL_GPL(kho_restore_vmalloc); + /* Handling for debug/kho/out */ =20 static struct dentry *debugfs_root; --=20 2.50.1 From nobody Thu Oct 2 10:49:39 2025 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 038832FF661 for ; Wed, 17 Sep 2025 17:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130856; cv=none; b=TjiAGzI9aDBLAJUh/uCcmMZUldjLQSpKW4x3PxwvzQTBLclCyvYyMoezDs2Lop+aDDI7CYIANOiSV7VsjVuveQXBtVDCoprZbc3s63JQ+SdLKvC7taOf8jbbFqSkpCpfnApJeKm+gjjd393EhxOxTLLJ/FbR8jZMKqQ41d5Kw04= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758130856; c=relaxed/simple; bh=C/FWcnjtqXbXmArF2cJQxrpogJR0iVY010DR1LrFIwM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GI06DFPhfVx4viwIMqR/lDD05cpAFtWGV9bB+47gyCfu9DejKMAmO3b590Wi/GANeJSdUfS/YLRiHGXPPQsvDL3uKhNFn2t0nPvtbKn+buw2Bc80O2coCeQHUqWrxP+hJ6DeiNVTii2hN0prDpHUPE/fYAQseNDXzRkoXzFAOVQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=iOltoQiY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iOltoQiY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E146C4CEFA; Wed, 17 Sep 2025 17:40:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1758130855; bh=C/FWcnjtqXbXmArF2cJQxrpogJR0iVY010DR1LrFIwM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iOltoQiY284U3hh2FAqR2YSLRiqan90fIeNrfxWEWrR29HIUddWKZ/avmPxyf4zyD 48SuIuMabx2PFhfGtKxw3xLX5yN5DEqSRLY46DPismYxhCr+uVHXdT9h6wZC73xizJ 2EccQO9M+RUx3o7svQ/39W44rBZFiTdpV+FOUQKTR4XmGqjI8B3m4HFfFOhU8jCI9W U6dECIYw2T4DZvhsu7ot2HGYrYhT0HTxXaB3kbSYA5t4TAvDuAaUfh+9rcdHUvY7cR 0W9RdaJ1bnVhKXdiQ8D7OWCD48DIlzjcmC8dr3jhzyWngx+DLnlkD1R+QfW+UQ3Zp9 BqkAomc0Ys5BA== From: Mike Rapoport To: Andrew Morton Cc: Alexander Graf , Baoquan He , Changyuan Lyu , Chris Li , Jason Gunthorpe , Mike Rapoport , Pasha Tatashin , Pratyush Yadav , kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 4/4] lib/test_kho: use kho_preserve_vmalloc instead of storing addresses in fdt Date: Wed, 17 Sep 2025 20:40:33 +0300 Message-ID: <20250917174033.3810435-5-rppt@kernel.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250917174033.3810435-1-rppt@kernel.org> References: <20250917174033.3810435-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Mike Rapoport (Microsoft)" KHO test stores physical addresses of the preserved folios directly in fdt. Use kho_preserve_vmalloc() instead of it and kho_restore_vmalloc() to retrieve the addresses after kexec. This makes the test more scalable from one side and adds tests coverage for kho_preserve_vmalloc() from the other. Signed-off-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- lib/test_kho.c | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/lib/test_kho.c b/lib/test_kho.c index fe8504e3407b..60cd899ea745 100644 --- a/lib/test_kho.c +++ b/lib/test_kho.c @@ -32,6 +32,7 @@ module_param(max_mem, long, 0644); struct kho_test_state { unsigned int nr_folios; struct folio **folios; + phys_addr_t *folios_info; struct folio *fdt; __wsum csum; }; @@ -67,18 +68,15 @@ static struct notifier_block kho_test_nb =3D { =20 static int kho_test_save_data(struct kho_test_state *state, void *fdt) { - phys_addr_t *folios_info; + phys_addr_t *folios_info __free(kvfree) =3D NULL; + struct kho_vmalloc folios_info_phys; int err =3D 0; =20 - err |=3D fdt_begin_node(fdt, "data"); - err |=3D fdt_property(fdt, "nr_folios", &state->nr_folios, - sizeof(state->nr_folios)); - err |=3D fdt_property_placeholder(fdt, "folios_info", - state->nr_folios * sizeof(*folios_info), - (void **)&folios_info); - err |=3D fdt_property(fdt, "csum", &state->csum, sizeof(state->csum)); - err |=3D fdt_end_node(fdt); + folios_info =3D vmalloc_array(state->nr_folios, sizeof(*folios_info)); + if (!folios_info) + return -ENOMEM; =20 + err =3D kho_preserve_vmalloc(folios_info, &folios_info_phys); if (err) return err; =20 @@ -93,6 +91,17 @@ static int kho_test_save_data(struct kho_test_state *sta= te, void *fdt) break; } =20 + err |=3D fdt_begin_node(fdt, "data"); + err |=3D fdt_property(fdt, "nr_folios", &state->nr_folios, + sizeof(state->nr_folios)); + err |=3D fdt_property(fdt, "folios_info", &folios_info_phys, + sizeof(folios_info_phys)); + err |=3D fdt_property(fdt, "csum", &state->csum, sizeof(state->csum)); + err |=3D fdt_end_node(fdt); + + if (!err) + state->folios_info =3D no_free_ptr(folios_info); + return err; } =20 @@ -209,8 +218,9 @@ static int kho_test_save(void) =20 static int kho_test_restore_data(const void *fdt, int node) { + const struct kho_vmalloc *folios_info_phys; const unsigned int *nr_folios; - const phys_addr_t *folios_info; + phys_addr_t *folios_info; const __wsum *old_csum; __wsum csum =3D 0; int len; @@ -225,8 +235,12 @@ static int kho_test_restore_data(const void *fdt, int = node) if (!old_csum || len !=3D sizeof(*old_csum)) return -EINVAL; =20 - folios_info =3D fdt_getprop(fdt, node, "folios_info", &len); - if (!folios_info || len !=3D sizeof(*folios_info) * *nr_folios) + folios_info_phys =3D fdt_getprop(fdt, node, "folios_info", &len); + if (!folios_info_phys || len !=3D sizeof(*folios_info_phys)) + return -EINVAL; + + folios_info =3D kho_restore_vmalloc(folios_info_phys); + if (!folios_info) return -EINVAL; =20 for (int i =3D 0; i < *nr_folios; i++) { @@ -246,6 +260,8 @@ static int kho_test_restore_data(const void *fdt, int n= ode) folio_put(folio); } =20 + vfree(folios_info); + if (csum !=3D *old_csum) return -EINVAL; =20 @@ -304,6 +320,7 @@ static void kho_test_cleanup(void) folio_put(kho_test_state.folios[i]); =20 kvfree(kho_test_state.folios); + vfree(kho_test_state.folios_info); folio_put(kho_test_state.fdt); } =20 --=20 2.50.1