Changes since v1:
- Dropped PATCH2 as it is no longer needed.
- Move PCI ids to linux/pci_ids.h [Michael]
- Correctly handle "!(pdev->resource[0].flags & IORESOURCE_MEM)" case
[Michael].
Passed through PCI device sometimes misbehave on Gen1 VMs when Hyper-V
DRM driver is also loaded. Looking at IOMEM assignment, we can see e.g.
$ cat /proc/iomem
...
f8000000-fffbffff : PCI Bus 0000:00
f8000000-fbffffff : 0000:00:08.0
f8000000-f8001fff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
...
fe0000000-fffffffff : PCI Bus 0000:00
fe0000000-fe07fffff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
fe0000000-fe07fffff : 2ba2:00:02.0
fe0000000-fe07fffff : mlx4_core
the interesting part is the 'f8000000' region as it is actually the
VM's framebuffer:
$ lspci -v
...
0000:00:08.0 VGA compatible controller: Microsoft Corporation Hyper-V virtual VGA (prog-if 00 [VGA controller])
Flags: bus master, fast devsel, latency 0, IRQ 11
Memory at f8000000 (32-bit, non-prefetchable) [size=64M]
...
Recently merged commit a0ab5abced55 ("drm/hyperv : Removing the restruction of
VRAM allocation with PCI bar size") improved the situation as resources,
reserved through vmbus_allocate_mmio() can't be allocated twice:
...
f8000000-fffbffff : PCI Bus 0000:00
f8000000-fbffffff : 0000:00:08.0
f8000000-f8001fff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
f8100000-f88fffff : 5620e0c7-8062-4dce-aeb7-520c7ef76171
...
Always reserve FB region on Gen1 VMs (PATCH2) and make sure we never allocate
anything besides framebuffer from there (PATCH3).
Vitaly Kuznetsov (3):
PCI: Move PCI_VENDOR_ID_MICROSOFT/PCI_DEVICE_ID_HYPERV_VIDEO
definitions to pci_ids.h
Drivers: hv: Always reserve framebuffer region for Gen1 VMs
Drivers: hv: Never allocate anything besides framebuffer from
framebuffer memory region
drivers/gpu/drm/hyperv/hyperv_drm_drv.c | 3 -
drivers/hv/vmbus_drv.c | 56 ++++++++++++++-----
.../net/ethernet/microsoft/mana/gdma_main.c | 4 --
drivers/video/fbdev/hyperv_fb.c | 4 --
include/linux/pci_ids.h | 3 +
5 files changed, 44 insertions(+), 26 deletions(-)
--
2.37.1
There are already three places in kernel which define PCI_VENDOR_ID_MICROSOFT
and two for PCI_DEVICE_ID_HYPERV_VIDEO and there's a need to use these
from core Vmbus code. Move the defines where they belong.
No functional change.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/gpu/drm/hyperv/hyperv_drm_drv.c | 3 ---
drivers/net/ethernet/microsoft/mana/gdma_main.c | 4 ----
drivers/video/fbdev/hyperv_fb.c | 4 ----
include/linux/pci_ids.h | 3 +++
4 files changed, 3 insertions(+), 11 deletions(-)
vmbus_reserve_fb() tries reserving framebuffer region iff
'screen_info.lfb_base' is set. Gen2 VMs seem to have it set by EFI fb
(or, in some edge cases like kexec, the address where the buffer was
moved, see
https://lore.kernel.org/all/20201014092429.1415040-1-kasong@redhat.com/)
but on Gen1 VM it depends on bootloader behavior. With grub, it depends
on 'gfxpayload=' setting but in some cases it is observed to be zero.
Relying on 'screen_info.lfb_base' to reserve framebuffer region is
risky. Instead, it is possible to get the address from the dedicated
PCI device which is always present.
Check for legacy PCI video device presence and reserve the whole
region for framebuffer on Gen1 VMs.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/hv/vmbus_drv.c | 46 +++++++++++++++++++++++++++++-------------
1 file changed, 32 insertions(+), 14 deletions(-)
@@ -2262,26 +2263,43 @@ static int vmbus_acpi_remove(struct acpi_device *device)staticvoidvmbus_reserve_fb(void){-intsize;+resource_size_tstart=0,size;+structpci_dev*pdev;++if(efi_enabled(EFI_BOOT)){+/* Gen2 VM: get FB base from EFI framebuffer */+start=screen_info.lfb_base;+size=max_t(__u32,screen_info.lfb_size,0x800000);+}else{+/* Gen1 VM: get FB base from PCI */+pdev=pci_get_device(PCI_VENDOR_ID_MICROSOFT,+PCI_DEVICE_ID_HYPERV_VIDEO,NULL);+if(!pdev)+return;++if(pdev->resource[0].flags&IORESOURCE_MEM){+start=pci_resource_start(pdev,0);+size=pci_resource_len(pdev,0);+}++/*+*ReleasethePCIdevicesohyperv_drmorhyperv_fbdrivercan+*grabitlater.+*/+pci_dev_put(pdev);+}++if(!start)+return;+/**Makeaclaimfortheframebufferintheresourcetreeunderthe*firstnode,whichwillbetheonebelow4GB.Thelengthseemsto*beunderreported,particularlyinaGeneration1VM.Sostartout*reservingalargerareaandmakeitsmalleruntilitsucceeds.*/--if(screen_info.lfb_base){-if(efi_enabled(EFI_BOOT))-size=max_t(__u32,screen_info.lfb_size,0x800000);-else-size=max_t(__u32,screen_info.lfb_size,0x4000000);--for(;!fb_mmio&&(size>=0x100000);size>>=1){-fb_mmio=__request_region(hyperv_mmio,-screen_info.lfb_base,size,-fb_mmio_name,0);-}-}+for(;!fb_mmio&&(size>=0x100000);size>>=1)+fb_mmio=__request_region(hyperv_mmio,start,size,fb_mmio_name,0);}/**
Passed through PCI device sometimes misbehave on Gen1 VMs when Hyper-V
DRM driver is also loaded. Looking at IOMEM assignment, we can see e.g.
$ cat /proc/iomem
...
f8000000-fffbffff : PCI Bus 0000:00
f8000000-fbffffff : 0000:00:08.0
f8000000-f8001fff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
...
fe0000000-fffffffff : PCI Bus 0000:00
fe0000000-fe07fffff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
fe0000000-fe07fffff : 2ba2:00:02.0
fe0000000-fe07fffff : mlx4_core
the interesting part is the 'f8000000' region as it is actually the
VM's framebuffer:
$ lspci -v
...
0000:00:08.0 VGA compatible controller: Microsoft Corporation Hyper-V virtual VGA (prog-if 00 [VGA controller])
Flags: bus master, fast devsel, latency 0, IRQ 11
Memory at f8000000 (32-bit, non-prefetchable) [size=64M]
...
hv_vmbus: registering driver hyperv_drm
hyperv_drm 5620e0c7-8062-4dce-aeb7-520c7ef76171: [drm] Synthvid Version major 3, minor 5
hyperv_drm 0000:00:08.0: vgaarb: deactivate vga console
hyperv_drm 0000:00:08.0: BAR 0: can't reserve [mem 0xf8000000-0xfbffffff]
hyperv_drm 5620e0c7-8062-4dce-aeb7-520c7ef76171: [drm] Cannot request framebuffer, boot fb still active?
Note: "Cannot request framebuffer" is not a fatal error in
hyperv_setup_gen1() as the code assumes there's some other framebuffer
device there but we actually have some other PCI device (mlx4 in this
case) config space there!
The problem appears to be that vmbus_allocate_mmio() can allocate from
the reserved framebuffer region (fb_overlap_ok), however, if the
request to allocate MMIO comes from some other device before
framebuffer region is taken, it can happily use framebuffer region for
it. Note, Gen2 VMs are usually unaffected by the issue because
framebuffer region is already taken by EFI fb (in case kernel supports
it) but Gen1 VMs may have this region unclaimed by the time Hyper-V PCI
pass-through driver tries allocating MMIO space if Hyper-V DRM/FB drivers
load after it. Devices can be brought up in any sequence so let's
resolve the issue by always ignoring 'fb_mmio' region for non-FB
requests, even if the region is unclaimed.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/hv/vmbus_drv.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
@@ -2366,6 +2366,14 @@ int vmbus_allocate_mmio(struct resource **new, struct hv_device *device_obj,range_max=iter->end;start=(range_min+align-1)&~(align-1);for(;start+size-1<=range_max;start+=align){+end=start+size-1;++/* Skip the whole fb_mmio region if not fb_overlap_ok */+if(!fb_overlap_ok&&fb_mmio&&+(((start>=fb_mmio->start)&&(start<=fb_mmio->end))||+((end>=fb_mmio->start)&&(end<=fb_mmio->end))))+continue;+shadow=__request_region(iter,start,size,NULL,IORESOURCE_BUSY);if(!shadow)
From: Michael Kelley (LINUX) <hidden> Date: 2022-08-25 14:37:55
From: Vitaly Kuznetsov <vkuznets@redhat.com> Sent: Thursday, August 25, 2022 2:00 AM
quoted hunk
There are already three places in kernel which define PCI_VENDOR_ID_MICROSOFT
and two for PCI_DEVICE_ID_HYPERV_VIDEO and there's a need to use these
from core Vmbus code. Move the defines where they belong.
No functional change.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/gpu/drm/hyperv/hyperv_drm_drv.c | 3 ---
drivers/net/ethernet/microsoft/mana/gdma_main.c | 4 ----
drivers/video/fbdev/hyperv_fb.c | 4 ----
include/linux/pci_ids.h | 3 +++
4 files changed, 3 insertions(+), 11 deletions(-)
From: Michael Kelley (LINUX) <hidden> Date: 2022-08-25 14:44:37
From: Vitaly Kuznetsov <vkuznets@redhat.com> Sent: Thursday, August 25, 2022 2:00 AM
vmbus_reserve_fb() tries reserving framebuffer region iff
'screen_info.lfb_base' is set. Gen2 VMs seem to have it set by EFI fb
Just so I'm clear, by "EFI fb" you mean the EFI layer code that sets
up the frame buffer before the Linux kernel ever boots, right?
You are not referring to the Linux kernel EFI framebuffer
driver, which may or may not be configured in the kernel.
quoted hunk
(or, in some edge cases like kexec, the address where the buffer was
moved, see https://lore.kernel.org/all/20201014092429.1415040-1-kasong@redhat.com/
but on Gen1 VM it depends on bootloader behavior. With grub, it depends
on 'gfxpayload=' setting but in some cases it is observed to be zero.
Relying on 'screen_info.lfb_base' to reserve framebuffer region is
risky. Instead, it is possible to get the address from the dedicated
PCI device which is always present.
Check for legacy PCI video device presence and reserve the whole
region for framebuffer on Gen1 VMs.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/hv/vmbus_drv.c | 46 +++++++++++++++++++++++++++++-------------
1 file changed, 32 insertions(+), 14 deletions(-)
@@ -2262,26 +2263,43 @@ static int vmbus_acpi_remove(struct acpi_device *device)staticvoidvmbus_reserve_fb(void){-intsize;+resource_size_tstart=0,size;+structpci_dev*pdev;++if(efi_enabled(EFI_BOOT)){+/* Gen2 VM: get FB base from EFI framebuffer */+start=screen_info.lfb_base;+size=max_t(__u32,screen_info.lfb_size,0x800000);+}else{+/* Gen1 VM: get FB base from PCI */+pdev=pci_get_device(PCI_VENDOR_ID_MICROSOFT,+PCI_DEVICE_ID_HYPERV_VIDEO,NULL);+if(!pdev)+return;++if(pdev->resource[0].flags&IORESOURCE_MEM){+start=pci_resource_start(pdev,0);+size=pci_resource_len(pdev,0);+}++/*+*ReleasethePCIdevicesohyperv_drmorhyperv_fbdrivercan+*grabitlater.+*/+pci_dev_put(pdev);+}++if(!start)+return;+/**Makeaclaimfortheframebufferintheresourcetreeunderthe*firstnode,whichwillbetheonebelow4GB.Thelengthseemsto*beunderreported,particularlyinaGeneration1VM.Sostartout*reservingalargerareaandmakeitsmalleruntilitsucceeds.*/--if(screen_info.lfb_base){-if(efi_enabled(EFI_BOOT))-size=max_t(__u32,screen_info.lfb_size,0x800000);-else-size=max_t(__u32,screen_info.lfb_size,0x4000000);--for(;!fb_mmio&&(size>=0x100000);size>>=1){-fb_mmio=__request_region(hyperv_mmio,-screen_info.lfb_base,size,-fb_mmio_name,0);-}-}+for(;!fb_mmio&&(size>=0x100000);size>>=1)+fb_mmio=__request_region(hyperv_mmio,start,size,fb_mmio_name,0);}/**--
From: Michael Kelley (LINUX) <hidden> Date: 2022-08-25 15:30:00
From: Vitaly Kuznetsov <vkuznets@redhat.com> Sent: Thursday, August 25, 2022 2:00 AM
Passed through PCI device sometimes misbehave on Gen1 VMs when Hyper-V
DRM driver is also loaded. Looking at IOMEM assignment, we can see e.g.
$ cat /proc/iomem
...
f8000000-fffbffff : PCI Bus 0000:00
f8000000-fbffffff : 0000:00:08.0
f8000000-f8001fff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
...
fe0000000-fffffffff : PCI Bus 0000:00
fe0000000-fe07fffff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
fe0000000-fe07fffff : 2ba2:00:02.0
fe0000000-fe07fffff : mlx4_core
the interesting part is the 'f8000000' region as it is actually the
VM's framebuffer:
$ lspci -v
...
0000:00:08.0 VGA compatible controller: Microsoft Corporation Hyper-V virtual VGA
(prog-if 00 [VGA controller])
Flags: bus master, fast devsel, latency 0, IRQ 11
Memory at f8000000 (32-bit, non-prefetchable) [size=64M]
...
hv_vmbus: registering driver hyperv_drm
hyperv_drm 5620e0c7-8062-4dce-aeb7-520c7ef76171: [drm] Synthvid Version major 3, minor 5
hyperv_drm 0000:00:08.0: vgaarb: deactivate vga console
hyperv_drm 0000:00:08.0: BAR 0: can't reserve [mem 0xf8000000-0xfbffffff]
hyperv_drm 5620e0c7-8062-4dce-aeb7-520c7ef76171: [drm] Cannot request framebuffer, boot fb still active?
Note: "Cannot request framebuffer" is not a fatal error in
hyperv_setup_gen1() as the code assumes there's some other framebuffer
device there but we actually have some other PCI device (mlx4 in this
case) config space there!
My apologies for not getting around to commenting on the previous
version of this patch. The function hyperv_setup_gen1() and the
"Cannot request framebuffer" message have gone away as of
commit a0ab5abced55.
The problem appears to be that vmbus_allocate_mmio() can allocate from
the reserved framebuffer region (fb_overlap_ok), however, if the
request to allocate MMIO comes from some other device before
framebuffer region is taken, it can happily use framebuffer region for
it.
Interesting. I had never looked at the details of vmbus_allocate_mmio().
The semantics one might assume of a parameter named "fb_overlap_ok"
aren't implemented because !fb_overlap_ok essentially has no effect. The
existing semantics are really "prefer_fb_overlap". This patch implements
the expected and needed semantics, which is to not allocate from the frame
buffer space when !fb_overlap_ok.
If that's an accurate high level summary, maybe this commit message
could describe it that way? The other details you provide about what can
go wrong should still be included as well.
quoted hunk
Note, Gen2 VMs are usually unaffected by the issue because
framebuffer region is already taken by EFI fb (in case kernel supports
it) but Gen1 VMs may have this region unclaimed by the time Hyper-V PCI
pass-through driver tries allocating MMIO space if Hyper-V DRM/FB drivers
load after it. Devices can be brought up in any sequence so let's
resolve the issue by always ignoring 'fb_mmio' region for non-FB
requests, even if the region is unclaimed.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/hv/vmbus_drv.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
On Thu, Aug 25, 2022 at 11:00:22AM +0200, Vitaly Kuznetsov wrote:
There are already three places in kernel which define PCI_VENDOR_ID_MICROSOFT
and two for PCI_DEVICE_ID_HYPERV_VIDEO and there's a need to use these
from core Vmbus code. Move the defines where they belong.
No functional change.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
From: Vitaly Kuznetsov <vkuznets@redhat.com> Sent: Thursday, August 25, 2022 2:00 AM
quoted
Passed through PCI device sometimes misbehave on Gen1 VMs when Hyper-V
DRM driver is also loaded. Looking at IOMEM assignment, we can see e.g.
$ cat /proc/iomem
...
f8000000-fffbffff : PCI Bus 0000:00
f8000000-fbffffff : 0000:00:08.0
f8000000-f8001fff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
...
fe0000000-fffffffff : PCI Bus 0000:00
fe0000000-fe07fffff : bb8c4f33-2ba2-4808-9f7f-02f3b4da22fe
fe0000000-fe07fffff : 2ba2:00:02.0
fe0000000-fe07fffff : mlx4_core
the interesting part is the 'f8000000' region as it is actually the
VM's framebuffer:
$ lspci -v
...
0000:00:08.0 VGA compatible controller: Microsoft Corporation Hyper-V virtual VGA
(prog-if 00 [VGA controller])
Flags: bus master, fast devsel, latency 0, IRQ 11
Memory at f8000000 (32-bit, non-prefetchable) [size=64M]
...
hv_vmbus: registering driver hyperv_drm
hyperv_drm 5620e0c7-8062-4dce-aeb7-520c7ef76171: [drm] Synthvid Version major 3, minor 5
hyperv_drm 0000:00:08.0: vgaarb: deactivate vga console
hyperv_drm 0000:00:08.0: BAR 0: can't reserve [mem 0xf8000000-0xfbffffff]
hyperv_drm 5620e0c7-8062-4dce-aeb7-520c7ef76171: [drm] Cannot request framebuffer, boot fb still active?
Note: "Cannot request framebuffer" is not a fatal error in
hyperv_setup_gen1() as the code assumes there's some other framebuffer
device there but we actually have some other PCI device (mlx4 in this
case) config space there!
My apologies for not getting around to commenting on the previous
version of this patch. The function hyperv_setup_gen1() and the
"Cannot request framebuffer" message have gone away as of
commit a0ab5abced55.
True, will fix!
quoted
The problem appears to be that vmbus_allocate_mmio() can allocate from
the reserved framebuffer region (fb_overlap_ok), however, if the
request to allocate MMIO comes from some other device before
framebuffer region is taken, it can happily use framebuffer region for
it.
Interesting. I had never looked at the details of vmbus_allocate_mmio().
The semantics one might assume of a parameter named "fb_overlap_ok"
aren't implemented because !fb_overlap_ok essentially has no effect. The
existing semantics are really "prefer_fb_overlap". This patch implements
the expected and needed semantics, which is to not allocate from the frame
buffer space when !fb_overlap_ok.
If that's an accurate high level summary, maybe this commit message
could describe it that way? The other details you provide about what can
go wrong should still be included as well.
That's acually a very good summary! Let me update the commit message,
I'll be sending out v3 shortly.
quoted
Note, Gen2 VMs are usually unaffected by the issue because
framebuffer region is already taken by EFI fb (in case kernel supports
it) but Gen1 VMs may have this region unclaimed by the time Hyper-V PCI
pass-through driver tries allocating MMIO space if Hyper-V DRM/FB drivers
load after it. Devices can be brought up in any sequence so let's
resolve the issue by always ignoring 'fb_mmio' region for non-FB
requests, even if the region is unclaimed.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/hv/vmbus_drv.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
From: Vitaly Kuznetsov <vkuznets@redhat.com> Sent: Thursday, August 25, 2022 2:00 AM
quoted
vmbus_reserve_fb() tries reserving framebuffer region iff
'screen_info.lfb_base' is set. Gen2 VMs seem to have it set by EFI fb
Just so I'm clear, by "EFI fb" you mean the EFI layer code that sets
up the frame buffer before the Linux kernel ever boots, right?
You are not referring to the Linux kernel EFI framebuffer
driver, which may or may not be configured in the kernel.
My very shallow understanding is that initially, screen_info comes from
boot_params and this depends on how Linux was booted. Kernel EFI
framebuffer (when enabled), however, gets it first and can modify it
(see efifb_setup()) before we get to analyze it in Vmbus.
quoted
(or, in some edge cases like kexec, the address where the buffer was
moved, see https://lore.kernel.org/all/20201014092429.1415040-1-kasong@redhat.com/
but on Gen1 VM it depends on bootloader behavior. With grub, it depends
on 'gfxpayload=' setting but in some cases it is observed to be zero.
Relying on 'screen_info.lfb_base' to reserve framebuffer region is
risky. Instead, it is possible to get the address from the dedicated
PCI device which is always present.
Check for legacy PCI video device presence and reserve the whole
region for framebuffer on Gen1 VMs.
Signed-off-by: Vitaly Kuznetsov <vkuznets@redhat.com>
---
drivers/hv/vmbus_drv.c | 46 +++++++++++++++++++++++++++++-------------
1 file changed, 32 insertions(+), 14 deletions(-)
@@ -2262,26 +2263,43 @@ static int vmbus_acpi_remove(struct acpi_device *device)staticvoidvmbus_reserve_fb(void){-intsize;+resource_size_tstart=0,size;+structpci_dev*pdev;++if(efi_enabled(EFI_BOOT)){+/* Gen2 VM: get FB base from EFI framebuffer */+start=screen_info.lfb_base;+size=max_t(__u32,screen_info.lfb_size,0x800000);+}else{+/* Gen1 VM: get FB base from PCI */+pdev=pci_get_device(PCI_VENDOR_ID_MICROSOFT,+PCI_DEVICE_ID_HYPERV_VIDEO,NULL);+if(!pdev)+return;++if(pdev->resource[0].flags&IORESOURCE_MEM){+start=pci_resource_start(pdev,0);+size=pci_resource_len(pdev,0);+}++/*+*ReleasethePCIdevicesohyperv_drmorhyperv_fbdrivercan+*grabitlater.+*/+pci_dev_put(pdev);+}++if(!start)+return;+/**Makeaclaimfortheframebufferintheresourcetreeunderthe*firstnode,whichwillbetheonebelow4GB.Thelengthseemsto*beunderreported,particularlyinaGeneration1VM.Sostartout*reservingalargerareaandmakeitsmalleruntilitsucceeds.*/--if(screen_info.lfb_base){-if(efi_enabled(EFI_BOOT))-size=max_t(__u32,screen_info.lfb_size,0x800000);-else-size=max_t(__u32,screen_info.lfb_size,0x4000000);--for(;!fb_mmio&&(size>=0x100000);size>>=1){-fb_mmio=__request_region(hyperv_mmio,-screen_info.lfb_base,size,-fb_mmio_name,0);-}-}+for(;!fb_mmio&&(size>=0x100000);size>>=1)+fb_mmio=__request_region(hyperv_mmio,start,size,fb_mmio_name,0);}/**--