OpenVZ / Virtuozzo kernel development (devel@openvz.org)
 help / color / mirror / Atom feed
* [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend
@ 2026-11-19 15:15 Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 1/9] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
                   ` (9 more replies)
  0 siblings, 10 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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 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-2 fix header validation and patch 3 fixes raw deltas, 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 are merged:
https://bitbucket.org/openvz/ploop.ovz/pull-requests/486
They drop the manual trimming of the image tail. The two can be
deployed in either order: old userspace trims the tail of the suspended
device itself, and the reload on resume picks up the new size; new
userspace on a kernel without this series just leaves the tail in
place.

The series is based on two patches of Denis V. Lunev, which go in
before it:
  dm-ploop: fix a double fput of the delta file when ploop_add_delta() fails
  dm-ploop: allocate the mtfile array as pointers, not struct files

https://virtuozzo.atlassian.net/browse/VSTOR-148171

v10:
- Rebase on the two patches above.
- Patch 4: keep storing the file in deltas[level] after the clones are
  made, as "dm-ploop: fix a double fput of the delta file when
  ploop_add_delta() fails" does.
- Patch 8:
  - drop pending prealloc requests once more under ctl_rwsem when
    setting ->suspended in postsuspend, and wait after releasing it
    unless in standby: a message that took the lock first saw the
    device not suspended and left its requests;
  - wait for the prealloc in flight in preresume instead of skipping
    the reload while preallocation is pending;
  - warn when the reload is skipped on dirty md pages;
  - drop ploop_set_suspended() here instead of in patch 9;
  - drop the misplaced comment of ploop_drop_prealloc() and fix the
    comment in ploop_flip_upper_deltas(), which named one case only.
- Patch 9: split the long paragraph of the message; say that status
  waits for the truncation I/O under ctl_rwsem.

v9:
- Patch 6: say that the replaced code calls wake_up_interruptible()
  and that wake_up_locked() wakes the uninterruptible wait in
  ploop_destroy().
- Patch 7: shorten the comment in ploop_preresume().
- Patch 8:
  - drop prealloc_stopped, which failed a resize or merge of the
    suspended device with -EIO; instead drop pending prealloc
    requests on postsuspend and at the end of a message to the
    suspended device, which waits for the one in flight after
    releasing ctl_rwsem;
  - wait for inflight_md in preresume once more under ctl_rwsem, so
    that a message slipped in before the lock doesn't make the reload
    skip silently;
  - fix the stale comment in ploop_flip_upper_deltas();
  - split the long paragraphs of the message.
- Patch 9: skip the truncation on dirty md pages without logging an
  error; move the two __dm_destroy() sentences of the message to a
  paragraph of their own.

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_suspend_submitting_pios() 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    | 252 +++++++++++++++++++++++++++++++++--
 drivers/md/dm-ploop-cmd.c    |  11 +-
 drivers/md/dm-ploop-map.c    |  51 ++++++-
 drivers/md/dm-ploop-target.c | 184 ++++++++++++++++++++-----
 drivers/md/dm-ploop.h        |  18 ++-
 5 files changed, 457 insertions(+), 59 deletions(-)

-- 
2.53.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 1/9] dm-ploop: reject a delta header whose BAT does not cover the target
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 2/9] dm-ploop: reject empty deltas Alexander Ivanov
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@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 3a44f7480ff2..f14f021fca1c 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] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 2/9] dm-ploop: reject empty deltas
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 1/9] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 3/9] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@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 f14f021fca1c..bf39caf8e950 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] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 3/9] dm-ploop: set delta is_raw before applying its mappings
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 1/9] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 2/9] dm-ploop: reject empty deltas Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 4/9] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
 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 bf39caf8e950..e94ec70e9803 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].mtfile = kcalloc(ploop->nkt_runners,
@@ -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] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 4/9] dm-ploop: factor out ploop_read_delta() from ploop_add_delta()
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
                   ` (2 preceding siblings ...)
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 3/9] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 5/9] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
 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 e94ec70e9803..138801914b61 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].mtfile = kcalloc(ploop->nkt_runners,
 				       sizeof(*deltas[level].mtfile), GFP_KERNEL);
 	if (!deltas[level].mtfile) {
@@ -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] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 5/9] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole()
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
                   ` (3 preceding siblings ...)
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 4/9] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@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] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
                   ` (4 preceding siblings ...)
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 5/9] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 7/9] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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_interruptible() 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 the wakeup and
ploop_free_piwb() are done with the ploop.

