From patchwork Sun Oct 8 14:53:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyi Liu X-Patchwork-Id: 149756 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a888:0:b0:403:3b70:6f57 with SMTP id x8csp1402368vqo; Sun, 8 Oct 2023 07:54:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGShk41X5KI2NblkVNF2m3JiBcKguC3brIQHERXrz13YvSHqvqbT96wd6AMdDwnecGfsvk9 X-Received: by 2002:a05:6a00:1390:b0:693:3d55:2ba6 with SMTP id t16-20020a056a00139000b006933d552ba6mr16915119pfg.9.1696776897394; Sun, 08 Oct 2023 07:54:57 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1696776897; cv=pass; d=google.com; s=arc-20160816; b=WthON79ULxxLzbrcBwm5Eng7TQPsGfl5XIFQMNyNzVFRzKb8O+xFPRIfyVshZWDGbd 7euB1C72ePINFd+0I8qARgytHvgvofCIN2hc8W8+kpZQ9dChFX+ImiSJIbet11i8J7h9 emldOG0JgPE2hUt1MBN0qZ3tN/VwsTFUrh1lCVXIXHqGA2v/uCCqxvZgoTilyEmNs/Ly h7myealZGOY7IdxlGM9FHmsEkqzfY/SCRZqMMEoohwHpfI2Ub1o583yiyzARK/PT3p+r qwZL9uVWEGUgDzvls8m4LQJmElLC7isFV8OvxaJ2fn7gshtAxtObvh2H1DaLTesqqtG5 qxOw== 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=V4dan7kHVeyvxdy/3lD+tO9lH46m373g697XhVl5n5o=; fh=F5FYRTUV4xCU93BuG+U5i7IE8Bv2SgSTNGSJMbfAlmQ=; b=zdrhUIUu10hez1RqoBGTuWPGmZ04t7OwoXrd5RB8YmmyG9W8WxqzwajWaJC85BaQHT LGLLAgdoJVxO9HZ5I66FL7egURrUERe62zPf7wzIx3a/e3rI49osRe2q8stvsMFLAQbn aA5v7Sx/FBPpfMPUOCWVTIsErftSgGy115vYtBxKm/6DnfgW8flZN2EP76ONjFU2DrDC JjJeM6oYNkAR0ib+8KX61rj7dwGkbei4kRTicLuI7Z6G6AzOzgC4zOO/+tsKRdECvFIX nTCLUFhvtBeQRp7XeOcM4p8P8S9CjrLPu4Qn3nQnATzNtfbbxCQKEpK9XKzxnqJ8Nq3n 9bkg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=rStEOiy+; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id u20-20020a634714000000b00578c9003932si7906478pga.808.2023.10.08.07.54.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 07:54:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=rStEOiy+; arc=pass (i=1); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 713AC8026838; Sun, 8 Oct 2023 07:54:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344903AbjJHOya (ORCPT + 18 others); Sun, 8 Oct 2023 10:54:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230303AbjJHOy2 (ORCPT ); Sun, 8 Oct 2023 10:54:28 -0400 Received: from AUS01-ME3-obe.outbound.protection.outlook.com (mail-me3aus01olkn2159.outbound.protection.outlook.com [40.92.63.159]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0C935A4; Sun, 8 Oct 2023 07:54:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WMmJa60OL51BurQg+847fy5d+UwpqF8hHYXORAKjergNrx9iIH+btJ3YEp964hBfk+El3hpE6gD66QDs1ESHRlZ7MYomIoc6dOBY+aIqWJ/Z9+kzx/ogR10w1aW2asghTwhShggwIELA4DtE8BlMf4ImbFEHilOhx/214hR2+7EqrHQ+rP/LoSKcsrquh8jQ5E36JBVi++aDkOaJ424L6ICU7cPojB7M6Zv11xXOOehZudETj/TClqYp+XehiqHEcx6duz1lFHmdruVPXEX2S68+8DMxHuYwilvO2LbyyNJunE487Tzw9TqWzskuklF70iAL1fH+SLtUQeBDBdbI2w== 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=V4dan7kHVeyvxdy/3lD+tO9lH46m373g697XhVl5n5o=; b=CvF8ss5hxHSZ2RFGh5rCoBTg3lL9UnWhZsnN6Hs3Zj+aGGDzTaDxX957O8SxkqcTwiLQmCmWyu+ZrOKJW/1Yd02/Z0t2CdLI3H7ii/LZjos14lDGGnn2VbrqXirx5SiHQLJ1ldSEP5/pwcaxjCIeHP5N9G/mXCD+7XDeQX2ZqBL8Rjt9r56NP9lF5d3c37ny/pNu8uCPKCxh4TaoooIQKNZxs70ZZtNCqtq9msO8cWguWpCpSQgFHHHEhyaN+r/h89TsiTI/nIqjKNTzH3pBd0dTvU/J/K7nnRy0GBa96hsDv7RnbGK8IZkwDCrLC18s0VZpv8ffh++ombsO/cKYAg== 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=V4dan7kHVeyvxdy/3lD+tO9lH46m373g697XhVl5n5o=; b=rStEOiy+g2M3KYn0YHg6AG7GWqNO1I50nkUH5LbNIrlFbiglzFZOnswMfP9vy3NJzQC7o8oCfm/vZjSuI66fovGjn7h4rpNnwdd4evx+rAhmZCURhMH2sJzolZmFTN0t18Qfal/2Dup5BaJZGeQrtcT7kJHF4D5hzndObqU5erkvmktfdGufMWgIpEcOfm+sOHAEWH7jtUaGU9lDjuuXoa0n+oJRJgSvxv/V90skvyjBBCCdC6gKb6OgzI93zu69bFWjKN5VBlhtDODdaTyEjozKPY/G38eLmZeWE8tQqu7y46zyVmV0A9VUc7NHP0XgxLSWEHMQoiYtY+DIpPZX5A== Received: from SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:ac::13) by SY4P282MB1371.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:a2::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.21; Sun, 8 Oct 2023 14:54:20 +0000 Received: from SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM ([fe80::e39e:17fc:36d8:1ea8]) by SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM ([fe80::e39e:17fc:36d8:1ea8%3]) with mapi id 15.20.6838.040; Sun, 8 Oct 2023 14:54:20 +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, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, adrian.hunter@intel.com, Tianyi Liu Subject: [PATCH v2 2/5] perf kvm: Introduce guest interfaces for sampling callchains Date: Sun, 8 Oct 2023 22:53:59 +0800 Message-ID: X-Mailer: git-send-email 2.42.0 In-Reply-To: References: X-TMN: [iekiAmTYnBBVvcmUmOfpwm9LbawksJQ1ZghS3+UOfjPumANR7m9DTQ==] X-ClientProxiedBy: PS2PR02CA0047.apcprd02.prod.outlook.com (2603:1096:300:59::35) To SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM (2603:10c6:10:ac::13) X-Microsoft-Original-Message-ID: <20231008145359.7269-1-i.pear@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SY4P282MB1084:EE_|SY4P282MB1371:EE_ X-MS-Office365-Filtering-Correlation-Id: 57133fc6-dd08-4e97-d9d1-08dbc80e738b X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: qf6odUHCy1AankStOFkVtSxY9m0Sq9VLW7IJw5hiqxXsrzkt7PL1Scln628kVsZSSXHO/iqBQQZ907wnHf2JgBr6KjcgOvxgVhK428UtgZAtuQa0b5ZdzckRixvRLR3RURwgMskQH88qIHFh11tV+V0OOhJMGcF5+UulU69Z9BliO7HkrzrCQQepOkazsylX29sfhDnrfrgvFs4rGkxMPC/qPbukOOjk9ohyaCd9r92Fb1pmLiifBz+VYvWKllwQ8az5fbLf+S0d2KqzWo6bFn5yfyPbX4LNyhXgYgi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cGIjyYi8dnCbR5IQcnWAFGOnKyy8CzXYoonjCr1sunnBE7mXnJ36mttjDHJiBE2q5mjeGcGNKhdsVSP+thLeW4xp97AGuCbZhXNQjWQmTr8efodif3Ee3Rzhg0IGJsh1dDEJyMrzsQl8vtDLBEERSMRld9m5Rkg0p9hilVnXXMDD+yM5oe1xZvaBNqVgxIRgDve4hNZZJ175w8heD73g6Fhczp/s/DEjG8GbrUBXvTP4+SWOR6Y29P2c6BKnNqukmhjO6uP+Syo0tzooIN6VTGt81LIn7R1r55XFjA7Hob7sCRL8ncf1av1p9z5xQu2L1036Y4dyn+figMZWe3ofvi0gQKtaHVbkqRr+p2a0/b63Y1hpiYVxBU0vUT4vDSUKSIFkzRu+2lOsZcNzQfvrMeDnVAwChEkwoUptCi9nF1LX9R5R14fDAO2sZU8jbWzuGWDd2cURsyE2h5bVgvuXhu7V6WzGeSRz9Y7j88/1loyk0gMagYOtdtrN8bmDPuvheXphxi03sOKujb+HVCMaJAEnJTMesOKhber8BpAzsAI/kKlwc8EZRre3li2eCJcAekEHx1H3cW/I+/uNRekbXb5OtAD8ggw X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 57133fc6-dd08-4e97-d9d1-08dbc80e738b X-MS-Exchange-CrossTenant-AuthSource: SY4P282MB1084.AUSP282.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Oct 2023 14:54:20.8541 (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: SY4P282MB1371 X-Spam-Status: No, score=3.0 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_SBL_CSS, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.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 (morse.vger.email [0.0.0.0]); Sun, 08 Oct 2023 07:54:55 -0700 (PDT) X-Spam-Level: ** X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779199531692599800 X-GMAIL-MSGID: 1779199531692599800 This patch introduces two callback interfaces used between perf and KVM: - get_frame_pointer: Return the frame pointer of the running vm. - read_virt: Read data from a virtual address of the running vm, used for reading the stack frames from the guest. This also introduces a new flag, `PERF_GUEST_64BIT`, to the `.state` callback interface, which indicates whether the vm is running in 64-bit mode. Signed-off-by: Tianyi Liu --- include/linux/perf_event.h | 15 +++++++++++++++ kernel/events/core.c | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index e85cd1c0e..d0f937a62 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -28,10 +28,13 @@ #define PERF_GUEST_ACTIVE 0x01 #define PERF_GUEST_USER 0x02 +#define PERF_GUEST_64BIT 0x04 struct perf_guest_info_callbacks { unsigned int (*state)(void); unsigned long (*get_ip)(void); + unsigned long (*get_frame_pointer)(void); + bool (*read_virt)(void *addr, void *dest, unsigned int len); unsigned int (*handle_intel_pt_intr)(void); }; @@ -1495,6 +1498,8 @@ extern struct perf_guest_info_callbacks __rcu *perf_guest_cbs; DECLARE_STATIC_CALL(__perf_guest_state, *perf_guest_cbs->state); DECLARE_STATIC_CALL(__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DECLARE_STATIC_CALL(__perf_guest_get_frame_pointer, *perf_guest_cbs->get_frame_pointer); +DECLARE_STATIC_CALL(__perf_guest_read_virt, *perf_guest_cbs->read_virt); DECLARE_STATIC_CALL(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); static inline unsigned int perf_guest_state(void) @@ -1505,6 +1510,14 @@ static inline unsigned long perf_guest_get_ip(void) { return static_call(__perf_guest_get_ip)(); } +static inline unsigned long perf_guest_get_frame_pointer(void) +{ + return static_call(__perf_guest_get_frame_pointer)(); +} +static inline bool perf_guest_read_virt(void *addr, void *dest, unsigned int length) +{ + return static_call(__perf_guest_read_virt)(addr, dest, length); +} static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return static_call(__perf_guest_handle_intel_pt_intr)(); @@ -1514,6 +1527,8 @@ extern void perf_unregister_guest_info_callbacks(struct perf_guest_info_callback #else static inline unsigned int perf_guest_state(void) { return 0; } static inline unsigned long perf_guest_get_ip(void) { return 0; } +static inline unsigned long perf_guest_get_frame_pointer(void) { return 0; } +static inline bool perf_guest_read_virt(void*, void*, unsigned int) { return 0; } static inline unsigned int perf_guest_handle_intel_pt_intr(void) { return 0; } #endif /* CONFIG_GUEST_PERF_EVENTS */ diff --git a/kernel/events/core.c b/kernel/events/core.c index 4c72a41f1..eaba00ec2 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -6759,6 +6759,8 @@ struct perf_guest_info_callbacks __rcu *perf_guest_cbs; DEFINE_STATIC_CALL_RET0(__perf_guest_state, *perf_guest_cbs->state); DEFINE_STATIC_CALL_RET0(__perf_guest_get_ip, *perf_guest_cbs->get_ip); +DEFINE_STATIC_CALL_RET0(__perf_guest_get_frame_pointer, *perf_guest_cbs->get_frame_pointer); +DEFINE_STATIC_CALL_RET0(__perf_guest_read_virt, *perf_guest_cbs->read_virt); DEFINE_STATIC_CALL_RET0(__perf_guest_handle_intel_pt_intr, *perf_guest_cbs->handle_intel_pt_intr); void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) @@ -6770,6 +6772,12 @@ void perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) static_call_update(__perf_guest_state, cbs->state); static_call_update(__perf_guest_get_ip, cbs->get_ip); + if (cbs->get_frame_pointer) + static_call_update(__perf_guest_get_frame_pointer, cbs->get_frame_pointer); + + if (cbs->read_virt) + static_call_update(__perf_guest_read_virt, cbs->read_virt); + /* Implementing ->handle_intel_pt_intr is optional. */ if (cbs->handle_intel_pt_intr) static_call_update(__perf_guest_handle_intel_pt_intr, @@ -6785,6 +6793,8 @@ void perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs) rcu_assign_pointer(perf_guest_cbs, NULL); static_call_update(__perf_guest_state, (void *)&__static_call_return0); static_call_update(__perf_guest_get_ip, (void *)&__static_call_return0); + static_call_update(__perf_guest_get_frame_pointer, (void *)&__static_call_return0); + static_call_update(__perf_guest_read_virt, (void *)&__static_call_return0); static_call_update(__perf_guest_handle_intel_pt_intr, (void *)&__static_call_return0); synchronize_rcu();