Skip to contentSkip to conversation

media: staging: rkisp1: various bug fixes in params

PATCH Superseded Dafna Hirschfeld 5 participants 19 messages Jun 25, 2020 – Aug 14, 2020

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover media: staging: rkisp1: various bug fixes in params

3 bug fixes found in rkisp1-params.c related to managing the vb2 buffers.

Testing:
--------

A test that streams the selfpath and params entities simultaneously can be found in this branch of v4l-utils:
https://gitlab.collabora.com/dafna/v4l-utils/-/tree/params-bug-fixes

In the repo:

$ cd contrib/test
$ python3 test-rkisp1.py -t RKISP1_TEST_simple_stream_with_params -o /root -S -v --params-buf params-buf.dat

Dafna Hirschfeld (3):
media: staging: rkisp1: params: don't reference the vb2 buffer after
calling vb2_buffer_done
media: staging: rkisp1: params: don't release lock in isr before
buffer is done
media: staging: rkisp1: params: in 'stop_streaming' don't release the
lock while returning buffers

 drivers/staging/media/rkisp1/rkisp1-params.c | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/3 media: staging: rkisp1: params: don't reference the vb2 buffer after calling vb2_buffer_done

The driver should not reference the buffer pointer of vb2_buffer after calling 'vb2_buffer_done' on that buffer since the call passes the buffer to userspace.

---
 drivers/staging/media/rkisp1/rkisp1-params.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/rkisp1/rkisp1-params.c +1 -1
index 797e79de659c..762c2259b807 100644
@@ -1457,9 +1457,9 @@ static void rkisp1_params_vb2_buf_queue(struct vb2_buffer *vb)
 		new_params = (struct rkisp1_params_cfg *)
 			(vb2_plane_vaddr(vb, 0));
 		vbuf->sequence = frame_sequence;
-		vb2_buffer_done(&params_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 		params->is_first_params = false;
 		params->cur_params = *new_params;
+		vb2_buffer_done(&params_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 		return;
 	}
 
Signature · 1 line
2.17.1
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] media: staging: rkisp1: params: don't reference the vb2 buffer after calling vb2_buffer_done

Hi Dafna,

Thanks for your patch.

Quoted 14 lines · Dafna Hirschfeld

The driver should not reference the buffer pointer of vb2_buffer after calling 'vb2_buffer_done' on that buffer since the call passes the buffer to userspace.

---
 drivers/staging/media/rkisp1/rkisp1-params.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -0
index 797e79de659c..762c2259b807 100644

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_buf_queue(struct vb2_buffer *vb) · line 1461

Quoted 6 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -1
@@ -1457,9 +1457,9 @@ static void rkisp1_params_vb2_buf_queue(struct vb2_buffer *vb)
 		new_params = (struct rkisp1_params_cfg *)
 			(vb2_plane_vaddr(vb, 0));
 		vbuf->sequence = frame_sequence;
