Skip to contentSkip to conversation

iommu/riscv: check pte null pointer before use

PATCH RESEND In review XianLiang Huang 3 participants 5 messages Aug 7, 2025 – Jun 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/1 iommu/riscv: check pte null pointer before use

The riscv_iommu_pte_fetch can return NULL when the provided iova is not yet mapped, the caller should check if the returned pointer is NULL or not, but riscv_iommu_iova_to_phys missed this, which will then lead to a kernel panic.

Check the pointer before using it to avoid the bug. Now, when iova_to_phys is called with an unmapped iova, the kernel will not crash here.

Fixes 488ffbf18171 ("iommu/riscv: Paging domain support")
Cc Tomasz Jeznach <[email protected]>

---
Changelog
v2:
- Update change description
- Add "Fixes" tag

 drivers/iommu/riscv/iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/riscv/iommu.c +1 -1
index 2d0d31ba2886..b0186faa0300 100644
@@ -1283,7 +1283,7 @@ static phys_addr_t riscv_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
 	unsigned long *ptr;
 
 	ptr = riscv_iommu_pte_fetch(domain, iova, &pte_size);
-	if (_io_pte_none(*ptr) || !_io_pte_present(*ptr))
+	if (!ptr || _io_pte_none(*ptr) || !_io_pte_present(*ptr))
 		return 0;
 
 	return pfn_to_phys(__page_val_to_pfn(*ptr)) | (iova & (pte_size - 1));
Signature · 1 line
2.34.1
#1 lore raw

…> Check the pointer before using it to avoid the bug. …

Would a summary phrase like “Prevent null pointer dereference in riscv_iommu_iova_to_phys()” be also helpful?

Regards,
Markus

#2 lore raw

Sure, 'prevent' is much appropriate than 'check' here...

Prevent null pointer dereference in riscv_iommu_iova_to_phys(). Now, when it's called with an unmapped iova, the kernel will not crash here.

#3 lore raw
Quoted 1 line

Sure, 'prevent' is much appropriate than 'check' here...

I propose to reconsider also different effects from your wording approaches according to the summary phrase and more detailed change description.

Regards,
Markus

maintainer
#4 lore raw
In reply to XianLiang Huang

Hi,

Quoted 5 lines · XianLiang Huang

The riscv_iommu_pte_fetch can return NULL when the provided iova is not yet mapped, the caller should check if the returned pointer is NULL or not, but riscv_iommu_iova_to_phys missed this, which will then lead to a kernel panic.

Quoted 7 lines

Check the pointer before using it to avoid the bug. Now, when iova_to_phys is called with an unmapped iova, the kernel will not crash here.

Fixes 488ffbf18171 ("iommu/riscv: Paging domain support")
Cc Tomasz Jeznach <[email protected]>

Going through some of the Patchwork backlog. The code that this patch changes no longer appears to exist. Care to take a look to see if something like this is still appropriate?

thanks,

- Paul