[RFC PATCH RESEND 2/3] io_uring: add IORING_WRITE_STREAM_DEFAULT for explicit device-default placement

From: Suho Son

Date: Mon Nov 23 2026 - 00:17:35 EST


From: Suho Son <suho.son@xxxxxxxxxxx>

Add a reserved sqe->write_stream value, IORING_WRITE_STREAM_DEFAULT
((__u8)-1), that explicitly requests the device-default stream for a
write. Unlike an omitted (zero) stream, which lets the kernel derive
one from the inode write hint or, with the cgroup policy added later
in this series, a per-cgroup io.stream value, the reserved value pins
the write to the device default and suppresses both.

The value is translated once in __io_prep_rw() into a request flag
(REQ_F_STREAM_DEFAULT); io_rw_init_file() then converts it into a
concrete stream 0 plus IOCB_WRITE_STREAM_EXPLICIT. Deciding at prep
time is required for re-issues, where ki_write_stream has already
been rewritten to zero, so the selection survives kiocb
reinitialization and IOSQE_ASYNC deferral.

The reservation is visible in the UAPI: 255 previously failed
validation with -EINVAL on any real device, so no working application
regresses, but a hypothetical device advertising more than 254
streams can no longer have its stream 255 selected through this
interface.

Signed-off-by: Suho Son <suho.son@xxxxxxxxxxx>
---
include/linux/io_uring_types.h | 4 ++++
include/uapi/linux/io_uring.h | 26 ++++++++++++++++++++++++++
io_uring/rw.c | 16 ++++++++++++++++
3 files changed, 46 insertions(+)

diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h
index 1468bcb028930..550928cd4ad95 100644
--- a/include/linux/io_uring_types.h
+++ b/include/linux/io_uring_types.h
@@ -622,6 +622,8 @@ enum {
REQ_F_SQE_COPIED_BIT,
REQ_F_IOPOLL_BIT,
REQ_F_DMABUF_BIT,
+ /* sqe->write_stream is IORING_WRITE_STREAM_DEFAULT */
+ REQ_F_STREAM_DEFAULT_BIT,

/* not a real bit, just to check we're not overflowing the space */
__REQ_F_LAST_BIT,
@@ -719,6 +721,8 @@ enum {
REQ_F_IOPOLL = IO_REQ_FLAG(REQ_F_IOPOLL_BIT),
/* there is a dma map attached to request that needs to be dropped */
REQ_F_DMABUF = IO_REQ_FLAG(REQ_F_DMABUF_BIT),
+ /* sqe->write_stream was IORING_WRITE_STREAM_DEFAULT (see io_rw_init_file()) */
+ REQ_F_STREAM_DEFAULT = IO_REQ_FLAG(REQ_F_STREAM_DEFAULT_BIT),
};

struct io_tw_req {
diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h
index cf1afd11bb871..3ae504d9ec1a0 100644
--- a/include/uapi/linux/io_uring.h
+++ b/include/uapi/linux/io_uring.h
@@ -122,6 +122,32 @@ struct io_uring_sqe {
/* sqe->attr_type_mask flags */
#define IORING_RW_ATTR_FLAG_PI (1U << 0)

+/*
+ * sqe->write_stream reserved value for explicitly requesting the
+ * device-default write stream.
+ *
+ * sqe->write_stream carries a per-I/O write stream identifier for write
+ * requests to block devices that support write streams (e.g. NVMe FDP):
+ *
+ * 0 the issuer does not express a stream; the kernel may
+ * then derive one from the file's write lifetime hint
+ * or, if configured, from the cgroup's io.stream
+ * policy.
+ * 1..max an explicit stream identifier, validated against the
+ * device's max_write_streams.
+ * 255 (below) an explicit request for the device-default stream.
+ * This overrides both the inode write hint and any
+ * cgroup io.stream value: the I/O is placed as if no
+ * stream had been requested by anyone.
+ *
+ * Any other value above the device's max_write_streams is rejected with
+ * -EINVAL, as before. Note that a value of 255 can therefore no longer
+ * select stream 255 on a hypothetical device offering that many streams;
+ * the bio-level write stream field uses 255 as its "unset" sentinel, so
+ * stream identifiers are effectively limited to 1..254 through this
+ * interface.
+ */
+#define IORING_WRITE_STREAM_DEFAULT ((__u8)-1)
/* PI attribute information in sqe->attr_ptr for IORING_RW_ATTR_FLAG_PI: */
struct io_uring_attr_pi {
__u16 flags;
diff --git a/io_uring/rw.c b/io_uring/rw.c
index 3ecb842147a4d..d1d154533bf14 100644
--- a/io_uring/rw.c
+++ b/io_uring/rw.c
@@ -273,6 +273,8 @@ static int __io_prep_rw(struct io_kiocb *req, const struct io_uring_sqe *sqe,
}
rw->kiocb.ki_flags = 0;
rw->kiocb.ki_write_stream = READ_ONCE(sqe->write_stream);
+ if (rw->kiocb.ki_write_stream == IORING_WRITE_STREAM_DEFAULT)
+ io_req_set_flags(req, REQ_F_STREAM_DEFAULT);

if (req->ctx->flags & IORING_SETUP_IOPOLL)
rw->kiocb.ki_complete = io_complete_rw_iopoll;
@@ -851,6 +853,20 @@ static int io_rw_init_file(struct io_kiocb *req, fmode_t mode, int rw_type)
if (unlikely(ret))
return ret;

+ /*
+ * IORING_WRITE_STREAM_DEFAULT is an explicit request for the
+ * device-default stream: carry it as a concrete stream 0 plus a
+ * marker, so neither the inode write hint nor a cgroup io.stream
+ * value can override the issuer's choice. The request flag is set
+ * once at prep time and survives the ki_flags reinitialization
+ * above as well as async re-issues, where ki_write_stream has
+ * already been rewritten to 0.
+ */
+ if (req->flags & REQ_F_STREAM_DEFAULT) {
+ kiocb->ki_write_stream = 0;
+ kiocb->ki_flags |= IOCB_WRITE_STREAM_EXPLICIT;
+ }
+
/*
* If the file is marked O_NONBLOCK, still allow retry for it if it
* supports async. Otherwise it's impossible to use O_NONBLOCK files
--
2.43.0