From d8d0d34c76c09640c469e9c12d12e653e14229d0 Mon Sep 17 00:00:00 2001 From: Mostafa Saleh Date: Tue, 8 Apr 2025 10:31:07 +0000 Subject: [PATCH] ANDROID: KVM: iommu: Check ops are implemented Before calling an ops, check if it is implemeted first, get_iommu_by_id() is quite common, so we make it mandatory. Bug: 357781595 Change-Id: Ib9dbfa42bfb6eccb7f468a689b50c59573d43fb2 Signed-off-by: Mostafa Saleh --- arch/arm64/kvm/hyp/nvhe/iommu/iommu.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/hyp/nvhe/iommu/iommu.c b/arch/arm64/kvm/hyp/nvhe/iommu/iommu.c index f40f2d1a06b2..ba543bdce68e 100644 --- a/arch/arm64/kvm/hyp/nvhe/iommu/iommu.c +++ b/arch/arm64/kvm/hyp/nvhe/iommu/iommu.c @@ -272,7 +272,8 @@ int kvm_iommu_init(struct kvm_iommu_ops *ops, if (!ops || !ops->init || !ops->alloc_domain || - !ops->free_domain) + !ops->free_domain || + !ops->get_iommu_by_id) return 0; ret = hyp_pool_init_empty(&iommu_host_pool, 64); @@ -394,6 +395,9 @@ int kvm_iommu_attach_dev(pkvm_handle_t iommu_id, pkvm_handle_t domain_id, struct pkvm_hyp_vcpu *hyp_vcpu = __get_vcpu(); struct pkvm_hyp_vm *vm = NULL; + if (!kvm_iommu_ops || !kvm_iommu_ops->attach_dev) + return -ENODEV; + iommu = kvm_iommu_ops->get_iommu_by_id(iommu_id); if (!iommu) return -EINVAL; @@ -431,6 +435,9 @@ int kvm_iommu_detach_dev(pkvm_handle_t iommu_id, pkvm_handle_t domain_id, struct pkvm_hyp_vcpu *hyp_vcpu = __get_vcpu(); struct pkvm_hyp_vm *vm = NULL; + if (!kvm_iommu_ops || !kvm_iommu_ops->detach_dev) + return -ENODEV; + iommu = kvm_iommu_ops->get_iommu_by_id(iommu_id); if (!iommu) return -EINVAL; @@ -471,6 +478,9 @@ size_t kvm_iommu_map_pages(pkvm_handle_t domain_id, size_t total_mapped = 0; struct kvm_hyp_iommu_domain *domain; + if (!kvm_iommu_ops || !kvm_iommu_ops->map_pages) + return -ENODEV; + *mapped = 0; if (prot & ~IOMMU_PROT_MASK) @@ -533,6 +543,9 @@ size_t kvm_iommu_unmap_pages(pkvm_handle_t domain_id, unsigned long iova, struct kvm_hyp_iommu_domain *domain; struct iommu_iotlb_gather iotlb_gather; + if (!kvm_iommu_ops || !kvm_iommu_ops->unmap_pages) + return -ENODEV; + if (!pgsize || !pgcount) return 0; @@ -568,6 +581,9 @@ phys_addr_t kvm_iommu_iova_to_phys(pkvm_handle_t domain_id, unsigned long iova) phys_addr_t phys = 0; struct kvm_hyp_iommu_domain *domain; + if (!kvm_iommu_ops || !kvm_iommu_ops->iova_to_phys) + return -ENODEV; + domain = handle_to_domain( domain_id); if (!domain || domain_get(domain))