Skip to contentSkip to conversation

videobuf2: always re-init queue memory consistency attribute

PATCH In review Sergey Senozhatsky 3 participants 5 messages Jun 9, 2020 – Jun 10, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 videobuf2: always re-init queue memory consistency attribute

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).

---
 drivers/media/common/videobuf2/videobuf2-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +3 -4
index 7e081716b8da..37d0186ba330 100644
@@ -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;
 }
 
Signature · 1 line
2.27.0
#1 lore raw

Hi Sergey,

Quoted 55 lines · 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).

---
 drivers/media/common/videobuf2/videobuf2-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +2 -0
index 7e081716b8da..37d0186ba330 100644
@@ -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;
+

drivers/media/common/videobuf2/videobuf2-core.c EXPORT_SYMBOL(vb2_verify_memory_type); · line 714

Quoted 10 lines
drivers/media/common/videobuf2/videobuf2-core.c +1 -4
 	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

#2 lore raw

Hi Hans,

Quoted 4 lines · Hans Verkuil

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
maintainer
#3 lore raw
In reply to Hans Verkuil
Quoted 65 lines · Hans Verkuil

Hi Sergey,

Quoted 61 lines · 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).

---
 drivers/media/common/videobuf2/videobuf2-core.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/media/common/videobuf2/videobuf2-core.c +3 -4
index 7e081716b8da..37d0186ba330 100644
@@ -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)
Quoted 8 lines

drivers/media/common/videobuf2/videobuf2-core.c EXPORT_SYMBOL(vb2_verify_memory_type); · line 714

Quoted 4 lines
drivers/media/common/videobuf2/videobuf2-core.c +0 -0
              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

#4 lore raw

On (20/06/10 12:17), Tomasz Figa wrote:
[..]

Quoted 8 lines
Quoted 3 lines

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