Skip to content

Commit baa8809

Browse files
rafaeljwgregkh
authored andcommitted
PM / runtime: Optimize the use of device links
If the device has no links to suppliers that should be used for runtime PM (links with DEVICE_LINK_PM_RUNTIME set), there is no reason to walk the list of suppliers for that device during runtime suspend and resume. Add a simple mechanism to detect that case and possibly avoid the extra unnecessary overhead. Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 21d5c57 commit baa8809

4 files changed

Lines changed: 38 additions & 10 deletions

File tree

‎drivers/base/core.c‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,14 +205,17 @@ struct device_link *device_link_add(struct device *consumer,
205205
if (!link)
206206
goto out;
207207

208-
if ((flags & DL_FLAG_PM_RUNTIME) && (flags & DL_FLAG_RPM_ACTIVE)) {
209-
if (pm_runtime_get_sync(supplier) < 0) {
210-
pm_runtime_put_noidle(supplier);
211-
kfree(link);
212-
link = NULL;
213-
goto out;
208+
if (flags & DL_FLAG_PM_RUNTIME) {
209+
if (flags & DL_FLAG_RPM_ACTIVE) {
210+
if (pm_runtime_get_sync(supplier) < 0) {
211+
pm_runtime_put_noidle(supplier);
212+
kfree(link);
213+
link = NULL;
214+
goto out;
215+
}
216+
link->rpm_active = true;
214217
}
215-
link->rpm_active = true;
218+
pm_runtime_new_link(consumer);
216219
}
217220
get_device(supplier);
218221
link->supplier = supplier;
@@ -296,6 +299,9 @@ static void __device_link_del(struct device_link *link)
296299
dev_info(link->consumer, "Dropping the link to %s\n",
297300
dev_name(link->supplier));
298301

302+
if (link->flags & DL_FLAG_PM_RUNTIME)
303+
pm_runtime_drop_link(link->consumer);
304+
299305
list_del_rcu(&link->s_node);
300306
list_del_rcu(&link->c_node);
301307
call_srcu(&device_links_srcu, &link->rcu_head, __device_link_free_srcu);

‎drivers/base/power/runtime.c‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
305305
__releases(&dev->power.lock) __acquires(&dev->power.lock)
306306
{
307307
int retval, idx;
308+
bool use_links = dev->power.links_count > 0;
308309

309310
if (dev->power.irq_safe) {
310311
spin_unlock(&dev->power.lock);
@@ -318,7 +319,7 @@ static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
318319
* routine returns, so it is safe to read the status outside of
319320
* the lock.
320321
*/
321-
if (dev->power.runtime_status == RPM_RESUMING) {
322+
if (use_links && dev->power.runtime_status == RPM_RESUMING) {
322323
idx = device_links_read_lock();
323324

324325
retval = rpm_get_suppliers(dev);
@@ -341,8 +342,9 @@ static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
341342
*
342343
* Do that if resume fails too.
343344
*/
344-
if ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
345-
|| (dev->power.runtime_status == RPM_RESUMING && retval)) {
345+
if (use_links
346+
&& ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
347+
|| (dev->power.runtime_status == RPM_RESUMING && retval))) {
346348
idx = device_links_read_lock();
347349

348350
fail:
@@ -1593,6 +1595,21 @@ void pm_runtime_put_suppliers(struct device *dev)
15931595
device_links_read_unlock(idx);
15941596
}
15951597

1598+
void pm_runtime_new_link(struct device *dev)
1599+
{
1600+
spin_lock_irq(&dev->power.lock);
1601+
dev->power.links_count++;
1602+
spin_unlock_irq(&dev->power.lock);
1603+
}
1604+
1605+
void pm_runtime_drop_link(struct device *dev)
1606+
{
1607+
spin_lock_irq(&dev->power.lock);
1608+
WARN_ON(dev->power.links_count == 0);
1609+
dev->power.links_count--;
1610+
spin_unlock_irq(&dev->power.lock);
1611+
}
1612+
15961613
/**
15971614
* pm_runtime_force_suspend - Force a device into suspend state if needed.
15981615
* @dev: Device to suspend.

‎include/linux/pm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ struct dev_pm_info {
597597
unsigned int use_autosuspend:1;
598598
unsigned int timer_autosuspends:1;
599599
unsigned int memalloc_noio:1;
600+
unsigned int links_count;
600601
enum rpm_request request;
601602
enum rpm_status runtime_status;
602603
int runtime_error;

‎include/linux/pm_runtime.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ extern void pm_runtime_set_memalloc_noio(struct device *dev, bool enable);
5858
extern void pm_runtime_clean_up_links(struct device *dev);
5959
extern void pm_runtime_get_suppliers(struct device *dev);
6060
extern void pm_runtime_put_suppliers(struct device *dev);
61+
extern void pm_runtime_new_link(struct device *dev);
62+
extern void pm_runtime_drop_link(struct device *dev);
6163

6264
static inline void pm_suspend_ignore_children(struct device *dev, bool enable)
6365
{
@@ -192,6 +194,8 @@ static inline void pm_runtime_set_memalloc_noio(struct device *dev,
192194
static inline void pm_runtime_clean_up_links(struct device *dev) {}
193195
static inline void pm_runtime_get_suppliers(struct device *dev) {}
194196
static inline void pm_runtime_put_suppliers(struct device *dev) {}
197+
static inline void pm_runtime_new_link(struct device *dev) {}
198+
static inline void pm_runtime_drop_link(struct device *dev) {}
195199

196200
#endif /* !CONFIG_PM */
197201

0 commit comments

Comments
 (0)