From: Marek Szyprowski <m.szyprowski@samsung.com>
To: Ricardo Ribalda <ribalda@chromium.org>,
Tomasz Figa <tfiga@chromium.org>,
Mauro Carvalho Chehab <mchehab@kernel.org>,
Hans Verkuil <hverkuil@kernel.org>,
Benjamin Gaignard <benjamin.gaignard@collabora.com>
Cc: linux-media@vger.kernel.org, linux-kernel@vger.kernel.org,
stable@vger.kernel.org
Subject: Re: [PATCH v2 2/2] media: videobuf2: order buffer initialization before bufs_bitmap publication
Date: Tue, 3 Nov 2026 14:39:36 +0100 [thread overview]
Message-ID: <c1caf0c5-d82e-44c1-9d0f-934008f4febd@samsung.com> (raw)
In-Reply-To: <20261103-vb2-mmap_lock-v2-2-054e11dcd013@chromium.org>
On 03.11.2026 11:56, Ricardo Ribalda wrote:
> Stateless mem2mem decoders call vb2_find_buffer() -> vb2_get_buffer()
> from .device_run() without holding q->lock or q->mmap_lock, which can
> run concurrently with VIDIOC_CREATE_BUFS on a streaming queue.
>
> Both set_bit() and test_bit() are unordered, and mutex_lock(&q->mmap_lock)
> is only an acquire barrier. On weakly-ordered architectures such as
> arm64, a lockless vb2_get_buffer() caller can observe a newly set bit in
> q->bufs_bitmap before the kzalloc() zeroing, buffer initialization, and
> q->bufs[] pointer store performed by __vb2_queue_alloc() become visible.
>
> Pair smp_mb__before_atomic() in vb2_queue_publish_buffers() with
> test_bit_acquire() in vb2_get_buffer() so that any reader observing a
> set bit in q->bufs_bitmap also sees a fully initialized vb2_buffer.
>
> Fixes: a286b0837e13 ("media: core: Add bitmap manage bufs array entries")
> Cc: stable@vger.kernel.org
> Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Can't we use ordinary locking instead of those memory barriers and relying
on not-always-atomic bit operations? For me this looks like over-optimization,
taking into account that those operations are not on the critical path nor
anyhow time-comparable with filling the whole buffer with image data.
> ---
> drivers/media/common/videobuf2/videobuf2-core.c | 8 ++++++++
> include/media/videobuf2-core.h | 6 +++++-
> 2 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
> index 70ef38c3d16a..d0dd4b22b646 100644
> --- a/drivers/media/common/videobuf2/videobuf2-core.c
> +++ b/drivers/media/common/videobuf2/videobuf2-core.c
> @@ -461,6 +461,14 @@ static void vb2_queue_publish_buffers(struct vb2_queue *q, unsigned int start,
>
> lockdep_assert_held(&q->mmap_lock);
>
> + /*
> + * Order the buffer initialization and the q->bufs[] stores done by
> + * __vb2_queue_alloc() before the bits that publish them. Pairs with
> + * test_bit_acquire() in vb2_get_buffer(), which is also called
> + * without q->lock or q->mmap_lock held, e.g. by vb2_find_buffer()
> + * from a mem2mem .device_run().
> + */
> + smp_mb__before_atomic();
> for (i = start; i < start + count; i++) {
> if (WARN_ON(i >= q->max_num_buffers || !q->bufs[i] ||
> test_bit(i, q->bufs_bitmap)))
> diff --git a/include/media/videobuf2-core.h b/include/media/videobuf2-core.h
> index 2e93e6985d5f..20b961da21e0 100644
> --- a/include/media/videobuf2-core.h
> +++ b/include/media/videobuf2-core.h
> @@ -1288,7 +1288,11 @@ static inline struct vb2_buffer *vb2_get_buffer(struct vb2_queue *q,
> if (index >= q->max_num_buffers)
> return NULL;
>
> - if (test_bit(index, q->bufs_bitmap))
> + /*
> + * A set bit guarantees that the buffer is fully initialized, pairs
> + * with smp_mb__before_atomic() in vb2_queue_publish_buffers().
> + */
> + if (test_bit_acquire(index, q->bufs_bitmap))
> return q->bufs[index];
> return NULL;
> }
>
Best regards
--
Marek Szyprowski, PhD
Samsung R&D Institute Poland
next prev parent reply other threads:[~2026-11-03 13:39 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-03 10:56 [PATCH v2 0/2] media: videobuf2: publish buffers in bufs_bitmap under mmap_lock Ricardo Ribalda
2026-11-03 10:56 ` [PATCH v2 1/2] " Ricardo Ribalda
2026-11-03 14:22 ` Hans Verkuil
2026-11-03 16:26 ` Marek Szyprowski
2026-11-03 10:56 ` [PATCH v2 2/2] media: videobuf2: order buffer initialization before bufs_bitmap publication Ricardo Ribalda
2026-11-03 13:39 ` Marek Szyprowski [this message]
2026-11-03 14:19 ` Hans Verkuil
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=c1caf0c5-d82e-44c1-9d0f-934008f4febd@samsung.com \
--to=m.szyprowski@samsung.com \
--cc=benjamin.gaignard@collabora.com \
--cc=hverkuil@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-media@vger.kernel.org \
--cc=mchehab@kernel.org \
--cc=ribalda@chromium.org \
--cc=stable@vger.kernel.org \
--cc=tfiga@chromium.org \
/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®