Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3792459pxj; Tue, 11 May 2021 11:58:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxAkKXEc9duSwapUyeQCjWS17Ch1La3qxANQ9D0tMSaZo/lDlR1AdKgX3rVnAQqZhYsuD+c X-Received: by 2002:a05:6402:40cb:: with SMTP id z11mr39648868edb.25.1620759492221; Tue, 11 May 2021 11:58:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620759492; cv=none; d=google.com; s=arc-20160816; b=efTM1dShcgiu5G0nvtNGp+z37dO4tAseoq+6b6zDKODbraWdWB+97XMACrEMtS5hzi RSwt0QtrGnF/iw6QzLwA/DsZptpKMaCKfjHtYhpJ/X2hheZX/eC8P2pBbmlTsKO1MTFb czIg1ET5Ns7ln37Lp7pQDC+r0Q6HLSO/b4PrQXG9yzYFMfEK2FEXgLGEqScQkaLi3ceU kjCk6BE30KcrFCVq6+2HKnjMLbePNGXYfJhKK0i5i7DYuaBOGqJLpLxHkz/kJ94zyuWE w4JdpefohtsrcReEFsfhRZ3SpU2naDkmV8wrcWSmX0+Hq8oz62UMkAq83nv28d2VJBQb Tu7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:organization :from:references:cc:to:subject:dkim-signature; bh=wFkkbYCZo1giPpX/66sSfgPGZd0thaWCB/6x9tKyjrU=; b=aX5BAjz7r+O3ck0i1AWTo9Se13IDu5mStH34QjDKl04dwaWvgdTwH5gS4VtaX4AO+E BozQv5EUG8c8yWyaBKKpm/qN5dznd9ert0W+FJBWhGowBN38pQlCXX5OeOnzN7tB2N1q TrUZ8Qoql+lufdjXFFYLynfO1ohAWOpFO7uOH0mNVyLbyRmpRsIOGpX898xwnMa4bmn2 bp3e9liLiZBmJfegKiqldOl0mSqsaXL5eM/lq0wG2NfCt9Xg/0H4ND1ZAi1sWm7hgwgv wbNatKbKjPMxbawhBUH+bmFDC1W92xE0isTY7cNk6gnOtz/w5zpNFG00amjvqFUXzege /ziw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Q0hGjiTi; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d12si21156096ejj.422.2021.05.11.11.57.47; Tue, 11 May 2021 11:58:12 -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=@redhat.com header.s=mimecast20190719 header.b=Q0hGjiTi; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231982AbhEKS57 (ORCPT + 99 others); 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-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