Commit 7649412
committed
KVM: x86: Load guest/host PKRU outside of the fastpath run loop
Move KVM's swapping of PKRU outside of the fastpath loop, as there is no
KVM code anywhere in the fastpath that accesses guest/userspace memory,
i.e. that can consume protection keys.
As documented by commit 1be0e61 ("KVM, pkeys: save/restore PKRU when
guest/host switches"), KVM just needs to ensure the host's PKRU is loaded
when KVM (or the kernel at-large) may access userspace memory. And at the
time of commit 1be0e61, KVM didn't have a fastpath, and PKU was
strictly contained to VMX, i.e. there was no reason to swap PKRU outside
of vmx_vcpu_run().
Over time, the "need" to swap PKRU close to VM-Enter was likely falsely
solidified by the association with XFEATUREs in commit 3748613
("KVM: x86: Fix pkru save/restore when guest CR4.PKE=0, move it to x86.c"),
and XFEATURE swapping was in turn moved close to VM-Enter/VM-Exit as a
KVM hack-a-fix ution for an #MC handler bug by commit 1811d97
("x86/kvm: move kvm_load/put_guest_xcr0 into atomic context").
Deferring the PKRU loads shaves ~40 cycles off the fastpath for Intel,
and ~60 cycles for AMD. E.g. using INVD in KVM-Unit-Test's vmexit.c,
with extra hacks to enable CR4.PKE and PKRU=(-1u & ~0x3), latency numbers
for AMD Turin go from ~1560 => ~1500, and for Intel Emerald Rapids, go
from ~810 => ~770.
Reviewed-by: Rick Edgecombe <rick.p.edgecombe@intel.com>
Reviewed-by: Jon Kohler <jon@nutanix.com>
Link: https://patch.msgid.link/20251118222328.2265758-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>1 parent 75c69c8 commit 7649412
4 files changed
+10
-12
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4250 | 4250 | | |
4251 | 4251 | | |
4252 | 4252 | | |
4253 | | - | |
4254 | 4253 | | |
4255 | 4254 | | |
4256 | 4255 | | |
| |||
4293 | 4292 | | |
4294 | 4293 | | |
4295 | 4294 | | |
4296 | | - | |
4297 | 4295 | | |
4298 | 4296 | | |
4299 | 4297 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7473 | 7473 | | |
7474 | 7474 | | |
7475 | 7475 | | |
7476 | | - | |
7477 | | - | |
7478 | 7476 | | |
7479 | 7477 | | |
7480 | 7478 | | |
| |||
7518 | 7516 | | |
7519 | 7517 | | |
7520 | 7518 | | |
7521 | | - | |
7522 | | - | |
7523 | 7519 | | |
7524 | 7520 | | |
7525 | 7521 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1235 | 1235 | | |
1236 | 1236 | | |
1237 | 1237 | | |
1238 | | - | |
| 1238 | + | |
1239 | 1239 | | |
1240 | 1240 | | |
1241 | 1241 | | |
| |||
1246 | 1246 | | |
1247 | 1247 | | |
1248 | 1248 | | |
1249 | | - | |
1250 | 1249 | | |
1251 | | - | |
| 1250 | + | |
1252 | 1251 | | |
1253 | 1252 | | |
1254 | 1253 | | |
| |||
1261 | 1260 | | |
1262 | 1261 | | |
1263 | 1262 | | |
1264 | | - | |
1265 | 1263 | | |
1266 | 1264 | | |
1267 | 1265 | | |
| |||
11303 | 11301 | | |
11304 | 11302 | | |
11305 | 11303 | | |
| 11304 | + | |
| 11305 | + | |
| 11306 | + | |
| 11307 | + | |
| 11308 | + | |
| 11309 | + | |
11306 | 11310 | | |
11307 | 11311 | | |
11308 | 11312 | | |
| |||
11331 | 11335 | | |
11332 | 11336 | | |
11333 | 11337 | | |
| 11338 | + | |
| 11339 | + | |
11334 | 11340 | | |
11335 | 11341 | | |
11336 | 11342 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
636 | 636 | | |
637 | 637 | | |
638 | 638 | | |
639 | | - | |
640 | | - | |
641 | 639 | | |
642 | 640 | | |
643 | 641 | | |
| |||
0 commit comments