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 3/9] dm-qcow2: map fast path reads without md_pages_lock
Date: Wed, 25 Nov 2026 02:25:19 +0100 [thread overview]
Message-ID: <20261125012525.681662-4-den@openvz.org> (raw)
In-Reply-To: <20261125012525.681662-1-den@openvz.org>
From: Denis V. Lunev <den@openvz.org>
Every fast path read takes md_pages_lock in parse_metadata() to look
up the L1 and L2 pages and read their entries. The lock is global for
the device, so reads from all submitting CPUs serialize on it, and
adding submitters makes each read more expensive instead of adding
throughput.
Map the common read without the lock: both pages cached and clean, no
index of them locked, and the cluster mapped exactly once, not
compressed and not all-zeroes. Anything else falls back to the locked
parse. The pages are found with rb_find_rcu(), which the RCU freeing
of md pages makes safe. L1 and L2 pages stay cached once they are
uptodate, so the pointers stay valid after rcu_read_unlock() as they
do after the lock.
An L1 or L2 entry changes only while its page is dirty, or under
writeback when a failed writeback is reverted. A qio on a locked
index waits for the unlock, so a page with a locked index is refused
as well. qcow2_md_page_change() publishes md->gen with release
semantics after MD_DIRTY or the lock bit is set and before an entry
changes, in qcow2_md_make_dirty(), md_index_set_locked() and
revert_l_entries_update(). The reader samples md->gen with
smp_load_acquire() before it checks the page. After reading the entries,
it uses smp_rmb() followed by READ_ONCE() to order the entry reads before
the second sample:
- a reader that sees the new md->gen sees the page dirty, locked or
under writeback, or a later status;
- a reader that sees a new entry, or one word of an extended entry,
sees the new md->gen.
MD_UPTODATE and the clean status after writeback are stored with
release, so a reader that sees them sees the page contents and the
reverted entries. md->status, md->wbd, md->lockd and the entries are
written with WRITE_ONCE() and read with READ_ONCE() or acquire.
As with the locked parse, nothing pins the cluster once the read is
mapped, so a discard racing with the read may let it return data of a
reused cluster. That race is VSTOR-154939, accepted as Won't Fix.
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 | 116 ++++++++++++++++++++++++++++++-----
drivers/md/dm-qcow2-target.c | 23 ++++++-
drivers/md/dm-qcow2.h | 25 ++++++++
3 files changed, 147 insertions(+), 17 deletions(-)
diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 1aabadfae763..b22cb5c90918 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -232,7 +232,7 @@ static u64 get_u64_from_page(struct page *page, int index_in_page)
u64 *indexes, val;
indexes = kmap_atomic(page);
- val = indexes[index_in_page];
+ val = READ_ONCE(indexes[index_in_page]);
kunmap_atomic(indexes);
return val;
@@ -248,7 +248,8 @@ static void set_u64_to_page(struct page *page, int index_in_page, u64 val)
u64 *indexes;
indexes = kmap_atomic(page);
- indexes[index_in_page] = val;
+ /* L1/L2 entries are read without md_pages_lock */
+ WRITE_ONCE(indexes[index_in_page], val);
kunmap_atomic(indexes);
}
@@ -1042,7 +1043,7 @@ static bool qcow2_md_make_dirty(struct qcow2 *qcow2, struct md_page *md, bool is
lockdep_assert_held(&qcow2->md_pages_lock);
if (!(md->status & MD_DIRTY)) {
- md->status |= MD_DIRTY;
+ WRITE_ONCE(md->status, md->status | MD_DIRTY);
list_add_tail(&md->wb_link, head);
new = true;
@@ -1052,6 +1053,7 @@ static bool qcow2_md_make_dirty(struct qcow2 *qcow2, struct md_page *md, bool is
/* Sanity: 1)only L1L2 have wbd, 2)only R1R2 allow redirtying */
WARN_ON(md->wbd && ((md->status & MD_WRITEBACK) || is_refs));
}
+ qcow2_md_page_change(qcow2, md);
return new;
}
@@ -1314,6 +1316,7 @@ static void md_index_set_locked(struct qcow2 *qcow2, struct md_page *md,
WARN_ON_ONCE(test_bit(index_in_page, md->lockd->indexes));
set_bit(index_in_page, md->lockd->indexes);
md->lockd->nr++;
+ qcow2_md_page_change(qcow2, md);
}
static bool delay_if_locked(struct qcow2 *qcow2, struct md_page *md,
@@ -1428,7 +1431,7 @@ static void do_md_page_read_complete(int ret, struct qcow2 *qcow2,
if (ret < 0)
qcow2_md_page_erase(qcow2, md);
else
- md->status |= MD_UPTODATE;
+ qcow2_md_set_uptodate(qcow2, md);
list_splice_tail_init(&md->wait_list, &wait_list);
spin_unlock_irqrestore(&qcow2->md_pages_lock, flags);
@@ -1449,6 +1452,7 @@ static void revert_l_entries_update(struct qcow2 *qcow2, struct wb_desc *wbd)
int i;
lockdep_assert_held(&qcow2->md_pages_lock);
+ qcow2_md_page_change(qcow2, wbd->md);
for_each_set_bit(i, wbd->changed_indexes, LX_INDEXES_PER_PAGE) {
/* Here we restore prealloced and compressed clu mappings */
old = pe_page ? get_u64_from_be_page(pe_page, i) : 0;
@@ -1462,14 +1466,16 @@ static void clear_writeback_status(struct qcow2 *qcow2, struct md_page *md,
{
lockdep_assert_held(&qcow2->md_pages_lock);
- md->status &= ~(MD_WRITEBACK|MD_WRITEBACK_ERROR);
+ /* Reverted entries before a clean status, see md_page_stable() */
+ smp_store_release(&md->status,
+ md->status & ~(MD_WRITEBACK|MD_WRITEBACK_ERROR));
list_splice_init(&md->wait_list, wait_list);
if (ret && !md->wbd) {
/*
* L1L2 updates can do safe revert,
* so here we care about R1R2 only.
*/
- md->status |= MD_WRITEBACK_ERROR;
+ WRITE_ONCE(md->status, md->status | MD_WRITEBACK_ERROR);
if (ret == -ENOSPC && !READ_ONCE(qcow2->md_wb_no_retry))
qcow2_md_make_dirty(qcow2, md, true);
}
@@ -1478,7 +1484,7 @@ static void clear_writeback_status(struct qcow2 *qcow2, struct md_page *md,
list_splice_init(&md->wbd->dependent_list, wait_list);
else
list_splice_init(&md->wbd->dependent_list, end_list);
- md->wbd = NULL;
+ WRITE_ONCE(md->wbd, NULL);
}
}
@@ -2110,6 +2116,80 @@ static int qcow2_handle_r1r2_maps(struct qcow2 *qcow2, loff_t pos, struct qio **
*/
#define QIO_NEEDS_MAIN_KWORK 2
+static bool md_page_stable(struct md_page *md, unsigned int *gen)
+{
+ unsigned int status;
+
+ /* Pairs with qcow2_md_page_change() */
+ *gen = smp_load_acquire(&md->gen);
+ /* Pairs with qcow2_md_set_uptodate(), clear_writeback_status() */
+ status = smp_load_acquire(&md->status);
+ return (status & (MD_UPTODATE | MD_DIRTY | MD_WRITEBACK |
+ MD_WRITEBACK_ERROR)) == MD_UPTODATE &&
+ !READ_ONCE(md->wbd) && !READ_ONCE(md->lockd);
+}
+
+/*
+ * 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.
+ */
+static bool parse_metadata_lockless(struct qcow2 *qcow2, struct qio *qio,
+ struct qcow2_map *map, u64 *ret_pos)
+{
+ unsigned int l1_gen, l2_gen;
+ struct md_page *l1, *l2;
+ u64 entry, pos, ext_l2 = 0;
+ bool ret = false;
+
+ rcu_read_lock();
+ l1 = qcow2_md_page_find_rcu(qcow2, map->l1.page_id);
+ if (!l1 || !md_page_stable(l1, &l1_gen))
+ goto out;
+ entry = get_u64_from_be_page(l1->page, map->l1.index_in_page);
+ pos = entry & ~LX_REFCOUNT_EXACTLY_ONE;
+ if (!(entry & LX_REFCOUNT_EXACTLY_ONE) || !pos ||
+ (entry & L1_RESERVED_ZERO_MASK) || CLU_OFF(qcow2, pos))
+ goto out;
+
+ pos += map->l2.index * sizeof(u64);
+ calc_page_id_and_index(pos, &map->l2.page_id, &map->l2.index_in_page);
+ l2 = qcow2_md_page_find_rcu(qcow2, map->l2.page_id);
+ if (!l2 || !md_page_stable(l2, &l2_gen))
+ goto out;
+ entry = get_u64_from_be_page(l2->page, map->l2.index_in_page);
+ pos = entry & ~LX_REFCOUNT_EXACTLY_ONE;
+ if (!(entry & LX_REFCOUNT_EXACTLY_ONE) || !pos ||
+ (pos & (L2_COMPRESSED_CLUSTER | L2_READS_ALL_ZEROES |
+ L2_RESERVED_ZERO_MASK)) || CLU_OFF(qcow2, pos))
+ goto out;
+ if (qcow2->ext_l2) {
+ ext_l2 = get_u64_from_be_page(l2->page,
+ map->l2.index_in_page + 1);
+ if (ext_l2 & (ext_l2 >> 32))
+ goto out;
+ }
+
+ /* Pairs with smp_wmb() in qcow2_md_page_change() */
+ smp_rmb();
+ if (READ_ONCE(l1->gen) != l1_gen || READ_ONCE(l2->gen) != l2_gen)
+ goto out;
+
+ map->l1.md = l1;
+ map->l2.md = l2;
+ map->level = L1_LEVEL | L2_LEVEL;
+ map->ext_l2 = ext_l2;
+ if (qcow2->ext_l2 && !fake_service_qio(qio))
+ map->subclus_mask = qio_subclus_mask(qcow2, qio);
+ map->data_clu_alloced = true;
+ *ret_pos = pos;
+ ret = true;
+out:
+ rcu_read_unlock();
+ return ret;
+}
+
/*
* This caches pages of allocated on disk md levels, which are
* required for submission @qio, and checks they are stable.
@@ -2135,6 +2215,11 @@ 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 &&
+ parse_metadata_lockless(qcow2, *qio, map, &pos)) {
+ map->data_clu_pos = pos;
+ return 0;
+ }
spin_lock_irq(&qcow2->md_pages_lock);
again:
/* Check L1 page */
@@ -2230,7 +2315,7 @@ static int place_r2(struct qcow2 *qcow2, struct qcow2_map_item *r1,
spin_lock_irq(&qcow2->md_pages_lock);
set_u64_to_be_page(r1->md->page, r1->index_in_page, r2_pos);
qcow2_md_make_dirty(qcow2, r1->md, true);
- r2->md->status |= MD_UPTODATE;
+ qcow2_md_set_uptodate(qcow2, r2->md);
spin_unlock_irq(&qcow2->md_pages_lock);
mark_cluster_used(qcow2, r2->md, r2->index_in_page);
@@ -2529,7 +2614,7 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
goto err_free_r2_pages;
spin_lock_irq(&qcow2->md_pages_lock);
qcow2_zero_fill_page_from(md->page, 0);
- md->status |= MD_UPTODATE;
+ qcow2_md_set_uptodate(qcow2, md);
spin_unlock_irq(&qcow2->md_pages_lock);
}
@@ -2752,7 +2837,7 @@ static int prepare_l_entry_update(struct qcow2 *qcow2, struct qio *qio,
spin_lock_irq(&qcow2->md_pages_lock);
if (qcow2_md_make_dirty(qcow2, md, false))
- md->wbd = new_wbd;
+ WRITE_ONCE(md->wbd, new_wbd);
else
WARN_ON_ONCE(new_wbd);
first = !test_and_set_bit(index_in_page, md->wbd->changed_indexes);
@@ -2875,7 +2960,7 @@ static int prepare_l_entry_replace(struct qcow2 *qcow2, struct qcow2_map *map,
if (!lockd)
return -ENOMEM;
spin_lock_irq(&qcow2->md_pages_lock);
- md->lockd = lockd;
+ WRITE_ONCE(md->lockd, lockd);
}
md_index_set_locked(qcow2, md, index_in_page);
@@ -4123,8 +4208,7 @@ static void submit_metadata_writeback(struct qcow2 *qcow2)
/* L1L2 mustn't be redirtyed, when wb in-flight! */
WARN_ON_ONCE(!(md->status & MD_DIRTY) ||
(md->wbd && (md->status & MD_WRITEBACK)));
- md->status |= MD_WRITEBACK;
- md->status &= ~MD_DIRTY;
+ WRITE_ONCE(md->status, (md->status & ~MD_DIRTY) | MD_WRITEBACK);
spin_unlock_irq(&qcow2->md_pages_lock);
submit_rw_md_page(WRITE, qcow2, md);
@@ -4843,8 +4927,10 @@ static void handle_cleanup_mask(struct qio *qio)
spin_lock_irqsave(&qcow2->md_pages_lock, flags);
clear_bit(ext->lx_index_in_page, md->lockd->indexes);
WARN_ON_ONCE(--md->lockd->nr < 0);
- if (!md->lockd->nr)
- swap(md->lockd, lockd);
+ if (!md->lockd->nr) {
+ lockd = md->lockd;
+ WRITE_ONCE(md->lockd, NULL);
+ }
list_splice_init(&md->wait_list, &qio_list);
spin_unlock_irqrestore(&qcow2->md_pages_lock, flags);
qcow2_dispatch_qios(qcow2, NULL, &qio_list);
diff --git a/drivers/md/dm-qcow2-target.c b/drivers/md/dm-qcow2-target.c
index 478e8cdf6d90..af8a6d286830 100644
--- a/drivers/md/dm-qcow2-target.c
+++ b/drivers/md/dm-qcow2-target.c
@@ -313,6 +313,24 @@ static struct md_page *qcow2_md_page_find(struct qcow2 *qcow2, u64 id)
}
ALLOW_ERROR_INJECTION(qcow2_md_page_find, NULL);
+static int md_page_cmp(const void *key, const struct rb_node *node)
+{
+ u64 id = *(const u64 *)key;
+ u64 md_id = READ_ONCE(rb_entry(node, struct md_page, node)->id);
+
+ if (id < md_id)
+ return -1;
+ return id > md_id;
+}
+
+/* Under rcu_read_lock(), may miss a page while the tree is changing */
+struct md_page *qcow2_md_page_find_rcu(struct qcow2 *qcow2, u64 id)
+{
+ struct rb_node *node = rb_find_rcu(&id, &qcow2->md_pages, md_page_cmp);
+
+ return node ? rb_entry(node, struct md_page, node) : NULL;
+}
+
/*
* This returns md if it's found and up to date, or NULL.
* @qio is zeroed if it's postponed.
@@ -380,7 +398,7 @@ struct md_page *qcow2_md_page_renumber(struct qcow2 *qcow2, u64 id, u64 new_id)
if (md) {
WARN_ON_ONCE(!list_empty(&md->wait_list));
qcow2_md_page_erase(qcow2, md);
- md->id = new_id;
+ WRITE_ONCE(md->id, new_id);
if (WARN_ON(md_page_try_insert(qcow2, md) < 0))
md = NULL;
}
@@ -409,6 +427,7 @@ int qcow2_alloc_and_insert_md_page(struct qcow2 *qcow2, u64 index, struct md_pag
(*md)->id = index;
(*md)->status = 0;
+ (*md)->gen = 0;
(*md)->wbd = NULL;
(*md)->lockd = NULL;
atomic_set(&(*md)->wpc_readers, 0);
@@ -470,7 +489,7 @@ int qcow2_md_page_read_sync(struct qcow2 *qcow2, u64 id, struct md_page **md)
}
spin_lock_irq(&qcow2->md_pages_lock);
- (*md)->status |= MD_UPTODATE;
+ qcow2_md_set_uptodate(qcow2, *md);
spin_unlock_irq(&qcow2->md_pages_lock);
return 0;
err:
diff --git a/drivers/md/dm-qcow2.h b/drivers/md/dm-qcow2.h
index 2107499ea947..accac35fa345 100644
--- a/drivers/md/dm-qcow2.h
+++ b/drivers/md/dm-qcow2.h
@@ -137,6 +137,7 @@ struct md_page {
#define MD_WRITEBACK (1U << 2) /* Writeback was submitted */
#define MD_WRITEBACK_ERROR (1U << 3) /* Last writeback failed with error */
unsigned int status;
+ unsigned int gen; /* See qcow2_md_page_change() */
u8 writer_gen;
u16 discard_waiters;
struct page *page;
@@ -418,6 +419,7 @@ int qcow2_alloc_and_insert_md_page(struct qcow2 *qcow2, u64 index,
struct md_page **md);
int qcow2_md_page_read_sync(struct qcow2 *qcow2, u64 id,
struct md_page **md);
+struct md_page *qcow2_md_page_find_rcu(struct qcow2 *qcow2, u64 id);
struct md_page *qcow2_md_page_find_or_postpone(struct qcow2 *qcow2,
u64 id, struct qio **qio);
struct md_page *qcow2_md_page_renumber(struct qcow2 *qcow2, u64 id,
@@ -447,6 +449,29 @@ loff_t qcow2_calc_used_end(struct qcow2 *qcow2);
void qcow2_merge_backward_work(struct work_struct *work);
void qcow2_merge_backward_cancel(struct qcow2_target *tgt);
+/*
+ * Called with MD_DIRTY, MD_WRITEBACK (on revert), or a lock bit set,
+ * before the L1/L2 entries change, so a reader that sees the new gen sees
+ * the flag or a later status, and a reader that sees a new entry sees the
+ * new gen.
+ * See md_page_stable().
+ */
+static inline void qcow2_md_page_change(struct qcow2 *qcow2,
+ struct md_page *md)
+{
+ lockdep_assert_held(&qcow2->md_pages_lock);
+ smp_store_release(&md->gen, md->gen + 1);
+ smp_wmb();
+}
+
+/* A reader that sees MD_UPTODATE sees the page contents */
+static inline void qcow2_md_set_uptodate(struct qcow2 *qcow2,
+ struct md_page *md)
+{
+ lockdep_assert_held(&qcow2->md_pages_lock);
+ smp_store_release(&md->status, md->status | MD_UPTODATE);
+}
+
static inline struct qcow2_target *to_qcow2_target(struct dm_target *ti)
{
return ti->private;
--
2.55.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
next prev 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 ` Denis V. Lunev [this message]
2026-11-25 11:19 ` [Devel] [PATCH hci-8.2 3/9] dm-qcow2: map fast path reads without md_pages_lock Konstantin Khorenko
2026-12-01 0:17 ` no-reply
2026-11-25 1:25 ` [Devel] [PATCH hci-8.2 4/9] dm-qcow2: map fast path writes " Denis V. Lunev
2026-11-25 11:19 ` 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-4-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.