Skip to contentSkip to conversation

KVM: emulator: do not needlesly sync registers from emulator ctxt to vcpu

PATCH Superseded Gleb Natapov 2 participants 16 messages Mar 29, 2011 – Mar 30, 2011

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: emulator: do not needlesly sync registers from emulator ctxt to vcpu

Currently we sync registers back and forth before/after exiting to userspace for IO, but during IO device model shouldn't need to read/write the registers, so we can as well skip those sync points. The only exaception is broken vmware backdor interface. The new code sync registers content during IO only if registers are read from/written to by userspace in the middle of the IO operation and this almost never happens in practise.

arch/x86/include/asm/kvm_host.h +2 -0
index 35f81b1..3bf77c2 100644
@@ -369,6 +369,8 @@ struct kvm_vcpu_arch {
 	/* emulate context */
 
 	struct x86_emulate_ctxt emulate_ctxt;
+	bool emulate_regs_need_sync_to_vcpu;
+	bool emulate_regs_need_sync_from_vcpu;
 
 	gpa_t time;
 	struct pvclock_vcpu_time_info hv_clock;
arch/x86/kvm/x86.c +36 -7

90 lines not on this page · raw message

--
Gleb.

#1 lore raw
Quoted 7 lines · Gleb Natapov

Currently we sync registers back and forth before/after exiting to userspace for IO, but during IO device model shouldn't need to read/write the registers, so we can as well skip those sync points. The only exaception is broken vmware backdor interface. The new code sync registers content during IO only if registers are read from/written to by userspace in the middle of the IO operation and this almost never happens in practise.

While this is a nice idea, how much does it save in practice? It does introduce more complexity.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Gleb Natapov

Currently we sync registers back and forth before/after exiting to userspace for IO, but during IO device model shouldn't need to read/write the registers, so we can as well skip those sync points. The only exaception is broken vmware backdor interface. The new code sync registers content during IO only if registers are read from/written to by userspace in the middle of the IO operation and this almost never happens in practise.

Quoted 3 lines

While this is a nice idea, how much does it save in practice? It does introduce more complexity.

I haven't measured, but can try to do so. It eliminates two copies of all registers on each MMIO/PIO read. I expect this to be measurable in workloads that do many such reads.

--
Gleb.

#3 lore raw
Quoted 10 lines · Gleb Natapov
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Gleb Natapov

Currently we sync registers back and forth before/after exiting to userspace for IO, but during IO device model shouldn't need to read/write the registers, so we can as well skip those sync points. The only exaception is broken vmware backdor interface. The new code sync registers content during IO only if registers are read from/written to by userspace in the middle of the IO operation and this almost never happens in practise.

Quoted 8 lines
Quoted 3 lines

While this is a nice idea, how much does it save in practice? It does introduce more complexity.

I haven't measured, but can try to do so. It eliminates two copies of all registers on each MMIO/PIO read. I expect this to be measurable in workloads that do many such reads.

I don't, especially if these are mmios to userspace. Perhaps it's better to remove the copy on kernel mmio, since it's much faster, if the result is simpler (there can be no KVM_SET_REGS in that context).

Signature · 1 line
error compiling committee.c: too many arguments to function
#4 lore raw
Quoted 20 lines · Avi Kivity
Quoted 10 lines · Gleb Natapov
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Gleb Natapov

Currently we sync registers back and forth before/after exiting to userspace for IO, but during IO device model shouldn't need to read/write the registers, so we can as well skip those sync points. The only exaception is broken vmware backdor interface. The new code sync registers content during IO only if registers are read from/written to by userspace in the middle of the IO operation and this almost never happens in practise.

Quoted 8 lines
Quoted 3 lines

While this is a nice idea, how much does it save in practice? It does introduce more complexity.

I haven't measured, but can try to do so. It eliminates two copies of all registers on each MMIO/PIO read. I expect this to be measurable in workloads that do many such reads.

Quoted 5 lines

I don't, especially if these are mmios to userspace. Perhaps it's better to remove the copy on kernel mmio, since it's much faster, if the result is simpler (there can be no KVM_SET_REGS in that context).

The patch saves copying of 256 bytes on each MMIO/PIO read. It may not save a lot comparing to time it takes to do one MMIO to userspace, but do 1 million of those and you saved a lot of CPU cycles. I do not think we should abandon the optimization so easily. Unfortunately I can't run perf on 2.6.38 kernel right now. It gives me strange errors and when it doesn't it makes kernel OOPS.

--
Gleb.

#5 lore raw
Quoted 27 lines · Gleb Natapov
Quoted 20 lines · Avi Kivity
Quoted 10 lines · Gleb Natapov
Quoted 9 lines · Avi Kivity
Quoted 7 lines · Gleb Natapov

Currently we sync registers back and forth before/after exiting to userspace for IO, but during IO device model shouldn't need to read/write the registers, so we can as well skip those sync points. The only exaception is broken vmware backdor interface. The new code sync registers content during IO only if registers are read from/written to by userspace in the middle of the IO operation and this almost never happens in practise.

Quoted 8 lines
Quoted 3 lines

While this is a nice idea, how much does it save in practice? It does introduce more complexity.

I haven't measured, but can try to do so. It eliminates two copies of all registers on each MMIO/PIO read. I expect this to be measurable in workloads that do many such reads.

Quoted 5 lines

I don't, especially if these are mmios to userspace. Perhaps it's better to remove the copy on kernel mmio, since it's much faster, if the result is simpler (there can be no KVM_SET_REGS in that context).

Quoted 7 lines

The patch saves copying of 256 bytes on each MMIO/PIO read. It may not save a lot comparing to time it takes to do one MMIO to userspace, but do 1 million of those and you saved a lot of CPU cycles. I do not think we should abandon the optimization so easily. Unfortunately I can't run perf on 2.6.38 kernel right now. It gives me strange errors and when it doesn't it makes kernel OOPS.

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

--
Gleb.

#6 lore raw
Quoted 12 lines · Gleb Natapov
Quoted 8 lines

The patch saves copying of 256 bytes on each MMIO/PIO read. It may not save a lot comparing to time it takes to do one MMIO to userspace, but do 1 million of those and you saved a lot of CPU cycles. I do not think we should abandon the optimization so easily. Unfortunately I can't run perf on 2.6.38 kernel right now. It gives me strange errors and when it doesn't it makes kernel OOPS.

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

Quoted 9 lines
1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

Well, that is probably significant.

Signature · 1 line
error compiling committee.c: too many arguments to function
#7 lore raw
Quoted 22 lines · Avi Kivity
Quoted 21 lines · Gleb Natapov
Quoted 2 lines

The patch saves copying of 256 bytes on each MMIO/PIO read. It may

not

Quoted 1 line

save a lot comparing to time it takes to do one MMIO to userspace,

but

Quoted 1 line

do 1 million of those and you saved a lot of CPU cycles. I do not

think

Quoted 1 line

we should abandon the optimization so easily. Unfortunately I

can't run

Quoted 1 line

perf on 2.6.38 kernel right now. It gives me strange errors and

when it

Quoted 2 lines

doesn't it makes kernel OOPS.

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
Quoted 8 lines
Quoted 5 lines

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

Well, that is probably significant.

Please rebase the patch after sse-mmio goes in.

Signature · 1 line
error compiling committee.c: too many arguments to function
#8 lore raw
In reply to Gleb Natapov
Quoted 4 lines · Gleb Natapov

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

Quoted 9 lines
1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

The cause might be kvm_rip_write() using vmwrite. Can you use perf to see where the hits are in x86_emulate_instruction?

If that's the case, we may be able to do local optimizations to kvm_rip_write(), kvm_set_rflags(), and toggle_interruptiblity() instead of this global change.

Signature · 1 line
error compiling committee.c: too many arguments to function
#9 lore raw
Quoted 15 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

Quoted 9 lines
1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
Quoted 7 lines

The cause might be kvm_rip_write() using vmwrite. Can you use perf to see where the hits are in x86_emulate_instruction?

If that's the case, we may be able to do local optimizations to kvm_rip_write(), kvm_set_rflags(), and toggle_interruptiblity() instead of this global change.

I can leave copying there and eliminate only kvm_rip_write and see perf data.

--
Gleb.

#10 lore raw
Quoted 19 lines · Gleb Natapov
Quoted 18 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

Quoted 9 lines
1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

The cause might be kvm_rip_write() using vmwrite. Can you use perf to see where the hits are in x86_emulate_instruction?

Quoted 7 lines
Quoted 4 lines

If that's the case, we may be able to do local optimizations to kvm_rip_write(), kvm_set_rflags(), and toggle_interruptiblity() instead of this global change.

I can leave copying there and eliminate only kvm_rip_write and see perf data.

1.75%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.60%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.42%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

This is with copy in place, but those are under if (writeback):

                toggle_interruptibility(vcpu,
                                vcpu->arch.emulate_ctxt.interruptibility);
                kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
                kvm_make_request(KVM_REQ_EVENT, vcpu);
                vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
                kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);

