Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp1670948ioo; Sun, 22 May 2022 23:49:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwqlsHIqFDB88bXcQDNe2giBO7lMAcQb2Tw7bN23T+R+qWgF5NnLkHkI4Xry1JZCJSR9BaY X-Received: by 2002:a63:5d10:0:b0:3c5:e836:ffd2 with SMTP id r16-20020a635d10000000b003c5e836ffd2mr19233315pgb.32.1653288552156; Sun, 22 May 2022 23:49:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653288552; cv=none; d=google.com; s=arc-20160816; b=QzEhWnvGFJWQGdIuMBg0ffwdP43gEGDBO+WtJBe4gaPIkI5g1xAQehz0NepZ5kBwQQ eME0jhDYsVHDybMKWtl/CQphH6wWOpsDLY4j3Cv2WtBPc2WxQmYh6MdiKLWTlB6VlsIP nQKIkxpjMmTB7rBl4eOBDZ5eFgWYgBVwqODTleIJeA4SVTB6B21wOILktisBsdFrnDeR RVsL3qUXJcjOVE4RJ2VnZheTfa6BOtJO8k9BQbu7MdXfVAUmKObIHbXBLAtURRNzKW2M wdbsQGNySOsPM5/axiV51vglN3V74izOYOcNFa6+vUDPy8kjnc03Wk5QSvipje2V3l6k vAHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=A6cUTxWsXUnuwvyQ1HidVIsNdaDHVwFtR1Iko0emZoU=; b=r+vDVZfn5FFi/QB8ebY2+ezMgr79KXlOe1RfVA3K1L48trjQciQJh9GYEDOcZWP+vt lADdmHXmuR8yIWHvgZcSxzJhrtRLuWuvU5eVwV6L3gtfzd44nD1RQKZyXcxtqNbpGm3O dHZMhHC4NML3SFZtmB9pcbbazMjD4WUQpx17EQVXBY7bs60poZYyALGAPYNbrnJA02hl yYAo7fQJKZkRfLCdTCfJlWQPnXzC/xXz0ApFCREi0chbP3/BVEkI7DjTTuoqfSowCKo0 hmFWbPwIzJTwXsZWKheyP/LAAhc9cYeFFSmzI+b17FNIOKsWZx/lZidt/nx9euE5+XcZ 9mjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ecgcnywt; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z13-20020aa785cd000000b0050df0383302si11721504pfn.255.2022.05.22.23.49.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 22 May 2022 23:49:12 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=Ecgcnywt; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E34CA3EF27; Sun, 22 May 2022 23:18:17 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229765AbiETJyl (ORCPT + 99 others); Fri, 20 May 2022 05:54:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347989AbiETJye (ORCPT ); Fri, 20 May 2022 05:54:34 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 65A1214CA1A; Fri, 20 May 2022 02:54:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1653040472; x=1684576472; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=e57KscECE+4podIf9mIVnQB3nlnxc3JwwKJkTZj1mHc=; b=EcgcnywtfIWQu7/ht1OIxQgWwIbIe2hSNx+yuzexvNaUuRsqOm/Ox3Hm YOCk7omaXQD5b3Yxqf9pXIVaoGZWS7393Cnk/Qs1C//vjtGozLt64/Jvj K6A8+DL6Kg2EnWz/pjMSd6cmd6az/SyGiMPAJyloCwAHlNGD6X5FMdNIR Uf4VfKa3fzI6aO17bQlJla8tNeM/XVm44mn9HB/7t5ZmGEFVMphXmHhMq tYUsQOokhM2gtSVzbybHsyr0krHpz49vZaWSuOCCW+/bDhVvF3HxsoWNC TnlzvXrmftPRB0Oyo7LOuql64yoo+GOewFGQ2Osl58nJoDs78PuRKt6IH g==; X-IronPort-AV: E=McAfee;i="6400,9594,10352"; a="271376521" X-IronPort-AV: E=Sophos;i="5.91,238,1647327600"; d="scan'208";a="271376521" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 20 May 2022 02:54:32 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.91,238,1647327600"; d="scan'208";a="557378558" Received: from yy-desk-7060.sh.intel.com (HELO localhost) ([10.239.159.76]) by orsmga002.jf.intel.com with ESMTP; 20 May 2022 02:54:29 -0700 Date: Fri, 20 May 2022 17:54:28 +0800 From: Yuan Yao To: Yun Lu Cc: pbonzini@redhat.com, seanjc@google.com, vkuznets@redhat.com, wanpengli@tencent.com, jmattson@google.com, joro@8bytes.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] KVM: x86/mmu: optimizing the code in mmu_try_to_unsync_pages Message-ID: <20220520095428.bahy37jxkznqtwx5@yy-desk-7060> References: <20220520060907.863136-1-luyun_611@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220520060907.863136-1-luyun_611@163.com> User-Agent: NeoMutt/20171215 X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_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 Fri, May 20, 2022 at 02:09:07PM +0800, Yun Lu wrote: > There is no need to check can_unsync and prefetch in the loop > every time, just move this check before the loop. > > Signed-off-by: Yun Lu > --- > arch/x86/kvm/mmu/mmu.c | 12 ++++++------ > 1 file changed, 6 insertions(+), 6 deletions(-) > > diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c > index 311e4e1d7870..e51e7735adca 100644 > --- a/arch/x86/kvm/mmu/mmu.c > +++ b/arch/x86/kvm/mmu/mmu.c > @@ -2534,6 +2534,12 @@ int mmu_try_to_unsync_pages(struct kvm *kvm, const struct kvm_memory_slot *slot, > if (kvm_slot_page_track_is_active(kvm, slot, gfn, KVM_PAGE_TRACK_WRITE)) > return -EPERM; > > + if (!can_unsync) > + return -EPERM; > + > + if (prefetch) > + return -EEXIST; > + > /* > * The page is not write-tracked, mark existing shadow pages unsync > * unless KVM is synchronizing an unsync SP (can_unsync = false). In > @@ -2541,15 +2547,9 @@ int mmu_try_to_unsync_pages(struct kvm *kvm, const struct kvm_memory_slot *slot, > * allowing shadow pages to become unsync (writable by the guest). > */ > for_each_gfn_indirect_valid_sp(kvm, sp, gfn) { > - if (!can_unsync) > - return -EPERM; > - > if (sp->unsync) > continue; > > - if (prefetch) > - return -EEXIST; > - Consider the case that for_each_gfn_indirect_valid_sp() loop is not triggered, means the gfn is not MMU page table page: The old behavior when : return 0; The new behavior with this change: returrn -EPERM / -EEXIST; It at least breaks FNAME(sync_page) -> make_spte(prefetch = true, can_unsync = false) which removes PT_WRITABLE_MASK from last level mapping unexpectedly. > /* > * TDP MMU page faults require an additional spinlock as they > * run with mmu_lock held for read, not write, and the unsync > -- > 2.25.1 > > > No virus found > Checked by Hillstone Network AntiVirus >