Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2893930pxb; Tue, 13 Apr 2021 12:56:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy+PfkbwFhy1fPR2o9PEtyzO0+NL57stEkRl86KcFMaXMQPVxZoEJgcN8tTVcvmLIXLvtb8 X-Received: by 2002:a17:90b:714:: with SMTP id s20mr1779724pjz.62.1618343817734; Tue, 13 Apr 2021 12:56:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618343817; cv=none; d=google.com; s=arc-20160816; b=tHNYNVW/e6LMZXjK47SnXgIL0ijNEpH7zpPWcKG8v1stoZq35f49iyknQumiodOFKi 3lORUj/vJQPu51cDs2PT8Gt/mvIe0RipHtUzRibwvZmUYvBq5bUQeyh40AaQEJ1uTvJ6 n78jz/q7ihQvi2s1Yvz324A7ZoAyYJIuc7bY2mPeDX0COrs8LBVDR5Xlb3EFAv7MJaKQ 7KeoNi9xkCbslkLxqvB6T3NAsSeWtG48JhruEY464vy+Fa/ykaPRdRZ4AgXnUZpEGcsz 2g2oo38DsPvT2MMOMhWFENsnwIVbX607+ym0ZZjaMfIgBdyAvWf+bfdKwV4FyN+f9vDM Sb+w== 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=VPlRyI+7BNHwC9qwLEvh1YYP8Yo8BcLklWgJtLaBR5U=; b=aG1ykqWmpB9hyrdPQVeOtIGeGarcNMpCRInHEGzw8augPZ/az0kbLVfYsmTvo7E+yb DmgnSn9GMvtfw5uNenlLzQrhD8khWyyX6eczIilp8Oa2aygXm/unMboMblO7AiGywEUc 3l8cMx0BCZLdHmUloUM7Ten6MK7e4HtSD8/FvQhuI/x64GVEUi8xWZsfY0jHBgEKH11S MjnJVv03ClzHtCxRId0mOBeoM7owVgdfjznXmVb+ZL9lXBlwW+GeC+kGe4PVQixhPYPL euLx7goJ1a9kVqk8ZkelsOSc29PsK9t6hzFAMMzD1kdhh26/C3KNWl9lVgRQhXHTPniH DpDg== 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 a4si4593465pld.127.2021.04.13.12.56.44; Tue, 13 Apr 2021 12:56:57 -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 S1346407AbhDMOME (ORCPT + 99 others); Tue, 13 Apr 2021 10:12:04 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:48280 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236867AbhDMOL4 (ORCPT ); Tue, 13 Apr 2021 10:11:56 -0400 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.93.0.4) (envelope-from ) id 1lWJks-00040d-GM; Tue, 13 Apr 2021 16:11:02 +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 v2 8/8] KVM: Optimize overlapping memslots check Date: Tue, 13 Apr 2021 16:10:14 +0200 Message-Id: <3df80b2e4cb3d598c951c9f1a715ee1022ca96be.1618322004.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 a027686657a6..448178f913fb 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1341,6 +1341,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. @@ -1426,16 +1479,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 */