From patchwork Mon Feb 4 16:25:52 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 10796071 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6DC4113A4 for ; Mon, 4 Feb 2019 16:26:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5A20F2BBFD for ; Mon, 4 Feb 2019 16:26:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4E05D2BC00; Mon, 4 Feb 2019 16:26:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=unavailable version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 020552BBFD for ; Mon, 4 Feb 2019 16:26:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=HLFIYsbkIy6EJPBxV8bMe4ftMtVrhONykGIovj095EQ=; b=p3TrV1AG9K7OkqnAmrnHNoVMIc 7QzLGb0DWJ7+B1z0gs66+f1Kd6QxlNuSYoYPGoWK1Nh8qv0vD74OIhQbcNJxX3bhOrTdYYl8tawxA vxaK/GF/W/M4k/XpR6w2lx8rzgi19m8rqfyPm0ZQRdsvK8ajLStHymUymautgBPlmjoWlzQmBXxQ3 4scyhThv15D385jjOe0ZuZvKVC2SLKfztzRcrT+c5azUhdQC6XpkiCVLTQhzL/Sk5ZMwsjY/cTF0Y hvRNTGgkwCZduezLUXncXEP5/ROQ+CIDfSvqtCrM2RFSLl5nQsr/zKi7646N7u0eexq4poXqJT3Ru HGtE8DOA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gqh4V-0000Yx-Sa; Mon, 04 Feb 2019 16:26:11 +0000 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gqh4K-0000My-6b for linux-arm-kernel@lists.infradead.org; Mon, 04 Feb 2019 16:26:01 +0000 Received: by mail-wr1-x442.google.com with SMTP id s12so494922wrt.4 for ; Mon, 04 Feb 2019 08:26:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=uh0mWn8UrzjjJpP8IvQQtBqJhS8PhQJ/5HnUmOlZIks=; b=gmZuOKV+QFRNuw7Xiq01qUxU2MZBcpwkazl6G5C+h5WY9pAGCiz91/y7DoyN/0jhFY DZ8i3Vafab444Z0m63eLyReMXiadyi2xz0C+aOngk/lRbyG81BWpa+kBRlKmiLkPSDct YfqFD1w+6asWT2k4jairtsCwvVi474zCNUtO0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=uh0mWn8UrzjjJpP8IvQQtBqJhS8PhQJ/5HnUmOlZIks=; b=U515ppHf5M+H88BXRezSb/Oj0j92vDk9rfQn0BjEqCQYSWSC+cRjL3yGOTMVIRZDIo tQZNm03W0+hLbXwEuWMdkEAK7eiFSLdG/LCFJs2OTI5jEDm975l+f+WnyL0Y1wfF5DrW wkxTBXep5XU19TCgx4ktfNRaX64Mb7s/dQ94YufXeb1KfYmngAbiiSGgVVLQHoeQl9Jh wneWunPSEIqIjVY85dY6ub8jEyxojWejhcCRex4m86pT6N7CDJL0CUC3nXZ2+537gcv9 75hVOVBqmo6QB6IjFDqP5vXVbR7wWv5h/D1nFwMZTZhUT+TQbHHasuk/9n28O0nvoCx6 Vy9g== X-Gm-Message-State: AHQUAualDJCkYGuN1QsqxXi7mrmjgx2FfnnaR6tmqFPL67kb/+AKZgTT 6tWL8Q27XYmdU+G2oBe3YRAzWw== X-Google-Smtp-Source: AHgI3IbIksHA+DShAzSt1p3DgS3JyqNTcMkRaDm5sNHqKr9m4slS51ApzahFGlivX+nk/RtPLDUzrg== X-Received: by 2002:a5d:6aca:: with SMTP id u10mr148455wrw.310.1549297558732; Mon, 04 Feb 2019 08:25:58 -0800 (PST) Received: from localhost.localdomain ([2a01:e0a:f:6020:70b6:b9c6:89e2:36a]) by smtp.gmail.com with ESMTPSA id a4sm1251992wrt.37.2019.02.04.08.25.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 04 Feb 2019 08:25:57 -0800 (PST) From: Vincent Guittot To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, rjw@rjwysocki.net, ulf.hansson@linaro.org, biju.das@bp.renesas.com Subject: [PATCH 1/2] PM-runtime: move runtime accounting on ktime_get_mono_fast_ns() Date: Mon, 4 Feb 2019 17:25:52 +0100 Message-Id: <1549297553-17647-2-git-send-email-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1549297553-17647-1-git-send-email-vincent.guittot@linaro.org> References: <1549297553-17647-1-git-send-email-vincent.guittot@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190204_082600_236772_4DD0F786 X-CRM114-Status: GOOD ( 14.04 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-renesas-soc@vger.kernel.org, Vincent Guittot , geert@linux-m68k.org, thara.gopinath@linaro.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Similarly to what happened whith autosuspend, a deadlock has been raised with runtime accounting in the sequence: change_clocksource ... write_seqcount_begin ... timekeeping_update ... sh_cmt_clocksource_enable ... rpm_resume update_pm_runtime_accounting ktime_get do read_seqcount_begin while read_seqcount_retry .... write_seqcount_end Move runtime accounting on ktime_get_mono_fast_ns() With ktime_get_mono_fast_ns, the timestamp is not guaranteed to be monotonic across an update of timekeeping and as a result time can goes backward. Add a test to skip accounting for such situation which should stay exceptional. Fixes: a08c2a5a3194 ("PM-runtime: Replace jiffies-based accounting with ktime-based accounting") Reported-by: Biju Das Signed-off-by: Vincent Guittot Reviewed-by: Ulf Hansson --- drivers/base/power/runtime.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index 1caa394..50740da 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -66,13 +66,22 @@ static int rpm_suspend(struct device *dev, int rpmflags); */ void update_pm_runtime_accounting(struct device *dev) { - u64 now = ktime_to_ns(ktime_get()); + u64 now = ktime_get_mono_fast_ns(); + u64 last = dev->power.accounting_timestamp; u64 delta; - delta = now - dev->power.accounting_timestamp; - dev->power.accounting_timestamp = now; + /* + * Because ktime_get_mono_fast_ns() is not monotonic during + * timekeeping update, we must ensure that now is after the last saved + * timesptamp + */ + if (now < last) + return; + + delta = now - last; + if (dev->power.disable_depth > 0) return; @@ -1312,7 +1321,7 @@ void pm_runtime_enable(struct device *dev) /* About to enable runtime pm, set accounting_timestamp to now */ if (!dev->power.disable_depth) - dev->power.accounting_timestamp = ktime_to_ns(ktime_get()); + dev->power.accounting_timestamp = ktime_get_mono_fast_ns(); } else { dev_warn(dev, "Unbalanced %s!\n", __func__); } From patchwork Mon Feb 4 16:25:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 10796075 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B419513A4 for ; Mon, 4 Feb 2019 16:26:30 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9FBF62BBFF for ; Mon, 4 Feb 2019 16:26:30 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 934CA2BC02; Mon, 4 Feb 2019 16:26:30 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.2 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 39E132BBFF for ; Mon, 4 Feb 2019 16:26:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:References: In-Reply-To:Message-Id:Date:Subject:To:From:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Owner; bh=seeTPMeHCDhguSgH+2KfD5fcACp77+WxVsXXNQZNpIE=; b=ljU77Gi2VfpvIJhWSsZPJXQUlD WzvXb35t0c0+iDYJo1EKy+WQqRBFWMs6km3ljDkzjx07gVkZWLrd/fjjaM55YwqBX5HPPLOkm4AwL FfiMAg7o06GdbIC+7Xt9GUDeoczblIlytqrAc5gXSpkCcyFVOO5qRvk8RLC0ytQ0jBtFxTXcq+f9l 4PIAMdkMCrL9ibMEU1f1m91cS6cmWev4/AU03lrnBjfSCPBSKeVdDHMvethX3NS2ZBLhEuWWPdi1G 2l/OnyF5stnPwLbKMm6dkKxHTA6F1tzXxLGWO4RaDeDXhWvNKA0Oq2dCE1au++h2zWYFaQHjbsJ9J lzB2vvog==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1gqh4k-0000qC-MC; Mon, 04 Feb 2019 16:26:26 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1gqh4L-0000NM-P5 for linux-arm-kernel@lists.infradead.org; Mon, 04 Feb 2019 16:26:05 +0000 Received: by mail-wm1-x344.google.com with SMTP id f188so520162wmf.5 for ; Mon, 04 Feb 2019 08:26:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=9lKepzvlFqSdyTmpHqFOS3txG3e1Qlnve7EeoiMfLO0=; b=aVSLiJosF7nsn2YlMbkWjfe/sQt+HQvDzUEKw4RhgQWgOVm70k9HPSiS5kbBVP/nE1 uxICapIcLqWzWM7keOzeU4tTM3C0LF+hWtIqKxl8GXPDB84dVtX59Ewhs+KjNoSXstgm a7axZ2Vk2Aky3bhhwgyM5lpXXxhrlCVDzQPX4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=9lKepzvlFqSdyTmpHqFOS3txG3e1Qlnve7EeoiMfLO0=; b=Xy30z5kqGLIQhIWPsIostegbKlR1soxcjBgJfi27d0FzGafyyPB2kAMg5tFKfU2Cm1 GFEYYiSV4zTZ4pnypioqk43kF/Gnje5U2BcaOIxrHRDlDf186gtvmvYa+irdKyvp3nL9 wbv1zo/N7RDYeYizv0qtNdVHcy5xw+pA6aP9aWqHBuwzTj3Wd09e9qMtuLJFVxQzxAL4 dA3PGPzwIjEdZfzwmBXHEyYSk7Y5ex08Fwg7mU2FhBFzcs2CK3rZSDUzU8YnVvsY7f0F w5tSChHkgqrHsw3NpiPrAxEqw1ckG/xQPt5wCVksC2RQFoIzIa6ItwjS0tWBAdP3ccZZ GEhg== X-Gm-Message-State: AHQUAuZrb7/BYF/FtKt8TlMfg5LBNvtSCZhcadYY+QgKURCqqu+EU9m8 SiNbFX8CM2crRTJLLhV2SUmYzg== X-Google-Smtp-Source: AHgI3IZ7M0ze84wTX8DLxUv882QQUfhGHmbTLyFg1PCjFq7A45aNiEqTtsmhl2YBtsRd3HsDJdr2vw== X-Received: by 2002:a1c:b456:: with SMTP id d83mr127459wmf.115.1549297559980; Mon, 04 Feb 2019 08:25:59 -0800 (PST) Received: from localhost.localdomain ([2a01:e0a:f:6020:70b6:b9c6:89e2:36a]) by smtp.gmail.com with ESMTPSA id a4sm1251992wrt.37.2019.02.04.08.25.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 04 Feb 2019 08:25:59 -0800 (PST) From: Vincent Guittot To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, rjw@rjwysocki.net, ulf.hansson@linaro.org, biju.das@bp.renesas.com Subject: [PATCH 2/2] PM-runtime: update time accounting only when enabled Date: Mon, 4 Feb 2019 17:25:53 +0100 Message-Id: <1549297553-17647-3-git-send-email-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1549297553-17647-1-git-send-email-vincent.guittot@linaro.org> References: <1549297553-17647-1-git-send-email-vincent.guittot@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190204_082602_123827_2E241630 X-CRM114-Status: GOOD ( 14.78 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-renesas-soc@vger.kernel.org, Vincent Guittot , geert@linux-m68k.org, thara.gopinath@linaro.org, linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Update accounting_timestamp field only when PM runtime is enable and don't forget to account the last state before disabling it. Suggested-by: Ulf Hansson Signed-off-by: Vincent Guittot Reviewed-by: Ulf Hansson --- drivers/base/power/runtime.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index 50740da..ed62cca 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -66,11 +66,14 @@ static int rpm_suspend(struct device *dev, int rpmflags); */ void update_pm_runtime_accounting(struct device *dev) { - u64 now = ktime_get_mono_fast_ns(); - u64 last = dev->power.accounting_timestamp; - u64 delta; + u64 now, last, delta; - dev->power.accounting_timestamp = now; + if (dev->power.disable_depth > 0) + return; + + last = dev->power.accounting_timestamp; + + dev->power.accounting_timestamp = now = ktime_get_mono_fast_ns(); /* * Because ktime_get_mono_fast_ns() is not monotonic during @@ -82,9 +85,6 @@ void update_pm_runtime_accounting(struct device *dev) delta = now - last; - if (dev->power.disable_depth > 0) - return; - if (dev->power.runtime_status == RPM_SUSPENDED) dev->power.suspended_time += delta; else @@ -1298,6 +1298,9 @@ void __pm_runtime_disable(struct device *dev, bool check_resume) pm_runtime_put_noidle(dev); } + /* Update time accounting before disabling pm runtime */ + update_pm_runtime_accounting(dev); + if (!dev->power.disable_depth++) __pm_runtime_barrier(dev); @@ -1521,7 +1524,6 @@ void pm_runtime_init(struct device *dev) dev->power.request_pending = false; dev->power.request = RPM_REQ_NONE; dev->power.deferred_resume = false; - dev->power.accounting_timestamp = 0; INIT_WORK(&dev->power.work, pm_runtime_work); dev->power.timer_expires = 0;