Message ID | 20230609022047.2195689-1-maobibo@loongson.cn |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp667358vqr; Thu, 8 Jun 2023 19:25:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4x31Zr+OsENUXOeuecJc+d9vsVRPN8zt+yOeY4ni+3qmqg9Q7flfqDVq2pnQVxKmyQrq3c X-Received: by 2002:a05:6359:c12:b0:121:7740:94e9 with SMTP id gn18-20020a0563590c1200b00121774094e9mr110750rwb.5.1686277556879; Thu, 08 Jun 2023 19:25:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686277556; cv=none; d=google.com; s=arc-20160816; b=ZvPDeM5yJRtYiHasei6gYGyLo6G7EFyICU99tz5TEaEmdNs8u0tbOxTlTDPAhP9SH4 TNPLFKycNJ0Q2M7Xkpar95IHVXoZP1cL5bW7F9X1GNWB1/GOEi2QGAF+UJH6IHVHFEY3 yuXdtOextSkQIHLr0P/ErTaAK2o/EjOTljuepJNMEbf3rQsKlCzpWUzVU+jK0FVXy5+b NX8ZWapMzvH7oe5EdyqctlUqtSo41M0J9NuWFK6xFUX9kDqkwspe6/qml3KXDRpw9xRG o+yu/wHRhyO7Q4xLUoc+cdNd1L4Q15CaQu+1KjGTery1nwwlW7yXwwLt6E9NW504h+jV AsFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=xxZesHmqvJZcArnznRAWQBE/jqxgNubgMg7+WJjUGV0=; b=qx7Oxl8defLl/xZihiW+3TvfhYDLf++MzbWI0bMG/D8fWSeEAZDrrl5e0g2T9SQJpK TUfVcyWf/0Gy7VuEWu0frdQebQTFGDYTFPp2D5vjDp96BhxCQkaocL7lyZmUXXereas4 X+EErYb1fuUyIEdZxZGX03glpfJOL7v0087wWwTMa4WkvA9FfK/HZR025WHndjCruldH RcLSuJ3HRZ2vOnnOp4XAEQiVEQ+k9i2lxyxTpL1P5q8z/X84+qILwnhT4HamWqu9c35n fxg0hHhfHJtkB5sp1L4qrBPq69Q0yG1omftNmeP34aj2yq8hqNYDSDuI1xzZpzMhzoe+ hdaw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bm18-20020a656e92000000b0053074c54c3fsi1922039pgb.868.2023.06.08.19.25.44; Thu, 08 Jun 2023 19:25:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236922AbjFICU4 (ORCPT <rfc822;literming00@gmail.com> + 99 others); Thu, 8 Jun 2023 22:20:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39428 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229709AbjFICUx (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 8 Jun 2023 22:20:53 -0400 Received: from mail.loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A973F210E; Thu, 8 Jun 2023 19:20:50 -0700 (PDT) Received: from loongson.cn (unknown [10.2.9.158]) by gateway (Coremail) with SMTP id _____8BxKuqAjIJkldgAAA--.2683S3; Fri, 09 Jun 2023 10:20:48 +0800 (CST) Received: from kvm-1-158.loongson.cn (unknown [10.2.9.158]) by localhost.localdomain (Coremail) with SMTP id AQAAf8DxbMp_jIJkDnwJAA--.20801S2; Fri, 09 Jun 2023 10:20:47 +0800 (CST) From: Bibo Mao <maobibo@loongson.cn> To: Bjorn Helgaas <bhelgaas@google.com> Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org, Huacai Chen <chenhuacai@kernel.org>, Will Deacon <will@kernel.org>, loongarch@lists.linux.dev, loongson-kernel@lists.loongnix.cn Subject: [PATCH v4] PCI: Align pci memory space base address with page size Date: Fri, 9 Jun 2023 10:20:47 +0800 Message-Id: <20230609022047.2195689-1-maobibo@loongson.cn> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: AQAAf8DxbMp_jIJkDnwJAA--.20801S2 X-CM-SenderInfo: xpdruxter6z05rqj20fqof0/ X-Coremail-Antispam: 1Uk129KBj93XoWxGr1rurW3JF48uF4UArWkAFc_yoW5Gr15pF y3A3ZrCrW8Kr1fJ3yDt34kuFZxXan2g3yYvryfCas3GFnrZF9rCw1kAry2qFyUCr4kGryj qFn5KF15Xay5XagCm3ZEXasCq-sJn29KB7ZKAUJUUUUr529EdanIXcx71UUUUU7KY7ZEXa sCq-sGcSsGvfJ3Ic02F40EFcxC0VAKzVAqx4xG6I80ebIjqfuFe4nvWSU5nxnvy29KBjDU 0xBIdaVrnRJUUU9Fb4IE77IF4wAFF20E14v26r1j6r4UM7CY07I20VC2zVCF04k26cxKx2 IYs7xG6rWj6s0DM7CIcVAFz4kK6r1Y6r17M28lY4IEw2IIxxk0rwA2F7IY1VAKz4vEj48v e4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7CjxVAFwI 0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_Gr1j6F4UJwA2z4x0Y4vEx4A2jsIEc7CjxVAF wI0_Gr1j6F4UJwAaw2AFwI0_Jrv_JF1le2I262IYc4CY6c8Ij28IcVAaY2xG8wAqjxCEc2 xF0cIa020Ex4CE44I27wAqx4xG64xvF2IEw4CE5I8CrVC2j2WlYx0E2Ix0cI8IcVAFwI0_ Jrv_JF1lYx0Ex4A2jsIE14v26r1j6r4UMcvjeVCFs4IE7xkEbVWUJVW8JwACjcxG0xvY0x 0EwIxGrwCF04k20xvY0x0EwIxGrwCFx2IqxVCFs4IE7xkEbVWUJVW8JwCFI7km07C267AK xVWUXVWUAwC20s026c02F40E14v26r1j6r18MI8I3I0E7480Y4vE14v26r106r1rMI8E67 AF67kF1VAFwI0_JF0_Jw1lIxkGc2Ij64vIr41lIxAIcVC0I7IYx2IY67AKxVWUJVWUCwCI 42IY6xIIjxv20xvEc7CjxVAFwI0_Jr0_Gr1lIxAIcVCF04k26cxKx2IYs7xG6r1j6r1xMI IF0xvEx4A2jsIE14v26r1j6r4UMIIF0xvEx4A2jsIEc7CjxVAFwI0_Jr0_GrUvcSsGvfC2 KfnxnUUI43ZEXa7IU8hiSPUUUUU== X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768190175677254713?= X-GMAIL-MSGID: =?utf-8?q?1768190175677254713?= |
Series |
[v4] PCI: Align pci memory space base address with page size
|
|
Commit Message
maobibo
June 9, 2023, 2:20 a.m. UTC
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,
here extra option PCI_MEMRES_PAGE_ALIGN is added, it can be enabled by
different architectures.
Signed-off-by: Bibo Mao <maobibo@loongson.cn>
---
Change history
v4: add extra kernel option PCI_MEMRES_PAGE_ALIGN to set memory resource
page aligned.
v3: move alignment requirement to generic pci code
v2: add pci resource alignment requirement in arch specified function
pcibios_align_resource on arm64/LoongArch platforms
---
arch/loongarch/Kconfig | 1 +
drivers/pci/Kconfig | 3 +++
drivers/pci/setup-res.c | 7 +++++++
3 files changed, 11 insertions(+)
Comments
Hi, Bibo, On Fri, Jun 9, 2023 at 10:20 AM Bibo Mao <maobibo@loongson.cn> wrote: > > 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, > here extra option PCI_MEMRES_PAGE_ALIGN is added, it can be enabled by > different architectures. > > Signed-off-by: Bibo Mao <maobibo@loongson.cn> > --- > Change history > v4: add extra kernel option PCI_MEMRES_PAGE_ALIGN to set memory resource > page aligned. > > v3: move alignment requirement to generic pci code > > v2: add pci resource alignment requirement in arch specified function > pcibios_align_resource on arm64/LoongArch platforms > > --- > arch/loongarch/Kconfig | 1 + > drivers/pci/Kconfig | 3 +++ > drivers/pci/setup-res.c | 7 +++++++ > 3 files changed, 11 insertions(+) > > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig > index d38b066fc931..65b2f6ba9f8e 100644 > --- a/arch/loongarch/Kconfig > +++ b/arch/loongarch/Kconfig > @@ -142,6 +142,7 @@ config LOONGARCH > select PCI_LOONGSON > select PCI_MSI_ARCH_FALLBACKS > select PCI_QUIRKS > + select PCI_MEMRES_PAGE_ALIGN > select PERF_USE_VMALLOC > select RTC_LIB > select SMP > diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig > index 9309f2469b41..9be5f85ff9dc 100644 > --- a/drivers/pci/Kconfig > +++ b/drivers/pci/Kconfig > @@ -128,6 +128,9 @@ config PCI_LOCKLESS_CONFIG > config PCI_BRIDGE_EMUL > bool > > +config PCI_MEMRES_PAGE_ALIGN > + bool > + > config PCI_IOV > bool "PCI IOV support" > select PCI_ATS > diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c > index 967f9a758923..6ad76734a670 100644 > --- a/drivers/pci/setup-res.c > +++ b/drivers/pci/setup-res.c > @@ -339,6 +339,13 @@ int pci_assign_resource(struct pci_dev *dev, int resno) > return -EINVAL; > } > > +#ifdef CONFIG_PCI_MEMRES_PAGE_ALIGN > + /* > + * force minimum page alignment for vfio pci usage > + */ > + if (res->flags & IORESOURCE_MEM) > + align = max_t(resource_size_t, PAGE_SIZE, align); > +#endif Does this really have its effect? The common version of pcibios_align_resource() simply returns res->start, and doesn't care about the 'align' parameter. Huacai > size = resource_size(res); > ret = _pci_assign_resource(dev, resno, size, align); > > -- > 2.27.0 >
在 2023/6/9 10:29, Huacai Chen 写道: > Hi, Bibo, > > On Fri, Jun 9, 2023 at 10:20 AM Bibo Mao <maobibo@loongson.cn> wrote: >> >> 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, >> here extra option PCI_MEMRES_PAGE_ALIGN is added, it can be enabled by >> different architectures. >> >> Signed-off-by: Bibo Mao <maobibo@loongson.cn> >> --- >> Change history >> v4: add extra kernel option PCI_MEMRES_PAGE_ALIGN to set memory resource >> page aligned. >> >> v3: move alignment requirement to generic pci code >> >> v2: add pci resource alignment requirement in arch specified function >> pcibios_align_resource on arm64/LoongArch platforms >> >> --- >> arch/loongarch/Kconfig | 1 + >> drivers/pci/Kconfig | 3 +++ >> drivers/pci/setup-res.c | 7 +++++++ >> 3 files changed, 11 insertions(+) >> >> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig >> index d38b066fc931..65b2f6ba9f8e 100644 >> --- a/arch/loongarch/Kconfig >> +++ b/arch/loongarch/Kconfig >> @@ -142,6 +142,7 @@ config LOONGARCH >> select PCI_LOONGSON >> select PCI_MSI_ARCH_FALLBACKS >> select PCI_QUIRKS >> + select PCI_MEMRES_PAGE_ALIGN >> select PERF_USE_VMALLOC >> select RTC_LIB >> select SMP >> diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig >> index 9309f2469b41..9be5f85ff9dc 100644 >> --- a/drivers/pci/Kconfig >> +++ b/drivers/pci/Kconfig >> @@ -128,6 +128,9 @@ config PCI_LOCKLESS_CONFIG >> config PCI_BRIDGE_EMUL >> bool >> >> +config PCI_MEMRES_PAGE_ALIGN >> + bool >> + >> config PCI_IOV >> bool "PCI IOV support" >> select PCI_ATS >> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c >> index 967f9a758923..6ad76734a670 100644 >> --- a/drivers/pci/setup-res.c >> +++ b/drivers/pci/setup-res.c >> @@ -339,6 +339,13 @@ int pci_assign_resource(struct pci_dev *dev, int resno) >> return -EINVAL; >> } >> >> +#ifdef CONFIG_PCI_MEMRES_PAGE_ALIGN >> + /* >> + * force minimum page alignment for vfio pci usage >> + */ >> + if (res->flags & IORESOURCE_MEM) >> + align = max_t(resource_size_t, PAGE_SIZE, align); >> +#endif > Does this really have its effect? The common version of > pcibios_align_resource() simply returns res->start, and doesn't care > about the 'align' parameter. yes, it works. The is output of command " lspci -vvv | grep Region" on my 3C5000+7A2000 box. After the patch base address of all pci mem resource is aligned with 16K. output without the patch: Region 0: Memory at e0045240000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045248000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045250000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045258000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045260000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045271400 (64-bit, non-prefetchable) [size=256] Region 2: Memory at e0040000000 (64-bit, non-prefetchable) [size=64M] Region 4: Memory at e0045200000 (64-bit, non-prefetchable) [size=64K] Region 0: Memory at e0045210000 (64-bit, non-prefetchable) [size=64K] Region 0: Memory at e0045220000 (64-bit, non-prefetchable) [size=64K] Region 0: Memory at e0045230000 (64-bit, non-prefetchable) [size=64K] Region 5: Memory at e0045271000 (32-bit, non-prefetchable) [size=1K] Region 0: Memory at e0045268000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045269000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004526a000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004526b000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004526c000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004526d000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004526e000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004526f000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045270000 (64-bit, non-prefetchable) [size=4K] Region 2: Memory at e0044000000 (64-bit, non-prefetchable) [size=16M] Region 0: Memory at e0045100000 (64-bit, non-prefetchable) [size=1M] Region 0: Memory at e0045000000 (64-bit, non-prefetchable) [size=16K] out put with the patch: Region 0: Memory at e0045240000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045248000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045250000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045258000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045260000 (64-bit, non-prefetchable) [size=32K] Region 0: Memory at e0045290000 (64-bit, non-prefetchable) [size=256] Region 2: Memory at e0040000000 (64-bit, non-prefetchable) [size=64M] Region 4: Memory at e0045200000 (64-bit, non-prefetchable) [size=64K] Region 0: Memory at e0045210000 (64-bit, non-prefetchable) [size=64K] Region 0: Memory at e0045220000 (64-bit, non-prefetchable) [size=64K] Region 0: Memory at e0045230000 (64-bit, non-prefetchable) [size=64K] Region 5: Memory at e004528c000 (32-bit, non-prefetchable) [size=1K] Region 0: Memory at e0045268000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004526c000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045270000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045274000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045278000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e004527c000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045280000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045284000 (64-bit, non-prefetchable) [size=4K] Region 0: Memory at e0045288000 (64-bit, non-prefetchable) [size=4K] Region 2: Memory at e0044000000 (64-bit, non-prefetchable) [size=16M] Region 0: Memory at e0045100000 (64-bit, non-prefetchable) [size=1M] Region 0: Memory at e0045000000 (64-bit, non-prefetchable) [size=16K] Regards Bibo, Mao > > Huacai >> size = resource_size(res); >> ret = _pci_assign_resource(dev, resno, size, align); >> >> -- >> 2.27.0 >>
On Fri, Jun 9, 2023 at 11:47 AM bibo, mao <maobibo@loongson.cn> wrote: > > > > 在 2023/6/9 10:29, Huacai Chen 写道: > > Hi, Bibo, > > > > On Fri, Jun 9, 2023 at 10:20 AM Bibo Mao <maobibo@loongson.cn> wrote: > >> > >> 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, > >> here extra option PCI_MEMRES_PAGE_ALIGN is added, it can be enabled by > >> different architectures. > >> > >> Signed-off-by: Bibo Mao <maobibo@loongson.cn> > >> --- > >> Change history > >> v4: add extra kernel option PCI_MEMRES_PAGE_ALIGN to set memory resource > >> page aligned. > >> > >> v3: move alignment requirement to generic pci code > >> > >> v2: add pci resource alignment requirement in arch specified function > >> pcibios_align_resource on arm64/LoongArch platforms > >> > >> --- > >> arch/loongarch/Kconfig | 1 + > >> drivers/pci/Kconfig | 3 +++ > >> drivers/pci/setup-res.c | 7 +++++++ > >> 3 files changed, 11 insertions(+) > >> > >> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig > >> index d38b066fc931..65b2f6ba9f8e 100644 > >> --- a/arch/loongarch/Kconfig > >> +++ b/arch/loongarch/Kconfig > >> @@ -142,6 +142,7 @@ config LOONGARCH > >> select PCI_LOONGSON > >> select PCI_MSI_ARCH_FALLBACKS > >> select PCI_QUIRKS > >> + select PCI_MEMRES_PAGE_ALIGN > >> select PERF_USE_VMALLOC > >> select RTC_LIB > >> select SMP > >> diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig > >> index 9309f2469b41..9be5f85ff9dc 100644 > >> --- a/drivers/pci/Kconfig > >> +++ b/drivers/pci/Kconfig > >> @@ -128,6 +128,9 @@ config PCI_LOCKLESS_CONFIG > >> config PCI_BRIDGE_EMUL > >> bool > >> > >> +config PCI_MEMRES_PAGE_ALIGN > >> + bool > >> + > >> config PCI_IOV > >> bool "PCI IOV support" > >> select PCI_ATS > >> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c > >> index 967f9a758923..6ad76734a670 100644 > >> --- a/drivers/pci/setup-res.c > >> +++ b/drivers/pci/setup-res.c > >> @@ -339,6 +339,13 @@ int pci_assign_resource(struct pci_dev *dev, int resno) > >> return -EINVAL; > >> } > >> > >> +#ifdef CONFIG_PCI_MEMRES_PAGE_ALIGN > >> + /* > >> + * force minimum page alignment for vfio pci usage > >> + */ > >> + if (res->flags & IORESOURCE_MEM) > >> + align = max_t(resource_size_t, PAGE_SIZE, align); > >> +#endif > > Does this really have its effect? The common version of > > pcibios_align_resource() simply returns res->start, and doesn't care > > about the 'align' parameter. > yes, it works. The is output of command " lspci -vvv | grep Region" on my > 3C5000+7A2000 box. After the patch base address of all pci mem resource > is aligned with 16K. > > output without the patch: > Region 0: Memory at e0045240000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045248000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045250000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045258000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045260000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045271400 (64-bit, non-prefetchable) [size=256] > Region 2: Memory at e0040000000 (64-bit, non-prefetchable) [size=64M] > Region 4: Memory at e0045200000 (64-bit, non-prefetchable) [size=64K] > Region 0: Memory at e0045210000 (64-bit, non-prefetchable) [size=64K] > Region 0: Memory at e0045220000 (64-bit, non-prefetchable) [size=64K] > Region 0: Memory at e0045230000 (64-bit, non-prefetchable) [size=64K] > Region 5: Memory at e0045271000 (32-bit, non-prefetchable) [size=1K] > Region 0: Memory at e0045268000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045269000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004526a000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004526b000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004526c000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004526d000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004526e000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004526f000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045270000 (64-bit, non-prefetchable) [size=4K] > Region 2: Memory at e0044000000 (64-bit, non-prefetchable) [size=16M] > Region 0: Memory at e0045100000 (64-bit, non-prefetchable) [size=1M] > Region 0: Memory at e0045000000 (64-bit, non-prefetchable) [size=16K] > > out put with the patch: > Region 0: Memory at e0045240000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045248000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045250000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045258000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045260000 (64-bit, non-prefetchable) [size=32K] > Region 0: Memory at e0045290000 (64-bit, non-prefetchable) [size=256] > Region 2: Memory at e0040000000 (64-bit, non-prefetchable) [size=64M] > Region 4: Memory at e0045200000 (64-bit, non-prefetchable) [size=64K] > Region 0: Memory at e0045210000 (64-bit, non-prefetchable) [size=64K] > Region 0: Memory at e0045220000 (64-bit, non-prefetchable) [size=64K] > Region 0: Memory at e0045230000 (64-bit, non-prefetchable) [size=64K] > Region 5: Memory at e004528c000 (32-bit, non-prefetchable) [size=1K] > Region 0: Memory at e0045268000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004526c000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045270000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045274000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045278000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e004527c000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045280000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045284000 (64-bit, non-prefetchable) [size=4K] > Region 0: Memory at e0045288000 (64-bit, non-prefetchable) [size=4K] > Region 2: Memory at e0044000000 (64-bit, non-prefetchable) [size=16M] > Region 0: Memory at e0045100000 (64-bit, non-prefetchable) [size=1M] > Region 0: Memory at e0045000000 (64-bit, non-prefetchable) [size=16K] OK, thanks. Reviewed-by: Huacai Chen <chenhuacai@loongson.cn> > > Regards > Bibo, Mao > > > > Huacai > >> size = resource_size(res); > >> ret = _pci_assign_resource(dev, resno, size, align); > >> > >> -- > >> 2.27.0 > >> >
Hi, Bjorn, What's your opinion? This patch indeed fixes a problem on LoongArch, and in theory other non-4K page platforms also need it. If someone disagrees, maybe we can use the old way: provide a LoongArch-specific pcibios_align_resource() at this time. Huacai On Fri, Jun 9, 2023 at 12:21 PM Huacai Chen <chenhuacai@kernel.org> wrote: > > On Fri, Jun 9, 2023 at 11:47 AM bibo, mao <maobibo@loongson.cn> wrote: > > > > > > > > 在 2023/6/9 10:29, Huacai Chen 写道: > > > Hi, Bibo, > > > > > > On Fri, Jun 9, 2023 at 10:20 AM Bibo Mao <maobibo@loongson.cn> wrote: > > >> > > >> 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, > > >> here extra option PCI_MEMRES_PAGE_ALIGN is added, it can be enabled by > > >> different architectures. > > >> > > >> Signed-off-by: Bibo Mao <maobibo@loongson.cn> > > >> --- > > >> Change history > > >> v4: add extra kernel option PCI_MEMRES_PAGE_ALIGN to set memory resource > > >> page aligned. > > >> > > >> v3: move alignment requirement to generic pci code > > >> > > >> v2: add pci resource alignment requirement in arch specified function > > >> pcibios_align_resource on arm64/LoongArch platforms > > >> > > >> --- > > >> arch/loongarch/Kconfig | 1 + > > >> drivers/pci/Kconfig | 3 +++ > > >> drivers/pci/setup-res.c | 7 +++++++ > > >> 3 files changed, 11 insertions(+) > > >> > > >> diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig > > >> index d38b066fc931..65b2f6ba9f8e 100644 > > >> --- a/arch/loongarch/Kconfig > > >> +++ b/arch/loongarch/Kconfig > > >> @@ -142,6 +142,7 @@ config LOONGARCH > > >> select PCI_LOONGSON > > >> select PCI_MSI_ARCH_FALLBACKS > > >> select PCI_QUIRKS > > >> + select PCI_MEMRES_PAGE_ALIGN > > >> select PERF_USE_VMALLOC > > >> select RTC_LIB > > >> select SMP > > >> diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig > > >> index 9309f2469b41..9be5f85ff9dc 100644 > > >> --- a/drivers/pci/Kconfig > > >> +++ b/drivers/pci/Kconfig > > >> @@ -128,6 +128,9 @@ config PCI_LOCKLESS_CONFIG > > >> config PCI_BRIDGE_EMUL > > >> bool > > >> > > >> +config PCI_MEMRES_PAGE_ALIGN > > >> + bool > > >> + > > >> config PCI_IOV > > >> bool "PCI IOV support" > > >> select PCI_ATS > > >> diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c > > >> index 967f9a758923..6ad76734a670 100644 > > >> --- a/drivers/pci/setup-res.c > > >> +++ b/drivers/pci/setup-res.c > > >> @@ -339,6 +339,13 @@ int pci_assign_resource(struct pci_dev *dev, int resno) > > >> return -EINVAL; > > >> } > > >> > > >> +#ifdef CONFIG_PCI_MEMRES_PAGE_ALIGN > > >> + /* > > >> + * force minimum page alignment for vfio pci usage > > >> + */ > > >> + if (res->flags & IORESOURCE_MEM) > > >> + align = max_t(resource_size_t, PAGE_SIZE, align); > > >> +#endif > > > Does this really have its effect? The common version of > > > pcibios_align_resource() simply returns res->start, and doesn't care > > > about the 'align' parameter. > > yes, it works. The is output of command " lspci -vvv | grep Region" on my > > 3C5000+7A2000 box. After the patch base address of all pci mem resource > > is aligned with 16K. > > > > output without the patch: > > Region 0: Memory at e0045240000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045248000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045250000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045258000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045260000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045271400 (64-bit, non-prefetchable) [size=256] > > Region 2: Memory at e0040000000 (64-bit, non-prefetchable) [size=64M] > > Region 4: Memory at e0045200000 (64-bit, non-prefetchable) [size=64K] > > Region 0: Memory at e0045210000 (64-bit, non-prefetchable) [size=64K] > > Region 0: Memory at e0045220000 (64-bit, non-prefetchable) [size=64K] > > Region 0: Memory at e0045230000 (64-bit, non-prefetchable) [size=64K] > > Region 5: Memory at e0045271000 (32-bit, non-prefetchable) [size=1K] > > Region 0: Memory at e0045268000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045269000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004526a000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004526b000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004526c000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004526d000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004526e000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004526f000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045270000 (64-bit, non-prefetchable) [size=4K] > > Region 2: Memory at e0044000000 (64-bit, non-prefetchable) [size=16M] > > Region 0: Memory at e0045100000 (64-bit, non-prefetchable) [size=1M] > > Region 0: Memory at e0045000000 (64-bit, non-prefetchable) [size=16K] > > > > out put with the patch: > > Region 0: Memory at e0045240000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045248000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045250000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045258000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045260000 (64-bit, non-prefetchable) [size=32K] > > Region 0: Memory at e0045290000 (64-bit, non-prefetchable) [size=256] > > Region 2: Memory at e0040000000 (64-bit, non-prefetchable) [size=64M] > > Region 4: Memory at e0045200000 (64-bit, non-prefetchable) [size=64K] > > Region 0: Memory at e0045210000 (64-bit, non-prefetchable) [size=64K] > > Region 0: Memory at e0045220000 (64-bit, non-prefetchable) [size=64K] > > Region 0: Memory at e0045230000 (64-bit, non-prefetchable) [size=64K] > > Region 5: Memory at e004528c000 (32-bit, non-prefetchable) [size=1K] > > Region 0: Memory at e0045268000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004526c000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045270000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045274000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045278000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e004527c000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045280000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045284000 (64-bit, non-prefetchable) [size=4K] > > Region 0: Memory at e0045288000 (64-bit, non-prefetchable) [size=4K] > > Region 2: Memory at e0044000000 (64-bit, non-prefetchable) [size=16M] > > Region 0: Memory at e0045100000 (64-bit, non-prefetchable) [size=1M] > > Region 0: Memory at e0045000000 (64-bit, non-prefetchable) [size=16K] > OK, thanks. > > Reviewed-by: Huacai Chen <chenhuacai@loongson.cn> > > > > > Regards > > Bibo, Mao > > > > > > Huacai > > >> size = resource_size(res); > > >> ret = _pci_assign_resource(dev, resno, size, align); > > >> > > >> -- > > >> 2.27.0 > > >> > >
On Fri, Jun 09, 2023 at 10:20:47AM +0800, Bibo Mao wrote: > 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. Shouldn't we also select this new Kconfig option on arm64 then? Will
在 2023/6/16 17:31, Will Deacon 写道: > On Fri, Jun 09, 2023 at 10:20:47AM +0800, Bibo Mao wrote: >> 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. > > Shouldn't we also select this new Kconfig option on arm64 then? OK, will add this option on arm64 also in next version. Regards Bibo, Mao > > Will
diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig index d38b066fc931..65b2f6ba9f8e 100644 --- a/arch/loongarch/Kconfig +++ b/arch/loongarch/Kconfig @@ -142,6 +142,7 @@ config LOONGARCH select PCI_LOONGSON select PCI_MSI_ARCH_FALLBACKS select PCI_QUIRKS + select PCI_MEMRES_PAGE_ALIGN select PERF_USE_VMALLOC select RTC_LIB select SMP diff --git a/drivers/pci/Kconfig b/drivers/pci/Kconfig index 9309f2469b41..9be5f85ff9dc 100644 --- a/drivers/pci/Kconfig +++ b/drivers/pci/Kconfig @@ -128,6 +128,9 @@ config PCI_LOCKLESS_CONFIG config PCI_BRIDGE_EMUL bool +config PCI_MEMRES_PAGE_ALIGN + bool + config PCI_IOV bool "PCI IOV support" select PCI_ATS diff --git a/drivers/pci/setup-res.c b/drivers/pci/setup-res.c index 967f9a758923..6ad76734a670 100644 --- a/drivers/pci/setup-res.c +++ b/drivers/pci/setup-res.c @@ -339,6 +339,13 @@ int pci_assign_resource(struct pci_dev *dev, int resno) return -EINVAL; } +#ifdef CONFIG_PCI_MEMRES_PAGE_ALIGN + /* + * force minimum page alignment for vfio pci usage + */ + 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);