Skip to contentSkip to conversation

s390/sclp: Resize for sclp-vt220 console

PATCH Superseded Maximilian Immanuel Brandtner 2 participants 5 messages Sep 22 s390

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover s390/sclp: Resize for sclp-vt220 console

This patch-set implements resize event handling for the sclp-vt220 console and refactors some of the surrounding code.

---
v2:
- don't set the initial winsize asynchronously
- validate sclp buffer length

Testing:
- Tested on QEMU: commit 38879a667f with the following patches applied:

    [PATCH] s390x/sclpconsole: implement resizing for vt220 consoles
    [PATCH v7 00/12] virtio-console: notify about the terminal size
    (https://lore.kernel.org/qemu-devel/[email protected]/)

- Tested on linux-next (commit c1f49dea2b8f)

Maximilian Immanuel Brandtner (2):
s390/sclp: Introduce dedicated sclp-vt220 event buffer type s390/sclp: Implement resize for sclp-vt220 console

 drivers/s390/char/sclp_vt220.c | 85 ++++++++++++++++++++++++++++------
 1 file changed, 72 insertions(+), 13 deletions(-)
Signature · 1 line
2.54.0
#1 lore raw

PATCH v2 1/2 s390/sclp: Introduce dedicated sclp-vt220 event buffer type

Introduce sclp-vt220 event buffer type to replace the byte-manipulation behavior in the receiver function with well defined primitives.

---
 drivers/s390/char/sclp_vt220.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)
drivers/s390/char/sclp_vt220.c +12 -9
index 62979adcb..6dd176049 100644
@@ -52,6 +52,12 @@ struct sclp_vt220_sccb {
 	struct evbuf_header evbuf;
 };
 
+struct sclp_vt220_evbuf {
+	struct evbuf_header header;
+	char type;
+	char data[];
+} __packed;
+
 #define SCLP_VT220_MAX_CHARS_PER_BUFFER	(PAGE_SIZE - \
 					 sizeof(struct sclp_vt220_request) - \
 					 sizeof(struct sclp_vt220_sccb))
@@ -522,25 +528,22 @@ static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
 /*
  * Called by the SCLP to report incoming event buffers.
  */
-static void
-sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
+static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
 {
-	char *buffer;
+	struct sclp_vt220_evbuf *buffer;
 	unsigned int count;
 
-	buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
-	count = evbuf->length - sizeof(struct evbuf_header);
+	buffer = (struct sclp_vt220_evbuf *)evbuf;
+	count = evbuf->length - offsetof(struct sclp_vt220_evbuf, data);
 
-	switch (*buffer) {
+	switch (buffer->type) {
 	case SCLP_VT220_SESSION_ENDED:
 	case SCLP_VT220_SESSION_STARTED:
 		sclp_vt220_reset_session();
 		break;
 	case SCLP_VT220_SESSION_DATA:
 		/* Send input to line discipline */
-		buffer++;
-		count--;
-		sclp_vt220_handle_input(buffer, count);
+		sclp_vt220_handle_input(buffer->data, count);
 		tty_flip_buffer_push(&sclp_vt220_port);
 		break;
 	}
Signature · 1 line
2.54.0
#3 lore raw

PATCH v2 2/2 s390/sclp: Implement resize for sclp-vt220 console

In reply to Maximilian Immanuel Brandtner

Add support for sclp vt220 resize events to enable host-initiated terminal resizing when using the sclp-vt220 console.

This allows QEMU to dynamically resize the console. On older kernel versions, these events are safely ignored as the kernel drops events with undefined tty codes.

---
 drivers/s390/char/sclp_vt220.c | 64 +++++++++++++++++++++++++++++++---
 1 file changed, 60 insertions(+), 4 deletions(-)
drivers/s390/char/sclp_vt220.c +60 -4
index 6dd176049..f5bdca7a8 100644
@@ -27,6 +27,7 @@
 #include <linux/init.h>
 #include <linux/reboot.h>
 #include <linux/slab.h>
+#include <linux/workqueue.h>
 
 #include <linux/uaccess.h>
 #include "sclp.h"
@@ -58,6 +59,11 @@ struct sclp_vt220_evbuf {
 	char data[];
 } __packed;
 
+struct sclp_vt220_resize_data_t {
+	u16 rows;
+	u16 cols;
+} __packed;
+
 #define SCLP_VT220_MAX_CHARS_PER_BUFFER	(PAGE_SIZE - \
 					 sizeof(struct sclp_vt220_request) - \
 					 sizeof(struct sclp_vt220_sccb))
@@ -98,6 +104,15 @@ static int __initdata sclp_vt220_init_count;
  * another buffer */
 static int sclp_vt220_flush_later;
 
+/* Work struct required for scheduling resize */
+static struct work_struct sclp_vt220_resize_work;
+
+/* Current vt220 terminal winsize */
+static struct winsize sclp_vt220_winsize = {
+	.ws_row = 24,
+	.ws_col = 80,
+};
+
 static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
 static int __sclp_vt220_emit(struct sclp_vt220_request *request);
 static void sclp_vt220_emit_current(void);
@@ -477,6 +492,7 @@ sclp_vt220_write(struct tty_struct *tty, const u8 *buf, size_t count)
 #define SCLP_VT220_SESSION_ENDED	0x01
 #define	SCLP_VT220_SESSION_STARTED	0x80
 #define SCLP_VT220_SESSION_DATA		0x00
+#define SCLP_VT220_SESSION_RESIZE	0x08
 
 #ifdef CONFIG_MAGIC_SYSRQ
 
@@ -525,6 +541,38 @@ static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
 
 #endif
 
+static void sclp_vt220_resize(struct work_struct *work)
+{
+	struct tty_struct *tty;
+	struct winsize ws;
+
+	spin_lock_irq(&sclp_vt220_lock);
+	ws = sclp_vt220_winsize;
+	spin_unlock_irq(&sclp_vt220_lock);
+
+	tty = tty_port_tty_get(&sclp_vt220_port);
+	if (!tty)
+		return;
+
+	tty_do_resize(tty, &ws);
+	tty_kref_put(tty);
+}
+
+static void sclp_vt220_resize_sched(void *buffer, unsigned int count)
+{
+	struct sclp_vt220_resize_data_t *data = buffer;
+	unsigned long flags;
+
+	if (count < sizeof(*data))
+		return;
+
+	spin_lock_irqsave(&sclp_vt220_lock, flags);
+	sclp_vt220_winsize.ws_row = data->rows;
+	sclp_vt220_winsize.ws_col = data->cols;
+	schedule_work(&sclp_vt220_resize_work);
+	spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+}
+
 /*
  * Called by the SCLP to report incoming event buffers.
  */
@@ -546,6 +594,9 @@ static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
 		sclp_vt220_handle_input(buffer->data, count);
 		tty_flip_buffer_push(&sclp_vt220_port);
 		break;
+	case SCLP_VT220_SESSION_RESIZE:
+		sclp_vt220_resize_sched(buffer->data, count);
+		break;
 	}
 }
 
