Skip to contentSkip to conversation

drm/gud: avoid divide error in gud_stats_debugfs

PATCH Applied YuanZhe Liu 2 participants 3 messages Oct 29 – Nov 12

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

PATCH 1/1 drm/gud: avoid divide error in gud_stats_debugfs

gud_stats_debugfs() computes the compression ratio by dividing the accumulated uncompressed length by stats_actual_length. That counter is zero until the first framebuffer flush has completed, so reading the stats debugfs file on a display which negotiates LZ4 compression raises a divide error before any frame has been sent. The wrap-around reset in gud_flush_rect() also clears the counter for a moment, which a concurrent read can observe.

Copy the counter into a local variable and skip the ratio computation when it is zero.

Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")
---
 drivers/gpu/drm/gud/gud_drv.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/gud/gud_drv.c +10 -3
index 3a1b9e2a2eaa..85ddb302d2ba 100644
@@ -329,10 +329,17 @@ static int gud_stats_debugfs(struct seq_file *m, void *data)
 	seq_puts(m, "\n");
 
 	if (gdrm->compression) {
+		u64 actual_length = gdrm->stats_actual_length;
 		u64 remainder;
-		u64 ratio = div64_u64_rem(gdrm->stats_length, gdrm->stats_actual_length,
-					  &remainder);
-		u64 ratio_frac = div64_u64(remainder * 10, gdrm->stats_actual_length);
+		u64 ratio, ratio_frac;
+
+		if (!actual_length) {
+			seq_puts(m, "Compression ratio: n/a\n");
+			return 0;
+		}
+
+		ratio = div64_u64_rem(gdrm->stats_length, actual_length, &remainder);
+		ratio_frac = div64_u64(remainder * 10, actual_length);
 
 		seq_printf(m, "Compression ratio: %llu.%llu\n", ratio, ratio_frac);
 	}
Signature · 1 line
2.34.1
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 14 lines · YuanZhe Liu

gud_stats_debugfs() computes the compression ratio by dividing the accumulated uncompressed length by stats_actual_length. That counter is
zero until the first framebuffer flush has completed, so reading the stats debugfs file on a display which negotiates LZ4 compression raises
a divide error before any frame has been sent. The wrap-around reset in
gud_flush_rect() also clears the counter for a moment, which a concurrent read can observe.

Copy the counter into a local variable and skip the ratio computation when it is zero.

Quoted 7 lines
Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")

Sashiko highlighted a potential issue with reading stats_actual_length twice when a flush_rect occurs between them. This *could* still trigger a divide error, however, this implementation is still better than current. The best thing to do would be to put a READ_ONCE when stats_actual_length is read.

This patch is better than the current implementation, and I don't think many compilers would optimise it to two memory reads? But it is possible, and compiler optimisation is a fairly complicated topic, so if you do wish you can add the READ_ONCE, but I'm also happy for it to go in without, as it is better than current.

Ruben

Quoted 36 lines

---
 drivers/gpu/drm/gud/gud_drv.c | 13 ++++++++++---

 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/gud/gud_drv.c +0 -0
index 3a1b9e2a2eaa..85ddb302d2ba 100644
@@ -329,10 +329,17 @@ static int gud_stats_debugfs(struct seq_file
 *m, void *data)
  	seq_puts(m, "\n");


  if (gdrm->compression) {

drivers/gpu/drm/gud/gud_drv.c +1 -0
+		u64 actual_length = gdrm->stats_actual_length;

  u64 remainder;

drivers/gpu/drm/gud/gud_drv.c +0 -1
-		u64 ratio = div64_u64_rem(gdrm->stats_length, gdrm-
Quoted 1 line

stats_actual_length,

drivers/gpu/drm/gud/gud_drv.c +0 -2
-					  &remainder);
-		u64 ratio_frac = div64_u64(remainder * 10, gdrm-
Quoted 1 line

stats_actual_length);

drivers/gpu/drm/gud/gud_drv.c +8 -0
+		u64 ratio, ratio_frac;
+
+		if (!actual_length) {
+			seq_puts(m, "Compression ratio: n/a\n");
+			return 0;
+		}
+
+		ratio = div64_u64_rem(gdrm->stats_length,

actual_length, &remainder);

drivers/gpu/drm/gud/gud_drv.c +1 -0
+		ratio_frac = div64_u64(remainder * 10,

actual_length);

  seq_printf(m, "Compression ratio: %llu.%llu\n", ratio, ratio_frac);
  }

maintainer Applied
#2 lore raw
Quoted 19 lines · Ruben Wauters
Quoted 18 lines · YuanZhe Liu

gud_stats_debugfs() computes the compression ratio by dividing the accumulated uncompressed length by stats_actual_length. That counter
is
zero until the first framebuffer flush has completed, so reading the
stats debugfs file on a display which negotiates LZ4 compression raises
a divide error before any frame has been sent. The wrap-around reset
in
gud_flush_rect() also clears the counter for a moment, which a concurrent read can observe.

Copy the counter into a local variable and skip the ratio computation
when it is zero.

Quoted 8 lines
Quoted 7 lines
Fixes 40e1a70b4aed ("drm: Add GUD USB Display driver")

Applied to drm-misc-fixes, thanks for the patch

Quoted 55 lines

Sashiko highlighted a potential issue with reading stats_actual_length
twice when a flush_rect occurs between them. This *could* still trigger
a divide error, however, this implementation is still better than current. The best thing to do would be to put a READ_ONCE when stats_actual_length is read.

This patch is better than the current implementation, and I don't think
many compilers would optimise it to two memory reads? But it is possible, and compiler optimisation is a fairly complicated topic, so if you do wish you can add the READ_ONCE, but I'm also happy for it to
go in without, as it is better than current.

Ruben

Quoted 37 lines

---
 drivers/gpu/drm/gud/gud_drv.c | 13 ++++++++++---

 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/gud/gud_drv.c +0 -0
index 3a1b9e2a2eaa..85ddb302d2ba 100644
@@ -329,10 +329,17 @@ static int gud_stats_debugfs(struct seq_file
 *m, void *data)
  	seq_puts(m, "\n");


  if (gdrm->compression) {

drivers/gpu/drm/gud/gud_drv.c +1 -0
+		u64 actual_length = gdrm->stats_actual_length;

  u64 remainder;

drivers/gpu/drm/gud/gud_drv.c +0 -1
-		u64 ratio = div64_u64_rem(gdrm->stats_length,

gdrm-

Quoted 1 line

stats_actual_length,

drivers/gpu/drm/gud/gud_drv.c +0 -2
-					  &remainder);
-		u64 ratio_frac = div64_u64(remainder * 10, gdrm-
Quoted 1 line

stats_actual_length);

drivers/gpu/drm/gud/gud_drv.c +8 -0
+		u64 ratio, ratio_frac;
+
+		if (!actual_length) {
+			seq_puts(m, "Compression ratio: n/a\n");
+			return 0;
+		}
+
+		ratio = div64_u64_rem(gdrm->stats_length,

actual_length, &remainder);

drivers/gpu/drm/gud/gud_drv.c +1 -0
+		ratio_frac = div64_u64(remainder * 10,

actual_length);

  seq_printf(m, "Compression ratio: %llu.%llu\n", ratio, ratio_frac);
  }