coresight: Fix device registration and unregistration
Leo Yan
reviewerv4 cover coresight: Fix device registration and unregistration
This series is to fix and refactor CoreSight device registration and unregistration, it can be divided into three small parts:
Patches 01-03: Three fixes for memory leak, device reference and mutex
protection.
Patches 04-05: Move connection cleanup operations into
coresight_remove_conns().
Patches 06-08: Refactor error handling in coresight_register().
This series is verified on Juno board.
---
Changes in v4:
- Applied James' review tags on patches 01-07.
- Kept the "registered" flag to use central place for mutex release
(James / Suzuki).
- Link to v3: https://lore.kernel.org/r/20260202-arm_coresight_refactor_dev_register-v3-0-03292470c48c@arm.com
Changes in v3:
- Updated patch 01 to use coesight core layer for device index list
(Suzuki).
- Link to v2: https://lore.kernel.org/r/20260126-arm_coresight_refactor_dev_register-v2-0-b5a3c0441c15@arm.com
Changes in v2:
- Refined the commit log in patch 06 (Suzuki).
- Unified to call coresight_unregister() for error handling (Suzuki).
- Refactor connection and sysfs group release.
- Link to v1: https://lore.kernel.org/linux-arm-kernel/[email protected]/
To: Suzuki K Poulose <[email protected]>
To: Mike Leach <[email protected]>
To: James Clark <[email protected]>
To: Alexander Shishkin <[email protected]>
To: Greg Kroah-Hartman <[email protected]>
To: Mathieu Poirier <[email protected]>
To: Mao Jinlong <[email protected]>
---
Leo Yan (8):
coresight: Fix memory leak in coresight_alloc_device_name()
coresight: Get parent device reference after sink ID map allocation
coresight: Protect unregistration with mutex
coresight: Refactor output connection sysfs link cleanup
coresight: Refactor sysfs connection group cleanup
coresight: Move sink validation into etm_perf_add_symlink_sink()
coresight: Do not mix success path with failure handling
coresight: Unify bus unregistration via coresight_unregister()
drivers/hwtracing/coresight/coresight-catu.c | 4 +- drivers/hwtracing/coresight/coresight-core.c | 214 +++++++++++++-------- drivers/hwtracing/coresight/coresight-ctcu-core.c | 4 +- drivers/hwtracing/coresight/coresight-cti-core.c | 19 +- drivers/hwtracing/coresight/coresight-dummy.c | 7 +- drivers/hwtracing/coresight/coresight-etb10.c | 4 +- drivers/hwtracing/coresight/coresight-etm-perf.c | 5 +- drivers/hwtracing/coresight/coresight-funnel.c | 4 +- drivers/hwtracing/coresight/coresight-platform.c | 2 +- drivers/hwtracing/coresight/coresight-priv.h | 3 +- drivers/hwtracing/coresight/coresight-replicator.c | 4 +- drivers/hwtracing/coresight/coresight-stm.c | 4 +- drivers/hwtracing/coresight/coresight-tmc-core.c | 12 +- drivers/hwtracing/coresight/coresight-tnoc.c | 4 +- drivers/hwtracing/coresight/coresight-tpda.c | 4 +- drivers/hwtracing/coresight/coresight-tpdm.c | 4 +- drivers/hwtracing/coresight/coresight-tpiu.c | 4 +- drivers/hwtracing/coresight/ultrasoc-smb.c | 4 +- include/linux/coresight.h | 14 +- 19 files changed, 167 insertions(+), 153 deletions(-)
---
base-commit: eebe8dbd8630f51cf70b1f68a440cd3d7f7a914d
change-id: 20260120-arm_coresight_refactor_dev_register-f16c069db41d
Best regards,
Signature · 1 line
Leo Yan <[email protected]>
Leo Yan
reviewerPATCH v4 1/8 coresight: Fix memory leak in coresight_alloc_device_name()
The memory leak detector reports:
echo clear > /sys/kernel/debug/kmemleak
modprobe coresight_funnel
rmmod coresight_funnel
# Scan memory leak and report it
echo scan > /sys/kernel/debug/kmemleak cat /sys/kernel/debug/kmemleak unreferenced object 0xffff0008020c7200 (size 64):
comm "modprobe", pid 410, jiffies 4295333721
hex dump (first 32 bytes):
d8 da fe 7e 09 00 ff ff e8 2e ff 7e 09 00 ff ff ...~.......~....
b0 6c ff 7e 09 00 ff ff 30 83 00 7f 09 00 ff ff .l.~....0.......
backtrace (crc 4116a690):
kmemleak_alloc+0xd8/0xf8
__kmalloc_node_track_caller_noprof+0x2c8/0x6f0
krealloc_node_align_noprof+0x13c/0x2c8
coresight_alloc_device_name+0xe4/0x158 [coresight]
0xffffd327ecef8394
0xffffd327ecef85ec
amba_probe+0x118/0x1c8
really_probe+0xc8/0x3f0
__driver_probe_device+0x88/0x190
driver_probe_device+0x44/0x120
__driver_attach+0x100/0x238
bus_for_each_dev+0x84/0xf0
driver_attach+0x2c/0x40
bus_add_driver+0x128/0x258
driver_register+0x64/0x138
__amba_driver_register+0x2c/0x48The memory leak is caused by not freeing the device list that maintains device indices.
This device list preserves stable device indices across unbind and rebind device operations, so it does not share the same lifetime as a device instances and must only be freed when the module is unloaded.
Some modules do not implement a module exit callback because they are registered using module_platform_driver(). As a result, the device list cannot be released during module exit for those modules.
Fix this by moving the device list into the core layer. As a general solution, instead of maintaining a static list in each driver, drivers now allocate device lists via coresight_allocate_device_list() and device indices via coresight_allocate_device_idx().
The list is released only when the core module is unloaded by calling coresight_release_device_list(), avoiding the leak.
--- drivers/hwtracing/coresight/coresight-catu.c | 4 +- drivers/hwtracing/coresight/coresight-core.c | 127 +++++++++++++++------ drivers/hwtracing/coresight/coresight-ctcu-core.c | 4 +- drivers/hwtracing/coresight/coresight-cti-core.c | 19 ++- drivers/hwtracing/coresight/coresight-dummy.c | 7 +- drivers/hwtracing/coresight/coresight-etb10.c | 4 +- drivers/hwtracing/coresight/coresight-funnel.c | 4 +- drivers/hwtracing/coresight/coresight-replicator.c | 4 +- drivers/hwtracing/coresight/coresight-stm.c | 4 +- drivers/hwtracing/coresight/coresight-tmc-core.c | 12 +- drivers/hwtracing/coresight/coresight-tnoc.c | 4 +- drivers/hwtracing/coresight/coresight-tpda.c | 4 +- drivers/hwtracing/coresight/coresight-tpdm.c | 4 +- drivers/hwtracing/coresight/coresight-tpiu.c | 4 +- drivers/hwtracing/coresight/ultrasoc-smb.c | 4 +- include/linux/coresight.h | 14 +-- 16 files changed, 120 insertions(+), 103 deletions(-)
drivers/hwtracing/coresight/coresight-catu.c +1 -3
@@ -30,8 +30,6 @@#define catu_dbg (x, ...) do {} while (0)#endif -DEFINE_CORESIGHT_DEVLIST (catu_devs,"catu" ); -struct catu_etr_buf {struct tmc_sg_table *catu_table;dma_addr_t sladdr;
@@ -530,7 +528,7 @@ static int __catu_probe(struct device *dev, struct resource *res)if (ret)return ret; - catu_desc.name =coresight_alloc_device_name (&catu_devs, dev); + catu_desc.name =coresight_alloc_device_name ("catu" , dev);if (!catu_desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-core.c +91 -36
170 lines not on this page · raw message
drivers/hwtracing/coresight/coresight-ctcu-core.c +1 -3
@@ -19,8 +19,6 @@#include "coresight-ctcu.h" #include "coresight-priv.h" -DEFINE_CORESIGHT_DEVLIST (ctcu_devs,"ctcu" ); -#define ctcu_writel (drvdata, val, offset) __raw_writel((val), drvdata->base + offset)#define ctcu_readl (drvdata, offset) __raw_readl(drvdata->base + offset)
@@ -187,7 +185,7 @@ static int ctcu_probe(struct platform_device *pdev)void __iomem *base;int i, ret; - desc.name =coresight_alloc_device_name (&ctcu_devs, dev); + desc.name =coresight_alloc_device_name ("ctcu" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-cti-core.c +8 -11
@@ -48,15 +48,6 @@ static int nr_cti_cpu;/* quick lookup list for CPU bound CTIs when power handling */ static struct cti_drvdata *cti_cpu_drvdata[NR_CPUS ]; -/* -* CTI naming. CTI bound to cores will have the name cti_cpu<N> where -* N is the CPU ID. System CTIs will have the name cti_sys<I> where I -* is an index allocated by order of discovery. -* -* CTI device name list - for CTI not bound to cores. -*/ -DEFINE_CORESIGHT_DEVLIST (cti_sys_devs,"cti_sys" ); -/* write set of regs to hardware - call with spinlock claimed */ void cti_write_all_hw_regs (struct cti_drvdata *drvdata) {
@@ -903,12 +894,18 @@ static int cti_probe(struct amba_device *adev, const struct amba_id *id)/* default to powered - could change on PM notifications */ drvdata->config.hw_powered = true; -/* set up device name - will depend if cpu bound or otherwise */ +/* +* Set up device name - will depend if cpu bound or otherwise. +* +* CTI bound to cores will have the name cti_cpu<N> where N is th +* eCPU ID. System CTIs will have the name cti_sys<I> where I is an +* index allocated by order of discovery. +*/ if (drvdata->ctidev.cpu >=0 ) cti_desc.name =devm_kasprintf (dev,GFP_KERNEL ,"cti_cpu%d" , drvdata->ctidev.cpu);else - cti_desc.name =coresight_alloc_device_name (&cti_sys_devs, dev); + cti_desc.name =coresight_alloc_device_name ("cti_sys" , dev);if (!cti_desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-dummy.c +2 -5
@@ -19,9 +19,6 @@ struct dummy_drvdata {u8 traceid; }; -DEFINE_CORESIGHT_DEVLIST (source_devs,"dummy_source" ); -DEFINE_CORESIGHT_DEVLIST (sink_devs,"dummy_sink" ); -static int dummy_source_enable (struct coresight_device *csdev,struct perf_event *event,enum cs_mode mode,__maybe_unused struct coresight_path *path)
@@ -126,7 +123,7 @@ static int dummy_probe(struct platform_device *pdev)if (of_device_is_compatible (node,"arm,coresight-dummy-source" )) { - desc.name =coresight_alloc_device_name (&source_devs, dev); + desc.name =coresight_alloc_device_name ("dummy_source" , dev);if (!desc.name)return -ENOMEM ;
@@ -155,7 +152,7 @@ static int dummy_probe(struct platform_device *pdev) drvdata->traceid = (u8 )trace_id; }else if (of_device_is_compatible (node,"arm,coresight-dummy-sink" )) { - desc.name =coresight_alloc_device_name (&sink_devs, dev); + desc.name =coresight_alloc_device_name ("dummy_sink" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-etb10.c +1 -3
@@ -63,8 +63,6 @@#define ETB_FFSR_BIT 1#define ETB_FRAME_SIZE_WORDS 4 -DEFINE_CORESIGHT_DEVLIST (etb_devs,"etb" ); -/** * struct etb_drvdata - specifics associated to an ETB component * @base: memory mapped base address for this component.
@@ -722,7 +720,7 @@ static int etb_probe(struct amba_device *adev, const struct amba_id *id)struct resource *res = &adev->res;struct coresight_desc desc = {0 }; - desc.name =coresight_alloc_device_name (&etb_devs, dev); + desc.name =coresight_alloc_device_name ("etb" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-funnel.c +1 -3
@@ -30,8 +30,6 @@#define FUNNEL_HOLDTIME (0x7 << FUNNEL_HOLDTIME_SHFT)#define FUNNEL_ENSx_MASK 0xff -DEFINE_CORESIGHT_DEVLIST (funnel_devs,"funnel" ); -/** * struct funnel_drvdata - specifics associated to a funnel component * @base: memory mapped base address for this component.
@@ -223,7 +221,7 @@ static int funnel_probe(struct device *dev, struct resource *res) of_device_is_compatible(dev->of_node, "arm,coresight-funnel"))dev_warn_once (dev,"Uses OBSOLETE CoreSight funnel binding\n" ); - desc.name =coresight_alloc_device_name (&funnel_devs, dev); + desc.name =coresight_alloc_device_name ("funnel" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-replicator.c +1 -3
@@ -24,8 +24,6 @@#define REPLICATOR_IDFILTER0 0x000#define REPLICATOR_IDFILTER1 0x004 -DEFINE_CORESIGHT_DEVLIST (replicator_devs,"replicator" ); -/** * struct replicator_drvdata - specifics associated to a replicator component * @base: memory mapped base address for this component. Also indicates
@@ -230,7 +228,7 @@ static int replicator_probe(struct device *dev, struct resource *res)dev_warn_once (dev,"Uses OBSOLETE CoreSight replicator binding\n" ); - desc.name =coresight_alloc_device_name (&replicator_devs, dev); + desc.name =coresight_alloc_device_name ("replicator" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-stm.c +1 -3
@@ -110,8 +110,6 @@ struct channel_space {unsigned long *guaranteed; }; -DEFINE_CORESIGHT_DEVLIST (stm_devs,"stm" ); -/** * struct stm_drvdata - specifics associated to an STM component * @base: memory mapped base address for this component.
@@ -834,7 +832,7 @@ static int __stm_probe(struct device *dev, struct resource *res)struct resource ch_res;struct coresight_desc desc = {0 }; - desc.name =coresight_alloc_device_name (&stm_devs, dev); + desc.name =coresight_alloc_device_name ("stm" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-tmc-core.c +4 -8
@@ -32,10 +32,6 @@#include "coresight-priv.h" #include "coresight-tmc.h" -DEFINE_CORESIGHT_DEVLIST (etb_devs,"tmc_etb" ); -DEFINE_CORESIGHT_DEVLIST (etf_devs,"tmc_etf" ); -DEFINE_CORESIGHT_DEVLIST (etr_devs,"tmc_etr" ); -int tmc_wait_for_tmcready (struct tmc_drvdata *drvdata) {struct coresight_device *csdev = drvdata->csdev;
@@ -777,7 +773,7 @@ static int __tmc_probe(struct device *dev, struct resource *res)struct coresight_platform_data *pdata =NULL ;struct tmc_drvdata *drvdata;struct coresight_desc desc = {0 }; -struct coresight_dev_list *dev_list =NULL ; +const char *dev_list =NULL ; drvdata =devm_kzalloc (dev,sizeof (*drvdata),GFP_KERNEL );if (!drvdata)
@@ -827,7 +823,7 @@ static int __tmc_probe(struct device *dev, struct resource *res) desc.type =CORESIGHT_DEV_TYPE_SINK ; desc.subtype.sink_subtype =CORESIGHT_DEV_SUBTYPE_SINK_BUFFER ; desc.ops = &tmc_etb_cs_ops; - dev_list = &etb_devs; + dev_list ="tmc_etb" ;break ;case TMC_CONFIG_TYPE_ETR : desc.groups = coresight_etr_groups;
@@ -839,7 +835,7 @@ static int __tmc_probe(struct device *dev, struct resource *res) goto out;idr_init (&drvdata->idr);mutex_init (&drvdata->idr_mutex); - dev_list = &etr_devs; + dev_list ="tmc_etr" ;break ;case TMC_CONFIG_TYPE_ETF : desc.groups = coresight_etf_groups;
@@ -847,7 +843,7 @@ static int __tmc_probe(struct device *dev, struct resource *res) desc.subtype.sink_subtype =CORESIGHT_DEV_SUBTYPE_SINK_BUFFER ; desc.subtype.link_subtype =CORESIGHT_DEV_SUBTYPE_LINK_FIFO ; desc.ops = &tmc_etf_cs_ops; - dev_list = &etf_devs; + dev_list ="tmc_etf" ;break ;default :pr_err ("%s: Unsupported TMC config\n" , desc.name);
drivers/hwtracing/coresight/coresight-tnoc.c +1 -3
@@ -47,8 +47,6 @@ struct trace_noc_drvdata {int atid; }; -DEFINE_CORESIGHT_DEVLIST (trace_noc_devs,"traceNoc" ); -static void trace_noc_enable_hw (struct trace_noc_drvdata *drvdata) {u32 val;
@@ -191,7 +189,7 @@ static int _tnoc_probe(struct device *dev, struct resource *res)struct coresight_desc desc = {0 };int ret; - desc.name =coresight_alloc_device_name (&trace_noc_devs, dev); + desc.name =coresight_alloc_device_name ("traceNoc" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/coresight-tpda.c +1 -3
@@ -20,8 +20,6 @@#include "coresight-trace-id.h" #include "coresight-tpdm.h" -DEFINE_CORESIGHT_DEVLIST (tpda_devs,"tpda" ); -static void tpda_clear_element_size (struct coresight_device *csdev) {struct tpda_drvdata *drvdata =dev_get_drvdata (csdev->dev.parent);
@@ -585,7 +583,7 @@ static int tpda_probe(struct amba_device *adev, const struct amba_id *id)if (ret)return ret; - desc.name =coresight_alloc_device_name (&tpda_devs, dev); + desc.name =coresight_alloc_device_name ("tpda" , dev);if (!desc.name)return -ENOMEM ; desc.type =CORESIGHT_DEV_TYPE_LINK ;
drivers/hwtracing/coresight/coresight-tpdm.c +1 -3
@@ -19,8 +19,6 @@#include "coresight-priv.h" #include "coresight-tpdm.h" -DEFINE_CORESIGHT_DEVLIST (tpdm_devs,"tpdm" ); -static bool tpdm_has_dsb_dataset (struct tpdm_drvdata *drvdata) {return (drvdata->datasets &TPDM_PIDR0_DS_DSB );
@@ -1416,7 +1414,7 @@ static int tpdm_probe(struct device *dev, struct resource *res) }/* Set up coresight component description */ - desc.name =coresight_alloc_device_name (&tpdm_devs, dev); + desc.name =coresight_alloc_device_name ("tpdm" , dev);if (!desc.name)return -ENOMEM ; desc.type =CORESIGHT_DEV_TYPE_SOURCE ;
drivers/hwtracing/coresight/coresight-tpiu.c +1 -3
@@ -49,8 +49,6 @@#define FFCR_FON_MAN BIT(6)#define FFCR_STOP_FI BIT(12) -DEFINE_CORESIGHT_DEVLIST (tpiu_devs,"tpiu" ); -/* * @base: memory mapped base address for this component. * @atclk: optional clock for the core parts of the TPIU.
@@ -134,7 +132,7 @@ static int __tpiu_probe(struct device *dev, struct resource *res)struct coresight_desc desc = {0 };int ret; - desc.name =coresight_alloc_device_name (&tpiu_devs, dev); + desc.name =coresight_alloc_device_name ("tpiu" , dev);if (!desc.name)return -ENOMEM ;
drivers/hwtracing/coresight/ultrasoc-smb.c +1 -3
@@ -17,8 +17,6 @@#include "coresight-priv.h" #include "ultrasoc-smb.h" -DEFINE_CORESIGHT_DEVLIST (sink_devs,"ultra_smb" ); -#define ULTRASOC_SMB_DSM_UUID "82ae1283-7f6a-4cbe-aa06-53e8fb24db18"static bool smb_buffer_not_empty (struct smb_drv_data *drvdata)
@@ -478,7 +476,7 @@ static int smb_register_sink(struct platform_device *pdev, desc.pdata = pdata; desc.dev = &pdev->dev; desc.groups = smb_sink_groups; - desc.name =coresight_alloc_device_name (&sink_devs, &pdev->dev); + desc.name =coresight_alloc_device_name ("ultra_smb" , &pdev->dev);if (!desc.name) {dev_err (&pdev->dev,"Failed to alloc coresight device name" );return -ENOMEM ;
include/linux/coresight.h +4 -10
@@ -306,24 +306,19 @@ struct coresight_device {* coresight_dev_list - Mapping for devices to "name" index for device * names. * +* @node: Node on the global device index list. * @nr_idx: Number of entries already allocated. * @pfx: Prefix pattern for device name. * @fwnode_list: Array of fwnode_handles associated with each allocated * index, upto nr_idx entries. */ struct coresight_dev_list { +struct list_head node;int nr_idx; -const char *pfx; +char *pfx;struct fwnode_handle **fwnode_list; }; -#define DEFINE_CORESIGHT_DEVLIST (var, dev_pfx) \ -static struct coresight_dev_list (var) = { \ - .pfx = dev_pfx, \ - .nr_idx = 0, \ - .fwnode_list = NULL, \ -} -#define to_coresight_device (d) container_of(d, struct coresight_device, dev)/**
@@ -663,8 +658,7 @@ void coresight_clear_self_claim_tag(struct csdev_access *csa);void coresight_clear_self_claim_tag_unlocked (struct csdev_access *csa);void coresight_disclaim_device (struct coresight_device *csdev);void coresight_disclaim_device_unlocked (struct coresight_device *csdev); -char *coresight_alloc_device_name (struct coresight_dev_list *devs, -struct device *dev); +char *coresight_alloc_device_name (const char *prefix,struct device *dev);bool coresight_loses_context_with_cpu (struct device *dev);
Signature · 1 line
2.34.1
Leo Yan
reviewerPATCH v4 2/8 coresight: Get parent device reference after sink ID map allocation
In reply to Leo YanThe parent device's reference count is incremented before allocating the sink ID map. If the allocation fails, the reference count is not decremented, preventing proper cleanup.
Fix this by incrementing the reference count only after the sink ID map is successfully allocated.
--- drivers/hwtracing/coresight/coresight-core.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +8 -6
@@ -1349,12 +1349,6 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) csdev->dev.parent = desc->dev; csdev->dev.release = coresight_device_release; csdev->dev.bus = &coresight_bustype; -/* -* Hold the reference to our parent device. This will be -* dropped only in coresight_device_release(). -*/ - csdev->dev.fwnode =fwnode_handle_get (dev_fwnode (desc->dev)); -dev_set_name (&csdev->dev,"%s" , desc->name);if (csdev->type ==CORESIGHT_DEV_TYPE_SINK || csdev->type ==CORESIGHT_DEV_TYPE_LINKSINK ) {
@@ -1366,6 +1360,14 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) goto err_out; } } + +/* +* Hold the reference to our parent device. This will be +* dropped only in coresight_device_release(). +*/ + csdev->dev.fwnode =fwnode_handle_get (dev_fwnode (desc->dev)); +dev_set_name (&csdev->dev,"%s" , desc->name); +/* * Make sure the device registration and the connection fixup * are synchronised, so that we don't see uninitialised devices
Signature · 1 line
2.34.1
Leo Yan
reviewerPATCH v4 3/8 coresight: Protect unregistration with mutex
In reply to Leo YanThe device registration is protected by CoreSight mutex to ensure the atomic operations when adding a device onto bus. One the other hand, the locking is absent when unregister a device.
Use mutex to ensure atomicity on device unregistration. During unregistration, unbinding the associated CTI device is not included in the locking region, as CTI has its own locking mechanism.
--- drivers/hwtracing/coresight/coresight-core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hwtracing/coresight/coresight-core.c +4 -1
@@ -1432,14 +1432,17 @@ EXPORT_SYMBOL_GPL(coresight_register);void coresight_unregister (struct coresight_device *csdev) { -etm_perf_del_symlink_sink (csdev);/* Remove references of that device in the topology */ if (cti_assoc_ops && cti_assoc_ops->remove) cti_assoc_ops->remove (csdev); + +mutex_lock (&coresight_mutex); +etm_perf_del_symlink_sink (csdev);coresight_remove_conns (csdev);coresight_clear_default_sink (csdev);coresight_release_platform_data (csdev, csdev->dev.parent, csdev->pdata);device_unregister (&csdev->dev); +mutex_unlock (&coresight_mutex); }EXPORT_SYMBOL_GPL (coresight_unregister);
Signature · 1 line
2.34.1
Leo Yan
reviewerPATCH v4 4/8 coresight: Refactor output connection sysfs link cleanup
In reply to Leo YanTo use a central place for releasing connections, move the output connection sysfs link cleanup into coresight_remove_conns().
Also update the comments accordingly.
--- drivers/hwtracing/coresight/coresight-core.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +7 -9
@@ -1152,7 +1152,6 @@ static int coresight_clear_filter_source(struct device *dev, void *data)return 0 ; } -/* coresight_remove_conns - Remove other device's references to this device */ static void coresight_remove_conns (struct coresight_device *csdev) {int i, j;
@@ -1162,10 +1161,6 @@ static void coresight_remove_conns(struct coresight_device *csdev)bus_for_each_dev (&coresight_bustype,NULL , csdev, coresight_clear_filter_source); -/* -* Remove the input connection references from the destination device -* for each output connection. -*/ for (i =0 ; i < csdev->pdata->nr_outconns; i++) { conn = csdev->pdata->out_conns[i];if (conn->filter_src_fwnode) {
@@ -1176,6 +1171,13 @@ static void coresight_remove_conns(struct coresight_device *csdev)if (!conn->dest_dev)continue ; +/* Remove sysfs links for the output connection */ +coresight_remove_links (csdev, conn); + +/* +* Remove the input connection references from the destination +* device for each output connection. +*/ for (j =0 ; j < conn->dest_dev->pdata->nr_inconns; ++j)if (conn->dest_dev->pdata->in_conns[j] == conn) { conn->dest_dev->pdata->in_conns[j] =NULL ;
@@ -1306,9 +1308,6 @@ void coresight_release_platform_data(struct coresight_device *csdev,struct coresight_connection **conns = pdata->out_conns;for (i =0 ; i < pdata->nr_outconns; i++) { -/* If we have made the links, remove them now */ -if (csdev && conns[i]->dest_dev) -coresight_remove_links (csdev, conns[i]);/* * Drop the refcount and clear the handle as this device * is going away
@@ -1424,7 +1423,6 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) } err_out: -/* Cleanup the connection information */ coresight_release_platform_data (NULL , desc->dev, desc->pdata);return ERR_PTR (ret); }
Signature · 1 line
2.34.1
Leo Yan
reviewerPATCH v4 5/8 coresight: Refactor sysfs connection group cleanup
In reply to Leo YanMove the sysfs connection group cleanup into coresight_remove_conns(), so that the driver removes connections and related sysfs resources in one go.
As side effect, the csdev argument to coresight_release_platform_data() is no longer needed; adjust the code for this.
--- drivers/hwtracing/coresight/coresight-core.c | 11 +++++------ drivers/hwtracing/coresight/coresight-platform.c | 2 +- drivers/hwtracing/coresight/coresight-priv.h | 3 +-- 3 files changed, 7 insertions(+), 9 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +5 -6
@@ -1200,6 +1200,8 @@ static void coresight_remove_conns(struct coresight_device *csdev)coresight_remove_links (conn->src_dev, conn); conn->dest_dev =NULL ; } + +coresight_remove_conns_sysfs_group (csdev); }/**
@@ -1300,8 +1302,7 @@ void coresight_write64(struct coresight_device *csdev, u64 val, u32 offset)* coresight_release_platform_data: Release references to the devices connected * to the output port of this device. */ -void coresight_release_platform_data (struct coresight_device *csdev, -struct device *dev, +void coresight_release_platform_data (struct device *dev,struct coresight_platform_data *pdata) {int i;
@@ -1319,8 +1320,6 @@ void coresight_release_platform_data(struct coresight_device *csdev,devm_kfree (dev, pdata->out_conns);devm_kfree (dev, pdata->in_conns);devm_kfree (dev, pdata); -if (csdev) -coresight_remove_conns_sysfs_group (csdev); }struct coresight_device *coresight_register (struct coresight_desc *desc)
@@ -1423,7 +1422,7 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) } err_out: -coresight_release_platform_data (NULL , desc->dev, desc->pdata); +coresight_release_platform_data (desc->dev, desc->pdata);return ERR_PTR (ret); }EXPORT_SYMBOL_GPL (coresight_register);
@@ -1438,7 +1437,7 @@ void coresight_unregister(struct coresight_device *csdev)etm_perf_del_symlink_sink (csdev);coresight_remove_conns (csdev);coresight_clear_default_sink (csdev); -coresight_release_platform_data (csdev, csdev->dev.parent, csdev->pdata); +coresight_release_platform_data (csdev->dev.parent, csdev->pdata);device_unregister (&csdev->dev);mutex_unlock (&coresight_mutex); }
drivers/hwtracing/coresight/coresight-platform.c +1 -1
@@ -849,7 +849,7 @@ coresight_get_platform_data(struct device *dev) error:if (!IS_ERR_OR_NULL (pdata))/* Cleanup the connection information */ -coresight_release_platform_data (NULL , dev, pdata); +coresight_release_platform_data (dev, pdata);return ERR_PTR (ret); }EXPORT_SYMBOL_GPL (coresight_get_platform_data);
drivers/hwtracing/coresight/coresight-priv.h +1 -2
@@ -239,8 +239,7 @@ static inline void *coresight_get_uci_data_from_amba(const struct amba_id *tablereturn NULL ; } -void coresight_release_platform_data (struct coresight_device *csdev, -struct device *dev, +void coresight_release_platform_data (struct device *dev,struct coresight_platform_data *pdata);struct coresight_device *coresight_find_csdev_by_fwnode (struct fwnode_handle *r_fwnode);
Signature · 1 line
2.34.1
Leo Yan
reviewerPATCH v4 6/8 coresight: Move sink validation into etm_perf_add_symlink_sink()
In reply to Leo YanMove the sink device type checks into etm_perf_add_symlink_sink(), and return -EOPNOTSUPP for unsupported devices. This simplifies the registration flow to invoke etm_perf_add_symlink_sink() unconditionally.
--- drivers/hwtracing/coresight/coresight-core.c | 23 +++++++++-------------- drivers/hwtracing/coresight/coresight-etm-perf.c | 5 ++++- 2 files changed, 13 insertions(+), 15 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +9 -14
@@ -1383,21 +1383,16 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) goto out_unlock; } -if ((csdev->type ==CORESIGHT_DEV_TYPE_SINK || - csdev->type ==CORESIGHT_DEV_TYPE_LINKSINK ) && -sink_ops (csdev)->alloc_buffer) { - ret =etm_perf_add_symlink_sink (csdev); + ret =etm_perf_add_symlink_sink (csdev); -if (ret) { -device_unregister (&csdev->dev); -/* -* As with the above, all resources are free'd -* explicitly via coresight_device_release() triggered -* from put_device(), which is in turn called from -* function device_unregister(). -*/ - goto out_unlock; - } +/* +* As with the above, all resources are free'd explicitly via +* coresight_device_release() triggered from put_device(), which is in +* turn called from function device_unregister(). +*/ +if (ret && ret != -EOPNOTSUPP ) { +device_unregister (&csdev->dev); + goto out_unlock; }/* Device is now registered */ registered = true;
drivers/hwtracing/coresight/coresight-etm-perf.c +4 -1
@@ -902,7 +902,10 @@ int etm_perf_add_symlink_sink(struct coresight_device *csdev)if (csdev->type !=CORESIGHT_DEV_TYPE_SINK && csdev->type !=CORESIGHT_DEV_TYPE_LINKSINK ) -return -EINVAL ; +return -EOPNOTSUPP ; + +if (!sink_ops (csdev)->alloc_buffer) +return -EOPNOTSUPP ;if (csdev->ea !=NULL )return -EINVAL ;
Signature · 1 line
2.34.1
Leo Yan
reviewerPATCH v4 7/8 coresight: Do not mix success path with failure handling
In reply to Leo YanSeparate the failure handling path from the successful case. Use the 'out_unlock' label only for failure handling.
--- drivers/hwtracing/coresight/coresight-core.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +13 -8
@@ -1398,17 +1398,22 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) registered = true; ret =coresight_create_conns_sysfs_group (csdev); -if (!ret) - ret =coresight_fixup_orphan_conns (csdev); +if (ret) + goto out_unlock; + + ret =coresight_fixup_orphan_conns (csdev); +if (ret) + goto out_unlock; + +mutex_unlock (&coresight_mutex); + +if (cti_assoc_ops && cti_assoc_ops->add) + cti_assoc_ops->add (csdev); + +return csdev; out_unlock:mutex_unlock (&coresight_mutex); -/* Success */ -if (!ret) { -if (cti_assoc_ops && cti_assoc_ops->add) - cti_assoc_ops->add (csdev); -return csdev; - }/* Unregister the device if needed */ if (registered) {
Signature · 1 line
2.34.1
Leo Yan
reviewerPATCH v4 8/8 coresight: Unify bus unregistration via coresight_unregister()
In reply to Leo YanOnce a device is successfully registered, set the "registered" flag to true. After that point, all failures jump to the out_unlock label to unwind the flow via coresight_unregister().
Since failure handling is unified, the comment about resource release for the etm_perf_add_symlink_sink() failure is no need, remove it.
--- drivers/hwtracing/coresight/coresight-core.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
drivers/hwtracing/coresight/coresight-core.c +4 -11
@@ -1383,20 +1383,13 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) goto out_unlock; } - ret =etm_perf_add_symlink_sink (csdev); - -/* -* As with the above, all resources are free'd explicitly via -* coresight_device_release() triggered from put_device(), which is in -* turn called from function device_unregister(). -*/ -if (ret && ret != -EOPNOTSUPP ) { -device_unregister (&csdev->dev); - goto out_unlock; - }/* Device is now registered */ registered = true; + ret =etm_perf_add_symlink_sink (csdev); +if (ret && ret != -EOPNOTSUPP ) + goto out_unlock; + ret =coresight_create_conns_sysfs_group (csdev);if (ret) goto out_unlock;
Signature · 1 line
2.34.1
Re: [PATCH v4 8/8] coresight: Unify bus unregistration via coresight_unregister()
Quoted 32 lines · Leo Yan
Once a device is successfully registered, set the "registered" flag to true. After that point, all failures jump to the
out_unlocklabel to unwind the flow viacoresight_unregister().Since failure handling is unified, the comment about resource release for the
etm_perf_add_symlink_sink()failure is no need, remove it.Signed-off-by Leo Yan <[email protected]>--- drivers/hwtracing/coresight/coresight-core.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)drivers/hwtracing/coresight/coresight-core.c +0
-11@@ -1383,20 +1383,13 @@ struct coresight_device *coresight_register(struct coresight_desc *desc) goto out_unlock; } - ret =etm_perf_add_symlink_sink (csdev); - -/* -* As with the above, all resources are free'd explicitly via -* coresight_device_release() triggered from put_device(), which is in -* turn called from function device_unregister(). -*/ -if (ret && ret != -EOPNOTSUPP ) { -device_unregister (&csdev->dev); - goto out_unlock; - }
drivers/hwtracing/coresight/coresight-core.c struct coresight_device *coresight_register(struct coresight_desc *desc) · line 1395
Quoted 10 lines
drivers/hwtracing/coresight/coresight-core.c +4
-0/* Device is now registered */ registered = true; + ret =etm_perf_add_symlink_sink (csdev); +if (ret && ret != -EOPNOTSUPP ) + goto out_unlock; + ret =coresight_create_conns_sysfs_group (csdev);if (ret) goto out_unlock;
Suzuki K Poulose
maintainerHi Leo
Quoted 11 lines · Leo Yan
This series is to fix and refactor CoreSight device registration and unregistration, it can be divided into three small parts:
Patches 01-03: Three fixes for memory leak, device reference and mutex
protection.
Patches 04-05: Move connection cleanup operations into
coresight_remove_conns().
Patches 06-08: Refactor error handling incoresight_register().This series is verified on Juno board.
I get the following splat with LOCKDEP, with the series applied, on Juno.
Here is how I reproduced it :
root@juno-server:~# for m in coresight_etm4x coresight_stm coresight_cti coresight_cpu_debug coresight_tmc coresight_funnel coresight_replicator coresight; do modprobe $m; done
root@juno-server:~# for m in coresight_etm4x coresight_stm coresight_cti coresight_cpu_debug coresight_tmc coresight_funnel coresight_replicator coresight; do modprobe -r $m; done
[ 826.897923] ======================================================
[ 826.904111] WARNING: possible circular locking dependency detected
[ 826.910300] 7.0.0-rc1+ #429 Not tainted
[ 826.914141] ------------------------------------------------------
[ 826.920328] modprobe/489 is trying to acquire lock:
[ 826.925211] ffffffc07bfd85e8 (ect_mutex){+.+.}-{4:4}, at: cti_device_release+0x3c/0x128 [coresight_cti]
[ 826.934650]
[ 826.934650] but task is already holding lock:
[ 826.940488] ffffffc07bf9b980 (coresight_mutex){+.+.}-{4:4}, at: coresight_unregister+0x50/0x218 [coresight]
[ 826.950283]
[ 826.950283] which lock already depends on the new lock.
[ 826.950283]
[ 826.958471]
[ 826.958471] the existing dependency chain (in reverse order) is:
[ 826.965962]
[ 826.965962] -> #1 (coresight_mutex){+.+.}-{4:4}:
[ 826.972073] __mutex_lock+0xb0/0x8e0
[ 826.976181] mutex_lock_nested+0x2c/0x40
[ 826.980634] coresight_add_helper+0x58/0x108 [coresight]
[ 826.986494] cti_probe+0x294/0x440 [coresight_cti]
[ 826.991819] amba_probe+0x11c/0x1d0
[ 826.995837] really_probe+0xc8/0x3f0
[ 826.999940] __driver_probe_device+0x88/0x190
[ 827.004825] driver_probe_device+0x48/0x120
[ 827.009535] __driver_attach+0x168/0x2b0
[ 827.013984] bus_for_each_dev+0x7c/0xe8
[ 827.018346] driver_attach+0x2c/0x40
[ 827.022446] bus_add_driver+0x128/0x258
[ 827.026808] driver_register+0x68/0x138
[ 827.031170] __amba_driver_register+0x2c/0x48
[ 827.036054] debug_runtime_resume+0x30/0x78 [coresight_cpu_debug]
[ 827.042680] do_one_initcall+0x6c/0x520
[ 827.047044] do_init_module+0x60/0x268
[ 827.051322] load_module+0x1f30/0x2128
[ 827.055600] init_module_from_file+0xdc/0x100
[ 827.060487] idempotent_init_module+0x190/0x290
[ 827.065548] __arm64_sys_finit_module+0x74/0xf8
[ 827.070604] invoke_syscall.constprop.0+0x58/0xf8
[ 827.075841] do_el0_svc+0x48/0xd0
[ 827.079683] el0_svc+0x5c/0x360
[ 827.083348] el0t_64_sync_handler+0xa0/0xe8
[ 827.088057] el0t_64_sync+0x198/0x1a0
[ 827.092245]
[ 827.092245] -> #0 (ect_mutex){+.+.}-{4:4}:
[ 827.097834] __lock_acquire+0x12e8/0x1ce0
[ 827.102370] lock_acquire+0x1c4/0x400
[ 827.106557] __mutex_lock+0xb0/0x8e0
[ 827.110659] mutex_lock_nested+0x2c/0x40
[ 827.115109] cti_device_release+0x3c/0x128 [coresight_cti]
[ 827.121129] device_release+0x3c/0xa0
[ 827.125319] kobject_put+0xb8/0x280
[ 827.129334] device_unregister+0x34/0x98
[ 827.133785] coresight_unregister+0x1b0/0x218 [coresight]
[ 827.139730] cti_remove+0xac/0xe8 [coresight_cti]
[ 827.144966] amba_remove+0x38/0x138
[ 827.148981] device_remove+0x54/0x90
[ 827.153081] device_release_driver_internal+0x1e8/0x250
[ 827.158836] driver_detach+0x58/0xa8
[ 827.162938] bus_remove_driver+0x78/0x108
[ 827.167474] driver_unregister+0x38/0x70
[ 827.171923] amba_driver_unregister+0x18/0x30
[ 827.176807] cti_exit+0x1c/0xe90 [coresight_cti]
[ 827.181955] __arm64_sys_delete_module+0x1bc/0x2f0
[ 827.187274] invoke_syscall.constprop.0+0x58/0xf8
[ 827.192509] do_el0_svc+0x48/0xd0
[ 827.196352] el0_svc+0x5c/0x360
[ 827.200016] el0t_64_sync_handler+0xa0/0xe8
[ 827.204725] el0t_64_sync+0x198/0x1a0
[ 827.208911]
[ 827.208911] other info that might help us debug this:
[ 827.208911]
[ 827.216924] Possible unsafe locking scenario:
[ 827.216924]
[ 827.222849] CPU0 CPU1
[ 827.227380] ---- ----
[ 827.231912] lock(coresight_mutex);
[ 827.235491] lock(ect_mutex);
[ 827.241073] lock(coresight_mutex);
[ 827.247176] lock(ect_mutex);
[ 827.250233]
[ 827.250233] *** DEADLOCK ***
[ 827.250233]
[ 827.256158] 2 locks held by modprobe/489:
[ 827.260170] #0: ffffff88015488f0 (&dev->mutex){....}-{4:4}, at: device_release_driver_internal+0x48/0x250
[ 827.269858] #1: ffffffc07bf9b980 (coresight_mutex){+.+.}-{4:4}, at: coresight_unregister+0x50/0x218 [coresight]
[ 827.280084]
[ 827.280084] stack backtrace:
[ 827.284444] CPU: 1 UID: 0 PID: 489 Comm: modprobe Not tainted 7.0.0-rc1+ #429 PREEMPT
[ 827.292377] Hardware name: ARM LTD ARM Juno Development Platform/ARM Juno Development Platform, BIOS EDK II Feb 1 2019
[ 827.303176] Call trace:
[ 827.305621] show_stack+0x20/0x38 (C)
[ 827.309290] dump_stack_lvl+0x90/0xd0
[ 827.312960] dump_stack+0x18/0x28
[ 827.316281] print_circular_bug+0x294/0x378
[ 827.320474] check_noncircular+0x15c/0x170
[ 827.324579] __lock_acquire+0x12e8/0x1ce0
[ 827.328593] lock_acquire+0x1c4/0x400
[ 827.332258] __mutex_lock+0xb0/0x8e0
[ 827.335838] mutex_lock_nested+0x2c/0x40
[ 827.339768] cti_device_release+0x3c/0x128 [coresight_cti]
[ 827.345267] device_release+0x3c/0xa0
[ 827.348935] kobject_put+0xb8/0x280
[ 827.352429] device_unregister+0x34/0x98
[ 827.356358] coresight_unregister+0x1b0/0x218 [coresight]
[ 827.361783] cti_remove+0xac/0xe8 [coresight_cti]
[ 827.366498] amba_remove+0x38/0x138
[ 827.369991] device_remove+0x54/0x90
[ 827.373571] device_release_driver_internal+0x1e8/0x250
[ 827.378804] driver_detach+0x58/0xa8
[ 827.382384] bus_remove_driver+0x78/0x108
[ 827.386399] driver_unregister+0x38/0x70
[ 827.390327] amba_driver_unregister+0x18/0x30
[ 827.394690] cti_exit+0x1c/0xe90 [coresight_cti]
[ 827.399318] __arm64_sys_delete_module+0x1bc/0x2f0
[ 827.404115] invoke_syscall.constprop.0+0x58/0xf8
[ 827.408829] do_el0_svc+0x48/0xd0
[ 827.412151] el0_svc+0x5c/0x360
[ 827.415293] el0t_64_sync_handler+0xa0/0xe8
[ 827.419481] el0t_64_sync+0x198/0x1a0Kind regards
Suzuki
Leo Yan
reviewerOn Mon, Feb 23, 2026 at 01:28:03PM +0000, Suzuki K Poulose wrote:
[...]
Quoted 10 lines
[ 827.216924] Possible unsafe locking scenario: [ 827.216924] [ 827.222849] CPU0 CPU1 [ 827.227380] ---- ---- [ 827.231912] lock(coresight_mutex); [ 827.235491] lock(ect_mutex); [ 827.241073] lock(coresight_mutex); [ 827.247176] lock(ect_mutex); [ 827.250233] [ 827.250233] *** DEADLOCK ***
Thanks for the test, Suzuki.
I have sent a refactor patch to dismiss locking chain:
https://lore.kernel.org/linux-arm-kernel/20260224-arm_coresight_refactor_cti_resource_release-v1-1-ff1b2bca9176@arm.com/T/#u
For recording, I enabled the following configs to reproduce the lockdep report:
CONFIG_LOCKUP_DETECTOR
CONFIG_PROVE_LOCKING
Thanks,
Leo
Suzuki K Poulose
maintainerQuoted 15 lines · Leo Yan
On Mon, Feb 23, 2026 at 01:28:03PM +0000, Suzuki K Poulose wrote:
[...]
Quoted 10 lines
[ 827.216924] Possible unsafe locking scenario: [ 827.216924] [ 827.222849] CPU0 CPU1 [ 827.227380] ---- ---- [ 827.231912] lock(coresight_mutex); [ 827.235491] lock(ect_mutex); [ 827.241073] lock(coresight_mutex); [ 827.247176] lock(ect_mutex); [ 827.250233] [ 827.250233] *** DEADLOCK ***
Quoted 8 lines
Thanks for the test, Suzuki.
I have sent a refactor patch to dismiss locking chain:
https://lore.kernel.org/linux-arm-kernel/20260224-arm_coresight_refactor_cti_resource_release-v1-1-ff1b2bca9176@arm.com/T/#uFor recording, I enabled the following configs to reproduce the lockdep report:
Thanks for the patch, I can confirm that it fixes the problem and is indeed a good cleanup. Hacking the device release operation wasn't really a good idea.
Cheers
Suzuki
Quoted 5 lines
CONFIG_LOCKUP_DETECTOR
CONFIG_PROVE_LOCKINGThanks,
Leo
Quoted 10 lines · Leo Yan
This series is to fix and refactor CoreSight device registration and unregistration, it can be divided into three small parts:
Patches 01-03: Three fixes for memory leak, device reference and mutex
protection.
Patches 04-05: Move connection cleanup operations into
coresight_remove_conns().
Patches 06-08: Refactor error handling incoresight_register().[...]
Applied, thanks!
[1/8] coresight: Fix memory leak in coresight_alloc_device_name()
https://git.kernel.org/coresight/c/0289ada4a316 [2/8] coresight: Get parent device reference after sink ID map allocation https://git.kernel.org/coresight/c/2f322f039281 [3/8] coresight: Protect unregistration with mutex https://git.kernel.org/coresight/c/a6c4da4b95a3 [4/8] coresight: Refactor output connection sysfs link cleanup https://git.kernel.org/coresight/c/32c225491ed8 [5/8] coresight: Refactor sysfs connection group cleanup https://git.kernel.org/coresight/c/babb987968d3 [6/8] coresight: Move sink validation into etm_perf_add_symlink_sink() https://git.kernel.org/coresight/c/6b1ffc542850 [7/8] coresight: Do not mix success path with failure handling https://git.kernel.org/coresight/c/8573756b235d [8/8] coresight: Unify bus unregistration via coresight_unregister() https://git.kernel.org/coresight/c/eef33a7cce23
Best regards,
Signature · 1 line
Suzuki K Poulose <[email protected]>