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

From: Suho Son

Date: Mon Nov 23 2026 - 00:27:34 EST


From: Suho Son <suho.son@xxxxxxxxxxx>

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.

Link: https://doi.org/10.1145/3837053.3837343

Signed-off-by: Suho Son <suho.son@xxxxxxxxxxx>
---
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

diff --git a/Documentation/admin-guide/cgroup-v2.rst b/Documentation/admin-guide/cgroup-v2.rst
index 9e3c67b02ae2e..b0fd93f86027f 100644
--- a/Documentation/admin-guide/cgroup-v2.rst
+++ b/Documentation/admin-guide/cgroup-v2.rst
@@ -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
---

diff --git a/block/Kconfig b/block/Kconfig
index 70e4a66d941ff..558d1251c4343 100644
--- a/block/Kconfig
+++ b/block/Kconfig
@@ -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
diff --git a/block/Makefile b/block/Makefile
index e7bd320e3d697..4b76d99bec298 100644
--- a/block/Makefile
+++ b/block/Makefile
@@ -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
diff --git a/block/blk-core.c b/block/blk-core.c
index 5054a6e21020c..ac346f8fe0aa9 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -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);
}

/**
diff --git a/block/blk-iostream.c b/block/blk-iostream.c
new file mode 100644
index 0000000000000..c3577c0e24601
--- /dev/null
+++ b/block/blk-iostream.c
@@ -0,0 +1,315 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Block cgroup policy that assigns a per-device write stream tag.
+ *
+ * The user writes "MAJ:MIN <stream>" into io.stream of a cgroup. Write
+ * I/O attributed to that cgroup then carries the tag via
+ * bio->bi_write_stream. This covers 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).
+ * 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.
+ *
+ * Per-device entries are upserted, not appended: writing the same
+ * MAJ:MIN twice replaces the previous value. The default state of a
+ * cgroup is inherit, which adopts the parent's effective stream for
+ * that device. An explicit stream of 0 is a distinct configuration
+ * that pins the cgroup to the device default stream and blocks
+ * inheritance. Writing "MAJ:MIN inherit" restores the default
+ * inherit state.
+ */
+
+#include <linux/bio.h>
+#include <linux/blk-mq.h>
+#include <linux/blk_types.h>
+#include <linux/blkdev.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/rcupdate.h>
+#include <linux/seq_file.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+
+#include "blk-cgroup.h"
+#include "blk-iostream.h"
+
+static struct blkcg_policy blkcg_policy_iostream;
+
+/*
+ * Per-(cgroup, device) data. The first member must embed
+ * struct blkg_policy_data so the blkcg core can hand us a pd pointer
+ * that round-trips through container_of().
+ *
+ * @stream is the whole policy state in a single word:
+ *
+ * IOSTREAM_INHERIT (-1): the default state; adopt the parent's
+ * effective stream for this device.
+ * 0: explicit device-default placement; a real
+ * entry that stops the inheritance walk.
+ * 1..max_write_streams: explicit stream identifier.
+ *
+ * Writers are serialized by the queue lock, which blkg_conf_prep()
+ * holds until blkg_conf_unprep() (see iostream_write()). A lockless
+ * READ_ONCE() of the single word therefore yields either the previous
+ * or the new complete configuration, never a mix of both.
+ */
+#define IOSTREAM_INHERIT (-1)
+
+struct iostream_grp {
+ struct blkg_policy_data pd;
+ int stream;
+ struct rcu_head rcu;
+};
+
+static struct iostream_grp *pd_to_isg(struct blkg_policy_data *pd)
+{
+ return pd ? container_of(pd, struct iostream_grp, pd) : NULL;
+}
+
+static struct iostream_grp *blkg_to_isg(struct blkcg_gq *blkg)
+{
+ return pd_to_isg(blkg_to_pd(blkg, &blkcg_policy_iostream));
+}
+
+static struct blkg_policy_data *iostream_pd_alloc(struct gendisk *disk,
+ struct blkcg *blkcg,
+ gfp_t gfp)
+{
+ struct iostream_grp *isg;
+
+ isg = kzalloc_node(sizeof(*isg), gfp, disk->node_id);
+ if (!isg)
+ return NULL;
+
+ return &isg->pd;
+}
+
+static void iostream_pd_init(struct blkg_policy_data *pd)
+{
+ struct iostream_grp *isg = pd_to_isg(pd);
+
+ isg->stream = IOSTREAM_INHERIT;
+}
+
+static void iostream_pd_free(struct blkg_policy_data *pd)
+{
+ /*
+ * blkcg_set_iostream() dereferences the pd from the bio submission
+ * path under rcu_read_lock() without holding the queue or blkcg
+ * lock, so the memory must survive an RCU grace period after it is
+ * detached from blkg->pd[]. This covers both free paths:
+ * blkcg_deactivate_policy(), which frees the pd immediately while
+ * bios may still be in flight, and blkg_free_workfn(), which only
+ * runs once the last blkg reference (including the one held by any
+ * in-flight bio) is dropped.
+ */
+ kfree_rcu(pd_to_isg(pd), rcu);
+}
+
+static u64 iostream_prfill(struct seq_file *sf, struct blkg_policy_data *pd,
+ int off)
+{
+ struct iostream_grp *isg = pd_to_isg(pd);
+ const char *dname = blkg_dev_name(pd->blkg);
+ int stream;
+
+ /*
+ * Called under rcu_read_lock() from blkcg_print_blkgs(), so the
+ * pd dereference is safe against iostream_pd_free(). Report every
+ * explicit configuration, including an explicit stream 0, so the
+ * readback reflects what was written.
+ */
+ if (!dname)
+ return 0;
+
+ stream = READ_ONCE(isg->stream);
+ if (stream == IOSTREAM_INHERIT)
+ return 0;
+
+ seq_printf(sf, "%s %u\n", dname, stream);
+ return 0;
+}
+
+static int iostream_show(struct seq_file *sf, void *v)
+{
+ blkcg_print_blkgs(sf, css_to_blkcg(seq_css(sf)), iostream_prfill,
+ &blkcg_policy_iostream, 0, false);
+ return 0;
+}
+
+static ssize_t iostream_write(struct kernfs_open_file *of, char *buf,
+ size_t nbytes, loff_t off)
+{
+ struct blkcg *blkcg = css_to_blkcg(of_css(of));
+ struct blkg_conf_ctx ctx;
+ struct iostream_grp *isg;
+ char *body;
+ unsigned int v;
+ char dummy;
+ int ret;
+
+ blkg_conf_init(&ctx, buf);
+
+ ret = blkg_conf_open_bdev(&ctx);
+ if (ret)
+ goto out_finish;
+
+ /*
+ * The policy is activated lazily on first use of io.stream for a
+ * given disk, mirroring blk-throttle's blk_throtl_init() pattern.
+ * Devices that do not support write streams are rejected up front.
+ */
+ if (!bdev_max_write_streams(ctx.bdev)) {
+ ret = -EOPNOTSUPP;
+ goto close_bdev;
+ }
+
+ if (!blkcg_policy_enabled(ctx.bdev->bd_queue, &blkcg_policy_iostream)) {
+ ret = blkcg_activate_policy(ctx.bdev->bd_disk,
+ &blkcg_policy_iostream);
+ if (ret)
+ goto close_bdev;
+ }
+
+ ret = blkg_conf_prep(blkcg, &blkcg_policy_iostream, &ctx);
+ if (ret)
+ goto close_bdev;
+
+ isg = blkg_to_isg(ctx.blkg);
+ body = strim(ctx.body);
+ ret = -EINVAL;
+
+ if (body[0] && !strncasecmp(body, "inherit", 7) && body[7] == '\0') {
+ /* restore the default inherit state */
+ WRITE_ONCE(isg->stream, IOSTREAM_INHERIT);
+ ret = 0;
+ } else if (sscanf(body, "%u %c", &v, &dummy) == 1 &&
+ v <= bdev_max_write_streams(ctx.bdev) &&
+ v <= U8_MAX && v != WRITE_STREAM_NONE) {
+ /*
+ * bi_write_stream is a u8 that reserves 255
+ * (WRITE_STREAM_NONE) as the "unset" sentinel, hence the
+ * U8_MAX bound and the WRITE_STREAM_NONE exclusion: a
+ * device advertising more than 254 streams cannot have
+ * its stream 255 selected through this interface. The
+ * single-word @stream state and its locking are
+ * described at struct iostream_grp.
+ */
+ WRITE_ONCE(isg->stream, (int)v);
+ ret = 0;
+ }
+
+ blkg_conf_unprep(&ctx);
+
+close_bdev:
+ blkg_conf_close_bdev(&ctx);
+out_finish:
+ return ret ?: nbytes;
+}
+
+/**
+ * blkcg_set_iostream - apply the cgroup's per-device write stream to a bio
+ * @bio: the bio about to be submitted
+ *
+ * Called from bio_set_ioprio() in the submit_bio() path with
+ * bio->bi_blkg already attached (see bio_associate_blkg() in
+ * bio_init/bio_reset). All write bios funnel through this hook: direct
+ * I/O issued by tasks in the cgroup and buffered writeback owned by
+ * the cgroup (wbc_init_bio() associates writeback bios with the
+ * writeback owner's blkcg_css, which requires CONFIG_CGROUP_WRITEBACK).
+ * Reads are naturally skipped by the op_is_write() filter. Devices
+ * that do not advertise write streams are skipped.
+ *
+ * Inheritance: the walk visits the bio's blkg and its ancestors and
+ * stops at the first one with an explicit entry for this device. A
+ * cgroup in the default inherit state (no entry) adopts the parent's
+ * effective stream; an explicit 0 is a real entry that stops the walk
+ * and pins the bio to the device default stream.
+ *
+ * Precedence: if the bio already carries a concrete bi_write_stream
+ * (i.e. anything other than WRITE_STREAM_NONE, e.g. set by an io_uring
+ * per-I/O write attribute), leave it alone — the more specific source
+ * wins. The cgroup value only fills in a bio that expressed no stream.
+ * A residual WRITE_STREAM_NONE (no issuer stream and no cgroup stream)
+ * is resolved to the device-default 0 later in the submit path
+ * (submit_bio_noacct_nocheck()), so the driver never sees the sentinel.
+ *
+ * Locking: the blkg and its ->parent chain are stable for the lifetime
+ * of the bio's blkg reference (blkg_tryget_closest(), blkg_get(parent)
+ * at creation), but the per-device policy data is not: it can be freed
+ * by blkcg_deactivate_policy() while a bio is still in flight. The pd
+ * is therefore only dereferenced inside an RCU read-side critical
+ * section, pairing with the kfree_rcu() in iostream_pd_free(). The
+ * enabled check is repeated inside the RCU section so the walk cannot
+ * race past policy deactivation. See struct iostream_grp for the
+ * locking of the single-word policy state.
+ */
+void blkcg_set_iostream(struct bio *bio)
+{
+ struct blkcg_gq *blkg = bio->bi_blkg;
+ int stream = IOSTREAM_INHERIT;
+
+ if (!op_is_write(bio->bi_opf))
+ return;
+ if (bio->bi_write_stream != WRITE_STREAM_NONE)
+ return;
+ if (!bdev_max_write_streams(bio->bi_bdev))
+ return;
+
+ rcu_read_lock();
+ if (blkcg_policy_enabled(blkg->q, &blkcg_policy_iostream)) {
+ /*
+ * Walk up to find the first ancestor with an explicit
+ * entry for this device, including an explicit 0.
+ */
+ while (blkg) {
+ struct iostream_grp *isg = blkg_to_isg(blkg);
+ int s;
+
+ if (!isg)
+ break;
+ s = READ_ONCE(isg->stream);
+ if (s != IOSTREAM_INHERIT) {
+ stream = s;
+ break;
+ }
+ blkg = blkg->parent;
+ }
+ }
+ rcu_read_unlock();
+
+ if (stream != IOSTREAM_INHERIT)
+ bio->bi_write_stream = stream;
+}
+
+static struct cftype iostream_files[] = {
+ {
+ .name = "stream",
+ .flags = CFTYPE_NOT_ON_ROOT,
+ .seq_show = iostream_show,
+ .write = iostream_write,
+ },
+ { } /* sentinel */
+};
+
+static struct blkcg_policy blkcg_policy_iostream = {
+ .dfl_cftypes = iostream_files,
+
+ .pd_alloc_fn = iostream_pd_alloc,
+ .pd_init_fn = iostream_pd_init,
+ .pd_free_fn = iostream_pd_free,
+};
+
+static int __init iostream_init(void)
+{
+ return blkcg_policy_register(&blkcg_policy_iostream);
+}
+
+static void __exit iostream_exit(void)
+{
+ blkcg_policy_unregister(&blkcg_policy_iostream);
+}
+
+module_init(iostream_init);
+module_exit(iostream_exit);
diff --git a/block/blk-iostream.h b/block/blk-iostream.h
new file mode 100644
index 0000000000000..eb3ca243ad791
--- /dev/null
+++ b/block/blk-iostream.h
@@ -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_ */
--
2.43.0