From patchwork Thu Feb 17 08:36:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Like Xu X-Patchwork-Id: 12749643 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C65FCC433EF for ; Thu, 17 Feb 2022 08:36:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237804AbiBQIg2 (ORCPT ); Thu, 17 Feb 2022 03:36:28 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:58988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237579AbiBQIg1 (ORCPT ); Thu, 17 Feb 2022 03:36:27 -0500 Received: from mail-pj1-x102d.google.com (mail-pj1-x102d.google.com [IPv6:2607:f8b0:4864:20::102d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52C3129A568; Thu, 17 Feb 2022 00:36:13 -0800 (PST) Received: by mail-pj1-x102d.google.com with SMTP id t14-20020a17090a3e4e00b001b8f6032d96so4758271pjm.2; Thu, 17 Feb 2022 00:36:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/TBhBCbqgGLkrSfwe557uNk0DpF24Rc/4Efq4hPMXTs=; b=SX7/In6dvYlBY75BkEpSCRzuwxjuoEiSFQQT8C4Ag8Sujft9j19gNJFIiziNmrUsMP /SA8S7Gt43bJHJ7JgfDNN0lVbpdL9wW1L5ebYB00q/p55MF+UBfSnWQzFIz5ciy/RVfe Ao6qXWj4fXmbLyqhwVrw7I+uoSMsfN3eWqMlritELeoDzdhvd4rD1OAw7FTg8z1bQP30 r2n7K4QLRqfo7aeoy7IbFzyjvYRBRpLWtunXvfENdqq8VqU7gIpLWeBLzV/QX+t/QnqR JFcACH1iuB77U+O/Ue74N8Nah/pLfoRloWSa1nRQrdcnJdxyIvqHb9M3xsfS2Oa7+c59 wUNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=/TBhBCbqgGLkrSfwe557uNk0DpF24Rc/4Efq4hPMXTs=; b=KaVgQLxzDxPP9C4RI6bW3IbVlycQn/NRbKdIASH7pxKWLtbqCqqJYuytoUyk8rJLUb PlAgw9K3ozc9lHpBcxxKiLwxRhxnTW49HXW8dHj0339nHKREWDOmEBj+3hNr/ZZjw5Ff YClMDoKygscqc5aZQ9Z/Ckq++RYT4JBGq6ZzKgTK7tyVeg2QDOcsj0YJKuYEIPUzi4W0 /I7Cr11f/SYbLaUxErPD4Q4C8IqvUNs951ydB0y/1U116WPbDj+MVkuenpCj880WB6pN agIXJte+Lc5Q/222G99rtUYFbSBTSEpWVY3ptt1Rm1P1PZHaEo/Sh7hYUpSqeE51wpvX NBzg== X-Gm-Message-State: AOAM532QQF5cwLDeDs8dG1Gg9llF6RxfU7JqyBb4yAlFaGvWuzv1mQU5 7aEN55dIOZrsRcCSvyFh2aI= X-Google-Smtp-Source: ABdhPJyA5XjxTU0tUv/e7TSo1fklrCpnDGB5vldR0gyDm3r+eJ1HR39sf+UhsZo6lmobfUcRm16Eww== X-Received: by 2002:a17:902:da82:b0:14e:bbe8:35e6 with SMTP id j2-20020a170902da8200b0014ebbe835e6mr1805773plx.13.1645086972790; Thu, 17 Feb 2022 00:36:12 -0800 (PST) Received: from localhost.localdomain ([103.7.29.32]) by smtp.gmail.com with ESMTPSA id a17sm5475843pfv.23.2022.02.17.00.36.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Feb 2022 00:36:12 -0800 (PST) From: Like Xu X-Google-Original-From: Like Xu To: Paolo Bonzini Cc: Sean Christopherson , Eric Hankland , Jim Mattson , Wanpeng Li , Vitaly Kuznetsov , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/2] KVM: x86/pmu: Extract check_pmu_event_filter() from the same semantics Date: Thu, 17 Feb 2022 16:36:00 +0800 Message-Id: <20220217083601.24829-1-likexu@tencent.com> X-Mailer: git-send-email 2.35.0 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Like Xu Checking the kvm->arch.pmu_event_filter policy in both gp and fixed code paths was somewhat redundant, so common parts can be extracted, which reduces code footprint, improves readability and facilitates the maintenance of SRCU logic for pmu_event_filter in a one place. Signed-off-by: Like Xu Reviewed-by: Wanpeng Li --- arch/x86/kvm/pmu.c | 61 +++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index f614f95acc6b..af2a3dd22dd9 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -179,12 +179,42 @@ static int cmp_u64(const void *a, const void *b) return *(__u64 *)a - *(__u64 *)b; } +static bool check_pmu_event_filter(struct kvm_pmc *pmc) +{ + struct kvm_pmu_event_filter *filter; + struct kvm *kvm = pmc->vcpu->kvm; + bool allow_event = true; + __u64 key; + int idx; + + filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu); + if (!filter) + goto out; + + if (pmc_is_gp(pmc)) { + key = pmc->eventsel & AMD64_RAW_EVENT_MASK_NB; + if (bsearch(&key, filter->events, filter->nevents, + sizeof(__u64), cmp_u64)) + allow_event = filter->action == KVM_PMU_EVENT_ALLOW; + else + allow_event = filter->action == KVM_PMU_EVENT_DENY; + } else { + idx = pmc->idx - INTEL_PMC_IDX_FIXED; + if (filter->action == KVM_PMU_EVENT_DENY && + test_bit(idx, (ulong *)&filter->fixed_counter_bitmap)) + allow_event = false; + if (filter->action == KVM_PMU_EVENT_ALLOW && + !test_bit(idx, (ulong *)&filter->fixed_counter_bitmap)) + allow_event = false; + } + +out: + return allow_event; +} + void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) { unsigned config, type = PERF_TYPE_RAW; - struct kvm *kvm = pmc->vcpu->kvm; - struct kvm_pmu_event_filter *filter; - bool allow_event = true; if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL) printk_once("kvm pmu: pin control bit is ignored\n"); @@ -196,17 +226,7 @@ void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel) if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc)) return; - filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu); - if (filter) { - __u64 key = eventsel & AMD64_RAW_EVENT_MASK_NB; - - if (bsearch(&key, filter->events, filter->nevents, - sizeof(__u64), cmp_u64)) - allow_event = filter->action == KVM_PMU_EVENT_ALLOW; - else - allow_event = filter->action == KVM_PMU_EVENT_DENY; - } - if (!allow_event) + if (!check_pmu_event_filter(pmc)) return; if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE | @@ -241,23 +261,14 @@ 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_pause_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; - } + if (!check_pmu_event_filter(pmc)) + return; if (pmc->current_config == (u64)ctrl && pmc_resume_counter(pmc)) return; From patchwork Thu Feb 17 08:36:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Like Xu X-Patchwork-Id: 12749644 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id F33BCC433EF for ; Thu, 17 Feb 2022 08:36:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237826AbiBQIge (ORCPT ); Thu, 17 Feb 2022 03:36:34 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:59002 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237579AbiBQIga (ORCPT ); Thu, 17 Feb 2022 03:36:30 -0500 Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4CF2129A568; Thu, 17 Feb 2022 00:36:16 -0800 (PST) Received: by mail-pl1-x633.google.com with SMTP id y18so4054427plb.11; Thu, 17 Feb 2022 00:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zFQNTKgYptCbdXsZlL/77IF7MTD6u26PIaEBtpH1VaM=; b=aDXX3JawX1MwT2vrGeK9N8VChXByzIuDCaxBM2jl3JLJmjXK9FkHS3K23DgfpV/GMS v51fLkB1/Sgltd+DYeAZ0rSdvc3CixpI4Oatc2UqenO9TBbwmghk7oJucbaFI1hSp/ms EzKUrN4t85TES1mttPn2RIbOncIhbNorBVID+AXpyBkOHPRPJ7I1+9pfA9h53OZnZGAl nWW6echqo1yugx8ikTic2wqnyP6Vh9oPj7XqjEjl986oaSzdYEARprtTxWbHJbMqlQE5 4wcb+ne3rSpuE8ljKe0N+RyS4KDJFgkuoopyZtgr1fo/jEz+YOISlfkNFHzKV8TAdlG+ 2h3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zFQNTKgYptCbdXsZlL/77IF7MTD6u26PIaEBtpH1VaM=; b=KJUPAdsKLzZp4OjVFrsWOwo356FCvyOW+5kL7oKph6ply5tkg3kpNwL53yWy7fMXTP aug1nKbdr4aaQiaqRM+mGzptdyMImYHx0TTtdM+m67+HfoafS7dUrEKiZ4OoP13muIqF Z1pAKB1lyjkUFnsPN331woJawJm1t6g4qfvZXhdXuKE0FJqVUuGpICu+kT5Qy5CXa11W 7D7KXGGkNW/WlmhTAkZhSc8Uj758BAnUWWnVfAR/KvLP0JV/hXdp1/BMAJXZH0tEdmMU AbIIiepthkR/kJZ/8DmyOGvgzuG6vSAASS2t+hsMAaOmkZguXrckOQLJXtG2o2ARC9BQ 7yfQ== X-Gm-Message-State: AOAM530vPdlCOdIltxOhL8xhn6aHQRyAwA10yzRnbDoIbhb+KyHNC/CF IH01uG57nfoYh4ZQirFzNNE= X-Google-Smtp-Source: ABdhPJwfoqlZhQYdFIyoiSza2d+G2zxKPTEJX5nCkLIuP8lHJj0/F+rD8p3wdPPo0yZDdp49YdwLMA== X-Received: by 2002:a17:90b:291:b0:1b9:3cb7:41ad with SMTP id az17-20020a17090b029100b001b93cb741admr6315770pjb.16.1645086975819; Thu, 17 Feb 2022 00:36:15 -0800 (PST) Received: from localhost.localdomain ([103.7.29.32]) by smtp.gmail.com with ESMTPSA id a17sm5475843pfv.23.2022.02.17.00.36.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Feb 2022 00:36:15 -0800 (PST) From: Like Xu X-Google-Original-From: Like Xu To: Paolo Bonzini Cc: Sean Christopherson , Eric Hankland , Jim Mattson , Wanpeng Li , Vitaly Kuznetsov , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/2] KVM: x86/pmu: Protect kvm->arch.pmu_event_filter with SRCU Date: Thu, 17 Feb 2022 16:36:01 +0800 Message-Id: <20220217083601.24829-2-likexu@tencent.com> X-Mailer: git-send-email 2.35.0 In-Reply-To: <20220217083601.24829-1-likexu@tencent.com> References: <20220217083601.24829-1-likexu@tencent.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Like Xu Fix the following positive warning: ============================= WARNING: suspicious RCU usage arch/x86/kvm/pmu.c:190 suspicious rcu_dereference_check() usage! other info that might help us debug this: rcu_scheduler_active = 2, debug_locks = 1 1 lock held by CPU 28/KVM/370841: #0: ff11004089f280b8 (&vcpu->mutex){+.+.}-{3:3}, at: kvm_vcpu_ioctl+0x87/0x730 [kvm] Call Trace: dump_stack_lvl+0x59/0x73 reprogram_fixed_counter+0x15d/0x1a0 [kvm] kvm_pmu_trigger_event+0x1a3/0x260 [kvm] ? free_moved_vector+0x1b4/0x1e0 complete_fast_pio_in+0x8a/0xd0 [kvm] [...] It's possible to call KVM_SET_PMU_EVENT_FILTER ioctl with the vCPU running. Similar to "kvm->arch.msr_filter", KVM should guarantee that vCPUs will see either the previous filter or the new filter so that guest pmu events with identical settings in both the old and new filter have deterministic behavior. Fixes: 66bb8a065f5a ("KVM: x86: PMU Event Filter") Signed-off-by: Like Xu Reviewed-by: Wanpeng Li --- arch/x86/kvm/pmu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c index af2a3dd22dd9..94319f627f64 100644 --- a/arch/x86/kvm/pmu.c +++ b/arch/x86/kvm/pmu.c @@ -185,8 +185,9 @@ static bool check_pmu_event_filter(struct kvm_pmc *pmc) struct kvm *kvm = pmc->vcpu->kvm; bool allow_event = true; __u64 key; - int idx; + int idx, srcu_idx; + srcu_idx = srcu_read_lock(&kvm->srcu); filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu); if (!filter) goto out; @@ -209,6 +210,7 @@ static bool check_pmu_event_filter(struct kvm_pmc *pmc) } out: + srcu_read_unlock(&kvm->srcu, srcu_idx); return allow_event; }