@@ -555,12 +606,15 @@ static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
 static int
 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
 {
+	struct winsize ws;
+
 	if (tty->count == 1) {
+		spin_lock_irq(&sclp_vt220_lock);
+		ws = sclp_vt220_winsize;
+		spin_unlock_irq(&sclp_vt220_lock);
+
 		tty_port_tty_set(&sclp_vt220_port, tty);
-		if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
-			tty->winsize.ws_row = 24;
-			tty->winsize.ws_col = 80;
-		}
+		tty_do_resize(tty, &ws);
 	}
 	return 0;
 }
@@ -754,6 +808,8 @@ static int __init sclp_vt220_tty_init(void)
 	rc = tty_register_driver(driver);
 	if (rc)
 		goto out_init;
+
+	INIT_WORK(&sclp_vt220_resize_work, sclp_vt220_resize);
 	rc = sclp_register(&sclp_vt220_register_input);
 	if (rc)
 		goto out_reg;
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH v2 1/2] s390/sclp: Introduce dedicated sclp-vt220 event buffer type

In reply to Maximilian Immanuel Brandtner
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Introduce sclp-vt220 event buffer type to replace the byte-manipulation behavior in the receiver function with well defined primitives.

Sashiko has reviewed this patch and found no issues. It looks great!

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

Re: [PATCH v2 2/2] s390/sclp: Implement resize for sclp-vt220 console

In reply to Maximilian Immanuel Brandtner
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] s390/sclp: lost terminal resize event due to TOCTOU race --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] s390/sclp: lost terminal resize event due to TOCTOU race
--

commit 66ee01928776071bac37719344cd6f2a8ad2ae89
Author: Maximilian Immanuel Brandtner <[email protected]>

s390/sclp: Implement resize for sclp-vt220 console

This commit adds support for sclp vt220 resize events to enable host-initiated terminal resizing when using the sclp-vt220 console. This allows QEMU to dynamically resize the console.

Quoted 4 lines
drivers/s390/char/sclp_vt220.c +0 -0
index 7c168a66e4afe..a5a9ac1b1fe07 100644

[ ... ]

Quoted 7 lines
drivers/s390/char/sclp_vt220.c +2 -0
@@ -552,12 +603,15 @@ static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
 static int
 sclp_vt220_open(struct tty_struct *tty, struct file *filp)
 {
+	struct winsize ws;
+
 	if (tty->count == 1) {

drivers/s390/char/sclp_vt220.c static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf) · line 614

Quoted 10 lines
drivers/s390/char/sclp_vt220.c +5 -4
+		spin_lock_irq(&sclp_vt220_lock);
+		ws = sclp_vt220_winsize;
+		spin_unlock_irq(&sclp_vt220_lock);
+
 		tty_port_tty_set(&sclp_vt220_port, tty);
-		if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
-			tty->winsize.ws_row = 24;
-			tty->winsize.ws_col = 80;
-		}
+		tty_do_resize(tty, &ws);

[Severity: Medium]
Could this sequence result in a lost terminal resize event due to a TOCTOU race?

If a host resize event arrives exactly after sclp_vt220_open() has read the cached window size into ws, but before it attaches the tty to the port:

sclp_vt220_open()

    ws = sclp_vt220_winsize;
    // Host resize event arrives here, updates sclp_vt220_winsize
    // and schedules sclp_vt220_resize_work

The resize worker could execute immediately:

sclp_vt220_resize()

    tty = tty_port_tty_get(&sclp_vt220_port);
    if (!tty)
        return; // Worker aborts silently because tty is not set yet

Then sclp_vt220_open() continues:

sclp_vt220_open()

    tty_port_tty_set(&sclp_vt220_port, tty);
    tty_do_resize(tty, &ws);

Would this cause the terminal to be unconditionally overwritten with the stale cached size, permanently desynchronizing it from the emulator's size until manually resized?

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