Skip to contentSkip to conversation

staging: media: imx: Replace list_entry with list_for_each_entry_safe

PATCH In review Sanjana Sanikommu 2 participants 3 messages Apr 1, 2019

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

PATCH 1/1 staging: media: imx: Replace list_entry with list_for_each_entry_safe

Challenge suggested by coccinelle.
Replace use of the combination of list_empty() and list_entry() with list_for_each_entry_safe() to simplify the code.

Issue found using below Coccinelle script.

@@
expression E1;
identifier I1, I2;
type T;
iterator name list_for_each_entry_safe;
@@

T *I1;
+ T *tmp;
...
- while (list_empty(&E1) == 0)
+ list_for_each_entry_safe (I1, tmp, &E1, I2)
{
...when != T *I1;
- I1 = list_entry(E1.next, T, I2);
...
}

---
 drivers/staging/media/imx/imx-media-capture.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/media/imx/imx-media-capture.c +2 -3
index 9703c85b19c4..facce18975d8 100644
@@ -556,6 +556,7 @@ static void capture_stop_streaming(struct vb2_queue *vq)
 {
 	struct capture_priv *priv = vb2_get_drv_priv(vq);
 	struct imx_media_buffer *frame;
+	struct imx_media_buffer *tmp;
 	unsigned long flags;
 	int ret;
 
@@ -570,9 +571,7 @@ static void capture_stop_streaming(struct vb2_queue *vq)
 
 	/* release all active buffers */
 	spin_lock_irqsave(&priv->q_lock, flags);
-	while (!list_empty(&priv->ready_q)) {
-		frame = list_entry(priv->ready_q.next,
-				   struct imx_media_buffer, list);
+	list_for_each_entry_safe(frame, tmp, &priv->ready_q, list) {
 		list_del(&frame->list);
 		vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
 	}
Signature · 1 line
2.17.1
#1 lore raw
Quoted 13 lines · Sanjana Sanikommu

Challenge suggested by coccinelle.
Replace use of the combination of list_empty() and list_entry() with list_for_each_entry_safe() to simplify the code.

Issue found using below Coccinelle script.

@@
expression E1;
identifier I1, I2;
type T;
iterator name list_for_each_entry_safe;
@@

Quoted 10 lines

T *I1;
+ T *tmp;
...
- while (list_empty(&E1) == 0)
+ list_for_each_entry_safe (I1, tmp, &E1, I2)
{
...when != T *I1;
- I1 = list_entry(E1.next, T, I2);
...
}

I haven't studied this carefully, but there was an old coccinelle script in this direction that made the code quite incorrect. You need to understand what you are doing here to be sure that it is correct.

julia

Quoted 39 lines
---
 drivers/staging/media/imx/imx-media-capture.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/media/imx/imx-media-capture.c +2 -4
index 9703c85b19c4..facce18975d8 100644
@@ -556,6 +556,7 @@ static void capture_stop_streaming(struct vb2_queue *vq)
 {
 	struct capture_priv *priv = vb2_get_drv_priv(vq);
 	struct imx_media_buffer *frame;
+	struct imx_media_buffer *tmp;
 	unsigned long flags;
 	int ret;
 
@@ -570,9 +571,7 @@ static void capture_stop_streaming(struct vb2_queue *vq)
 
 	/* release all active buffers */
 	spin_lock_irqsave(&priv->q_lock, flags);
-	while (!list_empty(&priv->ready_q)) {
-		frame = list_entry(priv->ready_q.next,
-				   struct imx_media_buffer, list);
+	list_for_each_entry_safe(frame, tmp, &priv->ready_q, list) {
 		list_del(&frame->list);
 		vb2_buffer_done(&frame->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
 	}
--

2.17.1

--
You received this message because you are subscribed to the Google Groups "outreachy-kernel" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/outreachy-kernel/20190401135637.8459-1-sanjana99reddy99%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

#2 lore raw
Quoted 29 lines · Julia Lawall
Quoted 13 lines · Sanjana Sanikommu

Challenge suggested by coccinelle.
Replace use of the combination of list_empty() and list_entry() with list_for_each_entry_safe() to simplify the code.

Issue found using below Coccinelle script.

@@
expression E1;
identifier I1, I2;
type T;
iterator name list_for_each_entry_safe;
@@

Quoted 10 lines

T *I1;
+ T *tmp;
...
- while (list_empty(&E1) == 0)
+ list_for_each_entry_safe (I1, tmp, &E1, I2)
{
...when != T *I1;
- I1 = list_entry(E1.next, T, I2);
...
}

Quoted 4 lines

I haven't studied this carefully, but there was an old coccinelle script in this direction that made the code quite incorrect. You need to understand what you are doing here to be sure that it is correct.

Here I used list_for_each_entry_safe to iterate list of given type. I made script by considering the below information.

https://www.kernel.org/doc/htmldocs/kernel-api/API-list-for-each-entry-safe.html

Please correct me if I am wrong :)
Thank you
Sanjana.

Quoted 52 lines

julia

Review

Quoted 7 lines
Signed-off-by Sanjana Sanikommu <[email protected] <javascript:>>

---

 drivers/staging/media/imx/imx-media-capture.c | 5 ++--- 
 1 file changed, 2 insertions(+), 3 deletions(-) 
(quoted diff) +0 -0

b/drivers/staging/media/imx/imx-media-capture.c

drivers/staging/media/imx/imx-media-capture.c static void capture_stop_streaming(struct vb2_queue

Quoted 4 lines

index 9703c85b19c4..facce18975d8 100644

drivers/staging/media/imx/imx-media-capture.c +0 -0
@@ -556,6 +556,7 @@ static void capture_stop_streaming(struct vb2_queue 

*vq)

drivers/staging/media/imx/imx-media-capture.c static void capture_stop_streaming(struct vb2_queue

Quoted 8 lines
drivers/staging/media/imx/imx-media-capture.c +1 -0
 { 
         struct capture_priv *priv = vb2_get_drv_priv(vq); 
         struct imx_media_buffer *frame; 
+        struct imx_media_buffer *tmp; 
         unsigned long flags; 
         int ret; 
 
@@ -570,9 +571,7 @@ static void capture_stop_streaming(struct vb2_queue 

*vq)

drivers/staging/media/imx/imx-media-capture.c static void capture_stop_streaming(struct vb2_queue *vq)

Quoted 9 lines
drivers/staging/media/imx/imx-media-capture.c +1 -3
         /* release all active buffers */ 
         spin_lock_irqsave(&priv->q_lock, flags); 
-        while (!list_empty(&priv->ready_q)) { 
-                frame = list_entry(priv->ready_q.next, 
-                                   struct imx_media_buffer, list); 
+        list_for_each_entry_safe(frame, tmp, &priv->ready_q, list) { 
                 list_del(&frame->list); 
                 vb2_buffer_done(&frame->vbuf.vb2_buf, 

VB2_BUF_STATE_ERROR);

Quoted 6 lines
drivers/staging/media/imx/imx-media-capture.c +0 -1
         } 
-- 

2.17.1

Signature · 1 line
You received this message because you are subscribed to the Google 

Groups "outreachy-kernel" group.

Quoted 1 line

To unsubscribe from this group and stop receiving emails from it, send

an email to [email protected] <javascript:>.

Quoted 1 line

To post to this group, send email to [email protected]

<javascript:>.

Quoted 1 line

To view this discussion on the web visit

https://groups.google.com/d/msgid/outreachy-kernel/20190401135637.8459-1-sanjana99reddy99%40gmail.com.

Quoted 2 lines

For more options, visit https://groups.google.com/d/optout.