All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: "Denis V. Lunev" <den@openvz.org>
To: andrey.zhadchenko@virtuozzo.com, alexander.ivanov@virtuozzo.com,
	vasileios.almpanis@virtuozzo.com
Cc: devel@openvz.org
Subject: [Devel] [PATCH hci-8.2 4/9] dm-qcow2: map fast path writes without md_pages_lock
Date: Wed, 25 Nov 2026 02:25:20 +0100	[thread overview]
Message-ID: <20261125012525.681662-5-den@openvz.org> (raw)
In-Reply-To: <20261125012525.681662-1-den@openvz.org>

From: Denis V. Lunev <den@openvz.org>

Fast path writes still take md_pages_lock in parse_metadata() to map
the cluster and count themselves in the L2 page's active_writers. With
reads off the lock it is the remaining global serialization point for
an overwrite workload.

Map plain writes of allocated clusters through
parse_metadata_lockless() too, then count the write and recheck the
L2 page's md->gen. A discard that unmaps the cluster locks its index,
which bumps md->gen, long before it waits for writers in
delay_discard_if_writers(). With a full barrier on both sides, either
the write sees the new md->gen, uncounts itself and takes the locked
path, or the discard sees the count and waits for the write to finish
before the cluster can be reused. The barrier is taken on entry, so a
discard that re-enters to recheck the generation it waits for is
covered as well.

The write reads md->writer_gen without the lock. If it reads the
generation the discard has just switched to, the switch comes after
the md->gen bump and the discard's barrier, so the write's recheck
sees the bump and backs off. If it reads the old one, the discard
waits for it as for any earlier write.

Discards and write-zeroes keep the locked parse.

https://virtuozzo.atlassian.net/browse/VSTOR-95494
Feature: dm-qcow2: block device over QCOW2 files driver
Signed-off-by: Denis V. Lunev <den@openvz.org>
---
 drivers/md/dm-qcow2-map.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index b22cb5c90918..9d68d713ec3b 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -621,7 +621,7 @@ static bool delay_if_has_wpc_readers(struct qcow2 *qcow2, struct md_page *md,
 static void inc_writers(struct md_page *md, struct qio *qio)
 {
 	qio->writer_md = md;
-	qio->writer_gen = md->writer_gen;
+	qio->writer_gen = READ_ONCE(md->writer_gen);
 	atomic_inc(&md->active_writers[qio->writer_gen]);
 }
 
@@ -670,6 +670,14 @@ static bool delay_discard_if_writers(struct qcow2 *qcow2, struct md_page *md,
 	u8 gen;
 
 	spin_lock_irq(&qcow2->md_pages_lock);
+	/*
+	 * md->active_writers checks are valid only if increases are from
+	 * main kwork (as we are), under md_pages_lock, or from a lockless
+	 * fast path write, which counts itself and then rechecks md->gen,
+	 * bumped by md_index_set_locked() when this discard locked the index.
+	 * Pairs with smp_mb__after_atomic() in parse_metadata_lockless().
+	 */
+	smp_mb();
 	if (qio->flags & QIO_DISCARD_WAIT_FL) {
 		if (atomic_read(&md->active_writers[qio->writer_gen]))
 			goto delay;
@@ -679,10 +687,6 @@ static bool delay_discard_if_writers(struct qcow2 *qcow2, struct md_page *md,
 		goto unlock;
 	}
 
-	/*
-	 * md->active_writers checks are valid only if increases are
-	 * from main kwork (as we are) or under md_pages_lock.
-	 */
 	gen = md->writer_gen;
 	if (md->discard_waiters) {
 		/*
@@ -2130,10 +2134,11 @@ static bool md_page_stable(struct md_page *md, unsigned int *gen)
 }
 
 /*
- * Map a read of an allocated cluster without md_pages_lock. Both L1 and
- * L2 pages must be cached and clean, and the cluster mapped exactly once,
- * not compressed and not all-zeroes. A page change seen through md->gen,
- * or any other case, returns false, and the caller parses under the lock.
+ * Map a read or write of an allocated cluster without md_pages_lock. Both
+ * L1 and L2 pages must be cached and clean, and the cluster mapped exactly
+ * once, not compressed and not all-zeroes. A page change seen through
+ * md->gen, or any other case, returns false, and the caller parses under
+ * the lock.
  */
 static bool parse_metadata_lockless(struct qcow2 *qcow2, struct qio *qio,
 				    struct qcow2_map *map, u64 *ret_pos)
@@ -2176,6 +2181,16 @@ static bool parse_metadata_lockless(struct qcow2 *qcow2, struct qio *qio,
 	if (READ_ONCE(l1->gen) != l1_gen || READ_ONCE(l2->gen) != l2_gen)
 		goto out;
 
+	if (op_is_write(qio->bi_op)) {
+		inc_writers(l2, qio);
+		/* Pairs with smp_mb() in delay_discard_if_writers() */
+		smp_mb__after_atomic();
+		if (READ_ONCE(l2->gen) != l2_gen) {
+			dec_writers(qio);
+			goto out;
+		}
+	}
+
 	map->l1.md = l1;
 	map->l2.md = l2;
 	map->level = L1_LEVEL | L2_LEVEL;
@@ -2215,7 +2230,8 @@ static int parse_metadata(struct qcow2 *qcow2, struct qio **qio,
 	WARN_ON_ONCE(map->data_clu_pos != 0);
 	if (qcow2_calc_cluster_map(qcow2, *qio, map) < 0)
 		return -EIO;
-	if (avoid_metadata_changes && !write &&
+	if (avoid_metadata_changes &&
+	    (!write || (*qio)->bi_op == REQ_OP_WRITE) &&
 	    parse_metadata_lockless(qcow2, *qio, map, &pos)) {
 		map->data_clu_pos = pos;
 		return 0;
-- 
2.55.0

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

  parent reply	other threads:[~2026-11-25  1:26 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25  1:25 [Devel] [PATCH hci-8.2 0/9] dm-qcow2: lockless fast path, direct completion, no I/O scheduler Denis V. Lunev
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 1/9] dm-qcow2: move md_pages_lock off the deferred_lock cache line Denis V. Lunev
2026-12-01  0:05   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 2/9] dm-qcow2: free md pages after an RCU grace period Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:06   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 3/9] dm-qcow2: map fast path reads without md_pages_lock Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:17   ` no-reply
2026-11-25  1:25 ` Denis V. Lunev [this message]
2026-11-25 11:19   ` [Devel] [PATCH hci-8.2 4/9] dm-qcow2: map fast path writes " Konstantin Khorenko
2026-12-01  0:25   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 5/9] dm-qcow2: submit fast path qios without deferred_lock Denis V. Lunev
2026-12-01  0:27   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 6/9] dm-rq: complete dm-qcow2 requests directly in task context Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:35   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 7/9] dm-qcow2: requeue requests when the request pool is empty Denis V. Lunev
2026-12-01  0:37   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 8/9] dm-ploop: " Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:38   ` no-reply
2026-11-25  1:25 ` [Devel] [PATCH hci-8.2 9/9] dm: default dm-qcow2 devices to no I/O scheduler Denis V. Lunev
2026-11-25 11:19   ` Konstantin Khorenko
2026-12-01  0:39   ` no-reply
2026-11-25 11:19 ` [Devel] [PATCH hci-8.2 0/9] dm-qcow2: lockless fast path, direct completion, " Konstantin Khorenko
2026-11-26 13:58 ` Andrey Zhadchenko

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=20261125012525.681662-5-den@openvz.org \
    --to=den@openvz.org \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=andrey.zhadchenko@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=vasileios.almpanis@virtuozzo.com \
    /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.