Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5353135rwb; Wed, 9 Aug 2023 02:51:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHtmLj4FLMBMN9P3EryWvuExFk/n/EoU67VBDno5EwPYENPQIzkFtGTI2U9Qg1jJNkcIbRi X-Received: by 2002:a05:6a20:2450:b0:12e:92c1:b1c8 with SMTP id t16-20020a056a20245000b0012e92c1b1c8mr2081752pzc.47.1691574713384; Wed, 09 Aug 2023 02:51:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1691574713; cv=pass; d=google.com; s=arc-20160816; b=NfJX2U/gwu37gwYWvdZ4mjcb5tRUScpVU8uOPW3GZbjyamjwBOkXR7EO2CH8qXJxXi JsmQALDxxgJeQ6dcB6lTb0hdmI0tq4pOqyEhRqnDNdHm37UaNY2+p932fk8Pda1oTSp4 /lgWLm1VhSWFxLjWBb1j6M1V5s4E/FHOAScyvQLRkH8hxNRxytGfhL0IwwSPakM8TUKR mjWN5mvMFJyx0vdZfzrJb+Aei4lbOmsx/+/bdfH+0qdMDJFv40Su/eCbqW+f5XqNTeny rYLcqpSaXyKzrE6JKqWzNJSlkA8YcUo4hhEbSKZsP/fDZCdH5Rz3v3t7P7VK6gnm52jz MeaA== 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 :in-reply-to:from:references:cc:to:subject:user-agent:date :message-id:dkim-signature; bh=VO3A0T6YXPBiOJxaagHYwhcmBj+2fqcr1nAF4wNnPWw=; fh=6/0KmpQQaKxVKknITXKNVealVlkbZpZIqTzbpOvtC6c=; b=niBoT25BSOJr2ngTy4fJ+Y3mrJrXSpzzRt4VWzdjalA58zGlmkSScoVxfImsMdIhws OTH7m4VTXZ3jCYlI1+NKZO/FFrb5Kocz5+l3p9AEak4ipJDYA6gbJZwb+rqnrOGpmXAJ /PAE61rQqjGHBPEqoSW3/cL1NCQHLn+m8ZjzgPsvA0yr90MQxzUIS2JJ//YqyZ75mIxa RtpjJtjejLbDHvbYKQcRuusWx2hMpcjwG2gfZkul5OVvn7VhCRLw482jQzpzJNCThVzJ +AGNpU3QCbAUZE3O34uazHH+QZkYeydvs5jKz+SfdnVrKgScbbTMLc3wleobXUb0k36D wPqw== ARC-Authentication-Results: i=2; mx.google.com; dkim=temperror (no key for signature) header.i=@amperemail.onmicrosoft.com header.s=selector1-amperemail-onmicrosoft-com header.b=C6tB1fOS; arc=pass (i=1 spf=pass spfdomain=os.amperecomputing.com dkim=pass dkdomain=amperemail.onmicrosoft.com dmarc=pass fromdomain=amperemail.onmicrosoft.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g186-20020a636bc3000000b00564997692c2si8810271pgc.711.2023.08.09.02.51.42; Wed, 09 Aug 2023 02:51:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=temperror (no key for signature) header.i=@amperemail.onmicrosoft.com header.s=selector1-amperemail-onmicrosoft-com header.b=C6tB1fOS; arc=pass (i=1 spf=pass spfdomain=os.amperecomputing.com dkim=pass dkdomain=amperemail.onmicrosoft.com dmarc=pass fromdomain=amperemail.onmicrosoft.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232067AbjHIJiI (ORCPT + 99 others); Wed, 9 Aug 2023 05:38:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229489AbjHIJiH (ORCPT ); Wed, 9 Aug 2023 05:38:07 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2135.outbound.protection.outlook.com [40.107.93.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 30DD4E5F; Wed, 9 Aug 2023 02:38:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B5+bZH6drXII8/DsZP6c0AKJgB4UVrRtoNhsNEpmzmRpcCe/2DdP9XbeM13hxruXqygBzd+wc1uI0J8pfzexn43PUPFJUFBSxgt62ykf7NLg0LAND3gxrgguOA3rUmQveqp0vBRZUYlbwrdwfCrVo3J+E1wOlfNQRl18bFs7WffOBUzclL2l2Tcp7n0z2DrlvTDxT5RPquDAWxJxZdnc3jVtuIpyBAcTJ4dwyatAEZIcauPcRz94suJ2924OK+I3/kDmrfjOK09d6bDMLk5Xvaw4e4nebLS5AEZJu27vmiJJRz9LIR3BpHd87O3d9t9h/d/PiFT6z+eElFaLycZqPA== 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=VO3A0T6YXPBiOJxaagHYwhcmBj+2fqcr1nAF4wNnPWw=; b=i2GcHhH6ltptk9Dsxvt5ja6hZNtiQaiZxjP+YOtvlQgy9NHvNZo043JfAaHS4jANh5wmc+iiU9AHoJrBAGGRcKFMdoqGU2D2Ldzefy48tvJSpGJZLcyPTDnF+tl19quSOI2U6/5/c5mDrbl6unHtfHLT1nByEzHW7gCiG6zs0ILXO+pxb4OMQ9agPPMGBbYuRmVtBv98xYw6H4gl73ZFQ3segtYUM0taSUb+tupvhUVaJRgCpN6RhQ0VC2D5A5l95j8GfnAxlaNITP8/2S6AuFrMkglmCKxxCiD8ZqSuAo+Lvz/yiOxAnb2UzRniJ52yvto2IRQav4v56iW/c3ozgQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=os.amperecomputing.com; dmarc=pass action=none header.from=amperemail.onmicrosoft.com; dkim=pass header.d=amperemail.onmicrosoft.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amperemail.onmicrosoft.com; s=selector1-amperemail-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=VO3A0T6YXPBiOJxaagHYwhcmBj+2fqcr1nAF4wNnPWw=; b=C6tB1fOSg5oKmnueSAq+NMhlow1Q6UJKvrMP7feT2vUpExKULvgB+zlctCcgo7ibeMMhc4aXCbpdMkmL25Xs+cKDAX7vQgO2Fd7jFDHBb1js5+A45mb1NOohQ5svZryz2a7On4NaQWOidA/KXiPeE+Ip7zHwpE9qbG+6aidHf8I= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amperemail.onmicrosoft.com; Received: from DM8PR01MB6824.prod.exchangelabs.com (2603:10b6:8:23::24) by PH0PR01MB6199.prod.exchangelabs.com (2603:10b6:510:1b::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6652.28; Wed, 9 Aug 2023 09:38:01 +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.6652.026; Wed, 9 Aug 2023 09:38:00 +0000 Message-ID: Date: Wed, 9 Aug 2023 17:37:51 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.14.0 Subject: Re: [PATCH] perf/core: fix the bug in the event multiplexing To: Mark Rutland , Oliver Upton Cc: Huang Shijie , maz@kernel.org, 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, 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 References: <20230809013953.7692-1-shijie@os.amperecomputing.com> From: Shijie Huang In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-ClientProxiedBy: CH0PR13CA0048.namprd13.prod.outlook.com (2603:10b6:610:b2::23) To DM8PR01MB6824.prod.exchangelabs.com (2603:10b6:8:23::24) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DM8PR01MB6824:EE_|PH0PR01MB6199:EE_ X-MS-Office365-Filtering-Correlation-Id: 160eee96-7ea7-4944-0634-08db98bc524f X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9/O//dE58QJemE8K0ohmr9h7OwyEs5aM7sRoelgRNgwzm5cSwF0JRrEYrFU7j6znFcfWjnRG+wplCHA0ESkqKsAUq1SkUEaI7HxQo2lX/ypW2BVKZEQOl7jKl1peYPfX1PCBRxu6qIGwHdckbsOaUxfl01dLncy0MCzWmGJYGBh8q2b+hiTdsCfAV9z+YG+rxlxna6njQIlhwY+WhuL67fIcnPw/V/xCpwKQXQKot83bDqVmxemC8GHUCueNZN0CQVvaUa3m47RVuEwXeu6aA0IVfR0Q4XuZvV0rM+cbbbvEsXRnUcwrWzBj+jOVTN4CHdGrZl/NT3qd23rnUT6Ptx5gltdnrWXriYLqzUVuxxEHBYd234MRPNLWwLAhRdCZZYLXcc6M9GY/nFF1PncTl40lf2r613k6DQusBNg9ecUDF1c1EW0iumd8s8Hgqd2VSxHyKJFpRRgeqMtT3AKOVV14lNOJQykxxdubqbpmutnAICpoDf4d5l66fEK5OcpZmNoKVPLOSGwrt27Gj6xSHkQ9s5ixgbFh4MJQwUHkiD1pVlgDK0G6SoHRtw7QpLsAaevpODTcpbyCkpX6WIEu3eZmr7GkMoITYottDJNzqOHcn1b9k30OkCLt+lb8R6SC3019mlPhQ/aUtlZ3B21NRQ== 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)(4636009)(346002)(39850400004)(376002)(366004)(136003)(396003)(1800799006)(186006)(451199021)(110136005)(478600001)(4326008)(66946007)(66556008)(107886003)(66476007)(6506007)(26005)(6486002)(6666004)(6512007)(2906002)(8676002)(316002)(41300700001)(7416002)(5660300002)(83170400001)(8936002)(31696002)(38350700002)(38100700002)(2616005)(83380400001)(42882007)(52116002)(31686004)(43740500002);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?R1V2UDNqbVFacEcxbFUxYVFQdnJXQnF0b1ZLSWhLYUVlSC9vVmZxVDZXa1FW?= =?utf-8?B?R0xaN0tpbFJDU2xvckdaYkpyUjdaTFVoOEt6QmVwVEUrWng5QjhwSGt3SlZP?= =?utf-8?B?cThpaGpBZUZoVVV3TVN4bnF6YjNkd1pReFJCdm9aNHJRYks2SW90YWMxZWZL?= =?utf-8?B?cTFFaW9uc0xDL2ZWaGphWGplTTlObjJDLzZyT1NGVElvbDcyVkhKM3dib3pw?= =?utf-8?B?TzZZZGgwdWVPVEdBSHNUSmlYUVpOa1R0ZTY0VUVROWZqYXlKbE5pMzExWHcy?= =?utf-8?B?aGZBWkdjWm5NY0RUbUJpM2NlU3ZMU0k1TktjNFpFbDV2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Tm01QXN0WTlTSnlud2JpUnJvTlRVV3dGYkZiaVBmalh3TjVoUmMwZEY2?= =?utf-8?Q?IFY7bs2mhnemDGYkWnf6JE8=3D?= X-OriginatorOrg: amperemail.onmicrosoft.com X-MS-Exchange-CrossTenant-Network-Message-Id: 160eee96-7ea7-4944-0634-08db98bc524f X-MS-Exchange-CrossTenant-AuthSource: DM8PR01MB6824.prod.exchangelabs.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 09 Aug 2023 09:38:00.7271 (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: PWm/LHbhhp+cCwYwrMubagV9SaUiGJztWI/S3oeiV9k8o+tqVUp3gZgoPTSgu4AEuhQcIi68dABXLhGNwIwlNbAM6fwmy3vdq4gI5u+nxJ/ydK9l5k9sm0rEhwBJMfyz X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR01MB6199 X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,NICE_REPLY_A,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Mark, 在 2023/8/9 17:22, Mark Rutland 写道: > On Wed, Aug 09, 2023 at 08:25:07AM +0000, Oliver Upton wrote: >> Hi Huang, >> >> On Wed, Aug 09, 2023 at 09:39:53AM +0800, Huang Shijie wrote: >>> 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 >>> event multiplexing in hrtimer: >>> merge_sched_in() -->perf_mux_hrtimer_restart() --> >>> perf_rotate_context(). >>> >>> In the perf_rotate_context(), it does not restore some PMU registers >>> as context_switch() does. In context_switch(): >>> kvm_sched_in() --> kvm_vcpu_pmu_restore_guest() >>> kvm_sched_out() --> kvm_vcpu_pmu_restore_host() >>> >>> So we got wrong result. >> This is a rather vague description of the problem. AFAICT, the >> issue here is on VHE systems we wind up getting the EL0 count >> enable/disable bits backwards when entering the guest, which is >> corroborated by the data you have below. > Yep; IIUC the issue here is that when we take an IRQ from a guest and reprogram > the PMU in the IRQ handler, the IRQ handler will program the PMU with > appropriate host/guest/user/etc filters for a *host* context, and then we'll > return back into the guest without reconfigurign the event filtering for a > *guest* context. Yes. > > That can happen for perf_rotate_context(), or when we install an event into a > running context, as that'll happen via an IPI. > >>> +void arch_perf_rotate_pmu_set(void) >>> +{ >>> + if (is_guest()) >>> + kvm_vcpu_pmu_restore_guest(NULL); >>> + else >>> + kvm_vcpu_pmu_restore_host(NULL); >>> +} >>> + >> This sort of hook is rather nasty, and I'd strongly prefer a solution >> that's confined to KVM. I don't think the !is_guest() branch is >> necessary at all. Regardless of how the pmu context is changed, we need >> to go through vcpu_put() before getting back out to userspace. >> >> We can check for a running vCPU (ick) from kvm_set_pmu_events() and either >> do the EL0 bit flip there or make a request on the vCPU to call >> kvm_vcpu_pmu_restore_guest() immediately before reentering the guest. >> I'm slightly leaning towards the latter, unless anyone has a better idea >> here. > The latter sounds reasonable to me. okay. I prefer the latter one now. :) Thanks Huang Shijie > > I suspect we need to take special care here to make sure we leave *all* events > in a good state when re-entering the guest or if we get to kvm_sched_out() > after *removing* an event via an IPI -- it'd be easy to mess either case up and > leave some events in a bad state. > > Thanks, > Mark.