[PATCH v4 1/2] fbdev: udlfb: fix shadow buffer handling when the framebuffer grows
From: YuanZhe Liu
Date: Sat Dec 05 2026 - 12:50:36 EST
dlfb_realloc_framebuffer() grows the virtual framebuffer when a larger
video mode is set, but keeps the previous, smaller shadow buffer when
shadowing is disabled at runtime or when the allocation of a new one
fails. dlfb_render_hline() then compares lines of the larger
framebuffer against that buffer and reads beyond its end.
Replace the shadow buffer even when no new one could be allocated, and
publish the framebuffers together with their line length under
dlfb->render_mutex, which already serializes damage rendering. The
replaced shadow buffer is only used by that rendering and can be freed
directly afterwards.
info->var can describe the new mode before dlfb_ops_set_par() installs
the matching framebuffer, so also reject damage requests that do not fit
the framebuffer that is installed right now.
Fixes: 7433914efd58 ("udlfb: allow reallocating the framebuffer")
Cc: stable@xxxxxxxxxxxxxxx
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Co-developed-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: Cheng Wen <wencheng@xxxxxxxxxxxxx>
Signed-off-by: YuanZhe Liu <25031212351@xxxxxxxxxxxxxxxxx>
---
Changes in v4:
- Publish the buffers and the line length under the existing
dlfb->render_mutex only; drop the local geometry snapshots.
Changes in v3:
- Publish the new buffers under dlfb->render_mutex and free the
stale shadow buffer directly instead of deferring it.
- Validate damage requests against the installed framebuffer.
drivers/video/fbdev/udlfb.c | 44 ++++++++++++++++++++++++++++---------
1 file changed, 34 insertions(+), 10 deletions(-)
diff --git a/drivers/video/fbdev/udlfb.c b/drivers/video/fbdev/udlfb.c
index 8b7b4bdac4fb..30d711884ce5 100644
--- a/drivers/video/fbdev/udlfb.c
+++ b/drivers/video/fbdev/udlfb.c
@@ -639,9 +639,17 @@ static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y, int width, i
width = DL_ALIGN_UP(width + (x-aligned_x), sizeof(unsigned long));
x = aligned_x;
+ /*
+ * info->var can describe the new mode before dlfb_ops_set_par()
+ * installs the matching framebuffer; never render past the
+ * framebuffer that is installed right now.
+ */
if ((width <= 0) ||
(x + width > dlfb->info->var.xres) ||
- (y + height > dlfb->info->var.yres)) {
+ (y + height > dlfb->info->var.yres) ||
+ (height > 0 &&
+ ((u64)y + height - 1) * dlfb->info->fix.line_length +
+ (u64)(x + width) * BPP > dlfb->info->fix.smem_len)) {
ret = -EINVAL;
goto unlock_ret;
}
@@ -1085,7 +1093,9 @@ static int dlfb_ops_set_par(struct fb_info *info)
return result;
dlfb->current_mode = fvs;
+ mutex_lock(&dlfb->render_mutex);
info->fix.line_length = line_length;
+ mutex_unlock(&dlfb->render_mutex);
if (dlfb->fb_count == 0) {
@@ -1209,6 +1219,7 @@ static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info
const void *old_fb = info->screen_buffer;
unsigned char *new_fb;
unsigned char *new_back = NULL;
+ char *old_back;
new_len = PAGE_ALIGN(new_len);
@@ -1235,11 +1246,6 @@ static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info
dlfb_deferred_vfree(dlfb, info->screen_buffer);
}
- info->screen_buffer = new_fb;
- info->fix.smem_len = new_len;
- info->fix.smem_start = (unsigned long) new_fb;
- info->flags = udlfb_info_flags;
-
/*
* Second framebuffer copy to mirror the framebuffer state
* on the physical USB device. We can function without this.
@@ -1251,10 +1257,28 @@ static int dlfb_realloc_framebuffer(struct dlfb_data *dlfb, struct fb_info *info
if (!new_back)
dev_info(info->dev,
"No shadow/backing buffer allocated\n");
- else {
- dlfb_deferred_vfree(dlfb, dlfb->backing_buffer);
- dlfb->backing_buffer = new_back;
- }
+
+ /*
+ * dlfb_render_hline() reads these buffers under
+ * dlfb->render_mutex. Publish them under the same lock, so that
+ * rendering never pairs the new framebuffer with the old shadow
+ * buffer, and replace the shadow buffer even when no new one
+ * could be allocated.
+ */
+ mutex_lock(&dlfb->render_mutex);
+ old_back = dlfb->backing_buffer;
+ dlfb->backing_buffer = new_back;
+ info->screen_buffer = new_fb;
+ info->fix.smem_len = new_len;
+ info->fix.smem_start = (unsigned long) new_fb;
+ info->flags = udlfb_info_flags;
+ mutex_unlock(&dlfb->render_mutex);
+
+ /*
+ * Only damage rendering used the old shadow buffer, and it
+ * holds dlfb->render_mutex, so it can be freed directly.
+ */
+ vfree(old_back);
}
return 0;
}
--
2.34.1