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 X-Spam-Level: X-Spam-Status: No, score=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B77BDC433E0 for ; Fri, 5 Mar 2021 15:04:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 736ED65011 for ; Fri, 5 Mar 2021 15:04:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230070AbhCEPD7 (ORCPT ); Fri, 5 Mar 2021 10:03:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41660 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229672AbhCEPDl (ORCPT ); Fri, 5 Mar 2021 10:03:41 -0500 Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88195C061574 for ; Fri, 5 Mar 2021 07:03:40 -0800 (PST) Received: by mail-wm1-x333.google.com with SMTP id n22so1726909wmc.2 for ; Fri, 05 Mar 2021 07:03:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=GpKc+xvCgCJSH13ZTIZLN2AMkxZK13gtHVewqGBpJ68=; b=cDP6bYf/Jne/eaTwyPukya/ajVOlbRSFeM9DU5NkgFY1C1PufCEwfcmjD4flOs6vvQ cEXq4jeuNQ9SwRjtFAmF8obyb3gMoMTaAKh6efRRsmNNQfTCbE0OIEg4yl2PRe4BWq94 68tqXY756a6i6ineeXI+cO6KLABD1tkD0X33Vn11m9z8aKu5ToC3QPn/6v2buKjvnS6Y TbTrKWqjFHE8tW0/nt9TvX4zEF4yv1Jozi0/1i3+ZyguoIMJDww8jaK5s5drDILD7BsK sswTxLXiNvjLhxpVc+b4VWnNKKD8uVRNUiIilKtA6LZSyPmdQcH/Rw/MF9mj2rDNkhOE 7miw== 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; bh=GpKc+xvCgCJSH13ZTIZLN2AMkxZK13gtHVewqGBpJ68=; b=eYcBexaTFUn3tfoWvKEx0iflctu00wN4xSKeiKLmcahdrlcXWIohd29r59CJa0xBkC yiYtUfoW/s0iFCv1y1yfIcEKBAr3iNrVgk/fX1LAJK+I4ogVCYd4TkxTQiDGZnuKigGW GhfnvMKmSChwK5wHWJU6jlnx1vC9MvD1h5o4TFlG7txN0kNsigICdzF7vgmux/N+hlic lznrT6b/WnbcMrCvbc2DsLsNv5qm+cMkM5+mflJB7bE/gpR0A3ivI1fkvHCXZyUVeDcn 0t0MZPbg18ZR3aSXhFuTtMmpv34AyWr45+ERO3zpXkFWZYlNVKkzpZOWnzQbN6HxF+hh 6IMQ== X-Gm-Message-State: AOAM5327ThvG5WFbvmfvp9/TNoXviu7+IvWnyY4in+mjBbt0xrLZwJIw ZKOb8yiYX+fFnO5qOrmptFyTPg== X-Google-Smtp-Source: ABdhPJyIVygS7OGfQ/tnLuo3JNzZV/t4cpIfs6xJ3+AzFEa7C1ZYEN/vjbSjEeIhwx5P2PjxYvt8TA== X-Received: by 2002:a7b:c151:: with SMTP id z17mr9405485wmi.189.1614956619109; Fri, 05 Mar 2021 07:03:39 -0800 (PST) Received: from google.com (230.69.233.35.bc.googleusercontent.com. [35.233.69.230]) by smtp.gmail.com with ESMTPSA id v9sm4931146wrn.86.2021.03.05.07.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 07:03:38 -0800 (PST) Date: Fri, 5 Mar 2021 15:03:36 +0000 From: Quentin Perret To: Will Deacon Cc: catalin.marinas@arm.com, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, android-kvm@google.com, linux-kernel@vger.kernel.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, tabba@google.com, mark.rutland@arm.com, dbrazdil@google.com, mate.toth-pal@arm.com, seanjc@google.com, robh+dt@kernel.org Subject: Re: [PATCH v3 28/32] KVM: arm64: Add kvm_pgtable_stage2_idmap_greedy() Message-ID: References: <20210302150002.3685113-1-qperret@google.com> <20210302150002.3685113-29-qperret@google.com> <20210305143941.GA23017@willie-the-truck> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210305143941.GA23017@willie-the-truck> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Friday 05 Mar 2021 at 14:39:42 (+0000), Will Deacon wrote: > On Tue, Mar 02, 2021 at 02:59:58PM +0000, Quentin Perret wrote: > > +/** > > + * kvm_pgtable_stage2_idmap_greedy() - Identity-map an Intermediate Physical > > + * Address with a leaf entry at the highest > > + * possible level. > > Not sure it's worth mentioning "highest possible level" here, as > realistically the caller still has to provide a memcache to deal with the > worst case and the structure of the page-table shouldn't matter. Right, we need to pass a range so I suppose that should be enough to say 'this tries to cover large portions of memory'. > > + * @pgt: Page-table structure initialised by kvm_pgtable_*_init(). > > + * @addr: Input address to identity-map. > > + * @prot: Permissions and attributes for the mapping. > > + * @range: Boundaries of the maximum memory region to map. > > + * @mc: Cache of pre-allocated memory from which to allocate page-table > > + * pages. > > + * > > + * This function attempts to install high-level identity-mappings covering @addr > > "high-level"? (again, I think I'd just drop this) > > > + * without overriding existing mappings with incompatible permissions or > > + * attributes. An existing table entry may be coalesced into a block mapping > > + * if and only if it covers @addr and all its leafs are either invalid and/or > > s/leafs/leaf entries/ Ack for both. > > + * have permissions and attributes strictly matching @prot. The mapping is > > + * guaranteed to be contained within the boundaries specified by @range at call > > + * time. If only a subset of the memory specified by @range is mapped (because > > + * of e.g. alignment issues or existing incompatible mappings), @range will be > > + * updated accordingly. > > + * > > + * Return: 0 on success, negative error code on failure. > > + */ > > +int kvm_pgtable_stage2_idmap_greedy(struct kvm_pgtable *pgt, u64 addr, > > + enum kvm_pgtable_prot prot, > > + struct kvm_mem_range *range, > > + void *mc); > > #endif /* __ARM64_KVM_PGTABLE_H__ */ > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > > index 8aa01a9e2603..6897d771e2b2 100644 > > --- a/arch/arm64/kvm/hyp/pgtable.c > > +++ b/arch/arm64/kvm/hyp/pgtable.c > > @@ -987,3 +987,122 @@ void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt) > > pgt->mm_ops->free_pages_exact(pgt->pgd, pgd_sz); > > pgt->pgd = NULL; > > } > > + > > +struct stage2_reduce_range_data { > > + kvm_pte_t attr; > > + u64 target_addr; > > + u32 start_level; > > + struct kvm_mem_range *range; > > +}; > > + > > +static int __stage2_reduce_range(struct stage2_reduce_range_data *data, u64 addr) > > +{ > > + u32 level = data->start_level; > > + > > + for (; level < KVM_PGTABLE_MAX_LEVELS; level++) { > > + u64 granule = kvm_granule_size(level); > > + u64 start = ALIGN_DOWN(data->target_addr, granule); > > + u64 end = start + granule; > > + > > + /* > > + * The pinned address is in the current range, try one level > > + * deeper. > > + */ > > + if (start == ALIGN_DOWN(addr, granule)) > > + continue; > > + > > + /* > > + * Make sure the current range is a reduction of the existing > > + * range before updating it. > > + */ > > + if (data->range->start <= start && end <= data->range->end) { > > + data->start_level = level; > > + data->range->start = start; > > + data->range->end = end; > > + return 0; > > + } > > + } > > + > > + return -EINVAL; > > +} > > + > > +#define KVM_PTE_LEAF_S2_COMPAT_MASK (KVM_PTE_LEAF_ATTR_S2_PERMS | \ > > + KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | \ > > + KVM_PTE_LEAF_SW_BIT_PROT_NONE) > > + > > +static int stage2_reduce_range_walker(u64 addr, u64 end, u32 level, > > + kvm_pte_t *ptep, > > + enum kvm_pgtable_walk_flags flag, > > + void * const arg) > > +{ > > + struct stage2_reduce_range_data *data = arg; > > + kvm_pte_t attr; > > + int ret; > > + > > + if (addr < data->range->start || addr >= data->range->end) > > + return 0; > > + > > + attr = *ptep & KVM_PTE_LEAF_S2_COMPAT_MASK; > > + if (!attr || attr == data->attr) > > + return 0; > > + > > + /* > > + * An existing mapping with incompatible protection attributes is > > + * 'pinned', so reduce the range if we hit one. > > + */ > > + ret = __stage2_reduce_range(data, addr); > > + if (ret) > > + return ret; > > + > > + return -EAGAIN; > > +} > > + > > +static int stage2_reduce_range(struct kvm_pgtable *pgt, u64 addr, > > + enum kvm_pgtable_prot prot, > > + struct kvm_mem_range *range) > > +{ > > + struct stage2_reduce_range_data data = { > > + .start_level = pgt->start_level, > > + .range = range, > > + .target_addr = addr, > > + }; > > + struct kvm_pgtable_walker walker = { > > + .cb = stage2_reduce_range_walker, > > + .flags = KVM_PGTABLE_WALK_LEAF, > > + .arg = &data, > > + }; > > + int ret; > > + > > + data.attr = stage2_get_prot_attr(prot) & KVM_PTE_LEAF_S2_COMPAT_MASK; > > + if (!data.attr) > > + return -EINVAL; > > (this will need updating based on the other discussion we had) Ack. > > + /* Reduce the kvm_mem_range to a granule size */ > > + ret = __stage2_reduce_range(&data, range->end); > > + if (ret) > > + return ret; > > + > > + /* Walk the range to check permissions and reduce further if needed */ > > + do { > > + ret = kvm_pgtable_walk(pgt, range->start, range->end, &walker); > > (we spent some time debugging an issue here and you spotted that you're > passing range->end instead of the size ;) Yep, I have the fix applied locally, and ready to fly in v4 :) > > + } while (ret == -EAGAIN); > > I'm a bit nervous about this loop -- what guarantees forward progress here? > Can we return to the host after a few tries instead? -EAGAIN only happens when we've been able to successfully reduce the range to a potentially valid granule size. That can't happen infinitely. We're guaranteed to fail when trying to reduce the range to a granularity smaller than PAGE_SIZE (the -EINVAL case of __stage2_reduce_range), which is indicative of a host memory abort in a page it should not access (because marked PROT_NONE for instance). Cheers, Quentin 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 X-Spam-Level: X-Spam-Status: No, score=-8.5 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 66A9AC433DB for ; Fri, 5 Mar 2021 15:03:45 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id CDB6165016 for ; Fri, 5 Mar 2021 15:03:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CDB6165016 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvmarm-bounces@lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 4445A4B232; Fri, 5 Mar 2021 10:03:44 -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 jbY6iXrKcNaO; Fri, 5 Mar 2021 10:03:43 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 1D8544B29B; Fri, 5 Mar 2021 10:03:43 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id AC7B64B226 for ; Fri, 5 Mar 2021 10:03:41 -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 reYh8BAMnhBU for ; Fri, 5 Mar 2021 10:03:40 -0500 (EST) Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 495354B1E6 for ; Fri, 5 Mar 2021 10:03:40 -0500 (EST) Received: by mail-wm1-f43.google.com with SMTP id w203-20020a1c49d40000b029010c706d0642so6176844wma.0 for ; Fri, 05 Mar 2021 07:03:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=GpKc+xvCgCJSH13ZTIZLN2AMkxZK13gtHVewqGBpJ68=; b=cDP6bYf/Jne/eaTwyPukya/ajVOlbRSFeM9DU5NkgFY1C1PufCEwfcmjD4flOs6vvQ cEXq4jeuNQ9SwRjtFAmF8obyb3gMoMTaAKh6efRRsmNNQfTCbE0OIEg4yl2PRe4BWq94 68tqXY756a6i6ineeXI+cO6KLABD1tkD0X33Vn11m9z8aKu5ToC3QPn/6v2buKjvnS6Y TbTrKWqjFHE8tW0/nt9TvX4zEF4yv1Jozi0/1i3+ZyguoIMJDww8jaK5s5drDILD7BsK sswTxLXiNvjLhxpVc+b4VWnNKKD8uVRNUiIilKtA6LZSyPmdQcH/Rw/MF9mj2rDNkhOE 7miw== 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; bh=GpKc+xvCgCJSH13ZTIZLN2AMkxZK13gtHVewqGBpJ68=; b=ExM+OPvsvioRsqeI9oRy4T81TwRDO5LI7P4UiTugbphpHn+kbZTh8GfrgkivdaBuUT WA9qYH1BIu+t6EeN7hg3cBRjYfUxOj5L0UwxAFDTgbn1WDQEscz960ndD1b3XBSxipRc B2Tcl8dgV1ceD47y3WvN7eXWCrg7lPsDUSFqwFhnLoytT3h62ach7VEq7i8KJZmFEoiJ iG6GWh95+cfmxTGtzT6QtknmxCGyTvQNZi+eyLZfnrUoOWdYqO84E3ow9vX/EfRKeiX6 G+/iOH7o6rEx9lnWr+kyDdmgW7Zg8CGvGr0yn/q50+G9FWE4gnQr6pgD4suVf2xr49iD ZdZg== X-Gm-Message-State: AOAM531JHjvAet8Aze6SLpsN7/3bfoFBmuzZfUwgTzAKBC/v3ftNCkfc wtxsqsf9t4ZtAWkqpMgf7nuG1A== X-Google-Smtp-Source: ABdhPJyIVygS7OGfQ/tnLuo3JNzZV/t4cpIfs6xJ3+AzFEa7C1ZYEN/vjbSjEeIhwx5P2PjxYvt8TA== X-Received: by 2002:a7b:c151:: with SMTP id z17mr9405485wmi.189.1614956619109; Fri, 05 Mar 2021 07:03:39 -0800 (PST) Received: from google.com (230.69.233.35.bc.googleusercontent.com. [35.233.69.230]) by smtp.gmail.com with ESMTPSA id v9sm4931146wrn.86.2021.03.05.07.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 07:03:38 -0800 (PST) Date: Fri, 5 Mar 2021 15:03:36 +0000 From: Quentin Perret To: Will Deacon Subject: Re: [PATCH v3 28/32] KVM: arm64: Add kvm_pgtable_stage2_idmap_greedy() Message-ID: References: <20210302150002.3685113-1-qperret@google.com> <20210302150002.3685113-29-qperret@google.com> <20210305143941.GA23017@willie-the-truck> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210305143941.GA23017@willie-the-truck> Cc: android-kvm@google.com, catalin.marinas@arm.com, mate.toth-pal@arm.com, seanjc@google.com, tabba@google.com, linux-kernel@vger.kernel.org, robh+dt@kernel.org, linux-arm-kernel@lists.infradead.org, maz@kernel.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu 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 Friday 05 Mar 2021 at 14:39:42 (+0000), Will Deacon wrote: > On Tue, Mar 02, 2021 at 02:59:58PM +0000, Quentin Perret wrote: > > +/** > > + * kvm_pgtable_stage2_idmap_greedy() - Identity-map an Intermediate Physical > > + * Address with a leaf entry at the highest > > + * possible level. > > Not sure it's worth mentioning "highest possible level" here, as > realistically the caller still has to provide a memcache to deal with the > worst case and the structure of the page-table shouldn't matter. Right, we need to pass a range so I suppose that should be enough to say 'this tries to cover large portions of memory'. > > + * @pgt: Page-table structure initialised by kvm_pgtable_*_init(). > > + * @addr: Input address to identity-map. > > + * @prot: Permissions and attributes for the mapping. > > + * @range: Boundaries of the maximum memory region to map. > > + * @mc: Cache of pre-allocated memory from which to allocate page-table > > + * pages. > > + * > > + * This function attempts to install high-level identity-mappings covering @addr > > "high-level"? (again, I think I'd just drop this) > > > + * without overriding existing mappings with incompatible permissions or > > + * attributes. An existing table entry may be coalesced into a block mapping > > + * if and only if it covers @addr and all its leafs are either invalid and/or > > s/leafs/leaf entries/ Ack for both. > > + * have permissions and attributes strictly matching @prot. The mapping is > > + * guaranteed to be contained within the boundaries specified by @range at call > > + * time. If only a subset of the memory specified by @range is mapped (because > > + * of e.g. alignment issues or existing incompatible mappings), @range will be > > + * updated accordingly. > > + * > > + * Return: 0 on success, negative error code on failure. > > + */ > > +int kvm_pgtable_stage2_idmap_greedy(struct kvm_pgtable *pgt, u64 addr, > > + enum kvm_pgtable_prot prot, > > + struct kvm_mem_range *range, > > + void *mc); > > #endif /* __ARM64_KVM_PGTABLE_H__ */ > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > > index 8aa01a9e2603..6897d771e2b2 100644 > > --- a/arch/arm64/kvm/hyp/pgtable.c > > +++ b/arch/arm64/kvm/hyp/pgtable.c > > @@ -987,3 +987,122 @@ void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt) > > pgt->mm_ops->free_pages_exact(pgt->pgd, pgd_sz); > > pgt->pgd = NULL; > > } > > + > > +struct stage2_reduce_range_data { > > + kvm_pte_t attr; > > + u64 target_addr; > > + u32 start_level; > > + struct kvm_mem_range *range; > > +}; > > + > > +static int __stage2_reduce_range(struct stage2_reduce_range_data *data, u64 addr) > > +{ > > + u32 level = data->start_level; > > + > > + for (; level < KVM_PGTABLE_MAX_LEVELS; level++) { > > + u64 granule = kvm_granule_size(level); > > + u64 start = ALIGN_DOWN(data->target_addr, granule); > > + u64 end = start + granule; > > + > > + /* > > + * The pinned address is in the current range, try one level > > + * deeper. > > + */ > > + if (start == ALIGN_DOWN(addr, granule)) > > + continue; > > + > > + /* > > + * Make sure the current range is a reduction of the existing > > + * range before updating it. > > + */ > > + if (data->range->start <= start && end <= data->range->end) { > > + data->start_level = level; > > + data->range->start = start; > > + data->range->end = end; > > + return 0; > > + } > > + } > > + > > + return -EINVAL; > > +} > > + > > +#define KVM_PTE_LEAF_S2_COMPAT_MASK (KVM_PTE_LEAF_ATTR_S2_PERMS | \ > > + KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | \ > > + KVM_PTE_LEAF_SW_BIT_PROT_NONE) > > + > > +static int stage2_reduce_range_walker(u64 addr, u64 end, u32 level, > > + kvm_pte_t *ptep, > > + enum kvm_pgtable_walk_flags flag, > > + void * const arg) > > +{ > > + struct stage2_reduce_range_data *data = arg; > > + kvm_pte_t attr; > > + int ret; > > + > > + if (addr < data->range->start || addr >= data->range->end) > > + return 0; > > + > > + attr = *ptep & KVM_PTE_LEAF_S2_COMPAT_MASK; > > + if (!attr || attr == data->attr) > > + return 0; > > + > > + /* > > + * An existing mapping with incompatible protection attributes is > > + * 'pinned', so reduce the range if we hit one. > > + */ > > + ret = __stage2_reduce_range(data, addr); > > + if (ret) > > + return ret; > > + > > + return -EAGAIN; > > +} > > + > > +static int stage2_reduce_range(struct kvm_pgtable *pgt, u64 addr, > > + enum kvm_pgtable_prot prot, > > + struct kvm_mem_range *range) > > +{ > > + struct stage2_reduce_range_data data = { > > + .start_level = pgt->start_level, > > + .range = range, > > + .target_addr = addr, > > + }; > > + struct kvm_pgtable_walker walker = { > > + .cb = stage2_reduce_range_walker, > > + .flags = KVM_PGTABLE_WALK_LEAF, > > + .arg = &data, > > + }; > > + int ret; > > + > > + data.attr = stage2_get_prot_attr(prot) & KVM_PTE_LEAF_S2_COMPAT_MASK; > > + if (!data.attr) > > + return -EINVAL; > > (this will need updating based on the other discussion we had) Ack. > > + /* Reduce the kvm_mem_range to a granule size */ > > + ret = __stage2_reduce_range(&data, range->end); > > + if (ret) > > + return ret; > > + > > + /* Walk the range to check permissions and reduce further if needed */ > > + do { > > + ret = kvm_pgtable_walk(pgt, range->start, range->end, &walker); > > (we spent some time debugging an issue here and you spotted that you're > passing range->end instead of the size ;) Yep, I have the fix applied locally, and ready to fly in v4 :) > > + } while (ret == -EAGAIN); > > I'm a bit nervous about this loop -- what guarantees forward progress here? > Can we return to the host after a few tries instead? -EAGAIN only happens when we've been able to successfully reduce the range to a potentially valid granule size. That can't happen infinitely. We're guaranteed to fail when trying to reduce the range to a granularity smaller than PAGE_SIZE (the -EINVAL case of __stage2_reduce_range), which is indicative of a host memory abort in a page it should not access (because marked PROT_NONE for instance). Cheers, Quentin _______________________________________________ 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 X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7CC8BC433DB for ; Fri, 5 Mar 2021 15:07:36 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 103E56508F for ; Fri, 5 Mar 2021 15:07:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 103E56508F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Yk/C4vYpvt7e1/GNg0VyHIsuMpHJz89fTlsTKytXUAw=; b=CrT4yu+YnONZ50MAUOod+Q+ZY p8o5Y5Qk1/ZfElhDKhBv6qPYLv+OQt2bp21wyKSDcOMBLe/5iIMBDBMYFd8FMo38atqZssJIHpYYU HItGENUrQMsjF/Jmr4kOh8JFWNO4PB1+5QE/4ZXphhAXi3DlQBclfoX1h3gq3DoVZS/ykFFoS6mxr bcBNWoIROptXfUH5yX6EA5LxhmbfLuGkZCrhzai5hH6SimTnjPlAWlYkdtQs04tcr4uJfH1MXQ2GI c2yHCbMcxjBey253Zjcxb84eLgyuvmD3wZzskeDBl1b4fw/LTSgSGS2JXH4MfwLSqaMoStW3ri1Rb t+F/RM6Zg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lIC0O-00FNl2-Ud; Fri, 05 Mar 2021 15:04:41 +0000 Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lIBzQ-00FNfS-1c for linux-arm-kernel@lists.infradead.org; Fri, 05 Mar 2021 15:03:47 +0000 Received: by mail-wm1-x32e.google.com with SMTP id l22so1728876wme.1 for ; Fri, 05 Mar 2021 07:03:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=GpKc+xvCgCJSH13ZTIZLN2AMkxZK13gtHVewqGBpJ68=; b=cDP6bYf/Jne/eaTwyPukya/ajVOlbRSFeM9DU5NkgFY1C1PufCEwfcmjD4flOs6vvQ cEXq4jeuNQ9SwRjtFAmF8obyb3gMoMTaAKh6efRRsmNNQfTCbE0OIEg4yl2PRe4BWq94 68tqXY756a6i6ineeXI+cO6KLABD1tkD0X33Vn11m9z8aKu5ToC3QPn/6v2buKjvnS6Y TbTrKWqjFHE8tW0/nt9TvX4zEF4yv1Jozi0/1i3+ZyguoIMJDww8jaK5s5drDILD7BsK sswTxLXiNvjLhxpVc+b4VWnNKKD8uVRNUiIilKtA6LZSyPmdQcH/Rw/MF9mj2rDNkhOE 7miw== 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; bh=GpKc+xvCgCJSH13ZTIZLN2AMkxZK13gtHVewqGBpJ68=; b=H1Xs4KfmFMyBg5hGMmgHrYvoVFh1wklzhXZ0oserDCDe9cpG2t8bzHeAaTUsoD4VJG bcEg5FqGAuQTIT5Wh4AqTwD0x8h+9jN7LqLqFPqq7OLNDEA+fkJCKVoEisgge36i4X1R OCbadkN6KPLP+VcS6q+eUwy430oiDXrUpHyUt88F+Qy7jiFQkfDlX1PgDLa73O+a07Qf Knxg567RXSgx82lK3CaagqsNmPXMPDOQu7MNLqzBGwKYJTJ2NQ/HKt20OBJRAXUCbNrG SQKDXt0gq3pJkCao8hV9tMlRf7z1q2x7OelyQZmB3SbJZOCner5ZUFjjPlY/yArQYPK9 RaTQ== X-Gm-Message-State: AOAM531F47EvlzaHoCTnRG6GgMQpoysMgpWAyqv/qnhUy7OAhJem2S8i kTzbThpAjIdkcUg/ZBZMsHbnaQ== X-Google-Smtp-Source: ABdhPJyIVygS7OGfQ/tnLuo3JNzZV/t4cpIfs6xJ3+AzFEa7C1ZYEN/vjbSjEeIhwx5P2PjxYvt8TA== X-Received: by 2002:a7b:c151:: with SMTP id z17mr9405485wmi.189.1614956619109; Fri, 05 Mar 2021 07:03:39 -0800 (PST) Received: from google.com (230.69.233.35.bc.googleusercontent.com. [35.233.69.230]) by smtp.gmail.com with ESMTPSA id v9sm4931146wrn.86.2021.03.05.07.03.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 05 Mar 2021 07:03:38 -0800 (PST) Date: Fri, 5 Mar 2021 15:03:36 +0000 From: Quentin Perret To: Will Deacon Cc: catalin.marinas@arm.com, maz@kernel.org, james.morse@arm.com, julien.thierry.kdev@gmail.com, suzuki.poulose@arm.com, android-kvm@google.com, linux-kernel@vger.kernel.org, kernel-team@android.com, kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, tabba@google.com, mark.rutland@arm.com, dbrazdil@google.com, mate.toth-pal@arm.com, seanjc@google.com, robh+dt@kernel.org Subject: Re: [PATCH v3 28/32] KVM: arm64: Add kvm_pgtable_stage2_idmap_greedy() Message-ID: References: <20210302150002.3685113-1-qperret@google.com> <20210302150002.3685113-29-qperret@google.com> <20210305143941.GA23017@willie-the-truck> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20210305143941.GA23017@willie-the-truck> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210305_150344_816075_DA46671D X-CRM114-Status: GOOD ( 40.21 ) 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 Friday 05 Mar 2021 at 14:39:42 (+0000), Will Deacon wrote: > On Tue, Mar 02, 2021 at 02:59:58PM +0000, Quentin Perret wrote: > > +/** > > + * kvm_pgtable_stage2_idmap_greedy() - Identity-map an Intermediate Physical > > + * Address with a leaf entry at the highest > > + * possible level. > > Not sure it's worth mentioning "highest possible level" here, as > realistically the caller still has to provide a memcache to deal with the > worst case and the structure of the page-table shouldn't matter. Right, we need to pass a range so I suppose that should be enough to say 'this tries to cover large portions of memory'. > > + * @pgt: Page-table structure initialised by kvm_pgtable_*_init(). > > + * @addr: Input address to identity-map. > > + * @prot: Permissions and attributes for the mapping. > > + * @range: Boundaries of the maximum memory region to map. > > + * @mc: Cache of pre-allocated memory from which to allocate page-table > > + * pages. > > + * > > + * This function attempts to install high-level identity-mappings covering @addr > > "high-level"? (again, I think I'd just drop this) > > > + * without overriding existing mappings with incompatible permissions or > > + * attributes. An existing table entry may be coalesced into a block mapping > > + * if and only if it covers @addr and all its leafs are either invalid and/or > > s/leafs/leaf entries/ Ack for both. > > + * have permissions and attributes strictly matching @prot. The mapping is > > + * guaranteed to be contained within the boundaries specified by @range at call > > + * time. If only a subset of the memory specified by @range is mapped (because > > + * of e.g. alignment issues or existing incompatible mappings), @range will be > > + * updated accordingly. > > + * > > + * Return: 0 on success, negative error code on failure. > > + */ > > +int kvm_pgtable_stage2_idmap_greedy(struct kvm_pgtable *pgt, u64 addr, > > + enum kvm_pgtable_prot prot, > > + struct kvm_mem_range *range, > > + void *mc); > > #endif /* __ARM64_KVM_PGTABLE_H__ */ > > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > > index 8aa01a9e2603..6897d771e2b2 100644 > > --- a/arch/arm64/kvm/hyp/pgtable.c > > +++ b/arch/arm64/kvm/hyp/pgtable.c > > @@ -987,3 +987,122 @@ void kvm_pgtable_stage2_destroy(struct kvm_pgtable *pgt) > > pgt->mm_ops->free_pages_exact(pgt->pgd, pgd_sz); > > pgt->pgd = NULL; > > } > > + > > +struct stage2_reduce_range_data { > > + kvm_pte_t attr; > > + u64 target_addr; > > + u32 start_level; > > + struct kvm_mem_range *range; > > +}; > > + > > +static int __stage2_reduce_range(struct stage2_reduce_range_data *data, u64 addr) > > +{ > > + u32 level = data->start_level; > > + > > + for (; level < KVM_PGTABLE_MAX_LEVELS; level++) { > > + u64 granule = kvm_granule_size(level); > > + u64 start = ALIGN_DOWN(data->target_addr, granule); > > + u64 end = start + granule; > > + > > + /* > > + * The pinned address is in the current range, try one level > > + * deeper. > > + */ > > + if (start == ALIGN_DOWN(addr, granule)) > > + continue; > > + > > + /* > > + * Make sure the current range is a reduction of the existing > > + * range before updating it. > > + */ > > + if (data->range->start <= start && end <= data->range->end) { > > + data->start_level = level; > > + data->range->start = start; > > + data->range->end = end; > > + return 0; > > + } > > + } > > + > > + return -EINVAL; > > +} > > + > > +#define KVM_PTE_LEAF_S2_COMPAT_MASK (KVM_PTE_LEAF_ATTR_S2_PERMS | \ > > + KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR | \ > > + KVM_PTE_LEAF_SW_BIT_PROT_NONE) > > + > > +static int stage2_reduce_range_walker(u64 addr, u64 end, u32 level, > > + kvm_pte_t *ptep, > > + enum kvm_pgtable_walk_flags flag, > > + void * const arg) > > +{ > > + struct stage2_reduce_range_data *data = arg; > > + kvm_pte_t attr; > > + int ret; > > + > > + if (addr < data->range->start || addr >= data->range->end) > > + return 0; > > + > > + attr = *ptep & KVM_PTE_LEAF_S2_COMPAT_MASK; > > + if (!attr || attr == data->attr) > > + return 0; > > + > > + /* > > + * An existing mapping with incompatible protection attributes is > > + * 'pinned', so reduce the range if we hit one. > > + */ > > + ret = __stage2_reduce_range(data, addr); > > + if (ret) > > + return ret; > > + > > + return -EAGAIN; > > +} > > + > > +static int stage2_reduce_range(struct kvm_pgtable *pgt, u64 addr, > > + enum kvm_pgtable_prot prot, > > + struct kvm_mem_range *range) > > +{ > > + struct stage2_reduce_range_data data = { > > + .start_level = pgt->start_level, > > + .range = range, > > + .target_addr = addr, > > + }; > > + struct kvm_pgtable_walker walker = { > > + .cb = stage2_reduce_range_walker, > > + .flags = KVM_PGTABLE_WALK_LEAF, > > + .arg = &data, > > + }; > > + int ret; > > + > > + data.attr = stage2_get_prot_attr(prot) & KVM_PTE_LEAF_S2_COMPAT_MASK; > > + if (!data.attr) > > + return -EINVAL; > > (this will need updating based on the other discussion we had) Ack. > > + /* Reduce the kvm_mem_range to a granule size */ > > + ret = __stage2_reduce_range(&data, range->end); > > + if (ret) > > + return ret; > > + > > + /* Walk the range to check permissions and reduce further if needed */ > > + do { > > + ret = kvm_pgtable_walk(pgt, range->start, range->end, &walker); > > (we spent some time debugging an issue here and you spotted that you're > passing range->end instead of the size ;) Yep, I have the fix applied locally, and ready to fly in v4 :) > > + } while (ret == -EAGAIN); > > I'm a bit nervous about this loop -- what guarantees forward progress here? > Can we return to the host after a few tries instead? -EAGAIN only happens when we've been able to successfully reduce the range to a potentially valid granule size. That can't happen infinitely. We're guaranteed to fail when trying to reduce the range to a granularity smaller than PAGE_SIZE (the -EINVAL case of __stage2_reduce_range), which is indicative of a host memory abort in a page it should not access (because marked PROT_NONE for instance). Cheers, Quentin _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel