Skip to content

Commit

Permalink
iommu: fix an incorrect NULL check on list iterator
Browse files Browse the repository at this point in the history
The bug is here:
	if (!iommu || iommu->dev->of_node != spec->np) {

The list iterator value 'iommu' will *always* be set and non-NULL by
list_for_each_entry(), so it is incorrect to assume that the iterator
value will be NULL if the list is empty or no element is found (in fact,
it will point to a invalid structure object containing HEAD).

To fix the bug, use a new value 'iter' as the list iterator, while use
the old value 'iommu' as a dedicated variable to point to the found one,
and remove the unneeded check for 'iommu->dev->of_node != spec->np'
outside the loop.

Cc: stable@vger.kernel.org
Fixes: f78ebca ("iommu/msm: Add support for generic master bindings")
Signed-off-by: Xiaomeng Tong <xiam0nd.tong@gmail.com>
  • Loading branch information
Xiaomeng Tong authored and intel-lab-lkp committed May 1, 2022
1 parent 85613ad commit 99e334b
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions drivers/iommu/msm_iommu.c
Expand Up @@ -615,16 +615,17 @@ static int insert_iommu_master(struct device *dev,
static int qcom_iommu_of_xlate(struct device *dev,
struct of_phandle_args *spec)
{
struct msm_iommu_dev *iommu;
struct msm_iommu_dev *iommu = NULL, *iter;
unsigned long flags;
int ret = 0;

spin_lock_irqsave(&msm_iommu_lock, flags);
list_for_each_entry(iommu, &qcom_iommu_devices, dev_node)
if (iommu->dev->of_node == spec->np)
list_for_each_entry(iter, &qcom_iommu_devices, dev_node)
if (iter->dev->of_node == spec->np) {
iommu = iter;
break;
}

if (!iommu || iommu->dev->of_node != spec->np) {
if (!iommu) {
ret = -ENODEV;
goto fail;
}
Expand Down

0 comments on commit 99e334b

Please sign in to comment.