* [PATCH v6 01/24] KVM: arm64: Introduce kvm_pgtable_stage2_table_install()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 12:07 ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 02/24] KVM: arm64: Preserve idmap stage-2 entries on block split Vincent Donnefort
` (23 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
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 <vdonnefort@google.com>
---
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
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 01/24] KVM: arm64: Introduce kvm_pgtable_stage2_table_install()
2026-12-03 11:51 ` [PATCH v6 01/24] KVM: arm64: Introduce kvm_pgtable_stage2_table_install() Vincent Donnefort
@ 2026-12-03 12:07 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 12:07 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] KVM: arm64: inconsistent name/arg order in
kvm_pgtable_stage2_table_install()
- [Low] KVM: arm64: incomplete kernel-doc for
kvm_pgtable_stage2_table_install()
--
commit 9882f928ffe9d645ba3fdb1199cbfda370f7397e
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Introduce kvm_pgtable_stage2_table_install()
This commit factors out stage2_table_install() and exposes
kvm_pgtable_stage2_table_install() so callers such as pKVM can replace a
stage-2 leaf entry with an unlinked page table.
> diff --git a/arch/arm64/include/asm/kvm_pgtable.h b/arch/arm64/include/asm/kvm_pgtable.h
> index 1c9c57cd6ae40..5d5d2165dbec9 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.
> + */
[Severity: Low]
Should the kernel-doc spell out the preconditions and ownership rules for
kvm_pgtable_stage2_table_install()?
Unlike sibling stage-2 helpers that ignore sub-page offsets, this function
requires addr to be aligned to kvm_granule_size(level) and returns -EINVAL
otherwise. It also performs a non-shared walk (KVM_PGTABLE_WALK_LEAF
only), so callers must hold exclusive access to pgt.
On success, pgt takes ownership of table (carrying its base reference plus
one per counted entry); on failure, the caller must free it (for example
via kvm_pgtable_stage2_free_unlinked()). Replacing a valid leaf performs
break-before-make TLB invalidation, but no cache maintenance on table.
> +int kvm_pgtable_stage2_table_install(struct kvm_pgtable *pgt, kvm_pte_t *table,
> + u64 addr, s8 level);
[Severity: Low]
Could this declaration match the naming and parameter order of the other
stage-2 helpers in this header?
Every other IPA-taking kvm_pgtable_stage2_*() helper takes (pgt, addr, ...)
first, and multi-word helpers use verb-object names (create_unlinked,
destroy_range, relax_perms). Renaming and reordering this to
kvm_pgtable_stage2_install_table(pgt, addr, level, table) would match them.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=1
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 02/24] KVM: arm64: Preserve idmap stage-2 entries on block split
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 01/24] KVM: arm64: Introduce kvm_pgtable_stage2_table_install() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 12:30 ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 03/24] KVM: arm64: Allow block-level stage-2 annotation Vincent Donnefort
` (22 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
pKVM relies on host stage-2 annoations to track page ownership and
metadata, which can't be rebuilt lazily on fault. In preparation for
allowing block-level stage-2 annotations, prefault the child table with
the existing block's mappings or annotations.
Issue a trailing dsb in kvm_pgtable_stage2_annotate() since it may now
install valid mappings.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/pgtable.c | 70 +++++++++++++++++++++++-------------
1 file changed, 46 insertions(+), 24 deletions(-)
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index 954ce008d130..a50bcfe72c8e 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -1051,10 +1051,15 @@ static int stage2_map_walk_table_pre(const struct kvm_pgtable_visit_ctx *ctx,
return 0;
}
+static kvm_pte_t *stage2_create_unlinked(struct kvm_pgtable *pgt,
+ kvm_pte_t pte, s8 level,
+ void *mc, bool force_pte);
+
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;
+ struct kvm_pgtable *pgt = data->mmu->pgt;
kvm_pte_t *childp;
int ret;
@@ -1068,18 +1073,24 @@ static int stage2_map_walk_leaf(const struct kvm_pgtable_visit_ctx *ctx,
if (!data->memcache)
return -ENOMEM;
- childp = mm_ops->zalloc_page(data->memcache);
- if (!childp)
- return -ENOMEM;
-
/*
* 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.
+ * will be mapped lazily. For an idmap stage-2, preserve existing
+ * block mappings and annotations in the new table instead.
*/
+ if ((pgt->flags & KVM_PGTABLE_S2_IDMAP) && ctx->old)
+ childp = stage2_create_unlinked(pgt, ctx->old, ctx->level,
+ data->memcache, false);
+ else
+ childp = mm_ops->zalloc_page(data->memcache) ?: ERR_PTR(-ENOMEM);
+
+ if (IS_ERR(childp))
+ return PTR_ERR(childp);
+
ret = stage2_table_install(ctx, data->mmu, childp);
if (ret)
- mm_ops->put_page(childp);
+ kvm_pgtable_stage2_free_unlinked(mm_ops, childp, ctx->level);
return ret;
}
@@ -1166,6 +1177,7 @@ int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size,
return -EINVAL;
ret = kvm_pgtable_walk(pgt, addr, size, &walker);
+ dsb(ishst);
return ret;
}
@@ -1432,13 +1444,16 @@ int kvm_pgtable_stage2_flush(struct kvm_pgtable *pgt, u64 addr, u64 size)
return kvm_pgtable_walk(pgt, addr, size, &walker);
}
-kvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt,
- u64 phys, s8 level,
- enum kvm_pgtable_prot prot,
- void *mc, bool force_pte)
+static kvm_pte_t *stage2_create_unlinked(struct kvm_pgtable *pgt,
+ kvm_pte_t pte, s8 level,
+ void *mc, bool force_pte)
{
+ bool valid = kvm_pte_valid(pte);
struct stage2_map_data map_data = {
- .phys = phys,
+ .phys = valid ? kvm_pte_to_phys(pte) : 0,
+ .attr = valid ? pte : 0,
+ .pte_annot = valid ? 0 : pte,
+ .annotation = !valid,
.mmu = pgt->mmu,
.memcache = mc,
.force_pte = force_pte,
@@ -1464,13 +1479,6 @@ kvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt,
kvm_pte_t *pgtable;
int ret;
- if (!IS_ALIGNED(phys, kvm_granule_size(level)))
- return ERR_PTR(-EINVAL);
-
- ret = stage2_set_prot_attr(pgt, prot, &map_data.attr);
- if (ret)
- return ERR_PTR(ret);
-
pgtable = mm_ops->zalloc_page(mc);
if (!pgtable)
return ERR_PTR(-ENOMEM);
@@ -1485,6 +1493,25 @@ kvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt,
return pgtable;
}
+kvm_pte_t *kvm_pgtable_stage2_create_unlinked(struct kvm_pgtable *pgt,
+ u64 phys, s8 level,
+ enum kvm_pgtable_prot prot,
+ void *mc, bool force_pte)
+{
+ kvm_pte_t attr;
+ int ret;
+
+ if (!IS_ALIGNED(phys, kvm_granule_size(level)))
+ return ERR_PTR(-EINVAL);
+
+ ret = stage2_set_prot_attr(pgt, prot, &attr);
+ if (ret)
+ return ERR_PTR(ret);
+
+ return stage2_create_unlinked(pgt, kvm_init_valid_leaf_pte(phys, attr, level),
+ level, mc, force_pte);
+}
+
/*
* Get the number of page-tables needed to replace a block with a
* fully populated tree up to the PTE entries. Note that @level is
@@ -1512,12 +1539,10 @@ 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;
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 */
@@ -1552,11 +1577,8 @@ static int stage2_split_walker(const struct kvm_pgtable_visit_ctx *ctx,
return -ENOMEM;
mmu = container_of(mc, struct kvm_s2_mmu, split_page_cache);
- phys = kvm_pte_to_phys(pte);
- prot = kvm_pgtable_stage2_pte_prot(pte);
- childp = kvm_pgtable_stage2_create_unlinked(mmu->pgt, phys,
- level, prot, mc, force_pte);
+ childp = stage2_create_unlinked(mmu->pgt, pte, level, mc, force_pte);
if (IS_ERR(childp))
return PTR_ERR(childp);
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 02/24] KVM: arm64: Preserve idmap stage-2 entries on block split
2026-12-03 11:51 ` [PATCH v6 02/24] KVM: arm64: Preserve idmap stage-2 entries on block split Vincent Donnefort
@ 2026-12-03 12:30 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 12:30 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] KVM: arm64: stage2_map_walk_leaf() prefaults overwritten
range on block split
- [Low] KVM: arm64: avoid full table walk on empty page in
stage2_map_walk_leaf()
--
commit eb0747ddd24665d91e3c9f56d57d2dfe7e0e9532
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Preserve idmap stage-2 entries on block split
When splitting a block entry in an idmap stage-2 page table, this commit
populates the new child table with the existing block's mappings or
annotations via stage2_create_unlinked() instead of leaving it zeroed.
It also adds a trailing dsb(ishst) to kvm_pgtable_stage2_annotate().
> diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
> index 954ce008d1305..a50bcfe72c8ef 100644
> --- a/arch/arm64/kvm/hyp/pgtable.c
> +++ b/arch/arm64/kvm/hyp/pgtable.c
[ ... ]
> @@ -1068,18 +1073,24 @@ static int stage2_map_walk_leaf(const struct kvm_pgtable_visit_ctx *ctx,
> if (!data->memcache)
> return -ENOMEM;
>
> - childp = mm_ops->zalloc_page(data->memcache);
> - if (!childp)
> - return -ENOMEM;
> -
> /*
> * 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.
> + * will be mapped lazily. For an idmap stage-2, preserve existing
> + * block mappings and annotations in the new table instead.
> */
> + if ((pgt->flags & KVM_PGTABLE_S2_IDMAP) && ctx->old)
> + childp = stage2_create_unlinked(pgt, ctx->old, ctx->level,
> + data->memcache, false);
> + else
> + childp = mm_ops->zalloc_page(data->memcache) ?: ERR_PTR(-ENOMEM);
[Severity: Medium]
Does prefaulting the entire child table when ctx->old is a valid block
mapping amplify TLB shootdowns for the subrange being overwritten?
When splitting a valid host_mmu.pgt block, stage2_create_unlinked() fills
all child entries with valid PTEs, including [ctx->addr, ctx->end).
Descending into that range triggers __kvm_tlb_flush_vmid_ipa()
(ipas2e1is and vmalle1is) for each overwritten entry under host_mmu.lock.
Previously, breaking the parent block issued one shootdown and descending
into the zeroed child table issued none. For an N-page sub-2MB donation
in __pkvm_host_donate_hyp() (e.g. from __pkvm_init_vm()), this turns 1
shootdown into 1 + N from a 2MB block (or 2 + N from a 1GB block).
Leaving entries within [ctx->addr, ctx->end) zeroed when splitting a
valid block would preserve sibling mappings while avoiding the redundant
shootdowns, since valid host mappings can be rebuilt on fault.
>
> + if (IS_ERR(childp))
> + return PTR_ERR(childp);
> +
> ret = stage2_table_install(ctx, data->mmu, childp);
> if (ret)
> - mm_ops->put_page(childp);
> + kvm_pgtable_stage2_free_unlinked(mm_ops, childp, ctx->level);
[Severity: Low]
Can this avoid calling kvm_pgtable_stage2_free_unlinked() when childp is
an empty page from mm_ops->zalloc_page()?
For non-idmap guest stage-2 tables, concurrent faults in
kvm_s2_fault_map() walk with KVM_PGTABLE_WALK_SHARED under
read_lock(&kvm->mmu_lock) and can hit -EAGAIN in stage2_table_install().
Unconditionally calling kvm_pgtable_stage2_free_unlinked() makes each
losing vCPU walk all PTRS_PER_PTE zero entries via stage2_free_walker()
while holding mmu_lock, where mm_ops->put_page(childp) would suffice.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=2
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 03/24] KVM: arm64: Allow block-level stage-2 annotation
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 01/24] KVM: arm64: Introduce kvm_pgtable_stage2_table_install() Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 02/24] KVM: arm64: Preserve idmap stage-2 entries on block split Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 12:52 ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 04/24] KVM: arm64: Use block-level annotations when setting up the host stage-2 Vincent Donnefort
` (21 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
On host to guest donation, pKVM annotates the host stage-2 invalid
entries with the GFN. Now that the host stage-2 preserves annotations on
block split, we can allow these annotations at block-level. This enables
safe support for guest stage-2 huge mappings.
Allowing block-level annotations also reduces stage-2 fragmentation when
unmapping a huge region, and reduces host stage-2 memory usage by
avoiding the allocation of intermediate page tables.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/pgtable.c | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c
index a50bcfe72c8e..5583bae812c1 100644
--- a/arch/arm64/kvm/hyp/pgtable.c
+++ b/arch/arm64/kvm/hyp/pgtable.c
@@ -968,9 +968,6 @@ static bool stage2_leaf_mapping_allowed(const struct kvm_pgtable_visit_ctx *ctx,
if (data->force_pte && ctx->level < KVM_PGTABLE_LAST_LEVEL)
return false;
- if (data->annotation)
- return true;
-
return kvm_block_mapping_supported(ctx, phys);
}
@@ -1158,8 +1155,9 @@ int kvm_pgtable_stage2_annotate(struct kvm_pgtable *pgt, u64 addr, u64 size,
struct stage2_map_data map_data = {
.mmu = pgt->mmu,
.memcache = mc,
- .force_pte = true,
.annotation = true,
+ /* Pretend ID-mapped for kvm_block_mapping_supported() */
+ .phys = ALIGN_DOWN(addr, PAGE_SIZE),
.pte_annot = pte_annot |
FIELD_PREP(KVM_INVALID_PTE_TYPE_MASK, type),
};
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 03/24] KVM: arm64: Allow block-level stage-2 annotation
2026-12-03 11:51 ` [PATCH v6 03/24] KVM: arm64: Allow block-level stage-2 annotation Vincent Donnefort
@ 2026-12-03 12:52 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 12:52 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: arm64: commit log misstates block-split GFN annotation
preservation
--
commit 425d642812353599b5ac614893302091bf811b55
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Allow block-level stage-2 annotation
This commit drops force_pte from kvm_pgtable_stage2_annotate() and removes
the annotation shortcut in stage2_leaf_mapping_allowed() so stage-2
annotations can be installed as block entries when alignment and size permit.
> On host to guest donation, pKVM annotates the host stage-2 invalid
> entries with the GFN. Now that the host stage-2 preserves annotations on
> block split, we can allow these annotations at block-level. This enables
> safe support for guest stage-2 huge mappings.
[Severity: Low]
Is the commit message accurate in stating that host stage-2 block-split
preservation safely covers GFN-annotated guest entries?
When stage2_map_walk_leaf() splits an idmap block, stage2_create_unlinked()
copies the invalid block PTE into map_data.pte_annot, and
stage2_map_walker_try_leaf() writes that same value to every child entry.
That preserves uniform annotations such as PKVM_ID_HYP, but for
PKVM_ID_GUEST blocks it would replicate the base GFN from
host_stage2_encode_gfn_meta() into every sub-page instead of incrementing it.
Clarifying that implicit block splits only preserve uniform annotations,
rather than per-page GFN metadata, would avoid misleading future readers.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=3
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 04/24] KVM: arm64: Use block-level annotations when setting up the host stage-2
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (2 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 03/24] KVM: arm64: Allow block-level stage-2 annotation Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 05/24] KVM: arm64: Make pKVM ownership selftest an HVC Vincent Donnefort
` (20 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
During pKVM initialisation, fix_host_ownership() configures the host
stage-2 mappings to match the hypervisor stage-1. Previously this was
done page-by-page. Now that the host stage-2 supports block-level
annotations, refactor the walker to apply the ownership state to an
entire range at once.
This reduces host stage-2 page-table fragmentation and memory usage by
avoiding allocation of intermediate page-table pages.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/include/nvhe/memory.h | 5 +
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 5 -
arch/arm64/kvm/hyp/nvhe/setup.c | 157 ++++++++++++++---------
3 files changed, 104 insertions(+), 63 deletions(-)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/memory.h b/arch/arm64/kvm/hyp/include/nvhe/memory.h
index b50712d47f6d..86a51cf1d590 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/memory.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/memory.h
@@ -122,6 +122,11 @@ static inline void set_hyp_state(struct hyp_page *p, enum pkvm_page_state state)
p->__hyp_state_comp = state ^ PKVM_PAGE_STATE_VMEMMAP_MASK;
}
+#define for_each_hyp_page(__p, __st, __sz) \
+ for (struct hyp_page *__p = hyp_phys_to_page(__st), \
+ *__e = __p + ((__sz) >> PAGE_SHIFT); \
+ __p < __e; __p++)
+
/*
* Refcounting for 'struct hyp_page'.
* hyp_pool::lock must be held if atomic access to the refcount is required.
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index d4c332a91803..58094d52b8a2 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -76,11 +76,6 @@ static void hyp_unlock_component(void)
hyp_spin_unlock(&pkvm_pgd_lock);
}
-#define for_each_hyp_page(__p, __st, __sz) \
- for (struct hyp_page *__p = hyp_phys_to_page(__st), \
- *__e = __p + ((__sz) >> PAGE_SHIFT); \
- __p < __e; __p++)
-
static void *host_s2_zalloc_pages_exact(size_t size)
{
void *addr = hyp_alloc_pages(&host_s2_pool, get_order(size));
diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c
index 7bbc26b28faa..e72b7626799d 100644
--- a/arch/arm64/kvm/hyp/nvhe/setup.c
+++ b/arch/arm64/kvm/hyp/nvhe/setup.c
@@ -187,50 +187,46 @@ static void hpool_put_page(void *addr)
hyp_put_page(&hpool, addr);
}
-static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
- enum kvm_pgtable_walk_flags visit)
+struct fix_host_ownership_data {
+ phys_addr_t phys;
+ size_t size;
+ enum kvm_pgtable_prot prot;
+};
+
+static int __fix_host_ownership(const struct fix_host_ownership_data *data)
{
- enum pkvm_page_state state;
- struct hyp_page *page;
- phys_addr_t phys;
- enum kvm_pgtable_prot prot;
+ enum pkvm_page_state state = pkvm_getstate(data->prot);
- if (!kvm_pte_valid(ctx->old))
+ if (!data->prot)
return 0;
- if (ctx->level != KVM_PGTABLE_LAST_LEVEL)
- return -EINVAL;
-
- phys = kvm_pte_to_phys(ctx->old);
- if (!addr_is_memory(phys))
- return -EINVAL;
-
- page = hyp_phys_to_page(phys);
-
/*
* Adjust the host stage-2 mappings to match the ownership attributes
* configured in the hypervisor stage-1, and make sure to propagate them
* to the hyp_vmemmap state.
*/
- prot = kvm_pgtable_hyp_pte_prot(ctx->old);
- state = pkvm_getstate(prot);
switch (state) {
case PKVM_PAGE_OWNED:
- set_hyp_state(page, PKVM_PAGE_OWNED);
- /* hyp text is RO in the host stage-2 to be inspected on panic. */
- if (addr_is_hyp_text(phys)) {
+ for_each_hyp_page(page, data->phys, data->size) {
+ set_hyp_state(page, PKVM_PAGE_OWNED);
set_host_state(page, PKVM_NOPAGE);
- return host_stage2_idmap_locked(phys, PAGE_SIZE, KVM_PGTABLE_PROT_R);
- } else {
- return host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HYP);
}
+ /* hyp text is RO in the host stage-2 to be inspected on panic. */
+ if (addr_is_hyp_text(data->phys))
+ return host_stage2_idmap_locked(data->phys, data->size, KVM_PGTABLE_PROT_R);
+ else
+ return host_stage2_set_owner_locked(data->phys, data->size, PKVM_ID_HYP);
case PKVM_PAGE_SHARED_OWNED:
- set_hyp_state(page, PKVM_PAGE_SHARED_OWNED);
- set_host_state(page, PKVM_PAGE_SHARED_BORROWED);
+ for_each_hyp_page(page, data->phys, data->size) {
+ set_hyp_state(page, PKVM_PAGE_SHARED_OWNED);
+ set_host_state(page, PKVM_PAGE_SHARED_BORROWED);
+ }
break;
case PKVM_PAGE_SHARED_BORROWED:
- set_hyp_state(page, PKVM_PAGE_SHARED_BORROWED);
- set_host_state(page, PKVM_PAGE_SHARED_OWNED);
+ for_each_hyp_page(page, data->phys, data->size) {
+ set_hyp_state(page, PKVM_PAGE_SHARED_BORROWED);
+ set_host_state(page, PKVM_PAGE_SHARED_OWNED);
+ }
break;
default:
return -EINVAL;
@@ -239,6 +235,81 @@ static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
return 0;
}
+static int fix_host_ownership_walker(const struct kvm_pgtable_visit_ctx *ctx,
+ enum kvm_pgtable_walk_flags visit)
+{
+ struct fix_host_ownership_data *data = ctx->arg;
+ enum kvm_pgtable_prot prot = 0;
+ phys_addr_t phys = 0;
+ int ret;
+
+ if (kvm_pte_valid(ctx->old)) {
+ prot = kvm_pgtable_hyp_pte_prot(ctx->old);
+ phys = kvm_pte_to_phys(ctx->old);
+ if (!addr_is_memory(phys))
+ return -EINVAL;
+ }
+
+ if (prot == data->prot &&
+ phys == data->phys + data->size) {
+ data->size += kvm_granule_size(ctx->level);
+
+ return 0;
+ }
+
+ ret = __fix_host_ownership(data);
+ if (ret)
+ return ret;
+
+ data->phys = phys;
+ data->size = kvm_granule_size(ctx->level);
+ data->prot = prot;
+
+ return 0;
+}
+
+static int fix_host_ownership(void)
+{
+ struct fix_host_ownership_data data;
+ struct kvm_pgtable_walker walker = {
+ .cb = fix_host_ownership_walker,
+ .flags = KVM_PGTABLE_WALK_LEAF,
+ .arg = &data,
+ };
+ int i, ret;
+
+ for (i = 0; i < hyp_memblock_nr; i++) {
+ struct memblock_region *reg = &hyp_memory[i];
+ u64 start = (u64)hyp_phys_to_virt(reg->base);
+
+ data = (struct fix_host_ownership_data){ 0 };
+ ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
+ if (ret)
+ return ret;
+
+ ret = __fix_host_ownership(&data);
+ if (ret)
+ return ret;
+ }
+
+ /* The stacks sit in the private VA range, not the linear map. */
+ for (i = 0; i < hyp_nr_cpus; i++) {
+ struct kvm_nvhe_init_params *params = per_cpu_ptr(&kvm_init_params, i);
+ u64 start = params->stack_hyp_va - NVHE_STACK_SIZE;
+
+ data = (struct fix_host_ownership_data){ 0 };
+ ret = kvm_pgtable_walk(&pkvm_pgtable, start, NVHE_STACK_SIZE, &walker);
+ if (ret)
+ return ret;
+
+ ret = __fix_host_ownership(&data);
+ if (ret)
+ return ret;
+ }
+
+ return 0;
+}
+
static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx,
enum kvm_pgtable_walk_flags visit)
{
@@ -253,36 +324,6 @@ static int fix_hyp_pgtable_refcnt_walker(const struct kvm_pgtable_visit_ctx *ctx
return 0;
}
-static int fix_host_ownership(void)
-{
- struct kvm_pgtable_walker walker = {
- .cb = fix_host_ownership_walker,
- .flags = KVM_PGTABLE_WALK_LEAF,
- };
- int i, ret;
-
- for (i = 0; i < hyp_memblock_nr; i++) {
- struct memblock_region *reg = &hyp_memory[i];
- u64 start = (u64)hyp_phys_to_virt(reg->base);
-
- ret = kvm_pgtable_walk(&pkvm_pgtable, start, reg->size, &walker);
- if (ret)
- return ret;
- }
-
- /* The stacks sit in the private VA range, not the linear map. */
- for (i = 0; i < hyp_nr_cpus; i++) {
- struct kvm_nvhe_init_params *params = per_cpu_ptr(&kvm_init_params, i);
- u64 start = params->stack_hyp_va - NVHE_STACK_SIZE;
-
- ret = kvm_pgtable_walk(&pkvm_pgtable, start, NVHE_STACK_SIZE, &walker);
- if (ret)
- return ret;
- }
-
- return 0;
-}
-
static int fix_hyp_pgtable_refcnt(void)
{
struct kvm_pgtable_walker walker = {
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 05/24] KVM: arm64: Make pKVM ownership selftest an HVC
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (3 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 04/24] KVM: arm64: Use block-level annotations when setting up the host stage-2 Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 06/24] KVM: arm64: Reject zero-sized ranges in pfn_range_is_valid() Vincent Donnefort
` (19 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for testing the pKVM ownership with huge mappings,
refactor the selftest to be triggered via an HVC using dynamically
allocated memory from the host. This avoids wasting memory which would
be even worse for testing ownership transitions for huge stage-2
mappings.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/include/asm/kvm_asm.h | 1 +
arch/arm64/include/asm/kvm_hcall.h | 2 +
arch/arm64/include/asm/kvm_pkvm.h | 6 ---
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 7 ++--
arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 4 ++
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 7 ++++
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 39 +++++++++++------
arch/arm64/kvm/hyp/nvhe/pkvm.c | 12 +++++-
arch/arm64/kvm/hyp/nvhe/setup.c | 8 ----
arch/arm64/kvm/pkvm.c | 42 +++++++++++++++++--
10 files changed, 92 insertions(+), 36 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
index abda476e023d..ed7bd93aea1b 100644
--- a/arch/arm64/include/asm/kvm_asm.h
+++ b/arch/arm64/include/asm/kvm_asm.h
@@ -63,6 +63,7 @@ enum __kvm_host_smccc_func {
__KVM_HOST_SMCCC_FUNC___vgic_v3_init_lrs,
__KVM_HOST_SMCCC_FUNC___vgic_v3_get_gic_config,
__KVM_HOST_SMCCC_FUNC___pkvm_hyp_alloc_selftest,
+ __KVM_HOST_SMCCC_FUNC___pkvm_ownership_selftest,
__KVM_HOST_SMCCC_FUNC___vgic_v5_make_resident,
__KVM_HOST_SMCCC_FUNC___vgic_v5_make_non_resident,
__KVM_HOST_SMCCC_FUNC___vgic_v5_vdpend,
diff --git a/arch/arm64/include/asm/kvm_hcall.h b/arch/arm64/include/asm/kvm_hcall.h
index 5f77a8aa2963..64d18b7f2392 100644
--- a/arch/arm64/include/asm/kvm_hcall.h
+++ b/arch/arm64/include/asm/kvm_hcall.h
@@ -156,6 +156,8 @@ DECLARE_KVM_HOST_HCALL0(void, __kvm_enable_ssbs)
DECLARE_KVM_HOST_HCALL0(void, __vgic_v3_init_lrs)
DECLARE_KVM_HOST_HCALL0(u64, __vgic_v3_get_gic_config)
DECLARE_KVM_HOST_HCALL0(int, __pkvm_hyp_alloc_selftest)
+DECLARE_KVM_HOST_HCALL(int, __pkvm_ownership_selftest,
+ u64, pfn, u64, pgtable_pfn, unsigned long, pgtable_nr_pages)
DECLARE_KVM_HOST_HCALL0(int, __pkvm_prot_finalize)
diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm_pkvm.h
index fd042d2b5462..b75dc3d03a98 100644
--- a/arch/arm64/include/asm/kvm_pkvm.h
+++ b/arch/arm64/include/asm/kvm_pkvm.h
@@ -193,12 +193,6 @@ static inline unsigned long host_s2_pgtable_pages(void)
return res;
}
-#ifdef CONFIG_NVHE_EL2_DEBUG
-static inline unsigned long pkvm_selftest_pages(void) { return 32; }
-#else
-static inline unsigned long pkvm_selftest_pages(void) { return 0; }
-#endif
-
#define KVM_FFA_MBOX_NR_PAGES 1
static inline unsigned long hyp_ffa_proxy_pages(void)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index ec85a9547120..a1c9c71beb36 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -76,10 +76,9 @@ static __always_inline void __load_host_stage2(void)
}
#ifdef CONFIG_NVHE_EL2_DEBUG
-void pkvm_ownership_selftest(void *base);
-struct pkvm_hyp_vcpu *init_selftest_vm(void *virt);
-void teardown_selftest_vm(void);
+int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr_pages);
#else
-static inline void pkvm_ownership_selftest(void *base) { }
+static inline int
+__pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr_pages) { return 0; }
#endif
#endif /* __KVM_NVHE_MEM_PROTECT__ */
diff --git a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
index 0261b2bc681f..aa5cf5568504 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
@@ -133,6 +133,10 @@ bool kvm_handle_pvm_restricted(struct kvm_vcpu *vcpu, u64 *exit_code);
void kvm_init_pvm_id_regs(struct kvm_vcpu *vcpu);
void kvm_reset_pvm_sys_regs(struct kvm_vcpu *vcpu);
int kvm_check_pvm_sysreg_table(void);
+#ifdef CONFIG_NVHE_EL2_DEBUG
+struct pkvm_hyp_vcpu *init_selftest_vm(void *virt, u64 nr_pages);
+void teardown_selftest_vm(void);
+#endif
int pkvm_reset_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu);
struct pkvm_hyp_vcpu *pkvm_mpidr_to_hyp_vcpu(struct pkvm_hyp_vm *vm, unsigned long mpidr);
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 179d7c8277b8..bf3d13a15b2f 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -1313,6 +1313,12 @@ DEFINE_KVM_HOST_HCALL(ulong, __pkvm_create_private_mapping,
return haddr;
}
+DEFINE_KVM_HOST_HCALL(int, __pkvm_ownership_selftest,
+ u64, pfn, u64, pgtable_pfn, unsigned long, pgtable_nr_pages)
+{
+ return __pkvm_ownership_selftest(pfn, pgtable_pfn, pgtable_nr_pages);
+}
+
DEFINE_KVM_HOST_HCALL0(int, __pkvm_prot_finalize)
{
return __pkvm_prot_finalize();
@@ -1550,6 +1556,7 @@ static const hcall_t host_hcall[] = {
HANDLE_FUNC(__vgic_v3_init_lrs),
HANDLE_FUNC(__vgic_v3_get_gic_config),
HANDLE_FUNC(__pkvm_hyp_alloc_selftest),
+ HANDLE_FUNC(__pkvm_ownership_selftest),
HANDLE_FUNC(__pkvm_prot_finalize),
HANDLE_FUNC(__kvm_adjust_pc),
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 58094d52b8a2..8d5bfae67396 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -1768,24 +1768,36 @@ static void assert_page_state(void)
assert_page_state(); \
} while (0)
-void pkvm_ownership_selftest(void *base)
+int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr_pages)
{
enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RWX;
- void *virt = hyp_alloc_pages(&host_s2_pool, 0);
struct pkvm_hyp_vcpu *vcpu;
- u64 phys, size, pfn, gfn;
struct pkvm_hyp_vm *vm;
+ u64 phys, size, gfn;
+ void *virt;
+ int ret;
- WARN_ON(!virt);
- selftest_page = hyp_virt_to_page(virt);
- selftest_page->refcount = 0;
- selftest_vcpu = vcpu = init_selftest_vm(base);
+ ret = __pkvm_host_donate_hyp(pgtable_pfn, pgtable_nr_pages);
+ if (ret)
+ return ret;
+
+ ret = __pkvm_host_donate_hyp(pfn, 1);
+ if (ret) {
+ WARN_ON(__pkvm_hyp_donate_host(pgtable_pfn, pgtable_nr_pages));
+ return ret;
+ }
+
+ selftest_vcpu = vcpu = init_selftest_vm(hyp_phys_to_virt(hyp_pfn_to_phys(pgtable_pfn)),
+ pgtable_nr_pages);
vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
- size = PAGE_SIZE << selftest_page->order;
- phys = hyp_virt_to_phys(virt);
- pfn = hyp_phys_to_pfn(phys);
+ phys = hyp_pfn_to_phys(pfn);
+ virt = hyp_phys_to_virt(phys);
gfn = hyp_phys_to_pfn(selftest_ipa());
+ size = PAGE_SIZE;
+
+ selftest_page = hyp_virt_to_page(virt);
+ selftest_page->refcount = 0;
selftest_state.host = PKVM_NOPAGE;
selftest_state.hyp = PKVM_PAGE_OWNED;
@@ -1946,8 +1958,11 @@ void pkvm_ownership_selftest(void *base)
selftest_state.hyp = PKVM_PAGE_OWNED;
assert_transition_res(0, __pkvm_host_donate_hyp, pfn, 1);
+ /* This also gives back the ownership to the host */
teardown_selftest_vm();
- selftest_page->refcount = 1;
- hyp_put_page(&host_s2_pool, virt);
+
+ WARN_ON(__pkvm_hyp_donate_host(pfn, 1));
+
+ return 0;
}
#endif
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index fc0475f0877d..ae34c09f1f39 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -843,7 +843,7 @@ static struct pkvm_hyp_vcpu selftest_vcpu = {
},
};
-struct pkvm_hyp_vcpu *init_selftest_vm(void *virt)
+struct pkvm_hyp_vcpu *init_selftest_vm(void *virt, u64 nr_pages)
{
struct hyp_page *p = hyp_virt_to_page(virt);
unsigned long min_pages, seeded = 0;
@@ -859,7 +859,7 @@ struct pkvm_hyp_vcpu *init_selftest_vm(void *virt)
*/
min_pages = kvm_mmu_cache_min_pages(&selftest_vm.kvm.arch.mmu);
- for (i = 0; i < pkvm_selftest_pages(); i++) {
+ for (i = 0; i < nr_pages; i++) {
if (p[i].refcount)
continue;
p[i].refcount = 1;
@@ -879,9 +879,17 @@ struct pkvm_hyp_vcpu *init_selftest_vm(void *virt)
void teardown_selftest_vm(void)
{
+ struct kvm_hyp_memcache *vcpu_mc = &selftest_vcpu.vcpu.arch.stage2_mc;
+ struct kvm_hyp_memcache mc = { 0 };
+
+ while (vcpu_mc->nr_pages)
+ hyp_put_page(&selftest_vm.pool, pop_hyp_memcache(vcpu_mc, hyp_phys_to_virt));
+
hyp_spin_lock(&vm_table_lock);
remove_vm_table_entry(selftest_vm.kvm.arch.pkvm.handle);
hyp_spin_unlock(&vm_table_lock);
+
+ reclaim_pgtable_pages(&selftest_vm, &mc);
}
#endif /* CONFIG_NVHE_EL2_DEBUG */
diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setup.c
index e72b7626799d..b2a953adedc3 100644
--- a/arch/arm64/kvm/hyp/nvhe/setup.c
+++ b/arch/arm64/kvm/hyp/nvhe/setup.c
@@ -29,7 +29,6 @@ static void *vmemmap_base;
static void *vm_table_base;
static void *hyp_pgt_base;
static void *host_s2_pgt_base;
-static void *selftest_base;
static void *ffa_proxy_pages;
static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops;
static struct hyp_pool hpool;
@@ -40,11 +39,6 @@ static int divide_memory_pool(void *virt, unsigned long size)
hyp_early_alloc_init(virt, size);
- nr_pages = pkvm_selftest_pages();
- selftest_base = hyp_early_alloc_contig(nr_pages);
- if (nr_pages && !selftest_base)
- return -ENOMEM;
-
nr_pages = hyp_vmemmap_pages(sizeof(struct hyp_page));
vmemmap_base = hyp_early_alloc_contig(nr_pages);
if (!vmemmap_base)
@@ -385,8 +379,6 @@ void __noreturn __pkvm_init_finalise(void)
goto out;
pkvm_hyp_vm_table_init(vm_table_base);
-
- pkvm_ownership_selftest(selftest_base);
out:
/*
* We tail-called to here from handle___pkvm_init() and will not return,
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 016997288fd2..29e7a51a1c03 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -64,7 +64,6 @@ void __init kvm_hyp_reserve(void)
hyp_mem_pages += host_s2_pgtable_pages();
hyp_mem_pages += hyp_vm_table_pages();
hyp_mem_pages += hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE);
- hyp_mem_pages += pkvm_selftest_pages();
hyp_mem_pages += hyp_ffa_proxy_pages();
/*
@@ -333,9 +332,36 @@ static int __init pkvm_drop_host_privileges(void)
return ret;
}
-static void __init pkvm_selftests(void)
-{
#ifdef CONFIG_NVHE_EL2_DEBUG
+static void __init pkvm_ownership_selftest(void)
+{
+ unsigned long pgtable_order = 5;
+ unsigned long pgtable = 0;
+ unsigned long page = 0;
+ long ret = -ENOMEM;
+
+ /* Memory for the VM page-table */
+ pgtable = __get_free_pages(GFP_KERNEL, pgtable_order);
+ if (!pgtable)
+ goto out;
+
+ /* Page whose ownership will be tested */
+ page = __get_free_page(GFP_KERNEL);
+ if (!page)
+ goto out;
+
+ ret = kvm_call_hyp_nvhe(__pkvm_ownership_selftest, virt_to_pfn((void *)page),
+ virt_to_pfn((void *)pgtable), 1 << pgtable_order);
+out:
+ if (ret)
+ kvm_err("pKVM ownership selftest failed (%ld)\n", ret);
+
+ free_pages(pgtable, pgtable_order);
+ free_page(page);
+}
+
+static void __init pkvm_hyp_alloc_selftest(void)
+{
int ret = pkvm_call_hyp_req(__pkvm_hyp_alloc_selftest);
unsigned long reclaimed;
@@ -346,9 +372,17 @@ static void __init pkvm_selftests(void)
WARN_ON(reclaimed != 6 /* SELFTEST_MAX_PAGES */);
else
kvm_err("pKVM hyp allocator selftest failed (%d)\n", ret);
-#endif
}
+static void __init pkvm_selftests(void)
+{
+ pkvm_ownership_selftest();
+ pkvm_hyp_alloc_selftest();
+}
+#else
+static void __init pkvm_selftests(void) { }
+#endif
+
static unsigned long pkvm_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
{
unsigned long reclaimable = 0;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 06/24] KVM: arm64: Reject zero-sized ranges in pfn_range_is_valid()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (4 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 05/24] KVM: arm64: Make pKVM ownership selftest an HVC Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 07/24] KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp() Vincent Donnefort
` (18 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
pfn_range_is_valid() validates that a [pfn, pfn + nr_pages) range fits
within the physical address space, but does nothing when nr_pages is 0.
Luckily, there's currently no existing check that could be evaded.
However the pfn_range_is_valid() should make size trustworthy and 0
could be problematic. Explicitly reject 0 range size.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 8d5bfae67396..385a7ed12b3c 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -400,6 +400,9 @@ static bool pfn_range_is_valid(u64 pfn, u64 nr_pages)
{
u64 limit = BIT(kvm_phys_shift(&host_mmu.arch.mmu) - PAGE_SHIFT);
+ if (!nr_pages)
+ return false;
+
return pfn < limit && ((limit - pfn) >= nr_pages);
}
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 07/24] KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (5 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 06/24] KVM: arm64: Reject zero-sized ranges in pfn_range_is_valid() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 13:46 ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 08/24] KVM: arm64: Add a range to __pkvm_host_donate_guest() Vincent Donnefort
` (17 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
Nothing prevents the host/hyp sharing transition from working on a
range. In preparation for testing huge mappings with the pKVM ownership
selftest, add a range to these hypercalls to allow them to be tested
just like the others without any special case.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 4 +-
arch/arm64/kvm/hyp/nvhe/ffa.c | 12 ++---
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 +-
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 50 +++++++++++--------
4 files changed, 38 insertions(+), 32 deletions(-)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index a1c9c71beb36..ec0bf9dff899 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -31,10 +31,10 @@ enum pkvm_component_id {
};
int __pkvm_prot_finalize(void);
-int __pkvm_host_share_hyp(u64 pfn);
+int __pkvm_host_share_hyp(u64 pfn, u64 nr_pages);
int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
-int __pkvm_host_unshare_hyp(u64 pfn);
+int __pkvm_host_unshare_hyp(u64 pfn, u64 nr_pages);
int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);
diff --git a/arch/arm64/kvm/hyp/nvhe/ffa.c b/arch/arm64/kvm/hyp/nvhe/ffa.c
index 5ad74ab265b5..cfcf93770c77 100644
--- a/arch/arm64/kvm/hyp/nvhe/ffa.c
+++ b/arch/arm64/kvm/hyp/nvhe/ffa.c
@@ -286,13 +286,13 @@ static void do_ffa_rxtx_map(struct arm_smccc_1_2_regs *res,
if (ret)
goto out_unlock;
- ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(tx));
+ ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(tx), 1);
if (ret) {
ret = FFA_RET_INVALID_PARAMETERS;
goto err_unmap;
}
- ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(rx));
+ ret = __pkvm_host_share_hyp(hyp_phys_to_pfn(rx), 1);
if (ret) {
ret = FFA_RET_INVALID_PARAMETERS;
goto err_unshare_tx;
@@ -324,9 +324,9 @@ static void do_ffa_rxtx_map(struct arm_smccc_1_2_regs *res,
err_unpin_tx:
hyp_unpin_shared_mem(tx_virt, tx_virt + 1);
err_unshare_rx:
- __pkvm_host_unshare_hyp(hyp_phys_to_pfn(rx));
+ __pkvm_host_unshare_hyp(hyp_phys_to_pfn(rx), 1);
err_unshare_tx:
- __pkvm_host_unshare_hyp(hyp_phys_to_pfn(tx));
+ __pkvm_host_unshare_hyp(hyp_phys_to_pfn(tx), 1);
err_unmap:
ffa_unmap_hyp_buffers();
goto out_unlock;
@@ -355,11 +355,11 @@ static void do_ffa_rxtx_unmap(struct arm_smccc_1_2_regs *res,
}
hyp_unpin_shared_mem(host_buffers.tx, host_buffers.tx + 1);
- WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.tx)));
+ WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.tx), 1));
host_buffers.tx = NULL;
hyp_unpin_shared_mem(host_buffers.rx, host_buffers.rx + 1);
- WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.rx)));
+ WARN_ON(__pkvm_host_unshare_hyp(hyp_virt_to_pfn(host_buffers.rx), 1));
host_buffers.rx = NULL;
ffa_unmap_hyp_buffers();
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index bf3d13a15b2f..853dad542597 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -1282,13 +1282,13 @@ DEFINE_KVM_HOST_HCALL(int, __pkvm_cpu_set_vector,
DEFINE_KVM_HOST_HCALL(int, __pkvm_host_share_hyp,
u64, pfn)
{
- return __pkvm_host_share_hyp(pfn);
+ return __pkvm_host_share_hyp(pfn, 1);
}
DEFINE_KVM_HOST_HCALL(int, __pkvm_host_unshare_hyp,
u64, pfn)
{
- return __pkvm_host_unshare_hyp(pfn);
+ return __pkvm_host_unshare_hyp(pfn, 1);
}
DEFINE_KVM_HOST_HCALL(ulong, __pkvm_create_private_mapping,
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 385a7ed12b3c..f7d249d35f50 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -992,12 +992,15 @@ int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu)
return ret;
}
-int __pkvm_host_share_hyp(u64 pfn)
+int __pkvm_host_share_hyp(u64 pfn, u64 nr_pages)
{
u64 phys = hyp_pfn_to_phys(pfn);
- u64 size = PAGE_SIZE;
+ u64 size = PAGE_SIZE * nr_pages;
int ret;
+ if (!pfn_range_is_valid(pfn, nr_pages))
+ return -EINVAL;
+
host_lock_component();
hyp_lock_component();
@@ -1084,12 +1087,15 @@ int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn)
return ret;
}
-int __pkvm_host_unshare_hyp(u64 pfn)
+int __pkvm_host_unshare_hyp(u64 pfn, u64 nr_pages)
{
u64 phys = hyp_pfn_to_phys(pfn);
- u64 size = PAGE_SIZE;
+ u64 size = PAGE_SIZE * nr_pages;
int ret;
+ if (!pfn_range_is_valid(pfn, nr_pages))
+ return -EINVAL;
+
host_lock_component();
hyp_lock_component();
@@ -1807,8 +1813,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.guest[0] = selftest_state.guest[1] = PKVM_NOPAGE;
assert_page_state();
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
@@ -1820,15 +1826,15 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.hyp = PKVM_NOPAGE;
assert_transition_res(0, __pkvm_hyp_donate_host, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1);
assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
selftest_state.host = PKVM_PAGE_SHARED_OWNED;
selftest_state.hyp = PKVM_PAGE_SHARED_BORROWED;
- assert_transition_res(0, __pkvm_host_share_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
+ assert_transition_res(0, __pkvm_host_share_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
@@ -1840,8 +1846,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size);
hyp_unpin_shared_mem(virt, virt + size);
WARN_ON(hyp_page_count(virt) != 1);
- assert_transition_res(-EBUSY, __pkvm_host_unshare_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
+ assert_transition_res(-EBUSY, __pkvm_host_unshare_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
@@ -1855,15 +1861,15 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.hyp = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(0, __pkvm_host_unshare_hyp, pfn, 1);
selftest_state.host = PKVM_PAGE_SHARED_OWNED;
selftest_state.hyp = PKVM_NOPAGE;
assert_transition_res(0, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
@@ -1881,8 +1887,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
@@ -1907,8 +1913,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
selftest_state.host = PKVM_PAGE_SHARED_BORROWED;
@@ -1921,8 +1927,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
selftest_state.host = PKVM_NOPAGE;
@@ -1935,8 +1941,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
selftest_state.host = PKVM_PAGE_OWNED;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 07/24] KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp()
2026-12-03 11:51 ` [PATCH v6 07/24] KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp() Vincent Donnefort
@ 2026-12-03 13:46 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 13:46 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: arm64: commit message says hypercalls gain range while ABI
stays 1 page
--
commit 43a888e5880d5dd8131f2b84615fc229cfac4c11
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp()
This commit adds an nr_pages parameter to __pkvm_host_share_hyp() and
__pkvm_host_unshare_hyp() in mem_protect.c and updates existing callers
to pass 1.
> Nothing prevents the host/hyp sharing transition from working on a
> range. In preparation for testing huge mappings with the pKVM ownership
> selftest, add a range to these hypercalls to allow them to be tested
> just like the others without any special case.
[Severity: Low]
Should this refer to the EL2 helpers rather than the hypercalls? The
host-to-hyp hypercall ABI in arch/arm64/include/asm/kvm_hcall.h still
takes only pfn, and the handlers in arch/arm64/kvm/hyp/nvhe/hyp-main.c
hardcode nr_pages to 1.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=7
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 08/24] KVM: arm64: Add a range to __pkvm_host_donate_guest()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (6 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 07/24] KVM: arm64: Add a range to __pkvm_host_share/unshare_hyp() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 09/24] KVM: arm64: Add a range to hyp_poison_page() Vincent Donnefort
` (16 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for supporting stage-2 huge mappings for protected VMs,
add a nr_pages argument to the __pkvm_host_donate_guest() hypercall.
This range supports only two values: 1 or PMD_SIZE / PAGE_SIZE (that is
512 on a 4K-pages system).
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/include/asm/kvm_hcall.h | 2 +-
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 2 +-
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 +-
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 54 +++++++++++--------
arch/arm64/kvm/pkvm.c | 2 +-
5 files changed, 36 insertions(+), 28 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_hcall.h b/arch/arm64/include/asm/kvm_hcall.h
index 64d18b7f2392..c47ee2653110 100644
--- a/arch/arm64/include/asm/kvm_hcall.h
+++ b/arch/arm64/include/asm/kvm_hcall.h
@@ -221,7 +221,7 @@ DECLARE_KVM_HOST_HCALL(int, __pkvm_host_share_hyp,
DECLARE_KVM_HOST_HCALL(int, __pkvm_host_unshare_hyp,
u64, pfn)
DECLARE_KVM_HOST_HCALL(int, __pkvm_host_donate_guest,
- u64, pfn, u64, gfn)
+ u64, pfn, u64, gfn, u64, nr_pages)
DECLARE_KVM_HOST_HCALL(int, __pkvm_host_share_guest,
u64, pfn, u64, gfn, u64, nr_pages, u64, prot)
DECLARE_KVM_HOST_HCALL(int, __pkvm_host_unshare_guest,
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index ec0bf9dff899..b04cedaf4ec6 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -39,7 +39,7 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
int __pkvm_host_share_ffa(u64 pfn, u64 nr_pages);
int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);
-int __pkvm_host_donate_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu);
+int __pkvm_host_donate_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu);
int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu);
int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys);
int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm);
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 853dad542597..4f420718ca6a 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -965,7 +965,7 @@ static int pkvm_refill_memcache(struct pkvm_hyp_vcpu *hyp_vcpu)
}
DEFINE_KVM_HOST_HCALL(int, __pkvm_host_donate_guest,
- u64, pfn, u64, gfn)
+ u64, pfn, u64, gfn, u64, nr_pages)
{
struct pkvm_hyp_vcpu *hyp_vcpu;
int ret;
@@ -978,7 +978,7 @@ DEFINE_KVM_HOST_HCALL(int, __pkvm_host_donate_guest,
if (ret)
return ret;
- return __pkvm_host_donate_guest(pfn, gfn, hyp_vcpu);
+ return __pkvm_host_donate_guest(pfn, gfn, nr_pages, hyp_vcpu);
}
DEFINE_KVM_HOST_HCALL(int, __pkvm_host_share_guest,
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index f7d249d35f50..f14bcc1b0de9 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -1438,9 +1438,9 @@ int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm)
/*
* share/donate install at most one stage-2 leaf (PAGE_SIZE, or one
- * KVM_PGTABLE_LAST_LEVEL - 1 block for share). kvm_mmu_cache_min_pages()
- * bounds the worst-case allocation: exact for the PAGE_SIZE leaf,
- * conservative by one for the block.
+ * KVM_PGTABLE_LAST_LEVEL - 1 block). kvm_mmu_cache_min_pages() bounds the
+ * worst-case allocation: exact for the PAGE_SIZE leaf, conservative by one
+ * for the block.
*/
static int __guest_check_pgtable_memcache(struct pkvm_hyp_vcpu *vcpu)
{
@@ -1452,22 +1452,30 @@ static int __guest_check_pgtable_memcache(struct pkvm_hyp_vcpu *vcpu)
return 0;
}
-int __pkvm_host_donate_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu)
+int __pkvm_host_donate_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu)
{
struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
u64 phys = hyp_pfn_to_phys(pfn);
u64 ipa = hyp_pfn_to_phys(gfn);
+ u64 size;
u64 meta;
int ret;
+ if (!pfn_range_is_valid(pfn, nr_pages))
+ return -EINVAL;
+
+ ret = __guest_check_transition_size(phys, ipa, nr_pages, &size);
+ if (ret)
+ return ret;
+
host_lock_component();
guest_lock_component(vm);
- ret = __host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_OWNED);
+ ret = __host_check_page_state_range(phys, size, PKVM_PAGE_OWNED);
if (ret)
goto unlock;
- ret = __guest_check_page_state_range(vm, ipa, PAGE_SIZE, PKVM_NOPAGE);
+ ret = __guest_check_page_state_range(vm, ipa, size, PKVM_NOPAGE);
if (ret)
goto unlock;
@@ -1476,9 +1484,9 @@ int __pkvm_host_donate_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu)
goto unlock;
meta = host_stage2_encode_gfn_meta(vm, gfn);
- WARN_ON(host_stage2_set_owner_metadata_locked(phys, PAGE_SIZE,
+ WARN_ON(host_stage2_set_owner_metadata_locked(phys, size,
PKVM_ID_GUEST, meta));
- WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys,
+ WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys,
pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_OWNED),
&vcpu->vcpu.arch.stage2_mc, 0));
@@ -1820,7 +1828,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.hyp = PKVM_NOPAGE;
@@ -1840,7 +1848,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size);
assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size);
@@ -1853,7 +1861,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
hyp_unpin_shared_mem(virt, virt + size);
assert_page_state();
@@ -1873,7 +1881,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
selftest_state.host = PKVM_PAGE_OWNED;
@@ -1890,7 +1898,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
selftest_state.guest[1] = PKVM_PAGE_SHARED_BORROWED;
@@ -1906,9 +1914,9 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.host = PKVM_NOPAGE;
selftest_state.guest[0] = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu);
+ assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
@@ -1921,8 +1929,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.guest[0] = PKVM_PAGE_SHARED_OWNED;
assert_transition_res(0, __pkvm_guest_share_host, vcpu, gfn);
assert_transition_res(-EPERM, __pkvm_guest_share_host, vcpu, gfn);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
@@ -1935,8 +1943,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.guest[0] = PKVM_PAGE_OWNED;
assert_transition_res(0, __pkvm_guest_unshare_host, vcpu, gfn);
assert_transition_res(-EPERM, __pkvm_guest_unshare_host, vcpu, gfn);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
@@ -1948,19 +1956,19 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.guest[0] = PKVM_POISON;
assert_transition_res(0, __pkvm_host_force_reclaim_page_guest, phys);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-EHWPOISON, __pkvm_guest_share_host, vcpu, gfn);
assert_transition_res(-EHWPOISON, __pkvm_guest_unshare_host, vcpu, gfn);
selftest_state.host = PKVM_NOPAGE;
selftest_state.guest[1] = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn + 1, vcpu);
+ assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.guest[1] = PKVM_NOPAGE;
assert_transition_res(0, __pkvm_host_reclaim_page_guest, gfn + 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
selftest_state.host = PKVM_NOPAGE;
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 29e7a51a1c03..d1acdca0254f 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -594,7 +594,7 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
return ret ? -EFAULT : -EAGAIN;
}
- ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn);
+ ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, 1);
} else {
if (WARN_ON_ONCE(size != PAGE_SIZE && size != PMD_SIZE))
return -EINVAL;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 09/24] KVM: arm64: Add a range to hyp_poison_page()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (7 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 08/24] KVM: arm64: Add a range to __pkvm_host_donate_guest() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 14:26 ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 10/24] KVM: arm64: Add a range to __pkvm_host_reclaim_guest() Vincent Donnefort
` (15 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for supporting stage-2 huge mappings for protected VMs,
add a nr_pages argument to the memory poisoning mechanism. Use the
existing __apply_guest_page() helper which speeds up the process by
using a PMD_SIZE fixmap when possible.
Also, rename the function to hyp_poison_range() to reflect the new
capability.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index f14bcc1b0de9..555566bbb9c2 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -1299,11 +1299,9 @@ static int __guest_check_transition_size(u64 phys, u64 ipa, u64 nr_pages, u64 *s
return 0;
}
-static void hyp_poison_page(phys_addr_t phys)
+static void __hyp_poison_page(void *addr, size_t size)
{
- void *addr = hyp_fixmap_map(phys);
-
- memset(addr, 0, PAGE_SIZE);
+ memset(addr, 0, size);
/*
* Prefer kvm_flush_dcache_to_poc() over __clean_dcache_guest_page()
* here as the latter may elide the CMO under the assumption that FWB
@@ -1311,8 +1309,12 @@ static void hyp_poison_page(phys_addr_t phys)
* host stage-2 and would otherwise lead to a malicious host potentially
* being able to read the contents of newly reclaimed guest pages.
*/
- kvm_flush_dcache_to_poc(addr, PAGE_SIZE);
- hyp_fixmap_unmap();
+ kvm_flush_dcache_to_poc(addr, size);
+}
+
+static void hyp_poison_range(phys_addr_t phys, u64 size)
+{
+ __apply_guest_page(__hyp_va(phys), size, __hyp_poison_page);
}
static int host_stage2_get_guest_info(phys_addr_t phys, struct pkvm_hyp_vm **vm,
@@ -1384,7 +1386,7 @@ int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
if (ret)
goto unlock_guest;
- hyp_poison_page(phys);
+ hyp_poison_range(phys, PAGE_SIZE);
WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST));
unlock_guest:
guest_unlock_component(vm);
@@ -1412,7 +1414,7 @@ int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm)
switch (guest_get_page_state(pte, ipa)) {
case PKVM_PAGE_OWNED:
WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE));
- hyp_poison_page(phys);
+ hyp_poison_range(phys, PAGE_SIZE);
break;
case PKVM_PAGE_SHARED_OWNED:
WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED));
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 09/24] KVM: arm64: Add a range to hyp_poison_page()
2026-12-03 11:51 ` [PATCH v6 09/24] KVM: arm64: Add a range to hyp_poison_page() Vincent Donnefort
@ 2026-12-03 14:26 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 14:26 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: arm64: changelog claims nr_pages instead of size in
hyp_poison_range()
--
commit f7bbe383208e5b4482797114ee8c110e048b2ff9
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Add a range to hyp_poison_page()
This commit renames hyp_poison_page() to hyp_poison_range() and extends
it to take a size argument, using __apply_guest_page() so that PMD_SIZE
fixmaps can be used when possible.
> In preparation for supporting stage-2 huge mappings for protected VMs,
> add a nr_pages argument to the memory poisoning mechanism.
[Severity: Low]
Should this say a size argument rather than nr_pages? hyp_poison_range()
takes a byte size (u64 size) and passes it directly to
__apply_guest_page(), and both callers pass PAGE_SIZE rather than a page
count.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=9
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 10/24] KVM: arm64: Add a range to __pkvm_host_reclaim_guest()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (8 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 09/24] KVM: arm64: Add a range to hyp_poison_page() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 14:47 ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 11/24] KVM: arm64: Add a range to __pkvm_guest_share_host() Vincent Donnefort
` (14 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for supporting stage-2 huge mappings for protected VMs,
add a nr_pages argument to the __pkvm_host_reclaim_guest() hypercall.
This range supports only two values: 1 or PMD_SIZE / PAGE_SIZE (that is
512 on a 4K-pages system).
Also, rename both the mem_protect function and the HVC to
__pkvm_host_reclaim_guest() as it can now operate on a range.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/include/asm/kvm_asm.h | 2 +-
arch/arm64/include/asm/kvm_hcall.h | 4 +-
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 2 +-
arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 2 +-
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 8 +--
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 58 +++++++++++--------
arch/arm64/kvm/hyp/nvhe/pkvm.c | 4 +-
arch/arm64/kvm/pkvm.c | 5 +-
8 files changed, 49 insertions(+), 36 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
index ed7bd93aea1b..3efc63e1a823 100644
--- a/arch/arm64/include/asm/kvm_asm.h
+++ b/arch/arm64/include/asm/kvm_asm.h
@@ -116,7 +116,7 @@ enum __kvm_host_smccc_func {
__KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu,
__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_in_poison_fault,
__KVM_HOST_SMCCC_FUNC___pkvm_force_reclaim_guest_page,
- __KVM_HOST_SMCCC_FUNC___pkvm_reclaim_dying_guest_page,
+ __KVM_HOST_SMCCC_FUNC___pkvm_reclaim_dying_guest,
__KVM_HOST_SMCCC_FUNC___pkvm_start_teardown_vm,
__KVM_HOST_SMCCC_FUNC___pkvm_finalize_teardown_vm,
__KVM_HOST_SMCCC_FUNC___pkvm_vcpu_load,
diff --git a/arch/arm64/include/asm/kvm_hcall.h b/arch/arm64/include/asm/kvm_hcall.h
index c47ee2653110..201d58907109 100644
--- a/arch/arm64/include/asm/kvm_hcall.h
+++ b/arch/arm64/include/asm/kvm_hcall.h
@@ -244,8 +244,8 @@ DECLARE_KVM_HOST_HCALL(int, __pkvm_init_vcpu,
DECLARE_KVM_HOST_HCALL0(int, __pkvm_vcpu_in_poison_fault)
DECLARE_KVM_HOST_HCALL(int, __pkvm_force_reclaim_guest_page,
phys_addr_t, phys)
-DECLARE_KVM_HOST_HCALL(int, __pkvm_reclaim_dying_guest_page,
- pkvm_handle_t, handle, u64, gfn)
+DECLARE_KVM_HOST_HCALL(int, __pkvm_reclaim_dying_guest,
+ pkvm_handle_t, handle, u64, gfn, u64, nr_pages)
DECLARE_KVM_HOST_HCALL(int, __pkvm_start_teardown_vm,
pkvm_handle_t, handle)
DECLARE_KVM_HOST_HCALL(int, __pkvm_finalize_teardown_vm,
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index b04cedaf4ec6..b05b0bcc3f2d 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -42,7 +42,7 @@ int __pkvm_host_unshare_ffa(u64 pfn, u64 nr_pages);
int __pkvm_host_donate_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu);
int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu);
int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys);
-int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm);
+int __pkvm_host_reclaim_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm);
int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu,
enum kvm_pgtable_prot prot);
int __pkvm_host_unshare_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *hyp_vm);
diff --git a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
index aa5cf5568504..b79567a5fe44 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/pkvm.h
@@ -111,7 +111,7 @@ void __pkvm_unreserve_vm(pkvm_handle_t handle);
int __pkvm_init_vm(struct kvm *host_kvm, void __kern *pgd_hva);
int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu);
-int __pkvm_reclaim_dying_guest_page(pkvm_handle_t handle, u64 gfn);
+int __pkvm_reclaim_dying_guest(pkvm_handle_t handle, u64 gfn, u64 nr_pages);
int __pkvm_start_teardown_vm(pkvm_handle_t handle);
int __pkvm_finalize_teardown_vm(pkvm_handle_t handle);
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 4f420718ca6a..2621a4ef7e1e 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -1360,10 +1360,10 @@ DEFINE_KVM_HOST_HCALL(int, __pkvm_force_reclaim_guest_page,
return __pkvm_host_force_reclaim_page_guest(phys);
}
-DEFINE_KVM_HOST_HCALL(int, __pkvm_reclaim_dying_guest_page,
- pkvm_handle_t, handle, u64, gfn)
+DEFINE_KVM_HOST_HCALL(int, __pkvm_reclaim_dying_guest,
+ pkvm_handle_t, handle, u64, gfn, u64, nr_pages)
{
- return __pkvm_reclaim_dying_guest_page(handle, gfn);
+ return __pkvm_reclaim_dying_guest(handle, gfn, nr_pages);
}
DEFINE_KVM_HOST_HCALL(int, __pkvm_start_teardown_vm,
@@ -1603,7 +1603,7 @@ static const hcall_t host_hcall[] = {
HANDLE_FUNC(__pkvm_init_vcpu),
HANDLE_FUNC(__pkvm_vcpu_in_poison_fault),
HANDLE_FUNC(__pkvm_force_reclaim_guest_page),
- HANDLE_FUNC(__pkvm_reclaim_dying_guest_page),
+ HANDLE_FUNC(__pkvm_reclaim_dying_guest),
HANDLE_FUNC(__pkvm_start_teardown_vm),
HANDLE_FUNC(__pkvm_finalize_teardown_vm),
HANDLE_FUNC(__pkvm_vcpu_load),
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 555566bbb9c2..12bec74eeade 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -921,7 +921,8 @@ static int __guest_check_page_state_range(struct pkvm_hyp_vm *vm, u64 addr,
return check_page_state_range(&vm->pgt, addr, size, &d);
}
-static int get_valid_guest_pte(struct pkvm_hyp_vm *vm, u64 ipa, kvm_pte_t *ptep, u64 *physp)
+static int __get_valid_guest_pte(struct pkvm_hyp_vm *vm, u64 ipa,
+ kvm_pte_t *ptep, u64 *physp, s8 *levelp)
{
kvm_pte_t pte;
u64 phys;
@@ -935,20 +936,32 @@ static int get_valid_guest_pte(struct pkvm_hyp_vm *vm, u64 ipa, kvm_pte_t *ptep,
return -EHWPOISON;
if (!kvm_pte_valid(pte))
return -ENOENT;
- if (level != KVM_PGTABLE_LAST_LEVEL)
- return -E2BIG;
phys = kvm_pte_to_phys(pte);
- ret = check_range_allowed_memory(phys, phys + PAGE_SIZE);
+ ret = check_range_allowed_memory(phys, phys + kvm_granule_size(level));
if (WARN_ON(ret))
return ret;
*ptep = pte;
*physp = phys;
+ *levelp = level;
return 0;
}
+static int get_valid_guest_pte(struct pkvm_hyp_vm *vm, u64 ipa, u64 size,
+ kvm_pte_t *ptep, u64 *physp)
+{
+ s8 level;
+ int ret;
+
+ ret = __get_valid_guest_pte(vm, ipa, ptep, physp, &level);
+ if (ret)
+ return ret;
+
+ return kvm_granule_size(level) == size ? 0 : -E2BIG;
+}
+
int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu)
{
struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(hyp_vcpu);
@@ -1031,7 +1044,7 @@ int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn)
host_lock_component();
guest_lock_component(vm);
- ret = get_valid_guest_pte(vm, ipa, &pte, &phys);
+ ret = get_valid_guest_pte(vm, ipa, PAGE_SIZE, &pte, &phys);
if (ret)
goto unlock;
@@ -1063,7 +1076,7 @@ int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn)
host_lock_component();
guest_lock_component(vm);
- ret = get_valid_guest_pte(vm, ipa, &pte, &phys);
+ ret = get_valid_guest_pte(vm, ipa, PAGE_SIZE, &pte, &phys);
if (ret)
goto unlock;
@@ -1369,7 +1382,7 @@ int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
ipa = hyp_pfn_to_phys(gfn);
guest_lock_component(vm);
- ret = get_valid_guest_pte(vm, ipa, &pte, &pa);
+ ret = get_valid_guest_pte(vm, ipa, PAGE_SIZE, &pte, &pa);
if (ret)
goto unlock_guest;
@@ -1397,35 +1410,40 @@ int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
return ret;
}
-int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm)
+int __pkvm_host_reclaim_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
{
u64 ipa = hyp_pfn_to_phys(gfn);
kvm_pte_t pte;
u64 phys;
+ u64 size;
int ret;
+ ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
+ if (ret)
+ return ret;
+
host_lock_component();
guest_lock_component(vm);
- ret = get_valid_guest_pte(vm, ipa, &pte, &phys);
+ ret = get_valid_guest_pte(vm, ipa, size, &pte, &phys);
if (ret)
goto unlock;
switch (guest_get_page_state(pte, ipa)) {
case PKVM_PAGE_OWNED:
- WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE));
- hyp_poison_range(phys, PAGE_SIZE);
+ WARN_ON(__host_check_page_state_range(phys, size, PKVM_NOPAGE));
+ hyp_poison_range(phys, size);
break;
case PKVM_PAGE_SHARED_OWNED:
- WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED));
+ WARN_ON(__host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED));
break;
default:
ret = -EPERM;
goto unlock;
}
- WARN_ON(kvm_pgtable_stage2_unmap(&vm->pgt, ipa, PAGE_SIZE));
- WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST));
+ WARN_ON(kvm_pgtable_stage2_unmap(&vm->pgt, ipa, size));
+ WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HOST));
unlock:
guest_unlock_component(vm);
@@ -1578,11 +1596,10 @@ static int __check_host_shared_guest(struct pkvm_hyp_vm *vm, u64 *__phys, u64 ip
s8 level;
int ret;
- ret = kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level, 0);
+ ret = __get_valid_guest_pte(vm, ipa, &pte, &phys, &level);
if (ret)
return ret;
- if (!kvm_pte_valid(pte))
- return -ENOENT;
+
if (size && kvm_granule_size(level) != size)
return -E2BIG;
@@ -1593,11 +1610,6 @@ static int __check_host_shared_guest(struct pkvm_hyp_vm *vm, u64 *__phys, u64 ip
if (state != PKVM_PAGE_SHARED_BORROWED)
return -EPERM;
- phys = kvm_pte_to_phys(pte);
- ret = check_range_allowed_memory(phys, phys + size);
- if (WARN_ON(ret))
- return ret;
-
for_each_hyp_page(page, phys, size) {
if (get_host_state(page) != PKVM_PAGE_SHARED_OWNED)
return -EPERM;
@@ -1969,7 +1981,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.guest[1] = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_host_reclaim_page_guest, gfn + 1, vm);
+ assert_transition_res(0, __pkvm_host_reclaim_guest, gfn + 1, 1, vm);
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index ae34c09f1f39..2a73e63e8f81 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -1042,7 +1042,7 @@ int __pkvm_init_vcpu(pkvm_handle_t handle, struct kvm_vcpu *host_vcpu)
return ret;
}
-int __pkvm_reclaim_dying_guest_page(pkvm_handle_t handle, u64 gfn)
+int __pkvm_reclaim_dying_guest(pkvm_handle_t handle, u64 gfn, u64 nr_pages)
{
struct pkvm_hyp_vm *hyp_vm = get_pkvm_hyp_vm(handle);
int ret = -EINVAL;
@@ -1051,7 +1051,7 @@ int __pkvm_reclaim_dying_guest_page(pkvm_handle_t handle, u64 gfn)
return ret;
if (hyp_vm->kvm.arch.pkvm.is_dying)
- ret = __pkvm_host_reclaim_page_guest(gfn, hyp_vm);
+ ret = __pkvm_host_reclaim_guest(gfn, nr_pages, hyp_vm);
put_pkvm_hyp_vm(hyp_vm);
return ret;
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index d1acdca0254f..4c80e0960154 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -498,8 +498,9 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
for_each_mapping_in_range_safe(pgt, start, end, mapping) {
struct page *page;
- ret = kvm_call_hyp_nvhe(__pkvm_reclaim_dying_guest_page,
- handle, mapping->gfn);
+ ret = kvm_call_hyp_nvhe(__pkvm_reclaim_dying_guest,
+ handle, mapping->gfn,
+ (u64)mapping->nr_pages);
if (WARN_ON(ret))
continue;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 10/24] KVM: arm64: Add a range to __pkvm_host_reclaim_guest()
2026-12-03 11:51 ` [PATCH v6 10/24] KVM: arm64: Add a range to __pkvm_host_reclaim_guest() Vincent Donnefort
@ 2026-12-03 14:47 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 14:47 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] KVM: arm64: PMD_SIZE reclaim in __pkvm_host_reclaim_guest()
stalls EL2
- [Low] KVM: arm64: commit message misnames __pkvm_reclaim_dying_guest()
HVC
--
commit 09b9348d8f7f8f99dfcf78626a4ea1cc2e74c269
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Add a range to __pkvm_host_reclaim_guest()
This commit extends dying-guest reclaim to accept a nr_pages argument of
either 1 or PMD_SIZE / PAGE_SIZE, and renames the helper and hypercall
accordingly.
> In preparation for supporting stage-2 huge mappings for protected VMs,
> add a nr_pages argument to the __pkvm_host_reclaim_guest() hypercall.
> This range supports only two values: 1 or PMD_SIZE / PAGE_SIZE (that is
> 512 on a 4K-pages system).
>
> Also, rename both the mem_protect function and the HVC to
> __pkvm_host_reclaim_guest() as it can now operate on a range.
[Severity: Low]
This isn't a bug, but the hypercall is renamed to
__pkvm_reclaim_dying_guest(), while only the mem_protect.c helper is named
__pkvm_host_reclaim_guest(). Should the commit message reflect that?
[ ... ]
> diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> index 555566bbb9c2b..12bec74eeade2 100644
> --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
[ ... ]
> @@ -1397,35 +1410,40 @@ int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
> return ret;
> }
>
> -int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm)
> +int __pkvm_host_reclaim_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *vm)
> {
> u64 ipa = hyp_pfn_to_phys(gfn);
> kvm_pte_t pte;
> u64 phys;
> + u64 size;
> int ret;
>
> + ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
> + if (ret)
> + return ret;
> +
> host_lock_component();
> guest_lock_component(vm);
>
> - ret = get_valid_guest_pte(vm, ipa, &pte, &phys);
> + ret = get_valid_guest_pte(vm, ipa, size, &pte, &phys);
> if (ret)
> goto unlock;
>
> switch (guest_get_page_state(pte, ipa)) {
> case PKVM_PAGE_OWNED:
> - WARN_ON(__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE));
> - hyp_poison_range(phys, PAGE_SIZE);
> + WARN_ON(__host_check_page_state_range(phys, size, PKVM_NOPAGE));
> + hyp_poison_range(phys, size);
> break;
[Severity: Medium]
Can hyp_poison_range() over a PMD_SIZE block stall EL2 and hold
host_mmu.lock for too long?
When tearing down a dying protected VM with PMD mappings, this zeroes and
flushes up to 2 MiB (4K pages), 32 MiB (16K pages), or 512 MiB (64K pages)
to PoC inside a single non-preemptible, IRQ-masked EL2 hypercall while
holding host_mmu.lock and vm->lock.
On 4K and 16K kernels, hyp_fixblock_map() also holds hyp_fixblock_lock
across the whole block. On 64K kernels, HAS_FIXBLOCK is undefined, so
__apply_guest_page() loops 8,192 times with per-page fixmaps, issuing a
synchronous broadcast vale2is TLBI in fixmap_clear_slot() each iteration.
Commit 0bf5f4d400cd ("KVM: arm64: Introduce
__pkvm_reclaim_dying_guest_page()") reclaimed one page per HVC so EL1
could reschedule. Poisoning a full PMD under host_mmu.lock risks long
IRQ-off stalls and lockups while blocking other CPUs on host_mmu.lock.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=10
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 11/24] KVM: arm64: Add a range to __pkvm_guest_share_host()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (9 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 10/24] KVM: arm64: Add a range to __pkvm_host_reclaim_guest() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 12/24] KVM: arm64: Add a range to __pkvm_guest_unshare_host() Vincent Donnefort
` (13 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for supporting stage-2 huge mappings for protected VMs,
add a nr_pages argument to __pkvm_guest_share_host(). This range
supports only two values: 1 or PMD_SIZE / PAGE_SIZE (that is 512 on a
4K-pages system).
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 2 +-
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 24 ++++++++++++-------
arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +-
3 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index b05b0bcc3f2d..6741c9ecb879 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -32,7 +32,7 @@ enum pkvm_component_id {
int __pkvm_prot_finalize(void);
int __pkvm_host_share_hyp(u64 pfn, u64 nr_pages);
-int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
+int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn, u64 nr_pages);
int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
int __pkvm_host_unshare_hyp(u64 pfn, u64 nr_pages);
int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 12bec74eeade..4aa00aa09526 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -1034,31 +1034,37 @@ int __pkvm_host_share_hyp(u64 pfn, u64 nr_pages)
return ret;
}
-int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn)
+static int __guest_check_transition_size(u64 phys, u64 ipa, u64 nr_pages, u64 *size);
+
+int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn, u64 nr_pages)
{
struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
- u64 phys, ipa = hyp_pfn_to_phys(gfn);
+ u64 size, phys, ipa = hyp_pfn_to_phys(gfn);
kvm_pte_t pte;
int ret;
+ ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
+ if (ret)
+ return ret;
+
host_lock_component();
guest_lock_component(vm);
- ret = get_valid_guest_pte(vm, ipa, PAGE_SIZE, &pte, &phys);
+ ret = get_valid_guest_pte(vm, ipa, size, &pte, &phys);
if (ret)
goto unlock;
ret = -EPERM;
if (pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte)) != PKVM_PAGE_OWNED)
goto unlock;
- if (__host_check_page_state_range(phys, PAGE_SIZE, PKVM_NOPAGE))
+ if (__host_check_page_state_range(phys, size, PKVM_NOPAGE))
goto unlock;
ret = 0;
- WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys,
+ WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys,
pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_SHARED_OWNED),
&vcpu->vcpu.arch.stage2_mc, 0));
- WARN_ON(__host_set_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED));
+ WARN_ON(__host_set_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED));
unlock:
guest_unlock_component(vm);
host_unlock_component();
@@ -1941,8 +1947,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.host = PKVM_PAGE_SHARED_BORROWED;
selftest_state.guest[0] = PKVM_PAGE_SHARED_OWNED;
- assert_transition_res(0, __pkvm_guest_share_host, vcpu, gfn);
- assert_transition_res(-EPERM, __pkvm_guest_share_host, vcpu, gfn);
+ assert_transition_res(0, __pkvm_guest_share_host, vcpu, gfn, 1);
+ assert_transition_res(-EPERM, __pkvm_guest_share_host, vcpu, gfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
@@ -1972,7 +1978,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(0, __pkvm_host_force_reclaim_page_guest, phys);
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-EHWPOISON, __pkvm_guest_share_host, vcpu, gfn);
+ assert_transition_res(-EHWPOISON, __pkvm_guest_share_host, vcpu, gfn, 1);
assert_transition_res(-EHWPOISON, __pkvm_guest_unshare_host, vcpu, gfn);
selftest_state.host = PKVM_NOPAGE;
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index 2a73e63e8f81..66d7493a6549 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -1178,7 +1178,7 @@ static bool pkvm_memshare_call(u64 *ret, struct kvm_vcpu *vcpu, u64 *exit_code)
goto out_guest;
hyp_vcpu = container_of(vcpu, struct pkvm_hyp_vcpu, vcpu);
- switch (__pkvm_guest_share_host(hyp_vcpu, hyp_phys_to_pfn(ipa))) {
+ switch (__pkvm_guest_share_host(hyp_vcpu, hyp_phys_to_pfn(ipa), 1)) {
case 0:
ret[0] = SMCCC_RET_SUCCESS;
goto out_guest;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 12/24] KVM: arm64: Add a range to __pkvm_guest_unshare_host()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (10 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 11/24] KVM: arm64: Add a range to __pkvm_guest_share_host() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 11:51 ` [PATCH v6 13/24] KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest() Vincent Donnefort
` (12 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for supporting stage-2 huge mappings for protected VMs,
add a nr_pages argument to __pkvm_guest_unshare_host(). This range
supports only two values: 1 or PMD_SIZE / PAGE_SIZE (that is 512 on a
4K-pages system).
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 2 +-
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 22 +++++++++++--------
arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +-
3 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index 6741c9ecb879..3fb55899be6b 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -33,7 +33,7 @@ enum pkvm_component_id {
int __pkvm_prot_finalize(void);
int __pkvm_host_share_hyp(u64 pfn, u64 nr_pages);
int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn, u64 nr_pages);
-int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn);
+int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn, u64 nr_pages);
int __pkvm_host_unshare_hyp(u64 pfn, u64 nr_pages);
int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages);
int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages);
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 4aa00aa09526..d9b67b4ca581 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -1072,31 +1072,35 @@ int __pkvm_guest_share_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn, u64 nr_pages)
return ret;
}
-int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn)
+int __pkvm_guest_unshare_host(struct pkvm_hyp_vcpu *vcpu, u64 gfn, u64 nr_pages)
{
struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
- u64 meta, phys, ipa = hyp_pfn_to_phys(gfn);
+ u64 size, meta, phys, ipa = hyp_pfn_to_phys(gfn);
kvm_pte_t pte;
int ret;
+ ret = __guest_check_transition_size(0, ipa, nr_pages, &size);
+ if (ret)
+ return ret;
+
host_lock_component();
guest_lock_component(vm);
- ret = get_valid_guest_pte(vm, ipa, PAGE_SIZE, &pte, &phys);
+ ret = get_valid_guest_pte(vm, ipa, size, &pte, &phys);
if (ret)
goto unlock;
ret = -EPERM;
if (pkvm_getstate(kvm_pgtable_stage2_pte_prot(pte)) != PKVM_PAGE_SHARED_OWNED)
goto unlock;
- if (__host_check_page_state_range(phys, PAGE_SIZE, PKVM_PAGE_SHARED_BORROWED))
+ if (__host_check_page_state_range(phys, size, PKVM_PAGE_SHARED_BORROWED))
goto unlock;
ret = 0;
meta = host_stage2_encode_gfn_meta(vm, gfn);
- WARN_ON(host_stage2_set_owner_metadata_locked(phys, PAGE_SIZE,
+ WARN_ON(host_stage2_set_owner_metadata_locked(phys, size,
PKVM_ID_GUEST, meta));
- WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, PAGE_SIZE, phys,
+ WARN_ON(kvm_pgtable_stage2_map(&vm->pgt, ipa, size, phys,
pkvm_mkstate(KVM_PGTABLE_PROT_RWX, PKVM_PAGE_OWNED),
&vcpu->vcpu.arch.stage2_mc, 0));
unlock:
@@ -1961,8 +1965,8 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.host = PKVM_NOPAGE;
selftest_state.guest[0] = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_guest_unshare_host, vcpu, gfn);
- assert_transition_res(-EPERM, __pkvm_guest_unshare_host, vcpu, gfn);
+ assert_transition_res(0, __pkvm_guest_unshare_host, vcpu, gfn, 1);
+ assert_transition_res(-EPERM, __pkvm_guest_unshare_host, vcpu, gfn, 1);
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
@@ -1979,7 +1983,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
assert_transition_res(-EHWPOISON, __pkvm_guest_share_host, vcpu, gfn, 1);
- assert_transition_res(-EHWPOISON, __pkvm_guest_unshare_host, vcpu, gfn);
+ assert_transition_res(-EHWPOISON, __pkvm_guest_unshare_host, vcpu, gfn, 1);
selftest_state.host = PKVM_NOPAGE;
selftest_state.guest[1] = PKVM_PAGE_OWNED;
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index 66d7493a6549..d92f4da1c180 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -1206,7 +1206,7 @@ static void pkvm_memunshare_call(u64 *ret, struct kvm_vcpu *vcpu)
return;
hyp_vcpu = container_of(vcpu, struct pkvm_hyp_vcpu, vcpu);
- if (!__pkvm_guest_unshare_host(hyp_vcpu, hyp_phys_to_pfn(ipa)))
+ if (!__pkvm_guest_unshare_host(hyp_vcpu, hyp_phys_to_pfn(ipa), 1))
ret[0] = SMCCC_RET_SUCCESS;
}
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 13/24] KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (11 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 12/24] KVM: arm64: Add a range to __pkvm_guest_unshare_host() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 15:37 ` sashiko-bot
2026-12-03 11:51 ` [PATCH v6 14/24] KVM: arm64: Handle huge mappings in __pkvm_vcpu_in_poison_fault() Vincent Donnefort
` (11 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for supporting stage-2 huge mappings for protected VMs,
allow __pkvm_host_force_reclaim_page_guest() to work with PMD_SIZE
mappings. As this HVC is called from a non-preemptible context, it is
not possible to rely on the host pkvm_mappings tree to get the mapping
size. Instead, use the actual host stage-2 mapping size and compare it
with the guest stage-2.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 33 +++++++++++++++++----------
1 file changed, 21 insertions(+), 12 deletions(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index d9b67b4ca581..da5aa3bbca72 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -1340,7 +1340,7 @@ static void hyp_poison_range(phys_addr_t phys, u64 size)
__apply_guest_page(__hyp_va(phys), size, __hyp_poison_page);
}
-static int host_stage2_get_guest_info(phys_addr_t phys, struct pkvm_hyp_vm **vm,
+static int host_stage2_get_guest_info(phys_addr_t phys, u64 *size, struct pkvm_hyp_vm **vm,
u64 *gfn)
{
enum pkvm_page_state state;
@@ -1368,34 +1368,43 @@ static int host_stage2_get_guest_info(phys_addr_t phys, struct pkvm_hyp_vm **vm,
if (ret)
return ret;
- if (WARN_ON(level != KVM_PGTABLE_LAST_LEVEL))
- return -EINVAL;
+ /* We only support either PAGE_SIZE or PMD_SIZE */
+ if (level < KVM_PGTABLE_LAST_LEVEL - 1)
+ return -E2BIG;
- return host_stage2_decode_gfn_meta(pte, vm, gfn);
+ ret = host_stage2_decode_gfn_meta(pte, vm, gfn);
+ if (ret)
+ return ret;
+
+ *size = kvm_granule_size(level);
+
+ return 0;
}
int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
{
+ u64 gfn, ipa, pa, size;
struct pkvm_hyp_vm *vm;
- u64 gfn, ipa, pa;
kvm_pte_t pte;
int ret;
- phys &= PAGE_MASK;
-
hyp_spin_lock(&vm_table_lock);
host_lock_component();
- ret = host_stage2_get_guest_info(phys, &vm, &gfn);
+ ret = host_stage2_get_guest_info(phys, &size, &vm, &gfn);
if (ret)
goto unlock_host;
ipa = hyp_pfn_to_phys(gfn);
+
guest_lock_component(vm);
- ret = get_valid_guest_pte(vm, ipa, PAGE_SIZE, &pte, &pa);
+ ret = get_valid_guest_pte(vm, ipa, size, &pte, &pa);
if (ret)
goto unlock_guest;
+ phys = ALIGN_DOWN(phys, size);
+ pa = ALIGN_DOWN(pa, size);
+
WARN_ON(pa != phys);
if (guest_get_page_state(pte, ipa) != PKVM_PAGE_OWNED) {
ret = -EPERM;
@@ -1403,14 +1412,14 @@ int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
}
/* We really shouldn't be allocating, so don't pass a memcache */
- ret = kvm_pgtable_stage2_annotate(&vm->pgt, ipa, PAGE_SIZE, NULL,
+ ret = kvm_pgtable_stage2_annotate(&vm->pgt, ipa, size, NULL,
KVM_GUEST_INVALID_PTE_TYPE_POISONED,
0);
if (ret)
goto unlock_guest;
- hyp_poison_range(phys, PAGE_SIZE);
- WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST));
+ hyp_poison_range(phys, size);
+ WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HOST));
unlock_guest:
guest_unlock_component(vm);
unlock_host:
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 13/24] KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest()
2026-12-03 11:51 ` [PATCH v6 13/24] KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest() Vincent Donnefort
@ 2026-12-03 15:37 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 15:37 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] kvm: __pkvm_host_force_reclaim_page_guest() holds global
locks over PMD
--
commit e4160fc03dbf17fa5fb410c53a8820d2609da1f1
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest()
This commit extends __pkvm_host_force_reclaim_page_guest() to handle PMD_SIZE
mappings by reading the mapping size from the host stage-2 page table and
validating it against the guest stage-2.
> diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> index d9b67b4ca581f..da5aa3bbca72f 100644
> --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
[ ... ]
> int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
> {
> + u64 gfn, ipa, pa, size;
> struct pkvm_hyp_vm *vm;
> - u64 gfn, ipa, pa;
> kvm_pte_t pte;
> int ret;
>
> - phys &= PAGE_MASK;
> -
> hyp_spin_lock(&vm_table_lock);
> host_lock_component();
>
> - ret = host_stage2_get_guest_info(phys, &vm, &gfn);
> + ret = host_stage2_get_guest_info(phys, &size, &vm, &gfn);
> if (ret)
> goto unlock_host;
>
> ipa = hyp_pfn_to_phys(gfn);
> +
> guest_lock_component(vm);
> - ret = get_valid_guest_pte(vm, ipa, PAGE_SIZE, &pte, &pa);
> + ret = get_valid_guest_pte(vm, ipa, size, &pte, &pa);
> if (ret)
> goto unlock_guest;
>
> + phys = ALIGN_DOWN(phys, size);
> + pa = ALIGN_DOWN(pa, size);
> +
> WARN_ON(pa != phys);
> if (guest_get_page_state(pte, ipa) != PKVM_PAGE_OWNED) {
> ret = -EPERM;
> @@ -1403,14 +1412,14 @@ int __pkvm_host_force_reclaim_page_guest(phys_addr_t phys)
> }
>
> /* We really shouldn't be allocating, so don't pass a memcache */
> - ret = kvm_pgtable_stage2_annotate(&vm->pgt, ipa, PAGE_SIZE, NULL,
> + ret = kvm_pgtable_stage2_annotate(&vm->pgt, ipa, size, NULL,
> KVM_GUEST_INVALID_PTE_TYPE_POISONED,
> 0);
> if (ret)
> goto unlock_guest;
>
> - hyp_poison_range(phys, PAGE_SIZE);
> - WARN_ON(host_stage2_set_owner_locked(phys, PAGE_SIZE, PKVM_ID_HOST));
> + hyp_poison_range(phys, size);
> + WARN_ON(host_stage2_set_owner_locked(phys, size, PKVM_ID_HOST));
> unlock_guest:
> guest_unlock_component(vm);
> unlock_host:
[Severity: Medium]
Can holding vm_table_lock, host_mmu.lock, and vm->lock across
hyp_poison_range() for a PMD_SIZE mapping cause long EL2 stalls across the
host and unrelated VMs?
hyp_poison_range() zeroes up to 2 MiB (4K), 32 MiB (16K), or 512 MiB (64K)
and flushes dcache to PoC with interrupts masked at EL2. Holding
vm_table_lock blocks pkvm_load_hyp_vcpu() and pkvm_put_hyp_vcpu() across all
VMs, while host_mmu.lock blocks host stage-2 faults across the host.
On 64K kernels, HAS_FIXBLOCK is undefined in arch/arm64/kvm/hyp/nvhe/mm.c,
so hyp_fixblock_map() falls back to PAGE_SIZE. __apply_guest_page() then
iterates 8192 times per 512 MiB PMD, issuing 8192 broadcast vale2is TLBIs
under these locks.
A VMM running a THP-backed protected VM can reach this path through a GUP
syscall that accesses a donated page via the kernel linear mapping
(is_spurious_el1_translation_fault() -> pkvm_force_reclaim_guest_page()).
Once kvm_pgtable_stage2_annotate() completes, neither vm nor vm_table is
used again, so guest_unlock_component(vm) and hyp_spin_unlock(&vm_table_lock)
could be released before hyp_poison_range(), leaving only host_mmu.lock held
until host_stage2_set_owner_locked() finishes.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=13
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 14/24] KVM: arm64: Handle huge mappings in __pkvm_vcpu_in_poison_fault()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (12 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 13/24] KVM: arm64: Handle huge mappings in __pkvm_host_force_reclaim_page_guest() Vincent Donnefort
@ 2026-12-03 11:51 ` Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 15/24] KVM: arm64: Add a range to pKVM ownership selftest Vincent Donnefort
` (10 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:51 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for supporting stage-2 huge mappings for protected VMs,
allow __pkvm_vcpu_in_poison_fault() to work with PMD_SIZE mappings.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index da5aa3bbca72..f68384dc822a 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -994,7 +994,7 @@ int __pkvm_vcpu_in_poison_fault(struct pkvm_hyp_vcpu *hyp_vcpu)
if (ret)
goto unlock;
- if (level != KVM_PGTABLE_LAST_LEVEL) {
+ if (level < KVM_PGTABLE_LAST_LEVEL - 1) {
ret = -EINVAL;
goto unlock;
}
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 15/24] KVM: arm64: Add a range to pKVM ownership selftest
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (13 preceding siblings ...)
2026-12-03 11:51 ` [PATCH v6 14/24] KVM: arm64: Handle huge mappings in __pkvm_vcpu_in_poison_fault() Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 16:17 ` sashiko-bot
2026-12-03 11:52 ` [PATCH v6 16/24] KVM: arm64: Warn on pKVM guest stage-2 block collapse Vincent Donnefort
` (9 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
Extend pkvm_ownership_selftest() with a nr_pages argument so that state
transitions can be tested with both page-granular (1) and block-granular
(PMD_SIZE / PAGE_SIZE) ranges.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/include/asm/kvm_hcall.h | 2 +-
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 9 +-
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 4 +-
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 214 ++++++++++--------
arch/arm64/kvm/pkvm.c | 42 ++--
5 files changed, 153 insertions(+), 118 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_hcall.h b/arch/arm64/include/asm/kvm_hcall.h
index 201d58907109..e54a4631a378 100644
--- a/arch/arm64/include/asm/kvm_hcall.h
+++ b/arch/arm64/include/asm/kvm_hcall.h
@@ -157,7 +157,7 @@ DECLARE_KVM_HOST_HCALL0(void, __vgic_v3_init_lrs)
DECLARE_KVM_HOST_HCALL0(u64, __vgic_v3_get_gic_config)
DECLARE_KVM_HOST_HCALL0(int, __pkvm_hyp_alloc_selftest)
DECLARE_KVM_HOST_HCALL(int, __pkvm_ownership_selftest,
- u64, pfn, u64, pgtable_pfn, unsigned long, pgtable_nr_pages)
+ u64, pfn, u64, nr_pages, u64, pgtable_pfn, unsigned long, pgtable_nr_pages)
DECLARE_KVM_HOST_HCALL0(int, __pkvm_prot_finalize)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index 3fb55899be6b..a991be3d4c9a 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -76,9 +76,14 @@ static __always_inline void __load_host_stage2(void)
}
#ifdef CONFIG_NVHE_EL2_DEBUG
-int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr_pages);
+int __pkvm_ownership_selftest(u64 pfn, u64 nr_pages, u64 pgtable_pfn,
+ unsigned long pgtable_nr_pages);
#else
static inline int
-__pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr_pages) { return 0; }
+__pkvm_ownership_selftest(u64 pfn, u64 nr_pages, u64 pgtable_pfn,
+ unsigned long pgtable_nr_pages)
+{
+ return 0;
+}
#endif
#endif /* __KVM_NVHE_MEM_PROTECT__ */
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 2621a4ef7e1e..c322ef4432c9 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -1314,9 +1314,9 @@ DEFINE_KVM_HOST_HCALL(ulong, __pkvm_create_private_mapping,
}
DEFINE_KVM_HOST_HCALL(int, __pkvm_ownership_selftest,
- u64, pfn, u64, pgtable_pfn, unsigned long, pgtable_nr_pages)
+ u64, pfn, u64, nr_pages, u64, pgtable_pfn, unsigned long, pgtable_nr_pages)
{
- return __pkvm_ownership_selftest(pfn, pgtable_pfn, pgtable_nr_pages);
+ return __pkvm_ownership_selftest(pfn, nr_pages, pgtable_pfn, pgtable_nr_pages);
}
DEFINE_KVM_HOST_HCALL0(int, __pkvm_prot_finalize)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index f68384dc822a..a22d3cef7df0 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -1787,14 +1787,19 @@ static u64 selftest_ipa(void)
return BIT(selftest_vcpu->vcpu.arch.hw_mmu->pgt->ia_bits - 1);
}
+static unsigned long selftest_nr_pages;
+
static void assert_page_state(void)
{
void *virt = hyp_page_to_virt(selftest_page);
- u64 size = PAGE_SIZE << selftest_page->order;
struct pkvm_hyp_vcpu *vcpu = selftest_vcpu;
+ u64 size = selftest_nr_pages * PAGE_SIZE;
u64 phys = hyp_virt_to_phys(virt);
- u64 ipa[2] = { selftest_ipa(), selftest_ipa() + PAGE_SIZE };
struct pkvm_hyp_vm *vm;
+ u64 ipa[2] = {
+ selftest_ipa(),
+ selftest_ipa() + size
+ };
vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
@@ -1818,7 +1823,8 @@ static void assert_page_state(void)
assert_page_state(); \
} while (0)
-int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr_pages)
+int __pkvm_ownership_selftest(u64 pfn, u64 nr_pages, u64 pgtable_pfn,
+ unsigned long pgtable_nr_pages)
{
enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_RWX;
struct pkvm_hyp_vcpu *vcpu;
@@ -1831,7 +1837,7 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
if (ret)
return ret;
- ret = __pkvm_host_donate_hyp(pfn, 1);
+ ret = __pkvm_host_donate_hyp(pfn, nr_pages);
if (ret) {
WARN_ON(__pkvm_hyp_donate_host(pgtable_pfn, pgtable_nr_pages));
return ret;
@@ -1841,10 +1847,11 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
pgtable_nr_pages);
vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
+ selftest_nr_pages = nr_pages;
phys = hyp_pfn_to_phys(pfn);
virt = hyp_phys_to_virt(phys);
gfn = hyp_phys_to_pfn(selftest_ipa());
- size = PAGE_SIZE;
+ size = nr_pages * PAGE_SIZE;
selftest_page = hyp_virt_to_page(virt);
selftest_page->refcount = 0;
@@ -1853,48 +1860,48 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.hyp = PKVM_PAGE_OWNED;
selftest_state.guest[0] = selftest_state.guest[1] = PKVM_NOPAGE;
assert_page_state();
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, nr_pages);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, nr_pages, vm);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.hyp = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1);
- assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
+ assert_transition_res(0, __pkvm_hyp_donate_host, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, nr_pages);
+ assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, nr_pages, vm);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
selftest_state.host = PKVM_PAGE_SHARED_OWNED;
selftest_state.hyp = PKVM_PAGE_SHARED_BORROWED;
- assert_transition_res(0, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(0, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, nr_pages, vm);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size);
assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size);
hyp_unpin_shared_mem(virt, virt + size);
WARN_ON(hyp_page_count(virt) != 1);
- assert_transition_res(-EBUSY, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(-EBUSY, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, nr_pages, vm);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
hyp_unpin_shared_mem(virt, virt + size);
assert_page_state();
@@ -1902,116 +1909,125 @@ int __pkvm_ownership_selftest(u64 pfn, u64 pgtable_pfn, unsigned long pgtable_nr
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.hyp = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_host_unshare_hyp, pfn, 1);
+ assert_transition_res(0, __pkvm_host_unshare_hyp, pfn, nr_pages);
selftest_state.host = PKVM_PAGE_SHARED_OWNED;
selftest_state.hyp = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(0, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, nr_pages, vm);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.hyp = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_host_unshare_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1);
+ assert_transition_res(0, __pkvm_host_unshare_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, nr_pages);
selftest_state.host = PKVM_PAGE_SHARED_OWNED;
selftest_state.guest[0] = PKVM_PAGE_SHARED_BORROWED;
- assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
+ assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size);
selftest_state.guest[1] = PKVM_PAGE_SHARED_BORROWED;
- assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
+ assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu, prot);
WARN_ON(hyp_virt_to_page(virt)->host_share_guest_count != 2);
selftest_state.guest[0] = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_host_unshare_guest, gfn, 1, vm);
+ assert_transition_res(0, __pkvm_host_unshare_guest, gfn, nr_pages, vm);
selftest_state.guest[1] = PKVM_NOPAGE;
selftest_state.host = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_host_unshare_guest, gfn + 1, 1, vm);
+ assert_transition_res(0, __pkvm_host_unshare_guest, gfn + nr_pages, nr_pages, vm);
selftest_state.host = PKVM_NOPAGE;
selftest_state.guest[0] = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
+ assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
selftest_state.host = PKVM_PAGE_SHARED_BORROWED;
selftest_state.guest[0] = PKVM_PAGE_SHARED_OWNED;
- assert_transition_res(0, __pkvm_guest_share_host, vcpu, gfn, 1);
- assert_transition_res(-EPERM, __pkvm_guest_share_host, vcpu, gfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
+ assert_transition_res(0, __pkvm_guest_share_host, vcpu, gfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_guest_share_host, vcpu, gfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
selftest_state.host = PKVM_NOPAGE;
selftest_state.guest[0] = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_guest_unshare_host, vcpu, gfn, 1);
- assert_transition_res(-EPERM, __pkvm_guest_unshare_host, vcpu, gfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + 1, 1, vcpu, prot);
- assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, 1);
- assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1);
+ assert_transition_res(0, __pkvm_guest_unshare_host, vcpu, gfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_guest_unshare_host, vcpu, gfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu, prot);
+ assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn, nr_pages);
+ assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, nr_pages);
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.guest[0] = PKVM_POISON;
assert_transition_res(0, __pkvm_host_force_reclaim_page_guest, phys);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
- assert_transition_res(-EHWPOISON, __pkvm_guest_share_host, vcpu, gfn, 1);
- assert_transition_res(-EHWPOISON, __pkvm_guest_unshare_host, vcpu, gfn, 1);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ assert_transition_res(-EHWPOISON, __pkvm_guest_share_host, vcpu, gfn, nr_pages);
+ assert_transition_res(-EHWPOISON, __pkvm_guest_unshare_host, vcpu, gfn, nr_pages);
selftest_state.host = PKVM_NOPAGE;
selftest_state.guest[1] = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn + 1, 1, vcpu);
+ assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn + nr_pages, nr_pages,
+ vcpu);
selftest_state.host = PKVM_PAGE_OWNED;
selftest_state.guest[1] = PKVM_NOPAGE;
- assert_transition_res(0, __pkvm_host_reclaim_guest, gfn + 1, 1, vm);
- assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, 1, vcpu);
- assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, 1, vcpu, prot);
+ assert_transition_res(0, __pkvm_host_reclaim_guest, gfn + nr_pages, nr_pages,
+ vm);
+ assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
selftest_state.host = PKVM_NOPAGE;
selftest_state.hyp = PKVM_PAGE_OWNED;
- assert_transition_res(0, __pkvm_host_donate_hyp, pfn, 1);
+ assert_transition_res(0, __pkvm_host_donate_hyp, pfn, nr_pages);
/* This also gives back the ownership to the host */
teardown_selftest_vm();
- WARN_ON(__pkvm_hyp_donate_host(pfn, 1));
+ WARN_ON(__pkvm_hyp_donate_host(pfn, nr_pages));
return 0;
}
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 4c80e0960154..7a6d4ba38a2b 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -333,31 +333,45 @@ static int __init pkvm_drop_host_privileges(void)
}
#ifdef CONFIG_NVHE_EL2_DEBUG
+static void __init __pkvm_ownership_selftest(unsigned long page_order,
+ unsigned long pgtable,
+ unsigned long pgtable_order)
+{
+ unsigned long nr_pages = 1UL << page_order;
+ unsigned long page;
+ int ret;
+
+ /* Page whose ownership will be tested */
+ page = __get_free_pages(GFP_KERNEL | __GFP_NOWARN, page_order);
+ if (!page) {
+ kvm_info("pKVM ownership selftest (order=%lu) skipped: failed to allocate memory\n",
+ page_order);
+ return;
+ }
+
+ ret = kvm_call_hyp_nvhe(__pkvm_ownership_selftest,
+ virt_to_pfn((void *)page), nr_pages,
+ virt_to_pfn((void *)pgtable), 1 << pgtable_order);
+ if (ret)
+ kvm_err("pKVM ownership selftest (order=%lu) failed (%d)\n", page_order, ret);
+
+ free_pages(page, page_order);
+}
+
static void __init pkvm_ownership_selftest(void)
{
unsigned long pgtable_order = 5;
unsigned long pgtable = 0;
- unsigned long page = 0;
- long ret = -ENOMEM;
/* Memory for the VM page-table */
pgtable = __get_free_pages(GFP_KERNEL, pgtable_order);
if (!pgtable)
- goto out;
+ return;
- /* Page whose ownership will be tested */
- page = __get_free_page(GFP_KERNEL);
- if (!page)
- goto out;
-
- ret = kvm_call_hyp_nvhe(__pkvm_ownership_selftest, virt_to_pfn((void *)page),
- virt_to_pfn((void *)pgtable), 1 << pgtable_order);
-out:
- if (ret)
- kvm_err("pKVM ownership selftest failed (%ld)\n", ret);
+ __pkvm_ownership_selftest(0, pgtable, pgtable_order);
+ __pkvm_ownership_selftest(PMD_ORDER, pgtable, pgtable_order);
free_pages(pgtable, pgtable_order);
- free_page(page);
}
static void __init pkvm_hyp_alloc_selftest(void)
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 15/24] KVM: arm64: Add a range to pKVM ownership selftest
2026-12-03 11:52 ` [PATCH v6 15/24] KVM: arm64: Add a range to pKVM ownership selftest Vincent Donnefort
@ 2026-12-03 16:17 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 16:17 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: arm64: PMD-sized pkvm_ownership_selftest() stalls EL2 on
64K pages
--
commit 2e29e0870ec6148393389f7db103e0a41251adef
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Add a range to pKVM ownership selftest
This commit extends __pkvm_ownership_selftest() with a nr_pages argument
and runs the boot-time selftest for both order-0 and PMD_ORDER ranges.
> diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
> index 4c80e0960154b..7a6d4ba38a2be 100644
> --- a/arch/arm64/kvm/pkvm.c
> +++ b/arch/arm64/kvm/pkvm.c
> @@ -333,31 +333,45 @@ static int __init pkvm_drop_host_privileges(void)
[ ... ]
> static void __init pkvm_ownership_selftest(void)
> {
> unsigned long pgtable_order = 5;
> unsigned long pgtable = 0;
> - unsigned long page = 0;
> - long ret = -ENOMEM;
>
> /* Memory for the VM page-table */
> pgtable = __get_free_pages(GFP_KERNEL, pgtable_order);
> if (!pgtable)
> - goto out;
> -
> - /* Page whose ownership will be tested */
> - page = __get_free_page(GFP_KERNEL);
> - if (!page)
> - goto out;
> + return;
>
> - ret = kvm_call_hyp_nvhe(__pkvm_ownership_selftest, virt_to_pfn((void *)page),
> - virt_to_pfn((void *)pgtable), 1 << pgtable_order);
> -out:
> - if (ret)
> - kvm_err("pKVM ownership selftest failed (%ld)\n", ret);
> + __pkvm_ownership_selftest(0, pgtable, pgtable_order);
> + __pkvm_ownership_selftest(PMD_ORDER, pgtable, pgtable_order);
[Severity: Low]
Can running the PMD_ORDER selftest inside a single EL2 hypercall stall the
boot CPU on 64K-page kernels?
On CONFIG_ARM64_64K_PAGES, PMD_ORDER is 13 (8,192 pages, 512 MiB) and
pkvm_ownership_selftest() runs from device_initcall_sync after SMP, RCU
stall detection, and lockup detectors are active. All transitions execute
with interrupts masked and preemption disabled.
Because HAS_FIXBLOCK is disabled for 64K pages, hyp_fixblock_map() falls
back to PAGE_SIZE fixmap slots. The two hyp_poison_range() passes in
__pkvm_host_force_reclaim_page_guest() and __pkvm_host_reclaim_guest()
zero and clean 1 GiB to PoC with 16,384 broadcast vale2is TLBIs.
In addition, pkvm_create_mappings_locked() maps PAGE_SIZE at a time,
so the three hyp unmap cycles in __pkvm_ownership_selftest() issue another
24,576 broadcast vale2is TLBIs in hyp_unmap_walker().
On emulated platforms or systems without FEAT_S2FWB/FEAT_DIC, this can
trigger boot-time softlockup or RCU stall warnings. Follow-up commit
"KVM: arm64: Extend pKVM page ownership selftests to cover guest block
split" adds two more full-PMD reclaim passes inside the same hypercall.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=15
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 16/24] KVM: arm64: Warn on pKVM guest stage-2 block collapse
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (14 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 15/24] KVM: arm64: Add a range to pKVM ownership selftest Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 17/24] KVM: arm64: Add vcpu conduit for pkvm_hyp_req Vincent Donnefort
` (8 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
When pKVM is enabled, the host relies on a pkvm_mapping tree that
mirrors the actual hypervisor-owned stage-2. The hypervisor is not
allowed to silently split blocks or collapse page-tables. This should be
treated as a bug.
Therefore, WARN on guest stage-2 page-table collapse which would
otherwise be fatal anyway.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index a22d3cef7df0..90651bc5efe5 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -226,6 +226,12 @@ static void guest_s2_put_page(void *addr)
hyp_put_page(¤t_vm->pool, addr);
}
+static void guest_s2_free_unlinked_table(void *addr, s8 level)
+{
+ /* We should never collapse guest stage-2 tables into blocks */
+ WARN_ON(1);
+}
+
static void __apply_guest_page(void *va, size_t size,
void (*func)(void *addr, size_t size))
{
@@ -294,6 +300,7 @@ int kvm_guest_prepare_stage2(struct pkvm_hyp_vm *vm, void *pgd)
.put_page = guest_s2_put_page,
.dcache_clean_inval_poc = clean_dcache_guest_page,
.icache_inval_pou = invalidate_icache_guest_page,
+ .free_unlinked_table = guest_s2_free_unlinked_table,
};
guest_lock_component(vm);
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 17/24] KVM: arm64: Add vcpu conduit for pkvm_hyp_req
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (15 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 16/24] KVM: arm64: Warn on pKVM guest stage-2 block collapse Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 17:07 ` sashiko-bot
2026-12-03 11:52 ` [PATCH v6 18/24] KVM: arm64: Factor out host_stage2_encode_annotation() Vincent Donnefort
` (7 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
The pKVM hypervisor depends on the host for a lot of operations like
memory allocation. It can raise a request by populating a pkvm_hyp_req
on the extra registers when returning a SMCCC call.
Add a new conduit, using a per-vCPU pkvm_hyp_req, populated on
ARM_EXCEPTION_PKVM_HYP_REQ. This allows processing the request later, in
a less restrictive context than the one from where the __kvm_vcpu_run
HVC is called, just like the other exceptions are handled.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/include/asm/kvm_asm.h | 10 ++++++----
arch/arm64/include/asm/kvm_host.h | 26 ++++++++++++++++++++++++++
arch/arm64/include/asm/kvm_pkvm.h | 24 +-----------------------
arch/arm64/kvm/handle_exit.c | 3 +++
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 2 ++
arch/arm64/kvm/pkvm.c | 9 +++++++--
6 files changed, 45 insertions(+), 29 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
index 3efc63e1a823..872185f9ac53 100644
--- a/arch/arm64/include/asm/kvm_asm.h
+++ b/arch/arm64/include/asm/kvm_asm.h
@@ -17,10 +17,11 @@
#define ARM_EXCEPTION_IS_TRAP(x) (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP)
#define ARM_SERROR_PENDING(x) !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT))
-#define ARM_EXCEPTION_IRQ 0
-#define ARM_EXCEPTION_EL1_SERROR 1
-#define ARM_EXCEPTION_TRAP 2
-#define ARM_EXCEPTION_IL 3
+#define ARM_EXCEPTION_IRQ 0
+#define ARM_EXCEPTION_EL1_SERROR 1
+#define ARM_EXCEPTION_TRAP 2
+#define ARM_EXCEPTION_IL 3
+#define ARM_EXCEPTION_PKVM_HYP_REQ 4
/* The hyp-stub will return this for any kvm_call_hyp() call */
#define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR
@@ -28,6 +29,7 @@
{ARM_EXCEPTION_IRQ, "IRQ" }, \
{ARM_EXCEPTION_EL1_SERROR, "SERROR" }, \
{ARM_EXCEPTION_TRAP, "TRAP" }, \
+ {ARM_EXCEPTION_PKVM_HYP_REQ, "PKVM_HYP_REQ" }, \
{ARM_EXCEPTION_HYP_GONE, "HYP_GONE" }
/*
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 97332ffcb130..0161bcb8123b 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -138,6 +138,29 @@ u32 __attribute_const__ kvm_target_cpu(void);
void kvm_reset_vcpu(struct kvm_vcpu *vcpu);
void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu);
+enum pkvm_hyp_req_type {
+ PKVM_HYP_NO_REQ = 0,
+ PKVM_HYP_REQ_HYP_ALLOC,
+ PKVM_HYP_REQ_HYP_ALLOC_SELFTEST,
+ __PKVM_HYP_REQ_TYPE_MAX,
+};
+
+#define PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX \
+ (sizeof(struct arm_smccc_res) - offsetof(struct arm_smccc_res, a2) - 1)
+
+struct pkvm_hyp_req {
+ u8 type;
+ union {
+ struct {
+ u32 nr_pages;
+ } mem;
+ struct {
+ /* Helper for SMCCC encoding/decoding */
+ u8 args[PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX];
+ } args;
+ };
+};
+
struct kvm_hyp_memcache {
phys_addr_t head;
unsigned long nr_pages;
@@ -1006,6 +1029,9 @@ struct kvm_vcpu_arch {
/* Pages to top-up the pKVM/EL2 guest stage-2 pool */
struct kvm_hyp_memcache stage2_mc;
+ /* To be read on ARM_EXCEPTION_PKVM_HYP_REQ */
+ struct pkvm_hyp_req pkvm_hyp_req;
+
/* Virtual SError ESR to restore when HCR_EL2.VSE is set */
u64 vsesr_el2;
diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm_pkvm.h
index b75dc3d03a98..3e51337f2d9e 100644
--- a/arch/arm64/include/asm/kvm_pkvm.h
+++ b/arch/arm64/include/asm/kvm_pkvm.h
@@ -22,6 +22,7 @@ int pkvm_create_hyp_vm(struct kvm *kvm);
bool pkvm_hyp_vm_is_created(struct kvm *kvm);
void pkvm_destroy_hyp_vm(struct kvm *kvm);
int pkvm_create_hyp_vcpu(struct kvm_vcpu *vcpu);
+int __pkvm_handle_vcpu_req(struct kvm_vcpu *vcpu);
/*
* Check whether the specific capability is allowed in pKVM.
@@ -231,29 +232,6 @@ struct pkvm_mapping {
u64 __subtree_last; /* Internal member for interval tree */
};
-enum pkvm_hyp_req_type {
- PKVM_HYP_NO_REQ = 0,
- PKVM_HYP_REQ_HYP_ALLOC,
- PKVM_HYP_REQ_HYP_ALLOC_SELFTEST,
- __PKVM_HYP_REQ_TYPE_MAX,
-};
-
-#define PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX \
- (sizeof(struct arm_smccc_res) - offsetof(struct arm_smccc_res, a2) - 1)
-
-struct pkvm_hyp_req {
- u8 type;
- union {
- struct {
- u32 nr_pages;
- } mem;
- struct {
- /* Helper for SMCCC encoding/decoding */
- u8 args[PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX];
- } args;
- };
-};
-
static inline size_t pkvm_hyp_req_arg_size(u8 type)
{
struct pkvm_hyp_req *req;
diff --git a/arch/arm64/kvm/handle_exit.c b/arch/arm64/kvm/handle_exit.c
index 9931192735d4..2030675304ca 100644
--- a/arch/arm64/kvm/handle_exit.c
+++ b/arch/arm64/kvm/handle_exit.c
@@ -18,6 +18,7 @@
#include <asm/kvm_emulate.h>
#include <asm/kvm_mmu.h>
#include <asm/kvm_nested.h>
+#include <asm/kvm_pkvm.h>
#include <asm/debug-monitors.h>
#include <asm/stacktrace/nvhe.h>
#include <asm/traps.h>
@@ -464,6 +465,8 @@ int handle_exit(struct kvm_vcpu *vcpu, int exception_index)
return 1;
case ARM_EXCEPTION_TRAP:
return handle_trap_exceptions(vcpu);
+ case ARM_EXCEPTION_PKVM_HYP_REQ:
+ return __pkvm_handle_vcpu_req(vcpu);
case ARM_EXCEPTION_HYP_GONE:
/*
* EL2 has been reset to the hyp-stub. This happens when a guest
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index c322ef4432c9..41817f45115d 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -734,6 +734,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
case ARM_EXCEPTION_IRQ:
case ARM_EXCEPTION_EL1_SERROR:
case ARM_EXCEPTION_IL:
+ case ARM_EXCEPTION_PKVM_HYP_REQ:
break;
case ARM_EXCEPTION_TRAP:
/* Nothing was marshalled for this trap, see sync_hyp_vcpu(). */
@@ -803,6 +804,7 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason)
break;
case ARM_EXCEPTION_EL1_SERROR:
case ARM_EXCEPTION_IL:
+ case ARM_EXCEPTION_PKVM_HYP_REQ:
break;
default:
BUG();
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 7a6d4ba38a2b..7ae4aa0cb81c 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -770,7 +770,7 @@ bool pkvm_force_reclaim_guest_page(phys_addr_t phys)
return !ret || ret == -EAGAIN;
}
-static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req)
+static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req, struct kvm_vcpu *vcpu)
{
int ret = -EINVAL;
@@ -793,7 +793,12 @@ int __pkvm_handle_smccc_req(struct arm_smccc_res *res)
struct pkvm_hyp_req req;
if (smccc_to_pkvm_hyp_req(&req, res))
- return pkvm_handle_hyp_req(&req);
+ return pkvm_handle_hyp_req(&req, NULL);
return res->a1;
}
+
+int __pkvm_handle_vcpu_req(struct kvm_vcpu *vcpu)
+{
+ return pkvm_handle_hyp_req(&vcpu->arch.pkvm_hyp_req, vcpu) ?: 1;
+}
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 17/24] KVM: arm64: Add vcpu conduit for pkvm_hyp_req
2026-12-03 11:52 ` [PATCH v6 17/24] KVM: arm64: Add vcpu conduit for pkvm_hyp_req Vincent Donnefort
@ 2026-12-03 17:07 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 17:07 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] KVM: arm64: perf arm64_exception_types.h missing
ARM_EXCEPTION_PKVM_HYP_REQ
--
commit 430e724675738054bd2e1e50f9941d5eee114a81
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Add vcpu conduit for pkvm_hyp_req
This commit adds a per-vCPU pkvm_hyp_req conduit triggered via a new
ARM_EXCEPTION_PKVM_HYP_REQ exit code so pKVM hypervisor requests can be
handled in the normal vCPU exit path.
> diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
> index 3efc63e1a8236..872185f9ac53b 100644
> --- a/arch/arm64/include/asm/kvm_asm.h
> +++ b/arch/arm64/include/asm/kvm_asm.h
> @@ -17,10 +17,11 @@
> #define ARM_EXCEPTION_IS_TRAP(x) (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP)
> #define ARM_SERROR_PENDING(x) !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT))
>
> -#define ARM_EXCEPTION_IRQ 0
> -#define ARM_EXCEPTION_EL1_SERROR 1
> -#define ARM_EXCEPTION_TRAP 2
> -#define ARM_EXCEPTION_IL 3
> +#define ARM_EXCEPTION_IRQ 0
> +#define ARM_EXCEPTION_EL1_SERROR 1
> +#define ARM_EXCEPTION_TRAP 2
> +#define ARM_EXCEPTION_IL 3
> +#define ARM_EXCEPTION_PKVM_HYP_REQ 4
> /* The hyp-stub will return this for any kvm_call_hyp() call */
> #define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR
>
> @@ -28,6 +29,7 @@
> {ARM_EXCEPTION_IRQ, "IRQ" }, \
> {ARM_EXCEPTION_EL1_SERROR, "SERROR" }, \
> {ARM_EXCEPTION_TRAP, "TRAP" }, \
> + {ARM_EXCEPTION_PKVM_HYP_REQ, "PKVM_HYP_REQ" }, \
> {ARM_EXCEPTION_HYP_GONE, "HYP_GONE" }
[Severity: Low]
Should tools/perf/util/kvm-stat-arch/arm64_exception_types.h also be
updated with ARM_EXCEPTION_PKVM_HYP_REQ?
That header mirrors the ARM_EXCEPTION_* definitions and
kvm_arm_exception_type for perf kvm stat on arm64. Without the new
entry, get_exit_reason() in tools/perf/util/kvm-stat.c logs an unknown
exit code error and aggregates ARM_EXCEPTION_PKVM_HYP_REQ exits under
"UNKNOWN" when decoding kvm:kvm_exit events.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=17
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 18/24] KVM: arm64: Factor out host_stage2_encode_annotation()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (16 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 17/24] KVM: arm64: Add vcpu conduit for pkvm_hyp_req Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 19/24] KVM: arm64: Return VM handle from host_stage2_decode_gfn_meta() Vincent Donnefort
` (6 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for allowing the split of annotated host stage-2 blocks,
add host_stage2_encode_annotation().
No functional change intended.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 90651bc5efe5..cc1579fac6b7 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -589,6 +589,13 @@ static void __host_update_page_state(phys_addr_t addr, u64 size, enum pkvm_page_
#define KVM_HOST_DONATION_PTE_OWNER_MASK GENMASK(3, 1)
#define KVM_HOST_DONATION_PTE_EXTRA_MASK GENMASK(59, 4)
+
+static kvm_pte_t host_stage2_encode_annotation(u8 owner_id, u64 meta)
+{
+ return FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, owner_id) |
+ FIELD_PREP(KVM_HOST_DONATION_PTE_EXTRA_MASK, meta);
+}
+
static int host_stage2_set_owner_metadata_locked(phys_addr_t addr, u64 size,
u8 owner_id, u64 meta)
{
@@ -607,8 +614,7 @@ static int host_stage2_set_owner_metadata_locked(phys_addr_t addr, u64 size,
if (!FIELD_FIT(KVM_HOST_DONATION_PTE_EXTRA_MASK, meta))
return -EINVAL;
- annotation = FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, owner_id) |
- FIELD_PREP(KVM_HOST_DONATION_PTE_EXTRA_MASK, meta);
+ annotation = host_stage2_encode_annotation(owner_id, meta);
ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt,
addr, size, &host_s2_pool,
KVM_HOST_INVALID_PTE_TYPE_DONATION, annotation);
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 19/24] KVM: arm64: Return VM handle from host_stage2_decode_gfn_meta()
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (17 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 18/24] KVM: arm64: Factor out host_stage2_encode_annotation() Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 20/24] KVM: arm64: Add __pkvm_host_split_guest HVC Vincent Donnefort
` (5 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
In preparation for allowing the split of annotated host stage-2 blocks,
move the VM handle lookup outside of host_stage2_decode_gfn_meta() and
instead, return the raw pkvm_handle_t.
No functional change intended.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 21 ++++++++++-----------
1 file changed, 10 insertions(+), 11 deletions(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index cc1579fac6b7..d94e1d94d995 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -678,10 +678,8 @@ static u64 host_stage2_encode_gfn_meta(struct pkvm_hyp_vm *vm, u64 gfn)
FIELD_PREP(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, gfn);
}
-static int host_stage2_decode_gfn_meta(kvm_pte_t pte, struct pkvm_hyp_vm **vm,
- u64 *gfn)
+static int host_stage2_decode_gfn_meta(kvm_pte_t pte, pkvm_handle_t *handle, u64 *gfn)
{
- pkvm_handle_t handle;
u64 meta;
if (WARN_ON(kvm_pte_valid(pte)))
@@ -696,13 +694,7 @@ static int host_stage2_decode_gfn_meta(kvm_pte_t pte, struct pkvm_hyp_vm **vm,
return -EPERM;
meta = FIELD_GET(KVM_HOST_DONATION_PTE_EXTRA_MASK, pte);
- handle = FIELD_GET(KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK, meta);
- *vm = get_vm_by_handle(handle);
- if (!*vm) {
- /* We probably raced with teardown; try again */
- return -EAGAIN;
- }
-
+ *handle = FIELD_GET(KVM_HOST_PTE_OWNER_GUEST_HANDLE_MASK, meta);
*gfn = FIELD_GET(KVM_HOST_PTE_OWNER_GUEST_GFN_MASK, meta);
return 0;
}
@@ -1357,6 +1349,7 @@ static int host_stage2_get_guest_info(phys_addr_t phys, u64 *size, struct pkvm_h
u64 *gfn)
{
enum pkvm_page_state state;
+ pkvm_handle_t handle;
kvm_pte_t pte;
s8 level;
int ret;
@@ -1385,10 +1378,16 @@ static int host_stage2_get_guest_info(phys_addr_t phys, u64 *size, struct pkvm_h
if (level < KVM_PGTABLE_LAST_LEVEL - 1)
return -E2BIG;
- ret = host_stage2_decode_gfn_meta(pte, vm, gfn);
+ ret = host_stage2_decode_gfn_meta(pte, &handle, gfn);
if (ret)
return ret;
+ *vm = get_vm_by_handle(handle);
+ if (!*vm) {
+ /* We probably raced with teardown; try again */
+ return -EAGAIN;
+ }
+
*size = kvm_granule_size(level);
return 0;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 20/24] KVM: arm64: Add __pkvm_host_split_guest HVC
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (18 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 19/24] KVM: arm64: Return VM handle from host_stage2_decode_gfn_meta() Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 17:49 ` sashiko-bot
2026-12-03 11:52 ` [PATCH v6 21/24] KVM: arm64: Extend pKVM page ownership selftests to cover guest block split Vincent Donnefort
` (4 subsequent siblings)
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
This HVC allows the host to break an existing block in a protected VM. This
will later allow the guest to share a sub-region of an existing
huge-mapping with the host.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/include/asm/kvm_asm.h | 1 +
arch/arm64/include/asm/kvm_hcall.h | 2 +
arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 1 +
arch/arm64/kvm/hyp/nvhe/hyp-main.c | 18 ++++
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 97 +++++++++++++++++++
5 files changed, 119 insertions(+)
diff --git a/arch/arm64/include/asm/kvm_asm.h b/arch/arm64/include/asm/kvm_asm.h
index 872185f9ac53..9efbd3b50901 100644
--- a/arch/arm64/include/asm/kvm_asm.h
+++ b/arch/arm64/include/asm/kvm_asm.h
@@ -112,6 +112,7 @@ enum __kvm_host_smccc_func {
__KVM_HOST_SMCCC_FUNC___pkvm_host_wrprotect_guest,
__KVM_HOST_SMCCC_FUNC___pkvm_host_test_clear_young_guest,
__KVM_HOST_SMCCC_FUNC___pkvm_host_mkyoung_guest,
+ __KVM_HOST_SMCCC_FUNC___pkvm_host_split_guest,
__KVM_HOST_SMCCC_FUNC___pkvm_reserve_vm,
__KVM_HOST_SMCCC_FUNC___pkvm_unreserve_vm,
__KVM_HOST_SMCCC_FUNC___pkvm_init_vm,
diff --git a/arch/arm64/include/asm/kvm_hcall.h b/arch/arm64/include/asm/kvm_hcall.h
index e54a4631a378..94eecb24d2c5 100644
--- a/arch/arm64/include/asm/kvm_hcall.h
+++ b/arch/arm64/include/asm/kvm_hcall.h
@@ -234,6 +234,8 @@ DECLARE_KVM_HOST_HCALL(int, __pkvm_host_test_clear_young_guest,
pkvm_handle_t, handle, u64, gfn, u64, nr_pages, bool, mkold)
DECLARE_KVM_HOST_HCALL(int, __pkvm_host_mkyoung_guest,
u64, gfn)
+DECLARE_KVM_HOST_HCALL(int, __pkvm_host_split_guest,
+ u64, gfn, u64, nr_pages)
DECLARE_KVM_HOST_HCALL0(int, __pkvm_reserve_vm)
DECLARE_KVM_HOST_HCALL(void, __pkvm_unreserve_vm,
pkvm_handle_t, handle)
diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
index a991be3d4c9a..c82b1d6352d5 100644
--- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
+++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h
@@ -50,6 +50,7 @@ int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enum kvm_
int __pkvm_host_wrprotect_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vm *hyp_vm);
int __pkvm_host_test_clear_young_guest(u64 gfn, u64 nr_pages, bool mkold, struct pkvm_hyp_vm *vm);
int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu);
+int __pkvm_host_split_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu);
bool addr_is_memory(phys_addr_t phys);
bool addr_is_hyp_text(phys_addr_t phys);
diff --git a/arch/arm64/kvm/hyp/nvhe/hyp-main.c b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
index 41817f45115d..e7b798253dc3 100644
--- a/arch/arm64/kvm/hyp/nvhe/hyp-main.c
+++ b/arch/arm64/kvm/hyp/nvhe/hyp-main.c
@@ -1072,6 +1072,23 @@ DEFINE_KVM_HOST_HCALL(int, __pkvm_host_mkyoung_guest,
return __pkvm_host_mkyoung_guest(gfn, hyp_vcpu);
}
+DEFINE_KVM_HOST_HCALL(int, __pkvm_host_split_guest,
+ u64, gfn, u64, nr_pages)
+{
+ struct pkvm_hyp_vcpu *hyp_vcpu;
+ int ret;
+
+ hyp_vcpu = pkvm_get_loaded_hyp_vcpu();
+ if (!hyp_vcpu || !pkvm_hyp_vcpu_is_protected(hyp_vcpu))
+ return -EINVAL;
+
+ ret = pkvm_refill_memcache(hyp_vcpu);
+ if (ret)
+ return ret;
+
+ return __pkvm_host_split_guest(gfn, nr_pages, hyp_vcpu);
+}
+
/*
* PKVM_HOST_STATE_DIRTY names the authoritative copy, the host's when set.
* A loaded protected vCPU takes the request at its next entry instead.
@@ -1599,6 +1616,7 @@ static const hcall_t host_hcall[] = {
HANDLE_FUNC(__pkvm_host_wrprotect_guest),
HANDLE_FUNC(__pkvm_host_test_clear_young_guest),
HANDLE_FUNC(__pkvm_host_mkyoung_guest),
+ HANDLE_FUNC(__pkvm_host_split_guest),
HANDLE_FUNC(__pkvm_reserve_vm),
HANDLE_FUNC(__pkvm_unreserve_vm),
HANDLE_FUNC(__pkvm_init_vm),
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index d94e1d94d995..118f92472299 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -699,6 +699,54 @@ static int host_stage2_decode_gfn_meta(kvm_pte_t pte, pkvm_handle_t *handle, u64
return 0;
}
+static int host_stage2_split_gfn_meta(phys_addr_t phys, u64 ipa, u64 size, struct pkvm_hyp_vm *vm)
+{
+ struct kvm_pgtable_mm_ops *mm_ops = &host_mmu.mm_ops;
+ struct kvm_pgtable *pgt = &host_mmu.pgt;
+ kvm_pte_t pte, *table, *ptep;
+ pkvm_handle_t handle;
+ s8 level;
+ u64 gfn;
+ int ret;
+
+ ret = kvm_pgtable_get_leaf(pgt, phys, &pte, &level, 0);
+ if (ret)
+ return ret;
+
+ if (kvm_granule_size(level) != size)
+ return -EINVAL;
+
+ ret = host_stage2_decode_gfn_meta(pte, &handle, &gfn);
+ if (ret)
+ return ret;
+
+ if (handle != vm->kvm.arch.pkvm.handle || gfn != (ipa >> PAGE_SHIFT))
+ return -EINVAL;
+
+ table = mm_ops->zalloc_page(&host_s2_pool);
+ if (!table)
+ return -ENOMEM;
+
+ ptep = table;
+ while (ptep < table + PTRS_PER_PTE) {
+ u64 meta = host_stage2_encode_gfn_meta(vm, gfn);
+ kvm_pte_t annotation = host_stage2_encode_annotation(PKVM_ID_GUEST, meta);
+
+ *ptep = FIELD_PREP(KVM_INVALID_PTE_TYPE_MASK, KVM_HOST_INVALID_PTE_TYPE_DONATION) |
+ annotation;
+
+ mm_ops->get_page(ptep);
+ ptep++;
+ gfn++;
+ }
+
+ ret = kvm_pgtable_stage2_table_install(pgt, table, phys, level);
+ if (ret)
+ kvm_pgtable_stage2_free_unlinked(mm_ops, table, level);
+
+ return ret;
+}
+
static bool host_stage2_force_pte_cb(u64 addr, u64 end, enum kvm_pgtable_prot prot)
{
/*
@@ -1783,6 +1831,55 @@ int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu)
return 0;
}
+int __pkvm_host_split_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu)
+{
+ struct kvm_hyp_memcache *mc = &vcpu->vcpu.arch.stage2_mc;
+ struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
+ u64 size = nr_pages << PAGE_SHIFT;
+ u64 ipa = hyp_pfn_to_phys(gfn);
+ kvm_pte_t pte, *table;
+ phys_addr_t phys;
+ s8 level;
+ int ret;
+
+ if (!mc->nr_pages)
+ return -ENOMEM;
+
+ if (size != PMD_SIZE || !IS_ALIGNED(ipa, PMD_SIZE))
+ return -EINVAL;
+
+ level = KVM_PGTABLE_LAST_LEVEL - 1;
+
+ host_lock_component();
+ guest_lock_component(vm);
+
+ ret = get_valid_guest_pte(vm, ipa, size, &pte, &phys);
+ if (ret)
+ goto unlock;
+
+ table = kvm_pgtable_stage2_create_unlinked(&vm->pgt, kvm_pte_to_phys(pte), level,
+ kvm_pgtable_stage2_pte_prot(pte),
+ mc, true);
+ if (IS_ERR(table)) {
+ ret = PTR_ERR(table);
+ goto unlock;
+ }
+
+ ret = host_stage2_try(host_stage2_split_gfn_meta, phys, ipa, size, vm);
+ if (ret) {
+ kvm_pgtable_stage2_free_unlinked(&vm->mm_ops, table, level);
+ goto unlock;
+ }
+
+ WARN_ON(kvm_pgtable_stage2_table_install(&vm->pgt, table, ipa, level));
+
+unlock:
+ guest_unlock_component(vm);
+ host_unlock_component();
+
+ return ret;
+}
+
#ifdef CONFIG_NVHE_EL2_DEBUG
struct pkvm_expected_state {
enum pkvm_page_state host;
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 20/24] KVM: arm64: Add __pkvm_host_split_guest HVC
2026-12-03 11:52 ` [PATCH v6 20/24] KVM: arm64: Add __pkvm_host_split_guest HVC Vincent Donnefort
@ 2026-12-03 17:49 ` sashiko-bot
0 siblings, 0 replies; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 17:49 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Low] KVM: arm64: __pkvm_host_split_guest() skips guest page state
check
- [Low] KVM: arm64: left-shift overflow on inputs in
__pkvm_host_split_guest()
- [Low] KVM: arm64: premature mc->nr_pages check in
__pkvm_host_split_guest()
--
commit c0a936c062694cf8e366e1ea2cfe5c0af145f622
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Add __pkvm_host_split_guest HVC
This commit adds the __pkvm_host_split_guest host hypercall and its EL2
implementation to split a PMD block mapping in a protected VM's stage-2
page table along with the corresponding host stage-2 GFN metadata.
> diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> index d94e1d94d9953..118f92472299d 100644
> --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
> +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
[ ... ]
> @@ -1783,6 +1831,55 @@ int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu)
> return 0;
> }
>
> +int __pkvm_host_split_guest(u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu *vcpu)
> +{
> + struct kvm_hyp_memcache *mc = &vcpu->vcpu.arch.stage2_mc;
> + struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu);
> + u64 size = nr_pages << PAGE_SHIFT;
> + u64 ipa = hyp_pfn_to_phys(gfn);
> + kvm_pte_t pte, *table;
> + phys_addr_t phys;
> + s8 level;
> + int ret;
> +
> + if (!mc->nr_pages)
> + return -ENOMEM;
[Severity: Low]
Is this upfront mc->nr_pages check needed here? Checking it before
validating the arguments and guest PTE returns -ENOMEM instead of
-EINVAL, -ENOENT, or -E2BIG when stage2_mc is empty, and also rejects
the split even when vm->pool already has free pages.
Since kvm_pgtable_stage2_create_unlinked() allocates before either page
table is modified, and guest_s2_zalloc_page() tries vm->pool before
falling back to stage2_mc, dropping this check would preserve error
precedence and allow reusing pooled pages.
> +
> + if (size != PMD_SIZE || !IS_ALIGNED(ipa, PMD_SIZE))
> + return -EINVAL;
[Severity: Low]
Can the left shifts on nr_pages and gfn silently truncate out-of-range
host HVC inputs? Non-zero bits in the upper PAGE_SHIFT bits wrap
modulo 2^64, allowing invalid arguments to pass these checks and split
an aliased PMD block instead of returning -EINVAL.
Comparing nr_pages directly against PMD_SIZE >> PAGE_SHIFT (as in
__guest_check_transition_size()) and checking hyp_phys_to_pfn(ipa) == gfn
would avoid the truncation.
> +
> + level = KVM_PGTABLE_LAST_LEVEL - 1;
> +
> + host_lock_component();
> + guest_lock_component(vm);
> +
> + ret = get_valid_guest_pte(vm, ipa, size, &pte, &phys);
> + if (ret)
> + goto unlock;
> +
> + table = kvm_pgtable_stage2_create_unlinked(&vm->pgt, kvm_pte_to_phys(pte), level,
> + kvm_pgtable_stage2_pte_prot(pte),
> + mc, true);
> + if (IS_ERR(table)) {
> + ret = PTR_ERR(table);
> + goto unlock;
> + }
> +
> + ret = host_stage2_try(host_stage2_split_gfn_meta, phys, ipa, size, vm);
[Severity: Low]
Should __pkvm_host_split_guest() verify that guest_get_page_state(pte, ipa)
is PKVM_PAGE_OWNED before allocating the unlinked table and calling
host_stage2_split_gfn_meta()?
For a PMD block in PKVM_PAGE_SHARED_OWNED state, __pkvm_guest_share_host()
leaves a valid PMD in both vm->pgt and host_mmu.pgt. Passing that valid
host PTE from host_stage2_split_gfn_meta() to host_stage2_decode_gfn_meta()
hits WARN_ON(kvm_pte_valid(pte)) and panics EL2.
Today only __pkvm_ownership_selftest() shares a PMD_SIZE block from a
protected VM, so this is host-kernel-only for now, but checking the guest
page state first avoids the EL2 panic and prevents it from becoming
guest-triggerable once huge-page guest memshare is enabled.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=20
^ permalink raw reply [flat|nested] 38+ messages in thread
* [PATCH v6 21/24] KVM: arm64: Extend pKVM page ownership selftests to cover guest block split
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (19 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 20/24] KVM: arm64: Add __pkvm_host_split_guest HVC Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 22/24] KVM: arm64: Add PKVM_HYP_REQ_SPLIT Vincent Donnefort
` (3 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
Extend the pKVM page ownership selftests to test the split of a guest
stage-2 block and the per-page reclaim of that same block.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/mem_protect.c | 41 +++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 118f92472299..c2635d8c4131 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -2129,6 +2129,47 @@ int __pkvm_ownership_selftest(u64 pfn, u64 nr_pages, u64 pgtable_pfn,
assert_transition_res(-EPERM, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, nr_pages, vcpu, prot);
+ if (size == PMD_SIZE) {
+ /* [gfn, gfn + nr_pages) was poisoned. Skip it */
+ gfn += nr_pages;
+
+ selftest_state.host = PKVM_NOPAGE;
+ selftest_state.guest[1] = PKVM_PAGE_OWNED;
+ assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(-E2BIG, __pkvm_host_reclaim_guest, gfn, 1, vm);
+ assert_transition_res(-E2BIG, __pkvm_guest_share_host, vcpu, gfn, 1);
+ assert_transition_res(0, __pkvm_host_split_guest, gfn, nr_pages, vcpu);
+
+ assert_transition_res(-E2BIG, __pkvm_host_split_guest, gfn, nr_pages, vcpu);
+ assert_transition_res(-E2BIG, __pkvm_host_reclaim_guest, gfn, nr_pages, vm);
+ assert_transition_res(-E2BIG, __pkvm_guest_share_host, vcpu, gfn, nr_pages);
+
+ for (int i = 0; i < nr_pages; i++) {
+ WARN_ON(__pkvm_guest_share_host(vcpu, gfn + i, 1));
+ WARN_ON(__pkvm_guest_unshare_host(vcpu, gfn + i, 1));
+ }
+ assert_page_state();
+
+ for (int i = 0; i < nr_pages; i++)
+ WARN_ON(__pkvm_host_reclaim_guest(gfn + i, 1, vm));
+
+ selftest_state.host = PKVM_PAGE_OWNED;
+ selftest_state.guest[1] = PKVM_NOPAGE;
+ assert_page_state();
+
+ selftest_state.host = PKVM_NOPAGE;
+ selftest_state.guest[1] = PKVM_PAGE_OWNED;
+ assert_transition_res(0, __pkvm_host_donate_guest, pfn, gfn, nr_pages, vcpu);
+ assert_transition_res(0, __pkvm_host_split_guest, gfn, nr_pages, vcpu);
+
+ for (int i = 0; i < nr_pages; i++)
+ WARN_ON(__pkvm_host_force_reclaim_page_guest(phys + i * PAGE_SIZE));
+
+ selftest_state.host = PKVM_PAGE_OWNED;
+ selftest_state.guest[1] = PKVM_POISON;
+ assert_page_state();
+ }
+
selftest_state.host = PKVM_NOPAGE;
selftest_state.hyp = PKVM_PAGE_OWNED;
assert_transition_res(0, __pkvm_host_donate_hyp, pfn, nr_pages);
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 22/24] KVM: arm64: Add PKVM_HYP_REQ_SPLIT
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (20 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 21/24] KVM: arm64: Extend pKVM page ownership selftests to cover guest block split Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 23/24] KVM: arm64: Raise PKVM_HYP_REQ_SPLIT on guest to host sharing Vincent Donnefort
` (2 subsequent siblings)
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
With the upcoming support for stage-2 huge mappings for protected VMs,
we need a way to split blocks. Since the host has its own "copy" of the
guest stage-2 in the pkvm_mappings rb-tree, the split must be done
simultaneously for both that tree and the guest stage-2. Therefore the
hypervisor can't do it on its own and must rely on the host for this
operation.
Create a pKVM hypervisor request to ask the host to split a specified
region of the guest. On this request, the host can synchronise the split
of both guest stage-2 (HVC __pkvm_host_split_guest) and the
pkvm_mappings tree. It ensures a concurrent VM teardown can't observe a
PMD_SIZE pkvm_mapping while the guest stage-2 is PAGE_SIZE.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/include/asm/kvm_host.h | 5 ++
arch/arm64/include/asm/kvm_pkvm.h | 5 +-
arch/arm64/kvm/pkvm.c | 133 ++++++++++++++++++++++++++++++
arch/arm64/kvm/trace_pkvm.h | 4 +-
4 files changed, 145 insertions(+), 2 deletions(-)
diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h
index 0161bcb8123b..4606c8073743 100644
--- a/arch/arm64/include/asm/kvm_host.h
+++ b/arch/arm64/include/asm/kvm_host.h
@@ -142,6 +142,7 @@ enum pkvm_hyp_req_type {
PKVM_HYP_NO_REQ = 0,
PKVM_HYP_REQ_HYP_ALLOC,
PKVM_HYP_REQ_HYP_ALLOC_SELFTEST,
+ PKVM_HYP_REQ_SPLIT,
__PKVM_HYP_REQ_TYPE_MAX,
};
@@ -154,6 +155,10 @@ struct pkvm_hyp_req {
struct {
u32 nr_pages;
} mem;
+ struct {
+ u64 gfn;
+ u64 nr_pages;
+ } split;
struct {
/* Helper for SMCCC encoding/decoding */
u8 args[PKVM_HYP_REQ_SMCCC_ARG_SIZE_MAX];
diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm_pkvm.h
index 3e51337f2d9e..5676eed1c71e 100644
--- a/arch/arm64/include/asm/kvm_pkvm.h
+++ b/arch/arm64/include/asm/kvm_pkvm.h
@@ -222,7 +222,10 @@ static inline size_t pkvm_host_sve_state_size(void)
}
struct pkvm_mapping {
- struct rb_node node;
+ union {
+ struct rb_node node;
+ struct list_head list;
+ };
u64 gfn;
u64 pfn;
struct {
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index 7ae4aa0cb81c..edc5416bbd8a 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -493,6 +493,64 @@ INTERVAL_TREE_DEFINE(struct pkvm_mapping, node, u64, __subtree_last,
}); \
)
+static void pkvm_mapping_free_spares(struct list_head *spares)
+{
+ struct pkvm_mapping *m, *tmp;
+
+ list_for_each_entry_safe(m, tmp, spares, list) {
+ list_del(&m->list);
+ kfree(m);
+ }
+}
+
+static int pkvm_mapping_alloc_spares(struct list_head *head, u64 nr_spares)
+{
+ struct pkvm_mapping *m;
+
+ while (nr_spares--) {
+ m = kzalloc_obj(*m, GFP_KERNEL_ACCOUNT);
+ if (!m) {
+ pkvm_mapping_free_spares(head);
+ return -ENOMEM;
+ }
+
+ list_add(&m->list, head);
+ }
+
+ return 0;
+}
+
+static void pkvm_mapping_split(struct pkvm_mapping *mapping, struct kvm_pgtable *pgt,
+ struct list_head *spares)
+{
+ struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
+ u64 nr_pages = mapping->nr_pages - 1;
+ gfn_t gfn = mapping->gfn + 1;
+ u64 pfn = mapping->pfn + 1;
+
+ lockdep_assert_held_write(&kvm->mmu_lock);
+
+ pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
+ mapping->nr_pages = 1;
+ pkvm_mapping_insert(mapping, &pgt->pkvm_mappings);
+
+ while (nr_pages--) {
+ struct pkvm_mapping *m;
+
+ if (WARN_ON(list_empty(spares)))
+ break;
+
+ m = list_first_entry(spares, struct pkvm_mapping, list);
+ list_del(&m->list);
+
+ m->nr_pages = 1;
+ m->gfn = gfn++;
+ m->pfn = pfn++;
+
+ pkvm_mapping_insert(m, &pgt->pkvm_mappings);
+ }
+}
+
int pkvm_pgtable_stage2_init(struct kvm_pgtable *pgt, struct kvm_s2_mmu *mmu,
struct kvm_pgtable_mm_ops *mm_ops)
{
@@ -758,6 +816,74 @@ int pkvm_pgtable_stage2_split(struct kvm_pgtable *pgt, u64 addr, u64 size,
return -EINVAL;
}
+/*
+ * Splitting is only expected on the back of a guest HVC, while
+ * pkvm_pgtable_stage2_split() can be called with dirty logging.
+ */
+static int __pkvm_pgtable_stage2_split(struct kvm_vcpu *vcpu, phys_addr_t ipa, u64 size)
+{
+ struct kvm_hyp_memcache *mc = &vcpu->arch.stage2_mc;
+ struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt;
+ struct pkvm_mapping *mapping;
+ struct kvm *kvm = vcpu->kvm;
+ struct list_head spares;
+ struct folio *folio;
+ u64 nr_pages;
+ int ret;
+
+ if (WARN_ON(!kvm_vm_is_protected(kvm)))
+ return -EINVAL;
+
+ if (!IS_ALIGNED(ipa, PMD_SIZE) || size != PMD_SIZE)
+ return -EINVAL;
+
+ ret = topup_hyp_memcache(mc, 1);
+ if (ret)
+ return ret;
+
+ /* We already have 1 pin on the huge-page */
+ nr_pages = (size / PAGE_SIZE) - 1;
+
+ INIT_LIST_HEAD(&spares);
+ ret = pkvm_mapping_alloc_spares(&spares, nr_pages);
+ if (ret)
+ return ret;
+
+ write_lock(&kvm->mmu_lock);
+
+ mapping = pkvm_mapping_iter_first(&pgt->pkvm_mappings, ipa, ipa + size - 1);
+ if (!mapping) {
+ ret = -EPERM;
+ goto unlock_mmu;
+ } else if (mapping->nr_pages == 1) {
+ /* We've raced with another vCPU */
+ ret = 0;
+ goto unlock_mmu;
+ } else if (mapping->nr_pages * PAGE_SIZE != PMD_SIZE) {
+ ret = -EINVAL;
+ goto unlock_mmu;
+ }
+
+ folio = page_folio(pfn_to_page(mapping->pfn));
+ ret = folio_add_pins(folio, nr_pages);
+ if (ret)
+ goto unlock_mmu;
+
+ ret = kvm_call_hyp_nvhe(__pkvm_host_split_guest, gpa_to_gfn(ipa), size / PAGE_SIZE);
+ if (ret) {
+ unpin_user_folio(folio, nr_pages);
+ goto unlock_mmu;
+ }
+
+ pkvm_mapping_split(mapping, pgt, &spares);
+
+unlock_mmu:
+ write_unlock(&kvm->mmu_lock);
+ pkvm_mapping_free_spares(&spares);
+
+ return ret;
+}
+
/*
* Forcefully reclaim a page from the guest, zeroing its contents and
* poisoning the stage-2 pte so that pages can no longer be mapped at
@@ -781,6 +907,13 @@ static int pkvm_handle_hyp_req(struct pkvm_hyp_req *req, struct kvm_vcpu *vcpu)
case PKVM_HYP_REQ_HYP_ALLOC_SELFTEST:
ret = pkvm_hyp_topup(PKVM_TOPUP_HYP_ALLOC_SELFTEST, req->mem.nr_pages);
break;
+ case PKVM_HYP_REQ_SPLIT:
+ if (WARN_ON_ONCE(!vcpu))
+ break;
+
+ ret = __pkvm_pgtable_stage2_split(vcpu, gfn_to_gpa(req->split.gfn),
+ req->split.nr_pages * PAGE_SIZE);
+ break;
}
trace_kvm_handle_pkvm_hyp_req(req, ret);
diff --git a/arch/arm64/kvm/trace_pkvm.h b/arch/arm64/kvm/trace_pkvm.h
index 493225e3282d..a0787114b892 100644
--- a/arch/arm64/kvm/trace_pkvm.h
+++ b/arch/arm64/kvm/trace_pkvm.h
@@ -10,10 +10,12 @@
TRACE_DEFINE_ENUM(PKVM_HYP_NO_REQ);
TRACE_DEFINE_ENUM(PKVM_HYP_REQ_HYP_ALLOC);
+TRACE_DEFINE_ENUM(PKVM_HYP_REQ_SPLIT);
#define PKVM_HYP_REQ_TYPES \
{ PKVM_HYP_NO_REQ, "NO_REQ" }, \
- { PKVM_HYP_REQ_HYP_ALLOC, "HYP_ALLOC" }
+ { PKVM_HYP_REQ_HYP_ALLOC, "HYP_ALLOC" }, \
+ { PKVM_HYP_REQ_SPLIT, "SPLIT" }
TRACE_EVENT(kvm_handle_pkvm_hyp_req,
TP_PROTO(struct pkvm_hyp_req *req, int ret),
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 23/24] KVM: arm64: Raise PKVM_HYP_REQ_SPLIT on guest to host sharing
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (21 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 22/24] KVM: arm64: Add PKVM_HYP_REQ_SPLIT Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 11:52 ` [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs Vincent Donnefort
2026-12-03 19:27 ` [PATCH v6 00/24] Huge mapping support " Fuad Tabba
24 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
Pages shared by the guest with the host are annotated into the guest
stage-2. If the shared page is backed by a huge mapping, we need to
split it first.
Raise a PKVM_HYP_REQ_SPLIT for the host on the guest HVC MEM_SHARE, if a
block exists (-E2BIG) and replay the HVC (by rewinding the ELR).
As the guest HVC MEM_SHARE is single-page only, MEM_UNSHARE is ensured
to find a PTE-level mapping and does not need to request a split.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/hyp/nvhe/pkvm.c | 23 ++++++++++++++++++++++-
1 file changed, 22 insertions(+), 1 deletion(-)
diff --git a/arch/arm64/kvm/hyp/nvhe/pkvm.c b/arch/arm64/kvm/hyp/nvhe/pkvm.c
index d92f4da1c180..b563100588b6 100644
--- a/arch/arm64/kvm/hyp/nvhe/pkvm.c
+++ b/arch/arm64/kvm/hyp/nvhe/pkvm.c
@@ -1169,16 +1169,34 @@ static u64 __pkvm_memshare_page_req(struct kvm_vcpu *vcpu, u64 ipa)
return ARM_EXCEPTION_TRAP;
}
+static u64 pkvm_request_split(struct pkvm_hyp_vcpu *hyp_vcpu, u64 gfn, u64 nr_pages)
+{
+ struct pkvm_hyp_req *req = &hyp_vcpu->host_vcpu->arch.pkvm_hyp_req;
+ u64 elr;
+
+ req->type = PKVM_HYP_REQ_SPLIT;
+ req->split.gfn = ALIGN_DOWN(gfn, PMD_SIZE / PAGE_SIZE);
+ req->split.nr_pages = ALIGN(gfn + nr_pages, PMD_SIZE / PAGE_SIZE) - req->split.gfn;
+
+ /* Rewind the ELR so we return to the HVC once the block is split */
+ elr = read_sysreg(elr_el2);
+ elr -= 4;
+ write_sysreg(elr, elr_el2);
+
+ return ARM_EXCEPTION_PKVM_HYP_REQ;
+}
+
static bool pkvm_memshare_call(u64 *ret, struct kvm_vcpu *vcpu, u64 *exit_code)
{
struct pkvm_hyp_vcpu *hyp_vcpu;
u64 ipa = smccc_get_arg1(vcpu);
+ u64 gfn = hyp_phys_to_pfn(ipa);
if (!PAGE_ALIGNED(ipa))
goto out_guest;
hyp_vcpu = container_of(vcpu, struct pkvm_hyp_vcpu, vcpu);
- switch (__pkvm_guest_share_host(hyp_vcpu, hyp_phys_to_pfn(ipa), 1)) {
+ switch (__pkvm_guest_share_host(hyp_vcpu, gfn, 1)) {
case 0:
ret[0] = SMCCC_RET_SUCCESS;
goto out_guest;
@@ -1189,6 +1207,9 @@ static bool pkvm_memshare_call(u64 *ret, struct kvm_vcpu *vcpu, u64 *exit_code)
*/
*exit_code = __pkvm_memshare_page_req(vcpu, ipa);
goto out_host;
+ case -E2BIG:
+ *exit_code = pkvm_request_split(hyp_vcpu, gfn, 1);
+ goto out_host;
}
out_guest:
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (22 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 23/24] KVM: arm64: Raise PKVM_HYP_REQ_SPLIT on guest to host sharing Vincent Donnefort
@ 2026-12-03 11:52 ` Vincent Donnefort
2026-12-03 19:28 ` sashiko-bot
2026-12-03 19:27 ` [PATCH v6 00/24] Huge mapping support " Fuad Tabba
24 siblings, 1 reply; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-03 11:52 UTC (permalink / raw)
To: maz, oupton, kvmarm, linux-arm-kernel
Cc: joey.gouly, seiden, suzuki.poulose, yuzenghui, catalin.marinas,
will, kernel-team, fuad.tabba, qperret, weilin.chang,
Vincent Donnefort
Enable PMD-sized stage-2 block mappings for protected VMs. This is
possible whenever the stage-1 mapping allows it, that is, if it is
itself backed by THPs.
When a THP is found, an entire PMD_SIZE mapping is donated to the guest,
unless it overlaps an existing mapping. The new PMD_SIZE mapping can
only be broken down via the HVC __pkvm_host_split_guest() which the
hypervisor can request with PKVM_HYP_REQ_SPLIT.
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
arch/arm64/kvm/mmu.c | 128 +++++++++++++++++++++++++++---------------
arch/arm64/kvm/pkvm.c | 34 +++++++----
2 files changed, 104 insertions(+), 58 deletions(-)
diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
index 1db10d8d6db7..33cd54cee0e4 100644
--- a/arch/arm64/kvm/mmu.c
+++ b/arch/arm64/kvm/mmu.c
@@ -1489,11 +1489,9 @@ static bool fault_supports_stage2_huge_mapping(const struct kvm_s2_fault_desc *s
if (map_size == PAGE_SIZE)
return true;
- /* pKVM only supports PMD_SIZE huge-mappings for non-protected VMs */
- if (is_protected_kvm_enabled()) {
- if (vcpu_is_protected(s2fd->vcpu) || map_size != PMD_SIZE)
- return false;
- }
+ /* pKVM only supports PMD_SIZE huge-mappings */
+ if (is_protected_kvm_enabled() && map_size != PMD_SIZE)
+ return false;
size = memslot->npages * PAGE_SIZE;
@@ -1843,42 +1841,23 @@ static int gmem_abort(const struct kvm_s2_fault_desc *s2fd,
return ret;
}
-static int protected_pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
+static int pkvm_pin_user_pages(const struct kvm_s2_fault_desc *s2fd,
+ struct kvm_s2_fault_vma_info *s2vi)
{
unsigned int flags = FOLL_HWPOISON | FOLL_LONGTERM | FOLL_WRITE;
- struct kvm_s2_fault_vma_info s2vi = {};
- struct kvm_vcpu *vcpu = s2fd->vcpu;
- struct kvm_pgtable *pgt = s2fd->mmu->pgt;
- struct mm_struct *mm = current->mm;
- struct kvm *kvm = vcpu->kvm;
- void *hyp_memcache;
- int ret;
+ long ret;
- hyp_memcache = get_mmu_memcache(vcpu);
- ret = topup_mmu_memcache(s2fd->mmu, hyp_memcache);
- if (ret)
- return -ENOMEM;
-
- ret = kvm_s2_fault_get_vma_info(s2fd, &s2vi);
+ ret = kvm_s2_fault_get_vma_info(s2fd, s2vi);
if (ret)
return ret;
- ret = account_locked_vm(mm, 1, true);
- if (ret)
- return ret;
-
- mmap_read_lock(mm);
- ret = pin_user_pages(s2fd->hva, 1, flags, &s2vi.page);
- mmap_read_unlock(mm);
-
+ ret = pin_user_pages_unlocked(s2fd->hva, 1, &s2vi->page, flags);
if (ret == -EHWPOISON) {
kvm_send_hwpoison_signal(s2fd->hva, PAGE_SHIFT);
- ret = 0;
- goto dec_account;
+ return ret;
} else if (ret != 1) {
- ret = -EFAULT;
- goto dec_account;
- } else if (!folio_test_swapbacked(page_folio(s2vi.page))) {
+ return -EFAULT;
+ } else if (!folio_test_swapbacked(page_folio(s2vi->page))) {
/*
* We really can't deal with page-cache pages returned by GUP
* because (a) we may trigger writeback of a page for which we
@@ -1893,26 +1872,83 @@ static int protected_pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
* pages backed by swap in the knowledge that the GUP pin will
* prevent try_to_unmap() from succeeding.
*/
- ret = -EIO;
- goto unpin;
+ unpin_user_page(s2vi->page);
+ return -EIO;
}
- write_lock(&kvm->mmu_lock);
- ret = pkvm_pgtable_stage2_map(pgt, gfn_to_gpa(s2vi.gfn), PAGE_SIZE,
- page_to_phys(s2vi.page), KVM_PGTABLE_PROT_RWX,
- hyp_memcache, 0);
- write_unlock(&kvm->mmu_lock);
- if (ret) {
- if (ret == -EAGAIN)
- ret = 0;
- goto unpin;
+ s2vi->pfn = page_to_pfn(s2vi->page);
+
+ ret = transparent_hugepage_adjust(s2fd, &s2vi->pfn, &s2vi->gfn);
+ if (ret < 0) {
+ unpin_user_page(s2vi->page);
+ return ret;
}
+ s2vi->vma_pagesize = ret;
+
return 0;
+}
+
+static int protected_pkvm_mem_abort(const struct kvm_s2_fault_desc *s2fd)
+{
+ struct kvm_s2_fault_vma_info s2vi = {};
+ struct kvm_vcpu *vcpu = s2fd->vcpu;
+ struct kvm_pgtable *pgt = s2fd->mmu->pgt;
+ struct mm_struct *mm = current->mm;
+ struct kvm *kvm = vcpu->kvm;
+ void *hyp_memcache;
+ u64 size;
+ int ret;
+
+ hyp_memcache = get_mmu_memcache(vcpu);
+ ret = topup_mmu_memcache(s2fd->mmu, hyp_memcache);
+ if (ret)
+ return -ENOMEM;
+
+ ret = pkvm_pin_user_pages(s2fd, &s2vi);
+ if (ret == -EHWPOISON || ret == -EAGAIN)
+ return 0;
+ else if (ret)
+ return ret;
+
+account:
+ size = s2vi.vma_pagesize;
+ ret = account_locked_vm(mm, size / PAGE_SIZE, true);
+
+retry:
+ /* undo transparent_hugepage_adjust() and try again */
+ if (ret && size > PAGE_SIZE) {
+ s2vi.vma_pagesize = PAGE_SIZE;
+ s2vi.gfn = gpa_to_gfn(s2fd->fault_ipa);
+ s2vi.pfn = page_to_pfn(s2vi.page);
+ goto account;
+ }
+
+ if (ret)
+ goto unpin;
+
+ kvm_fault_lock(kvm);
+ if (mmu_invalidate_retry(kvm, s2vi.mmu_seq))
+ ret = -EAGAIN;
+ else
+ ret = pkvm_pgtable_stage2_map(pgt, gfn_to_gpa(s2vi.gfn), size,
+ __pfn_to_phys(s2vi.pfn), KVM_PGTABLE_PROT_RWX,
+ hyp_memcache, 0);
+ kvm_fault_unlock(kvm);
+
+ if (ret) {
+ account_locked_vm(mm, size / PAGE_SIZE, false);
+ if (ret == -E2BIG)
+ goto retry;
+ }
+
unpin:
- unpin_user_page(s2vi.page);
-dec_account:
- account_locked_vm(mm, 1, false);
+ if (ret) {
+ unpin_user_page(s2vi.page);
+ if (ret == -EAGAIN)
+ ret = 0;
+ }
+
return ret;
}
diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
index edc5416bbd8a..0342f8e93b6d 100644
--- a/arch/arm64/kvm/pkvm.c
+++ b/arch/arm64/kvm/pkvm.c
@@ -577,9 +577,8 @@ static int __pkvm_pgtable_stage2_reclaim(struct kvm_pgtable *pgt, u64 start, u64
continue;
page = pfn_to_page(mapping->pfn);
- WARN_ON_ONCE(mapping->nr_pages != 1);
unpin_user_pages_dirty_lock(&page, 1, true);
- account_locked_vm(kvm->mm, 1, false);
+ account_locked_vm(kvm->mm, mapping->nr_pages, false);
pkvm_mapping_remove(mapping, &pgt->pkvm_mappings);
kfree(mapping);
}
@@ -632,9 +631,17 @@ void pkvm_pgtable_stage2_destroy_pgd(struct kvm_pgtable *pgt)
WARN_ON_ONCE(!RB_EMPTY_ROOT(&pgt->pkvm_mappings.rb_root));
}
+/*
+ * Return:
+ * 0 - Mapping successfully installed.
+ * -EAGAIN - Mapping already exists due to a concurrent vCPU fault race.
+ * -E2BIG - Mapping overlaps an existing smaller one.
+ * -EFAULT - Mapping overlaps an existing poisoned address.
+ * < 0 - Other negative error code on invalid arguments or HVC failure.
+ */
int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
- u64 phys, enum kvm_pgtable_prot prot,
- void *mc, enum kvm_pgtable_walk_flags flags)
+ u64 phys, enum kvm_pgtable_prot prot,
+ void *mc, enum kvm_pgtable_walk_flags flags)
{
struct kvm *kvm = kvm_s2_mmu_to_kvm(pgt->mmu);
struct pkvm_mapping *mapping = NULL;
@@ -644,20 +651,22 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
u64 end = addr + size;
int ret;
- if (WARN_ON_ONCE(!PAGE_ALIGNED(addr | size)))
+ if (WARN_ON_ONCE(!PAGE_ALIGNED(addr)))
+ return -EINVAL;
+
+ if (WARN_ON_ONCE(size != PAGE_SIZE && size != PMD_SIZE))
return -EINVAL;
lockdep_assert_held_write(&kvm->mmu_lock);
mapping = pkvm_mapping_iter_first(&pgt->pkvm_mappings, addr, end - 1);
if (kvm_vm_is_protected(kvm)) {
- /* Protected VMs are mapped using RWX page-granular mappings */
- if (WARN_ON_ONCE(size != PAGE_SIZE))
- return -EINVAL;
-
if (WARN_ON_ONCE(prot != KVM_PGTABLE_PROT_RWX))
return -EINVAL;
+ if (mapping && mapping->nr_pages == 1 && size > PAGE_SIZE)
+ return -E2BIG;
+
/*
* We either raced with another vCPU or the guest PTE
* has been poisoned by an erroneous host access.
@@ -667,10 +676,11 @@ int pkvm_pgtable_stage2_map(struct kvm_pgtable *pgt, u64 addr, u64 size,
return ret ? -EFAULT : -EAGAIN;
}
- ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, 1);
+ ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, size / PAGE_SIZE);
+
+ /* Warn if -E2BIG is overloaded */
+ WARN_ON_ONCE(ret == -E2BIG);
} else {
- if (WARN_ON_ONCE(size != PAGE_SIZE && size != PMD_SIZE))
- return -EINVAL;
/*
* We either raced with another vCPU or we're changing between
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] 38+ messages in thread* Re: [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs
2026-12-03 11:52 ` [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs Vincent Donnefort
@ 2026-12-03 19:28 ` sashiko-bot
2026-12-04 14:50 ` Vincent Donnefort
0 siblings, 1 reply; 38+ messages in thread
From: sashiko-bot @ 2026-12-03 19:28 UTC (permalink / raw)
To: Vincent Donnefort, kvmarm; +Cc: Oliver Upton, Marc Zyngier
Thank you for your contribution! Sashiko AI review found 4 potential
issues to consider:
- [High] KVM: arm64: unaligned PMD mapping breaks pKVM under 2-level
pagetables
- [Medium] KVM: arm64: missing PAGE_SIZE fallback on -EPERM in
protected_pkvm_mem_abort()
- [Medium] KVM: arm64: PMD hyp_poison_range() holds EL2 host_mmu.lock
with IRQs off
- [Medium] KVM: arm64: locked_vm over-charge and lock churn in
protected_pkvm_mem_abort()
--
commit 9c8f56cac08156f554f633ca7346cbaaeadec9a8
Author: Vincent Donnefort <vdonnefort@google.com>
KVM: arm64: Stage-2 huge mappings for protected VMs
This commit enables PMD_SIZE stage-2 block mappings for protected VMs
under pKVM when the host stage-1 mapping is backed by a transparent huge
page. It adjusts protected_pkvm_mem_abort() and pkvm_pgtable_stage2_map()
to donate PMD_SIZE ranges and fall back to PAGE_SIZE on -E2BIG or locked
memory accounting failures.
> diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> index 1db10d8d6db73..33cd54cee0e42 100644
> --- a/arch/arm64/kvm/mmu.c
> +++ b/arch/arm64/kvm/mmu.c
> @@ -1489,11 +1489,9 @@ static bool fault_supports_stage2_huge_mapping(const struct kvm_s2_fault_desc *s
> if (map_size == PAGE_SIZE)
> return true;
>
> - /* pKVM only supports PMD_SIZE huge-mappings for non-protected VMs */
> - if (is_protected_kvm_enabled()) {
> - if (vcpu_is_protected(s2fd->vcpu) || map_size != PMD_SIZE)
> - return false;
> - }
> + /* pKVM only supports PMD_SIZE huge-mappings */
> + if (is_protected_kvm_enabled() && map_size != PMD_SIZE)
> + return false;
[Severity: Medium]
Can enabling PMD_SIZE mappings for protected VMs hold host_mmu.lock at EL2
with IRQs masked for the entire block during reclaim?
On protected VM teardown, __pkvm_pgtable_stage2_reclaim() passes the full
mapping->nr_pages (512, 2048, or 8192 pages depending on PAGE_SIZE) to
__pkvm_host_reclaim_guest(), which calls hyp_poison_range() under
host_mmu.lock and vm->lock. Unlike non-protected VM CMOs,
__hyp_poison_page() unconditionally zeroes and flushes the full range to
PoC, and __pkvm_host_force_reclaim_page_guest() additionally holds
vm_table_lock across it.
This stalls host IRQs on the calling CPU and blocks concurrent host and
guest stage-2 transitions across all CPUs for up to 2MB, 32MB, or 512MB
at a time. On 64KB kernels, HAS_FIXBLOCK is disabled, so a single 512MB
reclaim loops 8,192 times issuing per-page broadcast tlbi vale2is
shootdowns while holding host_mmu.lock.
[ ... ]
> + s2vi->pfn = page_to_pfn(s2vi->page);
> +
> + ret = transparent_hugepage_adjust(s2fd, &s2vi->pfn, &s2vi->gfn);
> + if (ret < 0) {
> + unpin_user_page(s2vi->page);
> + return ret;
> + }
> +
> + s2vi->vma_pagesize = ret;
[Severity: High]
Does calling transparent_hugepage_adjust() here return unaligned gfn and
pfn values with PMD_SIZE when CONFIG_PGTABLE_LEVELS == 2?
arch/arm64/kvm/mmu.c:transparent_hugepage_adjust() {
...
gfn &= ~(PTRS_PER_PMD - 1);
*gfnp = gfn;
pfn &= ~(PTRS_PER_PMD - 1);
*pfnp = pfn;
return PMD_SIZE;
...
}
Under 2-level page tables (16KB pages with 36-bit VA, or 64KB pages with
42-bit VA), PTRS_PER_PMD is 1 while PMD_SIZE is 32MB or 512MB, so those
masks are no-ops. pkvm_pgtable_stage2_map() only checks
PAGE_ALIGNED(addr) rather than IS_ALIGNED(addr | phys, size).
On any non-PMD-aligned fault in a THP, the unaligned range either fails
in __guest_check_transition_size() with -EINVAL (aborting KVM_RUN because
protected_pkvm_mem_abort() only retries -E2BIG), or overlaps a mapping in
the next PMD and returns -EAGAIN in an infinite fault loop. Aligning gfn
and pfn to PMD_SIZE / PAGE_SIZE in transparent_hugepage_adjust() and
checking IS_ALIGNED(addr | phys, size) in pkvm_pgtable_stage2_map() would
avoid this.
[ ... ]
> +account:
> + size = s2vi.vma_pagesize;
> + ret = account_locked_vm(mm, size / PAGE_SIZE, true);
> +
> +retry:
> + /* undo transparent_hugepage_adjust() and try again */
> + if (ret && size > PAGE_SIZE) {
> + s2vi.vma_pagesize = PAGE_SIZE;
> + s2vi.gfn = gpa_to_gfn(s2fd->fault_ipa);
> + s2vi.pfn = page_to_pfn(s2vi.page);
> + goto account;
> + }
> +
> + if (ret)
> goto unpin;
> +
> + kvm_fault_lock(kvm);
> + if (mmu_invalidate_retry(kvm, s2vi.mmu_seq))
> + ret = -EAGAIN;
> + else
> + ret = pkvm_pgtable_stage2_map(pgt, gfn_to_gpa(s2vi.gfn), size,
> + __pfn_to_phys(s2vi.pfn), KVM_PGTABLE_PROT_RWX,
> + hyp_memcache, 0);
> + kvm_fault_unlock(kvm);
> +
> + if (ret) {
> + account_locked_vm(mm, size / PAGE_SIZE, false);
> + if (ret == -E2BIG)
> + goto retry;
> }
[Severity: Medium]
Could charging PMD_SIZE / PAGE_SIZE pages before checking
pgt->pkvm_mappings temporarily over-charge mm->locked_vm and churn
mmap_write_lock(mm)?
Donating a single page (or splitting a PMD via
__pkvm_pgtable_stage2_split()) leaves the host stage-1 PMD intact, so
get_user_mapping_size() keeps returning PMD_SIZE for later faults in that
PMD. Each fault charges PMD_SIZE / PAGE_SIZE pages under
mmap_write_lock(mm) while waiting on and holding write_lock(&kvm->mmu_lock),
which can push concurrent faults or VMM mlock() calls over RLIMIT_MEMLOCK.
When pkvm_pgtable_stage2_map() then returns -E2BIG, this drops
kvm_fault_lock(kvm), takes mmap_write_lock(mm) twice more to uncharge all
pages and re-charge one, and re-acquires write_lock(&kvm->mmu_lock) (plus
a fourth mmap_write_lock(mm) if the retry hits -EAGAIN).
Pre-checking pgt->pkvm_mappings before accounting, or retrying at
PAGE_SIZE under the same kvm_fault_lock(kvm) and uncharging the excess
pages once, would avoid the extra lock acquisitions and over-charge.
> diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
> index edc5416bbd8a5..0342f8e93b6df 100644
> --- a/arch/arm64/kvm/pkvm.c
> +++ b/arch/arm64/kvm/pkvm.c
[ ... ]
> + if (mapping && mapping->nr_pages == 1 && size > PAGE_SIZE)
> + return -E2BIG;
[ ... ]
> - ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, 1);
> + ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, size / PAGE_SIZE);
[Severity: Medium]
Should protected_pkvm_mem_abort() also fall back to PAGE_SIZE when
__pkvm_host_donate_guest() returns -EPERM or -EINVAL for a PMD_SIZE
donation?
pkvm_pgtable_stage2_map() only returns -E2BIG for overlaps in the current
VM's pgt->pkvm_mappings tree. If a sibling subpage of a swapbacked THP
(such as memfd) is not PKVM_PAGE_OWNED, __pkvm_host_donate_guest() ->
__host_check_page_state_range() returns -EPERM (or -EINVAL across memblocks).
Because protected_pkvm_mem_abort() only retries on -E2BIG, KVM_RUN now
fails on a host-owned page that succeeded at PAGE_SIZE before this patch.
Retrying at PAGE_SIZE on -EPERM and -EINVAL when size > PAGE_SIZE lets
the faulting page succeed while still failing if it is not host-owned.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=24
^ permalink raw reply [flat|nested] 38+ messages in thread* Re: [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs
2026-12-03 19:28 ` sashiko-bot
@ 2026-12-04 14:50 ` Vincent Donnefort
0 siblings, 0 replies; 38+ messages in thread
From: Vincent Donnefort @ 2026-12-04 14:50 UTC (permalink / raw)
To: sashiko-reviews; +Cc: kvmarm, Oliver Upton, Marc Zyngier
On Thu, Dec 03, 2026 at 07:28:20PM +0000, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 4 potential
> issues to consider:
> - [High] KVM: arm64: unaligned PMD mapping breaks pKVM under 2-level
> pagetables
> - [Medium] KVM: arm64: missing PAGE_SIZE fallback on -EPERM in
> protected_pkvm_mem_abort()
> - [Medium] KVM: arm64: PMD hyp_poison_range() holds EL2 host_mmu.lock
> with IRQs off
> - [Medium] KVM: arm64: locked_vm over-charge and lock churn in
> protected_pkvm_mem_abort()
> --
>
> commit 9c8f56cac08156f554f633ca7346cbaaeadec9a8
> Author: Vincent Donnefort <vdonnefort@google.com>
>
> KVM: arm64: Stage-2 huge mappings for protected VMs
>
> This commit enables PMD_SIZE stage-2 block mappings for protected VMs
> under pKVM when the host stage-1 mapping is backed by a transparent huge
> page. It adjusts protected_pkvm_mem_abort() and pkvm_pgtable_stage2_map()
> to donate PMD_SIZE ranges and fall back to PAGE_SIZE on -E2BIG or locked
> memory accounting failures.
>
> > diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c
> > index 1db10d8d6db73..33cd54cee0e42 100644
> > --- a/arch/arm64/kvm/mmu.c
> > +++ b/arch/arm64/kvm/mmu.c
> > @@ -1489,11 +1489,9 @@ static bool fault_supports_stage2_huge_mapping(const struct kvm_s2_fault_desc *s
> > if (map_size == PAGE_SIZE)
> > return true;
> >
> > - /* pKVM only supports PMD_SIZE huge-mappings for non-protected VMs */
> > - if (is_protected_kvm_enabled()) {
> > - if (vcpu_is_protected(s2fd->vcpu) || map_size != PMD_SIZE)
> > - return false;
> > - }
> > + /* pKVM only supports PMD_SIZE huge-mappings */
> > + if (is_protected_kvm_enabled() && map_size != PMD_SIZE)
> > + return false;
>
> [Severity: Medium]
> Can enabling PMD_SIZE mappings for protected VMs hold host_mmu.lock at EL2
> with IRQs masked for the entire block during reclaim?
>
> On protected VM teardown, __pkvm_pgtable_stage2_reclaim() passes the full
> mapping->nr_pages (512, 2048, or 8192 pages depending on PAGE_SIZE) to
> __pkvm_host_reclaim_guest(), which calls hyp_poison_range() under
> host_mmu.lock and vm->lock. Unlike non-protected VM CMOs,
> __hyp_poison_page() unconditionally zeroes and flushes the full range to
> PoC, and __pkvm_host_force_reclaim_page_guest() additionally holds
> vm_table_lock across it.
>
> This stalls host IRQs on the calling CPU and blocks concurrent host and
> guest stage-2 transitions across all CPUs for up to 2MB, 32MB, or 512MB
> at a time. On 64KB kernels, HAS_FIXBLOCK is disabled, so a single 512MB
> reclaim loops 8,192 times issuing per-page broadcast tlbi vale2is
> shootdowns while holding host_mmu.lock.
This is out of scope. I will make a separate patch to improve locking in the
reclaim path.
>
> [ ... ]
>
> > + s2vi->pfn = page_to_pfn(s2vi->page);
> > +
> > + ret = transparent_hugepage_adjust(s2fd, &s2vi->pfn, &s2vi->gfn);
> > + if (ret < 0) {
> > + unpin_user_page(s2vi->page);
> > + return ret;
> > + }
> > +
> > + s2vi->vma_pagesize = ret;
>
> [Severity: High]
> Does calling transparent_hugepage_adjust() here return unaligned gfn and
> pfn values with PMD_SIZE when CONFIG_PGTABLE_LEVELS == 2?
>
> arch/arm64/kvm/mmu.c:transparent_hugepage_adjust() {
> ...
> gfn &= ~(PTRS_PER_PMD - 1);
> *gfnp = gfn;
> pfn &= ~(PTRS_PER_PMD - 1);
> *pfnp = pfn;
>
> return PMD_SIZE;
> ...
> }
>
> Under 2-level page tables (16KB pages with 36-bit VA, or 64KB pages with
> 42-bit VA), PTRS_PER_PMD is 1 while PMD_SIZE is 32MB or 512MB, so those
> masks are no-ops. pkvm_pgtable_stage2_map() only checks
> PAGE_ALIGNED(addr) rather than IS_ALIGNED(addr | phys, size).
>
> On any non-PMD-aligned fault in a THP, the unaligned range either fails
> in __guest_check_transition_size() with -EINVAL (aborting KVM_RUN because
> protected_pkvm_mem_abort() only retries -E2BIG), or overlaps a mapping in
> the next PMD and returns -EAGAIN in an infinite fault loop. Aligning gfn
> and pfn to PMD_SIZE / PAGE_SIZE in transparent_hugepage_adjust() and
> checking IS_ALIGNED(addr | phys, size) in pkvm_pgtable_stage2_map() would
> avoid this.
>
> [ ... ]
This isn't related to this series, I will make a separate fix.
>
> > +account:
> > + size = s2vi.vma_pagesize;
> > + ret = account_locked_vm(mm, size / PAGE_SIZE, true);
> > +
> > +retry:
> > + /* undo transparent_hugepage_adjust() and try again */
> > + if (ret && size > PAGE_SIZE) {
> > + s2vi.vma_pagesize = PAGE_SIZE;
> > + s2vi.gfn = gpa_to_gfn(s2fd->fault_ipa);
> > + s2vi.pfn = page_to_pfn(s2vi.page);
> > + goto account;
> > + }
> > +
> > + if (ret)
> > goto unpin;
> > +
> > + kvm_fault_lock(kvm);
> > + if (mmu_invalidate_retry(kvm, s2vi.mmu_seq))
> > + ret = -EAGAIN;
> > + else
> > + ret = pkvm_pgtable_stage2_map(pgt, gfn_to_gpa(s2vi.gfn), size,
> > + __pfn_to_phys(s2vi.pfn), KVM_PGTABLE_PROT_RWX,
> > + hyp_memcache, 0);
> > + kvm_fault_unlock(kvm);
> > +
> > + if (ret) {
> > + account_locked_vm(mm, size / PAGE_SIZE, false);
> > + if (ret == -E2BIG)
> > + goto retry;
> > }
>
> [Severity: Medium]
> Could charging PMD_SIZE / PAGE_SIZE pages before checking
> pgt->pkvm_mappings temporarily over-charge mm->locked_vm and churn
> mmap_write_lock(mm)?
>
> Donating a single page (or splitting a PMD via
> __pkvm_pgtable_stage2_split()) leaves the host stage-1 PMD intact, so
> get_user_mapping_size() keeps returning PMD_SIZE for later faults in that
> PMD. Each fault charges PMD_SIZE / PAGE_SIZE pages under
> mmap_write_lock(mm) while waiting on and holding write_lock(&kvm->mmu_lock),
> which can push concurrent faults or VMM mlock() calls over RLIMIT_MEMLOCK.
>
> When pkvm_pgtable_stage2_map() then returns -E2BIG, this drops
> kvm_fault_lock(kvm), takes mmap_write_lock(mm) twice more to uncharge all
> pages and re-charge one, and re-acquires write_lock(&kvm->mmu_lock) (plus
> a fourth mmap_write_lock(mm) if the retry hits -EAGAIN).
>
> Pre-checking pgt->pkvm_mappings before accounting, or retrying at
> PAGE_SIZE under the same kvm_fault_lock(kvm) and uncharging the excess
> pages once, would avoid the extra lock acquisitions and over-charge.
>
This is a retry path that should never be hit unless the VMM has played games
with the mappings. So it doesn't feel like optimizing the number of times we
take the mmap_write_lock() is really important...
> > diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
> > index edc5416bbd8a5..0342f8e93b6df 100644
> > --- a/arch/arm64/kvm/pkvm.c
> > +++ b/arch/arm64/kvm/pkvm.c
>
> [ ... ]
>
> > + if (mapping && mapping->nr_pages == 1 && size > PAGE_SIZE)
> > + return -E2BIG;
>
> [ ... ]
>
> > - ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, 1);
> > + ret = kvm_call_hyp_nvhe(__pkvm_host_donate_guest, pfn, gfn, size / PAGE_SIZE);
>
> [Severity: Medium]
> Should protected_pkvm_mem_abort() also fall back to PAGE_SIZE when
> __pkvm_host_donate_guest() returns -EPERM or -EINVAL for a PMD_SIZE
> donation?
>
> pkvm_pgtable_stage2_map() only returns -E2BIG for overlaps in the current
> VM's pgt->pkvm_mappings tree. If a sibling subpage of a swapbacked THP
> (such as memfd) is not PKVM_PAGE_OWNED, __pkvm_host_donate_guest() ->
> __host_check_page_state_range() returns -EPERM (or -EINVAL across memblocks).
>
> Because protected_pkvm_mem_abort() only retries on -E2BIG, KVM_RUN now
> fails on a host-owned page that succeeded at PAGE_SIZE before this patch.
> Retrying at PAGE_SIZE on -EPERM and -EINVAL when size > PAGE_SIZE lets
> the faulting page succeed while still failing if it is not host-owned.
That would mean two VMs are sharing the same mappings? Is any VMM doing that? It
feels to me for protected VM we'd rather be quite restrictive...
--
Vincent
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261203115209.1690207-1-vdonnefort@google.com?part=24
^ permalink raw reply [flat|nested] 38+ messages in thread
* Re: [PATCH v6 00/24] Huge mapping support for protected VMs
2026-12-03 11:51 [PATCH v6 00/24] Huge mapping support for protected VMs Vincent Donnefort
` (23 preceding siblings ...)
2026-12-03 11:52 ` [PATCH v6 24/24] KVM: arm64: Stage-2 huge mappings for protected VMs Vincent Donnefort
@ 2026-12-03 19:27 ` Fuad Tabba
24 siblings, 0 replies; 38+ messages in thread
From: Fuad Tabba @ 2026-12-03 19:27 UTC (permalink / raw)
To: Vincent Donnefort
Cc: maz, oupton, kvmarm, linux-arm-kernel, joey.gouly, seiden,
suzuki.poulose, yuzenghui, catalin.marinas, will, kernel-team,
qperret, weilin.chang
Hi Vincent,
On Thu, 03 Dec 2026 11:51:45 +0000, Vincent Donnefort
<vdonnefort@google.com> wrote:
> Currently, pKVM handles stage-2 mappings and ownership tracking strictly
> at the PAGE_SIZE level. This series extends pKVM to support PMD_SIZE
> block mappings, thereby enabling Transparent Huge Pages (THP) for
> protected guests. hugetlbfs is currently unsupported as it does not
> appear as swapbacked.
Tested on QEMU with CONFIG_NVHE_EL2_DEBUG, protected and non-protected
guests, including a THP-backed protected guest that gets PMD mappings,
and one with a low RLIMIT_MEMLOCK that falls back to PAGE_SIZE.
For the series:
Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev>
Tested-by: Fuad Tabba <fuad.tabba@linux.dev>
Cheers,
/fuad
^ permalink raw reply [flat|nested] 38+ messages in thread