* [PATCH] drm: Use kvmemdup/kvfree for EDID data
@ 2025-09-05 15:11 Zilin Guan
2025-09-08 13:54 ` Jani Nikula
0 siblings, 1 reply; 2+ messages in thread
From: Zilin Guan @ 2025-09-05 15:11 UTC (permalink / raw)
To: maarten.lankhorst
Cc: mripard, tzimmermann, airlied, simona, dri-devel, linux-kernel,
jianhao.xu, Zilin Guan
The drm_edid_alloc() function uses kmemdup() to copy raw EDID data,
which can be loaded from firmware. A problem exists because the firmware
subsystem uses vfree() to release its data buffer. This implies the
buffer may be allocated with vmalloc() and can thus be larger than
kmalloc() typically supports. Since kmemdup() uses kmalloc() internally,
attempting to duplicate a large, vmalloc'd EDID buffer can lead to an
allocation failure.
To fix this mismatch and robustly handle EDID data of any size, this
patch replaces kmemdup() with kvmemdup(). The kvmemdup() function is
designed for this scenario, as it can safely handle a vmalloc'd source
and choose an appropriate allocator for the destination. The corresponding
free calls are therefore updated to kvfree().
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
drivers/gpu/drm/drm_edid.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/gpu/drm/drm_edid.c b/drivers/gpu/drm/drm_edid.c
index e2e85345aa9a..97142bfc45ad 100644
--- a/drivers/gpu/drm/drm_edid.c
+++ b/drivers/gpu/drm/drm_edid.c
@@ -2530,13 +2530,13 @@ const struct drm_edid *drm_edid_alloc(const void *edid, size_t size)
if (!edid || !size || size < EDID_LENGTH)
return NULL;
- edid = kmemdup(edid, size, GFP_KERNEL);
+ edid = kvmemdup(edid, size, GFP_KERNEL);
if (!edid)
return NULL;
drm_edid = _drm_edid_alloc(edid, size);
if (!drm_edid)
- kfree(edid);
+ kvfree(edid);
return drm_edid;
}
@@ -2568,7 +2568,7 @@ void drm_edid_free(const struct drm_edid *drm_edid)
if (!drm_edid)
return;
- kfree(drm_edid->edid);
+ kvfree(drm_edid->edid);
kfree(drm_edid);
}
EXPORT_SYMBOL(drm_edid_free);
--
2.34.1
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] drm: Use kvmemdup/kvfree for EDID data
2025-09-05 15:11 [PATCH] drm: Use kvmemdup/kvfree for EDID data Zilin Guan
@ 2025-09-08 13:54 ` Jani Nikula
0 siblings, 0 replies; 2+ messages in thread
From: Jani Nikula @ 2025-09-08 13:54 UTC (permalink / raw)
To: Zilin Guan, maarten.lankhorst
Cc: mripard, tzimmermann, airlied, simona, dri-devel, linux-kernel,
jianhao.xu, Zilin Guan
On Fri, 05 Sep 2025, Zilin Guan <zilin@seu.edu.cn> wrote:
> The drm_edid_alloc() function uses kmemdup() to copy raw EDID data,
> which can be loaded from firmware. A problem exists because the firmware
> subsystem uses vfree() to release its data buffer. This implies the
> buffer may be allocated with vmalloc() and can thus be larger than
> kmalloc() typically supports. Since kmemdup() uses kmalloc() internally,
> attempting to duplicate a large, vmalloc'd EDID buffer can lead to an
> allocation failure.
Real world EDIDs are usually under 1 kB and if the user provides a much
bigger EDID via the firmware loader it's okay to fail at the allocation
time.
BR,
Jani.
>
> To fix this mismatch and robustly handle EDID data of any size, this
> patch replaces kmemdup() with kvmemdup(). The kvmemdup() function is
> designed for this scenario, as it can safely handle a vmalloc'd source
> and choose an appropriate allocator for the destination. The corresponding
> free calls are therefore updated to kvfree().
>
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
> drivers/gpu/drm/drm_edid.c | 6 +++---
> 1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/gpu/drm/drm_edid.c b/drivers/gpu/drm/drm_edid.c
> index e2e85345aa9a..97142bfc45ad 100644
> --- a/drivers/gpu/drm/drm_edid.c
> +++ b/drivers/gpu/drm/drm_edid.c
> @@ -2530,13 +2530,13 @@ const struct drm_edid *drm_edid_alloc(const void *edid, size_t size)
> if (!edid || !size || size < EDID_LENGTH)
> return NULL;
>
> - edid = kmemdup(edid, size, GFP_KERNEL);
> + edid = kvmemdup(edid, size, GFP_KERNEL);
> if (!edid)
> return NULL;
>
> drm_edid = _drm_edid_alloc(edid, size);
> if (!drm_edid)
> - kfree(edid);
> + kvfree(edid);
>
> return drm_edid;
> }
> @@ -2568,7 +2568,7 @@ void drm_edid_free(const struct drm_edid *drm_edid)
> if (!drm_edid)
> return;
>
> - kfree(drm_edid->edid);
> + kvfree(drm_edid->edid);
> kfree(drm_edid);
> }
> EXPORT_SYMBOL(drm_edid_free);
--
Jani Nikula, Intel
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2025-09-08 13:54 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-05 15:11 [PATCH] drm: Use kvmemdup/kvfree for EDID data Zilin Guan
2025-09-08 13:54 ` Jani Nikula
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®