--
Gleb.

#11 lore raw
Quoted 32 lines · Gleb Natapov
Quoted 19 lines · Gleb Natapov
Quoted 18 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

Quoted 9 lines
1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

The cause might be kvm_rip_write() using vmwrite. Can you use perf to see where the hits are in x86_emulate_instruction?

Quoted 7 lines
Quoted 4 lines

If that's the case, we may be able to do local optimizations to kvm_rip_write(), kvm_set_rflags(), and toggle_interruptiblity() instead of this global change.

I can leave copying there and eliminate only kvm_rip_write and see perf data.

1.75%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.60%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.42%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
Quoted 8 lines

This is with copy in place, but those are under if (writeback):

                 toggle_interruptibility(vcpu,
                                 vcpu->arch.emulate_ctxt.interruptibility);
                 kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
                 kvm_make_request(KVM_REQ_EVENT, vcpu);
                 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
                 kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);

It's wierd. Do you get perf hits in the copying?

Copying a couple of hot cache lines shouldn't take any measurable time compared to a heavyweight exit.

Signature · 1 line
error compiling committee.c: too many arguments to function
#12 lore raw
Quoted 42 lines · Avi Kivity
Quoted 32 lines · Gleb Natapov
Quoted 19 lines · Gleb Natapov
Quoted 18 lines · Avi Kivity
Quoted 4 lines · Gleb Natapov

