Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp996732pxj; Thu, 27 May 2021 17:21:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/DixhNodNHAL+xQJi9xNflQG/k/hfQAyklW1bdzbGYSNojSxHFeGVhQWnWLQss9NsqBcP X-Received: by 2002:aa7:dd55:: with SMTP id o21mr7139696edw.295.1622161302621; Thu, 27 May 2021 17:21:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622161302; cv=none; d=google.com; s=arc-20160816; b=thxupRcVKjrDZT+WPTr7dsb1Qz2E07FVwMWJ66Lindx1RvDNYf1eenlM8ip6g3Ujue 1ORGsWeZP/IJwXb9HermGx9xs6DONz4yVtniDnggBnKAtSqDYZJp+hW/QdOJvwsK4rSR MTuCRABY8gr195bmwGrnsE6wbenzvExkCyvJy9mCDs9W1gRYkeY66SuODihqCH8czJTg ZTEYhkDmquUh1doP/V9kuA8LdoPHcty4iPfhGUBmj9Ps/GXcgSZky6lhhmgnbUKFREqD qckyFdxb9RxjDvS4Iod8MePf5/Z3gSi4d9bkb/GGBmRS/cC/GnylYTa4oqAprSD7EdOX g0Cg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=S9besEpmPZmfRI3aBf2tbBwcQShTomayh485iY22oVA=; b=0giYYYLBiztTgkzK2Q8ZYHsl+iXIM0RmWvckC8WQRRNxb1hspdIC7rhUuc7NB0IzuC g/SbYabGq2Sgpm+Zj+B4P+ojnj1YBZ4A9Zv8Q1Is24o9cm9UxQgMoPz+DWVN7CMUdlk3 Z2xyof1iIVQdGy+TTz+Nxa/m7ngl8YVadKZDxGcbEZcqX30cLPmlOUCKDMTNNsK64V9Q uoO/1AvVCGrCNmJorRngN/UTDenB4iR6U50uAoootzqvfiFfnL1N4ZAbjN5HXideDpJV CVIWz4F3CQiLgK44wR1puCA+sIhbTr8nto+zvSiJk+4Gvp4MHCddZ9WTuDA/Nzd7gy8D L+lQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oUWb+S9P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 5si3383045ejw.423.2021.05.27.17.21.19; Thu, 27 May 2021 17:21:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oUWb+S9P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236430AbhE0XFM (ORCPT + 99 others); Thu, 27 May 2021 19:05:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51598 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233203AbhE0XFL (ORCPT ); Thu, 27 May 2021 19:05:11 -0400 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 97286C061574; Thu, 27 May 2021 16:03:37 -0700 (PDT) Received: by mail-ej1-x632.google.com with SMTP id l1so2567111ejb.6; Thu, 27 May 2021 16:03:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=S9besEpmPZmfRI3aBf2tbBwcQShTomayh485iY22oVA=; b=oUWb+S9PbFqilEYFBHn/Z4KzrfSY6rB+h6crboBnNCYufx5Ta4+0SvKfjpdZBEcfMH XtfZjtfuJ58kVrC2AcgnAQs+mCV1OSF7PeZtw/Kx1cUreyNxEnFORHtTdkFKq+0JIsvz p5HNpFmtVPYHZEvRSHEoGGj4tKbl17PZ97fNP6sX4F2EoVWAMehS9Xoi4qSRNA/EW4Lk GMkUxVbxmBzZRKyMzjGQrsAKPAiR2DdAf1lJ+3lPDM5qXeGUNPpsTuFRzZhwidbcSZwB sKUaBQldWyNN+49i6tt3+tp/zlOlWebBkefQiuWclZTobLC4qakJv8loK1inHrDmvWqD SvnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=S9besEpmPZmfRI3aBf2tbBwcQShTomayh485iY22oVA=; b=hY2KbdEnhTGEd3YUzHAXsF+O/jUworlJzHhnIfNaZsQZ+Vw09TV7onJ/b2lud54biI HfBPNtSEGXZyFUF6US8vhO9uTuE5oQF+pWy2fgLDNNGIRc6/vlD8jALfMtSZCUN8hUYX 0MOfFuI927vmsN/JLyXktyCtdDV9gRg20kYmmbjMErLgL2gvhfW2dZ3mvX3QR0UiNP7/ L9ltTSbA+nMcQIIfRxqMDiQYFYAWOftqK4Cc2+DedN9qbPUUJP1MIwF6ct0dshGKhdnw rVQlVNLhrqWm7VeniZis4jCzF8y4xE41kmYQrIKccOREBtHHHHktG0Ss3PmfGYAtKC34 TDWw== X-Gm-Message-State: AOAM532qTB5WL9tyoUIY6tAQeKJ1a4AtkIRWxb6yojRoy+Ydpb1tzvhI fr2gK3EdnZofficMt/sMX59FoaY2d+UDTKNd9nw= X-Received: by 2002:a17:906:f285:: with SMTP id gu5mr6402516ejb.226.1622156615069; Thu, 27 May 2021 16:03:35 -0700 (PDT) MIME-Version: 1.0 References: <1622091679-31683-1-git-send-email-wanpengli@tencent.com> In-Reply-To: From: Wanpeng Li Date: Fri, 28 May 2021 07:03:24 +0800 Message-ID: Subject: Re: [PATCH v3 1/2] KVM: X86: Fix warning caused by stale emulation context To: Sean Christopherson Cc: LKML , kvm , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 28 May 2021 at 01:03, Sean Christopherson wrote: > > On Wed, May 26, 2021, Wanpeng Li wrote: > > From: Wanpeng Li > > > > Reported by syzkaller: > > > > WARNING: CPU: 7 PID: 10526 at /home/kernel/ssd/linux/arch/x86/kvm//x86.c:7621 x86_emulate_instruction+0x41b/0x510 [kvm] > > "/home/kernel/ssd/linux/" can be omitted to make the line length a bit shorter. > checkpatch also complains about using absolute path instead of relative path. > > > RIP: 0010:x86_emulate_instruction+0x41b/0x510 [kvm] > > Call Trace: > > kvm_mmu_page_fault+0x126/0x8f0 [kvm] > > vmx_handle_exit+0x11e/0x680 [kvm_intel] > > vcpu_enter_guest+0xd95/0x1b40 [kvm] > > kvm_arch_vcpu_ioctl_run+0x377/0x6a0 [kvm] > > kvm_vcpu_ioctl+0x389/0x630 [kvm] > > __x64_sys_ioctl+0x8e/0xd0 > > do_syscall_64+0x3c/0xb0 > > entry_SYSCALL_64_after_hwframe+0x44/0xae > > > > Commit 4a1e10d5b5d8c (KVM: x86: handle hardware breakpoints during emulation()) > > adds hardware breakpoints check before emulation the instruction and parts of > > emulation context initialization, actually we don't have the EMULTYPE_NO_DECODE flag > > here and the emulation context will not be reused. Commit c8848cee74ff (KVM: x86: > > set ctxt->have_exception in x86_decode_insn()) triggers the warning because it > > catches the stale emulation context has #UD, however, it is not during instruction > > decoding which should result in EMULATION_FAILED. This patch fixes it by moving > > the second part emulation context initialization into init_emulate_ctxt() and > > before hardware breakpoints check. > > > > syzkaller source: https://syzkaller.appspot.com/x/repro.c?x=134683fdd00000 > > > > Reported-by: syzbot+71271244f206d17f6441@syzkaller.appspotmail.com > > Fixes: 4a1e10d5b5d8 (KVM: x86: handle hardware breakpoints during emulation) > > Signed-off-by: Wanpeng Li > > --- > > v2 -> v3: > > * squash ctxt->ud > > v1 -> v2: > > * move the second part emulation context initialization into init_emulate_ctxt() > > > > arch/x86/kvm/x86.c | 14 +++++++------- > > 1 file changed, 7 insertions(+), 7 deletions(-) > > > > diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c > > index bbc4e04..ae47b19 100644 > > --- a/arch/x86/kvm/x86.c > > +++ b/arch/x86/kvm/x86.c > > @@ -7226,6 +7226,13 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) > > BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); > > BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); > > > > + ctxt->interruptibility = 0; > > + ctxt->have_exception = false; > > + ctxt->exception.vector = -1; > > + ctxt->perm_ok = false; > > + > > + ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; > > "ctxt->ud" should be left where it is in patch 01. "emulation_type" isn't passed > to init_emulate_ctxt(), and I don't see any reason to add it to the params since > ctxt->ud is only consumed by x86_decode_insn(), i.e. moving ctxt->ud isn't > necessary to fix the bug. How about this? diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bbc4e04..dba8077 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -7226,6 +7226,11 @@ static void init_emulate_ctxt(struct kvm_vcpu *vcpu) BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); + ctxt->interruptibility = 0; + ctxt->have_exception = false; + ctxt->exception.vector = -1; + ctxt->perm_ok = false; + init_decode_cache(ctxt); vcpu->arch.emulate_regs_need_sync_from_vcpu = false; } @@ -7561,11 +7566,6 @@ int x86_decode_emulated_instruction(struct kvm_vcpu *vcpu, int emulation_type, kvm_vcpu_check_breakpoint(vcpu, &r)) return r; - ctxt->interruptibility = 0; - ctxt->have_exception = false; - ctxt->exception.vector = -1; - ctxt->perm_ok = false; - ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; r = x86_decode_insn(ctxt, insn, insn_len);