From: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
To: alexander.ivanov@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2] dm-qcow2: associate worker kthread with request blkcg on submit
Date: Tue, 1 Dec 2026 16:52:05 +0300 [thread overview]
Message-ID: <20261201135205.163133-1-andrey.zhadchenko@virtuozzo.com> (raw)
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
next reply other threads:[~2026-12-01 13:58 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-01 13:52 Andrey Zhadchenko [this message]
2026-12-01 14:04 ` no-reply
2026-12-01 14:29 ` Konstantin Khorenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201135205.163133-1-andrey.zhadchenko@virtuozzo.com \
--to=andrey.zhadchenko@virtuozzo.com \
--cc=alexander.ivanov@virtuozzo.com \
--cc=devel@openvz.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.