From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D6E8C433ED for ; Tue, 11 May 2021 18:56:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5AFC6611C9 for ; Tue, 11 May 2021 18:56:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232122AbhEKS57 (ORCPT ); Tue, 11 May 2021 14:57:59 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:38076 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231454AbhEKS56 (ORCPT ); Tue, 11 May 2021 14:57:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620759410; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wFkkbYCZo1giPpX/66sSfgPGZd0thaWCB/6x9tKyjrU=; b=Q0hGjiTiGDvCf0Vih60v62y2uheCeg+tU6RaTxcHVNXx/2QiChHMTgVSi7l/P1EKGqv5KT AJQLrbyz5itPMrREJf/CsWsQvFZHtXIx+rBh4/vKm1zSbq3VZYEb7TsiLeJWYjLSbTIf4r dzJ37u08GkUUH4A/I4tDVrUyv0jrpz0= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-103-WqWW8yz_P8WHBLqMappGHw-1; Tue, 11 May 2021 14:56:47 -0400 X-MC-Unique: WqWW8yz_P8WHBLqMappGHw-1 Received: by mail-ed1-f70.google.com with SMTP id k10-20020a50cb8a0000b0290387e0173bf7so11524116edi.8 for ; Tue, 11 May 2021 11:56:46 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=wFkkbYCZo1giPpX/66sSfgPGZd0thaWCB/6x9tKyjrU=; b=SQT+8yMGkD3J2ZKVK5W8YcnAZrvSyrvZsjMDjpqBbGHeXdTzTsV2ouyprJ5hrNo4VN DHjYIReLAd2P7PGrIKtqwMjH2mdAmSTEhGUJd7Gf/ZNDI8X9JnMMsEfygQqJOwaV4Gmo l9uiV1g5LfWuBQh/i2jaZ60MHyPX4aBPaG9ElYfTIMRF9g8HSSeXVdM0eMn69Frk9ROJ PVtBAmREiEistdSTOtT/ZYeFIBGpvaJzIwKNH4NERF3wiUChxjgGxb+6Cx7D+6t6EO2X pidsa7coyFf02RhpDp8PspWj8fh1Cn+OzkB04Y9LKrOcKAxNMmrgkIxnoP7ZWBLQkx+G 2o/g== X-Gm-Message-State: AOAM533/WDqhNpPd8JRdPJGQPAe0n0dDEs1Bpcpmv1C8tjgvgY6qEpg2 Vs2TbAtHgQDhCDvSyOJfx5OnYNvRUuUny1Q4atDGjCwJZ4AQ4zr8QnoEl2NdVxnylxCgwIjCbyI CXWnTNaZOS4s91EFo857NDrzb X-Received: by 2002:a50:cdd1:: with SMTP id h17mr37939623edj.178.1620759405867; Tue, 11 May 2021 11:56:45 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzWXSLE3cFuMP6/lndEcrzy/s8nluFRQk7CGoFU7s22O0Mbr7C1CUvhRVVGWwjzcvm+bTWPxQ== X-Received: by 2002:a50:cdd1:: with SMTP id h17mr37939599edj.178.1620759405629; Tue, 11 May 2021 11:56:45 -0700 (PDT) Received: from [192.168.3.132] (p5b0c6329.dip0.t-ipconnect.de. [91.12.99.41]) by smtp.gmail.com with ESMTPSA id pw11sm12232276ejb.88.2021.05.11.11.56.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 11 May 2021 11:56:45 -0700 (PDT) Subject: Re: [PATCH v4 2/7] KVM: x86/mmu: Factor out allocating memslot rmap To: Ben Gardon , Sean Christopherson Cc: LKML , kvm , Paolo Bonzini , Peter Xu , Peter Shier , Yulei Zhang , Wanpeng Li , Xiao Guangrong , Kai Huang , Keqian Zhu References: <20210511171610.170160-1-bgardon@google.com> <20210511171610.170160-3-bgardon@google.com> From: David Hildenbrand Organization: Red Hat Message-ID: <47ba1a62-9035-08c6-22c3-acae9bdd3572@redhat.com> Date: Tue, 11 May 2021 20:56:44 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11.05.21 20:17, Ben Gardon wrote: > 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. No strong opinion, I tend to stick to Documentation/process/coding-style.rst which states "The goto statement comes in handy when a function exits from multiple locations and some common work such as cleanup has to be done." As we only have a single error exit and no complicated locking, at least for me the "goto" makes it unnecessary hard to read. -- Thanks, David / dhildenb