From patchwork Wed Dec 13 00:04:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177678 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8080154vqy; Tue, 12 Dec 2023 16:05:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IEBK9umDpbryvuW8K+C5IjpR4FznK8zr1H7GLlZfh/pyyvh/P+hxIaz523n6y+THQxeISTm X-Received: by 2002:a05:6358:9192:b0:16e:2740:7cfc with SMTP id j18-20020a056358919200b0016e27407cfcmr8251426rwa.25.1702425920650; Tue, 12 Dec 2023 16:05:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702425920; cv=none; d=google.com; s=arc-20160816; b=CqRJl9sbJJzLF0H4fUoJqrYPCRQm7sfIjOgSd5bLZ/fZfPReK964vt5G1K9O1rMnsp 7Df2Seo++bYGJHh9aIP9wwwvLQzJKeY1NZz0ZgbbRspb7tPDxWTum3ELGd4p+kj2HIP7 gQp96j2uD0QfI2TgRuCxQGlHY3gIn0W1hNM+uCi3+QE05MF7Juoe609953tc7CqNEQL/ jQiIvUiwvsB2qI92NtjPtKsLj9ZYjqUfXRydkbUxCztINf5ODCYtVmHJy7RJodvMK1c2 fxK7eyNKw29Pqp0Yoj07rn6T/ZfA79geON2DYruYRAVc1rjiTkO5Mu76zLy9YSnmJ8c0 Aneg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2/zyadG4/H/PJTWoJtupWHcuxbsnDpMhuO8wY//fQ4A=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=A6p7eNwwcxaWlQ1nnGSLmavAkDOiYw0t7TOXqMW+YBB9Sabn8hEI7zkBs4suLCEjUN yVu/x+35QV8GymLu54eWNRb3BUE6UzXQE+NUcCTJeHdM8wgqwF9I4BuKL38dIci1IStd EuCYWB2C5ItkyXLDp7FqdXS7XSBpw7aVAbkeVcmw4IvYd01UPlCUibBo7jkCtC2NrQy6 59ZL0Eicv+kw/Xos6GDR4pXIDcd1q3vUoesnuIknkR9GWiZYdiAz5h7eoFQeKBPCds5a 3HxzZgh1DBpaB2QPA0J9OrZBbZaA39D/cSddGQoYeTPThwde6mh4z/ghnr6xSI6aB9pp JXRQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="c8qYexr/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id m188-20020a6326c5000000b005c661f15600si8238283pgm.399.2023.12.12.16.05.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:05:20 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="c8qYexr/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 37F2E80B284A; Tue, 12 Dec 2023 16:05:14 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377632AbjLMAFF (ORCPT + 99 others); Tue, 12 Dec 2023 19:05:05 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377492AbjLMAFE (ORCPT ); Tue, 12 Dec 2023 19:05:04 -0500 Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C3BE3BC; Tue, 12 Dec 2023 16:05:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425910; x=1733961910; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2/zyadG4/H/PJTWoJtupWHcuxbsnDpMhuO8wY//fQ4A=; b=c8qYexr/7nuijexVqvHa/sjdBBaTUc27spRejHSbtAVYXBUtP2OHrqw8 wXajPf9DOxwlGdURVsu9XWnIN6zwTpwiuL7YgU4AUbMl84wJ12DtAk8Zk 0VWfUggQppPAvRLTEcBZyvND8BWmXlUItj9BSUgFFB9mbmnDKkJaVkjaN 0=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="600238157" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-1cca8d67.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:05:05 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2a-m6i4x-1cca8d67.us-west-2.amazon.com (Postfix) with ESMTPS id D22C680658; Wed, 13 Dec 2023 00:05:02 +0000 (UTC) Received: from EX19MTAUWB001.ant.amazon.com [10.0.21.151:56515] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.2.31:2525] with esmtp (Farcaster) id f13a4a39-aa9b-4426-8e9f-ccc718085517; Wed, 13 Dec 2023 00:05:02 +0000 (UTC) X-Farcaster-Flow-ID: f13a4a39-aa9b-4426-8e9f-ccc718085517 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:02 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:04:58 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 01/15] mm,memblock: Add support for scratch memory Date: Wed, 13 Dec 2023 00:04:38 +0000 Message-ID: <20231213000452.88295-2-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D031UWC001.ant.amazon.com (10.13.139.241) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:05:14 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785122962255935521 X-GMAIL-MSGID: 1785122962255935521 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 reserved memblocks array which may not be big enough to hold all allocations. To resize the array, we need to allocate memory. That brings us into a catch 22 situation. The solution to that is the scratch region: a safe region to operate in. KHO provides a "scratch region" as part of its metadata. This scratch region is a single, contiguous memory block that we know does not contain any KHO allocations. We can exclusively allocate from there until we finish kernel initialization to a point where it knows about all the KHO memory reservations. We introduce a new memblock_set_scratch_only() function that allows KHO to indicate that any memblock allocation must happen from the scratch region. Later, we may want to perform another KHO kexec. For that, we reuse the same scratch region. To ensure that no eventually handed over data gets allocated inside that 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 --- include/linux/memblock.h | 19 +++++++++++++ mm/Kconfig | 4 +++ mm/memblock.c | 61 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index ae3bde302f70..14043f5b696f 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -42,6 +42,10 @@ extern unsigned long long max_possible_pfn; * kernel resource tree. * @MEMBLOCK_RSRV_NOINIT: memory region for which struct pages are * not initialized (only for reserved regions). + * @MEMBLOCK_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 */ @@ -50,6 +54,7 @@ enum memblock_flags { MEMBLOCK_NOMAP = 0x4, /* don't add to kernel direct mapping */ MEMBLOCK_DRIVER_MANAGED = 0x8, /* always detected via a driver */ MEMBLOCK_RSRV_NOINIT = 0x10, /* don't initialize struct pages */ + MEMBLOCK_SCRATCH = 0x20, /* scratch memory for kexec handover */ }; /** @@ -129,6 +134,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_scratch(phys_addr_t base, phys_addr_t size); +int memblock_clear_scratch(phys_addr_t base, phys_addr_t size); void memblock_free_all(void); void memblock_free(void *ptr, size_t size); @@ -273,6 +280,11 @@ static inline bool memblock_is_driver_managed(struct memblock_region *m) return m->flags & MEMBLOCK_DRIVER_MANAGED; } +static inline bool memblock_is_scratch(struct memblock_region *m) +{ + return m->flags & MEMBLOCK_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, @@ -610,5 +622,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_SCRATCH +void memblock_set_scratch_only(void); +void memblock_clear_scratch_only(void); +#else +static inline void memblock_set_scratch_only(void) { } +static inline void memblock_clear_scratch_only(void) { } +#endif #endif /* _LINUX_MEMBLOCK_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 89971a894b60..36f5e7d95195 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -513,6 +513,10 @@ config ARCH_WANT_OPTIMIZE_HUGETLB_VMEMMAP config HAVE_MEMBLOCK_PHYS_MAP bool +# Enable memblock support for scratch memory which is needed for KHO +config MEMBLOCK_SCRATCH + bool + config HAVE_FAST_GUP depends on MMU bool diff --git a/mm/memblock.c b/mm/memblock.c index 5a88d6d24d79..e89e6c8f9d75 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -106,6 +106,13 @@ unsigned long min_low_pfn; unsigned long max_pfn; unsigned long long max_possible_pfn; +#ifdef CONFIG_MEMBLOCK_SCRATCH +/* When set to true, only allocate from MEMBLOCK_SCRATCH ranges */ +static bool scratch_only; +#else +#define 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 @@ -168,6 +175,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 (scratch_only) + return MEMBLOCK_SCRATCH; + return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; } @@ -643,7 +654,7 @@ static int __init_memblock memblock_add_range(struct memblock_type *type, #ifdef CONFIG_NUMA WARN_ON(nid != memblock_get_region_node(rgn)); #endif - WARN_ON(flags != rgn->flags); + WARN_ON(flags != (rgn->flags & ~MEMBLOCK_SCRATCH)); nr_new++; if (insert) { if (start_rgn == -1) @@ -890,6 +901,18 @@ int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size) } #endif +#ifdef CONFIG_MEMBLOCK_SCRATCH +__init_memblock void memblock_set_scratch_only(void) +{ + scratch_only = true; +} + +__init_memblock void memblock_clear_scratch_only(void) +{ + scratch_only = false; +} +#endif + /** * memblock_setclr_flag - set or clear flag for a memory region * @type: memblock type to set/clear flag for @@ -1015,6 +1038,33 @@ int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t MEMBLOCK_RSRV_NOINIT); } +/** + * memblock_mark_scratch - Mark a memory region with flag MEMBLOCK_SCRATCH. + * @base: the base phys addr of the region + * @size: the size of the region + * + * Only memory regions marked with %MEMBLOCK_SCRATCH will be considered for + * allocations during early boot with kexec handover. + * + * Return: 0 on success, -errno on failure. + */ +int __init_memblock memblock_mark_scratch(phys_addr_t base, phys_addr_t size) +{ + return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_SCRATCH); +} + +/** + * memblock_clear_scratch - Clear flag MEMBLOCK_SCRATCH 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_scratch(phys_addr_t base, phys_addr_t size) +{ + return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_SCRATCH); +} + static bool should_skip_region(struct memblock_type *type, struct memblock_region *m, int nid, int flags) @@ -1046,6 +1096,14 @@ 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 kho, we can only consider scratch allocations */ + if ((flags & MEMBLOCK_SCRATCH) && !memblock_is_scratch(m)) + return true; + + /* Leave scratch memory alone after scratch-only phase */ + if (!(flags & MEMBLOCK_SCRATCH) && memblock_is_scratch(m)) + return true; + return false; } @@ -2211,6 +2269,7 @@ static const char * const flagname[] = { [ilog2(MEMBLOCK_MIRROR)] = "MIRROR", [ilog2(MEMBLOCK_NOMAP)] = "NOMAP", [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG", + [ilog2(MEMBLOCK_SCRATCH)] = "SCRATCH", }; static int memblock_debug_show(struct seq_file *m, void *private) From patchwork Wed Dec 13 00:04:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177679 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8080356vqy; Tue, 12 Dec 2023 16:05:43 -0800 (PST) X-Google-Smtp-Source: AGHT+IGgbukIDk0jAECAj57r19foSODyXlw5HZ22cENljLAc4AuH1qkRJcTNARm9dB+4tToVp4qx X-Received: by 2002:a17:90b:a0e:b0:28a:e183:c6ef with SMTP id gg14-20020a17090b0a0e00b0028ae183c6efmr82924pjb.13.1702425943082; Tue, 12 Dec 2023 16:05:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702425943; cv=none; d=google.com; s=arc-20160816; b=i1Mm6lS6+nQkLBffq8bsgM/5mg/YaGgeU1WppHWdwtcVtnD/e8DufletZb2xA3Keu4 hCIrUpmAAgWHfqf1L7je6byUaBoXLprOxpK4a+cp8uWgYh+9F9+6KoZQD5zAiSlfWXis xgEAvbUNsjVT5HbVEU34+898tCGgTKuvDEcfFLuxukjav4jMJ7dZDmNOXaVgsTu+XpPE vVLR0FOBZBQ7bGVg0QbAxPVqszqN+/EaRy+ktQddW+EUzmiZkeKSV93+7ChtXpzJaM12 9TmgCPIZOMxrLhJ4xO8XmOcIJYzZMJT8pFrE1ACT8azSVG/hK3LtRZ6BtUn+JK/972Sl jLEQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=omkZqOlO4bdGSVCdXn2us+8UWaNPdUSggP3ul/1DY+c=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=I422nXMNN12uNWFnvQpSvG7h2GswO9gu8lPzTlyWiSi/yytd8Iakvnjga6Q/WuiwoU PAVsV61q3lR59d64PBaiRFHx7lUurDEzlzsPzlVFVqzuRvNRwKdDGsZ6mm63EPSWhqFU pEOUVhfD2amOekW22VopNwaHrOvYzZgV5H448UXGTvHadJa1pQ/QoxY1b43XDHHAUE2a 8ti+YyGJ3Yw2gucVSFNk1jDZoSQu6XhYwN8IoEYiJZUGBfYPKJZkjK6chbLTKPtHNn0N ZTZK0xK119g1zEuR7DpkKpEicQGqLtTr7hMsFBfhAntETrKMe36smd0wkpJEGXQvMM3a qq2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=X9Yt1Xsm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id f11-20020a17090ab94b00b002869a5326ddsi9861077pjw.75.2023.12.12.16.05.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:05:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=X9Yt1Xsm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 6091C804B2A4; Tue, 12 Dec 2023 16:05:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377626AbjLMAFR (ORCPT + 99 others); Tue, 12 Dec 2023 19:05:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377657AbjLMAFN (ORCPT ); Tue, 12 Dec 2023 19:05:13 -0500 Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DB79E8; Tue, 12 Dec 2023 16:05:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425919; x=1733961919; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=omkZqOlO4bdGSVCdXn2us+8UWaNPdUSggP3ul/1DY+c=; b=X9Yt1XsmBO8hon59sSvxtQj8sudWqrFA2eMdbdpBcQ7uvQVRzlSxKrQO OgbmJ+MikfkjHekk6fTOHfUOOaW5/W3wi27SdbsmJK7gFp6xC4x6MaBSN +v/6pCqlkBiJmxKg2mK6bWWImATRTVukCgdh4ubZa9hSTvH9GZbvI2+t8 Y=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="258636775" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-d23e07e8.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-80006.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:05:16 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-d23e07e8.us-east-1.amazon.com (Postfix) with ESMTPS id 8651A80643; Wed, 13 Dec 2023 00:05:07 +0000 (UTC) Received: from EX19MTAUWA001.ant.amazon.com [10.0.21.151:38785] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.34.165:2525] with esmtp (Farcaster) id ac23c6ff-3564-413c-9f1d-e68dec397645; Wed, 13 Dec 2023 00:05:06 +0000 (UTC) X-Farcaster-Flow-ID: ac23c6ff-3564-413c-9f1d-e68dec397645 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWA001.ant.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:06 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:02 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 02/15] memblock: Declare scratch memory as CMA Date: Wed, 13 Dec 2023 00:04:39 +0000 Message-ID: <20231213000452.88295-3-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D031UWC001.ant.amazon.com (10.13.139.241) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:05:40 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785122985748595813 X-GMAIL-MSGID: 1785122985748595813 When we finish populating our memory, we don't want to lose the scratch region as memory we can use for useful data. Do do that, we mark it as CMA memory. That means that any allocation within it only happens with movable memory which we can then happily discard for the next kexec. That way we don't lose the scratch region's memory anymore for allocations after boot. Signed-off-by: Alexander Graf --- mm/memblock.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/mm/memblock.c b/mm/memblock.c index e89e6c8f9d75..44741424dab7 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include @@ -1100,10 +1101,6 @@ static bool should_skip_region(struct memblock_type *type, if ((flags & MEMBLOCK_SCRATCH) && !memblock_is_scratch(m)) return true; - /* Leave scratch memory alone after scratch-only phase */ - if (!(flags & MEMBLOCK_SCRATCH) && memblock_is_scratch(m)) - return true; - return false; } @@ -2153,6 +2150,20 @@ static void __init __free_pages_memory(unsigned long start, unsigned long end) } } +static void reserve_scratch_mem(phys_addr_t start, phys_addr_t end) +{ +#ifdef CONFIG_MEMBLOCK_SCRATCH + ulong start_pfn = pageblock_start_pfn(PFN_DOWN(start)); + ulong end_pfn = pageblock_align(PFN_UP(end)); + ulong pfn; + + for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages) { + /* Mark as CMA to prevent kernel allocations in it */ + set_pageblock_migratetype(pfn_to_page(pfn), MIGRATE_CMA); + } +#endif +} + static unsigned long __init __free_memory_core(phys_addr_t start, phys_addr_t end) { @@ -2214,6 +2225,17 @@ static unsigned long __init free_low_memory_core_early(void) memmap_init_reserved_pages(); +#ifdef CONFIG_MEMBLOCK_SCRATCH + /* + * Mark scratch mem as CMA before we return it. That way we ensure that + * no kernel allocations happen on it. That means we can reuse it as + * scratch memory again later. + */ + __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, + MEMBLOCK_SCRATCH, &start, &end, NULL) + reserve_scratch_mem(start, end); +#endif + /* * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id * because in some case like Node0 doesn't have RAM installed From patchwork Wed Dec 13 00:04:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177680 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8080788vqy; Tue, 12 Dec 2023 16:06:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IFaBUuLnyp3zBP5Gk+UM4kGtQKYz/k36lmzlVCrrG/mbsprmU2+Iip4gSlOKhh3haVeNfyj X-Received: by 2002:a92:c264:0:b0:35d:59a2:9295 with SMTP id h4-20020a92c264000000b0035d59a29295mr8345036ild.41.1702425988660; Tue, 12 Dec 2023 16:06:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702425988; cv=none; d=google.com; s=arc-20160816; b=wjABMwPeOrmQQwhl19+JCXGFhmm1N8a4ebGrNuHFVj4KqT6enw9j7brRNd8MWsQdm5 EQGGfer9NDLSB36hugz62XUs+QLqkJyI+jTznFEdWOJyWHPhfs5+Le/+5DvsmLXaz9mC O0ZiO7od+J99flnlCcJe4DXxK7TxrSqVnr15nMR9Fm25/dtZqHQ5SMgb0Fwcn1kocOtR gN4dyLbSs12cC+4QytcAPRIkuVn7pFCcUiLoPtm/24NxdhwodrbDFfFEybeK8Yl1fg/P cblV0541iAQUZm3SUzFMPThHLzk71wa3LhKH+jIPFWYsP61qW2KP4JjYK4eXk1VY7Q7f 8cXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bvtk3HQrKsl8B+ppqatJMPW6FdHejxoz+znJi2t9qtA=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=En/Quo74GdPelF8M2Q/7fhiaFmrXHNsHMsLVXYv1x7omW37jDX1Ml9QlIzrwMn2Ji/ QCxsFMF1Q8WxVuwt0Dc2QrG6hefyix4HxoHEPGZ799St5yHkalALVFT+aPA4SOF7tTum bbpFH2e9pnV2/sCjRvB6p+l4EWkkU19u8cIXsLnoQ8iGVzMvyVH/HvMD6InEM2skLuAk UCeAeMpwhAY7tCIvzyTRv1+kq19upLm0dzb7KaacsmGSDH00LUte7yBJ/VHNuo/enx3J byjD8fSQxzHW4IppEeLePkVkhIB6MLIWaf6m8nWnnBWEOwBZRJHv2l4/WiL30X8JrzD5 3qGw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="a//RTaD/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id g5-20020a636b05000000b005c685a1b3b4si8410336pgc.347.2023.12.12.16.06.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:06:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b="a//RTaD/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id B16DE80896AD; Tue, 12 Dec 2023 16:05:57 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377625AbjLMAFs (ORCPT + 99 others); Tue, 12 Dec 2023 19:05:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34764 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377750AbjLMAFq (ORCPT ); Tue, 12 Dec 2023 19:05:46 -0500 Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A002C12B; Tue, 12 Dec 2023 16:05:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425944; x=1733961944; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bvtk3HQrKsl8B+ppqatJMPW6FdHejxoz+znJi2t9qtA=; b=a//RTaD/sBFScXhoV2eQWtTUWHkueFJa3zGfCfSDKmAkc6hMsS38X4Q2 ochk8SP5Q3qFAilPbaY8k3xE1nFFj60HpU8azhs+ES7OY4iTTh4W6Uvbw tOzjNIjoBzUjImPUBOkXojvtcoG7XtGo+FNnCeu+Yquf+UnXzf2uEaqgz g=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="375754434" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-a893d89c.us-west-2.amazon.com) ([10.43.8.2]) by smtp-border-fw-6001.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:05:39 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2b-m6i4x-a893d89c.us-west-2.amazon.com (Postfix) with ESMTPS id CDB7D40D47; Wed, 13 Dec 2023 00:05:32 +0000 (UTC) Received: from EX19MTAUWB002.ant.amazon.com [10.0.7.35:49387] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.11.243:2525] with esmtp (Farcaster) id 1de1abc8-aa8e-477e-a407-6ac5ab261a33; Wed, 13 Dec 2023 00:05:32 +0000 (UTC) X-Farcaster-Flow-ID: 1de1abc8-aa8e-477e-a407-6ac5ab261a33 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:31 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:27 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 03/15] kexec: Add Kexec HandOver (KHO) generation helpers Date: Wed, 13 Dec 2023 00:04:40 +0000 Message-ID: <20231213000452.88295-4-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:05:58 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123033453623445 X-GMAIL-MSGID: 1785123033453623445 This patch adds the core infrastructure to generate Kexec HandOver metadata. Kexec HandOver is a mechanism that allows Linux to preserve state - arbitrary properties as well as memory locations - across kexec. It does so using 3 concepts: 1) Device Tree - Every KHO kexec carries a KHO specific flattened device tree blob that describes the state of the system. Device drivers can register to KHO to serialize their state before kexec. 2) Mem cache - A memblocks like structure that contains full page ranges of reservations. These can not be part of the architectural reservations, because they differ on every kexec. 3) Scratch Region - A CMA region that we allocate in the first kernel. CMA gives us the guarantee that no handover pages land in that region, because handover pages must be at a static physical memory location. We use this region as the place to load future kexec images into which then won't collide with any handover data. Signed-off-by: Alexander Graf --- Documentation/ABI/testing/sysfs-kernel-kho | 53 +++ .../admin-guide/kernel-parameters.txt | 10 + MAINTAINERS | 1 + include/linux/kexec.h | 24 ++ include/uapi/linux/kexec.h | 6 + kernel/Makefile | 1 + kernel/kexec_kho_out.c | 316 ++++++++++++++++++ 7 files changed, 411 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-kernel-kho create mode 100644 kernel/kexec_kho_out.c diff --git a/Documentation/ABI/testing/sysfs-kernel-kho b/Documentation/ABI/testing/sysfs-kernel-kho new file mode 100644 index 000000000000..f69e7b81a337 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-kernel-kho @@ -0,0 +1,53 @@ +What: /sys/kernel/kho/active +Date: December 2023 +Contact: Alexander Graf +Description: + Kexec HandOver (KHO) allows Linux to transition the state of + compatible drivers into the next kexec'ed kernel. To do so, + device drivers will serialize their current state into a DT. + While the state is serialized, they are unable to perform + any modifications to state that was serialized, such as + handed over memory allocations. + + When this file contains "1", the system is in the transition + state. When contains "0", it is not. To switch between the + two states, echo the respective number into this file. + +What: /sys/kernel/kho/dt_max +Date: December 2023 +Contact: Alexander Graf +Description: + KHO needs to allocate a buffer for the DT that gets + generated before it knows the final size. By default, it + will allocate 10 MiB for it. You can write to this file + to modify the size of that allocation. + +What: /sys/kernel/kho/scratch_len +Date: December 2023 +Contact: Alexander Graf +Description: + To support continuous KHO kexecs, we need to reserve a + physically contiguous memory region that will always stay + available for future kexec allocations. This file describes + the length of that memory region. Kexec user space tooling + can use this to determine where it should place its payload + images. + +What: /sys/kernel/kho/scratch_phys +Date: December 2023 +Contact: Alexander Graf +Description: + To support continuous KHO kexecs, we need to reserve a + physically contiguous memory region that will always stay + available for future kexec allocations. This file describes + the physical location of that memory region. Kexec user space + tooling can use this to determine where it should place its + payload images. + +What: /sys/kernel/kho/dt +Date: December 2023 +Contact: Alexander Graf +Description: + When KHO is active, the kernel exposes the generated DT that + carries its current KHO state in this file. Kexec user space + tooling can use this as input file for the KHO payload image. diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt index 51575cd31741..efeef075617e 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -2504,6 +2504,16 @@ kgdbwait [KGDB] Stop kernel execution and enter the kernel debugger at the earliest opportunity. + kho_scratch=n[KMG] [KEXEC] Sets the size of the KHO scratch + region. The KHO scratch region is a physically + memory range that can only be used for non-kernel + allocations. That way, even when memory is heavily + fragmented with handed over memory, kexec will always + be able to find contiguous memory to place the next + kernel for kexec into. + + The default is 0. + kmac= [MIPS] Korina ethernet MAC address. Configure the RouterBoard 532 series on-chip Ethernet adapter MAC address. diff --git a/MAINTAINERS b/MAINTAINERS index 788be9ab5b73..4ebf7c5fd424 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11769,6 +11769,7 @@ M: Eric Biederman L: kexec@lists.infradead.org S: Maintained W: http://kernel.org/pub/linux/utils/kernel/kexec/ +F: Documentation/ABI/testing/sysfs-kernel-kho F: include/linux/kexec.h F: include/uapi/linux/kexec.h F: kernel/kexec* diff --git a/include/linux/kexec.h b/include/linux/kexec.h index 8227455192b7..db2597e5550d 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -21,6 +21,8 @@ #include #include +#include +#include extern note_buf_t __percpu *crash_notes; @@ -516,6 +518,28 @@ void set_kexec_sig_enforced(void); static inline void set_kexec_sig_enforced(void) {} #endif +#ifdef CONFIG_KEXEC_KHO +/* Notifier index */ +enum kho_event { + KEXEC_KHO_DUMP = 0, + KEXEC_KHO_ABORT = 1, +}; + +extern phys_addr_t kho_scratch_phys; +extern phys_addr_t kho_scratch_len; + +/* egest handover metadata */ +void kho_reserve(void); +int register_kho_notifier(struct notifier_block *nb); +int unregister_kho_notifier(struct notifier_block *nb); +bool kho_is_active(void); +#else +static inline void kho_reserve(void) { } +static inline int register_kho_notifier(struct notifier_block *nb) { return -EINVAL; } +static inline int unregister_kho_notifier(struct notifier_block *nb) { return -EINVAL; } +static inline bool kho_is_active(void) { return false; } +#endif + #endif /* !defined(__ASSEBMLY__) */ #endif /* LINUX_KEXEC_H */ diff --git a/include/uapi/linux/kexec.h b/include/uapi/linux/kexec.h index 01766dd839b0..d02ffd5960d6 100644 --- a/include/uapi/linux/kexec.h +++ b/include/uapi/linux/kexec.h @@ -49,6 +49,12 @@ /* The artificial cap on the number of segments passed to kexec_load. */ #define KEXEC_SEGMENT_MAX 16 +/* KHO passes an array of kho_mem as "mem cache" to the new kernel */ +struct kho_mem { + __u64 addr; + __u64 len; +}; + #ifndef __KERNEL__ /* * This structure is used to hold the arguments that are used when diff --git a/kernel/Makefile b/kernel/Makefile index 3947122d618b..a6bd31e22c09 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -73,6 +73,7 @@ obj-$(CONFIG_KEXEC_CORE) += kexec_core.o obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC_FILE) += kexec_file.o obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o +obj-$(CONFIG_KEXEC_KHO) += kexec_kho_out.o obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_CGROUPS) += cgroup/ diff --git a/kernel/kexec_kho_out.c b/kernel/kexec_kho_out.c new file mode 100644 index 000000000000..e6184bde5c10 --- /dev/null +++ b/kernel/kexec_kho_out.c @@ -0,0 +1,316 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * kexec_kho_out.c - kexec handover code to egest metadata. + * Copyright (C) 2023 Alexander Graf + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include + +struct kho_out { + struct kobject *kobj; + bool active; + struct cma *cma; + struct blocking_notifier_head chain_head; + void *dt; + u64 dt_len; + u64 dt_max; + struct mutex lock; +}; + +static struct kho_out kho = { + .dt_max = (1024 * 1024 * 10), + .chain_head = BLOCKING_NOTIFIER_INIT(kho.chain_head), + .lock = __MUTEX_INITIALIZER(kho.lock), +}; + +/* + * Size for scratch (non-KHO) memory. With KHO enabled, memory can become + * fragmented because KHO regions may be anywhere in physical address + * space. The scratch region gives us a safe zone that we will never see + * KHO allocations from. This is where we can later safely load our new kexec + * images into. + */ +static phys_addr_t kho_scratch_size __initdata; + +int register_kho_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&kho.chain_head, nb); +} +EXPORT_SYMBOL_GPL(register_kho_notifier); + +int unregister_kho_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&kho.chain_head, nb); +} +EXPORT_SYMBOL_GPL(unregister_kho_notifier); + +bool kho_is_active(void) +{ + return kho.active; +} +EXPORT_SYMBOL_GPL(kho_is_active); + +static ssize_t raw_read(struct file *file, struct kobject *kobj, + struct bin_attribute *attr, char *buf, + loff_t pos, size_t count) +{ + mutex_lock(&kho.lock); + memcpy(buf, attr->private + pos, count); + mutex_unlock(&kho.lock); + + return count; +} + +static BIN_ATTR(dt, 0400, raw_read, NULL, 0); + +static int kho_expose_dt(void *fdt) +{ + long fdt_len = fdt_totalsize(fdt); + int err; + + kho.dt = fdt; + kho.dt_len = fdt_len; + + bin_attr_dt.size = fdt_totalsize(fdt); + bin_attr_dt.private = fdt; + err = sysfs_create_bin_file(kho.kobj, &bin_attr_dt); + + return err; +} + +static void kho_abort(void) +{ + if (!kho.active) + return; + + sysfs_remove_bin_file(kho.kobj, &bin_attr_dt); + + kvfree(kho.dt); + kho.dt = NULL; + kho.dt_len = 0; + + blocking_notifier_call_chain(&kho.chain_head, KEXEC_KHO_ABORT, NULL); + + kho.active = false; +} + +static int kho_serialize(void) +{ + void *fdt = NULL; + int err; + + kho.active = true; + err = -ENOMEM; + + fdt = kvmalloc(kho.dt_max, GFP_KERNEL); + if (!fdt) + goto out; + + if (fdt_create(fdt, kho.dt_max)) { + err = -EINVAL; + goto out; + } + + err = fdt_finish_reservemap(fdt); + if (err) + goto out; + + err = fdt_begin_node(fdt, ""); + if (err) + goto out; + + err = fdt_property_string(fdt, "compatible", "kho-v1"); + if (err) + goto out; + + /* Loop through all kho dump functions */ + err = blocking_notifier_call_chain(&kho.chain_head, KEXEC_KHO_DUMP, fdt); + err = notifier_to_errno(err); + if (err) + goto out; + + /* Close / */ + err = fdt_end_node(fdt); + if (err) + goto out; + + err = fdt_finish(fdt); + if (err) + goto out; + + if (WARN_ON(fdt_check_header(fdt))) { + err = -EINVAL; + goto out; + } + + err = kho_expose_dt(fdt); + +out: + if (err) { + pr_err("kho failed to serialize state: %d", err); + kho_abort(); + } + return err; +} + +/* Handling for /sys/kernel/kho */ + +#define KHO_ATTR_RO(_name) static struct kobj_attribute _name##_attr = __ATTR_RO_MODE(_name, 0400) +#define KHO_ATTR_RW(_name) static struct kobj_attribute _name##_attr = __ATTR_RW_MODE(_name, 0600) + +static ssize_t active_store(struct kobject *dev, struct kobj_attribute *attr, + const char *buf, size_t size) +{ + ssize_t retsize = size; + bool val = false; + int ret; + + if (kstrtobool(buf, &val) < 0) + return -EINVAL; + + if (!kho_scratch_len) + return -ENOMEM; + + mutex_lock(&kho.lock); + if (val != kho.active) { + if (val) { + ret = kho_serialize(); + if (ret) { + retsize = -EINVAL; + goto out; + } + } else { + kho_abort(); + } + } + +out: + mutex_unlock(&kho.lock); + return retsize; +} + +static ssize_t active_show(struct kobject *dev, struct kobj_attribute *attr, + char *buf) +{ + ssize_t ret; + + mutex_lock(&kho.lock); + ret = sysfs_emit(buf, "%d\n", kho.active); + mutex_unlock(&kho.lock); + + return ret; +} +KHO_ATTR_RW(active); + +static ssize_t dt_max_store(struct kobject *dev, struct kobj_attribute *attr, + const char *buf, size_t size) +{ + u64 val; + + if (kstrtoull(buf, 0, &val)) + return -EINVAL; + + kho.dt_max = val; + + return size; +} + +static ssize_t dt_max_show(struct kobject *dev, struct kobj_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "0x%llx\n", kho.dt_max); +} +KHO_ATTR_RW(dt_max); + +static ssize_t scratch_len_show(struct kobject *dev, struct kobj_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "0x%llx\n", kho_scratch_len); +} +KHO_ATTR_RO(scratch_len); + +static ssize_t scratch_phys_show(struct kobject *dev, struct kobj_attribute *attr, + char *buf) +{ + return sysfs_emit(buf, "0x%llx\n", kho_scratch_phys); +} +KHO_ATTR_RO(scratch_phys); + +static __init int kho_out_init(void) +{ + int ret = 0; + + kho.kobj = kobject_create_and_add("kho", kernel_kobj); + if (!kho.kobj) { + ret = -ENOMEM; + goto err; + } + + ret = sysfs_create_file(kho.kobj, &active_attr.attr); + if (ret) + goto err; + + ret = sysfs_create_file(kho.kobj, &dt_max_attr.attr); + if (ret) + goto err; + + ret = sysfs_create_file(kho.kobj, &scratch_phys_attr.attr); + if (ret) + goto err; + + ret = sysfs_create_file(kho.kobj, &scratch_len_attr.attr); + if (ret) + goto err; + +err: + return ret; +} +late_initcall(kho_out_init); + +static int __init early_kho_scratch(char *p) +{ + kho_scratch_size = memparse(p, &p); + return 0; +} +early_param("kho_scratch", early_kho_scratch); + +/** + * kho_reserve - Reserve a contiguous chunk of memory for kexec + * + * With KHO we can preserve arbitrary pages in the system. To ensure we still + * have a large contiguous region of memory when we search the physical address + * space for target memory, let's make sure we always have a large CMA region + * active. This CMA region will only be used for movable pages which are not a + * problem for us during KHO because we can just move them somewhere else. + */ +__init void kho_reserve(void) +{ + int r; + + if (kho_get_fdt()) { + /* + * We came from a previous KHO handover, so we already have + * a known good scratch region that we preserve. No need to + * allocate another. + */ + return; + } + + /* Only allocate KHO scratch memory when we're asked to */ + if (!kho_scratch_size) + return; + + r = cma_declare_contiguous_nid(0, kho_scratch_size, 0, PAGE_SIZE, 0, + false, "kho", &kho.cma, NUMA_NO_NODE); + if (WARN_ON(r)) + return; + + kho_scratch_phys = cma_get_base(kho.cma); + kho_scratch_len = cma_get_size(kho.cma); +} From patchwork Wed Dec 13 00:04:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177681 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8080915vqy; Tue, 12 Dec 2023 16:06:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IE2jX8AcYszGyIvdJcvucypRWgINwR9yPXhMTDZWSdk3EjLRaxqfo6OlMqslpwLUi+4YhNY X-Received: by 2002:a17:902:b688:b0:1d0:6cfd:d3c4 with SMTP id c8-20020a170902b68800b001d06cfdd3c4mr6343985pls.17.1702426002529; Tue, 12 Dec 2023 16:06:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426002; cv=none; d=google.com; s=arc-20160816; b=KfSOsEqBPAVy6XNdxaUvgh+tIO4KL8O55bcdTnrmN8C7tanxZzO7WkLcRTLF8xBFLD knUmIbuNYNMMdkON5FlVcgOMlCi2LA8vcG/SuDdclfF1BWsOyYLQeMeFBDNsWqC180/y Y39UuvLzPUDOrPM46bz/KOuuTC/WVEsUFl7aH8LGLt0GtArJaIFPeXpbMt57WHczb2i9 CgpFeSEfThjE/Ojtck0pU93WnSYbjgjkghYWuN+VF3kLQaEsTyRiVVvy+pqxEWCC5F1W xOnTLvZjPQxNvd59Hb86XJY4FRl3lyJh16OHY1RB4Fmp6chqfPv1Z4f0v7ZE+UhIZ4ZU 0F0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=LESPtNw/smO8CIxZLa0BQCyFgAWbYWE2+aT46q12sNA=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=fx+qyc+DYsGkuEWewy5mE05xj9pcoVbehQHbNG74zm8myfvBOeEQkB/I99mz+EHIvH 5brWHMnZOzl52LI+RBs1IzblmaIj/CWz56PP6HXIi55t7slCXBfOpWTEfEG17kenbP3i WARUtO554HzR26DIeg5bE2t6yQ1kr0WLxA6IpzN2dVh+LSZb3M6f+XZrFovT9LTMRqSa oqb7pw00WCxpj9JSyJ9aFBBj/s/Naj5vYiq4TMIBF+zEw5O36xQy4KUCJeg8IIAD4RRD F3NLwzZN6Lzya6jQ+l4lYzZ15BUhL5VGdGWanQdYYm2z7EfhUX5iSuK3bFCpi6Y2nQEb EGFQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=KL+PPGgS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id x3-20020a1709027c0300b001cfd0495291si8494844pll.524.2023.12.12.16.06.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:06:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=KL+PPGgS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 210A38088A61; Tue, 12 Dec 2023 16:06:29 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377647AbjLMAFu (ORCPT + 99 others); Tue, 12 Dec 2023 19:05:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377651AbjLMAFs (ORCPT ); Tue, 12 Dec 2023 19:05:48 -0500 Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3744E3; Tue, 12 Dec 2023 16:05:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425952; x=1733961952; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=LESPtNw/smO8CIxZLa0BQCyFgAWbYWE2+aT46q12sNA=; b=KL+PPGgSD2yOWmBwoHKC+j1wdXhXpKC7GnCAzkiEMNO4DZxqAy8qybLS gVbPWlwvHpsJzB1YJlX9R3q4dzHCdPJ7b8u+665xjUeisiGN5G063Pnrj QFPUamMVpaFX/ORhPzeU2q3N/ixEl6GHCwSGt0JIKUDmAwk3K9315QVKe M=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="171586528" Received: from iad12-co-svc-p1-lb1-vlan2.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-0aba4706.us-east-1.amazon.com) ([10.43.8.2]) by smtp-border-fw-52004.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:05:50 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan2.iad.amazon.com [10.32.235.34]) by email-inbound-relay-iad-1e-m6i4x-0aba4706.us-east-1.amazon.com (Postfix) with ESMTPS id 4F134A54AC; Wed, 13 Dec 2023 00:05:36 +0000 (UTC) Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:55378] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.43.236:2525] with esmtp (Farcaster) id ec187307-8427-47be-99c6-38fe9b03912f; Wed, 13 Dec 2023 00:05:36 +0000 (UTC) X-Farcaster-Flow-ID: ec187307-8427-47be-99c6-38fe9b03912f Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:35 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:31 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 04/15] kexec: Add KHO parsing support Date: Wed, 13 Dec 2023 00:04:41 +0000 Message-ID: <20231213000452.88295-5-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:06:29 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123048086639418 X-GMAIL-MSGID: 1785123048086639418 When we have a KHO kexec, we get a device tree, mem cache and scratch region to populate the state of the system. Provide helper functions that allow architecture code to easily handle memory reservations based on them and give device drivers visibility into the KHO DT and memory reservations so they can recover their own state. Signed-off-by: Alexander Graf --- Documentation/ABI/testing/sysfs-firmware-kho | 9 + MAINTAINERS | 1 + include/linux/kexec.h | 23 ++ kernel/Makefile | 1 + kernel/kexec_kho_in.c | 298 +++++++++++++++++++ 5 files changed, 332 insertions(+) create mode 100644 Documentation/ABI/testing/sysfs-firmware-kho create mode 100644 kernel/kexec_kho_in.c diff --git a/Documentation/ABI/testing/sysfs-firmware-kho b/Documentation/ABI/testing/sysfs-firmware-kho new file mode 100644 index 000000000000..e4ed2cb7c810 --- /dev/null +++ b/Documentation/ABI/testing/sysfs-firmware-kho @@ -0,0 +1,9 @@ +What: /sys/firmware/kho/dt +Date: December 2023 +Contact: Alexander Graf +Description: + When the kernel was booted with Kexec HandOver (KHO), + the device tree that carries metadata about the previous + kernel's state is in this file. This file may disappear + when all consumers of it finished to interpret their + metadata. diff --git a/MAINTAINERS b/MAINTAINERS index 4ebf7c5fd424..ec92a0dd628d 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -11769,6 +11769,7 @@ M: Eric Biederman L: kexec@lists.infradead.org S: Maintained W: http://kernel.org/pub/linux/utils/kernel/kexec/ +F: Documentation/ABI/testing/sysfs-firmware-kho F: Documentation/ABI/testing/sysfs-kernel-kho F: include/linux/kexec.h F: include/uapi/linux/kexec.h diff --git a/include/linux/kexec.h b/include/linux/kexec.h index db2597e5550d..a3c4fee6f86a 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -528,12 +528,35 @@ enum kho_event { extern phys_addr_t kho_scratch_phys; extern phys_addr_t kho_scratch_len; +/* ingest handover metadata */ +void kho_reserve_mem(void); +void kho_populate(phys_addr_t dt_phys, phys_addr_t scratch_phys, u64 scratch_len, + phys_addr_t mem_phys, u64 mem_len); +void kho_populate_refcount(void); +void *kho_get_fdt(void); +void kho_return_mem(const struct kho_mem *mem); +void *kho_claim_mem(const struct kho_mem *mem); +static inline bool is_kho_boot(void) +{ + return !!kho_scratch_phys; +} + /* egest handover metadata */ void kho_reserve(void); int register_kho_notifier(struct notifier_block *nb); int unregister_kho_notifier(struct notifier_block *nb); bool kho_is_active(void); #else +/* ingest handover metadata */ +static inline void kho_reserve_mem(void) { } +static inline bool is_kho_boot(void) { return false; } +static inline void kho_populate(phys_addr_t dt_phys, phys_addr_t scratch_phys, + u64 scratch_len, phys_addr_t mem_phys, + u64 mem_len) { } +static inline void kho_populate_refcount(void) { } +static inline void *kho_get_fdt(void) { return NULL; } + +/* egest handover metadata */ static inline void kho_reserve(void) { } static inline int register_kho_notifier(struct notifier_block *nb) { return -EINVAL; } static inline int unregister_kho_notifier(struct notifier_block *nb) { return -EINVAL; } diff --git a/kernel/Makefile b/kernel/Makefile index a6bd31e22c09..7c3065e40c75 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -73,6 +73,7 @@ obj-$(CONFIG_KEXEC_CORE) += kexec_core.o obj-$(CONFIG_KEXEC) += kexec.o obj-$(CONFIG_KEXEC_FILE) += kexec_file.o obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o +obj-$(CONFIG_KEXEC_KHO) += kexec_kho_in.o obj-$(CONFIG_KEXEC_KHO) += kexec_kho_out.o obj-$(CONFIG_BACKTRACE_SELF_TEST) += backtracetest.o obj-$(CONFIG_COMPAT) += compat.o diff --git a/kernel/kexec_kho_in.c b/kernel/kexec_kho_in.c new file mode 100644 index 000000000000..12ec54fc537a --- /dev/null +++ b/kernel/kexec_kho_in.c @@ -0,0 +1,298 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * kexec_kho_in.c - kexec handover code to ingest metadata. + * Copyright (C) 2023 Alexander Graf + */ + +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include +#include +#include +#include +#include +#include + +/* The kho dt during runtime */ +static void *fdt; + +/* Globals to hand over phys/len from early to runtime */ +static phys_addr_t handover_phys __initdata; +static u32 handover_len __initdata; + +static phys_addr_t mem_phys __initdata; +static u32 mem_len __initdata; + +phys_addr_t kho_scratch_phys; +phys_addr_t kho_scratch_len; + +void *kho_get_fdt(void) +{ + return fdt; +} +EXPORT_SYMBOL_GPL(kho_get_fdt); + +/** + * kho_populate_refcount - Scan the DT for any memory ranges. Increase the + * affected pages' refcount by 1 for each. + */ +__init void kho_populate_refcount(void) +{ + void *fdt = kho_get_fdt(); + void *mem_virt = __va(mem_phys); + int offset = 0, depth = 0, initial_depth = 0, len; + + if (!fdt) + return; + + /* Go through the mem list and add 1 for each reference */ + for (offset = 0; + offset >= 0 && depth >= initial_depth; + offset = fdt_next_node(fdt, offset, &depth)) { + const struct kho_mem *mems; + u32 i; + + mems = fdt_getprop(fdt, offset, "mem", &len); + if (!mems || len & (sizeof(*mems) - 1)) + continue; + + for (i = 0; i < len; i += sizeof(*mems)) { + const struct kho_mem *mem = ((void *)mems) + i; + u64 start_pfn = PFN_DOWN(mem->addr); + u64 end_pfn = PFN_UP(mem->addr + mem->len); + u64 pfn; + + for (pfn = start_pfn; pfn < end_pfn; pfn++) + get_page(pfn_to_page(pfn)); + } + } + + /* + * Then reduce the reference count by 1 to offset the initial ref count + * of 1. In addition, unreserve the page. That way, we can free_page() + * it for every consumer and automatically free it to the global memory + * pool when everyone is done. + */ + for (offset = 0; offset < mem_len; offset += sizeof(struct kho_mem)) { + struct kho_mem *mem = mem_virt + offset; + u64 start_pfn = PFN_DOWN(mem->addr); + u64 end_pfn = PFN_UP(mem->addr + mem->len); + u64 pfn; + + for (pfn = start_pfn; pfn < end_pfn; pfn++) { + struct page *page = pfn_to_page(pfn); + + /* + * This is similar to free_reserved_page(), but + * preserves the reference count + */ + ClearPageReserved(page); + __free_page(page); + adjust_managed_page_count(page, 1); + } + } +} + +static void kho_return_pfn(ulong pfn) +{ + struct page *page = pfn_to_page(pfn); + + if (WARN_ON(!page)) + return; + __free_page(page); +} + +/** + * kho_return_mem - Notify the kernel that initially reserved memory is no + * longer needed. When the last consumer of a page returns their mem, kho + * returns the page to the buddy allocator as free page. + */ +void kho_return_mem(const struct kho_mem *mem) +{ + uint64_t start_pfn, end_pfn, pfn; + + start_pfn = PFN_DOWN(mem->addr); + end_pfn = PFN_UP(mem->addr + mem->len); + + for (pfn = start_pfn; pfn < end_pfn; pfn++) + kho_return_pfn(pfn); +} +EXPORT_SYMBOL_GPL(kho_return_mem); + +static void kho_claim_pfn(ulong pfn) +{ + struct page *page = pfn_to_page(pfn); + + WARN_ON(!page); + if (WARN_ON(page_count(page) != 1)) + pr_err("Claimed non kho pfn %lx", pfn); +} + +/** + * kho_claim_mem - Notify the kernel that a handed over memory range is now in + * use by a kernel subsystem and considered an allocated page. This function + * removes the reserved state for all pages that the mem spans. + */ +void *kho_claim_mem(const struct kho_mem *mem) +{ + u64 start_pfn, end_pfn, pfn; + void *va = __va(mem->addr); + + start_pfn = PFN_DOWN(mem->addr); + end_pfn = PFN_UP(mem->addr + mem->len); + + for (pfn = start_pfn; pfn < end_pfn; pfn++) + kho_claim_pfn(pfn); + + return va; +} +EXPORT_SYMBOL_GPL(kho_claim_mem); + +/** + * kho_reserve_mem - Adds all memory reservations into memblocks + * and moves us out of the scratch only phase. Must be called after page tables + * are initialized and memblock_allow_resize(). + */ +void __init kho_reserve_mem(void) +{ + void *mem_virt = __va(mem_phys); + int off, err; + + if (!handover_phys || !mem_phys) + return; + + /* + * We reached here because we are running inside a working linear map + * that allows us to resize memblocks dynamically. Use the chance and + * populate the global fdt pointer + */ + fdt = __va(handover_phys); + + off = fdt_path_offset(fdt, "/"); + if (off < 0) { + fdt = NULL; + return; + } + + err = fdt_node_check_compatible(fdt, off, "kho-v1"); + if (err) { + pr_warn("KHO has invalid compatible, disabling."); + return; + } + + /* Then populate all preserved memory areas as reserved */ + for (off = 0; off < mem_len; off += sizeof(struct kho_mem)) { + struct kho_mem *mem = mem_virt + off; + + memblock_reserve(mem->addr, mem->len); + } + + /* Unreserve the mem cache - we don't need it from here on */ + memblock_phys_free(mem_phys, mem_len); + + /* + * Now we know about all memory reservations, release the scratch only + * constraint and allow normal allocations from the scratch region. + */ + memblock_clear_scratch_only(); +} + +/* Handling for /sys/firmware/kho */ +static struct kobject *kho_kobj; + +static ssize_t raw_read(struct file *file, struct kobject *kobj, + struct bin_attribute *attr, char *buf, + loff_t pos, size_t count) +{ + memcpy(buf, attr->private + pos, count); + return count; +} + +static BIN_ATTR(dt, 0400, raw_read, NULL, 0); + +static __init int kho_in_init(void) +{ + int ret = 0; + + if (!fdt) + return 0; + + kho_kobj = kobject_create_and_add("kho", firmware_kobj); + if (!kho_kobj) { + ret = -ENOMEM; + goto err; + } + + bin_attr_dt.size = fdt_totalsize(fdt); + bin_attr_dt.private = fdt; + ret = sysfs_create_bin_file(kho_kobj, &bin_attr_dt); + if (ret) + goto err; + +err: + return ret; +} +subsys_initcall(kho_in_init); + +void __init kho_populate(phys_addr_t handover_dt_phys, phys_addr_t scratch_phys, + u64 scratch_len, phys_addr_t mem_cache_phys, + u64 mem_cache_len) +{ + void *handover_dt; + + /* Determine the real size of the DT */ + handover_dt = early_memremap(handover_dt_phys, sizeof(struct fdt_header)); + if (!handover_dt) { + pr_warn("setup: failed to memremap kexec FDT (0x%llx)\n", handover_dt_phys); + return; + } + + if (fdt_check_header(handover_dt)) { + pr_warn("setup: kexec handover FDT is invalid (0x%llx)\n", handover_dt_phys); + early_memunmap(handover_dt, PAGE_SIZE); + return; + } + + handover_len = fdt_totalsize(handover_dt); + handover_phys = handover_dt_phys; + + /* Reserve the DT so we can still access it in late boot */ + memblock_reserve(handover_phys, handover_len); + + /* Reserve the mem cache so we can still access it later */ + memblock_reserve(mem_cache_phys, mem_cache_len); + + /* + * We pass a safe contiguous block of memory to use for early boot purporses from + * the previous kernel so that we can resize the memblock array as needed. + */ + memblock_add(scratch_phys, scratch_len); + + if (WARN_ON(memblock_mark_scratch(scratch_phys, scratch_len))) { + pr_err("Kexec failed to mark the scratch region. Disabling KHO."); + handover_len = 0; + handover_phys = 0; + return; + } + pr_debug("Marked 0x%lx+0x%lx as scratch", (long)scratch_phys, (long)scratch_len); + + /* + * Now that we have a viable region of scratch memory, let's tell the memblocks + * allocator to only use that for any allocations. That way we ensure that nothing + * scribbles over in use data while we initialize the page tables which we will need + * to ingest all memory reservations from the previous kernel. + */ + memblock_set_scratch_only(); + + early_memunmap(handover_dt, sizeof(struct fdt_header)); + + /* Remember the mem cache location for kho_reserve_mem() */ + mem_len = mem_cache_len; + mem_phys = mem_cache_phys; + + /* Remember the scratch block - we will reuse it again for the next kexec */ + kho_scratch_phys = scratch_phys; + kho_scratch_len = scratch_len; + + pr_info("setup: Found kexec handover data. Will skip init for some devices\n"); +} From patchwork Wed Dec 13 00:04:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8080947vqy; Tue, 12 Dec 2023 16:06:47 -0800 (PST) X-Google-Smtp-Source: AGHT+IGiNpgrh2avfzpbRO550r8UDvVmVMBJ3Q9Y1lBwe5nPnsvW3s3fy2GhV0hAKaWpuCqoGwmj X-Received: by 2002:a05:6a00:1491:b0:6ce:2731:79f4 with SMTP id v17-20020a056a00149100b006ce273179f4mr3673256pfu.42.1702426007045; Tue, 12 Dec 2023 16:06:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426006; cv=none; d=google.com; s=arc-20160816; b=RGeUIH0en8BxrJOqtKszR4iBAKZmbbxzgDCY2PmlbKnMlrDlkzKqBx0+2gdBiH7y3d v6aD1k6U2MeqWAd3srgjU8kQUoKlIubm2ikdn+Nqpob/4KkTQwXP3hiye7a3WgzZMhQN 7aXVAFcpiVmIYOWOna6wIHQtV9v1oQuDNzA2pXkX553Do8MSp4f4Ow6G5ttNzsQ+nUJz WMilDdqDGQT8hMh/v42UeGDGaaC8xbOjuWK/+TC9jVoThHQKbvFm6Nl8W6BUhXZ2OIHJ irrbJLrwK209hqWzkfD3OEF5oF9m4SflxND5DqOAgiToeBWlFdCi1Jd0t+GwK61EnLqb OhJw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZFw7vHojuqK3RNa1/vBeZMhNA6TKCzlzgnpxEyXCDLU=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=i64j5G7hUg3YyIcZyAFM76FshmtpP9EvxJWggrhFJv5SSiiUK0Yap694quxbfzkjD5 lzX5L3TFq5WUqmmrW2h/+xRWM6bssAR23yC9x7eo+tcR9mEg4cbJ7tVNwmNj0v2woipU 93wOjhe38wLqZL7FU9ptoq8jCF57qpDpPH6ROw0Kcbwe/RecPAG8AEU0JFwvc1PMwqVj G/vvSMRsj0eoO0HoKj7o7xuLuNXuF1BnzAJWOWhu2O+LL8se5G5DG7hd1K3CCJGeEkXt ZT7bUW6r27/ORNO8wwxdPXNLrKd4brjZHmLFTwK/o1KUUdCS9lILAzrleIBPB5FluImW WoyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=YqBL1OxD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id k25-20020a6568d9000000b005c207717411si8514026pgt.864.2023.12.12.16.06.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:06:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=YqBL1OxD; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id EF0E58057E77; Tue, 12 Dec 2023 16:06:40 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377710AbjLMAGA (ORCPT + 99 others); Tue, 12 Dec 2023 19:06:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377660AbjLMAFw (ORCPT ); Tue, 12 Dec 2023 19:05:52 -0500 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62197CD; Tue, 12 Dec 2023 16:05:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425958; x=1733961958; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZFw7vHojuqK3RNa1/vBeZMhNA6TKCzlzgnpxEyXCDLU=; b=YqBL1OxDV0isTajVEickbGAbE58Ci+k94E84mjF4/iXo/w6uxnFgJRmm g1AWyaSw05SSr8P7TG+EA0oxw7vaAho8Gl/3TFHo9zbmhAB4sJHC8tiIM 7H4/d9zcxP1eBWQvTnE40raXq6UBL4D1jE7ofLkM4jpbBEpGpZi9bviiC s=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="316892987" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-83883bdb.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:05:49 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2a-m6i4x-83883bdb.us-west-2.amazon.com (Postfix) with ESMTPS id C557460A68; Wed, 13 Dec 2023 00:05:39 +0000 (UTC) Received: from EX19MTAUWB001.ant.amazon.com [10.0.21.151:63725] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.11.243:2525] with esmtp (Farcaster) id df4b2a7f-6229-4967-8f23-449065835394; Wed, 13 Dec 2023 00:05:39 +0000 (UTC) X-Farcaster-Flow-ID: df4b2a7f-6229-4967-8f23-449065835394 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:39 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:05:35 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 05/15] kexec: Add KHO support to kexec file loads Date: Wed, 13 Dec 2023 00:04:42 +0000 Message-ID: <20231213000452.88295-6-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:06:41 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123051720285643 X-GMAIL-MSGID: 1785123051720285643 Kexec has 2 modes: A user space driven mode and a kernel driven mode. For the kernel driven mode, kernel code determines the physical addresses of all target buffers that the payload gets copied into. With KHO, we can only safely copy payloads into the "scratch area". Teach the kexec file loader about it, so it only allocates for that area. In addition, enlighten it with support to ask the KHO subsystem for its respective payloads to copy into target memory. Also teach the KHO subsystem how to fill the images for file loads. Signed-off-by: Alexander Graf --- include/linux/kexec.h | 9 ++ kernel/kexec_file.c | 41 ++++++++ kernel/kexec_kho_out.c | 210 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 260 insertions(+) diff --git a/include/linux/kexec.h b/include/linux/kexec.h index a3c4fee6f86a..c8859a2ca872 100644 --- a/include/linux/kexec.h +++ b/include/linux/kexec.h @@ -362,6 +362,13 @@ struct kimage { size_t ima_buffer_size; #endif +#ifdef CONFIG_KEXEC_KHO + struct { + struct kexec_buf dt; + struct kexec_buf mem_cache; + } kho; +#endif + /* Core ELF header buffer */ void *elf_headers; unsigned long elf_headers_sz; @@ -543,6 +550,7 @@ static inline bool is_kho_boot(void) /* egest handover metadata */ void kho_reserve(void); +int kho_fill_kimage(struct kimage *image); int register_kho_notifier(struct notifier_block *nb); int unregister_kho_notifier(struct notifier_block *nb); bool kho_is_active(void); @@ -558,6 +566,7 @@ static inline void *kho_get_fdt(void) { return NULL; } /* egest handover metadata */ static inline void kho_reserve(void) { } +static inline int kho_fill_kimage(struct kimage *image) { return 0; } static inline int register_kho_notifier(struct notifier_block *nb) { return -EINVAL; } static inline int unregister_kho_notifier(struct notifier_block *nb) { return -EINVAL; } static inline bool kho_is_active(void) { return false; } diff --git a/kernel/kexec_file.c b/kernel/kexec_file.c index f9a419cd22d4..d895d0a49bd9 100644 --- a/kernel/kexec_file.c +++ b/kernel/kexec_file.c @@ -113,6 +113,13 @@ void kimage_file_post_load_cleanup(struct kimage *image) image->ima_buffer = NULL; #endif /* CONFIG_IMA_KEXEC */ +#ifdef CONFIG_KEXEC_KHO + kvfree(image->kho.mem_cache.buffer); + image->kho.mem_cache = (struct kexec_buf) {}; + kvfree(image->kho.dt.buffer); + image->kho.dt = (struct kexec_buf) {}; +#endif + /* See if architecture has anything to cleanup post load */ arch_kimage_file_post_load_cleanup(image); @@ -249,6 +256,11 @@ kimage_file_prepare_segments(struct kimage *image, int kernel_fd, int initrd_fd, /* IMA needs to pass the measurement list to the next kernel. */ ima_add_kexec_buffer(image); + /* If KHO is active, add its images to the list */ + ret = kho_fill_kimage(image); + if (ret) + goto out; + /* Call image load handler */ ldata = kexec_image_load_default(image); @@ -518,6 +530,24 @@ static int locate_mem_hole_callback(struct resource *res, void *arg) return locate_mem_hole_bottom_up(start, end, kbuf); } +#ifdef CONFIG_KEXEC_KHO +static int kexec_walk_kho_scratch(struct kexec_buf *kbuf, + int (*func)(struct resource *, void *)) +{ + int ret = 0; + + struct resource res = { + .start = kho_scratch_phys, + .end = kho_scratch_phys + kho_scratch_len, + }; + + /* Try to fit the kimage into our KHO scratch region */ + ret = func(&res, kbuf); + + return ret; +} +#endif + #ifdef CONFIG_ARCH_KEEP_MEMBLOCK static int kexec_walk_memblock(struct kexec_buf *kbuf, int (*func)(struct resource *, void *)) @@ -612,6 +642,17 @@ int kexec_locate_mem_hole(struct kexec_buf *kbuf) if (kbuf->mem != KEXEC_BUF_MEM_UNKNOWN) return 0; +#ifdef CONFIG_KEXEC_KHO + /* + * If KHO is active, only use KHO scratch memory. All other memory + * could potentially be handed over. + */ + if (kho_is_active() && kbuf->image->type != KEXEC_TYPE_CRASH) { + ret = kexec_walk_kho_scratch(kbuf, locate_mem_hole_callback); + return ret == 1 ? 0 : -EADDRNOTAVAIL; + } +#endif + if (!IS_ENABLED(CONFIG_ARCH_KEEP_MEMBLOCK)) ret = kexec_walk_resources(kbuf, locate_mem_hole_callback); else diff --git a/kernel/kexec_kho_out.c b/kernel/kexec_kho_out.c index e6184bde5c10..24ced6c3013f 100644 --- a/kernel/kexec_kho_out.c +++ b/kernel/kexec_kho_out.c @@ -50,6 +50,216 @@ int unregister_kho_notifier(struct notifier_block *nb) } EXPORT_SYMBOL_GPL(unregister_kho_notifier); +static int kho_mem_cache_add(void *fdt, struct kho_mem *mem_cache, int size, + struct kho_mem *new_mem) +{ + int entries = size / sizeof(*mem_cache); + u64 new_start = new_mem->addr; + u64 new_end = new_mem->addr + new_mem->len; + u64 prev_start = 0; + u64 prev_end = 0; + int i; + + if (WARN_ON((new_start < (kho_scratch_phys + kho_scratch_len)) && + (new_end > kho_scratch_phys))) { + pr_err("KHO memory runs over scratch memory"); + return -EINVAL; + } + + /* + * We walk the existing sorted mem cache and find the spot where this + * new entry would start, so we can insert it right there. + */ + for (i = 0; i < entries; i++) { + struct kho_mem *mem = &mem_cache[i]; + u64 mem_end = (mem->addr + mem->len); + + if (mem_end < new_start) { + /* No overlap */ + prev_start = mem->addr; + prev_end = mem->addr + mem->len; + continue; + } else if ((new_start >= mem->addr) && (new_end <= mem_end)) { + /* new_mem fits into mem, skip */ + return size; + } else if ((new_end >= mem->addr) && (new_start <= mem_end)) { + /* new_mem and mem overlap, fold them */ + bool remove = false; + + mem->addr = min(new_start, mem->addr); + mem->len = max(mem_end, new_end) - mem->addr; + mem_end = (mem->addr + mem->len); + + if (i > 0 && prev_end >= mem->addr) { + /* We now overlap with the previous mem, fold */ + struct kho_mem *prev = &mem_cache[i - 1]; + + prev->addr = min(prev->addr, mem->addr); + prev->len = max(mem_end, prev_end) - prev->addr; + remove = true; + } else if (i < (entries - 1) && mem_end >= mem_cache[i + 1].addr) { + /* We now overlap with the next mem, fold */ + struct kho_mem *next = &mem_cache[i + 1]; + u64 next_end = (next->addr + next->len); + + next->addr = min(next->addr, mem->addr); + next->len = max(mem_end, next_end) - next->addr; + remove = true; + } + + if (remove) { + /* We folded this mem into another, remove it */ + memmove(mem, mem + 1, (entries - i - 1) * sizeof(*mem)); + size -= sizeof(*new_mem); + } + + return size; + } else if (mem->addr > new_end) { + /* + * The mem cache is sorted. If we find the current + * entry start after our new_mem's end, we shot over + * which means we need to add it by creating a new + * hole right after the current entry. + */ + memmove(mem + 1, mem, (entries - i) * sizeof(*mem)); + break; + } + } + + mem_cache[i] = *new_mem; + size += sizeof(*new_mem); + + return size; +} + +/** + * kho_alloc_mem_cache - Allocate and initialize the mem cache kexec_buf + */ +static int kho_alloc_mem_cache(struct kimage *image, void *fdt) +{ + int offset, depth, initial_depth, len; + void *mem_cache; + int size; + + /* Count the elements inside all "mem" properties in the DT */ + size = offset = depth = initial_depth = 0; + for (offset = 0; + offset >= 0 && depth >= initial_depth; + offset = fdt_next_node(fdt, offset, &depth)) { + const struct kho_mem *mems; + + mems = fdt_getprop(fdt, offset, "mem", &len); + if (!mems || len & (sizeof(*mems) - 1)) + continue; + size += len; + } + + /* Allocate based on the max size we determined */ + mem_cache = kvmalloc(size, GFP_KERNEL); + if (!mem_cache) + return -ENOMEM; + + /* And populate the array */ + size = offset = depth = initial_depth = 0; + for (offset = 0; + offset >= 0 && depth >= initial_depth; + offset = fdt_next_node(fdt, offset, &depth)) { + const struct kho_mem *mems; + int nr_mems, i; + + mems = fdt_getprop(fdt, offset, "mem", &len); + if (!mems || len & (sizeof(*mems) - 1)) + continue; + + for (i = 0, nr_mems = len / sizeof(*mems); i < nr_mems; i++) { + const struct kho_mem *mem = &mems[i]; + ulong mstart = PAGE_ALIGN_DOWN(mem->addr); + ulong mend = PAGE_ALIGN(mem->addr + mem->len); + struct kho_mem cmem = { + .addr = mstart, + .len = (mend - mstart), + }; + + size = kho_mem_cache_add(fdt, mem_cache, size, &cmem); + if (size < 0) + return size; + } + } + + image->kho.mem_cache.buffer = mem_cache; + image->kho.mem_cache.bufsz = size; + image->kho.mem_cache.memsz = size; + + return 0; +} + +int kho_fill_kimage(struct kimage *image) +{ + int err = 0; + void *dt; + + mutex_lock(&kho.lock); + + if (!kho.active) + goto out; + + /* Initialize kexec_buf for mem_cache */ + image->kho.mem_cache = (struct kexec_buf) { + .image = image, + .buffer = NULL, + .bufsz = 0, + .mem = KEXEC_BUF_MEM_UNKNOWN, + .memsz = 0, + .buf_align = SZ_64K, /* Makes it easier to map */ + .buf_max = ULONG_MAX, + .top_down = true, + }; + + /* + * We need to make all allocations visible here via the mem_cache so that + * kho_is_destination_range() can identify overlapping regions and ensure + * that no kimage (including the DT one) lands on handed over memory. + * + * Since we conveniently already built an array of all allocations, let's + * pass that on to the target kernel so that reuse it to initialize its + * memory blocks. + */ + err = kho_alloc_mem_cache(image, kho.dt); + if (err) + goto out; + + err = kexec_add_buffer(&image->kho.mem_cache); + if (err) + goto out; + + /* + * Create a kexec copy of the DT here. We need this because lifetime may + * be different between kho.dt and the kimage + */ + dt = kvmemdup(kho.dt, kho.dt_len, GFP_KERNEL); + if (!dt) { + err = -ENOMEM; + goto out; + } + + /* Allocate target memory for kho dt */ + image->kho.dt = (struct kexec_buf) { + .image = image, + .buffer = dt, + .bufsz = kho.dt_len, + .mem = KEXEC_BUF_MEM_UNKNOWN, + .memsz = kho.dt_len, + .buf_align = SZ_64K, /* Makes it easier to map */ + .buf_max = ULONG_MAX, + .top_down = true, + }; + err = kexec_add_buffer(&image->kho.dt); + +out: + mutex_unlock(&kho.lock); + return err; +} + bool kho_is_active(void) { return kho.active; From patchwork Wed Dec 13 00:04:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177685 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081200vqy; Tue, 12 Dec 2023 16:07:20 -0800 (PST) X-Google-Smtp-Source: AGHT+IHbOdatsxLAFol5X5ylotMWc3gTyvIzcnT5W81itVeNDvOtAk9C96fZBysn/yjYiVUS6sa3 X-Received: by 2002:a05:6a20:5d2:b0:190:8d36:550c with SMTP id r18-20020a056a2005d200b001908d36550cmr5553420pzr.53.1702426038809; Tue, 12 Dec 2023 16:07:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426038; cv=none; d=google.com; s=arc-20160816; b=k2Ym9OW5SYoiD7xoGyL1/oKyJMnabqHty8KtAxAku8ZY6Ip4clvJ9pc60ATtbXO0XO vW2kgB/gCMabcMfN7ermam1RhwDCHNSxlzHPpG5pdV1fUvKNQLo1juhJ0AIY3eIeMLCb qHywsa+7NkkSgU1QpRnXR3SAUjTASjre+lbSDQifzABWH2bw/m/wKM3Qo9pzE+Ld30Y8 JdM2BSn3seaIpyvMD+hjdSDbwcA8gLJXjhRht0TTNp2TMPN9ASWerilD3jv8PsLCSie+ LrgEsZmK8PRBLqiArSl5mgwar7kDtRUXJeTVeij/2H44Ge9V/5ZArAJ5Z2MxzjxmN9rf Za0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=2/g7t8dqwJHCv4j3q+tOmI1oayO/LeEOR2vQQ38tfk8=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=nVF8aFsyVybWLZHSBjATsz+QncM++QWiB0+0PAyea/+6SgUywpNGopAQHMC2AZVE9u PyyI1TqDLzfFwoEAFNnGhEulau03/vndK/slZvzU1VhcDr5zPYSVlAf1RhtsPq/4MmDp U31jQVbJpEufmONn+dfUZSgCjmSwSX/dvSpfstyUyGxWwv7E+oIcmJUw2URoxoJpUaui CEqs1p1KWEdAVqQIYLJGELvCjabukewEgC6xXQ+MDybGLsNXVb5Ql8FBN4zwky3Qszh0 CW/GJNBreXkTYuL/lM0CNwCBjSsJ8LSv0GTngTyjufhbxIdt0N/IcYGTT4pfgWjSxIvj P2Ug== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=fUfjJqzt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id o16-20020a056a00215000b006ce4207c516si8435225pfk.388.2023.12.12.16.07.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:07:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=fUfjJqzt; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 3E532801B67B; Tue, 12 Dec 2023 16:07:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377706AbjLMAGb (ORCPT + 99 others); Tue, 12 Dec 2023 19:06:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56920 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377645AbjLMAG1 (ORCPT ); Tue, 12 Dec 2023 19:06:27 -0500 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 78AA9121; Tue, 12 Dec 2023 16:06:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425981; x=1733961981; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=2/g7t8dqwJHCv4j3q+tOmI1oayO/LeEOR2vQQ38tfk8=; b=fUfjJqztd2F7rAGVT5H6Q5wEw4dYyHilqI5Lb1uAV/i0NT9o5dGex9WD JLBgefCoj7ZxgRx/wz+I4rfCDMssJfx6AKkfi53DRee9qo/CWcI+LsPZ3 VWhBIJiFBqyA5/wQguHcRgCo50GaafAytrsSUK/sfiyi4ZXlWbyHwQdz5 s=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="373412697" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:06:19 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1e-m6i4x-529f0975.us-east-1.amazon.com (Postfix) with ESMTPS id CCC6E49815; Wed, 13 Dec 2023 00:06:05 +0000 (UTC) Received: from EX19MTAUWA001.ant.amazon.com [10.0.21.151:23290] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.43.236:2525] with esmtp (Farcaster) id 5b333ece-94a3-4a4f-9c0a-106bd493cbae; Wed, 13 Dec 2023 00:06:04 +0000 (UTC) X-Farcaster-Flow-ID: 5b333ece-94a3-4a4f-9c0a-106bd493cbae Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWA001.ant.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:04 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:00 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 06/15] arm64: Add KHO support Date: Wed, 13 Dec 2023 00:04:43 +0000 Message-ID: <20231213000452.88295-7-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D042UWA003.ant.amazon.com (10.13.139.44) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:07:07 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123086297844357 X-GMAIL-MSGID: 1785123086297844357 We now have all bits in place to support KHO kexecs. This patch adds awareness of KHO in the kexec file as well as boot path for arm64 and adds the respective kconfig option to the architecture so that it can use KHO successfully. Signed-off-by: Alexander Graf --- arch/arm64/Kconfig | 12 ++++++++++++ arch/arm64/kernel/setup.c | 2 ++ arch/arm64/mm/init.c | 8 ++++++++ drivers/of/fdt.c | 41 +++++++++++++++++++++++++++++++++++++++ drivers/of/kexec.c | 36 ++++++++++++++++++++++++++++++++++ 5 files changed, 99 insertions(+) diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index 7b071a00425d..1ba338ce7598 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -1501,6 +1501,18 @@ config ARCH_SUPPORTS_CRASH_DUMP config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION def_bool CRASH_CORE +config KEXEC_KHO + bool "kexec handover" + depends on KEXEC + select MEMBLOCK_SCRATCH + select LIBFDT + select CMA + help + Allow kexec to hand over state across kernels by generating and + passing additional metadata to the target kernel. This is useful + to keep data or state alive across the kexec. For this to work, + both source and target kernels need to have this option enabled. + config TRANS_TABLE def_bool y depends on HIBERNATION || KEXEC_CORE diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c index 417a8a86b2db..8035b673d96d 100644 --- a/arch/arm64/kernel/setup.c +++ b/arch/arm64/kernel/setup.c @@ -346,6 +346,8 @@ void __init __no_sanitize_address setup_arch(char **cmdline_p) paging_init(); + kho_reserve_mem(); + acpi_table_upgrade(); /* Parse the ACPI tables for possible boot-time configuration */ diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index 74c1db8ce271..254d82f3383a 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -358,6 +358,8 @@ void __init bootmem_init(void) */ arch_reserve_crashkernel(); + kho_reserve(); + memblock_dump_all(); } @@ -386,6 +388,12 @@ void __init mem_init(void) /* this will put all unused low memory onto the freelists */ memblock_free_all(); + /* + * Now that all KHO pages are marked as reserved, let's flip them back + * to normal pages with accurate refcount. + */ + kho_populate_refcount(); + /* * Check boundaries twice: Some fundamental inconsistencies can be * detected at build time already. diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index bf502ba8da95..af95139351ed 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -1006,6 +1006,44 @@ void __init early_init_dt_check_for_usable_mem_range(void) memblock_add(rgn[i].base, rgn[i].size); } +/** + * early_init_dt_check_kho - Decode info required for kexec handover from DT + */ +void __init early_init_dt_check_kho(void) +{ +#ifdef CONFIG_KEXEC_KHO + unsigned long node = chosen_node_offset; + u64 kho_start, scratch_start, scratch_size, mem_start, mem_size; + const __be32 *p; + int l; + + if ((long)node < 0) + return; + + p = of_get_flat_dt_prop(node, "linux,kho-dt", &l); + if (l != (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32)) + return; + + kho_start = dt_mem_next_cell(dt_root_addr_cells, &p); + + p = of_get_flat_dt_prop(node, "linux,kho-scratch", &l); + if (l != (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32)) + return; + + scratch_start = dt_mem_next_cell(dt_root_addr_cells, &p); + scratch_size = dt_mem_next_cell(dt_root_addr_cells, &p); + + p = of_get_flat_dt_prop(node, "linux,kho-mem", &l); + if (l != (dt_root_addr_cells + dt_root_size_cells) * sizeof(__be32)) + return; + + mem_start = dt_mem_next_cell(dt_root_addr_cells, &p); + mem_size = dt_mem_next_cell(dt_root_addr_cells, &p); + + kho_populate(kho_start, scratch_start, scratch_size, mem_start, mem_size); +#endif +} + #ifdef CONFIG_SERIAL_EARLYCON int __init early_init_dt_scan_chosen_stdout(void) @@ -1304,6 +1342,9 @@ void __init early_init_dt_scan_nodes(void) /* Handle linux,usable-memory-range property */ early_init_dt_check_for_usable_mem_range(); + + /* Handle kexec handover */ + early_init_dt_check_kho(); } bool __init early_init_dt_scan(void *params) diff --git a/drivers/of/kexec.c b/drivers/of/kexec.c index 68278340cecf..a612e6bb8c75 100644 --- a/drivers/of/kexec.c +++ b/drivers/of/kexec.c @@ -264,6 +264,37 @@ static inline int setup_ima_buffer(const struct kimage *image, void *fdt, } #endif /* CONFIG_IMA_KEXEC */ +static int kho_add_chosen(const struct kimage *image, void *fdt, int chosen_node) +{ + int ret = 0; + +#ifdef CONFIG_KEXEC_KHO + if (!image->kho.dt.buffer || !image->kho.mem_cache.buffer) + goto out; + + pr_debug("Adding kho metadata to DT"); + + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-dt", + image->kho.dt.mem, image->kho.dt.memsz); + if (ret) + goto out; + + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-scratch", + kho_scratch_phys, kho_scratch_len); + if (ret) + goto out; + + ret = fdt_appendprop_addrrange(fdt, 0, chosen_node, "linux,kho-mem", + image->kho.mem_cache.mem, + image->kho.mem_cache.bufsz); + if (ret) + goto out; + +out: +#endif + return ret; +} + /* * of_kexec_alloc_and_setup_fdt - Alloc and setup a new Flattened Device Tree * @@ -412,6 +443,11 @@ void *of_kexec_alloc_and_setup_fdt(const struct kimage *image, } } + /* Add kho metadata if this is a KHO image */ + ret = kho_add_chosen(image, fdt, chosen_node); + if (ret) + goto out; + /* add bootargs */ if (cmdline) { ret = fdt_setprop_string(fdt, chosen_node, "bootargs", cmdline); From patchwork Wed Dec 13 00:04:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177683 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081118vqy; Tue, 12 Dec 2023 16:07:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDvhWbT/WlUnH5TbzrywLMdd9XdcNBdxo6kgb/ogx/aueOEQBYxgNF8Fv1/givRSpt6OmV X-Received: by 2002:a05:6a20:9711:b0:18f:97c:825b with SMTP id hr17-20020a056a20971100b0018f097c825bmr3840927pzc.101.1702426026962; Tue, 12 Dec 2023 16:07:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426026; cv=none; d=google.com; s=arc-20160816; b=bdCvV9Ba8eDYbbMd67p5F1cByW100z0y429gbJTvDuplaND4AVRSBGvG+JN8uKdVzQ ixBBOWc4NeJmANP4P9owq65rmR6Y76kg9LqJx60korI+gFkyz0UtaaG2NMhyECYp1kmW cTTHbxoECS2slXrlRjfFkI3gF6tNFrdpvt5M8fSjB1HzsaTIhp8/aT0z3eGTRAyopQM3 nPLaHB8EJzpfC1FTfjIKMy9FvP1jrTd6iOHxysdrGKGF0ODkrqHPRySiSt/9aDzkIvya f1eTBhlyqpbRB9z5wS2Sy9oCd31iGp9hCiYO0SzCHFSmIExkOQMbWOngKmVpoIz7IL0N tXQQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Jz8kgyam6i59G9GfakNoGFINiG08t4Jr479SrWRohZo=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=i+AuMbP2r/U94FaKgLlcWIc8dGvsEAsul6AcawHg+JPejnRfBGxJ9o9bUZNIOzaoJm tAxaf5tFbiXcoqT5EonjzXy2BhbpTjfSxULzFAL90xV+f9bkX9P0t/RHb22HelYZu1F6 Qf4Kcsgtf9L5Gc8Jrf56y+stXgrfBXwvHDCbwQx3qCvrqPen+zCXm/C3lq6TwW4sMa1c WkE/CEnjqaoLQGWuSXkZZHkwnA6+yaU4eIjHIB2dUPvqyuP7D/F2aLKoIa2deccLJtJn 63uVqG5fgI6JqvEytjcV/nkcBBvZHNX6SOViZj+MN7rRICa913vdBVpW4y+60MJrWb97 A26g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rXMc1hXI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id mp5-20020a17090b190500b00286bba72e5fsi396150pjb.16.2023.12.12.16.07.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:07:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=rXMc1hXI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 15D318080ED3; Tue, 12 Dec 2023 16:07:03 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377707AbjLMAGm (ORCPT + 99 others); Tue, 12 Dec 2023 19:06:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377732AbjLMAGh (ORCPT ); Tue, 12 Dec 2023 19:06:37 -0500 Received: from smtp-fw-9105.amazon.com (smtp-fw-9105.amazon.com [207.171.188.204]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9EEF1132; Tue, 12 Dec 2023 16:06:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425992; x=1733961992; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Jz8kgyam6i59G9GfakNoGFINiG08t4Jr479SrWRohZo=; b=rXMc1hXIKd5oub+KFNLu8RGE+1zQvW59/i+2bNVEIwmnrTHgJNWzC27L 21LBt3FkW293/mZTOx0eAhle2gcngrshSXtZhnZx0WjD5mPDZottBe5Oh MbI9XznreKvrNT5hRzs1tqVJkE/kLrWH0b0BdU4HfuQAuWwzM6UPvdG/N Q=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="690702147" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1e-m6i4x-a65ebc6e.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-9105.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:06:24 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1e-m6i4x-a65ebc6e.us-east-1.amazon.com (Postfix) with ESMTPS id EC6BE67031; Wed, 13 Dec 2023 00:06:09 +0000 (UTC) Received: from EX19MTAUWC001.ant.amazon.com [10.0.38.20:9205] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.18.147:2525] with esmtp (Farcaster) id 3f50b193-e344-4aae-8069-116bad994162; Wed, 13 Dec 2023 00:06:08 +0000 (UTC) X-Farcaster-Flow-ID: 3f50b193-e344-4aae-8069-116bad994162 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:08 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:04 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 07/15] x86: Add KHO support Date: Wed, 13 Dec 2023 00:04:44 +0000 Message-ID: <20231213000452.88295-8-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D042UWA003.ant.amazon.com (10.13.139.44) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:07:03 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123073431836114 X-GMAIL-MSGID: 1785123073431836114 We now have all bits in place to support KHO kexecs. This patch adds awareness of KHO in the kexec file as well as boot path for x86 and adds the respective kconfig option to the architecture so that it can use KHO successfully. In addition, it enlightens it decompression code with KHO so that its KASLR location finder only considers memory regions that are not already occupied by KHO memory. Signed-off-by: Alexander Graf --- arch/x86/Kconfig | 12 ++++++ arch/x86/boot/compressed/kaslr.c | 55 +++++++++++++++++++++++++++ arch/x86/include/uapi/asm/bootparam.h | 15 +++++++- arch/x86/kernel/e820.c | 9 +++++ arch/x86/kernel/kexec-bzimage64.c | 39 +++++++++++++++++++ arch/x86/kernel/setup.c | 46 ++++++++++++++++++++++ arch/x86/mm/init_32.c | 7 ++++ arch/x86/mm/init_64.c | 7 ++++ 8 files changed, 189 insertions(+), 1 deletion(-) diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 3762f41bb092..849e6ddc5d94 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2103,6 +2103,18 @@ config ARCH_SUPPORTS_CRASH_HOTPLUG config ARCH_HAS_GENERIC_CRASHKERNEL_RESERVATION def_bool CRASH_CORE +config KEXEC_KHO + bool "kexec handover" + depends on KEXEC + select MEMBLOCK_SCRATCH + select LIBFDT + select CMA + help + Allow kexec to hand over state across kernels by generating and + passing additional metadata to the target kernel. This is useful + to keep data or state alive across the kexec. For this to work, + both source and target kernels need to have this option enabled. + config PHYSICAL_START hex "Physical address where the kernel is loaded" if (EXPERT || CRASH_DUMP) default "0x1000000" diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c index dec961c6d16a..93ea292e4c18 100644 --- a/arch/x86/boot/compressed/kaslr.c +++ b/arch/x86/boot/compressed/kaslr.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -472,6 +473,60 @@ static bool mem_avoid_overlap(struct mem_vector *img, } } +#ifdef CONFIG_KEXEC_KHO + if (ptr->type == SETUP_KEXEC_KHO) { + struct kho_data *kho = (struct kho_data *)ptr->data; + struct kho_mem *mems = (void *)kho->mem_cache_addr; + int nr_mems = kho->mem_cache_size / sizeof(*mems); + int i; + + /* Avoid the mem cache */ + avoid = (struct mem_vector) { + .start = kho->mem_cache_addr, + .size = kho->mem_cache_size, + }; + + if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) { + *overlap = avoid; + earliest = overlap->start; + is_overlapping = true; + } + + /* And the KHO DT */ + avoid = (struct mem_vector) { + .start = kho->dt_addr, + .size = kho->dt_size, + }; + + if (mem_overlaps(img, &avoid) && (avoid.start < earliest)) { + *overlap = avoid; + earliest = overlap->start; + is_overlapping = true; + } + + /* As well as any other KHO memory reservations */ + for (i = 0; i < nr_mems; i++) { + avoid = (struct mem_vector) { + .start = mems[i].addr, + .size = mems[i].len, + }; + + /* + * This mem starts after our current break. + * The array is sorted, so we're done. + */ + if (avoid.start >= earliest) + break; + + if (mem_overlaps(img, &avoid)) { + *overlap = avoid; + earliest = overlap->start; + is_overlapping = true; + } + } + } +#endif + ptr = (struct setup_data *)(unsigned long)ptr->next; } diff --git a/arch/x86/include/uapi/asm/bootparam.h b/arch/x86/include/uapi/asm/bootparam.h index 01d19fc22346..013af38a9673 100644 --- a/arch/x86/include/uapi/asm/bootparam.h +++ b/arch/x86/include/uapi/asm/bootparam.h @@ -13,7 +13,8 @@ #define SETUP_CC_BLOB 7 #define SETUP_IMA 8 #define SETUP_RNG_SEED 9 -#define SETUP_ENUM_MAX SETUP_RNG_SEED +#define SETUP_KEXEC_KHO 10 +#define SETUP_ENUM_MAX SETUP_KEXEC_KHO #define SETUP_INDIRECT (1<<31) #define SETUP_TYPE_MAX (SETUP_ENUM_MAX | SETUP_INDIRECT) @@ -181,6 +182,18 @@ struct ima_setup_data { __u64 size; } __attribute__((packed)); +/* + * Locations of kexec handover metadata + */ +struct kho_data { + __u64 dt_addr; + __u64 dt_size; + __u64 scratch_addr; + __u64 scratch_size; + __u64 mem_cache_addr; + __u64 mem_cache_size; +} __attribute__((packed)); + /* The so-called "zeropage" */ struct boot_params { struct screen_info screen_info; /* 0x000 */ diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c index fb8cf953380d..c891b83f5b1c 100644 --- a/arch/x86/kernel/e820.c +++ b/arch/x86/kernel/e820.c @@ -1341,6 +1341,15 @@ void __init e820__memblock_setup(void) continue; memblock_add(entry->addr, entry->size); + + /* + * At this point with KHO we only allocate from scratch memory + * and only from memory below ISA_END_ADDRESS. Make sure that + * when we add memory for the eligible range, we add it as + * scratch memory so that we can resize the memblocks array. + */ + if (is_kho_boot() && (end <= ISA_END_ADDRESS)) + memblock_mark_scratch(entry->addr, end); } /* Throw away partial pages: */ diff --git a/arch/x86/kernel/kexec-bzimage64.c b/arch/x86/kernel/kexec-bzimage64.c index a61c12c01270..0cb8d0650a02 100644 --- a/arch/x86/kernel/kexec-bzimage64.c +++ b/arch/x86/kernel/kexec-bzimage64.c @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -233,6 +234,33 @@ setup_ima_state(const struct kimage *image, struct boot_params *params, #endif /* CONFIG_IMA_KEXEC */ } +static void setup_kho(const struct kimage *image, struct boot_params *params, + unsigned long params_load_addr, + unsigned int setup_data_offset) +{ +#ifdef CONFIG_KEXEC_KHO + struct setup_data *sd = (void *)params + setup_data_offset; + struct kho_data *kho = (void *)sd + sizeof(*sd); + + sd->type = SETUP_KEXEC_KHO; + sd->len = sizeof(struct kho_data); + + /* Only add if we have all KHO images in place */ + if (!image->kho.dt.buffer || !image->kho.mem_cache.buffer) + return; + + /* Add setup data */ + kho->dt_addr = image->kho.dt.mem; + kho->dt_size = image->kho.dt.bufsz; + kho->scratch_addr = kho_scratch_phys; + kho->scratch_size = kho_scratch_len; + kho->mem_cache_addr = image->kho.mem_cache.mem; + kho->mem_cache_size = image->kho.mem_cache.bufsz; + sd->next = params->hdr.setup_data; + params->hdr.setup_data = params_load_addr + setup_data_offset; +#endif /* CONFIG_KEXEC_KHO */ +} + static int setup_boot_parameters(struct kimage *image, struct boot_params *params, unsigned long params_load_addr, @@ -305,6 +333,13 @@ setup_boot_parameters(struct kimage *image, struct boot_params *params, sizeof(struct ima_setup_data); } + if (IS_ENABLED(CONFIG_KEXEC_KHO)) { + /* Setup space to store preservation metadata */ + setup_kho(image, params, params_load_addr, setup_data_offset); + setup_data_offset += sizeof(struct setup_data) + + sizeof(struct kho_data); + } + /* Setup RNG seed */ setup_rng_seed(params, params_load_addr, setup_data_offset); @@ -470,6 +505,10 @@ static void *bzImage64_load(struct kimage *image, char *kernel, kbuf.bufsz += sizeof(struct setup_data) + sizeof(struct ima_setup_data); + if (IS_ENABLED(CONFIG_KEXEC_KHO)) + kbuf.bufsz += sizeof(struct setup_data) + + sizeof(struct kho_data); + params = kzalloc(kbuf.bufsz, GFP_KERNEL); if (!params) return ERR_PTR(-ENOMEM); diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 1526747bedf2..196414c9c9e6 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -382,6 +382,29 @@ int __init ima_get_kexec_buffer(void **addr, size_t *size) } #endif +static void __init add_kho(u64 phys_addr, u32 data_len) +{ +#ifdef CONFIG_KEXEC_KHO + struct kho_data *kho; + u64 addr = phys_addr + sizeof(struct setup_data); + u64 size = data_len - sizeof(struct setup_data); + + kho = early_memremap(addr, size); + if (!kho) { + pr_warn("setup: failed to memremap kho data (0x%llx, 0x%llx)\n", + addr, size); + return; + } + + kho_populate(kho->dt_addr, kho->scratch_addr, kho->scratch_size, + kho->mem_cache_addr, kho->mem_cache_size); + + early_memunmap(kho, size); +#else + pr_warn("Passed KHO data, but CONFIG_KEXEC_KHO not set. Ignoring.\n"); +#endif +} + static void __init parse_setup_data(void) { struct setup_data *data; @@ -410,6 +433,9 @@ static void __init parse_setup_data(void) case SETUP_IMA: add_early_ima_buffer(pa_data); break; + case SETUP_KEXEC_KHO: + add_kho(pa_data, data_len); + break; case SETUP_RNG_SEED: data = early_memremap(pa_data, data_len); add_bootloader_randomness(data->data, data->len); @@ -989,8 +1015,26 @@ void __init setup_arch(char **cmdline_p) cleanup_highmap(); memblock_set_current_limit(ISA_END_ADDRESS); + e820__memblock_setup(); + /* + * We can resize memblocks at this point, let's dump all KHO + * reservations in and switch from scratch-only to normal allocations + */ + kho_reserve_mem(); + + /* Allocations now skip scratch mem, return low 1M to the pool */ + if (is_kho_boot()) { + u64 i; + phys_addr_t base, end; + + __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, + MEMBLOCK_SCRATCH, &base, &end, NULL) + if (end <= ISA_END_ADDRESS) + memblock_clear_scratch(base, end - base); + } + /* * Needs to run after memblock setup because it needs the physical * memory size. @@ -1106,6 +1150,8 @@ void __init setup_arch(char **cmdline_p) */ arch_reserve_crashkernel(); + kho_reserve(); + memblock_find_dma_reserve(); if (!early_xdbc_setup_hardware()) diff --git a/arch/x86/mm/init_32.c b/arch/x86/mm/init_32.c index b63403d7179d..6c3810afed04 100644 --- a/arch/x86/mm/init_32.c +++ b/arch/x86/mm/init_32.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -738,6 +739,12 @@ void __init mem_init(void) after_bootmem = 1; x86_init.hyper.init_after_bootmem(); + /* + * Now that all KHO pages are marked as reserved, let's flip them back + * to normal pages with accurate refcount. + */ + kho_populate_refcount(); + /* * Check boundaries twice: Some fundamental inconsistencies can * be detected at build time already. diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c index a190aae8ceaf..3ce1a4767610 100644 --- a/arch/x86/mm/init_64.c +++ b/arch/x86/mm/init_64.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -1339,6 +1340,12 @@ void __init mem_init(void) after_bootmem = 1; x86_init.hyper.init_after_bootmem(); + /* + * Now that all KHO pages are marked as reserved, let's flip them back + * to normal pages with accurate refcount. + */ + kho_populate_refcount(); + /* * Must be done after boot memory is put on freelist, because here we * might set fields in deferred struct pages that have not yet been From patchwork Wed Dec 13 00:04:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177684 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081121vqy; Tue, 12 Dec 2023 16:07:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHnRb03p8oj23V85eikpdDREBaiZGf9SzxdAk4kGigH5XTc0iBXSL1xZq3iMNUyR0M/sARb X-Received: by 2002:a17:90b:297:b0:28a:b563:502b with SMTP id az23-20020a17090b029700b0028ab563502bmr1543688pjb.47.1702426027511; Tue, 12 Dec 2023 16:07:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426027; cv=none; d=google.com; s=arc-20160816; b=aaaz2ed45o6OKSWhNYYsk4e86AAohBbFF+4xyLq+69mT0G/wCUMy1V4xwvp/lcCQPw okJhxXgxbcnM28+AfwgMYTF0YxEbnr2lyYPmd083LgJsjAP4IX+5UdpWY6RYPiL7lWoR RRzkt2hkBzw98iMswq+aSxIugTSP9n1i+IOLgEtiTSx3G5Jw3NecBb2lW1IPANSQ283D u3h0nIDuwBtCOcu8R/oryy++Vbr2JGWCgN9hjBsNIORN1kow4ROFmUqZIfY2V2BdGntY 1fimZWdUixbkR+uqgJCZ+i2Ib4Y/sWTPVp4hveL4rfzA6vodEtr1om1b9ccyK27CPe9u SvRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IHxE/iueMjg5jk3J7QeMpZujdfbmXnsIes6SvH8utzQ=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=n6l2hWrcMQqZVDsMeN9upq+DHmL7TtZw26AbxDZsN9TxEN+BBkOq+Vjg0ksHqbiIJO Y1KcDOB9aS/YnWYW0k5G/qZu2bxBPeya66r9iKxJHDkrvo6ZcON9IlVZf8ow6QkDqtmC se/H10rFzXYsvIH9XURHGNowsWFWFzmN8FQ+Ga3BJfLv4nXZz+p+0WuihnkXL72xof+R fzewWHqKVTKQkZASXUJdLUCDVGh3narkTVpcLK3ExHYSW5XziaS61I+Lu3K9ND3yGTbb tqBXT3MaBnj2vd75aMylEvSRp/lTZZz3eQKQZNs577Gm4diOJ0eS7vYh7HjPmsITzbyt AMDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=h2YU4YR9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id b13-20020a170902d50d00b001d0b5aa3000si8765290plg.456.2023.12.12.16.07.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:07:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=h2YU4YR9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 69E818063D41; Tue, 12 Dec 2023 16:07:04 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377709AbjLMAGo (ORCPT + 99 others); Tue, 12 Dec 2023 19:06:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40404 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377734AbjLMAGh (ORCPT ); Tue, 12 Dec 2023 19:06:37 -0500 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4883811A; Tue, 12 Dec 2023 16:06:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425994; x=1733961994; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IHxE/iueMjg5jk3J7QeMpZujdfbmXnsIes6SvH8utzQ=; b=h2YU4YR9wcBn2kwVG+LxOLzaht5wGiWplBrw1iQvgQEnfE/GWJhznk7r kn/3+aUb/TwmwTlEEOclmOuQ+rDPLPiAmR4MVtM96JUaXdrOc1suHL+nf PDnNuuXCoW2qfXJwrjg0vijQQq3Vka0KecZGru6djuNuZyuShw/931eWC s=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="382524268" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-e651a362.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:06:27 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-e651a362.us-east-1.amazon.com (Postfix) with ESMTPS id E9CFD8179B; Wed, 13 Dec 2023 00:06:13 +0000 (UTC) Received: from EX19MTAUWB002.ant.amazon.com [10.0.7.35:11247] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.18.147:2525] with esmtp (Farcaster) id 7d1c1fe5-8e35-45c9-ab5b-5b98cbcd951c; Wed, 13 Dec 2023 00:06:12 +0000 (UTC) X-Farcaster-Flow-ID: 7d1c1fe5-8e35-45c9-ab5b-5b98cbcd951c Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:12 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:08 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 08/15] tracing: Introduce names for ring buffers Date: Wed, 13 Dec 2023 00:04:45 +0000 Message-ID: <20231213000452.88295-9-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D042UWA003.ant.amazon.com (10.13.139.44) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:07:04 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123074159718800 X-GMAIL-MSGID: 1785123074159718800 With KHO (Kexec HandOver), we want to preserve trace buffers across kexec. To carry over their state between kernels, the kernel needs a common handle for them that exists on both sides. As handle we introduce names for ring buffers. In a follow-up patch, the kernel can then use these names to recover buffer contents for specific ring buffers. Signed-off-by: Alexander Graf --- include/linux/ring_buffer.h | 7 ++++--- kernel/trace/ring_buffer.c | 5 ++++- kernel/trace/trace.c | 7 ++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h index 782e14f62201..f34538f97c75 100644 --- a/include/linux/ring_buffer.h +++ b/include/linux/ring_buffer.h @@ -85,17 +85,18 @@ void ring_buffer_discard_commit(struct trace_buffer *buffer, * size is in bytes for each per CPU buffer. */ struct trace_buffer * -__ring_buffer_alloc(unsigned long size, unsigned flags, struct lock_class_key *key); +__ring_buffer_alloc(const char *name, unsigned long size, unsigned flags, + struct lock_class_key *key); /* * Because the ring buffer is generic, if other users of the ring buffer get * traced by ftrace, it can produce lockdep warnings. We need to keep each * ring buffer's lock class separate. */ -#define ring_buffer_alloc(size, flags) \ +#define ring_buffer_alloc(name, size, flags) \ ({ \ static struct lock_class_key __key; \ - __ring_buffer_alloc((size), (flags), &__key); \ + __ring_buffer_alloc((name), (size), (flags), &__key); \ }) int ring_buffer_wait(struct trace_buffer *buffer, int cpu, int full); diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 43cc47d7faaf..eaaf823ddedb 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -557,6 +557,7 @@ struct trace_buffer { struct rb_irq_work irq_work; bool time_stamp_abs; + const char *name; }; struct ring_buffer_iter { @@ -1801,7 +1802,8 @@ static void rb_free_cpu_buffer(struct ring_buffer_per_cpu *cpu_buffer) * when the buffer wraps. If this flag is not set, the buffer will * drop data when the tail hits the head. */ -struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, +struct trace_buffer *__ring_buffer_alloc(const char *name, + unsigned long size, unsigned flags, struct lock_class_key *key) { struct trace_buffer *buffer; @@ -1823,6 +1825,7 @@ struct trace_buffer *__ring_buffer_alloc(unsigned long size, unsigned flags, buffer->flags = flags; buffer->clock = trace_clock_local; buffer->reader_lock_key = key; + buffer->name = name; init_irq_work(&buffer->irq_work.work, rb_wake_up_waiters); init_waitqueue_head(&buffer->irq_work.waiters); diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 9aebf904ff97..7700ca1be2a5 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -9384,7 +9384,8 @@ allocate_trace_buffer(struct trace_array *tr, struct array_buffer *buf, int size buf->tr = tr; - buf->buffer = ring_buffer_alloc(size, rb_flags); + buf->buffer = ring_buffer_alloc(tr->name ? tr->name : "global_trace", + size, rb_flags); if (!buf->buffer) return -ENOMEM; @@ -9421,7 +9422,7 @@ static int allocate_trace_buffers(struct trace_array *tr, int size) return ret; #ifdef CONFIG_TRACER_MAX_TRACE - ret = allocate_trace_buffer(tr, &tr->max_buffer, + ret = allocate_trace_buffer(NULL, &tr->max_buffer, allocate_snapshot ? size : 1); if (MEM_FAIL(ret, "Failed to allocate trace buffer\n")) { free_trace_buffer(&tr->array_buffer); @@ -10473,7 +10474,7 @@ __init static int tracer_alloc_buffers(void) goto out_free_cpumask; /* Used for event triggers */ ret = -ENOMEM; - temp_buffer = ring_buffer_alloc(PAGE_SIZE, RB_FL_OVERWRITE); + temp_buffer = ring_buffer_alloc("temp_buffer", PAGE_SIZE, RB_FL_OVERWRITE); if (!temp_buffer) goto out_rm_hp_state; From patchwork Wed Dec 13 00:04:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177687 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081300vqy; Tue, 12 Dec 2023 16:07:31 -0800 (PST) X-Google-Smtp-Source: AGHT+IGU1YpWfo1BvA+3xFZ9d2s8FbYesT/9C9S+XQOFdFKQjUcqrLrFC8e3WBvtDTfTJ1KFMuxG X-Received: by 2002:a17:902:c408:b0:1d1:c96a:c72 with SMTP id k8-20020a170902c40800b001d1c96a0c72mr5355292plk.49.1702426051345; Tue, 12 Dec 2023 16:07:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426051; cv=none; d=google.com; s=arc-20160816; b=hbvPfo8HVDPJvGTam/Rfo7PU4hRPc6hWZFFDniPHMd/GzayD8QLDsdDtAZ2qD1KLVe le7IB9qGKlCExN6l9O/UQibmKHuFV1ngmx3zkSP0davLGnJwHyFSeDZjKewOmrFmS9ks M5Hvi7jMfPj14+fmZleVMzUz+LS7OaFYV9/Xx+PuV/iUZCQYryziaSdQdXQU8fBHa5NW 68juMc/oW4g8sZJ9o8tX8HJtGHZCcADer10LiAxk9UaSlvBnHfpO4roHx0rX6+tOmxak gmEK9u9xUaKdZ39v0ioMWvgChV/alVxopoNRskPwyvIBXiyADAHqtLyVXQ+NTq7k7kRG rbRA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=W4f7NpctXKZH3cmvw+OkpzmI5P36spomZeCIdEWiFNg=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=hKodrwvFXsx3XXjmzEa86PXyBCueQEu1fpyWyZ/kLGBexFcLbcvRHnq+9PeX5/x8Ep rSuEjeCBpbYGlrob69cwaFEFo2ZyRsvnv2jWVkW4jAglGlcl7rmnhINHeodsZUMvZIHp gfOY9SoMtqtIP2N5t8mRSen+zq0kp9CljTNGvDuwOKAXLZLTlD0+/djwbhoMbIG63N6r rr/R+wYdhNyiNI+wpDWkKz72w1p97ODjcK/F5SK+Gbhluc24A99HVw6pOnPoTjNk8nw5 9roDc4LIArl4Nw7yVEYF7DR9vpyWE3Se7MrgJ5GsC+jsCgqX28rMH9tt5xlSnbnR3xGq HA3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=DkbR7zzf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id 19-20020a170902c21300b001cfbd271f2esi8370864pll.7.2023.12.12.16.07.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:07:31 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=DkbR7zzf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id D9E92804A715; Tue, 12 Dec 2023 16:07:27 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377736AbjLMAG4 (ORCPT + 99 others); Tue, 12 Dec 2023 19:06:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43544 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377735AbjLMAGs (ORCPT ); Tue, 12 Dec 2023 19:06:48 -0500 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F428BE; Tue, 12 Dec 2023 16:06:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426007; x=1733962007; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=W4f7NpctXKZH3cmvw+OkpzmI5P36spomZeCIdEWiFNg=; b=DkbR7zzfwsrmsGshX3hhRfCzBMcF7zk+VBbnG4zcEKOkwWOOS0Lfdhrw qlO/PG95sHD3MdIQFzZA51dRINcNWCTELZ0NXPFn9gfITZpHDHUlUZ4Iv HB6jDwD1t7CuMndMzQ0C/nkw4BvFnwtk7KddlrioPDHu+hm4LLRfxCDpi g=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="316893079" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-cadc3fbd.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:06:43 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan2.pdx.amazon.com [10.39.38.66]) by email-inbound-relay-pdx-2b-m6i4x-cadc3fbd.us-west-2.amazon.com (Postfix) with ESMTPS id 1B5EEA0AE1; Wed, 13 Dec 2023 00:06:38 +0000 (UTC) Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:30423] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.11.243:2525] with esmtp (Farcaster) id f27b5327-6195-40ac-be88-dcf792d94fa4; Wed, 13 Dec 2023 00:06:38 +0000 (UTC) X-Farcaster-Flow-ID: f27b5327-6195-40ac-be88-dcf792d94fa4 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:38 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:34 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 09/15] tracing: Introduce names for events Date: Wed, 13 Dec 2023 00:04:46 +0000 Message-ID: <20231213000452.88295-10-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D036UWB002.ant.amazon.com (10.13.139.139) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:07:28 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123099063926621 X-GMAIL-MSGID: 1785123099063926621 With KHO (Kexec HandOver), we want to preserve trace buffers. To parse them, we need to ensure that all trace events that exist in the logs are identical to the ones we parse as. That means we need to match the events before and after kexec. As a first step towards that, let's give every event a unique name. That way we can clearly identify the event before and after kexec and restore its ID post-kexec. Signed-off-by: Alexander Graf --- include/linux/trace_events.h | 1 + include/trace/trace_events.h | 2 ++ kernel/trace/blktrace.c | 1 + kernel/trace/trace_branch.c | 1 + kernel/trace/trace_events.c | 3 +++ kernel/trace/trace_functions_graph.c | 4 +++- kernel/trace/trace_output.c | 13 +++++++++++++ kernel/trace/trace_probe.c | 3 +++ kernel/trace/trace_syscalls.c | 29 ++++++++++++++++++++++++++++ 9 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/linux/trace_events.h b/include/linux/trace_events.h index d68ff9b1247f..7670224aa92d 100644 --- a/include/linux/trace_events.h +++ b/include/linux/trace_events.h @@ -149,6 +149,7 @@ struct trace_event { struct hlist_node node; int type; struct trace_event_functions *funcs; + const char *name; }; extern int register_trace_event(struct trace_event *event); diff --git a/include/trace/trace_events.h b/include/trace/trace_events.h index c2f9cabf154d..bb4e6a33eef9 100644 --- a/include/trace/trace_events.h +++ b/include/trace/trace_events.h @@ -443,6 +443,7 @@ static struct trace_event_call __used event_##call = { \ .tp = &__tracepoint_##call, \ }, \ .event.funcs = &trace_event_type_funcs_##template, \ + .event.name = __stringify(call), \ .print_fmt = print_fmt_##template, \ .flags = TRACE_EVENT_FL_TRACEPOINT, \ }; \ @@ -460,6 +461,7 @@ static struct trace_event_call __used event_##call = { \ .tp = &__tracepoint_##call, \ }, \ .event.funcs = &trace_event_type_funcs_##call, \ + .event.name = __stringify(template), \ .print_fmt = print_fmt_##call, \ .flags = TRACE_EVENT_FL_TRACEPOINT, \ }; \ diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c index d5d94510afd3..7f86fd41b38e 100644 --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -1584,6 +1584,7 @@ static struct trace_event_functions trace_blk_event_funcs = { static struct trace_event trace_blk_event = { .type = TRACE_BLK, .funcs = &trace_blk_event_funcs, + .name = "blk", }; static int __init init_blk_tracer(void) diff --git a/kernel/trace/trace_branch.c b/kernel/trace/trace_branch.c index e47fdb4c92fb..3372070f2e85 100644 --- a/kernel/trace/trace_branch.c +++ b/kernel/trace/trace_branch.c @@ -168,6 +168,7 @@ static struct trace_event_functions trace_branch_funcs = { static struct trace_event trace_branch_event = { .type = TRACE_BRANCH, .funcs = &trace_branch_funcs, + .name = "branch", }; static struct tracer branch_trace __read_mostly = diff --git a/kernel/trace/trace_events.c b/kernel/trace/trace_events.c index f29e815ca5b2..4f5d37f96a17 100644 --- a/kernel/trace/trace_events.c +++ b/kernel/trace/trace_events.c @@ -2658,6 +2658,9 @@ static int event_init(struct trace_event_call *call) if (WARN_ON(!name)) return -EINVAL; + if (!call->event.name) + call->event.name = name; + if (call->class->raw_init) { ret = call->class->raw_init(call); if (ret < 0 && ret != -ENOSYS) diff --git a/kernel/trace/trace_functions_graph.c b/kernel/trace/trace_functions_graph.c index c35fbaab2a47..088dfd4a1a56 100644 --- a/kernel/trace/trace_functions_graph.c +++ b/kernel/trace/trace_functions_graph.c @@ -1342,11 +1342,13 @@ static struct trace_event_functions graph_functions = { static struct trace_event graph_trace_entry_event = { .type = TRACE_GRAPH_ENT, .funcs = &graph_functions, + .name = "graph_ent", }; static struct trace_event graph_trace_ret_event = { .type = TRACE_GRAPH_RET, - .funcs = &graph_functions + .funcs = &graph_functions, + .name = "graph_ret", }; static struct tracer graph_trace __tracer_data = { diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c index d8b302d01083..f3677e0da795 100644 --- a/kernel/trace/trace_output.c +++ b/kernel/trace/trace_output.c @@ -1067,6 +1067,7 @@ static struct trace_event_functions trace_fn_funcs = { static struct trace_event trace_fn_event = { .type = TRACE_FN, .funcs = &trace_fn_funcs, + .name = "fn", }; /* TRACE_CTX an TRACE_WAKE */ @@ -1207,6 +1208,7 @@ static struct trace_event_functions trace_ctx_funcs = { static struct trace_event trace_ctx_event = { .type = TRACE_CTX, .funcs = &trace_ctx_funcs, + .name = "ctx", }; static struct trace_event_functions trace_wake_funcs = { @@ -1219,6 +1221,7 @@ static struct trace_event_functions trace_wake_funcs = { static struct trace_event trace_wake_event = { .type = TRACE_WAKE, .funcs = &trace_wake_funcs, + .name = "wake", }; /* TRACE_STACK */ @@ -1256,6 +1259,7 @@ static struct trace_event_functions trace_stack_funcs = { static struct trace_event trace_stack_event = { .type = TRACE_STACK, .funcs = &trace_stack_funcs, + .name = "stack", }; /* TRACE_USER_STACK */ @@ -1309,6 +1313,7 @@ static struct trace_event_functions trace_user_stack_funcs = { static struct trace_event trace_user_stack_event = { .type = TRACE_USER_STACK, .funcs = &trace_user_stack_funcs, + .name = "user_stack", }; /* TRACE_HWLAT */ @@ -1373,6 +1378,7 @@ static struct trace_event_functions trace_hwlat_funcs = { static struct trace_event trace_hwlat_event = { .type = TRACE_HWLAT, .funcs = &trace_hwlat_funcs, + .name = "hwlat", }; /* TRACE_OSNOISE */ @@ -1443,6 +1449,7 @@ static struct trace_event_functions trace_osnoise_funcs = { static struct trace_event trace_osnoise_event = { .type = TRACE_OSNOISE, .funcs = &trace_osnoise_funcs, + .name = "osnoise", }; /* TRACE_TIMERLAT */ @@ -1491,6 +1498,7 @@ static struct trace_event_functions trace_timerlat_funcs = { static struct trace_event trace_timerlat_event = { .type = TRACE_TIMERLAT, .funcs = &trace_timerlat_funcs, + .name = "timerlat", }; /* TRACE_BPUTS */ @@ -1535,6 +1543,7 @@ static struct trace_event_functions trace_bputs_funcs = { static struct trace_event trace_bputs_event = { .type = TRACE_BPUTS, .funcs = &trace_bputs_funcs, + .name = "bputs", }; /* TRACE_BPRINT */ @@ -1579,6 +1588,7 @@ static struct trace_event_functions trace_bprint_funcs = { static struct trace_event trace_bprint_event = { .type = TRACE_BPRINT, .funcs = &trace_bprint_funcs, + .name = "bprint", }; /* TRACE_PRINT */ @@ -1616,6 +1626,7 @@ static struct trace_event_functions trace_print_funcs = { static struct trace_event trace_print_event = { .type = TRACE_PRINT, .funcs = &trace_print_funcs, + .name = "print", }; static enum print_line_t trace_raw_data(struct trace_iterator *iter, int flags, @@ -1645,6 +1656,7 @@ static struct trace_event_functions trace_raw_data_funcs = { static struct trace_event trace_raw_data_event = { .type = TRACE_RAW_DATA, .funcs = &trace_raw_data_funcs, + .name = "raw_data", }; static enum print_line_t @@ -1691,6 +1703,7 @@ static struct trace_event_functions trace_func_repeats_funcs = { static struct trace_event trace_func_repeats_event = { .type = TRACE_FUNC_REPEATS, .funcs = &trace_func_repeats_funcs, + .name = "func_repeats", }; static struct trace_event *events[] __initdata = { diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c index 4dc74d73fc1d..9356f3f2fdc0 100644 --- a/kernel/trace/trace_probe.c +++ b/kernel/trace/trace_probe.c @@ -1835,6 +1835,9 @@ int trace_probe_register_event_call(struct trace_probe *tp) trace_probe_name(tp))) return -EEXIST; + if (!call->event.name) + call->event.name = call->name; + ret = register_trace_event(&call->event); if (!ret) return -ENODEV; diff --git a/kernel/trace/trace_syscalls.c b/kernel/trace/trace_syscalls.c index 9c581d6da843..3e7e10b691f5 100644 --- a/kernel/trace/trace_syscalls.c +++ b/kernel/trace/trace_syscalls.c @@ -441,8 +441,29 @@ static void unreg_event_syscall_exit(struct trace_event_file *file, mutex_unlock(&syscall_trace_lock); } +/** + * trace_csum - a simple checksum generator + * + * This returns a checksum for data that should not generate + * a lot of collisions, but is trivial to read. + */ +static u32 __init trace_csum(void *data, u32 len) +{ + u32 r = 0, i; + char *p = data; + + if (!data) + return 0; + + for (i = 0; i < len; i++) + r = (r >> 31) + (r << 1) + p[i]; + + return (r << 4) + len; +} + static int __init init_syscall_trace(struct trace_event_call *call) { + u32 csum; int id; int num; @@ -456,9 +477,17 @@ static int __init init_syscall_trace(struct trace_event_call *call) if (set_syscall_print_fmt(call) < 0) return -ENOMEM; + csum = (trace_csum(call->print_fmt, strlen(call->print_fmt)) << 4) + num; + call->event.name = kasprintf(GFP_KERNEL, "sc-%x", csum); + if (!call->event.name) { + free_syscall_print_fmt(call); + return -ENOMEM; + } + id = trace_event_raw_init(call); if (id < 0) { + kfree(call->event.name); free_syscall_print_fmt(call); return id; } From patchwork Wed Dec 13 00:04:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177686 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081221vqy; Tue, 12 Dec 2023 16:07:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IHvyvDyqEf2ORw58JpDnRedRM/JLDh8MejDSq4UiO245yFfql/t0zx3+Ua6Sgtt2Ipy54Kx X-Received: by 2002:a05:6a20:d42f:b0:190:53c1:fee2 with SMTP id il47-20020a056a20d42f00b0019053c1fee2mr8198938pzb.19.1702426041673; Tue, 12 Dec 2023 16:07:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426041; cv=none; d=google.com; s=arc-20160816; b=E5G1jF+pmf/HXAXFK2AQZtM86brFeDFhm4WBbYLO/z/PW/KCZgHYIpiq/hzdUSyiQr v3hj30eaooXl1S9FySE4ZNLbWZqhl7oFbGSf4khlb014Rm3PY5bv2P8+BwuAuFeriL6V XNFwROpyV5AYFpol62Lyyff0LKO9GKWEGUNypjR6bMgRdagKOPyUpoha/SCEPmP4nGHf IGfRDWBRL4p80KP7H6FjG3EToZlb59KKvHIfHUHrAYBSb62yhELrAgnBjp0HMrny+Vht wz/v5Hm7qXyXOm1xDHG/I4A1IrMTMJQLI9tfC7bmAKkvmReX3vwxlhhmfI2RzjkGN0ut 7dTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=YG4rwMst//uzBl8lCmh1WsDiUmAxsOlA2EWBHH+q6Y0=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=FE/+8q7z/13QO78KFJFOi58UZdANcBEyqxxzxwuI4SmAEmZFYrEqv3q8JsYgEAh013 xtaBXQEsHmHcaH+/BMtEDF2B60ckdmgLjFv1mvwDjTkXKV9SXs0At3cImBdC0qZ0mycx Mb58Ymx1tLO1GreQ/buCsnKk5uqQgg7mCVNaqwaULyLVo/wiUs1onm0bV/byJcOSDKhX mzXUNQ7LmcThevcKhTp/k48Oa/i80pO88LbsrlnEEFbblqLbPqsz+djKXIigwLbvMGtm bDm9BOCyJy8P0uwuKA6QGh2Zg8ncbBfMNlRHvP7wUZB3G14xrU4UZOi36gZjI0qnjAsC yxSw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=ZBkod8vn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id i3-20020a6551c3000000b005c6977f9c0dsi8511408pgq.214.2023.12.12.16.07.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:07:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=ZBkod8vn; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 9A1D98061B51; Tue, 12 Dec 2023 16:07:20 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377703AbjLMAHI (ORCPT + 99 others); Tue, 12 Dec 2023 19:07:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377632AbjLMAG5 (ORCPT ); Tue, 12 Dec 2023 19:06:57 -0500 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5937A18E; Tue, 12 Dec 2023 16:06:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426018; x=1733962018; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=YG4rwMst//uzBl8lCmh1WsDiUmAxsOlA2EWBHH+q6Y0=; b=ZBkod8vn6sA9dHhEWtz1kRQZvJTdFsBXS+osxx8h9RCflDvvbGdNSsOK jRX4i4MCEmD/LL/MpuvPMBywPScO3+zWno93v71Zz3FTdRBAGsPPQvOpc WLSLtUKLYfRRpXjS7vzd13YA+VOY2MXEBO5OOZyvAGtr3euXqfoq/VJZi 4=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="259290025" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-pdx-2b-m6i4x-ed19f671.us-west-2.amazon.com) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:06:55 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (pdx2-ws-svc-p26-lb5-vlan3.pdx.amazon.com [10.39.38.70]) by email-inbound-relay-pdx-2b-m6i4x-ed19f671.us-west-2.amazon.com (Postfix) with ESMTPS id 69799881F9; Wed, 13 Dec 2023 00:06:53 +0000 (UTC) Received: from EX19MTAUWC002.ant.amazon.com [10.0.38.20:53530] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.18.147:2525] with esmtp (Farcaster) id 28ae9c8f-1014-431a-84b5-e7284b5dddc6; Wed, 13 Dec 2023 00:06:53 +0000 (UTC) X-Farcaster-Flow-ID: 28ae9c8f-1014-431a-84b5-e7284b5dddc6 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWC002.ant.amazon.com (10.250.64.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:42 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:38 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 10/15] tracing: Introduce kho serialization Date: Wed, 13 Dec 2023 00:04:47 +0000 Message-ID: <20231213000452.88295-11-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D036UWB002.ant.amazon.com (10.13.139.139) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:07:20 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123089049117268 X-GMAIL-MSGID: 1785123089049117268 We want to be able to transfer ftrace state from one kernel to the next. To start off with, let's establish all the boiler plate to get a write hook when KHO wants to serialize and fill out basic data. Follow-up patches will fill in serialization of ring buffers and events. Signed-off-by: Alexander Graf --- kernel/trace/trace.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 7700ca1be2a5..3e7f61cf773e 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -866,6 +867,10 @@ static struct tracer *trace_types __read_mostly; */ DEFINE_MUTEX(trace_types_lock); +#ifdef CONFIG_FTRACE_KHO +static bool trace_in_kho; +#endif + /* * serialize the access of the ring buffer * @@ -10591,12 +10596,59 @@ void __init early_trace_init(void) init_events(); } +#ifdef CONFIG_FTRACE_KHO +static int trace_kho_notifier(struct notifier_block *self, + unsigned long cmd, + void *v) +{ + const char compatible[] = "ftrace-v1"; + void *fdt = v; + int err = 0; + + switch (cmd) { + case KEXEC_KHO_ABORT: + if (trace_in_kho) + mutex_unlock(&trace_types_lock); + trace_in_kho = false; + return NOTIFY_DONE; + case KEXEC_KHO_DUMP: + /* Handled below */ + break; + default: + return NOTIFY_BAD; + } + + if (unlikely(tracing_disabled)) + return NOTIFY_DONE; + + err |= fdt_begin_node(fdt, "ftrace"); + err |= fdt_property(fdt, "compatible", compatible, sizeof(compatible)); + err |= fdt_end_node(fdt); + + if (!err) { + /* Hold all future allocations */ + mutex_lock(&trace_types_lock); + trace_in_kho = true; + } + + return err ? NOTIFY_BAD : NOTIFY_DONE; +} + +static struct notifier_block trace_kho_nb = { + .notifier_call = trace_kho_notifier, +}; +#endif + void __init trace_init(void) { trace_event_init(); if (boot_instance_index) enable_instances(); + +#ifdef CONFIG_FTRACE_KHO + register_kho_notifier(&trace_kho_nb); +#endif } __init static void clear_boot_tracer(void) From patchwork Wed Dec 13 00:04:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177688 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081507vqy; Tue, 12 Dec 2023 16:07:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IFRTE7ZCW6dgy15voR29aFUjbH9G5dQVp+LycSJYV6xRoJRTDFnb1SiE+Nj8Tc9YAm7famT X-Received: by 2002:a05:6a20:3242:b0:190:245d:92b6 with SMTP id hm2-20020a056a20324200b00190245d92b6mr3574863pzc.8.1702426075531; Tue, 12 Dec 2023 16:07:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426075; cv=none; d=google.com; s=arc-20160816; b=bYT6J0YqhLb6EBZJW4dY7Iqedvn8qGOBYaYZyTaOJ9JFh2drmAf6sLIe0Gxpxq+d2a grKwP9ss8NB3b4woArSzob/kR/lfx9TJmPLP3c2fnEzfCVgTVqhxYrMv0tqewp2mLtyL gsu7PDx77fBBAphipXGip3NbG10PAXjfzOwSuw4kUu8/v65OG9UQ0bZyBgTu1Y8/YuS5 jh6tmQ+IZIIXynn5yQFKXq7REfX69/8EgmaEwP7I3ucCaIzItOmpzoQ1TmsKYTCaTtaj n7bNey3sxnIQuuBIDS5Ok7TC2+a69xXwBQNPXcvmP0ik0egJH4YG0IC3o+lXd0c4tO8i 3jmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AEaNvGFNPvF0yeo/Uvj2gVs9WvSmpPsv4M/fAIzD1+w=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=TghqJdAVtOuhGv8RD86DSS8FH6k/C+pWuwhg5x7q7cN+Vg8FBNECpofD5DifWE0Xb1 v0nvMOGfj4QtV1Io3mtSPSKzOOuBTKUjah1CmN+XBXTlVCbvKhTVsXOCiDxV2OTnfEZs Wk2fYanmY/x22TP4Cnz8yfVMGmZN01Rwbrd0bykj9TCU+2m/S5CMwrouEOaAplDgvg0m aUW3Xw49Tt31+3PDL/L62Kk4a23Im5IGw1cDZGhVWd7HuZX70jycXolacbTLDAFv3EIP X9hlltUjKc3ZFK99U9ODfBqBgDONO1sGX9bMzdWUQwXgrMSrc10plcSJhdM05+KRsNby J7EQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=nniyo2lK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id u6-20020a17090341c600b001cfb4a3adadsi8839896ple.344.2023.12.12.16.07.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:07:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=nniyo2lK; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 1D52980A8DB5; Tue, 12 Dec 2023 16:07:48 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377710AbjLMAHj (ORCPT + 99 others); Tue, 12 Dec 2023 19:07:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43252 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377689AbjLMAHc (ORCPT ); Tue, 12 Dec 2023 19:07:32 -0500 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C95AB18B; Tue, 12 Dec 2023 16:07:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426036; x=1733962036; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AEaNvGFNPvF0yeo/Uvj2gVs9WvSmpPsv4M/fAIzD1+w=; b=nniyo2lK+qzsrwSsUkYm/annSPRQk6N/wIWmWsKWLS4bnVZf/Ie1qmb3 K5UmJdW+kW70q34kVgnODCWoF5efbg+fP3Il0rEmjqkaQXEpW7sg35bur IGyxj0onWP0cyLKPhA//4dCkV9ACLTvTI1Mg6VNyKnEyesDWPTga/GS+U g=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="50248318" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-93c3b254.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:07:03 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan2.iad.amazon.com [10.32.235.34]) by email-inbound-relay-iad-1a-m6i4x-93c3b254.us-east-1.amazon.com (Postfix) with ESMTPS id 2F0D1E118A; Wed, 13 Dec 2023 00:06:55 +0000 (UTC) Received: from EX19MTAUWA001.ant.amazon.com [10.0.21.151:49570] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.34.165:2525] with esmtp (Farcaster) id 1bfa1fde-7b72-4349-97e0-73423d0282ab; Wed, 13 Dec 2023 00:06:55 +0000 (UTC) X-Farcaster-Flow-ID: 1bfa1fde-7b72-4349-97e0-73423d0282ab Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWA001.ant.amazon.com (10.250.64.204) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:46 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:06:42 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 11/15] tracing: Add kho serialization of trace buffers Date: Wed, 13 Dec 2023 00:04:48 +0000 Message-ID: <20231213000452.88295-12-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D036UWB002.ant.amazon.com (10.13.139.139) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:07:48 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123124678981402 X-GMAIL-MSGID: 1785123124678981402 When we do a kexec handover, we want to preserve previous ftrace data into the new kernel. At the point when we write out the handover data, ftrace may still be running and recording new events and we want to capture all of those too. To allow the new kernel to revive all trace data up to reboot, we store all locations of trace buffers as well as their linked list metadata. We can then later reuse the linked list to reconstruct the head pointer. This patch implements the write-out logic for trace buffers. Signed-off-by: Alexander Graf --- include/linux/ring_buffer.h | 2 + kernel/trace/ring_buffer.c | 89 +++++++++++++++++++++++++++++++++++++ kernel/trace/trace.c | 16 +++++++ 3 files changed, 107 insertions(+) diff --git a/include/linux/ring_buffer.h b/include/linux/ring_buffer.h index f34538f97c75..049565677ef8 100644 --- a/include/linux/ring_buffer.h +++ b/include/linux/ring_buffer.h @@ -212,4 +212,6 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node); #define trace_rb_cpu_prepare NULL #endif +int trace_kho_write_trace_buffer(void *fdt, struct trace_buffer *buffer); + #endif /* _LINUX_RING_BUFFER_H */ diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index eaaf823ddedb..691d1236eeb1 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -5921,6 +5922,94 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node) return 0; } +#ifdef CONFIG_FTRACE_KHO +static int trace_kho_write_cpu(void *fdt, struct trace_buffer *buffer, int cpu) +{ + int i = 0; + int err = 0; + struct list_head *tmp; + const char compatible[] = "ftrace,cpu-v1"; + char name[] = "cpuffffffff"; + int nr_pages; + struct ring_buffer_per_cpu *cpu_buffer; + bool first_loop = true; + struct kho_mem *mem; + uint64_t mem_len; + + if (!cpumask_test_cpu(cpu, buffer->cpumask)) + return 0; + + cpu_buffer = buffer->buffers[cpu]; + + nr_pages = cpu_buffer->nr_pages; + mem_len = sizeof(*mem) * nr_pages * 2; + mem = vmalloc(mem_len); + + snprintf(name, sizeof(name), "cpu%x", cpu); + + err |= fdt_begin_node(fdt, name); + err |= fdt_property(fdt, "compatible", compatible, sizeof(compatible)); + err |= fdt_property(fdt, "cpu", &cpu, sizeof(cpu)); + + for (tmp = rb_list_head(cpu_buffer->pages); + tmp != rb_list_head(cpu_buffer->pages) || first_loop; + tmp = rb_list_head(tmp->next), first_loop = false) { + struct buffer_page *bpage = (struct buffer_page *)tmp; + + /* Ring is larger than it should be? */ + if (i >= (nr_pages * 2)) { + pr_err("ftrace ring has more pages than nr_pages (%d / %d)", i, nr_pages); + err = -EINVAL; + break; + } + + /* First describe the bpage */ + mem[i++] = (struct kho_mem) { + .addr = __pa(bpage), + .len = sizeof(*bpage) + }; + + /* Then the data page */ + mem[i++] = (struct kho_mem) { + .addr = __pa(bpage->page), + .len = PAGE_SIZE + }; + } + + err |= fdt_property(fdt, "mem", mem, mem_len); + err |= fdt_end_node(fdt); + + vfree(mem); + return err; +} + +int trace_kho_write_trace_buffer(void *fdt, struct trace_buffer *buffer) +{ + const char compatible[] = "ftrace,buffer-v1"; + char name[] = "buffer"; + int err; + int i; + + err = fdt_begin_node(fdt, name); + if (err) + return err; + + fdt_property(fdt, "compatible", compatible, sizeof(compatible)); + + for (i = 0; i < buffer->cpus; i++) { + err = trace_kho_write_cpu(fdt, buffer, i); + if (err) + return err; + } + + err = fdt_end_node(fdt); + if (err) + return err; + + return 0; +} +#endif + #ifdef CONFIG_RING_BUFFER_STARTUP_TEST /* * This is a basic integrity check of the ring buffer. diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 3e7f61cf773e..71c249cc5b43 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -10597,6 +10597,21 @@ void __init early_trace_init(void) } #ifdef CONFIG_FTRACE_KHO +static int trace_kho_write_trace_array(void *fdt, struct trace_array *tr) +{ + const char *name = tr->name ? tr->name : "global_trace"; + const char compatible[] = "ftrace,array-v1"; + int err = 0; + + err |= fdt_begin_node(fdt, name); + err |= fdt_property(fdt, "compatible", compatible, sizeof(compatible)); + err |= fdt_property(fdt, "trace_flags", &tr->trace_flags, sizeof(tr->trace_flags)); + err |= trace_kho_write_trace_buffer(fdt, tr->array_buffer.buffer); + err |= fdt_end_node(fdt); + + return err; +} + static int trace_kho_notifier(struct notifier_block *self, unsigned long cmd, void *v) @@ -10623,6 +10638,7 @@ static int trace_kho_notifier(struct notifier_block *self, err |= fdt_begin_node(fdt, "ftrace"); err |= fdt_property(fdt, "compatible", compatible, sizeof(compatible)); + err |= trace_kho_write_trace_array(fdt, &global_trace); err |= fdt_end_node(fdt); if (!err) { From patchwork Wed Dec 13 00:04:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177692 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081791vqy; Tue, 12 Dec 2023 16:08:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IEZbB1gPyn+gAZYV18yQcQ0qSqRnRIB57sH2h8MSUHQ9uLtgbFp9+EeskedAS+Mblg4Hfpc X-Received: by 2002:a05:6e02:b4e:b0:35d:6e2e:9d18 with SMTP id f14-20020a056e020b4e00b0035d6e2e9d18mr7031772ilu.125.1702426107523; Tue, 12 Dec 2023 16:08:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426107; cv=none; d=google.com; s=arc-20160816; b=m8G6uagpWdki9tYbPQE8cPNNLskq1uoE5Wb3XNuXIz7+iAh8YAE2CQz3coBsQ1stfV th+AQwGls0mmlsLsSusOTWUlaoaYV9aLv9zQh7s3SJVvm+DAbYDfKXs7m3nElOHsbcqx JvaiDCpdcDzWBncbXZuhk3/y2XWLFS98NOwn+LtYPUXb859MQSPCMftLCtktkzG9tPI+ TB3xuQvr+ey1aivK6uXaReho+jMDr1mQVFnweWrL15hvgu3hwNhv5qPb2jmGAGmzIKuH 7oGHbVF5vTfE5qDdpeyRs4r2mYj0eCYJV6hRQop5et7Lpo+p2637eYucvFx07t+zCs80 HTKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DMVF+g90vjiGQ2+mH0UneD/R5uFZpo7f74QocXGx4Fo=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=LmRFbtOLidZz4AcGl7QhTxUzNibF9emUHyfZgiZCFafGOrhl2AJHFABuLEaO/1DBro axpNz0FbvfmOqmi94QWj5JIEJZTAoq1Zakq0V4MSo/PtH53icqtX35ckc6URUxScuYHv cr0DtbfX7sFLufw07N01+hg/1UQVi6kwGg7S4q0tVj8RS4X4BvoQrgyDedD2EjVlPn1A XUMHYlngeLVBGXvd8hCZdeFWRAOMOmg3c0Th6e+Udkf+MqNnip+En4v94CLG5XdMqNmp 35cyVQwSuaNKEh/dwRy1axAqQ1nsTIJ0h2c7sFhXQTLtOdGFq8EUrk2zM9eicvfDJ8Ez SzjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=tDAMyHkz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id bx2-20020a056a02050200b005be1e55546esi8976868pgb.51.2023.12.12.16.08.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:08:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=tDAMyHkz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 3A72E80879ED; Tue, 12 Dec 2023 16:08:24 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377855AbjLMAHy (ORCPT + 99 others); Tue, 12 Dec 2023 19:07:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56246 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377872AbjLMAHq (ORCPT ); Tue, 12 Dec 2023 19:07:46 -0500 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F01910F1; Tue, 12 Dec 2023 16:07:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426048; x=1733962048; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=DMVF+g90vjiGQ2+mH0UneD/R5uFZpo7f74QocXGx4Fo=; b=tDAMyHkzpGHvBkdyysb06lS0YYGx5ezQ6YbmvJ3XqhoDtF8wFG5ExLIa RMHdlLPnUnHCLj64VSqYAJy6umccZXapJtArq5ntEeM77Yngc5N+or+UQ HU2rKyBdkvz+RjqvKqZvDnxglCgiJlUKECb4SEMcl4Xi1W7AhkWzyoZUS U=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="259290097" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-bbc6e425.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80007.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:07:24 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-bbc6e425.us-east-1.amazon.com (Postfix) with ESMTPS id 9684F806AE; Wed, 13 Dec 2023 00:07:12 +0000 (UTC) Received: from EX19MTAUWC001.ant.amazon.com [10.0.38.20:42528] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.34.165:2525] with esmtp (Farcaster) id d4eed612-db0d-44d2-b4c4-2cf51a2e8457; Wed, 13 Dec 2023 00:07:11 +0000 (UTC) X-Farcaster-Flow-ID: d4eed612-db0d-44d2-b4c4-2cf51a2e8457 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWC001.ant.amazon.com (10.250.64.174) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:11 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:07 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 12/15] tracing: Recover trace buffers from kexec handover Date: Wed, 13 Dec 2023 00:04:49 +0000 Message-ID: <20231213000452.88295-13-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D037UWB002.ant.amazon.com (10.13.138.121) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:08:24 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123158358789189 X-GMAIL-MSGID: 1785123158358789189 When kexec handover is in place, we now know the location of all previous buffers for ftrace rings. With this patch applied, ftrace reassembles any new trace buffer that carries the same name as a previous one with the same data pages that the previous buffer had. That way, a buffer that we had in place before kexec becomes readable after kexec again as soon as it gets initialized with the same name. Signed-off-by: Alexander Graf --- kernel/trace/ring_buffer.c | 173 ++++++++++++++++++++++++++++++++++++- 1 file changed, 171 insertions(+), 2 deletions(-) diff --git a/kernel/trace/ring_buffer.c b/kernel/trace/ring_buffer.c index 691d1236eeb1..f3d07cb90762 100644 --- a/kernel/trace/ring_buffer.c +++ b/kernel/trace/ring_buffer.c @@ -575,6 +575,28 @@ struct ring_buffer_iter { int missed_events; }; +struct trace_kho_cpu { + const struct kho_mem *mem; + uint32_t nr_mems; +}; + +#ifdef CONFIG_FTRACE_KHO +static int trace_kho_replace_buffers(struct ring_buffer_per_cpu *cpu_buffer, + struct trace_kho_cpu *kho); +static int trace_kho_read_cpu(const char *name, int cpu, struct trace_kho_cpu *kho); +#else +static int trace_kho_replace_buffers(struct ring_buffer_per_cpu *cpu_buffer, + struct trace_kho_cpu *kho) +{ + return -EINVAL; +} + +static int trace_kho_read_cpu(const char *name, int cpu, struct trace_kho_cpu *kho) +{ + return -EINVAL; +} +#endif + #ifdef RB_TIME_32 /* @@ -1807,10 +1829,12 @@ struct trace_buffer *__ring_buffer_alloc(const char *name, unsigned long size, unsigned flags, struct lock_class_key *key) { + int cpu = raw_smp_processor_id(); + struct trace_kho_cpu kho = {}; struct trace_buffer *buffer; + bool use_kho = false; long nr_pages; int bsize; - int cpu; int ret; /* keep it in its own cache line */ @@ -1823,6 +1847,12 @@ struct trace_buffer *__ring_buffer_alloc(const char *name, goto fail_free_buffer; nr_pages = DIV_ROUND_UP(size, BUF_PAGE_SIZE); + if (!trace_kho_read_cpu(name, cpu, &kho) && kho.nr_mems > 4) { + nr_pages = kho.nr_mems / 2; + use_kho = true; + pr_debug("Using kho for buffer '%s' on CPU [%03d]", name, cpu); + } + buffer->flags = flags; buffer->clock = trace_clock_local; buffer->reader_lock_key = key; @@ -1843,12 +1873,14 @@ struct trace_buffer *__ring_buffer_alloc(const char *name, if (!buffer->buffers) goto fail_free_cpumask; - cpu = raw_smp_processor_id(); cpumask_set_cpu(cpu, buffer->cpumask); buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu); if (!buffer->buffers[cpu]) goto fail_free_buffers; + if (use_kho && trace_kho_replace_buffers(buffer->buffers[cpu], &kho)) + pr_warn("Could not revive all previous trace data"); + ret = cpuhp_state_add_instance(CPUHP_TRACE_RB_PREPARE, &buffer->node); if (ret < 0) goto fail_free_buffers; @@ -5886,7 +5918,9 @@ EXPORT_SYMBOL_GPL(ring_buffer_read_page); */ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node) { + struct trace_kho_cpu kho = {}; struct trace_buffer *buffer; + bool use_kho = false; long nr_pages_same; int cpu_i; unsigned long nr_pages; @@ -5910,6 +5944,12 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node) /* allocate minimum pages, user can later expand it */ if (!nr_pages_same) nr_pages = 2; + + if (!trace_kho_read_cpu(buffer->name, cpu, &kho) && kho.nr_mems > 4) { + nr_pages = kho.nr_mems / 2; + use_kho = true; + } + buffer->buffers[cpu] = rb_allocate_cpu_buffer(buffer, nr_pages, cpu); if (!buffer->buffers[cpu]) { @@ -5917,12 +5957,141 @@ int trace_rb_cpu_prepare(unsigned int cpu, struct hlist_node *node) cpu); return -ENOMEM; } + + if (use_kho && trace_kho_replace_buffers(buffer->buffers[cpu], &kho)) + pr_warn("Could not revive all previous trace data"); + smp_wmb(); cpumask_set_cpu(cpu, buffer->cpumask); return 0; } #ifdef CONFIG_FTRACE_KHO +static int trace_kho_replace_buffers(struct ring_buffer_per_cpu *cpu_buffer, + struct trace_kho_cpu *kho) +{ + bool first_loop = true; + struct list_head *tmp; + int err = 0; + int i = 0; + + if (kho->nr_mems != cpu_buffer->nr_pages * 2) + return -EINVAL; + + for (tmp = rb_list_head(cpu_buffer->pages); + tmp != rb_list_head(cpu_buffer->pages) || first_loop; + tmp = rb_list_head(tmp->next), first_loop = false) { + struct buffer_page *bpage = (struct buffer_page *)tmp; + const struct kho_mem *mem_bpage = &kho->mem[i++]; + const struct kho_mem *mem_page = &kho->mem[i++]; + const uint64_t rb_page_head = 1; + struct buffer_page *old_bpage; + void *old_page; + + old_bpage = __va(mem_bpage->addr); + if (!bpage) + goto out; + + if ((ulong)old_bpage->list.next & rb_page_head) { + struct list_head *new_lhead; + struct buffer_page *new_head; + + new_lhead = rb_list_head(bpage->list.next); + new_head = (struct buffer_page *)new_lhead; + + /* Assume the buffer is completely full */ + cpu_buffer->tail_page = bpage; + cpu_buffer->commit_page = bpage; + /* Set the head pointers to what they were before */ + cpu_buffer->head_page->list.prev->next = (struct list_head *) + ((ulong)cpu_buffer->head_page->list.prev->next & ~rb_page_head); + cpu_buffer->head_page = new_head; + bpage->list.next = (struct list_head *)((ulong)new_lhead | rb_page_head); + } + + if (rb_page_entries(old_bpage) || rb_page_write(old_bpage)) { + /* + * We want to recycle the pre-kho page, it contains + * trace data. To do so, we unreserve it and swap the + * current data page with the pre-kho one + */ + old_page = kho_claim_mem(mem_page); + + /* Recycle the old page, it contains data */ + free_page((ulong)bpage->page); + bpage->page = old_page; + + bpage->write = old_bpage->write; + bpage->entries = old_bpage->entries; + bpage->real_end = old_bpage->real_end; + + local_inc(&cpu_buffer->pages_touched); + } else { + kho_return_mem(mem_page); + } + + kho_return_mem(mem_bpage); + } + +out: + return err; +} + +static int trace_kho_read_cpu(const char *name, int cpu, + struct trace_kho_cpu *kho) +{ + void *fdt = kho_get_fdt(); + int mem_len; + int err = 0; + char *path; + int off; + + if (!fdt) + return -ENOENT; + + if (!kho) + return -EINVAL; + + path = kasprintf(GFP_KERNEL, "/ftrace/%s/buffer/cpu%x", name, cpu); + if (!path) + return -ENOMEM; + + pr_debug("Trying to revive trace buffer '%s'", path); + + off = fdt_path_offset(fdt, path); + if (off < 0) { + pr_debug("Could not find '%s' in DT", path); + err = -ENOENT; + goto out; + } + + err = fdt_node_check_compatible(fdt, off, "ftrace,cpu-v1"); + if (err) { + pr_warn("Node '%s' has invalid compatible", path); + err = -EINVAL; + goto out; + } + + kho->mem = fdt_getprop(fdt, off, "mem", &mem_len); + if (!kho->mem) { + pr_warn("Node '%s' has invalid mem property", path); + err = -EINVAL; + goto out; + } + + kho->nr_mems = mem_len / sizeof(*kho->mem); + + /* Should follow "bpage 0, page 0, bpage 1, page 1, ..." pattern */ + if ((kho->nr_mems & 1)) { + err = -EINVAL; + goto out; + } + +out: + kfree(path); + return err; +} + static int trace_kho_write_cpu(void *fdt, struct trace_buffer *buffer, int cpu) { int i = 0; From patchwork Wed Dec 13 00:04:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177689 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081649vqy; Tue, 12 Dec 2023 16:08:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IHdzD57Snttp/qu5ewZ0dIxC9MEsuzZMgHg7EdxyrJbmsT7xPMsLzAIcJTiLn1kozbl2XAS X-Received: by 2002:a05:6a20:7d8e:b0:190:2ce8:dc87 with SMTP id v14-20020a056a207d8e00b001902ce8dc87mr9623644pzj.60.1702426091118; Tue, 12 Dec 2023 16:08:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426091; cv=none; d=google.com; s=arc-20160816; b=LpVUOWLGzm3moyHqNugjmHE26MvhJP5SxH/NvLQCRQUmVpmk1sW4ZLE2IHmwJIYxws bHg2AO6YZCIaQg2uOi238W12E+7UxrGGQf3F3nS+9bdOmM3AAAz9DFS905hxxlwS3HuI Rtp2T+lapbtO8lqmGNJdNNyedlmwEHrl4yejaaMGa2PkETjU6SylIKSHizIyrLijzDdc NNd5ZWbF+0o4tlyXIlJyO5aW52YqCEXs+j1LbSSYlq0f6eZS+aQIznL4/3ULJB2RDS3i TLyPK6CFVHW8tyBUB0t0mxB6ktZHsgOUDF1Le2wNZvj3qsoFtsZBBy+DTdfM2bIt/7Eu f6cA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=mc8BtMBoJccPwXhN5lfGmopEl26ZPwt65Ef0Tkxcd1Y=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=odKZKcNoAP4Oe9EExZL7mkB8JIpScmYa3yIT95KoHW7BPMk2aM04jaKUn9g49/W340 zUBC7NBT5t3rBBmwIUGnXHWdPbX+bfp62x/ik6Wos8pjoP+2DQDFj15JIouvgF4aNniJ sIouqoIOYiRku4xW3dpqQOjfOPq5K+nlSjLLECkz0DEs12x/GJhy9NiaiiqOKR1VYbVd /liHGgSAV5Z2W3CxQhqTRvkopUDKm0vfMhpIonDzFxLTQagWp8PamM6IbC7yIkGUYLGM sDF4K18eLe+SVBTplejRgTERDYBGRqxANY8Yz2A3pqU7MT5yPuqkeU7KNRhalPkeB4+2 XvAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=FnOcI0xG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from snail.vger.email (snail.vger.email. [2620:137:e000::3:7]) by mx.google.com with ESMTPS id j12-20020a65428c000000b005be1ee5b9dfsi8192805pgp.454.2023.12.12.16.08.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:08:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) client-ip=2620:137:e000::3:7; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=FnOcI0xG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:7 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 1F8E580B81F1; Tue, 12 Dec 2023 16:08:10 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377887AbjLMAH5 (ORCPT + 99 others); Tue, 12 Dec 2023 19:07:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377879AbjLMAHr (ORCPT ); Tue, 12 Dec 2023 19:07:47 -0500 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C01E810E0; Tue, 12 Dec 2023 16:07:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426047; x=1733962047; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mc8BtMBoJccPwXhN5lfGmopEl26ZPwt65Ef0Tkxcd1Y=; b=FnOcI0xGSqHXF1IumCe7s2J3qekB/hoJrmN/2OgRnXsHHDROP/5rRwky ificEcPRknowG5k4oTm3djXQsxV/VkzrJzqssOywUQ/eUc5ZeAlsJyCem oeBXX1OPquiqqSaCUxRVTVdPCzdkJLN5oGMO/GzSpQTKMedUSRlxrg+dv s=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="368122532" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-e651a362.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-2101.iad2.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:07:24 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-e651a362.us-east-1.amazon.com (Postfix) with ESMTPS id 5BC7680493; Wed, 13 Dec 2023 00:07:16 +0000 (UTC) Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:32184] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.43.236:2525] with esmtp (Farcaster) id 342278a3-113e-4653-a12d-678bc8408d80; Wed, 13 Dec 2023 00:07:15 +0000 (UTC) X-Farcaster-Flow-ID: 342278a3-113e-4653-a12d-678bc8408d80 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:15 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:11 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 13/15] tracing: Add kho serialization of trace events Date: Wed, 13 Dec 2023 00:04:50 +0000 Message-ID: <20231213000452.88295-14-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D037UWB002.ant.amazon.com (10.13.138.121) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:08:10 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123141084565246 X-GMAIL-MSGID: 1785123141084565246 Events and thus their parsing handle in ftrace have dynamic IDs that get assigned whenever the event is added to the system. If we want to parse trace events after kexec, we need to link event IDs back to the original trace event that existed before we kexec'ed. There are broadly 2 paths we could take for that: 1) Save full event description across KHO, restore after kexec, merge identical trace events into a single identifier. 2) Recover the ID of post-kexec added events so they get the same ID after kexec that they had before kexec This patch implements the second option. It's simpler and thus less intrusive. However, it means we can not fully parse affected events when the kernel removes or modifies trace events across a kho kexec. Signed-off-by: Alexander Graf --- kernel/trace/trace.c | 1 + kernel/trace/trace_output.c | 28 ++++++++++++++++++++++++++++ kernel/trace/trace_output.h | 1 + 3 files changed, 30 insertions(+) diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c index 71c249cc5b43..26edfd2a85fd 100644 --- a/kernel/trace/trace.c +++ b/kernel/trace/trace.c @@ -10639,6 +10639,7 @@ static int trace_kho_notifier(struct notifier_block *self, err |= fdt_begin_node(fdt, "ftrace"); err |= fdt_property(fdt, "compatible", compatible, sizeof(compatible)); err |= trace_kho_write_trace_array(fdt, &global_trace); + err |= trace_kho_write_events(fdt); err |= fdt_end_node(fdt); if (!err) { diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c index f3677e0da795..113de40c616f 100644 --- a/kernel/trace/trace_output.c +++ b/kernel/trace/trace_output.c @@ -12,6 +12,7 @@ #include #include #include +#include #include "trace_output.h" @@ -669,6 +670,33 @@ int trace_print_lat_context(struct trace_iterator *iter) return !trace_seq_has_overflowed(s); } +int trace_kho_write_events(void *fdt) +{ +#ifdef CONFIG_FTRACE_KHO + const char compatible[] = "ftrace,events-v1"; + const char *name = "events"; + struct trace_event *event; + unsigned key; + int err = 0; + + err |= fdt_begin_node(fdt, name); + err |= fdt_property(fdt, "compatible", compatible, sizeof(compatible)); + + for (key = 0; key < EVENT_HASHSIZE; key++) { + hlist_for_each_entry(event, &event_hash[key], node) + err |= fdt_property(fdt, event->name, &event->type, + sizeof(event->type)); + } + + err |= fdt_end_node(fdt); + + return err; +#else + return 0; +#endif +} + + /** * ftrace_find_event - find a registered event * @type: the type of event to look for diff --git a/kernel/trace/trace_output.h b/kernel/trace/trace_output.h index dca40f1f1da4..36dc7963269e 100644 --- a/kernel/trace/trace_output.h +++ b/kernel/trace/trace_output.h @@ -25,6 +25,7 @@ extern enum print_line_t print_event_fields(struct trace_iterator *iter, extern void trace_event_read_lock(void); extern void trace_event_read_unlock(void); extern struct trace_event *ftrace_find_event(int type); +extern int trace_kho_write_events(void *fdt); extern enum print_line_t trace_nop_print(struct trace_iterator *iter, int flags, struct trace_event *event); From patchwork Wed Dec 13 00:04:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177691 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081716vqy; Tue, 12 Dec 2023 16:08:21 -0800 (PST) X-Google-Smtp-Source: AGHT+IGNWatmRsdNrtAshQiVA3uVshqMkYrcETr3wj4tY2EIDlbC6jqv2vqEM8rIUro30fp7fDSu X-Received: by 2002:a05:6a20:9149:b0:18f:97c:4f43 with SMTP id x9-20020a056a20914900b0018f097c4f43mr3982021pzc.79.1702426101209; Tue, 12 Dec 2023 16:08:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426101; cv=none; d=google.com; s=arc-20160816; b=MFwxIWddT6/bj3Jd31Ut9r0fRpwKA7kzWKJoelyEiVGSPcVSzLkXWLxsK1hpH4Z7JN JdOjgo1XPhgKIpo05eF8SWLyTLOyFNNWjxxr8xc3OSDipPXF4Vp8JMYNgWrvlqmVLWa1 yW4hg+TnurhYQNr+J8WrzzoqO9rmCJI1TKtH1zWIbhVGKis+flj3nKlyxs7B0Yv5hb4+ Wwl/XDdHQmqfcOHOkrJJeThBqTBi8e0jr3ypq3sLgiCX2pV63b61sdlS6BgNazxiS46Y /UPDYfnexM1L0T7s5HxAeIUxP0mGdW4Ptb9pYEzoI9Lv48MBcAibm6UfEIhNUAUowh/Y ZOrg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ijxY/skruFfgxp/KO2yhFBX0a87YYNVgJHNp25q/K2g=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=nsl6iZO/418GMH49j/vaUxlQ0V9aPUMFYfgAotdXsT/KZyDEFcHFPwrh+tx4Q3NWwa 6DlyclznYi7IgAsmIvBJ379xbwc7BOd/byB5CXgolTf4TLjAuag2L1RZTkVxeV1668HN 8VKQqXHmq5u/lY5yMXIGB2wUyDxc3MvsoSZ0j+JiDq508xkGXtSAHBNb2TZhLE5fykGs Cojxdad2MCZlVujOopCMcg/h9NphMGSsToDzVTlmwOicIcZU2EJL7Y3uLs8PUnFDZxjW DG5NQaVWJTY8EqHYzsxnfzyaQrKIujTL32Ana02wxNBE/YhaNsRJjzjui1GKOkbdSmO0 Uu3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=R7NSmQrU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id b15-20020a170902b60f00b001cf5d537eb6si1063368pls.190.2023.12.12.16.08.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:08:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=R7NSmQrU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 80568801B49B; Tue, 12 Dec 2023 16:08:07 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377870AbjLMAH4 (ORCPT + 99 others); Tue, 12 Dec 2023 19:07:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377876AbjLMAHr (ORCPT ); Tue, 12 Dec 2023 19:07:47 -0500 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BF7DC171F; Tue, 12 Dec 2023 16:07:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426050; x=1733962050; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ijxY/skruFfgxp/KO2yhFBX0a87YYNVgJHNp25q/K2g=; b=R7NSmQrUAop5SGqsD8840QQUTTgeEG6R8+wfoNYtOjYuIbwvdLvde1VW 5d+VaOun9FxuUzm6mgBiYxJCAbrImxyXOx0apfhi3NCOUk9CjJGeu+nYh HdTUt24cbcxcal1AevcOQhzmzyDXmDfkEkXBCI622UVvK0RppoC1zKDlZ Y=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="50248355" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO email-inbound-relay-iad-1d-m6i4x-00fceed5.us-east-1.amazon.com) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:07:28 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1d-m6i4x-00fceed5.us-east-1.amazon.com (Postfix) with ESMTPS id 96E81A0B0F; Wed, 13 Dec 2023 00:07:20 +0000 (UTC) Received: from EX19MTAUWA002.ant.amazon.com [10.0.21.151:13591] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.2.31:2525] with esmtp (Farcaster) id d7626d9c-b144-446f-b11a-bed8a3932442; Wed, 13 Dec 2023 00:07:19 +0000 (UTC) X-Farcaster-Flow-ID: d7626d9c-b144-446f-b11a-bed8a3932442 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:19 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:15 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 14/15] tracing: Recover trace events from kexec handover Date: Wed, 13 Dec 2023 00:04:51 +0000 Message-ID: <20231213000452.88295-15-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D037UWB002.ant.amazon.com (10.13.138.121) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:08:08 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123151211256012 X-GMAIL-MSGID: 1785123151211256012 This patch implements all logic necessary to match a new trace event that we add against preserved trace events from kho. If we find a match, we give the new trace event the old event's identifier. That way, trace read-outs are able to make sense of buffer contents again because the parsing code for events looks at the same identifiers. Signed-off-by: Alexander Graf --- kernel/trace/trace_output.c | 65 ++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c index 113de40c616f..d2e2a6346322 100644 --- a/kernel/trace/trace_output.c +++ b/kernel/trace/trace_output.c @@ -749,6 +749,67 @@ void trace_event_read_unlock(void) up_read(&trace_event_sem); } +/** + * trace_kho_fill_event_type - restore event type info from KHO + * @event: the event type to enumerate + * + * Event types are semi-dynamically generated. To ensure that + * their identifiers match before and after kexec with KHO, + * let's match up unique name identifiers and fill in the + * respective ID information if we booted with KHO. + */ +static bool trace_kho_fill_event_type(struct trace_event *event) +{ +#ifdef CONFIG_FTRACE_KHO + const char *path = "/ftrace/events"; + void *fdt = kho_get_fdt(); + int err, len, off, id; + const void *p; + + if (!fdt) + return false; + + if (WARN_ON(!event->name)) + return false; + + pr_debug("Trying to revive event '%s'", event->name); + + off = fdt_path_offset(fdt, path); + if (off < 0) { + pr_debug("Could not find '%s' in DT", path); + return false; + } + + err = fdt_node_check_compatible(fdt, off, "ftrace,events-v1"); + if (err) { + pr_warn("Node '%s' has invalid compatible", path); + return false; + } + + p = fdt_getprop(fdt, off, event->name, &len); + if (!p) { + pr_warn("Event '%s' not found", event->name); + return false; + } + + if (len != sizeof(event->type)) { + pr_warn("Event '%s' has invalid length", event->name); + return false; + } + + id = *(const u32 *)p; + + /* Mark ID as in use */ + if (ida_alloc_range(&trace_event_ida, id, id, GFP_KERNEL) != id) + return false; + + event->type = id; + return true; +#endif + + return false; +} + /** * register_trace_event - register output for an event type * @event: the event type to register @@ -777,7 +838,9 @@ int register_trace_event(struct trace_event *event) if (WARN_ON(!event->funcs)) goto out; - if (!event->type) { + if (trace_kho_fill_event_type(event)) { + pr_debug("Recovered '%s' as id=%d", event->name, event->type); + } else if (!event->type) { event->type = alloc_trace_event_type(); if (!event->type) goto out; From patchwork Wed Dec 13 00:04:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 177693 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp8081915vqy; Tue, 12 Dec 2023 16:08:41 -0800 (PST) X-Google-Smtp-Source: AGHT+IEX7bDIwvkhGl6AJ8EyLB5CzEL1nEzNB5pyV+0Y1GLcJkCzpT2iPt1CN26FEfv9J9UcAZW0 X-Received: by 2002:a05:6e02:12eb:b0:35d:591f:861 with SMTP id l11-20020a056e0212eb00b0035d591f0861mr11310851iln.8.1702426121048; Tue, 12 Dec 2023 16:08:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702426121; cv=none; d=google.com; s=arc-20160816; b=wN6lIdBrAnwsnroQU5Oo6DdZdQ6yMkKmUz9AuUcroRwE4jJi5ZuVOXMvydki3SA5tf z3iOFP2/mdksV6xcmJx3QulpwOB/9xhtow6XtK4+HSaLgtE3N4DHpwKfvoP7pSmPRVj8 Njhoioo9z7rDmLp2TQQxFGPCorJp/GVC5CiGvMAN9P86tAL0JXxfnEQASX5YL69GN8HU y1PpFPBHG6hJr6uD6mTGqZ73RpqGKEpyGsWznnb0r4vEF0P0oButHgK9Q4dBobomhKLT 2XHkXPP9I33uZDqlCQ+7kYxE6uGWRUtg5y97Zgtid8g7/TFd/k0uHGfZcpNNcVzWpejK dYvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=8E5gYeBn7LzFXeE9t3+WWJhfPWwonViYFj973oiYUIk=; fh=xprWcyUKTLi4rhte7gFCvduD673j12awuFkCPnYDQVY=; b=Ccs/Hw2QIUabXbSgM+At1RRS8CKGajWwZqdtufoRpMiIxC0S+NqR8XrhzHbS+BIsKB QFbvHdnJc+F/rV1CjYJF7NzNNC+hx9VRHfAn+RVnlV3N1+NybEglsIr4j2Aswd3cOgB0 7CNlno6s2od9Zq2GkZKdF/OhyVXgUs5TnQ/D0c0CNsrSquJ+J99bfoa2ko86F9yuVIL3 /ubL/I3GtsGKHuJom+RIOQtQUs9OTD8uL7v4E1PF1yG0VeZMP9ImaA96ASl4ltEB9wN9 x+9L4w4cdEoVXykPHAtdjoJJrAfqaweLWgWWpU8X6Ob+cOUC+VP2bLUmk4ssoBe+fB7J A0/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=a1vPSAn4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id k2-20020a63f002000000b005bdf597ed49si8705960pgh.56.2023.12.12.16.08.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 16:08:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=a1vPSAn4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id E8F6680879ED; Tue, 12 Dec 2023 16:08:37 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377838AbjLMAI1 (ORCPT + 99 others); Tue, 12 Dec 2023 19:08:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377851AbjLMAIP (ORCPT ); Tue, 12 Dec 2023 19:08:15 -0500 Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 70463D4D; Tue, 12 Dec 2023 16:08:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426083; x=1733962083; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8E5gYeBn7LzFXeE9t3+WWJhfPWwonViYFj973oiYUIk=; b=a1vPSAn4RN5MYIpsd3DyyRCkJuBHnCOBZooVN6/fJapR6P9VnnkYiZDS 1vd8AU/P7+v6f0O8Dzt4xCJuMNWr6H27HNgYhSjG9x5NzAiItEAdByflx 5JxfSMdqz0mSchn+8natHOk8lS7fixALyrIXy89rim6IuNE5YAMbj/jnk 0=; X-IronPort-AV: E=Sophos;i="6.04,271,1695686400"; d="scan'208";a="621371857" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-iad-1a-m6i4x-bbc6e425.us-east-1.amazon.com) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Dec 2023 00:08:01 +0000 Received: from smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev (iad7-ws-svc-p70-lb3-vlan3.iad.amazon.com [10.32.235.38]) by email-inbound-relay-iad-1a-m6i4x-bbc6e425.us-east-1.amazon.com (Postfix) with ESMTPS id EF61380632; Wed, 13 Dec 2023 00:07:52 +0000 (UTC) Received: from EX19MTAUWB001.ant.amazon.com [10.0.21.151:16104] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.43.236:2525] with esmtp (Farcaster) id 3bda4ab2-5ec6-4d32-90c9-2880fbf14c72; Wed, 13 Dec 2023 00:07:51 +0000 (UTC) X-Farcaster-Flow-ID: 3bda4ab2-5ec6-4d32-90c9-2880fbf14c72 Received: from EX19D020UWC004.ant.amazon.com (10.13.138.149) by EX19MTAUWB001.ant.amazon.com (10.250.64.248) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:44 +0000 Received: from dev-dsk-graf-1a-5ce218e4.eu-west-1.amazon.com (10.253.83.51) by EX19D020UWC004.ant.amazon.com (10.13.138.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Wed, 13 Dec 2023 00:07:40 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , "Rob Herring" , Steven Rostedt , "Andrew Morton" , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt Subject: [PATCH 15/15] tracing: Add config option for kexec handover Date: Wed, 13 Dec 2023 00:04:52 +0000 Message-ID: <20231213000452.88295-16-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231213000452.88295-1-graf@amazon.com> References: <20231213000452.88295-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D033UWA001.ant.amazon.com (10.13.139.103) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Tue, 12 Dec 2023 16:08:38 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785123172368332068 X-GMAIL-MSGID: 1785123172368332068 Now that all bits are in place to allow ftrace to pass its trace data into the next kernel on kexec, let's give users a kconfig option to enable the functionality. Signed-off-by: Alexander Graf --- kernel/trace/Kconfig | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/kernel/trace/Kconfig b/kernel/trace/Kconfig index 61c541c36596..af83ee755b9e 100644 --- a/kernel/trace/Kconfig +++ b/kernel/trace/Kconfig @@ -1169,6 +1169,19 @@ config HIST_TRIGGERS_DEBUG If unsure, say N. +config FTRACE_KHO + bool "Ftrace Kexec handover support" + depends on KEXEC_KHO + help + Enable support for ftrace to pass metadata across kexec so the new + kernel continues to use the previous kernel's trace buffers. + + This can be useful when debugging kexec performance or correctness + issues: The new kernel can dump the old kernel's trace buffer which + contains all events until reboot. + + If unsure, say N. + source "kernel/trace/rv/Kconfig" endif # FTRACE