From: Tianyi Chen <hi@tychen.cc>
To: io-uring@vger.kernel.org
Cc: axboe@kernel.dk
Subject: [PATCH liburing] test: cover blocking tty I/O on poll-ready files
Date: Sat, 19 Sep 2026 19:59:48 +0800 [thread overview]
Message-ID: <20260919115948.1269869-2-hi@tychen.cc> (raw)
Exercise a packet-mode tty read with a competing reader, using normal
and registered descriptors, and a write larger than the available tty
buffer. Bound submission latency and release blocked I/O so a regression
fails without leaving the test stuck.
Also check that blocking tty multishot reads complete once, while
O_NONBLOCK tty reads retain multishot behavior.
Link: https://github.com/axboe/liburing/issues/1623
Assisted-by: LLM
Signed-off-by: Tianyi Chen <hi@tychen.cc>
---
test/Makefile | 1 +
test/rw-poll-ready.c | 290 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 291 insertions(+)
create mode 100644 test/rw-poll-ready.c
diff --git a/test/Makefile b/test/Makefile
index 8b27681..3bf197d 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -246,6 +246,7 @@ test_srcs := \
ring-query.c \
rsrc_tags.c \
rw_merge_test.c \
+ rw-poll-ready.c \
self.c \
recvsend_bundle.c \
recvsend_bundle-inc.c \
diff --git a/test/rw-poll-ready.c b/test/rw-poll-ready.c
new file mode 100644
index 0000000..da9601a
--- /dev/null
+++ b/test/rw-poll-ready.c
@@ -0,0 +1,290 @@
+/* SPDX-License-Identifier: MIT */
+/* Poll-ready blocking tty reads and writes must not block the submitter. */
+#include <fcntl.h>
+#include <pthread.h>
+#include <sched.h>
+#include <stdatomic.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/resource.h>
+#include <termios.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "liburing.h"
+#include "helpers.h"
+
+struct reader {
+ int fd;
+ atomic_int started;
+ int ret;
+};
+
+static void *consume(void *arg)
+{
+ struct reader *r = arg;
+ char byte;
+
+ setpriority(PRIO_PROCESS, 0, 19);
+ atomic_store(&r->started, 1);
+ r->ret = read(r->fd, &byte, 1);
+ return NULL;
+}
+
+static void *release_readers(void *arg)
+{
+ int fd = *(int *)arg;
+
+ usleep(1000000);
+ if (write(fd, "xx", 2) != 2)
+ perror("release readers");
+ return NULL;
+}
+
+static long elapsed_ms(struct timespec *start, struct timespec *end)
+{
+ return (end->tv_sec - start->tv_sec) * 1000 +
+ (end->tv_nsec - start->tv_nsec) / 1000000;
+}
+
+static int wait_completions(struct io_uring *ring, int len)
+{
+ unsigned int seen = 0;
+ int i;
+
+ for (i = 0; i < 2; i++) {
+ struct __kernel_timespec wait = { .tv_sec = 2 };
+ struct io_uring_cqe *cqe;
+ int ret = io_uring_wait_cqe_timeout(ring, &cqe, &wait);
+
+ if (ret)
+ return T_EXIT_FAIL;
+ if (cqe->user_data == 1) {
+ if (cqe->res > len || (cqe->res < 0 &&
+ cqe->res != -ECANCELED && cqe->res != -EINTR))
+ return T_EXIT_FAIL;
+ } else if (cqe->user_data != 2 ||
+ (cqe->res != -ETIME && cqe->res != -ECANCELED &&
+ cqe->res != -ENOENT)) {
+ return T_EXIT_FAIL;
+ }
+ seen |= 1U << cqe->user_data;
+ io_uring_cqe_seen(ring, cqe);
+ }
+ return seen == 6 ? T_EXIT_PASS : T_EXIT_FAIL;
+}
+
+static int test(int fixed)
+{
+ struct __kernel_timespec timeout = { .tv_nsec = 100000000 };
+ struct io_uring ring;
+ struct io_uring_sqe *sqe;
+ struct timespec start, end;
+ struct reader reader = { .started = 0 };
+ pthread_t consumer, release;
+ char buf[8];
+ int master, slave, one = 1, ret, failed = 0;
+
+ master = posix_openpt(O_RDWR | O_NOCTTY);
+ if (master < 0 || grantpt(master) || unlockpt(master))
+ return T_EXIT_FAIL;
+ slave = open(ptsname(master), O_RDWR | O_NOCTTY);
+ if (slave < 0 || ioctl(master, TIOCPKT, &one))
+ return T_EXIT_FAIL;
+ ret = t_create_ring(8, &ring, 0);
+ if (ret == T_SETUP_SKIP)
+ return T_EXIT_SKIP;
+ if (ret != T_SETUP_OK)
+ return T_EXIT_FAIL;
+ if (fixed && io_uring_register_files(&ring, &master, 1))
+ return T_EXIT_FAIL;
+
+ reader.fd = master;
+ if (pthread_create(&consumer, NULL, consume, &reader))
+ return T_EXIT_FAIL;
+ while (!atomic_load(&reader.started))
+ usleep(1000);
+ /* Let the competing read acquire the tty read lock. */
+ usleep(20000);
+ if (pthread_create(&release, NULL, release_readers, &slave))
+ return T_EXIT_FAIL;
+ if (tcflush(slave, TCIFLUSH))
+ return T_EXIT_FAIL;
+
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_read(sqe, fixed ? 0 : master, buf, sizeof(buf), 0);
+ sqe->flags = IOSQE_IO_LINK | (fixed ? IOSQE_FIXED_FILE : 0);
+ sqe->user_data = 1;
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_link_timeout(sqe, &timeout, 0);
+ sqe->user_data = 2;
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ ret = io_uring_submit(&ring);
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ if (ret != 2 || elapsed_ms(&start, &end) > 500) {
+ fprintf(stderr, "fixed=%d: submit returned %d after %ld ms\n",
+ fixed, ret, elapsed_ms(&start, &end));
+ failed = 1;
+ }
+ failed |= wait_completions(&ring, sizeof(buf));
+ pthread_join(release, NULL);
+ pthread_join(consumer, NULL);
+ if (reader.ret != 1) {
+ fprintf(stderr, "consumer: %d\n", reader.ret);
+ failed = 1;
+ }
+ io_uring_queue_exit(&ring);
+ close(slave);
+ close(master);
+ return failed ? T_EXIT_FAIL : T_EXIT_PASS;
+}
+
+struct drain {
+ int fd;
+ atomic_int done;
+};
+
+static void *drain_output(void *arg)
+{
+ struct drain *d = arg;
+ char buf[4096];
+
+ usleep(1000000);
+ while (!atomic_load(&d->done)) {
+ if (read(d->fd, buf, sizeof(buf)) < 0 && errno != EAGAIN)
+ break;
+ usleep(1000);
+ }
+ return NULL;
+}
+
+static int test_write(void)
+{
+ struct __kernel_timespec timeout = { .tv_nsec = 100000000 };
+ struct io_uring ring;
+ struct io_uring_sqe *sqe;
+ struct timespec start, end;
+ struct drain drain = { .done = 0 };
+ pthread_t reader;
+ char buf[65536] = { 0 };
+ int master, slave, ret, failed = 0;
+
+ master = posix_openpt(O_RDWR | O_NOCTTY | O_NONBLOCK);
+ if (master < 0 || grantpt(master) || unlockpt(master))
+ return T_EXIT_FAIL;
+ slave = open(ptsname(master), O_RDWR | O_NOCTTY);
+ if (slave < 0 || io_uring_queue_init(8, &ring, 0))
+ return T_EXIT_FAIL;
+ drain.fd = master;
+ if (pthread_create(&reader, NULL, drain_output, &drain))
+ return T_EXIT_FAIL;
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_write(sqe, slave, buf, sizeof(buf), 0);
+ sqe->flags = IOSQE_IO_LINK;
+ sqe->user_data = 1;
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_link_timeout(sqe, &timeout, 0);
+ sqe->user_data = 2;
+ clock_gettime(CLOCK_MONOTONIC, &start);
+ ret = io_uring_submit(&ring);
+ clock_gettime(CLOCK_MONOTONIC, &end);
+ if (ret != 2 || elapsed_ms(&start, &end) > 500) {
+ fprintf(stderr, "write submit returned %d after %ld ms\n",
+ ret, elapsed_ms(&start, &end));
+ failed = 1;
+ }
+ failed |= wait_completions(&ring, sizeof(buf));
+ atomic_store(&drain.done, 1);
+ pthread_join(reader, NULL);
+ io_uring_queue_exit(&ring);
+ close(slave);
+ close(master);
+ return failed ? T_EXIT_FAIL : T_EXIT_PASS;
+}
+
+static int test_multishot(int nonblock)
+{
+ struct __kernel_timespec timeout = { .tv_sec = 2 };
+ struct io_uring_buf_ring *br;
+ struct io_uring ring;
+ struct io_uring_sqe *sqe;
+ struct io_uring_cqe *cqe;
+ char buf[2][8];
+ int master, slave, ret, failed;
+
+ master = posix_openpt(O_RDWR | O_NOCTTY | (nonblock ? O_NONBLOCK : 0));
+ if (master < 0 || grantpt(master) || unlockpt(master))
+ return T_EXIT_FAIL;
+ slave = open(ptsname(master), O_RDWR | O_NOCTTY);
+ if (slave < 0)
+ return T_EXIT_FAIL;
+ if (io_uring_queue_init(8, &ring, 0))
+ return T_EXIT_FAIL;
+ br = io_uring_setup_buf_ring(&ring, 2, 1, 0, &ret);
+ if (!br)
+ return ret == -EINVAL ? T_EXIT_SKIP : T_EXIT_FAIL;
+ io_uring_buf_ring_add(br, buf[0], sizeof(buf[0]), 0, 1, 0);
+ io_uring_buf_ring_add(br, buf[1], sizeof(buf[1]), 1, 1, 1);
+ io_uring_buf_ring_advance(br, 2);
+ if (write(slave, "a", 1) != 1)
+ return T_EXIT_FAIL;
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_read_multishot(sqe, master, 0, 0, 1);
+ sqe->user_data = 1;
+ if (io_uring_submit(&ring) != 1)
+ return T_EXIT_FAIL;
+ ret = io_uring_wait_cqe_timeout(&ring, &cqe, &timeout);
+ if (ret)
+ return T_EXIT_FAIL;
+ failed = cqe->res != 1 || !(cqe->flags & IORING_CQE_F_BUFFER) ||
+ !!(cqe->flags & IORING_CQE_F_MORE) != nonblock;
+ if (failed)
+ fprintf(stderr, "multishot nonblock=%d: res=%d flags=%u\n",
+ nonblock, cqe->res, cqe->flags);
+ io_uring_cqe_seen(&ring, cqe);
+ if (nonblock) {
+ sqe = io_uring_get_sqe(&ring);
+ io_uring_prep_cancel64(sqe, 1, 0);
+ if (io_uring_submit_and_wait(&ring, 2) != 1)
+ failed = 1;
+ }
+ io_uring_free_buf_ring(&ring, br, 2, 1);
+ io_uring_queue_exit(&ring);
+ close(slave);
+ close(master);
+ return failed ? T_EXIT_FAIL : T_EXIT_PASS;
+}
+
+int main(int argc, char *argv[])
+{
+ cpu_set_t cpus;
+ int cpu, ret;
+
+ if (argc > 1)
+ return T_EXIT_SKIP;
+ alarm(15);
+ /* Keep the awakened reader behind the submitter until it sleeps. */
+ if (sched_getaffinity(0, sizeof(cpus), &cpus))
+ return T_EXIT_FAIL;
+ for (cpu = 0; cpu < CPU_SETSIZE; cpu++)
+ if (CPU_ISSET(cpu, &cpus))
+ break;
+ CPU_ZERO(&cpus);
+ CPU_SET(cpu, &cpus);
+ if (sched_setaffinity(0, sizeof(cpus), &cpus))
+ return T_EXIT_FAIL;
+ ret = test(0);
+ if (ret)
+ return ret;
+ ret = test(1);
+ if (ret)
+ return ret;
+ ret = test_multishot(0);
+ if (ret)
+ return ret;
+ ret = test_multishot(1);
+ if (ret)
+ return ret;
+ return test_write();
+}
--
2.55.0
reply other threads:[~2026-09-19 12:00 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=20260919115948.1269869-2-hi@tychen.cc \
--to=hi@tychen.cc \
--cc=axboe@kernel.dk \
--cc=io-uring@vger.kernel.org \
/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