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 994A1C3ABAA for ; Thu, 1 May 2025 22:55:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D133B6B008A; Thu, 1 May 2025 18:55:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B874E6B0096; Thu, 1 May 2025 18:55:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92F446B0092; Thu, 1 May 2025 18:55:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 6A60B6B008A for ; Thu, 1 May 2025 18:55:14 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3A840C15E3 for ; Thu, 1 May 2025 22:55:15 +0000 (UTC) X-FDA: 83395846590.14.0AB7774 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by imf17.hostedemail.com (Postfix) with ESMTP id 657C340003 for ; Thu, 1 May 2025 22:55:13 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="hf3X7u/7"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 30PsTaAoKCF89E7KDVR7KIDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--changyuanl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=30PsTaAoKCF89E7KDVR7KIDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--changyuanl.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746140113; a=rsa-sha256; cv=none; b=uvjQL7MBw90Nrhz7DQyk+kqyUWPPapfuto5BD1s5KcPvFyiH5LCZZGJxGud1ynsbvTgP3u lEvnCui+gexnXD0QFsFfoJMC5qXqbkvIFmrZNK2Na8YJuR7qdjZ09LlTvnzNnUllUbe9dC oKu+O7eix5+psAswE2POvnLYfyiwx54= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="hf3X7u/7"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 30PsTaAoKCF89E7KDVR7KIDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--changyuanl.bounces.google.com designates 209.85.214.201 as permitted sender) smtp.mailfrom=30PsTaAoKCF89E7KDVR7KIDLLDIB.9LJIFKRU-JJHS79H.LOD@flex--changyuanl.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746140113; 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=KsrtYfKvpGG224Vuugu7gIhtwe4eY04G0mgo4ynGz+o=; b=AeUmekL5ncV0jjScSSmisAL2rEh+JPsGw02DofpuMBRmHC0Xe0adHDsXZnbhMcUwOVV0tO 25GPawfua5Vl1p41XZz108E9gbA9zW0zinWqmjbGO5CYdKacpW4WdJ0HpKjJD1eA1QJB6f /2Z/RnBnRWhnpdpsgjv3BU0fkuuFGZ8= Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2264e5b2b7cso11972395ad.2 for ; Thu, 01 May 2025 15:55:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1746140112; x=1746744912; 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=KsrtYfKvpGG224Vuugu7gIhtwe4eY04G0mgo4ynGz+o=; b=hf3X7u/7XKtj3hdGZ2fhmsk+kgPvsfK4O7zjDaoCSx+I/qlcrtc4sQGfPLVy4yEpXk qQiiilTxpg1XJoROru59VD4CzJiCDW3PxlcagOQL7ZoZxvpNFPWU/u1N5GIUzCX1EQT9 MOB8g1geHG0IVyOib7LmG1j7JcxDgoGFWj1sV6umTp3YWqHv1s1yK6TxKiM5pg9BABpW 4g0y2DXuQ6Sb0JLdWLod/i2M96870td1LsokQ5lpFAXqQ1wkBFEswVYzkPAVBwEo7Dtj gN/VX/cRcdT2VJgxNAE5s5ChP5FgSdswra1gaRGDD6QIIAk1QJNmumkZDBfsfLCzkOa5 Lj1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746140112; x=1746744912; 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=KsrtYfKvpGG224Vuugu7gIhtwe4eY04G0mgo4ynGz+o=; b=gqb9+G5UoPl8vsWZlkwO4409ofk396gEqpXvgZucJwW2gaKdrZnjjvb5EfuQJcuCwP 0u71KS0C6cUoR6830vlsb8RV0jfbe00/rkH5Q7CDwAWwkxJx5DXmxVgjDcNojnhjQvO0 HEJpvT83/dDismbM0mVmuZl+nCsiMye4YzgfuFS/65RhNqPUzCW3ivDlp9j7gO/BDZvn Cf5dUJOaRtXTqVNv8l6DTQelRuAKUg2IUh1cEfDreNU/F2o5cPn4E9tTxV/Y3ZyrMGtd 1U8ilZH9IX5Buupld/sCTnvOHee37ZvnEqPx7k7zJX3ESCtqJXv7NxuYk9F4puKQPsk/ axqA== X-Forwarded-Encrypted: i=1; AJvYcCXDYi3ZTwhzO/uTNK1uAYN9eQGUN5R9cxm65e0K84GxoSDGBPlZS3jeLjEMvauCK4IaevmRLEMSuQ==@kvack.org X-Gm-Message-State: AOJu0YxQelFa4RHhYJtRo314JqYjvOXqdcAD/slQCXWQa0hVdq72gij3 cUiEB3wVv50aSG26mxqI8UBZfDE/terkbDFq4mmAdQyAQ93c6bn0EoHY3t2izafJxm2z5VYrOXY AIs45m6aK5Ga1OWViLA== X-Google-Smtp-Source: AGHT+IFLb0PSdHgpE7Ok3NaDj2F1I/2reklfUyriSmie18M/0zt3atLCcdPUxCKomSzRS6uR7s3ME4uvfN3pXcz1 X-Received: from pgac15.prod.google.com ([2002:a05:6a02:294f:b0:b16:4869:6621]) (user=changyuanl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:dacf:b0:220:df73:b639 with SMTP id d9443c01a7336-22e10393d3dmr10129945ad.36.1746140112204; Thu, 01 May 2025 15:55:12 -0700 (PDT) Date: Thu, 1 May 2025 15:54:09 -0700 In-Reply-To: <20250501225425.635167-1-changyuanl@google.com> Mime-Version: 1.0 References: <20250501225425.635167-1-changyuanl@google.com> X-Mailer: git-send-email 2.49.0.906.g1f30a19c02-goog Message-ID: <20250501225425.635167-3-changyuanl@google.com> Subject: [PATCH v7 02/18] memblock: Add support for scratch memory From: Changyuan Lyu To: linux-kernel@vger.kernel.org Cc: changyuanl@google.com, akpm@linux-foundation.org, 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 Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 657C340003 X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: iq6u3jrjwudjqx1ksmkojex91qq4kr94 X-HE-Tag: 1746140113-912074 X-HE-Meta: U2FsdGVkX1/8lFO0XqYMl+AI34jIqsuxHYhx7Ym/UNf2axjEvicf6SiliFTKNJcoEUwh6RxjzfelVHPHjDnERlodzNtwkON3pBx+e6aAL82IfBdGXkK8unaL2eENauUZucsHXaxdEMkijotcQ24SBNZOBGUANNOLuDNXRB+nrWBvMTdpV3SvW11y4Gk8TOsfExD6DGiFCg23njUQQ19y2/PQA6ElwjPGUG+6uTyqtJJplJbSxLMlgoLcyZoqwAWLQBtkG2Z1y41aLajIOP+neP9puXL1YwCuHOyOotXD3THMH3BXYcf3+gP6G7TA4ldhMCkX7m0DfN0gmeRFWX3B5t/uIRcMFoGm6xDMaootHoxV+1hpfO2KxSC/BrJm9ppRxEGu7aoICUuTWDQgnLAVJVICa8jEEdPfWuNpsnwPvn6F44ASLRYw9GZms/X7HUwHza9LhqqizQ4mHRJd+jAQ53dgiMEZCc4BB70lBUFh1k3JGkMAYGZsEXMa2ZvusDglLleypNRYXMK5JByKXJD+/+Sy78fmAN6WT+EikAaxvtL2CpdX3wbnsXhs9aF5Jh8PneqzkUu/Sg95fvZd8+1lNLzhcLxy/lpjlSXMXbu7QbwA0JD8OaujTo+GNwPOPUvD9gP/+2pr2mXZ/SfO518wrHqPK7m7ZGnApiTbOiWvfXGL0ic97XYeQpAV+ArXUh/YwgJfYl97xu0L9tR/SGGRPNpMnArZ7caGACdWJvWwK3Zixu4v0bJjFadKxazVkS+osduetV+pj4LtmhiYk2CneH61nPKNQGVB8aWmN1M7bTegF/YNb4dxhzBvEtqCoIVAw0Mjwb9npAnvJqHOu/hgzlc24adA/G7VPLMAdu1oG3SMu7A/0K4l232oPcZbu8pqs5b91KLtgIGEfp34GOGpm3EmRS3wsJrXKwHu1QMl8Jk/eT8u1ODB0X8G6RrmuTWlASOR/ZEDb214n3f5Kpu fzFjamUl A5TzgxUpEHM935pVYihdaiG6zT+698GfLX1NUjoOnBhLFfXQw5oDvjycDAg5GOkP2vdCmfREl785sMAS6em/dECL495X82X1BnwVnpNv6ftcsUM1PhXbSvU2a6UYeAmSqxUAGs6Yej9UUiz5C0z14wGoI/uJoXeiO0FuBsZNYFaB4aHQkoX9lADd9J8OBNwXnmc6tIGWIUE/8oAF6mDdMga70psEJsvkSXFYxFmr/nLZZE3ZzoqIZuUWAUEQFc6q5HzyoapRg322b5iiC0LtUefNMAXHo70Mb/cbqBp5205dC1KOp0SSQYG16w+1yYt9XGBtmcVMQfsnTBvfKROWl5jNUpqQCTR/tIidqV6KOrN/T9oVnWYAlbTBRBYwucE5S4RpJBP+oCh93izGC/ZT2wXekr0kqFwyMD8Qxzw3MQ+dZLiwGbLJ1IgF1jnPXc++3G6AxVwnRqnA5NYvr4NmOgkMay7VPXTGLjUhLsJ+ykL9h+FyLDIWGHNquzAbebg48953UowioytlPuayfW0vTL5rq8iqHX9SffD2rLDOyMLRnTKtRAKH8XVo4vX12GMsASFlSTxEmil7nu1zM70wiH7yHVMpxEp/YSERhYPMKcWjaPAljTX7Kgm59oA== 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.906.g1f30a19c02-goog