Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2053298rwd; Fri, 9 Jun 2023 06:08:49 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4zoPmT6JODxSt8f7s+VewJwfgmneSz41VuyheJaJisDYnpFy3i/HIrNaIAQgb+0t1Y34AX X-Received: by 2002:a17:90b:4a41:b0:256:544a:74c9 with SMTP id lb1-20020a17090b4a4100b00256544a74c9mr1305170pjb.25.1686316129501; Fri, 09 Jun 2023 06:08:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686316129; cv=none; d=google.com; s=arc-20160816; b=Qy+/XZhvb0ReWZz8e3W+QwAANziys8gLHyfz/ZBzEv6cPygrk8y7cXmn1JYl9XBewP NUXLwt/NIoQ7yHyRCViDqYKp/AzkQD5d9ClQeiEt4ypxIgyrqwZg6nFAnfyn2+ZzMZkK fBtP3eGf+rATGAqq6fYJqE12JOoySBLDwWbUD7AgjuWNnBJf6hQZMraqAuPGjhw2ULL1 g9UKvYwAYMLAoTiLpc+Nnjw0YUqwZcVGHPbDNlB6vv5KU5aHZ6Pmb+PnNc9Fq7aZQmJx WiP/LdvwN2uSp7CKEPZIKnSc7F54WVIy5e1RTv32DO1g6N76mB7/go/tjEXwr+wj29Sk d6Ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=4ZJQ83pjNzols4v2WBtNMBH3X+AshAfubv2ssi7YgH0=; b=RMyfwPYa7tYUQJV0jPPUoFmRc2plHmfQuKo0ANbnZJC+uU5RbSzER09ZznuM/psKS/ ZACg1c2N/eGX8+/BKRpRjFU3uhr4xJ8JxeDdS2fpsk5JAWy9AH6Nq6bfe2RFGs9nxOzH 8O16JxeNIEaFOrAM3TnOrG3i83HBURDzYWPuh19PKZOszrzlxcAqDywkDq5Dud/dtS3I zqTFPcMr4IjGvwyaj2ntUXN4axfgJSTj0BGBNmAYd8XtMvt3Tp+uLxjzrYe1G/Cbx7Rp gdsOJ3bM6bIHCw+7EONgTjeH0OIFJTLfOORgrVf9+SCdIPlybopR3WptIiyXQP0MyWiG EjLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=ekp7Ryaf; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j63-20020a638b42000000b0051b9ce24708si2729089pge.103.2023.06.09.06.08.34; Fri, 09 Jun 2023 06:08:49 -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=@kernel.org header.s=k20201202 header.b=ekp7Ryaf; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240985AbjFIMzN (ORCPT + 99 others); Fri, 9 Jun 2023 08:55:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239243AbjFIMzK (ORCPT ); Fri, 9 Jun 2023 08:55:10 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8AD3630CD; Fri, 9 Jun 2023 05:54:50 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A9D026168A; Fri, 9 Jun 2023 12:53:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06DD9C433D2; Fri, 9 Jun 2023 12:53:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686315202; bh=7jChBWuCMk9Nu99JqVR5Rs9FVFSJLpKrOg+HaaHj1bg=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=ekp7Ryaf3YCnAGMZqmI0TWgkg09/MVSmUZmkQUv2bPK5AYOgNbTtX9V5Z8iJuoGF0 DrAhYN8yUu7/2Mh+KDfGVrExN/AMEhr3FQLJ/NDk2hkEDckwD2ZGPFYlA5+x1N6xbB tMXAc2/fb7ce5TjoMGstvgceZCjv5L3GuYCnaGi3mOiVflW/zII6Zwf3yTth1utHqk zr+7BqWXC9LW/50BODJNXBFsOyt+XJnjqGzciyELzuMmq4xI+Emz2w4U7BQ6myBb6n tExY+kG1VdXPceoZwLTy6VaQEPg+aDH4ls3A4W7W0LHlL12Jetw3DU8qk5+plTty0d ftdrtd6MfehXQ== Received: from 152.5.30.93.rev.sfr.net ([93.30.5.152] helo=wait-a-minute.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1q7bcF-0045MN-KL; Fri, 09 Jun 2023 13:53:19 +0100 Date: Fri, 09 Jun 2023 13:53:17 +0100 Message-ID: <874jngokzm.wl-maz@kernel.org> From: Marc Zyngier To: Gavin Shan Cc: kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, seanjc@google.com, oliver.upton@linux.dev, aarcange@redhat.com, peterx@redhat.com, david@redhat.com, hshuai@redhat.com, zhenyzha@redhat.com, shan.gavin@gmail.com Subject: Re: [PATCH v2] KVM: Avoid illegal stage2 mapping on invalid memory slot In-Reply-To: <20230609100420.521351-1-gshan@redhat.com> References: <20230609100420.521351-1-gshan@redhat.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/28.2 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 93.30.5.152 X-SA-Exim-Rcpt-To: gshan@redhat.com, kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, seanjc@google.com, oliver.upton@linux.dev, aarcange@redhat.com, peterx@redhat.com, david@redhat.com, hshuai@redhat.com, zhenyzha@redhat.com, shan.gavin@gmail.com X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Fri, 09 Jun 2023 11:04:20 +0100, Gavin Shan wrote: > > We run into guest hang in edk2 firmware when KSM is kept as running on > the host. The edk2 firmware is waiting for status 0x80 from QEMU's pflash > device (TYPE_PFLASH_CFI01) during the operation of sector erasing or > buffered write. The status is returned by reading the memory region of > the pflash device and the read request should have been forwarded to QEMU > and emulated by it. Unfortunately, the read request is covered by an > illegal stage2 mapping when the guest hang issue occurs. The read request > is completed with QEMU bypassed and wrong status is fetched. The edk2 > firmware runs into an infinite loop with the wrong status. > > The illegal stage2 mapping is populated due to same page sharing by KSM > at (C) even the associated memory slot has been marked as invalid at (B) > when the memory slot is requested to be deleted. It's notable that the > active and inactive memory slots can't be swapped when we're in the middle > of kvm_mmu_notifier_change_pte() because kvm->mn_active_invalidate_count > is elevated, and kvm_swap_active_memslots() will busy loop until it reaches > to zero again. Besides, the swapping from the active to the inactive memory > slots is also avoided by holding &kvm->srcu in __kvm_handle_hva_range(), > corresponding to synchronize_srcu_expedited() in kvm_swap_active_memslots(). > > CPU-A CPU-B > ----- ----- > ioctl(kvm_fd, KVM_SET_USER_MEMORY_REGION) > kvm_vm_ioctl_set_memory_region > kvm_set_memory_region > __kvm_set_memory_region > kvm_set_memslot(kvm, old, NULL, KVM_MR_DELETE) > kvm_invalidate_memslot > kvm_copy_memslot > kvm_replace_memslot > kvm_swap_active_memslots (A) > kvm_arch_flush_shadow_memslot (B) > same page sharing by KSM > kvm_mmu_notifier_invalidate_range_start > : > kvm_mmu_notifier_change_pte > kvm_handle_hva_range > __kvm_handle_hva_range (C) > : > kvm_mmu_notifier_invalidate_range_end > > Fix the issue by skipping the invalid memory slot at (C) to avoid the > illegal stage2 mapping so that the read request for the pflash's status > is forwarded to QEMU and emulated by it. In this way, the correct pflash's > status can be returned from QEMU to break the infinite loop in the edk2 > firmware. > > Cc: stable@vger.kernel.org # v5.13+ > Fixes: 3039bcc74498 ("KVM: Move x86's MMU notifier memslot walkers to generic code") > Reported-by: Shuai Hu > Reported-by: Zhenyu Zhang > Signed-off-by: Gavin Shan > --- > v2: Improved changelog suggested by Marc > --- > virt/kvm/kvm_main.c | 3 +++ > 1 file changed, 3 insertions(+) Reviewed-by: Marc Zyngier M. -- Without deviation from the norm, progress is not possible.