Skip to content

Commit 1b5b164

Browse files
James-A-ClarkSuzuki K Poulose
authored andcommitted
coresight: Fix CTI module refcount leak by making it a helper device
The CTI module has some hard coded refcounting code that has a leak. For example running perf and then trying to unload it fails: perf record -e cs_etm// -a -- ls rmmod coresight_cti rmmod: ERROR: Module coresight_cti is in use The coresight core already handles references of devices in use, so by making CTI a normal helper device, we get working refcounting for free. Reviewed-by: Mike Leach <mike.leach@linaro.org> Signed-off-by: James Clark <james.clark@arm.com> Signed-off-by: Suzuki K Poulose <suzuki.poulose@arm.com> Link: https://lore.kernel.org/r/20230425143542.2305069-14-james.clark@arm.com
1 parent 6148652 commit 1b5b164

7 files changed

Lines changed: 81 additions & 132 deletions

File tree

drivers/hwtracing/coresight/coresight-core.c

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright (c) 2012, The Linux Foundation. All rights reserved.
44
*/
55

6+
#include <linux/build_bug.h>
67
#include <linux/kernel.h>
78
#include <linux/init.h>
89
#include <linux/types.h>
@@ -236,60 +237,44 @@ void coresight_disclaim_device(struct coresight_device *csdev)
236237
}
237238
EXPORT_SYMBOL_GPL(coresight_disclaim_device);
238239

239-
/* enable or disable an associated CTI device of the supplied CS device */
240-
static int
241-
coresight_control_assoc_ectdev(struct coresight_device *csdev, bool enable)
240+
/*
241+
* Add a helper as an output device. This function takes the @coresight_mutex
242+
* because it's assumed that it's called from the helper device, outside of the
243+
* core code where the mutex would already be held. Don't add new calls to this
244+
* from inside the core code, instead try to add the new helper to the DT and
245+
* ACPI where it will be picked up and linked automatically.
246+
*/
247+
void coresight_add_helper(struct coresight_device *csdev,
248+
struct coresight_device *helper)
242249
{
243-
int ect_ret = 0;
244-
struct coresight_device *ect_csdev = csdev->ect_dev;
245-
struct module *mod;
250+
int i;
251+
struct coresight_connection conn = {};
252+
struct coresight_connection *new_conn;
246253

247-
if (!ect_csdev)
248-
return 0;
249-
if ((!ect_ops(ect_csdev)->enable) || (!ect_ops(ect_csdev)->disable))
250-
return 0;
254+
mutex_lock(&coresight_mutex);
255+
conn.dest_fwnode = fwnode_handle_get(dev_fwnode(&helper->dev));
256+
conn.dest_dev = helper;
257+
conn.dest_port = conn.src_port = -1;
258+
conn.src_dev = csdev;
251259

252-
mod = ect_csdev->dev.parent->driver->owner;
253-
if (enable) {
254-
if (try_module_get(mod)) {
255-
ect_ret = ect_ops(ect_csdev)->enable(ect_csdev);
256-
if (ect_ret) {
257-
module_put(mod);
258-
} else {
259-
get_device(ect_csdev->dev.parent);
260-
csdev->ect_enabled = true;
261-
}
262-
} else
263-
ect_ret = -ENODEV;
264-
} else {
265-
if (csdev->ect_enabled) {
266-
ect_ret = ect_ops(ect_csdev)->disable(ect_csdev);
267-
put_device(ect_csdev->dev.parent);
268-
module_put(mod);
269-
csdev->ect_enabled = false;
270-
}
271-
}
260+
/*
261+
* Check for duplicates because this is called every time a helper
262+
* device is re-loaded. Existing connections will get re-linked
263+
* automatically.
264+
*/
265+
for (i = 0; i < csdev->pdata->nr_outconns; ++i)
266+
if (csdev->pdata->out_conns[i]->dest_fwnode == conn.dest_fwnode)
267+
goto unlock;
272268

273-
/* output warning if ECT enable is preventing trace operation */
274-
if (ect_ret)
275-
dev_info(&csdev->dev, "Associated ECT device (%s) %s failed\n",
276-
dev_name(&ect_csdev->dev),
277-
enable ? "enable" : "disable");
278-
return ect_ret;
279-
}
269+
new_conn = coresight_add_out_conn(csdev->dev.parent, csdev->pdata,
270+
&conn);
271+
if (!IS_ERR(new_conn))
272+
coresight_add_in_conn(new_conn);
280273

281-
/*
282-
* Set the associated ect / cti device while holding the coresight_mutex
283-
* to avoid a race with coresight_enable that may try to use this value.
284-
*/
285-
void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
286-
struct coresight_device *ect_csdev)
287-
{
288-
mutex_lock(&coresight_mutex);
289-
csdev->ect_dev = ect_csdev;
274+
unlock:
290275
mutex_unlock(&coresight_mutex);
291276
}
292-
EXPORT_SYMBOL_GPL(coresight_set_assoc_ectdev_mutex);
277+
EXPORT_SYMBOL_GPL(coresight_add_helper);
293278

294279
static int coresight_enable_sink(struct coresight_device *csdev,
295280
enum cs_mode mode, void *data)
@@ -303,14 +288,10 @@ static int coresight_enable_sink(struct coresight_device *csdev,
303288
if (!sink_ops(csdev)->enable)
304289
return -EINVAL;
305290

306-
ret = coresight_control_assoc_ectdev(csdev, true);
307-
if (ret)
308-
return ret;
309291
ret = sink_ops(csdev)->enable(csdev, mode, data);
310-
if (ret) {
311-
coresight_control_assoc_ectdev(csdev, false);
292+
if (ret)
312293
return ret;
313-
}
294+
314295
csdev->enable = true;
315296

316297
return 0;
@@ -326,7 +307,6 @@ static void coresight_disable_sink(struct coresight_device *csdev)
326307
ret = sink_ops(csdev)->disable(csdev);
327308
if (ret)
328309
return;
329-
coresight_control_assoc_ectdev(csdev, false);
330310
csdev->enable = false;
331311
}
332312

@@ -351,17 +331,11 @@ static int coresight_enable_link(struct coresight_device *csdev,
351331
return PTR_ERR(outconn);
352332

353333
if (link_ops(csdev)->enable) {
354-
ret = coresight_control_assoc_ectdev(csdev, true);
355-
if (!ret) {
356-
ret = link_ops(csdev)->enable(csdev, inconn, outconn);
357-
if (ret)
358-
coresight_control_assoc_ectdev(csdev, false);
359-
}
334+
ret = link_ops(csdev)->enable(csdev, inconn, outconn);
335+
if (!ret)
336+
csdev->enable = true;
360337
}
361338

362-
if (!ret)
363-
csdev->enable = true;
364-
365339
return ret;
366340
}
367341

@@ -382,7 +356,6 @@ static void coresight_disable_link(struct coresight_device *csdev,
382356

383357
if (link_ops(csdev)->disable) {
384358
link_ops(csdev)->disable(csdev, inconn, outconn);
385-
coresight_control_assoc_ectdev(csdev, false);
386359
}
387360

388361
if (link_subtype == CORESIGHT_DEV_SUBTYPE_LINK_MERG) {
@@ -410,14 +383,9 @@ int coresight_enable_source(struct coresight_device *csdev, enum cs_mode mode,
410383

411384
if (!csdev->enable) {
412385
if (source_ops(csdev)->enable) {
413-
ret = coresight_control_assoc_ectdev(csdev, true);
414-
if (ret)
415-
return ret;
416386
ret = source_ops(csdev)->enable(csdev, data, mode);
417-
if (ret) {
418-
coresight_control_assoc_ectdev(csdev, false);
387+
if (ret)
419388
return ret;
420-
}
421389
}
422390
csdev->enable = true;
423391
}
@@ -488,7 +456,6 @@ bool coresight_disable_source(struct coresight_device *csdev, void *data)
488456
if (atomic_dec_return(&csdev->refcnt) == 0) {
489457
if (source_ops(csdev)->disable)
490458
source_ops(csdev)->disable(csdev, data);
491-
coresight_control_assoc_ectdev(csdev, false);
492459
coresight_disable_helpers(csdev);
493460
csdev->enable = false;
494461
}
@@ -1360,11 +1327,10 @@ static struct device_type coresight_dev_type[] = {
13601327
},
13611328
{
13621329
.name = "helper",
1363-
},
1364-
{
1365-
.name = "ect",
1366-
},
1330+
}
13671331
};
1332+
/* Ensure the enum matches the names and groups */
1333+
static_assert(ARRAY_SIZE(coresight_dev_type) == CORESIGHT_DEV_TYPE_MAX);
13681334

13691335
static void coresight_device_release(struct device *dev)
13701336
{

drivers/hwtracing/coresight/coresight-cti-core.c

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,10 @@ static void cti_add_assoc_to_csdev(struct coresight_device *csdev)
555555
mutex_lock(&ect_mutex);
556556

557557
/* exit if current is an ECT device.*/
558-
if ((csdev->type == CORESIGHT_DEV_TYPE_ECT) || list_empty(&ect_net))
558+
if ((csdev->type == CORESIGHT_DEV_TYPE_HELPER &&
559+
csdev->subtype.helper_subtype ==
560+
CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI) ||
561+
list_empty(&ect_net))
559562
goto cti_add_done;
560563

561564
/* if we didn't find the csdev previously we used the fwnode name */
@@ -571,8 +574,7 @@ static void cti_add_assoc_to_csdev(struct coresight_device *csdev)
571574
* if we found a matching csdev then update the ECT
572575
* association pointer for the device with this CTI.
573576
*/
574-
coresight_set_assoc_ectdev_mutex(csdev,
575-
ect_item->csdev);
577+
coresight_add_helper(csdev, ect_item->csdev);
576578
break;
577579
}
578580
}
@@ -582,26 +584,30 @@ static void cti_add_assoc_to_csdev(struct coresight_device *csdev)
582584

583585
/*
584586
* Removing the associated devices is easier.
585-
* A CTI will not have a value for csdev->ect_dev.
586587
*/
587588
static void cti_remove_assoc_from_csdev(struct coresight_device *csdev)
588589
{
589590
struct cti_drvdata *ctidrv;
590591
struct cti_trig_con *tc;
591592
struct cti_device *ctidev;
593+
union coresight_dev_subtype cti_subtype = {
594+
.helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI
595+
};
596+
struct coresight_device *cti_csdev = coresight_find_output_type(
597+
csdev->pdata, CORESIGHT_DEV_TYPE_HELPER, cti_subtype);
598+
599+
if (!cti_csdev)
600+
return;
592601

593602
mutex_lock(&ect_mutex);
594-
if (csdev->ect_dev) {
595-
ctidrv = csdev_to_cti_drvdata(csdev->ect_dev);
596-
ctidev = &ctidrv->ctidev;
597-
list_for_each_entry(tc, &ctidev->trig_cons, node) {
598-
if (tc->con_dev == csdev) {
599-
cti_remove_sysfs_link(ctidrv, tc);
600-
tc->con_dev = NULL;
601-
break;
602-
}
603+
ctidrv = csdev_to_cti_drvdata(cti_csdev);
604+
ctidev = &ctidrv->ctidev;
605+
list_for_each_entry(tc, &ctidev->trig_cons, node) {
606+
if (tc->con_dev == csdev) {
607+
cti_remove_sysfs_link(ctidrv, tc);
608+
tc->con_dev = NULL;
609+
break;
603610
}
604-
csdev->ect_dev = NULL;
605611
}
606612
mutex_unlock(&ect_mutex);
607613
}
@@ -630,8 +636,8 @@ static void cti_update_conn_xrefs(struct cti_drvdata *drvdata)
630636
/* if we can set the sysfs link */
631637
if (cti_add_sysfs_link(drvdata, tc))
632638
/* set the CTI/csdev association */
633-
coresight_set_assoc_ectdev_mutex(tc->con_dev,
634-
drvdata->csdev);
639+
coresight_add_helper(tc->con_dev,
640+
drvdata->csdev);
635641
else
636642
/* otherwise remove reference from CTI */
637643
tc->con_dev = NULL;
@@ -646,8 +652,6 @@ static void cti_remove_conn_xrefs(struct cti_drvdata *drvdata)
646652

647653
list_for_each_entry(tc, &ctidev->trig_cons, node) {
648654
if (tc->con_dev) {
649-
coresight_set_assoc_ectdev_mutex(tc->con_dev,
650-
NULL);
651655
cti_remove_sysfs_link(drvdata, tc);
652656
tc->con_dev = NULL;
653657
}
@@ -795,27 +799,27 @@ static void cti_pm_release(struct cti_drvdata *drvdata)
795799
}
796800

797801
/** cti ect operations **/
798-
int cti_enable(struct coresight_device *csdev)
802+
int cti_enable(struct coresight_device *csdev, enum cs_mode mode, void *data)
799803
{
800804
struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
801805

802806
return cti_enable_hw(drvdata);
803807
}
804808

805-
int cti_disable(struct coresight_device *csdev)
809+
int cti_disable(struct coresight_device *csdev, void *data)
806810
{
807811
struct cti_drvdata *drvdata = csdev_to_cti_drvdata(csdev);
808812

809813
return cti_disable_hw(drvdata);
810814
}
811815

812-
static const struct coresight_ops_ect cti_ops_ect = {
816+
static const struct coresight_ops_helper cti_ops_ect = {
813817
.enable = cti_enable,
814818
.disable = cti_disable,
815819
};
816820

817821
static const struct coresight_ops cti_ops = {
818-
.ect_ops = &cti_ops_ect,
822+
.helper_ops = &cti_ops_ect,
819823
};
820824

821825
/*
@@ -922,8 +926,8 @@ static int cti_probe(struct amba_device *adev, const struct amba_id *id)
922926

923927
/* set up coresight component description */
924928
cti_desc.pdata = pdata;
925-
cti_desc.type = CORESIGHT_DEV_TYPE_ECT;
926-
cti_desc.subtype.ect_subtype = CORESIGHT_DEV_SUBTYPE_ECT_CTI;
929+
cti_desc.type = CORESIGHT_DEV_TYPE_HELPER;
930+
cti_desc.subtype.helper_subtype = CORESIGHT_DEV_SUBTYPE_HELPER_ECT_CTI;
927931
cti_desc.ops = &cti_ops;
928932
cti_desc.groups = drvdata->ctidev.con_groups;
929933
cti_desc.dev = dev;

drivers/hwtracing/coresight/coresight-cti-sysfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ static ssize_t enable_store(struct device *dev,
112112
ret = pm_runtime_resume_and_get(dev->parent);
113113
if (ret)
114114
return ret;
115-
ret = cti_enable(drvdata->csdev);
115+
ret = cti_enable(drvdata->csdev, CS_MODE_SYSFS, NULL);
116116
if (ret)
117117
pm_runtime_put(dev->parent);
118118
} else {
119-
ret = cti_disable(drvdata->csdev);
119+
ret = cti_disable(drvdata->csdev, NULL);
120120
if (!ret)
121121
pm_runtime_put(dev->parent);
122122
}

drivers/hwtracing/coresight/coresight-cti.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ int cti_add_connection_entry(struct device *dev, struct cti_drvdata *drvdata,
215215
const char *assoc_dev_name);
216216
struct cti_trig_con *cti_allocate_trig_con(struct device *dev, int in_sigs,
217217
int out_sigs);
218-
int cti_enable(struct coresight_device *csdev);
219-
int cti_disable(struct coresight_device *csdev);
218+
int cti_enable(struct coresight_device *csdev, enum cs_mode mode, void *data);
219+
int cti_disable(struct coresight_device *csdev, void *data);
220220
void cti_write_all_hw_regs(struct cti_drvdata *drvdata);
221221
void cti_write_intack(struct device *dev, u32 ackval);
222222
void cti_write_single_reg(struct cti_drvdata *drvdata, int offset, u32 value);

drivers/hwtracing/coresight/coresight-priv.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ void coresight_release_platform_data(struct coresight_device *csdev,
211211
struct coresight_platform_data *pdata);
212212
struct coresight_device *
213213
coresight_find_csdev_by_fwnode(struct fwnode_handle *r_fwnode);
214-
void coresight_set_assoc_ectdev_mutex(struct coresight_device *csdev,
215-
struct coresight_device *ect_csdev);
214+
void coresight_add_helper(struct coresight_device *csdev,
215+
struct coresight_device *helper);
216216

217217
void coresight_set_percpu_sink(int cpu, struct coresight_device *csdev);
218218
struct coresight_device *coresight_get_percpu_sink(int cpu);

drivers/hwtracing/coresight/coresight-sysfs.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ int coresight_make_links(struct coresight_device *orig,
148148
char *outs = NULL, *ins = NULL;
149149
struct coresight_sysfs_link *link = NULL;
150150

151+
/* Helper devices aren't shown in sysfs */
152+
if (conn->dest_port == -1 && conn->src_port == -1)
153+
return 0;
154+
151155
do {
152156
outs = devm_kasprintf(&orig->dev, GFP_KERNEL,
153157
"out:%d", conn->src_port);

0 commit comments

Comments
 (0)