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