Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp455003imm; Fri, 14 Sep 2018 00:48:03 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYMPTNtzeP6vSMGkSwNNg1MjQe88RVAAITIV7KSqlCtMcqCil+MV6fWifwWSKEp89L7wv4S X-Received: by 2002:a62:5d89:: with SMTP id n9-v6mr11135656pfj.102.1536911283086; Fri, 14 Sep 2018 00:48:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536911283; cv=none; d=google.com; s=arc-20160816; b=BBauoWxbuxHkoCMmcV8sidwtLvhvNay51z1uwpWzX1Zk54dNL59gUgaHqkYGKThXTc aLlEzTwtMFlM2BHToQGS9QkN29pf4OUOclUrUyopMJm3kyjr1Zc0lnLCmBWEe11q0IqI 5B+EJhQ5BFhvsiLi6+OCObGW//i8tzYw+sH4SwlEinnumtgUNxR6mU7/tsqyntPcMG91 YGsyVCG2Yasjx2a+15IuqcYh9atlOPxueavBV6Bv03ZH7TdmUQBm0IIrOaGk+qzb3Kso 4fYlsOBexpINvl4t9JtcRGtcEDORgE+SNL9L89MjLlHzBBqOpnaNHMyUdOKRlev0DEM4 H6Jw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version; bh=FrKyp6qZRzwBDgDKF/oz/lNGiw1P+ewjt4SNqsBGbQA=; b=S+qJifPmIKEhyIYesEWdgBrvpg4CKBR1nSlnCfOs4xlZv0T70KETK77CfH7Y0ftxqt R3q3cikSLK14if+G7AmQry8jBii0NSUdR9fT4+hRvrFrwGk9KU/F2W01p+UCGsU//RsQ E12lg1eHcNypRmXWa+7Mg2UVF7/ZhXd+t6dnBDQLNGI9nat+wNOIJz3LR+CF10SmcmFT RlS1OND8wO244USocyBJ43GHhutgsglldXFuJ0rzVUbFbEoc5U+7xd7UfGJ/nDrQJMOc ThdS+yWKvYJhKtrqgVi9/Jw02OETqeAHTf5V4TUTzvZ4NevquA/i+/JQWqBW7/e69u8V fL1Q== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id i9-v6si6379032pgk.403.2018.09.14.00.47.46; Fri, 14 Sep 2018 00:48: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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727804AbeINNAy (ORCPT + 99 others); Fri, 14 Sep 2018 09:00:54 -0400 Received: from mail-ot1-f67.google.com ([209.85.210.67]:33039 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727194AbeINNAy (ORCPT ); Fri, 14 Sep 2018 09:00:54 -0400 Received: by mail-ot1-f67.google.com with SMTP id i10-v6so3718986oth.0; Fri, 14 Sep 2018 00:47:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=FrKyp6qZRzwBDgDKF/oz/lNGiw1P+ewjt4SNqsBGbQA=; b=QmmAAJRDx4qw1K+Jo9hmbT5kaYiGi9pZXFye+uxdGOmvuCFHYyz2ZbX9p7zHACo4Vm WfxH/W84CdPGxgV78yHhj/q2cBS5F6NwwkNa9L9Kr3DAQgNksGlr/DoAuVsmWmTvKELY ZA6Kt96VJnSbua+BMqtkEVgxIahAo2rUzcDQmvigb4+hkheRYQkO7CxHHpMhxIJ2a2jV OXW3hRt9Z2UHawMmwPGHIHPJbyFxhV5tdW8NlwHjvuMSIO3cMI6Zi0JtRQ3Gqzdl5AW0 D5aRmyJJS1wQVcT4NpETR5WrQhMWjmrB/rtNihMY9YPnl86i+9zQ9p/gZpXVDNHrNHS6 HNAg== X-Gm-Message-State: APzg51Ac6q5eQr/SaU0mYqzCQLhME5BWdoSCqgs3bRkKAgdJrjuukPLe eqNXEf8ToPhvZqTppr6GBE1nYVS10jtfEWD7xDEKJQ== X-Received: by 2002:a9d:19e8:: with SMTP id k95-v6mr3848225otk.248.1536911259768; Fri, 14 Sep 2018 00:47:39 -0700 (PDT) MIME-Version: 1.0 References: <9611469.2z7W9akjOQ@aspire.rjw.lan> <20180914074052.GF24124@hirez.programming.kicks-ass.net> In-Reply-To: <20180914074052.GF24124@hirez.programming.kicks-ass.net> From: "Rafael J. Wysocki" Date: Fri, 14 Sep 2018 09:47:28 +0200 Message-ID: Subject: Re: [PATCH] PM / suspend: Count suspend-to-idle loop as sleep time To: Peter Zijlstra Cc: "Rafael J. Wysocki" , Linux PM , ACPI Devel Maling List , Linux Kernel Mailing List , Mika Westerberg , Thomas Gleixner , Srinivas Pandruvada , Vitaly Kuznetsov Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 14, 2018 at 9:41 AM Peter Zijlstra wrote: > > On Fri, Sep 14, 2018 at 08:59:03AM +0200, Rafael J. Wysocki wrote: > > From: Rafael J. Wysocki > > > > There is a difference in behavior between suspend-to-idle and > > suspend-to-RAM in the timekeeping handling that leads to functional > > issues. Namely, every iteration of the loop in s2idle_loop() > > increases the monotinic clock somewhat, even if timekeeping_suspend() > > and timekeeping_resume() are invoked from s2idle_enter(), and if > > many of them are carried out in a row, the monotonic clock can grow > > significantly while the system is regarded as suspended, which > > doesn't happen during suspend-to-RAM and so it is unexpected and > > leads to confusion and misbehavior in user space (similar to what > > ensued when we tried to combine the boottime and monotonic clocks). > > > > To avoid that, count all iterations of the loop in s2idle_loop() > > as "sleep time" and adjust the clock for that on exit from > > suspend-to-idle. > > > > [That also covers systems on which timekeeping is not suspended > > by by s2idle_enter().] > > > > Signed-off-by: Rafael J. Wysocki > > Do we want a 'warning' of sorts when the delta becomes significant (for > whatever that is) ? That might be an indication that there are frequent > wakeups which we might not be expecting. Of keep the number of spurious > wakeups in a stat counter somewhere -- something to look at if the > battery drains faster than expected. If you echo 1 to /sys/power/pm_debug_messages, dmesg will tell you that (with gory details). :-) > Otherwise: > > Acked-by: Peter Zijlstra (Intel) > > One minor nit below: > > > --- > > kernel/power/suspend.c | 18 ++++++++++++++++++ > > 1 file changed, 18 insertions(+) > > > > Index: linux-pm/kernel/power/suspend.c > > =================================================================== > > --- linux-pm.orig/kernel/power/suspend.c > > +++ linux-pm/kernel/power/suspend.c > > @@ -109,8 +109,12 @@ static void s2idle_enter(void) > > > > static void s2idle_loop(void) > > { > > + ktime_t start, delta; > > + > > pm_pr_dbg("suspend-to-idle\n"); > > > > + start = ktime_get(); > > + > > for (;;) { > > int error; > > > > @@ -150,6 +154,20 @@ static void s2idle_loop(void) > > pm_wakeup_clear(false); > > } > > > > + /* > > + * If the monotonic clock difference between the start of the loop and > > + * this point is too large, user space may get confused about whether or > > + * not the system has been suspended and tasks may get killed by > > + * watchdogs etc., so count the loop as "sleep time" to compensate for > > + * that. > > + */ > > + delta = ktime_sub(ktime_get(), start); > > + if (ktime_to_ns(delta) > 0) { > > + struct timespec64 timespec64_delta = ktime_to_timespec64(delta); > > + > > + timekeeping_inject_sleeptime64(×pec64_delta); > > + } > > + > > pm_pr_dbg("resume from suspend-to-idle\n"); > > } > > Like I mentioned yesterday; I myself prefer the form: > > > u64 stamp = ktimer_get_ns(); > > for (;;) { > /* ... */ > } > > stamp = ktime_get_ns() - stamp; > if (stamp) > timekeeping_inject_sleeptime64(ns_to_timespec64(ns)); > > > Esp. since ktime_t _is_ s64 these days, there is no point in keep using > all the weird ktime helpers that make the code harder to read. Looks like a good idea, let me try that.