[Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit

Andrey Zhadchenko andrey.zhadchenko at virtuozzo.com
Tue Dec 1 16:52:05 MSK 2026


dm-qcow2 submits image file I/O from tgt->wq worker kthreads, which
belong to the root cgroup. As a result, bios issued to the underlying
device get associated with the root blkcg instead of the cgroup that
submitted the dm request, bypassing block cgroup limits configured on
the underlying device.

Mark split, compressed-read, and COW child qios whose endio_cb_data
points to a parent qio with QIO_HAS_PARENT_FL so qio_blkcg_css() can
walk up to the embedded root qio (endio_cb == qrq_endio) and obtain
bio_blkcg_css(qrq->rq->bio).

No extra css_get()/css_put() is needed on qrq: rq->bio->bi_blkg holds
a reference on the blkcg css until dm_complete_request() in qrq_endio(),
and kthread_associate_blkcg() takes its own reference across
qcow2_call_rw_iter().

https://virtuozzo.atlassian.net/jira/browse/VSTOR-143115

Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Andrey Zhadchenko <andrey.zhadchenko at virtuozzo.com>
---
 drivers/md/dm-qcow2-map.c | 36 +++++++++++++++++++++++++++++++-----
 drivers/md/dm-qcow2.h     |  1 +
 2 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 6be270e6c12cf..a3488d197fec2 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -7,6 +7,8 @@
 #include <linux/fs.h>
 #include <uapi/linux/falloc.h>
 #include <linux/blk-mq.h>
+#include <linux/blk-cgroup.h>
+#include <linux/kthread.h>
 #include <linux/zlib.h>
 #include <linux/error-injection.h>
 #include <linux/zstd.h>
@@ -92,6 +94,7 @@ static void process_read_qio(struct qcow2 *qcow2, struct qio *qio,
 			     struct qcow2_map *map);
 static void init_qrq_and_embedded_qio(struct qcow2_target *tgt, struct request *rq,
 				      struct qcow2_rq *qrq, struct qio *qio);
+static struct cgroup_subsys_state *qio_blkcg_css(struct qio *qio);
 static void qcow2_fast_submit(struct qcow2_target *tgt, struct qio *qio);
 
 static loff_t bytes_off_in_cluster(struct qcow2 *qcow2, struct qio *qio)
@@ -496,6 +499,7 @@ static void qio_chain(struct qio *qio, struct qio *parent)
 {
 	WARN_ON(qio->endio_cb_data || qio->endio_cb);
 
+	qio->flags |= QIO_HAS_PARENT_FL;
 	qio->endio_cb_data = parent;
 	qio->endio_cb = qio_chain_endio;
 	atomic_inc(&parent->remaining);
@@ -1387,6 +1391,7 @@ static void dec_cluster_usage(struct qcow2 *qcow2, struct md_page *r2_md,
 
 static void __submit_rw_mapped(struct qcow2 *qcow2, struct qio *qio, u32 nr_segs)
 {
+	struct cgroup_subsys_state *css;
 	struct bio_vec *bvec;
 	struct iov_iter iter;
 	unsigned int rw;
@@ -1400,7 +1405,12 @@ static void __submit_rw_mapped(struct qcow2 *qcow2, struct qio *qio, u32 nr_segs
 	iov_iter_bvec(&iter, rw, bvec, nr_segs, qio->bi_iter.bi_size);
 	iter.iov_offset = qio->bi_iter.bi_bvec_done;
 
+	css = qio_blkcg_css(qio);
+	if (css)
+		kthread_associate_blkcg(css);
 	qcow2_call_rw_iter(qcow2, pos, rw, &iter, qio);
+	if (css)
+		kthread_associate_blkcg(NULL);
 }
 
 static void submit_rw_mapped(struct qcow2 *qcow2, struct qio *qio)
@@ -3196,7 +3206,7 @@ static void submit_read_whole_cow_clu(struct qcow2_map *map, struct qio *qio)
 	read_qio->bi_iter.bi_sector = 0;
 	read_qio->complete = data_rw_complete;
 	read_qio->data = NULL;
-	read_qio->flags |= QIO_FREE_ON_ENDIO_FL;
+	read_qio->flags |= QIO_FREE_ON_ENDIO_FL | QIO_HAS_PARENT_FL;
 	read_qio->endio_cb = cow_read_endio;
 	read_qio->endio_cb_data = qio;
 
@@ -3479,7 +3489,7 @@ static void submit_read_compressed(struct qcow2_map *map, struct qio *qio,
 		return;
 	}
 
-	read_qio->flags |= QIO_FREE_ON_ENDIO_FL;
+	read_qio->flags |= QIO_FREE_ON_ENDIO_FL | QIO_HAS_PARENT_FL;
 	read_qio->endio_cb = compressed_read_endio;
 	read_qio->endio_cb_data = qio;
 
@@ -3575,7 +3585,7 @@ static void submit_read_sliced_clu(struct qcow2_map *map, struct qio *qio,
 	read_qio = qcow2_alloc_qio_with_qvec(qcow2, nr_pages, REQ_OP_READ, true, &qvec);
 	if (!read_qio)
 		goto err_alloc;
-	read_qio->flags |= QIO_FREE_ON_ENDIO_FL;
+	read_qio->flags |= QIO_FREE_ON_ENDIO_FL | QIO_HAS_PARENT_FL;
 	read_qio->endio_cb = endio_cb;
 	read_qio->endio_cb_data = qio;
 
@@ -4276,7 +4286,7 @@ static int prepare_sliced_data_write(struct qcow2 *qcow2, struct qio *qio,
 	if (!write_qio)
 		goto err_qio;
 	qcow2_init_qio(write_qio, REQ_OP_WRITE, qcow2);
-	write_qio->flags |= QIO_FREE_ON_ENDIO_FL;
+	write_qio->flags |= QIO_FREE_ON_ENDIO_FL | QIO_HAS_PARENT_FL;
 	write_qio->endio_cb = endio;
 	write_qio->endio_cb_data = qio;
 
@@ -4373,7 +4383,7 @@ static void submit_cow_data_write(struct qcow2 *qcow2, struct qio *qio, loff_t p
 	}
 	qcow2_init_qio(write_qio, REQ_OP_WRITE, qcow2);
 
-	write_qio->flags |= QIO_FREE_ON_ENDIO_FL;
+	write_qio->flags |= QIO_FREE_ON_ENDIO_FL | QIO_HAS_PARENT_FL;
 	write_qio->bi_io_vec = qvec->bvec;
 	write_qio->bi_iter.bi_size = clu_size;
 	write_qio->bi_iter.bi_idx = 0;
@@ -4733,6 +4743,22 @@ static void qrq_endio(struct qcow2_target *tgt, struct qio *qio,
 	dm_complete_request(rq, bi_status);
 }
 
+static struct cgroup_subsys_state *qio_blkcg_css(struct qio *qio)
+{
+	struct cgroup_subsys_state *css;
+	struct qcow2_rq *qrq;
+
+	while (qio->flags & QIO_HAS_PARENT_FL)
+		qio = qio->endio_cb_data;
+
+	if (qio->endio_cb != qrq_endio)
+		return NULL;
+
+	qrq = qio->endio_cb_data;
+	css = bio_blkcg_css(qrq->rq->bio);
+	return css == blkcg_root_css ? NULL : css;
+}
+
 static void init_qrq(struct qcow2_rq *qrq, struct request *rq)
 {
 	qrq->rq = rq;
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 6c5d3e932691c..da923ff288a64 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -362,6 +362,7 @@ struct qio {
 	unsigned int bi_op;
 	blk_status_t bi_status;
 #define QIO_FREE_ON_ENDIO_FL	(1 << 0) /* Free this qio memory from qio_endio() */
+#define QIO_HAS_PARENT_FL	(1 << 1) /* endio_cb_data points to parent qio */
 #define QIO_IS_MERGE_FL		(1 << 3) /* This is service merge qio */
 #define QIO_IS_DISCARD_FL	(1 << 4) /* This zeroes index (discard or backward merge) */
 #define QIO_IS_L1COW_FL		(1 << 5) /* This qio only wants COW at L1 */
-- 
2.43.5



More information about the Devel mailing list