Received: by 2002:a05:6a10:8a4d:0:0:0:0 with SMTP id dn13csp1080417pxb; Fri, 13 Aug 2021 13:03:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcqyBZPAS1u8bMvYYytMHOBzB+lHHOB2tRoOV4MuU35Hq+OW016080vkXDjbdRhnXk+X5p X-Received: by 2002:a05:6402:35d2:: with SMTP id z18mr5280062edc.282.1628885031518; Fri, 13 Aug 2021 13:03:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628885031; cv=none; d=google.com; s=arc-20160816; b=j2TKnjR56HJQ7Mn9NvaVnthQO05bfAaSL9ZtC1PXE6rkSW1SyDZs4xrWWu64zrm6dP cER8tHdET9vYCG7pQ40y8DoNJaqnlhIc7ZBXN3Gf7EFCJyMk8u75DnUx9T2A4qThBniL 3/NGwfunYXhOB1h1fckWsfi8qJO3QAuhrBu5E+xkaYCp1/vNa88xNuu8T6ZiAxHqJ4f4 uxqzH250P4F/yA3vKYJQIcC4Jc0TjUQJUwdMg4nCKznbwGlFdJO7X/AE4EkEeE0heut4 m8Bv1+N8EE6CB6wq7H6ClOqSaW/lCeAhdjHVXtSkxSgAiLSLiuEYLgycgkq5CoFg+iVj HZeQ== 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=l+zBPHG/YVHC3aaViKEzIyFp/FR9JnzAer+525+zREk=; b=CSCkTZI3egn7r9oh/t16Mm3xcz41LF5bcGr66FlmM2Vp7aTeCO5yfS1bVMC50aSLkq 3HZSozM9Dpty5JBDNTi0XNtwBy7lhWcQFSdZGXHTDJbB/jP2GotXvWeW8QT2jOyjfvPu iKsmcqiX/fGa1Ucv/vhet01GbwQDqNOXtQVxImsbBfxn1zYdJ79FzTyY31L098+OJnPP Fd6UjfeUPSCLrtGU/2jWZX9v08PtKWDsmNu+34JEeNLJ9v3NLyRjf0dAorp1W8Jev1+w 02iZoXSOTD578+j9WbjmbutziokwsgezJIWZAJgGxj4Nexah1e1acoyQ1kkw5sWc7j7F Vrgw== 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 qq24si2248548ejb.538.2021.08.13.13.03.27; Fri, 13 Aug 2021 13:03:51 -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 S234337AbhHMUBw (ORCPT + 99 others); Fri, 13 Aug 2021 16:01:52 -0400 Received: from vps-vb.mhejs.net ([37.28.154.113]:50324 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234308AbhHMUBm (ORCPT ); Fri, 13 Aug 2021 16:01:42 -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 1mEcwz-00047P-1v; Fri, 13 Aug 2021 21:34:41 +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 v4 13/13] KVM: Optimize overlapping memslots check Date: Fri, 13 Aug 2021 21:33:26 +0200 Message-Id: X-Mailer: git-send-email 2.32.0 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 | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 26aa3690b172..91304edb00e9 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1679,6 +1679,30 @@ 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 idx = slots->node_idx; + gfn_t nend = nslot->base_gfn + nslot->npages; + struct rb_node *node; + + kvm_for_each_memslot_in_gfn_range(node, slots, nslot->base_gfn, nend) { + struct kvm_memory_slot *cslot; + gfn_t cend; + + cslot = container_of(node, struct kvm_memory_slot, gfn_node[idx]); + cend = cslot->base_gfn + cslot->npages; + if (cslot->id == nslot->id) + continue; + + /* kvm_for_each_in_gfn_no_more() guarantees that cslot->base_gfn < nend */ + if (cend > nslot->base_gfn) + return true; + } + + return false; +} + /* * Allocate some memory and give it an address in the guest physical address * space. @@ -1764,16 +1788,10 @@ int __kvm_set_memory_region(struct kvm *kvm, } if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) { - int bkt; - /* Check for overlaps */ - kvm_for_each_memslot(tmp, bkt, __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 */