Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1483781pxb; Wed, 30 Mar 2022 04:46:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmYYQFGOWvi1jYxA4jodNa+IbohPzEQW+ptId/2+JCetHr300E5XvlO0REYoIm+siLbZWm X-Received: by 2002:a63:8343:0:b0:398:ab40:2de2 with SMTP id h64-20020a638343000000b00398ab402de2mr2241771pge.81.1648640810913; Wed, 30 Mar 2022 04:46:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648640810; cv=none; d=google.com; s=arc-20160816; b=TZZZgRUVlpQZeKyEqBXW7tUKa3ESiBA1tQL54hcC7UqJOojsB4ljJ05WHclTO3y900 wZJ53q22akEtkiFB9LLhhlKuWtl8/Om1EBhRa5q97GRVrg2dTG7bUjCsUvhbFbF9u7oO zpAGWzUlcY2Ht43l/Dt9KuN900fnaU2Sw9PKs8UgYP188gkkzgbGE1XcYt8IJLhxO9db AtvjLVIvhbn02D3MtygYQicDwEnRu2x0mcqQ/iyno3BdeMH3IcaLX/IaxrcFZ7wZJfSD 4eX/zcK+8SPIKDaKKuO8DeIKA5uRiE6SHQHTvzOCl3kaG3jLsK2igThB4110xXjs/QAm NkKw== 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:from:in-reply-to :references:mime-version:dkim-signature; bh=1zW5fOe+MFdbpvxDH6dhQKRRhQcdNFhyMSB/3QnzMXc=; b=XXCxbjSyyNcy0fu6G0wY10DiwrtAsNaYXriA0yMpz+WXzHKAYdCKXNw2l/z0O0PMMV r6G4cTUXp9U83zFfYPKIZSImvtqvx1BQTkFuw1P/c/inaVOLTqyjY6EF/+h5/cSWBFqE G0+rOkEfWqSwn0dl2707PW45ZjoAax9kBo6IGsnnIsu+szEbXCwo7eQ6zw2wkoRSEyYe tjtGL5n0E/6KcZm7lEtKADTw/HZ+Q4UqcHcRz4dih8SHSqcXcSO7A9VyeJic0QOV8VNu yRDTThlv/EwwMPjmvmBKPDvKoQa9aQ3Ix7/0QF1o9aP5wXK5jVfMKzG//C3WTKWPiCib X7Rg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=h3NI0Hgt; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y38-20020a056a00182600b004f70dcc690csi24171847pfa.1.2022.03.30.04.46.31; Wed, 30 Mar 2022 04:46:50 -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=@google.com header.s=20210112 header.b=h3NI0Hgt; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238889AbiC2Pj6 (ORCPT + 99 others); Tue, 29 Mar 2022 11:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41988 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237691AbiC2Pjz (ORCPT ); Tue, 29 Mar 2022 11:39:55 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7FCBC2527A5 for ; Tue, 29 Mar 2022 08:38:12 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id w21so20848936wra.2 for ; Tue, 29 Mar 2022 08:38:12 -0700 (PDT) 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=1zW5fOe+MFdbpvxDH6dhQKRRhQcdNFhyMSB/3QnzMXc=; b=h3NI0HgtUlwCcUgsYHILkSL9aU9skogREcS5hT/GLwzR9yahMv/qtWN8J35GRxI7YT olNlk3/1Ew7hbV0qaCpV0gIg5VwWx0W7PFNlAKI+hmfiYuqEMxe1Nb0pD95QiY1t4QGf wnJuv10rLacdDGLAlU5PPR763g0mwlwwlmZuvfMf+NXKo2Z8hIOpASwRzWPZdW8IG6EV qeAjrLuVZb1A3DY1HlR12lGWYik6kC21gGmtFSaQMlvbcDqFInn9j9fjORCpJUFBTLeO AMT3bcpfqvdf1AmpDAeARSXPqLZ01rhj0cBkZpuhFcC1cJGaTE4mbserwQW6wOSwrwGH 0ygg== 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=1zW5fOe+MFdbpvxDH6dhQKRRhQcdNFhyMSB/3QnzMXc=; b=O5QF1UF45iE0LuQMgmqrxIWshlqDVHAlMqr5TX5fnsLl/t2U9SRGReD8gqXdOJhfyO 5WNm6bg4wVRMhYbMWbgdCgBHtX2t0dNQsF7pz7WHPvAKYfb0+4acsWyajy0vgJ4WsOC+ 53B12L2VZp6Ijo68TZ5uu7US9phRmQIr7s/GcFe8OyiF5Ulk6qj2ZCQImGBWcSje936H yfPCXQgJTfIYxpPfOG+/VP2lL1CFpfpSOzbccfqePOOuhSpyFHsJ1fNFc/VqNwo/CPC3 K6UU5Bv5IKbA7MEMwA9mRFIDm34M7HPdM1R5pTiZkti0pDEa2qWTemF7se0V7TfRvh1E 0XNQ== X-Gm-Message-State: AOAM530yM59RitWx2Pm4qT8uvC6APkpb2YTrC8UHPP8XuU2Ooeo1drkE +S27GHSesu4cruWazS52vpcxIeVbtKgnuBGnI6CQrQ== X-Received: by 2002:a5d:528b:0:b0:203:d928:834c with SMTP id c11-20020a5d528b000000b00203d928834cmr32912325wrv.500.1648568288971; Tue, 29 Mar 2022 08:38:08 -0700 (PDT) MIME-Version: 1.0 References: <20220314200148.2695206-1-kaleshsingh@google.com> <20220314200148.2695206-2-kaleshsingh@google.com> In-Reply-To: From: Kalesh Singh Date: Tue, 29 Mar 2022 08:37:57 -0700 Message-ID: Subject: Re: [PATCH v6 1/8] KVM: arm64: Introduce hyp_alloc_private_va_range() To: Fuad Tabba Cc: Will Deacon , Marc Zyngier , Quentin Perret , 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" , Stephen Boyd , Andrew Scull , Andrew Jones , "moderated list:ARM64 PORT (AARCH64 ARCHITECTURE)" , kvmarm , LKML Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL 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 Tue, Mar 29, 2022 at 1:51 AM Fuad Tabba wrote: > > Hi Kalesh, > > > On Mon, Mar 14, 2022 at 8:02 PM 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 > > This looks good to me. I have also tested this entire series, and your > enhancements will make debugging easier. > > Tested-by: Fuad Tabba > Reviewed-by: Fuad Tabba Thanks for the reviews Fuad :) - Kalesh -Kalesh > > Thanks, > /fuad > > > > > --- > > > > 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 81839e9a8a24..3cc9aa25f510 100644 > > --- a/arch/arm64/include/asm/kvm_mmu.h > > +++ b/arch/arm64/include/asm/kvm_mmu.h > > @@ -153,6 +153,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 bc2aba953299..7326d683c500 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)) > > 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); > > + 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; > > } > > > > -- > > 2.35.1.723.g4982287a31-goog > >