Received: by 2002:a05:6a10:d5a5:0:0:0:0 with SMTP id gn37csp239804pxb; Wed, 6 Oct 2021 04:06:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDste/BgFTPuiLRSIGmLS+D27zbcuL//aHH9rJmfFEGVRvU4AOkLo+cKKsSwkd435IKzFp X-Received: by 2002:a17:907:c0c:: with SMTP id ga12mr31158355ejc.437.1633518379512; Wed, 06 Oct 2021 04:06:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1633518379; cv=none; d=google.com; s=arc-20160816; b=U0q52Q4c6buC/xUJvb8pmI1A+zrh5RlSUzBA2ljk6H1ABWftSfHTtODdF52y2824wb pQFvVCg+jtNJ6HJKgJBaRON/Fd/QsVNAg1NWQVk8p8/mk9KC5uWXOrNuuLZsfH9un1Dc uZorNQ+Rku7Xplfd4x7s3iJzYek9V6o7msK0ll6z8WgUhCKKxMTdz8Rn+GTAxCiMQqNQ PsmLKFJWP9Ih9Gy5zca5A1VkLjk3tvrm/M4czj3ildnlZlkHvjw8fEF/Gcc+Fv2YwpGX 1oTWQhrdIP5g6z+5gQ1WaLQsg/fKB9f/ydj5QKuizaTE1EMzPEo7YvCiZn2OKdRVawJU Qtmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=9tbrYH7ajTBp7O46tjzPCaXKNyHz+YBXLs6DxUMAvmU=; b=BdxJ1ekBo2wYh/Vvs+SfK7UWnY6V/Zdpv2K6FFhuZ63wu5jLpwMsc5Li/8UZVGmEMi b3q5ZG6sYdphZC2C6c/AcqwQAjAHzspCaPbUxBdgY/oDdQLmCvZP72Fgx08VE3IqX0L+ BvR1dMBXs3+mf5Xq9HwV9iD0MKCIZJXN8cZyjiXhtGOkwOrAuslBEEPoF6ocsmfo6zOf DFO0U6wyg0WCXbn+lsEHTFxOukaKQ+WLeQMokK2Z0BbzGaGw4VbCjOAWpMiCGfOSVD7R PgvQRyWJYtenEXNRTNOi7ST/vDfOeZrDH3wOeg1WuxC1k3y3kVrtVjP0WCxyizO9gp7R D1DA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=CH+MV79Z; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bo17si21140049ejb.766.2021.10.06.04.05.36; Wed, 06 Oct 2021 04:06:19 -0700 (PDT) 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=@redhat.com header.s=mimecast20190719 header.b=CH+MV79Z; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238136AbhJFLEJ (ORCPT + 99 others); Wed, 6 Oct 2021 07:04:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:46455 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237843AbhJFLEI (ORCPT ); Wed, 6 Oct 2021 07:04:08 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1633518136; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=9tbrYH7ajTBp7O46tjzPCaXKNyHz+YBXLs6DxUMAvmU=; b=CH+MV79ZPdJhE2OiUEufeMQ0eXZlfoI0jOCWGGiIRJYIyZuqtQkRCRtxuL5dOVfZfHu+7b oB/xMTIdMEE47I3KNWJae/oWmnQgkSUPqQYXpqxky2qVOOd2dIejAVXKgyt40YsfpR0+04 mnIKGU+JCfwChYlliRspzlnyxnd2Zqg= Received: from mail-ed1-f69.google.com (mail-ed1-f69.google.com [209.85.208.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-473-CY9fnMA8Nxu2SigwEZu7qg-1; Wed, 06 Oct 2021 07:02:15 -0400 X-MC-Unique: CY9fnMA8Nxu2SigwEZu7qg-1 Received: by mail-ed1-f69.google.com with SMTP id g28-20020a50d0dc000000b003dae69dfe3aso2284550edf.7 for ; Wed, 06 Oct 2021 04:02:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=9tbrYH7ajTBp7O46tjzPCaXKNyHz+YBXLs6DxUMAvmU=; b=m5bkQMB9SdDFI+MaWrYlevK6BTFyer22d8SeOL1z+3WvH1aAkft22j9tWtYqTPdlPs c43YqG7lQXWT7YULjfQ8Ux60yAvMkJP85pzejmOqdxk/3ViT5Lv+F6dMIFhTLIJl7hUS aGLLkIa+axpGhVEz+WrXu+obP5tMvnWZrJMtkom3+9aZyfT6IN7gyXoMJCLbFbYA1nec 7ascFJ5TKV1qH3X5u3GNTch4i/HNdvTlhoUBNvr3qlprcMi9daPT1gSBLco0GQV48kv7 JM4MgaH+m/oYynnRekmB1mLJm++nAXih/Mc2jeMaUtYjRNR1zbF+1up8rSWnWVHtCfOS nxAw== X-Gm-Message-State: AOAM531XocdkqyUV5R7DBu6m2Vn2JYt1mejn8BBxT9PZgh6oCuRu1dDK 16txkIMrWtG7HWyd74obmhq1fKYTOLAetLh24e7J7BNuvdEmEL2paVmNzoZlYT8cuv7+sI5h3Kr R28f3OR82IX6pTlOM3Wxy+CEJ X-Received: by 2002:a17:906:2a8e:: with SMTP id l14mr31357893eje.321.1633518134177; Wed, 06 Oct 2021 04:02:14 -0700 (PDT) X-Received: by 2002:a17:906:2a8e:: with SMTP id l14mr31357872eje.321.1633518133892; Wed, 06 Oct 2021 04:02:13 -0700 (PDT) Received: from gator.home (cst2-174-28.cust.vodafone.cz. [31.30.174.28]) by smtp.gmail.com with ESMTPSA id u19sm10114294edv.40.2021.10.06.04.02.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Oct 2021 04:02:13 -0700 (PDT) Date: Wed, 6 Oct 2021 13:02:11 +0200 From: Andrew Jones To: Marc Zyngier Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, will@kernel.org, qperret@google.com, dbrazdil@google.com, Steven Price , Fuad Tabba , Srivatsa Vaddagiri , Shanker R Donthineni , James Morse , Suzuki K Poulose , Alexandru Elisei , kernel-team@android.com Subject: Re: [PATCH v2 03/16] KVM: arm64: Turn kvm_pgtable_stage2_set_owner into kvm_pgtable_stage2_annotate Message-ID: <20211006110211.y6kzmjlzgardmwif@gator.home> References: <20211004174849.2831548-1-maz@kernel.org> <20211004174849.2831548-4-maz@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20211004174849.2831548-4-maz@kernel.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 04, 2021 at 06:48:36PM +0100, Marc Zyngier wrote: > kvm_pgtable_stage2_set_owner() could be generalised into a way > to store up to 63 bits in the page tables, as long as we don't > set bit 0. > > Let's just do that. > > Signed-off-by: Marc Zyngier > --- > arch/arm64/include/asm/kvm_pgtable.h | 12 ++++++----- > arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 2 +- > arch/arm64/kvm/hyp/nvhe/mem_protect.c | 11 ++++------ > arch/arm64/kvm/hyp/nvhe/setup.c | 10 +++++++++- > arch/arm64/kvm/hyp/pgtable.c | 20 ++++++------------- > 5 files changed, 27 insertions(+), 28 deletions(-) > > diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h > index 027783829584..d4d3ae0b5edb 100644 > --- a/arch/arm64/include/asm/kvm_pgtable.h > +++ b/arch/arm64/include/asm/kvm_pgtable.h > @@ -329,14 +329,16 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, > void *mc); > > /** > - * kvm_pgtable_stage2_set_owner() - Unmap and annotate pages in the IPA space to > - * track ownership. > + * kvm_pgtable_stage2_annotate() - Unmap and annotate pages in the IPA space > + * to track ownership (and more). > * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*(). > * @addr: Base intermediate physical address to annotate. > * @size: Size of the annotated range. > * @mc: Cache of pre-allocated and zeroed memory from which to allocate > * page-table pages. > - * @owner_id: Unique identifier for the owner of the page. > + * @annotation: A 63 bit value that will be stored in the page tables. > + * @annotation[0] must be 0, and @annotation[63:1] is stored > + * in the page tables. > * > * By default, all page-tables are owned by identifier 0. This function can be > * used to mark portions of the IPA space as owned by other entities. When a > @@ -345,8 +347,8 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, > * > * Return: 0 on success, negative error code on failure. > */ > -int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, > - void *mc, u8 owner_id); > +int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size, > + void *mc, kvm_pte_t annotation); > > /** > * kvm_pgtable_stage2_unmap() - Remove a mapping from a guest stage-2 page-table. > diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > index b58c910babaf..9d2ca173ea9a 100644 > --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h > @@ -53,7 +53,7 @@ int __pkvm_host_share_hyp(u64 pfn); > > bool addr_is_memory(phys_addr_t phys); > int host_stage2_idmap_locked(phys_addr_t addr, u64 size, enum kvm_pgtable_prot prot); > -int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id); > +int host_stage2_annotate_locked(phys_addr_t addr, u64 size, kvm_pte_t owner_id); > int kvm_host_prepare_stage2(void *pgt_pool_base); > void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt); > > diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > index bacd493a4eac..8cd0c3bdb911 100644 > --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c > +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c > @@ -286,17 +286,14 @@ static int host_stage2_adjust_range(u64 addr, struct kvm_mem_range *range) > int host_stage2_idmap_locked(phys_addr_t addr, u64 size, > enum kvm_pgtable_prot prot) > { > - hyp_assert_lock_held(&host_kvm.lock); > - > return host_stage2_try(__host_stage2_idmap, addr, addr + size, prot); > } > > -int host_stage2_set_owner_locked(phys_addr_t addr, u64 size, u8 owner_id) > +int host_stage2_annotate_locked(phys_addr_t addr, u64 size, > + kvm_pte_t annotation) > { > - hyp_assert_lock_held(&host_kvm.lock); Hi Marc, Why are the lock asserts getting dropped? > - > - return host_stage2_try(kvm_pgtable_stage2_set_owner, &host_kvm.pgt, > - addr, size, &host_s2_pool, owner_id); > + return host_stage2_try(kvm_pgtable_stage2_annotate, &host_kvm.pgt, > + addr, size, &host_s2_pool, annotation); > } > > static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot) > diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c > index 57c27846320f..d489fb9b8c29 100644 > --- a/arch/arm64/kvm/hyp/nvhe/setup.c > +++ b/arch/arm64/kvm/hyp/nvhe/setup.c > @@ -159,6 +159,13 @@ static void hpool_put_page(void *addr) > hyp_put_page(&hpool, addr); > } > > +#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) > + > +static kvm_pte_t kvm_init_invalid_leaf_owner(u8 owner_id) > +{ > + return FIELD_PREP(KVM_INVALID_PTE_OWNER_MASK, owner_id); > +} > + > static int finalize_host_mappings_walker(u64 addr, u64 end, u32 level, > kvm_pte_t *ptep, > enum kvm_pgtable_walk_flags flag, > @@ -186,7 +193,8 @@ static int finalize_host_mappings_walker(u64 addr, u64 end, u32 level, > state = pkvm_getstate(kvm_pgtable_hyp_pte_prot(pte)); > switch (state) { > case PKVM_PAGE_OWNED: > - return host_stage2_set_owner_locked(phys, PAGE_SIZE, pkvm_hyp_id); > + return host_stage2_annotate_locked(phys, PAGE_SIZE, > + kvm_init_invalid_leaf_owner(pkvm_hyp_id)); > case PKVM_PAGE_SHARED_OWNED: > prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, PKVM_PAGE_SHARED_BORROWED); > break; > diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c > index 6bbfd952f0c5..4201e512da48 100644 > --- a/arch/arm64/kvm/hyp/pgtable.c > +++ b/arch/arm64/kvm/hyp/pgtable.c > @@ -46,9 +46,6 @@ > KVM_PTE_LEAF_ATTR_LO_S2_S2AP_W | \ > KVM_PTE_LEAF_ATTR_HI_S2_XN) > > -#define KVM_INVALID_PTE_OWNER_MASK GENMASK(9, 2) > -#define KVM_MAX_OWNER_ID 1 > - > struct kvm_pgtable_walk_data { > struct kvm_pgtable *pgt; > struct kvm_pgtable_walker *walker; > @@ -167,11 +164,6 @@ static kvm_pte_t kvm_init_valid_leaf_pte(u64 pa, kvm_pte_t attr, u32 level) > return pte; > } > > -static kvm_pte_t kvm_init_invalid_leaf_owner(u8 owner_id) > -{ > - return FIELD_PREP(KVM_INVALID_PTE_OWNER_MASK, owner_id); > -} > - > static int kvm_pgtable_visitor_cb(struct kvm_pgtable_walk_data *data, u64 addr, > u32 level, kvm_pte_t *ptep, > enum kvm_pgtable_walk_flags flag) > @@ -503,7 +495,7 @@ void kvm_pgtable_hyp_destroy(struct kvm_pgtable *pgt) > struct stage2_map_data { > u64 phys; > kvm_pte_t attr; > - u8 owner_id; > + u64 annotation; > > kvm_pte_t *anchor; > kvm_pte_t *childp; > @@ -670,7 +662,7 @@ static int stage2_map_walker_try_leaf(u64 addr, u64 end, u32 level, > if (kvm_phys_is_valid(phys)) > new = kvm_init_valid_leaf_pte(phys, data->attr, level); > else > - new = kvm_init_invalid_leaf_owner(data->owner_id); > + new = data->annotation; > > if (stage2_pte_is_counted(old)) { > /* > @@ -859,8 +851,8 @@ int kvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size, > return ret; > } > > -int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, > - void *mc, u8 owner_id) > +int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size, > + void *mc, kvm_pte_t annotation) > { > int ret; > struct stage2_map_data map_data = { > @@ -868,8 +860,8 @@ int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, > .mmu = pgt->mmu, > .memcache = mc, > .mm_ops = pgt->mm_ops, > - .owner_id = owner_id, > .force_pte = true, > + .annotation = annotation, > }; > struct kvm_pgtable_walker walker = { > .cb = stage2_map_walker, > @@ -879,7 +871,7 @@ int kvm_pgtable_stage2_set_owner(struct kvm_pgtable *pgt, u64 addr, u64 size, > .arg = &map_data, > }; > > - if (owner_id > KVM_MAX_OWNER_ID) > + if (annotation & PTE_VALID) > return -EINVAL; > > ret = kvm_pgtable_walk(pgt, addr, size, &walker); > -- > 2.30.2 > Thanks, drew