Re: [PATCH v3] PCI: Align pci memory space base address with page size

From: bibo, mao
Date: Tue Jun 06 2023 - 06:07:10 EST


Huacai,

Although I post this patch, I think this should be arch specified rather than general problem.
X86 has solved this problem, arm64 with 64K page size is not popular. However LoongArch
has this problem, page size is 16K rather than 4K. It is the problem of LoongArch system
rather than generic issue.

There is such discussion before:
https://patchwork.kernel.org/project/linux-pci/patch/22400b8828ad44ddbccb876cc5ca3b11@xxxxxxxxxxxxxxxxxxxxxxx/#19319457

UEFI bios sets pci memory space 4K aligned, however Loongarch kernel rescans the pci
bus and reassigns pci memory resource. So it it strange like this, here is pci memory info on
my 7A2000 board.
root@user-pc:~# lspci -vvv | grep Region
Region 5: Memory at e003526e800 (32-bit, non-prefetchable) [size=1K]
Region 0: Memory at e003526ec00 (64-bit, non-prefetchable) [size=1K]

Regards
Bibo, Mao

在 2023/6/6 16:45, Bibo Mao 写道:
> Some PCI devices have only 4K memory space size, it is normal in general
> machines and aligned with page size. However some architectures which
> support different page size, default page size on LoongArch is 16K, and
> ARM64 supports page size varying from 4K to 64K. On machines where larger
> page size is use, memory space region of two different pci devices may be
> in one page. It is not safe with mmu protection, also VFIO pci device
> driver requires base address of pci memory space page aligned, so that it
> can be memory mapped to qemu user space when it is passed-through to vm.
>
> It consumes more pci memory resource with page size alignment requirement,
> it should not be a problem on 64 bit system.
>
> Signed-off-by: Bibo Mao <maobibo@xxxxxxxxxxx>
> ---
> drivers/pci/setup-res.c | 8 ++++++++
> 1 file changed, 8 insertions(+)
>
> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c
> index 967f9a758923..55440ae0128d 100644
> --- a/drivers/pci/setup-res.c
> +++ b/drivers/pci/setup-res.c
> @@ -339,6 +339,14 @@ int pci_assign_resource(struct pci_dev *dev, int resno)
> return -EINVAL;
> }
>
> +#ifdef CONFIG_64BIT
> + /*
> + * force minimum page alignment for vfio pci usage
> + * supposing there is enough pci memory resource on 64bit system
> + */
> + if (res->flags & IORESOURCE_MEM)
> + align = max_t(resource_size_t, PAGE_SIZE, align);
> +#endif
> size = resource_size(res);
> ret = _pci_assign_resource(dev, resno, size, align);
>