Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp2745798imj; Mon, 11 Feb 2019 07:52:55 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib/yE/bn6K8dU65ojLqUvCRgrTnl7pa4GqAQCyYeq3We3o1hSEDhoYt6iaYVSPDTBQyJdJb X-Received: by 2002:a65:624c:: with SMTP id q12mr34282720pgv.379.1549900375550; Mon, 11 Feb 2019 07:52:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549900375; cv=none; d=google.com; s=arc-20160816; b=BgG0+7sTFwhffsfXykzDEjWis3RcLyjBQ44yye7rc4HgJzM8xy1JYEIsboCStJwpDc 3qVNqE7HSSwMjvqVWR+byZ5htLJg0kchMnvhjBkLTMHQlstRF7/uMBdmtzm1Sgwagvd8 w4NtFCilvIIyqVlffSxp4l5NUFE9zCY4zib0DSq2JxFFBIej3laYN5nubfDexJH4LDc9 hGpxrsL2v9h7D1lvHxpK5rshButY6oB7KhbYIlBtlrkPK+OSkd7XIOBZ6sTcHey0bKM4 QnkfNfXNb1/xb5lPlDvypG/nvdKrkEzSUPoG3o8v6KJQCVCacDjjMFNHCOpGElMq1ZbI VYTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kUFv3mF27x7StHVnOu/rA9pjRX5/4cCmsX6jFVbQuGY=; b=hKHvdtm5hvtONSU7NGPxkreRiq++txV/6MGaJbkmHK1WGj2YjOMsSGadBwjyi6mbQ0 daPqOVV1ckwPzYMDgxUpsXZDORtEVou28/Ow7WvQcOCD3usxNTFzZtQO4N3Dta+1AK1I 3GO4F97MA0nwCnKq8Y/wuTr7+LbG7GTI7+BvCnpm9LqgxFmAoSY9Kg9TXVNZPdIKngoz RuJlyUAY8UuY4CttaioIQEMWaSAO15CpGJXKmOHwkgvV8gaxZgQSTERDI9yfLXlKs47u u72JaZ2rL68YYbiIthR9XXFRSPb0lMJ/Y/pSyYtD1YELSH55mk9B3MvhzO41pPuwR7Lb y6xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=OBipSYOX; 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 h17si9961308pgh.495.2019.02.11.07.52.39; Mon, 11 Feb 2019 07:52:55 -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=@kernel.org header.s=default header.b=OBipSYOX; 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 S1731599AbfBKOiJ (ORCPT + 99 others); Mon, 11 Feb 2019 09:38:09 -0500 Received: from mail.kernel.org ([198.145.29.99]:47686 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728985AbfBKOiG (ORCPT ); Mon, 11 Feb 2019 09:38:06 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 05AE721B18; Mon, 11 Feb 2019 14:38:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1549895885; bh=yRSTsrYDaN7SkIx5xSXLB1XbGBrVelzUtwpf8I+DefQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OBipSYOXmllMH2QPZSCpb5CU+bJEo/EyAabgKeTrZTNqgNkGRExZSzj4PJNKtAUCq 7sZWumAOIZjDYx9GK4XTwlgc4uyEB4lFIM310I4211BmJ874/xtwKHlrPPdyBvf33a OUDH7VxbIS6ENIF1CVhJ/TwtjV75AUv1LHyojopQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, He Zhe , Sebastian Andrzej Siewior , Alexander Shishkin , Arnaldo Carvalho de Melo , Jiri Olsa , Linus Torvalds , Peter Zijlstra , Thomas Gleixner , acme@kernel.org, bp@alien8.de, hpa@zytor.com, jolsa@kernel.org, kan.liang@linux.intel.com, namhyung@kernel.org, Ingo Molnar Subject: [PATCH 4.20 344/352] perf/x86/intel: Delay memory deallocation until x86_pmu_dead_cpu() Date: Mon, 11 Feb 2019 15:19:31 +0100 Message-Id: <20190211141909.069339947@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190211141846.543045703@linuxfoundation.org> References: <20190211141846.543045703@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.20-stable review patch. If anyone has any objections, please let me know. ------------------ From: Peter Zijlstra commit 602cae04c4864bb3487dfe4c2126c8d9e7e1614a upstream. intel_pmu_cpu_prepare() allocated memory for ->shared_regs among other members of struct cpu_hw_events. This memory is released in intel_pmu_cpu_dying() which is wrong. The counterpart of the intel_pmu_cpu_prepare() callback is x86_pmu_dead_cpu(). Otherwise if the CPU fails on the UP path between CPUHP_PERF_X86_PREPARE and CPUHP_AP_PERF_X86_STARTING then it won't release the memory but allocate new memory on the next attempt to online the CPU (leaking the old memory). Also, if the CPU down path fails between CPUHP_AP_PERF_X86_STARTING and CPUHP_PERF_X86_PREPARE then the CPU will go back online but never allocate the memory that was released in x86_pmu_dying_cpu(). Make the memory allocation/free symmetrical in regard to the CPU hotplug notifier by moving the deallocation to intel_pmu_cpu_dead(). This started in commit: a7e3ed1e47011 ("perf: Add support for supplementary event registers"). In principle the bug was introduced in v2.6.39 (!), but it will almost certainly not backport cleanly across the big CPU hotplug rewrite between v4.7-v4.15... [ bigeasy: Added patch description. ] [ mingo: Added backporting guidance. ] Reported-by: He Zhe Signed-off-by: Peter Zijlstra (Intel) # With developer hat on Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: Peter Zijlstra (Intel) # With maintainer hat on Cc: Alexander Shishkin Cc: Arnaldo Carvalho de Melo Cc: Jiri Olsa Cc: Linus Torvalds Cc: Peter Zijlstra Cc: Thomas Gleixner Cc: acme@kernel.org Cc: bp@alien8.de Cc: hpa@zytor.com Cc: jolsa@kernel.org Cc: kan.liang@linux.intel.com Cc: namhyung@kernel.org Cc: Fixes: a7e3ed1e47011 ("perf: Add support for supplementary event registers"). Link: https://lkml.kernel.org/r/20181219165350.6s3jvyxbibpvlhtq@linutronix.de Signed-off-by: Ingo Molnar Signed-off-by: Greg Kroah-Hartman --- arch/x86/events/intel/core.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3559,6 +3559,14 @@ static void free_excl_cntrs(int cpu) static void intel_pmu_cpu_dying(int cpu) { + fini_debug_store_on_cpu(cpu); + + if (x86_pmu.counter_freezing) + disable_counter_freeze(); +} + +static void intel_pmu_cpu_dead(int cpu) +{ struct cpu_hw_events *cpuc = &per_cpu(cpu_hw_events, cpu); struct intel_shared_regs *pc; @@ -3570,11 +3578,6 @@ static void intel_pmu_cpu_dying(int cpu) } free_excl_cntrs(cpu); - - fini_debug_store_on_cpu(cpu); - - if (x86_pmu.counter_freezing) - disable_counter_freeze(); } static void intel_pmu_sched_task(struct perf_event_context *ctx, @@ -3663,6 +3666,7 @@ static __initconst const struct x86_pmu .cpu_prepare = intel_pmu_cpu_prepare, .cpu_starting = intel_pmu_cpu_starting, .cpu_dying = intel_pmu_cpu_dying, + .cpu_dead = intel_pmu_cpu_dead, }; static struct attribute *intel_pmu_attrs[]; @@ -3703,6 +3707,8 @@ static __initconst const struct x86_pmu .cpu_prepare = intel_pmu_cpu_prepare, .cpu_starting = intel_pmu_cpu_starting, .cpu_dying = intel_pmu_cpu_dying, + .cpu_dead = intel_pmu_cpu_dead, + .guest_get_msrs = intel_guest_get_msrs, .sched_task = intel_pmu_sched_task, };