All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] videobuf2: always re-init queue memory consistency attribute
@ 2020-06-09  6:04 Sergey Senozhatsky
  2020-06-10  9:54 ` Hans Verkuil
  0 siblings, 1 reply; 5+ messages in thread
From: Sergey Senozhatsky @ 2020-06-09  6:04 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Mauro Carvalho Chehab, Tomasz Figa, linux-media, linux-kernel,
	Sergey Senozhatsky

We need a combination of two factors in order to permit modification
of queue's memory consistency attribute in set_queue_consistency():
  (1) queue should allow user-space cache hints
  (2) queue should be used for MMAP-ed I/O

Therefore the code in videobuf2 core looks as follows:

	q->memory = req->memory;
	set_queue_consistency(q, consistent);

This works when we do something like this (suppose that queue allows
cache hints)

	reqbufs.memory = V4L2_MEMORY_DMABUF;
	reqbufs.flags = 0;
	doioctl(node, VIDIOC_REQBUFS, &reqbufs);

	reqbufs.memory = V4L2_MEMORY_MMAP;
	reqbufs.flags = V4L2_FLAG_MEMORY_NON_CONSISTENT;
	doioctl(node, VIDIOC_REQBUFS, &reqbufs);

However, this doesn't work the other way around

	reqbufs.memory = V4L2_MEMORY_MMAP;
	reqbufs.flags = V4L2_FLAG_MEMORY_NON_CONSISTENT;
	doioctl(node, VIDIOC_REQBUFS, &reqbufs);

	reqbufs.memory = V4L2_MEMORY_DMABUF;
	reqbufs.flags = 0;
	doioctl(node, VIDIOC_REQBUFS, &reqbufs);

because __vb2_queue_free() doesn't clear queue's ->dma_attrs
once its don't freeing queue buffers, and by the time we call
set_queue_consistency() the queue is DMABUF so (2) is false
and we never clear the stale consistency attribute.

Re-implement set_queue_consistency() - it must always clear
queue's non-consistency attr and set it only if (1) and (2).

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
 drivers/media/common/videobuf2/videobuf2-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
index 7e081716b8da..37d0186ba330 100644
--- a/drivers/media/common/videobuf2/videobuf2-core.c
+++ b/drivers/media/common/videobuf2/videobuf2-core.c
@@ -704,12 +704,11 @@ EXPORT_SYMBOL(vb2_verify_memory_type);
 
 static void set_queue_consistency(struct vb2_queue *q, bool consistent_mem)
 {
+	q->dma_attrs &= ~DMA_ATTR_NON_CONSISTENT;
+
 	if (!vb2_queue_allows_cache_hints(q))
 		return;
-
-	if (consistent_mem)
-		q->dma_attrs &= ~DMA_ATTR_NON_CONSISTENT;
-	else
+	if (!consistent_mem)
 		q->dma_attrs |= DMA_ATTR_NON_CONSISTENT;
 }
 
-- 
2.27.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] videobuf2: always re-init queue memory consistency attribute
  2020-06-09  6:04 [PATCH] videobuf2: always re-init queue memory consistency attribute Sergey Senozhatsky
@ 2020-06-10  9:54 ` Hans Verkuil
  2020-06-10 10:10   ` Sergey Senozhatsky
  2020-06-10 10:17   ` Tomasz Figa
  0 siblings, 2 replies; 5+ messages in thread
From: Hans Verkuil @ 2020-06-10  9:54 UTC (permalink / raw)
  To: Sergey Senozhatsky
  Cc: Mauro Carvalho Chehab, Tomasz Figa, linux-media, linux-kernel

Hi Sergey,

