Skip to contentSkip to conversation

media: stk1160: allocate urb buffs with the DMA noncontiguous API

PATCH Superseded Dafna Hirschfeld 4 participants 16 messages Jan 11, 2022 – Jan 24, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover media: stk1160: allocate urb buffs with the DMA noncontiguous API

Replace urb buffers allocation to use the DMA noncontiguous API. Similarly to a commit sent to uvc: [1] This improves performance on Arm.

The series was tested on rock-pi4 (Arm64) and shows about 10x improvements:

NON_CONTIGUOUS
uS/qty: 9231963/106048 sec: 9231.963472 avg: 87.054 min: 0.291 max: 216.423 (uS) raw decode speed: 9.217 Gbits/s
bytes 9.971904900 G
bytes/urb: 100290

COHERENT
uS/qty: 92423219/98904 sec: 92423.219509 avg: 934.474 min: 0.583 max: 2196.583 (uS) raw decode speed: 787.698 Mbits/s
bytes 8.510250828 G
bytes/urb: 92010

On x86 there is no significant difference:

NON_CONTIGUOUS
uS/qty: 3203891/50096 sec: 3203.891724 avg: 63.955 min: 0.141 max: 11696.636 (uS) raw decode speed: 23.783 Gbits/s
bytes 8.932543668 G
bytes/urb: 190084

COHERENT
uS/qty: 3081680/50112 sec: 3081.680438 avg: 61.495 min: 0.160 max: 482.099 (uS) raw decode speed: 21.557 Gbits/s
bytes 7.786260844 G
bytes/urb: 165677

[1] https://lkml.org/lkml/2021/3/12/1506

Dafna Hirschfeld (3):
media: stk1160: fix number of buffers in case not all buffers are
created
media: stk1160: move transfer_buffer and urb to same struct

    'stk1160_urb'

media: stk1160: use dma_alloc_noncontiguous API

 drivers/media/usb/stk1160/stk1160-v4l.c   |   5 +-
 drivers/media/usb/stk1160/stk1160-video.c | 137 +++++++++++-----------
 drivers/media/usb/stk1160/stk1160.h       |  21 +++-
 3 files changed, 91 insertions(+), 72 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/3 media: stk1160: fix number of buffers in case not all buffers are created

In case we fail to allocate a transfer_buffer then we break the buffers creation loop and update the number of buffers to the number of successfully allocated which should be 'i' and not 'i - 1'

---
 drivers/media/usb/stk1160/stk1160-video.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/stk1160/stk1160-video.c +2 -2
index 202b084f65a2..91bd6adccdd1 100644
@@ -511,9 +511,9 @@ int stk1160_alloc_isoc(struct stk1160 *dev)
 	usb_free_urb(dev->isoc_ctl.urb[i]);
 	dev->isoc_ctl.urb[i] = NULL;
 
-	stk1160_warn("%d urbs allocated. Trying to continue...\n", i - 1);
+	stk1160_warn("%d urbs allocated. Trying to continue...\n", i);
 
-	dev->isoc_ctl.num_bufs = i - 1;
+	dev->isoc_ctl.num_bufs = i;
 
 	return 0;
 
Signature · 1 line
2.17.1
maintainer Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] media: stk1160: fix number of buffers in case not all buffers are created

Quoted 6 lines · Dafna Hirschfeld

In case we fail to allocate a transfer_buffer then we break the buffers creation loop and update the number of buffers to the number of successfully allocated which should be 'i' and not 'i - 1'

Quoted 23 lines
---
 drivers/media/usb/stk1160/stk1160-video.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/stk1160/stk1160-video.c +2 -2
index 202b084f65a2..91bd6adccdd1 100644
@@ -511,9 +511,9 @@ int stk1160_alloc_isoc(struct stk1160 *dev)
 	usb_free_urb(dev->isoc_ctl.urb[i]);
 	dev->isoc_ctl.urb[i] = NULL;
 
-	stk1160_warn("%d urbs allocated. Trying to continue...\n", i - 1);
+	stk1160_warn("%d urbs allocated. Trying to continue...\n", i);
 
-	dev->isoc_ctl.num_bufs = i - 1;
+	dev->isoc_ctl.num_bufs = i;
 
 	return 0;
 
Signature · 1 line
2.17.1
#3 lore raw

PATCH 2/3 media: stk1160: move transfer_buffer and urb to same struct 'stk1160_urb'

In reply to Dafna Hirschfeld

Instead of having two separated arrays, one for the urbs and one for their buffers, have one array of a struct containing both. In addition, the array is just 16 pointers, no need to dynamically allocate it.

---
 drivers/media/usb/stk1160/stk1160-v4l.c   |  2 +-
 drivers/media/usb/stk1160/stk1160-video.c | 50 ++++++++---------------
 drivers/media/usb/stk1160/stk1160.h       | 11 ++---
 3 files changed, 23 insertions(+), 40 deletions(-)