Free the piwb first, then decrement inflight_md and wake the waiters
with wake_up_locked() under inflight_md_wq.lock as the last access to
the ploop. Unlike wake_up_interruptible(), it wakes the uninterruptible
wait in ploop_destroy(). 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>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@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] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 7/9] dm-ploop: fix flip_upper_deltas racing with resume
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
                   ` (5 preceding siblings ...)
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 8/9] dm-ploop: reload metadata on resume Alexander Ivanov
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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>
Reviewed-by: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>
---
 drivers/md/dm-ploop-target.c | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/drivers/md/dm-ploop-target.c b/drivers/md/dm-ploop-target.c
index 3b5f37a58bb1..5cf561afb2e9 100644
--- a/drivers/md/dm-ploop-target.c
+++ b/drivers/md/dm-ploop-target.c
@@ -764,19 +764,16 @@ 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);
+	/* Singleton target: nothing after preresume can fail the resume */
+	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] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 8/9] dm-ploop: reload metadata on resume
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
                   ` (6 preceding siblings ...)
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 7/9] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:50   ` Konstantin Khorenko
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 9/9] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
  2026-11-19 15:50 ` [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back " Konstantin Khorenko
  9 siblings, 1 reply; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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 writes BAT synchronously,
but may drop its inflight_md reference after the message returns, so
wait in preresume too. Wait outside ctl_rwsem, so that a stuck
writeback doesn't block status and messages, and once more under it: a
message may slip in before the lock, and would make the reload skip
silently. 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. Wait for the prealloc in flight in preresume
as well, as the allocator changes the top delta size under bat_lock
only, and a message to the suspended device waits for it after
releasing ctl_rwsem. Skip the reload in standby, as the images may be
inaccessible.

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. Drop them once more under ctl_rwsem when setting
->suspended, and wait again: a message that took the lock first saw the
device not suspended and left its requests. Messages to the suspended
device (e.g. resize) still get their preallocation, as they wait for it;
requests left after a message are dropped the same way when it ends.
Both wait for the one in flight after releasing ctl_rwsem, so that a
stuck allocator doesn't block status and other messages.

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    | 145 ++++++++++++++++++++++++++++++++++-
 drivers/md/dm-ploop-cmd.c    |  11 ++-
 drivers/md/dm-ploop-map.c    |  26 ++++++-
 drivers/md/dm-ploop-target.c | 111 +++++++++++++++++++--------
 drivers/md/dm-ploop.h        |  14 +++-
 5 files changed, 265 insertions(+), 42 deletions(-)

diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index 138801914b61..eee2146901d1 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,145 @@ 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)) {
+		PL_WARN("md pages are dirty, metadata is not reloaded");
+		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_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..c89de44252e8 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;
 }
 
@@ -1193,7 +1194,7 @@ static int ploop_flip_upper_deltas(struct ploop *ploop)
 	if (ret)
 		return ret;
 
-	/* A resize of the suspended device may have restarted the allocator */
+	/* A message or postsuspend in standby may leave a prealloc in flight */
 	ret = wait_event_interruptible(ploop->dispatcher_wq_prealloc,
 				       ploop_no_pending_prealloc(ploop));
 	if (ret)
@@ -1231,7 +1232,7 @@ int ploop_message(struct dm_target *ti, unsigned int argc, char **argv,
 		  char *result, unsigned int maxlen)
 {
 	struct ploop *ploop = ti->private;
-	bool read, forward = true;
+	bool read, stop_prealloc, forward = true;
 	int ret = -EPERM;
 	u64 val;
 
@@ -1294,10 +1295,16 @@ int ploop_message(struct dm_target *ti, unsigned int argc, char **argv,
 	}
 
 unlock:
+	/* Don't leave prealloc of a message running under the next table */
+	stop_prealloc = !read && ploop->suspended;
+	if (stop_prealloc)
+		ploop_drop_prealloc(ploop);
 	if (read)
 		up_read(&ploop->ctl_rwsem);
 	else
 		up_write(&ploop->ctl_rwsem);
+	if (stop_prealloc)
+		ploop_wait_prealloc(ploop);
 out:
 	return ret;
 }
