Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1794351rwn; Fri, 9 Sep 2022 04:18:15 -0700 (PDT) X-Google-Smtp-Source: AA6agR6Xzclt6Pyv0A4Mwud3dM1+Z+aHWl0VcS7ifPxpr8lnlTGpFHJuBastUa9fVqLRlSn+6UHO X-Received: by 2002:aa7:8f2b:0:b0:540:d03f:37c5 with SMTP id y11-20020aa78f2b000000b00540d03f37c5mr4637198pfr.24.1662722295723; Fri, 09 Sep 2022 04:18:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662722295; cv=none; d=google.com; s=arc-20160816; b=Gk5XwfimNJlkLPkjG17MyXzsA8yuYCMnb/3WlCo3qqZ7wJxmcjpyxWa/F7tHfoLNWV A+QjRRXnfNNC/LrBhgV9qsVXi8cEc/CL7lLZgzKInVvkVcgv6G1qIajMyEhWcVRRVWPm TohfVHr7A+8gZ/ZmmCLPBQ9RHHEwFfdoM84woyr/4JD4wgkep6C7VBL0SiEeHF5RusJi f7Lq0BiZ91c+rn3xGGxxKqFjOuIYJmqjmUZgmBJ4gA7bKaAytTpLFSCU/zcGTKOMJnIg A7tUTCjugDJXONljEjRxf+eTyyKzWins2/VmhSzX4fzIbAh5NwGKltCX2wrR4VbAno2v lSxg== 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 :dkim-signature; bh=m8Uwqb2u54NGuVgopEXAeF7Kt4xDeaUDYurd/mYS65U=; b=CQa77l20uEgCfAhy3YnvsIgJMgu/3tn62Ii5wHlCLJOoJmqZRXzyAtVTkVTA2sgyOH e3K3EIFWDlVLJdxNVclQ0H6jklM3yn6Q1UYLFl8u7p6V5Gul8enhl5T/5tbJZEutWbq9 DAtvFWMrIw4PILtB8wSsqc8pxIj7I835Zw+f6VH2AVQwgZomH9UKyfkugPlqmgk8Y507 /JKhQvmtk8bRjv/EyUQBqkqEraFvemxxqMuSt6tFB32wbdhSrqMPCond/12Eu+SPV5Zu RTysY6ZR5iumCFoqIltsvZiVOrUiZZQBFGG2jfmLVr8xf4z7/BMAJyOhazKWRwRsa4RF Si+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VTYB6h4M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id on11-20020a17090b1d0b00b001fddf1a3380si260763pjb.109.2022.09.09.04.18.03; Fri, 09 Sep 2022 04:18:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=VTYB6h4M; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231204AbiIIKqN (ORCPT + 99 others); Fri, 9 Sep 2022 06:46:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39932 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231189AbiIIKpT (ORCPT ); Fri, 9 Sep 2022 06:45:19 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A84B44F6AE for ; Fri, 9 Sep 2022 03:45:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1662720316; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=m8Uwqb2u54NGuVgopEXAeF7Kt4xDeaUDYurd/mYS65U=; b=VTYB6h4M80xnaVbbXF7NUprc/JsrZGZL0cvh7xy/yPjdJhWKHbuDy1c/12QJ8Mjv6VqFR/ ZgmISEhgiSisDwIhfXbRL/cfHjKxpmYP+vJU2Vo/Y4PV8jPxLv8RusIv47yn9nwjrRkqtn 4cz7Rftp5Eu35TIlKpcAzWbrZxcGPlg= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-451-YI5_BTabN4-gBYxsFpbhVA-1; Fri, 09 Sep 2022 06:45:11 -0400 X-MC-Unique: YI5_BTabN4-gBYxsFpbhVA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 049063C0E210; Fri, 9 Sep 2022 10:45:11 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9645340D282E; Fri, 9 Sep 2022 10:45:10 +0000 (UTC) From: Emanuele Giuseppe Esposito To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , David Hildenbrand , Maxim Levitsky , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org, Emanuele Giuseppe Esposito Subject: [RFC PATCH 4/9] kvm_main.c: split logic in kvm_set_memslots Date: Fri, 9 Sep 2022 06:45:01 -0400 Message-Id: <20220909104506.738478-5-eesposit@redhat.com> In-Reply-To: <20220909104506.738478-1-eesposit@redhat.com> References: <20220909104506.738478-1-eesposit@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.11.54.2 X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org At this point it is also just a split, but later will handle atomic memslot updates (thus avoiding swapping the memslot list every time). No functional change intended. Signed-off-by: Emanuele Giuseppe Esposito --- virt/kvm/kvm_main.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index e4fab15d0d4b..17f07546d591 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -1790,12 +1790,15 @@ static void kvm_update_flags_memslot(struct kvm *kvm, kvm_activate_memslot(kvm, old, new); } -static int kvm_set_memslot(struct kvm *kvm, - struct kvm_internal_memory_region_list *batch) +/* + * Takes kvm->slots_arch_lock, and releases it only if + * invalid_slot allocation or kvm_prepare_memory_region failed. + */ +static int kvm_prepare_memslot(struct kvm *kvm, + struct kvm_internal_memory_region_list *batch) { struct kvm_memory_slot *invalid_slot; struct kvm_memory_slot *old = batch->old; - struct kvm_memory_slot *new = batch->new; enum kvm_mr_change change = batch->change; int r; @@ -1829,7 +1832,8 @@ static int kvm_set_memslot(struct kvm *kvm, * invalidation needs to be reverted. */ if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { - invalid_slot = kzalloc(sizeof(*invalid_slot), GFP_KERNEL_ACCOUNT); + invalid_slot = kzalloc(sizeof(*invalid_slot), + GFP_KERNEL_ACCOUNT); if (!invalid_slot) { mutex_unlock(&kvm->slots_arch_lock); return -ENOMEM; @@ -1847,13 +1851,24 @@ static int kvm_set_memslot(struct kvm *kvm, * release slots_arch_lock. */ if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) { + /* kvm_activate_memslot releases kvm->slots_arch_lock */ kvm_activate_memslot(kvm, invalid_slot, old); kfree(invalid_slot); } else { mutex_unlock(&kvm->slots_arch_lock); } - return r; } + return r; +} + +/* Must be called with kvm->slots_arch_lock held, but releases it. */ +static void kvm_finish_memslot(struct kvm *kvm, + struct kvm_internal_memory_region_list *batch) +{ + struct kvm_memory_slot *invalid_slot = batch->invalid; + struct kvm_memory_slot *old = batch->old; + struct kvm_memory_slot *new = batch->new; + enum kvm_mr_change change = batch->change; /* * For DELETE and MOVE, the working slot is now active as the INVALID @@ -1883,6 +1898,18 @@ static int kvm_set_memslot(struct kvm *kvm, * responsible for knowing that new->arch may be stale. */ kvm_commit_memory_region(kvm, batch); +} + +static int kvm_set_memslot(struct kvm *kvm, + struct kvm_internal_memory_region_list *batch) +{ + int r; + + r = kvm_prepare_memslot(kvm, batch); + if (r) + return r; + + kvm_finish_memslot(kvm, batch); return 0; } -- 2.31.1