Skip to contentSkip to conversation

coresight: tmc: Explicit type conversions to prevent integer overflow

PATCH RESEND Superseded Ruidong Tian 3 participants 10 messages Jul 14, 2023 – Aug 4, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 coresight: tmc: Explicit type conversions to prevent integer overflow

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.

---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +-
 drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
index 766325de0e29..1425ecd1cf78 100644
@@ -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;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index b97da39652d2..0ee48c5ba764 100644
@@ -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);
Signature · 1 line
2.33.1
#1 lore raw
Quoted 9 lines · Ruidong Tian

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/[email protected]/

Thanks
James

Quoted 32 lines
---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +-
 drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
index 766325de0e29..1425ecd1cf78 100644
@@ -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;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index b97da39652d2..0ee48c5ba764 100644
@@ -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);
#2 lore raw

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/[email protected]/

Thanks
Ruidong

Quoted 64 lines · James Clark
Quoted 9 lines · Ruidong Tian

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/[email protected]/

Thanks
James

Quoted 32 lines
---
  drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +-
  drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
index 766325de0e29..1425ecd1cf78 100644
@@ -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;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index b97da39652d2..0ee48c5ba764 100644
@@ -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);
Reviewed-by
#3 lore raw
Quoted 43 lines · Ruidong Tian

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

Quoted 7 lines

+ 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.

Thanks for the investigation, that makes more sense to me now. Are you able to send a v2 of the patch with an updated commit message describing these symptoms instead?

And you can also add:

Quoted 78 lines

[1]:
https://lore.kernel.org/bpf/[email protected]/

Thanks
Ruidong

Quoted 70 lines · James Clark
Quoted 10 lines · Ruidong Tian

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/[email protected]/

Thanks
James

Quoted 37 lines

---
  drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +- drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-

  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
index 766325de0e29..1425ecd1cf78 100644
@@ -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;

drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index b97da39652d2..0ee48c5ba764 100644
@@ -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);

#4 lore raw

PATCH v2 1/1 coresight: tmc: Explicit type conversions to prevent integer overflow

In reply to Ruidong Tian

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.

---
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +-
 drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
index 766325de0e29..1425ecd1cf78 100644
@@ -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;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index b97da39652d2..0ee48c5ba764 100644
@@ -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);
Signature · 7 lines
2.33.1


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
maintainer
#5 lore raw
Quoted 12 lines · Ruidong Tian

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.

Quoted 4 lines

Explicit convert nr_pages to 64 bit to avoid overflow.

Fixes 22f429f19c41 ("coresight: etm-perf: Add support for ETR backend")
Quoted 23 lines
---
  drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +-
  drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
index 766325de0e29..1425ecd1cf78 100644
@@ -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;
drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index b97da39652d2..0ee48c5ba764 100644

drivers/hwtracing/coresight/coresight-tmc.h ssize_t tmc_sg_table_get_data(struct tmc_sg_table *sg_table, · line 331

Quoted 8 lines
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
@@ -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

#6 lore raw

Sure, I will put them all to patch v3.

Ruidong

Quoted 65 lines · Suzuki K Poulose
Quoted 12 lines · Ruidong Tian

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.

Quoted 4 lines

Explicit convert nr_pages to 64 bit to avoid overflow.

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

---
  drivers/hwtracing/coresight/coresight-tmc-etr.c | 2 +- drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-

  2 files changed, 2 insertions(+), 2 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etr.c +1 -1
index 766325de0e29..1425ecd1cf78 100644
@@ -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;

drivers/hwtracing/coresight/coresight-tmc.h +0 -0
index b97da39652d2..0ee48c5ba764 100644
@@ -325,7 +325,7 @@ ssize_t tmc_sg_table_get_data(struct tmc_sg_table 
 *sg_table,
Quoted 7 lines

  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;
Quoted 3 lines

Are you able to fix all of them ?

Suzuki

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#7 lore raw

v3 cover coresight: tmc: Explicit type conversions to

In reply to Ruidong Tian

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 in tmc driver to avoid overflow.

Changes in V2:
Suzuki K Poulose:
Add another 2 places where needed to fix.

Changes in V1:
James Clark:
Use a more accurate commit message.

Ruidong Tian (1):
coresight: tmc: Explicit type conversions to prevent integer overflow

 drivers/hwtracing/coresight/coresight-tmc-etf.c | 2 +-
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 5 +++--
 drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
Signature · 1 line
2.33.1
#8 lore raw

PATCH v3 1/1 coresight: tmc: Explicit type conversions to prevent integer overflow

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.

Fixes 22f429f19c41 ("coresight: etm-perf: Add support for ETR backend")
---
 drivers/hwtracing/coresight/coresight-tmc-etf.c | 2 +-
 drivers/hwtracing/coresight/coresight-tmc-etr.c | 5 +++--
 drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)
drivers/hwtracing/coresight/coresight-tmc-etf.c +1 -1
index 79d8c64eac49..7406b65e2cdd 100644
@@ -452,7 +452,7 @@ static int tmc_set_etf_buffer(struct coresight_device *csdev,
 		return -EINVAL;
 
 	/* wrap head around to the amount of space we have */
-	head = handle->head & ((buf->nr_pages << PAGE_SHIFT) - 1);
+	head = handle->head & (((unsigned long)buf->nr_pages << PAGE_SHIFT) - 1);
 
 	/* find the page to write to */
 	buf->cur = head / PAGE_SIZE;
drivers/hwtracing/coresight/coresight-tmc-etr.c +3 -2
index 766325de0e29..66dc5f97a009 100644
@@ -45,7 +45,8 @@ struct etr_perf_buffer {
 };
 
 /* Convert the perf index to an offset within the ETR buffer */
-#define PERF_IDX2OFF(idx, buf)	((idx) % ((buf)->nr_pages << PAGE_SHIFT))
+#define PERF_IDX2OFF(idx, buf)		\
+		((idx) % ((unsigned long)(buf)->nr_pages << PAGE_SHIFT))
 
 /* Lower limit for ETR hardware buffer */
 #define TMC_ETR_PERF_MIN_BUF_SIZE	SZ_1M
@@ -1267,7 +1268,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;
drivers/hwtracing/coresight/coresight-tmc.h +1 -1
index b97da39652d2..0ee48c5ba764 100644
@@ -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);
Signature · 1 line
2.33.1
maintainer
#9 lore raw
Quoted 14 lines · Ruidong Tian

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.

Quoted 8 lines
Fixes 22f429f19c41 ("coresight: etm-perf: Add support for ETR backend")
---
  drivers/hwtracing/coresight/coresight-tmc-etf.c | 2 +-
  drivers/hwtracing/coresight/coresight-tmc-etr.c | 5 +++--
  drivers/hwtracing/coresight/coresight-tmc.h     | 2 +-
  3 files changed, 5 insertions(+), 4 deletions(-)

Added additional Fixes tags as required and pushed it here :

https://git.kernel.org/coresight/c/fd380097cdb3

Thanks!
Suzuki

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel