* [PATCH] vt: allocate the screen buffers with kvzalloc()
@ 2026-11-19 16:39 Arun Iyengar via B4 Relay
2026-11-19 16:49 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Arun Iyengar via B4 Relay @ 2026-11-19 16:39 UTC (permalink / raw)
To: Greg Kroah-Hartman, Jiri Slaby
Cc: linux-kernel, linux-serial, Nicolas Pitre, Arun Iyengar
From: Arun Iyengar <aiyengar1990@gmail.com>
A console's screen buffer grows with the display: 480x135 (3840x2160
with the 8x16 font) needs 129600 bytes, an order-5 kzalloc() that can
fail when memory is fragmented. On an Orange Pi Zero2 (1 GiB) under
memory load, plugging in an HDMI monitor gave an order:5 page
allocation failure in vc_do_resize() from fbcon_startup().
Commit 9a98e7a80f95 ("vt: don't use kmalloc() for the unicode screen
buffer") moved the unicode buffer to vmalloc for this reason; let the
screen buffer and its alternate screen copy fall back to vmalloc too.
Nothing needs them physically contiguous. con_init() keeps its
GFP_NOWAIT kzalloc(), which kvfree() also handles.
Cc: Nicolas Pitre <nico@fluxnic.net>
Signed-off-by: Arun Iyengar <aiyengar1990@gmail.com>
---
Tested on an Orange Pi Zero2 (H616) on 7.3.4 with KASAN and the order-5
kmalloc failed by fault injection: 20 of 20 fbcon rebinds fell back to
vmalloc and kept the screen contents (all 20 fail without the patch).
Developed with Claude (Anthropic), which wrote the code and the commit
message. I reviewed it.
---
drivers/tty/vt/vt.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
index 7dbc0072a40a..cdc862be4d6f 100644
--- a/drivers/tty/vt/vt.c
+++ b/drivers/tty/vt/vt.c
@@ -1232,7 +1232,7 @@ int vc_allocate(unsigned int currcons) /* return 0 on success */
vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size)
goto err_free;
err = -ENOMEM;
- vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
+ vc->vc_screenbuf = kvzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
if (!vc->vc_screenbuf)
goto err_free;
@@ -1324,14 +1324,14 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size)
return -EINVAL;
- newscreen = kzalloc(new_screen_size, GFP_USER);
+ newscreen = kvzalloc(new_screen_size, GFP_USER);
if (!newscreen)
return -ENOMEM;
if (vc->vc_uni_lines) {
new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
if (!new_uniscr) {
- kfree(newscreen);
+ kvfree(newscreen);
return -ENOMEM;
}
}
@@ -1344,7 +1344,7 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
err = resize_screen(vc, new_cols, new_rows, from_user);
if (err) {
- kfree(newscreen);
+ kvfree(newscreen);
vc_uniscr_free(new_uniscr);
return err;
}
@@ -1402,7 +1402,7 @@ static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
vc->vc_screenbuf = newscreen;
vc->vc_screenbuf_size = new_screen_size;
set_origin(vc);
- kfree(oldscreen);
+ kvfree(oldscreen);
/* do part of a reset_terminal() */
vc->vc_top = 0;
@@ -1483,10 +1483,10 @@ struct vc_data *vc_deallocate(unsigned int currcons)
con_free_unimap(vc);
put_pid(vc->vt_pid);
vc_uniscr_set(vc, NULL);
- kfree(vc->vc_screenbuf);
+ kvfree(vc->vc_screenbuf);
vc_cons[currcons].d = NULL;
if (vc->vc_saved_screen != NULL) {
- kfree(vc->vc_saved_screen);
+ kvfree(vc->vc_saved_screen);
vc->vc_saved_screen = NULL;
}
vc_uniscr_free(vc->vc_saved_uni_lines);
@@ -2032,7 +2032,7 @@ static void enter_alt_screen(struct vc_data *vc)
if (vc->vc_saved_screen != NULL)
return; /* Already inside an alt-screen */
- vc->vc_saved_screen = kmemdup((u16 *)vc->vc_origin, size, GFP_KERNEL);
+ vc->vc_saved_screen = kvmemdup((u16 *)vc->vc_origin, size, GFP_KERNEL);
if (vc->vc_saved_screen == NULL)
return;
vc->vc_saved_uni_lines = vc->vc_uni_lines;
@@ -2082,7 +2082,7 @@ static void leave_alt_screen(struct vc_data *vc)
/* Update the entire screen */
if (con_should_update(vc))
do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
- kfree(vc->vc_saved_screen);
+ kvfree(vc->vc_saved_screen);
vc->vc_saved_screen = NULL;
}
@@ -2400,7 +2400,7 @@ static void reset_terminal(struct vc_data *vc, int do_clear)
vc->vc_decim = 0;
if (vc->vc_saved_screen != NULL) {
- kfree(vc->vc_saved_screen);
+ kvfree(vc->vc_saved_screen);
vc->vc_saved_screen = NULL;
vc_uniscr_free(vc->vc_saved_uni_lines);
vc->vc_saved_uni_lines = NULL;
---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20261119-vt-kvzalloc-e6208d87a483
Best regards,
--
Arun Iyengar <aiyengar1990@gmail.com>
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] vt: allocate the screen buffers with kvzalloc()
2026-11-19 16:39 [PATCH] vt: allocate the screen buffers with kvzalloc() Arun Iyengar via B4 Relay
@ 2026-11-19 16:49 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-19 16:49 UTC (permalink / raw)
To: Arun Iyengar; +Cc: linux-serial
Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [Critical] tty: vt: workqueue list corruption in vc_allocate() [1]
--
commit 8bede9243468256cdb289eb4eeab40a8e30c6847
Author: Arun Iyengar <aiyengar1990@gmail.com>
vt: allocate the screen buffers with kvzalloc()
This commit replaces kzalloc(), kmemdup(), and kfree() with kvzalloc(),
kvmemdup(), and kvfree() for virtual console screen buffers and saved
alternate screen buffers so large allocations can fall back to vmalloc.
> diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c
> index 7dbc0072a40a9..cdc862be4d6fd 100644
> --- a/drivers/tty/vt/vt.c
> +++ b/drivers/tty/vt/vt.c
[ ... ]
> @@ -1483,10 +1483,10 @@ struct vc_data *vc_deallocate(unsigned int currcons)
> con_free_unimap(vc);
> put_pid(vc->vt_pid);
> vc_uniscr_set(vc, NULL);
> - kfree(vc->vc_screenbuf);
> + kvfree(vc->vc_screenbuf);
> vc_cons[currcons].d = NULL;
> if (vc->vc_saved_screen != NULL) {
> - kfree(vc->vc_saved_screen);
> + kvfree(vc->vc_saved_screen);
> vc->vc_saved_screen = NULL;
> }
> vc_uniscr_free(vc->vc_saved_uni_lines);
[Severity: Critical]
This problem wasn't introduced by this patch, but can vc_deallocate() leave
vc_cons[currcons].SAK_work pending [1] so that a subsequent
vc_allocate() corrupts the workqueue list?
When a Secure Attention Key event schedules vc_cons[currcons].SAK_work and
userspace immediately disallocates the console via VT_DISALLOCATE,
vc_deallocate() tears down the console without canceling SAK_work. If the
console is reallocated before the worker runs, vc_allocate() calls
INIT_WORK() on the queued work_struct:
drivers/tty/vt/vt.c:vc_allocate() {
...
vc_cons[currcons].d = vc;
tty_port_init(&vc->port);
vc->port.ops = &vc_port_ops;
INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
...
}
Reinitializing an active work_struct overwrites its internal list pointers
and corrupts the global workqueue.
[1] https://sashiko.dev/#/bug/linux-5b343630-72f6-4db2-9969-c2c292698bcb
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119-vt-kvzalloc-v1-1-5e5f59b37f61@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-19 16:49 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 16:39 [PATCH] vt: allocate the screen buffers with kvzalloc() Arun Iyengar via B4 Relay
2026-11-19 16:49 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox