Received: by 2002:a25:d7c1:0:0:0:0:0 with SMTP id o184csp5328198ybg; Tue, 22 Oct 2019 01:17:33 -0700 (PDT) X-Google-Smtp-Source: APXvYqzGdsuK/EnhTebe3WeL2C9h0HhTZrnxGJIh346XVBzPtNDSvq2EGiI1X9ZZSfgpuRbV/2Qe X-Received: by 2002:a17:906:6d45:: with SMTP id a5mr2083148ejt.218.1571732253274; Tue, 22 Oct 2019 01:17:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571732253; cv=none; d=google.com; s=arc-20160816; b=Tni7+yi86LlONhRJpX/zXsO+GNz0ZvSs80gpVbfRQMHHZ1T8pymaLhHO48B19eg0we eO/HeAQappNbqvTTgBLCUUL++yf/41HobSIHYHsYbjUYiJHiRfPKIv8fDcK9u93e05jI 1CKXUXNxFon1IG9r0HDRK1AIrXrNqlyb1f5CzIXJeWcE0t3wbghoat0zMh4wJmJ4j5On fZw3Yg4FbyenGE4FVkusKjntDjorqxV0eS3hhOQkgUhEx9sfHWWqK+Sy+shPUZmQQ4AF SA1a0jOnGy6Sh7eP6Sed55yFbZyV3q3M8Jic0udqWrxiSRKqLeyHa/yYYjO/qV5/3f3y LGIg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=b5B5y7gnPzrE3HbGNxcAwtBUvYiJj+bo0m9Am+9Onmo=; b=VqHjTwcWuKke60mVPIhDnDnLUH9w4B0rLdol6E8fWP+U5m1u0dHXw8WD/5T7kqQpyE KGRJPGMhoSVHsuf8zUGCyaOC6mP0lz8VX1ewMeNdVUfnglzf23swAPPc4u6ISuK8i9NE 69olXU4QaLCcnEcxVM24iz1xyHGb0mSFAG7mWW7I1MOqCVP+9pqNgaT7O6HDMFDifwJ4 C67+53X80shJu35GU8hd12jVmzd3hZU8xh+32IrCiIzmr5/VBlH+imBKhmeL0vR8o9Bu 3Ro40nhVmY/3PpU8jLmPyf6wvaVtRX6HK6/0ecEZipCHMyT5NR6hwVocMUm/OiKp77Se yDEw== 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 x25si2133893ejb.284.2019.10.22.01.17.08; Tue, 22 Oct 2019 01:17:33 -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 S2388252AbfJVIM1 (ORCPT + 99 others); Tue, 22 Oct 2019 04:12:27 -0400 Received: from mga06.intel.com ([134.134.136.31]:64390 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2388242AbfJVIM0 (ORCPT ); Tue, 22 Oct 2019 04:12:26 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga003.jf.intel.com ([10.7.209.27]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 22 Oct 2019 01:12:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.67,326,1566889200"; d="scan'208";a="200703497" Received: from sqa-gate.sh.intel.com (HELO clx-ap-likexu.tsp.org) ([10.239.48.212]) by orsmga003.jf.intel.com with ESMTP; 22 Oct 2019 01:12:24 -0700 From: Like Xu To: pbonzini@redhat.com, peterz@infradead.org, kvm@vger.kernel.org Cc: like.xu@intel.com, linux-kernel@vger.kernel.org, jmattson@google.com, sean.j.christopherson@intel.com, wei.w.wang@intel.com, kan.liang@intel.com Subject: [PATCH v3 2/6] perf/core: Provide a kernel-internal interface to pause perf_event Date: Tue, 22 Oct 2019 00:06:47 +0800 Message-Id: <20191021160651.49508-3-like.xu@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20191021160651.49508-1-like.xu@linux.intel.com> References: <20191021160651.49508-1-like.xu@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Exporting perf_event_pause() as an external accessor for kernel users (such as KVM) who may do both disable perf_event and read count with just one time to hold perf_event_ctx_lock. Also the value could be reset optionally. Suggested-by: Peter Zijlstra Signed-off-by: Like Xu --- include/linux/perf_event.h | 5 +++++ kernel/events/core.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h index d601df36e671..e9768bfc76f6 100644 --- a/include/linux/perf_event.h +++ b/include/linux/perf_event.h @@ -1337,6 +1337,7 @@ extern void perf_event_disable_inatomic(struct perf_event *event); extern void perf_event_task_tick(void); extern int perf_event_account_interrupt(struct perf_event *event); extern int perf_event_period(struct perf_event *event, u64 value); +extern u64 perf_event_pause(struct perf_event *event, bool reset); #else /* !CONFIG_PERF_EVENTS: */ static inline void * perf_aux_output_begin(struct perf_output_handle *handle, @@ -1420,6 +1421,10 @@ static inline int perf_event_period(struct perf_event *event, u64 value) { return -EINVAL; } +static inline u64 perf_event_pause(struct perf_event *event, bool reset) +{ + return 0; +} #endif #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_CPU_SUP_INTEL) diff --git a/kernel/events/core.c b/kernel/events/core.c index e1b83d2731da..fc9f5ebf4849 100644 --- a/kernel/events/core.c +++ b/kernel/events/core.c @@ -5029,6 +5029,24 @@ static void _perf_event_reset(struct perf_event *event) perf_event_update_userpage(event); } +/* Assume it's not an event with inherit set. */ +u64 perf_event_pause(struct perf_event *event, bool reset) +{ + struct perf_event_context *ctx; + u64 count; + + ctx = perf_event_ctx_lock(event); + WARN_ON_ONCE(event->attr.inherit); + _perf_event_disable(event); + count = local64_read(&event->count); + if (reset) + local64_set(&event->count, 0); + perf_event_ctx_unlock(event, ctx); + + return count; +} +EXPORT_SYMBOL_GPL(perf_event_pause); + /* * Holding the top-level event's child_mutex means that any * descendant process that has inherited this event will block -- 2.21.0