Skip to contentSkip to conversation

block: cgroup-based and explicit write stream placement for NVMe FDP

RFC PATCH NAK Suho Son 2 participants 6 messages Nov 23 – Nov 24 1 message not in the archive

Subsystems (5)
Documentation VFS Block layer cgroup +1 more subsystems
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CGME20261123051521epcms2p2445107f926f3acaa2d51c5e12abea90e@epcms2p2

Suho Son

author
#1 lore raw

cover block: cgroup-based and explicit write stream placement for NVMe FDP

Hello,

Resending: my earlier posting of this cover letter went out without the patches threaded to it. Sorry for the noise.

This RFC proposes an operator- and application-controlled write stream interface on top of the existing write stream plumbing (sqe->write_stream, bio->bi_write_stream, and the NVMe FDP placement translation, which are unchanged by this series):

1/3 block: distinguish unspecified and explicit device-default write streams 2/3 io_uring: add IORING_WRITE_STREAM_DEFAULT for explicit device-default placement 3/3 blk-cgroup: add cgroup-based write stream placement

The series is based on the block tree's for-next branch (see the base-commit below).

Motivation and background are described in our HotStorage '26 paper:

Motivation

NVMe Flexible Data Placement reduces write amplification by grouping similarly-aged data into per-reclaim-unit placement handles, and the kernel already lets a single application tag writes through io_uring. Two gaps remain:

(a) an operator cannot place whole workloads per cgroup without modifying applications, and
(b) an application cannot force the device-default placement and be sure that no policy overrides that choice.

The series addresses the root cause shared by both: write stream zero currently means both "unspecified" and "device default", so lower- precedence producers cannot tell whether a stream was requested. The resulting precedence, most specific first, is:

explicit io_uring stream (including DEFAULT)

Quoted 3 lines

inode write hint
cgroup io.stream
device default (0)

Patch 1 introduces the WRITE_STREAM_NONE sentinel for unspecified streams, resolves it centrally before the bio reaches the driver, and carries ki_write_stream through kiocb_clone() used by forwarded direct I/O. Patch 2 adds the explicit-default UAPI value, decided at prep time so it survives async re-issues. Patch 3 adds the io.stream cgroup v2 interface: per-device upsert entries, "inherit" reset, explicit 0, validation against device queue limits, lazy policy activation, and a single-word policy state read locklessly under RCU, with writers serialized by the queue lock held across blkg_conf_prep().

Runtime verification

We exercised the series, as posted, on an NVMe 2.0 FDP device (8 RUHs) with bpftrace observers at blkcg_set_iostream() entry/return, __submit_bio() and the nvme_setup_cmd() dsmgmt export:

- explicit streams 2/5/8 -> placement IDs 1/4/7 (tracepoint-verified)
- IORING_WRITE_STREAM_DEFAULT -> concrete 0, no placement exported, not overridable by an io.stream policy
- stream > max_write_streams -> -EINVAL, no bio issued
- io.stream explicit value observed end-to-end; nested cgroup inherits the parent's entry; explicit io.stream 0 resolves the sentinel to 0
- io.stream rejects streams above the device limit and the reserved 255 with -EINVAL, and devices without write streams with -EOPNOTSUPP; the root cgroup has no io.stream file
- the inode write hint beats the io.stream value, as documented
- IOSQE_ASYNC: an explicit stream, IORING_WRITE_STREAM_DEFAULT and the submitting task's cgroup association all survive io-wq deferral
- a 512K direct write splits into multiple bios; all keep the stream
- 64 concurrent writers with a common stream: 64/64 tagged, stable, no warnings
- buffered writeback on a mounted ext4: writeback bios adopt the first-dirtying cgroup via wbc_init_bio() and carry the resulting placement ID to the device; the nested cgroup inherits the parent's entry, explicit 0 and the child's own entry override it, and "inherit" restores the parent's value

Each patch builds without warnings at W=1, with CONFIG_BLK_CGROUP_IOSTREAM both enabled and disabled.

Not yet verified (please treat accordingly)

- WAF improvements on a production multi-tenant workload are not yet measured; device-level fdp status counters moved consistently with the placement IDs, but per-case deltas were below the background noise of the test setup

RFC aspects we would like feedback on

