Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp2533579yba; Mon, 6 May 2019 07:37:03 -0700 (PDT) X-Google-Smtp-Source: APXvYqxSlkBuzxxtxSp/vY1j1SMajid0oa/sJKmsovb662uoNtfZPdArCWqm4iM9Dxkyg2Btu1KW X-Received: by 2002:a62:125c:: with SMTP id a89mr33641635pfj.93.1557153423183; Mon, 06 May 2019 07:37:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1557153423; cv=none; d=google.com; s=arc-20160816; b=HlZdN5Qa8CUBGotmNkwhXIVTYBPUKM8xGSJC+tbGpe4Oy7PyVo9ku4In5MedZDbnaJ dhghaVkUe4lUP1twLUa6eHMVT7rM5yxEclJXIstRGQUCiDpI2enqK6ccUyOsQ33oZjDZ Np9idJzQ5KLU5WSsRbrxNjLcfqm3EcWIvf/Rwgpm9YCW+NSYpbvw4NhMdlVGkeAt0Rd0 zNRMi4IcPkj7BxCbXtR8SpDcZqjq+RlfDvNTAcjtYseeYWMTui4GDS2HvNaQt8x3LjYh op2b5hNuSwpo+PL3l7Zg2nl9FVyXyp0vCAXwIRxgA7Sx7Ll9Q8eKYmB0LWQolBJ0Pq/2 WkVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Tjc8/vsNzRQV0yNos6AAKxAaQ9Vd1wYNIDRSUoyvWMI=; b=cwPhES00Y8+KjAZ5EI1kcY7PYz30GWwHj/MlPga0EnJ+DA9tnr0FuhjGJIcqtjScsF CouXBJh6ePC85R1wrVPe+upDSHxlPOC4cCIqZpC6AULSxS3ZIbKel2YrqIK6LlX2M+oo zx2IRwDTHGP2BeDOItVsTmv4pJ2rCAFnOAXsQTHhmYr42aoRjv7a9j3OxLrQqkVb1qr4 9aur3SjjS1kmPlWPOu1dwLkjuFX6HhsBbpUkD48cDQ+IpZKzddYgUosaSSvzpFLNqQGl FURo/x5PNxASD+Vk04S6+HK5PHKdLrSulOhti4h3KmGmNpK5pNB+P2Ss3pp8UgU3k1qv 3F8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sksalBO8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h18si14508996pgg.265.2019.05.06.07.36.46; Mon, 06 May 2019 07:37:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sksalBO8; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726895AbfEFOfQ (ORCPT + 99 others); Mon, 6 May 2019 10:35:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:55226 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726393AbfEFOfP (ORCPT ); Mon, 6 May 2019 10:35:15 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C970F2087F; Mon, 6 May 2019 14:35:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1557153314; bh=DytcGrl3mGpbEuLxTjKHLaMmoB5X9NoU6eNM5+cV+XU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sksalBO8JrVcLYzwThGhfQxLQ8wvYyw7rGD+MPbQ/pRS1D7JFWZqiwrtMctCJKq97 31Bv03QdjBHzkGX3RrEi1ozZHe+J1p8fN6DfoIs58JVM5sLj02LQeHdptbdGu7M8s8 k8bsgwCVkIz9lRCflwI0RWqqAMbPVolk4RY6YH7U= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Joao Martins , Liran Alon , Paolo Bonzini Subject: [PATCH 5.0 021/122] KVM: x86: Consider LAPIC TSC-Deadline timer expired if deadline too short Date: Mon, 6 May 2019 16:31:19 +0200 Message-Id: <20190506143056.649682511@linuxfoundation.org> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190506143054.670334917@linuxfoundation.org> References: <20190506143054.670334917@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Liran Alon commit c09d65d9eab69985c75f98ed64541229f6fa9aa6 upstream. If guest sets MSR_IA32_TSCDEADLINE to value such that in host time-domain it's shorter than lapic_timer_advance_ns, we can reach a case that we call hrtimer_start() with expiration time set at the past. Because lapic_timer.timer is init with HRTIMER_MODE_ABS_PINNED, it is not allowed to run in softirq and therefore will never expire. To avoid such a scenario, verify that deadline expiration time is set on host time-domain further than (now + lapic_timer_advance_ns). A future patch can also consider adding a min_timer_deadline_ns module parameter, similar to min_timer_period_us to avoid races that amount of ns it takes to run logic could still call hrtimer_start() with expiration timer set at the past. Reviewed-by: Joao Martins Signed-off-by: Liran Alon Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/lapic.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -1543,9 +1543,12 @@ static void start_sw_tscdeadline(struct now = ktime_get(); guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); - if (likely(tscdeadline > guest_tsc)) { - ns = (tscdeadline - guest_tsc) * 1000000ULL; - do_div(ns, this_tsc_khz); + + ns = (tscdeadline - guest_tsc) * 1000000ULL; + do_div(ns, this_tsc_khz); + + if (likely(tscdeadline > guest_tsc) && + likely(ns > lapic_timer_advance_ns)) { expire = ktime_add_ns(now, ns); expire = ktime_sub_ns(expire, lapic_timer_advance_ns); hrtimer_start(&apic->lapic_timer.timer,