Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp596624lqp; Thu, 21 Mar 2024 09:48:08 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVsFAIquQE6sZEGrvRvcTrdkP8JYSteDHgUmstuFu2VKAH3Sf00VjfwqTjH32IuK3IGkv7olbW3li9GEYBUvEVKeTCEHpNgmEf0BLnG0A== X-Google-Smtp-Source: AGHT+IEx+HRXF2+4SzdlnHQABW4oNL8jVM7MyIGUQ8mjSWXKilu4+1ZJ8kVm2p6/8WjUxoGfSb8K X-Received: by 2002:a05:6a00:26c4:b0:6e7:82ec:b755 with SMTP id p4-20020a056a0026c400b006e782ecb755mr4797611pfw.27.1711039687772; Thu, 21 Mar 2024 09:48:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711039687; cv=pass; d=google.com; s=arc-20160816; b=yCw5BCCJD9rZV03txL5cqFuZfD6TAbEKKvop8XSILPqnfmb+ZJlj8al65XpgyCZeGj Tl/vVe3wHb6a+BBPwZZTPJt30AdxgYf6XmQ8+6orPUKw2Ik7TgJ4V41YOptwjxjR7aV1 8f7cSHP2yBD+30l3eywUj4T/AfCUb7wauuBc6hojE/Tz6jiWMyHYNvaqPry55AG4Qd5+ aogYFfgJm6DxcFoDJaiQIv3NATFQ6TxHAe+ZD+NyHIg07DnrxtE94izj7vp9b5iVJvx5 RvkBJ7MdO8mS9ExffmdMo7DhgSODmm8xTCf/M9kUs6k+GgZUtMwR/Vrh54JQPzY9b5FD DHOA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=OEFD9PsCOUXDCv0If8tukCS4a4VNOwHyCBI5J1/t1Ro=; fh=RRkWXDrutj1Lx3gPsKtVf+Be2JiZ2wU9Kn2QA5PRRrs=; b=Sbf0ZpDHzWcDETvqUuBAgCzbROMNHbum3weLiGz53TE/Bu6BKz6l7t7Y/N7KhaTeBN y6sjQvfytNchIHBqDGA/sZ8Af8qUbVxIrvXIEDH1pUvLCM+X2/YTmXztbvuCgDefCIKG bcPOHdJNR/N3iPcfAGnqDl9u9nhkdPox+nM/4PHHXgwm0krMjiaYRV1H9jLgIje2IISg o1K9ihgWs6brq9qYCTyGOL8MMGkJf/0EiPOZthjspb6C47e8/enKxtbFBXFWzuWKsVjA joXv5Y607q+mT1heZfmvE+/imt48WMjA5TFlYWq8yEkvVIrdi5a15QV2akKEctfdDob5 e+fw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TAYctVZs; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-110381-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110381-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id kq3-20020a056a004b0300b006e697817aa1si82691pfb.132.2024.03.21.09.48.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Mar 2024 09:48:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-110381-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=TAYctVZs; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-110381-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-110381-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 5CA7B2829E3 for ; Thu, 21 Mar 2024 16:47:52 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8EDDA13EFFF; Thu, 21 Mar 2024 16:38:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="TAYctVZs" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.7]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DF7C13B289; Thu, 21 Mar 2024 16:38:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.7 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039085; cv=none; b=JZnBxD2nYAhVXAipn7HbeDXWpbqltUAb45q4ggXL3Vsu/8Os2vBsFkWL6VS/qC9BA4+Snc3wopOt9Uq1kTGlR2pnSzAGG0OWpt/auBNLRvUBvnwybmZzzdxl2x/mxIX7hbVpfW+9/osqsN27uOSBu1x2arHo2n8I9vNk5m86644= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711039085; c=relaxed/simple; bh=hPRhzWBLAX0KSLPVRxVUSq+vfDY/HhSqH6GgEXF4edc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=uyYy1sPEJXMKyvuwCs4W1FUy2BY/cVUbqdBh079JWuKQnhA9ayKAqW9ltFcCmEN/soZDncRHJIY1HxH8Z5kmwkPY0/j8Rh0znYCbVIUeYlbl0lZrXnUVjCGjHbuL4rVRUdAx/Ta+sk8X+UISy376XrBySbShc+g3GOMhxKE2qns= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=TAYctVZs; arc=none smtp.client-ip=192.198.163.7 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711039083; x=1742575083; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=hPRhzWBLAX0KSLPVRxVUSq+vfDY/HhSqH6GgEXF4edc=; b=TAYctVZsEPUnEygPLVMWq60ffHSOrNJJ09dzURQ10OtaIj8V4TZe0F9J KQvYSgUr18Brq7XZgq0NHb5esjVU6awZalG2EnIIEC3SNbocFeUe9SsHf ZaL8mstVy6XLYj8k0RnAMx3i+BFRYYKwCCrN+bQ75Wj27XJopr275dFk4 apoDRj98PbUNDALh5/tk6oncGlT5uwXeNwVVikyDa1R+JRvPLIsqItTKy Ptaz0TjUSqv0pQzAi//bb8E8A0XqHRELm9Ee4/VOA4cW6rdfWEDXVy4ud yrEINjw8B0OtgtdjKlDOsP7jmhVb6W88c7KtrxnUDHJQ0pxk7GUVPaUzp w==; X-IronPort-AV: E=McAfee;i="6600,9927,11020"; a="31477560" X-IronPort-AV: E=Sophos;i="6.07,143,1708416000"; d="scan'208";a="31477560" Received: from orviesa003.jf.intel.com ([10.64.159.143]) by fmvoesa101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2024 09:37:58 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,143,1708416000"; d="scan'208";a="19280014" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by ORVIESA003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Mar 2024 09:37:58 -0700 From: Reinette Chatre To: isaku.yamahata@intel.com, pbonzini@redhat.com, erdemaktas@google.com, vkuznets@redhat.com, seanjc@google.com, vannapurve@google.com, jmattson@google.com, mlevitsk@redhat.com, xiaoyao.li@intel.com, chao.gao@intel.com, rick.p.edgecombe@intel.com Cc: reinette.chatre@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH V4 2/4] KVM: x86: Make nsec per APIC bus cycle a VM variable Date: Thu, 21 Mar 2024 09:37:42 -0700 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Isaku Yamahata Introduce the VM variable "nanoseconds per APIC bus cycle" in preparation to make the APIC bus frequency configurable. The TDX architecture hard-codes the core crystal clock frequency to 25MHz and mandates exposing it via CPUID leaf 0x15. The TDX architecture does not allow the VMM to override the value. In addition, per Intel SDM: "The APIC timer frequency will be the processor’s bus clock or core crystal clock frequency (when TSC/core crystal clock ratio is enumerated in CPUID leaf 0x15) divided by the value specified in the divide configuration register." The resulting 25MHz APIC bus frequency conflicts with the KVM hardcoded APIC bus frequency of 1GHz. Introduce the VM variable "nanoseconds per APIC bus cycle" to prepare for allowing userspace to tell KVM to use the frequency that TDX mandates instead of the default 1Ghz. Doing so ensures that the guest doesn't have a conflicting view of the APIC bus frequency. Signed-off-by: Isaku Yamahata Reviewed-by: Maxim Levitsky [reinette: rework changelog] Signed-off-by: Reinette Chatre --- Changes v4: - Reword changelog to address comments related to "bus clock" vs "core crystal clock" frequency. (Xiaoyao) - Typo in changelog ("APIC APIC" -> "APIC"). - Change logic "APIC bus cycles per nsec" -> "nanoseconds per APIC bus cycle". Changes V3: - Update commit message. - Dropped apic_bus_frequency according to Maxim Levitsky. Changes v2: - No change. arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/hyperv.c | 3 ++- arch/x86/kvm/lapic.c | 6 ++++-- arch/x86/kvm/lapic.h | 2 +- arch/x86/kvm/x86.c | 1 + 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 9e7b1a00e265..91ae7345700c 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1359,6 +1359,7 @@ struct kvm_arch { u32 default_tsc_khz; bool user_set_tsc; + u64 apic_bus_cycle_ns; seqcount_raw_spinlock_t pvclock_sc; bool use_master_clock; diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 1030701db967..5c31e715d2ad 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -1737,7 +1737,8 @@ static int kvm_hv_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, data = (u64)vcpu->arch.virtual_tsc_khz * 1000; break; case HV_X64_MSR_APIC_FREQUENCY: - data = div64_u64(1000000000ULL, APIC_BUS_CYCLE_NS); + data = div64_u64(1000000000ULL, + vcpu->kvm->arch.apic_bus_cycle_ns); break; default: kvm_pr_unimpl_rdmsr(vcpu, msr); diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 11b9a9bdc07a..f336a0ca1190 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1547,7 +1547,8 @@ static u32 apic_get_tmcct(struct kvm_lapic *apic) remaining = 0; ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period); - return div64_u64(ns, (APIC_BUS_CYCLE_NS * apic->divide_count)); + return div64_u64(ns, (apic->vcpu->kvm->arch.apic_bus_cycle_ns * + apic->divide_count)); } static void __report_tpr_access(struct kvm_lapic *apic, bool write) @@ -1965,7 +1966,8 @@ static void start_sw_tscdeadline(struct kvm_lapic *apic) static inline u64 tmict_to_ns(struct kvm_lapic *apic, u32 tmict) { - return (u64)tmict * APIC_BUS_CYCLE_NS * (u64)apic->divide_count; + return (u64)tmict * apic->vcpu->kvm->arch.apic_bus_cycle_ns * + (u64)apic->divide_count; } static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor) diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h index a20cb006b6c8..51e09f5a7fc5 100644 --- a/arch/x86/kvm/lapic.h +++ b/arch/x86/kvm/lapic.h @@ -16,7 +16,7 @@ #define APIC_DEST_NOSHORT 0x0 #define APIC_DEST_MASK 0x800 -#define APIC_BUS_CYCLE_NS 1 +#define APIC_BUS_CYCLE_NS_DEFAULT 1 #define APIC_BROADCAST 0xFF #define X2APIC_BROADCAST 0xFFFFFFFFul diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 66c4381460dc..23f25799c760 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -12559,6 +12559,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); kvm->arch.default_tsc_khz = max_tsc_khz ? : tsc_khz; + kvm->arch.apic_bus_cycle_ns = APIC_BUS_CYCLE_NS_DEFAULT; kvm->arch.guest_can_read_msr_platform_info = true; kvm->arch.enable_pmu = enable_pmu; -- 2.34.1