[Devel] [PATCH hci-8.1 v3 1/5] dm-qcow2: fix a failed refcount table relocation leaking md pages
Denis V. Lunev
den at openvz.org
Wed Nov 4 21:01:01 MSK 2026
From: Denis V. Lunev <den at openvz.org>
relocate_refcount_table() inserts md pages for the last cluster of the
new reftable and for the new refblocks, one page at a time from
pos + old_clus * clu_size up to r2_end. On error it frees pages from
end only, one per cluster, so the pages of the new reftable cluster
and all but the first page of each refblock cluster bigger than a page
stay in md_pages.
The header is rolled back, so the next allocation past the old refcount
coverage relocates the table again. Its first insert finds a leaked page
and fails with -EEXIST, the relocation returns -EIO, and every
allocating write beyond the old coverage fails with an I/O error until
the device is suspended and resumed or the table is reloaded. A failed
page allocation or a failed header write is enough to get there.
Free exactly the pages the allocation loop inserted when the relocation
fails before the header write. Once the header write has succeeded, the
error paths skip the freeing and only return -EIO: the header on disk
already points at the new table, and its pages may be dirty and queued
for writeback by then.
Fixes: 222b6f1291c1 ("dm-qcow2: Introduce driver to create block devices over QCOW2 files")
https://virtuozzo.atlassian.net/browse/VSTOR-153051
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 | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
index 1c269a78ad35..ac4dbe0623ea 100644
--- a/drivers/md/dm-qcow2-map.c
+++ b/drivers/md/dm-qcow2-map.c
@@ -2277,6 +2277,7 @@ static void set_reftable_in_raw_hdr(struct page *page0, loff_t pos, loff_t clus)
static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
{
loff_t i, old_pos, old_end, pos, end, r2_end, delta;
+ loff_t new_md_start, new_md_end;
u32 old_clus, clus, clu_size = qcow2->clu_size;
u32 r2_clus, bits = qcow2->refblock_bits;
unsigned long nr_pages, index;
@@ -2340,8 +2341,9 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
return ret;
/* Alloc R1/R2 pages covering clusters of new R1 and new R2 */
- for (i = pos + (u64)old_clus * clu_size; i < r2_end; i += PAGE_SIZE) {
- ret = qcow2_alloc_and_insert_md_page(qcow2, i >> PAGE_SHIFT, &md);
+ new_md_start = pos + (u64)old_clus * clu_size;
+ for (new_md_end = new_md_start; new_md_end < r2_end; new_md_end += PAGE_SIZE) {
+ ret = qcow2_alloc_and_insert_md_page(qcow2, new_md_end >> PAGE_SHIFT, &md);
if (ret < 0)
goto err_free_r2_pages;
spin_lock_irq(&qcow2->md_pages_lock);
@@ -2377,21 +2379,21 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
if (!WARN_ON_ONCE(!md))
qcow2_md_make_dirty(qcow2, md, true);
if (!md)
- break; /* goto err_free_r2_pages */
+ break; /* goto err_eio */
}
spin_unlock_irq(&qcow2->md_pages_lock);
if (i != nr_pages)
- goto err_free_r2_pages;
+ goto err_eio;
/* Connect new R2 to new R1 */
for (i = end; i < r2_end; i += clu_size) {
if (calc_refcounters_map(qcow2, i, &r1, &r2) < 0) {
WARN_ON_ONCE(1);
- goto err_free_r2_pages;
+ goto err_eio;
}
ret = handle_md_page(qcow2, r1.page_id, NULL, &md);
if (WARN_ON_ONCE(ret <= 0))
- goto err_free_r2_pages;
+ goto err_eio;
spin_lock_irq(&qcow2->md_pages_lock);
set_u64_to_be_page(md->page, r1.index_in_page, i);
qcow2_md_make_dirty(qcow2, md, true);
@@ -2402,7 +2404,7 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
for (i = pos; i < r2_end; i += clu_size) {
ret = qcow2_handle_r1r2_maps(qcow2, i, NULL, &r1, &r2, false);
if (WARN_ON_ONCE(ret <= 0))
- goto err_free_r2_pages;
+ goto err_eio;
mark_cluster_used(qcow2, r2.md, r2.index_in_page);
}
@@ -2410,14 +2412,14 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
for (i = old_pos; i < old_end; i += clu_size) {
ret = qcow2_handle_r1r2_maps(qcow2, i, NULL, &r1, &r2, false);
if (WARN_ON_ONCE(ret <= 0))
- goto err_free_r2_pages;
+ goto err_eio;
mark_cluster_unused(qcow2, r2.md, r2.index_in_page, i);
}
return 1;
err_free_r2_pages:
- for (i = end; i < r2_end; i += clu_size) {
+ for (i = new_md_start; i < new_md_end; i += PAGE_SIZE) {
ret = handle_md_page(qcow2, i >> PAGE_SHIFT, NULL, &md);
if (ret <= 0)
break;
@@ -2426,6 +2428,7 @@ static int relocate_refcount_table(struct qcow2 *qcow2, struct qio **qio)
spin_unlock_irq(&qcow2->md_pages_lock);
qcow2_free_md_page(md);
}
+err_eio:
/* TODO: switch to RO */
return -EIO;
}
--
2.53.0
More information about the Devel
mailing list