Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp27746pxb; Mon, 11 Apr 2022 17:52:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxxE69hbCgm+vCGkAzM/fDYDhDHLKLJXJ8b2RRN0NUpDkoEl+bFlBb7kQr/Ljb968ZFY+Tw X-Received: by 2002:a17:906:3144:b0:6ce:de5d:5e3b with SMTP id e4-20020a170906314400b006cede5d5e3bmr32418089eje.689.1649724734345; Mon, 11 Apr 2022 17:52:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649724734; cv=none; d=google.com; s=arc-20160816; b=js3HwYMFZlEVRPJ64aH9Q9F0pbOI9e1sQGM0QEZ5AWRw+ndphIHvZ5kkzQltHTW7Go zmx4h7bDIAdIbxaYY9aXXAqWHz4ZbdpdoNBdx3u4MHgz+GHglWMskfgjpRkXwpKuzoOU btaTJRz9e5Vm3uvgr9XQSAWSKpjFLkMqldmp9u0323lDSXo0pO3IjbmzIcj9aA9fjJYs 2N8VqaCUty4YQFm2JPJxG8xTb8AJMfmg4YFdl69xFhGYUzvwYsybaYu9rlx4LHZaKhTZ oLDJ6DA2FrtRvDIklhlfuNMqjSKxwja0+xacQTDy62xkxK7bcYH4wBSB4nuJdByWZW2E sAzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:in-reply-to :subject:cc:to:from:message-id:date:dkim-signature; bh=m/1/gfEXCQSc/0/8ZmEw4ohCasrDbFiDxZC9tiIIL+k=; b=f+g1kpozu9txVfZHd1TlNGcM5RbeC612FxQYzs9/e4jBQJ+NA0Buu+kIG8x+R+pO05 s0vAgQkGZgByIDXPpGb7p51vfWseJ698HW09XC5mVYqGukOm7dFPJJp4RREDZtKjNVtv NiXhC2KQ7bPzM29NCMAFbtQkxdieJJ9Pi4DgBp4+MgUam/qlVTwz/dQGmtdMpY5Ru3G2 cD/RrnJlQsQzpw7d29VjSLY3XKzWjmaR77DVlt9Sn4hkarrrtmWx6GWwJW3hDQFwM2jz TMHIkXIh2kVkTMPzDE9sQjlbokPtoLJeaHOjdspgJhYpdFH+x+uUq8ROdnDMKuyJW3yK uqvg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gDIXmFyX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b7-20020a170906660700b006e88f254442si3638334ejp.465.2022.04.11.17.51.50; Mon, 11 Apr 2022 17:52:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=gDIXmFyX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244645AbiDKGym (ORCPT + 99 others); Mon, 11 Apr 2022 02:54:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236025AbiDKGyl (ORCPT ); Mon, 11 Apr 2022 02:54:41 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 43AF319015 for ; Sun, 10 Apr 2022 23:52:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id BA347B8110C for ; Mon, 11 Apr 2022 06:52:26 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5D403C385A4; Mon, 11 Apr 2022 06:52:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1649659945; bh=yx+UeJ53L3lrvtA6p4tTHO/FMgJTGtQyiFwwhfFKWNQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=gDIXmFyXrkR/rHm8QEzS45Ezv6GyBt67dtUAkfRbVWrZ49RFcn1v69Nk/5nx7jk/p AkNx7mNb9CEtEZD8E7jtKspLrW20g1lCL7kJrpYAEFHSYbQgFtHBHF55B6DWxYqJge bZ9ZysQZ3XB0YIDd5+emhql2nfe2mY4PL3R4YA0F0XVL9LOtG+z84z10kcKCV2FTI4 GSSnugljbqUt32dfKl+1ucBUj+9JSHK4AtRmCZouswsmPwg5gq3IyKq/C3uk5umTAI 8rUvGMTj82q00kBEXQvvqeTQycnDw8Re6pX2BJR2fn0I5xHG692ko9sCY15VUNN7UU a09spn+vX4c6A== Received: from ip-185-104-136-29.ptr.icomera.net ([185.104.136.29] helo=billy-the-mountain.misterjones.org) by disco-boy.misterjones.org with esmtpsa (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ndnuP-003FOL-JJ; Mon, 11 Apr 2022 07:52:22 +0100 Date: Mon, 11 Apr 2022 07:52:07 +0100 Message-ID: <87bkx8cdt4.wl-maz@kernel.org> From: Marc Zyngier To: Kalesh Singh Cc: will@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Zenghui Yu , Masahiro Yamada , Nick Desaulniers , Changbin Du , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Subject: Re: [PATCH v7 1/6] KVM: arm64: Introduce hyp_alloc_private_va_range() In-Reply-To: <20220408200349.1529080-2-kaleshsingh@google.com> References: <20220408200349.1529080-1-kaleshsingh@google.com> <20220408200349.1529080-2-kaleshsingh@google.com> User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?B?R29qxY0=?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/27.1 (aarch64-unknown-linux-gnu) MULE/6.0 (HANACHIRUSATO) MIME-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: text/plain; charset=US-ASCII X-SA-Exim-Connect-IP: 185.104.136.29 X-SA-Exim-Rcpt-To: kaleshsingh@google.com, will@kernel.org, qperret@google.com, tabba@google.com, surenb@google.com, kernel-team@android.com, james.morse@arm.com, alexandru.elisei@arm.com, suzuki.poulose@arm.com, catalin.marinas@arm.com, mark.rutland@arm.com, yuzenghui@huawei.com, masahiroy@kernel.org, ndesaulniers@google.com, changbin.du@intel.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org X-SA-Exim-Mail-From: maz@kernel.org X-SA-Exim-Scanned: No (on disco-boy.misterjones.org); SAEximRunCond expanded to false X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 08 Apr 2022 21:03:24 +0100, Kalesh Singh wrote: > > hyp_alloc_private_va_range() can be used to reserve private VA ranges > in the nVHE hypervisor. Allocations are aligned based on the order of > the requested size. > > This will be used to implement stack guard pages for KVM nVHE hypervisor > (nVHE Hyp mode / not pKVM), in a subsequent patch in the series. > > Signed-off-by: Kalesh Singh > Tested-by: Fuad Tabba > Reviewed-by: Fuad Tabba > --- > > Changes in v7: > - Add Fuad's Reviewed-by and Tested-by tags. > > Changes in v6: > - Update kernel-doc for hyp_alloc_private_va_range() > and add return description, per Stephen > - Update hyp_alloc_private_va_range() to return an int error code, > per Stephen > - Replace IS_ERR() checks with IS_ERR_VALUE() check, per Stephen > - Clean up goto, per Stephen > > Changes in v5: > - Align private allocations based on the order of their size, per Marc > > Changes in v4: > - Handle null ptr in hyp_alloc_private_va_range() and replace > IS_ERR_OR_NULL checks in callers with IS_ERR checks, per Fuad > - Fix kernel-doc comments format, per Fuad > > Changes in v3: > - Handle null ptr in IS_ERR_OR_NULL checks, per Mark > > > arch/arm64/include/asm/kvm_mmu.h | 1 + > arch/arm64/kvm/mmu.c | 66 +++++++++++++++++++++----------- > 2 files changed, 45 insertions(+), 22 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_mmu.h b/arch/arm64/include/asm/kvm_mmu.h > index 74735a864eee..a50cbb5ba402 100644 > --- a/arch/arm64/include/asm/kvm_mmu.h > +++ b/arch/arm64/include/asm/kvm_mmu.h > @@ -154,6 +154,7 @@ static __always_inline unsigned long __kern_hyp_va(unsigned long v) > int kvm_share_hyp(void *from, void *to); > void kvm_unshare_hyp(void *from, void *to); > int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot); > +int hyp_alloc_private_va_range(size_t size, unsigned long *haddr); > int create_hyp_io_mappings(phys_addr_t phys_addr, size_t size, > void __iomem **kaddr, > void __iomem **haddr); > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index 0d19259454d8..3d3efea4e991 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -457,23 +457,22 @@ int create_hyp_mappings(void *from, void *to, enum kvm_pgtable_prot prot) > return 0; > } > > -static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > - unsigned long *haddr, > - enum kvm_pgtable_prot prot) > + > +/** > + * hyp_alloc_private_va_range - Allocates a private VA range. > + * @size: The size of the VA range to reserve. > + * @haddr: The hypervisor virtual start address of the allocation. > + * > + * The private virtual address (VA) range is allocated below io_map_base > + * and aligned based on the order of @size. > + * > + * Return: 0 on success or negative error code on failure. > + */ > +int hyp_alloc_private_va_range(size_t size, unsigned long *haddr) > { > unsigned long base; > int ret = 0; > > - if (!kvm_host_owns_hyp_mappings()) { > - base = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > - phys_addr, size, prot); > - if (IS_ERR_OR_NULL((void *)base)) > - return PTR_ERR((void *)base); > - *haddr = base; > - > - return 0; > - } > - > mutex_lock(&kvm_hyp_pgd_mutex); > > /* > @@ -484,30 +483,53 @@ static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > * > * The allocated size is always a multiple of PAGE_SIZE. > */ > - size = PAGE_ALIGN(size + offset_in_page(phys_addr)); > - base = io_map_base - size; > + base = io_map_base - PAGE_ALIGN(size); > + > + /* Align the allocation based on the order of its size */ > + base = ALIGN_DOWN(base, PAGE_SIZE << get_order(size)); > > /* > * Verify that BIT(VA_BITS - 1) hasn't been flipped by > * allocating the new area, as it would indicate we've > * overflowed the idmap/IO address range. > */ > - if ((base ^ io_map_base) & BIT(VA_BITS - 1)) > + if (!base || (base ^ io_map_base) & BIT(VA_BITS - 1)) I don't get this '!base' check. Why isn't it encompassed by the 'VA_BITS - 1' flip check? > ret = -ENOMEM; > else > - io_map_base = base; > + *haddr = io_map_base = base; > > mutex_unlock(&kvm_hyp_pgd_mutex); > > + return ret; > +} > + > +static int __create_hyp_private_mapping(phys_addr_t phys_addr, size_t size, > + unsigned long *haddr, > + enum kvm_pgtable_prot prot) > +{ > + unsigned long addr; > + int ret = 0; > + > + if (!kvm_host_owns_hyp_mappings()) { > + addr = kvm_call_hyp_nvhe(__pkvm_create_private_mapping, > + phys_addr, size, prot); > + if (IS_ERR_VALUE(addr)) > + return addr; > + *haddr = addr; > + > + return 0; > + } > + > + size += offset_in_page(phys_addr); This hardly makes any sense on its own. I get it that it is still doing the right thing as hyp_alloc_private_va_range() will fix it up, but I'd rather you keep the PAGE_ALIGN() here, even if it ends up being duplicated. > + ret = hyp_alloc_private_va_range(size, &addr); > if (ret) > - goto out; > + return ret; > > - ret = __create_hyp_mappings(base, size, phys_addr, prot); > + ret = __create_hyp_mappings(addr, size, phys_addr, prot); > if (ret) > - goto out; > + return ret; > > - *haddr = base + offset_in_page(phys_addr); > -out: > + *haddr = addr + offset_in_page(phys_addr); > return ret; > } > Thanks, M. -- Without deviation from the norm, progress is not possible.