-		vb2_buffer_done(&params_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 		params->is_first_params = false;
 		params->cur_params = *new_params;

Maybe we can remove this new_params variable entirely, and just copy directly to params->cur_params in the begining of the if statement, what do yo think?

With or without this change

Thanks
Helen

Quoted 4 lines
drivers/staging/media/rkisp1/rkisp1-params.c +1 -0
+		vb2_buffer_done(&params_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 		return;>  	}
 
 
#3 lore raw

PATCH 2/3 media: staging: rkisp1: params: don't release lock in isr before buffer is done

In reply to Dafna Hirschfeld

In the irq handler 'rkisp1_params_isr', the lock 'config_lock' should be held as long as the current buffer is used. Otherwise the stop_streaming calback might remove it from the list and pass it to userspace while it is referenced in the irq handler.

---
 drivers/staging/media/rkisp1/rkisp1-params.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +4 -5
index 762c2259b807..bf006dbeee2d 100644
@@ -1210,10 +1210,11 @@ void rkisp1_params_isr(struct rkisp1_device *rkisp1, u32 isp_mis)
 	if (!list_empty(&params->params))
 		cur_buf = list_first_entry(&params->params,
 					   struct rkisp1_buffer, queue);
-	spin_unlock(&params->config_lock);
 
-	if (!cur_buf)
+	if (!cur_buf) {
+		spin_unlock(&params->config_lock);
 		return;
+	}
 
 	new_params = (struct rkisp1_params_cfg *)(cur_buf->vaddr[0]);
 
@@ -1228,13 +1229,11 @@ void rkisp1_params_isr(struct rkisp1_device *rkisp1, u32 isp_mis)
 		isp_ctrl |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD;
 		rkisp1_write(params->rkisp1, isp_ctrl, RKISP1_CIF_ISP_CTRL);
 
-		spin_lock(&params->config_lock);
 		list_del(&cur_buf->queue);
-		spin_unlock(&params->config_lock);
-
 		cur_buf->vb.sequence = frame_sequence;
 		vb2_buffer_done(&cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 	}
+	spin_unlock(&params->config_lock);
 }
 
 static const struct rkisp1_cif_isp_awb_meas_config rkisp1_awb_params_default_config = {
Signature · 1 line
2.17.1
#4 lore raw

Re: [PATCH 2/3] media: staging: rkisp1: params: don't release lock in isr before buffer is done

Hi Dafna,

Thanks for the patch

Quoted 6 lines · Dafna Hirschfeld

In the irq handler 'rkisp1_params_isr', the lock 'config_lock' should be held as long as the current buffer is used. Otherwise the stop_streaming calback might remove it from the list and pass it to userspace while it is referenced in the irq handler.

I think we need to clarify what 'config_lock' protects, it seems it protects the is_streaming variable and the buffer list.
I see it being used by rkisp1_params_config_parameter(), which doesn't look right to me.

Quoted 23 lines
---
 drivers/staging/media/rkisp1/rkisp1-params.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +3 -2
index 762c2259b807..bf006dbeee2d 100644
@@ -1210,10 +1210,11 @@ void rkisp1_params_isr(struct rkisp1_device *rkisp1, u32 isp_mis)
 	if (!list_empty(&params->params))
 		cur_buf = list_first_entry(&params->params,
 					   struct rkisp1_buffer, queue);
-	spin_unlock(&params->config_lock);
 
-	if (!cur_buf)
+	if (!cur_buf) {
+		spin_unlock(&params->config_lock);
 		return;
+	}
 
 	new_params = (struct rkisp1_params_cfg *)(cur_buf->vaddr[0]);
 

drivers/staging/media/rkisp1/rkisp1-params.c void rkisp1_params_isr(struct rkisp1_device *rkisp1, u32 isp_mis) · line 1235

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -3
@@ -1228,13 +1229,11 @@ void rkisp1_params_isr(struct rkisp1_device *rkisp1, u32 isp_mis)
 		isp_ctrl |= RKISP1_CIF_ISP_CTRL_ISP_CFG_UPD;
 		rkisp1_write(params->rkisp1, isp_ctrl, RKISP1_CIF_ISP_CTRL);
 
-		spin_lock(&params->config_lock);
 		list_del(&cur_buf->queue);
-		spin_unlock(&params->config_lock);
-
 		cur_buf->vb.sequence = frame_sequence;
 		vb2_buffer_done(&cur_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 	}

Maybe we could refactor this whole function, as mentioned by Robin in patch 3/3, we could remove this identation with:

if (!(isp_mis & RKISP1_CIF_ISP_FRAME))

    return;

Keep in mind that params and stats were barely touched from the original driver, so don't be afraid to refactor things :)

Thanks
Helen

Quoted 5 lines
drivers/staging/media/rkisp1/rkisp1-params.c +1 -0
+	spin_unlock(&params->config_lock);
 }
 
 static const struct rkisp1_cif_isp_awb_meas_config rkisp1_awb_params_default_config = {
#5 lore raw

PATCH 3/3 media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

In reply to Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
 drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +1 -6
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
 	/* stop params input firstly */
 	spin_lock_irqsave(&params->config_lock, flags);
 	params->is_streaming = false;
-	spin_unlock_irqrestore(&params->config_lock, flags);
 
 	for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-		spin_lock_irqsave(&params->config_lock, flags);
 		if (!list_empty(&params->params)) {
 			buf = list_first_entry(&params->params,
 					       struct rkisp1_buffer, queue);
 			list_del(&buf->queue);
-			spin_unlock_irqrestore(&params->config_lock,
-					       flags);
 		} else {
-			spin_unlock_irqrestore(&params->config_lock,
-					       flags);
 			break;
 		}
 
@@ -1508,6 +1502,7 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
 			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 		buf = NULL;
 	}
