Skip to content

Commit ffc30b7

Browse files
author
Russell King
committed
component: track components via array rather than list
Since we now have an array which defines each component, maintain the components to be bound in the array rather than a separate list. We also need duplicate tracking so we can eliminate multiple bind calls for the same component: we preserve the list-based component order in that the first match which adds the component determines its position. Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent 29f1c7f commit ffc30b7

1 file changed

Lines changed: 80 additions & 74 deletions

File tree

‎drivers/base/component.c‎

Lines changed: 80 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@
1818
#include <linux/mutex.h>
1919
#include <linux/slab.h>
2020

21+
struct component;
22+
2123
struct component_match {
2224
size_t alloc;
2325
size_t num;
2426
struct {
2527
void *data;
2628
int (*fn)(struct device *, void *);
29+
struct component *component;
30+
bool duplicate;
2731
} compare[0];
2832
};
2933

3034
struct master {
3135
struct list_head node;
32-
struct list_head components;
3336
bool bound;
3437

3538
const struct component_master_ops *ops;
@@ -39,7 +42,6 @@ struct master {
3942

4043
struct component {
4144
struct list_head node;
42-
struct list_head master_node;
4345
struct master *master;
4446
bool bound;
4547

@@ -63,46 +65,20 @@ static struct master *__master_find(struct device *dev,
6365
return NULL;
6466
}
6567

66-
/* Attach an unattached component to a master. */
67-
static void component_attach_master(struct master *master, struct component *c)
68-
{
69-
c->master = master;
70-
71-
list_add_tail(&c->master_node, &master->components);
72-
}
73-
74-
/* Detach a component from a master. */
75-
static void component_detach_master(struct master *master, struct component *c)
76-
{
77-
list_del(&c->master_node);
78-
79-
c->master = NULL;
80-
}
81-
82-
/*
83-
* Add a component to a master, finding the component via the compare
84-
* function and compare data. This is safe to call for duplicate matches
85-
* and will not result in the same component being added multiple times.
86-
*/
87-
static int component_master_add_child(struct master *master,
68+
static struct component *find_component(struct master *master,
8869
int (*compare)(struct device *, void *), void *compare_data)
8970
{
9071
struct component *c;
91-
int ret = -ENXIO;
9272

9373
list_for_each_entry(c, &component_list, node) {
9474
if (c->master && c->master != master)
9575
continue;
9676

97-
if (compare(c->dev, compare_data)) {
98-
if (!c->master)
99-
component_attach_master(master, c);
100-
ret = 0;
101-
break;
102-
}
77+
if (compare(c->dev, compare_data))
78+
return c;
10379
}
10480

105-
return ret;
81+
return NULL;
10682
}
10783

10884
static int find_components(struct master *master)
@@ -116,26 +92,39 @@ static int find_components(struct master *master)
11692
* any components which are found to this master.
11793
*/
11894
for (i = 0; i < match->num; i++) {
119-
ret = component_master_add_child(master,
120-
match->compare[i].fn,
121-
match->compare[i].data);
122-
if (ret)
95+
struct component *c;
96+
97+
dev_dbg(master->dev, "Looking for component %zu\n", i);
98+
99+
if (match->compare[i].component)
100+
continue;
101+
102+
c = find_component(master, match->compare[i].fn,
103+
match->compare[i].data);
104+
if (!c) {
105+
ret = -ENXIO;
123106
break;
107+
}
108+
109+
dev_dbg(master->dev, "found component %s, duplicate %u\n", dev_name(c->dev), !!c->master);
110+
111+
/* Attach this component to the master */
112+
match->compare[i].duplicate = !!c->master;
113+
match->compare[i].component = c;
114+
c->master = master;
124115
}
125116
return ret;
126117
}
127118

128-
/* Detach all attached components from this master */
129-
static void master_remove_components(struct master *master)
119+
/* Detach component from associated master */
120+
static void remove_component(struct master *master, struct component *c)
130121
{
131-
while (!list_empty(&master->components)) {
132-
struct component *c = list_first_entry(&master->components,
133-
struct component, master_node);
134-
135-
WARN_ON(c->master != master);
122+
size_t i;
136123

137-
component_detach_master(master, c);
138-
}
124+
/* Detach the component from this master. */
125+
for (i = 0; i < master->match->num; i++)
126+
if (master->match->compare[i].component == c)
127+
master->match->compare[i].component = NULL;
139128
}
140129

141130
/*
@@ -150,37 +139,32 @@ static int try_to_bring_up_master(struct master *master,
150139
{
151140
int ret;
152141

142+
dev_dbg(master->dev, "trying to bring up master\n");
143+
153144
if (find_components(master)) {
154-
/* Failed to find all components */
155-
ret = 0;
156-
goto out;
145+
dev_dbg(master->dev, "master has incomplete components\n");
146+
return 0;
157147
}
158148

159149
if (component && component->master != master) {
160-
ret = 0;
161-
goto out;
150+
dev_dbg(master->dev, "master is not for this component (%s)\n",
151+
dev_name(component->dev));
152+
return 0;
162153
}
163154

164-
if (!devres_open_group(master->dev, NULL, GFP_KERNEL)) {
165-
ret = -ENOMEM;
166-
goto out;
167-
}
155+
if (!devres_open_group(master->dev, NULL, GFP_KERNEL))
156+
return -ENOMEM;
168157

169158
/* Found all components */
170159
ret = master->ops->bind(master->dev);
171160
if (ret < 0) {
172161
devres_release_group(master->dev, NULL);
173162
dev_info(master->dev, "master bind failed: %d\n", ret);
174-
goto out;
163+
return ret;
175164
}
176165

177166
master->bound = true;
178167
return 1;
179-
180-
out:
181-
master_remove_components(master);
182-
183-
return ret;
184168
}
185169

186170
static int try_to_bring_up_masters(struct component *component)
@@ -206,8 +190,6 @@ static void take_down_master(struct master *master)
206190
devres_release_group(master->dev, NULL);
207191
master->bound = false;
208192
}
209-
210-
master_remove_components(master);
211193
}
212194

213195
static size_t component_match_size(size_t num)
@@ -265,6 +247,7 @@ void component_match_add(struct device *dev, struct component_match **matchptr,
265247

266248
match->compare[match->num].fn = compare;
267249
match->compare[match->num].data = compare_data;
250+
match->compare[match->num].component = NULL;
268251
match->num++;
269252
}
270253
EXPORT_SYMBOL(component_match_add);
@@ -288,7 +271,6 @@ int component_master_add_with_match(struct device *dev,
288271
master->dev = dev;
289272
master->ops = ops;
290273
master->match = match;
291-
INIT_LIST_HEAD(&master->components);
292274

293275
/* Add to the list of available masters. */
294276
mutex_lock(&component_mutex);
@@ -311,13 +293,24 @@ void component_master_del(struct device *dev,
311293
const struct component_master_ops *ops)
312294
{
313295
struct master *master;
296+
int i;
314297

315298
mutex_lock(&component_mutex);
316299
master = __master_find(dev, ops);
317300
if (master) {
301+
struct component_match *match = master->match;
302+
318303
take_down_master(master);
319304

320305
list_del(&master->node);
306+
307+
if (match) {
308+
for (i = 0; i < match->num; i++) {
309+
struct component *c = match->compare[i].component;
310+
if (c)
311+
c->master = NULL;
312+
}
313+
}
321314
kfree(master);
322315
}
323316
mutex_unlock(&component_mutex);
@@ -340,15 +333,20 @@ void component_unbind_all(struct device *master_dev, void *data)
340333
{
341334
struct master *master;
342335
struct component *c;
336+
size_t i;
343337

344338
WARN_ON(!mutex_is_locked(&component_mutex));
345339

346340
master = __master_find(master_dev, NULL);
347341
if (!master)
348342
return;
349343

350-
list_for_each_entry_reverse(c, &master->components, master_node)
351-
component_unbind(c, master, data);
344+
/* Unbind components in reverse order */
345+
for (i = master->match->num; i--; )
346+
if (!master->match->compare[i].duplicate) {
347+
c = master->match->compare[i].component;
348+
component_unbind(c, master, data);
349+
}
352350
}
353351
EXPORT_SYMBOL_GPL(component_unbind_all);
354352

@@ -408,6 +406,7 @@ int component_bind_all(struct device *master_dev, void *data)
408406
{
409407
struct master *master;
410408
struct component *c;
409+
size_t i;
411410
int ret = 0;
412411

413412
WARN_ON(!mutex_is_locked(&component_mutex));
@@ -416,16 +415,21 @@ int component_bind_all(struct device *master_dev, void *data)
416415
if (!master)
417416
return -EINVAL;
418417

419-
list_for_each_entry(c, &master->components, master_node) {
420-
ret = component_bind(c, master, data);
421-
if (ret)
422-
break;
423-
}
418+
/* Bind components in match order */
419+
for (i = 0; i < master->match->num; i++)
420+
if (!master->match->compare[i].duplicate) {
421+
c = master->match->compare[i].component;
422+
ret = component_bind(c, master, data);
423+
if (ret)
424+
break;
425+
}
424426

425427
if (ret != 0) {
426-
list_for_each_entry_continue_reverse(c, &master->components,
427-
master_node)
428-
component_unbind(c, master, data);
428+
for (; i--; )
429+
if (!master->match->compare[i].duplicate) {
430+
c = master->match->compare[i].component;
431+
component_unbind(c, master, data);
432+
}
429433
}
430434

431435
return ret;
@@ -473,8 +477,10 @@ void component_del(struct device *dev, const struct component_ops *ops)
473477
break;
474478
}
475479

476-
if (component && component->master)
480+
if (component && component->master) {
477481
take_down_master(component->master);
482+
remove_component(component->master, component);
483+
}
478484

479485
mutex_unlock(&component_mutex);
480486

0 commit comments

Comments
 (0)