Skip to contentSkip to conversation

Lazy FPU save/restore

PATCH Applied Anthony Liguori 3 participants 9 messages Feb 16, 2007 – Feb 19, 2007 7 messages not in the archive KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Lazy FPU save/restore

The following patch saves the host FPU state and loads the guests FPU state if !(CR0.MP == 1 && CR0.TS == 1).

When CR0.MP == 1 && CR0.TS == 1, all FPU activity will generate exceptions. OS's use these exceptions to implement lazy FPU loading to improve context switch time. Since any FPU activity will generate traps, we don't have to worry about the guest modifying the host FPU state.

My microbenchmark of choice uses FPU operations so I think the results are currently tainted. I've only tested on a 32bit SVM system.

Regards,

Anthony Liguori

kernel/kvm.h +2 -0
diff -r 962b18bbd6d9 kernel/kvm.h
@@ -17,6 +17,7 @@
 #include <linux/kvm_para.h>
 
 #define CR0_PE_MASK (1ULL << 0)
+#define CR0_MP_MASK (1ULL << 1)
 #define CR0_TS_MASK (1ULL << 3)
 #define CR0_NE_MASK (1ULL << 5)
 #define CR0_WP_MASK (1ULL << 16)
@@ -263,6 +264,7 @@ struct kvm_vcpu {
 
 	struct kvm_guest_debug guest_debug;
 
+	int fx_active;
 	char fx_buf[FX_BUF_SIZE];
 	char *host_fx_image;
 	char *guest_fx_image;
kernel/kvm_main.c +2 -0
diff -r 962b18bbd6d9 kernel/kvm_main.c
@@ -404,6 +404,7 @@ void set_cr0(struct kvm_vcpu *vcpu, unsi
 
 	}
 
+	vcpu->fx_active = !((cr0 & CR0_TS_MASK) && (cr0 & CR0_MP_MASK));
 	kvm_arch_ops->set_cr0(vcpu, cr0);
 	vcpu->cr0 = cr0;
 
@@ -530,6 +531,7 @@ void fx_init(struct kvm_vcpu *vcpu)
 	fx_image->mxcsr = 0x1f80;
 	memset(vcpu->guest_fx_image + sizeof(struct fx_image_s),
 	       0, FX_IMAGE_SIZE - sizeof(struct fx_image_s));
+	vcpu->fx_active = 1;
 }
 EXPORT_SYMBOL_GPL(fx_init);
 
kernel/svm.c +8 -4
diff -r 962b18bbd6d9 kernel/svm.c
@@ -1461,8 +1461,10 @@ again:
 		load_db_regs(vcpu->svm->db_regs);
 	}
 
-	fx_save(vcpu->host_fx_image);
-	fx_restore(vcpu->guest_fx_image);
+	if (vcpu->fx_active) {
+		fx_save(vcpu->host_fx_image);
+		fx_restore(vcpu->guest_fx_image);
+	}
 
 	asm volatile (
 #ifdef CONFIG_X86_64
@@ -1573,8 +1575,10 @@ again:
 #endif
 		: "cc", "memory" );
 
-	fx_save(vcpu->guest_fx_image);
-	fx_restore(vcpu->host_fx_image);
+	if (vcpu->fx_active) {
+		fx_save(vcpu->guest_fx_image);
+		fx_restore(vcpu->host_fx_image);
+	}
 
 	if ((vcpu->svm->vmcb->save.dr7 & 0xff))
 		load_db_regs(vcpu->svm->host_db_regs);
kernel/vmx.c +9 -4
diff -r 962b18bbd6d9 kernel/vmx.c
@@ -1764,8 +1764,10 @@ again:
 	if (vcpu->guest_debug.enabled)
 		kvm_guest_debug_pre(vcpu);
 
-	fx_save(vcpu->host_fx_image);
-	fx_restore(vcpu->guest_fx_image);
+	if (vcpu->fx_active) {
+		fx_save(vcpu->host_fx_image);
+		fx_restore(vcpu->guest_fx_image);
+	}
 
 	save_msrs(vcpu->host_msrs, vcpu->nmsrs);
 	load_msrs(vcpu->guest_msrs, NR_BAD_MSRS);
@@ -1894,8 +1896,11 @@ again:
 	save_msrs(vcpu->guest_msrs, NR_BAD_MSRS);
 	load_msrs(vcpu->host_msrs, NR_BAD_MSRS);
 
-	fx_save(vcpu->guest_fx_image);
-	fx_restore(vcpu->host_fx_image);
+	if (vcpu->fx_active) {
+		fx_save(vcpu->guest_fx_image);
+		fx_restore(vcpu->host_fx_image);
+	}
+
 	vcpu->interrupt_window_open = (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0;
 
 	asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS));

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

A message that is not in the archive (sent off-list or not yet received): [email protected]

#2 lore raw
Quoted 2 lines · Anthony Liguori