After reboot perf started to work. I ran modified emulator.flat unit test. It was modified to run test_cmps() in an endless loop.

Without patch:

Quoted 9 lines
1.71%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.51%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.68%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

With patch:

0.84%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.96%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
0.89%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction

The cause might be kvm_rip_write() using vmwrite. Can you use perf to see where the hits are in x86_emulate_instruction?

Quoted 7 lines
Quoted 4 lines

If that's the case, we may be able to do local optimizations to kvm_rip_write(), kvm_set_rflags(), and toggle_interruptiblity() instead of this global change.

I can leave copying there and eliminate only kvm_rip_write and see perf data.

1.75%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.60%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
1.42%  qemu-system-x86  [kvm]                 [k] x86_emulate_instruction
Quoted 8 lines

This is with copy in place, but those are under if (writeback):

                toggle_interruptibility(vcpu,
                                vcpu->arch.emulate_ctxt.interruptibility);
                kvm_set_rflags(vcpu, vcpu->arch.emulate_ctxt.eflags);
                kvm_make_request(KVM_REQ_EVENT, vcpu);
                vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
                kvm_rip_write(vcpu, vcpu->arch.emulate_ctxt.eip);
Quoted 2 lines

It's wierd. Do you get perf hits in the copying?

How can I check. The memcpy is inlined.

Quoted 3 lines

Copying a couple of hot cache lines shouldn't take any measurable time compared to a heavyweight exit.

The whole function takes only 1.5% CPU. Perf measures how much this function become faster and heavyweight exit is not part of the function.

--
Gleb.

#13 lore raw
Quoted 5 lines · Gleb Natapov
Quoted 3 lines

It's wierd. Do you get perf hits in the copying?

How can I check. The memcpy is inlined.

perf annotate x86_emulate_instruction

(newer perf allows you to get there interactively from 'perf report')

Quoted 5 lines
Quoted 3 lines

Copying a couple of hot cache lines shouldn't take any measurable time compared to a heavyweight exit.

The whole function takes only 1.5% CPU. Perf measures how much this function become faster and heavyweight exit is not part of the function.

It's still relative to exit cost. If the total exit was 2 us, then a 1% decrease in cost translates to 40 ns.

(well, that's not unlikely for a 256 byte memcpy, but let's be sure).

Signature · 1 line
error compiling committee.c: too many arguments to function
#14 lore raw
Quoted 12 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov
Quoted 3 lines

It's wierd. Do you get perf hits in the copying?

How can I check. The memcpy is inlined.

perf annotate x86_emulate_instruction

(newer perf allows you to get there interactively from 'perf report')

Quoted 1 line
Quoted 1 line

Copying a couple of hot cache lines shouldn't take any measurable

Ah, forgot about it:

First one:
27.71 : 1179f: f3 a5 rep movsl %ds:(%rsi),%es:(%rdi)

Second one:
32.68 : 11888: f3 a5 rep movsl %ds:(%rsi),%es:(%rdi)

Quoted 12 lines
Quoted 4 lines
Quoted 2 lines

time compared to a heavyweight exit.

The whole function takes only 1.5% CPU. Perf measures how much this function become faster and heavyweight exit is not part of the function.

It's still relative to exit cost. If the total exit was 2 us, then a 1% decrease in cost translates to 40 ns.

(well, that's not unlikely for a 256 byte memcpy, but let's be sure).

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

--
Gleb.

#15 lore raw
Quoted 12 lines · Gleb Natapov
Quoted 11 lines · Avi Kivity
Quoted 5 lines · Gleb Natapov
Quoted 3 lines

It's wierd. Do you get perf hits in the copying?

How can I check. The memcpy is inlined.

perf annotate x86_emulate_instruction

(newer perf allows you to get there interactively from 'perf report')

Quoted 9 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line

Copying a couple of hot cache lines shouldn't take any measurable

Ah, forgot about it:

First one:
27.71 : 1179f: f3 a5 rep movsl %ds:(%rsi),%es:(%rdi)

Second one:
32.68 : 11888: f3 a5 rep movsl %ds:(%rsi),%es:(%rdi)

Okay, I'm convinced.

(looks like a missed optimization, should use rep movsq there)

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