Skip to contentSkip to conversation

staging: lustre: Move assignments out of function calls.

PATCH New Elise Lennion 3 participants 4 messages Oct 21, 2016 – Oct 23, 2016 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: lustre: Move assignments out of function calls.

Assignments inside of function calls confuse the reader and should be avoided, so they were moved out before the call.

Found with Coccinelle, semantic patch:
@@
identifier f;
expression e1, e2;
assignment operator a;
@@

+ e1 a e2;
f(...,
- (e1 a e2)
+ e1
,...);

---
 drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c | 5 ++---
 drivers/staging/lustre/lustre/llite/vvp_io.c    | 3 ++-
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +2 -3
index 12647af..ec7f2a8 100644
@@ -764,9 +764,8 @@ static int ldlm_bl_thread_main(void *arg)
 
 		if (!blwi) {
 			atomic_dec(&blp->blp_busy_threads);
-			l_wait_event_exclusive(blp->blp_waitq,
-					       (blwi = ldlm_bl_get_work(blp)),
-					       &lwi);
+			blwi = ldlm_bl_get_work(blp);
+			l_wait_event_exclusive(blp->blp_waitq, blwi, &lwi);
 			busy = atomic_inc_return(&blp->blp_busy_threads);
 		} else {
 			busy = atomic_read(&blp->blp_busy_threads);
drivers/staging/lustre/lustre/llite/vvp_io.c +2 -1
index 1a4b897..87fdab2 100644
@@ -450,7 +450,8 @@ static void vvp_io_advance(const struct lu_env *env,
 	struct vvp_io	 *vio = cl2vvp_io(env, ios);
 	CLOBINVRNT(env, obj, vvp_object_invariant(obj));
 
-	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count  -= nob);
+	vio->vui_tot_count -= nob;
+	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);
 }
 
 static void vvp_io_update_iov(const struct lu_env *env,
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 staging: lustre: Move assignments out of function calls.

Quoted 27 lines · Elise Lennion

Assignments inside of function calls confuse the reader and should be avoided, so they were moved out before the call.

Found with Coccinelle, semantic patch:
@@
identifier f;
expression e1, e2;
assignment operator a;
@@

+ e1 a e2;
f(...,
- (e1 a e2)
+ e1
,...);

---
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c | 5 ++---
drivers/staging/lustre/lustre/llite/vvp_io.c | 3 ++-

2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +0 -0
index 12647af..ec7f2a8 100644

drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c static int ldlm_bl_thread_main(void *arg) · line 768

Quoted 8 lines
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +2 -3
@@ -764,9 +764,8 @@ static int ldlm_bl_thread_main(void *arg)
 
 		if (!blwi) {
 			atomic_dec(&blp->blp_busy_threads);
-			l_wait_event_exclusive(blp->blp_waitq,
-					       (blwi = ldlm_bl_get_work(blp)),
-					       &lwi);
+			blwi = ldlm_bl_get_work(blp);
+			l_wait_event_exclusive(blp->blp_waitq, blwi, &lwi);

This one is wrong.
This is not a function call, this is a macro whee second argument determines when to terminate the waiting.
ldlm_bl_get_work returns the next work item, so we need to save it somewhere for later processing.

The way you coded it it would just hang in there as blwi becomes a constant.

Quoted 8 lines
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +0 -0
 			busy = atomic_inc_return(&blp->blp_busy_threads);
 		} else {
 			busy = atomic_read(&blp->blp_busy_threads);
drivers/staging/lustre/lustre/llite/vvp_io.c +0 -0
index 1a4b897..87fdab2 100644

drivers/staging/lustre/lustre/llite/vvp_io.c static void vvp_io_advance(const struct lu_env *env, · line 454

Quoted 6 lines
drivers/staging/lustre/lustre/llite/vvp_io.c +2 -1
@@ -450,7 +450,8 @@ static void vvp_io_advance(const struct lu_env *env,
 	struct vvp_io	 *vio = cl2vvp_io(env, ios);
 	CLOBINVRNT(env, obj, vvp_object_invariant(obj));
 
-	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count  -= nob);
+	vio->vui_tot_count -= nob;
+	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);

This one is probably ok.

Quoted 5 lines
drivers/staging/lustre/lustre/llite/vvp_io.c +0 -0
 }
 
 static void vvp_io_update_iov(const struct lu_env *env,
Signature · 1 line
2.7.4
#2 lore raw

PATCH 1/1 staging: lustre: Move assignments out of function calls.

In reply to Elise Lennion
Quoted 3 lines · Elise Lennion

Assignments inside of function calls confuse the reader and should be avoided, so they were moved out before the call.

Thanks for your patch. I agree that this kind of usage is bad and avoid it in my own code and try to remove it when I see it. However, in some cases it is needed to simplify the code flow (e.g. in conditionals that would otherwise need to duplicate logic).

More comments below.

Quoted 24 lines

Found with Coccinelle, semantic patch:
@@
identifier f;
expression e1, e2;
assignment operator a;
@@

+ e1 a e2;
f(...,
- (e1 a e2)
+ e1
,...);

---
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c | 5 ++---
drivers/staging/lustre/lustre/llite/vvp_io.c | 3 ++-

2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +0 -0
index 12647af..ec7f2a8 100644

drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c static int ldlm_bl_thread_main(void *arg) · line 768

Quoted 8 lines
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +2 -3
@@ -764,9 +764,8 @@ static int ldlm_bl_thread_main(void *arg)
 
 		if (!blwi) {
 			atomic_dec(&blp->blp_busy_threads);
-			l_wait_event_exclusive(blp->blp_waitq,
-					       (blwi = ldlm_bl_get_work(blp)),
-					       &lwi);
+			blwi = ldlm_bl_get_work(blp);
+			l_wait_event_exclusive(blp->blp_waitq, blwi, &lwi);

This first change is broken, since l_wait_event_exclusive() is actually a macro that is waiting until ldlm_bl_get_work() returns a work item. Moving the blwi assignment before the wait means that this will wait forever.

This can be ignored for now, as there is a patch in the pipeline to clean up this code (http://review.whamcloud.com/5843) that James should be getting close to pushing, and it removes this inline assignment.

Quoted 8 lines
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +0 -0
 			busy = atomic_inc_return(&blp->blp_busy_threads);
 		} else {
 			busy = atomic_read(&blp->blp_busy_threads);
drivers/staging/lustre/lustre/llite/vvp_io.c +0 -0
index 1a4b897..87fdab2 100644

drivers/staging/lustre/lustre/llite/vvp_io.c static void vvp_io_advance(const struct lu_env *env, · line 454

Quoted 6 lines
drivers/staging/lustre/lustre/llite/vvp_io.c +2 -1
@@ -450,7 +450,8 @@ static void vvp_io_advance(const struct lu_env *env,
 	struct vvp_io	 *vio = cl2vvp_io(env, ios);
 	CLOBINVRNT(env, obj, vvp_object_invariant(obj));
 
-	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count  -= nob);
+	vio->vui_tot_count -= nob;
+	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);

This one is fine. Please resubmit the patch with just this change. You can add "Reviewed-by: Andreas Dilger <[email protected]>" for that patch.

Cheers, Andreas

Quoted 6 lines
drivers/staging/lustre/lustre/llite/vvp_io.c +0 -0
 }
 
 static void vvp_io_update_iov(const struct lu_env *env,
Signature · 1 line
2.7.4
#3 lore raw

PATCH 1/1 staging: lustre: Move assignments out of function calls.

Quoted 72 lines · Andreas
Quoted 3 lines · Elise Lennion

Assignments inside of function calls confuse the reader and should be avoided, so they were moved out before the call.

Thanks for your patch. I agree that this kind of usage is bad and avoid it in my own code and try to remove it when I see it. However, in some cases it is needed to simplify the code flow (e.g. in conditionals that would otherwise need to duplicate logic).

More comments below.

Quoted 24 lines

Found with Coccinelle, semantic patch:
@@
identifier f;
expression e1, e2;
assignment operator a;
@@

+ e1 a e2;
f(...,
- (e1 a e2)
+ e1
,...);

---
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c | 5 ++---
drivers/staging/lustre/lustre/llite/vvp_io.c | 3 ++-

2 files changed, 4 insertions(+), 4 deletions(-)
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +0 -0
index 12647af..ec7f2a8 100644

drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c static int ldlm_bl_thread_main(void *arg) · line 768

Quoted 8 lines
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +2 -3
@@ -764,9 +764,8 @@ static int ldlm_bl_thread_main(void *arg)
 
 		if (!blwi) {
 			atomic_dec(&blp->blp_busy_threads);
-			l_wait_event_exclusive(blp->blp_waitq,
-					       (blwi = ldlm_bl_get_work(blp)),
-					       &lwi);
+			blwi = ldlm_bl_get_work(blp);
+			l_wait_event_exclusive(blp->blp_waitq, blwi, &lwi);

This first change is broken, since l_wait_event_exclusive() is actually a macro that is waiting until ldlm_bl_get_work() returns a work item. Moving the blwi assignment before the wait means that this will wait forever.

This can be ignored for now, as there is a patch in the pipeline to clean up this code (http://review.whamcloud.com/5843) that James should be getting close to pushing, and it removes this inline assignment.

Quoted 8 lines
drivers/staging/lustre/lustre/ldlm/ldlm_lockd.c +0 -0
 			busy = atomic_inc_return(&blp->blp_busy_threads);
 		} else {
 			busy = atomic_read(&blp->blp_busy_threads);
drivers/staging/lustre/lustre/llite/vvp_io.c +0 -0
index 1a4b897..87fdab2 100644

drivers/staging/lustre/lustre/llite/vvp_io.c static void vvp_io_advance(const struct lu_env *env, · line 454

Quoted 6 lines
drivers/staging/lustre/lustre/llite/vvp_io.c +2 -1
@@ -450,7 +450,8 @@ static void vvp_io_advance(const struct lu_env *env,
 	struct vvp_io	 *vio = cl2vvp_io(env, ios);
 	CLOBINVRNT(env, obj, vvp_object_invariant(obj));
 
-	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count  -= nob);
+	vio->vui_tot_count -= nob;
+	iov_iter_reexpand(vio->vui_iter, vio->vui_tot_count);

This one is fine. Please resubmit the patch with just this change. You can add "Reviewed-by: Andreas Dilger <[email protected]>" for that patch.

Quoted 9 lines

Cheers, Andreas

Quoted 6 lines
drivers/staging/lustre/lustre/llite/vvp_io.c +0 -0
 }
 
 static void vvp_io_update_iov(const struct lu_env *env,
Signature · 1 line
2.7.4

Sorry for the confusion with the macro usage, I'll send a v2 without this undesired change.

thank you all for the detailed answer,

elise