drivers/media/usb/stk1160/stk1160-v4l.c +1 -1
index 6a4eb616d516..a06030451db4 100644
@@ -232,7 +232,7 @@ static int stk1160_start_streaming(struct stk1160 *dev)
 
 	/* submit urbs and enables IRQ */
 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
-		rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
+		rc = usb_submit_urb(dev->isoc_ctl.stk1160_urb[i].urb, GFP_KERNEL);
 		if (rc) {
 			stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
 			goto out_uninit;
drivers/media/usb/stk1160/stk1160-video.c +16 -34

109 lines not on this page · raw message

drivers/media/usb/stk1160/stk1160.h +6 -5
index a31ea1c80f25..1ffca1343d88 100644
@@ -84,6 +84,11 @@ struct stk1160_buffer {
 	unsigned int pos;		/* current pos inside buffer */
 };
 
+struct stk1160_urb {
+	struct urb *urb;
+	char *transfer_buffer;
+};
+
 struct stk1160_isoc_ctl {
 	/* max packet size of isoc transaction */
 	int max_pkt_size;
@@ -91,11 +96,7 @@ struct stk1160_isoc_ctl {
 	/* number of allocated urbs */
 	int num_bufs;
 
-	/* urb for isoc transfers */
-	struct urb **urb;
-
-	/* transfer buffers for isoc transfer */
-	char **transfer_buffer;
+	struct stk1160_urb stk1160_urb[STK1160_NUM_BUFS];
 
 	/* current buffer */
 	struct stk1160_buffer *buf;
Signature · 1 line
2.17.1
#4 lore raw

Re: [PATCH 2/3] media: stk1160: move transfer_buffer and urb to same struct 'stk1160_urb'

The overly long lines make this pretty unreadable.

maintainer
#5 lore raw

Re: [PATCH 2/3] media: stk1160: move transfer_buffer and urb to same struct 'stk1160_urb'

In reply to Dafna Hirschfeld

Hi Dafna,

Quoted 17 lines · Dafna Hirschfeld

Instead of having two separated arrays, one for the urbs and one for their buffers, have one array of a struct containing both. In addition, the array is just 16 pointers, no need to dynamically allocate it.

---
 drivers/media/usb/stk1160/stk1160-v4l.c   |  2 +-
 drivers/media/usb/stk1160/stk1160-video.c | 50 ++++++++---------------
 drivers/media/usb/stk1160/stk1160.h       | 11 ++---
 3 files changed, 23 insertions(+), 40 deletions(-)
drivers/media/usb/stk1160/stk1160-v4l.c +0 -0
index 6a4eb616d516..a06030451db4 100644

drivers/media/usb/stk1160/stk1160-v4l.c static int stk1160_start_streaming(struct stk1160 *dev) · line 235

Quoted 5 lines
drivers/media/usb/stk1160/stk1160-v4l.c +1 -1
@@ -232,7 +232,7 @@ static int stk1160_start_streaming(struct stk1160 *dev)
 
 	/* submit urbs and enables IRQ */
 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
-		rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_KERNEL);
+		rc = usb_submit_urb(dev->isoc_ctl.stk1160_urb[i].urb, GFP_KERNEL);

The "stk1160_urb" field is a member of struct stk1160_isoc_ctl, so it's not really needed to name it "stk1160_urb", you could just name it "urb_ctl" or something like that.

I think the result should be saner:

		rc = usb_submit_urb(dev->isoc_ctl.urb_ctl[i].urb,
				    GFP_KERNEL);

Also, please make sure you run some regression tests with this patch alone applied (without 3/3). It's easy to make a mistake on this kind of cleanups.

Thanks,
Ezequiel

#6 lore raw

PATCH 3/3 media: stk1160: use dma_alloc_noncontiguous API

In reply to Dafna Hirschfeld

Replace the urb buffers allocation to
use the noncontiguous API. This improve performance on Arm.
The noncontiguous API require handling synchronization.
This commit is similar to the one sent to
uvc: [1]

[1] https://lkml.org/lkml/2021/3/12/1506

---
 drivers/media/usb/stk1160/stk1160-v4l.c   |   3 +
 drivers/media/usb/stk1160/stk1160-video.c | 109 +++++++++++++---------
 drivers/media/usb/stk1160/stk1160.h       |  10 ++
 3 files changed, 79 insertions(+), 43 deletions(-)
drivers/media/usb/stk1160/stk1160-v4l.c +3 -0
index a06030451db4..31c34734425b 100644
@@ -232,6 +232,9 @@ static int stk1160_start_streaming(struct stk1160 *dev)
 
 	/* submit urbs and enables IRQ */
 	for (i = 0; i < dev->isoc_ctl.num_bufs; i++) {
+		struct stk1160_urb *stk_urb = &dev->isoc_ctl.stk1160_urb[i];
+
+		dma_sync_sgtable_for_device(stk1160_get_dmadev(dev), stk_urb->sgt, DMA_FROM_DEVICE);
 		rc = usb_submit_urb(dev->isoc_ctl.stk1160_urb[i].urb, GFP_KERNEL);
 		if (rc) {
 			stk1160_err("cannot submit urb[%d] (%d)\n", i, rc);
drivers/media/usb/stk1160/stk1160-video.c +66 -43

185 lines not on this page · raw message

drivers/media/usb/stk1160/stk1160.h +10 -0
index 1ffca1343d88..52bea7815ae5 100644
@@ -16,6 +16,8 @@
 #include <media/videobuf2-v4l2.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ctrls.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
 
 #define STK1160_VERSION		"0.9.5"
 #define STK1160_VERSION_NUM	0x000905
@@ -87,6 +89,9 @@ struct stk1160_buffer {
 struct stk1160_urb {
 	struct urb *urb;
 	char *transfer_buffer;
+	struct sg_table *sgt;
+	struct stk1160 *dev;
+	dma_addr_t dma;
 };
 
 struct stk1160_isoc_ctl {
@@ -190,3 +195,8 @@ void stk1160_select_input(struct stk1160 *dev);
 
 /* Provided by stk1160-ac97.c */
 void stk1160_ac97_setup(struct stk1160 *dev);
+
+static inline struct device *stk1160_get_dmadev(struct stk1160 *dev)
+{
+	return bus_to_hcd(dev->udev->bus)->self.sysdev;
+}
Signature · 1 line
2.17.1
#7 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

Quoted 7 lines · Dafna Hirschfeld

Replace the urb buffers allocation to
use the noncontiguous API. This improve performance on Arm.
The noncontiguous API require handling synchronization.
This commit is similar to the one sent to
uvc: [1]

Strange formatting. I'd flow this as:

Replace the urb buffers allocation to use the noncontiguous API. This improve performance on ARM plattform (XXX: insert why here) The noncontiguous API requires the driver to handle synchronization. This commit is similar to this one for the uvc driver:

    https://lkml.org/lkml/2021/3/12/1506

drivers/media/usb/stk1160/stk1160-video.c static void stk1160_isoc_irq(struct urb *urb) · line 315

Quoted 6 lines
drivers/media/usb/stk1160/stk1160-video.c +2 -0
@@ -310,6 +311,9 @@ static void stk1160_isoc_irq(struct urb *urb)
 		return;
 	}
 
+	dma_sync_sgtable_for_cpu(stk1160_get_dmadev(dev), stk_urb->sgt, DMA_FROM_DEVICE);
+	invalidate_kernel_vmap_range(stk_urb->transfer_buffer, urb->transfer_buffer_length);

Besisdes the unreadably long lines, I'd invalidate the vmap range before the direct mapping range.

#8 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

Quoted 9 lines · Christoph Hellwig
Quoted 7 lines · Dafna Hirschfeld

Replace the urb buffers allocation to
use the noncontiguous API. This improve performance on Arm.
The noncontiguous API require handling synchronization.
This commit is similar to the one sent to
uvc: [1]

Quoted 8 lines

Strange formatting. I'd flow this as:

Replace the urb buffers allocation to use the noncontiguous API. This improve performance on ARM plattform (XXX: insert why here) The noncontiguous API requires the driver to handle synchronization. This commit is similar to this one for the uvc driver:

     https://lkml.org/lkml/2021/3/12/1506

I'll reformulate and explain.

Quoted 10 lines

drivers/media/usb/stk1160/stk1160-video.c static void stk1160_isoc_irq(struct urb *urb) · line 315

Quoted 7 lines
drivers/media/usb/stk1160/stk1160-video.c +2 -0
@@ -310,6 +311,9 @@ static void stk1160_isoc_irq(struct urb *urb)
  		return;
  	}
 
+	dma_sync_sgtable_for_cpu(stk1160_get_dmadev(dev), stk_urb->sgt, DMA_FROM_DEVICE);
+	invalidate_kernel_vmap_range(stk_urb->transfer_buffer, urb->transfer_buffer_length);

Besisdes the unreadably long lines, I'd invalidate the vmap range before the direct mapping range.

I'll send v2 with shorter lines. (the official limit is now 100 char which I still follow). I can send v2 with limit of 80.

You mean you would call "invalidate_kernel_vmap_range" before "dma_sync_sgtable_for_cpu" ?

Thanks,
Dafna

Quoted 1 line
#9 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

Quoted 1 line · Dafna Hirschfeld

I'll send v2 with shorter lines. (the official limit is now 100 char which I still follow).

No. It is 80 lines with an exception to go over it if it sigificantly improves readability.

Quoted 1 line

You mean you would call "invalidate_kernel_vmap_range" before "dma_sync_sgtable_for_cpu" ?

Yes.

#10 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

Quoted 5 lines · Christoph Hellwig
Quoted 1 line · Dafna Hirschfeld

I'll send v2 with shorter lines. (the official limit is now 100 char which I still follow).

No. It is 80 lines with an exception to go over it if it sigificantly improves readability.

Ok, didn't know that.

Quoted 4 lines
Quoted 2 lines

You mean you would call "invalidate_kernel_vmap_range" before "dma_sync_sgtable_for_cpu" ?

Yes.

Could you explain why?

Quoted 1 line
#11 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

Quoted 9 lines · Dafna Hirschfeld
Quoted 5 lines · Christoph Hellwig
Quoted 1 line · Dafna Hirschfeld

I'll send v2 with shorter lines. (the official limit is now 100 char which I still follow).

No. It is 80 lines with an exception to go over it if it sigificantly improves readability.

Quoted 8 lines

Ok, didn't know that.

Quoted 4 lines
Quoted 2 lines

You mean you would call "invalidate_kernel_vmap_range" before "dma_sync_sgtable_for_cpu" ?

Yes.

Could you explain why?

the vmap range is the one actually use for cpu access and thus most prone for speculation, so I'd invalidate it first. It probably does not matter to much, but that order looks more sensible.

#12 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

On (22/01/11 09:59), Christoph Hellwig wrote:
[..]

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

You mean you would call "invalidate_kernel_vmap_range" before "dma_sync_sgtable_for_cpu" ?

Yes.

Could you explain why?

the vmap range is the one actually use for cpu access and thus most prone for speculation, so I'd invalidate it first. It probably does not matter to much, but that order looks more sensible.

Shall we do the same in videobuf2?

----

drivers/media/common/videobuf2/videobuf2-dma-contig.c +6 -6
index 7c4096e62173..0e3f264122af 100644
@@ -132,12 +132,12 @@ static void vb2_dc_prepare(void *buf_priv)
 	if (!buf->non_coherent_mem)
 		return;
 
-	/* For both USERPTR and non-coherent MMAP */
-	dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir);
-
 	/* Non-coherent MMAP only */
 	if (buf->vaddr)
 		flush_kernel_vmap_range(buf->vaddr, buf->size);
+
+	/* For both USERPTR and non-coherent MMAP */
+	dma_sync_sgtable_for_device(buf->dev, sgt, buf->dma_dir);
 }
 
 static void vb2_dc_finish(void *buf_priv)
@@ -152,12 +152,12 @@ static void vb2_dc_finish(void *buf_priv)
 	if (!buf->non_coherent_mem)
 		return;
 
-	/* For both USERPTR and non-coherent MMAP */
-	dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir);
-
 	/* Non-coherent MMAP only */
 	if (buf->vaddr)
 		invalidate_kernel_vmap_range(buf->vaddr, buf->size);
+
+	/* For both USERPTR and non-coherent MMAP */
+	dma_sync_sgtable_for_cpu(buf->dev, sgt, buf->dma_dir);
 }
 
 /*********************************************/
maintainer
#13 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

In reply to Dafna Hirschfeld

Hi Dafna,

Very nice work.

I specially like all the testing that you mentioned in the cover-letter.

Back in the day, there were a few users trying to use STK1160 (Easycap) with Beaglebone boards, without success due to lack of USB throughput.

If anything else, I think it's good to see additional noncontiguous API users.

Quoted 10 lines · Dafna Hirschfeld

Replace the urb buffers allocation to
use the noncontiguous API. This improve performance on Arm.
The noncontiguous API require handling synchronization.
This commit is similar to the one sent to
uvc: [1]

[1] https://lkml.org/lkml/2021/3/12/1506

This commit description needs lots of attention. In particular, please add the test results here.

Quoted 7 lines
---
 drivers/media/usb/stk1160/stk1160-v4l.c   |   3 +
 drivers/media/usb/stk1160/stk1160-video.c | 109 +++++++++++++---------
 drivers/media/usb/stk1160/stk1160.h       |  10 ++
 3 files changed, 79 insertions(+), 43 deletions(-)

[..]

drivers/media/usb/stk1160/stk1160-video.c int stk1160_alloc_isoc(struct stk1160 *dev) · line 527

Quoted 6 lines
drivers/media/usb/stk1160/stk1160-video.c +1 -1
@@ -501,7 +524,7 @@ int stk1160_alloc_isoc(struct stk1160 *dev)
 
 free_i_bufs:
 	/* Save the allocated buffers so far, so we can properly free them */
