From nobody Thu Oct 2 23:48:30 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 DBDD3352095 for ; Tue, 9 Sep 2025 14:44: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=1757429084; cv=none; b=ePJCvT5aYxk9RatTDAmokredB6UDaKhJlB+7KN215x5cDMEtm17anjw7YnRO/mh4x2KfXVq/gK7KT/HeVfA+dxv8cCk9JA9X3Omwz4rqonY+GILJ6EWUXjqJlTRzAVwEKL7cOs/ubb+K4IW315d1q9/pn3BJxSxOt9TAfVgqtmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1757429084; c=relaxed/simple; bh=R58t8Ck7gaCyPwSxTfKs4rf/zne/bpphAKyTAFsHSEg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OuPGOp9sh647t5ZCuBLHCVutHiHyPxNgAPU2M7Y4El/HEKmEKT42mulEwQ+9eLUBAEz8hTBqVsWErxnO/7Oq3gQ5FjsX5rrzn7TJWF0Bl93UZrBMXNT0cXrpXIK44rXImDjMKJAyk0ZcqzmVTx/v9MCcB4JeBx0X258R2+/zCGo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=nTeyXhqa; 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="nTeyXhqa" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 547C8C4CEFC; Tue, 9 Sep 2025 14:44:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1757429084; bh=R58t8Ck7gaCyPwSxTfKs4rf/zne/bpphAKyTAFsHSEg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nTeyXhqaog50wiTHSSHI/usuM6Lz1jubOhD0MerikJmeK4c/dobUWCoKgpcwy2wFK Lb39k544yz4KwqKbBp7ObMKBgessNF88sW9kNtV3Ueo704WFmO60mTKjH0CrO62j2O HdVlqYH1A3Y42pMDsi8xJAc9FsgOPyNeayWGFy1Wd6wgpbDhyiUjCPscw9EP/5yrSi tVhcWdijota3s2Uc6HG8jMDzcVTXp1inUWpKioo0B3Dj28uWySSwbWDLOIe8VwGI0j 8IqLOJNwnFkzlz3AxXHJiIA4jQt3cWdaBsmtSeyUztA1fDXjqYXiEOiWJnuxVX7HlF uHD9wGZNDLPpw== From: Pratyush Yadav To: Alexander Graf , Mike Rapoport , Changyuan Lyu , Andrew Morton , Baoquan He , Pratyush Yadav , Pasha Tatashin , Jason Gunthorpe , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , Chris Li , Jason Miu , David Matlack , David Rientjes Cc: linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-mm@kvack.org Subject: [RFC PATCH 3/4] kho: add support for preserving vmalloc allocations Date: Tue, 9 Sep 2025 16:44:23 +0200 Message-ID: <20250909144426.33274-4-pratyush@kernel.org> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20250909144426.33274-1-pratyush@kernel.org> References: <20250909144426.33274-1-pratyush@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) [pratyush@kernel.org: use KHO array instead of linked list of pages to track physical addresses] Signed-off-by: Pratyush Yadav --- include/linux/kexec_handover.h | 21 +++++ kernel/kexec_handover.c | 143 +++++++++++++++++++++++++++++++++ 2 files changed, 164 insertions(+) diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h index 348844cffb136..633f94cec1a35 100644 --- a/include/linux/kexec_handover.h +++ b/include/linux/kexec_handover.h @@ -4,6 +4,7 @@ =20 #include #include +#include =20 struct kho_scratch { phys_addr_t addr; @@ -37,13 +38,23 @@ struct notifier_block; }) =20 struct kho_serialization; +struct kho_vmalloc; =20 #ifdef CONFIG_KEXEC_HANDOVER +struct kho_vmalloc { + struct kho_array ka; + unsigned int total_pages; + unsigned int flags; + unsigned short order; +}; + bool kho_is_enabled(void); =20 int kho_preserve_folio(struct folio *folio); +int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation); int kho_preserve_phys(phys_addr_t phys, size_t size); struct folio *kho_restore_folio(phys_addr_t phys); +void *kho_restore_vmalloc(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 @@ -70,11 +81,21 @@ static inline int kho_preserve_phys(phys_addr_t phys, s= ize_t size) return -EOPNOTSUPP; } =20 +static inline int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *pres= ervation) +{ + return -EOPNOTSUPP; +} + static inline struct folio *kho_restore_folio(phys_addr_t phys) { return NULL; } =20 +static inline void *kho_restore_vmalloc(struct kho_vmalloc *preservation) +{ + 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 26f9f5295f07d..5f89134ceeee0 100644 --- a/kernel/kexec_handover.c +++ b/kernel/kexec_handover.c @@ -19,6 +19,7 @@ #include #include #include +#include =20 #include =20 @@ -723,6 +724,148 @@ int kho_preserve_phys(phys_addr_t phys, size_t size) } EXPORT_SYMBOL_GPL(kho_preserve_phys); =20 +#define KHO_VMALLOC_FLAGS_MASK (VM_ALLOC | VM_ALLOW_HUGE_VMAP) + +/** + * kho_preserve_vmalloc - preserve memory allocated with vmalloc() across = kexec + * @ptr: pointer to the area in vmalloc address space + * @preservation: pointer to metadata for preserved data. + * + * 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 structure that describes the metadata for t= he + * preserved pages. @preservation itself is not KHO-preserved. The caller = must + * do that. + * + * 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_mem_track *track =3D &kho_out.ser.track; + struct vm_struct *vm =3D find_vm_area(ptr); + unsigned int order, flags; + struct ka_iter iter; + int err; + + if (!vm) + return -EINVAL; + + if (vm->flags & ~KHO_VMALLOC_FLAGS_MASK) + return -EOPNOTSUPP; + + flags =3D vm->flags & KHO_VMALLOC_FLAGS_MASK; + order =3D get_vm_area_page_order(vm); + + preservation->total_pages =3D vm->nr_pages; + preservation->flags =3D flags; + preservation->order =3D order; + + ka_iter_init_write(&iter, &preservation->ka); + + for (int i =3D 0, pos =3D 0; i < vm->nr_pages; i +=3D (1 << order), pos++= ) { + phys_addr_t phys =3D page_to_phys(vm->pages[i]); + + err =3D __kho_preserve_order(track, PHYS_PFN(phys), order); + if (err) + goto err_free; + + err =3D ka_iter_setpos(&iter, pos); + if (err) + goto err_free; + + err =3D ka_iter_setentry(&iter, ka_mk_value(phys)); + if (err) + goto err_free; + } + + err =3D kho_array_preserve(&preservation->ka); + if (err) + goto err_free; + + return 0; + +err_free: + kho_array_destroy(&preservation->ka); + 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: the 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(struct kho_vmalloc *preservation) +{ + unsigned int align, order, shift, flags; + unsigned int idx =3D 0, nr; + unsigned long addr, size; + struct vm_struct *area; + struct page **pages; + struct ka_iter iter; + void *entry; + int err; + + flags =3D preservation->flags; + if (flags & ~KHO_VMALLOC_FLAGS_MASK) + return NULL; + + err =3D ka_iter_init_restore(&iter, &preservation->ka); + if (err) + return NULL; + + nr =3D preservation->total_pages; + pages =3D kvmalloc_array(nr, sizeof(*pages), GFP_KERNEL); + if (!pages) + goto err_ka_destroy; + order =3D preservation->order; + shift =3D PAGE_SHIFT + order; + align =3D 1 << shift; + + ka_iter_for_each(&iter, entry) { + phys_addr_t phys =3D ka_to_value(entry); + struct page *page; + + page =3D phys_to_page(phys); + kho_restore_page(page, 0); + pages[idx++] =3D page; + phys +=3D PAGE_SIZE; + } + + area =3D __get_vm_area_node(nr * PAGE_SIZE, align, shift, 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; + + kho_array_destroy(&preservation->ka); + + return area->addr; + +err_free_vm_area: + free_vm_area(area); +err_free_pages_array: + kvfree(pages); +err_ka_destroy: + kho_array_destroy(&preservation->ka); + return NULL; +} +EXPORT_SYMBOL_GPL(kho_restore_vmalloc); + /* Handling for debug/kho/out */ =20 static struct dentry *debugfs_root; --=20 2.47.3