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

Huacai Chen <[email protected]> Tue, 21 Jul 2026 11:24:22 +0800
Newsgroups dev.linux.lists.loongarch,org.kernel.vger.linux-kernel
Message-ID <CAAhV-H4QLa0Y8-MqUPV1cb1oG_kcNRhA06CiN7n_+ekxZVR=ew@mail.gmail.com>
Hi, Xuewen,

On Tue, Jul 21, 2026 at 10:56 AM Xuewen Wang <[email protected]> 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 <[email protected]>
> ---
>  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
>