Skip to contentSkip to conversation

drm/i915: Fix failure paths around initial fbdev allocation

PATCH In review Tvrtko Ursulin 4 participants 6 messages Jun 15, 2015 – Jun 29, 2015

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

PATCH 1/1 drm/i915: Fix failure paths around initial fbdev allocation

From: Tvrtko Ursulin <[email protected]>

We had two failure modes here:

1.
Deadlock in intelfb_alloc failure path where it calls drm_framebuffer_remove, which grabs the struct mutex and intelfb_create (caller of intelfb_alloc) was already holding it.

2.
Double unreference on the object if __intel_framebuffer_create fails since both it and the caller (intelfb_alloc) do the unreference.

Cc Chris Wilson <[email protected]>
Cc Ville Syrjälä <[email protected]>
---
 drivers/gpu/drm/i915/intel_fbdev.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/intel_fbdev.c +13 -8
index 6372cfc..b998f69 100644
@@ -141,7 +141,7 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 {
 	struct intel_fbdev *ifbdev =
 		container_of(helper, struct intel_fbdev, helper);
-	struct drm_framebuffer *fb;
+	struct drm_framebuffer *fb = NULL;
 	struct drm_device *dev = helper->dev;
 	struct drm_mode_fb_cmd2 mode_cmd = {};
 	struct drm_i915_gem_object *obj;
@@ -159,6 +159,8 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
 							  sizes->surface_depth);
 
+	mutex_lock(&dev->struct_mutex);
+
 	size = mode_cmd.pitches[0] * mode_cmd.height;
 	size = PAGE_ALIGN(size);
 	obj = i915_gem_object_create_stolen(dev, size);
@@ -172,8 +174,9 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 
 	fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
 	if (IS_ERR(fb)) {
+		/* Drops object reference on failure. */
 		ret = PTR_ERR(fb);
-		goto out_unref;
+		goto out;
 	}
 
 	/* Flush everything out, we'll be doing GTT only from now on */
@@ -183,15 +186,18 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 		goto out_fb;
 	}
 
+	mutex_unlock(&dev->struct_mutex);
+
 	ifbdev->fb = to_intel_framebuffer(fb);
 
 	return 0;
 
 out_fb:
-	drm_framebuffer_remove(fb);
-out_unref:
 	drm_gem_object_unreference(&obj->base);
 out:
+	mutex_unlock(&dev->struct_mutex);
+	if (fb)
+		drm_framebuffer_remove(fb);
 	return ret;
 }
 