+	spin_unlock_irqrestore(&params->config_lock, flags);
 }
 
 static int
Signature · 1 line
2.17.1
#6 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
  drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
  1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
  	/* stop params input firstly */
  	spin_lock_irqsave(&params->config_lock, flags);
  	params->is_streaming = false;
-	spin_unlock_irqrestore(&params->config_lock, flags);
 
  	for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-		spin_lock_irqsave(&params->config_lock, flags);
  		if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
  			buf = list_first_entry(&params->params,
  					       struct rkisp1_buffer, queue);
  			list_del(&buf->queue);
-			spin_unlock_irqrestore(&params->config_lock,
-					       flags);
  		} else {
-			spin_unlock_irqrestore(&params->config_lock,
-					       flags);
  			break;
  		}

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

	if (!x) {
		//do stuff
	} else {
		break;
	}

it would be better as:

	if (x)
		break;
	//do stuff

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

	list_for_each_entry_safe(&params->params, ..., buf) {
		list_del(&buf->queue);
		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
	}

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Robin.

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +1 -0
@@ -1508,6 +1502,7 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
  			vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  		buf = NULL;
  	}
+	spin_unlock_irqrestore(&params->config_lock, flags);
  }
 
  static int
#7 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Quoted 58 lines · Robin Murphy

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
  drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
  1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
      /* stop params input firstly */
      spin_lock_irqsave(&params->config_lock, flags);
      params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
      for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
              if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                      buf = list_first_entry(&params->params,
                                             struct rkisp1_buffer, queue);
                      list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
              } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                      break;
              }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

        if (!x) {
                //do stuff
        } else {
                break;
        }

it would be better as:

        if (x)
                break;
        //do stuff

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

Quoted 7 lines
        list_for_each_entry_safe(&params->params, ..., buf) {
                list_del(&buf->queue);
                vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
        }

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of params->params to a local list_head under the spinlock, release it and then loop over the local head. As a side effect, one could even drop list_del() and switch to the non-safe variant of list_for_each_entry().

Best regards,
Tomasz

#8 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Quoted 55 lines · Tomasz Figa
Quoted 54 lines · Robin Murphy

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
   drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
   1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
       /* stop params input firstly */
       spin_lock_irqsave(&params->config_lock, flags);
       params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
       for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
               if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                       buf = list_first_entry(&params->params,
                                              struct rkisp1_buffer, queue);
                       list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
               } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                       break;
               }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

         if (!x) {
                 //do stuff
         } else {
                 break;
         }

it would be better as:

         if (x)
                 break;
         //do stuff
Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

         list_for_each_entry_safe(&params->params, ..., buf) {
                 list_del(&buf->queue);
                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
         }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 6 lines
Quoted 3 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Thanks,
Dafna

Quoted 8 lines

params->params to a local list_head under the spinlock, release it and then loop over the local head. As a side effect, one could even drop list_del() and switch to the non-safe variant of list_for_each_entry().

Best regards,
Tomasz

#9 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Quoted 72 lines · Dafna Hirschfeld
Quoted 55 lines · Tomasz Figa
Quoted 54 lines · Robin Murphy

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
   drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
   1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
       /* stop params input firstly */
       spin_lock_irqsave(&params->config_lock, flags);
       params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
       for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
               if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                       buf = list_first_entry(&params->params,
                                              struct rkisp1_buffer, queue);
                       list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
               } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                       break;
               }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

         if (!x) {
                 //do stuff
         } else {
                 break;
         }

it would be better as:

         if (x)
                 break;
         //do stuff
Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

         list_for_each_entry_safe(&params->params, ..., buf) {
                 list_del(&buf->queue);
                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
         }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Best regards,
Tomasz

#10 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Quoted 83 lines · Tomasz Figa
Quoted 72 lines · Dafna Hirschfeld
Quoted 55 lines · Tomasz Figa
Quoted 54 lines · Robin Murphy

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
    drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
    1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
        /* stop params input firstly */
        spin_lock_irqsave(&params->config_lock, flags);
        params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
        for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
                if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                        buf = list_first_entry(&params->params,
                                               struct rkisp1_buffer, queue);
                        list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                        break;
                }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

          if (!x) {
                  //do stuff
          } else {
                  break;
          }

it would be better as:

          if (x)
                  break;
          //do stuff
Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

          list_for_each_entry_safe(&params->params, ..., buf) {
                  list_del(&buf->queue);
                  vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
          }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Quoted 6 lines

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Right, with the way we handle interrupt affinity on Arm an IRQ can't target multiple CPUs in hardware, so any time spent with IRQs disabled might be preventing other devices' interrupts from being taken even if they're not explicitly affine to the current CPU.

Now that I've looked, it appears that vb2_buffer_done() might end up performing a DMA sync on the buffers, which, if it has to do order-of-megabytes worth of cache maintenance for large frames, is the kind of relatively slow operation that really doesn't want to be done with IRQs disabled (or under a lock at all, ideally) unless it absolutely *has* to be. If the lock is only needed here to protect modifications to the params list itself, then moving the whole list at once to do the cleanup "offline" sounds like a great idea to me.

Robin.

#11 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Am 26.06.20 um 18:58 schrieb Robin Murphy:

Quoted 91 lines · Tomasz Figa
Quoted 83 lines
Quoted 72 lines · Dafna Hirschfeld
Quoted 55 lines · Tomasz Figa
Quoted 54 lines · Robin Murphy

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
    drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------

    1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
         /* stop params input firstly */
         spin_lock_irqsave(&params->config_lock, flags);
         params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
         for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
                 if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                         buf = list_first_entry(&params->params,
                                                struct rkisp1_buffer, queue);
                         list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                 } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                         break;
                 }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

          if (!x) {
                  //do stuff
          } else {
                  break;
          }

it would be better as:

          if (x)
                  break;
          //do stuff

Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

          list_for_each_entry_safe(&params->params, ..., buf) { list_del(&buf->queue); vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Quoted 6 lines

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Quoted 3 lines

Right, with the way we handle interrupt affinity on Arm an IRQ can't target multiple CPUs in hardware, so any time spent with IRQs disabled might be preventing other devices' interrupts from being taken even if they're not explicitly affine to the current CPU.

Now that I've looked, it appears that vb2_buffer_done() might end up performing a DMA sync on the buffers, which, if it has to do order-of-megabytes worth of cache maintenance for large frames, is the kind of relatively slow operation that really doesn't want to be done with IRQs disabled (or under a lock at all, ideally) unless it absolutely *has* to be. If the lock is only needed here to protect modifications to the params list itself, then moving the whole list at once to do the cleanup "offline" sounds like a great idea to me.

ok, that might be a problem in v4l2 in general since vb2_buffer_done is actually often used inside an irq handler

Quoted 2 lines

Robin.

#12 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Hello,

Quoted 98 lines · Dafna Hirschfeld

Am 26.06.20 um 18:58 schrieb Robin Murphy:

Quoted 97 lines · Tomasz Figa
Quoted 76 lines · Dafna Hirschfeld
Quoted 66 lines · Tomasz Figa
Quoted 52 lines · Robin Murphy
Quoted 51 lines · Dafna Hirschfeld
Quoted 24 lines

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
    drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------

    1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
         /* stop params input firstly */
         spin_lock_irqsave(&params->config_lock, flags);
         params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
         for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
                 if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                         buf = list_first_entry(&params->params,
                                                struct rkisp1_buffer, queue);
                         list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                 } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                         break;
                 }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

          if (!x) {
                  //do stuff
          } else {
                  break;
          }

