From: Dan Carpenter <error27@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@intel.com>
Cc: Shivani Patel <patelshivani2283@gmail.com>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Parthiban Veerasooran <parthiban.veerasooran@microchip.com>,
Christian Gromm <christian.gromm@microchip.com>,
linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org,
Julia Lawall <julia.lawall@inria.fr>
Subject: Re: [PATCH v3 1/2] staging: most: video: Protect pending_mbos and mute with list_lock
Date: Wed, 18 Nov 2026 15:01:34 +0300 [thread overview]
Message-ID: <av2Tnre7L8iEAf98@stanley.mountain> (raw)
In-Reply-To: <av2KgdP5EuLamxpe@ashevche-desk.local>
On Wed, Nov 18, 2026 at 01:22:41PM +0200, Andy Shevchenko wrote:
> On Wed, Nov 18, 2026 at 12:20:32PM +0300, Dan Carpenter wrote:
> > On Tue, Nov 17, 2026 at 10:35:57PM +0530, Shivani Patel wrote:
> > > data_ready() and get_top_mbo() read mdev->pending_mbos without holding
> > > mdev->list_lock, racing with comp_rx_data() which modifies the list
> > > under mdev->list_lock. Also, comp_vdev_close() clears mdev->mute without
> > > holding mdev->list_lock.
> > >
> > > Acquire mdev->list_lock in data_ready(), get_top_mbo(), and when
> > > clearing mdev->mute in comp_vdev_close(). Use list_first_entry_or_null()
> > > in get_top_mbo() so comp_vdev_read() can drop the redundant data_ready()
> > > check inside the read loop.
>
> ...
>
> > > -static inline bool data_ready(struct most_video_dev *mdev)
> > > +static bool data_ready(struct most_video_dev *mdev)
> > > {
> > > - return !list_empty(&mdev->pending_mbos);
> > > + unsigned long flags;
> > > + bool ready;
> > > +
> > > + spin_lock_irqsave(&mdev->list_lock, flags);
> > > + ready = !list_empty(&mdev->pending_mbos);
> > > + spin_unlock_irqrestore(&mdev->list_lock, flags);
> > > +
> > > + return ready;
> >
> > It doesn't really matter if we take the lock where since we drop it
> > right away. The list_empty() doesn't anything that requires locking
> > so far as I can see.
>
> Right, but the problem is that we possibly might compare valid pointer
> (list head itself) with the stale pointer (head->next and/or head->prev).
It's fine. If you strip away the macros what the code is doing is.
diff --git a/drivers/staging/most/video/video.c b/drivers/staging/most/video/video.c
index 3a0445ff62f4..7bebbf1e11cb 100644
--- a/drivers/staging/most/video/video.c
+++ b/drivers/staging/most/video/video.c
@@ -62,7 +62,9 @@ static DEFINE_SPINLOCK(list_lock);
static inline bool data_ready(struct most_video_dev *mdev)
{
- return !list_empty(&mdev->pending_mbos);
+ if (mdev->pending_mbos.next == mdev->pending_mbos.prev)
+ return false;
+ return true;
}
static inline struct mbo *get_top_mbo(struct most_video_dev *mdev)
The lock is unecessary, but also harmless. Why I don't like adding a
lock is because we drop the lock before we return so the return value
could be wrong by the time it gets to the caller. Nothing really
relies on the return being correct. It's useful because we set the
error code based on it, but if it's wrong the system isn't going to
crash.
It's pretty common to do this sort of thing where we check in a racy
way if we can return early, then we take a lock and check again to
ensure that something is still true when it matters.
regards,
dan carpenter
prev parent reply other threads:[~2026-11-18 12:01 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-17 17:05 Shivani Patel
2026-11-17 17:05 ` [PATCH v3 2/2] staging: most: video: Document locks in struct most_video_dev Shivani Patel
2026-11-18 9:20 ` [PATCH v3 1/2] staging: most: video: Protect pending_mbos and mute with list_lock Dan Carpenter
2026-11-18 11:22 ` Andy Shevchenko
2026-11-18 12:01 ` Dan Carpenter [this message]
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=av2Tnre7L8iEAf98@stanley.mountain \
--to=error27@gmail.com \
--cc=andriy.shevchenko@intel.com \
--cc=christian.gromm@microchip.com \
--cc=gregkh@linuxfoundation.org \
--cc=julia.lawall@inria.fr \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-staging@lists.linux.dev \
--cc=parthiban.veerasooran@microchip.com \
--cc=patelshivani2283@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®