diff --git a/drivers/md/dm-ploop-map.c b/drivers/md/dm-ploop-map.c
index 2f85383c4682..4a8d4e5ab480 100644
--- a/drivers/md/dm-ploop-map.c
+++ b/drivers/md/dm-ploop-map.c
@@ -1212,9 +1212,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;
@@ -1266,6 +1263,28 @@ int ploop_no_pending_prealloc(struct ploop *ploop)
 	return ret;
 }
 
+void ploop_drop_prealloc(struct ploop *ploop)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&ploop->bat_lock, flags);
+	ploop->prealloc_size = 0;
+	spin_unlock_irqrestore(&ploop->bat_lock, flags);
+}
+
+void ploop_wait_prealloc(struct ploop *ploop)
+{
+	if (!READ_ONCE(ploop->standby))
+		wait_event(ploop->dispatcher_wq_prealloc,
+			   ploop_no_pending_prealloc(ploop));
+}
+
+void ploop_stop_prealloc(struct ploop *ploop)
+{
+	ploop_drop_prealloc(ploop);
+	ploop_wait_prealloc(ploop);
+}
+
 static int ploop_allocate_cluster(struct ploop *ploop, u32 *dst_clu, struct file *file)
 {
 	struct ploop_delta *top = ploop_top_delta(ploop);
@@ -1442,6 +1461,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 5cf561afb2e9..a11cefc0fd48 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:
@@ -720,13 +715,10 @@ 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)
+{
+	return READ_ONCE(ploop->standby);
 }
 
 static void ploop_presuspend(struct dm_target *ti)
@@ -755,25 +747,76 @@ static void ploop_postsuspend(struct dm_target *ti)
 {
 	struct ploop *ploop = ti->private;
 
-	wait_event(ploop->dispatcher_wq_prealloc,
-		   ploop_no_pending_prealloc(ploop));
-	ploop_set_suspended(ti, true);
+	/*
+	 * 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.
+	 */
+	ploop_stop_prealloc(ploop);
+	if (!ploop_is_standby(ploop))
+		wait_event(ploop->inflight_md_wq,
+			   !atomic_read(&ploop->inflight_md));
+	down_write(&ploop->ctl_rwsem);
+	ploop->suspended = true;
+	ploop_drop_prealloc(ploop);
+	up_write(&ploop->ctl_rwsem);
+	ploop_wait_prealloc(ploop);
 }
 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 drop inflight_md late */
+	if (!ploop_is_standby(ploop)) {
+		wait_event(ploop->inflight_md_wq,
+			   !atomic_read(&ploop->inflight_md));
+		ploop_wait_prealloc(ploop);
+	}
+
 	down_write(&ploop->ctl_rwsem);
-	if (ploop->noresume)
+	/* A message may have slipped in before the lock */
+	if (!ploop_is_standby(ploop)) {
+		wait_event(ploop->inflight_md_wq,
+			   !atomic_read(&ploop->inflight_md));
+		ploop_wait_prealloc(ploop);
+	}
+	if (ploop->noresume) {
 		ret = -EAGAIN;
+	} else if (!ploop_is_standby(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);
+	}
 	/* Singleton target: nothing after preresume can fail the resume */
