Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp3317488rdb; Sun, 10 Dec 2023 00:09:19 -0800 (PST) X-Google-Smtp-Source: AGHT+IFVjwXPB+Ue5wETupXebSDb4lgSRAQvZXvm0H0vmajbqACKOtRdjXZUIzZUMr4JJJKXhy5o X-Received: by 2002:a17:90a:bc83:b0:286:6cc1:3f21 with SMTP id x3-20020a17090abc8300b002866cc13f21mr1924103pjr.88.1702195759446; Sun, 10 Dec 2023 00:09:19 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702195759; cv=pass; d=google.com; s=arc-20160816; b=hNA3hHZGUDE7Yoj30qbHlMUyLs0XlhQjB7LhC8hX3+2GIXixfcSSFsk34AieKwfd/y cNGu/FmIYHFjJ1Xy6H/BRRLYDcmzK/Qc82FLQOU0at/CbGpJvMra817RTZ4EicGyFs/p x9EecgK9iUMbJmi5kRIMqDBYrbIZTL6NTpeRb76JAq9ksuxU2L1+uHiPskUzALa921a1 FSSaUo/x6LHMXVyFwSPTmhcOKTvcGqAWCD1bwSM/9yyejx2lfXEEWDx4oHry1OtEhtTb U3QvIxXaOruAASqFgN+Ljxb2M7fVONngl6CTFHvg6xBanhRuHN9gSPsihTlGaPlRlgvt VHWA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :message-id:date:subject:cc:to:from:dkim-signature; bh=fzAcBBC3/Qh5JHV4M83f1kBaLFDSVNfSmRZxxUCxyog=; fh=HIpGW3XNt20YYDdrqGWTdutDMTXUX2EnMhGwR8EwV4c=; b=DRYzO5ugjlNhqbio7KdnCLvyDl/64XP4i4PtycJc1mzXl+0VtPsBsh2U3D8GP09B0i TwS7X1nh5bcSQhmkul6sXz5Hes5sWhAbpc7vBD9saA0i3QyJ210AslDY+RN1HlUffGEC RAzeeZfyeQglLeQR48qCOrxTALOjHnCAb6HzUxjkqXV+d7V2Rgt1ksmS9tMp0qRgHcZD Skfm9O9HD6RgDOGtaKq9KTLpW96sGDtdtwf7YQlc/7V2wOWioaOgmblM2B/nTVlQ5xyd JTcO2eh0xN5bRgrq6O3KuuXIFGk5nbu+I/uMUo3KRSMpp8SZv+L7bp2cD20h1/I7opFe XKrg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=OZ8aMSRP; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Return-Path: Received: from pete.vger.email (pete.vger.email. [2620:137:e000::3:6]) by mx.google.com with ESMTPS id v6-20020a17090ac90600b00286593b0471si5417228pjt.120.2023.12.10.00.09.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 00:09:19 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) client-ip=2620:137:e000::3:6; Authentication-Results: mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=OZ8aMSRP; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:6 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by pete.vger.email (Postfix) with ESMTP id 6F2268051199; Sun, 10 Dec 2023 00:09:16 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231642AbjLJIIx (ORCPT + 99 others); Sun, 10 Dec 2023 03:08:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjLJIIu (ORCPT ); Sun, 10 Dec 2023 03:08:50 -0500 Received: from AUS01-SY4-obe.outbound.protection.outlook.com (mail-sy4aus01olkn2150.outbound.protection.outlook.com [40.92.62.150]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED3369D; Sun, 10 Dec 2023 00:08:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ivq+NRXjxGDwnKnLRZGXu/whyyqDohuGgc9DHRz/N+8kdS2qwgRx6tXeYiUZsCP4CspfnSLaytb2crBkUMURY84KRfHktzBUlyKtRp2axx7moGbpr/I2+AYNceeMpEw17O7qzSkn+JETGbJHVCcs+Hg6OfAnUIMGYAyYQ09zpQeJtigxpjf/Rc/Fuxxbg90ckylqyMxUsdotknmvOsFJBhH0MdLj/dFlfj8r/fqv8gjqf92aL3GBh7IQgYA3rRa4PW7Gf+BGQdhBFYUoZmzgQahTeN0+Pc10IDGKCikp/uiYsfO735bN0MhhsuJHv4XB5WjQPpUIJBI7GFdAtIKxBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=fzAcBBC3/Qh5JHV4M83f1kBaLFDSVNfSmRZxxUCxyog=; b=efli0bupdNhVgqjnwi18WmWOk8a5DPWcd1w1edMLvYh0Ujm2yxD5juHOK30nk0OJRRMvxmNPtfxXggYFbV0r6QrhsD6vMHO0wQs58Us8EE+K2t41B1LPBQxr9ok6T1QRYtbs3wzsJoNDjQde4QgA2xnm5HY6yMwaHDYGf9KGqQJfF/B64TQ0m8St6VA+lFToi3KwhJNiyZfzqwARMC3uMq1TXi9t7FIkXNKEjOHRpKQa5sym1blKtFciyHO7TXhwD4YsEDAiYegaM5eZWXOyRoqvrYX1Qc/YwajyuDUafv/NTWjc0xX+UBYmDC3ONBU4AdmHoqEV3ZDeZHJO0L2btw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fzAcBBC3/Qh5JHV4M83f1kBaLFDSVNfSmRZxxUCxyog=; b=OZ8aMSRPVUpn99S1lKPRGQWg7meUP76wavfBqFXDlh381lzEeFAA4hgb1JlOj46LcyizVdNsJp7o1qz+BECP7P7qMFPWNhD+/S3OMTAw8R8ff+PNIf1NsawHs5/22gssjQ5JB21vvdnsgTxXPSwS/N5oqNd4mBprA5hxDLZKgMoi1WVDPrlqxMJmw78mUyMzl7gRJB+cHpk+hz9DGWsQDxLF3dr4fPOHbQzeuMO/4zt8T+SJVXjuCrbPfjaM8rRTePfYfd5A56Js8mb3qwknKxT9x08We0j/R1xMpgKRHKKU1NUKLAEWBFOFHjTMwiJs/1z9jm77b4q+e4wF8Igw5g== Received: from SYBPR01MB6870.ausprd01.prod.outlook.com (2603:10c6:10:13d::10) by MEYPR01MB7339.ausprd01.prod.outlook.com (2603:10c6:220:15f::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7068.31; Sun, 10 Dec 2023 08:08:49 +0000 Received: from SYBPR01MB6870.ausprd01.prod.outlook.com ([fe80::be5a:160f:5418:9deb]) by SYBPR01MB6870.ausprd01.prod.outlook.com ([fe80::be5a:160f:5418:9deb%5]) with mapi id 15.20.7068.030; Sun, 10 Dec 2023 08:08:49 +0000 From: Tianyi Liu To: seanjc@google.com, pbonzini@redhat.com, peterz@infradead.org, mingo@redhat.com, acme@kernel.org Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, x86@kernel.org, mark.rutland@arm.com, mlevitsk@redhat.com, maz@kernel.org, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com, Tianyi Liu Subject: [PATCH v3 0/5] perf: KVM: Enable callchains for guests Date: Sun, 10 Dec 2023 16:07:42 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-TMN: [5hP5SFsKOSJHZzIDoL6ixfHLgGiQ2S0WroETPYou9oYzvUMOQ2+dyQ==] X-ClientProxiedBy: SG2PR01CA0189.apcprd01.prod.exchangelabs.com (2603:1096:4:189::15) To SYBPR01MB6870.ausprd01.prod.outlook.com (2603:10c6:10:13d::10) X-Microsoft-Original-Message-ID: <20231210080742.2135-1-i.pear@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SYBPR01MB6870:EE_|MEYPR01MB7339:EE_ X-MS-Office365-Filtering-Correlation-Id: bfc982f2-5ffa-4a9b-075e-08dbf9573bf5 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Z1xFtd/ebhKIquqP+AniVhqEOR5bQv6zvqqrZrQEiWkLDkudW76b3dS0SsnxRSN4Hd8/rd9kqY3hrraBts6vKj04KqNo1hYV8pt49wRx9UJ2Yyxd0xnh+PZP/VSwsJvaPxcUt9UcmNssbrvBX8VfNAqu3LuV1udXL0lel3fYGlYedfgqj8GCUZE8haVvHkUPyY/h4By/Mxyy49T4TH5tscrKw6IH5tryo+aO+ebXHm3Dd4oU6//THlfmYcKbDdvnizO4YspBPoDff2v1X5X0sR8yDay3cIRpBHv+u2ULBpCHc9iCd0IXGF1F8mK8fd9HB7f7/P25onZ6kRybCEogoJE5NeEkkZUEqeNqW9jUIltjaAQomi7ajZxBSM6jg5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ou0t57OSN3NVmqT2Hnfog8e?= =?us-ascii?Q?Zbo/vivWQci28E0XrdgkN08wE1iG6eoXw5P5aCtR/fZ6QK6JUdGtr855GTJl?= =?us-ascii?Q?ypoE4E/rzeL3S3hqfYZUIor72gka/MZX7ZXIgQCO/XsrFsiH45iuhUNblnVP?= =?us-ascii?Q?ynTxRipAPY24Gd3p4oVy7TXWU3dsjF+hJfcGHRzeYlvz8YrpKTn03ckQj5T0?= =?us-ascii?Q?ZING7SVnvvLLps0Ev2mF26fbjggEN77COpzpJUF8NYwmu/TDvh8/qDBFfw+o?= =?us-ascii?Q?yIklzw48/pcz8vhdxOrAKvNsqf6U+5uDwhHv4f3YsUCBFDoIoovB3AhQpbrT?= =?us-ascii?Q?HQQ2bbpKV2mNIrPlokdenJY2owPzxSObNz+xR1vgPuKeeHI0ErQgQR0fPt5p?= =?us-ascii?Q?NB3AbBVkWk7IaplOlTy1/XOLJ1pPRiyLJ5MQBHtIfop/TvoXlB90FYKzjgDL?= =?us-ascii?Q?QpM3ebECAkJZuXPzCGMzGdrTDoCk4JEY30IqZqwyozUtp41+tyMyWLrcM0SK?= =?us-ascii?Q?+ZNIqdWfIsPtK1fQV0LU9MwG5YjaMWGgPrzm8tzCLx9VWXs+a2f5sewXGKsl?= =?us-ascii?Q?qVtlsRsTsIo09W2kRsYSz2sBsfwfQ0xd7JV+q+oo8guvIW4P32EoXpV3w8WY?= =?us-ascii?Q?FyLx7qZK9JDMdPO52Rpy6s+dCbwH7mA29J+IbV/T8+Ew4Hidw2jvPCBLIICB?= =?us-ascii?Q?7/gIWUfqxvcum83cbRLz?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: bfc982f2-5ffa-4a9b-075e-08dbf9573bf5 X-MS-Exchange-CrossTenant-AuthSource: SYBPR01MB6870.ausprd01.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 10 Dec 2023 08:08:48.8734 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: MEYPR01MB7339 X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on pete.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (pete.vger.email [0.0.0.0]); Sun, 10 Dec 2023 00:09:16 -0800 (PST) This series of patches enables callchains for guests (used by `perf kvm`), which holds the top spot on the perf wiki TODO list [1]. This allows users to perform guest OS callchain or performance analysis from external using PMU events. This is also useful for guests like unikernels that lack performance event subsystems. The event processing flow is as follows (shown as backtrace): @0 kvm_arch_vcpu_get_unwind_info / kvm_arch_vcpu_read_virt (per arch impl) @1 kvm_guest_get_unwind_info / kvm_guest_read_virt @2 perf_guest_get_unwind_info / perf_guest_read_virt @3 perf_callchain_guest @4 get_perf_callchain @5 perf_callchain Between @0 and @1 is the interface between KVM and the arch-specific impl, while between @1 and @2 is the interface between Perf and KVM. The 1st patch implements @0. The 2nd patch extends interfaces between @1 and @2, while the 3rd patch implements @1. The 4th patch implements @3 and modifies @4 @5. The last patch is for userspace tools. Since arm64 hasn't provided some foundational infrastructure (interface for reading from a virtual address of guest), the arm64 implementation is stubbed for now because it's a bit complex, and will be implemented later. For safety, guests are designed to be read-only in this feature, and we will never inject page faults into the guests, ensuring that the guests are not interfered by profiling. In extremely rare cases, if the guest is modifying the page table, there is a possibility of reading incorrect data. Additionally, if certain programs running in the guest OS do not support frame pointers, it may also result in some erroneous data. These erroneous data will eventually appear as `[unknown]` entries in the report. It is sufficient as long as most of the records are correct for profiling. Regarding the necessity of implementing in the kernel: Indeed, we could implement this in userspace and access the guest vm through the KVM APIs, to interrupt the guest and perform unwinding. However, this approach will introduce higher latency, and the overhead of syscalls could limit the sampling frequency. Moreover, it appears that user space can only interrupt the VCPU at a certain frequency, without fully leveraging the richness of the PMU's performance events. On the other hand, if we incorporate the logic into kernel, `perf kvm` can bind to various PMU events and achieve faster performance in PMU interrupts. Tested with both Linux and unikernels as guests, the `perf script` command could correctly show the callchains. FlameGraphs could also be generated with this series of patches and [2]. [1] https://perf.wiki.kernel.org/index.php/Todo [2] https://github.com/brendangregg/FlameGraph v1: https://lore.kernel.org/kvm/SYYP282MB108686A73C0F896D90D246569DE5A@SYYP282MB1086.AUSP282.PROD.OUTLOOK.COM/ Changes since v1: Post the complete implementation, also updated some code based on Sean's feedback. v2: https://lore.kernel.org/kvm/SY4P282MB1084ECBCC1B176153B9E2A009DCFA@SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM/ Changes since v2: Refactored interface, packaged the info required by unwinding into a struct; Resolved some type mismatches; Provided more explanations based on the feedback from v2; more tests were performed. Tianyi Liu (5): KVM: Add arch specific interfaces for sampling guest callchains perf kvm: Introduce guest interfaces for sampling callchains KVM: implement new perf callback interfaces perf kvm: Support sampling guest callchains perf tools: Support PERF_CONTEXT_GUEST_* flags MAINTAINERS | 1 + arch/arm64/kvm/arm.c | 12 ++++++ arch/x86/events/core.c | 63 ++++++++++++++++++++++++----- arch/x86/kvm/x86.c | 24 +++++++++++ include/linux/kvm_host.h | 5 +++ include/linux/perf_event.h | 20 ++++++++- include/linux/perf_kvm.h | 18 +++++++++ kernel/bpf/stackmap.c | 8 ++-- kernel/events/callchain.c | 27 ++++++++++++- kernel/events/core.c | 17 +++++++- tools/perf/builtin-timechart.c | 6 +++ tools/perf/util/data-convert-json.c | 6 +++ tools/perf/util/machine.c | 6 +++ virt/kvm/kvm_main.c | 22 ++++++++++ 14 files changed, 218 insertions(+), 17 deletions(-) create mode 100644 include/linux/perf_kvm.h base-commit: 33cc938e65a98f1d29d0a18403dbbee050dcad9a -- 2.34.1