Skip to content

Commit adcae20

Browse files
andy-shevgregkh
authored andcommitted
driver core: Use guards for simple mutex locks
Guards can help to make the code more readable. So use it wherever they do so. Signed-off-by: Andy Shevchenko <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent a355a46 commit adcae20

File tree

1 file changed

+22
-28
lines changed

1 file changed

+22
-28
lines changed

drivers/base/core.c

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,9 @@ static int __fwnode_link_add(struct fwnode_handle *con,
9898
int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup,
9999
u8 flags)
100100
{
101-
int ret;
101+
guard(mutex)(&fwnode_link_lock);
102102

103-
mutex_lock(&fwnode_link_lock);
104-
ret = __fwnode_link_add(con, sup, flags);
105-
mutex_unlock(&fwnode_link_lock);
106-
return ret;
103+
return __fwnode_link_add(con, sup, flags);
107104
}
108105

109106
/**
@@ -144,10 +141,10 @@ static void fwnode_links_purge_suppliers(struct fwnode_handle *fwnode)
144141
{
145142
struct fwnode_link *link, *tmp;
146143

147-
mutex_lock(&fwnode_link_lock);
144+
guard(mutex)(&fwnode_link_lock);
145+
148146
list_for_each_entry_safe(link, tmp, &fwnode->suppliers, c_hook)
149147
__fwnode_link_del(link);
150-
mutex_unlock(&fwnode_link_lock);
151148
}
152149

153150
/**
@@ -160,10 +157,10 @@ static void fwnode_links_purge_consumers(struct fwnode_handle *fwnode)
160157
{
161158
struct fwnode_link *link, *tmp;
162159

163-
mutex_lock(&fwnode_link_lock);
160+
guard(mutex)(&fwnode_link_lock);
161+
164162
list_for_each_entry_safe(link, tmp, &fwnode->consumers, s_hook)
165163
__fwnode_link_del(link);
166-
mutex_unlock(&fwnode_link_lock);
167164
}
168165

169166
/**
@@ -1059,20 +1056,16 @@ int device_links_check_suppliers(struct device *dev)
10591056
* Device waiting for supplier to become available is not allowed to
10601057
* probe.
10611058
*/
1062-
mutex_lock(&fwnode_link_lock);
1063-
sup_fw = fwnode_links_check_suppliers(dev->fwnode);
1064-
if (sup_fw) {
1065-
if (!dev_is_best_effort(dev)) {
1066-
fwnode_ret = -EPROBE_DEFER;
1067-
dev_err_probe(dev, -EPROBE_DEFER,
1068-
"wait for supplier %pfwf\n", sup_fw);
1069-
} else {
1070-
fwnode_ret = -EAGAIN;
1059+
scoped_guard(mutex, &fwnode_link_lock) {
1060+
sup_fw = fwnode_links_check_suppliers(dev->fwnode);
1061+
if (sup_fw) {
1062+
if (dev_is_best_effort(dev))
1063+
fwnode_ret = -EAGAIN;
1064+
else
1065+
return dev_err_probe(dev, -EPROBE_DEFER,
1066+
"wait for supplier %pfwf\n", sup_fw);
10711067
}
10721068
}
1073-
mutex_unlock(&fwnode_link_lock);
1074-
if (fwnode_ret == -EPROBE_DEFER)
1075-
return fwnode_ret;
10761069

10771070
device_links_write_lock();
10781071

@@ -1247,9 +1240,8 @@ static ssize_t waiting_for_supplier_show(struct device *dev,
12471240
bool val;
12481241

12491242
device_lock(dev);
1250-
mutex_lock(&fwnode_link_lock);
1251-
val = !!fwnode_links_check_suppliers(dev->fwnode);
1252-
mutex_unlock(&fwnode_link_lock);
1243+
scoped_guard(mutex, &fwnode_link_lock)
1244+
val = !!fwnode_links_check_suppliers(dev->fwnode);
12531245
device_unlock(dev);
12541246
return sysfs_emit(buf, "%u\n", val);
12551247
}
@@ -1322,13 +1314,15 @@ void device_links_driver_bound(struct device *dev)
13221314
*/
13231315
if (dev->fwnode && dev->fwnode->dev == dev) {
13241316
struct fwnode_handle *child;
1317+
13251318
fwnode_links_purge_suppliers(dev->fwnode);
1326-
mutex_lock(&fwnode_link_lock);
1319+
1320+
guard(mutex)(&fwnode_link_lock);
1321+
13271322
fwnode_for_each_available_child_node(dev->fwnode, child)
13281323
__fw_devlink_pickup_dangling_consumers(child,
13291324
dev->fwnode);
13301325
__fw_devlink_link_to_consumers(dev);
1331-
mutex_unlock(&fwnode_link_lock);
13321326
}
13331327
device_remove_file(dev, &dev_attr_waiting_for_supplier);
13341328

@@ -2337,10 +2331,10 @@ static void fw_devlink_link_device(struct device *dev)
23372331

23382332
fw_devlink_parse_fwtree(fwnode);
23392333

2340-
mutex_lock(&fwnode_link_lock);
2334+
guard(mutex)(&fwnode_link_lock);
2335+
23412336
__fw_devlink_link_to_consumers(dev);
23422337
__fw_devlink_link_to_suppliers(dev, fwnode);
2343-
mutex_unlock(&fwnode_link_lock);
23442338
}
23452339

23462340
/* Device links support end. */

0 commit comments

Comments
 (0)