From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id AF7D0CD4F3D for ; Wed, 20 May 2026 15:27:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Type:Cc:To:From: Subject:Message-ID:Mime-Version:Date:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=DYdgcm/vWN2I8yVr+k4yh1FcDS21+U9vfHPjdUVDhrc=; b=XKL61VNYiCOF+4GkQdux3zNsf2 NmN9tYDLw2JOwOanua4MtYHNtaXMY01fYrZ8L7ToXmWmSRjn1p4n8KlOyUijUHveuGgPL/2PV9e5X VDbd06jD413hAGydlBGR7sdcSUGxxMtWG3DFkSwyJjU8puAjrWOke1/kcrm5ygkQKBI4mAi++Ay3Y IYy/AGyRFaVrJ3az6bX32iSQrKbsjSFfncYnSIJlRP3Da2lNDeW2XAxiNg/HzsmTM2J/uJrif1EH4 HG+BP9hRnjOj4aIjlIMM/B/cDbDoMR5Ma1SqWrc/ztSp2KZ8MtRvrJ29otfK23oJ7wOYdYS4MJRhf 3S83wfgA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wPipL-00000004z0C-0dXp; Wed, 20 May 2026 15:27:19 +0000 Received: from mail-wm1-x34a.google.com ([2a00:1450:4864:20::34a]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wPipI-00000004yyn-2iGM for linux-arm-kernel@lists.infradead.org; Wed, 20 May 2026 15:27:17 +0000 Received: by mail-wm1-x34a.google.com with SMTP id 5b1f17b1804b1-48fd64c32e8so33300215e9.3 for ; Wed, 20 May 2026 08:27:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779290834; x=1779895634; darn=lists.infradead.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=DYdgcm/vWN2I8yVr+k4yh1FcDS21+U9vfHPjdUVDhrc=; b=i10tf98oYnBqZjIWqxTJV/lh2UUNTpyTFB8fqVTSEZYRVrEpDD+q8B7PnNsRbzCTjY NgYHbsPOJZ+hubiRYlO2oemh3dIPvbsMMOEgEd8BcgnzL2DyXpKxaqaknTyrXF0xWsWM Xa8+2qES007mbNfe0he0UnymmQQN3qkQe1gRirzSnv/pX+qn2VZMAtA8aUB8ct49KJ5e 0zbwx6lYV3lkccDwcWhszkmMXrlRM4RQsSswdKh/ohyWJBNIaDxzK/Rx5D09QJQb9eSo Q1rtLbp+iQRRyVwMswu82bsH2J7gz4eDUuTf4qGAYrB9I5IJQNQAiCiv0PrT+EJFimWp nFVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779290834; x=1779895634; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=DYdgcm/vWN2I8yVr+k4yh1FcDS21+U9vfHPjdUVDhrc=; b=IfEaASPaVC4LM0+QkHDed3U05SmHFfTK7AyuZVk8yNRRY14H99vhFJ7kf1CL8/GfA+ qPqa8Qk8rHYNnAeKMJW5kt1NhvmQdMZX3UlcmrnBuZO83T2SKenzFwb0caOdrXoEFp8L STAhgWc8w/FEcREc79M1SsTfHFFznGGl0Ii7sVXjZ1mX2yWRvQMcFoIXsVZcDAJ/LFQE Fo0macB7zlALLmm3UqjI2KZ4qorT4oSgu/pTka5hDUHb/FBSrpXx4+pjyPcPD6ilRhTm EVIJmWF7mOn9eIVo77oHMkkBAUj6rjugOwDdl+i95DHHnbI0hC47axhwdNLKQa9vhGdQ jpxg== X-Gm-Message-State: AOJu0Yz0yGvAcvHxFPEkiZy5Nu+MB6RSK/iyNtKR0acaeaHkxEf2fK1e p2Ubnq1BazhiPSkY2kKugWQ0RsNdql4R62/JF+zdgoxLLnZNUOwrofSw+c+qv1Zmdg52veiGkqq PXgyoU11S46XECIJ2OkHS6g== X-Received: from wmbjd23.prod.google.com ([2002:a05:600c:68d7:b0:48a:5e23:da86]) (user=vdonnefort job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:885:b0:489:c57:7836 with SMTP id 5b1f17b1804b1-48fe6517fdfmr235377385e9.27.1779290833937; Wed, 20 May 2026 08:27:13 -0700 (PDT) Date: Wed, 20 May 2026 16:26:33 +0100 Mime-Version: 1.0 X-Mailer: git-send-email 2.54.0.631.ge1b05301d1-goog Message-ID: <20260520152650.4107895-1-vdonnefort@google.com> Subject: [PATCH 00/17] KVM: arm64: Introduce pKVM hypervisor heap allocator From: Vincent Donnefort To: maz@kernel.org, oliver.upton@linux.dev, joey.gouly@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, catalin.marinas@arm.com, will@kernel.org Cc: linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kernel-team@android.com, qperret@google.com, tabba@google.com, Vincent Donnefort Content-Type: text/plain; charset="UTF-8" X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260520_082716_710657_726B5102 X-CRM114-Status: GOOD ( 14.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org pKVM historically lacked a dynamic memory allocator: all hypervisor-side VM and VCPU structures had to be sized on the host, allocated as contiguous pages and donated to the hypervisor. This design tightly coupled the hypervisor's memory footprint to host-side constraints, complicated memory reclaim, and severely restricted VM scalability. This patch series introduces a dynamically-mapped custom heap allocator (hyp_allocator) to the pKVM hypervisor. The initial users are the pkvm_hyp_vm and pkvm_hyp_vcpu structs, and the hypervisor tracing metadata. In the near future, this heap allocator is expected to be leveraged to support SVE in protected VMs and in the distant future, it will also support dynamic device assignment. By moving to a hypervisor-managed dynamic allocator, we also allow deduplicating the donation/reclaim path of EL2-private structures. The main building blocks for this series are: 1. pkvm_hyp_req: ---------------- When the hypervisor heap allocator goes out of memory (-ENOMEM), it suspends the hypercall, embeds a PKVM_HYP_REQ_HYP_ALLOC top-up request into the SMCCC HVC return registers, and exits back to the host. This building block will also be useful for the future huge-mapping support in protected guests, allowing EL2 to raise requests such as block splitting back to the host. 2. hyp_allocator: ---------------- This heap allocator manages a reserved VA space range, dynamically mapping and unmapping physical pages on-demand to minimise the pKVM hypervisor footprint. As memory is reclaimed and relinquished to the host, unmapped holes are introduced within the VA space. To prevent orphan mapped regions, neighboring unused chunks cannot be merged if they are separated by an unmapped region. The allocator chunk metadata is stored directly into the VA space range. To minimize metadata overhead, chunks only link to each other via a relative 32-bit offset. A simple hardening of the metadata is added via a simple 32-bit hash. 3. shrinker: ------------ As the heap allocator isn't reclaimed actively on VM or tracing teardown, a shrinker is added to allow the host to reclaim unused memory from the hypervisor when the host is under heavy memory pressure. Vincent Donnefort (17): KVM: arm64: Add __pkvm_private_range_pa KVM: arm64: Add pkvm_remove_mappings KVM: arm64: Add __hyp_allocator_map for the pKVM hyp KVM: arm64: Add a heap allocator for the pKVM hyp KVM: arm64: Allow kvm_hyp_memcache usage outside of stage-2 KVM: arm64: Add topup interface for the pKVM heap allocator KVM: arm64: Add pkvm_hyp_req infrastructure KVM: arm64: Handle PKVM_HYP_REQ_HYP_ALLOC request KVM: arm64: Add reclaim interface for the pKVM heap alloc KVM: arm64: Add selftests for the pKVM heap allocator KVM: arm64: Add a shrinker for pKVM KVM: arm64: Filter out non-kernel addresses in kern_hyp_va KVM: arm64: Move hyp_vm refcount into the structure KVM: arm64: Use noclear for PGD in __pkvm_init_vm error path KVM: arm64: Alloc pkvm_hyp_vm using pKVM heap allocator KVM: arm64: Alloc pkvm_hyp_vcpu using pKVM heap allocator KVM: arm64: Alloc simple_buffer_page using pKVM hyp allocator arch/arm64/include/asm/kvm_asm.h | 4 + arch/arm64/include/asm/kvm_host.h | 14 +- arch/arm64/include/asm/kvm_mmu.h | 3 + arch/arm64/include/asm/kvm_pkvm.h | 104 ++ arch/arm64/kvm/arm.c | 24 + arch/arm64/kvm/hyp/hyp-constants.c | 2 - arch/arm64/kvm/hyp/include/nvhe/alloc.h | 24 + arch/arm64/kvm/hyp/include/nvhe/mm.h | 3 + arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 19 +- arch/arm64/kvm/hyp/nvhe/Makefile | 2 +- arch/arm64/kvm/hyp/nvhe/alloc.c | 1233 +++++++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/hyp-main.c | 123 ++- arch/arm64/kvm/hyp/nvhe/mm.c | 33 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 111 +- arch/arm64/kvm/hyp/nvhe/setup.c | 5 + arch/arm64/kvm/hyp/nvhe/trace.c | 69 +- arch/arm64/kvm/hyp_trace.c | 16 +- arch/arm64/kvm/mmu.c | 4 +- arch/arm64/kvm/pkvm.c | 130 ++- arch/arm64/kvm/trace_pkvm.h | 37 + 20 files changed, 1808 insertions(+), 152 deletions(-) create mode 100644 arch/arm64/kvm/hyp/include/nvhe/alloc.h create mode 100644 arch/arm64/kvm/hyp/nvhe/alloc.c create mode 100644 arch/arm64/kvm/trace_pkvm.h base-commit: 5d6919055dec134de3c40167a490f33c74c12581 -- 2.54.0.631.ge1b05301d1-goog