From patchwork Sun Dec 10 08:12:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyi Liu X-Patchwork-Id: 176304 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp6413931vqy; Sun, 10 Dec 2023 00:13:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGD/kuV6QckIOUGMs7MPOBwNuJ1Z5CCPuBuxEaccKkhaDDtzWPkJK91Emo9oxMRdtxLfwy+ X-Received: by 2002:a05:6871:a50d:b0:1fb:648:520e with SMTP id wc13-20020a056871a50d00b001fb0648520emr3283562oab.6.1702195992724; Sun, 10 Dec 2023 00:13:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1702195992; cv=pass; d=google.com; s=arc-20160816; b=dfb3R/lF0VLENzJWQnYWqv1wmldft9tsgN8LpMJm0IqH86N3mNNquPjh/JqL0Hmfdn IPxDvzRdGZvcyB8GDTDRGRxI7GU4GjnDe4N17l5ekILl2hoe6ly5bYeLUDm8ep290jnf NpYBykSLtMhvUlMcHzUHj87LILh5YuFcF9zSfBFLAzFSCUIYqyjSURvVJyePiVv9ROr2 zzVuQAY9vWIxSBU6+yZqG4JcWt0q6tkrtn63o7wTZJSWLRpjSLcwfBc8lgAI9QQuy69T mjK8uJ79q7kXMqdUhAxa9zfe5tWH+4JKEFv++c6TDLiEZwej9pa0Lt9ry7nXff6Lh3K9 TXUQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=QPuHegOvjI1Wqy9bawitz85TcVTd7dRsokA5lEbAaHg=; fh=HIpGW3XNt20YYDdrqGWTdutDMTXUX2EnMhGwR8EwV4c=; b=iPLc49hHOsIs2XB63YefJpRjqWZvPQHD+OYM8gYIGFXQBH8zZ0oHKuRlQoTpbJnbVT MPYl5tSRwRSksPldoCok1NpRQzvkY2aobdAGf8FTyQOQ/BBJ7W+EBF4lVjeiGU0s4kF0 amIYBHmYZDdWX6U7P93nEb6qJPq9qfjYJfr5yJy0xbq5qbrHJpzqRSCA//buDLTYlhJP n3BNnPKeilEhHI6geWxh5cxxDVYkeHfmQzcX0DkVxJcZFFNpM75glix63+Mb0MwEngPi fdav6HgxhqcqAxOn8Ynlel0L8kmm+5tJfyJslgc8nqDIsRDrH3hQnPmfwdr4lzq3LEqj f+bw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=pR+oypzw; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id bw9-20020a056a02048900b005c674eb1cb4si4270999pgb.477.2023.12.10.00.13.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 10 Dec 2023 00:13:12 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=pR+oypzw; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 71A648087214; Sun, 10 Dec 2023 00:13:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231653AbjLJIMz (ORCPT + 99 others); Sun, 10 Dec 2023 03:12:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229482AbjLJIMy (ORCPT ); Sun, 10 Dec 2023 03:12:54 -0500 Received: from AUS01-SY4-obe.outbound.protection.outlook.com (mail-sy4aus01olkn2175.outbound.protection.outlook.com [40.92.62.175]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06A9EC5; Sun, 10 Dec 2023 00:13:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ksGonCfeaPwR6l5GxKxoTf6pnuz1Bjt0wtGjUOpjJf6cj4uLUkoo7i9+iZU069MuRND+1EMuEymxCokQ3aV/PXa7nFGpKj2mTN1fYNx4U9fI7E89R6P8RQA936lUW8mjzyV9LYXLfc0+yS2lAgzVqQnKUtfVHwFrSKDQhY4KXs5wwj+zzsDHpa8/zxBq/99Z+FqKs/+KFMFT3hF673nI5vaN+KvBNJBGKjdb42c5rm6ec0PiP7lTeJ3DKBupa1L+FbhF85Ocb/IIB42yOSlMwDq9YzMYUGm4apaiyWOKCvIWZHLOwR/Li/ich7zfIg7vCoSW8mz9lCoOrn90oMuNKQ== 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=QPuHegOvjI1Wqy9bawitz85TcVTd7dRsokA5lEbAaHg=; b=CJJHfaihp31VcOb8dRdNImfqLJftgWnv8kkScbTIFaeSvgoh1/avnwQx38q+XkMMXlIPwsQmV8SC3a4ORVwHUubl3+Lqbq3VZcZCiA7JRgeCKJtxr7xXxunGCUHIdXRu41CHdH2Q2eGQCozVkOTOcWlvhhDqU3jWQGDsxSBv4sPyFni+Yr2lnfcCiijbs6/UeQXVtfiP9aPBmrquG0pW1nTKAXcO0PIjl8OsTxcDJfnsTD18Zh4QPnE85ZKgLNr/AYpja/XXsxJFDRCokptrgEzkK+J7VX0IqspcE4Pvzytg4hMzDrlUkm/ftXOS/9Y8IjwjwgtVO9fUoXLVRDwf5Q== 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=QPuHegOvjI1Wqy9bawitz85TcVTd7dRsokA5lEbAaHg=; b=pR+oypzw3XWu2MtJ9PWqLyoJXpzYdwiWXRfzQoGX014y3p2dqrcyjSL2EBeHqLD1vEZtVAos/eVyYkRWKlWLHvsIHdv2cadY1a1AFthCw3Kddg2r+I5tnDOtdht9xAT70t3XseQXoeSjM/uWUi1gEpi9DNGJBkjSv2Hvw0+mfEhSPjcNmABUwkwai2eHlzUj9+/Hj0j5zpKMzvQCWxZefL2BKsVXtacDKYgOI+NAN24nCZyBtpWYoTfZ49y8vCgacGCBykd509lQMgjfUDjm+KxA3103CmrL2Out2aP8J2KqDVxrC7OJNBECn22Q0R+d66PnDcDgOCEU3vkmfX2EGA== 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:12:53 +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:12:53 +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 1/5] KVM: Add arch specific interfaces for sampling guest callchains Date: Sun, 10 Dec 2023 16:12:18 +0800 Message-ID: X-Mailer: git-send-email 2.43.0 In-Reply-To: References: X-TMN: [QSggTfQqFdbZg1h9Gs/NfozjCC3waagQZgTkdujY8Bxzr5XF3x6M1A==] X-ClientProxiedBy: SG2PR01CA0125.apcprd01.prod.exchangelabs.com (2603:1096:4:40::29) To SYBPR01MB6870.ausprd01.prod.outlook.com (2603:10c6:10:13d::10) X-Microsoft-Original-Message-ID: <20231210081218.2226-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: e6e3b1ce-4bc6-4d46-15a7-08dbf957cec1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F3LN9hyPvN0CAVmU+CwhB+XgRrUEjHa4HI1zqe9BR1NM6xasWGUEsY21KafHRI3J6udF87Jntl/23EfJtc/Opb+vhobD14RLba1BTrlHBKYOmzbqq2koxkZRQyO47EL/bpUvwwpJs82XwF97jlhkabhXYL6QpsIGVVtU9lpA10RjaSs21fXg0BN0joIj17O6dK3xewzJ3XS8prVTjc6jURKdavKVp4MtSwUkCxXYXTcfMoFlZfpAgJUcjB64nhmc4a4vVp4s0GhSDaum5OMaz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UZTS1y7nukBk2PnlNdXxQ2YuzUVRW3EjOn8p7cXE3PZgLsTX7WAMNO/yXN5SKhPqpmQH4pPQNOax0EujHor/8aRPEK47rYrGW4rIJ31MBTuFs5Xmaw/SQYDIpGTeFjal0mMrPF0F+dlkrRO5uRf9irmJz+E13bXrKIfHtxMzvAvlxzxnFAGjomXzGlLCvykaq4KEmdBJOD4PK4OM9olx8GjBypIVsNCx1CznBHZvhM5A1m5YLZyB3QC+/Gre5VNyFdhuSMQtnIVlkwigUwSf1j9NjeChFKTXd7K4/B/hPRMa7XU8bgBRxNWBvhIyMeRQ3IaVbLBcl7O734I9D0RkV4LTul6ebVJ4h9s4JtbKxCDx27lwSqItG36eMojY/u+ux7wm78BUNJbvaPx0kp/odVsYv4lI/6K1ajjmPgKAOu5ilBDYqaWRtm2FivhJo5mlyJvTy+TvaGeo9Yk3kjjWGB6HQV63LoU+oYF+yPTxyQDZIoI4os49NP8tgvMnS9fRQTXyA1cyeV1WamQW4eIXQ9SufosCwL7XNF3jSwER7xay8v8Z46Fg1kCAsjn46YpMrZcRhsm/r69oYsuiqnU5C3g/Rv2dTFAh8nf4ZuhvQKY2o X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: e6e3b1ce-4bc6-4d46-15a7-08dbf957cec1 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:12:53.2638 (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 groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Sun, 10 Dec 2023 00:13:08 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1784881865247308596 X-GMAIL-MSGID: 1784881865247308596 This patch adds two architecture specific interfaces used by `perf kvm`: - kvm_arch_vcpu_get_unwind_info: Return required data for unwinding at once; including ip address, frame pointer, whether the guest vCPU is running in 32 or 64 bits, and possibly the base addresses of the segments. - kvm_arch_vcpu_read_virt: Read data from a virtual address of the guest vm. `perf_kvm.h` has been added to the `include/linux/` directory to store the interface structures between the perf events subsystem and the KVM subsystem. Since arm64 hasn't provided some foundational infrastructure, stub the arm64 implementation for now because it's a bit complex. The above interfaces require architecture support for `CONFIG_GUEST_PERF_EVENTS`, which is only implemented by x86 and arm64 currently. For more architectures, they need to implement these interfaces when enabling `CONFIG_GUEST_PERF_EVENTS`. In terms of 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. Signed-off-by: Tianyi Liu --- MAINTAINERS | 1 + arch/arm64/kvm/arm.c | 12 ++++++++++++ arch/x86/kvm/x86.c | 24 ++++++++++++++++++++++++ include/linux/kvm_host.h | 5 +++++ include/linux/perf_kvm.h | 18 ++++++++++++++++++ 5 files changed, 60 insertions(+) create mode 100644 include/linux/perf_kvm.h diff --git a/MAINTAINERS b/MAINTAINERS index 788be9ab5b73..5ee36b4a9701 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -16976,6 +16976,7 @@ F: arch/*/kernel/*/perf_event*.c F: arch/*/kernel/perf_callchain.c F: arch/*/kernel/perf_event*.c F: include/linux/perf_event.h +F: include/linux/perf_kvm.h F: include/uapi/linux/perf_event.h F: kernel/events/* F: tools/lib/perf/ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index e5f75f1f1085..5ae74b5c263a 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -574,6 +574,18 @@ unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) { return *vcpu_pc(vcpu); } + +bool kvm_arch_vcpu_get_unwind_info(struct kvm_vcpu *vcpu, struct perf_kvm_guest_unwind_info *info) +{ + /* TODO: implement */ + return false; +} + +bool kvm_arch_vcpu_read_virt(struct kvm_vcpu *vcpu, gva_t addr, void *dest, unsigned int length) +{ + /* TODO: implement */ + return false; +} #endif static int kvm_vcpu_initialized(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 2c924075f6f1..9341cd80f665 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13039,6 +13039,30 @@ unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu) return kvm_rip_read(vcpu); } +bool kvm_arch_vcpu_get_unwind_info(struct kvm_vcpu *vcpu, struct perf_kvm_guest_unwind_info *info) +{ + info->ip_pointer = kvm_rip_read(vcpu); + info->frame_pointer = kvm_register_read_raw(vcpu, VCPU_REGS_RBP); + + info->is_guest_64bit = is_64_bit_mode(vcpu); + if (info->is_guest_64bit) { + info->segment_cs_base = 0; + info->segment_ss_base = 0; + } else { + info->segment_cs_base = get_segment_base(vcpu, VCPU_SREG_CS); + info->segment_ss_base = get_segment_base(vcpu, VCPU_SREG_SS); + } + return true; +} + +bool kvm_arch_vcpu_read_virt(struct kvm_vcpu *vcpu, gva_t addr, void *dest, unsigned int length) +{ + struct x86_exception e; + + /* Return true on success */ + return kvm_read_guest_virt(vcpu, addr, dest, length, &e) == X86EMUL_CONTINUE; +} + int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) { return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4944136efaa2..6f5ff4209b0c 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -41,6 +41,7 @@ #include #include +#include #include #include @@ -1595,6 +1596,10 @@ static inline bool kvm_arch_intc_initialized(struct kvm *kvm) #ifdef CONFIG_GUEST_PERF_EVENTS unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu); +bool kvm_arch_vcpu_get_unwind_info(struct kvm_vcpu *vcpu, + struct perf_kvm_guest_unwind_info *info); +bool kvm_arch_vcpu_read_virt(struct kvm_vcpu *vcpu, gva_t addr, void *dest, + unsigned int length); void kvm_register_perf_callbacks(unsigned int (*pt_intr_handler)(void)); void kvm_unregister_perf_callbacks(void); diff --git a/include/linux/perf_kvm.h b/include/linux/perf_kvm.h new file mode 100644 index 000000000000..e77eeebddabb --- /dev/null +++ b/include/linux/perf_kvm.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PERF_KVM_H +#define _LINUX_PERF_KVM_H + +/* + * Structures as interface between Perf Event and KVM subsystem. + * Add more members for new architectures if necessary. + */ + +struct perf_kvm_guest_unwind_info { + unsigned long ip_pointer; + unsigned long frame_pointer; + bool is_guest_64bit; + unsigned long segment_cs_base; + unsigned long segment_ss_base; +}; + +#endif /* _LINUX_PERF_KVM_H */