[Devel] [PATCH hci-8.2 4/9] dm-qcow2: map fast path writes without md_pages_lock
Denis V. Lunev
den at openvz.org
Wed Nov 25 04:25:20 MSK 2026
From: Denis V. Lunev <den at 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 at 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
More information about the Devel
mailing list