From patchwork Sun Dec 10 21:44:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Wanpeng Li X-Patchwork-Id: 10104025 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 08FE5602A7 for ; Sun, 10 Dec 2017 21:44:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EDE2D2929E for ; Sun, 10 Dec 2017 21:44:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DF546292A2; Sun, 10 Dec 2017 21:44:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 990EE2929E for ; Sun, 10 Dec 2017 21:44:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752044AbdLJVoi (ORCPT ); Sun, 10 Dec 2017 16:44:38 -0500 Received: from mail-pf0-f193.google.com ([209.85.192.193]:37176 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751420AbdLJVof (ORCPT ); Sun, 10 Dec 2017 16:44:35 -0500 Received: by mail-pf0-f193.google.com with SMTP id n6so10097607pfa.4; Sun, 10 Dec 2017 13:44:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+pFXoxHfT3LQz5+QG5pXzBrSjLaAyitS6ThQ69AbJxY=; b=W6YMZeHAtjJoxDc4GfOLsrJpcIpWlzWgm0csAvqmy07Mipyc6Ftq+A8gf7gXgXvxh2 cLxpb+Y5KcrmMQsf608NtO4vLNKWMUO+22PqbQEBO7gMAMxKbACbKUWjylaIKFfmARu7 KU8+OniO8ccESJSNPKJEiTmEkchyrSGKDGVVTJ2ZoXoh+3nEn/hMHtV34R5Vdvn/7xNc 6gKEfiLEsfCm2nhH8q6d8oZcN67a5JA8LYpvyQ8kUTXkSgCFDmFjoqxYmKqu78V7JTp2 IwZ066IYPL+HG+ZAmx795DYRCjGW83ygC9xegLK6QaDeYo5f6+u+WbR5ISFInTImvK1U CQvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=+pFXoxHfT3LQz5+QG5pXzBrSjLaAyitS6ThQ69AbJxY=; b=S0kIzrQItVhxl8NlXwgHYI/Rs5Ej/Cc9d3ELqnejcH1oh9e2nuV7hbwCM1qdZ5NCvl OtgipxA/5EglcdhRDQlhjZ12p65eLb+Yzs0oJjQRMpd/2W66qcSsMmebcT7okNCbg4p4 DXMiNeVyfdtuSQJRSZcZUqhbGZ4ImfYDiG4LZz/hT8EUYF0NS7yqjBfM1/7yJ7ZirMDP CkrQuIJ3q6f4C8Tm1Q0ZwZdTdovlgpVtQycP9ppgeTXtaCM5L5dTPvDY9/pjzc3aMq4M YNJwQfV2cQo4XuNnzoWps5sZtfqHxjzaPFttHCO2GV1yA54eMrkIDbWGOSl0FnsN2Hsx MqcA== X-Gm-Message-State: AJaThX6oiGDenNXJHGCdEoFHPhgAc8lujZ9cM3C8wPHxTFB0L2dFpG6R NoUG8z4gKnSZqOJ1uohgrWHBWg== X-Google-Smtp-Source: AGs4zMY23tIMPqJg5XMpLRVhHdYzBjMuSnf66R6DM7aJi0JFDPwhGtjN0/8A3Q9D1OsdN4GguoyHcg== X-Received: by 10.99.108.67 with SMTP id h64mr36638255pgc.119.1512942274897; Sun, 10 Dec 2017 13:44:34 -0800 (PST) Received: from localhost ([45.248.77.205]) by smtp.gmail.com with ESMTPSA id i186sm20934540pfg.178.2017.12.10.13.44.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Dec 2017 13:44:34 -0800 (PST) From: Wanpeng Li X-Google-Original-From: Wanpeng Li To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Wanpeng Li , Rik van Riel , stable@vger.kernel.org Subject: [PATCH RESEND] KVM: X86: Fix load bad host fpu state Date: Sun, 10 Dec 2017 13:44:25 -0800 Message-Id: <1512942265-40606-1-git-send-email-wanpeng.li@hotmail.com> X-Mailer: git-send-email 2.7.4 MIME-Version: 1.0 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wanpeng Li ------------[ cut here ]------------ Bad FPU state detected at kvm_put_guest_fpu+0xd8/0x2d0 [kvm], reinitializing FPU registers. WARNING: CPU: 1 PID: 4594 at arch/x86/mm/extable.c:103 ex_handler_fprestore+0x88/0x90 CPU: 1 PID: 4594 Comm: qemu-system-x86 Tainted: G B OE 4.15.0-rc2+ #10 RIP: 0010:ex_handler_fprestore+0x88/0x90 Call Trace: fixup_exception+0x4e/0x60 do_general_protection+0xff/0x270 general_protection+0x22/0x30 RIP: 0010:kvm_put_guest_fpu+0xd8/0x2d0 [kvm] RSP: 0018:ffff8803d5627810 EFLAGS: 00010246 kvm_vcpu_reset+0x3b4/0x3c0 [kvm] kvm_apic_accept_events+0x1c0/0x240 [kvm] kvm_arch_vcpu_ioctl_run+0x1658/0x2fb0 [kvm] kvm_vcpu_ioctl+0x479/0x880 [kvm] do_vfs_ioctl+0x142/0x9a0 SyS_ioctl+0x74/0x80 do_syscall_64+0x15f/0x600 This can be reproduced by running any testcase in kvm-unit-tests since the qemu userspace FPU context is not initialized, which results in the init path from kvm_apic_accept_events() will load/put qemu userspace FPU context w/o initialized. In addition, w/o this splatting we still should initialize vcpu->arch.user_fpu instead of current->thread.fpu. This patch fixes it by initializing qemu user FPU context if it is uninitialized before KVM_RUN. Cc: Paolo Bonzini Cc: Radim Krčmář Cc: Rik van Riel Cc: stable@vger.kernel.org Fixes: f775b13eedee (x86,kvm: move qemu/guest FPU switching out to vcpu_run) Signed-off-by: Wanpeng Li --- arch/x86/kvm/x86.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index a92b22f..063a643 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7273,10 +7273,13 @@ static int complete_emulated_mmio(struct kvm_vcpu *vcpu) int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) { - struct fpu *fpu = ¤t->thread.fpu; + struct fpu *fpu = &vcpu->arch.user_fpu; int r; - fpu__initialize(fpu); + if (!fpu->initialized) { + fpstate_init(&fpu->state); + fpu->initialized = 1; + } kvm_sigset_activate(vcpu);