Skip to content

Commit f72e77c

Browse files
diandersDanilo Krummrich
authored andcommitted
device property: Make modifications of fwnode "flags" thread safe
In various places in the kernel, we modify the fwnode "flags" member by doing either: fwnode->flags |= SOME_FLAG; fwnode->flags &= ~SOME_FLAG; This type of modification is not thread-safe. If two threads are both mucking with the flags at the same time then one can clobber the other. While flags are often modified while under the "fwnode_link_lock", this is not universally true. Create some accessor functions for setting, clearing, and testing the FWNODE flags and move all users to these accessor functions. New accessor functions use set_bit() and clear_bit(), which are thread-safe. Cc: stable@vger.kernel.org Fixes: c2c724c ("driver core: Add fw_devlink_parse_fwtree()") Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Acked-by: Mark Brown <broonie@kernel.org> Reviewed-by: Wolfram Sang <wsa+renesas@sang-engineering.com> Signed-off-by: Douglas Anderson <dianders@chromium.org> Reviewed-by: Rafael J. Wysocki (Intel) <rafael@kernel.org> Reviewed-by: Saravana Kannan <saravanak@kernel.org> Link: https://patch.msgid.link/20260317090112.v2.1.I0a4d03104ecd5103df3d76f66c8d21b1d15a2e38@changeid [ Fix fwnode_clear_flag() argument alignment, restore dropped blank line in fwnode_dev_initialized(), and remove unnecessary parentheses around fwnode_test_flag() calls. - Danilo ] Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 14cf406 commit f72e77c

9 files changed

Lines changed: 53 additions & 31 deletions

File tree

drivers/base/core.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ void fw_devlink_purge_absent_suppliers(struct fwnode_handle *fwnode)
182182
if (fwnode->dev)
183183
return;
184184

185-
fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
185+
fwnode_set_flag(fwnode, FWNODE_FLAG_NOT_DEVICE);
186186
fwnode_links_purge_consumers(fwnode);
187187

