From patchwork Sun Aug 19 00:17:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Yang X-Patchwork-Id: 10569655 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 3C16B109C for ; Sun, 19 Aug 2018 00:18:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2A0A229A9E for ; Sun, 19 Aug 2018 00:18:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0767629AA8; Sun, 19 Aug 2018 00:18:06 +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 9867729A9E for ; Sun, 19 Aug 2018 00:18:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726337AbeHSD1d (ORCPT ); Sat, 18 Aug 2018 23:27:33 -0400 Received: from mail-pl0-f66.google.com ([209.85.160.66]:36856 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726255AbeHSD1c (ORCPT ); Sat, 18 Aug 2018 23:27:32 -0400 Received: by mail-pl0-f66.google.com with SMTP id e11-v6so5363555plb.3 for ; Sat, 18 Aug 2018 17:18:03 -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=inr7FeXthyhc/p0BWMy62+7bg3cc8Kr5rmiCjW3Jyro=; b=iqGeyTLu7r1Fd8hmxZ1WlJWGafSixfaOXQQ0Wei0d6Bo/quX1IOCwon4xyQTLJks5T XoGvn0iv7Sf7m+qZNIkBVx+ugonBIlbu8I+zKvkjLB8q74N8yKOhGdMdQf/QAiEzyuRP KwaxePfhUASAhdue/OqReiu5vTmvHdLIiHFeX9Bd6SQZrgCbilsRBTFkeVwH3bZGJQu6 BqG/bFjTiUSbYYIep5ji0oSkoGQSwCqYkg5qnQBB7XV+frXLpM+UZ0T+k+2N3Xm4lz74 DtBxFINk1CjaUe8Ud/387tnShYphdlvfw3pXbYN8tcicjL0TzIOnxBak4f219+4WPVNN Gz9w== 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=inr7FeXthyhc/p0BWMy62+7bg3cc8Kr5rmiCjW3Jyro=; b=S3XB6FsR3A5R4Iqf4bFE6ej6L3fpKUuHtW7FmaywqvHv+O1lDv37kocp/RHjKMN7sf VQUEDLGanXeDsyRJ7v28MIl00cbH6zKo1btI8f8Xkc9JPesEs1mIZsWVdipKHXZ1j8VN hBK9qui8zAuNWDC8adJvsb7m5VrLznqU6fO1aGwAWXTaTcZxMQektMdzOCbxr/HeBQv1 NLgLIZGWiCZEl9AWUSAwYXzJRp/t1BJAxNdlwb+qR/QvvBsZk7pKaqmf3Dzpjxp1UvWC WboxOslCAO7lrRnQukuP5CHCf4pmYOumdaxCutbnoVoBPw9IA37r0tCgRFZ7vWbPy+pS alQA== X-Gm-Message-State: AOUpUlHvWXSpJDpMY0JspuV5Sba1T7q6xWlOB8FNYNLEknPryCVQ5+ME HWXNo0rbDg2R0N81IrbmXrw= X-Google-Smtp-Source: AA+uWPywRTT9i2UFkZcDSXswyCbs4dnCglxq/ttJbPBlZ/RqqaQGpmsi6G+1v9ni5Mf7Ag6U0uYaLw== X-Received: by 2002:a17:902:5582:: with SMTP id g2-v6mr39560864pli.328.1534637882721; Sat, 18 Aug 2018 17:18:02 -0700 (PDT) Received: from localhost (65.49.233.5.16clouds.com. [65.49.233.5]) by smtp.gmail.com with ESMTPSA id p19-v6sm8788898pgh.60.2018.08.18.17.18.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 18 Aug 2018 17:18:02 -0700 (PDT) From: Wei Yang To: pbonzini@redhat.com, rkrcmar@redhat.com Cc: kvm@vger.kernel.org, Wei Yang Subject: [PATCH 1/2] kvm: leverage change to adjust slots->used_slots in update_memslots() Date: Sun, 19 Aug 2018 08:17:45 +0800 Message-Id: <20180819001746.21586-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 to indicate the following behavior. With this information, it is not necessary to do the calculation again in update_memslots(). By encoding the number of slot adjustment in the lowest byte of change, the slots->used_slots adjustment is accomplished by one addition. Signed-off-by: Wei Yang --- include/linux/kvm_host.h | 11 +++++++---- virt/kvm/kvm_main.c | 14 ++++---------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 4ee7bc548a83..e0ae6cf82c0d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -610,12 +610,15 @@ id_to_memslot(struct kvm_memslots *slots, int id) * * Since flags can be changed by some of these operations, the following * differentiation is the best we can do for __kvm_set_memory_region(): + * + * Encode the number of slot adjustment in the lowest byte, which will + * be used in update_memslots(). */ enum kvm_mr_change { - KVM_MR_CREATE, - KVM_MR_DELETE, - KVM_MR_MOVE, - KVM_MR_FLAGS_ONLY, + KVM_MR_CREATE = 0x0001, + KVM_MR_DELETE = 0x00ff, + KVM_MR_MOVE = 0x0000, + KVM_MR_FLAGS_ONLY = 0x0100, }; int kvm_set_memory_region(struct kvm *kvm, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 3d233ebfbee9..5c1911790f25 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -801,21 +801,15 @@ 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++; - } + slots->used_slots += (char)change; while (i < KVM_MEM_SLOTS_NUM - 1 && new->base_gfn <= mslots[i + 1].base_gfn) { @@ -1050,7 +1044,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);