it would be better as:

          if (x)
                  break;
          //do stuff

Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

          list_for_each_entry_safe(&params->params, ..., buf) { list_del(&buf->queue); vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Quoted 6 lines

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Right, with the way we handle interrupt affinity on Arm an IRQ can't target multiple CPUs in hardware, so any time spent with IRQs disabled might be preventing other devices' interrupts from being taken even if they're not explicitly affine to the current CPU.

Now that I've looked, it appears that vb2_buffer_done() might end up performing a DMA sync on the buffers, which, if it has to do order-of-megabytes worth of cache maintenance for large frames, is the kind of relatively slow operation that really doesn't want to be done with IRQs disabled (or under a lock at all, ideally) unless it absolutely *has* to be. If the lock is only needed here to protect modifications to the params list itself, then moving the whole list at once to do the cleanup "offline" sounds like a great idea to me.

Ouch.

Quoted 2 lines

ok, that might be a problem in v4l2 in general since vb2_buffer_done is actually often used inside an irq handler

Correct. The DMA sync should be moved to DQBUF time, there shouldn't be any reason to do it in the IRQ handler. I thought this had already been fixed :-(

Signature · 3 lines
Regards,

Laurent Pinchart
#13 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Quoted 103 lines · Laurent Pinchart

Hello,

Quoted 98 lines · Dafna Hirschfeld

Am 26.06.20 um 18:58 schrieb Robin Murphy:

Quoted 97 lines · Tomasz Figa
Quoted 76 lines · Dafna Hirschfeld
Quoted 66 lines · Tomasz Figa
Quoted 52 lines · Robin Murphy
Quoted 51 lines · Dafna Hirschfeld
Quoted 24 lines

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
    drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
    1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
        /* stop params input firstly */
        spin_lock_irqsave(&params->config_lock, flags);
        params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
        for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
                if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                        buf = list_first_entry(&params->params,
                                               struct rkisp1_buffer, queue);
                        list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                        break;
                }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

          if (!x) {
                  //do stuff
          } else {
                  break;
          }

it would be better as:

          if (x)
                  break;
          //do stuff
Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

          list_for_each_entry_safe(&params->params, ..., buf) {
                  list_del(&buf->queue);
                  vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
          }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Quoted 6 lines

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Right, with the way we handle interrupt affinity on Arm an IRQ can't target multiple CPUs in hardware, so any time spent with IRQs disabled might be preventing other devices' interrupts from being taken even if they're not explicitly affine to the current CPU.

Now that I've looked, it appears that vb2_buffer_done() might end up performing a DMA sync on the buffers, which, if it has to do order-of-megabytes worth of cache maintenance for large frames, is the kind of relatively slow operation that really doesn't want to be done with IRQs disabled (or under a lock at all, ideally) unless it absolutely *has* to be. If the lock is only needed here to protect modifications to the params list itself, then moving the whole list at once to do the cleanup "offline" sounds like a great idea to me.

Quoted 8 lines

Ouch.

Quoted 2 lines

ok, that might be a problem in v4l2 in general since vb2_buffer_done is actually often used inside an irq handler

Correct. The DMA sync should be moved to DQBUF time, there shouldn't be any reason to do it in the IRQ handler. I thought this had already been fixed :-(

For reference, there was a patch [1] proposed, but it moved the synchronization to a wrong place in the sequence, already after the .buf_finish queue callback, ending up breaking the drivers which need to access the buffer contents there.

[1] https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

Best regards,
Tomasz

#14 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Hi Tomasz,

Quoted 110 lines · Tomasz Figa
Quoted 100 lines · Laurent Pinchart
Quoted 98 lines · Dafna Hirschfeld

Am 26.06.20 um 18:58 schrieb Robin Murphy:

Quoted 97 lines · Tomasz Figa
Quoted 76 lines · Dafna Hirschfeld
Quoted 66 lines · Tomasz Figa
Quoted 52 lines · Robin Murphy
Quoted 51 lines · Dafna Hirschfeld
Quoted 24 lines

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
    drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
    1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
        /* stop params input firstly */
        spin_lock_irqsave(&params->config_lock, flags);
        params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
        for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
                if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                        buf = list_first_entry(&params->params,
                                               struct rkisp1_buffer, queue);
                        list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                        break;
                }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

          if (!x) {
                  //do stuff
          } else {
                  break;
          }