- the API shape of io.stream (per-device "MAJ:MIN <stream>" string, upsert and inherit semantics, explicit 0 as a real entry)
- the WRITE_STREAM_NONE sentinel as a u8 reservation, which caps addressable streams at 254 through the io_uring interface and excludes 255 from io.stream
- whether io.stream belongs in io.* under cgroup v2, and its interaction with the existing io controller file hierarchy
- buffered writeback semantics: attribution to the writeback owner is arguably what an operator wants, but degenerate share cases deserve a second opinion

No MAINTAINERS update is needed: block/ is covered by the BLOCK LAYER entry, and the io_uring paths by the IO_URING entry.

Tool use

This series was developed with substantial help from LLM coding assistants, in an interactive session rather than from a single prompt. The assistant drafted a large part of the implementation (the WRITE_STREAM_NONE sentinel and its central resolution, the fops.c precedence rework, the kiocb_clone() propagation, and the io.stream single-word policy state and its RCU lifetime), the changelogs, the cgroup-v2.rst section and this cover letter. It also wrote the io_uring and bpftrace test harnesses used for the runtime verification above, and rebased the series onto the current tree. I made the design decisions, directed and reviewed the testing, and reviewed, revised and take responsibility for all of the code and text.

Suho Son (3):
block: distinguish unspecified and explicit device-default write
streams
io_uring: add IORING_WRITE_STREAM_DEFAULT for explicit device-default
placement
blk-cgroup: add cgroup-based write stream placement

 Documentation/admin-guide/cgroup-v2.rst |  58 ++++-
 block/Kconfig                           |  16 ++
 block/Makefile                          |   1 +
 block/bio.c                             |   3 +-
 block/blk-core.c                        |  14 ++
 block/blk-iostream.c                    | 315 ++++++++++++++++++++++++
 block/blk-iostream.h                    |  18 ++
 block/fops.c                            |  25 +-
 include/linux/blk_types.h               |  13 +
 include/linux/fs.h                      |  11 +-
 include/linux/io_uring_types.h          |   4 +
 include/uapi/linux/io_uring.h           |  26 ++
 io_uring/rw.c                           |  16 ++
 13 files changed, 510 insertions(+), 10 deletions(-)
 create mode 100644 block/blk-iostream.c
 create mode 100644 block/blk-iostream.h

base-commit: d09b6d9d1fcce0770c1824c8b2b8ab153f2672d2

Signature · 1 line
2.43.0
author
#2 lore raw

RFC PATCH 1/3 block: distinguish unspecified and explicit device-default write streams

From: Suho Son <[email protected]>

bio->bi_write_stream uses zero to mean two different things: "the issuer did not express a stream" and "place this write in the device-default stream". With the io_uring per-I/O attribute and the inode write hint as the only producers this conflation is harmless, but it prevents a later, lower-precedence producer from telling whether a stream was requested at all.

Introduce WRITE_STREAM_NONE ((u8)-1) as the "unset" sentinel and initialize bios with it in bio_init()/bio_reset(). Resolve any residual sentinel to the device default (0) in submit_bio_noacct_nocheck(), before the bio can be turned into a request or merged, so drivers never observe the sentinel regardless of kernel configuration.

In blkdev_direct_IO(), keep the existing precedence and add the missing distinction: a concrete stream is validated against the device limit; otherwise the inode write hint is used if it was actually set (WRITE_LIFE_NOT_SET no longer maps to stream 0); otherwise the sentinel is left for later resolution.

Also carry ki_write_stream through kiocb_clone(), which is used by forwarded direct I/O (fs/backing-file.c, fs/erofs/ishare.c), so a clone does not silently drop the issuer's stream selection.

No driver changes: NVMe converts bi_write_stream to FDP placement identifiers as before.

---
 block/bio.c               |  3 ++-
 block/blk-core.c          | 12 ++++++++++++
 block/fops.c              | 25 +++++++++++++++++++------
 include/linux/blk_types.h | 13 +++++++++++++
 include/linux/fs.h        | 11 ++++++++++-
 5 files changed, 56 insertions(+), 8 deletions(-)
