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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A5B0DC433EF for ; Tue, 8 Mar 2022 23:09:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229706AbiCHXKc (ORCPT ); Tue, 8 Mar 2022 18:10:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43606 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229447AbiCHXKb (ORCPT ); Tue, 8 Mar 2022 18:10:31 -0500 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACFFF6E4FA for ; Tue, 8 Mar 2022 15:09:31 -0800 (PST) Received: by mail-wr1-x42c.google.com with SMTP id b5so429860wrr.2 for ; Tue, 08 Mar 2022 15:09:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1f5gPcrqfy750U/J52Wg+Mj+0/UcxsK3pfUKBKWLDR0=; b=rY724zyNQvt0W7k77uJ6mdsljpHhmPedp6A01Ybgi4ape+kd29m4bzWSt+PlFv6VGk 5F64VH7NbTfh9lbiAtg/QqctdXYwNeto4qq8ILBsk1UjKlgBgsCMQeBPoPLONRLpHghE cWdsR0etuAIMnRL3KlwUvfLr789Koi+WEO7RwGW0jHeOJ70GUKcFjnSzplqBRUYBRhT6 QP8IEuX2CtkcOTNnpXBR5MGZ0IWr8YG+CglWc/j5+G1oi8Hir+ky5riHmAiBgpzzzCL3 WDmeIljWzrU1rFQ9e3oINeWI9gqLxDH39hU6jNdl97UeioJbJwXvtYXCCKepzEEDLGHC BVFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1f5gPcrqfy750U/J52Wg+Mj+0/UcxsK3pfUKBKWLDR0=; b=bEOhlMhYpST0fnIumWxAU8tdw6za2naxeuhAbQ/0ywMFoCtoMc9XPW0rL0prEzVrIP w312e++lnMIqpFc7PO7poMv8VE9BnfOCYGyDblb52k6wBB0/DgvvLHk7FcqgsawMztER nYBsFNUk5Q1/dV3xpQJKN3F8POM+/Eq4GgwLC9fG/7zKhExnXgw35VYr/1C19WGsvfGM JLAveIiOtbvVOWHgMNO9++kEdKUocPHaIbgpHzxfkOqFSZucBkEem0Lm+ep8kT1ae6Te 28qCeBHm/q7HvpCGVn61K8mZUwOK9AalA1O4ffyhcUubuMiFTrarsbhNQ8sVdXQFVfOC rrLg== X-Gm-Message-State: AOAM530g5hz0m0cBa66a5H773VmVd86DGyrnTiSmiNpDG7RITG6cozl+ cIRthINXyLMqO5BcnL5DT7PDLt/mSm5tQYvD/5HvZw== X-Google-Smtp-Source: ABdhPJwNHy+VN+JXLWCDADBmqbJunnYlm8shlOKM3wtyDi3l0Nvo++c+lLHsCuWeoJlsi7IJiccAZFbP2qFwzcnxW0Q= X-Received: by 2002:adf:eb86:0:b0:1e6:8c92:af6b with SMTP id t6-20020adfeb86000000b001e68c92af6bmr13872277wrn.116.1646780969857; Tue, 08 Mar 2022 15:09:29 -0800 (PST) MIME-Version: 1.0 References: <20220307184935.1704614-1-kaleshsingh@google.com> <20220307184935.1704614-2-kaleshsingh@google.com> In-Reply-To: From: Kalesh Singh Date: Tue, 8 Mar 2022 15:09:18 -0800 Message-ID: Subject: Re: [PATCH v5 1/8] KVM: arm64: Introduce hyp_alloc_private_va_range() To: Stephen Boyd Cc: Will Deacon , Marc Zyngier , Quentin Perret , Fuad Tabba , Suren Baghdasaryan , "Cc: Android Kernel" , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Scull , "moderated list:ARM64 PORT (AARCH64 ARCHITECTURE)" , kvmarm , LKML Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 8, 2022 at 12:21 PM Stephen Boyd wrote: > HI Stephen. Thanks for the review. > 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 > Ack > > + */ > > +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? I wanted to keep consistent between the pkvm and traditional nvhe code. I will refactor both *alloc_private_va_range() functions to take a pointer and return an int error if that's preferred. There would still be a case of this kind of cast in __pkvm_create_private_mapping() which does return an unsigned long address or ERR_PTR(...). It looks like it was made to return the address to facilitate use as a hypercall (@Quentin CMIW). > > > 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()? Good idea, will remove the extra cast. > > > + 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()? Ack > > > + 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; Agreed, I'll remove the goto in the next version. Thanks, Kalesh > > > } 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 Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by smtp.lore.kernel.org (Postfix) with ESMTP id B6CC3C433F5 for ; Tue, 8 Mar 2022 23:09:35 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 2CB8849ED0; Tue, 8 Mar 2022 18:09:35 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@google.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id C7uPfpk+EOr1; Tue, 8 Mar 2022 18:09:33 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id D112649EC4; Tue, 8 Mar 2022 18:09:33 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 5939549EBE for ; Tue, 8 Mar 2022 18:09:32 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id MtufiNdRiAAd for ; Tue, 8 Mar 2022 18:09:31 -0500 (EST) Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 23FC549EBD for ; Tue, 8 Mar 2022 18:09:31 -0500 (EST) Received: by mail-wr1-f47.google.com with SMTP id j26so434621wrb.1 for ; Tue, 08 Mar 2022 15:09:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1f5gPcrqfy750U/J52Wg+Mj+0/UcxsK3pfUKBKWLDR0=; b=rY724zyNQvt0W7k77uJ6mdsljpHhmPedp6A01Ybgi4ape+kd29m4bzWSt+PlFv6VGk 5F64VH7NbTfh9lbiAtg/QqctdXYwNeto4qq8ILBsk1UjKlgBgsCMQeBPoPLONRLpHghE cWdsR0etuAIMnRL3KlwUvfLr789Koi+WEO7RwGW0jHeOJ70GUKcFjnSzplqBRUYBRhT6 QP8IEuX2CtkcOTNnpXBR5MGZ0IWr8YG+CglWc/j5+G1oi8Hir+ky5riHmAiBgpzzzCL3 WDmeIljWzrU1rFQ9e3oINeWI9gqLxDH39hU6jNdl97UeioJbJwXvtYXCCKepzEEDLGHC BVFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1f5gPcrqfy750U/J52Wg+Mj+0/UcxsK3pfUKBKWLDR0=; b=2QXRjuaT8pIAFnhLw/EpRJyoEnKjAruzyyW7AurJlPdy9psqNAlu4Xx7oK6O+46SOg qc3oKjaKsxeaJmTAHsQsE0NZhYqURHSJuslHV+xI8CoqP+A49sYJz+wWPqqvl4qcemJb KJ9qDlflKnG3kggt/sWCideWivk7OMh65VQG6oLtVzKi0s1GkRfgNTEYOIyPguBrMR+j PLxumwHHIY2+TY5bjRWe2oPAQTCuYZy37nTgWEvJ5KZEMVYSVqX1keLH/RDkpzIkaqby et4iL6y+v3kTurNu5uKbjHZ713Q6PjAycy1hhfiQJCT+eCFkqkndXkCN3KAIq1wBk9Qb VN4g== X-Gm-Message-State: AOAM531aR5e5e0eYrjEn7YUVAgjOXc+jBQ22o+RmOq2PnvGQteEVIgGB tpLT9ghUzHaac7hCQHOnimb7XmqBWSbgi9SzYWFLwA== X-Google-Smtp-Source: ABdhPJwNHy+VN+JXLWCDADBmqbJunnYlm8shlOKM3wtyDi3l0Nvo++c+lLHsCuWeoJlsi7IJiccAZFbP2qFwzcnxW0Q= X-Received: by 2002:adf:eb86:0:b0:1e6:8c92:af6b with SMTP id t6-20020adfeb86000000b001e68c92af6bmr13872277wrn.116.1646780969857; Tue, 08 Mar 2022 15:09:29 -0800 (PST) MIME-Version: 1.0 References: <20220307184935.1704614-1-kaleshsingh@google.com> <20220307184935.1704614-2-kaleshsingh@google.com> In-Reply-To: From: Kalesh Singh Date: Tue, 8 Mar 2022 15:09:18 -0800 Message-ID: Subject: Re: [PATCH v5 1/8] KVM: arm64: Introduce hyp_alloc_private_va_range() To: Stephen Boyd Cc: "Cc: Android Kernel" , "moderated list:ARM64 PORT \(AARCH64 ARCHITECTURE\)" , Will Deacon , Peter Collingbourne , Marc Zyngier , LKML , "Madhavan T. Venkataraman" , Mark Brown , Masami Hiramatsu , Catalin Marinas , Suren Baghdasaryan , kvmarm X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu On Tue, Mar 8, 2022 at 12:21 PM Stephen Boyd wrote: > HI Stephen. Thanks for the review. > 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 > Ack > > + */ > > +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? I wanted to keep consistent between the pkvm and traditional nvhe code. I will refactor both *alloc_private_va_range() functions to take a pointer and return an int error if that's preferred. There would still be a case of this kind of cast in __pkvm_create_private_mapping() which does return an unsigned long address or ERR_PTR(...). It looks like it was made to return the address to facilitate use as a hypercall (@Quentin CMIW). > > > 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()? Good idea, will remove the extra cast. > > > + 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()? Ack > > > + 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; Agreed, I'll remove the goto in the next version. Thanks, Kalesh > > > } _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm 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 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C0C19C433F5 for ; Tue, 8 Mar 2022 23:10:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:Cc:To:Subject:Message-ID:Date:From: In-Reply-To:References:MIME-Version:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=TSmuEZ+n+G9CoVyOxAysj/3b/urPHEo5GZxHERHv5so=; b=jFc4AXZfKmHt34 O9t4tvov+uNJUGhJZtCPo9mLh9mKeOZybqzRVGHpDcDk6OglT3C08/xfHP9zvddXmBgMaSmSP2a+F 0aY6z6ZBKkdG9ChI2nq2Zv8DD3A9zxxWm3IMeMmsX65/htjLlCIJviGVXe+mHg3IlOmu45ODnPL+2 /r+DpgAi8EMOBVFAuWnq701WWPimUedUsVf7CXp5cNonIcUuKgBl0vpUuDoog0f7Kld0csZOcB2jL 6LoEYfIjj9aSnvIcriv/YvCve8kbaISr/wNsq4WsoTMv+/+aUtPPK3F5TmHs6zZLYKQt42L03oSRD GMqejzYG8fNfpjeY1MLQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRixW-006V0n-GQ; Tue, 08 Mar 2022 23:09:38 +0000 Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nRixT-006V02-2L for linux-arm-kernel@lists.infradead.org; Tue, 08 Mar 2022 23:09:36 +0000 Received: by mail-wr1-x429.google.com with SMTP id i8so393728wrr.8 for ; Tue, 08 Mar 2022 15:09:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=1f5gPcrqfy750U/J52Wg+Mj+0/UcxsK3pfUKBKWLDR0=; b=rY724zyNQvt0W7k77uJ6mdsljpHhmPedp6A01Ybgi4ape+kd29m4bzWSt+PlFv6VGk 5F64VH7NbTfh9lbiAtg/QqctdXYwNeto4qq8ILBsk1UjKlgBgsCMQeBPoPLONRLpHghE cWdsR0etuAIMnRL3KlwUvfLr789Koi+WEO7RwGW0jHeOJ70GUKcFjnSzplqBRUYBRhT6 QP8IEuX2CtkcOTNnpXBR5MGZ0IWr8YG+CglWc/j5+G1oi8Hir+ky5riHmAiBgpzzzCL3 WDmeIljWzrU1rFQ9e3oINeWI9gqLxDH39hU6jNdl97UeioJbJwXvtYXCCKepzEEDLGHC BVFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=1f5gPcrqfy750U/J52Wg+Mj+0/UcxsK3pfUKBKWLDR0=; b=yM4ugzHJQKmYWFhhV8mwwxIcEdWLQJKNnxcDSCj23FoEn01ZV6U4UjYrVRfadcsCTr VHJiP42kX4FmUcdVW1tDKxp/YDQYUPFg0ZZ+j6EfOpKOk4KnqJLICWjDAIctV9lM62N8 JMCyZgLYrfXOL5xnVz8NteQm0avEGMK7TcqRrSdRpdoLP4X9y5luFaJgHLQzwfjW3pzY RG1WIKRJ44f7zgI7bO6cdiNVBZttowPSDmRUpTqy6JbYXqtyL5mE056SstitBqiXDrol mTe1+yaB3OZrBJ2/aj6CDIxmQbCbmcUuIdqMCnAbuVTzUtPhc7kanorq4BXmefPZihPn bhPQ== X-Gm-Message-State: AOAM533PAt9R5pcGyMuwUJBKFXXSwen069DuyrkZvc6Gu+0xoQzvr25E uZq+QMlOUp4ReAgOCqomXjxDWsTYzz18GtEO04slEg== X-Google-Smtp-Source: ABdhPJwNHy+VN+JXLWCDADBmqbJunnYlm8shlOKM3wtyDi3l0Nvo++c+lLHsCuWeoJlsi7IJiccAZFbP2qFwzcnxW0Q= X-Received: by 2002:adf:eb86:0:b0:1e6:8c92:af6b with SMTP id t6-20020adfeb86000000b001e68c92af6bmr13872277wrn.116.1646780969857; Tue, 08 Mar 2022 15:09:29 -0800 (PST) MIME-Version: 1.0 References: <20220307184935.1704614-1-kaleshsingh@google.com> <20220307184935.1704614-2-kaleshsingh@google.com> In-Reply-To: From: Kalesh Singh Date: Tue, 8 Mar 2022 15:09:18 -0800 Message-ID: Subject: Re: [PATCH v5 1/8] KVM: arm64: Introduce hyp_alloc_private_va_range() To: Stephen Boyd Cc: Will Deacon , Marc Zyngier , Quentin Perret , Fuad Tabba , Suren Baghdasaryan , "Cc: Android Kernel" , James Morse , Alexandru Elisei , Suzuki K Poulose , Catalin Marinas , Mark Rutland , Mark Brown , Masami Hiramatsu , Peter Collingbourne , "Madhavan T. Venkataraman" , Andrew Scull , "moderated list:ARM64 PORT (AARCH64 ARCHITECTURE)" , kvmarm , LKML X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220308_150935_141036_86450DB4 X-CRM114-Status: GOOD ( 33.55 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Tue, Mar 8, 2022 at 12:21 PM Stephen Boyd wrote: > HI Stephen. Thanks for the review. > 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 > Ack > > + */ > > +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? I wanted to keep consistent between the pkvm and traditional nvhe code. I will refactor both *alloc_private_va_range() functions to take a pointer and return an int error if that's preferred. There would still be a case of this kind of cast in __pkvm_create_private_mapping() which does return an unsigned long address or ERR_PTR(...). It looks like it was made to return the address to facilitate use as a hypercall (@Quentin CMIW). > > > 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()? Good idea, will remove the extra cast. > > > + 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()? Ack > > > + 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; Agreed, I'll remove the goto in the next version. Thanks, Kalesh > > > } _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel