Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp3511351pxp; Tue, 8 Mar 2022 16:17:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJyVaahhWVpGQBOPCwF6cXdVXF9K5OUOjuw/q96qSlroG9YhkjlDIDPkQZbZJuEOnqtLvSBt X-Received: by 2002:a17:90b:350c:b0:1bf:1dc5:1c3d with SMTP id ls12-20020a17090b350c00b001bf1dc51c3dmr7458858pjb.53.1646785054756; Tue, 08 Mar 2022 16:17:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646785054; cv=none; d=google.com; s=arc-20160816; b=TbBMTlluZttY+xOgWK373SkfjjkpL8jzbQ2+9Ty/BGh+PTSNubNjrCrq04hX6a1HM3 E6dfVzU1Qe5f7XitNuwrQPbZb94thNwRl75b4Y+Hp6kBsAqihOfFbryK7V9fy2K3dygj lO80ZPDaJPF90dy8gRX0V8v3sOBqR0b0BqYG9kVGLKAtChaT3dno4BA24PlBlfT8dOwO O75NFgBIC5p3jZqVPeGd+N8B2ejbkiuOkQwoNOeyJ36YI2/dsUzPTAk5Se4XAZot/DcF HASVWodBCZX8QSeoqJj8Yrd42qvixClZ4FIqSF/b/AotRru9ojf26t+/Ag27wjpkdgn7 Q4Vg== 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:user-agent:from :references:in-reply-to:mime-version:dkim-signature; bh=BVV59R1x/3zhBVnVWK6j4y0iat0LkIDDXlTtlhGa9CY=; b=j48LNSVymzDpq4FsRKGP5+O11G9atSe5X8YoXfNgYaFMl85Rx0uz+sAdIJeXg3TxOJ J1wRz5Fsop55A+r+47jUP1Jk86FvxsiP3uBVSM4jIvd2FCMvS4x4pmSw+KRRuADBbOnE qyVwE6+/GPcw24rB1jYQq81sF+xU8lTwtHHvD+cKJMPEJlrNkd0jW+LSPH81ObbdnY4F PNSuMkiFqiOEoKLDRKpagLJDyhKsBZJR2JAJL2qzdJlC+aIYg19NZDfvinWBxK+Grd1P jCwSsO5NRnYy6YfnK++fYepAvj7rfJyczue2fMwtymiroWgHB9s3iF+JNdbw1552lHuu KmuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lczPPxNy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id s16-20020a17090302d000b00151fe42aaacsi407069plk.34.2022.03.08.16.17.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Mar 2022 16:17:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=lczPPxNy; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D2CE195490; Tue, 8 Mar 2022 15:43:31 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350001AbiCHUWk (ORCPT + 99 others); Tue, 8 Mar 2022 15:22:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237865AbiCHUWj (ORCPT ); Tue, 8 Mar 2022 15:22:39 -0500 Received: from mail-oo1-xc30.google.com (mail-oo1-xc30.google.com [IPv6:2607:f8b0:4864:20::c30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BBF0853B6D for ; Tue, 8 Mar 2022 12:21:41 -0800 (PST) Received: by mail-oo1-xc30.google.com with SMTP id r41-20020a4a966c000000b0031bf85a4124so437972ooi.0 for ; Tue, 08 Mar 2022 12:21:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:user-agent:date:message-id :subject:to:cc; bh=BVV59R1x/3zhBVnVWK6j4y0iat0LkIDDXlTtlhGa9CY=; b=lczPPxNy5R21bIe/J5VnIJ82n/HdZ51itN67PDK3dFFcZNEcl/9RrkIuEYhsC36C3g qQk2NsFOqKIGWMY1eCMWE6sJZpxOWyJCWCvi/k7rbaHNsf9EE9MzW4opqLPCCDtyexdd fwr+NlWKn7wteWokK2Sh7UnE4ODwU+UJmsiNE= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:in-reply-to:references:from :user-agent:date:message-id:subject:to:cc; bh=BVV59R1x/3zhBVnVWK6j4y0iat0LkIDDXlTtlhGa9CY=; b=WnpjDq4JVSnkixgsjWHkiXmABEjlN8imEf9Mf8LFNbCl/uZ5djBDWoREf+VbKPcJXj fldK3VjECSIMxrhW+Zab/Ro8+acKuVv6PTjzhWUtoH98dLnuqiL4U0rVbJV131gnHKWJ /vVqWY6B1BCzmSq6302H88h0YTXgjORqqnZ4BZlCO6jfNy8guNxg06PmOansxtl+Injn gl864Y+s2ay8k+QAVj5aoce7zl4hQ2MSvLMv02T9lzknotDWJUn8C6dld9uRv+sPSAis 5rfxijACyYgiPbXxWw6g/VIdzKhRq/wswV014DIjO4fUKwkCO4fyKAu8nEgjTlJ9WC4m tHPQ== X-Gm-Message-State: AOAM530jpxPHnOyu2K7i0AEtKYleseZZu2dqefTHpyikUhpn7AXpb0hE jfhOlg6suyCdRDdnVqwf+PAoWPZNT0vZRqXr3vCZwQ== X-Received: by 2002:a05:6870:d250:b0:da:b3f:3211 with SMTP id h16-20020a056870d25000b000da0b3f3211mr3461463oac.193.1646770901112; Tue, 08 Mar 2022 12:21:41 -0800 (PST) Received: from 753933720722 named unknown by gmailapi.google.com with HTTPREST; Tue, 8 Mar 2022 12:21:40 -0800 MIME-Version: 1.0 In-Reply-To: <20220307184935.1704614-2-kaleshsingh@google.com> References: <20220307184935.1704614-1-kaleshsingh@google.com> <20220307184935.1704614-2-kaleshsingh@google.com> From: Stephen Boyd User-Agent: alot/0.10 Date: Tue, 8 Mar 2022 12:21:40 -0800 Message-ID: Subject: Re: [PATCH v5 1/8] KVM: arm64: Introduce hyp_alloc_private_va_range() To: Kalesh Singh Cc: will@kernel.org, maz@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 , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Scull , linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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 Quoting Kalesh Singh (2022-03-07 10:48:59) > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c > index bc2aba953299..ccb2847ee2f4 100644 > --- a/arch/arm64/kvm/mmu.c > +++ b/arch/arm64/kvm/mmu.c > @@ -457,22 +457,17 @@ 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. > + * > + * The private VA range is allocated below io_map_base and > + * aligned based on the order of @size. Add what it returns? Return: Start address of allocated VA range or some error value... (I don't understand this part). It may also be a good idea to write out what VA is in the description: The private virtual address (VA) range is allocated below io_map_base > + */ > +unsigned long hyp_alloc_private_va_range(size_t size) > { > 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,29 +479,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)) > - ret = -ENOMEM; > + if (!base || (base ^ io_map_base) & BIT(VA_BITS - 1)) > + base = (unsigned long)ERR_PTR(-ENOMEM); It looks odd to use an error pointer casted to unsigned long to return from an address allocation function. Why not pass a pointer for base like the function was written before and return an int from this function with 0 for success and negative error value? Otherwise some sort of define should made like DMA_MAPPING_ERROR and that can be used to indicate to the caller that the allocation failed, or a simple zero may work? > else > io_map_base = base; > > mutex_unlock(&kvm_hyp_pgd_mutex); > > - if (ret) > - goto out; > + return base; > +} > + > +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((void *)addr)) IS_ERR_VALUE()? > + return PTR_ERR((void *)addr); > + *haddr = addr; > + > + return 0; > + } > + > + size += offset_in_page(phys_addr); > + addr = hyp_alloc_private_va_range(size); > + if (IS_ERR((void *)addr)) IS_ERR_VALUE()? > + return PTR_ERR((void *)addr); > > - ret = __create_hyp_mappings(base, size, phys_addr, prot); > + ret = __create_hyp_mappings(addr, size, phys_addr, prot); > if (ret) > goto out; > > - *haddr = base + offset_in_page(phys_addr); > + *haddr = addr + offset_in_page(phys_addr); > out: > return ret; Would be simpler to remove the goto, or return early. if (!ret) *haddr = addr + offset_in_page(phys_addr); return ret; > }