Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2180480pxj; Sun, 16 May 2021 17:34:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyK0sIzSz8/N5TZ2erzS6dCbq5qNsMjXp158OJ5sn2ouRFXOBm9y8+oVLy5yXizgGmG/JN0 X-Received: by 2002:a02:ac05:: with SMTP id a5mr54633230jao.89.1621211695193; Sun, 16 May 2021 17:34:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621211695; cv=none; d=google.com; s=arc-20160816; b=O9Dg1XpXuclJF91OMpuFpsfy8Ta7dM0fXEUtqzj39rsmg/jErgDe1+OqjVkelu5H3M PCm8TLwH7tiZrKS3+s7/7DXEkxnTPzzVxE3pN0y6TuTmOSwyFN0XcUEmKY14KCxGTgMg JpnHs02CWP5pUxCScll8pVAL1rlqzPjKV0lNkoKbdjQoNuLBCCkut2qOxahSkDgTY6LK squqMAIqB42Wns3Q7EHjd75ompZcJymYjpzH58jEprNmW5ewLqxayrepVEeoqckvuQFb HhwsmekKSwlcVxVL9Qdc0gQm362+2Nmz/CH+jCcTv12BDSGCREYPRIgNoXDHwScMSqo5 bvgw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=aOriVOpLPBxgaeEq5b1h1bNwb7zcub9Pbm5AJWO8tj8=; b=Wwk5cwo97uN4T8f664pMlDtl7jADGIVdJOlthltzPL4HOTc/k69rbJVHNdVvPSEQMh XIAkiQQ0JLIinG8Gd4cyXNhXglloQn9d2GUjJksRARWD8uuHFs66kvsp7TP9mhS5lc/f 4p5zBxCTnrUdfFttCXGsZYlj5M0uZyQwwAze9K4BiVh4ms8lgGYUkFlTDpFF2N7103gb cYL69AigAD5/pkT4a04c/x9Hnh+QbWnU4gF2RwiTUaTxj/U5ls3bhWtK/rDo4z2E0R1R UnCHDzbF2RsUSbS5LP5u+oWVQ0L+3GcIsXXR8tn42wmvW3TzWqYssiZlMT/uIrnNEwAm FWFw== 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 y5si8189113ilu.66.2021.05.16.17.34.43; Sun, 16 May 2021 17:34:55 -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 S234384AbhEPVrH (ORCPT + 99 others); Sun, 16 May 2021 17:47:07 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:54500 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234389AbhEPVq4 (ORCPT ); Sun, 16 May 2021 17:46:56 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1liOZe-000824-SO; Sun, 16 May 2021 23:45:22 +0200 From: "Maciej S. Szmigiero" To: Paolo Bonzini , Vitaly Kuznetsov Cc: Sean Christopherson , 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 Subject: [PATCH v3 8/8] KVM: Optimize overlapping memslots check Date: Sun, 16 May 2021 23:44:34 +0200 Message-Id: <61d53c8121b5b906bbed363e493ac75aebbda693.1621191552.git.maciej.szmigiero@oracle.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Maciej S. Szmigiero" Do a quick lookup for possibly overlapping gfns when creating or moving a memslot instead of performing a linear scan of the whole memslot set. Signed-off-by: Maciej S. Szmigiero --- virt/kvm/kvm_main.c | 65 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 189504b27ca6..11814730cbcb 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1474,6 +1474,59 @@ static int kvm_delete_memslot(struct kvm *kvm, return kvm_set_memslot(kvm, mem, old, &new, as_id, KVM_MR_DELETE); } +static bool kvm_check_memslot_overlap(struct kvm_memslots *slots, + struct kvm_memory_slot *nslot) +{ + int idxactive = kvm_memslots_idx(slots); + struct rb_node *node; + + /* + * Find the slot with the lowest gfn that can possibly intersect with + * the new slot, so we'll ideally have slot start <= nslot start + */ + node = kvm_memslots_gfn_upper_bound(slots, nslot->base_gfn); + 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 > nslot start. + */ + pnode = rb_prev(node); + if (pnode) + node = pnode; + } else { + /* a NULL node below means no existing slots */ + node = rb_last(&slots->gfn_tree); + } + + for ( ; node; node = rb_next(node)) { + struct kvm_memory_slot *cslot; + + cslot = container_of(node, struct kvm_memory_slot, + gfn_node[idxactive]); + + /* + * if this slot starts beyond or at the end of the new slot + * so does every next one + */ + if (cslot->base_gfn >= nslot->base_gfn + nslot->npages) + break; + + if (cslot->id == nslot->id) + continue; + + if (cslot->base_gfn >= nslot->base_gfn) + return true; + + if (cslot->base_gfn + cslot->npages > nslot->base_gfn) + return true; + } + + return false; +} + /* * Allocate some memory and give it an address in the guest physical address * space. @@ -1559,16 +1612,10 @@ int __kvm_set_memory_region(struct kvm *kvm, } if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) { - int ctr; - /* Check for overlaps */ - kvm_for_each_memslot(tmp, ctr, __kvm_memslots(kvm, as_id)) { - if (tmp->id == id) - continue; - if (!((new.base_gfn + new.npages <= tmp->base_gfn) || - (new.base_gfn >= tmp->base_gfn + tmp->npages))) - return -EEXIST; - } + if (kvm_check_memslot_overlap(__kvm_memslots(kvm, as_id), + &new)) + return -EEXIST; } /* Allocate/free page dirty bitmap as needed */