On 09/06/2020 08:04, Sergey Senozhatsky wrote:
> We need a combination of two factors in order to permit modification
> of queue's memory consistency attribute in set_queue_consistency():
>   (1) queue should allow user-space cache hints
>   (2) queue should be used for MMAP-ed I/O
> 
> Therefore the code in videobuf2 core looks as follows:
> 
> 	q->memory = req->memory;
> 	set_queue_consistency(q, consistent);
> 
> This works when we do something like this (suppose that queue allows
> cache hints)
> 
> 	reqbufs.memory = V4L2_MEMORY_DMABUF;
> 	reqbufs.flags = 0;
> 	doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> 
> 	reqbufs.memory = V4L2_MEMORY_MMAP;
> 	reqbufs.flags = V4L2_FLAG_MEMORY_NON_CONSISTENT;
> 	doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> 
> However, this doesn't work the other way around
> 
> 	reqbufs.memory = V4L2_MEMORY_MMAP;
> 	reqbufs.flags = V4L2_FLAG_MEMORY_NON_CONSISTENT;
> 	doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> 
> 	reqbufs.memory = V4L2_MEMORY_DMABUF;
> 	reqbufs.flags = 0;
> 	doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> 
> because __vb2_queue_free() doesn't clear queue's ->dma_attrs
> once its don't freeing queue buffers, and by the time we call
> set_queue_consistency() the queue is DMABUF so (2) is false
> and we never clear the stale consistency attribute.
> 
> Re-implement set_queue_consistency() - it must always clear
> queue's non-consistency attr and set it only if (1) and (2).
> 
> Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> ---
>  drivers/media/common/videobuf2/videobuf2-core.c | 7 +++----
>  1 file changed, 3 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
> index 7e081716b8da..37d0186ba330 100644
> --- a/drivers/media/common/videobuf2/videobuf2-core.c
> +++ b/drivers/media/common/videobuf2/videobuf2-core.c
> @@ -704,12 +704,11 @@ EXPORT_SYMBOL(vb2_verify_memory_type);
>  
>  static void set_queue_consistency(struct vb2_queue *q, bool consistent_mem)
>  {
> +	q->dma_attrs &= ~DMA_ATTR_NON_CONSISTENT;
> +
>  	if (!vb2_queue_allows_cache_hints(q))
>  		return;
> -
> -	if (consistent_mem)
> -		q->dma_attrs &= ~DMA_ATTR_NON_CONSISTENT;
> -	else
> +	if (!consistent_mem)
>  		q->dma_attrs |= DMA_ATTR_NON_CONSISTENT;
>  }
>  
> 

Is it OK with you if I fold this patch into the original patch series and make a
new PR for it? I prefer to have a series merged without a bug, rather than fixing
it in a final patch.

