Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3764474pxj; Tue, 11 May 2021 11:19:32 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcjl/J61d52t7y5FeJiDvOODohahAmiNViDyB2Tfyj/hvqOY3ATTSjt2Q7qbOslq8HezLy X-Received: by 2002:a19:4f02:: with SMTP id d2mr20191959lfb.85.1620757171954; Tue, 11 May 2021 11:19:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620757171; cv=none; d=google.com; s=arc-20160816; b=xrGyRl1RB4OHjXPzvPqV4S6Zf1sAce8tvXztTFO7Y2VpJhDyeu3BlVEHTuXnu0yWa/ jpOezEIt6GAUqsO2TcFEZSYXq5MTqShrZPObjsM4iSBEyPEwe6JM6/EiuXwfGNwM1uCy NxtDVAdYgwJ78Hg1g3ZQr/E0s/rQ9Wgho2yl0KjT0usT8fWtJ25kjoTOr/ExZyxE3MY0 kMclsZzXZW9lV7jMmGI1L95TvC2FDZ4hLut0KRn2r7BPNbb5naKLh2qpGSlcL7jDaBrF BiDS/SSZnfNW8HBrWK4lZvV8F6l2KzwiXOZVmyf9fhGxqLojdlG64MWG7liTkJfD0eRr Ttqg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=vyc4dubnSLD+2BW6PH5UxSFi8txm49CBtKkbTHFdSjc=; b=PvEPu5iYkwSL/Z0U4j77Tb9z18NQ3gn0l41yLLpDxmE6jH7BhAuvZ35WKkPRQCQVrT smd4wFMvOHK+F+dfKWzWKn/4fUHIdfKB681+LP5IN0B9PCZG1nrcBfiAmpmGtoWIk1So 7myGuSPhzu5oUFSwinVNeR8xQQvefa+cu6XwtJK8jZtEOToK0QOyGye6uQLh5DHpjJxH fqj+o8GjPRtJT5Mo9bWWM1Za5olMnTJ7oieWbiYR4iHLLH67QpeyjCLHruFlSP9wG7E+ sCclzH1xhOT+GR1A3M70WjB3BBzMSHroMjKI4Lqa0P1JzLWjDcp5e1RmISit661kdmia IQzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=jQCVdIe9; 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 u9si24582608ljg.178.2021.05.11.11.19.00; Tue, 11 May 2021 11:19:31 -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=jQCVdIe9; 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 S231991AbhEKSS2 (ORCPT + 99 others); Tue, 11 May 2021 14:18:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231793AbhEKSS1 (ORCPT ); Tue, 11 May 2021 14:18:27 -0400 Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1CDECC06174A for ; Tue, 11 May 2021 11:17:21 -0700 (PDT) Received: by mail-il1-x12e.google.com with SMTP id s9so699792ilp.9 for ; Tue, 11 May 2021 11:17:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vyc4dubnSLD+2BW6PH5UxSFi8txm49CBtKkbTHFdSjc=; b=jQCVdIe9pgUV9koKXqaLjKWuIiMn1YNXIXTPdThrhRpk6e34GBJjPxJN9uMPajlmzN ZD7usNNeDjrGfq0yOPFxxTgRyRzcchcB4Z9tTo5GQj4ayixk1+x09iCZk4rGjDIbInAz kHL0bE03YePZNAJbRcRJEoQSBObgx62vHHqhh1JRfAgzi5ob0T5bEP4VSnxKPWKRgeuP P77IKBOh5RYA4ILQny03ShkO38Gl+18JHF/T3BFoTfXL3igPNsKDnVhRGGzXJvBMjN+f A//hOUXmPr1FlwzLJiR8XIDPJ948gVdA2qk4c7My1JA/MQ+V8TplWjmKanv4LtPVInU6 QCvg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vyc4dubnSLD+2BW6PH5UxSFi8txm49CBtKkbTHFdSjc=; b=BkFekdhYTUc31Q7vEz1kYQ7Mf/A1lnE77IqRtpVaT/NTLzhA1Cq6hJgorNqccCky2a sMEZ3jO5ANa8ML3YOsTEHnYltbZBpHWmvksJRs0VIYNsx4m5G95eF5uvWEQhMKIV2NHQ WXQ+UIW5XPvTmGZwjfcqaZs7lhvBSO17fLMonLyKnm6fuwfkaqMDWEHuGOS1rplowE0p IhuHy7/ygx9eCvZIgNfmBlozTWnEPG5RsVw3i269KQvFb1SB1ouASdEBS8iABCIII7eg faDJvoIUmvACLb+/aqkNIPO66MZv3QVvo6DPIPjc4m4eovATzrNbsJPYtEuOV2Ly+J8U +/Pw== X-Gm-Message-State: AOAM531AKb/DC1d/Tp74iPAYvGvDYEt/SGSqdrM92eQFMeXpVKK1dory 7fsEM2/Xj9tHyjA+UQxwrpgkahceR8H3ypSmhjLRbA== X-Received: by 2002:a05:6e02:dca:: with SMTP id l10mr27474797ilj.203.1620757040285; Tue, 11 May 2021 11:17:20 -0700 (PDT) MIME-Version: 1.0 References: <20210511171610.170160-1-bgardon@google.com> <20210511171610.170160-3-bgardon@google.com> In-Reply-To: From: Ben Gardon Date: Tue, 11 May 2021 11:17:09 -0700 Message-ID: Subject: Re: [PATCH v4 2/7] KVM: x86/mmu: Factor out allocating memslot rmap To: Sean Christopherson Cc: LKML , kvm , Paolo Bonzini , Peter Xu , Peter Shier , Yulei Zhang , Wanpeng Li , Xiao Guangrong , Kai Huang , Keqian Zhu , David Hildenbrand Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 11, 2021 at 10:56 AM Sean Christopherson wrote: > > On Tue, May 11, 2021, Ben Gardon wrote: > > 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; > > Might as well assign lpages at its declaration, i.e. > > int lpages = gfn_to_index(slot->base_gfn + npages - 1, > slot->base_gfn, level) + 1; I'll do this if I end up sending out a v5. > > + > > + slot->arch.rmap[i] = > > + kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), > > + GFP_KERNEL_ACCOUNT); > > Eh, I don't think avoiding a 3 char overrun is worth splitting across three lines. > E.g. this is perfectly readable > > slot->arch.rmap[i] = kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), > GFP_KERNEL_ACCOUNT); > > Alternatively, the rmap size could be captured in a local var, e.g. > > const int sz = sizeof(*slot->arch.rmap[0]); > > ... > > slot->arch.rmap[i] = kvcalloc(lpages, sz, GFP_KERNEL_ACCOUNT); I like this suggestion. Much nicer. Will incorporate if I send a v5. > if (!slot->arch.rmap[i]) { > memslot_rmap_free(slot); > return -ENOMEM; > } > > > + if (!slot->arch.rmap[i]) { > > + memslot_rmap_free(slot); > > + return -ENOMEM; > > Reaaaally getting into nitpicks, what do you think about changing this to a goto > with the error handling at the bottom? Obviously not necessary by any means, > but for me it makes it easier to see that all rmaps are freed on failure. My > eyes skipped over that on the first read through. E.g. > > if (!slot_arch.rmap[i]) > goto err; > } > > return 0; > > err: > memslot_rmap_free(slot); > return -ENOMEM; > Lol, I had a goto in v3, but David Hildenbrand suggested removing it and putting the free in the loop. I think I like it more this way too. > > > + } > > + } > > + > > + return 0; > > +} > > + > > static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, > > unsigned long npages) > > { > > int i; > > + int r; > > Personal preference, for short declarations like this I like putting 'em on a > single line. > > > /* > > * 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 > >