All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: no-reply@sashiko.virtuozzo.com
To: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>,
	alexander.ivanov@virtuozzo.com, khorenko@virtuozzo.com
Cc: devel@openvz.org
Subject: Re: [Devel] [PATCH hci-8.1 v2 5/7] dm-qcow2: implement per-qio fast processing
Date: Tue, 10 Nov 2026 12:08:13 +0000	[thread overview]
Message-ID: <010701a1c55bdb00-27a7dfe3-8768-4a91-8714-a8a6348f342f-000000@eu-central-1.amazonses.com> (raw)
In-Reply-To: <20261110114629.416695-6-andrey.zhadchenko@virtuozzo.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] dm-qcow2: stale data_clu_pos in fast path can write to a freed or reused cluster
--

Commit 34396f8c5df05ca113c51e62c4510b07f74a9fdc
Author: Andrey Zhadchenko <andrey.zhadchenko@virtuozzo.com>

dm-qcow2: implement per-qio fast processing

This patch makes do_qio_work() try to process each split qio immediately
with process_one_qio(..., true). Qios that need metadata changes go onto
the deferred list for do_qcow2_work(). As a result, parse and submit now
run in parallel with the main kwork.

> diff --git a/drivers/md/dm-qcow2-map.c b/drivers/md/dm-qcow2-map.c
> --- a/drivers/md/dm-qcow2-map.c
> +++ b/drivers/md/dm-qcow2-map.c

[ ... ]

> +static void qio_try_fast(struct qio *qio, struct list_head *deferred_qios)
> +{
> +	list_del(&qio->link);
> +
> +	if (process_one_qio(qio->qcow2, qio, true))
> +		list_add_tail(&qio->link, deferred_qios);
> +}

[Severity: High]
Can a write submitted from here go to a cluster that has been freed or
reused?

process_one_qio() runs handle_metadata() -> parse_metadata(), which
drops md_pages_lock and caches the result:

    unlock:
        spin_unlock_irq(&qcow2->md_pages_lock);
        if (ret <= 0) /* Err, delayed, DATA is not allocated, or zero read */
            return ret;

        map->data_clu_pos = pos;

The L2 entry is not re-validated after this point. Before this patch,
parse and submit ran sequentially in main kwork, so main kwork could not
change the index between the two steps.

The recheck added for the race with writeback in perform_rw_mapped()
looks only at MD_WRITEBACK, and at MD_DIRTY with the changed bit set:

    spin_lock_irqsave(&qcow2->md_pages_lock, flags);
    if (__delay_if_writeback(qcow2, md, index_in_page, &qio, false)) {
        ...
    }
    if ((md->status & MD_DIRTY) &&
        test_bit(index_in_page, md->wbd->changed_indexes)) {
        md->wbd->nr_submitted++;
        qio->data = md->wbd;
    }
    spin_unlock_irqrestore(&qcow2->md_pages_lock, flags);
    ...
    map_and_submit_rw(qcow2, map->data_clu_pos, qio);

It does not call delay_if_locked() and does not re-read the L2 entry. If
a discard's L2 update is DIRTY, the write is only attached to wbd and
still goes to the old data_clu_pos. If the discard's writeback has
already completed, the write goes to the stale position with no check.

On the main kwork side, the discard sequence is
prepare_cluster_discard() -> process_indexes_write() ->
process_indexes_end(). The last step waits only for wpc readers and then
calls dec_cluster_usage(), which frees the cluster for reuse. It does not
wait for fast-path writes that parsed the old mapping before the index
was locked.

A possible sequence:

fast worker: parse_metadata() caches data_clu_pos
  (worker is delayed, for example by preemption)
main kwork: discard locks the index, rewrites the L2 entry, completes
  writeback, and process_indexes_end() frees the cluster
fast worker: perform_rw_mapped() -> map_and_submit_rw() writes to the
  old data_clu_pos, which may now belong to other data

Is there an inflight ref, an index-lock recheck, or a wait for in-flight
writes that closes this window? I could not find one. It also looks like
the later patches in the series do not close it: one only batches the
do_qio_work() calls per CPU, and the other only sets DM_TARGET_NOWAIT.

The commit message says "a write may reach perform_rw_mapped() after
main kwork switched its L2 page from DIRTY to WRITEBACK. perform_rw_mapped()
parks such a write until the writeback ends." Is this the only race
introduced by running process_one_qio() in parallel with
do_qcow2_work()? The discard and cluster reuse race described above is
not mentioned.

> +
>  static void do_qio_work(struct work_struct *ws)
>  {

[ ... ]

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261110114629.416695-1-andrey.zhadchenko@virtuozzo.com?part=5

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

  reply	other threads:[~2026-11-10 12:09 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-10 11:46 [Devel] [PATCH hci-8.1 v2 0/7] dm-qcow2: per-qio fast path Andrey Zhadchenko
2026-11-10 11:46 ` [Devel] [PATCH hci-8.1 v2 1/7] dm-qcow2: repeat md page lookup if it was inserted in parallel Andrey Zhadchenko
2026-11-10 11:55   ` no-reply
2026-11-10 17:56   ` Konstantin Khorenko
2026-11-10 11:46 ` [Devel] [PATCH hci-8.1 v2 2/7] dm-qcow2: wait for L2 writeback in perform_rw_mapped() Andrey Zhadchenko
2026-11-10 11:56   ` no-reply
2026-11-10 17:56   ` Konstantin Khorenko
2026-11-10 11:46 ` [Devel] [PATCH hci-8.1 v2 3/7] dm-qcow2: prepare process_one_qio() for fastpath Andrey Zhadchenko
2026-11-10 11:59   ` no-reply
2026-11-10 13:50     ` Andrey Zhadchenko
2026-11-10 17:56   ` Konstantin Khorenko
2026-11-11 14:36   ` Alexander Ivanov
2026-11-10 11:46 ` [Devel] [PATCH hci-8.1 v2 4/7] dm-qcow2: add per-qio work infrastructure Andrey Zhadchenko
2026-11-10 12:02   ` no-reply
2026-11-11 15:34     ` Andrey Zhadchenko
2026-11-10 17:56   ` Konstantin Khorenko
2026-11-10 11:46 ` [Devel] [PATCH hci-8.1 v2 5/7] dm-qcow2: implement per-qio fast processing Andrey Zhadchenko
2026-11-10 12:08   ` no-reply [this message]
2026-11-10 17:56   ` Konstantin Khorenko
2026-11-10 11:46 ` [Devel] [PATCH hci-8.1 v2 6/7] dm-qcow2: process fast path qios in per-cpu batches on a workqueue Andrey Zhadchenko
2026-11-10 12:06   ` no-reply
2026-11-10 17:56   ` Konstantin Khorenko
2026-11-11 15:00   ` Alexander Ivanov
2026-11-10 11:46 ` [Devel] [PATCH hci-8.1 v2 7/7] dm-qcow2: declare DM_TARGET_NOWAIT Andrey Zhadchenko
2026-11-10 12:06   ` no-reply
2026-11-10 17:56 ` [Devel] [PATCH hci-8.1 v2 0/7] dm-qcow2: per-qio fast path Konstantin Khorenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=010701a1c55bdb00-27a7dfe3-8768-4a91-8714-a8a6348f342f-000000@eu-central-1.amazonses.com \
    --to=no-reply@sashiko.virtuozzo.com \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=andrey.zhadchenko@virtuozzo.com \
    --cc=devel@openvz.org \
    --cc=khorenko@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.