From b6ae4515d3d53f193fb7faced274754707255d01 Mon Sep 17 00:00:00 2001 From: Sukadev Bhattiprolu Date: Thu, 15 Sep 2022 23:15:19 -0700 Subject: [PATCH 1/4] qcom-iommu-debug: Fail incorrect fastmap usecases In the current iommu-debug fastmap test device, we map and unmap the entire iova range from 0..4G for each of the usecases. However for the following secure usecase: $ cd /sys/kernel/debug/iommu-test $ cat usecase $ echo 4 > usecase $ cat functional_fast_dma_api this mapping of iova range fails with following warning after about 496640 iterations of 8K-size mapping. batched_hyp_assign: Failed to assign memory protection, ret = -22 ------------[ cut here ]------------ failed to assign memory to VMID: 10 rc:-99 WARNING: CPU: 0 PID: 240 at drivers/iommu/qcom-io-pgtable-alloc.c:39 qcom_io_pgtable_alloc_page+0x110/0x120 Once this happens the memory is no longer usable by HLOS (according to following comments batched_hyp_assign(): 344 if (ret) { 345 pr_info("%s: Failed to assign memory protection, ret= %d\n", 346 __func__, ret); 347 /* 348 * Make it clear to clients that the memory may no 349 * longer be in a usable state. 350 */ 351 ret = -EADDRNOTAVAIL; 352 break; 353 } To run any other usecase, we must first destroy the device and recreate it. But when destroying the device, we get the following warning continuously (presumably for the 496640 mappings that succeeded): ------------[ cut here ]------------ failed to unassign memory from VMID: 10 rc: -99 WARNING: CPU: 0 PID: 240 at drivers/iommu/qcom-io-pgtable-alloc.c:54 io_pgtable_pool_release+0x1b8/0x24c Call trace: io_pgtable_pool_release+0x1b8/0x24c qcom_io_pgtable_allocator_unregister+0x5c/0xa8 arm_lpae_free_pgtable+0x30/0x4c qcom_free_io_pgtable_ops+0x80/0xa4 arm_smmu_destroy_domain_context+0xd0/0x1ec arm_smmu_domain_free+0x34/0x50 iommu_group_release+0x5c/0xa8 kobject_cleanup+0x78/0x1dc kobject_cleanup+0xd8/0x1dc kobject_put+0x68/0xa8 iommu_group_remove_device+0x114/0x184 iommu_release_device+0x48/0x8c iommu_bus_notifier+0x4c/0xa4 blocking_notifier_call_chain+0x5c/0xa8 device_del+0x2d8/0x3d4 platform_device_unregister+0x34/0xb4 of_platform_device_destroy+0xac/0xe4 iommu_debug_switch_usecase+0x38/0x194 iommu_debug_usecase_reset+0x18/0x28 iommu_debug_functional_fast_dma_api_show+0x13e8/0x1538 IOW, the device is unusable and requires a reboot. Its not clear what the behavior should be for a fastmap test on a non-fastmap device. For now, detect an invalid usecase and fail cleanly. Change-Id: I77b827825215f262e836e8902143ea297448612e Signed-off-by: Sukadev Bhattiprolu --- drivers/iommu/qcom-iommu-debug-user.c | 16 ++++++++++++++++ drivers/iommu/qcom-iommu-debug.c | 5 +++++ drivers/iommu/qcom-iommu-debug.h | 3 ++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/iommu/qcom-iommu-debug-user.c b/drivers/iommu/qcom-iommu-debug-user.c index c991a2030f80..71993447d9f1 100644 --- a/drivers/iommu/qcom-iommu-debug-user.c +++ b/drivers/iommu/qcom-iommu-debug-user.c @@ -1203,6 +1203,22 @@ static int iommu_debug_functional_fast_dma_api_show(struct seq_file *s, void *ignored) { int ret = 0; + struct iommu_debug_device *ddev = s->private; + + if (!ddev->test_dev) { + pr_err("%s:Have you selected a uscase?\n", __func__); + return -EINVAL; + } + + if (!ddev->fastmap_usecase) { + ps_printf(dev_name(ddev->test_dev), s, + "Not a fastmap usecase\n"); + return 0; + } else if (!IS_ENABLED(CONFIG_IOMMU_IO_PGTABLE_FAST)) { + ps_printf(dev_name(ddev->test_dev), s, + "CONFIG_IOMMU_IO_PGTABLE_FAST not enabled\n"); + return 0; + } ret |= __apply_to_new_mapping(s, __functional_dma_api_alloc_test, NULL); ret |= __apply_to_new_mapping(s, __functional_dma_api_basic_test, NULL); diff --git a/drivers/iommu/qcom-iommu-debug.c b/drivers/iommu/qcom-iommu-debug.c index feb51650662b..f30ef09aa390 100644 --- a/drivers/iommu/qcom-iommu-debug.c +++ b/drivers/iommu/qcom-iommu-debug.c @@ -133,6 +133,7 @@ iommu_debug_switch_usecase(struct iommu_debug_device *ddev, u32 usecase_nr) { struct platform_device *test_pdev; struct device_node *child; + const char *str; int child_nr = 0; int ret; @@ -169,6 +170,10 @@ iommu_debug_switch_usecase(struct iommu_debug_device *ddev, u32 usecase_nr) goto out; } + if (of_property_read_string(child, "qcom,iommu-dma", &str)) + str = "default"; + + ddev->fastmap_usecase = !strcmp(str, "fastmap"); ddev->usecase_nr = usecase_nr; ddev->test_dev = &test_pdev->dev; ddev->domain = iommu_get_domain_for_dev(ddev->test_dev); diff --git a/drivers/iommu/qcom-iommu-debug.h b/drivers/iommu/qcom-iommu-debug.h index 41e06082e01b..78f3106ea9eb 100644 --- a/drivers/iommu/qcom-iommu-debug.h +++ b/drivers/iommu/qcom-iommu-debug.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: GPL-2.0-only */ /* * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved. - * + * Copyright (c) 2022, Qualcomm Innovation Center, Inc. All rights reserved. */ #ifndef __DRIVERS_IOMMU_QCOM_IOMMU_DEBUG_H__ @@ -26,6 +26,7 @@ struct iommu_debug_device { struct device *test_dev; struct iommu_domain *domain; u32 usecase_nr; + bool fastmap_usecase; /* Protects test_dev */ struct mutex state_lock; /* For waiting for child probe to complete */ From f817953a3b823fa389d329615dddd5be8cf64e71 Mon Sep 17 00:00:00 2001 From: Chris Goldsworthy Date: Sat, 12 Nov 2022 18:31:41 -0800 Subject: [PATCH 2/4] dma-mapping-fast: IOMMU helper clean up iommu_put_dma_cookie() is now called by the IOMMU framework code before each driver's domain free callback function is invoked. Remove iommu_put_dma_cookie() accordingly. qcom_iommu_put_resv_regions() is now replaced by iommu_put_resv_regions(), drop all references to our qcom_ function versions. Change-Id: Ib9c1c431faf4988707e449ad00c1e3fe7ee2dd47 Signed-off-by: Chris Goldsworthy --- drivers/iommu/dma-mapping-fast.c | 4 +--- include/linux/qcom-iommu-util.h | 3 +-- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/drivers/iommu/dma-mapping-fast.c b/drivers/iommu/dma-mapping-fast.c index aa777b60322c..962e81b5e082 100644 --- a/drivers/iommu/dma-mapping-fast.c +++ b/drivers/iommu/dma-mapping-fast.c @@ -980,7 +980,7 @@ static void fast_smmu_reserve_iommu_regions(struct device *dev, bitmap_set(fast->clean_bitmap, lo, hi - lo + 1); } spin_unlock_irqrestore(&mapping->lock, flags); - qcom_iommu_put_resv_regions(dev, &resv_regions); + iommu_put_resv_regions(dev, &resv_regions); fast_smmu_reserve_msi_iova(dev, fast); } @@ -992,8 +992,6 @@ void fast_smmu_put_dma_cookie(struct iommu_domain *domain) if (!fast) return; - iommu_put_dma_cookie(domain); - if (fast->iovad) { put_iova_domain(fast->iovad); kfree(fast->iovad); diff --git a/include/linux/qcom-iommu-util.h b/include/linux/qcom-iommu-util.h index db5a07cdda7a..4bbb597dbcf8 100644 --- a/include/linux/qcom-iommu-util.h +++ b/include/linux/qcom-iommu-util.h @@ -112,9 +112,8 @@ int qcom_iommu_get_fast_iova_range(struct device *dev, dma_addr_t *ret_iova_base, dma_addr_t *ret_iova_end); -/* Remove once these functions are exported by upstream kernel */ +/* Remove once this function is exported by upstream kernel */ void qcom_iommu_get_resv_regions(struct device *dev, struct list_head *list); -void qcom_iommu_put_resv_regions(struct device *dev, struct list_head *list); phys_addr_t qcom_iommu_iova_to_phys_hard(struct iommu_domain *domain, struct qcom_iommu_atos_txn *txn); From 6acada3f6ea224fd34a97233d219cd73c684a58e Mon Sep 17 00:00:00 2001 From: Chris Goldsworthy Date: Sat, 12 Nov 2022 18:43:34 -0800 Subject: [PATCH 3/4] abi_gki_aarch64_qcom: Add fastmap symbols Add symbols for fastmap. Change-Id: Iad33c705b716b5268f42ed6e02c3de7d8717fe4d Signed-off-by: Chris Goldsworthy --- android/abi_gki_aarch64_qcom | 1 + 1 file changed, 1 insertion(+) diff --git a/android/abi_gki_aarch64_qcom b/android/abi_gki_aarch64_qcom index aa78cc5dbe6a..a37fec8b7402 100644 --- a/android/abi_gki_aarch64_qcom +++ b/android/abi_gki_aarch64_qcom @@ -1799,6 +1799,7 @@ __tracepoint_android_rvh_find_lowest_rq __tracepoint_android_rvh_flush_task __tracepoint_android_rvh_get_nohz_timer_target + __tracepoint_android_rvh_iommu_setup_dma_ops __tracepoint_android_rvh_is_cpu_allowed __tracepoint_android_rvh_migrate_queued_task __tracepoint_android_rvh_new_task_stats From 0e2621f585e2b51100649f09ccd767f71a2fcd83 Mon Sep 17 00:00:00 2001 From: Chris Goldsworthy Date: Sat, 12 Nov 2022 18:44:24 -0800 Subject: [PATCH 4/4] defconfig: pineapple: Enable fastmap Enable the fastmap driver for the IOMMU. Change-Id: Ie3a01ffe230fd3459e6a1408f5f78b64fbfae3b6 Signed-off-by: Chris Goldsworthy --- arch/arm64/configs/vendor/pineapple_GKI.config | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/arm64/configs/vendor/pineapple_GKI.config b/arch/arm64/configs/vendor/pineapple_GKI.config index 171529c7c970..974bf1c7b471 100644 --- a/arch/arm64/configs/vendor/pineapple_GKI.config +++ b/arch/arm64/configs/vendor/pineapple_GKI.config @@ -45,6 +45,7 @@ CONFIG_INPUT_QCOM_HV_HAPTICS=m CONFIG_INTERCONNECT_QCOM_DEBUG=m CONFIG_INTERCONNECT_QCOM_PINEAPPLE=m CONFIG_INTERCONNECT_TEST=m +CONFIG_IOMMU_IO_PGTABLE_FAST=y CONFIG_IPA3=m CONFIG_IPC_LOGGING=m CONFIG_IPC_LOG_MINIDUMP_BUFFERS=0