KVM: arm64: Support FFA_MEM_RECLAIM2 in host handler
Subsystems (3)
v3 cover KVM: arm64: Support FFA_MEM_RECLAIM2 in host handler
The Arm Firmware Framework for Arm A-profile (FF-A) v1.3 BET0 specification (DEN0140 rev K) [1] introduces the optional FFA_MEM_RECLAIM2 (0xC4000091) ABI.
Reclaiming large memory regions with the existing FFA_MEM_RECLAIM ABI can take significant time, or relies on a stateful time-slicing mechanism in the Relayer. FFA_MEM_RECLAIM2 allows an Owner to reclaim exclusive access to a subset of pages from a shared or lent memory region by passing an array of constituent memory region descriptors in its TX buffer along with a starting descriptor byte offset and initial page index. The Relayer returns the number of 4K pages reclaimed in that invocation without maintaining intermediate transaction state.
On a Pixel board (pKVM, 16K pages, EL3 SPMC), reclaiming a single 16K range drops from 1.3us to 0.7us (~45% faster) by avoiding FFA_MEM_RETRIEVE_REQ/RX_RELEASE (~0.6us), and single-call FFA_MEM_RECLAIM2 stays faster across small range counts (1.3us vs 1.9us at 16 ranges; 2 calls break even). Up to 64MiB (4096 ranges), batching 256 descriptors/call adds 15 preemption points, capping each EL2/EL3 call at ~11us while keeping total SMC time within 5%.
This series adds the FFA_MEM_RECLAIM2 function ID definition and implements host proxy support in the pKVM nVHE FF-A handler, unsharing the reclaimed pages in the host stage-2 page tables upon a successful reclaim response from EL3/SPMD.
With the FF-A v1.3 specification now at BETA (1.3 BET0), drop the RFC tag so the series can be considered for merging.
This series is based on v7.4-rc1.
Changes in v3:
- Drop RFC tag now that FF-A v1.3 BET0 (DEN0140 rev K) is released
- Update specification references and links to DEN0140 rev K (1.3 BET0)
- Add benchmark numbers to the cover letter and Patch 2 commit message
- Collect Vincent's Reviewed-by on Patch 2
- Rebase on v7.4-rc1
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Mark as RFC until the FF-A v1.3 specification reaches BETA (Sudeep)
- Rename FFA_FN64_MEM_RECLAIM2 to FFA_MEM_RECLAIM2 (sashiko)
- Check total_pages against the sum of constituent pg_cnt (Vincent)
- Simplify the flags and offset alignment checks (Vincent)
- Rebase on latest kvmarm/next
- Link to v1: https://lore.kernel.org/r/[email protected]
[1] https://support.arm.com/documentation/den0140/k
Snehal Koukuntla (2):
firmware: arm_ffa: Add FFA_MEM_RECLAIM2 function ID
KVM: arm64: Support FFA_MEM_RECLAIM2 in host handler
arch/arm64/kvm/hyp/nvhe/ffa.c | 126 ++++++++++++++++++++++++++++++++++ include/linux/arm_ffa.h | 1 + 2 files changed, 127 insertions(+)
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.56.0.317.g454f8302e1-goog
PATCH v3 1/2 firmware: arm_ffa: Add FFA_MEM_RECLAIM2 function ID
FF-A v1.3 BET0 (DEN0140 rev K) introduces the optional FFA_MEM_RECLAIM2 (0xC4000091) ABI, which enables an Owner to reclaim exclusive access to a subset of pages of a memory region without requiring the Relayer to maintain intermediate transaction state.
Add the FFA_MEM_RECLAIM2 function ID definition to arm_ffa.h.
---
Changes in v3:
- Drop RFC tag and update specification reference to FF-A v1.3 BET0 (DEN0140 rev K)
Changes in v2:
- Rename FFA_FN64_MEM_RECLAIM2 to FFA_MEM_RECLAIM2, following the existing convention for 64-bit-only ABIs (sashiko)
include/linux/arm_ffa.h | 1 + 1 file changed, 1 insertion(+)
include/linux/arm_ffa.h +1 -0
@@ -78,6 +78,7 @@#define FFA_EL3_INTR_HANDLE FFA_SMC_32(0x8C)#define FFA_MSG_SEND_DIRECT_REQ2 FFA_SMC_64(0x8D)#define FFA_MSG_SEND_DIRECT_RESP2 FFA_SMC_64(0x8E) +#define FFA_MEM_RECLAIM2 FFA_SMC_64(0x91)/* * For some calls it is necessary to use SMC64 to pass or return 64-bit values.
Signature · 1 line
2.56.0.317.g454f8302e1-goog
PATCH v3 2/2 KVM: arm64: Support FFA_MEM_RECLAIM2 in host handler
In reply to Snehal KoukuntlaFF-A v1.3 BET0 (DEN0140 rev K) adds the FFA_MEM_RECLAIM2 interface, allowing an Owner to reclaim a subset of pages from a shared or lent memory region by populating constituent memory region descriptors in its TX buffer along with a starting descriptor byte offset and initial page index. Upon completion, the Relayer returns the number of 4K pages reclaimed in x3.
Support FFA_MEM_RECLAIM2 in the host FF-A proxy handler. Validate the register arguments, copy the constituent descriptors from the host TX buffer into the hypervisor's TX buffer, and check the descriptor bounds, the total page count and the initial page index on that copy before forwarding the call to the SPMD. On success, unshare the reclaimed page ranges from the host stage-2 page tables.
On a Pixel board (pKVM, 16K pages, EL3 SPMC), reclaiming a single 16K range drops from 1.3us to 0.7us (~45% faster) by avoiding FFA_MEM_RETRIEVE_REQ/RX_RELEASE (~0.6us), and single-call FFA_MEM_RECLAIM2 stays faster across small range counts (1.3us vs 1.9us at 16 ranges; 2 calls break even). Up to 64MiB (4096 ranges), batching 256 descriptors/call adds 15 preemption points, capping each EL2/EL3 call at ~11us while keeping total SMC time within 5%.
---
Changes in v3:
- Drop RFC tag and update specification reference to FF-A v1.3 BET0 (DEN0140 rev K)
- Add benchmark numbers to commit description
- Add Vincent's Reviewed-by tag (no code changes)
Changes in v2:
- Rename FFA_FN64_MEM_RECLAIM2 to FFA_MEM_RECLAIM2 (sashiko)
- Check that the sum of pg_cnt across all constituent descriptors matches total_pages, guarding against overflow (Vincent)
- Simplify the flags and offset alignment checks (Vincent)
arch/arm64/kvm/hyp/nvhe/ffa.c | 126 ++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+)
arch/arm64/kvm/hyp/nvhe/ffa.c +126 -0
@@ -232,6 +232,19 @@ static void ffa_mem_reclaim(struct arm_smccc_1_2_regs *res, u32 handle_lo, }, res); } +static void ffa_mem_reclaim2 (struct arm_smccc_1_2_regs *res,u64 handle, +u64 flags,u64 x3,u64 x4,u64 x5) +{ +hyp_smccc_1_2_smc (&(struct arm_smccc_1_2_regs ) { + .a0 =FFA_MEM_RECLAIM2 , + .a1 = handle, + .a2 = flags, + .a3 = x3, + .a4 = x4, + .a5 = x5, + }, res); +} +static void ffa_retrieve_req (struct arm_smccc_1_2_regs *res,u32 len) { hyp_smccc_1_2_smc(&(struct arm_smccc_1_2_regs) {
@@ -611,6 +624,116 @@ static void __do_ffa_mem_xfer(const u64 func_id,__do_ffa_mem_xfer ((fid), (res), (ctxt)); \ }while (0 ); +static void do_ffa_mem_reclaim2 (struct arm_smccc_1_2_regs *res, +struct kvm_cpu_context *ctxt) +{ +DECLARE_REG (u64, handle, ctxt,1 ); +DECLARE_REG (u64, flags, ctxt,2 ); +DECLARE_REG (u64, x3, ctxt,3 ); +DECLARE_REG (u64, x4, ctxt,4 ); +DECLARE_REG (u64, x5, ctxt,5 ); +u32 req_desc_cnt = (u32)(x3 >>32 ); +u32 total_pages = (u32 )x3; +u32 desc_size = (u32 )x4; +u32 offset = (u32 )x5; +u32 page_index = (u32)(x5 >>32 ); +struct ffa_mem_region_addr_range *range; +u32 declared_pages =0 ; +u32 reclaimed_pages; +u32 reclaimed_count =0 ; +u32 in_idx; +int ret =0 ; + +if (ffa_check_unused_args_sbz (ctxt,6 ) || flags >1 || (x4 >>32 )) { +ffa_to_smccc_res (res,FFA_RET_INVALID_PARAMETERS ); +return ; + } + +if (!req_desc_cnt || !total_pages) { +ffa_to_smccc_res (res,FFA_RET_INVALID_PARAMETERS ); +return ; + } + +if (desc_size !=sizeof (struct ffa_mem_region_addr_range )) { +ffa_to_smccc_res (res,FFA_RET_INVALID_PARAMETERS ); +return ; + } + +if ((u64 )req_desc_cnt * desc_size >KVM_FFA_MBOX_NR_PAGES *PAGE_SIZE ) { +ffa_to_smccc_res (res,FFA_RET_INVALID_PARAMETERS ); +return ; + } + +if (offset % desc_size) { +ffa_to_smccc_res (res,FFA_RET_INVALID_PARAMETERS ); +return ; + } + +if (offset >= (u64 )req_desc_cnt * desc_size) { +ffa_to_smccc_res (res,FFA_RET_INVALID_PARAMETERS ); +return ; + } + +hyp_spin_lock (&host_buffers.lock); +if (!host_buffers.tx) { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out_unlock; + } + +memcpy (hyp_buffers.tx, host_buffers.tx, (u64 )req_desc_cnt * desc_size); + +for (in_idx =0 ; in_idx < req_desc_cnt; in_idx++) { + range = (void *)hyp_buffers.tx + in_idx * desc_size; +if (check_add_overflow (declared_pages, range->pg_cnt, &declared_pages)) { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out_unlock; + } + } + + range = (void *)hyp_buffers.tx + offset; +if (declared_pages != total_pages || page_index >= range->pg_cnt) { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out_unlock; + } + +ffa_mem_reclaim2 (res, handle, flags, x3, x4, x5); +if (res->a0 !=FFA_SUCCESS ) + goto out_unlock; + + reclaimed_pages = res->a3; + +for (in_idx = offset / desc_size; in_idx < req_desc_cnt; in_idx++) { +struct ffa_mem_region_addr_range temp; +u32 pg_cnt, to_reclaim; + +if (reclaimed_count >= reclaimed_pages) +break ; + + range = (void *)hyp_buffers.tx + in_idx * desc_size; +if (!range->pg_cnt) +continue ; + + pg_cnt = range->pg_cnt - page_index; + to_reclaim =min (pg_cnt, reclaimed_pages - reclaimed_count); + + temp = (struct ffa_mem_region_addr_range ) { + .address = range->address + (u64 )page_index *FFA_PAGE_SIZE , + .pg_cnt = to_reclaim, + }; + +WARN_ON (ffa_host_unshare_ranges (&temp,1 )); + + reclaimed_count += to_reclaim; + page_index =0 ; + } + +out_unlock: +hyp_spin_unlock (&host_buffers.lock); + +if (ret) +ffa_to_smccc_res (res, ret); +} +static void do_ffa_mem_reclaim (struct arm_smccc_1_2_regs *res,struct kvm_cpu_context *ctxt) {
@@ -1107,6 +1230,9 @@ bool kvm_host_ffa_handler(struct kvm_cpu_context *host_ctxt, u32 func_id)case FFA_MEM_RECLAIM :do_ffa_mem_reclaim (&res, host_ctxt); goto out_handled; +case FFA_MEM_RECLAIM2 : +do_ffa_mem_reclaim2 (&res, host_ctxt); + goto out_handled;case FFA_MEM_LEND :case FFA_FN64_MEM_LEND :do_ffa_mem_xfer (FFA_FN64_MEM_LEND , &res, host_ctxt);
Signature · 1 line
2.56.0.317.g454f8302e1-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] KVM: arm64: missing PAGE_SIZE alignment check in do_ffa_mem_reclaim2() --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] KVM: arm64: missing PAGE_SIZE alignment check in do_ffa_mem_reclaim2() --
commit 98f8a09e41b961093f58b8b2216332b0f1fd7346
Author: Snehal Koukuntla <[email protected]>
KVM: arm64: Support FFA_MEM_RECLAIM2 in host handler
This commit adds support for the FF-A v1.3 FFA_MEM_RECLAIM2 interface to the host FF-A proxy handler in pKVM, validating the constituent descriptors in the hypervisor TX buffer before forwarding the request to the SPMD and unsharing the reclaimed ranges from the host stage-2 page tables on success.
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +0
-0
[ ... ]
Quoted 7 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +3
-0@@ -611,6 +624,116 @@ static void __do_ffa_mem_xfer(const u64 func_id,__do_ffa_mem_xfer ((fid), (res), (ctxt)); \ }while (0 ); +static void do_ffa_mem_reclaim2 (struct arm_smccc_1_2_regs *res, +struct kvm_cpu_context *ctxt) +{
[ ... ]
Quoted 32 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +32
-0+memcpy (hyp_buffers.tx, host_buffers.tx, (u64 )req_desc_cnt * desc_size); + +for (in_idx =0 ; in_idx < req_desc_cnt; in_idx++) { + range = (void *)hyp_buffers.tx + in_idx * desc_size; +if (check_add_overflow (declared_pages, range->pg_cnt, &declared_pages)) { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out_unlock; + } + } + + range = (void *)hyp_buffers.tx + offset; +if (declared_pages != total_pages || page_index >= range->pg_cnt) { + ret =FFA_RET_INVALID_PARAMETERS ; + goto out_unlock; + } + +ffa_mem_reclaim2 (res, handle, flags, x3, x4, x5); +if (res->a0 !=FFA_SUCCESS ) + goto out_unlock; + + reclaimed_pages = res->a3; + +for (in_idx = offset / desc_size; in_idx < req_desc_cnt; in_idx++) { +struct ffa_mem_region_addr_range temp; +u32 pg_cnt, to_reclaim; + +if (reclaimed_count >= reclaimed_pages) +break ; + + range = (void *)hyp_buffers.tx + in_idx * desc_size; +if (!range->pg_cnt) +continue ;
arch/arm64/kvm/hyp/nvhe/ffa.c static void __do_ffa_mem_xfer(const u64 func_id, · line 724
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/ffa.c +10
-0+ + pg_cnt = range->pg_cnt - page_index; + to_reclaim =min (pg_cnt, reclaimed_pages - reclaimed_count); + + temp = (struct ffa_mem_region_addr_range ) { + .address = range->address + (u64 )page_index *FFA_PAGE_SIZE , + .pg_cnt = to_reclaim, + }; + +WARN_ON (ffa_host_unshare_ranges (&temp,1 ));
[Severity: Medium]
On systems where the kernel PAGE_SIZE is larger than FFA_PAGE_SIZE (16 KiB or 64 KiB pages vs. 4 KiB FF-A pages), should do_ffa_mem_reclaim2() in arch/arm64/kvm/hyp/nvhe/ffa.c verify that each constituent range's address and byte size, as well as the starting page_index offset, are aligned to PAGE_SIZE before forwarding the ffa_mem_reclaim2() SMC to EL3?
On the share paths (__do_ffa_mem_xfer() and do_ffa_mem_frag_tx()), ffa_host_share_ranges() runs prior to the SMC and enforces PAGE_SIZE alignment in __ffa_host_share_ranges().
In do_ffa_mem_reclaim2(), however, the pre-SMC validation only checks check_add_overflow(declared_pages, range->pg_cnt, &declared_pages), declared_pages != total_pages, and page_index >= range->pg_cnt. Once ffa_mem_reclaim2() succeeds in the SPMC, do_ffa_mem_reclaim2() calls ffa_host_unshare_ranges(&temp, 1) -> __ffa_host_unshare_ranges():
for (i = 0; i < nranges; ++i) {
struct ffa_mem_region_addr_range *range = &ranges[i];
u64 sz = (u64)range->pg_cnt * FFA_PAGE_SIZE;
u64 pfn = hyp_phys_to_pfn(range->address);
if (!PAGE_ALIGNED(sz | range->address))
break;If the host passes a descriptor or starting page_index that is not aligned to PAGE_SIZE, __ffa_host_unshare_ranges() breaks out and returns an error after EL3 has already reclaimed the pages. Does this trigger the EL2 WARN_ON() and leave those pages permanently stuck in the PKVM_PAGE_SHARED_OWNED state in the host stage-2 page tables?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2