Received: by 2002:a25:824b:0:0:0:0:0 with SMTP id d11csp8232267ybn; Tue, 1 Oct 2019 05:20:50 -0700 (PDT) X-Google-Smtp-Source: APXvYqxu8DO6Eag4oQ5SOkbY8htbdulON5ylShk+jAQFd0xSNsiqKm2txEGLjiwEP4ypN/xdsRe+ X-Received: by 2002:a50:b0c5:: with SMTP id j63mr25388430edd.90.1569932450634; Tue, 01 Oct 2019 05:20:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1569932450; cv=none; d=google.com; s=arc-20160816; b=iP8i7BEcVCFz/MisX7YwwPBCuCAOw9xn4ct6yfj9aqTuXCWs3ZCvFNXrf/YeMtUIuq PB4Sdgrb1Poj8/vmG2bsyZlewgRACtGU0ynBWdDjkYWGlYbaSJF3ixMeJRtpvhE2ATjg 4QBBo955zOSMSLwioyWlyGflH+9qV1pzwfw/Mw+g//4RV/jsAsUx6BG/D2g9zJWxsNwB dHT+FFj9Xv9B5cZL3J0AKCK1SQJ6IJ0T826yQvZX5aF9XLxYJW/Gb1DG0iIIJA63A+YT SX46O30P2EDx4ssQjOpPpWL2xfTJ6Vwq+F+EyPKi1uOQlZzlEA3PAR2Q4/ZDmGydri79 mNxg== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:organization:from:references:cc:to:subject; bh=P5G2Is0klRVWrU9dReovkj/2SxHEwdaEafK+6gNtMSs=; b=X3OW2uW0JHlFUxI1r1UzQ4jbE8y5wlqBDoeDFbbhyRLUdo6Z65F4RaRL8/hV8gs8Vx fNmcP9Tmu/Jkulc2wRPjIoeDU2efGACKhqx6NvMin4EdmssJg0CvXuGMhufzeAkRkQcC x1a1oaHY6kRaArOTf6UNp7gTyy56Feju71a0KTci61sIbGsbDXR3XN3+BboY/8BhVEGl IOhtyTdFsfmLeHAEfQuLwirj98DYLtsBIvSA4q4jy6fIQUwaPQpgn9SH60y15KYsCjn/ 1iEoBP8gD97I1QEFvmgn09I0V5d9ljBAFafT1geL6V9wdMZohMjmtg1P95WtAsxobSFZ HhaA== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ox13si8927331ejb.0.2019.10.01.05.20.24; Tue, 01 Oct 2019 05:20:50 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387755AbfJAMSv (ORCPT + 99 others); Tue, 1 Oct 2019 08:18:51 -0400 Received: from mga02.intel.com ([134.134.136.20]:42627 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732706AbfJAMSu (ORCPT ); Tue, 1 Oct 2019 08:18:50 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 01 Oct 2019 05:18:49 -0700 X-IronPort-AV: E=Sophos;i="5.64,571,1559545200"; d="scan'208";a="185165068" Received: from likexu-mobl1.ccr.corp.intel.com (HELO [10.249.172.165]) ([10.249.172.165]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/AES256-SHA; 01 Oct 2019 05:18:46 -0700 Subject: Re: [PATCH 2/3] KVM: x86/vPMU: Reuse perf_event to avoid unnecessary pmc_reprogram_counter To: Peter Zijlstra Cc: Paolo Bonzini , kvm@vger.kernel.org, rkrcmar@redhat.com, sean.j.christopherson@intel.com, vkuznets@redhat.com, Jim Mattson , Ingo Molnar , Arnaldo Carvalho de Melo , ak@linux.intel.com, wei.w.wang@intel.com, kan.liang@intel.com, like.xu@intel.com, ehankland@google.com, arbel.moshe@oracle.com, linux-kernel@vger.kernel.org References: <20190930072257.43352-1-like.xu@linux.intel.com> <20190930072257.43352-3-like.xu@linux.intel.com> <20191001082218.GK4519@hirez.programming.kicks-ass.net> From: Like Xu Organization: Intel OTC Message-ID: Date: Tue, 1 Oct 2019 20:18:45 +0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <20191001082218.GK4519@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Peter, On 2019/10/1 16:22, Peter Zijlstra wrote: > On Mon, Sep 30, 2019 at 03:22:56PM +0800, Like Xu wrote: >> diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c >> index 46875bbd0419..74bc5c42b8b5 100644 >> --- a/arch/x86/kvm/pmu.c >> +++ b/arch/x86/kvm/pmu.c >> @@ -140,6 +140,35 @@ static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type, >> clear_bit(pmc->idx, (unsigned long*)&pmc_to_pmu(pmc)->reprogram_pmi); >> } >> >> +static void pmc_pause_counter(struct kvm_pmc *pmc) >> +{ >> + if (!pmc->perf_event) >> + return; >> + >> + pmc->counter = pmc_read_counter(pmc); >> + >> + perf_event_disable(pmc->perf_event); >> + >> + /* reset count to avoid redundant accumulation */ >> + local64_set(&pmc->perf_event->count, 0); > > Yuck, don't frob in data structures you don't own. Yes, it's reasonable. Thanks. > > Just like you exported the IOC_PERIOD thing, so too is there a > IOC_RESET. > > Furthermore; wth do you call pmc_read_counter() _before_ doing > perf_event_disable() ? Doing it the other way around is much cheaper, > even better, you can use perf_event_count() after disable. Yes, it's much better and let me apply this. > >> +} >> + >> +static bool pmc_resume_counter(struct kvm_pmc *pmc) >> +{ >> + if (!pmc->perf_event) >> + return false; >> + >> + /* recalibrate sample period and check if it's accepted by perf core */ >> + if (perf_event_period(pmc->perf_event, >> + (-pmc->counter) & pmc_bitmask(pmc))) >> + return false; > > I'd do the reset here, but then you have 3 function in a row that do > perf_event_ctx_lock()+perf_event_ctx_unlock(), which is rather > expensive. Calling pmc_pause_counter() is not always followed by calling pmc_resume_counter(). The former may be called multiple times before the later is called, so if we do not reset event->count in the pmc_pause_counter(), it will be repeatedly accumulated into pmc->counter which is a functional error. > >> + >> + /* reuse perf_event to serve as pmc_reprogram_counter() does*/ >> + perf_event_enable(pmc->perf_event); >> + clear_bit(pmc->idx, (unsigned long *)&pmc_to_pmu(pmc)->reprogram_pmi); >> + return true; >> +} >