Regards,

	Hans

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] videobuf2: always re-init queue memory consistency attribute
  2020-06-10  9:54 ` Hans Verkuil
@ 2020-06-10 10:10   ` Sergey Senozhatsky
  2020-06-10 10:17   ` Tomasz Figa
  1 sibling, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2020-06-10 10:10 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Sergey Senozhatsky, Mauro Carvalho Chehab, Tomasz Figa,
	linux-media, linux-kernel

Hi Hans,

On (20/06/10 11:54), Hans Verkuil wrote:
> 
> Is it OK with you if I fold this patch into the original patch series and make a
> new PR for it? I prefer to have a series merged without a bug, rather than fixing
> it in a final patch.

Absolutely!

	-ss

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] videobuf2: always re-init queue memory consistency attribute
  2020-06-10  9:54 ` Hans Verkuil
  2020-06-10 10:10   ` Sergey Senozhatsky
@ 2020-06-10 10:17   ` Tomasz Figa
  2020-06-10 10:48     ` Sergey Senozhatsky
  1 sibling, 1 reply; 5+ messages in thread
From: Tomasz Figa @ 2020-06-10 10:17 UTC (permalink / raw)
  To: Hans Verkuil
  Cc: Sergey Senozhatsky, Mauro Carvalho Chehab,
	Linux Media Mailing List, Linux Kernel Mailing List

On Wed, Jun 10, 2020 at 11:54 AM Hans Verkuil <hverkuil@xs4all.nl> wrote:
>
> Hi Sergey,
>
> On 09/06/2020 08:04, Sergey Senozhatsky wrote:
> > We need a combination of two factors in order to permit modification
> > of queue's memory consistency attribute in set_queue_consistency():
> >   (1) queue should allow user-space cache hints
> >   (2) queue should be used for MMAP-ed I/O
> >
> > Therefore the code in videobuf2 core looks as follows:
> >
> >       q->memory = req->memory;
> >       set_queue_consistency(q, consistent);
> >
> > This works when we do something like this (suppose that queue allows
> > cache hints)
> >
> >       reqbufs.memory = V4L2_MEMORY_DMABUF;
> >       reqbufs.flags = 0;
> >       doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> >
> >       reqbufs.memory = V4L2_MEMORY_MMAP;
> >       reqbufs.flags = V4L2_FLAG_MEMORY_NON_CONSISTENT;
> >       doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> >
> > However, this doesn't work the other way around
> >
> >       reqbufs.memory = V4L2_MEMORY_MMAP;
> >       reqbufs.flags = V4L2_FLAG_MEMORY_NON_CONSISTENT;
> >       doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> >
> >       reqbufs.memory = V4L2_MEMORY_DMABUF;
> >       reqbufs.flags = 0;
> >       doioctl(node, VIDIOC_REQBUFS, &reqbufs);
> >
> > because __vb2_queue_free() doesn't clear queue's ->dma_attrs
> > once its don't freeing queue buffers, and by the time we call
> > set_queue_consistency() the queue is DMABUF so (2) is false
> > and we never clear the stale consistency attribute.
> >
> > Re-implement set_queue_consistency() - it must always clear
> > queue's non-consistency attr and set it only if (1) and (2).
> >
> > Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
> > ---
> >  drivers/media/common/videobuf2/videobuf2-core.c | 7 +++----
> >  1 file changed, 3 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/media/common/videobuf2/videobuf2-core.c b/drivers/media/common/videobuf2/videobuf2-core.c
> > index 7e081716b8da..37d0186ba330 100644
> > --- a/drivers/media/common/videobuf2/videobuf2-core.c
> > +++ b/drivers/media/common/videobuf2/videobuf2-core.c
> > @@ -704,12 +704,11 @@ EXPORT_SYMBOL(vb2_verify_memory_type);
> >
> >  static void set_queue_consistency(struct vb2_queue *q, bool consistent_mem)
> >  {
> > +     q->dma_attrs &= ~DMA_ATTR_NON_CONSISTENT;
> > +
> >       if (!vb2_queue_allows_cache_hints(q))
> >               return;
> > -
> > -     if (consistent_mem)
> > -             q->dma_attrs &= ~DMA_ATTR_NON_CONSISTENT;
> > -     else
> > +     if (!consistent_mem)
> >               q->dma_attrs |= DMA_ATTR_NON_CONSISTENT;
> >  }
> >
> >
>
> Is it OK with you if I fold this patch into the original patch series and make a
> new PR for it? I prefer to have a series merged without a bug, rather than fixing
> it in a final patch.

I believe this didn't introduce any real bug, because dma_attrs would
end up used only for MMAP buffers anyway. Still, the current behavior
could end up being confusing for whoever has to deal with vb2 in the
future, so should be fixed.

Best regards,
Tomasz

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] videobuf2: always re-init queue memory consistency attribute
  2020-06-10 10:17   ` Tomasz Figa
@ 2020-06-10 10:48     ` Sergey Senozhatsky
  0 siblings, 0 replies; 5+ messages in thread
From: Sergey Senozhatsky @ 2020-06-10 10:48 UTC (permalink / raw)
  To: Tomasz Figa
  Cc: Hans Verkuil, Sergey Senozhatsky, Mauro Carvalho Chehab,
	Linux Media Mailing List, Linux Kernel Mailing List

On (20/06/10 12:17), Tomasz Figa wrote:
[..]
> > Is it OK with you if I fold this patch into the original patch series and make a
> > new PR for it? I prefer to have a series merged without a bug, rather than fixing
> > it in a final patch.
> 
> I believe this didn't introduce any real bug, because dma_attrs would
> end up used only for MMAP buffers anyway. Still, the current behavior
> could end up being confusing for whoever has to deal with vb2 in the
> future, so should be fixed.

This also requires user-space code that constantly use different ->memory
types and ->flags in inoctl()-s on the same queue.

	-ss

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-06-10 10:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-09  6:04 [PATCH] videobuf2: always re-init queue memory consistency attribute Sergey Senozhatsky
2020-06-10  9:54 ` Hans Verkuil
2020-06-10 10:10   ` Sergey Senozhatsky
2020-06-10 10:17   ` Tomasz Figa
2020-06-10 10:48     ` Sergey Senozhatsky

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.