Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3716096pxj; Tue, 11 May 2021 10:18:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcHSLQu/2xDzOno2ncxsWVpkdTk1ym4Sv3sQy6tBIKU9TQY3OGxR0IPuqkRQMmOFKnxHij X-Received: by 2002:adf:f946:: with SMTP id q6mr16759214wrr.283.1620753489757; Tue, 11 May 2021 10:18:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620753489; cv=none; d=google.com; s=arc-20160816; b=N24YiUthD5dq2CPBEWuTcDKyBOZCoZvaY2PbqhNzE0trdmqlE6tmM5xdeJIHVFb0HU wJjHTTkJ5wMrcMuQribMVq0cLbaew9dwHQbz34q4CG8f+d4nyi3GIGtSwPG9cLrXLjdx fxJpNXSmd3eAcjrCjs4gPy5FTwSkZOVWwG+LGLzRBFUT0lFqsQHDLZzX6mIA2V6av2tK TM2/hStdFGt3MplvgI90GTWpmiGviDhD0e3c40PdV6dIjSf5btXFNe3PMUU7hI4H0ORZ Bj5UKs3BHi1RQXRkr5344E1vDu/OSXCFF+qyiO/ocRjceO12PSk/Optno1xcYbgeVz3+ PakQ== 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=uuu17mHGt/KubHNv6B4Puz8KNLHKsb7X5PjppcGXfqw=; b=wgc1h4iA/ckTr0AxJxulvtBPX86PK6ZZJHM1YP6us/xEO9M1u4+z20yEvvFRk8klIw srvFFhr+IKRUuKGSYP1iCZRF/0twqUxspeWfRHO71cU6SEpxCvpIxV3mlr3rXb1o5U17 Y8Jo6vmiLLImtvYeqUg54P1C31bI7uN9FiBC0Avgre9hN1lgDQTJUwhX9HhHn1Nlt+45 nEiYaAIEYyEszwYgj9KrO+vw1Sw/UD3zpaIpEDY9zi+86uvTbuV5iCHS9w4rVs9C6PbJ +bVVowc3E0uXMbDaixxAsuPmozhYWhKgl5o4rauAMbE9agAVCXZPVOs/u7FMJ3iWcfZJ Ui9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=lLwwzQ4K; 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 b70si10265400wmd.195.2021.05.11.10.17.43; Tue, 11 May 2021 10:18:09 -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=lLwwzQ4K; 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 S231897AbhEKRRn (ORCPT + 99 others); Tue, 11 May 2021 13:17:43 -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 S231896AbhEKRRa (ORCPT ); Tue, 11 May 2021 13:17:30 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 604CCC06174A for ; Tue, 11 May 2021 10:16:22 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id 9-20020a056a000729b029025d0d3c2062so13268017pfm.1 for ; Tue, 11 May 2021 10:16:22 -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=uuu17mHGt/KubHNv6B4Puz8KNLHKsb7X5PjppcGXfqw=; b=lLwwzQ4KPOuibe32HUV2T1sr9kwP5TBQNFzot9w74MdDyGsF+uiGVnb8qWE+Hz0L08 Yy5l7q6D5aOucPbnlpg7EQmY63WEIly0gHlowClLZoJMFDnNPAtH8L9IqX5q0hAsYy/m K5ShsqUGAbWzxwUT/d1NVm5+2YILtS+08fmvwYCjy7E99MRTPPV1Ify7JSD/dAjOSikI ufrwCi6dhW5ebSptucOd4bYo/iJ5YaVyrgFDLk3mUDXVAi/Q1Hk+BdGWtaeFScgqFqGN kJKHMOhPn+EhQ3+a+oR3itJYc8kqZNntih+7gA3i49cZFlJRsjg/kr0g+GaUrXXTn2BD +WyA== 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=uuu17mHGt/KubHNv6B4Puz8KNLHKsb7X5PjppcGXfqw=; b=b96dtvCo4tSDgM3Lxeq9VDu6v6sFrT7I0+T3dP1Wgb0Yu+X9lqJ5I6WyA1m3Dylgzb 5JxOevrefxt72phMn2OdTzoN7IURYDtEoDLJq+6rjs/vKnZ8LuhHoerfpsqjwqEsPmuN IuJY39ZrueyhmzUrE6dHBQVdquH1tc/5ogC1byrL4mQwcRc07NG1mUQ0NTDYLIyWpimP 0Tb3lAEfSlShwxBg+c4yA6C083zn5/VXCs7zcK9BHnjn5GipCx5+DWw8wNP9tHDHMgIj w1jsLqL1MVAvCk3yIeuH370DhZ5zw7RlwWv3qq1xENPtpVvhFR1JNtVwUgWx7DJWSVjg Clbw== X-Gm-Message-State: AOAM533FVWPdBlM/oIZefxDU+TD2yF6LxVoiOI9frIXRvTxNPGEGCCsv TKGgN9n6I3vDVB2mXjOBLB+U35nbaPqXP3mmrXNYGJErM1lU7r69bWCDzQ4N75Fab87MCyxz+bX U1b5gwVUX3JKVQMr9v1rQmRcvzTnto0MQgjyTmlqNm19UEpA/KmyJn0KdfzA5IhY/B0TOOjqk X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:e050:3342:9ea6:6859]) (user=bgardon job=sendgmr) by 2002:a17:90a:4487:: with SMTP id t7mr298411pjg.1.1620753380897; Tue, 11 May 2021 10:16:20 -0700 (PDT) Date: Tue, 11 May 2021 10:16:05 -0700 In-Reply-To: <20210511171610.170160-1-bgardon@google.com> Message-Id: <20210511171610.170160-3-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 2/7] KVM: x86/mmu: Factor out allocating memslot rmap 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 Small refactor to facilitate allocating rmaps for all memslots at once. No functional change expected. Signed-off-by: Ben Gardon --- arch/x86/kvm/x86.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1e1f4f31e586..cc0440b5b35d 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10911,10 +10911,35 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) kvm_page_track_free_memslot(slot); } +static int memslot_rmap_alloc(struct kvm_memory_slot *slot, + unsigned long npages) +{ + int i; + + for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { + int lpages; + int level = i + 1; + + lpages = gfn_to_index(slot->base_gfn + npages - 1, + slot->base_gfn, level) + 1; + + slot->arch.rmap[i] = + kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), + GFP_KERNEL_ACCOUNT); + if (!slot->arch.rmap[i]) { + memslot_rmap_free(slot); + return -ENOMEM; + } + } + + return 0; +} + static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, unsigned long npages) { int i; + int r; /* * Clear out the previous array pointers for the KVM_MR_MOVE case. The @@ -10923,7 +10948,11 @@ static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, */ memset(&slot->arch, 0, sizeof(slot->arch)); - for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { + r = memslot_rmap_alloc(slot, npages); + if (r) + return r; + + for (i = 1; i < KVM_NR_PAGE_SIZES; ++i) { struct kvm_lpage_info *linfo; unsigned long ugfn; int lpages; @@ -10932,14 +10961,6 @@ static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, lpages = gfn_to_index(slot->base_gfn + npages - 1, slot->base_gfn, level) + 1; - slot->arch.rmap[i] = - kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), - GFP_KERNEL_ACCOUNT); - if (!slot->arch.rmap[i]) - goto out_free; - if (i == 0) - continue; - linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); if (!linfo) goto out_free; -- 2.31.1.607.g51e8a6a459-goog