ALSA: virtio: use ack callback
Subsystems (3)
PATCH v4 1/1 ALSA: virtio: use ack callback
This commit uses the ack() callback to determine when a buffer has been updated, then exposes it to guest.
The current mechanism splits a dma buffer into descriptors that are exposed to the device. This dma buffer is shared with the user application. When the device consumes a buffer, the driver moves the request from the used ring to available ring.
The driver exposes the buffer to the device without knowing if the content has been updated from the user. The section 2.8.21.1 of the virtio spec states that: "The device MAY access the descriptor chains the driver created and the memory they refer to immediately". If the device picks up buffers from the available ring just after it is notified, it happens that the content may be old.
When the ack() callback is invoked, the driver exposes only the buffers that have already been updated, i.e., enqueued in the available ring. Thus, the device always picks up a buffer that is updated.
For capturing, the driver starts by exposing all the available buffers to device. After device updates the content of a buffer, it enqueues it in the used ring. It is only after the ack() for capturing is issued that the driver re-enqueues the buffer in the available ring.
---
Changelog:
v3 -> v4:
* Disable rewinds
* Add SNDRV_PCM_INFO_SYNC_APPLPTR flag
* Use pcm indirect API for pointer() and ack() callbacks
v2 -> v3:
* Use ack() callback instead of copy()/fill_silence()
* Change commit name
* Rewrite commit message
* Remove virtsnd_pcm_msg_send_locked()
* Use single callback for both capture and playback
* Fix kernel test robot warnings regarding documentation
* v2 patch at:
https://lore.kernel.org/lkml/[email protected]/T/
v1 -> v2:
* Use snd_pcm_set_managed_buffer_all()for buffer allocation/freeing.
* Make virtsnd_pcm_msg_send() generic by specifying the offset and size for the modified part of the buffer; this way no assumptions need to be made.
* Disable SNDRV_PCM_INFO_NO_REWINDS since now only sequential reading/writing of frames is supported.
* Correct comment at virtsnd_pcm_msg_send().
* v1 patch at:
https://lore.kernel.org/lkml/20231016151000.GE119987@fedora/t/
sound/virtio/virtio_pcm.c | 6 +- sound/virtio/virtio_pcm.h | 9 ++- sound/virtio/virtio_pcm_msg.c | 79 ++++++++++++--------- sound/virtio/virtio_pcm_ops.c | 125 +++++++++++++++++++++++++++------- 4 files changed, 158 insertions(+), 61 deletions(-)
sound/virtio/virtio_pcm.c +4 -2
@@ -109,7 +109,9 @@ static int virtsnd_pcm_build_hw(struct virtio_pcm_substream *vss,SNDRV_PCM_INFO_BATCH |SNDRV_PCM_INFO_BLOCK_TRANSFER |SNDRV_PCM_INFO_INTERLEAVED | -SNDRV_PCM_INFO_PAUSE ; +SNDRV_PCM_INFO_PAUSE | +SNDRV_PCM_INFO_NO_REWINDS | +SNDRV_PCM_INFO_SYNC_APPLPTR ;if (!info->channels_min || info->channels_min > info->channels_max) { dev_err(&vdev->dev,
@@ -471,7 +473,7 @@ int virtsnd_pcm_build_devs(struct virtio_snd *snd)for (kss = ks->substream; kss; kss = kss->next) vs->substreams[kss->number]->substream = kss; -snd_pcm_set_ops (vpcm->pcm, i, &virtsnd_pcm_ops); +snd_pcm_set_ops (vpcm->pcm, i, &virtsnd_pcm_ops[i]); } snd_pcm_set_managed_buffer_all(vpcm->pcm,
sound/virtio/virtio_pcm.h +6 -3
@@ -9,6 +9,7 @@#include <linux/atomic.h> #include <linux/virtio_config.h> #include <sound/pcm.h> +#include <sound/pcm-indirect.h> struct virtio_pcm ;struct virtio_pcm_msg ;
@@ -21,6 +22,7 @@ struct virtio_pcm_msg;* @direction: Stream data flow direction (SNDRV_PCM_STREAM_XXX). * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX). * @substream: Kernel ALSA substream. +* @pcm_indirect: Kernel indirect pcm structure. * @hw: Kernel ALSA substream hardware descriptor. * @elapsed_period: Kernel work to handle the elapsed period state. * @lock: Spinlock that protects fields shared by interrupt handlers and
@@ -46,6 +48,7 @@ struct virtio_pcm_substream {u32 direction;u32 features;struct snd_pcm_substream *substream; +struct snd_pcm_indirect pcm_indirect;struct snd_pcm_hardware hw;struct work_struct elapsed_period;spinlock_t lock;
@@ -57,7 +60,6 @@ struct virtio_pcm_substream {bool suspended;struct virtio_pcm_msg **msgs;unsigned int nmsgs; -int msg_last_enqueued;unsigned int msg_count;wait_queue_head_t msg_empty; };
@@ -90,7 +92,7 @@ struct virtio_pcm {struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST +1 ]; }; -extern const struct snd_pcm_ops virtsnd_pcm_ops; +extern const struct snd_pcm_ops virtsnd_pcm_ops[];int virtsnd_pcm_validate (struct virtio_device *vdev);
@@ -117,7 +119,8 @@ int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss,void virtsnd_pcm_msg_free (struct virtio_pcm_substream *vss); -int virtsnd_pcm_msg_send (struct virtio_pcm_substream *vss); +int virtsnd_pcm_msg_send (struct virtio_pcm_substream *vss,unsigned long offset, +unsigned long bytes);unsigned int virtsnd_pcm_msg_pending_num (struct virtio_pcm_substream *vss);
sound/virtio/virtio_pcm_msg.c +46 -33
128 lines not on this page · raw message
sound/virtio/virtio_pcm_ops.c +102 -23
166 lines not on this page · raw message
base-commit: 8a749fd1a8720d4619c91c8b6e7528c0a355c0aa
Signature · 6 lines
2.41.0 _______________________________________________ Virtualization mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/virtualization
Quoted 20 lines · Matias Ezequiel Vara Larsen
This commit uses the
ack()callback to determine when a buffer has been updated, then exposes it to guest.The current mechanism splits a dma buffer into descriptors that are exposed to the device. This dma buffer is shared with the user application. When the device consumes a buffer, the driver moves the request from the used ring to available ring.
The driver exposes the buffer to the device without knowing if the content has been updated from the user. The section 2.8.21.1 of the virtio spec states that: "The device MAY access the descriptor chains the driver created and the memory they refer to immediately". If the device picks up buffers from the available ring just after it is notified, it happens that the content may be old.
When the
ack()callback is invoked, the driver exposes only the buffers that have already been updated, i.e., enqueued in the available ring. Thus, the device always picks up a buffer that is updated.
Quoted 8 lines
For capturing, the driver starts by exposing all the available buffers to device. After device updates the content of a buffer, it enqueues it in the used ring. It is only after the
ack()for capturing is issued that the driver re-enqueues the buffer in the available ring.Co-developed-by Anton Yakovlev <[email protected]>Signed-off-by Anton Yakovlev <[email protected]>Signed-off-by Matias Ezequiel Vara Larsen <[email protected]>
Applied now to for-next branch.
thanks,
Takashi
Quoted 27 lines · Takashi Iwai
Quoted 25 lines · Matias Ezequiel Vara Larsen
This commit uses the
ack()callback to determine when a buffer has been updated, then exposes it to guest.The current mechanism splits a dma buffer into descriptors that are exposed to the device. This dma buffer is shared with the user application. When the device consumes a buffer, the driver moves the request from the used ring to available ring.
The driver exposes the buffer to the device without knowing if the content has been updated from the user. The section 2.8.21.1 of the virtio spec states that: "The device MAY access the descriptor chains the driver created and the memory they refer to immediately". If the device picks up buffers from the available ring just after it is notified, it happens that the content may be old.
When the
ack()callback is invoked, the driver exposes only the buffers that have already been updated, i.e., enqueued in the available ring. Thus, the device always picks up a buffer that is updated.For capturing, the driver starts by exposing all the available buffers to device. After device updates the content of a buffer, it enqueues it in the used ring. It is only after the
ack()for capturing is issued that the driver re-enqueues the buffer in the available ring.
Quoted 5 lines
Quoted 3 lines
Co-developed-by Anton Yakovlev <[email protected]>Signed-off-by Anton Yakovlev <[email protected]>Signed-off-by Matias Ezequiel Vara Larsen <[email protected]>Applied now to for-next branch.
Cool, thanks for that!
I just wonder if we should CC stable since we are fixing a virtio specification violation.
@Michael what do you think?
Thanks,
Stefano
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 46 lines · Michael S. Tsirkin
Quoted 36 lines · Stefano Garzarella
Quoted 27 lines · Takashi Iwai
Quoted 25 lines · Matias Ezequiel Vara Larsen
This commit uses the
ack()callback to determine when a buffer has been updated, then exposes it to guest.The current mechanism splits a dma buffer into descriptors that are exposed to the device. This dma buffer is shared with the user application. When the device consumes a buffer, the driver moves the request from the used ring to available ring.
The driver exposes the buffer to the device without knowing if the content has been updated from the user. The section 2.8.21.1 of the virtio spec states that: "The device MAY access the descriptor chains the driver created and the memory they refer to immediately". If the device picks up buffers from the available ring just after it is notified, it happens that the content may be old.
When the
ack()callback is invoked, the driver exposes only the buffers that have already been updated, i.e., enqueued in the available ring. Thus, the device always picks up a buffer that is updated.For capturing, the driver starts by exposing all the available buffers to device. After device updates the content of a buffer, it enqueues it in the used ring. It is only after the
ack()for capturing is issued that the driver re-enqueues the buffer in the available ring.Quoted 5 lines
Quoted 3 lines
Co-developed-by Anton Yakovlev <[email protected]>Signed-off-by Anton Yakovlev <[email protected]>Signed-off-by Matias Ezequiel Vara Larsen <[email protected]>Applied now to for-next branch.
Cool, thanks for that!
Quoted 7 lines
I just wonder if we should CC stable since we are fixing a virtio specification violation.
@Michael what do you think?
Thanks,
Stefano
Quoted 6 lines
Fixes de3a9980d8c3 ("ALSA: virtio: add virtio sound driver")The patch is too big for stable - more than 100 lines added. See:
Documentation/process/stable-kernel-rules.rst
Yeah, I see, thanks for sharing!
Stefano
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization