From nobody Sun Feb 8 14:53:06 2026 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.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 3FD78238D35 for ; Fri, 7 Feb 2025 14:54:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738940087; cv=none; b=jMvmC6D77Wbm405uGuWoaxV4kyFqwNZDNgQA6ERUTkZi9AYEA28LupF+gsde30FU05A9+aSHqOYeBjw4Y4wSPle5UU4BIu0A2J66oy7vGfj5XKL615JvF83mbfR2NeS5mS7EVjP3bLZi4JqJ8/NIqbQBajL4AJ2mY2URlLhSzIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738940087; c=relaxed/simple; bh=ZCRFTCY1hi3XMbgWbwzaOADq8bLynRa5HB5rf6QanHA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fl0f5qgmVqdKggwZMya9k6GIOT88/J98eFzutmk6qckjMlpaewqlx526XHlpsBHYAFFYmpiZd9pzx/bQ2MAZDL7qZPxQjw4pYawDqiP6MLsMI9JbrhcRBX6igVkfOM5n3XTroCKLl53RbpsPC+1vz+4Bd/QtYA/kqDvZjZd1sEs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--qperret.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=XCWH+U6n; arc=none smtp.client-ip=209.85.208.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--qperret.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XCWH+U6n" Received: by mail-ed1-f73.google.com with SMTP id 4fb4d7f45d1cf-5de573292e7so103773a12.1 for ; Fri, 07 Feb 2025 06:54:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738940083; x=1739544883; 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=QlxUuvHn55P4VJoa3VPKXet3o2roscTj71cLK/0XsCo=; b=XCWH+U6nLMubKCsEzoQx7/nyy+x16f0+2kISo1G9QCXjSNOr8cMCswcANMxQBBxx1g KqH9pwkngKuJPKDJP/jQOvLG/umj48pVnQ5uodt0ZIW3z4lZVIsclEyf9Ne3mWo58zgE fLngrGq4svFcgY8Z/4rYJbkginJgQV9QQsMvkQUR6oNfwXcf0xduZXK2St+EUxSzCd3m 6rkIz1xogPFSp8GAYbxHY/sYlWkWRJuSz8vBrUuOlgIMJRACZ3bUAKQ+MwnjbbGs/TnO cHRFcDp1m0R2h4t1Fc/ozdkdS0/k4vJgYmnoO9k4cdfSjrGgwt3ReZ15MLocnwlWTxu/ +vhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738940083; x=1739544883; 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=QlxUuvHn55P4VJoa3VPKXet3o2roscTj71cLK/0XsCo=; b=d/xkDVMNniJe049806jmTDW/OSJ92pNlyLXq9LYqi0EhUeeq1hV5uNC0u4cqNOxJH7 mc9zITSNQVmpL2k/nUPv1P5pGnqozLSVFuIIUIG0qMThoTbt7MmgNmPgphc/EZ8nBefy bD1qHRQsL2QwDYD0emX2dYUN/1vN6g2U17j5CrxxWBpHUeHUtEr/ATlcF6Va1V+OyaVh asZuv8Fl14+FhSJwVCd9jU/H1O5g4hyEXJIDb0p8VB/Dh8wpI+tEw9dzLB7XB+UNHpsS 4AX6IFXEWUjpWAHdWcB0VtRD5/cldOHAuGwiYs5fE9Wu4hDk/hbK165ZYaxB14nP8eHA 2z5w== X-Forwarded-Encrypted: i=1; AJvYcCUMn+ZZ95CdMw2yuQKjuzqu2QhSL7eaOeMWExjX8ZX67qQ7oy3eGeDQLZ79/nVo05DCq8zyE/z4X5zK9fs=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+pOC/pfKWKEAQoS/em+/WtkAO+LFqHtBUhXyN+ew3DE/LnBlA 7JgvYKN5bJP1Iq7Eyo6pCDgoCL+OJ1Z1UyZd1ixye48MRYmtfA5RVqg2gdK2GxvfCR7LDAwj9yl 0JtWuNA== X-Google-Smtp-Source: AGHT+IEuPMCv7YqXJlNHwKccMEWSYS/+UUaEr2aDY+SyLUIdyjbcKQ2U586hKi+BCjLaIqvUPKXUd5QYm4sG X-Received: from edax2.prod.google.com ([2002:a05:6402:4142:b0:5dc:110c:e5e2]) (user=qperret job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:2546:b0:5d9:f21a:aa26 with SMTP id 4fb4d7f45d1cf-5de450a9e4cmr3320543a12.24.1738940083729; Fri, 07 Feb 2025 06:54:43 -0800 (PST) Date: Fri, 7 Feb 2025 14:54:37 +0000 In-Reply-To: <20250207145438.1333475-1-qperret@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250207145438.1333475-1-qperret@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250207145438.1333475-2-qperret@google.com> Subject: [PATCH 1/2] KVM: arm64: Improve error handling from check_host_shared_guest() From: Quentin Perret To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Cc: Fuad Tabba , Vincent Donnefort , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The check_host_shared_guest() path expects to find a last-level valid PTE in the guest's stage-2 page-table. However, it checks the PTE's level before its validity, which makes it hard for callers to figure out what went wrong. To make error handling simpler, check the PTE's validity first. Signed-off-by: Quentin Perret Reviewed-by: Oliver Upton --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvh= e/mem_protect.c index 7ad7b133b81a..41847c04b270 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -943,10 +943,10 @@ static int __check_host_shared_guest(struct pkvm_hyp_= vm *vm, u64 *__phys, u64 ip ret =3D kvm_pgtable_get_leaf(&vm->pgt, ipa, &pte, &level); if (ret) return ret; - if (level !=3D KVM_PGTABLE_LAST_LEVEL) - return -E2BIG; if (!kvm_pte_valid(pte)) return -ENOENT; + if (level !=3D KVM_PGTABLE_LAST_LEVEL) + return -E2BIG; =20 state =3D guest_get_page_state(pte, ipa); if (state !=3D PKVM_PAGE_SHARED_BORROWED) --=20 2.48.1.502.g6dc24dfdaf-goog From nobody Sun Feb 8 14:53:06 2026 Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.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 82C0D1EB186 for ; Fri, 7 Feb 2025 14:54:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738940089; cv=none; b=FlV7HaSgtsrD6XK8Rjj24x/PcpA5EN4bWcqehK7D9xuNtIJQelFjoI4YL+vIUH1GbErXoMySPyDu3Q2b3DPB2ZGSuPUv5YGupgpMgAGh/V3fMlRGF1SoXDZoLhhMOi4au1an1VkOGGIT2g4L2Av22jJHL2MBOKvC2V6R9tPR+lc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738940089; c=relaxed/simple; bh=7YwVG7hAXiAQe6oe2H0WofBt3yAK0jzo/OvknGwgC6M=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=auT2qfQgoXSniPLkPdxk0nWrKkozxEGWyBvqSKOnNZiOtH9cRtc32HCKwo+PTk/G7FrjO6yySWGD17GAcNv6BBuJ30jejJhxg6d3T404mxjNoJf797ptGC8PHGqUeqGrkSjRvxr4jiPAl2j0Ev37Ar/a/ZxN1bw46Ab+Im4DN7U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--qperret.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Hi3mVsh0; arc=none smtp.client-ip=209.85.208.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--qperret.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Hi3mVsh0" Received: by mail-ed1-f73.google.com with SMTP id 4fb4d7f45d1cf-5dca72b752fso2420390a12.0 for ; Fri, 07 Feb 2025 06:54:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1738940086; x=1739544886; 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=EVbHfeOZZeQwuwJHSY7djFz56cmMNDUZpBf4HXbLLoc=; b=Hi3mVsh0LHkS2s+IEsd8GkDOkiSu7nsxf0cFvs42g1rh4uprA+7/cqGmt8epD4iERp +CQJ+YljRKkE3JFQ5m1YuOUKiQkfx+Ubu51QlfwA7dv3RVOlVf/sJkIKwzlOZFCsq8q0 KL7Cn8Hgfu5mxWHQcbq35J2nZE502XxI1W7LkrN7sjSiOWQAF7Hzu+wAGf88yqZM3tZF XNXu7AQGERtZykCvg2qMuFDObGMrgi2MgJsszA3rDUOCRIdK7AYZ1kBnpV/Kj2TvVfvq yWHeUUEzRMUrLThTDBiluL0i62pbABclQoxsvc11FvsbGHxxpSTGG7YisYf2eyljSPI1 DBHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738940086; x=1739544886; 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=EVbHfeOZZeQwuwJHSY7djFz56cmMNDUZpBf4HXbLLoc=; b=D7vVZVjQkeMzy28+x7dDrtP0shxVTdoTcBefOLgLXIwE37fxI6rZQ9up/IHQt4cqPl RjrJppBlybw+Fa3xTwr+xQSD8Vfh1EADMQ+2OL0dONDFAJESPnxzPaCpJ8IPT6oB89jg 0/jBrVWYHUjLbETbF4DVtfjcFd6INF9+KuTEpNP9Y84i0xBnjD7ZvlQ55J13BhCzS8Zm NLnNlsc9P4hzAyllDKO8yTZlZOsmuC6hWZJV1F8cwoXOxgW8SiL5zIdeu5/Sva/IdHTM j/hJOnG8B1z0ZefdMxySsugTh+nXfvrw4WP9KMW01ihMrTeXqwZj1L4c6OMMxMu828aE dwTg== X-Forwarded-Encrypted: i=1; AJvYcCWgVpOyJnmA30WklZDaG9k+QlH0Nffed2wOQi8XbgBSj5Y6tJkslWBwZotbEQS+LPnJsPM9aemIqmEaQ9Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yy/xQs6poWAKd2MWhvbQ7En3EAyXsk1nAR6Wuv6Iv3uuMWIUYNF h8lXGjNhKQfazIRA7KqMjd8I4Ine0bBhlLkvQKnH1jV6ji75ShPIPwXyVH5bY1IjbfhAtid2hPF o+E2oSQ== X-Google-Smtp-Source: AGHT+IHcADnZ88ZDBPcCM/6Viy+F4eISrGE+uNYHO/bQ+LOgCwyU9X5ytoU17kork4MbEbCQlQqyCizNz+Zl X-Received: from edbio9.prod.google.com ([2002:a05:6402:2189:b0:5dc:22e2:2325]) (user=qperret job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:35c6:b0:5de:5263:ae79 with SMTP id 4fb4d7f45d1cf-5de5263af0bmr1071447a12.12.1738940085850; Fri, 07 Feb 2025 06:54:45 -0800 (PST) Date: Fri, 7 Feb 2025 14:54:38 +0000 In-Reply-To: <20250207145438.1333475-1-qperret@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250207145438.1333475-1-qperret@google.com> X-Mailer: git-send-email 2.48.1.502.g6dc24dfdaf-goog Message-ID: <20250207145438.1333475-3-qperret@google.com> Subject: [PATCH 2/2] KVM: arm64: Simplify np-guest hypercalls From: Quentin Perret To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Cc: Fuad Tabba , Vincent Donnefort , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When the handling of a guest stage-2 permission fault races with an MMU notifier, the faulting page might be gone from the guest's stage-2 by the point we attempt to call (p)kvm_pgtable_stage2_relax_perms(). In the normal KVM case, this leads to returning -EAGAIN which user_mem_abort() handles correctly by simply re-entering the guest. However, the pKVM hypercall implementation has additional logic to check the page state using __check_host_shared_guest() which gets confused with absence of a page mapped at the requested IPA and returns -ENOENT, hence breaking user_mem_abort() and hilarity ensues. Luckily, several of the hypercalls for managing the stage-2 page-table of NP guests have no effect on the pKVM ownership tracking (wrprotect, test_clear_young, mkyoung, and crucially relax_perms), so the extra state checking logic is in fact not strictly necessary. So, to fix the discrepancy between standard KVM and pKVM, let's just drop the superfluous __check_host_shared_guest() logic from those hypercalls and make the extra state checking a debug assertion dependent on CONFIG_NVHE_EL2_DEBUG as we already do for other transitions. Signed-off-by: Quentin Perret Reviewed-by: Oliver Upton --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 69 +++++++++++++++------------ 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvh= e/mem_protect.c index 41847c04b270..4c2f6a6a2efe 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -998,63 +998,73 @@ int __pkvm_host_unshare_guest(u64 gfn, struct pkvm_hy= p_vm *vm) return ret; } =20 -int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enu= m kvm_pgtable_prot prot) +static void assert_host_shared_guest(struct pkvm_hyp_vm *vm, u64 ipa) { - struct pkvm_hyp_vm *vm =3D pkvm_hyp_vcpu_to_hyp_vm(vcpu); - u64 ipa =3D hyp_pfn_to_phys(gfn); u64 phys; int ret; =20 - if (prot & ~KVM_PGTABLE_PROT_RWX) - return -EINVAL; + if (!IS_ENABLED(CONFIG_NVHE_EL2_DEBUG)) + return; =20 host_lock_component(); guest_lock_component(vm); =20 ret =3D __check_host_shared_guest(vm, &phys, ipa); - if (!ret) - ret =3D kvm_pgtable_stage2_relax_perms(&vm->pgt, ipa, prot, 0); =20 guest_unlock_component(vm); host_unlock_component(); =20 - return ret; + WARN_ON(ret && ret !=3D -ENOENT); } =20 -int __pkvm_host_wrprotect_guest(u64 gfn, struct pkvm_hyp_vm *vm) +int __pkvm_host_relax_perms_guest(u64 gfn, struct pkvm_hyp_vcpu *vcpu, enu= m kvm_pgtable_prot prot) { + struct pkvm_hyp_vm *vm =3D pkvm_hyp_vcpu_to_hyp_vm(vcpu); u64 ipa =3D hyp_pfn_to_phys(gfn); - u64 phys; int ret; =20 - host_lock_component(); - guest_lock_component(vm); + if (pkvm_hyp_vm_is_protected(vm)) + return -EPERM; =20 - ret =3D __check_host_shared_guest(vm, &phys, ipa); - if (!ret) - ret =3D kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, PAGE_SIZE); + if (prot & ~KVM_PGTABLE_PROT_RWX) + return -EINVAL; =20 + assert_host_shared_guest(vm, ipa); + guest_lock_component(vm); + ret =3D kvm_pgtable_stage2_relax_perms(&vm->pgt, ipa, prot, 0); guest_unlock_component(vm); - host_unlock_component(); =20 return ret; } =20 -int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hy= p_vm *vm) +int __pkvm_host_wrprotect_guest(u64 gfn, struct pkvm_hyp_vm *vm) { u64 ipa =3D hyp_pfn_to_phys(gfn); - u64 phys; int ret; =20 - host_lock_component(); + if (pkvm_hyp_vm_is_protected(vm)) + return -EPERM; + + assert_host_shared_guest(vm, ipa); guest_lock_component(vm); + ret =3D kvm_pgtable_stage2_wrprotect(&vm->pgt, ipa, PAGE_SIZE); + guest_unlock_component(vm); =20 - ret =3D __check_host_shared_guest(vm, &phys, ipa); - if (!ret) - ret =3D kvm_pgtable_stage2_test_clear_young(&vm->pgt, ipa, PAGE_SIZE, mk= old); + return ret; +} =20 +int __pkvm_host_test_clear_young_guest(u64 gfn, bool mkold, struct pkvm_hy= p_vm *vm) +{ + u64 ipa =3D hyp_pfn_to_phys(gfn); + int ret; + + if (pkvm_hyp_vm_is_protected(vm)) + return -EPERM; + + assert_host_shared_guest(vm, ipa); + guest_lock_component(vm); + ret =3D kvm_pgtable_stage2_test_clear_young(&vm->pgt, ipa, PAGE_SIZE, mko= ld); guest_unlock_component(vm); - host_unlock_component(); =20 return ret; } @@ -1063,18 +1073,15 @@ int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_= hyp_vcpu *vcpu) { struct pkvm_hyp_vm *vm =3D pkvm_hyp_vcpu_to_hyp_vm(vcpu); u64 ipa =3D hyp_pfn_to_phys(gfn); - u64 phys; int ret; =20 - host_lock_component(); - guest_lock_component(vm); - - ret =3D __check_host_shared_guest(vm, &phys, ipa); - if (!ret) - kvm_pgtable_stage2_mkyoung(&vm->pgt, ipa, 0); + if (pkvm_hyp_vm_is_protected(vm)) + return -EPERM; =20 + assert_host_shared_guest(vm, ipa); + guest_lock_component(vm); + kvm_pgtable_stage2_mkyoung(&vm->pgt, ipa, 0); guest_unlock_component(vm); - host_unlock_component(); =20 return ret; } --=20 2.48.1.502.g6dc24dfdaf-goog