From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751958AbcFUONM (ORCPT ); Tue, 21 Jun 2016 10:13:12 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37670 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751576AbcFUOMv (ORCPT ); Tue, 21 Jun 2016 10:12:51 -0400 Subject: Re: kvm: GPF in kvm_lapic_set_tpr To: Dmitry Vyukov , mtosatti@redhat.com, yoshikawa_takuya_b1@lab.ntt.co.jp, guangrong.xiao@linux.intel.com, Steve Rutherford , rkrcmar@redhat.com, KVM list , LKML References: Cc: syzkaller , Alexander Potapenko , Sasha Levin , Kostya Serebryany , Peter Zijlstra From: Paolo Bonzini Message-ID: Date: Tue, 21 Jun 2016 16:12:39 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.1.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 21 Jun 2016 14:12:45 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 21/06/2016 15:18, Dmitry Vyukov wrote: > [] kvm_lapic_set_tpr+0x5e/0xe0 arch/x86/kvm/lapic.c:1648 > [] kvm_set_cr8.part.135+0x2c/0xb0 arch/x86/kvm/x86.c:809 > [] kvm_set_cr8+0x32/0x50 arch/x86/kvm/x86.c:806 > [] vmx_vcpu_reset+0x114/0xb60 arch/x86/kvm/vmx.c:4996 > [] kvm_vcpu_reset+0x410/0x580 arch/x86/kvm/x86.c:7470 > [] kvm_arch_vcpu_setup+0x35/0x60 arch/x86/kvm/x86.c:7393 > [< inline >] kvm_vm_ioctl_create_vcpu virt/kvm/kvm_main.c:2355 > [] kvm_vm_ioctl+0x582/0x10d0 virt/kvm/kvm_main.c:2839 > [< inline >] vfs_ioctl fs/ioctl.c:43 > [] do_vfs_ioctl+0x18c/0xff0 fs/ioctl.c:674 > [< inline >] SYSC_ioctl fs/ioctl.c:689 > [] SyS_ioctl+0x8f/0xc0 fs/ioctl.c:680 > [] entry_SYSCALL_64_fastpath+0x23/0xc1 The bug is not in KVM. Modulo the shuffling of system calls from threading, the program is equivalent to this: long r[7]; int main(void) { struct kvm_vcpu_events ve; r[0] = open("/dev/kvm", O_RDONLY); r[1] = ioctl(r[0], KVM_CREATE_VM, 0x0ul, 0, 0, 0); r[2] = ioctl(r[1], KVM_CREATE_VCPU, 0x1ul, 0, 0, 0); r[3] = ioctl(r[2], KVM_RUN, 0, 0, 0, 0); r[4] = ioctl(r[2], KVM_RUN, 0, 0, 0, 0); r[5] = ioctl(r[2], KVM_RUN, 0, 0, 0, 0); r[6] = ioctl(r[2], KVM_GET_VCPU_EVENTS, &ve, 0, 0, 0); return 0; } However, it is easy to see that there is a clear critical path in the bug, so that the threading must be completely irrelevant: 1) r[1] was valid in KVM_CREATE_VCPU, so KVM_CREATE_VCPU started after the assignment of KVM_CREATE_VM 2) r[0] was valid in KVM_CREATE_VM, so KVM_CREATE_VM started after the assignment of open("/dev/kvm") 3) the assignment of r[2] must not have happened because of the backtrace, so KVM_CREATE_VCPU has not finished and the other ioctls will not get to KVM. The GP _could_ happen because of a bug, because lapic_in_kernel attempts to skip a NULL check using a static key. But the static key is incremented before the point that is in the backtrace: kvm_vm_ioctl_create_vcpu (virt/kvm/kvm_main.c) kvm_arch_vcpu_create (arch/x86/kvm/x86.c) vmx_create_vcpu (arch/x86/kvm/vmx.c) kvm_vcpu_init (virt/kvm/kvm_main.c) kvm_arch_vcpu_init (arch/x86/kvm/x86.c) static_key_slow_inc <- <- <- <- kvm_arch_vcpu_setup (arch/x86/kvm/x86.c) kvm_vcpu_reset (arch/x86/kvm/x86.c) vmx_vcpu_reset (arch/x86/kvm/vmx.c) kvm_set_cr8 (arch/x86/kvm/x86.c) *mumble mumble* Ok, the bug shows up when you do two concurrent calls to static_key_slow_inc and is caused by commit 706249c22. Patch on its way. Paolo