Skip to content

Commit 1c2361f

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Use __try_cmpxchg_user() to emulate atomic accesses
Use the recently introduce __try_cmpxchg_user() to emulate atomic guest accesses via the associated userspace address instead of mapping the backing pfn into kernel address space. Using kvm_vcpu_map() is unsafe as it does not coordinate with KVM's mmu_notifier to ensure the hva=>pfn translation isn't changed/unmapped in the memremap() path, i.e. when there's no struct page and thus no elevated refcount. Fixes: 42e35f8 ("KVM/X86: Use kvm_vcpu_map in emulator_cmpxchg_emulated") Cc: [email protected] Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent f122dfe commit 1c2361f

File tree

1 file changed

+14
-21
lines changed

1 file changed

+14
-21
lines changed

arch/x86/kvm/x86.c

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7257,15 +7257,8 @@ static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
72577257
exception, &write_emultor);
72587258
}
72597259

7260-
#define CMPXCHG_TYPE(t, ptr, old, new) \
7261-
(cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
7262-
7263-
#ifdef CONFIG_X86_64
7264-
# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
7265-
#else
7266-
# define CMPXCHG64(ptr, old, new) \
7267-
(cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
7268-
#endif
7260+
#define emulator_try_cmpxchg_user(t, ptr, old, new) \
7261+
(__try_cmpxchg_user((t __user *)(ptr), (t *)(old), *(t *)(new), efault ## t))
72697262

72707263
static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
72717264
unsigned long addr,
@@ -7274,12 +7267,11 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
72747267
unsigned int bytes,
72757268
struct x86_exception *exception)
72767269
{
7277-
struct kvm_host_map map;
72787270
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
72797271
u64 page_line_mask;
7272+
unsigned long hva;
72807273
gpa_t gpa;
7281-
char *kaddr;
7282-
bool exchanged;
7274+
int r;
72837275

72847276
/* guests cmpxchg8b have to be emulated atomically */
72857277
if (bytes > 8 || (bytes & (bytes - 1)))
@@ -7303,31 +7295,32 @@ static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
73037295
if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask))
73047296
goto emul_write;
73057297

7306-
if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
7298+
hva = kvm_vcpu_gfn_to_hva(vcpu, gpa_to_gfn(gpa));
7299+
if (kvm_is_error_hva(addr))
73077300
goto emul_write;
73087301

7309-
kaddr = map.hva + offset_in_page(gpa);
7302+
hva += offset_in_page(gpa);
73107303

73117304
switch (bytes) {
73127305
case 1:
7313-
exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
7306+
r = emulator_try_cmpxchg_user(u8, hva, old, new);
73147307
break;
73157308
case 2:
7316-
exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
7309+
r = emulator_try_cmpxchg_user(u16, hva, old, new);
73177310
break;
73187311
case 4:
7319-
exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
7312+
r = emulator_try_cmpxchg_user(u32, hva, old, new);
73207313
break;
73217314
case 8:
7322-
exchanged = CMPXCHG64(kaddr, old, new);
7315+
r = emulator_try_cmpxchg_user(u64, hva, old, new);
73237316
break;
73247317
default:
73257318
BUG();
73267319
}
73277320

7328-
kvm_vcpu_unmap(vcpu, &map, true);
7329-
7330-
if (!exchanged)
7321+
if (r < 0)
7322+
goto emul_write;
7323+
if (r)
73317324
return X86EMUL_CMPXCHG_FAILED;
73327325

73337326
kvm_page_track_write(vcpu, gpa, new, bytes);

0 commit comments

Comments
 (0)