-	dev->isoc_ctl.num_bufs = i+1;
+	dev->isoc_ctl.num_bufs = i;

This looks like a separate fix, similar to 1/3 ?

Quoted 27 lines
drivers/media/usb/stk1160/stk1160-video.c +0 -0
 	stk1160_free_isoc(dev);
 	return -ENOMEM;
 }
drivers/media/usb/stk1160/stk1160.h +5 -0
index 1ffca1343d88..52bea7815ae5 100644
@@ -16,6 +16,8 @@
 #include <media/videobuf2-v4l2.h>
 #include <media/v4l2-device.h>
 #include <media/v4l2-ctrls.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
 
 #define STK1160_VERSION		"0.9.5"
 #define STK1160_VERSION_NUM	0x000905
@@ -87,6 +89,9 @@ struct stk1160_buffer {
 struct stk1160_urb {
 	struct urb *urb;
 	char *transfer_buffer;
+	struct sg_table *sgt;
+	struct stk1160 *dev;
+	dma_addr_t dma;
 };
 
 struct stk1160_isoc_ctl {

drivers/media/usb/stk1160/stk1160.h void stk1160_select_input(struct stk1160 *dev); · line 201

Quoted 7 lines
drivers/media/usb/stk1160/stk1160.h +4 -0
@@ -190,3 +195,8 @@ void stk1160_select_input(struct stk1160 *dev);
 
 /* Provided by stk1160-ac97.c */
 void stk1160_ac97_setup(struct stk1160 *dev);
+
+static inline struct device *stk1160_get_dmadev(struct stk1160 *dev)
+{
+	return bus_to_hcd(dev->udev->bus)->self.sysdev;

This function looks truly horrible, is there no other way to get the device ?

I suppose this function return something different than (struct stk1160*)dev->dev ?

Thanks,
Ezequiel

#14 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

Quoted 8 lines · Ezequiel Garcia

Hi Dafna,

Very nice work.

I specially like all the testing that you mentioned in the cover-letter.

Back in the day, there were a few users trying to use STK1160 (Easycap) with Beaglebone boards, without success due to lack of USB throughput.

Hi, what do you mean by "USB throughput" ?
I see that the FPS does not change with this patchset and stands on about 25 frames/sec So this patchset only improve cpu time.

Quoted 72 lines

If anything else, I think it's good to see additional noncontiguous API users.

Quoted 10 lines · Dafna Hirschfeld

Replace the urb buffers allocation to
use the noncontiguous API. This improve performance on Arm.
The noncontiguous API require handling synchronization.
This commit is similar to the one sent to
uvc: [1]

[1] https://lkml.org/lkml/2021/3/12/1506

This commit description needs lots of attention. In particular, please add the test results here.

Quoted 7 lines
---
  drivers/media/usb/stk1160/stk1160-v4l.c   |   3 +
  drivers/media/usb/stk1160/stk1160-video.c | 109 +++++++++++++---------
  drivers/media/usb/stk1160/stk1160.h       |  10 ++
  3 files changed, 79 insertions(+), 43 deletions(-)

[..]

drivers/media/usb/stk1160/stk1160-video.c int stk1160_alloc_isoc(struct stk1160 *dev) · line 527

Quoted 6 lines
drivers/media/usb/stk1160/stk1160-video.c +1 -1
@@ -501,7 +524,7 @@ int stk1160_alloc_isoc(struct stk1160 *dev)
 
  free_i_bufs:
  	/* Save the allocated buffers so far, so we can properly free them */
-	dev->isoc_ctl.num_bufs = i+1;
+	dev->isoc_ctl.num_bufs = i;

This looks like a separate fix, similar to 1/3 ?

Quoted 27 lines
drivers/media/usb/stk1160/stk1160-video.c +0 -0
  	stk1160_free_isoc(dev);
  	return -ENOMEM;
  }
drivers/media/usb/stk1160/stk1160.h +5 -0
index 1ffca1343d88..52bea7815ae5 100644
@@ -16,6 +16,8 @@
  #include <media/videobuf2-v4l2.h>
  #include <media/v4l2-device.h>
  #include <media/v4l2-ctrls.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
 
  #define STK1160_VERSION		"0.9.5"
  #define STK1160_VERSION_NUM	0x000905
@@ -87,6 +89,9 @@ struct stk1160_buffer {
  struct stk1160_urb {
  	struct urb *urb;
  	char *transfer_buffer;
+	struct sg_table *sgt;
+	struct stk1160 *dev;
+	dma_addr_t dma;
  };
 
  struct stk1160_isoc_ctl {

drivers/media/usb/stk1160/stk1160.h void stk1160_select_input(struct stk1160 *dev); · line 201

Quoted 7 lines
drivers/media/usb/stk1160/stk1160.h +4 -0
@@ -190,3 +195,8 @@ void stk1160_select_input(struct stk1160 *dev);
 
  /* Provided by stk1160-ac97.c */
  void stk1160_ac97_setup(struct stk1160 *dev);
+
+static inline struct device *stk1160_get_dmadev(struct stk1160 *dev)
+{
+	return bus_to_hcd(dev->udev->bus)->self.sysdev;

This function looks truly horrible, is there no other way to get the device ?

Quoted 1 line

I suppose this function return something different than (struct stk1160*)dev->dev ?

I remember I tried using this device and got an error that it is not cable of doing dma allocations or something like that. The function "stk1160_get_dmadev" is a copy-paste from the uvc equivalent:

static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream)

{
        return bus_to_hcd(stream->dev->udev->bus)->self.sysdev;
}

Thanks,
Dafna

Quoted 4 lines

Thanks,
Ezequiel

#15 lore raw

Re: [PATCH 3/3] media: stk1160: use dma_alloc_noncontiguous API

Quoted 90 lines · Dafna Hirschfeld
Quoted 8 lines · Ezequiel Garcia

Hi Dafna,

Very nice work.

I specially like all the testing that you mentioned in the cover-letter.

Back in the day, there were a few users trying to use STK1160 (Easycap) with Beaglebone boards, without success due to lack of USB throughput.

Hi, what do you mean by "USB throughput" ?
I see that the FPS does not change with this patchset and stands on about 25 frames/sec So this patchset only improve cpu time.

Quoted 68 lines

If anything else, I think it's good to see additional noncontiguous API users.

Quoted 10 lines · Dafna Hirschfeld

Replace the urb buffers allocation to
use the noncontiguous API. This improve performance on Arm.
The noncontiguous API require handling synchronization.
This commit is similar to the one sent to
uvc: [1]

[1] https://lkml.org/lkml/2021/3/12/1506

This commit description needs lots of attention. In particular, please add the test results here.

Quoted 7 lines

---
  drivers/media/usb/stk1160/stk1160-v4l.c   |   3 + drivers/media/usb/stk1160/stk1160-video.c | 109 +++++++++++++--------- drivers/media/usb/stk1160/stk1160.h       |  10 ++

  3 files changed, 79 insertions(+), 43 deletions(-)

[..]

Quoted 5 lines
(quoted diff) +0 -0
@@ -501,7 +524,7 @@ int stk1160_alloc_isoc(struct stk1160 *dev)

  free_i_bufs:
      /* Save the allocated buffers so far, so we can properly free them */ -    dev->isoc_ctl.num_bufs = i+1;
+    dev->isoc_ctl.num_bufs = i;

This looks like a separate fix, similar to 1/3 ?

Quoted 25 lines

      stk1160_free_isoc(dev);
      return -ENOMEM;
  }

drivers/media/usb/stk1160/stk1160.h +5 -0
index 1ffca1343d88..52bea7815ae5 100644
@@ -16,6 +16,8 @@
   #include <media/videobuf2-v4l2.h>
   #include <media/v4l2-device.h>
   #include <media/v4l2-ctrls.h>
+#include <linux/usb.h>
+#include <linux/usb/hcd.h>
   #define STK1160_VERSION        "0.9.5"
   #define STK1160_VERSION_NUM    0x000905
@@ -87,6 +89,9 @@ struct stk1160_buffer {
   struct stk1160_urb {
       struct urb *urb;
       char *transfer_buffer;
+    struct sg_table *sgt;
+    struct stk1160 *dev;
+    dma_addr_t dma;
   };
   struct stk1160_isoc_ctl {

drivers/media/usb/stk1160/stk1160.h void stk1160_select_input(struct stk1160 *dev); · line 200

Quoted 6 lines
drivers/media/usb/stk1160/stk1160.h +4 -0
@@ -190,3 +195,8 @@ void stk1160_select_input(struct stk1160 *dev);
   /* Provided by stk1160-ac97.c */
   void stk1160_ac97_setup(struct stk1160 *dev);
+
+static inline struct device *stk1160_get_dmadev(struct stk1160 *dev)
+{
+    return bus_to_hcd(dev->udev->bus)->self.sysdev;

This function looks truly horrible, is there no other way to get the device ?

Quoted 1 line

I suppose this function return something different than (struct stk1160*)dev->dev ?

I remember I tried using this device and got an error that it is not cable of doing dma allocations or something like that. The function "stk1160_get_dmadev" is a copy-paste from the uvc equivalent:

Quoted 7 lines

static inline struct device *uvc_stream_to_dmadev(struct uvc_streaming *stream) {
        return bus_to_hcd(stream->dev->udev->bus)->self.sysdev;
}

Thanks,
Dafna

There are 3 'struct device' related, when printing their names with:

        pr_err("dmadev device is %s\n", dev_name(stk1160_get_dmadev(dev)));
        pr_err("udev->dev device is %s\n", dev_name(&dev->udev->dev));
        pr_err("dev device is %s\n", dev_name(dev->dev));

I get:

[  107.770870] dmadev device is fe900000.usb
[  107.771253] udev->dev device is 7-1
[  107.771561] dev device is 7-1:1.0

If I try to use 'dev->dev' instead of the above stk1160_get_dmadev, streaming fails and I get two warnings. The first one is originated from:

https://elixir.bootlin.com/linux/v5.16-rc4/source/kernel/dma/mapping.c#L546

Which is:
WARN_ON_ONCE(!dev->coherent_dma_mask)

I wonder if that warning is required in our case since we remove the alloc_coherent usage.

The second warning in videobuf2-core.c:1612 seems to be a bug in the driver itself. I'll look at that.

[   65.554406] ------------[ cut here ]------------
[   65.554815] WARNING: CPU: 1 PID: 593 at /home/dafna/git/easycap_media_tree/kernel/dma/mapping.c:546 __dma_alloc_pages+0x78/0xc8
[   65.555836] Modules linked in: snd_usb_audio snd_hwdep snd_usbmidi_lib snd_rawmidi snd_soc_hdmi_codec dw_hdmi_i2s_audio saa7115 stk1160 videobuf2_vmalloc videobuf2_memops videobuf2_v4l2 videobuf2_common videodev mc crct10dif_ce panfrost snd_soc_simple_card snd_soc_audio_graph_card snd_soc_spdif_tx snd_soc_simple_card_utils gpu_sched phy_rockchip_pcie snd_soc_rockchip_i2s rockchipdrm analogix_dp dw_mipi_dsi dw_hdmi cec drm_kms_helper drm rtc_rk808 rockchip_saradc industrialio_triggered_buffer kfifo_buf rockchip_thermal pcie_rockchip_host ip_tables x_tables ipv6
[   65.560265] CPU: 1 PID: 593 Comm: v4l2src0:src Not tainted 5.16.0-rc4-62408-g32447129cb30-dirty #14
[   65.561269] Hardware name: Radxa ROCK Pi 4B (DT)
[   65.561676] pstate: 40000005 (nZcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   65.562288] pc : __dma_alloc_pages+0x78/0xc8
[   65.562849] lr : dma_alloc_noncontiguous+0x128/0x1a8
[   65.563288] sp : ffff800012bc39b0
[   65.563579] x29: ffff800012bc39b0 x28: 0000000000000000 x27: 0000000000030000
[   65.564213] x26: ffff00000908a950 x25: ffff0000018af830 x24: 0000000000410080
[   65.564843] x23: 0000000000000002 x22: ffff000005c88090 x21: 0000000000000cc0
[   65.565473] x20: ffff0000018af830 x19: 0000000000030000 x18: ffffffffffffffff
[   65.566103] x17: 000000040044ffff x16: 00400032b5503510 x15: ffff800011323f78
[   65.566735] x14: 0000000000000000 x13: 302e313a312d3720 x12: 7369206563697665
[   65.567366] x11: 0000000005f5e0ff x10: 000000000000005d x9 : 00000000ffffffd0
[   65.567997] x8 : ffff000005c880a0 x7 : 0000000000000000 x6 : ffff800012bc3750
[   65.568628] x5 : ffff800011b671f8 x4 : 0000000000000cc0 x3 : 0000000000000002
[   65.569259] x2 : ffff000005c88090 x1 : 0000000000030000 x0 : 0000000000000000
[   65.569891] Call trace:
[   65.570110]  __dma_alloc_pages+0x78/0xc8
[   65.570459]  dma_alloc_noncontiguous+0x128/0x1a8
[   65.570868]  stk1160_alloc_isoc+0xf0/0x2b0 [stk1160]
[   65.571318]  start_streaming+0xfc/0x250 [stk1160]
[   65.571740]  vb2_start_streaming+0x6c/0x160 [videobuf2_common]
[   65.572273]  vb2_core_streamon+0x17c/0x1a8 [videobuf2_common]
[   65.572794]  vb2_streamon+0x54/0x88 [videobuf2_v4l2]
[   65.573244]  vb2_ioctl_streamon+0x54/0x60 [videobuf2_v4l2]
[   65.573736]  v4l_streamon+0x3c/0x50 [videodev]
[   65.574195]  __video_do_ioctl+0x1a4/0x428 [videodev]
[   65.574684]  video_usercopy+0x320/0x828 [videodev]
[   65.575158]  video_ioctl2+0x3c/0x58 [videodev]
[   65.575603]  v4l2_ioctl+0x60/0x90 [videodev]
[   65.576031]  __arm64_sys_ioctl+0xa8/0xe0
[   65.576381]  invoke_syscall+0x54/0x118
[   65.576718]  el0_svc_common.constprop.3+0x84/0x100
[   65.577144]  do_el0_svc+0x34/0xa0
[   65.577441]  el0_svc+0x1c/0x50
[   65.577716]  el0t_64_sync_handler+0x88/0xb0
[   65.578086]  el0t_64_sync+0x16c/0x170
[   65.578413] ---[ end trace 578e0ba07742170c ]---
[   65.583222] ------------[ cut here ]------------
[   65.583633] WARNING: CPU: 5 PID: 593 at /home/dafna/git/easycap_media_tree/drivers/media/common/videobuf2/videobuf2-core.c:1612 vb2_start_streaming+0xd4/0x160 [videobuf2_common]
[   65.585027] Modules linked in: snd_usb_audio snd_hwdep snd_usbmidi_lib snd_rawmidi snd_soc_hdmi_codec dw_hdmi_i2s_audio saa7115 stk1160 videobuf2_vmalloc videobuf2_memops videobuf2_v4l2 videobuf2_common videodev mc crct10dif_ce panfrost snd_soc_simple_card snd_soc_audio_graph_card snd_soc_spdif_tx snd_soc_simple_card_utils gpu_sched phy_rockchip_pcie snd_soc_rockchip_i2s rockchipdrm analogix_dp dw_mipi_dsi dw_hdmi cec drm_kms_helper drm rtc_rk808 rockchip_saradc industrialio_triggered_buffer kfifo_buf rockchip_thermal pcie_rockchip_host ip_tables x_tables ipv6
[   65.589383] CPU: 5 PID: 593 Comm: v4l2src0:src Tainted: G        W         5.16.0-rc4-62408-g32447129cb30-dirty #14
[   65.590293] Hardware name: Radxa ROCK Pi 4B (DT)
[   65.590696] pstate: 80000005 (Nzcv daif -PAN -UAO -TCO -DIT -SSBS BTYPE=--)
[   65.591304] pc : vb2_start_streaming+0xd4/0x160 [videobuf2_common]
[   65.591850] lr : vb2_start_streaming+0x6c/0x160 [videobuf2_common]
[   65.592395] sp : ffff800012bc3ad0
[   65.592685] x29: ffff800012bc3ad0 x28: 0000000000000000 x27: ffff800012bc3cd8
[   65.593312] x26: 0000000000000000 x25: ffff00000d8a7800 x24: 0000000040045612
[   65.593938] x23: ffff800011323000 x22: ffff800012bc3cd8 x21: ffff00000908a8b0
[   65.594562] x20: ffff00000908a8c8 x19: 00000000fffffff4 x18: ffffffffffffffff
[   65.595188] x17: 000000040044ffff x16: 00400034b5503510 x15: ffff800011323f78
[   65.595813] x14: ffff000013163886 x13: ffff000013163885 x12: 00000000000002ce
[   65.596439] x11: 0000000000000028 x10: 0000000000000001 x9 : 0000000000000228
[   65.597064] x8 : 0101010101010101 x7 : 7f7f7f7f7f7f7f7f x6 : fefefeff726c5e78
[   65.597690] x5 : ffff800012bc3990 x4 : 0000000000000000 x3 : ffff000009a34880
[   65.598315] x2 : 0000000000000000 x1 : 0000000000000000 x0 : ffff000007cd99f0
[   65.598940] Call trace:
[   65.599155]  vb2_start_streaming+0xd4/0x160 [videobuf2_common]
[   65.599672]  vb2_core_streamon+0x17c/0x1a8 [videobuf2_common]
[   65.600179]  vb2_streamon+0x54/0x88 [videobuf2_v4l2]
[   65.600619]  vb2_ioctl_streamon+0x54/0x60 [videobuf2_v4l2]
[   65.601103]  v4l_streamon+0x3c/0x50 [videodev]
[   65.601521]  __video_do_ioctl+0x1a4/0x428 [videodev]
[   65.601977]  video_usercopy+0x320/0x828 [videodev]
[   65.602419]  video_ioctl2+0x3c/0x58 [videodev]
[   65.602830]  v4l2_ioctl+0x60/0x90 [videodev]
[   65.603227]  __arm64_sys_ioctl+0xa8/0xe0
[   65.603576]  invoke_syscall+0x54/0x118
[   65.603911]  el0_svc_common.constprop.3+0x84/0x100
[   65.604332]  do_el0_svc+0x34/0xa0
[   65.604625]  el0_svc+0x1c/0x50
[   65.604897]  el0t_64_sync_handler+0x88/0xb0
[   65.605264]  el0t_64_sync+0x16c/0x170
[   65.605587] ---[ end trace 578e0ba07742170d ]---

Thanks,
Dafna

Quoted 6 lines
Quoted 5 lines

Thanks,
Ezequiel