[PATCH v1] LoongArch: pci: add NULL check for map_bus return value in loongson_gpu_fixup_dma_hang

Xuewen Wang posted 1 patch 3 days, 22 hours ago
arch/loongarch/pci/pci.c | 5 +++++
1 file changed, 5 insertions(+)
[PATCH v1] LoongArch: pci: add NULL check for map_bus return value in loongson_gpu_fixup_dma_hang
Posted by Xuewen Wang 3 days, 22 hours ago
The map_bus() callback can return NULL when the target device does not
exist (e.g. non-existent device hidden by FLAG_DEV_HIDDEN, or device > 0
on internal bridges). All other callers in the kernel check the return
value, but loongson_gpu_fixup_dma_hang() uses it directly, leading to a
null pointer dereference via readw(base + PCI_DEVICE_ID).

Add the missing NULL check to prevent the crash.

Fixes: 95db0c9f526d ("LoongArch: Workaround LS2K/LS7A GPU DMA hang bug")
Signed-off-by: Xuewen Wang <wangxuewen@kylinos.cn>
---
 arch/loongarch/pci/pci.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/arch/loongarch/pci/pci.c b/arch/loongarch/pci/pci.c
index f33c7ea1443d..3cd89b0224e3 100644
--- a/arch/loongarch/pci/pci.c
+++ b/arch/loongarch/pci/pci.c
@@ -115,6 +115,11 @@ static void loongson_gpu_fixup_dma_hang(struct pci_dev *pdev, bool on)
 	static u32 crtc_status[CRTC_NUM_MAX] = { 0 };
 
 	base = pdev->bus->ops->map_bus(pdev->bus, pdev->devfn + 1, 0);
+	if (!base) {
+		pci_dbg(pdev, "cannot map config space for devfn %02x.%d\n",
+			PCI_SLOT(pdev->devfn + 1), PCI_FUNC(pdev->devfn + 1));
+		return;
+	}
 	device = readw(base + PCI_DEVICE_ID);
 
 	regbase = ioremap(readq(base + PCI_BASE_ADDRESS_0) & ~0xffull, SZ_64K);
-- 
2.25.1
Re: [PATCH v1] LoongArch: pci: add NULL check for map_bus return value in loongson_gpu_fixup_dma_hang
Posted by Huacai Chen 3 days, 21 hours ago
Hi, Xuewen,

On Tue, Jul 21, 2026 at 10:56 AM Xuewen Wang <wangxuewen@kylinos.cn> wrote:
>
> The map_bus() callback can return NULL when the target device does not
> exist (e.g. non-existent device hidden by FLAG_DEV_HIDDEN, or device > 0
> on internal bridges). All other callers in the kernel check the return
> value, but loongson_gpu_fixup_dma_hang() uses it directly, leading to a
> null pointer dereference via readw(base + PCI_DEVICE_ID).
>
> Add the missing NULL check to prevent the crash.
Have you really met the crash? Or just in your brain?

If you understand PCI fixup, you can know that map_bus() cannot get an
arbitrary input here. When the LS7A GPU exists, it is impossible that
DC doesn't exist.

Huacai

>
> Fixes: 95db0c9f526d ("LoongArch: Workaround LS2K/LS7A GPU DMA hang bug")
> Signed-off-by: Xuewen Wang <wangxuewen@kylinos.cn>
> ---
>  arch/loongarch/pci/pci.c | 5 +++++
>  1 file changed, 5 insertions(+)
>
> diff --git a/arch/loongarch/pci/pci.c b/arch/loongarch/pci/pci.c
> index f33c7ea1443d..3cd89b0224e3 100644
> --- a/arch/loongarch/pci/pci.c
> +++ b/arch/loongarch/pci/pci.c
> @@ -115,6 +115,11 @@ static void loongson_gpu_fixup_dma_hang(struct pci_dev *pdev, bool on)
>         static u32 crtc_status[CRTC_NUM_MAX] = { 0 };
>
>         base = pdev->bus->ops->map_bus(pdev->bus, pdev->devfn + 1, 0);
> +       if (!base) {
> +               pci_dbg(pdev, "cannot map config space for devfn %02x.%d\n",
> +                       PCI_SLOT(pdev->devfn + 1), PCI_FUNC(pdev->devfn + 1));
> +               return;
> +       }
>         device = readw(base + PCI_DEVICE_ID);
>
>         regbase = ioremap(readq(base + PCI_BASE_ADDRESS_0) & ~0xffull, SZ_64K);
> --
> 2.25.1
>
Re: [PATCH v1] LoongArch: pci: add NULL check for map_bus return value in loongson_gpu_fixup_dma_hang
Posted by Xuewen Wang 3 days, 18 hours ago
Hi,Huacai,

You are right, I haven't hit this crash in practice -- it was from                                                                                                                                                                                                        reading the code, not an observed failure. I'll drop this patch.                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                
I didn't verify carefully enough whether the NULL path is actually                                                                                                                                                                                                         
reachable here. After re-reading the code and the manual, map_bus()                                                                                                                                                                                                        
won't return NULL in this fixup context, so the check is unnecessary.                                                                                                                                                                                                      
                                                                                                                                                                                                                                                                                
Sorry for the noise. 

Best regards,
Xuewen Wang

在 2026/7/21 11:24, Huacai Chen 写道:
> Hi, Xuewen,
> 
> On Tue, Jul 21, 2026 at 10:56 AM Xuewen Wang <wangxuewen@kylinos.cn> wrote:
>>
>> The map_bus() callback can return NULL when the target device does not
>> exist (e.g. non-existent device hidden by FLAG_DEV_HIDDEN, or device > 0
>> on internal bridges). All other callers in the kernel check the return
>> value, but loongson_gpu_fixup_dma_hang() uses it directly, leading to a
>> null pointer dereference via readw(base + PCI_DEVICE_ID).
>>
>> Add the missing NULL check to prevent the crash.
> Have you really met the crash? Or just in your brain?
> 
> If you understand PCI fixup, you can know that map_bus() cannot get an
> arbitrary input here. When the LS7A GPU exists, it is impossible that
> DC doesn't exist.
> 
> Huacai
> 
>>
>> Fixes: 95db0c9f526d ("LoongArch: Workaround LS2K/LS7A GPU DMA hang bug")
>> Signed-off-by: Xuewen Wang <wangxuewen@kylinos.cn>
>> ---
>>  arch/loongarch/pci/pci.c | 5 +++++
>>  1 file changed, 5 insertions(+)
>>
>> diff --git a/arch/loongarch/pci/pci.c b/arch/loongarch/pci/pci.c
>> index f33c7ea1443d..3cd89b0224e3 100644
>> --- a/arch/loongarch/pci/pci.c
>> +++ b/arch/loongarch/pci/pci.c
>> @@ -115,6 +115,11 @@ static void loongson_gpu_fixup_dma_hang(struct pci_dev *pdev, bool on)
>>         static u32 crtc_status[CRTC_NUM_MAX] = { 0 };
>>
>>         base = pdev->bus->ops->map_bus(pdev->bus, pdev->devfn + 1, 0);
>> +       if (!base) {
>> +               pci_dbg(pdev, "cannot map config space for devfn %02x.%d\n",
>> +                       PCI_SLOT(pdev->devfn + 1), PCI_FUNC(pdev->devfn + 1));
>> +               return;
>> +       }
>>         device = readw(base + PCI_DEVICE_ID);
>>
>>         regbase = ioremap(readq(base + PCI_BASE_ADDRESS_0) & ~0xffull, SZ_64K);
>> --
>> 2.25.1
>>