Skip to contentSkip to conversation

x86/time: fix scale_delta() inline assembly

PATCH In review Jan Beulich 7 participants 14 messages Nov 26, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/time: fix scale_delta() inline assembly

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

xen/arch/x86/time.c +3 -2
@@ -127,8 +127,9 @@ static inline u64 scale_delta(u64 delta,
         delta <<= scale->shift;
 
     asm (
-        "mul %%rdx ; shrd $32,%%rdx,%%rax"
-        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
+        "mul %2 ; shrd $32,%1,%0"
+        : "=a" (product), "=d" (delta)
+        : "rm" (delta), "0" ((u64)scale->mul_frac) );
 
     return product;
 }

x86/time: fix scale_delta() inline assembly

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

xen/arch/x86/time.c +3 -2
@@ -127,8 +127,9 @@ static inline u64 scale_delta(u64 delta,
         delta <<= scale->shift;
 
     asm (
-        "mul %%rdx ; shrd $32,%%rdx,%%rax"
-        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
+        "mul %2 ; shrd $32,%1,%0"
+        : "=a" (product), "=d" (delta)
+        : "rm" (delta), "0" ((u64)scale->mul_frac) );
 
     return product;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jan Beulich

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

Ugh. Thank you very much, Jan! What a horrible bug.

Quoted 21 lines
xen/arch/x86/time.c +3 -2
@@ -127,8 +127,9 @@ static inline u64 scale_delta(u64 delta,
         delta <<= scale->shift;
 
     asm (
-        "mul %%rdx ; shrd $32,%%rdx,%%rax"
-        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
+        "mul %2 ; shrd $32,%1,%0"
+        : "=a" (product), "=d" (delta)
+        : "rm" (delta), "0" ((u64)scale->mul_frac) );
 
     return product;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#2 lore raw
Quoted 17 lines

From: Keir Fraser [mailto:[email protected]]
Sent: Monday, November 26, 2012 9:11 AM
To: Jan Beulich; xen-devel
Subject: Re: [Xen-devel] [PATCH] x86/time: fix scale_delta() inline assembly

Quoted 10 lines · Jan Beulich

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

Quoted 5 lines
Quoted 1 line

Ugh. Thank you very much, Jan! What a horrible bug.

Amazing work Jan!

Suggestion (especially since this is DOCDAY)...

Since this has caused a number of people a lot of pain over a period of years, might it make sense to very clearly call out the user-visible problem(s) fixed by this in the commit message -- possibly including a few xen-devel archive URLs and/or subject lines? Other than the reference to plt_overflow() (and the fact that I've been following this thread for years), I wouldn't have any idea from the commit message that this bug fixes the "clock shifts by 50min" problem.

It would probably also be nice to cc the various people who have contributed their time to provide supporting information that helped isolate the bug.

#3 lore raw

I've just deployed this to a couple test servers that exhibited the problem.

What I don't really understand is why are some HW affected and not others ?

Cheers,

Sylvain

#4 lore raw
Quoted 3 lines · Sylvain Munaut

I've just deployed this to a couple test servers that exhibited the problem.

What I don't really understand is why are some HW affected and not others ?

I have no idea why some machines are more affected than others, but I can clearly see what Jan is saying, and how it will cause "weird" things to happen - but only if you hit the case where the calls are made in the right pattern, which I think is a matter of "luck". It will probably also matter what version of gcc is used to compile the code - different compiler versions will use registers in different ways.

--
Mats

Quoted 12 lines

Cheers,

Sylvain

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw
In reply to Sylvain Munaut
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Sylvain Munaut

I've just deployed this to a couple test servers that exhibited the problem.

What I don't really understand is why are some HW affected and not others ?

The multiplication is most likely to produce a zero upper half when the multiplier is close to its smallest possible value (0x80000000); as soon as the upper half is non-zero, chances shrink dramatically for the warp value to be less than the current one.

Jan

#6 lore raw
In reply to Sylvain Munaut
Quoted 6 lines

From: Sylvain Munaut [mailto:[email protected]]
Subject: Re: [Xen-devel] [PATCH] x86/time: fix scale_delta() inline assembly

I've just deployed this to a couple test servers that exhibited the problem.

What I don't really understand is why are some HW affected and not others ?

I don't know, but I'd imagine there is something in the x86 platform software (aka firmware) of those machines that runs and blocks execution of Xen for long enough (via NMI/MCE/SMI) for the platform timer to overflow, and this doesn't occur on other machines.

Maybe Jan has a better idea...

#7 lore raw
Quoted 11 lines · Dan Magenheimer
Quoted 6 lines

From: Sylvain Munaut [mailto:[email protected]]
Subject: Re: [Xen-devel] [PATCH] x86/time: fix scale_delta() inline assembly

I've just deployed this to a couple test servers that exhibited the problem.

What I don't really understand is why are some HW affected and not others ?

I don't know, but I'd imagine there is something in the x86 platform software (aka firmware) of those machines that runs and blocks execution of Xen for long enough (via NMI/MCE/SMI) for the platform timer to overflow, and this doesn't occur on other machines.

It is just luck of the draw. There is no actual platform timer overflow happening when this bug triggers.

-- Keir

Quoted 1 line

Maybe Jan has a better idea...

#8 lore raw
In reply to Dan Magenheimer
Quoted 4 lines
Quoted 1 line
Quoted 1 line · Dan Magenheimer

It would probably also be nice to cc the various people who have contributed their time to provide supporting information that helped isolate the bug.

That was done with the backported version that I sent subsequently. Beyond that, I don#t think our Cc-ing rules (if we can call them such in the first place, i.e. if they exist at all) don't really request that. Plus - apart from the two guys I worked with to debug this, I don't even know who else might have spent time on it earlier on.

Jan

#9 lore raw
In reply to Jan Beulich
Quoted 15 lines · Jan Beulich

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

xen/arch/x86/time.c static inline u64 scale_delta(u64 delta, · line 132

Quoted 8 lines
xen/arch/x86/time.c +3 -2
@@ -127,8 +127,9 @@ static inline u64 scale_delta(u64 delta,
         delta <<= scale->shift;
 
     asm (
-        "mul %%rdx ; shrd $32,%%rdx,%%rax"
-        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
+        "mul %2 ; shrd $32,%1,%0"
+        : "=a" (product), "=d" (delta)
+        : "rm" (delta), "0" ((u64)scale->mul_frac) );

OOI is there a reason to favour an output constraint overwriting delta, which is then thrown away, over a straightforward clobber of rdx?

It took me ages to figure out that you had swapped which of the two inputs to the multiply was in rax (I was very confused until I spotted that!).

It's also a bit confusing to use %1 and %0 with the shrd since you aren't actually using the things specified by the constraints but rather the outputs of the mul which happen to (now) be in those registers, I'd argue that the previous use of the explicit names was more correctly describing what was going on. As it is I was wondering how
shrd $32,delta,product
could possibly make sense.

Since delta is the first argument to the containing function would it be helpful to put that in rax, where it already is due to the calling convention? I suppose any difference is overwhelmed by the multiply anyway.

Ian.

#10 lore raw
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 17 lines · Jan Beulich

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

xen/arch/x86/time.c +0 -0
@@ -127,8 +127,9 @@ static inline u64 scale_delta(u64 delta,
         delta <<= scale->shift;
 
Quoted 9 lines

xen/arch/x86/time.c static inline u64 scale_delta(u64 delta, · line 132

Quoted 6 lines
xen/arch/x86/time.c +3 -2
     asm (
-        "mul %%rdx ; shrd $32,%%rdx,%%rax"
-        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
+        "mul %2 ; shrd $32,%1,%0"
+        : "=a" (product), "=d" (delta)
+        : "rm" (delta), "0" ((u64)scale->mul_frac) );

OOI is there a reason to favour an output constraint overwriting delta, which is then thrown away, over a straightforward clobber of rdx?

Yes - with a clobber of %rdx the compiler can't pass "delta" in that register.

Quoted 3 lines

It took me ages to figure out that you had swapped which of the two inputs to the multiply was in rax (I was very confused until I spotted that!).

Sorry. The resulting code was better (and less different from the prior version) with the operands swapped.

Quoted 7 lines

It's also a bit confusing to use %1 and %0 with the shrd since you aren't actually using the things specified by the constraints but rather the outputs of the mul which happen to (now) be in those registers, I'd argue that the previous use of the explicit names was more correctly describing what was going on. As it is I was wondering how
shrd $32,delta,product
could possibly make sense.

That's how things are with multi-insn asm-s (and even with single- insn ones when an output different from an input get tied together through using the same register). I continue to think that using the numbered forms here is better.

Quoted 4 lines

Since delta is the first argument to the containing function would it be helpful to put that in rax, where it already is due to the calling convention? I suppose any difference is overwhelmed by the multiply anyway.

The first argument of a "real" function would be in %rdi. This being an inline function, there's no calling convention anyway.

Jan

#11 lore raw
Quoted 29 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 17 lines · Jan Beulich

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

xen/arch/x86/time.c +0 -0
@@ -127,8 +127,9 @@ static inline u64 scale_delta(u64 delta,
         delta <<= scale->shift;
 
Quoted 9 lines

xen/arch/x86/time.c static inline u64 scale_delta(u64 delta, · line 132

Quoted 6 lines
xen/arch/x86/time.c +3 -2
     asm (
-        "mul %%rdx ; shrd $32,%%rdx,%%rax"
-        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
+        "mul %2 ; shrd $32,%1,%0"
+        : "=a" (product), "=d" (delta)
+        : "rm" (delta), "0" ((u64)scale->mul_frac) );

OOI is there a reason to favour an output constraint overwriting delta, which is then thrown away, over a straightforward clobber of rdx?

Quoted 2 lines

Yes - with a clobber of %rdx the compiler can't pass "delta" in that register.

Ah, of course.

Quoted 8 lines
Quoted 7 lines

It's also a bit confusing to use %1 and %0 with the shrd since you aren't actually using the things specified by the constraints but rather the outputs of the mul which happen to (now) be in those registers, I'd argue that the previous use of the explicit names was more correctly describing what was going on. As it is I was wondering how
shrd $32,delta,product
could possibly make sense.

Quoted 4 lines

That's how things are with multi-insn asm-s (and even with single- insn ones when an output different from an input get tied together through using the same register). I continue to think that using the numbered forms here is better.

I think the important difference here is that the mul generates its result specifically in rdx:rax rather than user specified registers. Since the output is not specified via the constraints IMHO using the constraints when subsequently consuming those outputs is confusing.

I'd agree with you if mul was something like "mul %2,%0,%1" (where %2*% rax =>%0:%1) then "shrd $32,%0,%1" would be perfectly fine.

Anyway I guess we'll have to agree to disagree. In any case your opinion trumps mine on this code ;-)

Quoted 6 lines
Quoted 4 lines

Since delta is the first argument to the containing function would it be helpful to put that in rax, where it already is due to the calling convention? I suppose any difference is overwhelmed by the multiply anyway.

The first argument of a "real" function would be in %rdi.

Oops, yes.

Quoted 1 line

This being an inline function, there's no calling convention anyway.

Right.

Ian.

#12 lore raw
Quoted 10 lines · Ian Campbell
Quoted 4 lines

That's how things are with multi-insn asm-s (and even with single- insn ones when an output different from an input get tied together through using the same register). I continue to think that using the numbered forms here is better.

I think the important difference here is that the mul generates its result specifically in rdx:rax rather than user specified registers. Since the output is not specified via the constraints IMHO using the constraints when subsequently consuming those outputs is confusing.

Quoted 5 lines

I'd agree with you if mul was something like "mul %2,%0,%1" (where %2*% rax =>%0:%1) then "shrd $32,%0,%1" would be perfectly fine.

Anyway I guess we'll have to agree to disagree. In any case your opinion trumps mine on this code ;-)

I didn't find Jan's asm particularly confusing, any more than it kind of fundamentally is. It's fine by me for it to go in as it is.

-- Keir

#13 lore raw
In reply to Ian Campbell
Quoted 39 lines · Ian Campbell
Quoted 15 lines · Jan Beulich

The way it was coded, it clobbered %rdx without telling the compiler. This generally didn't cause any problems except when there are two back to back invocations (as in plt_overflow()), as in that case the compiler may validly assume that it can re-use for the second instance the value loaded into %rdx before the first one.

Once at it, also properly relax the second operand of "mul" (there's no need for it to be in %rdx, or a register at all), and switch away from using explicit register names in the instruction operands.

xen/arch/x86/time.c static inline u64 scale_delta(u64 delta, · line 132

Quoted 8 lines
xen/arch/x86/time.c +3 -2
@@ -127,8 +127,9 @@ static inline u64 scale_delta(u64 delta,
          delta <<= scale->shift;
 
      asm (
-        "mul %%rdx ; shrd $32,%%rdx,%%rax"
-        : "=a" (product) : "0" (delta), "d" ((u64)scale->mul_frac) );
+        "mul %2 ; shrd $32,%1,%0"
+        : "=a" (product), "=d" (delta)
+        : "rm" (delta), "0" ((u64)scale->mul_frac) );

OOI is there a reason to favour an output constraint overwriting delta, which is then thrown away, over a straightforward clobber of rdx?

It took me ages to figure out that you had swapped which of the two inputs to the multiply was in rax (I was very confused until I spotted that!).

It's also a bit confusing to use %1 and %0 with the shrd since you aren't actually using the things specified by the constraints but rather the outputs of the mul which happen to (now) be in those registers, I'd argue that the previous use of the explicit names was more correctly describing what was going on. As it is I was wondering how
shrd $32,delta,product
could possibly make sense.

Quoted 4 lines

Since delta is the first argument to the containing function would it be helpful to put that in rax, where it already is due to the calling convention? I suppose any difference is overwhelmed by the multiply anyway.

Probably makes no difference which register is used for the input, as the compiler is hopefully inlining the entire function, and thus the register which is used as "first argument" is whatever that value happens to be in when the compiler gets there. This is one of many reasons that makes inlining more efficient - less register pressure. Hard-coding register usage in the assembler is a good way to "make life difficult for the compiler" (as it now has to make sure the value is in that register).

And multiply on modern processors is pretty quick - 8 clocks latency in the case of MUL of 64 x 64 bit number on AMD processors - I'm pretty sure Intel figures are similar, but not sure where to find that information quickly, so couldn't be bothered to look it up. The shrd instruction adds another 6 clocks of latency to the whole thing (again AMD number).

I'm not sure how many cycles you'd change the code by if the registers are hard-coded or compiler can choose... But not hardcoding it would be the better choice.

--
Mats

Quoted 10 lines

Ian.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel