OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit
@ 2026-12-01 13:52 Andrey Zhadchenko
  2026-12-01 14:04 ` no-reply
  2026-12-01 14:29 ` Konstantin Khorenko
  0 siblings, 2 replies; 3+ messages in thread
From: Andrey Zhadchenko @ 2026-12-01 13:52 UTC (permalink / raw)
  To: alexander.ivanov; +Cc: devel

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@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

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit
  2026-12-01 13:52 [Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit Andrey Zhadchenko
@ 2026-12-01 14:04 ` no-reply
  2026-12-01 14:29 ` Konstantin Khorenko
  1 sibling, 0 replies; 3+ messages in thread
From: no-reply @ 2026-12-01 14:04 UTC (permalink / raw)
  To: Andrey Zhadchenko, alexander.ivanov; +Cc: devel

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] dm-qcow2: merge aux_qio lacks QIO_HAS_PARENT_FL, losing blkcg
  association
--

commit 0c8c7773401f0f94f9a8fd9cadaabfa9340d3b77
Author: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>

dm-qcow2: associate worker kthread with request blkcg on submit

dm-qcow2 submits image file I/O from tgt->wq worker kthreads, which belong
to the root cgroup. This patch marks child qios that point to a parent qio
with a new QIO_HAS_PARENT_FL flag. qio_blkcg_css() then walks up to the
embedded root qio and calls kthread_associate_blkcg() around
qcow2_call_rw_iter().

> diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
> --- a/drivers/md/dm-qcow2-map.c
> +++ b/drivers/md/dm-qcow2-map.c
[ ... ]
> +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;

[Severity: Low]
Does this walk miss the aux_qio created in prepare_backward_merge()? That
function sets endio_cb_data to the parent qio but does not set
QIO_HAS_PARENT_FL:

	aux_qio->flags = QIO_IS_MERGE_FL|QIO_FREE_ON_ENDIO_FL;
	...
	aux_qio->endio_cb = requeue_if_ok;
	aux_qio->endio_cb_data = *qio;

When a read hits a cluster under backward merge, a child qio (for example
the compressed or COW read with merge=true) walks up to aux_qio and stops.
Since requeue_if_ok != qrq_endio, qio_blkcg_css() returns NULL. That I/O is
then submitted from the root-cgroup worker, so the submitter's block cgroup
limits are bypassed on this path.

Should QIO_HAS_PARENT_FL be added to the flags assignment in
prepare_backward_merge() so the walk continues to the original qio?

Carry the trailer below into the next revision and Sashiko will not review this patch again.

Reviewed-by: Sashiko <no-reply@sashiko.virtuozzo.com>

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261201135205.163133-1-andrey.zhadchenko@virtuozzo.com?part=1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit
  2026-12-01 13:52 [Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit Andrey Zhadchenko
  2026-12-01 14:04 ` no-reply
@ 2026-12-01 14:29 ` Konstantin Khorenko
  1 sibling, 0 replies; 3+ messages in thread
From: Konstantin Khorenko @ 2026-12-01 14:29 UTC (permalink / raw)
  To: Andrey Zhadchenko; +Cc: devel

I won't apply this version, please send v2 with the changes below.
The walk itself is fine: every flagged qio points to a live parent,
and rq->bio->bi_blkg holds the css while the child is submitted.

- flag aux_qio in prepare_backward_merge() too
- say in the message what changes for the host disk and what stays
  in the root blkcg
- Jira URL: https://virtuozzo.atlassian.net/browse/VSTOR-143115

On Tue, 1 Dec 2026 16:52:05 +0300, Andrey Zhadchenko wrote:
> 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

aux_qio in prepare_backward_merge() has endio_cb_data = *qio as well
and is not flagged, so a backward merge copy started by a guest read
goes to the root blkcg, and the same copy started by a write goes to
the Container. The list here also misses the sliced read and write
children the patch flags.

The message should also say:
- the Container's limits and weights on the host disk now apply to
  its image I/O, COW included, and io.stat of the Container shows it;
- with io.cost or io.latency on the host disk a bio over budget puts
  the shared tgt->wq worker to sleep, and the other qios of that
  batch (other Containers, host I/O, the metadata writeback) wait
  with it; io.max only queues the bio;
- metadata reads and writeback, service merge qios and buffered
  fallbacks stay in the root blkcg.

dm-ploop has the nokblkcg table argument to turn this off. Does
dm-qcow2 need the same, or is it not used anywhere?

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

The tracker link form in vz commits is
https://virtuozzo.atlassian.net/browse/VSTOR-143115

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-01 14:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-01 13:52 [Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit Andrey Zhadchenko
2026-12-01 14:04 ` no-reply
2026-12-01 14:29 ` Konstantin Khorenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox