mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Arun Iyengar via B4 Relay <devnull+aiyengar1990.gmail.com@kernel.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Jiri Slaby <jirislaby@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-serial@vger.kernel.org,
	 Nicolas Pitre <nico@fluxnic.net>,
	Arun Iyengar <aiyengar1990@gmail.com>
Subject: [PATCH] vt: allocate the screen buffers with kvzalloc()
Date: Thu, 19 Nov 2026 16:39:09 +0000	[thread overview]
Message-ID: <20261119-vt-kvzalloc-v1-1-5e5f59b37f61@gmail.com> (raw)

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>



                 reply	other threads:[~2026-11-19 16:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119-vt-kvzalloc-v1-1-5e5f59b37f61@gmail.com \
    --to=devnull+aiyengar1990.gmail.com@kernel.org \
    --cc=aiyengar1990@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=nico@fluxnic.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®