Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp820481rwd; Thu, 15 Jun 2023 02:28:59 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4N4P0B/UBtLJX1JS8Vmd/3b0fGDAvj7B/TDK9NYlmjn3tsIW8jL4YGxC9B3myV7X0K31Db X-Received: by 2002:a17:902:dac5:b0:1b5:91:4693 with SMTP id q5-20020a170902dac500b001b500914693mr4460109plx.1.1686821339275; Thu, 15 Jun 2023 02:28:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686821339; cv=none; d=google.com; s=arc-20160816; b=FTbZ1gH8CXrTXD1LNyxbHkwJf7J+sAuuydqPsXUNo3zuS7jzDDRA3I7ijCbM2vNn0d 6W1s3L0oahss0XI4NNhWRrZMCcJ8yrS+J8vfU/xEUbzq2sTJiHxhCyATZQxeGNwVbl/n UG5nIPL8wUN5m8oWZNK2SP7a65xnFuvNvjInyfisWlmjuy8ALSrBKlz6E8pm80wbZbgS khT11v3dkXuy69VigsZVfQZlJH67Og7LHN4CbMuMzUu9Eqnb2m17i3Y35ncgG50h9ju9 ykakNLoZX0KGirKIJk8MO5tQ+sjydAPrpwPEbhZ/ix259feARnG5rBxz8h8CvsmsjbiS YobA== 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 :organization:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=WjUU9ZIccsBnD/tga66pDtvz51Usi0faNl1rzgCxArw=; b=eLC5ot2XtX6vTQh8zR3xKHznqcxSQWMQ3hcj5R/dUi+u/Ru+HkkFz3KYIQrly3QJ4v V6pDYh6jw7O4Iwb7iaxn6L+R9HNNX6WVQ3HPcHj2/41jNeytHyTNTiYmGV77pv10Une6 xGRcVuQCrT/UR28oPv4x402g90aLdPqWyFWNKjLWJRMc6I+ov5DP6cWVhRpqTg3jbuc6 ZehW31YbxmVFNweqJPggDGyC96PLG8w26SWhmweXBmn3FCGu/OOdcSCQlc+mAAbN6G5Y NvFslTZYwJm/u+7a3Cs8rKIHaEOqiLIcCirOTnBBtHG23KlxQvsQHejwDI8OXO2RtCuc GTfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=bixmUKEi; 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 r12-20020a170902c60c00b001b51c68c828si541350plr.211.2023.06.15.02.28.46; Thu, 15 Jun 2023 02:28:59 -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=bixmUKEi; 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 S239514AbjFOIyW (ORCPT + 99 others); Thu, 15 Jun 2023 04:54:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240542AbjFOIyA (ORCPT ); Thu, 15 Jun 2023 04:54:00 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 99E852D4C for ; Thu, 15 Jun 2023 01:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1686819187; 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=WjUU9ZIccsBnD/tga66pDtvz51Usi0faNl1rzgCxArw=; b=bixmUKEi2zPCmCRjhY+PE8LKPXNFMpa5Ys56J30Nmo5Jsk82eO3ihzFmTEW8Oi3zYLJXpt dlXSlGHEOIF5BxKBIQBFgfNr3ICZ1IEC/bV8iuuCFAnFaFtUP7JWcwuq3yduaoYRLjGKRY oFCp9a0icp3z6cKbpihVZRSJ8/u/rmI= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-639-O4B7c6o5NhOzu4fEka2Xwg-1; Thu, 15 Jun 2023 04:53:03 -0400 X-MC-Unique: O4B7c6o5NhOzu4fEka2Xwg-1 Received: by mail-wr1-f70.google.com with SMTP id ffacd0b85a97d-311145e2bacso264823f8f.1 for ; Thu, 15 Jun 2023 01:53:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686819182; x=1689411182; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=WjUU9ZIccsBnD/tga66pDtvz51Usi0faNl1rzgCxArw=; b=mDlGQD2bigpQ4zIdltp1O2kJsD3pkEO4iMusd2+03OFigmCI7H+hi6lSql1GO5PtIU nHZ1pxVNE7lWGiEQNgSRg5CZ+LS7gzTQa8YTAfPaAdwptt0eEKGXMiA9I5Tkn6ihj3ZD 7VlkiFWML3KFj0do5ITLaLIXap3dLl3L88TpwAxvjqhrh37odZGDiQU+xQLyKH4Brgep rs4wJCG4BzuZKCE1RnIac6kOXxQhbAzZ7GCVAqtsQX8DlVKIvixktJo4BBn3+Voa7bDh npgtpc0DmmttPCq0yNDou69JFcY0A1VkWY8oMis4dYJmeNxda6zeRkQg2+tjoA4OPJKh Y3kA== X-Gm-Message-State: AC+VfDy3eCeCiMOHl550uqUhx0iGg0oFrma5GTa0M2YScGPrhLtBnrUv DFTACIle9PE9/2O1B2z3NBIExMGUPWBJ9LO7RbeNTAFfM5qM91+fEtvIZ3ahyVtF4a5Nj+/aVE9 3oMvVDGGJGxR3ukRO1o9AtVBk X-Received: by 2002:a5d:5107:0:b0:30a:782a:7f97 with SMTP id s7-20020a5d5107000000b0030a782a7f97mr9969797wrt.12.1686819182435; Thu, 15 Jun 2023 01:53:02 -0700 (PDT) X-Received: by 2002:a5d:5107:0:b0:30a:782a:7f97 with SMTP id s7-20020a5d5107000000b0030a782a7f97mr9969783wrt.12.1686819182011; Thu, 15 Jun 2023 01:53:02 -0700 (PDT) Received: from ?IPV6:2a09:80c0:192:0:5dac:bf3d:c41:c3e7? ([2a09:80c0:192:0:5dac:bf3d:c41:c3e7]) by smtp.gmail.com with ESMTPSA id h4-20020adffa84000000b0030647d1f34bsm20347917wrr.1.2023.06.15.01.53.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Jun 2023 01:53:01 -0700 (PDT) Message-ID: <855fa10d-1211-d3d9-40a3-01a1ceaa33db@redhat.com> Date: Thu, 15 Jun 2023 10:53:00 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.12.0 Subject: Re: [PATCH v3] KVM: Avoid illegal stage2 mapping on invalid memory slot Content-Language: en-US To: Gavin Shan , kvmarm@lists.linux.dev Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, pbonzini@redhat.com, seanjc@google.com, mtosatti@redhat.com, maz@kernel.org, will@kernel.org, c.dall@virtualopensystems.com, peterx@redhat.com, aarcange@redhat.com, shahuang@redhat.com, hshuai@redhat.com, zhenyzha@redhat.com, shan.gavin@gmail.com References: <20230615054259.14911-1-gshan@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20230615054259.14911-1-gshan@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.2 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,T_SCC_BODY_TEXT_LINE 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 15.06.23 07:42, 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 > kvm_set_spte_gfn (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. > > We tried a git-bisect and the first problematic commit is cd4c71835228 (" > KVM: arm64: Convert to the gfn-based MMU notifier callbacks"). With this, > clean_dcache_guest_page() is called after the memory slots are iterated > in kvm_mmu_notifier_change_pte(). clean_dcache_guest_page() is called > before the iteration on the memory slots before this commit. This change > literally enlarges the racy window between kvm_mmu_notifier_change_pte() > and memory slot removal so that we're able to reproduce the issue in a > practical test case. However, the issue exists since commit d5d8184d35c9 > ("KVM: ARM: Memory virtualization setup"). > > Cc: stable@vger.kernel.org # v3.9+ > Fixes: d5d8184d35c9 ("KVM: ARM: Memory virtualization setup") > Reported-by: Shuai Hu > Reported-by: Zhenyu Zhang > Signed-off-by: Gavin Shan > --- > v3: Skip the invalid memory slots in change_pte() MMU notifier only, > suggested by Sean. Improved changelog to describe how the fixes > tag is given. > --- > virt/kvm/kvm_main.c | 20 +++++++++++++++++++- > 1 file changed, 19 insertions(+), 1 deletion(-) > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 479802a892d4..65f94f592ff8 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -686,6 +686,24 @@ static __always_inline int kvm_handle_hva_range_no_flush(struct mmu_notifier *mn > > return __kvm_handle_hva_range(kvm, &range); > } > + > +static bool kvm_change_spte_gfn(struct kvm *kvm, struct kvm_gfn_range *range) > +{ > + /* > + * Skipping invalid memslots is correct if and only change_pte() is > + * surrounded by invalidate_range_{start,end}(), which is currently > + * guaranteed by the primary MMU. If that ever changes, KVM needs to > + * unmap the memslot instead of skipping the memslot to ensure that KVM > + * doesn't hold references to the old PFN. > + */ > + WARN_ON_ONCE(!READ_ONCE(kvm->mn_active_invalidate_count)); > + > + if (range->slot->flags & KVM_MEMSLOT_INVALID) > + return false; > + > + return kvm_set_spte_gfn(kvm, range); > +} > + > static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, > struct mm_struct *mm, > unsigned long address, > @@ -707,7 +725,7 @@ static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn, > if (!READ_ONCE(kvm->mmu_invalidate_in_progress)) > return; > > - kvm_handle_hva_range(mn, address, address + 1, pte, kvm_set_spte_gfn); > + kvm_handle_hva_range(mn, address, address + 1, pte, kvm_change_spte_gfn); > } > > void kvm_mmu_invalidate_begin(struct kvm *kvm, unsigned long start, Makes perfect sense to me as well Reviewed-by: David Hildenbrand -- Cheers, David / dhildenb