Re: [PATCH v2] drm/log: Fix division by zero when scale module parameter is 0

From: Jani Nikula

Date: Wed Jul 29 2026 - 08:28:48 EST


On Wed, 29 Jul 2026, oushixiong1025@xxxxxxx wrote:
> From: Shixiong Ou <oushixiong@xxxxxxxxxx>
>
> The scale module parameter can be set to 0 via kernel command line or
> sysfs. When scale is 0, scaled_font_h and scaled_font_w become 0,
> causing a division by zero in the rows/columns calculation.
>
> Introduce a drm_log_scale() helper that returns scale ?: 1, and use it
> at all read sites. This avoids a race that a setter-based clamp would
> have between param_set_uint() and the subsequent check, where another
> CPU could observe scale == 0.
>
> Signed-off-by: Shixiong Ou <oushixiong@xxxxxxxxxx>
> ---
> v1->v2:
> Introduce a drm_log_scale() helper.
>
> drivers/gpu/drm/clients/drm_log.c | 15 ++++++++++-----
> 1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/gpu/drm/clients/drm_log.c b/drivers/gpu/drm/clients/drm_log.c
> index 294b3be1a6b3..9522c1344123 100644
> --- a/drivers/gpu/drm/clients/drm_log.c
> +++ b/drivers/gpu/drm/clients/drm_log.c
> @@ -29,6 +29,11 @@ static unsigned int scale = 1;
> module_param(scale, uint, 0444);
> MODULE_PARM_DESC(scale, "Integer scaling factor for drm_log, default is 1");
>
> +static inline unsigned int drm_log_scale(void)

Please don't use "inline" in .c files. The compiler will know better
what to do.

BR,
Jani.

> +{
> + return scale ?: 1;
> +}
> +
> /**
> * DOC: overview
> *
> @@ -76,13 +81,13 @@ static void drm_log_blit(struct iosys_map *dst, unsigned int dst_pitch,
> {
> switch (px_width) {
> case 2:
> - drm_draw_blit16(dst, dst_pitch, src, src_pitch, height, width, scale, color);
> + drm_draw_blit16(dst, dst_pitch, src, src_pitch, height, width, drm_log_scale(), color);
> break;
> case 3:
> - drm_draw_blit24(dst, dst_pitch, src, src_pitch, height, width, scale, color);
> + drm_draw_blit24(dst, dst_pitch, src, src_pitch, height, width, drm_log_scale(), color);
> break;
> case 4:
> - drm_draw_blit32(dst, dst_pitch, src, src_pitch, height, width, scale, color);
> + drm_draw_blit32(dst, dst_pitch, src, src_pitch, height, width, drm_log_scale(), color);
> break;
> default:
> WARN_ONCE(1, "Can't blit with pixel width %d\n", px_width);
> @@ -216,8 +221,8 @@ static int drm_log_setup_modeset(struct drm_client_dev *client,
> return -ENOMEM;
> }
> mode_set->fb = scanout->buffer->fb;
> - scanout->scaled_font_h = scanout->font->height * scale;
> - scanout->scaled_font_w = scanout->font->width * scale;
> + scanout->scaled_font_h = scanout->font->height * drm_log_scale();
> + scanout->scaled_font_w = scanout->font->width * drm_log_scale();
> scanout->rows = height / scanout->scaled_font_h;
> scanout->columns = width / scanout->scaled_font_w;
> scanout->front_color = drm_draw_color_from_xrgb8888(0xffffff, format);

--
Jani Nikula, Intel