[PATCH v24 2/8] coresight: tmc: add create/clean functions for etr_buf_list
From: Jie Gan
Date: Thu Nov 19 2026 - 22:28:20 EST
Introduce functions for creating and inserting or removing the
etr_buf_node to/from the etr_buf_list.
The byte-cntr functionality requires two etr_buf to receive trace data.
The active etr_buf collects the trace data from source device, while the
byte-cntr reading function accesses the deactivated etr_buf after it
has been filled and synced, transferring data to the userspace.
Reviewed-by: Mike Leach <mike.leach@xxxxxxx>
Signed-off-by: Jie Gan <jie.gan@xxxxxxxxxxxxxxxx>
---
drivers/hwtracing/coresight/coresight-tmc-core.c | 1 +
drivers/hwtracing/coresight/coresight-tmc-etr.c | 197 ++++++++++++++++++++++-
drivers/hwtracing/coresight/coresight-tmc.h | 20 +++
3 files changed, 216 insertions(+), 2 deletions(-)
diff --git a/drivers/hwtracing/coresight/coresight-tmc-core.c b/drivers/hwtracing/coresight/coresight-tmc-core.c
index bc5a133ada3e..bc7dd676da47 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-core.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-core.c
@@ -835,6 +835,7 @@ static int __tmc_probe(struct device *dev, struct resource *res)
idr_init(&drvdata->idr);
mutex_init(&drvdata->idr_mutex);
dev_list = "tmc_etr";
+ INIT_LIST_HEAD(&drvdata->etr_buf_list);
break;
case TMC_CONFIG_TYPE_ETF:
desc.groups = coresight_etf_groups;
diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c
index 361a433e6f0c..03eead4176d2 100644
--- a/drivers/hwtracing/coresight/coresight-tmc-etr.c
+++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c
@@ -1268,7 +1268,9 @@ static struct etr_buf *tmc_etr_get_sysfs_buffer(struct coresight_device *csdev)
raw_spin_lock_irqsave(&drvdata->spinlock, flags);
}
- if (drvdata->reading || coresight_get_mode(csdev) == CS_MODE_PERF) {
+ /* The sysfs_buf is owned by the etr_buf_list, don't replace it */
+ if (drvdata->reading || coresight_get_mode(csdev) == CS_MODE_PERF ||
+ !list_empty(&drvdata->etr_buf_list)) {
ret = -EBUSY;
goto out;
}
@@ -1918,6 +1920,196 @@ const struct coresight_ops tmc_etr_cs_ops = {
.panic_ops = &tmc_etr_sync_ops,
};
+/* Free the buffers owned by the nodes of a list, and the array of the nodes */
+static void tmc_etr_free_buf_nodes(struct list_head *head,
+ struct etr_buf_node *nodes)
+{
+ struct etr_buf_node *nd, *next;
+
+ list_for_each_entry_safe(nd, next, head, link) {
+ list_del(&nd->link);
+ /* The buffer is NULL if the node doesn't own one, which is ignored */
+ tmc_etr_free_sysfs_buf(nd->sysfs_buf);
+ }
+ kfree(nodes);
+}
+
+/**
+ * tmc_clean_etr_buf_list - clean the etr_buf_list.
+ * @drvdata: driver data of the TMC device.
+ *
+ * Remove all nodes from @drvdata->etr_buf_list and free their buffers.
+ * If a node holds the live sysfs_buf and the sink is enabled in sysfs mode, the
+ * node is removed but the buffer is not freed; ownership stays with
+ * drvdata->sysfs_buf.
+ *
+ * Locking: must not be called with @drvdata->spinlock held. The spinlock is
+ * taken internally to serialise the @drvdata->sysfs_buf accesses and the
+ * removal of the nodes against the ETR sink enable/disable paths and the
+ * generic sysfs read. There must be only one owner of the list at a time.
+ * Must be called from process context: buffers are freed with the lock
+ * released.
+ */
+void tmc_clean_etr_buf_list(struct tmc_drvdata *drvdata)
+{
+ struct etr_buf_node *nd, *next, *nodes;
+ LIST_HEAD(free_list);
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&drvdata->spinlock, flags);
+ list_for_each_entry_safe(nd, next, &drvdata->etr_buf_list, link) {
+ if (nd->sysfs_buf == drvdata->sysfs_buf) {
+ if (coresight_get_mode(drvdata->csdev) == CS_MODE_SYSFS)
+ /*
+ * The sink is still enabled in sysfs mode. Keep
+ * the live buffer owned by drvdata->sysfs_buf
+ * and only drop the list's reference to it.
+ */
+ nd->sysfs_buf = NULL;
+ else
+ /* Free the buffer below through nd->sysfs_buf */
+ drvdata->sysfs_buf = NULL;
+ }
+ list_move_tail(&nd->link, &free_list);
+ }
+ nodes = drvdata->etr_buf_nodes;
+ drvdata->etr_buf_nodes = NULL;
+ raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
+
+ /* Free the buffers and the nodes out of the lock */
+ tmc_etr_free_buf_nodes(&free_list, nodes);
+}
+EXPORT_SYMBOL_GPL(tmc_clean_etr_buf_list);
+
+/*
+ * Check whether the live sysfs_buf can be wrapped into the etr_buf_list.
+ * Must be called with @drvdata->spinlock held.
+ */
+static int tmc_etr_check_live_buf(struct tmc_drvdata *drvdata)
+{
+ /*
+ * The sysfs_buf is in use by a sysfs read session, or the list is owned
+ * by another session.
+ */
+ if (drvdata->reading || !list_empty(&drvdata->etr_buf_list))
+ return -EBUSY;
+
+ /*
+ * The sink enable path reuses or frees the sysfs_buf of a sink which
+ * isn't enabled, so only wrap it while the sink is enabled in sysfs mode.
+ * The mode of the buffer may have been changed since the sink was enabled.
+ */
+ if (coresight_get_mode(drvdata->csdev) != CS_MODE_SYSFS ||
+ !drvdata->sysfs_buf || drvdata->sysfs_buf->mode == ETR_MODE_RESRV)
+ return -EINVAL;
+
+ return 0;
+}
+
+/**
+ * tmc_create_etr_buf_list - create a list to manage the etr_buf_node.
+ * @drvdata: driver data of the TMC device.
+ * @num_nodes: number of nodes want to create with the list.
+ *
+ * The nodes are allocated at once and the buffers are allocated first, and
+ * added to the list in one critical section once the state of the sink is
+ * checked again, so @drvdata is not touched on failure. The live sysfs_buf of
+ * the sink is wrapped into the first node to preserve the captured trace data,
+ * and the buffers are allocated with the size of it.
+ *
+ * Locking: must not be called with @drvdata->spinlock held. The spinlock is
+ * taken internally to serialise the @drvdata->sysfs_buf accesses and every
+ * update of @drvdata->etr_buf_list against the ETR sink enable/disable paths
+ * and the generic sysfs read. There must be only one owner of the list at a
+ * time. Must be called from process context: buffers and nodes are allocated
+ * with the lock released.
+ *
+ * Return: 0 upon success, else the error number: -EBUSY if a generic sysfs
+ * read is in progress or the list exists, -EINVAL if the sink is not enabled
+ * in sysfs mode, its buffer was replaced or a buffer is of the reserved buffer
+ * mode, or the error of the allocation.
+ */
+int tmc_create_etr_buf_list(struct tmc_drvdata *drvdata, int num_nodes)
+{
+ struct etr_buf_node *nodes;
+ struct etr_buf *sysfs_buf;
+ LIST_HEAD(new_list);
+ unsigned long flags;
+ size_t size;
+ int i, ret;
+
+ /* We don't need a list if there is only one node */
+ if (num_nodes < 2)
+ return -EINVAL;
+
+ /*
+ * All the etr_buf of the reserved mode are mapped to the same reserved
+ * region, so they can't be used as separate buffers of the list.
+ */
+ if (drvdata->etr_mode == ETR_MODE_RESRV)
+ return -EINVAL;
+
+ /* Keep the buffers of the list in the size of the live one */
+ raw_spin_lock_irqsave(&drvdata->spinlock, flags);
+ ret = tmc_etr_check_live_buf(drvdata);
+ if (!ret)
+ size = drvdata->sysfs_buf->size;
+ raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
+ if (ret)
+ return ret;
+
+ /* We know the number of the nodes, allocate them at once */
+ nodes = kzalloc_objs(*nodes, num_nodes, GFP_KERNEL);
+ if (!nodes)
+ return -ENOMEM;
+
+ for (i = 0; i < num_nodes; i++) {
+ nodes[i].free = true;
+ list_add_tail(&nodes[i].link, &new_list);
+ }
+
+ /* The first node is for the live sysfs_buf */
+ for (i = 1; i < num_nodes; i++) {
+ /* Allocate the buffer with the lock released */
+ sysfs_buf = tmc_alloc_etr_buf(drvdata, size, 0, cpu_to_node(0), NULL);
+ if (IS_ERR(sysfs_buf)) {
+ ret = PTR_ERR(sysfs_buf);
+ goto err_free;
+ }
+
+ nodes[i].sysfs_buf = sysfs_buf;
+ /* The buffer mode may have been changed to the reserved mode */
+ if (sysfs_buf->mode == ETR_MODE_RESRV) {
+ ret = -EINVAL;
+ goto err_free;
+ }
+ }
+
+ raw_spin_lock_irqsave(&drvdata->spinlock, flags);
+ /* The sink may have been disabled or its buffer replaced meanwhile */
+ ret = tmc_etr_check_live_buf(drvdata);
+ if (!ret && drvdata->sysfs_buf->size != size)
+ ret = -EINVAL;
+ if (ret)
+ goto err_unlock;
+
+ nodes[0].sysfs_buf = drvdata->sysfs_buf;
+ nodes[0].free = false;
+ list_splice_tail_init(&new_list, &drvdata->etr_buf_list);
+ drvdata->etr_buf_nodes = nodes;
+ raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
+
+ return 0;
+
+err_unlock:
+ raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
+err_free:
+ tmc_etr_free_buf_nodes(&new_list, nodes);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(tmc_create_etr_buf_list);
+
int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
{
int ret = 0;
@@ -1928,7 +2120,8 @@ int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
return -EINVAL;
raw_spin_lock_irqsave(&drvdata->spinlock, flags);
- if (drvdata->reading) {
+ /* The sysfs_buf is owned by the etr_buf_list */
+ if (drvdata->reading || !list_empty(&drvdata->etr_buf_list)) {
ret = -EBUSY;
goto out;
}
diff --git a/drivers/hwtracing/coresight/coresight-tmc.h b/drivers/hwtracing/coresight/coresight-tmc.h
index 319a354ede9f..ff2dbd85415e 100644
--- a/drivers/hwtracing/coresight/coresight-tmc.h
+++ b/drivers/hwtracing/coresight/coresight-tmc.h
@@ -208,6 +208,20 @@ struct tmc_resrv_buf {
s64 len;
};
+/**
+ * struct etr_buf_node - node of the etr_buf_list
+ * @sysfs_buf: Allocated sysfs_buf.
+ * @free: Indicates whether the buffer is free to choose.
+ * @pos: Offset to the start of the buffer.
+ * @link: list_head of the node.
+ */
+struct etr_buf_node {
+ struct etr_buf *sysfs_buf;
+ bool free;
+ loff_t pos;
+ struct list_head link;
+};
+
/**
* struct tmc_drvdata - specifics associated to an TMC component
* @atclk: optional clock for the core parts of the TMC.
@@ -245,6 +259,8 @@ struct tmc_resrv_buf {
* (after crash) by default.
* @crash_mdata: Reserved memory for storing tmc crash metadata.
* Used by ETR/ETF.
+ * @etr_buf_list: List that is used to manage allocated etr_buf.
+ * @etr_buf_nodes: Array of the nodes of @etr_buf_list, allocated at once.
*/
struct tmc_drvdata {
struct clk *atclk;
@@ -275,6 +291,8 @@ struct tmc_drvdata {
struct etr_buf *perf_buf;
struct tmc_resrv_buf resrv_buf;
struct tmc_resrv_buf crash_mdata;
+ struct list_head etr_buf_list;
+ struct etr_buf_node *etr_buf_nodes;
};
struct etr_buf_operations {
@@ -447,5 +465,7 @@ struct etr_buf *tmc_etr_get_buffer(struct coresight_device *csdev,
enum cs_mode mode,
struct coresight_path *path);
extern const struct attribute_group coresight_etr_group;
+void tmc_clean_etr_buf_list(struct tmc_drvdata *drvdata);
+int tmc_create_etr_buf_list(struct tmc_drvdata *drvdata, int num_nodes);
#endif
--
2.34.1