Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752549AbcB2J6g (ORCPT ); Mon, 29 Feb 2016 04:58:36 -0500 Received: from mail-by2on0072.outbound.protection.outlook.com ([207.46.100.72]:59833 "EHLO na01-by2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752007AbcB2J6d (ORCPT ); Mon, 29 Feb 2016 04:58:33 -0500 Authentication-Results: spf=none (sender IP is 165.204.84.222) smtp.mailfrom=amd.com; alien8.de; dkim=none (message not signed) header.d=none;alien8.de; dmarc=permerror action=none header.from=amd.com; X-WSS-ID: 0O3AZPD-08-X2I-02 X-M-MSG: Date: Tue, 1 Mar 2016 02:00:50 +0800 From: Huang Rui To: Thomas Gleixner CC: Borislav Petkov , Peter Zijlstra , "Ingo Molnar" , Andy Lutomirski , "Robert Richter" , Jacob Shin , "Arnaldo Carvalho de Melo" , Kan Liang , , , , Suravee Suthikulpanit , Aravind Gopalakrishnan , Borislav Petkov , "Fengguang Wu" , Guenter Roeck Subject: Re: [PATCH v5] perf/x86/amd/power: Add AMD accumulated power reporting mechanism Message-ID: <20160229180048.GB20610@hr-amur2> References: <1456479650-4942-1-git-send-email-ray.huang@amd.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-EOPAttributedMessage: 0 X-Forefront-Antispam-Report: CIP:165.204.84.222;CTRY:US;IPV:NLI;EFV:NLI;SFV:NSPM;SFS:(10009020)(6009001)(2980300002)(428002)(199003)(164054003)(189002)(24454002)(47776003)(87936001)(33716001)(86362001)(101416001)(50466002)(76176999)(50986999)(92566002)(4001350100001)(54356999)(46406003)(1076002)(33656002)(106466001)(189998001)(110136002)(97756001)(1096002)(1220700001)(105586002)(11100500001)(586003)(77096005)(83506001)(2950100001)(5008740100001)(2906002)(4326007)(107986001);DIR:OUT;SFP:1101;SCL:1;SRVR:DM3PR12MB0858;H:atltwp02.amd.com;FPR:;SPF:None;MLV:sfv;A:1;MX:1;LANG:en; X-MS-Office365-Filtering-Correlation-Id: 812288c3-a590-43f0-5f3d-08d340eee01a X-Microsoft-Exchange-Diagnostics: 1;DM3PR12MB0858;2:nQbP/q8HY6fseKq1DTsjTPVuKu9bqAYzdUZP1bkS088d7n+3JW5/ig9lZogS5yZafFlYOo8llxXZeUtNWUvQwXvsqH8qgr0Ld4pCVghfJgkLy8TFA+Yuj/V2IQQproRN3lJwaUh/ny5Non5/9AlAu0dML/EoHZA10blA0dEepGjYfcoWiMUXagI+AbYjQAIQ;3:JyMCjAREiZyxFaYmC0s1j8V0pVK1gEag9oSfGaEWYx3i5xsUK+v3SDjZp7ymxLUY+4NueBRl+n4awlhy1TvC9sfCG8Pp94VS9gInsD86Rbw2rXlwOxH2itYrrTTd9i9aIC6NvBamAG5wXqKVHBiYvk+/Dgx59kXu6IGFI67o1WFu+Tec7KK5/HMpdV1vK/dDieoQ13/6qhw8NFKfc5pOSr8x8wq/7Flao/7h7OA5sLI=;25:ezox+QXWdud1jeUYjXzCpkL4oHGZK3msl8HrbSjQV22jqqxdmpGvnOlvahyRzT3va1Mo59S1tGvpqcCnmHNShToP5GWelEQdiRQvsvY/6m52jtZcScy8EbpJ4ksdHg+xSjR1NgheLygwsTeJJziXfP5B5egASxuifZjdrvBoiiICHpXkUcY/lKklcVBIUJ3IegOTykgqS2X98/BMLxFxLqf960aQr9FvbunbodzaTUWmTqJEbj31UePjOeGL0g9luPdWOZ5ujiLzyyKw4h8OstHZ2PijMvpESjHrGC+ZkSfOzqdaFop/YH4jKTjXDBjleh4yGdGXAEs3M9EIfxM15syLHA5Vm2iTHimM//cVrb8= X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:DM3PR12MB0858; X-Microsoft-Exchange-Diagnostics: 1;DM3PR12MB0858;20:nZ00T6bFqxnIiFfgXSTVgn7BSKewOE01sTrBTmLUYPdZltVP7CjJjPnKXau591PHnQele0QOomne7jo0LpI6h5xBcdpwDR5VHgk8Gc0OUXZoXUZLts/EO5aoT7Mev4dZTchQsR+/CXoKgscFMmlE77taQicUeegSmilW2cZ6TAaeOiiaq4S9YPwJkkKArmWvuEnbar0O1JPNH4LD2fosTEg9q54Ry/91EheFwhzrxmOYMIRtj5sJ/BBQ/tM7tEO2lK2m43ROphIW/JznZ4XsnaueB9s7srbL32WoJKfIjTOu3CiZkY9vxMtN6WM+bhh6t7xdHy1Xy6MuRGpAYSQbmdfWTLuJIvzNgc/ZRwqmoHpwrpqimvy/h7mxvetKN/Pqj5V30J/MPoCzGtT2fEA4vypCDYhgdM+Qt4+FvB6H2bokCgSOp2wZBgx08vn5lOm0AN2lTcJapNIqV/CXAVHSTENVi6+aTCVyhuulufYjpW3OTGsrfNLrYpWP7od6ClKk X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:; X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(601004)(2401047)(13023025)(13017025)(5005006)(13015025)(8121501046)(13024025)(13018025)(3002001)(10201501046);SRVR:DM3PR12MB0858;BCL:0;PCL:0;RULEID:;SRVR:DM3PR12MB0858; X-Microsoft-Exchange-Diagnostics: 1;DM3PR12MB0858;4:GLeqFM2O6xoNOC5r9PdY0nZekyB3nsRagZp1fNOSF8SX5dt0QBPWB1zTbib25bbGQASlZZ1nU0+nB157npCDaouNJPVS2DWWGv3vGL2dokvdks17C+QMak25x/D+LuumaazcUwfg5m9PKFzEn2yl+teUhSrfYNqsPhtc4Y3sMfZrP8CKAds/h4qWn2WOKrqoLfo2+TpO+C8Mxt8bOs5+amQiaiM8s2xIJ5nl0abhDdYbRm6bHe2NmaYcC7XT5N1n6FZvDAAMnSpfQRtB1gwEw9viHso5CIc7SW6K/9pqMf1ZtDPUl+h0U57iYYTcuLMBR3hKhJlxzUX7Dqt5REJGAz8hkry346lXubtSrhXTMH/6nSG4UgIM8J+X666yJyp8tBQZkl2b/7SN5W9ddZ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us-ascii?Q?/veRAY48y0G0R0m9HNqObP/pyEmfZqH0d3xLMIyyprEoVCh/4mCSzyDgyHg9?= =?us-ascii?Q?xpZkb9yvEXvbdb9ibBv7EXKsKmts4hNFX8MzxcovhE+NYvJPqbvCcEcRBzbe?= =?us-ascii?Q?lURpWSpzVTkheSeFRqBkq9zT1EE1dXRym2XUqvUOWRaMEXAF1Ba0veYc+Vmc?= =?us-ascii?Q?6KzbicksYBDxZCvvAZX1oVbHARj3Ol11MdvOiMJNA=3D=3D?= X-Microsoft-Exchange-Diagnostics: 1;DM3PR12MB0858;5:Y9Xzti+Sj83hfwQbDCBAUDLqX7a6w24LDWrFvcT7cwYUIqfWDOb+BTOVU0U0y+/WP4ZTGmfrIylumApsRiJgYGlqvvuRX+5vEHA+Z7LDkwK88hDqGqsfLKRNkB0B6kJ/bJesve8uNB6RVZ+390eLHQ==;24:8fjghjjnWYCfsrM24ysNA+Yqqj92TDDhVu8LGHifkaHr3c89jsffu2uXsb/QVphFsy5o/2Atj01mn617aptMzVNaPvK84vZQWPkkb3A4pWE=;20:H9fIVMjjJqV7bTHoJJiJMiEE3tQaZxMIkPSCM7FyNKbclmZAIYs9KlTGZMA243eaHDzpU/0gSnxztz47NfdNfZG5YSwIc/TDlW5W/LtsCHVYIb/WJ1HkXlXHh8hmRdzGqyaq2Ko0+nwjI+zgJk4FWeQ3IKumpmaI0rgI/drwSO2zC1Mid6XQdIkMYWVAoDRUx0hOuZ+4iquYwrg8bZzIsHfWofkQzfuejf9oFRlV6yGFvYRaJ5YySjKnrzlwdmDq SpamDiagnosticOutput: 1:23 SpamDiagnosticMetadata: NSPM X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Feb 2016 09:58:28.5902 (UTC) X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.222];Helo=[atltwp02.amd.com] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM3PR12MB0858 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Thomas, Thank you to your comments. :-) On Fri, Feb 26, 2016 at 11:18:28AM +0100, Thomas Gleixner wrote: > On Fri, 26 Feb 2016, Huang Rui wrote: > > > + > > +static DEFINE_PER_CPU(struct power_pmu_masks *, amd_power_pmu); > > This is complete overkill, really. > > > +static int power_cpu_exit(int cpu) > > +{ > > + struct power_pmu_masks *pmu = per_cpu(amd_power_pmu, cpu); > > + int target = nr_cpumask_bits; > > + int ret = 0; > > + > > + cpumask_copy(pmu->mask, topology_sibling_cpumask(cpu)); > > + > > + cpumask_clear_cpu(cpu, &cpu_mask); > > + cpumask_clear_cpu(cpu, pmu->mask); > > + > > + if (!cpumask_and(pmu->tmp_mask, pmu->mask, cpu_online_mask)) > > + goto out; > > + /* > > + * Find a new CPU on the same compute unit, if was set in cpumask > > + * and still some CPUs on compute unit. Then move on to the new CPU. > > + */ > > + target = cpumask_any(pmu->tmp_mask); > > + if (target < nr_cpumask_bits && target != cpu) > > + cpumask_set_cpu(target, &cpu_mask); > > + > > + WARN_ON(cpumask_empty(&cpu_mask)); > > + > > +out: > > + /* > > + * Migrate event and context to new CPU. > > + */ > > + if (target < nr_cpumask_bits) > > + perf_pmu_migrate_context(&pmu_class, cpu, target); > > + > > + return ret; > > Sigh. That whole thing can be simply done with: > > if (!cpumask_test_and_clear_cpu(cpu, &cpu_mask)) > return; > > target = cpumask_any_but(topology_sibling_cpumask(cpu), cpu); > if (target < nr_cpumask_bits) { > cpumask_set_cpu(target, &uncore_cpu_mask); > perf_pmu_migrate_context(&pmu_class, cpu, target); > } > > > > + > > +} > > + > > +static int power_cpu_init(int cpu) > > +{ > > + struct power_pmu_masks *pmu = per_cpu(amd_power_pmu, cpu); > > + > > + if (!pmu) > > + return 0; > > + > > + if (!cpumask_and(pmu->mask, topology_sibling_cpumask(cpu), &cpu_mask)) > > + cpumask_set_cpu(cpu, &cpu_mask); > > And that's equally convoluted and can be done with: > > target = cpumask_any_and(&cpu_mask, topology_sibling_cpumask(cpu)); > if (target < nr_cpu_ids) > cpumask_set_cpu(cpu, &cpu_mask); > > > +static int power_cpu_prepare(int cpu) > > +{ > > + struct power_pmu_masks *pmu = per_cpu(amd_power_pmu, cpu); > > + int phys_id = topology_physical_package_id(cpu); > > + int ret = 0; > > + > > + if (pmu) > > + return 0; > > + > > + if (phys_id < 0) > > + return -EINVAL; > > + > > + pmu = kzalloc_node(sizeof(*pmu), GFP_KERNEL, cpu_to_node(cpu)); > > + if (!pmu) > > + return -ENOMEM; > > + > > + if (!zalloc_cpumask_var(&pmu->mask, GFP_KERNEL)) { > > + ret = -ENOMEM; > > + goto out; > > + } > > + > > + if (!zalloc_cpumask_var(&pmu->tmp_mask, GFP_KERNEL)) { > > + ret = -ENOMEM; > > + goto out1; > > + } > > + > > + per_cpu(amd_power_pmu, cpu) = pmu; > > + return 0; > > + > > +out1: > > + free_cpumask_var(pmu->mask); > > +out: > > + kfree(pmu); > > + > > + return ret; > > +} > > + > > +static void power_cpu_kfree(int cpu) > > +{ > > + struct power_pmu_masks *pmu = per_cpu(amd_power_pmu, cpu); > > + > > + if (!pmu) > > + return; > > + > > + free_cpumask_var(pmu->mask); > > + free_cpumask_var(pmu->tmp_mask); > > + kfree(pmu); > > + > > + per_cpu(amd_power_pmu, cpu) = NULL; > > +} > > Both prepare and free can go away. > OK, I got your point. After enhounce the implementation of cpu notifier part of this driver, we can avoid to use weird "DEFINE_PER_CPU(struct power_pmu_masks *, amd_power_pmu)". Thanks. > > +static int __init amd_power_pmu_init(void) > > +{ > > + int i, ret; > > + u64 tmp; > > + > > + if (!x86_match_cpu(cpu_match)) > > + return 0; > > + > > + if (!boot_cpu_has(X86_FEATURE_ACC_POWER)) > > + return -ENODEV; > > + > > + cores_per_cu = amd_get_cores_per_cu(); > > + cu_num = boot_cpu_data.x86_max_cores / cores_per_cu; > > Please use the new package management functions which are on the way to tip. > Can you give me some hints? > > + > > + if (WARN_ON_ONCE(cu_num > MAX_CUS)) > > + return -EINVAL; > > + > > + cpu_pwr_sample_ratio = cpuid_ecx(0x80000007); > > + > > + if (rdmsrl_safe(MSR_F15H_CU_MAX_PWR_ACCUMULATOR, &tmp)) { > > + pr_err("Failed to read max compute unit power accumulator MSR\n"); > > + return -ENODEV; > > + } > > + max_cu_acc_power = tmp; > > + > > + cpu_notifier_register_begin(); > > + > > + /* Choose one online core of each compute unit. */ > > + for (i = 0; i < boot_cpu_data.x86_max_cores; i += cores_per_cu) { > > + WARN_ON(cpumask_empty(topology_sibling_cpumask(i))); > > + cpumask_set_cpu(cpumask_any(topology_sibling_cpumask(i)), &cpu_mask); > > + } > > > > > + > > + for_each_present_cpu(i) { > > + ret = power_cpu_prepare(i); > > + if (ret) { > > + /* Unwind on [0 ... i-1] CPUs. */ > > + while (i--) > > + power_cpu_kfree(i); > > + goto out; > > + } > > Not needed. > > > + ret = power_cpu_init(i); > > + if (ret) { > > + /* Unwind on [0 ... i] CPUs. */ > > + while (i >= 0) > > + power_cpu_kfree(i--); > > + goto out; > > + } > > You cannot issue the CPU STARTING callback on present, but not online cpus. > Do you mean we should change for_each_present_cpu to for_each_online_cpu? My orignal intent here, it's to allocate data structures of "power_pmu_masks" for each core. But now, I think we needn't below codes, right? for_each_online_cpu(i) power_cpu_init(i); > > + } > > + > > + __perf_cpu_notifier(power_cpu_notifier); > > + > > + ret = perf_pmu_register(&pmu_class, "power", -1); > > + if (WARN_ON(ret)) { > > + pr_warn("AMD Power PMU registration failed\n"); > > > So that leaves the notifier installed and leaks all the allocated memory. > OK, do you mean "issue CPU_STARTING callback on present cpus" will cause the memory leak here? Could you please explain more? > > + goto out; > > + } > > + > > + pr_info("AMD Power PMU detected, %d compute units\n", cu_num); > > + > > +out: > > + cpu_notifier_register_done(); > > + > > + return ret; > > +} > > +device_initcall(amd_power_pmu_init); > > Please make this as a module right away. There is no reason to compile all > this in. > OK, will update. Thanks, Rui