Received: by 10.223.176.5 with SMTP id f5csp1758699wra; Thu, 8 Feb 2018 03:02:31 -0800 (PST) X-Google-Smtp-Source: AH8x225jrDhwLhquqdpVDpdDp1u17XIyii6LV3CqV2nxEUp5doG/8lez0+qkPD+sgcljs14k1YCz X-Received: by 10.99.114.15 with SMTP id n15mr229295pgc.286.1518087751245; Thu, 08 Feb 2018 03:02:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518087751; cv=none; d=google.com; s=arc-20160816; b=uVQF6obX/tZwLk+pg/BfyoytA9g43QaDIzeSltDBLOQvAhW9yRJETnjomlvbKD9K9F P5UDxRO3YioT/fszvQNOPT57QH/F1jJUwst4Glpqhjp/dqVjPJhW+CyL4uOVIxkjK3R1 XB9psm76TPcEbczkFh5tETjtO2lj4+9BBC0bIUY4MTk3Kzdcs1F3SiOO/1crvYgmsL0i sUOrFFFKhCdL1PsETff4Obp6zSK8cf8bqAYJAFDqfhn97TzXEq6WBjhEWH2O8OpM5F7P 7PCZh3zcv5mmoKosOBcwu6cj7Ozu7ANPHvZNaPgYlJgMBxmT0F+owPz/mUg8ZpF06srN 8DTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=JjxyHnLdp9ncy8jLyuF25xSIEwFA8CNYhJLPKvZonss=; b=FMiLUOSiWhuXqp2jxhf5SGjw+5TuqI79S8XgkCCItfoI8CtKG/ITg83WCdxd5zSF/9 Z1rzjm9IK2mCQc1LbqWElIaKHAsc4Q+4wOQYhxCijq+AYUXm1QjC8P8oRlRBlFcd+2Pa douFvb0GxEqTuA66pc9ctxc34sYC0W4a1ezFxS4+TOlJHamIK7WOxqz4ZmWxSP5Gw565 m5WIQGiUKaOPdxq3zduPuDtIq1NNqB0MvSEj0GBkNjo3YQO1H8dksjmBX/tDFahGlkqF qxfW6mhnUAQhGOpVLfVrrmK8jTMaraH+9owcL35MPu3FHzlH2sbqT8WpZzWSBKTSftOZ 9EjQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MKimcAaB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t80si1051676pgb.662.2018.02.08.03.02.17; Thu, 08 Feb 2018 03:02:31 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=MKimcAaB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752446AbeBHLBU (ORCPT + 99 others); Thu, 8 Feb 2018 06:01:20 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:55572 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752410AbeBHLBR (ORCPT ); Thu, 8 Feb 2018 06:01:17 -0500 Received: by mail-wm0-f66.google.com with SMTP id 143so8666056wma.5 for ; Thu, 08 Feb 2018 03:01:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=JjxyHnLdp9ncy8jLyuF25xSIEwFA8CNYhJLPKvZonss=; b=MKimcAaBNhQs70gmmqKxu8BBS8s1gOzvWQOBxyH3kInFLyoQ4StW7rqTry5T7B2NU2 Z7fejeEryaWHwUXvdz7A5dATq5NosToHElmcgD1JTB3K686QNHtx6sbPSU2Di7JSmfTD oeO/ELfvd/PErr/TjyUJl+iGMlG4dOKEp0ozg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=JjxyHnLdp9ncy8jLyuF25xSIEwFA8CNYhJLPKvZonss=; b=kDqwTM6cUEz0v/5LA3mtymSs3CUpBoXsbtNMkPC4dmZ4+A21jXPozn2uPabIBxrjPV 0+mcp5XMhD8z6HyqBeLgSlonwn92oOEPLa3EJCkPjZ02W0fCbAontQjJbjIlUU36rqMl i3hqhjJdo5Ph0bpjQZkcTXgnGby+SkEVK9AQFtQzWPTZxZG59jcZWENhrF1srxHlGtwB GYUdJ5bPy9S2ui8P9lo76KrdF66S+8eH2rl2UvGX7tBZv32NDrtl24ng9OtM598J1mZu miuNIg9lOzimu90k2Pw1sNORP11h5f/6/AgYV3DK7K3rggBLsxjrr+3wxiHIh5g/kLlR 7xfg== X-Gm-Message-State: APf1xPAsMvYgPdafsSoeg7/6zFEj/P7+WrI+TzaiVN0UWGjVvydZc9+q +llGHKJPNKps4xqAMX/t8EGxZg== X-Received: by 10.80.159.1 with SMTP id b1mr951432edf.132.1518087676415; Thu, 08 Feb 2018 03:01:16 -0800 (PST) Received: from localhost (x50d2404e.cust.hiper.dk. [80.210.64.78]) by smtp.gmail.com with ESMTPSA id e12sm2278542edi.29.2018.02.08.03.01.15 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 08 Feb 2018 03:01:15 -0800 (PST) Date: Thu, 8 Feb 2018 12:01:14 +0100 From: Christoffer Dall To: Suzuki K Poulose Cc: linux-arm-kernel@lists.infradead.org, kvm@vger.kernel.org, kvmarm@lists.cs.columbia.edu, marc.zyngier@arm.com, linux-kernel@vger.kernel.org, kristina.martsenko@arm.com, peter.maydell@linaro.org, pbonzini@redhat.com, rkrcmar@redhat.com, will.deacon@arm.com, ard.biesheuvel@linaro.org, mark.rutland@arm.com, catalin.marinas@arm.com, Christoffer Dall Subject: Re: [PATCH v1 09/16] kvm: arm/arm64: Delay stage2 page table allocation Message-ID: <20180208110114.GL29286@cbox> References: <20180109190414.4017-1-suzuki.poulose@arm.com> <20180109190414.4017-10-suzuki.poulose@arm.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20180109190414.4017-10-suzuki.poulose@arm.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 09, 2018 at 07:04:04PM +0000, Suzuki K Poulose wrote: > We allocate the entry level page tables for stage2 when the > VM is created. This doesn't give us the flexibility of configuring > the physical address space size for a VM. In order to allow > the VM to choose the required size, we delay the allocation of > stage2 entry level tables until we really try to map something. > > This could be either when the VM creates a memory range or when > it tries to map a device memory. So we add in a hook to these > two places to make sure the tables are allocated. We use > kvm->slots_lock to serialize the allocation entry point, since > we add hooks to the arch specific call back with the mutex held. > > Cc: Marc Zyngier > Cc: Christoffer Dall > Signed-off-by: Suzuki K Poulose > --- > virt/kvm/arm/arm.c | 18 ++++++---------- > virt/kvm/arm/mmu.c | 61 +++++++++++++++++++++++++++++++++++++++++++++--------- > 2 files changed, 57 insertions(+), 22 deletions(-) > > diff --git a/virt/kvm/arm/arm.c b/virt/kvm/arm/arm.c > index 19b720ddedce..d06f00566664 100644 > --- a/virt/kvm/arm/arm.c > +++ b/virt/kvm/arm/arm.c > @@ -127,13 +127,13 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) > for_each_possible_cpu(cpu) > *per_cpu_ptr(kvm->arch.last_vcpu_ran, cpu) = -1; > > - ret = kvm_alloc_stage2_pgd(kvm); > - if (ret) > - goto out_fail_alloc; > - > ret = create_hyp_mappings(kvm, kvm + 1, PAGE_HYP); > - if (ret) > - goto out_free_stage2_pgd; > + if (ret) { > + free_percpu(kvm->arch.last_vcpu_ran); > + kvm->arch.last_vcpu_ran = NULL; > + return ret; > + } > + > > kvm_vgic_early_init(kvm); > > @@ -145,12 +145,6 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) > kvm_vgic_get_max_vcpus() : KVM_MAX_VCPUS; > > return ret; > -out_free_stage2_pgd: > - kvm_free_stage2_pgd(kvm); > -out_fail_alloc: > - free_percpu(kvm->arch.last_vcpu_ran); > - kvm->arch.last_vcpu_ran = NULL; > - return ret; > } > > bool kvm_arch_has_vcpu_debugfs(void) > diff --git a/virt/kvm/arm/mmu.c b/virt/kvm/arm/mmu.c > index c94c61ac38b9..257f2a8ccfc7 100644 > --- a/virt/kvm/arm/mmu.c > +++ b/virt/kvm/arm/mmu.c > @@ -1011,15 +1011,39 @@ static int stage2_pmdp_test_and_clear_young(pmd_t *pmd) > return stage2_ptep_test_and_clear_young((pte_t *)pmd); > } > > -/** > - * kvm_phys_addr_ioremap - map a device range to guest IPA > - * > - * @kvm: The KVM pointer > - * @guest_ipa: The IPA at which to insert the mapping > - * @pa: The physical address of the device > - * @size: The size of the mapping > +/* > + * Finalise the stage2 page table layout. Must be called with kvm->slots_lock > + * held. > */ > -int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, > +static int __kvm_init_stage2_table(struct kvm *kvm) > +{ > + /* Double check if somebody has already allocated it */ dubious comment: Either leave it out or explain that we need to check again with the mutex held. > + if (likely(kvm->arch.pgd)) > + return 0; > + return kvm_alloc_stage2_pgd(kvm); > +} > + > +static int kvm_init_stage2_table(struct kvm *kvm) > +{ > + int rc; > + > + /* > + * Once allocated, the stage2 entry level tables are only > + * freed when the KVM instance is destroyed. So, if we see > + * something valid here, that guarantees that we have > + * done the one time allocation and it is something valid > + * and won't go away until the last reference to the KVM > + * is gone. > + */ Really not sure if this comment adds something beyond what's described by the code already? Thanks, -Christoffer > + if (likely(kvm->arch.pgd)) > + return 0; > + mutex_lock(&kvm->slots_lock); > + rc = __kvm_init_stage2_table(kvm); > + mutex_unlock(&kvm->slots_lock); > + return rc; > +} > + > +static int __kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, > phys_addr_t pa, unsigned long size, bool writable) > { > phys_addr_t addr, end; > @@ -1055,6 +1079,23 @@ int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, > return ret; > } > > +/** > + * kvm_phys_addr_ioremap - map a device range to guest IPA. > + * Acquires kvm->slots_lock for making sure that the stage2 is initialized. > + * > + * @kvm: The KVM pointer > + * @guest_ipa: The IPA at which to insert the mapping > + * @pa: The physical address of the device > + * @size: The size of the mapping > + */ > +int kvm_phys_addr_ioremap(struct kvm *kvm, phys_addr_t guest_ipa, > + phys_addr_t pa, unsigned long size, bool writable) > +{ > + if (unlikely(kvm_init_stage2_table(kvm))) > + return -ENOMEM; > + return __kvm_phys_addr_ioremap(kvm, guest_ipa, pa, size, writable); > +} > + > static bool transparent_hugepage_adjust(kvm_pfn_t *pfnp, phys_addr_t *ipap) > { > kvm_pfn_t pfn = *pfnp; > @@ -1912,7 +1953,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, > goto out; > } > > - ret = kvm_phys_addr_ioremap(kvm, gpa, pa, > + ret = __kvm_phys_addr_ioremap(kvm, gpa, pa, > vm_end - vm_start, > writable); > if (ret) > @@ -1943,7 +1984,7 @@ void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free, > int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot, > unsigned long npages) > { > - return 0; > + return __kvm_init_stage2_table(kvm); > } > > void kvm_arch_memslots_updated(struct kvm *kvm, struct kvm_memslots *slots) > -- > 2.13.6 >