Received: by 2002:ac0:946b:0:0:0:0:0 with SMTP id j40csp3055247imj; Mon, 18 Feb 2019 18:41:21 -0800 (PST) X-Google-Smtp-Source: AHgI3IaA+KraQESEQXwEq3cePzMykqSPP/TmyLCUdoMEyUpGdFgmTU2E1g2unIGlu5FApIO6mo9W X-Received: by 2002:a17:902:5a8d:: with SMTP id r13mr28525191pli.190.1550544081398; Mon, 18 Feb 2019 18:41:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550544081; cv=none; d=google.com; s=arc-20160816; b=ngnsjR6M2rkMngQ1YEVs9fJJOfZWNrzhW1TJSUjA8fCN9zObG4OxJZZYEpkMWs7gGt qECtPjUcEfGwHqUsNVkHEHs0951YRJsNIFLjroazOzcJXZ4htt+juO2WiypK7yyYWn57 wu2oH5WVfR7IbYWDXs8N2nlqwAGRZrEz42LhnHGSPEo+K2S4h4xTaAgEd9kO3XwhJ7R3 xwBnUX3FNnkqj7OBVvcOBtsTejizdfpRM6LM8TEethIgjHf2ZA4bTVhmYsyg/1jL/bwh Sqpsa3yZfPZMSZkH6LLktxjTbMtLe5sGiO5GFZFQYjOOCYZGcDOImACmeYLdoruMqh+C 8Gcw== 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:mime-version :message-id:date:subject:cc:to:from; bh=XlDHGpcwCbVYWg/FOZN/eRVNP1Z88CvVCRzbjzj2S4A=; b=A1jKi2N5CKUf438EL85/dQONa/Kq+OMRIeZZ/v0oILrNRibvF+dvOKepwNe6mrz1Pf 85j4sz+5F/qZEyWj+gk61aKFR5BA7Sj99GapFo5cORSQutbXIvMiFDJP+HazqGVTpH2T HzPp3u/WYiYIW60yYIXorqsCNKm7K7VwqRkLp+Re4vLIX7NygJYpPLA9Q7FEWyi9QV8d zYd3ShD1hJC1OhxkJaQrPGauiCPHqAYJKilfJFDetG4OStGYUOOke1DQrhuhkgzrsNIj eYyW58GTN8zh5T8Z+uN2DgPI+CsYEisxz5+PDdKjftBAXOdN8ZXDySgwXnMYaHeozvt7 4oag== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o28si14815758pgm.238.2019.02.18.18.40.57; Mon, 18 Feb 2019 18:41:21 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731490AbfBRWJ3 (ORCPT + 99 others); Mon, 18 Feb 2019 17:09:29 -0500 Received: from cloudserver094114.home.pl ([79.96.170.134]:46935 "EHLO cloudserver094114.home.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729530AbfBRWJ2 (ORCPT ); Mon, 18 Feb 2019 17:09:28 -0500 Received: from 79.184.254.15.ipv4.supernova.orange.pl (79.184.254.15) (HELO aspire.rjw.lan) by serwer1319399.home.pl (79.96.170.134) with SMTP (IdeaSmtpServer 0.83.183) id acadc6c5712d3779; Mon, 18 Feb 2019 23:09:24 +0100 From: "Rafael J. Wysocki" To: Ulf Hansson , Jon Hunter Cc: Greg Kroah-Hartman , LKML , Linux PM , Daniel Vetter , Lukas Wunner , Andrzej Hajda , Russell King - ARM Linux , Lucas Stach , Linus Walleij , Thierry Reding , Laurent Pinchart , Marek Szyprowski Subject: [RFT/RFC][PATCH] driver core: Fix PM-runtime for links added during consumer probe Date: Mon, 18 Feb 2019 23:08:00 +0100 Message-ID: <14371356.imFjPFOrCi@aspire.rjw.lan> MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Rafael J. Wysocki Commit 4c06c4e6cf63 ("driver core: Fix possible supplier PM-usage counter imbalance") introduced a regression that causes suppliers to be suspended prematurely for device links added during consumer driver probe if the initial PM-runtime status of the consumer is "suspended" and the consumer is resumed after adding the link and before pm_runtime_put_suppliers() is called. In that case, pm_runtime_put_suppliers() will drop the rpm_active refcount for the link by one and (since rpm_active is equal to two after the preceding consumer resume) the supplier's PM-runtime usage counter will be decremented, which may cause the supplier to suspend even though the consumer's PM-runtime status is "active". For this reason, partially revert commit 4c06c4e6cf63 as the problem it tried to fix needs to be addressed somewhat differently, and change pm_runtime_get_suppliers() and pm_runtime_put_suppliers() so that the latter only drops rpm_active references acquired by the former. [This requires adding a new field to struct device_link, but I coulnd't find a cleaner way to address the issue that would work in all cases.] Fixes: 4c06c4e6cf63 ("driver core: Fix possible supplier PM-usage counter imbalance") Reported-by: Jon Hunter Signed-off-by: Rafael J. Wysocki --- On top of driver-core/driver-core-next. Jon, Ulf, please test! --- drivers/base/core.c | 4 ++-- drivers/base/power/runtime.c | 29 ++++++----------------------- include/linux/device.h | 1 + include/linux/pm_runtime.h | 4 ---- 4 files changed, 9 insertions(+), 29 deletions(-) Index: linux-pm/drivers/base/core.c =================================================================== --- linux-pm.orig/drivers/base/core.c +++ linux-pm/drivers/base/core.c @@ -277,7 +277,7 @@ struct device_link *device_link_add(stru link->flags |= DL_FLAG_PM_RUNTIME; } if (flags & DL_FLAG_RPM_ACTIVE) - pm_runtime_active_link(link, supplier); + refcount_inc(&link->rpm_active); } if (flags & DL_FLAG_STATELESS) { @@ -310,7 +310,7 @@ struct device_link *device_link_add(stru if (flags & DL_FLAG_PM_RUNTIME) { if (flags & DL_FLAG_RPM_ACTIVE) - pm_runtime_active_link(link, supplier); + refcount_inc(&link->rpm_active); pm_runtime_new_link(consumer); } Index: linux-pm/drivers/base/power/runtime.c =================================================================== --- linux-pm.orig/drivers/base/power/runtime.c +++ linux-pm/drivers/base/power/runtime.c @@ -1656,6 +1656,7 @@ void pm_runtime_get_suppliers(struct dev list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) if (link->flags & DL_FLAG_PM_RUNTIME) { + link->supplier_preactivated = true; refcount_inc(&link->rpm_active); pm_runtime_get_sync(link->supplier); } @@ -1675,9 +1676,11 @@ void pm_runtime_put_suppliers(struct dev idx = device_links_read_lock(); list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) - if (link->flags & DL_FLAG_PM_RUNTIME && - refcount_dec_not_one(&link->rpm_active)) - pm_runtime_put(link->supplier); + if (link->supplier_preactivated) { + link->supplier_preactivated = false; + if (refcount_dec_not_one(&link->rpm_active)) + pm_runtime_put(link->supplier); + } device_links_read_unlock(idx); } @@ -1689,26 +1692,6 @@ void pm_runtime_new_link(struct device * spin_unlock_irq(&dev->power.lock); } -/** - * pm_runtime_active_link - Set up new device link as active for PM-runtime. - * @link: Device link to be set up as active. - * @supplier: Supplier end of the link. - * - * Add 2 to the rpm_active refcount of @link and increment the PM-runtime - * usage counter of @supplier once more in case the link is being added while - * the consumer driver is probing and pm_runtime_put_suppliers() will be called - * subsequently. - * - * Note that this doesn't prevent rpm_put_suppliers() from decreasing the link's - * rpm_active refcount down to one, so runtime suspend of the consumer end of - * @link is not affected. - */ -void pm_runtime_active_link(struct device_link *link, struct device *supplier) -{ - refcount_add(2, &link->rpm_active); - pm_runtime_get_noresume(supplier); -} - void pm_runtime_drop_link(struct device *dev) { spin_lock_irq(&dev->power.lock); Index: linux-pm/include/linux/device.h =================================================================== --- linux-pm.orig/include/linux/device.h +++ linux-pm/include/linux/device.h @@ -861,6 +861,7 @@ struct device_link { #ifdef CONFIG_SRCU struct rcu_head rcu_head; #endif + bool supplier_preactivated; /* Owned by consumer probe. */ }; /** Index: linux-pm/include/linux/pm_runtime.h =================================================================== --- linux-pm.orig/include/linux/pm_runtime.h +++ linux-pm/include/linux/pm_runtime.h @@ -59,8 +59,6 @@ extern void pm_runtime_clean_up_links(st extern void pm_runtime_get_suppliers(struct device *dev); extern void pm_runtime_put_suppliers(struct device *dev); extern void pm_runtime_new_link(struct device *dev); -extern void pm_runtime_active_link(struct device_link *link, - struct device *supplier); extern void pm_runtime_drop_link(struct device *dev); static inline void pm_suspend_ignore_children(struct device *dev, bool enable) @@ -180,8 +178,6 @@ static inline void pm_runtime_clean_up_l static inline void pm_runtime_get_suppliers(struct device *dev) {} static inline void pm_runtime_put_suppliers(struct device *dev) {} static inline void pm_runtime_new_link(struct device *dev) {} -static inline void pm_runtime_active_link(struct device_link *link, - struct device *supplier) {} static inline void pm_runtime_drop_link(struct device *dev) {} #endif /* !CONFIG_PM */