Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp1686250pxj; Wed, 19 May 2021 11:27:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy2HliKhqEJwxvrWJnuZZ3xsvma9JxYkauYx+Hk5GENNI0Z54IabXxrA6uEpn/pGZvXEKWZ X-Received: by 2002:a5d:9d16:: with SMTP id j22mr858038ioj.92.1621448842762; Wed, 19 May 2021 11:27:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621448842; cv=none; d=google.com; s=arc-20160816; b=opTmX/Lmq3xzuo+coX7sVl4S+PhziUo8/r+BgP9Lgslx1pEIeHsqUOI75jph6MQ2ZX v9TWYGaGgML7dZbVyxEwhP5GfDAIMFuVC7PgzWZNhUQdBxFKFjmIuWwyfy+CR+s74eiP guPm/x7ha5vy7BCU8A5CARctWHxXVSnWVd7cPsg5CRi4r1fqZZlxWO0aGgEDcv+SPmeI 3tCxN9vOshXQPowyWuFyMffXqZ8e1U0vKCL/B1QmQr480tJzDH4cC2CExUn2LmtIhCJN QWoRf/hzzCviQhsIJoY3ZRlzFQN/IEH/0l8nml10vgbfmG80HKLmVPLhFRl2DTJrSIji ENtg== 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=rAGxH7RBjxWI4eFtU4uy5sDnRTRUjNhxYK0eIVMGyVI=; b=Dxs5/CTEHaHuO+Izr+bz6MVawIUDOp3Iy8q/E3kPFlfseCVuE6MBf5wKEXYe7OQvxn 23BadaaViYb6KWjsYmU/8O3aJq8lp5Y+gDC0ka3Z0Nn4UAkw+YsBajgpaGTh2C7wYMmR 0/40Ood7QwPs/CjESbVeqEIGCkpGyCC9mek2NDIPsqit2+yFMf/4W2qHNOwN7BgsYRMJ UvLiu0wElePyRXm504jx2cdY+VbyVevah+79Rr3stmFCYT+hTsPCz4R5HJfCYZMDXIYZ SHnUyvC9NguBSFoPLvdQ3NVqp+N2IQJzArZwsBEY57GtyrqSuccpS1mr/CIc/XZhu33B 70AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=cn9JrqjD; 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 p18si20055jam.69.2021.05.19.11.27.10; Wed, 19 May 2021 11:27: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=cn9JrqjD; 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 S1351185AbhERRfu (ORCPT + 99 others); Tue, 18 May 2021 13:35:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351147AbhERRfk (ORCPT ); Tue, 18 May 2021 13:35:40 -0400 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 519BCC0613ED for ; Tue, 18 May 2021 10:34:22 -0700 (PDT) Received: by mail-pg1-x549.google.com with SMTP id 15-20020a630c4f0000b029021a6da9af28so6765536pgm.22 for ; Tue, 18 May 2021 10:34: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=rAGxH7RBjxWI4eFtU4uy5sDnRTRUjNhxYK0eIVMGyVI=; b=cn9JrqjDkc1ruNg7PlqXWsUWgYudQBTGNMIOFUHAQXPJN5ofWlrtvcbSc+lhrBY5Hn GrowzrdOWmHfq2mco/UyT+uFkXSMGCKNk16WLuNzcVF1h/rP4i7bY7fGughzZedLMwuL qxFlWxwoEQGlmFnB9fWRKmYXLl/D7+LC5weN4zSg9zxCtQbAdFyNR6EbD6SXnlp+/uNV 2E11ctl5Blh+RWIGm6W9EoO15M73VxHuvI0KUhG1bxI0PKSGRPm4mj/9AZIOWW+5X8CD Pbzu/7eyhAv3jkcZmjV1YxMbreaDFcS9609X60T/5HPAIlOvk47GYut7jH1zx9Lk73E3 sYug== 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=rAGxH7RBjxWI4eFtU4uy5sDnRTRUjNhxYK0eIVMGyVI=; b=Hdeleft0VTsaaJb0DNwWPnu8nOd7ntO0f54RqbYANLXqeVpQwys1s///Tf/wC9pJO/ c7s5Jh0MYb9fxDu1k3/Ps/TJ23ODWyDG0B9MGAHnsd4uR28NMMn2lVMnPui1jWSNSFiB suDlMWe4JJKQbNWMguslfnHsD4JVteGi5+x3HzOVZRqEnF/Phwh2MH9Zwcb3s5jQpu70 NBXEYOaQQb92Q0HVRqaFaY9GNGZmHZldt6M1KCNVaXnbOOzWe4HJt1GJAvWYKxhxbpEm OVouYYCqYR6SkCr5mEUU2k5HP2trsaxBpXuEkrIyKNYedxwt8WZayS4V1kkDLnpKq1VJ alyw== X-Gm-Message-State: AOAM53026kEFXiZHP4ZNTy/KsVb5CGjgHruiieIzTZdzJLmaOIcTWxBD f9KtO3w/s6xa+yideWst8OYny6jO6oYUofaa60+yVmcy8zJdGgnglJWVB0P25t8r8LxUncxzGMs hZ5Rln70EsMAc8F+qFMEscXyiHdNmfymvqAmFRZ+laB51nwVpa/waFXDvYPyCpSi7uTK8kUOr X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:2715:2de:868e:9db7]) (user=bgardon job=sendgmr) by 2002:a17:902:ea10:b029:ef:d2:16c2 with SMTP id s16-20020a170902ea10b02900ef00d216c2mr5794298plg.9.1621359261715; Tue, 18 May 2021 10:34:21 -0700 (PDT) Date: Tue, 18 May 2021 10:34:09 -0700 In-Reply-To: <20210518173414.450044-1-bgardon@google.com> Message-Id: <20210518173414.450044-3-bgardon@google.com> Mime-Version: 1.0 References: <20210518173414.450044-1-bgardon@google.com> X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog Subject: [PATCH v5 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 | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 11908beae58b..4b3d53c5fc76 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10920,10 +10920,31 @@ 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) +{ + const int sz = sizeof(*slot->arch.rmap[0]); + int i; + + for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { + int level = i + 1; + int lpages = gfn_to_index(slot->base_gfn + npages - 1, + slot->base_gfn, level) + 1; + + slot->arch.rmap[i] = kvcalloc(lpages, sz, 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 i, r; /* * Clear out the previous array pointers for the KVM_MR_MOVE case. The @@ -10932,7 +10953,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; @@ -10941,14 +10966,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.751.gd2f1c929bd-goog