Skip to content

Commit defe49f

Browse files
davejiangvinodkoul
authored andcommitted
dmaengine: idxd: fix group conf_dev lifetime
Remove devm_* allocation and fix group->conf_dev 'struct device' lifetime. Address issues flagged by CONFIG_DEBUG_KOBJECT_RELEASE. Add release functions in order to free the allocated memory at the group->conf_dev destruction time. Reported-by: Jason Gunthorpe <[email protected]> Fixes: bfe1d56 ("dmaengine: idxd: Init and probe for Intel data accelerators") Signed-off-by: Dave Jiang <[email protected]> Link: https://lore.kernel.org/r/161852987144.2203940.8830315575880047.stgit@djiang5-desk3.ch.intel.com Signed-off-by: Vinod Koul <[email protected]>
1 parent 75b9113 commit defe49f

File tree

4 files changed

+88
-59
lines changed

4 files changed

+88
-59
lines changed

drivers/dma/idxd/device.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ static int idxd_groups_config_write(struct idxd_device *idxd)
659659
ioread32(idxd->reg_base + IDXD_GENCFG_OFFSET));
660660

661661
for (i = 0; i < idxd->max_groups; i++) {
662-
struct idxd_group *group = &idxd->groups[i];
662+
struct idxd_group *group = idxd->groups[i];
663663

664664
idxd_group_config_write(group);
665665
}
@@ -754,7 +754,7 @@ static void idxd_group_flags_setup(struct idxd_device *idxd)
754754

