From patchwork Fri Apr 11 05:37:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Changyuan Lyu X-Patchwork-Id: 14047562 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 A38BDC369A2 for ; Fri, 11 Apr 2025 05:38:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43075280159; Fri, 11 Apr 2025 01:38:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32462280154; Fri, 11 Apr 2025 01:38:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0B692280159; Fri, 11 Apr 2025 01:38:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CDA5F280154 for ; Fri, 11 Apr 2025 01:38:22 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AA9DBC1D45 for ; Fri, 11 Apr 2025 05:38:23 +0000 (UTC) X-FDA: 83320657686.27.75FC944 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf15.hostedemail.com (Postfix) with ESMTP id DE94CA0003 for ; Fri, 11 Apr 2025 05:38:21 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Dvcc9E4r; spf=pass (imf15.hostedemail.com: domain of 3zKr4ZwoKCEspun0tB7n0yt11tyr.p1zyv07A-zzx8npx.14t@flex--changyuanl.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3zKr4ZwoKCEspun0tB7n0yt11tyr.p1zyv07A-zzx8npx.14t@flex--changyuanl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744349901; 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=IlX8nA7Xk9H5c98D5iYmnhzWmAB9L4MDv6xIzYdT0mI=; b=Mm1YbMYUSadUaLyzjriVMyY7r7eWYcP4jWTZL/pBA9uByU9J0Du0Hf9swzCeQUoBNLFOrW P42fR401Mq5Pnbk+h3HGnDDVTHJKZK/4GgdcRB+EHry1fXRdR9L3pl4tM3O4q6+52OfrbF a9CMk8zWO7zxj0qfKIZEJpo9f6K3V4g= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Dvcc9E4r; spf=pass (imf15.hostedemail.com: domain of 3zKr4ZwoKCEspun0tB7n0yt11tyr.p1zyv07A-zzx8npx.14t@flex--changyuanl.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=3zKr4ZwoKCEspun0tB7n0yt11tyr.p1zyv07A-zzx8npx.14t@flex--changyuanl.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1744349901; a=rsa-sha256; cv=none; b=j1KN5VFUSryn13cQ2vxRUmx3tPxzczjDq7sw6qdGzadpQ57TsfNRByCm3vDt1u55XcA2P4 pkcAf93JsSJrYaafje5yLnSK0CewVVZ+XGQXV/yEtPqKQUKEB5xxiihuDbLfep4mVbSHLI /9/8q0c8ZSHA5O8/yZk1LD02j58pBGg= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-227a8cdd272so14415785ad.2 for ; Thu, 10 Apr 2025 22:38:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744349901; x=1744954701; 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=IlX8nA7Xk9H5c98D5iYmnhzWmAB9L4MDv6xIzYdT0mI=; b=Dvcc9E4rGp77iqvS5zXcwxRqQNpTiYknfE8OnWOSa1PxRr3YdCJAVX14YXxV1/Wffu IpcCXPlkt0i09Y5j2fPBI74OYJycAUEB8Og9Og8QgMYUvD4oIlG8UGDkD/pYT129afQR nPKSVPrDEzsGEma/i+WA84oI0fYazLTyEy1+7Kq51xgJIXQwLc/gMYFKWjJ59HgvXwUD TMASW9U4cZ0k2Fwwd2ekFMTPPrVVHl3AZHCWL+9zRoNbmpg9K1QgCIjCg8E5cWhj2wSg fKOVxNmsNurN8D8oOOIXuGVHVzwWUIngIZVQJTyRlytQqsHtWtdgKsVNOK6gu0uLCOZq 9+Yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744349901; x=1744954701; 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=IlX8nA7Xk9H5c98D5iYmnhzWmAB9L4MDv6xIzYdT0mI=; b=rVtqeThC2dvvTuDyvcywneyjv7GeDi0JlSQg0MUKW0THTD6E++3um+Ko2o7H9Z6rYe yxL7i1Q8Is4o18orjHwAJiqV3WKik0d+94Udqv6siQLGIXwfluf44qdYRGI5KtM5RzKV XfFEA1e2aML7I71jls3GQDwncK0fJmfIIOTqY6YoSqLFRRuGPEBrXZA2TrUC4vr72EZ5 AGicPmS1poL/kW+km9oLchl/l+oeADmL2yUXV/9W+sZOiXikCnc1HPdtTyG5/YRZRoJe J81BkH7HIBinaHzqxwK++gscVyAtwlmtp9OnWZivjzJZAl/SUUUPg6qT10rsvq/UK6hO fFxg== X-Forwarded-Encrypted: i=1; AJvYcCXRZJKCLCoHtH06CZs357U1+Gcf7L1ZhChF6Um/XE8JpHHBm032FmTAD6jJnG/0L4WqWnrMeyZMeQ==@kvack.org X-Gm-Message-State: AOJu0YxhHvhBNPup9g+gUp76Yyjpyxkf4JaMH4GrWMUiYRAiLnhg/9Kc 3SHLIw6iPdfjWydGdZPgx+rvlC1CN88niyHumAtBg95ukmGkwuqV8du0PwLJp3bYzcQBPvqUBKp tYlt9bQxkTO6vMNhOfQ== X-Google-Smtp-Source: AGHT+IGmLdsivViTeSGIGhIMJrpZ+eiCfZe7b18ElxHjAINDV7KJLIeK9aAKlBDGOH8e7R2YQZlTLbj0XmtsWn8h X-Received: from plfu7.prod.google.com ([2002:a17:902:e5c7:b0:220:ddee:5ee]) (user=changyuanl job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:c951:b0:21f:68ae:56e3 with SMTP id d9443c01a7336-22bea4f009amr23225795ad.39.1744349900801; Thu, 10 Apr 2025 22:38:20 -0700 (PDT) Date: Thu, 10 Apr 2025 22:37:33 -0700 In-Reply-To: <20250411053745.1817356-1-changyuanl@google.com> Mime-Version: 1.0 References: <20250411053745.1817356-1-changyuanl@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250411053745.1817356-3-changyuanl@google.com> Subject: [PATCH v6 02/14] memblock: Add support for scratch memory From: Changyuan Lyu To: linux-kernel@vger.kernel.org Cc: 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, Changyuan Lyu X-Rspamd-Queue-Id: DE94CA0003 X-Rspamd-Server: rspam05 X-Rspam-User: X-Stat-Signature: buo9nqdgnx4bfqyirbxyjt83wxmu5dis X-HE-Tag: 1744349901-814440 X-HE-Meta: U2FsdGVkX1+QgMW1AJNHw5+McG/GQIg5mTvu173BXdrhc0oIuwk/mPZjeO/5ZX8osGucUiFgZRzPitUnKhfrjvSorjzE85i9htrTpIf3/QLfg76qJFNTHrfFwMRgdalHxiLTUJF+0j3XYwrasJIhPckG8MbhukxUsRksfyJEpFsmqAj18RH0lCQqd82mRqZv574DrOiYjf+PI/0pc9GNZfhMruSNiOlclxr9eCv2jlc+VWTimmuMB6hSIZOCnfjpN7HJl4Kd0uWXxJkmVAvRyk3LQS1oD2VEp1wNoYffQw/VV4wgJhKpuBr0KioSMAHp+c03/VS9DKSjNpq0qUObzWFNX7YvxFbgDCR6TIaOpd689TNieeWvZYkrfhGfQLHe4qdX9SSdBvqqMclff2M/TpemMgHVChhGDVCO6W31KlqrQJQwIamPDvOTzt57e9dEPQbWLT3NhBHgDu0es0E4rCVQi9+sczE9GAPveLw57zHb8J3zXnXITyShj6n4yebQPre85O3e2c9sSmbp4+Tu62/D5Gigb8C755tSDTf1KilNKwG0sAWfUXCBq2lUqB0SZ3U0JFgUaLmYA6OSZ+C09BlcjSKzZ3yQigk7CGhOG9x6aNG47sTJ4yXTFqee4+5UbEYr1huPPA8yxMCvHWmj3MrI2qP/38N9XyCDZImpSp1hZgSlwX0nGTQHydGq6ItvpUA5H1V6JMuB50oK2Znum1Pdn5Z8On/S0Tq/xcbuvRZ5ivMT59y1BSDsOXcuZktdaUqrHXPwWEWHbHeu5lLCuBSorSRkDvcLTG1mToVUvzF37Vrg3ak+owgYjhTvOjcsqz3GFNvyHlwHrVJelpDeYYwjszVwOS+Mv8SBeCPqe92BZcxvMVMKOl5eIwkeHMaBfZM4KGh9EfClZXAlQg66s2B/TBsy07bd312CUhXWYNrxtf/2ITaUOf4P+AV8HqgkpPKTonejJIw+JGp/NXs PjBf/DQ7 +Lj+6szlNLf2OmV1tBfVopS+3RKjPaS46S3iuOsbLVqPYcLotn/DuHjZ7c9d/Bx0bIRosyztVn03PyWE/zSgTtm+5oHCfE+OW2Fi9hy2q2c+wWFhalPdLLe5SuIsINVwkeEabQB6SF3KnlskshdhGvIGuLMGhas7ju0TbhwnNGAjggNIRRB5QynWyEwvsoQhW5hMVMW05G4eYi7NCeknx8KW1mJC8aWaO2//43CdZ4cim8fZIIMVYAI5nE/0bL0IhvXGvCPQPTx79ar0TbCfL/hokb5VNDwth5Ldtee9bhDP7H6rKGmSdolIDqtbOZ0ONV/QRaZmAZf+pzQGzvfGkbM3MzE+Wv+vpk4dd18PqVQiXxtYKJDHq65ZpKmriiC2auYAmI8Yg0WSiDVUM2DCA7iykG1TcVStk7lkYVE6GiWB4XJnL6eU8ahEFCX4R5ul/lZmDA9phEDKLTx7Pq8w/Wze1oM7xDUp0PKy+Lr86q5Lq7ClgX2nFzdxBlDtDUoQ6MdoIC951KJ7vyME4DBhSp5HQraLVhewC11wi947KJYUkeCWxC5NSNvhusI943ZF6BTYI+zGrGxJ1LXQA27juipjFluJaDp0Kfc7cKJgayOalZ4iDQb63rkwUDQ== 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..3a213e2a485bc 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_memblock void memblock_set_kho_scratch_only(void) +{ + kho_scratch_only = true; +} + +__init_memblock 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. + */ +int __init_memblock 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. + */ +int __init_memblock 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)