From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.5 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_PASS,USER_AGENT_MUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14D3BC282DA for ; Wed, 17 Apr 2019 14:33:46 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E3F84206BA for ; Wed, 17 Apr 2019 14:33:45 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732595AbfDQOdo (ORCPT ); Wed, 17 Apr 2019 10:33:44 -0400 Received: from mga02.intel.com ([134.134.136.20]:50509 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731959AbfDQOdo (ORCPT ); Wed, 17 Apr 2019 10:33:44 -0400 X-Amp-Result: UNSCANNABLE X-Amp-File-Uploaded: False Received: from orsmga001.jf.intel.com ([10.7.209.18]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 17 Apr 2019 07:33:43 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,362,1549958400"; d="scan'208";a="224334312" Received: from sjchrist-coffee.jf.intel.com (HELO linux.intel.com) ([10.54.74.181]) by orsmga001.jf.intel.com with ESMTP; 17 Apr 2019 07:33:43 -0700 Date: Wed, 17 Apr 2019 07:33:43 -0700 From: Sean Christopherson To: Paolo Bonzini Cc: Radim =?utf-8?B?S3LEjW3DocWZ?= , kvm@vger.kernel.org, Liran Alon , Wanpeng Li Subject: Re: [PATCH v3 2/9] KVM: lapic: Convert guest TSC to host time domain when delaying Message-ID: <20190417143342.GA8567@linux.intel.com> References: <20190416203248.29429-1-sean.j.christopherson@intel.com> <20190416203248.29429-3-sean.j.christopherson@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org On Wed, Apr 17, 2019 at 02:56:53PM +0200, Paolo Bonzini wrote: > On 16/04/19 22:32, Sean Christopherson wrote: > > + /* ndelay uses delay_tsc whenever the hardware has TSC, thus always. */ > > + if (guest_tsc < tsc_deadline) { > > + ns = (tsc_deadline - guest_tsc) * 1000000ULL; > > + do_div(ns, vcpu->arch.virtual_tsc_khz); > > + ndelay(min_t(u32, ns, lapic_timer_advance_ns)); > > + } > > > > This min_t would allow the timer to expire in advance of the indicated > deadline. If this happens, wouldn't it be a bug elsewhere? Maybe? In this exact variation of the code, no, since userspace can modify lapic_timer_advance_ns while the vCPU is running. Obviously that case goes away when lapic_timer_advance_ns is no longer directly consumed by vCPUS. Originally, the check was added because lockup occurred if the guest's TSC was sent backwards[1]. The discussion surrounding the bugfix patch[2] is probably more interesting/amusing/relevant. We're basically rehashing that discussion. :-) Anyways, capping at a KVM controlled value is prudent since not doing so can hang the host if something does go wrong. As for the __delay() vs ndelay() discussion, I'll try to dig more into how/why I saw the timer fire with an incorrect TSC, i.e. why ndelay() "works" but __delay() does not. https://bugzilla.redhat.com/show_bug.cgi?id=1337667 https://patchwork.kernel.org/patch/9130687/