Received: by 2002:ab2:7855:0:b0:1f9:5764:f03e with SMTP id m21csp136372lqp; Tue, 21 May 2024 22:37:38 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXH6hzomujHD7/hUDBpqeQ9lIWoD8vcZF6wiUmYvkvW5XGL8XZt4zd+IcCQeKQke8EFtC99vbVL9qsO/gkCw2Oi7JrLI7GXjzsaGIh+9g== X-Google-Smtp-Source: AGHT+IFPWWoNFHh3NqtERmXz/q1UlU2mZK8rQhCqDZqACTkvlOEGzseWQwGf5Ndxs7242dL4HnYC X-Received: by 2002:a17:906:71cb:b0:a5c:e3f6:bee8 with SMTP id a640c23a62f3a-a5d5fd5971fmr86294266b.37.1716356258280; Tue, 21 May 2024 22:37:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716356258; cv=pass; d=google.com; s=arc-20160816; b=EbskfV0DhL4TV0mqOnykTmA6PaGtdLYit2utkJyMNBqgmGqiZNXHLG0489ZuG/pDN5 oTwhD2DZ5G9szvIH1BBkHWo7nG7a6MNzUZP7HCZ1XQ1gwuq6vA+MGnwNr2R+iXIZhC7H MDvqXa/Dw8F0fqGjbaOM9t/5wYkcX+T0pV+XcOziwVtk4ziVwU6Eouwib4EBFQ1vFvEq 6jHKmKmw7ssh7K3JeO8+BWwOEGPK4MLc/WCc1HIyUz8foUzTff+FhSYzMc5h2KLln3HA a/ko5eSof6eFePEIKXC4sSdaupswjYqxTIBMvtEum+fhKylcQr2YzPTPsE3c8IGZJMuT ON0Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=emrPkVDjyCI61l9vFnKZcwwq0GnteVpJUYbCEB1MhmI=; fh=cHbBeIcWPXug0itFmRfudfa44WmtKnJkoGAOtdzsqoA=; b=GUqpFptTCBquHQtUm4QuLJdLFlgIcA6nDmGqqiXR9djhq4Vg/4yDLIQryiu48FMarW Hi/izpeV0+nhg9upX7CM/UWsgedCxyA01yg1am0B+x7yfNsbn1VGH3BZeCuzepjfmb3z SKpGQ+3rK3xYgmlQFmXNLMyMaAXue5Rnlbc/XqMUVh+i8WbSmYDRu84I5TAYf6PGoJrV kGsCvvGMLUO2Ip+4v5y3tUqdl5qsDcaXfRmza/i2VDp7gucCdiIhofb398jD6OZJfYvl Gzdrq/9pU6SxbDGj8l296vC/i4cP++69gzpr2Xb97rmXyw/5AjrOgVAmp7ehmRqJ5HZv 0djw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=UDNk1zPq; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-185558-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185558-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a5a45645916si1240478966b.967.2024.05.21.22.37.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 May 2024 22:37:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-185558-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=UDNk1zPq; arc=pass (i=1 dkim=pass dkdomain=infradead.org); spf=pass (google.com: domain of linux-kernel+bounces-185558-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-185558-linux.lists.archive=gmail.com@vger.kernel.org" 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 am.mirrors.kernel.org (Postfix) with ESMTPS id 167461F260BD for ; Wed, 22 May 2024 00:24:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 67108145B0A; Wed, 22 May 2024 00:18:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="UDNk1zPq" Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (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 5253F2595; Wed, 22 May 2024 00:18:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716337118; cv=none; b=gPIJ3OxwGyQcLlmHM497UMwSqt1Fs5K12V3PTRROfRfYojZzhoJlBXj8CLXXGFQlStmz0yudFt0sA+mfMKUQBHDMohicm/bkvvgA9MtS1Mm1MRkHbhsdfiM2pRAPN7Bi1KGL12W4g3cMa7J6pGGKG619Fp9eRW60g+tkRZMcb3E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716337118; c=relaxed/simple; bh=+Hq2uw0ZiCJvZgejSs2T5jnEENChFImfxm55VF9PvqY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BBGp1atHuG5ur54HvE3aJtikEm/QjwmmSeIjYc+qPaqiKW/nUrutOeD0lQOPE26rx3/6YsdK7pQmyJfgMph/lfqcRfXzKk6oKeuHkGCNNAw8apF3JReIz96kLWr/nwrvTzu3LYxgToWnvNZ6gmWOjYWjVAG5YSNru+kkAwLvTuU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=UDNk1zPq; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=emrPkVDjyCI61l9vFnKZcwwq0GnteVpJUYbCEB1MhmI=; b=UDNk1zPqeyjrbjVxVZMBKzUQTG szGOHqyNiQp7Wvb1FkWwzNPv5MAdR2CGveIg7bjKPsX+HbBvmwhj++ocPT6QjnuhUeiCwMzhYEchg yakW2NcH8WR6UvyH8sPvxaRNVx2uYz1fv7J1Pie81aGHOtdc+JT5BLNa9gPAOQLT7V49gy+PrNIRo LYAIgmvrwBzhBavnfxVvwSUjf58jibZYFUgix9tTDe7wK4jnOv1GMyvzoltSnbj21gSFncby8gB45 /BESobMug2r1HdNvr6qG3hIpujC+R4rIL9oTY+iT2jdq4r4q3fcJf9XPq/icgtB10CkjwhExQFNxA uEkka6CQ==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.97.1 #2 (Red Hat Linux)) id 1s9ZgT-0000000081T-18OM; Wed, 22 May 2024 00:18:21 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.97.1 #2 (Red Hat Linux)) id 1s9ZgS-00000002b5Y-3M9T; Wed, 22 May 2024 01:18:20 +0100 From: David Woodhouse To: kvm@vger.kernel.org Cc: Paolo Bonzini , Jonathan Corbet , Sean Christopherson , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Paul Durrant , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , Shuah Khan , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, jalliste@amazon.co.uk, sveith@amazon.de, zide.chen@intel.com, Dongli Zhang , Chenyi Qiang Subject: [RFC PATCH v3 20/21] KVM: x86/xen: Prevent runstate times from becoming negative Date: Wed, 22 May 2024 01:17:15 +0100 Message-ID: <20240522001817.619072-21-dwmw2@infradead.org> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240522001817.619072-1-dwmw2@infradead.org> References: <20240522001817.619072-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse When kvm_xen_update_runstate() is invoked to set a vCPU's runstate, the time spent in the previous runstate is accounted. This is based on the delta between the current KVM clock time, and the previous value stored in vcpu->arch.xen.runstate_entry_time. If the KVM clock goes backwards, that delta will be negative. Or, since it's an unsigned 64-bit integer, very *large*. Linux guests deal with that particularly badly, reporting 100% steal time for ever more (well, for *centuries* at least, until the delta has been consumed). So when a negative delta is detected, just refrain from updating the runstates until the KVM clock catches up with runstate_entry_time again. The userspace APIs for setting the runstate times do not allow them to be set past the current KVM clock, but userspace can still adjust the KVM clock *after* setting the runstate times, which would cause this situation to occur. Signed-off-by: David Woodhouse --- arch/x86/kvm/xen.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/xen.c b/arch/x86/kvm/xen.c index 014048c22652..3d4111de4472 100644 --- a/arch/x86/kvm/xen.c +++ b/arch/x86/kvm/xen.c @@ -538,24 +538,34 @@ void kvm_xen_update_runstate(struct kvm_vcpu *v, int state) { struct kvm_vcpu_xen *vx = &v->arch.xen; u64 now = get_kvmclock_ns(v->kvm); - u64 delta_ns = now - vx->runstate_entry_time; u64 run_delay = current->sched_info.run_delay; + s64 delta_ns = now - vx->runstate_entry_time; + s64 steal_ns = run_delay - vx->last_steal; if (unlikely(!vx->runstate_entry_time)) vx->current_runstate = RUNSTATE_offline; + vx->last_steal = run_delay; + + /* + * If KVM clock time went backwards, stop updating until it + * catches up (or the runstates are reset by userspace). + */ + if (delta_ns < 0) + return; + /* * Time waiting for the scheduler isn't "stolen" if the * vCPU wasn't running anyway. */ - if (vx->current_runstate == RUNSTATE_running) { - u64 steal_ns = run_delay - vx->last_steal; + if (vx->current_runstate == RUNSTATE_running && steal_ns > 0) { + if (steal_ns > delta_ns) + steal_ns = delta_ns; delta_ns -= steal_ns; vx->runstate_times[RUNSTATE_runnable] += steal_ns; } - vx->last_steal = run_delay; vx->runstate_times[vx->current_runstate] += delta_ns; vx->current_runstate = state; -- 2.44.0