From nobody Tue Feb 10 05:26:08 2026 Received: from mail-yx1-f51.google.com (mail-yx1-f51.google.com [74.125.224.51]) (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 8EFD22D5938 for ; Fri, 14 Nov 2025 19:00:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763146821; cv=none; b=RBYjPjmTtYl4lKeeBYAmSXJdVhnxqmLWj3mpWxkYUVvXZa4nK0UhwtPrqOLzqRnrxnHayx4rDc31YSCU1x1XBWUlDomRjRsKtaahClRcfxa1qoGu2d7sQ7+RAxDaAeGGy30tNjImXP0oOpKP4SDqQaTsL8CTX0wlJxTXIwSUFBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763146821; c=relaxed/simple; bh=J7/W6C6ENMusCfv2at49pc4MZ2P1DOOurmGOftGx8uU=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=vFhuw0zfVXkoqrBS38AXUjhhSTzLE3YXnw9BIoQ76n/nJDHz30rlIv/L/duTdshY93sBi12Pavx0H+HomGzDb6OjRePoopCWjcQHMlsublsRzJ+VXKZtzcbMVqKJxlbdcBL6Tf8Fbh8rKdiK1UveC09vZA9t6vUKjyVXpkLAMiI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b=L+xT/JHm; arc=none smtp.client-ip=74.125.224.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen.com header.i=@soleen.com header.b="L+xT/JHm" Received: by mail-yx1-f51.google.com with SMTP id 956f58d0204a3-640d8b78608so1811542d50.1 for ; Fri, 14 Nov 2025 11:00:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; t=1763146810; x=1763751610; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=Ihe0loas1LrTsn9bRuxuUBOFUFQUEsXdHcA1ZYhZ3IY=; b=L+xT/JHmJ5PxuVzVQWgadFo5zjYiWeMPpThXNPYS38RZW9QlH+wjlzwF0Ms5dwkqAy coC/4K32CBQWn5ynXyT7T9mq3R9W6blgnt3cY9o+ldwxsDuzhunp+cSFPH09qrSF+u4T k6WNuzGrsBN2Wfg86Efg2mgsIGBSBIMYGtu07oCdBwEbFZgnbCq5jcJDXdCPchfhRB9/ sFrYN5SeaTPsqfjUHdlPrYuO2FIA5zT1R8+vQgw8YE/8oqFDva4agV/J7CwqLyw5PHnB oMe4vyGI4WNjNcu2FaaZWlWIBYAE59vLw64d16oNjQO1EDXUyZdiaejGV4CqjwIhIXnF C9ug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763146810; x=1763751610; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=Ihe0loas1LrTsn9bRuxuUBOFUFQUEsXdHcA1ZYhZ3IY=; b=ftbzuqYjG3REbdY8MjUtBRJBmIlkTXp08NojLsWmCGCbQSQhKC8bEZ8Z8HEhMJqM8E pHII3EE3NSUVZUE3xI/vo8DE3PE1a4WJIhYtf8lFrdoJhUd0tYL8p39eLJqzjsrA6rF8 GixNKQCNQrTAQVGhUNJbrV9AHcWmLUoXJhx7jWyiW9dXHfD4IzHZEZBvzaml8McS4P2r iZ1M14WwIEuZzMCnqAq8tAqr5x7PqZekLzfavM/jlQG1C/omPYACtspabEVYTco2TYBe YmlWPf9cKkTnPgLJKgF3rxD2HHpCoAaam7OGXQFjDj98WG1HBxTxeBASrt5IyizPGFLf TbtA== X-Forwarded-Encrypted: i=1; AJvYcCVGzo+Kj3Gflg0PsSFCo359cjMZAt7bPANhW8Cl7qBo3eXrKwegxE/sWY9Dv/Qdm5aUrCR3yA0QBFuKHPg=@vger.kernel.org X-Gm-Message-State: AOJu0YwUdSWBTHRmDr8vY0xmawi4oetkNVdZnrUrzBMowPI7pqNRpGLG ACLnY7alkLhulaNvSwg/0WMbBccA5z6fOaWpSezysIeZrDK/22VwM0OMbH3A9zb94Ts= X-Gm-Gg: ASbGncvcmpAj31oJvelidoCKhI2q4pdb2vl2GL+QmOmFewsFDry9T0RAQUB/wFLs5PN 7VIAWhO9Ajiowe5C++7sDW944M6XQ3nuYQJFzUDuLkadzv1+EvLELwHm8Ca+D0hayKM+rOYsseS kQncm3EiY5fOLaJcUIkGVOkw8UDngrcDsdaWwoDYgnBqtuHiZHMzbqhqqgLz3hG1sL78kuvkqBB hqLA2LnDAUMI9dz5eLw7aXOnRvEjaWaMbRXvhMEmQyPFDJ2ii9o+lfKFYvJy7TGETNsai8qrVbU RXTcI/gIEuQ1KnE9SVASVdKesD35IFTOmGh3doIah4/BRRUX3mAvAOlBJVLHdQtMY6bQeZ3UOXx 6168X7RWZgZ0Ey9hZgiXTxZBtdMTl2C/wIrkzC5kIWnEVlVNsqKCXTG6eeFwi87PiZUtNTfc8TV q9jbwk0De9XJvtJy55VHzmAKAJuAZ8+9X9nful0Sv/UIHyiEZwwfhpq+6xf6Pd6riW6iMvH492V qsapxw= X-Google-Smtp-Source: AGHT+IHtcsADCaRWmb9vZYl521slB2k1TyP9yi6kP/peGKq0p/n4sbxoK2XQz6YSUfgIDyLwv/pwEw== X-Received: by 2002:a05:690e:23c3:b0:636:563a:6725 with SMTP id 956f58d0204a3-641e7644cefmr2737299d50.49.1763146810418; Fri, 14 Nov 2025 11:00:10 -0800 (PST) Received: from soleen.c.googlers.com.com (182.221.85.34.bc.googleusercontent.com. [34.85.221.182]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-6410e8f4f2esm2014058d50.0.2025.11.14.11.00.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Nov 2025 11:00:10 -0800 (PST) From: Pasha Tatashin To: akpm@linux-foundation.org, bhe@redhat.com, pasha.tatashin@soleen.com, rppt@kernel.org, jasonmiu@google.com, arnd@arndb.de, coxu@redhat.com, dave@vasilevsky.ca, ebiggers@google.com, graf@amazon.com, kees@kernel.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-mm@kvack.org Subject: [PATCH v2 03/13] kho: Introduce high-level memory allocation API Date: Fri, 14 Nov 2025 13:59:52 -0500 Message-ID: <20251114190002.3311679-4-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.52.0.rc1.455.g30608eb744-goog In-Reply-To: <20251114190002.3311679-1-pasha.tatashin@soleen.com> References: <20251114190002.3311679-1-pasha.tatashin@soleen.com> 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" Currently, clients of KHO must manually allocate memory (e.g., via alloc_pages), calculate the page order, and explicitly call kho_preserve_folio(). Similarly, cleanup requires separate calls to unpreserve and free the memory. Introduce a high-level API to streamline this common pattern: - kho_alloc_preserve(size): Allocates physically contiguous, zeroed memory and immediately marks it for preservation. - kho_unpreserve_free(ptr): Unpreserves and frees the memory in the current kernel. - kho_restore_free(ptr): Restores the struct page state of preserved memory in the new kernel and immediately frees it to the page allocator. Signed-off-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) Reviewed-by: Pratyush Yadav --- include/linux/kexec_handover.h | 22 +++++--- kernel/liveupdate/kexec_handover.c | 87 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 7 deletions(-) diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h index 80ece4232617..38a9487a1a00 100644 --- a/include/linux/kexec_handover.h +++ b/include/linux/kexec_handover.h @@ -2,8 +2,9 @@ #ifndef LINUX_KEXEC_HANDOVER_H #define LINUX_KEXEC_HANDOVER_H =20 -#include +#include #include +#include =20 struct kho_scratch { phys_addr_t addr; @@ -48,6 +49,9 @@ int kho_preserve_pages(struct page *page, unsigned int nr= _pages); int kho_unpreserve_pages(struct page *page, unsigned int nr_pages); int kho_preserve_vmalloc(void *ptr, struct kho_vmalloc *preservation); int kho_unpreserve_vmalloc(struct kho_vmalloc *preservation); +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_pages(phys_addr_t phys, unsigned int nr_pages); void *kho_restore_vmalloc(const struct kho_vmalloc *preservation); @@ -101,6 +105,14 @@ static inline int kho_unpreserve_vmalloc(struct kho_vm= alloc *preservation) return -EOPNOTSUPP; } =20 +void *kho_alloc_preserve(size_t size) +{ + return ERR_PTR(-EOPNOTSUPP); +} + +void kho_unpreserve_free(void *mem) { } +void kho_restore_free(void *mem) { } + static inline struct folio *kho_restore_folio(phys_addr_t phys) { return NULL; @@ -122,18 +134,14 @@ static inline int kho_add_subtree(const char *name, v= oid *fdt) return -EOPNOTSUPP; } =20 -static inline void kho_remove_subtree(void *fdt) -{ -} +static inline void kho_remove_subtree(void *fdt) { } =20 static inline int kho_retrieve_subtree(const char *name, phys_addr_t *phys) { return -EOPNOTSUPP; } =20 -static inline void kho_memory_init(void) -{ -} +static inline void kho_memory_init(void) { } =20 static inline void kho_populate(phys_addr_t fdt_phys, u64 fdt_len, phys_addr_t scratch_phys, u64 scratch_len) diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_h= andover.c index bc7f046a1313..5c5c9c46fe92 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -4,6 +4,7 @@ * Copyright (C) 2023 Alexander Graf * Copyright (C) 2025 Microsoft Corporation, Mike Rapoport * Copyright (C) 2025 Google LLC, Changyuan Lyu + * Copyright (C) 2025 Pasha Tatashin */ =20 #define pr_fmt(fmt) "KHO: " fmt @@ -1117,6 +1118,92 @@ void *kho_restore_vmalloc(const struct kho_vmalloc *= preservation) } EXPORT_SYMBOL_GPL(kho_restore_vmalloc); =20 +/** + * kho_alloc_preserve - Allocate, zero, and preserve memory. + * @size: The number of bytes to allocate. + * + * Allocates a physically contiguous block of zeroed pages that is large + * enough to hold @size bytes. The allocated memory is then registered with + * KHO for preservation across a kexec. + * + * Note: The actual allocated size will be rounded up to the nearest + * power-of-two page boundary. + * + * @return A virtual pointer to the allocated and preserved memory on succ= ess, + * or an ERR_PTR() encoded error on failure. + */ +void *kho_alloc_preserve(size_t size) +{ + struct folio *folio; + int order, ret; + + if (!size) + return ERR_PTR(-EINVAL); + + order =3D get_order(size); + if (order > MAX_PAGE_ORDER) + return ERR_PTR(-E2BIG); + + folio =3D folio_alloc(GFP_KERNEL | __GFP_ZERO, order); + if (!folio) + return ERR_PTR(-ENOMEM); + + ret =3D kho_preserve_folio(folio); + if (ret) { + folio_put(folio); + return ERR_PTR(ret); + } + + return folio_address(folio); +} +EXPORT_SYMBOL_GPL(kho_alloc_preserve); + +/** + * kho_unpreserve_free - Unpreserve and free memory. + * @mem: Pointer to the memory allocated by kho_alloc_preserve(). + * + * Unregisters the memory from KHO preservation and frees the underlying + * pages back to the system. This function should be called to clean up + * memory allocated with kho_alloc_preserve(). + */ +void kho_unpreserve_free(void *mem) +{ + struct folio *folio; + + if (!mem) + return; + + folio =3D virt_to_folio(mem); + WARN_ON_ONCE(kho_unpreserve_folio(folio)); + folio_put(folio); +} +EXPORT_SYMBOL_GPL(kho_unpreserve_free); + +/** + * kho_restore_free - Restore and free memory after kexec. + * @mem: Pointer to the memory (in the new kernel's address space) + * that was allocated by the old kernel. + * + * This function is intended to be called in the new kernel (post-kexec) + * to take ownership of and free a memory region that was preserved by the + * old kernel using kho_alloc_preserve(). + * + * It first restores the pages from KHO (using their physical address) + * and then frees the pages back to the new kernel's page allocator. + */ +void kho_restore_free(void *mem) +{ + struct folio *folio; + + if (!mem) + return; + + folio =3D kho_restore_folio(__pa(mem)); + if (!WARN_ON(!folio)) + folio_put(folio); +} +EXPORT_SYMBOL_GPL(kho_restore_free); + static void __kho_abort(void) { if (kho_out.preserved_mem_map) { --=20 2.52.0.rc1.455.g30608eb744-goog