Skip to contentSkip to conversation

Check for valid framebuffer's format

PATCH In review Maíra Canal 2 participants 8 messages Jan 13, 2023 – Jan 17, 2023

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

v3 cover Check for valid framebuffer's format

This series is a follow-up of the [1] in which I introduced a check for valid formats on drm_gem_fb_create(). During the discussion, I realized that would be a better idea to put the check inside framebuffer_check() so that it wouldn't be needed to hit any driver-specific code path when the check fails.

Therefore, add the valid format check inside framebuffer_check() and remove the same check from the drivers, except from i915, because this doesn't work for the legacy tiling->modifier path. Adding the check to framebuffer_check() will guarantee that the igt@kms_addfb_basic@addfb25-bad-modifier IGT test passes, showing the correct behavior of the check.

This patchset was tested on amdgpu and vc4 with the IGT tests.

[1] https://lore.kernel.org/dri-devel/[email protected]/T/

---

v1 -> v2: https://lore.kernel.org/dri-devel/[email protected]/T/

- Don't remove check from i915 driver (Ville Syrjälä).
- Don't unexport drm_any_plane_has_format().

v2 -> v3: https://lore.kernel.org/dri-devel/[email protected]/T/

- Check if r->modifier[0] != 0 (Ville Syrjälä).

---

Best Regards,
- Maíra Canal

Maíra Canal (3):
drm/framebuffer: Check for valid formats drm/amdgpu: Remove redundant framebuffer format check drm/vmwgfx: Remove redundant framebuffer format check

 Documentation/gpu/todo.rst                  |  9 ++++-----
 drivers/gpu/drm/amd/amdgpu/amdgpu_display.c | 10 ----------
 drivers/gpu/drm/drm_framebuffer.c           |  8 ++++++++
 drivers/gpu/drm/vmwgfx/vmwgfx_kms.c         | 18 ------------------
 4 files changed, 12 insertions(+), 33 deletions(-)
Signature · 1 line
2.39.0
#1 lore raw

PATCH v3 1/3 drm/framebuffer: Check for valid formats

Currently, framebuffer_check() doesn't check if the pixel format is supported, which can lead to the acceptance of invalid pixel formats e.g. the acceptance of invalid modifiers. Therefore, add a check for valid formats on framebuffer_check(), so that the ADDFB2 IOCTL rejects calls with invalid formats.

Moreover, note that this check is only valid for atomic drivers, because, for non-atomic drivers, checking drm_any_plane_has_format() is not possible since the format list for the primary plane is fake, and we'd therefore reject valid formats.

---
 Documentation/gpu/todo.rst        | 9 ++++-----
 drivers/gpu/drm/drm_framebuffer.c | 8 ++++++++
 2 files changed, 12 insertions(+), 5 deletions(-)
Documentation/gpu/todo.rst +4 -5
index 1f8a5ebe188e..3a79c26c5cc7 100644
@@ -276,11 +276,10 @@ Various hold-ups:
 - Need to switch to drm_fbdev_generic_setup(), otherwise a lot of the custom fb
   setup code can't be deleted.
 
-- Many drivers wrap drm_gem_fb_create() only to check for valid formats. For
-  atomic drivers we could check for valid formats by calling
-  drm_plane_check_pixel_format() against all planes, and pass if any plane
-  supports the format. For non-atomic that's not possible since like the format
-  list for the primary plane is fake and we'd therefor reject valid formats.
+- Need to switch to drm_gem_fb_create(), as now framebuffer_check() checks for
+  valid formats for atomic drivers.
+
+- Add an addfb format validation for non-atomic drivers.
 
 - Many drivers subclass drm_framebuffer, we'd need a embedding compatible
   version of the varios drm_gem_fb_create functions. Maybe called
drivers/gpu/drm/drm_framebuffer.c +8 -0
index aff3746dedfb..0afc9e39188a 100644
@@ -280,6 +280,14 @@ static int framebuffer_check(struct drm_device *dev,
 		}
 	}
 
+	/* Verify that the modifier is supported. */
+	if (r->modifier[0] && drm_drv_uses_atomic_modeset(dev) &&
+	    !drm_any_plane_has_format(dev, r->pixel_format, r->modifier[0])) {
+		drm_dbg_kms(dev, "Unsupported pixel format %p4cc / modifier 0x%llx\n",
+			    &r->pixel_format, r->modifier[0]);
+		return -EINVAL;
+	}
+
 	return 0;
 }
 
Signature · 1 line
2.39.0
#2 lore raw

Re: [PATCH v3 1/3] drm/framebuffer: Check for valid formats