-	if (ret == 0)
+	if (ret == 0) {
 		ploop->suspended = false;
+		ploop->top_flipped = 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..b00e3eef7b66 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,
@@ -653,6 +662,9 @@ extern void ploop_enable_writeback_delay(struct ploop *ploop);
 
 extern void ploop_should_prealloc(struct ploop *ploop);
 extern int ploop_no_pending_prealloc(struct ploop *ploop);
+extern void ploop_drop_prealloc(struct ploop *ploop);
+extern void ploop_wait_prealloc(struct ploop *ploop);
+extern void ploop_stop_prealloc(struct ploop *ploop);
 extern void ploop_resubmit_enospc_pios(struct ploop *ploop);
 
 #endif /* __DM_PLOOP_H */
-- 
2.53.0

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Devel] [PATCH hci-8.2 v10 9/9] dm-ploop: truncate unused tail of top delta on suspend
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
                   ` (7 preceding siblings ...)
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 8/9] dm-ploop: reload metadata on resume Alexander Ivanov
@ 2026-11-19 15:15 ` Alexander Ivanov
  2026-11-19 15:50 ` [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back " Konstantin Khorenko
  9 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-19 15:15 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.

Userspace writes format extension (CBT) behind the last data cluster of
suspended device on umount, resumes it, and then removes the device. 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.

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.

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. A message
slipping in between the waits and the lock can at worst make the
truncation skip, as pending prealloc and the conditions below are
checked under the lock. Like messages and the metadata reload in
ploop_preresume(), the truncation does delta I/O under the lock, so
status waits for it if the top delta stalls.

Skip read-only devices and raw top delta. Skip the truncation if md
pages are not clean, as on-disk BAT may miss allocated clusters then.
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    | 54 ++++++++++++++++++++++++++++-
 drivers/md/dm-ploop-target.c | 67 ++++++++++++++++++++++++++++++++++--
 drivers/md/dm-ploop.h        |  4 ++-
 3 files changed, 121 insertions(+), 4 deletions(-)

diff --git a/drivers/md/dm-ploop-bat.c b/drivers/md/dm-ploop-bat.c
index eee2146901d1..f2abe95e0086 100644
--- a/drivers/md/dm-ploop-bat.c
+++ b/drivers/md/dm-ploop-bat.c
@@ -569,7 +569,7 @@ 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)
+bool ploop_md_pages_are_clean(struct ploop *ploop)
 {
 	struct rb_node *node;
 	struct md_page *md;
@@ -708,6 +708,58 @@ 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;
+
+	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 a11cefc0fd48..fb4f2a108b59 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)
 {
@@ -743,9 +744,67 @@ 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;
+	/* BAT on disk may miss allocated clusters until writeback is done */
+	if (!ploop_md_pages_are_clean(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
@@ -763,6 +822,10 @@ static void ploop_postsuspend(struct dm_target *ti)
 		wait_event(ploop->inflight_md_wq,
 			   !atomic_read(&ploop->inflight_md));
 	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;
 	ploop_drop_prealloc(ploop);
 	up_write(&ploop->ctl_rwsem);
diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
index b00e3eef7b66..ccc0c713e078 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,8 @@ 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 bool ploop_md_pages_are_clean(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] 13+ messages in thread

* Re: [Devel] [PATCH hci-8.2 v10 8/9] dm-ploop: reload metadata on resume
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 8/9] dm-ploop: reload metadata on resume Alexander Ivanov
@ 2026-11-19 15:50   ` Konstantin Khorenko
  0 siblings, 0 replies; 13+ messages in thread
From: Konstantin Khorenko @ 2026-11-19 15:50 UTC (permalink / raw)
  To: Alexander Ivanov; +Cc: devel

I won't apply this version, please send v11, items 1 and 2 of the
cover letter reply.

On Thu, 19 Nov 2026 16:15:46 +0100, Alexander Ivanov wrote:
> +void ploop_drop_prealloc(struct ploop *ploop)
> +{
> +	unsigned long flags;
> +
> +	spin_lock_irqsave(&ploop->bat_lock, flags);
> +	ploop->prealloc_size = 0;
> +	spin_unlock_irqrestore(&ploop->bat_lock, flags);
> +}

ploop_req_prealloc() wakes only the allocator thread, and
ploop_allocator() calls wake_up() only after it took a non-zero
prealloc_size. A drop between the request and the allocator's look
leaves the waiter of that request asleep with its condition already
true. The first ploop_stop_prealloc() in postsuspend runs before
ctl_rwsem, so a resize of the live device can be that waiter, and
postsuspend then blocks on ctl_rwsem with md->suspend_lock held.

> a message may slip in before the lock, and would make the reload skip
> silently. Keep metadata that is still dirty on resume, as md pages may

v10 warns on that skip now.

> ->suspended, and wait again: a message that took the lock first saw the
> device not suspended and left its requests. Messages to the suspended

Five sentences in this paragraph; start a new one at "Drop them once
more". "Both wait" below reads as the two kinds of requests, name
postsuspend and messages.

