Commit fab610be authored by Rob Herring's avatar Rob Herring

of: Refactor node and property manipulation function locking

All callers of __of_{add,remove,update}_property() and
__of_{attach,detach}_node() wrap the call with the devtree_lock
spinlock. Let's move the spinlock into the functions. This allows moving
the sysfs update functions into those functions as well.

Link: https://lore.kernel.org/r/20230801-dt-changeset-fixes-v3-6-5f0410e007dd@kernel.orgSigned-off-by: default avatarRob Herring <robh@kernel.org>
parent 6701c2c7
...@@ -1551,21 +1551,32 @@ static struct property *__of_remove_property_from_list(struct property **list, s ...@@ -1551,21 +1551,32 @@ static struct property *__of_remove_property_from_list(struct property **list, s
*/ */
int __of_add_property(struct device_node *np, struct property *prop) int __of_add_property(struct device_node *np, struct property *prop)
{ {
int rc = 0;
unsigned long flags;
struct property **next; struct property **next;
raw_spin_lock_irqsave(&devtree_lock, flags);
__of_remove_property_from_list(&np->deadprops, prop); __of_remove_property_from_list(&np->deadprops, prop);
prop->next = NULL; prop->next = NULL;
next = &np->properties; next = &np->properties;
while (*next) { while (*next) {
if (strcmp(prop->name, (*next)->name) == 0) if (strcmp(prop->name, (*next)->name) == 0) {
/* duplicate ! don't insert it */ /* duplicate ! don't insert it */
return -EEXIST; rc = -EEXIST;
goto out_unlock;
}
next = &(*next)->next; next = &(*next)->next;
} }
*next = prop; *next = prop;
out_unlock:
raw_spin_unlock_irqrestore(&devtree_lock, flags);
if (rc)
return rc;
__of_add_property_sysfs(np, prop);
return 0; return 0;
} }
...@@ -1576,18 +1587,10 @@ int __of_add_property(struct device_node *np, struct property *prop) ...@@ -1576,18 +1587,10 @@ int __of_add_property(struct device_node *np, struct property *prop)
*/ */
int of_add_property(struct device_node *np, struct property *prop) int of_add_property(struct device_node *np, struct property *prop)
{ {
unsigned long flags;
int rc; int rc;
mutex_lock(&of_mutex); mutex_lock(&of_mutex);
raw_spin_lock_irqsave(&devtree_lock, flags);
rc = __of_add_property(np, prop); rc = __of_add_property(np, prop);
raw_spin_unlock_irqrestore(&devtree_lock, flags);
if (!rc)
__of_add_property_sysfs(np, prop);
mutex_unlock(&of_mutex); mutex_unlock(&of_mutex);
if (!rc) if (!rc)
...@@ -1599,14 +1602,24 @@ EXPORT_SYMBOL_GPL(of_add_property); ...@@ -1599,14 +1602,24 @@ EXPORT_SYMBOL_GPL(of_add_property);
int __of_remove_property(struct device_node *np, struct property *prop) int __of_remove_property(struct device_node *np, struct property *prop)
{ {
unsigned long flags;
int rc = -ENODEV;
raw_spin_lock_irqsave(&devtree_lock, flags);
if (__of_remove_property_from_list(&np->properties, prop)) { if (__of_remove_property_from_list(&np->properties, prop)) {
/* Found the property, add it to deadprops list */ /* Found the property, add it to deadprops list */
prop->next = np->deadprops; prop->next = np->deadprops;
np->deadprops = prop; np->deadprops = prop;
return 0; rc = 0;
} }
return -ENODEV; raw_spin_unlock_irqrestore(&devtree_lock, flags);
if (rc)
return rc;
__of_remove_property_sysfs(np, prop);
return 0;
} }
/** /**
...@@ -1621,21 +1634,13 @@ int __of_remove_property(struct device_node *np, struct property *prop) ...@@ -1621,21 +1634,13 @@ int __of_remove_property(struct device_node *np, struct property *prop)
*/ */
int of_remove_property(struct device_node *np, struct property *prop) int of_remove_property(struct device_node *np, struct property *prop)
{ {
unsigned long flags;
int rc; int rc;
if (!prop) if (!prop)
return -ENODEV; return -ENODEV;
mutex_lock(&of_mutex); mutex_lock(&of_mutex);
raw_spin_lock_irqsave(&devtree_lock, flags);
rc = __of_remove_property(np, prop); rc = __of_remove_property(np, prop);
raw_spin_unlock_irqrestore(&devtree_lock, flags);
if (!rc)
__of_remove_property_sysfs(np, prop);
mutex_unlock(&of_mutex); mutex_unlock(&of_mutex);
if (!rc) if (!rc)
...@@ -1649,6 +1654,9 @@ int __of_update_property(struct device_node *np, struct property *newprop, ...@@ -1649,6 +1654,9 @@ int __of_update_property(struct device_node *np, struct property *newprop,
struct property **oldpropp) struct property **oldpropp)
{ {
struct property **next, *oldprop; struct property **next, *oldprop;
unsigned long flags;
raw_spin_lock_irqsave(&devtree_lock, flags);
__of_remove_property_from_list(&np->deadprops, newprop); __of_remove_property_from_list(&np->deadprops, newprop);
...@@ -1670,6 +1678,10 @@ int __of_update_property(struct device_node *np, struct property *newprop, ...@@ -1670,6 +1678,10 @@ int __of_update_property(struct device_node *np, struct property *newprop,
*next = newprop; *next = newprop;
} }
raw_spin_unlock_irqrestore(&devtree_lock, flags);
__of_update_property_sysfs(np, newprop, oldprop);
return 0; return 0;
} }
...@@ -1685,21 +1697,13 @@ int __of_update_property(struct device_node *np, struct property *newprop, ...@@ -1685,21 +1697,13 @@ int __of_update_property(struct device_node *np, struct property *newprop,
int of_update_property(struct device_node *np, struct property *newprop) int of_update_property(struct device_node *np, struct property *newprop)
{ {
struct property *oldprop; struct property *oldprop;
unsigned long flags;
int rc; int rc;
if (!newprop->name) if (!newprop->name)
return -EINVAL; return -EINVAL;
mutex_lock(&of_mutex); mutex_lock(&of_mutex);
raw_spin_lock_irqsave(&devtree_lock, flags);
rc = __of_update_property(np, newprop, &oldprop); rc = __of_update_property(np, newprop, &oldprop);
raw_spin_unlock_irqrestore(&devtree_lock, flags);
if (!rc)
__of_update_property_sysfs(np, newprop, oldprop);
mutex_unlock(&of_mutex); mutex_unlock(&of_mutex);
if (!rc) if (!rc)
......
...@@ -198,6 +198,9 @@ static void __of_attach_node(struct device_node *np) ...@@ -198,6 +198,9 @@ static void __of_attach_node(struct device_node *np)
{ {
const __be32 *phandle; const __be32 *phandle;
int sz; int sz;
unsigned long flags;
raw_spin_lock_irqsave(&devtree_lock, flags);
if (!of_node_check_flag(np, OF_OVERLAY)) { if (!of_node_check_flag(np, OF_OVERLAY)) {
np->name = __of_get_property(np, "name", NULL); np->name = __of_get_property(np, "name", NULL);
...@@ -220,6 +223,10 @@ static void __of_attach_node(struct device_node *np) ...@@ -220,6 +223,10 @@ static void __of_attach_node(struct device_node *np)
np->parent->child = np; np->parent->child = np;
of_node_clear_flag(np, OF_DETACHED); of_node_clear_flag(np, OF_DETACHED);
np->fwnode.flags |= FWNODE_FLAG_NOT_DEVICE; np->fwnode.flags |= FWNODE_FLAG_NOT_DEVICE;
raw_spin_unlock_irqrestore(&devtree_lock, flags);
__of_attach_node_sysfs(np);
} }
/** /**
...@@ -229,17 +236,12 @@ static void __of_attach_node(struct device_node *np) ...@@ -229,17 +236,12 @@ static void __of_attach_node(struct device_node *np)
int of_attach_node(struct device_node *np) int of_attach_node(struct device_node *np)
{ {
struct of_reconfig_data rd; struct of_reconfig_data rd;
unsigned long flags;
memset(&rd, 0, sizeof(rd)); memset(&rd, 0, sizeof(rd));
rd.dn = np; rd.dn = np;
mutex_lock(&of_mutex); mutex_lock(&of_mutex);
raw_spin_lock_irqsave(&devtree_lock, flags);
__of_attach_node(np); __of_attach_node(np);
raw_spin_unlock_irqrestore(&devtree_lock, flags);
__of_attach_node_sysfs(np);
mutex_unlock(&of_mutex); mutex_unlock(&of_mutex);
of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd); of_reconfig_notify(OF_RECONFIG_ATTACH_NODE, &rd);
...@@ -250,13 +252,15 @@ int of_attach_node(struct device_node *np) ...@@ -250,13 +252,15 @@ int of_attach_node(struct device_node *np)
void __of_detach_node(struct device_node *np) void __of_detach_node(struct device_node *np)
{ {
struct device_node *parent; struct device_node *parent;
unsigned long flags;
if (WARN_ON(of_node_check_flag(np, OF_DETACHED))) raw_spin_lock_irqsave(&devtree_lock, flags);
return;
parent = np->parent; parent = np->parent;
if (WARN_ON(!parent)) if (WARN_ON(of_node_check_flag(np, OF_DETACHED) || !parent)) {
raw_spin_unlock_irqrestore(&devtree_lock, flags);
return; return;
}
if (parent->child == np) if (parent->child == np)
parent->child = np->sibling; parent->child = np->sibling;
...@@ -273,6 +277,10 @@ void __of_detach_node(struct device_node *np) ...@@ -273,6 +277,10 @@ void __of_detach_node(struct device_node *np)
/* race with of_find_node_by_phandle() prevented by devtree_lock */ /* race with of_find_node_by_phandle() prevented by devtree_lock */
__of_phandle_cache_inv_entry(np->phandle); __of_phandle_cache_inv_entry(np->phandle);
raw_spin_unlock_irqrestore(&devtree_lock, flags);
__of_detach_node_sysfs(np);
} }
/** /**
...@@ -282,17 +290,12 @@ void __of_detach_node(struct device_node *np) ...@@ -282,17 +290,12 @@ void __of_detach_node(struct device_node *np)
int of_detach_node(struct device_node *np) int of_detach_node(struct device_node *np)
{ {
struct of_reconfig_data rd; struct of_reconfig_data rd;
unsigned long flags;
memset(&rd, 0, sizeof(rd)); memset(&rd, 0, sizeof(rd));
rd.dn = np; rd.dn = np;
mutex_lock(&of_mutex); mutex_lock(&of_mutex);
raw_spin_lock_irqsave(&devtree_lock, flags);
__of_detach_node(np); __of_detach_node(np);
raw_spin_unlock_irqrestore(&devtree_lock, flags);
__of_detach_node_sysfs(np);
mutex_unlock(&of_mutex); mutex_unlock(&of_mutex);
of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd); of_reconfig_notify(OF_RECONFIG_DETACH_NODE, &rd);
...@@ -564,12 +567,10 @@ static int __of_changeset_entry_notify(struct of_changeset_entry *ce, ...@@ -564,12 +567,10 @@ static int __of_changeset_entry_notify(struct of_changeset_entry *ce,
static int __of_changeset_entry_apply(struct of_changeset_entry *ce) static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
{ {
unsigned long flags;
int ret = 0; int ret = 0;
of_changeset_action_debug("apply: ", ce->action, ce->np, ce->prop); of_changeset_action_debug("apply: ", ce->action, ce->np, ce->prop);
raw_spin_lock_irqsave(&devtree_lock, flags);
switch (ce->action) { switch (ce->action) {
case OF_RECONFIG_ATTACH_NODE: case OF_RECONFIG_ATTACH_NODE:
__of_attach_node(ce->np); __of_attach_node(ce->np);
...@@ -590,32 +591,12 @@ static int __of_changeset_entry_apply(struct of_changeset_entry *ce) ...@@ -590,32 +591,12 @@ static int __of_changeset_entry_apply(struct of_changeset_entry *ce)
default: default:
ret = -EINVAL; ret = -EINVAL;
} }
raw_spin_unlock_irqrestore(&devtree_lock, flags);
if (ret) { if (ret) {
of_changeset_action_err("apply failed: ", ce->action, ce->np, ce->prop); of_changeset_action_err("apply failed: ", ce->action, ce->np, ce->prop);
return ret; return ret;
} }
switch (ce->action) {
case OF_RECONFIG_ATTACH_NODE:
__of_attach_node_sysfs(ce->np);
break;
case OF_RECONFIG_DETACH_NODE:
__of_detach_node_sysfs(ce->np);
break;
case OF_RECONFIG_ADD_PROPERTY:
/* ignore duplicate names */
__of_add_property_sysfs(ce->np, ce->prop);
break;
case OF_RECONFIG_REMOVE_PROPERTY:
__of_remove_property_sysfs(ce->np, ce->prop);
break;
case OF_RECONFIG_UPDATE_PROPERTY:
__of_update_property_sysfs(ce->np, ce->prop, ce->old_prop);
break;
}
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment