mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jie Gan <jie.gan@oss.qualcomm.com>
To: Suzuki K Poulose <suzuki.poulose@arm.com>,
	Mike Leach <mike.leach@arm.com>,
	James Clark <james.clark@linaro.org>, Leo Yan <leo.yan@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Rob Herring <robh@kernel.org>,
	Krzysztof Kozlowski <krzk+dt@kernel.org>,
	Conor Dooley <conor+dt@kernel.org>,
	Tingwei Zhang <tingwei.zhang@oss.qualcomm.com>,
	Bjorn Andersson <andersson@kernel.org>,
	Konrad Dybcio <konradybcio@kernel.org>,
	Mao Jinlong <jinlong.mao@oss.qualcomm.com>,
	Abel Vesa <abelvesa@kernel.org>,
	Yuanfang Zhang <yuanfang.zhang@oss.qualcomm.com>
Cc: coresight@lists.linaro.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org, linux-arm-msm@vger.kernel.org,
	devicetree@vger.kernel.org,
	linux-stm32@st-md-mailman.stormreply.com
Subject: Re: [PATCH v23 2/8] coresight: tmc: add create/clean functions for etr_buf_list
Date: Thu, 19 Nov 2026 09:46:22 +0800	[thread overview]
Message-ID: <ad300534-54a0-49f1-bda9-3d781e3d730d@oss.qualcomm.com> (raw)
In-Reply-To: <3348a751-87df-4f22-80e0-8467c6b263f9@arm.com>



On 11/18/2026 7:16 PM, Suzuki K Poulose wrote:
> On 18/11/2026 08:02, Jie Gan wrote:
>> 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 is
>> has been filled and synced, transferring data to the userspace.

[...]

>> +     * We expect that sysfs_buf in drvdata has already been allocated.
>> +     * Wrap the live sysfs_buf into the first node so the captured trace
>> +     * data is preserved. Take the sysfs_buf and add the node under the
>> +     * lock, so that the ETR sink enable path sees a non-empty list and
>> +     * doesn't replace the buffer.
>> +     */
>> +    new_node = kzalloc_obj(*new_node, GFP_KERNEL);
>> +    if (!new_node)
>> +        return -ENOMEM;
> 
> Since we know we need num_node entries at max, could we not allocate 
> them in oneshot (kcalloc()) and free the unused ones at the end ?

I hadn't considered it from this way before. It's a good solution for 
allocating the required buffers while simplifying the code and making 
the locking easier to handle correctly.

