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: 13490136 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C038C4167B for ; Wed, 13 Dec 2023 00:05:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 90FF78D000B; Tue, 12 Dec 2023 19:05:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BFDC8D0009; Tue, 12 Dec 2023 19:05:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7614C8D000B; Tue, 12 Dec 2023 19:05:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 61CEF8D0009 for ; Tue, 12 Dec 2023 19:05:12 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 2A8711409DA for ; Wed, 13 Dec 2023 00:05:12 +0000 (UTC) X-FDA: 81559850064.10.6B34591 Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by imf18.hostedemail.com (Postfix) with ESMTP id E612D1C002A for ; Wed, 13 Dec 2023 00:05:09 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="c8qYexr/"; spf=pass (imf18.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.119.213.150 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425910; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2/zyadG4/H/PJTWoJtupWHcuxbsnDpMhuO8wY//fQ4A=; b=fgn75t9dKC1Rt++yq3fB9M2eZFJ9wNbyAWCIVvDMhT2iZDQhC6tz9OOove3qUZqOJ0HjQw Vf0sWOTYP7Sk8E15HnEfXZOBCc0cbb1C1nv5jtRDXanwHThNuO5RtpbAxxZXHxhk4rreo0 5vkfNw6IW7BUzJ0TSa6Z9DYeuyX9e9Y= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425910; a=rsa-sha256; cv=none; b=CSVbkqJ58xmr/aTUCxGnPQlLKktypsRoklGlr5JbJecnorMQ4IbYCAXm+KPrHpvgyC4B1W ebh/qUptwmzTrRJIPPnaIUuYKLAo1RJfDXOyySrkGOAgoPUfesvCv0qOSTeQDxRCucfMJl p7mKJzghE8S/fWlXZ0mKJlYb4xnPxKo= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="c8qYexr/"; spf=pass (imf18.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.119.213.150 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com 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-Rspamd-Queue-Id: E612D1C002A X-Rspam-User: X-Stat-Signature: zeq76atarhw6eee8ejcrxz71681tjezx X-Rspamd-Server: rspam03 X-HE-Tag: 1702425909-121338 X-HE-Meta: U2FsdGVkX1/Ywp+8nxrrsKTmNgow5maj2zFLsJtvMItyj8QJy0ynqvkmbzBq//wQPff7F2/rkJQCblOLohbvThXKu2D6tuZJvGx7gBnVk+MMkXiZ4xQTiePqV6qMMP9ZEcvD1DEbhmO7T31+hc7fz7IHFVoLC1Mz9KGu4P4SWoPKP/9F8FrAGe/w4ucvioYnC7V6TG1pGBaQbX5BWmJ+1m1/NhZXhaOYBPTjVKS/ln4yx6I/ByizrIlzkmdVk1tBHWjkKwL+jVvSLjUEivLqVBczaIPLGYHTs7RbR/VoiENCfpPO1zfTL40xe35KZTq3CoeziC3Q81V6bL+YS4/lBa+mCy6eLGm/BMrWnQq4t/ZbvLl7q7VMa4oKGp0oEfMnBb+VEfiztsN+XYrLg1mHW87qTaSjaGqzGxoIWWzgY4L+tOqH7fkq06Yh+see3Dyl+8c2jYKSQRPFyia88LPo4h7rAOJU8YwcrRbBXALUXViXvV3d6xnmlOgFF8NyuWZ4WWaAaOFiDGc2RWBDtm8lt0hOvDRFbu5bfXOsFCr1Xz8tSRgYFNBW4Qm7GhE9Yxjk2owXKNDj3nrDzKyKJ+aVBfuOU9wHR7rG8YZHTqlizWPkn11sTTP8YyWe9qt2pqKIHNXSTL6DSTMZT7OclAaPlrL720qOAy/87POF8/I0R/csKMIaj5sRb8MerNKILnilvkjU/KPm7QQoVKeDpnpUcFtCV9ICaX8ATGi4OZ2bKGt6XQaNZQUbkrNXlko6XARW26ugQ0ApjjFAxRFZuzEJePVp9BxrV04b4LRl0QA6b1a2fvs5Yt2Py3g5/qfP/x9hyE6OSf/h6UvYjBnwEm4kuJ+7RRYzm3x3IYWHBjXqn/xIj+4V/exIycSK6fa/IBZfhldc0TnACAUrePnXNjeGcu9u/3H4/6TGTpjxF61tIB4cdxv1FvmjjaDYUEk4uhEboHbtqDblmtgxfPcFgOz 7c+a7RZL jFO9rVZbVcZ6ohb2wv3wA6SP08kysk9Hs9m+DhuxxleQccfmjjQjmTvtuVDniw8T6FLTWq+PrH2ENeNu3yeYHscbnaUD5WprT/Z7qcN2dB4PavmQnB/2M/4KQjVOVKszztVNlWMGJKwS3oiyHHCxCv6etiu9hkcz2BnH6JXPm1KB2DhE3sVq6+3QWI0vh7uU07YHF6u4WgTgIoBs7Aa8o0jF+y+7cFCrzJTE+f8TboDvL8nNdPwxyfWE1DYGY8IW4TGY2zCqIItk2FgrZ94cA3C7n2o8gf/JYtdh/fSvYHPH4vEncvfk61lTFq7WXQFBWXdae84GUihTYX9Dv2lZL1Al46yy5rd9Bl8zB9N5mRGRJDURwIm5beZVsivpddGoC/89KLCg5kjr/aQyByHdH26GqZet6pP58Njg2HtlagiKBZBp4mEWLAfbY5IFLFx/NRIVWTGF343Sj5UGUzVN+ULof+x3RFNJNSqKPSSbrU2+FSUqghy4ZBQ28LaD1dx3bmW5QI3p0uBvT4LRFiFvRP1JwQymX9MVch5DTJtw+ik4TVcuKpYgs8FsYiWSNhDXUsxbZy5LT6ogDxJZaikhDjERcsoL6m5Or2X2E67/wMsipVTs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490138 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB461C4332F for ; Wed, 13 Dec 2023 00:05:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4084D6B019E; Tue, 12 Dec 2023 19:05:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3912E6B01A2; Tue, 12 Dec 2023 19:05:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E4676B01A5; Tue, 12 Dec 2023 19:05:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 024906B019E for ; Tue, 12 Dec 2023 19:05:21 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C0462120A9B for ; Wed, 13 Dec 2023 00:05:20 +0000 (UTC) X-FDA: 81559850400.22.B583D05 Received: from smtp-fw-80006.amazon.com (smtp-fw-80006.amazon.com [99.78.197.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 9209516001C for ; Wed, 13 Dec 2023 00:05:18 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=X9Yt1Xsm; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf08.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.217 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425918; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=omkZqOlO4bdGSVCdXn2us+8UWaNPdUSggP3ul/1DY+c=; b=MC8xncBcZJYZuBvo47bho0cwHTGK/HilcT2iejjLmPI+BDSe4Hfc0s4X03lHnOnHthMvEE kU5dp3yeJZDANdfsTQt/DwJo8evUzP5g3fueioVWD7vmkMQ5M13HYe4hOXipkuht2AMICj VjI5fmCoLJ4QWnuxvtNvnnjQkFCzpM4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=X9Yt1Xsm; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf08.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.217 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425918; a=rsa-sha256; cv=none; b=GXi8fCJZafSKQ3nK0DbtszSDrqn98blyPjY4r3n1g/iD0PuXAPrVYOxx2qC56SLzVJ/bkD 5mk40daeI3/fJbQDTxTiR5Z1v2bjUYTblVYCQ9RHmbEC2AvL7zCh1siyoU+O4G5Fvf9jIV zqUsKCIOXHtfqdauoA0zGs1v48kmpbM= 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-Rspamd-Queue-Id: 9209516001C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ymc6hemajyaiy5799fnbrnp6typy4jai X-HE-Tag: 1702425918-549451 X-HE-Meta: U2FsdGVkX1/CLCtLGbKTStqdxG8tJg3vBSsxmNMybQLTwyhcVkLCq1DnLzEtT2IRAGNOMbIHAS/QqVLBerAeYX7ZjjHmJ/fYKtS+k8NgsLZ1teiUxc7af8lFj8CBmH0w49UZAsZolu6fQyQZnZs9VJhpOb9XomrTu8eWNo/ORrX076hQo2183GpqTVoYM8M7ynjc3lF16WjNMU0NNN1UQsADBvYZxzkoQDmIQPqcjx1Gpnn73m76ffVlZpbUt/n4ZzrHMv3E2dQhoBo8QDRHoh7AOHgWlmit6eBDYQkQytRgwd6KbDol8pZn2iS0lCdx62fqJw/lvhHIRTBi0SOJnVLA92BxzWCUQJLnjuiB3cZydZkvksDYkmJbB0zPnCRzmS+FiFxsW5d/xFnNRytaXLx2EUANL3qtujWdUmp6fKtcJ7PELUQoqYpdduxWyfrTqFf6r2U6g09DScTrymx3lW1TQG1GEn4FXeEoi7r+aqAslmtyK1fOMVm+E1kLHYBfxSiCyrUUATY9iazhzL3H5hnTaHx91Gu8KO+uCFPtnpDcCTZKODhMIpfSV4jrADunYxMdFJd36l0lSBjahaL2XVKtAt5hBiUC6yQvhDRetVtbWF3xkuMaEMu/CcOLTi2e4i7hlI3XkgbdtNba9sFAoDg5tp7jV9M9e6JUoryAUYn/lGA67dE/hNZhDp+NKuJtCwkPV8Nh9ybzmJZf1MlSdwzlDasef2l/lBpRh2wWC4MHlKM7x83Rd1ek9C92814Sng8sjFqyBdyRP3hmNRcgpXfkYvSUc1Tx4+NTkuQCW+HQ4PvZoFP5UZP3aTal7S9+7PiQmHuDObX8S7cr4D+fwGgqmfE0tPOWcHnUicFha8i5RgCEz6RhDpSwZG24fz0jQR1b9IYM0qtkl3bOZIosq/So+GeLtshqLvgpms5rgJN3VCL24gnNOcQFI9/7KVobUDjp+MKlc1IGpNHnIfb kC1f9fMl 785kNimh/z2stO32wtmoqAmmcrIP0vXOfnPrJmO6O02e68CYbz7D9pJ/yQjftHa/4RsCqkrO37WbCDo5If0yfyTqD5McjeoyP0wbSHhc5nVmN4jRk4DXVKI1MTlNoR9a7BA1I4pR0yLKmtvXAX2leIK5WeSrqpQyvf28aQAMtE2ZG4OKsr/jilC+cAeXuNItCTp2ZDzbvDiijBiP8MBVPRmcmvMpyk8a1KsUO0OdNGHNB5pbZX+pN9QrFvOByTdSykos+kwsGuCKSHBKANFFwwcizW1sCsGjHGiudbkxaS6ewAz4KYvzTPZCm/woFizQ75lmCU5ZNRCoLlSm9NgKdtbkccKrxOskQLlgs7ejcLzgwSCB5X0yHYpenxw4fbeRLJ6X6mxpPEDQHKmvro1S5+s3hND9UevpvcdrrJxz7AIQRCbJXm4aOfuw/mGwjDhszc/Wypa+w1E6qFi16jb+RFX030OBDwUbTE9M6AQ8J0AdewYnxUHSjRqFUeAWYo56hgZC9FMZAQP4HcnwGVkcq4ZhiSrNfeSQG6GqeVLSANXLDQTGOLPQjm6Rcm/DPYvuG7kyL7Y7Y5MqrNEJkZkPPzrr3OezrNbt/SQZur/D4oEjsFMfny3YUMTlOcQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490139 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CD993C4332F for ; Wed, 13 Dec 2023 00:05:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3BB9D6B0391; Tue, 12 Dec 2023 19:05:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 369B56B0393; Tue, 12 Dec 2023 19:05:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E3946B0395; Tue, 12 Dec 2023 19:05:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 0864B6B0391 for ; Tue, 12 Dec 2023 19:05:46 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id C8AD580B6B for ; Wed, 13 Dec 2023 00:05:45 +0000 (UTC) X-FDA: 81559851450.05.57474DD Received: from smtp-fw-6001.amazon.com (smtp-fw-6001.amazon.com [52.95.48.154]) by imf19.hostedemail.com (Postfix) with ESMTP id 9CDD91A0020 for ; Wed, 13 Dec 2023 00:05:43 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=rBGXCCSw; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf19.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.95.48.154 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425943; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=bvtk3HQrKsl8B+ppqatJMPW6FdHejxoz+znJi2t9qtA=; b=LxQQASUfG0XlYNkqeFgLCo9rr/V5t/eQcYw4DrJOXG8KF3HGYFs5oM4IaF/n3GBCffFnw3 Ddul/vRZpyIrWJgNR8jvbrmC8Ej3gB0/pv+agSuRKXp6w2Th+qx2975F6ZUP1KrobNyS8T zwpO+X3twpsfCZUtpHoxFpRdyRxzjwU= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=rBGXCCSw; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf19.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.95.48.154 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425943; a=rsa-sha256; cv=none; b=KSBvzXW5Bspu2XBYADhMxswuY7pB+i/+TGNaQOPPJO8fzxm6+cFfxyH1UpZ1o8BABTDIla 4ZK0bfF0QuCjHMKm8CtUsy80uA5/P5ELg9D4Qewan83pllUm90A1tdYqEq3/UMdp+oE9hP +gYmLqPpqFej5tcfd+BdmJTSk+4mrhk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425943; x=1733961943; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bvtk3HQrKsl8B+ppqatJMPW6FdHejxoz+znJi2t9qtA=; b=rBGXCCSwOe0Otch8qmyj/vsBDcZ8JlGzFewHnbwKFuYYF1uw4kkj2uEp vM9Vt0isF9XiE8pBgSgM1x29bHjmKYVFWkoGHQ7PdekNCYd5A2NV0S9sH bw3t9JIfx+ixBDyZf8kl9wG9TFA9aCQawNvg+OozhlGB378QDfxY+YV8z k=; 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-Rspamd-Queue-Id: 9CDD91A0020 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 44tjoet8s6g3377gueou71hn3ewcj717 X-HE-Tag: 1702425943-305562 X-HE-Meta: U2FsdGVkX18sKdcVsWuP4GgdurCYUXmX58HeMOJG7yT8H8kv4je4MjDnZ7qnTSP7LSkZMm2uMa0boe2zZ29nA7jmFQqEFCMPwO5jWx5Wsv4KcEaow43bqReoF0zfJc/a0CTMUReHODSXGlE7xBxDOPbUWA9CVn/oLvzhgGra6VpemWx2p6FvQSF+xAdtCfKRTbICJYcuzHU1+eUvsJXYcOSnppXrz3YzTwUhtQSSgDnHDNUcsQdtrOWyDFSxdjnWrz0l2t/bUUACGfJxvKiw8lAVnmvesMHMPKvlncijEr7sEYD5ScwL7p/rwHAPwTljNSDOpFcFTGepK1T+4GgIQ1mx3z9WUeAz98EbVzFj6foJpRR9OcrZ+R+zNhRVVf2Q96LQYT2m0un5f2icvuEcqbNdfICzPCb8bO+NOaVm9eeOUH4GYAuMZvwFX+8cfO7JioSXE/V4V1+FvC2G0PmeFlucq3xBPAyFSAHHqU4LHkT3UwK5Rd9EjDoFynQbsBp9P2osSGBDjv1Qp/EbziewpDZ0xi0XR+fuYf6rhzAVluYCkNqcLkNZOKC76VC3cu8ksIyd+DnJ0CWoZYBmC0dWcyHVJ0dGjDFYVTOwot3KNy2TmawjiGM6m1HJuYRjzuYYjjSet/dLA9DFzpNzwgdUGBf17IB3pif9/6HZO6qsBk8Nguk8/X+LMrGuQ1dnya+R+Z1ZEIJtB0PpsFG8dqi+07l6ZvGzDEwRVJvQ0wv35LcmJfsDRiLpyfpuY0Eh8Gg+P15meDP/Cv8oIbKb+Xx9xtBmuWGw0R0gytnWpgz/FfTk1n1UffZljzDbe/MR6DMhQ0ItDz8w6hQhpA3dNyKQ4+pwVfCt9KcbugRA6k9+iZbgf42R4+xLEn88Kyi40vHN1tq9FI2a7jyucjvRSmjzn6cg0TSXj5JXr67yCxPeKB7CmQW379qq2qA/LKgoTE01ODJk3XNtsDW2+Bkx+Ug tJ6Cb5mr kK+ENm/cXvghim2E/3W3gNcT7dDfRdwOT6OmI8RTdJjqXQd0zttCkrXBB8fgpO/iQVKnilp3tPFLgl3edHmbhAU0HgzEa2NhfMhO/3N8CKrA1lI0k4ssl3q4Ng8en/TlsSQgJU7c1R61EwIlyG8Pmqfbcihoho6F1K65+8g+aZyoWeY1NLmTkzZ3yk2dhwjOyK7DGORjUhFH2/hTLlbWmvi700Wd0Y1m3MIdhFqLb4TTMh+dAKpHiDVJAwbVDC6KwG4vLUOvDM1HIsCZ2If9dtqtLzpc288jAK8UTL2sU8neh1rhq/R9Vtq/DcCn9qmwuju1D5P0Nc2iAqpehtV8JDWKa5QuDiCNHz4K5v5GQh3mCre/IByquwT36wP+HMn6I6qcsnhSWYV3KGjIU6T+lVGByIt42uth2PwHFTIiAa4ahZs1vvye9rUtihckHgQKbAR6iq98GzDZmlQcETwBQwk0OjnLupb4LLK42/249ntveC54nFGoHqgJtmITSomYtnDcpypVDJp/ktLN3dSH6rASoAn5CUWO/l7iFsg6whCIdvlfTq1YKNWqlr+AdLPbt7K4looSRQ2HRe5YvC8otRKJiAUvAE9Fgy8TbvoXiEdg4RNZbOnEN8xMCJpBsQShTB7CKNWh9DwvlBrnC3CZtK2Rq5K+IBKKaV1IisFiTpPkukGlVUOgL7RWAA3ZRtPAIQwZdolN+piJ0qjDzOxWMfT4EhMcqcL5l2SEMwvoekxVyVZMdtIWghRH1SanqBGWRkNLELO4N1MGEusP21viLjD4/aHkKH/A4/HzK8bbAA5micuLpL3jDf6y6Bg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490140 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1297C4167D for ; Wed, 13 Dec 2023 00:05:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69EE06B0399; Tue, 12 Dec 2023 19:05:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 64F936B039A; Tue, 12 Dec 2023 19:05:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4F11E6B039B; Tue, 12 Dec 2023 19:05:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3B2C36B0399 for ; Tue, 12 Dec 2023 19:05:54 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1363D16085A for ; Wed, 13 Dec 2023 00:05:54 +0000 (UTC) X-FDA: 81559851828.18.6FB977E Received: from smtp-fw-52004.amazon.com (smtp-fw-52004.amazon.com [52.119.213.154]) by imf07.hostedemail.com (Postfix) with ESMTP id D66B14002A for ; Wed, 13 Dec 2023 00:05:51 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=KL+PPGgS; spf=pass (imf07.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425952; a=rsa-sha256; cv=none; b=XSUZ8pgUOBFF1bQthbZRN5HxguyU19uKWQzQHJGuSu2lVUGIbCKapAtgZanLmdzYQwQTSf S2KYUjh7pSmm/uciaUljh10SZT/B3bzARLI34JMsxlLWHbtv50zTExIBwJcIYYoVoO8WFo XGWWilvlWZwJE09yDm7hH3eoHV4JU/I= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=KL+PPGgS; spf=pass (imf07.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.119.213.154 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425952; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=LESPtNw/smO8CIxZLa0BQCyFgAWbYWE2+aT46q12sNA=; b=JYKqVUJPPyNbnuWG9/9D2l5VOdYEwOGRGHgZFItWFBcIBi077RLzIJ+WsErev1YuR58h0t D8Upou1IsLXuhWtW+QbUwX33EXuR5GGbKHqlMcMrc7gYFRRXM/tPCsb7jLKPJbFpVHkr2v rrbgNLHt92mfbUTIIu+Cy7nPSosV0kA= 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: D66B14002A X-Stat-Signature: p7e8yc6do59ht5b14yfkswr47ypj4m8n X-Rspam-User: X-HE-Tag: 1702425951-646932 X-HE-Meta: U2FsdGVkX1+2UlWXpoPd8O57lLrNgQpGYdr9dtunAoVjjYJcBQV+TYoQSJcSfztj/8sDfzHo5tq+jz8Z0qZD6V4h+D07JqAo6rcV1+2kkQfe4y88wozhA1atdIWblMqcduCDZkuyhR+haBiXi/x0Nv7NZ2ASBRxe5NiFNkl23A5gLwYrD9EI+yWrZian4zZH7sXoirAkjozZ536R6eIOh82cytDdSRKMkSnqdIdyo1hAOJas1DL1LNI1jHrc4LCvrRONAmxk4yYeVCxZSPodPQXCOkcYZXx2JGyu6mxR+fC+h/cmsfnpDVtbGwJ83hzfVmEfn4iyZ61Am7Wxz0NDBv2rZiUd5sEohcH1NxsHseQrC/+gLgVDop/tTDvUSiOESHhmbVL1kNzCQWGasiejVUTFrCqwkbhWn+wuohsQEzBBngc0ImX+ZFm6HnRi7eqhJ23tcMyaDZ1ojl09KHOSQpsxI6754iZEMT9tnsodcMNxmezJMdc5Jn/Rsksw8f55Zz60NX0vt50ZE1LJUDLcd6/GZ783CrAdJBPw+6+DAhmw5U3g1heCvwlKARBKL/QbN+t4NfSoNWx+UJQrQ+KVlYpgYfAKmfk/4pU1rNmjpN8+7CEq9KQqXVbuKsgP+EbOcuYsi6qCxskHfxqj6+dgH4gp3gj5L4wuOPKkpR/x+BRhkrSsJV89gIK7RrASy83Begh74udh4z/+iDAdxnnSo82OL7OTtYcT4rBMeTPJweP6X19XjRnOS3NaTW/RDvNVpglPYSj3tMO1zNF0dRSucsA7Z7XkEPZ6VPYWdVNHU1Lbx+ddjQG/htdufN1WObl/JeaTMms206N9GM4BLR9zDryMO29RX8xyZCsU/Z6hkg9/qJXatNRQ6te8OsUsKMplqSA6vWDmky4JnX/Lpr5nr+SqJvnHhLpliR/GfA91j4GOcG+Vhs8z3iL/dP9HR789dzEvButoa/C9ZH+op+u Icx0wvgY +po6l28sOhmHwTlkUgv+LFRcv9ZnwOilZSb713V1lSsiqpLAlh+BSrmoCYMtDMngSMOrPtz67/j1dlchoQuHYecO6ROmB9QJ2i5UW1/vRZGmOkWOKmUwnUG0wgMOqbFx1nECP7uO0JHnqrfLgHuBhNN8dT5Vtb62Y+fS4BAcAzDl2B6T1lz9XvQeFNUijZdskEiADjUiHeAhdTNJTKFR8hMUldl/Of8FRALG13vXYi+TS/oUWG+gKcEo4sv/VRBBAYB+2GwxMyJo3nKL7mopaqSnPhmegLb+cCQK9EpoL/bMJHzf7gcjd/8QWxDoL8heRmrWHLYh3lYDvsqcSqsebR1dEnYrXD5dJ/0UHDf5Hlh4vmVJAmPH8T+FrvtmcIkk2z8pYEmj2aZuUr1UqJsu6QcRKUZBDNiikv/I5JQpBSIi4jAHodhHmB6khnBYL5PcwWnsvrmKV9bez1rM93Ji3FL9NmTMyVCxHtCMjLZrQWmoZELL34sqUJ/RJRCosHahbSAZrIYkjP0BpqXqzyeNOXB/5zYth/5oIlkFWW1MZO7G1cTcnHGJ8xdUoCI/Azd3IxkHlC9+9pyNpqy1PWPWmh9a3gvX0gyv/IzL4VPrpu1bUJEpUsZRAu2U8DNCZaV6NFg3zXs+Xhg9LkAQQ+NnbAQ1Ji0ui+WGrqtqbrLT0AB18YRa+R6vehY5b50qKYk5rc1XU5MS6vQHRe2u/1opfRYJ6UrA9qALtu1+7H/+C1BpWfYSqZIez9maFK7E4AMYZJ74KsIEaP9QrFcOY6+NKYtmPSWheJPmwRs4YJEFa0oJseHL2MrZlGw56Oy0VLAvfsDF2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490141 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F8BEC4167D for ; Wed, 13 Dec 2023 00:06:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E8466B03A1; Tue, 12 Dec 2023 19:06:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 16F316B03A2; Tue, 12 Dec 2023 19:06:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F06776B03A5; Tue, 12 Dec 2023 19:06:01 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id DBDF56B03A1 for ; Tue, 12 Dec 2023 19:06:01 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id A8FA180993 for ; Wed, 13 Dec 2023 00:06:01 +0000 (UTC) X-FDA: 81559852122.08.6A6E3E3 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf23.hostedemail.com (Postfix) with ESMTP id 5F56314001C for ; Wed, 13 Dec 2023 00:05:59 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=rE3qf8rZ; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf23.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425959; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ZFw7vHojuqK3RNa1/vBeZMhNA6TKCzlzgnpxEyXCDLU=; b=bC++FLEERj6+yKgAKcqmsrBEn73sn+3Mdp200Hp6ZBM8YMQ/f3pestC2W9o9sL3EjZaS0a vlISGHj8tBJYl3L447s/UQittfa0Cb+Kh0dzYFaYnJT6psnWKnVm9Sb3fGD9JpsJYlYBD+ 8AiyNFwzyhITsD7mHxxFwE0sLMsEdJw= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=rE3qf8rZ; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf23.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425959; a=rsa-sha256; cv=none; b=lU0eSlIr/+AwK56bmf3H730Ft2K5SU22th3hFXHsm4+nT1KIvOtbjccagSRXliBypET5u/ bPp3ZIbSRk37mBd5Y1HRlCRHaSCwR1u0HPdNW2XJwFTA5tX81WGSpxmJABXv8JhLoIBG+D wKyPda5h5WTBplArRMGlm8DCmHcJ7mw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425959; x=1733961959; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ZFw7vHojuqK3RNa1/vBeZMhNA6TKCzlzgnpxEyXCDLU=; b=rE3qf8rZ+jBbTGaXrrb61o5zMcLYv8fQIgkBte6IVPll8g/CpyivahOv lwG9RjtZJIDdLRxMBhanxFIL+HNHWEGjbvn9+Ok6SJWdQJjYO/0Xhb+kJ QHlyLfM8i+GoxZ+xYT3YkYgYAM1mmvNmQxiLl1gy+0MiemLNkTwmmTnWD E=; 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 5F56314001C X-Stat-Signature: g849p71umjstngcxpm3wa9ii57fxarf9 X-HE-Tag: 1702425959-398287 X-HE-Meta: U2FsdGVkX1+WOxBdOhbgmdrBo14tvybh5m87WwPOdKcvhVi03F8XwL3wJYYAj9EzJKE6pHlpf7tjtdrfJToqPGdaJ5c1ovECuel1BTmaR05EKb/xur7RYws1UebQj7twqjPRG/TUlF7rnXQSn8dTg+kt6TrS9u64bMsebHk3kO148hHtuc2m36hqrOvUsZBIdxwRGBXBbT/spEBgAS4WcxJ2FkYXDa1rI5lUM4yNxUddPiCTyrDyA+Y7AeO5qH3eEPlB3NF2iWnmKcYFIih61ubMuh/2SLogbucntch87mxzVYq7TkwLu9RQIpwiMH4v8H+fsLfRDmI/UlDbyOAqbDyPFLk+HV8YHXP7Fne5gh7jNf0/IraYkOBnQWYN1HWDrojFxXpllNyOkbTa/D/qjzLujcbEXC2bs8thK0V2Bd+jWJn8fJFVI5gVEWgzbbgmjbh5DrgX4SJuegbKhNSZS4w7bly53V8C1q9ThT+6cgaD/t0Yevz3UUF/qWItA7/e2zJQtWLY6vRaJ8e+x2VXPrt3bxeK6jv6dnAzISQJVCVaqz54lKyZNU/sA+/g+hFbRp8D2Nc60Vnj/ysMOBM59IeHVz9LILMOs3QKYRsE5ZBWg0IdxZum6193l8L0eL8p6IjlpJirjYy859S3qbvZbwQ0Yw7hk8tHdZ9SwCrTw03CpxYbF/J6E6+DuI4WXXqF9vIcGmUvtlhqZ97bWNaqchujGRbCpkbkUCWky8WIrZU6Ydclm6OLpoFYq2QoPDDi3euufR3ZtGj9VXYFamDXWFM7pwYmuCoadvnnvUsKsyLJCFwjNdn4yW+nLvZY2/uB3uNWdlobElazq7Khw0dvV/BgDCBANA9apWjUldN8H9dQA7JIIu9qdBpTvopHXqhm6NKcDhqetKhj26mKANEinaaefHKKda02TmL3zk/kdehU9PrOEaeGYgldytfLV4TkHrudLVX7wuGQuEBOjF7 JL+w+IT3 iUMx2O9BwSqiS/01KxsrEVoUnBeGz2yuP1hK0zvonueyNQ7/fDVWcodoab3CkMrVTt/QInx7or0k5alAYQ1IIK1OF0pK/JeXNO3C/lW+yxmVYnyJWWVkXdDK8hybBgjvhF0hZevW+V72vQzJ1BcblIB/Gph5NRzvmkoCyxNU0Xe73MSZREzLa9528J1gofcX+ycgfPnit39RKE9MqBqADyuspqka+CiZ0rppRLanNnhfelIGeTdVOuk/0NxyIgD1AhhLv8bxwLfdpUiYz9v9ZOagof1IP6q2bX0qJfcR+izJ/S/CTdkTpnsutZYf9exVTaqoz0q3ytRS0EWmo/j3szYrUH7VWiTWUOjhO2ggK2Vg2wI8tl9vWakylt3ugklfW2gf5GPc+PXOL+tRPLlmElpHIkfJy6MQ+putrzlVVgKi0/XiUy4MUUTVotnMNSRfCMpW3QbbB7Hk/QZwKQ2OC09u52YX+SLot4iWm30tfpgkGQumKJ1l6iZid0WbuCztdSVRyjqDMaalAkR5h6I8S+zm4wRtBoVakr/MtVDfa95fUq6WM7R30/qTlyPmjnPmrz7DJMu5M5IeZRqr1WIhJM8gIcoWcHYFYzCShLuLB2fsVw6L6rLIUOOqrEg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490142 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 78A59C4332F for ; Wed, 13 Dec 2023 00:06:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0D5136B03B4; Tue, 12 Dec 2023 19:06:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 084406B03B5; Tue, 12 Dec 2023 19:06:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E18066B03B7; Tue, 12 Dec 2023 19:06:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id D2CC56B03B4 for ; Tue, 12 Dec 2023 19:06:22 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 930DDC0B1B for ; Wed, 13 Dec 2023 00:06:22 +0000 (UTC) X-FDA: 81559853004.23.4701BD5 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by imf01.hostedemail.com (Postfix) with ESMTP id 6C4D340006 for ; Wed, 13 Dec 2023 00:06:20 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=fUfjJqzt; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf01.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.95.49.90 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425980; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=2/g7t8dqwJHCv4j3q+tOmI1oayO/LeEOR2vQQ38tfk8=; b=VPRY4xAw+asFQGIU879JGIBWfY4ZlUglu/X9eHPQLIthC7yjI1WQhPhZiYLf5bOsWmyK1N 7QRnRtRiKfwrqexW0VP9Zc/7GqYC0lwsMWm5wCXi43VHGkiztXKhajEMRmp+YJCY2IpO+d zvz/80dfEB0MuGfoVc5ldF/cBfh8xYI= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=fUfjJqzt; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf01.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.95.49.90 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425980; a=rsa-sha256; cv=none; b=cs5iZlq3k8GYumv3ua8ZojT8+FCliwOh7Z+JR4aX9P3DprXcS8wJp44Hvy4UXeY0HO+Pt8 G67ADlaxSWCEriI+ZfmnWIT/lI9IgUTdetFyeYe1JWaQsAKcJbj0G44NHoQGsRxyCrp8io gkNK5rS5Nm9wC7E1RiU7bIcFVK/5Jgg= 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 6C4D340006 X-Stat-Signature: u1n3imyj4wttooafyokn9j18hiuzutqr X-HE-Tag: 1702425980-145515 X-HE-Meta: U2FsdGVkX19KKHitw1Z1Jw73+uv+vXh56eZK5MwciDR5ARg02eFun0LcNuYXmYgT4ih5Gu5Rdoz+wNnN4apnxZ4waqfIRIbojVmg57YCIuTV5sTQ0wABLr4SERJ1Oo4J6pePtLytxNw0ej3AAOiGmtgvI9GoKIeQJFMZuarewIs5kld1+8FJvYEih4flHyVwbTjLiDqXA5PsLw2yEKIzXs+ocHMxqSTDTFI2u6T5cgYOhVPWf6+f8bm3ASU4OUFstnhA7cWnepVF/NJpYFWQoqI2E+ZXPE+AVbiRdv6PyEVpclKn3nHkCtf3e2BNaGxRZJAnEpjv7JAqggexdC9VmRyszDVqufJnRsAwDeItk/OnfmtDiE0MTzjkp5Lfwd0DhnC8MFw7kTCXO0H4fJ47GpJPULwtBjHrldkf/EvLTu3nLFY25ajkr09QM6IIp9Y160KbbTRv9Yqd6Vl/q0b4ZsnRE5sFFc5ZuqBc+bAj2+8istV7E5M7MmQkXOu/MXYfmEAjGqqAIorN4q0b3pudr+8FDK3ywGgopWUEPnTe8vPkwTVZXkczNLpSjUloYtCydx2BFl6rtOXhzHH4ZI3HLO/FXEvMIsV0sqyMC6mRtegwD94BnbLZR9fnpzhT3KNb2wJ6RTktxjZivzoypgV+RJmpcuWoaEC2uq6kQ9yR0I8h+W1BZk2xCTYaCvOJtRdZ4B1MIReQp4VBL9q/GmD41WoxI/5uP0I4PyLJxBgBB9MeVtxf4P+jEKy7eNR8Ywua04W1EoQ4ZsJgmeL30mp344bua1KzWZUnZDpj73z+gDRhuRb8DV372JidzkZwjILdwu5AM0riv93N3cYLZb6BkVg5cxky1W55fDlS1qzaamiPRLugg7P1ZMFbWTzVht/wWyBnmWOFkOfaqVUiXQlweBstDgRg7qjRNwYrqCsstT5VpVXSA6lGqzTeRKLhpvt6+lRv3qF38xY9W9Th3Lz TMAz/h9m kqUO92z9XDhivX4Wh52Vr7+512wfL1iaCrSyhzsDgiVOMNmIXAgarwxUh2BA/3vSne1oNCX7lYYu3Ch/hHI1mWpLmacUTBEv6Q9febNNYInH8096tPCJzSX9K088HNXFn3902FrRFH4yRirBq6zEP/P5DcnjEpbCzSrGnsYowwr7x2UPazp+WvYwkLHEs7PxwVSbXV3fqvxETxK1hTAViifUVDESB90g9P7d1GiZtWw/PRsYs8r5kDgChJNJsHbTpUy4VJEUbtfMxHrb7cKLe5FFhYS8YI4jxdUL4DmPqMzHNt+Ivpr+wUa2PuomTBI8U56DFO0UQDTSaFAwT7TZAMua51ria37bB+4wC+FQzaYQiju1UUhjY5LopyLU/RlH0VJv9q9gaI/iLh9wS8DoPKQinOOSxJT9mmYOn26gm7ELAMkbOnkAc9df4yUQJaDYbph1CgcpFodch8WFGDUWAUJMRbw5UmFG1iBZkwktkYH90AsibBjfNglUJ9YXgYt1nRiTWtBJSrNjYL3jL6MQw3N8THtYwjXWswgaOYiqeMXUrZJyBq/kK6zauQ/QehtKqCa+yXB+wbm5sM2otaUbMI5zVXiCMqHMEd8Qu1tAok420d8U= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490143 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4276DC4332F for ; Wed, 13 Dec 2023 00:06:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D2EAB6B03B8; Tue, 12 Dec 2023 19:06:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CDF816B03BB; Tue, 12 Dec 2023 19:06:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA73E6B03BD; Tue, 12 Dec 2023 19:06:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A9A806B03B8 for ; Tue, 12 Dec 2023 19:06:35 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 87CE9C08C7 for ; Wed, 13 Dec 2023 00:06:35 +0000 (UTC) X-FDA: 81559853550.12.F139261 Received: from smtp-fw-9105.amazon.com (smtp-fw-9105.amazon.com [207.171.188.204]) by imf24.hostedemail.com (Postfix) with ESMTP id 5821F180032 for ; Wed, 13 Dec 2023 00:06:32 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=rXMc1hXI; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf24.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425992; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Jz8kgyam6i59G9GfakNoGFINiG08t4Jr479SrWRohZo=; b=QwWHUDKIBUJOjvdQPf3gZhzcPRcPyRKEkcfaa5GIn9HipoxVLYJEZUpWTwD2ZLxNCE2TZP emOZ0PKQ/iRfcx5mWxSjPztpnN4veKbHDlKvcchq51/52EV+tHrQE/s4vjsc+8JuMOLO+W nw7tG2W8OsWKy+5iMrX8snP/31duLXk= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=rXMc1hXI; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf24.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.188.204 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425992; a=rsa-sha256; cv=none; b=YFcLw8Z6zo4l1ZjOQZBiRyAowftMczm4NQ4uSFcwg84rtFZNlx3CYXwOIz4AMNmmiZl96+ NoSRnDuF0+Ob8YdYYzifv3zHv6qK6Jrpy4WC69qDi3JEt9tsJJuasISLII/Obg4Q/FgD8g eSBfe2zTDjeg+mZ1of7v2KcmVfNxVDg= 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-Rspam-User: X-Stat-Signature: kdx37juykd6xstnj7ow8ha79i661cgr8 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5821F180032 X-HE-Tag: 1702425992-306700 X-HE-Meta: U2FsdGVkX19/y5vN166kw2RW0nANRH7e4IlIjq0fa1RPf46ugAwsxSkcZUDC9LAUgpZ0pjmFo/aAHJzPSP3oAkvlfgMoNJ+bSBux4xWCQBE7YuwSo1epO2dRIbZCe3zf8T5jPtTRWHWDXtRkUXtIIuFsvQrFaSolHRMSOC8AMcX66HHIQW2PR1sfAdLY0U5UFAOowsJkvEr1qk8mGCra4r+s91Gio5x+97SmBi5rxeQlAhZkRC7JCBesBap7SQUcJiSPs6mZraH7/yI+3YelQljrhJ5wKvTvSUSVl5ffdFdL0s39jblLAPH2tJCyu84xk3m84xS7zB+S0GF+YCDzjbcq8q7izk7LHm3JiipV3NIYx3y8osFj//8QxWT4ZO4JbhZdybeLw3wHomNNDl+8obq9mZ+E0gpJi4/kh0b64BYHFTXWiQ3C1wGn3Td8EvE8va4/rqE3QZ6Eq6zKCOhiDDId1RkOryE4dOsdmZYVx+BTlHLIGinNrsy4CSa+PL1keEqiReejWq97ujo5zABoYyGPjqUMUKB19dx9Hj02FKsmcyUrXLoDFQ/RBsDa5NvupZafFVssjEesPN/+Aucd/b0GI49RcEqb2Kep+CPMzihZzLwgkuzVNirBfloIvs3l2w8WV79KrX3X+fw+hDW6QVsHTrBQlkSU/P0nLUO6a+SIryEvO0lwBwCk2P5Fdt9EVqQzJ3Dq7k/A2+Zwb2iWIXvGBbrXZm/RElT3xSBri9Upd39o0J2/lG4XRCtLIDINzkzLA8ohR9MYrVeng/V/vbMKJgu0n9NCPfv6OLHuiwexjaoyqGovelX7X3MuwnHuWPazNONpLRUFfuNzU8y3ARDAgKP2KmVDcIorB5OyMZgPBHd8yOhbkheTgmft3DLIZAOOeMrW7//qwhCOgklwE7LIvvu3kt7EhF5a36MbHMeoE1lnNsIOnvywZzI4HONHItAx4Nz/8x0kuT/TF7Y F5wBUTVi Ta4ProXTTQ5HKqU5/qhSxdRGUkddQ/drl0fLFTZVx50RTmKSj08TI6gusdbnGNJTlBZXAHa2wLkkdc7yR0fyRrl4jNZB/Gs2GHZ4KW5+cahGQQgZy8BIBGxCuYpAvgdMHt8QGGbaNswVzWnS6VXm+BklExaiXOJmaRf0R1L991KNOwaaeLl3gR2kLFTbAsRcCMCFz4S7+RTgaexM+SRhBtZuDJeSxW8q5ryb57lJJ7pd5dkk5J0mQ/2zhsujTXOfpMAXgCzf9K5XtFvvE75qH2kCs9guyKdoEvov8EUTVPh155Q5pNFTHPqGd+FspAgbjy0Cgipg/4U6lQt2Q8bD8yJDOXf2MB8iivOEYl/CPrm9H13ca6KZwwwGdI/Pi5lv3teAKyq4vlour6dEvluPopqbzfmlzMHMEgffNlIOSkBy8rIXXohCj71u4uyJB6A0TmA3tMko2hIF6tKnzmJvsJQc7faOejBicCl41Mmbrrjw937y9RMNjzTcEcpH9o/8SYCeMyFLjpQs5l6haNrbuo51WzUG0GsYvMI88qICG6Hyiugy2kQe2wB8TnuutK9InKATY8pM69iJhrlzPGmuO5qgLn4gb7h0pBIGwAy9uybkeuCuiJsPf4ZN10Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490144 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 764A1C4167D for ; Wed, 13 Dec 2023 00:06:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9D58F6B03BB; Tue, 12 Dec 2023 19:06:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 910F46B03BD; Tue, 12 Dec 2023 19:06:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6C54D6B03BE; Tue, 12 Dec 2023 19:06:36 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5ECAD6B03BB for ; Tue, 12 Dec 2023 19:06:36 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 323C340704 for ; Wed, 13 Dec 2023 00:06:36 +0000 (UTC) X-FDA: 81559853592.03.88A7C31 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf19.hostedemail.com (Postfix) with ESMTP id 0BD611A001E for ; Wed, 13 Dec 2023 00:06:33 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="X/LCBsT9"; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf19.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702425994; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=IHxE/iueMjg5jk3J7QeMpZujdfbmXnsIes6SvH8utzQ=; b=CdihJxbh9rvfFfr2Nn//sHZpIMUGfsQeQxlJgKT17HALgps3kDy9FMveETzclhirAy1jNY 4GDD87AvdC2CgArztjWiX4NC9d8uMrweV2s1vsKZh66no/hpmqcC67kLxj42CTgUlOlJHt hPZnZAL6YD+PqK5wynx3fh2AHe1KOL8= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="X/LCBsT9"; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf19.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702425994; a=rsa-sha256; cv=none; b=Ius3cSMsyDCgGZknUczvtRP+WjaAXNEUfVAGTB5Ormr3+p7DasUt8WHTOTAtZVzicPVb5V fovAdsfijmvzoU0sGg4/jW9G/iccGrjnOpagMmigyOd+ArBdogaUaGC2v/WneQw6oM34oM TrZHZJvZsjDl1Np4mvRDG4i9QuYejWU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702425995; x=1733961995; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=IHxE/iueMjg5jk3J7QeMpZujdfbmXnsIes6SvH8utzQ=; b=X/LCBsT9TxNgfbo7kUbPdNi/YPILwe0/6w0qrNmm2NdyCfAvP+4a4Zx3 YixVxVrk/o2gmVMNE81u/UxLmR6TY8uoVKO5j/iILuYSWUi5u6nfd/iXZ NTgbUH/Htht3bRRnXoW2osOWJG0wY2MtinAis0G43Gv4t1XH0wyFiU9dY 4=; 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-Rspamd-Queue-Id: 0BD611A001E X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: ciz1y1f5kb5p6rjonsoeyh3d3zxgsk4t X-HE-Tag: 1702425993-652350 X-HE-Meta: U2FsdGVkX1/guZqqf6BQI53NlDVxQHmlKjXmrfy9vaRhLQOqHba6PMuUCDQdWzfm97wsB/+S3eRHeN9aUCjSU/ApjIW/H/yCveLWdd0s6GtKG2AMuDQRrBLTXrpYfo8HS2gfxvIKZCye+E0UurcV1yZ68L1C1IxUTzFzy/JfWVWbXLuN9N6sjnk8uzxe//LNeKscHVW9forWdUxxGrJDs6u/A64BdLzxUXRgrAgfcJISgwPOZ4PBAOjYfS3URhqz6ywGHY7pP/ozT7VUl5hsARGxp1ARul+/qrCc3IQmtluvbyVkE13Hwoj3dm29GXuLpy8gj4kY1f/Uos/F0FdiaseZ5PgL4cN3Q9hQC6Lu3ksz7cxfY+5L/q7z8WNl8CEDAxhxarQdI/JSCS6vfrNSsJ8eDato2/xgSvAvfLcTworfgkvFKtqDWv86M04CcpTJmdKeYi+K888dQZBkmdoe/dtIylm7FrGAaY8Zffyivgi0Z/D8Yj5iy/wJ8gTTHgzN+QreOORA9TpN3SNg4lGjQeosXVZ3swGLg7zjg4DdDHuHtnUXiCjw7OkOp3DMEvQGK6y/5UNeJCJEfZhGr4eWFYUw9f6/PMcfgf+jhDZSn0DhBqRPZx8tiJq7tSBWV2GCHDo6vKSHOMZVcr7RMtvMkkdZMLIKAlKbyub4HfqXiyKCTzjzR38EyDxcOl1W0NBp0+mHgazp9I/bfEWuVOjC/lre3Ig6ZdUZ//RYU9adzwQ01p9IHaqSojJ9zFPW3kifo4MIjOXaLPwkhN7+7Aoow9ecyPA8tyZr3Piwx2cRhzYoLBOwLALX+lcBIGQg2lodet4u7JUllU9hPLy9sGL00MUUv7h2AgYK7PPJieDK0zopznIt51GiRUCOYDhdf9LVcxeHAvJBrlDrMaUVYK9BqTCrBuSC/mGjTAt6j8iFsePtSU7k5Aes1tioaeuuyVTaDzCu0KOMMsEe0QW0PqG TH8iWgmX I6FGtNVuSu5gA3odh5IDPdEwUkiwy2iZwZLAvtXyBgwPxuak5OiChnsRi99y2Wiih/V13drNap3rIU7uB9jyBhtqfPBS1Tb3gyzmH96yoyrSFmKPb6LPRQye1Cjf3GTANHErke77themGxgBO625GZx9ap7JvqPN8nF8binyfm4ymBjemW2D/aC6Q096iXgpICrUK+9vjMpoeXoTL8J3eWPOLVp45QPKcDxCW9JEImdrc8HwFaFkL0RRQjmW0k8zuey9M9ZdfoqdfJjOM+OAhdf679PEGDmzxteSgwX5AEi75GAw5q4W6+SZraBKqs8roy1OuqZl1Vyqb7mtMimNijmNQg4+yjJfdWTXHVUstyzTX3uSdFb/2NikmeX5phSbxDh97/7q3qgNfOgzX+Wt3vcUj92h7hWk/5vwOf+z76SfauwbsyWQ+KKET0jSt3l/UwEm+c+Te/P0DFe91s+oXmGK4hdT6qiaJSsPbgH1spOiYsbpCIXm7N7AbCCSp90qB2uzcnRpSiSTKK7JCuhSvZC/OjBb2q5JLoW1jikdNPyhVDZx1L5R8/u5DSR2vf9CgAJpGFhB4m/Z3HgeYZzHj2QxMpY6Gxe0HOgM1XARC8nXptMA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490145 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEDCCC4332F for ; Wed, 13 Dec 2023 00:06:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47D8C8D000D; Tue, 12 Dec 2023 19:06:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 42CEF8D0009; Tue, 12 Dec 2023 19:06:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A78E8D000D; Tue, 12 Dec 2023 19:06:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1640F8D0009 for ; Tue, 12 Dec 2023 19:06:50 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id E438240A29 for ; Wed, 13 Dec 2023 00:06:49 +0000 (UTC) X-FDA: 81559854138.13.DF65F25 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf23.hostedemail.com (Postfix) with ESMTP id A51FE140018 for ; Wed, 13 Dec 2023 00:06:47 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=DkbR7zzf; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf23.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702426007; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W4f7NpctXKZH3cmvw+OkpzmI5P36spomZeCIdEWiFNg=; b=UBNUEfQ0fQNd8wv1AEASmbWF+pJjMpwAxzC2szTu6UWKLEaj9oHmj5VAbgozwjFKsRh6AY 46mCfq5Wkhyu4mxeoh1rrT3TvOAptELMyBKQrFwvsz0tm0kzi44YFo4aCkQM3J+BAiKV21 8YlJ+ZHMBSqUZ6yMchIwrJ7ceB9OTcA= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=DkbR7zzf; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf23.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702426007; a=rsa-sha256; cv=none; b=dJuUmE9GPT9IYi18PxD1EF3htOKXcLD8SYj169VwzoLdGK6XeneVPgfdOMALxB2Z2yPgYf VO2ifwVtzHdabuU5Laf7Au4/qP+tdJkxfW2CQ6qth+Ow08TMVR2gBL2cpNcG6kClZqrEnb jL7JuyBsQKPQAjN+mO+aQju87VnS5I0= 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-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: A51FE140018 X-Stat-Signature: yt6pjneirnga3jose98181npiawmgkm9 X-HE-Tag: 1702426007-516512 X-HE-Meta: U2FsdGVkX19IpH/HKtxlJ2KxPgkKfwfEmX/zqnZnvkaabTGUmzzA6rRbbUEddUdeQQbI4HdaYZ7bb4bMvldx84LflBe0W0or31uy9tm5JVY/kuh1WwTDYmLnA3KsuStybkZJQAMHccAeqS7TV0yIWeWknvdPOGipN3/z2xe1fmDSFkgPNQ2wuvtcWwg5nsVOFfbVk8gaP8Tw4M744y0PNvUs2Gf4nxfTixfmiFONXEF1YriRWbkLTToTQruoCs60G9ng3oPgqIzrP7wzB4F8MJnwK1m9sbcrNj8jwtqO01e1g1tfZIj7gP+OMMYlxiVd9OYFO3Df5mhjqL7b3zzUndn5sgCa+j3UWOuZB/wQ0638d2ZX1fL58m7a5vujkNLsb/H52pz21s8zVJ3vW8Al5ViMbBCi+rYFfmOhY3CCL+3GGFtnwNeCra9cVb3XucLQZrjfVJgQ8NpryRMRb4bD0LgnvkudysPhe/6oM9+w2n/3ieU+xWEf3vTkmdnf8okoZIt0wuaDC+1aaYRPCOEk/QFZpxZ8Eilb4WleYJnHSkhJMkD6Knndvv/EZyX3pCi0rDoMgNIdqjJV41FbS8h+SizNKXZvV67pO03tqGQIMo/FRxeQAcWayCq4BhbQ1qS3U6XPYK6Xiri8CndypI7iJBexDOBxazmgzFpipudQGmvTzZAg/Le4MP0rLXiC5pK8WjiPSlVNNSsiiynXOeolUlZPuOvCQg2lDt0/V2dZ126iQWhPnnjs/ATQFkqscrjcUzvP7FFu2miwIq6aWIOZ4Nn1Qc/KusimzQzwldiQ2pa6Cii1sLG8waTz991mqIDZf+rxa3UGkBe7NGvNSh3IjGNoMHpUcBZzRAn5OU+kXVRF3PkgfnuXK4iEd0ovJE38iitlyPAaWxSNx368Y4AvqePAt2OKkbzeGKOsgM4F5HRaXGAuHAQZRwHsBxK2I5ihPQCs2Ar442rM7y0/VVu 8hjak3KS 9qMaQ5enGU+hscTZ1ZW8nKHiBBpflwPCov96w7iznyjfHHtEwMfjFyLp9N0WS8UkFnwNqFxwX/CnCP5O/l93bgBY128UsXZm4Bi2Xc6c0AxU41nIRissVB7eKzcoeiHpjFznCjZZwXFJ3l1RnQKUgbPhE8pjgHGdurgUgkoxl8YEpt7l0fIRZv7mBCfs/1MhmrbdtHZmu9YYW5C82zzjwYpmfnb9/PbXIkDkzd7JO8+vsZqNXGlSND5k2oDhl5PTVCkvP0wTy40QV5IH9Y3O7YrP+hAxjD348rjGhaZZ3nN6YYo72CMskS592z5UPDhh7MZkoJARnPNZIAr0q8MznCTWIwcYuITosXs0c4ETWB/YZayhEIWcTF1XgjRMKnWruG+ypkCtzBE3KP3sy38saVchA9gFPsgamrvc46+ZW8v+GhhUjRZIEo4d+X0hd5CYF3WKuIPIQbZR0vyLH0YBMkpXf9monJCutGxpVUrhpEJERqkTYL6uIemwNTHKvEe85gHo9kj7xiuFGYarn0WejAEQ1WkP9Cnj3z6Hv378G3lfORNEirjdWSB6kbdNsYSJau+cLnflmdJnV0n8wV2OPoo3545eKOZbozP+pg2rvnDW809YuGEdEdacgaw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490146 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9874AC4332F for ; Wed, 13 Dec 2023 00:07:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2F6E06B03C5; Tue, 12 Dec 2023 19:07:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2AA506B03C6; Tue, 12 Dec 2023 19:07:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DB0F6B03C7; Tue, 12 Dec 2023 19:07:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EE25F6B03C5 for ; Tue, 12 Dec 2023 19:06:59 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id D011B40AB8 for ; Wed, 13 Dec 2023 00:06:59 +0000 (UTC) X-FDA: 81559854558.15.C307C34 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by imf29.hostedemail.com (Postfix) with ESMTP id A3B8E120020 for ; Wed, 13 Dec 2023 00:06:57 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=ZBkod8vn; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf29.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.218 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702426017; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=YG4rwMst//uzBl8lCmh1WsDiUmAxsOlA2EWBHH+q6Y0=; b=hCehv6Ad7eSojZSwtijpDw9K3Ou/11OeaPUXDROoCqygbntT39a3qi/UhrZGokAVUieBco /S5eccUpTJJNE8fhC9NFrPa6FQl299xL9ELS8e0vFkmhGMT6F9NgsicvaRMRH8v0q6aPxc JGHBXHXONArKFilndKCn3mvnq/jQwwA= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=ZBkod8vn; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf29.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.218 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702426017; a=rsa-sha256; cv=none; b=V+Z5D6E7O3WuUCcr8zXTU8cIZSpbY9rPUb/g0pw95UhYCi5LTmk4VnmIvlIJv2EGNPvGKl GkvvAzttXtjrSwYA8SpKZDUsntD9LNaUMsqVfXjX7Lh651TAKkk2gqt173y9VAg3fdUiP8 VIf530/lO6Xea7fKU5NnEHWI95dsREY= 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-Rspam-User: X-Stat-Signature: q7fpxmp1hw4bbfz7z1z7dgzhunq8nizg X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: A3B8E120020 X-HE-Tag: 1702426017-169650 X-HE-Meta: U2FsdGVkX184g81MydVckEBM/Om23TIQPvU87oqshj4KWHsmg6SZV2OGNr9QAiMhRNKvlV+nyE5H5B+zbzV6Wbmez7/D9wotLymD92d1c8ayazTzIyClqLaqIRfTpfbUfvXWcsXj9ToyURlLsX1wVnonzoHlkkJ3qYUHlpF3dR+y/iXUst80Olk0/jhnuaN3EKhv8JkIYAfRXyXIann8VRfMbmIpoN8yAtbN0rEqRpPDeCccEhXt99p6SovB9xkex2PeVTkRw495lGNciCNtvmSUipI0ePUbnuj0pJY8qlfQ3yts94Fs4B2Sz2ZcxdbEYN4le56iNlk1tbqt+Tv1N2FBAg/elu7VnSZYg/KSiIhMuzi93uL5rDmPXQGN4RZECnNke5FaCUU+X542r54jX/R5ODB9ktcIffH+AU8Q4c1y+oTRH1Z+Fa87E2MSfOM2ewcy4UKqFlm+ql3CfVsxV5UzpqZITmtHyiwpW6GXWN+LdIrW7G2kfRGz+0dGL1S+dfBR74H2VGJenJEM8A6h4do4H6ptlE+LqOilpop6+BNIUGuAlNOsT70fCUsOWdAkKtIVX6tGa1SCnRu7Kc+5sqAh4AV1cvOGmrZ5CsGSNtEWZJUIAbvQZQRtpnDcNBLLZ0BQTwRFKzDJbLCTA5mKEV+5wkQRZzsrU+ALOn824/+g+shP6KT3DIi0A2zp5VImXq1s6fYhN2DyekPcWXUdM9vpFhcs669JQfF+qEAzoondBstoA2Yws6SmH7RmkbTxxkMw1Y+Uw++a1nk+N1bpAeAuuCD0oK74MlAnnEF/LTosPWWxUkpZobQlU4/TvSFZScuhc/OBO5eSvLybdQ7mfUluslMG3G8P8dCWD5H23Htugklrs0WAqcMCibxiT42Z9J6GUL08vblC271SbdTuXpFtWcBl035a/S0TATGyZWqkDXSBYtwuJDnO57Gv5/ZpbCeW+k6VbURkBtUtxSz ItOZ0BvA Zm0Vi7nXCqi+Gs/RHPg+sPcA7mIird3BurGOnE1PKiYxOiq8zjw1UzaKbFF0Atrzytq/pP54o90O9o8kXk4kcVLDW1AVCOQqJJJ4/f+fGedOxPpMi7HgMNYYR8XW1g4YKFyY3xlCUIsILN95Lss2VUxGnzjxpmJI5ChINv7Rpd/m1HY5cgJQ882MOVKzRvCq13fsPwU//MzVJMymjBU6nNlY1qTJXyCxVNU233fI4jy/0TlqcpMBkd48Ox7hKUa/wbn4oxCKcRuDNU65i8VsKgqDDvYQuGvHAlbJhkGez6oDcYOw55AScAe5W38B/Wv6omaf/MEMaEYYiEV8h9tNcKJfOPdNzYSTMZYUnQmibNwAgD/+jE/JFeAvlIZOwcWsDgn6sA1bIM9N79gGUSHz4RZk+m8OAmyGc3CP+Cs2sGdgNC5uJKqoUiZk41d87oxizYLZrH7HY0e3h6U/hiyhwo//qtC2D2ghpQVkZTV3NzGDHKchJyHqT6MnlCTvq9pzThdSZd9c7VZeA78oEnHMWVsq/JEH6NfCh/Fz5mq6Ptre1c5ohdkQm4tS490XjFYJkirABy+IEc024A77xQ9o7qvHXfMnnwuViOLDd17jue1Bz3dOvOfghuTzby4tpjBDwEJ+W8/e5TJCHgrk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490147 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 977E6C4332F for ; Wed, 13 Dec 2023 00:07:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 282506B03CF; Tue, 12 Dec 2023 19:07:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 232D46B03D0; Tue, 12 Dec 2023 19:07:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 084886B03D1; Tue, 12 Dec 2023 19:07:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E91476B03CF for ; Tue, 12 Dec 2023 19:07:10 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BBD331A0AE1 for ; Wed, 13 Dec 2023 00:07:10 +0000 (UTC) X-FDA: 81559855020.06.2BA76A5 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by imf29.hostedemail.com (Postfix) with ESMTP id 49F4E120010 for ; Wed, 13 Dec 2023 00:07:08 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="cCnJ/mbJ"; spf=pass (imf29.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702426028; a=rsa-sha256; cv=none; b=BafebDMKx1y5MaRdIMPwY5TFMGVZFArdJ2gPhO8zjiadaVWn3NctxyND2XzRHwSQh0wz4E N12GYtptS9+LD9lyRQpeY6EDd29P/qG8oXxOx+wMw/fNK4yuKGmYTRGNxmeDemaO0LgfqD aL93SW3SKsDpFXT3qQKohkesSM1PYtQ= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="cCnJ/mbJ"; spf=pass (imf29.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702426028; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AEaNvGFNPvF0yeo/Uvj2gVs9WvSmpPsv4M/fAIzD1+w=; b=paV/rpPhI+Xv15NUMYNmkWjxMe1Y6IV5EHcmHyKEZ8zOrX12E4WP8YIBgRXio38kP94dt2 V9VTBu1oQhvtcoLV8ttpIQ/FwMokhbkA95LPCEE2ZB9yVtf344pYDtKXf/dUwElCddsvUX HeMTn6t/wvKmHzpoicT+YzJpxSDPO0Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426028; x=1733962028; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=AEaNvGFNPvF0yeo/Uvj2gVs9WvSmpPsv4M/fAIzD1+w=; b=cCnJ/mbJ0Bd3KbxOq+FVhWe3xliZxNBMEXEhuAmUn4RxX5DLe8JnWQj1 lXRKdrLjsx0Ff6bE4jocPA7CltRHRNJUurWCINbVVJG5UVsEoFCAgMrSO 4uMNgbtfVIGpZpy0apgw3h34GNAqu0K+lHhBiE8pEM9Y44XzS7jpFCo/+ 4=; 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 49F4E120010 X-Stat-Signature: o4qsjg63h84t5gxwp4kay3dq7i6xggmk X-Rspam-User: X-HE-Tag: 1702426028-233353 X-HE-Meta: U2FsdGVkX19YjfBORRuabwPRlcUrotUGxqO3kSlTC9e8FcK0WlCQ3Gy0GZPIe41bLFi0F/GOf2LKAwXHPCpHvC6EqXz7OmJUtbufmQVVpRbjOgbegauz/TtxBvLt7Bp4XuaKH26DqpmcyRn6Bc13X33Hh1eZcGNH6DVcFPfTkZpf5NOnDe2CWsGsqwA1HKf5Q3DAfIrO1a0rbVsl5Z9hwK5RNc8SXe80X+kPDMRNa9f3yqlb0/9+Ac6ZMm4AINf6sQKsxchjlfrbzhP9XeOE+RtufawHwIbx3idZSIQJxGoxf0cm+qaOi4O4dBRMlTha8AMh+0U/rwpTQb30n2twOvWBvqC6hTDze8ssPg5o8XPNnhu5rVe1rhwUKvzeKJhDUOI8qf6Eak39FOGN/BU4BqVxQ9jzwUB95bABxXBb/kJ9kZq8fPiDbXU9TqZ3n8k9fg97EH6lpUY25mdr9dx73sM9jPXc2yLB2Kf2vbmeQswFB2yilLw84ets1LQl3jpEZzy78zBzXSm0Yw0XF6EAdzMCjxmE0/1W+OCSyKlLTo7yhqVNBWNIMLTqhuGNy1nZdWUG14m3Jwv2RzB6S5Ye7huByiGA3D3Ob1eCke6OHL9aY3H7dY2ztI00j9THKFt8FAumU/Hr10WShxAIc6+9kWeC2kYH8i+/TgWkzuGePzt9WjHP36ubg8MFt8czLh/XFfa8QIzO77eeyIokbhauy0NAzLp1RQkXGpaib9mFUL2nV9G8b/LZM9YRQwI5MFirHi7nDUQOWX6F+GiPpVcljBg1sYr/uyIlzpThiXdFyQQj3EZtzKpOyYch0k9fyEx6CAoXjB4xWN4iUPmZlMFk1Sr+f83Ch4XsFhG+zYt3V0jHgzlpZyC933nbRMmKJ4x+hP6vBTf8pI/Eb+xCYyLZ6Y0DjPzbXVWZbaW5Gxz7fHANuCzRS87BtEPZytnmszeXIaxhIFqWyJnJtM3Cu6v HLvvRVj6 cv85PtPjpBDwkw0UgQHv2ZZMA+TiyKYRd9Cl8WRFGRyQpA5ze5n5nqt3++HHaocf6/nXMWvmsLJByKn1fjN801uYm4x+6UY7PFROhPgppkPScWSjWLXQ4Y1EOpVuXunQ0DZWFRF0zCR0OQv7Nd2WWBAxcQcHVHWysfLv+nnsu+/FAfqmX3GG+1H/3KG3O0pSZtONLs0ZtyhInDy/g/C+jnA8WqP2H6hWW+0fleShb+6t4tvfq0uM333yYQtDhJxaInt8xX0fAAttormAfgr5OFRSPBxtvjiV0auDKZpANYTfahwwvl1S4LExndnAbt0qfPkVS0NS53lmxkayrwZY2HJjdlmYrYc3bvKIEiwCp9kxB72U1S1XAGXjafS5z6sOelIIZmiNr/1vRFwX0rv0o2KLefEenRpO9/U5Qt3jZQu2Gw4tMdlP8kUJgqwRwu8EagDlK2lCe8oYLrZhOmh+u4JMpwo6hxnDNSrJ6L5hLSBHzFggCfMhO69CY0bPBJZ0gSa37vL+IjarwveuydG8Zq9McmnqTIlvysYnQZdMKbJ/Q8Tn7ZhGy4SnLYq9slQsE1g2MDeMYjiXWfcqUVnZVqLX4PAh03jWnmwn0gKpBmSKAI7R3lPgHVFN8q/RO+28bnU7oJEtAfXdsN0TM0reNCbE5GQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490148 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF51CC4332F for ; Wed, 13 Dec 2023 00:07:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4306C6B03F5; Tue, 12 Dec 2023 19:07:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E25A6B03F7; Tue, 12 Dec 2023 19:07:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 233496B03F6; Tue, 12 Dec 2023 19:07:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 0F0F86B03F4 for ; Tue, 12 Dec 2023 19:07:29 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E0BA31C145D for ; Wed, 13 Dec 2023 00:07:28 +0000 (UTC) X-FDA: 81559855776.28.ADAF642 Received: from smtp-fw-80007.amazon.com (smtp-fw-80007.amazon.com [99.78.197.218]) by imf17.hostedemail.com (Postfix) with ESMTP id A326240006 for ; Wed, 13 Dec 2023 00:07:26 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=OFgX5URF; spf=pass (imf17.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.218 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702426046; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DMVF+g90vjiGQ2+mH0UneD/R5uFZpo7f74QocXGx4Fo=; b=3OdJSaqhNiGcMYpqLKCOzvkEgZcQX+vf0LHTf7aKbKwgE7FA02QsMEEekIEezf7IWc7VRn K9nFF37F3JaQFKmWwZz8LqGw0jY+EnxMEJO9h4isLPOmwBXnb7c0z//BoFNG/TFL6qvNIF paNuliE1581hBm4OIk69ILNqm1wjxoY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702426046; a=rsa-sha256; cv=none; b=Zu6PKkbL649aSWmC4Y/3ico5CLyGjE20BzEzFq9n5pzAMb/zSMyfYFQB7NV7IaM+TmYTCD tWAdh7DzZnpLhOf4B0s38Twew3vR3TgB4XQskH9X5ShsWC50fXmwrqxi1MbbfstXwElpwm P9dhTH7C3B0N6VaAp5eUeUAvZfeqFBE= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=OFgX5URF; spf=pass (imf17.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.218 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com 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=DMVF+g90vjiGQ2+mH0UneD/R5uFZpo7f74QocXGx4Fo=; b=OFgX5URFuqPb5CBW19rJLjI3h/YZJwxiRlImzdUziEiBGCOJg/7TXYrd exiKw+A4RGoS500K8VZDV7m+Ym2k8ilksbVmxHgbCH4rCOx78fZNKD5on V4L+I0ReaW+DD++67by5KOS1SAg22uAKdBaPLathD/PsDEvei06pB4Wgo 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-Rspamd-Queue-Id: A326240006 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 54sa671puaskxobk1ei1hyn6841immkx X-HE-Tag: 1702426046-422422 X-HE-Meta: U2FsdGVkX19ZjDFymLwkWGAAslkszGZhbiy8y825RSM58Qeh5OHUEcMyvcI2tmKG0tvfTHVXekkgKQ9LUnNVwbljLnuLx8ywmMxu33hqVV5kkS5sbmXXRwSSkkrFG0XALClohu2VEkoL+l9hSL/hazr1rtoPJrJAfLGwxhV6PqX1hWijDZGOgjASllQii2vxM6S5VmHof8Hd4Ynz4bJ2D0VHQwFcBoTi2F1JmiGt1jOKsp6a4dZI4GGsJ++tFHUfd5KS4G9NMeVw4HL5x5rbXUM7cabtLOYh0xpQ1vflMxmJZ0Rjpzv8tl04ijoGiClgc9IupkEabfok2KXY3j6gJkzgmN2pM83D8QuRpjfxDOfGKrP8bfBmasFqzK5BrZZH1l2qJBgmKeKYTjarsfmjN852bBM23ui7v4fWnl3bXC67bVt4LiQoUS9uQT8kgeT63sihm/j02aaUkWLys556rjJFhtBvayCRORlNrxjNMr3zHiUQ5isaGeM+hKY1Lthoupfn8n1frzm2m7FRzR0FgKLioOpfh8er5E/ucFsQJcFcdPpOvkB2gph0MKzNT+WS+Of9ovNLAiQkdAxQFfhZDvhYdtoi0PHWw71sdzgYKiF+ClsQ3scYtQWiInx/z+5lfudWyrQOP5otlR4ZiJ++z3zYfG5K9zKRoG66dHQWuYgdQzm+7+VnEGdjIN52Xoub+++CRndDy/VRE80AcokWk1cV5YwAlvDPTje6lUeXMJOceP+XDWmLP3hEd5jMmM0Oniruxy2yY0ru7h78x1FxOA6xlQL3i/awG22bk59r+HsOCp8rXPlP4Kf+baGhh+mgst/Mz5FRHZ/SGnoUsFW7vsJU8DVUgDNbcNo1W0Q9PKiX5WnBOfx/qwB83TIZ9WGFUqHXDEGojUsQh3S9IURtZZAqtnzMaBiU7nol8K1Ighckj07D0tt+zZdwVuWeA5EuRgBbbN1tItuTH/CbZFR /05OwYWi q8fKZaOC2xKa5cjGgYoa7kYI5u0HonMvrdVVZERC7v0hgwlkFV+8jeLdiRStVvPec57agsjuLJ4CiwnKWOrlCz36sN4rKhfcF8pEDVxOCUeHyBvHBT4x88xBRHXwmsi8eOAV+vDAwGWeUztOCWRhpwypul/EOtC84HdBwZglwEP67W0bBaRUW7eBNlh+3JyrSdcOc2wL+Oqe1DYVBiT9VFG0SQ/blBmEHtIyxmcCPeSR0WBVNdSbvnhweQ/MuLgxECG9JkELzqNWeT0ZXsFRKPv8aKYdVHWkv7lEMCgDIE/tBA9DdoR7QSQa9X3JzM59cmhbI5NCRI+JaZmVP0W5cnde1UX6+tIzctrWaMROiU0/Lq5+iWP6WUYbBf0rPNLCmk6uDkXsFcSDbJ0tNDlM88hTRwFn9EJSaaiFFcSpoBrqRwbYn2HftSF7v1Y7OKQJhX/vX3IfjfdvGjyzNIkcdO+Jh/Cc07SYKzcEp+7tPkDCPnlOVUce/XjWqAR3YLugYgsKq0wLcQRUNgUNpfUiyHQPxgOAjTpBcMJ0/XjcXxheJasfUV+RAHVhYxktwi+EzUvKJ2NutboFM6hkk9I5+ySgxQT1Vx9odWmx55qquXaH2Ii59KuQ6aI/DIg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490149 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03C6CC4167D for ; Wed, 13 Dec 2023 00:07:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 829526B03F4; Tue, 12 Dec 2023 19:07:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7DCA96B03F8; Tue, 12 Dec 2023 19:07:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 511106B03F4; Tue, 12 Dec 2023 19:07:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2D77E6B03F4 for ; Tue, 12 Dec 2023 19:07:29 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id E9863120A90 for ; Wed, 13 Dec 2023 00:07:28 +0000 (UTC) X-FDA: 81559855776.28.9782235 Received: from smtp-fw-2101.amazon.com (smtp-fw-2101.amazon.com [72.21.196.25]) by imf18.hostedemail.com (Postfix) with ESMTP id DF5041C0026 for ; Wed, 13 Dec 2023 00:07:25 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=nJP9DAV+; spf=pass (imf18.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 72.21.196.25 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702426045; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=mc8BtMBoJccPwXhN5lfGmopEl26ZPwt65Ef0Tkxcd1Y=; b=h65RfaLbQYZEqLxEwf7wS5wF5wuaRKY8ckZtPjKbCtKSCTeOvy6cEG/dI9MK5zFbbC1HP+ D1qE3ynjdXI2BhrxJ9xlw25Ua5M2HY6ydn9HJDeVbsu4qIt/MhRcUTHgiMZy4jxbAc56Eq fh/9PfSIC4xtET9V6Wh+07K3zarsIus= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702426045; a=rsa-sha256; cv=none; b=FOUdXA7/1/fbUJsVdvcOfJRMG4IUI4oG+x62oF0WLc316bbqtny1qdiDxzCijfxgPd4FgE p0frum0rZW8YPsmXRWCpPbpLyC/nlJ1PsOh1E9YZR7yJxwpGv1KherVLGvfU/oVDr36tpm 7KlPOCHZpM0iVOO8QIuKR9xDOuZ1tbg= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=nJP9DAV+; spf=pass (imf18.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 72.21.196.25 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de"; dmarc=pass (policy=quarantine) header.from=amazon.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426046; x=1733962046; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=mc8BtMBoJccPwXhN5lfGmopEl26ZPwt65Ef0Tkxcd1Y=; b=nJP9DAV+PMYgo36zjEPVnQszs1x7x97mrNCJJx7xx16JpMT8DRxxcLmt tDqFYct2S9Hom/kiK7hVqLwY/o/DCg7FOXYBXNE1oNXr7WR1OdIEz6faS GQoxmWBycdZm8Rp6RyPsK29WCAzO/8u/tlvJcGh1LTweRgcC6tzlYmW0F o=; 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-Stat-Signature: 3f9m7u7ppetcucuu1s9sp1qerrwo9sm3 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: DF5041C0026 X-Rspam-User: X-HE-Tag: 1702426045-277304 X-HE-Meta: U2FsdGVkX18rY876b0ZSuYhCaTm5uihEZkCyGh7CV8jJoXc0XdPr6OVPffu9cF2FRMNb8gWCaEPas6eTG6lsj2F/WvtzH7RpW5iE9BUg1xQI9cMecgMtdi6hqQwJzbqcervCOAdodraDEXFPYGtYhwepSLgxbGyJbvUan6Djo9qjqHunRycOEaZf4ZyuWJ0wh8PtyE1qrAuuUl1oaxNokAJTYQHqFxXWnV2fGyxb1C1WzFV1I4NCrfm2aluXqRjKC/isDrlYgpwwLncO/JDOqYyHhWzZrGvG4tLU4y1GfV4wCIQvVUWvRWbTDkJEP3sNyJpZ+BMpq92M6hGEcD8Br5a+YUIyfgoDB6Elw/mOLdTx2R2si1jSCEJxEwtx4RJr3gsFwIf0NX3gwlNVwRaOT3Tuas5CNhStfAjySI5FHVuXbAWz6EF0TXUezGhkG2evFKz9ulEpTnEB6ZIrNcT5kFzk9ZNvj3BRsiy9lu3IUtbb5b82j9A5du3ZdLcJh8qMl7j3K8FNLvGaJZBQyapK1KjY7GCuh0wF8oyBO7ftqXStUnT3CRfv9xq6MlUOJMNfQdlPUPKKz9vjROMHj320LDs2n5Ac72O8hk3W86x2ssVBcYIEnSMo69TDsQx7nbCRCxZAKxVo/8vl1rhgDB1yeoBn2vzCHiuJ9zjHQKMVtmPoOQKABI2ptkVcnRnTK4mnYaoMEFpU5XowCXxlvNtNbBISl9pw/tpHG8dEihMYc9BPXZMsl2kyCl811LT99P+nCXAmrqLTsqRaZrl+vGNYSZtT36Ne77An7Sb4zZQDvPKY2txGsphd27k7n4XCsxA+Z8Fkrze4UDc6u7HRD8Gg953jOzlJRMbpScaEPI6H+77r6WYP6ubgvJvzun0zC1fbZJvwXDdf9bCl0+JLhBXYyTUqu7slfMBm2/I44PNjomSAzFrz5J986CiNrSTbnbA4MIaToD8VEAbjJyn0zHu UBLUSd/E pGqoojSqnnhRfo1M6VBrHVOOc8h2FD3aEeQjZtuzhJJWE00cqJnnMsfqGrbw6k2q8ybMxPY0aoVtZ9GT7IWpNRFpVm1dTby5ponRIfBfJKEJr2wML3pbmOW2VTqgLb9E/dlO2mu5D98TWVD5xq9mp9IY1BAOVFabrY251q4Px/mUnijj9W8+eDNGwDz0jJYTrlHk6hHFFR4kxhzU7aLimUUcodkJMVQwML2jH+J2BXnwNRdE0My5N/vRbAsMYxNgyGk0X3hEkDz0PB8ouY6PxH7f8V2VLZecxLTRdH+3zZuAcfAFTtHOQ9WxL/rbFgA5Jnt3typPijFjVXUSeepX0BEdliA5VmLDz1fT3wgBD2u8QTkkWKb9rN02slrs5OBrR2S2d X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490150 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 344F4C35274 for ; Wed, 13 Dec 2023 00:07:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C6D086B03F8; Tue, 12 Dec 2023 19:07:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C1D876B03F9; Tue, 12 Dec 2023 19:07:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A98986B03FA; Tue, 12 Dec 2023 19:07:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 948AA6B03F8 for ; Tue, 12 Dec 2023 19:07:33 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 5DC7F120A94 for ; Wed, 13 Dec 2023 00:07:33 +0000 (UTC) X-FDA: 81559855986.05.4F75946 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by imf29.hostedemail.com (Postfix) with ESMTP id 33C25120019 for ; Wed, 13 Dec 2023 00:07:31 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=Vd8MHtcd; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf29.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702426051; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ijxY/skruFfgxp/KO2yhFBX0a87YYNVgJHNp25q/K2g=; b=8pxq9ef7tj9vQV3Z22Yjd3cG9m5dGbYkmMqJ57AA3rap9KRm0djGg6q/ufTQURLLU6Zyuu sRVF5tX/qVa9BQb/iKW7yViqd4qnvXrqSRmZoLvHryRz+W+bOcDDB4IZHfEwE/zjrkIoV/ I4UUvVv7rZy4Yxxl6bEAl7xI4LRfUXk= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=Vd8MHtcd; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf29.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702426051; a=rsa-sha256; cv=none; b=IMT+yjrDU3MLh/X61etGfJccexSUPrqiH6EDAqw+0x4vyVKdcz7XOS6KWEDwAxgPg4c12z KvvwB6sWSJdQuqGjgBeEtNHnFa7abxfHktyPp6fbO4qEwH+UPPOY5DkZ0yQ2ZLGuDmIBQd 4wtBdJFhqArLZM4skp/SMkAIfJj9lOA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1702426051; x=1733962051; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ijxY/skruFfgxp/KO2yhFBX0a87YYNVgJHNp25q/K2g=; b=Vd8MHtcdaudKrb3RrBAHReqYpyUIwQIB4O6c9StD8QprE/Rvm/ivyBlQ dJC7gY1dinV1Y8B16b2MfPyGSpSkHDaPsRY9ITqmxjO4ghGqx7GH7KscC OtDNMKXCE+rFwYXqc0R6s+28ytDdZc9Yq32+pVXLSmPn2w+v/loJKdFJU c=; 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-Rspamd-Queue-Id: 33C25120019 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: aebozdo6xj7wrgjekcxibh9ez3ookhjy X-HE-Tag: 1702426051-16840 X-HE-Meta: U2FsdGVkX1/AHQdrUei1qa4mu+xe13paeA4ZrkUP/VwxYlof9OzXDz2kp+qfUzO1RpOiq+x21kCml4G0J+wmAowkjxKyF7i97VyFbD7onGORMXBwgjzuRGJ8qn+8a0Q4FJz8IMl5joDJjGY3YiVdOnJjWSdZ3HYvEqicBOMkv06BwNC5owE6WCLmAohbhpu+P+HpCt+s6U7Nm84uNt1Dx02JSdy5MppNidchFH6ICvcfaI6ABTaODTt9LzeZyxkO6Y4iEWebJVXW2XxxD0H0pTV/Yh1mz3rFipPstUMELHI4VUjYLQhBIALI6TmJw9MT0OJCMMxjT7yqbzKLUpOTrLzmPSIOrDFdZ9NnbORsW2DCrAkOR01ftulxd3ZEBhSj+GPKICeEEj3+4rFqIchzmC3soc2igaDFiJEL3Qm3zDyuYSQqj4P9XZ2WbAnA34sErMwshsa7oAJkDWJfPe9mojDWgq/YX1QA/cq7aieb8a9hiGmczvwK8lHnpm+2XlCAU+5XknW6T7PpI9yClNVIWISilCwZ7ShDy4nQ3Kstm4+AlXJhvZ+LMChtzA8A6LB7RtvtbmAwHe0SBcHZ+2G1xsXAoMAbzFftQwEb0UJDOLrjqESEHGqvjcinAfdw59BzVfj9/jl0ZR0egh1YX54TJEKWvXxGwntwjWUToq/81axe9jMjxKIrN9/wKLTuexrnrSa2NQs2W8Moj43Tp+6o2/Pxh/ECQD3Z1RUMBV9cIUMCJX5ltFIPznHJUsJkhtffLWUYvn5+H++Y6riUux+NjCZnAwdfAKN1dWequ+frNxxaveBpKlx0oGQhd2yzKugghXnniQVlrofwxvWOb0nZTZq/9sbMo5PjRqoCd+SEFlMYZpxMPfxhGZpx5UwChuU3KKqw64lqcep9n9ERwUshm4BpJg3CX7aUNPF46YNBv65CIchcjqhF7qB6rGgBGm1AXW/NDUNmXxWC+cmo/7a cLnr62Sl 1ncLYYIBGxHjyWWNSbv5QeuXebACVSgd42RU3VjZXSH2DwDICGTwnfMzl/DuAC+GebCt+GiGVGoaB7DNKTgpgj12pQtkoO5E0okhz4X2tMHEh7Vpy8JBLHFJdBGFpjw9MOx6qA5fFd0UJH+11C195qgl97zSAQB5Z2xBn2ocsb/vcB1BdzTn3mB3CtU2fFPwCPpPDInuc3WSOKt8LVw3BKR+VyH/htrOvPWIB1bJKEGwNjTCfveVvvPunNnnONZpsO2kfM0viEDkGYUC4ISxOaQuzX8Ynlq0QMLDt7IpjFqjYmUK3K7XycGyNjNRWfmNrfkXTYpWbIrAldmCxcI/6WH/t6y9vHFtdq2ECsVS/i1drN9OpdlEaBq7ax1FYYes3vZ8ERdD6mRtGQa95DQtGP/t/DTrz/MdRIZyIyBXj3pxsFZlrArc9+liUIF15r0JQIT2cATCda+xxvH546idor3UVDa8043y5d9LPieDHldkKr7hKH5/XzUPrPRZQcu3UhOudkxDDNaYJ86kllnkHGy7tFqxMV4HM89B/rB+bb04fhzA12Z3+lFXOpr+Ko7PXQDdd6PHROFh2JAlsgJ4whRjYp+yRzbOYoCzhKtvK+NKVacQgxxBqmTQAXDCuirJsLSHdDKKk/vRuww4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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: 13490182 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8EC88C35274 for ; Wed, 13 Dec 2023 00:08:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 03ACC8D000E; Tue, 12 Dec 2023 19:08:05 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F2C618D0009; Tue, 12 Dec 2023 19:08:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DCBB28D000E; Tue, 12 Dec 2023 19:08:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id C8DDD8D0009 for ; Tue, 12 Dec 2023 19:08:04 -0500 (EST) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A2D8DA20DE for ; Wed, 13 Dec 2023 00:08:04 +0000 (UTC) X-FDA: 81559857288.16.BE0AC9D Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by imf17.hostedemail.com (Postfix) with ESMTP id 699B34000E for ; Wed, 13 Dec 2023 00:08:02 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=a1vPSAn4; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf17.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1702426082; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8E5gYeBn7LzFXeE9t3+WWJhfPWwonViYFj973oiYUIk=; b=01Q2sa4uI0EXh+FQLFmByWeqnPwVgwMOXj1+9MgeYGM8gEK8zJzmfcSAqnOWu5R5j+YeEX cuZIifBy577XGtDDCTDkexM4QlFMux4+zp0/z5vUsdB6YWcTqWdSyI+Tc1xK7QQ8AE5VZI OZIkhVg+kJvNbi2YzltdzQdnHceit8k= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=a1vPSAn4; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf17.hostedemail.com: domain of "prvs=704f7accf=graf@amazon.de" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=704f7accf=graf@amazon.de" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1702426082; a=rsa-sha256; cv=none; b=IvG4SqXAtjjzPZmycyFZT5xh8QfbVb1j2a8cKPD+ZY1WncWCcpY5xkr5uYABB6hy1+izf7 UwOhNZlyu513+W2fXh3f/Qk9nYT2UiFsl9B9qqI8NiqfoskuHtqQYupfP17Os/z18WxCye KVjBx74Zvr2Kami2Si/SSKQC2nzP+cc= 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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 699B34000E X-Stat-Signature: w75a5egrsfdhe1q6w7xph15hxtp75rgp X-Rspam-User: X-HE-Tag: 1702426082-16033 X-HE-Meta: U2FsdGVkX18Fz28nLuNBBUPvFMcNmFTTTxzvBAj9MsRt6uNxnO0DRaskScPDpxFBpuk0FVxRdxciF+HQ3e2xaBJ1uZmYehN6do3FcXVQynLRglBt+wsdh+tiB7mq256cFVYccf6PkvKujo/pvjoar4IJAhkanzNRJwQGhtPgVnLKCjR73pdt46zQ+CVHr/dsLO0K4LuLRypoBhS/VcN1f8qraZpKcXl86K+9JDnVFbIU6bRF91A1PUePgQWeqMb+GrK6mr19wst9W/OqEuYOC/iPCkhgW99DD059SiWM32zd0sybxV7CfPkpyDdKNPCHzP7jSFVgJRQ+kuDU4C2ZLiOvmtgMYrptJDadoqxYWX3z2/4VhjpTDKANDlys1WlPSLDt7L+JtJI3WSZTe+nYuXofg27oyyB9b1FpznSxy+zgCZElOTTZ0VuWyTdBdpTBfcviVmgM3c+KGqoD8Pc661a6eaSVA8c/Gr+PFWnqR+3yDxx+U5EOoGrf6RlBb0DpJ2fgO7wr21iMHqflDY+RG/wX/ZNq6ij9d6jq2e5YhYkLsvmMkFMqJfhvHUZ4fTHHt2dATC2NmSCKoG4sYwDpkfTmP8lpPjjILzH03EJWNafMu18hUJ3XgCNHshraPgbaN8U5BEQV5KKZY84s4uDcB9KyNZL0gmJ9CrV6Wz/CCKiBmowqPQpX4Llfgnam4HfTXWsWr+3sOiQF0W8iKetQgXpUYMi6fiC6TveyKSEw5kxLu853Q3vZ51GSYUhzAiWxvUU324TSbtpAC7dcSEHaETsYlPfNCHf+er1jLHsPNHG09C+BRNT9ud46KYOFG9ScarWjdjjf3brrCPBgElzVUl22DN5YOBukoZ8FhQwYBkARxQ7xxu+VwvxQ0nT5YMCRyuywadyZ98tkYmwYeaI0XGo0INClk9iQMKMGIdKtWXihK7Ldk0HoOvE+NXPZ96tjdfoR+kSEvtyKzExySpB ZdIq9s8J nskcFzV6zjL91U5oJmeqOSIAgWFKdG95gkLi0wpAcN470IhAICTbY45WKhk5uMpAB6zoNrctiNZB3PUYtzC5kwVjAA95f4vGa+Bgnc1mMhFMLkCYEjR1WWH7+T7Fhe/WsJ9MNbe9Sfi79dlhOv5j9Iw6DVu+XztvVulowDt6Y7HX6z5CPJ4/WQRczTMzljrBA4Yc+DMXvOjW9WM0ThgW099ue49jJSm5tX+nczTtwRSOOGsXbGmFpkA3DrWPyoctK8es4ocubLxG2e5QczfdsqWXXatgr6cm7hFNPth++v3gurOxSBINBhPB6DkuHnCHJZ0kTwAN6j5USxYlE4pO4nGvIuDuiIZP3OYeni0Hynbv5kM9rr2efayC0TzSizAjujzX902CvPqmYxRuz5y6LRBATYC4EsyHOkJmGdLHyWm146fLsguycVonFHUqhYliFYeAmSfTg5Hn/8Hd1HWEItb65fUxr7lK+qw2ggY9mqgMN1pzhCxQSKOuxdbW5rnjsHB/JvN8c3M5EVrZ+eBh6DQ2wEi8U5ktqdvKuL73d0e54rcxdnx4BORy+ccsAvCGuOY/khyoPIyFNc/AmY+r7S1W5FdAjRINptWQLX9FNEOJKs3CcrDi8HsodHg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: 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