Skip to content

Commit faf305c

Browse files
rmurphy-armjoergroedel
authored andcommitted
iommu/qcom: Fix bogus detach logic
Currently, the implementation of qcom_iommu_domain_free() is guaranteed to do one of two things: WARN() and leak everything, or dereference NULL and crash. That alone is terrible, but in fact the whole idea of trying to track the liveness of a domain via the qcom_domain->iommu pointer as a sanity check is full of fundamentally flawed assumptions. Make things robust and actually functional by not trying to be quite so clever. Reported-by: Brian Masney <[email protected]> Tested-by: Brian Masney <[email protected]> Reported-by: Naresh Kamboju <[email protected]> Fixes: 0ae349a ("iommu/qcom: Add qcom_iommu") Signed-off-by: Robin Murphy <[email protected]> Tested-by: Stephan Gerhold <[email protected]> Cc: [email protected] # v4.14+ Signed-off-by: Joerg Roedel <[email protected]>
1 parent 3dfee47 commit faf305c

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

drivers/iommu/qcom_iommu.c

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -344,21 +344,19 @@ static void qcom_iommu_domain_free(struct iommu_domain *domain)
344344
{
345345
struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
346346

347-
if (WARN_ON(qcom_domain->iommu)) /* forgot to detach? */
348-
return;
349-
350347
iommu_put_dma_cookie(domain);
351348

352-
/* NOTE: unmap can be called after client device is powered off,
353-
* for example, with GPUs or anything involving dma-buf. So we
354-
* cannot rely on the device_link. Make sure the IOMMU is on to
355-
* avoid unclocked accesses in the TLB inv path:
356-
*/
357-
pm_runtime_get_sync(qcom_domain->iommu->dev);
358-
359-
free_io_pgtable_ops(qcom_domain->pgtbl_ops);
360-
361-
pm_runtime_put_sync(qcom_domain->iommu->dev);
349+
if (qcom_domain->iommu) {
350+
/*
351+
* NOTE: unmap can be called after client device is powered
352+
* off, for example, with GPUs or anything involving dma-buf.
353+
* So we cannot rely on the device_link. Make sure the IOMMU
354+
* is on to avoid unclocked accesses in the TLB inv path:
355+
*/
356+
pm_runtime_get_sync(qcom_domain->iommu->dev);
357+
free_io_pgtable_ops(qcom_domain->pgtbl_ops);
358+
pm_runtime_put_sync(qcom_domain->iommu->dev);
359+
}
362360

363361
kfree(qcom_domain);
364362
}
@@ -404,7 +402,7 @@ static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *de
404402
struct qcom_iommu_domain *qcom_domain = to_qcom_iommu_domain(domain);
405403
unsigned i;
406404

407-
if (!qcom_domain->iommu)
405+
if (WARN_ON(!qcom_domain->iommu))
408406
return;
409407

410408
pm_runtime_get_sync(qcom_iommu->dev);
@@ -417,8 +415,6 @@ static void qcom_iommu_detach_dev(struct iommu_domain *domain, struct device *de
417415
ctx->domain = NULL;
418416
}
419417
pm_runtime_put_sync(qcom_iommu->dev);
420-
421-
qcom_domain->iommu = NULL;
422418
}
423419

424420
static int qcom_iommu_map(struct iommu_domain *domain, unsigned long iova,

0 commit comments

Comments
 (0)