From patchwork Fri Oct 6 01:54:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 9988227 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E932E60291 for ; Fri, 6 Oct 2017 01:55:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC5DC28CE0 for ; Fri, 6 Oct 2017 01:55:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D112A28D53; Fri, 6 Oct 2017 01:55:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9187428CE0 for ; Fri, 6 Oct 2017 01:55:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751654AbdJFByt (ORCPT ); Thu, 5 Oct 2017 21:54:49 -0400 Received: from mail-qt0-f196.google.com ([209.85.216.196]:52975 "EHLO mail-qt0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751604AbdJFByq (ORCPT ); Thu, 5 Oct 2017 21:54:46 -0400 Received: by mail-qt0-f196.google.com with SMTP id o52so28911795qtc.9; Thu, 05 Oct 2017 18:54:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=oxKV/P0ttnjLUO53irrASvGRLVTb2SH9XM3Rwv9P9eU=; b=Gc8xFuXLdRAXf5BUrfqB4oqyhbAvOyhcsMCPpdwopi6ZcX3SrDtmyUdF49kYzURMlf XS65dYwzrFpucGzC7Jrzb0aOALAVAz/OoTVfsuwdwVWwQ1noe1JZKprWrufgfnSLzgWN b2U25KTiTWzZ3qDLlW3z9bM0GJ78lFdryAZecEXcN8T9PfGLDK9SbE13vlbnXO43vhdz LQ1kQXxEVXGR+/6NKgg3Sa6vM95C8bUFmeYU0CrSvPNCJp8mExFirNE7mJHV2NktLPhg MJ2CzN8wiWmJwiTg5DAAQAW3XZMb55n9Qcb7ST4cXp76gkvUyVMcN21KuiU46SWrHDqe yvTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=oxKV/P0ttnjLUO53irrASvGRLVTb2SH9XM3Rwv9P9eU=; b=pXLwkYSX7Xd3sf8EjmmBQU4wcSVr9zufTN8Kqvn7a8Xj+QE72Cy9u2RJPeu/3Ac5Js VvUyDuM9yjjonxLPi9M1ZOZ+UuOzAndbGuqKGVkj4DabvqYgv36k6sG7HdVnhkJZSk/C zFhV53gn5wBC5DhWui38pfg0U1kcNGUXBm7XYfCZlYh5C6Pf9MNkiYXFawCe+1yVRgTj ZpPAojJ8Qlpox4C/j+kZHNs9sdEZI2L2yrBJ+zsH52LeLMCBo5WqCKqB/8rf++ZPUnJr s/UQ9yfl6HZSYSFmMSdIjkVnHNg/LW8tZnygrFdHaQiarJKjzPPLDp3gIyAK/T/NbDi8 ntuw== X-Gm-Message-State: AMCzsaWNHNBh74O/RPOEpUuLDxORHan0nTvKWFC6JaqS9q6WMfbduIsb w0WCTuNy9arilKUyBh1B4VRMozpt X-Google-Smtp-Source: AOwi7QBVEB+Q7/elIawRV5jAbEfMIjRSphJlMCedK5e+92w3TOgUXk5VmnASpEOjw0AFxE5DcLGD5g== X-Received: by 10.237.41.135 with SMTP id o7mr859368qtd.222.1507254885708; Thu, 05 Oct 2017 18:54:45 -0700 (PDT) Received: from localhost ([67.205.145.118]) by smtp.gmail.com with ESMTPSA id n4sm274274qkf.49.2017.10.05.18.54.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 05 Oct 2017 18:54:45 -0700 (PDT) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Wanpeng Li Subject: [PATCH v6 2/3] KVM: LAPIC: Keep timer running when switching between one-shot and periodic mode Date: Thu, 5 Oct 2017 18:54:25 -0700 Message-Id: <1507254866-5020-3-git-send-email-wanpeng.li@hotmail.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1507254866-5020-1-git-send-email-wanpeng.li@hotmail.com> References: <1507254866-5020-1-git-send-email-wanpeng.li@hotmail.com> MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li If we take TSC-deadline mode timer out of the picture, the Intel SDM does not say that the timer is disable when the timer mode is change, either from one-shot to periodic or vice versa. After this patch, the timer is no longer disarmed on change of mode, so the counter (TMCCT) keeps counting down. So what does a write to LVTT changes ? On baremetal, the change of mode is probably taken into account only when the counter reach 0. When this happen, LVTT is use to figure out if the counter should restard counting down from TMICT (so periodic mode) or stop counting (if one-shot mode). This patch is based on observation of the behavior of the APIC timer on baremetal as well as check that they does not go against the description written in the Intel SDM. Cc: Paolo Bonzini Cc: Radim Krčmář Signed-off-by: Wanpeng Li --- arch/x86/kvm/lapic.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index 8841bb5..14f63b3 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1329,10 +1329,14 @@ static void apic_update_lvtt(struct kvm_lapic *apic) if (apic->lapic_timer.timer_mode != timer_mode) { if (apic_lvtt_tscdeadline(apic) != (timer_mode == - APIC_LVT_TIMER_TSCDEADLINE)) + APIC_LVT_TIMER_TSCDEADLINE)) { kvm_lapic_set_reg(apic, APIC_TMICT, 0); + hrtimer_cancel(&apic->lapic_timer.timer); + } + if (apic_lvtt_oneshot(apic) && (timer_mode == + APIC_LVT_TIMER_PERIODIC)) + limit_periodic_timer_frequency(apic); apic->lapic_timer.timer_mode = timer_mode; - hrtimer_cancel(&apic->lapic_timer.timer); } }