* [Devel] [PATCH hci-8.2 v8 1/9] dm-ploop: reject a delta header whose BAT does not cover the target
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:14 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 2/9] dm-ploop: reject empty deltas Alexander Ivanov
` (9 subsequent siblings)
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
ploop_setup_metadata() takes the number of BAT entries from m_Size of
the top delta header, but checks only m_SizeInSectors_v2 against the
target length. A header with a small m_Size (including zero) and a large
enough m_SizeInSectors_v2 passes all checks. Requests past the last BAT
entry then hit WARN_ONCE() in ploop_rq_valid(), which panics the host
with panic_on_warn.
Reject the header if the BAT does not cover the target, taking the
skip_off shift of request positions into account.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/md/dm-ploop-bat.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 5ddb8ffdff07..7b12b0f9fe81 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -10,6 +10,7 @@
#include <linux/uio.h>
#include <linux/error-injection.h>
#include <linux/mm.h>
+#include <linux/overflow.h>
#include "dm-ploop.h"
struct md_page *ploop_md_page_find(struct ploop *ploop, u32 id)
@@ -189,6 +190,7 @@ int ploop_setup_metadata(struct ploop *ploop, struct page *page)
struct ploop_pvd_header *m_hdr = NULL;
u32 bat_clusters, offset_clusters;
struct dm_target *ti = ploop->ti;
+ u64 end;
int ret;
m_hdr = kmap(page);
@@ -229,6 +231,11 @@ int ploop_setup_metadata(struct ploop *ploop, struct page *page)
PL_ERR("Too short BAT");
goto out;
}
+ if (check_add_overflow((u64)ti->len, ploop->skip_off, &end) ||
+ SEC_TO_CLU(ploop, end - 1) >= ploop->nr_bat_entries) {
+ PL_ERR("BAT does not cover the target");
+ goto out;
+ }
kunmap(page);
m_hdr = NULL;
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* [Devel] [PATCH hci-8.2 v8 2/9] dm-ploop: reject empty deltas
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 1/9] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:15 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
` (8 subsequent siblings)
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
ploop_delta_check_header() accepts a header with m_Size == 0, and
ploop_add_delta() accepts a raw delta of zero size. The BAT iterators
can't handle nr_be == 0: ploop_bat_clu_to_page_nr(nr_be - 1) wraps to
page 0, so md pages past the first one are iterated whole and the
clu == nr_be - 1 stop condition never matches. A non-raw empty delta
has a single md page, so ploop_apply_delta_mappings() and
notify_delta_merged() (and ploop_check_delta_before_flip() with
PLOOP_DEBUG) walk past it and dereference a NULL d_md on a device with
more than one md page. A raw delta is not handled as raw yet, as
ploop_add_delta() sets is_raw only after applying its mappings; once it
is, an empty raw delta would get all the clusters mapped to it.
Reject empty deltas in ploop_delta_check_header(), which covers every
caller reading a delta header, and in ploop_add_delta() for raw deltas.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/md/dm-ploop-bat.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 7b12b0f9fe81..4fc99400a0a4 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -268,7 +268,7 @@ static int ploop_delta_check_header(struct ploop *ploop,
offset_clusters = SEC_TO_CLU(ploop, le32_to_cpu(d_hdr->m_FirstBlockOffset));
bat_clusters = ploop_nr_bat_clusters(ploop, delta_nr_be);
- if (delta_nr_be > ploop->nr_bat_entries ||
+ if (!delta_nr_be || delta_nr_be > ploop->nr_bat_entries ||
bat_clusters != offset_clusters)
goto out;
@@ -503,6 +503,9 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
goto out;
} else {
nr_be = POS_TO_CLU(ploop, file_size);
+ ret = -EPROTO;
+ if (!nr_be)
+ goto out;
}
ret = -EBADSLT; /* Lower delta can't be bigger then upper */
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 2/9] dm-ploop: reject empty deltas
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 2/9] dm-ploop: reject empty deltas Alexander Ivanov
@ 2026-11-18 13:15 ` no-reply
0 siblings, 0 replies; 26+ messages in thread
From: no-reply @ 2026-11-18 13:15 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
> ploop_delta_check_header() accepts a header with m_Size == 0, and
> ploop_add_delta() accepts a raw delta of zero size. The BAT iterators
> can't handle nr_be == 0: ploop_bat_clu_to_page_nr(nr_be - 1) wraps to
> page 0, so md pages past the first one are iterated whole and the
> clu == nr_be - 1 stop condition never matches. A non-raw empty delta
> has a single md page, so ploop_apply_delta_mappings() and
> notify_delta_merged() (and ploop_check_delta_before_flip() with
> PLOOP_DEBUG) walk past it and dereference a NULL d_md on a device with
> more than one md page. A raw delta is not handled as raw yet, as
> ploop_add_delta() sets is_raw only after applying its mappings; once it
> is, an empty raw delta would get all the clusters mapped to it.
>
> Reject empty deltas in ploop_delta_check_header(), which covers every
> caller reading a delta header, and in ploop_add_delta() for raw deltas.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-148171
> Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
> Feature: dm-ploop: ploop target driver
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118131032.3372996-1-alexander.ivanov@virtuozzo.com?part=2
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 1/9] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 2/9] dm-ploop: reject empty deltas Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:12 ` Denis V. Lunev
2026-11-18 13:16 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 4/9] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
` (7 subsequent siblings)
10 siblings, 2 replies; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
ploop_apply_delta_mappings() takes is_raw from deltas[level], but
ploop_add_delta() sets deltas[level].is_raw only after the mappings
are applied. So a raw delta is handled as a non-raw one: its md_root
is empty, ploop_md_first_entry() returns NULL and d_md->kmpage
dereferences it.
This has been the case since the driver was added, i.e. raw deltas
have never worked here; this is not a regression.
Set is_raw before applying the mappings.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Denis V. Lunev <den@openvz.org>
---
drivers/md/dm-ploop-bat.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 4fc99400a0a4..3427d1a90d81 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -512,6 +512,7 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
if (level != ploop_top_level(ploop) && nr_be > deltas[level + 1].nr_be)
goto out;
+ deltas[level].is_raw = is_raw;
ploop_apply_delta_mappings(ploop, deltas, level, &md_root, nr_be);
deltas[level].file = file;
@@ -532,7 +533,6 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
deltas[level].file_size = file_size;
deltas[level].file_preallocated_area_start = file_size;
deltas[level].nr_be = nr_be;
- deltas[level].is_raw = is_raw;
ret = 0;
out:
ploop_free_md_pages_tree(&md_root);
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
@ 2026-11-18 13:12 ` Denis V. Lunev
2026-11-18 13:18 ` Alexander Ivanov
2026-11-18 13:16 ` no-reply
1 sibling, 1 reply; 26+ messages in thread
From: Denis V. Lunev @ 2026-11-18 13:12 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
On 11/18/26 14:10, Alexander Ivanov wrote:
> ploop_apply_delta_mappings() takes is_raw from deltas[level], but
> ploop_add_delta() sets deltas[level].is_raw only after the mappings
> are applied. So a raw delta is handled as a non-raw one: its md_root
> is empty, ploop_md_first_entry() returns NULL and d_md->kmpage
> dereferences it.
>
> This has been the case since the driver was added, i.e. raw deltas
> have never worked here; this is not a regression.
>
> Set is_raw before applying the mappings.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-148171
> Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
> Feature: dm-ploop: ploop target driver
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> Reviewed-by: Denis V. Lunev <den@openvz.org>
> ---
> drivers/md/dm-ploop-bat.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
> index 4fc99400a0a4..3427d1a90d81 100644
> --- a/drivers/md/dm-ploop-bat.c
> +++ b/drivers/md/dm-ploop-bat.c
> @@ -512,6 +512,7 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
> if (level != ploop_top_level(ploop) && nr_be > deltas[level + 1].nr_be)
> goto out;
>
> + deltas[level].is_raw = is_raw;
> ploop_apply_delta_mappings(ploop, deltas, level, &md_root, nr_be);
>
> deltas[level].file = file;
> @@ -532,7 +533,6 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
> deltas[level].file_size = file_size;
> deltas[level].file_preallocated_area_start = file_size;
> deltas[level].nr_be = nr_be;
> - deltas[level].is_raw = is_raw;
> ret = 0;
> out:
> ploop_free_md_pages_tree(&md_root);
AFAIR I have seen task to drop raw support in kernel. Why touching this?
Den
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings
2026-11-18 13:12 ` Denis V. Lunev
@ 2026-11-18 13:18 ` Alexander Ivanov
0 siblings, 0 replies; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:18 UTC (permalink / raw)
To: Denis V. Lunev, andrey.zhadchenko; +Cc: devel
>> diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
>> index 4fc99400a0a4..3427d1a90d81 100644
>> --- a/drivers/md/dm-ploop-bat.c
>> +++ b/drivers/md/dm-ploop-bat.c
>> @@ -512,6 +512,7 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
>> if (level != ploop_top_level(ploop) && nr_be > deltas[level + 1].nr_be)
>> goto out;
>>
>> + deltas[level].is_raw = is_raw;
>> ploop_apply_delta_mappings(ploop, deltas, level, &md_root, nr_be);
>>
>> deltas[level].file = file;
>> @@ -532,7 +533,6 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
>> deltas[level].file_size = file_size;
>> deltas[level].file_preallocated_area_start = file_size;
>> deltas[level].nr_be = nr_be;
>> - deltas[level].is_raw = is_raw;
>> ret = 0;
>> out:
>> ploop_free_md_pages_tree(&md_root);
> AFAIR I have seen task to drop raw support in kernel. Why touching this?
>
> Den
Until raw support is actually dropped, "raw@" is still accepted by
ploop_ctr(), and with it the driver dereferences NULL in
ploop_apply_delta_mappings() on table load. The fix is one moved line,
so it is cheap to carry and can go away together with raw support.
There is also a dependency: the next patch factors out ploop_read_delta(),
which takes is_raw from deltas[level] because on resume it re-reads
already added deltas and has no is_raw argument. So ploop_add_delta()
has to set is_raw before the read anyway. Without this patch the refactor
would fix the crash silently while claiming "no functional changes".
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
2026-11-18 13:12 ` Denis V. Lunev
@ 2026-11-18 13:16 ` no-reply
1 sibling, 0 replies; 26+ messages in thread
From: no-reply @ 2026-11-18 13:16 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
> ploop_apply_delta_mappings() takes is_raw from deltas[level], but
> ploop_add_delta() sets deltas[level].is_raw only after the mappings
> are applied. So a raw delta is handled as a non-raw one: its md_root
> is empty, ploop_md_first_entry() returns NULL and d_md->kmpage
> dereferences it.
>
> This has been the case since the driver was added, i.e. raw deltas
> have never worked here; this is not a regression.
>
> Set is_raw before applying the mappings.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-148171
> Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
> Feature: dm-ploop: ploop target driver
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118131032.3372996-1-alexander.ivanov@virtuozzo.com?part=3
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [Devel] [PATCH hci-8.2 v8 4/9] dm-ploop: factor out ploop_read_delta() from ploop_add_delta()
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (2 preceding siblings ...)
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 3/9] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:16 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 5/9] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
` (6 subsequent siblings)
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
Move reading of delta BAT and applying its mappings to
ploop->bat_entries into a separate function ploop_read_delta(). It
will be used to reload metadata of already added deltas.
ploop_read_delta() takes is_raw from deltas[level], so ploop_add_delta()
sets it before the call.
Replace the stale comment of ploop_add_delta(): it claimed deltas are
added from oldest to newest, while ploop_add_deltas_stack() adds them
from top to base, which ploop_read_delta() relies on.
No functional changes.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
Reviewed-by: Denis V. Lunev <den@openvz.org>
---
drivers/md/dm-ploop-bat.c | 43 +++++++++++++++++++++++++++++++--------
1 file changed, 35 insertions(+), 8 deletions(-)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 3427d1a90d81..5a89771a2e02 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -481,23 +481,27 @@ int ploop_check_delta_length(struct ploop *ploop, struct file *file, loff_t *fil
ALLOW_ERROR_INJECTION(ploop_check_delta_length, ERRNO);
/*
- * @fd refers to a new delta, which is placed right before top_delta.
- * So, userspace has to populate deltas stack from oldest to newest.
+ * Read BAT of delta @level and apply its mappings to ploop->bat_entries.
+ * deltas[@level].is_raw and nr_be of upper deltas must be already set.
+ *
+ * Deltas must be read from top to base: mappings of upper deltas take
+ * precedence (already populated BAT entries are not overwritten), and
+ * size of the delta is checked against nr_be of the upper one.
*/
-int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_raw)
+static int ploop_read_delta(struct ploop *ploop, u32 level, struct file *file,
+ loff_t *file_size_ret, u32 *nr_be_ret)
{
struct ploop_delta *deltas = ploop->deltas;
struct rb_root md_root = RB_ROOT;
- struct file *cloned_file = NULL;
loff_t file_size;
u32 nr_be;
- int ret, i;
+ int ret;
ret = ploop_check_delta_length(ploop, file, &file_size);
if (ret)
goto out;
- if (!is_raw) {
+ if (!deltas[level].is_raw) {
ret = ploop_read_delta_metadata(ploop, file, &md_root, &nr_be);
if (ret)
goto out;
@@ -512,9 +516,33 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
if (level != ploop_top_level(ploop) && nr_be > deltas[level + 1].nr_be)
goto out;
- deltas[level].is_raw = is_raw;
ploop_apply_delta_mappings(ploop, deltas, level, &md_root, nr_be);
+ *file_size_ret = file_size;
+ *nr_be_ret = nr_be;
+ ret = 0;
+out:
+ ploop_free_md_pages_tree(&md_root);
+ return ret;
+}
+
+/*
+ * Add @file as delta @level. Deltas must be added from top to base,
+ * see ploop_read_delta().
+ */
+int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_raw)
+{
+ struct ploop_delta *deltas = ploop->deltas;
+ struct file *cloned_file = NULL;
+ loff_t file_size;
+ u32 nr_be;
+ int ret, i;
+
+ deltas[level].is_raw = is_raw;
+ ret = ploop_read_delta(ploop, level, file, &file_size, &nr_be);
+ if (ret)
+ goto out;
+
deltas[level].file = file;
deltas[level].mtfile = kcalloc(ploop->nkt_runners, sizeof(*file),
GFP_KERNEL);
@@ -535,7 +563,6 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
deltas[level].nr_be = nr_be;
ret = 0;
out:
- ploop_free_md_pages_tree(&md_root);
return ret;
}
ALLOW_ERROR_INJECTION(ploop_add_delta, ERRNO);
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* [Devel] [PATCH hci-8.2 v8 5/9] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole()
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (3 preceding siblings ...)
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 4/9] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:19 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
` (5 subsequent siblings)
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
ploop_llseek_hole() walks the md pages tree without any lock, while
resize inserts new md pages into it in ploop_add_md_pages(). A lookup
racing with rb-tree rebalancing may follow a stale pointer or miss the
page and return -EINVAL.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Fixes: 03d5aeeaa9ea ("dm/dm-ploop: add llseek_hole")
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/md/dm-ploop-map.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 58d1c7092602..1df31bbc5410 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -2773,7 +2773,11 @@ loff_t ploop_llseek_hole(struct dm_target *ti, loff_t offset, int whence)
while (clu < READ_ONCE(ploop->nr_bat_entries)) {
if (id != ploop_bat_clu_to_page_nr(clu)) {
id = ploop_bat_clu_to_page_nr(clu);
+
+ spin_lock_irq(&ploop->bat_lock);
md = ploop_md_page_find(ploop, id);
+ spin_unlock_irq(&ploop->bat_lock);
+
if (!md)
return -EINVAL;
}
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* [Devel] [PATCH hci-8.2 v8 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (4 preceding siblings ...)
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 5/9] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:24 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 7/9] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
` (4 subsequent siblings)
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
The last piwb reference is often dropped from the BAT write completion
in aio context. ploop_bat_write_finish() completes the data pios there,
and once the last original request is done, suspend can finish and a
table reload or remove can free the ploop in ploop_destroy(). The
completing CPU still decrements inflight_md, calls wake_up() on
inflight_md_wq and frees the piwb into ploop->pio_pool, so it can lock
a freed waitqueue or free into a destroyed mempool.
Nothing on the suspend or destroy path waits for inflight_md, and the
one waiter that exists, ploop_suspend_submitting_pios(), checks the
counter without a lock. It can see zero and return before wake_up() and
ploop_free_piwb() are done with the ploop.
Free the piwb first, then decrement inflight_md and wake the waiters
under inflight_md_wq.lock as the last access to the ploop. In
ploop_destroy(), wait for inflight_md to drop to zero and then take the
same lock before freeing anything, so the waker is done with the ploop.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Fixes: 9dff9fe86d29 ("dm-ploop: rework bat completion logic")
Fixes: d1a1e5108aeb ("dm-ploop: add inflight metadata writeback counter")
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/md/dm-ploop-map.c | 21 +++++++++++++++++----
drivers/md/dm-ploop-target.c | 7 +++++++
2 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 1df31bbc5410..2f85383c4682 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -924,9 +924,24 @@ static void ploop_free_piwb(struct ploop_index_wb *piwb)
static void ploop_bat_write_finish(struct pio *pio, void *piwb_ptr,
blk_status_t bi_status);
+/*
+ * ploop_destroy() takes inflight_md_wq.lock before freeing the ploop, so it
+ * stays valid while we hold the lock. This must be the last access to it.
+ */
+static void ploop_dec_inflight_md(struct ploop *ploop)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&ploop->inflight_md_wq.lock, flags);
+ if (atomic_dec_and_test(&ploop->inflight_md))
+ wake_up_locked(&ploop->inflight_md_wq);
+ spin_unlock_irqrestore(&ploop->inflight_md_wq.lock, flags);
+}
+
static void ploop_put_piwb(struct ploop_index_wb *piwb)
{
if (atomic_dec_and_test(&piwb->count)) {
+ struct ploop *ploop = piwb->ploop;
ploop_bat_write_finish(piwb->pio, piwb, piwb->bi_status);
@@ -940,9 +955,8 @@ static void ploop_put_piwb(struct ploop_index_wb *piwb)
* zero keeps it as is
*/
- if (atomic_dec_and_test(&piwb->ploop->inflight_md))
- wake_up_interruptible(&piwb->ploop->inflight_md_wq);
ploop_free_piwb(piwb);
+ ploop_dec_inflight_md(ploop);
}
}
@@ -1113,9 +1127,8 @@ void ploop_break_bat_update(struct ploop *ploop, struct md_page *md,
spin_unlock(&md->md_lock);
spin_unlock_irqrestore(&ploop->bat_lock, flags);
- if (atomic_dec_and_test(&ploop->inflight_md))
- wake_up_interruptible(&ploop->inflight_md_wq);
ploop_free_piwb(piwb);
+ ploop_dec_inflight_md(ploop);
}
static void ploop_bat_page_zero_cluster(struct ploop *ploop,
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 799b9d61dcdb..3b5f37a58bb1 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -194,6 +194,13 @@ static void ploop_destroy(struct ploop *ploop)
if (ploop->kt_allocator)
kthread_stop(ploop->kt_allocator->task);
+ /* BAT writes may still be completing from aio context */
+ wait_event(ploop->inflight_md_wq,
+ !atomic_read(&ploop->inflight_md));
+ /* Wait for ploop_dec_inflight_md() to drop the lock */
+ spin_lock_irq(&ploop->inflight_md_wq.lock);
+ spin_unlock_irq(&ploop->inflight_md_wq.lock);
+
WARN_ON(!llist_empty(&ploop->pios[PLOOP_LIST_PREPARE]));
WARN_ON(!llist_empty(&ploop->llresubmit_pios));
WARN_ON(!llist_empty(&ploop->enospc_pios));
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
@ 2026-11-18 13:24 ` no-reply
2026-11-18 13:29 ` Alexander Ivanov
0 siblings, 1 reply; 26+ messages in thread
From: no-reply @ 2026-11-18 13:24 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm-ploop: inflight_md drain in ploop_destroy() runs after
kthread_stop()
--
commit 8139d701139f09d811617b4ccc06b25fb85ca925
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
dm-ploop: fix use-after-free on the last inflight_md decrement
The patch frees the piwb first, then decrements inflight_md and wakes waiters under inflight_md_wq.lock as the last access to the ploop. ploop_destroy() now waits for inflight_md to reach zero and takes the same lock before freeing anything.
> diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
> index 799b9d61dcdb..3b5f37a58bb1 100644
> --- a/drivers/md/dm-ploop-target.c
> +++ b/drivers/md/dm-ploop-target.c
> @@ -194,6 +194,13 @@ static void ploop_destroy(struct ploop *ploop)
> if (ploop->kt_allocator)
> kthread_stop(ploop->kt_allocator->task);
>
> + /* BAT writes may still be completing from aio context */
> + wait_event(ploop->inflight_md_wq,
> + !atomic_read(&ploop->inflight_md));
> + /* Wait for ploop_dec_inflight_md() to drop the lock */
> + spin_lock_irq(&ploop->inflight_md_wq.lock);
> + spin_unlock_irq(&ploop->inflight_md_wq.lock);
> +
[Severity: Medium]
Is this wait too late? It runs after kthread_stop() has already stopped and released the runners, kt_worker and kt_allocator. A BAT write that is still in flight can complete during this wait_event(). ploop_put_piwb() then calls ploop_bat_write_finish(), which reaches ploop_dispatch_pios() or ploop_complete_cow() and ends up in ploop_schedule_work(). That calls wake_up_process(ploop->kt_worker->task) on a task that has already exited, so it can be a use-after-free of the task_struct. Any pios queued this way are never processed, because the threads are gone.
This is reachable when ploop_postsuspend() skipped its own inflight_md wait because the device is in standby (the !ploop_is_standby() check), and dtr then runs with BAT writes still in flight. The patch protects the lifetime of the ploop, the pio_pool and the waitqueue, but not that of the stopped kthreads.
Would it be better to drain inflight_md before the kthread_stop() calls? The worker would still have to be able to process the pios dispatched by those completions.
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118131032.3372996-1-alexander.ivanov@virtuozzo.com?part=6
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread
* Re: [Devel] [PATCH hci-8.2 v8 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement
2026-11-18 13:24 ` no-reply
@ 2026-11-18 13:29 ` Alexander Ivanov
0 siblings, 0 replies; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:29 UTC (permalink / raw)
To: no-reply, andrey.zhadchenko; +Cc: devel
>> diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
>> index 799b9d61dcdb..3b5f37a58bb1 100644
>> --- a/drivers/md/dm-ploop-target.c
>> +++ b/drivers/md/dm-ploop-target.c
>> @@ -194,6 +194,13 @@ static void ploop_destroy(struct ploop *ploop)
>> if (ploop->kt_allocator)
>> kthread_stop(ploop->kt_allocator->task);
>>
>> + /* BAT writes may still be completing from aio context */
>> + wait_event(ploop->inflight_md_wq,
>> + !atomic_read(&ploop->inflight_md));
>> + /* Wait for ploop_dec_inflight_md() to drop the lock */
>> + spin_lock_irq(&ploop->inflight_md_wq.lock);
>> + spin_unlock_irq(&ploop->inflight_md_wq.lock);
>> +
> [Severity: Medium]
> Is this wait too late? It runs after kthread_stop() has already stopped and released the runners, kt_worker and kt_allocator. A BAT write that is still in flight can complete during this wait_event(). ploop_put_piwb() then calls ploop_bat_write_finish(), which reaches ploop_dispatch_pios() or ploop_complete_cow() and ends up in ploop_schedule_work(). That calls wake_up_process(ploop->kt_worker->task) on a task that has already exited, so it can be a use-after-free of the task_struct. Any pios queued this way are never processed, because the threads are gone.
>
> This is reachable when ploop_postsuspend() skipped its own inflight_md wait because the device is in standby (the !ploop_is_standby() check), and dtr then runs with BAT writes still in flight. The patch protects the lifetime of the ploop, the pio_pool and the waitqueue, but not that of the stopped kthreads.
>
> Would it be better to drain inflight_md before the kthread_stop() calls? The worker would still have to be able to process the pios dispatched by those completions.
This race is older than the patch and is not specific to BAT writes
(ploop_complete_cow() has it too), so it is out of scope here.
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [Devel] [PATCH hci-8.2 v8 7/9] dm-ploop: fix flip_upper_deltas racing with resume
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (5 preceding siblings ...)
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:21 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 8/9] dm-ploop: reload metadata on resume Alexander Ivanov
` (3 subsequent siblings)
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
ploop_preresume() checks noresume under ctl_rwsem, drops the lock and
only then clears ->suspended. Messages are not serialized with resume:
target_message() uses the live table without md->suspend_lock, so
"set_noresume 1" followed by "flip_upper_deltas" can pass the check
in ploop_flip_upper_deltas() in that gap. The device then goes live
with noresume set and the top deltas swapped.
Clear ->suspended in the same critical section as the noresume check,
and only if it passes.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/md/dm-ploop-target.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 3b5f37a58bb1..bfadca7f12ae 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -764,19 +764,20 @@ static int ploop_preresume(struct dm_target *ti)
struct ploop *ploop = ti->private;
int ret = 0;
- down_read(&ploop->ctl_rwsem);
+ down_write(&ploop->ctl_rwsem);
if (ploop->noresume)
ret = -EAGAIN;
- up_read(&ploop->ctl_rwsem);
-
- if (ret == 0) {
- /*
- * We are singleton target. There will be
- * no more reasons to break resume.
- */
- ploop_set_suspended(ti, false);
+ /*
+ * We are singleton target. There will be no more reasons to break
+ * resume. Clear it under the same lock as the noresume check, so
+ * that flip_upper_deltas can't sneak in before the device goes live.
+ */
+ if (ret == 0)
+ ploop->suspended = false;
+ up_write(&ploop->ctl_rwsem);
+
+ if (ret == 0)
ploop_set_wants_suspend(ti, false);
- }
return ret;
}
ALLOW_ERROR_INJECTION(ploop_preresume, ERRNO);
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* [Devel] [PATCH hci-8.2 v8 8/9] dm-ploop: reload metadata on resume
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (6 preceding siblings ...)
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 7/9] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:30 ` no-reply
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 9/9] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
` (2 subsequent siblings)
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
On "dmsetup reload" the new ploop reads image metadata in ploop_ctr(),
but IO keeps going through the old ploop until suspend, and userspace
may modify images of a suspended device (merge zeroes BAT entries of
the base delta, snapshot creation trims the top delta). So after resume
the new ploop may work with a stale BAT and file sizes, e.g. allocate
clusters already in use.
Reload all metadata in ploop_preresume(): the top delta header, BAT of
every delta and delta file sizes, and rebuild the BAT tree and
holes_bitmap. On error the old metadata is kept and resume fails. DM
has already swapped the new table in by then, so the device stays
suspended and the next resume retries. Userspace must check the result
and must not resume the previous instance on the same images.
Skip the in-use signature check on reload, it is done in ploop_ctr().
Take the signature from disk: libploop marks the image closed on the
suspended device before umount, and an in-memory IN_USE written over
it makes umount trim the image and lose CBT. After flip_upper_deltas
keep the in-memory signature, as userspace relies on it being written
to the new top delta, otherwise the next load fails with -ENOLCK after
online merge.
Data bios complete before BAT writeback, so wait for inflight_md in
postsuspend. A resize of the suspended device may leave BAT writeback
in flight as well, so wait for it in preresume too. Keep metadata that
is still dirty on resume, as md pages may still be referenced.
Request preallocation in ploop_preresume() after the reload instead of
in ploop_ctr(), so the new ploop doesn't truncate the top delta back to
a stale size before resume. ploop_postsuspend() waits for pending
preallocation since a5836033b8fe ("dm-ploop: wait for the
ploop_allocator() thread before changing deltas[]"). Skip this wait and
the one for BAT writeback in standby, as both may hang on a dead file,
so that dm_replace() of libploop can suspend the device to recover it.
Otherwise keep the waits uninterruptible: an ftruncate() of the old
allocator finished after the next table has grown the top delta would
cut its data, and a late BAT writeback would overwrite the reloaded
BAT. Drop the pending requests on postsuspend, so that after a skipped
wait the old allocator doesn't resize the top delta from its stale size
under a ploop of a reloaded table. Only the operation already in flight
is left unfenced. Skip the reload and prealloc on resume while
preallocation is pending, as the allocator changes the top delta size
under bat_lock only. Skip the reload in standby too, as the images may
be inaccessible.
Track standby per ploop instead of the queue bit. The queue is shared
by all tables, and ploop_ctr() of a new table clears the bit before the
old table is suspended, so the old ploop would wait for its stuck
allocator in postsuspend. The flag is never cleared: once the files of
a ploop failed, it is recovered only by a new table via dm_replace().
Drop the comment claiming that ploop_preallocate_cluster() is the only
place to change file_size.
ploop_llseek_hole() needs no locking against the reload:
dm_llseek_hole() calls it under md->io_barrier and returns -EAGAIN on
a suspended device.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/md/dm-ploop-bat.c | 144 ++++++++++++++++++++++++++++++++++-
drivers/md/dm-ploop-cmd.c | 1 +
drivers/md/dm-ploop-map.c | 6 +-
drivers/md/dm-ploop-target.c | 97 ++++++++++++++++-------
drivers/md/dm-ploop.h | 13 +++-
5 files changed, 228 insertions(+), 33 deletions(-)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 5a89771a2e02..a6894910a5bf 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -185,7 +185,8 @@ static int ploop_setup_holes_bitmap(struct ploop *ploop, u32 bat_clusters)
}
ALLOW_ERROR_INJECTION(ploop_setup_holes_bitmap, ERRNO);
-int ploop_setup_metadata(struct ploop *ploop, struct page *page)
+int ploop_setup_metadata(struct ploop *ploop, struct page *page,
+ bool check_in_use)
{
struct ploop_pvd_header *m_hdr = NULL;
u32 bat_clusters, offset_clusters;
@@ -200,7 +201,8 @@ int ploop_setup_metadata(struct ploop *ploop, struct page *page)
goto out;
ret = -ENOLCK;
- if (m_hdr->m_DiskInUse != cpu_to_le32(SIGNATURE_DISK_IN_USE) &&
+ if (check_in_use &&
+ m_hdr->m_DiskInUse != cpu_to_le32(SIGNATURE_DISK_IN_USE) &&
!ploop_is_ro(ploop) && !ignore_signature_disk_in_use)
goto out;
@@ -567,6 +569,144 @@ int ploop_add_delta(struct ploop *ploop, u32 level, struct file *file, bool is_r
}
ALLOW_ERROR_INJECTION(ploop_add_delta, ERRNO);
+static bool ploop_md_pages_are_clean(struct ploop *ploop)
+{
+ struct rb_node *node;
+ struct md_page *md;
+
+ if (!llist_empty(&ploop->wb_batch_llist) ||
+ atomic_read(&ploop->inflight_md))
+ return false;
+
+ ploop_for_each_md_page(ploop, md, node) {
+ if (test_bit(MD_DIRTY, &md->status) ||
+ test_bit(MD_WRITEBACK, &md->status) ||
+ test_bit(MD_UPDATING, &md->status) ||
+ md->piwb || !llist_empty(&md->wait_llist))
+ return false;
+ }
+
+ return true;
+}
+
+/*
+ * Build in-memory metadata from scratch: re-read header of top delta,
+ * BAT of every delta and sizes of delta files. This is called on resume
+ * of suspended device, since after "dmsetup reload" the metadata of the
+ * new ploop may be outdated: the images could be changed by the old
+ * ploop after the new one was constructed. On error the old metadata
+ * is kept.
+ */
+int ploop_reload_metadata(struct ploop *ploop)
+{
+ struct rb_root old_bat_entries = ploop->bat_entries;
+ void *old_holes_bitmap = ploop->holes_bitmap;
+ u32 old_nr_bat_entries = ploop->nr_bat_entries;
+ u32 old_hb_nr = ploop->hb_nr;
+ u32 old_m_Type = ploop->m_Type;
+ u32 old_m_Sectors = ploop->m_Sectors;
+ struct ploop_pvd_header *hdr, *old_hdr;
+ u8 old_m_Sig[sizeof(ploop->m_Sig)];
+ struct ploop_delta *delta, *old_deltas;
+ struct page *page;
+ loff_t file_size;
+ u32 nr_be;
+ int level, ret;
+
+ /*
+ * Metadata of the device that has already been running is up to
+ * date, so we only need to reload clean metadata. Keep dirty
+ * metadata, as md pages are still in use.
+ */
+ if (!ploop_md_pages_are_clean(ploop))
+ return 0;
+
+ memcpy(old_m_Sig, ploop->m_Sig, sizeof(old_m_Sig));
+
+ old_deltas = kmemdup(ploop->deltas,
+ sizeof(*old_deltas) * ploop->nr_deltas,
+ GFP_KERNEL);
+ page = alloc_page(GFP_KERNEL);
+ ret = -ENOMEM;
+ if (!old_deltas || !page)
+ goto out;
+
+ ret = ploop_rw_page_sync(READ, ploop_top_delta(ploop)->file, 0, page);
+ if (ret)
+ goto out;
+
+ ploop->bat_entries = RB_ROOT;
+ ploop->holes_bitmap = NULL;
+
+ /* Image is already checked for being in use in ploop_ctr() */
+ ret = ploop_setup_metadata(ploop, page, false);
+ if (ret)
+ goto restore;
+
+ ret = ploop_prealloc_md_pages(&ploop->bat_entries, 0,
+ ploop->nr_bat_entries);
+ if (ret)
+ goto restore;
+
+ /*
+ * Same order as in ploop_add_deltas_stack(): from top to base.
+ * ploop_read_delta() relies on it, see the comment there. nr_be of
+ * the delta is updated right away, as the lower delta is checked
+ * against it on the next iteration.
+ */
+ for (level = ploop_top_level(ploop); level >= 0; level--) {
+ delta = &ploop->deltas[level];
+
+ ret = ploop_read_delta(ploop, level, delta->file,
+ &file_size, &nr_be);
+ if (ret)
+ goto restore;
+
+ delta->file_size = file_size;
+ delta->file_preallocated_area_start = file_size;
+ delta->nr_be = nr_be;
+ }
+
+ /*
+ * Header of top delta is written with BAT page 0. Userspace doesn't
+ * mark the delta, which becomes top after flip_upper_deltas, as in
+ * use, and relies that the in-use signature of the old top delta
+ * will be written to it. So keep the signature we had. In other
+ * cases take it from disk: userspace may have changed it on the
+ * suspended device (e.g. marked the image closed on umount).
+ */
+ if (ploop->top_flipped) {
+ old_hdr = ploop_md_first_entry(&old_bat_entries)->kmpage;
+ hdr = ploop_md_first_entry(&ploop->bat_entries)->kmpage;
+ hdr->m_DiskInUse = old_hdr->m_DiskInUse;
+ ploop->top_flipped = false;
+ }
+
+ ploop_free_md_pages_tree(&old_bat_entries);
+ kvfree(old_holes_bitmap);
+ goto out;
+restore:
+ ploop_free_md_pages_tree(&ploop->bat_entries);
+ kvfree(ploop->holes_bitmap);
+ ploop->bat_entries = old_bat_entries;
+ ploop->holes_bitmap = old_holes_bitmap;
+ ploop->nr_bat_entries = old_nr_bat_entries;
+ ploop->hb_nr = old_hb_nr;
+ memcpy(ploop->m_Sig, old_m_Sig, sizeof(ploop->m_Sig));
+ ploop->m_Type = old_m_Type;
+ ploop->m_Sectors = old_m_Sectors;
+ memcpy(ploop->deltas, old_deltas,
+ sizeof(*old_deltas) * ploop->nr_deltas);
+out:
+ if (page)
+ put_page(page);
+ kfree(old_deltas);
+ if (ret)
+ PL_ERR("Failed to reload metadata: %d", ret);
+ return ret;
+}
+ALLOW_ERROR_INJECTION(ploop_reload_metadata, ERRNO);
+
void ploop_put_delta(struct ploop *ploop, struct ploop_delta *delta)
{
int i;
diff --git a/drivers/md/dm-ploop-cmd.c b/drivers/md/dm-ploop-cmd.c
index 2d1fa449b052..e1b6aea4af94 100644
--- a/drivers/md/dm-ploop-cmd.c
+++ b/drivers/md/dm-ploop-cmd.c
@@ -1087,6 +1087,7 @@ static int process_flip_upper_deltas(struct ploop *ploop)
/* FIXME */
swap(ploop->deltas[level], ploop->deltas[level+1]);
+ ploop->top_flipped = true;
return 0;
}
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 2f85383c4682..9db80a046af1 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -1195,6 +1195,8 @@ static int ploop_preallocate_cluster(struct ploop *ploop, struct file *file)
spin_lock_irqsave(&ploop->bat_lock, flags);
prealloc_more:
+ if (READ_ONCE(ploop->prealloc_stopped))
+ ploop->prealloc_size = 0;
ploop->prealloc_in_progress = ploop->prealloc_size;
ploop->prealloc_size = 0;
if (!ploop->prealloc_in_progress) {
@@ -1212,9 +1214,6 @@ static int ploop_preallocate_cluster(struct ploop *ploop, struct file *file)
goto out;
}
- /*
- * Here must be the only place to change file_size.
- */
spin_lock_irqsave(&ploop->bat_lock, flags);
if (top->file_size < new_len) {
top->file_size = new_len;
@@ -1442,6 +1441,7 @@ static void ploop_check_standby_mode(struct ploop *ploop, long res)
spin_lock_irqsave(&q->queue_lock, flags);
prev = test_and_set_bit(QUEUE_FLAG_STANDBY, &q->queue_flags);
spin_unlock_irqrestore(&q->queue_lock, flags);
+ WRITE_ONCE(ploop->standby, true);
if (!prev)
PL_INFO("was switched into the standby mode");
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index bfadca7f12ae..2be2aaeaa2d1 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -21,6 +21,7 @@
#include <linux/uio.h>
#include <linux/timer.h>
#include <linux/error-injection.h>
+#include <linux/sched/mm.h>
#include "dm-ploop.h"
#define DM_MSG_PREFIX "ploop"
@@ -297,7 +298,7 @@ static int ploop_check_top_delta(struct ploop *ploop, struct file *file)
/* flush dirty pages before changing direct IO */
vfs_fsync(file, 1);
- ret = ploop_setup_metadata(ploop, page);
+ ret = ploop_setup_metadata(ploop, page, true);
if (ret)
goto out;
@@ -428,8 +429,7 @@ static struct ploop_worker *ploop_worker_create(struct ploop *ploop,
* suspended and resumed. This behavior is intentional to allow for atomic
* updates to the mapping table without disrupting ongoing I/O operations.
*
- * But dm-ploop is not fully compatible with this design. Ploop does not do a
- * "metadata" reload on resume. So there can be situations like:
+ * So there can be situations like:
*
* 1. dmsetup reload is called, and new ploop is created with ploop_ctr for
* the same delta image file as an old ploop and ploop_add_deltas_stack reads
@@ -440,23 +440,19 @@ static struct ploop_worker *ploop_worker_create(struct ploop *ploop,
* the new one, new ploop has outdated metadata and thus may corrupt the
* images by relying on it.
*
- * For instance we thought to truncate the preallocations to the delta image
- * file on ploop_dtr, but that can't be done as ploop_dtr is called on the old
- * ploop after the new one is resumed, so we can't possibly modify the image
- * from the old ploop context at this point, as new ploop may be already doing
- * its own IO or preallocation.
+ * To handle this, ploop_preresume reloads all the metadata from the images
+ * (see ploop_reload_metadata), so that the new ploop "sees" all previous
+ * updates from the old one. For the same reason the new ploop must not
+ * modify the images before resume, so preallocation is started only after
+ * the metadata reload, and the old ploop waits for its preallocation to
+ * complete on postsuspend.
*
- * The better way to do this truncation is on ploop_*suspend, and then on resume
- * to reload the metadata in the new ploop so that the new ploop "sees" all
- * previous updates from the old one.
- *
- * For now we can leave with it, as we chose not to do this truncation on
- * destroy and we rely that userspace always does dmsetup reload on already
- * suspended device.
- *
- * FIXME: We must implement metadata reload on resume. After that we can
- * implement preallocation truncation on suspend (see dm-qcow2 as it already
- * implements that in qcow2_parse_metadata and qcow2_truncate_unused_tail).
+ * Still, we can't truncate the preallocations to the delta image file on
+ * ploop_dtr, as ploop_dtr is called on the old ploop after the new one is
+ * resumed, so we can't possibly modify the image from the old ploop context
+ * at this point, as new ploop may be already doing its own IO or
+ * preallocation. This truncation should be done on ploop_*suspend (see
+ * dm-qcow2 as it already implements that in qcow2_truncate_unused_tail).
*/
static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
@@ -495,6 +491,8 @@ static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv)
spin_lock_init(&ploop->deferred_lock);
init_llist_head(&ploop->llsuspended_pios);
+ /* A new table stays suspended until the first successful resume */
+ ploop->suspended = true;
for (i = 0; i < PLOOP_LIST_COUNT; i++)
init_llist_head(&ploop->pios[i]);
@@ -646,9 +644,6 @@ static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv)
ti->num_discard_bios = 1;
ti->discards_supported = true;
- if (!ploop_is_ro(ploop) && ploop->nr_deltas > 0)
- ploop_should_prealloc(ploop);
-
return 0;
err:
@@ -729,6 +724,11 @@ static void ploop_set_suspended(struct dm_target *ti, bool suspended)
up_write(&ploop->ctl_rwsem);
}
+static bool ploop_is_standby(struct ploop *ploop)
+{
+ return READ_ONCE(ploop->standby);
+}
+
static void ploop_presuspend(struct dm_target *ti)
{
struct ploop *ploop = ti->private;
@@ -755,29 +755,72 @@ static void ploop_postsuspend(struct dm_target *ti)
{
struct ploop *ploop = ti->private;
- wait_event(ploop->dispatcher_wq_prealloc,
- ploop_no_pending_prealloc(ploop));
+ /*
+ * Let the next resumed ploop (maybe of reloaded table) see
+ * final size of the top delta and clean md pages: data bios
+ * are completed before BAT writeback is finished (see
+ * ploop_put_piwb()). Don't wait in standby mode: the allocator
+ * or BAT writeback may be stuck on the dead file, while
+ * dm_replace() of libploop must be able to suspend the device
+ * to recover it. Pending prealloc requests are dropped, so if
+ * the wait is skipped, only the operation already in flight may
+ * change the top delta under a ploop of a reloaded table.
+ */
+ WRITE_ONCE(ploop->prealloc_stopped, true);
+ if (!ploop_is_standby(ploop)) {
+ wait_event(ploop->dispatcher_wq_prealloc,
+ ploop_no_pending_prealloc(ploop));
+ wait_event(ploop->inflight_md_wq,
+ !atomic_read(&ploop->inflight_md));
+ }
ploop_set_suspended(ti, true);
}
static int ploop_preresume(struct dm_target *ti)
{
struct ploop *ploop = ti->private;
+ unsigned int noio_flags;
+ bool reload = false;
int ret = 0;
+ /* A resize of the suspended device may leave BAT writeback in flight */
+ if (!ploop_is_standby(ploop))
+ wait_event(ploop->inflight_md_wq,
+ !atomic_read(&ploop->inflight_md));
+
down_write(&ploop->ctl_rwsem);
- if (ploop->noresume)
+ if (ploop->noresume) {
ret = -EAGAIN;
+ } else if (!ploop_is_standby(ploop) &&
+ ploop_no_pending_prealloc(ploop)) {
+ reload = true;
+ /*
+ * The device is suspended, so reclaim must not wait
+ * for IO to it. Otherwise we deadlock with ourselves.
+ */
+ noio_flags = memalloc_noio_save();
+ ret = ploop_reload_metadata(ploop);
+ memalloc_noio_restore(noio_flags);
+ }
/*
* We are singleton target. There will be no more reasons to break
* resume. Clear it under the same lock as the noresume check, so
* that flip_upper_deltas can't sneak in before the device goes live.
*/
- if (ret == 0)
+ if (ret == 0) {
ploop->suspended = false;
+ WRITE_ONCE(ploop->prealloc_stopped, false);
+ }
up_write(&ploop->ctl_rwsem);
- if (ret == 0)
+ if (ret == 0) {
+ /*
+ * Preallocate only after the metadata is reloaded,
+ * as prealloc relies on the actual top delta size.
+ */
+ if (reload && !ploop_is_ro(ploop))
+ ploop_should_prealloc(ploop);
ploop_set_wants_suspend(ti, false);
+ }
return ret;
}
ALLOW_ERROR_INJECTION(ploop_preresume, ERRNO);
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index 5ae02ef5ffc2..389a234ec80a 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -246,6 +246,15 @@ struct ploop {
/* Resume is prohibited */
bool noresume;
+ /* Top delta was swapped by flip_upper_deltas */
+ bool top_flipped;
+ /*
+ * This ploop switched the queue into standby. Unlike the queue
+ * bit, it is not cleared by ploop_ctr() of a new table.
+ */
+ bool standby;
+ /* Suspended, the allocator must not start new requests */
+ bool prealloc_stopped;
/* Device is suspended */
bool suspended;
/* Device wants suspend */
@@ -634,7 +643,9 @@ extern struct pio *ploop_alloc_pio_with_pages(struct ploop *ploop);
extern void ploop_free_pio_with_pages(struct ploop *ploop, struct pio *pio);
extern void ploop_pio_prepare_offsets(struct ploop *ploop, struct pio *pio, u32 clu);
-extern int ploop_setup_metadata(struct ploop *ploop, struct page *page);
+extern int ploop_setup_metadata(struct ploop *ploop, struct page *page,
+ bool check_in_use);
+extern int ploop_reload_metadata(struct ploop *ploop);
extern int ploop_read_delta_metadata(struct ploop *ploop, struct file *file,
struct rb_root *md_root, u32 *delta_nr_be);
extern void ploop_index_wb_init(struct ploop_index_wb *piwb,
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 8/9] dm-ploop: reload metadata on resume
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 8/9] dm-ploop: reload metadata on resume Alexander Ivanov
@ 2026-11-18 13:30 ` no-reply
2026-11-18 14:07 ` Alexander Ivanov
0 siblings, 1 reply; 26+ messages in thread
From: no-reply @ 2026-11-18 13:30 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] dm-ploop: prealloc_stopped drops resize allocation requests
on suspended device
- [Low] dm-ploop: top_flipped is not cleared when reload is skipped on
resume
- [Low] dm-ploop: preresume wait and reload clean check are not under
one lock
--
commit 240134d129537e6ddb55602e031529c851f3f05c
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
dm-ploop: reload metadata on resume
The patch reloads the top delta header, the BAT of every delta, the delta file sizes, the BAT tree and holes_bitmap in ploop_preresume(). Preallocation is requested after the reload. The patch also adds waits in postsuspend and preresume, and a per-ploop standby flag.
> diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
> --- a/drivers/md/dm-ploop-bat.c
> +++ b/drivers/md/dm-ploop-bat.c
[ ... ]
> + /*
> + * Header of top delta is written with BAT page 0. Userspace doesn't
> + * mark the delta, which becomes top after flip_upper_deltas, as in
> + * use, and relies that the in-use signature of the old top delta
> + * will be written to it. So keep the signature we had. In other
> + * cases take it from disk: userspace may have changed it on the
> + * suspended device (e.g. marked the image closed on umount).
> + */
> + if (ploop->top_flipped) {
> + old_hdr = ploop_md_first_entry(&old_bat_entries)->kmpage;
> + hdr = ploop_md_first_entry(&ploop->bat_entries)->kmpage;
> + hdr->m_DiskInUse = old_hdr->m_DiskInUse;
> + ploop->top_flipped = false;
> + }
[Severity: Low]
Is top_flipped ever cleared if the reload is skipped?
It is cleared only here, after a full reload. ploop_reload_metadata() returns 0 early when the md pages are not clean, and ploop_preresume() skips the reload in standby or while preallocation is pending. In all those cases resume still succeeds and the flag stays set after flip_upper_deltas.
If userspace later marks the image closed on disk while suspended, the next successful reload copies the in-memory IN_USE over the on-disk value. Umount then trims the image and CBT is lost. That contradicts the commit message, which says the signature is taken from disk except right after flip_upper_deltas.
[ ... ]
> diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
> --- a/drivers/md/dm-ploop-map.c
> +++ b/drivers/md/dm-ploop-map.c
> @@ -1195,6 +1195,8 @@ static int ploop_preallocate_cluster(struct ploop *ploop, struct file *file)
>
> spin_lock_irqsave(&ploop->bat_lock, flags);
> prealloc_more:
> + if (READ_ONCE(ploop->prealloc_stopped))
> + ploop->prealloc_size = 0;
> ploop->prealloc_in_progress = ploop->prealloc_size;
> ploop->prealloc_size = 0;
[Severity: Medium]
Does this also drop the synchronous request that a resize message on a suspended device depends on?
ploop_postsuspend() sets prealloc_stopped and only a successful preresume clears it. A grow resize sent while suspended goes ploop_resize() -> ... -> ploop_prepare_reloc_index_wb() -> ploop_allocate_cluster(). That calls ploop_req_prealloc() and wakes the allocator. ploop_preallocate_cluster() then zeroes prealloc_size and grows nothing.
ploop_allocate_cluster() then sees end > top->file_size and logs "Prealloc failed". It retries three times, each dropped the same way, and returns -EIO. The resize fails whenever the relocation needs the top delta to grow.
The commit message says only pending requests are dropped. The flag cannot tell a stale background request from a required synchronous one. The last patch in the series, which truncates the unused tail on suspend, makes growth more likely and does not change this check.
[ ... ]
> diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
> --- a/drivers/md/dm-ploop-target.c
> +++ b/drivers/md/dm-ploop-target.c
[ ... ]
> static int ploop_preresume(struct dm_target *ti)
> {
> struct ploop *ploop = ti->private;
> + unsigned int noio_flags;
> + bool reload = false;
> int ret = 0;
>
> + /* A resize of the suspended device may leave BAT writeback in flight */
> + if (!ploop_is_standby(ploop))
> + wait_event(ploop->inflight_md_wq,
> + !atomic_read(&ploop->inflight_md));
> +
> down_write(&ploop->ctl_rwsem);
[Severity: Low]
Can a resize message slip in between this wait and down_write(&ploop->ctl_rwsem)?
ploop_resize() does not require the device to be suspended, so it can start BAT writeback or dirty md pages in that window. ploop_reload_metadata() then sees unclean pages and returns 0 without any log message. Resume succeeds and the device goes live with the stale BAT read in ploop_ctr(). That is the situation this patch is meant to prevent.
Would it work to wait for inflight_md and run the clean check under the same lock?
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261118131032.3372996-1-alexander.ivanov@virtuozzo.com?part=8
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 8/9] dm-ploop: reload metadata on resume
2026-11-18 13:30 ` no-reply
@ 2026-11-18 14:07 ` Alexander Ivanov
0 siblings, 0 replies; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 14:07 UTC (permalink / raw)
To: no-reply, andrey.zhadchenko; +Cc: devel
>> + /*
>> + * Header of top delta is written with BAT page 0. Userspace doesn't
>> + * mark the delta, which becomes top after flip_upper_deltas, as in
>> + * use, and relies that the in-use signature of the old top delta
>> + * will be written to it. So keep the signature we had. In other
>> + * cases take it from disk: userspace may have changed it on the
>> + * suspended device (e.g. marked the image closed on umount).
>> + */
>> + if (ploop->top_flipped) {
>> + old_hdr = ploop_md_first_entry(&old_bat_entries)->kmpage;
>> + hdr = ploop_md_first_entry(&ploop->bat_entries)->kmpage;
>> + hdr->m_DiskInUse = old_hdr->m_DiskInUse;
>> + ploop->top_flipped = false;
>> + }
> [Severity: Low]
> Is top_flipped ever cleared if the reload is skipped?
>
> It is cleared only here, after a full reload. ploop_reload_metadata() returns 0 early when the md pages are not clean, and ploop_preresume() skips the reload in standby or while preallocation is pending. In all those cases resume still succeeds and the flag stays set after flip_upper_deltas.
>
> If userspace later marks the image closed on disk while suspended, the next successful reload copies the in-memory IN_USE over the on-disk value. Umount then trims the image and CBT is lost. That contradicts the commit message, which says the signature is taken from disk except right after flip_upper_deltas.
Fixed.
>
> [ ... ]
>
>> diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
>> --- a/drivers/md/dm-ploop-map.c
>> +++ b/drivers/md/dm-ploop-map.c
>> @@ -1195,6 +1195,8 @@ static int ploop_preallocate_cluster(struct ploop *ploop, struct file *file)
>>
>> spin_lock_irqsave(&ploop->bat_lock, flags);
>> prealloc_more:
>> + if (READ_ONCE(ploop->prealloc_stopped))
>> + ploop->prealloc_size = 0;
>> ploop->prealloc_in_progress = ploop->prealloc_size;
>> ploop->prealloc_size = 0;
> [Severity: Medium]
> Does this also drop the synchronous request that a resize message on a suspended device depends on?
>
> ploop_postsuspend() sets prealloc_stopped and only a successful preresume clears it. A grow resize sent while suspended goes ploop_resize() -> ... -> ploop_prepare_reloc_index_wb() -> ploop_allocate_cluster(). That calls ploop_req_prealloc() and wakes the allocator. ploop_preallocate_cluster() then zeroes prealloc_size and grows nothing.
>
> ploop_allocate_cluster() then sees end > top->file_size and logs "Prealloc failed". It retries three times, each dropped the same way, and returns -EIO. The resize fails whenever the relocation needs the top delta to grow.
>
> The commit message says only pending requests are dropped. The flag cannot tell a stale background request from a required synchronous one. The last patch in the series, which truncates the unused tail on suspend, makes growth more likely and does not change this check.
Fixed.
>> static int ploop_preresume(struct dm_target *ti)
>> {
>> struct ploop *ploop = ti->private;
>> + unsigned int noio_flags;
>> + bool reload = false;
>> int ret = 0;
>>
>> + /* A resize of the suspended device may leave BAT writeback in flight */
>> + if (!ploop_is_standby(ploop))
>> + wait_event(ploop->inflight_md_wq,
>> + !atomic_read(&ploop->inflight_md));
>> +
>> down_write(&ploop->ctl_rwsem);
> [Severity: Low]
> Can a resize message slip in between this wait and down_write(&ploop->ctl_rwsem)?
>
> ploop_resize() does not require the device to be suspended, so it can start BAT writeback or dirty md pages in that window. ploop_reload_metadata() then sees unclean pages and returns 0 without any log message. Resume succeeds and the device goes live with the stale BAT read in ploop_ctr(). That is the situation this patch is meant to prevent.
>
> Would it work to wait for inflight_md and run the clean check under the same lock?
The race needs userspace to send a resize to the new table concurrently
with its resume,
which libploop does not do, and waiting under ctl_rwsem would not cover
the wider
case of a resize sent to a table before its first resume anyway.
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread
* [Devel] [PATCH hci-8.2 v8 9/9] dm-ploop: truncate unused tail of top delta on suspend
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (7 preceding siblings ...)
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 8/9] dm-ploop: reload metadata on resume Alexander Ivanov
@ 2026-11-18 13:10 ` Alexander Ivanov
2026-11-18 13:29 ` no-reply
2026-11-18 13:11 ` [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back " Andrey Zhadchenko
2026-11-18 13:41 ` Konstantin Khorenko
10 siblings, 1 reply; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 13:10 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
Preallocated space and clusters at the end of the top delta freed by
discard are never given back, so the file size never shrinks. Now that
ploop reloads metadata on resume, we can truncate the top delta on
suspend: the next resumed ploop (maybe of reloaded table) picks up the
new file size.
Add ploop_calc_used_end(), which returns the end of the last cluster
used by the delta: either BAT area, the last mapped cluster or format
extension. Metadata is read from disk rather than taken from memory:
the next table load checks BAT entries against the file size (-EPROTO
otherwise), while in-memory BAT may differ from on-disk one after
failed BAT writeback. Skip it if md pages are not clean, as on-disk BAT
may miss allocated clusters then.
Userspace writes format extension (CBT) behind the last data cluster of
suspended device on umount, resumes it, and then removes the device.
Removal of a live device runs the suspend hooks, and so the truncation,
from __dm_destroy() without dm_suspend() waiting for inflight IO. This
is safe only because an open device cannot be removed. So keep the area
up to the end of format extension header, which is the last cluster of
the extension, as userspace does. Name the corresponding header field
properly instead of m_Reserved. The field was never interpreted before,
so if it is not cluster-aligned or out of range, don't truncate
anything: unaligned truncation would make the next resume fail with
-EPROTO.
Truncate the top delta to the used end in ploop_postsuspend() after
pending preallocation is completed. Do it under ctl_rwsem, so that
ploop messages (e.g. resize) don't change the image in parallel. Take
it only after the waits, so that a stuck top delta doesn't block status
and messages: a message slipping in before the lock can at worst make
the truncation skip, as the checks under the lock catch pending
prealloc, file size change and unclean md pages. Skip read-only devices
and raw top delta. On error just leave the file as is.
Skip devices in standby mode as well: the top delta file is dead then,
and the truncation may hang with md->suspend_lock held, so dm_replace()
of libploop could neither replace nor remove the device.
Also skip it if the file size differs from top->file_size. Then another
ploop on the same images writes to the top delta (e.g. a live one left
by libploop error path), and on-disk BAT may miss clusters it has just
allocated, so the truncation would cut its data. The check is best
effort: a ploop that preallocated before our last metadata reload and
allocates clusters in that area later does not change the size.
https://virtuozzo.atlassian.net/browse/VSTOR-148171
Feature: dm-ploop: ploop target driver
Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
---
drivers/md/dm-ploop-bat.c | 56 ++++++++++++++++++++++++++
drivers/md/dm-ploop-target.c | 76 ++++++++++++++++++++++++++++++------
drivers/md/dm-ploop.h | 3 +-
3 files changed, 123 insertions(+), 12 deletions(-)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index a6894910a5bf..a39ae5b9be6b 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -707,6 +707,62 @@ int ploop_reload_metadata(struct ploop *ploop)
}
ALLOW_ERROR_INJECTION(ploop_reload_metadata, ERRNO);
+/*
+ * Returns the end of the last cluster of delta @file used by BAT area,
+ * mapped clusters or format extension, according to on-disk metadata.
+ * Must be called on suspended device.
+ */
+loff_t ploop_calc_used_end(struct ploop *ploop, struct file *file)
+{
+ u32 i, end, nr_be, dst_clu, used_clus, *bat_entries;
+ struct rb_root md_root = RB_ROOT;
+ struct ploop_pvd_header *hdr;
+ struct rb_node *node;
+ struct md_page *md;
+ u64 ext_off, ext_clu;
+ loff_t used_end;
+ int ret;
+
+ /* BAT on disk may miss allocated clusters until writeback is done */
+ if (!ploop_md_pages_are_clean(ploop))
+ return -EBUSY;
+
+ ret = ploop_read_delta_metadata(ploop, file, &md_root, &nr_be);
+ if (ret)
+ return ret;
+
+ used_clus = ploop_nr_bat_clusters(ploop, nr_be);
+ rb_root_for_each_md_page(&md_root, md, node) {
+ bat_entries = md->kmpage;
+ init_be_iter(nr_be, md->id, &i, &end);
+ for (; i <= end; i++) {
+ dst_clu = READ_ONCE(bat_entries[i]);
+ if (dst_clu != BAT_ENTRY_NONE && dst_clu >= used_clus)
+ used_clus = dst_clu + 1;
+ }
+ }
+
+ /* The header is the last cluster, see delta_save_optional_header() */
+ hdr = ploop_md_first_entry(&md_root)->kmpage;
+ ext_off = le64_to_cpu(hdr->m_FormatExtensionOffset);
+ if (ext_off) {
+ ext_clu = SEC_TO_CLU(ploop, ext_off);
+ if (CLU_TO_SEC(ploop, ext_clu) != ext_off ||
+ ext_clu >= BAT_ENTRY_NONE) {
+ used_end = -EPROTO;
+ goto out;
+ }
+ if (ext_clu >= used_clus)
+ used_clus = ext_clu + 1;
+ }
+
+ used_end = CLU_TO_POS(ploop, used_clus);
+out:
+ ploop_free_md_pages_tree(&md_root);
+ return used_end;
+}
+ALLOW_ERROR_INJECTION(ploop_calc_used_end, ERRNO);
+
void ploop_put_delta(struct ploop *ploop, struct ploop_delta *delta)
{
int i;
diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 2be2aaeaa2d1..026db69add1f 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -451,8 +451,9 @@ static struct ploop_worker *ploop_worker_create(struct ploop *ploop,
* ploop_dtr, as ploop_dtr is called on the old ploop after the new one is
* resumed, so we can't possibly modify the image from the old ploop context
* at this point, as new ploop may be already doing its own IO or
- * preallocation. This truncation should be done on ploop_*suspend (see
- * dm-qcow2 as it already implements that in qcow2_truncate_unused_tail).
+ * preallocation. So this truncation is done on ploop_postsuspend (see
+ * ploop_truncate_unused_tail), and the new ploop picks up the new size
+ * of the top delta on metadata reload.
*/
static int ploop_ctr(struct dm_target *ti, unsigned int argc, char **argv)
{
@@ -715,14 +716,6 @@ static void ploop_set_wants_suspend(struct dm_target *ti, bool wants)
ploop->wants_suspend = wants;
spin_unlock_irq(&ploop->deferred_lock);
}
-static void ploop_set_suspended(struct dm_target *ti, bool suspended)
-{
- struct ploop *ploop = ti->private;
-
- down_write(&ploop->ctl_rwsem);
- ploop->suspended = suspended;
- up_write(&ploop->ctl_rwsem);
-}
static bool ploop_is_standby(struct ploop *ploop)
{
@@ -751,9 +744,64 @@ static void ploop_presuspend_undo(struct dm_target *ti)
{
ploop_set_wants_suspend(ti, false);
}
+
+/*
+ * Truncate preallocations and the tail of the top delta, which became
+ * unused after discard. The next resumed ploop (maybe of reloaded
+ * table) re-reads the size of the top delta in ploop_preresume().
+ */
+static void ploop_truncate_unused_tail(struct ploop *ploop)
+{
+ struct ploop_delta *top = ploop_top_delta(ploop);
+ unsigned long flags;
+ loff_t end;
+ int ret;
+
+ lockdep_assert_held_write(&ploop->ctl_rwsem);
+
+ if (ploop_is_ro(ploop) || top->is_raw)
+ return;
+ if (ploop_is_standby(ploop))
+ return;
+ if (!ploop_no_pending_prealloc(ploop))
+ return;
+ if (i_size_read(file_inode(top->file)) != top->file_size) {
+ PL_ERR("Top delta is changed by someone else, don't truncate it");
+ return;
+ }
+
+ end = ploop_calc_used_end(ploop, top->file);
+ if (end < 0) {
+ PL_ERR("Can't find used end of top delta: %lld", end);
+ return;
+ }
+ if (end >= top->file_size)
+ return;
+
+ /* BAT writeback finished after ploop_presuspend() is not durable yet */
+ ret = vfs_fsync(top->file, 0);
+ if (ret) {
+ PL_ERR("Can't sync top delta: %d", ret);
+ return;
+ }
+
+ ret = do_ftruncate(top->file, end, 0);
+ if (ret) {
+ PL_ERR("Can't truncate unused tail of top delta: %d", ret);
+ return;
+ }
+
+ spin_lock_irqsave(&ploop->bat_lock, flags);
+ top->file_size = end;
+ if (top->file_preallocated_area_start > end)
+ top->file_preallocated_area_start = end;
+ spin_unlock_irqrestore(&ploop->bat_lock, flags);
+}
+
static void ploop_postsuspend(struct dm_target *ti)
{
struct ploop *ploop = ti->private;
+ unsigned int noio_flags;
/*
* Let the next resumed ploop (maybe of reloaded table) see
@@ -773,7 +821,13 @@ static void ploop_postsuspend(struct dm_target *ti)
wait_event(ploop->inflight_md_wq,
!atomic_read(&ploop->inflight_md));
}
- ploop_set_suspended(ti, true);
+ down_write(&ploop->ctl_rwsem);
+ /* The device is suspended, see ploop_preresume() */
+ noio_flags = memalloc_noio_save();
+ ploop_truncate_unused_tail(ploop);
+ memalloc_noio_restore(noio_flags);
+ ploop->suspended = true;
+ up_write(&ploop->ctl_rwsem);
}
static int ploop_preresume(struct dm_target *ti)
{
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index 389a234ec80a..3fda60cb999f 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -37,7 +37,7 @@ struct ploop_pvd_header {
__u32 m_DiskInUse; /* Disk in use */
__u32 m_FirstBlockOffset; /* First data block offset (in sectors) */
__u32 m_Flags; /* Misc flags */
- __u8 m_Reserved[8]; /* Reserved */
+ __u64 m_FormatExtensionOffset; /* Format extension offset (in sectors) */
};
#pragma pack(pop)
@@ -646,6 +646,7 @@ extern void ploop_pio_prepare_offsets(struct ploop *ploop, struct pio *pio, u32
extern int ploop_setup_metadata(struct ploop *ploop, struct page *page,
bool check_in_use);
extern int ploop_reload_metadata(struct ploop *ploop);
+extern loff_t ploop_calc_used_end(struct ploop *ploop, struct file *file);
extern int ploop_read_delta_metadata(struct ploop *ploop, struct file *file,
struct rb_root *md_root, u32 *delta_nr_be);
extern void ploop_index_wb_init(struct ploop_index_wb *piwb,
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (8 preceding siblings ...)
2026-11-18 13:10 ` [Devel] [PATCH hci-8.2 v8 9/9] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
@ 2026-11-18 13:11 ` Andrey Zhadchenko
2026-11-18 13:41 ` Konstantin Khorenko
10 siblings, 0 replies; 26+ messages in thread
From: Andrey Zhadchenko @ 2026-11-18 13:11 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
On 11/18/26 14:10, Alexander Ivanov wrote:
> Preallocated space and clusters at the end of the top delta freed by
> discard are never given back, so the image file never shrinks. The
> last patch truncates the top delta to the end of its last used cluster
> on suspend.
>
> The next resumed ploop must see the new file size, so the previous
> patch reloads all the metadata (header, BAT, file sizes) on resume.
> This also fixes "dmsetup reload", after which the new ploop worked with
> metadata read in ploop_ctr() while the old one kept changing images.
>
> Patches 1-3 fix header validation, which the reload would otherwise
> hit. Patches 4-6 prepare the reload and fix races on md pages and
> inflight_md that the new waits on suspend expose. Patch 7 fixes
> flip_upper_deltas racing with resume in ploop_preresume(), where the
> reload goes.
>
> The corresponding ploop userspace changes:
> https://bitbucket.org/openvz/ploop.ovz/pull-requests/486
>
> https://virtuozzo.atlassian.net/browse/VSTOR-148171
>
> v8:
> - Add Fixes tags to patches 1-3 and 5-7.
> - Patch 3: drop the wrong hash of the driver commit from the message,
> it is in the Fixes tag now.
> - Drop patch 6 ("dm-ploop: use wake_up() for inflight_md_wq"): patch 6
> (was 7) replaces these wakeups with wake_up_locked(), which wakes
> uninterruptible waiters too.
> - Patch 6 (was 7): only ploop_destroy() takes inflight_md_wq.lock after
> the wait, leave the wait in ploop_resize() as is.
> - Patch 8 (was 9):
> - keep the waits for preallocation and BAT writeback in postsuspend
> uninterruptible and skip both in standby; drop
> ploop_wait_prealloc();
> - drop pending prealloc requests while suspended (prealloc_stopped),
> so after a skipped wait the old allocator doesn't resize the top
> delta under a ploop of a reloaded table;
> - wait for BAT writeback in preresume too, as a resize of the
> suspended device may leave it in flight;
> - keep dirty metadata on resume without WARN_ON_ONCE();
> - drop the stale comment that ploop_preallocate_cluster() is the only
> place to change file_size.
> - Patch 9 (was 10): take ctl_rwsem after the waits, so a stuck top
> delta doesn't block status and messages; skip the truncation without
> WARN_ON_ONCE() if md pages are dirty; describe the truncation on
> removal of a live device and the best-effort size check in the
> message.
>
> v7:
> - Rebase on the current hci-8.1.
> - Drop patch 1, it is merged as 932bd3ae4705 ("dm-ploop: fix u32
> overflow of the delta BAT byte size").
> - Drop patch 2, the same lost wakeup is fixed by 50651c191a31
> ("dm-ploop: fix a lost wakeup of the ploop_allocator() thread").
> - Patch 6 (was 8): drop the dispatcher_wq_prealloc part, a5836033b8fe
> ("dm-ploop: wait for the ploop_allocator() thread before changing
> deltas[]") already wakes it with wake_up().
> - Patch 7 (was 9): keep the wait for preallocation that a5836033b8fe
> added to ploop_suspend_submitting_pios() after the inflight_md one.
> - Patch 8 (was 10): add the missing Jira link, Feature and
> Signed-off-by.
> - Patch 9 (was 11): ploop_postsuspend() already waits for
> preallocation since a5836033b8fe; make that wait killable and skip
> it in standby instead of adding a new one.
>
> v6:
> - Patch 10: new. Clear ->suspended in ploop_preresume() under the same
> ctl_rwsem as the noresume check, so that flip_upper_deltas can't
> pass its check between them.
> - Patch 11 (was 10): rebase on patch 10; mark a new ploop suspended in
> ploop_ctr(), as it is not live until the first successful resume.
> - Patch 12 (was 11): fsync the top delta before the truncation, as BAT
> writeback finished after ploop_presuspend() is not durable yet; drop
> ploop_set_suspended(), which is unused now.
>
> v5:
> - Patch 9: in ploop_destroy(), wait for inflight_md with wait_event()
> and then take and drop inflight_md_wq.lock to wait for
> ploop_dec_inflight_md() to release it, instead of waiting with
> wait_event_lock_irq() under the lock. Reword the comment of
> ploop_dec_inflight_md() and the commit message accordingly.
>
> v4:
> - Patch 1: was sent separately as "[PATCH hci-8.1] dm-ploop: fix u32
> overflow of the delta BAT byte size", unchanged.
> - Patch 2 (was 1): drop the comment above set_current_state(), add
> Reviewed-by.
> - Patches 3, 4: new. Reject a top delta header whose BAT does not
> cover the target, and reject empty deltas, instead of skipping the
> mappings of an empty delta in patch 5.
> - Patch 5 (was 2): say that raw deltas have never worked since the
> driver was added; drop the empty delta handling, see patches 3, 4;
> add Reviewed-by.
> - Patch 6 (was 3): fix the stale comment of ploop_add_delta() about
> the order deltas are added in.
> - Patches 7, 8: split from patch 10. Patch 7 takes bat_lock around md
> page lookup in ploop_llseek_hole() to protect it from resize, so
> llseek doesn't wait for long control commands.
> - Patch 9: new, fixes a use-after-free of the ploop on the last
> inflight_md decrement from BAT write completion.
> - Patch 10 (was 4):
> - test MD_* flags with test_bit(), they are bit numbers;
> - keep the in-memory m_DiskInUse only after flip_upper_deltas, take
> it from disk otherwise, so the CBT written on umount isn't lost;
> - don't wait for preallocation on postsuspend in standby, and make
> the wait killable; skip the reload and prealloc on resume in
> standby or while preallocation is pending;
> - track standby per ploop, as ploop_ctr() of a new table clears the
> queue bit before the old table is suspended;
> - drop ctl_rwsem and the suspended check from ploop_llseek_hole():
> dm core doesn't call it on a suspended device;
> - describe the new preresume failures in the commit message.
> - Patch 11 (was 5): don't truncate in standby, if the wait for
> preallocation was killed, or if the file size differs from
> top->file_size, i.e. another ploop writes to the top delta.
> Shorten the comments of ploop_calc_used_end(), which repeated the
> commit message.
>
> v3:
> - Split from the common series with dm-qcow2: dm-qcow2 patches are
> sent as a separate series. Patches 3-6 of v2 are patches 2-5 now.
> - Patch 1: new patch.
> - Patch 2: also skip applying the mappings of an empty delta, as BAT
> iterators can't handle nr_be == 0.
> - Patch 4: wait for inflight_md on postsuspend, as data bios are
> completed before BAT writeback is finished, and use wake_up() for
> inflight_md_wq; reload metadata under memalloc_noio_save(), as the
> device is suspended; restore m_Sig, m_Type and m_Sectors on reload
> failure.
> - Patch 5: don't truncate anything if m_FormatExtensionOffset is not
> cluster-aligned or out of range; truncate under memalloc_noio_save().
>
> v2:
> - Add dm-ploop support (patches 3-6), as requested in v1 review.
> - dm-qcow2: move get_md_page_sync() to a separate patch as
> qcow2_md_page_read_sync() and use it in qcow2_parse_header() as well.
> - dm-qcow2: start the refcount scan below file_preallocated_area_start
> instead of the end of the file, and truncate to the used end directly
> instead of min(file_preallocated_area_start, used end).
>
> Alexander Ivanov (9):
> dm-ploop: reject a delta header whose BAT does not cover the target
> dm-ploop: reject empty deltas
> dm-ploop: set delta is_raw before applying its mappings
> dm-ploop: factor out ploop_read_delta() from ploop_add_delta()
> dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole()
> dm-ploop: fix use-after-free on the last inflight_md decrement
> dm-ploop: fix flip_upper_deltas racing with resume
> dm-ploop: reload metadata on resume
> dm-ploop: truncate unused tail of top delta on suspend
>
> drivers/md/dm-ploop-bat.c | 255 +++++++++++++++++++++++++++++++++--
> drivers/md/dm-ploop-cmd.c | 1 +
> drivers/md/dm-ploop-map.c | 31 ++++-
> drivers/md/dm-ploop-target.c | 179 +++++++++++++++++++-----
> drivers/md/dm-ploop.h | 16 ++-
> 5 files changed, 425 insertions(+), 57 deletions(-)
>
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend
2026-11-18 13:10 [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (9 preceding siblings ...)
2026-11-18 13:11 ` [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back " Andrey Zhadchenko
@ 2026-11-18 13:41 ` Konstantin Khorenko
2026-11-18 15:07 ` Alexander Ivanov
10 siblings, 1 reply; 26+ messages in thread
From: Konstantin Khorenko @ 2026-11-18 13:41 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v9 with the changes below.
All eight v7 items are done in the code, thanks. What is left:
1. 8/9: prealloc_stopped drops every preallocation request for the
whole time the device is suspended, so a "resize" (or a merge) of
a suspended device that needs a cluster past top->file_size now
fails with -EIO after the "Prealloc failed" retries; before it grew
the file. Either fence only when the postsuspend wait was skipped
(standby), or refuse resize and merge on a suspended device with
-EBUSY, and say which in the message. ploop_flip_upper_deltas()
still says "A resize of the suspended device may have restarted
the allocator".
2. 8/9: the preresume wait for inflight_md runs before ctl_rwsem, so a
resize issued during the resume can leave it at 1 and the reload is
skipped with no trace; at least log it.
3. Messages: 6/9 says "calls wake_up()" where the replaced code calls
wake_up_interruptible(), and does not say that wake_up_locked() is
what wakes the new uninterruptible wait in ploop_destroy(); split
the "Request preallocation..." paragraph of 8/9 (eight sentences)
and give the two __dm_destroy() sentences of 9/9 their own
paragraph; 9/9 logs an expected -EBUSY skip on dirty md as an
error.
4. Cover letter: the deploy order against the ploop PR is still
missing; 3/9 fixes the is_raw order, not header validation; the
wait left as is is in ploop_suspend_submitting_pios(), not in
ploop_resize().
On Wed, 18 Nov 2026 14:10:23 +0100, Alexander Ivanov wrote:
> - Patch 8 (was 9):
> - keep the waits for preallocation and BAT writeback in postsuspend
> uninterruptible and skip both in standby; drop
> ploop_wait_prealloc();
> - drop pending prealloc requests while suspended (prealloc_stopped),
> so after a skipped wait the old allocator doesn't resize the top
> delta under a ploop of a reloaded table;
1. Only work that can run after the table swap needs fencing, and the
standby case is the only one where the wait is skipped. A message on
the old table finishes before the swap (__bind() calls dm_sync_table()),
so a resize of the suspended device does not race with the new table.
Thanks.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread* Re: [Devel] [PATCH hci-8.2 v8 0/9] dm-ploop: give back unused tail of top delta on suspend
2026-11-18 13:41 ` Konstantin Khorenko
@ 2026-11-18 15:07 ` Alexander Ivanov
0 siblings, 0 replies; 26+ messages in thread
From: Alexander Ivanov @ 2026-11-18 15:07 UTC (permalink / raw)
To: Konstantin Khorenko; +Cc: devel
On 11/18/26 14:41, Konstantin Khorenko wrote:
> I won't apply this version, please send v9 with the changes below.
>
> All eight v7 items are done in the code, thanks. What is left:
>
> 1. 8/9: prealloc_stopped drops every preallocation request for the
> whole time the device is suspended, so a "resize" (or a merge) of
> a suspended device that needs a cluster past top->file_size now
> fails with -EIO after the "Prealloc failed" retries; before it grew
> the file. Either fence only when the postsuspend wait was skipped
> (standby), or refuse resize and merge on a suspended device with
> -EBUSY, and say which in the message. ploop_flip_upper_deltas()
> still says "A resize of the suspended device may have restarted
> the allocator".
There is no persistent fence in the new version: ploop_stop_prealloc()
zeroes prealloc_size once in postsuspend and once at the end of a
message to a suspended device, and waits for the operation in flight
unless in standby. A resize or merge of a suspended device still gets
its preallocation while it runs, so it doesn't hit "Prealloc failed" / -EIO.
Only the requests left after it ends are dropped.
Rewrote the comment to "In standby, postsuspend may leave a prealloc in
flight".
> 2. 8/9: the preresume wait for inflight_md runs before ctl_rwsem, so a
> resize issued during the resume can leave it at 1 and the reload is
> skipped with no trace; at least log it.
Agreed, a message can slip in between the wait and down_write(). Fixed
by repeating the wait under ctl_rwsem. The first wait stays outside the
lock so a stuck writeback doesn't block status and messages.
> 3. Messages: 6/9 says "calls wake_up()" where the replaced code calls
> wake_up_interruptible(), and does not say that wake_up_locked() is
> what wakes the new uninterruptible wait in ploop_destroy(); split
> the "Request preallocation..." paragraph of 8/9 (eight sentences)
> and give the two __dm_destroy() sentences of 9/9 their own
> paragraph; 9/9 logs an expected -EBUSY skip on dirty md as an
> error.
- 6/9: fixed to wake_up_interruptible(), and the message now
says wake_up_locked() is what wakes the uninterruptible wait in
ploop_destroy().
- 8/9: split the preallocation paragraph into three.
- 9/9: moved the __dm_destroy() sentences into their own paragraph.
Moved the md_pages_are_clean check into ploop_truncate_unused_tail(),
so dirty md now skips the truncation silently instead of logging an
error.
> 4. Cover letter: the deploy order against the ploop PR is still
> missing; 3/9 fixes the is_raw order, not header validation; the
> wait left as is is in ploop_suspend_submitting_pios(), not in
> ploop_resize().
Fixed 2 and 3 in the cover letter. There is no deploy order to state:
the userspace PR is already merged. The cover letter now says
that it dropped the manual tail trimming, so the tail is not given
back until this series is applied.
> On Wed, 18 Nov 2026 14:10:23 +0100, Alexander Ivanov wrote:
>> - Patch 8 (was 9):
>> - keep the waits for preallocation and BAT writeback in postsuspend
>> uninterruptible and skip both in standby; drop
>> ploop_wait_prealloc();
>> - drop pending prealloc requests while suspended (prealloc_stopped),
>> so after a skipped wait the old allocator doesn't resize the top
>> delta under a ploop of a reloaded table;
> 1. Only work that can run after the table swap needs fencing, and the
> standby case is the only one where the wait is skipped. A message on
> the old table finishes before the swap (__bind() calls dm_sync_table()),
> so a resize of the suspended device does not race with the new table.
dm_sync_table() waits for the message, but not for the ploop_allocator()
thread it kicks: a resize relocates clusters via
ploop_allocate_cluster(), and
the allocator may keep preallocating after the swap, standby or not,
hence ploop_stop_prealloc() at the end of a message to the suspended
device. The BAT write of a resize is synchronous indeed, I reworded the
message: only its inflight_md reference may be dropped late, which
matters for resume of the same table.
> Thanks.
>
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 26+ messages in thread