Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp2638921ybi; Thu, 18 Jul 2019 11:39:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqz9kjkRNZ6/3EgCDE3H53ELB6R9ojo0rZUCWC+Uuwet1WaCZpG57iFnY51QDNg1pNKtpMVd X-Received: by 2002:a17:90a:2244:: with SMTP id c62mr53763116pje.29.1563475154785; Thu, 18 Jul 2019 11:39:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563475154; cv=none; d=google.com; s=arc-20160816; b=qZY4J5Oezj/wtgXPjGnLMA4PwjtftOujeX2wztWS+PIMOgNp946EVT3lTxu88WWi/Y QBpL1ckuyp5kEzJQhO74NDn0elg4HoBEG5d4wXW3su+DBahO0r6NDTVkSIWkLZeL8+y3 d4rK6/mgfcaTw3RXUb4Gu+fBlNALMCqmn63A/pPDBAh+druWe1MMX+SUjrZtRsQ7bT5G 1+RX2gZ0ohBU5hIYZTa2m/nkniNDjCANIHgfToIw78HxO7UGj0KPCk/1JhTgUV+6VW7u vD6DWbPxpCFDB7KgG8fgycMVLWgBjaP63AcKVsA04WAJaVsjjiLepAEM2GJ13LdHYGLV kFxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=n8VorecpPGmR7cZwCga8Jqx3nBBTF8cHAFyZf3z5/Dc=; b=F10eYTvahIVeM/faol8WnUEFmBmBMA8Jvisjk2VLWoGcgvVOLvYsIOXk+TWRQcTw3B dSJ7IbOFNVB2GwH+/DrvvtfrzBOQ6QL1Ng24kQiFnQh8GsR2I1rxRLaEk78MmzHGpE2v CjywvIwUrJHGsveB+QE1RDypjqrgl31IDTKSG2Ijozdw6ev9zEjBVJX1FUO6phoXeKw0 lyBlCQ8XsIw0bPVf1ub87em5nGh+cjD9goYqjNq45gS52R+Jtm/jaoabQMwVj3XP/3A2 mrX61A+38e78VXbT5f2u+95+BCGyklnbbL1u6E0lnyyF01Ew+0ampJ9UPrPLLyOSLx1u FMFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=OKg+YDsl; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q2si662407plh.59.2019.07.18.11.38.58; Thu, 18 Jul 2019 11:39:14 -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=pass header.i=@google.com header.s=20161025 header.b=OKg+YDsl; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732457AbfGRSiW (ORCPT + 99 others); Thu, 18 Jul 2019 14:38:22 -0400 Received: from mail-pg1-f201.google.com ([209.85.215.201]:45693 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727623AbfGRSiW (ORCPT ); Thu, 18 Jul 2019 14:38:22 -0400 Received: by mail-pg1-f201.google.com with SMTP id n3so7307586pgh.12 for ; Thu, 18 Jul 2019 11:38:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=n8VorecpPGmR7cZwCga8Jqx3nBBTF8cHAFyZf3z5/Dc=; b=OKg+YDslZgql7NzppycnzhnaaEHSefuv6GvCgntPJSplHAjZc3Wj2+Akp5UvKETYp1 T95Eqc0Yasu59LBHw+0leU9b3aeA25+xjGThT62o0QUFVDhsg8mkcJLFc2UkvYeHU0xT T0MQWY0cbXxMWiww9TDF4UxcgRMmcqOcdHl/kdvRtuqgAg5uLKYmBSFps3EvfrY90UkK TObrzh8/82mfVxce6YozV1BoeMpA7KBT+H6bCnsdVijB6UUUNe5EtRnZrOFdmrIGp52h dV4qy52Rh+nnaH2Lrrvg+oU8dAybxCIS9itOR2snqSJCazsi7y7Zdn/LAKMdiqRF2s3V AhtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=n8VorecpPGmR7cZwCga8Jqx3nBBTF8cHAFyZf3z5/Dc=; b=Xmddy9WYpmnu6YTw9VzTn1jRWyRStf00ddPnRvUKQfVx2Di40Eb7ljly+iEHWE9+2F NHwY7GLRXVJmzuoz1H0p78sTMgWMIPhSCFherczN3cRXPrFzTRje38szGAb+KRB7QkF+ SBlWwIv5DaHH6V7ODQ2MFHD9IAcZnocdNTL9rG6oV2qpIXDeVBe2HSXwqtBoZGsNddiF JYJQc5AKRKBOBVzsWQfYpKifbcmIezZvF/olFeUWED0X/0j1W1b1X39uoz8WbTKTyQp9 RsakYcLO7NRNppVLwpviSidK1Lsx1yYrnuB54RyfUNnVIlay9MpBBk8oYbhg1+0eU5NV TB2Q== X-Gm-Message-State: APjAAAXg8si8AdkDtnj3IGzDCBQL9Mg1mqCJ9ZeD4kxfgHfjOVa11s0w v0HgD3G8ZAKhy5TSKyn72v+bp5sXE8+OGYk= X-Received: by 2002:a63:e5a:: with SMTP id 26mr47410389pgo.3.1563475101281; Thu, 18 Jul 2019 11:38:21 -0700 (PDT) Date: Thu, 18 Jul 2019 11:38:18 -0700 Message-Id: <20190718183818.190051-1-ehankland@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.22.0.510.g264f2c817a-goog Subject: [PATCH] KVM: x86: Add fixed counters to PMU filter From: Eric Hankland To: Wei Wang , Paolo Bonzini Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, Stephane Eranian , ehankland Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: ehankland Updates KVM_CAP_PMU_EVENT_FILTER so it can also whitelist or blacklist fixed counters. Signed-off-by: ehankland --- Documentation/virtual/kvm/api.txt | 13 ++++++++----- arch/x86/include/uapi/asm/kvm.h | 9 ++++++--- arch/x86/kvm/pmu.c | 30 +++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/Documentation/virtual/kvm/api.txt b/Documentation/virtual/kvm/api.txt index 2cd6250b2896..96bcf1aa1931 100644 --- a/Documentation/virtual/kvm/api.txt +++ b/Documentation/virtual/kvm/api.txt @@ -4090,17 +4090,20 @@ Parameters: struct kvm_pmu_event_filter (in) Returns: 0 on success, -1 on error struct kvm_pmu_event_filter { - __u32 action; - __u32 nevents; - __u64 events[0]; + __u32 action; + __u32 nevents; + __u32 fixed_counter_bitmap; + __u32 flags; + __u32 pad[4]; + __u64 events[0]; }; This ioctl restricts the set of PMU events that the guest can program. The argument holds a list of events which will be allowed or denied. The eventsel+umask of each event the guest attempts to program is compared against the events field to determine whether the guest should have access. -This only affects general purpose counters; fixed purpose counters can -be disabled by changing the perfmon CPUID leaf. +The events field only controls general purpose counters; fixed purpose +counters are controlled by the fixed_counter_bitmap. Valid values for 'action': #define KVM_PMU_EVENT_ALLOW 0 diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index e901b0ab116f..503d3f42da16 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -435,9 +435,12 @@ struct kvm_nested_state { /* for KVM_CAP_PMU_EVENT_FILTER */ struct kvm_pmu_event_filter { - __u32 action; - __u32 nevents; - __u64 events[0]; + __u32 action; + __u32 nevents; + __u32 fixed_counter_bitmap; + __u32 flags; + __u32 pad[4]; + __u64 events[0]; }; #define KVM_PMU_EVENT_ALLOW 0 diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index aa5a2597305a..ae5cd1b02086 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -19,8 +19,8 @@ #include "lapic.h" #include "pmu.h" -/* This keeps the total size of the filter under 4k. */ -#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 63 +/* This is enough to filter the vast majority of currently defined events. */ +#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300 /* NOTE: * - Each perf counter is defined as "struct kvm_pmc"; @@ -206,12 +206,25 @@ void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx) { unsigned en_field = ctrl & 0x3; bool pmi = ctrl & 0x8; + struct kvm_pmu_event_filter *filter; + struct kvm *kvm = pmc->vcpu->kvm; + pmc_stop_counter(pmc); if (!en_field || !pmc_is_enabled(pmc)) return; + filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu); + if (filter) { + if (filter->action == KVM_PMU_EVENT_DENY && + test_bit(idx, (ulong *)&filter->fixed_counter_bitmap)) + return; + if (filter->action == KVM_PMU_EVENT_ALLOW && + !test_bit(idx, (ulong *)&filter->fixed_counter_bitmap)) + return; + } + pmc_reprogram_counter(pmc, PERF_TYPE_HARDWARE, kvm_x86_ops->pmu_ops->find_fixed_event(idx), !(en_field & 0x2), /* exclude user */ @@ -376,7 +389,7 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp) { struct kvm_pmu_event_filter tmp, *filter; size_t size; - int r; + int r, i; if (copy_from_user(&tmp, argp, sizeof(tmp))) return -EFAULT; @@ -385,6 +398,13 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp) tmp.action != KVM_PMU_EVENT_DENY) return -EINVAL; + if (tmp.flags != 0) + return -EINVAL; + + for (i = 0; i < ARRAY_SIZE(tmp.pad); i++) + if (tmp.pad[i] != 0) + return -EINVAL; + if (tmp.nevents > KVM_PMU_EVENT_FILTER_MAX_EVENTS) return -E2BIG; @@ -406,8 +426,8 @@ int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp) mutex_unlock(&kvm->lock); synchronize_srcu_expedited(&kvm->srcu); - r = 0; + r = 0; cleanup: kfree(filter); - return r; + return r; }