Your answer to Sashiko on v9, that standby is set only on completion
of DM data and flush bios, does not hold: resize and merge pios and
BAT writeback complete through ploop_data_rw_complete() too, which
calls ploop_check_standby_mode(). The wait is no worse than before
the series, so no change is asked, just do not put that claim in a
message.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend
  2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
                   ` (8 preceding siblings ...)
  2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 9/9] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
@ 2026-11-19 15:50 ` Konstantin Khorenko
  2026-11-20  8:17   ` Alexander Ivanov
  9 siblings, 1 reply; 13+ messages in thread
From: Konstantin Khorenko @ 2026-11-19 15:50 UTC (permalink / raw)
  To: Alexander Ivanov; +Cc: devel

I won't apply this version, please send v11 with the changes below.

All four v9 items are done, thanks, and so is Sashiko's v9 Medium:
preresume now waits for the prealloc and always reloads outside
standby. 3/9, 4/9 and 9/9 are fine as they are. What is left, all in
8/9:

1. ploop_drop_prealloc() clears prealloc_size without a wake_up() of
   dispatcher_wq_prealloc. A waiter in ploop_allocate_cluster() or in
   a resize sleeps until the allocator wakes it, and the allocator
   wakes only after it took a non-zero request. If the drop comes
   first, the waiter's condition is true but nobody wakes it: the
   resize, or a runner doing a merge COW, sleeps forever, and
   postsuspend then hangs in down_write(&ctl_rwsem) under the
   suspend lock. Wake the queue in ploop_drop_prealloc() when it
   cleared a request.
2. Commit message: "would make the reload skip silently" is stale, v10
   warns on that skip. The "Drop the pending requests on postsuspend"
   paragraph is five sentences after "Drop them once more ..." was
   spliced in; split it there, and say who "Both" are ("Postsuspend
   and messages wait ...").

On Thu, 19 Nov 2026 16:15:38 +0100, Alexander Ivanov wrote:
> v10:
> - Rebase on the two patches above.

Checked: 4/9 keeps "deltas[level].file = file;" after the clone
loop and no error path brings the double fput back.

-- 
Konstantin Khorenko,
Virtuozzo Linux Kernel Team

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend
  2026-11-19 15:50 ` [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back " Konstantin Khorenko
@ 2026-11-20  8:17   ` Alexander Ivanov
  0 siblings, 0 replies; 13+ messages in thread
From: Alexander Ivanov @ 2026-11-20  8:17 UTC (permalink / raw)
  To: Konstantin Khorenko; +Cc: devel

On 11/19/26 16:50, Konstantin Khorenko wrote:
> I won't apply this version, please send v11 with the changes below.
>
> All four v9 items are done, thanks, and so is Sashiko's v9 Medium:
> preresume now waits for the prealloc and always reloads outside
> standby. 3/9, 4/9 and 9/9 are fine as they are. What is left, all in
> 8/9:
>
> 1. ploop_drop_prealloc() clears prealloc_size without a wake_up() of
>     dispatcher_wq_prealloc. A waiter in ploop_allocate_cluster() or in
>     a resize sleeps until the allocator wakes it, and the allocator
>     wakes only after it took a non-zero request. If the drop comes
>     first, the waiter's condition is true but nobody wakes it: the
>     resize, or a runner doing a merge COW, sleeps forever, and
>     postsuspend then hangs in down_write(&ctl_rwsem) under the
>     suspend lock. Wake the queue in ploop_drop_prealloc() when it
>     cleared a request.
Fixed.
> 2. Commit message: "would make the reload skip silently" is stale, v10
>     warns on that skip. The "Drop the pending requests on postsuspend"
>     paragraph is five sentences after "Drop them once more ..." was
>     spliced in; split it there, and say who "Both" are ("Postsuspend
>     and messages wait ...").
Reworded the commit message.
>
> On Thu, 19 Nov 2026 16:15:38 +0100, Alexander Ivanov wrote:
>> v10:
>> - Rebase on the two patches above.
> Checked: 4/9 keeps "deltas[level].file = file;" after the clone
> loop and no error path brings the double fput back.
>
_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2026-11-20  8:19 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 15:15 [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 1/9] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 2/9] dm-ploop: reject empty deltas Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 3/9] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 4/9] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 5/9] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 6/9] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 7/9] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 8/9] dm-ploop: reload metadata on resume Alexander Ivanov
2026-11-19 15:50   ` Konstantin Khorenko
2026-11-19 15:15 ` [Devel] [PATCH hci-8.2 v10 9/9] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
2026-11-19 15:50 ` [Devel] [PATCH hci-8.2 v10 0/9] dm-ploop: give back " Konstantin Khorenko
2026-11-20  8:17   ` Alexander Ivanov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox