From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE38AC433FE for ; Wed, 20 Oct 2021 22:39:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A38CB61354 for ; Wed, 20 Oct 2021 22:39:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230494AbhJTWlh (ORCPT ); Wed, 20 Oct 2021 18:41:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51186 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229842AbhJTWlf (ORCPT ); Wed, 20 Oct 2021 18:41:35 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5C7E8C061749 for ; Wed, 20 Oct 2021 15:39:20 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id f21so17159758plb.3 for ; Wed, 20 Oct 2021 15:39:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=eGCn0f41D4HpobJWo3fx2mTIugtvwU4VYQMdEwSg0U0=; b=af2oC3k0qcnhiF7y6ar4o729lYsprHHjORYSkiipWKcFN7EujBzRwlS0tM4+QKpWJM HqcOQkGTCWeDXiZlwA29Ae1wXUjBSOCCjbbdrOhFM47MmQe/YsRVnF2Lg/wS6Ug2mING 3zAoiE0ZQx+QTxKNfElvnO45sIaMMOfwpe/O+JqMBhVPrKt8bz7fmKQ5gg7OI+Q5A9Ru mxFZrcimLWX+8JWmFs5hl7gLqPMJ4YtcCZLu5B+nkELZ653oUXVbG/cp+xOK4T9AR1ta bqJw5XNRe+OFARm/ngy0aTudPN2oJn+vRz95YcyC8SZeZrJc4p42SvWiI3yQbY2juzX1 u0eQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=eGCn0f41D4HpobJWo3fx2mTIugtvwU4VYQMdEwSg0U0=; b=Sm3Mtb/v7SboQuf3o7nNFIec0096EBdx1LiqDptkNTK9sFTfTG9/yLZljuCWos0SB0 6kVOkk7wdBKZwTYHlUNoV8n9PkbwU0yWhVHikC3I3yt2dDw4mrm6o53sdNs/HOOL8mCm quzPCktJqGf6txvfhENfIDr9WK+yUZze8hbOfl/tOTNkt9I65iWaBbClw6rmiGrn/YaR XtW5M78wo8sOSpuYkxdLHtbN64elgC47idaFfyphtw9cMeTAbCKUP81izl8ATwuj3Afl 6mvI5vV+hVbXv73T2Z6jTCvMDUwt2WtoaVHEVpULRioPGAhy3mtu+89L3m7V1UBxG2pi tSbQ== X-Gm-Message-State: AOAM533DrUxxuBY4cotPkPsATFkC+Zrz7JVrFApoUn/sSrYNhKVC4htB EIFZbDSp6DUAXD1zEBh/4B/r/w== X-Google-Smtp-Source: ABdhPJx+QU7ObcFGp4YAIapaqa4lFYaMZDXOPAfMUKLdl4uUX1J2w0lDXSx4ODnf4X0d4Enkf0E9Mg== X-Received: by 2002:a17:90a:73ce:: with SMTP id n14mr2074254pjk.215.1634769559470; Wed, 20 Oct 2021 15:39:19 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id q8sm3956649pfu.167.2021.10.20.15.39.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Oct 2021 15:39:18 -0700 (PDT) Date: Wed, 20 Oct 2021 22:39:15 +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 v5 08/13] KVM: Resolve memslot ID via a hash table instead of via a static array Message-ID: References: <555f58fdaec120aa7a6f6fbad06cca796a8c9168.1632171479.git.maciej.szmigiero@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <555f58fdaec120aa7a6f6fbad06cca796a8c9168.1632171479.git.maciej.szmigiero@oracle.com> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Sep 20, 2021, Maciej S. Szmigiero wrote: > @@ -1251,12 +1251,13 @@ static inline void kvm_memslot_delete(struct kvm_memslots *slots, > if (atomic_read(&slots->last_used_slot) >= slots->used_slots) > atomic_set(&slots->last_used_slot, 0); > > - for (i = slots->id_to_index[memslot->id]; i < slots->used_slots; i++) { > + for (i = oldslot - mslots; i < slots->used_slots; i++) { > + hash_del(&mslots[i].id_node); > mslots[i] = mslots[i + 1]; > - slots->id_to_index[mslots[i].id] = i; > + hash_add(slots->id_hash, &mslots[i].id_node, mslots[i].id); > } > + hash_del(&mslots[i].id_node); > mslots[i] = *memslot; > - slots->id_to_index[memslot->id] = -1; Aha! This code has been bugging and I finally figured out why. Fundamentally, this is identical to kvm_memslot_move_backward(), the only difference being that the _backward() variant has a second "stop" condition. But yet this is subtly different in the hash manipulations because performs the final node deletion (which is a random node, that may not be the target node!) outside of the loop, whereas _backward() deletes the target node before the loop. I like the _backward() variant a lot more. And if this code is converted to that approach, i.e. for (i = oldslot - mslots; i < slots->used_slots; i++) { hash_del(&mslots[i + 1].id_node); mslots[i] = mslots[i + 1]; hash_add(slots->id_hash, &mslots[i].id_node, mslots[i].id); } then all three loops fit a similar pattern and we can extract the node craziness into a helper. I know this mostly goes away in the end, but I think/hope it will make the future patches easier to follow this there's only ever one "replace" helper. For convenience, with the s/mmemslot/oldslot and comment changes. --- virt/kvm/kvm_main.c | 63 ++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 26 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 50597608d085..6f5298bc7710 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1231,6 +1231,23 @@ static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot) return 0; } +static void kvm_memslot_replace(struct kvm_memslots *slots, int dst, int src) +{ + struct kvm_memory_slot *mslots = slots->memslots; + + /* + * Remove the source from the hash list, copying the hlist_node data + * would corrupt the list. + */ + hash_del(&mslots[src].id_node); + + /* Copy the source *data*, not the pointer, to the destination. */ + mslots[dst] = mslots[src]; + + /* Re-add the memslot to the list using the destination's node. */ + hash_add(slots->id_hash, &mslots[dst].id_node, mslots[dst].id); +} + /* * Delete a memslot by decrementing the number of used slots and shifting all * other entries in the array forward one spot. @@ -1251,12 +1268,16 @@ static inline void kvm_memslot_delete(struct kvm_memslots *slots, if (atomic_read(&slots->last_used_slot) >= slots->used_slots) atomic_set(&slots->last_used_slot, 0); - for (i = oldslot - mslots; i < slots->used_slots; i++) { - hash_del(&mslots[i].id_node); - mslots[i] = mslots[i + 1]; - hash_add(slots->id_hash, &mslots[i].id_node, mslots[i].id); - } - hash_del(&mslots[i].id_node); + /* + * Remove the to-be-deleted memslot from the list _before_ shifting + * the trailing memslots forward, its data will be overwritten. + * Defer the (somewhat pointless) copying of the memslot until after + * the last slot has been shifted to avoid overwriting said last slot. + */ + hash_del(&oldslot->id_node); + + for (i = oldslot - mslots; i < slots->used_slots; i++) + kvm_memslot_replace(slots, i, i + 1); mslots[i] = *memslot; } @@ -1282,39 +1303,32 @@ static inline int kvm_memslot_move_backward(struct kvm_memslots *slots, struct kvm_memory_slot *memslot) { struct kvm_memory_slot *mslots = slots->memslots; - struct kvm_memory_slot *mmemslot = id_to_memslot(slots, memslot->id); + struct kvm_memory_slot *oldslot = id_to_memslot(slots, memslot->id); int i; - if (!mmemslot || !slots->used_slots) + if (!oldslot || !slots->used_slots) return -1; /* - * The loop below will (possibly) overwrite the target memslot with - * data of the next memslot, or a similar loop in - * kvm_memslot_move_forward() will overwrite it with data of the - * previous memslot. - * Then update_memslots() will unconditionally overwrite and re-add - * it to the hash table. - * That's why the memslot has to be first removed from the hash table - * here. + * Delete the slot from the hash table before sorting the remaining + * slots, the slot's data may be overwritten when copying slots as part + * of the sorting proccess. update_memslots() will unconditionally + * rewrite the entire slot and re-add it to the hash table. */ - hash_del(&mmemslot->id_node); + hash_del(&oldslot->id_node); /* * Move the target memslot backward in the array by shifting existing * memslots with a higher GFN (than the target memslot) towards the * front of the array. */ - for (i = mmemslot - mslots; i < slots->used_slots - 1; i++) { + for (i = oldslot - mslots; i < slots->used_slots - 1; i++) { if (memslot->base_gfn > mslots[i + 1].base_gfn) break; WARN_ON_ONCE(memslot->base_gfn == mslots[i + 1].base_gfn); - /* Shift the next memslot forward one and update its index. */ - hash_del(&mslots[i + 1].id_node); - mslots[i] = mslots[i + 1]; - hash_add(slots->id_hash, &mslots[i].id_node, mslots[i].id); + kvm_memslot_replace(slots, i, i + 1); } return i; } @@ -1343,10 +1357,7 @@ static inline int kvm_memslot_move_forward(struct kvm_memslots *slots, WARN_ON_ONCE(memslot->base_gfn == mslots[i - 1].base_gfn); - /* Shift the next memslot back one and update its index. */ - hash_del(&mslots[i - 1].id_node); - mslots[i] = mslots[i - 1]; - hash_add(slots->id_hash, &mslots[i].id_node, mslots[i].id); + kvm_memslot_replace(slots, i, i - 1); } return i; } --