From patchwork Wed Jul 24 01:10:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13740533 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A4830C3DA63 for ; Wed, 24 Jul 2024 01:14:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=JW+jUUoqcXqadXhonZGbNsXZ7+Zma3rlwNzyePdiaMw=; b=0lZMfyN4CeccV3e94gheA4HmMs Kcna1bd4zrUJ/Op2Hl7/a22KSL0VKrD/vnwE9qe4lIGfYw7OMIrKIMy1tHH9ahjZf7GBdZ54oQewF EEg9NaBxjZ7IZYwYDCduZxe02dyI6QME0Qzn7MDIy8rcjtcfS55oFq7N2DpwRA7bMg233ZxUBp2zu zJQXUdm+ZLkM76MTBPceIcw/Qiehpf8rtbyHItiDsz/aMc0J5tBKLy+ARa6jzOPFF6Q2FJATX9Zg/ +4dz4AhImplAJrcjm6KO5fS7xgD7J0K0+AKmOXsMXlHdMlvZ+Sig6sqRNuiqvLqLVT9mWS35X8GZA YvOUJ+Bg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sWQab-0000000Dyjx-2Ot8; Wed, 24 Jul 2024 01:14:45 +0000 Received: from mail-yw1-x114a.google.com ([2607:f8b0:4864:20::114a]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sWQXJ-0000000DxBM-3pvy for linux-arm-kernel@lists.infradead.org; Wed, 24 Jul 2024 01:11:23 +0000 Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-650ab31aabdso167189447b3.3 for ; Tue, 23 Jul 2024 18:11:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1721783480; x=1722388280; darn=lists.infradead.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JW+jUUoqcXqadXhonZGbNsXZ7+Zma3rlwNzyePdiaMw=; b=kjkfoKMQgTHc/aPn0lvga3hdizH+tWx9NEtgaSbX5KSc/ixNhX/xCni+gBHaGSRtz4 kDeA2lUutul3KFPi7a79B6BjSPR6t4D0YFRxR5UsThND3bsUXA4WhS78Fma25rxJKzt+ NAdiH/Vr7tH2MnP+EBchvbiy9KGURG2x0vi0FJw3ol00v31b7sYYwViA9twB3htOjbU2 uhMnPotaOEqEGBx4sEQ6gr0tIm3BuZ63Rc7os4gKg4S51Erj1uwzLMvpQPX0OyrLSCFZ VAnJILcgGtnEbuMhrqWZnrs2J5/RDZ1WgTHQ349XtShYYLDdt7fzi/CemNnc1zNVn5+X huog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1721783480; x=1722388280; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JW+jUUoqcXqadXhonZGbNsXZ7+Zma3rlwNzyePdiaMw=; b=E0yJdOY8o3wXkXVmMAhf5eNKbi7NHWksqlC0SpDVq6yDDK89A2QavjwHIc1wio9xHl rekGz8C6zJjtvPPMxjNPTC2iBq7qCiuvz+rrVygdwZjM0PL/Wer/HAWJ9Ypl02PE8X98 St1cS3qQkolLAHr1HShFjmILRIC1+cSQRycjsfaFjE9bhtVDdEXegHXGUrIY3ND/0QYp vGpCHM9HB2yDwuHQAUCmk3AlfAonRHPANXv8raOd1DElSggElnvUjmNPrHoQ5heLcmd+ D4lj+dzZB+Tuc++6mZfMRhwI6QBZWD0m981685FVPbkj7Lks+2+YRNw+C4u2YVmN0Pip Vkrg== X-Forwarded-Encrypted: i=1; AJvYcCUC6EJD8BpgI6nIxxJzO8cF0vyZ3taCyZWVroLxGnHJX4yGhq3CUQPJs+CkFx01YxlQf4MSkrU7IVQMOpZ64ctDiyft9hoERhX+sJbCjH7JXpgthsE= X-Gm-Message-State: AOJu0YxdSxZEtke4HszNY38uY5zheps9rgsuxKx93JdR59+zZ4gmRbLL Gd1I+qQ4bovdcQqTSwmL7dc4ffuG3Hqy9GCPREo8U8mPuY2bOUjfxV1nmSxf1/lEO9xLOSgbM6f Cl3a8Tsd5RcE/9YmZqA== X-Google-Smtp-Source: AGHT+IHToUjK35hNR0etsZGQeAjUCPxr50Jqta5Si1cnS8NseXl88g/0Ew0GUmmAUp0HML6hFdfWagbQP5rsIFm/ X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a05:690c:102:b0:66b:fb2f:1c2 with SMTP id 00721157ae682-671f4e0a6damr821967b3.7.1721783480393; Tue, 23 Jul 2024 18:11:20 -0700 (PDT) Date: Wed, 24 Jul 2024 01:10:33 +0000 In-Reply-To: <20240724011037.3671523-1-jthoughton@google.com> Mime-Version: 1.0 References: <20240724011037.3671523-1-jthoughton@google.com> X-Mailer: git-send-email 2.46.0.rc1.232.g9752f9e123-goog Message-ID: <20240724011037.3671523-9-jthoughton@google.com> Subject: [PATCH v6 08/11] KVM: x86: Optimize kvm_{test_,}age_gfn a little bit From: James Houghton To: Andrew Morton , Paolo Bonzini Cc: Ankit Agrawal , Axel Rasmussen , Catalin Marinas , David Matlack , David Rientjes , James Houghton , James Morse , Jason Gunthorpe , Jonathan Corbet , Marc Zyngier , Oliver Upton , Raghavendra Rao Ananta , Ryan Roberts , Sean Christopherson , Shaoqin Huang , Suzuki K Poulose , Wei Xu , Will Deacon , Yu Zhao , Zenghui Yu , kvmarm@lists.linux.dev, kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240723_181121_988471_12D65DD0 X-CRM114-Status: GOOD ( 13.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Optimize both kvm_age_gfn and kvm_test_age_gfn's interaction with the shadow MMU by, rather than checking if our memslot has rmaps, check if there are any indirect_shadow_pages at all. Also, for kvm_test_age_gfn, reorder the TDP MMU check to be first. If we find that the range is young, we do not need to check the shadow MMU. Signed-off-by: James Houghton --- arch/x86/kvm/mmu/mmu.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 7b93ce8f0680..919d59385f89 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -1629,19 +1629,24 @@ static void rmap_add(struct kvm_vcpu *vcpu, const struct kvm_memory_slot *slot, __rmap_add(vcpu->kvm, cache, slot, spte, gfn, access); } +static bool kvm_has_shadow_mmu_sptes(struct kvm *kvm) +{ + return !tdp_mmu_enabled || READ_ONCE(kvm->arch.indirect_shadow_pages); +} + bool kvm_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) { bool young = false; - if (kvm_memslots_have_rmaps(kvm)) { + if (tdp_mmu_enabled) + young |= kvm_tdp_mmu_age_gfn_range(kvm, range); + + if (kvm_has_shadow_mmu_sptes(kvm)) { write_lock(&kvm->mmu_lock); young = kvm_handle_gfn_range(kvm, range, kvm_age_rmap); write_unlock(&kvm->mmu_lock); } - if (tdp_mmu_enabled) - young |= kvm_tdp_mmu_age_gfn_range(kvm, range); - return young; } @@ -1649,15 +1654,15 @@ bool kvm_test_age_gfn(struct kvm *kvm, struct kvm_gfn_range *range) { bool young = false; - if (kvm_memslots_have_rmaps(kvm)) { + if (tdp_mmu_enabled) + young |= kvm_tdp_mmu_test_age_gfn(kvm, range); + + if (!young && kvm_has_shadow_mmu_sptes(kvm)) { write_lock(&kvm->mmu_lock); young = kvm_handle_gfn_range(kvm, range, kvm_test_age_rmap); write_unlock(&kvm->mmu_lock); } - if (tdp_mmu_enabled) - young |= kvm_tdp_mmu_test_age_gfn(kvm, range); - return young; }