it would be better as:

          if (x)
                  break;
          //do stuff
Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

          list_for_each_entry_safe(&params->params, ..., buf) {
                  list_del(&buf->queue);
                  vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
          }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Quoted 6 lines

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Right, with the way we handle interrupt affinity on Arm an IRQ can't target multiple CPUs in hardware, so any time spent with IRQs disabled might be preventing other devices' interrupts from being taken even if they're not explicitly affine to the current CPU.

Now that I've looked, it appears that vb2_buffer_done() might end up performing a DMA sync on the buffers, which, if it has to do order-of-megabytes worth of cache maintenance for large frames, is the kind of relatively slow operation that really doesn't want to be done with IRQs disabled (or under a lock at all, ideally) unless it absolutely *has* to be. If the lock is only needed here to protect modifications to the params list itself, then moving the whole list at once to do the cleanup "offline" sounds like a great idea to me.

Quoted 8 lines

Ouch.

Quoted 2 lines

ok, that might be a problem in v4l2 in general since vb2_buffer_done is actually often used inside an irq handler

Correct. The DMA sync should be moved to DQBUF time, there shouldn't be any reason to do it in the IRQ handler. I thought this had already been fixed :-(

Quoted 6 lines

For reference, there was a patch [1] proposed, but it moved the synchronization to a wrong place in the sequence, already after the .buf_finish queue callback, ending up breaking the drivers which need to access the buffer contents there.

[1] https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

I think we need to fix the drivers. We just can't do cache sync in IRQ context by default because a few drivers need to access the buffer contents. Those drivers should instead deffer access to a work queue, and sync explicitly. We could possibly provide helpers for that, making it transparent if a queue flag is set.

Signature · 3 lines
Regards,

Laurent Pinchart
#15 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Quoted 119 lines · Laurent Pinchart

Hi Tomasz,

Quoted 115 lines · Tomasz Figa
Quoted 100 lines · Laurent Pinchart
Quoted 98 lines · Dafna Hirschfeld

Am 26.06.20 um 18:58 schrieb Robin Murphy:

Quoted 97 lines · Tomasz Figa
Quoted 76 lines · Dafna Hirschfeld
Quoted 66 lines · Tomasz Figa
Quoted 52 lines · Robin Murphy
Quoted 51 lines · Dafna Hirschfeld
Quoted 24 lines

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
    drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
    1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
        /* stop params input firstly */
        spin_lock_irqsave(&params->config_lock, flags);
        params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
        for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
                if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                        buf = list_first_entry(&params->params,
                                               struct rkisp1_buffer, queue);
                        list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                        break;
                }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

          if (!x) {
                  //do stuff
          } else {
                  break;
          }

it would be better as:

          if (x)
                  break;
          //do stuff
Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

          list_for_each_entry_safe(&params->params, ..., buf) {
                  list_del(&buf->queue);
                  vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
          }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Quoted 6 lines

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Right, with the way we handle interrupt affinity on Arm an IRQ can't target multiple CPUs in hardware, so any time spent with IRQs disabled might be preventing other devices' interrupts from being taken even if they're not explicitly affine to the current CPU.

Now that I've looked, it appears that vb2_buffer_done() might end up performing a DMA sync on the buffers, which, if it has to do order-of-megabytes worth of cache maintenance for large frames, is the kind of relatively slow operation that really doesn't want to be done with IRQs disabled (or under a lock at all, ideally) unless it absolutely *has* to be. If the lock is only needed here to protect modifications to the params list itself, then moving the whole list at once to do the cleanup "offline" sounds like a great idea to me.

Quoted 8 lines

Ouch.

Quoted 2 lines

ok, that might be a problem in v4l2 in general since vb2_buffer_done is actually often used inside an irq handler

Correct. The DMA sync should be moved to DQBUF time, there shouldn't be any reason to do it in the IRQ handler. I thought this had already been fixed :-(

For reference, there was a patch [1] proposed, but it moved the synchronization to a wrong place in the sequence, already after the .buf_finish queue callback, ending up breaking the drivers which need to access the buffer contents there.

Quoted 7 lines
Quoted 1 line

