From patchwork Wed Jan 17 14:47:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Graf X-Patchwork-Id: 13521815 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 E4EDDC47258 for ; Wed, 17 Jan 2024 14:50:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 83B616B00F5; Wed, 17 Jan 2024 09:50:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7EC386B00F6; Wed, 17 Jan 2024 09:50:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 68D566B00F7; Wed, 17 Jan 2024 09:50:08 -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 580856B00F5 for ; Wed, 17 Jan 2024 09:50:08 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2E185120BCC for ; Wed, 17 Jan 2024 14:50:08 +0000 (UTC) X-FDA: 81689088096.13.375EAF1 Received: from smtp-fw-6002.amazon.com (smtp-fw-6002.amazon.com [52.95.49.90]) by imf13.hostedemail.com (Postfix) with ESMTP id 1AD2620010 for ; Wed, 17 Jan 2024 14:50:05 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="l3/Lz/l9"; spf=pass (imf13.hostedemail.com: domain of "prvs=7399cbc58=graf@amazon.de" designates 52.95.49.90 as permitted sender) smtp.mailfrom="prvs=7399cbc58=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=1705503006; 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=V7RkoctXcKrPHqjafH3kK9YvW0BnbS/G0GKRcf3+doU=; b=mfa2Hl/mlmLFfUGMKX4A8uSJjTCgfRXlnDnbtlzqtJUvVWlEaWPOaGJDhTbvsBZGLcyKXm waD/EegTUEugO67PFgiI6n/wtHkqi04KkUCtFUHaGnYXSG3N0R3bPQQOFMKE6Atu5zAfDS LsnM/6FbQ6dpiCsIA8w2BcSxp7fBn1U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705503006; a=rsa-sha256; cv=none; b=mi3rVEBJI7stnO8MeJGrMSkrz+2VD4wdvy5tcIOCCK7No6Zj9TpdMwAZGaOERJnP8T1DO6 IA2Wen88WAMaiaUTpcyds1nq2k4E+WiOhqoGU7MC5ECMdn38Ze7uFeg3+7oswFaEi/3TGI R9xxBGVQAtHYYh3XDPq5Y06mglO15EY= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b="l3/Lz/l9"; spf=pass (imf13.hostedemail.com: domain of "prvs=7399cbc58=graf@amazon.de" designates 52.95.49.90 as permitted sender) smtp.mailfrom="prvs=7399cbc58=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=1705503006; x=1737039006; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=V7RkoctXcKrPHqjafH3kK9YvW0BnbS/G0GKRcf3+doU=; b=l3/Lz/l9o2egQ1M+lRh589LVsnsi859F9jtjDq7IZkfSkKNKKKj1y5f4 v3wYWJhAjqvF3H+S4rMX7wtHpmSvExNFF11eVB20qHIgREiWsPxBrRcS0 1vE4aGM3AWVhRRe/xgDgrMZpgbLs1ct8VUYkWCnHsycc1EruHREPUsPAm I=; X-IronPort-AV: E=Sophos;i="6.05,200,1701129600"; d="scan'208";a="380369071" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-pdx-2a-m6i4x-8a14c045.us-west-2.amazon.com) ([10.43.8.6]) by smtp-border-fw-6002.iad6.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Jan 2024 14:50:01 +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-8a14c045.us-west-2.amazon.com (Postfix) with ESMTPS id 1F47E8A896; Wed, 17 Jan 2024 14:49:59 +0000 (UTC) Received: from EX19MTAUWC002.ant.amazon.com [10.0.7.35:26388] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.14.60:2525] with esmtp (Farcaster) id 20269b52-e38b-433c-9abe-1cc6bb154dc9; Wed, 17 Jan 2024 14:49:58 +0000 (UTC) X-Farcaster-Flow-ID: 20269b52-e38b-433c-9abe-1cc6bb154dc9 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, 17 Jan 2024 14:49:58 +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, 17 Jan 2024 14:49:54 +0000 From: Alexander Graf To: CC: , , , , , , , Eric Biederman , "H . Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Steven Rostedt , Andrew Morton , Mark Rutland , "Tom Lendacky" , Ashish Kalra , James Gowans , Stanislav Kinsburskii , , , , Anthony Yznaga , Usama Arif , David Woodhouse , Benjamin Herrenschmidt , Rob Herring , Krzysztof Kozlowski Subject: [PATCH v3 15/17] tracing: Recover trace events from kexec handover Date: Wed, 17 Jan 2024 14:47:02 +0000 Message-ID: <20240117144704.602-16-graf@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240117144704.602-1-graf@amazon.com> References: <20240117144704.602-1-graf@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.253.83.51] X-ClientProxiedBy: EX19D041UWB004.ant.amazon.com (10.13.139.143) To EX19D020UWC004.ant.amazon.com (10.13.138.149) X-Rspamd-Queue-Id: 1AD2620010 X-Rspam-User: X-Stat-Signature: jdg5engai456rsphibhzq6epxdw7nnts X-Rspamd-Server: rspam03 X-HE-Tag: 1705503005-70460 X-HE-Meta: U2FsdGVkX1+3Lxex1x5wT3JsSt0vH8lxF6mCDncsZyDOpqnrvOHDGiZNybwmD9A9666nfhzMCA1yuUZLLjqIo32DpE6iIWBhzS9PsFtQBztv49+UUff5C11/nt6fbAd2ucko+ejCrqjpIvCyaHVSDIXcX1XJ+TtiSeJmUTfm5D5NqFch6kgDkMl8dkfpu0dRbfLsYRCtcJoM7FdmhRE7sUQyywaMizgYbtm0cSrAWHC9j1hdTmoIlw1ih5l10EjfrIheJJ8jgFNdEJ13SqA2wRdvyN6Lt0IRfoM9JGyg423FEPFAHV38es+5BpE0TYxLQ7aqCgBwBK1EJO+kMDQYUdDdQSrMLzoLZn/VF+IUZVacYsQeFrc+QAbrOKwcsWKraXRHFf1AOfOvmSXHBt9Ejd7P8lqPJNWQSNJkBhyM9HEeDbOyOWcQ7OWMJBwCLcGp7o7uC+0veYLj+xZluwwlCYZOvTN3C/5PwoaywUmqBMoX1vpVpmSNqL0w803KpkowxOz9cd1E6HZUfYmaHKyXLvBFAyuABJGrcYc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Qm307WJs= 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 --- v1 -> v2: - make kho_get_fdt() const - Get events as array from a property, use fingerprint instead of names to identify events - Remove ifdefs --- kernel/trace/trace_output.c | 158 +++++++++++++++++++++++++++++++++++- 1 file changed, 156 insertions(+), 2 deletions(-) diff --git a/kernel/trace/trace_output.c b/kernel/trace/trace_output.c index 7d8815352e20..937002a204e1 100644 --- a/kernel/trace/trace_output.c +++ b/kernel/trace/trace_output.c @@ -24,6 +24,8 @@ DECLARE_RWSEM(trace_event_sem); static struct hlist_head event_hash[EVENT_HASHSIZE] __read_mostly; +static bool trace_is_kho_event(int type); + enum print_line_t trace_print_bputs_msg_only(struct trace_iterator *iter) { struct trace_seq *s = &iter->seq; @@ -784,7 +786,7 @@ static DEFINE_IDA(trace_event_ida); static void free_trace_event_type(int type) { - if (type >= __TRACE_LAST_TYPE) + if (type >= __TRACE_LAST_TYPE && !trace_is_kho_event(type)) ida_free(&trace_event_ida, type); } @@ -810,6 +812,156 @@ void trace_event_read_unlock(void) up_read(&trace_event_sem); } + +/** + * trace_kho_get_map - Return the KHO event map + * @pmap: Pointer to a trace map array. Will be filled on success. + * @plen: Pointer to the length of the map. Will be filled on success. + * @unallocated: True if the event does not have an ID yet + * + * Event types are semi-dynamically generated. To ensure that + * their identifiers match before and after kexec with KHO, + * we store an event map in the KHO DT. Whenever we need the + * map, this function provides it. + * + * The first time we request a map, it also walks through it and + * reserves all identifiers so later event registration has find their + * identifier already reserved. + */ +static int trace_kho_get_map(const struct trace_event_map **pmap, int *plen, + bool unallocated) +{ + static const struct trace_event_map *event_map; + static int event_map_len; + static bool event_map_reserved; + const struct trace_event_map *map = NULL; + const void *fdt = kho_get_fdt(); + const char *path = "/ftrace"; + int off, err, len = 0; + int i; + + if (!IS_ENABLED(CONFIG_FTRACE_KHO) || !fdt) + return -EINVAL; + + if (event_map) { + map = event_map; + len = event_map_len; + } + + if (!map) { + off = fdt_path_offset(fdt, path); + + if (off < 0) { + pr_debug("Could not find '%s' in DT", path); + return -EINVAL; + } + + err = fdt_node_check_compatible(fdt, off, "ftrace-v1"); + if (err) { + pr_warn("Node '%s' has invalid compatible", path); + return -EINVAL; + } + + map = fdt_getprop(fdt, off, "events", &len); + if (!map) + return -EINVAL; + + event_map = map; + event_map_len = len; + } + + if (unallocated && !event_map_reserved) { + /* + * Reserve all IDs in our IDA. We only have a working IDA later + * in boot, so restrict it to when we allocate a dynamic type id + * for an event. + */ + for (i = 0; i < len; i += sizeof(*map)) { + const struct trace_event_map *imap = (void *)map + i; + + if (imap->type < __TRACE_LAST_TYPE) + continue; + if (ida_alloc_range(&trace_event_ida, imap->type, imap->type, + GFP_KERNEL) != imap->type) { + pr_warn("Unable to reserve id %d", imap->type); + return -EINVAL; + } + } + + event_map_reserved = true; + } + + *pmap = map; + *plen = len; + + return 0; +} + +/** + * trace_is_kho_event - returns true if the event type is KHO reserved + * @event: the event type to enumerate + * + * With KHO, we reserve all previous kernel's trace event types in the + * KHO DT. Then, when we allocate a type, we just reuse the previous + * kernel's value. However, that means we have to keep these type identifiers + * reserved across the lifetime of the system, because we may get a new event + * that matches the old kernel's event fingerprint. This function is a small + * helper that allows us to check whether a type ID is in use by KHO. + */ +static bool trace_is_kho_event(int type) +{ + const struct trace_event_map *map = NULL; + int len, i; + + if (trace_kho_get_map(&map, &len, false)) + return false; + + if (!map) + return false; + + for (i = 0; i < len; i += sizeof(*map), map++) + if (map->type == type) + return true; + + return false; +} + +/** + * trace_kho_fill_event_type - restore event type info from KHO + * @event: the event 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 fingerprint - either their predetermined + * type or their crc32 value - and fill in the respective type + * information if we booted with KHO. + */ +static bool trace_kho_fill_event_type(struct trace_event *event) +{ + const struct trace_event_map *map = NULL; + int len = 0, i; + u32 crc32; + + if (trace_kho_get_map(&map, &len, !event->type)) + return false; + + crc32 = event2fp(event); + + for (i = 0; i < len; i += sizeof(*map), map++) { + if (map->crc32 == crc32) { + if (!map->type) + return false; + + event->type = map->type; + return true; + } + } + + pr_debug("Could not find event"); + + return false; +} + /** * register_trace_event - register output for an event type * @event: the event type to register @@ -838,7 +990,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 id=%d", event->type); + } else if (!event->type) { event->type = alloc_trace_event_type(); if (!event->type) goto out;