@@ -209,8 +215,6 @@ static int intelfb_create(struct drm_fb_helper *helper,
 	int size, ret;
 	bool prealloc = false;
 
-	mutex_lock(&dev->struct_mutex);
-
 	if (intel_fb &&
 	    (sizes->fb_width > intel_fb->base.width ||
 	     sizes->fb_height > intel_fb->base.height)) {
@@ -225,7 +229,7 @@ static int intelfb_create(struct drm_fb_helper *helper,
 		DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
 		ret = intelfb_alloc(helper, sizes);
 		if (ret)
-			goto out_unlock;
+			return ret;
 		intel_fb = ifbdev->fb;
 	} else {
 		DRM_DEBUG_KMS("re-using BIOS fb\n");
@@ -237,6 +241,8 @@ static int intelfb_create(struct drm_fb_helper *helper,
 	obj = intel_fb->obj;
 	size = obj->base.size;
 
+	mutex_lock(&dev->struct_mutex);
+
 	info = framebuffer_alloc(0, &dev->pdev->dev);
 	if (!info) {
 		ret = -ENOMEM;
@@ -307,7 +313,6 @@ static int intelfb_create(struct drm_fb_helper *helper,
 out_unpin:
 	i915_gem_object_ggtt_unpin(obj);
 	drm_gem_object_unreference(&obj->base);
-out_unlock:
 	mutex_unlock(&dev->struct_mutex);
 	return ret;
 }
Signature · 6 lines
2.4.2

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#1 lore raw
Quoted 4 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

We had two failure modes here:

Quoted 8 lines

1.
Deadlock in intelfb_alloc failure path where it calls drm_framebuffer_remove, which grabs the struct mutex and intelfb_create (caller of intelfb_alloc) was already holding it.

2.
Double unreference on the object if __intel_framebuffer_create fails since both it and the caller (intelfb_alloc) do the unreference.

I would suggest removing the unref from __intel_framebuffer_create().

Quoted 99 lines
Cc Chris Wilson <[email protected]>
Cc Ville Syrjälä <[email protected]>
---
 drivers/gpu/drm/i915/intel_fbdev.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/intel_fbdev.c +13 -8
index 6372cfc..b998f69 100644
@@ -141,7 +141,7 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 {
 	struct intel_fbdev *ifbdev =
 		container_of(helper, struct intel_fbdev, helper);
-	struct drm_framebuffer *fb;
+	struct drm_framebuffer *fb = NULL;
 	struct drm_device *dev = helper->dev;
 	struct drm_mode_fb_cmd2 mode_cmd = {};
 	struct drm_i915_gem_object *obj;
@@ -159,6 +159,8 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
 							  sizes->surface_depth);
 
+	mutex_lock(&dev->struct_mutex);
+
 	size = mode_cmd.pitches[0] * mode_cmd.height;
 	size = PAGE_ALIGN(size);
 	obj = i915_gem_object_create_stolen(dev, size);
@@ -172,8 +174,9 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 
 	fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
 	if (IS_ERR(fb)) {
+		/* Drops object reference on failure. */
 		ret = PTR_ERR(fb);
-		goto out_unref;
+		goto out;
 	}
 
 	/* Flush everything out, we'll be doing GTT only from now on */
@@ -183,15 +186,18 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 		goto out_fb;
 	}
 
+	mutex_unlock(&dev->struct_mutex);
+
 	ifbdev->fb = to_intel_framebuffer(fb);
 
 	return 0;
 
 out_fb:
-	drm_framebuffer_remove(fb);
-out_unref:
 	drm_gem_object_unreference(&obj->base);
 out:
+	mutex_unlock(&dev->struct_mutex);
+	if (fb)
+		drm_framebuffer_remove(fb);
 	return ret;
 }
 
@@ -209,8 +215,6 @@ static int intelfb_create(struct drm_fb_helper *helper,
 	int size, ret;
 	bool prealloc = false;
 
-	mutex_lock(&dev->struct_mutex);
-
 	if (intel_fb &&
 	    (sizes->fb_width > intel_fb->base.width ||
 	     sizes->fb_height > intel_fb->base.height)) {
@@ -225,7 +229,7 @@ static int intelfb_create(struct drm_fb_helper *helper,
 		DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
 		ret = intelfb_alloc(helper, sizes);
 		if (ret)
-			goto out_unlock;
+			return ret;
 		intel_fb = ifbdev->fb;
 	} else {
 		DRM_DEBUG_KMS("re-using BIOS fb\n");
@@ -237,6 +241,8 @@ static int intelfb_create(struct drm_fb_helper *helper,
 	obj = intel_fb->obj;
 	size = obj->base.size;
 
+	mutex_lock(&dev->struct_mutex);
+
 	info = framebuffer_alloc(0, &dev->pdev->dev);
 	if (!info) {
 		ret = -ENOMEM;
@@ -307,7 +313,6 @@ static int intelfb_create(struct drm_fb_helper *helper,
 out_unpin:
 	i915_gem_object_ggtt_unpin(obj);
 	drm_gem_object_unreference(&obj->base);
-out_unlock:
 	mutex_unlock(&dev->struct_mutex);
 	return ret;
 }
Signature · 1 line
2.4.2
Signature · 6 lines
Ville Syrjälä
Intel OTC
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Tested-by on v1 1/1
#2 lore raw
In reply to Tvrtko Ursulin

Task id: 6642 -------------------------------------Summary-------------------------------------
Platform Delta drm-intel-nightly Series Applied

ILK                                  302/302              302/302
SNB                                  312/316              312/316
IVB                                  343/343              343/343

BYT -1 287/287 286/287
HSW 380/380 380/380
-------------------------------------Detailed-------------------------------------
Platform Test drm-intel-nightly Series Applied
*BYT igt@gem_tiled_partial_pwrite_pread@reads PASS(1) FAIL(1)
Note: You need to pay more attention to line start with '*'
_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx

Reviewed-by on v1 1/1
#3 lore raw
In reply to Tvrtko Ursulin

Hi,

Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

We had two failure modes here:

1.
Deadlock in intelfb_alloc failure path where it calls drm_framebuffer_remove, which grabs the struct mutex and intelfb_create (caller of intelfb_alloc) was already holding it.

s/intelfb_alloc/intelfb_create/
s/(caller of intelfb_alloc)//

Quoted 8 lines

2.
Double unreference on the object if __intel_framebuffer_create fails since both it and the caller (intelfb_alloc) do the unreference.

Cc Chris Wilson <[email protected]>
Cc Ville Syrjälä <[email protected]>
Fixes 60a5ca015ffd2aacfe5674b5a401cd2a37159e07
Quoted 45 lines
---
 drivers/gpu/drm/i915/intel_fbdev.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
drivers/gpu/drm/i915/intel_fbdev.c +7 -2
index 6372cfc..b998f69 100644
@@ -141,7 +141,7 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 {
 	struct intel_fbdev *ifbdev =
 		container_of(helper, struct intel_fbdev, helper);
-	struct drm_framebuffer *fb;
+	struct drm_framebuffer *fb = NULL;
 	struct drm_device *dev = helper->dev;
 	struct drm_mode_fb_cmd2 mode_cmd = {};
 	struct drm_i915_gem_object *obj;
@@ -159,6 +159,8 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
 							  sizes->surface_depth);
 
+	mutex_lock(&dev->struct_mutex);
+
 	size = mode_cmd.pitches[0] * mode_cmd.height;
 	size = PAGE_ALIGN(size);
 	obj = i915_gem_object_create_stolen(dev, size);
@@ -172,8 +174,9 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 
 	fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
 	if (IS_ERR(fb)) {
+		/* Drops object reference on failure. */
 		ret = PTR_ERR(fb);
-		goto out_unref;
+		goto out;
 	}
 
 	/* Flush everything out, we'll be doing GTT only from now on */
@@ -183,15 +186,18 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
 		goto out_fb;
 	}
 
+	mutex_unlock(&dev->struct_mutex);
+
 	ifbdev->fb = to_intel_framebuffer(fb);
 

drivers/gpu/drm/i915/intel_fbdev.c static int intelfb_alloc(struct drm_fb_helper *helper, · line 200

Quoted 10 lines
drivers/gpu/drm/i915/intel_fbdev.c +3 -2
 	return 0;
 
 out_fb:
-	drm_framebuffer_remove(fb);
-out_unref:
 	drm_gem_object_unreference(&obj->base);
 out:
+	mutex_unlock(&dev->struct_mutex);
+	if (fb)
+		drm_framebuffer_remove(fb);

Hm, the order of drm_gem_object_unreference() and drm_framebuffer_remove() is reversed now, could this cause any issues?

Apart from that,

I will also test the patch and report back in a bit.

Thanks a lot,

Lukas

Quoted 45 lines
drivers/gpu/drm/i915/intel_fbdev.c +3 -4
 	return ret;
 }
 
@@ -209,8 +215,6 @@ static int intelfb_create(struct drm_fb_helper *helper,
 	int size, ret;
 	bool prealloc = false;
 
-	mutex_lock(&dev->struct_mutex);
-
 	if (intel_fb &&
 	    (sizes->fb_width > intel_fb->base.width ||
 	     sizes->fb_height > intel_fb->base.height)) {
@@ -225,7 +229,7 @@ static int intelfb_create(struct drm_fb_helper *helper,
 		DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
 		ret = intelfb_alloc(helper, sizes);
 		if (ret)
-			goto out_unlock;
+			return ret;
 		intel_fb = ifbdev->fb;
 	} else {
 		DRM_DEBUG_KMS("re-using BIOS fb\n");
@@ -237,6 +241,8 @@ static int intelfb_create(struct drm_fb_helper *helper,
 	obj = intel_fb->obj;
 	size = obj->base.size;
 
+	mutex_lock(&dev->struct_mutex);
+
 	info = framebuffer_alloc(0, &dev->pdev->dev);
 	if (!info) {
 		ret = -ENOMEM;
@@ -307,7 +313,6 @@ static int intelfb_create(struct drm_fb_helper *helper,
 out_unpin:
 	i915_gem_object_ggtt_unpin(obj);
 	drm_gem_object_unreference(&obj->base);
-out_unlock:
 	mutex_unlock(&dev->struct_mutex);
 	return ret;
 }
Signature · 6 lines
2.4.2

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

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

#4 lore raw

Hi,

Quoted 7 lines · Lukas Wunner

Hi,

Quoted 4 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

We had two failure modes here:

Quoted 7 lines
Quoted 4 lines

1.
Deadlock in intelfb_alloc failure path where it calls drm_framebuffer_remove, which grabs the struct mutex and intelfb_create (caller of intelfb_alloc) was already holding it.

s/intelfb_alloc/intelfb_create/
s/(caller of intelfb_alloc)//

I looked again and don't see that I made a mistake with regards to this?

Quoted 10 lines
Quoted 7 lines

2.
Double unreference on the object if __intel_framebuffer_create fails since both it and the caller (intelfb_alloc) do the unreference.

Cc Chris Wilson <[email protected]>
Cc Ville Syrjälä <[email protected]>
Fixes 60a5ca015ffd2aacfe5674b5a401cd2a37159e07

Sorry probably my oversight, don't remember now how it all came about.

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/i915/intel_fbdev.c +2 -0
@@ -183,15 +186,18 @@ static int intelfb_alloc(struct drm_fb_helper *helper,
  		goto out_fb;
  	}
 
+	mutex_unlock(&dev->struct_mutex);
+
  	ifbdev->fb = to_intel_framebuffer(fb);
 
  	return 0;
 
Quoted 11 lines

drivers/gpu/drm/i915/intel_fbdev.c static int intelfb_alloc(struct drm_fb_helper *helper, · line 200

Quoted 8 lines
drivers/gpu/drm/i915/intel_fbdev.c +3 -2
  out_fb:
-	drm_framebuffer_remove(fb);
-out_unref:
  	drm_gem_object_unreference(&obj->base);
  out:
+	mutex_unlock(&dev->struct_mutex);
+	if (fb)
+		drm_framebuffer_remove(fb);

Hm, the order of drm_gem_object_unreference() and drm_framebuffer_remove() is reversed now, could this cause any issues?

No, that is fine.

Quoted 5 lines

Apart from that,

I will also test the patch and report back in a bit.

That will be very useful. I did not test it extensively myself, just fired it off quickly since I was briefly hitting this failure path myself.

Regards,

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

#5 lore raw

Hi Tvrtko,

Quoted 11 lines · Tvrtko Ursulin
Quoted 10 lines · Lukas Wunner
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

We had two failure modes here:

1.
Deadlock in intelfb_alloc failure path where it calls drm_framebuffer_remove, which grabs the struct mutex and intelfb_create (caller of intelfb_alloc) was already holding it.

Quoted 4 lines
Quoted 2 lines

s/intelfb_alloc/intelfb_create/
s/(caller of intelfb_alloc)//

I looked again and don't see that I made a mistake with regards to this?

Sorry, I was confused myself:

Apart from the two issues you mentioned in the commit message there's a third one fixed by this patch which could briefly be described as:

"Deadlock in intelfb_create failure path where it calls drm_framebuffer_unreference, which grabs the struct mutex and intelfb_create was already holding it."

Only that single issue was reported by me, the others were subsequently discovered by you or your colleagues.

A somewhat more elaborate description is in my original report:
http://lists.freedesktop.org/archives/intel-gfx/2015-June/067965.html

Quoted 4 lines
Quoted 1 line

I will also test the patch and report back in a bit.

That will be very useful. I did not test it extensively myself, just fired it off quickly since I was briefly hitting this failure path myself.

Okay the patch works for me (in the specific scenario on the MacBook Pro where the existing fb is discarded because it is too small and a new one is allocated).

PRTS did report some issue on Baytrail though:
http://lists.freedesktop.org/archives/intel-gfx/2015-June/069978.html

One nitpick is that your commit message exceeds 72 characters per line.

Also, Ville had posted a suggestion which was not discussed further, removing the drm_gem_object_unreference() from __intel_framebuffer_create() in favor of the one you now removed from intelfb_alloc(). (Personally I'm totally indifferent on that.)

Best regards,

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