Skip to contentSkip to conversation

vt: allocate the screen buffers with kvzalloc()

PATCH New Arun Iyengar 2 participants 2 messages Nov 19 TTY layer and serial drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vt: allocate the screen buffers with kvzalloc()

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.

---
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(-)
drivers/tty/vt/vt.c +10 -10
index 7dbc0072a40a..cdc862be4d6f 100644
@@ -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,

Signature · 1 line
Arun Iyengar <[email protected]>
#1 lore raw
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…

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 <[email protected]>

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.

Quoted 4 lines
drivers/tty/vt/vt.c +0 -0
index 7dbc0072a40a9..cdc862be4d6fd 100644

[ ... ]

drivers/tty/vt/vt.c struct vc_data *vc_deallocate(unsigned int currcons) · line 1492

Quoted 13 lines
drivers/tty/vt/vt.c +2 -2
@@ -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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1