From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 694DB435AB2 for ; Fri, 7 Aug 2026 16:43:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.69 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786121025; cv=none; b=Idir2erubm3BDkE/xctI1lVgcv06GD0BX0oi/UIN0GLSHnWE55bERHfjnpC/y1c3JZmMondH+UPetr/2+TUjIFILK35HNkwzGuCkBdDYxR4Pd9r604/QLfy2bsX0S5xX9XNYkpgu1Npc1shD1+rh3UrDiZ+pYVunJ5e4oweV8Kg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786121025; c=relaxed/simple; bh=WoOZLp8mrxOOu0Pc3Lyh8+preRrxVya03MwTvMjg4l4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PJSbsofO5/i0xJFX4XA/lQTzfooa27YEKsW54dh7hCK6dFKp+HkMldt2GNARugRaKWK9e4WdSvakviofTA79z7cXuLl4Lwl4UOjhYC+8gegQ9kZKRoslS/2h0K45R3zK1ozS/8hDEI/+CwgfIsx2RftuBE88iqA8638AypHq9pE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=G/nYVLyv; arc=none smtp.client-ip=209.85.128.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--sebastianene.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="G/nYVLyv" Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-495474a5fbcso31228405e9.1 for ; Fri, 07 Aug 2026 09:43:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786121020; x=1786725820; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=8GYrEvMvff5zvEdtopJDUiIvGuJM4d42FcGqOwQV2a4=; b=G/nYVLyv25Yj9LC/zJr7RQ+34rkCDk/LK2GPXOSoWZt6fUPC1ggXSYDBNccWiOqiGM dB4clBwpcAWAFu1CwZizuRI6K7+4aQHC7E2zvc2ZNZEkGO0Xazo6L4CvcA7beDgbmkfb xfbvF4DiFMC2+Y9+bqIdvmoAy7uSvswkCcWBZJhZd/0Oh3lBapFfeOcBPHqKxzOzWRap 1Vli+Us36bJ1tGVA2+lN3TrXZBVfQpp2QKC1D0eHclyGCedsXUAGbHq6m9CK5ZCPhCjA UQA14ArEmH7+OVR+9u76SxoaGDci1+FZrUqlaxo2PbzBC3C4c6WBC82wkJkKqcXPBM0K EAEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786121020; x=1786725820; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=8GYrEvMvff5zvEdtopJDUiIvGuJM4d42FcGqOwQV2a4=; b=YGrdVQjfD/9Nqxu3dKB5v5zi5dMmOAhqnR8+bFrmKNmuUtbBLOjIhPYE5Fl9J/BA2y f/RYxT7osEwA8+hnh4KrlYORt+obdyyuiD6fygKXbT3hTFW3jN3YSsndua66e0iIQcNX ghr2z9wRc3RmDdlEb3zv4yr/37eC96MyLuwfCZ0+ezfkS9zR3rDX4W0DGDEb0p1GGO5t Oc13O/eiFtcSrnW2AMxX/Eo+1AgS42mJTbsSChk+8DbEwSB+lX0kRwwqUKNM9JDUP2P4 EAp/R2p/hGzuLulNnf8VMjiI2LY0gE6/oiWdwkVNtvx8tX6Sn6iEtFum5x24h62c7GfR kT7A== X-Forwarded-Encrypted: i=1; AHgh+RqVtA6XuG+VMUfvjJPBdTE8fLIr0tFwk8vp7eN3wr/vTSMy1vBnlTS9TVw1QP7h2MKYaIQxk1UntSK6tVE=@vger.kernel.org X-Gm-Message-State: AOJu0Yw3CMMq5MxHR9yJHq5j9yzLPKyp40a1qPCUe9r1hCLgduhDvcAS 9bV3hMVbeddPg2VbGJ1zrHGOY3vrHppdmwTe+16rtYvhkssF2NWpWgHz48WBUrN6mjtTNRsqso/ VWpAyooQ0elq7DCO3+TIohAo0d4IS9g== X-Received: from wmbb14.prod.google.com ([2002:a05:600c:588e:b0:495:4a25:69f7]) (user=sebastianene job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d23:b0:499:51cc:4e57 with SMTP id 5b1f17b1804b1-499617f7699mr13301535e9.0.1786121019899; Fri, 07 Aug 2026 09:43:39 -0700 (PDT) Date: Fri, 7 Aug 2026 16:43:11 +0000 In-Reply-To: <20260807164322.2970811-2-sebastianene@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260807164322.2970811-2-sebastianene@google.com> X-Mailer: git-send-email 2.55.0.654.g21b8a5bc05-goog Message-ID: <20260807164322.2970811-3-sebastianene@google.com> Subject: [PATCH v2 01/13] KVM: arm64: Donate MMIO to the hypervisor From: Sebastian Ene To: catalin.marinas@arm.com, fuad.tabba@linux.dev, joey.gouly@arm.com, mark.rutland@arm.com, maz@kernel.org, oupton@kernel.org, rananta@google.com, Sascha.Bischoff@arm.com, suzuki.poulose@arm.com, will@kernel.org Cc: kvmarm@lists.linux.dev, android-kvm@google.com, bgrzesik@google.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, nathan@kernel.org, perlarsen@google.com, sebastianene@google.com, seiden@linux.ibm.com, smostafa@google.com, tglx@kernel.org, vdonnefort@google.com, vladimir.murzin@arm.com, yuzenghui@huawei.com, zenghui.yu@linux.dev Content-Type: text/plain; charset="UTF-8" From: Mostafa Saleh Extend the pKVM API to allow the donation of MMIO from the host address space to the hypervisor linear map. Initialize the host s2 page table with an invalid leaf with the owner ID of the hypervisor to prevent the host from mapping the page on faults. Prevent kvm_pgtable_stage2_unmap() from removing owner ID from stage-2 PTEs, as this can be triggered from recycle logic under memory pressure. Signed-off-by: Mostafa Saleh Signed-off-by: Sebastian Ene --- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 7 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 137 +++++++++++++++++- arch/arm64/kvm/hyp/pgtable.c | 11 +- 3 files changed, 148 insertions(+), 7 deletions(-) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h index 29935c7da1de..6aa83b129e61 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -36,6 +36,13 @@ 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_donate_hyp(u64 pfn, u64 nr_pages); +/* + * Donate MMIO range to the hypervisor, it will be mapped in the hypervisor's + * linea map and unmapped from the host stage-2. + */ +int __pkvm_host_donate_hyp_mmio(phys_addr_t addr, size_t size); +/* Remaps MMIO range in the host, typically used in error path. */ +int __pkvm_hyp_donate_host_mmio(phys_addr_t addr, size_t size); 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); diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 4e329e39a695..5cf7c4a0ed20 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -378,7 +378,11 @@ static int host_stage2_unmap_dev_all(void) u64 addr = 0; int i, ret; - /* Unmap all non-memory regions to recycle the pages */ + /* + * Unmap all non-memory regions to recycle the pages. + * That relies on kvm_pgtable_stage2_unmap() not clearing + * counted PTEs which include hypervisor MMIO. + */ for (i = 0; i < hyp_memblock_nr; i++, addr = reg->base + reg->size) { reg = &hyp_memory[i]; ret = kvm_pgtable_stage2_unmap(pgt, addr, reg->base - addr); @@ -1119,6 +1123,137 @@ int __pkvm_host_donate_hyp(u64 pfn, u64 nr_pages) return ret; } +int __pkvm_host_donate_hyp_mmio(phys_addr_t addr, size_t size) +{ + kvm_pte_t pte; + u64 offset; + void *virt; + int ret; + + /* Only before de-privilege. */ + if (static_branch_unlikely(&kvm_protected_mode_initialized)) + return -EPERM; + + if (!PAGE_ALIGNED(addr | size) || + !pfn_range_is_valid(hyp_phys_to_pfn(addr), size >> PAGE_SHIFT)) + return -EINVAL; + + host_lock_component(); + hyp_lock_component(); + + for (offset = 0; offset < size; offset += PAGE_SIZE) { + if (addr_is_memory(addr + offset)) { + ret = -EINVAL; + goto err_with_mapping; + } + + ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr + offset, &pte, NULL); + if (ret) + goto err_with_mapping; + + if (pte && !kvm_pte_valid(pte)) { + ret = -EPERM; + goto err_with_mapping; + } + + virt = __hyp_va(addr + offset); + ret = kvm_pgtable_get_leaf(&pkvm_pgtable, (u64)virt, &pte, NULL); + if (ret) + goto err_with_mapping; + if (pte) { + ret = -EBUSY; + goto err_with_mapping; + } + + ret = pkvm_create_mappings_locked(virt, virt + PAGE_SIZE, PAGE_HYP_DEVICE); + if (ret) + goto err_with_mapping; + } + + /* + * We set HYP as the owner of the MMIO pages in the host stage-2, for: + * - host aborts: host_stage2_adjust_range() would fail for invalid non zero PTEs. + * - recycle under memory pressure: host_stage2_unmap_dev_all() would call + * kvm_pgtable_stage2_unmap() which will not clear non zero invalid ptes (counted). + * - other MMIO donation: Would fail as we check that the PTE is valid or empty. + */ + ret = host_stage2_try(kvm_pgtable_stage2_annotate, &host_mmu.pgt, + addr, size, &host_s2_pool, + KVM_HOST_INVALID_PTE_TYPE_DONATION, + FIELD_PREP(KVM_HOST_DONATION_PTE_OWNER_MASK, PKVM_ID_HYP)); + if (ret) + goto err_with_mapping; +unlock: + hyp_unlock_component(); + host_unlock_component(); + return ret; +err_with_mapping: + if (!offset) + goto unlock; + + while (offset) { + offset -= PAGE_SIZE; + virt = __hyp_va(addr + offset); + WARN_ON(kvm_pgtable_hyp_unmap(&pkvm_pgtable, (u64)virt, PAGE_SIZE) != PAGE_SIZE); + } + goto unlock; +} + +int __pkvm_hyp_donate_host_mmio(phys_addr_t addr, size_t size) +{ + kvm_pte_t pte; + u64 offset; + int ret = 0; + void *virt; + + if (static_branch_unlikely(&kvm_protected_mode_initialized)) + return -EPERM; + + if (!PAGE_ALIGNED(addr | size) || + !pfn_range_is_valid(hyp_phys_to_pfn(addr), size >> PAGE_SHIFT)) + return -EINVAL; + + host_lock_component(); + hyp_lock_component(); + + for (offset = 0; offset < size; offset += PAGE_SIZE) { + if (addr_is_memory(addr + offset)) { + ret = -EINVAL; + goto err_with_unmap; + } + ret = kvm_pgtable_get_leaf(&host_mmu.pgt, addr + offset, &pte, NULL); + if (ret) + goto err_with_unmap; + if (!pte || kvm_pte_valid(pte)) { + ret = -EINVAL; + goto err_with_unmap; + } + if (FIELD_GET(KVM_HOST_DONATION_PTE_OWNER_MASK, pte) != PKVM_ID_HYP) { + ret = -EPERM; + goto err_with_unmap; + } + + virt = __hyp_va(addr + offset); + if (kvm_pgtable_hyp_unmap(&pkvm_pgtable, (u64)virt, PAGE_SIZE) != PAGE_SIZE) + goto err_with_unmap; + } + WARN_ON(host_stage2_idmap_locked(addr, size, PKVM_HOST_MMIO_PROT)); +unlock: + hyp_unlock_component(); + host_unlock_component(); + return ret; +err_with_unmap: + if (!offset) + goto unlock; + + while (offset) { + offset -= PAGE_SIZE; + virt = __hyp_va(addr + offset); + WARN_ON(pkvm_create_mappings_locked(virt, virt + PAGE_SIZE, PAGE_HYP_DEVICE)); + } + goto unlock; +} + int __pkvm_hyp_donate_host(u64 pfn, u64 nr_pages) { u64 phys = hyp_pfn_to_phys(pfn); diff --git a/arch/arm64/kvm/hyp/pgtable.c b/arch/arm64/kvm/hyp/pgtable.c index b74dd5ce1efd..7638213bd893 100644 --- a/arch/arm64/kvm/hyp/pgtable.c +++ b/arch/arm64/kvm/hyp/pgtable.c @@ -1161,13 +1161,12 @@ static int stage2_unmap_walker(const struct kvm_pgtable_visit_ctx *ctx, kvm_pte_t *childp = NULL; bool need_flush = false; - if (!kvm_pte_valid(ctx->old)) { - if (stage2_pte_is_counted(ctx->old)) { - kvm_clear_pte(ctx->ptep); - mm_ops->put_page(ctx->ptep); - } + /* + * That also ignores stage2_pte_is_counted() instead of clearing + * the PTE as the MMIO can be owned by the hypervisor. + */ + if (!kvm_pte_valid(ctx->old)) return 0; - } if (kvm_pte_table(ctx->old, ctx->level)) { childp = kvm_pte_follow(ctx->old, mm_ops); -- 2.55.0.654.g21b8a5bc05-goog