[PATCH v5 10/18] KVM: arm64: Add a shrinker for pKVM
Fuad Tabba
fuad.tabba at linux.dev
Tue Sep 1 10:30:00 PDT 2026
On Tue, 1 Sept 2026 at 09:10, Vincent Donnefort <vdonnefort at google.com> wrote:
>
> Integrate the pKVM memory reclaim interface with the host's memory
> management subsystem.
>
> This allows the host to automatically recover unused memory fom the
> hypervisor's heap allocator when the host is under memory pressure.
>
> Tested-by: Fuad Tabba <fuad.tabba at linux.dev>
> Signed-off-by: Vincent Donnefort <vdonnefort at google.com>
Reviewed-by: Fuad Tabba <fuad.tabba at linux.dev>
Cheers,
/fuad
>
> diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm_pkvm.h
> index 366bbd5c854b..976fa65522fe 100644
> --- a/arch/arm64/include/asm/kvm_pkvm.h
> +++ b/arch/arm64/include/asm/kvm_pkvm.h
> @@ -19,6 +19,7 @@
>
> enum pkvm_topup_id {
> PKVM_TOPUP_HYP_ALLOC,
> + NR_PKVM_TOPUP_HYP_IDS,
> PKVM_TOPUP_HYP_ALLOC_SELFTEST,
> };
>
> diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c
> index 7821f2592eba..ea96744f41fb 100644
> --- a/arch/arm64/kvm/pkvm.c
> +++ b/arch/arm64/kvm/pkvm.c
> @@ -133,7 +133,7 @@ static unsigned long __pkvm_hyp_reclaim(enum pkvm_topup_id id, unsigned long tar
> return reclaimed;
> }
>
> -static __maybe_unused unsigned long pkvm_hyp_reclaim(enum pkvm_topup_id id, unsigned long target)
> +static unsigned long pkvm_hyp_reclaim(enum pkvm_topup_id id, unsigned long target)
> {
> unsigned long reclaimed = 0;
>
> @@ -154,7 +154,7 @@ static __maybe_unused unsigned long pkvm_hyp_reclaim(enum pkvm_topup_id id, unsi
> return reclaimed;
> }
>
> -static __maybe_unused unsigned long pkvm_hyp_reclaimable(enum pkvm_topup_id id)
> +static unsigned long pkvm_hyp_reclaimable(enum pkvm_topup_id id)
> {
> return kvm_call_hyp_nvhe(__pkvm_hyp_reclaimable, id);
> }
> @@ -360,8 +360,39 @@ void __init pkvm_selftests(void)
> #endif
> }
>
> +static unsigned long pkvm_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)
> +{
> + unsigned long reclaimable = 0;
> + int id;
> +
> + for (id = 0; id < NR_PKVM_TOPUP_HYP_IDS; id++)
> + reclaimable += pkvm_hyp_reclaimable(id);
> +
> + return reclaimable ?: SHRINK_EMPTY;
> +}
> +
> +static unsigned long pkvm_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)
> +{
> + unsigned long reclaimed = 0;
> + int id;
> +
> + sc->nr_scanned = 0;
> +
> + for (id = 0; id < NR_PKVM_TOPUP_HYP_IDS; id++) {
> + unsigned long r = pkvm_hyp_reclaim(id, sc->nr_to_scan - sc->nr_scanned);
> +
> + reclaimed += r;
> + sc->nr_scanned += r;
> + if (sc->nr_scanned >= sc->nr_to_scan)
> + break;
> + }
> +
> + return reclaimed ?: SHRINK_STOP;
> +}
> +
> static int __init finalize_pkvm(void)
> {
> + struct shrinker *pkvm_shrinker;
> int ret;
>
> if (!is_protected_kvm_enabled() || !is_kvm_arm_initialised())
> @@ -377,10 +408,21 @@ static int __init finalize_pkvm(void)
> kmemleak_free_part_phys(hyp_mem_base, hyp_mem_size);
>
> ret = pkvm_drop_host_privileges();
> - if (ret)
> + if (ret) {
> pr_err("Failed to finalize Hyp protection: %d\n", ret);
> + return ret;
> + }
>
> - return ret;
> + pkvm_shrinker = shrinker_alloc(0, "pkvm");
> + if (pkvm_shrinker) {
> + pkvm_shrinker->count_objects = pkvm_shrinker_count;
> + pkvm_shrinker->scan_objects = pkvm_shrinker_scan;
> + shrinker_register(pkvm_shrinker);
> + } else {
> + kvm_err("Failed to register shrinker for pKVM\n");
> + }
> +
> + return 0;
> }
> device_initcall_sync(finalize_pkvm);
>
> --
> 2.55.0.897.gb25b4bd76c-goog
>
More information about the linux-arm-kernel
mailing list