Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp5409405imu; Wed, 19 Dec 2018 10:35:09 -0800 (PST) X-Google-Smtp-Source: AFSGD/X2vC/gMefUUUoey80zPdn4+CwOzoNZlSmz2BW6+DML2YrcJ1sl6YoeSBHOcEMH5PQ7JXKE X-Received: by 2002:a65:5286:: with SMTP id y6mr20404955pgp.439.1545244509007; Wed, 19 Dec 2018 10:35:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545244508; cv=none; d=google.com; s=arc-20160816; b=sBhGTQd5mZuXGH3SPtB/nhGo/YJxwC+ssy9tmkTCG6AlV51DomdQ8ROwilzSlZ51y/ HDHWH5PVIaYTQJbxT5wbsd5+Nr0rZICg+QBDzm+zd/4Gzlmifnggnowr5Q9Jm24UQHNt mpPPd7I/dE9d/egkYZv9YaEB2xdrp2DPJLkB6aKVMb2yJwkIUlpX3+JbOt1KSQTOMrqS s1x2U668FNTd4cQSr89Lat6pJ+REDTuN0uXe+JViFQxukvFAFUcby+F409gSMvBAW/yH 05sdWrAFg+yY0nj1WWJUWNJt3Gov5YOuOCEcwl5WPpq4TFXvDh+nv/he8Er30vUERuU/ +T3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=CrwGDFj6TNVDh1dbkm0ZlisHB3oXLoLB2FOq55WC5c8=; b=Kzap3VM+mOAVUIu5Vx+cwbqXFXGGiekMu6XEOt9SGeMJYUM9RrjWdqHLwegYcD0oQa 3soJ+h/Nx/e+fxY1TmQipn5bKybYnOQp0HD/dRLdinVvjcGc6hKcsZMGvwouJ2WysWyG vTJ+h4aG9MOlMfDxMDm+d/IotXxxJCs18zJ4o90lHy5T8jVkRCzqhZpqXP4xiXetf8HY JiKfV8a5RKzVFCPWcNvYTCX3gOIi+d9F1d1qEaUUWP0uCvmyfGAD19pGIwv1hbxRAwcl bguZEqSs0AJWjB/JzqxQaacg+Bjq5HzjL9HabFrPxtv9d3qUgo9iT3SwVpHNzsxghO58 DPkQ== ARC-Authentication-Results: i=1; mx.google.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 i189si16995333pfg.265.2018.12.19.10.34.53; Wed, 19 Dec 2018 10:35:08 -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; 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 S1729516AbeLSQyU (ORCPT + 99 others); Wed, 19 Dec 2018 11:54:20 -0500 Received: from Galois.linutronix.de ([146.0.238.70]:59335 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728543AbeLSQyU (ORCPT ); Wed, 19 Dec 2018 11:54:20 -0500 Received: from bigeasy by Galois.linutronix.de with local (Exim 4.80) (envelope-from ) id 1gZf6V-0008K6-5g; Wed, 19 Dec 2018 17:53:51 +0100 Date: Wed, 19 Dec 2018 17:53:50 +0100 From: Sebastian Andrzej Siewior To: Peter Zijlstra Cc: zhe.he@windriver.com, acme@kernel.org, ak@linux.intel.com, alexander.shishkin@linux.intel.com, bp@alien8.de, hpa@zytor.com, jolsa@kernel.org, jolsa@redhat.com, kan.liang@linux.intel.com, mingo@redhat.com, namhyung@kernel.org, tglx@linutronix.de, x86@kernel.org, linux-kernel@vger.kernel.org, linux-rt-users@vger.kernel.org Subject: [PATCH] perf/x86/intel: Delay memory deallocation until x86_pmu_dead_cpu() Message-ID: <20181219165350.6s3jvyxbibpvlhtq@linutronix.de> References: <1545129033-21757-1-git-send-email-zhe.he@windriver.com> <20181218110209.GD15430@hirez.programming.kicks-ass.net> <20181218111637.b53u5pi3nxnyldaq@linutronix.de> <20181218113119.GG15430@hirez.programming.kicks-ass.net> <20181218113700.GC16284@hirez.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20181218113700.GC16284@hirez.programming.kicks-ass.net> User-Agent: NeoMutt/20180716 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Peter Zijlstra 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"). Cc: stable@vger.kernel.org Reported-by: He Zhe Fixes: a7e3ed1e47011 ("perf: Add support for supplementary event registers"). Signed-off-by: Peter Zijlstra (Intel) [bigeasy: patch description] Signed-off-by: Sebastian Andrzej Siewior --- arch/x86/events/intel/core.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c index ecc3e34ca955f..90b6718ff861d 100644 --- a/arch/x86/events/intel/core.c +++ b/arch/x86/events/intel/core.c @@ -3558,6 +3558,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 core_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 intel_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, }; -- 2.20.1