From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66050C3ABBC for ; Fri, 9 May 2025 07:47:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5543B6B00B8; Fri, 9 May 2025 03:46:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B2366B00B9; Fri, 9 May 2025 03:46:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 26A1A6B00BA; Fri, 9 May 2025 03:46:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 037306B00B8 for ; Fri, 9 May 2025 03:46:57 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 43233CB89A for ; Fri, 9 May 2025 07:46:59 +0000 (UTC) X-FDA: 83422588158.14.AE05E99 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf26.hostedemail.com (Postfix) with ESMTP id 7419814000B for ; Fri, 9 May 2025 07:46:57 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="u8nHr/Ab"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 38LIdaAoKCBUx2v81JFv8619916z.x97638FI-775Gvx5.9C1@flex--changyuanl.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=38LIdaAoKCBUx2v81JFv8619916z.x97638FI-775Gvx5.9C1@flex--changyuanl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746776817; a=rsa-sha256; cv=none; b=6yuJScIMgKRYiwc6dIhbxIWGNzRVy6wPPV+yjPReNUkwMrARRIk/QOV+mWmrs/ACzg+7VC FKQvjHRTvsdCS//BzYMXs5KvZZElpIQYuwgsfndoWyULcylcamH0F19ByN0bWsoR2HguMT bMKHWoTBglNhIIF6L04GNaoVT2VsQWo= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="u8nHr/Ab"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 38LIdaAoKCBUx2v81JFv8619916z.x97638FI-775Gvx5.9C1@flex--changyuanl.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=38LIdaAoKCBUx2v81JFv8619916z.x97638FI-775Gvx5.9C1@flex--changyuanl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746776817; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=1bmne2y90D8JQiy5XkADi80jEOrlEPK+1vvd1ABYARg=; b=gFXY3TtAfdWtAE38HPlbfYDeQf4mjJ5CwDg0GrP4yKD91oePg4KraWak1CgBrQYU30yUzv h5uZSjaofXV9BubjOgLCAblvpvYAWplxp/QvB91wsQfNVAjF665F/MYpaQaPK7CA53XrSF d7hRTstc+bNivpFC2Py6bZjmBH+Ga78= Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-af2f03fcc95so2054259a12.1 for ; Fri, 09 May 2025 00:46:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746776816; x=1747381616; darn=kvack.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=1bmne2y90D8JQiy5XkADi80jEOrlEPK+1vvd1ABYARg=; b=u8nHr/AbWvPgzikR3+esmsfp1A0l17ZciC4UWZ0czCUtuvfNeGu6YpOoRtk2ZYhG/R tnZ0+qKaWlvqCPs8ZG+APFoT9L/z0NFsSsy7YrjmJs7egQUOxVR0bA7U+W+j/8db1xt6 DAPU+zuYU580eBTuAkENL2TZuQoAs9yysJgb041pYomZfyBrQTlhWKepAp98GLm8PJZk B6BGj7DSYgcoRQ+q6ziLkEjHkPxbBES2RPYfLRVShWrISCiExgGnlzATzbMzSTiOyTSc ZYSGk7T6MIr25S8RzevVTUrZP7f0gM9mFyBwqobt23SlP5MzTrhAf35VZPBSmPGN3Hc/ 4/6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746776816; x=1747381616; 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=1bmne2y90D8JQiy5XkADi80jEOrlEPK+1vvd1ABYARg=; b=wE6Ii3iPz154tC0jzPrGQk3QTOZHlRdrI51g9KMDmqnJq17iXJpMCWGSisPwqMHfw8 XZh5/jlY5rdi3pEJf15MI4S5Q/Lhx920a/GA9dxge9OgLo/PSw/DWyS0kf1Ri+KeuH8R HG5Hh9CBS86rsVkKzE1NzZx1jg1igO6vsn+gQ+Pmhr3de6dVQIL9JP7LaiNRwbISWad2 /JgIUND+B7Px2b4kKtW++f9qf618P+TAIHKAd7mchyWisRVU0YBQT9GdgXmrjLVr8/dm JELDZ1togZFTUhVLXl8cTE2yEJ7xTEkGGmfIDA3pTul6PLaVx6qJqWKPOzylydBnZtuh L92A== X-Forwarded-Encrypted: i=1; AJvYcCVJG9VpxhzaYGO2CXKZmCPK4Har36Jd0TmoNLVlNlrQ7HAvyEI7Ku1cT2+nYwf1faT3aDrwvUn8yw==@kvack.org X-Gm-Message-State: AOJu0YxBhn6fV/3/EUx2l8tzCDMDvK8hjprX/r6snxPOj6KMe50HoQSn FsvbmX/gLSYo8vdO6SpWSNWRANu3pW/fFlM9DPbNNmZKi6dBJ/GeNRH6zL4kFwdOtuZ9IgAyZCO X8UJ25H2DOXjKDHBupw== X-Google-Smtp-Source: AGHT+IHIomAzXg5M2xD8Ke7Ch9cLJaJgpC1gqGvq2UBKZ/ILunuCV6BiFzeH+RQUHWtdNNF1Cpyd6Zk06ESRNXDD X-Received: from pgbdk10.prod.google.com ([2002:a05:6a02:c8a:b0:b1f:fd39:8314]) (user=changyuanl job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:2450:b0:1f5:6c94:2cc9 with SMTP id adf61e73a8af0-215abc22f24mr3411999637.22.1746776816238; Fri, 09 May 2025 00:46:56 -0700 (PDT) Date: Fri, 9 May 2025 00:46:20 -0700 In-Reply-To: <20250509074635.3187114-1-changyuanl@google.com> Mime-Version: 1.0 References: <20250509074635.3187114-1-changyuanl@google.com> X-Mailer: git-send-email 2.49.0.1015.ga840276032-goog Message-ID: <20250509074635.3187114-3-changyuanl@google.com> Subject: [PATCH v8 02/17] memblock: Add support for scratch memory From: Changyuan Lyu To: akpm@linux-foundation.org, linux-kernel@vger.kernel.org Cc: anthony.yznaga@oracle.com, arnd@arndb.de, ashish.kalra@amd.com, benh@kernel.crashing.org, bp@alien8.de, catalin.marinas@arm.com, corbet@lwn.net, dave.hansen@linux.intel.com, devicetree@vger.kernel.org, dwmw2@infradead.org, ebiederm@xmission.com, graf@amazon.com, hpa@zytor.com, jgowans@amazon.com, kexec@lists.infradead.org, krzk@kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, luto@kernel.org, mark.rutland@arm.com, mingo@redhat.com, pasha.tatashin@soleen.com, pbonzini@redhat.com, peterz@infradead.org, ptyadav@amazon.de, robh@kernel.org, rostedt@goodmis.org, rppt@kernel.org, saravanak@google.com, skinsburskii@linux.microsoft.com, tglx@linutronix.de, thomas.lendacky@amd.com, will@kernel.org, x86@kernel.org, Changyuan Lyu Content-Type: text/plain; charset="UTF-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ZxpnlP04KXJv/tgXXXl8ls99rryuXMH 2MqsgIan pNc3qSpFs73dWxMlvMOuZ0HcPq1tfwWV0IpSYEQPdcLG1V4YeTx7SzxUYtfzNvDWaLSS58Azo+djW/7pglf5L6N7Zgl5vFLThYX+e3hrQ/tF7vaucq8DfbRd7ezA7ej/ytphTll/ugs08rGLMsW069I/4xEBaHcACL7Emo51SrFTwyTTZddlga8auFJTv6X2nwn0YwLXIMYV11vER3gG+hK9CImw1rKGCh5HGb4NNBuBaYLAtLmSDlzoqMj2JN3mP6PZYZgbGzVI4GoeW1r6mgyj9eA+yEhAWaXCBPtZqq3tfTfIaKjBOHYj1ZsbzH4jMU/fZViZKHldO+gx8DCs8HUwevu+/rR7cyP0gX5/StGW820eY9yPJ0qhQvUp3pDxhxQvVE7vC2e2da7R55ActkAVAUx5Ja9LSWYxoU64HjN7VGlAUG/uC4mQt0OleXOp9Vo8E63wZNl3aEtVf6T5Cp4/k1LfzOuXZeGX40em7kRwROpsjcK/C5oQGgUHrQ+3wd6PVPlkTFSgP3CnFJmVDuWeWf86Qadgwz7wptnOTSZwhtzc1D1CfDdcwqTE+iBS3yeb6y1dmvFiEshFT+AC97CeI8iPLYC1g+9imnqwQqvU6cWKyI3H74rLZJw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alexander Graf With KHO (Kexec HandOver), we need a way to ensure that the new kernel does not allocate memory on top of any memory regions that the previous kernel was handing over. But to know where those are, we need to include them in the memblock.reserved array which may not be big enough to hold all ranges that need to be persisted across kexec. To resize the array, we need to allocate memory. That brings us into a catch 22 situation. The solution to that is limit memblock allocations to the scratch regions: safe regions to operate in the case when there is memory that should remain intact across kexec. KHO provides several "scratch regions" as part of its metadata. These scratch regions are contiguous memory blocks that known not to contain any memory that should be persisted across kexec. These regions should be large enough to accommodate all memblock allocations done by the kexeced kernel. We introduce a new memblock_set_scratch_only() function that allows KHO to indicate that any memblock allocation must happen from the scratch regions. Later, we may want to perform another KHO kexec. For that, we reuse the same scratch regions. To ensure that no eventually handed over data gets allocated inside a scratch region, we flip the semantics of the scratch region with memblock_clear_scratch_only(): After that call, no allocations may happen from scratch memblock regions. We will lift that restriction in the next patch. Signed-off-by: Alexander Graf Co-developed-by: Mike Rapoport (Microsoft) Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Changyuan Lyu --- include/linux/memblock.h | 20 +++++++++++++ mm/Kconfig | 4 +++ mm/memblock.c | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 6c00fbc085132..993937a6b9620 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -45,6 +45,11 @@ extern unsigned long long max_possible_pfn; * @MEMBLOCK_RSRV_KERN: memory region that is reserved for kernel use, * either explictitly with memblock_reserve_kern() or via memblock * allocation APIs. All memblock allocations set this flag. + * @MEMBLOCK_KHO_SCRATCH: memory region that kexec can pass to the next + * kernel in handover mode. During early boot, we do not know about all + * memory reservations yet, so we get scratch memory from the previous + * kernel that we know is good to use. It is the only memory that + * allocations may happen from in this phase. */ enum memblock_flags { MEMBLOCK_NONE = 0x0, /* No special request */ @@ -54,6 +59,7 @@ enum memblock_flags { MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */ MEMBLOCK_RSRV_NOINIT = 0x10, /* don't initialize struct pages */ MEMBLOCK_RSRV_KERN = 0x20, /* memory reserved for kernel use */ + MEMBLOCK_KHO_SCRATCH = 0x40, /* scratch memory for kexec handover */ }; /** @@ -148,6 +154,8 @@ int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); int memblock_mark_nomap(phys_addr_t base, phys_addr_t size); int memblock_clear_nomap(phys_addr_t base, phys_addr_t size); int memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size); +int memblock_mark_kho_scratch(phys_addr_t base, phys_addr_t size); +int memblock_clear_kho_scratch(phys_addr_t base, phys_addr_t size); void memblock_free(void *ptr, size_t size); void reset_all_zones_managed_pages(void); @@ -291,6 +299,11 @@ static inline bool memblock_is_driver_managed(struct memblock_region *m) return m->flags & MEMBLOCK_DRIVER_MANAGED; } +static inline bool memblock_is_kho_scratch(struct memblock_region *m) +{ + return m->flags & MEMBLOCK_KHO_SCRATCH; +} + int memblock_search_pfn_nid(unsigned long pfn, unsigned long *start_pfn, unsigned long *end_pfn); void __next_mem_pfn_range(int *idx, int nid, unsigned long *out_start_pfn, @@ -619,5 +632,12 @@ static inline void early_memtest(phys_addr_t start, phys_addr_t end) { } static inline void memtest_report_meminfo(struct seq_file *m) { } #endif +#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH +void memblock_set_kho_scratch_only(void); +void memblock_clear_kho_scratch_only(void); +#else +static inline void memblock_set_kho_scratch_only(void) { } +static inline void memblock_clear_kho_scratch_only(void) { } +#endif #endif /* _LINUX_MEMBLOCK_H */ diff --git a/mm/Kconfig b/mm/Kconfig index e113f713b4938..60ea9eba48140 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -469,6 +469,10 @@ config HAVE_GUP_FAST depends on MMU bool +# Enable memblock support for scratch memory which is needed for kexec handover +config MEMBLOCK_KHO_SCRATCH + bool + # Don't discard allocated memory used to track "memory" and "reserved" memblocks # after early boot, so it can still be used to test for validity of memory. # Also, memblocks are updated with memory hot(un)plug. diff --git a/mm/memblock.c b/mm/memblock.c index 2dc95ecdee5ce..6eba0dfe87155 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -107,6 +107,13 @@ unsigned long min_low_pfn; unsigned long max_pfn; unsigned long long max_possible_pfn; +#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH +/* When set to true, only allocate from MEMBLOCK_KHO_SCRATCH ranges */ +static bool kho_scratch_only; +#else +#define kho_scratch_only false +#endif + static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock; static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock; #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP @@ -166,6 +173,10 @@ bool __init_memblock memblock_has_mirror(void) static enum memblock_flags __init_memblock choose_memblock_flags(void) { + /* skip non-scratch memory for kho early boot allocations */ + if (kho_scratch_only) + return MEMBLOCK_KHO_SCRATCH; + return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; } @@ -925,6 +936,18 @@ int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size) } #endif +#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH +__init void memblock_set_kho_scratch_only(void) +{ + kho_scratch_only = true; +} + +__init void memblock_clear_kho_scratch_only(void) +{ + kho_scratch_only = false; +} +#endif + /** * memblock_setclr_flag - set or clear flag for a memory region * @type: memblock type to set/clear flag for @@ -1050,6 +1073,36 @@ int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t MEMBLOCK_RSRV_NOINIT); } +/** + * memblock_mark_kho_scratch - Mark a memory region as MEMBLOCK_KHO_SCRATCH. + * @base: the base phys addr of the region + * @size: the size of the region + * + * Only memory regions marked with %MEMBLOCK_KHO_SCRATCH will be considered + * for allocations during early boot with kexec handover. + * + * Return: 0 on success, -errno on failure. + */ +__init int memblock_mark_kho_scratch(phys_addr_t base, phys_addr_t size) +{ + return memblock_setclr_flag(&memblock.memory, base, size, 1, + MEMBLOCK_KHO_SCRATCH); +} + +/** + * memblock_clear_kho_scratch - Clear MEMBLOCK_KHO_SCRATCH flag for a + * specified region. + * @base: the base phys addr of the region + * @size: the size of the region + * + * Return: 0 on success, -errno on failure. + */ +__init int memblock_clear_kho_scratch(phys_addr_t base, phys_addr_t size) +{ + return memblock_setclr_flag(&memblock.memory, base, size, 0, + MEMBLOCK_KHO_SCRATCH); +} + static bool should_skip_region(struct memblock_type *type, struct memblock_region *m, int nid, int flags) @@ -1081,6 +1134,13 @@ static bool should_skip_region(struct memblock_type *type, if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m)) return true; + /* + * In early alloc during kexec handover, we can only consider + * MEMBLOCK_KHO_SCRATCH regions for the allocations + */ + if ((flags & MEMBLOCK_KHO_SCRATCH) && !memblock_is_kho_scratch(m)) + return true; + return false; } @@ -2465,6 +2525,7 @@ static const char * const flagname[] = { [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG", [ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT", [ilog2(MEMBLOCK_RSRV_KERN)] = "RSV_KERN", + [ilog2(MEMBLOCK_KHO_SCRATCH)] = "KHO_SCRATCH", }; static int memblock_debug_show(struct seq_file *m, void *private) -- 2.49.0.1015.ga840276032-goog