Skip to contentSkip to conversation

xen/credit2: Drop unnecessary bit test

PATCH In review Andrew Cooper 3 participants 6 messages Jan 11, 2018 – Jan 12, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/credit2: Drop unnecessary bit test

---
CC: George Dunlap <[email protected]>
CC: Dario Faggioli <[email protected]>

Notices by chance while inspecting the disassembly delta for "x86/bitops:
Introduce variable/constant pairs for __{set,clear,change}_bit()"

---
 xen/common/sched_credit2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/sched_credit2.c +1 -1
index 18f39ca..ee9768e 100644
@@ -2063,7 +2063,7 @@ csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc)
         update_load(ops, svc->rqd, svc, -1, NOW());
         runq_remove(svc);
     }
-    else if ( svc->flags & CSFLAG_delayed_runq_add )
+    else
         __clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);
 }
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 16 lines · Andrew Cooper

---
CC: George Dunlap <[email protected]>
CC: Dario Faggioli <[email protected]>

Notices by chance while inspecting the disassembly delta for "x86/bitops:
Introduce variable/constant pairs for __{set,clear,change}_bit()"

---
 xen/common/sched_credit2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/sched_credit2.c +0 -0
index 18f39ca..ee9768e 100644

xen/common/sched_credit2.c csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc) · line 2067

Quoted 6 lines
xen/common/sched_credit2.c +1 -1
@@ -2063,7 +2063,7 @@ csched2_vcpu_sleep(const struct scheduler *ops, struct vcpu *vc)
         update_load(ops, svc->rqd, svc, -1, NOW());
         runq_remove(svc);
     }
-    else if ( svc->flags & CSFLAG_delayed_runq_add )
+    else
         __clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);

There was a reason for this at some point, I'm sure. Did this used to be the atomic version (without the __) originally?

-George

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 13 lines · George Dunlap
Quoted 12 lines · Andrew Cooper

---
CC: George Dunlap <[email protected]>
CC: Dario Faggioli <[email protected]>

Notices by chance while inspecting the disassembly delta for "x86/bitops:
Introduce variable/constant pairs for __{set,clear,change}_bit()"

---
 xen/common/sched_credit2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines

xen/common/sched_credit2.c csched2_vcpu_sleep(const struct scheduler · line 2068

Quoted 13 lines
xen/common/sched_credit2.c +1 -1
index 18f39ca..ee9768e 100644
@@ -2063,7 +2063,7 @@ csched2_vcpu_sleep(const struct scheduler
 *ops, struct vcpu *vc)
         update_load(ops, svc->rqd, svc, -1, NOW());
         runq_remove(svc);
     }
-    else if ( svc->flags & CSFLAG_delayed_runq_add )
+    else
         __clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);

There was a reason for this at some point, I'm sure.

Adding Juergen, as commit e8abdea48a ("use masking operation instead of test_bit for CSFLAG bits") is his.

Quoted 3 lines

Did this used to
be the atomic version (without the __) originally?

At the beginning, yes. In fact, if you look at how the code was before Juergen's patch:

    else if ( test_bit(__CSFLAG_delayed_runq_add, &svc->flags) )
         clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);

Which indeed was overkill. That patch got rid of test_bit(), but did not touch clear_bit().

I then turned the clear_bit() in __clear_bit() in commit 222234f2ad ("xen: credit2: use non-atomic cpumask and bit operations") but kept the test.

From a code readability perspective, I like this patch (and have thought about doing this myself many times). From a performance perspective, the test may make sense. In fact, we do a technically unnecessary "load", but that may avoid having to pay the price of a "store".

I guess it's debatable whether that is worth or not, in general. However, at least in this specific case, I don't think this matters too
much, and I'd be inclined to take the patch.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Software Engineer @ SUSE https://www.suse.com/

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
Quoted 54 lines · Dario Faggioli
Quoted 13 lines · George Dunlap
Quoted 12 lines · Andrew Cooper

---
CC: George Dunlap <[email protected]>
CC: Dario Faggioli <[email protected]>

Notices by chance while inspecting the disassembly delta for "x86/bitops:
Introduce variable/constant pairs for __{set,clear,change}_bit()"

---
 xen/common/sched_credit2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

xen/common/sched_credit2.c csched2_vcpu_sleep(const struct scheduler · line 2068

Quoted 13 lines
xen/common/sched_credit2.c +1 -1
index 18f39ca..ee9768e 100644
@@ -2063,7 +2063,7 @@ csched2_vcpu_sleep(const struct scheduler
 *ops, struct vcpu *vc)
         update_load(ops, svc->rqd, svc, -1, NOW());
         runq_remove(svc);
     }
