From: Suho Son 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. Signed-off-by: Suho Son --- 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(-) diff --git a/block/bio.c b/block/bio.c index b0fcc69de6535..0fbf0dfefe85c 100644 --- a/block/bio.c +++ b/block/bio.c @@ -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) diff --git a/block/blk-core.c b/block/blk-core.c index 2ee079c1ce150..5054a6e21020c 100644 --- a/block/blk-core.c +++ b/block/blk-core.c @@ -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); /* diff --git a/block/fops.c b/block/fops.c index a0ccadaa9c79d..4dab46da49032 100644 --- a/block/fops.c +++ b/block/fops.c @@ -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; } } diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index 1a5812fa40529..016707437702f 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -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) diff --git a/include/linux/fs.h b/include/linux/fs.h index 99caef43ff3c4..e796a1858e187 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -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, }; } -- 2.43.0 From: Suho Son 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 --- 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 From: Suho Son 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 --- 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 " 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 " 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#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 + +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