[PATCH v2 1/2] fbdev: udlfb: fix shadow buffer handling when the framebuffer grows

From: YuanZhe Liu

Date: Fri Dec 04 2026 - 11:09:10 EST


dlfb_realloc_framebuffer() grows the virtual framebuffer when a larger
video mode is set. The shadow buffer that mirrors the data sent to the
device is only reallocated when shadowing is enabled and the allocation
succeeds. If shadowing is disabled at runtime or the allocation fails,
the driver keeps the previous, smaller shadow buffer, and
dlfb_render_hline() later compares lines of the larger framebuffer
against it, reading beyond the end of the smaller allocation.

Allocate the new shadow buffer before publishing the new geometry and
replace both buffers under dlfb->render_mutex, which serializes damage
rendering. Free the old shadow buffer directly afterwards; it is only
used by damage rendering, which no longer references it.

info->var can already describe the new mode before dlfb_ops_set_par()
installs the matching framebuffer. Reject damage requests that do not
fit into the installed framebuffer, so that concurrent rendering cannot
walk past the buffers of the previous mode either.

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 v2:
- Publish the new buffers and geometry under dlfb->render_mutex and
free the stale shadow buffer directly instead of deferring it.
- Validate damage requests against the installed framebuffer so that
concurrent rendering cannot walk past the previous mode's buffers.

drivers/video/fbdev/udlfb.c | 45 +++++++++++++++++++++++++++++--------
1 file changed, 36 insertions(+), 9 deletions(-)

diff --git a/drivers/video/fbdev/udlfb.c b/drivers/video/fbdev/udlfb.c
index 8b7b4bdac4fb..66d30cdfbe60 100644
--- a/drivers/video/fbdev/udlfb.c
+++ b/drivers/video/fbdev/udlfb.c
@@ -646,6 +646,18 @@ static int dlfb_handle_damage(struct dlfb_data *dlfb, int x, int y, int width, i
goto unlock_ret;
}

+ /*
+ * info->var can describe the new mode before dlfb_ops_set_par()
+ * installs the matching framebuffer. Reject damage that does not
+ * fit into the framebuffer that is currently installed.
+ */
+ if (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;
+ }
+
if (!atomic_read(&dlfb->usb_active)) {
ret = 0;
goto unlock_ret;
@@ -1209,6 +1221,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;
+ unsigned char *old_back;

new_len = PAGE_ALIGN(new_len);

@@ -1235,11 +1248,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 +1259,29 @@ 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;
- }
+
+ /*
+ * Damage rendering reads the buffers and the geometry under
+ * dlfb->render_mutex. Publish the new state under the same
+ * lock so that it never pairs the new framebuffer with the
+ * old shadow buffer.
+ */
+ 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);
+
+ /*
+ * The shadow buffer is only used by damage rendering, which
+ * no longer references the old one; it can be freed directly.
+ * The old framebuffer may still be touched by fbcon, so it
+ * stays on the deferred list.
+ */
+ vfree(old_back);
}
return 0;
}
--
2.34.1