From patchwork Wed Aug 22 13:57:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 10572975 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id ACE83139B for ; Wed, 22 Aug 2018 13:57:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C431295E0 for ; Wed, 22 Aug 2018 13:57:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 8FB752A870; Wed, 22 Aug 2018 13:57:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 33207295E0 for ; Wed, 22 Aug 2018 13:57:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729002AbeHVRWX (ORCPT ); Wed, 22 Aug 2018 13:22:23 -0400 Received: from mail-pf1-f194.google.com ([209.85.210.194]:47074 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728567AbeHVRWX (ORCPT ); Wed, 22 Aug 2018 13:22:23 -0400 Received: by mail-pf1-f194.google.com with SMTP id u24-v6so1004392pfn.13 for ; Wed, 22 Aug 2018 06:57:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=4ykthOlllU4NA0AXbUhkG8dzhjY/XGAgosG0M5KABC4=; b=KiyEkvk95U9xkkoqcO/SYu7GzaedQd9LPi5sEMaVobjc89YHhNZ3YcWQbKEQQRN7nJ 96l053CnJkJATIQdykoJ7air6PatVDEeq2jP7b7Se1PO+jqX/x8vSifpZ+xhkI6zj2DZ OMjc/W3wusOI+lcaf+36n7O/5BPDIP3atCWAIMYaoGRRYPBw5WvJx+JCpzhxzdWXbi6y rEZPotN+2tuKW5BvyKLFucxsnNGt6An9N6cd3/lc7EH3g38Flis9eZAQcpt5NNtLaJMx BMK4wKYp8Q2cmHEtERr3/sVBbyyjilUVphJmfJ1HKXNh9j1/IFotbzzpNk0w7TDkoOA0 itnw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=4ykthOlllU4NA0AXbUhkG8dzhjY/XGAgosG0M5KABC4=; b=NtfjpkYwwnFlNwhnCWbZsaieugqVNXiNDgxd77I/XkjsrXfZV1dKuu4zxc6fwINZb8 0EQMUjwKN0CkYfIUVAZ4bFchT6j0ZNNUCjDTPc47izFVIc7Q1u0+5jWcRqEU3bexFV9V ApFKhZkpSToqP3NfzQtS3DplQ+eUi/WWquFDcCSXynLYOWIMhUUWX91UGfDceLokdpWL XrS1H3JujYoZe5pZJS8KGw0FsnxP67EKeYpD3q2uFX9kCmqDusdukSM8OI0i/KCimGlP o6YXgjhM5BxzK2OT+qXJRXz3duFkpzk/TmbLjqeVStVaAjrpvvH52V3fUOjZ15lbTUqv a8zg== X-Gm-Message-State: AOUpUlGqyG9Gb1VZhlijd40qhyDmB3ckcgIwcBwK1YPVreP7E7X1Ews8 NS2+ABpDoSPn3KKebdYwe9U/IsqI1xI= X-Google-Smtp-Source: AA+uWPyxs0pA8G+N+pbeizfW8iziZ0jB32uZhZlO5dNFMtaFs0KDwpSporII0ThOzXkr96OJXETgsQ== X-Received: by 2002:a63:7557:: with SMTP id f23-v6mr16084715pgn.135.1534946241596; Wed, 22 Aug 2018 06:57:21 -0700 (PDT) Received: from localhost ([185.92.221.13]) by smtp.gmail.com with ESMTPSA id 3-v6sm2837369pfq.10.2018.08.22.06.57.20 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 22 Aug 2018 06:57:20 -0700 (PDT) From: Wei Yang To: pbonzini@redhat.com, rkrcmar@redhat.com, konrad.wilk@oracle.com Cc: kvm@vger.kernel.org, Wei Yang Subject: [PATCH v2] KVM: leverage change to adjust slots->used_slots in update_memslots() Date: Wed, 22 Aug 2018 21:57:11 +0800 Message-Id: <20180822135711.7389-1-richard.weiyang@gmail.com> X-Mailer: git-send-email 2.15.1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP update_memslots() is only called by __kvm_set_memory_region(), in which "change" is calculated and indicates how to adjust slots->used_slots * increase by one if it is KVM_MR_CREATE * decrease by one if it is KVM_MR_DELETE * not change for others This patch adjusts slots->used_slots in update_memslots() based on "change" value instead of re-calculate those states again. Signed-off-by: Wei Yang --- v2: use switch on change instead of encode the adjustment in it. add warning in case the status is not correct --- virt/kvm/kvm_main.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0df592c4f09f..692885c1457f 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -802,20 +802,25 @@ static int kvm_create_dirty_bitmap(struct kvm_memory_slot *memslot) * sorted array and known changed memslot position. */ static void update_memslots(struct kvm_memslots *slots, - struct kvm_memory_slot *new) + struct kvm_memory_slot *new, + enum kvm_mr_change change) { int id = new->id; int i = slots->id_to_index[id]; struct kvm_memory_slot *mslots = slots->memslots; WARN_ON(mslots[i].id != id); - if (!new->npages) { - WARN_ON(!mslots[i].npages); - if (mslots[i].npages) - slots->used_slots--; - } else { - if (!mslots[i].npages) - slots->used_slots++; + switch (change) { + case KVM_MR_CREATE: + slots->used_slots++; + WARN_ON(mslots[i].npages || !new->npages); + break; + case KVM_MR_DELETE: + slots->used_slots--; + WARN_ON(new->npages || !mslots[i].npages); + break; + default: + break; } while (i < KVM_MEM_SLOTS_NUM - 1 && @@ -1051,7 +1056,7 @@ int __kvm_set_memory_region(struct kvm *kvm, memset(&new.arch, 0, sizeof(new.arch)); } - update_memslots(slots, &new); + update_memslots(slots, &new, change); old_memslots = install_new_memslots(kvm, as_id, slots); kvm_arch_commit_memory_region(kvm, mem, &old, &new, change);