Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp78830rbb; Fri, 23 Feb 2024 12:23:07 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX6qYWXy4LXExdjKY8uiRuSIRzjjMKUAU3lfqUrq536oeX+PrKRUSkJatQF2Lu7gQ97cPzKRpWnlnbn1vmvwiztPKXRpaLCMxM79PWX7Q== X-Google-Smtp-Source: AGHT+IEidryOUPg0x2mRuN15BzeXpEouH2St8hKzC5UpXYzbKAdn3RBscITQ0mcwWTaz/pRDQs4b X-Received: by 2002:a05:6402:400c:b0:565:2ef3:e017 with SMTP id d12-20020a056402400c00b005652ef3e017mr811100eda.4.1708719787808; Fri, 23 Feb 2024 12:23:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708719787; cv=pass; d=google.com; s=arc-20160816; b=qtLmFusSu87EMTdAer9S5uy7NYAuYaGd7f8c7Zz38ToZsuMk4olBlcywmMCSW1PxH7 4ulfx8Q2hgM+8L5zH692DGLOQeLhIXwzNM3bYDhT04fJiQSF1yp5juvo4m/HToGfJ0JR UIJybty/lMyHPzK7nXax8c9TanexFyLjKTp6Rrhz0ZSEuuJJ8W6LuHyWRv0Pht5Z9Ldu 0/qN8GcozkUKa/RjRHX85nA0YmnuK3ifdDt4VaQMxACWRM7UkP916jmTvJ/gIDXhD1FW avEgzh9DHTPtqQbnpythz/+/c8o4ysS9luLqNw2e6AJe1fF/W1fbaNNT7Fonc/CRGLPZ 3TQA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=4AhBqaFKsEgrMMKsPnTb4KDGMvhddDO4DQwV/SsUl6M=; fh=hjhNgp9pt9rxaKUObiJEXVeBTsHkrXcmlwzxVWEGekA=; b=rk/ftVqpntLyN48wRBv3aJOdmc9aVRC2dd/MwghscP3aLUxTAEY1l180PIj6MpfSCZ jvhiSesVk/uRVWv1opjJpxbdmWYEXm9VMm+3lhvmVnc9E1tT0/Z5OQlykLfdIw14oVfZ Ik+jNMqh9OLBOsYSyJg5rDzqyETHjc6i1ilxD8bfUe0cxx9wmG0mT8c+5cs6QvQChr1S +tnUBwNswjNiscUcOv7Rppzyr3DIuhI230kIF1fQh8SqUBAy+6QoRFOOXr8KKxTT84J1 N2kM5Hjsi8IUf6RvQxWT/1xzTXXNtUAk+Rd98azYSDFYRslQeYPxomsdG2Kyz8Cjjgx4 kyOQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wk+bWj8T; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-79095-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79095-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id 30-20020a508e5e000000b005644479ae60si5724226edx.473.2024.02.23.12.23.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 12:23:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-79095-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=wk+bWj8T; arc=pass (i=1 spf=pass spfdomain=flex--seanjc.bounces.google.com dkim=pass dkdomain=google.com dmarc=pass fromdomain=google.com); spf=pass (google.com: domain of linux-kernel+bounces-79095-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-79095-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 620091F22E4C for ; Fri, 23 Feb 2024 20:23:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 90C4A14DFCC; Fri, 23 Feb 2024 20:21:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="wk+bWj8T" Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1D94014CAB3 for ; Fri, 23 Feb 2024 20:21:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708719674; cv=none; b=ckMcehqRv7rs8Co2vjxQn+jY2PgVZHR9Hooj9Pn5eYoipAtNNVwjryniJ1mk86RbPOrTwvrVZPoxPjV8LOePlsX1hzOD2Y+uqDDQdQPKxbg3CoQ8/vzdjLSmzi8zITO4SH4fVMw0Z5mMG1VQ3Ow8IhIU41SRpa5iPWI0AfZOMHI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708719674; c=relaxed/simple; bh=+lJk5AK8dlVL05cps/qThtV9mpJgp251EZLu0QB3UtM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iy4i8nFGow/ESP3VwB3FK2PEzVzklyN4wRpi6vcD0Yp+TLX2Maem49+nHDkE9t4QIO19n7vpC8rXVyN8g5SvRDOedc8UeWKtyK5mAsMASxWkojRf8IYSplJyc82iJ3a3zBIETex0ehqBAuHJZ2KL7YD004BaoVg9VyBYSpTBItc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=wk+bWj8T; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-29a86539b3cso340640a91.0 for ; Fri, 23 Feb 2024 12:21:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1708719672; x=1709324472; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=4AhBqaFKsEgrMMKsPnTb4KDGMvhddDO4DQwV/SsUl6M=; b=wk+bWj8T05NuASRY4t99y3sPoOQodcX+XY5Y/efVhoDnzqIR/gZsEv/FDad1IUMNdw TqZFbnNwa5YOi2aVNeWL/Nly6lvl8U2+QMHAyotSt6cRhSRe0HSzIWyz6GPEmCw2sqPg 5LPOUmooA8UHT5pa0Lq3csjGLkSSZocnMFOlbm3ZK3wkalpEJCo158N2xoiQcB17ChXu utjpYd4SOdO4I+TTJCCQdG4WJNPHOgxHitNsbIMv+AAjk9Kdo1GXlkK8w+JQMmQZaivR nGIj+UD5DhwNbAjMp1w72XoZLDKWudIHnhkF5qNYKEVogbsFaMiRtPivNLEltiHN6DdO Idsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708719672; x=1709324472; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4AhBqaFKsEgrMMKsPnTb4KDGMvhddDO4DQwV/SsUl6M=; b=wKNzg7dYbEZ7JdY0QSEvMu3BfN+89c1YtmfaFah36b/fNq1wjJmqvXwq4+pomzc6aH yodIPIOAhv4YWDaVNJwI2PEjM4UkP/rCQZSvxDtm8ic0BgOsH6fQvG0qxZxlpntM2PBe bR/AEAklb1QurzZbvhm/VYrUYjLwr8ABWo79ma4t9DQ4kzxV0Ag8QhXH0qhek1RwrFga SVAgbZHM+Cvwb0dW2g/Fdz+6qEoNe1hoiIoVA9q/NWvQBN61qU79l/OcZ9J7vURCVoEk 0qGm4amP3+CqtLim3otr7kvcppTb9N0B1Le0fGYxI6Nx+HhNRBXY5Chu1oGr5BcT6plY 3MQw== X-Forwarded-Encrypted: i=1; AJvYcCWq54mQUWBK947FFbm0q+p1R7G3/+tFycRSDi0/SkzpgRLBKcnmHGcWY9Lasuj7iCFAggiQNjnPlKcfMUJJmmsSpTRDUImK+7iqS1bL X-Gm-Message-State: AOJu0Yx8yBRek7NLyLxTrQ+F40dpLs1ffRGNHUE+MwuvtLVUpZ/AUtt4 f+yx8ikobseNEDrjcmMA/xBNkVXRlstoZ7FgFBjxHIdRkvLXWAufBscs28/3hg5/iT+Y9FYJABE f8w== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:2d89:b0:299:4395:a21c with SMTP id sj9-20020a17090b2d8900b002994395a21cmr2519pjb.7.1708719672505; Fri, 23 Feb 2024 12:21:12 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 23 Feb 2024 12:21:04 -0800 In-Reply-To: <20240223202104.3330974-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240223202104.3330974-1-seanjc@google.com> X-Mailer: git-send-email 2.44.0.rc0.258.g7320e95886-goog Message-ID: <20240223202104.3330974-4-seanjc@google.com> Subject: [PATCH v2 3/3] KVM: VMX: Combine "check" and "get" APIs for passthrough MSR lookups From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Dongli Zhang Content-Type: text/plain; charset="UTF-8" Combine possible_passthrough_msr_slot() and is_valid_passthrough_msr() into a single function, vmx_get_passthrough_msr_slot(), and have the combined helper return the slot on success, using a negative value to indicate "failure". Combining the operations avoids iterating over the array of passthrough MSRs twice for relevant MSRs. Suggested-by: Dongli Zhang Signed-off-by: Sean Christopherson --- arch/x86/kvm/vmx/vmx.c | 63 +++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 38 deletions(-) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 014cf47dc66b..969fd3aa0da3 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -658,25 +658,14 @@ static inline bool cpu_need_virtualize_apic_accesses(struct kvm_vcpu *vcpu) return flexpriority_enabled && lapic_in_kernel(vcpu); } -static int possible_passthrough_msr_slot(u32 msr) +static int vmx_get_passthrough_msr_slot(u32 msr) { - u32 i; - - for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++) - if (vmx_possible_passthrough_msrs[i] == msr) - return i; - - return -ENOENT; -} - -static bool is_valid_passthrough_msr(u32 msr) -{ - bool r; + int i; switch (msr) { case 0x800 ... 0x8ff: /* x2APIC MSRs. These are handled in vmx_update_msr_bitmap_x2apic() */ - return true; + return -ENOENT; case MSR_IA32_RTIT_STATUS: case MSR_IA32_RTIT_OUTPUT_BASE: case MSR_IA32_RTIT_OUTPUT_MASK: @@ -691,14 +680,16 @@ static bool is_valid_passthrough_msr(u32 msr) case MSR_LBR_CORE_FROM ... MSR_LBR_CORE_FROM + 8: case MSR_LBR_CORE_TO ... MSR_LBR_CORE_TO + 8: /* LBR MSRs. These are handled in vmx_update_intercept_for_lbr_msrs() */ - return true; + return -ENOENT; } - r = possible_passthrough_msr_slot(msr) != -ENOENT; - - WARN(!r, "Invalid MSR %x, please adapt vmx_possible_passthrough_msrs[]", msr); + for (i = 0; i < ARRAY_SIZE(vmx_possible_passthrough_msrs); i++) { + if (vmx_possible_passthrough_msrs[i] == msr) + return i; + } - return r; + WARN(1, "Invalid MSR %x, please adapt vmx_possible_passthrough_msrs[]", msr); + return -ENOENT; } struct vmx_uret_msr *vmx_find_uret_msr(struct vcpu_vmx *vmx, u32 msr) @@ -3954,6 +3945,7 @@ void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) { struct vcpu_vmx *vmx = to_vmx(vcpu); unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; + int idx; if (!cpu_has_vmx_msr_bitmap()) return; @@ -3963,16 +3955,13 @@ void vmx_disable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) /* * Mark the desired intercept state in shadow bitmap, this is needed * for resync when the MSR filters change. - */ - if (is_valid_passthrough_msr(msr)) { - int idx = possible_passthrough_msr_slot(msr); - - if (idx != -ENOENT) { - if (type & MSR_TYPE_R) - clear_bit(idx, vmx->shadow_msr_intercept.read); - if (type & MSR_TYPE_W) - clear_bit(idx, vmx->shadow_msr_intercept.write); - } + */ + idx = vmx_get_passthrough_msr_slot(msr); + if (idx >= 0) { + if (type & MSR_TYPE_R) + clear_bit(idx, vmx->shadow_msr_intercept.read); + if (type & MSR_TYPE_W) + clear_bit(idx, vmx->shadow_msr_intercept.write); } if ((type & MSR_TYPE_R) && @@ -3998,6 +3987,7 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) { struct vcpu_vmx *vmx = to_vmx(vcpu); unsigned long *msr_bitmap = vmx->vmcs01.msr_bitmap; + int idx; if (!cpu_has_vmx_msr_bitmap()) return; @@ -4008,15 +3998,12 @@ void vmx_enable_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type) * Mark the desired intercept state in shadow bitmap, this is needed * for resync when the MSR filter changes. */ - if (is_valid_passthrough_msr(msr)) { - int idx = possible_passthrough_msr_slot(msr); - - if (idx != -ENOENT) { - if (type & MSR_TYPE_R) - set_bit(idx, vmx->shadow_msr_intercept.read); - if (type & MSR_TYPE_W) - set_bit(idx, vmx->shadow_msr_intercept.write); - } + idx = vmx_get_passthrough_msr_slot(msr); + if (idx >= 0) { + if (type & MSR_TYPE_R) + set_bit(idx, vmx->shadow_msr_intercept.read); + if (type & MSR_TYPE_W) + set_bit(idx, vmx->shadow_msr_intercept.write); } if (type & MSR_TYPE_R) -- 2.44.0.rc0.258.g7320e95886-goog