public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Ruidong Tian <tianruidong@linux.alibaba.com>
To: James Clark <james.clark@arm.com>, coresight@lists.linaro.org
Cc: mike.leach@linaro.org, alexander.shishkin@linux.intel.com,
	linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org
Subject: Re: [RESEND PATCH] coresight: tmc: Explicit type conversions to prevent integer overflow
Date: Wed, 2 Aug 2023 20:25:30 +0800	[thread overview]
Message-ID: <a448f505-fef4-e1b7-c31e-e1f310c16e8f@linux.alibaba.com> (raw)
In-Reply-To: <61f11ffe-473c-a36e-c51d-9e526a6fd375@arm.com>

Hi James,

Sorry, some local patch caused inaccurate information. Please allow me 
to reintroduce the question:

If you use perf with 1G AUX buffer, you can get 1G perf data. Perf 
workload is kernel build here:

     perf record -C 0 -m ,1G -e cs_etm// taskset -c 0 make

     [ perf record: Captured and wrote 1025.557 MB perf.data ]

But if you use 2G AUX buffer, perf was executed unexpectedly:

     perf record -C 0 -m ,2G -e cs_etm// taskset -c 0 make

     [ perf record: Captured and wrote 2.615 MB perf.data ]

There are just 2.615 MB perf data rather than 2G, if you probe function 
"tmc_alloc_etr_buf" in

coresight_tmc module, you can find some clues:

   perf probe -m coresight_tmc "tmc_alloc_etr_buf size:s64"

   perf record -e probe:tmc_alloc_etr_buf -aR -- perf record -C 0 -m ,2G 
-e cs_etm// -o cs.data taskset -c 0 make

   perf script
             perf 118267 [064]  4640.324670: probe:tmc_alloc_etr_buf: 
(ffff80007a9dce60) size_s64=-2147483648
             perf 118267 [064]  4640.324681: probe:tmc_alloc_etr_buf: 
(ffff80007a9dce60) size_s64=1048576

It's pretty obvious what's going on here. The first call of 
tmc_alloc_etr_buf in alloc_etr_buf was

failed because of overflow, the second call of tmc_alloc_etr_buf just 
alloc 1M AUX buffer which

is default ETR buffer size rather than 2G. That is why we can just get 
2.615MB ( 1M AUX data

+ perf header ).

It is necessary to check the conversion from int to s64 in coresight_tmc 
driver. The issue[1] also

exists in coresight/perf, but it's different from this topic.


[1]:
https://lore.kernel.org/bpf/20230711014120.53461-1-xueshuai@linux.alibaba.com/ 


Thanks
Ruidong

On 2023/7/24 23:38, James Clark wrote:
>
> On 14/07/2023 09:43, Ruidong Tian wrote:
>> Perf cs_etm session will failed when AUX buffer > 1G.
>>
>>    perf record -C 0 -m ,2G -e cs_etm// -- taskset -c 0 ls
>>    failed to mmap with 12 (Cannot allocate memory)
>>
>> In coresight tmc driver, "nr_pages << PAGE_SHIFT" will overflow when
>> nr_pages >= 0x80000(correspond to 1G AUX buffer). Explicit convert nr_pages
>> to 64 bit to avoid overflow.
>>
> Hi Ruidong,
>
> I couldn't reproduce this exact issue with the error message in the
> commit message. Is it not another manifestation related to this change
> [1]? I don't actually get any error message, but I was able to get a
> warning in dmesg even with [1] applied.
>
> Does the overflow not result in a successful session but with the wrong
> buffer size?
>
> I think the change makes sense, but maybe we also need a check for
> MAX_ORDER because I can trigger the same WARN_ON from [1]. Or maybe I'm
> a bit confused because of the other change and not being able to
> reproduce this exactly coming at the same time.
>
> [1]:
> https://lore.kernel.org/bpf/20230711014120.53461-1-xueshuai@linux.alibaba.com/
>
> Thanks
> James
>
>> Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
>> ---
>>   drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +-
>>   drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
>>   2 files changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/drivers/hwtracing/coresight/coresight-tmc-etr.c b/drivers/hwtracing/coresight/coresight-tmc-etr.c
>> index 766325de0e29..1425ecd1cf78 100644
>> --- a/drivers/hwtracing/coresight/coresight-tmc-etr.c
>> +++ b/drivers/hwtracing/coresight/coresight-tmc-etr.c
>> @@ -1267,7 +1267,7 @@ alloc_etr_buf(struct tmc_drvdata *drvdata, struct perf_event *event,
>>   	 * than the size requested via sysfs.
>>   	 */
>>   	if ((nr_pages << PAGE_SHIFT) > drvdata->size) {
>> -		etr_buf = tmc_alloc_etr_buf(drvdata, (nr_pages << PAGE_SHIFT),
>> +		etr_buf = tmc_alloc_etr_buf(drvdata, ((ssize_t)nr_pages << PAGE_SHIFT),
>>   					    0, node, NULL);
>>   		if (!IS_ERR(etr_buf))
>>   			goto done;
>> diff --git a/drivers/hwtracing/coresight/coresight-tmc.h b/drivers/hwtracing/coresight/coresight-tmc.h
>> index b97da39652d2..0ee48c5ba764 100644
>> --- a/drivers/hwtracing/coresight/coresight-tmc.h
>> +++ b/drivers/hwtracing/coresight/coresight-tmc.h
>> @@ -325,7 +325,7 @@ ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table,
>>   static inline unsigned long
>>   tmc_sg_table_buf_size(struct tmc_sg_table *sg_table)
>>   {
>> -	return sg_table->data_pages.nr_pages << PAGE_SHIFT;
>> +	return (unsigned long)sg_table->data_pages.nr_pages << PAGE_SHIFT;
>>   }
>>   
>>   struct coresight_device *tmc_etr_get_catu_device(struct tmc_drvdata *drvdata);

  reply	other threads:[~2023-08-02 12:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-14  8:43 [RESEND PATCH] coresight: tmc: Explicit type conversions to prevent integer overflow Ruidong Tian
2023-07-24 15:38 ` James Clark
2023-08-02 12:25   ` Ruidong Tian [this message]
2023-08-02 12:32     ` James Clark
2023-08-03  2:33 ` [PATCH v2] " Ruidong Tian
2023-08-03 16:19   ` Suzuki K Poulose
2023-08-04  3:06     ` Ruidong Tian
2023-08-04  8:15 ` [PATCH v3 0/1] coresight: tmc: Explicit type conversions to Ruidong Tian
2023-08-04  8:15   ` [PATCH v3 1/1] coresight: tmc: Explicit type conversions to prevent integer overflow Ruidong Tian
2023-08-04 13:16     ` Suzuki K Poulose

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=a448f505-fef4-e1b7-c31e-e1f310c16e8f@linux.alibaba.com \
    --to=tianruidong@linux.alibaba.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=coresight@lists.linaro.org \
    --cc=james.clark@arm.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mike.leach@linaro.org \
    /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