Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3910183ybl; Tue, 21 Jan 2020 09:15:30 -0800 (PST) X-Google-Smtp-Source: APXvYqy5BU2cFbrHyYqeoGIUUl8lhsLJ7vl/mAunzIrGEeus0kYSpC+xiezWJbXgiFPvp9p8UV7A X-Received: by 2002:aca:fcd0:: with SMTP id a199mr3580982oii.143.1579626930452; Tue, 21 Jan 2020 09:15:30 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1579626930; cv=pass; d=google.com; s=arc-20160816; b=R6CA2fPP7p4y5P/FRZaoncFWrsjo6udP3xvb4DFhPpZ9HYHpDsYt/ERpSITMwNkiju Q5BOl/IVpM1iwb8/8q+v2h+ZGD4b4qlpo9NKeCsTFymuPfUkKwlz2fkkKl7ptKDjgadG izPQHD/ihuSMo4cj0ho22MQYlsj1S+6zW9qZdyILt/LNLTlhBpeLgNF+19Gu4ybgUhy0 cxWSfP8225rjmTOtPt+z7/AYI+BvGz2BATv1s6cg8No6hmbv84HU7zCHdmHdjKre9Dye q1JD+x9kFZkPg/Zrhl+JZOb/+k05w1EltVDTWeY91cjmaDmjnTFkxq9ADoUVzmE0ntie YNqA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=O1hcWWXjsopH3dd6OiBrCbx8YTvgHq0ZBcaVWbi5ipI=; b=Rm2cWavZ1Tz6cipeSm/4cQ7Aqsek7ErQkp6kZjG5k5OS40Ej0a4s7RkXLbLID57DdA eZadToMYIrIzEz070eSTXsLzHcSjMmTALFVhGkWaPui4aA/fsowYSn6Sg45WfRZAOKaF ymqvgcQuxHXR6pB++9/AVUU1bXpuwTLSvfhjzy7I1xSovFfEjxBXHHASxXh6rdiwNYIf 18s+AVn+JD65g9r5zWDw3v/BQDdYm6vz7NSV6fcydkq9raAW4OJHUdxYmhRH2YO5tqnd kWrWjFS75TCNN/T8cZcD0oWn9GT47RadjgwhQo6GIdLjD5J/weXROKGkDePhpNfFnlaJ mTWg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=2Z2ylBdF; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h9si21427223otb.49.2020.01.21.09.15.17; Tue, 21 Jan 2020 09:15:30 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=2Z2ylBdF; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729340AbgAURNC (ORCPT + 99 others); Tue, 21 Jan 2020 12:13:02 -0500 Received: from mail-dm6nam12on2066.outbound.protection.outlook.com ([40.107.243.66]:43169 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729127AbgAURNC (ORCPT ); Tue, 21 Jan 2020 12:13:02 -0500 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ixL9CLdZHwXAT38QWn9u+WfZBq1EeaCImnr7R28klhXSLN8XVLG+qTrmMXb51WxuiFSUiWthcQEfJks4PNZat3sfDtPdLdnFStj+Y0k+hTpWXBbRdioNZzPmWNXe8sGh0tKF+3A7ugxOibgIS1x+f/UB3SY5EUa3fFPJrCzuUmjCJnOfeA5RrEP8hGn05xdLUtfq9LGds+VCC79f9e5F6uazu/pG7VrPiApDSB/6M6zWkQppmsRfEkwgsfaE4jvMLznC+ZJqW97e1/QclyMtZy62tIzn42Ue8WnYM0j9cHNz8ocuTjFb0EcORQ9oUcBMf/9CQpNyFJO0CPjvzHGTkg== 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-SenderADCheck; bh=O1hcWWXjsopH3dd6OiBrCbx8YTvgHq0ZBcaVWbi5ipI=; b=MTrueZQP9peGYp3r0hhp9t9cgGRMdtKmQmfMklt4OQRa/wYaha5VVUNW+pwlky+aFhTYO56uUJht+yb+N78T8g4OFBSB/Rqd29z4sn4k0Pzp4qjPqP6m7vnB5qJ37Ron6CEhYQ9LbnPiMR811EjbUW5ONje3ZKrYFb1GmptJtdZdbUDyxvIuDD8wVSsabR5p/czcZ7kCZE772S/j5On07gE1+OZvPKbhQ8Eu6lVzEtEP54spR2e6dgrDashKQTz5BeivEXVxTc5oU47Armwgu0bL2qmHXoAPIdSWpnBdUmt+uz8IzyRiBTP26zUJiGpHiUprJRsBjuGAu7QYzF+FaA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=O1hcWWXjsopH3dd6OiBrCbx8YTvgHq0ZBcaVWbi5ipI=; b=2Z2ylBdFQots70XtnMovIfxoXib+87t91CFZdvw1yctdl8K4s+ArzcYb0AA9JQlf8Ppir2jTNG/aW061ei7QkUyYxscWMFfmeEex2dSJc+dh9gUf9mdy48k6qnlxajDD9I8NYkIjFbZRUIzsS9qtCs7qbqYk24Pvtp+wLWzTvGo= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=kim.phillips@amd.com; Received: from SN6PR12MB2845.namprd12.prod.outlook.com (52.135.106.33) by SN6PR12MB2703.namprd12.prod.outlook.com (52.135.106.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2644.20; Tue, 21 Jan 2020 17:12:56 +0000 Received: from SN6PR12MB2845.namprd12.prod.outlook.com ([fe80::48af:8c71:edee:5bc]) by SN6PR12MB2845.namprd12.prod.outlook.com ([fe80::48af:8c71:edee:5bc%7]) with mapi id 15.20.2644.026; Tue, 21 Jan 2020 17:12:56 +0000 From: Kim Phillips To: Peter Zijlstra , Ingo Molnar , Ingo Molnar , kim.phillips@amd.com Cc: Alexander Shishkin , Andi Kleen , Arnaldo Carvalho de Melo , Babu Moger , Borislav Petkov , Fenghua Yu , Frank van der Linden , "H . Peter Anvin" , Huang Rui , Janakarajan Natarajan , Jan Beulich , Jiaxun Yang , Jiri Olsa , Josh Poimboeuf , Linus Torvalds , Luwei Kang , =?UTF-8?q?Martin=20Li=C5=A1ka?= , Matt Fleming , Namhyung Kim , Paolo Bonzini , Pawan Gupta , Suravee Suthikulpanit , Thomas Gleixner , Tom Lendacky , x86@kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org Subject: [PATCH 2/2 v2] x86/cpu/amd: Enable the fixed intructions retired free counter IRPERF Date: Tue, 21 Jan 2020 11:12:32 -0600 Message-Id: <20200121171232.28839-2-kim.phillips@amd.com> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200121171232.28839-1-kim.phillips@amd.com> References: <20200121171232.28839-1-kim.phillips@amd.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-ClientProxiedBy: DM6PR17CA0025.namprd17.prod.outlook.com (2603:10b6:5:1b3::38) To SN6PR12MB2845.namprd12.prod.outlook.com (2603:10b6:805:75::33) MIME-Version: 1.0 Received: from fritz.amd.com (165.204.77.1) by DM6PR17CA0025.namprd17.prod.outlook.com (2603:10b6:5:1b3::38) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2644.20 via Frontend Transport; Tue, 21 Jan 2020 17:12:53 +0000 X-Mailer: git-send-email 2.25.0 X-Originating-IP: [165.204.77.1] X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: a10f20cd-1577-4c5c-5f95-08d79e952846 X-MS-TrafficTypeDiagnostic: SN6PR12MB2703:|SN6PR12MB2703: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2150; X-Forefront-PRVS: 0289B6431E X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4636009)(39860400002)(136003)(396003)(346002)(376002)(366004)(189003)(199004)(186003)(16526019)(8676002)(26005)(8936002)(478600001)(966005)(81166006)(6486002)(2616005)(81156014)(956004)(2906002)(1076003)(316002)(66574012)(4326008)(36756003)(44832011)(86362001)(5660300002)(7696005)(52116002)(54906003)(7416002)(110136005)(6666004)(66946007)(66556008)(66476007);DIR:OUT;SFP:1101;SCL:1;SRVR:SN6PR12MB2703;H:SN6PR12MB2845.namprd12.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;MX:1;A:1; Received-SPF: None (protection.outlook.com: amd.com does not designate permitted sender hosts) X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: PryH9lGOmhD38Uo3kvM0M6wgTafMoAZj2nccPTfZ5rW+bBfCtuM1ANTIrB6DMje8/aYeP9RebvQbdrdCdnVr4UKHbeDzxP0BvKAv15E/vraGFMK5PNTqnrV1BrAJNzQ3YUB6Q6fWZFW5BJ2ksg4RKjPCm+PVTF8+yrumM6Fw1UV/S0sXP3ZE/jP5MhkVXl/dB/lWiUCLxbDe/hWQn2oFJJb5iXQmdi7ozgULTURScKcMjCRg8CBGzezNufILmm1lh82qSxDdTiayWhp7zTIas4i4rF3n8ZwTDlhgGbW/O2Z86m0EBiI9rJRovWLzB8ceWzBzbZZcuMDV8O3MEOT4cYGg/wofwrpsZvezmG99VolujElB2cJJ+CUFGbF5SIhgjja2KL6HXzXoQ1nMggLhekdx9H68mvAg9UXhBxJM/ws+/tNP6ltXBwD+AEKNUIX9Z7AXnx57gjSV91rvkUf19TV0ZxKwOTRLw13n1DFEAKYa7rUDkxlbdcPAY8jaNeA9vfK4lPLHsSpIOga5KTVXvw== X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: a10f20cd-1577-4c5c-5f95-08d79e952846 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Jan 2020 17:12:56.0740 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: RB1Br0Pv+wFtYAgt0qdOkIaXmsrfQvb9iLa8tbII3mzl/q6rB4kDf2X62lp+8QFh2F5LJAqba03EMQOCQ+GdTQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2703 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org commit aaf248848db50 ("perf/x86/msr: Add AMD IRPERF (Instructions Retired) performance counter") added support for 'perf -e msr/irperf/', but when exercised, we always get a 0 count: BEFORE: $ sudo perf stat -e instructions,msr/irperf/ true Performance counter stats for 'true': 624,833 instructions # 0.00 stalled cycles per insn 0 msr/irperf/ It turns out it simply needs its enable bit - HWCR bit 30 - set. This patch does just that. Enablement is restricted to all machines advertising IRPERF capability, except those susceptible to an erratum that makes the IRPERF return bad values. That erratum occurs in Family 17h models 00-1fh [1], but not in F17h models 20h and above [2]. AFTER (on a family 17h model 31h machine): $ sudo perf stat -e instructions,msr/irperf/ true Performance counter stats for 'true': 621,690 instructions # 0.00 stalled cycles per insn 622,490 msr/irperf/ [1] "Revision Guide for AMD Family 17h Models 00h-0Fh Processors", currently available here: https://www.amd.com/system/files/TechDocs/55449_Fam_17h_M_00h-0Fh_Rev_Guide.pdf [2] "Revision Guide for AMD Family 17h Models 30h-3Fh Processors", currently available here: https://developer.amd.com/wp-content/resources/56323-PUB_0.74.pdf Cc: Alexander Shishkin Cc: Andi Kleen Cc: Arnaldo Carvalho de Melo Cc: Babu Moger Cc: Borislav Petkov Cc: Fenghua Yu Cc: Frank van der Linden Cc: H. Peter Anvin Cc: Huang Rui Cc: Ingo Molnar Cc: Ingo Molnar Cc: Janakarajan Natarajan Cc: Jan Beulich Cc: Jiaxun Yang Cc: Jiri Olsa Cc: Josh Poimboeuf Cc: Linus Torvalds Cc: Luwei Kang Cc: Martin Liška Cc: Matt Fleming Cc: Namhyung Kim Cc: Paolo Bonzini Cc: Pawan Gupta Cc: Peter Zijlstra Cc: Suravee Suthikulpanit Cc: Thomas Gleixner Cc: Tom Lendacky Cc: x86@kernel.org Cc: linux-kernel@vger.kernel.org Cc: stable@vger.kernel.org Fixes: aaf248848db50 ("perf/x86/msr: Add AMD IRPERF (Instructions Retired) performance counter") Signed-off-by: Kim Phillips --- v2: Based on Andi Kleen's review: https://lore.kernel.org/lkml/20200116040324.GI302770@tassilo.jf.intel.com/ Add an amd_erratum_1054 and use cpu_has_bug infrastructure instead of open-coding the {family,model} check. arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/kernel/cpu/amd.c | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index f3327cb56edf..1c1600e7476b 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -404,5 +404,6 @@ #define X86_BUG_SWAPGS X86_BUG(21) /* CPU is affected by speculation through SWAPGS */ #define X86_BUG_TAA X86_BUG(22) /* CPU is affected by TSX Async Abort(TAA) */ #define X86_BUG_ITLB_MULTIHIT X86_BUG(23) /* CPU may incur MCE during certain page attribute changes */ +#define X86_BUG_AMD_E1054 X86_BUG(24) /* CPU is among the affected by Erratum 1054 */ #endif /* _ASM_X86_CPUFEATURES_H */ diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index ebe1685e92dd..d5e517d1c3dd 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -512,6 +512,8 @@ #define MSR_K7_HWCR 0xc0010015 #define MSR_K7_HWCR_SMMLOCK_BIT 0 #define MSR_K7_HWCR_SMMLOCK BIT_ULL(MSR_K7_HWCR_SMMLOCK_BIT) +#define MSR_K7_HWCR_IRPERF_EN_BIT 30 +#define MSR_K7_HWCR_IRPERF_EN BIT_ULL(MSR_K7_HWCR_IRPERF_EN_BIT) #define MSR_K7_FID_VID_CTL 0xc0010041 #define MSR_K7_FID_VID_STATUS 0xc0010042 diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 62c30279be77..c067234a271f 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -28,6 +28,7 @@ static const int amd_erratum_383[]; static const int amd_erratum_400[]; +static const int amd_erratum_1054[]; static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum); /* @@ -701,6 +702,9 @@ static void early_init_amd(struct cpuinfo_x86 *c) if (cpu_has_amd_erratum(c, amd_erratum_400)) set_cpu_bug(c, X86_BUG_AMD_E400); + if (cpu_has_amd_erratum(c, amd_erratum_1054)) + set_cpu_bug(c, X86_BUG_AMD_E1054); + early_detect_mem_encrypt(c); /* Re-enable TopologyExtensions if switched off by BIOS */ @@ -978,6 +982,15 @@ static void init_amd(struct cpuinfo_x86 *c) /* AMD CPUs don't reset SS attributes on SYSRET, Xen does. */ if (!cpu_has(c, X86_FEATURE_XENPV)) set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS); + + /* + * Turn on the Instructions Retired free counter on machines not + * susceptible to erratum #1054 "Instructions Retired Performance + * Counter May Be Inaccurate" + */ + if (cpu_has(c, X86_FEATURE_IRPERF) && + !cpu_has_bug(c, X86_BUG_AMD_E1054)) + msr_set_bit(MSR_K7_HWCR, MSR_K7_HWCR_IRPERF_EN_BIT); } #ifdef CONFIG_X86_32 @@ -1105,6 +1118,10 @@ static const int amd_erratum_400[] = static const int amd_erratum_383[] = AMD_OSVW_ERRATUM(3, AMD_MODEL_RANGE(0x10, 0, 0, 0xff, 0xf)); +/* #1054: Instructions Retired Performance Counter May Be Inaccurate */ +static const int amd_erratum_1054[] = + AMD_OSVW_ERRATUM(0, AMD_MODEL_RANGE(0x17, 0, 0, 0x2f, 0xf)); + static bool cpu_has_amd_erratum(struct cpuinfo_x86 *cpu, const int *erratum) { -- 2.25.0