Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp523577rwb; Wed, 28 Sep 2022 06:02:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4JmzBUUzEUF/AXQ+hc5+J16w8ImFsR3kweS0C+5LZpBmYrR/924GtsjCVXF3t1Hd0OtTkc X-Received: by 2002:a05:6402:35cb:b0:451:6a0a:6688 with SMTP id z11-20020a05640235cb00b004516a0a6688mr33029052edc.415.1664370153283; Wed, 28 Sep 2022 06:02:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664370153; cv=none; d=google.com; s=arc-20160816; b=rt/9tLuhPOLoeZ0vc66FwL4YUkrxdjp90qiPhYk6dOHxyYWPQTovq3kN5QGbpTjPKE t0Wq4HgW0b2rDQIMh1zoI9RxHSmXamvd/FEujWQxsLbK27ONWshg6c/vaIQjIASeq5zz pYrPI/PePc7ONxB7Xtk/NkMf8G8TQaWcH/Kre2tMIi9UnduaSmb26Z/jcDi7jgvBD9gE CIvthTtw8LnsU+Q4HaNNk4s/8WXe8tTmMoW1gIzs+rwOOgqBjV3puyLGLeGBhkBliv0U /r5BNFDDzQJ6MwCixufrTQipI3hCPTiqsNx6qc8Ju5EYRgRbg9q23Fy0Bxafv5uovZx3 yQaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=Qw69+PrzJ+2+9QRJKVTJ19LmNv5Ecvv+R+yRw3efh/0=; b=jegAoicXDGSp4zCRbiipZqi3Ic6sXbsTY73Hvdw9fDNQ9nCnTQ4SMgQQCKI401LDzy jrXKVRdo4SONFD5QvZzC26wluB93T0Z8HiCsVU/169c5NM2uFCTJPOFk1QhEk2fvyYcS dvKpo8f+FXc85A4o9EpsUemr4M7xYGhV9hn75/yui9u0DbhDAbKtDl2yfiuOCDcV2xLI 1n+tLTzJwJQ9Kwqm82xCetW0IZzfuOhVHAn4ygSrC1vb7UQynaDxM0wDRSIC0n2H99Cy +3StLogXXcNV55pRfzXbNwyQogBh/B6rnMGh5ZKwBM6tkIfFAI6Sr/UwQoU4qlAsyyOT rqZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=C1gylgAl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id he39-20020a1709073da700b0078356aaeb63si4949830ejc.288.2022.09.28.06.02.06; Wed, 28 Sep 2022 06:02:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=C1gylgAl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233467AbiI1MxE (ORCPT + 99 others); Wed, 28 Sep 2022 08:53:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34726 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233998AbiI1MxB (ORCPT ); Wed, 28 Sep 2022 08:53:01 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AB697A520 for ; Wed, 28 Sep 2022 05:52:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1664369575; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qw69+PrzJ+2+9QRJKVTJ19LmNv5Ecvv+R+yRw3efh/0=; b=C1gylgAl/IM42+Z26Jvffj03XwIOuVJv/HRLZl7lsmAghcChAG3UNwkoB0JEotI0Ywp1Eq OVPx0Qy1U3Ia3jl0WIFUJ51mwywopvh86INC83wXGAdHXZt+RqRiXkaN/k03RYXXzF/FCw eVSvU7Lk1Ks8zWduWh3mXXEBaRe7nkY= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-314-rfQgzvUUPm6fznqmjvrBzg-1; Wed, 28 Sep 2022 08:52:54 -0400 X-MC-Unique: rfQgzvUUPm6fznqmjvrBzg-1 Received: by mail-wm1-f69.google.com with SMTP id b5-20020a05600c4e0500b003b499f99aceso1167257wmq.1 for ; Wed, 28 Sep 2022 05:52:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date; bh=Qw69+PrzJ+2+9QRJKVTJ19LmNv5Ecvv+R+yRw3efh/0=; b=AA3K0oKXqwgnv6wqWY8PfHaxRwQYDDaB+ej/ZInZMHNCP/xKfSArLScHoLiWWURDGK yVUCXLaZ/Sgup6XIdIOliNSwv6DF5XqOe7rVsoFktf6Iw+cQvkwElKRBQY2ft81HLNO0 bpYCEYRLHZuuFjhY1qqCAHvDR3eSzeTUK4KluXBPOkokdbgQwzUIxFcm1dyViJ6YD7oZ mdsO/lm/9cIvBsRwUrx/I9HCAg9vfod1p0F0Zc3vY6JubMFELTCZt7rAeBN4h0rmYLUR q/qOCdMxt6ETb9z6+/q0+7QM8APRd/4QTylH/p0K0m4WHJbUqol1EJ0wwWs+v2kfVUfL PyDw== X-Gm-Message-State: ACrzQf317K25gqmQAaGuCso9xHFh+f42kF5+zLHmjNCm3EciqxXD6kL/ scDUFGM5KpFPvGwrEjjJncfEKJNzFqTnDgdjYziAQ2TQsZlQWRfDrRFplrTRn7RhTk9aHaOsvUU OKawc/qucaTfkeNwSXRfCdIum X-Received: by 2002:a5d:49c8:0:b0:22a:35a1:1755 with SMTP id t8-20020a5d49c8000000b0022a35a11755mr19562617wrs.180.1664369573150; Wed, 28 Sep 2022 05:52:53 -0700 (PDT) X-Received: by 2002:a5d:49c8:0:b0:22a:35a1:1755 with SMTP id t8-20020a5d49c8000000b0022a35a11755mr19562584wrs.180.1664369572851; Wed, 28 Sep 2022 05:52:52 -0700 (PDT) Received: from ?IPV6:2003:cb:c704:1100:add9:5f61:6b94:7540? (p200300cbc7041100add95f616b947540.dip0.t-ipconnect.de. [2003:cb:c704:1100:add9:5f61:6b94:7540]) by smtp.gmail.com with ESMTPSA id co4-20020a0560000a0400b00228d6bc8450sm5090561wrb.108.2022.09.28.05.52.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Sep 2022 05:52:52 -0700 (PDT) Message-ID: Date: Wed, 28 Sep 2022 14:52:51 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.0 Content-Language: en-US To: Maxim Levitsky , Emanuele Giuseppe Esposito , Sean Christopherson Cc: Paolo Bonzini , kvm@vger.kernel.org, Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, Like Xu References: <5f0345d2-d4d1-f4fe-86ba-6e22561cb6bd@redhat.com> <37b3162e-7b3a-919f-80e2-f96eca7d4b4c@redhat.com> <55d7f0bd-ace1-506b-ea5b-105a86290114@redhat.com> <111a46c1-7082-62e3-4f3a-860a95cd560a@redhat.com> <14d5b8f2-7cb6-ce24-c7a7-32aa9117c953@redhat.com> <3b04db9d-0177-7e6e-a54c-a28ada8b1d36@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [RFC PATCH 0/9] kvm: implement atomic memslot updates In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 28.09.22 13:14, Maxim Levitsky wrote: > On Wed, 2022-09-28 at 11:11 +0200, Emanuele Giuseppe Esposito wrote: >> >> Am 27/09/2022 um 17:58 schrieb Sean Christopherson: >>> On Tue, Sep 27, 2022, Emanuele Giuseppe Esposito wrote: >>>> Am 26/09/2022 um 23:28 schrieb Sean Christopherson: >>>>> On Mon, Sep 26, 2022, David Hildenbrand wrote: >>>>>> As Sean said "This is an awful lot of a complexity to take on for something >>>>>> that appears to be solvable in userspace." >>>>> >>>>> And if the userspace solution is unpalatable for whatever reason, I'd like to >>>>> understand exactly what KVM behavior is problematic for userspace. E.g. the >>>>> above RHBZ bug should no longer be an issue as the buggy commit has since been >>>>> reverted. >>>> >>>> It still is because I can reproduce the bug, as also pointed out in >>>> multiple comments below. >>> >>> You can reproduce _a_ bug, but it's obviously not the original bug, because the >>> last comment says: >>> >>> Second, indeed the patch was reverted and somehow accepted without generating >>> too much noise: >>> >>> ... >>> >>> The underlying issue of course as we both know is still there. >>> >>> You might have luck reproducing it with this bug >>> >>> https://bugzilla.redhat.com/show_bug.cgi?id=1855298 >>> >>> But for me it looks like it is 'working' as well, so you might have >>> to write a unit test to trigger the issue. >>> >>>>> If the issue is KVM doing something nonsensical on a code fetch to MMIO, then I'd >>>>> much rather fix _that_ bug and improve KVM's user exit ABI to let userspace handle >>>>> the race _if_ userspace chooses not to pause vCPUs. >>>>> >>>> >>>> Also on the BZ they all seem (Paolo included) to agree that the issue is >>>> non-atomic memslots update. >>> >>> Yes, non-atomic memslot likely results in the guest fetching from a GPA without a >>> memslot. I'm asking for an explanation of exactly what happens when that occurs, >>> because it should be possible to adjust KVM and/or QEMU to play nice with the >>> fetch, e.g. to resume the guest until the new memslot is installed, in which case >>> an atomic update isn't needed. >>> >>> I assume the issue is that KVM exits with KVM_EXIT_INTERNAL_ERROR because the >>> guest is running at CPL=0, and QEMU kills the guest in response. If that's correct, >>> then that problem can be solved by exiting to userspace with KVM_EXIT_MMIO instead >>> of KVM_EXIT_INTERNAL_ERROR so that userspace can do something sane in response to >>> the MMIO code fetch. >>> >>> I'm pretty sure this patch will Just Work for QEMU, because QEMU simply resumes >>> the vCPU if mmio.len==0. It's a bit of a hack, but I don't think it violates KVM's >>> ABI in any way, and it can even become "official" behavior since KVM x86 doesn't >>> otherwise exit with mmio.len==0. >>> >>> Compile tested only... >> >> So basically you are just making KVM catch the failed >> kvm_vcpu_read_guest_page() by retuning mmio.len = 0 to QEMU which >> basically ends up in doing nothing and retry again executing the >> instruction? >> >> I wonder if there are some performance implications in this, but it's >> definitely simpler than what I did. >> >> Tested on the same failing machine used for the BZ, fixes the bug. >> >> Do you want me to re-send the patch on your behalf (and add probably a >> small documentation on Documentation/virt/kvm/api.rst)? >> >> Emanuele >>> --- >>> From: Sean Christopherson >>> Date: Tue, 27 Sep 2022 08:16:03 -0700 >>> Subject: [PATCH] KVM: x86: Exit to userspace with zero-length MMIO "read" on >>> MMIO fetch >>> >>> Exit to userspace with KVM_EXIT_MMIO if emulation fails due to not being >>> able to fetch instruction bytes, e.g. if the resolved GPA isn't backed by >>> a memslot. If userspace is manipulating memslots without pausing vCPUs, >>> e.g. to emulate BIOS relocation, then a vCPU may fetch while there is no >>> valid memslot installed. Depending on guest context, KVM will either >>> exit to userspace with KVM_EXIT_INTERNAL_ERROR (L1, CPL=0) or simply >>> resume the guest (L2 or CPL>0), neither of which is desirable as exiting >>> with "emulation error" effectively kills the VM, and resuming the guest >>> doesn't provide userspace an opportunity to react the to fetch. >>> >>> Use "mmio.len == 0" to indicate "fetch". This is a bit of a hack, but >>> there is no other way to communicate "fetch" to userspace without >>> defining an entirely new exit reason, e.g. "mmio.is_write" is a boolean >>> and not a flag, and there is no known use case for actually supporting >>> code fetches from MMIO, i.e. there's no need to allow userspace to fill >>> in the instruction bytes. >>> >>> Signed-off-by: Sean Christopherson >>> --- >>> arch/x86/kvm/emulate.c | 2 ++ >>> arch/x86/kvm/kvm_emulate.h | 1 + >>> arch/x86/kvm/x86.c | 9 ++++++++- >>> 3 files changed, 11 insertions(+), 1 deletion(-) >>> >>> diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c >>> index f092c54d1a2f..e141238d93b0 100644 >>> --- a/arch/x86/kvm/emulate.c >>> +++ b/arch/x86/kvm/emulate.c >>> @@ -5353,6 +5353,8 @@ int x86_decode_insn(struct x86_emulate_ctxt *ctxt, void *insn, int insn_len, int >>> done: >>> if (rc == X86EMUL_PROPAGATE_FAULT) >>> ctxt->have_exception = true; >>> + if (rc == X86EMUL_IO_NEEDED) >>> + return EMULATION_IO_FETCH; >>> return (rc != X86EMUL_CONTINUE) ? EMULATION_FAILED : EMULATION_OK; >>> } >>> >>> diff --git a/arch/x86/kvm/kvm_emulate.h b/arch/x86/kvm/kvm_emulate.h >>> index 89246446d6aa..3cb2e321fcd2 100644 >>> --- a/arch/x86/kvm/kvm_emulate.h >>> +++ b/arch/x86/kvm/kvm_emulate.h >>> @@ -516,6 +516,7 @@ bool x86_page_table_writing_insn(struct x86_emulate_ctxt *ctxt); >>> #define EMULATION_OK 0 >>> #define EMULATION_RESTART 1 >>> #define EMULATION_INTERCEPTED 2 >>> +#define EMULATION_IO_FETCH 3 >>> void init_decode_cache(struct x86_emulate_ctxt *ctxt); >>> int x86_emulate_insn(struct x86_emulate_ctxt *ctxt); >>> int emulator_task_switch(struct x86_emulate_ctxt *ctxt, >>> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c >>> index aa5ab0c620de..7eb72694c601 100644 >>> --- a/arch/x86/kvm/x86.c >>> +++ b/arch/x86/kvm/x86.c >>> @@ -7129,8 +7129,13 @@ static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, >>> bytes = (unsigned)PAGE_SIZE - offset; >>> ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val, >>> offset, bytes); >>> - if (unlikely(ret < 0)) >>> + if (unlikely(ret < 0)) { >>> + vcpu->run->mmio.phys_addr = gpa; >>> + vcpu->run->mmio.len = 0; >>> + vcpu->run->mmio.is_write = 0; >>> + vcpu->run->exit_reason = KVM_EXIT_MMIO; >>> return X86EMUL_IO_NEEDED; >>> + } >>> >>> return X86EMUL_CONTINUE; >>> } >>> @@ -8665,6 +8670,8 @@ int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, >>> r = x86_decode_emulated_instruction(vcpu, emulation_type, >>> insn, insn_len); >>> if (r != EMULATION_OK) { >>> + if (r == EMULATION_IO_FETCH) >>> + return 0; >>> if ((emulation_type & EMULTYPE_TRAP_UD) || >>> (emulation_type & EMULTYPE_TRAP_UD_FORCED)) { >>> kvm_queue_exception(vcpu, UD_VECTOR); >>> >>> base-commit: 39d9b48cc777bdf6d67d01ed24f1f89b13f5fbb2 >>> > > Note that AFAIK, there is another case (and probably more), if TDP is disabled, > and MMU root is in mmio, we kill the guest. > > > mmu_alloc_shadow_roots -> mmu_check_root > > > I used to have few hacks in KVM to cope with this, but AFAIK, > I gave up on it, because the issue would show up again and again. IIRC, s390x can have real problems if we temporarily remove a memslot. In case the emulation/interpretation code tries accessing guest memory and fails because there is no memslot describing that portion of guest RAM. Note that resizing/merging/splitting currently shouldn't happen on s390x, though. But resizing of memslots might happen in the near future with virtio-mem in QEMU. -- Thanks, David / dhildenb