diff mbox series

[v2] coresight: tmc: Explicit type conversions to prevent integer overflow

Message ID 20230803023321.111078-1-tianruidong@linux.alibaba.com (mailing list archive)
State New, archived
Headers show
Series [v2] coresight: tmc: Explicit type conversions to prevent integer overflow | expand

Commit Message

Ruidong Tian Aug. 3, 2023, 2:33 a.m. UTC
Perf cs_etm session executed unexpectedly when AUX buffer > 1G.

  perf record -C 0 -m ,2G -e cs_etm// -- <workload>
  [ perf record: Captured and wrote 2.615 MB perf.data ]

Perf only collect about 2M perf data rather than 2G. This is becasuse
the operation, "nr_pages << PAGE_SHIFT", in coresight tmc driver, will
overflow when nr_pages >= 0x80000(correspond to 1G AUX buffer). The
overflow cause buffer allocation to fail, and TMC driver will alloc
minimal buffer size(1M). You can just get about 2M perf data(1M AUX
buffer + perf data header) at least.

Explicit convert nr_pages to 64 bit to avoid overflow.

Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
Reviewed-by: James Clark <james.clark@arm.com>
---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +-
 drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Comments

Suzuki K Poulose Aug. 3, 2023, 4:19 p.m. UTC | #1
On 03/08/2023 03:33, Ruidong Tian wrote:
> Perf cs_etm session executed unexpectedly when AUX buffer > 1G.
> 
>    perf record -C 0 -m ,2G -e cs_etm// -- <workload>
>    [ perf record: Captured and wrote 2.615 MB perf.data ]
> 
> Perf only collect about 2M perf data rather than 2G. This is becasuse
> the operation, "nr_pages << PAGE_SHIFT", in coresight tmc driver, will
> overflow when nr_pages >= 0x80000(correspond to 1G AUX buffer). The
> overflow cause buffer allocation to fail, and TMC driver will alloc
> minimal buffer size(1M). You can just get about 2M perf data(1M AUX
> buffer + perf data header) at least.
> 
> Explicit convert nr_pages to 64 bit to avoid overflow.
> 
> Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
> Reviewed-by: James Clark <james.clark@arm.com>

Fixes: 22f429f19c41 ("coresight: etm-perf: Add support for ETR backend")


> ---
>   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);

There are other places where such a fix is needed.
e.g.,

$ git grep "nr_pages << PAGE_SHIFT" 
drivers/hwtracing/coresight/coresight-tmc*
drivers/hwtracing/coresight/coresight-tmc-etf.c:        head = 
handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
drivers/hwtracing/coresight/coresight-tmc-etr.c:#define 
PERF_IDX2OFF(idx, buf)  ((idx) % ((buf)->nr_pages << PAGE_SHIFT))
drivers/hwtracing/coresight/coresight-tmc-etr.c:        if ((nr_pages << 
PAGE_SHIFT) > drvdata->size) {
drivers/hwtracing/coresight/coresight-tmc-etr.c:                etr_buf 
= tmc_alloc_etr_buf(drvdata, (nr_pages << PAGE_SHIFT),
drivers/hwtracing/coresight/coresight-tmc.h:    return 
sg_table->data_pages.nr_pages << PAGE_SHIFT;

Are you able to fix all of them ?

Suzuki
Ruidong Tian Aug. 4, 2023, 3:06 a.m. UTC | #2
Sure, I will put them all to patch v3.

Ruidong

On 2023/8/4 00:19, Suzuki K Poulose wrote:
> On 03/08/2023 03:33, Ruidong Tian wrote:
>> Perf cs_etm session executed unexpectedly when AUX buffer > 1G.
>>
>>    perf record -C 0 -m ,2G -e cs_etm// -- <workload>
>>    [ perf record: Captured and wrote 2.615 MB perf.data ]
>>
>> Perf only collect about 2M perf data rather than 2G. This is becasuse
>> the operation, "nr_pages << PAGE_SHIFT", in coresight tmc driver, will
>> overflow when nr_pages >= 0x80000(correspond to 1G AUX buffer). The
>> overflow cause buffer allocation to fail, and TMC driver will alloc
>> minimal buffer size(1M). You can just get about 2M perf data(1M AUX
>> buffer + perf data header) at least.
>>
>> Explicit convert nr_pages to 64 bit to avoid overflow.
>>
>> Signed-off-by: Ruidong Tian <tianruidong@linux.alibaba.com>
>> Reviewed-by: James Clark <james.clark@arm.com>
>
> Fixes: 22f429f19c41 ("coresight: etm-perf: Add support for ETR backend")
>
>
>> ---
>>   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);
>
> There are other places where such a fix is needed.
> e.g.,
>
> $ git grep "nr_pages << PAGE_SHIFT" 
> drivers/hwtracing/coresight/coresight-tmc*
> drivers/hwtracing/coresight/coresight-tmc-etf.c:        head = 
> handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
> drivers/hwtracing/coresight/coresight-tmc-etr.c:#define 
> PERF_IDX2OFF(idx, buf)  ((idx) % ((buf)->nr_pages << PAGE_SHIFT))
> drivers/hwtracing/coresight/coresight-tmc-etr.c:        if ((nr_pages 
> << PAGE_SHIFT) > drvdata->size) {
> drivers/hwtracing/coresight/coresight-tmc-etr.c: etr_buf = 
> tmc_alloc_etr_buf(drvdata, (nr_pages << PAGE_SHIFT),
> drivers/hwtracing/coresight/coresight-tmc.h:    return 
> sg_table->data_pages.nr_pages << PAGE_SHIFT;
>
> Are you able to fix all of them ?
>
> Suzuki
diff mbox series

Patch

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);