Received: by 2002:a05:6a10:a841:0:0:0:0 with SMTP id d1csp1772138pxy; Thu, 29 Apr 2021 14:21:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzEGwMJscm0sbPUAztz5vx912hB7jpyGZMjDcjHHTviB0WFbVady3+GLM3zmNzOFqHxZp5w X-Received: by 2002:a17:906:b0cd:: with SMTP id bk13mr570329ejb.184.1619731259817; Thu, 29 Apr 2021 14:20:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1619731259; cv=none; d=google.com; s=arc-20160816; b=nYGLQZ955hvbBvciOVVqTSFs+4CQ7RoazqM2KV0Oo8LU8TuPuDOTOKjM/tosql/TBs lDbaViTaxECyAnS1nLkOpzBBTH9u9GaQsNBoQm1y5lsaxf8XD2dSWlkBP9+6VlnSiyMe spdiQG4Q2Ke8CeIrYH3AZsj4TXZnioQ/4nRx8vImtvy9CUChlPYBQteV/4+j5a6ajLmI r03yCACuyKLcqafTFfDbyDRHOGsPoonfAIU+OlWhfVgoqjeSrWdE28T1Od3VS6TjKnJC ghRwMwYpB12ZMXqNgCvObVyrpPA/aEjjupUZQ5Jz3TkheVZQFIClHCOdaUAQZ59v0e7L XrJA== 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=V/i9JLKn2bODnum9/FR/aKnhuikAVXFu3BpBHLyrfLg=; b=wqB9XuO01nROTjbkW4ridnQu2a3gKJLEO+wylWrM9Rw6aSAGRN5OGyQ9KW9PVYM6mB baFlriFRINJYNcvzxrx8orkvLWx5l7zStOQLsknLe5l0nNfb+w4BO3f379S8dfOR+iuX LKSHsSdy09f5JMARogHHYtqbJO0+2NATmtODNF7/JFKcw5HGtTpu4s1CGR9yzcYfDOoh 6tOG6XK2Fwp8y5hlkPyfWCQIHcSm3bGd5tq7k1IZvrw7ztRdXml+pjU/tJmCSotdehM/ 736CVEb5O3v08MKBO5ZITeZoqvWam/m6/v4MwekdNnzzgnrVuQmLgjA2W3Vp/LPhsiKT MfXQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=I9jyJqDz; 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 i17si1194562ejo.105.2021.04.29.14.20.32; Thu, 29 Apr 2021 14:20:59 -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=I9jyJqDz; 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 S237482AbhD2VTu (ORCPT + 99 others); Thu, 29 Apr 2021 17:19:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237452AbhD2VTp (ORCPT ); Thu, 29 Apr 2021 17:19:45 -0400 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A031AC061342 for ; Thu, 29 Apr 2021 14:18:58 -0700 (PDT) Received: by mail-qt1-x84a.google.com with SMTP id s4-20020ac85cc40000b02901b59d9c0986so25302100qta.19 for ; Thu, 29 Apr 2021 14:18:58 -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=V/i9JLKn2bODnum9/FR/aKnhuikAVXFu3BpBHLyrfLg=; b=I9jyJqDzeQ9s48jJcFT9qziZLErEBCH11Qi47JBM9REY7FJl3TRaDRh686S2S0LIZm 0ryfFDQdWCmESL7K3WFI2F8Bvvfqc872VR+LFCip039aSna3fcOw+08XHdeCV5iAruU7 Mm/r1KmyO438ITRn1YuzUvLJlFzIOM9MxiyFIdxKJTcCQ+evKkQxTpNMjbElj6Whi0Ss g7/EoWAAIwU43ifUhHvg0oN8RgAuH6IeaBmQGDY5CHemIbOhzGQ2X8ZdTq1fRbQuEhHJ nuWv/On0GGu4QyS5FY/+NL9Znd6yD44zp05zyNrgV6O9DPJrN3k11MoFICA32im5ACKj EclQ== 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=V/i9JLKn2bODnum9/FR/aKnhuikAVXFu3BpBHLyrfLg=; b=kH48IywCKoMknwuc4mfIHEhNzkYyD1/a27y9G5AfkoLLgourq1P2IhOP6JZ0JU07l7 fzsber/LyWGBlAWvZrjkRqF9HQiaBfvZSZUNHvdTPgqkKb2J5mYsK+1cWsKdkeFL4caB /KLUGnfqkNHboQMz/7Z/ZvgrFa3WCpUh2gG3QT4ZObFX3EYOoNm9J25qBleGemAgOgtc f8KoLQbte6Ku1w/77q73Y8ArosNNN9ia3A0K0/HZYw3vnbRLd1O/AbTKSceocGy82hyj 3MlObi5l4k0UMqlJ6b6jBygcZc3lOKFaxn17dFf+WYcKrFgguCj7dYlcgF6RCIPof8B3 B0mQ== X-Gm-Message-State: AOAM5327H8z+FHB9MYBaISrZapnmgBcjnRbfYzndtoBJJ8gjVxLK90Jg Tfwls/l97aqXeNYccSG0gV9nObLqqjhX/yhXz/NcystundCQcwRlQa9Dl06JYqVdYbdx3UbmdxW sXcxSJYehWtU1v335QI7v0uosQEjbE0HPtj8dW4kZE1NYEOS+Cy0Mcs1mSxBMYddx4GEjuCc4 X-Received: from bgardon.sea.corp.google.com ([2620:15c:100:202:1a18:9719:a02:56fb]) (user=bgardon job=sendgmr) by 2002:ad4:518a:: with SMTP id b10mr1895601qvp.19.1619731137789; Thu, 29 Apr 2021 14:18:57 -0700 (PDT) Date: Thu, 29 Apr 2021 14:18:30 -0700 In-Reply-To: <20210429211833.3361994-1-bgardon@google.com> Message-Id: <20210429211833.3361994-5-bgardon@google.com> Mime-Version: 1.0 References: <20210429211833.3361994-1-bgardon@google.com> X-Mailer: git-send-email 2.31.1.527.g47e6f16901-goog Subject: [PATCH v2 4/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 , Junaid Shahid , Jim Mattson , Yulei Zhang , Wanpeng Li , Vitaly Kuznetsov , Xiao Guangrong , 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 | 41 ++++++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 5bcf07465c47..fc32a7dbe4c4 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10842,10 +10842,37 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) kvm_page_track_free_memslot(slot); } +static int alloc_memslot_rmap(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]) + goto out_free; + } + + return 0; + +out_free: + free_memslot_rmap(slot); + return -ENOMEM; +} + 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 @@ -10854,7 +10881,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 = alloc_memslot_rmap(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; @@ -10863,14 +10894,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.527.g47e6f16901-goog