From patchwork Wed Jan 30 17:26:02 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Guittot X-Patchwork-Id: 157076 Delivered-To: patch@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp6201965jaa; Wed, 30 Jan 2019 09:26:33 -0800 (PST) X-Google-Smtp-Source: ALg8bN50R29UNxOUGRcN1iPVQI7GFxqMEvMmYXCSKRFhOpX7ksuODx7FFo32CRnGcvRIMplmMs9O X-Received: by 2002:a62:3811:: with SMTP id f17mr31967660pfa.206.1548869193796; Wed, 30 Jan 2019 09:26:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548869193; cv=none; d=google.com; s=arc-20160816; b=uG/2be+xkVHE0A2+t4T3cjlO/blMXj4rhU8BG7ln+m3O5N0fqK5gCXTiE15560EdHS oBN3McHBQyHGoJd0zZ9EW3LbDs6GLQ9v373o1ZfVd+ND90Nc6vKsGVcSQTxF6z6hrqJW YP68W1fSW8SxizNnmZi6GZBZTnerrZEycM2mP5Lqa3ouddyTS2MZjzXE+8QrIVExySF+ TnZc0NSMA71FYC5P8Yj4GUzrT0oFnFuXDjpuaYgSHLAXw8R3mMwSfu5CJKk0O7NB5W/k 22dp+dIv5MHQt2kUz2agg7JyfCC4TxzJVc0wCUyx/EGANa3mAMHE/fi05uwFu1cUcl2p XM8w== 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=XrfQDMdrfjMyVkUbTxAT96m6kQZT5x+Q2hxt4/3RgHY=; b=H1Jv3fLdBSR+uUDQeoDTv1zi+XwLqD9eY1mv5Fb9iclZ/RyJjlDNff1ztkYM0U3IXE CWCFqrn7V9iRJYurjXVqg3WA8M2n5kZDzWAvKInGCPuaZBl+zl9UsamcZDvMusXXVIiH 3TUhrnyVT0sr+myOM2g3V4hC4Fkf7Hx0CBMiQwuQ0kT3aoW+62tYtw5getOo8TP7BqbZ Ac7W9ZOslkGSGmGplXkDWXcluFKPnXaIhW/Tg1/EAG0CJWcX4KJq1Hl4/I5sDmL2/Izv j7Rc9W/sm5aWYSyWRBBSNcATjm8PHu+chMR7Hdbm9GP+9mBSbdbW53uczq8jzEqVFkjb wNKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=PX4Vmspc; 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 a3si2090345pld.252.2019.01.30.09.26.33; Wed, 30 Jan 2019 09:26:33 -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=PX4Vmspc; 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 S1732484AbfA3R0c (ORCPT + 31 others); Wed, 30 Jan 2019 12:26:32 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:46374 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731582AbfA3R0b (ORCPT ); Wed, 30 Jan 2019 12:26:31 -0500 Received: by mail-wr1-f65.google.com with SMTP id l9so297494wrt.13 for ; Wed, 30 Jan 2019 09:26:30 -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=XrfQDMdrfjMyVkUbTxAT96m6kQZT5x+Q2hxt4/3RgHY=; b=PX4VmspcuGJlI5kSXkkrHEjx7uuMvuSu4Tdiq2IndHoZypWy3KWdndOmsskUfneroW 3Qyws2Sb+LFkk61+IirdrbA+LYmcw9j2s0CzVxg0R4ZhG80+kZPM6kS2ITvdCWQTcbLo 21xwige6RxuDBe2AkP6g5UG0Jxn/xQUpB0NDI= 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=XrfQDMdrfjMyVkUbTxAT96m6kQZT5x+Q2hxt4/3RgHY=; b=gWrCxMu6lKaPliWT1qRvlb7mF0CbJo4/fgvHp4O9zhd/M7mf9iUX9oVTLDj4XgdXAd /ijWrDVzjA+Iz2dzrSwlf2AgBbbw8wasishdI91MHzZNI1nwnsr9MV9IxQhmqBSPmQ7H eON+DY7rHOUYnh4Qi8gNkDutNJWOEbtmPPJP+aJIU4u1FLY1cWL4e5Xk0mEarksWzhVe lsDKBcGFrkEI31RN29VT5tNqYj9Hj+/lCLTfQK/AFMFZf6Z4wffoFOx7LR+017HNrcu7 /kAQIvmde8Da0ImIY/9F73NghjhKBI7uSn2Gw8L3GO7fVedOrrw8lJ31T3oqRTrlbjjS S3gA== X-Gm-Message-State: AJcUukezzBjYQJqGXYT3pMjjLsdFtbgGkQ7vPSn+xCOGB5fpjnntqvEZ uHi+PtS7JMarEgr6KxbS1TFnqA== X-Received: by 2002:a5d:6a42:: with SMTP id t2mr33405207wrw.50.1548869189563; Wed, 30 Jan 2019 09:26:29 -0800 (PST) Received: from localhost.localdomain ([2a01:e0a:f:6020:79e5:6689:e4d2:d212]) by smtp.gmail.com with ESMTPSA id n124sm3618880wmd.15.2019.01.30.09.26.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 30 Jan 2019 09:26:28 -0800 (PST) From: Vincent Guittot To: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-omap@vger.kernel.org, rjw@rjwysocki.net, ulf.hansson@linaro.org, biju.das@bp.renesas.com Cc: geert@linux-m68k.org, linux-renesas-soc@vger.kernel.org, Vincent Guittot Subject: [PATCH v3] PM-runtime: fix deadlock with ktime Date: Wed, 30 Jan 2019 18:26:02 +0100 Message-Id: <1548869162-6223-1-git-send-email-vincent.guittot@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org A deadlock has been seen when swicthing clocksources which use PM runtime. The call path is: change_clocksource ... write_seqcount_begin ... timekeeping_update ... sh_cmt_clocksource_enable ... rpm_resume pm_runtime_mark_last_busy ktime_get do read_seqcount_begin while read_seqcount_retry .... write_seqcount_end Although we should be safe because we haven't yet changed the clocksource at that time, we can't because of seqcount protection. Use ktime_get_mono_fast_ns() instead which is lock safe for such case With ktime_get_mono_fast_ns, the timestamp is not guaranteed to be monotonic across an update and as a result can goes backward. According to update_fast_timekeeper() description: "In the worst case, this can result is a slightly wrong timestamp (a few nanoseconds)". For PM runtime autosuspend, this means only that the suspend decision can be slightly sub optimal. Fixes: 8234f6734c5d ("PM-runtime: Switch autosuspend over to using hrtimers") Reported-by: Biju Das Signed-off-by: Vincent Guittot --- Hi Rafael, Sorry, I sent the version with the typo mistake that generated the compilation error reported by kbuild-test-robot This version doesn't have the typo. Regards, Vincent - v3: fix typo error that appears while updating commit message - v2: Updated commit message to explain the impact of using ktime_get_mono_fast_ns() drivers/base/power/runtime.c | 10 +++++----- include/linux/pm_runtime.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) -- 2.7.4 diff --git a/drivers/base/power/runtime.c b/drivers/base/power/runtime.c index 457be03..0ea2139 100644 --- a/drivers/base/power/runtime.c +++ b/drivers/base/power/runtime.c @@ -130,7 +130,7 @@ u64 pm_runtime_autosuspend_expiration(struct device *dev) { int autosuspend_delay; u64 last_busy, expires = 0; - u64 now = ktime_to_ns(ktime_get()); + u64 now = ktime_get_mono_fast_ns(); if (!dev->power.use_autosuspend) goto out; @@ -909,7 +909,7 @@ static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer) * If 'expires' is after the current time, we've been called * too early. */ - if (expires > 0 && expires < ktime_to_ns(ktime_get())) { + if (expires > 0 && expires < ktime_get_mono_fast_ns()) { dev->power.timer_expires = 0; rpm_suspend(dev, dev->power.timer_autosuspends ? (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC); @@ -928,7 +928,7 @@ static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer) int pm_schedule_suspend(struct device *dev, unsigned int delay) { unsigned long flags; - ktime_t expires; + u64 expires; int retval; spin_lock_irqsave(&dev->power.lock, flags); @@ -945,8 +945,8 @@ int pm_schedule_suspend(struct device *dev, unsigned int delay) /* Other scheduled or pending requests need to be canceled. */ pm_runtime_cancel_pending(dev); - expires = ktime_add(ktime_get(), ms_to_ktime(delay)); - dev->power.timer_expires = ktime_to_ns(expires); + expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC; + dev->power.timer_expires = expires; dev->power.timer_autosuspends = 0; hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS); diff --git a/include/linux/pm_runtime.h b/include/linux/pm_runtime.h index 54af4ee..fed5be7 100644 --- a/include/linux/pm_runtime.h +++ b/include/linux/pm_runtime.h @@ -105,7 +105,7 @@ static inline bool pm_runtime_callbacks_present(struct device *dev) static inline void pm_runtime_mark_last_busy(struct device *dev) { - WRITE_ONCE(dev->power.last_busy, ktime_to_ns(ktime_get())); + WRITE_ONCE(dev->power.last_busy, ktime_get_mono_fast_ns()); } static inline bool pm_runtime_is_irq_safe(struct device *dev)