Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3798701pxj; Tue, 1 Jun 2021 13:26:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwHqg8U+rWNaiIeyATSpuZndOLY3S0FDGppm3ZM82y8gqmMhNCZH/ty2mWs+0TNapBOJ+xU X-Received: by 2002:a92:3f03:: with SMTP id m3mr22091272ila.34.1622579203462; Tue, 01 Jun 2021 13:26:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622579203; cv=none; d=google.com; s=arc-20160816; b=JSpv6FgJm1hAm9RJCulkQPUyTh173h3YwbKkhP1J+sQDcFV3Wzq8IO6prOwTGWxrGL aH2sIMBkxhqzfQNcZWUqa0nYjfldSdYU86xgxLOp0dXD2khJGFb25mmQhd1MiXz2sOCK MFhTxTF058oXRnNC3kTx3Ex+0Dsox94nLyTxIG1WYjmmFmjvCeaGDsEW2BLQmzw/nGT5 CkJgUMsyUj2MBNbIYBRel6a42SRmvlp49mRGg38JhWRf7Btep74Hv8p7ibxBLnh7P/ot Mn3n25M8apIf75afyAQQudaS/ACFQbRcuSE8L4oCuCc6LYGIZ/WjXYh7K2z2wSFw3Vqx ZETg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:subject:from :references:cc:to; bh=o3LvnIwti7SLTM4C0wncBPilL/iwg6xuKsihd4RKN2A=; b=WrZnJahvMREIm1LOAbydUrTSF303JMOCdK1jsZ0NMFmwrxHxwVOmDwdfV/A/V3kjvL lVhemW5ITeOSBSf7gkRUTi54cW1YYL1lCUD+cis2cgbbec6Vp8U3bUkIVoU+SX3YFy9G wgxe+jmkvBgVePW3N+sg+kNM2vvhgjCyLHiWsNYF9Shv7QBkOY3EwnTpeNGp5k7K0XiF sp91HV0BIFLO6psAjw8WdLSTLrKdnpeCNc4Q0GBQrcy7sRlCbOR0GvLE9WYzat4b+FFZ OD8XpEtVfV1+jEafIDfkzJHCVUyg+CtSEcjZ8F6mfUVOWrJtE6VefipzYENz25eHOXYl wnHg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f2si20960731ioz.41.2021.06.01.13.26.30; Tue, 01 Jun 2021 13:26:43 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234851AbhFAU1O (ORCPT + 99 others); Tue, 1 Jun 2021 16:27:14 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:35336 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234799AbhFAU1N (ORCPT ); Tue, 1 Jun 2021 16:27:13 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1loAx2-0008T9-Ui; Tue, 01 Jun 2021 22:25:24 +0200 To: Sean Christopherson Cc: Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Igor Mammedov , Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , Huacai Chen , Aleksandar Markovic , Paul Mackerras , Christian Borntraeger , Janosch Frank , David Hildenbrand , Cornelia Huck , Claudio Imbrenda , Joerg Roedel , kvm@vger.kernel.org, linux-kernel@vger.kernel.org References: <38333ef36e7812e1b9f9d24e726ca632997a8ef1.1621191552.git.maciej.szmigiero@oracle.com> From: "Maciej S. Szmigiero" Subject: Re: [PATCH v3 7/8] KVM: Optimize gfn lookup in kvm_zap_gfn_range() Message-ID: <99a961d2-d46c-ead9-1138-45f6587a60ed@maciej.szmigiero.name> Date: Tue, 1 Jun 2021 22:25:19 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 26.05.2021 19:33, Sean Christopherson wrote: > On Sun, May 16, 2021, Maciej S. Szmigiero wrote: >> From: "Maciej S. Szmigiero" >> >> Introduce a memslots gfn upper bound operation and use it to optimize >> kvm_zap_gfn_range(). >> This way this handler can do a quick lookup for intersecting gfns and won't >> have to do a linear scan of the whole memslot set. >> >> Signed-off-by: Maciej S. Szmigiero >> --- >> arch/x86/kvm/mmu/mmu.c | 41 ++++++++++++++++++++++++++++++++++++++-- >> include/linux/kvm_host.h | 22 +++++++++++++++++++++ >> 2 files changed, 61 insertions(+), 2 deletions(-) >> >> diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c >> index 7222b552d139..f23398cf0316 100644 >> --- a/arch/x86/kvm/mmu/mmu.c >> +++ b/arch/x86/kvm/mmu/mmu.c >> @@ -5490,14 +5490,51 @@ void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end) >> int i; >> bool flush = false; >> >> + if (gfn_end == gfn_start || WARN_ON(gfn_end < gfn_start)) >> + return; >> + >> write_lock(&kvm->mmu_lock); >> for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { >> - int ctr; >> + int idxactive; >> + struct rb_node *node; >> >> slots = __kvm_memslots(kvm, i); >> - kvm_for_each_memslot(memslot, ctr, slots) { >> + idxactive = kvm_memslots_idx(slots); >> + >> + /* >> + * Find the slot with the lowest gfn that can possibly intersect with >> + * the range, so we'll ideally have slot start <= range start >> + */ >> + node = kvm_memslots_gfn_upper_bound(slots, gfn_start); >> + if (node) { >> + struct rb_node *pnode; >> + >> + /* >> + * A NULL previous node means that the very first slot >> + * already has a higher start gfn. >> + * In this case slot start > range start. >> + */ >> + pnode = rb_prev(node); >> + if (pnode) >> + node = pnode; >> + } else { >> + /* a NULL node below means no slots */ >> + node = rb_last(&slots->gfn_tree); >> + } >> + >> + for ( ; node; node = rb_next(node)) { >> gfn_t start, end; > > Can this be abstracted into something like: > > kvm_for_each_memslot_in_gfn_range(...) { > > } > > and share that implementation with kvm_check_memslot_overlap() in the next patch? > > I really don't think arch code should be poking into gfn_tree, and ideally arch > code wouldn't even be aware that gfn_tree exists. That's a good idea, will do. Thanks, Maciej