Small USB-audio fixes
cover Small USB-audio fixes
Hi,
here are a few small patches for USB-audio, mainly for covering the missing buffer overflow check at silencing the playback packets.
Takashi
===
Takashi Iwai (4):
ALSA: usb-audio: Update the number of packets properly at receiving
ALSA: usb-audio: Optimize the copy of packet sizes for implicit fb
handling
ALSA: usb-audio: Add sanity check for OOB writes at silencing
ALSA: usb-audio: Avoid potentially repeated XRUN error messages
sound/usb/card.h | 2 +- sound/usb/endpoint.c | 67 ++++++++++++++++++++++++++------------------ 2 files changed, 40 insertions(+), 29 deletions(-)
Signature · 1 line
2.53.0
PATCH 1/4 ALSA: usb-audio: Update the number of packets properly at receiving
At receiving the packets from the implicit feedback source, we didn't update ctx->packets field but only the ctx->packet_size[] data. In exceptional cases, this might lead to unexpectedly superfluous data transfer (although this won't happen usually due to the nature of USB isochronous transfer). Fix it to update the field properly.
--- sound/usb/endpoint.c | 1 + 1 file changed, 1 insertion(+)
sound/usb/endpoint.c +1 -0
@@ -481,6 +481,7 @@ int snd_usb_queue_pending_output_urbs(struct snd_usb_endpoint *ep,/* copy over the length information */ if (implicit_fb) { + ctx->packets = packet->packets;for (i =0 ; i < packet->packets; i++) ctx->packet_size[i] = packet->packet_size[i]; }
Signature · 1 line
2.53.0
PATCH 2/4 ALSA: usb-audio: Optimize the copy of packet sizes for implicit fb handling
In reply to Takashi IwaiWe did manual copies over loop for the packet data update of the implicit feedback, but this can be optimized with a simple memcpy().
Along with it, change the data type of snd_usb_packet_info struct to align with other (from uint32_t to int).
No functional changes but only code optimizations.
--- sound/usb/card.h | 2 +- sound/usb/endpoint.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-)
sound/usb/card.h +1 -1
@@ -90,7 +90,7 @@ struct snd_usb_endpoint {struct snd_urb_ctx urb[MAX_URBS ];struct snd_usb_packet_info { -uint32_t packet_size[MAX_PACKS_HS ]; +int packet_size[MAX_PACKS_HS ];int packets; } next_packet[MAX_URBS ];unsigned int next_packet_head;/* ring buffer offset to read */
sound/usb/endpoint.c +3 -3
@@ -462,7 +462,7 @@ int snd_usb_queue_pending_output_urbs(struct snd_usb_endpoint *ep,while (ep_state_running (ep)) {struct snd_usb_packet_info *packet;struct snd_urb_ctx *ctx =NULL ; -int err, i; +int err; scoped_guard(spinlock_irqsave, &ep->lock) { if ((!implicit_fb || ep->next_packet_queued > 0) &&
@@ -482,8 +482,8 @@ int snd_usb_queue_pending_output_urbs(struct snd_usb_endpoint *ep,/* copy over the length information */ if (implicit_fb) { ctx->packets = packet->packets; -for (i =0 ; i < packet->packets; i++) - ctx->packet_size[i] = packet->packet_size[i]; +memcpy (ctx->packet_size, packet->packet_size, + packet->packets *sizeof (packet->packet_size[0 ])); }/* call the data handler to fill in playback data */
Signature · 1 line
2.53.0
PATCH 3/4 ALSA: usb-audio: Add sanity check for OOB writes at silencing
In reply to Takashi IwaiAt silencing the playback URB packets in the implicit fb mode before the actual playback, we blindly assume that the received packets fit with the buffer size. But when the setup in the capture stream differs from the playback stream (e.g. due to the USB core limitation of max packet size), such an inconsistency may lead to OOB writes to the buffer, resulting in a crash.
For addressing it, add a sanity check of the transfer buffer size at prepare_silent_urb(), and stop the data copy if the received data overflows. Also, report back the transfer error properly from there, too.
Note that this doesn't fix the root cause of the playback error itself, but this merely covers the kernel Oops.
--- sound/usb/endpoint.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-)
sound/usb/endpoint.c +21 -16
@@ -275,8 +275,8 @@ static inline bool has_tx_length_quirk(struct snd_usb_audio *chip)return chip->quirk_flags &QUIRK_FLAG_TX_LENGTH ; } -static void prepare_silent_urb (struct snd_usb_endpoint *ep, -struct snd_urb_ctx *ctx) +static int prepare_silent_urb (struct snd_usb_endpoint *ep, +struct snd_urb_ctx *ctx) {struct urb *urb = ctx->urb;unsigned int offs =0 ;
@@ -289,28 +289,34 @@ static void prepare_silent_urb(struct snd_usb_endpoint *ep, extra =sizeof (packet_length);for (i =0 ; i < ctx->packets; ++i) { -unsigned int offset; -unsigned int length; -int counts; +int length; - counts =snd_usb_endpoint_next_packet_size (ep, ctx, i,0 ); - length = counts * ep->stride;/* number of silent bytes */ - offset = offs * ep->stride + extra * i; - urb->iso_frame_desc[i].offset = offset; + length =snd_usb_endpoint_next_packet_size (ep, ctx, i,0 ); +if (length <0 ) +return length; + length *= ep->stride;/* number of silent bytes */ +if (offs + length + extra > ctx->buffer_size) +break ; + urb->iso_frame_desc[i].offset = offs; urb->iso_frame_desc[i].length = length + extra;if (extra) { packet_length =cpu_to_le32 (length); -memcpy (urb->transfer_buffer + offset, +memcpy (urb->transfer_buffer + offs, &packet_length,sizeof (packet_length)); + offs += extra; } -memset (urb->transfer_buffer + offset + extra, +memset (urb->transfer_buffer + offs, ep->silence_value, length); - offs += counts; + offs += length; } - urb->number_of_packets = ctx->packets; - urb->transfer_buffer_length = offs * ep->stride + ctx->packets * extra; +if (!offs) +return -EPIPE ; + + urb->number_of_packets = i; + urb->transfer_buffer_length = offs; ctx->queued =0 ; +return 0 ; }/*
@@ -332,8 +338,7 @@ static int prepare_outbound_urb(struct snd_usb_endpoint *ep,if (data_subs && ep->prepare_data_urb)return ep->prepare_data_urb (data_subs, urb, in_stream_lock);/* no data provider, so send silence */ -prepare_silent_urb (ep, ctx); -break ; +return prepare_silent_urb (ep, ctx);case SND_USB_ENDPOINT_TYPE_SYNC :if (snd_usb_get_speed (ep->chip->dev) >=USB_SPEED_HIGH ) {
Signature · 1 line
2.53.0
PATCH 4/4 ALSA: usb-audio: Avoid potentially repeated XRUN error messages
In reply to Takashi IwaiSome XRUN-related error messages may repeat themselves, because there can be multiple pending packets. Modify notify_xrun() function to return whether it's being stopped or not, and show the error message only once when the XRUN is actually handled.
Along with it, fix a typo of word package to packet in the error message.
--- sound/usb/endpoint.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-)
sound/usb/endpoint.c +14 -9
@@ -402,18 +402,21 @@ static int prepare_inbound_urb(struct snd_usb_endpoint *ep, }/* notify an error as XRUN to the assigned PCM data substream */ -static void notify_xrun (struct snd_usb_endpoint *ep) +static bool notify_xrun (struct snd_usb_endpoint *ep) {struct snd_usb_substream *data_subs;struct snd_pcm_substream *psubs; data_subs =READ_ONCE (ep->data_subs);if (!data_subs) -return ; +return false; psubs = data_subs->pcm_substream;if (psubs && psubs->runtime && - psubs->runtime->state ==SNDRV_PCM_STATE_RUNNING ) + psubs->runtime->state ==SNDRV_PCM_STATE_RUNNING ) {snd_pcm_stop_xrun (psubs); +return true; + } +return false; } static struct snd_usb_packet_info *
@@ -594,8 +597,9 @@ static void snd_complete_urb(struct urb *urb)return ;if (!atomic_read (&ep->chip->shutdown)) { -usb_audio_err (ep->chip,"cannot submit urb (err = %d)\n" , err); -notify_xrun (ep); +if (notify_xrun (ep)) +usb_audio_err (ep->chip, +"cannot submit urb (err = %d)\n" , err); } exit_clear:
@@ -1779,10 +1783,11 @@ static void snd_usb_handle_sync_urb(struct snd_usb_endpoint *ep,spin_lock_irqsave (&ep->lock, flags);if (ep->next_packet_queued >=ARRAY_SIZE (ep->next_packet)) {spin_unlock_irqrestore (&ep->lock, flags); -usb_audio_err (ep->chip, -"next package FIFO overflow EP 0x%x\n" , - ep->ep_num); -notify_xrun (ep); +if (notify_xrun (ep)) { +usb_audio_err (ep->chip, +"next packet FIFO overflow EP 0x%x\n" , + ep->ep_num); + }return ; }
Signature · 1 line
2.53.0