Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3719022pxj; Tue, 11 May 2021 10:22:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzzoQZqLcm05l+uKVk89c48IlpgDw8pkmwqjlf8jCqKsCa4M0gf1Rphyxte0wHkmXZh2kL+ X-Received: by 2002:a05:6512:2256:: with SMTP id i22mr21837014lfu.641.1620753742096; Tue, 11 May 2021 10:22:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620753742; cv=none; d=google.com; s=arc-20160816; b=0M2emb6oljP3tBUYlx6gn5qSQZt9Wb+fnuhs/MUJ1NypHoSL7mRJ9XGf7CFq7hHLOL oCbz3VcfvyqMJiEi0QGxdwa3j3KyxApDYVgZx//UMvD1Fi/Rn341EyiwbxSHX9MxzA2C /r23RWSTksu8ReliAxfBeVUfYMxsNEKH3oohx20sY2XtitZS8yLguOzZToGUiwJ0KuCn ydivuwrQ4qcoOLj0GOIfF8cG6mjgCrNXraEpQ0jh76a1Yp2YxXPHjhlL5HmfjX6EjH0P kR9lgoYmXVct2265UeLGkTWR7Vz0NWpXCTT9obSk0AqL6M1MTCk3MUyiJTj7ezBXokEu fzrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=Ngmzrc1OFlZl8Ai9tz2syHudz9d0nSq8e5MZecARPTE=; b=d8bk9f2UGka9UW90/YSWX9Uhg+yfsokoCMHh/+t5cwkW4aL4BZP4MBNPORqSOi5BKb 8Uoul9c0ohX7Q6sfG8F0cXhDacwtVxaMAZ/znNkkrQOQff3Qp9I1Sz9evMXEzm07jVst GTN1dPFEYvmXUOYZ9I5B1dTH1Y2cGIM27y5/wQaAwzVGQxT0OhOCywfjAjQ5k5T6WY30 8PodBhMf7QqiMnPHsT0wKsZaHKMVBs5eqYC4CCNGAdqM4Wa6jR9SAJbX8p7/5PUWbU1D v3oSDEjz8zOVxmFU807KbxtKXY98VbLjc1wt4JwH4m7vLxdfm7jH6XOoAElAzSoLQdph trqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=n3Guhyxd; 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 z16si21026494lfd.479.2021.05.11.10.21.49; Tue, 11 May 2021 10:22:22 -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=20161025 header.b=n3Guhyxd; 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 S232063AbhEKRRz (ORCPT + 99 others); Tue, 11 May 2021 13:17:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231944AbhEKRRn (ORCPT ); Tue, 11 May 2021 13:17:43 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 904B9C061346 for ; Tue, 11 May 2021 10:16:29 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id y18-20020a0cd9920000b02901c32e3e18f7so16075375qvj.15 for ; Tue, 11 May 2021 10:16:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=Ngmzrc1OFlZl8Ai9tz2syHudz9d0nSq8e5MZecARPTE=; b=n3Guhyxdq4cgqPfyN+sJwX7soNr8JrbxP9j8x1tRS6JFnDlxPGztO9hSVvPZ311zv0 fbABmRwsqx5XDc232fireb8CPH1YXrzIeq5mEih2hx4UR2uKq9PkkESo89MWnCa5tMPg eBVqvWL94S89OCWn5/valg14OzblkbA/R0cDIwALpDEqpC3IjWIXeIfIQz6WDcVHYtUQ 5rFn3a5PZy2j1G6Vk508PaYzWZ9PFs8uCJsU+V5iaFnckYJncg1govjNlTTmUJcrz5jQ Ix1N2hHwZ2ZyrvSMB52w42tCGoIwcAVNRu4r2reo+ItBsB3jqv/sxC4W4EdoZYRRbT+y aspA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Ngmzrc1OFlZl8Ai9tz2syHudz9d0nSq8e5MZecARPTE=; b=fVXH8LklahSgYtOYrC6QBKAC8k3RSnWSzFHP8u1MLOMkU2Pj4VdEsJHpxQsvB1RyCt tSzzFE3aRoye7GHFcxJ3RXVidsQdcmwDOj/dutnNmi13mrb2fsilYZ/IXV8s8RkPzNON kMiEztmufTuJkQYLsMIYflSRz9drwjESMWmGEWsnjmhgYnsVBtRyMSyVdRoBQHW7R5vv Ci2xj5ZHXbHOkdfdplHWkAO+250F3dfhVdFttBcjXKrchA1tPmp6K+uSPlaGXWJGrMCQ r8bRUMAU9l+RSXT1RGTygF+ANpeSKdgHXEZOrtDi0HNlMHHkFShpP5DWPCpwN6S67anN D17A== X-Gm-Message-State: AOAM530LPBtFwcGc/efM1D0LuFwHjjL2SIJUmTWgDsqlOsEbbgkzBRhR kfBqeuj0r2Kh2wr7iocMw95XBgQs8tSEbKqSXxRe7A5gf4DteuUa9XDd4nlDKqkWnJ+pdOpjBOG XBCoHfOz7xbW2K8zXEJKh9Kc5jTcjQl4PtjvqaTrDhOb8PtbcxJ0bsw509YstxGZlK80mBcu8 X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:e050:3342:9ea6:6859]) (user=bgardon job=sendgmr) by 2002:ad4:5490:: with SMTP id q16mr14227216qvy.40.1620753388262; Tue, 11 May 2021 10:16:28 -0700 (PDT) Date: Tue, 11 May 2021 10:16:08 -0700 In-Reply-To: <20210511171610.170160-1-bgardon@google.com> Message-Id: <20210511171610.170160-6-bgardon@google.com> Mime-Version: 1.0 References: <20210511171610.170160-1-bgardon@google.com> X-Mailer: git-send-email 2.31.1.607.g51e8a6a459-goog Subject: [PATCH v4 5/7] KVM: x86/mmu: Add a field to control memslot rmap allocation From: Ben Gardon To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Paolo Bonzini , Peter Xu , Sean Christopherson , Peter Shier , Yulei Zhang , Wanpeng Li , Xiao Guangrong , Kai Huang , Keqian Zhu , David Hildenbrand , Ben Gardon Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a field to control whether new memslots should have rmaps allocated for them. As of this change, it's not safe to skip allocating rmaps, so the field is always set to allocate rmaps. Future changes will make it safe to operate without rmaps, using the TDP MMU. Then further changes will allow the rmaps to be allocated lazily when needed for nested oprtation. No functional change expected. Reviewed-by: David Hildenbrand Signed-off-by: Ben Gardon --- arch/x86/include/asm/kvm_host.h | 6 ++++++ arch/x86/kvm/mmu/mmu.c | 2 ++ arch/x86/kvm/x86.c | 13 ++++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 55efbacfc244..fc75ed49bfee 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1124,6 +1124,12 @@ struct kvm_arch { */ spinlock_t tdp_mmu_pages_lock; #endif /* CONFIG_X86_64 */ + + /* + * If set, rmaps have been allocated for all memslots and should be + * allocated for any newly created or modified memslots. + */ + bool memslots_have_rmaps; }; struct kvm_vm_stat { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 0144c40d09c7..f059f2e8c6fe 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5469,6 +5469,8 @@ void kvm_mmu_init_vm(struct kvm *kvm) kvm_mmu_init_tdp_mmu(kvm); + kvm->arch.memslots_have_rmaps = true; + node->track_write = kvm_mmu_pte_write; node->track_flush_slot = kvm_mmu_invalidate_zap_pages_in_memslot; kvm_page_track_register_notifier(kvm, node); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index cc0440b5b35d..03b6bcff2a53 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10935,7 +10935,8 @@ static int memslot_rmap_alloc(struct kvm_memory_slot *slot, return 0; } -static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, +static int kvm_alloc_memslot_metadata(struct kvm *kvm, + struct kvm_memory_slot *slot, unsigned long npages) { int i; @@ -10948,9 +10949,11 @@ static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, */ memset(&slot->arch, 0, sizeof(slot->arch)); - r = memslot_rmap_alloc(slot, npages); - if (r) - return r; + if (kvm->arch.memslots_have_rmaps) { + r = memslot_rmap_alloc(slot, npages); + if (r) + return r; + } for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) { struct kvm_lpage_info *linfo; @@ -11021,7 +11024,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, enum kvm_mr_change change) { if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) - return kvm_alloc_memslot_metadata(memslot, + return kvm_alloc_memslot_metadata(kvm, memslot, mem->memory_size >> PAGE_SHIFT); return 0; } -- 2.31.1.607.g51e8a6a459-goog