block/bio.c +2 -1
index b0fcc69de6535..0fbf0dfefe85c 100644
@@ -223,7 +223,7 @@ void bio_init(struct bio *bio, struct block_device *bdev, struct bio_vec *table,
 	bio->bi_flags = 0;
 	bio->bi_ioprio = 0;
 	bio->bi_write_hint = 0;
-	bio->bi_write_stream = 0;
+	bio->bi_write_stream = WRITE_STREAM_NONE;
 	bio->bi_status = 0;
 	bio->bi_bvec_gap_bit = 0;
 	bio->bi_iter.bi_sector = 0;
@@ -278,6 +278,7 @@ void bio_reset(struct bio *bio, struct block_device *bdev, blk_opf_t opf)
 	bio_uninit(bio);
 	memset(bio, 0, BIO_RESET_BYTES);
 	atomic_set(&bio->__bi_remaining, 1);
+	bio->bi_write_stream = WRITE_STREAM_NONE;
 	bio->bi_io_vec = bv;
 	bio->bi_bdev = bdev;
 	if (bio->bi_bdev)
block/blk-core.c +12 -0
index 2ee079c1ce150..5054a6e21020c 100644
@@ -765,6 +765,18 @@ void submit_bio_noacct_nocheck(struct bio *bio, bool split)
 
 	blk_cgroup_bio_start(bio);
 
+	/*
+	 * Resolve the write-stream sentinel before the bio can be turned into
+	 * a request or merged. By now every higher-precedence producer has had
+	 * its chance (an io_uring per-I/O attribute at init time, io.stream via
+	 * bio_set_ioprio()); a field still holding WRITE_STREAM_NONE expressed
+	 * no stream, so it falls back to the device default (0). This is the
+	 * single point that guarantees the sentinel never reaches the driver,
+	 * independent of CONFIG_BLK_CGROUP_IOSTREAM.
+	 */
+	if (bio->bi_write_stream == WRITE_STREAM_NONE)
+		bio->bi_write_stream = 0;
+
 	if (!bio_flagged(bio, BIO_TRACE_COMPLETION)) {
 		trace_block_bio_queue(bio);
 		/*
block/fops.c +19 -6
index a0ccadaa9c79d..4dab46da49032 100644
@@ -417,20 +417,33 @@ static ssize_t blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
 	if (iov_iter_rw(iter) == WRITE) {
 		u16 max_write_streams = bdev_max_write_streams(bdev);
 
-		if (iocb->ki_write_stream) {
+		if (iocb->ki_write_stream &&
+		    iocb->ki_write_stream != WRITE_STREAM_NONE) {
 			if (iocb->ki_write_stream > max_write_streams)
 				return -EINVAL;
-		} else if (max_write_streams) {
+		} else if (max_write_streams &&
+			   !(iocb->ki_flags & IOCB_WRITE_STREAM_EXPLICIT)) {
 			enum rw_hint write_hint =
 				file_inode(iocb->ki_filp)->i_write_hint;
 
 			/*
-			 * Just use the write hint as write stream for block
-			 * device writes.  This assumes no file system is
-			 * mounted that would use the streams differently.
+			 * No concrete stream was expressed on this I/O and
+			 * the issuer did not request the device default
+			 * explicitly either (in which case the concrete
+			 * stream 0 is kept so neither the inode write hint
+			 * nor a cgroup io.stream value applies). Use the
+			 * inode write hint as the write stream for block
+			 * device writes (this assumes no file system is
+			 * mounted that would use the streams differently);
+			 * otherwise leave the field as WRITE_STREAM_NONE so a
+			 * lower-precedence producer (io.stream) can fill it
+			 * in at submit time.
 			 */
-			if (write_hint <= max_write_streams)
+			if (write_hint != WRITE_LIFE_NOT_SET &&
+			    write_hint <= max_write_streams)
 				iocb->ki_write_stream = write_hint;
+			else
+				iocb->ki_write_stream = WRITE_STREAM_NONE;
 		}
 	}
 
include/linux/blk_types.h +13 -0
index 1a5812fa40529..016707437702f 100644
@@ -203,6 +203,19 @@ typedef __u32 __bitwise blk_opf_t;
 typedef unsigned int blk_qc_t;
 #define BLK_QC_T_NONE		-1U
 
+/*
+ * Sentinel value for bi_write_stream / ki_write_stream meaning "no write
+ * stream expressed". Distinguished from an explicit request for the
+ * device-default stream (0): a concrete stream set by an issuer (e.g. an
+ * io_uring per-I/O write attribute) always wins, while a field still holding
+ * WRITE_STREAM_NONE lets a lower-precedence producer (io.stream) fill it in.
+ * The submit path converts any residual WRITE_STREAM_NONE to 0 before the bio
+ * reaches the driver. Both fields are u8, so the sentinel is (u8)-1 = 255;
+ * kernel interfaces limit selectable stream identifiers to 0..254, so the
+ * sentinel never collides with a stream an issuer can request.
+ */
+#define WRITE_STREAM_NONE	((u8)-1)
+
 /*
  * main unit of I/O for the block layer and lower layers (ie drivers and
  * stacking drivers)
include/linux/fs.h +10 -1
index 99caef43ff3c4..e796a1858e187 100644
@@ -357,6 +357,13 @@ struct readahead_control;
 /* kiocb is a read or write operation submitted by fs/aio.c. */
 #define IOCB_AIO_RW		(1 << 22)
 #define IOCB_HAS_METADATA	(1 << 23)
