* [Devel] [PATCH hci-8.1 v7 01/10] dm-ploop: reject a delta header whose BAT does not cover the target
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:22 ` no-reply
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 02/10] dm-ploop: reject empty deltas Alexander Ivanov
` (9 subsequent siblings)
10 siblings, 1 reply; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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
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] 27+ messages in thread* [Devel] [PATCH hci-8.1 v7 02/10] dm-ploop: reject empty deltas
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 01/10] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:22 ` no-reply
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 03/10] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
` (8 subsequent siblings)
10 siblings, 1 reply; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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
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] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 02/10] dm-ploop: reject empty deltas
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 02/10] dm-ploop: reject empty deltas Alexander Ivanov
@ 2026-11-17 17:22 ` no-reply
0 siblings, 0 replies; 27+ messages in thread
From: no-reply @ 2026-11-17 17:22 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] dm-ploop: bug-fix commit lacks a Fixes: trailer for the driver
import
--
commit b52a2d20671de0cbbaeaf4282116b164f5fc9dc9
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
dm-ploop: reject empty deltas
This commit rejects deltas with zero BAT entries. It adds a !delta_nr_be check in ploop_delta_check_header() and a zero-size check for raw deltas in ploop_add_delta().
> dm-ploop: reject empty deltas
>
> 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
> Feature: dm-ploop: ploop target driver
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
[Severity: Low]
This fixes a real validation gap that has been there since the driver was
added. Should it carry a Fixes: tag, so that backport tooling can track
it?
Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261117171743.3216794-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] 27+ messages in thread
* [Devel] [PATCH hci-8.1 v7 03/10] dm-ploop: set delta is_raw before applying its mappings
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 01/10] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 02/10] dm-ploop: reject empty deltas Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:25 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 04/10] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
` (7 subsequent siblings)
10 siblings, 2 replies; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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 in commit
6ddad5a4d226 ("dm-ploop: Add ploop target driver"), 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
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] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 03/10] dm-ploop: set delta is_raw before applying its mappings
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 03/10] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
@ 2026-11-17 17:25 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: no-reply @ 2026-11-17 17:25 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] dm-ploop: commit message cites wrong introducing SHA and lacks
Fixes trailer
--
commit 0edbd8e132b17fcb0f8abc552d98a02d5ce9484c
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> dm-ploop: set delta is_raw before applying its mappings
>
> 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 in commit
> 6ddad5a4d226 ("dm-ploop: Add ploop target driver"), 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
> Feature: dm-ploop: ploop target driver
>
> Signed-off-by: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
> Reviewed-by: Denis V. Lunev <den@openvz.org>
[Severity: Low]
Is the commit cited in the message the right one? 6ddad5a4d226 ("dm-ploop: Add ploop target driver") is not an ancestor of the baseline. The commit that introduced the driver in this history is 90bb85536272 ("dm-ploop: Add ploop target driver"), and the message should cite that one.
This is a bug fix, but it carries no Fixes: tag. Should it include:
Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver")
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261117171743.3216794-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] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 03/10] dm-ploop: set delta is_raw before applying its mappings
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 03/10] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
2026-11-17 17:25 ` no-reply
@ 2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: Konstantin Khorenko @ 2026-11-17 18:23 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v8 with the changes below.
On Tue, 17 Nov 2026 18:17:37 +0100, Alexander Ivanov wrote:
> This has been the case since the driver was added in commit
> 6ddad5a4d226 ("dm-ploop: Add ploop target driver"), i.e. raw deltas
Item 8 of the cover letter reply: in this branch the driver was added
by 90bb85536272; 6ddad5a4d226 is not reachable from it.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread
* [Devel] [PATCH hci-8.1 v7 04/10] dm-ploop: factor out ploop_read_delta() from ploop_add_delta()
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (2 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 03/10] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:24 ` no-reply
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 05/10] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
` (6 subsequent siblings)
10 siblings, 1 reply; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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] 27+ messages in thread* [Devel] [PATCH hci-8.1 v7 05/10] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole()
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (3 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 04/10] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:27 ` no-reply
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 06/10] dm-ploop: use wake_up() for inflight_md_wq Alexander Ivanov
` (5 subsequent siblings)
10 siblings, 1 reply; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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
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] 27+ messages in thread* [Devel] [PATCH hci-8.1 v7 06/10] dm-ploop: use wake_up() for inflight_md_wq
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (4 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 05/10] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:26 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 07/10] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
` (4 subsequent siblings)
10 siblings, 2 replies; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 UTC (permalink / raw)
To: andrey.zhadchenko; +Cc: devel
wake_up_interruptible() wakes only TASK_INTERRUPTIBLE waiters. All the
current waiters on inflight_md_wq sleep interruptibly, but one of the
next patches adds uninterruptible ones on postsuspend, which would never
be woken. Use wake_up(), which wakes both kinds.
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-map.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 1df31bbc5410..ccecc90aa2ca 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -941,7 +941,7 @@ static void ploop_put_piwb(struct ploop_index_wb *piwb)
*/
if (atomic_dec_and_test(&piwb->ploop->inflight_md))
- wake_up_interruptible(&piwb->ploop->inflight_md_wq);
+ wake_up(&piwb->ploop->inflight_md_wq);
ploop_free_piwb(piwb);
}
}
@@ -1114,7 +1114,7 @@ void ploop_break_bat_update(struct ploop *ploop, struct md_page *md,
spin_unlock_irqrestore(&ploop->bat_lock, flags);
if (atomic_dec_and_test(&ploop->inflight_md))
- wake_up_interruptible(&ploop->inflight_md_wq);
+ wake_up(&ploop->inflight_md_wq);
ploop_free_piwb(piwb);
}
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 06/10] dm-ploop: use wake_up() for inflight_md_wq
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 06/10] dm-ploop: use wake_up() for inflight_md_wq Alexander Ivanov
@ 2026-11-17 17:26 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: no-reply @ 2026-11-17 17:26 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
> wake_up_interruptible() wakes only TASK_INTERRUPTIBLE waiters. All the
> current waiters on inflight_md_wq sleep interruptibly, but one of the
> next patches adds uninterruptible ones on postsuspend, which would never
> be woken. Use wake_up(), which wakes both kinds.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-148171
> 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/20261117171743.3216794-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] 27+ messages in thread
* Re: [Devel] [PATCH hci-8.1 v7 06/10] dm-ploop: use wake_up() for inflight_md_wq
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 06/10] dm-ploop: use wake_up() for inflight_md_wq Alexander Ivanov
2026-11-17 17:26 ` no-reply
@ 2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: Konstantin Khorenko @ 2026-11-17 18:23 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v8 with the changes below.
On Tue, 17 Nov 2026 18:17:40 +0100, Alexander Ivanov wrote:
> - wake_up_interruptible(&piwb->ploop->inflight_md_wq);
Item 6 of the cover letter reply: 7/10 replaces both calls this patch
changes, so the patch has no effect of its own.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread
* [Devel] [PATCH hci-8.1 v7 07/10] dm-ploop: fix use-after-free on the last inflight_md decrement
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (5 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 06/10] dm-ploop: use wake_up() for inflight_md_wq Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:32 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 08/10] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
` (3 subsequent siblings)
10 siblings, 2 replies; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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, so the message can finish
and the table can be destroyed under the waker.
Free the piwb first, then decrement inflight_md and wake the waiters
under inflight_md_wq.lock as the last access to the ploop. Have the
waiters take the same lock once they see zero, so they cannot proceed
until the waker has released it, and wait for it in ploop_destroy()
before freeing anything.
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-cmd.c | 6 ++++--
drivers/md/dm-ploop-map.c | 22 ++++++++++++++++++----
drivers/md/dm-ploop-target.c | 7 +++++++
3 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/drivers/md/dm-ploop-cmd.c b/drivers/md/dm-ploop-cmd.c
index 2d1fa449b052..fedf13969036 100644
--- a/drivers/md/dm-ploop-cmd.c
+++ b/drivers/md/dm-ploop-cmd.c
@@ -157,8 +157,10 @@ static int ploop_suspend_submitting_pios(struct ploop *ploop)
if (ret)
goto out;
- ret = wait_event_interruptible(ploop->inflight_md_wq,
- !atomic_read(&ploop->inflight_md));
+ spin_lock_irq(&ploop->inflight_md_wq.lock);
+ ret = wait_event_interruptible_locked_irq(ploop->inflight_md_wq,
+ !atomic_read(&ploop->inflight_md));
+ spin_unlock_irq(&ploop->inflight_md_wq.lock);
if (ret)
goto out;
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index ccecc90aa2ca..442537340b51 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -924,9 +924,25 @@ 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);
+/*
+ * Waiters do not act on zero inflight_md without holding inflight_md_wq.lock,
+ * so the ploop stays valid while we hold it. Once we drop it, the ploop may
+ * be freed, so 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 +956,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(&piwb->ploop->inflight_md_wq);
ploop_free_piwb(piwb);
+ ploop_dec_inflight_md(ploop);
}
}
@@ -1113,9 +1128,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(&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] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 07/10] dm-ploop: fix use-after-free on the last inflight_md decrement
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 07/10] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
@ 2026-11-17 17:32 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: no-reply @ 2026-11-17 17:32 UTC (permalink / raw)
To: Alexander Ivanov, 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, so the message can finish
> and the table can be destroyed under the waker.
>
> Free the piwb first, then decrement inflight_md and wake the waiters
> under inflight_md_wq.lock as the last access to the ploop. Have the
> waiters take the same lock once they see zero, so they cannot proceed
> until the waker has released it, and wait for it in ploop_destroy()
> before freeing anything.
>
> https://virtuozzo.atlassian.net/browse/VSTOR-148171
> 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/20261117171743.3216794-1-alexander.ivanov@virtuozzo.com?part=7
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [Devel] [PATCH hci-8.1 v7 07/10] dm-ploop: fix use-after-free on the last inflight_md decrement
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 07/10] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
2026-11-17 17:32 ` no-reply
@ 2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: Konstantin Khorenko @ 2026-11-17 18:23 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v8 with the changes below.
On Tue, 17 Nov 2026 18:17:41 +0100, Alexander Ivanov wrote:
> +/*
> + * Waiters do not act on zero inflight_md without holding inflight_md_wq.lock,
> + * so the ploop stays valid while we hold it. Once we drop it, the ploop may
> + * be freed, so this must be the last access to it.
> + */
Item 5 of the cover letter reply: the wait added to ploop_postsuspend()
in 9/10 acts on zero without the lock. It is safe because
ploop_destroy(), the only place that frees the ploop, takes the lock;
the comment should say that. Item 8: Fixes: d1a1e5108aeb.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread
* [Devel] [PATCH hci-8.1 v7 08/10] dm-ploop: fix flip_upper_deltas racing with resume
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (6 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 07/10] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:28 ` no-reply
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 09/10] dm-ploop: reload metadata on resume Alexander Ivanov
` (2 subsequent siblings)
10 siblings, 1 reply; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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
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] 27+ messages in thread* [Devel] [PATCH hci-8.1 v7 09/10] dm-ploop: reload metadata on resume
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (7 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 08/10] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:36 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 10/10] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
2026-11-17 18:23 ` [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back " Konstantin Khorenko
10 siblings, 2 replies; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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.
If metadata is dirty after suspend, keep it, as md pages may still be
referenced. Data bios complete before BAT writeback, so wait for
inflight_md in postsuspend.
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. Make the wait for pending preallocation in
postsuspend killable, as the allocator may hang on a dead file, and
skip it in standby, so that dm_replace() of libploop can suspend the
device to recover it. 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.
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 | 146 ++++++++++++++++++++++++++++++++++-
drivers/md/dm-ploop-cmd.c | 1 +
drivers/md/dm-ploop-map.c | 7 ++
drivers/md/dm-ploop-target.c | 88 ++++++++++++++-------
drivers/md/dm-ploop.h | 12 ++-
5 files changed, 225 insertions(+), 29 deletions(-)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 5a89771a2e02..c060afdd8d3e 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,146 @@ 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. Dirty metadata
+ * must not appear after suspend, as all IO and BAT writeback are
+ * completed (see ploop_postsuspend()), but we can't free md pages,
+ * which are still in use, in any case.
+ */
+ if (WARN_ON_ONCE(!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 fedf13969036..ee0c3e5fad86 100644
--- a/drivers/md/dm-ploop-cmd.c
+++ b/drivers/md/dm-ploop-cmd.c
@@ -1089,6 +1089,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 442537340b51..e7571b383f00 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -1267,6 +1267,12 @@ int ploop_no_pending_prealloc(struct ploop *ploop)
return ret;
}
+void ploop_wait_prealloc(struct ploop *ploop)
+{
+ wait_event_killable(ploop->dispatcher_wq_prealloc,
+ ploop_no_pending_prealloc(ploop));
+}
+
static int ploop_allocate_cluster(struct ploop *ploop, u32 *dst_clu, struct file *file)
{
struct ploop_delta *top = ploop_top_delta(ploop);
@@ -1443,6 +1449,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..968a608c67f4 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,18 +755,47 @@ 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. Don't wait in standby
+ * mode: the allocator may be stuck on the dead file, while
+ * dm_replace() of libploop must be able to suspend the device
+ * to recover it. The wait is killable for the same reason.
+ * If the wait is skipped or killed, a ploop of a reloaded
+ * table may see a stale size, and a late truncate of our
+ * allocator may shrink the file under it.
+ */
+ if (!ploop_is_standby(ploop))
+ ploop_wait_prealloc(ploop);
+ /*
+ * Data bios are completed before BAT writeback is finished
+ * (see ploop_put_piwb()), so wait for it explicitly to make
+ * md pages clean for ploop_reload_metadata().
+ */
+ 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;
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
@@ -776,8 +805,15 @@ static int ploop_preresume(struct dm_target *ti)
ploop->suspended = 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..97841e13568b 100644
--- a/drivers/md/dm-ploop.h
+++ b/drivers/md/dm-ploop.h
@@ -246,6 +246,13 @@ 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;
/* Device is suspended */
bool suspended;
/* Device wants suspend */
@@ -634,7 +641,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,
@@ -652,6 +661,7 @@ extern void ploop_disable_writeback_delay(struct ploop *ploop);
extern void ploop_enable_writeback_delay(struct ploop *ploop);
extern void ploop_should_prealloc(struct ploop *ploop);
+extern void ploop_wait_prealloc(struct ploop *ploop);
extern int ploop_no_pending_prealloc(struct ploop *ploop);
extern void ploop_resubmit_enospc_pios(struct ploop *ploop);
--
2.53.0
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 09/10] dm-ploop: reload metadata on resume
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 09/10] dm-ploop: reload metadata on resume Alexander Ivanov
@ 2026-11-17 17:36 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: no-reply @ 2026-11-17 17:36 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: killed prealloc wait in postsuspend() leaves old
allocator unfenced
- [Medium] dm-ploop: postsuspend waits uninterruptibly on inflight_md
even in standby
- [Low] dm-ploop: sticky ploop->standby flag disables reload and
prealloc on later resumes
--
commit 351119f1dd440a802da0e2263d32e93b2a12c2df
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
dm-ploop: reload metadata on resume
This patch makes ploop_preresume() reload the top delta header, the BAT of every delta and the delta file sizes. It also rebuilds the BAT tree and holes_bitmap, so a ploop created by "dmsetup reload" does not resume with stale metadata. It moves preallocation from ploop_ctr() to after the reload, makes the postsuspend wait for preallocation killable, and tracks standby per ploop instance.
> diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
> index 442537340b51..e7571b383f00 100644
> --- a/drivers/md/dm-ploop-map.c
> +++ b/drivers/md/dm-ploop-map.c
> @@ -1267,6 +1267,12 @@ int ploop_no_pending_prealloc(struct ploop *ploop)
> return ret;
> }
>
> +void ploop_wait_prealloc(struct ploop *ploop)
> +{
> + wait_event_killable(ploop->dispatcher_wq_prealloc,
> + ploop_no_pending_prealloc(ploop));
> +}
> +
[Severity: Medium]
Does dropping the return value of wait_event_killable() leave the old allocator unfenced?
If dmsetup gets a fatal signal while a preallocation is pending, the wait returns -ERESTARTSYS. ploop_postsuspend() returns void, so it carries on and marks the device suspended while the allocator may still be running.
ploop_preallocate_cluster() computes new_len = top->file_size + prealloc_in_progress and calls do_ftruncate() or vfs_fallocate() with no suspend check. It can then resize the top delta under a new ploop that has already reloaded the size. ploop_preresume() of the new ploop only checks its own prealloc state, which is zero.
That can corrupt the top delta image. Could the killed wait be reported to the caller, or the old allocator be stopped from touching the file once suspended?
> static int ploop_allocate_cluster(struct ploop *ploop, u32 *dst_clu, struct file *file)
> {
> struct ploop_delta *top = ploop_top_delta(ploop);
> @@ -1443,6 +1449,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");
[Severity: Low]
Is it intended that ploop->standby is never cleared?
This is the only writer. The queue bit it shadows is cleared by ploop_ctr() of a new table. Suppose an instance enters standby, a new table's ploop_ctr() clears the queue bit, and the new table is then abandoned. The old instance serves IO again, but ploop_preresume() keeps skipping ploop_reload_metadata() and ploop_should_prealloc() for it, and ploop_postsuspend() keeps skipping the prealloc wait.
If userspace then changes the images on the suspended device (merge or snapshot trim) and resumes this same instance, it keeps a stale BAT. That is the corruption this patch is meant to prevent. Normal recovery through dm_replace() creates a new instance, so it is not affected. Should the flag be cleared when the instance leaves standby?
[ ... ]
> diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
> index bfadca7f12ae..968a608c67f4 100644
> --- a/drivers/md/dm-ploop-target.c
> +++ b/drivers/md/dm-ploop-target.c
[ ... ]
> @@ -755,18 +755,47 @@ 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. Don't wait in standby
> + * mode: the allocator may be stuck on the dead file, while
> + * dm_replace() of libploop must be able to suspend the device
> + * to recover it. The wait is killable for the same reason.
> + * If the wait is skipped or killed, a ploop of a reloaded
> + * table may see a stale size, and a late truncate of our
> + * allocator may shrink the file under it.
> + */
> + if (!ploop_is_standby(ploop))
> + ploop_wait_prealloc(ploop);
> + /*
> + * Data bios are completed before BAT writeback is finished
> + * (see ploop_put_piwb()), so wait for it explicitly to make
> + * md pages clean for ploop_reload_metadata().
> + */
> + wait_event(ploop->inflight_md_wq, !atomic_read(&ploop->inflight_md));
> ploop_set_suspended(ti, true);
> }
[Severity: Medium]
Can this wait_event() hang suspend in standby?
The comment above explains that the prealloc wait is skipped in standby and made killable because the backing file may be dead. The inflight_md wait right below has neither a standby bypass nor a killable variant. A BAT index write stuck on a hung delta file keeps inflight_md non-zero, so ploop_postsuspend() sits in D state and the dm_replace() recovery stalls.
ploop_presuspend() does not drain BAT writes. The kt_worker->inflight_pios counter it waits on drops once the pio is handed to a runner through ploop_runners_add_work() in ploop_index_wb_submit(), not when the write completes.
Skipping the wait in standby looks safe, since ploop_preresume() skips the reload there. The final patch of the series, "dm-ploop: truncate unused tail of top delta on suspend", keeps this wait and moves it under ctl_rwsem held for write, so a hang would then also block ploop_message() and ploop_preresume().
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261117171743.3216794-1-alexander.ivanov@virtuozzo.com?part=9
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 09/10] dm-ploop: reload metadata on resume
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 09/10] dm-ploop: reload metadata on resume Alexander Ivanov
2026-11-17 17:36 ` no-reply
@ 2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: Konstantin Khorenko @ 2026-11-17 18:23 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v8 with the changes below.
On Tue, 17 Nov 2026 18:17:43 +0100, Alexander Ivanov wrote:
> +void ploop_wait_prealloc(struct ploop *ploop)
> +{
> + wait_event_killable(ploop->dispatcher_wq_prealloc,
> + ploop_no_pending_prealloc(ploop));
> +}
Items 2-4 of the cover letter reply. Killing this wait lets the old
allocator truncate the file to a stale size after the next table has
grown it; the comment in ploop_postsuspend() admits it, but that loses
data on the next load instead of hanging. Also wait for inflight_md in
ploop_preresume(), as postsuspend does.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread
* [Devel] [PATCH hci-8.1 v7 10/10] dm-ploop: truncate unused tail of top delta on suspend
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (8 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 09/10] dm-ploop: reload metadata on resume Alexander Ivanov
@ 2026-11-17 17:17 ` Alexander Ivanov
2026-11-17 17:40 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
2026-11-17 18:23 ` [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back " Konstantin Khorenko
10 siblings, 2 replies; 27+ messages in thread
From: Alexander Ivanov @ 2026-11-17 17:17 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,
which suspends it again. 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. 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.
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 | 79 ++++++++++++++++++++++++++++++------
drivers/md/dm-ploop.h | 3 +-
3 files changed, 125 insertions(+), 13 deletions(-)
diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index c060afdd8d3e..7bab746a3707 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -709,6 +709,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 (WARN_ON_ONCE(!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 968a608c67f4..aa71f22352aa 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,10 +744,66 @@ 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;
+ down_write(&ploop->ctl_rwsem);
/*
* Let the next resumed ploop (maybe of reloaded table)
* see final size of the top delta. Don't wait in standby
@@ -770,10 +819,16 @@ static void ploop_postsuspend(struct dm_target *ti)
/*
* Data bios are completed before BAT writeback is finished
* (see ploop_put_piwb()), so wait for it explicitly to make
- * md pages clean for ploop_reload_metadata().
+ * md pages clean for ploop_reload_metadata() and
+ * ploop_calc_used_end().
*/
wait_event(ploop->inflight_md_wq, !atomic_read(&ploop->inflight_md));
- ploop_set_suspended(ti, true);
+ /* 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 97841e13568b..55bc0ade6db5 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)
@@ -644,6 +644,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] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 10/10] dm-ploop: truncate unused tail of top delta on suspend
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 10/10] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
@ 2026-11-17 17:40 ` no-reply
2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: no-reply @ 2026-11-17 17:40 UTC (permalink / raw)
To: Alexander Ivanov, andrey.zhadchenko; +Cc: devel
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] dm-ploop: ploop_calc_used_end() reads the whole BAT on every
suspend
- [Low] dm-ploop: ploop_postsuspend() holds ctl_rwsem across unbounded
waits
--
commit 1bc9305c23ed033ac0c00538c0091d4e15947faa
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>
dm-ploop: truncate unused tail of top delta on suspend
This commit adds ploop_calc_used_end(), which computes the used end of the top delta from the on-disk BAT and format extension header. ploop_postsuspend() now uses it to truncate the unused tail of the top delta under ctl_rwsem.
> 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
[ ... ]
> + ret = ploop_read_delta_metadata(ploop, file, &md_root, &nr_be);
> + if (ret)
> + return ret;
[Severity: Low]
Does this read the whole on-disk BAT on every suspend of a writable, non-raw device? ploop_read_delta_metadata() allocates md pages for the entire BAT, reads and converts it, and frees it again. This happens even when there is no unused tail, because the early checks in ploop_truncate_unused_tail() cannot detect that case. Suspend latency therefore grows with BAT size, and resume then reloads the metadata a second time. Could a cheap pre-check skip the read when nothing can be reclaimed? For example, compare top->file_size with the used end computed from the in-memory state.
> 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 void ploop_postsuspend(struct dm_target *ti)
> {
> struct ploop *ploop = ti->private;
> + unsigned int noio_flags;
>
> + down_write(&ploop->ctl_rwsem);
> /*
> * Let the next resumed ploop (maybe of reloaded table)
> * see final size of the top delta. Don't wait in standby
[Severity: Low]
As reported by no-reply@sashiko.virtuozzo.com on v4 [1], is it necessary to take ctl_rwsem for write before the waits in ploop_postsuspend()? The write lock is now held across ploop_wait_prealloc(), the uninterruptible wait_event() on inflight_md_wq, and the fsync and truncate I/O.
If any of these stalls, for example a stuck backing file with unfinished BAT writeback, ploop_status() and every ploop_message() handler except get_event sleep in down_read() or down_write() on ctl_rwsem. Messages such as set_noresume then cannot run during the stall. Before the patch, ctl_rwsem was taken only briefly in ploop_set_suspended(), after both waits.
The truncate path already re-checks ploop_no_pending_prealloc() and the i_size equality. A message slipping in between the waits and the lock would at worst make the truncation skip. Would taking ctl_rwsem after the waits avoid the extra blocking?
[1] https://lore.kernel.org/r/010701a1305e757d-fdba1b14-3cb2-40e0-98a8-66720e942d61-000000@eu-central-1.amazonses.com
--
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261117171743.3216794-1-alexander.ivanov@virtuozzo.com?part=10
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread* Re: [Devel] [PATCH hci-8.1 v7 10/10] dm-ploop: truncate unused tail of top delta on suspend
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 10/10] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
2026-11-17 17:40 ` no-reply
@ 2026-11-17 18:23 ` Konstantin Khorenko
1 sibling, 0 replies; 27+ messages in thread
From: Konstantin Khorenko @ 2026-11-17 18:23 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v8 with the changes below.
On Tue, 17 Nov 2026 18:17:43 +0100, Alexander Ivanov wrote:
> 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.
Item 7 of the cover letter reply: another ploop that preallocated
before our last reload does not change the size, so the check does
not see it; say the check is best effort. Item 4: the "only place to
change file_size" comment.
--
Konstantin Khorenko,
Virtuozzo Linux Kernel Team
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel
^ permalink raw reply [flat|nested] 27+ messages in thread
* Re: [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend
2026-11-17 17:17 [Devel] [PATCH hci-8.1 v7 00/10] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
` (9 preceding siblings ...)
2026-11-17 17:17 ` [Devel] [PATCH hci-8.1 v7 10/10] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
@ 2026-11-17 18:23 ` Konstantin Khorenko
10 siblings, 0 replies; 27+ messages in thread
From: Konstantin Khorenko @ 2026-11-17 18:23 UTC (permalink / raw)
To: Alexander Ivanov; +Cc: devel
I won't apply this version, please send v8 with the changes below.
1. Cover letter: it is the b4 placeholder. Say why the series is
needed, which patches are fixes and which build the feature, the
userspace part it depends on and the deploy order, and what
changed since v6.
2. 9/10: a killed or skipped wait for preallocation in postsuspend
lets the old allocator shrink the top delta under the next table.
Make the allocator unable to shrink the file, or keep the wait
uninterruptible outside standby; name a5836033b8fe9 in the
message.
3. 9/10: wait for inflight_md in ploop_preresume() too: a resize on
the suspended device can leave it non-zero, and the WARN_ON_ONCE()
in ploop_reload_metadata() fires.
4. 9/10, 10/10: the comment "Here must be the only place to change
file_size" in ploop_preallocate_cluster() is not true any more.
5. 7/10: the comment of ploop_dec_inflight_md() says waiters take the
lock before acting on zero; the postsuspend wait of 9/10 does not.
Say that ploop_destroy() takes the lock, which is what keeps it
safe.
6. 6/10: fold it into 7/10, which replaces both lines it changes.
7. 10/10 message: the file size check is best effort, and removal of
a live device runs the suspend hooks from __dm_destroy() only
because an open device cannot be removed; say both.
8. Fixes: 90bb85536272 ("dm-ploop: Add ploop target driver") on 1/10,
2/10, 3/10 and 8/10; Fixes: d1a1e5108aeb ("dm-ploop: add inflight
metadata writeback counter") on 7/10. In 3/10 cite 90bb85536272
too: 6ddad5a4d226 is not in this branch.
Separate patches, not needed for this series. I created a task:
1. ploop_add_delta() stores the file in deltas[level].file before it
clones it; if kcalloc() or file_clone_open() fails, the file is
put twice, at err_fput and in ploop_destroy().
https://virtuozzo.atlassian.net/browse/VSTOR-155723
On Tue, 17 Nov 2026 18:17:34 +0100, Alexander Ivanov wrote:
> *** BLURB HERE ***
2. The table reload case: the suspending process is killed while
postsuspend waits for the allocator of the old table. The new table
reloads metadata and preallocates. The old allocator then calls
do_ftruncate() with the new_len it computed from its stale file_size,
and ploop_truncate_prealloc_safe() compares new_len only with that
stale size, so the file shrinks and the clusters the new table has
written are cut. The next load fails with -EPROTO. a5836033b8fe9 made
this wait uninterruptible for that reason.
3. ploop_put_piwb() calls complete() before ploop_dec_inflight_md().
A "resize" message on the suspended device returns as soon as it is
woken; a resume right after it sees inflight_md == 1, the WARN fires,
which panics hosts with panic_on_warn, and the reload is skipped.
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] 27+ messages in thread