Skip to content

Commit ce657b1

Browse files
author
Russell King
committed
component: add support for releasing match data
The component helper treats the void match data pointer as an opaque object which needs no further management. When device nodes being passed, this is not true: the caller should pass its refcount to the component helper, and there should be a way to drop the refcount when the matching information is destroyed. This patch provides a per-match release method in addition to the match method to solve this issue. Rather than using component_match_add(), users should use component_match_add_release() which takes an additional function pointer for releasing this reference. Signed-off-by: Russell King <rmk+kernel@arm.linux.org.uk>
1 parent ffc30b7 commit ce657b1

2 files changed

Lines changed: 87 additions & 42 deletions

File tree

‎drivers/base/component.c‎

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,18 @@
2020

2121
struct component;
2222

23+
struct component_match_array {
24+
void *data;
25+
int (*compare)(struct device *, void *);
26+
void (*release)(struct device *, void *);
27+
struct component *component;
28+
bool duplicate;
29+
};
30+
2331
struct component_match {
2432
size_t alloc;
2533
size_t num;
26-
struct {
27-
void *data;
28-
int (*fn)(struct device *, void *);
29-
struct component *component;
30-
bool duplicate;
31-
} compare[0];
34+
struct component_match_array *compare;
3235
};
3336

3437
struct master {
@@ -92,15 +95,15 @@ static int find_components(struct master *master)
9295
* any components which are found to this master.
9396
*/
9497
for (i = 0; i < match->num; i++) {
98+
struct component_match_array *mc = &match->compare[i];
9599
struct component *c;
96100

97101
dev_dbg(master->dev, "Looking for component %zu\n", i);
98102

99103
if (match->compare[i].component)
100104
continue;
101105

102-
c = find_component(master, match->compare[i].fn,
103-
match->compare[i].data);
106+
c = find_component(master, mc->compare, mc->data);
104107
if (!c) {
105108
ret = -ENXIO;
106109
break;
@@ -192,65 +195,93 @@ static void take_down_master(struct master *master)
192195
}
193196
}
194197

195-
static size_t component_match_size(size_t num)
198+
static void component_match_release(struct device *master,
199+
struct component_match *match)
200+
{
201+
unsigned int i;
202+
203+
for (i = 0; i < match->num; i++) {
204+
struct component_match_array *mc = &match->compare[i];
205+
206+
if (mc->release)
207+
mc->release(master, mc->data);
208+
}
209+
}
210+
211+
static void devm_component_match_release(struct device *dev, void *res)
196212
{
197-
return offsetof(struct component_match, compare[num]);
213+
component_match_release(dev, res);
198214
}
199215

200-
static struct component_match *component_match_realloc(struct device *dev,
216+
static int component_match_realloc(struct device *dev,
201217
struct component_match *match, size_t num)
202218
{
203-
struct component_match *new;
219+
struct component_match_array *new;
204220

205-
if (match && match->alloc == num)
206-
return match;
221+
if (match->alloc == num)
222+
return 0;
207223

208-
new = devm_kmalloc(dev, component_match_size(num), GFP_KERNEL);
224+
new = devm_kmalloc_array(dev, num, sizeof(*new), GFP_KERNEL);
209225
if (!new)
210-
return ERR_PTR(-ENOMEM);
226+
return -ENOMEM;
211227

212-
if (match) {
213-
memcpy(new, match, component_match_size(min(match->num, num)));
214-
devm_kfree(dev, match);
215-
} else {
216-
new->num = 0;
228+
if (match->compare) {
229+
memcpy(new, match->compare, sizeof(*new) *
230+
min(match->num, num));
231+
devm_kfree(dev, match->compare);
217232
}
233+
match->compare = new;
234+
match->alloc = num;
218235

219-
new->alloc = num;
220-
221-
return new;
236+
return 0;
222237
}
223238

224239
/*
225-
* Add a component to be matched.
240+
* Add a component to be matched, with a release function.
226241
*
227242
* The match array is first created or extended if necessary.
228243
*/
229-
void component_match_add(struct device *dev, struct component_match **matchptr,
244+
void component_match_add_release(struct device *master,
245+
struct component_match **matchptr,
246+
void (*release)(struct device *, void *),
230247
int (*compare)(struct device *, void *), void *compare_data)
231248
{
232249
struct component_match *match = *matchptr;
233250

234251
if (IS_ERR(match))
235252
return;
236253

237-
if (!match || match->num == match->alloc) {
238-
size_t new_size = match ? match->alloc + 16 : 15;
254+
if (!match) {
255+
match = devres_alloc(devm_component_match_release,
256+
sizeof(*match), GFP_KERNEL);
257+
if (!match) {
258+
*matchptr = ERR_PTR(-ENOMEM);
259+
return;
260+
}
239261

240-
match = component_match_realloc(dev, match, new_size);
262+
devres_add(master, match);
241263

242264
*matchptr = match;
265+
}
266+
267+
if (match->num == match->alloc) {
268+
size_t new_size = match ? match->alloc + 16 : 15;
269+
int ret;
243270

244-
if (IS_ERR(match))
271+
ret = component_match_realloc(master, match, new_size);
272+
if (ret) {
273+
*matchptr = ERR_PTR(ret);
245274
return;
275+
}
246276
}
247277

248-
match->compare[match->num].fn = compare;
278+
match->compare[match->num].compare = compare;
279+
match->compare[match->num].release = release;
249280
match->compare[match->num].data = compare_data;
250281
match->compare[match->num].component = NULL;
251282
match->num++;
252283
}
253-
EXPORT_SYMBOL(component_match_add);
284+
EXPORT_SYMBOL(component_match_add_release);
254285

255286
int component_master_add_with_match(struct device *dev,
256287
const struct component_master_ops *ops,
@@ -260,9 +291,9 @@ int component_master_add_with_match(struct device *dev,
260291
int ret;
261292

262293
/* Reallocate the match array for its true size */
263-
match = component_match_realloc(dev, match, match->num);
264-
if (IS_ERR(match))
265-
return PTR_ERR(match);
294+
ret = component_match_realloc(dev, match, match->num);
295+
if (ret)
296+
return ret;
266297

267298
master = kzalloc(sizeof(*master), GFP_KERNEL);
268299
if (!master)

‎include/linux/component.h‎

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
#ifndef COMPONENT_H
22
#define COMPONENT_H
33

4+
#include <linux/stddef.h>
5+
46
struct device;
57

68
struct component_ops {
7-
int (*bind)(struct device *, struct device *, void *);
8-
void (*unbind)(struct device *, struct device *, void *);
9+
int (*bind)(struct device *comp, struct device *master,
10+
void *master_data);
11+
void (*unbind)(struct device *comp, struct device *master,
12+
void *master_data);
913
};
1014

1115
int component_add(struct device *, const struct component_ops *);
1216
void component_del(struct device *, const struct component_ops *);
1317

14-
int component_bind_all(struct device *, void *);
15-
void component_unbind_all(struct device *, void *);
18+
int component_bind_all(struct device *master, void *master_data);
19+
void component_unbind_all(struct device *master, void *master_data);
1620

1721
struct master;
1822

1923
struct component_master_ops {
20-
int (*bind)(struct device *);
21-
void (*unbind)(struct device *);
24+
int (*bind)(struct device *master);
25+
void (*unbind)(struct device *master);
2226
};
2327

2428
void component_master_del(struct device *,
@@ -28,7 +32,17 @@ struct component_match;
2832

2933
int component_master_add_with_match(struct device *,
3034
const struct component_master_ops *, struct component_match *);
31-
void component_match_add(struct device *, struct component_match **,
35+
void component_match_add_release(struct device *master,
36+
struct component_match **matchptr,
37+
void (*release)(struct device *, void *),
3238
int (*compare)(struct device *, void *), void *compare_data);
3339

40+
static inline void component_match_add(struct device *master,
41+
struct component_match **matchptr,
42+
int (*compare)(struct device *, void *), void *compare_data)
43+
{
44+
component_match_add_release(master, matchptr, NULL, compare,
45+
compare_data);
46+
}
47+
3448
#endif

0 commit comments

Comments
 (0)