+/*
+ * The issuer explicitly requested the device-default write stream
+ * (IORING_WRITE_STREAM_DEFAULT in the io_uring SQE): ki_write_stream
+ * holds a concrete 0 and must not be overridden by the inode write hint
+ * or a cgroup io.stream value.
+ */
+#define IOCB_WRITE_STREAM_EXPLICIT	BIT(24)
 
 /* for use in trace events */
 #define TRACE_IOCB_STRINGS \
@@ -374,7 +381,8 @@ struct readahead_control;
 	{ IOCB_NOIO,		"NOIO" }, \
 	{ IOCB_ALLOC_CACHE,	"ALLOC_CACHE" }, \
 	{ IOCB_AIO_RW,		"AIO_RW" }, \
-	{ IOCB_HAS_METADATA,	"AIO_HAS_METADATA" }
+	{ IOCB_HAS_METADATA,	"AIO_HAS_METADATA" }, \
+	{ IOCB_WRITE_STREAM_EXPLICIT, "WRITE_STREAM_EXPLICIT" }
 
 struct kiocb {
 	struct file		*ki_filp;
@@ -2202,6 +2210,7 @@ static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
 		.ki_filp = filp,
 		.ki_flags = kiocb_src->ki_flags,
 		.ki_ioprio = kiocb_src->ki_ioprio,
+		.ki_write_stream = kiocb_src->ki_write_stream,
 		.ki_pos = kiocb_src->ki_pos,
 	};
 }
Signature · 1 line
2.43.0
author
#3 lore raw

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

In reply to Suho Son

From: Suho Son <[email protected]>

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.

---
 include/linux/io_uring_types.h |  4 ++++
 include/uapi/linux/io_uring.h  | 26 ++++++++++++++++++++++++++
 io_uring/rw.c                  | 16 ++++++++++++++++
 3 files changed, 46 insertions(+)
