Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp949704pxb; Tue, 19 Oct 2021 16:56:24 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy6IpEinS3UxCB7T4aGjVTCgJQshHn+iV8/jVAM4eGIp3jwtrXIiHNiNGypK+K0gWCUKPEB X-Received: by 2002:a17:902:da8a:b0:13f:1a39:e02f with SMTP id j10-20020a170902da8a00b0013f1a39e02fmr36410212plx.82.1634687783830; Tue, 19 Oct 2021 16:56:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634687783; cv=none; d=google.com; s=arc-20160816; b=iaLU59nfsCcVjdLf1kHJ59pOLdjr2fVnELEgSknpLK82rjHuG3UOw9hCA6hMAUUCkd irjIGP3kIKvym8kgPv/blQnuAvKD9UJ/M0TJ4Q7gBBfZItgOzwhDLXBL7YMSbFLBs/vP NFaYtKsNZUJLdlaoD9FHPusF+grMTw8KJbrNdhDWHiLVnCkGHqmV3WZRstvKs6ZJehMO DvxloM+EwtZ2GsB5AIFQV0+pf8tHMvPnqc/U1efBdxdCc3ysD51T4hg5cIkxEWcKSZZI pmhPXY8FAGxrSUk/JY1QkWBwLzV4E1pIyMoHG+62p98zghtKNFlmUtEuTDIOObVhcg7C X9kA== 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=FD+dLvVnnCC/StfyVK70KDJDUKEKowHXu1o53Gq30mA=; b=u+BeKWIISPd12chWR7TsW0rU0GpFj1A3EowyXrpq8XbhzlZvRBJC2F3bEuAd6w97Hr A9rf2AUOrq/ECdYlA0aui9u1ZL5oGRlogGLvUZUDhivmcYklP4vLW8lZNj3MpJIFquaq 39hQ65EolNPO1pidVzs+tNM05EcLd9IMzk8pWJ1jNGPRaiefTvYaWAuSZOYg2QQ7I4NV i1cdk8L0o7f/P4okJVZBUopzoH8F3eODDL+xt1QuVTsHpebUA3LpSwF8ygRtpSkbFRtE ND01VWKhzeFUA6h+v6+0llpwVxtL62aeHS76PpE+pyuF+FW4Bk9XfHEOBPTab5KATbEl 0jrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b="VB/HUAgg"; 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 a22si720622pgw.118.2021.10.19.16.56.10; Tue, 19 Oct 2021 16:56:23 -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=20210112 header.b="VB/HUAgg"; 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 S229707AbhJSX51 (ORCPT + 99 others); Tue, 19 Oct 2021 19:57:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229602AbhJSX50 (ORCPT ); Tue, 19 Oct 2021 19:57:26 -0400 Received: from mail-pg1-x52a.google.com (mail-pg1-x52a.google.com [IPv6:2607:f8b0:4864:20::52a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DE866C061746 for ; Tue, 19 Oct 2021 16:55:11 -0700 (PDT) Received: by mail-pg1-x52a.google.com with SMTP id c4so13820930pgv.11 for ; Tue, 19 Oct 2021 16:55:11 -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=FD+dLvVnnCC/StfyVK70KDJDUKEKowHXu1o53Gq30mA=; b=VB/HUAggfIPegcPbYKVjT4DV1b471yus1m7ifa3zfMqwPSXH+lm+/mouqjOlJinjys j2XfS5PC2W8r+Y71yY+AQCUO2gu2MmCIYBGf/BPIYB/2CiwoW2mk1h4aFtrSX1lsscq+ TiE82aKzNVh3Og7as7mxcMpp6nUSLSrL6KG+wGVc8yYQwsNRi5N27h885DNfXpPrj5+c dxnHXynpgQN2WhJFmII1m9yYb3zG6QfF3aOLBWtiAvxD3R4YEpvamUh3XDpnLbxj5iSw sLyCjfNcgqtBQVN8x4HGTgPUcRxrKWgorj80o2O+K7XLXhkTxNZZ4ru5JrxdiXlNBtyX IzPw== 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=FD+dLvVnnCC/StfyVK70KDJDUKEKowHXu1o53Gq30mA=; b=b1isAlGUiGPtiU48rk4gcpsyNm6Cw8FSqFgmdRegC5Rl+fSiiqGTxsiSu5dD4PdYnB N6I1cTISurqSMQLM1BcBCRDc3T8PajDahWLL9NKvjp/Pb3LGogbeilUJc1kZLlAsGNoL 7ixgTlx5Y6zrtLsUFdv4rL6HLUN76Rl8Tz1URLN9PsPuulpP/M2FQ0EH/LdXmNPIlNHN hwYYw/WBDrMzDrTZJYjvQEec04MFcqpOj/CQz37tbhpBqLWfJYEglasTWMYN/IrEZ6ff VM4knL9LgDCKAUiLVYLYDpkg9rMIhzwoUJFkuaqOYH0Wk1OknzoaviqI5mjLbk5qLT6q yWLw== X-Gm-Message-State: AOAM533MBEWYF2eWWBJJiOsZii6nAF8QjRnHE6rvSQ20Sbij7pk+dl3w VMIBSaFc4rae+jkEDnUDXoUH2g== X-Received: by 2002:a63:18d:: with SMTP id 135mr31151991pgb.78.1634687710983; Tue, 19 Oct 2021 16:55:10 -0700 (PDT) Received: from google.com (157.214.185.35.bc.googleusercontent.com. [35.185.214.157]) by smtp.gmail.com with ESMTPSA id bt5sm262736pjb.9.2021.10.19.16.55.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Oct 2021 16:55:10 -0700 (PDT) Date: Tue, 19 Oct 2021 23:55:06 +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 07/13] KVM: Just resync arch fields when slots_arch_lock gets reacquired Message-ID: References: <311810ebd1111bed50d931d424297384171afc36.1632171479.git.maciej.szmigiero@oracle.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <311810ebd1111bed50d931d424297384171afc36.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: > From: "Maciej S. Szmigiero" > > There is no need to copy the whole memslot data after releasing > slots_arch_lock for a moment to install temporary memslots copy in > kvm_set_memslot() since this lock only protects the arch field of each > memslot. > > Just resync this particular field after reacquiring slots_arch_lock. I assume this needed to avoid having a mess when introducing the r-b tree? If so, please call that out. Iterating over the slots might actually be slower than the full memcpy, i.e. as a standalone patch this may or may not be make sense. > Signed-off-by: Maciej S. Szmigiero > --- > virt/kvm/kvm_main.c | 17 ++++++++++++----- > 1 file changed, 12 insertions(+), 5 deletions(-) > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > index 348fae880189..48d182840060 100644 > --- a/virt/kvm/kvm_main.c > +++ b/virt/kvm/kvm_main.c > @@ -1482,6 +1482,15 @@ static void kvm_copy_memslots(struct kvm_memslots *to, > memcpy(to, from, kvm_memslots_size(from->used_slots)); > } > > +static void kvm_copy_memslots_arch(struct kvm_memslots *to, > + struct kvm_memslots *from) > +{ > + int i; > + > + for (i = 0; i < from->used_slots; i++) > + to->memslots[i].arch = from->memslots[i].arch; This should probably be a memcpy(), I don't know what all shenanigans the compiler can throw at us if it gets to copy a struct by value. > +} > + > /* > * Note, at a minimum, the current number of used slots must be allocated, even > * when deleting a memslot, as we need a complete duplicate of the memslots for There's an out-of-sight comment that's now stale, can you revert to the pre-slots_arch_lock comment? diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 48d182840060..ef3345428047 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1555,9 +1555,10 @@ static int kvm_set_memslot(struct kvm *kvm, slot->flags |= KVM_MEMSLOT_INVALID; /* - * We can re-use the memory from the old memslots. - * It will be overwritten with a copy of the new memslots - * after reacquiring the slots_arch_lock below. + * We can re-use the old memslots, the only difference from the + * newly installed memslots is the invalid flag, which will get + * dropped by update_memslots anyway. We'll also revert to the + * old memslots if preparing the new memory region fails. */ slots = install_new_memslots(kvm, as_id, slots); > @@ -1567,10 +1576,10 @@ static int kvm_set_memslot(struct kvm *kvm, > /* > * The arch-specific fields of the memslots could have changed > * between releasing the slots_arch_lock in > - * install_new_memslots and here, so get a fresh copy of the > - * slots. > + * install_new_memslots and here, so get a fresh copy of these > + * fields. > */ > - kvm_copy_memslots(slots, __kvm_memslots(kvm, as_id)); > + kvm_copy_memslots_arch(slots, __kvm_memslots(kvm, as_id)); > } > > r = kvm_arch_prepare_memory_region(kvm, old, new, mem, change); > @@ -1587,8 +1596,6 @@ static int kvm_set_memslot(struct kvm *kvm, > > out_slots: > if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { > - slot = id_to_memslot(slots, old->id); > - slot->flags &= ~KVM_MEMSLOT_INVALID; > slots = install_new_memslots(kvm, as_id, slots); > } else { The braces can be dropped since both branches are now single lines. > mutex_unlock(&kvm->slots_arch_lock);