Received: by 2002:a05:6602:18e:0:0:0:0 with SMTP id m14csp16223ioo; Fri, 20 May 2022 12:44:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzbV6IeoHnHfZ+UIZSvrgoVFbM4s7p9e/R7uaMuqioA7omtyDsD22Ypy4yKNzC1xNeROjFR X-Received: by 2002:a63:1702:0:b0:3f2:82e2:4a96 with SMTP id x2-20020a631702000000b003f282e24a96mr9675429pgl.459.1653075873160; Fri, 20 May 2022 12:44:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1653075873; cv=none; d=google.com; s=arc-20160816; b=wjl64neYV2kh6dy32c9DpIV+mbQPvtBpBxlLc1Ow6P24X3aBjLQ0HDpAOP3kFc/iZX hpo7wXoaZubpEJrK3Se4GqvWjvpxyxmaMRW3oeSDe6HipXexvLnS4eYkxoV42NbDvKwp 4Vz6ibEkcUl0JWTvnBsV1uOfud+B9VkOTmBFsSms+s0b8zUiNMSkL+b0Hsp8KDKUk6GF C0HMv12aSo85ty3nsrY5lLreUxZuSw2ZWu0GCfY7f6zc0os/9Jqlthiz7GJ3LWRcE897 Q+Ya6vHCTgh1FUkryrtvmjNEcMWWmf4+okE0vsH9tuHs01bktR2syAXw5eG1HQENiNlU 6u7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=EDLaBjALBZshwxMN5oSHQMdP+5yMRSPM50LnskauBiI=; b=hEgv0x0SLuCGHu8sEiDsDJXbUV4OcaB7TaiE0XCj7vOjpXZmBEz4yRXSxhjkogcGF4 K1e9b98THJpKlF5UNpw0QAhEl0Tzl6AJy39Gp9MMsLCUtvIX/KY+ZTS3jIy91qtGRQFv p0vyIGJehN7nNB1hJMMJznKlaQHCWe6rHa+h8Yft+KET/CHDRwI+UU6vLMZXRfhld7x9 pSMD15Nt5+YKNT+ZukRbNIQeVDeAm6qqmIEsymaeVC6KXH1kRMw+P/TatvySM3wyNj6G O/C8hi+paszMVvwCZX/Ax1WQvzoMgM/cvSlFhzzuDH2n/OXAPZPhIDKyTIxGKU3HNgJs 6Efw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@163.com header.s=s110527 header.b=ZLThMmtf; 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=163.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x4-20020a634a04000000b003db17cfc40csi382180pga.659.2022.05.20.12.44.11; Fri, 20 May 2022 12:44: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=@163.com header.s=s110527 header.b=ZLThMmtf; 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=163.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345808AbiETGZc (ORCPT + 99 others); Fri, 20 May 2022 02:25:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55634 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232003AbiETGZ1 (ORCPT ); Fri, 20 May 2022 02:25:27 -0400 X-Greylist: delayed 907 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Thu, 19 May 2022 23:25:25 PDT Received: from mail-m973.mail.163.com (mail-m973.mail.163.com [123.126.97.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 04ABA14C765; Thu, 19 May 2022 23:25:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-Id:MIME-Version; bh=EDLaB jALBZshwxMN5oSHQMdP+5yMRSPM50LnskauBiI=; b=ZLThMmtfDaLxrmpAqjeXi QH3+pNFbEKqCDU4f3pgmMJeYX5ijiaoqUaYPpxQ/dFMltEmFc6XgtnJZ2Q821gWS ZHiFdmA6OSJpOmEgkvq/s4PxFrwxj+yFLqY11xzzxeKj0gsuoTOrygVLVPsgSglq 8evm833vcpAuKq2UhL5Ggs= Received: from localhost.localdomain (unknown [116.128.244.169]) by smtp3 (Coremail) with SMTP id G9xpCgA3TJyqMIdizyGuDg--.28397S2; Fri, 20 May 2022 14:09:47 +0800 (CST) From: Yun Lu To: pbonzini@redhat.com Cc: 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: [PATCH] KVM: x86/mmu: optimizing the code in mmu_try_to_unsync_pages Date: Fri, 20 May 2022 14:09:07 +0800 Message-Id: <20220520060907.863136-1-luyun_611@163.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: G9xpCgA3TJyqMIdizyGuDg--.28397S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7Aw1DXFWxAF18trWfKr43GFg_yoW8GrW7pr ZrGrsIyr45GrsIq3s7Cw4kC347uws7KF48GryUWas8Zwn7K3s3ta4rKw4ftrs3XrWrGr1a va1ruF43WF18Jw7anT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UJ8nrUUUUU= X-Originating-IP: [116.128.244.169] X-CM-SenderInfo: pox130jbwriqqrwthudrp/1tbiMgwHzlWBzeFtBQAAsn X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,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 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; - /* * 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