提交 b48b3e9a 编写于 作者: R Rafael J. Wysocki 提交者: Zheng Zengkai

PM: runtime: Add safety net to supplier device release

stable inclusion
from stable-v5.10.94
commit 37b25de3af10a3082f9ef5888de0f8602c5dcadf
bugzilla: https://gitee.com/openeuler/kernel/issues/I531X9

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=37b25de3af10a3082f9ef5888de0f8602c5dcadf

--------------------------------

[ Upstream commit d1579e61 ]

Because refcount_dec_not_one() returns true if the target refcount
becomes saturated, it is generally unsafe to use its return value as
a loop termination condition, but that is what happens when a device
link's supplier device is released during runtime PM suspend
operations and on device link removal.

To address this, introduce pm_runtime_release_supplier() to be used
in the above cases which will check the supplier device's runtime
PM usage counter in addition to the refcount_dec_not_one() return
value, so the loop can be terminated in case the rpm_active refcount
value becomes invalid, and update the code in question to use it as
appropriate.

This change is not expected to have any visible functional impact.
Reported-by: NPeter Zijlstra <peterz@infradead.org>
Signed-off-by: NRafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: NGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Acked-by: NPeter Zijlstra (Intel) <peterz@infradead.org>
Signed-off-by: NSasha Levin <sashal@kernel.org>
Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
Acked-by: NXie XiuQi <xiexiuqi@huawei.com>
上级 25368d5f
...@@ -348,8 +348,7 @@ static void device_link_release_fn(struct work_struct *work) ...@@ -348,8 +348,7 @@ static void device_link_release_fn(struct work_struct *work)
/* Ensure that all references to the link object have been dropped. */ /* Ensure that all references to the link object have been dropped. */
device_link_synchronize_removal(); device_link_synchronize_removal();
while (refcount_dec_not_one(&link->rpm_active)) pm_runtime_release_supplier(link, true);
pm_runtime_put(link->supplier);
put_device(link->consumer); put_device(link->consumer);
put_device(link->supplier); put_device(link->supplier);
......
...@@ -305,19 +305,40 @@ static int rpm_get_suppliers(struct device *dev) ...@@ -305,19 +305,40 @@ static int rpm_get_suppliers(struct device *dev)
return 0; return 0;
} }
/**
* pm_runtime_release_supplier - Drop references to device link's supplier.
* @link: Target device link.
* @check_idle: Whether or not to check if the supplier device is idle.
*
* Drop all runtime PM references associated with @link to its supplier device
* and if @check_idle is set, check if that device is idle (and so it can be
* suspended).
*/
void pm_runtime_release_supplier(struct device_link *link, bool check_idle)
{
struct device *supplier = link->supplier;
/*
* The additional power.usage_count check is a safety net in case
* the rpm_active refcount becomes saturated, in which case
* refcount_dec_not_one() would return true forever, but it is not
* strictly necessary.
*/
while (refcount_dec_not_one(&link->rpm_active) &&
atomic_read(&supplier->power.usage_count) > 0)
pm_runtime_put_noidle(supplier);
if (check_idle)
pm_request_idle(supplier);
}
static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend) static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend)
{ {
struct device_link *link; struct device_link *link;
list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
device_links_read_lock_held()) { device_links_read_lock_held())
pm_runtime_release_supplier(link, try_to_suspend);
while (refcount_dec_not_one(&link->rpm_active))
pm_runtime_put_noidle(link->supplier);
if (try_to_suspend)
pm_request_idle(link->supplier);
}
} }
static void rpm_put_suppliers(struct device *dev) static void rpm_put_suppliers(struct device *dev)
...@@ -1755,9 +1776,7 @@ void pm_runtime_drop_link(struct device_link *link) ...@@ -1755,9 +1776,7 @@ void pm_runtime_drop_link(struct device_link *link)
return; return;
pm_runtime_drop_link_count(link->consumer); pm_runtime_drop_link_count(link->consumer);
pm_runtime_release_supplier(link, true);
while (refcount_dec_not_one(&link->rpm_active))
pm_runtime_put(link->supplier);
} }
static bool pm_runtime_need_not_resume(struct device *dev) static bool pm_runtime_need_not_resume(struct device *dev)
......
...@@ -58,6 +58,7 @@ extern void pm_runtime_get_suppliers(struct device *dev); ...@@ -58,6 +58,7 @@ extern void pm_runtime_get_suppliers(struct device *dev);
extern void pm_runtime_put_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_new_link(struct device *dev);
extern void pm_runtime_drop_link(struct device_link *link); extern void pm_runtime_drop_link(struct device_link *link);
extern void pm_runtime_release_supplier(struct device_link *link, bool check_idle);
/** /**
* pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter. * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter.
...@@ -279,6 +280,8 @@ static inline void pm_runtime_get_suppliers(struct device *dev) {} ...@@ -279,6 +280,8 @@ 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_put_suppliers(struct device *dev) {}
static inline void pm_runtime_new_link(struct device *dev) {} static inline void pm_runtime_new_link(struct device *dev) {}
static inline void pm_runtime_drop_link(struct device_link *link) {} static inline void pm_runtime_drop_link(struct device_link *link) {}
static inline void pm_runtime_release_supplier(struct device_link *link,
bool check_idle) {}
#endif /* !CONFIG_PM */ #endif /* !CONFIG_PM */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册