Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp1384264pxb; Thu, 4 Mar 2021 09:56:41 -0800 (PST) X-Google-Smtp-Source: ABdhPJxzFxlv7c/TI55N9DEs/36Qq+gPK+wY8BjC/NG/dCSl5vhNNK5Cbt3NU7o4ETKteUxtrVbn X-Received: by 2002:a17:906:3496:: with SMTP id g22mr5728680ejb.143.1614880601309; Thu, 04 Mar 2021 09:56:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614880601; cv=none; d=google.com; s=arc-20160816; b=N8s+Mch8YHCtzP6NwpRsq9091kwMhPQiiHxVNd2/J6aKOIk6gxDgSjiTHpLIY21l5+ ZqVh/eKOqv1Z/dhW8w303Deh0esisroYpBRNvJL8xc5sSuhMLXWXhG2RvNihS+VIKHS6 tcBMZnP6ZLMksxITOUBIBzpggpsqIeytlH9TSqj8DgUQYW7i0TfX6bK8Y94R6n/3dJ/o IcKttEQ7CbY4hHRJPPwxSu3Ng57NgH1n/SUUGZ5JKh2Rr/Hvm28vp5XCj5Fl7mpghk8r X1Y/xa29+zHrIlxF6ZfWBQGSwUrYd25oh79BeDFdsjhV1utTR7wzIXyRs3zlYb6eCfNC doDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=dDTbsRqRQQm1gWf6AW6J7Y+PcV7VEoVbL54pZnYxhjk=; b=ttmykMOnuRHvsuHFgsxUbUtdtIzxO9XefOZReUSH9zvjCOwAHtdI9t7ejI3Btvj1Ks pJlz9zVL4+wGXEcLwgPMgRsPENuEMxKRwLHbNYYVNH1ixC4WGWVWflj0j33He65QLgxv hsjwMZ1axQJhyaUuBvzNGZMLztrQj2aVhw7CPMVffP/God7SvbuHSazlekccmJ+U9nrf DriShnu7wP7bG2o10zEKUyXGRRfxwrO6Ita6WabBtS01DpNwUZSsYuod5c1cT/lQnN7h 3BaKmyqTpR1pvRFO5p2h13RosCDVnyRweh3VhvcCv4mDBDzBnPJgkGeCIoEnDSQAKyfJ 7toA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LADuN40E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t13si17138842eje.700.2021.03.04.09.56.18; Thu, 04 Mar 2021 09:56:41 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=LADuN40E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S233950AbhCDPbc (ORCPT + 99 others); Thu, 4 Mar 2021 10:31:32 -0500 Received: from mail.kernel.org ([198.145.29.99]:43680 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232171AbhCDPbX (ORCPT ); Thu, 4 Mar 2021 10:31:23 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id F057A64F21; Thu, 4 Mar 2021 15:30:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1614871842; bh=zcvP/xRNovmcUNqEXMV6SKPJwphc6KAygaqAb/6nSsM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=LADuN40EQStLkH6O6LUzMIxhc/aLZYRwZKhO9mkajUqlnhNqVCpweEwDTWfe/rlFO 1uQCI+qt3ZZN1DOkNmnQe1WEpAOpg+1xC++v9qwF9uND09qh5dh9soS9HnTO9ml74b b19FJ4Ma/0z5HrqtqOZrTUhKPKI4qgfRfgkAnyix0VgknVwyKyMu04vrSE44IXyPCg BmPKyobfvS7+/1bL+Ue+5VOJAyrGb4U1Im9vvRkmZkXTxt6xqmXsmb9bncj+iQeQnP WgKxC+ZQ7pQPsKNNYzx7rkiyzoluF2q4M3OwXSoy8LrssD3fUB8E4IrcEcf4ZCzi3V zhLZ8/pyh3I3A== Date: Thu, 4 Mar 2021 15:30:36 +0000 From: Will Deacon To: Quentin Perret 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 12/32] KVM: arm64: Introduce a Hyp buddy page allocator Message-ID: <20210304153036.GA21507@willie-the-truck> References: <20210302150002.3685113-1-qperret@google.com> <20210302150002.3685113-13-qperret@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210302150002.3685113-13-qperret@google.com> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 02, 2021 at 02:59:42PM +0000, Quentin Perret wrote: > When memory protection is enabled, the hyp code will require a basic > form of memory management in order to allocate and free memory pages at > EL2. This is needed for various use-cases, including the creation of hyp > mappings or the allocation of stage 2 page tables. > > To address these use-case, introduce a simple memory allocator in the > hyp code. The allocator is designed as a conventional 'buddy allocator', > working with a page granularity. It allows to allocate and free > physically contiguous pages from memory 'pools', with a guaranteed order > alignment in the PA space. Each page in a memory pool is associated > with a struct hyp_page which holds the page's metadata, including its > refcount, as well as its current order, hence mimicking the kernel's > buddy system in the GFP infrastructure. The hyp_page metadata are made > accessible through a hyp_vmemmap, following the concept of > SPARSE_VMEMMAP in the kernel. > > Signed-off-by: Quentin Perret > --- > arch/arm64/kvm/hyp/include/nvhe/gfp.h | 55 +++++++ > arch/arm64/kvm/hyp/include/nvhe/memory.h | 28 ++++ > arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- > arch/arm64/kvm/hyp/nvhe/page_alloc.c | 195 +++++++++++++++++++++++ > 4 files changed, 279 insertions(+), 1 deletion(-) > create mode 100644 arch/arm64/kvm/hyp/include/nvhe/gfp.h > create mode 100644 arch/arm64/kvm/hyp/nvhe/page_alloc.c [...] > +static void __hyp_attach_page(struct hyp_pool *pool, > + struct hyp_page *p) > +{ > + unsigned int order = p->order; > + struct hyp_page *buddy; > + > + memset(hyp_page_to_virt(p), 0, PAGE_SIZE << p->order); > + > + /* > + * Only the first struct hyp_page of a high-order page (otherwise known > + * as the 'head') should have p->order set. The non-head pages should > + * have p->order = HYP_NO_ORDER. Here @p may no longer be the head > + * after coallescing, so make sure to mark it HYP_NO_ORDER proactively. > + */ > + p->order = HYP_NO_ORDER; > + for (; (order + 1) < pool->max_order; order++) { > + buddy = __find_buddy_avail(pool, p, order); > + if (!buddy) > + break; > + > + /* Take the buddy out of its list, and coallesce with @p */ > + list_del_init(&buddy->node); > + buddy->order = HYP_NO_ORDER; > + p = (p < buddy) ? p : buddy; nit: this is min() > + } > + > + /* Mark the new head, and insert it */ > + p->order = order; > + list_add_tail(&p->node, &pool->free_area[order]); > +} > + > +static void hyp_attach_page(struct hyp_page *p) > +{ > + struct hyp_pool *pool = hyp_page_to_pool(p); > + > + hyp_spin_lock(&pool->lock); > + __hyp_attach_page(pool, p); > + hyp_spin_unlock(&pool->lock); > +} > + > +static struct hyp_page *__hyp_extract_page(struct hyp_pool *pool, > + struct hyp_page *p, > + unsigned int order) > +{ > + struct hyp_page *buddy; > + > + list_del_init(&p->node); > + while (p->order > order) { > + /* > + * The buddy of order n - 1 currently has HYP_NO_ORDER as it > + * is covered by a higher-level page (whose head is @p). Use > + * __find_buddy_nocheck() to find it and inject it in the > + * free_list[n - 1], effectively splitting @p in half. > + */ > + p->order--; > + buddy = __find_buddy_nocheck(pool, p, p->order); > + buddy->order = p->order; > + list_add_tail(&buddy->node, &pool->free_area[buddy->order]); > + } > + > + return p; > +} > + > +void hyp_put_page(void *addr) > +{ > + struct hyp_page *p = hyp_virt_to_page(addr); > + > + if (hyp_page_ref_dec_and_test(p)) > + hyp_attach_page(p); > +} > + > +void hyp_get_page(void *addr) > +{ > + struct hyp_page *p = hyp_virt_to_page(addr); > + > + hyp_page_ref_inc(p); > +} > + > +void *hyp_alloc_pages(struct hyp_pool *pool, unsigned int order) > +{ > + unsigned int i = order; > + struct hyp_page *p; > + > + hyp_spin_lock(&pool->lock); > + > + /* Look for a high-enough-order page */ > + while (i < pool->max_order && list_empty(&pool->free_area[i])) > + i++; > + if (i >= pool->max_order) { > + hyp_spin_unlock(&pool->lock); > + return NULL; > + } > + > + /* Extract it from the tree at the right order */ > + p = list_first_entry(&pool->free_area[i], struct hyp_page, node); > + p = __hyp_extract_page(pool, p, order); > + > + hyp_spin_unlock(&pool->lock); > + hyp_page_ref_inc(p); I find this a little scary, as we momentarily drop the lock. It think it's ok because the reference count on the page must be 0 at this point, but actually then I think it would be clearer to have a hyp_page_ref_init() function which could take the lock, check that the refcount is indeed 0 and then set it to 1. What do you think? Will