My microbenchmark of choice uses FPU operations so I think the results are currently tainted. I've only tested on a 32bit SVM system.

So did you have that virtbench patch for me? 8)

Rusty.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

Applied
#3 lore raw
In reply to a message not in the archive
Quoted 9 lines · Anthony Liguori

The following patch saves the host FPU state and loads the guests FPU state if !(CR0.MP == 1 && CR0.TS == 1).

When CR0.MP == 1 && CR0.TS == 1, all FPU activity will generate exceptions. OS's use these exceptions to implement lazy FPU loading to improve context switch time. Since any FPU activity will generate traps, we don't have to worry about the guest modifying the host FPU state.

Quoted 5 lines

My microbenchmark of choice uses FPU operations so I think the results are currently tainted. I've only tested on a 32bit SVM system.

Applied, thanks.

Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

A message that is not in the archive (sent off-list or not yet received): 45D81EB6.9010407-atKUWr5tajBWk0Htik3J/[email protected]

#5 lore raw
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Anthony Liguori

The following patch saves the host FPU state and loads the guests FPU state if !(CR0.MP == 1 && CR0.TS == 1).

When CR0.MP == 1 && CR0.TS == 1, all FPU activity will generate exceptions. OS's use these exceptions to implement lazy FPU loading to improve context switch time. Since any FPU activity will generate traps, we don't have to worry about the guest modifying the host FPU state.

Quoted 8 lines
Quoted 5 lines

My microbenchmark of choice uses FPU operations so I think the results are currently tainted. I've only tested on a 32bit SVM system.

Applied, thanks.

... and reverted.

Running an fpu load on both guest and host shows corruption (both on the guest and host). The load is

Quoted 12 lines

#include <stdio.h>

void test_fpu()

{
    int i;
    double f = 0;

    for (i = 0; i < 10000000; ++i)
    f += 1 / (1.0 + i);
    printf("%20.16f\n", f);
}
Quoted 5 lines

int main(int ac, char **av)

{
    while (1)
    test_fpu();
}

Compiled statically with -O2 on 64-bit. I don't know what's wrong -- the code looks correct (well, on Intel it wants KVM_GUEST_CR0_MASK to include CR0_MP_MASK and CR0_TS_MASK, but it fails with that too, and it fails on AMD as well).

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

A message that is not in the archive (sent off-list or not yet received): 45D861CD.4020207-atKUWr5tajBWk0Htik3J/[email protected]

#7 lore raw
Quoted 44 lines · Avi Kivity
Quoted 10 lines · Avi Kivity
Quoted 9 lines · Anthony Liguori

The following patch saves the host FPU state and loads the guests FPU state if !(CR0.MP == 1 && CR0.TS == 1).

When CR0.MP == 1 && CR0.TS == 1, all FPU activity will generate exceptions. OS's use these exceptions to implement lazy FPU loading to improve context switch time. Since any FPU activity will generate traps, we don't have to worry about the guest modifying the host FPU state.

Quoted 8 lines
Quoted 5 lines

My microbenchmark of choice uses FPU operations so I think the results are currently tainted. I've only tested on a 32bit SVM system.

Applied, thanks.

... and reverted.

Running an fpu load on both guest and host shows corruption (both on the guest and host). The load is

Quoted 12 lines

#include <stdio.h>

void test_fpu()

{
    int i;
    double f = 0;

    for (i = 0; i < 10000000; ++i)
    f += 1 / (1.0 + i);
    printf("%20.16f\n", f);
}
Quoted 5 lines

int main(int ac, char **av)

{
    while (1)
    test_fpu();
}
Quoted 4 lines

Compiled statically with -O2 on 64-bit. I don't know what's wrong -- the code looks correct (well, on Intel it wants KVM_GUEST_CR0_MASK to include CR0_MP_MASK and CR0_TS_MASK, but it fails with that too, and it fails on AMD as well).

I'll have to try and setup a 64 bit system. I cannot reproduce on my 32bit system with your test program.

Thanks for figuring this out!

Regards,

Anthony Liguori

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

A message that is not in the archive (sent off-list or not yet received): [email protected]

#9 lore raw
Quoted 25 lines · Anthony Liguori
Quoted 25 lines

... and reverted.

Running an fpu load on both guest and host shows corruption (both on the guest and host). The load is

Quoted 17 lines

#include <stdio.h>

void test_fpu()

{
    int i;
    double f = 0;

    for (i = 0; i < 10000000; ++i)
    f += 1 / (1.0 + i);
    printf("%20.16f\n", f);
}

int main(int ac, char **av)

{
    while (1)
    test_fpu();
}
Quoted 7 lines
Quoted 4 lines

Compiled statically with -O2 on 64-bit. I don't know what's wrong -- the code looks correct (well, on Intel it wants KVM_GUEST_CR0_MASK to include CR0_MP_MASK and CR0_TS_MASK, but it fails with that too, and it fails on AMD as well).

I'll have to try and setup a 64 bit system. I cannot reproduce on my 32bit system with your test program.

