From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 071C8606A334 for ; Thu, 3 Dec 2026 11:52:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=bXrhByVq; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=Gy4tmRop DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:References:Mime-Version:In-Reply-To:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=uDYhI4BU+Un91/HTD3ZjorHQ9b9iGBW7p6zC/ehwUUE=; b=bXrhByVq2vSU58+eA/6zHGQeIx 5T0VmI1jc4EQH4JMOr19sXMX7TkwsynBNMW1yAijHaj2LZlsb5zsn9ccNf32s4mIvHz6yRzL5//wf YK1uxJoYoQYUFjuwJZSWRvIvucVDY5eCbhomGialYJxArUEdWdfHV8eS3ldUJJjlBpZ+OQ2g2/9lQ Yle1DPBzAAhS0cdJVraxyGQiFLaxZNF2VyO7xjylvt2uvwVz/zQ2RjMENBvExeN+Z1jtgLBiiWBQ+ OWJYXxGBvN+8fF/TzJHFPfzTtlTvk4VGnsBaJFaxd36Jfrb4ZSyVnh3x2knCsBDI01lTfmHUG8j1s F0A5K9yA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZ5Mm-0000000ERjz-3xHl; Thu, 03 Dec 2026 11:52:48 +0000 Received: from mail-wm1-x347.google.com ([2a00:1450:4864:20::347]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZ5Mi-0000000ERht-3k9z for linux-arm-kernel@lists.infradead.org; Thu, 03 Dec 2026 11:52:47 +0000 Received: by mail-wm1-x347.google.com with SMTP id 5b1f17b1804b1-4ba01393800so50894975e9.1 for ; Thu, 03 Dec 2026 03:52:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796298762; x=1796903562; darn=lists.infradead.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=uDYhI4BU+Un91/HTD3ZjorHQ9b9iGBW7p6zC/ehwUUE=; b=Gy4tmRopm0nkdoBRrVK+3oEqttwkEWdMGtpRQPgm3Rn6WE34HNfKkqceQAPoTDWplF kffjt9bMJXUS2HHQyezUAHBePmJuka2V9Quq0c0HoTdd+I+8LXIXDar8a3owYMc4B+rA 5DJA/Py4DMdrucK+WG8yxK7MJMMEmaZQz183f777PUVdYtOjuD0CMzhbO9gDyF7CpztJ N3D1rD3Xk0KrqL0dzFvJ19310bKXEZO9dgUtl8WUT/dvF75GgdMB+MgBum2Icx3yDY/+ TCes4jcns2xruIalhN36Xyr+qBZQf9UNjGGO0ykV8qJhlWSbvXybvwxMR+bKcQn/oSBk VxXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796298762; x=1796903562; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=uDYhI4BU+Un91/HTD3ZjorHQ9b9iGBW7p6zC/ehwUUE=; b=LCDVsWheaGj+JSf8iFOyiXKCgt6Z4qfiNhkBw5Xy4TETWyhxNa/Eyu55xZ/U/dkOtg KXJyJf7L0wXDai1P2bg+XPx5ZyuGYA3UIfQS84q9nx0yxfzZ0SPCFd8iGcv2GiB39fKV XNLeNrX2dMqShs5xU7++gFXhYS2LSLY0yTfrG8uIyXh96fJuNYgBd2wkgGT2c5BVfhLZ /lAS2LRY8VyfHqBWWP1CLEHRv+1ib07klcjvXNPXKMxHU9kloCXyxE20FLbP8+GGen2G 6IBTheGA0JzXg4bdULRLj+YzuHHVleLbhiWp0H1ZmYWwGdrGDtGZ7Mjy6ilsU7CpHGDA Ns+Q== X-Forwarded-Encrypted: i=1; AL+hErFPUu8n9gEhn58U2tED6u9qEGq+tUbr2PW4CUD9GWiBt8SPmTocUd28W7qIgYw3JlwEz7v6ilp0DG4NnqbTR9os@lists.infradead.org X-Gm-Message-State: APuU4Z9B9vuH1rDIAwmq4GTYMACw1fgv+UajGbos0L9Z541AQ4NWuIR7 Yj3XFhJ5L/mAwDYvl+VQIpKP+KeXbEoyN2yKDb4Npuj03gMCV0P4u4tMPyprb7enIj8vl/V4mUo PmoNc7Cr9KIi+4WaOJejxKw== X-Received: from wmo20.prod.google.com ([2002:a05:600c:2314:b0:4a1:f4c2:f6c7]) (user=vdonnefort job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3493:b0:4bb:e1f0:1fd6 with SMTP id 5b1f17b1804b1-4bbe1f02166mr29156205e9.4.1796298762305; Thu, 03 Dec 2026 03:52:42 -0800 (PST) Date: Thu, 3 Dec 2026 11:51:46 +0000 In-Reply-To: <20261203115209.1690207-1-vdonnefort@google.com> X-Archived-By: Mime-Version: 1.0 References: <20261203115209.1690207-1-vdonnefort@google.com> X-Mailer: git-send-email 2.56.1.911.g883d8bcb20-goog Message-ID: <20261203115209.1690207-2-vdonnefort@google.com> Subject: [PATCH v6 01/24] KVM: arm64: Introduce kvm_pgtable_stage2_table_install() From: Vincent Donnefort To: maz@kernel.org, oupton@kernel.org, kvmarm@lists.linux.dev, linux-arm-kernel@lists.infradead.org Cc: joey.gouly@arm.com, seiden@linux.ibm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org, kernel-team@android.com, fuad.tabba@linux.dev, qperret@google.com, weilin.chang@arm.com, Vincent Donnefort Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261203_035244_957375_D4351757 X-CRM114-Status: GOOD ( 17.61 ) X-Spam-Score: -9.5 (---------) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" X-Spam-Status: No, score=-26.4 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIMWL_WL_MED,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The existing kvm_pgtable_stage2_split() is unfit for pKVM: it relies on kvm_mmu_memory_cache which pKVM doesn't use and has no easy way to handle pKVM-specific annotations. Add the pgtable helper kvm_pgtable_stage2_table_install() so pKVM can handle block splitting in mem_protect.c. That helper can take a table either populated with kvm_pgtable_stage2_create_unlinked() or one filled with annotations. Signed-off-by: Vincent Donnefort --- arch/arm64/include/asm/kvm_pgtable.h | 13 ++++ arch/arm64/kvm/hyp/pgtable.c | 100 +++++++++++++++++++++------ 2 files changed, 93 insertions(+), 20 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h index 1c9c57cd6ae4..5d5d2165dbec 100644 --- a/arch/arm64/include/asm/kvm_pgtable.h +++ b/arch/arm64/include/asm/kvm_pgtable.h @@ -835,6 +835,19 @@ int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size); int kvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size, struct kvm_mmu_memory_cache *mc); +/** + * kvm_pgtable_stage2_table_install() - Replace a stage-2 leaf entry with a table. + * @pgt: Page-table structure initialised by kvm_pgtable_stage2_init*(). + * @table: Unlinked stage-2 paging structure to install, such as one + * created by kvm_pgtable_stage2_create_unlinked(). + * @addr: Address identifying the leaf entry to replace. + * @level: Level of the leaf entry to replace. + * + * Return: 0 on success, negative error code on failure. + */ +int kvm_pgtable_stage2_table_install(struct kvm_pgtable *pgt, kvm_pte_t *table, + u64 addr, s8 level); + /** * kvm_pgtable_walk() - Walk a page-table. * @pgt: Page-table structure initialised by kvm_pgtable_*_init(). diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index 9c83837b184d..954ce008d130 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -880,6 +880,25 @@ static void stage2_make_pte(const struct kvm_pgtable_visit_ctx *ctx, kvm_pte_t n smp_store_release(ctx->ptep, new); } +static int stage2_table_install(const struct kvm_pgtable_visit_ctx *ctx, + struct kvm_s2_mmu *mmu, + kvm_pte_t *table) +{ + kvm_pte_t new; + + if (!stage2_try_break_pte(ctx, mmu)) + return -EAGAIN; + + /* + * Note, the contents of the page table are guaranteed to be made + * visible before the new PTE is assigned because stage2_make_pte() + * writes the PTE using smp_store_release(). + */ + new = kvm_init_table_pte(table, ctx->mm_ops); + stage2_make_pte(ctx, new); + return 0; +} + static bool stage2_unmap_defer_tlb_flush(struct kvm_pgtable *pgt) { /* @@ -1036,7 +1055,7 @@ static int stage2_map_walk_leaf(const struct kvm_pgtable_visit_ctx *ctx, struct stage2_map_data *data) { struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops; - kvm_pte_t *childp, new; + kvm_pte_t *childp; int ret; ret = stage2_map_walker_try_leaf(ctx, data); @@ -1053,20 +1072,16 @@ static int stage2_map_walk_leaf(const struct kvm_pgtable_visit_ctx *ctx, if (!childp) return -ENOMEM; - if (!stage2_try_break_pte(ctx, data->mmu)) { - mm_ops->put_page(childp); - return -EAGAIN; - } - /* * If we've run into an existing block mapping then replace it with * a table. Accesses beyond 'end' that fall within the new table * will be mapped lazily. */ - new = kvm_init_table_pte(childp, mm_ops); - stage2_make_pte(ctx, new); + ret = stage2_table_install(ctx, data->mmu, childp); + if (ret) + mm_ops->put_page(childp); - return 0; + return ret; } /* @@ -1496,13 +1511,14 @@ static int stage2_split_walker(const struct kvm_pgtable_visit_ctx *ctx, { struct kvm_pgtable_mm_ops *mm_ops = ctx->mm_ops; struct kvm_mmu_memory_cache *mc = ctx->arg; - struct kvm_s2_mmu *mmu; - kvm_pte_t pte = ctx->old, new, *childp; + kvm_pte_t pte = ctx->old, *childp; enum kvm_pgtable_prot prot; + struct kvm_s2_mmu *mmu; s8 level = ctx->level; bool force_pte; int nr_pages; u64 phys; + int ret; /* No huge-pages exist at the last level */ if (level == KVM_PGTABLE_LAST_LEVEL) @@ -1544,18 +1560,12 @@ static int stage2_split_walker(const struct kvm_pgtable_visit_ctx *ctx, if (IS_ERR(childp)) return PTR_ERR(childp); - if (!stage2_try_break_pte(ctx, mmu)) { + ret = stage2_table_install(ctx, mmu, childp); + if (ret) { kvm_pgtable_stage2_free_unlinked(mm_ops, childp, level); - return -EAGAIN; + return ret; } - /* - * Note, the contents of the page table are guaranteed to be made - * visible before the new PTE is assigned because stage2_make_pte() - * writes the PTE using smp_store_release(). - */ - new = kvm_init_table_pte(childp, mm_ops); - stage2_make_pte(ctx, new); return 0; } @@ -1574,6 +1584,56 @@ int kvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size, return ret; } +struct stage2_table_install_data { + struct kvm_s2_mmu *mmu; + kvm_pte_t *table; + s8 level; +}; + +static int stage2_table_install_walker(const struct kvm_pgtable_visit_ctx *ctx, + enum kvm_pgtable_walk_flags visit) +{ + struct stage2_table_install_data *data = ctx->arg; + + if (ctx->level != data->level) + return -EINVAL; + + /* + * On success, return a positive value to prevent descending into the + * new table. + */ + return stage2_table_install(ctx, data->mmu, data->table) ?: 1; +} + +int kvm_pgtable_stage2_table_install(struct kvm_pgtable *pgt, kvm_pte_t *table, + u64 addr, s8 level) +{ + struct stage2_table_install_data data = { + .mmu = pgt->mmu, + .table = table, + .level = level, + }; + struct kvm_pgtable_walker walker = { + .cb = stage2_table_install_walker, + .flags = KVM_PGTABLE_WALK_LEAF, + .arg = &data, + }; + int ret; + + if (level < pgt->start_level || level >= KVM_PGTABLE_LAST_LEVEL) + return -EINVAL; + + if (!IS_ALIGNED(addr, kvm_granule_size(level))) + return -EINVAL; + + ret = kvm_pgtable_walk(pgt, addr, kvm_granule_size(level), &walker); + if (ret < 0) + return ret; + + dsb(ishst); + return 0; +} + int __kvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu, struct kvm_pgtable_mm_ops *mm_ops, enum kvm_pgtable_stage2_flags flags, -- 2.56.1.911.g883d8bcb20-goog