From patchwork Mon Aug 14 03:06:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Huang Shijie X-Patchwork-Id: 13352322 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7A2FAC001DB for ; Mon, 14 Aug 2023 03:11:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=7SFLWzdDhRDbDVytKK+2UiisvRqKEMlrxtmSh9vjZro=; b=0qFFSxOVLH8ccm KFBF8J+Yy6J57aOuv041r+4BnK04rIK264cJBYEtO1tWEflCUj9FlZUiiBh4DH7XcjodJlGBH+aKB sYozf+Y3j5PrJ2TdASAaaW4MN5P/JPt/jesFjlYhrM96DP4rs/1OlEUGzRvmylWavuvLyX3MaYaah 221H+PzRknRVxliB0eHeZfQNwO0mzJ1djdCykheujMhpzvXOhQcW4NkjSqU9QeP5b1ptjFZsPHIir vWewhylVgPQrQs3NFSge7JEC56s5qgV27htIj6qgdE2CiRgqcO1ekFcatES1NQY6mhTceMK/N1jJy UtZyggm8yFKOD+GgBDaA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qVNzR-00G3vJ-29; Mon, 14 Aug 2023 03:11:33 +0000 Received: from mail-bn8nam12on2072c.outbound.protection.outlook.com ([2a01:111:f400:fe5b::72c] helo=NAM12-BN8-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qVNzO-00G3uR-0Q for linux-arm-kernel@lists.infradead.org; Mon, 14 Aug 2023 03:11:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=j2jE3e3MdBKY+o4xbnV/CKGp+tKhUWF3+/AHOaO66Fe6/GCbUEnCVk8G4/6ZFsLBW5sDMhSkOUD2TJ3azcbayJrk0pSzYELe2bpnsWqVYfKHTGdyAr75k364cN/S/L1SJPsegTUiR5i2ieUlEUtGw92VQ5Rhh+A5UJnEvT8bz5wuzdOJ65ayd/w5C76Re5a641eXeEgMH1UQx6vwofrR3uEyy2ZeP01VmSzEtWTzoqrUmWpXatJoSZMYR2nBRSIAtLlykXm0QBxEYN9k1WHTSpSRVN+Y2TcOnZABPETk6R8sZr/YbJ1/2CM/GrOCcaWbeX+UcgzHDwWl73e0rppVQQ== 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=mRDzB3aExq86YiQZ0pvp3QAv6QJHLKcJnBjY+XMJ5cE=; b=fOXM8rW/sPQdVbwqFmkSG85NgiZOvxYb1gdkX/PrrqaxZ5I5K7rwsiCEVxh+0vBa2g4G7LKIwJbYjtHVuExuDEjF/PIxGwgApVtuqqtbJs3vydrIaleneD/4YQ2fonJY6+hL517s4oicKP/1N6WePIiipCE3q/vNwb4Pdw3FpYQOqqaU7RINA+x+j4as7pFeVizRv2THmwjuH5n1qliFx5MqJW4Y2SHbZNbVDSmftRDgRnCmNdP6sBV/nMwA68b/evRt12dI0Mi0wfFrCcBcIyEqykq2jo1e576NoulXC+jYPscHmCHwKLNgY+OgMkhosBC606LI436Uq7lWcEKm+A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=os.amperecomputing.com; dkim=pass header.d=os.amperecomputing.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=os.amperecomputing.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=mRDzB3aExq86YiQZ0pvp3QAv6QJHLKcJnBjY+XMJ5cE=; b=U8DbK9eRYcXVl5hLxROjhzdWF/t7tc34p4QPCjti10/tHUVAe082r2wWwZl74ujufFIrEfa1zA8AcbKNke79oWzjK25n1wkCIGoaqRoCSR5StGstStWjdI2bWd4lEy4SYlULM1V8Vldtrggxz4R6rpYvP0lMxbQ+3dd20agMXXc= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=os.amperecomputing.com; Received: from DM8PR01MB6824.prod.exchangelabs.com (2603:10b6:8:23::24) by PH0PR01MB6501.prod.exchangelabs.com (2603:10b6:510:15::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6678.24; Mon, 14 Aug 2023 03:11:21 +0000 Received: from DM8PR01MB6824.prod.exchangelabs.com ([fe80::d62f:d774:15b0:4a40]) by DM8PR01MB6824.prod.exchangelabs.com ([fe80::d62f:d774:15b0:4a40%4]) with mapi id 15.20.6678.022; Mon, 14 Aug 2023 03:11:21 +0000 From: Huang Shijie To: maz@kernel.org Cc: oliver.upton@linux.dev, james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, pbonzini@redhat.com, peterz@infradead.org, ingo@redhat.com, acme@kernel.org, mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, namhyung@kernel.org, irogers@google.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-perf-users@vger.kernel.org, patches@amperecomputing.com, zwang@amperecomputing.com, Huang Shijie Subject: [PATCH v3] KVM:arm64: reconfigurate the event filters for guest context Date: Mon, 14 Aug 2023 11:06:45 +0800 Message-Id: <20230814030645.11632-1-shijie@os.amperecomputing.com> X-Mailer: git-send-email 2.39.2 X-ClientProxiedBy: CH0PR04CA0118.namprd04.prod.outlook.com (2603:10b6:610:75::33) To DM8PR01MB6824.prod.exchangelabs.com (2603:10b6:8:23::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM8PR01MB6824:EE_|PH0PR01MB6501:EE_ X-MS-Office365-Filtering-Correlation-Id: a5f92c55-15d5-4890-c9be-08db9c742233 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 46UedWsdpBUlkwlLw0OWEa0VsFiQaerf8H9r1t8/zzriQpACKAGV/rg7wGrkQ8DbawoV9ybqObzBPqJr8HQGDP/BFsK+yZCF2TQLa3fqbmihcqJctBq48628mvzsTsvT1sTHypJ+/lD3Ocj7oqfGRHeA2D/3hyPnToS3YEpc84GAhKa8nrEK/uoV3ml7mALdYIYugVg5cHJUhgTrx6iZOjNwoo48iDflxenHbS662ih2Gc/A4unD4RbJoomAhma2+pwu2X4AKggPd0dAhO3gtddCvOi70e2KrtdoZLyn/F48rTowh3qO62DMPc4T8EQeo+LaeDEzqK39BHLsgIam8b8sHkn3a78KOlKKd2h0kpxI1mHBpxEWQutg16jm0qUnTRBz/cuzv4gbEtRYNzX/ITXd8GV/MkfeMWP+RpfuYGPxwQYjWdsyvbnlZKrxRwYCqOZm3kds+gy07kShQFn3UrUpmyLNQA/JYbzg4TH3cZScA6jgpIawjjbEOckNaKWJlTHpknLaDXaEQ1/2RkzGaMXfhqXBwpOZsW/zOz+iFK/ZA9cP8J/3KgcHHEwGRLfEbDpVCF3jnG1Hi74dNTElaemIMW8J6dkJhdXmkVisPm4= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM8PR01MB6824.prod.exchangelabs.com;PTR:;CAT:NONE;SFS:(13230028)(39840400004)(346002)(376002)(136003)(396003)(366004)(186006)(1800799006)(451199021)(6512007)(6486002)(6666004)(52116002)(966005)(86362001)(38350700002)(38100700002)(83380400001)(26005)(6506007)(107886003)(1076003)(2906002)(478600001)(2616005)(8676002)(66556008)(66476007)(8936002)(5660300002)(7416002)(41300700001)(66946007)(316002)(4326008)(6916009);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MSUAMzOsYMA2frSXAk91pODPzEBTPYbpRoBQi46UwbstKFQnOGORqDUw1ryJ5Rp6LFz/0O3IsdvTPfPhxrpHrXH3JYjYkD0VIg+ffo3RGT3CSA/3VegcnCiQ+aPlLotyEAA5KH/vf5KMIPyTJ0LYwGpEd7ReoDAKgQhHpSDlljhbXkIjGy2JF704xtEIqF11EG6CO7etAbIsvlhYkfJYgtgFtz2TQSm2VvdSyJ6FkPzkMP80Lfongfibx2dCdiCV084sae8B6qc1ko1RFID47Y37epDuQ3/ftPIi2KoOYKozVQ80d0OJz1GmthZdx5xSoue8zB4NijjOR4CusYFLrk9tNupdaVtIcdXYkg5C8d5s7XieHdlAmdl8B9caOCWRELNopZrGGvECAAgwkvIQZ83xbEN2BQ+27cmYLlCsuIWtikJoZJHAxKLnJblkMFEuMes2iRFWt0Ym3ULEwlNEpr/mYS7+QdmSHSc8B4OTQ2UoxQ6xslZRIBvzSKGJlZQQdcn0ZXF7REg65OIgr4szCfhgSWHPr45MeD+aQ3O3euqQ00pFiRJn/8ZUucWGy5vxw8CFyqXEc4J12pdElb5MXBkGQURK9+JPRzfZ9c70XhDMhAirNWJdxx9UwTomEsRizZuszPCmf049q27r7vZS3ZBlc2Crpk2a2qBzBCT4aywinEzvt6mFAChLffUKUHhSJ7wiykdQv1InANWILH3jXydIOKA7tcojh3bfCE54d2iqNZm4FG32PAAhB8XlZjAveWBsNxLlwKbgicWHBbk4Odwf8eUdPHHS5zGE9MiWip9iHhdzhAf8yHs3JN0EgQkN1KZKtFdRtmceJb7KYq25nWmrDwzMKxPBPAJF8dm8L1j2sDdm5lCHSrtPgo8MnoJvwdzALIkTCrpcH4ghzfCFHbbaOaoqYdr4vwaGR/Sx7kpHjIbiOq1ozCLPkh4LafOONn5/y27gh5ijtkjN5adGko7wlvWGBFCag9fcyDia2wXZij+4rSVMt2CLKmaaZbsnqfe03zCWkB/WpZMiMfYS8WpwPzAqe5pto+EH/jnX6WnlOS+Hvu3M/0oh613ztd9WgMaCo9H8ybGDYQUTJbsCpWdw2n75f6TYi6mnBjZcn6WaNuBLnCIJybdDzOrzChySsIp2DKcc4AL7BIkKyaE+dbRd14HEPW6LLBDmgo2gb83Q5CEDqYSnuUG+llYpXeJzzom8F3JtRyyBvwF/0U8l3kSiS0LQu+NZUzgF/yKrvKyLmTR+9ViiKZhdHRDENiCFLf6GV80FDd6tQpWAT/sU/pH2bGvYdK0N9SfjPatWA0gkhLnY5Oqv2ImaDQE1nn/KvfTIQsBwDB9WIS9n7SuoX57u0/iLo0MomOa/E+eAa+Hc0tnlcIF9QRAXYCp6VqyrwxPdQLC1gJyREHbcExdeqH8UC6T0kOMT4wckoDEV7yKgLqDx55QXUQjMzu2Y6ggz/Iv/hts/w85j7tZv8m7WeMRMbqu551QJPujJHXvCVezjT/3cJZkokR7njazxigfjij07AKSLkNdqxdiClfnxurqwUxleNf7WcDEOsAktpFBVInEuv8eCCmfYOKC2TZVVe64mBVW3R9doWr8MTKVH3KnxtdfiOrYOkpyh5SAe8Sc= X-OriginatorOrg: os.amperecomputing.com X-MS-Exchange-CrossTenant-Network-Message-Id: a5f92c55-15d5-4890-c9be-08db9c742233 X-MS-Exchange-CrossTenant-AuthSource: DM8PR01MB6824.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Aug 2023 03:11:21.5751 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3bc2b170-fd94-476d-b0ce-4229bdc904a7 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pC5O0TbSpMjRlha7tR6Q/l8R+eyN/XCPb4yT7k0V1ZZWk+acNaoj3lJpBplLk0G8OApDXYH+EMR4xpmvbexEtyRaAZIU9/Ghg95lx7QCeNv7oJZKjS8NSRLfQjirrtFJ X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR01MB6501 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230813_201130_265256_537428FE X-CRM114-Status: GOOD ( 16.33 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 1.) Background. 1.1) In arm64, start a guest with Qemu which is running as a VMM of KVM, and bind the guest to core 33 and run program "a" in guest. The code of "a" shows below: ---------------------------------------------------------- #include int main() { unsigned long i = 0; for (;;) { i++; } printf("i:%ld\n", i); return 0; } ---------------------------------------------------------- 1.2) Use the following perf command in host: #perf stat -e cycles:G,cycles:H -C 33 -I 1000 sleep 1 # time counts unit events 1.000817400 3,299,471,572 cycles:G 1.000817400 3,240,586 cycles:H This result is correct, my cpu's frequency is 3.3G. 1.3) Use the following perf command in host: #perf stat -e cycles:G,cycles:H -C 33 -d -d -I 1000 sleep 1 time counts unit events 1.000831480 153,634,097 cycles:G (70.03%) 1.000831480 3,147,940,599 cycles:H (70.03%) 1.000831480 1,143,598,527 L1-dcache-loads (70.03%) 1.000831480 9,986 L1-dcache-load-misses # 0.00% of all L1-dcache accesses (70.03%) 1.000831480 LLC-loads 1.000831480 LLC-load-misses 1.000831480 580,887,696 L1-icache-loads (70.03%) 1.000831480 77,855 L1-icache-load-misses # 0.01% of all L1-icache accesses (70.03%) 1.000831480 6,112,224,612 dTLB-loads (70.03%) 1.000831480 16,222 dTLB-load-misses # 0.00% of all dTLB cache accesses (69.94%) 1.000831480 590,015,996 iTLB-loads (59.95%) 1.000831480 505 iTLB-load-misses # 0.00% of all iTLB cache accesses (59.95%) This result is wrong. The "cycle:G" should be nearly 3.3G. 2.) Root cause. There is only 7 counters in my arm64 platform: (one cycle counter) + (6 normal counters) In 1.3 above, we will use 10 event counters. Since we only have 7 counters, the perf core will trigger multiplexing in hrtimer: perf_mux_hrtimer_restart() --> perf_rotate_context(). If the hrtimer occurs when the host is running, it's fine. If the hrtimer occurs when the guest is running, the perf_rotate_context() will program the PMU with filters for host context. The KVM does not have a chance to restore PMU registers with kvm_vcpu_pmu_restore_guest(). The PMU does not work correctly, so we got wrong result. 3.) About this patch. 3.1) Add a week arch_perf_mux() for perf_mux_hrtimer_handler(). 3.2) In the arm64, implement the arch_perf_mux(). Make a KVM_REQ_PMU_RESTORE_GUEST request if the perf multiplexing occurs in the guest context. 4.) Test result of this patch: #perf stat -e cycles:G,cycles:H -C 33 -d -d -I 1000 sleep 1 time counts unit events 1.001006400 3,298,348,656 cycles:G (70.03%) 1.001006400 3,144,532 cycles:H (70.03%) 1.001006400 941,149 L1-dcache-loads (70.03%) 1.001006400 17,937 L1-dcache-load-misses # 1.91% of all L1-dcache accesses (70.03%) 1.001006400 LLC-loads 1.001006400 LLC-load-misses 1.001006400 1,101,889 L1-icache-loads (70.03%) 1.001006400 121,638 L1-icache-load-misses # 11.04% of all L1-icache accesses (70.03%) 1.001006400 1,031,228 dTLB-loads (70.03%) 1.001006400 26,952 dTLB-load-misses # 2.61% of all dTLB cache accesses (69.93%) 1.001006400 1,030,678 iTLB-loads (59.94%) 1.001006400 338 iTLB-load-misses # 0.03% of all iTLB cache accesses (59.94%) The result is correct. The "cycle:G" is nearly 3.3G now. Signed-off-by: Huang Shijie --- v1 --> v2: Do not change perf/core code, only change the ARM64 kvm code. v1: https://lkml.org/lkml/2023/8/8/1465 v2 --> v3: Disdurb the perf/core again. v2:http://lists.infradead.org/pipermail/linux-arm-kernel/2023-August/858427.html --- arch/arm64/include/asm/kvm_host.h | 1 + arch/arm64/kvm/arm.c | 18 ++++++++++++++++++ include/linux/perf_event.h | 2 ++ kernel/events/core.c | 5 +++++ 4 files changed, 26 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 8b6096753740..151810445d79 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -49,6 +49,7 @@ #define KVM_REQ_RELOAD_GICv4 KVM_ARCH_REQ(4) #define KVM_REQ_RELOAD_PMU KVM_ARCH_REQ(5) #define KVM_REQ_SUSPEND KVM_ARCH_REQ(6) +#define KVM_REQ_PMU_RESTORE_GUEST KVM_ARCH_REQ(7) #define KVM_DIRTY_LOG_MANUAL_CAPS (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \ KVM_DIRTY_LOG_INITIALLY_SET) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index c2c14059f6a8..53d6555912d1 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -792,6 +792,9 @@ static int check_vcpu_requests(struct kvm_vcpu *vcpu) preempt_enable(); } + if (kvm_check_request(KVM_REQ_PMU_RESTORE_GUEST, vcpu)) + kvm_vcpu_pmu_restore_guest(vcpu); + if (kvm_check_request(KVM_REQ_RELOAD_PMU, vcpu)) kvm_pmu_handle_pmcr(vcpu, __vcpu_sys_reg(vcpu, PMCR_EL0)); @@ -878,6 +881,21 @@ static int noinstr kvm_arm_vcpu_enter_exit(struct kvm_vcpu *vcpu) return ret; } +void arch_perf_mux(bool rotations) +{ + struct kvm_vcpu *vcpu; + + if (!kvm_arm_support_pmu_v3() || !has_vhe()) + return; + + vcpu = kvm_get_running_vcpu(); + if (!vcpu) + return; + + if (rotations) + kvm_make_request(KVM_REQ_PMU_RESTORE_GUEST, vcpu); +} + /** * kvm_arch_vcpu_ioctl_run - the main VCPU run function to execute guest code * @vcpu: The VCPU pointer diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index 2166a69e3bf2..07ef1aa00226 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1860,6 +1860,8 @@ extern void arch_perf_update_userpage(struct perf_event *event, struct perf_event_mmap_page *userpg, u64 now); +extern void arch_perf_mux(bool rotations); + #ifdef CONFIG_MMU extern __weak u64 arch_perf_get_page_size(struct mm_struct *mm, unsigned long addr); #endif diff --git a/kernel/events/core.c b/kernel/events/core.c index 6fd9272eec6e..eefb28198dd1 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -1059,6 +1059,10 @@ static void perf_cgroup_switch(struct task_struct *task) } #endif +void __weak arch_perf_mux(bool rotations) +{ +} + /* * set default to be dependent on timer tick just * like original code @@ -1076,6 +1080,7 @@ static enum hrtimer_restart perf_mux_hrtimer_handler(struct hrtimer *hr) cpc = container_of(hr, struct perf_cpu_pmu_context, hrtimer); rotations = perf_rotate_context(cpc); + arch_perf_mux(rotations); raw_spin_lock(&cpc->hrtimer_lock); if (rotations)