From 2e7005654d76b71f78fe07fcf98a3570022f5034 Mon Sep 17 00:00:00 2001 From: Thomas Zimmermann Date: Fri, 10 Jan 2025 09:35:12 +0100 Subject: [PATCH] add fallback for unknown bpp --- drivers/gpu/drm/drm_dumb_buffers.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/drm_dumb_buffers.c b/drivers/gpu/drm/drm_dumb_buffers.c index fd39720bd617..5f2d026c764c 100644 --- a/drivers/gpu/drm/drm_dumb_buffers.c +++ b/drivers/gpu/drm/drm_dumb_buffers.c @@ -119,9 +119,8 @@ int drm_mode_size_dumb(struct drm_device *dev, unsigned long pitch_align, unsigned long size_align) { + u64 pitch = 0; u32 fourcc; - const struct drm_format_info *info; - u64 pitch; /* * The scanline pitch depends on the buffer width and the color @@ -135,12 +134,25 @@ int drm_mode_size_dumb(struct drm_device *dev, * color-mode constant. */ fourcc = drm_driver_color_mode_format(dev, args->bpp); - if (fourcc == DRM_FORMAT_INVALID) - return -EINVAL; - info = drm_format_info(fourcc); - if (!info) - return -EINVAL; - pitch = drm_format_info_min_pitch(info, 0, args->width); + if (fourcc != DRM_FORMAT_INVALID) { + const struct drm_format_info *info = drm_format_info(fourcc); + + if (!info) + return -EINVAL; + pitch = drm_format_info_min_pitch(info, 0, args->width); + } else if (args->bpp) { + /* + * Some userspace throws in arbitrary values for bpp and + * relies on the kernel to figure it out. In this case we + * fall back to the old method of using bpp directly. + */ + drm_warn(dev, "Unknown color mode %d; guessing buffer size.\n", args->bpp); + if (args->bpp < 8) + pitch = DIV_ROUND_UP(args->width * args->bpp, SZ_8); + else + pitch = args->width * DIV_ROUND_UP(args->bpp, SZ_8); + } + if (!pitch || pitch > U32_MAX) return -EINVAL; -- 2.47.1