Received: by 2002:ab2:6816:0:b0:1f9:5764:f03e with SMTP id t22csp1106785lqo; Fri, 17 May 2024 10:49:05 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUgy2P5KQYvqHU8T+IYtugUWYahU6QDPl3+R8jzJY8EpVogMXGAGLPILQcsgZOXZoAsw0psOSXA0v2zzaogYvYFxTPAN8j6RbniQkMh+Q== X-Google-Smtp-Source: AGHT+IHwcj56Kr1OvQ1f0MDvKTHCjFI1pIxIknnzKTELtulv+fanDY04PZy+3kgU6HBxbLwDm3Ws X-Received: by 2002:a05:6358:8aa:b0:183:f636:8a46 with SMTP id e5c5f4694b2df-193bd0078b5mr2110300955d.27.1715968144967; Fri, 17 May 2024 10:49:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715968144; cv=pass; d=google.com; s=arc-20160816; b=Qz8lSF1hrSdtvDfs+a+YLjXmjqjYy2N+Is9TsLUULpT1CKOxmLJOaIDF9Q3kxhqC1C wDysXPfMI8MAc1bcnJvl/rVFhsQ9taGbRI+XWu/vxXM47bJc8cboXZ247kFH7FaNY3bl Loo6OJVSXqO5JiqgD+/XdwfgLeIuRja/EzhR6L3x++dsljgfg8o5pqwK8f7OCgtPygjN ZGfj/Q2aE2QGqNIN6wlI6jDs5IeuCZosCP7SdiT9q+B5DWYLbpfOAhTzaUOCAnIgFqxx NhgLuptw1O9bHcq43H8KKi10NXpM80Or2sGrC1aZ7OnJhXdfir8VapoKyk0PoztMtudl 8ZhA== 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=lNzeyIHxwW/+xXRnTMfyELju+tot5aIGCckb2dIAUSc=; fh=23YpuQUy9g4MU0pIVDv24hNCFvIwVqX6aR8Nshfl4ZE=; b=FlLrDqlSGYCmX/JmAPvHF+6cPnVJ4ef6pRWe7NOHaNy5RCfJB5+trPMHyXQm0g1igM iGTfJz6Ppr5pDMtTnvLjByYd0cNS6T+9hGvqDu0LsC5F6YXn7WyFkGdnLc5XRZJZIyUn LDBF6n1MM7ghZmo1c9bcshW9jZ/6ZF6Pice6Vu0sXm4a8RMNSh2P7OzbUxzJp2VPkoir A0Wh+mWze8UQPvq1ZpB+E02MXK8W4AHEMLJzs61PP/tXLOZJnXYZ21I5j4PzRt80rtHn gYhMWc81Tv/BGYQj/FliavXGwRq25SNXZfXjs07kMq9lMSrKul+sGK2xUxJwCti6TGP1 r17Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fsbHYPZ+; 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-182462-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182462-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id d75a77b69052e-43e09bb9c78si152239311cf.470.2024.05.17.10.49.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 May 2024 10:49:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-182462-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=fsbHYPZ+; 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-182462-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-182462-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 988FD1C2121C for ; Fri, 17 May 2024 17:49:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 52C9315443B; Fri, 17 May 2024 17:40:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fsbHYPZ+" Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 E15F4153810 for ; Fri, 17 May 2024 17:40:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715967641; cv=none; b=YGMkBI8RGqmR8DvVQjvROtGgDnF4BXsri6LXlUJEk65+XyO8ufkjFQZQYSetQmz4P0tq3342GpLTml0o3+LqgmQIjQsqWJAUuTzKxix5zSzWZR+ZFC1WNC3X0So/kETt6WYhNPutPEhFbsg/6pRIZagzDtZIOcl6YYr734c5cO0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715967641; c=relaxed/simple; bh=q6H24XTOw5jOHO1mRplgtF7Ho2CCrC831sb6N2j+gE4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jVUiQESdjnyzHU81DLwJzfmJ2VbHnE068HbgBGZEFeMLUXjkaBnkymntfbJWKI7DEoX6QQhEYnr0H5SN9SdXfhp/CU6AZtgY3J9PIh0dymharrRcxCXV9MhHh+VbIiurWVJ6F37eBEOAw2KrZObPM8l7hyf4roYmQBYPH8sR+NE= 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=fsbHYPZ+; arc=none smtp.client-ip=209.85.128.202 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-yw1-f202.google.com with SMTP id 00721157ae682-61c9e368833so180140087b3.3 for ; Fri, 17 May 2024 10:40:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1715967639; x=1716572439; 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=lNzeyIHxwW/+xXRnTMfyELju+tot5aIGCckb2dIAUSc=; b=fsbHYPZ+kQu/V2Fug9aey7tMUbw57I0lMJ0hxmzKDdL29wEn4E2XR485jhrCO0YjsH eE+3JzFYXMBSvRsZnIoydPqCEbCG4P3XT33Ej5jxvNNkFu77InibCmRRScYg9W4IbqOS DegDEEa39aS2cw169phg3hvVTQmSPF1LNF4qMrdJ2SwckQv4zG5qFP7UcI/X20x5Ogzp 2NIEZA60qIl52ID7cD+PZvwM30P2oW5crKtJ87SkBCqgdCD4ZrpwnRHFObzEbp8yKZGw 2aECr7V298M1Z5NfNrrii4oz6StxNClRJyG120bPRwHp32BTeXot39VgDmorcQMzH5tx lbIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715967639; x=1716572439; 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=lNzeyIHxwW/+xXRnTMfyELju+tot5aIGCckb2dIAUSc=; b=YJ06Ve6bYqh/Mn3G+7JUVjec1DNlRkxuWcbM/d1Gpv84n5bRPbB6WRcM3FKI+eUPET AACv/awzyVLu8kLB4L/Tv1txZif1dbzn6htTMS0gAtUfFQJnnjpmsoCCCBFoLBbUA7X2 lDFIVFPtJymddWLCsX96jyBl0E2rW3uWebBjUK0Pj+a67ZCH6sN2EwF34c56QchCv48N HiYCKpMMpiQIgWf7/3GMhCWrzJBX9sAolC2+0iDfquUpZRxVsHgRmPoXFkeNjGbVEMMU pnW1UebItQIbh3nAJVkFdSAuBN9pvGNlY3tNdCBC3mrZowE/ckbADVmtmhvg23FFI/hf 3bkg== X-Forwarded-Encrypted: i=1; AJvYcCVBYQbg42bnOP9wadqtmrUraTplKztI5numgd8orZ8zQrri7adrc7DpFrrVQXNkaOk0Ph3ENe/O8MW87YHqL2izUDML1dm4IBXTIRi8 X-Gm-Message-State: AOJu0YwGAyCjD1A+b/YQt95w8QO/6XzK1fwB5JseIxX/CfQiOaEt+TPI 8u2fJNU2SfmtJfxk1sIQLgBZyzVKFRv0wsp4N+kU/JwyYzmDGLtoDegT5fYCZsOAJfptanpRKHX igw== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a0d:d8d5:0:b0:627:7cac:6afb with SMTP id 00721157ae682-6277cac6b6cmr11324997b3.9.1715967639056; Fri, 17 May 2024 10:40:39 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 17 May 2024 10:39:06 -0700 In-Reply-To: <20240517173926.965351-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: <20240517173926.965351-1-seanjc@google.com> X-Mailer: git-send-email 2.45.0.215.g3402c0e53f-goog Message-ID: <20240517173926.965351-30-seanjc@google.com> Subject: [PATCH v2 29/49] KVM: x86: Remove unnecessary caching of KVM's PV CPUID base From: Sean Christopherson To: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hou Wenlong , Kechen Lu , Oliver Upton , Maxim Levitsky , Binbin Wu , Yang Weijiang , Robert Hoo Content-Type: text/plain; charset="UTF-8" Now that KVM only searches for KVM's PV CPUID base when userspace sets guest CPUID, drop the cache and simply do the search every time. Practically speaking, this is a nop except for situations where userspace sets CPUID _after_ running the vCPU, which is anything but a hot path, e.g. QEMU does so only when hotplugging a vCPU. And on the flip side, caching guest CPUID information, especially information that is used to query/modify _other_ CPUID state, is inherently dangerous as it's all too easy to use stale information, i.e. KVM should only cache CPUID state when the performance and/or programming benefits justify it. Signed-off-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 1 - arch/x86/kvm/cpuid.c | 34 +++++++-------------------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index aabf1648a56a..3003e99155e7 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -858,7 +858,6 @@ struct kvm_vcpu_arch { int cpuid_nent; struct kvm_cpuid_entry2 *cpuid_entries; - struct kvm_hypervisor_cpuid kvm_cpuid; bool is_amd_compatible; /* diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 93a7399dc0db..7290f91c422c 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -269,28 +269,16 @@ static struct kvm_hypervisor_cpuid kvm_get_hypervisor_cpuid(struct kvm_vcpu *vcp vcpu->arch.cpuid_nent, sig); } -static struct kvm_cpuid_entry2 *__kvm_find_kvm_cpuid_features(struct kvm_cpuid_entry2 *entries, - int nent, u32 kvm_cpuid_base) -{ - return cpuid_entry2_find(entries, nent, kvm_cpuid_base | KVM_CPUID_FEATURES, - KVM_CPUID_INDEX_NOT_SIGNIFICANT); -} - -static struct kvm_cpuid_entry2 *kvm_find_kvm_cpuid_features(struct kvm_vcpu *vcpu) -{ - u32 base = vcpu->arch.kvm_cpuid.base; - - if (!base) - return NULL; - - return __kvm_find_kvm_cpuid_features(vcpu->arch.cpuid_entries, - vcpu->arch.cpuid_nent, base); -} - static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu) { - struct kvm_cpuid_entry2 *best = kvm_find_kvm_cpuid_features(vcpu); + struct kvm_hypervisor_cpuid kvm_cpuid; + struct kvm_cpuid_entry2 *best; + kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); + if (!kvm_cpuid.base) + return 0; + + best = kvm_find_cpuid_entry(vcpu, kvm_cpuid.base | KVM_CPUID_FEATURES); if (!best) return 0; @@ -491,13 +479,6 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, * whether the supplied CPUID data is equal to what's already set. */ if (kvm_vcpu_has_run(vcpu)) { - /* - * Note, runtime CPUID updates may consume other CPUID-driven - * vCPU state, e.g. KVM or Xen CPUID bases. Updating runtime - * state before full CPUID processing is functionally correct - * only because any change in CPUID is disallowed, i.e. using - * stale data is ok because KVM will reject the change. - */ kvm_update_cpuid_runtime(vcpu); kvm_apply_cpuid_pv_features_quirk(vcpu); @@ -519,7 +500,6 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, if (r) goto err; - vcpu->arch.kvm_cpuid = kvm_get_hypervisor_cpuid(vcpu, KVM_SIGNATURE); #ifdef CONFIG_KVM_XEN vcpu->arch.xen.cpuid = kvm_get_hypervisor_cpuid(vcpu, XEN_SIGNATURE); #endif -- 2.45.0.215.g3402c0e53f-goog