From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) (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 97FFA3A3E81 for ; Fri, 1 May 2026 11:21:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.73 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777634520; cv=none; b=QOg19FAuk1SXKy0/3dy9VwiMF4NwLBjPAORgbv/V8IVtccktR+rkmriHkuAA7qEaicVIwAP7aE0tTGkarEb9uElmxi7JQdf4vapxv3CSj3iN44FQ8uD5I7Ekd7X3G8wlAvB0CR33zVh/KfoHW/6OJLfmi1dm574NtXLwAUoglyE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777634520; c=relaxed/simple; bh=S7p3SvZAGgFaCzOgURZOrBYd5goc2tc8o7A1j2alrrE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aZSBHZI3e2PVe7Csp0/PgJmh0ff9kpn0uvLrSSZh1xLRWA9YCWTzSC3K9F0djWe3oJ1p9KzT1S0J8GONLwH3Ud1sb1n97AZCf0MCZfDrB0JQYsYgX7Rj5Nzm/fcBLAXIYgHZ4wGdb47hmGeI53nqrQakIFXMkVlPvfR+H0bAD1Q= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=GKNBNwlV; arc=none smtp.client-ip=209.85.218.73 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--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="GKNBNwlV" Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-ba78dfab8aaso169713466b.3 for ; Fri, 01 May 2026 04:21:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777634517; x=1778239317; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=hX/U36pu+btUiKMTGpxWXuvgYMFqfHpYvSZt+8MNmUg=; b=GKNBNwlVnskJovghLZWRS1maoQxUAu3c4kwFZfTSiiI5rFxeUjwsNrhotkG1MomwAb Sa3+q0DQnIb0e/W0uOPBj7yrOPhSm+SNkSelQE9hQMpsbEbnDWUoEKJlVQLCfT2NzWku m38jgGxwpqrphfHskS51Uygr4eNgJlKk+nHODeMUPMuBNMwcNWPbIH+tUSRk6dGQ+6mA +L82v1ttQWIQYmnmuzwbJnyhgrlcrZFqsa/fw2WhF4y2JQWSKPKeuvuXBc7OU8GdkFCo rq9oL3zqLOqowYckbLR5LJhQ/Ri7yVWBSPfkS4FE4kOwqYcMQ+M4AH3aIjOPCxQG1rle Y7Kg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777634517; x=1778239317; h=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; bh=hX/U36pu+btUiKMTGpxWXuvgYMFqfHpYvSZt+8MNmUg=; b=jwuGgszCn49Oqw4DajWgXhTBp6Bt9i6DSfumsCHeMSeV8mzpEBEwPYUW879u8+s4Qg MshBlIDEYeuJr3VnS5YbXw7UJaQf9lqLDd/7ua4tx9B0teYLMqcO2DG3nKlTF1BUc4PX y+9IyCm1Hw9Qap9pcUFOXkpOBuAzJSMgEIbD38xLWoylxXvgXinkd4qv70bg90EGP1YI ao4CdtnpunK/idD3GYi4Ofic83WEJnshd7kognh8BZfoj9PTZrNyG57xgSpDLWszWdaB lWLE1oi7piRBm9tsjT1zo9oJSmtYTCREXmJxHFEWQrwtZmzjDWYcAcw4FLRHPslvHGfZ kz8Q== X-Forwarded-Encrypted: i=1; AFNElJ+3FdBjKfTItfyZ5VtCnizLExFL2JjvlwU62OaXK8eCQQFIsX1RL5kqzJ68H8TEfLlKHtPXQlZYVmWtx0g=@vger.kernel.org X-Gm-Message-State: AOJu0Yy6K1gbrzY8L9zBzEmKtrae4v8XkXWftOjPkNXWpDy3BdWk6gnh MdZDN16gMHtIY5U3JLT6gNNEVByRc0kD3mt9Q5ggAcXWLD8HvHKFiZna8HK5sacxj7Hwra/7Ipt y/w== X-Received: from ejbwt14.prod.google.com ([2002:a17:906:ee8e:b0:b98:23e7:c41]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a17:906:f58e:b0:bb7:be6a:7671 with SMTP id a640c23a62f3a-bbac5ac10c5mr381051166b.6.1777634516878; Fri, 01 May 2026 04:21:56 -0700 (PDT) Date: Fri, 1 May 2026 12:21:48 +0100 In-Reply-To: <20260501112149.2824881-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260501112149.2824881-1-tabba@google.com> X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260501112149.2824881-6-tabba@google.com> Subject: [PATCH v2 5/6] KVM: arm64: Pre-check vcpu memcache for host->guest share From: Fuad Tabba To: maz@kernel.org, oliver.upton@linux.dev Cc: james.morse@arm.com, suzuki.poulose@arm.com, yuzenghui@huawei.com, qperret@google.com, vdonnefort@google.com, tabba@google.com, catalin.marinas@arm.com, will@kernel.org, yaoyuan@linux.alibaba.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" __pkvm_host_share_guest() ends with kvm_pgtable_stage2_map() to install the guest stage-2 mapping, after a forward pass that mutates the host vmemmap (sets PKVM_PAGE_SHARED_OWNED and increments host_share_guest_count) for every page in the range. The map's return value is wrapped in WARN_ON() and otherwise discarded, asserting that the call cannot fail. WARN_ON() at nVHE EL2 panics, so this assertion is only correct if the call genuinely cannot fail. kvm_pgtable_stage2_map() can fail with -ENOMEM when the stage-2 walker exhausts the caller's memcache, and the host controls the vcpu memcache via the topup interface, so an under-provisioned share request would otherwise turn a recoverable -ENOMEM into a fatal hyp panic. Bound the worst-case walker allocation in the existing pre-check pass so that kvm_pgtable_stage2_map() cannot fail at the call site, using kvm_mmu_cache_min_pages() -- the same bound host EL1 uses for its own stage-2 maps. If the vcpu memcache holds fewer pages, return -ENOMEM before any state mutation. Fixes: d0bd3e6570ae ("KVM: arm64: Introduce __pkvm_host_share_guest()") Assisted-by: Gemini:gemini-3.1-pro review-prompts Signed-off-by: Fuad Tabba --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c index 28a471d1927c..e428304f94f2 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -1369,6 +1369,22 @@ int __pkvm_host_reclaim_page_guest(u64 gfn, struct pkvm_hyp_vm *vm) return ret && ret != -EHWPOISON ? ret : 0; } +/* + * 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. + */ +static int __guest_check_pgtable_memcache(struct pkvm_hyp_vcpu *vcpu) +{ + struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); + + if (vcpu->vcpu.arch.pkvm_memcache.nr_pages < kvm_mmu_cache_min_pages(vm->pgt.mmu)) + return -ENOMEM; + + return 0; +} + int __pkvm_host_donate_guest(u64 pfn, u64 gfn, struct pkvm_hyp_vcpu *vcpu) { struct pkvm_hyp_vm *vm = pkvm_hyp_vcpu_to_hyp_vm(vcpu); @@ -1453,6 +1469,10 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, u64 nr_pages, struct pkvm_hyp_vcpu } } + ret = __guest_check_pgtable_memcache(vcpu); + if (ret) + goto unlock; + for_each_hyp_page(page, phys, size) { set_host_state(page, PKVM_PAGE_SHARED_OWNED); page->host_share_guest_count++; -- 2.54.0.545.g6539524ca2-goog