Skip to contentSkip to conversation

drm/i915: prevent division by zero when asking for chipset power

PATCH In review Eugeni Dodonov 3 participants 4 messages Nov 10, 2011 – Nov 11, 2011

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

PATCH 1/1 drm/i915: prevent division by zero when asking for chipset power

This prevents an in-kernel division by zero which happens when we are asking for i915_chipset_val too quickly, or within a race condition between the power monitoring thread and userspace accesses via debugfs.

The issue can be reproduced easily via the following command:
while ``; do cat /sys/kernel/debug/dri/0/i915_emon_status; done

This is particularly dangerous because it can be triggered by a non-privileged user by just reading the debugfs entry.

This issue was also found independently by Konstantin Belousov <[email protected]>, who proposed a similar patch.

---
 drivers/gpu/drm/i915/i915_dma.c |   10 ++++++++++
 drivers/gpu/drm/i915/i915_drv.h |    1 +
 2 files changed, 11 insertions(+), 0 deletions(-)
drivers/gpu/drm/i915/i915_dma.c +10 -0
index 8a3942c..de199e7 100644
@@ -1453,6 +1453,14 @@ unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
 
 	diff1 = now - dev_priv->last_time1;
 
+	/* Prevent division-by-zero if we are asking too fast.
+	 * Also, we don't get interesting results in we are polling
+	 * faster than once in 10ms, so just return the saved value
+	 * in such cases.
+	 */
+	if (diff1 <= 10)
+		return dev_priv->chipset_power;
+
 	count1 = I915_READ(DMIEC);
 	count2 = I915_READ(DDREC);
 	count3 = I915_READ(CSIEC);
@@ -1483,6 +1491,8 @@ unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
 	dev_priv->last_count1 = total_count;
 	dev_priv->last_time1 = now;
 
+	dev_priv->chipset_power = ret;
+
 	return ret;
 }
 
drivers/gpu/drm/i915/i915_drv.h +1 -0
index 7916bd9..1a2a2d1 100644
@@ -707,6 +707,7 @@ typedef struct drm_i915_private {
 
 	u64 last_count1;
 	unsigned long last_time1;
+	unsigned long chipset_power;
 	u64 last_count2;
 	struct timespec last_time2;
 	unsigned long gfx_power;
Signature · 1 line
1.7.7.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Eugeni Dodonov

This prevents an in-kernel division by zero which happens when we are asking for i915_chipset_val too quickly, or within a race condition between the power monitoring thread and userspace accesses via debugfs.

The issue can be reproduced easily via the following command:
while ``; do cat /sys/kernel/debug/dri/0/i915_emon_status; done

This is particularly dangerous because it can be triggered by a non-privileged user by just reading the debugfs entry.

Quoted 8 lines

This issue was also found independently by Konstantin Belousov <[email protected]>, who proposed a similar patch.

Just spotted a typo...

Quoted 10 lines
---
 drivers/gpu/drm/i915/i915_dma.c |   10 ++++++++++
 drivers/gpu/drm/i915/i915_drv.h |    1 +
 2 files changed, 11 insertions(+), 0 deletions(-)
drivers/gpu/drm/i915/i915_dma.c +0 -0
index 8a3942c..de199e7 100644

drivers/gpu/drm/i915/i915_dma.c unsigned long i915_chipset_val(struct drm_i915_private *dev_priv) · line 1457

Quoted 5 lines
drivers/gpu/drm/i915/i915_dma.c +2 -0
@@ -1453,6 +1453,14 @@ unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
 
 	diff1 = now - dev_priv->last_time1;
 
+	/* Prevent division-by-zero if we are asking too fast.
+	 * Also, we don't get interesting results in we are polling

s/in/if/
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#2 lore raw

PATCH 1/1 drm/i915: prevent division by zero when asking for chipset power

This prevents an in-kernel division by zero which happens when we are asking for i915_chipset_val too quickly, or within a race condition between the power monitoring thread and userspace accesses via debugfs.

The issue can be reproduced easily via the following command:
while ``; do cat /sys/kernel/debug/dri/0/i915_emon_status; done

This is particularly dangerous because it can be triggered by a non-privileged user by just reading the debugfs entry.

This issue was also found independently by Konstantin Belousov <[email protected]>, who proposed a similar patch.

---
 drivers/gpu/drm/i915/i915_dma.c |   10 ++++++++++
 drivers/gpu/drm/i915/i915_drv.h |    1 +
 2 files changed, 11 insertions(+), 0 deletions(-)
drivers/gpu/drm/i915/i915_dma.c +10 -0
index 8a3942c..c72b590 100644
@@ -1453,6 +1453,14 @@ unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
 
 	diff1 = now - dev_priv->last_time1;
 
+	/* Prevent division-by-zero if we are asking too fast.
+	 * Also, we don't get interesting results if we are polling
+	 * faster than once in 10ms, so just return the saved value
+	 * in such cases.
+	 */
+	if (diff1 <= 10)
+		return dev_priv->chipset_power;
+
 	count1 = I915_READ(DMIEC);
 	count2 = I915_READ(DDREC);
 	count3 = I915_READ(CSIEC);
@@ -1483,6 +1491,8 @@ unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
 	dev_priv->last_count1 = total_count;
 	dev_priv->last_time1 = now;
 
+	dev_priv->chipset_power = ret;
+
 	return ret;
 }
 
drivers/gpu/drm/i915/i915_drv.h +1 -0
index 7916bd9..1a2a2d1 100644
@@ -707,6 +707,7 @@ typedef struct drm_i915_private {
 
 	u64 last_count1;
 	unsigned long last_time1;
+	unsigned long chipset_power;
 	u64 last_count2;
 	struct timespec last_time2;
 	unsigned long gfx_power;
Signature · 1 line
1.7.7.1
#3 lore raw
In reply to Eugeni Dodonov
Quoted 13 lines · On Thu, 10 Nov 2011 10:51:26 -0200 Eugeni Dodonov <[email protected]>…

This prevents an in-kernel division by zero which happens when we are asking for i915_chipset_val too quickly, or within a race condition between the power monitoring thread and userspace accesses via debugfs.

The issue can be reproduced easily via the following command:
while ``; do cat /sys/kernel/debug/dri/0/i915_emon_status; done

This is particularly dangerous because it can be triggered by a non-privileged user by just reading the debugfs entry.

This issue was also found independently by Konstantin Belousov <[email protected]>, who proposed a similar patch.

Quoted 6 lines

---

Yep, looks fine, thanks for pushing this Eugeni.

Signature · 1 line
Jesse Barnes, Intel Open Source Technology Center

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx