Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6141251imu; Wed, 30 Jan 2019 09:27:19 -0800 (PST) X-Google-Smtp-Source: ALg8bN6f6LyvZJHWDOuWzKgUh2HV67LGU/LH9U2/a7NWO82eYm2pmurHK+j2retWCMZNOsxcu1x9 X-Received: by 2002:a17:902:209:: with SMTP id 9mr31778321plc.288.1548869239881; Wed, 30 Jan 2019 09:27:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548869239; cv=none; d=google.com; s=arc-20160816; b=wz7fIwt7OyhdGq6jrT7YigbE517qg6QSw4AOqc02WOl39zegfUmIqU0dKhOzazp638 It3QYxxtfCZiF9IBqap3+yInmS/1ZRDki6eSkewLElWqlTaijc1m4qVxzZzCuFm6FD60 FCRwVHfuQCn8xQrW1KFoXZKdrQRpCRBKfWTrhiNxjozGS1g4AInP8EIzlI0P1Zh6s0T5 m6mRL/0CPUIYJnLnc/Z8cJyIHwsOF85BpvQY9K2MVYAlXIbkcITjBQF9/PkJlw2axvDe Q2m+owEzb+18EYbtlHdJf5Unwl16UpwLZ40Vu3GbB14v+iuczQiRba5Ed7bVCwA5Is+3 tWGg== 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 g10si2138028plm.1.2019.01.30.09.27.01; Wed, 30 Jan 2019 09:27:19 -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 S1732463AbfA3R0b (ORCPT + 99 others); Wed, 30 Jan 2019 12:26:31 -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(-) 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) -- 2.7.4