ANDROID: KVM: arm64: Don't update IOMMUs for share/unshare

Share/unshare initiated by host doesn't change memory permission, and
as currently pKVM doesn't support device assignment, there is no need
to update the IOMMU unnecessarily as it waste cycles.

Once device assignment is enabled, this assumption will not be valid
as guests have access to DMA.

Bug: 300458646
Bug: 291843613
Change-Id: I28c69ec8f721711d5b59fa2784386fa61654fe5a
Signed-off-by: Mostafa Saleh <[email protected]>
(cherry picked from commit b2cc1ef410c162d904ec67d674db89bdd5dd190f)
(cherry picked from commit 83cfe9e475387e65852688d108246909884d60c1)
diff --git a/arch/arm64/kvm/hyp/nvhe/mem_protect.c b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
index 402b22f..1086b06 100644
--- a/arch/arm64/kvm/hyp/nvhe/mem_protect.c
+++ b/arch/arm64/kvm/hyp/nvhe/mem_protect.c
@@ -833,9 +833,20 @@ static int __host_check_page_state_range(u64 addr, u64 size,
 static int __host_set_page_state_range(u64 addr, u64 size,
 				       enum pkvm_page_state state)
 {
+	bool update_iommu = true;
 	enum kvm_pgtable_prot prot = pkvm_mkstate(PKVM_HOST_MEM_PROT, state);
 
-	return host_stage2_idmap_locked(addr, size, prot, true);
+	/*
+	 * Sharing and unsharing host pages shouldn't change the IOMMU page tables,
+	 * so avoid extra page tables walks for the IOMMU.
+	 * HOWEVER THIS WILL NOT WORK WHEN DEVICE ASSIGNMENT IS SUPPORTED AS THE GUEST
+	 * MIGHT HAVE ACCESS TO DMA.
+	 * but as Android-14 doesn't support device assignment this should be fine.
+	 */
+	if ((state == PKVM_PAGE_OWNED) || (state == PKVM_PAGE_SHARED_OWNED))
+		update_iommu = false;
+
+	return host_stage2_idmap_locked(addr, size, prot, update_iommu);
 }
 
 static int host_request_owned_transition(u64 *completer_addr,