-    else if ( svc->flags & CSFLAG_delayed_runq_add )
+    else
         __clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);

There was a reason for this at some point, I'm sure.

Adding Juergen, as commit e8abdea48a ("use masking operation instead of test_bit for CSFLAG bits") is his.

Quoted 3 lines

Did this used to
be the atomic version (without the __) originally?

At the beginning, yes. In fact, if you look at how the code was before Juergen's patch:

    else if ( test_bit(__CSFLAG_delayed_runq_add, &svc->flags) )
         clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);

Which indeed was overkill. That patch got rid of test_bit(), but did not touch clear_bit().

I then turned the clear_bit() in __clear_bit() in commit 222234f2ad ("xen: credit2: use non-atomic cpumask and bit operations") but kept the test.

From a code readability perspective, I like this patch (and have thought about doing this myself many times). From a performance perspective, the test may make sense. In fact, we do a technically unnecessary "load", but that may avoid having to pay the price of a "store".

Quoted 3 lines

I guess it's debatable whether that is worth or not, in general. However, at least in this specific case, I don't think this matters too
much, and I'd be inclined to take the patch.

It is generally worth doing a read to conditionally avoid a locked RMW, in the case that you expect the locked RMW to be unnecessary (i.e. the modification is already present).

The same is not true for plain memory reads and writes.  The overhead of the conditional jump far outweighs the saving of possibly not dirtying the cache line.

The reason I noticed this is because (with my bitops change), the compiler optimised the if out entirely.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Acked-by
#4 lore raw
Quoted 63 lines · Andrew Cooper
Quoted 54 lines · Dario Faggioli
Quoted 13 lines · George Dunlap
Quoted 12 lines · Andrew Cooper

---
CC: George Dunlap <[email protected]>
CC: Dario Faggioli <[email protected]>

Notices by chance while inspecting the disassembly delta for "x86/bitops:
Introduce variable/constant pairs for __{set,clear,change}_bit()"

---
 xen/common/sched_credit2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 15 lines

xen/common/sched_credit2.c csched2_vcpu_sleep(const struct scheduler · line 2068

Quoted 13 lines
xen/common/sched_credit2.c +1 -1
index 18f39ca..ee9768e 100644
@@ -2063,7 +2063,7 @@ csched2_vcpu_sleep(const struct scheduler
 *ops, struct vcpu *vc)
         update_load(ops, svc->rqd, svc, -1, NOW());
         runq_remove(svc);
     }
-    else if ( svc->flags & CSFLAG_delayed_runq_add )
+    else
         __clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);

There was a reason for this at some point, I'm sure.

Adding Juergen, as commit e8abdea48a ("use masking operation instead of test_bit for CSFLAG bits") is his.

Quoted 3 lines

Did this used to
be the atomic version (without the __) originally?

At the beginning, yes. In fact, if you look at how the code was before Juergen's patch:

    else if ( test_bit(__CSFLAG_delayed_runq_add, &svc->flags) )
         clear_bit(__CSFLAG_delayed_runq_add, &svc->flags);

Which indeed was overkill. That patch got rid of test_bit(), but did not touch clear_bit().

I then turned the clear_bit() in __clear_bit() in commit 222234f2ad ("xen: credit2: use non-atomic cpumask and bit operations") but kept the test.

From a code readability perspective, I like this patch (and have thought about doing this myself many times). From a performance perspective, the test may make sense. In fact, we do a technically unnecessary "load", but that may avoid having to pay the price of a "store".

Quoted 3 lines

I guess it's debatable whether that is worth or not, in general. However, at least in this specific case, I don't think this matters too
much, and I'd be inclined to take the patch.

It is generally worth doing a read to conditionally avoid a locked RMW, in the case that you expect the locked RMW to be unnecessary (i.e. the modification is already present).

Quoted 6 lines

The same is not true for plain memory reads and writes.  The overhead of the conditional jump far outweighs the saving of possibly not dirtying the cache line.

The reason I noticed this is because (with my bitops change), the compiler optimised the if out entirely.

Yes, if it's not necessary we might as well remove it.

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by
#5 lore raw
In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper

It is generally worth doing a read to conditionally avoid a locked RMW,
in the case that you expect the locked RMW to be unnecessary (i.e. the
modification is already present).

Quoted 6 lines

The same is not true for plain memory reads and writes. The overhead of
the conditional jump far outweighs the saving of possibly not dirtying
the cache line.

Sure.

Quoted 3 lines

The reason I noticed this is because (with my bitops change), the compiler optimised the if out entirely.

Ah, interesting. :-)

It's not necessary, as George Acked it already, but feel free to add a:

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Software Engineer @ SUSE https://www.suse.com/

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel