All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sinan Turunc" <enscrpt@pm.me>
Cc: linux-sound@vger.kernel.org
Subject: Re: [PATCH] ALSA: line6: Bound capture copies by the PCM buffer size
Date: Tue, 03 Nov 2026 12:24:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-195233@kernel.org> (raw)
In-Reply-To: <20261103120723.328-1-enscrpt@pm.me>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] ALSA: line6: clamping frames to buffer_size in line6_capture_copy() stalls PCM
--

commit dece7d69941f7080a104841df1e03f3fae6b7577
Author: Sinan Turunc <enscrpt@pm.me>

ALSA: line6: Bound capture copies by the PCM buffer size

This patch clamps the number of captured frames in line6_capture_copy() to
runtime->buffer_size so that oversized incoming USB packets do not overflow
the single ring-buffer wrap calculation.

> diff --git a/sound/usb/line6/capture.c b/sound/usb/line6/capture.c
> index 17f49f567efbf..8b6a0dd2ddde2 100644
> --- a/sound/usb/line6/capture.c
> +++ b/sound/usb/line6/capture.c
> @@ -94,6 +94,17 @@ void line6_capture_copy(struct snd_line6_pcm *line6pcm, char *fbuf, int fsize)
>  	if (runtime == NULL)
>  		return;
>  
> +	/*
> +	 * A packet can carry up to max_packet_size_in, which the device
> +	 * chooses, while the ring buffer can be as small as one 64-byte
> +	 * period. The single wrap below is only valid for
> +	 * frames <= buffer_size, so keep what fits.
> +	 */
> +	if (unlikely(frames > runtime->buffer_size)) {
> +		frames = runtime->buffer_size;
> +		fsize = frames * bytes_per_frame;
> +	}

[Severity: High]
Does clamping frames to runtime->buffer_size here cause the capture stream
to stall whenever an incoming packet exceeds the buffer size (for example,
when a user configures a 1-period 64-byte capture buffer of 11 or 16 frames
and receives a regular 44- or 48-frame 1 ms packet or a larger USB/IP
packet)?

At the end of line6_capture_copy(), pos_done is updated with:

sound/usb/line6/capture.c:line6_capture_copy() {
	...
	line6pcm->in.pos_done += frames;
	if (line6pcm->in.pos_done >= runtime->buffer_size)
		line6pcm->in.pos_done -= runtime->buffer_size;
}

When frames is clamped to runtime->buffer_size, adding and immediately
subtracting runtime->buffer_size leaves line6pcm->in.pos_done unchanged on
every packet.

When audio_in_callback() then calls snd_pcm_period_elapsed(),
snd_pcm_update_hw_ptr0() reads the same position via snd_line6_pointer().
In the interrupt path, snd_pcm_update_hw_ptr0() only treats an unchanged
pointer as a full-buffer wrap when hdelta exceeds
runtime->hw_ptr_buffer_jiffies / 2 + 1:

sound/core/pcm_lib.c:snd_pcm_update_hw_ptr0() {
	...
	if (in_interrupt) {
		/* we know that one period was processed */
		/* delta = "expected next hw_ptr" for in_interrupt != 0 */
		delta = runtime->hw_ptr_interrupt + runtime->period_size;
		if (delta > new_hw_ptr) {
			/* check for double acknowledged interrupts */
			hdelta = curr_jiffies - runtime->hw_ptr_jiffies;
			if (hdelta > runtime->hw_ptr_buffer_jiffies/2 + 1) {
				hw_base += runtime->buffer_size;
				...
			}
		}
	}
	/* new_hw_ptr might be lower than old_hw_ptr in case of */
	/* pointer crosses the end of the ring buffer */
	if (runtime->status->hw_ptr == new_hw_ptr) {
		runtime->hw_ptr_jiffies = curr_jiffies;
		update_audio_tstamp(substream, &curr_tstamp, &audio_tstamp);
		return 0;
	}
}

Because isochronous URBs complete every 1 ms (or 125 us), hdelta is at most
1 jiffy, so snd_pcm_update_hw_ptr0() takes the
runtime->status->hw_ptr == new_hw_ptr return path and refreshes
runtime->hw_ptr_jiffies on every URB without ever advancing hw_ptr or waking
user space.

Also, fsize is not read again after frames is computed at the top of
line6_capture_copy(), so is the assignment to fsize here unused?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261103120723.328-1-enscrpt@pm.me?part=1

  reply	other threads:[~2026-11-03 12:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 12:07 [PATCH] ALSA: line6: Bound capture copies by the PCM buffer size Sinan Turunc
2026-11-03 12:24 ` sashiko-bot [this message]
2026-11-03 13:22   ` Sinan Turunc

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-195233@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=enscrpt@pm.me \
    --cc=linux-sound@vger.kernel.org \
    --cc=sashiko-reviews@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 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.