Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp575527imm; Fri, 14 Sep 2018 03:07:06 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZz2MqiJrXPG9+8GXiTafd3Ge/AmIw1cYpi7pVzR5JA5v1ipUo3mRoFl6ZWuATh+7xVJYcj X-Received: by 2002:a17:902:b68d:: with SMTP id c13-v6mr11362968pls.139.1536919626231; Fri, 14 Sep 2018 03:07:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536919626; cv=none; d=google.com; s=arc-20160816; b=AkH4apHN0naMR8zaeqgzDOZPy7bEETvG5jJIcKBJ5UpJMHQ0fawZbhA4VjA0qvb6++ x0TrItHXzB2ZO+g/xvpHJKUc2vn8nfoOLZh8kYvVxGB4JoVQhPfVeVCuzeMHajWesYj6 IiK1P0x4ZJsqbbUqftIPa059lyDrK9b1K7UD4pehJWqkbpAbQHo4oa7o4tSjeiDMNsRF veuNI724DQcsmFSrfaulA0TK/9gtOixwu/aWlFZSKD/4O/E635K/9vpqMwmmoTiOnNeS 0EkgryXt6pINADb9BLgaZAPiz8mlJXIvI4Yy8hRDuIMk4A675ycld/tNjJlbsutYxiq3 yJYQ== 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:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=W+nHiWH4VUADQqfSjUJKJXkvxHH/TK30Vf6f/In9w7I=; b=lbWuM0CY+NX3ZKPrmRxI1mehNePyADKmHxC9Khb6M7hqdmZVZ+YxndRj0PGac6KNOy LBK+eesCwRb4Lh61MiJDN93GbA8gM2cruc+Y7rjPs6HXbJbKOgXBKi+j//PMTHuAt7Oq TksrAcB8+8O4RUE+4aa9Wcx0+rZEYP9mxstBJXV63vKtRuMCTrqZZNaK2bm7d3t7IMNm eR7ZVdf3NLikw4YkxllNHPP1eqUmF4/3o4vs9X75e0DIXIGO01aCzpUH8Shf0xvzVSOR M1ko9VrGJUKpCCCZmmgIZegAnJdeHVWYkhXLHEI+9nu8dN6uNb3q8I7wCGUy82ItHAQe /uvQ== 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 u8-v6si6419778plz.481.2018.09.14.03.06.50; Fri, 14 Sep 2018 03:07:06 -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 S1727839AbeINPUb convert rfc822-to-8bit (ORCPT + 99 others); Fri, 14 Sep 2018 11:20:31 -0400 Received: from mail-ot1-f66.google.com ([209.85.210.66]:39381 "EHLO mail-ot1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726868AbeINPUb (ORCPT ); Fri, 14 Sep 2018 11:20:31 -0400 Received: by mail-ot1-f66.google.com with SMTP id c12-v6so4037395otl.6; Fri, 14 Sep 2018 03:06:44 -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:content-transfer-encoding; bh=LhJ7VGsjU15WgOrkRMuAvoAlQWwnmBzU5YvauPjraxQ=; b=pDpsVw+VVb9f7lsL5DIFcnZBTAdPAOv6Miw59r3XCwxcj4noug5+H5VG/2+ybJzsoS l0kg1ye7bKj5D6KvPIGJWp3W9BFg9bXwfgaV7IRBIjtHmN77pKfgp5PyDF05wQ48wnJo q9ZOU6QrHxX47YRFKSJ8FnG9CW24swEcGTJkEgX2mo5OWW4omJd3svWKERTYPwJSb+bn mc5uaZXXt/2HgvBujpbgtHu7TCUU+4FSQ7c0ZwA+E+Xp2uxg1bzLVessaO4PAIwOnqx7 HgIl8aVTWHEICImGuR+CkrR/21jY68CJG8vsHzTMH9wqueKqRHAUO2neE7ITkWr4RJTo enEQ== X-Gm-Message-State: APzg51CT2Fbv/bLWFHtGYllCFyLSwd7hcMWnEN6PbWZ7NMeGSdscKdET fGeFCrHrY1DhOuCqx3N5n/mcJexmY5SJAGeK8vM= X-Received: by 2002:a9d:2645:: with SMTP id a63-v6mr3754304otb.270.1536919603377; Fri, 14 Sep 2018 03:06:43 -0700 (PDT) MIME-Version: 1.0 References: <9611469.2z7W9akjOQ@aspire.rjw.lan> <767e9ec4-cc35-9255-360a-4d12736aa4de@nextfour.com> <1831106.CYv9dyYV15@aspire.rjw.lan> In-Reply-To: From: "Rafael J. Wysocki" Date: Fri, 14 Sep 2018 12:06:31 +0200 Message-ID: Subject: Re: [PATCH] PM / suspend: Count suspend-to-idle loop as sleep time To: mika.penttila@nextfour.com Cc: "Rafael J. Wysocki" , Linux PM , ACPI Devel Maling List , Linux Kernel Mailing List , Mika Westerberg , Peter Zijlstra , Thomas Gleixner , Srinivas Pandruvada , Vitaly Kuznetsov 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 On Fri, Sep 14, 2018 at 11:53 AM Mika Penttilä wrote: > > On 09/14/2018 11:46 AM, Rafael J. Wysocki wrote: > > On Friday, September 14, 2018 10:28:44 AM CEST Mika Penttilä wrote: > >> Hi! > >> > >> > >> On 09/14/2018 09:59 AM, 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 > >>> --- > >>> > >>> This is a replacement for https://patchwork.kernel.org/patch/10599209/ > >>> > >>> I decided to count the entire loop in s2idle_loop() as "sleep time" as the > >>> patch is then simpler and it also covers systems where timekeeping is not > >>> suspended in the final step of suspend-to-idle. > >>> > >>> I dropped the "Fixes:" tag, because the monotonic clock delta problem > >>> has been present on the latter since the very introduction of "freeze" > >>> (as suspend-to-idle was referred to previously) and so this doesn't fix > >>> any particular later commits. > >>> > >>> --- > >>> 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); > >>> + } > >> > >> But doesn't injecting sleep time here make monotonic clock too large by the amount of sleeptime? > >> tick_freeze() / tick_unfreeze() already injects the sleeptime (otherwise delta would be 0). > > > > No, it doesn't. > > > > The delta here is the extra time taken by the loop which hasn't been counted > > as sleep time yet. > > I said incorrectly monotonic clock, but timekeeping_inject_sleeptime64() forwards the wall time, by the amount of delta. > Why wouldn't some other cpu update xtime when one cpu is in the loop? And if all cpus enter s2idle, tick_unfreeze() > injects sleeptime. My point is that this extra injection makes wall time wrong, no? OK, you're right. I got that the other way around. So, the patch is withdrawn.