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: 157438 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp4104201jaa; Mon, 4 Feb 2019 08:26:08 -0800 (PST) X-Google-Smtp-Source: AHgI3Ibbs55nGZJQ4Kuo/0cvEcQNqkIrxN8wV+JtDj0+iqyv4TEUtx/AiUUDbIEsjtkxPJKSxI1H X-Received: by 2002:a62:4641:: with SMTP id t62mr171461pfa.141.1549297568572; Mon, 04 Feb 2019 08:26:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549297568; cv=none; d=google.com; s=arc-20160816; b=PSy4922bs+1qHZ2U7sI+RS4Du5tFS9zv0il8D0QnJlBuYNWQciA4uoaxE5yPgbYVrb DbtyWFNAMM3AJLaXXiT82bokPUeT+JSTW7Dhhh9S1N7ldACjBWxHZhLdOKUbpT4AZnnO 94K4yRjbKcmmDKrlqHtNG5T9Wqgr/Zse9lRvgAgI3EpPAsyjXdFmbczG9rNpj8N3uFcE uwXujnlNa0tydFcPXqDSSl6IqCNF5JbEmDPWdGquXJHb/+P+dq6rdMzaSXHslVcyYTpZ Q0PIDEBP9EZjQzPPknwhztDBQP8hFMuorCVagLS9VPutSsLtAa5lCLdkq+xNzeanrfVr 8RHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=uh0mWn8UrzjjJpP8IvQQtBqJhS8PhQJ/5HnUmOlZIks=; b=CMG9ljG9Oyp6KP0hptNKZMN+oeTUgVUfF1fLhxhMDlCrMf3gUeLPTq/dsp8IG1eN5y xIqneocNjzcPNahHysX4CAaBqoV53+HjNhU8gmgAkFwFpq4jxDmSXll8vEuAhD4ZiUkp 8mGSegXCYsdYOBjux5s3h4WZiIVYZuvJcAvy3C7prLHzLRl1FJ1JvDMNhAFyDEbT2lX1 vp51t/fNUBYkI1zUdEQ3WF+jgudeKHUz4HEGZ8/KYgGeOHmXTf6+osZXOFtB+bmo9ntl 6W6s0RfNzbUyuuFIGraBqq6k2w7s1dVsn8AsYPWOmH0AlnfmxP4+9DccB1eX8Hkcruih /BYQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=gmZuOKV+; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a11si401359pga.198.2019.02.04.08.26.05; Mon, 04 Feb 2019 08:26:08 -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=@linaro.org header.s=google header.b=gmZuOKV+; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731224AbfBDQ0D (ORCPT + 31 others); Mon, 4 Feb 2019 11:26:03 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:45716 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729761AbfBDQ0A (ORCPT ); Mon, 4 Feb 2019 11:26:00 -0500 Received: by mail-wr1-f65.google.com with SMTP id q15so433554wro.12 for ; Mon, 04 Feb 2019 08:25:59 -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=gzoVrM1yc9LpplA4RMvambaHynufPg15jBfAWNsft8JU7CCoN4HDXzNzMZvFu+D5zH 9Gl2coNxmaBjMxyNFyXqPJkszcZpNPslWs275Zc0KDxZCUWfgImuObW4vMvuL/R9jG3X nS6nbs7LdWaHrc7dJThwic3HAElNEhZOoW7jDWyWIKex6JgrpvmKfumiLsOiOIsPY6S1 FsxllZdT0Bs72kdZOVYuErBeINa7rHHzTf9uMUDXPtf5Un+WZx6IzFCg6Y+TkFquszqQ eubPnSqXBokWugqcPEcEJEMppLHBBnSG0RlJXUWaOa+jWfEPz/taDBWeka7sjusf7uzS vimQ== X-Gm-Message-State: AHQUAubYTiyMRJ/BftIvnJdW67CLvWIS8L2XyFR3uB2FClzx9+IJoLMp GwlL2Js+glM3l7uLKCZTYOMIJw== 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 Cc: linux-arm-kernel@lists.infradead.org, geert@linux-m68k.org, thara.gopinath@linaro.org, linux-renesas-soc@vger.kernel.org, Vincent Guittot 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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- drivers/base/power/runtime.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) -- 2.7.4 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: 157439 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp4104238jaa; Mon, 4 Feb 2019 08:26:11 -0800 (PST) X-Google-Smtp-Source: AHgI3Ia6MO7u9lobytamdUgfOsVjylsUNA+PRoYnngljC592Rtq/W6ZbD/k8PJzNcv0Nn8IIwHGt X-Received: by 2002:a63:6506:: with SMTP id z6mr192895pgb.334.1549297571089; Mon, 04 Feb 2019 08:26:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549297571; cv=none; d=google.com; s=arc-20160816; b=QhFDQ0Q5K4iDNDj635YmIBvluu5eaikNAXnyWqRkt9jIMC0tJo/AJTQT/fYg49EL9e vjTsFk4ZHapb9LX83mNtmKQkLbu7zHLnvYHFfZQ6Ft6m2pEeGvEOJugN+u3AqPi7v2my EEOkfANW5sEImZ13XIFUfmzARP5UX1u9u+616gQsc2kNJgTBgQeTXQLuaP3g43wcKKTt J7hI8QhECu6wks19JLseYckyMT9H8CCI6SliTfdG/lBms0WfnLy4PityhYMiCMNamykY tkUWMp8ag965nYepxfA9yZLW7l3PsoMi2F5I7UivY8JSNOw/N8xY47/ar0K7vEdYhK96 acxw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=9lKepzvlFqSdyTmpHqFOS3txG3e1Qlnve7EeoiMfLO0=; b=JBl75HwHAn3/H+Rw/o8UMgsuqjxjo/WBPed6Fg3AQF8Pmod0WvfVuLjO1ZMWDoulLM K/2lhx8OYUBAQW3vSGN5mmgMoL9qScZjYTeDhEXlpta/wuIzFMIbLT5ZB9kvqHhF5QXd Zjclaq/7HhP0vkNP07pIAnKUDy5+KrVT2nh4AstkEKuXNGV/sPK+DF2FgNbtc90wUjqj hWb44Ko4cDODWqzpndOVeCRvYsStjVmIXpIjxQWP+dWAt4Ycly2N2hZlkbVTkHipfE45 lADfdJOUv4sg44GiJIMq7A3XW65QsYyNCsp8Nn/q83hbQu4KES9OOTjcQdAGQgJtyUGQ Dd6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aVSLiJos; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 10si416090pfy.206.2019.02.04.08.26.09; Mon, 04 Feb 2019 08:26:11 -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=@linaro.org header.s=google header.b=aVSLiJos; 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=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731510AbfBDQ0H (ORCPT + 31 others); Mon, 4 Feb 2019 11:26:07 -0500 Received: from mail-wm1-f66.google.com ([209.85.128.66]:35114 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730735AbfBDQ0C (ORCPT ); Mon, 4 Feb 2019 11:26:02 -0500 Received: by mail-wm1-f66.google.com with SMTP id t200so566774wmt.0 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=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=m2QdDiCEhN8KXf6xqnfnU/07DhtQK0QPr+7Fg/ZUluVenBqgnXbVep8Kh4MRX+gHw7 Lkf6EHkJo91mDdAwUx2APUcshLac8bx48IcQ34atYAb5w51PMUjBBuFiuguBm9wuqcey GL5+YeP8BeUvJfKF2I8+ZvA+KD7wtL4qR6fXpIjhar19FPmzR5XgiBw7MszN39b9dHk/ vtdK2W8eBlqWtvCaOhu/nVIodzrM/KBVsUwYG77kiape/ccK1y6onRze6du+Ji01azPh KUorsqKVLgvVScUsKQCgafh+dQGIy5m+kWraUGHCrD+cvvPYMCnWt312m0IaarJnRZ50 2WeA== X-Gm-Message-State: AHQUAuYerl3o3XHX+sqx6pIbdI9nkTuUrmfYSZK9tU9d49IgvJiH0joZ ScoEuuRnV6BH9Dm+krgkF3xFug== 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 Cc: linux-arm-kernel@lists.infradead.org, geert@linux-m68k.org, thara.gopinath@linaro.org, linux-renesas-soc@vger.kernel.org, Vincent Guittot 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> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- drivers/base/power/runtime.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) -- 2.7.4 Reviewed-by: Ulf Hansson 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;