Skip to contentSkip to conversation

drm/tiny: Use kmalloc_array() instead of kmalloc()

PATCH In review Rahul Kumar 2 participants 3 messages Sep 23, 2025 – Sep 24, 2025

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

PATCH 1/1 drm/tiny: Use kmalloc_array() instead of kmalloc()

Documentation/process/deprecated.rst recommends against the use of kmalloc with dynamic size calculations due to the risk of overflow and smaller allocation being made than the caller was expecting.

Replace kmalloc() with kmalloc_array() in drivers/gpu/drm/tiny/repaper.c to make the intended allocation size clearer and avoid potential overflow issues.

---
 drivers/gpu/drm/tiny/repaper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tiny/repaper.c +1 -1
index 5c3b51eb0a97..4d439a2d973a 100644
@@ -535,7 +535,7 @@ static int repaper_fb_dirty(struct drm_framebuffer *fb,
 	DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
 		  epd->factored_stage_time);
 
-	buf = kmalloc(fb->width * fb->height / 8, GFP_KERNEL);
+	buf = kmalloc_array(fb->width, fb->height / 8, GFP_KERNEL);
 	if (!buf) {
 		ret = -ENOMEM;
 		goto out_exit;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 18 lines · Rahul Kumar

Documentation/process/deprecated.rst recommends against the use of kmalloc with dynamic size calculations due to the risk of overflow and smaller allocation being made than the caller was expecting.

Replace kmalloc() with kmalloc_array() in drivers/gpu/drm/tiny/repaper.c to make the intended allocation size clearer and avoid potential overflow issues.

---
 drivers/gpu/drm/tiny/repaper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tiny/repaper.c +0 -0
index 5c3b51eb0a97..4d439a2d973a 100644

drivers/gpu/drm/tiny/repaper.c static int repaper_fb_dirty(struct drm_framebuffer *fb, · line 538

Quoted 5 lines
drivers/gpu/drm/tiny/repaper.c +1 -1
@@ -535,7 +535,7 @@ static int repaper_fb_dirty(struct drm_framebuffer *fb,
 	DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
 		  epd->factored_stage_time);
 
-	buf = kmalloc(fb->width * fb->height / 8, GFP_KERNEL);
+	buf = kmalloc_array(fb->width, fb->height / 8, GFP_KERNEL);

That's just not the same thing, though.

BR,
Jani.

Quoted 3 lines
drivers/gpu/drm/tiny/repaper.c +0 -0
 	if (!buf) {
 		ret = -ENOMEM;
 		goto out_exit;
Signature · 1 line
Jani Nikula, Intel
#2 lore raw
Quoted 23 lines · Jani Nikula
Quoted 21 lines · Rahul Kumar

Documentation/process/deprecated.rst recommends against the use of kmalloc with dynamic size calculations due to the risk of overflow and smaller allocation being made than the caller was expecting.

Replace kmalloc() with kmalloc_array() in drivers/gpu/drm/tiny/repaper.c to make the intended allocation size clearer and avoid potential overflow issues.

---
 drivers/gpu/drm/tiny/repaper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/tiny/repaper.c +0 -0
index 5c3b51eb0a97..4d439a2d973a 100644
@@ -535,7 +535,7 @@ static int repaper_fb_dirty(struct drm_framebuffer *fb,
      DRM_DEBUG("Flushing [FB:%d] st=%ums\n", fb->base.id,
                epd->factored_stage_time);
 
Quoted 5 lines

drivers/gpu/drm/tiny/repaper.c static int repaper_fb_dirty(struct drm_framebuffer *fb, · line 538

Quoted 2 lines
drivers/gpu/drm/tiny/repaper.c +1 -1
-     buf = kmalloc(fb->width * fb->height / 8, GFP_KERNEL);
+     buf = kmalloc_array(fb->width, fb->height / 8, GFP_KERNEL);

That's just not the same thing, though.

Thanks Jani, you are right. My change made the allocation size different from the original (width * height / 8). The current code is fine, so I will not change it.

Thanks,
Rahul

Quoted 9 lines

BR,
Jani.

drivers/gpu/drm/tiny/repaper.c static int repaper_fb_dirty(struct drm_framebuffer *fb, · line 541

Quoted 3 lines
drivers/gpu/drm/tiny/repaper.c +0 -0
      if (!buf) {
              ret = -ENOMEM;
              goto out_exit;

--
Jani Nikula, Intel