It may be related to 64-bit (as that uses sse for floating point), or to the fact that I use ssh on a remote host, which causes more context switches. I'll try to reproduce on 32 bits with my setup.

Signature · 8 lines
error compiling committee.c: too many arguments to function


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

A message that is not in the archive (sent off-list or not yet received): 45D87C2E.2090806-atKUWr5tajBWk0Htik3J/[email protected]

#11 lore raw
Quoted 5 lines · Avi Kivity
Quoted 2 lines

I'll have to try and setup a 64 bit system. I cannot reproduce on my 32bit system with your test program.

It may be related to 64-bit (as that uses sse for floating point), or to the fact that I use ssh on a remote host, which causes more context switches. I'll try to reproduce on 32 bits with my setup.

It doesn't reproduce on a 32-bit host. However, it does reproduce on a 32-bit guest on a 64-bit host, and on a 32-bit user load running in a 64-bit guest on a 64-bit host.

I'm attaching an updated test program which makes it easier to spot the failures. You only need to run it on the guest, although running it on the host as well increases the failure rate significantly.

Signature · 1 line
error compiling committee.c: too many arguments to function

#include <stdio.h>
#include <math.h>

double test_fpu_once()

{
    int i;
    double f = 0;

    for (i = 0; i < 10000000; ++i)
	f += 1 / (1.0 + i);
    return f;
}

void test_fpu()

{
    double a, b;
    int runs;

    runs = 0;
    a = test_fpu_once();
    while (1) {
	b = test_fpu_once();
	if (fabs(a - b) > 1e-9)
	    printf("error: %20.16f -> %20.16f\n", a, b);
	a = b;
	if (++runs % 100 == 0)
	    printf("runs: %8d\n", runs);
    }
}

int main(int ac, char **av)

{
    test_fpu();
    return 0;
}

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

_______________________________________________
kvm-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/kvm-devel

A message that is not in the archive (sent off-list or not yet received): 45D89917.9090003-atKUWr5tajBWk0Htik3J/[email protected]

#13 lore raw
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Avi Kivity
Quoted 3 lines

I'll have to try and setup a 64 bit system. I cannot reproduce on my 32bit system with your test program.

Quoted 8 lines
Quoted 4 lines

It may be related to 64-bit (as that uses sse for floating point), or to the fact that I use ssh on a remote host, which causes more context switches. I'll try to reproduce on 32 bits with my setup.

It doesn't reproduce on a 32-bit host. However, it does reproduce on a 32-bit guest on a 64-bit host, and on a 32-bit user load running in a 64-bit guest on a 64-bit host.

Thanks, I'll setup a 64 bit partition and start debugging. I'm a bit confused though, I thought CR0.TS even covers SIMD instructions?

I'll take a look at Xen and see if it's doing something else.

Regards,

Anthony Liguori

Quoted 37 lines

I'm attaching an updated test program which makes it easier to spot the failures. You only need to run it on the guest, although running it on the host as well increases the failure rate significantly.

------------------------------------------------------------------------

#include <stdio.h>
#include <math.h>

double test_fpu_once()

{
    int i;
    double f = 0;

    for (i = 0; i < 10000000; ++i)
	f += 1 / (1.0 + i);
    return f;
}

void test_fpu()

{
    double a, b;
    int runs;

    runs = 0;
    a = test_fpu_once();
    while (1) {
	b = test_fpu_once();
	if (fabs(a - b) > 1e-9)
	    printf("error: %20.16f -> %20.16f\n", a, b);
	a = b;
	if (++runs % 100 == 0)
	    printf("runs: %8d\n", runs);
    }
}
Quoted 6 lines

int main(int ac, char **av)

{
    test_fpu();
    return 0;
}

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

A message that is not in the archive (sent off-list or not yet received): [email protected]

#15 lore raw
Quoted 11 lines · Anthony Liguori
Quoted 10 lines · Avi Kivity
Quoted 8 lines · Avi Kivity
Quoted 3 lines

I'll have to try and setup a 64 bit system. I cannot reproduce on my 32bit system with your test program.

It may be related to 64-bit (as that uses sse for floating point), or to the fact that I use ssh on a remote host, which causes more context switches. I'll try to reproduce on 32 bits with my setup.

Quoted 6 lines
Quoted 3 lines

It doesn't reproduce on a 32-bit host. However, it does reproduce on a 32-bit guest on a 64-bit host, and on a 32-bit user load running in a 64-bit guest on a 64-bit host.

Thanks, I'll setup a 64 bit partition and start debugging. I'm a bit confused though, I thought CR0.TS even covers SIMD instructions?

It does, and the fact that a 32-bit guest suffers from the same issue indicate that sse is not the cause.

Locking the vcpu to a cpu does not change the failures, so it isn't related to vcpu migration.

Signature · 8 lines
Do not meddle in the internals of kernels, for they are subtle and quick to panic.


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV