* [PATCH] vt: allocate the screen buffers with kvzalloc()
@ 2026-11-19 16:39 Arun Iyengar via B4 Relay
0 siblings, 0 replies; only message 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 [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-19 16:39 UTC | newest]
Thread overview: (only message) (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
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®