From: Vincent Donnefort <vdonnefort@google.com>
To: catalin.marinas@arm.com, will@kernel.org
Cc: mark.rutland@arm.com, linux-arm-kernel@lists.infradead.org,
linux-kernel@vger.kernel.org, kernel-team@android.com,
fuad.tabba@linux.dev, Vincent Donnefort <vdonnefort@google.com>
Subject: [PATCH 3/5] drivers/virt: pkvm: Handle a granule larger than PAGE_SIZE for MEM_SHARE
Date: Wed, 7 Oct 2026 16:02:53 +0100 [thread overview]
Message-ID: <20261007150255.1648849-4-vdonnefort@google.com> (raw)
In-Reply-To: <20261007150255.1648849-1-vdonnefort@google.com>
From: Fuad Tabba <fuad.tabba@linux.dev>
In preparation for allowing protected VMs to run on a system where the
granule is bigger than their PAGE_SIZE, allow the encryption/decryption
callbacks to work with the hypervisor granule increment instead of
PAGE_SIZE.
Reject requests which are not aligned with the granule.
Signed-off-by: Fuad Tabba <fuad.tabba@linux.dev>
Co-developed-by: Vincent Donnefort <vdonnefort@google.com>
Signed-off-by: Vincent Donnefort <vdonnefort@google.com>
---
drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c | 42 +++++++------------
1 file changed, 15 insertions(+), 27 deletions(-)
diff --git a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
index fbcb57c31186..98b1026cf68b 100644
--- a/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
+++ b/drivers/virt/coco/pkvm-guest/arm-pkvm-guest.c
@@ -20,35 +20,19 @@
static size_t __ro_after_init pkvm_granule;
DEFINE_STATIC_KEY_FALSE_RO(pkvm_guest);
-static int arm_smccc_do_one_page(u32 func_id, phys_addr_t phys)
+static int arm_smccc_do_range(u32 func_id, phys_addr_t phys, size_t size)
{
- phys_addr_t end = phys + PAGE_SIZE;
+ phys_addr_t end = phys + size;
- while (phys < end) {
+ if (!IS_ALIGNED(phys | size, pkvm_granule))
+ return -EINVAL;
+
+ for (; phys < end; phys += pkvm_granule) {
struct arm_smccc_res res;
arm_smccc_1_1_invoke(func_id, phys, 0, 0, &res);
if (res.a0 != SMCCC_RET_SUCCESS)
return -EPERM;
-
- phys += pkvm_granule;
- }
-
- return 0;
-}
-
-static int __set_memory_range(u32 func_id, unsigned long start, int numpages)
-{
- void *addr = (void *)start, *end = addr + numpages * PAGE_SIZE;
-
- while (addr < end) {
- int err;
-
- err = arm_smccc_do_one_page(func_id, virt_to_phys(addr));
- if (err)
- return err;
-
- addr += PAGE_SIZE;
}
return 0;
@@ -56,14 +40,16 @@ static int __set_memory_range(u32 func_id, unsigned long start, int numpages)
static int pkvm_set_memory_encrypted(unsigned long addr, int numpages)
{
- return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID,
- addr, numpages);
+ return arm_smccc_do_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_UNSHARE_FUNC_ID,
+ virt_to_phys((void *)addr),
+ numpages * PAGE_SIZE);
}
static int pkvm_set_memory_decrypted(unsigned long addr, int numpages)
{
- return __set_memory_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
- addr, numpages);
+ return arm_smccc_do_range(ARM_SMCCC_VENDOR_HYP_KVM_MEM_SHARE_FUNC_ID,
+ virt_to_phys((void *)addr),
+ numpages * PAGE_SIZE);
}
static const struct arm64_mem_crypt_ops pkvm_crypt_ops = {
@@ -90,7 +76,7 @@ static int mmio_guard_ioremap_hook(phys_addr_t phys, size_t size,
while (phys < end) {
const int func_id = ARM_SMCCC_VENDOR_HYP_KVM_MMIO_GUARD_FUNC_ID;
- WARN_ON_ONCE(arm_smccc_do_one_page(func_id, phys));
+ WARN_ON_ONCE(arm_smccc_do_range(func_id, phys, PAGE_SIZE));
phys += PAGE_SIZE;
}
@@ -118,6 +104,8 @@ void __init pkvm_init_hyp_services(void)
return;
pkvm_granule = res.a0;
+ if (pkvm_granule > PAGE_SIZE)
+ pr_info("pKVM: sharing memory in %zu-byte granules\n", pkvm_granule);
arm64_mem_crypt_ops_register(&pkvm_crypt_ops);
if (kvm_arm_hyp_service_available(ARM_SMCCC_KVM_FUNC_MMIO_GUARD))
--
2.56.0.rc1.315.gc6ed9934b7-goog
next prev parent reply other threads:[~2026-10-07 15:03 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-07 15:02 [PATCH 0/5] drivers/virt: pkvm: Protected VMs with PAGE_SIZE smaller than the hypervisor granule Vincent Donnefort
2026-10-07 15:02 ` [PATCH 1/5] drivers/virt: pkvm: Make pkvm_init_hyp_services() __init Vincent Donnefort
2026-10-07 15:02 ` [PATCH 2/5] drivers/virt: pkvm: Make pkvm_granule __ro_after_init Vincent Donnefort
2026-10-07 15:02 ` Vincent Donnefort [this message]
2026-10-07 15:02 ` [PATCH 4/5] drivers/virt: pkvm: Handle a granule larger than PAGE_SIZE for MMIO_GUARD Vincent Donnefort
2026-10-07 15:02 ` [PATCH 5/5] drivers/virt: pkvm: Allow granule larger than PAGE_SIZE Vincent Donnefort
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261007150255.1648849-4-vdonnefort@google.com \
--to=vdonnefort@google.com \
--cc=catalin.marinas@arm.com \
--cc=fuad.tabba@linux.dev \
--cc=kernel-team@android.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®