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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 15B77C61DA4 for ; Thu, 2 Feb 2023 12:15:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:Subject:CC :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=jmLzA+RdtZK5khfB2Qy5IiqDD1i6YyPV+x64E2PMVgk=; b=NpyrKnY3Sl6NgR nDAmlWNURA/9AS9l3o29brAVEBpzaU53qtWzx0uzkVAcKOJEJ3rrkhXXqkblBp5Qyhf/byr/CAVwH iHe2sG48Lb9B+YFt5X5uFmZKUVVKhrqck75WGF3i5SSxSOQ/YAq1BMOKZ5Jiegmzqr8bOXQTF1jQV RvWZ8AU972Fl0bS6BC1BlQAOY9yQ6c96bxzjkZN0gFoMsNMhvSkWbmwx+CnRuSlD5kwX9lxnOUYYV uk4M618HBLrqyZKByM4CRbKEY1y0oaNc/q5Pja6vvba4ROI0XtY5+zj8GxaEu49AUdOxQOoeIG+0v AJRcpBZXi1M0++fY0g6Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNYUY-00Fin2-6U; Thu, 02 Feb 2023 12:15:02 +0000 Received: from smtp-fw-9103.amazon.com ([207.171.188.200]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pNYUU-00Fikx-Re for linux-arm-kernel@lists.infradead.org; Thu, 02 Feb 2023 12:15:00 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.de; i=@amazon.de; q=dns/txt; s=amazon201209; t=1675340098; x=1706876098; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=YEdeuKShvic7Pw2lcSJzml6AFBhDPQuX3TvfdNIgH3Q=; b=FOaPbJFdNJximLEoWMlkZ5fwEP+RCqZ3gYijKtzJWo2EcHRfvHJ/5amU sO+ypy/lBOGdppa6KGpBNO3a2F8BzvC4iAJNtedRFWsaUnv2N3oZ2b0A+ xNl+ZOfv/M5L5fcallHWD+YIwsodB77Zyi7ViXmCK4kj3mW1ue8IPzE5H k=; X-IronPort-AV: E=Sophos;i="5.97,267,1669075200"; d="scan'208";a="1098608469" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO email-inbound-relay-iad-1box-1dm6-7f722725.us-east-1.amazon.com) ([10.25.36.214]) by smtp-border-fw-9103.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Feb 2023 12:14:48 +0000 Received: from EX13D47EUC002.ant.amazon.com (iad12-ws-svc-p26-lb9-vlan2.iad.amazon.com [10.40.163.34]) by email-inbound-relay-iad-1box-1dm6-7f722725.us-east-1.amazon.com (Postfix) with ESMTPS id 84B62E1A6D; Thu, 2 Feb 2023 12:14:45 +0000 (UTC) Received: from EX19D018EUC003.ant.amazon.com (10.252.51.231) by EX13D47EUC002.ant.amazon.com (10.43.164.47) with Microsoft SMTP Server (TLS) id 15.0.1497.45; Thu, 2 Feb 2023 12:14:44 +0000 Received: from u2fd957c5393b54.ant.amazon.com (10.43.161.198) by EX19D018EUC003.ant.amazon.com (10.252.51.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.1118.24; Thu, 2 Feb 2023 12:14:40 +0000 From: Simon Veith To: CC: Simon Veith , , Catalin Marinas , Will Deacon , Marc Zyngier , James Morse , Suzuki K Poulose , Oliver Upton , Zenghui Yu , Subject: [PATCH] arm64: kvm: Expose timer offset directly via KVM_{GET,SET}_ONE_REG Date: Thu, 2 Feb 2023 13:13:14 +0100 Message-ID: <20230202121314.206195-1-sveith@amazon.de> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.43.161.198] X-ClientProxiedBy: EX13D32UWB001.ant.amazon.com (10.43.161.248) To EX19D018EUC003.ant.amazon.com (10.252.51.231) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230202_041458_985257_FC79AE25 X-CRM114-Status: GOOD ( 16.98 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org The virtual timer count register (CNTVCT_EL0) is virtualized by configuring offset register CNTVOFF_EL2 to subtract from the underlying raw hardware timer count when the guest reads the current count. Currently, we offer userspace the ability to serialize and deserialize only the absolute count register value, using KVM_{GET,SET}_ONE_REG with KVM_REG_ARM_TIMER_CNT. Internally, we then compute and set the offset register accordingly to obtain the requested count value. Allowing to set this timer count register only by absolute value poses some problems to virtual machine monitors that try to maintain the illusion of continuously ticking clocks to the guest: In workflows like live migration or liveupdate, the timers must be increased artificially to account for pause time. Any delays between userspace computing the correct timer count value and actually setting it in kernel space by KVM_SET_ONE_REG (such as can be incurred by scheduling) become visible as under-accounted pause time in the guest, meaning the guest observes that its system clock seems to have fallen behind its NTP time reference. The issue is further complicated when vCPU setup is performed by independent threads which may experience different delays, leading to jitter between the clocks of different vCPUs. We could deliver a more stable timer in such scenarios if we allowed userspace to set the offset with regards to the physical counter directly. Expose the KVM_REG_ARM_TIMER_OFF register directly to userspace, as an alternative view of the timer counts. By default, userspace still sees only the existing KVM_REG_ARM_TIMER_CNT register when querying the list with KVM_GET_REG_LIST, as that register value is portable across different VM hosts and thus safe to persist. Signed-off-by: Simon Veith CC: dwmw2@infradead.org CC: Catalin Marinas CC: Will Deacon CC: Marc Zyngier CC: James Morse CC: Suzuki K Poulose CC: Oliver Upton CC: Zenghui Yu CC: linux-arm-kernel@lists.infradead.org --- arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arch_timer.c | 10 ++++++++++ arch/arm64/kvm/guest.c | 9 +++++++++ include/kvm/arm_arch_timer.h | 1 + 4 files changed, 21 insertions(+) diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index a7a857f1784d..077699e403ab 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -260,6 +260,7 @@ struct kvm_arm_copy_mte_tags { #define KVM_REG_ARM_TIMER_CTL ARM64_SYS_REG(3, 3, 14, 3, 1) #define KVM_REG_ARM_TIMER_CVAL ARM64_SYS_REG(3, 3, 14, 0, 2) #define KVM_REG_ARM_TIMER_CNT ARM64_SYS_REG(3, 3, 14, 3, 2) +#define KVM_REG_ARM_TIMER_OFF ARM64_SYS_REG(3, 4, 14, 0, 3) /* KVM-as-firmware specific pseudo-registers */ #define KVM_REG_ARM_FW (0x0014 << KVM_REG_ARM_COPROC_SHIFT) diff --git a/arch/arm64/kvm/arch_timer.c b/arch/arm64/kvm/arch_timer.c index bb24a76b4224..f68b9edbea6b 100644 --- a/arch/arm64/kvm/arch_timer.c +++ b/arch/arm64/kvm/arch_timer.c @@ -830,6 +830,9 @@ int kvm_arm_timer_set_reg(struct kvm_vcpu *vcpu, u64 regid, u64 value) timer = vcpu_vtimer(vcpu); update_vtimer_cntvoff(vcpu, kvm_phys_timer_read() - value); break; + case KVM_REG_ARM_TIMER_OFF: + update_vtimer_cntvoff(vcpu, value); + break; case KVM_REG_ARM_TIMER_CVAL: timer = vcpu_vtimer(vcpu); kvm_arm_timer_write(vcpu, timer, TIMER_REG_CVAL, value); @@ -875,6 +878,9 @@ u64 kvm_arm_timer_get_reg(struct kvm_vcpu *vcpu, u64 regid) case KVM_REG_ARM_TIMER_CNT: return kvm_arm_timer_read(vcpu, vcpu_vtimer(vcpu), TIMER_REG_CNT); + case KVM_REG_ARM_TIMER_OFF: + return kvm_arm_timer_read(vcpu, + vcpu_vtimer(vcpu), TIMER_REG_OFF); case KVM_REG_ARM_TIMER_CVAL: return kvm_arm_timer_read(vcpu, vcpu_vtimer(vcpu), TIMER_REG_CVAL); @@ -915,6 +921,10 @@ static u64 kvm_arm_timer_read(struct kvm_vcpu *vcpu, val = kvm_phys_timer_read() - timer_get_offset(timer); break; + case TIMER_REG_OFF: + val = timer_get_offset(timer); + break; + default: BUG(); } diff --git a/arch/arm64/kvm/guest.c b/arch/arm64/kvm/guest.c index cf4c495a4321..a934189e1811 100644 --- a/arch/arm64/kvm/guest.c +++ b/arch/arm64/kvm/guest.c @@ -586,6 +586,10 @@ static unsigned long num_core_regs(const struct kvm_vcpu *vcpu) /** * ARM64 versions of the TIMER registers, always available on arm64 + * + * Note that the _OFF register is another view of the _CNT register, and is + * therefore not counted separately in NUM_TIMER_REGS, nor included in + * KVM_GET_REG_LIST. */ #define NUM_TIMER_REGS 3 @@ -595,6 +599,7 @@ static bool is_timer_reg(u64 index) switch (index) { case KVM_REG_ARM_TIMER_CTL: case KVM_REG_ARM_TIMER_CNT: + case KVM_REG_ARM_TIMER_OFF: case KVM_REG_ARM_TIMER_CVAL: return true; } @@ -609,6 +614,10 @@ static int copy_timer_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) if (put_user(KVM_REG_ARM_TIMER_CNT, uindices)) return -EFAULT; uindices++; + /* + * KVM_REG_ARM_TIMER_OFF is another view of KVM_REG_ARM_TIMER_CNT and + * therefore not included in the register list. + */ if (put_user(KVM_REG_ARM_TIMER_CVAL, uindices)) return -EFAULT; diff --git a/include/kvm/arm_arch_timer.h b/include/kvm/arm_arch_timer.h index cd6d8f260eab..66de7aa2018e 100644 --- a/include/kvm/arm_arch_timer.h +++ b/include/kvm/arm_arch_timer.h @@ -21,6 +21,7 @@ enum kvm_arch_timer_regs { TIMER_REG_CVAL, TIMER_REG_TVAL, TIMER_REG_CTL, + TIMER_REG_OFF, }; struct arch_timer_context { -- 2.34.1 Amazon Development Center Germany GmbH Krausenstr. 38 10117 Berlin Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B Sitz: Berlin Ust-ID: DE 289 237 879 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel