Skip to content

Commit 8a2b22a

Browse files
committed
of: Make devicetree sysfs update functions consistent.
All of the DT modification functions are split into two parts, the first part manipulates the DT data structure, and the second part updates sysfs, but the code isn't very consistent about how the second half is called. They don't all enforce the same rules about when it is valid to update sysfs, and there isn't any clarity on locking. The transactional DT modification feature that is coming also needs access to these functions so that it can perform all the structure changes together, and then all the sysfs updates as a second stage instead of doing each one at a time. Fix up the second have by creating a separate __of_*_sysfs() function for each of the helpers. The new functions have consistent naming (ie. of_node_add() becomes __of_attach_node_sysfs()) and all of them now defer if of_init hasn't been called yet. Callers of the new functions must hold the of_mutex to ensure there are no race conditions with of_init(). The mutex ensures that there will only ever be one writer to the tree at any given time. There can still be any number of readers and the raw_spin_lock is still used to make sure access to the data structure is still consistent. Finally, put the function prototypes into of_private.h so they are accessible to the transaction code. Signed-off-by: Pantelis Antoniou <pantelis.antoniou@konsulko.com> [grant.likely: Changed suffix from _post to _sysfs to match existing code] [grant.likely: Reorganized to eliminate trivial wrappers] Signed-off-by: Grant Likely <grant.likely@linaro.org>
1 parent d8c5008 commit 8a2b22a

4 files changed

Lines changed: 69 additions & 51 deletions

File tree

‎drivers/of/base.c‎

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ struct device_node *of_chosen;
3737
struct device_node *of_aliases;
3838
static struct device_node *of_stdout;
3939

40-
static struct kset *of_kset;
40+
struct kset *of_kset;
4141

4242
/*
43-
* Used to protect the of_aliases, to hold off addition of nodes to sysfs
43+
* Used to protect the of_aliases, to hold off addition of nodes to sysfs.
44+
* This mutex must be held whenever modifications are being made to the
45+
* device tree. The of_{attach,detach}_node() and
46+
* of_{add,remove,update}_property() helpers make sure this happens.
4447
*/
4548
DEFINE_MUTEX(of_mutex);
4649

@@ -127,13 +130,16 @@ static const char *safe_name(struct kobject *kobj, const char *orig_name)
127130
return name;
128131
}
129132

130-
static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
133+
int __of_add_property_sysfs(struct device_node *np, struct property *pp)
131134
{
132135
int rc;
133136

134137
/* Important: Don't leak passwords */
135138
bool secure = strncmp(pp->name, "security-", 9) == 0;
136139

140+
if (!of_kset || !of_node_is_attached(np))
141+
return 0;
142+
137143
sysfs_bin_attr_init(&pp->attr);
138144
pp->attr.attr.name = safe_name(&np->kobj, pp->name);
139145
pp->attr.attr.mode = secure ? S_IRUSR : S_IRUGO;
@@ -145,12 +151,15 @@ static int __of_add_property_sysfs(struct device_node *np, struct property *pp)
145151
return rc;
146152
}
147153

148-
static int __of_node_add(struct device_node *np)
154+
int __of_attach_node_sysfs(struct device_node *np)
149155
{
150156
const char *name;
151157
struct property *pp;
152158
int rc;
153159

160+
if (!of_kset)
161+
return 0;
162+
154163
np->kobj.kset = of_kset;
155164
if (!np->parent) {
156165
/* Nodes without parents are new top level trees */
@@ -172,26 +181,6 @@ static int __of_node_add(struct device_node *np)
172181
return 0;
173182
}
174183

175-
int of_node_add(struct device_node *np)
176-
{
177-
int rc = 0;
178-
179-
BUG_ON(!of_node_is_initialized(np));
180-
181-
/*
182-
* Grab the mutex here so that in a race condition between of_init() and
183-
* of_node_add(), node addition will still be consistent.
184-
*/
185-
mutex_lock(&of_mutex);
186-
if (of_kset)
187-
rc = __of_node_add(np);
188-
else
189-
/* This scenario may be perfectly valid, but report it anyway */
190-
pr_info("of_node_add(%s) before of_init()\n", np->full_name);
191-
mutex_unlock(&of_mutex);
192-
return rc;
193-
}
194-
195184
static int __init of_init(void)
196185
{
197186
struct device_node *np;
@@ -204,7 +193,7 @@ static int __init of_init(void)
204193
return -ENOMEM;
205194
}
206195
for_each_of_allnodes(np)
207-
__of_node_add(np);
196+
__of_attach_node_sysfs(np);
208197
mutex_unlock(&of_mutex);
209198

210199
/* Symlink in /proc as required by userspace ABI */
@@ -1689,15 +1678,17 @@ int of_add_property(struct device_node *np, struct property *prop)
16891678
if (rc)
16901679
return rc;
16911680

1681+
mutex_lock(&of_mutex);
1682+
16921683
raw_spin_lock_irqsave(&devtree_lock, flags);
16931684
rc = __of_add_property(np, prop);
16941685
raw_spin_unlock_irqrestore(&devtree_lock, flags);
1695-
if (rc)
1696-
return rc;
16971686

1698-
if (of_node_is_attached(np))
1687+
if (!rc)
16991688
__of_add_property_sysfs(np, prop);
17001689

1690+
mutex_unlock(&of_mutex);
1691+
17011692
return rc;
17021693
}
17031694

@@ -1720,6 +1711,13 @@ int __of_remove_property(struct device_node *np, struct property *prop)
17201711
return 0;
17211712
}
17221713

1714+
void __of_remove_property_sysfs(struct device_node *np, struct property *prop)
1715+
{
1716+
/* at early boot, bail here and defer setup to of_init() */
1717+
if (of_kset && of_node_is_attached(np))
1718+
sysfs_remove_bin_file(&np->kobj, &prop->attr);
1719+
}
1720+
17231721
/**
17241722
* of_remove_property - Remove a property from a node.
17251723
*
@@ -1737,20 +1735,18 @@ int of_remove_property(struct device_node *np, struct property *prop)
17371735
if (rc)
17381736
return rc;
17391737

1738+
mutex_lock(&of_mutex);
1739+
17401740
raw_spin_lock_irqsave(&devtree_lock, flags);
17411741
rc = __of_remove_property(np, prop);
17421742
raw_spin_unlock_irqrestore(&devtree_lock, flags);
17431743

1744-
if (rc)
1745-
return rc;
1746-
1747-
/* at early boot, bail hear and defer setup to of_init() */
1748-
if (!of_kset)
1749-
return 0;
1744+
if (!rc)
1745+
__of_remove_property_sysfs(np, prop);
17501746

1751-
sysfs_remove_bin_file(&np->kobj, &prop->attr);
1747+
mutex_unlock(&of_mutex);
17521748

1753-
return 0;
1749+
return rc;
17541750
}
17551751

17561752
int __of_update_property(struct device_node *np, struct property *newprop,
@@ -1779,6 +1775,18 @@ int __of_update_property(struct device_node *np, struct property *newprop,
17791775
return 0;
17801776
}
17811777

1778+
void __of_update_property_sysfs(struct device_node *np, struct property *newprop,
1779+
struct property *oldprop)
1780+
{
1781+
/* At early boot, bail out and defer setup to of_init() */
1782+
if (!of_kset)
1783+
return;
1784+
1785+
if (oldprop)
1786+
sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1787+
__of_add_property_sysfs(np, newprop);
1788+
}
1789+
17821790
/*
17831791
* of_update_property - Update a property in a node, if the property does
17841792
* not exist, add it.
@@ -1801,22 +1809,18 @@ int of_update_property(struct device_node *np, struct property *newprop)
18011809
if (rc)
18021810
return rc;
18031811

1812+
mutex_lock(&of_mutex);
1813+
18041814
raw_spin_lock_irqsave(&devtree_lock, flags);
18051815
rc = __of_update_property(np, newprop, &oldprop);
18061816
raw_spin_unlock_irqrestore(&devtree_lock, flags);
1807-
if (rc)
1808-
return rc;
18091817

1810-
/* At early boot, bail out and defer setup to of_init() */
1811-
if (!of_kset)
1812-
return 0;
1818+
if (!rc)
1819+
__of_update_property_sysfs(np, newprop, oldprop);
18131820

1814-
/* Update the sysfs attribute */
1815-
if (oldprop)
1816-
sysfs_remove_bin_file(&np->kobj, &oldprop->attr);
1817-
__of_add_property_sysfs(np, newprop);
1821+
mutex_unlock(&of_mutex);
18181822

1819-
return 0;
1823+
return rc;
18201824
}
18211825

18221826
static void of_alias_add(struct alias_prop *ap, struct device_node *np,

‎drivers/of/dynamic.c‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ void of_node_put(struct device_node *node)
4141
}
4242
EXPORT_SYMBOL(of_node_put);
4343

44-
static void of_node_remove(struct device_node *np)
44+
void __of_detach_node_sysfs(struct device_node *np)
4545
{
4646
struct property *pp;
4747

4848
BUG_ON(!of_node_is_initialized(np));
49+
if (!of_kset)
50+
return;
4951

5052
/* only remove properties if on sysfs */
5153
if (of_node_is_attached(np)) {
@@ -115,11 +117,13 @@ int of_attach_node(struct device_node *np)
115117
if (rc)
116118
return rc;
117119

120+
mutex_lock(&of_mutex);
118121
raw_spin_lock_irqsave(&devtree_lock, flags);
119122
__of_attach_node(np);
120123
raw_spin_unlock_irqrestore(&devtree_lock, flags);
121124

122-
of_node_add(np);
125+
__of_attach_node_sysfs(np);
126+
mutex_unlock(&of_mutex);
123127
return 0;
124128
}
125129

@@ -174,11 +178,13 @@ int of_detach_node(struct device_node *np)
174178
if (rc)
175179
return rc;
176180

181+
mutex_lock(&of_mutex);
177182
raw_spin_lock_irqsave(&devtree_lock, flags);
178183
__of_detach_node(np);
179184
raw_spin_unlock_irqrestore(&devtree_lock, flags);
180185

181-
of_node_remove(np);
186+
__of_detach_node_sysfs(np);
187+
mutex_unlock(&of_mutex);
182188
return rc;
183189
}
184190

‎drivers/of/of_private.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct alias_prop {
3333

3434
extern struct mutex of_mutex;
3535
extern struct list_head aliases_lookup;
36+
extern struct kset *of_kset;
37+
3638

3739
static inline struct device_node *kobj_to_device_node(struct kobject *kobj)
3840
{
@@ -62,11 +64,19 @@ struct property *__of_prop_dup(const struct property *prop, gfp_t allocflags);
6264
struct device_node *__of_node_alloc(const char *full_name, gfp_t allocflags);
6365

6466
extern int __of_add_property(struct device_node *np, struct property *prop);
67+
extern int __of_add_property_sysfs(struct device_node *np,
68+
struct property *prop);
6569
extern int __of_remove_property(struct device_node *np, struct property *prop);
70+
extern void __of_remove_property_sysfs(struct device_node *np,
71+
struct property *prop);
6672
extern int __of_update_property(struct device_node *np,
6773
struct property *newprop, struct property **oldprop);
74+
extern void __of_update_property_sysfs(struct device_node *np,
75+
struct property *newprop, struct property *oldprop);
6876

6977
extern void __of_attach_node(struct device_node *np);
78+
extern int __of_attach_node_sysfs(struct device_node *np);
7079
extern void __of_detach_node(struct device_node *np);
80+
extern void __of_detach_node_sysfs(struct device_node *np);
7181

7282
#endif /* _LINUX_OF_PRIVATE_H */

‎include/linux/of.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ struct of_phandle_args {
7474
uint32_t args[MAX_PHANDLE_ARGS];
7575
};
7676

77-
extern int of_node_add(struct device_node *node);
78-
7977
/* initialize a node */
8078
extern struct kobj_type of_node_ktype;
8179
static inline void of_node_init(struct device_node *node)

0 commit comments

Comments
 (0)