Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp267075img; Mon, 18 Mar 2019 02:39:35 -0700 (PDT) X-Google-Smtp-Source: APXvYqxNLeAPnw8dbojn2bH3eSV+iBOt+W7Qfz6a7O95BxlVg6xnvJPJlNRZs6jk6D0a56r8XPVD X-Received: by 2002:a65:60cb:: with SMTP id r11mr17060062pgv.143.1552901974927; Mon, 18 Mar 2019 02:39:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552901974; cv=none; d=google.com; s=arc-20160816; b=fiay5H6wsFyzUTNT5notOSXi0I9gD8OSFCfwGFeAudbnAty9dXtZhPWUK53IlNGomA uHTNusibB84vbBOaQjgDtXa0oX1NYedjscNAyTgkO/5nIAt7UGldAoMDqXqD/N4mspgt vnQPKD0lqHQNR5LFUr5sCVu+IAeXMHNK8c72rTjAdOrIYdQUQBF8cCE9iPa8IuClHKCY k/04bcGYFfG6vAPjU2+SDIwWR3TuuzT7QJR4GOwaKfYcjWXv8rLw7nHavviRuOVSUilC DHqgorwaR+eY7ZaGbeDc7fYWvCLYhYDaV3GD3LJFk4v0Vh6tCLmyaYrR/twQmpuEsKBT 3QvA== 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:dkim-signature; bh=Y3NiwwRe/QDvRzEhR0UQDgHYLh/EGideUmQY76n5dxg=; b=cecwDj4HoN5loeHjpiLM+bjwPAva//xkUNXGaXZxU7KE/pDJ2x87cwCsNpVp3CiNL4 wV75VSxtLHfC4KgXNdRFP5xVrTTP1rPuVJ4vvuinxh32VIlioXZcvjlVKwMdeT6X9bpb qVsrWP889YM2Nhv2yVBPQXBPckDxifWlFX95ycxlYVYIOtxaKquVrHzIhOqZjGq3rVtl 35bZ8bOlNa6YQZXmnGdetmR9bFzUsZbZ5oo0b8l6A4BSEl7zy7K+y3ecUnzf36v0Td0j DvTPBTrq/HnNCtr6fFAgtgXf2Wrp5UjVBDcAdiuqJuSa+4F4lsAXZ2TVNSGEaiAagoMg KZWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=YoCWDAW0; 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 m18si1136723pgl.483.2019.03.18.02.39.19; Mon, 18 Mar 2019 02:39:34 -0700 (PDT) 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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=YoCWDAW0; 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 S1728433AbfCRJiJ (ORCPT + 99 others); Mon, 18 Mar 2019 05:38:09 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:55800 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387910AbfCRJhg (ORCPT ); Mon, 18 Mar 2019 05:37:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=Y3NiwwRe/QDvRzEhR0UQDgHYLh/EGideUmQY76n5dxg=; b=YoCWDAW0kjG6izfCRgmyjPHTy 1KK6LSYcm3Gf4l+2PBbvCnoyAHHrlb/GWRRJXfQK/3CZOTzFakw4q/A3+xp9hcUl2vSsTLBeI0/Ia XqTByWB6ZDqZr+eMGJeLW2UBgg+cTw06CaQjQLgYzkFBqgynVwFJk5NFlbfrK/Mzq+SL5BUBm6Zai pKgGqROmSHcHn8vOdikeF6KRvFPwNz7gIxhB2Q/yGl6Ykm82zWhTaewHyv/XjN52yuVzV8K4QJyWl s4oRun254CK/1/1fVAeoaRz/9uCqlbi/abyZl77eYqJ03Ioo+Qbqt/BATbcVHygfqUdR8fZcXRdBv 4oktorCiA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1h5oi1-0003L2-AC; Mon, 18 Mar 2019 09:37:29 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 298A1202194ED; Mon, 18 Mar 2019 10:37:25 +0100 (CET) Date: Mon, 18 Mar 2019 10:37:25 +0100 From: Peter Zijlstra To: "Lendacky, Thomas" Cc: "x86@kernel.org" , "linux-kernel@vger.kernel.org" , Arnaldo Carvalho de Melo , Alexander Shishkin , Ingo Molnar , Borislav Petkov , Namhyung Kim , Thomas Gleixner , Jiri Olsa Subject: Re: [RFC PATCH v2 1/2] x86/perf/amd: Resolve race condition when disabling PMC Message-ID: <20190318093725.GJ6058@hirez.programming.kicks-ass.net> References: <155268244291.14761.3432013617741218607.stgit@tlendack-t1.amdoffice.net> <155268245032.14761.7748666703519081877.stgit@tlendack-t1.amdoffice.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <155268245032.14761.7748666703519081877.stgit@tlendack-t1.amdoffice.net> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Mar 15, 2019 at 08:40:53PM +0000, Lendacky, Thomas wrote: > +void amd_pmu_disable_all(void) > +{ > + unsigned long overflow_check[BITS_TO_LONGS(X86_PMC_IDX_MAX)]; > + struct cpu_hw_events *cpuc = this_cpu_ptr(&cpu_hw_events); > + int idx; > + > + bitmap_zero(overflow_check, X86_PMC_IDX_MAX); > + > + for (idx = 0; idx < x86_pmu.num_counters; idx++) { > + u64 val; > + > + if (!test_bit(idx, cpuc->active_mask)) > + continue; > + > + rdmsrl(x86_pmu_config_addr(idx), val); > + if (!(val & ARCH_PERFMON_EVENTSEL_ENABLE)) > + continue; > + > + val &= ~ARCH_PERFMON_EVENTSEL_ENABLE; > + wrmsrl(x86_pmu_config_addr(idx), val); > + > + /* > + * If the interrupt is enabled, this counter must be checked > + * for an overflow condition to avoid possibly changing the > + * counter value before the NMI handler runs. > + */ > + if (val & ARCH_PERFMON_EVENTSEL_INT) > + __set_bit(idx, overflow_check); > + } I think you can ditch overflow_check and directly call x86_pmu_disable_all() here. > + > + /* > + * This shouldn't be called from NMI context, but add a safeguard here > + * to return, since if we're in NMI context we can't wait for an NMI > + * to reset an overflowed counter value. > + */ > + if (in_nmi()) > + return; > + > + /* > + * Check each counter for overflow and wait for it to be reset by the > + * NMI if it has overflowed. > + */ > + for (idx = 0; idx < x86_pmu.num_counters; idx++) { > + if (!test_bit(idx, overflow_check)) And simply iterate cpuc->active_mask again here. > + continue; > + > + amd_pmu_wait_on_overflow(idx); > + } > +} Because, per x86_pmu_hw_config() we _always_ have EVENTSEL_INT set, even for !sampling events -- such that we can deal with overflow, and we should 'always' have EVENTSEL_ENABLE set when 'active', see x86_pmu_enable_all().