Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp6546791ybv; Wed, 12 Feb 2020 14:34:40 -0800 (PST) X-Google-Smtp-Source: APXvYqwwQyNVHj6yhOKzuBtM2zTgXqpTg8/eX3f/S82PNYKIBorfN2IwSX6OxaCemWunw3Aoau63 X-Received: by 2002:a05:6808:50:: with SMTP id v16mr889949oic.133.1581546879818; Wed, 12 Feb 2020 14:34:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581546879; cv=none; d=google.com; s=arc-20160816; b=RXcmfreBgZwOXYUSSatDS863V8BsowZ7pVYfvEiS8YdAFQ5fuSeInDTv/s/l24xv5a 0X1yf/xPLH1/MCEa0uKR+Cq3PHFj3EAcstNMka9KPAzyqlXWU4xD0ZQAZzNdRb1qcwNQ Wit0cmoig8JIdlzS9cJWR+AhKqgYxRbhRr74hyU6J7Dl2562D2M66oHEcYqxMHJ6u3sF iCcBVOEfWtHsSafq4SGeyu/UT9N7eFTNjoywcccJrcr5JWkWIXEAvIlkfDBvN5OgDPGc G+2/ybq70h4isR3PNVJMK9HWRSBPhjmZma9tFuMGgSu2vfjJkNsj7PMtvlMtgXrpbs8y vkpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:content-disposition :mime-version:message-id:subject:to:from:date:ironport-sdr :dkim-signature; bh=h6//YngfznNHckkx9Ke5t7SZWnTpuxZy2r6uPSfxRCE=; b=al5Z1ovethJf/YgteUG39oPCNXM4LhMOR3lavN0JEGFSDPIzu3+NveWtumBI8TAKQi fCBW3+pqbatPb+zm/OGzzUdHKraPvSDJNpr6S3jYiFReoUnybH1SILwdF0KOtxuTll1o k3wkx/atJkBOdC+cQNZoAeJAgZXoBBS+X4AmY1TVcPVsW/Zdhv8TplxHPfEo//5JQPdz B5eQwyb9ktTYUy9jZs+kZENNTVBOWLMzVmLXcbngKPDJ/w382fLFueC99h5gNfrbgkQ/ cx2Di/ZZHMvw8clZyOu+rM6gK/uXDO4/nMpxZjvZcxIxPWTikVtXwlxq1BWLkiLgDT02 iK0A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=tPn3ZqL+; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c22si101399otr.97.2020.02.12.14.34.27; Wed, 12 Feb 2020 14:34:39 -0800 (PST) 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=@amazon.com header.s=amazon201209 header.b=tPn3ZqL+; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729176AbgBLWeR (ORCPT + 99 others); Wed, 12 Feb 2020 17:34:17 -0500 Received: from smtp-fw-9102.amazon.com ([207.171.184.29]:21070 "EHLO smtp-fw-9102.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727947AbgBLWeR (ORCPT ); Wed, 12 Feb 2020 17:34:17 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1581546856; x=1613082856; h=date:from:to:subject:message-id:mime-version; bh=h6//YngfznNHckkx9Ke5t7SZWnTpuxZy2r6uPSfxRCE=; b=tPn3ZqL+1B2ulBs4TtC/x5igj99yJsK+o2s+GmDjgGvwtour6nmLwgsW Jx0K27NMKOof3dltvdloC8oEntTvXDgWlK92Uta2UlBQfR5PVnl2+qxYG v+3SkVfjXHIFmXoE7e/xrM6UsaywsHadR6VWDEEnwhMGlPDLWMQ6Lkc/I o=; IronPort-SDR: 2sE+e2jd+GGBElALb7Ph5Zz+ct1lsXDGX/PXq+AtmOqwAOaaSF/aeEOS4lmkGyqTdM+lQJLtnt f0+oC1hLM+lQ== X-IronPort-AV: E=Sophos;i="5.70,434,1574121600"; d="scan'208";a="24723164" Received: from sea32-co-svc-lb4-vlan3.sea.corp.amazon.com (HELO email-inbound-relay-1d-37fd6b3d.us-east-1.amazon.com) ([10.47.23.38]) by smtp-border-fw-out-9102.sea19.amazon.com with ESMTP; 12 Feb 2020 22:34:13 +0000 Received: from EX13MTAUWB001.ant.amazon.com (iad55-ws-svc-p15-lb9-vlan2.iad.amazon.com [10.40.159.162]) by email-inbound-relay-1d-37fd6b3d.us-east-1.amazon.com (Postfix) with ESMTPS id 96E8728364D; Wed, 12 Feb 2020 22:34:04 +0000 (UTC) Received: from EX13D07UWB002.ant.amazon.com (10.43.161.131) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 12 Feb 2020 22:33:36 +0000 Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13D07UWB002.ant.amazon.com (10.43.161.131) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Wed, 12 Feb 2020 22:33:35 +0000 Received: from dev-dsk-anchalag-2a-9c2d1d96.us-west-2.amazon.com (172.22.96.68) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Wed, 12 Feb 2020 22:33:35 +0000 Received: by dev-dsk-anchalag-2a-9c2d1d96.us-west-2.amazon.com (Postfix, from userid 4335130) id 01AA9400D1; Wed, 12 Feb 2020 22:33:35 +0000 (UTC) Date: Wed, 12 Feb 2020 22:33:35 +0000 From: Anchal Agarwal To: , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH v3 08/12] xen/time: introduce xen_{save,restore}_steal_clock Message-ID: <20200212223335.GA4330@dev-dsk-anchalag-2a-9c2d1d96.us-west-2.amazon.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Munehisa Kamata Currently, steal time accounting code in scheduler expects steal clock callback to provide monotonically increasing value. If the accounting code receives a smaller value than previous one, it uses a negative value to calculate steal time and results in incorrectly updated idle and steal time accounting. This breaks userspace tools which read /proc/stat. top - 08:05:35 up 2:12, 3 users, load average: 0.00, 0.07, 0.23 Tasks: 80 total, 1 running, 79 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,30100.0%id, 0.0%wa, 0.0%hi, 0.0%si,-1253874204672.0%st This can actually happen when a Xen PVHVM guest gets restored from hibernation, because such a restored guest is just a fresh domain from Xen perspective and the time information in runstate info starts over from scratch. This patch introduces xen_save_steal_clock() which saves current values in runstate info into per-cpu variables. Its couterpart, xen_restore_steal_clock(), sets offset if it found the current values in runstate info are smaller than previous ones. xen_steal_clock() is also modified to use the offset to ensure that scheduler only sees monotonically increasing number. Signed-off-by: Munehisa Kamata Signed-off-by: Anchal Agarwal --- Changes since V2: * separated the previously merged patches * In V2, introduction of save/restore steal clock and usage in hibernation code was merged in a single patch --- drivers/xen/time.c | 29 ++++++++++++++++++++++++++++- include/xen/xen-ops.h | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/drivers/xen/time.c b/drivers/xen/time.c index 0968859c29d0..3560222cc0dd 100644 --- a/drivers/xen/time.c +++ b/drivers/xen/time.c @@ -23,6 +23,9 @@ static DEFINE_PER_CPU(struct vcpu_runstate_info, xen_runstate); static DEFINE_PER_CPU(u64[4], old_runstate_time); +static DEFINE_PER_CPU(u64, xen_prev_steal_clock); +static DEFINE_PER_CPU(u64, xen_steal_clock_offset); + /* return an consistent snapshot of 64-bit time/counter value */ static u64 get64(const u64 *p) { @@ -149,7 +152,7 @@ bool xen_vcpu_stolen(int vcpu) return per_cpu(xen_runstate, vcpu).state == RUNSTATE_runnable; } -u64 xen_steal_clock(int cpu) +static u64 __xen_steal_clock(int cpu) { struct vcpu_runstate_info state; @@ -157,6 +160,30 @@ u64 xen_steal_clock(int cpu) return state.time[RUNSTATE_runnable] + state.time[RUNSTATE_offline]; } +u64 xen_steal_clock(int cpu) +{ + return __xen_steal_clock(cpu) + per_cpu(xen_steal_clock_offset, cpu); +} + +void xen_save_steal_clock(int cpu) +{ + per_cpu(xen_prev_steal_clock, cpu) = xen_steal_clock(cpu); +} + +void xen_restore_steal_clock(int cpu) +{ + u64 steal_clock = __xen_steal_clock(cpu); + + if (per_cpu(xen_prev_steal_clock, cpu) > steal_clock) { + /* Need to update the offset */ + per_cpu(xen_steal_clock_offset, cpu) = + per_cpu(xen_prev_steal_clock, cpu) - steal_clock; + } else { + /* Avoid unnecessary steal clock warp */ + per_cpu(xen_steal_clock_offset, cpu) = 0; + } +} + void xen_setup_runstate_info(int cpu) { struct vcpu_register_runstate_memory_area area; diff --git a/include/xen/xen-ops.h b/include/xen/xen-ops.h index 3b3992b5b0c2..12b3f4474a05 100644 --- a/include/xen/xen-ops.h +++ b/include/xen/xen-ops.h @@ -37,6 +37,8 @@ void xen_time_setup_guest(void); void xen_manage_runstate_time(int action); void xen_get_runstate_snapshot(struct vcpu_runstate_info *res); u64 xen_steal_clock(int cpu); +void xen_save_steal_clock(int cpu); +void xen_restore_steal_clock(int cpu); int xen_setup_shutdown_event(void); -- 2.24.1.AMZN