[1] https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

I think we need to fix the drivers. We just can't do cache sync in IRQ context by default because a few drivers need to access the buffer contents. Those drivers should instead deffer access to a work queue, and sync explicitly. We could possibly provide helpers for that, making it transparent if a queue flag is set.

The drivers don't access the buffers explicitly from the IRQ. The vb2 queue .buf_finish callback is called at DQBUF time. It was just the patch mentioned that moved it to a part of DQBUF executed too late.

Best regards,
Tomasz

_______________________________________________
Linux-rockchip mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-rockchip

#16 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Hi Tomasz,

Quoted 125 lines · Tomasz Figa
Quoted 116 lines · Laurent Pinchart
Quoted 115 lines · Tomasz Figa
Quoted 100 lines · Laurent Pinchart
Quoted 98 lines · Dafna Hirschfeld

Am 26.06.20 um 18:58 schrieb Robin Murphy:

Quoted 97 lines · Tomasz Figa
Quoted 76 lines · Dafna Hirschfeld
Quoted 66 lines · Tomasz Figa
Quoted 52 lines · Robin Murphy
Quoted 51 lines · Dafna Hirschfeld
Quoted 24 lines

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
    drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
    1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
        /* stop params input firstly */
        spin_lock_irqsave(&params->config_lock, flags);
        params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
        for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
                if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                        buf = list_first_entry(&params->params,
                                               struct rkisp1_buffer, queue);
                        list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                        break;
                }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

          if (!x) {
                  //do stuff
          } else {
                  break;
          }

it would be better as:

          if (x)
                  break;
          //do stuff
Quoted 8 lines
Quoted 8 lines

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

          list_for_each_entry_safe(&params->params, ..., buf) {
                  list_del(&buf->queue);
                  vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
          }

Hi, indeed this is a much simpler implementation, greping 'return_all_buffers' I see that many drivers implement it this way.
thanks!

Quoted 1 line
Quoted 1 line
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

But this code runs when userspace asks to stop the streaming so I don't think it is important at that stage to allow the interrupts.

Quoted 6 lines

It's generally a good practice to reduce the time spent with interrupts disabled. Disabling the interrupts prevents the system from handling external events, including timer interrupts, and scheduling higher priority tasks, including real time ones. How much the system runs with interrupts disabled is one of the factors determining the general system latency.

Right, with the way we handle interrupt affinity on Arm an IRQ can't target multiple CPUs in hardware, so any time spent with IRQs disabled might be preventing other devices' interrupts from being taken even if they're not explicitly affine to the current CPU.

Now that I've looked, it appears that vb2_buffer_done() might end up performing a DMA sync on the buffers, which, if it has to do order-of-megabytes worth of cache maintenance for large frames, is the kind of relatively slow operation that really doesn't want to be done with IRQs disabled (or under a lock at all, ideally) unless it absolutely *has* to be. If the lock is only needed here to protect modifications to the params list itself, then moving the whole list at once to do the cleanup "offline" sounds like a great idea to me.

Quoted 8 lines

Ouch.

Quoted 2 lines

ok, that might be a problem in v4l2 in general since vb2_buffer_done is actually often used inside an irq handler

Correct. The DMA sync should be moved to DQBUF time, there shouldn't be any reason to do it in the IRQ handler. I thought this had already been fixed :-(

For reference, there was a patch [1] proposed, but it moved the synchronization to a wrong place in the sequence, already after the .buf_finish queue callback, ending up breaking the drivers which need to access the buffer contents there.

Quoted 7 lines
Quoted 1 line

[1] https://patchwork.linuxtv.org/project/linux-media/patch/[email protected]/

I think we need to fix the drivers. We just can't do cache sync in IRQ context by default because a few drivers need to access the buffer contents. Those drivers should instead deffer access to a work queue, and sync explicitly. We could possibly provide helpers for that, making it transparent if a queue flag is set.

Quoted 3 lines

The drivers don't access the buffers explicitly from the IRQ. The vb2 queue .buf_finish callback is called at DQBUF time. It was just the patch mentioned that moved it to a part of DQBUF executed too late.