include/linux/io_uring_types.h +4 -0
index 1468bcb028930..550928cd4ad95 100644
@@ -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 {
include/uapi/linux/io_uring.h +26 -0
index cf1afd11bb871..3ae504d9ec1a0 100644
@@ -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;
io_uring/rw.c +16 -0
index 3ecb842147a4d..d1d154533bf14 100644
@@ -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
Signature · 1 line
2.43.0
author
#4 lore raw

RFC PATCH 3/3 blk-cgroup: add cgroup-based write stream placement

In reply to Suho Son

From: Suho Son <[email protected]>

NVMe Flexible Data Placement (FDP) allows writes with similar lifetimes to be placed in separate reclaim units, reducing write amplification. However, existing write stream interfaces require applications to explicitly assign streams, leaving unmodified buffered-I/O workloads without an operator-controlled placement mechanism.

Introduce io.stream, a cgroup v2 I/O controller interface that allows operators to assign write streams on a per-device basis. Write I/O attributed to the cgroup carries the tag via bio->bi_write_stream:
direct writes issued by tasks in the cgroup, and buffered writeback owned by the cgroup (wbc_init_bio() re-associates writeback bios with the writeback owner's blkcg_css; requires CONFIG_CGROUP_WRITEBACK). Devices that support write streams (e.g. NVMe FDP) translate the tag into a placement identifier so similarly-aged data ends up in the same reclaim unit.

Key properties:

- Per-device entries are upserts; "MAJ:MIN inherit" restores the default state, which adopts the parent's effective stream for the device. An explicit stream 0 is a real entry which pins the cgroup to the device default and stops the inheritance walk.
- Configured streams are validated against the device queue limits; devices without write streams are rejected with -EOPNOTSUPP, and the reserved sentinel (255) is not selectable.
- Only bios still carrying the WRITE_STREAM_NONE sentinel are
filled: explicit application placement (an io_uring per-I/O stream, and filesystem-internal assignments such as f2fs) always wins.
- The policy state is a single word read with READ_ONCE() on the submission path; writers are serialized by the queue lock held across blkg_conf_prep()/blkg_conf_unprep(). Policy data is freed via kfree_rcu() so lockless readers are safe against policy deactivation, and the policy activates lazily on first use per disk, mirroring blk-throttle.

---
 Documentation/admin-guide/cgroup-v2.rst |  58 ++++-
 block/Kconfig                           |  16 ++
 block/Makefile                          |   1 +
 block/blk-core.c                        |   2 +
 block/blk-iostream.c                    | 315 ++++++++++++++++++++++++
 block/blk-iostream.h                    |  18 ++
 6 files changed, 408 insertions(+), 2 deletions(-)
 create mode 100644 block/blk-iostream.c
 create mode 100644 block/blk-iostream.h
Documentation/admin-guide/cgroup-v2.rst +56 -2
index 9e3c67b02ae2e..b0fd93f86027f 100644
@@ -2020,8 +2020,9 @@ blk-throttle and apply to all devices, while weight based proportional
 distribution is provided by the iocost cost model controller
 (CONFIG_BLK_CGROUP_IOCOST) and, when the BFQ I/O scheduler is in use
 for a device, by BFQ's own cgroup support.  Latency-based protection
-(CONFIG_BLK_CGROUP_IOLATENCY) and I/O priority assignment
-(CONFIG_BLK_CGROUP_IOPRIO) are also available.
+(CONFIG_BLK_CGROUP_IOLATENCY), I/O priority assignment
+(CONFIG_BLK_CGROUP_IOPRIO), and per-device write stream placement
+(CONFIG_BLK_CGROUP_IOSTREAM) are also available.
 
 
 IO Interface Files
@@ -2443,6 +2444,59 @@ The algorithm to set the I/O priority class for a request is as follows:
   into the maximum of the I/O priority class policy number and the numerical
   I/O priority class.
 
+IO Stream Placement
+~~~~~~~~~~~~~~~~~~~
+
+The stream placement cgroup policy (CONFIG_BLK_CGROUP_IOSTREAM) allows
+an operator to assign write streams on a per-device basis for write
+I/O attributed to a cgroup.  Write streams are a block-layer hint
+(bio->bi_write_stream) that devices which support write placement
+directives, such as NVMe Flexible Data Placement (FDP), translate into
+placement identifiers, so that data with similar lifetimes is grouped
+into the same reclaim unit.  Write I/O is attributed to the cgroup as
+follows: direct writes issued by tasks in the cgroup, and buffered
+writeback owned by the cgroup; the writeback case requires
+CONFIG_CGROUP_WRITEBACK.
+
+Only bios that do not already carry a stream are affected: a stream
+explicitly assigned by an application or by a file system always
+wins, and a write that expresses no stream at all is placed in the
+device-default stream.
+
+  io.stream
+	A read-write flat-keyed file which exists on non-root
+	cgroups.  Lines are keyed by $MAJ:$MIN device numbers.
+
+	Writing "MAJ:MIN <stream>" assigns the write stream to use on
+	that device.  Valid stream numbers are 0 up to the device's
+	maximum write stream count: 0 explicitly selects the
+	device-default stream, larger values select the corresponding
+	device stream.  Writing the same device twice replaces the
+	previous value.  Writing "MAJ:MIN inherit" restores the
+	default state, and a cgroup in the default state adopts the
+	effective stream of the closest ancestor with an entry for
+	that device; an explicit 0 is a real entry which stops this
+	inheritance walk.
+
+	Stream numbers above the device's maximum, and the reserved
+	value 255, are rejected with -EINVAL.  Configuring a device
+	which does not support write streams is rejected with
+	-EOPNOTSUPP.
+
+	Reading returns only the explicit entries; a cgroup in the
+	inherit state has no line for the device.  For example::
+
+	  8:16 3
+
+	To place the writes of a cgroup in stream 3 of a device
+	currently exposed as 8:16, write::
+
+	  echo "8:16 3" > io.stream
+
+On direct I/O to a raw block device, the file's write lifetime hint
+(F_SET_RW_HINT) takes precedence over the cgroup value; the cgroup
+value only applies to writes that express no stream.
+
 PID
 ---
 
block/Kconfig +16 -0
index 70e4a66d941ff..558d1251c4343 100644
@@ -170,6 +170,22 @@ config BLK_CGROUP_IOPRIO
 	scheduler and block devices process requests. Only some I/O schedulers
 	and some block devices support I/O priorities.
 
+config BLK_CGROUP_IOSTREAM
+	bool "Cgroup I/O controller for assigning a per-device write stream"
+	depends on BLK_CGROUP
+	help
+	Enable the io.stream interface that lets a cgroup associate a
+	block-layer write stream tag with a specific block device. Writes
+	attributed to the cgroup then carry that tag via
+	bio->bi_write_stream. This covers direct I/O issued by tasks in
+	the cgroup as well as buffered writeback owned by the cgroup
+	(which requires CONFIG_CGROUP_WRITEBACK), so devices that support
+	write streams (e.g. NVMe FDP) can group similarly-aged data into
+	the same reclaim unit, reducing write amplification and tail
+	latency for multi-tenant workloads.
+
+	If unsure, say N.
+
 config BLK_DEBUG_FS
 	bool "Block layer debugging information in debugfs"
 	default y
block/Makefile +1 -0
index e7bd320e3d697..4b76d99bec298 100644
@@ -21,6 +21,7 @@ obj-$(CONFIG_BLK_CGROUP_RWSTAT)	+= blk-cgroup-rwstat.o
 obj-$(CONFIG_BLK_CGROUP_FC_APPID) += blk-cgroup-fc-appid.o
 obj-$(CONFIG_BLK_DEV_THROTTLING)	+= blk-throttle.o
 obj-$(CONFIG_BLK_CGROUP_IOPRIO)	+= blk-ioprio.o
+obj-$(CONFIG_BLK_CGROUP_IOSTREAM)	+= blk-iostream.o
 obj-$(CONFIG_BLK_CGROUP_IOLATENCY)	+= blk-iolatency.o
 obj-$(CONFIG_BLK_CGROUP_IOCOST)	+= blk-iocost.o
 obj-$(CONFIG_MQ_IOSCHED_DEADLINE)	+= mq-deadline.o
block/blk-core.c +2 -0
index 5054a6e21020c..ac346f8fe0aa9 100644
@@ -51,6 +51,7 @@
 #include "blk-cgroup.h"
 #include "blk-throttle.h"
 #include "blk-ioprio.h"
+#include "blk-iostream.h"
 #include "error-injection.h"
 
 struct dentry *blk_debugfs_root;
@@ -952,6 +953,7 @@ static void bio_set_ioprio(struct bio *bio)
 	if (IOPRIO_PRIO_CLASS(bio->bi_ioprio) == IOPRIO_CLASS_NONE)
 		bio->bi_ioprio = get_current_ioprio();
 	blkcg_set_ioprio(bio);
+	blkcg_set_iostream(bio);
 }
 
 /**
block/blk-iostream.c new file +315 -0

315 lines not on this page · raw message

block/blk-iostream.h new file +18 -0
new file mode 100644
index 0000000000000..eb3ca243ad791
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#ifndef _BLK_IOSTREAM_H_
+#define _BLK_IOSTREAM_H_
+
+#include <linux/kconfig.h>
+
+struct bio;
+
+#ifdef CONFIG_BLK_CGROUP_IOSTREAM
+void blkcg_set_iostream(struct bio *bio);
+#else
+static inline void blkcg_set_iostream(struct bio *bio)
+{
+}
+#endif
+
+#endif /* _BLK_IOSTREAM_H_ */
Signature · 1 line
2.43.0
maintainer Nacked-by on the v1 series NAK
#5 lore raw
In reply to Suho Son

Hello, Suho.

On Mon, Nov 23, 2026 at 02:15:21PM +0900, Suho Son wrote:
...

Quoted 2 lines

- whether io.stream belongs in io.* under cgroup v2, and its interaction with the existing io controller file hierarchy

It doesn't. cgroup controllers and their interface files are for hierarchical resource control and this isn't that. If you want to select write streams per cgroup, feel free to identify the target cgroups by cgroup ID and use the internal APIs, but the configuration doesn't belong in the cgroup interface.

For the cgroup interface,

Thanks.

--
tejun

author
#6 lore raw

Hi Tejun,

Quoted 6 lines · Tejun Heo
Quoted 2 lines · Suho Son

- whether io.stream belongs in io.* under cgroup v2, and its interaction with the existing io controller file hierarchy

It doesn't. cgroup controllers and their interface files are for hierarchical resource control and this isn't that.

[...]

Quoted 1 line

Understood. I'll drop the cgroupfs interface.

Jens, would a minimal BPF hook for selecting the write stream in the submit path, with target cgroups identified by cgroup ID as Tejun suggested, be acceptable for the block layer, or would you prefer a different approach? This is for unmodified workloads, including their buffered writeback.

Adding Christoph, Keith and Pavel.

Thanks,
Suho