Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp776983pxb; Thu, 26 Aug 2021 14:36:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwmIH31o+PGDSFFeVuOlkkbzFP+n/doY8K9ebvfWv9lHnZ3NRYZAwjjyarYCMWlvZMkDm8m X-Received: by 2002:a05:6e02:1c27:: with SMTP id m7mr4296243ilh.190.1630013801857; Thu, 26 Aug 2021 14:36:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1630013801; cv=none; d=google.com; s=arc-20160816; b=hAL+JcQsE2hjZp2e89G4/rNkX7zcBfkS/97FuGKsCwjw7ql/vD8RQHlc8pEBRsgJNs 4iBwyituDGElQZqVLKzZjHaIQOmTINDqpx9jBOIjRNZsaFqYo986JBIwUuL/mXP2GCep bebosNeURV16uGDQ5hk0fub7CZbmZBtrbk/0PjCVnSDXf78L1VPZdhZOLcYJcHSQbBIB 2Hdx3KQ+aDoekVc6JwvGUkkcdzQrldLNy5Y0q6feIZiueOsQRP66PBnyhBwitbjcZWQ8 pny7Ik3UZKGa60M1KZzmCw6bpLDG5upvOuwOq3SWaDuLpOsX1tiTs87A0R6NIePfF+5A 28lQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vjf5k/4LGu/VKE/74uCD3RNhClu4tQPAgKlOCuWM+IE=; b=EmblHwoaC22BADw5ohs5fm0aDecWHzlnZEVFOSxSVNVzec07MKPUAg3EErniCaaF9M IRY7gYqjhN0BW4NMyYk/uUr+uv4jsUsUwiXBqtSKmwQj9FFvsuzYCxCzCg/qN2CDP/Ux MzDuyPplVdMuySEDlEiDyGGZwSm9bJ6EAuMdWsCxCOmyGY0BDxeIFXsZ3HkBoZ6hKfHq bHaR94hWfU3xcrtS/ZKSY0oFSPzwJovQTLPq0j53yh5GZ62XWDhVTOwB6JjlyjbCz5rP 9Y64sF7sy8580k3qHWf72mAx0FqZbSS6muPn33z/jv0irrb9/IXFbQyvriYvFKnq9DGh hEGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=RdK2k7sv; 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 f12si3766509jav.15.2021.08.26.14.36.27; Thu, 26 Aug 2021 14:36:41 -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=RdK2k7sv; 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 S234348AbhHZVg0 (ORCPT + 99 others); Thu, 26 Aug 2021 17:36:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45480 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230095AbhHZVgZ (ORCPT ); Thu, 26 Aug 2021 17:36:25 -0400 Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED06BC061757 for ; Thu, 26 Aug 2021 14:35:37 -0700 (PDT) Received: by mail-il1-x132.google.com with SMTP id y3so4791504ilm.6 for ; Thu, 26 Aug 2021 14:35:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vjf5k/4LGu/VKE/74uCD3RNhClu4tQPAgKlOCuWM+IE=; b=RdK2k7svMeppUYTFEqP3Z80P/Z/0oxZXSu0k5H3JnQ0aucPp3J/xMi3CbPvX/4pwoO plEmtha7+abcpG4+UQY1za6l4uRod8L4ea6qJOL997zfnPHUXnI2niTdt7NsyxdEv5Rl oM83PcwqsyvddAW2YwV6VytJx/EFB6WLpcQRFElN1u93JtThi3ICBnVA/D9+Nbg2KjiW X3M+SFT5NkMEl5Xkb9guEjWQffL/EOwpU9Tmhxzh2nnouh0GoHjCsM8kwFcyCi7cGgil O9xr862aswoySolE/Uce4EM23J7V8rfUrHmbIvw6Ey+UyRj5zcVVIZ7g5fSIJecWbZJ6 8MgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vjf5k/4LGu/VKE/74uCD3RNhClu4tQPAgKlOCuWM+IE=; b=U/SLYHqauplfuOe9OlAhj1G9QNuzO57hPC9MG1uaq7dyOctPTVIHDfQ08LiNFUIjfp IYxUHulrnCMYweouRl3Nw8qzFMieJnmpU25NUP2ClZnMdqrRdPpKDY6uM2QMtqPLFJsC RS/Sy3GItZvQz0SkbQ3bQL2FHGypZoQoojXUs2Mvu1QAMxBVR9S7wFS+Buu/E/ije8m1 CLzFeHUjJTw3ab/4iQEP4FyRXVDPuY4k/pYQ/VA9AwhldAj3A0bNWILS0cDVfYho557k AGIoROScV6YjensYjdUN72zJ8RJ1dEGm7oTkGATEzVsAXR4yzmhUrEkQo27FprB2o9BS 5ZRw== X-Gm-Message-State: AOAM532hk8gKXYPbc26pL5n9v2FBi5gKEVOVnAMCpUiKk+ugi9cHKPJa xrL8DCRzYQNbds35roYbvjcWwUHgbEdbgrmJpANnYA== X-Received: by 2002:a92:d586:: with SMTP id a6mr4067917iln.283.1630013737153; Thu, 26 Aug 2021 14:35:37 -0700 (PDT) MIME-Version: 1.0 References: <20210818235615.2047588-1-seanjc@google.com> In-Reply-To: From: Ben Gardon Date: Thu, 26 Aug 2021 14:35:26 -0700 Message-ID: Subject: Re: [PATCH] KVM: x86/mmu: Complete prefetch for trailing SPTEs for direct, legacy MMU To: Lai Jiangshan Cc: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm , LKML , Sergey Senozhatsky Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 25, 2021 at 3:49 PM Lai Jiangshan wrote: > > On Thu, Aug 19, 2021 at 7:57 AM Sean Christopherson wrote: > > > > 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 Reviewed-by: Ben Gardon > > --- > > > > 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. Yeah, that probably does represent a reduction in performance for very small VMs. Besides keeping read critical sections small, there's no reason not to do prefetching with the TDP MMU, it just needs to be implemented. > > > > 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); It might be worth explaining some of what you laid out in the commit description here. This function's implementation is not the easiest to read. > > } > > > Reviewed-by: Lai Jiangshan > > > > > static void direct_pte_prefetch(struct kvm_vcpu *vcpu, u64 *sptep) > > -- > > 2.33.0.rc1.237.g0d66db33f3-goog > >