From nobody Sun Feb 8 15:47:06 2026 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 81F1820FA9D for ; Wed, 16 Apr 2025 16:09:07 +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=1744819749; cv=none; b=m35nh7J37YWvvVIf2eeE884shY9MU7g+3BWUWTAXUOtsQrfDkedwLbUnFovKa8Q4MINNZ7AvqY355atqjJi8tMw3a9Afxn3/tSGmYSgBr9FJYu4BnwY0tf7OUzECPcfbhM/r13FRux4zizk31XxtHHyc24bG6kpx0esNEQrLmg8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744819749; c=relaxed/simple; bh=Phj6Z8bd0z00tKtTpT2UJxiQehIZbjeTZC1Km0GG+JQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=FceX3NL2TvHlH0IGNK8fCfxDix5WPtd5LzzaUwcPgMrPEt9sEUd5GhOv2dUkq83/anuIxXgTBrVstPJchALFvzddBQ1JiCAbjqN1w5ZV4ACMx5YH/E5FbMOS75irZ63OwwVXJo7R08adcTsa4lY8RNj9lUi77IHi+fxMYtvcmIM= 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=xszPPFL7; 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--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="xszPPFL7" Received: by mail-ej1-f73.google.com with SMTP id a640c23a62f3a-ac6ce5fe9bfso709375766b.1 for ; Wed, 16 Apr 2025 09:09:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744819746; x=1745424546; 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=Qk46ikY00j+5+57DCaLJRftXVwdO91Q7aH3vc94jYB4=; b=xszPPFL7gY/Tz3vD+t7ful0kctKp1Jwk9hSAnKDtwraU4wuXmB4lv48prEEZVVhIxx TiZFy5k8n+u+LVOJo3ttKUnvyOyYknJm47Mgl3E04jNDkuG79jPryGg97oPSD192GBDM eY+VcTRhIxpQ8jo2gj35HK1JGNOm5ZzdsroAVi/4Z7Fr+CFrKXtaIdqtJhG+6pJp+a16 R4NAroOi0xJyq1/ZKsYHjTievv1SGJMJ4Vy5qc1nM8opJje1+b7yfqv2j1I8/oCelioZ rNFLEqS0YizjfqTk8e5LK7V7sonBq+8XT76gaBs3Iyrb1KWfoR4luuR5iQedlZ3GnQiY lIUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744819746; x=1745424546; 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=Qk46ikY00j+5+57DCaLJRftXVwdO91Q7aH3vc94jYB4=; b=WKC3uaSMsmoivgcEcu+w6Yhh3kpSFmF8UUp32IyN4jHmTE0nnEJgzRG97k8Lrmvy7a iKqOt2VWzc7AySshBDhSEr9EFNhFB1lR9vsvcYTY/xfm/A761NzEZ6/VnBuNsRQ+tAyJ /WYfYKvM2uZBWDubnfUJtDY8n+ohD+wuIYbPcr0YcUzXh3eTQZl0GF6wBOj55jdaAqDI JxFd7PS+Bs92kLtfAh+RvSyL6KY9E+ua2Gl4Q/sXB1fW1/D2u1TlM2buFJOPOxpPPUsR boy1eNE8h9zXQ8N2No3A1rwRo11u1xzXE6fhHD9d4ihzZ3+VJdL0WfG+PCJlXLXkds3e S27A== X-Forwarded-Encrypted: i=1; AJvYcCWbpB5K/a/gc+1QiDLhr0aKZNMeUZigqxTK7k3HZSyrHqK+p+6Savb2HvcuG0VgCoHQWg3pBfdMm+jNsZ8=@vger.kernel.org X-Gm-Message-State: AOJu0YzLBrgRsSQclAg5EK2vxRwegWx6wnbxAtcxs5EN7ODZE0aCrkmr A/UBedie1SPMOycldzfYElxXpq+NAxPyPLBi1HtwOHcfqGJjnOCl/fzAjaZ2zwZk+J+EXIwV9OW iIk6WiA== X-Google-Smtp-Source: AGHT+IF0Qgk4Z9REtBCKxe6tx7t27J9UAsPYpAOJnWiB4lAJ8d3qRn0XaVBtwgLkIydXHfh5DlnZK0uF0PD6 X-Received: from edcf13.prod.google.com ([2002:a50:a6cd:0:b0:5ec:d8f6:4a7a]) (user=qperret job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:97c5:b0:ac6:f4c1:b797 with SMTP id a640c23a62f3a-acb42987babmr251744266b.19.1744819745708; Wed, 16 Apr 2025 09:09:05 -0700 (PDT) Date: Wed, 16 Apr 2025 16:08:57 +0000 In-Reply-To: <20250416160900.3078417-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: <20250416160900.3078417-1-qperret@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250416160900.3078417-2-qperret@google.com> Subject: [PATCH v3 1/4] KVM: arm64: Add .hyp.data section From: Quentin Perret To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Cc: Quentin Perret , 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" From: David Brazdil The hypervisor has not needed its own .data section because all globals were either .rodata or .bss. To avoid having to initialize future data-structures at run-time, let's introduce add a .data section to the hypervisor. Signed-off-by: David Brazdil Signed-off-by: Quentin Perret --- arch/arm64/include/asm/sections.h | 1 + arch/arm64/kernel/image-vars.h | 2 ++ arch/arm64/kernel/vmlinux.lds.S | 18 +++++++++++++++--- arch/arm64/kvm/arm.c | 7 +++++++ arch/arm64/kvm/hyp/nvhe/hyp.lds.S | 2 ++ arch/arm64/kvm/hyp/nvhe/setup.c | 4 ++++ arch/arm64/kvm/pkvm.c | 1 + 7 files changed, 32 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/sections.h b/arch/arm64/include/asm/sec= tions.h index 40971ac1303f..51b0d594239e 100644 --- a/arch/arm64/include/asm/sections.h +++ b/arch/arm64/include/asm/sections.h @@ -11,6 +11,7 @@ extern char __alt_instructions[], __alt_instructions_end[= ]; extern char __hibernate_exit_text_start[], __hibernate_exit_text_end[]; extern char __hyp_idmap_text_start[], __hyp_idmap_text_end[]; extern char __hyp_text_start[], __hyp_text_end[]; +extern char __hyp_data_start[], __hyp_data_end[]; extern char __hyp_rodata_start[], __hyp_rodata_end[]; extern char __hyp_reloc_begin[], __hyp_reloc_end[]; extern char __hyp_bss_start[], __hyp_bss_end[]; diff --git a/arch/arm64/kernel/image-vars.h b/arch/arm64/kernel/image-vars.h index 5e3c4b58f279..9c5345f1ab66 100644 --- a/arch/arm64/kernel/image-vars.h +++ b/arch/arm64/kernel/image-vars.h @@ -131,6 +131,8 @@ KVM_NVHE_ALIAS(__hyp_text_start); KVM_NVHE_ALIAS(__hyp_text_end); KVM_NVHE_ALIAS(__hyp_bss_start); KVM_NVHE_ALIAS(__hyp_bss_end); +KVM_NVHE_ALIAS(__hyp_data_start); +KVM_NVHE_ALIAS(__hyp_data_end); KVM_NVHE_ALIAS(__hyp_rodata_start); KVM_NVHE_ALIAS(__hyp_rodata_end); =20 diff --git a/arch/arm64/kernel/vmlinux.lds.S b/arch/arm64/kernel/vmlinux.ld= s.S index e73326bd3ff7..7c770053f072 100644 --- a/arch/arm64/kernel/vmlinux.lds.S +++ b/arch/arm64/kernel/vmlinux.lds.S @@ -13,7 +13,7 @@ *(__kvm_ex_table) \ __stop___kvm_ex_table =3D .; =20 -#define HYPERVISOR_DATA_SECTIONS \ +#define HYPERVISOR_RODATA_SECTIONS \ HYP_SECTION_NAME(.rodata) : { \ . =3D ALIGN(PAGE_SIZE); \ __hyp_rodata_start =3D .; \ @@ -23,6 +23,15 @@ __hyp_rodata_end =3D .; \ } =20 +#define HYPERVISOR_DATA_SECTION \ + HYP_SECTION_NAME(.data) : { \ + . =3D ALIGN(PAGE_SIZE); \ + __hyp_data_start =3D .; \ + *(HYP_SECTION_NAME(.data)) \ + . =3D ALIGN(PAGE_SIZE); \ + __hyp_data_end =3D .; \ + } + #define HYPERVISOR_PERCPU_SECTION \ . =3D ALIGN(PAGE_SIZE); \ HYP_SECTION_NAME(.data..percpu) : { \ @@ -51,7 +60,8 @@ #define SBSS_ALIGN PAGE_SIZE #else /* CONFIG_KVM */ #define HYPERVISOR_EXTABLE -#define HYPERVISOR_DATA_SECTIONS +#define HYPERVISOR_RODATA_SECTIONS +#define HYPERVISOR_DATA_SECTION #define HYPERVISOR_PERCPU_SECTION #define HYPERVISOR_RELOC_SECTION #define SBSS_ALIGN 0 @@ -190,7 +200,7 @@ SECTIONS /* everything from this point to __init_begin will be marked RO NX */ RO_DATA(PAGE_SIZE) =20 - HYPERVISOR_DATA_SECTIONS + HYPERVISOR_RODATA_SECTIONS =20 .got : { *(.got) } /* @@ -295,6 +305,8 @@ SECTIONS _sdata =3D .; RW_DATA(L1_CACHE_BYTES, PAGE_SIZE, THREAD_ALIGN) =20 + HYPERVISOR_DATA_SECTION + /* * Data written with the MMU off but read with the MMU on requires * cache lines to be invalidated, discarding up to a Cache Writeback diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 68fec8c95fee..58e8119f66bd 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -2604,6 +2604,13 @@ static int __init init_hyp_mode(void) goto out_err; } =20 + err =3D create_hyp_mappings(kvm_ksym_ref(__hyp_data_start), + kvm_ksym_ref(__hyp_data_end), PAGE_HYP); + if (err) { + kvm_err("Cannot map .hyp.data section\n"); + goto out_err; + } + err =3D create_hyp_mappings(kvm_ksym_ref(__hyp_rodata_start), kvm_ksym_ref(__hyp_rodata_end), PAGE_HYP_RO); if (err) { diff --git a/arch/arm64/kvm/hyp/nvhe/hyp.lds.S b/arch/arm64/kvm/hyp/nvhe/hy= p.lds.S index f4562f417d3f..d724f6d69302 100644 --- a/arch/arm64/kvm/hyp/nvhe/hyp.lds.S +++ b/arch/arm64/kvm/hyp/nvhe/hyp.lds.S @@ -25,5 +25,7 @@ SECTIONS { BEGIN_HYP_SECTION(.data..percpu) PERCPU_INPUT(L1_CACHE_BYTES) END_HYP_SECTION + HYP_SECTION(.bss) + HYP_SECTION(.data) } diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setu= p.c index 955c431af5d0..2050bb5a6de7 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -119,6 +119,10 @@ static int recreate_hyp_mappings(phys_addr_t phys, uns= igned long size, if (ret) return ret; =20 + ret =3D pkvm_create_mappings(__hyp_data_start, __hyp_data_end, PAGE_HYP); + if (ret) + return ret; + ret =3D pkvm_create_mappings(__hyp_rodata_start, __hyp_rodata_end, PAGE_H= YP_RO); if (ret) return ret; diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c index 0f89157d31fd..c462140e640a 100644 --- a/arch/arm64/kvm/pkvm.c +++ b/arch/arm64/kvm/pkvm.c @@ -262,6 +262,7 @@ static int __init finalize_pkvm(void) * at, which would end badly once inaccessible. */ kmemleak_free_part(__hyp_bss_start, __hyp_bss_end - __hyp_bss_start); + kmemleak_free_part(__hyp_data_start, __hyp_data_end - __hyp_data_start); kmemleak_free_part(__hyp_rodata_start, __hyp_rodata_end - __hyp_rodata_st= art); kmemleak_free_part_phys(hyp_mem_base, hyp_mem_size); =20 --=20 2.49.0.604.gff1f9ca942-goog From nobody Sun Feb 8 15:47:06 2026 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) (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 3A6C81EE032 for ; Wed, 16 Apr 2025 16:09:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744819752; cv=none; b=bhTInZ18/LpdNkEOCoAg9ENRjg5IJoSpj3bRNEGmq4Od6p55Veabb5X4NTMLNMuS86BXTuL+y2p0Fg1OrAUcIEKB636mywCB5yjvCzWUTVaho6jsL+8YnJ8ogugCQE8SbGUj2qqd9m6PZQn4p65H2ucu+Z4iCgxQ7bbIRrt2Rxs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744819752; c=relaxed/simple; bh=hhevQAoaSXWNMR7psqWfCUWbj4Q7A3+E702C4036N/o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=mo9OT1NzRXWsvpz70tSUhCdU73Yfqtul3Qx1ycjaoWIf+E2GkoFgL0qt+lwDoI9CL6SYdnASfwkst5oYyU7n3d+nL9g6Jq2ZLGAiCLIU6ySfqqOQAuo3FstWjF/f6wvKznFwb8rtMsYUw7ad9qpV2/IEsF0KToMoaQGxwcQEZVs= 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=YuybVPR7; arc=none smtp.client-ip=209.85.218.74 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="YuybVPR7" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-ac3e0c1336dso570694366b.3 for ; Wed, 16 Apr 2025 09:09:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744819749; x=1745424549; 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=RtCepilI5OBYKPwI8IAZ8i1mwTcn2lnNiKdpddQ34xM=; b=YuybVPR7fkIXM9CDZqWr7cIHSelcdtFZu7d/uuMM3M8R75KK0rEo3IlSOT1cNLXsb/ mhiFzb+bPg0x5b84w64x0Z8Gn+cTawbiqFVrEmAtc25MvH8hp3vw/T9O2XwoBG77gSQJ hHlbjAYttJuFEwOp2Rb8Ho6jpa38EuHJsVB2j4iED4hP61kcDMgxzXHVGBuW/Hn7g1Fd pYu/EXKIktbyyENOuLMasJd6cQqcjquTgTu8l2rQxF5yjy5cd7q0xwbFKUm0EfLvb6dX isDSm3JxbAR4r9obzZ2Xb1Ug8tLK6UW/0dgpbem49IPMduX4cuIBfEJm6vEdcI5R8ubN 3cEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744819749; x=1745424549; 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=RtCepilI5OBYKPwI8IAZ8i1mwTcn2lnNiKdpddQ34xM=; b=N4n/yLHvMosxDTWnCO2fuS2Aw4FsHyGLf2pOxPpxQNKgydfhAqUKOtoPMWItE35kpC sp3PZ0fb9KlyjaaXRB9TRZ9Yb3Zx+FIg5MX4m3RBNVVRG87M0iVkkjmiKU4/BzPoaKC+ eF5bNWS5+STDvfClFRdRJdaoal9GXBqOaD1j1PEcwZKZD37npwDiJQpVcvKrtkHCK9fT zK7rusSXjSA3U+9UKpGmeUd2eQQL3CVPgVrHTKH4lO51Gkhl1K9EJ+3/DVFeS/OjcRCz 9qk7RwKJDRayA6rYPsOgqDmr3MDM3QMXQePfnIwDdYHcEFqaJVHqwCxKuaPLsAmuz3/m V/zg== X-Forwarded-Encrypted: i=1; AJvYcCX3sunqX3IuHTRAJ+9RzJLg+ZRu1oPsVulOSbUkrYQ+3YaHIoEvqeFl6wjnwyTsw9D2L+RxhrkTEV+EnvQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yw8QQVlg9ez2Gn/hl//Tv4kN5FEUbZ90TTYdS9YUQjLTO1Uy/ZD zGXwVYM5VcCjlrIsX+Z0tgwnR9GB7hvkCp2be7N9s80wf453iSL+Q365GFI683I1Pyf5oiGVl/b 17S/8GQ== X-Google-Smtp-Source: AGHT+IE8dk7jeA22VzWx81hj6NCNcV+aNpnoCWIVeIKMbzFyUD7FL3Q4EHx6osWargcZiCuELO7brSsBTvZK X-Received: from ejcl9.prod.google.com ([2002:a17:906:789:b0:aca:9c9f:6358]) (user=qperret job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:7203:b0:ac7:82b3:7ad with SMTP id a640c23a62f3a-acb42b6bb71mr224251566b.46.1744819747999; Wed, 16 Apr 2025 09:09:07 -0700 (PDT) Date: Wed, 16 Apr 2025 16:08:58 +0000 In-Reply-To: <20250416160900.3078417-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: <20250416160900.3078417-1-qperret@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250416160900.3078417-3-qperret@google.com> Subject: [PATCH v3 2/4] KVM: arm64: Don't WARN from __pkvm_host_share_guest() From: Quentin Perret To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Cc: Quentin Perret , 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" We currently WARN() if the host attempts to share a page that is not in an acceptable state with a guest. This isn't strictly necessary and makes testing much harder, so drop the WARN and make sure to propage the error code instead. Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 1 - 1 file changed, 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvh= e/mem_protect.c index 709d286999a1..4a0f98943577 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -921,7 +921,6 @@ int __pkvm_host_share_guest(u64 pfn, u64 gfn, struct pk= vm_hyp_vcpu *vcpu, if (page->host_share_guest_count) break; /* Only host to np-guest multi-sharing is tolerated */ - WARN_ON(1); fallthrough; default: ret =3D -EPERM; --=20 2.49.0.604.gff1f9ca942-goog From nobody Sun Feb 8 15:47:06 2026 Received: from mail-ed1-f74.google.com (mail-ed1-f74.google.com [209.85.208.74]) (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 5AA331DF985 for ; Wed, 16 Apr 2025 16:09:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744819754; cv=none; b=SZcOARF7O3UuQBWLd5BpjTRMmxGIsouoZLQp95H4hVBA/efPLzFsWbidSaLRmbnK1nHzui0GSO9wUv5Bto0i8eUL1YHFDAEi8qZmIarU+pbiSndgsVyTtsV/4m1nPAQSFZ9VZfGHMWJhviOhF+E2RaM6bk7a1tg44zlyte+sdUo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744819754; c=relaxed/simple; bh=f7rKsYLxVEDkcwBjeJizVeYDa7FJpyKUWA8MZrDtyWg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SG1ZfHSOvuFrq1h92FJdZKv4cTpudE3wyiTGfzP4T/VpIzlNwNNBugOBMc7kySvXDie4wLlxoJsWTj4inQCUxJvGfcE8AlNEsPdvA9vDIHjsW3+TTplWk1VUmgP9GAAW5zhaxNIe5xnCg11twcMGrprejLo0GpsmExXSJQUQsco= 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=QzXNbb3v; arc=none smtp.client-ip=209.85.208.74 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="QzXNbb3v" Received: by mail-ed1-f74.google.com with SMTP id 4fb4d7f45d1cf-5e68e9d9270so5717467a12.0 for ; Wed, 16 Apr 2025 09:09:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744819750; x=1745424550; 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=bDcOKwVW00nNGG4NyR6ISobI0N8ZjJsPjdepkGhedWo=; b=QzXNbb3vhLSYVvwEt2iqjYITgWJLI0NXuiffS/QutNekBlvtdmmuI85VWkDVNx4s64 ZHJmhppoHF8UjZLeijPRCrWNQIAsDdYMj8Mmfu0Rd5ZL9x98BWFxdz1Msz+LiNEHsrt4 VIMNnzTCV3aXLp8HnXbH2/iWNSpiLK+5pViGIHcNFppUvZo79Mz1v2/w7nE5S95q9w08 F5k8NZIoldxrGcMFMFmLUZPcXmdym8441J5ODQO0Fs5SjcypcKR/swCY0/BzkZaxwhWa eIjs5pyTc4AjmTXB6ZAA4Vg1OF7Qv8Rv8byNXFSLwySq4Ljau3Lbv1u19ZrdCr1hZUC6 HaTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744819750; x=1745424550; 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=bDcOKwVW00nNGG4NyR6ISobI0N8ZjJsPjdepkGhedWo=; b=akzAMlYUb8kN1U1c6g9UEMVQ0pkpqTvEVsgoTkCOpBc6dEw/m7/i+ksBhIg+SvACZM Pe35whELeWeTzdnXqMBeSDGhNTkjw5NyCvuG5oQv/KAx4SuI0fF5QEut+R1LQqBqrTCn iJBpnS/PHStt7yixC2Kr1mn2/DYjlte5dWD2LUZegRlR3auxL2+Uwy4q3Tot5JXiSjd6 SAAIbmYDZkgCRJc2YL3hhnCwqkYYG63F+gEl+C83F+ZlMrm2wvY/CrGOzYsQgoznx+Ku L2cJ3e52MuhhsEG8HkGb4mTSb00CRATpcfKnFsOWNqXpNy3ElM/Hs83wNDHX02mjDdm6 +FMA== X-Forwarded-Encrypted: i=1; AJvYcCXjPr76oD2fu1AXokr49zcn1Q0X7uaGOCUZQDDTOJQTV9p+V1HZu9FDQ0QIAcC9rDtCWv49cL2n+qrQ+Bc=@vger.kernel.org X-Gm-Message-State: AOJu0YyQ1tWZUTbE1FtbNm7tFLUHPQE0BytNit1nyGHJrZuPie+Wk9qb QLotIkLyU5cYP78DshhS3f0hgZ7IOEcSBdufhZoB+Qg3yQXoeNB59nYyxCCtSQbQZm9mdSDu7os cl7ElWA== X-Google-Smtp-Source: AGHT+IH44M1oUP6au2Aa62Uwe3W7YocYkvgokTJLiLufvbBovB2uSIcZxzrsLQa0fm+cjgs2UKBEThpNgR1E X-Received: from eduf26.prod.google.com ([2002:a05:6402:5a:b0:5ed:19b4:3ce]) (user=qperret job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6402:27ce:b0:5f3:5863:64b5 with SMTP id 4fb4d7f45d1cf-5f4b747b2c6mr2174230a12.12.1744819750663; Wed, 16 Apr 2025 09:09:10 -0700 (PDT) Date: Wed, 16 Apr 2025 16:08:59 +0000 In-Reply-To: <20250416160900.3078417-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: <20250416160900.3078417-1-qperret@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250416160900.3078417-4-qperret@google.com> Subject: [PATCH v3 3/4] KVM: arm64: Selftest for pKVM transitions From: Quentin Perret To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Cc: Quentin Perret , 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" We have recently found a bug [1] in the pKVM memory ownership transitions by code inspection, but it could have been caught with a test. Introduce a boot-time selftest exercising all the known pKVM memory transitions and importantly checks the rejection of illegal transitions. The new test is hidden behind a new Kconfig option separate from CONFIG_EL2_NVHE_DEBUG on purpose as that has side effects on the transition checks ([1] doesn't reproduce with EL2 debug enabled). [1] https://lore.kernel.org/kvmarm/20241128154406.602875-1-qperret@google.c= om/ Suggested-by: Will Deacon Signed-off-by: Quentin Perret --- arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 6 + arch/arm64/kvm/hyp/nvhe/mem_protect.c | 111 ++++++++++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 2 + 3 files changed, 119 insertions(+) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm= /hyp/include/nvhe/mem_protect.h index ea0a704da9b8..535722cd8417 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -67,4 +67,10 @@ static __always_inline void __load_host_stage2(void) else write_sysreg(0, vttbr_el2); } + +#ifdef CONFIG_NVHE_EL2_DEBUG +void pkvm_ownership_selftest(void); +#else +static inline void pkvm_ownership_selftest(void) { } +#endif #endif /* __KVM_NVHE_MEM_PROTECT__ */ diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvh= e/mem_protect.c index 4a0f98943577..1912cc22c987 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -1092,3 +1092,114 @@ int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_= hyp_vcpu *vcpu) =20 return 0; } + +#ifdef CONFIG_NVHE_EL2_DEBUG +struct pkvm_expected_state { + enum pkvm_page_state host; + enum pkvm_page_state hyp; +}; + +static struct pkvm_expected_state selftest_state; +static struct hyp_page *selftest_page; + +static void assert_page_state(void) +{ + void *virt =3D hyp_page_to_virt(selftest_page); + u64 size =3D PAGE_SIZE << selftest_page->order; + u64 phys =3D hyp_virt_to_phys(virt); + + host_lock_component(); + WARN_ON(__host_check_page_state_range(phys, size, selftest_state.host)); + host_unlock_component(); + + hyp_lock_component(); + WARN_ON(__hyp_check_page_state_range(phys, size, selftest_state.hyp)); + hyp_unlock_component(); +} + +#define assert_transition_res(res, fn, ...) \ + do { \ + WARN_ON(fn(__VA_ARGS__) !=3D res); \ + assert_page_state(); \ + } while (0) + +void pkvm_ownership_selftest(void) +{ + void *virt =3D hyp_alloc_pages(&host_s2_pool, 0); + u64 phys, size, pfn; + + WARN_ON(!virt); + selftest_page =3D hyp_virt_to_page(virt); + selftest_page->refcount =3D 0; + + size =3D PAGE_SIZE << selftest_page->order; + phys =3D hyp_virt_to_phys(virt); + pfn =3D hyp_phys_to_pfn(phys); + + selftest_state.host =3D PKVM_NOPAGE; + selftest_state.hyp =3D PKVM_PAGE_OWNED; + assert_page_state(); + assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); + assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); + + selftest_state.host =3D PKVM_PAGE_OWNED; + selftest_state.hyp =3D PKVM_NOPAGE; + assert_transition_res(0, __pkvm_hyp_donate_host, pfn, 1); + assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); + assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); + + selftest_state.host =3D PKVM_PAGE_SHARED_OWNED; + selftest_state.hyp =3D PKVM_PAGE_SHARED_BORROWED; + assert_transition_res(0, __pkvm_host_share_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); + assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + + assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size); + assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size); + hyp_unpin_shared_mem(virt, virt + size); + WARN_ON(hyp_page_count(virt) !=3D 1); + assert_transition_res(-EBUSY, __pkvm_host_unshare_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); + assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + + hyp_unpin_shared_mem(virt, virt + size); + assert_page_state(); + WARN_ON(hyp_page_count(virt)); + + selftest_state.host =3D PKVM_PAGE_OWNED; + selftest_state.hyp =3D PKVM_NOPAGE; + assert_transition_res(0, __pkvm_host_unshare_hyp, pfn); + + selftest_state.host =3D PKVM_PAGE_SHARED_OWNED; + selftest_state.hyp =3D PKVM_NOPAGE; + assert_transition_res(0, __pkvm_host_share_ffa, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); + + selftest_state.host =3D PKVM_PAGE_OWNED; + selftest_state.hyp =3D PKVM_NOPAGE; + assert_transition_res(0, __pkvm_host_unshare_ffa, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); + + selftest_state.host =3D PKVM_NOPAGE; + selftest_state.hyp =3D PKVM_PAGE_OWNED; + assert_transition_res(0, __pkvm_host_donate_hyp, pfn, 1); + + selftest_page->refcount =3D 1; + hyp_put_page(&host_s2_pool, virt); +} +#endif diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setu= p.c index 2050bb5a6de7..47294134cf1b 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -312,6 +312,8 @@ void __noreturn __pkvm_init_finalise(void) goto out; =20 pkvm_hyp_vm_table_init(vm_table_base); + + pkvm_ownership_selftest(); out: /* * We tail-called to here from handle___pkvm_init() and will not return, --=20 2.49.0.604.gff1f9ca942-goog From nobody Sun Feb 8 15:47:06 2026 Received: from mail-ej1-f74.google.com (mail-ej1-f74.google.com [209.85.218.74]) (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 7665B212B31 for ; Wed, 16 Apr 2025 16:09:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744819756; cv=none; b=UGkW3vhshRy5o0pMZTkKHXTyVtZmTI0p1KRrppy5hkEoZzYTIxulSdhc7DxdtIVupEeAl0m/O4K5rCiSeRgfhx3P8asGj7o1+T/w0Kd80/RsBJNTkjZwF0IrOroWcUemvS/bx+DFv16ivNp0jtdG0DUy6r6F1O5VmhmBFLkZkPA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744819756; c=relaxed/simple; bh=fe2nMdzscGwLFIb9lWleR34iODAJvtXU+xcZ8COUBVI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ULrcfbeC570VFz2o0IeYludUWqaOlAvwQePQLHE/jWY03baetCs/x+QUohM+MPFHaYg71KE4el+4zJjCZ36ghDd/hDHhi8wjysVSBzHHB39tPWsnr3oZ7mgDRXsEDpGQJb8S4s7kftNOP9qJZQ0t+ex7bCHlLFebAwDtvG2n1gk= 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=dlBImrea; arc=none smtp.client-ip=209.85.218.74 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="dlBImrea" Received: by mail-ej1-f74.google.com with SMTP id a640c23a62f3a-ac28f255a36so551324766b.3 for ; Wed, 16 Apr 2025 09:09:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1744819753; x=1745424553; 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=o7DwcRfwYwPR3lOoszfjZJQ8USmI21/b/JDlDzaI3Rk=; b=dlBImreaxuxLNDq00Dwa5Herp2TIJ9C1ytHbwCEedVv537bdzNP7A5w0dBZ0hFquC4 TCBqFYMPqinlh1BkCGLTFPaQ1Naxkhx2j/gQe8eF5epaSaPFZjptKSDiVn7GM71bkg8C 7o42oQ+WSSse2g3pfebrxJyfu/TjhjifcB6SE/dkIQSaE3FbH2xVjrle/u4hJKrfG0rh gU3T3SM0X5L0hx00QJ6A+bO6z4zwv8jy5+ANGPj2/BMthrcwEbxk9XAE+cWwg595aDzG H/ikhpnm2X44/zz8RlHq29lx0i7bZdg6o1WGs5xlQSPTaUIGSWR5AQUNl3rjA2neJNo+ gdew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744819753; x=1745424553; 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=o7DwcRfwYwPR3lOoszfjZJQ8USmI21/b/JDlDzaI3Rk=; b=wm7wj4dWIApDc2lv94XTqGfg1Fbs0iyz7Em0qA8QuhM2QBbSwCTG1R40gCz5h5yq+r Kprl6T/BoPEHg5FHP8aOt+TfnfJe+CitrOMrM/qO/MsOwE996Vml2mLsb/D4PQSRE4ib Me7c+qSKwg3rC+1fRUFZre1SEiZh5vQMotuYjrK6j0P4xirS4ukiOafxQjSz4+D0izcv MEHIVa4bVla/q6hpizBudUIvLwMbh52SG7nb91fZUB8f0fjVUNZciln5KhHHi20c14P4 P/TUMolE1TZOfwKLrtSRktjL4zb+pnerhK1dWZXUidBFKasA6F2TieA5Wn65pKvBQB0w Opug== X-Forwarded-Encrypted: i=1; AJvYcCXG3LYuNtKZpS5eVgs9mAUmaYeNLJiB2IJhF9i/YCaQWYmhv8hxRpqktZe0YWZ1PYI9sZoA8AGNqm1S1XI=@vger.kernel.org X-Gm-Message-State: AOJu0YwbKzENtmHV6owqx5FiFERvUeYBP5BM9UoYeER7GdUE/UPZKVM7 zSJ5TdgRBdmObw7biaQAgw/bRvtg+y1JUaehyG30UZ66XUFtzvnto0fv7xsDxEJlKkPWlBWsarG hOZTFiw== X-Google-Smtp-Source: AGHT+IEwA7qflYoiDn9H27dpBiFGe6SdivaSu7w3Um8NapHX8djDr1ytd9Hav23fIHauqobhXm8GW66CUx/Y X-Received: from ejbml21.prod.google.com ([2002:a17:906:cc15:b0:acb:205e:e0ad]) (user=qperret job=prod-delivery.src-stubby-dispatcher) by 2002:a17:907:2da5:b0:ac2:e748:9f1c with SMTP id a640c23a62f3a-acb429e6f1cmr234278666b.33.1744819752696; Wed, 16 Apr 2025 09:09:12 -0700 (PDT) Date: Wed, 16 Apr 2025 16:09:00 +0000 In-Reply-To: <20250416160900.3078417-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: <20250416160900.3078417-1-qperret@google.com> X-Mailer: git-send-email 2.49.0.604.gff1f9ca942-goog Message-ID: <20250416160900.3078417-5-qperret@google.com> Subject: [PATCH v3 4/4] KVM: arm64: Extend pKVM selftest for np-guests From: Quentin Perret To: Marc Zyngier , Oliver Upton , Joey Gouly , Suzuki K Poulose , Zenghui Yu , Catalin Marinas , Will Deacon Cc: Quentin Perret , 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 pKVM selftest intends to test as many memory 'transitions' as possible, so extend it to cover sharing pages with non-protected guests, including in the case of multi-sharing. Signed-off-by: Quentin Perret --- arch/arm64/include/asm/kvm_pkvm.h | 6 ++ arch/arm64/kvm/hyp/include/nvhe/mem_protect.h | 4 +- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 90 ++++++++++++++++++- arch/arm64/kvm/hyp/nvhe/setup.c | 8 +- arch/arm64/kvm/pkvm.c | 1 + 5 files changed, 104 insertions(+), 5 deletions(-) diff --git a/arch/arm64/include/asm/kvm_pkvm.h b/arch/arm64/include/asm/kvm= _pkvm.h index abd693ce5b93..d91bfcf2db56 100644 --- a/arch/arm64/include/asm/kvm_pkvm.h +++ b/arch/arm64/include/asm/kvm_pkvm.h @@ -135,6 +135,12 @@ static inline unsigned long host_s2_pgtable_pages(void) return res; } =20 +#ifdef CONFIG_NVHE_EL2_DEBUG +static inline unsigned long pkvm_selftest_pages(void) { return 32; } +#else +static inline unsigned long pkvm_selftest_pages(void) { return 0; } +#endif + #define KVM_FFA_MBOX_NR_PAGES 1 =20 static inline unsigned long hyp_ffa_proxy_pages(void) diff --git a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h b/arch/arm64/kvm= /hyp/include/nvhe/mem_protect.h index 535722cd8417..26016eb9323f 100644 --- a/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h +++ b/arch/arm64/kvm/hyp/include/nvhe/mem_protect.h @@ -69,8 +69,8 @@ static __always_inline void __load_host_stage2(void) } =20 #ifdef CONFIG_NVHE_EL2_DEBUG -void pkvm_ownership_selftest(void); +void pkvm_ownership_selftest(void *base); #else -static inline void pkvm_ownership_selftest(void) { } +static inline void pkvm_ownership_selftest(void *base) { } #endif #endif /* __KVM_NVHE_MEM_PROTECT__ */ diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvh= e/mem_protect.c index 1912cc22c987..31173c694695 100644 --- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c +++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c @@ -1097,16 +1097,60 @@ int __pkvm_host_mkyoung_guest(u64 gfn, struct pkvm_= hyp_vcpu *vcpu) struct pkvm_expected_state { enum pkvm_page_state host; enum pkvm_page_state hyp; + enum pkvm_page_state guest[2]; /* [ gfn, gfn + 1 ] */ }; =20 static struct pkvm_expected_state selftest_state; static struct hyp_page *selftest_page; =20 +static struct pkvm_hyp_vm selftest_vm =3D { + .kvm =3D { + .arch =3D { + .mmu =3D { + .arch =3D &selftest_vm.kvm.arch, + .pgt =3D &selftest_vm.pgt, + }, + }, + }, +}; + +static struct pkvm_hyp_vcpu selftest_vcpu =3D { + .vcpu =3D { + .arch =3D { + .hw_mmu =3D &selftest_vm.kvm.arch.mmu, + }, + .kvm =3D &selftest_vm.kvm, + }, +}; + +static void init_selftest_vm(void *virt) +{ + struct hyp_page *p =3D hyp_virt_to_page(virt); + int i; + + selftest_vm.kvm.arch.mmu.vtcr =3D host_mmu.arch.mmu.vtcr; + WARN_ON(kvm_guest_prepare_stage2(&selftest_vm, virt)); + + for (i =3D 0; i < pkvm_selftest_pages(); i++) { + if (p[i].refcount) + continue; + p[i].refcount =3D 1; + hyp_put_page(&selftest_vm.pool, hyp_page_to_virt(&p[i])); + } +} + +static u64 selftest_ipa(void) +{ + return BIT(selftest_vm.pgt.ia_bits - 1); +} + static void assert_page_state(void) { void *virt =3D hyp_page_to_virt(selftest_page); u64 size =3D PAGE_SIZE << selftest_page->order; + struct pkvm_hyp_vcpu *vcpu =3D &selftest_vcpu; u64 phys =3D hyp_virt_to_phys(virt); + u64 ipa[2] =3D { selftest_ipa(), selftest_ipa() + PAGE_SIZE }; =20 host_lock_component(); WARN_ON(__host_check_page_state_range(phys, size, selftest_state.host)); @@ -1115,6 +1159,11 @@ static void assert_page_state(void) hyp_lock_component(); WARN_ON(__hyp_check_page_state_range(phys, size, selftest_state.hyp)); hyp_unlock_component(); + + guest_lock_component(&selftest_vm); + WARN_ON(__guest_check_page_state_range(vcpu, ipa[0], size, selftest_state= .guest[0])); + WARN_ON(__guest_check_page_state_range(vcpu, ipa[1], size, selftest_state= .guest[1])); + guest_unlock_component(&selftest_vm); } =20 #define assert_transition_res(res, fn, ...) \ @@ -1123,21 +1172,27 @@ static void assert_page_state(void) assert_page_state(); \ } while (0) =20 -void pkvm_ownership_selftest(void) +void pkvm_ownership_selftest(void *base) { + enum kvm_pgtable_prot prot =3D KVM_PGTABLE_PROT_RWX; void *virt =3D hyp_alloc_pages(&host_s2_pool, 0); - u64 phys, size, pfn; + struct pkvm_hyp_vcpu *vcpu =3D &selftest_vcpu; + struct pkvm_hyp_vm *vm =3D &selftest_vm; + u64 phys, size, pfn, gfn; =20 WARN_ON(!virt); selftest_page =3D hyp_virt_to_page(virt); selftest_page->refcount =3D 0; + init_selftest_vm(base); =20 size =3D PAGE_SIZE << selftest_page->order; phys =3D hyp_virt_to_phys(virt); pfn =3D hyp_phys_to_pfn(phys); + gfn =3D hyp_phys_to_pfn(selftest_ipa()); =20 selftest_state.host =3D PKVM_NOPAGE; selftest_state.hyp =3D PKVM_PAGE_OWNED; + selftest_state.guest[0] =3D selftest_state.guest[1] =3D PKVM_NOPAGE; assert_page_state(); assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); @@ -1145,6 +1200,8 @@ void pkvm_ownership_selftest(void) assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); + assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, vcpu, pr= ot); + assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, vm); =20 selftest_state.host =3D PKVM_PAGE_OWNED; selftest_state.hyp =3D PKVM_NOPAGE; @@ -1152,6 +1209,7 @@ void pkvm_ownership_selftest(void) assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); + assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, vm); assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); =20 selftest_state.host =3D PKVM_PAGE_SHARED_OWNED; @@ -1161,6 +1219,8 @@ void pkvm_ownership_selftest(void) assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, vcpu, pr= ot); + assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, vm); =20 assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size); assert_transition_res(0, hyp_pin_shared_mem, virt, virt + size); @@ -1171,6 +1231,8 @@ void pkvm_ownership_selftest(void) assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, vcpu, pr= ot); + assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, vm); =20 hyp_unpin_shared_mem(virt, virt + size); assert_page_state(); @@ -1188,6 +1250,8 @@ void pkvm_ownership_selftest(void) assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, vcpu, pr= ot); + assert_transition_res(-ENOENT, __pkvm_host_unshare_guest, gfn, vm); assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); =20 selftest_state.host =3D PKVM_PAGE_OWNED; @@ -1195,6 +1259,28 @@ void pkvm_ownership_selftest(void) assert_transition_res(0, __pkvm_host_unshare_ffa, pfn, 1); assert_transition_res(-EPERM, __pkvm_host_unshare_ffa, pfn, 1); =20 + selftest_state.host =3D PKVM_PAGE_SHARED_OWNED; + selftest_state.guest[0] =3D PKVM_PAGE_SHARED_BORROWED; + assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn, vcpu, prot); + assert_transition_res(-EPERM, __pkvm_host_share_guest, pfn, gfn, vcpu, pr= ot); + assert_transition_res(-EPERM, __pkvm_host_share_ffa, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_donate_hyp, pfn, 1); + assert_transition_res(-EPERM, __pkvm_host_share_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_host_unshare_hyp, pfn); + assert_transition_res(-EPERM, __pkvm_hyp_donate_host, pfn, 1); + assert_transition_res(-EPERM, hyp_pin_shared_mem, virt, virt + size); + + selftest_state.guest[1] =3D PKVM_PAGE_SHARED_BORROWED; + assert_transition_res(0, __pkvm_host_share_guest, pfn, gfn + 1, vcpu, pro= t); + WARN_ON(hyp_virt_to_page(virt)->host_share_guest_count !=3D 2); + + selftest_state.guest[0] =3D PKVM_NOPAGE; + assert_transition_res(0, __pkvm_host_unshare_guest, gfn, vm); + + selftest_state.guest[1] =3D PKVM_NOPAGE; + selftest_state.host =3D PKVM_PAGE_OWNED; + assert_transition_res(0, __pkvm_host_unshare_guest, gfn + 1, vm); + selftest_state.host =3D PKVM_NOPAGE; selftest_state.hyp =3D PKVM_PAGE_OWNED; assert_transition_res(0, __pkvm_host_donate_hyp, pfn, 1); diff --git a/arch/arm64/kvm/hyp/nvhe/setup.c b/arch/arm64/kvm/hyp/nvhe/setu= p.c index 47294134cf1b..6d513a4b3763 100644 --- a/arch/arm64/kvm/hyp/nvhe/setup.c +++ b/arch/arm64/kvm/hyp/nvhe/setup.c @@ -28,6 +28,7 @@ static void *vmemmap_base; static void *vm_table_base; static void *hyp_pgt_base; static void *host_s2_pgt_base; +static void *selftest_base; static void *ffa_proxy_pages; static struct kvm_pgtable_mm_ops pkvm_pgtable_mm_ops; static struct hyp_pool hpool; @@ -38,6 +39,11 @@ static int divide_memory_pool(void *virt, unsigned long = size) =20 hyp_early_alloc_init(virt, size); =20 + nr_pages =3D pkvm_selftest_pages(); + selftest_base =3D hyp_early_alloc_contig(nr_pages); + if (nr_pages && !selftest_base) + return -ENOMEM; + nr_pages =3D hyp_vmemmap_pages(sizeof(struct hyp_page)); vmemmap_base =3D hyp_early_alloc_contig(nr_pages); if (!vmemmap_base) @@ -313,7 +319,7 @@ void __noreturn __pkvm_init_finalise(void) =20 pkvm_hyp_vm_table_init(vm_table_base); =20 - pkvm_ownership_selftest(); + pkvm_ownership_selftest(selftest_base); out: /* * We tail-called to here from handle___pkvm_init() and will not return, diff --git a/arch/arm64/kvm/pkvm.c b/arch/arm64/kvm/pkvm.c index c462140e640a..f4761a479280 100644 --- a/arch/arm64/kvm/pkvm.c +++ b/arch/arm64/kvm/pkvm.c @@ -79,6 +79,7 @@ void __init kvm_hyp_reserve(void) hyp_mem_pages +=3D host_s2_pgtable_pages(); hyp_mem_pages +=3D hyp_vm_table_pages(); hyp_mem_pages +=3D hyp_vmemmap_pages(STRUCT_HYP_PAGE_SIZE); + hyp_mem_pages +=3D pkvm_selftest_pages(); hyp_mem_pages +=3D hyp_ffa_proxy_pages(); =20 /* --=20 2.49.0.604.gff1f9ca942-goog