From: sashiko-bot@kernel.org
To: "Joonho Lee" <mynameisjune111@gmail.com>, linux-sound@vger.kernel.org
Cc: virtualization@lists.linux.dev,
"Michael S. Tsirkin" <mst@redhat.com>,
"Eugenio Perez" <eperezma@redhat.com>
Subject: Re: [PATCH v7 07/16] ALSA: virtio: stage VZ capture and restart paused streams
Date: Wed, 28 Oct 2026 10:14:53 +0000 [thread overview]
Message-ID: <sashiko-outbox-187737@kernel.org> (raw)
In-Reply-To: <20261028094132.20364-8-mynameisjune111@gmail.com>
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] ALSA: virtio: DMA cacheline sharing in virtsnd_pcm_capture_send()
- [Low] ALSA: virtio: 64-bit division and modulo on u64 capture counters break 32-bit builds
--
commit b45632282ebc50eaee6059eb5173344a95c32ede
Author: Joonho Lee <mynameisjune111@gmail.com>
ALSA: virtio: stage VZ capture and restart paused streams
This patch adds opt-in capture staging using request-owned buffers with
response validation and ring accounting. It also introduces a pause
release and reprepare state machine for staged capture streams.
> diff --git a/sound/virtio/virtio_pcm.c b/sound/virtio/virtio_pcm.c
> index be3893de40a55..a3478fc8848db 100644
> --- a/sound/virtio/virtio_pcm.c
> +++ b/sound/virtio/virtio_pcm.c
> @@ -310,8 +310,94 @@ static void virtsnd_pcm_period_elapsed(struct work_struct *work)
> {
[ ... ]
> + period_bytes = snd_pcm_lib_period_bytes(substream);
> + if (vss->xfer_xrun) {
> + notify = true;
> + } else if (!period_bytes || period_bytes >= vss->buffer_bytes ||
> + !virtsnd_pcm_capture_counters_valid(vss) ||
> + vss->capture_notified_periods >
> + vss->capture_copied_total / period_bytes) {
> + vss->xfer_xrun = true;
> + notify = true;
> + } else {
> + period_floor = vss->capture_copied_total / period_bytes;
> + if (period_floor > vss->capture_notified_periods) {
> + /* Reserve crossed boundaries before the callback. */
> + vss->capture_notified_periods = period_floor;
> + notify = true;
> + }
> + }
[ ... ]
> diff --git a/sound/virtio/virtio_pcm.h b/sound/virtio/virtio_pcm.h
> index 61faf7b13cbc1..e143a15592f05 100644
> --- a/sound/virtio/virtio_pcm.h
> +++ b/sound/virtio/virtio_pcm.h
[ ... ]
> +static inline bool
> +virtsnd_pcm_capture_counters_valid(const struct virtio_pcm_substream *vss)
> +{
> + return vss->buffer_bytes &&
> + vss->capture_copied_total >= vss->capture_acked_total &&
> + vss->capture_copied_total - vss->capture_acked_total ==
> + (u64)vss->capture_ready &&
> + vss->capture_ready < vss->buffer_bytes &&
> + vss->capture_pointer_seen_total <= vss->capture_copied_total &&
> + vss->hw_ptr == vss->capture_copied_total % vss->buffer_bytes &&
> + (!vss->capture_callback_snapshot_active ||
> + (vss->capture_callback_snapshot >=
> + vss->capture_pointer_seen_total &&
> + vss->capture_callback_snapshot <=
> + vss->capture_copied_total));
> +}
[Severity: Low]
Will these raw 64-bit divisions and modulo operations break 32-bit builds
with undefined references to __udivdi3 and __umoddi3?
Here in virtsnd_pcm_capture_counters_valid() (sound/virtio/virtio_pcm.h)
and virtsnd_pcm_period_elapsed() (sound/virtio/virtio_pcm.c), as well as in
virtsnd_pcm_capture_complete() (sound/virtio/virtio_pcm_msg.c):
else if (vss->hw_ptr !=
vss->capture_copied_total % vss->buffer_bytes)
...
size_t ring_ptr = vss->capture_copied_total %
vss->buffer_bytes;
...
vss->hw_ptr = next_copied_total % vss->buffer_bytes;
and in virtsnd_pcm_cp_pointer() (sound/virtio/virtio_pcm_ops.c):
hw_ptr = total % vss->buffer_bytes;
the u64 counters (capture_copied_total, next_copied_total, and total) are
divided by or taken modulo a size_t value (vss->buffer_bytes or
period_bytes).
On 32-bit architectures, usual arithmetic conversions promote the size_t
operand to u64, causing the compiler to emit __udivdi3 and __umoddi3 calls.
Should these use helpers from linux/math64.h such as div64_ul() or
div_u64_rem() instead?
> diff --git a/sound/virtio/virtio_pcm_msg.c b/sound/virtio/virtio_pcm_msg.c
> index b54642a9521db..795e52e09564b 100644
> --- a/sound/virtio/virtio_pcm_msg.c
> +++ b/sound/virtio/virtio_pcm_msg.c
> @@ -16,6 +17,7 @@
> * @xfer: Request header payload.
> * @status: Response header payload.
> * @length: Data length in bytes.
> + * @data: Request-owned payload for staged capture.
> * @data_offset: Offset in the driver-owned PCM buffer on NOMMU systems.
> * @sgs: Payload scatter-gather table.
> */
> @@ -24,6 +26,7 @@ struct virtio_pcm_msg {
> struct virtio_snd_pcm_xfer xfer;
> struct virtio_snd_pcm_status status;
> size_t length;
> + u8 *data;
> #ifndef CONFIG_MMU
> size_t data_offset;
> #endif
[ ... ]
> + for (i = 0; i < vss->nmsgs && vss->msg_count < request_limit; ++i) {
> + struct virtio_pcm_msg *msg = vss->msgs[i];
> + struct scatterlist *sgs[] = {
> + &msg->sgs[PCM_MSG_SG_XFER],
> + &msg->sgs[PCM_MSG_SG_DATA],
> + &msg->sgs[PCM_MSG_SG_STATUS]
> + };
> + int rc;
> +
> + if (msg->length)
> + continue;
> +
> + msg->xfer.stream_id = cpu_to_le32(vss->sid);
> + /* Do not let an absent packed trailer reuse old PCM bytes. */
> + memset(msg->data, 0, period_bytes);
> + memset(&msg->status, 0, sizeof(msg->status));
> + msg->status.status = cpu_to_le32(VIRTIO_SND_S_BAD_MSG);
> + rc = virtqueue_add_sgs(vqueue, sgs, 1, 2, msg, GFP_ATOMIC);
> + if (rc) {
> + if (added && !(vss->features &
> + (1U << VIRTIO_SND_PCM_F_MSG_POLLING)) &&
> + virtqueue_kick_prepare(vqueue))
> + virtqueue_notify(vqueue);
> + return rc;
> + }
> +
> + msg->length = period_bytes;
> + vss->msg_count++;
> + added = true;
> + }
[Severity: Medium]
Can touching msg->length after virtqueue_add_sgs() in
virtsnd_pcm_capture_send() corrupt DMA transfers on non-coherent platforms?
In struct virtio_pcm_msg, status, length, and data are adjacent fields in
the same slab allocation and share a CPU cacheline.
When virtsnd_pcm_capture_send() calls virtqueue_add_sgs(), msg->status is
mapped for device-to-driver DMA. Immediately after virtqueue_add_sgs()
returns, virtsnd_pcm_capture_send() writes msg->length = period_bytes, and
subsequent calls to virtsnd_pcm_capture_send() read msg->length while the
descriptor is still queued.
On non-coherent DMA systems where virtio uses the DMA API, dirtying the
cacheline containing msg->length after msg->status is mapped can cause a
later cacheline writeback to overwrite the device's DMA write to
msg->status, or cache invalidation during DMA unmap in virtqueue_get_buf()
can discard the write to msg->length so virtsnd_pcm_capture_complete() sees
msg->length == 0 and triggers a false xfer_xrun.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261028094132.20364-1-mynameisjune111@gmail.com?part=7
next prev parent reply other threads:[~2026-10-28 10:14 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-28 9:41 [PATCH v7 00/16] ALSA: virtio: staged capture and dsnoop recovery Joonho Lee
2026-10-28 9:41 ` [PATCH v7 01/16] ALSA: virtio: Propagate PCM send failures through ACK Joonho Lee
2026-10-28 9:41 ` [PATCH v7 02/16] ALSA: virtio: Recover PCM state after a failed start Joonho Lee
2026-10-28 9:41 ` [PATCH v7 03/16] ALSA: virtio: Retain queued messages through device reset Joonho Lee
2026-10-28 9:41 ` [PATCH v7 04/16] ALSA: virtio: Keep PCM DMA buffers alive through device ownership Joonho Lee
2026-10-28 9:41 ` [PATCH v7 05/16] ALSA: virtio: Keep control payloads alive through DMA completion Joonho Lee
2026-10-28 9:41 ` [PATCH v7 06/16] ALSA: virtio: wait through signals for PCM teardown Joonho Lee
2026-10-28 9:41 ` [PATCH v7 07/16] ALSA: virtio: stage VZ capture and restart paused streams Joonho Lee
2026-10-28 10:14 ` sashiko-bot [this message]
2026-10-28 9:41 ` [PATCH v7 08/16] ALSA: virtio: account staged capture reads before RX refill Joonho Lee
2026-10-28 9:41 ` [PATCH v7 09/16] ALSA: virtio: prioritize staged capture period work Joonho Lee
2026-10-28 9:41 ` [PATCH v7 10/16] ALSA: pcm: mark stop-threshold accesses for capture policy sampling Joonho Lee
2026-10-28 9:41 ` [PATCH v7 11/16] ALSA: virtio-snd: fix dsnoop capture with staged buffers Joonho Lee
2026-10-28 10:20 ` sashiko-bot
2026-10-28 9:41 ` [PATCH v7 12/16] ALSA: virtio: complete control transitions and preserve drain tail Joonho Lee
2026-10-28 9:41 ` [PATCH v7 13/16] ALSA: virtio: fence staged capture state and recovery Joonho Lee
2026-10-28 9:41 ` [PATCH v7 14/16] docs: describe virtio-sound capture options Joonho Lee
2026-10-28 9:41 ` [PATCH v7 15/16] ALSA: pcm: synchronize stopped paused streams Joonho Lee
2026-10-28 10:21 ` sashiko-bot
2026-10-28 9:41 ` [PATCH v7 16/16] ALSA: virtio: serialize capture lifecycle and reset state Joonho Lee
2026-10-28 10:26 ` sashiko-bot
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=sashiko-outbox-187737@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=eperezma@redhat.com \
--cc=linux-sound@vger.kernel.org \
--cc=mst@redhat.com \
--cc=mynameisjune111@gmail.com \
--cc=sashiko-reviews@lists.linux.dev \
--cc=virtualization@lists.linux.dev \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox