Received: by 2002:ab2:6c55:0:b0:1fd:c486:4f03 with SMTP id v21csp631521lqp; Wed, 12 Jun 2024 11:16:50 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCU6fwzHEZrJRDksiaupNtFlbaQ18JhIFQguIy47lZeqKm9z9Wk9AnLz04a1muxl2d7UMCyv47pK70g0a4W4xz9+cnL2/CtoTUnyN52Yng== X-Google-Smtp-Source: AGHT+IFWzNzQjsIwpgI05RZZFM7EhsRbKkirpaDKKUs3uLyqMPsjr8fFssLV8daWN8UyNZpnd9No X-Received: by 2002:a05:620a:4089:b0:795:5efe:e85 with SMTP id af79cd13be357-797f5da1163mr258985185a.0.1718216209775; Wed, 12 Jun 2024 11:16:49 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1718216209; cv=pass; d=google.com; s=arc-20160816; b=WKD+4+vCm59j5ucXRTetnnR/AFQY5Jll1nBRrWcsoLCuW9R9sV3JoJo7upQgjzLMc5 /iCgmLPI5k1QwDNO6YPP+cSmRyx5ZUSZtdNp/LO6rKqa+BVwRnfdC48zND/iVs7Y2mwn rFeG+BR/fDM3pGKAdfjWcqfPvCxEVvnlFoPHWW35Kx30J8tpbZKZKDZW+txTfwFWkv9+ gfW4ZGK4fDBjaMK+TY8TexkXIO8BWlTATA/e5E/MHpv+x3Cf3M+2q0xHvx2urMZXT5y3 Bedl1fygL1BHQJOCQl6jYFC7+kMy+LVTh+xpBOwFaXPIibU+fEPyS08XtSChLlPvzsHf wu+g== 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:message-id:date:subject:cc:to :from:dkim-signature; bh=CMNGnhzjJGxAVpjIU01VS/jYlrWT5Mrnnakvy6l8UYk=; fh=39c7xHRXKGHRweehzLnY8wn6iHFCUfmQLOpmt3ijdwY=; b=IK3UupyJGjwcvLt/rZ/GKMev3vHuGzrNo0Uy39VwaAUlrDAUo1gQuFKQbZaco2CLpY 06NFagTOxmiaiOpI4rg77lonv/mz52OGc5aqV1emrtrV0JLOlP+pHVyRLigVwxxj/NmM jjD3h0fzvFuIEVRHg0hNTnsd8TU0YOWwP+S7/VN6+zygh19ZxE8dxE/5mKCFNcIJvw3D xKiaVLLi21lSIRw5siWqvM/BjJcqTyBw464BW029SX+rHrQNVaEKtxZh6k0gBU/y+F+I nXxaxGRzqlvzwZ0LdmApZ/phVgwCuhpWP5enmE3obgkWfMuPdKp3kEy5lIfZGYaxg6pe 35dw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="DS/+L8JK"; 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-212051-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212051-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.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 af79cd13be357-7973fa48648si707287685a.582.2024.06.12.11.16.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Jun 2024 11:16:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-212051-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=@intel.com header.s=Intel header.b="DS/+L8JK"; 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-212051-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-212051-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 75C941C2162B for ; Wed, 12 Jun 2024 18:16:49 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6284B405D8; Wed, 12 Jun 2024 18:16:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="DS/+L8JK" Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.17]) (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 B950E3A1C9; Wed, 12 Jun 2024 18:16:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=192.198.163.17 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718216182; cv=none; b=QKK9Cz7PcaS5ACnyRzEpkDGkeEX2cEi++cQIxxE39drvuIW7afZ3UXYGkxniw2E4OyldboMGoEPAUNQH97w5wvsLr4Aji7N7C+M69lSSdHyT9aydnvl0+AVc6HkaFeu4Rio4JPDQcvltl1yrwIVWIRec2GzUfeBsKCahF8anZMk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718216182; c=relaxed/simple; bh=C/sTM/+AwVpVzWE4Xx22Er7RhO2ScdTSZELduPJ41sI=; h=From:To:Cc:Subject:Date:Message-Id:MIME-Version:Content-Type; b=TCTkHDZgwP0s45LkoAn2vUjDJo5+TrhQv6NDd4u95CvrtUJeDVig+e58FDWF7tA7n2ZHPtHwcRfPbyufXFWs4F7dMzOIaMNUA9mkVgM53BZoBOAdrEZMQRDLp7t3Kc9tncS1SYlEqPOd6VUqHSMnfRZIV7IkxO74aX9UNKMhqdM= 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=DS/+L8JK; arc=none smtp.client-ip=192.198.163.17 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=1718216179; x=1749752179; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=C/sTM/+AwVpVzWE4Xx22Er7RhO2ScdTSZELduPJ41sI=; b=DS/+L8JKJAIt9AjLtjKxulgoXawWoVrhllpQFbF5l73coRAzhhd9Rybu AOduiQy38pOjHc97wFrO8HNEsj0sTk8ejhR2gbvF6mQLy/W6P9HtlQwID jdZkYbUynN96sk/ofLFSs9IkCmveOnpPMBswYzbFORQT9Rvrgm7Y9uR1u 07i+WN/Aq7+vOYv6KXcEnF3cnUH9jFxWZKNdMqxPZ4L74KeYUi4xAsoeU gtvg9jbpIw88HndOHXOk3C822HlpDo6oIropIozuiLcfpcWQi1OAl31Nt FqXRHhEZbjEYY9RqZs9EcTfiaSn5UPtVWig0XJf8VcT0Nen9CvyY9CANK w==; X-CSE-ConnectionGUID: 2Dom8jopQbe90lpf/eywYg== X-CSE-MsgGUID: fkaCltTBRWGYBJfJF9fEyA== X-IronPort-AV: E=McAfee;i="6700,10204,11101"; a="14875083" X-IronPort-AV: E=Sophos;i="6.08,234,1712646000"; d="scan'208";a="14875083" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by fmvoesa111.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2024 11:16:19 -0700 X-CSE-ConnectionGUID: 96RRl6OaTeabx/ud40lFpA== X-CSE-MsgGUID: YC08FACsSJGdJJV+WxxwHg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.08,234,1712646000"; d="scan'208";a="39823169" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jun 2024 11:16:18 -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, yuan.yao@intel.com Cc: reinette.chatre@intel.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH V9 0/2] KVM: x86: Make bus clock frequency for vAPIC timer configurable Date: Wed, 12 Jun 2024 11:16:10 -0700 Message-Id: X-Mailer: git-send-email 2.34.1 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 Changes from v8: - v8: https://lore.kernel.org/lkml/cover.1718043121.git.reinette.chatre@intel.com/ - Many changes to new udelay() utility patch as well as the APIC bus frequency test aimed to make it more robust (additional ASSERTs, consistent types, eliminate duplicate code, etc.) and useful with support for more user configuration. Please refer to individual patches for detailed changes. - Series applies cleanly to next branch of kvm-x86 with HEAD e4e9e1067138e5620cf0500c3e5f6ebfb9d322c8. Changes from v7: - v7: https://lore.kernel.org/lkml/cover.1717695426.git.reinette.chatre@intel.com/ - Use appropriate ioctl() return type. - Let new tsc_khz used by KVM selftest be of the same type as same variable used in kernel. - Please refer to individual patches for detailed changes. Changes from v6: - V6: https://lore.kernel.org/lkml/cover.1715017765.git.reinette.chatre@intel.com/ - KVM changes were merged into kvm-x86 misc and subsequently dropped from this submission. This submission only contains the selftest changes. https://lore.kernel.org/lkml/ZmBw9R_jkNLYXkJh@google.com/ - New patch to add udelay() utility to x86_64 KVM selftests. (Sean) - Many changes to APIC bus frequency selftest. Please refer to the patch for detailed changes. - Series applies cleanly to next branch of kvm-x86 with HEAD af0903ab52ee6d6f0f63af67fa73d5eb00f79b9a. Changes from v5: - v5: https://lore.kernel.org/lkml/cover.1714081725.git.reinette.chatre@intel.com/ - Rebased on latest of "next" branch of https://github.com/kvm-x86/linux.git - Added Xiaoyao Li and Yuan Yao's Reviewed-by tags. - Use the KVM selftest vm_create() wrapper instead of open coding it. (Zide) - Improve grammar of selftest description. (Zide) Changes from v4: - v4: https://lore.kernel.org/lkml/cover.1711035400.git.reinette.chatre@intel.com/ - Rename capability from KVM_CAP_X86_APIC_BUS_FREQUENCY to KVM_CAP_X86_APIC_BUS_CYCLES_NS. (Xiaoyao Li). - Include "Testing" section in cover letter. - Add Rick's Reviewed-by tags. - Rebased on latest of "next" branch of https://github.com/kvm-x86/linux.git Changes from v3: - v3: https://lore.kernel.org/all/cover.1702974319.git.isaku.yamahata@intel.com/ - Reworked all changelogs. - Regarding code changes: patches #1 and #2 are unchanged, patch #3 was reworked according to Sean's guidance, and patch #4 (the test) needed changes after rebase to kvm-x86/next and the implementation (patch #3) changes. - Added Reviewed-by tags to patches #1, #2, and #4, but removed Maxim's Reviewed-by tag from patch #3 because it changed so much. - Added a "Suggested-by" to patch #4 to reflect that it represents Sean's guidance. - Reworked cover to match customs (in subject line) and reflect feedback to patches: capability renamed to KVM_CAP_X86_APIC_BUS_FREQUENCY, clarify distinction between "core crystal clock" and "bus clock", and update pro/con list. - Please refer to individual patches for detailed changes. Changes from v2: - v2: https://lore.kernel.org/lkml/cover.1699936040.git.isaku.yamahata@intel.com/ - Removed APIC_BUS_FREQUENCY and apic_bus_frequency of struct kvm-arch. - Update the commit messages. - Added reviewed-by (Maxim Levitsky) - Use 1.5GHz instead of 1GHz as frequency for the test case. Changes from v1: - v1: https://lore.kernel.org/all/cover.1699383993.git.isaku.yamahata@intel.com/ - Added a test case - Fix a build error for i386 platform - Add check if vcpu isn't created. - Add check if lapic chip is in-kernel emulation. - Updated api.rst Summary ------- Add KVM_CAP_X86_APIC_BUS_CYCLES_NS capability to configure the APIC bus clock frequency for APIC timer emulation. Allow KVM_ENABLE_CAPABILITY(KVM_CAP_X86_APIC_BUS_CYCLES_NS) to set the frequency in nanoseconds. When using this capability, the user space VMM should configure CPUID leaf 0x15 to advertise the frequency. Description ----------- Vishal reported [1] that the TDX guest kernel expects a 25MHz APIC bus frequency but ends up getting interrupts at a significantly higher rate. 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. The KVM doesn't enumerate CPUID leaf 0x15 to the guest unless the user space VMM sets it using KVM_SET_CPUID. If the CPUID leaf 0x15 is enumerated, the guest kernel uses it as the APIC bus frequency. If not, the guest kernel measures the frequency based on other known timers like the ACPI timer or the legacy PIT. As reported in [1] the TDX guest kernel expects a 25MHz timer frequency but gets timer interrupt more frequently due to the 1GHz frequency used by KVM. To ensure that the guest doesn't have a conflicting view of the APIC bus frequency, allow the userspace to tell KVM to use the same frequency that TDX mandates instead of the default 1Ghz. There are several options to address this: 1. Make the KVM able to configure APIC bus frequency (this series). Pro: It resembles the existing hardware. The recent Intel CPUs adopts 25MHz. Con: Require the VMM to emulate the APIC timer at 25MHz. 2. Make the TDX architecture enumerate CPUID leaf 0x15 to configurable frequency or not enumerate it. Pro: Any APIC bus frequency is allowed. Con: Deviates from TDX architecture. 3. Make the TDX guest kernel use 1GHz when it's running on KVM. Con: The kernel ignores CPUID leaf 0x15. 4. Change CPUID leaf 0x15 under TDX to report the crystal clock frequency as 1 GHz. Pro: This has been the virtual APIC frequency for KVM guests for 13 years. Pro: This requires changing only one hard-coded constant in TDX. Con: It doesn't work with other VMMs as TDX isn't specific to KVM. Con: Core crystal clock frequency is also used to calculate TSC frequency. Con: If it is configured to value different from hardware, it will break the correctness of INTEL-PT Mini Time Count (MTC) packets in TDs. Testing ------- Tested on non-TDX host using included kselftest. Host running kernel with this series applied to "next" branch of https://github.com/kvm-x86/linux.git Tested on TDX host and TD using a modified version of x86/apic.c:test_apic_timer_one_shot() available from https://github.com/intel/kvm-unit-tests-tdx/blob/tdx/x86/apic.c. Host running TDX KVM development patches and QEMU with corresponding TDX changes. The test needed to be modified to (a) stop any lingering timers before the test starts, and (b) use CPUID 0x15 in TDX to accurately determine the TSC and APIC frequencies instead of making 1GHz assumption and use similar check as the kselftest test introduced in this series (while increasing the amount with which the frequency is allowed to deviate by 1%). The core changes made to x86/apic.c:test_apic_timer_one_shot() for this testing are shown below for reference. Work is in progress to upstream these modifications. @@ -477,11 +478,29 @@ static void lvtt_handler(isr_regs_t *regs) static void test_apic_timer_one_shot(void) { - uint64_t tsc1, tsc2; static const uint32_t interval = 0x10000; + uint64_t measured_apic_freq, tsc2, tsc1; + uint32_t tsc_freq = 0, apic_freq = 0; + struct cpuid cpuid_tsc = {}; #define APIC_LVT_TIMER_VECTOR (0xee) + /* + * CPUID 0x15 is not available in VMX, can use it to obtain + * TSC and APIC frequency for accurate testing + */ + if (is_tdx_guest()) { + cpuid_tsc = raw_cpuid(0x15, 0); + tsc_freq = cpuid_tsc.c * cpuid_tsc.b / cpuid_tsc.a; + apic_freq = cpuid_tsc.c; + } + /* + stop already fired local timer + the test case can be negative failure if the timer fired + after installed lvtt_handler but *before* + write to TIMICT again. + */ + apic_write(APIC_TMICT, 0); handle_irq(APIC_LVT_TIMER_VECTOR, lvtt_handler); /* One shot mode */ @@ -503,8 +522,16 @@ static void test_apic_timer_one_shot(void) * cases, the following should satisfy on all modern * processors. */ - report((lvtt_counter == 1) && (tsc2 - tsc1 >= interval), - "APIC LVT timer one shot"); + if (is_tdx_guest()) { + measured_apic_freq = interval * (tsc_freq / (tsc2 - tsc1)); + report((lvtt_counter == 1) && + (measured_apic_freq < apic_freq * 102 / 100) && + (measured_apic_freq > apic_freq * 98 / 100), + "APIC LVT timer one shot"); + } else { + report((lvtt_counter == 1) && (tsc2 - tsc1 >= interval), + "APIC LVT timer one shot"); + } } [1] https://lore.kernel.org/lkml/20231006011255.4163884-1-vannapurve@google.com/ Isaku Yamahata (1): KVM: selftests: Add test for configure of x86 APIC bus frequency Reinette Chatre (1): KVM: selftests: Add x86_64 guest udelay() utility tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/apic.h | 8 + .../selftests/kvm/include/x86_64/processor.h | 17 ++ .../selftests/kvm/lib/x86_64/processor.c | 11 + .../kvm/x86_64/apic_bus_clock_test.c | 202 ++++++++++++++++++ 5 files changed, 239 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/apic_bus_clock_test.c base-commit: e4e9e1067138e5620cf0500c3e5f6ebfb9d322c8 -- 2.34.1