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/kJ9kZq8fPiDbXU9TqZ3n8k9fg97EH6lpUY25mdr9dx73sM9jPXc2yLB2Kf2vbmeQswFB2yilLw84ets1LQl3jpEZzy78zBzXSm0Yw0XF6EAdzMCjxm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SKAI7R3lPgHVFN8q/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) {