Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp4482959ybx; Mon, 4 Nov 2019 14:10:30 -0800 (PST) X-Google-Smtp-Source: APXvYqxTk1HrKQmaQQGetKX3DAxOop/j5xIW9+uVDOjGuChoGPkRfulbwNdnbwR45i/zRN/Qf7Kj X-Received: by 2002:a50:fa4a:: with SMTP id c10mr13614601edq.51.1572905430530; Mon, 04 Nov 2019 14:10:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1572905430; cv=none; d=google.com; s=arc-20160816; b=q//1BXZNsrYXrmqG3ZjGMmyILoS35mIQoXPQEyTIbVD6LA9ca9uWkUHfVxdGusr/SJ y0Rzhi1jNZCPcVgBUGYlg1e+V0RtzhRcjXsp0hJ+h7LE2PwfBoCkTfSnJ/9Et1Twa50q ycBOM3rkQR4B5hdvDMl6oPfv3Bzo5Q7xQcEhNDRsIqwAYNONKpXQWqx1rG+wMWH3rP7M oYq1qrZSJGvbAL034PN+bH25U5el4KvBG21SNN+jID5xaBvaMUQCZQMLR1M4T/qOI9L8 5hYp4ZA2Htyk93T4Ic3RVMYqeSYUEUZVPJZC7ntcYnONob3fy/rhGVAMYXMnpJzNiYYA OQrA== 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=dDnkGIj+50XvhdNosMPdLjEe569zDiAkLXr9zM0xrxU=; b=dX1FoC1j7Rg0B7ktzbXynqR1Nc/kdpD2294mDXqEZgFdm5U61chZN7rir9YyRGb2ka 9RyoqQ0kg06V0LCytDpXSfVtUpT+HbMHNCUhN6IG/x5d6LdRKQ/SUgndvjDupvyPiJsc DCBfxqEYiiJKF/h7m0rxTUnmTqffODJcAwO6UUh4G3NZn9bnjdWa4kgaaZuOYWjAhPKB mdYxidG8HjqzAfYLXsUAEl1doheytyPR3lWe2hdtjatwgX4pQj+QkBsZ+tmcbE+FwJoN jcFv6qfiHOm5M/8/hRI7z2seKN/4dZdMX+4j1fVjcjmX5hnMxZKSh3Cya+AZ5P9MHnvR xcQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SNLF9UQs; 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 w7si8483873edj.96.2019.11.04.14.10.07; Mon, 04 Nov 2019 14:10:30 -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=@kernel.org header.s=default header.b=SNLF9UQs; 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 S2390440AbfKDWJj (ORCPT + 99 others); Mon, 4 Nov 2019 17:09:39 -0500 Received: from mail.kernel.org ([198.145.29.99]:42790 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390055AbfKDWJi (ORCPT ); Mon, 4 Nov 2019 17:09:38 -0500 Received: from localhost (6.204-14-84.ripe.coltfrance.com [84.14.204.6]) (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 2EFBA214D8; Mon, 4 Nov 2019 22:09:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1572905377; bh=WUYtsEowu6tUKxR++J7FijFndFCjk6G0o2q2sXvWxvY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SNLF9UQsZ2I8F1fWJmUIbA1skzUX8yGJR/HrGnTByGuOFquvqjpGYHBaRmIBNUWI6 BSsy0299Td4hQ0Vy99azXZJY0fZIaILcw+8oE3oJLm3vwB7yOnR/56UcHHSMBg38tp 3yUICoCvoSSneaH7W90kha54tW3oDRoJVF47vUFs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Heiko Carstens , Vasily Gorbik Subject: [PATCH 5.3 124/163] s390/idle: fix cpu idle time calculation Date: Mon, 4 Nov 2019 22:45:14 +0100 Message-Id: <20191104212149.211530990@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20191104212140.046021995@linuxfoundation.org> References: <20191104212140.046021995@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: Heiko Carstens commit 3d7efa4edd07be5c5c3ffa95ba63e97e070e1f3f upstream. The idle time reported in /proc/stat sometimes incorrectly contains huge values on s390. This is caused by a bug in arch_cpu_idle_time(). The kernel tries to figure out when a different cpu entered idle by accessing its per-cpu data structure. There is an ordering problem: if the remote cpu has an idle_enter value which is not zero, and an idle_exit value which is zero, it is assumed it is idle since "now". The "now" timestamp however is taken before the idle_enter value is read. Which in turn means that "now" can be smaller than idle_enter of the remote cpu. Unconditionally subtracting idle_enter from "now" can thus lead to a negative value (aka large unsigned value). Fix this by moving the get_tod_clock() invocation out of the loop. While at it also make the code a bit more readable. A similar bug also exists for show_idle_time(). Fix this is as well. Cc: Signed-off-by: Heiko Carstens Signed-off-by: Vasily Gorbik Signed-off-by: Greg Kroah-Hartman --- arch/s390/kernel/idle.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) --- a/arch/s390/kernel/idle.c +++ b/arch/s390/kernel/idle.c @@ -69,18 +69,26 @@ DEVICE_ATTR(idle_count, 0444, show_idle_ static ssize_t show_idle_time(struct device *dev, struct device_attribute *attr, char *buf) { + unsigned long long now, idle_time, idle_enter, idle_exit, in_idle; struct s390_idle_data *idle = &per_cpu(s390_idle, dev->id); - unsigned long long now, idle_time, idle_enter, idle_exit; unsigned int seq; do { - now = get_tod_clock(); seq = read_seqcount_begin(&idle->seqcount); idle_time = READ_ONCE(idle->idle_time); idle_enter = READ_ONCE(idle->clock_idle_enter); idle_exit = READ_ONCE(idle->clock_idle_exit); } while (read_seqcount_retry(&idle->seqcount, seq)); - idle_time += idle_enter ? ((idle_exit ? : now) - idle_enter) : 0; + in_idle = 0; + now = get_tod_clock(); + if (idle_enter) { + if (idle_exit) { + in_idle = idle_exit - idle_enter; + } else if (now > idle_enter) { + in_idle = now - idle_enter; + } + } + idle_time += in_idle; return sprintf(buf, "%llu\n", idle_time >> 12); } DEVICE_ATTR(idle_time_us, 0444, show_idle_time, NULL); @@ -88,17 +96,24 @@ DEVICE_ATTR(idle_time_us, 0444, show_idl u64 arch_cpu_idle_time(int cpu) { struct s390_idle_data *idle = &per_cpu(s390_idle, cpu); - unsigned long long now, idle_enter, idle_exit; + unsigned long long now, idle_enter, idle_exit, in_idle; unsigned int seq; do { - now = get_tod_clock(); seq = read_seqcount_begin(&idle->seqcount); idle_enter = READ_ONCE(idle->clock_idle_enter); idle_exit = READ_ONCE(idle->clock_idle_exit); } while (read_seqcount_retry(&idle->seqcount, seq)); - - return cputime_to_nsecs(idle_enter ? ((idle_exit ?: now) - idle_enter) : 0); + in_idle = 0; + now = get_tod_clock(); + if (idle_enter) { + if (idle_exit) { + in_idle = idle_exit - idle_enter; + } else if (now > idle_enter) { + in_idle = now - idle_enter; + } + } + return cputime_to_nsecs(in_idle); } void arch_cpu_idle_enter(void)