Aahhh it's better than I thought :-) Shouldn't be too hard to fix then. Thanks for refreshing my memory.

Signature · 3 lines
Regards,

Laurent Pinchart
#17 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

In reply to Tomasz Figa

Am 26.06.20 um 16:03 schrieb Tomasz Figa:

Quoted 64 lines · Robin Murphy
Quoted 63 lines

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
   drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
   1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
       /* stop params input firstly */
       spin_lock_irqsave(&params->config_lock, flags);
       params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
       for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
               if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                       buf = list_first_entry(&params->params,
                                              struct rkisp1_buffer, queue);
                       list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
               } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                       break;
               }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

         if (!x) {
                 //do stuff
         } else {
                 break;
         }

it would be better as:

         if (x)
                 break;
         //do stuff

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

         list_for_each_entry_safe(&params->params, ..., buf) {
                 list_del(&buf->queue);
                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
         }
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

Hi, list_splice combines two lists together, I guess you meant list_cut_position which cut a list into two

Thanks,
Dafna

Quoted 8 lines

params->params to a local list_head under the spinlock, release it and then loop over the local head. As a side effect, one could even drop list_del() and switch to the non-safe variant of list_for_each_entry().

Best regards,
Tomasz

#18 lore raw

Re: [PATCH 3/3] media: staging: rkisp1: params: in 'stop_streaming' don't release the lock while returning buffers

Quoted 68 lines · Dafna Hirschfeld

Am 26.06.20 um 16:03 schrieb Tomasz Figa:

Quoted 64 lines · Robin Murphy
Quoted 63 lines

Hi Dafna,

Quoted 24 lines · Dafna Hirschfeld

In the stop_streaming callback 'rkisp1_params_vb2_stop_streaming' there is no need to release the lock 'config_lock' and then acquire it again at each iteration when returning all buffers. This is because the stream is about to end and there is no need to let the isr access a buffer.

---
   drivers/staging/media/rkisp1/rkisp1-params.c | 7 +------
   1 file changed, 1 insertion(+), 6 deletions(-)
drivers/staging/media/rkisp1/rkisp1-params.c +0 -2
index bf006dbeee2d..5169b02731f1 100644
@@ -1488,19 +1488,13 @@ static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq)
       /* stop params input firstly */
       spin_lock_irqsave(&params->config_lock, flags);
       params->is_streaming = false;
-     spin_unlock_irqrestore(&params->config_lock, flags);
 
       for (i = 0; i < RKISP1_ISP_PARAMS_REQ_BUFS_MAX; i++) {
-             spin_lock_irqsave(&params->config_lock, flags);
               if (!list_empty(&params->params)) {

drivers/staging/media/rkisp1/rkisp1-params.c static void rkisp1_params_vb2_stop_streaming(struct vb2_queue *vq) · line 1499

Quoted 10 lines
drivers/staging/media/rkisp1/rkisp1-params.c +0 -4
                       buf = list_first_entry(&params->params,
                                              struct rkisp1_buffer, queue);
                       list_del(&buf->queue);
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
               } else {
-                     spin_unlock_irqrestore(&params->config_lock,
-                                            flags);
                       break;
               }

Just skimming through out of idle curiosity I was going to comment that if you end up with this pattern:

         if (!x) {
                 //do stuff
         } else {
                 break;
         }

it would be better as:

         if (x)
                 break;
         //do stuff

However I then went and looked at the whole function and frankly it's a bit of a WTF. As best I could decipher, this whole crazy loop appears to be a baroque reinvention of:

         list_for_each_entry_safe(&params->params, ..., buf) {
                 list_del(&buf->queue);
                 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
         }
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

(assuming from context that the list should never contain more than RKISP1_ISP_PARAMS_REQ_BUFS_MAX entries in the first place)

Or if we want to avoid disabling the interrupts for the whole iteration, we could use list_splice() to move all the entries of

Hi, list_splice combines two lists together, I guess you meant list_cut_position which cut a list into two

No, I meant list_splice() and it does the expected thing here. It adds all the elements from one list (the list argument) to another list (the head argument). Since an element can't be on two lists at the same time, the first list is rendered invalid.

Best regards,
Tomasz