Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp95083pxb; Wed, 18 Aug 2021 16:59:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzW0CDu8nAdq6tf+u3rO3KqyTWmg1W9fpaPVayGmZHIbj3eZtnBPYQoXc0FYNozHPXRDJVC X-Received: by 2002:a6b:6603:: with SMTP id a3mr9430701ioc.68.1629331179636; Wed, 18 Aug 2021 16:59:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629331179; cv=none; d=google.com; s=arc-20160816; b=c4qwiIocdBplgmAowKOa71xefTHvisFQmDkb2C3NZwzJoVmbK9hE1cTRxJZSYFQQ19 k0mIOwjq7I4323IuFYZkSL7cSw2SSAcDN+D+P+Z6hjwIIFhAFZH0ro3v6h1x/HLB7XNN QVeusAxe0z7kki6tOyHVQ6jwIb/PWq5dTOJC3fPyrwKdqdclN9XDDZrkhG2xcVP16w9m 5a1FbZvO7M71GleGinqnjuA0lVADHOJa467hKC2oK3VRi1qWAZf6Bs2q0LCScM8Qe/Zs XlTg5dnL/jzU8ZvPRcx8HtRky4L+sA02twG3HnVjlT9XFV00ELPZnsFotFMGfu+c0dqv Tk6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:mime-version:message-id:date :reply-to:dkim-signature; bh=Scvb2gfJzrYfNJq90EL74fT7SS7nVGstZhceM9SRTyY=; b=ipZNDBn8W/upC2APTyGfsSZ911AOts4MoVInLruWfzjJ4pveSWqzI3nd7xnwKVSFXx BvzBIQE/4njRH7akIrtxqLJ//dyM48Zc/uNIUxyo5BMlFBkZUxGMvQrIwr6gsNaltJ9o 8oagXuvSKRcQMbsnsnU03wNDUl+5mQ+0ns71A7EWkk0vvU+Hl2czysKxr43GD3aTQw5d mN+r1IJq5/Xoq2jRu5W69TCPeMIsdzxwbEqgpKzJ2K5RCszvgnEPpBz7ZJ6Q6jrWOxyk drFYA2R10QmIOpzhERl6NXaU+2hsciU41s74vBI2fBo3x7sQbnfT1KbAQF85dGJbIWRa X4tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=CPqsJcRd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h30si1279792ila.1.2021.08.18.16.59.22; Wed, 18 Aug 2021 16:59:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=CPqsJcRd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235069AbhHRX46 (ORCPT + 99 others); Wed, 18 Aug 2021 19:56:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40410 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234949AbhHRX44 (ORCPT ); Wed, 18 Aug 2021 19:56:56 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62FDFC061764 for ; Wed, 18 Aug 2021 16:56:21 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id z23-20020a17090abd97b0290176898bbb9cso2147488pjr.8 for ; Wed, 18 Aug 2021 16:56:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=reply-to:date:message-id:mime-version:subject:from:to:cc; bh=Scvb2gfJzrYfNJq90EL74fT7SS7nVGstZhceM9SRTyY=; b=CPqsJcRdPyzWVw2UkNu0Wn9hUr9Qxq5bXEX1GOFSOmGUzn4ZXDSKec71mwcN+FHo9/ J7Hl7xy+UKc/xgVNUN88urCR/U61zjMcY6asIwsJ1NN2YS9qGf+aKvKCDIrLpWvwMxY7 2BngKUPmIzEBdYGZPYHGwKJ9jNq1gVsHj9z0RLCoU1r/NmEOi3G9vIVtbVYCcORZAjlI UndfdDGE3KQaTr65pLvCdgbH6llmSIJz96OFnUlXCMFadPkBkt7tK63U5VkNx63TbLCo ERRq3MYzS7Mvlz1HG8GN5rM0km+KLmdMUSZjv6uehqHwlltzlCfbOOiB7zBMlniQMnlZ qqtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:reply-to:date:message-id:mime-version:subject :from:to:cc; bh=Scvb2gfJzrYfNJq90EL74fT7SS7nVGstZhceM9SRTyY=; b=PKc/jwTK10VI9xfGG6T8z8lTmBDyAqrxSaWm1XRj1JeSAhxxSEukRWz8iheHEZGn6I XE7qZGGytpZ/31DNWF/gStHnm3NTegxo08Gcu9I+SHQyI/FHPsDO13RvJpHiEx4DV1/Z zHBEUnXauZuaiSkO5PcghlcZkB+YVl1X32841VCzaEqjof2bLsVVAhm92RJ5jsVa8Xi4 ZsvYFzYEyAJbNhnp15QRkAKwTlfACjf9zTuxKRJ9QznyRrsrc4SOvEH9UfB0gNPyk1vM SBNm5fsmIOYutfzpLZ/duqumIsCP1kkf48aU1cZ8kQAUFACcx8kBJqiz1OBcGXwS7jJ/ Jsww== X-Gm-Message-State: AOAM5334hnHVKQJZuOEHx8Hyr/oln7Pqmkd//OVTc3GuYE9VbcAol8tm Y19KGygi5gBAZxL8Zi0ur/SKWVfxFjE= X-Received: from seanjc.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:3e5]) (user=seanjc job=sendgmr) by 2002:a17:902:e9c6:b029:12d:4cb3:3985 with SMTP id 6-20020a170902e9c6b029012d4cb33985mr9312260plk.56.1629330980601; Wed, 18 Aug 2021 16:56:20 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 18 Aug 2021 23:56:15 +0000 Message-Id: <20210818235615.2047588-1-seanjc@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.33.0.rc1.237.g0d66db33f3-goog Subject: [PATCH] KVM: x86/mmu: Complete prefetch for trailing SPTEs for direct, legacy MMU From: Sean Christopherson To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Sergey Senozhatsky , Ben Gardon Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Make a final call to direct_pte_prefetch_many() if there are "trailing" SPTEs to prefetch, i.e. SPTEs for GFNs following the faulting GFN. The call to direct_pte_prefetch_many() in the loop only handles the case where there are !PRESENT SPTEs preceding a PRESENT SPTE. E.g. if the faulting GFN is a multiple of 8 (the prefetch size) and all SPTEs for the following GFNs are !PRESENT, the loop will terminate with "start = sptep+1" and not prefetch any SPTEs. Prefetching trailing SPTEs as intended can drastically reduce the number of guest page faults, e.g. accessing the first byte of every 4kb page in a 6gb chunk of virtual memory, in a VM with 8gb of preallocated memory, the number of pf_fixed events observed in L0 drops from ~1.75M to <0.27M. Note, this only affects memory that is backed by 4kb pages as KVM doesn't prefetch when installing hugepages. Shadow paging prefetching is not affected as it does not batch the prefetches due to the need to process the corresponding guest PTE. The TDP MMU is not affected because it doesn't have prefetching, yet... Fixes: 957ed9effd80 ("KVM: MMU: prefetch ptes when intercepted guest #PF") Cc: Sergey Senozhatsky Cc: Ben Gardon Signed-off-by: Sean Christopherson --- Cc'd Ben as this highlights a potential gap with the TDP MMU, which lacks prefetching of any sort. For large VMs, which are likely backed by hugepages anyways, this is a non-issue as the benefits of holding mmu_lock for read likely masks the cost of taking more VM-Exits. But VMs with a small number of vCPUs won't benefit as much from parallel page faults, e.g. there's no benefit at all if there's a single vCPU. arch/x86/kvm/mmu/mmu.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index a272ccbddfa1..daf7df35f788 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -2818,11 +2818,13 @@ static void __direct_pte_prefetch(struct kvm_vcpu *vcpu, if (!start) continue; if (direct_pte_prefetch_many(vcpu, sp, start, spte) < 0) - break; + return; start = NULL; } else if (!start) start = spte; } + if (start) + direct_pte_prefetch_many(vcpu, sp, start, spte); } static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) -- 2.33.0.rc1.237.g0d66db33f3-goog