From: Takashi Iwai <tiwai@suse.de>
To: Hillf Danton <hdanton@sina.com>
Cc: alsa-devel@alsa-project.org,
Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>,
LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/9] ALSA: usb-audio: Fix possible race at sync of urb completions
Date: Sat, 19 Apr 2025 08:50:46 +0200 [thread overview]
Message-ID: <875xj0ve49.wl-tiwai@suse.de> (raw)
In-Reply-To: <20250418144518.4097-1-hdanton@sina.com>
On Fri, 18 Apr 2025 16:45:17 +0200,
Hillf Danton wrote:
>
> On Fri, 18 Apr 2025 13:08:32 +0200 Takashi Iwai wrote:
> > On Fri, 18 Apr 2025 12:35:32 +0200 Hillf Danton wrote:
> > > On Wed, 29 Sep 2021 10:08:37 +0200 Takashi Iwai wrote:
> > > > USB-audio driver tries to sync with the clear of all pending URBs in
> > > > wait_clear_urbs(), and it waits for all bits in active_mask getting
> > > > cleared. This works fine for the normal operations, but when a stream
> > > > is managed in the implicit feedback mode, there is still a very thin
> > > > race window: namely, in snd_complete_usb(), the active_mask bit for
> > > > the current URB is once cleared before re-submitted in
> > > > queue_pending_output_urbs(). If wait_clear_urbs() is called during
> > > > that period, it may pass the test and go forward even though there may
> > > > be a still pending URB.
> > > >
> > > > For covering it, this patch adds a new counter to each endpoint to
> > > > keep the number of in-flight URBs, and changes wait_clear_urbs()
> > > > checking this number instead. The counter is decremented at the end
> > > > of URB complete, hence the reference is kept as long as the URB
> > > > complete is in process.
> > > >
> > > > Signed-off-by: Takashi Iwai <tiwai@suse.de>
> > > > ---
> > > > sound/usb/card.h | 1 +
> > > > sound/usb/endpoint.c | 7 ++++++-
> > > > 2 files changed, 7 insertions(+), 1 deletion(-)
> > > >
> > > > diff --git a/sound/usb/card.h b/sound/usb/card.h
> > > > index 3329ce710cb9..746a765b2437 100644
> > > > --- a/sound/usb/card.h
> > > > +++ b/sound/usb/card.h
> > > > @@ -97,6 +97,7 @@ struct snd_usb_endpoint {
> > > > unsigned int nominal_queue_size; /* total buffer sizes in URBs */
> > > > unsigned long active_mask; /* bitmask of active urbs */
> > > > unsigned long unlink_mask; /* bitmask of unlinked urbs */
> > > > + atomic_t submitted_urbs; /* currently submitted urbs */
> > > > char *syncbuf; /* sync buffer for all sync URBs */
> > > > dma_addr_t sync_dma; /* DMA address of syncbuf */
> > > >
> > > > diff --git a/sound/usb/endpoint.c b/sound/usb/endpoint.c
> > > > index 29c4865966f5..06241568abf7 100644
> > > > --- a/sound/usb/endpoint.c
> > > > +++ b/sound/usb/endpoint.c
> > > > @@ -451,6 +451,7 @@ static void queue_pending_output_urbs(struct snd_usb_endpoint *ep)
> > > > }
> > > >
> > > > set_bit(ctx->index, &ep->active_mask);
> > > > + atomic_inc(&ep->submitted_urbs);
> > > > }
> > > > }
> > > >
> > > > @@ -488,6 +489,7 @@ static void snd_complete_urb(struct urb *urb)
> > > > clear_bit(ctx->index, &ep->active_mask);
> > > > spin_unlock_irqrestore(&ep->lock, flags);
> > > > queue_pending_output_urbs(ep);
> > >
> > > smp_mb();
> > >
> > > > + atomic_dec(&ep->submitted_urbs); /* decrement at last */
> > >
> > > Does it match the comment to add a mb?
> >
> > How...? I don't understand your intention.
> >
> In addition to the UAF report [1], I saw a customer report of list
> corruption of linux-6.1.99 on arm64 this week without reproducer.
>
> list corruption
> list_add_tail();
> push_back_to_ready_list();
> snd_complete_urb();
>
> And after another look at this patch I wonder if the race can not be
> erased without the certainty that ep will be no longer used after the
> atomic decrement.
But why adding more barrier if you perform the atomic op...?
Takashi
>
> [1] https://lore.kernel.org/lkml/CABXGCsOposU1A_HavA_jmtkJMKhDZgh5m1b_YJK1Es5wyE-hZw@mail.gmail.com/
next prev parent reply other threads:[~2025-04-19 6:50 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20210929080844.11583-1-tiwai@suse.de>
[not found] ` <20210929080844.11583-3-tiwai@suse.de>
[not found] ` <20250418103533.4078-1-hdanton@sina.com>
2025-04-18 11:08 ` Takashi Iwai
[not found] ` <20250418144518.4097-1-hdanton@sina.com>
2025-04-19 6:50 ` Takashi Iwai [this message]
[not found] ` <20250419080410.4148-1-hdanton@sina.com>
2025-04-20 7:49 ` Takashi Iwai
[not found] ` <20250421051832.4179-1-hdanton@sina.com>
2025-04-21 7:23 ` Takashi Iwai
[not found] ` <20250421104343.4197-1-hdanton@sina.com>
2025-04-21 14:36 ` Takashi Iwai
[not found] ` <20250421233900.4221-1-hdanton@sina.com>
2025-04-22 7:03 ` Takashi Iwai
[not found] ` <20250422102933.4239-1-hdanton@sina.com>
2025-04-22 11:03 ` Takashi Iwai
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=875xj0ve49.wl-tiwai@suse.de \
--to=tiwai@suse.de \
--cc=alsa-devel@alsa-project.org \
--cc=hdanton@sina.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mikhail.v.gavrilov@gmail.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®