> 
>> +
>> +    raw_spin_lock_irqsave(&drvdata->spinlock, flags);
>> +    /* The sysfs_buf is in use by a sysfs read session */
>> +    if (drvdata->reading) {
> 
>> +        raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
>> +        kfree(new_node);
>> +        return -EBUSY;
> 
> This pattern is repeating quite a lot, do you think this can be a single
> exit from the function for such cases, something like goto err_unlock;
> 

That would be better.

>> +    }
>> +
>> +    /*
>> +     * 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.
>> +     */
>> +    if (coresight_get_mode(drvdata->csdev) != CS_MODE_SYSFS) {
>> +        raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
>> +        kfree(new_node);
>> +        return -EINVAL;
> 
> 2
> 
>> +    }
>> +
>> +    if (drvdata->sysfs_buf) {
>> +        /* The mode may have been changed since the sink was enabled */
>> +        if (drvdata->sysfs_buf->mode == ETR_MODE_RESRV) {
>> +            raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
>> +            kfree(new_node);
>> +            return -EINVAL;
> 
> 3
> 
>> +        }
>> +
>> +        live_buf = drvdata->sysfs_buf;
>> +        /* Keep the buffers of the list in the size of the live one */
>> +        size = live_buf->size;
>> +        new_node->sysfs_buf = live_buf;
>> +        new_node->is_free = false;
>> +        list_add(&new_node->link, &drvdata->etr_buf_list);
>> +        new_node = NULL;
>> +        i++;
>> +    }
>> +    raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
>> +    /* Free the node if there is no live sysfs_buf to wrap */
>> +    kfree(new_node);
>> +
>> +    while (i < num_nodes) {
>> +        new_node = kzalloc_obj(*new_node, GFP_KERNEL);
>> +        if (!new_node) {
>> +            ret = -ENOMEM;
>> +            break;
>> +        }
>> +
>> +        /* 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)) {
>> +            kfree(new_node);
>> +            ret = PTR_ERR(sysfs_buf);
>> +            break;
>> +        }
>> +
>> +        /* The buffer mode may have been changed to the reserved mode */
>> +        if (sysfs_buf->mode == ETR_MODE_RESRV) {
>> +            tmc_etr_free_sysfs_buf(sysfs_buf);
>> +            kfree(new_node);
>> +            ret = -EINVAL;
>> +            break;
>> +        }
>> +
>> +        new_node->sysfs_buf = sysfs_buf;
>> +        /*
>> +         * The drvdata->sysfs_buf write and the list update need the
>> +         * spinlock, to serialise against the ETR sink enable/disable
>> +         * paths.
>> +         */
>> +        raw_spin_lock_irqsave(&drvdata->spinlock, flags);
>> +        /* We don't have an available sysfs_buf in drvdata, set one 
>> up */
>> +        if (!drvdata->sysfs_buf) {
>> +            drvdata->sysfs_buf = sysfs_buf;
>> +            new_node->is_free = false;
> 
> Is this supposed to be done in enable ?

Yes, picking the node during enable and marking it as free makes the 
logic much easier to understand.

> 
>> +        } else {
>> +            new_node->is_free = true;
>> +        }
>> +        list_add_tail(&new_node->link, &drvdata->etr_buf_list);
>> +        raw_spin_unlock_irqrestore(&drvdata->spinlock, flags);
> 
> Instead of adding every node as we go, could we maintain a local list 
> and add that to the the etr_buf_list at the end of all the allocations
> and when we are sure that things are still sane to do ?
> Set all new_node->free = true and when we get to add the local list
> set the sys_buf with the first entry and mark it false.
> 

I will try this solution.

>> +        i++;
>> +    }
>> +
>> +    /*
>> +     * Clean the list if there is an error, but keep the live 
>> sysfs_buf even
>> +     * if the sink was disabled meanwhile, as it holds the captured 
>> trace data.
>> +     */
>> +    if (ret)
>> +        __tmc_clean_etr_buf_list(drvdata, live_buf);
>> +
>> +    return ret;
>> +}
>> +EXPORT_SYMBOL_GPL(tmc_create_etr_buf_list);
>> +
>>   int tmc_read_prepare_etr(struct tmc_drvdata *drvdata)
>>   {
>>       int ret = 0;
>> @@ -1928,7 +2128,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..7c604b9f92e5 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.
>> + * @is_free:    Indicates whether the buffer is free to choose.
> 
> super minor nit: Does this mean the buffer is available for tracing ?
> Simply "free" is good enough. Or choose inverted logic "live" or "busy"
> 

free is enough. I will update it.

Thanks,
Jie

> Suzuki
>  


  reply	other threads:[~2026-11-19  1:46 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  8:02 [PATCH v23 0/8] coresight: ctcu: Enable byte-cntr function for TMC ETR Jie Gan
2026-11-18  8:02 ` [PATCH v23 1/8] coresight: core: refactor ctcu_get_active_port and make it generic Jie Gan
2026-11-18  8:02 ` [PATCH v23 2/8] coresight: tmc: add create/clean functions for etr_buf_list Jie Gan
2026-11-18 11:16   ` Suzuki K Poulose
2026-11-19  1:46     ` Jie Gan [this message]
2026-11-18  8:02 ` [PATCH v23 3/8] coresight: tmc: add a function to switch the ETR to a free etr_buf Jie Gan
2026-11-18 11:33   ` Suzuki K Poulose
2026-11-19  1:55     ` Jie Gan
2026-11-18  8:02 ` [PATCH v23 4/8] coresight: tmc: introduce tmc_sysfs_ops to wrap sysfs read operations Jie Gan
2026-11-18  8:02 ` [PATCH v23 5/8] coresight: etr: add a new function to retrieve the CTCU device Jie Gan
2026-11-18  8:02 ` [PATCH v23 6/8] dt-bindings: arm: add an interrupt property for Coresight CTCU Jie Gan
2026-11-18  8:02 ` [PATCH v23 7/8] coresight: ctcu: enable byte-cntr for TMC ETR devices Jie Gan
2026-11-18  8:02 ` [PATCH v23 8/8] arm64: dts: qcom: lemans: add interrupts to CTCU device Jie Gan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ad300534-54a0-49f1-bda9-3d781e3d730d@oss.qualcomm.com \
    --to=jie.gan@oss.qualcomm.com \
    --cc=abelvesa@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=andersson@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=coresight@lists.linaro.org \
    --cc=devicetree@vger.kernel.org \
    --cc=james.clark@linaro.org \
    --cc=jinlong.mao@oss.qualcomm.com \
    --cc=konradybcio@kernel.org \
    --cc=krzk+dt@kernel.org \
    --cc=leo.yan@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-stm32@st-md-mailman.stormreply.com \
    --cc=mike.leach@arm.com \
    --cc=robh@kernel.org \
    --cc=suzuki.poulose@arm.com \
    --cc=tingwei.zhang@oss.qualcomm.com \
    --cc=yuanfang.zhang@oss.qualcomm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®