Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp3873635imm; Sun, 13 May 2018 22:15:30 -0700 (PDT) X-Google-Smtp-Source: AB8JxZo/WUKnIcLv+h2WyV0tn/wIbrBk0N1lSaBDZ4FuSu2XXKVJsTCRwrf+dwaNGX/tZb65MUTS X-Received: by 2002:a17:902:284b:: with SMTP id e69-v6mr8039009plb.240.1526274930632; Sun, 13 May 2018 22:15:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526274930; cv=none; d=google.com; s=arc-20160816; b=YWQ7Mbdev3AR317ckMyOLdOECt/TW0foiAUTSulfNyeeZG8bow2Yt19ArXjgq3iJVQ dTA/zhAQRjSp6wo1ahYJXAG+o/1oDNF73mokPItLR5aOXUnIXkB3wnNGuMp8klMEOAjw 3nZ3zUghQiaO4eDgOzGi37w3Z4fq1MAUxqRET0/cHTyqUywAxEEfC4L85leOl9wv6t3G ikkX8wIZN5F+G8yRZT1138wR1E2QUASKAwSRfkMnRvnsSL/AQVUX0SSdF2j8FLAsov/H LDkXQqKfDsPmamC864LAYNFY1AEn8gg6JL2ShsSPOpG4jaVWLxgcsGlPhgh6L2FBw25g YCXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=mowXnjcnDqWAna4GqgRfmf0ILwovYhBQnnbEuLqxL7k=; b=zDooiiFvl8i5qwB4O9Tsxfb+CEl2nKFbXHSWU047k4h6ukys6dmqOABM9JWscXTF4q A0I3g+HssHNU1o4NCe7G8MCEcLNL6CdMbou4kXbTbc8x7w2zv8SNxHjmPUSybBrWEU7C O5rFFaiie5eY9DXq7qAWq+rMRB4eZOQWAMNi/PKkRClfcZ6yFuyfh5hLzAMXGcgWVB1A SjvRrg7rE52l9pFYPUpQ0mzql+TgE6dI5K5fkLEDKgHu4vtO20XkjGaadijK5R3yuDmF PZEVPMJitVPMJD2+mANMzPVkhJYi9WVLFSiN9ZJn5VuGD8zcIbQpVFZmyo0RumGjFAeu 2Cpg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=JDIk5bMO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q6-v6si1763506pgf.547.2018.05.13.22.15.15; Sun, 13 May 2018 22:15:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=JDIk5bMO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751498AbeENFPE (ORCPT + 99 others); Mon, 14 May 2018 01:15:04 -0400 Received: from mail-pl0-f48.google.com ([209.85.160.48]:36161 "EHLO mail-pl0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750750AbeENFPC (ORCPT ); Mon, 14 May 2018 01:15:02 -0400 Received: by mail-pl0-f48.google.com with SMTP id v24-v6so6657285plo.3 for ; Sun, 13 May 2018 22:15:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=mowXnjcnDqWAna4GqgRfmf0ILwovYhBQnnbEuLqxL7k=; b=JDIk5bMOJV6sd9i69SSexNhlZj2MCIN+wEjy55+VzPBQTlkUJNic2HPgK59fxCK+H0 m0nPqHbB0iw5iAW9cmqUsbTq0plc7B7T7xKLDP5QWeG4lir9W5pHuSKpBm0YoyDCcj5w Y5CdqtHcepoYe9TNUHhxiL4Zx5opdf4ye3GzreEUVbAy/ice/YsbuiFs1x1CooVtkV90 sH7tfNuUKkbaJB/5l2NSiJdw15ytN9cahCVVHWlVszSJC15cU8zrHwOuTzonQP2U8ZtC v3wtE2Il72hvDl/FuRutcvmSQmX0pWu7LzM0T+lEGha/ALHzkn1HaTIQ3OgvJpc+SXZo Or8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=mowXnjcnDqWAna4GqgRfmf0ILwovYhBQnnbEuLqxL7k=; b=Q7Mb8xQGwW02mHpzxcByIna6q/1TAS/F/COhSh3tivp76yqFybGS7DtehPQSeg07kB XNFqF5dbi/1bsEcOPHoL2r/UedsdLdfsrFdAzIZ0Me1GP0HXDNodjdZzZfzbZVJsLFNp FtA5MTqdEhFJYLRWoj4FIaB6f/hZ3tVh3vhNqulBWL5MhaWetoyMIkBGNJeWAY+z3Krx kJMmrtWtMPstMlLlFrgRInRttsbyJdmBvtQ2+ZhXSWiEEPkr27xZ1TyhooRHMCuFzC37 SgNadrWNj7chJrmDjV0NokMvnauN/8wtnuYDYWoCviMvNZQRbqYvH76/Jx/4QEtnpJeE 7avA== X-Gm-Message-State: ALKqPwemAXLFVZsCw0CdN0QfOE0RAqHpQcR2+buADa48ETgWaV6WqwWu YTEnzn2+DJfmPqITcN8dxK5lkmmCqNqauU9mDUe3Wg== X-Received: by 2002:a17:902:264:: with SMTP id 91-v6mr8217997plc.341.1526274901577; Sun, 13 May 2018 22:15:01 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a17:90a:d01:0:0:0:0 with HTTP; Sun, 13 May 2018 22:14:41 -0700 (PDT) In-Reply-To: <20180514030205.GI677@sol.localdomain> References: <20180514030007.GH677@sol.localdomain> <20180514030205.GI677@sol.localdomain> From: Dmitry Vyukov Date: Mon, 14 May 2018 07:14:41 +0200 Message-ID: Subject: Re: CONFIG_KCOV causing crash in svm_vcpu_run() To: Eric Biggers Cc: KVM list , karahmed@amazon.de, "the arch/x86 maintainers" , LKML Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 14, 2018 at 5:02 AM, Eric Biggers wrote: > Sorry, messed up address for KVM mailing list. See message below. > > On Sun, May 13, 2018 at 08:00:07PM -0700, Eric Biggers wrote: >> With CONFIG_KCOV=y and an AMD processor, running the following program crashes >> the kernel with no output (I'm testing in a VM, so it's using nested >> virtualization): >> >> #include >> #include >> #include >> >> int main() >> { >> int dev, vm, cpu; >> char page[4096] __attribute__((aligned(4096))) = { 0 }; >> struct kvm_userspace_memory_region memreg = { >> .memory_size = 4096, >> .userspace_addr = (unsigned long)page, >> }; >> dev = open("/dev/kvm", O_RDONLY); >> vm = ioctl(dev, KVM_CREATE_VM, 0); >> cpu = ioctl(vm, KVM_CREATE_VCPU, 0); >> ioctl(vm, KVM_SET_USER_MEMORY_REGION, &memreg); >> ioctl(cpu, KVM_RUN, 0); >> } >> >> It bisects down to commit b2ac58f90540e39 ("KVM/SVM: Allow direct access to >> MSR_IA32_SPEC_CTRL"). The bug is apparently that due to the new code for >> managing the SPEC_CTRL MSR, __sanitizer_cov_trace_pc() is being called from >> svm_vcpu_run() before the host's MSR_GS_BASE has been restored, which causes a >> crash somehow. The following patch fixes it, though I don't know that it's the >> right solution; maybe KCOV should be disabled in the function instead, or maybe >> there's a more fundamental problem. What do people think? If __sanitizer_cov_trace_pc() crashes, I would expect there must be few more of them here: if (unlikely(!msr_write_intercepted(vcpu, MSR_IA32_SPEC_CTRL))) svm->spec_ctrl = native_read_msr(MSR_IA32_SPEC_CTRL); if (svm->spec_ctrl) native_wrmsrl(MSR_IA32_SPEC_CTRL, 0); Compiler inserts these callbacks into every basic block/edge.. Aren't there? Unfortunately we don't have an attribute that disables instrumentation of a single function. This is currently possible only on file level. >> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c >> index 1fc05e428aba8..d35ef241e66d8 100644 >> --- a/arch/x86/kvm/svm.c >> +++ b/arch/x86/kvm/svm.c >> @@ -5652,6 +5652,15 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) >> #endif >> ); >> >> +#ifdef CONFIG_X86_64 >> + wrmsrl(MSR_GS_BASE, svm->host.gs_base); >> +#else >> + loadsegment(fs, svm->host.fs); >> +#ifndef CONFIG_X86_32_LAZY_GS >> + loadsegment(gs, svm->host.gs); >> +#endif >> +#endif >> + >> /* >> * We do not use IBRS in the kernel. If this vCPU has used the >> * SPEC_CTRL MSR it may have left it on; save the value and >> @@ -5676,15 +5685,6 @@ static void svm_vcpu_run(struct kvm_vcpu *vcpu) >> /* Eliminate branch target predictions from guest mode */ >> vmexit_fill_RSB(); >> >> -#ifdef CONFIG_X86_64 >> - wrmsrl(MSR_GS_BASE, svm->host.gs_base); >> -#else >> - loadsegment(fs, svm->host.fs); >> -#ifndef CONFIG_X86_32_LAZY_GS >> - loadsegment(gs, svm->host.gs); >> -#endif >> -#endif >> - >> reload_tss(vcpu); >> >> local_irq_disable();