188188
fwnode_for_each_available_child_node(fwnode, child)
@@ -228,7 +228,7 @@ static void __fw_devlink_pickup_dangling_consumers(struct fwnode_handle *fwnode,
228228
if (fwnode->dev && fwnode->dev->bus)
229229
return;
230230

231-
fwnode->flags |= FWNODE_FLAG_NOT_DEVICE;
231+
fwnode_set_flag(fwnode, FWNODE_FLAG_NOT_DEVICE);
232232
__fwnode_links_move_consumers(fwnode, new_sup);
233233

234234
fwnode_for_each_available_child_node(fwnode, child)
@@ -1012,7 +1012,7 @@ static void device_links_missing_supplier(struct device *dev)
10121012
static bool dev_is_best_effort(struct device *dev)
10131013
{
10141014
return (fw_devlink_best_effort && dev->can_match) ||
1015-
(dev->fwnode && (dev->fwnode->flags & FWNODE_FLAG_BEST_EFFORT));
1015+
(dev->fwnode && fwnode_test_flag(dev->fwnode, FWNODE_FLAG_BEST_EFFORT));
10161016
}
10171017

10181018
static struct fwnode_handle *fwnode_links_check_suppliers(
@@ -1723,11 +1723,11 @@ bool fw_devlink_is_strict(void)
17231723

17241724
static void fw_devlink_parse_fwnode(struct fwnode_handle *fwnode)
17251725
{
1726-
if (fwnode->flags & FWNODE_FLAG_LINKS_ADDED)
1726+
if (fwnode_test_flag(fwnode, FWNODE_FLAG_LINKS_ADDED))
17271727
return;
17281728

17291729
fwnode_call_int_op(fwnode, add_links);
1730-
fwnode->flags |= FWNODE_FLAG_LINKS_ADDED;
1730+
fwnode_set_flag(fwnode, FWNODE_FLAG_LINKS_ADDED);
17311731
}
17321732

17331733
static void fw_devlink_parse_fwtree(struct fwnode_handle *fwnode)
@@ -1885,7 +1885,7 @@ static bool fwnode_init_without_drv(struct fwnode_handle *fwnode)
18851885
struct device *dev;
18861886
bool ret;
18871887

1888-
if (!(fwnode->flags & FWNODE_FLAG_INITIALIZED))
1888+
if (!fwnode_test_flag(fwnode, FWNODE_FLAG_INITIALIZED))
18891889
return false;
18901890

18911891
dev = get_dev_from_fwnode(fwnode);
@@ -2001,10 +2001,10 @@ static bool __fw_devlink_relax_cycles(struct fwnode_handle *con_handle,
20012001
* We aren't trying to find all cycles. Just a cycle between con and
20022002
* sup_handle.
20032003
*/
2004-
if (sup_handle->flags & FWNODE_FLAG_VISITED)
2004+
if (fwnode_test_flag(sup_handle, FWNODE_FLAG_VISITED))
20052005
return false;
20062006

2007-
sup_handle->flags |= FWNODE_FLAG_VISITED;
2007+
fwnode_set_flag(sup_handle, FWNODE_FLAG_VISITED);
20082008

20092009
/* Termination condition. */
20102010
if (sup_handle == con_handle) {
@@ -2074,7 +2074,7 @@ static bool __fw_devlink_relax_cycles(struct fwnode_handle *con_handle,
20742074
}
20752075

20762076
out:
2077-
sup_handle->flags &= ~FWNODE_FLAG_VISITED;
2077+
fwnode_clear_flag(sup_handle, FWNODE_FLAG_VISITED);
20782078
put_device(sup_dev);
20792079
put_device(con_dev);
20802080
put_device(par_dev);
@@ -2127,7 +2127,7 @@ static int fw_devlink_create_devlink(struct device *con,
21272127
* When such a flag is set, we can't create device links where P is the
21282128
* supplier of C as that would delay the probe of C.
21292129
*/
2130-
if (sup_handle->flags & FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD &&
2130+
if (fwnode_test_flag(sup_handle, FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD) &&
21312131
fwnode_is_ancestor_of(sup_handle, con->fwnode))
21322132
return -EINVAL;
21332133

@@ -2150,7 +2150,7 @@ static int fw_devlink_create_devlink(struct device *con,
21502150
else
21512151
flags = FW_DEVLINK_FLAGS_PERMISSIVE;
21522152

2153-
if (sup_handle->flags & FWNODE_FLAG_NOT_DEVICE)
2153+
if (fwnode_test_flag(sup_handle, FWNODE_FLAG_NOT_DEVICE))
21542154
sup_dev = fwnode_get_next_parent_dev(sup_handle);
21552155
else
21562156
sup_dev = get_dev_from_fwnode(sup_handle);
@@ -2162,7 +2162,7 @@ static int fw_devlink_create_devlink(struct device *con,
21622162
* supplier device indefinitely.
21632163
*/
21642164
if (sup_dev->links.status == DL_DEV_NO_DRIVER &&
2165-
sup_handle->flags & FWNODE_FLAG_INITIALIZED) {
2165+
fwnode_test_flag(sup_handle, FWNODE_FLAG_INITIALIZED)) {
21662166
dev_dbg(con,
21672167
"Not linking %pfwf - dev might never probe\n",
21682168
sup_handle);

drivers/bus/imx-weim.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ static int of_weim_notify(struct notifier_block *nb, unsigned long action,
332332
* fw_devlink doesn't skip adding consumers to this
333333
* device.
334334
*/
335-
rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
335+
fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
336336
if (!of_platform_device_create(rd->dn, NULL, &pdev->dev)) {
337337
dev_err(&pdev->dev,
338338
"Failed to create child device '%pOF'\n",

drivers/i2c/i2c-core-of.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
180180
* Clear the flag before adding the device so that fw_devlink
181181
* doesn't skip adding consumers to this device.
182182
*/
183-
rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
183+
fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
184184
client = of_i2c_register_device(adap, rd->dn);
185185
if (IS_ERR(client)) {
186186
dev_err(&adap->dev, "failed to create client for '%pOF'\n",

drivers/net/phy/mdio_bus_provider.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ int __mdiobus_register(struct mii_bus *bus, struct module *owner)
294294
return -EINVAL;
295295

296296
if (bus->parent && bus->parent->of_node)
297-
bus->parent->of_node->fwnode.flags |=
298-
FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD;
297+
fwnode_set_flag(&bus->parent->of_node->fwnode,
298+
FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD);
299299

300300
WARN(bus->state != MDIOBUS_ALLOCATED &&
301301
bus->state != MDIOBUS_UNREGISTERED,

drivers/of/base.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1943,7 +1943,7 @@ void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
19431943
if (name)
19441944
of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
19451945
if (of_stdout)
1946-
of_stdout->fwnode.flags |= FWNODE_FLAG_BEST_EFFORT;
1946+
fwnode_set_flag(&of_stdout->fwnode, FWNODE_FLAG_BEST_EFFORT);
19471947
}
19481948

19491949
if (!of_aliases)

drivers/of/dynamic.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static void __of_attach_node(struct device_node *np)
225225
np->sibling = np->parent->child;
226226
np->parent->child = np;
227227
of_node_clear_flag(np, OF_DETACHED);
228-
np->fwnode.flags |= FWNODE_FLAG_NOT_DEVICE;
228+
fwnode_set_flag(&np->fwnode, FWNODE_FLAG_NOT_DEVICE);
229229

230230
raw_spin_unlock_irqrestore(&devtree_lock, flags);
231231

drivers/of/platform.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ static int of_platform_notify(struct notifier_block *nb,
742742
* Clear the flag before adding the device so that fw_devlink
743743
* doesn't skip adding consumers to this device.
744744
*/
745-
rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
745+
fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
746746
/* pdev_parent may be NULL when no bus platform device */
747747
pdev_parent = of_find_device_by_node(parent);
748748
pdev = of_platform_device_create(rd->dn, NULL,

drivers/spi/spi.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4937,7 +4937,7 @@ static int of_spi_notify(struct notifier_block *nb, unsigned long action,
49374937
* Clear the flag before adding the device so that fw_devlink
49384938
* doesn't skip adding consumers to this device.
49394939
*/
4940-
rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
4940+
fwnode_clear_flag(&rd->dn->fwnode, FWNODE_FLAG_NOT_DEVICE);
49414941
spi = of_register_spi_device(ctlr, rd->dn);
49424942
put_device(&ctlr->dev);
49434943

include/linux/fwnode.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define _LINUX_FWNODE_H_
1616

1717
#include <linux/bits.h>
18+
#include <linux/bitops.h>
1819
#include <linux/err.h>
1920
#include <linux/list.h>
2021
#include <linux/types.h>
@@ -42,12 +43,12 @@ struct device;
4243
* suppliers. Only enforce ordering with suppliers that have
4344
* drivers.
4445
*/
45-
#define FWNODE_FLAG_LINKS_ADDED BIT(0)
46-
#define FWNODE_FLAG_NOT_DEVICE BIT(1)
47-
#define FWNODE_FLAG_INITIALIZED BIT(2)
48-
#define FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD BIT(3)
49-
#define FWNODE_FLAG_BEST_EFFORT BIT(4)
50-
#define FWNODE_FLAG_VISITED BIT(5)
46+
#define FWNODE_FLAG_LINKS_ADDED 0
47+
#define FWNODE_FLAG_NOT_DEVICE 1
48+
#define FWNODE_FLAG_INITIALIZED 2
49+
#define FWNODE_FLAG_NEEDS_CHILD_BOUND_ON_ADD 3
50+
#define FWNODE_FLAG_BEST_EFFORT 4
51+
#define FWNODE_FLAG_VISITED 5
5152

5253
struct fwnode_handle {
5354
struct fwnode_handle *secondary;
@@ -57,7 +58,7 @@ struct fwnode_handle {
5758
struct device *dev;
5859
struct list_head suppliers;
5960
struct list_head consumers;
60-
u8 flags;
61+
unsigned long flags;
6162
};
6263

6364
/*
@@ -212,16 +213,37 @@ static inline void fwnode_init(struct fwnode_handle *fwnode,
212213
INIT_LIST_HEAD(&fwnode->suppliers);
213214
}
214215

216+
static inline void fwnode_set_flag(struct fwnode_handle *fwnode,
217+
unsigned int bit)
218+
{
219+
set_bit(bit, &fwnode->flags);
220+
}
221+
222+
static inline void fwnode_clear_flag(struct fwnode_handle *fwnode,
223+
unsigned int bit)
224+
{
225+
clear_bit(bit, &fwnode->flags);
226+
}
227+
228+
static inline void fwnode_assign_flag(struct fwnode_handle *fwnode,
229+
unsigned int bit, bool value)
230+
{
231+
assign_bit(bit, &fwnode->flags, value);
232+
}
233+
234+
static inline bool fwnode_test_flag(struct fwnode_handle *fwnode,
235+
unsigned int bit)
236+
{
237+
return test_bit(bit, &fwnode->flags);
238+
}
239+
215240
static inline void fwnode_dev_initialized(struct fwnode_handle *fwnode,
216241
bool initialized)
217242
{
218243
if (IS_ERR_OR_NULL(fwnode))
219244
return;
220245

221-
if (initialized)
222-
fwnode->flags |= FWNODE_FLAG_INITIALIZED;
223-
else
224-
fwnode->flags &= ~FWNODE_FLAG_INITIALIZED;
246+
fwnode_assign_flag(fwnode, FWNODE_FLAG_INITIALIZED, initialized);
225247
}
226248

227249
int fwnode_link_add(struct fwnode_handle *con, struct fwnode_handle *sup,

0 commit comments

Comments
 (0)