drivers/gpu/drm/drm_framebuffer.c static int framebuffer_check(struct drm_device *dev, · line 289

Quoted 7 lines · Maíra Canal
drivers/gpu/drm/drm_framebuffer.c +7 -0
+	/* Verify that the modifier is supported. */
+	if (r->modifier[0] && drm_drv_uses_atomic_modeset(dev) &&
+	    !drm_any_plane_has_format(dev, r->pixel_format, r->modifier[0])) {
+		drm_dbg_kms(dev, "Unsupported pixel format %p4cc / modifier 0x%llx\n",
+			    &r->pixel_format, r->modifier[0]);
+		return -EINVAL;
+	}

User-space indicates whether there is a modifier in the IOCTL data by supplying the DRM_MODE_FB_MODIFIERS flag. I believe we need to check that flag instead of r->modifier[0]: the zero modifier is a valid modifier (LINEAR).

#3 lore raw

PATCH v3 2/3 drm/amdgpu: Remove redundant framebuffer format check

In reply to Maíra Canal

Now that framebuffer_check() verifies that the format is properly supported, there is no need to check it again on amdgpu's inside helpers.

Therefore, remove the redundant framebuffer format check from the amdgpu_display_gem_fb_verify_and_init() function, letting framebuffer_check() perform the framebuffer validation.

---
 drivers/gpu/drm/amd/amdgpu/amdgpu_display.c | 10 ----------
 1 file changed, 10 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_display.c +0 -10
index b22471b3bd63..611b7a4b086c 100644
@@ -1120,16 +1120,6 @@ static int amdgpu_display_gem_fb_verify_and_init(struct drm_device *dev,
 
 	rfb->base.obj[0] = obj;
 	drm_helper_mode_fill_fb_struct(dev, &rfb->base, mode_cmd);
-	/* Verify that the modifier is supported. */
-	if (!drm_any_plane_has_format(dev, mode_cmd->pixel_format,
-				      mode_cmd->modifier[0])) {
-		drm_dbg_kms(dev,
-			    "unsupported pixel format %p4cc / modifier 0x%llx\n",
-			    &mode_cmd->pixel_format, mode_cmd->modifier[0]);
-
-		ret = -EINVAL;
-		goto err;
-	}
 
 	ret = amdgpu_display_framebuffer_init(dev, rfb, mode_cmd, obj);
 	if (ret)
Signature · 1 line
2.39.0
#4 lore raw

Re: [PATCH v3 2/3] drm/amdgpu: Remove redundant framebuffer format check

Hm, unfortunately I think we need to keep the check in amdgpu for the same reason as i915: amdgpu will pick a modifier if user-space didn't supply one on GFX9+.

I wonder if that also applies to vmwgfx? Maybe that would be a reason to have the check in framebuffer_init()? (Not sure!)

#5 lore raw

Re: [PATCH v3 2/3] drm/amdgpu: Remove redundant framebuffer format check

Quoted 6 lines · Simon Ser

Hm, unfortunately I think we need to keep the check in amdgpu for the same reason as i915: amdgpu will pick a modifier if user-space didn't supply one on GFX9+.

I wonder if that also applies to vmwgfx? Maybe that would be a reason to have the check in framebuffer_init()? (Not sure!)

Considering that we could then remove the check from i915 and amdgpu if we move the check to framebuffer_init(), I believe that this would be a good reason to perform the check there. I'll send a v4 including the check on framebuffer_init() and removing the check from i915.

Thanks for the feedback!

Best Regards,
- Maíra Canal

#6 lore raw

Re: [PATCH v3 2/3] drm/amdgpu: Remove redundant framebuffer format check

In reply to Simon Ser

Hi Simon,

Quoted 6 lines · Simon Ser

Hm, unfortunately I think we need to keep the check in amdgpu for the same reason as i915: amdgpu will pick a modifier if user-space didn't supply one on GFX9+.

I wonder if that also applies to vmwgfx? Maybe that would be a reason to have the check in framebuffer_init()? (Not sure!)

I tried to move the check to framebuffer_init(), but it ended up causing problems in the i915 driver (the kernel was emitting warnings when running the IGT tests). I was thinking of going back to the drm_gem_fb_create() approach [1], as it would make the other drivers return EINVAL in the case of a bad modifier and it wouldn't change the current behavior of i915 and amdgpu.

[1] https://lore.kernel.org/dri-devel/[email protected]/T/

Best Regards,
- Maíra Canal

#7 lore raw

PATCH v3 3/3 drm/vmwgfx: Remove redundant framebuffer format check

In reply to Maíra Canal

Now that framebuffer_check() verifies that the format is properly supported, there is no need to check it again on vmwgfx's inside helpers.

Therefore, remove the redundant framebuffer format check from the vmw_kms_new_framebuffer_surface() and vmw_kms_new_framebuffer_bo() functions, letting framebuffer_check() perform the framebuffer validation.

---
 drivers/gpu/drm/vmwgfx/vmwgfx_kms.c | 18 ------------------
 1 file changed, 18 deletions(-)
drivers/gpu/drm/vmwgfx/vmwgfx_kms.c +0 -18
index 257f090071f1..05b8d8f912bf 100644
@@ -1317,15 +1317,6 @@ static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
 	 * Sanity checks.
 	 */
 
-	if (!drm_any_plane_has_format(&dev_priv->drm,
-				      mode_cmd->pixel_format,
-				      mode_cmd->modifier[0])) {
-		drm_dbg(&dev_priv->drm,
-			"unsupported pixel format %p4cc / modifier 0x%llx\n",
-			&mode_cmd->pixel_format, mode_cmd->modifier[0]);
-		return -EINVAL;
-	}
-
 	/* Surface must be marked as a scanout. */
 	if (unlikely(!surface->metadata.scanout))
 		return -EINVAL;
@@ -1648,15 +1639,6 @@ static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
 		return -EINVAL;
 	}
 
-	if (!drm_any_plane_has_format(&dev_priv->drm,
-				      mode_cmd->pixel_format,
-				      mode_cmd->modifier[0])) {
-		drm_dbg(&dev_priv->drm,
-			"unsupported pixel format %p4cc / modifier 0x%llx\n",
-			&mode_cmd->pixel_format, mode_cmd->modifier[0]);
-		return -EINVAL;
-	}
-
 	vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
 	if (!vfbd) {
 		ret = -ENOMEM;
Signature · 1 line
2.39.0