755755
/* TC-A 0 and TC-B 1 should be defaults */
756756
for (i = 0; i < idxd->max_groups; i++) {
757-
struct idxd_group *group = &idxd->groups[i];
757+
struct idxd_group *group = idxd->groups[i];
758758

759759
if (group->tc_a == -1)
760760
group->tc_a = group->grpcfg.flags.tc_a = 0;
@@ -781,7 +781,7 @@ static int idxd_engines_setup(struct idxd_device *idxd)
781781
struct idxd_group *group;
782782

783783
for (i = 0; i < idxd->max_groups; i++) {
784-
group = &idxd->groups[i];
784+
group = idxd->groups[i];
785785
group->grpcfg.engines = 0;
786786
}
787787

@@ -810,7 +810,7 @@ static int idxd_wqs_setup(struct idxd_device *idxd)
810810
struct device *dev = &idxd->pdev->dev;
811811

812812
for (i = 0; i < idxd->max_groups; i++) {
813-
group = &idxd->groups[i];
813+
group = idxd->groups[i];
814814
for (j = 0; j < 4; j++)
815815
group->grpcfg.wqs[j] = 0;
816816
}

drivers/dma/idxd/idxd.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ struct idxd_device {
193193

194194
spinlock_t dev_lock; /* spinlock for device */
195195
struct completion *cmd_done;
196-
struct idxd_group *groups;
196+
struct idxd_group **groups;
197197
struct idxd_wq **wqs;
198198
struct idxd_engine **engines;
199199

@@ -260,6 +260,7 @@ extern struct device_type dsa_device_type;
260260
extern struct device_type iax_device_type;
261261
extern struct device_type idxd_wq_device_type;
262262
extern struct device_type idxd_engine_device_type;
263+
extern struct device_type idxd_group_device_type;
263264

264265
static inline bool is_dsa_dev(struct device *dev)
265266
{

drivers/dma/idxd/init.c

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,54 @@ static int idxd_setup_engines(struct idxd_device *idxd)
236236
return rc;
237237
}
238238

239-
static int idxd_setup_internals(struct idxd_device *idxd)
239+
static int idxd_setup_groups(struct idxd_device *idxd)
240240
{
241241
struct device *dev = &idxd->pdev->dev;
242+
struct idxd_group *group;
242243
int i, rc;
243244

245+
idxd->groups = kcalloc_node(idxd->max_groups, sizeof(struct idxd_group *),
246+
GFP_KERNEL, dev_to_node(dev));
247+
if (!idxd->groups)
248+
return -ENOMEM;
249+
250+
for (i = 0; i < idxd->max_groups; i++) {
251+
group = kzalloc_node(sizeof(*group), GFP_KERNEL, dev_to_node(dev));
252+
if (!group) {
253+
rc = -ENOMEM;
254+
goto err;
255+
}
256+
257+
group->id = i;
258+
group->idxd = idxd;
259+
device_initialize(&group->conf_dev);
260+
group->conf_dev.parent = &idxd->conf_dev;
261+
group->conf_dev.bus = idxd_get_bus_type(idxd);
262+
group->conf_dev.type = &idxd_group_device_type;
263+
rc = dev_set_name(&group->conf_dev, "group%d.%d", idxd->id, group->id);
264+
if (rc < 0) {
265+
put_device(&group->conf_dev);
266+
goto err;
267+
}
268+
269+
idxd->groups[i] = group;
270+
group->tc_a = -1;
271+
group->tc_b = -1;
272+
}
273+
274+
return 0;
275+
276+
err:
277+
while (--i >= 0)
278+
put_device(&idxd->groups[i]->conf_dev);
279+
return rc;
280+
}
281+
282+
static int idxd_setup_internals(struct idxd_device *idxd)
283+
{
284+
struct device *dev = &idxd->pdev->dev;
285+
int rc, i;
286+
244287
init_waitqueue_head(&idxd->cmd_waitq);
245288

246289
rc = idxd_setup_wqs(idxd);
@@ -251,29 +294,22 @@ static int idxd_setup_internals(struct idxd_device *idxd)
251294
if (rc < 0)
252295
goto err_engine;
253296

254-
idxd->groups = devm_kcalloc(dev, idxd->max_groups,
255-
sizeof(struct idxd_group), GFP_KERNEL);
256-
if (!idxd->groups) {
257-
rc = -ENOMEM;
258-
goto err;
259-
}
260-
261-
for (i = 0; i < idxd->max_groups; i++) {
262-
idxd->groups[i].idxd = idxd;
263-
idxd->groups[i].id = i;
264-
idxd->groups[i].tc_a = -1;
265-
idxd->groups[i].tc_b = -1;
266-
}
297+
rc = idxd_setup_groups(idxd);
298+
if (rc < 0)
299+
goto err_group;
267300

268301
idxd->wq = create_workqueue(dev_name(dev));
269302
if (!idxd->wq) {
270303
rc = -ENOMEM;
271-
goto err;
304+
goto err_wkq_create;
272305
}
273306

274307
return 0;
275308

276-
err:
309+
err_wkq_create:
310+
for (i = 0; i < idxd->max_groups; i++)
311+
put_device(&idxd->groups[i]->conf_dev);
312+
err_group:
277313
for (i = 0; i < idxd->max_engines; i++)
278314
put_device(&idxd->engines[i]->conf_dev);
279315
err_engine:

drivers/dma/idxd/sysfs.c

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ static char *idxd_wq_type_names[] = {
1616
[IDXD_WQT_USER] = "user",
1717
};
1818

19-
static void idxd_conf_sub_device_release(struct device *dev)
20-
{
21-
dev_dbg(dev, "%s for %s\n", __func__, dev_name(dev));
22-
}
23-
24-
static struct device_type idxd_group_device_type = {
25-
.name = "group",
26-
.release = idxd_conf_sub_device_release,
27-
};
28-
2919
static int idxd_config_bus_match(struct device *dev,
3020
struct device_driver *drv)
3121
{
@@ -435,7 +425,7 @@ static ssize_t engine_group_id_store(struct device *dev,
435425

436426
if (prevg)
437427
prevg->num_engines--;
438-
engine->group = &idxd->groups[id];
428+
engine->group = idxd->groups[id];
439429
engine->group->num_engines++;
440430

441431
return count;
@@ -479,7 +469,7 @@ static void idxd_set_free_tokens(struct idxd_device *idxd)
479469
int i, tokens;
480470

481471
for (i = 0, tokens = 0; i < idxd->max_groups; i++) {
482-
struct idxd_group *g = &idxd->groups[i];
472+
struct idxd_group *g = idxd->groups[i];
483473

484474
tokens += g->tokens_reserved;
485475
}
@@ -784,6 +774,19 @@ static const struct attribute_group *idxd_group_attribute_groups[] = {
784774
NULL,
785775
};
786776

777+
static void idxd_conf_group_release(struct device *dev)
778+
{
779+
struct idxd_group *group = container_of(dev, struct idxd_group, conf_dev);
780+
781+
kfree(group);
782+
}
783+
784+
struct device_type idxd_group_device_type = {
785+
.name = "group",
786+
.release = idxd_conf_group_release,
787+
.groups = idxd_group_attribute_groups,
788+
};
789+
787790
/* IDXD work queue attribs */
788791
static ssize_t wq_clients_show(struct device *dev,
789792
struct device_attribute *attr, char *buf)
@@ -856,7 +859,7 @@ static ssize_t wq_group_id_store(struct device *dev,
856859
return count;
857860
}
858861

859-
group = &idxd->groups[id];
862+
group = idxd->groups[id];
860863
prevg = wq->group;
861864

862865
if (prevg)
@@ -1666,37 +1669,27 @@ static int idxd_register_engine_devices(struct idxd_device *idxd)
16661669
return rc;
16671670
}
16681671

1669-
static int idxd_setup_group_sysfs(struct idxd_device *idxd)
1672+
static int idxd_register_group_devices(struct idxd_device *idxd)
16701673
{
1671-
struct device *dev = &idxd->pdev->dev;
1672-
int i, rc;
1674+
int i, j, rc;
16731675

16741676
for (i = 0; i < idxd->max_groups; i++) {
1675-
struct idxd_group *group = &idxd->groups[i];
1676-
1677-
group->conf_dev.parent = &idxd->conf_dev;
1678-
dev_set_name(&group->conf_dev, "group%d.%d",
1679-
idxd->id, group->id);
1680-
group->conf_dev.bus = idxd_get_bus_type(idxd);
1681-
group->conf_dev.groups = idxd_group_attribute_groups;
1682-
group->conf_dev.type = &idxd_group_device_type;
1683-
dev_dbg(dev, "Group device register: %s\n",
1684-
dev_name(&group->conf_dev));
1685-
rc = device_register(&group->conf_dev);
1686-
if (rc < 0) {
1687-
put_device(&group->conf_dev);
1677+
struct idxd_group *group = idxd->groups[i];
1678+
1679+
rc = device_add(&group->conf_dev);
1680+
if (rc < 0)
16881681
goto cleanup;
1689-
}
16901682
}
16911683

16921684
return 0;
16931685

16941686
cleanup:
1695-
while (i--) {
1696-
struct idxd_group *group = &idxd->groups[i];
1687+
j = i - 1;
1688+
for (; i < idxd->max_groups; i++)
1689+
put_device(&idxd->groups[i]->conf_dev);
16971690

1698-
device_unregister(&group->conf_dev);
1699-
}
1691+
while (j--)
1692+
device_unregister(&idxd->groups[j]->conf_dev);
17001693
return rc;
17011694
}
17021695

@@ -1745,10 +1738,9 @@ int idxd_register_devices(struct idxd_device *idxd)
17451738
goto err_engine;
17461739
}
17471740

1748-
rc = idxd_setup_group_sysfs(idxd);
1741+
rc = idxd_register_group_devices(idxd);
17491742
if (rc < 0) {
1750-
/* unregister conf dev */
1751-
dev_dbg(dev, "Group sysfs registering failed: %d\n", rc);
1743+
dev_dbg(dev, "Group device registering failed: %d\n", rc);
17521744
goto err_group;
17531745
}
17541746

@@ -1782,7 +1774,7 @@ void idxd_unregister_devices(struct idxd_device *idxd)
17821774
}
17831775

17841776
for (i = 0; i < idxd->max_groups; i++) {
1785-
struct idxd_group *group = &idxd->groups[i];
1777+
struct idxd_group *group = idxd->groups[i];
17861778

17871779
device_unregister(&group->conf_dev);
17881780
}

0 commit comments

Comments
 (0)