Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1865243pxj; Wed, 19 May 2021 16:08:06 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxyw+lCHrnmsdYpzxIQUBQzHyhAyDZNWYTRx2ELdtjsCI5n80heI/pGxDfI9WF876KbkCws X-Received: by 2002:a02:ca4b:: with SMTP id i11mr1939882jal.77.1621465686541; Wed, 19 May 2021 16:08:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621465686; cv=none; d=google.com; s=arc-20160816; b=pqAEav96aOeipa/R+xZEDbDCObit+TAbM0C8GhEWVCQ6ZXFXRavkZnsrTRtst40nNS dRmTMo5pacoGiKEcUs3DZ+g1WvWgBJN0aoEY0218D7+fhBdgtP813iqRB0zDfkE6e3jy 9bcUHxo7pDzMivdNVH3z+GDL1+nZ65j3/Y0bOhlw+Dhm/Q1KgmOY9E3zQDwhnlg68DY1 GC60NbJldRvM93O5N5YvET895Zg6ffJUn9ZGrY9BPQG1NoVrbQQiWwKVQI7fqd9eqaFt vNo8tbVDrLbuDyN7MvYp+RCni4pIB+i0PBh1dFZ4IgBWkzH4NhuCYUTQxNp1KAwrWaH9 6lBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=RyfmXcmpiPf29zi7Cou4H0ygilUNz1aN0mITS576Vy8=; b=CIDlXOuJA+KiWhbkbjYDAcmI97B0Cu8FBtj7SiVH01u52lR4CbQVTHQvuJNuP924wt QyiOxcGaNVGViHQ/ShccLad4a2SfXwvUXdIn17pDLPhEfjhSxu9VxC3KpjNJt+Mtm1tr rQ8Iv87DA5UuUUfURFVnJZudfnUXIcHyduO5iDulYSvXVeK9lsizEGA1JXnImdzhaUma iIfYogSoVbHyKScT+vNXZsPn3yLdNiGJ+V2u45vPaAVcLi2gJR+O/vb4kkRLVowns2sL Xor5FNOwaRSodE2ulbzpf+PNnFW4yP38HVBktNv+JKvC8dgiLip7l+ohh5Oy4p8q1cfP ExAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=p5kMLOk1; 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 o14si809359iow.26.2021.05.19.16.07.52; Wed, 19 May 2021 16:08:06 -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=p5kMLOk1; 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 S229953AbhESXIb (ORCPT + 99 others); Wed, 19 May 2021 19:08:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229465AbhESXIa (ORCPT ); Wed, 19 May 2021 19:08:30 -0400 Received: from mail-pj1-x1029.google.com (mail-pj1-x1029.google.com [IPv6:2607:f8b0:4864:20::1029]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 20C8AC061574 for ; Wed, 19 May 2021 16:07:09 -0700 (PDT) Received: by mail-pj1-x1029.google.com with SMTP id n6-20020a17090ac686b029015d2f7aeea8so4379994pjt.1 for ; Wed, 19 May 2021 16:07:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=RyfmXcmpiPf29zi7Cou4H0ygilUNz1aN0mITS576Vy8=; b=p5kMLOk18AUGUrUrS4DlS27GN3nypoHJTs+FtdrccTagn+H1860CPR39MOF6TjlTqU SDMnN0WGV3oOSuEt2l1EIvQkhZwCuHWhur6mLeTCa46lcJHHs9HMCPUnEB+P9bj89pmk T3F8+5or506sH8OOj63imrYap+w2KjNxgoKFeDDQdT2HjvsRxPggXk1L6+OkqacbZO05 o1jmT9rJ6a1z0x2IGCR6kKG46wfSxu/bUHRgHR5cubhhsc+NyFIkf7K9ID0nHZ1hliCx 7DPIwq38FBONTOArY4n2qKTZ/kc+BtO7l2S2ifYuA2N2hRHPtfoO0cY7C4T7lwMt4sCr 57ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=RyfmXcmpiPf29zi7Cou4H0ygilUNz1aN0mITS576Vy8=; b=ubUf36vJOUI8tqvzV5f70SbpZAFuwdM8jeNgiVuPvi5FzDllpaKFikNnsEpGnDO7+X VRysQgWrM5kBJlMvw40J+cw23cTM/2qp1B5Z5D9vQNZ0gGSSmgkgBjKn5miF5Z4G2/Ox l7eVysKCgQ/Qs8H7WkiAYO9NjtHkYcYxAQ8VNM7ZoAx0Gx/TyNsOD8jVBIf/ibwRE3eG 5FQCqD5YEbNLcSiSD6unQZMM9Ey2VFVWi1ADLz+qBG6mVOr47QOsnhV3rQi5DOtV4uaf 1EhOo3jCcsMwclRtERJpV0Og1FubpgIgDa/p2+/e5XcKP6bINPLDlXnWHCP+CIOOxY17 /OmQ== X-Gm-Message-State: AOAM530lBNx/U4useCKnoBx6Wr8N/cRPsqcfmA6KEfPZ36Y5jorW34Cd In48BF6w3aztMNWI4L+5MsRQEQ== X-Received: by 2002:a17:90a:8816:: with SMTP id s22mr1733707pjn.25.1621465628405; Wed, 19 May 2021 16:07:08 -0700 (PDT) Received: from google.com (240.111.247.35.bc.googleusercontent.com. [35.247.111.240]) by smtp.gmail.com with ESMTPSA id l67sm340936pgl.18.2021.05.19.16.07.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 May 2021 16:07:07 -0700 (PDT) Date: Wed, 19 May 2021 23:07:03 +0000 From: Sean Christopherson To: "Maciej S. Szmigiero" 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 Subject: Re: [PATCH v3 4/8] KVM: Introduce memslots hva tree Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Nit: something like "KVM: Use interval tree to do fast hva lookup in memslots" would be more helpful when perusing the shortlogs. Stating that a tree is being added doesn't provide any hint as to why, or even the what is somewhat unclear. On Sun, May 16, 2021, Maciej S. Szmigiero wrote: > From: "Maciej S. Szmigiero" > > The current memslots implementation only allows quick binary search by gfn, > quick lookup by hva is not possible - the implementation has to do a linear > scan of the whole memslots array, even though the operation being performed > might apply just to a single memslot. > > This significantly hurts performance of per-hva operations with higher > memslot counts. > > Since hva ranges can overlap between memslots an interval tree is needed > for tracking them. > > Signed-off-by: Maciej S. Szmigiero > --- ... > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > index d3a35646dfd8..f59847b6e9b3 100644 > --- a/include/linux/kvm_host.h > +++ b/include/linux/kvm_host.h > @@ -27,6 +27,7 @@ > #include > #include > #include > +#include > #include > #include > > @@ -358,6 +359,7 @@ static inline int kvm_vcpu_exiting_guest_mode(struct kvm_vcpu *vcpu) > > struct kvm_memory_slot { > struct hlist_node id_node; > + struct interval_tree_node hva_node; > gfn_t base_gfn; > unsigned long npages; > unsigned long *dirty_bitmap; > @@ -459,6 +461,7 @@ static inline int kvm_arch_vcpu_memslots_id(struct kvm_vcpu *vcpu) > */ > struct kvm_memslots { > u64 generation; > + struct rb_root_cached hva_tree; > /* The mapping table from slot id to the index in memslots[]. */ > DECLARE_HASHTABLE(id_hash, 7); > atomic_t lru_slot; > @@ -679,6 +682,11 @@ static inline struct kvm_memslots *kvm_vcpu_memslots(struct kvm_vcpu *vcpu) > return __kvm_memslots(vcpu->kvm, as_id); > } > > +#define kvm_for_each_hva_range_memslot(node, slots, start, last) \ kvm_for_each_memslot_in_range()? Or kvm_for_each_memslot_in_hva_range()? Please add a comment about whether start is inclusive or exclusive. I'd also be in favor of hiding this in kvm_main.c, just above the MMU notifier usage. It'd be nice to discourage arch code from adding lookups that more than likely belong in generic code. > + for (node = interval_tree_iter_first(&slots->hva_tree, start, last); \ > + node; \ > + node = interval_tree_iter_next(node, start, last)) \ > + > static inline > struct kvm_memory_slot *id_to_memslot(struct kvm_memslots *slots, int id) > { > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 50f9bc9bb1e0..a55309432c9a 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -488,6 +488,9 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, > struct kvm_memslots *slots; > int i, idx; > > + if (range->end == range->start || WARN_ON(range->end < range->staart)) I'm pretty sure both of these are WARNable offenses, i.e. they can be combined. It'd also be a good idea to use WARN_ON_ONCE(); if a caller does manage to trigger this, odds are good it will get spammed. Also, does interval_tree_iter_first() explode if given bad inputs? If not, I'd probably say just omit this entirely. If it does explode, it might be a good idea to work the sanity check into the macro, even if the macro is hidden here. > + return 0; > + > /* A null handler is allowed if and only if on_lock() is provided. */ > if (WARN_ON_ONCE(IS_KVM_NULL_FN(range->on_lock) && > IS_KVM_NULL_FN(range->handler))) > @@ -507,15 +510,18 @@ static __always_inline int __kvm_handle_hva_range(struct kvm *kvm, > } > > for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { > + struct interval_tree_node *node; > + > slots = __kvm_memslots(kvm, i); > - kvm_for_each_memslot(slot, slots) { > + kvm_for_each_hva_range_memslot(node, slots, > + range->start, range->end - 1) { > unsigned long hva_start, hva_end; > > + slot = container_of(node, struct kvm_memory_slot, > + hva_node); Eh, let that poke out. The 80 limit is more of a guideline. > hva_start = max(range->start, slot->userspace_addr); > hva_end = min(range->end, slot->userspace_addr + > (slot->npages << PAGE_SHIFT)); > - if (hva_start >= hva_end) > - continue; > > /* > * To optimize for the likely case where the address > @@ -787,6 +793,7 @@ static struct kvm_memslots *kvm_alloc_memslots(void) > if (!slots) > return NULL; > > + slots->hva_tree = RB_ROOT_CACHED; > hash_init(slots->id_hash); > > return slots; > @@ -1113,10 +1120,14 @@ static inline void kvm_memslot_delete(struct kvm_memslots *slots, > atomic_set(&slots->lru_slot, 0); > > for (i = dmemslot - mslots; i < slots->used_slots; i++) { > + interval_tree_remove(&mslots[i].hva_node, &slots->hva_tree); > hash_del(&mslots[i].id_node); I think it would make sense to add helpers for these? Not sure I like the names, but it would certainly dedup the code a bit. static void kvm_memslot_remove(struct kvm_memslots *slots, struct kvm_memslot *memslot) { interval_tree_remove(&memslot->hva_node, &slots->hva_tree); hash_del(&memslot->id_node); } static void kvm_memslot_insert(struct kvm_memslots *slots, struct kvm_memslot *memslot) { interval_tree_insert(&memslot->hva_node, &slots->hva_tree); hash_add(slots->id_hash, &memslot->id_node, memslot->id); } > + > mslots[i] = mslots[i + 1]; > + interval_tree_insert(&mslots[i].hva_node, &slots->hva_tree); > hash_add(slots->id_hash, &mslots[i].id_node, mslots[i].id); > } > + interval_tree_remove(&mslots[i].hva_node, &slots->hva_tree); > hash_del(&mslots[i].id_node); > mslots[i] = *memslot; > }